swscale_internal.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  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 "version.h"
  25. #include "libavutil/avassert.h"
  26. #include "libavutil/avutil.h"
  27. #include "libavutil/common.h"
  28. #include "libavutil/frame.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/log.h"
  31. #include "libavutil/mem_internal.h"
  32. #include "libavutil/pixfmt.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/slicethread.h"
  35. #include "libavutil/ppc/util_altivec.h"
  36. #define STR(s) AV_TOSTRING(s) // AV_STRINGIFY is too long
  37. #define YUVRGB_TABLE_HEADROOM 512
  38. #define YUVRGB_TABLE_LUMA_HEADROOM 512
  39. #define MAX_FILTER_SIZE SWS_MAX_FILTER_SIZE
  40. #define DITHER1XBPP
  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. /// Unscaled conversion of luma plane to YV12 for horizontal scaler.
  512. void (*lumToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  513. int width, uint32_t *pal);
  514. /// Unscaled conversion of alpha plane to YV12 for horizontal scaler.
  515. void (*alpToYV12)(uint8_t *dst, const uint8_t *src, const uint8_t *src2, const uint8_t *src3,
  516. int width, uint32_t *pal);
  517. /// Unscaled conversion of chroma planes to YV12 for horizontal scaler.
  518. void (*chrToYV12)(uint8_t *dstU, uint8_t *dstV,
  519. const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  520. int width, uint32_t *pal);
  521. /**
  522. * Functions to read planar input, such as planar RGB, and convert
  523. * internally to Y/UV/A.
  524. */
  525. /** @{ */
  526. void (*readLumPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  527. void (*readChrPlanar)(uint8_t *dstU, uint8_t *dstV, const uint8_t *src[4],
  528. int width, int32_t *rgb2yuv);
  529. void (*readAlpPlanar)(uint8_t *dst, const uint8_t *src[4], int width, int32_t *rgb2yuv);
  530. /** @} */
  531. /**
  532. * Scale one horizontal line of input data using a bilinear filter
  533. * to produce one line of output data. Compared to SwsContext->hScale(),
  534. * please take note of the following caveats when using these:
  535. * - Scaling is done using only 7 bits instead of 14-bit coefficients.
  536. * - You can use no more than 5 input pixels to produce 4 output
  537. * pixels. Therefore, this filter should not be used for downscaling
  538. * by more than ~20% in width (because that equals more than 5/4th
  539. * downscaling and thus more than 5 pixels input per 4 pixels output).
  540. * - In general, bilinear filters create artifacts during downscaling
  541. * (even when <20%), because one output pixel will span more than one
  542. * input pixel, and thus some pixels will need edges of both neighbor
  543. * pixels to interpolate the output pixel. Since you can use at most
  544. * two input pixels per output pixel in bilinear scaling, this is
  545. * impossible and thus downscaling by any size will create artifacts.
  546. * To enable this type of scaling, set SWS_FLAG_FAST_BILINEAR
  547. * in SwsContext->flags.
  548. */
  549. /** @{ */
  550. void (*hyscale_fast)(struct SwsContext *c,
  551. int16_t *dst, int dstWidth,
  552. const uint8_t *src, int srcW, int xInc);
  553. void (*hcscale_fast)(struct SwsContext *c,
  554. int16_t *dst1, int16_t *dst2, int dstWidth,
  555. const uint8_t *src1, const uint8_t *src2,
  556. int srcW, int xInc);
  557. /** @} */
  558. /**
  559. * Scale one horizontal line of input data using a filter over the input
  560. * lines, to produce one (differently sized) line of output data.
  561. *
  562. * @param dst pointer to destination buffer for horizontally scaled
  563. * data. If the number of bits per component of one
  564. * destination pixel (SwsContext->dstBpc) is <= 10, data
  565. * will be 15 bpc in 16 bits (int16_t) width. Else (i.e.
  566. * SwsContext->dstBpc == 16), data will be 19bpc in
  567. * 32 bits (int32_t) width.
  568. * @param dstW width of destination image
  569. * @param src pointer to source data to be scaled. If the number of
  570. * bits per component of a source pixel (SwsContext->srcBpc)
  571. * is 8, this is 8bpc in 8 bits (uint8_t) width. Else
  572. * (i.e. SwsContext->dstBpc > 8), this is native depth
  573. * in 16 bits (uint16_t) width. In other words, for 9-bit
  574. * YUV input, this is 9bpc, for 10-bit YUV input, this is
  575. * 10bpc, and for 16-bit RGB or YUV, this is 16bpc.
  576. * @param filter filter coefficients to be used per output pixel for
  577. * scaling. This contains 14bpp filtering coefficients.
  578. * Guaranteed to contain dstW * filterSize entries.
  579. * @param filterPos position of the first input pixel to be used for
  580. * each output pixel during scaling. Guaranteed to
  581. * contain dstW entries.
  582. * @param filterSize the number of input coefficients to be used (and
  583. * thus the number of input pixels to be used) for
  584. * creating a single output pixel. Is aligned to 4
  585. * (and input coefficients thus padded with zeroes)
  586. * to simplify creating SIMD code.
  587. */
  588. /** @{ */
  589. void (*hyScale)(struct SwsContext *c, int16_t *dst, int dstW,
  590. const uint8_t *src, const int16_t *filter,
  591. const int32_t *filterPos, int filterSize);
  592. void (*hcScale)(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. /** @} */
  596. /// Color range conversion function for luma plane if needed.
  597. void (*lumConvertRange)(int16_t *dst, int width);
  598. /// Color range conversion function for chroma planes if needed.
  599. void (*chrConvertRange)(int16_t *dst1, int16_t *dst2, int width);
  600. int needs_hcscale; ///< Set if there are chroma planes to be converted.
  601. SwsDither dither;
  602. SwsAlphaBlend alphablend;
  603. // scratch buffer for converting packed rgb0 sources
  604. // filled with a copy of the input frame + fully opaque alpha,
  605. // then passed as input to further conversion
  606. uint8_t *rgb0_scratch;
  607. unsigned int rgb0_scratch_allocated;
  608. // scratch buffer for converting XYZ sources
  609. // filled with the input converted to rgb48
  610. // then passed as input to further conversion
  611. uint8_t *xyz_scratch;
  612. unsigned int xyz_scratch_allocated;
  613. unsigned int dst_slice_align;
  614. atomic_int stride_unaligned_warned;
  615. atomic_int data_unaligned_warned;
  616. } SwsContext;
  617. //FIXME check init (where 0)
  618. SwsFunc ff_yuv2rgb_get_func_ptr(SwsContext *c);
  619. int ff_yuv2rgb_c_init_tables(SwsContext *c, const int inv_table[4],
  620. int fullRange, int brightness,
  621. int contrast, int saturation);
  622. void ff_yuv2rgb_init_tables_ppc(SwsContext *c, const int inv_table[4],
  623. int brightness, int contrast, int saturation);
  624. void ff_updateMMXDitherTables(SwsContext *c, int dstY);
  625. av_cold void ff_sws_init_range_convert(SwsContext *c);
  626. SwsFunc ff_yuv2rgb_init_x86(SwsContext *c);
  627. SwsFunc ff_yuv2rgb_init_ppc(SwsContext *c);
  628. static av_always_inline int is16BPS(enum AVPixelFormat pix_fmt)
  629. {
  630. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  631. av_assert0(desc);
  632. return desc->comp[0].depth == 16;
  633. }
  634. static av_always_inline int is32BPS(enum AVPixelFormat pix_fmt)
  635. {
  636. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  637. av_assert0(desc);
  638. return desc->comp[0].depth == 32;
  639. }
  640. static av_always_inline int isNBPS(enum AVPixelFormat pix_fmt)
  641. {
  642. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  643. av_assert0(desc);
  644. return desc->comp[0].depth >= 9 && desc->comp[0].depth <= 14;
  645. }
  646. static av_always_inline int isBE(enum AVPixelFormat pix_fmt)
  647. {
  648. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  649. av_assert0(desc);
  650. return desc->flags & AV_PIX_FMT_FLAG_BE;
  651. }
  652. static av_always_inline int isYUV(enum AVPixelFormat pix_fmt)
  653. {
  654. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  655. av_assert0(desc);
  656. return !(desc->flags & AV_PIX_FMT_FLAG_RGB) && desc->nb_components >= 2;
  657. }
  658. static av_always_inline int isPlanarYUV(enum AVPixelFormat pix_fmt)
  659. {
  660. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  661. av_assert0(desc);
  662. return ((desc->flags & AV_PIX_FMT_FLAG_PLANAR) && isYUV(pix_fmt));
  663. }
  664. /*
  665. * Identity semi-planar YUV formats. Specifically, those are YUV formats
  666. * where the second and third components (U & V) are on the same plane.
  667. */
  668. static av_always_inline int isSemiPlanarYUV(enum AVPixelFormat pix_fmt)
  669. {
  670. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  671. av_assert0(desc);
  672. return (isPlanarYUV(pix_fmt) && desc->comp[1].plane == desc->comp[2].plane);
  673. }
  674. static av_always_inline int isRGB(enum AVPixelFormat pix_fmt)
  675. {
  676. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  677. av_assert0(desc);
  678. return (desc->flags & AV_PIX_FMT_FLAG_RGB);
  679. }
  680. static av_always_inline int isGray(enum AVPixelFormat pix_fmt)
  681. {
  682. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  683. av_assert0(desc);
  684. return !(desc->flags & AV_PIX_FMT_FLAG_PAL) &&
  685. !(desc->flags & AV_PIX_FMT_FLAG_HWACCEL) &&
  686. desc->nb_components <= 2 &&
  687. pix_fmt != AV_PIX_FMT_MONOBLACK &&
  688. pix_fmt != AV_PIX_FMT_MONOWHITE;
  689. }
  690. static av_always_inline int isRGBinInt(enum AVPixelFormat pix_fmt)
  691. {
  692. return pix_fmt == AV_PIX_FMT_RGB48BE ||
  693. pix_fmt == AV_PIX_FMT_RGB48LE ||
  694. pix_fmt == AV_PIX_FMT_RGB32 ||
  695. pix_fmt == AV_PIX_FMT_RGB32_1 ||
  696. pix_fmt == AV_PIX_FMT_RGB24 ||
  697. pix_fmt == AV_PIX_FMT_RGB565BE ||
  698. pix_fmt == AV_PIX_FMT_RGB565LE ||
  699. pix_fmt == AV_PIX_FMT_RGB555BE ||
  700. pix_fmt == AV_PIX_FMT_RGB555LE ||
  701. pix_fmt == AV_PIX_FMT_RGB444BE ||
  702. pix_fmt == AV_PIX_FMT_RGB444LE ||
  703. pix_fmt == AV_PIX_FMT_RGB8 ||
  704. pix_fmt == AV_PIX_FMT_RGB4 ||
  705. pix_fmt == AV_PIX_FMT_RGB4_BYTE ||
  706. pix_fmt == AV_PIX_FMT_RGBA64BE ||
  707. pix_fmt == AV_PIX_FMT_RGBA64LE ||
  708. pix_fmt == AV_PIX_FMT_MONOBLACK ||
  709. pix_fmt == AV_PIX_FMT_MONOWHITE;
  710. }
  711. static av_always_inline int isBGRinInt(enum AVPixelFormat pix_fmt)
  712. {
  713. return pix_fmt == AV_PIX_FMT_BGR48BE ||
  714. pix_fmt == AV_PIX_FMT_BGR48LE ||
  715. pix_fmt == AV_PIX_FMT_BGR32 ||
  716. pix_fmt == AV_PIX_FMT_BGR32_1 ||
  717. pix_fmt == AV_PIX_FMT_BGR24 ||
  718. pix_fmt == AV_PIX_FMT_BGR565BE ||
  719. pix_fmt == AV_PIX_FMT_BGR565LE ||
  720. pix_fmt == AV_PIX_FMT_BGR555BE ||
  721. pix_fmt == AV_PIX_FMT_BGR555LE ||
  722. pix_fmt == AV_PIX_FMT_BGR444BE ||
  723. pix_fmt == AV_PIX_FMT_BGR444LE ||
  724. pix_fmt == AV_PIX_FMT_BGR8 ||
  725. pix_fmt == AV_PIX_FMT_BGR4 ||
  726. pix_fmt == AV_PIX_FMT_BGR4_BYTE ||
  727. pix_fmt == AV_PIX_FMT_BGRA64BE ||
  728. pix_fmt == AV_PIX_FMT_BGRA64LE ||
  729. pix_fmt == AV_PIX_FMT_MONOBLACK ||
  730. pix_fmt == AV_PIX_FMT_MONOWHITE;
  731. }
  732. static av_always_inline int isBayer(enum AVPixelFormat pix_fmt)
  733. {
  734. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  735. av_assert0(desc);
  736. return !!(desc->flags & AV_PIX_FMT_FLAG_BAYER);
  737. }
  738. static av_always_inline int isBayer16BPS(enum AVPixelFormat pix_fmt)
  739. {
  740. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  741. av_assert0(desc);
  742. return desc->comp[1].depth == 8;
  743. }
  744. static av_always_inline int isAnyRGB(enum AVPixelFormat pix_fmt)
  745. {
  746. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  747. av_assert0(desc);
  748. return (desc->flags & AV_PIX_FMT_FLAG_RGB) ||
  749. pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
  750. }
  751. static av_always_inline int isFloat(enum AVPixelFormat pix_fmt)
  752. {
  753. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  754. av_assert0(desc);
  755. return desc->flags & AV_PIX_FMT_FLAG_FLOAT;
  756. }
  757. static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
  758. {
  759. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  760. av_assert0(desc);
  761. if (pix_fmt == AV_PIX_FMT_PAL8)
  762. return 1;
  763. return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
  764. }
  765. static av_always_inline int isPacked(enum AVPixelFormat pix_fmt)
  766. {
  767. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  768. av_assert0(desc);
  769. return (desc->nb_components >= 2 && !(desc->flags & AV_PIX_FMT_FLAG_PLANAR)) ||
  770. pix_fmt == AV_PIX_FMT_PAL8 ||
  771. pix_fmt == AV_PIX_FMT_MONOBLACK || pix_fmt == AV_PIX_FMT_MONOWHITE;
  772. }
  773. static av_always_inline int isPlanar(enum AVPixelFormat pix_fmt)
  774. {
  775. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  776. av_assert0(desc);
  777. return (desc->nb_components >= 2 && (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
  778. }
  779. static av_always_inline int isPackedRGB(enum AVPixelFormat pix_fmt)
  780. {
  781. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  782. av_assert0(desc);
  783. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) == AV_PIX_FMT_FLAG_RGB);
  784. }
  785. static av_always_inline int isPlanarRGB(enum AVPixelFormat pix_fmt)
  786. {
  787. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  788. av_assert0(desc);
  789. return ((desc->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) ==
  790. (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB));
  791. }
  792. static av_always_inline int usePal(enum AVPixelFormat pix_fmt)
  793. {
  794. switch (pix_fmt) {
  795. case AV_PIX_FMT_PAL8:
  796. case AV_PIX_FMT_BGR4_BYTE:
  797. case AV_PIX_FMT_BGR8:
  798. case AV_PIX_FMT_GRAY8:
  799. case AV_PIX_FMT_RGB4_BYTE:
  800. case AV_PIX_FMT_RGB8:
  801. return 1;
  802. default:
  803. return 0;
  804. }
  805. }
  806. /*
  807. * Identity formats where the data is in the high bits, and the low bits are shifted away.
  808. */
  809. static av_always_inline int isDataInHighBits(enum AVPixelFormat pix_fmt)
  810. {
  811. int i;
  812. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  813. av_assert0(desc);
  814. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM | AV_PIX_FMT_FLAG_HWACCEL))
  815. return 0;
  816. for (i = 0; i < desc->nb_components; i++) {
  817. if (!desc->comp[i].shift)
  818. return 0;
  819. if ((desc->comp[i].shift + desc->comp[i].depth) & 0x7)
  820. return 0;
  821. }
  822. return 1;
  823. }
  824. /*
  825. * Identity formats where the chroma planes are swapped (CrCb order).
  826. */
  827. static av_always_inline int isSwappedChroma(enum AVPixelFormat pix_fmt)
  828. {
  829. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
  830. av_assert0(desc);
  831. if (!isYUV(pix_fmt))
  832. return 0;
  833. if ((desc->flags & AV_PIX_FMT_FLAG_ALPHA) && desc->nb_components < 4)
  834. return 0;
  835. if (desc->nb_components < 3)
  836. return 0;
  837. if (!isPlanarYUV(pix_fmt) || isSemiPlanarYUV(pix_fmt))
  838. return desc->comp[1].offset > desc->comp[2].offset;
  839. else
  840. return desc->comp[1].plane > desc->comp[2].plane;
  841. }
  842. extern const uint64_t ff_dither4[2];
  843. extern const uint64_t ff_dither8[2];
  844. extern const uint8_t ff_dither_2x2_4[3][8];
  845. extern const uint8_t ff_dither_2x2_8[3][8];
  846. extern const uint8_t ff_dither_4x4_16[5][8];
  847. extern const uint8_t ff_dither_8x8_32[9][8];
  848. extern const uint8_t ff_dither_8x8_73[9][8];
  849. extern const uint8_t ff_dither_8x8_128[9][8];
  850. extern const uint8_t ff_dither_8x8_220[9][8];
  851. extern const int32_t ff_yuv2rgb_coeffs[11][4];
  852. extern const AVClass ff_sws_context_class;
  853. /**
  854. * Set c->convert_unscaled to an unscaled converter if one exists for the
  855. * specific source and destination formats, bit depths, flags, etc.
  856. */
  857. void ff_get_unscaled_swscale(SwsContext *c);
  858. void ff_get_unscaled_swscale_ppc(SwsContext *c);
  859. void ff_get_unscaled_swscale_arm(SwsContext *c);
  860. void ff_get_unscaled_swscale_aarch64(SwsContext *c);
  861. void ff_sws_init_scale(SwsContext *c);
  862. void ff_sws_init_input_funcs(SwsContext *c);
  863. void ff_sws_init_output_funcs(SwsContext *c,
  864. yuv2planar1_fn *yuv2plane1,
  865. yuv2planarX_fn *yuv2planeX,
  866. yuv2interleavedX_fn *yuv2nv12cX,
  867. yuv2packed1_fn *yuv2packed1,
  868. yuv2packed2_fn *yuv2packed2,
  869. yuv2packedX_fn *yuv2packedX,
  870. yuv2anyX_fn *yuv2anyX);
  871. void ff_sws_init_swscale_ppc(SwsContext *c);
  872. void ff_sws_init_swscale_vsx(SwsContext *c);
  873. void ff_sws_init_swscale_x86(SwsContext *c);
  874. void ff_sws_init_swscale_aarch64(SwsContext *c);
  875. void ff_sws_init_swscale_arm(SwsContext *c);
  876. void ff_hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  877. const uint8_t *src, int srcW, int xInc);
  878. void ff_hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  879. int dstWidth, const uint8_t *src1,
  880. const uint8_t *src2, int srcW, int xInc);
  881. int ff_init_hscaler_mmxext(int dstW, int xInc, uint8_t *filterCode,
  882. int16_t *filter, int32_t *filterPos,
  883. int numSplits);
  884. void ff_hyscale_fast_mmxext(SwsContext *c, int16_t *dst,
  885. int dstWidth, const uint8_t *src,
  886. int srcW, int xInc);
  887. void ff_hcscale_fast_mmxext(SwsContext *c, int16_t *dst1, int16_t *dst2,
  888. int dstWidth, const uint8_t *src1,
  889. const uint8_t *src2, int srcW, int xInc);
  890. /**
  891. * Allocate and return an SwsContext.
  892. * This is like sws_getContext() but does not perform the init step, allowing
  893. * the user to set additional AVOptions.
  894. *
  895. * @see sws_getContext()
  896. */
  897. struct SwsContext *sws_alloc_set_opts(int srcW, int srcH, enum AVPixelFormat srcFormat,
  898. int dstW, int dstH, enum AVPixelFormat dstFormat,
  899. int flags, const double *param);
  900. int ff_sws_alphablendaway(SwsContext *c, const uint8_t *src[],
  901. int srcStride[], int srcSliceY, int srcSliceH,
  902. uint8_t *dst[], int dstStride[]);
  903. static inline void fillPlane16(uint8_t *plane, int stride, int width, int height, int y,
  904. int alpha, int bits, const int big_endian)
  905. {
  906. int i, j;
  907. uint8_t *ptr = plane + stride * y;
  908. int v = alpha ? 0xFFFF>>(16-bits) : (1<<(bits-1));
  909. for (i = 0; i < height; i++) {
  910. #define FILL(wfunc) \
  911. for (j = 0; j < width; j++) {\
  912. wfunc(ptr+2*j, v);\
  913. }
  914. if (big_endian) {
  915. FILL(AV_WB16);
  916. } else {
  917. FILL(AV_WL16);
  918. }
  919. ptr += stride;
  920. }
  921. #undef FILL
  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. int i, j;
  927. uint8_t *ptr = plane + stride * y;
  928. uint32_t v;
  929. uint32_t onef32 = 0x3f800000;
  930. if (is_float)
  931. v = alpha ? onef32 : 0;
  932. else
  933. v = alpha ? 0xFFFFFFFF>>(32-bits) : (1<<(bits-1));
  934. for (i = 0; i < height; i++) {
  935. #define FILL(wfunc) \
  936. for (j = 0; j < width; j++) {\
  937. wfunc(ptr+4*j, v);\
  938. }
  939. if (big_endian) {
  940. FILL(AV_WB32);
  941. } else {
  942. FILL(AV_WL32);
  943. }
  944. ptr += stride;
  945. }
  946. #undef FILL
  947. }
  948. #define MAX_SLICE_PLANES 4
  949. /// Slice plane
  950. typedef struct SwsPlane
  951. {
  952. int available_lines; ///< max number of lines that can be hold by this plane
  953. int sliceY; ///< index of first line
  954. int sliceH; ///< number of lines
  955. uint8_t **line; ///< line buffer
  956. uint8_t **tmp; ///< Tmp line buffer used by mmx code
  957. } SwsPlane;
  958. /**
  959. * Struct which defines a slice of an image to be scaled or an output for
  960. * a scaled slice.
  961. * A slice can also be used as intermediate ring buffer for scaling steps.
  962. */
  963. typedef struct SwsSlice
  964. {
  965. int width; ///< Slice line width
  966. int h_chr_sub_sample; ///< horizontal chroma subsampling factor
  967. int v_chr_sub_sample; ///< vertical chroma subsampling factor
  968. int is_ring; ///< flag to identify if this slice is a ring buffer
  969. int should_free_lines; ///< flag to identify if there are dynamic allocated lines
  970. enum AVPixelFormat fmt; ///< planes pixel format
  971. SwsPlane plane[MAX_SLICE_PLANES]; ///< color planes
  972. } SwsSlice;
  973. /**
  974. * Struct which holds all necessary data for processing a slice.
  975. * A processing step can be a color conversion or horizontal/vertical scaling.
  976. */
  977. typedef struct SwsFilterDescriptor
  978. {
  979. SwsSlice *src; ///< Source slice
  980. SwsSlice *dst; ///< Output slice
  981. int alpha; ///< Flag for processing alpha channel
  982. void *instance; ///< Filter instance data
  983. /// Function for processing input slice sliceH lines starting from line sliceY
  984. int (*process)(SwsContext *c, struct SwsFilterDescriptor *desc, int sliceY, int sliceH);
  985. } SwsFilterDescriptor;
  986. // warp input lines in the form (src + width*i + j) to slice format (line[i][j])
  987. // relative=true means first line src[x][0] otherwise first line is src[x][lum/crh Y]
  988. 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);
  989. // Initialize scaler filter descriptor chain
  990. int ff_init_filters(SwsContext *c);
  991. // Free all filter data
  992. int ff_free_filters(SwsContext *c);
  993. /*
  994. function for applying ring buffer logic into slice s
  995. It checks if the slice can hold more @lum lines, if yes
  996. do nothing otherwise remove @lum least used lines.
  997. It applies the same procedure for @chr lines.
  998. */
  999. int ff_rotate_slice(SwsSlice *s, int lum, int chr);
  1000. /// initializes gamma conversion descriptor
  1001. int ff_init_gamma_convert(SwsFilterDescriptor *desc, SwsSlice * src, uint16_t *table);
  1002. /// initializes lum pixel format conversion descriptor
  1003. int ff_init_desc_fmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
  1004. /// initializes lum horizontal scaling descriptor
  1005. int ff_init_desc_hscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
  1006. /// initializes chr pixel format conversion descriptor
  1007. int ff_init_desc_cfmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal);
  1008. /// initializes chr horizontal scaling descriptor
  1009. int ff_init_desc_chscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc);
  1010. int ff_init_desc_no_chr(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst);
  1011. /// initializes vertical scaling descriptors
  1012. int ff_init_vscale(SwsContext *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst);
  1013. /// setup vertical scaler functions
  1014. void ff_init_vscale_pfn(SwsContext *c, yuv2planar1_fn yuv2plane1, yuv2planarX_fn yuv2planeX,
  1015. yuv2interleavedX_fn yuv2nv12cX, yuv2packed1_fn yuv2packed1, yuv2packed2_fn yuv2packed2,
  1016. yuv2packedX_fn yuv2packedX, yuv2anyX_fn yuv2anyX, int use_mmx);
  1017. void ff_sws_slice_worker(void *priv, int jobnr, int threadnr,
  1018. int nb_jobs, int nb_threads);
  1019. //number of extra lines to process
  1020. #define MAX_LINES_AHEAD 4
  1021. //shuffle filter and filterPos for hyScale and hcScale filters in avx2
  1022. void ff_shuffle_filter_coefficients(SwsContext *c, int* filterPos, int filterSize, int16_t *filter, int dstW);
  1023. #endif /* SWSCALE_SWSCALE_INTERNAL_H */