swscale_internal.h 48 KB

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