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

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

Issue 756673003: libvpx: Pull from upstream (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 6 years 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 | Annotate | Revision Log
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_firstpass.h ('k') | source/libvpx/vp9/encoder/vp9_mcomp.h » ('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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 #define FIRST_PASS_Q 10.0 46 #define FIRST_PASS_Q 10.0
47 #define GF_MAX_BOOST 96.0 47 #define GF_MAX_BOOST 96.0
48 #define INTRA_MODE_PENALTY 1024 48 #define INTRA_MODE_PENALTY 1024
49 #define KF_MAX_BOOST 128.0 49 #define KF_MAX_BOOST 128.0
50 #define MIN_ARF_GF_BOOST 240 50 #define MIN_ARF_GF_BOOST 240
51 #define MIN_DECAY_FACTOR 0.01 51 #define MIN_DECAY_FACTOR 0.01
52 #define MIN_GF_INTERVAL 4 52 #define MIN_GF_INTERVAL 4
53 #define MIN_KF_BOOST 300 53 #define MIN_KF_BOOST 300
54 #define NEW_MV_MODE_PENALTY 32 54 #define NEW_MV_MODE_PENALTY 32
55 #define SVC_FACTOR_PT_LOW 0.45 55 #define SVC_FACTOR_PT_LOW 0.45
56 #define DARK_THRESH 64
56 57
57 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001) 58 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x) - 0.000001 : (x) + 0.000001)
58 59
59 #if ARF_STATS_OUTPUT 60 #if ARF_STATS_OUTPUT
60 unsigned int arf_count = 0; 61 unsigned int arf_count = 0;
61 #endif 62 #endif
62 63
63 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { 64 static void swap_yv12(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) {
64 YV12_BUFFER_CONFIG temp = *a; 65 YV12_BUFFER_CONFIG temp = *a;
65 *a = *b; 66 *a = *b;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 pkt.data.twopass_stats.buf = stats; 100 pkt.data.twopass_stats.buf = stats;
100 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS); 101 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
101 vpx_codec_pkt_list_add(pktlist, &pkt); 102 vpx_codec_pkt_list_add(pktlist, &pkt);
102 103
103 // TEMP debug code 104 // TEMP debug code
104 #if OUTPUT_FPF 105 #if OUTPUT_FPF
105 { 106 {
106 FILE *fpfile; 107 FILE *fpfile;
107 fpfile = fopen("firstpass.stt", "a"); 108 fpfile = fopen("firstpass.stt", "a");
108 109
109 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.4f %12.4f" 110 fprintf(fpfile, "%12.0f %12.4f %12.0f %12.0f %12.0f %12.4f %12.4f"
110 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f" 111 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
111 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n", 112 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
112 stats->frame, 113 stats->frame,
114 stats->weight,
113 stats->intra_error, 115 stats->intra_error,
114 stats->coded_error, 116 stats->coded_error,
115 stats->sr_coded_error, 117 stats->sr_coded_error,
116 stats->pcnt_inter, 118 stats->pcnt_inter,
117 stats->pcnt_motion, 119 stats->pcnt_motion,
118 stats->pcnt_second_ref, 120 stats->pcnt_second_ref,
119 stats->pcnt_neutral, 121 stats->pcnt_neutral,
120 stats->MVr, 122 stats->MVr,
121 stats->mvr_abs, 123 stats->mvr_abs,
122 stats->MVc, 124 stats->MVc,
123 stats->mvc_abs, 125 stats->mvc_abs,
124 stats->MVrv, 126 stats->MVrv,
125 stats->MVcv, 127 stats->MVcv,
126 stats->mv_in_out_count, 128 stats->mv_in_out_count,
127 stats->new_mv_count, 129 stats->new_mv_count,
128 stats->count, 130 stats->count,
129 stats->duration); 131 stats->duration);
130 fclose(fpfile); 132 fclose(fpfile);
131 } 133 }
132 #endif 134 #endif
133 } 135 }
134 136
135 #if CONFIG_FP_MB_STATS 137 #if CONFIG_FP_MB_STATS
136 static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm, 138 static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm,
137 struct vpx_codec_pkt_list *pktlist) { 139 struct vpx_codec_pkt_list *pktlist) {
138 struct vpx_codec_cx_pkt pkt; 140 struct vpx_codec_cx_pkt pkt;
139 pkt.kind = VPX_CODEC_FPMB_STATS_PKT; 141 pkt.kind = VPX_CODEC_FPMB_STATS_PKT;
140 pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats; 142 pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats;
141 pkt.data.firstpass_mb_stats.sz = cm->MBs * sizeof(uint8_t); 143 pkt.data.firstpass_mb_stats.sz = cm->initial_mbs * sizeof(uint8_t);
142 vpx_codec_pkt_list_add(pktlist, &pkt); 144 vpx_codec_pkt_list_add(pktlist, &pkt);
143 } 145 }
144 #endif 146 #endif
145 147
146 static void zero_stats(FIRSTPASS_STATS *section) { 148 static void zero_stats(FIRSTPASS_STATS *section) {
147 section->frame = 0.0; 149 section->frame = 0.0;
150 section->weight = 0.0;
148 section->intra_error = 0.0; 151 section->intra_error = 0.0;
149 section->coded_error = 0.0; 152 section->coded_error = 0.0;
150 section->sr_coded_error = 0.0; 153 section->sr_coded_error = 0.0;
151 section->pcnt_inter = 0.0; 154 section->pcnt_inter = 0.0;
152 section->pcnt_motion = 0.0; 155 section->pcnt_motion = 0.0;
153 section->pcnt_second_ref = 0.0; 156 section->pcnt_second_ref = 0.0;
154 section->pcnt_neutral = 0.0; 157 section->pcnt_neutral = 0.0;
155 section->MVr = 0.0; 158 section->MVr = 0.0;
156 section->mvr_abs = 0.0; 159 section->mvr_abs = 0.0;
157 section->MVc = 0.0; 160 section->MVc = 0.0;
158 section->mvc_abs = 0.0; 161 section->mvc_abs = 0.0;
159 section->MVrv = 0.0; 162 section->MVrv = 0.0;
160 section->MVcv = 0.0; 163 section->MVcv = 0.0;
161 section->mv_in_out_count = 0.0; 164 section->mv_in_out_count = 0.0;
162 section->new_mv_count = 0.0; 165 section->new_mv_count = 0.0;
163 section->count = 0.0; 166 section->count = 0.0;
164 section->duration = 1.0; 167 section->duration = 1.0;
165 section->spatial_layer_id = 0; 168 section->spatial_layer_id = 0;
166 } 169 }
167 170
168 static void accumulate_stats(FIRSTPASS_STATS *section, 171 static void accumulate_stats(FIRSTPASS_STATS *section,
169 const FIRSTPASS_STATS *frame) { 172 const FIRSTPASS_STATS *frame) {
170 section->frame += frame->frame; 173 section->frame += frame->frame;
174 section->weight += frame->weight;
171 section->spatial_layer_id = frame->spatial_layer_id; 175 section->spatial_layer_id = frame->spatial_layer_id;
172 section->intra_error += frame->intra_error; 176 section->intra_error += frame->intra_error;
173 section->coded_error += frame->coded_error; 177 section->coded_error += frame->coded_error;
174 section->sr_coded_error += frame->sr_coded_error; 178 section->sr_coded_error += frame->sr_coded_error;
175 section->pcnt_inter += frame->pcnt_inter; 179 section->pcnt_inter += frame->pcnt_inter;
176 section->pcnt_motion += frame->pcnt_motion; 180 section->pcnt_motion += frame->pcnt_motion;
177 section->pcnt_second_ref += frame->pcnt_second_ref; 181 section->pcnt_second_ref += frame->pcnt_second_ref;
178 section->pcnt_neutral += frame->pcnt_neutral; 182 section->pcnt_neutral += frame->pcnt_neutral;
179 section->MVr += frame->MVr; 183 section->MVr += frame->MVr;
180 section->mvr_abs += frame->mvr_abs; 184 section->mvr_abs += frame->mvr_abs;
181 section->MVc += frame->MVc; 185 section->MVc += frame->MVc;
182 section->mvc_abs += frame->mvc_abs; 186 section->mvc_abs += frame->mvc_abs;
183 section->MVrv += frame->MVrv; 187 section->MVrv += frame->MVrv;
184 section->MVcv += frame->MVcv; 188 section->MVcv += frame->MVcv;
185 section->mv_in_out_count += frame->mv_in_out_count; 189 section->mv_in_out_count += frame->mv_in_out_count;
186 section->new_mv_count += frame->new_mv_count; 190 section->new_mv_count += frame->new_mv_count;
187 section->count += frame->count; 191 section->count += frame->count;
188 section->duration += frame->duration; 192 section->duration += frame->duration;
189 } 193 }
190 194
191 static void subtract_stats(FIRSTPASS_STATS *section, 195 static void subtract_stats(FIRSTPASS_STATS *section,
192 const FIRSTPASS_STATS *frame) { 196 const FIRSTPASS_STATS *frame) {
193 section->frame -= frame->frame; 197 section->frame -= frame->frame;
198 section->weight -= frame->weight;
194 section->intra_error -= frame->intra_error; 199 section->intra_error -= frame->intra_error;
195 section->coded_error -= frame->coded_error; 200 section->coded_error -= frame->coded_error;
196 section->sr_coded_error -= frame->sr_coded_error; 201 section->sr_coded_error -= frame->sr_coded_error;
197 section->pcnt_inter -= frame->pcnt_inter; 202 section->pcnt_inter -= frame->pcnt_inter;
198 section->pcnt_motion -= frame->pcnt_motion; 203 section->pcnt_motion -= frame->pcnt_motion;
199 section->pcnt_second_ref -= frame->pcnt_second_ref; 204 section->pcnt_second_ref -= frame->pcnt_second_ref;
200 section->pcnt_neutral -= frame->pcnt_neutral; 205 section->pcnt_neutral -= frame->pcnt_neutral;
201 section->MVr -= frame->MVr; 206 section->MVr -= frame->MVr;
202 section->mvr_abs -= frame->mvr_abs; 207 section->mvr_abs -= frame->mvr_abs;
203 section->MVc -= frame->MVc; 208 section->MVc -= frame->MVc;
204 section->mvc_abs -= frame->mvc_abs; 209 section->mvc_abs -= frame->mvc_abs;
205 section->MVrv -= frame->MVrv; 210 section->MVrv -= frame->MVrv;
206 section->MVcv -= frame->MVcv; 211 section->MVcv -= frame->MVcv;
207 section->mv_in_out_count -= frame->mv_in_out_count; 212 section->mv_in_out_count -= frame->mv_in_out_count;
208 section->new_mv_count -= frame->new_mv_count; 213 section->new_mv_count -= frame->new_mv_count;
209 section->count -= frame->count; 214 section->count -= frame->count;
210 section->duration -= frame->duration; 215 section->duration -= frame->duration;
211 } 216 }
212 217
213 218
214 // Calculate a modified Error used in distributing bits between easier and 219 // Calculate a modified Error used in distributing bits between easier and
215 // harder frames. 220 // harder frames.
216 static double calculate_modified_err(const TWO_PASS *twopass, 221 static double calculate_modified_err(const TWO_PASS *twopass,
217 const VP9EncoderConfig *oxcf, 222 const VP9EncoderConfig *oxcf,
218 const FIRSTPASS_STATS *this_frame) { 223 const FIRSTPASS_STATS *this_frame) {
219 const FIRSTPASS_STATS *const stats = &twopass->total_stats; 224 const FIRSTPASS_STATS *const stats = &twopass->total_stats;
220 const double av_err = stats->coded_error / stats->count; 225 const double av_weight = stats->weight / stats->count;
221 const double modified_error = av_err * 226 const double av_err = (stats->coded_error * av_weight) / stats->count;
222 pow(this_frame->coded_error / DOUBLE_DIVIDE_CHECK(av_err), 227 const double modified_error =
223 oxcf->two_pass_vbrbias / 100.0); 228 av_err * pow(this_frame->coded_error * this_frame->weight /
229 DOUBLE_DIVIDE_CHECK(av_err), oxcf->two_pass_vbrbias / 100.0);
224 return fclamp(modified_error, 230 return fclamp(modified_error,
225 twopass->modified_error_min, twopass->modified_error_max); 231 twopass->modified_error_min, twopass->modified_error_max);
226 } 232 }
227 233
228 // This function returns the maximum target rate per frame. 234 // This function returns the maximum target rate per frame.
229 static int frame_max_bits(const RATE_CONTROL *rc, 235 static int frame_max_bits(const RATE_CONTROL *rc,
230 const VP9EncoderConfig *oxcf) { 236 const VP9EncoderConfig *oxcf) {
231 int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth * 237 int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
232 (int64_t)oxcf->two_pass_vbrmax_section) / 100; 238 (int64_t)oxcf->two_pass_vbrmax_section) / 100;
233 if (max_bits < 0) 239 if (max_bits < 0)
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 int bd) { 331 int bd) {
326 unsigned int sse; 332 unsigned int sse;
327 const vp9_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd); 333 const vp9_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
328 fn(src->buf, src->stride, ref->buf, ref->stride, &sse); 334 fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
329 return sse; 335 return sse;
330 } 336 }
331 #endif // CONFIG_VP9_HIGHBITDEPTH 337 #endif // CONFIG_VP9_HIGHBITDEPTH
332 338
333 // Refine the motion search range according to the frame dimension 339 // Refine the motion search range according to the frame dimension
334 // for first pass test. 340 // for first pass test.
335 static int get_search_range(const VP9_COMMON *cm) { 341 static int get_search_range(const VP9_COMP *cpi) {
336 int sr = 0; 342 int sr = 0;
337 const int dim = MIN(cm->width, cm->height); 343 const int dim = MIN(cpi->initial_width, cpi->initial_height);
338 344
339 while ((dim << sr) < MAX_FULL_PEL_VAL) 345 while ((dim << sr) < MAX_FULL_PEL_VAL)
340 ++sr; 346 ++sr;
341 return sr; 347 return sr;
342 } 348 }
343 349
344 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x, 350 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
345 const MV *ref_mv, MV *best_mv, 351 const MV *ref_mv, MV *best_mv,
346 int *best_motion_err) { 352 int *best_motion_err) {
347 MACROBLOCKD *const xd = &x->e_mbd; 353 MACROBLOCKD *const xd = &x->e_mbd;
348 MV tmp_mv = {0, 0}; 354 MV tmp_mv = {0, 0};
349 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3}; 355 MV ref_mv_full = {ref_mv->row >> 3, ref_mv->col >> 3};
350 int num00, tmp_err, n; 356 int num00, tmp_err, n;
351 const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type; 357 const BLOCK_SIZE bsize = xd->mi[0].src_mi->mbmi.sb_type;
352 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize]; 358 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
353 const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY; 359 const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
354 360
355 int step_param = 3; 361 int step_param = 3;
356 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param; 362 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
357 const int sr = get_search_range(&cpi->common); 363 const int sr = get_search_range(cpi);
358 step_param += sr; 364 step_param += sr;
359 further_steps -= sr; 365 further_steps -= sr;
360 366
361 // Override the default variance function to use MSE. 367 // Override the default variance function to use MSE.
362 v_fn_ptr.vf = get_block_variance_fn(bsize); 368 v_fn_ptr.vf = get_block_variance_fn(bsize);
363 #if CONFIG_VP9_HIGHBITDEPTH 369 #if CONFIG_VP9_HIGHBITDEPTH
364 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { 370 if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
365 v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd); 371 v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd);
366 } 372 }
367 #endif // CONFIG_VP9_HIGHBITDEPTH 373 #endif // CONFIG_VP9_HIGHBITDEPTH
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 const int intrapenalty = INTRA_MODE_PENALTY; 479 const int intrapenalty = INTRA_MODE_PENALTY;
474 int neutral_count = 0; 480 int neutral_count = 0;
475 int new_mv_count = 0; 481 int new_mv_count = 0;
476 int sum_in_vectors = 0; 482 int sum_in_vectors = 0;
477 MV lastmv = {0, 0}; 483 MV lastmv = {0, 0};
478 TWO_PASS *twopass = &cpi->twopass; 484 TWO_PASS *twopass = &cpi->twopass;
479 const MV zero_mv = {0, 0}; 485 const MV zero_mv = {0, 0};
480 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12; 486 const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
481 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ? 487 LAYER_CONTEXT *const lc = is_two_pass_svc(cpi) ?
482 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL; 488 &cpi->svc.layer_context[cpi->svc.spatial_layer_id] : NULL;
489 double intra_factor;
490 double brightness_factor;
483 491
484 #if CONFIG_FP_MB_STATS 492 #if CONFIG_FP_MB_STATS
485 if (cpi->use_fp_mb_stats) { 493 if (cpi->use_fp_mb_stats) {
486 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->MBs); 494 vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs);
487 } 495 }
488 #endif 496 #endif
489 497
490 vp9_clear_system_state(); 498 vp9_clear_system_state();
491 499
500 intra_factor = 0.0;
501 brightness_factor = 0.0;
502
492 set_first_pass_params(cpi); 503 set_first_pass_params(cpi);
493 vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth)); 504 vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
494 505
495 if (lc != NULL) { 506 if (lc != NULL) {
496 twopass = &lc->twopass; 507 twopass = &lc->twopass;
497 508
498 cpi->lst_fb_idx = cpi->svc.spatial_layer_id; 509 cpi->lst_fb_idx = cpi->svc.spatial_layer_id;
499 cpi->ref_frame_flags = VP9_LAST_FLAG; 510 cpi->ref_frame_flags = VP9_LAST_FLAG;
500 511
501 if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id < 512 if (cpi->svc.number_spatial_layers + cpi->svc.spatial_layer_id <
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 589
579 // Set up limit values for motion vectors to prevent them extending 590 // Set up limit values for motion vectors to prevent them extending
580 // outside the UMV borders. 591 // outside the UMV borders.
581 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16); 592 x->mv_row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
582 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16) 593 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
583 + BORDER_MV_PIXELS_B16; 594 + BORDER_MV_PIXELS_B16;
584 595
585 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) { 596 for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
586 int this_error; 597 int this_error;
587 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row); 598 const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
588 double error_weight = 1.0;
589 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col); 599 const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
600 double log_intra;
601 int level_sample;
602
590 #if CONFIG_FP_MB_STATS 603 #if CONFIG_FP_MB_STATS
591 const int mb_index = mb_row * cm->mb_cols + mb_col; 604 const int mb_index = mb_row * cm->mb_cols + mb_col;
592 #endif 605 #endif
593 606
594 vp9_clear_system_state(); 607 vp9_clear_system_state();
595 608
596 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset; 609 xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
597 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset; 610 xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
598 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset; 611 xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
599 xd->left_available = (mb_col != 0); 612 xd->left_available = (mb_col != 0);
600 xd->mi[0].src_mi->mbmi.sb_type = bsize; 613 xd->mi[0].src_mi->mbmi.sb_type = bsize;
601 xd->mi[0].src_mi->mbmi.ref_frame[0] = INTRA_FRAME; 614 xd->mi[0].src_mi->mbmi.ref_frame[0] = INTRA_FRAME;
602 set_mi_row_col(xd, &tile, 615 set_mi_row_col(xd, &tile,
603 mb_row << 1, num_8x8_blocks_high_lookup[bsize], 616 mb_row << 1, num_8x8_blocks_high_lookup[bsize],
604 mb_col << 1, num_8x8_blocks_wide_lookup[bsize], 617 mb_col << 1, num_8x8_blocks_wide_lookup[bsize],
605 cm->mi_rows, cm->mi_cols); 618 cm->mi_rows, cm->mi_cols);
606 619
607 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
608 const int energy = vp9_block_energy(cpi, x, bsize);
609 error_weight = vp9_vaq_inv_q_ratio(energy);
610 }
611
612 // Do intra 16x16 prediction. 620 // Do intra 16x16 prediction.
613 x->skip_encode = 0; 621 x->skip_encode = 0;
614 xd->mi[0].src_mi->mbmi.mode = DC_PRED; 622 xd->mi[0].src_mi->mbmi.mode = DC_PRED;
615 xd->mi[0].src_mi->mbmi.tx_size = use_dc_pred ? 623 xd->mi[0].src_mi->mbmi.tx_size = use_dc_pred ?
616 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4; 624 (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
617 vp9_encode_intra_block_plane(x, bsize, 0); 625 vp9_encode_intra_block_plane(x, bsize, 0);
618 this_error = vp9_get_mb_ss(x->plane[0].src_diff); 626 this_error = vp9_get_mb_ss(x->plane[0].src_diff);
619 #if CONFIG_VP9_HIGHBITDEPTH 627 #if CONFIG_VP9_HIGHBITDEPTH
620 if (cm->use_highbitdepth) { 628 if (cm->use_highbitdepth) {
621 switch (cm->bit_depth) { 629 switch (cm->bit_depth) {
622 case VPX_BITS_8: 630 case VPX_BITS_8:
623 break; 631 break;
624 case VPX_BITS_10: 632 case VPX_BITS_10:
625 this_error >>= 4; 633 this_error >>= 4;
626 break; 634 break;
627 case VPX_BITS_12: 635 case VPX_BITS_12:
628 this_error >>= 8; 636 this_error >>= 8;
629 break; 637 break;
630 default: 638 default:
631 assert(0 && "cm->bit_depth should be VPX_BITS_8, " 639 assert(0 && "cm->bit_depth should be VPX_BITS_8, "
632 "VPX_BITS_10 or VPX_BITS_12"); 640 "VPX_BITS_10 or VPX_BITS_12");
633 return; 641 return;
634 } 642 }
635 } 643 }
636 #endif // CONFIG_VP9_HIGHBITDEPTH 644 #endif // CONFIG_VP9_HIGHBITDEPTH
637 645
638 if (cpi->oxcf.aq_mode == VARIANCE_AQ) { 646 vp9_clear_system_state();
639 vp9_clear_system_state(); 647 log_intra = log(this_error + 1.0);
640 this_error = (int)(this_error * error_weight); 648 if (log_intra < 10.0)
641 } 649 intra_factor += 1.0 + ((10.0 - log_intra) * 0.05);
650 else
651 intra_factor += 1.0;
652
653 #if CONFIG_VP9_HIGHBITDEPTH
654 if (cm->use_highbitdepth)
655 level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0];
656 else
657 level_sample = x->plane[0].src.buf[0];
658 #else
659 level_sample = x->plane[0].src.buf[0];
660 #endif
661 if ((level_sample < DARK_THRESH) && (log_intra < 9.0))
662 brightness_factor += 1.0 + (0.01 * (DARK_THRESH - level_sample));
663 else
664 brightness_factor += 1.0;
642 665
643 // Intrapenalty below deals with situations where the intra and inter 666 // Intrapenalty below deals with situations where the intra and inter
644 // error scores are very low (e.g. a plain black frame). 667 // error scores are very low (e.g. a plain black frame).
645 // We do not have special cases in first pass for 0,0 and nearest etc so 668 // We do not have special cases in first pass for 0,0 and nearest etc so
646 // all inter modes carry an overhead cost estimate for the mv. 669 // all inter modes carry an overhead cost estimate for the mv.
647 // When the error score is very low this causes us to pick all or lots of 670 // When the error score is very low this causes us to pick all or lots of
648 // INTRA modes and throw lots of key frames. 671 // INTRA modes and throw lots of key frames.
649 // This penalty adds a cost matching that of a 0,0 mv to the intra case. 672 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
650 this_error += intrapenalty; 673 this_error += intrapenalty;
651 674
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
704 #else 727 #else
705 raw_motion_error = get_prediction_error( 728 raw_motion_error = get_prediction_error(
706 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d); 729 bsize, &x->plane[0].src, &unscaled_last_source_buf_2d);
707 #endif // CONFIG_VP9_HIGHBITDEPTH 730 #endif // CONFIG_VP9_HIGHBITDEPTH
708 731
709 // TODO(pengchong): Replace the hard-coded threshold 732 // TODO(pengchong): Replace the hard-coded threshold
710 if (raw_motion_error > 25 || lc != NULL) { 733 if (raw_motion_error > 25 || lc != NULL) {
711 // Test last reference frame using the previous best mv as the 734 // Test last reference frame using the previous best mv as the
712 // starting point (best reference) for the search. 735 // starting point (best reference) for the search.
713 first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error); 736 first_pass_motion_search(cpi, x, &best_ref_mv, &mv, &motion_error);
714 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
715 vp9_clear_system_state();
716 motion_error = (int)(motion_error * error_weight);
717 }
718 737
719 // If the current best reference mv is not centered on 0,0 then do a 738 // If the current best reference mv is not centered on 0,0 then do a
720 // 0,0 based search as well. 739 // 0,0 based search as well.
721 if (!is_zero_mv(&best_ref_mv)) { 740 if (!is_zero_mv(&best_ref_mv)) {
722 tmp_err = INT_MAX; 741 tmp_err = INT_MAX;
723 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err); 742 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
724 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
725 vp9_clear_system_state();
726 tmp_err = (int)(tmp_err * error_weight);
727 }
728 743
729 if (tmp_err < motion_error) { 744 if (tmp_err < motion_error) {
730 motion_error = tmp_err; 745 motion_error = tmp_err;
731 mv = tmp_mv; 746 mv = tmp_mv;
732 } 747 }
733 } 748 }
734 749
735 // Search in an older reference frame. 750 // Search in an older reference frame.
736 if (((lc == NULL && cm->current_video_frame > 1) || 751 if (((lc == NULL && cm->current_video_frame > 1) ||
737 (lc != NULL && lc->current_video_frame_in_layer > 1)) 752 (lc != NULL && lc->current_video_frame_in_layer > 1))
(...skipping 10 matching lines...) Expand all
748 gf_motion_error = get_prediction_error( 763 gf_motion_error = get_prediction_error(
749 bsize, &x->plane[0].src, &xd->plane[0].pre[0]); 764 bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
750 } 765 }
751 #else 766 #else
752 gf_motion_error = get_prediction_error( 767 gf_motion_error = get_prediction_error(
753 bsize, &x->plane[0].src, &xd->plane[0].pre[0]); 768 bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
754 #endif // CONFIG_VP9_HIGHBITDEPTH 769 #endif // CONFIG_VP9_HIGHBITDEPTH
755 770
756 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, 771 first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv,
757 &gf_motion_error); 772 &gf_motion_error);
758 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
759 vp9_clear_system_state();
760 gf_motion_error = (int)(gf_motion_error * error_weight);
761 }
762 773
763 if (gf_motion_error < motion_error && gf_motion_error < this_error) 774 if (gf_motion_error < motion_error && gf_motion_error < this_error)
764 ++second_ref_count; 775 ++second_ref_count;
765 776
766 // Reset to last frame as reference buffer. 777 // Reset to last frame as reference buffer.
767 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset; 778 xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
768 xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset; 779 xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
769 xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset; 780 xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
770 781
771 // In accumulating a score for the older reference frame take the 782 // In accumulating a score for the older reference frame take the
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 // Adjust to the next row of MBs. 935 // Adjust to the next row of MBs.
925 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols; 936 x->plane[0].src.buf += 16 * x->plane[0].src.stride - 16 * cm->mb_cols;
926 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride - 937 x->plane[1].src.buf += uv_mb_height * x->plane[1].src.stride -
927 uv_mb_height * cm->mb_cols; 938 uv_mb_height * cm->mb_cols;
928 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride - 939 x->plane[2].src.buf += uv_mb_height * x->plane[1].src.stride -
929 uv_mb_height * cm->mb_cols; 940 uv_mb_height * cm->mb_cols;
930 941
931 vp9_clear_system_state(); 942 vp9_clear_system_state();
932 } 943 }
933 944
934 vp9_clear_system_state();
935 { 945 {
936 FIRSTPASS_STATS fps; 946 FIRSTPASS_STATS fps;
937 // The minimum error here insures some bit alocation to frames even 947 // The minimum error here insures some bit allocation to frames even
938 // in static regions. The allocation per MB declines for larger formats 948 // in static regions. The allocation per MB declines for larger formats
939 // where the typical "real" energy per MB also falls. 949 // where the typical "real" energy per MB also falls.
940 // Initial estimate here uses sqrt(mbs) to define the min_err, where the 950 // Initial estimate here uses sqrt(mbs) to define the min_err, where the
941 // number of mbs is propotional to image area. 951 // number of mbs is proportional to the image area.
942 const double min_err = 200 * sqrt(cm->MBs); 952 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
953 ? cpi->initial_mbs : cpi->common.MBs;
954 const double min_err = 200 * sqrt(num_mbs);
955
956 intra_factor = intra_factor / (double)num_mbs;
957 brightness_factor = brightness_factor / (double)num_mbs;
958 fps.weight = intra_factor * brightness_factor;
943 959
944 fps.frame = cm->current_video_frame; 960 fps.frame = cm->current_video_frame;
945 fps.spatial_layer_id = cpi->svc.spatial_layer_id; 961 fps.spatial_layer_id = cpi->svc.spatial_layer_id;
946 fps.coded_error = (double)(coded_error >> 8) + min_err; 962 fps.coded_error = (double)(coded_error >> 8) + min_err;
947 fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err; 963 fps.sr_coded_error = (double)(sr_coded_error >> 8) + min_err;
948 fps.intra_error = (double)(intra_error >> 8) + min_err; 964 fps.intra_error = (double)(intra_error >> 8) + min_err;
949 fps.count = 1.0; 965 fps.count = 1.0;
950 fps.pcnt_inter = (double)intercount / cm->MBs; 966 fps.pcnt_inter = (double)intercount / num_mbs;
951 fps.pcnt_second_ref = (double)second_ref_count / cm->MBs; 967 fps.pcnt_second_ref = (double)second_ref_count / num_mbs;
952 fps.pcnt_neutral = (double)neutral_count / cm->MBs; 968 fps.pcnt_neutral = (double)neutral_count / num_mbs;
953 969
954 if (mvcount > 0) { 970 if (mvcount > 0) {
955 fps.MVr = (double)sum_mvr / mvcount; 971 fps.MVr = (double)sum_mvr / mvcount;
956 fps.mvr_abs = (double)sum_mvr_abs / mvcount; 972 fps.mvr_abs = (double)sum_mvr_abs / mvcount;
957 fps.MVc = (double)sum_mvc / mvcount; 973 fps.MVc = (double)sum_mvc / mvcount;
958 fps.mvc_abs = (double)sum_mvc_abs / mvcount; 974 fps.mvc_abs = (double)sum_mvc_abs / mvcount;
959 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount; 975 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / mvcount)) / mvcount;
960 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount; 976 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / mvcount)) / mvcount;
961 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2); 977 fps.mv_in_out_count = (double)sum_in_vectors / (mvcount * 2);
962 fps.new_mv_count = new_mv_count; 978 fps.new_mv_count = new_mv_count;
963 fps.pcnt_motion = (double)mvcount / cm->MBs; 979 fps.pcnt_motion = (double)mvcount / num_mbs;
964 } else { 980 } else {
965 fps.MVr = 0.0; 981 fps.MVr = 0.0;
966 fps.mvr_abs = 0.0; 982 fps.mvr_abs = 0.0;
967 fps.MVc = 0.0; 983 fps.MVc = 0.0;
968 fps.mvc_abs = 0.0; 984 fps.mvc_abs = 0.0;
969 fps.MVrv = 0.0; 985 fps.MVrv = 0.0;
970 fps.MVcv = 0.0; 986 fps.MVcv = 0.0;
971 fps.mv_in_out_count = 0.0; 987 fps.mv_in_out_count = 0.0;
972 fps.new_mv_count = 0.0; 988 fps.new_mv_count = 0.0;
973 fps.pcnt_motion = 0.0; 989 fps.pcnt_motion = 0.0;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 1083
1068 static int get_twopass_worst_quality(const VP9_COMP *cpi, 1084 static int get_twopass_worst_quality(const VP9_COMP *cpi,
1069 const FIRSTPASS_STATS *stats, 1085 const FIRSTPASS_STATS *stats,
1070 int section_target_bandwidth) { 1086 int section_target_bandwidth) {
1071 const RATE_CONTROL *const rc = &cpi->rc; 1087 const RATE_CONTROL *const rc = &cpi->rc;
1072 const VP9EncoderConfig *const oxcf = &cpi->oxcf; 1088 const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1073 1089
1074 if (section_target_bandwidth <= 0) { 1090 if (section_target_bandwidth <= 0) {
1075 return rc->worst_quality; // Highest value allowed 1091 return rc->worst_quality; // Highest value allowed
1076 } else { 1092 } else {
1077 const int num_mbs = cpi->common.MBs; 1093 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1094 ? cpi->initial_mbs : cpi->common.MBs;
1078 const double section_err = stats->coded_error / stats->count; 1095 const double section_err = stats->coded_error / stats->count;
1079 const double err_per_mb = section_err / num_mbs; 1096 const double err_per_mb = section_err / num_mbs;
1080 const double speed_term = 1.0 + 0.04 * oxcf->speed; 1097 const double speed_term = 1.0 + 0.04 * oxcf->speed;
1081 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR; 1098 const double ediv_size_correction = num_mbs / EDIV_SIZE_FACTOR;
1082 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth << 1099 const int target_norm_bits_per_mb = ((uint64_t)section_target_bandwidth <<
1083 BPER_MB_NORMBITS) / num_mbs; 1100 BPER_MB_NORMBITS) / num_mbs;
1084 1101
1085 int q; 1102 int q;
1086 int is_svc_upper_layer = 0; 1103 int is_svc_upper_layer = 0;
1087 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0) 1104 if (is_two_pass_svc(cpi) && cpi->svc.spatial_layer_id > 0)
1088 is_svc_upper_layer = 1; 1105 is_svc_upper_layer = 1;
1089 1106
1090 // Try and pick a max Q that will be high enough to encode the 1107 // Try and pick a max Q that will be high enough to encode the
1091 // content at the given rate. 1108 // content at the given rate.
1092 for (q = rc->best_quality; q < rc->worst_quality; ++q) { 1109 for (q = rc->best_quality; q < rc->worst_quality; ++q) {
1093 const double factor = 1110 const double factor =
1094 calc_correction_factor(err_per_mb, ERR_DIVISOR - ediv_size_correction, 1111 calc_correction_factor(err_per_mb,
1112 ERR_DIVISOR - ediv_size_correction,
1095 is_svc_upper_layer ? SVC_FACTOR_PT_LOW : 1113 is_svc_upper_layer ? SVC_FACTOR_PT_LOW :
1096 FACTOR_PT_LOW, FACTOR_PT_HIGH, q, 1114 FACTOR_PT_LOW, FACTOR_PT_HIGH, q,
1097 cpi->common.bit_depth); 1115 cpi->common.bit_depth);
1098 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q, 1116 const int bits_per_mb = vp9_rc_bits_per_mb(INTER_FRAME, q,
1099 factor * speed_term, 1117 factor * speed_term,
1100 cpi->common.bit_depth); 1118 cpi->common.bit_depth);
1101 if (bits_per_mb <= target_norm_bits_per_mb) 1119 if (bits_per_mb <= target_norm_bits_per_mb)
1102 break; 1120 break;
1103 } 1121 }
1104 1122
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 twopass->last_kfgroup_zeromotion_pct = 100; 1199 twopass->last_kfgroup_zeromotion_pct = 100;
1182 } 1200 }
1183 1201
1184 #define SR_DIFF_PART 0.0015 1202 #define SR_DIFF_PART 0.0015
1185 #define MOTION_AMP_PART 0.003 1203 #define MOTION_AMP_PART 0.003
1186 #define INTRA_PART 0.005 1204 #define INTRA_PART 0.005
1187 #define DEFAULT_DECAY_LIMIT 0.75 1205 #define DEFAULT_DECAY_LIMIT 0.75
1188 #define LOW_SR_DIFF_TRHESH 0.1 1206 #define LOW_SR_DIFF_TRHESH 0.1
1189 #define SR_DIFF_MAX 128.0 1207 #define SR_DIFF_MAX 128.0
1190 1208
1191 static double get_sr_decay_rate(const VP9_COMMON *cm, 1209 static double get_sr_decay_rate(const VP9_COMP *cpi,
1192 const FIRSTPASS_STATS *frame) { 1210 const FIRSTPASS_STATS *frame) {
1193 double sr_diff = (frame->sr_coded_error - frame->coded_error) / cm->MBs; 1211 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1212 ? cpi->initial_mbs : cpi->common.MBs;
1213 double sr_diff =
1214 (frame->sr_coded_error - frame->coded_error) / num_mbs;
1194 double sr_decay = 1.0; 1215 double sr_decay = 1.0;
1195 const double motion_amplitude_factor = 1216 const double motion_amplitude_factor =
1196 frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) / 2); 1217 frame->pcnt_motion * ((frame->mvc_abs + frame->mvr_abs) / 2);
1197 const double pcnt_intra = 100 * (1.0 - frame->pcnt_inter); 1218 const double pcnt_intra = 100 * (1.0 - frame->pcnt_inter);
1198 1219
1199 if ((sr_diff > LOW_SR_DIFF_TRHESH)) { 1220 if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1200 sr_diff = MIN(sr_diff, SR_DIFF_MAX); 1221 sr_diff = MIN(sr_diff, SR_DIFF_MAX);
1201 sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) - 1222 sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) -
1202 (MOTION_AMP_PART * motion_amplitude_factor) - 1223 (MOTION_AMP_PART * motion_amplitude_factor) -
1203 (INTRA_PART * pcnt_intra); 1224 (INTRA_PART * pcnt_intra);
1204 } 1225 }
1205 return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, frame->pcnt_inter)); 1226 return MAX(sr_decay, MIN(DEFAULT_DECAY_LIMIT, frame->pcnt_inter));
1206 } 1227 }
1207 1228
1208 // This function gives an estimate of how badly we believe the prediction 1229 // This function gives an estimate of how badly we believe the prediction
1209 // quality is decaying from frame to frame. 1230 // quality is decaying from frame to frame.
1210 static double get_zero_motion_factor(const VP9_COMMON *cm, 1231 static double get_zero_motion_factor(const VP9_COMP *cpi,
1211 const FIRSTPASS_STATS *frame) { 1232 const FIRSTPASS_STATS *frame) {
1212 const double zero_motion_pct = frame->pcnt_inter - 1233 const double zero_motion_pct = frame->pcnt_inter -
1213 frame->pcnt_motion; 1234 frame->pcnt_motion;
1214 double sr_decay = get_sr_decay_rate(cm, frame); 1235 double sr_decay = get_sr_decay_rate(cpi, frame);
1215 return MIN(sr_decay, zero_motion_pct); 1236 return MIN(sr_decay, zero_motion_pct);
1216 } 1237 }
1217 1238
1218 #define ZM_POWER_FACTOR 0.75 1239 #define ZM_POWER_FACTOR 0.75
1219 1240
1220 static double get_prediction_decay_rate(const VP9_COMMON *cm, 1241 static double get_prediction_decay_rate(const VP9_COMP *cpi,
1221 const FIRSTPASS_STATS *next_frame) { 1242 const FIRSTPASS_STATS *next_frame) {
1222 const double sr_decay_rate = get_sr_decay_rate(cm, next_frame); 1243 const double sr_decay_rate = get_sr_decay_rate(cpi, next_frame);
1223 const double zero_motion_factor = 1244 const double zero_motion_factor =
1224 (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion), 1245 (0.95 * pow((next_frame->pcnt_inter - next_frame->pcnt_motion),
1225 ZM_POWER_FACTOR)); 1246 ZM_POWER_FACTOR));
1226 1247
1227 return MAX(zero_motion_factor, 1248 return MAX(zero_motion_factor,
1228 (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor))); 1249 (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1229 } 1250 }
1230 1251
1231 // Function to test for a condition where a complex transition is followed 1252 // Function to test for a condition where a complex transition is followed
1232 // by a static section. For example in slide shows where there is a fade 1253 // by a static section. For example in slide shows where there is a fade
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
1306 1327
1307 #define BASELINE_ERR_PER_MB 1000.0 1328 #define BASELINE_ERR_PER_MB 1000.0
1308 static double calc_frame_boost(VP9_COMP *cpi, 1329 static double calc_frame_boost(VP9_COMP *cpi,
1309 const FIRSTPASS_STATS *this_frame, 1330 const FIRSTPASS_STATS *this_frame,
1310 double this_frame_mv_in_out, 1331 double this_frame_mv_in_out,
1311 double max_boost) { 1332 double max_boost) {
1312 double frame_boost; 1333 double frame_boost;
1313 const double lq = 1334 const double lq =
1314 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME], 1335 vp9_convert_qindex_to_q(cpi->rc.avg_frame_qindex[INTER_FRAME],
1315 cpi->common.bit_depth); 1336 cpi->common.bit_depth);
1316 const double boost_correction = MIN((0.5 + (lq * 0.015)), 1.5); 1337 const double boost_q_correction = MIN((0.5 + (lq * 0.015)), 1.5);
1338 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1339 ? cpi->initial_mbs : cpi->common.MBs;
1317 1340
1318 // Underlying boost factor is based on inter error ratio. 1341 // Underlying boost factor is based on inter error ratio.
1319 frame_boost = (BASELINE_ERR_PER_MB * cpi->common.MBs) / 1342 frame_boost = (BASELINE_ERR_PER_MB * num_mbs) /
1320 DOUBLE_DIVIDE_CHECK(this_frame->coded_error); 1343 DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1321 frame_boost = frame_boost * BOOST_FACTOR * boost_correction; 1344 frame_boost = frame_boost * BOOST_FACTOR * boost_q_correction;
1322 1345
1323 // Increase boost for frames where new data coming into frame (e.g. zoom out). 1346 // Increase boost for frames where new data coming into frame (e.g. zoom out).
1324 // Slightly reduce boost if there is a net balance of motion out of the frame 1347 // Slightly reduce boost if there is a net balance of motion out of the frame
1325 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0. 1348 // (zoom in). The range for this_frame_mv_in_out is -1.0 to +1.0.
1326 if (this_frame_mv_in_out > 0.0) 1349 if (this_frame_mv_in_out > 0.0)
1327 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0); 1350 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1328 // In the extreme case the boost is halved. 1351 // In the extreme case the boost is halved.
1329 else 1352 else
1330 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0); 1353 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1331 1354
1332 return MIN(frame_boost, max_boost * boost_correction); 1355 return MIN(frame_boost, max_boost * boost_q_correction);
1333 } 1356 }
1334 1357
1335 static int calc_arf_boost(VP9_COMP *cpi, int offset, 1358 static int calc_arf_boost(VP9_COMP *cpi, int offset,
1336 int f_frames, int b_frames, 1359 int f_frames, int b_frames,
1337 int *f_boost, int *b_boost) { 1360 int *f_boost, int *b_boost) {
1338 TWO_PASS *const twopass = &cpi->twopass; 1361 TWO_PASS *const twopass = &cpi->twopass;
1339 int i; 1362 int i;
1340 double boost_score = 0.0; 1363 double boost_score = 0.0;
1341 double mv_ratio_accumulator = 0.0; 1364 double mv_ratio_accumulator = 0.0;
1342 double decay_accumulator = 1.0; 1365 double decay_accumulator = 1.0;
(...skipping 15 matching lines...) Expand all
1358 &abs_mv_in_out_accumulator, 1381 &abs_mv_in_out_accumulator,
1359 &mv_ratio_accumulator); 1382 &mv_ratio_accumulator);
1360 1383
1361 // We want to discount the flash frame itself and the recovery 1384 // We want to discount the flash frame itself and the recovery
1362 // frame that follows as both will have poor scores. 1385 // frame that follows as both will have poor scores.
1363 flash_detected = detect_flash(twopass, i + offset) || 1386 flash_detected = detect_flash(twopass, i + offset) ||
1364 detect_flash(twopass, i + offset + 1); 1387 detect_flash(twopass, i + offset + 1);
1365 1388
1366 // Accumulate the effect of prediction quality decay. 1389 // Accumulate the effect of prediction quality decay.
1367 if (!flash_detected) { 1390 if (!flash_detected) {
1368 decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame); 1391 decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
1369 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR 1392 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1370 ? MIN_DECAY_FACTOR : decay_accumulator; 1393 ? MIN_DECAY_FACTOR : decay_accumulator;
1371 } 1394 }
1372 1395
1373 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame, 1396 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1374 this_frame_mv_in_out, 1397 this_frame_mv_in_out,
1375 GF_MAX_BOOST); 1398 GF_MAX_BOOST);
1376 } 1399 }
1377 1400
1378 *f_boost = (int)boost_score; 1401 *f_boost = (int)boost_score;
(...skipping 18 matching lines...) Expand all
1397 &abs_mv_in_out_accumulator, 1420 &abs_mv_in_out_accumulator,
1398 &mv_ratio_accumulator); 1421 &mv_ratio_accumulator);
1399 1422
1400 // We want to discount the the flash frame itself and the recovery 1423 // We want to discount the the flash frame itself and the recovery
1401 // frame that follows as both will have poor scores. 1424 // frame that follows as both will have poor scores.
1402 flash_detected = detect_flash(twopass, i + offset) || 1425 flash_detected = detect_flash(twopass, i + offset) ||
1403 detect_flash(twopass, i + offset + 1); 1426 detect_flash(twopass, i + offset + 1);
1404 1427
1405 // Cumulative effect of prediction quality decay. 1428 // Cumulative effect of prediction quality decay.
1406 if (!flash_detected) { 1429 if (!flash_detected) {
1407 decay_accumulator *= get_prediction_decay_rate(&cpi->common, this_frame); 1430 decay_accumulator *= get_prediction_decay_rate(cpi, this_frame);
1408 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR 1431 decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1409 ? MIN_DECAY_FACTOR : decay_accumulator; 1432 ? MIN_DECAY_FACTOR : decay_accumulator;
1410 } 1433 }
1411 1434
1412 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame, 1435 boost_score += decay_accumulator * calc_frame_boost(cpi, this_frame,
1413 this_frame_mv_in_out, 1436 this_frame_mv_in_out,
1414 GF_MAX_BOOST); 1437 GF_MAX_BOOST);
1415 } 1438 }
1416 *b_boost = (int)boost_score; 1439 *b_boost = (int)boost_score;
1417 1440
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after
1716 // Note the error of the frame at the start of the group. This will be 1739 // Note the error of the frame at the start of the group. This will be
1717 // the GF frame error if we code a normal gf. 1740 // the GF frame error if we code a normal gf.
1718 gf_first_frame_err = mod_frame_err; 1741 gf_first_frame_err = mod_frame_err;
1719 1742
1720 // If this is a key frame or the overlay from a previous arf then 1743 // If this is a key frame or the overlay from a previous arf then
1721 // the error score / cost of this frame has already been accounted for. 1744 // the error score / cost of this frame has already been accounted for.
1722 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active) 1745 if (cpi->common.frame_type == KEY_FRAME || rc->source_alt_ref_active)
1723 gf_group_err -= gf_first_frame_err; 1746 gf_group_err -= gf_first_frame_err;
1724 1747
1725 // Motion breakout threshold for loop below depends on image size. 1748 // Motion breakout threshold for loop below depends on image size.
1726 mv_ratio_accumulator_thresh = (cpi->common.width + cpi->common.height) / 4.0; 1749 mv_ratio_accumulator_thresh =
1750 (cpi->initial_height + cpi->initial_width) / 4.0;
1727 1751
1728 // Set a maximum and minimum interval for the GF group. 1752 // Set a maximum and minimum interval for the GF group.
1729 // If the image appears almost completely static we can extend beyond this. 1753 // If the image appears almost completely static we can extend beyond this.
1730 { 1754 {
1731 int int_max_q = 1755 int int_max_q =
1732 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality, 1756 (int)(vp9_convert_qindex_to_q(twopass->active_worst_quality,
1733 cpi->common.bit_depth)); 1757 cpi->common.bit_depth));
1734 int int_lbq = 1758 int int_lbq =
1735 (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex, 1759 (int)(vp9_convert_qindex_to_q(rc->last_boosted_qindex,
1736 cpi->common.bit_depth)); 1760 cpi->common.bit_depth));
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1768 1792
1769 // Update the motion related elements to the boost calculation. 1793 // Update the motion related elements to the boost calculation.
1770 accumulate_frame_motion_stats(&next_frame, 1794 accumulate_frame_motion_stats(&next_frame,
1771 &this_frame_mv_in_out, &mv_in_out_accumulator, 1795 &this_frame_mv_in_out, &mv_in_out_accumulator,
1772 &abs_mv_in_out_accumulator, 1796 &abs_mv_in_out_accumulator,
1773 &mv_ratio_accumulator); 1797 &mv_ratio_accumulator);
1774 1798
1775 // Accumulate the effect of prediction quality decay. 1799 // Accumulate the effect of prediction quality decay.
1776 if (!flash_detected) { 1800 if (!flash_detected) {
1777 last_loop_decay_rate = loop_decay_rate; 1801 last_loop_decay_rate = loop_decay_rate;
1778 loop_decay_rate = get_prediction_decay_rate(&cpi->common, &next_frame); 1802 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1779 1803
1780 decay_accumulator = decay_accumulator * loop_decay_rate; 1804 decay_accumulator = decay_accumulator * loop_decay_rate;
1781 1805
1782 // Monitor for static sections. 1806 // Monitor for static sections.
1783 zero_motion_accumulator = 1807 zero_motion_accumulator =
1784 MIN(zero_motion_accumulator, 1808 MIN(zero_motion_accumulator, get_zero_motion_factor(cpi, &next_frame));
1785 get_zero_motion_factor(&cpi->common, &next_frame));
1786 1809
1787 // Break clause to detect very still sections after motion. For example, 1810 // Break clause to detect very still sections after motion. For example,
1788 // a static image after a fade or other transition. 1811 // a static image after a fade or other transition.
1789 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate, 1812 if (detect_transition_to_still(twopass, i, 5, loop_decay_rate,
1790 last_loop_decay_rate)) { 1813 last_loop_decay_rate)) {
1791 allow_alt_ref = 0; 1814 allow_alt_ref = 0;
1792 break; 1815 break;
1793 } 1816 }
1794 } 1817 }
1795 1818
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after
2041 // Provided that we are not at the end of the file... 2064 // Provided that we are not at the end of the file...
2042 if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) { 2065 if (cpi->oxcf.auto_key && twopass->stats_in < twopass->stats_in_end) {
2043 double loop_decay_rate; 2066 double loop_decay_rate;
2044 2067
2045 // Check for a scene cut. 2068 // Check for a scene cut.
2046 if (test_candidate_kf(twopass, &last_frame, this_frame, 2069 if (test_candidate_kf(twopass, &last_frame, this_frame,
2047 twopass->stats_in)) 2070 twopass->stats_in))
2048 break; 2071 break;
2049 2072
2050 // How fast is the prediction quality decaying? 2073 // How fast is the prediction quality decaying?
2051 loop_decay_rate = get_prediction_decay_rate(&cpi->common, 2074 loop_decay_rate = get_prediction_decay_rate(cpi, twopass->stats_in);
2052 twopass->stats_in);
2053 2075
2054 // We want to know something about the recent past... rather than 2076 // We want to know something about the recent past... rather than
2055 // as used elsewhere where we are concerned with decay in prediction 2077 // as used elsewhere where we are concerned with decay in prediction
2056 // quality since the last GF or KF. 2078 // quality since the last GF or KF.
2057 recent_loop_decay[i % 8] = loop_decay_rate; 2079 recent_loop_decay[i % 8] = loop_decay_rate;
2058 decay_accumulator = 1.0; 2080 decay_accumulator = 1.0;
2059 for (j = 0; j < 8; ++j) 2081 for (j = 0; j < 8; ++j)
2060 decay_accumulator *= recent_loop_decay[j]; 2082 decay_accumulator *= recent_loop_decay[j];
2061 2083
2062 // Special check for transition or high motion followed by a 2084 // Special check for transition or high motion followed by a
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
2153 // how many bits to spend on it. 2175 // how many bits to spend on it.
2154 decay_accumulator = 1.0; 2176 decay_accumulator = 1.0;
2155 boost_score = 0.0; 2177 boost_score = 0.0;
2156 for (i = 0; i < (rc->frames_to_key - 1); ++i) { 2178 for (i = 0; i < (rc->frames_to_key - 1); ++i) {
2157 if (EOF == input_stats(twopass, &next_frame)) 2179 if (EOF == input_stats(twopass, &next_frame))
2158 break; 2180 break;
2159 2181
2160 // Monitor for static sections. 2182 // Monitor for static sections.
2161 zero_motion_accumulator = 2183 zero_motion_accumulator =
2162 MIN(zero_motion_accumulator, 2184 MIN(zero_motion_accumulator,
2163 get_zero_motion_factor(&cpi->common, &next_frame)); 2185 get_zero_motion_factor(cpi, &next_frame));
2164 2186
2165 // Not all frames in the group are necessarily used in calculating boost. 2187 // Not all frames in the group are necessarily used in calculating boost.
2166 if ((i <= rc->max_gf_interval) || 2188 if ((i <= rc->max_gf_interval) ||
2167 ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) { 2189 ((i <= (rc->max_gf_interval * 4)) && (decay_accumulator > 0.5))) {
2168 const double frame_boost = 2190 const double frame_boost =
2169 calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST); 2191 calc_frame_boost(cpi, this_frame, 0, KF_MAX_BOOST);
2170 2192
2171 // How fast is prediction quality decaying. 2193 // How fast is prediction quality decaying.
2172 if (!detect_flash(twopass, 0)) { 2194 if (!detect_flash(twopass, 0)) {
2173 const double loop_decay_rate = 2195 const double loop_decay_rate =
2174 get_prediction_decay_rate(&cpi->common, &next_frame); 2196 get_prediction_decay_rate(cpi, &next_frame);
2175 decay_accumulator *= loop_decay_rate; 2197 decay_accumulator *= loop_decay_rate;
2176 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR); 2198 decay_accumulator = MAX(decay_accumulator, MIN_DECAY_FACTOR);
2177 av_decay_accumulator += decay_accumulator; 2199 av_decay_accumulator += decay_accumulator;
2178 ++loop_decay_counter; 2200 ++loop_decay_counter;
2179 } 2201 }
2180 boost_score += (decay_accumulator * frame_boost); 2202 boost_score += (decay_accumulator * frame_boost);
2181 } 2203 }
2182 } 2204 }
2183 av_decay_accumulator /= (double)loop_decay_counter; 2205 av_decay_accumulator /= (double)loop_decay_counter;
2184 2206
(...skipping 25 matching lines...) Expand all
2210 2232
2211 // Note the total error score of the kf group minus the key frame itself. 2233 // Note the total error score of the kf group minus the key frame itself.
2212 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err); 2234 twopass->kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2213 2235
2214 // Adjust the count of total modified error left. 2236 // Adjust the count of total modified error left.
2215 // The count of bits left is adjusted elsewhere based on real coded frame 2237 // The count of bits left is adjusted elsewhere based on real coded frame
2216 // sizes. 2238 // sizes.
2217 twopass->modified_error_left -= kf_group_err; 2239 twopass->modified_error_left -= kf_group_err;
2218 } 2240 }
2219 2241
2220 #define VBR_PCT_ADJUSTMENT_LIMIT 50
2221 // For VBR...adjustment to the frame target based on error from previous frames
2222 void vbr_rate_correction(VP9_COMP *cpi,
2223 int * this_frame_target,
2224 const int64_t vbr_bits_off_target) {
2225 int max_delta;
2226 double position_factor = 1.0;
2227
2228 // How far through the clip are we.
2229 // This number is used to damp the per frame rate correction.
2230 // Range 0 - 1.0
2231 if (cpi->twopass.total_stats.count) {
2232 position_factor = sqrt((double)cpi->common.current_video_frame /
2233 cpi->twopass.total_stats.count);
2234 }
2235 max_delta = (int)(position_factor *
2236 ((*this_frame_target * VBR_PCT_ADJUSTMENT_LIMIT) / 100));
2237
2238 // vbr_bits_off_target > 0 means we have extra bits to spend
2239 if (vbr_bits_off_target > 0) {
2240 *this_frame_target +=
2241 (vbr_bits_off_target > max_delta) ? max_delta
2242 : (int)vbr_bits_off_target;
2243 } else {
2244 *this_frame_target -=
2245 (vbr_bits_off_target < -max_delta) ? max_delta
2246 : (int)-vbr_bits_off_target;
2247 }
2248 }
2249
2250 // Define the reference buffers that will be updated post encode. 2242 // Define the reference buffers that will be updated post encode.
2251 void configure_buffer_updates(VP9_COMP *cpi) { 2243 void configure_buffer_updates(VP9_COMP *cpi) {
2252 TWO_PASS *const twopass = &cpi->twopass; 2244 TWO_PASS *const twopass = &cpi->twopass;
2253 2245
2254 cpi->rc.is_src_frame_alt_ref = 0; 2246 cpi->rc.is_src_frame_alt_ref = 0;
2255 switch (twopass->gf_group.update_type[twopass->gf_group.index]) { 2247 switch (twopass->gf_group.update_type[twopass->gf_group.index]) {
2256 case KF_UPDATE: 2248 case KF_UPDATE:
2257 cpi->refresh_last_frame = 1; 2249 cpi->refresh_last_frame = 1;
2258 cpi->refresh_golden_frame = 1; 2250 cpi->refresh_golden_frame = 1;
2259 cpi->refresh_alt_ref_frame = 1; 2251 cpi->refresh_alt_ref_frame = 1;
(...skipping 28 matching lines...) Expand all
2288 cpi->refresh_last_frame = 0; 2280 cpi->refresh_last_frame = 0;
2289 cpi->refresh_golden_frame = 0; 2281 cpi->refresh_golden_frame = 0;
2290 } 2282 }
2291 if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0) 2283 if (cpi->svc.layer_context[cpi->svc.spatial_layer_id].gold_ref_idx < 0)
2292 cpi->refresh_golden_frame = 0; 2284 cpi->refresh_golden_frame = 0;
2293 if (cpi->alt_ref_source == NULL) 2285 if (cpi->alt_ref_source == NULL)
2294 cpi->refresh_alt_ref_frame = 0; 2286 cpi->refresh_alt_ref_frame = 0;
2295 } 2287 }
2296 } 2288 }
2297 2289
2290 int is_skippable_frame(const VP9_COMP *cpi) {
2291 // If the current frame does not have non-zero motion vector detected in the
2292 // first pass, and so do its previous and forward frames, then this frame
2293 // can be skipped for partition check, and the partition size is assigned
2294 // according to the variance
2295 const SVC *const svc = &cpi->svc;
2296 const TWO_PASS *const twopass = is_two_pass_svc(cpi) ?
2297 &svc->layer_context[svc->spatial_layer_id].twopass : &cpi->twopass;
2298
2299 return (!frame_is_intra_only(&cpi->common) &&
2300 twopass->stats_in - 2 > twopass->stats_in_start &&
2301 twopass->stats_in < twopass->stats_in_end &&
2302 (twopass->stats_in - 1)->pcnt_inter - (twopass->stats_in - 1)->pcnt_motion
2303 == 1 &&
2304 (twopass->stats_in - 2)->pcnt_inter - (twopass->stats_in - 2)->pcnt_motion
2305 == 1 &&
2306 twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
2307 }
2298 2308
2299 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { 2309 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
2300 VP9_COMMON *const cm = &cpi->common; 2310 VP9_COMMON *const cm = &cpi->common;
2301 RATE_CONTROL *const rc = &cpi->rc; 2311 RATE_CONTROL *const rc = &cpi->rc;
2302 TWO_PASS *const twopass = &cpi->twopass; 2312 TWO_PASS *const twopass = &cpi->twopass;
2303 GF_GROUP *const gf_group = &twopass->gf_group; 2313 GF_GROUP *const gf_group = &twopass->gf_group;
2304 int frames_left; 2314 int frames_left;
2305 FIRSTPASS_STATS this_frame; 2315 FIRSTPASS_STATS this_frame;
2306 FIRSTPASS_STATS this_frame_copy; 2316 FIRSTPASS_STATS this_frame_copy;
2307 2317
(...skipping 14 matching lines...) Expand all
2322 2332
2323 // If this is an arf frame then we dont want to read the stats file or 2333 // If this is an arf frame then we dont want to read the stats file or
2324 // advance the input pointer as we already have what we need. 2334 // advance the input pointer as we already have what we need.
2325 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) { 2335 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
2326 int target_rate; 2336 int target_rate;
2327 configure_buffer_updates(cpi); 2337 configure_buffer_updates(cpi);
2328 target_rate = gf_group->bit_allocation[gf_group->index]; 2338 target_rate = gf_group->bit_allocation[gf_group->index];
2329 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); 2339 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2330 rc->base_frame_target = target_rate; 2340 rc->base_frame_target = target_rate;
2331 2341
2332 // Correction to rate target based on prior over or under shoot.
2333 if (cpi->oxcf.rc_mode == VPX_VBR)
2334 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target);
2335
2336 vp9_rc_set_frame_target(cpi, target_rate);
2337 cm->frame_type = INTER_FRAME; 2342 cm->frame_type = INTER_FRAME;
2338 2343
2339 if (lc != NULL) { 2344 if (lc != NULL) {
2340 if (cpi->svc.spatial_layer_id == 0) { 2345 if (cpi->svc.spatial_layer_id == 0) {
2341 lc->is_key_frame = 0; 2346 lc->is_key_frame = 0;
2342 } else { 2347 } else {
2343 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame; 2348 lc->is_key_frame = cpi->svc.layer_context[0].is_key_frame;
2344 2349
2345 if (lc->is_key_frame) 2350 if (lc->is_key_frame)
2346 cpi->ref_frame_flags &= (~VP9_LAST_FLAG); 2351 cpi->ref_frame_flags &= (~VP9_LAST_FLAG);
2347 } 2352 }
2348 } 2353 }
2349 2354
2355 // Do the firstpass stats indicate that this frame is skippable for the
2356 // partition search?
2357 if (cpi->sf.allow_partition_search_skip &&
2358 cpi->oxcf.pass == 2 && (!cpi->use_svc || is_two_pass_svc(cpi))) {
2359 cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
2360 }
2361
2350 return; 2362 return;
2351 } 2363 }
2352 2364
2353 vp9_clear_system_state(); 2365 vp9_clear_system_state();
2354 2366
2355 if (cpi->oxcf.rc_mode == VPX_Q) { 2367 if (cpi->oxcf.rc_mode == VPX_Q) {
2356 twopass->active_worst_quality = cpi->oxcf.cq_level; 2368 twopass->active_worst_quality = cpi->oxcf.cq_level;
2357 } else if (cm->current_video_frame == 0 || 2369 } else if (cm->current_video_frame == 0 ||
2358 (lc != NULL && lc->current_video_frame_in_layer == 0)) { 2370 (lc != NULL && lc->current_video_frame_in_layer == 0)) {
2359 // Special case code for first frame. 2371 // Special case code for first frame.
(...skipping 10 matching lines...) Expand all
2370 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME]; 2382 rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
2371 } 2383 }
2372 vp9_zero(this_frame); 2384 vp9_zero(this_frame);
2373 if (EOF == input_stats(twopass, &this_frame)) 2385 if (EOF == input_stats(twopass, &this_frame))
2374 return; 2386 return;
2375 2387
2376 // Local copy of the current frame's first pass stats. 2388 // Local copy of the current frame's first pass stats.
2377 this_frame_copy = this_frame; 2389 this_frame_copy = this_frame;
2378 2390
2379 // Keyframe and section processing. 2391 // Keyframe and section processing.
2380 if (rc->frames_to_key == 0 || 2392 if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2381 (cpi->frame_flags & FRAMEFLAGS_KEY)) {
2382 // Define next KF group and assign bits to it. 2393 // Define next KF group and assign bits to it.
2383 find_next_key_frame(cpi, &this_frame_copy); 2394 find_next_key_frame(cpi, &this_frame_copy);
2384 } else { 2395 } else {
2385 cm->frame_type = INTER_FRAME; 2396 cm->frame_type = INTER_FRAME;
2386 } 2397 }
2387 2398
2388 if (lc != NULL) { 2399 if (lc != NULL) {
2389 if (cpi->svc.spatial_layer_id == 0) { 2400 if (cpi->svc.spatial_layer_id == 0) {
2390 lc->is_key_frame = (cm->frame_type == KEY_FRAME); 2401 lc->is_key_frame = (cm->frame_type == KEY_FRAME);
2391 if (lc->is_key_frame) { 2402 if (lc->is_key_frame) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
2424 cm->current_video_frame, rc->frames_till_gf_update_due, 2435 cm->current_video_frame, rc->frames_till_gf_update_due,
2425 rc->kf_boost, arf_count, rc->gfu_boost); 2436 rc->kf_boost, arf_count, rc->gfu_boost);
2426 2437
2427 fclose(fpfile); 2438 fclose(fpfile);
2428 } 2439 }
2429 #endif 2440 #endif
2430 } 2441 }
2431 2442
2432 configure_buffer_updates(cpi); 2443 configure_buffer_updates(cpi);
2433 2444
2445 // Do the firstpass stats indicate that this frame is skippable for the
2446 // partition search?
2447 if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
2448 (!cpi->use_svc || is_two_pass_svc(cpi))) {
2449 cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
2450 }
2451
2434 target_rate = gf_group->bit_allocation[gf_group->index]; 2452 target_rate = gf_group->bit_allocation[gf_group->index];
2435 if (cpi->common.frame_type == KEY_FRAME) 2453 if (cpi->common.frame_type == KEY_FRAME)
2436 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate); 2454 target_rate = vp9_rc_clamp_iframe_target_size(cpi, target_rate);
2437 else 2455 else
2438 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate); 2456 target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
2439 2457
2440 rc->base_frame_target = target_rate; 2458 rc->base_frame_target = target_rate;
2441 2459
2442 // Correction to rate target based on prior over or under shoot. 2460 {
2443 if (cpi->oxcf.rc_mode == VPX_VBR) 2461 const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
2444 vbr_rate_correction(cpi, &target_rate, rc->vbr_bits_off_target); 2462 ? cpi->initial_mbs : cpi->common.MBs;
2445 2463 // The multiplication by 256 reverses a scaling factor of (>> 8)
2446 vp9_rc_set_frame_target(cpi, target_rate); 2464 // applied when combining MB error values for the frame.
2465 twopass->mb_av_energy =
2466 log(((this_frame.intra_error * 256.0) / num_mbs) + 1.0);
2467 }
2447 2468
2448 // Update the total stats remaining structure. 2469 // Update the total stats remaining structure.
2449 subtract_stats(&twopass->total_left_stats, &this_frame); 2470 subtract_stats(&twopass->total_left_stats, &this_frame);
2450 } 2471 }
2451 2472
2452 #define MINQ_ADJ_LIMIT 32 2473 #define MINQ_ADJ_LIMIT 32
2453 #define Q_LIMIT_STEP 1
2454 void vp9_twopass_postencode_update(VP9_COMP *cpi) { 2474 void vp9_twopass_postencode_update(VP9_COMP *cpi) {
2455 TWO_PASS *const twopass = &cpi->twopass; 2475 TWO_PASS *const twopass = &cpi->twopass;
2456 RATE_CONTROL *const rc = &cpi->rc; 2476 RATE_CONTROL *const rc = &cpi->rc;
2457 const int bits_used = rc->base_frame_target; 2477 const int bits_used = rc->base_frame_target;
2458 2478
2459 // VBR correction is done through rc->vbr_bits_off_target. Based on the 2479 // VBR correction is done through rc->vbr_bits_off_target. Based on the
2460 // sign of this value, a limited % adjustment is made to the target rate 2480 // sign of this value, a limited % adjustment is made to the target rate
2461 // of subsequent frames, to try and push it back towards 0. This method 2481 // of subsequent frames, to try and push it back towards 0. This method
2462 // is designed to prevent extreme behaviour at the end of a clip 2482 // is designed to prevent extreme behaviour at the end of a clip
2463 // or group of frames. 2483 // or group of frames.
(...skipping 24 matching lines...) Expand all
2488 if ((cpi->oxcf.rc_mode == VPX_VBR) && 2508 if ((cpi->oxcf.rc_mode == VPX_VBR) &&
2489 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) && 2509 (cpi->twopass.gf_zeromotion_pct < VLOW_MOTION_THRESHOLD) &&
2490 !cpi->rc.is_src_frame_alt_ref) { 2510 !cpi->rc.is_src_frame_alt_ref) {
2491 const int maxq_adj_limit = 2511 const int maxq_adj_limit =
2492 rc->worst_quality - twopass->active_worst_quality; 2512 rc->worst_quality - twopass->active_worst_quality;
2493 2513
2494 // Undershoot. 2514 // Undershoot.
2495 if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) { 2515 if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) {
2496 --twopass->extend_maxq; 2516 --twopass->extend_maxq;
2497 if (rc->rolling_target_bits >= rc->rolling_actual_bits) 2517 if (rc->rolling_target_bits >= rc->rolling_actual_bits)
2498 twopass->extend_minq += Q_LIMIT_STEP; 2518 ++twopass->extend_minq;
2499 // Overshoot. 2519 // Overshoot.
2500 } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) { 2520 } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) {
2501 --twopass->extend_minq; 2521 --twopass->extend_minq;
2502 if (rc->rolling_target_bits < rc->rolling_actual_bits) 2522 if (rc->rolling_target_bits < rc->rolling_actual_bits)
2503 twopass->extend_maxq += Q_LIMIT_STEP; 2523 ++twopass->extend_maxq;
2504 } else { 2524 } else {
2525 // Adjustment for extreme local overshoot.
2526 if (rc->projected_frame_size > (2 * rc->base_frame_target) &&
2527 rc->projected_frame_size > (2 * rc->avg_frame_bandwidth))
2528 ++twopass->extend_maxq;
2529
2530 // Unwind undershoot or overshoot adjustment.
2505 if (rc->rolling_target_bits < rc->rolling_actual_bits) 2531 if (rc->rolling_target_bits < rc->rolling_actual_bits)
2506 --twopass->extend_minq; 2532 --twopass->extend_minq;
2507 if (rc->rolling_target_bits > rc->rolling_actual_bits) 2533 else if (rc->rolling_target_bits > rc->rolling_actual_bits)
2508 --twopass->extend_maxq; 2534 --twopass->extend_maxq;
2509 } 2535 }
2510 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT); 2536 twopass->extend_minq = clamp(twopass->extend_minq, 0, MINQ_ADJ_LIMIT);
2511 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit); 2537 twopass->extend_maxq = clamp(twopass->extend_maxq, 0, maxq_adj_limit);
2512 } 2538 }
2513 } 2539 }
OLDNEW
« no previous file with comments | « source/libvpx/vp9/encoder/vp9_firstpass.h ('k') | source/libvpx/vp9/encoder/vp9_mcomp.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698