vp9_noise_estimate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2015 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 <math.h>
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "vpx_dsp/vpx_dsp_common.h"
  15. #include "vpx_scale/yv12config.h"
  16. #include "vpx/vpx_integer.h"
  17. #include "vp9/common/vp9_reconinter.h"
  18. #include "vp9/encoder/vp9_context_tree.h"
  19. #include "vp9/encoder/vp9_noise_estimate.h"
  20. #include "vp9/encoder/vp9_encoder.h"
  21. #if CONFIG_VP9_TEMPORAL_DENOISING
  22. // For SVC: only do noise estimation on top spatial layer.
  23. static INLINE int noise_est_svc(const struct VP9_COMP *const cpi) {
  24. return (!cpi->use_svc ||
  25. (cpi->use_svc &&
  26. cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1));
  27. }
  28. #endif
  29. void vp9_noise_estimate_init(NOISE_ESTIMATE *const ne, int width, int height) {
  30. ne->enabled = 0;
  31. ne->level = (width * height < 1280 * 720) ? kLowLow : kLow;
  32. ne->value = 0;
  33. ne->count = 0;
  34. ne->thresh = 90;
  35. ne->last_w = 0;
  36. ne->last_h = 0;
  37. if (width * height >= 1920 * 1080) {
  38. ne->thresh = 200;
  39. } else if (width * height >= 1280 * 720) {
  40. ne->thresh = 140;
  41. } else if (width * height >= 640 * 360) {
  42. ne->thresh = 115;
  43. }
  44. ne->num_frames_estimate = 15;
  45. ne->adapt_thresh = (3 * ne->thresh) >> 1;
  46. }
  47. static int enable_noise_estimation(VP9_COMP *const cpi) {
  48. #if CONFIG_VP9_HIGHBITDEPTH
  49. if (cpi->common.use_highbitdepth) return 0;
  50. #endif
  51. // Enable noise estimation if denoising is on.
  52. #if CONFIG_VP9_TEMPORAL_DENOISING
  53. if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi) &&
  54. cpi->common.width >= 320 && cpi->common.height >= 180)
  55. return 1;
  56. #endif
  57. // Only allow noise estimate under certain encoding mode.
  58. // Enabled for 1 pass CBR, speed >=5, and if resolution is same as original.
  59. // Not enabled for SVC mode and screen_content_mode.
  60. // Not enabled for low resolutions.
  61. if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == VPX_CBR &&
  62. cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cpi->oxcf.speed >= 5 &&
  63. cpi->resize_state == ORIG && cpi->resize_pending == 0 && !cpi->use_svc &&
  64. cpi->oxcf.content != VP9E_CONTENT_SCREEN &&
  65. cpi->common.width * cpi->common.height >= 640 * 360)
  66. return 1;
  67. else
  68. return 0;
  69. }
  70. #if CONFIG_VP9_TEMPORAL_DENOISING
  71. static void copy_frame(YV12_BUFFER_CONFIG *const dest,
  72. const YV12_BUFFER_CONFIG *const src) {
  73. int r;
  74. const uint8_t *srcbuf = src->y_buffer;
  75. uint8_t *destbuf = dest->y_buffer;
  76. assert(dest->y_width == src->y_width);
  77. assert(dest->y_height == src->y_height);
  78. for (r = 0; r < dest->y_height; ++r) {
  79. memcpy(destbuf, srcbuf, dest->y_width);
  80. destbuf += dest->y_stride;
  81. srcbuf += src->y_stride;
  82. }
  83. }
  84. #endif // CONFIG_VP9_TEMPORAL_DENOISING
  85. NOISE_LEVEL vp9_noise_estimate_extract_level(NOISE_ESTIMATE *const ne) {
  86. int noise_level = kLowLow;
  87. if (ne->value > (ne->thresh << 1)) {
  88. noise_level = kHigh;
  89. } else {
  90. if (ne->value > ne->thresh)
  91. noise_level = kMedium;
  92. else if (ne->value > (ne->thresh >> 1))
  93. noise_level = kLow;
  94. else
  95. noise_level = kLowLow;
  96. }
  97. return noise_level;
  98. }
  99. void vp9_update_noise_estimate(VP9_COMP *const cpi) {
  100. const VP9_COMMON *const cm = &cpi->common;
  101. NOISE_ESTIMATE *const ne = &cpi->noise_estimate;
  102. const int low_res = (cm->width <= 352 && cm->height <= 288);
  103. // Estimate of noise level every frame_period frames.
  104. int frame_period = 8;
  105. int thresh_consec_zeromv = 6;
  106. int frame_counter = cm->current_video_frame;
  107. // Estimate is between current source and last source.
  108. YV12_BUFFER_CONFIG *last_source = cpi->Last_Source;
  109. #if CONFIG_VP9_TEMPORAL_DENOISING
  110. if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi)) {
  111. last_source = &cpi->denoiser.last_source;
  112. // Tune these thresholds for different resolutions when denoising is
  113. // enabled.
  114. if (cm->width > 640 && cm->width <= 1920) {
  115. thresh_consec_zeromv = 2;
  116. }
  117. }
  118. #endif
  119. ne->enabled = enable_noise_estimation(cpi);
  120. if (cpi->svc.number_spatial_layers > 1)
  121. frame_counter = cpi->svc.current_superframe;
  122. if (!ne->enabled || frame_counter % frame_period != 0 ||
  123. last_source == NULL ||
  124. (cpi->svc.number_spatial_layers == 1 &&
  125. (ne->last_w != cm->width || ne->last_h != cm->height))) {
  126. #if CONFIG_VP9_TEMPORAL_DENOISING
  127. if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi))
  128. copy_frame(&cpi->denoiser.last_source, cpi->Source);
  129. #endif
  130. if (last_source != NULL) {
  131. ne->last_w = cm->width;
  132. ne->last_h = cm->height;
  133. }
  134. return;
  135. } else if (frame_counter > 60 && cpi->svc.num_encoded_top_layer > 1 &&
  136. cpi->rc.frames_since_key > cpi->svc.number_spatial_layers &&
  137. cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1 &&
  138. cpi->rc.avg_frame_low_motion < (low_res ? 60 : 40)) {
  139. // Force noise estimation to 0 and denoiser off if content has high motion.
  140. ne->level = kLowLow;
  141. ne->count = 0;
  142. ne->num_frames_estimate = 10;
  143. #if CONFIG_VP9_TEMPORAL_DENOISING
  144. if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi) &&
  145. cpi->svc.current_superframe > 1) {
  146. vp9_denoiser_set_noise_level(cpi, ne->level);
  147. copy_frame(&cpi->denoiser.last_source, cpi->Source);
  148. }
  149. #endif
  150. return;
  151. } else {
  152. unsigned int bin_size = 100;
  153. unsigned int hist[MAX_VAR_HIST_BINS] = { 0 };
  154. unsigned int hist_avg[MAX_VAR_HIST_BINS];
  155. unsigned int max_bin = 0;
  156. unsigned int max_bin_count = 0;
  157. unsigned int bin_cnt;
  158. int bsize = BLOCK_16X16;
  159. // Loop over sub-sample of 16x16 blocks of frame, and for blocks that have
  160. // been encoded as zero/small mv at least x consecutive frames, compute
  161. // the variance to update estimate of noise in the source.
  162. const uint8_t *src_y = cpi->Source->y_buffer;
  163. const int src_ystride = cpi->Source->y_stride;
  164. const uint8_t *last_src_y = last_source->y_buffer;
  165. const int last_src_ystride = last_source->y_stride;
  166. const uint8_t *src_u = cpi->Source->u_buffer;
  167. const uint8_t *src_v = cpi->Source->v_buffer;
  168. const int src_uvstride = cpi->Source->uv_stride;
  169. int mi_row, mi_col;
  170. int num_low_motion = 0;
  171. int frame_low_motion = 1;
  172. for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
  173. for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
  174. int bl_index = mi_row * cm->mi_cols + mi_col;
  175. if (cpi->consec_zero_mv[bl_index] > thresh_consec_zeromv)
  176. num_low_motion++;
  177. }
  178. }
  179. if (num_low_motion < ((3 * cm->mi_rows * cm->mi_cols) >> 3))
  180. frame_low_motion = 0;
  181. for (mi_row = 0; mi_row < cm->mi_rows; mi_row++) {
  182. for (mi_col = 0; mi_col < cm->mi_cols; mi_col++) {
  183. // 16x16 blocks, 1/4 sample of frame.
  184. if (mi_row % 4 == 0 && mi_col % 4 == 0 && mi_row < cm->mi_rows - 1 &&
  185. mi_col < cm->mi_cols - 1) {
  186. int bl_index = mi_row * cm->mi_cols + mi_col;
  187. int bl_index1 = bl_index + 1;
  188. int bl_index2 = bl_index + cm->mi_cols;
  189. int bl_index3 = bl_index2 + 1;
  190. int consec_zeromv =
  191. VPXMIN(cpi->consec_zero_mv[bl_index],
  192. VPXMIN(cpi->consec_zero_mv[bl_index1],
  193. VPXMIN(cpi->consec_zero_mv[bl_index2],
  194. cpi->consec_zero_mv[bl_index3])));
  195. // Only consider blocks that are likely steady background. i.e, have
  196. // been encoded as zero/low motion x (= thresh_consec_zeromv) frames
  197. // in a row. consec_zero_mv[] defined for 8x8 blocks, so consider all
  198. // 4 sub-blocks for 16x16 block. And exclude this frame if
  199. // high_source_sad is true (i.e., scene/content change).
  200. if (frame_low_motion && consec_zeromv > thresh_consec_zeromv &&
  201. !cpi->rc.high_source_sad &&
  202. !cpi->svc.high_source_sad_superframe) {
  203. int is_skin = 0;
  204. if (cpi->use_skin_detection) {
  205. is_skin =
  206. vp9_compute_skin_block(src_y, src_u, src_v, src_ystride,
  207. src_uvstride, bsize, consec_zeromv, 0);
  208. }
  209. if (!is_skin) {
  210. unsigned int sse;
  211. // Compute variance between co-located blocks from current and
  212. // last input frames.
  213. unsigned int variance = cpi->fn_ptr[bsize].vf(
  214. src_y, src_ystride, last_src_y, last_src_ystride, &sse);
  215. unsigned int hist_index = variance / bin_size;
  216. if (hist_index < MAX_VAR_HIST_BINS)
  217. hist[hist_index]++;
  218. else if (hist_index < 3 * (MAX_VAR_HIST_BINS >> 1))
  219. hist[MAX_VAR_HIST_BINS - 1]++; // Account for the tail
  220. }
  221. }
  222. }
  223. src_y += 8;
  224. last_src_y += 8;
  225. src_u += 4;
  226. src_v += 4;
  227. }
  228. src_y += (src_ystride << 3) - (cm->mi_cols << 3);
  229. last_src_y += (last_src_ystride << 3) - (cm->mi_cols << 3);
  230. src_u += (src_uvstride << 2) - (cm->mi_cols << 2);
  231. src_v += (src_uvstride << 2) - (cm->mi_cols << 2);
  232. }
  233. ne->last_w = cm->width;
  234. ne->last_h = cm->height;
  235. // Adjust histogram to account for effect that histogram flattens
  236. // and shifts to zero as scene darkens.
  237. if (hist[0] > 10 && (hist[MAX_VAR_HIST_BINS - 1] > hist[0] >> 2)) {
  238. hist[0] = 0;
  239. hist[1] >>= 2;
  240. hist[2] >>= 2;
  241. hist[3] >>= 2;
  242. hist[4] >>= 1;
  243. hist[5] >>= 1;
  244. hist[6] = 3 * hist[6] >> 1;
  245. hist[MAX_VAR_HIST_BINS - 1] >>= 1;
  246. }
  247. // Average hist[] and find largest bin
  248. for (bin_cnt = 0; bin_cnt < MAX_VAR_HIST_BINS; bin_cnt++) {
  249. if (bin_cnt == 0)
  250. hist_avg[bin_cnt] = (hist[0] + hist[1] + hist[2]) / 3;
  251. else if (bin_cnt == MAX_VAR_HIST_BINS - 1)
  252. hist_avg[bin_cnt] = hist[MAX_VAR_HIST_BINS - 1] >> 2;
  253. else if (bin_cnt == MAX_VAR_HIST_BINS - 2)
  254. hist_avg[bin_cnt] = (hist[bin_cnt - 1] + 2 * hist[bin_cnt] +
  255. (hist[bin_cnt + 1] >> 1) + 2) >>
  256. 2;
  257. else
  258. hist_avg[bin_cnt] =
  259. (hist[bin_cnt - 1] + 2 * hist[bin_cnt] + hist[bin_cnt + 1] + 2) >>
  260. 2;
  261. if (hist_avg[bin_cnt] > max_bin_count) {
  262. max_bin_count = hist_avg[bin_cnt];
  263. max_bin = bin_cnt;
  264. }
  265. }
  266. // Scale by 40 to work with existing thresholds
  267. ne->value = (int)((3 * ne->value + max_bin * 40) >> 2);
  268. // Quickly increase VNR strength when the noise level increases suddenly.
  269. if (ne->level < kMedium && ne->value > ne->adapt_thresh) {
  270. ne->count = ne->num_frames_estimate;
  271. } else {
  272. ne->count++;
  273. }
  274. if (ne->count == ne->num_frames_estimate) {
  275. // Reset counter and check noise level condition.
  276. ne->num_frames_estimate = 30;
  277. ne->count = 0;
  278. ne->level = vp9_noise_estimate_extract_level(ne);
  279. #if CONFIG_VP9_TEMPORAL_DENOISING
  280. if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi))
  281. vp9_denoiser_set_noise_level(cpi, ne->level);
  282. #endif
  283. }
  284. }
  285. #if CONFIG_VP9_TEMPORAL_DENOISING
  286. if (cpi->oxcf.noise_sensitivity > 0 && noise_est_svc(cpi))
  287. copy_frame(&cpi->denoiser.last_source, cpi->Source);
  288. #endif
  289. }