I tend to be a lazy coder, so I occasionally look at other people's code to see how stuff should be done.
I looked at how you used a bitstream filter h264_mp4toannexb in your code. I must say I had written a couple of bitstream converters between ISO and annexb but this time I wanted to use the stock ffmpeg filter. And my valgrind was not very pleased with how I used h264_mp4toannexb, which was exactly as you did. I mean, there was a huge leak, all of the incoming packet data leaked while being converted to annexb.
ffmpeg is way too poorly documented so I often look into its code to see how it really works, because documentation is so scarce. So I came up with this:
Hi Akagi201,
I tend to be a lazy coder, so I occasionally look at other people's code to see how stuff should be done. I looked at how you used a bitstream filter h264_mp4toannexb in your code. I must say I had written a couple of bitstream converters between ISO and annexb but this time I wanted to use the stock ffmpeg filter. And my valgrind was not very pleased with how I used h264_mp4toannexb, which was exactly as you did. I mean, there was a huge leak, all of the incoming packet data leaked while being converted to annexb. ffmpeg is way too poorly documented so I often look into its code to see how it really works, because documentation is so scarce. So I came up with this:
instead of simply calling av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);
which will allocate a new buffer and place it in pkt.data, while leaking the original buffer stored in pkt.data, I wrote this:
uint8_t* outBuf = NULL; int outBufSize = 0; av_bitstream_filter_filter(h264bsfc, in_stream->codec, NULL, &outBuf, &outBufSize, pkt.data, pkt.size, 0); av_free_packet(&pkt); av_packet_from_data(&pkt, outBuf, outbufSize);
No more complaints from valgrind now. The leak is gone.
Cheers Greg