OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <assert.h> | 11 #include <assert.h> |
12 #include <limits.h> | 12 #include <limits.h> |
13 #include "vpx_scale/yv12config.h" | 13 #include "vpx_scale/yv12config.h" |
14 #include "vpx/vpx_integer.h" | 14 #include "vpx/vpx_integer.h" |
15 #include "vp9/common/vp9_reconinter.h" | 15 #include "vp9/common/vp9_reconinter.h" |
| 16 #include "vp9/encoder/vp9_context_tree.h" |
16 #include "vp9/encoder/vp9_denoiser.h" | 17 #include "vp9/encoder/vp9_denoiser.h" |
17 | 18 |
18 /* The VP9 denoiser is a work-in-progress. It currently is only designed to work | 19 /* The VP9 denoiser is a work-in-progress. It currently is only designed to work |
19 * with speed 6, though it (inexplicably) seems to also work with speed 5 (one | 20 * with speed 6, though it (inexplicably) seems to also work with speed 5 (one |
20 * would need to modify the source code in vp9_pickmode.c and vp9_encoder.c to | 21 * would need to modify the source code in vp9_pickmode.c and vp9_encoder.c to |
21 * make the calls to the vp9_denoiser_* functions when in speed 5). | 22 * make the calls to the vp9_denoiser_* functions when in speed 5). |
22 * | 23 * |
23 * The implementation is very similar to that of the VP8 denoiser. While | 24 * The implementation is very similar to that of the VP8 denoiser. While |
24 * choosing the motion vectors / reference frames, the denoiser is run, and if | 25 * choosing the motion vectors / reference frames, the denoiser is run, and if |
25 * it did not modify the signal to much, the denoised block is copied to the | 26 * it did not modify the signal to much, the denoised block is copied to the |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 dest += dest_stride; | 177 dest += dest_stride; |
177 src += src_stride; | 178 src += src_stride; |
178 } | 179 } |
179 } | 180 } |
180 | 181 |
181 static VP9_DENOISER_DECISION perform_motion_compensation(VP9_DENOISER *denoiser, | 182 static VP9_DENOISER_DECISION perform_motion_compensation(VP9_DENOISER *denoiser, |
182 MACROBLOCK *mb, | 183 MACROBLOCK *mb, |
183 BLOCK_SIZE bs, | 184 BLOCK_SIZE bs, |
184 int increase_denoising, | 185 int increase_denoising, |
185 int mi_row, | 186 int mi_row, |
186 int mi_col) { | 187 int mi_col, |
| 188 PICK_MODE_CONTEXT *ctx |
| 189 ) { |
187 int mv_col, mv_row; | 190 int mv_col, mv_row; |
188 int sse_diff = denoiser->zero_mv_sse - denoiser->best_sse; | 191 int sse_diff = ctx->zeromv_sse - ctx->newmv_sse; |
189 MV_REFERENCE_FRAME frame; | 192 MV_REFERENCE_FRAME frame; |
190 MACROBLOCKD *filter_mbd = &mb->e_mbd; | 193 MACROBLOCKD *filter_mbd = &mb->e_mbd; |
191 MB_MODE_INFO *mbmi = &filter_mbd->mi[0]->mbmi; | 194 MB_MODE_INFO *mbmi = &filter_mbd->mi[0]->mbmi; |
192 | 195 |
193 MB_MODE_INFO saved_mbmi; | 196 MB_MODE_INFO saved_mbmi; |
194 int i, j; | 197 int i, j; |
195 struct buf_2d saved_dst[MAX_MB_PLANE]; | 198 struct buf_2d saved_dst[MAX_MB_PLANE]; |
196 struct buf_2d saved_pre[MAX_MB_PLANE][2]; // 2 pre buffers | 199 struct buf_2d saved_pre[MAX_MB_PLANE][2]; // 2 pre buffers |
197 | 200 |
198 // We will restore these after motion compensation. | 201 // We will restore these after motion compensation. |
199 saved_mbmi = *mbmi; | 202 saved_mbmi = *mbmi; |
200 for (i = 0; i < MAX_MB_PLANE; ++i) { | 203 for (i = 0; i < MAX_MB_PLANE; ++i) { |
201 for (j = 0; j < 2; ++j) { | 204 for (j = 0; j < 2; ++j) { |
202 saved_pre[i][j] = filter_mbd->plane[i].pre[j]; | 205 saved_pre[i][j] = filter_mbd->plane[i].pre[j]; |
203 } | 206 } |
204 saved_dst[i] = filter_mbd->plane[i].dst; | 207 saved_dst[i] = filter_mbd->plane[i].dst; |
205 } | 208 } |
206 | 209 |
207 mv_col = denoiser->best_sse_mv.as_mv.col; | 210 mv_col = ctx->best_sse_mv.as_mv.col; |
208 mv_row = denoiser->best_sse_mv.as_mv.row; | 211 mv_row = ctx->best_sse_mv.as_mv.row; |
209 | 212 |
210 frame = denoiser->best_reference_frame; | 213 frame = ctx->best_reference_frame; |
211 | 214 |
212 // If the best reference frame uses inter-prediction and there is enough of a | 215 // If the best reference frame uses inter-prediction and there is enough of a |
213 // difference in sum-squared-error, use it. | 216 // difference in sum-squared-error, use it. |
214 if (frame != INTRA_FRAME && | 217 if (frame != INTRA_FRAME && |
215 sse_diff > sse_diff_thresh(bs, increase_denoising, mv_row, mv_col)) { | 218 sse_diff > sse_diff_thresh(bs, increase_denoising, mv_row, mv_col)) { |
216 mbmi->ref_frame[0] = denoiser->best_reference_frame; | 219 mbmi->ref_frame[0] = ctx->best_reference_frame; |
217 mbmi->mode = denoiser->best_sse_inter_mode; | 220 mbmi->mode = ctx->best_sse_inter_mode; |
218 mbmi->mv[0] = denoiser->best_sse_mv; | 221 mbmi->mv[0] = ctx->best_sse_mv; |
219 } else { | 222 } else { |
220 // Otherwise, use the zero reference frame. | 223 // Otherwise, use the zero reference frame. |
221 frame = denoiser->best_zeromv_reference_frame; | 224 frame = ctx->best_zeromv_reference_frame; |
222 | 225 |
223 mbmi->ref_frame[0] = denoiser->best_zeromv_reference_frame; | 226 mbmi->ref_frame[0] = ctx->best_zeromv_reference_frame; |
224 mbmi->mode = ZEROMV; | 227 mbmi->mode = ZEROMV; |
225 mbmi->mv[0].as_int = 0; | 228 mbmi->mv[0].as_int = 0; |
226 | 229 |
227 denoiser->best_sse_inter_mode = ZEROMV; | 230 ctx->best_sse_inter_mode = ZEROMV; |
228 denoiser->best_sse_mv.as_int = 0; | 231 ctx->best_sse_mv.as_int = 0; |
229 denoiser->best_sse = denoiser->zero_mv_sse; | 232 ctx->newmv_sse = ctx->zeromv_sse; |
230 } | 233 } |
231 | 234 |
232 // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser | 235 // Set the pointers in the MACROBLOCKD to point to the buffers in the denoiser |
233 // struct. | 236 // struct. |
234 for (j = 0; j < 2; ++j) { | 237 for (j = 0; j < 2; ++j) { |
235 filter_mbd->plane[0].pre[j].buf = | 238 filter_mbd->plane[0].pre[j].buf = |
236 block_start(denoiser->running_avg_y[frame].y_buffer, | 239 block_start(denoiser->running_avg_y[frame].y_buffer, |
237 denoiser->running_avg_y[frame].y_stride, | 240 denoiser->running_avg_y[frame].y_stride, |
238 mi_row, mi_col); | 241 mi_row, mi_col); |
239 filter_mbd->plane[0].pre[j].stride = | 242 filter_mbd->plane[0].pre[j].stride = |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 | 274 |
272 // Restore everything to its original state | 275 // Restore everything to its original state |
273 *mbmi = saved_mbmi; | 276 *mbmi = saved_mbmi; |
274 for (i = 0; i < MAX_MB_PLANE; ++i) { | 277 for (i = 0; i < MAX_MB_PLANE; ++i) { |
275 for (j = 0; j < 2; ++j) { | 278 for (j = 0; j < 2; ++j) { |
276 filter_mbd->plane[i].pre[j] = saved_pre[i][j]; | 279 filter_mbd->plane[i].pre[j] = saved_pre[i][j]; |
277 } | 280 } |
278 filter_mbd->plane[i].dst = saved_dst[i]; | 281 filter_mbd->plane[i].dst = saved_dst[i]; |
279 } | 282 } |
280 | 283 |
281 mv_row = denoiser->best_sse_mv.as_mv.row; | 284 mv_row = ctx->best_sse_mv.as_mv.row; |
282 mv_col = denoiser->best_sse_mv.as_mv.col; | 285 mv_col = ctx->best_sse_mv.as_mv.col; |
283 | 286 |
284 if (denoiser->best_sse > sse_thresh(bs, increase_denoising)) { | 287 if (ctx->newmv_sse > sse_thresh(bs, increase_denoising)) { |
285 return COPY_BLOCK; | 288 return COPY_BLOCK; |
286 } | 289 } |
287 if (mv_row * mv_row + mv_col * mv_col > | 290 if (mv_row * mv_row + mv_col * mv_col > |
288 8 * noise_motion_thresh(bs, increase_denoising)) { | 291 8 * noise_motion_thresh(bs, increase_denoising)) { |
289 return COPY_BLOCK; | 292 return COPY_BLOCK; |
290 } | 293 } |
291 return FILTER_BLOCK; | 294 return FILTER_BLOCK; |
292 } | 295 } |
293 | 296 |
294 void vp9_denoiser_denoise(VP9_DENOISER *denoiser, MACROBLOCK *mb, | 297 void vp9_denoiser_denoise(VP9_DENOISER *denoiser, MACROBLOCK *mb, |
295 int mi_row, int mi_col, BLOCK_SIZE bs) { | 298 int mi_row, int mi_col, BLOCK_SIZE bs, |
| 299 PICK_MODE_CONTEXT *ctx) { |
296 VP9_DENOISER_DECISION decision = FILTER_BLOCK; | 300 VP9_DENOISER_DECISION decision = FILTER_BLOCK; |
297 YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME]; | 301 YV12_BUFFER_CONFIG avg = denoiser->running_avg_y[INTRA_FRAME]; |
298 YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y; | 302 YV12_BUFFER_CONFIG mc_avg = denoiser->mc_running_avg_y; |
299 uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col); | 303 uint8_t *avg_start = block_start(avg.y_buffer, avg.y_stride, mi_row, mi_col); |
300 uint8_t *mc_avg_start = block_start(mc_avg.y_buffer, mc_avg.y_stride, | 304 uint8_t *mc_avg_start = block_start(mc_avg.y_buffer, mc_avg.y_stride, |
301 mi_row, mi_col); | 305 mi_row, mi_col); |
302 struct buf_2d src = mb->plane[0].src; | 306 struct buf_2d src = mb->plane[0].src; |
303 | 307 |
304 decision = perform_motion_compensation(denoiser, mb, bs, | 308 decision = perform_motion_compensation(denoiser, mb, bs, |
305 denoiser->increase_denoising, | 309 denoiser->increase_denoising, |
306 mi_row, mi_col); | 310 mi_row, mi_col, ctx); |
307 | 311 |
308 if (decision == FILTER_BLOCK) { | 312 if (decision == FILTER_BLOCK) { |
309 decision = denoiser_filter(src.buf, src.stride, | 313 decision = denoiser_filter(src.buf, src.stride, |
310 mc_avg_start, mc_avg.y_stride, | 314 mc_avg_start, mc_avg.y_stride, |
311 avg_start, avg.y_stride, | 315 avg_start, avg.y_stride, |
312 0, bs); | 316 0, bs); |
313 } | 317 } |
314 | 318 |
315 if (decision == FILTER_BLOCK) { | 319 if (decision == FILTER_BLOCK) { |
316 copy_block(src.buf, src.stride, avg_start, avg.y_stride, bs); | 320 copy_block(src.buf, src.stride, avg_start, avg.y_stride, bs); |
(...skipping 17 matching lines...) Expand all Loading... |
334 } | 338 } |
335 | 339 |
336 void vp9_denoiser_update_frame_info(VP9_DENOISER *denoiser, | 340 void vp9_denoiser_update_frame_info(VP9_DENOISER *denoiser, |
337 YV12_BUFFER_CONFIG src, | 341 YV12_BUFFER_CONFIG src, |
338 FRAME_TYPE frame_type, | 342 FRAME_TYPE frame_type, |
339 int refresh_alt_ref_frame, | 343 int refresh_alt_ref_frame, |
340 int refresh_golden_frame, | 344 int refresh_golden_frame, |
341 int refresh_last_frame) { | 345 int refresh_last_frame) { |
342 if (frame_type == KEY_FRAME) { | 346 if (frame_type == KEY_FRAME) { |
343 int i; | 347 int i; |
344 copy_frame(denoiser->running_avg_y[LAST_FRAME], src); | 348 // Start at 1 so as not to overwrite the INTRA_FRAME |
345 for (i = 2; i < MAX_REF_FRAMES - 1; i++) { | 349 for (i = 1; i < MAX_REF_FRAMES; ++i) { |
346 copy_frame(denoiser->running_avg_y[i], | 350 copy_frame(denoiser->running_avg_y[i], src); |
347 denoiser->running_avg_y[LAST_FRAME]); | |
348 } | 351 } |
349 } else { /* For non key frames */ | 352 } else { /* For non key frames */ |
350 if (refresh_alt_ref_frame) { | 353 if (refresh_alt_ref_frame) { |
351 copy_frame(denoiser->running_avg_y[ALTREF_FRAME], | 354 copy_frame(denoiser->running_avg_y[ALTREF_FRAME], |
352 denoiser->running_avg_y[INTRA_FRAME]); | 355 denoiser->running_avg_y[INTRA_FRAME]); |
353 } | 356 } |
354 if (refresh_golden_frame) { | 357 if (refresh_golden_frame) { |
355 copy_frame(denoiser->running_avg_y[GOLDEN_FRAME], | 358 copy_frame(denoiser->running_avg_y[GOLDEN_FRAME], |
356 denoiser->running_avg_y[INTRA_FRAME]); | 359 denoiser->running_avg_y[INTRA_FRAME]); |
357 } | 360 } |
358 if (refresh_last_frame) { | 361 if (refresh_last_frame) { |
359 copy_frame(denoiser->running_avg_y[LAST_FRAME], | 362 copy_frame(denoiser->running_avg_y[LAST_FRAME], |
360 denoiser->running_avg_y[INTRA_FRAME]); | 363 denoiser->running_avg_y[INTRA_FRAME]); |
361 } | 364 } |
362 } | 365 } |
363 } | 366 } |
364 | 367 |
365 void vp9_denoiser_reset_frame_stats(VP9_DENOISER *denoiser) { | 368 void vp9_denoiser_reset_frame_stats(PICK_MODE_CONTEXT *ctx) { |
366 denoiser->zero_mv_sse = UINT_MAX; | 369 ctx->zeromv_sse = UINT_MAX; |
367 denoiser->best_sse = UINT_MAX; | 370 ctx->newmv_sse = UINT_MAX; |
368 } | 371 } |
369 | 372 |
370 void vp9_denoiser_update_frame_stats(VP9_DENOISER *denoiser, MB_MODE_INFO *mbmi, | 373 void vp9_denoiser_update_frame_stats(VP9_DENOISER *denoiser, MB_MODE_INFO *mbmi, |
371 unsigned int sse, PREDICTION_MODE mode) { | 374 unsigned int sse, PREDICTION_MODE mode, |
| 375 PICK_MODE_CONTEXT *ctx) { |
372 // TODO(tkopp): Use both MVs if possible | 376 // TODO(tkopp): Use both MVs if possible |
373 if (mbmi->mv[0].as_int == 0 && sse < denoiser->zero_mv_sse) { | 377 if (mbmi->mv[0].as_int == 0 && sse < ctx->zeromv_sse) { |
374 denoiser->zero_mv_sse = sse; | 378 ctx->zeromv_sse = sse; |
375 denoiser->best_zeromv_reference_frame = mbmi->ref_frame[0]; | 379 ctx->best_zeromv_reference_frame = mbmi->ref_frame[0]; |
376 } | 380 } |
377 | 381 |
378 if (mbmi->mv[0].as_int != 0 && sse < denoiser->best_sse) { | 382 if (mode == NEWMV) { |
379 denoiser->best_sse = sse; | 383 ctx->newmv_sse = sse; |
380 denoiser->best_sse_inter_mode = mode; | 384 ctx->best_sse_inter_mode = mode; |
381 denoiser->best_sse_mv = mbmi->mv[0]; | 385 ctx->best_sse_mv = mbmi->mv[0]; |
382 denoiser->best_reference_frame = mbmi->ref_frame[0]; | 386 ctx->best_reference_frame = mbmi->ref_frame[0]; |
383 } | 387 } |
384 } | 388 } |
385 | 389 |
386 int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height, | 390 int vp9_denoiser_alloc(VP9_DENOISER *denoiser, int width, int height, |
387 int ssx, int ssy, int border) { | 391 int ssx, int ssy, int border) { |
388 int i, fail; | 392 int i, fail; |
389 assert(denoiser != NULL); | 393 assert(denoiser != NULL); |
390 | 394 |
391 for (i = 0; i < MAX_REF_FRAMES; ++i) { | 395 for (i = 0; i < MAX_REF_FRAMES; ++i) { |
392 fail = vp9_alloc_frame_buffer(&denoiser->running_avg_y[i], width, height, | 396 fail = vp9_alloc_frame_buffer(&denoiser->running_avg_y[i], width, height, |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 for (r = 0; r < yuv->uv_height / 2; ++r) { | 444 for (r = 0; r < yuv->uv_height / 2; ++r) { |
441 for (c = 0; c < yuv->uv_width / 2; ++c) { | 445 for (c = 0; c < yuv->uv_width / 2; ++c) { |
442 u[c] = UINT8_MAX / 2; | 446 u[c] = UINT8_MAX / 2; |
443 v[c] = UINT8_MAX / 2; | 447 v[c] = UINT8_MAX / 2; |
444 } | 448 } |
445 u += yuv->uv_stride + yuv->uv_width / 2; | 449 u += yuv->uv_stride + yuv->uv_width / 2; |
446 v += yuv->uv_stride + yuv->uv_width / 2; | 450 v += yuv->uv_stride + yuv->uv_width / 2; |
447 } | 451 } |
448 } | 452 } |
449 #endif | 453 #endif |
OLD | NEW |