-1

I'm attempting to use ffmpeg to decode and convert an h264 bitstream.

My code works, but it's leaking quite badly on the call to av_bitstream_filter_filter.

I can't see what I'm doing wrong - could this be an ffmpeg internal bug? Am I handling some resource incorrectly?

Edit: The return value of av_bitstream_filter_filter is always 1.

extern "C" {
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
}

#include <string>

AVBitStreamFilterContext *annexb = nullptr;
int videoStreamIdx = -1;
AVFormatContext *fmt_ctx = nullptr;
AVPacket pkt;
AVPacket pkt2;

main(int argc, char *argv[])
{
        av_register_all();
        avformat_network_init();
        annexb = av_bitstream_filter_init("h264_mp4toannexb");

        std::string fn = "epicloop.mp4";

        int err = avformat_open_input(&fmt_ctx, fn.c_str(), nullptr, nullptr);
        if (err < 0)
        {
                printf("Unable to open stream hex: %x", err);
                return -1;
        }

        if (avformat_find_stream_info(fmt_ctx, nullptr) < 0)
        {
                printf("Unable to retrieve stream info");
                return -2;
        }

        for (unsigned int i = 0; i < fmt_ctx->nb_streams; i++)
        {
                AVStream * pStream = fmt_ctx->streams[i];
                switch (pStream->codec->codec_type)
                {
                        case AVMEDIA_TYPE_VIDEO:
                                videoStreamIdx = i;
                                break;
                }
        }

        if (videoStreamIdx == -1)
        {
                printf("Unable to find video stream");
                return -3;
        }

        av_init_packet(&pkt);
        av_init_packet(&pkt2);
        pkt.data = nullptr;
        pkt.size = 0;
        pkt2.data = nullptr;
        pkt2.size = 0;

        int result = 0;
        while(true)
        {
                if (pkt.data != nullptr)
                {
                        av_free_packet(&pkt);
                        pkt.data = nullptr;
                        pkt.size = 0;
                }
                if (pkt2.data != nullptr)
                {
                        av_free_packet(&pkt2);
                        pkt2.data = nullptr;
                        pkt2.size = 0;
                }
                result = av_read_frame(fmt_ctx, &pkt);
                if (result < 0)
                {
                        printf("Failed to read frame. EOS?");
                        result = av_seek_frame(fmt_ctx, -1, 0, AVSEEK_FLAG_BACKWARD);
                        if (result < 0)
                        {
                                printf("Failed to seek");
                                return result;
                        }
                        else
                        {
                                result = av_read_frame(fmt_ctx, &pkt);
                                if (result < 0)
                                {
                                        printf("Failed to read frame after seek");
                                        return result;
                                }
                        }
                }
                if (pkt.stream_index == videoStreamIdx)
                {
                        int a = av_bitstream_filter_filter(annexb,
                                               fmt_ctx->streams[pkt.stream_index]->codec,
                                               NULL,
                                               &pkt2.data,
                                               &pkt2.size,
                                               pkt.data,
                                               pkt.size,
                                               pkt.flags & AV_PKT_FLAG_KEY);

                        av_free_packet(&pkt);
                        pkt.data = NULL;
                        pkt.size = 0;

                        /*    HANDLE VIDEO IN PKT2    */
                        printf(".");
                        fflush(stdout);
                }
        }
}

Here's what Valgrind has to say:

