作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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);
}
}
}
=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,我能够解决这个问题。
看来这里有几个陷阱:
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/
我正在尝试使用 ffmpeg 解码和转换 h264 比特流。 我的代码有效,但在调用 av_bitstream_filter_filter 时泄漏非常严重. 我看不出我做错了什么——这可能是 ffmp
我是一名优秀的程序员,十分优秀!