vp9_aq_variance.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <math.h>
  11. #include "vpx_ports/mem.h"
  12. #include "vpx_ports/system_state.h"
  13. #include "vp9/encoder/vp9_aq_variance.h"
  14. #include "vp9/common/vp9_seg_common.h"
  15. #include "vp9/encoder/vp9_ratectrl.h"
  16. #include "vp9/encoder/vp9_rd.h"
  17. #include "vp9/encoder/vp9_encodeframe.h"
  18. #include "vp9/encoder/vp9_segmentation.h"
  19. #define ENERGY_MIN (-4)
  20. #define ENERGY_MAX (1)
  21. #define ENERGY_SPAN (ENERGY_MAX - ENERGY_MIN + 1)
  22. #define ENERGY_IN_BOUNDS(energy) \
  23. assert((energy) >= ENERGY_MIN && (energy) <= ENERGY_MAX)
  24. static const double rate_ratio[MAX_SEGMENTS] = { 2.5, 2.0, 1.5, 1.0,
  25. 0.75, 1.0, 1.0, 1.0 };
  26. static const int segment_id[ENERGY_SPAN] = { 0, 1, 1, 2, 3, 4 };
  27. #define SEGMENT_ID(i) segment_id[(i)-ENERGY_MIN]
  28. DECLARE_ALIGNED(16, static const uint8_t, vp9_64_zeros[64]) = { 0 };
  29. #if CONFIG_VP9_HIGHBITDEPTH
  30. DECLARE_ALIGNED(16, static const uint16_t, vp9_highbd_64_zeros[64]) = { 0 };
  31. #endif
  32. unsigned int vp9_vaq_segment_id(int energy) {
  33. ENERGY_IN_BOUNDS(energy);
  34. return SEGMENT_ID(energy);
  35. }
  36. void vp9_vaq_frame_setup(VP9_COMP *cpi) {
  37. VP9_COMMON *cm = &cpi->common;
  38. struct segmentation *seg = &cm->seg;
  39. int i;
  40. if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
  41. cpi->refresh_alt_ref_frame || cpi->force_update_segmentation ||
  42. (cpi->refresh_golden_frame && !cpi->rc.is_src_frame_alt_ref)) {
  43. vp9_enable_segmentation(seg);
  44. vp9_clearall_segfeatures(seg);
  45. seg->abs_delta = SEGMENT_DELTADATA;
  46. vpx_clear_system_state();
  47. for (i = 0; i < MAX_SEGMENTS; ++i) {
  48. int qindex_delta =
  49. vp9_compute_qdelta_by_rate(&cpi->rc, cm->frame_type, cm->base_qindex,
  50. rate_ratio[i], cm->bit_depth);
  51. // We don't allow qindex 0 in a segment if the base value is not 0.
  52. // Q index 0 (lossless) implies 4x4 encoding only and in AQ mode a segment
  53. // Q delta is sometimes applied without going back around the rd loop.
  54. // This could lead to an illegal combination of partition size and q.
  55. if ((cm->base_qindex != 0) && ((cm->base_qindex + qindex_delta) == 0)) {
  56. qindex_delta = -cm->base_qindex + 1;
  57. }
  58. // No need to enable SEG_LVL_ALT_Q for this segment.
  59. if (rate_ratio[i] == 1.0) {
  60. continue;
  61. }
  62. vp9_set_segdata(seg, i, SEG_LVL_ALT_Q, qindex_delta);
  63. vp9_enable_segfeature(seg, i, SEG_LVL_ALT_Q);
  64. }
  65. }
  66. }
  67. /* TODO(agrange, paulwilkins): The block_variance calls the unoptimized versions
  68. * of variance() and highbd_8_variance(). It should not.
  69. */
  70. static void aq_variance(const uint8_t *a, int a_stride, const uint8_t *b,
  71. int b_stride, int w, int h, unsigned int *sse,
  72. int *sum) {
  73. int i, j;
  74. *sum = 0;
  75. *sse = 0;
  76. for (i = 0; i < h; i++) {
  77. for (j = 0; j < w; j++) {
  78. const int diff = a[j] - b[j];
  79. *sum += diff;
  80. *sse += diff * diff;
  81. }
  82. a += a_stride;
  83. b += b_stride;
  84. }
  85. }
  86. #if CONFIG_VP9_HIGHBITDEPTH
  87. static void aq_highbd_variance64(const uint8_t *a8, int a_stride,
  88. const uint8_t *b8, int b_stride, int w, int h,
  89. uint64_t *sse, int64_t *sum) {
  90. int i, j;
  91. uint16_t *a = CONVERT_TO_SHORTPTR(a8);
  92. uint16_t *b = CONVERT_TO_SHORTPTR(b8);
  93. *sum = 0;
  94. *sse = 0;
  95. for (i = 0; i < h; i++) {
  96. for (j = 0; j < w; j++) {
  97. const int diff = a[j] - b[j];
  98. *sum += diff;
  99. *sse += diff * diff;
  100. }
  101. a += a_stride;
  102. b += b_stride;
  103. }
  104. }
  105. #endif // CONFIG_VP9_HIGHBITDEPTH
  106. static unsigned int block_variance(VP9_COMP *cpi, MACROBLOCK *x,
  107. BLOCK_SIZE bs) {
  108. MACROBLOCKD *xd = &x->e_mbd;
  109. unsigned int var, sse;
  110. int right_overflow =
  111. (xd->mb_to_right_edge < 0) ? ((-xd->mb_to_right_edge) >> 3) : 0;
  112. int bottom_overflow =
  113. (xd->mb_to_bottom_edge < 0) ? ((-xd->mb_to_bottom_edge) >> 3) : 0;
  114. if (right_overflow || bottom_overflow) {
  115. const int bw = 8 * num_8x8_blocks_wide_lookup[bs] - right_overflow;
  116. const int bh = 8 * num_8x8_blocks_high_lookup[bs] - bottom_overflow;
  117. int avg;
  118. #if CONFIG_VP9_HIGHBITDEPTH
  119. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  120. uint64_t sse64 = 0;
  121. int64_t sum64 = 0;
  122. aq_highbd_variance64(x->plane[0].src.buf, x->plane[0].src.stride,
  123. CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros), 0, bw, bh,
  124. &sse64, &sum64);
  125. sse = (unsigned int)(sse64 >> (2 * (xd->bd - 8)));
  126. avg = (int)(sum64 >> (xd->bd - 8));
  127. } else {
  128. aq_variance(x->plane[0].src.buf, x->plane[0].src.stride, vp9_64_zeros, 0,
  129. bw, bh, &sse, &avg);
  130. }
  131. #else
  132. aq_variance(x->plane[0].src.buf, x->plane[0].src.stride, vp9_64_zeros, 0,
  133. bw, bh, &sse, &avg);
  134. #endif // CONFIG_VP9_HIGHBITDEPTH
  135. var = sse - (unsigned int)(((int64_t)avg * avg) / (bw * bh));
  136. return (unsigned int)(((uint64_t)256 * var) / (bw * bh));
  137. } else {
  138. #if CONFIG_VP9_HIGHBITDEPTH
  139. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  140. var =
  141. cpi->fn_ptr[bs].vf(x->plane[0].src.buf, x->plane[0].src.stride,
  142. CONVERT_TO_BYTEPTR(vp9_highbd_64_zeros), 0, &sse);
  143. } else {
  144. var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, x->plane[0].src.stride,
  145. vp9_64_zeros, 0, &sse);
  146. }
  147. #else
  148. var = cpi->fn_ptr[bs].vf(x->plane[0].src.buf, x->plane[0].src.stride,
  149. vp9_64_zeros, 0, &sse);
  150. #endif // CONFIG_VP9_HIGHBITDEPTH
  151. return (unsigned int)(((uint64_t)256 * var) >> num_pels_log2_lookup[bs]);
  152. }
  153. }
  154. double vp9_log_block_var(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
  155. unsigned int var = block_variance(cpi, x, bs);
  156. vpx_clear_system_state();
  157. return log(var + 1.0);
  158. }
  159. // Get the range of sub block energy values;
  160. void vp9_get_sub_block_energy(VP9_COMP *cpi, MACROBLOCK *mb, int mi_row,
  161. int mi_col, BLOCK_SIZE bsize, int *min_e,
  162. int *max_e) {
  163. VP9_COMMON *const cm = &cpi->common;
  164. const int bw = num_8x8_blocks_wide_lookup[bsize];
  165. const int bh = num_8x8_blocks_high_lookup[bsize];
  166. const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
  167. const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
  168. int x, y;
  169. if (xmis < bw || ymis < bh) {
  170. vp9_setup_src_planes(mb, cpi->Source, mi_row, mi_col);
  171. *min_e = vp9_block_energy(cpi, mb, bsize);
  172. *max_e = *min_e;
  173. } else {
  174. int energy;
  175. *min_e = ENERGY_MAX;
  176. *max_e = ENERGY_MIN;
  177. for (y = 0; y < ymis; ++y) {
  178. for (x = 0; x < xmis; ++x) {
  179. vp9_setup_src_planes(mb, cpi->Source, mi_row + y, mi_col + x);
  180. energy = vp9_block_energy(cpi, mb, BLOCK_8X8);
  181. *min_e = VPXMIN(*min_e, energy);
  182. *max_e = VPXMAX(*max_e, energy);
  183. }
  184. }
  185. }
  186. // Re-instate source pointers back to what they should have been on entry.
  187. vp9_setup_src_planes(mb, cpi->Source, mi_row, mi_col);
  188. }
  189. #define DEFAULT_E_MIDPOINT 10.0
  190. int vp9_block_energy(VP9_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bs) {
  191. double energy;
  192. double energy_midpoint;
  193. vpx_clear_system_state();
  194. energy_midpoint =
  195. (cpi->oxcf.pass == 2) ? cpi->twopass.mb_av_energy : DEFAULT_E_MIDPOINT;
  196. energy = vp9_log_block_var(cpi, x, bs) - energy_midpoint;
  197. return clamp((int)round(energy), ENERGY_MIN, ENERGY_MAX);
  198. }