swscale_internal.h 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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 SWS_MAX_FILTER_SIZE
  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. #define DITHER32_INT (11*8+4*4*MAX_FILTER_SIZE*3+80) // value equal to above, used for checking that the struct hasnt been changed by mistake
  403. DECLARE_ALIGNED(8, uint64_t, redDither);
  404. DECLARE_ALIGNED(8, uint64_t, greenDither);
  405. DECLARE_ALIGNED(8, uint64_t, blueDither);
  406. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  407. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  408. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  409. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  410. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  411. DECLARE_ALIGNED(8, uint64_t, yOffset);
  412. DECLARE_ALIGNED(8, uint64_t, uOffset);
  413. DECLARE_ALIGNED(8, uint64_t, vOffset);
  414. int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
  415. int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
  416. int dstW; ///< Width of destination luma/alpha planes.
  417. DECLARE_ALIGNED(8, uint64_t, esp);
  418. DECLARE_ALIGNED(8, uint64_t, vRounder);
  419. DECLARE_ALIGNED(8, uint64_t, u_temp);
  420. DECLARE_ALIGNED(8, uint64_t, v_temp);
  421. DECLARE_ALIGNED(8, uint64_t, y_temp);
  422. int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
  423. // alignment of these values is not necessary, but merely here
  424. // to maintain the same offset across x8632 and x86-64. Once we
  425. // use proper offset macros in the asm, they can be removed.
  426. DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
  427. DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
  428. DECLARE_ALIGNED(8, uint16_t, dither16)[8];
  429. DECLARE_ALIGNED(8, uint32_t, dither32)[8];
  430. const uint8_t *chrDither8, *lumDither8;
  431. #if HAVE_ALTIVEC
  432. vector signed short CY;
  433. vector signed short CRV;
  434. vector signed short CBU;
  435. vector signed short CGU;
  436. vector signed short CGV;
  437. vector signed short OY;
  438. vector unsigned short CSHIFT;
  439. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  440. #endif
  441. int use_mmx_vfilter;
  442. /* pre defined color-spaces gamma */
  443. #define XYZ_GAMMA (2.6f)
  444. #define RGB_GAMMA (2.2f)
  445. int16_t *xyzgamma;
  446. int16_t *rgbgamma;
  447. int16_t *xyzgammainv;
  448. int16_t *rgbgammainv;
  449. int16_t xyz2rgb_matrix[3][4];
  450. int16_t rgb2xyz_matrix[3][4];
  451. /* function pointers for swscale() */
  452. yuv2planar1_fn yuv2plane1;
  453. yuv2planarX_fn yuv2planeX;
  454. yuv2interleavedX_fn yuv2nv12cX;
  455. yuv2packed1_fn yuv2packed1;
  456. yuv2packed2_fn yuv2packed2;
  457. yuv2packedX_fn yuv2packedX;
  458. yuv2anyX_fn yuv2anyX;
  459. /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
  460. void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  461. int width, uint32_t *pal);
  462. /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  463. void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  464. int width, uint32_t *pal);
  465. /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  466. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  467. const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  468. int width, uint32_t *pal);
  469. /**
  470. * Functions to read planar input, such as planar RGB, and convert
  471. * internally to Y/UV/A.
  472. */
  473. /** @{ */
  474. void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  475. void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
  476. int width, int32_t *rgb2yuv);
  477. void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  478. /** @} */
  479. /**
  480. * Scale one horizontal line of input data using a bilinear filter
  481. * to produce one line of output data. Compared to SwsContext->hScale(),
  482. * please take note of the following caveats when using these:
  483. * - Scaling is done using only 7bit instead of 14bit coefficients.
  484. * - You can use no more than 5 input pixels to produce 4 output
  485. * pixels. Therefore, this filter should not be used for downscaling
  486. * by more than ~20% in width (because that equals more than 5/4th
  487. * downscaling and thus more than 5 pixels input per 4 pixels output).
  488. * - In general, bilinear filters create artifacts during downscaling
  489. * (even when <20%), because one output pixel will span more than one
  490. * input pixel, and thus some pixels will need edges of both neighbor
  491. * pixels to interpolate the output pixel. Since you can use at most
  492. * two input pixels per output pixel in bilinear scaling, this is
  493. * impossible and thus downscaling by any size will create artifacts.
  494. * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
  495. * in SwsContext->flags.
  496. */
  497. /** @{ */
  498. void (*hyscale_fast)(struct SwsContext *c,
  499. int16_t *dst, int dstWidth,
  500. const uint8_t *src, int srcW, int xInc);
  501. void (*hcscale_fast)(struct SwsContext *c,
  502. int16_t *dst1, int16_t *dst2, int dstWidth,
  503. const uint8_t *src1, const uint8_t *src2,
  504. int srcW, int xInc);
  505. /** @} */
  506. /**
  507. * Scale one horizontal line of input data using a filter over the input
  508. * lines, to produce one (differently sized) line of output data.
  509. *
  510. * @param dst pointer to destination buffer for horizontally scaled
  511. * data. If the number of bits per component of one
  512. * destination pixel (SwsContext->dstBpc) is <= 10, data
  513. * will be 15bpc in 16bits (int16_t) width. Else (i.e.
  514. * SwsContext->dstBpc == 16), data will be 19bpc in
  515. * 32bits (int32_t) width.
  516. * @param dstW width of destination image
  517. * @param src pointer to source data to be scaled. If the number of
  518. * bits per component of a source pixel (SwsContext->srcBpc)
  519. * is 8, this is 8bpc in 8bits (uint8_t) width. Else
  520. * (i.e. SwsContext->dstBpc > 8), this is native depth
  521. * in 16bits (uint16_t) width. In other words, for 9-bit
  522. * YUV input, this is 9bpc, for 10-bit YUV input, this is
  523. * 10bpc, and for 16-bit RGB or YUV, this is 16bpc.
  524. * @param filter filter coefficients to be used per output pixel for
  525. * scaling. This contains 14bpp filtering coefficients.
  526. * Guaranteed to contain dstW * filterSize entries.
  527. * @param filterPos position of the first input pixel to be used for
  528. * each output pixel during scaling. Guaranteed to
  529. * contain dstW entries.
  530. * @param filterSize the number of input coefficients to be used (and
  531. * thus the number of input pixels to be used) for
  532. * creating a single output pixel. Is aligned to 4
  533. * (and input coefficients thus padded with zeroes)
  534. * to simplify creating SIMD code.
  535. */
  536. /** @{ */
  537. void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
  538. const uint8_t *src, const int16_t *filter,
  539. const int32_t *filterPos, int filterSize);
  540. void (*hcScale)(struct SwsContext *c, int16_t *dst, int dstW,
  541. const uint8_t *src, const int16_t *filter,
  542. const int32_t *filterPos, int filterSize);
  543. /** @} */
  544. /// Color range conversion function for luma plane if needed.
  545. void (*lumConvertRange)(int16_t *dst, int width);
  546. /// Color range conversion function for chroma planes if needed.
  547. void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
  548. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  549. SwsDither dither;
  550. } SwsContext;
  551. //FIXME check init (where 0)
  552. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  553. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  554. int fullRange, int brightness,
  555. int contrast, int saturation);
  556. void ff_yuv2rgb_init_tables_ppc(SwsContext *c, const int inv_table[4],
  557. int brightness, int contrast, int saturation);
  558. void updateMMXDitherTables(SwsContext *c, int dstY, int lumBufIndex, int chrBufIndex,
  559. int lastInLumBuf, int lastInChrBuf);
  560. av_cold void ff_sws_init_range_convert(SwsContext *c);
  561. SwsFunc ff_yuv2rgb_init_x86(SwsContext *c);
  562. SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c);
  563. #if FF_API_SWS_FORMAT_NAME
  564. /**
  565. * @deprecated Use av_get_pix_fmt_name() instead.
  566. */
  567. attribute_deprecated
  568. const char *sws_format_name(enum AVPixelFormat format);
  569. #endif
  570. static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
  571. {
  572. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  573. av_assert0(desc);
  574. return desc->comp[0].depth_minus1 == 15;
  575. }
  576. static av_always_inline int is9_OR_10BPS(enum AVPixelFormat pix_fmt)
  577. {
  578. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  579. av_assert0(desc);
  580. return desc->comp[0].depth_minus1 >= 8 && desc->comp[0].depth_minus1 <= 13;
  581. }
  582. #define isNBPS(x) is9_OR_10BPS(x)
  583. static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
  584. {
  585. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  586. av_assert0(desc);
  587. return desc->flags & AV_PIX_FMT_FLAG_BE;
  588. }
  589. static av_always_inline int isYUV(enum AVPixelFormat pix_fmt)
  590. {
  591. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  592. av_assert0(desc);
  593. return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
  594. }
  595. static av_always_inline int isPlanarYUV(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_PLANAR) && isYUV(pix_fmt));
  600. }
  601. static av_always_inline int isRGB(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);
  606. }
  607. #if 0 // FIXME
  608. #define isGray(x) \
  609. (!(av_pix_fmt_desc_get(x)->flags & AV_PIX_FMT_FLAG_PAL) && \
  610. av_pix_fmt_desc_get(x)->nb_components <= 2)
  611. #else
  612. #define isGray(x) \
  613. ((x) == AV_PIX_FMT_GRAY8 || \
  614. (x) == AV_PIX_FMT_Y400A || \
  615. (x) == AV_PIX_FMT_GRAY16BE || \
  616. (x) == AV_PIX_FMT_GRAY16LE)
  617. #endif
  618. #define isRGBinInt(x) \
  619. ( \
  620. (x) == AV_PIX_FMT_RGB48BE || \
  621. (x) == AV_PIX_FMT_RGB48LE || \
  622. (x) == AV_PIX_FMT_RGB32 || \
  623. (x) == AV_PIX_FMT_RGB32_1 || \
  624. (x) == AV_PIX_FMT_RGB24 || \
  625. (x) == AV_PIX_FMT_RGB565BE || \
  626. (x) == AV_PIX_FMT_RGB565LE || \
  627. (x) == AV_PIX_FMT_RGB555BE || \
  628. (x) == AV_PIX_FMT_RGB555LE || \
  629. (x) == AV_PIX_FMT_RGB444BE || \
  630. (x) == AV_PIX_FMT_RGB444LE || \
  631. (x) == AV_PIX_FMT_RGB8 || \
  632. (x) == AV_PIX_FMT_RGB4 || \
  633. (x) == AV_PIX_FMT_RGB4_BYTE || \
  634. (x) == AV_PIX_FMT_RGBA64BE || \
  635. (x) == AV_PIX_FMT_RGBA64LE || \
  636. (x) == AV_PIX_FMT_MONOBLACK || \
  637. (x) == AV_PIX_FMT_MONOWHITE \
  638. )
  639. #define isBGRinInt(x) \
  640. ( \
  641. (x) == AV_PIX_FMT_BGR48BE || \
  642. (x) == AV_PIX_FMT_BGR48LE || \
  643. (x) == AV_PIX_FMT_BGR32 || \
  644. (x) == AV_PIX_FMT_BGR32_1 || \
  645. (x) == AV_PIX_FMT_BGR24 || \
  646. (x) == AV_PIX_FMT_BGR565BE || \
  647. (x) == AV_PIX_FMT_BGR565LE || \
  648. (x) == AV_PIX_FMT_BGR555BE || \
  649. (x) == AV_PIX_FMT_BGR555LE || \
  650. (x) == AV_PIX_FMT_BGR444BE || \
  651. (x) == AV_PIX_FMT_BGR444LE || \
  652. (x) == AV_PIX_FMT_BGR8 || \
  653. (x) == AV_PIX_FMT_BGR4 || \
  654. (x) == AV_PIX_FMT_BGR4_BYTE || \
  655. (x) == AV_PIX_FMT_BGRA64BE || \
  656. (x) == AV_PIX_FMT_BGRA64LE || \
  657. (x) == AV_PIX_FMT_MONOBLACK || \
  658. (x) == AV_PIX_FMT_MONOWHITE \
  659. )
  660. #define isRGBinBytes(x) ( \
  661. (x) == AV_PIX_FMT_RGB48BE \
  662. || (x) == AV_PIX_FMT_RGB48LE \
  663. || (x) == AV_PIX_FMT_RGBA64BE \
  664. || (x) == AV_PIX_FMT_RGBA64LE \
  665. || (x) == AV_PIX_FMT_RGBA \
  666. || (x) == AV_PIX_FMT_ARGB \
  667. || (x) == AV_PIX_FMT_RGB24 \
  668. )
  669. #define isBGRinBytes(x) ( \
  670. (x) == AV_PIX_FMT_BGR48BE \
  671. || (x) == AV_PIX_FMT_BGR48LE \
  672. || (x) == AV_PIX_FMT_BGRA64BE \
  673. || (x) == AV_PIX_FMT_BGRA64LE \
  674. || (x) == AV_PIX_FMT_BGRA \
  675. || (x) == AV_PIX_FMT_ABGR \
  676. || (x) == AV_PIX_FMT_BGR24 \
  677. )
  678. #define isBayer(x) ( \
  679. (x)==AV_PIX_FMT_BAYER_BGGR8 \
  680. || (x)==AV_PIX_FMT_BAYER_BGGR16LE \
  681. || (x)==AV_PIX_FMT_BAYER_BGGR16BE \
  682. || (x)==AV_PIX_FMT_BAYER_RGGB8 \
  683. || (x)==AV_PIX_FMT_BAYER_RGGB16LE \
  684. || (x)==AV_PIX_FMT_BAYER_RGGB16BE \
  685. || (x)==AV_PIX_FMT_BAYER_GBRG8 \
  686. || (x)==AV_PIX_FMT_BAYER_GBRG16LE \
  687. || (x)==AV_PIX_FMT_BAYER_GBRG16BE \
  688. || (x)==AV_PIX_FMT_BAYER_GRBG8 \
  689. || (x)==AV_PIX_FMT_BAYER_GRBG16LE \
  690. || (x)==AV_PIX_FMT_BAYER_GRBG16BE \
  691. )
  692. #define isAnyRGB(x) \
  693. ( \
  694. isBayer(x) || \
  695. isRGBinInt(x) || \
  696. isBGRinInt(x) || \
  697. isRGB(x) \
  698. )
  699. static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
  700. {
  701. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  702. av_assert0(desc);
  703. if (pix_fmt == AV_PIX_FMT_PAL8)
  704. return 1;
  705. return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  706. }
  707. #if 1
  708. #define isPacked(x) ( \
  709. (x)==AV_PIX_FMT_PAL8 \
  710. || (x)==AV_PIX_FMT_YUYV422 \
  711. || (x)==AV_PIX_FMT_YVYU422 \
  712. || (x)==AV_PIX_FMT_UYVY422 \
  713. || (x)==AV_PIX_FMT_Y400A \
  714. || isRGBinInt(x) \
  715. || isBGRinInt(x) \
  716. )
  717. #else
  718. static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
  719. {
  720. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  721. av_assert0(desc);
  722. return ((desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
  723. pix_fmt == AV_PIX_FMT_PAL8);
  724. }
  725. #endif
  726. static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
  727. {
  728. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  729. av_assert0(desc);
  730. return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  731. }
  732. static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
  733. {
  734. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  735. av_assert0(desc);
  736. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
  737. }
  738. static av_always_inline int isPlanarRGB(enum AVPixelFormat pix_fmt)
  739. {
  740. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  741. av_assert0(desc);
  742. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) ==
  743. (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
  744. }
  745. static av_always_inline int usePal(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_PAL) || (desc->flags & AV_PIX_FMT_FLAG_PSEUDOPAL);
  750. }
  751. extern const uint64_t ff_dither4[2];
  752. extern const uint64_t ff_dither8[2];
  753. extern const uint8_t ff_dither_2x2_4[3][8];
  754. extern const uint8_t ff_dither_2x2_8[3][8];
  755. extern const uint8_t ff_dither_4x4_16[5][8];
  756. extern const uint8_t ff_dither_8x8_32[9][8];
  757. extern const uint8_t ff_dither_8x8_73[9][8];
  758. extern const uint8_t ff_dither_8x8_128[9][8];
  759. extern const uint8_t ff_dither_8x8_220[9][8];
  760. extern const int32_t ff_yuv2rgb_coeffs[8][4];
  761. extern const AVClass sws_context_class;
  762. /**
  763. * Set c->swscale to an unscaled converter if one exists for the specific
  764. * source and destination formats, bit depths, flags, etc.
  765. */
  766. void ff_get_unscaled_swscale(SwsContext *c);
  767. void ff_get_unscaled_swscale_ppc(SwsContext *c);
  768. void ff_get_unscaled_swscale_arm(SwsContext *c);
  769. /**
  770. * Return function pointer to fastest main scaler path function depending
  771. * on architecture and available optimizations.
  772. */
  773. SwsFunc ff_getSwsFunc(SwsContext *c);
  774. void ff_sws_init_input_funcs(SwsContext *c);
  775. void ff_sws_init_output_funcs(SwsContext *c,
  776. yuv2planar1_fn *yuv2plane1,
  777. yuv2planarX_fn *yuv2planeX,
  778. yuv2interleavedX_fn *yuv2nv12cX,
  779. yuv2packed1_fn *yuv2packed1,
  780. yuv2packed2_fn *yuv2packed2,
  781. yuv2packedX_fn *yuv2packedX,
  782. yuv2anyX_fn *yuv2anyX);
  783. void ff_sws_init_swscale_ppc(SwsContext *c);
  784. void ff_sws_init_swscale_x86(SwsContext *c);
  785. static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
  786. int alpha, int bits, const int big_endian)
  787. {
  788. int i, j;
  789. uint8_t *ptr = plane + stride * y;
  790. int v = alpha ? 0xFFFF>>(15-bits) : (1<<bits);
  791. for (i = 0; i < height; i++) {
  792. #define FILL(wfunc) \
  793. for (j = 0; j < width; j++) {\
  794. wfunc(ptr+2*j, v);\
  795. }
  796. if (big_endian) {
  797. FILL(AV_WB16);
  798. } else {
  799. FILL(AV_WL16);
  800. }
  801. ptr += stride;
  802. }
  803. }
  804. #endif /* SWSCALE_SWSCALE_INTERNAL_H */