av_bitstream_filter_filter memory free

转载 2016年08月31日 09:21:37

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:

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: bad
  
good  

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:

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  
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: bad
  
good  

Solution 2

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库函数的坑

项目中使用ffmpeg库处理视频时掉进了两个内存泄漏的坑。1. av_bitstream_filter_filter下面这种用法,av_bitstream_filter_filter(h264bsfc...
  • BikeyTang
  • BikeyTang
  • 2017年03月08日 22:16
  • 1862

av_bitstream_filter_filter memory free

How may I free pkt in an ffmpeg write frame method Rate this: ...
  • zhuweigangzwg
  • zhuweigangzwg
  • 2016年11月29日 14:30
  • 1333

分离(MP4/FLV/MKV等)中的H.264视频信息 保存并播放

参考http://blog.csdn.net/leixiaohua1020/article/details/11800877 在分离H.264码流的时候,直接存储AVPacket后的文件可能是不...
  • tropicofcancer9
  • tropicofcancer9
  • 2017年01月09日 11:13
  • 591

【视频开发】关于FFMPEG中内存泄漏的问题之av_bitstream_filter_filter

How may I free pkt in an ffmpeg write frame method Rate this: ...
  • LG1259156776
  • LG1259156776
  • 2017年06月15日 14:04
  • 1180

Linux Memory Usage: free

Episode 008 – free: Understanding Linux Memory Usage Short answer: Cached is the size of the pag...
  • JJRFJYFJYFJDFJRUJDJD
  • JJRFJYFJYFJDFJRUJDJD
  • 2014年12月09日 19:48
  • 647

使用FFMPEG类库分离出多媒体文件中的H.264码流

在使用FFMPEG的类库进行编程的过程中,可以直接输出解复用之后的的视频数据码流。只需要在每次调用av_read_frame()之后将得到的视频的AVPacket存为本地文件即可。 经试验,在分离M...
  • leixiaohua1020
  • leixiaohua1020
  • 2013年09月18日 11:15
  • 26249

biTStream功能介绍与应用

一、 背景在上一篇《浅析mpeg-ts封装结构》中介绍了mpegts流的封装格式,接下来在了解了基本结构之后就是如何解析了。如果自己写代码一个位一个位的去判断并处理,这固然很有乐趣,但你的老板并...
  • michaeluo
  • michaeluo
  • 2017年07月20日 17:51
  • 181

BEA-310003 There is danger of OutOfMemoryError

最近忙一个项目,发现项目在启动一段时间总是出现下面的情况:   在Oracle官网查询得到如下错误解释: BEA-310003 Critical: Fre...
  • PrinceTerence
  • PrinceTerence
  • 2012年12月20日 11:37
  • 6603

WCF 内存入口检查失败 Memory gates checking failed

WCF 内存入口检查失败 Memory gates checking failed
  • wangdan199112
  • wangdan199112
  • 2015年02月28日 10:24
  • 2167

FFMPEG的一些使用技巧

1 在vc中使用ffmpeg静态库做二次开发的时候,如果不是控制台应用程序,又需要查看ffmpeg的日志信息,可使用 av_log_set_callback 函数注册一个回调函数,如 void ff_...
  • zxwangyun
  • zxwangyun
  • 2013年07月26日 21:34
  • 3839
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:av_bitstream_filter_filter memory free
举报原因:
原因补充:

(最多只允许输入30个字)