dsp.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Speed-critical functions.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #ifndef WEBP_DSP_DSP_H_
  14. #define WEBP_DSP_DSP_H_
  15. #ifdef HAVE_CONFIG_H
  16. #include "../webp/config.h"
  17. #endif
  18. #include "../webp/types.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define BPS 32 // this is the common stride for enc/dec
  23. //------------------------------------------------------------------------------
  24. // WEBP_RESTRICT
  25. // Declares a pointer with the restrict type qualifier if available.
  26. // This allows code to hint to the compiler that only this pointer references a
  27. // particular object or memory region within the scope of the block in which it
  28. // is declared. This may allow for improved optimizations due to the lack of
  29. // pointer aliasing. See also:
  30. // https://en.cppreference.com/w/c/language/restrict
  31. #if defined(__GNUC__)
  32. #define WEBP_RESTRICT __restrict__
  33. #elif defined(_MSC_VER)
  34. #define WEBP_RESTRICT __restrict
  35. #else
  36. #define WEBP_RESTRICT
  37. #endif
  38. //------------------------------------------------------------------------------
  39. // CPU detection
  40. #if defined(__GNUC__)
  41. # define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
  42. # define LOCAL_GCC_PREREQ(maj, min) \
  43. (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
  44. #else
  45. # define LOCAL_GCC_VERSION 0
  46. # define LOCAL_GCC_PREREQ(maj, min) 0
  47. #endif
  48. #if defined(__clang__)
  49. # define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
  50. # define LOCAL_CLANG_PREREQ(maj, min) \
  51. (LOCAL_CLANG_VERSION >= (((maj) << 8) | (min)))
  52. #else
  53. # define LOCAL_CLANG_VERSION 0
  54. # define LOCAL_CLANG_PREREQ(maj, min) 0
  55. #endif
  56. #ifndef __has_builtin
  57. # define __has_builtin(x) 0
  58. #endif
  59. #if !defined(HAVE_CONFIG_H)
  60. #if defined(_MSC_VER) && _MSC_VER > 1310 && \
  61. (defined(_M_X64) || defined(_M_IX86))
  62. #define WEBP_MSC_SSE2 // Visual C++ SSE2 targets
  63. #endif
  64. #if defined(_MSC_VER) && _MSC_VER >= 1500 && \
  65. (defined(_M_X64) || defined(_M_IX86))
  66. #define WEBP_MSC_SSE41 // Visual C++ SSE4.1 targets
  67. #endif
  68. #endif
  69. // WEBP_HAVE_* are used to indicate the presence of the instruction set in dsp
  70. // files without intrinsics, allowing the corresponding Init() to be called.
  71. // Files containing intrinsics will need to be built targeting the instruction
  72. // set so should succeed on one of the earlier tests.
  73. #if (defined(__SSE2__) || defined(WEBP_MSC_SSE2)) && \
  74. (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_SSE2))
  75. #define WEBP_USE_SSE2
  76. #endif
  77. #if defined(WEBP_USE_SSE2) && !defined(WEBP_HAVE_SSE2)
  78. #define WEBP_HAVE_SSE2
  79. #endif
  80. #if (defined(__SSE4_1__) || defined(WEBP_MSC_SSE41)) && \
  81. (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_SSE41))
  82. #define WEBP_USE_SSE41
  83. #endif
  84. #if defined(WEBP_USE_SSE41) && !defined(WEBP_HAVE_SSE41)
  85. #define WEBP_HAVE_SSE41
  86. #endif
  87. #undef WEBP_MSC_SSE41
  88. #undef WEBP_MSC_SSE2
  89. // The intrinsics currently cause compiler errors with arm-nacl-gcc and the
  90. // inline assembly would need to be modified for use with Native Client.
  91. #if ((defined(__ARM_NEON__) || defined(__aarch64__)) && \
  92. (!defined(HAVE_CONFIG_H) || defined(WEBP_HAVE_NEON))) && \
  93. !defined(__native_client__)
  94. #define WEBP_USE_NEON
  95. #endif
  96. #if !defined(WEBP_USE_NEON) && defined(__ANDROID__) && \
  97. defined(__ARM_ARCH_7A__) && defined(HAVE_CPU_FEATURES_H)
  98. #define WEBP_ANDROID_NEON // Android targets that may have NEON
  99. #define WEBP_USE_NEON
  100. #endif
  101. // Note: ARM64 is supported in Visual Studio 2017, but requires the direct
  102. // inclusion of arm64_neon.h; Visual Studio 2019 includes this file in
  103. // arm_neon.h.
  104. #if defined(_MSC_VER) && \
  105. ((_MSC_VER >= 1700 && defined(_M_ARM)) || \
  106. (_MSC_VER >= 1920 && defined(_M_ARM64)))
  107. #define WEBP_USE_NEON
  108. #define WEBP_USE_INTRINSICS
  109. #endif
  110. #if defined(WEBP_USE_NEON) && !defined(WEBP_HAVE_NEON)
  111. #define WEBP_HAVE_NEON
  112. #endif
  113. #if defined(__mips__) && !defined(__mips64) && \
  114. defined(__mips_isa_rev) && (__mips_isa_rev >= 1) && (__mips_isa_rev < 6)
  115. #define WEBP_USE_MIPS32
  116. #if (__mips_isa_rev >= 2)
  117. #define WEBP_USE_MIPS32_R2
  118. #if defined(__mips_dspr2) || (defined(__mips_dsp_rev) && __mips_dsp_rev >= 2)
  119. #define WEBP_USE_MIPS_DSP_R2
  120. #endif
  121. #endif
  122. #endif
  123. #if defined(__mips_msa) && defined(__mips_isa_rev) && (__mips_isa_rev >= 5)
  124. #define WEBP_USE_MSA
  125. #endif
  126. #ifndef WEBP_DSP_OMIT_C_CODE
  127. #define WEBP_DSP_OMIT_C_CODE 1
  128. #endif
  129. #if defined(WEBP_USE_NEON) && WEBP_DSP_OMIT_C_CODE
  130. #define WEBP_NEON_OMIT_C_CODE 1
  131. #else
  132. #define WEBP_NEON_OMIT_C_CODE 0
  133. #endif
  134. #if !(LOCAL_CLANG_PREREQ(3,8) || LOCAL_GCC_PREREQ(4,8) || defined(__aarch64__))
  135. #define WEBP_NEON_WORK_AROUND_GCC 1
  136. #else
  137. #define WEBP_NEON_WORK_AROUND_GCC 0
  138. #endif
  139. // This macro prevents thread_sanitizer from reporting known concurrent writes.
  140. #define WEBP_TSAN_IGNORE_FUNCTION
  141. #if defined(__has_feature)
  142. #if __has_feature(thread_sanitizer)
  143. #undef WEBP_TSAN_IGNORE_FUNCTION
  144. #define WEBP_TSAN_IGNORE_FUNCTION __attribute__((no_sanitize_thread))
  145. #endif
  146. #endif
  147. #if defined(WEBP_USE_THREAD) && !defined(_WIN32)
  148. #include <pthread.h> // NOLINT
  149. #define WEBP_DSP_INIT(func) do { \
  150. static volatile VP8CPUInfo func ## _last_cpuinfo_used = \
  151. (VP8CPUInfo)&func ## _last_cpuinfo_used; \
  152. static pthread_mutex_t func ## _lock = PTHREAD_MUTEX_INITIALIZER; \
  153. if (pthread_mutex_lock(&func ## _lock)) break; \
  154. if (func ## _last_cpuinfo_used != VP8GetCPUInfo) func(); \
  155. func ## _last_cpuinfo_used = VP8GetCPUInfo; \
  156. (void)pthread_mutex_unlock(&func ## _lock); \
  157. } while (0)
  158. #else // !(defined(WEBP_USE_THREAD) && !defined(_WIN32))
  159. #define WEBP_DSP_INIT(func) do { \
  160. static volatile VP8CPUInfo func ## _last_cpuinfo_used = \
  161. (VP8CPUInfo)&func ## _last_cpuinfo_used; \
  162. if (func ## _last_cpuinfo_used == VP8GetCPUInfo) break; \
  163. func(); \
  164. func ## _last_cpuinfo_used = VP8GetCPUInfo; \
  165. } while (0)
  166. #endif // defined(WEBP_USE_THREAD) && !defined(_WIN32)
  167. // Defines an Init + helper function that control multiple initialization of
  168. // function pointers / tables.
  169. /* Usage:
  170. WEBP_DSP_INIT_FUNC(InitFunc) {
  171. ...function body
  172. }
  173. */
  174. #define WEBP_DSP_INIT_FUNC(name) \
  175. static WEBP_TSAN_IGNORE_FUNCTION void name ## _body(void); \
  176. WEBP_TSAN_IGNORE_FUNCTION void name(void) { \
  177. WEBP_DSP_INIT(name ## _body); \
  178. } \
  179. static WEBP_TSAN_IGNORE_FUNCTION void name ## _body(void)
  180. #define WEBP_UBSAN_IGNORE_UNDEF
  181. #define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
  182. #if defined(__clang__) && defined(__has_attribute)
  183. #if __has_attribute(no_sanitize)
  184. // This macro prevents the undefined behavior sanitizer from reporting
  185. // failures. This is only meant to silence unaligned loads on platforms that
  186. // are known to support them.
  187. #undef WEBP_UBSAN_IGNORE_UNDEF
  188. #define WEBP_UBSAN_IGNORE_UNDEF \
  189. __attribute__((no_sanitize("undefined")))
  190. // This macro prevents the undefined behavior sanitizer from reporting
  191. // failures related to unsigned integer overflows. This is only meant to
  192. // silence cases where this well defined behavior is expected.
  193. #undef WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW
  194. #define WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW \
  195. __attribute__((no_sanitize("unsigned-integer-overflow")))
  196. #endif
  197. #endif
  198. // If 'ptr' is NULL, returns NULL. Otherwise returns 'ptr + off'.
  199. // Prevents undefined behavior sanitizer nullptr-with-nonzero-offset warning.
  200. #if !defined(WEBP_OFFSET_PTR)
  201. #define WEBP_OFFSET_PTR(ptr, off) (((ptr) == NULL) ? NULL : ((ptr) + (off)))
  202. #endif
  203. // Regularize the definition of WEBP_SWAP_16BIT_CSP (backward compatibility)
  204. #if !defined(WEBP_SWAP_16BIT_CSP)
  205. #define WEBP_SWAP_16BIT_CSP 0
  206. #endif
  207. // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
  208. #if !defined(WORDS_BIGENDIAN) && \
  209. (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
  210. (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
  211. #define WORDS_BIGENDIAN
  212. #endif
  213. typedef enum {
  214. kSSE2,
  215. kSSE3,
  216. kSlowSSSE3, // special feature for slow SSSE3 architectures
  217. kSSE4_1,
  218. kAVX,
  219. kAVX2,
  220. kNEON,
  221. kMIPS32,
  222. kMIPSdspR2,
  223. kMSA
  224. } CPUFeature;
  225. // returns true if the CPU supports the feature.
  226. typedef int (*VP8CPUInfo)(CPUFeature feature);
  227. WEBP_EXTERN VP8CPUInfo VP8GetCPUInfo;
  228. //------------------------------------------------------------------------------
  229. // Init stub generator
  230. // Defines an init function stub to ensure each module exposes a symbol,
  231. // avoiding a compiler warning.
  232. #define WEBP_DSP_INIT_STUB(func) \
  233. extern void func(void); \
  234. void func(void) {}
  235. //------------------------------------------------------------------------------
  236. // Encoding
  237. // Transforms
  238. // VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
  239. // will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
  240. typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
  241. int do_two);
  242. typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
  243. typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
  244. extern VP8Idct VP8ITransform;
  245. extern VP8Fdct VP8FTransform;
  246. extern VP8Fdct VP8FTransform2; // performs two transforms at a time
  247. extern VP8WHT VP8FTransformWHT;
  248. // Predictions
  249. // *dst is the destination block. *top and *left can be NULL.
  250. typedef void (*VP8IntraPreds)(uint8_t* dst, const uint8_t* left,
  251. const uint8_t* top);
  252. typedef void (*VP8Intra4Preds)(uint8_t* dst, const uint8_t* top);
  253. extern VP8Intra4Preds VP8EncPredLuma4;
  254. extern VP8IntraPreds VP8EncPredLuma16;
  255. extern VP8IntraPreds VP8EncPredChroma8;
  256. typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
  257. extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
  258. typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
  259. const uint16_t* const weights);
  260. // The weights for VP8TDisto4x4 and VP8TDisto16x16 contain a row-major
  261. // 4 by 4 symmetric matrix.
  262. extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
  263. // Compute the average (DC) of four 4x4 blocks.
  264. // Each sub-4x4 block #i sum is stored in dc[i].
  265. typedef void (*VP8MeanMetric)(const uint8_t* ref, uint32_t dc[4]);
  266. extern VP8MeanMetric VP8Mean16x4;
  267. typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
  268. extern VP8BlockCopy VP8Copy4x4;
  269. extern VP8BlockCopy VP8Copy16x8;
  270. // Quantization
  271. struct VP8Matrix; // forward declaration
  272. typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
  273. const struct VP8Matrix* const mtx);
  274. // Same as VP8QuantizeBlock, but quantizes two consecutive blocks.
  275. typedef int (*VP8Quantize2Blocks)(int16_t in[32], int16_t out[32],
  276. const struct VP8Matrix* const mtx);
  277. extern VP8QuantizeBlock VP8EncQuantizeBlock;
  278. extern VP8Quantize2Blocks VP8EncQuantize2Blocks;
  279. // specific to 2nd transform:
  280. typedef int (*VP8QuantizeBlockWHT)(int16_t in[16], int16_t out[16],
  281. const struct VP8Matrix* const mtx);
  282. extern VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
  283. extern const int VP8DspScan[16 + 4 + 4];
  284. // Collect histogram for susceptibility calculation.
  285. #define MAX_COEFF_THRESH 31 // size of histogram used by CollectHistogram.
  286. typedef struct {
  287. // We only need to store max_value and last_non_zero, not the distribution.
  288. int max_value;
  289. int last_non_zero;
  290. } VP8Histogram;
  291. typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
  292. int start_block, int end_block,
  293. VP8Histogram* const histo);
  294. extern VP8CHisto VP8CollectHistogram;
  295. // General-purpose util function to help VP8CollectHistogram().
  296. void VP8SetHistogramData(const int distribution[MAX_COEFF_THRESH + 1],
  297. VP8Histogram* const histo);
  298. // must be called before using any of the above
  299. void VP8EncDspInit(void);
  300. //------------------------------------------------------------------------------
  301. // cost functions (encoding)
  302. extern const uint16_t VP8EntropyCost[256]; // 8bit fixed-point log(p)
  303. // approximate cost per level:
  304. extern const uint16_t VP8LevelFixedCosts[2047 /*MAX_LEVEL*/ + 1];
  305. extern const uint8_t VP8EncBands[16 + 1];
  306. struct VP8Residual;
  307. typedef void (*VP8SetResidualCoeffsFunc)(const int16_t* const coeffs,
  308. struct VP8Residual* const res);
  309. extern VP8SetResidualCoeffsFunc VP8SetResidualCoeffs;
  310. // Cost calculation function.
  311. typedef int (*VP8GetResidualCostFunc)(int ctx0,
  312. const struct VP8Residual* const res);
  313. extern VP8GetResidualCostFunc VP8GetResidualCost;
  314. // must be called before anything using the above
  315. void VP8EncDspCostInit(void);
  316. //------------------------------------------------------------------------------
  317. // SSIM / PSNR utils
  318. // struct for accumulating statistical moments
  319. typedef struct {
  320. uint32_t w; // sum(w_i) : sum of weights
  321. uint32_t xm, ym; // sum(w_i * x_i), sum(w_i * y_i)
  322. uint32_t xxm, xym, yym; // sum(w_i * x_i * x_i), etc.
  323. } VP8DistoStats;
  324. // Compute the final SSIM value
  325. // The non-clipped version assumes stats->w = (2 * VP8_SSIM_KERNEL + 1)^2.
  326. double VP8SSIMFromStats(const VP8DistoStats* const stats);
  327. double VP8SSIMFromStatsClipped(const VP8DistoStats* const stats);
  328. #define VP8_SSIM_KERNEL 3 // total size of the kernel: 2 * VP8_SSIM_KERNEL + 1
  329. typedef double (*VP8SSIMGetClippedFunc)(const uint8_t* src1, int stride1,
  330. const uint8_t* src2, int stride2,
  331. int xo, int yo, // center position
  332. int W, int H); // plane dimension
  333. #if !defined(WEBP_REDUCE_SIZE)
  334. // This version is called with the guarantee that you can load 8 bytes and
  335. // 8 rows at offset src1 and src2
  336. typedef double (*VP8SSIMGetFunc)(const uint8_t* src1, int stride1,
  337. const uint8_t* src2, int stride2);
  338. extern VP8SSIMGetFunc VP8SSIMGet; // unclipped / unchecked
  339. extern VP8SSIMGetClippedFunc VP8SSIMGetClipped; // with clipping
  340. #endif
  341. #if !defined(WEBP_DISABLE_STATS)
  342. typedef uint32_t (*VP8AccumulateSSEFunc)(const uint8_t* src1,
  343. const uint8_t* src2, int len);
  344. extern VP8AccumulateSSEFunc VP8AccumulateSSE;
  345. #endif
  346. // must be called before using any of the above directly
  347. void VP8SSIMDspInit(void);
  348. //------------------------------------------------------------------------------
  349. // Decoding
  350. typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
  351. // when doing two transforms, coeffs is actually int16_t[2][16].
  352. typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
  353. extern VP8DecIdct2 VP8Transform;
  354. extern VP8DecIdct VP8TransformAC3;
  355. extern VP8DecIdct VP8TransformUV;
  356. extern VP8DecIdct VP8TransformDC;
  357. extern VP8DecIdct VP8TransformDCUV;
  358. extern VP8WHT VP8TransformWHT;
  359. // *dst is the destination block, with stride BPS. Boundary samples are
  360. // assumed accessible when needed.
  361. typedef void (*VP8PredFunc)(uint8_t* dst);
  362. extern VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
  363. extern VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
  364. extern VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
  365. // clipping tables (for filtering)
  366. extern const int8_t* const VP8ksclip1; // clips [-1020, 1020] to [-128, 127]
  367. extern const int8_t* const VP8ksclip2; // clips [-112, 112] to [-16, 15]
  368. extern const uint8_t* const VP8kclip1; // clips [-255,511] to [0,255]
  369. extern const uint8_t* const VP8kabs0; // abs(x) for x in [-255,255]
  370. // must be called first
  371. void VP8InitClipTables(void);
  372. // simple filter (only for luma)
  373. typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
  374. extern VP8SimpleFilterFunc VP8SimpleVFilter16;
  375. extern VP8SimpleFilterFunc VP8SimpleHFilter16;
  376. extern VP8SimpleFilterFunc VP8SimpleVFilter16i; // filter 3 inner edges
  377. extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
  378. // regular filter (on both macroblock edges and inner edges)
  379. typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
  380. int thresh, int ithresh, int hev_t);
  381. typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
  382. int thresh, int ithresh, int hev_t);
  383. // on outer edge
  384. extern VP8LumaFilterFunc VP8VFilter16;
  385. extern VP8LumaFilterFunc VP8HFilter16;
  386. extern VP8ChromaFilterFunc VP8VFilter8;
  387. extern VP8ChromaFilterFunc VP8HFilter8;
  388. // on inner edge
  389. extern VP8LumaFilterFunc VP8VFilter16i; // filtering 3 inner edges altogether
  390. extern VP8LumaFilterFunc VP8HFilter16i;
  391. extern VP8ChromaFilterFunc VP8VFilter8i; // filtering u and v altogether
  392. extern VP8ChromaFilterFunc VP8HFilter8i;
  393. // Dithering. Combines dithering values (centered around 128) with dst[],
  394. // according to: dst[] = clip(dst[] + (((dither[]-128) + 8) >> 4)
  395. #define VP8_DITHER_DESCALE 4
  396. #define VP8_DITHER_DESCALE_ROUNDER (1 << (VP8_DITHER_DESCALE - 1))
  397. #define VP8_DITHER_AMP_BITS 7
  398. #define VP8_DITHER_AMP_CENTER (1 << VP8_DITHER_AMP_BITS)
  399. extern void (*VP8DitherCombine8x8)(const uint8_t* dither, uint8_t* dst,
  400. int dst_stride);
  401. // must be called before anything using the above
  402. void VP8DspInit(void);
  403. //------------------------------------------------------------------------------
  404. // WebP I/O
  405. #define FANCY_UPSAMPLING // undefined to remove fancy upsampling support
  406. // Convert a pair of y/u/v lines together to the output rgb/a colorspace.
  407. // bottom_y can be NULL if only one line of output is needed (at top/bottom).
  408. typedef void (*WebPUpsampleLinePairFunc)(
  409. const uint8_t* top_y, const uint8_t* bottom_y,
  410. const uint8_t* top_u, const uint8_t* top_v,
  411. const uint8_t* cur_u, const uint8_t* cur_v,
  412. uint8_t* top_dst, uint8_t* bottom_dst, int len);
  413. #ifdef FANCY_UPSAMPLING
  414. // Fancy upsampling functions to convert YUV to RGB(A) modes
  415. extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
  416. #endif // FANCY_UPSAMPLING
  417. // Per-row point-sampling methods.
  418. typedef void (*WebPSamplerRowFunc)(const uint8_t* y,
  419. const uint8_t* u, const uint8_t* v,
  420. uint8_t* dst, int len);
  421. // Generic function to apply 'WebPSamplerRowFunc' to the whole plane:
  422. void WebPSamplerProcessPlane(const uint8_t* y, int y_stride,
  423. const uint8_t* u, const uint8_t* v, int uv_stride,
  424. uint8_t* dst, int dst_stride,
  425. int width, int height, WebPSamplerRowFunc func);
  426. // Sampling functions to convert rows of YUV to RGB(A)
  427. extern WebPSamplerRowFunc WebPSamplers[/* MODE_LAST */];
  428. // General function for converting two lines of ARGB or RGBA.
  429. // 'alpha_is_last' should be true if 0xff000000 is stored in memory as
  430. // as 0x00, 0x00, 0x00, 0xff (little endian).
  431. WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
  432. // YUV444->RGB converters
  433. typedef void (*WebPYUV444Converter)(const uint8_t* y,
  434. const uint8_t* u, const uint8_t* v,
  435. uint8_t* dst, int len);
  436. extern WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
  437. // Must be called before using the WebPUpsamplers[] (and for premultiplied
  438. // colorspaces like rgbA, rgbA4444, etc)
  439. void WebPInitUpsamplers(void);
  440. // Must be called before using WebPSamplers[]
  441. void WebPInitSamplers(void);
  442. // Must be called before using WebPYUV444Converters[]
  443. void WebPInitYUV444Converters(void);
  444. //------------------------------------------------------------------------------
  445. // ARGB -> YUV converters
  446. // Convert ARGB samples to luma Y.
  447. extern void (*WebPConvertARGBToY)(const uint32_t* argb, uint8_t* y, int width);
  448. // Convert ARGB samples to U/V with downsampling. do_store should be '1' for
  449. // even lines and '0' for odd ones. 'src_width' is the original width, not
  450. // the U/V one.
  451. extern void (*WebPConvertARGBToUV)(const uint32_t* argb, uint8_t* u, uint8_t* v,
  452. int src_width, int do_store);
  453. // Convert a row of accumulated (four-values) of rgba32 toward U/V
  454. extern void (*WebPConvertRGBA32ToUV)(const uint16_t* rgb,
  455. uint8_t* u, uint8_t* v, int width);
  456. // Convert RGB or BGR to Y
  457. extern void (*WebPConvertRGB24ToY)(const uint8_t* rgb, uint8_t* y, int width);
  458. extern void (*WebPConvertBGR24ToY)(const uint8_t* bgr, uint8_t* y, int width);
  459. // used for plain-C fallback.
  460. extern void WebPConvertARGBToUV_C(const uint32_t* argb, uint8_t* u, uint8_t* v,
  461. int src_width, int do_store);
  462. extern void WebPConvertRGBA32ToUV_C(const uint16_t* rgb,
  463. uint8_t* u, uint8_t* v, int width);
  464. // utilities for accurate RGB->YUV conversion
  465. extern uint64_t (*WebPSharpYUVUpdateY)(const uint16_t* src, const uint16_t* ref,
  466. uint16_t* dst, int len);
  467. extern void (*WebPSharpYUVUpdateRGB)(const int16_t* src, const int16_t* ref,
  468. int16_t* dst, int len);
  469. extern void (*WebPSharpYUVFilterRow)(const int16_t* A, const int16_t* B,
  470. int len,
  471. const uint16_t* best_y, uint16_t* out);
  472. // Must be called before using the above.
  473. void WebPInitConvertARGBToYUV(void);
  474. //------------------------------------------------------------------------------
  475. // Rescaler
  476. struct WebPRescaler;
  477. // Import a row of data and save its contribution in the rescaler.
  478. // 'channel' denotes the channel number to be imported. 'Expand' corresponds to
  479. // the wrk->x_expand case. Otherwise, 'Shrink' is to be used.
  480. typedef void (*WebPRescalerImportRowFunc)(struct WebPRescaler* const wrk,
  481. const uint8_t* src);
  482. extern WebPRescalerImportRowFunc WebPRescalerImportRowExpand;
  483. extern WebPRescalerImportRowFunc WebPRescalerImportRowShrink;
  484. // Export one row (starting at x_out position) from rescaler.
  485. // 'Expand' corresponds to the wrk->y_expand case.
  486. // Otherwise 'Shrink' is to be used
  487. typedef void (*WebPRescalerExportRowFunc)(struct WebPRescaler* const wrk);
  488. extern WebPRescalerExportRowFunc WebPRescalerExportRowExpand;
  489. extern WebPRescalerExportRowFunc WebPRescalerExportRowShrink;
  490. // Plain-C implementation, as fall-back.
  491. extern void WebPRescalerImportRowExpand_C(struct WebPRescaler* const wrk,
  492. const uint8_t* src);
  493. extern void WebPRescalerImportRowShrink_C(struct WebPRescaler* const wrk,
  494. const uint8_t* src);
  495. extern void WebPRescalerExportRowExpand_C(struct WebPRescaler* const wrk);
  496. extern void WebPRescalerExportRowShrink_C(struct WebPRescaler* const wrk);
  497. // Main entry calls:
  498. extern void WebPRescalerImportRow(struct WebPRescaler* const wrk,
  499. const uint8_t* src);
  500. // Export one row (starting at x_out position) from rescaler.
  501. extern void WebPRescalerExportRow(struct WebPRescaler* const wrk);
  502. // Must be called first before using the above.
  503. void WebPRescalerDspInit(void);
  504. //------------------------------------------------------------------------------
  505. // Utilities for processing transparent channel.
  506. // Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
  507. // alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
  508. extern void (*WebPApplyAlphaMultiply)(
  509. uint8_t* rgba, int alpha_first, int w, int h, int stride);
  510. // Same, buf specifically for RGBA4444 format
  511. extern void (*WebPApplyAlphaMultiply4444)(
  512. uint8_t* rgba4444, int w, int h, int stride);
  513. // Dispatch the values from alpha[] plane to the ARGB destination 'dst'.
  514. // Returns true if alpha[] plane has non-trivial values different from 0xff.
  515. extern int (*WebPDispatchAlpha)(const uint8_t* WEBP_RESTRICT alpha,
  516. int alpha_stride, int width, int height,
  517. uint8_t* WEBP_RESTRICT dst, int dst_stride);
  518. // Transfer packed 8b alpha[] values to green channel in dst[], zero'ing the
  519. // A/R/B values. 'dst_stride' is the stride for dst[] in uint32_t units.
  520. extern void (*WebPDispatchAlphaToGreen)(const uint8_t* WEBP_RESTRICT alpha,
  521. int alpha_stride, int width, int height,
  522. uint32_t* WEBP_RESTRICT dst,
  523. int dst_stride);
  524. // Extract the alpha values from 32b values in argb[] and pack them into alpha[]
  525. // (this is the opposite of WebPDispatchAlpha).
  526. // Returns true if there's only trivial 0xff alpha values.
  527. extern int (*WebPExtractAlpha)(const uint8_t* WEBP_RESTRICT argb,
  528. int argb_stride, int width, int height,
  529. uint8_t* WEBP_RESTRICT alpha,
  530. int alpha_stride);
  531. // Extract the green values from 32b values in argb[] and pack them into alpha[]
  532. // (this is the opposite of WebPDispatchAlphaToGreen).
  533. extern void (*WebPExtractGreen)(const uint32_t* WEBP_RESTRICT argb,
  534. uint8_t* WEBP_RESTRICT alpha, int size);
  535. // Pre-Multiply operation transforms x into x * A / 255 (where x=Y,R,G or B).
  536. // Un-Multiply operation transforms x into x * 255 / A.
  537. // Pre-Multiply or Un-Multiply (if 'inverse' is true) argb values in a row.
  538. extern void (*WebPMultARGBRow)(uint32_t* const ptr, int width, int inverse);
  539. // Same a WebPMultARGBRow(), but for several rows.
  540. void WebPMultARGBRows(uint8_t* ptr, int stride, int width, int num_rows,
  541. int inverse);
  542. // Same for a row of single values, with side alpha values.
  543. extern void (*WebPMultRow)(uint8_t* WEBP_RESTRICT const ptr,
  544. const uint8_t* WEBP_RESTRICT const alpha,
  545. int width, int inverse);
  546. // Same a WebPMultRow(), but for several 'num_rows' rows.
  547. void WebPMultRows(uint8_t* WEBP_RESTRICT ptr, int stride,
  548. const uint8_t* WEBP_RESTRICT alpha, int alpha_stride,
  549. int width, int num_rows, int inverse);
  550. // Plain-C versions, used as fallback by some implementations.
  551. void WebPMultRow_C(uint8_t* WEBP_RESTRICT const ptr,
  552. const uint8_t* WEBP_RESTRICT const alpha,
  553. int width, int inverse);
  554. void WebPMultARGBRow_C(uint32_t* const ptr, int width, int inverse);
  555. #ifdef WORDS_BIGENDIAN
  556. // ARGB packing function: a/r/g/b input is rgba or bgra order.
  557. extern void (*WebPPackARGB)(const uint8_t* WEBP_RESTRICT a,
  558. const uint8_t* WEBP_RESTRICT r,
  559. const uint8_t* WEBP_RESTRICT g,
  560. const uint8_t* WEBP_RESTRICT b,
  561. int len, uint32_t* WEBP_RESTRICT out);
  562. #endif
  563. // RGB packing function. 'step' can be 3 or 4. r/g/b input is rgb or bgr order.
  564. extern void (*WebPPackRGB)(const uint8_t* WEBP_RESTRICT r,
  565. const uint8_t* WEBP_RESTRICT g,
  566. const uint8_t* WEBP_RESTRICT b,
  567. int len, int step, uint32_t* WEBP_RESTRICT out);
  568. // This function returns true if src[i] contains a value different from 0xff.
  569. extern int (*WebPHasAlpha8b)(const uint8_t* src, int length);
  570. // This function returns true if src[4*i] contains a value different from 0xff.
  571. extern int (*WebPHasAlpha32b)(const uint8_t* src, int length);
  572. // replaces transparent values in src[] by 'color'.
  573. extern void (*WebPAlphaReplace)(uint32_t* src, int length, uint32_t color);
  574. // To be called first before using the above.
  575. void WebPInitAlphaProcessing(void);
  576. //------------------------------------------------------------------------------
  577. // Filter functions
  578. typedef enum { // Filter types.
  579. WEBP_FILTER_NONE = 0,
  580. WEBP_FILTER_HORIZONTAL,
  581. WEBP_FILTER_VERTICAL,
  582. WEBP_FILTER_GRADIENT,
  583. WEBP_FILTER_LAST = WEBP_FILTER_GRADIENT + 1, // end marker
  584. WEBP_FILTER_BEST, // meta-types
  585. WEBP_FILTER_FAST
  586. } WEBP_FILTER_TYPE;
  587. typedef void (*WebPFilterFunc)(const uint8_t* in, int width, int height,
  588. int stride, uint8_t* out);
  589. // In-place un-filtering.
  590. // Warning! 'prev_line' pointer can be equal to 'cur_line' or 'preds'.
  591. typedef void (*WebPUnfilterFunc)(const uint8_t* prev_line, const uint8_t* preds,
  592. uint8_t* cur_line, int width);
  593. // Filter the given data using the given predictor.
  594. // 'in' corresponds to a 2-dimensional pixel array of size (stride * height)
  595. // in raster order.
  596. // 'stride' is number of bytes per scan line (with possible padding).
  597. // 'out' should be pre-allocated.
  598. extern WebPFilterFunc WebPFilters[WEBP_FILTER_LAST];
  599. // In-place reconstruct the original data from the given filtered data.
  600. // The reconstruction will be done for 'num_rows' rows starting from 'row'
  601. // (assuming rows upto 'row - 1' are already reconstructed).
  602. extern WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST];
  603. // To be called first before using the above.
  604. void VP8FiltersInit(void);
  605. #ifdef __cplusplus
  606. } // extern "C"
  607. #endif
  608. #endif // WEBP_DSP_DSP_H_