Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: source/libvpx/vp9/encoder/vp9_firstpass.c

Issue 1217053002: libvpx: Pull from upstream (Closed) Base URL: https://chromium.googlesource.com/chromium/deps/libvpx.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_firstpass.h ('k') | source/libvpx/vp9/encoder/vp9_mcomp.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved. 2 * Copyright (c) 2010 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
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); 106 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
107 vpx_codec_pkt_list_add(pktlist, &pkt); 107 vpx_codec_pkt_list_add(pktlist, &pkt);
108 108
109 // TEMP debug code 109 // TEMP debug code
110 #if OUTPUT_FPF 110 #if OUTPUT_FPF
111 { 111 {
112 FILE *fpfile; 112 FILE *fpfile;
113 fpfile = fopen("firstpass.stt", "a"); 113 fpfile = fopen("firstpass.stt", "a");
114 114
115 fprintf(fpfile, "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf" 115 fprintf(fpfile, "%12.0lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf %12.4lf"
116 "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf" 116 "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf"
117 "%12.4lf %12.0lf %12.0lf %12.0lf %12.4lf\n", 117 "%12.4lf %12.4lf %12.0lf %12.0lf %12.0lf %12.4lf\n",
118 stats->frame, 118 stats->frame,
119 stats->weight, 119 stats->weight,
120 stats->intra_error, 120 stats->intra_error,
121 stats->coded_error, 121 stats->coded_error,
122 stats->sr_coded_error, 122 stats->sr_coded_error,
123 stats->pcnt_inter, 123 stats->pcnt_inter,
124 stats->pcnt_motion, 124 stats->pcnt_motion,
125 stats->pcnt_second_ref, 125 stats->pcnt_second_ref,
126 stats->pcnt_neutral, 126 stats->pcnt_neutral,
127 stats->intra_skip_pct,
128 stats->inactive_zone_rows,
127 stats->MVr, 129 stats->MVr,
128 stats->mvr_abs, 130 stats->mvr_abs,
129 stats->MVc, 131 stats->MVc,
130 stats->mvc_abs, 132 stats->mvc_abs,
131 stats->MVrv, 133 stats->MVrv,
132 stats->MVcv, 134 stats->MVcv,
133 stats->mv_in_out_count, 135 stats->mv_in_out_count,
134 stats->new_mv_count, 136 stats->new_mv_count,
135 stats->count, 137 stats->count,
136 stats->duration); 138 stats->duration);
(...skipping 16 matching lines...) Expand all
153 static void zero_stats(FIRSTPASS_STATS *section) { 155 static void zero_stats(FIRSTPASS_STATS *section) {
154 section->frame = 0.0; 156 section->frame = 0.0;
155 section->weight = 0.0; 157 section->weight = 0.0;
156 section->intra_error = 0.0; 158 section->intra_error = 0.0;
157 section->coded_error = 0.0; 159 section->coded_error = 0.0;
158 section->sr_coded_error = 0.0; 160 section->sr_coded_error = 0.0;
159 section->pcnt_inter = 0.0; 161 section->pcnt_inter = 0.0;
160 section->pcnt_motion = 0.0; 162 section->pcnt_motion = 0.0;
161 section->pcnt_second_ref = 0.0; 163 section->pcnt_second_ref = 0.0;
162 section->pcnt_neutral = 0.0; 164 section->pcnt_neutral = 0.0;
163 section->MVr = 0.0; 165 section->intra_skip_pct = 0.0;
166 section->inactive_zone_rows = 0.0;
167 section->MVr = 0.0;
164 section->mvr_abs = 0.0; 168 section->mvr_abs = 0.0;
165 section->MVc = 0.0; 169 section->MVc = 0.0;
166 section->mvc_abs = 0.0; 170 section->mvc_abs = 0.0;
167 section->MVrv = 0.0; 171 section->MVrv = 0.0;
168 section->MVcv = 0.0; 172 section->MVcv = 0.0;
169 section->mv_in_out_count = 0.0; 173 section->mv_in_out_count = 0.0;
170 section->new_mv_count = 0.0; 174 section->new_mv_count = 0.0;
171 section->count = 0.0; 175 section->count = 0.0;
172 section->duration = 1.0; 176 section->duration = 1.0;
173 section->spatial_layer_id = 0; 177 section->spatial_layer_id = 0;
174 } 178 }
175 179
176 static void accumulate_stats(FIRSTPASS_STATS *section, 180 static void accumulate_stats(FIRSTPASS_STATS *section,
177 const FIRSTPASS_STATS *frame) { 181 const FIRSTPASS_STATS *frame) {
178 section->frame += frame->frame; 182 section->frame += frame->frame;
179 section->weight += frame->weight; 183 section->weight += frame->weight;
180 section->spatial_layer_id = frame->spatial_layer_id; 184 section->spatial_layer_id = frame->spatial_layer_id;
181 section->intra_error += frame->intra_error; 185 section->intra_error += frame->intra_error;
182 section->coded_error += frame->coded_error; 186 section->coded_error += frame->coded_error;
183 section->sr_coded_error += frame->sr_coded_error; 187 section->sr_coded_error += frame->sr_coded_error;
184 section->pcnt_inter += frame->pcnt_inter; 188 section->pcnt_inter += frame->pcnt_inter;
185 section->pcnt_motion += frame->pcnt_motion; 189 section->pcnt_motion += frame->pcnt_motion;
186 section->pcnt_second_ref += frame->pcnt_second_ref; 190 section->pcnt_second_ref += frame->pcnt_second_ref;
187 section->pcnt_neutral += frame->pcnt_neutral; 191 section->pcnt_neutral += frame->pcnt_neutral;
188 section->MVr += frame->MVr; 192 section->intra_skip_pct += frame->intra_skip_pct;
193 section->inactive_zone_rows += frame->inactive_zone_rows;
194 section->MVr += frame->MVr;
189 section->mvr_abs += frame->mvr_abs; 195 section->mvr_abs += frame->mvr_abs;
190 section->MVc += frame->MVc; 196 section->MVc += frame->MVc;
191 section->mvc_abs += frame->mvc_abs; 197 section->mvc_abs += frame->mvc_abs;
192 section->MVrv += frame->MVrv; 198 section->MVrv += frame->MVrv;
193 section->MVcv += frame->MVcv; 199 section->MVcv += frame->MVcv;
194 section->mv_in_out_count += frame->mv_in_out_count; 200 section->mv_in_out_count += frame->mv_in_out_count;
195 section->new_mv_count += frame->new_mv_count; 201 section->new_mv_count += frame->new_mv_count;
196 section->count += frame->count; 202 section->count += frame->count;
197 section->duration += frame->duration; 203 section->duration += frame->duration;
198 } 204 }
199 205
200 static void subtract_stats(FIRSTPASS_STATS *section, 206 static void subtract_stats(FIRSTPASS_STATS *section,
201 const FIRSTPASS_STATS *frame) { 207 const FIRSTPASS_STATS *frame) {
202 section->frame -= frame->frame; 208 section->frame -= frame->frame;
203 section->weight -= frame->weight; 209 section->weight -= frame->weight;
204 section->intra_error -= frame->intra_error; 210 section->intra_error -= frame->intra_error;
205 section->coded_error -= frame->coded_error; 211 section->coded_error -= frame->coded_error;
206 section->sr_coded_error -= frame->sr_coded_error; 212 section->sr_coded_error -= frame->sr_coded_error;
207 section->pcnt_inter -= frame->pcnt_inter; 213 section->pcnt_inter -= frame->pcnt_inter;
208 section->pcnt_motion -= frame->pcnt_motion; 214 section->pcnt_motion -= frame->pcnt_motion;
209 section->pcnt_second_ref -= frame->pcnt_second_ref; 215 section->pcnt_second_ref -= frame->pcnt_second_ref;
210 section->pcnt_neutral -= frame->pcnt_neutral; 216 section->pcnt_neutral -= frame->pcnt_neutral;
211 section->MVr -= frame->MVr; 217 section->intra_skip_pct -= frame->intra_skip_pct;
218 section->inactive_zone_rows -= frame->inactive_zone_rows;
219 section->MVr -= frame->MVr;
212 section->mvr_abs -= frame->mvr_abs; 220 section->mvr_abs -= frame->mvr_abs;
213 section->MVc -= frame->MVc; 221 section->MVc -= frame->MVc;
214 section->mvc_abs -= frame->mvc_abs; 222 section->mvc_abs -= frame->mvc_abs;
215 section->MVrv -= frame->MVrv; 223 section->MVrv -= frame->MVrv;
216 section->MVcv -= frame->MVcv; 224 section->MVcv -= frame->MVcv;
217 section->mv_in_out_count -= frame->mv_in_out_count; 225 section->mv_in_out_count -= frame->mv_in_out_count;
218 section->new_mv_count -= frame->new_mv_count; 226 section->new_mv_count -= frame->new_mv_count;
219 section->count -= frame->count; 227 section->count -= frame->count;
220 section->duration -= frame->duration; 228 section->duration -= frame->duration;
221 } 229 }
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 (cm->current_video_frame == 0 || 454 (cm->current_video_frame == 0 ||
447 (cpi->frame_flags & FRAMEFLAGS_KEY))) { 455 (cpi->frame_flags & FRAMEFLAGS_KEY))) {
448 cm->frame_type = KEY_FRAME; 456 cm->frame_type = KEY_FRAME;
449 } else { 457 } else {
450 cm->frame_type = INTER_FRAME; 458 cm->frame_type = INTER_FRAME;
451 } 459 }
452 // Do not use periodic key frames. 460 // Do not use periodic key frames.
453 cpi->rc.frames_to_key = INT_MAX; 461 cpi->rc.frames_to_key = INT_MAX;
454 } 462 }
455 463
464 #define UL_INTRA_THRESH 50
465 #define INVALID_ROW -1
456 void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) { 466 void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
457 int mb_row, mb_col; 467 int mb_row, mb_col;
458 MACROBLOCK *const x = &cpi->td.mb; 468 MACROBLOCK *const x = &cpi->td.mb;
459 VP9_COMMON *const cm = &cpi->common; 469 VP9_COMMON *const cm = &cpi->common;
460 MACROBLOCKD *const xd = &x->e_mbd; 470 MACROBLOCKD *const xd = &x->e_mbd;
461 TileInfo tile; 471 TileInfo tile;
462 struct macroblock_plane *const p = x->plane; 472 struct macroblock_plane *const p = x->plane;
463 struct macroblockd_plane *const pd = xd->plane; 473 struct macroblockd_plane *const pd = xd->plane;
464 const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none; 474 const PICK_MODE_CONTEXT *ctx = &cpi->td.pc_root->none;
465 int i; 475 int i;
466 476
467 int recon_yoffset, recon_uvoffset; 477 int recon_yoffset, recon_uvoffset;
468 int64_t intra_error = 0; 478 int64_t intra_error = 0;
469 int64_t coded_error = 0; 479 int64_t coded_error = 0;
470 int64_t sr_coded_error = 0; 480 int64_t sr_coded_error = 0;
471 481
472 int sum_mvr = 0, sum_mvc = 0; 482 int sum_mvr = 0, sum_mvc = 0;
473 int sum_mvr_abs = 0, sum_mvc_abs = 0; 483 int sum_mvr_abs = 0, sum_mvc_abs = 0;
474 int64_t sum_mvrs = 0, sum_mvcs = 0; 484 int64_t sum_mvrs = 0, sum_mvcs = 0;
475 int mvcount = 0; 485 int mvcount = 0;
476 int intercount = 0; 486 int intercount = 0;
477 int second_ref_count = 0; 487 int second_ref_count = 0;
478 const int intrapenalty = INTRA_MODE_PENALTY; 488 const int intrapenalty = INTRA_MODE_PENALTY;
479 double neutral_count; 489 double neutral_count;
490 int intra_skip_count = 0;
491 int image_data_start_row = INVALID_ROW;
480 int new_mv_count = 0; 492 int new_mv_count = 0;
481 int sum_in_vectors = 0; 493 int sum_in_vectors = 0;
482 MV lastmv = {0, 0}; 494 MV lastmv = {0, 0};
483 TWO_PASS *twopass = &cpi->twopass; 495 TWO_PASS *twopass = &cpi->twopass;
484 const MV zero_mv = {0, 0}; 496 const MV zero_mv = {0, 0};
485 int recon_y_stride, recon_uv_stride, uv_mb_height; 497 int recon_y_stride, recon_uv_stride, uv_mb_height;
486 498
487 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME); 499 YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
488 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME); 500 YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
489 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm); 501 YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
629 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], 641 mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
630 cm->mi_rows, cm->mi_cols); 642 cm->mi_rows, cm->mi_cols);
631 643
632 // Do intra 16x16 prediction. 644 // Do intra 16x16 prediction.
633 x->skip_encode = 0; 645 x->skip_encode = 0;
634 xd->mi[0]->mbmi.mode = DC_PRED; 646 xd->mi[0]->mbmi.mode = DC_PRED;
635 xd->mi[0]->mbmi.tx_size = use_dc_pred ? 647 xd->mi[0]->mbmi.tx_size = use_dc_pred ?
636 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4; 648 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
637 vp9_encode_intra_block_plane(x, bsize, 0); 649 vp9_encode_intra_block_plane(x, bsize, 0);
638 this_error = vpx_get_mb_ss(x->plane[0].src_diff); 650 this_error = vpx_get_mb_ss(x->plane[0].src_diff);
651
652 // Keep a record of blocks that have almost no intra error residual
653 // (i.e. are in effect completely flat and untextured in the intra
654 // domain). In natural videos this is uncommon, but it is much more
655 // common in animations, graphics and screen content, so may be used
656 // as a signal to detect these types of content.
657 if (this_error < UL_INTRA_THRESH) {
658 ++intra_skip_count;
659 } else if ((mb_col > 0) && (image_data_start_row == INVALID_ROW)) {
660 image_data_start_row = mb_row;
661 }
662
639 #if CONFIG_VP9_HIGHBITDEPTH 663 #if CONFIG_VP9_HIGHBITDEPTH
640 if (cm->use_highbitdepth) { 664 if (cm->use_highbitdepth) {
641 switch (cm->bit_depth) { 665 switch (cm->bit_depth) {
642 case VPX_BITS_8: 666 case VPX_BITS_8:
643 break; 667 break;
644 case VPX_BITS_10: 668 case VPX_BITS_10:
645 this_error >>= 4; 669 this_error >>= 4;
646 break; 670 break;
647 case VPX_BITS_12: 671 case VPX_BITS_12:
648 this_error >>= 8; 672 this_error >>= 8;
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
956 // Adjust to the next row of MBs. 980 // Adjust to the next row of MBs.
957 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols; 981 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
958 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride - 982 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
959 uv_mb_height * cm->mb_cols; 983 uv_mb_height * cm->mb_cols;
960 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride - 984 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
961 uv_mb_height * cm->mb_cols; 985 uv_mb_height * cm->mb_cols;
962 986
963 vp9_clear_system_state(); 987 vp9_clear_system_state();
964 } 988 }
965 989
990 // Clamp the image start to rows/2. This number of rows is discarded top
991 // and bottom as dead data so rows / 2 means the frame is blank.
992 if ((image_data_start_row > cm->mb_rows / 2) ||
993 (image_data_start_row == INVALID_ROW)) {
994 image_data_start_row = cm->mb_rows / 2;
995 }
996 // Exclude any image dead zone
997 if (image_data_start_row > 0) {
998 intra_skip_count =
999 MAX(0, intra_skip_count - (image_data_start_row * cm->mb_cols * 2));
1000 }
1001
966 { 1002 {
967 FIRSTPASS_STATS fps; 1003 FIRSTPASS_STATS fps;
968 // The minimum error here insures some bit allocation to frames even 1004 // The minimum error here insures some bit allocation to frames even
969 // in static regions. The allocation per MB declines for larger formats 1005 // in static regions. The allocation per MB declines for larger formats
970 // where the typical "real" energy per MB also falls. 1006 // where the typical "real" energy per MB also falls.
971 // Initial estimate here uses sqrt(mbs) to define the min_err, where the 1007 // Initial estimate here uses sqrt(mbs) to define the min_err, where the
972 // number of mbs is proportional to the image area. 1008 // number of mbs is proportional to the image area.
973 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) 1009 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
974 ? cpi->initial_mbs : cpi->common.MBs; 1010 ? cpi->initial_mbs : cpi->common.MBs;
975 const double min_err = 200 * sqrt(num_mbs); 1011 const double min_err = 200 * sqrt(num_mbs);
976 1012
977 intra_factor = intra_factor / (double)num_mbs; 1013 intra_factor = intra_factor / (double)num_mbs;
978 brightness_factor = brightness_factor / (double)num_mbs; 1014 brightness_factor = brightness_factor / (double)num_mbs;
979 fps.weight = intra_factor * brightness_factor; 1015 fps.weight = intra_factor * brightness_factor;
980 1016
981 fps.frame = cm->current_video_frame; 1017 fps.frame = cm->current_video_frame;
982 fps.spatial_layer_id = cpi->svc.spatial_layer_id; 1018 fps.spatial_layer_id = cpi->svc.spatial_layer_id;
983 fps.coded_error = (double)(coded_error >> 8) + min_err; 1019 fps.coded_error = (double)(coded_error >> 8) + min_err;
984 fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err; 1020 fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
985 fps.intra_error = (double)(intra_error >> 8) + min_err; 1021 fps.intra_error = (double)(intra_error >> 8) + min_err;
986 fps.count = 1.0; 1022 fps.count = 1.0;
987 fps.pcnt_inter = (double)intercount / num_mbs; 1023 fps.pcnt_inter = (double)intercount / num_mbs;
988 fps.pcnt_second_ref = (double)second_ref_count / num_mbs; 1024 fps.pcnt_second_ref = (double)second_ref_count / num_mbs;
989 fps.pcnt_neutral = (double)neutral_count / num_mbs; 1025 fps.pcnt_neutral = (double)neutral_count / num_mbs;
1026 fps.intra_skip_pct = (double)intra_skip_count / num_mbs;
1027 fps.inactive_zone_rows = (double)image_data_start_row;
990 1028
991 if (mvcount > 0) { 1029 if (mvcount > 0) {
992 fps.MVr = (double)sum_mvr / mvcount; 1030 fps.MVr = (double)sum_mvr / mvcount;
993 fps.mvr_abs = (double)sum_mvr_abs / mvcount; 1031 fps.mvr_abs = (double)sum_mvr_abs / mvcount;
994 fps.MVc = (double)sum_mvc / mvcount; 1032 fps.MVc = (double)sum_mvc / mvcount;
995 fps.mvc_abs = (double)sum_mvc_abs / mvcount; 1033 fps.mvc_abs = (double)sum_mvc_abs / mvcount;
996 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount; 1034 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount;
997 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount; 1035 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount;
998 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2); 1036 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
999 fps.new_mv_count = new_mv_count; 1037 fps.new_mv_count = new_mv_count;
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
1101 return fclamp(pow(error_term, power_term), 0.05, 5.0); 1139 return fclamp(pow(error_term, power_term), 0.05, 5.0);
1102 } 1140 }
1103 1141
1104 // Larger image formats are expected to be a little harder to code relatively 1142 // Larger image formats are expected to be a little harder to code relatively
1105 // given the same prediction error score. This in part at least relates to the 1143 // given the same prediction error score. This in part at least relates to the
1106 // increased size and hence coding cost of motion vectors. 1144 // increased size and hence coding cost of motion vectors.
1107 #define EDIV_SIZE_FACTOR 800 1145 #define EDIV_SIZE_FACTOR 800
1108 1146
1109 static int get_twopass_worst_quality(const VP9_COMP *cpi, 1147 static int get_twopass_worst_quality(const VP9_COMP *cpi,
1110 const double section_err, 1148 const double section_err,
1149 double inactive_zone,
1111 int section_target_bandwidth, 1150 int section_target_bandwidth,
1112 double group_weight_factor) { 1151 double group_weight_factor) {
1113 const RATE_CONTROL *const rc = &cpi->rc; 1152 const RATE_CONTROL *const rc = &cpi->rc;
1114 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1153 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1115 1154
1155 inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
1156
1116 if (section_target_bandwidth <= 0) { 1157 if (section_target_bandwidth <= 0) {
1117 return rc->worst_quality; // Highest value allowed 1158 return rc->worst_quality; // Highest value allowed
1118 } else { 1159 } else {
1119 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) 1160 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1120 ? cpi->initial_mbs : cpi->common.MBs; 1161 ? cpi->initial_mbs : cpi->common.MBs;
1121 const double err_per_mb = section_err / num_mbs; 1162 const int active_mbs = MAX(1, num_mbs - (int)(num_mbs * inactive_zone));
1163 const double av_err_per_mb = section_err / active_mbs;
1122 const double speed_term = 1.0 + 0.04 * oxcf->speed; 1164 const double speed_term = 1.0 + 0.04 * oxcf->speed;
1123 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR; 1165 const double ediv_size_correction = (double)num_mbs / EDIV_SIZE_FACTOR;
1124 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << 1166 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth <<
1125 BPER_MB_NORMBITS) / num_mbs; 1167 BPER_MB_NORMBITS) / active_mbs;
1126 1168
1127 int q; 1169 int q;
1128 int is_svc_upper_layer = 0; 1170 int is_svc_upper_layer = 0;
1129 1171
1130 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) 1172 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
1131 is_svc_upper_layer = 1; 1173 is_svc_upper_layer = 1;
1132 1174
1133 1175
1134 // Try and pick a max Q that will be high enough to encode the 1176 // Try and pick a max Q that will be high enough to encode the
1135 // content at the given rate. 1177 // content at the given rate.
1136 for (q = rc->best_quality; q < rc->worst_quality; ++q) { 1178 for (q = rc->best_quality; q < rc->worst_quality; ++q) {
1137 const double factor = 1179 const double factor =
1138 calc_correction_factor(err_per_mb, 1180 calc_correction_factor(av_err_per_mb,
1139 ERR_DIVISOR - ediv_size_correction, 1181 ERR_DIVISOR - ediv_size_correction,
1140 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : 1182 is_svc_upper_layer ? SVC_FACTOR_PT_LOW :
1141 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, 1183 FACTOR_PT_LOW, FACTOR_PT_HIGH, q,
1142 cpi->common.bit_depth); 1184 cpi->common.bit_depth);
1143 const int bits_per_mb = 1185 const int bits_per_mb =
1144 vp9_rc_bits_per_mb(INTER_FRAME, q, 1186 vp9_rc_bits_per_mb(INTER_FRAME, q,
1145 factor * speed_term * group_weight_factor, 1187 factor * speed_term * group_weight_factor,
1146 cpi->common.bit_depth); 1188 cpi->common.bit_depth);
1147 if (bits_per_mb <= target_norm_bits_per_mb) 1189 if (bits_per_mb <= target_norm_bits_per_mb)
1148 break; 1190 break;
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
1407 double this_frame_mv_in_out, 1449 double this_frame_mv_in_out,
1408 double max_boost) { 1450 double max_boost) {
1409 double frame_boost; 1451 double frame_boost;
1410 const double lq = 1452 const double lq =
1411 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME], 1453 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME],
1412 cpi->common.bit_depth); 1454 cpi->common.bit_depth);
1413 const double boost_q_correction = MIN((0.5 + (lq * 0.015)), 1.5); 1455 const double boost_q_correction = MIN((0.5 + (lq * 0.015)), 1.5);
1414 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) 1456 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1415 ? cpi->initial_mbs : cpi->common.MBs; 1457 ? cpi->initial_mbs : cpi->common.MBs;
1416 1458
1459 // TODO(paulwilkins): correct for dead zone
1460
1417 // Underlying boost factor is based on inter error ratio. 1461 // Underlying boost factor is based on inter error ratio.
1418 frame_boost = (BASELINE_ERR_PER_MB * num_mbs) / 1462 frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
1419 DOUBLE_DIVIDE_CHECK(this_frame->coded_error); 1463 DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1420 frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction; 1464 frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction;
1421 1465
1422 // Increase boost for frames where new data coming into frame (e.g. zoom out). 1466 // Increase boost for frames where new data coming into frame (e.g. zoom out).
1423 // Slightly reduce boost if there is a net balance of motion out of the frame 1467 // Slightly reduce boost if there is a net balance of motion out of the frame
1424 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. 1468 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1425 if (this_frame_mv_in_out > 0.0) 1469 if (this_frame_mv_in_out > 0.0)
1426 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); 1470 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
1772 FIRSTPASS_STATS next_frame; 1816 FIRSTPASS_STATS next_frame;
1773 const FIRSTPASS_STATS *const start_pos = twopass->stats_in; 1817 const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
1774 int i; 1818 int i;
1775 1819
1776 double boost_score = 0.0; 1820 double boost_score = 0.0;
1777 double old_boost_score = 0.0; 1821 double old_boost_score = 0.0;
1778 double gf_group_err = 0.0; 1822 double gf_group_err = 0.0;
1779 #if GROUP_ADAPTIVE_MAXQ 1823 #if GROUP_ADAPTIVE_MAXQ
1780 double gf_group_raw_error = 0.0; 1824 double gf_group_raw_error = 0.0;
1781 #endif 1825 #endif
1826 double gf_group_skip_pct = 0.0;
1827 double gf_group_inactive_zone_rows = 0.0;
1782 double gf_first_frame_err = 0.0; 1828 double gf_first_frame_err = 0.0;
1783 double mod_frame_err = 0.0; 1829 double mod_frame_err = 0.0;
1784 1830
1785 double mv_ratio_accumulator = 0.0; 1831 double mv_ratio_accumulator = 0.0;
1786 double decay_accumulator = 1.0; 1832 double decay_accumulator = 1.0;
1787 double zero_motion_accumulator = 1.0; 1833 double zero_motion_accumulator = 1.0;
1788 1834
1789 double loop_decay_rate = 1.00; 1835 double loop_decay_rate = 1.00;
1790 double last_loop_decay_rate = 1.00; 1836 double last_loop_decay_rate = 1.00;
1791 1837
(...skipping 29 matching lines...) Expand all
1821 // the GF frame error if we code a normal gf. 1867 // the GF frame error if we code a normal gf.
1822 gf_first_frame_err = mod_frame_err; 1868 gf_first_frame_err = mod_frame_err;
1823 1869
1824 // If this is a key frame or the overlay from a previous arf then 1870 // If this is a key frame or the overlay from a previous arf then
1825 // the error score / cost of this frame has already been accounted for. 1871 // the error score / cost of this frame has already been accounted for.
1826 if (is_key_frame || rc->source_alt_ref_active) { 1872 if (is_key_frame || rc->source_alt_ref_active) {
1827 gf_group_err -= gf_first_frame_err; 1873 gf_group_err -= gf_first_frame_err;
1828 #if GROUP_ADAPTIVE_MAXQ 1874 #if GROUP_ADAPTIVE_MAXQ
1829 gf_group_raw_error -= this_frame->coded_error; 1875 gf_group_raw_error -= this_frame->coded_error;
1830 #endif 1876 #endif
1877 gf_group_skip_pct -= this_frame->intra_skip_pct;
1878 gf_group_inactive_zone_rows -= this_frame->inactive_zone_rows;
1831 } 1879 }
1832 1880
1833 // Motion breakout threshold for loop below depends on image size. 1881 // Motion breakout threshold for loop below depends on image size.
1834 mv_ratio_accumulator_thresh = 1882 mv_ratio_accumulator_thresh =
1835 (cpi->initial_height + cpi->initial_width) / 4.0; 1883 (cpi->initial_height + cpi->initial_width) / 4.0;
1836 1884
1837 // Set a maximum and minimum interval for the GF group. 1885 // Set a maximum and minimum interval for the GF group.
1838 // If the image appears almost completely static we can extend beyond this. 1886 // If the image appears almost completely static we can extend beyond this.
1839 { 1887 {
1840 int int_max_q = 1888 int int_max_q =
(...skipping 24 matching lines...) Expand all
1865 i = 0; 1913 i = 0;
1866 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) { 1914 while (i < rc->static_scene_max_gf_interval && i < rc->frames_to_key) {
1867 ++i; 1915 ++i;
1868 1916
1869 // Accumulate error score of frames in this gf group. 1917 // Accumulate error score of frames in this gf group.
1870 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame); 1918 mod_frame_err = calculate_modified_err(twopass, oxcf, this_frame);
1871 gf_group_err += mod_frame_err; 1919 gf_group_err += mod_frame_err;
1872 #if GROUP_ADAPTIVE_MAXQ 1920 #if GROUP_ADAPTIVE_MAXQ
1873 gf_group_raw_error += this_frame->coded_error; 1921 gf_group_raw_error += this_frame->coded_error;
1874 #endif 1922 #endif
1923 gf_group_skip_pct += this_frame->intra_skip_pct;
1924 gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
1875 1925
1876 if (EOF == input_stats(twopass, &next_frame)) 1926 if (EOF == input_stats(twopass, &next_frame))
1877 break; 1927 break;
1878 1928
1879 // Test for the case where there is a brief flash but the prediction 1929 // Test for the case where there is a brief flash but the prediction
1880 // quality back to an earlier frame is then restored. 1930 // quality back to an earlier frame is then restored.
1881 flash_detected = detect_flash(twopass, 0); 1931 flash_detected = detect_flash(twopass, 0);
1882 1932
1883 // Update the motion related elements to the boost calculation. 1933 // Update the motion related elements to the boost calculation.
1884 accumulate_frame_motion_stats(&next_frame, 1934 accumulate_frame_motion_stats(&next_frame,
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
1967 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1; 2017 int count = (1 << (cpi->svc.number_temporal_layers - 1)) - 1;
1968 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count); 2018 int new_gf_interval = (rc->baseline_gf_interval + count) & (~count);
1969 int j; 2019 int j;
1970 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) { 2020 for (j = 0; j < new_gf_interval - rc->baseline_gf_interval; ++j) {
1971 if (EOF == input_stats(twopass, this_frame)) 2021 if (EOF == input_stats(twopass, this_frame))
1972 break; 2022 break;
1973 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame); 2023 gf_group_err += calculate_modified_err(twopass, oxcf, this_frame);
1974 #if GROUP_ADAPTIVE_MAXQ 2024 #if GROUP_ADAPTIVE_MAXQ
1975 gf_group_raw_error += this_frame->coded_error; 2025 gf_group_raw_error += this_frame->coded_error;
1976 #endif 2026 #endif
2027 gf_group_skip_pct += this_frame->intra_skip_pct;
2028 gf_group_inactive_zone_rows += this_frame->inactive_zone_rows;
1977 } 2029 }
1978 rc->baseline_gf_interval = new_gf_interval; 2030 rc->baseline_gf_interval = new_gf_interval;
1979 } 2031 }
1980 2032
1981 rc->frames_till_gf_update_due = rc->baseline_gf_interval; 2033 rc->frames_till_gf_update_due = rc->baseline_gf_interval;
1982 2034
1983 // Reset the file position. 2035 // Reset the file position.
1984 reset_fpf_position(twopass, start_pos); 2036 reset_fpf_position(twopass, start_pos);
1985 2037
1986 // Calculate the bits to be allocated to the gf/arf group as a whole 2038 // Calculate the bits to be allocated to the gf/arf group as a whole
1987 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err); 2039 gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
1988 2040
1989 #if GROUP_ADAPTIVE_MAXQ 2041 #if GROUP_ADAPTIVE_MAXQ
1990 // Calculate an estimate of the maxq needed for the group. 2042 // Calculate an estimate of the maxq needed for the group.
1991 // We are more agressive about correcting for sections 2043 // We are more agressive about correcting for sections
1992 // where there could be significant overshoot than for easier 2044 // where there could be significant overshoot than for easier
1993 // sections where we do not wish to risk creating an overshoot 2045 // sections where we do not wish to risk creating an overshoot
1994 // of the allocated bit budget. 2046 // of the allocated bit budget.
1995 if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) { 2047 if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) {
1996 const int vbr_group_bits_per_frame = 2048 const int vbr_group_bits_per_frame =
1997 (int)(gf_group_bits / rc->baseline_gf_interval); 2049 (int)(gf_group_bits / rc->baseline_gf_interval);
1998 const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval; 2050 const double group_av_err = gf_group_raw_error / rc->baseline_gf_interval;
2051 const double group_av_skip_pct =
2052 gf_group_skip_pct / rc->baseline_gf_interval;
2053 const double group_av_inactive_zone =
2054 ((gf_group_inactive_zone_rows * 2) /
2055 (rc->baseline_gf_interval * (double)cm->mb_rows));
2056
1999 int tmp_q; 2057 int tmp_q;
2000 // rc factor is a weight factor that corrects for local rate control drift. 2058 // rc factor is a weight factor that corrects for local rate control drift.
2001 double rc_factor = 1.0; 2059 double rc_factor = 1.0;
2002 if (rc->rate_error_estimate > 0) { 2060 if (rc->rate_error_estimate > 0) {
2003 rc_factor = MAX(RC_FACTOR_MIN, 2061 rc_factor = MAX(RC_FACTOR_MIN,
2004 (double)(100 - rc->rate_error_estimate) / 100.0); 2062 (double)(100 - rc->rate_error_estimate) / 100.0);
2005 } else { 2063 } else {
2006 rc_factor = MIN(RC_FACTOR_MAX, 2064 rc_factor = MIN(RC_FACTOR_MAX,
2007 (double)(100 - rc->rate_error_estimate) / 100.0); 2065 (double)(100 - rc->rate_error_estimate) / 100.0);
2008 } 2066 }
2009 tmp_q = 2067 tmp_q =
2010 get_twopass_worst_quality(cpi, group_av_err, vbr_group_bits_per_frame, 2068 get_twopass_worst_quality(cpi, group_av_err,
2069 (group_av_skip_pct + group_av_inactive_zone),
2070 vbr_group_bits_per_frame,
2011 twopass->kfgroup_inter_fraction * rc_factor); 2071 twopass->kfgroup_inter_fraction * rc_factor);
2012 twopass->active_worst_quality = 2072 twopass->active_worst_quality =
2013 MAX(tmp_q, twopass->active_worst_quality >> 1); 2073 MAX(tmp_q, twopass->active_worst_quality >> 1);
2014 } 2074 }
2015 #endif 2075 #endif
2016 2076
2017 // Calculate the extra bits to be used for boosted frame(s) 2077 // Calculate the extra bits to be used for boosted frame(s)
2018 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval, 2078 gf_arf_bits = calculate_boost_bits(rc->baseline_gf_interval,
2019 rc->gfu_boost, gf_group_bits); 2079 rc->gfu_boost, gf_group_bits);
2020 2080
(...skipping 518 matching lines...) Expand 10 before | Expand all | Expand 10 after
2539 2599
2540 vp9_clear_system_state(); 2600 vp9_clear_system_state();
2541 2601
2542 if (cpi->oxcf.rc_mode == VPX_Q) { 2602 if (cpi->oxcf.rc_mode == VPX_Q) {
2543 twopass->active_worst_quality = cpi->oxcf.cq_level; 2603 twopass->active_worst_quality = cpi->oxcf.cq_level;
2544 } else if (cm->current_video_frame == 0 || 2604 } else if (cm->current_video_frame == 0 ||
2545 (lc != NULL && lc->current_video_frame_in_layer == 0)) { 2605 (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2546 // Special case code for first frame. 2606 // Special case code for first frame.
2547 const int section_target_bandwidth = (int)(twopass->bits_left / 2607 const int section_target_bandwidth = (int)(twopass->bits_left /
2548 frames_left); 2608 frames_left);
2609 const double section_length = twopass->total_left_stats.count;
2549 const double section_error = 2610 const double section_error =
2550 twopass->total_left_stats.coded_error / twopass->total_left_stats.count; 2611 twopass->total_left_stats.coded_error / section_length;
2612 const double section_intra_skip =
2613 twopass->total_left_stats.intra_skip_pct / section_length;
2614 const double section_inactive_zone =
2615 (twopass->total_left_stats.inactive_zone_rows * 2) /
2616 ((double)cm->mb_rows * section_length);
2551 const int tmp_q = 2617 const int tmp_q =
2552 get_twopass_worst_quality(cpi, section_error, 2618 get_twopass_worst_quality(cpi, section_error,
2619 section_intra_skip + section_inactive_zone,
2553 section_target_bandwidth, DEFAULT_GRP_WEIGHT); 2620 section_target_bandwidth, DEFAULT_GRP_WEIGHT);
2554 2621
2555 twopass->active_worst_quality = tmp_q; 2622 twopass->active_worst_quality = tmp_q;
2556 twopass->baseline_active_worst_quality = tmp_q; 2623 twopass->baseline_active_worst_quality = tmp_q;
2557 rc->ni_av_qi = tmp_q; 2624 rc->ni_av_qi = tmp_q;
2558 rc->last_q[INTER_FRAME] = tmp_q; 2625 rc->last_q[INTER_FRAME] = tmp_q;
2559 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth); 2626 rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
2560 rc->avg_frame_qindex[INTER_FRAME] = tmp_q; 2627 rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
2561 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2; 2628 rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2;
2562 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; 2629 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
2563 } 2630 }
2564 vp9_zero(this_frame); 2631 vp9_zero(this_frame);
2565 if (EOF == input_stats(twopass, &this_frame)) 2632 if (EOF == input_stats(twopass, &this_frame))
2566 return; 2633 return;
2567 2634
2635 // Set the frame content type flag.
2636 if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH)
2637 twopass->fr_content_type = FC_GRAPHICS_ANIMATION;
2638 else
2639 twopass->fr_content_type = FC_NORMAL;
2640
2568 // Keyframe and section processing. 2641 // Keyframe and section processing.
2569 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) { 2642 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2570 FIRSTPASS_STATS this_frame_copy; 2643 FIRSTPASS_STATS this_frame_copy;
2571 this_frame_copy = this_frame; 2644 this_frame_copy = this_frame;
2572 // Define next KF group and assign bits to it. 2645 // Define next KF group and assign bits to it.
2573 find_next_key_frame(cpi, &this_frame); 2646 find_next_key_frame(cpi, &this_frame);
2574 this_frame = this_frame_copy; 2647 this_frame = this_frame_copy;
2575 } else { 2648 } else {
2576 cm->frame_type = INTER_FRAME; 2649 cm->frame_type = INTER_FRAME;
2577 } 2650 }
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after
2740 minq_adj_limit - twopass->extend_minq); 2813 minq_adj_limit - twopass->extend_minq);
2741 } else if (rc->vbr_bits_off_target_fast) { 2814 } else if (rc->vbr_bits_off_target_fast) {
2742 twopass->extend_minq_fast = MIN(twopass->extend_minq_fast, 2815 twopass->extend_minq_fast = MIN(twopass->extend_minq_fast,
2743 minq_adj_limit - twopass->extend_minq); 2816 minq_adj_limit - twopass->extend_minq);
2744 } else { 2817 } else {
2745 twopass->extend_minq_fast = 0; 2818 twopass->extend_minq_fast = 0;
2746 } 2819 }
2747 } 2820 }
2748 } 2821 }
2749 } 2822 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_firstpass.h ('k') | source/libvpx/vp9/encoder/vp9_mcomp.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698