How may I free pkt in an ffmpeg write frame method

Rate this:
  
 
See
more: C++ ffmpeg
Greetings

I'm looking at an ffmpeg source code example at :http://svn.perian.org/ffmpeg/ffmpeg.c[^]

In the code, I'm focusing on a method that writes the video frame.

Here is that method:

Hide   Expand    Copy
Code
static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
int ret;
 
while(bsfc){
AVPacket new_pkt= *pkt;
int a= av_bitstream_filter_filter(bsfc, avctx, NULL,
&new_pkt.data, &new_pkt.size,
pkt->data, pkt->size,
pkt->flags & AV_PKT_FLAG_KEY);
if(a>0){
av_free_packet(pkt);
new_pkt.destruct= av_destruct_packet;
} else if(a<0){
fprintf(stderr, "%s failed for stream %d, codec %s",
bsfc->filter->name, pkt->stream_index,
avctx->codec ? avctx->codec->name : "copy");
print_error("", a);
if (exit_on_error)
exit_program(1);
}
*pkt= new_pkt;
 
bsfc= bsfc->next;
}
 
ret= av_interleaved_write_frame(s, pkt);
if(ret < 0){
print_error("av_interleaved_write_frame()", ret);
exit_program(1);
}
}

If I test the code as is, I get a crash at the write "ret= av_interleaved_write_frame(s, pkt);"

But if I comment our the //av_free_packet(pkt);

It works fine, and here is the problem, until after a while, memory grows very very large.

I suspect it's because I've commented out the av_free_packet(pkt)

Any ideas as to why I crash with the original code (with av_free_packet(pkt))?

Posted 28-Feb-14 12:33pm

radnix824
Updated 28-Feb-14 14:55pm

CHill60157.4K
Comments
The_Inventor 2-Mar-14
7:51am

You need to write one frame at a time. Also you are freeing a pointer to what it is that you are trying to write before you write it. Thus it crashes. If you don't free it at all you then keep allocating more and more RAM instead of writing to HDD file, and
freeing memory for next frame.
radnix 4-Mar-14
18:08pm

Thanks ! yes, you've got me thinking.....:)

2 solutions

Rate this:
  
 

Solution 1

Well, I did finally get it working and will post my particular solution here with the hope it may help others. Oh, almost forgot. If you have a suggestion for change to this code, please do:

Hide   Expand    Copy
Code
static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx, AVBitStreamFilterContext *bsfc){
int ret;
 
while(bsfc){
AVPacket new_pkt= *pkt;
int a= av_bitstream_filter_filter(bsfc, avctx, NULL,
&new_pkt.data, &new_pkt.size,
pkt->data, pkt->size,
pkt->flags & AV_PKT_FLAG_KEY);
if(a>0){
//av_free_packet(pkt);//-comment this out if (new_pkt.data != pkt->data)//-added this
{
av_free_packet(pkt);
 
pkt->data = new_pkt.data;
pkt->size = new_pkt.size;
 
pkt->destruct = av_destruct_packet;
}

  new_pkt.destruct= av_destruct_packet;
} else if(a&lt;0){
fprintf(stderr, &quot;%s failed for stream %d, codec %s&quot;,
bsfc-&gt;filter-&gt;name, pkt-&gt;stream_index,
avctx-&gt;codec ? avctx-&gt;codec-&gt;name : &quot;copy&quot;);
print_error(&quot;&quot;, a);
if (exit_on_error)
exit_program(1);
}
<b>// *pkt= new_pkt;//-comment this out</b> bsfc= bsfc-&gt;next;
}
 
ret= av_interleaved_write_frame(s, pkt);
 
av_free_packet(pkt);//-added here av_bitstream_filter_close(bsfc);//-added here   if(ret &lt; 0){
print_error(&quot;av_interleaved_write_frame()&quot;, ret);
exit_program(1);
}
}</pre>
  Permalink  
Posted 4-Mar-14 12:05pm

radnix824
Comments
The_Inventor 5-Mar-14
6:51am

Looks better. Just remember in a function like this, you need to use your 'dummy' variables correctly. The dummy receives the info, the function operates on the dummy, when the dummy is happy it spits it back in the format you wanted.
radnix 5-Mar-14
18:17pm

Excellent analysis and a pretty good step ahead for future application. The original code is from ffmpeg.c .
Rate this:
  
 

Solution 2

Hide   Copy Code
AVPacket new_pkt = pkt;
int a = av_bitstream_filter_filter(m_bsfDecoderContext, out_stream->codec,
NULL,
&new_pkt.data,
&new_pkt.size,
pkt.data,
pkt.size,
pkt.flags & AV_PKT_FLAG_KEY);
 
av_free_packet(&pkt);
pkt.data = new_pkt.data;
pkt.size = new_pkt.size;
 
if (av_interleaved_write_frame(ofmt_ctx, &pkt) < 0)break;
av_free(new_pkt.data);

