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

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

Issue 11555023: libvpx: Add VP9 decoder. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/libvpx/
Patch Set: Created 8 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "math.h"
12 #include "limits.h"
13 #include "vp9/encoder/vp9_block.h"
14 #include "vp9/encoder/vp9_onyx_int.h"
15 #include "vp9/encoder/vp9_variance.h"
16 #include "vp9/encoder/vp9_encodeintra.h"
17 #include "vp9/common/vp9_setupintrarecon.h"
18 #include "vp9/encoder/vp9_mcomp.h"
19 #include "vp9/encoder/vp9_firstpass.h"
20 #include "vpx_scale/vpxscale.h"
21 #include "vp9/encoder/vp9_encodeframe.h"
22 #include "vp9/encoder/vp9_encodemb.h"
23 #include "vp9/common/vp9_extend.h"
24 #include "vp9/common/vp9_systemdependent.h"
25 #include "vpx_mem/vpx_mem.h"
26 #include "vp9/common/vp9_swapyv12buffer.h"
27 #include <stdio.h>
28 #include "vp9/encoder/vp9_quantize.h"
29 #include "vp9/encoder/vp9_rdopt.h"
30 #include "vp9/encoder/vp9_ratectrl.h"
31 #include "vp9/common/vp9_quant_common.h"
32 #include "vp9/common/vp9_entropymv.h"
33 #include "vp9/encoder/vp9_encodemv.h"
34 #include "./vpx_scale_rtcd.h"
35
36 #define OUTPUT_FPF 0
37
38 #define IIFACTOR 12.5
39 #define IIKFACTOR1 12.5
40 #define IIKFACTOR2 15.0
41 #define RMAX 128.0
42 #define GF_RMAX 96.0
43 #define ERR_DIVISOR 150.0
44
45 #define KF_MB_INTRA_MIN 300
46 #define GF_MB_INTRA_MIN 200
47
48 #define DOUBLE_DIVIDE_CHECK(X) ((X)<0?(X)-.000001:(X)+.000001)
49
50 #define POW1 (double)cpi->oxcf.two_pass_vbrbias/100.0
51 #define POW2 (double)cpi->oxcf.two_pass_vbrbias/100.0
52
53 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame);
54
55 static int select_cq_level(int qindex) {
56 int ret_val = QINDEX_RANGE - 1;
57 int i;
58
59 double target_q = (vp9_convert_qindex_to_q(qindex) * 0.5847) + 1.0;
60
61 for (i = 0; i < QINDEX_RANGE; i++) {
62 if (target_q <= vp9_convert_qindex_to_q(i)) {
63 ret_val = i;
64 break;
65 }
66 }
67
68 return ret_val;
69 }
70
71
72 // Resets the first pass file to the given position using a relative seek from t he current position
73 static void reset_fpf_position(VP9_COMP *cpi, FIRSTPASS_STATS *Position) {
74 cpi->twopass.stats_in = Position;
75 }
76
77 static int lookup_next_frame_stats(VP9_COMP *cpi, FIRSTPASS_STATS *next_frame) {
78 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
79 return EOF;
80
81 *next_frame = *cpi->twopass.stats_in;
82 return 1;
83 }
84
85 // Read frame stats at an offset from the current position
86 static int read_frame_stats(VP9_COMP *cpi,
87 FIRSTPASS_STATS *frame_stats,
88 int offset) {
89 FIRSTPASS_STATS *fps_ptr = cpi->twopass.stats_in;
90
91 // Check legality of offset
92 if (offset >= 0) {
93 if (&fps_ptr[offset] >= cpi->twopass.stats_in_end)
94 return EOF;
95 } else if (offset < 0) {
96 if (&fps_ptr[offset] < cpi->twopass.stats_in_start)
97 return EOF;
98 }
99
100 *frame_stats = fps_ptr[offset];
101 return 1;
102 }
103
104 static int input_stats(VP9_COMP *cpi, FIRSTPASS_STATS *fps) {
105 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end)
106 return EOF;
107
108 *fps = *cpi->twopass.stats_in;
109 cpi->twopass.stats_in =
110 (void *)((char *)cpi->twopass.stats_in + sizeof(FIRSTPASS_STATS));
111 return 1;
112 }
113
114 static void output_stats(const VP9_COMP *cpi,
115 struct vpx_codec_pkt_list *pktlist,
116 FIRSTPASS_STATS *stats) {
117 struct vpx_codec_cx_pkt pkt;
118 pkt.kind = VPX_CODEC_STATS_PKT;
119 pkt.data.twopass_stats.buf = stats;
120 pkt.data.twopass_stats.sz = sizeof(FIRSTPASS_STATS);
121 vpx_codec_pkt_list_add(pktlist, &pkt);
122
123 // TEMP debug code
124 #if OUTPUT_FPF
125
126 {
127 FILE *fpfile;
128 fpfile = fopen("firstpass.stt", "a");
129
130 fprintf(fpfile, "%12.0f %12.0f %12.0f %12.0f %12.0f %12.4f %12.4f"
131 "%12.4f %12.4f %12.4f %12.4f %12.4f %12.4f %12.4f"
132 "%12.0f %12.0f %12.4f %12.0f %12.0f %12.4f\n",
133 stats->frame,
134 stats->intra_error,
135 stats->coded_error,
136 stats->sr_coded_error,
137 stats->ssim_weighted_pred_err,
138 stats->pcnt_inter,
139 stats->pcnt_motion,
140 stats->pcnt_second_ref,
141 stats->pcnt_neutral,
142 stats->MVr,
143 stats->mvr_abs,
144 stats->MVc,
145 stats->mvc_abs,
146 stats->MVrv,
147 stats->MVcv,
148 stats->mv_in_out_count,
149 stats->new_mv_count,
150 stats->count,
151 stats->duration);
152 fclose(fpfile);
153 }
154 #endif
155 }
156
157 static void zero_stats(FIRSTPASS_STATS *section) {
158 section->frame = 0.0;
159 section->intra_error = 0.0;
160 section->coded_error = 0.0;
161 section->sr_coded_error = 0.0;
162 section->ssim_weighted_pred_err = 0.0;
163 section->pcnt_inter = 0.0;
164 section->pcnt_motion = 0.0;
165 section->pcnt_second_ref = 0.0;
166 section->pcnt_neutral = 0.0;
167 section->MVr = 0.0;
168 section->mvr_abs = 0.0;
169 section->MVc = 0.0;
170 section->mvc_abs = 0.0;
171 section->MVrv = 0.0;
172 section->MVcv = 0.0;
173 section->mv_in_out_count = 0.0;
174 section->new_mv_count = 0.0;
175 section->count = 0.0;
176 section->duration = 1.0;
177 }
178
179 static void accumulate_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
180 section->frame += frame->frame;
181 section->intra_error += frame->intra_error;
182 section->coded_error += frame->coded_error;
183 section->sr_coded_error += frame->sr_coded_error;
184 section->ssim_weighted_pred_err += frame->ssim_weighted_pred_err;
185 section->pcnt_inter += frame->pcnt_inter;
186 section->pcnt_motion += frame->pcnt_motion;
187 section->pcnt_second_ref += frame->pcnt_second_ref;
188 section->pcnt_neutral += frame->pcnt_neutral;
189 section->MVr += frame->MVr;
190 section->mvr_abs += frame->mvr_abs;
191 section->MVc += frame->MVc;
192 section->mvc_abs += frame->mvc_abs;
193 section->MVrv += frame->MVrv;
194 section->MVcv += frame->MVcv;
195 section->mv_in_out_count += frame->mv_in_out_count;
196 section->new_mv_count += frame->new_mv_count;
197 section->count += frame->count;
198 section->duration += frame->duration;
199 }
200
201 static void subtract_stats(FIRSTPASS_STATS *section, FIRSTPASS_STATS *frame) {
202 section->frame -= frame->frame;
203 section->intra_error -= frame->intra_error;
204 section->coded_error -= frame->coded_error;
205 section->sr_coded_error -= frame->sr_coded_error;
206 section->ssim_weighted_pred_err -= frame->ssim_weighted_pred_err;
207 section->pcnt_inter -= frame->pcnt_inter;
208 section->pcnt_motion -= frame->pcnt_motion;
209 section->pcnt_second_ref -= frame->pcnt_second_ref;
210 section->pcnt_neutral -= frame->pcnt_neutral;
211 section->MVr -= frame->MVr;
212 section->mvr_abs -= frame->mvr_abs;
213 section->MVc -= frame->MVc;
214 section->mvc_abs -= frame->mvc_abs;
215 section->MVrv -= frame->MVrv;
216 section->MVcv -= frame->MVcv;
217 section->mv_in_out_count -= frame->mv_in_out_count;
218 section->new_mv_count -= frame->new_mv_count;
219 section->count -= frame->count;
220 section->duration -= frame->duration;
221 }
222
223 static void avg_stats(FIRSTPASS_STATS *section) {
224 if (section->count < 1.0)
225 return;
226
227 section->intra_error /= section->count;
228 section->coded_error /= section->count;
229 section->sr_coded_error /= section->count;
230 section->ssim_weighted_pred_err /= section->count;
231 section->pcnt_inter /= section->count;
232 section->pcnt_second_ref /= section->count;
233 section->pcnt_neutral /= section->count;
234 section->pcnt_motion /= section->count;
235 section->MVr /= section->count;
236 section->mvr_abs /= section->count;
237 section->MVc /= section->count;
238 section->mvc_abs /= section->count;
239 section->MVrv /= section->count;
240 section->MVcv /= section->count;
241 section->mv_in_out_count /= section->count;
242 section->duration /= section->count;
243 }
244
245 // Calculate a modified Error used in distributing bits between easier and harde r frames
246 static double calculate_modified_err(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
247 double av_err = (cpi->twopass.total_stats->ssim_weighted_pred_err /
248 cpi->twopass.total_stats->count);
249 double this_err = this_frame->ssim_weighted_pred_err;
250 double modified_err;
251
252 if (this_err > av_err)
253 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW1);
254 else
255 modified_err = av_err * pow((this_err / DOUBLE_DIVIDE_CHECK(av_err)), POW2);
256
257 return modified_err;
258 }
259
260 static const double weight_table[256] = {
261 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000 ,
262 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000 ,
263 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000 ,
264 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000, 0.020000 ,
265 0.020000, 0.031250, 0.062500, 0.093750, 0.125000, 0.156250, 0.187500, 0.218750 ,
266 0.250000, 0.281250, 0.312500, 0.343750, 0.375000, 0.406250, 0.437500, 0.468750 ,
267 0.500000, 0.531250, 0.562500, 0.593750, 0.625000, 0.656250, 0.687500, 0.718750 ,
268 0.750000, 0.781250, 0.812500, 0.843750, 0.875000, 0.906250, 0.937500, 0.968750 ,
269 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
270 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
271 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
272 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
273 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
274 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
275 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
276 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
277 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
278 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
279 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
280 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
281 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
282 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
283 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
284 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
285 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
286 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
287 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
288 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
289 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
290 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
291 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000 ,
292 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000
293 };
294
295 static double simple_weight(YV12_BUFFER_CONFIG *source) {
296 int i, j;
297
298 unsigned char *src = source->y_buffer;
299 double sum_weights = 0.0;
300
301 // Loop throught the Y plane raw examining levels and creating a weight for th e image
302 i = source->y_height;
303 do {
304 j = source->y_width;
305 do {
306 sum_weights += weight_table[ *src];
307 src++;
308 } while (--j);
309 src -= source->y_width;
310 src += source->y_stride;
311 } while (--i);
312
313 sum_weights /= (source->y_height * source->y_width);
314
315 return sum_weights;
316 }
317
318
319 // This function returns the current per frame maximum bitrate target
320 static int frame_max_bits(VP9_COMP *cpi) {
321 // Max allocation for a single frame based on the max section guidelines passe d in and how many bits are left
322 int max_bits;
323
324 // For VBR base this on the bits and frames left plus the two_pass_vbrmax_sect ion rate passed in by the user
325 max_bits = (int)(((double)cpi->twopass.bits_left / (cpi->twopass.total_stats-> count - (double)cpi->common.current_video_frame)) * ((double)cpi->oxcf.two_pass_ vbrmax_section / 100.0));
326
327 // Trap case where we are out of bits
328 if (max_bits < 0)
329 max_bits = 0;
330
331 return max_bits;
332 }
333
334 void vp9_init_first_pass(VP9_COMP *cpi) {
335 zero_stats(cpi->twopass.total_stats);
336 }
337
338 void vp9_end_first_pass(VP9_COMP *cpi) {
339 output_stats(cpi, cpi->output_pkt_list, cpi->twopass.total_stats);
340 }
341
342 static void zz_motion_search(VP9_COMP *cpi, MACROBLOCK *x, YV12_BUFFER_CONFIG *r econ_buffer, int *best_motion_err, int recon_yoffset) {
343 MACROBLOCKD *const xd = &x->e_mbd;
344 BLOCK *b = &x->block[0];
345 BLOCKD *d = &x->e_mbd.block[0];
346
347 unsigned char *src_ptr = (*(b->base_src) + b->src);
348 int src_stride = b->src_stride;
349 unsigned char *ref_ptr;
350 int ref_stride = d->pre_stride;
351
352 // Set up pointers for this macro block recon buffer
353 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
354
355 ref_ptr = (unsigned char *)(*(d->base_pre) + d->pre);
356
357 vp9_mse16x16(src_ptr, src_stride, ref_ptr, ref_stride,
358 (unsigned int *)(best_motion_err));
359 }
360
361 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
362 int_mv *ref_mv, MV *best_mv,
363 YV12_BUFFER_CONFIG *recon_buffer,
364 int *best_motion_err, int recon_yoffset) {
365 MACROBLOCKD *const xd = &x->e_mbd;
366 BLOCK *b = &x->block[0];
367 BLOCKD *d = &x->e_mbd.block[0];
368 int num00;
369
370 int_mv tmp_mv;
371 int_mv ref_mv_full;
372
373 int tmp_err;
374 int step_param = 3;
375 int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
376 int n;
377 vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[BLOCK_16X16];
378 int new_mv_mode_penalty = 256;
379
380 // override the default variance function to use MSE
381 v_fn_ptr.vf = vp9_mse16x16;
382
383 // Set up pointers for this macro block recon buffer
384 xd->pre.y_buffer = recon_buffer->y_buffer + recon_yoffset;
385
386 // Initial step/diamond search centred on best mv
387 tmp_mv.as_int = 0;
388 ref_mv_full.as_mv.col = ref_mv->as_mv.col >> 3;
389 ref_mv_full.as_mv.row = ref_mv->as_mv.row >> 3;
390 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv, step_param,
391 x->sadperbit16, &num00, &v_fn_ptr,
392 x->nmvjointcost,
393 x->mvcost, ref_mv);
394 if (tmp_err < INT_MAX - new_mv_mode_penalty)
395 tmp_err += new_mv_mode_penalty;
396
397 if (tmp_err < *best_motion_err) {
398 *best_motion_err = tmp_err;
399 best_mv->row = tmp_mv.as_mv.row;
400 best_mv->col = tmp_mv.as_mv.col;
401 }
402
403 // Further step/diamond searches as necessary
404 n = num00;
405 num00 = 0;
406
407 while (n < further_steps) {
408 n++;
409
410 if (num00)
411 num00--;
412 else {
413 tmp_err = cpi->diamond_search_sad(x, b, d, &ref_mv_full, &tmp_mv,
414 step_param + n, x->sadperbit16,
415 &num00, &v_fn_ptr,
416 x->nmvjointcost,
417 x->mvcost, ref_mv);
418 if (tmp_err < INT_MAX - new_mv_mode_penalty)
419 tmp_err += new_mv_mode_penalty;
420
421 if (tmp_err < *best_motion_err) {
422 *best_motion_err = tmp_err;
423 best_mv->row = tmp_mv.as_mv.row;
424 best_mv->col = tmp_mv.as_mv.col;
425 }
426 }
427 }
428 }
429
430 void vp9_first_pass(VP9_COMP *cpi) {
431 int mb_row, mb_col;
432 MACROBLOCK *const x = &cpi->mb;
433 VP9_COMMON *const cm = &cpi->common;
434 MACROBLOCKD *const xd = &x->e_mbd;
435
436 int recon_yoffset, recon_uvoffset;
437 YV12_BUFFER_CONFIG *lst_yv12 = &cm->yv12_fb[cm->lst_fb_idx];
438 YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
439 YV12_BUFFER_CONFIG *gld_yv12 = &cm->yv12_fb[cm->gld_fb_idx];
440 int recon_y_stride = lst_yv12->y_stride;
441 int recon_uv_stride = lst_yv12->uv_stride;
442 int64_t intra_error = 0;
443 int64_t coded_error = 0;
444 int64_t sr_coded_error = 0;
445
446 int sum_mvr = 0, sum_mvc = 0;
447 int sum_mvr_abs = 0, sum_mvc_abs = 0;
448 int sum_mvrs = 0, sum_mvcs = 0;
449 int mvcount = 0;
450 int intercount = 0;
451 int second_ref_count = 0;
452 int intrapenalty = 256;
453 int neutral_count = 0;
454 int new_mv_count = 0;
455 int sum_in_vectors = 0;
456 uint32_t lastmv_as_int = 0;
457
458 int_mv zero_ref_mv;
459
460 zero_ref_mv.as_int = 0;
461
462 vp9_clear_system_state(); // __asm emms;
463
464 x->src = * cpi->Source;
465 xd->pre = *lst_yv12;
466 xd->dst = *new_yv12;
467
468 x->partition_info = x->pi;
469
470 xd->mode_info_context = cm->mi;
471
472 vp9_build_block_offsets(x);
473
474 vp9_setup_block_dptrs(&x->e_mbd);
475
476 vp9_setup_block_ptrs(x);
477
478 // set up frame new frame for intra coded blocks
479 vp9_setup_intra_recon(new_yv12);
480 vp9_frame_init_quantizer(cpi);
481
482 // Initialise the MV cost table to the defaults
483 // if( cm->current_video_frame == 0)
484 // if ( 0 )
485 {
486 vp9_init_mv_probs(cm);
487 vp9_initialize_rd_consts(cpi, cm->base_qindex + cm->y1dc_delta_q);
488 }
489
490 // for each macroblock row in image
491 for (mb_row = 0; mb_row < cm->mb_rows; mb_row++) {
492 int_mv best_ref_mv;
493
494 best_ref_mv.as_int = 0;
495
496 // reset above block coeffs
497 xd->up_available = (mb_row != 0);
498 recon_yoffset = (mb_row * recon_y_stride * 16);
499 recon_uvoffset = (mb_row * recon_uv_stride * 8);
500
501 // Set up limit values for motion vectors to prevent them extending outside the UMV borders
502 x->mv_row_min = -((mb_row * 16) + (VP9BORDERINPIXELS - 16));
503 x->mv_row_max = ((cm->mb_rows - 1 - mb_row) * 16)
504 + (VP9BORDERINPIXELS - 16);
505
506
507 // for each macroblock col in image
508 for (mb_col = 0; mb_col < cm->mb_cols; mb_col++) {
509 int this_error;
510 int gf_motion_error = INT_MAX;
511 int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
512
513 xd->dst.y_buffer = new_yv12->y_buffer + recon_yoffset;
514 xd->dst.u_buffer = new_yv12->u_buffer + recon_uvoffset;
515 xd->dst.v_buffer = new_yv12->v_buffer + recon_uvoffset;
516 xd->left_available = (mb_col != 0);
517
518 #if !CONFIG_SUPERBLOCKS
519 // Copy current mb to a buffer
520 vp9_copy_mem16x16(x->src.y_buffer, x->src.y_stride, x->thismb, 16);
521 #endif
522
523 // do intra 16x16 prediction
524 this_error = vp9_encode_intra(cpi, x, use_dc_pred);
525
526 // "intrapenalty" below deals with situations where the intra and inter er ror scores are very low (eg a plain black frame)
527 // We do not have special cases in first pass for 0,0 and nearest etc so a ll inter modes carry an overhead cost estimate fot the mv.
528 // When the error score is very low this causes us to pick all or lots of INTRA modes and throw lots of key frames.
529 // This penalty adds a cost matching that of a 0,0 mv to the intra case.
530 this_error += intrapenalty;
531
532 // Cumulative intra error total
533 intra_error += (int64_t)this_error;
534
535 // Set up limit values for motion vectors to prevent them extending outsid e the UMV borders
536 x->mv_col_min = -((mb_col * 16) + (VP9BORDERINPIXELS - 16));
537 x->mv_col_max = ((cm->mb_cols - 1 - mb_col) * 16)
538 + (VP9BORDERINPIXELS - 16);
539
540 // Other than for the first frame do a motion search
541 if (cm->current_video_frame > 0) {
542 int tmp_err;
543 int motion_error = INT_MAX;
544 int_mv mv, tmp_mv;
545
546 // Simple 0,0 motion with no mv overhead
547 zz_motion_search(cpi, x, lst_yv12, &motion_error, recon_yoffset);
548 mv.as_int = tmp_mv.as_int = 0;
549
550 // Test last reference frame using the previous best mv as the
551 // starting point (best reference) for the search
552 first_pass_motion_search(cpi, x, &best_ref_mv,
553 &mv.as_mv, lst_yv12,
554 &motion_error, recon_yoffset);
555
556 // If the current best reference mv is not centred on 0,0 then do a 0,0 based search as well
557 if (best_ref_mv.as_int) {
558 tmp_err = INT_MAX;
559 first_pass_motion_search(cpi, x, &zero_ref_mv, &tmp_mv.as_mv,
560 lst_yv12, &tmp_err, recon_yoffset);
561
562 if (tmp_err < motion_error) {
563 motion_error = tmp_err;
564 mv.as_int = tmp_mv.as_int;
565 }
566 }
567
568 // Experimental search in an older reference frame
569 if (cm->current_video_frame > 1) {
570 // Simple 0,0 motion with no mv overhead
571 zz_motion_search(cpi, x, gld_yv12,
572 &gf_motion_error, recon_yoffset);
573
574 first_pass_motion_search(cpi, x, &zero_ref_mv,
575 &tmp_mv.as_mv, gld_yv12,
576 &gf_motion_error, recon_yoffset);
577
578 if ((gf_motion_error < motion_error) &&
579 (gf_motion_error < this_error)) {
580 second_ref_count++;
581 }
582
583 // Reset to last frame as reference buffer
584 xd->pre.y_buffer = lst_yv12->y_buffer + recon_yoffset;
585 xd->pre.u_buffer = lst_yv12->u_buffer + recon_uvoffset;
586 xd->pre.v_buffer = lst_yv12->v_buffer + recon_uvoffset;
587
588 // In accumulating a score for the older reference frame
589 // take the best of the motion predicted score and
590 // the intra coded error (just as will be done for)
591 // accumulation of "coded_error" for the last frame.
592 if (gf_motion_error < this_error)
593 sr_coded_error += gf_motion_error;
594 else
595 sr_coded_error += this_error;
596 } else
597 sr_coded_error += motion_error;
598
599 /* Intra assumed best */
600 best_ref_mv.as_int = 0;
601
602 if (motion_error <= this_error) {
603 // Keep a count of cases where the inter and intra were
604 // very close and very low. This helps with scene cut
605 // detection for example in cropped clips with black bars
606 // at the sides or top and bottom.
607 if ((((this_error - intrapenalty) * 9) <=
608 (motion_error * 10)) &&
609 (this_error < (2 * intrapenalty))) {
610 neutral_count++;
611 }
612
613 mv.as_mv.row <<= 3;
614 mv.as_mv.col <<= 3;
615 this_error = motion_error;
616 vp9_set_mbmode_and_mvs(x, NEWMV, &mv);
617 xd->mode_info_context->mbmi.txfm_size = TX_4X4;
618 vp9_encode_inter16x16y(x);
619 sum_mvr += mv.as_mv.row;
620 sum_mvr_abs += abs(mv.as_mv.row);
621 sum_mvc += mv.as_mv.col;
622 sum_mvc_abs += abs(mv.as_mv.col);
623 sum_mvrs += mv.as_mv.row * mv.as_mv.row;
624 sum_mvcs += mv.as_mv.col * mv.as_mv.col;
625 intercount++;
626
627 best_ref_mv.as_int = mv.as_int;
628
629 // Was the vector non-zero
630 if (mv.as_int) {
631 mvcount++;
632
633 // Was it different from the last non zero vector
634 if (mv.as_int != lastmv_as_int)
635 new_mv_count++;
636 lastmv_as_int = mv.as_int;
637
638 // Does the Row vector point inwards or outwards
639 if (mb_row < cm->mb_rows / 2) {
640 if (mv.as_mv.row > 0)
641 sum_in_vectors--;
642 else if (mv.as_mv.row < 0)
643 sum_in_vectors++;
644 } else if (mb_row > cm->mb_rows / 2) {
645 if (mv.as_mv.row > 0)
646 sum_in_vectors++;
647 else if (mv.as_mv.row < 0)
648 sum_in_vectors--;
649 }
650
651 // Does the Row vector point inwards or outwards
652 if (mb_col < cm->mb_cols / 2) {
653 if (mv.as_mv.col > 0)
654 sum_in_vectors--;
655 else if (mv.as_mv.col < 0)
656 sum_in_vectors++;
657 } else if (mb_col > cm->mb_cols / 2) {
658 if (mv.as_mv.col > 0)
659 sum_in_vectors++;
660 else if (mv.as_mv.col < 0)
661 sum_in_vectors--;
662 }
663 }
664 }
665 } else
666 sr_coded_error += (int64_t)this_error;
667
668 coded_error += (int64_t)this_error;
669
670 // adjust to the next column of macroblocks
671 x->src.y_buffer += 16;
672 x->src.u_buffer += 8;
673 x->src.v_buffer += 8;
674
675 recon_yoffset += 16;
676 recon_uvoffset += 8;
677 }
678
679 // adjust to the next row of mbs
680 x->src.y_buffer += 16 * x->src.y_stride - 16 * cm->mb_cols;
681 x->src.u_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
682 x->src.v_buffer += 8 * x->src.uv_stride - 8 * cm->mb_cols;
683
684 // extend the recon for intra prediction
685 vp9_extend_mb_row(new_yv12, xd->dst.y_buffer + 16,
686 xd->dst.u_buffer + 8, xd->dst.v_buffer + 8);
687 vp9_clear_system_state(); // __asm emms;
688 }
689
690 vp9_clear_system_state(); // __asm emms;
691 {
692 double weight = 0.0;
693
694 FIRSTPASS_STATS fps;
695
696 fps.frame = cm->current_video_frame;
697 fps.intra_error = (double)(intra_error >> 8);
698 fps.coded_error = (double)(coded_error >> 8);
699 fps.sr_coded_error = (double)(sr_coded_error >> 8);
700 weight = simple_weight(cpi->Source);
701
702
703 if (weight < 0.1)
704 weight = 0.1;
705
706 fps.ssim_weighted_pred_err = fps.coded_error * weight;
707
708 fps.pcnt_inter = 0.0;
709 fps.pcnt_motion = 0.0;
710 fps.MVr = 0.0;
711 fps.mvr_abs = 0.0;
712 fps.MVc = 0.0;
713 fps.mvc_abs = 0.0;
714 fps.MVrv = 0.0;
715 fps.MVcv = 0.0;
716 fps.mv_in_out_count = 0.0;
717 fps.new_mv_count = 0.0;
718 fps.count = 1.0;
719
720 fps.pcnt_inter = 1.0 * (double)intercount / cm->MBs;
721 fps.pcnt_second_ref = 1.0 * (double)second_ref_count / cm->MBs;
722 fps.pcnt_neutral = 1.0 * (double)neutral_count / cm->MBs;
723
724 if (mvcount > 0) {
725 fps.MVr = (double)sum_mvr / (double)mvcount;
726 fps.mvr_abs = (double)sum_mvr_abs / (double)mvcount;
727 fps.MVc = (double)sum_mvc / (double)mvcount;
728 fps.mvc_abs = (double)sum_mvc_abs / (double)mvcount;
729 fps.MVrv = ((double)sum_mvrs - (fps.MVr * fps.MVr / (double)mvcount)) / (d ouble)mvcount;
730 fps.MVcv = ((double)sum_mvcs - (fps.MVc * fps.MVc / (double)mvcount)) / (d ouble)mvcount;
731 fps.mv_in_out_count = (double)sum_in_vectors / (double)(mvcount * 2);
732 fps.new_mv_count = new_mv_count;
733
734 fps.pcnt_motion = 1.0 * (double)mvcount / cpi->common.MBs;
735 }
736
737 // TODO: handle the case when duration is set to 0, or something less
738 // than the full time between subsequent cpi->source_time_stamp s .
739 fps.duration = (double)(cpi->source->ts_end
740 - cpi->source->ts_start);
741
742 // don't want to do output stats with a stack variable!
743 memcpy(cpi->twopass.this_frame_stats,
744 &fps,
745 sizeof(FIRSTPASS_STATS));
746 output_stats(cpi, cpi->output_pkt_list, cpi->twopass.this_frame_stats);
747 accumulate_stats(cpi->twopass.total_stats, &fps);
748 }
749
750 // Copy the previous Last Frame back into gf and and arf buffers if
751 // the prediction is good enough... but also dont allow it to lag too far
752 if ((cpi->twopass.sr_update_lag > 3) ||
753 ((cm->current_video_frame > 0) &&
754 (cpi->twopass.this_frame_stats->pcnt_inter > 0.20) &&
755 ((cpi->twopass.this_frame_stats->intra_error /
756 cpi->twopass.this_frame_stats->coded_error) > 2.0))) {
757 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
758 cpi->twopass.sr_update_lag = 1;
759 } else
760 cpi->twopass.sr_update_lag++;
761
762 // swap frame pointers so last frame refers to the frame we just compressed
763 vp9_swap_yv12_buffer(lst_yv12, new_yv12);
764 vp8_yv12_extend_frame_borders(lst_yv12);
765
766 // Special case for the first frame. Copy into the GF buffer as a second refer ence.
767 if (cm->current_video_frame == 0) {
768 vp8_yv12_copy_frame(lst_yv12, gld_yv12);
769 }
770
771
772 // use this to see what the first pass reconstruction looks like
773 if (0) {
774 char filename[512];
775 FILE *recon_file;
776 sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
777
778 if (cm->current_video_frame == 0)
779 recon_file = fopen(filename, "wb");
780 else
781 recon_file = fopen(filename, "ab");
782
783 (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
784 fclose(recon_file);
785 }
786
787 cm->current_video_frame++;
788
789 }
790
791 // Estimate a cost per mb attributable to overheads such as the coding of
792 // modes and motion vectors.
793 // Currently simplistic in its assumptions for testing.
794 //
795
796
797 static double bitcost(double prob) {
798 return -(log(prob) / log(2.0));
799 }
800
801 static long long estimate_modemvcost(VP9_COMP *cpi,
802 FIRSTPASS_STATS *fpstats) {
803 int mv_cost;
804 int mode_cost;
805
806 double av_pct_inter = fpstats->pcnt_inter / fpstats->count;
807 double av_pct_motion = fpstats->pcnt_motion / fpstats->count;
808 double av_intra = (1.0 - av_pct_inter);
809
810 double zz_cost;
811 double motion_cost;
812 double intra_cost;
813
814 zz_cost = bitcost(av_pct_inter - av_pct_motion);
815 motion_cost = bitcost(av_pct_motion);
816 intra_cost = bitcost(av_intra);
817
818 // Estimate of extra bits per mv overhead for mbs
819 // << 9 is the normalization to the (bits * 512) used in vp9_bits_per_mb
820 mv_cost = ((int)(fpstats->new_mv_count / fpstats->count) * 8) << 9;
821
822 // Crude estimate of overhead cost from modes
823 // << 9 is the normalization to (bits * 512) used in vp9_bits_per_mb
824 mode_cost =
825 (int)((((av_pct_inter - av_pct_motion) * zz_cost) +
826 (av_pct_motion * motion_cost) +
827 (av_intra * intra_cost)) * cpi->common.MBs) << 9;
828
829 // return mv_cost + mode_cost;
830 // TODO PGW Fix overhead costs for extended Q range
831 return 0;
832 }
833
834 static double calc_correction_factor(double err_per_mb,
835 double err_divisor,
836 double pt_low,
837 double pt_high,
838 int Q) {
839 double power_term;
840 double error_term = err_per_mb / err_divisor;
841 double correction_factor;
842
843 // Adjustment based on actual quantizer to power term.
844 power_term = (vp9_convert_qindex_to_q(Q) * 0.01) + pt_low;
845 power_term = (power_term > pt_high) ? pt_high : power_term;
846
847 // Adjustments to error term
848 // TBD
849
850 // Calculate correction factor
851 correction_factor = pow(error_term, power_term);
852
853 // Clip range
854 correction_factor =
855 (correction_factor < 0.05)
856 ? 0.05 : (correction_factor > 2.0) ? 2.0 : correction_factor;
857
858 return correction_factor;
859 }
860
861 // Given a current maxQ value sets a range for future values.
862 // PGW TODO..
863 // This code removes direct dependency on QIndex to determin the range
864 // (now uses the actual quantizer) but has not been tuned.
865 static void adjust_maxq_qrange(VP9_COMP *cpi) {
866 int i;
867 double q;
868
869 // Set the max corresponding to cpi->avg_q * 2.0
870 q = cpi->avg_q * 2.0;
871 cpi->twopass.maxq_max_limit = cpi->worst_quality;
872 for (i = cpi->best_quality; i <= cpi->worst_quality; i++) {
873 cpi->twopass.maxq_max_limit = i;
874 if (vp9_convert_qindex_to_q(i) >= q)
875 break;
876 }
877
878 // Set the min corresponding to cpi->avg_q * 0.5
879 q = cpi->avg_q * 0.5;
880 cpi->twopass.maxq_min_limit = cpi->best_quality;
881 for (i = cpi->worst_quality; i >= cpi->best_quality; i--) {
882 cpi->twopass.maxq_min_limit = i;
883 if (vp9_convert_qindex_to_q(i) <= q)
884 break;
885 }
886 }
887
888 static int estimate_max_q(VP9_COMP *cpi,
889 FIRSTPASS_STATS *fpstats,
890 int section_target_bandwitdh,
891 int overhead_bits) {
892 int Q;
893 int num_mbs = cpi->common.MBs;
894 int target_norm_bits_per_mb;
895
896 double section_err = (fpstats->coded_error / fpstats->count);
897 double sr_err_diff;
898 double sr_correction;
899 double err_per_mb = section_err / num_mbs;
900 double err_correction_factor;
901 double speed_correction = 1.0;
902 double overhead_bits_per_mb;
903
904 if (section_target_bandwitdh <= 0)
905 return cpi->twopass.maxq_max_limit; // Highest value allowed
906
907 target_norm_bits_per_mb =
908 (section_target_bandwitdh < (1 << 20))
909 ? (512 * section_target_bandwitdh) / num_mbs
910 : 512 * (section_target_bandwitdh / num_mbs);
911
912 // Look at the drop in prediction quality between the last frame
913 // and the GF buffer (which contained an older frame).
914 sr_err_diff =
915 (fpstats->sr_coded_error - fpstats->coded_error) /
916 (fpstats->count * cpi->common.MBs);
917 sr_correction = (sr_err_diff / 32.0);
918 sr_correction = pow(sr_correction, 0.25);
919 if (sr_correction < 0.75)
920 sr_correction = 0.75;
921 else if (sr_correction > 1.25)
922 sr_correction = 1.25;
923
924 // Calculate a corrective factor based on a rolling ratio of bits spent
925 // vs target bits
926 if ((cpi->rolling_target_bits > 0) &&
927 (cpi->active_worst_quality < cpi->worst_quality)) {
928 double rolling_ratio;
929
930 rolling_ratio = (double)cpi->rolling_actual_bits /
931 (double)cpi->rolling_target_bits;
932
933 if (rolling_ratio < 0.95)
934 cpi->twopass.est_max_qcorrection_factor -= 0.005;
935 else if (rolling_ratio > 1.05)
936 cpi->twopass.est_max_qcorrection_factor += 0.005;
937
938 cpi->twopass.est_max_qcorrection_factor =
939 (cpi->twopass.est_max_qcorrection_factor < 0.1)
940 ? 0.1
941 : (cpi->twopass.est_max_qcorrection_factor > 10.0)
942 ? 10.0 : cpi->twopass.est_max_qcorrection_factor;
943 }
944
945 // Corrections for higher compression speed settings
946 // (reduced compression expected)
947 if (cpi->compressor_speed == 1) {
948 if (cpi->oxcf.cpu_used <= 5)
949 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
950 else
951 speed_correction = 1.25;
952 }
953
954 // Estimate of overhead bits per mb
955 // Correction to overhead bits for min allowed Q.
956 // PGW TODO.. This code is broken for the extended Q range
957 // for now overhead set to 0.
958 overhead_bits_per_mb = overhead_bits / num_mbs;
959 overhead_bits_per_mb *= pow(0.98, (double)cpi->twopass.maxq_min_limit);
960
961 // Try and pick a max Q that will be high enough to encode the
962 // content at the given rate.
963 for (Q = cpi->twopass.maxq_min_limit; Q < cpi->twopass.maxq_max_limit; Q++) {
964 int bits_per_mb_at_this_q;
965
966 err_correction_factor =
967 calc_correction_factor(err_per_mb, ERR_DIVISOR, 0.4, 0.90, Q) *
968 sr_correction * speed_correction *
969 cpi->twopass.est_max_qcorrection_factor;
970
971 if (err_correction_factor < 0.05)
972 err_correction_factor = 0.05;
973 else if (err_correction_factor > 5.0)
974 err_correction_factor = 5.0;
975
976 bits_per_mb_at_this_q =
977 vp9_bits_per_mb(INTER_FRAME, Q) + (int)overhead_bits_per_mb;
978
979 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor *
980 (double)bits_per_mb_at_this_q);
981
982 // Mode and motion overhead
983 // As Q rises in real encode loop rd code will force overhead down
984 // We make a crude adjustment for this here as *.98 per Q step.
985 // PGW TODO.. This code is broken for the extended Q range
986 // for now overhead set to 0.
987 // overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
988
989 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
990 break;
991 }
992
993 // Restriction on active max q for constrained quality mode.
994 if ((cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) &&
995 (Q < cpi->cq_target_quality)) {
996 Q = cpi->cq_target_quality;
997 }
998
999 // Adjust maxq_min_limit and maxq_max_limit limits based on
1000 // averaga q observed in clip for non kf/gf/arf frames
1001 // Give average a chance to settle though.
1002 // PGW TODO.. This code is broken for the extended Q range
1003 if ((cpi->ni_frames >
1004 ((int)cpi->twopass.total_stats->count >> 8)) &&
1005 (cpi->ni_frames > 150)) {
1006 adjust_maxq_qrange(cpi);
1007 }
1008
1009 return Q;
1010 }
1011
1012 // For cq mode estimate a cq level that matches the observed
1013 // complexity and data rate.
1014 static int estimate_cq(VP9_COMP *cpi,
1015 FIRSTPASS_STATS *fpstats,
1016 int section_target_bandwitdh,
1017 int overhead_bits) {
1018 int Q;
1019 int num_mbs = cpi->common.MBs;
1020 int target_norm_bits_per_mb;
1021
1022 double section_err = (fpstats->coded_error / fpstats->count);
1023 double err_per_mb = section_err / num_mbs;
1024 double err_correction_factor;
1025 double sr_err_diff;
1026 double sr_correction;
1027 double speed_correction = 1.0;
1028 double clip_iiratio;
1029 double clip_iifactor;
1030 double overhead_bits_per_mb;
1031
1032
1033 target_norm_bits_per_mb = (section_target_bandwitdh < (1 << 20))
1034 ? (512 * section_target_bandwitdh) / num_mbs
1035 : 512 * (section_target_bandwitdh / num_mbs);
1036
1037 // Estimate of overhead bits per mb
1038 overhead_bits_per_mb = overhead_bits / num_mbs;
1039
1040 // Corrections for higher compression speed settings
1041 // (reduced compression expected)
1042 if (cpi->compressor_speed == 1) {
1043 if (cpi->oxcf.cpu_used <= 5)
1044 speed_correction = 1.04 + (cpi->oxcf.cpu_used * 0.04);
1045 else
1046 speed_correction = 1.25;
1047 }
1048
1049 // Look at the drop in prediction quality between the last frame
1050 // and the GF buffer (which contained an older frame).
1051 sr_err_diff =
1052 (fpstats->sr_coded_error - fpstats->coded_error) /
1053 (fpstats->count * cpi->common.MBs);
1054 sr_correction = (sr_err_diff / 32.0);
1055 sr_correction = pow(sr_correction, 0.25);
1056 if (sr_correction < 0.75)
1057 sr_correction = 0.75;
1058 else if (sr_correction > 1.25)
1059 sr_correction = 1.25;
1060
1061 // II ratio correction factor for clip as a whole
1062 clip_iiratio = cpi->twopass.total_stats->intra_error /
1063 DOUBLE_DIVIDE_CHECK(cpi->twopass.total_stats->coded_error);
1064 clip_iifactor = 1.0 - ((clip_iiratio - 10.0) * 0.025);
1065 if (clip_iifactor < 0.80)
1066 clip_iifactor = 0.80;
1067
1068 // Try and pick a Q that can encode the content at the given rate.
1069 for (Q = 0; Q < MAXQ; Q++) {
1070 int bits_per_mb_at_this_q;
1071
1072 // Error per MB based correction factor
1073 err_correction_factor =
1074 calc_correction_factor(err_per_mb, 100.0, 0.4, 0.90, Q) *
1075 sr_correction * speed_correction * clip_iifactor;
1076
1077 if (err_correction_factor < 0.05)
1078 err_correction_factor = 0.05;
1079 else if (err_correction_factor > 5.0)
1080 err_correction_factor = 5.0;
1081
1082 bits_per_mb_at_this_q =
1083 vp9_bits_per_mb(INTER_FRAME, Q) + (int)overhead_bits_per_mb;
1084
1085 bits_per_mb_at_this_q = (int)(.5 + err_correction_factor *
1086 (double)bits_per_mb_at_this_q);
1087
1088 // Mode and motion overhead
1089 // As Q rises in real encode loop rd code will force overhead down
1090 // We make a crude adjustment for this here as *.98 per Q step.
1091 // PGW TODO.. This code is broken for the extended Q range
1092 // for now overhead set to 0.
1093 overhead_bits_per_mb = (int)((double)overhead_bits_per_mb * 0.98);
1094
1095 if (bits_per_mb_at_this_q <= target_norm_bits_per_mb)
1096 break;
1097 }
1098
1099 // Clip value to range "best allowed to (worst allowed - 1)"
1100 Q = select_cq_level(Q);
1101 if (Q >= cpi->worst_quality)
1102 Q = cpi->worst_quality - 1;
1103 if (Q < cpi->best_quality)
1104 Q = cpi->best_quality;
1105
1106 return Q;
1107 }
1108
1109
1110 extern void vp9_new_frame_rate(VP9_COMP *cpi, double framerate);
1111
1112 void vp9_init_second_pass(VP9_COMP *cpi) {
1113 FIRSTPASS_STATS this_frame;
1114 FIRSTPASS_STATS *start_pos;
1115
1116 double lower_bounds_min_rate = FRAME_OVERHEAD_BITS * cpi->oxcf.frame_rate;
1117 double two_pass_min_rate = (double)(cpi->oxcf.target_bandwidth
1118 * cpi->oxcf.two_pass_vbrmin_section / 100) ;
1119
1120 if (two_pass_min_rate < lower_bounds_min_rate)
1121 two_pass_min_rate = lower_bounds_min_rate;
1122
1123 zero_stats(cpi->twopass.total_stats);
1124 zero_stats(cpi->twopass.total_left_stats);
1125
1126 if (!cpi->twopass.stats_in_end)
1127 return;
1128
1129 *cpi->twopass.total_stats = *cpi->twopass.stats_in_end;
1130 *cpi->twopass.total_left_stats = *cpi->twopass.total_stats;
1131
1132 // each frame can have a different duration, as the frame rate in the source
1133 // isn't guaranteed to be constant. The frame rate prior to the first frame
1134 // encoded in the second pass is a guess. However the sum duration is not.
1135 // Its calculated based on the actual durations of all frames from the first
1136 // pass.
1137 vp9_new_frame_rate(cpi,
1138 10000000.0 * cpi->twopass.total_stats->count /
1139 cpi->twopass.total_stats->duration);
1140
1141 cpi->output_frame_rate = cpi->oxcf.frame_rate;
1142 cpi->twopass.bits_left = (int64_t)(cpi->twopass.total_stats->duration *
1143 cpi->oxcf.target_bandwidth / 10000000.0);
1144 cpi->twopass.bits_left -= (int64_t)(cpi->twopass.total_stats->duration *
1145 two_pass_min_rate / 10000000.0);
1146
1147 // Calculate a minimum intra value to be used in determining the IIratio
1148 // scores used in the second pass. We have this minimum to make sure
1149 // that clips that are static but "low complexity" in the intra domain
1150 // are still boosted appropriately for KF/GF/ARF
1151 cpi->twopass.kf_intra_err_min = KF_MB_INTRA_MIN * cpi->common.MBs;
1152 cpi->twopass.gf_intra_err_min = GF_MB_INTRA_MIN * cpi->common.MBs;
1153
1154 // This variable monitors how far behind the second ref update is lagging
1155 cpi->twopass.sr_update_lag = 1;
1156
1157 // Scan the first pass file and calculate an average Intra / Inter error score ratio for the sequence
1158 {
1159 double sum_iiratio = 0.0;
1160 double IIRatio;
1161
1162 start_pos = cpi->twopass.stats_in; // Note starting "file" pos ition
1163
1164 while (input_stats(cpi, &this_frame) != EOF) {
1165 IIRatio = this_frame.intra_error / DOUBLE_DIVIDE_CHECK(this_frame.coded_er ror);
1166 IIRatio = (IIRatio < 1.0) ? 1.0 : (IIRatio > 20.0) ? 20.0 : IIRatio;
1167 sum_iiratio += IIRatio;
1168 }
1169
1170 cpi->twopass.avg_iiratio = sum_iiratio / DOUBLE_DIVIDE_CHECK((double)cpi->tw opass.total_stats->count);
1171
1172 // Reset file position
1173 reset_fpf_position(cpi, start_pos);
1174 }
1175
1176 // Scan the first pass file and calculate a modified total error based upon th e bias/power function
1177 // used to allocate bits
1178 {
1179 start_pos = cpi->twopass.stats_in; // Note starting "file" pos ition
1180
1181 cpi->twopass.modified_error_total = 0.0;
1182 cpi->twopass.modified_error_used = 0.0;
1183
1184 while (input_stats(cpi, &this_frame) != EOF) {
1185 cpi->twopass.modified_error_total += calculate_modified_err(cpi, &this_fra me);
1186 }
1187 cpi->twopass.modified_error_left = cpi->twopass.modified_error_total;
1188
1189 reset_fpf_position(cpi, start_pos); // Reset file position
1190
1191 }
1192 }
1193
1194 void vp9_end_second_pass(VP9_COMP *cpi) {
1195 }
1196
1197 // This function gives and estimate of how badly we believe
1198 // the prediction quality is decaying from frame to frame.
1199 static double get_prediction_decay_rate(VP9_COMP *cpi,
1200 FIRSTPASS_STATS *next_frame) {
1201 double prediction_decay_rate;
1202 double second_ref_decay;
1203 double mb_sr_err_diff;
1204
1205 // Initial basis is the % mbs inter coded
1206 prediction_decay_rate = next_frame->pcnt_inter;
1207
1208 // Look at the observed drop in prediction quality between the last frame
1209 // and the GF buffer (which contains an older frame).
1210 mb_sr_err_diff =
1211 (next_frame->sr_coded_error - next_frame->coded_error) /
1212 (cpi->common.MBs);
1213 second_ref_decay = 1.0 - (mb_sr_err_diff / 512.0);
1214 second_ref_decay = pow(second_ref_decay, 0.5);
1215 if (second_ref_decay < 0.85)
1216 second_ref_decay = 0.85;
1217 else if (second_ref_decay > 1.0)
1218 second_ref_decay = 1.0;
1219
1220 if (second_ref_decay < prediction_decay_rate)
1221 prediction_decay_rate = second_ref_decay;
1222
1223 return prediction_decay_rate;
1224 }
1225
1226 // Function to test for a condition where a complex transition is followed
1227 // by a static section. For example in slide shows where there is a fade
1228 // between slides. This is to help with more optimal kf and gf positioning.
1229 static int detect_transition_to_still(
1230 VP9_COMP *cpi,
1231 int frame_interval,
1232 int still_interval,
1233 double loop_decay_rate,
1234 double last_decay_rate) {
1235 BOOL trans_to_still = FALSE;
1236
1237 // Break clause to detect very still sections after motion
1238 // For example a static image after a fade or other transition
1239 // instead of a clean scene cut.
1240 if ((frame_interval > MIN_GF_INTERVAL) &&
1241 (loop_decay_rate >= 0.999) &&
1242 (last_decay_rate < 0.9)) {
1243 int j;
1244 FIRSTPASS_STATS *position = cpi->twopass.stats_in;
1245 FIRSTPASS_STATS tmp_next_frame;
1246 double zz_inter;
1247
1248 // Look ahead a few frames to see if static condition
1249 // persists...
1250 for (j = 0; j < still_interval; j++) {
1251 if (EOF == input_stats(cpi, &tmp_next_frame))
1252 break;
1253
1254 zz_inter =
1255 (tmp_next_frame.pcnt_inter - tmp_next_frame.pcnt_motion);
1256 if (zz_inter < 0.999)
1257 break;
1258 }
1259 // Reset file position
1260 reset_fpf_position(cpi, position);
1261
1262 // Only if it does do we signal a transition to still
1263 if (j == still_interval)
1264 trans_to_still = TRUE;
1265 }
1266
1267 return trans_to_still;
1268 }
1269
1270 // This function detects a flash through the high relative pcnt_second_ref
1271 // score in the frame following a flash frame. The offset passed in should
1272 // reflect this
1273 static BOOL detect_flash(VP9_COMP *cpi, int offset) {
1274 FIRSTPASS_STATS next_frame;
1275
1276 BOOL flash_detected = FALSE;
1277
1278 // Read the frame data.
1279 // The return is FALSE (no flash detected) if not a valid frame
1280 if (read_frame_stats(cpi, &next_frame, offset) != EOF) {
1281 // What we are looking for here is a situation where there is a
1282 // brief break in prediction (such as a flash) but subsequent frames
1283 // are reasonably well predicted by an earlier (pre flash) frame.
1284 // The recovery after a flash is indicated by a high pcnt_second_ref
1285 // comapred to pcnt_inter.
1286 if ((next_frame.pcnt_second_ref > next_frame.pcnt_inter) &&
1287 (next_frame.pcnt_second_ref >= 0.5)) {
1288 flash_detected = TRUE;
1289 }
1290 }
1291
1292 return flash_detected;
1293 }
1294
1295 // Update the motion related elements to the GF arf boost calculation
1296 static void accumulate_frame_motion_stats(
1297 VP9_COMP *cpi,
1298 FIRSTPASS_STATS *this_frame,
1299 double *this_frame_mv_in_out,
1300 double *mv_in_out_accumulator,
1301 double *abs_mv_in_out_accumulator,
1302 double *mv_ratio_accumulator) {
1303 // double this_frame_mv_in_out;
1304 double this_frame_mvr_ratio;
1305 double this_frame_mvc_ratio;
1306 double motion_pct;
1307
1308 // Accumulate motion stats.
1309 motion_pct = this_frame->pcnt_motion;
1310
1311 // Accumulate Motion In/Out of frame stats
1312 *this_frame_mv_in_out = this_frame->mv_in_out_count * motion_pct;
1313 *mv_in_out_accumulator += this_frame->mv_in_out_count * motion_pct;
1314 *abs_mv_in_out_accumulator +=
1315 fabs(this_frame->mv_in_out_count * motion_pct);
1316
1317 // Accumulate a measure of how uniform (or conversely how random)
1318 // the motion field is. (A ratio of absmv / mv)
1319 if (motion_pct > 0.05) {
1320 this_frame_mvr_ratio = fabs(this_frame->mvr_abs) /
1321 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVr));
1322
1323 this_frame_mvc_ratio = fabs(this_frame->mvc_abs) /
1324 DOUBLE_DIVIDE_CHECK(fabs(this_frame->MVc));
1325
1326 *mv_ratio_accumulator +=
1327 (this_frame_mvr_ratio < this_frame->mvr_abs)
1328 ? (this_frame_mvr_ratio * motion_pct)
1329 : this_frame->mvr_abs * motion_pct;
1330
1331 *mv_ratio_accumulator +=
1332 (this_frame_mvc_ratio < this_frame->mvc_abs)
1333 ? (this_frame_mvc_ratio * motion_pct)
1334 : this_frame->mvc_abs * motion_pct;
1335
1336 }
1337 }
1338
1339 // Calculate a baseline boost number for the current frame.
1340 static double calc_frame_boost(
1341 VP9_COMP *cpi,
1342 FIRSTPASS_STATS *this_frame,
1343 double this_frame_mv_in_out) {
1344 double frame_boost;
1345
1346 // Underlying boost factor is based on inter intra error ratio
1347 if (this_frame->intra_error > cpi->twopass.gf_intra_err_min)
1348 frame_boost = (IIFACTOR * this_frame->intra_error /
1349 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1350 else
1351 frame_boost = (IIFACTOR * cpi->twopass.gf_intra_err_min /
1352 DOUBLE_DIVIDE_CHECK(this_frame->coded_error));
1353
1354 // Increase boost for frames where new data coming into frame
1355 // (eg zoom out). Slightly reduce boost if there is a net balance
1356 // of motion out of the frame (zoom in).
1357 // The range for this_frame_mv_in_out is -1.0 to +1.0
1358 if (this_frame_mv_in_out > 0.0)
1359 frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1360 // In extreme case boost is halved
1361 else
1362 frame_boost += frame_boost * (this_frame_mv_in_out / 2.0);
1363
1364 // Clip to maximum
1365 if (frame_boost > GF_RMAX)
1366 frame_boost = GF_RMAX;
1367
1368 return frame_boost;
1369 }
1370
1371 static int calc_arf_boost(
1372 VP9_COMP *cpi,
1373 int offset,
1374 int f_frames,
1375 int b_frames,
1376 int *f_boost,
1377 int *b_boost) {
1378 FIRSTPASS_STATS this_frame;
1379
1380 int i;
1381 double boost_score = 0.0;
1382 double mv_ratio_accumulator = 0.0;
1383 double decay_accumulator = 1.0;
1384 double this_frame_mv_in_out = 0.0;
1385 double mv_in_out_accumulator = 0.0;
1386 double abs_mv_in_out_accumulator = 0.0;
1387 int arf_boost;
1388 BOOL flash_detected = FALSE;
1389
1390 // Search forward from the proposed arf/next gf position
1391 for (i = 0; i < f_frames; i++) {
1392 if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF)
1393 break;
1394
1395 // Update the motion related elements to the boost calculation
1396 accumulate_frame_motion_stats(cpi, &this_frame,
1397 &this_frame_mv_in_out, &mv_in_out_accumulator,
1398 &abs_mv_in_out_accumulator, &mv_ratio_accumula tor);
1399
1400 // We want to discount the the flash frame itself and the recovery
1401 // frame that follows as both will have poor scores.
1402 flash_detected = detect_flash(cpi, (i + offset)) ||
1403 detect_flash(cpi, (i + offset + 1));
1404
1405 // Cumulative effect of prediction quality decay
1406 if (!flash_detected) {
1407 decay_accumulator =
1408 decay_accumulator *
1409 get_prediction_decay_rate(cpi, &this_frame);
1410 decay_accumulator =
1411 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1412 }
1413
1414 boost_score += (decay_accumulator *
1415 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1416 }
1417
1418 *f_boost = (int)boost_score;
1419
1420 // Reset for backward looking loop
1421 boost_score = 0.0;
1422 mv_ratio_accumulator = 0.0;
1423 decay_accumulator = 1.0;
1424 this_frame_mv_in_out = 0.0;
1425 mv_in_out_accumulator = 0.0;
1426 abs_mv_in_out_accumulator = 0.0;
1427
1428 // Search backward towards last gf position
1429 for (i = -1; i >= -b_frames; i--) {
1430 if (read_frame_stats(cpi, &this_frame, (i + offset)) == EOF)
1431 break;
1432
1433 // Update the motion related elements to the boost calculation
1434 accumulate_frame_motion_stats(cpi, &this_frame,
1435 &this_frame_mv_in_out, &mv_in_out_accumulator,
1436 &abs_mv_in_out_accumulator, &mv_ratio_accumula tor);
1437
1438 // We want to discount the the flash frame itself and the recovery
1439 // frame that follows as both will have poor scores.
1440 flash_detected = detect_flash(cpi, (i + offset)) ||
1441 detect_flash(cpi, (i + offset + 1));
1442
1443 // Cumulative effect of prediction quality decay
1444 if (!flash_detected) {
1445 decay_accumulator =
1446 decay_accumulator *
1447 get_prediction_decay_rate(cpi, &this_frame);
1448 decay_accumulator =
1449 decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
1450 }
1451
1452 boost_score += (decay_accumulator *
1453 calc_frame_boost(cpi, &this_frame, this_frame_mv_in_out));
1454
1455 }
1456 *b_boost = (int)boost_score;
1457
1458 arf_boost = (*f_boost + *b_boost);
1459 if (arf_boost < ((b_frames + f_frames) * 20))
1460 arf_boost = ((b_frames + f_frames) * 20);
1461
1462 return arf_boost;
1463 }
1464
1465 static void configure_arnr_filter(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1466 int half_gf_int;
1467 int frames_after_arf;
1468 int frames_bwd = cpi->oxcf.arnr_max_frames - 1;
1469 int frames_fwd = cpi->oxcf.arnr_max_frames - 1;
1470
1471 // Define the arnr filter width for this group of frames:
1472 // We only filter frames that lie within a distance of half
1473 // the GF interval from the ARF frame. We also have to trap
1474 // cases where the filter extends beyond the end of clip.
1475 // Note: this_frame->frame has been updated in the loop
1476 // so it now points at the ARF frame.
1477 half_gf_int = cpi->baseline_gf_interval >> 1;
1478 frames_after_arf = (int)(cpi->twopass.total_stats->count -
1479 this_frame->frame - 1);
1480
1481 switch (cpi->oxcf.arnr_type) {
1482 case 1: // Backward filter
1483 frames_fwd = 0;
1484 if (frames_bwd > half_gf_int)
1485 frames_bwd = half_gf_int;
1486 break;
1487
1488 case 2: // Forward filter
1489 if (frames_fwd > half_gf_int)
1490 frames_fwd = half_gf_int;
1491 if (frames_fwd > frames_after_arf)
1492 frames_fwd = frames_after_arf;
1493 frames_bwd = 0;
1494 break;
1495
1496 case 3: // Centered filter
1497 default:
1498 frames_fwd >>= 1;
1499 if (frames_fwd > frames_after_arf)
1500 frames_fwd = frames_after_arf;
1501 if (frames_fwd > half_gf_int)
1502 frames_fwd = half_gf_int;
1503
1504 frames_bwd = frames_fwd;
1505
1506 // For even length filter there is one more frame backward
1507 // than forward: e.g. len=6 ==> bbbAff, len=7 ==> bbbAfff.
1508 if (frames_bwd < half_gf_int)
1509 frames_bwd += (cpi->oxcf.arnr_max_frames + 1) & 0x1;
1510 break;
1511 }
1512
1513 cpi->active_arnr_frames = frames_bwd + 1 + frames_fwd;
1514 }
1515
1516 // Analyse and define a gf/arf group .
1517 static void define_gf_group(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1518 FIRSTPASS_STATS next_frame;
1519 FIRSTPASS_STATS *start_pos;
1520 int i;
1521 double boost_score = 0.0;
1522 double old_boost_score = 0.0;
1523 double gf_group_err = 0.0;
1524 double gf_first_frame_err = 0.0;
1525 double mod_frame_err = 0.0;
1526
1527 double mv_ratio_accumulator = 0.0;
1528 double decay_accumulator = 1.0;
1529 double zero_motion_accumulator = 1.0;
1530
1531 double loop_decay_rate = 1.00; // Starting decay rate
1532 double last_loop_decay_rate = 1.00;
1533
1534 double this_frame_mv_in_out = 0.0;
1535 double mv_in_out_accumulator = 0.0;
1536 double abs_mv_in_out_accumulator = 0.0;
1537
1538 int max_bits = frame_max_bits(cpi); // Max for a single frame
1539
1540 unsigned int allow_alt_ref =
1541 cpi->oxcf.play_alternate && cpi->oxcf.lag_in_frames;
1542
1543 int f_boost = 0;
1544 int b_boost = 0;
1545 BOOL flash_detected;
1546
1547 cpi->twopass.gf_group_bits = 0;
1548
1549 vp9_clear_system_state(); // __asm emms;
1550
1551 start_pos = cpi->twopass.stats_in;
1552
1553 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
1554
1555 // Load stats for the current frame.
1556 mod_frame_err = calculate_modified_err(cpi, this_frame);
1557
1558 // Note the error of the frame at the start of the group (this will be
1559 // the GF frame error if we code a normal gf
1560 gf_first_frame_err = mod_frame_err;
1561
1562 // Special treatment if the current frame is a key frame (which is also
1563 // a gf). If it is then its error score (and hence bit allocation) need
1564 // to be subtracted out from the calculation for the GF group
1565 if (cpi->common.frame_type == KEY_FRAME)
1566 gf_group_err -= gf_first_frame_err;
1567
1568 // Scan forward to try and work out how many frames the next gf group
1569 // should contain and what level of boost is appropriate for the GF
1570 // or ARF that will be coded with the group
1571 i = 0;
1572
1573 while (((i < cpi->twopass.static_scene_max_gf_interval) ||
1574 ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL)) &&
1575 (i < cpi->twopass.frames_to_key)) {
1576 i++; // Increment the loop counter
1577
1578 // Accumulate error score of frames in this gf group
1579 mod_frame_err = calculate_modified_err(cpi, this_frame);
1580 gf_group_err += mod_frame_err;
1581
1582 if (EOF == input_stats(cpi, &next_frame))
1583 break;
1584
1585 // Test for the case where there is a brief flash but the prediction
1586 // quality back to an earlier frame is then restored.
1587 flash_detected = detect_flash(cpi, 0);
1588
1589 // Update the motion related elements to the boost calculation
1590 accumulate_frame_motion_stats(cpi, &next_frame,
1591 &this_frame_mv_in_out, &mv_in_out_accumulator,
1592 &abs_mv_in_out_accumulator, &mv_ratio_accumula tor);
1593
1594 // Cumulative effect of prediction quality decay
1595 if (!flash_detected) {
1596 last_loop_decay_rate = loop_decay_rate;
1597 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
1598 decay_accumulator = decay_accumulator * loop_decay_rate;
1599
1600 // Monitor for static sections.
1601 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
1602 zero_motion_accumulator) {
1603 zero_motion_accumulator =
1604 (next_frame.pcnt_inter - next_frame.pcnt_motion);
1605 }
1606
1607 // Break clause to detect very still sections after motion
1608 // (for example a staic image after a fade or other transition).
1609 if (detect_transition_to_still(cpi, i, 5, loop_decay_rate,
1610 last_loop_decay_rate)) {
1611 allow_alt_ref = FALSE;
1612 break;
1613 }
1614 }
1615
1616 // Calculate a boost number for this frame
1617 boost_score +=
1618 (decay_accumulator *
1619 calc_frame_boost(cpi, &next_frame, this_frame_mv_in_out));
1620
1621 // Break out conditions.
1622 if (
1623 // Break at cpi->max_gf_interval unless almost totally static
1624 (i >= cpi->max_gf_interval && (zero_motion_accumulator < 0.995)) ||
1625 (
1626 // Dont break out with a very short interval
1627 (i > MIN_GF_INTERVAL) &&
1628 // Dont break out very close to a key frame
1629 ((cpi->twopass.frames_to_key - i) >= MIN_GF_INTERVAL) &&
1630 ((boost_score > 125.0) || (next_frame.pcnt_inter < 0.75)) &&
1631 (!flash_detected) &&
1632 ((mv_ratio_accumulator > 100.0) ||
1633 (abs_mv_in_out_accumulator > 3.0) ||
1634 (mv_in_out_accumulator < -2.0) ||
1635 ((boost_score - old_boost_score) < 12.5))
1636 )) {
1637 boost_score = old_boost_score;
1638 break;
1639 }
1640
1641 vpx_memcpy(this_frame, &next_frame, sizeof(*this_frame));
1642
1643 old_boost_score = boost_score;
1644 }
1645
1646 // Dont allow a gf too near the next kf
1647 if ((cpi->twopass.frames_to_key - i) < MIN_GF_INTERVAL) {
1648 while (i < cpi->twopass.frames_to_key) {
1649 i++;
1650
1651 if (EOF == input_stats(cpi, this_frame))
1652 break;
1653
1654 if (i < cpi->twopass.frames_to_key) {
1655 mod_frame_err = calculate_modified_err(cpi, this_frame);
1656 gf_group_err += mod_frame_err;
1657 }
1658 }
1659 }
1660
1661 // Set the interval till the next gf or arf.
1662 cpi->baseline_gf_interval = i;
1663
1664 // Should we use the alternate refernce frame
1665 if (allow_alt_ref &&
1666 (i < cpi->oxcf.lag_in_frames) &&
1667 (i >= MIN_GF_INTERVAL) &&
1668 // dont use ARF very near next kf
1669 (i <= (cpi->twopass.frames_to_key - MIN_GF_INTERVAL)) &&
1670 ((next_frame.pcnt_inter > 0.75) ||
1671 (next_frame.pcnt_second_ref > 0.5)) &&
1672 ((mv_in_out_accumulator / (double)i > -0.2) ||
1673 (mv_in_out_accumulator > -2.0)) &&
1674 (boost_score > 100)) {
1675 // Alterrnative boost calculation for alt ref
1676 cpi->gfu_boost = calc_arf_boost(cpi, 0, (i - 1), (i - 1), &f_boost, &b_boost );
1677 cpi->source_alt_ref_pending = TRUE;
1678
1679 configure_arnr_filter(cpi, this_frame);
1680 } else {
1681 cpi->gfu_boost = (int)boost_score;
1682 cpi->source_alt_ref_pending = FALSE;
1683 }
1684
1685 // Now decide how many bits should be allocated to the GF group as a
1686 // proportion of those remaining in the kf group.
1687 // The final key frame group in the clip is treated as a special case
1688 // where cpi->twopass.kf_group_bits is tied to cpi->twopass.bits_left.
1689 // This is also important for short clips where there may only be one
1690 // key frame.
1691 if (cpi->twopass.frames_to_key >= (int)(cpi->twopass.total_stats->count -
1692 cpi->common.current_video_frame)) {
1693 cpi->twopass.kf_group_bits =
1694 (cpi->twopass.bits_left > 0) ? cpi->twopass.bits_left : 0;
1695 }
1696
1697 // Calculate the bits to be allocated to the group as a whole
1698 if ((cpi->twopass.kf_group_bits > 0) &&
1699 (cpi->twopass.kf_group_error_left > 0)) {
1700 cpi->twopass.gf_group_bits =
1701 (int)((double)cpi->twopass.kf_group_bits *
1702 (gf_group_err / cpi->twopass.kf_group_error_left));
1703 } else
1704 cpi->twopass.gf_group_bits = 0;
1705
1706 cpi->twopass.gf_group_bits =
1707 (cpi->twopass.gf_group_bits < 0)
1708 ? 0
1709 : (cpi->twopass.gf_group_bits > cpi->twopass.kf_group_bits)
1710 ? cpi->twopass.kf_group_bits : cpi->twopass.gf_group_bits;
1711
1712 // Clip cpi->twopass.gf_group_bits based on user supplied data rate
1713 // variability limit (cpi->oxcf.two_pass_vbrmax_section)
1714 if (cpi->twopass.gf_group_bits > max_bits * cpi->baseline_gf_interval)
1715 cpi->twopass.gf_group_bits = max_bits * cpi->baseline_gf_interval;
1716
1717 // Reset the file position
1718 reset_fpf_position(cpi, start_pos);
1719
1720 // Update the record of error used so far (only done once per gf group)
1721 cpi->twopass.modified_error_used += gf_group_err;
1722
1723 // Assign bits to the arf or gf.
1724 for (i = 0; i <= (cpi->source_alt_ref_pending && cpi->common.frame_type != KEY _FRAME); i++) {
1725 int boost;
1726 int allocation_chunks;
1727 int Q = (cpi->oxcf.fixed_q < 0) ? cpi->last_q[INTER_FRAME] : cpi->oxcf.fixed _q;
1728 int gf_bits;
1729
1730 boost = (cpi->gfu_boost * vp9_gfboost_qadjust(Q)) / 100;
1731
1732 // Set max and minimum boost and hence minimum allocation
1733 if (boost > ((cpi->baseline_gf_interval + 1) * 200))
1734 boost = ((cpi->baseline_gf_interval + 1) * 200);
1735 else if (boost < 125)
1736 boost = 125;
1737
1738 if (cpi->source_alt_ref_pending && i == 0)
1739 allocation_chunks =
1740 ((cpi->baseline_gf_interval + 1) * 100) + boost;
1741 else
1742 allocation_chunks =
1743 (cpi->baseline_gf_interval * 100) + (boost - 100);
1744
1745 // Prevent overflow
1746 if (boost > 1028) {
1747 int divisor = boost >> 10;
1748 boost /= divisor;
1749 allocation_chunks /= divisor;
1750 }
1751
1752 // Calculate the number of bits to be spent on the gf or arf based on
1753 // the boost number
1754 gf_bits = (int)((double)boost *
1755 (cpi->twopass.gf_group_bits /
1756 (double)allocation_chunks));
1757
1758 // If the frame that is to be boosted is simpler than the average for
1759 // the gf/arf group then use an alternative calculation
1760 // based on the error score of the frame itself
1761 if (mod_frame_err < gf_group_err / (double)cpi->baseline_gf_interval) {
1762 double alt_gf_grp_bits;
1763 int alt_gf_bits;
1764
1765 alt_gf_grp_bits =
1766 (double)cpi->twopass.kf_group_bits *
1767 (mod_frame_err * (double)cpi->baseline_gf_interval) /
1768 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left);
1769
1770 alt_gf_bits = (int)((double)boost * (alt_gf_grp_bits /
1771 (double)allocation_chunks));
1772
1773 if (gf_bits > alt_gf_bits) {
1774 gf_bits = alt_gf_bits;
1775 }
1776 }
1777 // Else if it is harder than other frames in the group make sure it at
1778 // least receives an allocation in keeping with its relative error
1779 // score, otherwise it may be worse off than an "un-boosted" frame
1780 else {
1781 int alt_gf_bits =
1782 (int)((double)cpi->twopass.kf_group_bits *
1783 mod_frame_err /
1784 DOUBLE_DIVIDE_CHECK(cpi->twopass.kf_group_error_left));
1785
1786 if (alt_gf_bits > gf_bits) {
1787 gf_bits = alt_gf_bits;
1788 }
1789 }
1790
1791 // Dont allow a negative value for gf_bits
1792 if (gf_bits < 0)
1793 gf_bits = 0;
1794
1795 gf_bits += cpi->min_frame_bandwidth; // Add in minimum f or a frame
1796
1797 if (i == 0) {
1798 cpi->twopass.gf_bits = gf_bits;
1799 }
1800 if (i == 1 || (!cpi->source_alt_ref_pending && (cpi->common.frame_type != KE Y_FRAME))) {
1801 cpi->per_frame_bandwidth = gf_bits; // Per frame bit targe t for this frame
1802 }
1803 }
1804
1805 {
1806 // Adjust KF group bits and error remainin
1807 cpi->twopass.kf_group_error_left -= (int64_t)gf_group_err;
1808 cpi->twopass.kf_group_bits -= cpi->twopass.gf_group_bits;
1809
1810 if (cpi->twopass.kf_group_bits < 0)
1811 cpi->twopass.kf_group_bits = 0;
1812
1813 // Note the error score left in the remaining frames of the group.
1814 // For normal GFs we want to remove the error score for the first frame
1815 // of the group (except in Key frame case where this has already
1816 // happened)
1817 if (!cpi->source_alt_ref_pending && cpi->common.frame_type != KEY_FRAME)
1818 cpi->twopass.gf_group_error_left = (int64_t)(gf_group_err
1819 - gf_first_frame_err);
1820 else
1821 cpi->twopass.gf_group_error_left = (int64_t)gf_group_err;
1822
1823 cpi->twopass.gf_group_bits -= cpi->twopass.gf_bits - cpi->min_frame_bandwidt h;
1824
1825 if (cpi->twopass.gf_group_bits < 0)
1826 cpi->twopass.gf_group_bits = 0;
1827
1828 // This condition could fail if there are two kfs very close together
1829 // despite (MIN_GF_INTERVAL) and would cause a devide by 0 in the
1830 // calculation of cpi->twopass.alt_extra_bits.
1831 if (cpi->baseline_gf_interval >= 3) {
1832 int boost = (cpi->source_alt_ref_pending)
1833 ? b_boost : cpi->gfu_boost;
1834
1835 if (boost >= 150) {
1836 int pct_extra;
1837
1838 pct_extra = (boost - 100) / 50;
1839 pct_extra = (pct_extra > 20) ? 20 : pct_extra;
1840
1841 cpi->twopass.alt_extra_bits = (int)
1842 ((cpi->twopass.gf_group_bits * pct_extra) / 100);
1843 cpi->twopass.gf_group_bits -= cpi->twopass.alt_extra_bits;
1844 cpi->twopass.alt_extra_bits /=
1845 ((cpi->baseline_gf_interval - 1) >> 1);
1846 } else
1847 cpi->twopass.alt_extra_bits = 0;
1848 } else
1849 cpi->twopass.alt_extra_bits = 0;
1850 }
1851
1852 if (cpi->common.frame_type != KEY_FRAME) {
1853 FIRSTPASS_STATS sectionstats;
1854
1855 zero_stats(&sectionstats);
1856 reset_fpf_position(cpi, start_pos);
1857
1858 for (i = 0; i < cpi->baseline_gf_interval; i++) {
1859 input_stats(cpi, &next_frame);
1860 accumulate_stats(&sectionstats, &next_frame);
1861 }
1862
1863 avg_stats(&sectionstats);
1864
1865 cpi->twopass.section_intra_rating = (int)
1866 (sectionstats.intra_error /
1867 DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
1868
1869 reset_fpf_position(cpi, start_pos);
1870 }
1871 }
1872
1873 // Allocate bits to a normal frame that is neither a gf an arf or a key frame.
1874 static void assign_std_frame_bits(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
1875 int target_frame_size; // gf_group_error_left
1876
1877 double modified_err;
1878 double err_fraction; // What portion of the remaining GF group error is used by this frame
1879
1880 int max_bits = frame_max_bits(cpi); // Max for a single frame
1881
1882 // Calculate modified prediction error used in bit allocation
1883 modified_err = calculate_modified_err(cpi, this_frame);
1884
1885 if (cpi->twopass.gf_group_error_left > 0)
1886 err_fraction = modified_err / cpi->twopass.gf_group_error_left; // What portion of the remaining GF group error is used by this frame
1887 else
1888 err_fraction = 0.0;
1889
1890 target_frame_size = (int)((double)cpi->twopass.gf_group_bits * err_fraction); // How many of those bits available for allocation should we give it?
1891
1892 // Clip to target size to 0 - max_bits (or cpi->twopass.gf_group_bits) at the top end.
1893 if (target_frame_size < 0)
1894 target_frame_size = 0;
1895 else {
1896 if (target_frame_size > max_bits)
1897 target_frame_size = max_bits;
1898
1899 if (target_frame_size > cpi->twopass.gf_group_bits)
1900 target_frame_size = (int)cpi->twopass.gf_group_bits;
1901 }
1902
1903 // Adjust error remaining
1904 cpi->twopass.gf_group_error_left -= (int64_t)modified_err;
1905 cpi->twopass.gf_group_bits -= target_frame_size; // Adjust bits remaining
1906
1907 if (cpi->twopass.gf_group_bits < 0)
1908 cpi->twopass.gf_group_bits = 0;
1909
1910 target_frame_size += cpi->min_frame_bandwidth; // Add in the minimum number of bits that is set aside for every frame .
1911
1912
1913 cpi->per_frame_bandwidth = target_frame_size; // Per frame bit target for this frame
1914 }
1915
1916 // Make a damped adjustment to the active max q.
1917 static int adjust_active_maxq(int old_maxqi, int new_maxqi) {
1918 int i;
1919 int ret_val = new_maxqi;
1920 double old_q;
1921 double new_q;
1922 double target_q;
1923
1924 old_q = vp9_convert_qindex_to_q(old_maxqi);
1925 new_q = vp9_convert_qindex_to_q(new_maxqi);
1926
1927 target_q = ((old_q * 7.0) + new_q) / 8.0;
1928
1929 if (target_q > old_q) {
1930 for (i = old_maxqi; i <= new_maxqi; i++) {
1931 if (vp9_convert_qindex_to_q(i) >= target_q) {
1932 ret_val = i;
1933 break;
1934 }
1935 }
1936 } else {
1937 for (i = old_maxqi; i >= new_maxqi; i--) {
1938 if (vp9_convert_qindex_to_q(i) <= target_q) {
1939 ret_val = i;
1940 break;
1941 }
1942 }
1943 }
1944
1945 return ret_val;
1946 }
1947
1948 void vp9_second_pass(VP9_COMP *cpi) {
1949 int tmp_q;
1950 int frames_left = (int)(cpi->twopass.total_stats->count - cpi->common.current_ video_frame);
1951
1952 FIRSTPASS_STATS this_frame;
1953 FIRSTPASS_STATS this_frame_copy;
1954
1955 double this_frame_error;
1956 double this_frame_intra_error;
1957 double this_frame_coded_error;
1958
1959 FIRSTPASS_STATS *start_pos;
1960
1961 int overhead_bits;
1962
1963 if (!cpi->twopass.stats_in) {
1964 return;
1965 }
1966
1967 vp9_clear_system_state();
1968
1969 vpx_memset(&this_frame, 0, sizeof(FIRSTPASS_STATS));
1970
1971 if (EOF == input_stats(cpi, &this_frame))
1972 return;
1973
1974 this_frame_error = this_frame.ssim_weighted_pred_err;
1975 this_frame_intra_error = this_frame.intra_error;
1976 this_frame_coded_error = this_frame.coded_error;
1977
1978 start_pos = cpi->twopass.stats_in;
1979
1980 // keyframe and section processing !
1981 if (cpi->twopass.frames_to_key == 0) {
1982 // Define next KF group and assign bits to it
1983 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
1984 find_next_key_frame(cpi, &this_frame_copy);
1985 }
1986
1987 // Is this a GF / ARF (Note that a KF is always also a GF)
1988 if (cpi->frames_till_gf_update_due == 0) {
1989 // Define next gf group and assign bits to it
1990 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
1991 define_gf_group(cpi, &this_frame_copy);
1992
1993 // If we are going to code an altref frame at the end of the group and the c urrent frame is not a key frame....
1994 // If the previous group used an arf this frame has already benefited from t hat arf boost and it should not be given extra bits
1995 // If the previous group was NOT coded using arf we may want to apply some b oost to this GF as well
1996 if (cpi->source_alt_ref_pending && (cpi->common.frame_type != KEY_FRAME)) {
1997 // Assign a standard frames worth of bits from those allocated to the GF g roup
1998 int bak = cpi->per_frame_bandwidth;
1999 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2000 assign_std_frame_bits(cpi, &this_frame_copy);
2001 cpi->per_frame_bandwidth = bak;
2002 }
2003 }
2004
2005 // Otherwise this is an ordinary frame
2006 else {
2007 // Assign bits from those allocated to the GF group
2008 vpx_memcpy(&this_frame_copy, &this_frame, sizeof(this_frame));
2009 assign_std_frame_bits(cpi, &this_frame_copy);
2010 }
2011
2012 // Keep a globally available copy of this and the next frame's iiratio.
2013 cpi->twopass.this_iiratio = (int)(this_frame_intra_error /
2014 DOUBLE_DIVIDE_CHECK(this_frame_coded_error));
2015 {
2016 FIRSTPASS_STATS next_frame;
2017 if (lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2018 cpi->twopass.next_iiratio = (int)(next_frame.intra_error /
2019 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2020 }
2021 }
2022
2023 // Set nominal per second bandwidth for this frame
2024 cpi->target_bandwidth = (int)(cpi->per_frame_bandwidth
2025 * cpi->output_frame_rate);
2026 if (cpi->target_bandwidth < 0)
2027 cpi->target_bandwidth = 0;
2028
2029
2030 // Account for mv, mode and other overheads.
2031 overhead_bits = (int)estimate_modemvcost(
2032 cpi, cpi->twopass.total_left_stats);
2033
2034 // Special case code for first frame.
2035 if (cpi->common.current_video_frame == 0) {
2036 cpi->twopass.est_max_qcorrection_factor = 1.0;
2037
2038 // Set a cq_level in constrained quality mode.
2039 if (cpi->oxcf.end_usage == USAGE_CONSTRAINED_QUALITY) {
2040 int est_cq;
2041
2042 est_cq =
2043 estimate_cq(cpi,
2044 cpi->twopass.total_left_stats,
2045 (int)(cpi->twopass.bits_left / frames_left),
2046 overhead_bits);
2047
2048 cpi->cq_target_quality = cpi->oxcf.cq_level;
2049 if (est_cq > cpi->cq_target_quality)
2050 cpi->cq_target_quality = est_cq;
2051 }
2052
2053 // guess at maxq needed in 2nd pass
2054 cpi->twopass.maxq_max_limit = cpi->worst_quality;
2055 cpi->twopass.maxq_min_limit = cpi->best_quality;
2056
2057 tmp_q = estimate_max_q(
2058 cpi,
2059 cpi->twopass.total_left_stats,
2060 (int)(cpi->twopass.bits_left / frames_left),
2061 overhead_bits);
2062
2063 cpi->active_worst_quality = tmp_q;
2064 cpi->ni_av_qi = tmp_q;
2065 cpi->avg_q = vp9_convert_qindex_to_q(tmp_q);
2066
2067 // Limit the maxq value returned subsequently.
2068 // This increases the risk of overspend or underspend if the initial
2069 // estimate for the clip is bad, but helps prevent excessive
2070 // variation in Q, especially near the end of a clip
2071 // where for example a small overspend may cause Q to crash
2072 adjust_maxq_qrange(cpi);
2073 }
2074
2075 // The last few frames of a clip almost always have to few or too many
2076 // bits and for the sake of over exact rate control we dont want to make
2077 // radical adjustments to the allowed quantizer range just to use up a
2078 // few surplus bits or get beneath the target rate.
2079 else if ((cpi->common.current_video_frame <
2080 (((unsigned int)cpi->twopass.total_stats->count * 255) >> 8)) &&
2081 ((cpi->common.current_video_frame + cpi->baseline_gf_interval) <
2082 (unsigned int)cpi->twopass.total_stats->count)) {
2083 if (frames_left < 1)
2084 frames_left = 1;
2085
2086 tmp_q = estimate_max_q(
2087 cpi,
2088 cpi->twopass.total_left_stats,
2089 (int)(cpi->twopass.bits_left / frames_left),
2090 overhead_bits);
2091
2092 // Make a damped adjustment to active max Q
2093 cpi->active_worst_quality =
2094 adjust_active_maxq(cpi->active_worst_quality, tmp_q);
2095 }
2096
2097 cpi->twopass.frames_to_key--;
2098
2099 // Update the total stats remaining sturcture
2100 subtract_stats(cpi->twopass.total_left_stats, &this_frame);
2101 }
2102
2103
2104 static BOOL test_candidate_kf(VP9_COMP *cpi, FIRSTPASS_STATS *last_frame, FIRST PASS_STATS *this_frame, FIRSTPASS_STATS *next_frame) {
2105 BOOL is_viable_kf = FALSE;
2106
2107 // Does the frame satisfy the primary criteria of a key frame
2108 // If so, then examine how well it predicts subsequent frames
2109 if ((this_frame->pcnt_second_ref < 0.10) &&
2110 (next_frame->pcnt_second_ref < 0.10) &&
2111 ((this_frame->pcnt_inter < 0.05) ||
2112 (
2113 ((this_frame->pcnt_inter - this_frame->pcnt_neutral) < .35) &&
2114 ((this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error )) < 2.5) &&
2115 ((fabs(last_frame->coded_error - this_frame->coded_error) / DOUBLE_DIVI DE_CHECK(this_frame->coded_error) > .40) ||
2116 (fabs(last_frame->intra_error - this_frame->intra_error) / DOUBLE_DIVI DE_CHECK(this_frame->intra_error) > .40) ||
2117 ((next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_erro r)) > 3.5)
2118 )
2119 )
2120 )
2121 ) {
2122 int i;
2123 FIRSTPASS_STATS *start_pos;
2124
2125 FIRSTPASS_STATS local_next_frame;
2126
2127 double boost_score = 0.0;
2128 double old_boost_score = 0.0;
2129 double decay_accumulator = 1.0;
2130 double next_iiratio;
2131
2132 vpx_memcpy(&local_next_frame, next_frame, sizeof(*next_frame));
2133
2134 // Note the starting file position so we can reset to it
2135 start_pos = cpi->twopass.stats_in;
2136
2137 // Examine how well the key frame predicts subsequent frames
2138 for (i = 0; i < 16; i++) {
2139 next_iiratio = (IIKFACTOR1 * local_next_frame.intra_error / DOUBLE_DIVIDE_ CHECK(local_next_frame.coded_error));
2140
2141 if (next_iiratio > RMAX)
2142 next_iiratio = RMAX;
2143
2144 // Cumulative effect of decay in prediction quality
2145 if (local_next_frame.pcnt_inter > 0.85)
2146 decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2147 else
2148 decay_accumulator = decay_accumulator * ((0.85 + local_next_frame.pcnt_i nter) / 2.0);
2149
2150 // decay_accumulator = decay_accumulator * local_next_frame.pcnt_inter;
2151
2152 // Keep a running total
2153 boost_score += (decay_accumulator * next_iiratio);
2154
2155 // Test various breakout clauses
2156 if ((local_next_frame.pcnt_inter < 0.05) ||
2157 (next_iiratio < 1.5) ||
2158 (((local_next_frame.pcnt_inter -
2159 local_next_frame.pcnt_neutral) < 0.20) &&
2160 (next_iiratio < 3.0)) ||
2161 ((boost_score - old_boost_score) < 3.0) ||
2162 (local_next_frame.intra_error < 200)
2163 ) {
2164 break;
2165 }
2166
2167 old_boost_score = boost_score;
2168
2169 // Get the next frame details
2170 if (EOF == input_stats(cpi, &local_next_frame))
2171 break;
2172 }
2173
2174 // If there is tolerable prediction for at least the next 3 frames then brea k out else discard this pottential key frame and move on
2175 if (boost_score > 30.0 && (i > 3))
2176 is_viable_kf = TRUE;
2177 else {
2178 // Reset the file position
2179 reset_fpf_position(cpi, start_pos);
2180
2181 is_viable_kf = FALSE;
2182 }
2183 }
2184
2185 return is_viable_kf;
2186 }
2187 static void find_next_key_frame(VP9_COMP *cpi, FIRSTPASS_STATS *this_frame) {
2188 int i, j;
2189 FIRSTPASS_STATS last_frame;
2190 FIRSTPASS_STATS first_frame;
2191 FIRSTPASS_STATS next_frame;
2192 FIRSTPASS_STATS *start_position;
2193
2194 double decay_accumulator = 1.0;
2195 double zero_motion_accumulator = 1.0;
2196 double boost_score = 0;
2197 double old_boost_score = 0.0;
2198 double loop_decay_rate;
2199
2200 double kf_mod_err = 0.0;
2201 double kf_group_err = 0.0;
2202 double kf_group_intra_err = 0.0;
2203 double kf_group_coded_err = 0.0;
2204 double recent_loop_decay[8] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
2205
2206 vpx_memset(&next_frame, 0, sizeof(next_frame)); // assure clean
2207
2208 vp9_clear_system_state(); // __asm emms;
2209 start_position = cpi->twopass.stats_in;
2210
2211 cpi->common.frame_type = KEY_FRAME;
2212
2213 // is this a forced key frame by interval
2214 cpi->this_key_frame_forced = cpi->next_key_frame_forced;
2215
2216 // Clear the alt ref active flag as this can never be active on a key frame
2217 cpi->source_alt_ref_active = FALSE;
2218
2219 // Kf is always a gf so clear frames till next gf counter
2220 cpi->frames_till_gf_update_due = 0;
2221
2222 cpi->twopass.frames_to_key = 1;
2223
2224 // Take a copy of the initial frame details
2225 vpx_memcpy(&first_frame, this_frame, sizeof(*this_frame));
2226
2227 cpi->twopass.kf_group_bits = 0; // Total bits avaialable to kf group
2228 cpi->twopass.kf_group_error_left = 0; // Group modified error score.
2229
2230 kf_mod_err = calculate_modified_err(cpi, this_frame);
2231
2232 // find the next keyframe
2233 i = 0;
2234 while (cpi->twopass.stats_in < cpi->twopass.stats_in_end) {
2235 // Accumulate kf group error
2236 kf_group_err += calculate_modified_err(cpi, this_frame);
2237
2238 // These figures keep intra and coded error counts for all frames including key frames in the group.
2239 // The effect of the key frame itself can be subtracted out using the first_ frame data collected above
2240 kf_group_intra_err += this_frame->intra_error;
2241 kf_group_coded_err += this_frame->coded_error;
2242
2243 // load a the next frame's stats
2244 vpx_memcpy(&last_frame, this_frame, sizeof(*this_frame));
2245 input_stats(cpi, this_frame);
2246
2247 // Provided that we are not at the end of the file...
2248 if (cpi->oxcf.auto_key
2249 && lookup_next_frame_stats(cpi, &next_frame) != EOF) {
2250 // Normal scene cut check
2251 if (test_candidate_kf(cpi, &last_frame, this_frame, &next_frame)) {
2252 break;
2253 }
2254
2255 // How fast is prediction quality decaying
2256 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2257
2258 // We want to know something about the recent past... rather than
2259 // as used elsewhere where we are concened with decay in prediction
2260 // quality since the last GF or KF.
2261 recent_loop_decay[i % 8] = loop_decay_rate;
2262 decay_accumulator = 1.0;
2263 for (j = 0; j < 8; j++) {
2264 decay_accumulator = decay_accumulator * recent_loop_decay[j];
2265 }
2266
2267 // Special check for transition or high motion followed by a
2268 // to a static scene.
2269 if (detect_transition_to_still(cpi, i,
2270 (cpi->key_frame_frequency - i),
2271 loop_decay_rate,
2272 decay_accumulator)) {
2273 break;
2274 }
2275
2276
2277 // Step on to the next frame
2278 cpi->twopass.frames_to_key++;
2279
2280 // If we don't have a real key frame within the next two
2281 // forcekeyframeevery intervals then break out of the loop.
2282 if (cpi->twopass.frames_to_key >= 2 * (int)cpi->key_frame_frequency)
2283 break;
2284 } else
2285 cpi->twopass.frames_to_key++;
2286
2287 i++;
2288 }
2289
2290 // If there is a max kf interval set by the user we must obey it.
2291 // We already breakout of the loop above at 2x max.
2292 // This code centers the extra kf if the actual natural
2293 // interval is between 1x and 2x
2294 if (cpi->oxcf.auto_key
2295 && cpi->twopass.frames_to_key > (int)cpi->key_frame_frequency) {
2296 FIRSTPASS_STATS *current_pos = cpi->twopass.stats_in;
2297 FIRSTPASS_STATS tmp_frame;
2298
2299 cpi->twopass.frames_to_key /= 2;
2300
2301 // Copy first frame details
2302 vpx_memcpy(&tmp_frame, &first_frame, sizeof(first_frame));
2303
2304 // Reset to the start of the group
2305 reset_fpf_position(cpi, start_position);
2306
2307 kf_group_err = 0;
2308 kf_group_intra_err = 0;
2309 kf_group_coded_err = 0;
2310
2311 // Rescan to get the correct error data for the forced kf group
2312 for (i = 0; i < cpi->twopass.frames_to_key; i++) {
2313 // Accumulate kf group errors
2314 kf_group_err += calculate_modified_err(cpi, &tmp_frame);
2315 kf_group_intra_err += tmp_frame.intra_error;
2316 kf_group_coded_err += tmp_frame.coded_error;
2317
2318 // Load a the next frame's stats
2319 input_stats(cpi, &tmp_frame);
2320 }
2321
2322 // Reset to the start of the group
2323 reset_fpf_position(cpi, current_pos);
2324
2325 cpi->next_key_frame_forced = TRUE;
2326 } else
2327 cpi->next_key_frame_forced = FALSE;
2328
2329 // Special case for the last frame of the file
2330 if (cpi->twopass.stats_in >= cpi->twopass.stats_in_end) {
2331 // Accumulate kf group error
2332 kf_group_err += calculate_modified_err(cpi, this_frame);
2333
2334 // These figures keep intra and coded error counts for all frames including key frames in the group.
2335 // The effect of the key frame itself can be subtracted out using the first_ frame data collected above
2336 kf_group_intra_err += this_frame->intra_error;
2337 kf_group_coded_err += this_frame->coded_error;
2338 }
2339
2340 // Calculate the number of bits that should be assigned to the kf group.
2341 if ((cpi->twopass.bits_left > 0) && (cpi->twopass.modified_error_left > 0.0)) {
2342 // Max for a single normal frame (not key frame)
2343 int max_bits = frame_max_bits(cpi);
2344
2345 // Maximum bits for the kf group
2346 int64_t max_grp_bits;
2347
2348 // Default allocation based on bits left and relative
2349 // complexity of the section
2350 cpi->twopass.kf_group_bits = (int64_t)(cpi->twopass.bits_left *
2351 (kf_group_err /
2352 cpi->twopass.modified_error_left));
2353
2354 // Clip based on maximum per frame rate defined by the user.
2355 max_grp_bits = (int64_t)max_bits * (int64_t)cpi->twopass.frames_to_key;
2356 if (cpi->twopass.kf_group_bits > max_grp_bits)
2357 cpi->twopass.kf_group_bits = max_grp_bits;
2358 } else
2359 cpi->twopass.kf_group_bits = 0;
2360
2361 // Reset the first pass file position
2362 reset_fpf_position(cpi, start_position);
2363
2364 // determine how big to make this keyframe based on how well the subsequent fr ames use inter blocks
2365 decay_accumulator = 1.0;
2366 boost_score = 0.0;
2367 loop_decay_rate = 1.00; // Starting decay rate
2368
2369 for (i = 0; i < cpi->twopass.frames_to_key; i++) {
2370 double r;
2371
2372 if (EOF == input_stats(cpi, &next_frame))
2373 break;
2374
2375 if (next_frame.intra_error > cpi->twopass.kf_intra_err_min)
2376 r = (IIKFACTOR2 * next_frame.intra_error /
2377 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2378 else
2379 r = (IIKFACTOR2 * cpi->twopass.kf_intra_err_min /
2380 DOUBLE_DIVIDE_CHECK(next_frame.coded_error));
2381
2382 if (r > RMAX)
2383 r = RMAX;
2384
2385 // Monitor for static sections.
2386 if ((next_frame.pcnt_inter - next_frame.pcnt_motion) <
2387 zero_motion_accumulator) {
2388 zero_motion_accumulator =
2389 (next_frame.pcnt_inter - next_frame.pcnt_motion);
2390 }
2391
2392 // How fast is prediction quality decaying
2393 if (!detect_flash(cpi, 0)) {
2394 loop_decay_rate = get_prediction_decay_rate(cpi, &next_frame);
2395 decay_accumulator = decay_accumulator * loop_decay_rate;
2396 decay_accumulator = decay_accumulator < 0.1 ? 0.1 : decay_accumulator;
2397 }
2398
2399 boost_score += (decay_accumulator * r);
2400
2401 if ((i > MIN_GF_INTERVAL) &&
2402 ((boost_score - old_boost_score) < 6.25)) {
2403 break;
2404 }
2405
2406 old_boost_score = boost_score;
2407 }
2408
2409 {
2410 FIRSTPASS_STATS sectionstats;
2411
2412 zero_stats(&sectionstats);
2413 reset_fpf_position(cpi, start_position);
2414
2415 for (i = 0; i < cpi->twopass.frames_to_key; i++) {
2416 input_stats(cpi, &next_frame);
2417 accumulate_stats(&sectionstats, &next_frame);
2418 }
2419
2420 avg_stats(&sectionstats);
2421
2422 cpi->twopass.section_intra_rating = (int)
2423 (sectionstats.intra_error
2424 / DOUBLE_DIVIDE_CHECK(sectionstats.coded_error));
2425 }
2426
2427 // Reset the first pass file position
2428 reset_fpf_position(cpi, start_position);
2429
2430 // Work out how many bits to allocate for the key frame itself
2431 if (1) {
2432 int kf_boost = (int)boost_score;
2433 int allocation_chunks;
2434 int alt_kf_bits;
2435
2436 if (kf_boost < 300) {
2437 kf_boost += (cpi->twopass.frames_to_key * 3);
2438 if (kf_boost > 300)
2439 kf_boost = 300;
2440 }
2441
2442 if (kf_boost < 250) // Min KF boost
2443 kf_boost = 250;
2444
2445 // Make a note of baseline boost and the zero motion
2446 // accumulator value for use elsewhere.
2447 cpi->kf_boost = kf_boost;
2448 cpi->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
2449
2450 // We do three calculations for kf size.
2451 // The first is based on the error score for the whole kf group.
2452 // The second (optionaly) on the key frames own error if this is
2453 // smaller than the average for the group.
2454 // The final one insures that the frame receives at least the
2455 // allocation it would have received based on its own error score vs
2456 // the error score remaining
2457 // Special case if the sequence appears almost totaly static
2458 // In this case we want to spend almost all of the bits on the
2459 // key frame.
2460 // cpi->twopass.frames_to_key-1 because key frame itself is taken
2461 // care of by kf_boost.
2462 if (zero_motion_accumulator >= 0.99) {
2463 allocation_chunks =
2464 ((cpi->twopass.frames_to_key - 1) * 10) + kf_boost;
2465 } else {
2466 allocation_chunks =
2467 ((cpi->twopass.frames_to_key - 1) * 100) + kf_boost;
2468 }
2469
2470 // Prevent overflow
2471 if (kf_boost > 1028) {
2472 int divisor = kf_boost >> 10;
2473 kf_boost /= divisor;
2474 allocation_chunks /= divisor;
2475 }
2476
2477 cpi->twopass.kf_group_bits = (cpi->twopass.kf_group_bits < 0) ? 0 : cpi->two pass.kf_group_bits;
2478
2479 // Calculate the number of bits to be spent on the key frame
2480 cpi->twopass.kf_bits = (int)((double)kf_boost * ((double)cpi->twopass.kf_gr oup_bits / (double)allocation_chunks));
2481
2482 // If the key frame is actually easier than the average for the
2483 // kf group (which does sometimes happen... eg a blank intro frame)
2484 // Then use an alternate calculation based on the kf error score
2485 // which should give a smaller key frame.
2486 if (kf_mod_err < kf_group_err / cpi->twopass.frames_to_key) {
2487 double alt_kf_grp_bits =
2488 ((double)cpi->twopass.bits_left *
2489 (kf_mod_err * (double)cpi->twopass.frames_to_key) /
2490 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left));
2491
2492 alt_kf_bits = (int)((double)kf_boost *
2493 (alt_kf_grp_bits / (double)allocation_chunks));
2494
2495 if (cpi->twopass.kf_bits > alt_kf_bits) {
2496 cpi->twopass.kf_bits = alt_kf_bits;
2497 }
2498 }
2499 // Else if it is much harder than other frames in the group make sure
2500 // it at least receives an allocation in keeping with its relative
2501 // error score
2502 else {
2503 alt_kf_bits =
2504 (int)((double)cpi->twopass.bits_left *
2505 (kf_mod_err /
2506 DOUBLE_DIVIDE_CHECK(cpi->twopass.modified_error_left)));
2507
2508 if (alt_kf_bits > cpi->twopass.kf_bits) {
2509 cpi->twopass.kf_bits = alt_kf_bits;
2510 }
2511 }
2512
2513 cpi->twopass.kf_group_bits -= cpi->twopass.kf_bits;
2514 // Add in the minimum frame allowance
2515 cpi->twopass.kf_bits += cpi->min_frame_bandwidth;
2516
2517 // Peer frame bit target for this frame
2518 cpi->per_frame_bandwidth = cpi->twopass.kf_bits;
2519 // Convert to a per second bitrate
2520 cpi->target_bandwidth = (int)(cpi->twopass.kf_bits *
2521 cpi->output_frame_rate);
2522 }
2523
2524 // Note the total error score of the kf group minus the key frame itself
2525 cpi->twopass.kf_group_error_left = (int)(kf_group_err - kf_mod_err);
2526
2527 // Adjust the count of total modified error left.
2528 // The count of bits left is adjusted elsewhere based on real coded frame size s
2529 cpi->twopass.modified_error_left -= kf_group_err;
2530 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698