ffmpeg linked to mll fails when calling avcodec_find_encoder

ffmpeg linked to mll fails when calling avcodec_find_encoder

Anonymous
Not applicable
4,381 Views
12 Replies
Message 1 of 13

ffmpeg linked to mll fails when calling avcodec_find_encoder

Anonymous
Not applicable

I've been trying to use my own build of ffmpeg with my .mll plugin, but I keep getting NULL when attempting to find the encoder by the codec ID. Here is part of the code:

 

#include <windows.h>
#include <Lmcons.h>
#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>

extern "C"
{
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#include <libavcodec\avcodec.h>
#include <libavformat\avformat.h>
}

#pragma comment(lib, "avcodec.lib")
#pragma comment (lib, "avformat.lib")

using namespace std;

DeclareSimpleCommand(h264_test, "Autodesk", "2016");

MStatus h264_test::doIt(const MArgList&)
{
av_register_all(); AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_H264); if (!codec) {     MGlobal::displayInfo("Codec not found");
} else {
MGlobal::displayInfo("H264 codec was found");
}
return MS::kSuccess;
}

NOTES:

- I have tried the compiled ffmpeg from Zeranoe, tried my own compiled version of ffmpeg and the one provided by Autodesk here.

- I tried compiling with Microsoft Visual Studio 2013 and with Microsoft Visual Studio 2012 Update 4 (suggested by Autodesk on Maya 2016 SKD Documentation)

- I have placed my compiled .mll and ffmpeg dlls in "C:\Program Files\Autodesk\Maya2016\bin" and made sure the environment variable PATH includes that directory.

- When I compile the same code as a Windows executable (.exe), the program is able to find the codec and output the message "H264 codec was found", but for some reason Maya doesn't seem to allow my .mll plugin to execute the appropriate calls to ffmpeg dlls.

- This issue doesn't happen with h264 only, any other codec ID I try returns NULL, such as AV_CODEC_ID_MPEG2VIDEO.

0 Likes
Accepted solutions (1)
4,382 Views
12 Replies
Replies (12)
Message 2 of 13

cheng_xi_li
Autodesk Support
Autodesk Support

Hi Iclibardi,

 

Can you to create a module file for your plug-in?

 

I think it is better to have a module file when you have multiple files for your plugin. 

 

For example, Bifrost is a plugin with Maya, you can find the bifrost.mod in maya\modules folder. You can use it as reference for your mod file.

 

You can check this document for more details.

 

Yours,

Li

 

0 Likes
Message 3 of 13

Anonymous
Not applicable

Thanks for the prompt response Li!

I've created a module file with the content:

 

 

+ h264plugin 1.0 ..\plug-ins\h264plugin

 

Here's the plugin directory tree:

 

 

C:\Program Files\Autodesk\Maya2016\plug-ins\h264plugin\plug-ins

    |_ avcodec-57.dll
    |_ avdevice-57.dll
    |_ avfilter-6.dll
    |_ avformat-57.dll
    |_ avutil-55.dll
    |_ ffmpeg.exe
    |_ ffplay.exe
    |_ ffprobe.exe
    |_ postproc-54.dll
    |_ swresample-2.dll
    |_ swscale-4.dll
    |_ h264plugin.exp
    |_ h264plugin.ilk
    |_ h264plugin.lib
    |_ h264plugin.mll
    |_ h264plugin.pdb

 

Based on the Module limitations described here, I placed duplicates of .dll and .exe files inside Maya's bin directory:

 

C:\Program Files\Autodesk\Maya2016\bin

    |_ avcodec-57.dll
    |_ avdevice-57.dll
    |_ avfilter-6.dll
    |_ avformat-57.dll
    |_ avutil-55.dll
    |_ ffmpeg.exe
    |_ ffplay.exe
    |_ ffprobe.exe
    |_ postproc-54.dll
    |_ swresample-2.dll
    |_ swscale-4.dll

 

 

I opened Maya and ran the following commands to make sure everything was correctly setup:

getenv PATH

