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.

904 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[2];
  288. int cascaded_tmpStride[4];
  289. uint8_t *cascaded_tmp[4];
  290. uint32_t pal_yuv[256];
  291. uint32_t pal_rgb[256];
  292. /**
  293. * @name Scaled horizontal lines ring buffer.
  294. * The horizontal scaler keeps just enough scaled lines in a ring buffer
  295. * so they may be passed to the vertical scaler. The pointers to the
  296. * allocated buffers for each line are duplicated in sequence in the ring
  297. * buffer to simplify indexing and avoid wrapping around between lines
  298. * inside the vertical scaler code. The wrapping is done before the
  299. * vertical scaler is called.
  300. */
  301. //@{
  302. int16_t **lumPixBuf; ///< Ring buffer for scaled horizontal luma plane lines to be fed to the vertical scaler.
  303. int16_t **chrUPixBuf; ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
  304. int16_t **chrVPixBuf; ///< Ring buffer for scaled horizontal chroma plane lines to be fed to the vertical scaler.
  305. int16_t **alpPixBuf; ///< Ring buffer for scaled horizontal alpha plane lines to be fed to the vertical scaler.
  306. int vLumBufSize; ///< Number of vertical luma/alpha lines allocated in the ring buffer.
  307. int vChrBufSize; ///< Number of vertical chroma lines allocated in the ring buffer.
  308. int lastInLumBuf; ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
  309. int lastInChrBuf; ///< Last scaled horizontal chroma line from source in the ring buffer.
  310. int lumBufIndex; ///< Index in ring buffer of the last scaled horizontal luma/alpha line from source.
  311. int chrBufIndex; ///< Index in ring buffer of the last scaled horizontal chroma line from source.
  312. //@}
  313. uint8_t *formatConvBuffer;
  314. /**
  315. * @name Horizontal and vertical filters.
  316. * To better understand the following fields, here is a pseudo-code of
  317. * their usage in filtering a horizontal line:
  318. * @code
  319. * for (i = 0; i < width; i++) {
  320. * dst[i] = 0;
  321. * for (j = 0; j < filterSize; j++)
  322. * dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
  323. * dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
  324. * }
  325. * @endcode
  326. */
  327. //@{
  328. int16_t *hLumFilter; ///< Array of horizontal filter coefficients for luma/alpha planes.
  329. int16_t *hChrFilter; ///< Array of horizontal filter coefficients for chroma planes.
  330. int16_t *vLumFilter; ///< Array of vertical filter coefficients for luma/alpha planes.
  331. int16_t *vChrFilter; ///< Array of vertical filter coefficients for chroma planes.
  332. int32_t *hLumFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
  333. int32_t *hChrFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for chroma planes.
  334. int32_t *vLumFilterPos; ///< Array of vertical filter starting positions for each dst[i] for luma/alpha planes.
  335. int32_t *vChrFilterPos; ///< Array of vertical filter starting positions for each dst[i] for chroma planes.
  336. int hLumFilterSize; ///< Horizontal filter size for luma/alpha pixels.
  337. int hChrFilterSize; ///< Horizontal filter size for chroma pixels.
  338. int vLumFilterSize; ///< Vertical filter size for luma/alpha pixels.
  339. int vChrFilterSize; ///< Vertical filter size for chroma pixels.
  340. //@}
  341. int lumMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for luma/alpha planes.
  342. int chrMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for chroma planes.
  343. uint8_t *lumMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for luma/alpha planes.
  344. uint8_t *chrMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for chroma planes.
  345. int canMMXEXTBeUsed;
  346. int dstY; ///< Last destination vertical line output from last slice.
  347. int flags; ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
  348. void *yuvTable; // pointer to the yuv->rgb table start so it can be freed()
  349. // alignment ensures the offset can be added in a single
  350. // instruction on e.g. ARM
  351. DECLARE_ALIGNED(16, int, table_gV)[256 + 2*YUVRGB_TABLE_HEADROOM];
  352. uint8_t *table_rV[256 + 2*YUVRGB_TABLE_HEADROOM];
  353. uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
  354. uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
  355. 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
  356. #define RY_IDX 0
  357. #define GY_IDX 1
  358. #define BY_IDX 2
  359. #define RU_IDX 3
  360. #define GU_IDX 4
  361. #define BU_IDX 5
  362. #define RV_IDX 6
  363. #define GV_IDX 7
  364. #define BV_IDX 8
  365. #define RGB2YUV_SHIFT 15
  366. int *dither_error[4];
  367. //Colorspace stuff
  368. int contrast, brightness, saturation; // for sws_getColorspaceDetails
  369. int srcColorspaceTable[4];
  370. int dstColorspaceTable[4];
  371. int srcRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (source image).
  372. int dstRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
  373. int src0Alpha;
  374. int dst0Alpha;
  375. int srcXYZ;
  376. int dstXYZ;
  377. int src_h_chr_pos;
  378. int dst_h_chr_pos;
  379. int src_v_chr_pos;
  380. int dst_v_chr_pos;
  381. int yuv2rgb_y_offset;
  382. int yuv2rgb_y_coeff;
  383. int yuv2rgb_v2r_coeff;
  384. int yuv2rgb_v2g_coeff;
  385. int yuv2rgb_u2g_coeff;
  386. int yuv2rgb_u2b_coeff;
  387. #define RED_DITHER "0*8"
  388. #define GREEN_DITHER "1*8"
  389. #define BLUE_DITHER "2*8"
  390. #define Y_COEFF "3*8"
  391. #define VR_COEFF "4*8"
  392. #define UB_COEFF "5*8"
  393. #define VG_COEFF "6*8"
  394. #define UG_COEFF "7*8"
  395. #define Y_OFFSET "8*8"
  396. #define U_OFFSET "9*8"
  397. #define V_OFFSET "10*8"
  398. #define LUM_MMX_FILTER_OFFSET "11*8"
  399. #define CHR_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)
  400. #define DSTW_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2"
  401. #define ESP_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+8"
  402. #define VROUNDER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+16"
  403. #define U_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+24"
  404. #define V_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+32"
  405. #define Y_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+40"
  406. #define ALP_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+48"
  407. #define UV_OFF_PX "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+48"
  408. #define UV_OFF_BYTE "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+56"
  409. #define DITHER16 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+64"
  410. #define DITHER32 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+80"
  411. #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
  412. DECLARE_ALIGNED(8, uint64_t, redDither);
  413. DECLARE_ALIGNED(8, uint64_t, greenDither);
  414. DECLARE_ALIGNED(8, uint64_t, blueDither);
  415. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  416. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  417. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  418. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  419. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  420. DECLARE_ALIGNED(8, uint64_t, yOffset);
  421. DECLARE_ALIGNED(8, uint64_t, uOffset);
  422. DECLARE_ALIGNED(8, uint64_t, vOffset);
  423. int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
  424. int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
  425. int dstW; ///< Width of destination luma/alpha planes.
  426. DECLARE_ALIGNED(8, uint64_t, esp);
  427. DECLARE_ALIGNED(8, uint64_t, vRounder);
  428. DECLARE_ALIGNED(8, uint64_t, u_temp);
  429. DECLARE_ALIGNED(8, uint64_t, v_temp);
  430. DECLARE_ALIGNED(8, uint64_t, y_temp);
  431. int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
  432. // alignment of these values is not necessary, but merely here
  433. // to maintain the same offset across x8632 and x86-64. Once we
  434. // use proper offset macros in the asm, they can be removed.
  435. DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
  436. DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
  437. DECLARE_ALIGNED(8, uint16_t, dither16)[8];
  438. DECLARE_ALIGNED(8, uint32_t, dither32)[8];
  439. const uint8_t *chrDither8, *lumDither8;
  440. #if HAVE_ALTIVEC
  441. vector signed short CY;
  442. vector signed short CRV;
  443. vector signed short CBU;
  444. vector signed short CGU;
  445. vector signed short CGV;
  446. vector signed short OY;
  447. vector unsigned short CSHIFT;
  448. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  449. #endif
  450. int use_mmx_vfilter;
  451. /* pre defined color-spaces gamma */
  452. #define XYZ_GAMMA (2.6f)
  453. #define RGB_GAMMA (2.2f)
  454. int16_t *xyzgamma;
  455. int16_t *rgbgamma;
  456. int16_t *xyzgammainv;
  457. int16_t *rgbgammainv;
  458. int16_t xyz2rgb_matrix[3][4];
  459. int16_t rgb2xyz_matrix[3][4];
  460. /* function pointers for swscale() */
  461. yuv2planar1_fn yuv2plane1;
  462. yuv2planarX_fn yuv2planeX;
  463. yuv2interleavedX_fn yuv2nv12cX;
  464. yuv2packed1_fn yuv2packed1;
  465. yuv2packed2_fn yuv2packed2;
  466. yuv2packedX_fn yuv2packedX;
  467. yuv2anyX_fn yuv2anyX;
  468. /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
  469. void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  470. int width, uint32_t *pal);
  471. /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  472. void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  473. int width, uint32_t *pal);
  474. /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  475. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  476. const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  477. int width, uint32_t *pal);
  478. /**
  479. * Functions to read planar input, such as planar RGB, and convert
  480. * internally to Y/UV/A.
  481. */
  482. /** @{ */
  483. void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  484. void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
  485. int width, int32_t *rgb2yuv);
  486. void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  487. /** @} */
  488. /**
  489. * Scale one horizontal line of input data using a bilinear filter
  490. * to produce one line of output data. Compared to SwsContext->hScale(),
  491. * please take note of the following caveats when using these:
  492. * - Scaling is done using only 7bit instead of 14bit coefficients.
  493. * - You can use no more than 5 input pixels to produce 4 output
  494. * pixels. Therefore, this filter should not be used for downscaling
  495. * by more than ~20% in width (because that equals more than 5/4th
  496. * downscaling and thus more than 5 pixels input per 4 pixels output).
  497. * - In general, bilinear filters create artifacts during downscaling
  498. * (even when <20%), because one output pixel will span more than one
  499. * input pixel, and thus some pixels will need edges of both neighbor
  500. * pixels to interpolate the output pixel. Since you can use at most
  501. * two input pixels per output pixel in bilinear scaling, this is
  502. * impossible and thus downscaling by any size will create artifacts.
  503. * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
  504. * in SwsContext->flags.
  505. */
  506. /** @{ */
  507. void (*hyscale_fast)(struct SwsContext *c,
  508. int16_t *dst, int dstWidth,
  509. const uint8_t *src, int srcW, int xInc);
  510. void (*hcscale_fast)(struct SwsContext *c,
  511. int16_t *dst1, int16_t *dst2, int dstWidth,
  512. const uint8_t *src1, const uint8_t *src2,
  513. int srcW, int xInc);
  514. /** @} */
  515. /**
  516. * Scale one horizontal line of input data using a filter over the input
  517. * lines, to produce one (differently sized) line of output data.
  518. *
  519. * @param dst pointer to destination buffer for horizontally scaled
  520. * data. If the number of bits per component of one
  521. * destination pixel (SwsContext->dstBpc) is <= 10, data
  522. * will be 15bpc in 16bits (int16_t) width. Else (i.e.
  523. * SwsContext->dstBpc == 16), data will be 19bpc in
  524. * 32bits (int32_t) width.
  525. * @param dstW width of destination image
  526. * @param src pointer to source data to be scaled. If the number of
  527. * bits per component of a source pixel (SwsContext->srcBpc)
  528. * is 8, this is 8bpc in 8bits (uint8_t) width. Else
  529. * (i.e. SwsContext->dstBpc > 8), this is native depth
  530. * in 16bits (uint16_t) width. In other words, for 9-bit
  531. * YUV input, this is 9bpc, for 10-bit YUV input, this is
  532. * 10bpc, and for 16-bit RGB or YUV, this is 16bpc.
  533. * @param filter filter coefficients to be used per output pixel for
  534. * scaling. This contains 14bpp filtering coefficients.
  535. * Guaranteed to contain dstW * filterSize entries.
  536. * @param filterPos position of the first input pixel to be used for
  537. * each output pixel during scaling. Guaranteed to
  538. * contain dstW entries.
  539. * @param filterSize the number of input coefficients to be used (and
  540. * thus the number of input pixels to be used) for
  541. * creating a single output pixel. Is aligned to 4
  542. * (and input coefficients thus padded with zeroes)
  543. * to simplify creating SIMD code.
  544. */
  545. /** @{ */
  546. void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
  547. const uint8_t *src, const int16_t *filter,
  548. const int32_t *filterPos, int filterSize);
  549. void (*hcScale)(struct SwsContext *c, int16_t *dst, int dstW,
  550. const uint8_t *src, const int16_t *filter,
  551. const int32_t *filterPos, int filterSize);
  552. /** @} */
  553. /// Color range conversion function for luma plane if needed.
  554. void (*lumConvertRange)(int16_t *dst, int width);
  555. /// Color range conversion function for chroma planes if needed.
  556. void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
  557. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  558. SwsDither dither;
  559. } SwsContext;
  560. //FIXME check init (where 0)
  561. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  562. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  563. int fullRange, int brightness,
  564. int contrast, int saturation);
  565. void ff_yuv2rgb_init_tables_ppc(SwsContext *c, const int inv_table[4],
  566. int brightness, int contrast, int saturation);
  567. void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
  568. int lastInLumBuf, int lastInChrBuf);
  569. av_cold void ff_sws_init_range_convert(SwsContext *c);
  570. SwsFunc ff_yuv2rgb_init_x86(SwsContext *c);
  571. SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c);
  572. static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
  573. {
  574. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  575. av_assert0(desc);
  576. return desc->comp[0].depth_minus1 == 15;
  577. }
  578. static av_always_inline int is9_OR_10BPS(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 >= 8 && desc->comp[0].depth_minus1 <= 13;
  583. }
  584. #define isNBPS(x) is9_OR_10BPS(x)
  585. static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
  586. {
  587. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  588. av_assert0(desc);
  589. return desc->flags & AV_PIX_FMT_FLAG_BE;
  590. }
  591. static av_always_inline int isYUV(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_RGB) && desc->nb_components >= 2;
  596. }
  597. static av_always_inline int isPlanarYUV(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_PLANAR) && isYUV(pix_fmt));
  602. }
  603. static av_always_inline int isRGB(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_RGB);
  608. }
  609. #if 0 // FIXME
  610. #define isGray(x) \
  611. (!(av_pix_fmt_desc_get(x)->flags & AV_PIX_FMT_FLAG_PAL) && \
  612. av_pix_fmt_desc_get(x)->nb_components <= 2)
  613. #else
  614. #define isGray(x) \
  615. ((x) == AV_PIX_FMT_GRAY8 || \
  616. (x) == AV_PIX_FMT_YA8 || \
  617. (x) == AV_PIX_FMT_GRAY16BE || \
  618. (x) == AV_PIX_FMT_GRAY16LE || \
  619. (x) == AV_PIX_FMT_YA16BE || \
  620. (x) == AV_PIX_FMT_YA16LE)
  621. #endif
  622. #define isRGBinInt(x) \
  623. ( \
  624. (x) == AV_PIX_FMT_RGB48BE || \
  625. (x) == AV_PIX_FMT_RGB48LE || \
  626. (x) == AV_PIX_FMT_RGB32 || \
  627. (x) == AV_PIX_FMT_RGB32_1 || \
  628. (x) == AV_PIX_FMT_RGB24 || \
  629. (x) == AV_PIX_FMT_RGB565BE || \
  630. (x) == AV_PIX_FMT_RGB565LE || \
  631. (x) == AV_PIX_FMT_RGB555BE || \
  632. (x) == AV_PIX_FMT_RGB555LE || \
  633. (x) == AV_PIX_FMT_RGB444BE || \
  634. (x) == AV_PIX_FMT_RGB444LE || \
  635. (x) == AV_PIX_FMT_RGB8 || \
  636. (x) == AV_PIX_FMT_RGB4 || \
  637. (x) == AV_PIX_FMT_RGB4_BYTE || \
  638. (x) == AV_PIX_FMT_RGBA64BE || \
  639. (x) == AV_PIX_FMT_RGBA64LE || \
  640. (x) == AV_PIX_FMT_MONOBLACK || \
  641. (x) == AV_PIX_FMT_MONOWHITE \
  642. )
  643. #define isBGRinInt(x) \
  644. ( \
  645. (x) == AV_PIX_FMT_BGR48BE || \
  646. (x) == AV_PIX_FMT_BGR48LE || \
  647. (x) == AV_PIX_FMT_BGR32 || \
  648. (x) == AV_PIX_FMT_BGR32_1 || \
  649. (x) == AV_PIX_FMT_BGR24 || \
  650. (x) == AV_PIX_FMT_BGR565BE || \
  651. (x) == AV_PIX_FMT_BGR565LE || \
  652. (x) == AV_PIX_FMT_BGR555BE || \
  653. (x) == AV_PIX_FMT_BGR555LE || \
  654. (x) == AV_PIX_FMT_BGR444BE || \
  655. (x) == AV_PIX_FMT_BGR444LE || \
  656. (x) == AV_PIX_FMT_BGR8 || \
  657. (x) == AV_PIX_FMT_BGR4 || \
  658. (x) == AV_PIX_FMT_BGR4_BYTE || \
  659. (x) == AV_PIX_FMT_BGRA64BE || \
  660. (x) == AV_PIX_FMT_BGRA64LE || \
  661. (x) == AV_PIX_FMT_MONOBLACK || \
  662. (x) == AV_PIX_FMT_MONOWHITE \
  663. )
  664. #define isRGBinBytes(x) ( \
  665. (x) == AV_PIX_FMT_RGB48BE \
  666. || (x) == AV_PIX_FMT_RGB48LE \
  667. || (x) == AV_PIX_FMT_RGBA64BE \
  668. || (x) == AV_PIX_FMT_RGBA64LE \
  669. || (x) == AV_PIX_FMT_RGBA \
  670. || (x) == AV_PIX_FMT_ARGB \
  671. || (x) == AV_PIX_FMT_RGB24 \
  672. )
  673. #define isBGRinBytes(x) ( \
  674. (x) == AV_PIX_FMT_BGR48BE \
  675. || (x) == AV_PIX_FMT_BGR48LE \
  676. || (x) == AV_PIX_FMT_BGRA64BE \
  677. || (x) == AV_PIX_FMT_BGRA64LE \
  678. || (x) == AV_PIX_FMT_BGRA \
  679. || (x) == AV_PIX_FMT_ABGR \
  680. || (x) == AV_PIX_FMT_BGR24 \
  681. )
  682. #define isBayer(x) ( \
  683. (x)==AV_PIX_FMT_BAYER_BGGR8 \
  684. || (x)==AV_PIX_FMT_BAYER_BGGR16LE \
  685. || (x)==AV_PIX_FMT_BAYER_BGGR16BE \
  686. || (x)==AV_PIX_FMT_BAYER_RGGB8 \
  687. || (x)==AV_PIX_FMT_BAYER_RGGB16LE \
  688. || (x)==AV_PIX_FMT_BAYER_RGGB16BE \
  689. || (x)==AV_PIX_FMT_BAYER_GBRG8 \
  690. || (x)==AV_PIX_FMT_BAYER_GBRG16LE \
  691. || (x)==AV_PIX_FMT_BAYER_GBRG16BE \
  692. || (x)==AV_PIX_FMT_BAYER_GRBG8 \
  693. || (x)==AV_PIX_FMT_BAYER_GRBG16LE \
  694. || (x)==AV_PIX_FMT_BAYER_GRBG16BE \
  695. )
  696. #define isAnyRGB(x) \
  697. ( \
  698. isBayer(x) || \
  699. isRGBinInt(x) || \
  700. isBGRinInt(x) || \
  701. isRGB(x) \
  702. )
  703. static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
  704. {
  705. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  706. av_assert0(desc);
  707. if (pix_fmt == AV_PIX_FMT_PAL8)
  708. return 1;
  709. return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  710. }
  711. #if 1
  712. #define isPacked(x) ( \
  713. (x)==AV_PIX_FMT_PAL8 \
  714. || (x)==AV_PIX_FMT_YUYV422 \
  715. || (x)==AV_PIX_FMT_YVYU422 \
  716. || (x)==AV_PIX_FMT_UYVY422 \
  717. || (x)==AV_PIX_FMT_YA8 \
  718. || (x)==AV_PIX_FMT_YA16LE \
  719. || (x)==AV_PIX_FMT_YA16BE \
  720. || isRGBinInt(x) \
  721. || isBGRinInt(x) \
  722. )
  723. #else
  724. static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
  725. {
  726. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  727. av_assert0(desc);
  728. return ((desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
  729. pix_fmt == AV_PIX_FMT_PAL8);
  730. }
  731. #endif
  732. static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
  733. {
  734. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  735. av_assert0(desc);
  736. return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  737. }
  738. static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
  739. {
  740. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  741. av_assert0(desc);
  742. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
  743. }
  744. static av_always_inline int isPlanarRGB(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)) ==
  749. (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
  750. }
  751. static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
  752. {
  753. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  754. av_assert0(desc);
  755. return (desc->flags & AV_PIX_FMT_FLAG_PAL) || (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL);
  756. }
  757. extern const uint64_t ff_dither4[2];
  758. extern const uint64_t ff_dither8[2];
  759. extern const uint8_t ff_dither_2x2_4[3][8];
  760. extern const uint8_t ff_dither_2x2_8[3][8];
  761. extern const uint8_t ff_dither_4x4_16[5][8];
  762. extern const uint8_t ff_dither_8x8_32[9][8];
  763. extern const uint8_t ff_dither_8x8_73[9][8];
  764. extern const uint8_t ff_dither_8x8_128[9][8];
  765. extern const uint8_t ff_dither_8x8_220[9][8];
  766. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  767. extern const AVClass sws_context_class;
  768. /**
  769. * Set c->swscale to an unscaled converter if one exists for the specific
  770. * source and destination formats, bit depths, flags, etc.
  771. */
  772. void ff_get_unscaled_swscale(SwsContext *c);
  773. void ff_get_unscaled_swscale_ppc(SwsContext *c);
  774. void ff_get_unscaled_swscale_arm(SwsContext *c);
  775. /**
  776. * Return function pointer to fastest main scaler path function depending
  777. * on architecture and available optimizations.
  778. */
  779. SwsFunc ff_getSwsFunc(SwsContext *c);
  780. void ff_sws_init_input_funcs(SwsContext *c);
  781. void ff_sws_init_output_funcs(SwsContext *c,
  782. yuv2planar1_fn *yuv2plane1,
  783. yuv2planarX_fn *yuv2planeX,
  784. yuv2interleavedX_fn *yuv2nv12cX,
  785. yuv2packed1_fn *yuv2packed1,
  786. yuv2packed2_fn *yuv2packed2,
  787. yuv2packedX_fn *yuv2packedX,
  788. yuv2anyX_fn *yuv2anyX);
  789. void ff_sws_init_swscale_ppc(SwsContext *c);
  790. void ff_sws_init_swscale_x86(SwsContext *c);
  791. void ff_hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  792. const uint8_t *src, int srcW, int xInc);
  793. void ff_hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  794. int dstWidth, const uint8_t *src1,
  795. const uint8_t *src2, int srcW, int xInc);
  796. int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
  797. int16_t *filter, int32_t *filterPos,
  798. int numSplits);
  799. void ff_hyscale_fast_mmxext(SwsContext *c, int16_t *dst,
  800. int dstWidth, const uint8_t *src,
  801. int srcW, int xInc);
  802. void ff_hcscale_fast_mmxext(SwsContext *c, int16_t *dst1, int16_t *dst2,
  803. int dstWidth, const uint8_t *src1,
  804. const uint8_t *src2, int srcW, int xInc);
  805. static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
  806. int alpha, int bits, const int big_endian)
  807. {
  808. int i, j;
  809. uint8_t *ptr = plane + stride * y;
  810. int v = alpha ? 0xFFFF>>(15-bits) : (1<<bits);
  811. for (i = 0; i < height; i++) {
  812. #define FILL(wfunc) \
  813. for (j = 0; j < width; j++) {\
  814. wfunc(ptr+2*j, v);\
  815. }
  816. if (big_endian) {
  817. FILL(AV_WB16);
  818. } else {
  819. FILL(AV_WL16);
  820. }
  821. ptr += stride;
  822. }
  823. }
  824. #endif /* SWSCALE_SWSCALE_INTERNAL_H */