Log in

View Full Version : libavcodec.dll in VC++


Pages : [1] 2

RahulOP
11th June 2006, 19:05
Hello,
I am trying to write a simple dialog based VC++ program that will encode files with simple options using the libavocodec dlls that were generated when I built FFMPEG. While I can link the dll in my program, I actually have no clue as to how I am supposed to do this. Has anybody done this before ? And if so, is there any sample code that you can show me? Stuff like how to register all the codecs, how to call a codec and then encode a file using this codec.

As of now, this is what I am trying

HMODULE hmodule;
hmodule = AfxLoadLibrary("libavcodec.dll");
if (!AfxLoadLibrary(_T("libavcodec.dll")))
// return 1;
AfxMessageBox("failed to load");
typedef void * (__stdcall *avcodec_register_all)();
GetProcAddress(hmodule,"avcodec_register_all");
void * objptr= avcodec_register_all();

Any help will be greatly appreciated :thanks:

Eretria-chan
12th June 2006, 09:35
Static way:
Link the lib file and use a header file with the functions available in libavcodec. Then you can call them just like any other functions in your program.

Dynamic way:
HMODULE hDll = LoadLibrary("dll here.dll");
(hDll should not be NULL here)
typedef return_type (name_of_type)(arguments here);
name_of_type* p = (name_of_type*)GetProcAddress(hDll, "Name of function");
p(arguments here);
FreeLibrary(hDll);

I hope that information helps. It's standard Dll usage.

RahulOP
12th June 2006, 10:05
Thanks for this :-)

HMODULE hDll = LoadLibrary("libavcodec.dll");
//(hDll should not be NULL here)
typedef void (avcodec_register_all)();
avcodec_register_all* p = (avcodec_register_all*)GetProcAddress(hDll, "avcodec_register_all");
p();
FreeLibrary(hDll);

Built and runs fine. Now, how would I encode video?
(from avcodec.h)

Step 1:Open the codec
int avcodec_open(AVCodecContext *avctx, AVCodec *codec);

Step 2:Encode
int avcodec_encode_video(AVCodecContext *avctx, uint8_t *buf, int buf_size, const AVFrame *pict);

Step 3:Close
int avcodec_close(AVCodecContext *avctx);

Step1:
HMODULE hDll = LoadLibrary("libavcodec.dll");
//(hDll should not be NULL here)
typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p = (avcodec_open*)GetProcAddress(hDll, "avcodec_open");
p(AVCodecContext *avctx, AVCodec *codec);
FreeLibrary(hDll);

Would this be correct so far?

Eretria-chan
12th June 2006, 10:15
Built and runs fine. Now, how would I encode video?
(from avcodec.h)
If you have a lib file to the avcodec dll, then you can do this the static way, which is easier and requires fewer lines.

Step1:
HMODULE hDll = LoadLibrary("libavcodec.dll");
if (hDll == NULL) { /* Show error here. Failure. */ }
typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p = (avcodec_open*)GetProcAddress(hDll, "avcodec_open"); // Find the address of the function
if (p == NULL) { /* Show error here. Failure. */ }
p([pointer to an object of type AVCodecContext], [pointer to something of type AVCodec]); // Call the function
FreeLibrary(hDll); // Unload the dll. WARNING: Unloading the dll will cause all your function pointer to become invalid. Hence, no calls to the dll can be made. Close the DLL only AFTER you're done with it.

Of course, you need to repeat step 2 & 3 to aquire the other functions and call them respectively.
Otherwise, your code should be correct.

RahulOP
12th June 2006, 10:34
If you have a lib file to the avcodec dll, then you can do this the static way, which is easier and requires fewer lines.

I have the avcodec lib file, the dll file and the avocdec.h header file. However, the .h file has no class only plenty of structs.I am not very familiar with dlls and linking(ok i can link the file and build the program)and calling which is why I am struggling so much :stupid: COde samples would be greatly appreciated


HMODULE hDll = LoadLibrary("libavcodec.dll");
if (hDll == NULL) { /* Show error here. Failure. */ }
typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p = (avcodec_open*)GetProcAddress(hDll, "avcodec_open"); // Find the address of the function
if (p == NULL) { /* Show error here. Failure. */ }
p([pointer to an object of type AVCodecContext], [pointer to something of type AVCodec]); // Call the function
FreeLibrary(hDll); // Unload the dll. WARNING: Unloading the dll will cause all your function pointer to become invalid. Hence, no calls to the dll can be made. Close the DLL only AFTER you're done with it.

Of course, you need to repeat step 2 & 3 to aquire the other functions and call them respectively.
Otherwise, your code should be correct.

AVCodecContext *avctx;
AVCodec *codec;
avctx = new AVCodecContext;
codec = new AVCodec;

HMODULE hDll2 = LoadLibrary("libavcodec.dll");

typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll2, "avcodec_open");
p2(avctx,codec);
FreeLibrary(hDll2);

Tried this but I get unhandled exception at p2.

Thanks for all your time and effort though. Really appreciate it

Eretria-chan
12th June 2006, 10:41
I have the avcodec lib file, the dll file and the avocdec.h header file. However, the .h file has no class only plenty of structs.I am not very familiar with dlls and linking(ok i can link the file and build the program)and calling which is why I am struggling so much :stupid: COde samples would be greatly appreciated
Very easy :)
Goto your linker options and specify the path to the lib file for libavcodec. Then include libavcodec.h in the start of your source file. Now you can call the functions in libavcodec as if they were in your own code.

To make an example out of your own code...

AVCodecContext *avctx;
AVCodec *codec;
avctx = new AVCodecContext;
codec = new AVCodec;
avcodec_open(avctx,codec);

That's it! Because you tell the linker the path of the lib file for libavcodec, it will now find the functions avcodec_open in the libavcodec.dll file. If you would not give the path for the lib file, the linker would complain it can't find the function (or symbol) avcodec_open. There are differences between definition and declaration. Declaration are made in headers, often, as in the libavcodec.h file. They simply tell the compiler that these functions exists. It doesn't tell where they are, though. That's the linkers job, which searches and finds where these functions are. And that, is why static linking is preferable to dynamic.

