我如何在ffmpeg写帧方法中释放pkt [英] How may I free pkt in an ffmpeg write frame method

查看:77
本文介绍了我如何在ffmpeg写帧方法中释放pkt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候

我正在查看一个ffmpeg源代码示例: http:// svn.perian.org/ffmpeg/ffmpeg.c [ ^ ]



在代码中,我专注于编写视频帧的方法。



这是方法:



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);
    }
}





如果我按原样测试代码,我会在写入时遇到崩溃< u> ret = av_interleaved_write_frame(s,pkt);



但是如果我评论我们的// av_free_packet(pkt);



它工作正常,这就是问题,直到一段时间后,内存变得非常大。

我怀疑它是因为我已经注释掉了 av_free_packet(pkt)



关于为什么我会崩溃原始代码的任何想法(使用av_free_packet(pkt) ))?



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))?

推荐答案

好吧,我终于让它工作了,并将在这里发布我的特定解决方案,希望它可以帮助别人。哦,差点忘了。如果您有更改此代码的建议,请执行以下操作:



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>


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写帧方法中释放pkt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