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 11 #include "vp8/common/onyxc_int.h" 12 #include "onyx_int.h" 13 #include "vp8/common/systemdependent.h" 14 #include "vp8/encoder/quantize.h" 15 #include "vp8/common/alloccommon.h" 16 #include "mcomp.h" 17 #include "firstpass.h" 18 #include "vpx_scale/vpx_scale.h" 19 #include "vp8/common/extend.h" 20 #include "ratectrl.h" 21 #include "vp8/common/quant_common.h" 22 #include "segmentation.h" 23 #include "temporal_filter.h" 24 #include "vpx_mem/vpx_mem.h" 25 #include "vp8/common/swapyv12buffer.h" 26 #include "vp8/common/threading.h" 27 #include "vpx_ports/vpx_timer.h" 28 29 #include <math.h> 30 #include <limits.h> 31 32 #define ALT_REF_MC_ENABLED 1 /* toggle MC in AltRef filtering */ 33 #define ALT_REF_SUBPEL_ENABLED 1 /* toggle subpel in MC AltRef filtering */ 34 35 #if VP8_TEMPORAL_ALT_REF 36 37 static void vp8_temporal_filter_predictors_mb_c( 38 MACROBLOCKD *x, unsigned char *y_mb_ptr, unsigned char *u_mb_ptr, 39 unsigned char *v_mb_ptr, int stride, int mv_row, int mv_col, 40 unsigned char *pred) { 41 int offset; 42 unsigned char *yptr, *uptr, *vptr; 43 44 /* Y */ 45 yptr = y_mb_ptr + (mv_row >> 3) * stride + (mv_col >> 3); 46 47 if ((mv_row | mv_col) & 7) { 48 x->subpixel_predict16x16(yptr, stride, mv_col & 7, mv_row & 7, &pred[0], 49 16); 50 } else { 51 vp8_copy_mem16x16(yptr, stride, &pred[0], 16); 52 } 53 54 /* U & V */ 55 mv_row >>= 1; 56 mv_col >>= 1; 57 stride = (stride + 1) >> 1; 58 offset = (mv_row >> 3) * stride + (mv_col >> 3); 59 uptr = u_mb_ptr + offset; 60 vptr = v_mb_ptr + offset; 61 62 if ((mv_row | mv_col) & 7) { 63 x->subpixel_predict8x8(uptr, stride, mv_col & 7, mv_row & 7, &pred[256], 8); 64 x->subpixel_predict8x8(vptr, stride, mv_col & 7, mv_row & 7, &pred[320], 8); 65 } else { 66 vp8_copy_mem8x8(uptr, stride, &pred[256], 8); 67 vp8_copy_mem8x8(vptr, stride, &pred[320], 8); 68 } 69 } 70 void vp8_temporal_filter_apply_c(unsigned char *frame1, unsigned int stride, 71 unsigned char *frame2, unsigned int block_size, 72 int strength, int filter_weight, 73 unsigned int *accumulator, 74 unsigned short *count) { 75 unsigned int i, j, k; 76 int modifier; 77 int byte = 0; 78 const int rounding = strength > 0 ? 1 << (strength - 1) : 0; 79 80 for (i = 0, k = 0; i < block_size; ++i) { 81 for (j = 0; j < block_size; j++, k++) { 82 int src_byte = frame1[byte]; 83 int pixel_value = *frame2++; 84 85 modifier = src_byte - pixel_value; 86 /* This is an integer approximation of: 87 * float coeff = (3.0 * modifer * modifier) / pow(2, strength); 88 * modifier = (int)roundf(coeff > 16 ? 0 : 16-coeff); 89 */ 90 modifier *= modifier; 91 modifier *= 3; 92 modifier += rounding; 93 modifier >>= strength; 94 95 if (modifier > 16) modifier = 16; 96 97 modifier = 16 - modifier; 98 modifier *= filter_weight; 99 100 count[k] += modifier; 101 accumulator[k] += modifier * pixel_value; 102 103 byte++; 104 } 105 106 byte += stride - block_size; 107 } 108 } 109 110 #if ALT_REF_MC_ENABLED 111 112 static int vp8_temporal_filter_find_matching_mb_c(VP8_COMP *cpi, 113 YV12_BUFFER_CONFIG *arf_frame, 114 YV12_BUFFER_CONFIG *frame_ptr, 115 int mb_offset, 116 int error_thresh) { 117 MACROBLOCK *x = &cpi->mb; 118 int step_param; 119 int sadpb = x->sadperbit16; 120 int bestsme = INT_MAX; 121 122 BLOCK *b = &x->block[0]; 123 BLOCKD *d = &x->e_mbd.block[0]; 124 int_mv best_ref_mv1; 125 int_mv best_ref_mv1_full; /* full-pixel value of best_ref_mv1 */ 126 127 /* Save input state */ 128 unsigned char **base_src = b->base_src; 129 int src = b->src; 130 int src_stride = b->src_stride; 131 unsigned char *base_pre = x->e_mbd.pre.y_buffer; 132 int pre = d->offset; 133 int pre_stride = x->e_mbd.pre.y_stride; 134 135 (void)error_thresh; 136 137 best_ref_mv1.as_int = 0; 138 best_ref_mv1_full.as_mv.col = best_ref_mv1.as_mv.col >> 3; 139 best_ref_mv1_full.as_mv.row = best_ref_mv1.as_mv.row >> 3; 140 141 /* Setup frame pointers */ 142 b->base_src = &arf_frame->y_buffer; 143 b->src_stride = arf_frame->y_stride; 144 b->src = mb_offset; 145 146 x->e_mbd.pre.y_buffer = frame_ptr->y_buffer; 147 x->e_mbd.pre.y_stride = frame_ptr->y_stride; 148 d->offset = mb_offset; 149 150 /* Further step/diamond searches as necessary */ 151 if (cpi->Speed < 8) { 152 step_param = cpi->sf.first_step + (cpi->Speed > 5); 153 } else { 154 step_param = cpi->sf.first_step + 2; 155 } 156 157 /* TODO Check that the 16x16 vf & sdf are selected here */ 158 /* Ignore mv costing by sending NULL cost arrays */ 159 bestsme = 160 vp8_hex_search(x, b, d, &best_ref_mv1_full, &d->bmi.mv, step_param, sadpb, 161 &cpi->fn_ptr[BLOCK_16X16], NULL, NULL, &best_ref_mv1); 162 163 #if ALT_REF_SUBPEL_ENABLED 164 /* Try sub-pixel MC? */ 165 { 166 int distortion; 167 unsigned int sse; 168 /* Ignore mv costing by sending NULL cost array */ 169 bestsme = cpi->find_fractional_mv_step( 170 x, b, d, &d->bmi.mv, &best_ref_mv1, x->errorperbit, 171 &cpi->fn_ptr[BLOCK_16X16], NULL, &distortion, &sse); 172 } 173 #endif 174 175 /* Save input state */ 176 b->base_src = base_src; 177 b->src = src; 178 b->src_stride = src_stride; 179 x->e_mbd.pre.y_buffer = base_pre; 180 d->offset = pre; 181 x->e_mbd.pre.y_stride = pre_stride; 182 183 return bestsme; 184 } 185 #endif 186 187 static void vp8_temporal_filter_iterate_c(VP8_COMP *cpi, int frame_count, 188 int alt_ref_index, int strength) { 189 int byte; 190 int frame; 191 int mb_col, mb_row; 192 unsigned int filter_weight; 193 int mb_cols = cpi->common.mb_cols; 194 int mb_rows = cpi->common.mb_rows; 195 int mb_y_offset = 0; 196 int mb_uv_offset = 0; 197 DECLARE_ALIGNED(16, unsigned int, accumulator[16 * 16 + 8 * 8 + 8 * 8]); 198 DECLARE_ALIGNED(16, unsigned short, count[16 * 16 + 8 * 8 + 8 * 8]); 199 MACROBLOCKD *mbd = &cpi->mb.e_mbd; 200 YV12_BUFFER_CONFIG *f = cpi->frames[alt_ref_index]; 201 unsigned char *dst1, *dst2; 202 DECLARE_ALIGNED(16, unsigned char, predictor[16 * 16 + 8 * 8 + 8 * 8]); 203 204 /* Save input state */ 205 unsigned char *y_buffer = mbd->pre.y_buffer; 206 unsigned char *u_buffer = mbd->pre.u_buffer; 207 unsigned char *v_buffer = mbd->pre.v_buffer; 208 209 for (mb_row = 0; mb_row < mb_rows; ++mb_row) { 210 #if ALT_REF_MC_ENABLED 211 /* Source frames are extended to 16 pixels. This is different than 212 * L/A/G reference frames that have a border of 32 (VP8BORDERINPIXELS) 213 * A 6 tap filter is used for motion search. This requires 2 pixels 214 * before and 3 pixels after. So the largest Y mv on a border would 215 * then be 16 - 3. The UV blocks are half the size of the Y and 216 * therefore only extended by 8. The largest mv that a UV block 217 * can support is 8 - 3. A UV mv is half of a Y mv. 218 * (16 - 3) >> 1 == 6 which is greater than 8 - 3. 219 * To keep the mv in play for both Y and UV planes the max that it 220 * can be on a border is therefore 16 - 5. 221 */ 222 cpi->mb.mv_row_min = -((mb_row * 16) + (16 - 5)); 223 cpi->mb.mv_row_max = ((cpi->common.mb_rows - 1 - mb_row) * 16) + (16 - 5); 224 #endif 225 226 for (mb_col = 0; mb_col < mb_cols; ++mb_col) { 227 int i, j, k; 228 int stride; 229 230 memset(accumulator, 0, 384 * sizeof(unsigned int)); 231 memset(count, 0, 384 * sizeof(unsigned short)); 232 233 #if ALT_REF_MC_ENABLED 234 cpi->mb.mv_col_min = -((mb_col * 16) + (16 - 5)); 235 cpi->mb.mv_col_max = ((cpi->common.mb_cols - 1 - mb_col) * 16) + (16 - 5); 236 #endif 237 238 for (frame = 0; frame < frame_count; ++frame) { 239 if (cpi->frames[frame] == NULL) continue; 240 241 mbd->block[0].bmi.mv.as_mv.row = 0; 242 mbd->block[0].bmi.mv.as_mv.col = 0; 243 244 if (frame == alt_ref_index) { 245 filter_weight = 2; 246 } else { 247 int err = 0; 248 #if ALT_REF_MC_ENABLED 249 #define THRESH_LOW 10000 250 #define THRESH_HIGH 20000 251 /* Find best match in this frame by MC */ 252 err = vp8_temporal_filter_find_matching_mb_c( 253 cpi, cpi->frames[alt_ref_index], cpi->frames[frame], mb_y_offset, 254 THRESH_LOW); 255 #endif 256 /* Assign higher weight to matching MB if it's error 257 * score is lower. If not applying MC default behavior 258 * is to weight all MBs equal. 259 */ 260 filter_weight = err < THRESH_LOW ? 2 : err < THRESH_HIGH ? 1 : 0; 261 } 262 263 if (filter_weight != 0) { 264 /* Construct the predictors */ 265 vp8_temporal_filter_predictors_mb_c( 266 mbd, cpi->frames[frame]->y_buffer + mb_y_offset, 267 cpi->frames[frame]->u_buffer + mb_uv_offset, 268 cpi->frames[frame]->v_buffer + mb_uv_offset, 269 cpi->frames[frame]->y_stride, mbd->block[0].bmi.mv.as_mv.row, 270 mbd->block[0].bmi.mv.as_mv.col, predictor); 271 272 /* Apply the filter (YUV) */ 273 vp8_temporal_filter_apply(f->y_buffer + mb_y_offset, f->y_stride, 274 predictor, 16, strength, filter_weight, 275 accumulator, count); 276 277 vp8_temporal_filter_apply(f->u_buffer + mb_uv_offset, f->uv_stride, 278 predictor + 256, 8, strength, filter_weight, 279 accumulator + 256, count + 256); 280 281 vp8_temporal_filter_apply(f->v_buffer + mb_uv_offset, f->uv_stride, 282 predictor + 320, 8, strength, filter_weight, 283 accumulator + 320, count + 320); 284 } 285 } 286 287 /* Normalize filter output to produce AltRef frame */ 288 dst1 = cpi->alt_ref_buffer.y_buffer; 289 stride = cpi->alt_ref_buffer.y_stride; 290 byte = mb_y_offset; 291 for (i = 0, k = 0; i < 16; ++i) { 292 for (j = 0; j < 16; j++, k++) { 293 unsigned int pval = accumulator[k] + (count[k] >> 1); 294 pval *= cpi->fixed_divide[count[k]]; 295 pval >>= 19; 296 297 dst1[byte] = (unsigned char)pval; 298 299 /* move to next pixel */ 300 byte++; 301 } 302 303 byte += stride - 16; 304 } 305 306 dst1 = cpi->alt_ref_buffer.u_buffer; 307 dst2 = cpi->alt_ref_buffer.v_buffer; 308 stride = cpi->alt_ref_buffer.uv_stride; 309 byte = mb_uv_offset; 310 for (i = 0, k = 256; i < 8; ++i) { 311 for (j = 0; j < 8; j++, k++) { 312 int m = k + 64; 313 314 /* U */ 315 unsigned int pval = accumulator[k] + (count[k] >> 1); 316 pval *= cpi->fixed_divide[count[k]]; 317 pval >>= 19; 318 dst1[byte] = (unsigned char)pval; 319 320 /* V */ 321 pval = accumulator[m] + (count[m] >> 1); 322 pval *= cpi->fixed_divide[count[m]]; 323 pval >>= 19; 324 dst2[byte] = (unsigned char)pval; 325 326 /* move to next pixel */ 327 byte++; 328 } 329 330 byte += stride - 8; 331 } 332 333 mb_y_offset += 16; 334 mb_uv_offset += 8; 335 } 336 337 mb_y_offset += 16 * (f->y_stride - mb_cols); 338 mb_uv_offset += 8 * (f->uv_stride - mb_cols); 339 } 340 341 /* Restore input state */ 342 mbd->pre.y_buffer = y_buffer; 343 mbd->pre.u_buffer = u_buffer; 344 mbd->pre.v_buffer = v_buffer; 345 } 346 347 void vp8_temporal_filter_prepare_c(VP8_COMP *cpi, int distance) { 348 int frame = 0; 349 350 int num_frames_backward = 0; 351 int num_frames_forward = 0; 352 int frames_to_blur_backward = 0; 353 int frames_to_blur_forward = 0; 354 int frames_to_blur = 0; 355 int start_frame = 0; 356 357 int strength = cpi->oxcf.arnr_strength; 358 359 int blur_type = cpi->oxcf.arnr_type; 360 361 int max_frames = cpi->active_arnr_frames; 362 363 num_frames_backward = distance; 364 num_frames_forward = 365 vp8_lookahead_depth(cpi->lookahead) - (num_frames_backward + 1); 366 367 switch (blur_type) { 368 case 1: 369 /* Backward Blur */ 370 371 frames_to_blur_backward = num_frames_backward; 372 373 if (frames_to_blur_backward >= max_frames) { 374 frames_to_blur_backward = max_frames - 1; 375 } 376 377 frames_to_blur = frames_to_blur_backward + 1; 378 break; 379 380 case 2: 381 /* Forward Blur */ 382 383 frames_to_blur_forward = num_frames_forward; 384 385 if (frames_to_blur_forward >= max_frames) { 386 frames_to_blur_forward = max_frames - 1; 387 } 388 389 frames_to_blur = frames_to_blur_forward + 1; 390 break; 391 392 case 3: 393 default: 394 /* Center Blur */ 395 frames_to_blur_forward = num_frames_forward; 396 frames_to_blur_backward = num_frames_backward; 397 398 if (frames_to_blur_forward > frames_to_blur_backward) { 399 frames_to_blur_forward = frames_to_blur_backward; 400 } 401 402 if (frames_to_blur_backward > frames_to_blur_forward) { 403 frames_to_blur_backward = frames_to_blur_forward; 404 } 405 406 /* When max_frames is even we have 1 more frame backward than forward */ 407 if (frames_to_blur_forward > (max_frames - 1) / 2) { 408 frames_to_blur_forward = ((max_frames - 1) / 2); 409 } 410 411 if (frames_to_blur_backward > (max_frames / 2)) { 412 frames_to_blur_backward = (max_frames / 2); 413 } 414 415 frames_to_blur = frames_to_blur_backward + frames_to_blur_forward + 1; 416 break; 417 } 418 419 start_frame = distance + frames_to_blur_forward; 420 421 /* Setup frame pointers, NULL indicates frame not included in filter */ 422 memset(cpi->frames, 0, max_frames * sizeof(YV12_BUFFER_CONFIG *)); 423 for (frame = 0; frame < frames_to_blur; ++frame) { 424 int which_buffer = start_frame - frame; 425 struct lookahead_entry *buf = 426 vp8_lookahead_peek(cpi->lookahead, which_buffer, PEEK_FORWARD); 427 cpi->frames[frames_to_blur - 1 - frame] = &buf->img; 428 } 429 430 vp8_temporal_filter_iterate_c(cpi, frames_to_blur, frames_to_blur_backward, 431 strength); 432 } 433 #endif 434