Now, as for your exception, are you getting access violation or something else? Try static linking and see how it works!

RahulOP
12th June 2006, 11:03
I tried the static way and am getting the same access violations as before.
I followed the following steps to the T

In the project Properties Select C/C++ General Tab
and set the path for 'Include directories' seperated by semicolon( i.e path to libavcodec, libavformatl)

In the Linker Tab put the path to Lib files of ffmpeg in the option 'Additional Library Directories path' (i.e the path to libavformat.lib and libavcodec.lib)

In the Linker Tab Put the Library files names to the option 'Additional dependencies' ( avformat.lib avcodec.lib)

Edit: I just passed this line
av_register_all();

Got a whole list of linker errors.60!!!! unresolved external symbol
Which means Static linking is kinda working for me YAY!!!!

p.s.LNK 2001 and LNK 2019


fflib error LNK2019: unresolved external symbol ___udivdi3 referenced in function _MPV_common_end
fflib error LNK2001: unresolved external symbol ___divdi3
fflib error LNK2001: unresolved external symbol ___divdi3
fflib error LNK2019: unresolved external symbol ___divdi3 referenced in function _av_reduce
fflib error LNK2001: unresolved external symbol ___divdi3
fflib error LNK2001: unresolved external symbol ___divdi3
fflib error LNK2001: unresolved external symbol ___divdi3
fflib error LNK2019: unresolved external symbol ___moddi3 referenced in function _av_reduce
fflib error LNK2001: unresolved external symbol ___moddi3
fflib error LNK2001: unresolved external symbol ___moddi3
fflib error LNK2001: unresolved external symbol __alloca
fflib error LNK2001: unresolved external symbol __alloca
fflib error LNK2019: unresolved external symbol __alloca referenced in function _rd8x8_c
fflib error LNK2001: unresolved external symbol __alloca
fflib error LNK2001: unresolved external symbol __alloca
fflib error LNK2001: unresolved external symbol __alloca
fflib error LNK2019: unresolved external symbol ___udivdi3 referenced in function _mpeg1_encode_sequence_header
fflib error LNK2019: unresolved external symbol ___umoddi3 referenced in function _mpeg1_encode_sequence_header
fflib error LNK2019: unresolved external symbol ___fixunssfdi referenced in function _ff_rate_control_init
fflib error LNK2019: unresolved external symbol ___fixunsdfdi referenced in function _ff_rate_estimate_qscale
fflib fatal error LNK1120: 7 unresolved externals

Any suggestions??

Eretria-chan
12th June 2006, 11:34
Hmmm... LNK2001 means "unresolved external symbol," eg it can't find the function you're trying to call. So you are saying that you've included the lib file (the linker finds it and looks for symbols in there) and you still get external errors? Are you sure you're using the right lib file? And what functions are you trying?

If you properly make it work with static linking then it probably is something that's happening inside the dll that causes your error.

RahulOP
12th June 2006, 12:34
Hmmm... LNK2001 means "unresolved external symbol," eg it can't find the function you're trying to call. So you are saying that you've included the lib file (the linker finds it and looks for symbols in there) and you still get external errors? Are you sure you're using the right lib file? And what functions are you trying?
If you properly make it work with static linking then it probably is something that's happening inside the dll that causes your error.

I am not calling any function

avcodec_init();

this line alone is enough to generate the above mentioned errors. will building a different version of FFMPEG help ??

Eretria-chan
12th June 2006, 12:41
I'm not sure why you get these errors... you can try a different build. Perhaps you could also put a link where you find all this you use for your build and I might look into it and see if I can find out what's wrong when I have time.

RahulOP
12th June 2006, 13:19
I used a patched version of FFMPEG that I got from here

http://www.salyens.com/mingw/index.html

I followed the instructions given and it worked fine

Eretria-chan
12th June 2006, 13:21
Great! Hope everything works fine, then.

RahulOP
13th June 2006, 04:22
Great! Hope everything works fine, then.
No No , I meant I downloaded that version, followed the instructions to build and I didnt have any problems. Of course, when I linked it to my program, all the errors came rushing out :stupid:

Sorry for the confusion.

Edit:
I believe I am using the release version of FFMPEG. However I was told that I should use the CVS/SVN version as that would give me libavutil.lib which is also supposed to be linked to my VC++ program. Please advise.

RahulOP
13th June 2006, 08:06
I got the SVN version and built that using

./configure --extra-cflags="-mno-cygwin -mms-bitfields" --extra-ldflags="-Wl,--add-stdcall-al
ias" --enable-mingw32 --enable-shared --disable-ffserver --disable-ffplay --disable-static --en
able-memalign-hack

Then
cd libavcodec
make

But then i got an error for libavutil--something about error51. I then built libavutil first and repeated the above steps. It built but gave an error 127(ignored) and gave me avcodec dll but not avcodec lib or any other lib files for that matter. Is this correct?

All this is in MSYS btw

Eretria-chan
13th June 2006, 08:31
I'm not familiar with any sort of build make files. You never know what they might do. The files might have been stored in a different directory, though. You could always search.

RahulOP
13th June 2006, 08:57
Hmm.

oK i tried to link to the the newly created dll using the code you gave earlier. Unfortunately, now it doesnt even seem to do that.
hDll remains NULL. any pointers here?

Eretria-chan
13th June 2006, 09:00
If hDll is NULL, it means it can't load the library. Check with GetLastError() to see what the error seems to be.

RahulOP
13th June 2006, 10:24
Not really sure how to use . Anyway I went back to static linking and I now link to the old release version(for libavcodec and libavformat i have both the lib files) and to the new SVN version(for libavutil though i still dont have any lib files for libavutil).

Still the same errors. I am now at my wits end over this?? Does anybody have a version that i can download and use?

