http://blog.csdn.net/leixiaohua1020/article/details/44305697

版权声明:本文为博主原创文章,未经博主允许不得转载。

目录(?)[+]

=====================================================

FFmpeg的库函数源代码分析文章列表:

【架构图】

FFmpeg源代码结构图 - 解码

FFmpeg源代码结构图 - 编码

【通用】

FFmpeg 源代码简单分析:av_register_all()

FFmpeg 源代码简单分析:avcodec_register_all()

FFmpeg 源代码简单分析:内存的分配和释放(av_malloc()、av_free()等)

FFmpeg 源代码简单分析:常见结构体的初始化和销毁(AVFormatContext,AVFrame等)

FFmpeg 源代码简单分析:avio_open2()

FFmpeg 源代码简单分析:av_find_decoder()和av_find_encoder()

FFmpeg 源代码简单分析:avcodec_open2()

FFmpeg 源代码简单分析:avcodec_close()

【解码】

图解FFMPEG打开媒体的函数avformat_open_input

FFmpeg 源代码简单分析:avformat_open_input()

FFmpeg 源代码简单分析:avformat_find_stream_info()

FFmpeg 源代码简单分析:av_read_frame()

FFmpeg 源代码简单分析:avcodec_decode_video2()

FFmpeg 源代码简单分析:avformat_close_input()

【编码】

FFmpeg 源代码简单分析:avformat_alloc_output_context2()

FFmpeg 源代码简单分析:avformat_write_header()

FFmpeg 源代码简单分析:avcodec_encode_video()

FFmpeg 源代码简单分析:av_write_frame()

FFmpeg 源代码简单分析:av_write_trailer()

【其它】

FFmpeg源代码简单分析:日志输出系统(av_log()等)

FFmpeg源代码简单分析:结构体成员管理系统-AVClass

FFmpeg源代码简单分析:结构体成员管理系统-AVOption

FFmpeg源代码简单分析:libswscale的sws_getContext()

FFmpeg源代码简单分析:libswscale的sws_scale()

FFmpeg源代码简单分析:libavdevice的avdevice_register_all()

FFmpeg源代码简单分析:libavdevice的gdigrab

【脚本】

FFmpeg源代码简单分析:makefile

FFmpeg源代码简单分析:configure

【H.264】

FFmpeg的H.264解码器源代码简单分析:概述

=====================================================

打算写两篇文章记录FFmpeg中的图像处理(缩放,YUV/RGB格式转换)类库libswsscale的源代码。libswscale是一个主要用于处理图片像素数据的类库。可以完成图片像素格式的转换,图片的拉伸等工作。有关libswscale的使用可以参考文章:

《最简单的基于FFmpeg的libswscale的示例(YUV转RGB)》

libswscale常用的函数数量很少,一般情况下就3个:

sws_getContext():初始化一个SwsContext。

sws_scale():处理图像数据。

sws_freeContext():释放一个SwsContext。

其中sws_getContext()也可以用sws_getCachedContext()取代。

尽管libswscale从表面上看常用函数的个数不多,它的内部却有一个大大的“世界”。做为一个几乎“万能”的图片像素数据处理类库,它的内部包含了大量的代码。因此计划写两篇文章分析它的源代码。本文首先分析它的初始化函数sws_getContext(),而下一篇文章则分析它的数据处理函数sws_scale()。

函数调用结构图

分析得到的libswscale的函数调用关系如下图所示。

Libswscale处理数据流程

Libswscale处理像素数据的流程可以概括为下图。

从图中可以看出,libswscale处理数据有两条最主要的方式:unscaled和scaled。unscaled用于处理不需要拉伸的像素数据(属于比较特殊的情况),scaled用于处理需要拉伸的像素数据。Unscaled只需要对图像像素格式进行转换;而Scaled则除了对像素格式进行转换之外,还需要对图像进行缩放。Scaled方式可以分成以下几个步骤:

  • XXX to YUV Converter:首相将数据像素数据转换为8bitYUV格式;
  • Horizontal scaler:水平拉伸图像,并且转换为15bitYUV;
  • Vertical scaler:垂直拉伸图像;
  • Output converter:转换为输出像素格式。

SwsContext

SwsContext是使用libswscale时候一个贯穿始终的结构体。但是我们在使用FFmpeg的类库进行开发的时候,是无法看到它的内部结构的。在libswscale\swscale.h中只能看到一行定义:

[cpp] view plaincopy
  1. struct SwsContext;

一般人看到这个只有一行定义的结构体,会猜测它的内部一定十分简单。但是假使我们看一下FFmpeg的源代码,会发现这个猜测是完全错误的——SwsContext的定义是十分复杂的。它的定义位于libswscale\swscale_internal.h中,如下所示。

[cpp] view plaincopy
  1. /* This struct should be aligned on at least a 32-byte boundary. */
  2. typedef struct SwsContext {
  3. /**
  4. * info on struct for av_log
  5. */
  6. const AVClass *av_class;
  7. /**
  8. * Note that src, dst, srcStride, dstStride will be copied in the
  9. * sws_scale() wrapper so they can be freely modified here.
  10. */
  11. SwsFunc swscale;
  12. int srcW;                     ///< Width  of source      luma/alpha planes.
  13. int srcH;                     ///< Height of source      luma/alpha planes.
  14. int dstH;                     ///< Height of destination luma/alpha planes.
  15. int chrSrcW;                  ///< Width  of source      chroma     planes.
  16. int chrSrcH;                  ///< Height of source      chroma     planes.
  17. int chrDstW;                  ///< Width  of destination chroma     planes.
  18. int chrDstH;                  ///< Height of destination chroma     planes.
  19. int lumXInc, chrXInc;
  20. int lumYInc, chrYInc;
  21. enum AVPixelFormat dstFormat; ///< Destination pixel format.
  22. enum AVPixelFormat srcFormat; ///< Source      pixel format.
  23. int dstFormatBpp;             ///< Number of bits per pixel of the destination pixel format.
  24. int srcFormatBpp;             ///< Number of bits per pixel of the source      pixel format.
  25. int dstBpc, srcBpc;
  26. int chrSrcHSubSample;         ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in source      image.
  27. int chrSrcVSubSample;         ///< Binary logarithm of vertical   subsampling factor between luma/alpha and chroma planes in source      image.
  28. int chrDstHSubSample;         ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in destination image.
  29. int chrDstVSubSample;         ///< Binary logarithm of vertical   subsampling factor between luma/alpha and chroma planes in destination image.
  30. int vChrDrop;                 ///< Binary logarithm of extra vertical subsampling factor in source image chroma planes specified by user.
  31. int sliceDir;                 ///< Direction that slices are fed to the scaler (1 = top-to-bottom, -1 = bottom-to-top).
  32. double param[2];              ///< Input parameters for scaling algorithms that need them.
  33. /* The cascaded_* fields allow spliting a scaler task into multiple
  34. * sequential steps, this is for example used to limit the maximum
  35. * downscaling factor that needs to be supported in one scaler.
  36. */
  37. struct SwsContext *cascaded_context[2];
  38. int cascaded_tmpStride[4];
  39. uint8_t *cascaded_tmp[4];
  40. uint32_t pal_yuv[256];
  41. uint32_t pal_rgb[256];
  42. /**
  43. * @name Scaled horizontal lines ring buffer.
  44. * The horizontal scaler keeps just enough scaled lines in a ring buffer
  45. * so they may be passed to the vertical scaler. The pointers to the
  46. * allocated buffers for each line are duplicated in sequence in the ring
  47. * buffer to simplify indexing and avoid wrapping around between lines
  48. * inside the vertical scaler code. The wrapping is done before the
  49. * vertical scaler is called.
  50. */
  51. //@{
  52. int16_t **lumPixBuf;          ///< Ring buffer for scaled horizontal luma   plane lines to be fed to the vertical scaler.
  53. int16_t **chrUPixBuf;         ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
  54. int16_t **chrVPixBuf;         ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
  55. int16_t **alpPixBuf;          ///< Ring buffer for scaled horizontal alpha  plane lines to be fed to the vertical scaler.
  56. int vLumBufSize;              ///< Number of vertical luma/alpha lines allocated in the ring buffer.
  57. int vChrBufSize;              ///< Number of vertical chroma     lines allocated in the ring buffer.
  58. int lastInLumBuf;             ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
  59. int lastInChrBuf;             ///< Last scaled horizontal chroma     line from source in the ring buffer.
  60. int lumBufIndex;              ///< Index in ring buffer of the last scaled horizontal luma/alpha line from source.
  61. int chrBufIndex;              ///< Index in ring buffer of the last scaled horizontal chroma     line from source.
  62. //@}
  63. uint8_t *formatConvBuffer;
  64. /**
  65. * @name Horizontal and vertical filters.
  66. * To better understand the following fields, here is a pseudo-code of
  67. * their usage in filtering a horizontal line:
  68. * @code
  69. * for (i = 0; i < width; i++) {
  70. *     dst[i] = 0;
  71. *     for (j = 0; j < filterSize; j++)
  72. *         dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
  73. *     dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
  74. * }
  75. * @endcode
  76. */
  77. //@{
  78. int16_t *hLumFilter;          ///< Array of horizontal filter coefficients for luma/alpha planes.
  79. int16_t *hChrFilter;          ///< Array of horizontal filter coefficients for chroma     planes.
  80. int16_t *vLumFilter;          ///< Array of vertical   filter coefficients for luma/alpha planes.
  81. int16_t *vChrFilter;          ///< Array of vertical   filter coefficients for chroma     planes.
  82. int32_t *hLumFilterPos;       ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
  83. int32_t *hChrFilterPos;       ///< Array of horizontal filter starting positions for each dst[i] for chroma     planes.
  84. int32_t *vLumFilterPos;       ///< Array of vertical   filter starting positions for each dst[i] for luma/alpha planes.
  85. int32_t *vChrFilterPos;       ///< Array of vertical   filter starting positions for each dst[i] for chroma     planes.
  86. int hLumFilterSize;           ///< Horizontal filter size for luma/alpha pixels.
  87. int hChrFilterSize;           ///< Horizontal filter size for chroma     pixels.
  88. int vLumFilterSize;           ///< Vertical   filter size for luma/alpha pixels.
  89. int vChrFilterSize;           ///< Vertical   filter size for chroma     pixels.
  90. //@}
  91. int lumMmxextFilterCodeSize;  ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for luma/alpha planes.
  92. int chrMmxextFilterCodeSize;  ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for chroma planes.
  93. uint8_t *lumMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for luma/alpha planes.
  94. uint8_t *chrMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for chroma planes.
  95. int canMMXEXTBeUsed;
  96. int dstY;                     ///< Last destination vertical line output from last slice.
  97. int flags;                    ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
  98. void *yuvTable;             // pointer to the yuv->rgb table start so it can be freed()
  99. // alignment ensures the offset can be added in a single
  100. // instruction on e.g. ARM
  101. DECLARE_ALIGNED(16, int, table_gV)[256 + 2*YUVRGB_TABLE_HEADROOM];
  102. uint8_t *table_rV[256 + 2*YUVRGB_TABLE_HEADROOM];
  103. uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
  104. uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
  105. DECLARE_ALIGNED(16, int32_t, input_rgb2yuv_table)[16+40*4]; // This table can contain both C and SIMD formatted values, the C vales are always at the XY_IDX points
  106. #define RY_IDX 0
  107. #define GY_IDX 1
  108. #define BY_IDX 2
  109. #define RU_IDX 3
  110. #define GU_IDX 4
  111. #define BU_IDX 5
  112. #define RV_IDX 6
  113. #define GV_IDX 7
  114. #define BV_IDX 8
  115. #define RGB2YUV_SHIFT 15
  116. int *dither_error[4];
  117. //Colorspace stuff
  118. int contrast, brightness, saturation;    // for sws_getColorspaceDetails
  119. int srcColorspaceTable[4];
  120. int dstColorspaceTable[4];
  121. int srcRange;                 ///< 0 = MPG YUV range, 1 = JPG YUV range (source      image).
  122. int dstRange;                 ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
  123. int src0Alpha;
  124. int dst0Alpha;
  125. int srcXYZ;
  126. int dstXYZ;
  127. int src_h_chr_pos;
  128. int dst_h_chr_pos;
  129. int src_v_chr_pos;
  130. int dst_v_chr_pos;
  131. int yuv2rgb_y_offset;
  132. int yuv2rgb_y_coeff;
  133. int yuv2rgb_v2r_coeff;
  134. int yuv2rgb_v2g_coeff;
  135. int yuv2rgb_u2g_coeff;
  136. int yuv2rgb_u2b_coeff;
  137. #define RED_DITHER            "0*8"
  138. #define GREEN_DITHER          "1*8"
  139. #define BLUE_DITHER           "2*8"
  140. #define Y_COEFF               "3*8"
  141. #define VR_COEFF              "4*8"
  142. #define UB_COEFF              "5*8"
  143. #define VG_COEFF              "6*8"
  144. #define UG_COEFF              "7*8"
  145. #define Y_OFFSET              "8*8"
  146. #define U_OFFSET              "9*8"
  147. #define V_OFFSET              "10*8"
  148. #define LUM_MMX_FILTER_OFFSET "11*8"
  149. #define CHR_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)
  150. #define DSTW_OFFSET           "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2"
  151. #define ESP_OFFSET            "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+8"
  152. #define VROUNDER_OFFSET       "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+16"
  153. #define U_TEMP                "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+24"
  154. #define V_TEMP                "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+32"
  155. #define Y_TEMP                "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+40"
  156. #define ALP_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+48"
  157. #define UV_OFF_PX             "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+48"
  158. #define UV_OFF_BYTE           "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+56"
  159. #define DITHER16              "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+64"
  160. #define DITHER32              "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+80"
  161. #define DITHER32_INT          (11*8+4*4*MAX_FILTER_SIZE*3+80) // value equal to above, used for checking that the struct hasn't been changed by mistake
  162. DECLARE_ALIGNED(8, uint64_t, redDither);
  163. DECLARE_ALIGNED(8, uint64_t, greenDither);
  164. DECLARE_ALIGNED(8, uint64_t, blueDither);
  165. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  166. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  167. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  168. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  169. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  170. DECLARE_ALIGNED(8, uint64_t, yOffset);
  171. DECLARE_ALIGNED(8, uint64_t, uOffset);
  172. DECLARE_ALIGNED(8, uint64_t, vOffset);
  173. int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
  174. int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
  175. int dstW;                     ///< Width  of destination luma/alpha planes.
  176. DECLARE_ALIGNED(8, uint64_t, esp);
  177. DECLARE_ALIGNED(8, uint64_t, vRounder);
  178. DECLARE_ALIGNED(8, uint64_t, u_temp);
  179. DECLARE_ALIGNED(8, uint64_t, v_temp);
  180. DECLARE_ALIGNED(8, uint64_t, y_temp);
  181. int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
  182. // alignment of these values is not necessary, but merely here
  183. // to maintain the same offset across x8632 and x86-64. Once we
  184. // use proper offset macros in the asm, they can be removed.
  185. DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
  186. DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
  187. DECLARE_ALIGNED(8, uint16_t, dither16)[8];
  188. DECLARE_ALIGNED(8, uint32_t, dither32)[8];
  189. const uint8_t *chrDither8, *lumDither8;
  190. #if HAVE_ALTIVEC
  191. vector signed short   CY;
  192. vector signed short   CRV;
  193. vector signed short   CBU;
  194. vector signed short   CGU;
  195. vector signed short   CGV;
  196. vector signed short   OY;
  197. vector unsigned short CSHIFT;
  198. vector signed short  *vYCoeffsBank, *vCCoeffsBank;
  199. #endif
  200. int use_mmx_vfilter;
  201. /* pre defined color-spaces gamma */
  202. #define XYZ_GAMMA (2.6f)
  203. #define RGB_GAMMA (2.2f)
  204. int16_t *xyzgamma;
  205. int16_t *rgbgamma;
  206. int16_t *xyzgammainv;
  207. int16_t *rgbgammainv;
  208. int16_t xyz2rgb_matrix[3][4];
  209. int16_t rgb2xyz_matrix[3][4];
  210. /* function pointers for swscale() */
  211. yuv2planar1_fn yuv2plane1;
  212. yuv2planarX_fn yuv2planeX;
  213. yuv2interleavedX_fn yuv2nv12cX;
  214. yuv2packed1_fn yuv2packed1;
  215. yuv2packed2_fn yuv2packed2;
  216. yuv2packedX_fn yuv2packedX;
  217. yuv2anyX_fn yuv2anyX;
  218. /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
  219. void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  220. int width, uint32_t *pal);
  221. /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  222. void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  223. int width, uint32_t *pal);
  224. /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  225. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  226. const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  227. int width, uint32_t *pal);
  228. /**
  229. * Functions to read planar input, such as planar RGB, and convert
  230. * internally to Y/UV/A.
  231. */
  232. /** @{ */
  233. void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  234. void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
  235. int width, int32_t *rgb2yuv);
  236. void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  237. /** @} */
  238. /**
  239. * Scale one horizontal line of input data using a bilinear filter
  240. * to produce one line of output data. Compared to SwsContext->hScale(),
  241. * please take note of the following caveats when using these:
  242. * - Scaling is done using only 7bit instead of 14bit coefficients.
  243. * - You can use no more than 5 input pixels to produce 4 output
  244. *   pixels. Therefore, this filter should not be used for downscaling
  245. *   by more than ~20% in width (because that equals more than 5/4th
  246. *   downscaling and thus more than 5 pixels input per 4 pixels output).
  247. * - In general, bilinear filters create artifacts during downscaling
  248. *   (even when <20%), because one output pixel will span more than one
  249. *   input pixel, and thus some pixels will need edges of both neighbor
  250. *   pixels to interpolate the output pixel. Since you can use at most
  251. *   two input pixels per output pixel in bilinear scaling, this is
  252. *   impossible and thus downscaling by any size will create artifacts.
  253. * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
  254. * in SwsContext->flags.
  255. */
  256. /** @{ */
  257. void (*hyscale_fast)(struct SwsContext *c,
  258. int16_t *dst, int dstWidth,
  259. const uint8_t *src, int srcW, int xInc);
  260. void (*hcscale_fast)(struct SwsContext *c,
  261. int16_t *dst1, int16_t *dst2, int dstWidth,
  262. const uint8_t *src1, const uint8_t *src2,
  263. int srcW, int xInc);
  264. /** @} */
  265. /**
  266. * Scale one horizontal line of input data using a filter over the input
  267. * lines, to produce one (differently sized) line of output data.
  268. *
  269. * @param dst        pointer to destination buffer for horizontally scaled
  270. *                   data. If the number of bits per component of one
  271. *                   destination pixel (SwsContext->dstBpc) is <= 10, data
  272. *                   will be 15bpc in 16bits (int16_t) width. Else (i.e.
  273. *                   SwsContext->dstBpc == 16), data will be 19bpc in
  274. *                   32bits (int32_t) width.
  275. * @param dstW       width of destination image
  276. * @param src        pointer to source data to be scaled. If the number of
  277. *                   bits per component of a source pixel (SwsContext->srcBpc)
  278. *                   is 8, this is 8bpc in 8bits (uint8_t) width. Else
  279. *                   (i.e. SwsContext->dstBpc > 8), this is native depth
  280. *                   in 16bits (uint16_t) width. In other words, for 9-bit
  281. *                   YUV input, this is 9bpc, for 10-bit YUV input, this is
  282. *                   10bpc, and for 16-bit RGB or YUV, this is 16bpc.
  283. * @param filter     filter coefficients to be used per output pixel for
  284. *                   scaling. This contains 14bpp filtering coefficients.
  285. *                   Guaranteed to contain dstW * filterSize entries.
  286. * @param filterPos  position of the first input pixel to be used for
  287. *                   each output pixel during scaling. Guaranteed to
  288. *                   contain dstW entries.
  289. * @param filterSize the number of input coefficients to be used (and
  290. *                   thus the number of input pixels to be used) for
  291. *                   creating a single output pixel. Is aligned to 4
  292. *                   (and input coefficients thus padded with zeroes)
  293. *                   to simplify creating SIMD code.
  294. */
  295. /** @{ */
  296. void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
  297. const uint8_t *src, const int16_t *filter,
  298. const int32_t *filterPos, int filterSize);
  299. void (*hcScale)(struct SwsContext *c, int16_t *dst, int dstW,
  300. const uint8_t *src, const int16_t *filter,
  301. const int32_t *filterPos, int filterSize);
  302. /** @} */
  303. /// Color range conversion function for luma plane if needed.
  304. void (*lumConvertRange)(int16_t *dst, int width);
  305. /// Color range conversion function for chroma planes if needed.
  306. void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
  307. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  308. SwsDither dither;
  309. } SwsContext;

