Opened 5 years ago

Closed 4 years ago

Last modified 4 years ago

#984 closed defect (worksforme)

avio_alloc_context and callback on write

Reported by: mrdebug Owned by:
Priority: normal Component: avformat
Version: unspecified Keywords:
Cc: Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

For my application is essential use the callbacks. I have understood how to use callback on decoding but not on encoding.

If is right, on order to obtain an encoded file from a sequence of images the steps are:
1) initialize AVOutputFormat
2) initialize AVFormatContext and referred it to AVOutputFormat
3) use avio_open for open the output file
4) initialize AVStream
5) initialize AVCodec
6) using avcodec_encode_video, AVPacket and av_interleaved_write_frame is possible to store the inage into the output file.

That works. I can produce webm, mp4 and mpg file. But now the goal is to modify the sequence in order to use callback on write event.

I have modified the sequence like this

0) AVIOContext= avio_alloc_context(pBuffer, BUFFERSIZE, 0, this, NULL, WritePacket?, NULL);
1) initialize AVOutputFormat
2) initialize AVFormatContext and referred it to AVOutputFormat and AVIOContext
"pAVFormatContext->oformat= pAVOutputFormat;"
"pAVFormatContext->pb= pAVIOContext;"
3) remove avio_open for open the output file
4) initialize AVStream
5) initialize AVCodec
6) using avcodec_encode_video, AVPacket and av_interleaved_write_frame.

but does not work with all codecs. For example works using mpeg or webm but not with mp4 (x264) The WritePacket? callback is always called but the produced mp4 file is not readable from mplayer or cvlc. The webm or mpg files work well instead.

Attachments (2)

qthcamera.cpp (20.0 KB) - added by mrdebug 5 years ago.
It's a thread that grab images with opencv and uses ffmpeg to generate a mp4 file
main.c (7.8 KB) - added by mrdebug 4 years ago.
C source for get the bug

Download all attachments as: .zip

Change History (8)

Changed 5 years ago by mrdebug

It's a thread that grab images with opencv and uses ffmpeg to generate a mp4 file

comment:1 Changed 5 years ago by cehoyos

  • Priority changed from critical to normal

comment:2 Changed 5 years ago by mrdebug

Do you think is a good idea to upload an C example instead of C++ (Qt)?

Changed 4 years ago by mrdebug

C source for get the bug

comment:3 Changed 4 years ago by mrdebug

After a lot of time on studying FFmpeg I think there is something that does
not works correctly. In order to produce a file you can use avio_open or
avio_open_dyn_buf functions. But these functions are used for obtain a file.
There is another approach, a callback using avio_alloc_context function. For
me this approach is a lot important but does not works correctly!
How you can try by using the attached source code you can obtain an avi file
correctly but if you try mp4 or webm the produced files can not be played with
vlc or other.
With ogg the program goes to crash.
Is my code wrong or is a problem of FFmpeg?

See the C source for a quick check.

I hope you can help me.

comment:4 Changed 4 years ago by reimar

  • Resolution set to fixed
  • Status changed from new to closed

You do not provide a seek function for the avio context, so completely unsurprisingly the formats that are not "naturally" streamable fail.
In addition, you do not even set the "streamable" AVIO context member to indicate that streaming will not be possible, so even for those formats that can be streamed and FFmpeg implemented that feature properly wouldn't work correctly.
However I cannot reproduce the crash with ogg using:
ffmpeg -i some_file -f ogg - > test.ogg
Without a proper backtrace and valgrind log I'm not going to debug this further than that.

comment:5 Changed 4 years ago by reimar

  • Resolution changed from fixed to worksforme

comment:6 Changed 4 years ago by mrdebug

Thanks for your quickly reply. Where I can found the list of the streamable formats and some documentation? Is my approach completely wrong or with some correction can works? Have you got an example on using callback?

Thanks thousand.

Note: See TracTickets for help on using tickets.