| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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 <math.h> | 13 #include <math.h> |
| 14 #include <stdio.h> | 14 #include <stdio.h> |
| 15 | 15 |
| 16 #include "./vp9_rtcd.h" | 16 #include "./vp9_rtcd.h" |
| 17 | 17 |
| 18 #include "vpx_mem/vpx_mem.h" | 18 #include "vpx_mem/vpx_mem.h" |
| 19 | 19 |
| 20 #include "vp9/common/vp9_blockd.h" |
| 20 #include "vp9/common/vp9_common.h" | 21 #include "vp9/common/vp9_common.h" |
| 21 #include "vp9/common/vp9_mvref_common.h" | 22 #include "vp9/common/vp9_mvref_common.h" |
| 22 #include "vp9/common/vp9_reconinter.h" | 23 #include "vp9/common/vp9_reconinter.h" |
| 23 #include "vp9/common/vp9_reconintra.h" | 24 #include "vp9/common/vp9_reconintra.h" |
| 24 | 25 |
| 25 #include "vp9/encoder/vp9_encoder.h" | 26 #include "vp9/encoder/vp9_encoder.h" |
| 26 #include "vp9/encoder/vp9_pickmode.h" | 27 #include "vp9/encoder/vp9_pickmode.h" |
| 27 #include "vp9/encoder/vp9_ratectrl.h" | 28 #include "vp9/encoder/vp9_ratectrl.h" |
| 28 #include "vp9/encoder/vp9_rd.h" | 29 #include "vp9/encoder/vp9_rd.h" |
| 29 | 30 |
| (...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 // TODO(yunqingwang): In this function, only y-plane dist is | 337 // TODO(yunqingwang): In this function, only y-plane dist is |
| 337 // calculated. | 338 // calculated. |
| 338 *dist = (sse << 4); // + ((sse_u + sse_v) << 4); | 339 *dist = (sse << 4); // + ((sse_u + sse_v) << 4); |
| 339 | 340 |
| 340 // *disable_skip = 1; | 341 // *disable_skip = 1; |
| 341 } | 342 } |
| 342 } | 343 } |
| 343 } | 344 } |
| 344 } | 345 } |
| 345 | 346 |
| 347 struct estimate_block_intra_args { |
| 348 VP9_COMP *cpi; |
| 349 MACROBLOCK *x; |
| 350 PREDICTION_MODE mode; |
| 351 int rate; |
| 352 int64_t dist; |
| 353 }; |
| 354 |
| 355 static void estimate_block_intra(int plane, int block, BLOCK_SIZE plane_bsize, |
| 356 TX_SIZE tx_size, void *arg) { |
| 357 struct estimate_block_intra_args* const args = arg; |
| 358 VP9_COMP *const cpi = args->cpi; |
| 359 MACROBLOCK *const x = args->x; |
| 360 MACROBLOCKD *const xd = &x->e_mbd; |
| 361 struct macroblock_plane *const p = &x->plane[0]; |
| 362 struct macroblockd_plane *const pd = &xd->plane[0]; |
| 363 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tx_size]; |
| 364 uint8_t *const src_buf_base = p->src.buf; |
| 365 uint8_t *const dst_buf_base = pd->dst.buf; |
| 366 const int src_stride = p->src.stride; |
| 367 const int dst_stride = pd->dst.stride; |
| 368 int i, j; |
| 369 int rate; |
| 370 int64_t dist; |
| 371 unsigned int var_y, sse_y; |
| 372 txfrm_block_to_raster_xy(plane_bsize, tx_size, block, &i, &j); |
| 373 assert(plane == 0); |
| 374 (void) plane; |
| 375 |
| 376 p->src.buf = &src_buf_base[4 * (j * src_stride + i)]; |
| 377 pd->dst.buf = &dst_buf_base[4 * (j * dst_stride + i)]; |
| 378 // Use source buffer as an approximation for the fully reconstructed buffer. |
| 379 vp9_predict_intra_block(xd, block >> (2 * tx_size), |
| 380 b_width_log2(plane_bsize), |
| 381 tx_size, args->mode, |
| 382 p->src.buf, src_stride, |
| 383 pd->dst.buf, dst_stride, |
| 384 i, j, 0); |
| 385 // This procedure assumes zero offset from p->src.buf and pd->dst.buf. |
| 386 model_rd_for_sb_y(cpi, bsize_tx, x, xd, &rate, &dist, &var_y, &sse_y); |
| 387 p->src.buf = src_buf_base; |
| 388 pd->dst.buf = dst_buf_base; |
| 389 args->rate += rate; |
| 390 args->dist += dist; |
| 391 } |
| 392 |
| 346 static const THR_MODES mode_idx[MAX_REF_FRAMES - 1][4] = { | 393 static const THR_MODES mode_idx[MAX_REF_FRAMES - 1][4] = { |
| 347 {THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV}, | 394 {THR_NEARESTMV, THR_NEARMV, THR_ZEROMV, THR_NEWMV}, |
| 348 {THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG}, | 395 {THR_NEARESTG, THR_NEARG, THR_ZEROG, THR_NEWG}, |
| 349 {THR_NEARESTA, THR_NEARA, THR_ZEROA, THR_NEWA}, | 396 {THR_NEARESTA, THR_NEARA, THR_ZEROA, THR_NEWA}, |
| 350 }; | 397 }; |
| 351 | 398 |
| 352 // TODO(jingning) placeholder for inter-frame non-RD mode decision. | 399 // TODO(jingning) placeholder for inter-frame non-RD mode decision. |
| 353 // this needs various further optimizations. to be continued.. | 400 // this needs various further optimizations. to be continued.. |
| 354 int64_t vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, | 401 int64_t vp9_pick_inter_mode(VP9_COMP *cpi, MACROBLOCK *x, |
| 355 const TileInfo *const tile, | 402 const TileInfo *const tile, |
| 356 int mi_row, int mi_col, | 403 int mi_row, int mi_col, |
| 357 int *returnrate, | 404 int *returnrate, |
| 358 int64_t *returndistortion, | 405 int64_t *returndistortion, |
| 359 BLOCK_SIZE bsize, | 406 BLOCK_SIZE bsize, |
| 360 PICK_MODE_CONTEXT *ctx) { | 407 PICK_MODE_CONTEXT *ctx) { |
| 361 MACROBLOCKD *xd = &x->e_mbd; | 408 MACROBLOCKD *xd = &x->e_mbd; |
| 362 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; | 409 MB_MODE_INFO *mbmi = &xd->mi[0]->mbmi; |
| 363 struct macroblock_plane *const p = &x->plane[0]; | |
| 364 struct macroblockd_plane *const pd = &xd->plane[0]; | 410 struct macroblockd_plane *const pd = &xd->plane[0]; |
| 365 PREDICTION_MODE this_mode, best_mode = ZEROMV; | 411 PREDICTION_MODE this_mode, best_mode = ZEROMV; |
| 366 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME; | 412 MV_REFERENCE_FRAME ref_frame, best_ref_frame = LAST_FRAME; |
| 367 TX_SIZE best_tx_size = MIN(max_txsize_lookup[bsize], | 413 TX_SIZE best_tx_size = MIN(max_txsize_lookup[bsize], |
| 368 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]); | 414 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]); |
| 369 INTERP_FILTER best_pred_filter = EIGHTTAP; | 415 INTERP_FILTER best_pred_filter = EIGHTTAP; |
| 370 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES]; | 416 int_mv frame_mv[MB_MODE_COUNT][MAX_REF_FRAMES]; |
| 371 struct buf_2d yv12_mb[4][MAX_MB_PLANE]; | 417 struct buf_2d yv12_mb[4][MAX_MB_PLANE]; |
| 372 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG, | 418 static const int flag_list[4] = { 0, VP9_LAST_FLAG, VP9_GOLD_FLAG, |
| 373 VP9_ALT_FLAG }; | 419 VP9_ALT_FLAG }; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 390 unsigned char segment_id = mbmi->segment_id; | 436 unsigned char segment_id = mbmi->segment_id; |
| 391 const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize]; | 437 const int *const rd_threshes = cpi->rd.threshes[segment_id][bsize]; |
| 392 const int *const rd_thresh_freq_fact = cpi->rd.thresh_freq_fact[bsize]; | 438 const int *const rd_thresh_freq_fact = cpi->rd.thresh_freq_fact[bsize]; |
| 393 // Mode index conversion form THR_MODES to PREDICTION_MODE for a ref frame. | 439 // Mode index conversion form THR_MODES to PREDICTION_MODE for a ref frame. |
| 394 INTERP_FILTER filter_ref = cm->interp_filter; | 440 INTERP_FILTER filter_ref = cm->interp_filter; |
| 395 int bsl = mi_width_log2(bsize); | 441 int bsl = mi_width_log2(bsize); |
| 396 const int pred_filter_search = cm->interp_filter == SWITCHABLE ? | 442 const int pred_filter_search = cm->interp_filter == SWITCHABLE ? |
| 397 (((mi_row + mi_col) >> bsl) + | 443 (((mi_row + mi_col) >> bsl) + |
| 398 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0; | 444 get_chessboard_index(cm->current_video_frame)) & 0x1 : 0; |
| 399 int const_motion[MAX_REF_FRAMES] = { 0 }; | 445 int const_motion[MAX_REF_FRAMES] = { 0 }; |
| 400 int bh = num_4x4_blocks_high_lookup[bsize] << 2; | 446 const int bh = num_4x4_blocks_high_lookup[bsize] << 2; |
| 401 int bw = num_4x4_blocks_wide_lookup[bsize] << 2; | 447 const int bw = num_4x4_blocks_wide_lookup[bsize] << 2; |
| 402 int pixels_in_block = bh * bw; | 448 const int pixels_in_block = bh * bw; |
| 403 // For speed 6, the result of interp filter is reused later in actual encoding | 449 // For speed 6, the result of interp filter is reused later in actual encoding |
| 404 // process. | 450 // process. |
| 405 // tmp[3] points to dst buffer, and the other 3 point to allocated buffers. | 451 // tmp[3] points to dst buffer, and the other 3 point to allocated buffers. |
| 406 PRED_BUFFER tmp[4]; | 452 PRED_BUFFER tmp[4]; |
| 407 DECLARE_ALIGNED_ARRAY(16, uint8_t, pred_buf, 3 * 64 * 64); | 453 DECLARE_ALIGNED_ARRAY(16, uint8_t, pred_buf, 3 * 64 * 64); |
| 408 struct buf_2d orig_dst = pd->dst; | 454 struct buf_2d orig_dst = pd->dst; |
| 409 PRED_BUFFER *best_pred = NULL; | 455 PRED_BUFFER *best_pred = NULL; |
| 410 PRED_BUFFER *this_mode_pred = NULL; | 456 PRED_BUFFER *this_mode_pred = NULL; |
| 411 int i; | 457 int i; |
| 412 | 458 |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 mbmi->tx_size = best_tx_size; | 709 mbmi->tx_size = best_tx_size; |
| 664 mbmi->ref_frame[0] = best_ref_frame; | 710 mbmi->ref_frame[0] = best_ref_frame; |
| 665 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int; | 711 mbmi->mv[0].as_int = frame_mv[best_mode][best_ref_frame].as_int; |
| 666 xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int; | 712 xd->mi[0]->bmi[0].as_mv[0].as_int = mbmi->mv[0].as_int; |
| 667 x->skip_txfm[0] = skip_txfm; | 713 x->skip_txfm[0] = skip_txfm; |
| 668 | 714 |
| 669 // Perform intra prediction search, if the best SAD is above a certain | 715 // Perform intra prediction search, if the best SAD is above a certain |
| 670 // threshold. | 716 // threshold. |
| 671 if (!x->skip && best_rd > inter_mode_thresh && | 717 if (!x->skip && best_rd > inter_mode_thresh && |
| 672 bsize <= cpi->sf.max_intra_bsize) { | 718 bsize <= cpi->sf.max_intra_bsize) { |
| 673 int i, j; | 719 struct estimate_block_intra_args args = { cpi, x, DC_PRED, 0, 0 }; |
| 674 const int width = num_4x4_blocks_wide_lookup[bsize]; | |
| 675 const int height = num_4x4_blocks_high_lookup[bsize]; | |
| 676 | 720 |
| 677 int rate2 = 0; | 721 const TX_SIZE intra_tx_size = |
| 678 int64_t dist2 = 0; | 722 MIN(max_txsize_lookup[bsize], |
| 679 const int dst_stride = cpi->sf.reuse_inter_pred_sby ? bw : pd->dst.stride; | 723 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]); |
| 680 const int src_stride = p->src.stride; | |
| 681 int block_idx = 0; | |
| 682 | |
| 683 TX_SIZE tmp_tx_size = MIN(max_txsize_lookup[bsize], | |
| 684 tx_mode_to_biggest_tx_size[cpi->common.tx_mode]); | |
| 685 const BLOCK_SIZE bsize_tx = txsize_to_bsize[tmp_tx_size]; | |
| 686 const int step = 1 << tmp_tx_size; | |
| 687 | 724 |
| 688 if (cpi->sf.reuse_inter_pred_sby) { | 725 if (cpi->sf.reuse_inter_pred_sby) { |
| 689 pd->dst.buf = tmp[0].data; | 726 pd->dst.buf = tmp[0].data; |
| 690 pd->dst.stride = bw; | 727 pd->dst.stride = bw; |
| 691 } | 728 } |
| 692 | 729 |
| 693 for (this_mode = DC_PRED; this_mode <= DC_PRED; ++this_mode) { | 730 for (this_mode = DC_PRED; this_mode <= DC_PRED; ++this_mode) { |
| 694 uint8_t *const src_buf_base = p->src.buf; | 731 const TX_SIZE saved_tx_size = mbmi->tx_size; |
| 695 uint8_t *const dst_buf_base = pd->dst.buf; | 732 args.mode = this_mode; |
| 696 for (j = 0; j < height; j += step) { | 733 args.rate = 0; |
| 697 for (i = 0; i < width; i += step) { | 734 args.dist = 0; |
| 698 p->src.buf = &src_buf_base[4 * (j * src_stride + i)]; | 735 mbmi->tx_size = intra_tx_size; |
| 699 pd->dst.buf = &dst_buf_base[4 * (j * dst_stride + i)]; | 736 vp9_foreach_transformed_block_in_plane(xd, bsize, 0, |
| 700 // Use source buffer as an approximation for the fully reconstructed | 737 estimate_block_intra, &args); |
| 701 // buffer | 738 mbmi->tx_size = saved_tx_size; |
| 702 vp9_predict_intra_block(xd, block_idx, b_width_log2(bsize), | 739 rate = args.rate; |
| 703 tmp_tx_size, this_mode, | 740 dist = args.dist; |
| 704 p->src.buf, src_stride, | |
| 705 pd->dst.buf, dst_stride, | |
| 706 i, j, 0); | |
| 707 model_rd_for_sb_y(cpi, bsize_tx, x, xd, &rate, &dist, &var_y, &sse_y); | |
| 708 rate2 += rate; | |
| 709 dist2 += dist; | |
| 710 ++block_idx; | |
| 711 } | |
| 712 } | |
| 713 p->src.buf = src_buf_base; | |
| 714 pd->dst.buf = dst_buf_base; | |
| 715 | |
| 716 rate = rate2; | |
| 717 dist = dist2; | |
| 718 | |
| 719 rate += cpi->mbmode_cost[this_mode]; | 741 rate += cpi->mbmode_cost[this_mode]; |
| 720 rate += intra_cost_penalty; | 742 rate += intra_cost_penalty; |
| 721 this_rd = RDCOST(x->rdmult, x->rddiv, rate, dist); | 743 this_rd = RDCOST(x->rdmult, x->rddiv, rate, dist); |
| 722 | 744 |
| 723 if (cpi->sf.reuse_inter_pred_sby) | |
| 724 pd->dst = orig_dst; | |
| 725 | |
| 726 if (this_rd + intra_mode_cost < best_rd) { | 745 if (this_rd + intra_mode_cost < best_rd) { |
| 727 best_rd = this_rd; | 746 best_rd = this_rd; |
| 728 *returnrate = rate; | 747 *returnrate = rate; |
| 729 *returndistortion = dist; | 748 *returndistortion = dist; |
| 730 mbmi->mode = this_mode; | 749 mbmi->mode = this_mode; |
| 731 mbmi->tx_size = tmp_tx_size; | 750 mbmi->tx_size = intra_tx_size; |
| 732 mbmi->ref_frame[0] = INTRA_FRAME; | 751 mbmi->ref_frame[0] = INTRA_FRAME; |
| 733 mbmi->uv_mode = this_mode; | 752 mbmi->uv_mode = this_mode; |
| 734 mbmi->mv[0].as_int = INVALID_MV; | 753 mbmi->mv[0].as_int = INVALID_MV; |
| 735 } else { | 754 } else { |
| 736 x->skip_txfm[0] = skip_txfm; | 755 x->skip_txfm[0] = skip_txfm; |
| 737 } | 756 } |
| 738 } | 757 } |
| 758 if (cpi->sf.reuse_inter_pred_sby) |
| 759 pd->dst = orig_dst; |
| 739 } | 760 } |
| 740 | 761 |
| 741 return INT64_MAX; | 762 return INT64_MAX; |
| 742 } | 763 } |
| OLD | NEW |