这个结构体的定义确实比较复杂,里面包含了libswscale所需要的全部变量。一一分析这些变量是不太现实的,在后文中会简单分析其中的几个变量。

sws_getContext()

sws_getContext()是初始化SwsContext的函数。sws_getContext()的声明位于libswscale\swscale.h,如下所示。

[cpp] view plaincopy
  1. /**
  2. * Allocate and return an SwsContext. You need it to perform
  3. * scaling/conversion operations using sws_scale().
  4. *
  5. * @param srcW the width of the source image
  6. * @param srcH the height of the source image
  7. * @param srcFormat the source image format
  8. * @param dstW the width of the destination image
  9. * @param dstH the height of the destination image
  10. * @param dstFormat the destination image format
  11. * @param flags specify which algorithm and options to use for rescaling
  12. * @return a pointer to an allocated context, or NULL in case of error
  13. * @note this function is to be removed after a saner alternative is
  14. *       written
  15. */
  16. struct SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
  17. int dstW, int dstH, enum AVPixelFormat dstFormat,
  18. int flags, SwsFilter *srcFilter,
  19. SwsFilter *dstFilter, const double *param);

该函数包含以下参数:

srcW:源图像的宽
srcH:源图像的高
srcFormat:源图像的像素格式
dstW:目标图像的宽
dstH:目标图像的高
dstFormat:目标图像的像素格式
flags:设定图像拉伸使用的算法

成功执行的话返回生成的SwsContext,否则返回NULL。
sws_getContext()的定义位于libswscale\utils.c,如下所示。

[cpp] view plaincopy
  1. SwsContext *sws_getContext(int srcW, int srcH, enum AVPixelFormat srcFormat,
  2. int dstW, int dstH, enum AVPixelFormat dstFormat,
  3. int flags, SwsFilter *srcFilter,
  4. SwsFilter *dstFilter, const double *param)
  5. {
  6. SwsContext *c;
  7. if (!(c = sws_alloc_context()))
  8. return NULL;
  9. c->flags     = flags;
  10. c->srcW      = srcW;
  11. c->srcH      = srcH;
  12. c->dstW      = dstW;
  13. c->dstH      = dstH;
  14. c->srcFormat = srcFormat;
  15. c->dstFormat = dstFormat;
  16. if (param) {
  17. c->param[0] = param[0];
  18. c->param[1] = param[1];
  19. }
  20. if (sws_init_context(c, srcFilter, dstFilter) < 0) {
  21. sws_freeContext(c);
  22. return NULL;
  23. }
  24. return c;
  25. }

从sws_getContext()的定义中可以看出,它首先调用了一个函数sws_alloc_context()用于给SwsContext分配内存。然后将传入的源图像,目标图像的宽高,像素格式,以及标志位分别赋值给该SwsContext相应的字段。最后调用一个函数sws_init_context()完成初始化工作。下面我们分别看一下sws_alloc_context()和sws_init_context()这两个函数。

sws_alloc_context()

sws_alloc_context()是FFmpeg的一个API,用于给SwsContext分配内存,它的声明如下所示。

[cpp] view plaincopy
  1. /**
  2. * Allocate an empty SwsContext. This must be filled and passed to
  3. * sws_init_context(). For filling see AVOptions, options.c and
  4. * sws_setColorspaceDetails().
  5. */
  6. struct SwsContext *sws_alloc_context(void);

sws_alloc_context()的定义位于libswscale\utils.c,如下所示。

[cpp] view plaincopy
  1. SwsContext *sws_alloc_context(void)
  2. {
  3. SwsContext *c = av_mallocz(sizeof(SwsContext));
  4. av_assert0(offsetof(SwsContext, redDither) + DITHER32_INT == offsetof(SwsContext, dither32));
  5. if (c) {
  6. c->av_class = &sws_context_class;
  7. av_opt_set_defaults(c);
  8. }
  9. return c;
  10. }

从代码中可以看出,sws_alloc_context()首先调用av_mallocz()为SwsContext结构体分配了一块内存;然后设置了该结构体的AVClass,并且给该结构体的字段设置了默认值。

sws_init_context()

sws_init_context()的是FFmpeg的一个API,用于初始化SwsContext。

[cpp] view plaincopy
  1. /**
  2. * Initialize the swscaler context sws_context.
  3. *
  4. * @return zero or positive value on success, a negative value on
  5. * error
  6. */
  7. int sws_init_context(struct SwsContext *sws_context, SwsFilter *srcFilter, SwsFilter *dstFilter);

sws_init_context()的函数定义非常的长,位于libswscale\utils.c,如下所示。

