1、分配空间 进行图像转换
old:使用avpicture_get_size()函数
// 获取解码器上下文
AVCodecContext* pInCodecCtx = avcodec_alloc_context3(vCodec);
int width = pInCodecCtx->width;
int height = pInCodecCtx->height;
// 分配空间 进行图像转换
int nSize = avpicture_get_size(AV_PIX_FMT_YUV420P, width, height);
new:avpicture_get_size()函数已被弃用,现在改为使用av_image_get_size(),返回对应图像格式和大小的图像所占的字节数,最后一个参数是内存对齐的对齐数,也就是按多大的字节进行内存对齐。比如设置为1,表示按1字节对齐,那么得到的结果就是与实际的内存大小一样。再比如设置为4,表示按4字节对齐。也就是内存的起始地址必须是4的整倍数。
// 获取解码器上下文
AVCodecContext* pInCodecCtx = avcodec_alloc_context3(vCodec);
int width = pInCodecCtx->width;
int height = pInCodecCtx->height;
// 分配空间 进行图像转换
int nSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, width, height, 1);
2、将分配的内存空间给frameYUV使用
old:使用avpicture_fill()函数
avpicture_fill((AVPicture *)frameYUV, buff, AV_PIX_FMT_YUV420P, pInCodecCtx->width, pInCodecCtx->height);
new:现已改用av_image_fill_arrays()函数
av_image_fill_arrays(frameYUV->data, frameYUV->linesize, buff, AV_PIX_FMT_YUV420P, pInCodecCtx->width, pInCodecCtx->height, 1);
3、设置图像转换上下文(为解决)
在VS中设置图像转换上下文时,程序执行到这就直接退出,返回值为3(0x3)。
但在QT中就没有问题,不知道是为什么。。。
// 转换上下文
SwsContext* swsCtx = sws_getContext(pInCodecCtx->width, pInCodecCtx->height, pInCodecCtx->pix_fmt, pInCodecCtx->width, pInCodecCtx->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
swscale.h中函数的说明:
/**
* Allocate and return an SwsContext. You need it to perform
* scaling/conversion operations using sws_scale().
*
* @param srcW the width of the source image
* @param srcH the height of the source image
* @param srcFormat the source image format
* @param dstW the width of the destination image
* @param dstH the height of the destination image
* @param dstFormat the destination image format
* @param flags specify which algorithm and options to use for rescaling
* @param param extra parameters to tune the used scaler
* For SWS_BICUBIC param[0] and [1] tune the shape of the basis
* function, param[0] tunes f(1) and param[1] f´(1)
* For SWS_GAUSS param[0] tunes the exponent and thus cutoff
* frequency
* For SWS_LANCZOS param[0] tunes the width of the window function
* @return a pointer to an allocated context, or NULL in case of error
* @note this function is to be removed after a saner alternative is
* written
*/
struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
int dstW, int dstH, enum AVPixelFormat dstFormat,
int flags, SwsFilter *srcFilter,
SwsFilter *dstFilter, const double *param);