dsp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. //------------------------------------------------------------------------------
  23. // CPU detection
  24. #if defined(__GNUC__)
  25. # define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
  26. # define LOCAL_GCC_PREREQ(maj, min) \
  27. (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
  28. #else
  29. # define LOCAL_GCC_VERSION 0
  30. # define LOCAL_GCC_PREREQ(maj, min) 0
  31. #endif
  32. #ifdef __clang__
  33. # define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
  34. # define LOCAL_CLANG_PREREQ(maj, min) \
  35. (LOCAL_CLANG_VERSION >= (((maj) << 8) | (min)))
  36. #else
  37. # define LOCAL_CLANG_VERSION 0
  38. # define LOCAL_CLANG_PREREQ(maj, min) 0
  39. #endif // __clang__
  40. #if defined(_MSC_VER) && _MSC_VER > 1310 && \
  41. (defined(_M_X64) || defined(_M_IX86))
  42. #define WEBP_MSC_SSE2 // Visual C++ SSE2 targets
  43. #endif
  44. // WEBP_HAVE_* are used to indicate the presence of the instruction set in dsp
  45. // files without intrinsics, allowing the corresponding Init() to be called.
  46. // Files containing intrinsics will need to be built targeting the instruction
  47. // set so should succeed on one of the earlier tests.
  48. #if defined(__SSE2__) || defined(WEBP_MSC_SSE2) || defined(WEBP_HAVE_SSE2)
  49. #define WEBP_USE_SSE2
  50. #endif
  51. #if defined(__AVX2__) || defined(WEBP_HAVE_AVX2)
  52. #define WEBP_USE_AVX2
  53. #endif
  54. #if defined(__ANDROID__) && defined(__ARM_ARCH_7A__)
  55. #define WEBP_ANDROID_NEON // Android targets that might support NEON
  56. #endif
  57. #if defined(__ARM_NEON__) || defined(WEBP_ANDROID_NEON) || defined(__aarch64__)
  58. #define WEBP_USE_NEON
  59. #endif
  60. #if defined(__mips__) && !defined(__mips64) && (__mips_isa_rev < 6)
  61. #define WEBP_USE_MIPS32
  62. #if (__mips_isa_rev >= 2)
  63. #define WEBP_USE_MIPS32_R2
  64. #endif
  65. #endif
  66. typedef enum {
  67. kSSE2,
  68. kSSE3,
  69. kAVX,
  70. kAVX2,
  71. kNEON,
  72. kMIPS32
  73. } CPUFeature;
  74. // returns true if the CPU supports the feature.
  75. typedef int (*VP8CPUInfo)(CPUFeature feature);
  76. extern VP8CPUInfo VP8GetCPUInfo;
  77. //------------------------------------------------------------------------------
  78. // Encoding
  79. // Transforms
  80. // VP8Idct: Does one of two inverse transforms. If do_two is set, the transforms
  81. // will be done for (ref, in, dst) and (ref + 4, in + 16, dst + 4).
  82. typedef void (*VP8Idct)(const uint8_t* ref, const int16_t* in, uint8_t* dst,
  83. int do_two);
  84. typedef void (*VP8Fdct)(const uint8_t* src, const uint8_t* ref, int16_t* out);
  85. typedef void (*VP8WHT)(const int16_t* in, int16_t* out);
  86. extern VP8Idct VP8ITransform;
  87. extern VP8Fdct VP8FTransform;
  88. extern VP8WHT VP8FTransformWHT;
  89. // Predictions
  90. // *dst is the destination block. *top and *left can be NULL.
  91. typedef void (*VP8IntraPreds)(uint8_t *dst, const uint8_t* left,
  92. const uint8_t* top);
  93. typedef void (*VP8Intra4Preds)(uint8_t *dst, const uint8_t* top);
  94. extern VP8Intra4Preds VP8EncPredLuma4;
  95. extern VP8IntraPreds VP8EncPredLuma16;
  96. extern VP8IntraPreds VP8EncPredChroma8;
  97. typedef int (*VP8Metric)(const uint8_t* pix, const uint8_t* ref);
  98. extern VP8Metric VP8SSE16x16, VP8SSE16x8, VP8SSE8x8, VP8SSE4x4;
  99. typedef int (*VP8WMetric)(const uint8_t* pix, const uint8_t* ref,
  100. const uint16_t* const weights);
  101. extern VP8WMetric VP8TDisto4x4, VP8TDisto16x16;
  102. typedef void (*VP8BlockCopy)(const uint8_t* src, uint8_t* dst);
  103. extern VP8BlockCopy VP8Copy4x4;
  104. // Quantization
  105. struct VP8Matrix; // forward declaration
  106. typedef int (*VP8QuantizeBlock)(int16_t in[16], int16_t out[16],
  107. const struct VP8Matrix* const mtx);
  108. extern VP8QuantizeBlock VP8EncQuantizeBlock;
  109. // specific to 2nd transform:
  110. typedef int (*VP8QuantizeBlockWHT)(int16_t in[16], int16_t out[16],
  111. const struct VP8Matrix* const mtx);
  112. extern VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
  113. // Collect histogram for susceptibility calculation and accumulate in histo[].
  114. struct VP8Histogram;
  115. typedef void (*VP8CHisto)(const uint8_t* ref, const uint8_t* pred,
  116. int start_block, int end_block,
  117. struct VP8Histogram* const histo);
  118. extern const int VP8DspScan[16 + 4 + 4];
  119. extern VP8CHisto VP8CollectHistogram;
  120. void VP8EncDspInit(void); // must be called before using any of the above
  121. //------------------------------------------------------------------------------
  122. // Decoding
  123. typedef void (*VP8DecIdct)(const int16_t* coeffs, uint8_t* dst);
  124. // when doing two transforms, coeffs is actually int16_t[2][16].
  125. typedef void (*VP8DecIdct2)(const int16_t* coeffs, uint8_t* dst, int do_two);
  126. extern VP8DecIdct2 VP8Transform;
  127. extern VP8DecIdct VP8TransformAC3;
  128. extern VP8DecIdct VP8TransformUV;
  129. extern VP8DecIdct VP8TransformDC;
  130. extern VP8DecIdct VP8TransformDCUV;
  131. extern VP8WHT VP8TransformWHT;
  132. // *dst is the destination block, with stride BPS. Boundary samples are
  133. // assumed accessible when needed.
  134. typedef void (*VP8PredFunc)(uint8_t* dst);
  135. extern const VP8PredFunc VP8PredLuma16[/* NUM_B_DC_MODES */];
  136. extern const VP8PredFunc VP8PredChroma8[/* NUM_B_DC_MODES */];
  137. extern const VP8PredFunc VP8PredLuma4[/* NUM_BMODES */];
  138. // clipping tables (for filtering)
  139. extern const int8_t* const VP8ksclip1; // clips [-1020, 1020] to [-128, 127]
  140. extern const int8_t* const VP8ksclip2; // clips [-112, 112] to [-16, 15]
  141. extern const uint8_t* const VP8kclip1; // clips [-255,511] to [0,255]
  142. extern const uint8_t* const VP8kabs0; // abs(x) for x in [-255,255]
  143. void VP8InitClipTables(void); // must be called first
  144. // simple filter (only for luma)
  145. typedef void (*VP8SimpleFilterFunc)(uint8_t* p, int stride, int thresh);
  146. extern VP8SimpleFilterFunc VP8SimpleVFilter16;
  147. extern VP8SimpleFilterFunc VP8SimpleHFilter16;
  148. extern VP8SimpleFilterFunc VP8SimpleVFilter16i; // filter 3 inner edges
  149. extern VP8SimpleFilterFunc VP8SimpleHFilter16i;
  150. // regular filter (on both macroblock edges and inner edges)
  151. typedef void (*VP8LumaFilterFunc)(uint8_t* luma, int stride,
  152. int thresh, int ithresh, int hev_t);
  153. typedef void (*VP8ChromaFilterFunc)(uint8_t* u, uint8_t* v, int stride,
  154. int thresh, int ithresh, int hev_t);
  155. // on outer edge
  156. extern VP8LumaFilterFunc VP8VFilter16;
  157. extern VP8LumaFilterFunc VP8HFilter16;
  158. extern VP8ChromaFilterFunc VP8VFilter8;
  159. extern VP8ChromaFilterFunc VP8HFilter8;
  160. // on inner edge
  161. extern VP8LumaFilterFunc VP8VFilter16i; // filtering 3 inner edges altogether
  162. extern VP8LumaFilterFunc VP8HFilter16i;
  163. extern VP8ChromaFilterFunc VP8VFilter8i; // filtering u and v altogether
  164. extern VP8ChromaFilterFunc VP8HFilter8i;
  165. // must be called before anything using the above
  166. void VP8DspInit(void);
  167. //------------------------------------------------------------------------------
  168. // WebP I/O
  169. #define FANCY_UPSAMPLING // undefined to remove fancy upsampling support
  170. // Convert a pair of y/u/v lines together to the output rgb/a colorspace.
  171. // bottom_y can be NULL if only one line of output is needed (at top/bottom).
  172. typedef void (*WebPUpsampleLinePairFunc)(
  173. const uint8_t* top_y, const uint8_t* bottom_y,
  174. const uint8_t* top_u, const uint8_t* top_v,
  175. const uint8_t* cur_u, const uint8_t* cur_v,
  176. uint8_t* top_dst, uint8_t* bottom_dst, int len);
  177. #ifdef FANCY_UPSAMPLING
  178. // Fancy upsampling functions to convert YUV to RGB(A) modes
  179. extern WebPUpsampleLinePairFunc WebPUpsamplers[/* MODE_LAST */];
  180. #endif // FANCY_UPSAMPLING
  181. // Per-row point-sampling methods.
  182. typedef void (*WebPSamplerRowFunc)(const uint8_t* y,
  183. const uint8_t* u, const uint8_t* v,
  184. uint8_t* dst, int len);
  185. // Generic function to apply 'WebPSamplerRowFunc' to the whole plane:
  186. void WebPSamplerProcessPlane(const uint8_t* y, int y_stride,
  187. const uint8_t* u, const uint8_t* v, int uv_stride,
  188. uint8_t* dst, int dst_stride,
  189. int width, int height, WebPSamplerRowFunc func);
  190. // Sampling functions to convert rows of YUV to RGB(A)
  191. extern WebPSamplerRowFunc WebPSamplers[/* MODE_LAST */];
  192. // General function for converting two lines of ARGB or RGBA.
  193. // 'alpha_is_last' should be true if 0xff000000 is stored in memory as
  194. // as 0x00, 0x00, 0x00, 0xff (little endian).
  195. WebPUpsampleLinePairFunc WebPGetLinePairConverter(int alpha_is_last);
  196. // YUV444->RGB converters
  197. typedef void (*WebPYUV444Converter)(const uint8_t* y,
  198. const uint8_t* u, const uint8_t* v,
  199. uint8_t* dst, int len);
  200. extern const WebPYUV444Converter WebPYUV444Converters[/* MODE_LAST */];
  201. // Must be called before using the WebPUpsamplers[] (and for premultiplied
  202. // colorspaces like rgbA, rgbA4444, etc)
  203. void WebPInitUpsamplers(void);
  204. // Must be called before using WebPSamplers[]
  205. void WebPInitSamplers(void);
  206. //------------------------------------------------------------------------------
  207. // Utilities for processing transparent channel.
  208. // Apply alpha pre-multiply on an rgba, bgra or argb plane of size w * h.
  209. // alpha_first should be 0 for argb, 1 for rgba or bgra (where alpha is last).
  210. extern void (*WebPApplyAlphaMultiply)(
  211. uint8_t* rgba, int alpha_first, int w, int h, int stride);
  212. // Same, buf specifically for RGBA4444 format
  213. extern void (*WebPApplyAlphaMultiply4444)(
  214. uint8_t* rgba4444, int w, int h, int stride);
  215. // Extract the alpha values from 32b values in argb[] and pack them into alpha[]
  216. // (this is the opposite of WebPDispatchAlpha).
  217. // Returns true if there's only trivial 0xff alpha values.
  218. extern int (*WebPExtractAlpha)(const uint8_t* argb, int argb_stride,
  219. int width, int height,
  220. uint8_t* alpha, int alpha_stride);
  221. // Pre-Multiply operation transforms x into x * A / 255 (where x=Y,R,G or B).
  222. // Un-Multiply operation transforms x into x * 255 / A.
  223. // Pre-Multiply or Un-Multiply (if 'inverse' is true) argb values in a row.
  224. extern void (*WebPMultARGBRow)(uint32_t* const ptr, int width, int inverse);
  225. // Same a WebPMultARGBRow(), but for several rows.
  226. void WebPMultARGBRows(uint8_t* ptr, int stride, int width, int num_rows,
  227. int inverse);
  228. // Same for a row of single values, with side alpha values.
  229. extern void (*WebPMultRow)(uint8_t* const ptr, const uint8_t* const alpha,
  230. int width, int inverse);
  231. // Same a WebPMultRow(), but for several 'num_rows' rows.
  232. void WebPMultRows(uint8_t* ptr, int stride,
  233. const uint8_t* alpha, int alpha_stride,
  234. int width, int num_rows, int inverse);
  235. // To be called first before using the above.
  236. void WebPInitAlphaProcessing(void);
  237. #ifdef __cplusplus
  238. } // extern "C"
  239. #endif
  240. #endif /* WEBP_DSP_DSP_H_ */