dec.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. // Copyright 2010 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 decoding functions.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "./dsp.h"
  14. #include "../dec/vp8i.h"
  15. //------------------------------------------------------------------------------
  16. static WEBP_INLINE uint8_t clip_8b(int v) {
  17. return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
  18. }
  19. //------------------------------------------------------------------------------
  20. // Transforms (Paragraph 14.4)
  21. #define STORE(x, y, v) \
  22. dst[x + y * BPS] = clip_8b(dst[x + y * BPS] + ((v) >> 3))
  23. #define STORE2(y, dc, d, c) do { \
  24. const int DC = (dc); \
  25. STORE(0, y, DC + (d)); \
  26. STORE(1, y, DC + (c)); \
  27. STORE(2, y, DC - (c)); \
  28. STORE(3, y, DC - (d)); \
  29. } while (0)
  30. static const int kC1 = 20091 + (1 << 16);
  31. static const int kC2 = 35468;
  32. #define MUL(a, b) (((a) * (b)) >> 16)
  33. static void TransformOne(const int16_t* in, uint8_t* dst) {
  34. int C[4 * 4], *tmp;
  35. int i;
  36. tmp = C;
  37. for (i = 0; i < 4; ++i) { // vertical pass
  38. const int a = in[0] + in[8]; // [-4096, 4094]
  39. const int b = in[0] - in[8]; // [-4095, 4095]
  40. const int c = MUL(in[4], kC2) - MUL(in[12], kC1); // [-3783, 3783]
  41. const int d = MUL(in[4], kC1) + MUL(in[12], kC2); // [-3785, 3781]
  42. tmp[0] = a + d; // [-7881, 7875]
  43. tmp[1] = b + c; // [-7878, 7878]
  44. tmp[2] = b - c; // [-7878, 7878]
  45. tmp[3] = a - d; // [-7877, 7879]
  46. tmp += 4;
  47. in++;
  48. }
  49. // Each pass is expanding the dynamic range by ~3.85 (upper bound).
  50. // The exact value is (2. + (kC1 + kC2) / 65536).
  51. // After the second pass, maximum interval is [-3794, 3794], assuming
  52. // an input in [-2048, 2047] interval. We then need to add a dst value
  53. // in the [0, 255] range.
  54. // In the worst case scenario, the input to clip_8b() can be as large as
  55. // [-60713, 60968].
  56. tmp = C;
  57. for (i = 0; i < 4; ++i) { // horizontal pass
  58. const int dc = tmp[0] + 4;
  59. const int a = dc + tmp[8];
  60. const int b = dc - tmp[8];
  61. const int c = MUL(tmp[4], kC2) - MUL(tmp[12], kC1);
  62. const int d = MUL(tmp[4], kC1) + MUL(tmp[12], kC2);
  63. STORE(0, 0, a + d);
  64. STORE(1, 0, b + c);
  65. STORE(2, 0, b - c);
  66. STORE(3, 0, a - d);
  67. tmp++;
  68. dst += BPS;
  69. }
  70. }
  71. // Simplified transform when only in[0], in[1] and in[4] are non-zero
  72. static void TransformAC3(const int16_t* in, uint8_t* dst) {
  73. const int a = in[0] + 4;
  74. const int c4 = MUL(in[4], kC2);
  75. const int d4 = MUL(in[4], kC1);
  76. const int c1 = MUL(in[1], kC2);
  77. const int d1 = MUL(in[1], kC1);
  78. STORE2(0, a + d4, d1, c1);
  79. STORE2(1, a + c4, d1, c1);
  80. STORE2(2, a - c4, d1, c1);
  81. STORE2(3, a - d4, d1, c1);
  82. }
  83. #undef MUL
  84. #undef STORE2
  85. static void TransformTwo(const int16_t* in, uint8_t* dst, int do_two) {
  86. TransformOne(in, dst);
  87. if (do_two) {
  88. TransformOne(in + 16, dst + 4);
  89. }
  90. }
  91. static void TransformUV(const int16_t* in, uint8_t* dst) {
  92. VP8Transform(in + 0 * 16, dst, 1);
  93. VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
  94. }
  95. static void TransformDC(const int16_t *in, uint8_t* dst) {
  96. const int DC = in[0] + 4;
  97. int i, j;
  98. for (j = 0; j < 4; ++j) {
  99. for (i = 0; i < 4; ++i) {
  100. STORE(i, j, DC);
  101. }
  102. }
  103. }
  104. static void TransformDCUV(const int16_t* in, uint8_t* dst) {
  105. if (in[0 * 16]) VP8TransformDC(in + 0 * 16, dst);
  106. if (in[1 * 16]) VP8TransformDC(in + 1 * 16, dst + 4);
  107. if (in[2 * 16]) VP8TransformDC(in + 2 * 16, dst + 4 * BPS);
  108. if (in[3 * 16]) VP8TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
  109. }
  110. #undef STORE
  111. //------------------------------------------------------------------------------
  112. // Paragraph 14.3
  113. static void TransformWHT(const int16_t* in, int16_t* out) {
  114. int tmp[16];
  115. int i;
  116. for (i = 0; i < 4; ++i) {
  117. const int a0 = in[0 + i] + in[12 + i];
  118. const int a1 = in[4 + i] + in[ 8 + i];
  119. const int a2 = in[4 + i] - in[ 8 + i];
  120. const int a3 = in[0 + i] - in[12 + i];
  121. tmp[0 + i] = a0 + a1;
  122. tmp[8 + i] = a0 - a1;
  123. tmp[4 + i] = a3 + a2;
  124. tmp[12 + i] = a3 - a2;
  125. }
  126. for (i = 0; i < 4; ++i) {
  127. const int dc = tmp[0 + i * 4] + 3; // w/ rounder
  128. const int a0 = dc + tmp[3 + i * 4];
  129. const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
  130. const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
  131. const int a3 = dc - tmp[3 + i * 4];
  132. out[ 0] = (a0 + a1) >> 3;
  133. out[16] = (a3 + a2) >> 3;
  134. out[32] = (a0 - a1) >> 3;
  135. out[48] = (a3 - a2) >> 3;
  136. out += 64;
  137. }
  138. }
  139. void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
  140. //------------------------------------------------------------------------------
  141. // Intra predictions
  142. #define DST(x, y) dst[(x) + (y) * BPS]
  143. static WEBP_INLINE void TrueMotion(uint8_t *dst, int size) {
  144. const uint8_t* top = dst - BPS;
  145. const uint8_t* const clip0 = VP8kclip1 - top[-1];
  146. int y;
  147. for (y = 0; y < size; ++y) {
  148. const uint8_t* const clip = clip0 + dst[-1];
  149. int x;
  150. for (x = 0; x < size; ++x) {
  151. dst[x] = clip[top[x]];
  152. }
  153. dst += BPS;
  154. }
  155. }
  156. static void TM4(uint8_t *dst) { TrueMotion(dst, 4); }
  157. static void TM8uv(uint8_t *dst) { TrueMotion(dst, 8); }
  158. static void TM16(uint8_t *dst) { TrueMotion(dst, 16); }
  159. //------------------------------------------------------------------------------
  160. // 16x16
  161. static void VE16(uint8_t *dst) { // vertical
  162. int j;
  163. for (j = 0; j < 16; ++j) {
  164. memcpy(dst + j * BPS, dst - BPS, 16);
  165. }
  166. }
  167. static void HE16(uint8_t *dst) { // horizontal
  168. int j;
  169. for (j = 16; j > 0; --j) {
  170. memset(dst, dst[-1], 16);
  171. dst += BPS;
  172. }
  173. }
  174. static WEBP_INLINE void Put16(int v, uint8_t* dst) {
  175. int j;
  176. for (j = 0; j < 16; ++j) {
  177. memset(dst + j * BPS, v, 16);
  178. }
  179. }
  180. static void DC16(uint8_t *dst) { // DC
  181. int DC = 16;
  182. int j;
  183. for (j = 0; j < 16; ++j) {
  184. DC += dst[-1 + j * BPS] + dst[j - BPS];
  185. }
  186. Put16(DC >> 5, dst);
  187. }
  188. static void DC16NoTop(uint8_t *dst) { // DC with top samples not available
  189. int DC = 8;
  190. int j;
  191. for (j = 0; j < 16; ++j) {
  192. DC += dst[-1 + j * BPS];
  193. }
  194. Put16(DC >> 4, dst);
  195. }
  196. static void DC16NoLeft(uint8_t *dst) { // DC with left samples not available
  197. int DC = 8;
  198. int i;
  199. for (i = 0; i < 16; ++i) {
  200. DC += dst[i - BPS];
  201. }
  202. Put16(DC >> 4, dst);
  203. }
  204. static void DC16NoTopLeft(uint8_t *dst) { // DC with no top and left samples
  205. Put16(0x80, dst);
  206. }
  207. //------------------------------------------------------------------------------
  208. // 4x4
  209. #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
  210. #define AVG2(a, b) (((a) + (b) + 1) >> 1)
  211. static void VE4(uint8_t *dst) { // vertical
  212. const uint8_t* top = dst - BPS;
  213. const uint8_t vals[4] = {
  214. AVG3(top[-1], top[0], top[1]),
  215. AVG3(top[ 0], top[1], top[2]),
  216. AVG3(top[ 1], top[2], top[3]),
  217. AVG3(top[ 2], top[3], top[4])
  218. };
  219. int i;
  220. for (i = 0; i < 4; ++i) {
  221. memcpy(dst + i * BPS, vals, sizeof(vals));
  222. }
  223. }
  224. static void HE4(uint8_t *dst) { // horizontal
  225. const int A = dst[-1 - BPS];
  226. const int B = dst[-1];
  227. const int C = dst[-1 + BPS];
  228. const int D = dst[-1 + 2 * BPS];
  229. const int E = dst[-1 + 3 * BPS];
  230. *(uint32_t*)(dst + 0 * BPS) = 0x01010101U * AVG3(A, B, C);
  231. *(uint32_t*)(dst + 1 * BPS) = 0x01010101U * AVG3(B, C, D);
  232. *(uint32_t*)(dst + 2 * BPS) = 0x01010101U * AVG3(C, D, E);
  233. *(uint32_t*)(dst + 3 * BPS) = 0x01010101U * AVG3(D, E, E);
  234. }
  235. static void DC4(uint8_t *dst) { // DC
  236. uint32_t dc = 4;
  237. int i;
  238. for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
  239. dc >>= 3;
  240. for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
  241. }
  242. static void RD4(uint8_t *dst) { // Down-right
  243. const int I = dst[-1 + 0 * BPS];
  244. const int J = dst[-1 + 1 * BPS];
  245. const int K = dst[-1 + 2 * BPS];
  246. const int L = dst[-1 + 3 * BPS];
  247. const int X = dst[-1 - BPS];
  248. const int A = dst[0 - BPS];
  249. const int B = dst[1 - BPS];
  250. const int C = dst[2 - BPS];
  251. const int D = dst[3 - BPS];
  252. DST(0, 3) = AVG3(J, K, L);
  253. DST(0, 2) = DST(1, 3) = AVG3(I, J, K);
  254. DST(0, 1) = DST(1, 2) = DST(2, 3) = AVG3(X, I, J);
  255. DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
  256. DST(1, 0) = DST(2, 1) = DST(3, 2) = AVG3(B, A, X);
  257. DST(2, 0) = DST(3, 1) = AVG3(C, B, A);
  258. DST(3, 0) = AVG3(D, C, B);
  259. }
  260. static void LD4(uint8_t *dst) { // Down-Left
  261. const int A = dst[0 - BPS];
  262. const int B = dst[1 - BPS];
  263. const int C = dst[2 - BPS];
  264. const int D = dst[3 - BPS];
  265. const int E = dst[4 - BPS];
  266. const int F = dst[5 - BPS];
  267. const int G = dst[6 - BPS];
  268. const int H = dst[7 - BPS];
  269. DST(0, 0) = AVG3(A, B, C);
  270. DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
  271. DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
  272. DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
  273. DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
  274. DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
  275. DST(3, 3) = AVG3(G, H, H);
  276. }
  277. static void VR4(uint8_t *dst) { // Vertical-Right
  278. const int I = dst[-1 + 0 * BPS];
  279. const int J = dst[-1 + 1 * BPS];
  280. const int K = dst[-1 + 2 * BPS];
  281. const int X = dst[-1 - BPS];
  282. const int A = dst[0 - BPS];
  283. const int B = dst[1 - BPS];
  284. const int C = dst[2 - BPS];
  285. const int D = dst[3 - BPS];
  286. DST(0, 0) = DST(1, 2) = AVG2(X, A);
  287. DST(1, 0) = DST(2, 2) = AVG2(A, B);
  288. DST(2, 0) = DST(3, 2) = AVG2(B, C);
  289. DST(3, 0) = AVG2(C, D);
  290. DST(0, 3) = AVG3(K, J, I);
  291. DST(0, 2) = AVG3(J, I, X);
  292. DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
  293. DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
  294. DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
  295. DST(3, 1) = AVG3(B, C, D);
  296. }
  297. static void VL4(uint8_t *dst) { // Vertical-Left
  298. const int A = dst[0 - BPS];
  299. const int B = dst[1 - BPS];
  300. const int C = dst[2 - BPS];
  301. const int D = dst[3 - BPS];
  302. const int E = dst[4 - BPS];
  303. const int F = dst[5 - BPS];
  304. const int G = dst[6 - BPS];
  305. const int H = dst[7 - BPS];
  306. DST(0, 0) = AVG2(A, B);
  307. DST(1, 0) = DST(0, 2) = AVG2(B, C);
  308. DST(2, 0) = DST(1, 2) = AVG2(C, D);
  309. DST(3, 0) = DST(2, 2) = AVG2(D, E);
  310. DST(0, 1) = AVG3(A, B, C);
  311. DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
  312. DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
  313. DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
  314. DST(3, 2) = AVG3(E, F, G);
  315. DST(3, 3) = AVG3(F, G, H);
  316. }
  317. static void HU4(uint8_t *dst) { // Horizontal-Up
  318. const int I = dst[-1 + 0 * BPS];
  319. const int J = dst[-1 + 1 * BPS];
  320. const int K = dst[-1 + 2 * BPS];
  321. const int L = dst[-1 + 3 * BPS];
  322. DST(0, 0) = AVG2(I, J);
  323. DST(2, 0) = DST(0, 1) = AVG2(J, K);
  324. DST(2, 1) = DST(0, 2) = AVG2(K, L);
  325. DST(1, 0) = AVG3(I, J, K);
  326. DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
  327. DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
  328. DST(3, 2) = DST(2, 2) =
  329. DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
  330. }
  331. static void HD4(uint8_t *dst) { // Horizontal-Down
  332. const int I = dst[-1 + 0 * BPS];
  333. const int J = dst[-1 + 1 * BPS];
  334. const int K = dst[-1 + 2 * BPS];
  335. const int L = dst[-1 + 3 * BPS];
  336. const int X = dst[-1 - BPS];
  337. const int A = dst[0 - BPS];
  338. const int B = dst[1 - BPS];
  339. const int C = dst[2 - BPS];
  340. DST(0, 0) = DST(2, 1) = AVG2(I, X);
  341. DST(0, 1) = DST(2, 2) = AVG2(J, I);
  342. DST(0, 2) = DST(2, 3) = AVG2(K, J);
  343. DST(0, 3) = AVG2(L, K);
  344. DST(3, 0) = AVG3(A, B, C);
  345. DST(2, 0) = AVG3(X, A, B);
  346. DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
  347. DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
  348. DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
  349. DST(1, 3) = AVG3(L, K, J);
  350. }
  351. #undef DST
  352. #undef AVG3
  353. #undef AVG2
  354. //------------------------------------------------------------------------------
  355. // Chroma
  356. static void VE8uv(uint8_t *dst) { // vertical
  357. int j;
  358. for (j = 0; j < 8; ++j) {
  359. memcpy(dst + j * BPS, dst - BPS, 8);
  360. }
  361. }
  362. static void HE8uv(uint8_t *dst) { // horizontal
  363. int j;
  364. for (j = 0; j < 8; ++j) {
  365. memset(dst, dst[-1], 8);
  366. dst += BPS;
  367. }
  368. }
  369. // helper for chroma-DC predictions
  370. static WEBP_INLINE void Put8x8uv(uint8_t value, uint8_t* dst) {
  371. int j;
  372. for (j = 0; j < 8; ++j) {
  373. memset(dst + j * BPS, value, 8);
  374. }
  375. }
  376. static void DC8uv(uint8_t *dst) { // DC
  377. int dc0 = 8;
  378. int i;
  379. for (i = 0; i < 8; ++i) {
  380. dc0 += dst[i - BPS] + dst[-1 + i * BPS];
  381. }
  382. Put8x8uv(dc0 >> 4, dst);
  383. }
  384. static void DC8uvNoLeft(uint8_t *dst) { // DC with no left samples
  385. int dc0 = 4;
  386. int i;
  387. for (i = 0; i < 8; ++i) {
  388. dc0 += dst[i - BPS];
  389. }
  390. Put8x8uv(dc0 >> 3, dst);
  391. }
  392. static void DC8uvNoTop(uint8_t *dst) { // DC with no top samples
  393. int dc0 = 4;
  394. int i;
  395. for (i = 0; i < 8; ++i) {
  396. dc0 += dst[-1 + i * BPS];
  397. }
  398. Put8x8uv(dc0 >> 3, dst);
  399. }
  400. static void DC8uvNoTopLeft(uint8_t *dst) { // DC with nothing
  401. Put8x8uv(0x80, dst);
  402. }
  403. //------------------------------------------------------------------------------
  404. // default C implementations
  405. const VP8PredFunc VP8PredLuma4[NUM_BMODES] = {
  406. DC4, TM4, VE4, HE4, RD4, VR4, LD4, VL4, HD4, HU4
  407. };
  408. const VP8PredFunc VP8PredLuma16[NUM_B_DC_MODES] = {
  409. DC16, TM16, VE16, HE16,
  410. DC16NoTop, DC16NoLeft, DC16NoTopLeft
  411. };
  412. const VP8PredFunc VP8PredChroma8[NUM_B_DC_MODES] = {
  413. DC8uv, TM8uv, VE8uv, HE8uv,
  414. DC8uvNoTop, DC8uvNoLeft, DC8uvNoTopLeft
  415. };
  416. //------------------------------------------------------------------------------
  417. // Edge filtering functions
  418. // 4 pixels in, 2 pixels out
  419. static WEBP_INLINE void do_filter2(uint8_t* p, int step) {
  420. const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
  421. const int a = 3 * (q0 - p0) + VP8ksclip1[p1 - q1]; // in [-893,892]
  422. const int a1 = VP8ksclip2[(a + 4) >> 3]; // in [-16,15]
  423. const int a2 = VP8ksclip2[(a + 3) >> 3];
  424. p[-step] = VP8kclip1[p0 + a2];
  425. p[ 0] = VP8kclip1[q0 - a1];
  426. }
  427. // 4 pixels in, 4 pixels out
  428. static WEBP_INLINE void do_filter4(uint8_t* p, int step) {
  429. const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
  430. const int a = 3 * (q0 - p0);
  431. const int a1 = VP8ksclip2[(a + 4) >> 3];
  432. const int a2 = VP8ksclip2[(a + 3) >> 3];
  433. const int a3 = (a1 + 1) >> 1;
  434. p[-2*step] = VP8kclip1[p1 + a3];
  435. p[- step] = VP8kclip1[p0 + a2];
  436. p[ 0] = VP8kclip1[q0 - a1];
  437. p[ step] = VP8kclip1[q1 - a3];
  438. }
  439. // 6 pixels in, 6 pixels out
  440. static WEBP_INLINE void do_filter6(uint8_t* p, int step) {
  441. const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
  442. const int q0 = p[0], q1 = p[step], q2 = p[2*step];
  443. const int a = VP8ksclip1[3 * (q0 - p0) + VP8ksclip1[p1 - q1]];
  444. // a is in [-128,127], a1 in [-27,27], a2 in [-18,18] and a3 in [-9,9]
  445. const int a1 = (27 * a + 63) >> 7; // eq. to ((3 * a + 7) * 9) >> 7
  446. const int a2 = (18 * a + 63) >> 7; // eq. to ((2 * a + 7) * 9) >> 7
  447. const int a3 = (9 * a + 63) >> 7; // eq. to ((1 * a + 7) * 9) >> 7
  448. p[-3*step] = VP8kclip1[p2 + a3];
  449. p[-2*step] = VP8kclip1[p1 + a2];
  450. p[- step] = VP8kclip1[p0 + a1];
  451. p[ 0] = VP8kclip1[q0 - a1];
  452. p[ step] = VP8kclip1[q1 - a2];
  453. p[ 2*step] = VP8kclip1[q2 - a3];
  454. }
  455. static WEBP_INLINE int hev(const uint8_t* p, int step, int thresh) {
  456. const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
  457. return (VP8kabs0[p1 - p0] > thresh) || (VP8kabs0[q1 - q0] > thresh);
  458. }
  459. static WEBP_INLINE int needs_filter(const uint8_t* p, int step, int t) {
  460. const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
  461. return ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) <= t);
  462. }
  463. static WEBP_INLINE int needs_filter2(const uint8_t* p,
  464. int step, int t, int it) {
  465. const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step];
  466. const int p0 = p[-step], q0 = p[0];
  467. const int q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
  468. if ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) > t) return 0;
  469. return VP8kabs0[p3 - p2] <= it && VP8kabs0[p2 - p1] <= it &&
  470. VP8kabs0[p1 - p0] <= it && VP8kabs0[q3 - q2] <= it &&
  471. VP8kabs0[q2 - q1] <= it && VP8kabs0[q1 - q0] <= it;
  472. }
  473. //------------------------------------------------------------------------------
  474. // Simple In-loop filtering (Paragraph 15.2)
  475. static void SimpleVFilter16(uint8_t* p, int stride, int thresh) {
  476. int i;
  477. const int thresh2 = 2 * thresh + 1;
  478. for (i = 0; i < 16; ++i) {
  479. if (needs_filter(p + i, stride, thresh2)) {
  480. do_filter2(p + i, stride);
  481. }
  482. }
  483. }
  484. static void SimpleHFilter16(uint8_t* p, int stride, int thresh) {
  485. int i;
  486. const int thresh2 = 2 * thresh + 1;
  487. for (i = 0; i < 16; ++i) {
  488. if (needs_filter(p + i * stride, 1, thresh2)) {
  489. do_filter2(p + i * stride, 1);
  490. }
  491. }
  492. }
  493. static void SimpleVFilter16i(uint8_t* p, int stride, int thresh) {
  494. int k;
  495. for (k = 3; k > 0; --k) {
  496. p += 4 * stride;
  497. SimpleVFilter16(p, stride, thresh);
  498. }
  499. }
  500. static void SimpleHFilter16i(uint8_t* p, int stride, int thresh) {
  501. int k;
  502. for (k = 3; k > 0; --k) {
  503. p += 4;
  504. SimpleHFilter16(p, stride, thresh);
  505. }
  506. }
  507. //------------------------------------------------------------------------------
  508. // Complex In-loop filtering (Paragraph 15.3)
  509. static WEBP_INLINE void FilterLoop26(uint8_t* p,
  510. int hstride, int vstride, int size,
  511. int thresh, int ithresh, int hev_thresh) {
  512. const int thresh2 = 2 * thresh + 1;
  513. while (size-- > 0) {
  514. if (needs_filter2(p, hstride, thresh2, ithresh)) {
  515. if (hev(p, hstride, hev_thresh)) {
  516. do_filter2(p, hstride);
  517. } else {
  518. do_filter6(p, hstride);
  519. }
  520. }
  521. p += vstride;
  522. }
  523. }
  524. static WEBP_INLINE void FilterLoop24(uint8_t* p,
  525. int hstride, int vstride, int size,
  526. int thresh, int ithresh, int hev_thresh) {
  527. const int thresh2 = 2 * thresh + 1;
  528. while (size-- > 0) {
  529. if (needs_filter2(p, hstride, thresh2, ithresh)) {
  530. if (hev(p, hstride, hev_thresh)) {
  531. do_filter2(p, hstride);
  532. } else {
  533. do_filter4(p, hstride);
  534. }
  535. }
  536. p += vstride;
  537. }
  538. }
  539. // on macroblock edges
  540. static void VFilter16(uint8_t* p, int stride,
  541. int thresh, int ithresh, int hev_thresh) {
  542. FilterLoop26(p, stride, 1, 16, thresh, ithresh, hev_thresh);
  543. }
  544. static void HFilter16(uint8_t* p, int stride,
  545. int thresh, int ithresh, int hev_thresh) {
  546. FilterLoop26(p, 1, stride, 16, thresh, ithresh, hev_thresh);
  547. }
  548. // on three inner edges
  549. static void VFilter16i(uint8_t* p, int stride,
  550. int thresh, int ithresh, int hev_thresh) {
  551. int k;
  552. for (k = 3; k > 0; --k) {
  553. p += 4 * stride;
  554. FilterLoop24(p, stride, 1, 16, thresh, ithresh, hev_thresh);
  555. }
  556. }
  557. static void HFilter16i(uint8_t* p, int stride,
  558. int thresh, int ithresh, int hev_thresh) {
  559. int k;
  560. for (k = 3; k > 0; --k) {
  561. p += 4;
  562. FilterLoop24(p, 1, stride, 16, thresh, ithresh, hev_thresh);
  563. }
  564. }
  565. // 8-pixels wide variant, for chroma filtering
  566. static void VFilter8(uint8_t* u, uint8_t* v, int stride,
  567. int thresh, int ithresh, int hev_thresh) {
  568. FilterLoop26(u, stride, 1, 8, thresh, ithresh, hev_thresh);
  569. FilterLoop26(v, stride, 1, 8, thresh, ithresh, hev_thresh);
  570. }
  571. static void HFilter8(uint8_t* u, uint8_t* v, int stride,
  572. int thresh, int ithresh, int hev_thresh) {
  573. FilterLoop26(u, 1, stride, 8, thresh, ithresh, hev_thresh);
  574. FilterLoop26(v, 1, stride, 8, thresh, ithresh, hev_thresh);
  575. }
  576. static void VFilter8i(uint8_t* u, uint8_t* v, int stride,
  577. int thresh, int ithresh, int hev_thresh) {
  578. FilterLoop24(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
  579. FilterLoop24(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
  580. }
  581. static void HFilter8i(uint8_t* u, uint8_t* v, int stride,
  582. int thresh, int ithresh, int hev_thresh) {
  583. FilterLoop24(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
  584. FilterLoop24(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
  585. }
  586. //------------------------------------------------------------------------------
  587. VP8DecIdct2 VP8Transform;
  588. VP8DecIdct VP8TransformAC3;
  589. VP8DecIdct VP8TransformUV;
  590. VP8DecIdct VP8TransformDC;
  591. VP8DecIdct VP8TransformDCUV;
  592. VP8LumaFilterFunc VP8VFilter16;
  593. VP8LumaFilterFunc VP8HFilter16;
  594. VP8ChromaFilterFunc VP8VFilter8;
  595. VP8ChromaFilterFunc VP8HFilter8;
  596. VP8LumaFilterFunc VP8VFilter16i;
  597. VP8LumaFilterFunc VP8HFilter16i;
  598. VP8ChromaFilterFunc VP8VFilter8i;
  599. VP8ChromaFilterFunc VP8HFilter8i;
  600. VP8SimpleFilterFunc VP8SimpleVFilter16;
  601. VP8SimpleFilterFunc VP8SimpleHFilter16;
  602. VP8SimpleFilterFunc VP8SimpleVFilter16i;
  603. VP8SimpleFilterFunc VP8SimpleHFilter16i;
  604. extern void VP8DspInitSSE2(void);
  605. extern void VP8DspInitNEON(void);
  606. extern void VP8DspInitMIPS32(void);
  607. void VP8DspInit(void) {
  608. VP8InitClipTables();
  609. VP8TransformWHT = TransformWHT;
  610. VP8Transform = TransformTwo;
  611. VP8TransformUV = TransformUV;
  612. VP8TransformDC = TransformDC;
  613. VP8TransformDCUV = TransformDCUV;
  614. VP8TransformAC3 = TransformAC3;
  615. VP8VFilter16 = VFilter16;
  616. VP8HFilter16 = HFilter16;
  617. VP8VFilter8 = VFilter8;
  618. VP8HFilter8 = HFilter8;
  619. VP8VFilter16i = VFilter16i;
  620. VP8HFilter16i = HFilter16i;
  621. VP8VFilter8i = VFilter8i;
  622. VP8HFilter8i = HFilter8i;
  623. VP8SimpleVFilter16 = SimpleVFilter16;
  624. VP8SimpleHFilter16 = SimpleHFilter16;
  625. VP8SimpleVFilter16i = SimpleVFilter16i;
  626. VP8SimpleHFilter16i = SimpleHFilter16i;
  627. // If defined, use CPUInfo() to overwrite some pointers with faster versions.
  628. if (VP8GetCPUInfo != NULL) {
  629. #if defined(WEBP_USE_SSE2)
  630. if (VP8GetCPUInfo(kSSE2)) {
  631. VP8DspInitSSE2();
  632. }
  633. #elif defined(WEBP_USE_NEON)
  634. if (VP8GetCPUInfo(kNEON)) {
  635. VP8DspInitNEON();
  636. }
  637. #elif defined(WEBP_USE_MIPS32)
  638. if (VP8GetCPUInfo(kMIPS32)) {
  639. VP8DspInitMIPS32();
  640. }
  641. #endif
  642. }
  643. }