【视频开发】关于FFMPEG中内存泄漏的问题之av_bitstream_filter_filter的更多相关文章

  1. C++中内存泄漏的检测方法介绍

    C++中内存泄漏的检测方法介绍 首先我们需要知道程序有没有内存泄露,然后定位到底是哪行代码出现内存泄露了,这样才能将其修复. 最简单的方法当然是借助于专业的检测工具,比较有名如BoundsCheck, ...

  2. Android开发常见的Activity中内存泄漏及解决办法

    上一篇文章楼主提到由Context引发的内存泄漏,在这一篇文章里,我们来谈谈Android开发中常见的Activity内存泄漏及解决办法.本文将会以“为什么”“怎么解决”的方式来介绍这几种内存泄漏. ...

  3. Cocos性能优化工具的开发介绍Visual Studio内存泄漏检测工具——Visual Leak Detector

    然后,Windows下有什么好的内存泄漏检測工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检測功能.我们能够使用第三方工具Visual Leak Detector(下面简 ...

  4. 浅谈C++中内存泄漏的检测

    首先我们需要知道程序有没有内存泄露,然后定位到底是哪行代码出现内存泄露了,这样才能将其修复.最简单的方法当然是借助于专业的检测工具,比较有名如BoundsCheck,功能非常强大,相信做C++开发的人 ...

  5. 每日一问:Android 中内存泄漏都有哪些注意点?

    内存泄漏对每一位 Android 开发一定是司空见惯,大家或多或少都肯定有些许接触.大家都知道,每一个手机都有一定的承载上限,多处的内存泄漏堆积一定会堆积如山,最终出现内存爆炸 OOM. 而这,也是极 ...

  6. 【视频开发】ffmpeg实现dxva2硬件加速

    这几天在做dxva2硬件加速,找不到什么资料,翻译了一下微软的两篇相关文档.这是第二篇,记录用ffmpeg实现dxva2. 第一篇翻译的Direct3D device manager,链接:http: ...

  7. 什么是内存溢出以及java中内存泄漏5种情况的总结

    内存泄漏定义(memory leak):一个不再被程序使用的对象或变量还在内存中占有存储空间. 一次内存泄漏似乎不会有大的影响,但内存泄漏堆积后的后果就是内存溢出.内存溢出 out of memory ...

  8. linux中内存泄漏的检測(五)记录内存泄漏的代码

    到眼下为止,先后通过wrap malloc.new函数重载和计算指针内存大小的方法.基本上满足了对内存泄漏检測的须要. 假设发现了内存泄漏.那么就要找到内存泄漏的地方而且修正它了. 茫茫代码.如何去找 ...

  9. linux中内存泄漏的检測(一)最简单的方法

    什么是内存泄漏 内存泄漏是指程序动态申请的内存在使用完后没有释放,导致这段内存不能被操作系统回收再利用. 比如这段程序,申请了4个字节的空间但没有释放,有4个字节的内存泄漏. #include < ...

随机推荐

  1. HDU4091:Zombie’s Treasure Chest (分类-数学)

    题意:给两种宝石,体积S1,S2,价值V1,V2,背包容量n,求最大收益. 所有数据都在32位整数范围内. 思路:只有两种物品的背包,显然不是常见的背包,应该从背包之外的思路下手. 1:可以猜想其中一 ...

  2. Mybatis框架-Delete节点元素的使用

    这个就也比较简单,需求:将我们最新插入的那条数据删除掉,从用户表中. UserMapper.xml UserMapper.java 编写测试方法: @Test public void testDele ...

  3. CF547E Mike and Friends 后缀自动机+线段树合并

    裸题,敲完后没调就过了 ~ code: #include <bits/stdc++.h> using namespace std; #define ll long long #define ...

  4. vue-cli搭建SPA项目

    1. 什么是vue-cli? vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下: vue init webpack xxx 2. 安装vue-cl ...

  5. 算法:贪心、回溯(su)、分治、动态规划,思想简要

    贪心算法: 只做出当前看来最好的选择,而不从整体考虑最优,他所作出的是局部最优解.使用该算法的前提是必须具备无后效性,即某个状态以前的选择不会影响以后的状态的选择,只与当前状态有关. 回溯算法: 本质 ...

  6. 深搜的剪枝技巧(三)——Sticks(可行性剪枝、上下界剪枝、最优性剪枝)

    小木棍(最优性剪枝.可行性剪枝) 一.问题描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,已知每段的长都不超过 50 .现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍 ...

  7. 【0521模拟赛】小Z爱划水

    题目描述 小Z和其它机房同学都面临一个艰难的抉择,那就是 要不要划水? 每个人都有自己的一个意见,有的人想做题,有的人想划水. 当然,每个人只能选择一个事情做.如果一个人做的事情和他想做的不同,那么他 ...

  8. 使用docker 基于centos7制作mysql镜像

    说明:由于业务需要使用centos7.6+mysql5.7+jdk8以及其他的java程序,本想在网上找一个现成的,发现镜像都不适合我. 一.yum方式安装mysql 1.编写dockerfile文件 ...

  9. httpd.exe你的电脑中缺失msvcr110.dll怎么办(WIN2008服务器环境装WAMP2.5出现的问题)

    httpd.exe你的电脑中缺失msvcr110.dll怎么办 去微软官方下载相应的文件 1 打开上面说的网址 Download and install, if you not have it alr ...

  10. iwms后台编辑器无法粘贴word格式的解决方法

    iwms后台编辑器用的是tiny_mce,默认会自动过滤word粘贴中的格式,以减小数据库的占用,但在word中辛苦做的字体和格式都不见了,可采用下方法关闭编辑器的自动清除格式功能. 编辑文件:\ti ...