You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

911 lines
40KB

  1. /*
  2. * Copyright (C) 2001-2011 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef SWSCALE_SWSCALE_INTERNAL_H
  21. #define SWSCALE_SWSCALE_INTERNAL_H
  22. #include "config.h"
  23. #if HAVE_ALTIVEC_H
  24. #include <altivec.h>
  25. #endif
  26. #include "version.h"
  27. #include "libavutil/avassert.h"
  28. #include "libavutil/avutil.h"
  29. #include "libavutil/common.h"
  30. #include "libavutil/intreadwrite.h"
  31. #include "libavutil/log.h"
  32. #include "libavutil/pixfmt.h"
  33. #include "libavutil/pixdesc.h"
  34. #define STR(s) AV_TOSTRING(s) // AV_STRINGIFY is too long
  35. #define YUVRGB_TABLE_HEADROOM 256
  36. #define MAX_FILTER_SIZE SWS_MAX_FILTER_SIZE
  37. #define DITHER1XBPP
  38. #if HAVE_BIGENDIAN
  39. #define ALT32_CORR (-1)
  40. #else
  41. #define ALT32_CORR 1
  42. #endif
  43. #if ARCH_X86_64
  44. # define APCK_PTR2 8
  45. # define APCK_COEF 16
  46. # define APCK_SIZE 24
  47. #else
  48. # define APCK_PTR2 4
  49. # define APCK_COEF 8
  50. # define APCK_SIZE 16
  51. #endif
  52. #define RETCODE_USE_CASCADE -12345
  53. struct SwsContext;
  54. typedef enum SwsDither {
  55. SWS_DITHER_NONE = 0,
  56. SWS_DITHER_AUTO,
  57. SWS_DITHER_BAYER,
  58. SWS_DITHER_ED,
  59. SWS_DITHER_A_DITHER,
  60. SWS_DITHER_X_DITHER,
  61. NB_SWS_DITHER,
  62. } SwsDither;
  63. typedef int (*SwsFunc)(struct SwsContext *context, const uint8_t *src[],
  64. int srcStride[], int srcSliceY, int srcSliceH,
  65. uint8_t *dst[], int dstStride[]);
  66. /**
  67. * Write one line of horizontally scaled data to planar output
  68. * without any additional vertical scaling (or point-scaling).
  69. *
  70. * @param src scaled source data, 15bit for 8-10bit output,
  71. * 19-bit for 16bit output (in int32_t)
  72. * @param dest pointer to the output plane. For >8bit
  73. * output, this is in uint16_t
  74. * @param dstW width of destination in pixels
  75. * @param dither ordered dither array of type int16_t and size 8
  76. * @param offset Dither offset
  77. */
  78. typedef void (*yuv2planar1_fn)(const int16_t *src, uint8_t *dest, int dstW,
  79. const uint8_t *dither, int offset);
  80. /**
  81. * Write one line of horizontally scaled data to planar output
  82. * with multi-point vertical scaling between input pixels.
  83. *
  84. * @param filter vertical luma/alpha scaling coefficients, 12bit [0,4096]
  85. * @param src scaled luma (Y) or alpha (A) source data, 15bit for 8-10bit output,
  86. * 19-bit for 16bit output (in int32_t)
  87. * @param filterSize number of vertical input lines to scale
  88. * @param dest pointer to output plane. For >8bit
  89. * output, this is in uint16_t
  90. * @param dstW width of destination pixels
  91. * @param offset Dither offset
  92. */
  93. typedef void (*yuv2planarX_fn)(const int16_t *filter, int filterSize,
  94. const int16_t **src, uint8_t *dest, int dstW,
  95. const uint8_t *dither, int offset);
  96. /**
  97. * Write one line of horizontally scaled chroma to interleaved output
  98. * with multi-point vertical scaling between input pixels.
  99. *
  100. * @param c SWS scaling context
  101. * @param chrFilter vertical chroma scaling coefficients, 12bit [0,4096]
  102. * @param chrUSrc scaled chroma (U) source data, 15bit for 8-10bit output,
  103. * 19-bit for 16bit output (in int32_t)
  104. * @param chrVSrc scaled chroma (V) source data, 15bit for 8-10bit output,
  105. * 19-bit for 16bit output (in int32_t)
  106. * @param chrFilterSize number of vertical chroma input lines to scale
  107. * @param dest pointer to the output plane. For >8bit
  108. * output, this is in uint16_t
  109. * @param dstW width of chroma planes
  110. */
  111. typedef void (*yuv2interleavedX_fn)(struct SwsContext *c,
  112. const int16_t *chrFilter,
  113. int chrFilterSize,
  114. const int16_t **chrUSrc,
  115. const int16_t **chrVSrc,
  116. uint8_t *dest, int dstW);
  117. /**
  118. * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
  119. * output without any additional vertical scaling (or point-scaling). Note
  120. * that this function may do chroma scaling, see the "uvalpha" argument.
  121. *
  122. * @param c SWS scaling context
  123. * @param lumSrc scaled luma (Y) source data, 15bit for 8-10bit output,
  124. * 19-bit for 16bit output (in int32_t)
  125. * @param chrUSrc scaled chroma (U) source data, 15bit for 8-10bit output,
  126. * 19-bit for 16bit output (in int32_t)
  127. * @param chrVSrc scaled chroma (V) source data, 15bit for 8-10bit output,
  128. * 19-bit for 16bit output (in int32_t)
  129. * @param alpSrc scaled alpha (A) source data, 15bit for 8-10bit output,
  130. * 19-bit for 16bit output (in int32_t)
  131. * @param dest pointer to the output plane. For 16bit output, this is
  132. * uint16_t
  133. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  134. * to write into dest[]
  135. * @param uvalpha chroma scaling coefficient for the second line of chroma
  136. * pixels, either 2048 or 0. If 0, one chroma input is used
  137. * for 2 output pixels (or if the SWS_FLAG_FULL_CHR_INT flag
  138. * is set, it generates 1 output pixel). If 2048, two chroma
  139. * input pixels should be averaged for 2 output pixels (this
  140. * only happens if SWS_FLAG_FULL_CHR_INT is not set)
  141. * @param y vertical line number for this output. This does not need
  142. * to be used to calculate the offset in the destination,
  143. * but can be used to generate comfort noise using dithering
  144. * for some output formats.
  145. */
  146. typedef void (*yuv2packed1_fn)(struct SwsContext *c, const int16_t *lumSrc,
  147. const int16_t *chrUSrc[2],
  148. const int16_t *chrVSrc[2],
  149. const int16_t *alpSrc, uint8_t *dest,
  150. int dstW, int uvalpha, int y);
  151. /**
  152. * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
  153. * output by doing bilinear scaling between two input lines.
  154. *
  155. * @param c SWS scaling context
  156. * @param lumSrc scaled luma (Y) source data, 15bit for 8-10bit output,
  157. * 19-bit for 16bit output (in int32_t)
  158. * @param chrUSrc scaled chroma (U) source data, 15bit for 8-10bit output,
  159. * 19-bit for 16bit output (in int32_t)
  160. * @param chrVSrc scaled chroma (V) source data, 15bit for 8-10bit output,
  161. * 19-bit for 16bit output (in int32_t)
  162. * @param alpSrc scaled alpha (A) source data, 15bit for 8-10bit output,
  163. * 19-bit for 16bit output (in int32_t)
  164. * @param dest pointer to the output plane. For 16bit output, this is
  165. * uint16_t
  166. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  167. * to write into dest[]
  168. * @param yalpha luma/alpha scaling coefficients for the second input line.
  169. * The first line's coefficients can be calculated by using
  170. * 4096 - yalpha
  171. * @param uvalpha chroma scaling coefficient for the second input line. The
  172. * first line's coefficients can be calculated by using
  173. * 4096 - uvalpha
  174. * @param y vertical line number for this output. This does not need
  175. * to be used to calculate the offset in the destination,
  176. * but can be used to generate comfort noise using dithering
  177. * for some output formats.
  178. */
  179. typedef void (*yuv2packed2_fn)(struct SwsContext *c, const int16_t *lumSrc[2],
  180. const int16_t *chrUSrc[2],
  181. const int16_t *chrVSrc[2],
  182. const int16_t *alpSrc[2],
  183. uint8_t *dest,
  184. int dstW, int yalpha, int uvalpha, int y);
  185. /**
  186. * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
  187. * output by doing multi-point vertical scaling between input pixels.
  188. *
  189. * @param c SWS scaling context
  190. * @param lumFilter vertical luma/alpha scaling coefficients, 12bit [0,4096]
  191. * @param lumSrc scaled luma (Y) source data, 15bit for 8-10bit output,
  192. * 19-bit for 16bit output (in int32_t)
  193. * @param lumFilterSize number of vertical luma/alpha input lines to scale
  194. * @param chrFilter vertical chroma scaling coefficients, 12bit [0,4096]
  195. * @param chrUSrc scaled chroma (U) source data, 15bit for 8-10bit output,
  196. * 19-bit for 16bit output (in int32_t)
  197. * @param chrVSrc scaled chroma (V) source data, 15bit for 8-10bit output,
  198. * 19-bit for 16bit output (in int32_t)
  199. * @param chrFilterSize number of vertical chroma input lines to scale
  200. * @param alpSrc scaled alpha (A) source data, 15bit for 8-10bit output,
  201. * 19-bit for 16bit output (in int32_t)
  202. * @param dest pointer to the output plane. For 16bit output, this is
  203. * uint16_t
  204. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  205. * to write into dest[]
  206. * @param y vertical line number for this output. This does not need
  207. * to be used to calculate the offset in the destination,
  208. * but can be used to generate comfort noise using dithering
  209. * or some output formats.
  210. */
  211. typedef void (*yuv2packedX_fn)(struct SwsContext *c, const int16_t *lumFilter,
  212. const int16_t **lumSrc, int lumFilterSize,
  213. const int16_t *chrFilter,
  214. const int16_t **chrUSrc,
  215. const int16_t **chrVSrc, int chrFilterSize,
  216. const int16_t **alpSrc, uint8_t *dest,
  217. int dstW, int y);
  218. /**
  219. * Write one line of horizontally scaled Y/U/V/A to YUV/RGB
  220. * output by doing multi-point vertical scaling between input pixels.
  221. *
  222. * @param c SWS scaling context
  223. * @param lumFilter vertical luma/alpha scaling coefficients, 12bit [0,4096]
  224. * @param lumSrc scaled luma (Y) source data, 15bit for 8-10bit output,
  225. * 19-bit for 16bit output (in int32_t)
  226. * @param lumFilterSize number of vertical luma/alpha input lines to scale
  227. * @param chrFilter vertical chroma scaling coefficients, 12bit [0,4096]
  228. * @param chrUSrc scaled chroma (U) source data, 15bit for 8-10bit output,
  229. * 19-bit for 16bit output (in int32_t)
  230. * @param chrVSrc scaled chroma (V) source data, 15bit for 8-10bit output,
  231. * 19-bit for 16bit output (in int32_t)
  232. * @param chrFilterSize number of vertical chroma input lines to scale
  233. * @param alpSrc scaled alpha (A) source data, 15bit for 8-10bit output,
  234. * 19-bit for 16bit output (in int32_t)
  235. * @param dest pointer to the output planes. For 16bit output, this is
  236. * uint16_t
  237. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  238. * to write into dest[]
  239. * @param y vertical line number for this output. This does not need
  240. * to be used to calculate the offset in the destination,
  241. * but can be used to generate comfort noise using dithering
  242. * or some output formats.
  243. */
  244. typedef void (*yuv2anyX_fn)(struct SwsContext *c, const int16_t *lumFilter,
  245. const int16_t **lumSrc, int lumFilterSize,
  246. const int16_t *chrFilter,
  247. const int16_t **chrUSrc,
  248. const int16_t **chrVSrc, int chrFilterSize,
  249. const int16_t **alpSrc, uint8_t **dest,
  250. int dstW, int y);
  251. /* This struct should be aligned on at least a 32-byte boundary. */
  252. typedef struct SwsContext {
  253. /**
  254. * info on struct for av_log
  255. */
  256. const AVClass *av_class;
  257. /**
  258. * Note that src, dst, srcStride, dstStride will be copied in the
  259. * sws_scale() wrapper so they can be freely modified here.
  260. */
  261. SwsFunc swscale;
  262. int srcW; ///< Width of source luma/alpha planes.
  263. int srcH; ///< Height of source luma/alpha planes.
  264. int dstH; ///< Height of destination luma/alpha planes.
  265. int chrSrcW; ///< Width of source chroma planes.
  266. int chrSrcH; ///< Height of source chroma planes.
  267. int chrDstW; ///< Width of destination chroma planes.
  268. int chrDstH; ///< Height of destination chroma planes.
  269. int lumXInc, chrXInc;
  270. int lumYInc, chrYInc;
  271. enum AVPixelFormat dstFormat; ///< Destination pixel format.
  272. enum AVPixelFormat srcFormat; ///< Source pixel format.
  273. int dstFormatBpp; ///< Number of bits per pixel of the destination pixel format.
  274. int srcFormatBpp; ///< Number of bits per pixel of the source pixel format.
  275. int dstBpc, srcBpc;
  276. int chrSrcHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in source image.
  277. int chrSrcVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in source image.
  278. int chrDstHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in destination image.
  279. int chrDstVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in destination image.
  280. int vChrDrop; ///< Binary logarithm of extra vertical subsampling factor in source image chroma planes specified by user.
  281. int sliceDir; ///< Direction that slices are fed to the scaler (1 = top-to-bottom, -1 = bottom-to-top).
  282. double param[2]; ///< Input parameters for scaling algorithms that need them.
  283. /* The cascaded_* fields allow spliting a scaler task into multiple
  284. * sequential steps, this is for example used to limit the maximum
  285. * downscaling factor that needs to be supported in one scaler.
  286. */
  287. struct SwsContext *cascaded_context[3];
  288. int cascaded_tmpStride[4];
  289. uint8_t *cascaded_tmp[4];
  290. int cascaded1_tmpStride[4];
  291. uint8_t *cascaded1_tmp[4];
  292. double gamma_value;
  293. int gamma_flag;
  294. uint16_t *gamma;
  295. uint16_t *inv_gamma;
  296. uint32_t pal_yuv[256];
  297. uint32_t pal_rgb[256];
  298. /**
  299. * @name Scaled horizontal lines ring buffer.
  300. * The horizontal scaler keeps just enough scaled lines in a ring buffer
  301. * so they may be passed to the vertical scaler. The pointers to the
  302. * allocated buffers for each line are duplicated in sequence in the ring
  303. * buffer to simplify indexing and avoid wrapping around between lines
  304. * inside the vertical scaler code. The wrapping is done before the
  305. * vertical scaler is called.
  306. */
  307. //@{
  308. int16_t **lumPixBuf; ///< Ring buffer for scaled horizontal luma plane lines to be fed to the vertical scaler.
  309. int16_t **chrUPixBuf; ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
  310. int16_t **chrVPixBuf; ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
  311. int16_t **alpPixBuf; ///< Ring buffer for scaled horizontal alpha plane lines to be fed to the vertical scaler.
  312. int vLumBufSize; ///< Number of vertical luma/alpha lines allocated in the ring buffer.
  313. int vChrBufSize; ///< Number of vertical chroma lines allocated in the ring buffer.
  314. int lastInLumBuf; ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
  315. int lastInChrBuf; ///< Last scaled horizontal chroma line from source in the ring buffer.
  316. int lumBufIndex; ///< Index in ring buffer of the last scaled horizontal luma/alpha line from source.
  317. int chrBufIndex; ///< Index in ring buffer of the last scaled horizontal chroma line from source.
  318. //@}
  319. uint8_t *formatConvBuffer;
  320. /**
  321. * @name Horizontal and vertical filters.
  322. * To better understand the following fields, here is a pseudo-code of
  323. * their usage in filtering a horizontal line:
  324. * @code
  325. * for (i = 0; i < width; i++) {
  326. * dst[i] = 0;
  327. * for (j = 0; j < filterSize; j++)
  328. * dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
  329. * dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
  330. * }
  331. * @endcode
  332. */
  333. //@{
  334. int16_t *hLumFilter; ///< Array of horizontal filter coefficients for luma/alpha planes.
  335. int16_t *hChrFilter; ///< Array of horizontal filter coefficients for chroma planes.
  336. int16_t *vLumFilter; ///< Array of vertical filter coefficients for luma/alpha planes.
  337. int16_t *vChrFilter; ///< Array of vertical filter coefficients for chroma planes.
  338. int32_t *hLumFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
  339. int32_t *hChrFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for chroma planes.
  340. int32_t *vLumFilterPos; ///< Array of vertical filter starting positions for each dst[i] for luma/alpha planes.
  341. int32_t *vChrFilterPos; ///< Array of vertical filter starting positions for each dst[i] for chroma planes.
  342. int hLumFilterSize; ///< Horizontal filter size for luma/alpha pixels.
  343. int hChrFilterSize; ///< Horizontal filter size for chroma pixels.
  344. int vLumFilterSize; ///< Vertical filter size for luma/alpha pixels.
  345. int vChrFilterSize; ///< Vertical filter size for chroma pixels.
  346. //@}
  347. int lumMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for luma/alpha planes.
  348. int chrMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for chroma planes.
  349. uint8_t *lumMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for luma/alpha planes.
  350. uint8_t *chrMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for chroma planes.
  351. int canMMXEXTBeUsed;
  352. int dstY; ///< Last destination vertical line output from last slice.
  353. int flags; ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
  354. void *yuvTable; // pointer to the yuv->rgb table start so it can be freed()
  355. // alignment ensures the offset can be added in a single
  356. // instruction on e.g. ARM
  357. DECLARE_ALIGNED(16, int, table_gV)[256 + 2*YUVRGB_TABLE_HEADROOM];
  358. uint8_t *table_rV[256 + 2*YUVRGB_TABLE_HEADROOM];
  359. uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
  360. uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
  361. 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
  362. #define RY_IDX 0
  363. #define GY_IDX 1
  364. #define BY_IDX 2
  365. #define RU_IDX 3
  366. #define GU_IDX 4
  367. #define BU_IDX 5
  368. #define RV_IDX 6
  369. #define GV_IDX 7
  370. #define BV_IDX 8
  371. #define RGB2YUV_SHIFT 15
  372. int *dither_error[4];
  373. //Colorspace stuff
  374. int contrast, brightness, saturation; // for sws_getColorspaceDetails
  375. int srcColorspaceTable[4];
  376. int dstColorspaceTable[4];
  377. int srcRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (source image).
  378. int dstRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
  379. int src0Alpha;
  380. int dst0Alpha;
  381. int srcXYZ;
  382. int dstXYZ;
  383. int src_h_chr_pos;
  384. int dst_h_chr_pos;
  385. int src_v_chr_pos;
  386. int dst_v_chr_pos;
  387. int yuv2rgb_y_offset;
  388. int yuv2rgb_y_coeff;
  389. int yuv2rgb_v2r_coeff;
  390. int yuv2rgb_v2g_coeff;
  391. int yuv2rgb_u2g_coeff;
  392. int yuv2rgb_u2b_coeff;
  393. #define RED_DITHER "0*8"
  394. #define GREEN_DITHER "1*8"
  395. #define BLUE_DITHER "2*8"
  396. #define Y_COEFF "3*8"
  397. #define VR_COEFF "4*8"
  398. #define UB_COEFF "5*8"
  399. #define VG_COEFF "6*8"
  400. #define UG_COEFF "7*8"
  401. #define Y_OFFSET "8*8"
  402. #define U_OFFSET "9*8"
  403. #define V_OFFSET "10*8"
  404. #define LUM_MMX_FILTER_OFFSET "11*8"
  405. #define CHR_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)
  406. #define DSTW_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2"
  407. #define ESP_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+8"
  408. #define VROUNDER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+16"
  409. #define U_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+24"
  410. #define V_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+32"
  411. #define Y_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+40"
  412. #define ALP_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+48"
  413. #define UV_OFF_PX "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+48"
  414. #define UV_OFF_BYTE "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+56"
  415. #define DITHER16 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+64"
  416. #define DITHER32 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+80"
  417. #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
  418. DECLARE_ALIGNED(8, uint64_t, redDither);
  419. DECLARE_ALIGNED(8, uint64_t, greenDither);
  420. DECLARE_ALIGNED(8, uint64_t, blueDither);
  421. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  422. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  423. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  424. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  425. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  426. DECLARE_ALIGNED(8, uint64_t, yOffset);
  427. DECLARE_ALIGNED(8, uint64_t, uOffset);
  428. DECLARE_ALIGNED(8, uint64_t, vOffset);
  429. int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
  430. int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
  431. int dstW; ///< Width of destination luma/alpha planes.
  432. DECLARE_ALIGNED(8, uint64_t, esp);
  433. DECLARE_ALIGNED(8, uint64_t, vRounder);
  434. DECLARE_ALIGNED(8, uint64_t, u_temp);
  435. DECLARE_ALIGNED(8, uint64_t, v_temp);
  436. DECLARE_ALIGNED(8, uint64_t, y_temp);
  437. int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
  438. // alignment of these values is not necessary, but merely here
  439. // to maintain the same offset across x8632 and x86-64. Once we
  440. // use proper offset macros in the asm, they can be removed.
  441. DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
  442. DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
  443. DECLARE_ALIGNED(8, uint16_t, dither16)[8];
  444. DECLARE_ALIGNED(8, uint32_t, dither32)[8];
  445. const uint8_t *chrDither8, *lumDither8;
  446. #if HAVE_ALTIVEC
  447. vector signed short CY;
  448. vector signed short CRV;
  449. vector signed short CBU;
  450. vector signed short CGU;
  451. vector signed short CGV;
  452. vector signed short OY;
  453. vector unsigned short CSHIFT;
  454. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  455. #endif
  456. int use_mmx_vfilter;
  457. /* pre defined color-spaces gamma */
  458. #define XYZ_GAMMA (2.6f)
  459. #define RGB_GAMMA (2.2f)
  460. int16_t *xyzgamma;
  461. int16_t *rgbgamma;
  462. int16_t *xyzgammainv;
  463. int16_t *rgbgammainv;
  464. int16_t xyz2rgb_matrix[3][4];
  465. int16_t rgb2xyz_matrix[3][4];
  466. /* function pointers for swscale() */
  467. yuv2planar1_fn yuv2plane1;
  468. yuv2planarX_fn yuv2planeX;
  469. yuv2interleavedX_fn yuv2nv12cX;
  470. yuv2packed1_fn yuv2packed1;
  471. yuv2packed2_fn yuv2packed2;
  472. yuv2packedX_fn yuv2packedX;
  473. yuv2anyX_fn yuv2anyX;
  474. /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
  475. void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  476. int width, uint32_t *pal);
  477. /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  478. void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  479. int width, uint32_t *pal);
  480. /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  481. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  482. const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  483. int width, uint32_t *pal);
  484. /**
  485. * Functions to read planar input, such as planar RGB, and convert
  486. * internally to Y/UV/A.
  487. */
  488. /** @{ */
  489. void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  490. void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
  491. int width, int32_t *rgb2yuv);
  492. void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  493. /** @} */
  494. /**
  495. * Scale one horizontal line of input data using a bilinear filter
  496. * to produce one line of output data. Compared to SwsContext->hScale(),
  497. * please take note of the following caveats when using these:
  498. * - Scaling is done using only 7bit instead of 14bit coefficients.
  499. * - You can use no more than 5 input pixels to produce 4 output
  500. * pixels. Therefore, this filter should not be used for downscaling
  501. * by more than ~20% in width (because that equals more than 5/4th
  502. * downscaling and thus more than 5 pixels input per 4 pixels output).
  503. * - In general, bilinear filters create artifacts during downscaling
  504. * (even when <20%), because one output pixel will span more than one
  505. * input pixel, and thus some pixels will need edges of both neighbor
  506. * pixels to interpolate the output pixel. Since you can use at most
  507. * two input pixels per output pixel in bilinear scaling, this is
  508. * impossible and thus downscaling by any size will create artifacts.
  509. * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
  510. * in SwsContext->flags.
  511. */
  512. /** @{ */
  513. void (*hyscale_fast)(struct SwsContext *c,
  514. int16_t *dst, int dstWidth,
  515. const uint8_t *src, int srcW, int xInc);
  516. void (*hcscale_fast)(struct SwsContext *c,
  517. int16_t *dst1, int16_t *dst2, int dstWidth,
  518. const uint8_t *src1, const uint8_t *src2,
  519. int srcW, int xInc);
  520. /** @} */
  521. /**
  522. * Scale one horizontal line of input data using a filter over the input
  523. * lines, to produce one (differently sized) line of output data.
  524. *
  525. * @param dst pointer to destination buffer for horizontally scaled
  526. * data. If the number of bits per component of one
  527. * destination pixel (SwsContext->dstBpc) is <= 10, data
  528. * will be 15bpc in 16bits (int16_t) width. Else (i.e.
  529. * SwsContext->dstBpc == 16), data will be 19bpc in
  530. * 32bits (int32_t) width.
  531. * @param dstW width of destination image
  532. * @param src pointer to source data to be scaled. If the number of
  533. * bits per component of a source pixel (SwsContext->srcBpc)
  534. * is 8, this is 8bpc in 8bits (uint8_t) width. Else
  535. * (i.e. SwsContext->dstBpc > 8), this is native depth
  536. * in 16bits (uint16_t) width. In other words, for 9-bit
  537. * YUV input, this is 9bpc, for 10-bit YUV input, this is
  538. * 10bpc, and for 16-bit RGB or YUV, this is 16bpc.
  539. * @param filter filter coefficients to be used per output pixel for
  540. * scaling. This contains 14bpp filtering coefficients.
  541. * Guaranteed to contain dstW * filterSize entries.
  542. * @param filterPos position of the first input pixel to be used for
  543. * each output pixel during scaling. Guaranteed to
  544. * contain dstW entries.
  545. * @param filterSize the number of input coefficients to be used (and
  546. * thus the number of input pixels to be used) for
  547. * creating a single output pixel. Is aligned to 4
  548. * (and input coefficients thus padded with zeroes)
  549. * to simplify creating SIMD code.
  550. */
  551. /** @{ */
  552. void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
  553. const uint8_t *src, const int16_t *filter,
  554. const int32_t *filterPos, int filterSize);
  555. void (*hcScale)(struct SwsContext *c, int16_t *dst, int dstW,
  556. const uint8_t *src, const int16_t *filter,
  557. const int32_t *filterPos, int filterSize);
  558. /** @} */
  559. /// Color range conversion function for luma plane if needed.
  560. void (*lumConvertRange)(int16_t *dst, int width);
  561. /// Color range conversion function for chroma planes if needed.
  562. void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
  563. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  564. SwsDither dither;
  565. } SwsContext;
  566. //FIXME check init (where 0)
  567. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  568. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  569. int fullRange, int brightness,
  570. int contrast, int saturation);
  571. void ff_yuv2rgb_init_tables_ppc(SwsContext *c, const int inv_table[4],
  572. int brightness, int contrast, int saturation);
  573. void ff_updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
  574. int lastInLumBuf, int lastInChrBuf);
  575. av_cold void ff_sws_init_range_convert(SwsContext *c);
  576. SwsFunc ff_yuv2rgb_init_x86(SwsContext *c);
  577. SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c);
  578. static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
  579. {
  580. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  581. av_assert0(desc);
  582. return desc->comp[0].depth_minus1 == 15;
  583. }
  584. static av_always_inline int is9_OR_10BPS(enum AVPixelFormat pix_fmt)
  585. {
  586. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  587. av_assert0(desc);
  588. return desc->comp[0].depth_minus1 >= 8 && desc->comp[0].depth_minus1 <= 13;
  589. }
  590. #define isNBPS(x) is9_OR_10BPS(x)
  591. static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
  592. {
  593. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  594. av_assert0(desc);
  595. return desc->flags & AV_PIX_FMT_FLAG_BE;
  596. }
  597. static av_always_inline int isYUV(enum AVPixelFormat pix_fmt)
  598. {
  599. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  600. av_assert0(desc);
  601. return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
  602. }
  603. static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
  604. {
  605. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  606. av_assert0(desc);
  607. return ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && isYUV(pix_fmt));
  608. }
  609. static av_always_inline int isRGB(enum AVPixelFormat pix_fmt)
  610. {
  611. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  612. av_assert0(desc);
  613. return (desc->flags & AV_PIX_FMT_FLAG_RGB);
  614. }
  615. #if 0 // FIXME
  616. #define isGray(x) \
  617. (!(av_pix_fmt_desc_get(x)->flags & AV_PIX_FMT_FLAG_PAL) && \
  618. av_pix_fmt_desc_get(x)->nb_components <= 2)
  619. #else
  620. #define isGray(x) \
  621. ((x) == AV_PIX_FMT_GRAY8 || \
  622. (x) == AV_PIX_FMT_YA8 || \
  623. (x) == AV_PIX_FMT_GRAY16BE || \
  624. (x) == AV_PIX_FMT_GRAY16LE || \
  625. (x) == AV_PIX_FMT_YA16BE || \
  626. (x) == AV_PIX_FMT_YA16LE)
  627. #endif
  628. #define isRGBinInt(x) \
  629. ( \
  630. (x) == AV_PIX_FMT_RGB48BE || \
  631. (x) == AV_PIX_FMT_RGB48LE || \
  632. (x) == AV_PIX_FMT_RGB32 || \
  633. (x) == AV_PIX_FMT_RGB32_1 || \
  634. (x) == AV_PIX_FMT_RGB24 || \
  635. (x) == AV_PIX_FMT_RGB565BE || \
  636. (x) == AV_PIX_FMT_RGB565LE || \
  637. (x) == AV_PIX_FMT_RGB555BE || \
  638. (x) == AV_PIX_FMT_RGB555LE || \
  639. (x) == AV_PIX_FMT_RGB444BE || \
  640. (x) == AV_PIX_FMT_RGB444LE || \
  641. (x) == AV_PIX_FMT_RGB8 || \
  642. (x) == AV_PIX_FMT_RGB4 || \
  643. (x) == AV_PIX_FMT_RGB4_BYTE || \
  644. (x) == AV_PIX_FMT_RGBA64BE || \
  645. (x) == AV_PIX_FMT_RGBA64LE || \
  646. (x) == AV_PIX_FMT_MONOBLACK || \
  647. (x) == AV_PIX_FMT_MONOWHITE \
  648. )
  649. #define isBGRinInt(x) \
  650. ( \
  651. (x) == AV_PIX_FMT_BGR48BE || \
  652. (x) == AV_PIX_FMT_BGR48LE || \
  653. (x) == AV_PIX_FMT_BGR32 || \
  654. (x) == AV_PIX_FMT_BGR32_1 || \
  655. (x) == AV_PIX_FMT_BGR24 || \
  656. (x) == AV_PIX_FMT_BGR565BE || \
  657. (x) == AV_PIX_FMT_BGR565LE || \
  658. (x) == AV_PIX_FMT_BGR555BE || \
  659. (x) == AV_PIX_FMT_BGR555LE || \
  660. (x) == AV_PIX_FMT_BGR444BE || \
  661. (x) == AV_PIX_FMT_BGR444LE || \
  662. (x) == AV_PIX_FMT_BGR8 || \
  663. (x) == AV_PIX_FMT_BGR4 || \
  664. (x) == AV_PIX_FMT_BGR4_BYTE || \
  665. (x) == AV_PIX_FMT_BGRA64BE || \
  666. (x) == AV_PIX_FMT_BGRA64LE || \
  667. (x) == AV_PIX_FMT_MONOBLACK || \
  668. (x) == AV_PIX_FMT_MONOWHITE \
  669. )
  670. #define isRGBinBytes(x) ( \
  671. (x) == AV_PIX_FMT_RGB48BE \
  672. || (x) == AV_PIX_FMT_RGB48LE \
  673. || (x) == AV_PIX_FMT_RGBA64BE \
  674. || (x) == AV_PIX_FMT_RGBA64LE \
  675. || (x) == AV_PIX_FMT_RGBA \
  676. || (x) == AV_PIX_FMT_ARGB \
  677. || (x) == AV_PIX_FMT_RGB24 \
  678. )
  679. #define isBGRinBytes(x) ( \
  680. (x) == AV_PIX_FMT_BGR48BE \
  681. || (x) == AV_PIX_FMT_BGR48LE \
  682. || (x) == AV_PIX_FMT_BGRA64BE \
  683. || (x) == AV_PIX_FMT_BGRA64LE \
  684. || (x) == AV_PIX_FMT_BGRA \
  685. || (x) == AV_PIX_FMT_ABGR \
  686. || (x) == AV_PIX_FMT_BGR24 \
  687. )
  688. #define isBayer(x) ( \
  689. (x)==AV_PIX_FMT_BAYER_BGGR8 \
  690. || (x)==AV_PIX_FMT_BAYER_BGGR16LE \
  691. || (x)==AV_PIX_FMT_BAYER_BGGR16BE \
  692. || (x)==AV_PIX_FMT_BAYER_RGGB8 \
  693. || (x)==AV_PIX_FMT_BAYER_RGGB16LE \
  694. || (x)==AV_PIX_FMT_BAYER_RGGB16BE \
  695. || (x)==AV_PIX_FMT_BAYER_GBRG8 \
  696. || (x)==AV_PIX_FMT_BAYER_GBRG16LE \
  697. || (x)==AV_PIX_FMT_BAYER_GBRG16BE \
  698. || (x)==AV_PIX_FMT_BAYER_GRBG8 \
  699. || (x)==AV_PIX_FMT_BAYER_GRBG16LE \
  700. || (x)==AV_PIX_FMT_BAYER_GRBG16BE \
  701. )
  702. #define isAnyRGB(x) \
  703. ( \
  704. isBayer(x) || \
  705. isRGBinInt(x) || \
  706. isBGRinInt(x) || \
  707. isRGB(x) \
  708. )
  709. static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
  710. {
  711. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  712. av_assert0(desc);
  713. if (pix_fmt == AV_PIX_FMT_PAL8)
  714. return 1;
  715. return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  716. }
  717. #if 1
  718. #define isPacked(x) ( \
  719. (x)==AV_PIX_FMT_PAL8 \
  720. || (x)==AV_PIX_FMT_YUYV422 \
  721. || (x)==AV_PIX_FMT_YVYU422 \
  722. || (x)==AV_PIX_FMT_UYVY422 \
  723. || (x)==AV_PIX_FMT_YA8 \
  724. || (x)==AV_PIX_FMT_YA16LE \
  725. || (x)==AV_PIX_FMT_YA16BE \
  726. || isRGBinInt(x) \
  727. || isBGRinInt(x) \
  728. )
  729. #else
  730. static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
  731. {
  732. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  733. av_assert0(desc);
  734. return ((desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
  735. pix_fmt == AV_PIX_FMT_PAL8);
  736. }
  737. #endif
  738. static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
  739. {
  740. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  741. av_assert0(desc);
  742. return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  743. }
  744. static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
  745. {
  746. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  747. av_assert0(desc);
  748. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
  749. }
  750. static av_always_inline int isPlanarRGB(enum AVPixelFormat pix_fmt)
  751. {
  752. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  753. av_assert0(desc);
  754. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) ==
  755. (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
  756. }
  757. static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
  758. {
  759. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  760. av_assert0(desc);
  761. return (desc->flags & AV_PIX_FMT_FLAG_PAL) || (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL);
  762. }
  763. extern const uint64_t ff_dither4[2];
  764. extern const uint64_t ff_dither8[2];
  765. extern const uint8_t ff_dither_2x2_4[3][8];
  766. extern const uint8_t ff_dither_2x2_8[3][8];
  767. extern const uint8_t ff_dither_4x4_16[5][8];
  768. extern const uint8_t ff_dither_8x8_32[9][8];
  769. extern const uint8_t ff_dither_8x8_73[9][8];
  770. extern const uint8_t ff_dither_8x8_128[9][8];
  771. extern const uint8_t ff_dither_8x8_220[9][8];
  772. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  773. extern const AVClass sws_context_class;
  774. /**
  775. * Set c->swscale to an unscaled converter if one exists for the specific
  776. * source and destination formats, bit depths, flags, etc.
  777. */
  778. void ff_get_unscaled_swscale(SwsContext *c);
  779. void ff_get_unscaled_swscale_ppc(SwsContext *c);
  780. void ff_get_unscaled_swscale_arm(SwsContext *c);
  781. /**
  782. * Return function pointer to fastest main scaler path function depending
  783. * on architecture and available optimizations.
  784. */
  785. SwsFunc ff_getSwsFunc(SwsContext *c);
  786. void ff_sws_init_input_funcs(SwsContext *c);
  787. void ff_sws_init_output_funcs(SwsContext *c,
  788. yuv2planar1_fn *yuv2plane1,
  789. yuv2planarX_fn *yuv2planeX,
  790. yuv2interleavedX_fn *yuv2nv12cX,
  791. yuv2packed1_fn *yuv2packed1,
  792. yuv2packed2_fn *yuv2packed2,
  793. yuv2packedX_fn *yuv2packedX,
  794. yuv2anyX_fn *yuv2anyX);
  795. void ff_sws_init_swscale_ppc(SwsContext *c);
  796. void ff_sws_init_swscale_x86(SwsContext *c);
  797. void ff_hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  798. const uint8_t *src, int srcW, int xInc);
  799. void ff_hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  800. int dstWidth, const uint8_t *src1,
  801. const uint8_t *src2, int srcW, int xInc);
  802. int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
  803. int16_t *filter, int32_t *filterPos,
  804. int numSplits);
  805. void ff_hyscale_fast_mmxext(SwsContext *c, int16_t *dst,
  806. int dstWidth, const uint8_t *src,
  807. int srcW, int xInc);
  808. void ff_hcscale_fast_mmxext(SwsContext *c, int16_t *dst1, int16_t *dst2,
  809. int dstWidth, const uint8_t *src1,
  810. const uint8_t *src2, int srcW, int xInc);
  811. static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
  812. int alpha, int bits, const int big_endian)
  813. {
  814. int i, j;
  815. uint8_t *ptr = plane + stride * y;
  816. int v = alpha ? 0xFFFF>>(15-bits) : (1<<bits);
  817. for (i = 0; i < height; i++) {
  818. #define FILL(wfunc) \
  819. for (j = 0; j < width; j++) {\
  820. wfunc(ptr+2*j, v);\
  821. }
  822. if (big_endian) {
  823. FILL(AV_WB16);
  824. } else {
  825. FILL(AV_WL16);
  826. }
  827. ptr += stride;
  828. }
  829. }
  830. #endif /* SWSCALE_SWSCALE_INTERNAL_H */