// Result: C:/Program Files/Autodesk/Maya2016/bin/Cg;
           C:/Program Files/Autodesk/Maya2016/bin;
           C:/Python27/;
           C:/Python27/Scripts;
           C:/Windows/system32;
           C:/Windows;
           C:/Program Files/Autodesk/Maya2016/bin;
           C:/Program Files/Autodesk/Maya2016/plug-ins/bifrost/bin;
           C:/Program Files/Autodesk/mentalrayForMaya2016/bin;
           C:/Program Files/Autodesk/Maya2016/plug-ins/substance/bin;
           C:/Program Files/Autodesk/Maya2016/plug-ins/xgen/bin //

... and I ran this:

getModulePath -moduleName "h264plugin"

// Result: C:/Program Files/Autodesk/Maya2016/plug-ins/h264plugin// 

 

Unfortunately, loading and running the plugin still gives me: "Codec not found".

 

Here is where I think the conflict is happening:

1) Maya's documentation says that dinamic libraries should be placed in Maya's bin directory.

2) According to this post, I'm supposed to copy the FFMPEG .dlls and .exe to the same directory of my compiled binary.

 

So where should I place those .dll and .exe files? Do I have to write code inside my .mll file to call them from a hard coded path?

 

It's worth mentioning that when I compiled the plugin as an .exe everything works, because I place the FFMPEG dlls inside the same directory and this situation doesn't depend on Maya to load an .mll, then make the calls. The .exe spawns a new process and it is able to call the linked dlls with no problems.

0 Likes
Message 4 of 13

cheng_xi_li
Autodesk Support
Autodesk Support
Hi,

I am traveling now, I'll look into it next week.

Yours,
Li
0 Likes
Message 5 of 13

Anonymous
Not applicable

No problem Li. Thanks for you support so far!

0 Likes
Message 6 of 13

cheng_xi_li
Autodesk Support
Autodesk Support

Hi,

 

I did some tests with your code earlier and I was able to get the codec loaded.

ffmpeg.png

Here are my steps:

 

I've created a ffmpegPlugin.mod in the modules folder of Maya.

 

 

+ ffmpegPlugin 1.0 ../plug-ins/ffmpegPlugin
PATH+:=bin

 

Then, I've created a ffmpegPlugin folder in the plug-ins folder. The content is below:

 

 

 Volume in drive C is System
 Volume Serial Number is 0C38-9B4F

 Directory of C:\Program Files\Autodesk\Maya2016\plug-ins\ffmpegPlugin

2016/03/30  14:16    <DIR>          .
2016/03/30  14:16    <DIR>          ..
2016/03/30  13:55    <DIR>          bin
2016/03/30  14:16                 0 folder.txt
2016/03/30  14:04    <DIR>          plug-ins
               1 File(s)              0 bytes

 Directory of C:\Program Files\Autodesk\Maya2016\plug-ins\ffmpegPlugin\bin

2016/03/30  13:55    <DIR>          .
2016/03/30  13:55    <DIR>          ..
2016/03/27  11:20        30,254,592 avcodec-57.dll
2016/03/27  11:20         1,429,504 avdevice-57.dll
2016/03/27  11:20         4,348,416 avfilter-6.dll
2016/03/27  11:20         5,353,472 avformat-57.dll
2016/03/27  11:20           591,872 avutil-55.dll
2016/03/27  11:20           492,544 ffmpeg.exe
2016/03/27  11:20           555,008 ffplay.exe
2016/03/27  11:20           162,816 ffprobe.exe
2016/03/27  11:20           121,856 postproc-54.dll
2016/03/27  11:20           357,888 swresample-2.dll
2016/03/27  11:20           916,480 swscale-4.dll
              11 File(s)     44,584,448 bytes

 Directory of C:\Program Files\Autodesk\Maya2016\plug-ins\ffmpegPlugin\plug-ins

