Opened 5 years ago

Closed 5 years ago

#520 closed defect (invalid)

Issue with using multiple avio_open_dyn_buf() / avio_close_dyn_buf()

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


I have a system requirement for building an ffmpeg wrapper library that takes both audio and images as inputs and output an mp4 video buffer as output. The inputs are of unknown length since they are being captured in real time and could be going for hours. Using avio_open_dyn_buf() and avio_close_dyn_buf() I am able get the buffer.

However, it is not a practical solution since it would require a huge amount of memory especially when there are multiple users with hour’s long recordings. So I tried reducing the amount of dynamic memory being used by doing multiple avio_open_dyn_buf() / avio_close_dyn_bufz() paired. That appears to corrupt my output as I am no longer able to play the output as an mp4 file.

ofstream ostream;
uint8_t *tmp = NULL;, ios::out | ios::binary);

if(avio_open_dyn_buf(&fmtCtx->pb) != 0) {

cout << "ERROR: Unable to open dynamic buffer" << endl;


avformat_write_header(fmtCtx, NULL);


  • multiple close/open of the dynamic buffer
  • is causing an issue with the playback. Why? */

int size2 = avio_close_dyn_buf(fmtCtx->pb, &tmp);
ostream.write((char*)tmp, size2);

if(avio_open_dyn_buf(&fmtCtx->pb) != 0) {

cout << "ERROR: Unable to open dynamic buffer" << endl;


for(int i=0; i < numOfImages; ++i) {

WriteVideoFrame?(fmtCtx, vidStream, vid);
WriteAudioFrame?(fmtCtx, audStream, aud);



CloseVideo?(fmtCtx, vidStream);
CloseAudio?(fmtCtx, audStream);

for(int i = 0; i < fmtCtx->nb_streams; i++) {



int size1 = avio_close_dyn_buf(fmtCtx->pb, &tmp);
ostream.write((char*)tmp, size1);


Change History (2)

comment:1 Changed 5 years ago by cehoyos

  • Priority changed from important to normal

comment:2 Changed 5 years ago by michael

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

av_write_trailer() can lead to updates of the area written in the header earlier for some formats.
Also replacing the context like you do is not possible, theres state in it that will be lost and lead to all kinds of odd issues.
You could implement your own URLProtocol or io context but probably what you really want is to use pipes and a format that works with non seekable output, mpeg-ps comes to mind here

So i think the issue is in how you use avio_open_dyn_buf() and not in ffmpeg, if you disagree feel free to reopen and elaborate

Note: See TracTickets for help on using tickets.