Eretria-chan
13th June 2006, 17:46
For the first part... use GetLastError() to get the error. Then use the C++ lookup utility to see what that errors means.
You should never try to use a lib file with a recent build. With that I mean, they don't match in your case. The lib file contains information about what things are exported from a dll file and also the address where to find them. If you somehow got that program running, you'd most likely get an access violation because it's jumping to the wrong part of the dll and executing code. Worse to this is that in this way it can be used as a security flaw, because code can be "injected" into the dll where it jumps and hence the code can take control of your computer. Bad idea. Very.

Now, as for the static linking... try to actually copy the declarations of all the functions you need (from libavcodec.h) to a seperate header file. Include the new header but do not include the libavcodec one. See if it will compile.

RahulOP
13th June 2006, 19:35
Yes, but in my recent build, I dont have any of the lib files. Neither does the application load the dll correctly :-(:stupid: :stupid: :stupid:

:eek: :eek: :( :(

Eretria-chan
13th June 2006, 20:19
If you don't have any lib files, then you must use dynamic linking. If the dll file won't load with LoadLibrary, then call GetLastError to get the error code and look it up in the c++ error lookup tool. When knowing the nature of the error, try to fix it.

RahulOP
14th June 2006, 05:38
Using Dynamic Linking, and GetErrorLast the error I get is 2.Atleast that's what the code below shows

avctx=(AVCodecContext *)p4;
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
avctx->bit_rate=64000;
The avctx is the part that throws the error.

This is my entire code
AVCodecContext *avctx;
AVCodec *codec;
avctx = new AVCodecContext;
codec = new AVCodec;
HMODULE hDll = LoadLibrary("avcodec.dll");
if(hDll== NULL)
{
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("Failed loading1");
}

typedef void (avcodec_init)();
avcodec_init* p = (avcodec_init*)GetProcAddress(hDll, "avcodec_init");
if(p==NULL)
AfxMessageBox("Failed loading2");
p();
FreeLibrary(hDll);

HMODULE hDll3 = LoadLibrary("avcodec.dll");
typedef void (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll3, "avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MP2);
if(p3==NULL)
AfxMessageBox("Failed loading3");
FreeLibrary(hDll3);

codec=(AVCodec *)p3;

HMODULE hDll4 = LoadLibrary("libavcodec.dll");
//(hDll should not be NULL here)
typedef void (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll4, "avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();
FreeLibrary(hDll4);

avctx=(AVCodecContext *)p4;
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
avctx->bit_rate=64000;
avctx->sample_rate=44100;
avctx->channels=2;

HMODULE hDll2 = LoadLibrary("libavcodec.dll");
//(hDll should not be NULL here)
typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll2, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2((AVCodecContext *)p4,(AVCodec *)p3);
FreeLibrary(hDll2);

foxyshadis
14th June 2006, 09:49
MSDN is your best friend whenever you code for windows, always read the documentation on functions you don't understand. Even if you still don't fully understand, you'll have a better idea of how to use it. ;) See:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/formatmessage.asp
and for a better way to wrap it:
http://www.flounder.com/formatmessage.htm

Anyway, the errormessage given is a win32 error, 2 being "FILENOTFOUND".

Do you have a debugger? Can you learn windbg, even? It's a lot easier to debug and have a full sense of the program at any time than a shotgun compile-and-pray with a ton of printouts.

RahulOP
14th June 2006, 10:09
Yup,I got that but thanks anyway.I had typdef'd the code wrong :stupid:
typedef AVCodecContext* (avcodec_alloc_context)(); this is the correct type.

Unfortunately a similar conversion now gives an error
typedef AVFrame* (avcodec_alloc_frame)();
avcodec_alloc_frame* p7 =(avcodec_alloc_frame)GetProcAddress(hDll7,"avcodec_alloc_frame");
p7();
c2066:cast to function type is illegal :-(

Eretria-chan
14th June 2006, 12:21
RahulOP:
Please use the C++ lookup tool for error codes. It's there for a reason and is very handy. I went to the Tools menu in Visual Studio and selected Error Lookup. Entered 2 and look up. It gives me the error, "file not found." And so, I know now that it can't find the file, and that's why it fails.

Anyway, for that compile error...
avcodec_alloc_frame is a function prototype. You can't cast to a function prototype. You forgot the "*". It should be (avcodec_alloc_frame*). This tells the compiler you're casting a to a POINTER to that kind of function, which is legal.

RahulOP
14th June 2006, 13:04
RahulOP:
Please use the C++ lookup tool for error codes. It's there for a reason and is very handy. I went to the Tools menu in Visual Studio and selected Error Lookup. Entered 2 and look up. It gives me the error, "file not found." And so, I know now that it can't find the file, and that's why it fails.


That was a very neat tip and one that I am embarrased to admit I didnt know about. I spent an hour searching the MSDN for that error:stupid:

Anyway, I realised that I had made a mistake in building FFMPEG(I missed out this step---Add a call to `vcvars32.bat' (which sets up the environment variables for the Visual C++ tools) as the first line of `msys.bat'. The standard location for `vcvars32.bat' is `C:\Program Files\Microsoft Visual Studio8\VC\bin\vcvars32.bat', and the standard location for `msys.bat' is `C:\msys\1.0\msys.bat'. If this corresponds to your setup, add the following line as the first line of `msys.bat': call "C:\Program Files\Microsoft Visual Studio 8\VC\bin\vcvars32.bat") so I corrected this mistake, built it again and got my libs!!! Yay!!!

So now I am able to link and execute without errors :-)
I then tried what you suggested yesterday i.e.
avcodec_init();
But it gave me this error
fflib3 error LNK2019: unresolved external symbol _avcodec_init referenced in function "public: void __thiscall Cfflib3Dlg::OnBnClickedOk(void)" (?OnBnClickedOk@Cfflib3Dlg@@QAEXXZ)

From the avcodec.h file I know that avcodec_init is defined as
void avcodec_init(void);

so I simply placed a void in front of my initial command and ran--
NO errors!!
So now I am confused.. I remember you telling me that I should be able to call them as if it were my own code

I then tried this
AVCodec *codec;
codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);

Similar error and so I checked the .h file again
AVCodec *avcodec_find_encoder(enum CodecID id);

So now??
codec = (AVCodec *)avcodec_find_encoder(CODEC_ID_MPEG1VIDEO); threw the same error


p.s. At the end of this, I am writing "The Complete Idiot's/Moron's/Bird-Brain's Guide to using libavcodec in vc++"

Eretria-chan
14th June 2006, 13:49
So... what you are writing is that even if you copy the headers, you recieve a linking error saying the symbol was not found? If so, then there must be something wrong with the lib you are linking against, since this lib file should contain this "symbol."

How did dynamic linking go?

RahulOP
14th June 2006, 13:55
So... what you are writing is that even if you copy the headers, you recieve a linking error saying the symbol was not found? If so, then there must be something wrong with the lib you are linking against, since this lib file should contain this "symbol."

But I did a fresh build of FFMPEG from a clean SVN version!!:eek:

How did dynamic linking go?

<Sob!!> Now you want me to go back to that??

NNNNOOOooooooooooooooooo........................

Eretria-chan
14th June 2006, 14:03
Yes, it's weird... are you using an old header or something? :/ Perhaps it's a bug. I know I found this in Visual Studio some time ago. Had the right lib, yet couldn't find the function.

I just thought you should try dynamic just in case static throws you all of these errors... you can make an easy wrapper class for the dll to take care of the problems. That would save you time everytime you make a call.

RahulOP
14th June 2006, 19:04
I just thought you should try dynamic just in case static throws you all of these errors... you can make an easy wrapper class for the dll to take care of the problems. That would save you time everytime you make a call.

I just got the hang of static and dynamic linking and now you throw in "Wrapper Class"

You are conspiring against me arent you :scared:

Eretria-chan
14th June 2006, 19:53
Now, now, calm down...
I'm mentioning that IF and only IF you need to use dynamic linking, you can create a wrapper class, with which the dynamic linking will look as if you use static linking.

So let's just recap... what happened when you used static linking? You copied those headers (and exluded the libavcodec.h file) and you got linking errors?

RahulOP
15th June 2006, 06:22
So let's just recap... what happened when you used static linking? You copied those headers (and exluded the libavcodec.h file) and you got linking errors?

Got hold of a clean SVN version
Built in in MSYS without any add-ons(mp3lame,AMR_WB......)
While I got the dll file(as well as getting an Exports Library ), my lib file came out as avcodec.dll.lib (Object File Library and it wasnt even very big just ast 1.5kb)
VC++ was not able to open the lib file nor link to the dll file correctly. I kept getting error 126 even though I had copied the dll to the Window/System32 folder, and the project folder, And the Debug Folder and the Resources Folder(yeah me not taking any chance)
I didnt copy the headers anywhere. I just included it in my project file and linked to the appropriate directory.

That's all I've done.

RahulOP
15th June 2006, 07:58
So I go back to Dynamic Linking.

<code>AVCodec *codec;
int i, out_size, size, x, y, outbuf_size;
FILE *f;
uint8_t *outbuf, *picture_buf;


HMODULE hDll3 = LoadLibrary("avcodec.dll");
typedef void (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll3, "avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MPEG1VIDEO);
if(p3==NULL)
AfxMessageBox("Failed loading3");


codec=(AVCodec *)p3;

HMODULE hDll4 = LoadLibrary("avcodec.dll");
//(hDll should not be NULL here)
typedef AVCodecContext* (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll4, "avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();
if(p4==NULL)
AfxMessageBox("Failed loading4");

AVCodecContext *avctx = p4();

avctx->bit_rate=400000;
avctx->width = 352;
avctx->height = 288;
/* frames per second */
avctx->frame_rate = 25;
avctx->frame_rate_base= 1;
avctx->gop_size = 10; /* emit one intra frame every ten frames */
avctx->max_b_frames=1;

HMODULE hDll2 = LoadLibrary("avcodec.dll");
//(hDll should not be NULL here)
typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll2, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2((AVCodecContext *)p4,(AVCodec*)p3);
</code>
Notice the last line--p2. p2 should be of the form (avctx,codec) i.e.alloc_code_context and avcodec_find_Encoder. The problem(given my display of my knowledge of pointes, does this really surprise you?) is that I cant pass avctx and codec directly. Also, since avctx takes values like bit_rate, width,height, those have to be reflected when I open the required codec. Any ideas??

Eretria-chan
15th June 2006, 12:42
For the static linking part:
I wanted you to copy the headers from libavcodec.h to your own project so that you didn't have to include libavcodec.h, as there might be something there causing problems.
...And I don't know what error #126 (compiler or linker?) is. If you could post a line or a reference to the error, that would be helpful.

For the dynamic:
You keep loading the same dll over and over again. You need only load it once. Once it's loaded, you can use GetProcAddress to get the addresses of the functions you wish to call.
I see you also calling functions twice or more.

HMODULE hDll3 = LoadLibrary("avcodec.dll");
typedef void (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll3, "avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MPEG1VIDEO);
if(p3==NULL)
AfxMessageBox("Failed loading3");
You check for p3 == NULL twice. But after the first check, it cannot be NULL or anything else because there is nothing that can change your pointer. It's an unneccesary check. You can remove it.

codec=(AVCodec *)p3;
I don't claim to know your code, but you're basically assigning the function pointer p3 to codec. Are you supposed to pass a FUNCTION to avcodec_open? That seems weird to me. Isn't it some kind of result it is expecting or something similar?

HMODULE hDll4 = LoadLibrary("avcodec.dll");
//(hDll should not be NULL here)
typedef AVCodecContext* (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll4, "avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();
if(p4==NULL)
AfxMessageBox("Failed loading4");
First, you don't need to open the library. Use hDll3 instead as it is already open.
Same thing here, you're checking the function pointer for NULL after you've called the function. It will never be NULL there.
You're calling p4 twice. You don't need to, and probably shouldn't. Remove the bold lines here.

AVCodecContext *avctx = p4();
...But be sure to keep this one since this returns the struct you want.

HMODULE hDll2 = LoadLibrary("avcodec.dll");
//(hDll should not be NULL here)
typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll2, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2((AVCodecContext *)p4,(AVCodec*)p3);
Use hDll3 instead of reopening the library.
You should actually be able to pass the variables directly as they are of the same type, but what I find weird is that you send the actual FUNCTION POINTERS. I mean, why does the function need function pointers? Should it not be asking for some sort of result?

The first argument should be avctx , should it not? The struct returned by the function stored in p4. The second ... I haven't the slightest clue what it is looking for... Perhaps the first function, p3, should return av AVCodec struct?

RahulOP
16th June 2006, 05:48
I am not sure I completely understand the copying of the headers part since the .h file also enums the codec ids.So I am not sure what to copy and what not to copy.

Error 126 was got from GetLastError. Cldnt find file which didnt make sense since I pasted the damn file everywhere:D

In any case, as of now, the following code is fully functional!!:)

Of course, all it does is to create a video file that is 95kb in size and opens and shuts almost immediately!! But YAY!!! I got something to work!! YAY!!!!!!!
(I'm pretty excited even though if it werent for all your help, I wouldnt even have reached this far but YAY!!!!!!

int i, out_size, size, x, y, outbuf_size;
FILE *f;
uint8_t *outbuf, *picture_buf;


HMODULE hDll = LoadLibrary("avcodec.dll");
if(hDll== NULL)
{
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("first");
AfxMessageBox("Failed loading1");
}

typedef void (avcodec_init)();
avcodec_init* p = (avcodec_init*)GetProcAddress(hDll, "avcodec_init");
if(p==NULL)
AfxMessageBox("Failed loading2");
p();

typedef void (avcodec_register_all)();
avcodec_register_all* p1a = (avcodec_register_all*)GetProcAddress(hDll, "avcodec_register_all");
if(p1a==NULL)
AfxMessageBox("Failed loading3");
p1a();

typedef AVCodec * (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll, "avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MPEG1VIDEO);

AVCodec *codec=p3(CODEC_ID_MPEG1VIDEO);

typedef AVCodecContext* (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll, "avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();

AVCodecContext *avctx = p4();

typedef AVFrame* (avcodec_alloc_frame)();
avcodec_alloc_frame* p7 =(avcodec_alloc_frame*)GetProcAddress(hDll,"avcodec_alloc_frame");
p7();
AVFrame *picture =p7();

avctx->bit_rate=400000;
avctx->width = 352;
avctx->height = 288;
/* frames per second */
avctx->frame_rate = 25;
avctx->frame_rate_base= 1;
avctx->gop_size = 10; /* emit one intra frame every ten frames */
avctx->max_b_frames=1;


typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2(avctx,codec);

typedef int (avcodec_encode_video)(AVCodecContext *avctx, uint8_t *buf, int buf_size,
const AVFrame *pict);
avcodec_encode_video *p9=(avcodec_encode_video*)GetProcAddress(hDll, "avcodec_encode_video");
if(p9==NULL)
AfxMessageBox("Failed loading9");

typedef int (avcodec_close)(AVCodecContext *avctx);
avcodec_close *p10=(avcodec_close*)GetProcAddress(hDll, "avcodec_close");
if(p10==NULL)
AfxMessageBox("Failed loading9");


f=fopen("C:/Rahul.avi","w");
if(!f){
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("file");
}

outbuf_size = 200000;
outbuf = (uint8_t*)malloc(outbuf_size);
size = avctx->width * avctx->height;
picture_buf = (uint8_t*)malloc((size * 3) / 2);

picture->data[0] = picture_buf;
picture->data[1] = picture->data[0] + size;
picture->data[2] = picture->data[1] + size / 4;
picture->linesize[0] = avctx->width;
picture->linesize[1] = avctx->width / 2;
picture->linesize[2] = avctx->width / 2;


for(i=0;i<25;i++) {
fflush(stdout);
/* prepare a dummy image */
/* Y */
for(y=0;y<avctx->height;y++) {
for(x=0;x<avctx->width;x++) {
picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
}
}

/* Cb and Cr */
for(y=0;y<avctx->height/2;y++) {
for(x=0;x<avctx->width/2;x++) {
picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
}
}

out_size= p9(avctx, outbuf, outbuf_size, picture);
/* encode the image */
// out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
//
fwrite(outbuf, 1, out_size, f);
}

/* get the delayed frames */ //Undo from below
for(; out_size; i++) {
fflush(stdout);

out_size = p9(avctx, outbuf, outbuf_size, NULL);
//printf("write frame %3d (size=%5d)\n", i, out_size);
fwrite(outbuf, 1, out_size, f);
}

///* add sequence end code to have a real mpeg file */
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
fwrite(outbuf, 1, 4, f);
fclose(f);
free(picture_buf);
free(outbuf);

//avcodec_close(c);
p10(avctx);
// free(avctx);
// free(picture);
//printf("\n");

// TODO: Add your control notification handler code here

FreeLibrary(hDll);

Eretria-chan
16th June 2006, 12:03
I am not sure I completely understand the copying of the headers part since the .h file also enums the codec ids.So I am not sure what to copy and what not to copy.

Error 126 was got from GetLastError. Cldnt find file which didnt make sense since I pasted the damn file everywhere:D

In any case, as of now, the following code is fully functional!!:)

Of course, all it does is to create a video file that is 95kb in size and opens and shuts almost immediately!! But YAY!!! I got something to work!! YAY!!!!!!!
(I'm pretty excited even though if it werent for all your help, I wouldnt even have reached this far but YAY!!!!!!
Great that you've got it working. When I mention copying the stuff, I mean copying every header (or function prototype) that your app uses and all the structs/enums that those prototypes uses. So, in short, copy all the minimum (or bare) information that is needed for the compiler to compile the code without using the libavcodec.h file.

As far as loading dlls are concerned: it looks first in the current directory (you could use GetCurrentDirectory() Windows API to get it), then it looks in your windows\system32 folder for it. And also every path mention in the PATH envoirment variable.

Also... if you want to be really savvy, you can something more...
HMODULE hDll = LoadLibrary("mylibrary.dll");
void* p;
typedef void (function1)();
p = (void*)GetProcAddress(hDll, "my_function_1");
((function1*)p)();

typedef int (function2)(int);
p = (void*)GetProcAddress(hDll, "my_function_2");
((function2*)p)(79);

Do you understand what this code means? Basically, it simply stores the address to your function and casts it to the right function pointer before calling. You need only one variable and it's a little more savvy ;)

RahulOP
16th June 2006, 12:55
Also... if you want to be really savvy, you can something more...
HMODULE hDll = LoadLibrary("mylibrary.dll");
void* p;
typedef void (function1)();
p = (void*)GetProcAddress(hDll, "my_function_1");
((function1*)p)();

typedef int (function2)(int);
p = (void*)GetProcAddress(hDll, "my_function_2");
((function2*)p)(79);

Do you understand what this code means? Basically, it simply stores the address to your function and casts it to the right function pointer before calling. You need only one variable and it's a little more savvy ;)

Right.
Let us not get too carried away:D I've struggled enough with pointers as is evident from here (http://www.codeproject.com/script/comments/forums.asp?forumid=1647&XtraIDs=1647&searchkw=error+code&sd=3%2F17%2F2006&ed=6%2F15%2F2006&select=1533196&df=100&fr=444.5&msg=1533196)

Eretria-chan
16th June 2006, 13:26
Pointers are a very handy tool. You'd best learn them correctly! There are other tricks as well, as I can show you here...

HMODULE hDll = LoadLibrary("mylibrary.dll");
typedef void (function1)(param1, param2);
typedef some_struct1* (function2)();
typedef some_struct2* (function3)();
( (function1*)GetProcAddress(hDll, "function1") )
(
( (function2*)GetProcAddress(hDll, "function2") )(),
( (function3*)GetProcAddress(hDll, "function3") )()
);

I know the code looks messy, but it goes to show you that it can be done :p Pointers are wonderful and if you ever get to advanced programming, you'll love them, so let's get used to them right now :sly:

RahulOP
16th June 2006, 13:50
This is a partial video encoding thingy. I am stuck at this line

snprintf(buf, sizeof(buf), filename, frame);

From what I understand, snprintf is a Linux command used to write to a stream. Any clue how its done in VC++??

p.s. Lemme finish with this thing and I'll do all the learning of pointers that you want me to do:D

int frame, sized, got_picture, len,outbuf_size;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
char buf[1024];
/* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
//int i, out_size, size, x, y, outbuf_size;
FILE *f;
uint8_t *outbuf, *picture_buf;

HMODULE hDll = LoadLibrary("avcodec.dll");
if(hDll== NULL)
{
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("first");
AfxMessageBox("Failed loading1");
}

typedef void (avcodec_init)();
avcodec_init* p = (avcodec_init*)GetProcAddress(hDll, "avcodec_init");
if(p==NULL)
AfxMessageBox("Failed loading2");
p();

typedef void (avcodec_register_all)();
avcodec_register_all* p1a = (avcodec_register_all*)GetProcAddress(hDll, "avcodec_register_all");
if(p1a==NULL)
AfxMessageBox("Failed loading3");
p1a();

typedef AVCodec * (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll, "avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MPEG1VIDEO);


AVCodec *codec=p3(CODEC_ID_MPEG1VIDEO);

typedef AVCodecContext* (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll, "avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();

AVCodecContext *avctx = p4();

typedef AVFrame* (avcodec_alloc_frame)();
avcodec_alloc_frame* p7 =(avcodec_alloc_frame*)GetProcAddress(hDll,"avcodec_alloc_frame");
p7();
AVFrame *picture =p7();

avctx->bit_rate=400000;
avctx->width = 352;
avctx->height = 288;
avctx->frame_rate = 25;

if(codec->capabilities&CODEC_CAP_TRUNCATED)
avctx->flags|= CODEC_FLAG_TRUNCATED;

typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2(avctx,codec);

typedef int (avcodec_encode_video)(AVCodecContext *avctx, uint8_t *buf, int buf_size,const AVFrame *pict);
avcodec_encode_video *p9=(avcodec_encode_video*)GetProcAddress(hDll, "avcodec_encode_video");
if(p9==NULL)
AfxMessageBox("Failed loading9");

typedef int (avcodec_close)(AVCodecContext *avctx);
avcodec_close *p10=(avcodec_close*)GetProcAddress(hDll, "avcodec_close");
if(p10==NULL)
AfxMessageBox("Failed loading9");



f=fopen("C:/Rahul.avi","r");
if(!f){
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("file");
}
frame = 0;
for(;;) {
sized = fread(inbuf, 1, INBUF_SIZE, f);
if (sized == 0)
break;
inbuf_ptr = inbuf;
while (sized > 0)
{

len = p9(avctx, inbuf,INBUF_SIZE ,picture);
if (len < 0) {
//fprintf(stderr, "Error while decoding frame %d\n", frame);
AfxMessageBox("error while encoding");
exit(1);
}
//if (got_picture)
else {
//printf("saving frame %3d\n", frame);
fflush(stdout);

//snprintf(buf, sizeof(buf), filename, frame);
pgmsave(picture->data[0], picture->linesize[0],
avctx->width, avctx->height, buf);
frame++;
}
sized -= len;
inbuf_ptr += len;
}
}

len = p9(avctx, inbuf,INBUF_SIZE ,picture);
if (got_picture) {
// printf("saving last frame %3d\n", frame);
fflush(stdout);

/* the picture is allocated by the decoder. no need to
free it */
//sprintf(buf, sizeof(buf), filename, frame);
pgmsave(picture->data[0], picture->linesize[0],
avctx->width, avctx->height, buf);
frame++;
}

fclose(f);
p10(avctx);

// TODO: Add your control notification handler code here
FreeLibrary(hDll);

Eretria-chan
16th June 2006, 15:21
Not sure here...
If you're trying to write a FILE, then try fwrite. If you're trying to write formatted data into a string, then try sprintf. Formatted data can also be writted to files via fprintf.

MasamuneXGP
17th June 2006, 18:06
Hello all. I'm also having some trouble getting libavcodec to work in my program. Hopefully I haven't found this thread too late.

I would very much prefer static linking to dynamic. However, I can't seem to get the .lib files to compile correctly. I have a clean download from the svn, and followed the documentation on how to compile in win32 to the letter, however, like RahulOP, all I got were .dll.lib files. The files are obviously not the real .lib files, as they are all less than 2kb in size. Trying to link to them gets me a flood of unresolved externals from the sample file included with the source. Help?

Thanks in advance...

RahulOP
19th June 2006, 05:43
Hello all. I'm also having some trouble getting libavcodec to work in my program. Hopefully I haven't found this thread too late.

I would very much prefer static linking to dynamic. However, I can't seem to get the .lib files to compile correctly. I have a clean download from the svn, and followed the documentation on how to compile in win32 to the letter, however, like RahulOP, all I got were .dll.lib files. The files are obviously not the real .lib files, as they are all less than 2kb in size. Trying to link to them gets me a flood of unresolved externals from the sample file included with the source. Help?
Thanks in advance...

Hi Masamune,
I had the same problems when i compiled with a clean version.I've mentioned this as the reason I switched from Static to Dynamic.
You could try this (http://www.salyens.com/downloads/index.html#ffmpeg-0.4.7)version that I got from www.salyens.com

ATB!! and do share the code that you are using:D

MasamuneXGP
19th June 2006, 20:18
After a lot of googleing, I figured it out. Static linking works. This is apperently a known issue and there's a patch for it. Just open the configure file, find the following lines (they're around the halfway point):


SLIBPREF=""
SLIBSUF=".dll"
EXESUF=".exe"
if test "$force_prefix" != yes; then prefix="/c/Program Files/FFmpeg"; fi
if test "$force_libdir" != yes; then bindir="$prefix"; fi


and insert the following two lines right after the EXESUF line, so that it looks like this:


SLIBPREF=""
SLIBSUF=".dll"
EXESUF=".exe"
SLIBNAME_WITH_VERSION='$(SLIBPREF)$(NAME).$(LIBVERSION)$(SLIBSUF)'
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(NAME).$(LIBMAJOR)$(SLIBSUF)'
if test "$force_prefix" != yes; then prefix="/c/Program Files/FFmpeg"; fi
if test "$force_libdir" != yes; then bindir="$prefix"; fi


Open up msys, configure with the appropriate switches, make, and you'll get .lib files that will actually work. However, the .dll files are also required by the .libs apperently, so don't discard them.

Alas, this turned out to be the easy part, I have bigger problems now... which are beyond the scope of this thread, so I should probably make my own.

RahulOP
20th June 2006, 07:00
After a lot of googleing, I figured it out. Static linking works. This is apperently a known issue and there's a patch for it. Just open the configure file, find the following lines (they're around the halfway point):

SLIBPREF=""
SLIBSUF=".dll"
EXESUF=".exe"
if test "$force_prefix" != yes; then prefix="/c/Program Files/FFmpeg"; fi
if test "$force_libdir" != yes; then bindir="$prefix"; fi

and insert the following two lines right after the EXESUF line, so that it looks like this:

SLIBPREF=""
SLIBSUF=".dll"
EXESUF=".exe"
SLIBNAME_WITH_VERSION='$(SLIBPREF)$(NAME).$(LIBVERSION)$(SLIBSUF)'
SLIBNAME_WITH_MAJOR='$(SLIBPREF)$(NAME).$(LIBMAJOR)$(SLIBSUF)'
if test "$force_prefix" != yes; then prefix="/c/Program Files/FFmpeg"; fi
if test "$force_libdir" != yes; then bindir="$prefix"; fi

Open up msys, configure with the appropriate switches, make, and you'll get .lib files that will actually work. However, the .dll files are also required by the .libs apperently, so don't discard them.
Alas, this turned out to be the easy part, I have bigger problems now... which are beyond the scope of this thread, so I should probably make my own.

Alas, this still didnt work for me.

First I changed the configure file for an ffmpeg version that I had already compiled amd this gave me,for libavcodec, an avcodec.lib file that was only 1.53kb in size. Unfortunately, VC++ wasnt able to open this

So then I took a clean version, made the changes in the configure file and built it again. This time I only got avcodec.51.lib but no avcodec.lib file :scared: :stupid: Of course vc++ wouldnt open this file as well so I guess its back to Dyanmic linking for me :(
Anybody have any idea on the snprintf command?
snprintf(buf, sizeof(buf), filename, frame);
this needs to be re-written into VC++
Thanks

Edit: Found this cool tip http://www.devx.com/tips/Tip/14537
in short snprintf can be written in vc++ as _snprintf

RahulOP
20th June 2006, 13:38
All rightey then, i now seem to have some sorta code that runs fine and creates a 100Kb MPEG file(from a 12Mb file) that Windows Media PLayer absolutely refuses to play:(

I dont think I am encoding the entire file/ all the frames. I believe MasamuneXGP has a similar problem. So any clues anyone??


void Cfflib4Dlg::pgmsave(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
{
FILE *f2;
int i;

f2=fopen("C:/test.mpeg","w");
if(!f2){
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("file");
}
fprintf(f2,"P5\n%d %d\n%d\n",xsize,ysize,255);
for(i=0;i<ysize;i++)
fwrite(buf + i * wrap,1,xsize,f2);
fclose(f2);
}

void Cfflib4Dlg::OnBnClickedOk()
{
// int frame, sized, got_picture, len,outbuf_size;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
char buf[1024];
/* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
int i, out_size, size, x, y, outbuf_size,len,got_picture,frame;
FILE *f;
uint8_t *outbuf, *picture_buf;



HMODULE hDll = LoadLibrary("avcodec.dll");

if(hDll== NULL)
{
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("first");
AfxMessageBox("Failed loading1");
}

typedef void (avcodec_init)();
avcodec_init* p = (avcodec_init*)GetProcAddress(hDll, "avcodec_init");
if(p==NULL)
AfxMessageBox("Failed loading2");
p();

typedef void (avcodec_register_all)();
avcodec_register_all* p1a = (avcodec_register_all*)GetProcAddress(hDll, "avcodec_register_all");
if(p1a==NULL)
AfxMessageBox("Failed loading3");
p1a();

typedef AVCodec * (avcodec_find_encoder)(int CODECID);
avcodec_find_encoder* p3 = (avcodec_find_encoder*)GetProcAddress(hDll, "avcodec_find_encoder");
if(p3==NULL)
AfxMessageBox("Failed loading3");
p3(CODEC_ID_MPEG1VIDEO);


AVCodec *codec=p3(CODEC_ID_MPEG1VIDEO);

typedef AVCodecContext* (avcodec_alloc_context)();
avcodec_alloc_context* p4 = (avcodec_alloc_context*)GetProcAddress(hDll, "avcodec_alloc_context");
if(p4==NULL)
AfxMessageBox("Failed loading4");
p4();

AVCodecContext *avctx = p4();

typedef AVFrame* (avcodec_alloc_frame)();
avcodec_alloc_frame* p7 =(avcodec_alloc_frame*)GetProcAddress(hDll,"avcodec_alloc_frame");
p7();
AVFrame *picture =p7();

avctx->width = 352;
avctx->height = 288;
avctx->frame_rate = 25;

typedef int (avcodec_open)(AVCodecContext *avctx, AVCodec *codec);
avcodec_open* p2 = (avcodec_open*)GetProcAddress(hDll, "avcodec_open");
if(p2==NULL)
AfxMessageBox("Failed loading2");
p2(avctx,codec);

typedef int (avcodec_encode_video)(AVCodecContext *avctx, uint8_t *buf, int buf_size,
const AVFrame *pict);
avcodec_encode_video *p9=(avcodec_encode_video*)GetProcAddress(hDll, "avcodec_encode_video");
if(p9==NULL)
AfxMessageBox("Failed loading9");

typedef int (avcodec_close)(AVCodecContext *avctx);
avcodec_close *p10=(avcodec_close*)GetProcAddress(hDll, "avcodec_close");
if(p10==NULL)
AfxMessageBox("Failed loading9");

f=fopen("C:/Rahul.avi","r");
if(!f){
CString str;
int a = GetLastError();
str.Format("%d",a);
AfxMessageBox(str);
AfxMessageBox("file");
}
outbuf_size = 100000;
outbuf = (uint8_t*)malloc(outbuf_size);
size = avctx->width * avctx->height;
picture_buf = (uint8_t*)malloc((size * 3) / 2); /* size for YUV 420 */

picture->data[0] = picture_buf;
picture->data[1] = picture->data[0] + size;
picture->data[2] = picture->data[1] + size / 4;
picture->linesize[0] = avctx->width;
picture->linesize[1] = avctx->width / 2;
picture->linesize[2] = avctx->width / 2;

frame = 0;
for(;;) {
size = fread(inbuf, 1, INBUF_SIZE, f);
if (size == 0)
break;
inbuf_ptr = inbuf;
while (size > 0)
{

len = p9(avctx,outbuf, outbuf_size, picture);
if (len < 0) {
//fprintf(stderr, "Error while decoding frame %d\n", frame);
//("error while encoding");
exit(1);
}
else
{
//printf("saving frame %3d\n", frame);
fflush(stdout);

_snprintf(buf, sizeof(buf),"C:\test.mpeg",frame);
//buf +=sprintf(buf,frame);
pgmsave(picture->data[0], picture->linesize[0],
avctx->width, avctx->height, buf);
frame++;
}
size -= len;
inbuf_ptr += len;
}
}

len = p9(avctx,outbuf, outbuf_size, picture);
// if (got_picture) {
// printf("saving last frame %3d\n", frame);
fflush(stdout);

/* the picture is allocated by the decoder. no need to
free it */
_snprintf(buf, sizeof(buf),"C:\test.mpeg",frame);
pgmsave(picture->data[0], picture->linesize[0],
avctx->width, avctx->height, buf);
frame++;
// }
outbuf[0] = 0x00;
outbuf[1] = 0x00;
outbuf[2] = 0x01;
outbuf[3] = 0xb7;
FILE *f2;
// int i;

f2=fopen("C:/test.mpeg","a+");
fwrite(outbuf,1,4,f2);

fclose(f);
fclose(f2);

p10(avctx);

// TODO: Add your control notification handler code here
FreeLibrary(hDll);

OnOK();
}

MasamuneXGP
20th June 2006, 14:05
avcodec.51.lib is the correct lib file. Link to it, making sure not to move it from its folder, and it'll work. Also, to test static linking, (as well as decoding) there's a lovely demo program here:
http://www.inb.uni-luebeck.de/~boehme/libavcodec_update.html

RahulOP
20th June 2006, 14:25
It may be correct but does the file size really come to 150kb or something like that??

MasamuneXGP
20th June 2006, 21:53
yep. All the code is in the dll files, the lib files are just an interface. So it's kind of like dynamic linking but with a static interface for convenience. Or so I assume.

Actually, I believe there's a line in the documentation somewhere about real static linking, eg, with no dll file, being impossible on win32 for some reason.

Eretria-chan
20th June 2006, 22:00
The lib files contains the addressess for the functions exported through the dll. The linker needs it to translate the function names into the address it needs to jump to.
When you use dynamic linking, you do this: load the dll into the virtual memory space of the application. Find the address of a function. This is an address within the virtual memory where the function is located. Then you call it via a function pointer. Unloading the dll removes it virtual memory of the application that uses it.

RahulOP
21st June 2006, 06:19
Well, I tried a bit more but the application still wont open the lib. makes me wonder if I am doing something wrong.

I've seen Martin's program before but since I am using static liking and encoding not sure how that helps me. I have a 1 min video file that i recorded on my system using my webcam. using nokia multi media player i determined that my recorded file was recorded with the video codec MS H.263. Trying to convert this to MPEG (eventual goal is to convert to 3GP)