swscale_internal.h 49 KB

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