首页 技术 正文
技术 2022年11月11日
0 收藏 852 点赞 4,251 浏览 1151 个字

ffmpeg实现音视频编解码是非常常用的工具,视频解码出来的raw数据是yuv格式,用来进行后续的图像处理一般是RGB格式的。所以需要从yuv到rgb或者bgr的转换,ffmpeg提供了相应的转换API函数:

下面代码中dec_ctx是解码器上下文,AV_PIX_FMT_BGR24是要转换成的图像数据格式,通过avpicture_get_size()函数获取图像的数据占用空间大小,并使用av_malloc()分配一个outBuff。将outbuff挂到video_frameBGR结构体上,并设置好格式转换上下文sws_getContext()。当然也要用OpenCV声明一个Mat 来保存最后的BGR图像。

    struct SwsContext *pSwsCtx;
AVFrame *video_frameBGR=NULL;
video_frameBGR = av_frame_alloc();
uint8_t *outBuff = NULL;
int frameSize;
{
frameSize = avpicture_get_size(AV_PIX_FMT_BGR24, dec_ctx->width, dec_ctx->height);
outBuff = (uint8_t*)av_malloc(frameSize);
avpicture_fill((AVPicture *)video_frameBGR, outBuff, AV_PIX_FMT_BGR24, dec_ctx->width, dec_ctx->height); //ÉèÖÃͼÏñת»»ÉÏÏÂÎÄ
pSwsCtx = sws_getContext(dec_ctx->width, dec_ctx->height, dec_ctx->pix_fmt,
dec_ctx->width, dec_ctx->height, AV_PIX_FMT_BGR24,
SWS_BICUBIC, NULL, NULL, NULL);
}
cv::Mat img = cv::Mat::zeros(dec_ctx->height, dec_ctx->width, CV_8UC3);

使用ffmpeg的sws_scale()接口函数实现YUV格式的video_frame到BGR格式的video_frameBGR的转换,数据保存在缓冲outBuff中,从outBuff中拷贝到Mat中就得到一副BGR图像供OpenCV使用。

sws_scale(pSwsCtx, video_frame->data,
video_frame->linesize, , dec_ctx->height,
video_frameBGR->data, video_frameBGR->linesize); memcpy(img.data, outBuff, frameSize);

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902