[cpp] view plaincopy
  1. av_cold int sws_init_context(SwsContext *c, SwsFilter *srcFilter,
  2. SwsFilter *dstFilter)
  3. {
  4. int i, j;
  5. int usesVFilter, usesHFilter;
  6. int unscaled;
  7. SwsFilter dummyFilter = { NULL, NULL, NULL, NULL };
  8. int srcW              = c->srcW;
  9. int srcH              = c->srcH;
  10. int dstW              = c->dstW;
  11. int dstH              = c->dstH;
  12. int dst_stride        = FFALIGN(dstW * sizeof(int16_t) + 66, 16);
  13. int flags, cpu_flags;
  14. enum AVPixelFormat srcFormat = c->srcFormat;
  15. enum AVPixelFormat dstFormat = c->dstFormat;
  16. const AVPixFmtDescriptor *desc_src;
  17. const AVPixFmtDescriptor *desc_dst;
  18. int ret = 0;
  19. //获取
  20. cpu_flags = av_get_cpu_flags();
  21. flags     = c->flags;
  22. emms_c();
  23. if (!rgb15to16)
  24. sws_rgb2rgb_init();
  25. //如果输入的宽高和输出的宽高一样,则做特殊处理
  26. unscaled = (srcW == dstW && srcH == dstH);
  27. //如果是JPEG标准(Y取值0-255),则需要设置这两项
  28. c->srcRange |= handle_jpeg(&c->srcFormat);
  29. c->dstRange |= handle_jpeg(&c->dstFormat);
  30. if(srcFormat!=c->srcFormat || dstFormat!=c->dstFormat)
  31. av_log(c, AV_LOG_WARNING, "deprecated pixel format used, make sure you did set range correctly\n");
  32. //设置Colorspace
  33. if (!c->contrast && !c->saturation && !c->dstFormatBpp)
  34. sws_setColorspaceDetails(c, ff_yuv2rgb_coeffs[SWS_CS_DEFAULT], c->srcRange,
  35. ff_yuv2rgb_coeffs[SWS_CS_DEFAULT],
  36. c->dstRange, 0, 1 << 16, 1 << 16);
  37. handle_formats(c);
  38. srcFormat = c->srcFormat;
  39. dstFormat = c->dstFormat;
  40. desc_src = av_pix_fmt_desc_get(srcFormat);
  41. desc_dst = av_pix_fmt_desc_get(dstFormat);
  42. //转换大小端?
  43. if (!(unscaled && sws_isSupportedEndiannessConversion(srcFormat) &&
  44. av_pix_fmt_swap_endianness(srcFormat) == dstFormat)) {
  45. //检查输入格式是否支持
  46. if (!sws_isSupportedInput(srcFormat)) {
  47. av_log(c, AV_LOG_ERROR, "%s is not supported as input pixel format\n",
  48. av_get_pix_fmt_name(srcFormat));
  49. return AVERROR(EINVAL);
  50. }
  51. //检查输出格式是否支持
  52. if (!sws_isSupportedOutput(dstFormat)) {
  53. av_log(c, AV_LOG_ERROR, "%s is not supported as output pixel format\n",
  54. av_get_pix_fmt_name(dstFormat));
  55. return AVERROR(EINVAL);
  56. }
  57. }
  58. //检查拉伸的方法
  59. i = flags & (SWS_POINT         |
  60. SWS_AREA          |
  61. SWS_BILINEAR      |
  62. SWS_FAST_BILINEAR |
  63. SWS_BICUBIC       |
  64. SWS_X             |
  65. SWS_GAUSS         |
  66. SWS_LANCZOS       |
  67. SWS_SINC          |
  68. SWS_SPLINE        |
  69. SWS_BICUBLIN);
  70. /* provide a default scaler if not set by caller */
  71. //如果没有指定,就使用默认的
  72. if (!i) {
  73. if (dstW < srcW && dstH < srcH)
  74. flags |= SWS_BICUBIC;
  75. else if (dstW > srcW && dstH > srcH)
  76. flags |= SWS_BICUBIC;
  77. else
  78. flags |= SWS_BICUBIC;
  79. c->flags = flags;
  80. } else if (i & (i - 1)) {
  81. av_log(c, AV_LOG_ERROR,
  82. "Exactly one scaler algorithm must be chosen, got %X\n", i);
  83. return AVERROR(EINVAL);
  84. }
  85. /* sanity check */
  86. //检查宽高参数
  87. if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
  88. /* FIXME check if these are enough and try to lower them after
  89. * fixing the relevant parts of the code */
  90. av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
  91. srcW, srcH, dstW, dstH);
  92. return AVERROR(EINVAL);
  93. }
  94. if (!dstFilter)
  95. dstFilter = &dummyFilter;
  96. if (!srcFilter)
  97. srcFilter = &dummyFilter;
  98. c->lumXInc      = (((int64_t)srcW << 16) + (dstW >> 1)) / dstW;
  99. c->lumYInc      = (((int64_t)srcH << 16) + (dstH >> 1)) / dstH;
  100. c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
  101. c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
  102. c->vRounder     = 4 * 0x0001000100010001ULL;
  103. usesVFilter = (srcFilter->lumV && srcFilter->lumV->length > 1) ||
  104. (srcFilter->chrV && srcFilter->chrV->length > 1) ||
  105. (dstFilter->lumV && dstFilter->lumV->length > 1) ||
  106. (dstFilter->chrV && dstFilter->chrV->length > 1);
  107. usesHFilter = (srcFilter->lumH && srcFilter->lumH->length > 1) ||
  108. (srcFilter->chrH && srcFilter->chrH->length > 1) ||
  109. (dstFilter->lumH && dstFilter->lumH->length > 1) ||
  110. (dstFilter->chrH && dstFilter->chrH->length > 1);
  111. av_pix_fmt_get_chroma_sub_sample(srcFormat, &c->chrSrcHSubSample, &c->chrSrcVSubSample);
  112. av_pix_fmt_get_chroma_sub_sample(dstFormat, &c->chrDstHSubSample, &c->chrDstVSubSample);
  113. if (isAnyRGB(dstFormat) && !(flags&SWS_FULL_CHR_H_INT)) {
  114. if (dstW&1) {
  115. av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to odd output size\n");
  116. flags |= SWS_FULL_CHR_H_INT;
  117. c->flags = flags;
  118. }
  119. if (   c->chrSrcHSubSample == 0
  120. && c->chrSrcVSubSample == 0
  121. && c->dither != SWS_DITHER_BAYER //SWS_FULL_CHR_H_INT is currently not supported with SWS_DITHER_BAYER
  122. && !(c->flags & SWS_FAST_BILINEAR)
  123. ) {
  124. av_log(c, AV_LOG_DEBUG, "Forcing full internal H chroma due to input having non subsampled chroma\n");
  125. flags |= SWS_FULL_CHR_H_INT;
  126. c->flags = flags;
  127. }
  128. }
  129. if (c->dither == SWS_DITHER_AUTO) {
  130. if (flags & SWS_ERROR_DIFFUSION)
  131. c->dither = SWS_DITHER_ED;
  132. }
  133. if(dstFormat == AV_PIX_FMT_BGR4_BYTE ||
  134. dstFormat == AV_PIX_FMT_RGB4_BYTE ||
  135. dstFormat == AV_PIX_FMT_BGR8 ||
  136. dstFormat == AV_PIX_FMT_RGB8) {
  137. if (c->dither == SWS_DITHER_AUTO)
  138. c->dither = (flags & SWS_FULL_CHR_H_INT) ? SWS_DITHER_ED : SWS_DITHER_BAYER;
  139. if (!(flags & SWS_FULL_CHR_H_INT)) {
  140. if (c->dither == SWS_DITHER_ED || c->dither == SWS_DITHER_A_DITHER || c->dither == SWS_DITHER_X_DITHER) {
  141. av_log(c, AV_LOG_DEBUG,
  142. "Desired dithering only supported in full chroma interpolation for destination format '%s'\n",
  143. av_get_pix_fmt_name(dstFormat));
  144. flags   |= SWS_FULL_CHR_H_INT;
  145. c->flags = flags;
  146. }
  147. }
  148. if (flags & SWS_FULL_CHR_H_INT) {
  149. if (c->dither == SWS_DITHER_BAYER) {
  150. av_log(c, AV_LOG_DEBUG,
  151. "Ordered dither is not supported in full chroma interpolation for destination format '%s'\n",
  152. av_get_pix_fmt_name(dstFormat));
  153. c->dither = SWS_DITHER_ED;
  154. }
  155. }
  156. }
  157. if (isPlanarRGB(dstFormat)) {
  158. if (!(flags & SWS_FULL_CHR_H_INT)) {
  159. av_log(c, AV_LOG_DEBUG,
  160. "%s output is not supported with half chroma resolution, switching to full\n",
  161. av_get_pix_fmt_name(dstFormat));
  162. flags   |= SWS_FULL_CHR_H_INT;
  163. c->flags = flags;
  164. }
  165. }
  166. /* reuse chroma for 2 pixels RGB/BGR unless user wants full
  167. * chroma interpolation */
  168. if (flags & SWS_FULL_CHR_H_INT &&
  169. isAnyRGB(dstFormat)        &&
  170. !isPlanarRGB(dstFormat)    &&
  171. dstFormat != AV_PIX_FMT_RGBA  &&
  172. dstFormat != AV_PIX_FMT_ARGB  &&
  173. dstFormat != AV_PIX_FMT_BGRA  &&
  174. dstFormat != AV_PIX_FMT_ABGR  &&
  175. dstFormat != AV_PIX_FMT_RGB24 &&
  176. dstFormat != AV_PIX_FMT_BGR24 &&
  177. dstFormat != AV_PIX_FMT_BGR4_BYTE &&
  178. dstFormat != AV_PIX_FMT_RGB4_BYTE &&
  179. dstFormat != AV_PIX_FMT_BGR8 &&
  180. dstFormat != AV_PIX_FMT_RGB8
  181. ) {
  182. av_log(c, AV_LOG_WARNING,
  183. "full chroma interpolation for destination format '%s' not yet implemented\n",
  184. av_get_pix_fmt_name(dstFormat));
  185. flags   &= ~SWS_FULL_CHR_H_INT;
  186. c->flags = flags;
  187. }
  188. if (isAnyRGB(dstFormat) && !(flags & SWS_FULL_CHR_H_INT))
  189. c->chrDstHSubSample = 1;
  190. // drop some chroma lines if the user wants it
  191. c->vChrDrop          = (flags & SWS_SRC_V_CHR_DROP_MASK) >>
  192. SWS_SRC_V_CHR_DROP_SHIFT;
  193. c->chrSrcVSubSample += c->vChrDrop;
  194. /* drop every other pixel for chroma calculation unless user
  195. * wants full chroma */
  196. if (isAnyRGB(srcFormat) && !(flags & SWS_FULL_CHR_H_INP)   &&
  197. srcFormat != AV_PIX_FMT_RGB8 && srcFormat != AV_PIX_FMT_BGR8 &&
  198. srcFormat != AV_PIX_FMT_RGB4 && srcFormat != AV_PIX_FMT_BGR4 &&
  199. srcFormat != AV_PIX_FMT_RGB4_BYTE && srcFormat != AV_PIX_FMT_BGR4_BYTE &&
  200. srcFormat != AV_PIX_FMT_GBRP9BE   && srcFormat != AV_PIX_FMT_GBRP9LE  &&
  201. srcFormat != AV_PIX_FMT_GBRP10BE  && srcFormat != AV_PIX_FMT_GBRP10LE &&
  202. srcFormat != AV_PIX_FMT_GBRP12BE  && srcFormat != AV_PIX_FMT_GBRP12LE &&
  203. srcFormat != AV_PIX_FMT_GBRP14BE  && srcFormat != AV_PIX_FMT_GBRP14LE &&
  204. srcFormat != AV_PIX_FMT_GBRP16BE  && srcFormat != AV_PIX_FMT_GBRP16LE &&
  205. ((dstW >> c->chrDstHSubSample) <= (srcW >> 1) ||
  206. (flags & SWS_FAST_BILINEAR)))
  207. c->chrSrcHSubSample = 1;
  208. // Note the FF_CEIL_RSHIFT is so that we always round toward +inf.
  209. c->chrSrcW = FF_CEIL_RSHIFT(srcW, c->chrSrcHSubSample);
  210. c->chrSrcH = FF_CEIL_RSHIFT(srcH, c->chrSrcVSubSample);
  211. c->chrDstW = FF_CEIL_RSHIFT(dstW, c->chrDstHSubSample);
  212. c->chrDstH = FF_CEIL_RSHIFT(dstH, c->chrDstVSubSample);
  213. FF_ALLOC_OR_GOTO(c, c->formatConvBuffer, FFALIGN(srcW*2+78, 16) * 2, fail);
  214. c->srcBpc = 1 + desc_src->comp[0].depth_minus1;
  215. if (c->srcBpc < 8)
  216. c->srcBpc = 8;
  217. c->dstBpc = 1 + desc_dst->comp[0].depth_minus1;
  218. if (c->dstBpc < 8)
  219. c->dstBpc = 8;
  220. if (isAnyRGB(srcFormat) || srcFormat == AV_PIX_FMT_PAL8)
  221. c->srcBpc = 16;
  222. if (c->dstBpc == 16)
  223. dst_stride <<= 1;
  224. if (INLINE_MMXEXT(cpu_flags) && c->srcBpc == 8 && c->dstBpc <= 14) {
  225. c->canMMXEXTBeUsed = dstW >= srcW && (dstW & 31) == 0 &&
  226. c->chrDstW >= c->chrSrcW &&
  227. (srcW & 15) == 0;
  228. if (!c->canMMXEXTBeUsed && dstW >= srcW && c->chrDstW >= c->chrSrcW && (srcW & 15) == 0
  229. && (flags & SWS_FAST_BILINEAR)) {
  230. if (flags & SWS_PRINT_INFO)
  231. av_log(c, AV_LOG_INFO,
  232. "output width is not a multiple of 32 -> no MMXEXT scaler\n");
  233. }
  234. if (usesHFilter || isNBPS(c->srcFormat) || is16BPS(c->srcFormat) || isAnyRGB(c->srcFormat))
  235. c->canMMXEXTBeUsed = 0;
  236. } else
  237. c->canMMXEXTBeUsed = 0;
  238. c->chrXInc = (((int64_t)c->chrSrcW << 16) + (c->chrDstW >> 1)) / c->chrDstW;
  239. c->chrYInc = (((int64_t)c->chrSrcH << 16) + (c->chrDstH >> 1)) / c->chrDstH;
  240. /* Match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src
  241. * to pixel n-2 of dst, but only for the FAST_BILINEAR mode otherwise do
  242. * correct scaling.
  243. * n-2 is the last chrominance sample available.
  244. * This is not perfect, but no one should notice the difference, the more
  245. * correct variant would be like the vertical one, but that would require
  246. * some special code for the first and last pixel */
  247. if (flags & SWS_FAST_BILINEAR) {
  248. if (c->canMMXEXTBeUsed) {
  249. c->lumXInc += 20;
  250. c->chrXInc += 20;
  251. }
  252. // we don't use the x86 asm scaler if MMX is available
  253. else if (INLINE_MMX(cpu_flags) && c->dstBpc <= 14) {
  254. c->lumXInc = ((int64_t)(srcW       - 2) << 16) / (dstW       - 2) - 20;
  255. c->chrXInc = ((int64_t)(c->chrSrcW - 2) << 16) / (c->chrDstW - 2) - 20;
  256. }
  257. }
  258. if (isBayer(srcFormat)) {
  259. if (!unscaled ||
  260. (dstFormat != AV_PIX_FMT_RGB24 && dstFormat != AV_PIX_FMT_YUV420P)) {
  261. enum AVPixelFormat tmpFormat = AV_PIX_FMT_RGB24;
  262. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  263. srcW, srcH, tmpFormat, 64);
  264. if (ret < 0)
  265. return ret;
  266. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  267. srcW, srcH, tmpFormat,
  268. flags, srcFilter, NULL, c->param);
  269. if (!c->cascaded_context[0])
  270. return -1;
  271. c->cascaded_context[1] = sws_getContext(srcW, srcH, tmpFormat,
  272. dstW, dstH, dstFormat,
  273. flags, NULL, dstFilter, c->param);
  274. if (!c->cascaded_context[1])
  275. return -1;
  276. return 0;
  277. }
  278. }
  279. #define USE_MMAP (HAVE_MMAP && HAVE_MPROTECT && defined MAP_ANONYMOUS)
  280. /* precalculate horizontal scaler filter coefficients */
  281. {
  282. #if HAVE_MMXEXT_INLINE
  283. // can't downscale !!!
  284. if (c->canMMXEXTBeUsed && (flags & SWS_FAST_BILINEAR)) {
  285. c->lumMmxextFilterCodeSize = ff_init_hscaler_mmxext(dstW, c->lumXInc, NULL,
  286. NULL, NULL, 8);
  287. c->chrMmxextFilterCodeSize = ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc,
  288. NULL, NULL, NULL, 4);
  289. #if USE_MMAP
  290. c->lumMmxextFilterCode = mmap(NULL, c->lumMmxextFilterCodeSize,
  291. PROT_READ | PROT_WRITE,
  292. MAP_PRIVATE | MAP_ANONYMOUS,
  293. -1, 0);
  294. c->chrMmxextFilterCode = mmap(NULL, c->chrMmxextFilterCodeSize,
  295. PROT_READ | PROT_WRITE,
  296. MAP_PRIVATE | MAP_ANONYMOUS,
  297. -1, 0);
  298. #elif HAVE_VIRTUALALLOC
  299. c->lumMmxextFilterCode = VirtualAlloc(NULL,
  300. c->lumMmxextFilterCodeSize,
  301. MEM_COMMIT,
  302. PAGE_EXECUTE_READWRITE);
  303. c->chrMmxextFilterCode = VirtualAlloc(NULL,
  304. c->chrMmxextFilterCodeSize,
  305. MEM_COMMIT,
  306. PAGE_EXECUTE_READWRITE);
  307. #else
  308. c->lumMmxextFilterCode = av_malloc(c->lumMmxextFilterCodeSize);
  309. c->chrMmxextFilterCode = av_malloc(c->chrMmxextFilterCodeSize);
  310. #endif
  311. #ifdef MAP_ANONYMOUS
  312. if (c->lumMmxextFilterCode == MAP_FAILED || c->chrMmxextFilterCode == MAP_FAILED)
  313. #else
  314. if (!c->lumMmxextFilterCode || !c->chrMmxextFilterCode)
  315. #endif
  316. {
  317. av_log(c, AV_LOG_ERROR, "Failed to allocate MMX2FilterCode\n");
  318. return AVERROR(ENOMEM);
  319. }
  320. FF_ALLOCZ_OR_GOTO(c, c->hLumFilter,    (dstW           / 8 + 8) * sizeof(int16_t), fail);
  321. FF_ALLOCZ_OR_GOTO(c, c->hChrFilter,    (c->chrDstW     / 4 + 8) * sizeof(int16_t), fail);
  322. FF_ALLOCZ_OR_GOTO(c, c->hLumFilterPos, (dstW       / 2 / 8 + 8) * sizeof(int32_t), fail);
  323. FF_ALLOCZ_OR_GOTO(c, c->hChrFilterPos, (c->chrDstW / 2 / 4 + 8) * sizeof(int32_t), fail);
  324. ff_init_hscaler_mmxext(      dstW, c->lumXInc, c->lumMmxextFilterCode,
  325. c->hLumFilter, (uint32_t*)c->hLumFilterPos, 8);
  326. ff_init_hscaler_mmxext(c->chrDstW, c->chrXInc, c->chrMmxextFilterCode,
  327. c->hChrFilter, (uint32_t*)c->hChrFilterPos, 4);
  328. #if USE_MMAP
  329. if (   mprotect(c->lumMmxextFilterCode, c->lumMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1
  330. || mprotect(c->chrMmxextFilterCode, c->chrMmxextFilterCodeSize, PROT_EXEC | PROT_READ) == -1) {
  331. av_log(c, AV_LOG_ERROR, "mprotect failed, cannot use fast bilinear scaler\n");
  332. goto fail;
  333. }
  334. #endif
  335. } else
  336. #endif /* HAVE_MMXEXT_INLINE */
  337. {
  338. const int filterAlign = X86_MMX(cpu_flags)     ? 4 :
  339. PPC_ALTIVEC(cpu_flags) ? 8 : 1;
  340. if ((ret = initFilter(&c->hLumFilter, &c->hLumFilterPos,
  341. &c->hLumFilterSize, c->lumXInc,
  342. srcW, dstW, filterAlign, 1 << 14,
  343. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  344. cpu_flags, srcFilter->lumH, dstFilter->lumH,
  345. c->param,
  346. get_local_pos(c, 0, 0, 0),
  347. get_local_pos(c, 0, 0, 0))) < 0)
  348. goto fail;
  349. if ((ret = initFilter(&c->hChrFilter, &c->hChrFilterPos,
  350. &c->hChrFilterSize, c->chrXInc,
  351. c->chrSrcW, c->chrDstW, filterAlign, 1 << 14,
  352. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  353. cpu_flags, srcFilter->chrH, dstFilter->chrH,
  354. c->param,
  355. get_local_pos(c, c->chrSrcHSubSample, c->src_h_chr_pos, 0),
  356. get_local_pos(c, c->chrDstHSubSample, c->dst_h_chr_pos, 0))) < 0)
  357. goto fail;
  358. }
  359. } // initialize horizontal stuff
  360. /* precalculate vertical scaler filter coefficients */
  361. {
  362. const int filterAlign = X86_MMX(cpu_flags)     ? 2 :
  363. PPC_ALTIVEC(cpu_flags) ? 8 : 1;
  364. if ((ret = initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize,
  365. c->lumYInc, srcH, dstH, filterAlign, (1 << 12),
  366. (flags & SWS_BICUBLIN) ? (flags | SWS_BICUBIC) : flags,
  367. cpu_flags, srcFilter->lumV, dstFilter->lumV,
  368. c->param,
  369. get_local_pos(c, 0, 0, 1),
  370. get_local_pos(c, 0, 0, 1))) < 0)
  371. goto fail;
  372. if ((ret = initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize,
  373. c->chrYInc, c->chrSrcH, c->chrDstH,
  374. filterAlign, (1 << 12),
  375. (flags & SWS_BICUBLIN) ? (flags | SWS_BILINEAR) : flags,
  376. cpu_flags, srcFilter->chrV, dstFilter->chrV,
  377. c->param,
  378. get_local_pos(c, c->chrSrcVSubSample, c->src_v_chr_pos, 1),
  379. get_local_pos(c, c->chrDstVSubSample, c->dst_v_chr_pos, 1))) < 0)
  380. goto fail;
  381. #if HAVE_ALTIVEC
  382. FF_ALLOC_OR_GOTO(c, c->vYCoeffsBank, sizeof(vector signed short) * c->vLumFilterSize * c->dstH,    fail);
  383. FF_ALLOC_OR_GOTO(c, c->vCCoeffsBank, sizeof(vector signed short) * c->vChrFilterSize * c->chrDstH, fail);
  384. for (i = 0; i < c->vLumFilterSize * c->dstH; i++) {
  385. int j;
  386. short *p = (short *)&c->vYCoeffsBank[i];
  387. for (j = 0; j < 8; j++)
  388. p[j] = c->vLumFilter[i];
  389. }
  390. for (i = 0; i < c->vChrFilterSize * c->chrDstH; i++) {
  391. int j;
  392. short *p = (short *)&c->vCCoeffsBank[i];
  393. for (j = 0; j < 8; j++)
  394. p[j] = c->vChrFilter[i];
  395. }
  396. #endif
  397. }
  398. // calculate buffer sizes so that they won't run out while handling these damn slices
  399. c->vLumBufSize = c->vLumFilterSize;
  400. c->vChrBufSize = c->vChrFilterSize;
  401. for (i = 0; i < dstH; i++) {
  402. int chrI      = (int64_t)i * c->chrDstH / dstH;
  403. int nextSlice = FFMAX(c->vLumFilterPos[i] + c->vLumFilterSize - 1,
  404. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)
  405. << c->chrSrcVSubSample));
  406. nextSlice >>= c->chrSrcVSubSample;
  407. nextSlice <<= c->chrSrcVSubSample;
  408. if (c->vLumFilterPos[i] + c->vLumBufSize < nextSlice)
  409. c->vLumBufSize = nextSlice - c->vLumFilterPos[i];
  410. if (c->vChrFilterPos[chrI] + c->vChrBufSize <
  411. (nextSlice >> c->chrSrcVSubSample))
  412. c->vChrBufSize = (nextSlice >> c->chrSrcVSubSample) -
  413. c->vChrFilterPos[chrI];
  414. }
  415. for (i = 0; i < 4; i++)
  416. FF_ALLOCZ_OR_GOTO(c, c->dither_error[i], (c->dstW+2) * sizeof(int), fail);
  417. /* Allocate pixbufs (we use dynamic allocation because otherwise we would
  418. * need to allocate several megabytes to handle all possible cases) */
  419. FF_ALLOC_OR_GOTO(c, c->lumPixBuf,  c->vLumBufSize * 3 * sizeof(int16_t *), fail);
  420. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
  421. FF_ALLOC_OR_GOTO(c, c->chrVPixBuf, c->vChrBufSize * 3 * sizeof(int16_t *), fail);
  422. if (CONFIG_SWSCALE_ALPHA && isALPHA(c->srcFormat) && isALPHA(c->dstFormat))
  423. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf, c->vLumBufSize * 3 * sizeof(int16_t *), fail);
  424. /* Note we need at least one pixel more at the end because of the MMX code
  425. * (just in case someone wants to replace the 4000/8000). */
  426. /* align at 16 bytes for AltiVec */
  427. for (i = 0; i < c->vLumBufSize; i++) {
  428. FF_ALLOCZ_OR_GOTO(c, c->lumPixBuf[i + c->vLumBufSize],
  429. dst_stride + 16, fail);
  430. c->lumPixBuf[i] = c->lumPixBuf[i + c->vLumBufSize];
  431. }
  432. // 64 / c->scalingBpp is the same as 16 / sizeof(scaling_intermediate)
  433. c->uv_off   = (dst_stride>>1) + 64 / (c->dstBpc &~ 7);
  434. c->uv_offx2 = dst_stride + 16;
  435. for (i = 0; i < c->vChrBufSize; i++) {
  436. FF_ALLOC_OR_GOTO(c, c->chrUPixBuf[i + c->vChrBufSize],
  437. dst_stride * 2 + 32, fail);
  438. c->chrUPixBuf[i] = c->chrUPixBuf[i + c->vChrBufSize];
  439. c->chrVPixBuf[i] = c->chrVPixBuf[i + c->vChrBufSize]
  440. = c->chrUPixBuf[i] + (dst_stride >> 1) + 8;
  441. }
  442. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf)
  443. for (i = 0; i < c->vLumBufSize; i++) {
  444. FF_ALLOCZ_OR_GOTO(c, c->alpPixBuf[i + c->vLumBufSize],
  445. dst_stride + 16, fail);
  446. c->alpPixBuf[i] = c->alpPixBuf[i + c->vLumBufSize];
  447. }
  448. // try to avoid drawing green stuff between the right end and the stride end
  449. for (i = 0; i < c->vChrBufSize; i++)
  450. if(desc_dst->comp[0].depth_minus1 == 15){
  451. av_assert0(c->dstBpc > 14);
  452. for(j=0; j<dst_stride/2+1; j++)
  453. ((int32_t*)(c->chrUPixBuf[i]))[j] = 1<<18;
  454. } else
  455. for(j=0; j<dst_stride+1; j++)
  456. ((int16_t*)(c->chrUPixBuf[i]))[j] = 1<<14;
  457. av_assert0(c->chrDstH <= dstH);
  458. //是否要输出
  459. if (flags & SWS_PRINT_INFO) {
  460. const char *scaler = NULL, *cpucaps;
  461. for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
  462. if (flags & scale_algorithms[i].flag) {
  463. scaler = scale_algorithms[i].description;
  464. break;
  465. }
  466. }
  467. if (!scaler)
  468. scaler =  "ehh flags invalid?!";
  469. av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
  470. scaler,
  471. av_get_pix_fmt_name(srcFormat),
  472. #ifdef DITHER1XBPP
  473. dstFormat == AV_PIX_FMT_BGR555   || dstFormat == AV_PIX_FMT_BGR565   ||
  474. dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
  475. dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
  476. "dithered " : "",
  477. #else
  478. "",
  479. #endif
  480. av_get_pix_fmt_name(dstFormat));
  481. if (INLINE_MMXEXT(cpu_flags))
  482. cpucaps = "MMXEXT";
  483. else if (INLINE_AMD3DNOW(cpu_flags))
  484. cpucaps = "3DNOW";
  485. else if (INLINE_MMX(cpu_flags))
  486. cpucaps = "MMX";
  487. else if (PPC_ALTIVEC(cpu_flags))
  488. cpucaps = "AltiVec";
  489. else
  490. cpucaps = "C";
  491. av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
  492. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  493. av_log(c, AV_LOG_DEBUG,
  494. "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  495. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  496. av_log(c, AV_LOG_DEBUG,
  497. "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  498. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
  499. c->chrXInc, c->chrYInc);
  500. }
  501. /* unscaled special cases */
  502. //不拉伸的情况
  503. if (unscaled && !usesHFilter && !usesVFilter &&
  504. (c->srcRange == c->dstRange || isAnyRGB(dstFormat))) {
  505. //不许拉伸的情况下,初始化相应的函数
  506. ff_get_unscaled_swscale(c);
  507. if (c->swscale) {
  508. if (flags & SWS_PRINT_INFO)
  509. av_log(c, AV_LOG_INFO,
  510. "using unscaled %s -> %s special converter\n",
  511. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  512. return 0;
  513. }
  514. }
  515. //关键:设置SwsContext中的swscale()指针
  516. c->swscale = ff_getSwsFunc(c);
  517. return 0;
  518. fail: // FIXME replace things by appropriate error codes
  519. if (ret == RETCODE_USE_CASCADE)  {
  520. int tmpW = sqrt(srcW * (int64_t)dstW);
  521. int tmpH = sqrt(srcH * (int64_t)dstH);
  522. enum AVPixelFormat tmpFormat = AV_PIX_FMT_YUV420P;
  523. if (srcW*(int64_t)srcH <= 4LL*dstW*dstH)
  524. return AVERROR(EINVAL);
  525. ret = av_image_alloc(c->cascaded_tmp, c->cascaded_tmpStride,
  526. tmpW, tmpH, tmpFormat, 64);
  527. if (ret < 0)
  528. return ret;
  529. c->cascaded_context[0] = sws_getContext(srcW, srcH, srcFormat,
  530. tmpW, tmpH, tmpFormat,
  531. flags, srcFilter, NULL, c->param);
  532. if (!c->cascaded_context[0])
  533. return -1;
  534. c->cascaded_context[1] = sws_getContext(tmpW, tmpH, tmpFormat,
  535. dstW, dstH, dstFormat,
  536. flags, NULL, dstFilter, c->param);
  537. if (!c->cascaded_context[1])
  538. return -1;
  539. return 0;
  540. }
  541. return -1;
  542. }

