predictor_enc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. // Copyright 2016 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. // Image transform methods for lossless encoder.
  11. //
  12. // Authors: Vikas Arora (vikaas.arora@gmail.com)
  13. // Jyrki Alakuijala (jyrki@google.com)
  14. // Urvang Joshi (urvang@google.com)
  15. // Vincent Rabaud (vrabaud@google.com)
  16. #include "../dsp/lossless.h"
  17. #include "../dsp/lossless_common.h"
  18. #include "./vp8li_enc.h"
  19. #define MAX_DIFF_COST (1e30f)
  20. static const float kSpatialPredictorBias = 15.f;
  21. static const int kPredLowEffort = 11;
  22. static const uint32_t kMaskAlpha = 0xff000000;
  23. // Mostly used to reduce code size + readability
  24. static WEBP_INLINE int GetMin(int a, int b) { return (a > b) ? b : a; }
  25. static WEBP_INLINE int GetMax(int a, int b) { return (a < b) ? b : a; }
  26. //------------------------------------------------------------------------------
  27. // Methods to calculate Entropy (Shannon).
  28. static float PredictionCostSpatial(const int counts[256], int weight_0,
  29. double exp_val) {
  30. const int significant_symbols = 256 >> 4;
  31. const double exp_decay_factor = 0.6;
  32. double bits = weight_0 * counts[0];
  33. int i;
  34. for (i = 1; i < significant_symbols; ++i) {
  35. bits += exp_val * (counts[i] + counts[256 - i]);
  36. exp_val *= exp_decay_factor;
  37. }
  38. return (float)(-0.1 * bits);
  39. }
  40. static float PredictionCostSpatialHistogram(const int accumulated[4][256],
  41. const int tile[4][256]) {
  42. int i;
  43. double retval = 0;
  44. for (i = 0; i < 4; ++i) {
  45. const double kExpValue = 0.94;
  46. retval += PredictionCostSpatial(tile[i], 1, kExpValue);
  47. retval += VP8LCombinedShannonEntropy(tile[i], accumulated[i]);
  48. }
  49. return (float)retval;
  50. }
  51. static WEBP_INLINE void UpdateHisto(int histo_argb[4][256], uint32_t argb) {
  52. ++histo_argb[0][argb >> 24];
  53. ++histo_argb[1][(argb >> 16) & 0xff];
  54. ++histo_argb[2][(argb >> 8) & 0xff];
  55. ++histo_argb[3][argb & 0xff];
  56. }
  57. //------------------------------------------------------------------------------
  58. // Spatial transform functions.
  59. static WEBP_INLINE void PredictBatch(int mode, int x_start, int y,
  60. int num_pixels, const uint32_t* current,
  61. const uint32_t* upper, uint32_t* out) {
  62. if (x_start == 0) {
  63. if (y == 0) {
  64. // ARGB_BLACK.
  65. VP8LPredictorsSub[0](current, NULL, 1, out);
  66. } else {
  67. // Top one.
  68. VP8LPredictorsSub[2](current, upper, 1, out);
  69. }
  70. ++x_start;
  71. ++out;
  72. --num_pixels;
  73. }
  74. if (y == 0) {
  75. // Left one.
  76. VP8LPredictorsSub[1](current + x_start, NULL, num_pixels, out);
  77. } else {
  78. VP8LPredictorsSub[mode](current + x_start, upper + x_start, num_pixels,
  79. out);
  80. }
  81. }
  82. static int MaxDiffBetweenPixels(uint32_t p1, uint32_t p2) {
  83. const int diff_a = abs((int)(p1 >> 24) - (int)(p2 >> 24));
  84. const int diff_r = abs((int)((p1 >> 16) & 0xff) - (int)((p2 >> 16) & 0xff));
  85. const int diff_g = abs((int)((p1 >> 8) & 0xff) - (int)((p2 >> 8) & 0xff));
  86. const int diff_b = abs((int)(p1 & 0xff) - (int)(p2 & 0xff));
  87. return GetMax(GetMax(diff_a, diff_r), GetMax(diff_g, diff_b));
  88. }
  89. static int MaxDiffAroundPixel(uint32_t current, uint32_t up, uint32_t down,
  90. uint32_t left, uint32_t right) {
  91. const int diff_up = MaxDiffBetweenPixels(current, up);
  92. const int diff_down = MaxDiffBetweenPixels(current, down);
  93. const int diff_left = MaxDiffBetweenPixels(current, left);
  94. const int diff_right = MaxDiffBetweenPixels(current, right);
  95. return GetMax(GetMax(diff_up, diff_down), GetMax(diff_left, diff_right));
  96. }
  97. static uint32_t AddGreenToBlueAndRed(uint32_t argb) {
  98. const uint32_t green = (argb >> 8) & 0xff;
  99. uint32_t red_blue = argb & 0x00ff00ffu;
  100. red_blue += (green << 16) | green;
  101. red_blue &= 0x00ff00ffu;
  102. return (argb & 0xff00ff00u) | red_blue;
  103. }
  104. static void MaxDiffsForRow(int width, int stride, const uint32_t* const argb,
  105. uint8_t* const max_diffs, int used_subtract_green) {
  106. uint32_t current, up, down, left, right;
  107. int x;
  108. if (width <= 2) return;
  109. current = argb[0];
  110. right = argb[1];
  111. if (used_subtract_green) {
  112. current = AddGreenToBlueAndRed(current);
  113. right = AddGreenToBlueAndRed(right);
  114. }
  115. // max_diffs[0] and max_diffs[width - 1] are never used.
  116. for (x = 1; x < width - 1; ++x) {
  117. up = argb[-stride + x];
  118. down = argb[stride + x];
  119. left = current;
  120. current = right;
  121. right = argb[x + 1];
  122. if (used_subtract_green) {
  123. up = AddGreenToBlueAndRed(up);
  124. down = AddGreenToBlueAndRed(down);
  125. right = AddGreenToBlueAndRed(right);
  126. }
  127. max_diffs[x] = MaxDiffAroundPixel(current, up, down, left, right);
  128. }
  129. }
  130. // Quantize the difference between the actual component value and its prediction
  131. // to a multiple of quantization, working modulo 256, taking care not to cross
  132. // a boundary (inclusive upper limit).
  133. static uint8_t NearLosslessComponent(uint8_t value, uint8_t predict,
  134. uint8_t boundary, int quantization) {
  135. const int residual = (value - predict) & 0xff;
  136. const int boundary_residual = (boundary - predict) & 0xff;
  137. const int lower = residual & ~(quantization - 1);
  138. const int upper = lower + quantization;
  139. // Resolve ties towards a value closer to the prediction (i.e. towards lower
  140. // if value comes after prediction and towards upper otherwise).
  141. const int bias = ((boundary - value) & 0xff) < boundary_residual;
  142. if (residual - lower < upper - residual + bias) {
  143. // lower is closer to residual than upper.
  144. if (residual > boundary_residual && lower <= boundary_residual) {
  145. // Halve quantization step to avoid crossing boundary. This midpoint is
  146. // on the same side of boundary as residual because midpoint >= residual
  147. // (since lower is closer than upper) and residual is above the boundary.
  148. return lower + (quantization >> 1);
  149. }
  150. return lower;
  151. } else {
  152. // upper is closer to residual than lower.
  153. if (residual <= boundary_residual && upper > boundary_residual) {
  154. // Halve quantization step to avoid crossing boundary. This midpoint is
  155. // on the same side of boundary as residual because midpoint <= residual
  156. // (since upper is closer than lower) and residual is below the boundary.
  157. return lower + (quantization >> 1);
  158. }
  159. return upper & 0xff;
  160. }
  161. }
  162. // Quantize every component of the difference between the actual pixel value and
  163. // its prediction to a multiple of a quantization (a power of 2, not larger than
  164. // max_quantization which is a power of 2, smaller than max_diff). Take care if
  165. // value and predict have undergone subtract green, which means that red and
  166. // blue are represented as offsets from green.
  167. static uint32_t NearLossless(uint32_t value, uint32_t predict,
  168. int max_quantization, int max_diff,
  169. int used_subtract_green) {
  170. int quantization;
  171. uint8_t new_green = 0;
  172. uint8_t green_diff = 0;
  173. uint8_t a, r, g, b;
  174. if (max_diff <= 2) {
  175. return VP8LSubPixels(value, predict);
  176. }
  177. quantization = max_quantization;
  178. while (quantization >= max_diff) {
  179. quantization >>= 1;
  180. }
  181. if ((value >> 24) == 0 || (value >> 24) == 0xff) {
  182. // Preserve transparency of fully transparent or fully opaque pixels.
  183. a = ((value >> 24) - (predict >> 24)) & 0xff;
  184. } else {
  185. a = NearLosslessComponent(value >> 24, predict >> 24, 0xff, quantization);
  186. }
  187. g = NearLosslessComponent((value >> 8) & 0xff, (predict >> 8) & 0xff, 0xff,
  188. quantization);
  189. if (used_subtract_green) {
  190. // The green offset will be added to red and blue components during decoding
  191. // to obtain the actual red and blue values.
  192. new_green = ((predict >> 8) + g) & 0xff;
  193. // The amount by which green has been adjusted during quantization. It is
  194. // subtracted from red and blue for compensation, to avoid accumulating two
  195. // quantization errors in them.
  196. green_diff = (new_green - (value >> 8)) & 0xff;
  197. }
  198. r = NearLosslessComponent(((value >> 16) - green_diff) & 0xff,
  199. (predict >> 16) & 0xff, 0xff - new_green,
  200. quantization);
  201. b = NearLosslessComponent((value - green_diff) & 0xff, predict & 0xff,
  202. 0xff - new_green, quantization);
  203. return ((uint32_t)a << 24) | ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
  204. }
  205. // Stores the difference between the pixel and its prediction in "out".
  206. // In case of a lossy encoding, updates the source image to avoid propagating
  207. // the deviation further to pixels which depend on the current pixel for their
  208. // predictions.
  209. static WEBP_INLINE void GetResidual(
  210. int width, int height, uint32_t* const upper_row,
  211. uint32_t* const current_row, const uint8_t* const max_diffs, int mode,
  212. int x_start, int x_end, int y, int max_quantization, int exact,
  213. int used_subtract_green, uint32_t* const out) {
  214. if (exact) {
  215. PredictBatch(mode, x_start, y, x_end - x_start, current_row, upper_row,
  216. out);
  217. } else {
  218. const VP8LPredictorFunc pred_func = VP8LPredictors[mode];
  219. int x;
  220. for (x = x_start; x < x_end; ++x) {
  221. uint32_t predict;
  222. uint32_t residual;
  223. if (y == 0) {
  224. predict = (x == 0) ? ARGB_BLACK : current_row[x - 1]; // Left.
  225. } else if (x == 0) {
  226. predict = upper_row[x]; // Top.
  227. } else {
  228. predict = pred_func(current_row[x - 1], upper_row + x);
  229. }
  230. if (max_quantization == 1 || mode == 0 || y == 0 || y == height - 1 ||
  231. x == 0 || x == width - 1) {
  232. residual = VP8LSubPixels(current_row[x], predict);
  233. } else {
  234. residual = NearLossless(current_row[x], predict, max_quantization,
  235. max_diffs[x], used_subtract_green);
  236. // Update the source image.
  237. current_row[x] = VP8LAddPixels(predict, residual);
  238. // x is never 0 here so we do not need to update upper_row like below.
  239. }
  240. if ((current_row[x] & kMaskAlpha) == 0) {
  241. // If alpha is 0, cleanup RGB. We can choose the RGB values of the
  242. // residual for best compression. The prediction of alpha itself can be
  243. // non-zero and must be kept though. We choose RGB of the residual to be
  244. // 0.
  245. residual &= kMaskAlpha;
  246. // Update the source image.
  247. current_row[x] = predict & ~kMaskAlpha;
  248. // The prediction for the rightmost pixel in a row uses the leftmost
  249. // pixel
  250. // in that row as its top-right context pixel. Hence if we change the
  251. // leftmost pixel of current_row, the corresponding change must be
  252. // applied
  253. // to upper_row as well where top-right context is being read from.
  254. if (x == 0 && y != 0) upper_row[width] = current_row[0];
  255. }
  256. out[x - x_start] = residual;
  257. }
  258. }
  259. }
  260. // Returns best predictor and updates the accumulated histogram.
  261. // If max_quantization > 1, assumes that near lossless processing will be
  262. // applied, quantizing residuals to multiples of quantization levels up to
  263. // max_quantization (the actual quantization level depends on smoothness near
  264. // the given pixel).
  265. static int GetBestPredictorForTile(int width, int height,
  266. int tile_x, int tile_y, int bits,
  267. int accumulated[4][256],
  268. uint32_t* const argb_scratch,
  269. const uint32_t* const argb,
  270. int max_quantization,
  271. int exact, int used_subtract_green,
  272. const uint32_t* const modes) {
  273. const int kNumPredModes = 14;
  274. const int start_x = tile_x << bits;
  275. const int start_y = tile_y << bits;
  276. const int tile_size = 1 << bits;
  277. const int max_y = GetMin(tile_size, height - start_y);
  278. const int max_x = GetMin(tile_size, width - start_x);
  279. // Whether there exist columns just outside the tile.
  280. const int have_left = (start_x > 0);
  281. const int have_right = (max_x < width - start_x);
  282. // Position and size of the strip covering the tile and adjacent columns if
  283. // they exist.
  284. const int context_start_x = start_x - have_left;
  285. const int context_width = max_x + have_left + have_right;
  286. const int tiles_per_row = VP8LSubSampleSize(width, bits);
  287. // Prediction modes of the left and above neighbor tiles.
  288. const int left_mode = (tile_x > 0) ?
  289. (modes[tile_y * tiles_per_row + tile_x - 1] >> 8) & 0xff : 0xff;
  290. const int above_mode = (tile_y > 0) ?
  291. (modes[(tile_y - 1) * tiles_per_row + tile_x] >> 8) & 0xff : 0xff;
  292. // The width of upper_row and current_row is one pixel larger than image width
  293. // to allow the top right pixel to point to the leftmost pixel of the next row
  294. // when at the right edge.
  295. uint32_t* upper_row = argb_scratch;
  296. uint32_t* current_row = upper_row + width + 1;
  297. uint8_t* const max_diffs = (uint8_t*)(current_row + width + 1);
  298. float best_diff = MAX_DIFF_COST;
  299. int best_mode = 0;
  300. int mode;
  301. int histo_stack_1[4][256];
  302. int histo_stack_2[4][256];
  303. // Need pointers to be able to swap arrays.
  304. int (*histo_argb)[256] = histo_stack_1;
  305. int (*best_histo)[256] = histo_stack_2;
  306. int i, j;
  307. uint32_t residuals[1 << MAX_TRANSFORM_BITS];
  308. assert(bits <= MAX_TRANSFORM_BITS);
  309. assert(max_x <= (1 << MAX_TRANSFORM_BITS));
  310. for (mode = 0; mode < kNumPredModes; ++mode) {
  311. float cur_diff;
  312. int relative_y;
  313. memset(histo_argb, 0, sizeof(histo_stack_1));
  314. if (start_y > 0) {
  315. // Read the row above the tile which will become the first upper_row.
  316. // Include a pixel to the left if it exists; include a pixel to the right
  317. // in all cases (wrapping to the leftmost pixel of the next row if it does
  318. // not exist).
  319. memcpy(current_row + context_start_x,
  320. argb + (start_y - 1) * width + context_start_x,
  321. sizeof(*argb) * (max_x + have_left + 1));
  322. }
  323. for (relative_y = 0; relative_y < max_y; ++relative_y) {
  324. const int y = start_y + relative_y;
  325. int relative_x;
  326. uint32_t* tmp = upper_row;
  327. upper_row = current_row;
  328. current_row = tmp;
  329. // Read current_row. Include a pixel to the left if it exists; include a
  330. // pixel to the right in all cases except at the bottom right corner of
  331. // the image (wrapping to the leftmost pixel of the next row if it does
  332. // not exist in the current row).
  333. memcpy(current_row + context_start_x,
  334. argb + y * width + context_start_x,
  335. sizeof(*argb) * (max_x + have_left + (y + 1 < height)));
  336. if (max_quantization > 1 && y >= 1 && y + 1 < height) {
  337. MaxDiffsForRow(context_width, width, argb + y * width + context_start_x,
  338. max_diffs + context_start_x, used_subtract_green);
  339. }
  340. GetResidual(width, height, upper_row, current_row, max_diffs, mode,
  341. start_x, start_x + max_x, y, max_quantization, exact,
  342. used_subtract_green, residuals);
  343. for (relative_x = 0; relative_x < max_x; ++relative_x) {
  344. UpdateHisto(histo_argb, residuals[relative_x]);
  345. }
  346. }
  347. cur_diff = PredictionCostSpatialHistogram(
  348. (const int (*)[256])accumulated, (const int (*)[256])histo_argb);
  349. // Favor keeping the areas locally similar.
  350. if (mode == left_mode) cur_diff -= kSpatialPredictorBias;
  351. if (mode == above_mode) cur_diff -= kSpatialPredictorBias;
  352. if (cur_diff < best_diff) {
  353. int (*tmp)[256] = histo_argb;
  354. histo_argb = best_histo;
  355. best_histo = tmp;
  356. best_diff = cur_diff;
  357. best_mode = mode;
  358. }
  359. }
  360. for (i = 0; i < 4; i++) {
  361. for (j = 0; j < 256; j++) {
  362. accumulated[i][j] += best_histo[i][j];
  363. }
  364. }
  365. return best_mode;
  366. }
  367. // Converts pixels of the image to residuals with respect to predictions.
  368. // If max_quantization > 1, applies near lossless processing, quantizing
  369. // residuals to multiples of quantization levels up to max_quantization
  370. // (the actual quantization level depends on smoothness near the given pixel).
  371. static void CopyImageWithPrediction(int width, int height,
  372. int bits, uint32_t* const modes,
  373. uint32_t* const argb_scratch,
  374. uint32_t* const argb,
  375. int low_effort, int max_quantization,
  376. int exact, int used_subtract_green) {
  377. const int tiles_per_row = VP8LSubSampleSize(width, bits);
  378. // The width of upper_row and current_row is one pixel larger than image width
  379. // to allow the top right pixel to point to the leftmost pixel of the next row
  380. // when at the right edge.
  381. uint32_t* upper_row = argb_scratch;
  382. uint32_t* current_row = upper_row + width + 1;
  383. uint8_t* current_max_diffs = (uint8_t*)(current_row + width + 1);
  384. uint8_t* lower_max_diffs = current_max_diffs + width;
  385. int y;
  386. for (y = 0; y < height; ++y) {
  387. int x;
  388. uint32_t* const tmp32 = upper_row;
  389. upper_row = current_row;
  390. current_row = tmp32;
  391. memcpy(current_row, argb + y * width,
  392. sizeof(*argb) * (width + (y + 1 < height)));
  393. if (low_effort) {
  394. PredictBatch(kPredLowEffort, 0, y, width, current_row, upper_row,
  395. argb + y * width);
  396. } else {
  397. if (max_quantization > 1) {
  398. // Compute max_diffs for the lower row now, because that needs the
  399. // contents of argb for the current row, which we will overwrite with
  400. // residuals before proceeding with the next row.
  401. uint8_t* const tmp8 = current_max_diffs;
  402. current_max_diffs = lower_max_diffs;
  403. lower_max_diffs = tmp8;
  404. if (y + 2 < height) {
  405. MaxDiffsForRow(width, width, argb + (y + 1) * width, lower_max_diffs,
  406. used_subtract_green);
  407. }
  408. }
  409. for (x = 0; x < width;) {
  410. const int mode =
  411. (modes[(y >> bits) * tiles_per_row + (x >> bits)] >> 8) & 0xff;
  412. int x_end = x + (1 << bits);
  413. if (x_end > width) x_end = width;
  414. GetResidual(width, height, upper_row, current_row, current_max_diffs,
  415. mode, x, x_end, y, max_quantization, exact,
  416. used_subtract_green, argb + y * width + x);
  417. x = x_end;
  418. }
  419. }
  420. }
  421. }
  422. // Finds the best predictor for each tile, and converts the image to residuals
  423. // with respect to predictions. If near_lossless_quality < 100, applies
  424. // near lossless processing, shaving off more bits of residuals for lower
  425. // qualities.
  426. void VP8LResidualImage(int width, int height, int bits, int low_effort,
  427. uint32_t* const argb, uint32_t* const argb_scratch,
  428. uint32_t* const image, int near_lossless_quality,
  429. int exact, int used_subtract_green) {
  430. const int tiles_per_row = VP8LSubSampleSize(width, bits);
  431. const int tiles_per_col = VP8LSubSampleSize(height, bits);
  432. int tile_y;
  433. int histo[4][256];
  434. const int max_quantization = 1 << VP8LNearLosslessBits(near_lossless_quality);
  435. if (low_effort) {
  436. int i;
  437. for (i = 0; i < tiles_per_row * tiles_per_col; ++i) {
  438. image[i] = ARGB_BLACK | (kPredLowEffort << 8);
  439. }
  440. } else {
  441. memset(histo, 0, sizeof(histo));
  442. for (tile_y = 0; tile_y < tiles_per_col; ++tile_y) {
  443. int tile_x;
  444. for (tile_x = 0; tile_x < tiles_per_row; ++tile_x) {
  445. const int pred = GetBestPredictorForTile(width, height, tile_x, tile_y,
  446. bits, histo, argb_scratch, argb, max_quantization, exact,
  447. used_subtract_green, image);
  448. image[tile_y * tiles_per_row + tile_x] = ARGB_BLACK | (pred << 8);
  449. }
  450. }
  451. }
  452. CopyImageWithPrediction(width, height, bits, image, argb_scratch, argb,
  453. low_effort, max_quantization, exact,
  454. used_subtract_green);
  455. }
  456. //------------------------------------------------------------------------------
  457. // Color transform functions.
  458. static WEBP_INLINE void MultipliersClear(VP8LMultipliers* const m) {
  459. m->green_to_red_ = 0;
  460. m->green_to_blue_ = 0;
  461. m->red_to_blue_ = 0;
  462. }
  463. static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
  464. VP8LMultipliers* const m) {
  465. m->green_to_red_ = (color_code >> 0) & 0xff;
  466. m->green_to_blue_ = (color_code >> 8) & 0xff;
  467. m->red_to_blue_ = (color_code >> 16) & 0xff;
  468. }
  469. static WEBP_INLINE uint32_t MultipliersToColorCode(
  470. const VP8LMultipliers* const m) {
  471. return 0xff000000u |
  472. ((uint32_t)(m->red_to_blue_) << 16) |
  473. ((uint32_t)(m->green_to_blue_) << 8) |
  474. m->green_to_red_;
  475. }
  476. static float PredictionCostCrossColor(const int accumulated[256],
  477. const int counts[256]) {
  478. // Favor low entropy, locally and globally.
  479. // Favor small absolute values for PredictionCostSpatial
  480. static const double kExpValue = 2.4;
  481. return VP8LCombinedShannonEntropy(counts, accumulated) +
  482. PredictionCostSpatial(counts, 3, kExpValue);
  483. }
  484. static float GetPredictionCostCrossColorRed(
  485. const uint32_t* argb, int stride, int tile_width, int tile_height,
  486. VP8LMultipliers prev_x, VP8LMultipliers prev_y, int green_to_red,
  487. const int accumulated_red_histo[256]) {
  488. int histo[256] = { 0 };
  489. float cur_diff;
  490. VP8LCollectColorRedTransforms(argb, stride, tile_width, tile_height,
  491. green_to_red, histo);
  492. cur_diff = PredictionCostCrossColor(accumulated_red_histo, histo);
  493. if ((uint8_t)green_to_red == prev_x.green_to_red_) {
  494. cur_diff -= 3; // favor keeping the areas locally similar
  495. }
  496. if ((uint8_t)green_to_red == prev_y.green_to_red_) {
  497. cur_diff -= 3; // favor keeping the areas locally similar
  498. }
  499. if (green_to_red == 0) {
  500. cur_diff -= 3;
  501. }
  502. return cur_diff;
  503. }
  504. static void GetBestGreenToRed(
  505. const uint32_t* argb, int stride, int tile_width, int tile_height,
  506. VP8LMultipliers prev_x, VP8LMultipliers prev_y, int quality,
  507. const int accumulated_red_histo[256], VP8LMultipliers* const best_tx) {
  508. const int kMaxIters = 4 + ((7 * quality) >> 8); // in range [4..6]
  509. int green_to_red_best = 0;
  510. int iter, offset;
  511. float best_diff = GetPredictionCostCrossColorRed(
  512. argb, stride, tile_width, tile_height, prev_x, prev_y,
  513. green_to_red_best, accumulated_red_histo);
  514. for (iter = 0; iter < kMaxIters; ++iter) {
  515. // ColorTransformDelta is a 3.5 bit fixed point, so 32 is equal to
  516. // one in color computation. Having initial delta here as 1 is sufficient
  517. // to explore the range of (-2, 2).
  518. const int delta = 32 >> iter;
  519. // Try a negative and a positive delta from the best known value.
  520. for (offset = -delta; offset <= delta; offset += 2 * delta) {
  521. const int green_to_red_cur = offset + green_to_red_best;
  522. const float cur_diff = GetPredictionCostCrossColorRed(
  523. argb, stride, tile_width, tile_height, prev_x, prev_y,
  524. green_to_red_cur, accumulated_red_histo);
  525. if (cur_diff < best_diff) {
  526. best_diff = cur_diff;
  527. green_to_red_best = green_to_red_cur;
  528. }
  529. }
  530. }
  531. best_tx->green_to_red_ = green_to_red_best;
  532. }
  533. static float GetPredictionCostCrossColorBlue(
  534. const uint32_t* argb, int stride, int tile_width, int tile_height,
  535. VP8LMultipliers prev_x, VP8LMultipliers prev_y,
  536. int green_to_blue, int red_to_blue, const int accumulated_blue_histo[256]) {
  537. int histo[256] = { 0 };
  538. float cur_diff;
  539. VP8LCollectColorBlueTransforms(argb, stride, tile_width, tile_height,
  540. green_to_blue, red_to_blue, histo);
  541. cur_diff = PredictionCostCrossColor(accumulated_blue_histo, histo);
  542. if ((uint8_t)green_to_blue == prev_x.green_to_blue_) {
  543. cur_diff -= 3; // favor keeping the areas locally similar
  544. }
  545. if ((uint8_t)green_to_blue == prev_y.green_to_blue_) {
  546. cur_diff -= 3; // favor keeping the areas locally similar
  547. }
  548. if ((uint8_t)red_to_blue == prev_x.red_to_blue_) {
  549. cur_diff -= 3; // favor keeping the areas locally similar
  550. }
  551. if ((uint8_t)red_to_blue == prev_y.red_to_blue_) {
  552. cur_diff -= 3; // favor keeping the areas locally similar
  553. }
  554. if (green_to_blue == 0) {
  555. cur_diff -= 3;
  556. }
  557. if (red_to_blue == 0) {
  558. cur_diff -= 3;
  559. }
  560. return cur_diff;
  561. }
  562. #define kGreenRedToBlueNumAxis 8
  563. #define kGreenRedToBlueMaxIters 7
  564. static void GetBestGreenRedToBlue(
  565. const uint32_t* argb, int stride, int tile_width, int tile_height,
  566. VP8LMultipliers prev_x, VP8LMultipliers prev_y, int quality,
  567. const int accumulated_blue_histo[256],
  568. VP8LMultipliers* const best_tx) {
  569. const int8_t offset[kGreenRedToBlueNumAxis][2] =
  570. {{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
  571. const int8_t delta_lut[kGreenRedToBlueMaxIters] = { 16, 16, 8, 4, 2, 2, 2 };
  572. const int iters =
  573. (quality < 25) ? 1 : (quality > 50) ? kGreenRedToBlueMaxIters : 4;
  574. int green_to_blue_best = 0;
  575. int red_to_blue_best = 0;
  576. int iter;
  577. // Initial value at origin:
  578. float best_diff = GetPredictionCostCrossColorBlue(
  579. argb, stride, tile_width, tile_height, prev_x, prev_y,
  580. green_to_blue_best, red_to_blue_best, accumulated_blue_histo);
  581. for (iter = 0; iter < iters; ++iter) {
  582. const int delta = delta_lut[iter];
  583. int axis;
  584. for (axis = 0; axis < kGreenRedToBlueNumAxis; ++axis) {
  585. const int green_to_blue_cur =
  586. offset[axis][0] * delta + green_to_blue_best;
  587. const int red_to_blue_cur = offset[axis][1] * delta + red_to_blue_best;
  588. const float cur_diff = GetPredictionCostCrossColorBlue(
  589. argb, stride, tile_width, tile_height, prev_x, prev_y,
  590. green_to_blue_cur, red_to_blue_cur, accumulated_blue_histo);
  591. if (cur_diff < best_diff) {
  592. best_diff = cur_diff;
  593. green_to_blue_best = green_to_blue_cur;
  594. red_to_blue_best = red_to_blue_cur;
  595. }
  596. if (quality < 25 && iter == 4) {
  597. // Only axis aligned diffs for lower quality.
  598. break; // next iter.
  599. }
  600. }
  601. if (delta == 2 && green_to_blue_best == 0 && red_to_blue_best == 0) {
  602. // Further iterations would not help.
  603. break; // out of iter-loop.
  604. }
  605. }
  606. best_tx->green_to_blue_ = green_to_blue_best;
  607. best_tx->red_to_blue_ = red_to_blue_best;
  608. }
  609. #undef kGreenRedToBlueMaxIters
  610. #undef kGreenRedToBlueNumAxis
  611. static VP8LMultipliers GetBestColorTransformForTile(
  612. int tile_x, int tile_y, int bits,
  613. VP8LMultipliers prev_x,
  614. VP8LMultipliers prev_y,
  615. int quality, int xsize, int ysize,
  616. const int accumulated_red_histo[256],
  617. const int accumulated_blue_histo[256],
  618. const uint32_t* const argb) {
  619. const int max_tile_size = 1 << bits;
  620. const int tile_y_offset = tile_y * max_tile_size;
  621. const int tile_x_offset = tile_x * max_tile_size;
  622. const int all_x_max = GetMin(tile_x_offset + max_tile_size, xsize);
  623. const int all_y_max = GetMin(tile_y_offset + max_tile_size, ysize);
  624. const int tile_width = all_x_max - tile_x_offset;
  625. const int tile_height = all_y_max - tile_y_offset;
  626. const uint32_t* const tile_argb = argb + tile_y_offset * xsize
  627. + tile_x_offset;
  628. VP8LMultipliers best_tx;
  629. MultipliersClear(&best_tx);
  630. GetBestGreenToRed(tile_argb, xsize, tile_width, tile_height,
  631. prev_x, prev_y, quality, accumulated_red_histo, &best_tx);
  632. GetBestGreenRedToBlue(tile_argb, xsize, tile_width, tile_height,
  633. prev_x, prev_y, quality, accumulated_blue_histo,
  634. &best_tx);
  635. return best_tx;
  636. }
  637. static void CopyTileWithColorTransform(int xsize, int ysize,
  638. int tile_x, int tile_y,
  639. int max_tile_size,
  640. VP8LMultipliers color_transform,
  641. uint32_t* argb) {
  642. const int xscan = GetMin(max_tile_size, xsize - tile_x);
  643. int yscan = GetMin(max_tile_size, ysize - tile_y);
  644. argb += tile_y * xsize + tile_x;
  645. while (yscan-- > 0) {
  646. VP8LTransformColor(&color_transform, argb, xscan);
  647. argb += xsize;
  648. }
  649. }
  650. void VP8LColorSpaceTransform(int width, int height, int bits, int quality,
  651. uint32_t* const argb, uint32_t* image) {
  652. const int max_tile_size = 1 << bits;
  653. const int tile_xsize = VP8LSubSampleSize(width, bits);
  654. const int tile_ysize = VP8LSubSampleSize(height, bits);
  655. int accumulated_red_histo[256] = { 0 };
  656. int accumulated_blue_histo[256] = { 0 };
  657. int tile_x, tile_y;
  658. VP8LMultipliers prev_x, prev_y;
  659. MultipliersClear(&prev_y);
  660. MultipliersClear(&prev_x);
  661. for (tile_y = 0; tile_y < tile_ysize; ++tile_y) {
  662. for (tile_x = 0; tile_x < tile_xsize; ++tile_x) {
  663. int y;
  664. const int tile_x_offset = tile_x * max_tile_size;
  665. const int tile_y_offset = tile_y * max_tile_size;
  666. const int all_x_max = GetMin(tile_x_offset + max_tile_size, width);
  667. const int all_y_max = GetMin(tile_y_offset + max_tile_size, height);
  668. const int offset = tile_y * tile_xsize + tile_x;
  669. if (tile_y != 0) {
  670. ColorCodeToMultipliers(image[offset - tile_xsize], &prev_y);
  671. }
  672. prev_x = GetBestColorTransformForTile(tile_x, tile_y, bits,
  673. prev_x, prev_y,
  674. quality, width, height,
  675. accumulated_red_histo,
  676. accumulated_blue_histo,
  677. argb);
  678. image[offset] = MultipliersToColorCode(&prev_x);
  679. CopyTileWithColorTransform(width, height, tile_x_offset, tile_y_offset,
  680. max_tile_size, prev_x, argb);
  681. // Gather accumulated histogram data.
  682. for (y = tile_y_offset; y < all_y_max; ++y) {
  683. int ix = y * width + tile_x_offset;
  684. const int ix_end = ix + all_x_max - tile_x_offset;
  685. for (; ix < ix_end; ++ix) {
  686. const uint32_t pix = argb[ix];
  687. if (ix >= 2 &&
  688. pix == argb[ix - 2] &&
  689. pix == argb[ix - 1]) {
  690. continue; // repeated pixels are handled by backward references
  691. }
  692. if (ix >= width + 2 &&
  693. argb[ix - 2] == argb[ix - width - 2] &&
  694. argb[ix - 1] == argb[ix - width - 1] &&
  695. pix == argb[ix - width]) {
  696. continue; // repeated pixels are handled by backward references
  697. }
  698. ++accumulated_red_histo[(pix >> 16) & 0xff];
  699. ++accumulated_blue_histo[(pix >> 0) & 0xff];
  700. }
  701. }
  702. }
  703. }
  704. }