vp9_picklpf.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2010 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 <assert.h>
  11. #include <limits.h>
  12. #include "./vpx_scale_rtcd.h"
  13. #include "vpx_dsp/psnr.h"
  14. #include "vpx_mem/vpx_mem.h"
  15. #include "vpx_ports/mem.h"
  16. #include "vp9/common/vp9_loopfilter.h"
  17. #include "vp9/common/vp9_onyxc_int.h"
  18. #include "vp9/common/vp9_quant_common.h"
  19. #include "vp9/encoder/vp9_encoder.h"
  20. #include "vp9/encoder/vp9_picklpf.h"
  21. #include "vp9/encoder/vp9_quantize.h"
  22. static int get_max_filter_level(const VP9_COMP *cpi) {
  23. if (cpi->oxcf.pass == 2) {
  24. return cpi->twopass.section_intra_rating > 8 ? MAX_LOOP_FILTER * 3 / 4
  25. : MAX_LOOP_FILTER;
  26. } else {
  27. return MAX_LOOP_FILTER;
  28. }
  29. }
  30. static int64_t try_filter_frame(const YV12_BUFFER_CONFIG *sd,
  31. VP9_COMP *const cpi, int filt_level,
  32. int partial_frame) {
  33. VP9_COMMON *const cm = &cpi->common;
  34. int64_t filt_err;
  35. vp9_build_mask_frame(cm, filt_level, partial_frame);
  36. if (cpi->num_workers > 1)
  37. vp9_loop_filter_frame_mt(cm->frame_to_show, cm, cpi->td.mb.e_mbd.plane,
  38. filt_level, 1, partial_frame, cpi->workers,
  39. cpi->num_workers, &cpi->lf_row_sync);
  40. else
  41. vp9_loop_filter_frame(cm->frame_to_show, cm, &cpi->td.mb.e_mbd, filt_level,
  42. 1, partial_frame);
  43. #if CONFIG_VP9_HIGHBITDEPTH
  44. if (cm->use_highbitdepth) {
  45. filt_err = vpx_highbd_get_y_sse(sd, cm->frame_to_show);
  46. } else {
  47. filt_err = vpx_get_y_sse(sd, cm->frame_to_show);
  48. }
  49. #else
  50. filt_err = vpx_get_y_sse(sd, cm->frame_to_show);
  51. #endif // CONFIG_VP9_HIGHBITDEPTH
  52. // Re-instate the unfiltered frame
  53. vpx_yv12_copy_y(&cpi->last_frame_uf, cm->frame_to_show);
  54. return filt_err;
  55. }
  56. static int search_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
  57. int partial_frame) {
  58. const VP9_COMMON *const cm = &cpi->common;
  59. const struct loopfilter *const lf = &cm->lf;
  60. const int min_filter_level = 0;
  61. const int max_filter_level = get_max_filter_level(cpi);
  62. int filt_direction = 0;
  63. int64_t best_err;
  64. int filt_best;
  65. // Start the search at the previous frame filter level unless it is now out of
  66. // range.
  67. int filt_mid = clamp(lf->last_filt_level, min_filter_level, max_filter_level);
  68. int filter_step = filt_mid < 16 ? 4 : filt_mid / 4;
  69. // Sum squared error at each filter level
  70. int64_t ss_err[MAX_LOOP_FILTER + 1];
  71. // Set each entry to -1
  72. memset(ss_err, 0xFF, sizeof(ss_err));
  73. // Make a copy of the unfiltered / processed recon buffer
  74. vpx_yv12_copy_y(cm->frame_to_show, &cpi->last_frame_uf);
  75. best_err = try_filter_frame(sd, cpi, filt_mid, partial_frame);
  76. filt_best = filt_mid;
  77. ss_err[filt_mid] = best_err;
  78. while (filter_step > 0) {
  79. const int filt_high = VPXMIN(filt_mid + filter_step, max_filter_level);
  80. const int filt_low = VPXMAX(filt_mid - filter_step, min_filter_level);
  81. // Bias against raising loop filter in favor of lowering it.
  82. int64_t bias = (best_err >> (15 - (filt_mid / 8))) * filter_step;
  83. if ((cpi->oxcf.pass == 2) && (cpi->twopass.section_intra_rating < 20))
  84. bias = (bias * cpi->twopass.section_intra_rating) / 20;
  85. // yx, bias less for large block size
  86. if (cm->tx_mode != ONLY_4X4) bias >>= 1;
  87. if (filt_direction <= 0 && filt_low != filt_mid) {
  88. // Get Low filter error score
  89. if (ss_err[filt_low] < 0) {
  90. ss_err[filt_low] = try_filter_frame(sd, cpi, filt_low, partial_frame);
  91. }
  92. // If value is close to the best so far then bias towards a lower loop
  93. // filter value.
  94. if ((ss_err[filt_low] - bias) < best_err) {
  95. // Was it actually better than the previous best?
  96. if (ss_err[filt_low] < best_err) best_err = ss_err[filt_low];
  97. filt_best = filt_low;
  98. }
  99. }
  100. // Now look at filt_high
  101. if (filt_direction >= 0 && filt_high != filt_mid) {
  102. if (ss_err[filt_high] < 0) {
  103. ss_err[filt_high] = try_filter_frame(sd, cpi, filt_high, partial_frame);
  104. }
  105. // Was it better than the previous best?
  106. if (ss_err[filt_high] < (best_err - bias)) {
  107. best_err = ss_err[filt_high];
  108. filt_best = filt_high;
  109. }
  110. }
  111. // Half the step distance if the best filter value was the same as last time
  112. if (filt_best == filt_mid) {
  113. filter_step /= 2;
  114. filt_direction = 0;
  115. } else {
  116. filt_direction = (filt_best < filt_mid) ? -1 : 1;
  117. filt_mid = filt_best;
  118. }
  119. }
  120. return filt_best;
  121. }
  122. void vp9_pick_filter_level(const YV12_BUFFER_CONFIG *sd, VP9_COMP *cpi,
  123. LPF_PICK_METHOD method) {
  124. VP9_COMMON *const cm = &cpi->common;
  125. struct loopfilter *const lf = &cm->lf;
  126. lf->sharpness_level = cm->frame_type == KEY_FRAME ? 0 : cpi->oxcf.sharpness;
  127. if (method == LPF_PICK_MINIMAL_LPF && lf->filter_level) {
  128. lf->filter_level = 0;
  129. } else if (method >= LPF_PICK_FROM_Q) {
  130. const int min_filter_level = 0;
  131. const int max_filter_level = get_max_filter_level(cpi);
  132. const int q = vp9_ac_quant(cm->base_qindex, 0, cm->bit_depth);
  133. // These values were determined by linear fitting the result of the
  134. // searched level, filt_guess = q * 0.316206 + 3.87252
  135. #if CONFIG_VP9_HIGHBITDEPTH
  136. int filt_guess;
  137. switch (cm->bit_depth) {
  138. case VPX_BITS_8:
  139. filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
  140. break;
  141. case VPX_BITS_10:
  142. filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 4060632, 20);
  143. break;
  144. case VPX_BITS_12:
  145. filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 16242526, 22);
  146. break;
  147. default:
  148. assert(0 &&
  149. "bit_depth should be VPX_BITS_8, VPX_BITS_10 "
  150. "or VPX_BITS_12");
  151. return;
  152. }
  153. #else
  154. int filt_guess = ROUND_POWER_OF_TWO(q * 20723 + 1015158, 18);
  155. #endif // CONFIG_VP9_HIGHBITDEPTH
  156. if (cm->frame_type == KEY_FRAME) filt_guess -= 4;
  157. lf->filter_level = clamp(filt_guess, min_filter_level, max_filter_level);
  158. } else {
  159. lf->filter_level =
  160. search_filter_level(sd, cpi, method == LPF_PICK_FROM_SUBIMAGE);
  161. }
  162. }