sws_init_context()除了对SwsContext中的各种变量进行赋值之外,主要按照顺序完成了以下一些工作:

1. 通过sws_rgb2rgb_init()初始化RGB转RGB(或者YUV转YUV)的函数(注意不包含RGB与YUV相互转换的函数)。
2. 通过判断输入输出图像的宽高来判断图像是否需要拉伸。如果图像需要拉伸,那么unscaled变量会被标记为1。
3. 通过sws_setColorspaceDetails()初始化颜色空间。
4. 一些输入参数的检测。例如:如果没有设置图像拉伸方法的话,默认设置为SWS_BICUBIC;如果输入和输出图像的宽高小于等于0的话,也会返回错误信息。
5. 初始化Filter。这一步根据拉伸方法的不同,初始化不同的Filter。
6. 如果flags中设置了“打印信息”选项SWS_PRINT_INFO,则输出信息。
7. 如果不需要拉伸的话,调用ff_get_unscaled_swscale()将特定的像素转换函数的指针赋值给SwsContext中的swscale指针。
8. 如果需要拉伸的话,调用ff_getSwsFunc()将通用的swscale()赋值给SwsContext中的swscale指针(这个地方有点绕,但是确实是这样的)。

下面分别记录一下上述步骤的实现。

1.初始化RGB转RGB(或者YUV转YUV)的函数。注意这部分函数不包含RGB与YUV相互转换的函数。

sws_rgb2rgb_init()

sws_rgb2rgb_init()的定义位于libswscale\rgb2rgb.c,如下所示。

[cpp] view plaincopy
  1. av_cold void sws_rgb2rgb_init(void){
  2. rgb2rgb_init_c();
  3. if (ARCH_X86)
  4. rgb2rgb_init_x86();
  5. }

从sws_rgb2rgb_init()代码中可以看出,有两个初始化函数:rgb2rgb_init_c()是初始化C语言版本的RGB互转(或者YUV互转)的函数,rgb2rgb_init_x86()则是初始化X86汇编版本的RGB互转的函数。
PS:在libswscale中有一点需要注意:很多的函数名称中包含类似“_c”这样的字符串,代表了该函数是C语言写的。与之对应的还有其它标记,比如“_mmx”,“sse2”等。

rgb2rgb_init_c()

首先来看一下C语言版本的RGB互转函数的初始化函数rgb2rgb_init_c(),定义位于libswscale\rgb2rgb_template.c,如下所示。

[cpp] view plaincopy
  1. static av_cold void rgb2rgb_init_c(void)
  2. {
  3. rgb15to16          = rgb15to16_c;
  4. rgb15tobgr24       = rgb15tobgr24_c;
  5. rgb15to32          = rgb15to32_c;
  6. rgb16tobgr24       = rgb16tobgr24_c;
  7. rgb16to32          = rgb16to32_c;
  8. rgb16to15          = rgb16to15_c;
  9. rgb24tobgr16       = rgb24tobgr16_c;
  10. rgb24tobgr15       = rgb24tobgr15_c;
  11. rgb24tobgr32       = rgb24tobgr32_c;
  12. rgb32to16          = rgb32to16_c;
  13. rgb32to15          = rgb32to15_c;
  14. rgb32tobgr24       = rgb32tobgr24_c;
  15. rgb24to15          = rgb24to15_c;
  16. rgb24to16          = rgb24to16_c;
  17. rgb24tobgr24       = rgb24tobgr24_c;
  18. shuffle_bytes_2103 = shuffle_bytes_2103_c;
  19. rgb32tobgr16       = rgb32tobgr16_c;
  20. rgb32tobgr15       = rgb32tobgr15_c;
  21. yv12toyuy2         = yv12toyuy2_c;
  22. yv12touyvy         = yv12touyvy_c;
  23. yuv422ptoyuy2      = yuv422ptoyuy2_c;
  24. yuv422ptouyvy      = yuv422ptouyvy_c;
  25. yuy2toyv12         = yuy2toyv12_c;
  26. planar2x           = planar2x_c;
  27. ff_rgb24toyv12     = ff_rgb24toyv12_c;
  28. interleaveBytes    = interleaveBytes_c;
  29. deinterleaveBytes  = deinterleaveBytes_c;
  30. vu9_to_vu12        = vu9_to_vu12_c;
  31. yvu9_to_yuy2       = yvu9_to_yuy2_c;
  32. uyvytoyuv420       = uyvytoyuv420_c;
  33. uyvytoyuv422       = uyvytoyuv422_c;
  34. yuyvtoyuv420       = yuyvtoyuv420_c;
  35. yuyvtoyuv422       = yuyvtoyuv422_c;
  36. }

可以看出rgb2rgb_init_c()执行后,会把C语言版本的图像格式转换函数赋值给系统的函数指针。

下面我们选择几个函数看一下这些转换函数的定义。

rgb24tobgr24_c()

rgb24tobgr24_c()完成了RGB24向BGR24格式的转换。函数的定义如下所示。从代码中可以看出,该函数实现了“R”与“B”之间位置的对调,从而完成了这两种格式之间的转换。

[cpp] view plaincopy
  1. static inline void rgb24tobgr24_c(const uint8_t *src, uint8_t *dst, int src_size)
  2. {
  3. unsigned i;
  4. for (i = 0; i < src_size; i += 3) {
  5. register uint8_t x = src[i + 2];
  6. dst[i + 1]         = src[i + 1];
  7. dst[i + 2]         = src[i + 0];
  8. dst[i + 0]         = x;
  9. }
  10. }

rgb24to16_c()

rgb24to16_c()完成了RGB24向RGB16像素格式的转换。函数的定义如下所示。

[cpp] view plaincopy
  1. static inline void rgb24to16_c(const uint8_t *src, uint8_t *dst, int src_size)
  2. {
  3. uint16_t *d        = (uint16_t *)dst;
  4. const uint8_t *s   = src;
  5. const uint8_t *end = s + src_size;
  6. while (s < end) {
  7. const int r = *s++;
  8. const int g = *s++;
  9. const int b = *s++;
  10. *d++        = (b >> 3) | ((g & 0xFC) << 3) | ((r & 0xF8) << 8);
  11. }
  12. }

yuyvtoyuv422_c()

yuyvtoyuv422_c()完成了YUYV向YUV422像素格式的转换。函数的定义如下所示。

[cpp] view plaincopy
  1. static void yuyvtoyuv422_c(uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  2. const uint8_t *src, int width, int height,
  3. int lumStride, int chromStride, int srcStride)
  4. {
  5. int y;
  6. const int chromWidth = FF_CEIL_RSHIFT(width, 1);
  7. for (y = 0; y < height; y++) {
  8. extract_even_c(src, ydst, width);
  9. extract_odd2_c(src, udst, vdst, chromWidth);
  10. src  += srcStride;
  11. ydst += lumStride;
  12. udst += chromStride;
  13. vdst += chromStride;
  14. }
  15. }

该函数将YUYV像素数据分离成为Y,U,V三个分量的像素数据。其中extract_even_c()用于获取一行像素中序数为偶数的像素,对应提取了YUYV像素格式中的“Y”。extract_odd2_c()用于获取一行像素中序数为奇数的像素,并且把这些像素值再次按照奇偶的不同,存储于两个数组中。对应提取了YUYV像素格式中的“U”和“V”。
extract_even_c()定义如下所示。

[cpp] view plaincopy
  1. static void extract_even_c(const uint8_t *src, uint8_t *dst, int count)
  2. {
  3. dst   +=  count;
  4. src   +=  count * 2;
  5. count  = -count;
  6. while (count < 0) {
  7. dst[count] = src[2 * count];
  8. count++;
  9. }
  10. }

extract_odd2_c()定义如下所示。

[cpp] view plaincopy
  1. static void extract_even2_c(const uint8_t *src, uint8_t *dst0, uint8_t *dst1,
  2. int count)
  3. {
  4. dst0  +=  count;
  5. dst1  +=  count;
  6. src   +=  count * 4;
  7. count  = -count;
  8. while (count < 0) {
  9. dst0[count] = src[4 * count + 0];
  10. dst1[count] = src[4 * count + 2];
  11. count++;
  12. }
  13. }

rgb2rgb_init_x86()

rgb2rgb_init_x86()用于初始化基于X86汇编语言的RGB互转的代码。由于对汇编不是很熟,不再作详细分析,出于和rgb2rgb_init_c()相对比的目的,列出它的代码。它的代码位于libswscale\x86\rgb2rgb.c,如下所示。

PS:所有和汇编有关的代码都位于libswscale目录的x86子目录下。

[cpp] view plaincopy
  1. av_cold void rgb2rgb_init_x86(void)
  2. {
  3. #if HAVE_INLINE_ASM
  4. int cpu_flags = av_get_cpu_flags();
  5. if (INLINE_MMX(cpu_flags))
  6. rgb2rgb_init_mmx();
  7. if (INLINE_AMD3DNOW(cpu_flags))
  8. rgb2rgb_init_3dnow();
  9. if (INLINE_MMXEXT(cpu_flags))
  10. rgb2rgb_init_mmxext();
  11. if (INLINE_SSE2(cpu_flags))
  12. rgb2rgb_init_sse2();
  13. if (INLINE_AVX(cpu_flags))
  14. rgb2rgb_init_avx();
  15. #endif /* HAVE_INLINE_ASM */
  16. }

可以看出,rgb2rgb_init_x86()首先调用了av_get_cpu_flags()获取CPU支持的特性,根据特性调用rgb2rgb_init_mmx(),rgb2rgb_init_3dnow(),rgb2rgb_init_mmxext(),rgb2rgb_init_sse2(),rgb2rgb_init_avx()等函数。

2.判断图像是否需要拉伸。

这一步主要通过比较输入图像和输出图像的宽高实现。系统使用一个unscaled变量记录图像是否需要拉伸,如下所示。

[cpp] view plaincopy
  1. unscaled = (srcW == dstW && srcH == dstH);

3.初始化颜色空间。

初始化颜色空间通过函数sws_setColorspaceDetails()完成。sws_setColorspaceDetails()是FFmpeg的一个API函数,它的声明如下所示:

[cpp] view plaincopy
  1. /**
  2. * @param dstRange flag indicating the while-black range of the output (1=jpeg / 0=mpeg)
  3. * @param srcRange flag indicating the while-black range of the input (1=jpeg / 0=mpeg)
  4. * @param table the yuv2rgb coefficients describing the output yuv space, normally ff_yuv2rgb_coeffs[x]
  5. * @param inv_table the yuv2rgb coefficients describing the input yuv space, normally ff_yuv2rgb_coeffs[x]
  6. * @param brightness 16.16 fixed point brightness correction
  7. * @param contrast 16.16 fixed point contrast correction
  8. * @param saturation 16.16 fixed point saturation correction
  9. * @return -1 if not supported
  10. */
  11. int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
  12. int srcRange, const int table[4], int dstRange,
  13. int brightness, int contrast, int saturation);

简单解释一下几个参数的含义:

c:需要设定的SwsContext。
inv_table:描述输出YUV颜色空间的参数表。
srcRange:输入图像的取值范围(“1”代表JPEG标准,取值范围是0-255;“0”代表MPEG标准,取值范围是16-235)。
table:描述输入YUV颜色空间的参数表。
dstRange:输出图像的取值范围。
brightness:未研究。
contrast:未研究。
saturation:未研究。

如果返回-1代表设置不成功。
其中描述颜色空间的参数表可以通过sws_getCoefficients()获取。该函数在后文中再详细记录。
sws_setColorspaceDetails()的定义位于libswscale\utils.c,如下所示。

[cpp] view plaincopy
  1. int sws_setColorspaceDetails(struct SwsContext *c, const int inv_table[4],
  2. int srcRange, const int table[4], int dstRange,
  3. int brightness, int contrast, int saturation)
  4. {
  5. const AVPixFmtDescriptor *desc_dst;
  6. const AVPixFmtDescriptor *desc_src;
  7. int need_reinit = 0;
  8. memmove(c->srcColorspaceTable, inv_table, sizeof(int) * 4);
  9. memmove(c->dstColorspaceTable, table, sizeof(int) * 4);
  10. handle_formats(c);
  11. desc_dst = av_pix_fmt_desc_get(c->dstFormat);
  12. desc_src = av_pix_fmt_desc_get(c->srcFormat);
  13. if(!isYUV(c->dstFormat) && !isGray(c->dstFormat))
  14. dstRange = 0;
  15. if(!isYUV(c->srcFormat) && !isGray(c->srcFormat))
  16. srcRange = 0;
  17. c->brightness = brightness;
  18. c->contrast   = contrast;
  19. c->saturation = saturation;
  20. if (c->srcRange != srcRange || c->dstRange != dstRange)
  21. need_reinit = 1;
  22. c->srcRange   = srcRange;
  23. c->dstRange   = dstRange;
  24. //The srcBpc check is possibly wrong but we seem to lack a definitive reference to test this
  25. //and what we have in ticket 2939 looks better with this check
  26. if (need_reinit && (c->srcBpc == 8 || !isYUV(c->srcFormat)))
  27. ff_sws_init_range_convert(c);
  28. if ((isYUV(c->dstFormat) || isGray(c->dstFormat)) && (isYUV(c->srcFormat) || isGray(c->srcFormat)))
  29. return -1;
  30. c->dstFormatBpp = av_get_bits_per_pixel(desc_dst);
  31. c->srcFormatBpp = av_get_bits_per_pixel(desc_src);
  32. if (!isYUV(c->dstFormat) && !isGray(c->dstFormat)) {
  33. ff_yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness,
  34. contrast, saturation);
  35. // FIXME factorize
  36. if (ARCH_PPC)
  37. ff_yuv2rgb_init_tables_ppc(c, inv_table, brightness,
  38. contrast, saturation);
  39. }
  40. fill_rgb2yuv_table(c, table, dstRange);
  41. return 0;
  42. }

从sws_setColorspaceDetails()定义中可以看出,该函数将输入的参数分别赋值给了相应的变量,并且在最后调用了一个函数fill_rgb2yuv_table()。fill_rgb2yuv_table()函数还没有弄懂,暂时不记录。

sws_getCoefficients()

sws_getCoefficients()用于获取描述颜色空间的参数表。它的声明如下。

[cpp] view plaincopy
  1. /**
  2. * Return a pointer to yuv<->rgb coefficients for the given colorspace
  3. * suitable for sws_setColorspaceDetails().
  4. *
  5. * @param colorspace One of the SWS_CS_* macros. If invalid,
  6. * SWS_CS_DEFAULT is used.
  7. */
  8. const int *sws_getCoefficients(int colorspace);

其中colorspace可以取值如下变量。默认的取值SWS_CS_DEFAULT等同于SWS_CS_ITU601或者SWS_CS_SMPTE170M。

[cpp] view plaincopy
  1. #define SWS_CS_ITU709         1
  2. #define SWS_CS_FCC            4
  3. #define SWS_CS_ITU601         5
  4. #define SWS_CS_ITU624         5
  5. #define SWS_CS_SMPTE170M      5
  6. #define SWS_CS_SMPTE240M      7
  7. #define SWS_CS_DEFAULT        5

下面看一下sws_getCoefficients()的定义,位于libswscale\yuv2rgb.c,如下所示。

[cpp] view plaincopy
  1. const int *sws_getCoefficients(int colorspace)
  2. {
  3. if (colorspace > 7 || colorspace < 0)
  4. colorspace = SWS_CS_DEFAULT;
  5. return ff_yuv2rgb_coeffs[colorspace];
  6. }

可以看出它返回了一个名称为ff_yuv2rgb_coeffs的数组中的一个元素,该数组的定义如下所示。

[cpp] view plaincopy
  1. const int32_t ff_yuv2rgb_coeffs[8][4] = {
  2. { 117504, 138453, 13954, 34903 }, /* no sequence_display_extension */
  3. { 117504, 138453, 13954, 34903 }, /* ITU-R Rec. 709 (1990) */
  4. { 104597, 132201, 25675, 53279 }, /* unspecified */
  5. { 104597, 132201, 25675, 53279 }, /* reserved */
  6. { 104448, 132798, 24759, 53109 }, /* FCC */
  7. { 104597, 132201, 25675, 53279 }, /* ITU-R Rec. 624-4 System B, G */
  8. { 104597, 132201, 25675, 53279 }, /* SMPTE 170M */
  9. { 117579, 136230, 16907, 35559 }  /* SMPTE 240M (1987) */
  10. };

4.一些输入参数的检测。

例如:如果没有设置图像拉伸方法的话,默认设置为SWS_BICUBIC;如果输入和输出图像的宽高小于等于0的话,也会返回错误信息。有关这方面的代码比较多,简单举个例子。

[cpp] view plaincopy
  1. i = flags & (SWS_POINT         |
  2. SWS_AREA          |
  3. SWS_BILINEAR      |
  4. SWS_FAST_BILINEAR |
  5. SWS_BICUBIC       |
  6. SWS_X             |
  7. SWS_GAUSS         |
  8. SWS_LANCZOS       |
  9. SWS_SINC          |
  10. SWS_SPLINE        |
  11. SWS_BICUBLIN);
  12. /* provide a default scaler if not set by caller */
  13. if (!i) {
  14. if (dstW < srcW && dstH < srcH)
  15. flags |= SWS_BICUBIC;
  16. else if (dstW > srcW && dstH > srcH)
  17. flags |= SWS_BICUBIC;
  18. else
  19. flags |= SWS_BICUBIC;
  20. c->flags = flags;
  21. } else if (i & (i - 1)) {
  22. av_log(c, AV_LOG_ERROR,
  23. "Exactly one scaler algorithm must be chosen, got %X\n", i);
  24. return AVERROR(EINVAL);
  25. }
  26. /* sanity check */
  27. if (srcW < 1 || srcH < 1 || dstW < 1 || dstH < 1) {
  28. /* FIXME check if these are enough and try to lower them after
  29. * fixing the relevant parts of the code */
  30. av_log(c, AV_LOG_ERROR, "%dx%d -> %dx%d is invalid scaling dimension\n",
  31. srcW, srcH, dstW, dstH);
  32. return AVERROR(EINVAL);
  33. }

