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.

896 lines
39KB

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