swscale_internal.h 48 KB

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