dec.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 <assert.h>
  14. #include "./dsp.h"
  15. #include "../dec/vp8i_dec.h"
  16. #include "../utils/utils.h"
  17. //------------------------------------------------------------------------------
  18. static WEBP_INLINE uint8_t clip_8b(int v) {
  19. return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
  20. }
  21. //------------------------------------------------------------------------------
  22. // Transforms (Paragraph 14.4)
  23. #define STORE(x, y, v) \
  24. dst[(x) + (y) * BPS] = clip_8b(dst[(x) + (y) * BPS] + ((v) >> 3))
  25. #define STORE2(y, dc, d, c) do { \
  26. const int DC = (dc); \
  27. STORE(0, y, DC + (d)); \
  28. STORE(1, y, DC + (c)); \
  29. STORE(2, y, DC - (c)); \
  30. STORE(3, y, DC - (d)); \
  31. } while (0)
  32. #define MUL1(a) ((((a) * 20091) >> 16) + (a))
  33. #define MUL2(a) (((a) * 35468) >> 16)
  34. #if !WEBP_NEON_OMIT_C_CODE
  35. static void TransformOne_C(const int16_t* in, uint8_t* dst) {
  36. int C[4 * 4], *tmp;
  37. int i;
  38. tmp = C;
  39. for (i = 0; i < 4; ++i) { // vertical pass
  40. const int a = in[0] + in[8]; // [-4096, 4094]
  41. const int b = in[0] - in[8]; // [-4095, 4095]
  42. const int c = MUL2(in[4]) - MUL1(in[12]); // [-3783, 3783]
  43. const int d = MUL1(in[4]) + MUL2(in[12]); // [-3785, 3781]
  44. tmp[0] = a + d; // [-7881, 7875]
  45. tmp[1] = b + c; // [-7878, 7878]
  46. tmp[2] = b - c; // [-7878, 7878]
  47. tmp[3] = a - d; // [-7877, 7879]
  48. tmp += 4;
  49. in++;
  50. }
  51. // Each pass is expanding the dynamic range by ~3.85 (upper bound).
  52. // The exact value is (2. + (20091 + 35468) / 65536).
  53. // After the second pass, maximum interval is [-3794, 3794], assuming
  54. // an input in [-2048, 2047] interval. We then need to add a dst value
  55. // in the [0, 255] range.
  56. // In the worst case scenario, the input to clip_8b() can be as large as
  57. // [-60713, 60968].
  58. tmp = C;
  59. for (i = 0; i < 4; ++i) { // horizontal pass
  60. const int dc = tmp[0] + 4;
  61. const int a = dc + tmp[8];
  62. const int b = dc - tmp[8];
  63. const int c = MUL2(tmp[4]) - MUL1(tmp[12]);
  64. const int d = MUL1(tmp[4]) + MUL2(tmp[12]);
  65. STORE(0, 0, a + d);
  66. STORE(1, 0, b + c);
  67. STORE(2, 0, b - c);
  68. STORE(3, 0, a - d);
  69. tmp++;
  70. dst += BPS;
  71. }
  72. }
  73. // Simplified transform when only in[0], in[1] and in[4] are non-zero
  74. static void TransformAC3_C(const int16_t* in, uint8_t* dst) {
  75. const int a = in[0] + 4;
  76. const int c4 = MUL2(in[4]);
  77. const int d4 = MUL1(in[4]);
  78. const int c1 = MUL2(in[1]);
  79. const int d1 = MUL1(in[1]);
  80. STORE2(0, a + d4, d1, c1);
  81. STORE2(1, a + c4, d1, c1);
  82. STORE2(2, a - c4, d1, c1);
  83. STORE2(3, a - d4, d1, c1);
  84. }
  85. #undef MUL1
  86. #undef MUL2
  87. #undef STORE2
  88. static void TransformTwo_C(const int16_t* in, uint8_t* dst, int do_two) {
  89. TransformOne_C(in, dst);
  90. if (do_two) {
  91. TransformOne_C(in + 16, dst + 4);
  92. }
  93. }
  94. #endif // !WEBP_NEON_OMIT_C_CODE
  95. static void TransformUV_C(const int16_t* in, uint8_t* dst) {
  96. VP8Transform(in + 0 * 16, dst, 1);
  97. VP8Transform(in + 2 * 16, dst + 4 * BPS, 1);
  98. }
  99. #if !WEBP_NEON_OMIT_C_CODE
  100. static void TransformDC_C(const int16_t* in, uint8_t* dst) {
  101. const int DC = in[0] + 4;
  102. int i, j;
  103. for (j = 0; j < 4; ++j) {
  104. for (i = 0; i < 4; ++i) {
  105. STORE(i, j, DC);
  106. }
  107. }
  108. }
  109. #endif // !WEBP_NEON_OMIT_C_CODE
  110. static void TransformDCUV_C(const int16_t* in, uint8_t* dst) {
  111. if (in[0 * 16]) VP8TransformDC(in + 0 * 16, dst);
  112. if (in[1 * 16]) VP8TransformDC(in + 1 * 16, dst + 4);
  113. if (in[2 * 16]) VP8TransformDC(in + 2 * 16, dst + 4 * BPS);
  114. if (in[3 * 16]) VP8TransformDC(in + 3 * 16, dst + 4 * BPS + 4);
  115. }
  116. #undef STORE
  117. //------------------------------------------------------------------------------
  118. // Paragraph 14.3
  119. #if !WEBP_NEON_OMIT_C_CODE
  120. static void TransformWHT_C(const int16_t* in, int16_t* out) {
  121. int tmp[16];
  122. int i;
  123. for (i = 0; i < 4; ++i) {
  124. const int a0 = in[0 + i] + in[12 + i];
  125. const int a1 = in[4 + i] + in[ 8 + i];
  126. const int a2 = in[4 + i] - in[ 8 + i];
  127. const int a3 = in[0 + i] - in[12 + i];
  128. tmp[0 + i] = a0 + a1;
  129. tmp[8 + i] = a0 - a1;
  130. tmp[4 + i] = a3 + a2;
  131. tmp[12 + i] = a3 - a2;
  132. }
  133. for (i = 0; i < 4; ++i) {
  134. const int dc = tmp[0 + i * 4] + 3; // w/ rounder
  135. const int a0 = dc + tmp[3 + i * 4];
  136. const int a1 = tmp[1 + i * 4] + tmp[2 + i * 4];
  137. const int a2 = tmp[1 + i * 4] - tmp[2 + i * 4];
  138. const int a3 = dc - tmp[3 + i * 4];
  139. out[ 0] = (a0 + a1) >> 3;
  140. out[16] = (a3 + a2) >> 3;
  141. out[32] = (a0 - a1) >> 3;
  142. out[48] = (a3 - a2) >> 3;
  143. out += 64;
  144. }
  145. }
  146. #endif // !WEBP_NEON_OMIT_C_CODE
  147. void (*VP8TransformWHT)(const int16_t* in, int16_t* out);
  148. //------------------------------------------------------------------------------
  149. // Intra predictions
  150. #define DST(x, y) dst[(x) + (y) * BPS]
  151. #if !WEBP_NEON_OMIT_C_CODE
  152. static WEBP_INLINE void TrueMotion(uint8_t* dst, int size) {
  153. const uint8_t* top = dst - BPS;
  154. const uint8_t* const clip0 = VP8kclip1 - top[-1];
  155. int y;
  156. for (y = 0; y < size; ++y) {
  157. const uint8_t* const clip = clip0 + dst[-1];
  158. int x;
  159. for (x = 0; x < size; ++x) {
  160. dst[x] = clip[top[x]];
  161. }
  162. dst += BPS;
  163. }
  164. }
  165. static void TM4_C(uint8_t* dst) { TrueMotion(dst, 4); }
  166. static void TM8uv_C(uint8_t* dst) { TrueMotion(dst, 8); }
  167. static void TM16_C(uint8_t* dst) { TrueMotion(dst, 16); }
  168. //------------------------------------------------------------------------------
  169. // 16x16
  170. static void VE16_C(uint8_t* dst) { // vertical
  171. int j;
  172. for (j = 0; j < 16; ++j) {
  173. memcpy(dst + j * BPS, dst - BPS, 16);
  174. }
  175. }
  176. static void HE16_C(uint8_t* dst) { // horizontal
  177. int j;
  178. for (j = 16; j > 0; --j) {
  179. memset(dst, dst[-1], 16);
  180. dst += BPS;
  181. }
  182. }
  183. static WEBP_INLINE void Put16(int v, uint8_t* dst) {
  184. int j;
  185. for (j = 0; j < 16; ++j) {
  186. memset(dst + j * BPS, v, 16);
  187. }
  188. }
  189. static void DC16_C(uint8_t* dst) { // DC
  190. int DC = 16;
  191. int j;
  192. for (j = 0; j < 16; ++j) {
  193. DC += dst[-1 + j * BPS] + dst[j - BPS];
  194. }
  195. Put16(DC >> 5, dst);
  196. }
  197. static void DC16NoTop_C(uint8_t* dst) { // DC with top samples not available
  198. int DC = 8;
  199. int j;
  200. for (j = 0; j < 16; ++j) {
  201. DC += dst[-1 + j * BPS];
  202. }
  203. Put16(DC >> 4, dst);
  204. }
  205. static void DC16NoLeft_C(uint8_t* dst) { // DC with left samples not available
  206. int DC = 8;
  207. int i;
  208. for (i = 0; i < 16; ++i) {
  209. DC += dst[i - BPS];
  210. }
  211. Put16(DC >> 4, dst);
  212. }
  213. static void DC16NoTopLeft_C(uint8_t* dst) { // DC with no top and left samples
  214. Put16(0x80, dst);
  215. }
  216. #endif // !WEBP_NEON_OMIT_C_CODE
  217. VP8PredFunc VP8PredLuma16[NUM_B_DC_MODES];
  218. //------------------------------------------------------------------------------
  219. // 4x4
  220. #define AVG3(a, b, c) ((uint8_t)(((a) + 2 * (b) + (c) + 2) >> 2))
  221. #define AVG2(a, b) (((a) + (b) + 1) >> 1)
  222. #if !WEBP_NEON_OMIT_C_CODE
  223. static void VE4_C(uint8_t* dst) { // vertical
  224. const uint8_t* top = dst - BPS;
  225. const uint8_t vals[4] = {
  226. AVG3(top[-1], top[0], top[1]),
  227. AVG3(top[ 0], top[1], top[2]),
  228. AVG3(top[ 1], top[2], top[3]),
  229. AVG3(top[ 2], top[3], top[4])
  230. };
  231. int i;
  232. for (i = 0; i < 4; ++i) {
  233. memcpy(dst + i * BPS, vals, sizeof(vals));
  234. }
  235. }
  236. #endif // !WEBP_NEON_OMIT_C_CODE
  237. static void HE4_C(uint8_t* dst) { // horizontal
  238. const int A = dst[-1 - BPS];
  239. const int B = dst[-1];
  240. const int C = dst[-1 + BPS];
  241. const int D = dst[-1 + 2 * BPS];
  242. const int E = dst[-1 + 3 * BPS];
  243. WebPUint32ToMem(dst + 0 * BPS, 0x01010101U * AVG3(A, B, C));
  244. WebPUint32ToMem(dst + 1 * BPS, 0x01010101U * AVG3(B, C, D));
  245. WebPUint32ToMem(dst + 2 * BPS, 0x01010101U * AVG3(C, D, E));
  246. WebPUint32ToMem(dst + 3 * BPS, 0x01010101U * AVG3(D, E, E));
  247. }
  248. #if !WEBP_NEON_OMIT_C_CODE
  249. static void DC4_C(uint8_t* dst) { // DC
  250. uint32_t dc = 4;
  251. int i;
  252. for (i = 0; i < 4; ++i) dc += dst[i - BPS] + dst[-1 + i * BPS];
  253. dc >>= 3;
  254. for (i = 0; i < 4; ++i) memset(dst + i * BPS, dc, 4);
  255. }
  256. static void RD4_C(uint8_t* dst) { // Down-right
  257. const int I = dst[-1 + 0 * BPS];
  258. const int J = dst[-1 + 1 * BPS];
  259. const int K = dst[-1 + 2 * BPS];
  260. const int L = dst[-1 + 3 * BPS];
  261. const int X = dst[-1 - BPS];
  262. const int A = dst[0 - BPS];
  263. const int B = dst[1 - BPS];
  264. const int C = dst[2 - BPS];
  265. const int D = dst[3 - BPS];
  266. DST(0, 3) = AVG3(J, K, L);
  267. DST(1, 3) = DST(0, 2) = AVG3(I, J, K);
  268. DST(2, 3) = DST(1, 2) = DST(0, 1) = AVG3(X, I, J);
  269. DST(3, 3) = DST(2, 2) = DST(1, 1) = DST(0, 0) = AVG3(A, X, I);
  270. DST(3, 2) = DST(2, 1) = DST(1, 0) = AVG3(B, A, X);
  271. DST(3, 1) = DST(2, 0) = AVG3(C, B, A);
  272. DST(3, 0) = AVG3(D, C, B);
  273. }
  274. static void LD4_C(uint8_t* dst) { // Down-Left
  275. const int A = dst[0 - BPS];
  276. const int B = dst[1 - BPS];
  277. const int C = dst[2 - BPS];
  278. const int D = dst[3 - BPS];
  279. const int E = dst[4 - BPS];
  280. const int F = dst[5 - BPS];
  281. const int G = dst[6 - BPS];
  282. const int H = dst[7 - BPS];
  283. DST(0, 0) = AVG3(A, B, C);
  284. DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
  285. DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
  286. DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
  287. DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
  288. DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
  289. DST(3, 3) = AVG3(G, H, H);
  290. }
  291. #endif // !WEBP_NEON_OMIT_C_CODE
  292. static void VR4_C(uint8_t* dst) { // Vertical-Right
  293. const int I = dst[-1 + 0 * BPS];
  294. const int J = dst[-1 + 1 * BPS];
  295. const int K = dst[-1 + 2 * BPS];
  296. const int X = dst[-1 - BPS];
  297. const int A = dst[0 - BPS];
  298. const int B = dst[1 - BPS];
  299. const int C = dst[2 - BPS];
  300. const int D = dst[3 - BPS];
  301. DST(0, 0) = DST(1, 2) = AVG2(X, A);
  302. DST(1, 0) = DST(2, 2) = AVG2(A, B);
  303. DST(2, 0) = DST(3, 2) = AVG2(B, C);
  304. DST(3, 0) = AVG2(C, D);
  305. DST(0, 3) = AVG3(K, J, I);
  306. DST(0, 2) = AVG3(J, I, X);
  307. DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
  308. DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
  309. DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
  310. DST(3, 1) = AVG3(B, C, D);
  311. }
  312. static void VL4_C(uint8_t* dst) { // Vertical-Left
  313. const int A = dst[0 - BPS];
  314. const int B = dst[1 - BPS];
  315. const int C = dst[2 - BPS];
  316. const int D = dst[3 - BPS];
  317. const int E = dst[4 - BPS];
  318. const int F = dst[5 - BPS];
  319. const int G = dst[6 - BPS];
  320. const int H = dst[7 - BPS];
  321. DST(0, 0) = AVG2(A, B);
  322. DST(1, 0) = DST(0, 2) = AVG2(B, C);
  323. DST(2, 0) = DST(1, 2) = AVG2(C, D);
  324. DST(3, 0) = DST(2, 2) = AVG2(D, E);
  325. DST(0, 1) = AVG3(A, B, C);
  326. DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
  327. DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
  328. DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
  329. DST(3, 2) = AVG3(E, F, G);
  330. DST(3, 3) = AVG3(F, G, H);
  331. }
  332. static void HU4_C(uint8_t* dst) { // Horizontal-Up
  333. const int I = dst[-1 + 0 * BPS];
  334. const int J = dst[-1 + 1 * BPS];
  335. const int K = dst[-1 + 2 * BPS];
  336. const int L = dst[-1 + 3 * BPS];
  337. DST(0, 0) = AVG2(I, J);
  338. DST(2, 0) = DST(0, 1) = AVG2(J, K);
  339. DST(2, 1) = DST(0, 2) = AVG2(K, L);
  340. DST(1, 0) = AVG3(I, J, K);
  341. DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
  342. DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
  343. DST(3, 2) = DST(2, 2) =
  344. DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
  345. }
  346. static void HD4_C(uint8_t* dst) { // Horizontal-Down
  347. const int I = dst[-1 + 0 * BPS];
  348. const int J = dst[-1 + 1 * BPS];
  349. const int K = dst[-1 + 2 * BPS];
  350. const int L = dst[-1 + 3 * BPS];
  351. const int X = dst[-1 - BPS];
  352. const int A = dst[0 - BPS];
  353. const int B = dst[1 - BPS];
  354. const int C = dst[2 - BPS];
  355. DST(0, 0) = DST(2, 1) = AVG2(I, X);
  356. DST(0, 1) = DST(2, 2) = AVG2(J, I);
  357. DST(0, 2) = DST(2, 3) = AVG2(K, J);
  358. DST(0, 3) = AVG2(L, K);
  359. DST(3, 0) = AVG3(A, B, C);
  360. DST(2, 0) = AVG3(X, A, B);
  361. DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
  362. DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
  363. DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
  364. DST(1, 3) = AVG3(L, K, J);
  365. }
  366. #undef DST
  367. #undef AVG3
  368. #undef AVG2
  369. VP8PredFunc VP8PredLuma4[NUM_BMODES];
  370. //------------------------------------------------------------------------------
  371. // Chroma
  372. #if !WEBP_NEON_OMIT_C_CODE
  373. static void VE8uv_C(uint8_t* dst) { // vertical
  374. int j;
  375. for (j = 0; j < 8; ++j) {
  376. memcpy(dst + j * BPS, dst - BPS, 8);
  377. }
  378. }
  379. static void HE8uv_C(uint8_t* dst) { // horizontal
  380. int j;
  381. for (j = 0; j < 8; ++j) {
  382. memset(dst, dst[-1], 8);
  383. dst += BPS;
  384. }
  385. }
  386. // helper for chroma-DC predictions
  387. static WEBP_INLINE void Put8x8uv(uint8_t value, uint8_t* dst) {
  388. int j;
  389. for (j = 0; j < 8; ++j) {
  390. memset(dst + j * BPS, value, 8);
  391. }
  392. }
  393. static void DC8uv_C(uint8_t* dst) { // DC
  394. int dc0 = 8;
  395. int i;
  396. for (i = 0; i < 8; ++i) {
  397. dc0 += dst[i - BPS] + dst[-1 + i * BPS];
  398. }
  399. Put8x8uv(dc0 >> 4, dst);
  400. }
  401. static void DC8uvNoLeft_C(uint8_t* dst) { // DC with no left samples
  402. int dc0 = 4;
  403. int i;
  404. for (i = 0; i < 8; ++i) {
  405. dc0 += dst[i - BPS];
  406. }
  407. Put8x8uv(dc0 >> 3, dst);
  408. }
  409. static void DC8uvNoTop_C(uint8_t* dst) { // DC with no top samples
  410. int dc0 = 4;
  411. int i;
  412. for (i = 0; i < 8; ++i) {
  413. dc0 += dst[-1 + i * BPS];
  414. }
  415. Put8x8uv(dc0 >> 3, dst);
  416. }
  417. static void DC8uvNoTopLeft_C(uint8_t* dst) { // DC with nothing
  418. Put8x8uv(0x80, dst);
  419. }
  420. #endif // !WEBP_NEON_OMIT_C_CODE
  421. VP8PredFunc VP8PredChroma8[NUM_B_DC_MODES];
  422. //------------------------------------------------------------------------------
  423. // Edge filtering functions
  424. #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  425. // 4 pixels in, 2 pixels out
  426. static WEBP_INLINE void DoFilter2_C(uint8_t* p, int step) {
  427. const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
  428. const int a = 3 * (q0 - p0) + VP8ksclip1[p1 - q1]; // in [-893,892]
  429. const int a1 = VP8ksclip2[(a + 4) >> 3]; // in [-16,15]
  430. const int a2 = VP8ksclip2[(a + 3) >> 3];
  431. p[-step] = VP8kclip1[p0 + a2];
  432. p[ 0] = VP8kclip1[q0 - a1];
  433. }
  434. // 4 pixels in, 4 pixels out
  435. static WEBP_INLINE void DoFilter4_C(uint8_t* p, int step) {
  436. const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
  437. const int a = 3 * (q0 - p0);
  438. const int a1 = VP8ksclip2[(a + 4) >> 3];
  439. const int a2 = VP8ksclip2[(a + 3) >> 3];
  440. const int a3 = (a1 + 1) >> 1;
  441. p[-2*step] = VP8kclip1[p1 + a3];
  442. p[- step] = VP8kclip1[p0 + a2];
  443. p[ 0] = VP8kclip1[q0 - a1];
  444. p[ step] = VP8kclip1[q1 - a3];
  445. }
  446. // 6 pixels in, 6 pixels out
  447. static WEBP_INLINE void DoFilter6_C(uint8_t* p, int step) {
  448. const int p2 = p[-3*step], p1 = p[-2*step], p0 = p[-step];
  449. const int q0 = p[0], q1 = p[step], q2 = p[2*step];
  450. const int a = VP8ksclip1[3 * (q0 - p0) + VP8ksclip1[p1 - q1]];
  451. // a is in [-128,127], a1 in [-27,27], a2 in [-18,18] and a3 in [-9,9]
  452. const int a1 = (27 * a + 63) >> 7; // eq. to ((3 * a + 7) * 9) >> 7
  453. const int a2 = (18 * a + 63) >> 7; // eq. to ((2 * a + 7) * 9) >> 7
  454. const int a3 = (9 * a + 63) >> 7; // eq. to ((1 * a + 7) * 9) >> 7
  455. p[-3*step] = VP8kclip1[p2 + a3];
  456. p[-2*step] = VP8kclip1[p1 + a2];
  457. p[- step] = VP8kclip1[p0 + a1];
  458. p[ 0] = VP8kclip1[q0 - a1];
  459. p[ step] = VP8kclip1[q1 - a2];
  460. p[ 2*step] = VP8kclip1[q2 - a3];
  461. }
  462. static WEBP_INLINE int Hev(const uint8_t* p, int step, int thresh) {
  463. const int p1 = p[-2*step], p0 = p[-step], q0 = p[0], q1 = p[step];
  464. return (VP8kabs0[p1 - p0] > thresh) || (VP8kabs0[q1 - q0] > thresh);
  465. }
  466. #endif // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  467. #if !WEBP_NEON_OMIT_C_CODE
  468. static WEBP_INLINE int NeedsFilter_C(const uint8_t* p, int step, int t) {
  469. const int p1 = p[-2 * step], p0 = p[-step], q0 = p[0], q1 = p[step];
  470. return ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) <= t);
  471. }
  472. #endif // !WEBP_NEON_OMIT_C_CODE
  473. #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  474. static WEBP_INLINE int NeedsFilter2_C(const uint8_t* p,
  475. int step, int t, int it) {
  476. const int p3 = p[-4 * step], p2 = p[-3 * step], p1 = p[-2 * step];
  477. const int p0 = p[-step], q0 = p[0];
  478. const int q1 = p[step], q2 = p[2 * step], q3 = p[3 * step];
  479. if ((4 * VP8kabs0[p0 - q0] + VP8kabs0[p1 - q1]) > t) return 0;
  480. return VP8kabs0[p3 - p2] <= it && VP8kabs0[p2 - p1] <= it &&
  481. VP8kabs0[p1 - p0] <= it && VP8kabs0[q3 - q2] <= it &&
  482. VP8kabs0[q2 - q1] <= it && VP8kabs0[q1 - q0] <= it;
  483. }
  484. #endif // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  485. //------------------------------------------------------------------------------
  486. // Simple In-loop filtering (Paragraph 15.2)
  487. #if !WEBP_NEON_OMIT_C_CODE
  488. static void SimpleVFilter16_C(uint8_t* p, int stride, int thresh) {
  489. int i;
  490. const int thresh2 = 2 * thresh + 1;
  491. for (i = 0; i < 16; ++i) {
  492. if (NeedsFilter_C(p + i, stride, thresh2)) {
  493. DoFilter2_C(p + i, stride);
  494. }
  495. }
  496. }
  497. static void SimpleHFilter16_C(uint8_t* p, int stride, int thresh) {
  498. int i;
  499. const int thresh2 = 2 * thresh + 1;
  500. for (i = 0; i < 16; ++i) {
  501. if (NeedsFilter_C(p + i * stride, 1, thresh2)) {
  502. DoFilter2_C(p + i * stride, 1);
  503. }
  504. }
  505. }
  506. static void SimpleVFilter16i_C(uint8_t* p, int stride, int thresh) {
  507. int k;
  508. for (k = 3; k > 0; --k) {
  509. p += 4 * stride;
  510. SimpleVFilter16_C(p, stride, thresh);
  511. }
  512. }
  513. static void SimpleHFilter16i_C(uint8_t* p, int stride, int thresh) {
  514. int k;
  515. for (k = 3; k > 0; --k) {
  516. p += 4;
  517. SimpleHFilter16_C(p, stride, thresh);
  518. }
  519. }
  520. #endif // !WEBP_NEON_OMIT_C_CODE
  521. //------------------------------------------------------------------------------
  522. // Complex In-loop filtering (Paragraph 15.3)
  523. #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  524. static WEBP_INLINE void FilterLoop26_C(uint8_t* p,
  525. int hstride, int vstride, int size,
  526. int thresh, int ithresh,
  527. int hev_thresh) {
  528. const int thresh2 = 2 * thresh + 1;
  529. while (size-- > 0) {
  530. if (NeedsFilter2_C(p, hstride, thresh2, ithresh)) {
  531. if (Hev(p, hstride, hev_thresh)) {
  532. DoFilter2_C(p, hstride);
  533. } else {
  534. DoFilter6_C(p, hstride);
  535. }
  536. }
  537. p += vstride;
  538. }
  539. }
  540. static WEBP_INLINE void FilterLoop24_C(uint8_t* p,
  541. int hstride, int vstride, int size,
  542. int thresh, int ithresh,
  543. int hev_thresh) {
  544. const int thresh2 = 2 * thresh + 1;
  545. while (size-- > 0) {
  546. if (NeedsFilter2_C(p, hstride, thresh2, ithresh)) {
  547. if (Hev(p, hstride, hev_thresh)) {
  548. DoFilter2_C(p, hstride);
  549. } else {
  550. DoFilter4_C(p, hstride);
  551. }
  552. }
  553. p += vstride;
  554. }
  555. }
  556. #endif // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  557. #if !WEBP_NEON_OMIT_C_CODE
  558. // on macroblock edges
  559. static void VFilter16_C(uint8_t* p, int stride,
  560. int thresh, int ithresh, int hev_thresh) {
  561. FilterLoop26_C(p, stride, 1, 16, thresh, ithresh, hev_thresh);
  562. }
  563. static void HFilter16_C(uint8_t* p, int stride,
  564. int thresh, int ithresh, int hev_thresh) {
  565. FilterLoop26_C(p, 1, stride, 16, thresh, ithresh, hev_thresh);
  566. }
  567. // on three inner edges
  568. static void VFilter16i_C(uint8_t* p, int stride,
  569. int thresh, int ithresh, int hev_thresh) {
  570. int k;
  571. for (k = 3; k > 0; --k) {
  572. p += 4 * stride;
  573. FilterLoop24_C(p, stride, 1, 16, thresh, ithresh, hev_thresh);
  574. }
  575. }
  576. #endif // !WEBP_NEON_OMIT_C_CODE
  577. #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  578. static void HFilter16i_C(uint8_t* p, int stride,
  579. int thresh, int ithresh, int hev_thresh) {
  580. int k;
  581. for (k = 3; k > 0; --k) {
  582. p += 4;
  583. FilterLoop24_C(p, 1, stride, 16, thresh, ithresh, hev_thresh);
  584. }
  585. }
  586. #endif // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  587. #if !WEBP_NEON_OMIT_C_CODE
  588. // 8-pixels wide variant, for chroma filtering
  589. static void VFilter8_C(uint8_t* u, uint8_t* v, int stride,
  590. int thresh, int ithresh, int hev_thresh) {
  591. FilterLoop26_C(u, stride, 1, 8, thresh, ithresh, hev_thresh);
  592. FilterLoop26_C(v, stride, 1, 8, thresh, ithresh, hev_thresh);
  593. }
  594. #endif // !WEBP_NEON_OMIT_C_CODE
  595. #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  596. static void HFilter8_C(uint8_t* u, uint8_t* v, int stride,
  597. int thresh, int ithresh, int hev_thresh) {
  598. FilterLoop26_C(u, 1, stride, 8, thresh, ithresh, hev_thresh);
  599. FilterLoop26_C(v, 1, stride, 8, thresh, ithresh, hev_thresh);
  600. }
  601. #endif // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  602. #if !WEBP_NEON_OMIT_C_CODE
  603. static void VFilter8i_C(uint8_t* u, uint8_t* v, int stride,
  604. int thresh, int ithresh, int hev_thresh) {
  605. FilterLoop24_C(u + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
  606. FilterLoop24_C(v + 4 * stride, stride, 1, 8, thresh, ithresh, hev_thresh);
  607. }
  608. #endif // !WEBP_NEON_OMIT_C_CODE
  609. #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  610. static void HFilter8i_C(uint8_t* u, uint8_t* v, int stride,
  611. int thresh, int ithresh, int hev_thresh) {
  612. FilterLoop24_C(u + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
  613. FilterLoop24_C(v + 4, 1, stride, 8, thresh, ithresh, hev_thresh);
  614. }
  615. #endif // !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  616. //------------------------------------------------------------------------------
  617. static void DitherCombine8x8_C(const uint8_t* dither, uint8_t* dst,
  618. int dst_stride) {
  619. int i, j;
  620. for (j = 0; j < 8; ++j) {
  621. for (i = 0; i < 8; ++i) {
  622. const int delta0 = dither[i] - VP8_DITHER_AMP_CENTER;
  623. const int delta1 =
  624. (delta0 + VP8_DITHER_DESCALE_ROUNDER) >> VP8_DITHER_DESCALE;
  625. dst[i] = clip_8b((int)dst[i] + delta1);
  626. }
  627. dst += dst_stride;
  628. dither += 8;
  629. }
  630. }
  631. //------------------------------------------------------------------------------
  632. VP8DecIdct2 VP8Transform;
  633. VP8DecIdct VP8TransformAC3;
  634. VP8DecIdct VP8TransformUV;
  635. VP8DecIdct VP8TransformDC;
  636. VP8DecIdct VP8TransformDCUV;
  637. VP8LumaFilterFunc VP8VFilter16;
  638. VP8LumaFilterFunc VP8HFilter16;
  639. VP8ChromaFilterFunc VP8VFilter8;
  640. VP8ChromaFilterFunc VP8HFilter8;
  641. VP8LumaFilterFunc VP8VFilter16i;
  642. VP8LumaFilterFunc VP8HFilter16i;
  643. VP8ChromaFilterFunc VP8VFilter8i;
  644. VP8ChromaFilterFunc VP8HFilter8i;
  645. VP8SimpleFilterFunc VP8SimpleVFilter16;
  646. VP8SimpleFilterFunc VP8SimpleHFilter16;
  647. VP8SimpleFilterFunc VP8SimpleVFilter16i;
  648. VP8SimpleFilterFunc VP8SimpleHFilter16i;
  649. void (*VP8DitherCombine8x8)(const uint8_t* dither, uint8_t* dst,
  650. int dst_stride);
  651. extern void VP8DspInitSSE2(void);
  652. extern void VP8DspInitSSE41(void);
  653. extern void VP8DspInitNEON(void);
  654. extern void VP8DspInitMIPS32(void);
  655. extern void VP8DspInitMIPSdspR2(void);
  656. extern void VP8DspInitMSA(void);
  657. WEBP_DSP_INIT_FUNC(VP8DspInit) {
  658. VP8InitClipTables();
  659. #if !WEBP_NEON_OMIT_C_CODE
  660. VP8TransformWHT = TransformWHT_C;
  661. VP8Transform = TransformTwo_C;
  662. VP8TransformDC = TransformDC_C;
  663. VP8TransformAC3 = TransformAC3_C;
  664. #endif
  665. VP8TransformUV = TransformUV_C;
  666. VP8TransformDCUV = TransformDCUV_C;
  667. #if !WEBP_NEON_OMIT_C_CODE
  668. VP8VFilter16 = VFilter16_C;
  669. VP8VFilter16i = VFilter16i_C;
  670. VP8HFilter16 = HFilter16_C;
  671. VP8VFilter8 = VFilter8_C;
  672. VP8VFilter8i = VFilter8i_C;
  673. VP8SimpleVFilter16 = SimpleVFilter16_C;
  674. VP8SimpleHFilter16 = SimpleHFilter16_C;
  675. VP8SimpleVFilter16i = SimpleVFilter16i_C;
  676. VP8SimpleHFilter16i = SimpleHFilter16i_C;
  677. #endif
  678. #if !WEBP_NEON_OMIT_C_CODE || WEBP_NEON_WORK_AROUND_GCC
  679. VP8HFilter16i = HFilter16i_C;
  680. VP8HFilter8 = HFilter8_C;
  681. VP8HFilter8i = HFilter8i_C;
  682. #endif
  683. #if !WEBP_NEON_OMIT_C_CODE
  684. VP8PredLuma4[0] = DC4_C;
  685. VP8PredLuma4[1] = TM4_C;
  686. VP8PredLuma4[2] = VE4_C;
  687. VP8PredLuma4[4] = RD4_C;
  688. VP8PredLuma4[6] = LD4_C;
  689. #endif
  690. VP8PredLuma4[3] = HE4_C;
  691. VP8PredLuma4[5] = VR4_C;
  692. VP8PredLuma4[7] = VL4_C;
  693. VP8PredLuma4[8] = HD4_C;
  694. VP8PredLuma4[9] = HU4_C;
  695. #if !WEBP_NEON_OMIT_C_CODE
  696. VP8PredLuma16[0] = DC16_C;
  697. VP8PredLuma16[1] = TM16_C;
  698. VP8PredLuma16[2] = VE16_C;
  699. VP8PredLuma16[3] = HE16_C;
  700. VP8PredLuma16[4] = DC16NoTop_C;
  701. VP8PredLuma16[5] = DC16NoLeft_C;
  702. VP8PredLuma16[6] = DC16NoTopLeft_C;
  703. VP8PredChroma8[0] = DC8uv_C;
  704. VP8PredChroma8[1] = TM8uv_C;
  705. VP8PredChroma8[2] = VE8uv_C;
  706. VP8PredChroma8[3] = HE8uv_C;
  707. VP8PredChroma8[4] = DC8uvNoTop_C;
  708. VP8PredChroma8[5] = DC8uvNoLeft_C;
  709. VP8PredChroma8[6] = DC8uvNoTopLeft_C;
  710. #endif
  711. VP8DitherCombine8x8 = DitherCombine8x8_C;
  712. // If defined, use CPUInfo() to overwrite some pointers with faster versions.
  713. if (VP8GetCPUInfo != NULL) {
  714. #if defined(WEBP_HAVE_SSE2)
  715. if (VP8GetCPUInfo(kSSE2)) {
  716. VP8DspInitSSE2();
  717. #if defined(WEBP_HAVE_SSE41)
  718. if (VP8GetCPUInfo(kSSE4_1)) {
  719. VP8DspInitSSE41();
  720. }
  721. #endif
  722. }
  723. #endif
  724. #if defined(WEBP_USE_MIPS32)
  725. if (VP8GetCPUInfo(kMIPS32)) {
  726. VP8DspInitMIPS32();
  727. }
  728. #endif
  729. #if defined(WEBP_USE_MIPS_DSP_R2)
  730. if (VP8GetCPUInfo(kMIPSdspR2)) {
  731. VP8DspInitMIPSdspR2();
  732. }
  733. #endif
  734. #if defined(WEBP_USE_MSA)
  735. if (VP8GetCPUInfo(kMSA)) {
  736. VP8DspInitMSA();
  737. }
  738. #endif
  739. }
  740. #if defined(WEBP_HAVE_NEON)
  741. if (WEBP_NEON_OMIT_C_CODE ||
  742. (VP8GetCPUInfo != NULL && VP8GetCPUInfo(kNEON))) {
  743. VP8DspInitNEON();
  744. }
  745. #endif
  746. assert(VP8TransformWHT != NULL);
  747. assert(VP8Transform != NULL);
  748. assert(VP8TransformDC != NULL);
  749. assert(VP8TransformAC3 != NULL);
  750. assert(VP8TransformUV != NULL);
  751. assert(VP8TransformDCUV != NULL);
  752. assert(VP8VFilter16 != NULL);
  753. assert(VP8HFilter16 != NULL);
  754. assert(VP8VFilter8 != NULL);
  755. assert(VP8HFilter8 != NULL);
  756. assert(VP8VFilter16i != NULL);
  757. assert(VP8HFilter16i != NULL);
  758. assert(VP8VFilter8i != NULL);
  759. assert(VP8HFilter8i != NULL);
  760. assert(VP8SimpleVFilter16 != NULL);
  761. assert(VP8SimpleHFilter16 != NULL);
  762. assert(VP8SimpleVFilter16i != NULL);
  763. assert(VP8SimpleHFilter16i != NULL);
  764. assert(VP8PredLuma4[0] != NULL);
  765. assert(VP8PredLuma4[1] != NULL);
  766. assert(VP8PredLuma4[2] != NULL);
  767. assert(VP8PredLuma4[3] != NULL);
  768. assert(VP8PredLuma4[4] != NULL);
  769. assert(VP8PredLuma4[5] != NULL);
  770. assert(VP8PredLuma4[6] != NULL);
  771. assert(VP8PredLuma4[7] != NULL);
  772. assert(VP8PredLuma4[8] != NULL);
  773. assert(VP8PredLuma4[9] != NULL);
  774. assert(VP8PredLuma16[0] != NULL);
  775. assert(VP8PredLuma16[1] != NULL);
  776. assert(VP8PredLuma16[2] != NULL);
  777. assert(VP8PredLuma16[3] != NULL);
  778. assert(VP8PredLuma16[4] != NULL);
  779. assert(VP8PredLuma16[5] != NULL);
  780. assert(VP8PredLuma16[6] != NULL);
  781. assert(VP8PredChroma8[0] != NULL);
  782. assert(VP8PredChroma8[1] != NULL);
  783. assert(VP8PredChroma8[2] != NULL);
  784. assert(VP8PredChroma8[3] != NULL);
  785. assert(VP8PredChroma8[4] != NULL);
  786. assert(VP8PredChroma8[5] != NULL);
  787. assert(VP8PredChroma8[6] != NULL);
  788. assert(VP8DitherCombine8x8 != NULL);
  789. }