ffmpeg-struct SwsContext使用心得

【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) 

ffmpeg-struct SwsContext使用心得

1. struct SwsContext的简介

定义路径: ffmpeg/libswscale/swscale_internal.h

  struct SwsContext结构体位于libswscale类库中, 该类库主要用于处理图片像素数据, 可以完成图片像素格式的转换, 图片的拉伸等工作.

2. struct SwsContext的使用

2.1 struct SwsContext初始化: sws_getContext()

/**
 * 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);

函数参数说明:

@param srcW 		源图像的宽度
@param srcH 		源图像的高度
@param srcFormat 源图像的图像格式.:
		AV_PIX_FMT_YUYV422,   ///< packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr
		AV_PIX_FMT_RGB24,     ///< packed RGB 8:8:8, 24bpp, RGBRGB...
@param dstW 		目标图像的宽度
@param dstH 		目标图像的高度
@param dstFormat	目标图像的图像格式(如上的srcFormat)
@param flags		标志值, 用于指定rescaling操作的算法和选项
@param param		格外的参数. 

函数作用:
  分配并返回一个SwsContext结构体, 这个结构体将用于担任scaling/conversion操作的关键变量, 通过sws_scale().

举个例子:

struct SwsContext *img_convert_ctx = NULL;
img_convert_ctx = sws_getContext(pCodecCtx->width, pCodecCtx->height,
                pCodecCtx->pix_fmt, pCodecCtx->width, pCodecCtx->height,
                AV_PIX_FMT_RGB24, SWS_BICUBIC, NULL, NULL, NULL);

2.2 struct SwsContext的使用: sws_scale()

/**
 * Scale the image slice in srcSlice and put the resulting scaled
 * slice in the image in dst. A slice is a sequence of consecutive
 * rows in an image.
 *
 * Slices have to be provided in sequential order, either in
 * top-bottom or bottom-top order. If slices are provided in
 * non-sequential order the behavior of the function is undefined.
 *
 * @param c         the scaling context previously created with
 *                  sws_getContext()
 * @param srcSlice  the array containing the pointers to the planes of
 *                  the source slice
 * @param srcStride the array containing the strides for each plane of
 *                  the source image
 * @param srcSliceY the position in the source image of the slice to
 *                  process, that is the number (counted starting from
 *                  zero) in the image of the first row of the slice
 * @param srcSliceH the height of the source slice, that is the number
 *                  of rows in the slice
 * @param dst       the array containing the pointers to the planes of
 *                  the destination image
 * @param dstStride the array containing the strides for each plane of
 *                  the destination image
 * @return          the height of the output slice
 */
int sws_scale(struct SwsContext *c, const uint8_t *const srcSlice[],
              const int srcStride[], int srcSliceY, int srcSliceH,
              uint8_t *const dst[], const int dstStride[]);

函数参数说明:

@param c			scaling结构体, 也就是2.1节时通过sws_getContext()函数创建的struct SwsContext结构体.
@param srcSlice		源图像的slice
@param srcStride	源图像的宽
@param srcSliceY	要处理的slice在源图像中的位置, 即切片第一行图像中的编号(从零开始计数)
@param srcSliceH	源图像的高
@param dst      	目标图像地址(该内存由用户自己创建, 供此函数使用, 为输出型参数) 
@param dstStride	目标图像的宽

@return          the height of the output slice
	该函数返回输出slice的高度.

函数作用:
  对源图像中的slice进行处理, 将得到的结果放到dst中. 处理的要求在struct SwsContext被创建时就被指定了(也就是sws_getContext()函数).

多说一句:
A slice is a sequence of consecutive * rows in an image.
  视频流中的一个packet不一定就是一帧图像, 它有可能是一帧图像中的一部分(因为一帧图像太大了, 超过了一个packet的容纳大小, 将会被切分为几份, 逐个发送出去), 所以这里才会用到slice的概念, slice代表着一副图像的一部分图像数据, 当然也有可能是全部.
  如果一帧图像被分为了几个slice, 那么这些slice必须按顺序得提供给sws_scale()函数, 可以是从上而下的顺序, 也可以是从下而上的顺序提供. 如果是以非连续的顺序提供切片(slice), 那么函数的行为是未定义的.

举个例子:

sws_scale(img_convert_ctx,
                    (uint8_t const * const *) pFrame->data,
                    pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data,
                    pFrameRGB->linesize);

2.3 struct SwsContext的释放: sws_freeContext()

/**
 * Free the swscaler context swsContext.
 * If swsContext is NULL, then does nothing.
 */
void sws_freeContext(struct SwsContext *swsContext);

函数作用:
  释放struct swsContext结构体, 若swsContext为空则不进行操作.

举个例子:

sws_freeContext(img_convert_ctx);

3. struct SwsContext的demo


托管平台: github
项目名: Hiboat
commit: 4e9aefc25bf5118f075c9cb7bbe6f3c93ade1528
message: 实现ppm图片的截图功能
环境: qt5 + ffmpeg4.2.2

发布了68 篇原创文章 · 获赞 22 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/MACMACip/article/details/105450185