enc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // Copyright 2011 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Speed-critical encoding functions.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <assert.h>
  14. #include <stdlib.h> // for abs()
  15. #include "./dsp.h"
  16. #include "../enc/vp8enci.h"
  17. static WEBP_INLINE uint8_t clip_8b(int v) {
  18. return (!(v & ~0xff)) ? v : (v < 0) ? 0 : 255;
  19. }
  20. static WEBP_INLINE int clip_max(int v, int max) {
  21. return (v > max) ? max : v;
  22. }
  23. //------------------------------------------------------------------------------
  24. // Compute susceptibility based on DCT-coeff histograms:
  25. // the higher, the "easier" the macroblock is to compress.
  26. const int VP8DspScan[16 + 4 + 4] = {
  27. // Luma
  28. 0 + 0 * BPS, 4 + 0 * BPS, 8 + 0 * BPS, 12 + 0 * BPS,
  29. 0 + 4 * BPS, 4 + 4 * BPS, 8 + 4 * BPS, 12 + 4 * BPS,
  30. 0 + 8 * BPS, 4 + 8 * BPS, 8 + 8 * BPS, 12 + 8 * BPS,
  31. 0 + 12 * BPS, 4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS,
  32. 0 + 0 * BPS, 4 + 0 * BPS, 0 + 4 * BPS, 4 + 4 * BPS, // U
  33. 8 + 0 * BPS, 12 + 0 * BPS, 8 + 4 * BPS, 12 + 4 * BPS // V
  34. };
  35. static void CollectHistogram(const uint8_t* ref, const uint8_t* pred,
  36. int start_block, int end_block,
  37. VP8Histogram* const histo) {
  38. int j;
  39. for (j = start_block; j < end_block; ++j) {
  40. int k;
  41. int16_t out[16];
  42. VP8FTransform(ref + VP8DspScan[j], pred + VP8DspScan[j], out);
  43. // Convert coefficients to bin.
  44. for (k = 0; k < 16; ++k) {
  45. const int v = abs(out[k]) >> 3; // TODO(skal): add rounding?
  46. const int clipped_value = clip_max(v, MAX_COEFF_THRESH);
  47. histo->distribution[clipped_value]++;
  48. }
  49. }
  50. }
  51. //------------------------------------------------------------------------------
  52. // run-time tables (~4k)
  53. static uint8_t clip1[255 + 510 + 1]; // clips [-255,510] to [0,255]
  54. // We declare this variable 'volatile' to prevent instruction reordering
  55. // and make sure it's set to true _last_ (so as to be thread-safe)
  56. static volatile int tables_ok = 0;
  57. static void InitTables(void) {
  58. if (!tables_ok) {
  59. int i;
  60. for (i = -255; i <= 255 + 255; ++i) {
  61. clip1[255 + i] = clip_8b(i);
  62. }
  63. tables_ok = 1;
  64. }
  65. }
  66. //------------------------------------------------------------------------------
  67. // Transforms (Paragraph 14.4)
  68. #define STORE(x, y, v) \
  69. dst[(x) + (y) * BPS] = clip_8b(ref[(x) + (y) * BPS] + ((v) >> 3))
  70. static const int kC1 = 20091 + (1 << 16);
  71. static const int kC2 = 35468;
  72. #define MUL(a, b) (((a) * (b)) >> 16)
  73. static WEBP_INLINE void ITransformOne(const uint8_t* ref, const int16_t* in,
  74. uint8_t* dst) {
  75. int C[4 * 4], *tmp;
  76. int i;
  77. tmp = C;
  78. for (i = 0; i < 4; ++i) { // vertical pass
  79. const int a = in[0] + in[8];
  80. const int b = in[0] - in[8];
  81. const int c = MUL(in[4], kC2) - MUL(in[12], kC1);
  82. const int d = MUL(in[4], kC1) + MUL(in[12], kC2);
  83. tmp[0] = a + d;
  84. tmp[1] = b + c;
  85. tmp[2] = b - c;
  86. tmp[3] = a - d;
  87. tmp += 4;
  88. in++;
  89. }
  90. tmp = C;
  91. for (i = 0; i < 4; ++i) { // horizontal pass
  92. const int dc = tmp[0] + 4;
  93. const int a = dc + tmp[8];
  94. const int b = dc - tmp[8];
  95. const int c = MUL(tmp[4], kC2) - MUL(tmp[12], kC1);
  96. const int d = MUL(tmp[4], kC1) + MUL(tmp[12], kC2);
  97. STORE(0, i, a + d);
  98. STORE(1, i, b + c);
  99. STORE(2, i, b - c);
  100. STORE(3, i, a - d);
  101. tmp++;
  102. }
  103. }
  104. static void ITransform(const uint8_t* ref, const int16_t* in, uint8_t* dst,
  105. int do_two) {
  106. ITransformOne(ref, in, dst);
  107. if (do_two) {
  108. ITransformOne(ref + 4, in + 16, dst + 4);
  109. }
  110. }
  111. static void FTransform(const uint8_t* src, const uint8_t* ref, int16_t* out) {
  112. int i;
  113. int tmp[16];
  114. for (i = 0; i < 4; ++i, src += BPS, ref += BPS) {
  115. const int d0 = src[0] - ref[0]; // 9bit dynamic range ([-255,255])
  116. const int d1 = src[1] - ref[1];
  117. const int d2 = src[2] - ref[2];
  118. const int d3 = src[3] - ref[3];
  119. const int a0 = (d0 + d3); // 10b [-510,510]
  120. const int a1 = (d1 + d2);
  121. const int a2 = (d1 - d2);
  122. const int a3 = (d0 - d3);
  123. tmp[0 + i * 4] = (a0 + a1) * 8; // 14b [-8160,8160]
  124. tmp[1 + i * 4] = (a2 * 2217 + a3 * 5352 + 1812) >> 9; // [-7536,7542]
  125. tmp[2 + i * 4] = (a0 - a1) * 8;
  126. tmp[3 + i * 4] = (a3 * 2217 - a2 * 5352 + 937) >> 9;
  127. }
  128. for (i = 0; i < 4; ++i) {
  129. const int a0 = (tmp[0 + i] + tmp[12 + i]); // 15b
  130. const int a1 = (tmp[4 + i] + tmp[ 8 + i]);
  131. const int a2 = (tmp[4 + i] - tmp[ 8 + i]);
  132. const int a3 = (tmp[0 + i] - tmp[12 + i]);
  133. out[0 + i] = (a0 + a1 + 7) >> 4; // 12b
  134. out[4 + i] = ((a2 * 2217 + a3 * 5352 + 12000) >> 16) + (a3 != 0);
  135. out[8 + i] = (a0 - a1 + 7) >> 4;
  136. out[12+ i] = ((a3 * 2217 - a2 * 5352 + 51000) >> 16);
  137. }
  138. }
  139. static void FTransformWHT(const int16_t* in, int16_t* out) {
  140. // input is 12b signed
  141. int32_t tmp[16];
  142. int i;
  143. for (i = 0; i < 4; ++i, in += 64) {
  144. const int a0 = (in[0 * 16] + in[2 * 16]); // 13b
  145. const int a1 = (in[1 * 16] + in[3 * 16]);
  146. const int a2 = (in[1 * 16] - in[3 * 16]);
  147. const int a3 = (in[0 * 16] - in[2 * 16]);
  148. tmp[0 + i * 4] = a0 + a1; // 14b
  149. tmp[1 + i * 4] = a3 + a2;
  150. tmp[2 + i * 4] = a3 - a2;
  151. tmp[3 + i * 4] = a0 - a1;
  152. }
  153. for (i = 0; i < 4; ++i) {
  154. const int a0 = (tmp[0 + i] + tmp[8 + i]); // 15b
  155. const int a1 = (tmp[4 + i] + tmp[12+ i]);
  156. const int a2 = (tmp[4 + i] - tmp[12+ i]);
  157. const int a3 = (tmp[0 + i] - tmp[8 + i]);
  158. const int b0 = a0 + a1; // 16b
  159. const int b1 = a3 + a2;
  160. const int b2 = a3 - a2;
  161. const int b3 = a0 - a1;
  162. out[ 0 + i] = b0 >> 1; // 15b
  163. out[ 4 + i] = b1 >> 1;
  164. out[ 8 + i] = b2 >> 1;
  165. out[12 + i] = b3 >> 1;
  166. }
  167. }
  168. #undef MUL
  169. #undef STORE
  170. //------------------------------------------------------------------------------
  171. // Intra predictions
  172. #define DST(x, y) dst[(x) + (y) * BPS]
  173. static WEBP_INLINE void Fill(uint8_t* dst, int value, int size) {
  174. int j;
  175. for (j = 0; j < size; ++j) {
  176. memset(dst + j * BPS, value, size);
  177. }
  178. }
  179. static WEBP_INLINE void VerticalPred(uint8_t* dst,
  180. const uint8_t* top, int size) {
  181. int j;
  182. if (top) {
  183. for (j = 0; j < size; ++j) memcpy(dst + j * BPS, top, size);
  184. } else {
  185. Fill(dst, 127, size);
  186. }
  187. }
  188. static WEBP_INLINE void HorizontalPred(uint8_t* dst,
  189. const uint8_t* left, int size) {
  190. if (left) {
  191. int j;
  192. for (j = 0; j < size; ++j) {
  193. memset(dst + j * BPS, left[j], size);
  194. }
  195. } else {
  196. Fill(dst, 129, size);
  197. }
  198. }
  199. static WEBP_INLINE void TrueMotion(uint8_t* dst, const uint8_t* left,
  200. const uint8_t* top, int size) {
  201. int y;
  202. if (left) {
  203. if (top) {
  204. const uint8_t* const clip = clip1 + 255 - left[-1];
  205. for (y = 0; y < size; ++y) {
  206. const uint8_t* const clip_table = clip + left[y];
  207. int x;
  208. for (x = 0; x < size; ++x) {
  209. dst[x] = clip_table[top[x]];
  210. }
  211. dst += BPS;
  212. }
  213. } else {
  214. HorizontalPred(dst, left, size);
  215. }
  216. } else {
  217. // true motion without left samples (hence: with default 129 value)
  218. // is equivalent to VE prediction where you just copy the top samples.
  219. // Note that if top samples are not available, the default value is
  220. // then 129, and not 127 as in the VerticalPred case.
  221. if (top) {
  222. VerticalPred(dst, top, size);
  223. } else {
  224. Fill(dst, 129, size);
  225. }
  226. }
  227. }
  228. static WEBP_INLINE void DCMode(uint8_t* dst, const uint8_t* left,
  229. const uint8_t* top,
  230. int size, int round, int shift) {
  231. int DC = 0;
  232. int j;
  233. if (top) {
  234. for (j = 0; j < size; ++j) DC += top[j];
  235. if (left) { // top and left present
  236. for (j = 0; j < size; ++j) DC += left[j];
  237. } else { // top, but no left
  238. DC += DC;
  239. }
  240. DC = (DC + round) >> shift;
  241. } else if (left) { // left but no top
  242. for (j = 0; j < size; ++j) DC += left[j];
  243. DC += DC;
  244. DC = (DC + round) >> shift;
  245. } else { // no top, no left, nothing.
  246. DC = 0x80;
  247. }
  248. Fill(dst, DC, size);
  249. }
  250. //------------------------------------------------------------------------------
  251. // Chroma 8x8 prediction (paragraph 12.2)
  252. static void IntraChromaPreds(uint8_t* dst, const uint8_t* left,
  253. const uint8_t* top) {
  254. // U block
  255. DCMode(C8DC8 + dst, left, top, 8, 8, 4);
  256. VerticalPred(C8VE8 + dst, top, 8);
  257. HorizontalPred(C8HE8 + dst, left, 8);
  258. TrueMotion(C8TM8 + dst, left, top, 8);
  259. // V block
  260. dst += 8;
  261. if (top) top += 8;
  262. if (left) left += 16;
  263. DCMode(C8DC8 + dst, left, top, 8, 8, 4);
  264. VerticalPred(C8VE8 + dst, top, 8);
  265. HorizontalPred(C8HE8 + dst, left, 8);
  266. TrueMotion(C8TM8 + dst, left, top, 8);
  267. }
  268. //------------------------------------------------------------------------------
  269. // luma 16x16 prediction (paragraph 12.3)
  270. static void Intra16Preds(uint8_t* dst,
  271. const uint8_t* left, const uint8_t* top) {
  272. DCMode(I16DC16 + dst, left, top, 16, 16, 5);
  273. VerticalPred(I16VE16 + dst, top, 16);
  274. HorizontalPred(I16HE16 + dst, left, 16);
  275. TrueMotion(I16TM16 + dst, left, top, 16);
  276. }
  277. //------------------------------------------------------------------------------
  278. // luma 4x4 prediction
  279. #define AVG3(a, b, c) (((a) + 2 * (b) + (c) + 2) >> 2)
  280. #define AVG2(a, b) (((a) + (b) + 1) >> 1)
  281. static void VE4(uint8_t* dst, const uint8_t* top) { // vertical
  282. const uint8_t vals[4] = {
  283. AVG3(top[-1], top[0], top[1]),
  284. AVG3(top[ 0], top[1], top[2]),
  285. AVG3(top[ 1], top[2], top[3]),
  286. AVG3(top[ 2], top[3], top[4])
  287. };
  288. int i;
  289. for (i = 0; i < 4; ++i) {
  290. memcpy(dst + i * BPS, vals, 4);
  291. }
  292. }
  293. static void HE4(uint8_t* dst, const uint8_t* top) { // horizontal
  294. const int X = top[-1];
  295. const int I = top[-2];
  296. const int J = top[-3];
  297. const int K = top[-4];
  298. const int L = top[-5];
  299. *(uint32_t*)(dst + 0 * BPS) = 0x01010101U * AVG3(X, I, J);
  300. *(uint32_t*)(dst + 1 * BPS) = 0x01010101U * AVG3(I, J, K);
  301. *(uint32_t*)(dst + 2 * BPS) = 0x01010101U * AVG3(J, K, L);
  302. *(uint32_t*)(dst + 3 * BPS) = 0x01010101U * AVG3(K, L, L);
  303. }
  304. static void DC4(uint8_t* dst, const uint8_t* top) {
  305. uint32_t dc = 4;
  306. int i;
  307. for (i = 0; i < 4; ++i) dc += top[i] + top[-5 + i];
  308. Fill(dst, dc >> 3, 4);
  309. }
  310. static void RD4(uint8_t* dst, const uint8_t* top) {
  311. const int X = top[-1];
  312. const int I = top[-2];
  313. const int J = top[-3];
  314. const int K = top[-4];
  315. const int L = top[-5];
  316. const int A = top[0];
  317. const int B = top[1];
  318. const int C = top[2];
  319. const int D = top[3];
  320. DST(0, 3) = AVG3(J, K, L);
  321. DST(0, 2) = DST(1, 3) = AVG3(I, J, K);
  322. DST(0, 1) = DST(1, 2) = DST(2, 3) = AVG3(X, I, J);
  323. DST(0, 0) = DST(1, 1) = DST(2, 2) = DST(3, 3) = AVG3(A, X, I);
  324. DST(1, 0) = DST(2, 1) = DST(3, 2) = AVG3(B, A, X);
  325. DST(2, 0) = DST(3, 1) = AVG3(C, B, A);
  326. DST(3, 0) = AVG3(D, C, B);
  327. }
  328. static void LD4(uint8_t* dst, const uint8_t* top) {
  329. const int A = top[0];
  330. const int B = top[1];
  331. const int C = top[2];
  332. const int D = top[3];
  333. const int E = top[4];
  334. const int F = top[5];
  335. const int G = top[6];
  336. const int H = top[7];
  337. DST(0, 0) = AVG3(A, B, C);
  338. DST(1, 0) = DST(0, 1) = AVG3(B, C, D);
  339. DST(2, 0) = DST(1, 1) = DST(0, 2) = AVG3(C, D, E);
  340. DST(3, 0) = DST(2, 1) = DST(1, 2) = DST(0, 3) = AVG3(D, E, F);
  341. DST(3, 1) = DST(2, 2) = DST(1, 3) = AVG3(E, F, G);
  342. DST(3, 2) = DST(2, 3) = AVG3(F, G, H);
  343. DST(3, 3) = AVG3(G, H, H);
  344. }
  345. static void VR4(uint8_t* dst, const uint8_t* top) {
  346. const int X = top[-1];
  347. const int I = top[-2];
  348. const int J = top[-3];
  349. const int K = top[-4];
  350. const int A = top[0];
  351. const int B = top[1];
  352. const int C = top[2];
  353. const int D = top[3];
  354. DST(0, 0) = DST(1, 2) = AVG2(X, A);
  355. DST(1, 0) = DST(2, 2) = AVG2(A, B);
  356. DST(2, 0) = DST(3, 2) = AVG2(B, C);
  357. DST(3, 0) = AVG2(C, D);
  358. DST(0, 3) = AVG3(K, J, I);
  359. DST(0, 2) = AVG3(J, I, X);
  360. DST(0, 1) = DST(1, 3) = AVG3(I, X, A);
  361. DST(1, 1) = DST(2, 3) = AVG3(X, A, B);
  362. DST(2, 1) = DST(3, 3) = AVG3(A, B, C);
  363. DST(3, 1) = AVG3(B, C, D);
  364. }
  365. static void VL4(uint8_t* dst, const uint8_t* top) {
  366. const int A = top[0];
  367. const int B = top[1];
  368. const int C = top[2];
  369. const int D = top[3];
  370. const int E = top[4];
  371. const int F = top[5];
  372. const int G = top[6];
  373. const int H = top[7];
  374. DST(0, 0) = AVG2(A, B);
  375. DST(1, 0) = DST(0, 2) = AVG2(B, C);
  376. DST(2, 0) = DST(1, 2) = AVG2(C, D);
  377. DST(3, 0) = DST(2, 2) = AVG2(D, E);
  378. DST(0, 1) = AVG3(A, B, C);
  379. DST(1, 1) = DST(0, 3) = AVG3(B, C, D);
  380. DST(2, 1) = DST(1, 3) = AVG3(C, D, E);
  381. DST(3, 1) = DST(2, 3) = AVG3(D, E, F);
  382. DST(3, 2) = AVG3(E, F, G);
  383. DST(3, 3) = AVG3(F, G, H);
  384. }
  385. static void HU4(uint8_t* dst, const uint8_t* top) {
  386. const int I = top[-2];
  387. const int J = top[-3];
  388. const int K = top[-4];
  389. const int L = top[-5];
  390. DST(0, 0) = AVG2(I, J);
  391. DST(2, 0) = DST(0, 1) = AVG2(J, K);
  392. DST(2, 1) = DST(0, 2) = AVG2(K, L);
  393. DST(1, 0) = AVG3(I, J, K);
  394. DST(3, 0) = DST(1, 1) = AVG3(J, K, L);
  395. DST(3, 1) = DST(1, 2) = AVG3(K, L, L);
  396. DST(3, 2) = DST(2, 2) =
  397. DST(0, 3) = DST(1, 3) = DST(2, 3) = DST(3, 3) = L;
  398. }
  399. static void HD4(uint8_t* dst, const uint8_t* top) {
  400. const int X = top[-1];
  401. const int I = top[-2];
  402. const int J = top[-3];
  403. const int K = top[-4];
  404. const int L = top[-5];
  405. const int A = top[0];
  406. const int B = top[1];
  407. const int C = top[2];
  408. DST(0, 0) = DST(2, 1) = AVG2(I, X);
  409. DST(0, 1) = DST(2, 2) = AVG2(J, I);
  410. DST(0, 2) = DST(2, 3) = AVG2(K, J);
  411. DST(0, 3) = AVG2(L, K);
  412. DST(3, 0) = AVG3(A, B, C);
  413. DST(2, 0) = AVG3(X, A, B);
  414. DST(1, 0) = DST(3, 1) = AVG3(I, X, A);
  415. DST(1, 1) = DST(3, 2) = AVG3(J, I, X);
  416. DST(1, 2) = DST(3, 3) = AVG3(K, J, I);
  417. DST(1, 3) = AVG3(L, K, J);
  418. }
  419. static void TM4(uint8_t* dst, const uint8_t* top) {
  420. int x, y;
  421. const uint8_t* const clip = clip1 + 255 - top[-1];
  422. for (y = 0; y < 4; ++y) {
  423. const uint8_t* const clip_table = clip + top[-2 - y];
  424. for (x = 0; x < 4; ++x) {
  425. dst[x] = clip_table[top[x]];
  426. }
  427. dst += BPS;
  428. }
  429. }
  430. #undef DST
  431. #undef AVG3
  432. #undef AVG2
  433. // Left samples are top[-5 .. -2], top_left is top[-1], top are
  434. // located at top[0..3], and top right is top[4..7]
  435. static void Intra4Preds(uint8_t* dst, const uint8_t* top) {
  436. DC4(I4DC4 + dst, top);
  437. TM4(I4TM4 + dst, top);
  438. VE4(I4VE4 + dst, top);
  439. HE4(I4HE4 + dst, top);
  440. RD4(I4RD4 + dst, top);
  441. VR4(I4VR4 + dst, top);
  442. LD4(I4LD4 + dst, top);
  443. VL4(I4VL4 + dst, top);
  444. HD4(I4HD4 + dst, top);
  445. HU4(I4HU4 + dst, top);
  446. }
  447. //------------------------------------------------------------------------------
  448. // Metric
  449. static WEBP_INLINE int GetSSE(const uint8_t* a, const uint8_t* b,
  450. int w, int h) {
  451. int count = 0;
  452. int y, x;
  453. for (y = 0; y < h; ++y) {
  454. for (x = 0; x < w; ++x) {
  455. const int diff = (int)a[x] - b[x];
  456. count += diff * diff;
  457. }
  458. a += BPS;
  459. b += BPS;
  460. }
  461. return count;
  462. }
  463. static int SSE16x16(const uint8_t* a, const uint8_t* b) {
  464. return GetSSE(a, b, 16, 16);
  465. }
  466. static int SSE16x8(const uint8_t* a, const uint8_t* b) {
  467. return GetSSE(a, b, 16, 8);
  468. }
  469. static int SSE8x8(const uint8_t* a, const uint8_t* b) {
  470. return GetSSE(a, b, 8, 8);
  471. }
  472. static int SSE4x4(const uint8_t* a, const uint8_t* b) {
  473. return GetSSE(a, b, 4, 4);
  474. }
  475. //------------------------------------------------------------------------------
  476. // Texture distortion
  477. //
  478. // We try to match the spectral content (weighted) between source and
  479. // reconstructed samples.
  480. // Hadamard transform
  481. // Returns the weighted sum of the absolute value of transformed coefficients.
  482. static int TTransform(const uint8_t* in, const uint16_t* w) {
  483. int sum = 0;
  484. int tmp[16];
  485. int i;
  486. // horizontal pass
  487. for (i = 0; i < 4; ++i, in += BPS) {
  488. const int a0 = in[0] + in[2];
  489. const int a1 = in[1] + in[3];
  490. const int a2 = in[1] - in[3];
  491. const int a3 = in[0] - in[2];
  492. tmp[0 + i * 4] = a0 + a1;
  493. tmp[1 + i * 4] = a3 + a2;
  494. tmp[2 + i * 4] = a3 - a2;
  495. tmp[3 + i * 4] = a0 - a1;
  496. }
  497. // vertical pass
  498. for (i = 0; i < 4; ++i, ++w) {
  499. const int a0 = tmp[0 + i] + tmp[8 + i];
  500. const int a1 = tmp[4 + i] + tmp[12+ i];
  501. const int a2 = tmp[4 + i] - tmp[12+ i];
  502. const int a3 = tmp[0 + i] - tmp[8 + i];
  503. const int b0 = a0 + a1;
  504. const int b1 = a3 + a2;
  505. const int b2 = a3 - a2;
  506. const int b3 = a0 - a1;
  507. sum += w[ 0] * abs(b0);
  508. sum += w[ 4] * abs(b1);
  509. sum += w[ 8] * abs(b2);
  510. sum += w[12] * abs(b3);
  511. }
  512. return sum;
  513. }
  514. static int Disto4x4(const uint8_t* const a, const uint8_t* const b,
  515. const uint16_t* const w) {
  516. const int sum1 = TTransform(a, w);
  517. const int sum2 = TTransform(b, w);
  518. return abs(sum2 - sum1) >> 5;
  519. }
  520. static int Disto16x16(const uint8_t* const a, const uint8_t* const b,
  521. const uint16_t* const w) {
  522. int D = 0;
  523. int x, y;
  524. for (y = 0; y < 16 * BPS; y += 4 * BPS) {
  525. for (x = 0; x < 16; x += 4) {
  526. D += Disto4x4(a + x + y, b + x + y, w);
  527. }
  528. }
  529. return D;
  530. }
  531. //------------------------------------------------------------------------------
  532. // Quantization
  533. //
  534. static const uint8_t kZigzag[16] = {
  535. 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15
  536. };
  537. // Simple quantization
  538. static int QuantizeBlock(int16_t in[16], int16_t out[16],
  539. const VP8Matrix* const mtx) {
  540. int last = -1;
  541. int n;
  542. for (n = 0; n < 16; ++n) {
  543. const int j = kZigzag[n];
  544. const int sign = (in[j] < 0);
  545. const uint32_t coeff = (sign ? -in[j] : in[j]) + mtx->sharpen_[j];
  546. if (coeff > mtx->zthresh_[j]) {
  547. const uint32_t Q = mtx->q_[j];
  548. const uint32_t iQ = mtx->iq_[j];
  549. const uint32_t B = mtx->bias_[j];
  550. int level = QUANTDIV(coeff, iQ, B);
  551. if (level > MAX_LEVEL) level = MAX_LEVEL;
  552. if (sign) level = -level;
  553. in[j] = level * Q;
  554. out[n] = level;
  555. if (level) last = n;
  556. } else {
  557. out[n] = 0;
  558. in[j] = 0;
  559. }
  560. }
  561. return (last >= 0);
  562. }
  563. static int QuantizeBlockWHT(int16_t in[16], int16_t out[16],
  564. const VP8Matrix* const mtx) {
  565. int n, last = -1;
  566. for (n = 0; n < 16; ++n) {
  567. const int j = kZigzag[n];
  568. const int sign = (in[j] < 0);
  569. const uint32_t coeff = sign ? -in[j] : in[j];
  570. assert(mtx->sharpen_[j] == 0);
  571. if (coeff > mtx->zthresh_[j]) {
  572. const uint32_t Q = mtx->q_[j];
  573. const uint32_t iQ = mtx->iq_[j];
  574. const uint32_t B = mtx->bias_[j];
  575. int level = QUANTDIV(coeff, iQ, B);
  576. if (level > MAX_LEVEL) level = MAX_LEVEL;
  577. if (sign) level = -level;
  578. in[j] = level * Q;
  579. out[n] = level;
  580. if (level) last = n;
  581. } else {
  582. out[n] = 0;
  583. in[j] = 0;
  584. }
  585. }
  586. return (last >= 0);
  587. }
  588. //------------------------------------------------------------------------------
  589. // Block copy
  590. static WEBP_INLINE void Copy(const uint8_t* src, uint8_t* dst, int size) {
  591. int y;
  592. for (y = 0; y < size; ++y) {
  593. memcpy(dst, src, size);
  594. src += BPS;
  595. dst += BPS;
  596. }
  597. }
  598. static void Copy4x4(const uint8_t* src, uint8_t* dst) { Copy(src, dst, 4); }
  599. //------------------------------------------------------------------------------
  600. // Initialization
  601. // Speed-critical function pointers. We have to initialize them to the default
  602. // implementations within VP8EncDspInit().
  603. VP8CHisto VP8CollectHistogram;
  604. VP8Idct VP8ITransform;
  605. VP8Fdct VP8FTransform;
  606. VP8WHT VP8FTransformWHT;
  607. VP8Intra4Preds VP8EncPredLuma4;
  608. VP8IntraPreds VP8EncPredLuma16;
  609. VP8IntraPreds VP8EncPredChroma8;
  610. VP8Metric VP8SSE16x16;
  611. VP8Metric VP8SSE8x8;
  612. VP8Metric VP8SSE16x8;
  613. VP8Metric VP8SSE4x4;
  614. VP8WMetric VP8TDisto4x4;
  615. VP8WMetric VP8TDisto16x16;
  616. VP8QuantizeBlock VP8EncQuantizeBlock;
  617. VP8QuantizeBlockWHT VP8EncQuantizeBlockWHT;
  618. VP8BlockCopy VP8Copy4x4;
  619. extern void VP8EncDspInitSSE2(void);
  620. extern void VP8EncDspInitAVX2(void);
  621. extern void VP8EncDspInitNEON(void);
  622. extern void VP8EncDspInitMIPS32(void);
  623. void VP8EncDspInit(void) {
  624. VP8DspInit(); // common inverse transforms
  625. InitTables();
  626. // default C implementations
  627. VP8CollectHistogram = CollectHistogram;
  628. VP8ITransform = ITransform;
  629. VP8FTransform = FTransform;
  630. VP8FTransformWHT = FTransformWHT;
  631. VP8EncPredLuma4 = Intra4Preds;
  632. VP8EncPredLuma16 = Intra16Preds;
  633. VP8EncPredChroma8 = IntraChromaPreds;
  634. VP8SSE16x16 = SSE16x16;
  635. VP8SSE8x8 = SSE8x8;
  636. VP8SSE16x8 = SSE16x8;
  637. VP8SSE4x4 = SSE4x4;
  638. VP8TDisto4x4 = Disto4x4;
  639. VP8TDisto16x16 = Disto16x16;
  640. VP8EncQuantizeBlock = QuantizeBlock;
  641. VP8EncQuantizeBlockWHT = QuantizeBlockWHT;
  642. VP8Copy4x4 = Copy4x4;
  643. // If defined, use CPUInfo() to overwrite some pointers with faster versions.
  644. if (VP8GetCPUInfo != NULL) {
  645. #if defined(WEBP_USE_SSE2)
  646. if (VP8GetCPUInfo(kSSE2)) {
  647. VP8EncDspInitSSE2();
  648. }
  649. #endif
  650. #if defined(WEBP_USE_AVX2)
  651. if (VP8GetCPUInfo(kAVX2)) {
  652. VP8EncDspInitAVX2();
  653. }
  654. #endif
  655. #if defined(WEBP_USE_NEON)
  656. if (VP8GetCPUInfo(kNEON)) {
  657. VP8EncDspInitNEON();
  658. }
  659. #endif
  660. #if defined(WEBP_USE_MIPS32)
  661. if (VP8GetCPUInfo(kMIPS32)) {
  662. VP8EncDspInitMIPS32();
  663. }
  664. #endif
  665. }
  666. }