=19276== Process terminating with default action of signal 2 (SIGINT)
==19276==    at 0x734B363: read (in /lib64/libc-2.26.so)
==19276==    by 0x656BED6: ??? (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x65508F3: ??? (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x655276B: avio_read (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x664DBBF: ??? (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x65BA94E: ??? (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x6651A69: ??? (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x665228B: ??? (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x6653017: av_read_frame (in /usr/lib64/libavformat.so.57.71.100)
==19276==    by 0x109188: main (in /root/a.out)
==19276== 
==19276== HEAP SUMMARY:
==19276==     in use at exit: 2,643,751,020 bytes in 51,065 blocks
==19276==   total heap usage: 707,985 allocs, 656,920 frees, 8,060,126,970 bytes allocated
==19276== 
==19276== 8,303,769 bytes in 81 blocks are possibly lost in loss record 67 of 68
==19276==    at 0x4C2F216: memalign (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==19276==    by 0x4C2F331: posix_memalign (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==19276==    by 0x62B5CF2: av_malloc (in /usr/lib64/libavutil.so.55.58.100)
==19276==    by 0x4F7BFB9: av_bitstream_filter_filter (in /usr/lib64/libavcodec.so.57.89.100)
==19276==    by 0x109293: main (in /root/a.out)
==19276== 
==19276== 2,635,145,556 bytes in 50,896 blocks are definitely lost in loss record 68 of 68
==19276==    at 0x4C2F216: memalign (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==19276==    by 0x4C2F331: posix_memalign (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==19276==    by 0x62B5CF2: av_malloc (in /usr/lib64/libavutil.so.55.58.100)
==19276==    by 0x4F7BFB9: av_bitstream_filter_filter (in /usr/lib64/libavcodec.so.57.89.100)
==19276==    by 0x109293: main (in /root/a.out)
==19276== 
==19276== LEAK SUMMARY:
==19276==    definitely lost: 2,635,145,556 bytes in 50,896 blocks
==19276==    indirectly lost: 0 bytes in 0 blocks
==19276==      possibly lost: 8,303,769 bytes in 81 blocks
==19276==    still reachable: 301,695 bytes in 88 blocks
==19276==         suppressed: 0 bytes in 0 blocks
==19276== Reachable blocks (those to which a pointer was found) are not shown.
==19276== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==19276== 
==19276== For counts of detected and suppressed errors, rerun with: -v
==19276== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

1 Answer 1

0

Thanks to BtbN in #ffmpeg I was able to resolve this.

It appears that there are a couple of gotchas here:

  • av_free_packet does not free pkt.data
  • .. but av_read_frame does

So under normal circumstances, it's not necessary to free pkt.data, however it is necessary to free the output from av_bitstream_filter_filter.

Here's the code with the leak fixed:

extern "C" {
    #include <libavcodec/avcodec.h>
    #include <libavformat/avformat.h>
}

#include <string>

AVBitStreamFilterContext *annexb = nullptr;
int videoStreamIdx = -1;
AVFormatContext *fmt_ctx = nullptr;
AVPacket pkt;
AVPacket pkt2;

main(int argc, char *argv[])
{
        av_register_all();
        avformat_network_init();
        annexb = av_bitstream_filter_init("h264_mp4toannexb");

        std::string fn = "epicloop.mp4";

        int err = avformat_open_input(&fmt_ctx, fn.c_str(), nullptr, nullptr);
        if (err < 0)
        {
                printf("Unable to open stream hex: %x", err);
                return -1;
        }

        if (avformat_find_stream_info(fmt_ctx, nullptr) < 0)
        {
                printf("Unable to retrieve stream info");
                return -2;
        }

        for (unsigned int i = 0; i < fmt_ctx->nb_streams; i++)
        {
                AVStream * pStream = fmt_ctx->streams[i];
                switch (pStream->codec->codec_type)
                {
                        case AVMEDIA_TYPE_VIDEO:
                                videoStreamIdx = i;
                                break;
                }
        }

        if (videoStreamIdx == -1)
        {
                printf("Unable to find video stream");
                return -3;
        }

        av_init_packet(&pkt);
        av_init_packet(&pkt2);
        pkt.data = nullptr;
        pkt.size = 0;
        pkt2.data = nullptr;
        pkt2.size = 0;

        int result = 0;
        while(true)
        {
                if (pkt2.data != nullptr)
                {
                        av_freep(&pkt2.data);
                        av_packet_free_side_data(&pkt2);
                        av_free_packet(&pkt2);
                        pkt2.data = nullptr;
                        pkt2.size = 0;
                }
                if (pkt.data != nullptr)
                {
                        av_free_packet(&pkt);
                }
                result = av_read_frame(fmt_ctx, &pkt);
                if (result < 0)
                {
                       printf("Failed to read frame. EOS?");
                        result = av_seek_frame(fmt_ctx, -1, 0, AVSEEK_FLAG_BACKWARD);
                        if (result < 0)
                        {
                                printf("Failed to seek");
                                return result;
                        }
                        else
                        {
                                result = av_read_frame(fmt_ctx, &pkt);
                                if (result < 0)
                                {
                                        printf("Failed to read frame after seek");
                                        return result;
                                }
                        }
                }
                if (pkt.stream_index == videoStreamIdx)
                {
                        int a = av_bitstream_filter_filter(annexb,
                                               fmt_ctx->streams[pkt.stream_index]->codec,
                                               NULL,
                                               &pkt2.data,
                                               &pkt2.size,
                                               pkt.data,
                                               pkt.size,
                                               pkt.flags & AV_PKT_FLAG_KEY);
                        /*    HANDLE VIDEO IN PKT2    */
                        printf(".");
                        fflush(stdout);
                }
        }

}
Sign up to request clarification or add additional context in comments.

2 Comments

Wouldn't it be best to also close the filter and the input to ensure full cleanup after the while loop? i.e. : av_bitstream_filter_close(annexb ); and avformat_close_input(&fmt_ctx);
Well, it's in main() which means these resources get freed automatically when the program ends. But yes, normally you would.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.