【FFmpeg】解码时refcounted_frames标志的使用

127 篇文章 133 订阅 ¥69.90 ¥99.00

【目录】郭老二博文之:图像视频汇总

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 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
使用FFmpeg解码B帧和视频抽帧,可以结合使用avcodec_send_packet()和avcodec_receive_frame()函数进行解码,然后使用av_write_frame()函数将解码后的帧写入输出文件。具体步骤如下: 1. 初始化FFmpeg ``` av_register_all(); avcodec_register_all(); ``` 2. 打开输入文件 ``` AVFormatContext *inputFormatCtx = NULL; avformat_open_input(&inputFormatCtx, inputFilePath, NULL, NULL); avformat_find_stream_info(inputFormatCtx, NULL); ``` 3. 找到视频流 ``` AVCodec *inputCodec = NULL; int videoStreamIndex = av_find_best_stream(inputFormatCtx, AVMEDIA_TYPE_VIDEO, -1, -1, &inputCodec, 0); AVCodecContext *inputCodecCtx = inputFormatCtx->streams[videoStreamIndex]->codec; ``` 4. 打开解码器 ``` avcodec_open2(inputCodecCtx, inputCodec, NULL); ``` 5. 初始化输出文件 ``` AVFormatContext *outputFormatCtx = NULL; avformat_alloc_output_context2(&outputFormatCtx, NULL, NULL, outputFilePath); avio_open(&outputFormatCtx->pb, outputFilePath, AVIO_FLAG_WRITE); ``` 6. 写入输出文件头 ``` avformat_write_header(outputFormatCtx, NULL); ``` 7. 读取帧数据并解码 ``` AVPacket packet; AVFrame *frame = av_frame_alloc(); while (av_read_frame(inputFormatCtx, &packet) == 0) { if (packet.stream_index == videoStreamIndex) { avcodec_send_packet(inputCodecCtx, &packet); while (avcodec_receive_frame(inputCodecCtx, frame) == 0) { // do something with the decoded frame, e.g. write to output file av_write_frame(outputFormatCtx, frame); } } av_packet_unref(&packet); } ``` 8. 写入输出文件尾 ``` av_write_trailer(outputFormatCtx); ``` 9. 释放资源 ``` avformat_close_input(&inputFormatCtx); avcodec_free_context(&inputCodecCtx); avformat_free_context(inputFormatCtx); avformat_free_context(outputFormatCtx); av_frame_free(&frame); ``` 这样,就可以使用FFmpeg解码B帧和视频抽帧了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭老二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值