Thursday, January 19, 2012

Problem with avformat_open_input()

I lost too much time because of a misleading error message reported by avformat_open_input function! I used the following code that exhibit the error:
AVFormatContext *pFormatCtx = NULL;
...
ret = avformat_open_input(&pFormatCtx, argv[1], NULL, NULL);
if (ret < 0)
   print_error_and_exit(ret, "avformat_open_input()");
print_error_and_exit() is a helper function that uses av_strerror() to give textual representation of the error code stored in ret. Running this code produced the following output:
$ ./a.out infile.wav outfile.wav
avformat_open_input(): No such file or directory
But the infile.wav was there! Using strace I found that open system call wasn't called and so it was certainly an internal error. This was frustrating! The reason I started to write this code was to find out why I'm getting another error in VoIP tool simulator, but I was stuck on something even more basic: Not being able to open a wav file. Googling around I finally found the following link which explained me a real cause of the error, i.e. I forgot to call av_register_all() initialization function.

This again shows how important good error reporting is, in both libraries and application programs!

I had also another problem with the previous code. It was segfaulting within avformat_open_input(). At first, I thought that I found a bug within a library but then I realized I forgot to initialize pFormatCtx to NULL. Namely, allocating a dynamic variable on stack didn't zero it so it was non-NULL and avformat_open_input() misbehaved. Mea culpa! :)

8 comments:

Unknown said...
This comment has been removed by the author.
Unknown said...

I have a probleme with avformat_open_input() function;
it works fine with an http url, but it won't work with an mms url.. any ideas?

Unknown said...

I have a probleme with avformat_open_input() function;
it works fine with an http url, but it won't work with an mms url.. any ideas?

Stjepan Groš (sgros) said...

Are you certain that mms url is supported?

Unknown said...

ffmpeg doesn't support mms:// in the URL try mmsh:// instead. That fixed my problem

Unknown said...
This comment has been removed by the author.
Mark Glines said...

"I forgot to initialize pFormatCtx to NULL."

I ran into the same thing. This just saved me some valuable debugging time, thanks!

Unknown said...

thank you so much for that "I forgot to initialize pFormatCtx to NULL." bit. saved me several hours and headaches.

About Me

scientist, consultant, security specialist, networking guy, system administrator, philosopher ;)

Blog Archive