dec.c 23 KB

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