5.初始化Filter。这一步根据拉伸方法的不同,初始化不同的Filter。

这一部分的工作在函数initFilter()中完成,暂时不详细分析。

6.如果flags中设置了“打印信息”选项SWS_PRINT_INFO,则输出信息。

SwsContext初始化的时候,可以给flags设置SWS_PRINT_INFO标记。这样SwsContext初始化完成的时候就可以打印出一些配置信息。与打印相关的代码如下所示。

[cpp] view plaincopy
  1. if (flags & SWS_PRINT_INFO) {
  2. const char *scaler = NULL, *cpucaps;
  3. for (i = 0; i < FF_ARRAY_ELEMS(scale_algorithms); i++) {
  4. if (flags & scale_algorithms[i].flag) {
  5. scaler = scale_algorithms[i].description;
  6. break;
  7. }
  8. }
  9. if (!scaler)
  10. scaler =  "ehh flags invalid?!";
  11. av_log(c, AV_LOG_INFO, "%s scaler, from %s to %s%s ",
  12. scaler,
  13. av_get_pix_fmt_name(srcFormat),
  14. #ifdef DITHER1XBPP
  15. dstFormat == AV_PIX_FMT_BGR555   || dstFormat == AV_PIX_FMT_BGR565   ||
  16. dstFormat == AV_PIX_FMT_RGB444BE || dstFormat == AV_PIX_FMT_RGB444LE ||
  17. dstFormat == AV_PIX_FMT_BGR444BE || dstFormat == AV_PIX_FMT_BGR444LE ?
  18. "dithered " : "",
  19. #else
  20. "",
  21. #endif
  22. av_get_pix_fmt_name(dstFormat));
  23. if (INLINE_MMXEXT(cpu_flags))
  24. cpucaps = "MMXEXT";
  25. else if (INLINE_AMD3DNOW(cpu_flags))
  26. cpucaps = "3DNOW";
  27. else if (INLINE_MMX(cpu_flags))
  28. cpucaps = "MMX";
  29. else if (PPC_ALTIVEC(cpu_flags))
  30. cpucaps = "AltiVec";
  31. else
  32. cpucaps = "C";
  33. av_log(c, AV_LOG_INFO, "using %s\n", cpucaps);
  34. av_log(c, AV_LOG_VERBOSE, "%dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  35. av_log(c, AV_LOG_DEBUG,
  36. "lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  37. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  38. av_log(c, AV_LOG_DEBUG,
  39. "chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  40. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH,
  41. c->chrXInc, c->chrYInc);
  42. }

7.如果不需要拉伸的话,就会调用ff_get_unscaled_swscale()将特定的像素转换函数的指针赋值给SwsContext中的swscale指针。

ff_get_unscaled_swscale()

ff_get_unscaled_swscale()的定义如下所示。该函数根据输入图像像素格式和输出图像像素格式,选择不同的像素格式转换函数。

[cpp] view plaincopy
  1. void ff_get_unscaled_swscale(SwsContext *c)
  2. {
  3. const enum AVPixelFormat srcFormat = c->srcFormat;
  4. const enum AVPixelFormat dstFormat = c->dstFormat;
  5. const int flags = c->flags;
  6. const int dstH = c->dstH;
  7. int needsDither;
  8. needsDither = isAnyRGB(dstFormat) &&
  9. c->dstFormatBpp < 24 &&
  10. (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  11. /* yv12_to_nv12 */
  12. if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
  13. (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
  14. c->swscale = planarToNv12Wrapper;
  15. }
  16. /* nv12_to_yv12 */
  17. if (dstFormat == AV_PIX_FMT_YUV420P &&
  18. (srcFormat == AV_PIX_FMT_NV12 || srcFormat == AV_PIX_FMT_NV21)) {
  19. c->swscale = nv12ToPlanarWrapper;
  20. }
  21. /* yuv2bgr */
  22. if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
  23. srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
  24. !(flags & SWS_ACCURATE_RND) && (c->dither == SWS_DITHER_BAYER || c->dither == SWS_DITHER_AUTO) && !(dstH & 1)) {
  25. c->swscale = ff_yuv2rgb_get_func_ptr(c);
  26. }
  27. if (srcFormat == AV_PIX_FMT_YUV410P && !(dstH & 3) &&
  28. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
  29. !(flags & SWS_BITEXACT)) {
  30. c->swscale = yvu9ToYv12Wrapper;
  31. }
  32. /* bgr24toYV12 */
  33. if (srcFormat == AV_PIX_FMT_BGR24 &&
  34. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
  35. !(flags & SWS_ACCURATE_RND))
  36. c->swscale = bgr24ToYv12Wrapper;
  37. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  38. if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
  39. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  40. c->swscale = rgbToRgbWrapper;
  41. if ((srcFormat == AV_PIX_FMT_GBRP && dstFormat == AV_PIX_FMT_GBRAP) ||
  42. (srcFormat == AV_PIX_FMT_GBRAP && dstFormat == AV_PIX_FMT_GBRP))
  43. c->swscale = planarRgbToplanarRgbWrapper;
  44. #define isByteRGB(f) (             \
  45. f == AV_PIX_FMT_RGB32   || \
  46. f == AV_PIX_FMT_RGB32_1 || \
  47. f == AV_PIX_FMT_RGB24   || \
  48. f == AV_PIX_FMT_BGR32   || \
  49. f == AV_PIX_FMT_BGR32_1 || \
  50. f == AV_PIX_FMT_BGR24)
  51. if (srcFormat == AV_PIX_FMT_GBRP && isPlanar(srcFormat) && isByteRGB(dstFormat))
  52. c->swscale = planarRgbToRgbWrapper;
  53. if ((srcFormat == AV_PIX_FMT_RGB48LE  || srcFormat == AV_PIX_FMT_RGB48BE  ||
  54. srcFormat == AV_PIX_FMT_BGR48LE  || srcFormat == AV_PIX_FMT_BGR48BE  ||
  55. srcFormat == AV_PIX_FMT_RGBA64LE || srcFormat == AV_PIX_FMT_RGBA64BE ||
  56. srcFormat == AV_PIX_FMT_BGRA64LE || srcFormat == AV_PIX_FMT_BGRA64BE) &&
  57. (dstFormat == AV_PIX_FMT_GBRP9LE  || dstFormat == AV_PIX_FMT_GBRP9BE  ||
  58. dstFormat == AV_PIX_FMT_GBRP10LE || dstFormat == AV_PIX_FMT_GBRP10BE ||
  59. dstFormat == AV_PIX_FMT_GBRP12LE || dstFormat == AV_PIX_FMT_GBRP12BE ||
  60. dstFormat == AV_PIX_FMT_GBRP14LE || dstFormat == AV_PIX_FMT_GBRP14BE ||
  61. dstFormat == AV_PIX_FMT_GBRP16LE || dstFormat == AV_PIX_FMT_GBRP16BE ||
  62. dstFormat == AV_PIX_FMT_GBRAP16LE || dstFormat == AV_PIX_FMT_GBRAP16BE ))
  63. c->swscale = Rgb16ToPlanarRgb16Wrapper;
  64. if ((srcFormat == AV_PIX_FMT_GBRP9LE  || srcFormat == AV_PIX_FMT_GBRP9BE  ||
  65. srcFormat == AV_PIX_FMT_GBRP16LE || srcFormat == AV_PIX_FMT_GBRP16BE ||
  66. srcFormat == AV_PIX_FMT_GBRP10LE || srcFormat == AV_PIX_FMT_GBRP10BE ||
  67. srcFormat == AV_PIX_FMT_GBRP12LE || srcFormat == AV_PIX_FMT_GBRP12BE ||
  68. srcFormat == AV_PIX_FMT_GBRP14LE || srcFormat == AV_PIX_FMT_GBRP14BE ||
  69. srcFormat == AV_PIX_FMT_GBRAP16LE || srcFormat == AV_PIX_FMT_GBRAP16BE) &&
  70. (dstFormat == AV_PIX_FMT_RGB48LE  || dstFormat == AV_PIX_FMT_RGB48BE  ||
  71. dstFormat == AV_PIX_FMT_BGR48LE  || dstFormat == AV_PIX_FMT_BGR48BE  ||
  72. dstFormat == AV_PIX_FMT_RGBA64LE || dstFormat == AV_PIX_FMT_RGBA64BE ||
  73. dstFormat == AV_PIX_FMT_BGRA64LE || dstFormat == AV_PIX_FMT_BGRA64BE))
  74. c->swscale = planarRgb16ToRgb16Wrapper;
  75. if (av_pix_fmt_desc_get(srcFormat)->comp[0].depth_minus1 == 7 &&
  76. isPackedRGB(srcFormat) && dstFormat == AV_PIX_FMT_GBRP)
  77. c->swscale = rgbToPlanarRgbWrapper;
  78. if (isBayer(srcFormat)) {
  79. if (dstFormat == AV_PIX_FMT_RGB24)
  80. c->swscale = bayer_to_rgb24_wrapper;
  81. else if (dstFormat == AV_PIX_FMT_YUV420P)
  82. c->swscale = bayer_to_yv12_wrapper;
  83. else if (!isBayer(dstFormat)) {
  84. av_log(c, AV_LOG_ERROR, "unsupported bayer conversion\n");
  85. av_assert0(0);
  86. }
  87. }
  88. /* bswap 16 bits per pixel/component packed formats */
  89. if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_BGGR16) ||
  90. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_RGGB16) ||
  91. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_GBRG16) ||
  92. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_GRBG16) ||
  93. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR444) ||
  94. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR48)  ||
  95. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
  96. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR555) ||
  97. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR565) ||
  98. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
  99. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY16) ||
  100. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YA16)   ||
  101. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP9)  ||
  102. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP10) ||
  103. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP12) ||
  104. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP14) ||
  105. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP16) ||
  106. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRAP16) ||
  107. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB444) ||
  108. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB48)  ||
  109. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
  110. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB555) ||
  111. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB565) ||
  112. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
  113. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_XYZ12)  ||
  114. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P9)  ||
  115. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P10) ||
  116. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P12) ||
  117. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P14) ||
  118. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P16) ||
  119. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P9)  ||
  120. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P10) ||
  121. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P12) ||
  122. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P14) ||
  123. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P16) ||
  124. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P9)  ||
  125. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P10) ||
  126. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P12) ||
  127. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P14) ||
  128. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P16))
  129. c->swscale = packed_16bpc_bswap;
  130. if (usePal(srcFormat) && isByteRGB(dstFormat))
  131. c->swscale = palToRgbWrapper;
  132. if (srcFormat == AV_PIX_FMT_YUV422P) {
  133. if (dstFormat == AV_PIX_FMT_YUYV422)
  134. c->swscale = yuv422pToYuy2Wrapper;
  135. else if (dstFormat == AV_PIX_FMT_UYVY422)
  136. c->swscale = yuv422pToUyvyWrapper;
  137. }
  138. /* LQ converters if -sws 0 or -sws 4*/
  139. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  140. /* yv12_to_yuy2 */
  141. if (srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) {
  142. if (dstFormat == AV_PIX_FMT_YUYV422)
  143. c->swscale = planarToYuy2Wrapper;
  144. else if (dstFormat == AV_PIX_FMT_UYVY422)
  145. c->swscale = planarToUyvyWrapper;
  146. }
  147. }
  148. if (srcFormat == AV_PIX_FMT_YUYV422 &&
  149. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
  150. c->swscale = yuyvToYuv420Wrapper;
  151. if (srcFormat == AV_PIX_FMT_UYVY422 &&
  152. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
  153. c->swscale = uyvyToYuv420Wrapper;
  154. if (srcFormat == AV_PIX_FMT_YUYV422 && dstFormat == AV_PIX_FMT_YUV422P)
  155. c->swscale = yuyvToYuv422Wrapper;
  156. if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P)
  157. c->swscale = uyvyToYuv422Wrapper;
  158. #define isPlanarGray(x) (isGray(x) && (x) != AV_PIX_FMT_YA8 && (x) != AV_PIX_FMT_YA16LE && (x) != AV_PIX_FMT_YA16BE)
  159. /* simple copy */
  160. if ( srcFormat == dstFormat ||
  161. (srcFormat == AV_PIX_FMT_YUVA420P && dstFormat == AV_PIX_FMT_YUV420P) ||
  162. (srcFormat == AV_PIX_FMT_YUV420P && dstFormat == AV_PIX_FMT_YUVA420P) ||
  163. (isPlanarYUV(srcFormat) && isPlanarGray(dstFormat)) ||
  164. (isPlanarYUV(dstFormat) && isPlanarGray(srcFormat)) ||
  165. (isPlanarGray(dstFormat) && isPlanarGray(srcFormat)) ||
  166. (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
  167. c->chrDstHSubSample == c->chrSrcHSubSample &&
  168. c->chrDstVSubSample == c->chrSrcVSubSample &&
  169. dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
  170. srcFormat != AV_PIX_FMT_NV12 && srcFormat != AV_PIX_FMT_NV21))
  171. {
  172. if (isPacked(c->srcFormat))
  173. c->swscale = packedCopyWrapper;
  174. else /* Planar YUV or gray */
  175. c->swscale = planarCopyWrapper;
  176. }
  177. if (ARCH_PPC)
  178. ff_get_unscaled_swscale_ppc(c);
  179. //     if (ARCH_ARM)
  180. //         ff_get_unscaled_swscale_arm(c);
  181. }

从ff_get_unscaled_swscale()源代码中可以看出,赋值给SwsContext的swscale指针的函数名称大多数为XXXWrapper()。实际上这些函数封装了一些基本的像素格式转换函数。例如yuyvToYuv422Wrapper()的定义如下所示。

[cpp] view plaincopy
  1. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
  2. int srcStride[], int srcSliceY, int srcSliceH,
  3. uint8_t *dstParam[], int dstStride[])
  4. {
  5. uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
  6. uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
  7. uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
  8. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
  9. dstStride[1], srcStride[0]);
  10. return srcSliceH;
  11. }

从yuyvToYuv422Wrapper()的定义中可以看出,它调用了yuyvtoyuv422()。而yuyvtoyuv422()则是rgb2rgb.c中的一个函数,用于将YUVU转换为YUV422(该函数在前文中已经记录)。

8.如果需要拉伸的话,就会调用ff_getSwsFunc()将通用的swscale()赋值给SwsContext中的swscale指针,然后返回。
上一步骤(图像不用缩放)实际上是一种不太常见的情况,更多的情况下会执行本步骤。这个时候就会调用ff_getSwsFunc()获取图像的缩放函数。

ff_getSwsFunc()

ff_getSwsFunc()用于获取通用的swscale()函数。该函数的定义如下。

[cpp] view plaincopy
  1. SwsFunc ff_getSwsFunc(SwsContext *c)
  2. {
  3. sws_init_swscale(c);
  4. if (ARCH_PPC)
  5. ff_sws_init_swscale_ppc(c);
  6. if (ARCH_X86)
  7. ff_sws_init_swscale_x86(c);
  8. return swscale;
  9. }

从源代码中可以看出ff_getSwsFunc()调用了函数sws_init_swscale()。如果系统支持X86汇编的话,还会调用ff_sws_init_swscale_x86()。

sws_init_swscale()

sws_init_swscale()的定义位于libswscale\swscale.c,如下所示。

[cpp] view plaincopy
  1. static av_cold void sws_init_swscale(SwsContext *c)
  2. {
  3. enum AVPixelFormat srcFormat = c->srcFormat;
  4. ff_sws_init_output_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  5. &c->yuv2nv12cX, &c->yuv2packed1,
  6. &c->yuv2packed2, &c->yuv2packedX, &c->yuv2anyX);
  7. ff_sws_init_input_funcs(c);
  8. if (c->srcBpc == 8) {
  9. if (c->dstBpc <= 14) {
  10. c->hyScale = c->hcScale = hScale8To15_c;
  11. if (c->flags & SWS_FAST_BILINEAR) {
  12. c->hyscale_fast = ff_hyscale_fast_c;
  13. c->hcscale_fast = ff_hcscale_fast_c;
  14. }
  15. } else {
  16. c->hyScale = c->hcScale = hScale8To19_c;
  17. }
  18. } else {
  19. c->hyScale = c->hcScale = c->dstBpc > 14 ? hScale16To19_c
  20. : hScale16To15_c;
  21. }
  22. ff_sws_init_range_convert(c);
  23. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  24. srcFormat == AV_PIX_FMT_MONOBLACK || srcFormat == AV_PIX_FMT_MONOWHITE))
  25. c->needs_hcscale = 1;
  26. }

从函数中可以看出,sws_init_swscale()主要调用了3个函数:ff_sws_init_output_funcs(),ff_sws_init_input_funcs(),ff_sws_init_range_convert()。其中,ff_sws_init_output_funcs()用于初始化输出的函数,ff_sws_init_input_funcs()用于初始化输入的函数,ff_sws_init_range_convert()用于初始化像素值范围转换的函数。

ff_sws_init_output_funcs()

ff_sws_init_output_funcs()用于初始化“输出函数”。“输出函数”在libswscale中的作用就是将处理后的一行像素数据输出出来。ff_sws_init_output_funcs()的定义位于libswscale\output.c,如下所示。

