gpt4 book ai didi

c++ - av_bitstream_filter_filter 内存泄漏

转载 作者:行者123 更新时间:2023-12-04 23:33:47 38 4
gpt4 key购买 nike

我正在尝试使用 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)

最佳答案

感谢#ffmpeg 中的BtbN,我能够解决这个问题。

看来这里有几个陷阱:

  • av_free_packet 不释放 pkt.data
  • .. 但是 av_read_frame 确实

  • 所以在正常情况下,不需要释放 pkt.data,但是需要释放 av_bitstream_filter_filter 的输出。

    这是修复了泄漏的代码:
    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);
    }
    }

    }

    关于c++ - av_bitstream_filter_filter 内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53498403/

    38 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com