swscale_internal.h 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  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 <stdatomic.h>
  23. #include "config.h"
  24. #include "libavutil/avassert.h"
  25. #include "libavutil/common.h"
  26. #include "libavutil/frame.h"
  27. #include "libavutil/intreadwrite.h"
  28. #include "libavutil/log.h"
  29. #include "libavutil/mem_internal.h"
  30. #include "libavutil/pixfmt.h"
  31. #include "libavutil/pixdesc.h"
  32. #include "libavutil/slicethread.h"
  33. #if HAVE_ALTIVEC
  34. #include "libavutil/ppc/util_altivec.h"
  35. #endif
  36. #include "libavutil/half2float.h"
  37. #define STR(s) AV_TOSTRING(s) // AV_STRINGIFY is too long
  38. #define YUVRGB_TABLE_HEADROOM 512
  39. #define YUVRGB_TABLE_LUMA_HEADROOM 512
  40. #define MAX_FILTER_SIZE SWS_MAX_FILTER_SIZE
  41. #define DITHER1XBPP
  42. #if HAVE_BIGENDIAN
  43. #define ALT32_CORR (-1)
  44. #else
  45. #define ALT32_CORR 1
  46. #endif
  47. #if ARCH_X86_64
  48. # define APCK_PTR2 8
  49. # define APCK_COEF 16
  50. # define APCK_SIZE 24
  51. #else
  52. # define APCK_PTR2 4
  53. # define APCK_COEF 8
  54. # define APCK_SIZE 16
  55. #endif
  56. #define RETCODE_USE_CASCADE -12345
  57. struct SwsContext;
  58. typedef enum SwsDither {
  59. SWS_DITHER_NONE = 0,
  60. SWS_DITHER_AUTO,
  61. SWS_DITHER_BAYER,
  62. SWS_DITHER_ED,
  63. SWS_DITHER_A_DITHER,
  64. SWS_DITHER_X_DITHER,
  65. NB_SWS_DITHER,
  66. } SwsDither;
  67. typedef enum SwsAlphaBlend {
  68. SWS_ALPHA_BLEND_NONE = 0,
  69. SWS_ALPHA_BLEND_UNIFORM,
  70. SWS_ALPHA_BLEND_CHECKERBOARD,
  71. SWS_ALPHA_BLEND_NB,
  72. } SwsAlphaBlend;
  73. typedef struct Range {
  74. unsigned int start;
  75. unsigned int len;
  76. } Range;
  77. typedef struct RangeList {
  78. Range *ranges;
  79. unsigned int nb_ranges;
  80. int ranges_allocated;
  81. } RangeList;
  82. int ff_range_add(RangeList *r, unsigned int start, unsigned int len);
  83. typedef int (*SwsFunc)(struct SwsContext *context, const uint8_t *src[],
  84. int srcStride[], int srcSliceY, int srcSliceH,
  85. uint8_t *dst[], int dstStride[]);
  86. /**
  87. * Write one line of horizontally scaled data to planar output
  88. * without any additional vertical scaling (or point-scaling).
  89. *
  90. * @param src scaled source data, 15 bits for 8-10-bit output,
  91. * 19 bits for 16-bit output (in int32_t)
  92. * @param dest pointer to the output plane. For >8-bit
  93. * output, this is in uint16_t
  94. * @param dstW width of destination in pixels
  95. * @param dither ordered dither array of type int16_t and size 8
  96. * @param offset Dither offset
  97. */
  98. typedef void (*yuv2planar1_fn)(const int16_t *src, uint8_t *dest, int dstW,
  99. const uint8_t *dither, int offset);
  100. /**
  101. * Write one line of horizontally scaled data to planar output
  102. * with multi-point vertical scaling between input pixels.
  103. *
  104. * @param filter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
  105. * @param src scaled luma (Y) or alpha (A) source data, 15 bits for
  106. * 8-10-bit output, 19 bits for 16-bit output (in int32_t)
  107. * @param filterSize number of vertical input lines to scale
  108. * @param dest pointer to output plane. For >8-bit
  109. * output, this is in uint16_t
  110. * @param dstW width of destination pixels
  111. * @param offset Dither offset
  112. */
  113. typedef void (*yuv2planarX_fn)(const int16_t *filter, int filterSize,
  114. const int16_t **src, uint8_t *dest, int dstW,
  115. const uint8_t *dither, int offset);
  116. /**
  117. * Write one line of horizontally scaled chroma to interleaved output
  118. * with multi-point vertical scaling between input pixels.
  119. *
  120. * @param dstFormat destination pixel format
  121. * @param chrDither ordered dither array of type uint8_t and size 8
  122. * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
  123. * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit
  124. * output, 19 bits for 16-bit output (in int32_t)
  125. * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit
  126. * output, 19 bits for 16-bit output (in int32_t)
  127. * @param chrFilterSize number of vertical chroma input lines to scale
  128. * @param dest pointer to the output plane. For >8-bit
  129. * output, this is in uint16_t
  130. * @param dstW width of chroma planes
  131. */
  132. typedef void (*yuv2interleavedX_fn)(enum AVPixelFormat dstFormat,
  133. const uint8_t *chrDither,
  134. const int16_t *chrFilter,
  135. int chrFilterSize,
  136. const int16_t **chrUSrc,
  137. const int16_t **chrVSrc,
  138. uint8_t *dest, int dstW);
  139. /**
  140. * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
  141. * output without any additional vertical scaling (or point-scaling). Note
  142. * that this function may do chroma scaling, see the "uvalpha" argument.
  143. *
  144. * @param c SWS scaling context
  145. * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
  146. * 19 bits for 16-bit output (in int32_t)
  147. * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
  148. * 19 bits for 16-bit output (in int32_t)
  149. * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
  150. * 19 bits for 16-bit output (in int32_t)
  151. * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
  152. * 19 bits for 16-bit output (in int32_t)
  153. * @param dest pointer to the output plane. For 16-bit output, this is
  154. * uint16_t
  155. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  156. * to write into dest[]
  157. * @param uvalpha chroma scaling coefficient for the second line of chroma
  158. * pixels, either 2048 or 0. If 0, one chroma input is used
  159. * for 2 output pixels (or if the SWS_FLAG_FULL_CHR_INT flag
  160. * is set, it generates 1 output pixel). If 2048, two chroma
  161. * input pixels should be averaged for 2 output pixels (this
  162. * only happens if SWS_FLAG_FULL_CHR_INT is not set)
  163. * @param y vertical line number for this output. This does not need
  164. * to be used to calculate the offset in the destination,
  165. * but can be used to generate comfort noise using dithering
  166. * for some output formats.
  167. */
  168. typedef void (*yuv2packed1_fn)(struct SwsContext *c, const int16_t *lumSrc,
  169. const int16_t *chrUSrc[2],
  170. const int16_t *chrVSrc[2],
  171. const int16_t *alpSrc, uint8_t *dest,
  172. int dstW, int uvalpha, int y);
  173. /**
  174. * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
  175. * output by doing bilinear scaling between two input lines.
  176. *
  177. * @param c SWS scaling context
  178. * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
  179. * 19 bits for 16-bit output (in int32_t)
  180. * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
  181. * 19 bits for 16-bit output (in int32_t)
  182. * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
  183. * 19 bits for 16-bit output (in int32_t)
  184. * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
  185. * 19 bits for 16-bit output (in int32_t)
  186. * @param dest pointer to the output plane. For 16-bit output, this is
  187. * uint16_t
  188. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  189. * to write into dest[]
  190. * @param yalpha luma/alpha scaling coefficients for the second input line.
  191. * The first line's coefficients can be calculated by using
  192. * 4096 - yalpha
  193. * @param uvalpha chroma scaling coefficient for the second input line. The
  194. * first line's coefficients can be calculated by using
  195. * 4096 - uvalpha
  196. * @param y vertical line number for this output. This does not need
  197. * to be used to calculate the offset in the destination,
  198. * but can be used to generate comfort noise using dithering
  199. * for some output formats.
  200. */
  201. typedef void (*yuv2packed2_fn)(struct SwsContext *c, const int16_t *lumSrc[2],
  202. const int16_t *chrUSrc[2],
  203. const int16_t *chrVSrc[2],
  204. const int16_t *alpSrc[2],
  205. uint8_t *dest,
  206. int dstW, int yalpha, int uvalpha, int y);
  207. /**
  208. * Write one line of horizontally scaled Y/U/V/A to packed-pixel YUV/RGB
  209. * output by doing multi-point vertical scaling between input pixels.
  210. *
  211. * @param c SWS scaling context
  212. * @param lumFilter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
  213. * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
  214. * 19 bits for 16-bit output (in int32_t)
  215. * @param lumFilterSize number of vertical luma/alpha input lines to scale
  216. * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
  217. * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
  218. * 19 bits for 16-bit output (in int32_t)
  219. * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
  220. * 19 bits for 16-bit output (in int32_t)
  221. * @param chrFilterSize number of vertical chroma input lines to scale
  222. * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
  223. * 19 bits for 16-bit output (in int32_t)
  224. * @param dest pointer to the output plane. For 16-bit output, this is
  225. * uint16_t
  226. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  227. * to write into dest[]
  228. * @param y vertical line number for this output. This does not need
  229. * to be used to calculate the offset in the destination,
  230. * but can be used to generate comfort noise using dithering
  231. * or some output formats.
  232. */
  233. typedef void (*yuv2packedX_fn)(struct SwsContext *c, const int16_t *lumFilter,
  234. const int16_t **lumSrc, int lumFilterSize,
  235. const int16_t *chrFilter,
  236. const int16_t **chrUSrc,
  237. const int16_t **chrVSrc, int chrFilterSize,
  238. const int16_t **alpSrc, uint8_t *dest,
  239. int dstW, int y);
  240. /**
  241. * Write one line of horizontally scaled Y/U/V/A to YUV/RGB
  242. * output by doing multi-point vertical scaling between input pixels.
  243. *
  244. * @param c SWS scaling context
  245. * @param lumFilter vertical luma/alpha scaling coefficients, 12 bits [0,4096]
  246. * @param lumSrc scaled luma (Y) source data, 15 bits for 8-10-bit output,
  247. * 19 bits for 16-bit output (in int32_t)
  248. * @param lumFilterSize number of vertical luma/alpha input lines to scale
  249. * @param chrFilter vertical chroma scaling coefficients, 12 bits [0,4096]
  250. * @param chrUSrc scaled chroma (U) source data, 15 bits for 8-10-bit output,
  251. * 19 bits for 16-bit output (in int32_t)
  252. * @param chrVSrc scaled chroma (V) source data, 15 bits for 8-10-bit output,
  253. * 19 bits for 16-bit output (in int32_t)
  254. * @param chrFilterSize number of vertical chroma input lines to scale
  255. * @param alpSrc scaled alpha (A) source data, 15 bits for 8-10-bit output,
  256. * 19 bits for 16-bit output (in int32_t)
  257. * @param dest pointer to the output planes. For 16-bit output, this is
  258. * uint16_t
  259. * @param dstW width of lumSrc and alpSrc in pixels, number of pixels
  260. * to write into dest[]
  261. * @param y vertical line number for this output. This does not need
  262. * to be used to calculate the offset in the destination,
  263. * but can be used to generate comfort noise using dithering
  264. * or some output formats.
  265. */
  266. typedef void (*yuv2anyX_fn)(struct SwsContext *c, const int16_t *lumFilter,
  267. const int16_t **lumSrc, int lumFilterSize,
  268. const int16_t *chrFilter,
  269. const int16_t **chrUSrc,
  270. const int16_t **chrVSrc, int chrFilterSize,
  271. const int16_t **alpSrc, uint8_t **dest,
  272. int dstW, int y);
  273. struct SwsSlice;
  274. struct SwsFilterDescriptor;
  275. /* This struct should be aligned on at least a 32-byte boundary. */
  276. typedef struct SwsContext {
  277. /**
  278. * info on struct for av_log
  279. */
  280. const AVClass *av_class;
  281. struct SwsContext *parent;
  282. AVSliceThread *slicethread;
  283. struct SwsContext **slice_ctx;
  284. int *slice_err;
  285. int nb_slice_ctx;
  286. // values passed to current sws_receive_slice() call
  287. int dst_slice_start;
  288. int dst_slice_height;
  289. /**
  290. * Note that src, dst, srcStride, dstStride will be copied in the
  291. * sws_scale() wrapper so they can be freely modified here.
  292. */
  293. SwsFunc convert_unscaled;
  294. int srcW; ///< Width of source luma/alpha planes.
  295. int srcH; ///< Height of source luma/alpha planes.
  296. int dstH; ///< Height of destination luma/alpha planes.
  297. int chrSrcW; ///< Width of source chroma planes.
  298. int chrSrcH; ///< Height of source chroma planes.
  299. int chrDstW; ///< Width of destination chroma planes.
  300. int chrDstH; ///< Height of destination chroma planes.
  301. int lumXInc, chrXInc;
  302. int lumYInc, chrYInc;
  303. enum AVPixelFormat dstFormat; ///< Destination pixel format.
  304. enum AVPixelFormat srcFormat; ///< Source pixel format.
  305. int dstFormatBpp; ///< Number of bits per pixel of the destination pixel format.
  306. int srcFormatBpp; ///< Number of bits per pixel of the source pixel format.
  307. int dstBpc, srcBpc;
  308. int chrSrcHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in source image.
  309. int chrSrcVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in source image.
  310. int chrDstHSubSample; ///< Binary logarithm of horizontal subsampling factor between luma/alpha and chroma planes in destination image.
  311. int chrDstVSubSample; ///< Binary logarithm of vertical subsampling factor between luma/alpha and chroma planes in destination image.
  312. int vChrDrop; ///< Binary logarithm of extra vertical subsampling factor in source image chroma planes specified by user.
  313. int sliceDir; ///< Direction that slices are fed to the scaler (1 = top-to-bottom, -1 = bottom-to-top).
  314. int nb_threads; ///< Number of threads used for scaling
  315. double param[2]; ///< Input parameters for scaling algorithms that need them.
  316. AVFrame *frame_src;
  317. AVFrame *frame_dst;
  318. RangeList src_ranges;
  319. /* The cascaded_* fields allow spliting a scaler task into multiple
  320. * sequential steps, this is for example used to limit the maximum
  321. * downscaling factor that needs to be supported in one scaler.
  322. */
  323. struct SwsContext *cascaded_context[3];
  324. int cascaded_tmpStride[4];
  325. uint8_t *cascaded_tmp[4];
  326. int cascaded1_tmpStride[4];
  327. uint8_t *cascaded1_tmp[4];
  328. int cascaded_mainindex;
  329. double gamma_value;
  330. int gamma_flag;
  331. int is_internal_gamma;
  332. uint16_t *gamma;
  333. uint16_t *inv_gamma;
  334. int numDesc;
  335. int descIndex[2];
  336. int numSlice;
  337. struct SwsSlice *slice;
  338. struct SwsFilterDescriptor *desc;
  339. uint32_t pal_yuv[256];
  340. uint32_t pal_rgb[256];
  341. float uint2float_lut[256];
  342. /**
  343. * @name Scaled horizontal lines ring buffer.
  344. * The horizontal scaler keeps just enough scaled lines in a ring buffer
  345. * so they may be passed to the vertical scaler. The pointers to the
  346. * allocated buffers for each line are duplicated in sequence in the ring
  347. * buffer to simplify indexing and avoid wrapping around between lines
  348. * inside the vertical scaler code. The wrapping is done before the
  349. * vertical scaler is called.
  350. */
  351. //@{
  352. int lastInLumBuf; ///< Last scaled horizontal luma/alpha line from source in the ring buffer.
  353. int lastInChrBuf; ///< Last scaled horizontal chroma line from source in the ring buffer.
  354. //@}
  355. uint8_t *formatConvBuffer;
  356. int needAlpha;
  357. /**
  358. * @name Horizontal and vertical filters.
  359. * To better understand the following fields, here is a pseudo-code of
  360. * their usage in filtering a horizontal line:
  361. * @code
  362. * for (i = 0; i < width; i++) {
  363. * dst[i] = 0;
  364. * for (j = 0; j < filterSize; j++)
  365. * dst[i] += src[ filterPos[i] + j ] * filter[ filterSize * i + j ];
  366. * dst[i] >>= FRAC_BITS; // The actual implementation is fixed-point.
  367. * }
  368. * @endcode
  369. */
  370. //@{
  371. int16_t *hLumFilter; ///< Array of horizontal filter coefficients for luma/alpha planes.
  372. int16_t *hChrFilter; ///< Array of horizontal filter coefficients for chroma planes.
  373. int16_t *vLumFilter; ///< Array of vertical filter coefficients for luma/alpha planes.
  374. int16_t *vChrFilter; ///< Array of vertical filter coefficients for chroma planes.
  375. int32_t *hLumFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for luma/alpha planes.
  376. int32_t *hChrFilterPos; ///< Array of horizontal filter starting positions for each dst[i] for chroma planes.
  377. int32_t *vLumFilterPos; ///< Array of vertical filter starting positions for each dst[i] for luma/alpha planes.
  378. int32_t *vChrFilterPos; ///< Array of vertical filter starting positions for each dst[i] for chroma planes.
  379. int hLumFilterSize; ///< Horizontal filter size for luma/alpha pixels.
  380. int hChrFilterSize; ///< Horizontal filter size for chroma pixels.
  381. int vLumFilterSize; ///< Vertical filter size for luma/alpha pixels.
  382. int vChrFilterSize; ///< Vertical filter size for chroma pixels.
  383. //@}
  384. int lumMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for luma/alpha planes.
  385. int chrMmxextFilterCodeSize; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code size for chroma planes.
  386. uint8_t *lumMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for luma/alpha planes.
  387. uint8_t *chrMmxextFilterCode; ///< Runtime-generated MMXEXT horizontal fast bilinear scaler code for chroma planes.
  388. int canMMXEXTBeUsed;
  389. int warned_unuseable_bilinear;
  390. int dstY; ///< Last destination vertical line output from last slice.
  391. int flags; ///< Flags passed by the user to select scaler algorithm, optimizations, subsampling, etc...
  392. void *yuvTable; // pointer to the yuv->rgb table start so it can be freed()
  393. // alignment ensures the offset can be added in a single
  394. // instruction on e.g. ARM
  395. DECLARE_ALIGNED(16, int, table_gV)[256 + 2*YUVRGB_TABLE_HEADROOM];
  396. uint8_t *table_rV[256 + 2*YUVRGB_TABLE_HEADROOM];
  397. uint8_t *table_gU[256 + 2*YUVRGB_TABLE_HEADROOM];
  398. uint8_t *table_bU[256 + 2*YUVRGB_TABLE_HEADROOM];
  399. 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
  400. #define RY_IDX 0
  401. #define GY_IDX 1
  402. #define BY_IDX 2
  403. #define RU_IDX 3
  404. #define GU_IDX 4
  405. #define BU_IDX 5
  406. #define RV_IDX 6
  407. #define GV_IDX 7
  408. #define BV_IDX 8
  409. #define RGB2YUV_SHIFT 15
  410. int *dither_error[4];
  411. //Colorspace stuff
  412. int contrast, brightness, saturation; // for sws_getColorspaceDetails
  413. int srcColorspaceTable[4];
  414. int dstColorspaceTable[4];
  415. int srcRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (source image).
  416. int dstRange; ///< 0 = MPG YUV range, 1 = JPG YUV range (destination image).
  417. int src0Alpha;
  418. int dst0Alpha;
  419. int srcXYZ;
  420. int dstXYZ;
  421. int src_h_chr_pos;
  422. int dst_h_chr_pos;
  423. int src_v_chr_pos;
  424. int dst_v_chr_pos;
  425. int yuv2rgb_y_offset;
  426. int yuv2rgb_y_coeff;
  427. int yuv2rgb_v2r_coeff;
  428. int yuv2rgb_v2g_coeff;
  429. int yuv2rgb_u2g_coeff;
  430. int yuv2rgb_u2b_coeff;
  431. #define RED_DITHER "0*8"
  432. #define GREEN_DITHER "1*8"
  433. #define BLUE_DITHER "2*8"
  434. #define Y_COEFF "3*8"
  435. #define VR_COEFF "4*8"
  436. #define UB_COEFF "5*8"
  437. #define VG_COEFF "6*8"
  438. #define UG_COEFF "7*8"
  439. #define Y_OFFSET "8*8"
  440. #define U_OFFSET "9*8"
  441. #define V_OFFSET "10*8"
  442. #define LUM_MMX_FILTER_OFFSET "11*8"
  443. #define CHR_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)
  444. #define DSTW_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2"
  445. #define ESP_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+8"
  446. #define VROUNDER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+16"
  447. #define U_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+24"
  448. #define V_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+32"
  449. #define Y_TEMP "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+40"
  450. #define ALP_MMX_FILTER_OFFSET "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*2+48"
  451. #define UV_OFF_PX "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+48"
  452. #define UV_OFF_BYTE "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+56"
  453. #define DITHER16 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+64"
  454. #define DITHER32 "11*8+4*4*"AV_STRINGIFY(MAX_FILTER_SIZE)"*3+80"
  455. #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
  456. DECLARE_ALIGNED(8, uint64_t, redDither);
  457. DECLARE_ALIGNED(8, uint64_t, greenDither);
  458. DECLARE_ALIGNED(8, uint64_t, blueDither);
  459. DECLARE_ALIGNED(8, uint64_t, yCoeff);
  460. DECLARE_ALIGNED(8, uint64_t, vrCoeff);
  461. DECLARE_ALIGNED(8, uint64_t, ubCoeff);
  462. DECLARE_ALIGNED(8, uint64_t, vgCoeff);
  463. DECLARE_ALIGNED(8, uint64_t, ugCoeff);
  464. DECLARE_ALIGNED(8, uint64_t, yOffset);
  465. DECLARE_ALIGNED(8, uint64_t, uOffset);
  466. DECLARE_ALIGNED(8, uint64_t, vOffset);
  467. int32_t lumMmxFilter[4 * MAX_FILTER_SIZE];
  468. int32_t chrMmxFilter[4 * MAX_FILTER_SIZE];
  469. int dstW; ///< Width of destination luma/alpha planes.
  470. DECLARE_ALIGNED(8, uint64_t, esp);
  471. DECLARE_ALIGNED(8, uint64_t, vRounder);
  472. DECLARE_ALIGNED(8, uint64_t, u_temp);
  473. DECLARE_ALIGNED(8, uint64_t, v_temp);
  474. DECLARE_ALIGNED(8, uint64_t, y_temp);
  475. int32_t alpMmxFilter[4 * MAX_FILTER_SIZE];
  476. // alignment of these values is not necessary, but merely here
  477. // to maintain the same offset across x8632 and x86-64. Once we
  478. // use proper offset macros in the asm, they can be removed.
  479. DECLARE_ALIGNED(8, ptrdiff_t, uv_off); ///< offset (in pixels) between u and v planes
  480. DECLARE_ALIGNED(8, ptrdiff_t, uv_offx2); ///< offset (in bytes) between u and v planes
  481. DECLARE_ALIGNED(8, uint16_t, dither16)[8];
  482. DECLARE_ALIGNED(8, uint32_t, dither32)[8];
  483. const uint8_t *chrDither8, *lumDither8;
  484. #if HAVE_ALTIVEC
  485. vector signed short CY;
  486. vector signed short CRV;
  487. vector signed short CBU;
  488. vector signed short CGU;
  489. vector signed short CGV;
  490. vector signed short OY;
  491. vector unsigned short CSHIFT;
  492. vector signed short *vYCoeffsBank, *vCCoeffsBank;
  493. #endif
  494. int use_mmx_vfilter;
  495. /* pre defined color-spaces gamma */
  496. #define XYZ_GAMMA (2.6f)
  497. #define RGB_GAMMA (2.2f)
  498. int16_t *xyzgamma;
  499. int16_t *rgbgamma;
  500. int16_t *xyzgammainv;
  501. int16_t *rgbgammainv;
  502. int16_t xyz2rgb_matrix[3][4];
  503. int16_t rgb2xyz_matrix[3][4];
  504. /* function pointers for swscale() */
  505. yuv2planar1_fn yuv2plane1;
  506. yuv2planarX_fn yuv2planeX;
  507. yuv2interleavedX_fn yuv2nv12cX;
  508. yuv2packed1_fn yuv2packed1;
  509. yuv2packed2_fn yuv2packed2;
  510. yuv2packedX_fn yuv2packedX;
  511. yuv2anyX_fn yuv2anyX;
  512. /// Opaque data pointer passed to all input functions.
  513. void *input_opaque;
  514. /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
  515. void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  516. int width, uint32_t *pal, void *opq);
  517. /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  518. void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  519. int width, uint32_t *pal, void *opq);
  520. /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  521. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  522. const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  523. int width, uint32_t *pal, void *opq);
  524. /**
  525. * Functions to read planar input, such as planar RGB, and convert
  526. * internally to Y/UV/A.
  527. */
  528. /** @{ */
  529. void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv,
  530. void *opq);
  531. void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
  532. int width, int32_t *rgb2yuv, void *opq);
  533. void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv,
  534. void *opq);
  535. /** @} */
  536. /**
  537. * Scale one horizontal line of input data using a bilinear filter
  538. * to produce one line of output data. Compared to SwsContext->hScale(),
  539. * please take note of the following caveats when using these:
  540. * - Scaling is done using only 7 bits instead of 14-bit coefficients.
  541. * - You can use no more than 5 input pixels to produce 4 output
  542. * pixels. Therefore, this filter should not be used for downscaling
  543. * by more than ~20% in width (because that equals more than 5/4th
  544. * downscaling and thus more than 5 pixels input per 4 pixels output).
  545. * - In general, bilinear filters create artifacts during downscaling
  546. * (even when <20%), because one output pixel will span more than one
  547. * input pixel, and thus some pixels will need edges of both neighbor
  548. * pixels to interpolate the output pixel. Since you can use at most
  549. * two input pixels per output pixel in bilinear scaling, this is
  550. * impossible and thus downscaling by any size will create artifacts.
  551. * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
  552. * in SwsContext->flags.
  553. */
  554. /** @{ */
  555. void (*hyscale_fast)(struct SwsContext *c,
  556. int16_t *dst, int dstWidth,
  557. const uint8_t *src, int srcW, int xInc);
  558. void (*hcscale_fast)(struct SwsContext *c,
  559. int16_t *dst1, int16_t *dst2, int dstWidth,
  560. const uint8_t *src1, const uint8_t *src2,
  561. int srcW, int xInc);
  562. /** @} */
  563. /**
  564. * Scale one horizontal line of input data using a filter over the input
  565. * lines, to produce one (differently sized) line of output data.
  566. *
  567. * @param dst pointer to destination buffer for horizontally scaled
  568. * data. If the number of bits per component of one
  569. * destination pixel (SwsContext->dstBpc) is <= 10, data
  570. * will be 15 bpc in 16 bits (int16_t) width. Else (i.e.
  571. * SwsContext->dstBpc == 16), data will be 19bpc in
  572. * 32 bits (int32_t) width.
  573. * @param dstW width of destination image
  574. * @param src pointer to source data to be scaled. If the number of
  575. * bits per component of a source pixel (SwsContext->srcBpc)
  576. * is 8, this is 8bpc in 8 bits (uint8_t) width. Else
  577. * (i.e. SwsContext->dstBpc > 8), this is native depth
  578. * in 16 bits (uint16_t) width. In other words, for 9-bit
  579. * YUV input, this is 9bpc, for 10-bit YUV input, this is
  580. * 10bpc, and for 16-bit RGB or YUV, this is 16bpc.
  581. * @param filter filter coefficients to be used per output pixel for
  582. * scaling. This contains 14bpp filtering coefficients.
  583. * Guaranteed to contain dstW * filterSize entries.
  584. * @param filterPos position of the first input pixel to be used for
  585. * each output pixel during scaling. Guaranteed to
  586. * contain dstW entries.
  587. * @param filterSize the number of input coefficients to be used (and
  588. * thus the number of input pixels to be used) for
  589. * creating a single output pixel. Is aligned to 4
  590. * (and input coefficients thus padded with zeroes)
  591. * to simplify creating SIMD code.
  592. */
  593. /** @{ */
  594. void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
  595. const uint8_t *src, const int16_t *filter,
  596. const int32_t *filterPos, int filterSize);
  597. void (*hcScale)(struct SwsContext *c, int16_t *dst, int dstW,
  598. const uint8_t *src, const int16_t *filter,
  599. const int32_t *filterPos, int filterSize);
  600. /** @} */
  601. /// Color range conversion function for luma plane if needed.
  602. void (*lumConvertRange)(int16_t *dst, int width);
  603. /// Color range conversion function for chroma planes if needed.
  604. void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
  605. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  606. SwsDither dither;
  607. SwsAlphaBlend alphablend;
  608. // scratch buffer for converting packed rgb0 sources
  609. // filled with a copy of the input frame + fully opaque alpha,
  610. // then passed as input to further conversion
  611. uint8_t *rgb0_scratch;
  612. unsigned int rgb0_scratch_allocated;
  613. // scratch buffer for converting XYZ sources
  614. // filled with the input converted to rgb48
  615. // then passed as input to further conversion
  616. uint8_t *xyz_scratch;
  617. unsigned int xyz_scratch_allocated;
  618. unsigned int dst_slice_align;
  619. atomic_int stride_unaligned_warned;
  620. atomic_int data_unaligned_warned;
  621. Half2FloatTables *h2f_tables;
  622. } SwsContext;
  623. //FIXME check init (where 0)
  624. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  625. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  626. int fullRange, int brightness,
  627. int contrast, int saturation);
  628. void ff_yuv2rgb_init_tables_ppc(SwsContext *c, const int inv_table[4],
  629. int brightness, int contrast, int saturation);
  630. void ff_updateMMXDitherTables(SwsContext *c, int dstY);
  631. av_cold void ff_sws_init_range_convert(SwsContext *c);
  632. av_cold void ff_sws_init_range_convert_aarch64(SwsContext *c);
  633. av_cold void ff_sws_init_range_convert_loongarch(SwsContext *c);
  634. av_cold void ff_sws_init_range_convert_x86(SwsContext *c);
  635. SwsFunc ff_yuv2rgb_init_x86(SwsContext *c);
  636. SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c);
  637. SwsFunc ff_yuv2rgb_init_loongarch(SwsContext *c);
  638. static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
  639. {
  640. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  641. av_assert0(desc);
  642. return desc->comp[0].depth == 16;
  643. }
  644. static av_always_inline int is32BPS(enum AVPixelFormat pix_fmt)
  645. {
  646. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  647. av_assert0(desc);
  648. return desc->comp[0].depth == 32;
  649. }
  650. static av_always_inline int isNBPS(enum AVPixelFormat pix_fmt)
  651. {
  652. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  653. av_assert0(desc);
  654. return desc->comp[0].depth >= 9 && desc->comp[0].depth <= 14;
  655. }
  656. static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
  657. {
  658. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  659. av_assert0(desc);
  660. return desc->flags & AV_PIX_FMT_FLAG_BE;
  661. }
  662. static av_always_inline int isYUV(enum AVPixelFormat pix_fmt)
  663. {
  664. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  665. av_assert0(desc);
  666. return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
  667. }
  668. static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
  669. {
  670. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  671. av_assert0(desc);
  672. return ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && isYUV(pix_fmt));
  673. }
  674. /*
  675. * Identity semi-planar YUV formats. Specifically, those are YUV formats
  676. * where the second and third components (U & V) are on the same plane.
  677. */
  678. static av_always_inline int isSemiPlanarYUV(enum AVPixelFormat pix_fmt)
  679. {
  680. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  681. av_assert0(desc);
  682. return (isPlanarYUV(pix_fmt) && desc->comp[1].plane == desc->comp[2].plane);
  683. }
  684. static av_always_inline int isRGB(enum AVPixelFormat pix_fmt)
  685. {
  686. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  687. av_assert0(desc);
  688. return (desc->flags & AV_PIX_FMT_FLAG_RGB);
  689. }
  690. static av_always_inline int isGray(enum AVPixelFormat pix_fmt)
  691. {
  692. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  693. av_assert0(desc);
  694. return !(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
  695. !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
  696. desc->nb_components <= 2 &&
  697. pix_fmt != AV_PIX_FMT_MONOBLACK &&
  698. pix_fmt != AV_PIX_FMT_MONOWHITE;
  699. }
  700. static av_always_inline int isRGBinInt(enum AVPixelFormat pix_fmt)
  701. {
  702. return pix_fmt == AV_PIX_FMT_RGB48BE ||
  703. pix_fmt == AV_PIX_FMT_RGB48LE ||
  704. pix_fmt == AV_PIX_FMT_RGB32 ||
  705. pix_fmt == AV_PIX_FMT_RGB32_1 ||
  706. pix_fmt == AV_PIX_FMT_RGB24 ||
  707. pix_fmt == AV_PIX_FMT_RGB565BE ||
  708. pix_fmt == AV_PIX_FMT_RGB565LE ||
  709. pix_fmt == AV_PIX_FMT_RGB555BE ||
  710. pix_fmt == AV_PIX_FMT_RGB555LE ||
  711. pix_fmt == AV_PIX_FMT_RGB444BE ||
  712. pix_fmt == AV_PIX_FMT_RGB444LE ||
  713. pix_fmt == AV_PIX_FMT_RGB8 ||
  714. pix_fmt == AV_PIX_FMT_RGB4 ||
  715. pix_fmt == AV_PIX_FMT_RGB4_BYTE ||
  716. pix_fmt == AV_PIX_FMT_RGBA64BE ||
  717. pix_fmt == AV_PIX_FMT_RGBA64LE ||
  718. pix_fmt == AV_PIX_FMT_MONOBLACK ||
  719. pix_fmt == AV_PIX_FMT_MONOWHITE;
  720. }
  721. static av_always_inline int isBGRinInt(enum AVPixelFormat pix_fmt)
  722. {
  723. return pix_fmt == AV_PIX_FMT_BGR48BE ||
  724. pix_fmt == AV_PIX_FMT_BGR48LE ||
  725. pix_fmt == AV_PIX_FMT_BGR32 ||
  726. pix_fmt == AV_PIX_FMT_BGR32_1 ||
  727. pix_fmt == AV_PIX_FMT_BGR24 ||
  728. pix_fmt == AV_PIX_FMT_BGR565BE ||
  729. pix_fmt == AV_PIX_FMT_BGR565LE ||
  730. pix_fmt == AV_PIX_FMT_BGR555BE ||
  731. pix_fmt == AV_PIX_FMT_BGR555LE ||
  732. pix_fmt == AV_PIX_FMT_BGR444BE ||
  733. pix_fmt == AV_PIX_FMT_BGR444LE ||
  734. pix_fmt == AV_PIX_FMT_BGR8 ||
  735. pix_fmt == AV_PIX_FMT_BGR4 ||
  736. pix_fmt == AV_PIX_FMT_BGR4_BYTE ||
  737. pix_fmt == AV_PIX_FMT_BGRA64BE ||
  738. pix_fmt == AV_PIX_FMT_BGRA64LE ||
  739. pix_fmt == AV_PIX_FMT_MONOBLACK ||
  740. pix_fmt == AV_PIX_FMT_MONOWHITE;
  741. }
  742. static av_always_inline int isBayer(enum AVPixelFormat pix_fmt)
  743. {
  744. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  745. av_assert0(desc);
  746. return !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
  747. }
  748. static av_always_inline int isBayer16BPS(enum AVPixelFormat pix_fmt)
  749. {
  750. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  751. av_assert0(desc);
  752. return desc->comp[1].depth == 8;
  753. }
  754. static av_always_inline int isAnyRGB(enum AVPixelFormat pix_fmt)
  755. {
  756. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  757. av_assert0(desc);
  758. return (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
  759. pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
  760. }
  761. static av_always_inline int isFloat(enum AVPixelFormat pix_fmt)
  762. {
  763. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  764. av_assert0(desc);
  765. return desc->flags & AV_PIX_FMT_FLAG_FLOAT;
  766. }
  767. static av_always_inline int isFloat16(enum AVPixelFormat pix_fmt)
  768. {
  769. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  770. av_assert0(desc);
  771. return (desc->flags & AV_PIX_FMT_FLAG_FLOAT) && desc->comp[0].depth == 16;
  772. }
  773. static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
  774. {
  775. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  776. av_assert0(desc);
  777. if (pix_fmt == AV_PIX_FMT_PAL8)
  778. return 1;
  779. return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  780. }
  781. static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
  782. {
  783. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  784. av_assert0(desc);
  785. return (desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
  786. pix_fmt == AV_PIX_FMT_PAL8 ||
  787. pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
  788. }
  789. static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
  790. {
  791. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  792. av_assert0(desc);
  793. return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  794. }
  795. static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
  796. {
  797. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  798. av_assert0(desc);
  799. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
  800. }
  801. static av_always_inline int isPlanarRGB(enum AVPixelFormat pix_fmt)
  802. {
  803. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  804. av_assert0(desc);
  805. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) ==
  806. (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
  807. }
  808. static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
  809. {
  810. switch (pix_fmt) {
  811. case AV_PIX_FMT_PAL8:
  812. case AV_PIX_FMT_BGR4_BYTE:
  813. case AV_PIX_FMT_BGR8:
  814. case AV_PIX_FMT_GRAY8:
  815. case AV_PIX_FMT_RGB4_BYTE:
  816. case AV_PIX_FMT_RGB8:
  817. return 1;
  818. default:
  819. return 0;
  820. }
  821. }
  822. /*
  823. * Identity formats where the data is in the high bits, and the low bits are shifted away.
  824. */
  825. static av_always_inline int isDataInHighBits(enum AVPixelFormat pix_fmt)
  826. {
  827. int i;
  828. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  829. av_assert0(desc);
  830. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL))
  831. return 0;
  832. for (i = 0; i < desc->nb_components; i++) {
  833. if (!desc->comp[i].shift)
  834. return 0;
  835. if ((desc->comp[i].shift + desc->comp[i].depth) & 0x7)
  836. return 0;
  837. }
  838. return 1;
  839. }
  840. /*
  841. * Identity formats where the chroma planes are swapped (CrCb order).
  842. */
  843. static av_always_inline int isSwappedChroma(enum AVPixelFormat pix_fmt)
  844. {
  845. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  846. av_assert0(desc);
  847. if (!isYUV(pix_fmt))
  848. return 0;
  849. if ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) && desc->nb_components < 4)
  850. return 0;
  851. if (desc->nb_components < 3)
  852. return 0;
  853. if (!isPlanarYUV(pix_fmt) || isSemiPlanarYUV(pix_fmt))
  854. return desc->comp[1].offset > desc->comp[2].offset;
  855. else
  856. return desc->comp[1].plane > desc->comp[2].plane;
  857. }
  858. extern const uint64_t ff_dither4[2];
  859. extern const uint64_t ff_dither8[2];
  860. extern const uint8_t ff_dither_2x2_4[3][8];
  861. extern const uint8_t ff_dither_2x2_8[3][8];
  862. extern const uint8_t ff_dither_4x4_16[5][8];
  863. extern const uint8_t ff_dither_8x8_32[9][8];
  864. extern const uint8_t ff_dither_8x8_73[9][8];
  865. extern const uint8_t ff_dither_8x8_128[9][8];
  866. extern const uint8_t ff_dither_8x8_220[9][8];
  867. extern const int32_t ff_yuv2rgb_coeffs[11][4];
  868. extern const AVClass ff_sws_context_class;
  869. /**
  870. * Set c->convert_unscaled to an unscaled converter if one exists for the
  871. * specific source and destination formats, bit depths, flags, etc.
  872. */
  873. void ff_get_unscaled_swscale(SwsContext *c);
  874. void ff_get_unscaled_swscale_ppc(SwsContext *c);
  875. void ff_get_unscaled_swscale_arm(SwsContext *c);
  876. void ff_get_unscaled_swscale_aarch64(SwsContext *c);
  877. void ff_sws_init_scale(SwsContext *c);
  878. void ff_sws_init_input_funcs(SwsContext *c);
  879. void ff_sws_init_output_funcs(SwsContext *c,
  880. yuv2planar1_fn *yuv2plane1,
  881. yuv2planarX_fn *yuv2planeX,
  882. yuv2interleavedX_fn *yuv2nv12cX,
  883. yuv2packed1_fn *yuv2packed1,
  884. yuv2packed2_fn *yuv2packed2,
  885. yuv2packedX_fn *yuv2packedX,
  886. yuv2anyX_fn *yuv2anyX);
  887. void ff_sws_init_swscale_ppc(SwsContext *c);
  888. void ff_sws_init_swscale_vsx(SwsContext *c);
  889. void ff_sws_init_swscale_x86(SwsContext *c);
  890. void ff_sws_init_swscale_aarch64(SwsContext *c);
  891. void ff_sws_init_swscale_arm(SwsContext *c);
  892. void ff_sws_init_swscale_loongarch(SwsContext *c);
  893. void ff_sws_init_swscale_riscv(SwsContext *c);
  894. void ff_hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  895. const uint8_t *src, int srcW, int xInc);
  896. void ff_hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  897. int dstWidth, const uint8_t *src1,
  898. const uint8_t *src2, int srcW, int xInc);
  899. int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
  900. int16_t *filter, int32_t *filterPos,
  901. int numSplits);
  902. void ff_hyscale_fast_mmxext(SwsContext *c, int16_t *dst,
  903. int dstWidth, const uint8_t *src,
  904. int srcW, int xInc);
  905. void ff_hcscale_fast_mmxext(SwsContext *c, int16_t *dst1, int16_t *dst2,
  906. int dstWidth, const uint8_t *src1,
  907. const uint8_t *src2, int srcW, int xInc);
  908. int ff_sws_alphablendaway(SwsContext *c, const uint8_t *src[],
  909. int srcStride[], int srcSliceY, int srcSliceH,
  910. uint8_t *dst[], int dstStride[]);
  911. static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
  912. int alpha, int bits, const int big_endian)
  913. {
  914. uint8_t *ptr = plane + stride * y;
  915. int v = alpha ? 0xFFFF>>(16-bits) : (1<<(bits-1));
  916. if (big_endian != HAVE_BIGENDIAN)
  917. v = av_bswap16(v);
  918. for (int i = 0; i < height; i++) {
  919. for (int j = 0; j < width; j++)
  920. AV_WN16(ptr + 2 * j, v);
  921. ptr += stride;
  922. }
  923. }
  924. static inline void fillPlane32(uint8_t *plane, int stride, int width, int height, int y,
  925. int alpha, int bits, const int big_endian, int is_float)
  926. {
  927. uint8_t *ptr = plane + stride * y;
  928. uint32_t v;
  929. uint32_t onef32 = 0x3f800000;
  930. if (is_float)
  931. v = alpha ? onef32 : 0;
  932. else
  933. v = alpha ? 0xFFFFFFFF>>(32-bits) : (1<<(bits-1));
  934. if (big_endian != HAVE_BIGENDIAN)
  935. v = av_bswap32(v);
  936. for (int i = 0; i < height; i++) {
  937. for (int j = 0; j < width; j++)
  938. AV_WN32(ptr + 4 * j, v);
  939. ptr += stride;
  940. }
  941. }
  942. #define MAX_SLICE_PLANES 4
  943. /// Slice plane
  944. typedef struct SwsPlane
  945. {
  946. int available_lines; ///< max number of lines that can be hold by this plane
  947. int sliceY; ///< index of first line
  948. int sliceH; ///< number of lines
  949. uint8_t **line; ///< line buffer
  950. uint8_t **tmp; ///< Tmp line buffer used by mmx code
  951. } SwsPlane;
  952. /**
  953. * Struct which defines a slice of an image to be scaled or an output for
  954. * a scaled slice.
  955. * A slice can also be used as intermediate ring buffer for scaling steps.
  956. */
  957. typedef struct SwsSlice
  958. {
  959. int width; ///< Slice line width
  960. int h_chr_sub_sample; ///< horizontal chroma subsampling factor
  961. int v_chr_sub_sample; ///< vertical chroma subsampling factor
  962. int is_ring; ///< flag to identify if this slice is a ring buffer
  963. int should_free_lines; ///< flag to identify if there are dynamic allocated lines
  964. enum AVPixelFormat fmt; ///< planes pixel format
  965. SwsPlane plane[MAX_SLICE_PLANES]; ///< color planes
  966. } SwsSlice;
  967. /**
  968. * Struct which holds all necessary data for processing a slice.
  969. * A processing step can be a color conversion or horizontal/vertical scaling.
  970. */
  971. typedef struct SwsFilterDescriptor
  972. {
  973. SwsSlice *src; ///< Source slice
  974. SwsSlice *dst; ///< Output slice
  975. int alpha; ///< Flag for processing alpha channel
  976. void *instance; ///< Filter instance data
  977. /// Function for processing input slice sliceH lines starting from line sliceY
  978. int (*process)(SwsContext *c, struct SwsFilterDescriptor *desc, int sliceY, int sliceH);
  979. } SwsFilterDescriptor;
  980. // warp input lines in the form (src + width*i + j) to slice format (line[i][j])
  981. // relative=true means first line src[x][0] otherwise first line is src[x][lum/crh Y]
  982. int ff_init_slice_from_src(SwsSlice * s, uint8_t *src[4], int stride[4], int srcW, int lumY, int lumH, int chrY, int chrH, int relative);
  983. // Initialize scaler filter descriptor chain
  984. int ff_init_filters(SwsContext *c);
  985. // Free all filter data
  986. int ff_free_filters(SwsContext *c);
  987. /*
  988. function for applying ring buffer logic into slice s
  989. It checks if the slice can hold more @lum lines, if yes
  990. do nothing otherwise remove @lum least used lines.
  991. It applies the same procedure for @chr lines.
  992. */
  993. int ff_rotate_slice(SwsSlice *s, int lum, int chr);
  994. /// initializes gamma conversion descriptor
  995. int ff_init_gamma_convert(SwsFilterDescriptor *desc, SwsSlice * src, uint16_t *table);
  996. /// initializes lum pixel format conversion descriptor
  997. int ff_init_desc_fmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
  998. /// initializes lum horizontal scaling descriptor
  999. int ff_init_desc_hscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
  1000. /// initializes chr pixel format conversion descriptor
  1001. int ff_init_desc_cfmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
  1002. /// initializes chr horizontal scaling descriptor
  1003. int ff_init_desc_chscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
  1004. int ff_init_desc_no_chr(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst);
  1005. /// initializes vertical scaling descriptors
  1006. int ff_init_vscale(SwsContext *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst);
  1007. /// setup vertical scaler functions
  1008. void ff_init_vscale_pfn(SwsContext *c, yuv2planar1_fn yuv2plane1, yuv2planarX_fn yuv2planeX,
  1009. yuv2interleavedX_fn yuv2nv12cX, yuv2packed1_fn yuv2packed1, yuv2packed2_fn yuv2packed2,
  1010. yuv2packedX_fn yuv2packedX, yuv2anyX_fn yuv2anyX, int use_mmx);
  1011. void ff_sws_slice_worker(void *priv, int jobnr, int threadnr,
  1012. int nb_jobs, int nb_threads);
  1013. //number of extra lines to process
  1014. #define MAX_LINES_AHEAD 4
  1015. //shuffle filter and filterPos for hyScale and hcScale filters in avx2
  1016. int ff_shuffle_filter_coefficients(SwsContext *c, int* filterPos, int filterSize, int16_t *filter, int dstW);
  1017. #endif /* SWSCALE_SWSCALE_INTERNAL_H */