[cpp] view plaincopy
  1. av_cold void ff_sws_init_output_funcs(SwsContext *c,
  2. yuv2planar1_fn *yuv2plane1,
  3. yuv2planarX_fn *yuv2planeX,
  4. yuv2interleavedX_fn *yuv2nv12cX,
  5. yuv2packed1_fn *yuv2packed1,
  6. yuv2packed2_fn *yuv2packed2,
  7. yuv2packedX_fn *yuv2packedX,
  8. yuv2anyX_fn *yuv2anyX)
  9. {
  10. enum AVPixelFormat dstFormat = c->dstFormat;
  11. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(dstFormat);
  12. if (is16BPS(dstFormat)) {
  13. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c  : yuv2planeX_16LE_c;
  14. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c  : yuv2plane1_16LE_c;
  15. } else if (is9_OR_10BPS(dstFormat)) {
  16. if (desc->comp[0].depth_minus1 == 8) {
  17. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c  : yuv2planeX_9LE_c;
  18. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c  : yuv2plane1_9LE_c;
  19. } else if (desc->comp[0].depth_minus1 == 9) {
  20. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c  : yuv2planeX_10LE_c;
  21. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c  : yuv2plane1_10LE_c;
  22. } else if (desc->comp[0].depth_minus1 == 11) {
  23. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_12BE_c  : yuv2planeX_12LE_c;
  24. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_12BE_c  : yuv2plane1_12LE_c;
  25. } else if (desc->comp[0].depth_minus1 == 13) {
  26. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_14BE_c  : yuv2planeX_14LE_c;
  27. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_14BE_c  : yuv2plane1_14LE_c;
  28. } else
  29. av_assert0(0);
  30. } else {
  31. *yuv2plane1 = yuv2plane1_8_c;
  32. *yuv2planeX = yuv2planeX_8_c;
  33. if (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)
  34. *yuv2nv12cX = yuv2nv12cX_c;
  35. }
  36. if(c->flags & SWS_FULL_CHR_H_INT) {
  37. switch (dstFormat) {
  38. case AV_PIX_FMT_RGBA:
  39. #if CONFIG_SMALL
  40. *yuv2packedX = yuv2rgba32_full_X_c;
  41. *yuv2packed2 = yuv2rgba32_full_2_c;
  42. *yuv2packed1 = yuv2rgba32_full_1_c;
  43. #else
  44. #if CONFIG_SWSCALE_ALPHA
  45. if (c->alpPixBuf) {
  46. *yuv2packedX = yuv2rgba32_full_X_c;
  47. *yuv2packed2 = yuv2rgba32_full_2_c;
  48. *yuv2packed1 = yuv2rgba32_full_1_c;
  49. } else
  50. #endif /* CONFIG_SWSCALE_ALPHA */
  51. {
  52. *yuv2packedX = yuv2rgbx32_full_X_c;
  53. *yuv2packed2 = yuv2rgbx32_full_2_c;
  54. *yuv2packed1 = yuv2rgbx32_full_1_c;
  55. }
  56. #endif /* !CONFIG_SMALL */
  57. break;
  58. case AV_PIX_FMT_ARGB:
  59. #if CONFIG_SMALL
  60. *yuv2packedX = yuv2argb32_full_X_c;
  61. *yuv2packed2 = yuv2argb32_full_2_c;
  62. *yuv2packed1 = yuv2argb32_full_1_c;
  63. #else
  64. #if CONFIG_SWSCALE_ALPHA
  65. if (c->alpPixBuf) {
  66. *yuv2packedX = yuv2argb32_full_X_c;
  67. *yuv2packed2 = yuv2argb32_full_2_c;
  68. *yuv2packed1 = yuv2argb32_full_1_c;
  69. } else
  70. #endif /* CONFIG_SWSCALE_ALPHA */
  71. {
  72. *yuv2packedX = yuv2xrgb32_full_X_c;
  73. *yuv2packed2 = yuv2xrgb32_full_2_c;
  74. *yuv2packed1 = yuv2xrgb32_full_1_c;
  75. }
  76. #endif /* !CONFIG_SMALL */
  77. break;
  78. case AV_PIX_FMT_BGRA:
  79. #if CONFIG_SMALL
  80. *yuv2packedX = yuv2bgra32_full_X_c;
  81. *yuv2packed2 = yuv2bgra32_full_2_c;
  82. *yuv2packed1 = yuv2bgra32_full_1_c;
  83. #else
  84. #if CONFIG_SWSCALE_ALPHA
  85. if (c->alpPixBuf) {
  86. *yuv2packedX = yuv2bgra32_full_X_c;
  87. *yuv2packed2 = yuv2bgra32_full_2_c;
  88. *yuv2packed1 = yuv2bgra32_full_1_c;
  89. } else
  90. #endif /* CONFIG_SWSCALE_ALPHA */
  91. {
  92. *yuv2packedX = yuv2bgrx32_full_X_c;
  93. *yuv2packed2 = yuv2bgrx32_full_2_c;
  94. *yuv2packed1 = yuv2bgrx32_full_1_c;
  95. }
  96. #endif /* !CONFIG_SMALL */
  97. break;
  98. case AV_PIX_FMT_ABGR:
  99. #if CONFIG_SMALL
  100. *yuv2packedX = yuv2abgr32_full_X_c;
  101. *yuv2packed2 = yuv2abgr32_full_2_c;
  102. *yuv2packed1 = yuv2abgr32_full_1_c;
  103. #else
  104. #if CONFIG_SWSCALE_ALPHA
  105. if (c->alpPixBuf) {
  106. *yuv2packedX = yuv2abgr32_full_X_c;
  107. *yuv2packed2 = yuv2abgr32_full_2_c;
  108. *yuv2packed1 = yuv2abgr32_full_1_c;
  109. } else
  110. #endif /* CONFIG_SWSCALE_ALPHA */
  111. {
  112. *yuv2packedX = yuv2xbgr32_full_X_c;
  113. *yuv2packed2 = yuv2xbgr32_full_2_c;
  114. *yuv2packed1 = yuv2xbgr32_full_1_c;
  115. }
  116. #endif /* !CONFIG_SMALL */
  117. break;
  118. case AV_PIX_FMT_RGB24:
  119. *yuv2packedX = yuv2rgb24_full_X_c;
  120. *yuv2packed2 = yuv2rgb24_full_2_c;
  121. *yuv2packed1 = yuv2rgb24_full_1_c;
  122. break;
  123. case AV_PIX_FMT_BGR24:
  124. *yuv2packedX = yuv2bgr24_full_X_c;
  125. *yuv2packed2 = yuv2bgr24_full_2_c;
  126. *yuv2packed1 = yuv2bgr24_full_1_c;
  127. break;
  128. case AV_PIX_FMT_BGR4_BYTE:
  129. *yuv2packedX = yuv2bgr4_byte_full_X_c;
  130. *yuv2packed2 = yuv2bgr4_byte_full_2_c;
  131. *yuv2packed1 = yuv2bgr4_byte_full_1_c;
  132. break;
  133. case AV_PIX_FMT_RGB4_BYTE:
  134. *yuv2packedX = yuv2rgb4_byte_full_X_c;
  135. *yuv2packed2 = yuv2rgb4_byte_full_2_c;
  136. *yuv2packed1 = yuv2rgb4_byte_full_1_c;
  137. break;
  138. case AV_PIX_FMT_BGR8:
  139. *yuv2packedX = yuv2bgr8_full_X_c;
  140. *yuv2packed2 = yuv2bgr8_full_2_c;
  141. *yuv2packed1 = yuv2bgr8_full_1_c;
  142. break;
  143. case AV_PIX_FMT_RGB8:
  144. *yuv2packedX = yuv2rgb8_full_X_c;
  145. *yuv2packed2 = yuv2rgb8_full_2_c;
  146. *yuv2packed1 = yuv2rgb8_full_1_c;
  147. break;
  148. case AV_PIX_FMT_GBRP:
  149. case AV_PIX_FMT_GBRP9BE:
  150. case AV_PIX_FMT_GBRP9LE:
  151. case AV_PIX_FMT_GBRP10BE:
  152. case AV_PIX_FMT_GBRP10LE:
  153. case AV_PIX_FMT_GBRP12BE:
  154. case AV_PIX_FMT_GBRP12LE:
  155. case AV_PIX_FMT_GBRP14BE:
  156. case AV_PIX_FMT_GBRP14LE:
  157. case AV_PIX_FMT_GBRP16BE:
  158. case AV_PIX_FMT_GBRP16LE:
  159. case AV_PIX_FMT_GBRAP:
  160. *yuv2anyX = yuv2gbrp_full_X_c;
  161. break;
  162. }
  163. if (!*yuv2packedX && !*yuv2anyX)
  164. goto YUV_PACKED;
  165. } else {
  166. YUV_PACKED:
  167. switch (dstFormat) {
  168. case AV_PIX_FMT_RGBA64LE:
  169. #if CONFIG_SWSCALE_ALPHA
  170. if (c->alpPixBuf) {
  171. *yuv2packed1 = yuv2rgba64le_1_c;
  172. *yuv2packed2 = yuv2rgba64le_2_c;
  173. *yuv2packedX = yuv2rgba64le_X_c;
  174. } else
  175. #endif /* CONFIG_SWSCALE_ALPHA */
  176. {
  177. *yuv2packed1 = yuv2rgbx64le_1_c;
  178. *yuv2packed2 = yuv2rgbx64le_2_c;
  179. *yuv2packedX = yuv2rgbx64le_X_c;
  180. }
  181. break;
  182. case AV_PIX_FMT_RGBA64BE:
  183. #if CONFIG_SWSCALE_ALPHA
  184. if (c->alpPixBuf) {
  185. *yuv2packed1 = yuv2rgba64be_1_c;
  186. *yuv2packed2 = yuv2rgba64be_2_c;
  187. *yuv2packedX = yuv2rgba64be_X_c;
  188. } else
  189. #endif /* CONFIG_SWSCALE_ALPHA */
  190. {
  191. *yuv2packed1 = yuv2rgbx64be_1_c;
  192. *yuv2packed2 = yuv2rgbx64be_2_c;
  193. *yuv2packedX = yuv2rgbx64be_X_c;
  194. }
  195. break;
  196. case AV_PIX_FMT_BGRA64LE:
  197. #if CONFIG_SWSCALE_ALPHA
  198. if (c->alpPixBuf) {
  199. *yuv2packed1 = yuv2bgra64le_1_c;
  200. *yuv2packed2 = yuv2bgra64le_2_c;
  201. *yuv2packedX = yuv2bgra64le_X_c;
  202. } else
  203. #endif /* CONFIG_SWSCALE_ALPHA */
  204. {
  205. *yuv2packed1 = yuv2bgrx64le_1_c;
  206. *yuv2packed2 = yuv2bgrx64le_2_c;
  207. *yuv2packedX = yuv2bgrx64le_X_c;
  208. }
  209. break;
  210. case AV_PIX_FMT_BGRA64BE:
  211. #if CONFIG_SWSCALE_ALPHA
  212. if (c->alpPixBuf) {
  213. *yuv2packed1 = yuv2bgra64be_1_c;
  214. *yuv2packed2 = yuv2bgra64be_2_c;
  215. *yuv2packedX = yuv2bgra64be_X_c;
  216. } else
  217. #endif /* CONFIG_SWSCALE_ALPHA */
  218. {
  219. *yuv2packed1 = yuv2bgrx64be_1_c;
  220. *yuv2packed2 = yuv2bgrx64be_2_c;
  221. *yuv2packedX = yuv2bgrx64be_X_c;
  222. }
  223. break;
  224. case AV_PIX_FMT_RGB48LE:
  225. *yuv2packed1 = yuv2rgb48le_1_c;
  226. *yuv2packed2 = yuv2rgb48le_2_c;
  227. *yuv2packedX = yuv2rgb48le_X_c;
  228. break;
  229. case AV_PIX_FMT_RGB48BE:
  230. *yuv2packed1 = yuv2rgb48be_1_c;
  231. *yuv2packed2 = yuv2rgb48be_2_c;
  232. *yuv2packedX = yuv2rgb48be_X_c;
  233. break;
  234. case AV_PIX_FMT_BGR48LE:
  235. *yuv2packed1 = yuv2bgr48le_1_c;
  236. *yuv2packed2 = yuv2bgr48le_2_c;
  237. *yuv2packedX = yuv2bgr48le_X_c;
  238. break;
  239. case AV_PIX_FMT_BGR48BE:
  240. *yuv2packed1 = yuv2bgr48be_1_c;
  241. *yuv2packed2 = yuv2bgr48be_2_c;
  242. *yuv2packedX = yuv2bgr48be_X_c;
  243. break;
  244. case AV_PIX_FMT_RGB32:
  245. case AV_PIX_FMT_BGR32:
  246. #if CONFIG_SMALL
  247. *yuv2packed1 = yuv2rgb32_1_c;
  248. *yuv2packed2 = yuv2rgb32_2_c;
  249. *yuv2packedX = yuv2rgb32_X_c;
  250. #else
  251. #if CONFIG_SWSCALE_ALPHA
  252. if (c->alpPixBuf) {
  253. *yuv2packed1 = yuv2rgba32_1_c;
  254. *yuv2packed2 = yuv2rgba32_2_c;
  255. *yuv2packedX = yuv2rgba32_X_c;
  256. } else
  257. #endif /* CONFIG_SWSCALE_ALPHA */
  258. {
  259. *yuv2packed1 = yuv2rgbx32_1_c;
  260. *yuv2packed2 = yuv2rgbx32_2_c;
  261. *yuv2packedX = yuv2rgbx32_X_c;
  262. }
  263. #endif /* !CONFIG_SMALL */
  264. break;
  265. case AV_PIX_FMT_RGB32_1:
  266. case AV_PIX_FMT_BGR32_1:
  267. #if CONFIG_SMALL
  268. *yuv2packed1 = yuv2rgb32_1_1_c;
  269. *yuv2packed2 = yuv2rgb32_1_2_c;
  270. *yuv2packedX = yuv2rgb32_1_X_c;
  271. #else
  272. #if CONFIG_SWSCALE_ALPHA
  273. if (c->alpPixBuf) {
  274. *yuv2packed1 = yuv2rgba32_1_1_c;
  275. *yuv2packed2 = yuv2rgba32_1_2_c;
  276. *yuv2packedX = yuv2rgba32_1_X_c;
  277. } else
  278. #endif /* CONFIG_SWSCALE_ALPHA */
  279. {
  280. *yuv2packed1 = yuv2rgbx32_1_1_c;
  281. *yuv2packed2 = yuv2rgbx32_1_2_c;
  282. *yuv2packedX = yuv2rgbx32_1_X_c;
  283. }
  284. #endif /* !CONFIG_SMALL */
  285. break;
  286. case AV_PIX_FMT_RGB24:
  287. *yuv2packed1 = yuv2rgb24_1_c;
  288. *yuv2packed2 = yuv2rgb24_2_c;
  289. *yuv2packedX = yuv2rgb24_X_c;
  290. break;
  291. case AV_PIX_FMT_BGR24:
  292. *yuv2packed1 = yuv2bgr24_1_c;
  293. *yuv2packed2 = yuv2bgr24_2_c;
  294. *yuv2packedX = yuv2bgr24_X_c;
  295. break;
  296. case AV_PIX_FMT_RGB565LE:
  297. case AV_PIX_FMT_RGB565BE:
  298. case AV_PIX_FMT_BGR565LE:
  299. case AV_PIX_FMT_BGR565BE:
  300. *yuv2packed1 = yuv2rgb16_1_c;
  301. *yuv2packed2 = yuv2rgb16_2_c;
  302. *yuv2packedX = yuv2rgb16_X_c;
  303. break;
  304. case AV_PIX_FMT_RGB555LE:
  305. case AV_PIX_FMT_RGB555BE:
  306. case AV_PIX_FMT_BGR555LE:
  307. case AV_PIX_FMT_BGR555BE:
  308. *yuv2packed1 = yuv2rgb15_1_c;
  309. *yuv2packed2 = yuv2rgb15_2_c;
  310. *yuv2packedX = yuv2rgb15_X_c;
  311. break;
  312. case AV_PIX_FMT_RGB444LE:
  313. case AV_PIX_FMT_RGB444BE:
  314. case AV_PIX_FMT_BGR444LE:
  315. case AV_PIX_FMT_BGR444BE:
  316. *yuv2packed1 = yuv2rgb12_1_c;
  317. *yuv2packed2 = yuv2rgb12_2_c;
  318. *yuv2packedX = yuv2rgb12_X_c;
  319. break;
  320. case AV_PIX_FMT_RGB8:
  321. case AV_PIX_FMT_BGR8:
  322. *yuv2packed1 = yuv2rgb8_1_c;
  323. *yuv2packed2 = yuv2rgb8_2_c;
  324. *yuv2packedX = yuv2rgb8_X_c;
  325. break;
  326. case AV_PIX_FMT_RGB4:
  327. case AV_PIX_FMT_BGR4:
  328. *yuv2packed1 = yuv2rgb4_1_c;
  329. *yuv2packed2 = yuv2rgb4_2_c;
  330. *yuv2packedX = yuv2rgb4_X_c;
  331. break;
  332. case AV_PIX_FMT_RGB4_BYTE:
  333. case AV_PIX_FMT_BGR4_BYTE:
  334. *yuv2packed1 = yuv2rgb4b_1_c;
  335. *yuv2packed2 = yuv2rgb4b_2_c;
  336. *yuv2packedX = yuv2rgb4b_X_c;
  337. break;
  338. }
  339. }
  340. switch (dstFormat) {
  341. case AV_PIX_FMT_MONOWHITE:
  342. *yuv2packed1 = yuv2monowhite_1_c;
  343. *yuv2packed2 = yuv2monowhite_2_c;
  344. *yuv2packedX = yuv2monowhite_X_c;
  345. break;
  346. case AV_PIX_FMT_MONOBLACK:
  347. *yuv2packed1 = yuv2monoblack_1_c;
  348. *yuv2packed2 = yuv2monoblack_2_c;
  349. *yuv2packedX = yuv2monoblack_X_c;
  350. break;
  351. case AV_PIX_FMT_YUYV422:
  352. *yuv2packed1 = yuv2yuyv422_1_c;
  353. *yuv2packed2 = yuv2yuyv422_2_c;
  354. *yuv2packedX = yuv2yuyv422_X_c;
  355. break;
  356. case AV_PIX_FMT_YVYU422:
  357. *yuv2packed1 = yuv2yvyu422_1_c;
  358. *yuv2packed2 = yuv2yvyu422_2_c;
  359. *yuv2packedX = yuv2yvyu422_X_c;
  360. break;
  361. case AV_PIX_FMT_UYVY422:
  362. *yuv2packed1 = yuv2uyvy422_1_c;
  363. *yuv2packed2 = yuv2uyvy422_2_c;
  364. *yuv2packedX = yuv2uyvy422_X_c;
  365. break;
  366. }
  367. }

ff_sws_init_output_funcs()根据输出像素格式的不同,对以下几个函数指针进行赋值:

yuv2plane1:是yuv2planar1_fn类型的函数指针。该函数用于输出一行水平拉伸后的planar格式数据。数据没有使用垂直拉伸。
yuv2planeX:是yuv2planarX_fn类型的函数指针。该函数用于输出一行水平拉伸后的planar格式数据。数据使用垂直拉伸。
yuv2packed1:是yuv2packed1_fn类型的函数指针。该函数用于输出一行水平拉伸后的packed格式数据。数据没有使用垂直拉伸。
yuv2packed2:是yuv2packed2_fn类型的函数指针。该函数用于输出一行水平拉伸后的packed格式数据。数据使用两行数据进行垂直拉伸。
yuv2packedX:是yuv2packedX_fn类型的函数指针。该函数用于输出一行水平拉伸后的packed格式数据。数据使用垂直拉伸。
yuv2nv12cX:是yuv2interleavedX_fn类型的函数指针。还没有研究该函数。
yuv2anyX:是yuv2anyX_fn类型的函数指针。还没有研究该函数。

