av_bitstream_filter_filtermemoryfree-朱韦刚(zhuweigangzwg)的技术博客-博客频道-.NET朱韦刚(zhuweigangzwg)的技术博客QQ:379969650QQ技术群:62054820目录视图摘要视图订阅【技术主题月】物联网全栈开发 &..." />
javaee论坛

普通会员

225648

帖子

345

回复

359

积分

楼主
发表于 2017-06-08 10:58:59 | 查看: 1235 | 回复: 1
tml xmlns="http://www.w3.org/1999/xhtml"> av_bitstream_filter_filter memory free - 朱韦刚(zhuweigangzwg)的技术博客 - 博客频道 - .NET

av_bitstream_filter_filter memory free

439人阅读 评论(0) 收藏 举报
分类:

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:

< lang="cpp" id="737572" class="notranslate" name="code" style="white-space: -wrap; word-wrap: break-word; color: rgb(0, 0, 0); padding: 6px; font-size: 9pt; font-family: Consolas, "Courier New", Courier, mono; border: 1px solid rgb(251, 237, 187); overflow: auto; word-break: break-word; margin-top: 0px; height: 400px; background-color: rgb(251, 237, 187);">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
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.....:)
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:

< lang="cs" id="402627" class="notranslate" name="code" style="white-space: -wrap; word-wrap: break-word; color: rgb(0, 0, 0); padding: 6px; font-size: 9pt; font-family: Consolas, "Courier New", Courier, mono; border: 1px solid rgb(251, 237, 187); overflow: auto; word-break: break-word; margin-top: 0px; height: 400px; background-color: rgb(251, 237, 187);">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); }}</>
  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 tty good step ahead for future application. The original code is from ffmpeg.c .
Rate this:bad
  
good 

Solution 2

< code_snippet_id="2015265" snippet_file_name="blog_20161129_3_8346608" lang="c++" id="412825" class="notranslate" name="code" style="white-space: -wrap; word-wrap: break-word; color: rgb(0, 0, 0); padding: 6px; font-size: 9pt; font-family: Consolas, "Courier New", Courier, mono; border: 1px solid rgb(251, 237, 187); word-break: break-word; margin-top: 0px; background-color: rgb(251, 237, 187); overflow: auto !important;">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);

普通会员

0

帖子

323

回复

331

积分
沙发
发表于 2022-12-16 11:54:02

百因必有果你的报应就是我

您需要登录后才可以回帖 登录 | 立即注册

触屏版| 电脑版

技术支持 历史网 V2.0 © 2016-2017