ffmpeg 的各种声明已被否决,整理

目前用ffmpeg 3.4 version ,在 Windows 上开发。遇到各种声明已被否决,这里记录一下:

1、warning C4996: 'AVStream::codec': 被声明为已否决

解决方案:

  1. 原版:        if (pFmtCtx_Video_in->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
  2. 修改之后: if (pFmtCtx_Video_in->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)

  • 原版:        pCodecCtx_Video_in = pFmtCtx_Video_in->streams[VideoIndex]->codec;
  • 修改之后:
  1. pCodecCtx_Video_in = avcodec_alloc_context3(NULL); // 一定要注意,初始化,否则直接写下面那句会报错
  2. avcodec_parameters_to_context(pCodecCtx_Video_in, pFmtCtx_Video_in->streams[VideoIndex]->codecpar)
  3. 注意:视频解码器这样设置没问题,音频解码器也这样设置,就出错了。不知道为啥。

2、 warning C4996: 'avpicture_get_size': 被声明为已否决

解决方案:

原版: frameYUV_size = avpicture_get_size(AV_PIX_FMT_YUV420P, 
                    pCodecCtx_Video_in->width, pCodecCtx_Video_in->height);

改版: frameYUV_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, 
                     pCodecCtx_Video_in->width, pCodecCtx_Video_in->height, 1);// 1按1字节对齐,实际大小。4按4字节对齐,实际大小的4倍

参考链接

3、warning C4996: 'avpicture_fill': 被声明为已否决

解决方案:pFrameYUV,是将RGB转换成YUV之后,存储YUV一帧图像用的。 out_buffer是YUV的存储空间。下面两个宽高,是图像的宽高(由于输出的宽高=输入的宽高,所以整个例子中只有一组宽高)。

详细的内容,参考链接:链接1 链接2.

4、warning C4996: 'avcodec_encode_video2': 被声明为已否决

看源码,可知,这个函数,被另外两个函数代替了。

解决方案:

  • avcodec_send_frame(avctx, frame);
  • avcodec_receive_packet(avctx, &avpkt);  //AVPacket avpkt; 

改变之后,不能编码成功!不能输出MP4文件。

5、warning C4996: 'av_free_packet': 被声明为已否决

看源码可知,已被 av_packet_unref() 取代

解决方案:

6、warning C4996: 'avcodec_encode_audio2': 被声明为已否决

看源码,可知,这个函数,被另外两个函数代替了

解决方案:

改变之后,不能编码成功!不能输出MP4文件。

7、 warning C4996: 'avcodec_decode_audio4': 被声明为已否决

看源码,可知,这个函数,被另外两个函数代替了

解决方案:

改变之后,不能编码成功!不能输出MP4文件。

8、 warning C4996: 'avcodec_decode_video2': 被声明为已否决

看源码,可知,这个函数,被另外两个函数代替了

解决方案:

改变之后,不能编码成功!不能输出MP4文件。

在Windows上,使用ffmpeg开发录制视频的demo。从网上找的例子,可能用的ffmpeg版本比较旧,所以会有各种声明已被否决。

参考:ffmpeg 的各种声明已被否决,整理_qq_34732729的博客-CSDN博客_avcodec_encode_video2

-------------------------------------------------------------------------------------------------------------------------------------

4.1版本弃用清单

ffmpeg API里面有各种接口改变的记录,如果发现接口不能用了,可以去搜索文档,可以找到对应的新接口,然后到新接口对应的头文件中找到说明文字。

FFmpeg: Deprecated List
在这里插入图片描述

常见的替换的API

1) 不认识guess_format.
解决: #define guess_format av_guess_format
接口不变。

2) 不认识av_alloc_format_context
解决: #define av_alloc_format_context avformat_alloc_output_context
接口调整

3) 不认识CODEC_TYPE_VIDEO 和 CODEC_TYPE_AUDIO
解决:
#define CODEC_TYPE_VIDEO AVMEDIA_TYPE_VIDEO
#define CODEC_TYPE_AUDIO AVMEDIA_TYPE_AUDIO

4) 不认识audio_resample_init
解决:#define audio_resample_init av_audio_resample_init
接口调整。

5) avcodec_decode_video 到 avcodec_decode_video2接口调整
旧代码:
len = avcodec_decode_video(c, (short *)outbuf, &out_size, inbuf_ptr, size);
新代码:

av_init_packet(&pkt);
pkt.data = (unsigned char*)inbuf_ptr;
pkt.size = size;
len = avcodec_decode_video2(c, &tmpFrame, &got_picture, &pkt);

6) 新的ffmpeg库不需要集中初始化的组建
旧的接口:av_register_all();
新版的API,此处不需要注册组建

7)新的API中将AVStream结构体中codec作了遗弃处理,当需要解码器上下文的时候,需要用AVCodecParameters去转化,解决方案是如下

AVCodecContext *avCodecContext = avcodec_alloc_context3(NULL);
if (avCodecContext == NULL) {
return;
}
// avcodec_parameters_to_context(AVCodecContext *codec, constAVCodecParameters *par)
// 将新的API中的 codecpar 转成 AVCodecContext
// 此处的avcodecParameters 是AVStream中的codecpar属性,codecpar里面包含了视频基础信息,
// 但是此处不能直接使用codecpar中的width和height,否者会报错误,错误如下
// Assertion desc failed at src/libswscale/swscale_internal.h:668
avcodec_parameters_to_context(avCodecContext, avcodecParameters);

8)PIX_FMT_YUV420P -> AV_PIX_FMT_YUV420P

‘AVStream::codec’: 被声明为已否决:

if(pFormatCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO){
=>
if(pFormatCtx->streams[i]->codecpar->codec_type==AVMEDIA_TYPE_VIDEO){

‘avpicture_get_size’: 被声明为已否决:

avpicture_get_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height)
=>
#include "libavutil/imgutils.h"
av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx-height, 1)

‘avpicture_fill’: 被声明为已否决:

avpicture_fill((AVPicture *)pFrameYUV, out_buffer, AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height);
=>
av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer,AV_PIX_FMT_YUV420P, pCodecCtx->width, pCodecCtx->height, 1);

‘avcodec_decode_video2’: 被声明为已否决:

	ret = avcodec_decode_video2(pCodecCtx, pFrame, &got_picture, packet); //got_picture_ptr Zero if no frame could be decompressed
	=>
	ret = avcodec_send_packet(pCodecCtx, packet);
	got_picture = avcodec_receive_frame(pCodecCtx, pFrame); //got_picture = 0 success, a frame was returned
	//注意:got_picture含义相反
或者:
	int ret = avcodec_send_packet(aCodecCtx, &pkt);
	if (ret != 0)
	{
	prinitf("%s/n","error");
	return;
	}
	while( avcodec_receive_frame(aCodecCtx, &frame) == 0){
	//读取到一帧音频或者视频
	//处理解码后音视频 frame
}

‘av_free_packet’: 被声明为已否决:

av_free_packet(packet);
=>
av_packet_unref(packet);

avcodec_decode_audio4:被声明为已否决:

int ret = avcodec_send_packet(aCodecCtx, &pkt);
if (ret != 0){prinitf("%s/n","error");}
while( avcodec_receive_frame(aCodecCtx, &frame) == 0){
//读取到一帧音频或者视频
//处理解码后音视频 frame
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

顺其自然~

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

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

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

打赏作者

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

抵扣说明:

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

余额充值