Opened 12 years ago

Closed 12 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

Description

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;

ostream.open(filename, ios::out | ios::binary);

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

cout << "ERROR: Unable to open dynamic buffer" << endl;
exit(1);

}

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);
av_free(tmp);

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

cout << "ERROR: Unable to open dynamic buffer" << endl;
exit(1);

}

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

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

}

av_write_trailer(fmtCtx);

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

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

av_freep(&fmtCtx->streams[i]->codec);
av_freep(&fmtCtx->streams[i]);

}

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

av_free(fmtCtx);

Change History (2)

comment:1 by Carl Eugen Hoyos, 12 years ago

Priority: importantnormal

comment:2 by Michael Niedermayer, 12 years ago

Resolution: invalid
Status: newclosed

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.