vp9_denoiser.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /*
  2. * Copyright (c) 2012 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_denoiser.h"
  20. #include "vp9/encoder/vp9_encoder.h"
  21. #ifdef OUTPUT_YUV_DENOISED
  22. static void make_grayscale(YV12_BUFFER_CONFIG *yuv);
  23. #endif
  24. static int absdiff_thresh(BLOCK_SIZE bs, int increase_denoising) {
  25. (void)bs;
  26. return 3 + (increase_denoising ? 1 : 0);
  27. }
  28. static int delta_thresh(BLOCK_SIZE bs, int increase_denoising) {
  29. (void)bs;
  30. (void)increase_denoising;
  31. return 4;
  32. }
  33. static int noise_motion_thresh(BLOCK_SIZE bs, int increase_denoising) {
  34. (void)bs;
  35. (void)increase_denoising;
  36. return 625;
  37. }
  38. static unsigned int sse_thresh(BLOCK_SIZE bs, int increase_denoising) {
  39. return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 80 : 40);
  40. }
  41. static int sse_diff_thresh(BLOCK_SIZE bs, int increase_denoising,
  42. int motion_magnitude) {
  43. if (motion_magnitude > noise_motion_thresh(bs, increase_denoising)) {
  44. if (increase_denoising)
  45. return (1 << num_pels_log2_lookup[bs]) << 2;
  46. else
  47. return 0;
  48. } else {
  49. return (1 << num_pels_log2_lookup[bs]) << 4;
  50. }
  51. }
  52. static int total_adj_weak_thresh(BLOCK_SIZE bs, int increase_denoising) {
  53. return (1 << num_pels_log2_lookup[bs]) * (increase_denoising ? 3 : 2);
  54. }
  55. // TODO(jackychen): If increase_denoising is enabled in the future,
  56. // we might need to update the code for calculating 'total_adj' in
  57. // case the C code is not bit-exact with corresponding sse2 code.
  58. int vp9_denoiser_filter_c(const uint8_t *sig, int sig_stride,
  59. const uint8_t *mc_avg, int mc_avg_stride,
  60. uint8_t *avg, int avg_stride, int increase_denoising,
  61. BLOCK_SIZE bs, int motion_magnitude) {
  62. int r, c;
  63. const uint8_t *sig_start = sig;
  64. const uint8_t *mc_avg_start = mc_avg;
  65. uint8_t *avg_start = avg;
  66. int diff, adj, absdiff, delta;
  67. int adj_val[] = { 3, 4, 6 };
  68. int total_adj = 0;
  69. int shift_inc = 1;
  70. // If motion_magnitude is small, making the denoiser more aggressive by
  71. // increasing the adjustment for each level. Add another increment for
  72. // blocks that are labeled for increase denoising.
  73. if (motion_magnitude <= MOTION_MAGNITUDE_THRESHOLD) {
  74. if (increase_denoising) {
  75. shift_inc = 2;
  76. }
  77. adj_val[0] += shift_inc;
  78. adj_val[1] += shift_inc;
  79. adj_val[2] += shift_inc;
  80. }
  81. // First attempt to apply a strong temporal denoising filter.
  82. for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
  83. for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
  84. diff = mc_avg[c] - sig[c];
  85. absdiff = abs(diff);
  86. if (absdiff <= absdiff_thresh(bs, increase_denoising)) {
  87. avg[c] = mc_avg[c];
  88. total_adj += diff;
  89. } else {
  90. switch (absdiff) {
  91. case 4:
  92. case 5:
  93. case 6:
  94. case 7: adj = adj_val[0]; break;
  95. case 8:
  96. case 9:
  97. case 10:
  98. case 11:
  99. case 12:
  100. case 13:
  101. case 14:
  102. case 15: adj = adj_val[1]; break;
  103. default: adj = adj_val[2];
  104. }
  105. if (diff > 0) {
  106. avg[c] = VPXMIN(UINT8_MAX, sig[c] + adj);
  107. total_adj += adj;
  108. } else {
  109. avg[c] = VPXMAX(0, sig[c] - adj);
  110. total_adj -= adj;
  111. }
  112. }
  113. }
  114. sig += sig_stride;
  115. avg += avg_stride;
  116. mc_avg += mc_avg_stride;
  117. }
  118. // If the strong filter did not modify the signal too much, we're all set.
  119. if (abs(total_adj) <= total_adj_strong_thresh(bs, increase_denoising)) {
  120. return FILTER_BLOCK;
  121. }
  122. // Otherwise, we try to dampen the filter if the delta is not too high.
  123. delta = ((abs(total_adj) - total_adj_strong_thresh(bs, increase_denoising)) >>
  124. num_pels_log2_lookup[bs]) +
  125. 1;
  126. if (delta >= delta_thresh(bs, increase_denoising)) {
  127. return COPY_BLOCK;
  128. }
  129. mc_avg = mc_avg_start;
  130. avg = avg_start;
  131. sig = sig_start;
  132. for (r = 0; r < (4 << b_height_log2_lookup[bs]); ++r) {
  133. for (c = 0; c < (4 << b_width_log2_lookup[bs]); ++c) {
  134. diff = mc_avg[c] - sig[c];
  135. adj = abs(diff);
  136. if (adj > delta) {
  137. adj = delta;
  138. }
  139. if (diff > 0) {
  140. // Diff positive means we made positive adjustment above
  141. // (in first try/attempt), so now make negative adjustment to bring
  142. // denoised signal down.
  143. avg[c] = VPXMAX(0, avg[c] - adj);
  144. total_adj -= adj;
  145. } else {
  146. // Diff negative means we made negative adjustment above
  147. // (in first try/attempt), so now make positive adjustment to bring
  148. // denoised signal up.
  149. avg[c] = VPXMIN(UINT8_MAX, avg[c] + adj);
  150. total_adj += adj;
  151. }
  152. }
  153. sig += sig_stride;
  154. avg += avg_stride;
  155. mc_avg += mc_avg_stride;
  156. }
  157. // We can use the filter if it has been sufficiently dampened
  158. if (abs(total_adj) <= total_adj_weak_thresh(bs, increase_denoising)) {
  159. return FILTER_BLOCK;
  160. }
  161. return COPY_BLOCK;
  162. }
  163. static uint8_t *block_start(uint8_t *framebuf, int stride, int mi_row,
  164. int mi_col) {
  165. return framebuf + (stride * mi_row << 3) + (mi_col << 3);
  166. }
  167. static VP9_DENOISER_DECISION perform_motion_compensation(
  168. VP9_COMMON *const cm, VP9_DENOISER *denoiser, MACROBLOCK *mb, BLOCK_SIZE bs,
  169. int increase_denoising, int mi_row, int mi_col, PICK_MODE_CONTEXT *ctx,
  170. int motion_magnitude, int is_skin, int *zeromv_filter, int consec_zeromv,
  171. int num_spatial_layers, int width, int lst_fb_idx, int gld_fb_idx,
  172. int use_svc, int spatial_layer, int use_gf_temporal_ref) {
  173. const int sse_diff = (ctx->newmv_sse == UINT_MAX)
  174. ? 0
  175. : ((int)ctx->zeromv_sse - (int)ctx->newmv_sse);
  176. int frame;
  177. int denoise_layer_idx = 0;
  178. MACROBLOCKD *filter_mbd = &mb->e_mbd;
  179. MODE_INFO *mi = filter_mbd->mi[0];
  180. MODE_INFO saved_mi;
  181. int i;
  182. struct buf_2d saved_dst[MAX_MB_PLANE];
  183. struct buf_2d saved_pre[MAX_MB_PLANE];
  184. const RefBuffer *saved_block_refs[2];
  185. MV_REFERENCE_FRAME saved_frame;
  186. frame = ctx->best_reference_frame;
  187. saved_mi = *mi;
  188. if (is_skin && (motion_magnitude > 0 || consec_zeromv < 4)) return COPY_BLOCK;
  189. // Avoid denoising small blocks. When noise > kDenLow or frame width > 480,
  190. // denoise 16x16 blocks.
  191. if (bs == BLOCK_8X8 || bs == BLOCK_8X16 || bs == BLOCK_16X8 ||
  192. (bs == BLOCK_16X16 && width > 480 &&
  193. denoiser->denoising_level <= kDenLow))
  194. return COPY_BLOCK;
  195. // If the best reference frame uses inter-prediction and there is enough of a
  196. // difference in sum-squared-error, use it.
  197. if (frame != INTRA_FRAME && frame != ALTREF_FRAME && frame != GOLDEN_FRAME &&
  198. sse_diff > sse_diff_thresh(bs, increase_denoising, motion_magnitude)) {
  199. mi->ref_frame[0] = ctx->best_reference_frame;
  200. mi->mode = ctx->best_sse_inter_mode;
  201. mi->mv[0] = ctx->best_sse_mv;
  202. } else {
  203. // Otherwise, use the zero reference frame.
  204. frame = ctx->best_zeromv_reference_frame;
  205. ctx->newmv_sse = ctx->zeromv_sse;
  206. // Bias to last reference.
  207. if ((num_spatial_layers > 1 && !use_gf_temporal_ref) ||
  208. frame == ALTREF_FRAME ||
  209. (frame == GOLDEN_FRAME && use_gf_temporal_ref) ||
  210. (frame != LAST_FRAME &&
  211. ((ctx->zeromv_lastref_sse<(5 * ctx->zeromv_sse)>> 2) ||
  212. denoiser->denoising_level >= kDenHigh))) {
  213. frame = LAST_FRAME;
  214. ctx->newmv_sse = ctx->zeromv_lastref_sse;
  215. }
  216. mi->ref_frame[0] = frame;
  217. mi->mode = ZEROMV;
  218. mi->mv[0].as_int = 0;
  219. ctx->best_sse_inter_mode = ZEROMV;
  220. ctx->best_sse_mv.as_int = 0;
  221. *zeromv_filter = 1;
  222. if (denoiser->denoising_level > kDenMedium) {
  223. motion_magnitude = 0;
  224. }
  225. }
  226. saved_frame = frame;
  227. // When using SVC, we need to map REF_FRAME to the frame buffer index.
  228. if (use_svc) {
  229. if (frame == LAST_FRAME)
  230. frame = lst_fb_idx + 1;
  231. else if (frame == GOLDEN_FRAME)
  232. frame = gld_fb_idx + 1;
  233. // Shift for the second spatial layer.
  234. if (num_spatial_layers - spatial_layer == 2)
  235. frame = frame + denoiser->num_ref_frames;
  236. denoise_layer_idx = num_spatial_layers - spatial_layer - 1;
  237. }
  238. // Force copy (no denoise, copy source in denoised buffer) if
  239. // running_avg_y[frame] is NULL.
  240. if (denoiser->running_avg_y[frame].buffer_alloc == NULL) {
  241. // Restore everything to its original state
  242. *mi = saved_mi;
  243. return COPY_BLOCK;
  244. }
  245. if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) {
  246. // Restore everything to its original state
  247. *mi = saved_mi;
  248. return COPY_BLOCK;
  249. }
  250. if (motion_magnitude > (noise_motion_thresh(bs, increase_denoising) << 3)) {
  251. // Restore everything to its original state
  252. *mi = saved_mi;
  253. return COPY_BLOCK;
  254. }
  255. // We will restore these after motion compensation.
  256. for (i = 0; i < MAX_MB_PLANE; ++i) {
  257. saved_pre[i] = filter_mbd->plane[i].pre[0];
  258. saved_dst[i] = filter_mbd->plane[i].dst;
  259. }
  260. saved_block_refs[0] = filter_mbd->block_refs[0];
  261. // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser
  262. // struct.
  263. filter_mbd->plane[0].pre[0].buf =
  264. block_start(denoiser->running_avg_y[frame].y_buffer,
  265. denoiser->running_avg_y[frame].y_stride, mi_row, mi_col);
  266. filter_mbd->plane[0].pre[0].stride = denoiser->running_avg_y[frame].y_stride;
  267. filter_mbd->plane[1].pre[0].buf =
  268. block_start(denoiser->running_avg_y[frame].u_buffer,
  269. denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
  270. filter_mbd->plane[1].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
  271. filter_mbd->plane[2].pre[0].buf =
  272. block_start(denoiser->running_avg_y[frame].v_buffer,
  273. denoiser->running_avg_y[frame].uv_stride, mi_row, mi_col);
  274. filter_mbd->plane[2].pre[0].stride = denoiser->running_avg_y[frame].uv_stride;
  275. filter_mbd->plane[0].dst.buf = block_start(
  276. denoiser->mc_running_avg_y[denoise_layer_idx].y_buffer,
  277. denoiser->mc_running_avg_y[denoise_layer_idx].y_stride, mi_row, mi_col);
  278. filter_mbd->plane[0].dst.stride =
  279. denoiser->mc_running_avg_y[denoise_layer_idx].y_stride;
  280. filter_mbd->plane[1].dst.buf = block_start(
  281. denoiser->mc_running_avg_y[denoise_layer_idx].u_buffer,
  282. denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride, mi_row, mi_col);
  283. filter_mbd->plane[1].dst.stride =
  284. denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride;
  285. filter_mbd->plane[2].dst.buf = block_start(
  286. denoiser->mc_running_avg_y[denoise_layer_idx].v_buffer,
  287. denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride, mi_row, mi_col);
  288. filter_mbd->plane[2].dst.stride =
  289. denoiser->mc_running_avg_y[denoise_layer_idx].uv_stride;
  290. set_ref_ptrs(cm, filter_mbd, saved_frame, NONE);
  291. vp9_build_inter_predictors_sby(filter_mbd, mi_row, mi_col, bs);
  292. // Restore everything to its original state
  293. *mi = saved_mi;
  294. filter_mbd->block_refs[0] = saved_block_refs[0];
  295. for (i = 0; i < MAX_MB_PLANE; ++i) {
  296. filter_mbd->plane[i].pre[0] = saved_pre[i];
  297. filter_mbd->plane[i].dst = saved_dst[i];
  298. }
  299. return FILTER_BLOCK;
  300. }
  301. void vp9_denoiser_denoise(VP9_COMP *cpi, MACROBLOCK *mb, int mi_row, int mi_col,
  302. BLOCK_SIZE bs, PICK_MODE_CONTEXT *ctx,
  303. VP9_DENOISER_DECISION *denoiser_decision,
  304. int use_gf_temporal_ref) {
  305. int mv_col, mv_row;
  306. int motion_magnitude = 0;
  307. int zeromv_filter = 0;
  308. VP9_DENOISER *denoiser = &cpi->denoiser;
  309. VP9_DENOISER_DECISION decision = COPY_BLOCK;
  310. const int shift =
  311. cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id == 2
  312. ? denoiser->num_ref_frames
  313. : 0;
  314. YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME + shift];
  315. const int denoise_layer_index =
  316. cpi->svc.number_spatial_layers - cpi->svc.spatial_layer_id - 1;
  317. YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y[denoise_layer_index];
  318. uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col);
  319. uint8_t *mc_avg_start =
  320. block_start(mc_avg.y_buffer, mc_avg.y_stride, mi_row, mi_col);
  321. struct buf_2d src = mb->plane[0].src;
  322. int is_skin = 0;
  323. int increase_denoising = 0;
  324. int consec_zeromv = 0;
  325. int last_is_reference = cpi->ref_frame_flags & VP9_LAST_FLAG;
  326. mv_col = ctx->best_sse_mv.as_mv.col;
  327. mv_row = ctx->best_sse_mv.as_mv.row;
  328. motion_magnitude = mv_row * mv_row + mv_col * mv_col;
  329. if (cpi->use_skin_detection && bs <= BLOCK_32X32 &&
  330. denoiser->denoising_level < kDenHigh) {
  331. int motion_level = (motion_magnitude < 16) ? 0 : 1;
  332. // If motion for current block is small/zero, compute consec_zeromv for
  333. // skin detection (early exit in skin detection is done for large
  334. // consec_zeromv when current block has small/zero motion).
  335. consec_zeromv = 0;
  336. if (motion_level == 0) {
  337. VP9_COMMON *const cm = &cpi->common;
  338. int j, i;
  339. // Loop through the 8x8 sub-blocks.
  340. const int bw = num_8x8_blocks_wide_lookup[bs];
  341. const int bh = num_8x8_blocks_high_lookup[bs];
  342. const int xmis = VPXMIN(cm->mi_cols - mi_col, bw);
  343. const int ymis = VPXMIN(cm->mi_rows - mi_row, bh);
  344. const int block_index = mi_row * cm->mi_cols + mi_col;
  345. consec_zeromv = 100;
  346. for (i = 0; i < ymis; i++) {
  347. for (j = 0; j < xmis; j++) {
  348. int bl_index = block_index + i * cm->mi_cols + j;
  349. consec_zeromv = VPXMIN(cpi->consec_zero_mv[bl_index], consec_zeromv);
  350. // No need to keep checking 8x8 blocks if any of the sub-blocks
  351. // has small consec_zeromv (since threshold for no_skin based on
  352. // zero/small motion in skin detection is high, i.e, > 4).
  353. if (consec_zeromv < 4) {
  354. i = ymis;
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. // TODO(marpan): Compute skin detection over sub-blocks.
  361. is_skin = vp9_compute_skin_block(
  362. mb->plane[0].src.buf, mb->plane[1].src.buf, mb->plane[2].src.buf,
  363. mb->plane[0].src.stride, mb->plane[1].src.stride, bs, consec_zeromv,
  364. motion_level);
  365. }
  366. if (!is_skin && denoiser->denoising_level == kDenHigh) increase_denoising = 1;
  367. // Copy block if LAST_FRAME is not a reference.
  368. // Last doesn't always exist when SVC layers are dynamically changed, e.g. top
  369. // spatial layer doesn't have last reference when it's brought up for the
  370. // first time on the fly.
  371. if (last_is_reference && denoiser->denoising_level >= kDenLow &&
  372. !ctx->sb_skip_denoising)
  373. decision = perform_motion_compensation(
  374. &cpi->common, denoiser, mb, bs, increase_denoising, mi_row, mi_col, ctx,
  375. motion_magnitude, is_skin, &zeromv_filter, consec_zeromv,
  376. cpi->svc.number_spatial_layers, cpi->Source->y_width, cpi->lst_fb_idx,
  377. cpi->gld_fb_idx, cpi->use_svc, cpi->svc.spatial_layer_id,
  378. use_gf_temporal_ref);
  379. if (decision == FILTER_BLOCK) {
  380. decision = vp9_denoiser_filter(src.buf, src.stride, mc_avg_start,
  381. mc_avg.y_stride, avg_start, avg.y_stride,
  382. increase_denoising, bs, motion_magnitude);
  383. }
  384. if (decision == FILTER_BLOCK) {
  385. vpx_convolve_copy(avg_start, avg.y_stride, src.buf, src.stride, NULL, 0, 0,
  386. 0, 0, num_4x4_blocks_wide_lookup[bs] << 2,
  387. num_4x4_blocks_high_lookup[bs] << 2);
  388. } else { // COPY_BLOCK
  389. vpx_convolve_copy(src.buf, src.stride, avg_start, avg.y_stride, NULL, 0, 0,
  390. 0, 0, num_4x4_blocks_wide_lookup[bs] << 2,
  391. num_4x4_blocks_high_lookup[bs] << 2);
  392. }
  393. *denoiser_decision = decision;
  394. if (decision == FILTER_BLOCK && zeromv_filter == 1)
  395. *denoiser_decision = FILTER_ZEROMV_BLOCK;
  396. }
  397. static void copy_frame(YV12_BUFFER_CONFIG *const dest,
  398. const YV12_BUFFER_CONFIG *const src) {
  399. int r;
  400. const uint8_t *srcbuf = src->y_buffer;
  401. uint8_t *destbuf = dest->y_buffer;
  402. assert(dest->y_width == src->y_width);
  403. assert(dest->y_height == src->y_height);
  404. for (r = 0; r < dest->y_height; ++r) {
  405. memcpy(destbuf, srcbuf, dest->y_width);
  406. destbuf += dest->y_stride;
  407. srcbuf += src->y_stride;
  408. }
  409. }
  410. static void swap_frame_buffer(YV12_BUFFER_CONFIG *const dest,
  411. YV12_BUFFER_CONFIG *const src) {
  412. uint8_t *tmp_buf = dest->y_buffer;
  413. assert(dest->y_width == src->y_width);
  414. assert(dest->y_height == src->y_height);
  415. dest->y_buffer = src->y_buffer;
  416. src->y_buffer = tmp_buf;
  417. }
  418. void vp9_denoiser_update_frame_info(
  419. VP9_DENOISER *denoiser, YV12_BUFFER_CONFIG src, struct SVC *svc,
  420. FRAME_TYPE frame_type, int refresh_alt_ref_frame, int refresh_golden_frame,
  421. int refresh_last_frame, int alt_fb_idx, int gld_fb_idx, int lst_fb_idx,
  422. int resized, int svc_refresh_denoiser_buffers, int second_spatial_layer) {
  423. const int shift = second_spatial_layer ? denoiser->num_ref_frames : 0;
  424. // Copy source into denoised reference buffers on KEY_FRAME or
  425. // if the just encoded frame was resized. For SVC, copy source if the base
  426. // spatial layer was key frame.
  427. if (frame_type == KEY_FRAME || resized != 0 || denoiser->reset ||
  428. svc_refresh_denoiser_buffers) {
  429. int i;
  430. // Start at 1 so as not to overwrite the INTRA_FRAME
  431. for (i = 1; i < denoiser->num_ref_frames; ++i) {
  432. if (denoiser->running_avg_y[i + shift].buffer_alloc != NULL)
  433. copy_frame(&denoiser->running_avg_y[i + shift], &src);
  434. }
  435. denoiser->reset = 0;
  436. return;
  437. }
  438. if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  439. svc->use_set_ref_frame_config) {
  440. int i;
  441. for (i = 0; i < REF_FRAMES; i++) {
  442. if (svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i))
  443. copy_frame(&denoiser->running_avg_y[i + 1 + shift],
  444. &denoiser->running_avg_y[INTRA_FRAME + shift]);
  445. }
  446. } else {
  447. // If more than one refresh occurs, must copy frame buffer.
  448. if ((refresh_alt_ref_frame + refresh_golden_frame + refresh_last_frame) >
  449. 1) {
  450. if (refresh_alt_ref_frame) {
  451. copy_frame(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
  452. &denoiser->running_avg_y[INTRA_FRAME + shift]);
  453. }
  454. if (refresh_golden_frame) {
  455. copy_frame(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
  456. &denoiser->running_avg_y[INTRA_FRAME + shift]);
  457. }
  458. if (refresh_last_frame) {
  459. copy_frame(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
  460. &denoiser->running_avg_y[INTRA_FRAME + shift]);
  461. }
  462. } else {
  463. if (refresh_alt_ref_frame) {
  464. swap_frame_buffer(&denoiser->running_avg_y[alt_fb_idx + 1 + shift],
  465. &denoiser->running_avg_y[INTRA_FRAME + shift]);
  466. }
  467. if (refresh_golden_frame) {
  468. swap_frame_buffer(&denoiser->running_avg_y[gld_fb_idx + 1 + shift],
  469. &denoiser->running_avg_y[INTRA_FRAME + shift]);
  470. }
  471. if (refresh_last_frame) {
  472. swap_frame_buffer(&denoiser->running_avg_y[lst_fb_idx + 1 + shift],
  473. &denoiser->running_avg_y[INTRA_FRAME + shift]);
  474. }
  475. }
  476. }
  477. }
  478. void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) {
  479. ctx->zeromv_sse = UINT_MAX;
  480. ctx->newmv_sse = UINT_MAX;
  481. ctx->zeromv_lastref_sse = UINT_MAX;
  482. ctx->best_sse_mv.as_int = 0;
  483. }
  484. void vp9_denoiser_update_frame_stats(MODE_INFO *mi, unsigned int sse,
  485. PREDICTION_MODE mode,
  486. PICK_MODE_CONTEXT *ctx) {
  487. if (mi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) {
  488. ctx->zeromv_sse = sse;
  489. ctx->best_zeromv_reference_frame = mi->ref_frame[0];
  490. if (mi->ref_frame[0] == LAST_FRAME) ctx->zeromv_lastref_sse = sse;
  491. }
  492. if (mi->mv[0].as_int != 0 && sse < ctx->newmv_sse) {
  493. ctx->newmv_sse = sse;
  494. ctx->best_sse_inter_mode = mode;
  495. ctx->best_sse_mv = mi->mv[0];
  496. ctx->best_reference_frame = mi->ref_frame[0];
  497. }
  498. }
  499. static int vp9_denoiser_realloc_svc_helper(VP9_COMMON *cm,
  500. VP9_DENOISER *denoiser, int fb_idx) {
  501. int fail = 0;
  502. if (denoiser->running_avg_y[fb_idx].buffer_alloc == NULL) {
  503. fail =
  504. vpx_alloc_frame_buffer(&denoiser->running_avg_y[fb_idx], cm->width,
  505. cm->height, cm->subsampling_x, cm->subsampling_y,
  506. #if CONFIG_VP9_HIGHBITDEPTH
  507. cm->use_highbitdepth,
  508. #endif
  509. VP9_ENC_BORDER_IN_PIXELS, 0);
  510. if (fail) {
  511. vp9_denoiser_free(denoiser);
  512. return 1;
  513. }
  514. }
  515. return 0;
  516. }
  517. int vp9_denoiser_realloc_svc(VP9_COMMON *cm, VP9_DENOISER *denoiser,
  518. struct SVC *svc, int svc_buf_shift,
  519. int refresh_alt, int refresh_gld, int refresh_lst,
  520. int alt_fb_idx, int gld_fb_idx, int lst_fb_idx) {
  521. int fail = 0;
  522. if (svc->temporal_layering_mode == VP9E_TEMPORAL_LAYERING_MODE_BYPASS &&
  523. svc->use_set_ref_frame_config) {
  524. int i;
  525. for (i = 0; i < REF_FRAMES; i++) {
  526. if (cm->frame_type == KEY_FRAME ||
  527. svc->update_buffer_slot[svc->spatial_layer_id] & (1 << i)) {
  528. fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
  529. i + 1 + svc_buf_shift);
  530. }
  531. }
  532. } else {
  533. if (refresh_alt) {
  534. // Increase the frame buffer index by 1 to map it to the buffer index in
  535. // the denoiser.
  536. fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
  537. alt_fb_idx + 1 + svc_buf_shift);
  538. if (fail) return 1;
  539. }
  540. if (refresh_gld) {
  541. fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
  542. gld_fb_idx + 1 + svc_buf_shift);
  543. if (fail) return 1;
  544. }
  545. if (refresh_lst) {
  546. fail = vp9_denoiser_realloc_svc_helper(cm, denoiser,
  547. lst_fb_idx + 1 + svc_buf_shift);
  548. if (fail) return 1;
  549. }
  550. }
  551. return 0;
  552. }
  553. int vp9_denoiser_alloc(VP9_COMMON *cm, struct SVC *svc, VP9_DENOISER *denoiser,
  554. int use_svc, int noise_sen, int width, int height,
  555. int ssx, int ssy,
  556. #if CONFIG_VP9_HIGHBITDEPTH
  557. int use_highbitdepth,
  558. #endif
  559. int border) {
  560. int i, layer, fail, init_num_ref_frames;
  561. const int legacy_byte_alignment = 0;
  562. int num_layers = 1;
  563. int scaled_width = width;
  564. int scaled_height = height;
  565. if (use_svc) {
  566. LAYER_CONTEXT *lc = &svc->layer_context[svc->spatial_layer_id *
  567. svc->number_temporal_layers +
  568. svc->temporal_layer_id];
  569. get_layer_resolution(width, height, lc->scaling_factor_num,
  570. lc->scaling_factor_den, &scaled_width, &scaled_height);
  571. // For SVC: only denoise at most 2 spatial (highest) layers.
  572. if (noise_sen >= 2)
  573. // Denoise from one spatial layer below the top.
  574. svc->first_layer_denoise = VPXMAX(svc->number_spatial_layers - 2, 0);
  575. else
  576. // Only denoise the top spatial layer.
  577. svc->first_layer_denoise = VPXMAX(svc->number_spatial_layers - 1, 0);
  578. num_layers = svc->number_spatial_layers - svc->first_layer_denoise;
  579. }
  580. assert(denoiser != NULL);
  581. denoiser->num_ref_frames = use_svc ? SVC_REF_FRAMES : NONSVC_REF_FRAMES;
  582. init_num_ref_frames = use_svc ? MAX_REF_FRAMES : NONSVC_REF_FRAMES;
  583. denoiser->num_layers = num_layers;
  584. CHECK_MEM_ERROR(cm, denoiser->running_avg_y,
  585. vpx_calloc(denoiser->num_ref_frames * num_layers,
  586. sizeof(denoiser->running_avg_y[0])));
  587. CHECK_MEM_ERROR(
  588. cm, denoiser->mc_running_avg_y,
  589. vpx_calloc(num_layers, sizeof(denoiser->mc_running_avg_y[0])));
  590. for (layer = 0; layer < num_layers; ++layer) {
  591. const int denoise_width = (layer == 0) ? width : scaled_width;
  592. const int denoise_height = (layer == 0) ? height : scaled_height;
  593. for (i = 0; i < init_num_ref_frames; ++i) {
  594. fail = vpx_alloc_frame_buffer(
  595. &denoiser->running_avg_y[i + denoiser->num_ref_frames * layer],
  596. denoise_width, denoise_height, ssx, ssy,
  597. #if CONFIG_VP9_HIGHBITDEPTH
  598. use_highbitdepth,
  599. #endif
  600. border, legacy_byte_alignment);
  601. if (fail) {
  602. vp9_denoiser_free(denoiser);
  603. return 1;
  604. }
  605. #ifdef OUTPUT_YUV_DENOISED
  606. make_grayscale(&denoiser->running_avg_y[i]);
  607. #endif
  608. }
  609. fail = vpx_alloc_frame_buffer(&denoiser->mc_running_avg_y[layer],
  610. denoise_width, denoise_height, ssx, ssy,
  611. #if CONFIG_VP9_HIGHBITDEPTH
  612. use_highbitdepth,
  613. #endif
  614. border, legacy_byte_alignment);
  615. if (fail) {
  616. vp9_denoiser_free(denoiser);
  617. return 1;
  618. }
  619. }
  620. // denoiser->last_source only used for noise_estimation, so only for top
  621. // layer.
  622. fail = vpx_alloc_frame_buffer(&denoiser->last_source, width, height, ssx, ssy,
  623. #if CONFIG_VP9_HIGHBITDEPTH
  624. use_highbitdepth,
  625. #endif
  626. border, legacy_byte_alignment);
  627. if (fail) {
  628. vp9_denoiser_free(denoiser);
  629. return 1;
  630. }
  631. #ifdef OUTPUT_YUV_DENOISED
  632. make_grayscale(&denoiser->running_avg_y[i]);
  633. #endif
  634. denoiser->frame_buffer_initialized = 1;
  635. denoiser->denoising_level = kDenMedium;
  636. denoiser->prev_denoising_level = kDenMedium;
  637. denoiser->reset = 0;
  638. denoiser->current_denoiser_frame = 0;
  639. return 0;
  640. }
  641. void vp9_denoiser_free(VP9_DENOISER *denoiser) {
  642. int i;
  643. if (denoiser == NULL) {
  644. return;
  645. }
  646. denoiser->frame_buffer_initialized = 0;
  647. for (i = 0; i < denoiser->num_ref_frames * denoiser->num_layers; ++i) {
  648. vpx_free_frame_buffer(&denoiser->running_avg_y[i]);
  649. }
  650. vpx_free(denoiser->running_avg_y);
  651. denoiser->running_avg_y = NULL;
  652. for (i = 0; i < denoiser->num_layers; ++i) {
  653. vpx_free_frame_buffer(&denoiser->mc_running_avg_y[i]);
  654. }
  655. vpx_free(denoiser->mc_running_avg_y);
  656. denoiser->mc_running_avg_y = NULL;
  657. vpx_free_frame_buffer(&denoiser->last_source);
  658. }
  659. static void force_refresh_longterm_ref(VP9_COMP *const cpi) {
  660. SVC *const svc = &cpi->svc;
  661. // If long term reference is used, force refresh of that slot, so
  662. // denoiser buffer for long term reference stays in sync.
  663. if (svc->use_gf_temporal_ref_current_layer) {
  664. int index = svc->spatial_layer_id;
  665. if (svc->number_spatial_layers == 3) index = svc->spatial_layer_id - 1;
  666. assert(index >= 0);
  667. cpi->alt_fb_idx = svc->buffer_gf_temporal_ref[index].idx;
  668. cpi->refresh_alt_ref_frame = 1;
  669. }
  670. }
  671. void vp9_denoiser_set_noise_level(VP9_COMP *const cpi, int noise_level) {
  672. VP9_DENOISER *const denoiser = &cpi->denoiser;
  673. denoiser->denoising_level = noise_level;
  674. if (denoiser->denoising_level > kDenLowLow &&
  675. denoiser->prev_denoising_level == kDenLowLow) {
  676. denoiser->reset = 1;
  677. force_refresh_longterm_ref(cpi);
  678. } else {
  679. denoiser->reset = 0;
  680. }
  681. denoiser->prev_denoising_level = denoiser->denoising_level;
  682. }
  683. // Scale/increase the partition threshold
  684. // for denoiser speed-up.
  685. int64_t vp9_scale_part_thresh(int64_t threshold, VP9_DENOISER_LEVEL noise_level,
  686. int content_state, int temporal_layer_id) {
  687. if ((content_state == kLowSadLowSumdiff) ||
  688. (content_state == kHighSadLowSumdiff) ||
  689. (content_state == kLowVarHighSumdiff) || (noise_level == kDenHigh) ||
  690. (temporal_layer_id != 0)) {
  691. int64_t scaled_thr =
  692. (temporal_layer_id < 2) ? (3 * threshold) >> 1 : (7 * threshold) >> 2;
  693. return scaled_thr;
  694. } else {
  695. return (5 * threshold) >> 2;
  696. }
  697. }
  698. // Scale/increase the ac skip threshold for
  699. // denoiser speed-up.
  700. int64_t vp9_scale_acskip_thresh(int64_t threshold,
  701. VP9_DENOISER_LEVEL noise_level, int abs_sumdiff,
  702. int temporal_layer_id) {
  703. if (noise_level >= kDenLow && abs_sumdiff < 5)
  704. return threshold *=
  705. (noise_level == kDenLow) ? 2 : (temporal_layer_id == 2) ? 10 : 6;
  706. else
  707. return threshold;
  708. }
  709. void vp9_denoiser_reset_on_first_frame(VP9_COMP *const cpi) {
  710. if (vp9_denoise_svc_non_key(cpi) &&
  711. cpi->denoiser.current_denoiser_frame == 0) {
  712. cpi->denoiser.reset = 1;
  713. force_refresh_longterm_ref(cpi);
  714. }
  715. }
  716. void vp9_denoiser_update_ref_frame(VP9_COMP *const cpi) {
  717. VP9_COMMON *const cm = &cpi->common;
  718. SVC *const svc = &cpi->svc;
  719. if (cpi->oxcf.noise_sensitivity > 0 && denoise_svc(cpi) &&
  720. cpi->denoiser.denoising_level > kDenLowLow) {
  721. int svc_refresh_denoiser_buffers = 0;
  722. int denoise_svc_second_layer = 0;
  723. FRAME_TYPE frame_type = cm->intra_only ? KEY_FRAME : cm->frame_type;
  724. cpi->denoiser.current_denoiser_frame++;
  725. if (cpi->use_svc) {
  726. const int svc_buf_shift =
  727. svc->number_spatial_layers - svc->spatial_layer_id == 2
  728. ? cpi->denoiser.num_ref_frames
  729. : 0;
  730. int layer =
  731. LAYER_IDS_TO_IDX(svc->spatial_layer_id, svc->temporal_layer_id,
  732. svc->number_temporal_layers);
  733. LAYER_CONTEXT *const lc = &svc->layer_context[layer];
  734. svc_refresh_denoiser_buffers =
  735. lc->is_key_frame || svc->spatial_layer_sync[svc->spatial_layer_id];
  736. denoise_svc_second_layer =
  737. svc->number_spatial_layers - svc->spatial_layer_id == 2 ? 1 : 0;
  738. // Check if we need to allocate extra buffers in the denoiser
  739. // for refreshed frames.
  740. if (vp9_denoiser_realloc_svc(cm, &cpi->denoiser, svc, svc_buf_shift,
  741. cpi->refresh_alt_ref_frame,
  742. cpi->refresh_golden_frame,
  743. cpi->refresh_last_frame, cpi->alt_fb_idx,
  744. cpi->gld_fb_idx, cpi->lst_fb_idx))
  745. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  746. "Failed to re-allocate denoiser for SVC");
  747. }
  748. vp9_denoiser_update_frame_info(
  749. &cpi->denoiser, *cpi->Source, svc, frame_type,
  750. cpi->refresh_alt_ref_frame, cpi->refresh_golden_frame,
  751. cpi->refresh_last_frame, cpi->alt_fb_idx, cpi->gld_fb_idx,
  752. cpi->lst_fb_idx, cpi->resize_pending, svc_refresh_denoiser_buffers,
  753. denoise_svc_second_layer);
  754. }
  755. }
  756. #ifdef OUTPUT_YUV_DENOISED
  757. static void make_grayscale(YV12_BUFFER_CONFIG *yuv) {
  758. int r, c;
  759. uint8_t *u = yuv->u_buffer;
  760. uint8_t *v = yuv->v_buffer;
  761. for (r = 0; r < yuv->uv_height; ++r) {
  762. for (c = 0; c < yuv->uv_width; ++c) {
  763. u[c] = UINT8_MAX / 2;
  764. v[c] = UINT8_MAX / 2;
  765. }
  766. u += yuv->uv_stride;
  767. v += yuv->uv_stride;
  768. }
  769. }
  770. #endif