1、refcounted_frames说明
在接口 avcodec_decode_video2 的注释中,有关于 refcounted_frames 的详细说明:
(1)当 AVCodecContext.refcounted_frames 被设置为1,该 AVFrame 被引用计数,返回的引用属于调用者。当不再需要 AVFrame 时,调用者必须使用 av_frame_unref() 来释放frame。只有在 av_frame_is_writable() 返回1,调用者才可以向frame中写入数据。
(2)当 AVCodecContext.refcounted_frames 被设置为0,返回的引用属于解码器,只有在下一次调用该函数或关闭或刷新解码器之前有效。调用者不能向 AVFrame 中写入数据。
2、启动该标志,
在执行打开编解码器时,用avcodec_open2设置:
//AVCodecContext *dec_ctx;
//AVCodec *dec;
AVDictionary *opts = NULL;
int refcount = 1;
av_dict_set(&opts, "refcounted_frames", refcount, 0);
avcodec_open2(dec_ctx, dec, &opts)
3、解码后,记得释放
AVFrame *frame = av_frame_alloc();
AVPacket pkt;
int got_frame
avcodec_decode_video2(dec_ctx, frame, &got_frame, &pkt);
// dosometihing()
if (got_frame && refcount)
av_frame_unref(frame)