Утечка памяти av_bitstream_filter_filter

Tom Mettam спросил: 03 ноября 2018 в 09:32 в: c++

Я пытаюсь использовать ffmpeg для декодирования и преобразования битового потока h264.

Мой код работает, но он очень плохо просачивается при вызове av_bitstream_filter_filter.

Я не вижу, что я делаю неправильно - это может быть внутренняя ошибка ffmpeg? Я неправильно обрабатываю какой-то ресурс?

Редактировать: возвращаемое значение av_bitstream_filter_filter всегда равно 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);
                }
        }
}

Вот что говорит Valgrind:

=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)

0 ответов