ff_sws_init_input_funcs()

ff_sws_init_input_funcs()用于初始化“输入函数”。“输入函数”在libswscale中的作用就是任意格式的像素转换为YUV格式以供后续的处理。ff_sws_init_input_funcs()的定义位于libswscale\input.c,如下所示。

[cpp] view plaincopy
  1. av_cold void ff_sws_init_input_funcs(SwsContext *c)
  2. {
  3. enum AVPixelFormat srcFormat = c->srcFormat;
  4. c->chrToYV12 = NULL;
  5. switch (srcFormat) {
  6. case AV_PIX_FMT_YUYV422:
  7. c->chrToYV12 = yuy2ToUV_c;
  8. break;
  9. case AV_PIX_FMT_YVYU422:
  10. c->chrToYV12 = yvy2ToUV_c;
  11. break;
  12. case AV_PIX_FMT_UYVY422:
  13. c->chrToYV12 = uyvyToUV_c;
  14. break;
  15. case AV_PIX_FMT_NV12:
  16. c->chrToYV12 = nv12ToUV_c;
  17. break;
  18. case AV_PIX_FMT_NV21:
  19. c->chrToYV12 = nv21ToUV_c;
  20. break;
  21. case AV_PIX_FMT_RGB8:
  22. case AV_PIX_FMT_BGR8:
  23. case AV_PIX_FMT_PAL8:
  24. case AV_PIX_FMT_BGR4_BYTE:
  25. case AV_PIX_FMT_RGB4_BYTE:
  26. c->chrToYV12 = palToUV_c;
  27. break;
  28. case AV_PIX_FMT_GBRP9LE:
  29. c->readChrPlanar = planar_rgb9le_to_uv;
  30. break;
  31. case AV_PIX_FMT_GBRP10LE:
  32. c->readChrPlanar = planar_rgb10le_to_uv;
  33. break;
  34. case AV_PIX_FMT_GBRP12LE:
  35. c->readChrPlanar = planar_rgb12le_to_uv;
  36. break;
  37. case AV_PIX_FMT_GBRP14LE:
  38. c->readChrPlanar = planar_rgb14le_to_uv;
  39. break;
  40. case AV_PIX_FMT_GBRAP16LE:
  41. case AV_PIX_FMT_GBRP16LE:
  42. c->readChrPlanar = planar_rgb16le_to_uv;
  43. break;
  44. case AV_PIX_FMT_GBRP9BE:
  45. c->readChrPlanar = planar_rgb9be_to_uv;
  46. break;
  47. case AV_PIX_FMT_GBRP10BE:
  48. c->readChrPlanar = planar_rgb10be_to_uv;
  49. break;
  50. case AV_PIX_FMT_GBRP12BE:
  51. c->readChrPlanar = planar_rgb12be_to_uv;
  52. break;
  53. case AV_PIX_FMT_GBRP14BE:
  54. c->readChrPlanar = planar_rgb14be_to_uv;
  55. break;
  56. case AV_PIX_FMT_GBRAP16BE:
  57. case AV_PIX_FMT_GBRP16BE:
  58. c->readChrPlanar = planar_rgb16be_to_uv;
  59. break;
  60. case AV_PIX_FMT_GBRAP:
  61. case AV_PIX_FMT_GBRP:
  62. c->readChrPlanar = planar_rgb_to_uv;
  63. break;
  64. #if HAVE_BIGENDIAN
  65. case AV_PIX_FMT_YUV444P9LE:
  66. case AV_PIX_FMT_YUV422P9LE:
  67. case AV_PIX_FMT_YUV420P9LE:
  68. case AV_PIX_FMT_YUV422P10LE:
  69. case AV_PIX_FMT_YUV444P10LE:
  70. case AV_PIX_FMT_YUV420P10LE:
  71. case AV_PIX_FMT_YUV422P12LE:
  72. case AV_PIX_FMT_YUV444P12LE:
  73. case AV_PIX_FMT_YUV420P12LE:
  74. case AV_PIX_FMT_YUV422P14LE:
  75. case AV_PIX_FMT_YUV444P14LE:
  76. case AV_PIX_FMT_YUV420P14LE:
  77. case AV_PIX_FMT_YUV420P16LE:
  78. case AV_PIX_FMT_YUV422P16LE:
  79. case AV_PIX_FMT_YUV444P16LE:
  80. case AV_PIX_FMT_YUVA444P9LE:
  81. case AV_PIX_FMT_YUVA422P9LE:
  82. case AV_PIX_FMT_YUVA420P9LE:
  83. case AV_PIX_FMT_YUVA444P10LE:
  84. case AV_PIX_FMT_YUVA422P10LE:
  85. case AV_PIX_FMT_YUVA420P10LE:
  86. case AV_PIX_FMT_YUVA420P16LE:
  87. case AV_PIX_FMT_YUVA422P16LE:
  88. case AV_PIX_FMT_YUVA444P16LE:
  89. c->chrToYV12 = bswap16UV_c;
  90. break;
  91. #else
  92. case AV_PIX_FMT_YUV444P9BE:
  93. case AV_PIX_FMT_YUV422P9BE:
  94. case AV_PIX_FMT_YUV420P9BE:
  95. case AV_PIX_FMT_YUV444P10BE:
  96. case AV_PIX_FMT_YUV422P10BE:
  97. case AV_PIX_FMT_YUV420P10BE:
  98. case AV_PIX_FMT_YUV444P12BE:
  99. case AV_PIX_FMT_YUV422P12BE:
  100. case AV_PIX_FMT_YUV420P12BE:
  101. case AV_PIX_FMT_YUV444P14BE:
  102. case AV_PIX_FMT_YUV422P14BE:
  103. case AV_PIX_FMT_YUV420P14BE:
  104. case AV_PIX_FMT_YUV420P16BE:
  105. case AV_PIX_FMT_YUV422P16BE:
  106. case AV_PIX_FMT_YUV444P16BE:
  107. case AV_PIX_FMT_YUVA444P9BE:
  108. case AV_PIX_FMT_YUVA422P9BE:
  109. case AV_PIX_FMT_YUVA420P9BE:
  110. case AV_PIX_FMT_YUVA444P10BE:
  111. case AV_PIX_FMT_YUVA422P10BE:
  112. case AV_PIX_FMT_YUVA420P10BE:
  113. case AV_PIX_FMT_YUVA420P16BE:
  114. case AV_PIX_FMT_YUVA422P16BE:
  115. case AV_PIX_FMT_YUVA444P16BE:
  116. c->chrToYV12 = bswap16UV_c;
  117. break;
  118. #endif
  119. }
  120. if (c->chrSrcHSubSample) {
  121. switch (srcFormat) {
  122. case AV_PIX_FMT_RGBA64BE:
  123. c->chrToYV12 = rgb64BEToUV_half_c;
  124. break;
  125. case AV_PIX_FMT_RGBA64LE:
  126. c->chrToYV12 = rgb64LEToUV_half_c;
  127. break;
  128. case AV_PIX_FMT_BGRA64BE:
  129. c->chrToYV12 = bgr64BEToUV_half_c;
  130. break;
  131. case AV_PIX_FMT_BGRA64LE:
  132. c->chrToYV12 = bgr64LEToUV_half_c;
  133. break;
  134. case AV_PIX_FMT_RGB48BE:
  135. c->chrToYV12 = rgb48BEToUV_half_c;
  136. break;
  137. case AV_PIX_FMT_RGB48LE:
  138. c->chrToYV12 = rgb48LEToUV_half_c;
  139. break;
  140. case AV_PIX_FMT_BGR48BE:
  141. c->chrToYV12 = bgr48BEToUV_half_c;
  142. break;
  143. case AV_PIX_FMT_BGR48LE:
  144. c->chrToYV12 = bgr48LEToUV_half_c;
  145. break;
  146. case AV_PIX_FMT_RGB32:
  147. c->chrToYV12 = bgr32ToUV_half_c;
  148. break;
  149. case AV_PIX_FMT_RGB32_1:
  150. c->chrToYV12 = bgr321ToUV_half_c;
  151. break;
  152. case AV_PIX_FMT_BGR24:
  153. c->chrToYV12 = bgr24ToUV_half_c;
  154. break;
  155. case AV_PIX_FMT_BGR565LE:
  156. c->chrToYV12 = bgr16leToUV_half_c;
  157. break;
  158. case AV_PIX_FMT_BGR565BE:
  159. c->chrToYV12 = bgr16beToUV_half_c;
  160. break;
  161. case AV_PIX_FMT_BGR555LE:
  162. c->chrToYV12 = bgr15leToUV_half_c;
  163. break;
  164. case AV_PIX_FMT_BGR555BE:
  165. c->chrToYV12 = bgr15beToUV_half_c;
  166. break;
  167. case AV_PIX_FMT_GBRAP:
  168. case AV_PIX_FMT_GBRP:
  169. c->chrToYV12 = gbr24pToUV_half_c;
  170. break;
  171. case AV_PIX_FMT_BGR444LE:
  172. c->chrToYV12 = bgr12leToUV_half_c;
  173. break;
  174. case AV_PIX_FMT_BGR444BE:
  175. c->chrToYV12 = bgr12beToUV_half_c;
  176. break;
  177. case AV_PIX_FMT_BGR32:
  178. c->chrToYV12 = rgb32ToUV_half_c;
  179. break;
  180. case AV_PIX_FMT_BGR32_1:
  181. c->chrToYV12 = rgb321ToUV_half_c;
  182. break;
  183. case AV_PIX_FMT_RGB24:
  184. c->chrToYV12 = rgb24ToUV_half_c;
  185. break;
  186. case AV_PIX_FMT_RGB565LE:
  187. c->chrToYV12 = rgb16leToUV_half_c;
  188. break;
  189. case AV_PIX_FMT_RGB565BE:
  190. c->chrToYV12 = rgb16beToUV_half_c;
  191. break;
  192. case AV_PIX_FMT_RGB555LE:
  193. c->chrToYV12 = rgb15leToUV_half_c;
  194. break;
  195. case AV_PIX_FMT_RGB555BE:
  196. c->chrToYV12 = rgb15beToUV_half_c;
  197. break;
  198. case AV_PIX_FMT_RGB444LE:
  199. c->chrToYV12 = rgb12leToUV_half_c;
  200. break;
  201. case AV_PIX_FMT_RGB444BE:
  202. c->chrToYV12 = rgb12beToUV_half_c;
  203. break;
  204. }
  205. } else {
  206. switch (srcFormat) {
  207. case AV_PIX_FMT_RGBA64BE:
  208. c->chrToYV12 = rgb64BEToUV_c;
  209. break;
  210. case AV_PIX_FMT_RGBA64LE:
  211. c->chrToYV12 = rgb64LEToUV_c;
  212. break;
  213. case AV_PIX_FMT_BGRA64BE:
  214. c->chrToYV12 = bgr64BEToUV_c;
  215. break;
  216. case AV_PIX_FMT_BGRA64LE:
  217. c->chrToYV12 = bgr64LEToUV_c;
  218. break;
  219. case AV_PIX_FMT_RGB48BE:
  220. c->chrToYV12 = rgb48BEToUV_c;
  221. break;
  222. case AV_PIX_FMT_RGB48LE:
  223. c->chrToYV12 = rgb48LEToUV_c;
  224. break;
  225. case AV_PIX_FMT_BGR48BE:
  226. c->chrToYV12 = bgr48BEToUV_c;
  227. break;
  228. case AV_PIX_FMT_BGR48LE:
  229. c->chrToYV12 = bgr48LEToUV_c;
  230. break;
  231. case AV_PIX_FMT_RGB32:
  232. c->chrToYV12 = bgr32ToUV_c;
  233. break;
  234. case AV_PIX_FMT_RGB32_1:
  235. c->chrToYV12 = bgr321ToUV_c;
  236. break;
  237. case AV_PIX_FMT_BGR24:
  238. c->chrToYV12 = bgr24ToUV_c;
  239. break;
  240. case AV_PIX_FMT_BGR565LE:
  241. c->chrToYV12 = bgr16leToUV_c;
  242. break;
  243. case AV_PIX_FMT_BGR565BE:
  244. c->chrToYV12 = bgr16beToUV_c;
  245. break;
  246. case AV_PIX_FMT_BGR555LE:
  247. c->chrToYV12 = bgr15leToUV_c;
  248. break;
  249. case AV_PIX_FMT_BGR555BE:
  250. c->chrToYV12 = bgr15beToUV_c;
  251. break;
  252. case AV_PIX_FMT_BGR444LE:
  253. c->chrToYV12 = bgr12leToUV_c;
  254. break;
  255. case AV_PIX_FMT_BGR444BE:
  256. c->chrToYV12 = bgr12beToUV_c;
  257. break;
  258. case AV_PIX_FMT_BGR32:
  259. c->chrToYV12 = rgb32ToUV_c;
  260. break;
  261. case AV_PIX_FMT_BGR32_1:
  262. c->chrToYV12 = rgb321ToUV_c;
  263. break;
  264. case AV_PIX_FMT_RGB24:
  265. c->chrToYV12 = rgb24ToUV_c;
  266. break;
  267. case AV_PIX_FMT_RGB565LE:
  268. c->chrToYV12 = rgb16leToUV_c;
  269. break;
  270. case AV_PIX_FMT_RGB565BE:
  271. c->chrToYV12 = rgb16beToUV_c;
  272. break;
  273. case AV_PIX_FMT_RGB555LE:
  274. c->chrToYV12 = rgb15leToUV_c;
  275. break;
  276. case AV_PIX_FMT_RGB555BE:
  277. c->chrToYV12 = rgb15beToUV_c;
  278. break;
  279. case AV_PIX_FMT_RGB444LE:
  280. c->chrToYV12 = rgb12leToUV_c;
  281. break;
  282. case AV_PIX_FMT_RGB444BE:
  283. c->chrToYV12 = rgb12beToUV_c;
  284. break;
  285. }
  286. }
  287. c->lumToYV12 = NULL;
  288. c->alpToYV12 = NULL;
  289. switch (srcFormat) {
  290. case AV_PIX_FMT_GBRP9LE:
  291. c->readLumPlanar = planar_rgb9le_to_y;
  292. break;
  293. case AV_PIX_FMT_GBRP10LE:
  294. c->readLumPlanar = planar_rgb10le_to_y;
  295. break;
  296. case AV_PIX_FMT_GBRP12LE:
  297. c->readLumPlanar = planar_rgb12le_to_y;
  298. break;
  299. case AV_PIX_FMT_GBRP14LE:
  300. c->readLumPlanar = planar_rgb14le_to_y;
  301. break;
  302. case AV_PIX_FMT_GBRAP16LE:
  303. case AV_PIX_FMT_GBRP16LE:
  304. c->readLumPlanar = planar_rgb16le_to_y;
  305. break;
  306. case AV_PIX_FMT_GBRP9BE:
  307. c->readLumPlanar = planar_rgb9be_to_y;
  308. break;
  309. case AV_PIX_FMT_GBRP10BE:
  310. c->readLumPlanar = planar_rgb10be_to_y;
  311. break;
  312. case AV_PIX_FMT_GBRP12BE:
  313. c->readLumPlanar = planar_rgb12be_to_y;
  314. break;
  315. case AV_PIX_FMT_GBRP14BE:
  316. c->readLumPlanar = planar_rgb14be_to_y;
  317. break;
  318. case AV_PIX_FMT_GBRAP16BE:
  319. case AV_PIX_FMT_GBRP16BE:
  320. c->readLumPlanar = planar_rgb16be_to_y;
  321. break;
  322. case AV_PIX_FMT_GBRAP:
  323. c->readAlpPlanar = planar_rgb_to_a;
  324. case AV_PIX_FMT_GBRP:
  325. c->readLumPlanar = planar_rgb_to_y;
  326. break;
  327. #if HAVE_BIGENDIAN
  328. case AV_PIX_FMT_YUV444P9LE:
  329. case AV_PIX_FMT_YUV422P9LE:
  330. case AV_PIX_FMT_YUV420P9LE:
  331. case AV_PIX_FMT_YUV444P10LE:
  332. case AV_PIX_FMT_YUV422P10LE:
  333. case AV_PIX_FMT_YUV420P10LE:
  334. case AV_PIX_FMT_YUV444P12LE:
  335. case AV_PIX_FMT_YUV422P12LE:
  336. case AV_PIX_FMT_YUV420P12LE:
  337. case AV_PIX_FMT_YUV444P14LE:
  338. case AV_PIX_FMT_YUV422P14LE:
  339. case AV_PIX_FMT_YUV420P14LE:
  340. case AV_PIX_FMT_YUV420P16LE:
  341. case AV_PIX_FMT_YUV422P16LE:
  342. case AV_PIX_FMT_YUV444P16LE:
  343. case AV_PIX_FMT_GRAY16LE:
  344. c->lumToYV12 = bswap16Y_c;
  345. break;
  346. case AV_PIX_FMT_YUVA444P9LE:
  347. case AV_PIX_FMT_YUVA422P9LE:
  348. case AV_PIX_FMT_YUVA420P9LE:
  349. case AV_PIX_FMT_YUVA444P10LE:
  350. case AV_PIX_FMT_YUVA422P10LE:
  351. case AV_PIX_FMT_YUVA420P10LE:
  352. case AV_PIX_FMT_YUVA420P16LE:
  353. case AV_PIX_FMT_YUVA422P16LE:
  354. case AV_PIX_FMT_YUVA444P16LE:
  355. c->lumToYV12 = bswap16Y_c;
  356. c->alpToYV12 = bswap16Y_c;
  357. break;
  358. #else
  359. case AV_PIX_FMT_YUV444P9BE:
  360. case AV_PIX_FMT_YUV422P9BE:
  361. case AV_PIX_FMT_YUV420P9BE:
  362. case AV_PIX_FMT_YUV444P10BE:
  363. case AV_PIX_FMT_YUV422P10BE:
  364. case AV_PIX_FMT_YUV420P10BE:
  365. case AV_PIX_FMT_YUV444P12BE:
  366. case AV_PIX_FMT_YUV422P12BE:
  367. case AV_PIX_FMT_YUV420P12BE:
  368. case AV_PIX_FMT_YUV444P14BE:
  369. case AV_PIX_FMT_YUV422P14BE:
  370. case AV_PIX_FMT_YUV420P14BE:
  371. case AV_PIX_FMT_YUV420P16BE:
  372. case AV_PIX_FMT_YUV422P16BE:
  373. case AV_PIX_FMT_YUV444P16BE:
  374. case AV_PIX_FMT_GRAY16BE:
  375. c->lumToYV12 = bswap16Y_c;
  376. break;
  377. case AV_PIX_FMT_YUVA444P9BE:
  378. case AV_PIX_FMT_YUVA422P9BE:
  379. case AV_PIX_FMT_YUVA420P9BE:
  380. case AV_PIX_FMT_YUVA444P10BE:
  381. case AV_PIX_FMT_YUVA422P10BE:
  382. case AV_PIX_FMT_YUVA420P10BE:
  383. case AV_PIX_FMT_YUVA420P16BE:
  384. case AV_PIX_FMT_YUVA422P16BE:
  385. case AV_PIX_FMT_YUVA444P16BE:
  386. c->lumToYV12 = bswap16Y_c;
  387. c->alpToYV12 = bswap16Y_c;
  388. break;
  389. #endif
  390. case AV_PIX_FMT_YA16LE:
  391. c->lumToYV12 = read_ya16le_gray_c;
  392. c->alpToYV12 = read_ya16le_alpha_c;
  393. break;
  394. case AV_PIX_FMT_YA16BE:
  395. c->lumToYV12 = read_ya16be_gray_c;
  396. c->alpToYV12 = read_ya16be_alpha_c;
  397. break;
  398. case AV_PIX_FMT_YUYV422:
  399. case AV_PIX_FMT_YVYU422:
  400. case AV_PIX_FMT_YA8:
  401. c->lumToYV12 = yuy2ToY_c;
  402. break;
  403. case AV_PIX_FMT_UYVY422:
  404. c->lumToYV12 = uyvyToY_c;
  405. break;
  406. case AV_PIX_FMT_BGR24:
  407. c->lumToYV12 = bgr24ToY_c;
  408. break;
  409. case AV_PIX_FMT_BGR565LE:
  410. c->lumToYV12 = bgr16leToY_c;
  411. break;
  412. case AV_PIX_FMT_BGR565BE:
  413. c->lumToYV12 = bgr16beToY_c;
  414. break;
  415. case AV_PIX_FMT_BGR555LE:
  416. c->lumToYV12 = bgr15leToY_c;
  417. break;
  418. case AV_PIX_FMT_BGR555BE:
  419. c->lumToYV12 = bgr15beToY_c;
  420. break;
  421. case AV_PIX_FMT_BGR444LE:
  422. c->lumToYV12 = bgr12leToY_c;
  423. break;
  424. case AV_PIX_FMT_BGR444BE:
  425. c->lumToYV12 = bgr12beToY_c;
  426. break;
  427. case AV_PIX_FMT_RGB24:
  428. c->lumToYV12 = rgb24ToY_c;
  429. break;
  430. case AV_PIX_FMT_RGB565LE:
  431. c->lumToYV12 = rgb16leToY_c;
  432. break;
  433. case AV_PIX_FMT_RGB565BE:
  434. c->lumToYV12 = rgb16beToY_c;
  435. break;
  436. case AV_PIX_FMT_RGB555LE:
  437. c->lumToYV12 = rgb15leToY_c;
  438. break;
  439. case AV_PIX_FMT_RGB555BE:
  440. c->lumToYV12 = rgb15beToY_c;
  441. break;
  442. case AV_PIX_FMT_RGB444LE:
  443. c->lumToYV12 = rgb12leToY_c;
  444. break;
  445. case AV_PIX_FMT_RGB444BE:
  446. c->lumToYV12 = rgb12beToY_c;
  447. break;
  448. case AV_PIX_FMT_RGB8:
  449. case AV_PIX_FMT_BGR8:
  450. case AV_PIX_FMT_PAL8:
  451. case AV_PIX_FMT_BGR4_BYTE:
  452. case AV_PIX_FMT_RGB4_BYTE:
  453. c->lumToYV12 = palToY_c;
  454. break;
  455. case AV_PIX_FMT_MONOBLACK:
  456. c->lumToYV12 = monoblack2Y_c;
  457. break;
  458. case AV_PIX_FMT_MONOWHITE:
  459. c->lumToYV12 = monowhite2Y_c;
  460. break;
  461. case AV_PIX_FMT_RGB32:
  462. c->lumToYV12 = bgr32ToY_c;
  463. break;
  464. case AV_PIX_FMT_RGB32_1:
  465. c->lumToYV12 = bgr321ToY_c;
  466. break;
  467. case AV_PIX_FMT_BGR32:
  468. c->lumToYV12 = rgb32ToY_c;
  469. break;
  470. case AV_PIX_FMT_BGR32_1:
  471. c->lumToYV12 = rgb321ToY_c;
  472. break;
  473. case AV_PIX_FMT_RGB48BE:
  474. c->lumToYV12 = rgb48BEToY_c;
  475. break;
  476. case AV_PIX_FMT_RGB48LE:
  477. c->lumToYV12 = rgb48LEToY_c;
  478. break;
  479. case AV_PIX_FMT_BGR48BE:
  480. c->lumToYV12 = bgr48BEToY_c;
  481. break;
  482. case AV_PIX_FMT_BGR48LE:
  483. c->lumToYV12 = bgr48LEToY_c;
  484. break;
  485. case AV_PIX_FMT_RGBA64BE:
  486. c->lumToYV12 = rgb64BEToY_c;
  487. break;
  488. case AV_PIX_FMT_RGBA64LE:
  489. c->lumToYV12 = rgb64LEToY_c;
  490. break;
  491. case AV_PIX_FMT_BGRA64BE:
  492. c->lumToYV12 = bgr64BEToY_c;
  493. break;
  494. case AV_PIX_FMT_BGRA64LE:
  495. c->lumToYV12 = bgr64LEToY_c;
  496. }
  497. if (c->alpPixBuf) {
  498. if (is16BPS(srcFormat) || isNBPS(srcFormat)) {
  499. if (HAVE_BIGENDIAN == !isBE(srcFormat))
  500. c->alpToYV12 = bswap16Y_c;
  501. }
  502. switch (srcFormat) {
  503. case AV_PIX_FMT_BGRA64LE:
  504. case AV_PIX_FMT_BGRA64BE:
  505. case AV_PIX_FMT_RGBA64LE:
  506. case AV_PIX_FMT_RGBA64BE:  c->alpToYV12 = rgba64ToA_c; break;
  507. case AV_PIX_FMT_BGRA:
  508. case AV_PIX_FMT_RGBA:
  509. c->alpToYV12 = rgbaToA_c;
  510. break;
  511. case AV_PIX_FMT_ABGR:
  512. case AV_PIX_FMT_ARGB:
  513. c->alpToYV12 = abgrToA_c;
  514. break;
  515. case AV_PIX_FMT_YA8:
  516. c->alpToYV12 = uyvyToY_c;
  517. break;
  518. case AV_PIX_FMT_PAL8 :
  519. c->alpToYV12 = palToA_c;
  520. break;
  521. }
  522. }
  523. }

