swscale_internal.h 48 KB

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