swscale_internal.h 39 KB

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