ff_sws_init_input_funcs()根据输入像素格式的不同,对以下几个函数指针进行赋值:

lumToYV12:转换得到Y分量。
chrToYV12:转换得到UV分量。
alpToYV12:转换得到Alpha分量。
readLumPlanar:读取planar格式的数据转换为Y。
readChrPlanar:读取planar格式的数据转换为UV。

下面看几个例子。

当输入像素格式为AV_PIX_FMT_RGB24的时候,lumToYV12()指针指向的函数是rgb24ToY_c(),如下所示。

[cpp] view plaincopy
  1. case AV_PIX_FMT_RGB24:
  2. c->lumToYV12 = rgb24ToY_c;
  3. break;

rgb24ToY_c()

rgb24ToY_c()的定义如下。

[cpp] view plaincopy
  1. static void rgb24ToY_c(uint8_t *_dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
  2. uint32_t *rgb2yuv)
  3. {
  4. int16_t *dst = (int16_t *)_dst;
  5. int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
  6. int i;
  7. for (i = 0; i < width; i++) {
  8. int r = src[i * 3 + 0];
  9. int g = src[i * 3 + 1];
  10. int b = src[i * 3 + 2];
  11. dst[i] = ((ry*r + gy*g + by*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  12. }
  13. }

从源代码中可以看出,该函数主要完成了以下三步:

1. 取系数。通过读取rgb2yuv数组中存储的参数获得R,G,B每个分量的系数。
2. 取像素值。分别读取R,G,B每个分量的像素值。
3. 计算得到亮度值。根据R,G,B的系数和值,计算得到亮度值Y。

当输入像素格式为AV_PIX_FMT_RGB24的时候,chrToYV12 ()指针指向的函数是rgb24ToUV_half_c(),如下所示。

[cpp] view plaincopy
  1. case AV_PIX_FMT_RGB24:
  2. c->chrToYV12 = rgb24ToUV_half_c;
  3. break;

rgb24ToUV_half_c()

rgb24ToUV_half_c()定义如下。

[cpp] view plaincopy
  1. static void rgb24ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *src1,
  2. const uint8_t *src2, int width, uint32_t *rgb2yuv)
  3. {
  4. int16_t *dstU = (int16_t *)_dstU;
  5. int16_t *dstV = (int16_t *)_dstV;
  6. int i;
  7. int32_t ru = rgb2yuv[RU_IDX], gu = rgb2yuv[GU_IDX], bu = rgb2yuv[BU_IDX];
  8. int32_t rv = rgb2yuv[RV_IDX], gv = rgb2yuv[GV_IDX], bv = rgb2yuv[BV_IDX];
  9. av_assert1(src1 == src2);
  10. for (i = 0; i < width; i++) {
  11. int r = src1[6 * i + 0] + src1[6 * i + 3];
  12. int g = src1[6 * i + 1] + src1[6 * i + 4];
  13. int b = src1[6 * i + 2] + src1[6 * i + 5];
  14. dstU[i] = (ru*r + gu*g + bu*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  15. dstV[i] = (rv*r + gv*g + bv*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  16. }
  17. }

rgb24ToUV_half_c()的过程相比rgb24ToY_c()要稍微复杂些。这主要是因为U,V取值的数量只有Y的一半。因此需要首先求出每2个像素点的平均值之后,再进行计算。
当输入像素格式为AV_PIX_FMT_GBRP(注意这个是planar格式,三个分量分别为G,B,R)的时候,readLumPlanar指向的函数是planar_rgb_to_y(),如下所示。

[cpp] view plaincopy
  1. case AV_PIX_FMT_GBRP:
  2. c->readLumPlanar = planar_rgb_to_y;
  3. break;

planar_rgb_to_y()

planar_rgb_to_y()定义如下。

[cpp] view plaincopy
  1. static void planar_rgb_to_y(uint8_t *_dst, const uint8_t *src[4], int width, int32_t *rgb2yuv)
  2. {
  3. uint16_t *dst = (uint16_t *)_dst;
  4. int32_t ry = rgb2yuv[RY_IDX], gy = rgb2yuv[GY_IDX], by = rgb2yuv[BY_IDX];
  5. int i;
  6. for (i = 0; i < width; i++) {
  7. int g = src[0][i];
  8. int b = src[1][i];
  9. int r = src[2][i];
  10. dst[i] = (ry*r + gy*g + by*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
  11. }
  12. }

可以看出处理planar格式的GBR数据和处理packed格式的RGB数据的方法是基本一样的,在这里不再重复。

ff_sws_init_range_convert()

ff_sws_init_range_convert()用于初始化像素值范围转换的函数,它的定义位于libswscale\swscale.c,如下所示。

[cpp] view plaincopy
  1. av_cold void ff_sws_init_range_convert(SwsContext *c)
  2. {
  3. c->lumConvertRange = NULL;
  4. c->chrConvertRange = NULL;
  5. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  6. if (c->dstBpc <= 14) {
  7. if (c->srcRange) {
  8. c->lumConvertRange = lumRangeFromJpeg_c;
  9. c->chrConvertRange = chrRangeFromJpeg_c;
  10. } else {
  11. c->lumConvertRange = lumRangeToJpeg_c;
  12. c->chrConvertRange = chrRangeToJpeg_c;
  13. }
  14. } else {
  15. if (c->srcRange) {
  16. c->lumConvertRange = lumRangeFromJpeg16_c;
  17. c->chrConvertRange = chrRangeFromJpeg16_c;
  18. } else {
  19. c->lumConvertRange = lumRangeToJpeg16_c;
  20. c->chrConvertRange = chrRangeToJpeg16_c;
  21. }
  22. }
  23. }
  24. }

ff_sws_init_range_convert()包含了两种像素取值范围的转换:

lumConvertRange:亮度分量取值范围的转换。
chrConvertRange:色度分量取值范围的转换。

从JPEG标准转换为MPEG标准的函数有:lumRangeFromJpeg_c()和chrRangeFromJpeg_c()。

lumRangeFromJpeg_c()

亮度转换(0-255转换为16-235)函数lumRangeFromJpeg_c()如下所示。

[cpp] view plaincopy
  1. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  2. {
  3. int i;
  4. for (i = 0; i < width; i++)
  5. dst[i] = (dst[i] * 14071 + 33561947) >> 14;
  6. }

可以简单代入一个数字验证一下上述函数的正确性。该函数将亮度值“0”映射成“16”,“255”映射成“235”,因此我们可以代入一个“255”看看转换后的数值是否为“235”。在这里需要注意,dst中存储的像素数值是15bit的亮度值。因此我们需要将8bit的数值“255”左移7位后带入。经过计算,255左移7位后取值为32640,计算后得到的数值为30080,右移7位后得到的8bit亮度值即为235。

后续几个函数都可以用上面描述的方法进行验证,就不再重复了。

chrRangeFromJpeg_c()

色度转换(0-255转换为16-240)函数chrRangeFromJpeg_c()如下所示。

[cpp] view plaincopy
  1. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  2. {
  3. int i;
  4. for (i = 0; i < width; i++) {
  5. dstU[i] = (dstU[i] * 1799 + 4081085) >> 11; // 1469
  6. dstV[i] = (dstV[i] * 1799 + 4081085) >> 11; // 1469
  7. }
  8. }

从MPEG标准转换为JPEG标准的函数有:lumRangeToJpeg_c()和chrRangeToJpeg_c()。

lumRangeToJpeg_c()

亮度转换(16-235转换为0-255)函数lumRangeToJpeg_c()定义如下所示。

[cpp] view plaincopy
  1. static void lumRangeToJpeg_c(int16_t *dst, int width)
  2. {
  3. int i;
  4. for (i = 0; i < width; i++)
  5. dst[i] = (FFMIN(dst[i], 30189) * 19077 - 39057361) >> 14;
  6. }

chrRangeToJpeg_c()

色度转换(16-240转换为0-255)函数chrRangeToJpeg_c()定义如下所示。

[cpp] view plaincopy
  1. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  2. {
  3. int i;
  4. for (i = 0; i < width; i++) {
  5. dstU[i] = (FFMIN(dstU[i], 30775) * 4663 - 9289992) >> 12; // -264
  6. dstV[i] = (FFMIN(dstV[i], 30775) * 4663 - 9289992) >> 12; // -264
  7. }
  8. }

至此sws_getContext()的源代码就基本上分析完毕了。

雷霄骅
leixiaohua1020@126.com
http://blog.csdn.net/leixiaohua1020

颜色格式转换: FFmpeg源代码简单分析:libswscale的sws_getContext()相关推荐

  1. FFmpeg源代码简单分析:libswscale的sws_getContext()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  2. FFmpeg源代码简单分析-其他-libswscale的sws_getContext()

    参考链接 FFmpeg源代码简单分析:libswscale的sws_getContext()_雷霄骅的博客-CSDN博客 libswscale的sws_getContext() FFmpeg中类库li ...

  3. FFmpeg源代码简单分析:libswscale的sws_scale()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  4. FFmpeg源代码简单分析:configure

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  5. FFmpeg源代码简单分析:结构体成员管理系统-AVOption

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  6. FFmpeg源代码简单分析:日志输出系统(av_log()等)

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  7. FFmpeg源代码简单分析:avformat_write_header()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...

  8. FFmpeg源代码简单分析 日志输出系统(av log 等)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! ==== ...

  9. (转)FFmpeg源代码简单分析:avformat_open_input()

    目录(?)[+] ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结 ...

  10. FFmpeg源代码简单分析 configure

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! ==== ...

最新文章

  1. EditPlus集成Java编译和运行命令组建轻量级Java SE开发工具
  2. 复化梯形公式,Newton-Cotes公式,变量代换后的复化梯形公式,Gauss-Legendre公式,Gauss-Jacobi公式插值积分的精确度比较
  3. C++ Primer 5th笔记(chap 17 标准库特殊设施)tuple 返回多个值
  4. java对象复制到新对象_java – 使用新生成的ID将Hibernate复制对象值复制到新对象中...
  5. 赞!史上最全的互联网思维精髓总结
  6. 国家电网和南方电网还傻傻分不清?
  7. 十行代码实现网页标题滚动效果!
  8. 计算机少年宫辅导教师总结,微机兴趣小组活动总结
  9. fiber报错 (type *big.Int has no field or method FillBytes)
  10. qt连接mysql创建表_用Qt访问数据库写一个 表格
  11. 技术的好文章和烂文章
  12. win7系统蓝屏修复工具如何使用
  13. 系统学习深度学习(十六)--Overfeat
  14. 【Python】快速简单实现图像背景更换
  15. 大众点评爬取------分析成都必吃菜
  16. 捣鼓openwrt不死bootloader (1)
  17. JAVA实现Excel模板导入案例分析
  18. 算法合集之《信息学中守恒法的应用》(不错的文章保存一下)
  19. 足球点球 html5,疯狂的点球(5-1)
  20. 按键精灵定位坐标循环_LinkTrack UWB定位正式支持ROS机器人操作系统,驱动开源,自由定制消息格式...

热门文章

  1. go channel的用法总结
  2. docker部署time machine服务
  3. LayUI项目之我的会议(送审以及排座)
  4. 计算机网络中表征数据传输有效性的指标是,表征数据传输有效性的指标是
  5. 批量修改word文字字体字号
  6. 如何修复Windows 10中最烦人的东西
  7. 华为、海尔之后,阿里在全屋智能领域有新动作,这次牵手的是萤石
  8. 别被你的双眼所欺骗!100张神奇的视觉欺骗图
  9. B - Mountainous landscape Gym - 100543B(线段树+计算几何)
  10. BCrypt算法,想想spring security里的BCryptPasswordEncoder