2016/03/30  14:04    <DIR>          .
2016/03/30  14:04    <DIR>          ..
2016/03/30  14:03            69,120 ffmpegPlugin.mll
               1 File(s)         69,120 bytes

     Total Files Listed:
              13 File(s)     44,653,568 bytes
               8 Dir(s)   6,381,305,856 bytes free

 

I've put ffmpeg library in the ffmpegPlugin/bin folder and plugin in the plug-ins folder. The ffmpeg library I've used is download from https://ffmpeg.zeranoe.com/builds/ . It is shared-win64 build. 

 

Hope it helps!

 

Yours,

Li

 

0 Likes
Message 7 of 13

Anonymous
Not applicable

Hi Li,

 

Thank you so much for taking the time to compile the code and perform some tests!


I followed your steps and recreated the Visual Studio project 3 times in my Desktop and twice in my laptop. I used the Zeranoe shared-win64 ffmpeg build you provided. Unfortunately, none of the .mll plugins generated on my Windows machines could load the codec.

 

I'm sorry to provide so much detail, but would you please confirm if your Visual Studio project matches this setup:

 

 

General
	Target Extension = .mll
	Configuration Type = Dynamic Library (.dll)

VC++ Directories
	Include Directories = C:\Program Files\Autodesk\Maya2016\include;$(ProjectDir)\ffmpeg\dev\include\
	Library Directories = C:\Program Files\Autodesk\Maya2016\lib;$(ProjectDir)\ffmpeg\dev\lib\

C/C++ 
	> General
		Additional Include Directories = C:\Program Files\Autodesk\Maya2016\include;$(ProjectDir)\ffmpeg\dev\include\

	> Optimization
		Optimization: Disabled (/Od)

	> Preprocessor 
		Preprocessor Definitions:
		_WINDLL;_DEBUG;WIN32;_WIN64;NDEBUG;_WINDOWS;_USRDLL;NT_PLUGIN;REQUIRE_IOSTREAM

	> Code Generation
		Runtime Library = Multi-threaded Debug DLL (/MDd)

	> Language
		Enable Run-Time Type Information = Yes (/GR)

	> CommandLine
		Additional Options:
			/I "C:\Program Files\Autodesk\Maya2016\include" 
			/I $(ProjectDir)\ffmpeg\dev\include\ 

Linker
	> General
		Aditional Library Directories:
		    C:\Program Files\Autodesk\Maya2016\lib;C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib\x64;$(ProjectDir)\ffmpeg\dev\include\
		
		Enable Incremental Linking = Yes (/INCREMENTAL)

	> Input
		Additional Dependencies:
		    kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;
		    Foundation.lib;OpenMaya.lib;OpenMayaUI.lib;OpenMayaAnim.lib;OpenMayaFX.lib;OpenMayaRender.lib;Image.lib;OpenGL32.Lib;avcodec.lib;avdevice.lib;avfilter.lib;avformat.lib;avutil.lib;postproc.lib;swresample.lib;swscale.lib

	> System
		SubSystem = Windows (/SUBSYSTEM:WINDOWS)
	
	> Advanced
		Randomized Base Address = No (/DYNAMICBASE:NO)
	
	> CommandLine
		Additional Options:
		    /LIBPATH:"C:\Program Files\Autodesk\Maya2016\lib;$(ProjectDir)\ffmpeg\dev\include\"
		    /export:initializePlugin /export:uninitializePlugin

 

 

Again, thank you for all your support so far!

 

Sincerely,
Lucas

0 Likes
Message 8 of 13

cheng_xi_li
Autodesk Support
Autodesk Support

Hi Iclibardi,

 

I pasted your sample code into the helloWorldCmd sample for testing. I've added addtional include path and library path for FFMpeg and changed nothing else.

 

If you followed my method of creating plug-in module and it still doesn't work, could you please send me your plug-in project (both solution and binary), module file and the whole plug-in module folder itself? I would like to test it on my computer.

 

Yours,

Li

0 Likes
Message 9 of 13

Anonymous
Not applicable

Hi Li,

 

I tried using the helloWorldCmd as a template as well. I had the ffmpegPlugin.mod file in Maya's modules directory for all tests I performed.

