filters.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. // Spatial prediction using various filters
  11. //
  12. // Author: Urvang (urvang@google.com)
  13. #include "./dsp.h"
  14. #include <assert.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. //------------------------------------------------------------------------------
  18. // Helpful macro.
  19. # define SANITY_CHECK(in, out) \
  20. assert(in != NULL); \
  21. assert(out != NULL); \
  22. assert(width > 0); \
  23. assert(height > 0); \
  24. assert(stride >= width); \
  25. assert(row >= 0 && num_rows > 0 && row + num_rows <= height); \
  26. (void)height; // Silence unused warning.
  27. static WEBP_INLINE void PredictLine(const uint8_t* src, const uint8_t* pred,
  28. uint8_t* dst, int length, int inverse) {
  29. int i;
  30. if (inverse) {
  31. for (i = 0; i < length; ++i) dst[i] = src[i] + pred[i];
  32. } else {
  33. for (i = 0; i < length; ++i) dst[i] = src[i] - pred[i];
  34. }
  35. }
  36. //------------------------------------------------------------------------------
  37. // Horizontal filter.
  38. static WEBP_INLINE void DoHorizontalFilter(const uint8_t* in,
  39. int width, int height, int stride,
  40. int row, int num_rows,
  41. int inverse, uint8_t* out) {
  42. const uint8_t* preds;
  43. const size_t start_offset = row * stride;
  44. const int last_row = row + num_rows;
  45. SANITY_CHECK(in, out);
  46. in += start_offset;
  47. out += start_offset;
  48. preds = inverse ? out : in;
  49. if (row == 0) {
  50. // Leftmost pixel is the same as input for topmost scanline.
  51. out[0] = in[0];
  52. PredictLine(in + 1, preds, out + 1, width - 1, inverse);
  53. row = 1;
  54. preds += stride;
  55. in += stride;
  56. out += stride;
  57. }
  58. // Filter line-by-line.
  59. while (row < last_row) {
  60. // Leftmost pixel is predicted from above.
  61. PredictLine(in, preds - stride, out, 1, inverse);
  62. PredictLine(in + 1, preds, out + 1, width - 1, inverse);
  63. ++row;
  64. preds += stride;
  65. in += stride;
  66. out += stride;
  67. }
  68. }
  69. //------------------------------------------------------------------------------
  70. // Vertical filter.
  71. static WEBP_INLINE void DoVerticalFilter(const uint8_t* in,
  72. int width, int height, int stride,
  73. int row, int num_rows,
  74. int inverse, uint8_t* out) {
  75. const uint8_t* preds;
  76. const size_t start_offset = row * stride;
  77. const int last_row = row + num_rows;
  78. SANITY_CHECK(in, out);
  79. in += start_offset;
  80. out += start_offset;
  81. preds = inverse ? out : in;
  82. if (row == 0) {
  83. // Very first top-left pixel is copied.
  84. out[0] = in[0];
  85. // Rest of top scan-line is left-predicted.
  86. PredictLine(in + 1, preds, out + 1, width - 1, inverse);
  87. row = 1;
  88. in += stride;
  89. out += stride;
  90. } else {
  91. // We are starting from in-between. Make sure 'preds' points to prev row.
  92. preds -= stride;
  93. }
  94. // Filter line-by-line.
  95. while (row < last_row) {
  96. PredictLine(in, preds, out, width, inverse);
  97. ++row;
  98. preds += stride;
  99. in += stride;
  100. out += stride;
  101. }
  102. }
  103. //------------------------------------------------------------------------------
  104. // Gradient filter.
  105. static WEBP_INLINE int GradientPredictor(uint8_t a, uint8_t b, uint8_t c) {
  106. const int g = a + b - c;
  107. return ((g & ~0xff) == 0) ? g : (g < 0) ? 0 : 255; // clip to 8bit
  108. }
  109. static WEBP_INLINE void DoGradientFilter(const uint8_t* in,
  110. int width, int height, int stride,
  111. int row, int num_rows,
  112. int inverse, uint8_t* out) {
  113. const uint8_t* preds;
  114. const size_t start_offset = row * stride;
  115. const int last_row = row + num_rows;
  116. SANITY_CHECK(in, out);
  117. in += start_offset;
  118. out += start_offset;
  119. preds = inverse ? out : in;
  120. // left prediction for top scan-line
  121. if (row == 0) {
  122. out[0] = in[0];
  123. PredictLine(in + 1, preds, out + 1, width - 1, inverse);
  124. row = 1;
  125. preds += stride;
  126. in += stride;
  127. out += stride;
  128. }
  129. // Filter line-by-line.
  130. while (row < last_row) {
  131. int w;
  132. // leftmost pixel: predict from above.
  133. PredictLine(in, preds - stride, out, 1, inverse);
  134. for (w = 1; w < width; ++w) {
  135. const int pred = GradientPredictor(preds[w - 1],
  136. preds[w - stride],
  137. preds[w - stride - 1]);
  138. out[w] = in[w] + (inverse ? pred : -pred);
  139. }
  140. ++row;
  141. preds += stride;
  142. in += stride;
  143. out += stride;
  144. }
  145. }
  146. #undef SANITY_CHECK
  147. //------------------------------------------------------------------------------
  148. static void HorizontalFilter(const uint8_t* data, int width, int height,
  149. int stride, uint8_t* filtered_data) {
  150. DoHorizontalFilter(data, width, height, stride, 0, height, 0, filtered_data);
  151. }
  152. static void VerticalFilter(const uint8_t* data, int width, int height,
  153. int stride, uint8_t* filtered_data) {
  154. DoVerticalFilter(data, width, height, stride, 0, height, 0, filtered_data);
  155. }
  156. static void GradientFilter(const uint8_t* data, int width, int height,
  157. int stride, uint8_t* filtered_data) {
  158. DoGradientFilter(data, width, height, stride, 0, height, 0, filtered_data);
  159. }
  160. //------------------------------------------------------------------------------
  161. static void HorizontalUnfilter(const uint8_t* prev, const uint8_t* in,
  162. uint8_t* out, int width) {
  163. uint8_t pred = (prev == NULL) ? 0 : prev[0];
  164. int i;
  165. for (i = 0; i < width; ++i) {
  166. out[i] = pred + in[i];
  167. pred = out[i];
  168. }
  169. }
  170. static void VerticalUnfilter(const uint8_t* prev, const uint8_t* in,
  171. uint8_t* out, int width) {
  172. if (prev == NULL) {
  173. HorizontalUnfilter(NULL, in, out, width);
  174. } else {
  175. int i;
  176. for (i = 0; i < width; ++i) out[i] = prev[i] + in[i];
  177. }
  178. }
  179. static void GradientUnfilter(const uint8_t* prev, const uint8_t* in,
  180. uint8_t* out, int width) {
  181. if (prev == NULL) {
  182. HorizontalUnfilter(NULL, in, out, width);
  183. } else {
  184. uint8_t top = prev[0], top_left = top, left = top;
  185. int i;
  186. for (i = 0; i < width; ++i) {
  187. top = prev[i]; // need to read this first, in case prev==out
  188. left = in[i] + GradientPredictor(left, top, top_left);
  189. top_left = top;
  190. out[i] = left;
  191. }
  192. }
  193. }
  194. //------------------------------------------------------------------------------
  195. // Init function
  196. WebPFilterFunc WebPFilters[WEBP_FILTER_LAST];
  197. WebPUnfilterFunc WebPUnfilters[WEBP_FILTER_LAST];
  198. extern void VP8FiltersInitMIPSdspR2(void);
  199. extern void VP8FiltersInitMSA(void);
  200. extern void VP8FiltersInitNEON(void);
  201. extern void VP8FiltersInitSSE2(void);
  202. static volatile VP8CPUInfo filters_last_cpuinfo_used =
  203. (VP8CPUInfo)&filters_last_cpuinfo_used;
  204. WEBP_TSAN_IGNORE_FUNCTION void VP8FiltersInit(void) {
  205. if (filters_last_cpuinfo_used == VP8GetCPUInfo) return;
  206. WebPUnfilters[WEBP_FILTER_NONE] = NULL;
  207. WebPUnfilters[WEBP_FILTER_HORIZONTAL] = HorizontalUnfilter;
  208. WebPUnfilters[WEBP_FILTER_VERTICAL] = VerticalUnfilter;
  209. WebPUnfilters[WEBP_FILTER_GRADIENT] = GradientUnfilter;
  210. WebPFilters[WEBP_FILTER_NONE] = NULL;
  211. WebPFilters[WEBP_FILTER_HORIZONTAL] = HorizontalFilter;
  212. WebPFilters[WEBP_FILTER_VERTICAL] = VerticalFilter;
  213. WebPFilters[WEBP_FILTER_GRADIENT] = GradientFilter;
  214. if (VP8GetCPUInfo != NULL) {
  215. #if defined(WEBP_USE_SSE2)
  216. if (VP8GetCPUInfo(kSSE2)) {
  217. VP8FiltersInitSSE2();
  218. }
  219. #endif
  220. #if defined(WEBP_USE_NEON)
  221. if (VP8GetCPUInfo(kNEON)) {
  222. VP8FiltersInitNEON();
  223. }
  224. #endif
  225. #if defined(WEBP_USE_MIPS_DSP_R2)
  226. if (VP8GetCPUInfo(kMIPSdspR2)) {
  227. VP8FiltersInitMIPSdspR2();
  228. }
  229. #endif
  230. #if defined(WEBP_USE_MSA)
  231. if (VP8GetCPUInfo(kMSA)) {
  232. VP8FiltersInitMSA();
  233. }
  234. #endif
  235. }
  236. filters_last_cpuinfo_used = VP8GetCPUInfo;
  237. }