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.

876 lines
38KB

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