Still no luck on loading the codec, unfortunately.

 

I attached the Visual Studio project with binaries and libraries, as well as all the content in my modules folder.

 

Sincerely,

Lucas

0 Likes
Message 10 of 13

cheng_xi_li
Autodesk Support
Autodesk Support
Accepted solution

Hi Iclibardi,

 

I am having the same issue on my home computer. I did test on my work computer yesterday and it was fine. 

 

However, I think the path isn't the issue here. I tried to LoadLibrary in the runtime, and it worked.

 

 

//-
// ==========================================================================
// Copyright 1995,2006,2008 Autodesk, Inc. All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk
// license agreement provided at the time of installation or download,
// or which otherwise accompanies this software in either electronic
// or hard copy form.
// ==========================================================================
//+

// Example Plugin: helloWorldCmd.cpp
//
// This plugin uses DeclareSimpleCommand macro
// to do the necessary initialization for a simple command
// plugin.
//
// Once this plugin is loaded by the Plugin Manager,
// it can be run from the Maya command line (i.e. script
// editor) like this:
//
//    helloWorld
//
// which will simply print the following message
// in the Output Window:
//
//    Hello World

#include <windows.h>
#include <maya/MSimple.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>

extern "C"
{
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS
#endif
#include <libavcodec\avcodec.h>
#include <libavformat\avformat.h>
}

#pragma comment(lib, "avcodec.lib")
#pragma comment (lib, "avformat.lib")

using namespace std;

DeclareSimpleCommand(ffmpegPlugin, "Autodesk", "2016");

typedef void (__cdecl *lib_av_register_all)(void);
typedef AVCodec* (__cdecl *lib_avcodec_find_encoder)(AVCodecID);

MStatus ffmpegPlugin::doIt(const MArgList&)
{
	// Path shouldn't be the problem, following code works

	auto hAVCodec = LoadLibrary(TEXT("avcodec-57.dll"));
	auto hAVFormat = LoadLibrary(TEXT("avformat-57.dll"));
	auto my_register_all = (lib_av_register_all)GetProcAddress(hAVFormat, TEXT("av_register_all"));
	auto my_avcodec_find_encoder = (lib_avcodec_find_encoder)GetProcAddress(hAVCodec, TEXT("avcodec_find_encoder"));
	my_register_all();

	AVCodec *codec = my_avcodec_find_encoder(AV_CODEC_ID_H264);
	if (!codec) {
		MGlobal::displayInfo("Codec not found");
	} else {
		MGlobal::displayInfo("H264 codec was found");
	}
	FreeLibrary(hAVCodec);
	FreeLibrary(hAVFormat);
	return MS::kSuccess;
}

 

There maybe some system environment factors inside of it(e.g. Home computer is Windows 10, Work is Windows 7, etc...) which I can not make sure of it.

 

Yours,

Li

0 Likes
Message 11 of 13

Anonymous
Not applicable

Thank you so much for that solution Li, it works here!

I will try explicit linking for now. I really wish I could initiate the library functions during load-time, but it's hard to determine if it's an issue coming from the way Maya loads the .mll or something missing during the compilation in Visual Studio.

 

My computers are also running Windows 7. Could you please confirm which version of the .NET Framework you have installed in your Work Computer?

I have these:

 

Microsoft .NET Framework 4.5 Multi-Targeting Pack

Microsoft .NET Framework 4.5 SDK

Microsoft .NET Framework 4.6.00081

 

Also, which version of Visual Studio? 

I am using Microsoft Visual Studio Ultimate 2012 Version 11.0.61030.00 Update 4.

0 Likes
Message 12 of 13

Anonymous
Not applicable

Accidentally repeated the post.

0 Likes
Message 13 of 13

cheng_xi_li
Autodesk Support
Autodesk Support
Hi,


I am not in office now, I'll confirm the .Net version on Tuesday. But I think it should be .Net 4.0 and .Net 4.5.


I am using Visual Studio Update 4 like you.


Yours,

Li
0 Likes