一 首先熟悉一下ffmpeg的两个结构体
AVPacket和AVFrame
AVPacket 主要域:*data size
AVFrame主要域:*data[POINTER_NUM]; linesize[POINTER_NUM];
压缩过的数据存放在AVPacket结构体中,解压后的数据YUV存放在AVFrame结构体中
二 ffmpeg中缩放图像的函数是sws_scale函数
示例代码如下:
int resample_picture(const int &width,
const int &height,
const int &width,
const int &height,
AVFrame *src_frame,
AVFrame **dst_frame)
{
AVFrame *tmp = av_frame_alloc();
if(tmp == NULL)
{
return -2;
}
tmp->width = width;
tmp->height = height;
tmp->format = AV_PIX_FMT_YUV420P;
int pic_size = avpicture_get_size((AVPixelFormat)tmp->format,width,height);
uint8_t *m_yuvBuffer = new uint8_t[pic_size];
avpicture_fill((AVPicture*) (tmp),
m_yuvBuffer,
(AVPixelFormat)tmp->format,
tmp->width,
tmp->height);
SwsContext *scaleCtx = sws_getContext(
width,height, f_SrcCodecCtx_->pix_fmt,
width, height, (AVPixelFormat)tmp->format,
SWS_BICUBIC, NULL, NULL, NULL);
if (scaleCtx == NULL)
{
return -3;
}
if(sws_scale(scaleCtx,
src_frame->data, src_frame->linesize,
0,
height,
tmp->data, tmp->linesize) <= 0)
{
return -4;
}
sws_freeContext(scaleCtx);
*dst_frame = tmp;
return 0;
}