• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <limits.h>
12 #include <math.h>
13 #include <stdio.h>
14 
15 #include "./vpx_dsp_rtcd.h"
16 #include "./vpx_scale_rtcd.h"
17 
18 #include "vpx_dsp/vpx_dsp_common.h"
19 #include "vpx_mem/vpx_mem.h"
20 #include "vpx_ports/mem.h"
21 #include "vpx_ports/system_state.h"
22 #include "vpx_scale/vpx_scale.h"
23 #include "vpx_scale/yv12config.h"
24 
25 #include "vp9/common/vp9_entropymv.h"
26 #include "vp9/common/vp9_quant_common.h"
27 #include "vp9/common/vp9_reconinter.h"  // vp9_setup_dst_planes()
28 #include "vp9/encoder/vp9_aq_variance.h"
29 #include "vp9/encoder/vp9_block.h"
30 #include "vp9/encoder/vp9_encodeframe.h"
31 #include "vp9/encoder/vp9_encodemb.h"
32 #include "vp9/encoder/vp9_encodemv.h"
33 #include "vp9/encoder/vp9_encoder.h"
34 #include "vp9/encoder/vp9_ethread.h"
35 #include "vp9/encoder/vp9_extend.h"
36 #include "vp9/encoder/vp9_firstpass.h"
37 #include "vp9/encoder/vp9_mcomp.h"
38 #include "vp9/encoder/vp9_quantize.h"
39 #include "vp9/encoder/vp9_rd.h"
40 #include "vpx_dsp/variance.h"
41 
42 #define OUTPUT_FPF 0
43 #define ARF_STATS_OUTPUT 0
44 #define COMPLEXITY_STATS_OUTPUT 0
45 
46 #define FIRST_PASS_Q 10.0
47 #define NORMAL_BOOST 100
48 #define MIN_ARF_GF_BOOST 250
49 #define MIN_DECAY_FACTOR 0.01
50 #define NEW_MV_MODE_PENALTY 32
51 #define DARK_THRESH 64
52 #define LOW_I_THRESH 24000
53 
54 #define NCOUNT_INTRA_THRESH 8192
55 #define NCOUNT_INTRA_FACTOR 3
56 
57 #define DOUBLE_DIVIDE_CHECK(x) ((x) < 0 ? (x)-0.000001 : (x) + 0.000001)
58 
59 #if ARF_STATS_OUTPUT
60 unsigned int arf_count = 0;
61 #endif
62 
63 // Resets the first pass file to the given position using a relative seek from
64 // the current position.
reset_fpf_position(TWO_PASS * p,const FIRSTPASS_STATS * position)65 static void reset_fpf_position(TWO_PASS *p, const FIRSTPASS_STATS *position) {
66   p->stats_in = position;
67 }
68 
69 // Read frame stats at an offset from the current position.
read_frame_stats(const TWO_PASS * p,int offset)70 static const FIRSTPASS_STATS *read_frame_stats(const TWO_PASS *p, int offset) {
71   if ((offset >= 0 && p->stats_in + offset >= p->stats_in_end) ||
72       (offset < 0 && p->stats_in + offset < p->stats_in_start)) {
73     return NULL;
74   }
75 
76   return &p->stats_in[offset];
77 }
78 
input_stats(TWO_PASS * p,FIRSTPASS_STATS * fps)79 static int input_stats(TWO_PASS *p, FIRSTPASS_STATS *fps) {
80   if (p->stats_in >= p->stats_in_end) return EOF;
81 
82   *fps = *p->stats_in;
83   ++p->stats_in;
84   return 1;
85 }
86 
output_stats(FIRSTPASS_STATS * stats)87 static void output_stats(FIRSTPASS_STATS *stats) {
88   (void)stats;
89 // TEMP debug code
90 #if OUTPUT_FPF
91   {
92     FILE *fpfile;
93     fpfile = fopen("firstpass.stt", "a");
94 
95     fprintf(fpfile,
96             "%12.0lf %12.4lf %12.2lf %12.2lf %12.2lf %12.0lf %12.4lf %12.4lf"
97             "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.4lf"
98             "%12.4lf %12.4lf %12.4lf %12.4lf %12.4lf %12.0lf %12.4lf %12.0lf"
99             "%12.4lf"
100             "\n",
101             stats->frame, stats->weight, stats->intra_error, stats->coded_error,
102             stats->sr_coded_error, stats->frame_noise_energy, stats->pcnt_inter,
103             stats->pcnt_motion, stats->pcnt_second_ref, stats->pcnt_neutral,
104             stats->pcnt_intra_low, stats->pcnt_intra_high,
105             stats->intra_skip_pct, stats->intra_smooth_pct,
106             stats->inactive_zone_rows, stats->inactive_zone_cols, stats->MVr,
107             stats->mvr_abs, stats->MVc, stats->mvc_abs, stats->MVrv,
108             stats->MVcv, stats->mv_in_out_count, stats->count, stats->duration);
109     fclose(fpfile);
110   }
111 #endif
112 }
113 
114 #if CONFIG_FP_MB_STATS
output_fpmb_stats(uint8_t * this_frame_mb_stats,VP9_COMMON * cm,struct vpx_codec_pkt_list * pktlist)115 static void output_fpmb_stats(uint8_t *this_frame_mb_stats, VP9_COMMON *cm,
116                               struct vpx_codec_pkt_list *pktlist) {
117   struct vpx_codec_cx_pkt pkt;
118   pkt.kind = VPX_CODEC_FPMB_STATS_PKT;
119   pkt.data.firstpass_mb_stats.buf = this_frame_mb_stats;
120   pkt.data.firstpass_mb_stats.sz = cm->initial_mbs * sizeof(uint8_t);
121   vpx_codec_pkt_list_add(pktlist, &pkt);
122 }
123 #endif
124 
zero_stats(FIRSTPASS_STATS * section)125 static void zero_stats(FIRSTPASS_STATS *section) {
126   section->frame = 0.0;
127   section->weight = 0.0;
128   section->intra_error = 0.0;
129   section->coded_error = 0.0;
130   section->sr_coded_error = 0.0;
131   section->frame_noise_energy = 0.0;
132   section->pcnt_inter = 0.0;
133   section->pcnt_motion = 0.0;
134   section->pcnt_second_ref = 0.0;
135   section->pcnt_neutral = 0.0;
136   section->intra_skip_pct = 0.0;
137   section->intra_smooth_pct = 0.0;
138   section->pcnt_intra_low = 0.0;
139   section->pcnt_intra_high = 0.0;
140   section->inactive_zone_rows = 0.0;
141   section->inactive_zone_cols = 0.0;
142   section->MVr = 0.0;
143   section->mvr_abs = 0.0;
144   section->MVc = 0.0;
145   section->mvc_abs = 0.0;
146   section->MVrv = 0.0;
147   section->MVcv = 0.0;
148   section->mv_in_out_count = 0.0;
149   section->count = 0.0;
150   section->duration = 1.0;
151   section->spatial_layer_id = 0;
152 }
153 
accumulate_stats(FIRSTPASS_STATS * section,const FIRSTPASS_STATS * frame)154 static void accumulate_stats(FIRSTPASS_STATS *section,
155                              const FIRSTPASS_STATS *frame) {
156   section->frame += frame->frame;
157   section->weight += frame->weight;
158   section->spatial_layer_id = frame->spatial_layer_id;
159   section->intra_error += frame->intra_error;
160   section->coded_error += frame->coded_error;
161   section->sr_coded_error += frame->sr_coded_error;
162   section->frame_noise_energy += frame->frame_noise_energy;
163   section->pcnt_inter += frame->pcnt_inter;
164   section->pcnt_motion += frame->pcnt_motion;
165   section->pcnt_second_ref += frame->pcnt_second_ref;
166   section->pcnt_neutral += frame->pcnt_neutral;
167   section->intra_skip_pct += frame->intra_skip_pct;
168   section->intra_smooth_pct += frame->intra_smooth_pct;
169   section->pcnt_intra_low += frame->pcnt_intra_low;
170   section->pcnt_intra_high += frame->pcnt_intra_high;
171   section->inactive_zone_rows += frame->inactive_zone_rows;
172   section->inactive_zone_cols += frame->inactive_zone_cols;
173   section->MVr += frame->MVr;
174   section->mvr_abs += frame->mvr_abs;
175   section->MVc += frame->MVc;
176   section->mvc_abs += frame->mvc_abs;
177   section->MVrv += frame->MVrv;
178   section->MVcv += frame->MVcv;
179   section->mv_in_out_count += frame->mv_in_out_count;
180   section->count += frame->count;
181   section->duration += frame->duration;
182 }
183 
subtract_stats(FIRSTPASS_STATS * section,const FIRSTPASS_STATS * frame)184 static void subtract_stats(FIRSTPASS_STATS *section,
185                            const FIRSTPASS_STATS *frame) {
186   section->frame -= frame->frame;
187   section->weight -= frame->weight;
188   section->intra_error -= frame->intra_error;
189   section->coded_error -= frame->coded_error;
190   section->sr_coded_error -= frame->sr_coded_error;
191   section->frame_noise_energy -= frame->frame_noise_energy;
192   section->pcnt_inter -= frame->pcnt_inter;
193   section->pcnt_motion -= frame->pcnt_motion;
194   section->pcnt_second_ref -= frame->pcnt_second_ref;
195   section->pcnt_neutral -= frame->pcnt_neutral;
196   section->intra_skip_pct -= frame->intra_skip_pct;
197   section->intra_smooth_pct -= frame->intra_smooth_pct;
198   section->pcnt_intra_low -= frame->pcnt_intra_low;
199   section->pcnt_intra_high -= frame->pcnt_intra_high;
200   section->inactive_zone_rows -= frame->inactive_zone_rows;
201   section->inactive_zone_cols -= frame->inactive_zone_cols;
202   section->MVr -= frame->MVr;
203   section->mvr_abs -= frame->mvr_abs;
204   section->MVc -= frame->MVc;
205   section->mvc_abs -= frame->mvc_abs;
206   section->MVrv -= frame->MVrv;
207   section->MVcv -= frame->MVcv;
208   section->mv_in_out_count -= frame->mv_in_out_count;
209   section->count -= frame->count;
210   section->duration -= frame->duration;
211 }
212 
213 // Calculate an active area of the image that discounts formatting
214 // bars and partially discounts other 0 energy areas.
215 #define MIN_ACTIVE_AREA 0.5
216 #define MAX_ACTIVE_AREA 1.0
calculate_active_area(const FRAME_INFO * frame_info,const FIRSTPASS_STATS * this_frame)217 static double calculate_active_area(const FRAME_INFO *frame_info,
218                                     const FIRSTPASS_STATS *this_frame) {
219   double active_pct;
220 
221   active_pct =
222       1.0 -
223       ((this_frame->intra_skip_pct / 2) +
224        ((this_frame->inactive_zone_rows * 2) / (double)frame_info->mb_rows));
225   return fclamp(active_pct, MIN_ACTIVE_AREA, MAX_ACTIVE_AREA);
226 }
227 
228 // Get the average weighted error for the clip (or corpus)
get_distribution_av_err(VP9_COMP * cpi,TWO_PASS * const twopass)229 static double get_distribution_av_err(VP9_COMP *cpi, TWO_PASS *const twopass) {
230   const double av_weight =
231       twopass->total_stats.weight / twopass->total_stats.count;
232 
233   if (cpi->oxcf.vbr_corpus_complexity)
234     return av_weight * twopass->mean_mod_score;
235   else
236     return (twopass->total_stats.coded_error * av_weight) /
237            twopass->total_stats.count;
238 }
239 
240 #define ACT_AREA_CORRECTION 0.5
241 // Calculate a modified Error used in distributing bits between easier and
242 // harder frames.
calculate_mod_frame_score(const VP9_COMP * cpi,const VP9EncoderConfig * oxcf,const FIRSTPASS_STATS * this_frame,const double av_err)243 static double calculate_mod_frame_score(const VP9_COMP *cpi,
244                                         const VP9EncoderConfig *oxcf,
245                                         const FIRSTPASS_STATS *this_frame,
246                                         const double av_err) {
247   double modified_score =
248       av_err * pow(this_frame->coded_error * this_frame->weight /
249                        DOUBLE_DIVIDE_CHECK(av_err),
250                    oxcf->two_pass_vbrbias / 100.0);
251 
252   // Correction for active area. Frames with a reduced active area
253   // (eg due to formatting bars) have a higher error per mb for the
254   // remaining active MBs. The correction here assumes that coding
255   // 0.5N blocks of complexity 2X is a little easier than coding N
256   // blocks of complexity X.
257   modified_score *= pow(calculate_active_area(&cpi->frame_info, this_frame),
258                         ACT_AREA_CORRECTION);
259 
260   return modified_score;
261 }
262 
calc_norm_frame_score(const VP9EncoderConfig * oxcf,const FRAME_INFO * frame_info,const FIRSTPASS_STATS * this_frame,double mean_mod_score,double av_err)263 static double calc_norm_frame_score(const VP9EncoderConfig *oxcf,
264                                     const FRAME_INFO *frame_info,
265                                     const FIRSTPASS_STATS *this_frame,
266                                     double mean_mod_score, double av_err) {
267   double modified_score =
268       av_err * pow(this_frame->coded_error * this_frame->weight /
269                        DOUBLE_DIVIDE_CHECK(av_err),
270                    oxcf->two_pass_vbrbias / 100.0);
271 
272   const double min_score = (double)(oxcf->two_pass_vbrmin_section) / 100.0;
273   const double max_score = (double)(oxcf->two_pass_vbrmax_section) / 100.0;
274 
275   // Correction for active area. Frames with a reduced active area
276   // (eg due to formatting bars) have a higher error per mb for the
277   // remaining active MBs. The correction here assumes that coding
278   // 0.5N blocks of complexity 2X is a little easier than coding N
279   // blocks of complexity X.
280   modified_score *=
281       pow(calculate_active_area(frame_info, this_frame), ACT_AREA_CORRECTION);
282 
283   // Normalize to a midpoint score.
284   modified_score /= DOUBLE_DIVIDE_CHECK(mean_mod_score);
285   return fclamp(modified_score, min_score, max_score);
286 }
287 
calculate_norm_frame_score(const VP9_COMP * cpi,const TWO_PASS * twopass,const VP9EncoderConfig * oxcf,const FIRSTPASS_STATS * this_frame,const double av_err)288 static double calculate_norm_frame_score(const VP9_COMP *cpi,
289                                          const TWO_PASS *twopass,
290                                          const VP9EncoderConfig *oxcf,
291                                          const FIRSTPASS_STATS *this_frame,
292                                          const double av_err) {
293   return calc_norm_frame_score(oxcf, &cpi->frame_info, this_frame,
294                                twopass->mean_mod_score, av_err);
295 }
296 
297 // This function returns the maximum target rate per frame.
frame_max_bits(const RATE_CONTROL * rc,const VP9EncoderConfig * oxcf)298 static int frame_max_bits(const RATE_CONTROL *rc,
299                           const VP9EncoderConfig *oxcf) {
300   int64_t max_bits = ((int64_t)rc->avg_frame_bandwidth *
301                       (int64_t)oxcf->two_pass_vbrmax_section) /
302                      100;
303   if (max_bits < 0)
304     max_bits = 0;
305   else if (max_bits > rc->max_frame_bandwidth)
306     max_bits = rc->max_frame_bandwidth;
307 
308   return (int)max_bits;
309 }
310 
vp9_init_first_pass(VP9_COMP * cpi)311 void vp9_init_first_pass(VP9_COMP *cpi) {
312   zero_stats(&cpi->twopass.total_stats);
313 }
314 
vp9_end_first_pass(VP9_COMP * cpi)315 void vp9_end_first_pass(VP9_COMP *cpi) {
316   output_stats(&cpi->twopass.total_stats);
317   cpi->twopass.first_pass_done = 1;
318   vpx_free(cpi->twopass.fp_mb_float_stats);
319   cpi->twopass.fp_mb_float_stats = NULL;
320 }
321 
get_block_variance_fn(BLOCK_SIZE bsize)322 static vpx_variance_fn_t get_block_variance_fn(BLOCK_SIZE bsize) {
323   switch (bsize) {
324     case BLOCK_8X8: return vpx_mse8x8;
325     case BLOCK_16X8: return vpx_mse16x8;
326     case BLOCK_8X16: return vpx_mse8x16;
327     default: return vpx_mse16x16;
328   }
329 }
330 
get_prediction_error(BLOCK_SIZE bsize,const struct buf_2d * src,const struct buf_2d * ref)331 static unsigned int get_prediction_error(BLOCK_SIZE bsize,
332                                          const struct buf_2d *src,
333                                          const struct buf_2d *ref) {
334   unsigned int sse;
335   const vpx_variance_fn_t fn = get_block_variance_fn(bsize);
336   fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
337   return sse;
338 }
339 
340 #if CONFIG_VP9_HIGHBITDEPTH
highbd_get_block_variance_fn(BLOCK_SIZE bsize,int bd)341 static vpx_variance_fn_t highbd_get_block_variance_fn(BLOCK_SIZE bsize,
342                                                       int bd) {
343   switch (bd) {
344     default:
345       switch (bsize) {
346         case BLOCK_8X8: return vpx_highbd_8_mse8x8;
347         case BLOCK_16X8: return vpx_highbd_8_mse16x8;
348         case BLOCK_8X16: return vpx_highbd_8_mse8x16;
349         default: return vpx_highbd_8_mse16x16;
350       }
351       break;
352     case 10:
353       switch (bsize) {
354         case BLOCK_8X8: return vpx_highbd_10_mse8x8;
355         case BLOCK_16X8: return vpx_highbd_10_mse16x8;
356         case BLOCK_8X16: return vpx_highbd_10_mse8x16;
357         default: return vpx_highbd_10_mse16x16;
358       }
359       break;
360     case 12:
361       switch (bsize) {
362         case BLOCK_8X8: return vpx_highbd_12_mse8x8;
363         case BLOCK_16X8: return vpx_highbd_12_mse16x8;
364         case BLOCK_8X16: return vpx_highbd_12_mse8x16;
365         default: return vpx_highbd_12_mse16x16;
366       }
367       break;
368   }
369 }
370 
highbd_get_prediction_error(BLOCK_SIZE bsize,const struct buf_2d * src,const struct buf_2d * ref,int bd)371 static unsigned int highbd_get_prediction_error(BLOCK_SIZE bsize,
372                                                 const struct buf_2d *src,
373                                                 const struct buf_2d *ref,
374                                                 int bd) {
375   unsigned int sse;
376   const vpx_variance_fn_t fn = highbd_get_block_variance_fn(bsize, bd);
377   fn(src->buf, src->stride, ref->buf, ref->stride, &sse);
378   return sse;
379 }
380 #endif  // CONFIG_VP9_HIGHBITDEPTH
381 
382 // Refine the motion search range according to the frame dimension
383 // for first pass test.
get_search_range(const VP9_COMP * cpi)384 static int get_search_range(const VP9_COMP *cpi) {
385   int sr = 0;
386   const int dim = VPXMIN(cpi->initial_width, cpi->initial_height);
387 
388   while ((dim << sr) < MAX_FULL_PEL_VAL) ++sr;
389   return sr;
390 }
391 
first_pass_motion_search(VP9_COMP * cpi,MACROBLOCK * x,const MV * ref_mv,MV * best_mv,int * best_motion_err)392 static void first_pass_motion_search(VP9_COMP *cpi, MACROBLOCK *x,
393                                      const MV *ref_mv, MV *best_mv,
394                                      int *best_motion_err) {
395   MACROBLOCKD *const xd = &x->e_mbd;
396   MV tmp_mv = { 0, 0 };
397   MV ref_mv_full = { ref_mv->row >> 3, ref_mv->col >> 3 };
398   int num00, tmp_err, n;
399   const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
400   vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
401   const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
402 
403   int step_param = 3;
404   int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
405   const int sr = get_search_range(cpi);
406   step_param += sr;
407   further_steps -= sr;
408 
409   // Override the default variance function to use MSE.
410   v_fn_ptr.vf = get_block_variance_fn(bsize);
411 #if CONFIG_VP9_HIGHBITDEPTH
412   if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
413     v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd);
414   }
415 #endif  // CONFIG_VP9_HIGHBITDEPTH
416 
417   // Center the initial step/diamond search on best mv.
418   tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
419                                     step_param, x->sadperbit16, &num00,
420                                     &v_fn_ptr, ref_mv);
421   if (tmp_err < INT_MAX)
422     tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
423   if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
424 
425   if (tmp_err < *best_motion_err) {
426     *best_motion_err = tmp_err;
427     *best_mv = tmp_mv;
428   }
429 
430   // Carry out further step/diamond searches as necessary.
431   n = num00;
432   num00 = 0;
433 
434   while (n < further_steps) {
435     ++n;
436 
437     if (num00) {
438       --num00;
439     } else {
440       tmp_err = cpi->diamond_search_sad(x, &cpi->ss_cfg, &ref_mv_full, &tmp_mv,
441                                         step_param + n, x->sadperbit16, &num00,
442                                         &v_fn_ptr, ref_mv);
443       if (tmp_err < INT_MAX)
444         tmp_err = vp9_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
445       if (tmp_err < INT_MAX - new_mv_mode_penalty)
446         tmp_err += new_mv_mode_penalty;
447 
448       if (tmp_err < *best_motion_err) {
449         *best_motion_err = tmp_err;
450         *best_mv = tmp_mv;
451       }
452     }
453   }
454 }
455 
get_bsize(const VP9_COMMON * cm,int mb_row,int mb_col)456 static BLOCK_SIZE get_bsize(const VP9_COMMON *cm, int mb_row, int mb_col) {
457   if (2 * mb_col + 1 < cm->mi_cols) {
458     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_16X16 : BLOCK_16X8;
459   } else {
460     return 2 * mb_row + 1 < cm->mi_rows ? BLOCK_8X16 : BLOCK_8X8;
461   }
462 }
463 
find_fp_qindex(vpx_bit_depth_t bit_depth)464 static int find_fp_qindex(vpx_bit_depth_t bit_depth) {
465   int i;
466 
467   for (i = 0; i < QINDEX_RANGE; ++i)
468     if (vp9_convert_qindex_to_q(i, bit_depth) >= FIRST_PASS_Q) break;
469 
470   if (i == QINDEX_RANGE) i--;
471 
472   return i;
473 }
474 
set_first_pass_params(VP9_COMP * cpi)475 static void set_first_pass_params(VP9_COMP *cpi) {
476   VP9_COMMON *const cm = &cpi->common;
477   if (!cpi->refresh_alt_ref_frame &&
478       (cm->current_video_frame == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY))) {
479     cm->frame_type = KEY_FRAME;
480   } else {
481     cm->frame_type = INTER_FRAME;
482   }
483   // Do not use periodic key frames.
484   cpi->rc.frames_to_key = INT_MAX;
485 }
486 
487 // Scale an sse threshold to account for 8/10/12 bit.
scale_sse_threshold(VP9_COMMON * cm,int thresh)488 static int scale_sse_threshold(VP9_COMMON *cm, int thresh) {
489   int ret_val = thresh;
490 #if CONFIG_VP9_HIGHBITDEPTH
491   if (cm->use_highbitdepth) {
492     switch (cm->bit_depth) {
493       case VPX_BITS_8: ret_val = thresh; break;
494       case VPX_BITS_10: ret_val = thresh << 4; break;
495       default:
496         assert(cm->bit_depth == VPX_BITS_12);
497         ret_val = thresh << 8;
498         break;
499     }
500   }
501 #else
502   (void)cm;
503 #endif  // CONFIG_VP9_HIGHBITDEPTH
504   return ret_val;
505 }
506 
507 // This threshold is used to track blocks where to all intents and purposes
508 // the intra prediction error 0. Though the metric we test against
509 // is technically a sse we are mainly interested in blocks where all the pixels
510 // in the 8 bit domain have an error of <= 1 (where error = sse) so a
511 // linear scaling for 10 and 12 bit gives similar results.
512 #define UL_INTRA_THRESH 50
get_ul_intra_threshold(VP9_COMMON * cm)513 static int get_ul_intra_threshold(VP9_COMMON *cm) {
514   int ret_val = UL_INTRA_THRESH;
515 #if CONFIG_VP9_HIGHBITDEPTH
516   if (cm->use_highbitdepth) {
517     switch (cm->bit_depth) {
518       case VPX_BITS_8: ret_val = UL_INTRA_THRESH; break;
519       case VPX_BITS_10: ret_val = UL_INTRA_THRESH << 2; break;
520       default:
521         assert(cm->bit_depth == VPX_BITS_12);
522         ret_val = UL_INTRA_THRESH << 4;
523         break;
524     }
525   }
526 #else
527   (void)cm;
528 #endif  // CONFIG_VP9_HIGHBITDEPTH
529   return ret_val;
530 }
531 
532 #define SMOOTH_INTRA_THRESH 4000
get_smooth_intra_threshold(VP9_COMMON * cm)533 static int get_smooth_intra_threshold(VP9_COMMON *cm) {
534   int ret_val = SMOOTH_INTRA_THRESH;
535 #if CONFIG_VP9_HIGHBITDEPTH
536   if (cm->use_highbitdepth) {
537     switch (cm->bit_depth) {
538       case VPX_BITS_8: ret_val = SMOOTH_INTRA_THRESH; break;
539       case VPX_BITS_10: ret_val = SMOOTH_INTRA_THRESH << 4; break;
540       default:
541         assert(cm->bit_depth == VPX_BITS_12);
542         ret_val = SMOOTH_INTRA_THRESH << 8;
543         break;
544     }
545   }
546 #else
547   (void)cm;
548 #endif  // CONFIG_VP9_HIGHBITDEPTH
549   return ret_val;
550 }
551 
552 #define FP_DN_THRESH 8
553 #define FP_MAX_DN_THRESH 24
554 #define KERNEL_SIZE 3
555 
556 // Baseline Kernal weights for first pass noise metric
557 static uint8_t fp_dn_kernal_3[KERNEL_SIZE * KERNEL_SIZE] = { 1, 2, 1, 2, 4,
558                                                              2, 1, 2, 1 };
559 
560 // Estimate noise at a single point based on the impace of a spatial kernal
561 // on the point value
fp_estimate_point_noise(uint8_t * src_ptr,const int stride)562 static int fp_estimate_point_noise(uint8_t *src_ptr, const int stride) {
563   int sum_weight = 0;
564   int sum_val = 0;
565   int i, j;
566   int max_diff = 0;
567   int diff;
568   int dn_diff;
569   uint8_t *tmp_ptr;
570   uint8_t *kernal_ptr;
571   uint8_t dn_val;
572   uint8_t centre_val = *src_ptr;
573 
574   kernal_ptr = fp_dn_kernal_3;
575 
576   // Apply the kernal
577   tmp_ptr = src_ptr - stride - 1;
578   for (i = 0; i < KERNEL_SIZE; ++i) {
579     for (j = 0; j < KERNEL_SIZE; ++j) {
580       diff = abs((int)centre_val - (int)tmp_ptr[j]);
581       max_diff = VPXMAX(max_diff, diff);
582       if (diff <= FP_DN_THRESH) {
583         sum_weight += *kernal_ptr;
584         sum_val += (int)tmp_ptr[j] * (int)*kernal_ptr;
585       }
586       ++kernal_ptr;
587     }
588     tmp_ptr += stride;
589   }
590 
591   if (max_diff < FP_MAX_DN_THRESH)
592     // Update the source value with the new filtered value
593     dn_val = (sum_val + (sum_weight >> 1)) / sum_weight;
594   else
595     dn_val = *src_ptr;
596 
597   // return the noise energy as the square of the difference between the
598   // denoised and raw value.
599   dn_diff = (int)*src_ptr - (int)dn_val;
600   return dn_diff * dn_diff;
601 }
602 #if CONFIG_VP9_HIGHBITDEPTH
fp_highbd_estimate_point_noise(uint8_t * src_ptr,const int stride)603 static int fp_highbd_estimate_point_noise(uint8_t *src_ptr, const int stride) {
604   int sum_weight = 0;
605   int sum_val = 0;
606   int i, j;
607   int max_diff = 0;
608   int diff;
609   int dn_diff;
610   uint8_t *tmp_ptr;
611   uint16_t *tmp_ptr16;
612   uint8_t *kernal_ptr;
613   uint16_t dn_val;
614   uint16_t centre_val = *CONVERT_TO_SHORTPTR(src_ptr);
615 
616   kernal_ptr = fp_dn_kernal_3;
617 
618   // Apply the kernal
619   tmp_ptr = src_ptr - stride - 1;
620   for (i = 0; i < KERNEL_SIZE; ++i) {
621     tmp_ptr16 = CONVERT_TO_SHORTPTR(tmp_ptr);
622     for (j = 0; j < KERNEL_SIZE; ++j) {
623       diff = abs((int)centre_val - (int)tmp_ptr16[j]);
624       max_diff = VPXMAX(max_diff, diff);
625       if (diff <= FP_DN_THRESH) {
626         sum_weight += *kernal_ptr;
627         sum_val += (int)tmp_ptr16[j] * (int)*kernal_ptr;
628       }
629       ++kernal_ptr;
630     }
631     tmp_ptr += stride;
632   }
633 
634   if (max_diff < FP_MAX_DN_THRESH)
635     // Update the source value with the new filtered value
636     dn_val = (sum_val + (sum_weight >> 1)) / sum_weight;
637   else
638     dn_val = *CONVERT_TO_SHORTPTR(src_ptr);
639 
640   // return the noise energy as the square of the difference between the
641   // denoised and raw value.
642   dn_diff = (int)(*CONVERT_TO_SHORTPTR(src_ptr)) - (int)dn_val;
643   return dn_diff * dn_diff;
644 }
645 #endif
646 
647 // Estimate noise for a block.
fp_estimate_block_noise(MACROBLOCK * x,BLOCK_SIZE bsize)648 static int fp_estimate_block_noise(MACROBLOCK *x, BLOCK_SIZE bsize) {
649 #if CONFIG_VP9_HIGHBITDEPTH
650   MACROBLOCKD *xd = &x->e_mbd;
651 #endif
652   uint8_t *src_ptr = &x->plane[0].src.buf[0];
653   const int width = num_4x4_blocks_wide_lookup[bsize] * 4;
654   const int height = num_4x4_blocks_high_lookup[bsize] * 4;
655   int w, h;
656   int stride = x->plane[0].src.stride;
657   int block_noise = 0;
658 
659   // Sampled points to reduce cost overhead.
660   for (h = 0; h < height; h += 2) {
661     for (w = 0; w < width; w += 2) {
662 #if CONFIG_VP9_HIGHBITDEPTH
663       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH)
664         block_noise += fp_highbd_estimate_point_noise(src_ptr, stride);
665       else
666         block_noise += fp_estimate_point_noise(src_ptr, stride);
667 #else
668       block_noise += fp_estimate_point_noise(src_ptr, stride);
669 #endif
670       ++src_ptr;
671     }
672     src_ptr += (stride - width);
673   }
674   return block_noise << 2;  // Scale << 2 to account for sampling.
675 }
676 
677 // This function is called to test the functionality of row based
678 // multi-threading in unit tests for bit-exactness
accumulate_floating_point_stats(VP9_COMP * cpi,TileDataEnc * first_tile_col)679 static void accumulate_floating_point_stats(VP9_COMP *cpi,
680                                             TileDataEnc *first_tile_col) {
681   VP9_COMMON *const cm = &cpi->common;
682   int mb_row, mb_col;
683   first_tile_col->fp_data.intra_factor = 0;
684   first_tile_col->fp_data.brightness_factor = 0;
685   first_tile_col->fp_data.neutral_count = 0;
686   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
687     for (mb_col = 0; mb_col < cm->mb_cols; ++mb_col) {
688       const int mb_index = mb_row * cm->mb_cols + mb_col;
689       first_tile_col->fp_data.intra_factor +=
690           cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_intra_factor;
691       first_tile_col->fp_data.brightness_factor +=
692           cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_brightness_factor;
693       first_tile_col->fp_data.neutral_count +=
694           cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_neutral_count;
695     }
696   }
697 }
698 
first_pass_stat_calc(VP9_COMP * cpi,FIRSTPASS_STATS * fps,FIRSTPASS_DATA * fp_acc_data)699 static void first_pass_stat_calc(VP9_COMP *cpi, FIRSTPASS_STATS *fps,
700                                  FIRSTPASS_DATA *fp_acc_data) {
701   VP9_COMMON *const cm = &cpi->common;
702   // The minimum error here insures some bit allocation to frames even
703   // in static regions. The allocation per MB declines for larger formats
704   // where the typical "real" energy per MB also falls.
705   // Initial estimate here uses sqrt(mbs) to define the min_err, where the
706   // number of mbs is proportional to the image area.
707   const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE) ? cpi->initial_mbs
708                                                              : cpi->common.MBs;
709   const double min_err = 200 * sqrt(num_mbs);
710 
711   // Clamp the image start to rows/2. This number of rows is discarded top
712   // and bottom as dead data so rows / 2 means the frame is blank.
713   if ((fp_acc_data->image_data_start_row > cm->mb_rows / 2) ||
714       (fp_acc_data->image_data_start_row == INVALID_ROW)) {
715     fp_acc_data->image_data_start_row = cm->mb_rows / 2;
716   }
717   // Exclude any image dead zone
718   if (fp_acc_data->image_data_start_row > 0) {
719     fp_acc_data->intra_skip_count =
720         VPXMAX(0, fp_acc_data->intra_skip_count -
721                       (fp_acc_data->image_data_start_row * cm->mb_cols * 2));
722   }
723 
724   fp_acc_data->intra_factor = fp_acc_data->intra_factor / (double)num_mbs;
725   fp_acc_data->brightness_factor =
726       fp_acc_data->brightness_factor / (double)num_mbs;
727   fps->weight = fp_acc_data->intra_factor * fp_acc_data->brightness_factor;
728 
729   fps->frame = cm->current_video_frame;
730   fps->spatial_layer_id = cpi->svc.spatial_layer_id;
731 
732   fps->coded_error =
733       ((double)(fp_acc_data->coded_error >> 8) + min_err) / num_mbs;
734   fps->sr_coded_error =
735       ((double)(fp_acc_data->sr_coded_error >> 8) + min_err) / num_mbs;
736   fps->intra_error =
737       ((double)(fp_acc_data->intra_error >> 8) + min_err) / num_mbs;
738 
739   fps->frame_noise_energy =
740       (double)(fp_acc_data->frame_noise_energy) / (double)num_mbs;
741   fps->count = 1.0;
742   fps->pcnt_inter = (double)(fp_acc_data->intercount) / num_mbs;
743   fps->pcnt_second_ref = (double)(fp_acc_data->second_ref_count) / num_mbs;
744   fps->pcnt_neutral = (double)(fp_acc_data->neutral_count) / num_mbs;
745   fps->pcnt_intra_low = (double)(fp_acc_data->intra_count_low) / num_mbs;
746   fps->pcnt_intra_high = (double)(fp_acc_data->intra_count_high) / num_mbs;
747   fps->intra_skip_pct = (double)(fp_acc_data->intra_skip_count) / num_mbs;
748   fps->intra_smooth_pct = (double)(fp_acc_data->intra_smooth_count) / num_mbs;
749   fps->inactive_zone_rows = (double)(fp_acc_data->image_data_start_row);
750   // Currently set to 0 as most issues relate to letter boxing.
751   fps->inactive_zone_cols = (double)0;
752 
753   if (fp_acc_data->mvcount > 0) {
754     fps->MVr = (double)(fp_acc_data->sum_mvr) / fp_acc_data->mvcount;
755     fps->mvr_abs = (double)(fp_acc_data->sum_mvr_abs) / fp_acc_data->mvcount;
756     fps->MVc = (double)(fp_acc_data->sum_mvc) / fp_acc_data->mvcount;
757     fps->mvc_abs = (double)(fp_acc_data->sum_mvc_abs) / fp_acc_data->mvcount;
758     fps->MVrv = ((double)(fp_acc_data->sum_mvrs) -
759                  ((double)(fp_acc_data->sum_mvr) * (fp_acc_data->sum_mvr) /
760                   fp_acc_data->mvcount)) /
761                 fp_acc_data->mvcount;
762     fps->MVcv = ((double)(fp_acc_data->sum_mvcs) -
763                  ((double)(fp_acc_data->sum_mvc) * (fp_acc_data->sum_mvc) /
764                   fp_acc_data->mvcount)) /
765                 fp_acc_data->mvcount;
766     fps->mv_in_out_count =
767         (double)(fp_acc_data->sum_in_vectors) / (fp_acc_data->mvcount * 2);
768     fps->pcnt_motion = (double)(fp_acc_data->mvcount) / num_mbs;
769   } else {
770     fps->MVr = 0.0;
771     fps->mvr_abs = 0.0;
772     fps->MVc = 0.0;
773     fps->mvc_abs = 0.0;
774     fps->MVrv = 0.0;
775     fps->MVcv = 0.0;
776     fps->mv_in_out_count = 0.0;
777     fps->pcnt_motion = 0.0;
778   }
779 }
780 
accumulate_fp_mb_row_stat(TileDataEnc * this_tile,FIRSTPASS_DATA * fp_acc_data)781 static void accumulate_fp_mb_row_stat(TileDataEnc *this_tile,
782                                       FIRSTPASS_DATA *fp_acc_data) {
783   this_tile->fp_data.intra_factor += fp_acc_data->intra_factor;
784   this_tile->fp_data.brightness_factor += fp_acc_data->brightness_factor;
785   this_tile->fp_data.coded_error += fp_acc_data->coded_error;
786   this_tile->fp_data.sr_coded_error += fp_acc_data->sr_coded_error;
787   this_tile->fp_data.frame_noise_energy += fp_acc_data->frame_noise_energy;
788   this_tile->fp_data.intra_error += fp_acc_data->intra_error;
789   this_tile->fp_data.intercount += fp_acc_data->intercount;
790   this_tile->fp_data.second_ref_count += fp_acc_data->second_ref_count;
791   this_tile->fp_data.neutral_count += fp_acc_data->neutral_count;
792   this_tile->fp_data.intra_count_low += fp_acc_data->intra_count_low;
793   this_tile->fp_data.intra_count_high += fp_acc_data->intra_count_high;
794   this_tile->fp_data.intra_skip_count += fp_acc_data->intra_skip_count;
795   this_tile->fp_data.mvcount += fp_acc_data->mvcount;
796   this_tile->fp_data.sum_mvr += fp_acc_data->sum_mvr;
797   this_tile->fp_data.sum_mvr_abs += fp_acc_data->sum_mvr_abs;
798   this_tile->fp_data.sum_mvc += fp_acc_data->sum_mvc;
799   this_tile->fp_data.sum_mvc_abs += fp_acc_data->sum_mvc_abs;
800   this_tile->fp_data.sum_mvrs += fp_acc_data->sum_mvrs;
801   this_tile->fp_data.sum_mvcs += fp_acc_data->sum_mvcs;
802   this_tile->fp_data.sum_in_vectors += fp_acc_data->sum_in_vectors;
803   this_tile->fp_data.intra_smooth_count += fp_acc_data->intra_smooth_count;
804   this_tile->fp_data.image_data_start_row =
805       VPXMIN(this_tile->fp_data.image_data_start_row,
806              fp_acc_data->image_data_start_row) == INVALID_ROW
807           ? VPXMAX(this_tile->fp_data.image_data_start_row,
808                    fp_acc_data->image_data_start_row)
809           : VPXMIN(this_tile->fp_data.image_data_start_row,
810                    fp_acc_data->image_data_start_row);
811 }
812 
813 #define NZ_MOTION_PENALTY 128
814 #define INTRA_MODE_PENALTY 1024
vp9_first_pass_encode_tile_mb_row(VP9_COMP * cpi,ThreadData * td,FIRSTPASS_DATA * fp_acc_data,TileDataEnc * tile_data,MV * best_ref_mv,int mb_row)815 void vp9_first_pass_encode_tile_mb_row(VP9_COMP *cpi, ThreadData *td,
816                                        FIRSTPASS_DATA *fp_acc_data,
817                                        TileDataEnc *tile_data, MV *best_ref_mv,
818                                        int mb_row) {
819   int mb_col;
820   MACROBLOCK *const x = &td->mb;
821   VP9_COMMON *const cm = &cpi->common;
822   MACROBLOCKD *const xd = &x->e_mbd;
823   TileInfo tile = tile_data->tile_info;
824   const int mb_col_start = ROUND_POWER_OF_TWO(tile.mi_col_start, 1);
825   const int mb_col_end = ROUND_POWER_OF_TWO(tile.mi_col_end, 1);
826   struct macroblock_plane *const p = x->plane;
827   struct macroblockd_plane *const pd = xd->plane;
828   const PICK_MODE_CONTEXT *ctx = &td->pc_root->none;
829   int i, c;
830   int num_mb_cols = get_num_cols(tile_data->tile_info, 1);
831 
832   int recon_yoffset, recon_uvoffset;
833   const int intrapenalty = INTRA_MODE_PENALTY;
834   const MV zero_mv = { 0, 0 };
835   int recon_y_stride, recon_uv_stride, uv_mb_height;
836 
837   YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
838   YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
839   YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
840   const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
841 
842   MODE_INFO mi_above, mi_left;
843 
844   double mb_intra_factor;
845   double mb_brightness_factor;
846   double mb_neutral_count;
847   int scaled_low_intra_thresh = scale_sse_threshold(cm, LOW_I_THRESH);
848 
849   // First pass code requires valid last and new frame buffers.
850   assert(new_yv12 != NULL);
851   assert(frame_is_intra_only(cm) || (lst_yv12 != NULL));
852 
853   xd->mi = cm->mi_grid_visible + xd->mi_stride * (mb_row << 1) + mb_col_start;
854   xd->mi[0] = cm->mi + xd->mi_stride * (mb_row << 1) + mb_col_start;
855 
856   for (i = 0; i < MAX_MB_PLANE; ++i) {
857     p[i].coeff = ctx->coeff_pbuf[i][1];
858     p[i].qcoeff = ctx->qcoeff_pbuf[i][1];
859     pd[i].dqcoeff = ctx->dqcoeff_pbuf[i][1];
860     p[i].eobs = ctx->eobs_pbuf[i][1];
861   }
862 
863   recon_y_stride = new_yv12->y_stride;
864   recon_uv_stride = new_yv12->uv_stride;
865   uv_mb_height = 16 >> (new_yv12->y_height > new_yv12->uv_height);
866 
867   // Reset above block coeffs.
868   recon_yoffset = (mb_row * recon_y_stride * 16) + mb_col_start * 16;
869   recon_uvoffset =
870       (mb_row * recon_uv_stride * uv_mb_height) + mb_col_start * uv_mb_height;
871 
872   // Set up limit values for motion vectors to prevent them extending
873   // outside the UMV borders.
874   x->mv_limits.row_min = -((mb_row * 16) + BORDER_MV_PIXELS_B16);
875   x->mv_limits.row_max =
876       ((cm->mb_rows - 1 - mb_row) * 16) + BORDER_MV_PIXELS_B16;
877 
878   for (mb_col = mb_col_start, c = 0; mb_col < mb_col_end; ++mb_col, c++) {
879     int this_error;
880     int this_intra_error;
881     const int use_dc_pred = (mb_col || mb_row) && (!mb_col || !mb_row);
882     const BLOCK_SIZE bsize = get_bsize(cm, mb_row, mb_col);
883     double log_intra;
884     int level_sample;
885     const int mb_index = mb_row * cm->mb_cols + mb_col;
886 
887 #if CONFIG_FP_MB_STATS
888     const int mb_index = mb_row * cm->mb_cols + mb_col;
889 #endif
890 
891     (*(cpi->row_mt_sync_read_ptr))(&tile_data->row_mt_sync, mb_row, c);
892 
893     // Adjust to the next column of MBs.
894     x->plane[0].src.buf = cpi->Source->y_buffer +
895                           mb_row * 16 * x->plane[0].src.stride + mb_col * 16;
896     x->plane[1].src.buf = cpi->Source->u_buffer +
897                           mb_row * uv_mb_height * x->plane[1].src.stride +
898                           mb_col * uv_mb_height;
899     x->plane[2].src.buf = cpi->Source->v_buffer +
900                           mb_row * uv_mb_height * x->plane[1].src.stride +
901                           mb_col * uv_mb_height;
902 
903     vpx_clear_system_state();
904 
905     xd->plane[0].dst.buf = new_yv12->y_buffer + recon_yoffset;
906     xd->plane[1].dst.buf = new_yv12->u_buffer + recon_uvoffset;
907     xd->plane[2].dst.buf = new_yv12->v_buffer + recon_uvoffset;
908     xd->mi[0]->sb_type = bsize;
909     xd->mi[0]->ref_frame[0] = INTRA_FRAME;
910     set_mi_row_col(xd, &tile, mb_row << 1, num_8x8_blocks_high_lookup[bsize],
911                    mb_col << 1, num_8x8_blocks_wide_lookup[bsize], cm->mi_rows,
912                    cm->mi_cols);
913     // Are edges available for intra prediction?
914     // Since the firstpass does not populate the mi_grid_visible,
915     // above_mi/left_mi must be overwritten with a nonzero value when edges
916     // are available.  Required by vp9_predict_intra_block().
917     xd->above_mi = (mb_row != 0) ? &mi_above : NULL;
918     xd->left_mi = ((mb_col << 1) > tile.mi_col_start) ? &mi_left : NULL;
919 
920     // Do intra 16x16 prediction.
921     x->skip_encode = 0;
922     x->fp_src_pred = 0;
923     // Do intra prediction based on source pixels for tile boundaries
924     if (mb_col == mb_col_start && mb_col != 0) {
925       xd->left_mi = &mi_left;
926       x->fp_src_pred = 1;
927     }
928     xd->mi[0]->mode = DC_PRED;
929     xd->mi[0]->tx_size =
930         use_dc_pred ? (bsize >= BLOCK_16X16 ? TX_16X16 : TX_8X8) : TX_4X4;
931     // Fix - zero the 16x16 block first. This ensures correct this_error for
932     // block sizes smaller than 16x16.
933     vp9_zero_array(x->plane[0].src_diff, 256);
934     vp9_encode_intra_block_plane(x, bsize, 0, 0);
935     this_error = vpx_get_mb_ss(x->plane[0].src_diff);
936     this_intra_error = this_error;
937 
938     // Keep a record of blocks that have very low intra error residual
939     // (i.e. are in effect completely flat and untextured in the intra
940     // domain). In natural videos this is uncommon, but it is much more
941     // common in animations, graphics and screen content, so may be used
942     // as a signal to detect these types of content.
943     if (this_error < get_ul_intra_threshold(cm)) {
944       ++(fp_acc_data->intra_skip_count);
945     } else if ((mb_col > 0) &&
946                (fp_acc_data->image_data_start_row == INVALID_ROW)) {
947       fp_acc_data->image_data_start_row = mb_row;
948     }
949 
950     // Blocks that are mainly smooth in the intra domain.
951     // Some special accounting for CQ but also these are better for testing
952     // noise levels.
953     if (this_error < get_smooth_intra_threshold(cm)) {
954       ++(fp_acc_data->intra_smooth_count);
955     }
956 
957     // Special case noise measurement for first frame.
958     if (cm->current_video_frame == 0) {
959       if (this_intra_error < scale_sse_threshold(cm, LOW_I_THRESH)) {
960         fp_acc_data->frame_noise_energy += fp_estimate_block_noise(x, bsize);
961       } else {
962         fp_acc_data->frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
963       }
964     }
965 
966 #if CONFIG_VP9_HIGHBITDEPTH
967     if (cm->use_highbitdepth) {
968       switch (cm->bit_depth) {
969         case VPX_BITS_8: break;
970         case VPX_BITS_10: this_error >>= 4; break;
971         default:
972           assert(cm->bit_depth == VPX_BITS_12);
973           this_error >>= 8;
974           break;
975       }
976     }
977 #endif  // CONFIG_VP9_HIGHBITDEPTH
978 
979     vpx_clear_system_state();
980     log_intra = log(this_error + 1.0);
981     if (log_intra < 10.0) {
982       mb_intra_factor = 1.0 + ((10.0 - log_intra) * 0.05);
983       fp_acc_data->intra_factor += mb_intra_factor;
984       if (cpi->row_mt_bit_exact)
985         cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_intra_factor =
986             mb_intra_factor;
987     } else {
988       fp_acc_data->intra_factor += 1.0;
989       if (cpi->row_mt_bit_exact)
990         cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_intra_factor = 1.0;
991     }
992 
993 #if CONFIG_VP9_HIGHBITDEPTH
994     if (cm->use_highbitdepth)
995       level_sample = CONVERT_TO_SHORTPTR(x->plane[0].src.buf)[0];
996     else
997       level_sample = x->plane[0].src.buf[0];
998 #else
999     level_sample = x->plane[0].src.buf[0];
1000 #endif
1001     if ((level_sample < DARK_THRESH) && (log_intra < 9.0)) {
1002       mb_brightness_factor = 1.0 + (0.01 * (DARK_THRESH - level_sample));
1003       fp_acc_data->brightness_factor += mb_brightness_factor;
1004       if (cpi->row_mt_bit_exact)
1005         cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_brightness_factor =
1006             mb_brightness_factor;
1007     } else {
1008       fp_acc_data->brightness_factor += 1.0;
1009       if (cpi->row_mt_bit_exact)
1010         cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_brightness_factor =
1011             1.0;
1012     }
1013 
1014     // Intrapenalty below deals with situations where the intra and inter
1015     // error scores are very low (e.g. a plain black frame).
1016     // We do not have special cases in first pass for 0,0 and nearest etc so
1017     // all inter modes carry an overhead cost estimate for the mv.
1018     // When the error score is very low this causes us to pick all or lots of
1019     // INTRA modes and throw lots of key frames.
1020     // This penalty adds a cost matching that of a 0,0 mv to the intra case.
1021     this_error += intrapenalty;
1022 
1023     // Accumulate the intra error.
1024     fp_acc_data->intra_error += (int64_t)this_error;
1025 
1026 #if CONFIG_FP_MB_STATS
1027     if (cpi->use_fp_mb_stats) {
1028       // initialization
1029       cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
1030     }
1031 #endif
1032 
1033     // Set up limit values for motion vectors to prevent them extending
1034     // outside the UMV borders.
1035     x->mv_limits.col_min = -((mb_col * 16) + BORDER_MV_PIXELS_B16);
1036     x->mv_limits.col_max =
1037         ((cm->mb_cols - 1 - mb_col) * 16) + BORDER_MV_PIXELS_B16;
1038 
1039     // Other than for the first frame do a motion search.
1040     if (cm->current_video_frame > 0) {
1041       int tmp_err, motion_error, this_motion_error, raw_motion_error;
1042       // Assume 0,0 motion with no mv overhead.
1043       MV mv = { 0, 0 }, tmp_mv = { 0, 0 };
1044       struct buf_2d unscaled_last_source_buf_2d;
1045       vp9_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
1046 
1047       xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
1048 #if CONFIG_VP9_HIGHBITDEPTH
1049       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1050         motion_error = highbd_get_prediction_error(
1051             bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
1052         this_motion_error = highbd_get_prediction_error(
1053             bsize, &x->plane[0].src, &xd->plane[0].pre[0], 8);
1054       } else {
1055         motion_error =
1056             get_prediction_error(bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
1057         this_motion_error = motion_error;
1058       }
1059 #else
1060       motion_error =
1061           get_prediction_error(bsize, &x->plane[0].src, &xd->plane[0].pre[0]);
1062       this_motion_error = motion_error;
1063 #endif  // CONFIG_VP9_HIGHBITDEPTH
1064 
1065       // Compute the motion error of the 0,0 motion using the last source
1066       // frame as the reference. Skip the further motion search on
1067       // reconstructed frame if this error is very small.
1068       unscaled_last_source_buf_2d.buf =
1069           cpi->unscaled_last_source->y_buffer + recon_yoffset;
1070       unscaled_last_source_buf_2d.stride = cpi->unscaled_last_source->y_stride;
1071 #if CONFIG_VP9_HIGHBITDEPTH
1072       if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1073         raw_motion_error = highbd_get_prediction_error(
1074             bsize, &x->plane[0].src, &unscaled_last_source_buf_2d, xd->bd);
1075       } else {
1076         raw_motion_error = get_prediction_error(bsize, &x->plane[0].src,
1077                                                 &unscaled_last_source_buf_2d);
1078       }
1079 #else
1080       raw_motion_error = get_prediction_error(bsize, &x->plane[0].src,
1081                                               &unscaled_last_source_buf_2d);
1082 #endif  // CONFIG_VP9_HIGHBITDEPTH
1083 
1084       if (raw_motion_error > NZ_MOTION_PENALTY) {
1085         // Test last reference frame using the previous best mv as the
1086         // starting point (best reference) for the search.
1087         first_pass_motion_search(cpi, x, best_ref_mv, &mv, &motion_error);
1088 
1089         v_fn_ptr.vf = get_block_variance_fn(bsize);
1090 #if CONFIG_VP9_HIGHBITDEPTH
1091         if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1092           v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, 8);
1093         }
1094 #endif  // CONFIG_VP9_HIGHBITDEPTH
1095         this_motion_error =
1096             vp9_get_mvpred_var(x, &mv, best_ref_mv, &v_fn_ptr, 0);
1097 
1098         // If the current best reference mv is not centered on 0,0 then do a
1099         // 0,0 based search as well.
1100         if (!is_zero_mv(best_ref_mv)) {
1101           tmp_err = INT_MAX;
1102           first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &tmp_err);
1103 
1104           if (tmp_err < motion_error) {
1105             motion_error = tmp_err;
1106             mv = tmp_mv;
1107             this_motion_error =
1108                 vp9_get_mvpred_var(x, &tmp_mv, &zero_mv, &v_fn_ptr, 0);
1109           }
1110         }
1111 
1112         // Search in an older reference frame.
1113         if ((cm->current_video_frame > 1) && gld_yv12 != NULL) {
1114           // Assume 0,0 motion with no mv overhead.
1115           int gf_motion_error;
1116 
1117           xd->plane[0].pre[0].buf = gld_yv12->y_buffer + recon_yoffset;
1118 #if CONFIG_VP9_HIGHBITDEPTH
1119           if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
1120             gf_motion_error = highbd_get_prediction_error(
1121                 bsize, &x->plane[0].src, &xd->plane[0].pre[0], xd->bd);
1122           } else {
1123             gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
1124                                                    &xd->plane[0].pre[0]);
1125           }
1126 #else
1127           gf_motion_error = get_prediction_error(bsize, &x->plane[0].src,
1128                                                  &xd->plane[0].pre[0]);
1129 #endif  // CONFIG_VP9_HIGHBITDEPTH
1130 
1131           first_pass_motion_search(cpi, x, &zero_mv, &tmp_mv, &gf_motion_error);
1132 
1133           if (gf_motion_error < motion_error && gf_motion_error < this_error)
1134             ++(fp_acc_data->second_ref_count);
1135 
1136           // Reset to last frame as reference buffer.
1137           xd->plane[0].pre[0].buf = first_ref_buf->y_buffer + recon_yoffset;
1138           xd->plane[1].pre[0].buf = first_ref_buf->u_buffer + recon_uvoffset;
1139           xd->plane[2].pre[0].buf = first_ref_buf->v_buffer + recon_uvoffset;
1140 
1141           // In accumulating a score for the older reference frame take the
1142           // best of the motion predicted score and the intra coded error
1143           // (just as will be done for) accumulation of "coded_error" for
1144           // the last frame.
1145           if (gf_motion_error < this_error)
1146             fp_acc_data->sr_coded_error += gf_motion_error;
1147           else
1148             fp_acc_data->sr_coded_error += this_error;
1149         } else {
1150           fp_acc_data->sr_coded_error += motion_error;
1151         }
1152       } else {
1153         fp_acc_data->sr_coded_error += motion_error;
1154       }
1155 
1156       // Start by assuming that intra mode is best.
1157       best_ref_mv->row = 0;
1158       best_ref_mv->col = 0;
1159 
1160 #if CONFIG_FP_MB_STATS
1161       if (cpi->use_fp_mb_stats) {
1162         // intra prediction statistics
1163         cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
1164         cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_DCINTRA_MASK;
1165         cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
1166         if (this_error > FPMB_ERROR_LARGE_TH) {
1167           cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
1168         } else if (this_error < FPMB_ERROR_SMALL_TH) {
1169           cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
1170         }
1171       }
1172 #endif
1173 
1174       if (motion_error <= this_error) {
1175         vpx_clear_system_state();
1176 
1177         // Keep a count of cases where the inter and intra were very close
1178         // and very low. This helps with scene cut detection for example in
1179         // cropped clips with black bars at the sides or top and bottom.
1180         if (((this_error - intrapenalty) * 9 <= motion_error * 10) &&
1181             (this_error < (2 * intrapenalty))) {
1182           fp_acc_data->neutral_count += 1.0;
1183           if (cpi->row_mt_bit_exact)
1184             cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_neutral_count =
1185                 1.0;
1186           // Also track cases where the intra is not much worse than the inter
1187           // and use this in limiting the GF/arf group length.
1188         } else if ((this_error > NCOUNT_INTRA_THRESH) &&
1189                    (this_error < (NCOUNT_INTRA_FACTOR * motion_error))) {
1190           mb_neutral_count =
1191               (double)motion_error / DOUBLE_DIVIDE_CHECK((double)this_error);
1192           fp_acc_data->neutral_count += mb_neutral_count;
1193           if (cpi->row_mt_bit_exact)
1194             cpi->twopass.fp_mb_float_stats[mb_index].frame_mb_neutral_count =
1195                 mb_neutral_count;
1196         }
1197 
1198         mv.row *= 8;
1199         mv.col *= 8;
1200         this_error = motion_error;
1201         xd->mi[0]->mode = NEWMV;
1202         xd->mi[0]->mv[0].as_mv = mv;
1203         xd->mi[0]->tx_size = TX_4X4;
1204         xd->mi[0]->ref_frame[0] = LAST_FRAME;
1205         xd->mi[0]->ref_frame[1] = NONE;
1206         vp9_build_inter_predictors_sby(xd, mb_row << 1, mb_col << 1, bsize);
1207         vp9_encode_sby_pass1(x, bsize);
1208         fp_acc_data->sum_mvr += mv.row;
1209         fp_acc_data->sum_mvr_abs += abs(mv.row);
1210         fp_acc_data->sum_mvc += mv.col;
1211         fp_acc_data->sum_mvc_abs += abs(mv.col);
1212         fp_acc_data->sum_mvrs += mv.row * mv.row;
1213         fp_acc_data->sum_mvcs += mv.col * mv.col;
1214         ++(fp_acc_data->intercount);
1215 
1216         *best_ref_mv = mv;
1217 
1218 #if CONFIG_FP_MB_STATS
1219         if (cpi->use_fp_mb_stats) {
1220           // inter prediction statistics
1221           cpi->twopass.frame_mb_stats_buf[mb_index] = 0;
1222           cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_DCINTRA_MASK;
1223           cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_ZERO_MASK;
1224           if (this_error > FPMB_ERROR_LARGE_TH) {
1225             cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_LARGE_MASK;
1226           } else if (this_error < FPMB_ERROR_SMALL_TH) {
1227             cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_ERROR_SMALL_MASK;
1228           }
1229         }
1230 #endif
1231 
1232         if (!is_zero_mv(&mv)) {
1233           ++(fp_acc_data->mvcount);
1234 
1235 #if CONFIG_FP_MB_STATS
1236           if (cpi->use_fp_mb_stats) {
1237             cpi->twopass.frame_mb_stats_buf[mb_index] &= ~FPMB_MOTION_ZERO_MASK;
1238             // check estimated motion direction
1239             if (mv.as_mv.col > 0 && mv.as_mv.col >= abs(mv.as_mv.row)) {
1240               // right direction
1241               cpi->twopass.frame_mb_stats_buf[mb_index] |=
1242                   FPMB_MOTION_RIGHT_MASK;
1243             } else if (mv.as_mv.row < 0 &&
1244                        abs(mv.as_mv.row) >= abs(mv.as_mv.col)) {
1245               // up direction
1246               cpi->twopass.frame_mb_stats_buf[mb_index] |= FPMB_MOTION_UP_MASK;
1247             } else if (mv.as_mv.col < 0 &&
1248                        abs(mv.as_mv.col) >= abs(mv.as_mv.row)) {
1249               // left direction
1250               cpi->twopass.frame_mb_stats_buf[mb_index] |=
1251                   FPMB_MOTION_LEFT_MASK;
1252             } else {
1253               // down direction
1254               cpi->twopass.frame_mb_stats_buf[mb_index] |=
1255                   FPMB_MOTION_DOWN_MASK;
1256             }
1257           }
1258 #endif
1259           // Does the row vector point inwards or outwards?
1260           if (mb_row < cm->mb_rows / 2) {
1261             if (mv.row > 0)
1262               --(fp_acc_data->sum_in_vectors);
1263             else if (mv.row < 0)
1264               ++(fp_acc_data->sum_in_vectors);
1265           } else if (mb_row > cm->mb_rows / 2) {
1266             if (mv.row > 0)
1267               ++(fp_acc_data->sum_in_vectors);
1268             else if (mv.row < 0)
1269               --(fp_acc_data->sum_in_vectors);
1270           }
1271 
1272           // Does the col vector point inwards or outwards?
1273           if (mb_col < cm->mb_cols / 2) {
1274             if (mv.col > 0)
1275               --(fp_acc_data->sum_in_vectors);
1276             else if (mv.col < 0)
1277               ++(fp_acc_data->sum_in_vectors);
1278           } else if (mb_col > cm->mb_cols / 2) {
1279             if (mv.col > 0)
1280               ++(fp_acc_data->sum_in_vectors);
1281             else if (mv.col < 0)
1282               --(fp_acc_data->sum_in_vectors);
1283           }
1284         }
1285         if (this_intra_error < scaled_low_intra_thresh) {
1286           fp_acc_data->frame_noise_energy += fp_estimate_block_noise(x, bsize);
1287         } else {
1288           fp_acc_data->frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
1289         }
1290       } else {  // Intra < inter error
1291         if (this_intra_error < scaled_low_intra_thresh) {
1292           fp_acc_data->frame_noise_energy += fp_estimate_block_noise(x, bsize);
1293           if (this_motion_error < scaled_low_intra_thresh) {
1294             fp_acc_data->intra_count_low += 1.0;
1295           } else {
1296             fp_acc_data->intra_count_high += 1.0;
1297           }
1298         } else {
1299           fp_acc_data->frame_noise_energy += (int64_t)SECTION_NOISE_DEF;
1300           fp_acc_data->intra_count_high += 1.0;
1301         }
1302       }
1303     } else {
1304       fp_acc_data->sr_coded_error += (int64_t)this_error;
1305     }
1306     fp_acc_data->coded_error += (int64_t)this_error;
1307 
1308     recon_yoffset += 16;
1309     recon_uvoffset += uv_mb_height;
1310 
1311     // Accumulate row level stats to the corresponding tile stats
1312     if (cpi->row_mt && mb_col == mb_col_end - 1)
1313       accumulate_fp_mb_row_stat(tile_data, fp_acc_data);
1314 
1315     (*(cpi->row_mt_sync_write_ptr))(&tile_data->row_mt_sync, mb_row, c,
1316                                     num_mb_cols);
1317   }
1318   vpx_clear_system_state();
1319 }
1320 
first_pass_encode(VP9_COMP * cpi,FIRSTPASS_DATA * fp_acc_data)1321 static void first_pass_encode(VP9_COMP *cpi, FIRSTPASS_DATA *fp_acc_data) {
1322   VP9_COMMON *const cm = &cpi->common;
1323   int mb_row;
1324   TileDataEnc tile_data;
1325   TileInfo *tile = &tile_data.tile_info;
1326   MV zero_mv = { 0, 0 };
1327   MV best_ref_mv;
1328   // Tiling is ignored in the first pass.
1329   vp9_tile_init(tile, cm, 0, 0);
1330 
1331   for (mb_row = 0; mb_row < cm->mb_rows; ++mb_row) {
1332     best_ref_mv = zero_mv;
1333     vp9_first_pass_encode_tile_mb_row(cpi, &cpi->td, fp_acc_data, &tile_data,
1334                                       &best_ref_mv, mb_row);
1335   }
1336 }
1337 
vp9_first_pass(VP9_COMP * cpi,const struct lookahead_entry * source)1338 void vp9_first_pass(VP9_COMP *cpi, const struct lookahead_entry *source) {
1339   MACROBLOCK *const x = &cpi->td.mb;
1340   VP9_COMMON *const cm = &cpi->common;
1341   MACROBLOCKD *const xd = &x->e_mbd;
1342   TWO_PASS *twopass = &cpi->twopass;
1343 
1344   YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
1345   YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
1346   YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
1347   const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
1348 
1349   BufferPool *const pool = cm->buffer_pool;
1350 
1351   FIRSTPASS_DATA fp_temp_data;
1352   FIRSTPASS_DATA *fp_acc_data = &fp_temp_data;
1353 
1354   vpx_clear_system_state();
1355   vp9_zero(fp_temp_data);
1356   fp_acc_data->image_data_start_row = INVALID_ROW;
1357 
1358   // First pass code requires valid last and new frame buffers.
1359   assert(new_yv12 != NULL);
1360   assert(frame_is_intra_only(cm) || (lst_yv12 != NULL));
1361 
1362 #if CONFIG_FP_MB_STATS
1363   if (cpi->use_fp_mb_stats) {
1364     vp9_zero_array(cpi->twopass.frame_mb_stats_buf, cm->initial_mbs);
1365   }
1366 #endif
1367 
1368   set_first_pass_params(cpi);
1369   vp9_set_quantizer(cm, find_fp_qindex(cm->bit_depth));
1370 
1371   vp9_setup_block_planes(&x->e_mbd, cm->subsampling_x, cm->subsampling_y);
1372 
1373   vp9_setup_src_planes(x, cpi->Source, 0, 0);
1374   vp9_setup_dst_planes(xd->plane, new_yv12, 0, 0);
1375 
1376   if (!frame_is_intra_only(cm)) {
1377     vp9_setup_pre_planes(xd, 0, first_ref_buf, 0, 0, NULL);
1378   }
1379 
1380   xd->mi = cm->mi_grid_visible;
1381   xd->mi[0] = cm->mi;
1382 
1383   vp9_frame_init_quantizer(cpi);
1384 
1385   x->skip_recode = 0;
1386 
1387   vp9_init_mv_probs(cm);
1388   vp9_initialize_rd_consts(cpi);
1389 
1390   cm->log2_tile_rows = 0;
1391 
1392   if (cpi->row_mt_bit_exact && cpi->twopass.fp_mb_float_stats == NULL)
1393     CHECK_MEM_ERROR(
1394         cm, cpi->twopass.fp_mb_float_stats,
1395         vpx_calloc(cm->MBs * sizeof(*cpi->twopass.fp_mb_float_stats), 1));
1396 
1397   {
1398     FIRSTPASS_STATS fps;
1399     TileDataEnc *first_tile_col;
1400     if (!cpi->row_mt) {
1401       cm->log2_tile_cols = 0;
1402       cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read_dummy;
1403       cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write_dummy;
1404       first_pass_encode(cpi, fp_acc_data);
1405       first_pass_stat_calc(cpi, &fps, fp_acc_data);
1406     } else {
1407       cpi->row_mt_sync_read_ptr = vp9_row_mt_sync_read;
1408       cpi->row_mt_sync_write_ptr = vp9_row_mt_sync_write;
1409       if (cpi->row_mt_bit_exact) {
1410         cm->log2_tile_cols = 0;
1411         vp9_zero_array(cpi->twopass.fp_mb_float_stats, cm->MBs);
1412       }
1413       vp9_encode_fp_row_mt(cpi);
1414       first_tile_col = &cpi->tile_data[0];
1415       if (cpi->row_mt_bit_exact)
1416         accumulate_floating_point_stats(cpi, first_tile_col);
1417       first_pass_stat_calc(cpi, &fps, &(first_tile_col->fp_data));
1418     }
1419 
1420     // Dont allow a value of 0 for duration.
1421     // (Section duration is also defaulted to minimum of 1.0).
1422     fps.duration = VPXMAX(1.0, (double)(source->ts_end - source->ts_start));
1423 
1424     // Don't want to do output stats with a stack variable!
1425     twopass->this_frame_stats = fps;
1426     output_stats(&twopass->this_frame_stats);
1427     accumulate_stats(&twopass->total_stats, &fps);
1428 
1429 #if CONFIG_FP_MB_STATS
1430     if (cpi->use_fp_mb_stats) {
1431       output_fpmb_stats(twopass->frame_mb_stats_buf, cm, cpi->output_pkt_list);
1432     }
1433 #endif
1434   }
1435 
1436   // Copy the previous Last Frame back into gf and and arf buffers if
1437   // the prediction is good enough... but also don't allow it to lag too far.
1438   if ((twopass->sr_update_lag > 3) ||
1439       ((cm->current_video_frame > 0) &&
1440        (twopass->this_frame_stats.pcnt_inter > 0.20) &&
1441        ((twopass->this_frame_stats.intra_error /
1442          DOUBLE_DIVIDE_CHECK(twopass->this_frame_stats.coded_error)) > 2.0))) {
1443     if (gld_yv12 != NULL) {
1444       ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
1445                  cm->ref_frame_map[cpi->lst_fb_idx]);
1446     }
1447     twopass->sr_update_lag = 1;
1448   } else {
1449     ++twopass->sr_update_lag;
1450   }
1451 
1452   vpx_extend_frame_borders(new_yv12);
1453 
1454   // The frame we just compressed now becomes the last frame.
1455   ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
1456              cm->new_fb_idx);
1457 
1458   // Special case for the first frame. Copy into the GF buffer as a second
1459   // reference.
1460   if (cm->current_video_frame == 0 && cpi->gld_fb_idx != INVALID_IDX) {
1461     ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
1462                cm->ref_frame_map[cpi->lst_fb_idx]);
1463   }
1464 
1465   // Use this to see what the first pass reconstruction looks like.
1466   if (0) {
1467     char filename[512];
1468     FILE *recon_file;
1469     snprintf(filename, sizeof(filename), "enc%04d.yuv",
1470              (int)cm->current_video_frame);
1471 
1472     if (cm->current_video_frame == 0)
1473       recon_file = fopen(filename, "wb");
1474     else
1475       recon_file = fopen(filename, "ab");
1476 
1477     (void)fwrite(lst_yv12->buffer_alloc, lst_yv12->frame_size, 1, recon_file);
1478     fclose(recon_file);
1479   }
1480 
1481   ++cm->current_video_frame;
1482   if (cpi->use_svc) vp9_inc_frame_in_layer(cpi);
1483 }
1484 
1485 static const double q_pow_term[(QINDEX_RANGE >> 5) + 1] = { 0.65, 0.70, 0.75,
1486                                                             0.85, 0.90, 0.90,
1487                                                             0.90, 1.00, 1.25 };
1488 
calc_correction_factor(double err_per_mb,double err_divisor,int q)1489 static double calc_correction_factor(double err_per_mb, double err_divisor,
1490                                      int q) {
1491   const double error_term = err_per_mb / DOUBLE_DIVIDE_CHECK(err_divisor);
1492   const int index = q >> 5;
1493   double power_term;
1494 
1495   assert((index >= 0) && (index < (QINDEX_RANGE >> 5)));
1496 
1497   // Adjustment based on quantizer to the power term.
1498   power_term =
1499       q_pow_term[index] +
1500       (((q_pow_term[index + 1] - q_pow_term[index]) * (q % 32)) / 32.0);
1501 
1502   // Calculate correction factor.
1503   if (power_term < 1.0) assert(error_term >= 0.0);
1504 
1505   return fclamp(pow(error_term, power_term), 0.05, 5.0);
1506 }
1507 
wq_err_divisor(VP9_COMP * cpi)1508 static double wq_err_divisor(VP9_COMP *cpi) {
1509   const VP9_COMMON *const cm = &cpi->common;
1510   unsigned int screen_area = (cm->width * cm->height);
1511 
1512   // Use a different error per mb factor for calculating boost for
1513   //  different formats.
1514   if (screen_area <= 640 * 360) {
1515     return 115.0;
1516   } else if (screen_area < 1280 * 720) {
1517     return 125.0;
1518   } else if (screen_area <= 1920 * 1080) {
1519     return 130.0;
1520   } else if (screen_area < 3840 * 2160) {
1521     return 150.0;
1522   }
1523 
1524   // Fall through to here only for 4K and above.
1525   return 200.0;
1526 }
1527 
1528 #define NOISE_FACTOR_MIN 0.9
1529 #define NOISE_FACTOR_MAX 1.1
get_twopass_worst_quality(VP9_COMP * cpi,const double section_err,double inactive_zone,double section_noise,int section_target_bandwidth)1530 static int get_twopass_worst_quality(VP9_COMP *cpi, const double section_err,
1531                                      double inactive_zone, double section_noise,
1532                                      int section_target_bandwidth) {
1533   const RATE_CONTROL *const rc = &cpi->rc;
1534   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
1535   TWO_PASS *const twopass = &cpi->twopass;
1536   double last_group_rate_err;
1537 
1538   // Clamp the target rate to VBR min / max limts.
1539   const int target_rate =
1540       vp9_rc_clamp_pframe_target_size(cpi, section_target_bandwidth);
1541   double noise_factor = pow((section_noise / SECTION_NOISE_DEF), 0.5);
1542   noise_factor = fclamp(noise_factor, NOISE_FACTOR_MIN, NOISE_FACTOR_MAX);
1543   inactive_zone = fclamp(inactive_zone, 0.0, 1.0);
1544 
1545 // TODO(jimbankoski): remove #if here or below when this has been
1546 // well tested.
1547 #if CONFIG_ALWAYS_ADJUST_BPM
1548   // based on recent history adjust expectations of bits per macroblock.
1549   last_group_rate_err =
1550       (double)twopass->rolling_arf_group_actual_bits /
1551       DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
1552   last_group_rate_err = VPXMAX(0.25, VPXMIN(4.0, last_group_rate_err));
1553   twopass->bpm_factor *= (3.0 + last_group_rate_err) / 4.0;
1554   twopass->bpm_factor = VPXMAX(0.25, VPXMIN(4.0, twopass->bpm_factor));
1555 #endif
1556 
1557   if (target_rate <= 0) {
1558     return rc->worst_quality;  // Highest value allowed
1559   } else {
1560     const int num_mbs = (cpi->oxcf.resize_mode != RESIZE_NONE)
1561                             ? cpi->initial_mbs
1562                             : cpi->common.MBs;
1563     const double active_pct = VPXMAX(0.01, 1.0 - inactive_zone);
1564     const int active_mbs = (int)VPXMAX(1, (double)num_mbs * active_pct);
1565     const double av_err_per_mb = section_err / active_pct;
1566     const double speed_term = 1.0 + 0.04 * oxcf->speed;
1567     const int target_norm_bits_per_mb =
1568         (int)(((uint64_t)target_rate << BPER_MB_NORMBITS) / active_mbs);
1569     int q;
1570 
1571 // TODO(jimbankoski): remove #if here or above when this has been
1572 // well tested.
1573 #if !CONFIG_ALWAYS_ADJUST_BPM
1574     // based on recent history adjust expectations of bits per macroblock.
1575     last_group_rate_err =
1576         (double)twopass->rolling_arf_group_actual_bits /
1577         DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
1578     last_group_rate_err = VPXMAX(0.25, VPXMIN(4.0, last_group_rate_err));
1579     twopass->bpm_factor *= (3.0 + last_group_rate_err) / 4.0;
1580     twopass->bpm_factor = VPXMAX(0.25, VPXMIN(4.0, twopass->bpm_factor));
1581 #endif
1582 
1583     // Try and pick a max Q that will be high enough to encode the
1584     // content at the given rate.
1585     for (q = rc->best_quality; q < rc->worst_quality; ++q) {
1586       const double factor =
1587           calc_correction_factor(av_err_per_mb, wq_err_divisor(cpi), q);
1588       const int bits_per_mb = vp9_rc_bits_per_mb(
1589           INTER_FRAME, q,
1590           factor * speed_term * cpi->twopass.bpm_factor * noise_factor,
1591           cpi->common.bit_depth);
1592       if (bits_per_mb <= target_norm_bits_per_mb) break;
1593     }
1594 
1595     // Restriction on active max q for constrained quality mode.
1596     if (cpi->oxcf.rc_mode == VPX_CQ) q = VPXMAX(q, oxcf->cq_level);
1597     return q;
1598   }
1599 }
1600 
setup_rf_level_maxq(VP9_COMP * cpi)1601 static void setup_rf_level_maxq(VP9_COMP *cpi) {
1602   int i;
1603   RATE_CONTROL *const rc = &cpi->rc;
1604   for (i = INTER_NORMAL; i < RATE_FACTOR_LEVELS; ++i) {
1605     int qdelta = vp9_frame_type_qdelta(cpi, i, rc->worst_quality);
1606     rc->rf_level_maxq[i] = VPXMAX(rc->worst_quality + qdelta, rc->best_quality);
1607   }
1608 }
1609 
init_subsampling(VP9_COMP * cpi)1610 static void init_subsampling(VP9_COMP *cpi) {
1611   const VP9_COMMON *const cm = &cpi->common;
1612   RATE_CONTROL *const rc = &cpi->rc;
1613   const int w = cm->width;
1614   const int h = cm->height;
1615   int i;
1616 
1617   for (i = 0; i < FRAME_SCALE_STEPS; ++i) {
1618     // Note: Frames with odd-sized dimensions may result from this scaling.
1619     rc->frame_width[i] = (w * 16) / frame_scale_factor[i];
1620     rc->frame_height[i] = (h * 16) / frame_scale_factor[i];
1621   }
1622 
1623   setup_rf_level_maxq(cpi);
1624 }
1625 
calculate_coded_size(VP9_COMP * cpi,int * scaled_frame_width,int * scaled_frame_height)1626 void calculate_coded_size(VP9_COMP *cpi, int *scaled_frame_width,
1627                           int *scaled_frame_height) {
1628   RATE_CONTROL *const rc = &cpi->rc;
1629   *scaled_frame_width = rc->frame_width[rc->frame_size_selector];
1630   *scaled_frame_height = rc->frame_height[rc->frame_size_selector];
1631 }
1632 
vp9_init_second_pass(VP9_COMP * cpi)1633 void vp9_init_second_pass(VP9_COMP *cpi) {
1634   VP9EncoderConfig *const oxcf = &cpi->oxcf;
1635   RATE_CONTROL *const rc = &cpi->rc;
1636   TWO_PASS *const twopass = &cpi->twopass;
1637   double frame_rate;
1638   FIRSTPASS_STATS *stats;
1639 
1640   zero_stats(&twopass->total_stats);
1641   zero_stats(&twopass->total_left_stats);
1642 
1643   if (!twopass->stats_in_end) return;
1644 
1645   stats = &twopass->total_stats;
1646 
1647   *stats = *twopass->stats_in_end;
1648   twopass->total_left_stats = *stats;
1649 
1650   // Scan the first pass file and calculate a modified score for each
1651   // frame that is used to distribute bits. The modified score is assumed
1652   // to provide a linear basis for bit allocation. I.e a frame A with a score
1653   // that is double that of frame B will be allocated 2x as many bits.
1654   {
1655     double modified_score_total = 0.0;
1656     const FIRSTPASS_STATS *s = twopass->stats_in;
1657     double av_err;
1658 
1659     if (oxcf->vbr_corpus_complexity) {
1660       twopass->mean_mod_score = (double)oxcf->vbr_corpus_complexity / 10.0;
1661       av_err = get_distribution_av_err(cpi, twopass);
1662     } else {
1663       av_err = get_distribution_av_err(cpi, twopass);
1664       // The first scan is unclamped and gives a raw average.
1665       while (s < twopass->stats_in_end) {
1666         modified_score_total += calculate_mod_frame_score(cpi, oxcf, s, av_err);
1667         ++s;
1668       }
1669 
1670       // The average error from this first scan is used to define the midpoint
1671       // error for the rate distribution function.
1672       twopass->mean_mod_score =
1673           modified_score_total / DOUBLE_DIVIDE_CHECK(stats->count);
1674     }
1675 
1676     // Second scan using clamps based on the previous cycle average.
1677     // This may modify the total and average somewhat but we dont bother with
1678     // further itterations.
1679     modified_score_total = 0.0;
1680     s = twopass->stats_in;
1681     while (s < twopass->stats_in_end) {
1682       modified_score_total +=
1683           calculate_norm_frame_score(cpi, twopass, oxcf, s, av_err);
1684       ++s;
1685     }
1686     twopass->normalized_score_left = modified_score_total;
1687 
1688     // If using Corpus wide VBR mode then update the clip target bandwidth to
1689     // reflect how the clip compares to the rest of the corpus.
1690     if (oxcf->vbr_corpus_complexity) {
1691       oxcf->target_bandwidth =
1692           (int64_t)((double)oxcf->target_bandwidth *
1693                     (twopass->normalized_score_left / stats->count));
1694     }
1695 
1696 #if COMPLEXITY_STATS_OUTPUT
1697     {
1698       FILE *compstats;
1699       compstats = fopen("complexity_stats.stt", "a");
1700       fprintf(compstats, "%10.3lf\n",
1701               twopass->normalized_score_left / stats->count);
1702       fclose(compstats);
1703     }
1704 #endif
1705   }
1706 
1707   frame_rate = 10000000.0 * stats->count / stats->duration;
1708   // Each frame can have a different duration, as the frame rate in the source
1709   // isn't guaranteed to be constant. The frame rate prior to the first frame
1710   // encoded in the second pass is a guess. However, the sum duration is not.
1711   // It is calculated based on the actual durations of all frames from the
1712   // first pass.
1713   vp9_new_framerate(cpi, frame_rate);
1714   twopass->bits_left =
1715       (int64_t)(stats->duration * oxcf->target_bandwidth / 10000000.0);
1716 
1717   // This variable monitors how far behind the second ref update is lagging.
1718   twopass->sr_update_lag = 1;
1719 
1720   // Reset the vbr bits off target counters
1721   rc->vbr_bits_off_target = 0;
1722   rc->vbr_bits_off_target_fast = 0;
1723   rc->rate_error_estimate = 0;
1724 
1725   // Static sequence monitor variables.
1726   twopass->kf_zeromotion_pct = 100;
1727   twopass->last_kfgroup_zeromotion_pct = 100;
1728 
1729   // Initialize bits per macro_block estimate correction factor.
1730   twopass->bpm_factor = 1.0;
1731   // Initialize actual and target bits counters for ARF groups so that
1732   // at the start we have a neutral bpm adjustment.
1733   twopass->rolling_arf_group_target_bits = 1;
1734   twopass->rolling_arf_group_actual_bits = 1;
1735 
1736   if (oxcf->resize_mode != RESIZE_NONE) {
1737     init_subsampling(cpi);
1738   }
1739 
1740   // Initialize the arnr strangth adjustment to 0
1741   twopass->arnr_strength_adjustment = 0;
1742 }
1743 
1744 #define SR_DIFF_PART 0.0015
1745 #define INTRA_PART 0.005
1746 #define DEFAULT_DECAY_LIMIT 0.75
1747 #define LOW_SR_DIFF_TRHESH 0.1
1748 #define SR_DIFF_MAX 128.0
1749 #define LOW_CODED_ERR_PER_MB 10.0
1750 #define NCOUNT_FRAME_II_THRESH 6.0
1751 
get_sr_decay_rate(const FRAME_INFO * frame_info,const FIRSTPASS_STATS * frame)1752 static double get_sr_decay_rate(const FRAME_INFO *frame_info,
1753                                 const FIRSTPASS_STATS *frame) {
1754   double sr_diff = (frame->sr_coded_error - frame->coded_error);
1755   double sr_decay = 1.0;
1756   double modified_pct_inter;
1757   double modified_pcnt_intra;
1758   const double motion_amplitude_part =
1759       frame->pcnt_motion *
1760       ((frame->mvc_abs + frame->mvr_abs) /
1761        (frame_info->frame_height + frame_info->frame_width));
1762 
1763   modified_pct_inter = frame->pcnt_inter;
1764   if ((frame->coded_error > LOW_CODED_ERR_PER_MB) &&
1765       ((frame->intra_error / DOUBLE_DIVIDE_CHECK(frame->coded_error)) <
1766        (double)NCOUNT_FRAME_II_THRESH)) {
1767     modified_pct_inter =
1768         frame->pcnt_inter + frame->pcnt_intra_low - frame->pcnt_neutral;
1769   }
1770   modified_pcnt_intra = 100 * (1.0 - modified_pct_inter);
1771 
1772   if ((sr_diff > LOW_SR_DIFF_TRHESH)) {
1773     sr_diff = VPXMIN(sr_diff, SR_DIFF_MAX);
1774     sr_decay = 1.0 - (SR_DIFF_PART * sr_diff) - motion_amplitude_part -
1775                (INTRA_PART * modified_pcnt_intra);
1776   }
1777   return VPXMAX(sr_decay, DEFAULT_DECAY_LIMIT);
1778 }
1779 
1780 // This function gives an estimate of how badly we believe the prediction
1781 // quality is decaying from frame to frame.
get_zero_motion_factor(const FRAME_INFO * frame_info,const FIRSTPASS_STATS * frame_stats)1782 static double get_zero_motion_factor(const FRAME_INFO *frame_info,
1783                                      const FIRSTPASS_STATS *frame_stats) {
1784   const double zero_motion_pct =
1785       frame_stats->pcnt_inter - frame_stats->pcnt_motion;
1786   double sr_decay = get_sr_decay_rate(frame_info, frame_stats);
1787   return VPXMIN(sr_decay, zero_motion_pct);
1788 }
1789 
1790 #define ZM_POWER_FACTOR 0.75
1791 
get_prediction_decay_rate(const FRAME_INFO * frame_info,const FIRSTPASS_STATS * frame_stats)1792 static double get_prediction_decay_rate(const FRAME_INFO *frame_info,
1793                                         const FIRSTPASS_STATS *frame_stats) {
1794   const double sr_decay_rate = get_sr_decay_rate(frame_info, frame_stats);
1795   const double zero_motion_factor =
1796       (0.95 * pow((frame_stats->pcnt_inter - frame_stats->pcnt_motion),
1797                   ZM_POWER_FACTOR));
1798 
1799   return VPXMAX(zero_motion_factor,
1800                 (sr_decay_rate + ((1.0 - sr_decay_rate) * zero_motion_factor)));
1801 }
1802 
get_show_idx(const TWO_PASS * twopass)1803 static int get_show_idx(const TWO_PASS *twopass) {
1804   return (int)(twopass->stats_in - twopass->stats_in_start);
1805 }
1806 // Function to test for a condition where a complex transition is followed
1807 // by a static section. For example in slide shows where there is a fade
1808 // between slides. This is to help with more optimal kf and gf positioning.
check_transition_to_still(const FIRST_PASS_INFO * first_pass_info,int show_idx,int still_interval)1809 static int check_transition_to_still(const FIRST_PASS_INFO *first_pass_info,
1810                                      int show_idx, int still_interval) {
1811   int j;
1812   int num_frames = fps_get_num_frames(first_pass_info);
1813   if (show_idx + still_interval > num_frames) {
1814     return 0;
1815   }
1816 
1817   // Look ahead a few frames to see if static condition persists...
1818   for (j = 0; j < still_interval; ++j) {
1819     const FIRSTPASS_STATS *stats =
1820         fps_get_frame_stats(first_pass_info, show_idx + j);
1821     if (stats->pcnt_inter - stats->pcnt_motion < 0.999) break;
1822   }
1823 
1824   // Only if it does do we signal a transition to still.
1825   return j == still_interval;
1826 }
1827 
1828 // This function detects a flash through the high relative pcnt_second_ref
1829 // score in the frame following a flash frame. The offset passed in should
1830 // reflect this.
detect_flash_from_frame_stats(const FIRSTPASS_STATS * frame_stats)1831 static int detect_flash_from_frame_stats(const FIRSTPASS_STATS *frame_stats) {
1832   // What we are looking for here is a situation where there is a
1833   // brief break in prediction (such as a flash) but subsequent frames
1834   // are reasonably well predicted by an earlier (pre flash) frame.
1835   // The recovery after a flash is indicated by a high pcnt_second_ref
1836   // useage or a second ref coded error notabley lower than the last
1837   // frame coded error.
1838   if (frame_stats == NULL) {
1839     return 0;
1840   }
1841   return (frame_stats->sr_coded_error < frame_stats->coded_error) ||
1842          ((frame_stats->pcnt_second_ref > frame_stats->pcnt_inter) &&
1843           (frame_stats->pcnt_second_ref >= 0.5));
1844 }
1845 
detect_flash(const TWO_PASS * twopass,int offset)1846 static int detect_flash(const TWO_PASS *twopass, int offset) {
1847   const FIRSTPASS_STATS *const next_frame = read_frame_stats(twopass, offset);
1848   return detect_flash_from_frame_stats(next_frame);
1849 }
1850 
1851 // Update the motion related elements to the GF arf boost calculation.
accumulate_frame_motion_stats(const FIRSTPASS_STATS * stats,double * mv_in_out,double * mv_in_out_accumulator,double * abs_mv_in_out_accumulator,double * mv_ratio_accumulator)1852 static void accumulate_frame_motion_stats(const FIRSTPASS_STATS *stats,
1853                                           double *mv_in_out,
1854                                           double *mv_in_out_accumulator,
1855                                           double *abs_mv_in_out_accumulator,
1856                                           double *mv_ratio_accumulator) {
1857   const double pct = stats->pcnt_motion;
1858 
1859   // Accumulate Motion In/Out of frame stats.
1860   *mv_in_out = stats->mv_in_out_count * pct;
1861   *mv_in_out_accumulator += *mv_in_out;
1862   *abs_mv_in_out_accumulator += fabs(*mv_in_out);
1863 
1864   // Accumulate a measure of how uniform (or conversely how random) the motion
1865   // field is (a ratio of abs(mv) / mv).
1866   if (pct > 0.05) {
1867     const double mvr_ratio =
1868         fabs(stats->mvr_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVr));
1869     const double mvc_ratio =
1870         fabs(stats->mvc_abs) / DOUBLE_DIVIDE_CHECK(fabs(stats->MVc));
1871 
1872     *mv_ratio_accumulator +=
1873         pct * (mvr_ratio < stats->mvr_abs ? mvr_ratio : stats->mvr_abs);
1874     *mv_ratio_accumulator +=
1875         pct * (mvc_ratio < stats->mvc_abs ? mvc_ratio : stats->mvc_abs);
1876   }
1877 }
1878 
1879 #define BASELINE_ERR_PER_MB 12500.0
1880 #define GF_MAX_BOOST 96.0
calc_frame_boost(const FRAME_INFO * frame_info,const FIRSTPASS_STATS * this_frame,int avg_frame_qindex,double this_frame_mv_in_out)1881 static double calc_frame_boost(const FRAME_INFO *frame_info,
1882                                const FIRSTPASS_STATS *this_frame,
1883                                int avg_frame_qindex,
1884                                double this_frame_mv_in_out) {
1885   double frame_boost;
1886   const double lq =
1887       vp9_convert_qindex_to_q(avg_frame_qindex, frame_info->bit_depth);
1888   const double boost_q_correction = VPXMIN((0.5 + (lq * 0.015)), 1.5);
1889   const double active_area = calculate_active_area(frame_info, this_frame);
1890 
1891   // Underlying boost factor is based on inter error ratio.
1892   frame_boost = (BASELINE_ERR_PER_MB * active_area) /
1893                 DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
1894 
1895   // Small adjustment for cases where there is a zoom out
1896   if (this_frame_mv_in_out > 0.0)
1897     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1898 
1899   // Q correction and scalling
1900   frame_boost = frame_boost * boost_q_correction;
1901 
1902   return VPXMIN(frame_boost, GF_MAX_BOOST * boost_q_correction);
1903 }
1904 
kf_err_per_mb(VP9_COMP * cpi)1905 static double kf_err_per_mb(VP9_COMP *cpi) {
1906   const VP9_COMMON *const cm = &cpi->common;
1907   unsigned int screen_area = (cm->width * cm->height);
1908 
1909   // Use a different error per mb factor for calculating boost for
1910   //  different formats.
1911   if (screen_area < 1280 * 720) {
1912     return 2000.0;
1913   } else if (screen_area < 1920 * 1080) {
1914     return 500.0;
1915   }
1916   return 250.0;
1917 }
1918 
calc_kf_frame_boost(VP9_COMP * cpi,const FIRSTPASS_STATS * this_frame,double * sr_accumulator,double this_frame_mv_in_out,double max_boost)1919 static double calc_kf_frame_boost(VP9_COMP *cpi,
1920                                   const FIRSTPASS_STATS *this_frame,
1921                                   double *sr_accumulator,
1922                                   double this_frame_mv_in_out,
1923                                   double max_boost) {
1924   double frame_boost;
1925   const double lq = vp9_convert_qindex_to_q(
1926       cpi->rc.avg_frame_qindex[INTER_FRAME], cpi->common.bit_depth);
1927   const double boost_q_correction = VPXMIN((0.50 + (lq * 0.015)), 2.00);
1928   const double active_area =
1929       calculate_active_area(&cpi->frame_info, this_frame);
1930 
1931   // Underlying boost factor is based on inter error ratio.
1932   frame_boost = (kf_err_per_mb(cpi) * active_area) /
1933                 DOUBLE_DIVIDE_CHECK(this_frame->coded_error + *sr_accumulator);
1934 
1935   // Update the accumulator for second ref error difference.
1936   // This is intended to give an indication of how much the coded error is
1937   // increasing over time.
1938   *sr_accumulator += (this_frame->sr_coded_error - this_frame->coded_error);
1939   *sr_accumulator = VPXMAX(0.0, *sr_accumulator);
1940 
1941   // Small adjustment for cases where there is a zoom out
1942   if (this_frame_mv_in_out > 0.0)
1943     frame_boost += frame_boost * (this_frame_mv_in_out * 2.0);
1944 
1945   // Q correction and scaling
1946   // The 40.0 value here is an experimentally derived baseline minimum.
1947   // This value is in line with the minimum per frame boost in the alt_ref
1948   // boost calculation.
1949   frame_boost = ((frame_boost + 40.0) * boost_q_correction);
1950 
1951   return VPXMIN(frame_boost, max_boost * boost_q_correction);
1952 }
1953 
compute_arf_boost(const FRAME_INFO * frame_info,const FIRST_PASS_INFO * first_pass_info,int arf_show_idx,int f_frames,int b_frames,int avg_frame_qindex)1954 static int compute_arf_boost(const FRAME_INFO *frame_info,
1955                              const FIRST_PASS_INFO *first_pass_info,
1956                              int arf_show_idx, int f_frames, int b_frames,
1957                              int avg_frame_qindex) {
1958   int i;
1959   double boost_score = 0.0;
1960   double mv_ratio_accumulator = 0.0;
1961   double decay_accumulator = 1.0;
1962   double this_frame_mv_in_out = 0.0;
1963   double mv_in_out_accumulator = 0.0;
1964   double abs_mv_in_out_accumulator = 0.0;
1965   int arf_boost;
1966   int flash_detected = 0;
1967 
1968   // Search forward from the proposed arf/next gf position.
1969   for (i = 0; i < f_frames; ++i) {
1970     const FIRSTPASS_STATS *this_frame =
1971         fps_get_frame_stats(first_pass_info, arf_show_idx + i);
1972     const FIRSTPASS_STATS *next_frame =
1973         fps_get_frame_stats(first_pass_info, arf_show_idx + i + 1);
1974     if (this_frame == NULL) break;
1975 
1976     // Update the motion related elements to the boost calculation.
1977     accumulate_frame_motion_stats(
1978         this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
1979         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
1980 
1981     // We want to discount the flash frame itself and the recovery
1982     // frame that follows as both will have poor scores.
1983     flash_detected = detect_flash_from_frame_stats(this_frame) ||
1984                      detect_flash_from_frame_stats(next_frame);
1985 
1986     // Accumulate the effect of prediction quality decay.
1987     if (!flash_detected) {
1988       decay_accumulator *= get_prediction_decay_rate(frame_info, this_frame);
1989       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
1990                               ? MIN_DECAY_FACTOR
1991                               : decay_accumulator;
1992     }
1993     boost_score += decay_accumulator * calc_frame_boost(frame_info, this_frame,
1994                                                         avg_frame_qindex,
1995                                                         this_frame_mv_in_out);
1996   }
1997 
1998   arf_boost = (int)boost_score;
1999 
2000   // Reset for backward looking loop.
2001   boost_score = 0.0;
2002   mv_ratio_accumulator = 0.0;
2003   decay_accumulator = 1.0;
2004   this_frame_mv_in_out = 0.0;
2005   mv_in_out_accumulator = 0.0;
2006   abs_mv_in_out_accumulator = 0.0;
2007 
2008   // Search backward towards last gf position.
2009   for (i = -1; i >= -b_frames; --i) {
2010     const FIRSTPASS_STATS *this_frame =
2011         fps_get_frame_stats(first_pass_info, arf_show_idx + i);
2012     const FIRSTPASS_STATS *next_frame =
2013         fps_get_frame_stats(first_pass_info, arf_show_idx + i + 1);
2014     if (this_frame == NULL) break;
2015 
2016     // Update the motion related elements to the boost calculation.
2017     accumulate_frame_motion_stats(
2018         this_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
2019         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
2020 
2021     // We want to discount the the flash frame itself and the recovery
2022     // frame that follows as both will have poor scores.
2023     flash_detected = detect_flash_from_frame_stats(this_frame) ||
2024                      detect_flash_from_frame_stats(next_frame);
2025 
2026     // Cumulative effect of prediction quality decay.
2027     if (!flash_detected) {
2028       decay_accumulator *= get_prediction_decay_rate(frame_info, this_frame);
2029       decay_accumulator = decay_accumulator < MIN_DECAY_FACTOR
2030                               ? MIN_DECAY_FACTOR
2031                               : decay_accumulator;
2032     }
2033     boost_score += decay_accumulator * calc_frame_boost(frame_info, this_frame,
2034                                                         avg_frame_qindex,
2035                                                         this_frame_mv_in_out);
2036   }
2037   arf_boost += (int)boost_score;
2038 
2039   if (arf_boost < ((b_frames + f_frames) * 40))
2040     arf_boost = ((b_frames + f_frames) * 40);
2041   arf_boost = VPXMAX(arf_boost, MIN_ARF_GF_BOOST);
2042 
2043   return arf_boost;
2044 }
2045 
calc_arf_boost(VP9_COMP * cpi,int f_frames,int b_frames)2046 static int calc_arf_boost(VP9_COMP *cpi, int f_frames, int b_frames) {
2047   const FRAME_INFO *frame_info = &cpi->frame_info;
2048   TWO_PASS *const twopass = &cpi->twopass;
2049   const int avg_inter_frame_qindex = cpi->rc.avg_frame_qindex[INTER_FRAME];
2050   int arf_show_idx = get_show_idx(twopass);
2051   return compute_arf_boost(frame_info, &twopass->first_pass_info, arf_show_idx,
2052                            f_frames, b_frames, avg_inter_frame_qindex);
2053 }
2054 
2055 // Calculate a section intra ratio used in setting max loop filter.
calculate_section_intra_ratio(const FIRSTPASS_STATS * begin,const FIRSTPASS_STATS * end,int section_length)2056 static int calculate_section_intra_ratio(const FIRSTPASS_STATS *begin,
2057                                          const FIRSTPASS_STATS *end,
2058                                          int section_length) {
2059   const FIRSTPASS_STATS *s = begin;
2060   double intra_error = 0.0;
2061   double coded_error = 0.0;
2062   int i = 0;
2063 
2064   while (s < end && i < section_length) {
2065     intra_error += s->intra_error;
2066     coded_error += s->coded_error;
2067     ++s;
2068     ++i;
2069   }
2070 
2071   return (int)(intra_error / DOUBLE_DIVIDE_CHECK(coded_error));
2072 }
2073 
2074 // Calculate the total bits to allocate in this GF/ARF group.
calculate_total_gf_group_bits(VP9_COMP * cpi,double gf_group_err)2075 static int64_t calculate_total_gf_group_bits(VP9_COMP *cpi,
2076                                              double gf_group_err) {
2077   VP9_COMMON *const cm = &cpi->common;
2078   const RATE_CONTROL *const rc = &cpi->rc;
2079   const TWO_PASS *const twopass = &cpi->twopass;
2080   const int max_bits = frame_max_bits(rc, &cpi->oxcf);
2081   int64_t total_group_bits;
2082   const int is_key_frame = frame_is_intra_only(cm);
2083   const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active;
2084   int gop_frames =
2085       rc->baseline_gf_interval + rc->source_alt_ref_pending - arf_active_or_kf;
2086 
2087   // Calculate the bits to be allocated to the group as a whole.
2088   if ((twopass->kf_group_bits > 0) && (twopass->kf_group_error_left > 0.0)) {
2089     int key_frame_interval = rc->frames_since_key + rc->frames_to_key;
2090     int distance_from_next_key_frame =
2091         rc->frames_to_key -
2092         (rc->baseline_gf_interval + rc->source_alt_ref_pending);
2093     int max_gf_bits_bias = rc->avg_frame_bandwidth;
2094     double gf_interval_bias_bits_normalize_factor =
2095         (double)rc->baseline_gf_interval / 16;
2096     total_group_bits = (int64_t)(twopass->kf_group_bits *
2097                                  (gf_group_err / twopass->kf_group_error_left));
2098     // TODO(ravi): Experiment with different values of max_gf_bits_bias
2099     total_group_bits +=
2100         (int64_t)((double)distance_from_next_key_frame / key_frame_interval *
2101                   max_gf_bits_bias * gf_interval_bias_bits_normalize_factor);
2102   } else {
2103     total_group_bits = 0;
2104   }
2105 
2106   // Clamp odd edge cases.
2107   total_group_bits = (total_group_bits < 0)
2108                          ? 0
2109                          : (total_group_bits > twopass->kf_group_bits)
2110                                ? twopass->kf_group_bits
2111                                : total_group_bits;
2112 
2113   // Clip based on user supplied data rate variability limit.
2114   if (total_group_bits > (int64_t)max_bits * gop_frames)
2115     total_group_bits = (int64_t)max_bits * gop_frames;
2116 
2117   return total_group_bits;
2118 }
2119 
2120 // Calculate the number bits extra to assign to boosted frames in a group.
calculate_boost_bits(int frame_count,int boost,int64_t total_group_bits)2121 static int calculate_boost_bits(int frame_count, int boost,
2122                                 int64_t total_group_bits) {
2123   int allocation_chunks;
2124 
2125   // return 0 for invalid inputs (could arise e.g. through rounding errors)
2126   if (!boost || (total_group_bits <= 0) || (frame_count < 0)) return 0;
2127 
2128   allocation_chunks = (frame_count * NORMAL_BOOST) + boost;
2129 
2130   // Prevent overflow.
2131   if (boost > 1023) {
2132     int divisor = boost >> 10;
2133     boost /= divisor;
2134     allocation_chunks /= divisor;
2135   }
2136 
2137   // Calculate the number of extra bits for use in the boosted frame or frames.
2138   return VPXMAX((int)(((int64_t)boost * total_group_bits) / allocation_chunks),
2139                 0);
2140 }
2141 
2142 // Used in corpus vbr: Calculates the total normalized group complexity score
2143 // for a given number of frames starting at the current position in the stats
2144 // file.
calculate_group_score(VP9_COMP * cpi,double av_score,int frame_count)2145 static double calculate_group_score(VP9_COMP *cpi, double av_score,
2146                                     int frame_count) {
2147   VP9EncoderConfig *const oxcf = &cpi->oxcf;
2148   TWO_PASS *const twopass = &cpi->twopass;
2149   const FIRSTPASS_STATS *s = twopass->stats_in;
2150   double score_total = 0.0;
2151   int i = 0;
2152 
2153   // We dont ever want to return a 0 score here.
2154   if (frame_count == 0) return 1.0;
2155 
2156   while ((i < frame_count) && (s < twopass->stats_in_end)) {
2157     score_total += calculate_norm_frame_score(cpi, twopass, oxcf, s, av_score);
2158     ++s;
2159     ++i;
2160   }
2161 
2162   return score_total;
2163 }
2164 
find_arf_order(VP9_COMP * cpi,GF_GROUP * gf_group,int * index_counter,int depth,int start,int end)2165 static void find_arf_order(VP9_COMP *cpi, GF_GROUP *gf_group,
2166                            int *index_counter, int depth, int start, int end) {
2167   TWO_PASS *twopass = &cpi->twopass;
2168   const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
2169   FIRSTPASS_STATS fpf_frame;
2170   const int mid = (start + end + 1) >> 1;
2171   const int min_frame_interval = 2;
2172   int idx;
2173 
2174   // Process regular P frames
2175   if ((end - start < min_frame_interval) ||
2176       (depth > gf_group->allowed_max_layer_depth)) {
2177     for (idx = start; idx <= end; ++idx) {
2178       gf_group->update_type[*index_counter] = LF_UPDATE;
2179       gf_group->arf_src_offset[*index_counter] = 0;
2180       gf_group->frame_gop_index[*index_counter] = idx;
2181       gf_group->rf_level[*index_counter] = INTER_NORMAL;
2182       gf_group->layer_depth[*index_counter] = depth;
2183       gf_group->gfu_boost[*index_counter] = NORMAL_BOOST;
2184       ++(*index_counter);
2185     }
2186     gf_group->max_layer_depth = VPXMAX(gf_group->max_layer_depth, depth);
2187     return;
2188   }
2189 
2190   assert(abs(mid - start) >= 1 && abs(mid - end) >= 1);
2191 
2192   // Process ARF frame
2193   gf_group->layer_depth[*index_counter] = depth;
2194   gf_group->update_type[*index_counter] = ARF_UPDATE;
2195   gf_group->arf_src_offset[*index_counter] = mid - start;
2196   gf_group->frame_gop_index[*index_counter] = mid;
2197   gf_group->rf_level[*index_counter] = GF_ARF_LOW;
2198 
2199   for (idx = 0; idx <= mid; ++idx)
2200     if (EOF == input_stats(twopass, &fpf_frame)) break;
2201 
2202   gf_group->gfu_boost[*index_counter] =
2203       VPXMAX(MIN_ARF_GF_BOOST,
2204              calc_arf_boost(cpi, end - mid + 1, mid - start) >> depth);
2205 
2206   reset_fpf_position(twopass, start_pos);
2207 
2208   ++(*index_counter);
2209 
2210   find_arf_order(cpi, gf_group, index_counter, depth + 1, start, mid - 1);
2211 
2212   gf_group->update_type[*index_counter] = USE_BUF_FRAME;
2213   gf_group->arf_src_offset[*index_counter] = 0;
2214   gf_group->frame_gop_index[*index_counter] = mid;
2215   gf_group->rf_level[*index_counter] = INTER_NORMAL;
2216   gf_group->layer_depth[*index_counter] = depth;
2217   ++(*index_counter);
2218 
2219   find_arf_order(cpi, gf_group, index_counter, depth + 1, mid + 1, end);
2220 }
2221 
set_gf_overlay_frame_type(GF_GROUP * gf_group,int frame_index,int source_alt_ref_active)2222 static INLINE void set_gf_overlay_frame_type(GF_GROUP *gf_group,
2223                                              int frame_index,
2224                                              int source_alt_ref_active) {
2225   if (source_alt_ref_active) {
2226     gf_group->update_type[frame_index] = OVERLAY_UPDATE;
2227     gf_group->rf_level[frame_index] = INTER_NORMAL;
2228     gf_group->layer_depth[frame_index] = MAX_ARF_LAYERS - 1;
2229     gf_group->gfu_boost[frame_index] = NORMAL_BOOST;
2230   } else {
2231     gf_group->update_type[frame_index] = GF_UPDATE;
2232     gf_group->rf_level[frame_index] = GF_ARF_STD;
2233     gf_group->layer_depth[frame_index] = 0;
2234   }
2235 }
2236 
define_gf_group_structure(VP9_COMP * cpi)2237 static void define_gf_group_structure(VP9_COMP *cpi) {
2238   RATE_CONTROL *const rc = &cpi->rc;
2239   TWO_PASS *const twopass = &cpi->twopass;
2240   GF_GROUP *const gf_group = &twopass->gf_group;
2241   int frame_index = 0;
2242   int key_frame = cpi->common.frame_type == KEY_FRAME;
2243   int layer_depth = 1;
2244   int gop_frames =
2245       rc->baseline_gf_interval - (key_frame || rc->source_alt_ref_pending);
2246 
2247   gf_group->frame_start = cpi->common.current_video_frame;
2248   gf_group->frame_end = gf_group->frame_start + rc->baseline_gf_interval;
2249   gf_group->max_layer_depth = 0;
2250   gf_group->allowed_max_layer_depth = 0;
2251 
2252   // For key frames the frame target rate is already set and it
2253   // is also the golden frame.
2254   // === [frame_index == 0] ===
2255   if (!key_frame)
2256     set_gf_overlay_frame_type(gf_group, frame_index, rc->source_alt_ref_active);
2257 
2258   ++frame_index;
2259 
2260   // === [frame_index == 1] ===
2261   if (rc->source_alt_ref_pending) {
2262     gf_group->update_type[frame_index] = ARF_UPDATE;
2263     gf_group->rf_level[frame_index] = GF_ARF_STD;
2264     gf_group->layer_depth[frame_index] = layer_depth;
2265     gf_group->arf_src_offset[frame_index] =
2266         (unsigned char)(rc->baseline_gf_interval - 1);
2267     gf_group->frame_gop_index[frame_index] = rc->baseline_gf_interval;
2268     gf_group->max_layer_depth = 1;
2269     ++frame_index;
2270     ++layer_depth;
2271     gf_group->allowed_max_layer_depth = cpi->oxcf.enable_auto_arf;
2272   }
2273 
2274   find_arf_order(cpi, gf_group, &frame_index, layer_depth, 1, gop_frames);
2275 
2276   set_gf_overlay_frame_type(gf_group, frame_index, rc->source_alt_ref_pending);
2277   gf_group->arf_src_offset[frame_index] = 0;
2278   gf_group->frame_gop_index[frame_index] = rc->baseline_gf_interval;
2279 
2280   // Set the frame ops number.
2281   gf_group->gf_group_size = frame_index;
2282 }
2283 
allocate_gf_group_bits(VP9_COMP * cpi,int64_t gf_group_bits,int gf_arf_bits)2284 static void allocate_gf_group_bits(VP9_COMP *cpi, int64_t gf_group_bits,
2285                                    int gf_arf_bits) {
2286   VP9EncoderConfig *const oxcf = &cpi->oxcf;
2287   RATE_CONTROL *const rc = &cpi->rc;
2288   TWO_PASS *const twopass = &cpi->twopass;
2289   GF_GROUP *const gf_group = &twopass->gf_group;
2290   FIRSTPASS_STATS frame_stats;
2291   int i;
2292   int frame_index = 0;
2293   int target_frame_size;
2294   int key_frame;
2295   const int max_bits = frame_max_bits(&cpi->rc, oxcf);
2296   int64_t total_group_bits = gf_group_bits;
2297   int mid_frame_idx;
2298   int normal_frames;
2299   int normal_frame_bits;
2300   int last_frame_reduction = 0;
2301   double av_score = 1.0;
2302   double tot_norm_frame_score = 1.0;
2303   double this_frame_score = 1.0;
2304 
2305   // Define the GF structure and specify
2306   int gop_frames = gf_group->gf_group_size;
2307 
2308   key_frame = cpi->common.frame_type == KEY_FRAME;
2309 
2310   // For key frames the frame target rate is already set and it
2311   // is also the golden frame.
2312   // === [frame_index == 0] ===
2313   if (!key_frame) {
2314     gf_group->bit_allocation[frame_index] =
2315         rc->source_alt_ref_active ? 0 : gf_arf_bits;
2316   }
2317 
2318   // Deduct the boost bits for arf (or gf if it is not a key frame)
2319   // from the group total.
2320   if (rc->source_alt_ref_pending || !key_frame) total_group_bits -= gf_arf_bits;
2321 
2322   ++frame_index;
2323 
2324   // === [frame_index == 1] ===
2325   // Store the bits to spend on the ARF if there is one.
2326   if (rc->source_alt_ref_pending) {
2327     gf_group->bit_allocation[frame_index] = gf_arf_bits;
2328 
2329     ++frame_index;
2330   }
2331 
2332   // Define middle frame
2333   mid_frame_idx = frame_index + (rc->baseline_gf_interval >> 1) - 1;
2334 
2335   normal_frames = (rc->baseline_gf_interval - 1);
2336   if (normal_frames > 1)
2337     normal_frame_bits = (int)(total_group_bits / normal_frames);
2338   else
2339     normal_frame_bits = (int)total_group_bits;
2340 
2341   gf_group->gfu_boost[1] = rc->gfu_boost;
2342 
2343   if (cpi->multi_layer_arf) {
2344     int idx;
2345     int arf_depth_bits[MAX_ARF_LAYERS] = { 0 };
2346     int arf_depth_count[MAX_ARF_LAYERS] = { 0 };
2347     int arf_depth_boost[MAX_ARF_LAYERS] = { 0 };
2348     int total_arfs = 1;  // Account for the base layer ARF.
2349 
2350     for (idx = 0; idx < gop_frames; ++idx) {
2351       if (gf_group->update_type[idx] == ARF_UPDATE) {
2352         arf_depth_boost[gf_group->layer_depth[idx]] += gf_group->gfu_boost[idx];
2353         ++arf_depth_count[gf_group->layer_depth[idx]];
2354       }
2355     }
2356 
2357     for (idx = 2; idx < MAX_ARF_LAYERS; ++idx) {
2358       if (arf_depth_boost[idx] == 0) break;
2359       arf_depth_bits[idx] = calculate_boost_bits(
2360           rc->baseline_gf_interval - total_arfs - arf_depth_count[idx],
2361           arf_depth_boost[idx], total_group_bits);
2362 
2363       total_group_bits -= arf_depth_bits[idx];
2364       total_arfs += arf_depth_count[idx];
2365     }
2366 
2367     // offset the base layer arf
2368     normal_frames -= (total_arfs - 1);
2369     if (normal_frames > 1)
2370       normal_frame_bits = (int)(total_group_bits / normal_frames);
2371     else
2372       normal_frame_bits = (int)total_group_bits;
2373 
2374     target_frame_size = normal_frame_bits;
2375     target_frame_size =
2376         clamp(target_frame_size, 0, VPXMIN(max_bits, (int)total_group_bits));
2377 
2378     // The first layer ARF has its bit allocation assigned.
2379     for (idx = frame_index; idx < gop_frames; ++idx) {
2380       switch (gf_group->update_type[idx]) {
2381         case ARF_UPDATE:
2382           gf_group->bit_allocation[idx] =
2383               (int)(((int64_t)arf_depth_bits[gf_group->layer_depth[idx]] *
2384                      gf_group->gfu_boost[idx]) /
2385                     arf_depth_boost[gf_group->layer_depth[idx]]);
2386           break;
2387         case USE_BUF_FRAME: gf_group->bit_allocation[idx] = 0; break;
2388         default: gf_group->bit_allocation[idx] = target_frame_size; break;
2389       }
2390     }
2391     gf_group->bit_allocation[idx] = 0;
2392 
2393     return;
2394   }
2395 
2396   if (oxcf->vbr_corpus_complexity) {
2397     av_score = get_distribution_av_err(cpi, twopass);
2398     tot_norm_frame_score = calculate_group_score(cpi, av_score, normal_frames);
2399   }
2400 
2401   // Allocate bits to the other frames in the group.
2402   for (i = 0; i < normal_frames; ++i) {
2403     if (EOF == input_stats(twopass, &frame_stats)) break;
2404     if (oxcf->vbr_corpus_complexity) {
2405       this_frame_score = calculate_norm_frame_score(cpi, twopass, oxcf,
2406                                                     &frame_stats, av_score);
2407       normal_frame_bits = (int)((double)total_group_bits *
2408                                 (this_frame_score / tot_norm_frame_score));
2409     }
2410 
2411     target_frame_size = normal_frame_bits;
2412     if ((i == (normal_frames - 1)) && (i >= 1)) {
2413       last_frame_reduction = normal_frame_bits / 16;
2414       target_frame_size -= last_frame_reduction;
2415     }
2416 
2417     target_frame_size =
2418         clamp(target_frame_size, 0, VPXMIN(max_bits, (int)total_group_bits));
2419 
2420     gf_group->bit_allocation[frame_index] = target_frame_size;
2421     ++frame_index;
2422   }
2423 
2424   // Add in some extra bits for the middle frame in the group.
2425   gf_group->bit_allocation[mid_frame_idx] += last_frame_reduction;
2426 
2427   // Note:
2428   // We need to configure the frame at the end of the sequence + 1 that will be
2429   // the start frame for the next group. Otherwise prior to the call to
2430   // vp9_rc_get_second_pass_params() the data will be undefined.
2431 }
2432 
2433 // Adjusts the ARNF filter for a GF group.
adjust_group_arnr_filter(VP9_COMP * cpi,double section_noise,double section_inter,double section_motion)2434 static void adjust_group_arnr_filter(VP9_COMP *cpi, double section_noise,
2435                                      double section_inter,
2436                                      double section_motion) {
2437   TWO_PASS *const twopass = &cpi->twopass;
2438   double section_zeromv = section_inter - section_motion;
2439 
2440   twopass->arnr_strength_adjustment = 0;
2441 
2442   if (section_noise < 150) {
2443     twopass->arnr_strength_adjustment -= 1;
2444     if (section_noise < 75) twopass->arnr_strength_adjustment -= 1;
2445   } else if (section_noise > 250)
2446     twopass->arnr_strength_adjustment += 1;
2447 
2448   if (section_zeromv > 0.50) twopass->arnr_strength_adjustment += 1;
2449 }
2450 
2451 // Analyse and define a gf/arf group.
2452 #define ARF_ABS_ZOOM_THRESH 4.0
2453 
2454 #define MAX_GF_BOOST 5400
2455 
2456 typedef struct RANGE {
2457   int min;
2458   int max;
2459 } RANGE;
2460 
get_gop_coding_frame_num(int * use_alt_ref,const FRAME_INFO * frame_info,const FIRST_PASS_INFO * first_pass_info,const RATE_CONTROL * rc,int gf_start_show_idx,const RANGE * active_gf_interval,double gop_intra_factor,int lag_in_frames)2461 static int get_gop_coding_frame_num(
2462     int *use_alt_ref, const FRAME_INFO *frame_info,
2463     const FIRST_PASS_INFO *first_pass_info, const RATE_CONTROL *rc,
2464     int gf_start_show_idx, const RANGE *active_gf_interval,
2465     double gop_intra_factor, int lag_in_frames) {
2466   double loop_decay_rate = 1.00;
2467   double mv_ratio_accumulator = 0.0;
2468   double this_frame_mv_in_out = 0.0;
2469   double mv_in_out_accumulator = 0.0;
2470   double abs_mv_in_out_accumulator = 0.0;
2471   double sr_accumulator = 0.0;
2472   // Motion breakout threshold for loop below depends on image size.
2473   double mv_ratio_accumulator_thresh =
2474       (frame_info->frame_height + frame_info->frame_width) / 4.0;
2475   double zero_motion_accumulator = 1.0;
2476   int gop_coding_frames;
2477 
2478   *use_alt_ref = 1;
2479   gop_coding_frames = 0;
2480   while (gop_coding_frames < rc->static_scene_max_gf_interval &&
2481          gop_coding_frames < rc->frames_to_key) {
2482     const FIRSTPASS_STATS *next_next_frame;
2483     const FIRSTPASS_STATS *next_frame;
2484     int flash_detected;
2485     ++gop_coding_frames;
2486 
2487     next_frame = fps_get_frame_stats(first_pass_info,
2488                                      gf_start_show_idx + gop_coding_frames);
2489     if (next_frame == NULL) {
2490       break;
2491     }
2492 
2493     // Test for the case where there is a brief flash but the prediction
2494     // quality back to an earlier frame is then restored.
2495     next_next_frame = fps_get_frame_stats(
2496         first_pass_info, gf_start_show_idx + gop_coding_frames + 1);
2497     flash_detected = detect_flash_from_frame_stats(next_next_frame);
2498 
2499     // Update the motion related elements to the boost calculation.
2500     accumulate_frame_motion_stats(
2501         next_frame, &this_frame_mv_in_out, &mv_in_out_accumulator,
2502         &abs_mv_in_out_accumulator, &mv_ratio_accumulator);
2503 
2504     // Monitor for static sections.
2505     if ((rc->frames_since_key + gop_coding_frames - 1) > 1) {
2506       zero_motion_accumulator =
2507           VPXMIN(zero_motion_accumulator,
2508                  get_zero_motion_factor(frame_info, next_frame));
2509     }
2510 
2511     // Accumulate the effect of prediction quality decay.
2512     if (!flash_detected) {
2513       double last_loop_decay_rate = loop_decay_rate;
2514       loop_decay_rate = get_prediction_decay_rate(frame_info, next_frame);
2515 
2516       // Break clause to detect very still sections after motion. For example,
2517       // a static image after a fade or other transition.
2518       if (gop_coding_frames > rc->min_gf_interval && loop_decay_rate >= 0.999 &&
2519           last_loop_decay_rate < 0.9) {
2520         int still_interval = 5;
2521         if (check_transition_to_still(first_pass_info,
2522                                       gf_start_show_idx + gop_coding_frames,
2523                                       still_interval)) {
2524           *use_alt_ref = 0;
2525           break;
2526         }
2527       }
2528 
2529       // Update the accumulator for second ref error difference.
2530       // This is intended to give an indication of how much the coded error is
2531       // increasing over time.
2532       if (gop_coding_frames == 1) {
2533         sr_accumulator += next_frame->coded_error;
2534       } else {
2535         sr_accumulator +=
2536             (next_frame->sr_coded_error - next_frame->coded_error);
2537       }
2538     }
2539 
2540     // Break out conditions.
2541     // Break at maximum of active_gf_interval->max unless almost totally
2542     // static.
2543     //
2544     // Note that the addition of a test of rc->source_alt_ref_active is
2545     // deliberate. The effect of this is that after a normal altref group even
2546     // if the material is static there will be one normal length GF group
2547     // before allowing longer GF groups. The reason for this is that in cases
2548     // such as slide shows where slides are separated by a complex transition
2549     // such as a fade, the arf group spanning the transition may not be coded
2550     // at a very high quality and hence this frame (with its overlay) is a
2551     // poor golden frame to use for an extended group.
2552     if ((gop_coding_frames >= active_gf_interval->max) &&
2553         ((zero_motion_accumulator < 0.995) || (rc->source_alt_ref_active))) {
2554       break;
2555     }
2556     if (
2557         // Don't break out with a very short interval.
2558         (gop_coding_frames >= active_gf_interval->min) &&
2559         // If possible dont break very close to a kf
2560         ((rc->frames_to_key - gop_coding_frames) >= rc->min_gf_interval) &&
2561         (gop_coding_frames & 0x01) && (!flash_detected) &&
2562         ((mv_ratio_accumulator > mv_ratio_accumulator_thresh) ||
2563          (abs_mv_in_out_accumulator > ARF_ABS_ZOOM_THRESH) ||
2564          (sr_accumulator > gop_intra_factor * next_frame->intra_error))) {
2565       break;
2566     }
2567   }
2568   *use_alt_ref &= zero_motion_accumulator < 0.995;
2569   *use_alt_ref &= gop_coding_frames < lag_in_frames;
2570   *use_alt_ref &= gop_coding_frames >= rc->min_gf_interval;
2571   return gop_coding_frames;
2572 }
2573 
get_active_gf_inverval_range(const FRAME_INFO * frame_info,const RATE_CONTROL * rc,int arf_active_or_kf,int gf_start_show_idx,int active_worst_quality,int last_boosted_qindex)2574 static RANGE get_active_gf_inverval_range(
2575     const FRAME_INFO *frame_info, const RATE_CONTROL *rc, int arf_active_or_kf,
2576     int gf_start_show_idx, int active_worst_quality, int last_boosted_qindex) {
2577   RANGE active_gf_interval;
2578 #if CONFIG_RATE_CTRL
2579   (void)frame_info;
2580   (void)gf_start_show_idx;
2581   (void)active_worst_quality;
2582   (void)last_boosted_qindex;
2583   active_gf_interval.min = rc->min_gf_interval + arf_active_or_kf + 2;
2584 
2585   active_gf_interval.max = 16 + arf_active_or_kf;
2586 
2587   if ((active_gf_interval.max <= rc->frames_to_key) &&
2588       (active_gf_interval.max >= (rc->frames_to_key - rc->min_gf_interval))) {
2589     active_gf_interval.min = rc->frames_to_key / 2;
2590     active_gf_interval.max = rc->frames_to_key / 2;
2591   }
2592 #else
2593   int int_max_q = (int)(vp9_convert_qindex_to_q(active_worst_quality,
2594                                                 frame_info->bit_depth));
2595   int q_term = (gf_start_show_idx == 0)
2596                    ? int_max_q / 32
2597                    : (int)(vp9_convert_qindex_to_q(last_boosted_qindex,
2598                                                    frame_info->bit_depth) /
2599                            6);
2600   active_gf_interval.min =
2601       rc->min_gf_interval + arf_active_or_kf + VPXMIN(2, int_max_q / 200);
2602   active_gf_interval.min =
2603       VPXMIN(active_gf_interval.min, rc->max_gf_interval + arf_active_or_kf);
2604 
2605   // The value chosen depends on the active Q range. At low Q we have
2606   // bits to spare and are better with a smaller interval and smaller boost.
2607   // At high Q when there are few bits to spare we are better with a longer
2608   // interval to spread the cost of the GF.
2609   active_gf_interval.max = 11 + arf_active_or_kf + VPXMIN(5, q_term);
2610 
2611   // Force max GF interval to be odd.
2612   active_gf_interval.max = active_gf_interval.max | 0x01;
2613 
2614   // We have: active_gf_interval.min <=
2615   // rc->max_gf_interval + arf_active_or_kf.
2616   if (active_gf_interval.max < active_gf_interval.min) {
2617     active_gf_interval.max = active_gf_interval.min;
2618   } else {
2619     active_gf_interval.max =
2620         VPXMIN(active_gf_interval.max, rc->max_gf_interval + arf_active_or_kf);
2621   }
2622 
2623   // Would the active max drop us out just before the near the next kf?
2624   if ((active_gf_interval.max <= rc->frames_to_key) &&
2625       (active_gf_interval.max >= (rc->frames_to_key - rc->min_gf_interval))) {
2626     active_gf_interval.max = rc->frames_to_key / 2;
2627   }
2628   active_gf_interval.max =
2629       VPXMAX(active_gf_interval.max, active_gf_interval.min);
2630 #endif
2631   return active_gf_interval;
2632 }
2633 
get_arf_layers(int multi_layer_arf,int max_layers,int coding_frame_num)2634 static int get_arf_layers(int multi_layer_arf, int max_layers,
2635                           int coding_frame_num) {
2636   assert(max_layers <= MAX_ARF_LAYERS);
2637   if (multi_layer_arf) {
2638     int layers = 0;
2639     int i;
2640     for (i = coding_frame_num; i > 0; i >>= 1) {
2641       ++layers;
2642     }
2643     layers = VPXMIN(max_layers, layers);
2644     return layers;
2645   } else {
2646     return 1;
2647   }
2648 }
2649 
define_gf_group(VP9_COMP * cpi,int gf_start_show_idx)2650 static void define_gf_group(VP9_COMP *cpi, int gf_start_show_idx) {
2651   VP9_COMMON *const cm = &cpi->common;
2652   RATE_CONTROL *const rc = &cpi->rc;
2653   VP9EncoderConfig *const oxcf = &cpi->oxcf;
2654   TWO_PASS *const twopass = &cpi->twopass;
2655   const FRAME_INFO *frame_info = &cpi->frame_info;
2656   const FIRST_PASS_INFO *first_pass_info = &twopass->first_pass_info;
2657   const FIRSTPASS_STATS *const start_pos = twopass->stats_in;
2658   int gop_coding_frames;
2659 
2660   double gf_group_err = 0.0;
2661   double gf_group_raw_error = 0.0;
2662   double gf_group_noise = 0.0;
2663   double gf_group_skip_pct = 0.0;
2664   double gf_group_inactive_zone_rows = 0.0;
2665   double gf_group_inter = 0.0;
2666   double gf_group_motion = 0.0;
2667 
2668   int allow_alt_ref = is_altref_enabled(cpi);
2669   int use_alt_ref;
2670 
2671   int64_t gf_group_bits;
2672   int gf_arf_bits;
2673   const int is_key_frame = frame_is_intra_only(cm);
2674   // If this is a key frame or the overlay from a previous arf then
2675   // the error score / cost of this frame has already been accounted for.
2676   const int arf_active_or_kf = is_key_frame || rc->source_alt_ref_active;
2677   int is_alt_ref_flash = 0;
2678 
2679   double gop_intra_factor;
2680   int gop_frames;
2681   RANGE active_gf_interval;
2682 
2683   // Reset the GF group data structures unless this is a key
2684   // frame in which case it will already have been done.
2685   if (is_key_frame == 0) {
2686     vp9_zero(twopass->gf_group);
2687   }
2688 
2689   vpx_clear_system_state();
2690 
2691   active_gf_interval = get_active_gf_inverval_range(
2692       frame_info, rc, arf_active_or_kf, gf_start_show_idx,
2693       twopass->active_worst_quality, rc->last_boosted_qindex);
2694 
2695   if (cpi->multi_layer_arf) {
2696     int arf_layers = get_arf_layers(cpi->multi_layer_arf, oxcf->enable_auto_arf,
2697                                     active_gf_interval.max);
2698     gop_intra_factor = 1.0 + 0.25 * arf_layers;
2699   } else {
2700     gop_intra_factor = 1.0;
2701   }
2702 
2703   {
2704     gop_coding_frames = get_gop_coding_frame_num(
2705         &use_alt_ref, frame_info, first_pass_info, rc, gf_start_show_idx,
2706         &active_gf_interval, gop_intra_factor, cpi->oxcf.lag_in_frames);
2707     use_alt_ref &= allow_alt_ref;
2708   }
2709 
2710   // Was the group length constrained by the requirement for a new KF?
2711   rc->constrained_gf_group = (gop_coding_frames >= rc->frames_to_key) ? 1 : 0;
2712 
2713   // Should we use the alternate reference frame.
2714   if (use_alt_ref) {
2715     const int f_frames =
2716         (rc->frames_to_key - gop_coding_frames >= gop_coding_frames - 1)
2717             ? gop_coding_frames - 1
2718             : VPXMAX(0, rc->frames_to_key - gop_coding_frames);
2719     const int b_frames = gop_coding_frames - 1;
2720     const int avg_inter_frame_qindex = rc->avg_frame_qindex[INTER_FRAME];
2721     // TODO(angiebird): figure out why arf's location is assigned this way
2722     const int arf_show_idx = VPXMIN(gf_start_show_idx + gop_coding_frames + 1,
2723                                     fps_get_num_frames(first_pass_info));
2724 
2725     // Calculate the boost for alt ref.
2726     rc->gfu_boost =
2727         compute_arf_boost(frame_info, first_pass_info, arf_show_idx, f_frames,
2728                           b_frames, avg_inter_frame_qindex);
2729     rc->source_alt_ref_pending = 1;
2730   } else {
2731     const int f_frames = gop_coding_frames - 1;
2732     const int b_frames = 0;
2733     const int avg_inter_frame_qindex = rc->avg_frame_qindex[INTER_FRAME];
2734     // TODO(angiebird): figure out why arf's location is assigned this way
2735     const int gld_show_idx =
2736         VPXMIN(gf_start_show_idx + 1, fps_get_num_frames(first_pass_info));
2737     const int arf_boost =
2738         compute_arf_boost(frame_info, first_pass_info, gld_show_idx, f_frames,
2739                           b_frames, avg_inter_frame_qindex);
2740     rc->gfu_boost = VPXMIN(MAX_GF_BOOST, arf_boost);
2741     rc->source_alt_ref_pending = 0;
2742   }
2743 
2744 #define LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR 0.2
2745   rc->arf_active_best_quality_adjustment_factor = 1.0;
2746   rc->arf_increase_active_best_quality = 0;
2747 
2748   if (!is_lossless_requested(&cpi->oxcf)) {
2749     if (rc->frames_since_key >= rc->frames_to_key) {
2750       // Increase the active best quality in the second half of key frame
2751       // interval.
2752       rc->arf_active_best_quality_adjustment_factor =
2753           LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR +
2754           (1.0 - LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR) *
2755               (rc->frames_to_key - gop_coding_frames) /
2756               (VPXMAX(1, ((rc->frames_to_key + rc->frames_since_key) / 2 -
2757                           gop_coding_frames)));
2758       rc->arf_increase_active_best_quality = 1;
2759     } else if ((rc->frames_to_key - gop_coding_frames) > 0) {
2760       // Reduce the active best quality in the first half of key frame interval.
2761       rc->arf_active_best_quality_adjustment_factor =
2762           LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR +
2763           (1.0 - LAST_ALR_ACTIVE_BEST_QUALITY_ADJUSTMENT_FACTOR) *
2764               (rc->frames_since_key + gop_coding_frames) /
2765               (VPXMAX(1, (rc->frames_to_key + rc->frames_since_key) / 2 +
2766                              gop_coding_frames));
2767       rc->arf_increase_active_best_quality = -1;
2768     }
2769   }
2770 
2771 #ifdef AGGRESSIVE_VBR
2772   // Limit maximum boost based on interval length.
2773   rc->gfu_boost = VPXMIN((int)rc->gfu_boost, gop_coding_frames * 140);
2774 #else
2775   rc->gfu_boost = VPXMIN((int)rc->gfu_boost, gop_coding_frames * 200);
2776 #endif
2777 
2778   // Cap the ARF boost when perceptual quality AQ mode is enabled. This is
2779   // designed to improve the perceptual quality of high value content and to
2780   // make consistent quality across consecutive frames. It will hurt objective
2781   // quality.
2782   if (oxcf->aq_mode == PERCEPTUAL_AQ)
2783     rc->gfu_boost = VPXMIN(rc->gfu_boost, MIN_ARF_GF_BOOST);
2784 
2785   rc->baseline_gf_interval = gop_coding_frames - rc->source_alt_ref_pending;
2786 
2787   if (rc->source_alt_ref_pending)
2788     is_alt_ref_flash = detect_flash(twopass, rc->baseline_gf_interval);
2789 
2790   {
2791     const double av_err = get_distribution_av_err(cpi, twopass);
2792     const double mean_mod_score = twopass->mean_mod_score;
2793     // If the first frame is a key frame or the overlay from a previous arf then
2794     // the error score / cost of this frame has already been accounted for.
2795     int start_idx = arf_active_or_kf ? 1 : 0;
2796     int j;
2797     for (j = start_idx; j < gop_coding_frames; ++j) {
2798       int show_idx = gf_start_show_idx + j;
2799       const FIRSTPASS_STATS *frame_stats =
2800           fps_get_frame_stats(first_pass_info, show_idx);
2801       // Accumulate error score of frames in this gf group.
2802       gf_group_err += calc_norm_frame_score(oxcf, frame_info, frame_stats,
2803                                             mean_mod_score, av_err);
2804       gf_group_raw_error += frame_stats->coded_error;
2805       gf_group_noise += frame_stats->frame_noise_energy;
2806       gf_group_skip_pct += frame_stats->intra_skip_pct;
2807       gf_group_inactive_zone_rows += frame_stats->inactive_zone_rows;
2808       gf_group_inter += frame_stats->pcnt_inter;
2809       gf_group_motion += frame_stats->pcnt_motion;
2810     }
2811   }
2812 
2813   // Calculate the bits to be allocated to the gf/arf group as a whole
2814   gf_group_bits = calculate_total_gf_group_bits(cpi, gf_group_err);
2815 
2816   gop_frames =
2817       rc->baseline_gf_interval + rc->source_alt_ref_pending - arf_active_or_kf;
2818 
2819   // Store the average moise level measured for the group
2820   // TODO(any): Experiment with removal of else condition (gop_frames = 0) so
2821   // that consumption of group noise energy is based on previous gf group
2822   if (gop_frames > 0)
2823     twopass->gf_group.group_noise_energy = (int)(gf_group_noise / gop_frames);
2824   else
2825     twopass->gf_group.group_noise_energy = 0;
2826 
2827   // Calculate an estimate of the maxq needed for the group.
2828   // We are more aggressive about correcting for sections
2829   // where there could be significant overshoot than for easier
2830   // sections where we do not wish to risk creating an overshoot
2831   // of the allocated bit budget.
2832   if ((cpi->oxcf.rc_mode != VPX_Q) && (rc->baseline_gf_interval > 1)) {
2833     const int vbr_group_bits_per_frame = (int)(gf_group_bits / gop_frames);
2834     const double group_av_err = gf_group_raw_error / gop_frames;
2835     const double group_av_noise = gf_group_noise / gop_frames;
2836     const double group_av_skip_pct = gf_group_skip_pct / gop_frames;
2837     const double group_av_inactive_zone = ((gf_group_inactive_zone_rows * 2) /
2838                                            (gop_frames * (double)cm->mb_rows));
2839     int tmp_q = get_twopass_worst_quality(
2840         cpi, group_av_err, (group_av_skip_pct + group_av_inactive_zone),
2841         group_av_noise, vbr_group_bits_per_frame);
2842     twopass->active_worst_quality =
2843         (tmp_q + (twopass->active_worst_quality * 3)) >> 2;
2844 
2845 #if CONFIG_ALWAYS_ADJUST_BPM
2846     // Reset rolling actual and target bits counters for ARF groups.
2847     twopass->rolling_arf_group_target_bits = 0;
2848     twopass->rolling_arf_group_actual_bits = 0;
2849 #endif
2850   }
2851 
2852   // Context Adjustment of ARNR filter strength
2853   if (rc->baseline_gf_interval > 1) {
2854     adjust_group_arnr_filter(cpi, (gf_group_noise / gop_frames),
2855                              (gf_group_inter / gop_frames),
2856                              (gf_group_motion / gop_frames));
2857   } else {
2858     twopass->arnr_strength_adjustment = 0;
2859   }
2860 
2861   // Calculate the extra bits to be used for boosted frame(s)
2862   gf_arf_bits = calculate_boost_bits((rc->baseline_gf_interval - 1),
2863                                      rc->gfu_boost, gf_group_bits);
2864 
2865   // Adjust KF group bits and error remaining.
2866   twopass->kf_group_error_left -= gf_group_err;
2867 
2868   // Decide GOP structure.
2869   define_gf_group_structure(cpi);
2870 
2871   // Allocate bits to each of the frames in the GF group.
2872   allocate_gf_group_bits(cpi, gf_group_bits, gf_arf_bits);
2873 
2874   // Reset the file position.
2875   reset_fpf_position(twopass, start_pos);
2876 
2877   // Calculate a section intra ratio used in setting max loop filter.
2878   twopass->section_intra_rating = calculate_section_intra_ratio(
2879       start_pos, twopass->stats_in_end, rc->baseline_gf_interval);
2880 
2881   if (oxcf->resize_mode == RESIZE_DYNAMIC) {
2882     // Default to starting GF groups at normal frame size.
2883     cpi->rc.next_frame_size_selector = UNSCALED;
2884   }
2885 #if !CONFIG_ALWAYS_ADJUST_BPM
2886   // Reset rolling actual and target bits counters for ARF groups.
2887   twopass->rolling_arf_group_target_bits = 0;
2888   twopass->rolling_arf_group_actual_bits = 0;
2889 #endif
2890   rc->preserve_arf_as_gld = rc->preserve_next_arf_as_gld;
2891   rc->preserve_next_arf_as_gld = 0;
2892   // If alt ref frame is flash do not set preserve_arf_as_gld
2893   if (!is_lossless_requested(&cpi->oxcf) && !cpi->use_svc &&
2894       cpi->oxcf.aq_mode == NO_AQ && cpi->multi_layer_arf && !is_alt_ref_flash)
2895     rc->preserve_next_arf_as_gld = 1;
2896 }
2897 
2898 // Intra / Inter threshold very low
2899 #define VERY_LOW_II 1.5
2900 // Clean slide transitions we expect a sharp single frame spike in error.
2901 #define ERROR_SPIKE 5.0
2902 
2903 // Slide show transition detection.
2904 // Tests for case where there is very low error either side of the current frame
2905 // but much higher just for this frame. This can help detect key frames in
2906 // slide shows even where the slides are pictures of different sizes.
2907 // Also requires that intra and inter errors are very similar to help eliminate
2908 // harmful false positives.
2909 // It will not help if the transition is a fade or other multi-frame effect.
slide_transition(const FIRSTPASS_STATS * this_frame,const FIRSTPASS_STATS * last_frame,const FIRSTPASS_STATS * next_frame)2910 static int slide_transition(const FIRSTPASS_STATS *this_frame,
2911                             const FIRSTPASS_STATS *last_frame,
2912                             const FIRSTPASS_STATS *next_frame) {
2913   return (this_frame->intra_error < (this_frame->coded_error * VERY_LOW_II)) &&
2914          (this_frame->coded_error > (last_frame->coded_error * ERROR_SPIKE)) &&
2915          (this_frame->coded_error > (next_frame->coded_error * ERROR_SPIKE));
2916 }
2917 
2918 // This test looks for anomalous changes in the nature of the intra signal
2919 // related to the previous and next frame as an indicator for coding a key
2920 // frame. This test serves to detect some additional scene cuts,
2921 // especially in lowish motion and low contrast sections, that are missed
2922 // by the other tests.
intra_step_transition(const FIRSTPASS_STATS * this_frame,const FIRSTPASS_STATS * last_frame,const FIRSTPASS_STATS * next_frame)2923 static int intra_step_transition(const FIRSTPASS_STATS *this_frame,
2924                                  const FIRSTPASS_STATS *last_frame,
2925                                  const FIRSTPASS_STATS *next_frame) {
2926   double last_ii_ratio;
2927   double this_ii_ratio;
2928   double next_ii_ratio;
2929   double last_pcnt_intra = 1.0 - last_frame->pcnt_inter;
2930   double this_pcnt_intra = 1.0 - this_frame->pcnt_inter;
2931   double next_pcnt_intra = 1.0 - next_frame->pcnt_inter;
2932   double mod_this_intra = this_pcnt_intra + this_frame->pcnt_neutral;
2933 
2934   // Calculate ii ratio for this frame last frame and next frame.
2935   last_ii_ratio =
2936       last_frame->intra_error / DOUBLE_DIVIDE_CHECK(last_frame->coded_error);
2937   this_ii_ratio =
2938       this_frame->intra_error / DOUBLE_DIVIDE_CHECK(this_frame->coded_error);
2939   next_ii_ratio =
2940       next_frame->intra_error / DOUBLE_DIVIDE_CHECK(next_frame->coded_error);
2941 
2942   // Return true the intra/inter ratio for the current frame is
2943   // low but better in the next and previous frame and the relative useage of
2944   // intra in the current frame is markedly higher than the last and next frame.
2945   if ((this_ii_ratio < 2.0) && (last_ii_ratio > 2.25) &&
2946       (next_ii_ratio > 2.25) && (this_pcnt_intra > (3 * last_pcnt_intra)) &&
2947       (this_pcnt_intra > (3 * next_pcnt_intra)) &&
2948       ((this_pcnt_intra > 0.075) || (mod_this_intra > 0.85))) {
2949     return 1;
2950     // Very low inter intra ratio (i.e. not much gain from inter coding), most
2951     // blocks neutral on coding method and better inter prediction either side
2952   } else if ((this_ii_ratio < 1.25) && (mod_this_intra > 0.85) &&
2953              (this_ii_ratio < last_ii_ratio * 0.9) &&
2954              (this_ii_ratio < next_ii_ratio * 0.9)) {
2955     return 1;
2956   } else {
2957     return 0;
2958   }
2959 }
2960 
2961 // Minimum % intra coding observed in first pass (1.0 = 100%)
2962 #define MIN_INTRA_LEVEL 0.25
2963 // Threshold for use of the lagging second reference frame. Scene cuts do not
2964 // usually have a high second ref useage.
2965 #define SECOND_REF_USEAGE_THRESH 0.2
2966 // Hard threshold where the first pass chooses intra for almost all blocks.
2967 // In such a case even if the frame is not a scene cut coding a key frame
2968 // may be a good option.
2969 #define VERY_LOW_INTER_THRESH 0.05
2970 // Maximum threshold for the relative ratio of intra error score vs best
2971 // inter error score.
2972 #define KF_II_ERR_THRESHOLD 2.5
2973 #define KF_II_MAX 128.0
2974 #define II_FACTOR 12.5
2975 // Test for very low intra complexity which could cause false key frames
2976 #define V_LOW_INTRA 0.5
2977 
test_candidate_kf(const FIRST_PASS_INFO * first_pass_info,int show_idx)2978 static int test_candidate_kf(const FIRST_PASS_INFO *first_pass_info,
2979                              int show_idx) {
2980   const FIRSTPASS_STATS *last_frame =
2981       fps_get_frame_stats(first_pass_info, show_idx - 1);
2982   const FIRSTPASS_STATS *this_frame =
2983       fps_get_frame_stats(first_pass_info, show_idx);
2984   const FIRSTPASS_STATS *next_frame =
2985       fps_get_frame_stats(first_pass_info, show_idx + 1);
2986   int is_viable_kf = 0;
2987   double pcnt_intra = 1.0 - this_frame->pcnt_inter;
2988 
2989   // Does the frame satisfy the primary criteria of a key frame?
2990   // See above for an explanation of the test criteria.
2991   // If so, then examine how well it predicts subsequent frames.
2992   detect_flash_from_frame_stats(next_frame);
2993   if (!detect_flash_from_frame_stats(this_frame) &&
2994       !detect_flash_from_frame_stats(next_frame) &&
2995       (this_frame->pcnt_second_ref < SECOND_REF_USEAGE_THRESH) &&
2996       ((this_frame->pcnt_inter < VERY_LOW_INTER_THRESH) ||
2997        (slide_transition(this_frame, last_frame, next_frame)) ||
2998        (intra_step_transition(this_frame, last_frame, next_frame)) ||
2999        (((this_frame->coded_error > (next_frame->coded_error * 1.2)) &&
3000          (this_frame->coded_error > (last_frame->coded_error * 1.2))) &&
3001         (pcnt_intra > MIN_INTRA_LEVEL) &&
3002         ((pcnt_intra + this_frame->pcnt_neutral) > 0.5) &&
3003         ((this_frame->intra_error /
3004           DOUBLE_DIVIDE_CHECK(this_frame->coded_error)) <
3005          KF_II_ERR_THRESHOLD)))) {
3006     int i;
3007     double boost_score = 0.0;
3008     double old_boost_score = 0.0;
3009     double decay_accumulator = 1.0;
3010 
3011     // Examine how well the key frame predicts subsequent frames.
3012     for (i = 0; i < 16; ++i) {
3013       const FIRSTPASS_STATS *frame_stats =
3014           fps_get_frame_stats(first_pass_info, show_idx + 1 + i);
3015       double next_iiratio = (II_FACTOR * frame_stats->intra_error /
3016                              DOUBLE_DIVIDE_CHECK(frame_stats->coded_error));
3017 
3018       if (next_iiratio > KF_II_MAX) next_iiratio = KF_II_MAX;
3019 
3020       // Cumulative effect of decay in prediction quality.
3021       if (frame_stats->pcnt_inter > 0.85)
3022         decay_accumulator *= frame_stats->pcnt_inter;
3023       else
3024         decay_accumulator *= (0.85 + frame_stats->pcnt_inter) / 2.0;
3025 
3026       // Keep a running total.
3027       boost_score += (decay_accumulator * next_iiratio);
3028 
3029       // Test various breakout clauses.
3030       if ((frame_stats->pcnt_inter < 0.05) || (next_iiratio < 1.5) ||
3031           (((frame_stats->pcnt_inter - frame_stats->pcnt_neutral) < 0.20) &&
3032            (next_iiratio < 3.0)) ||
3033           ((boost_score - old_boost_score) < 3.0) ||
3034           (frame_stats->intra_error < V_LOW_INTRA)) {
3035         break;
3036       }
3037 
3038       old_boost_score = boost_score;
3039 
3040       // Get the next frame details
3041       if (show_idx + 1 + i == fps_get_num_frames(first_pass_info) - 1) break;
3042     }
3043 
3044     // If there is tolerable prediction for at least the next 3 frames then
3045     // break out else discard this potential key frame and move on
3046     if (boost_score > 30.0 && (i > 3)) {
3047       is_viable_kf = 1;
3048     } else {
3049       is_viable_kf = 0;
3050     }
3051   }
3052 
3053   return is_viable_kf;
3054 }
3055 
3056 #define FRAMES_TO_CHECK_DECAY 8
3057 #define MIN_KF_TOT_BOOST 300
3058 #define DEFAULT_SCAN_FRAMES_FOR_KF_BOOST 32
3059 #define MAX_SCAN_FRAMES_FOR_KF_BOOST 48
3060 #define MIN_SCAN_FRAMES_FOR_KF_BOOST 32
3061 #define KF_ABS_ZOOM_THRESH 6.0
3062 
3063 #ifdef AGGRESSIVE_VBR
3064 #define KF_MAX_FRAME_BOOST 80.0
3065 #define MAX_KF_TOT_BOOST 4800
3066 #else
3067 #define KF_MAX_FRAME_BOOST 96.0
3068 #define MAX_KF_TOT_BOOST 5400
3069 #endif
3070 
vp9_get_frames_to_next_key(const VP9EncoderConfig * oxcf,const FRAME_INFO * frame_info,const FIRST_PASS_INFO * first_pass_info,int kf_show_idx,int min_gf_interval)3071 int vp9_get_frames_to_next_key(const VP9EncoderConfig *oxcf,
3072                                const FRAME_INFO *frame_info,
3073                                const FIRST_PASS_INFO *first_pass_info,
3074                                int kf_show_idx, int min_gf_interval) {
3075   double recent_loop_decay[FRAMES_TO_CHECK_DECAY];
3076   int j;
3077   int frames_to_key;
3078   int max_frames_to_key = first_pass_info->num_frames - kf_show_idx;
3079   max_frames_to_key = VPXMIN(max_frames_to_key, oxcf->key_freq);
3080 
3081   // Initialize the decay rates for the recent frames to check
3082   for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j) recent_loop_decay[j] = 1.0;
3083   // Find the next keyframe.
3084   if (!oxcf->auto_key) {
3085     frames_to_key = max_frames_to_key;
3086   } else {
3087     frames_to_key = 1;
3088     while (frames_to_key < max_frames_to_key) {
3089       // Provided that we are not at the end of the file...
3090       if (kf_show_idx + frames_to_key + 1 < first_pass_info->num_frames) {
3091         double loop_decay_rate;
3092         double decay_accumulator;
3093         const FIRSTPASS_STATS *next_frame = fps_get_frame_stats(
3094             first_pass_info, kf_show_idx + frames_to_key + 1);
3095 
3096         // Check for a scene cut.
3097         if (test_candidate_kf(first_pass_info, kf_show_idx + frames_to_key))
3098           break;
3099 
3100         // How fast is the prediction quality decaying?
3101         loop_decay_rate = get_prediction_decay_rate(frame_info, next_frame);
3102 
3103         // We want to know something about the recent past... rather than
3104         // as used elsewhere where we are concerned with decay in prediction
3105         // quality since the last GF or KF.
3106         recent_loop_decay[(frames_to_key - 1) % FRAMES_TO_CHECK_DECAY] =
3107             loop_decay_rate;
3108         decay_accumulator = 1.0;
3109         for (j = 0; j < FRAMES_TO_CHECK_DECAY; ++j)
3110           decay_accumulator *= recent_loop_decay[j];
3111 
3112         // Special check for transition or high motion followed by a
3113         // static scene.
3114         if ((frames_to_key - 1) > min_gf_interval && loop_decay_rate >= 0.999 &&
3115             decay_accumulator < 0.9) {
3116           int still_interval = oxcf->key_freq - (frames_to_key - 1);
3117           // TODO(angiebird): Figure out why we use "+1" here
3118           int show_idx = kf_show_idx + frames_to_key;
3119           if (check_transition_to_still(first_pass_info, show_idx,
3120                                         still_interval)) {
3121             break;
3122           }
3123         }
3124       }
3125       ++frames_to_key;
3126     }
3127   }
3128   return frames_to_key;
3129 }
3130 
find_next_key_frame(VP9_COMP * cpi,int kf_show_idx)3131 static void find_next_key_frame(VP9_COMP *cpi, int kf_show_idx) {
3132   int i;
3133   RATE_CONTROL *const rc = &cpi->rc;
3134   TWO_PASS *const twopass = &cpi->twopass;
3135   GF_GROUP *const gf_group = &twopass->gf_group;
3136   const VP9EncoderConfig *const oxcf = &cpi->oxcf;
3137   const FIRST_PASS_INFO *first_pass_info = &twopass->first_pass_info;
3138   const FRAME_INFO *frame_info = &cpi->frame_info;
3139   const FIRSTPASS_STATS *const start_position = twopass->stats_in;
3140   const FIRSTPASS_STATS *keyframe_stats =
3141       fps_get_frame_stats(first_pass_info, kf_show_idx);
3142   FIRSTPASS_STATS next_frame;
3143   int kf_bits = 0;
3144   int64_t max_kf_bits;
3145   double zero_motion_accumulator = 1.0;
3146   double zero_motion_sum = 0.0;
3147   double zero_motion_avg;
3148   double motion_compensable_sum = 0.0;
3149   double motion_compensable_avg;
3150   int num_frames = 0;
3151   int kf_boost_scan_frames = DEFAULT_SCAN_FRAMES_FOR_KF_BOOST;
3152   double boost_score = 0.0;
3153   double kf_mod_err = 0.0;
3154   double kf_raw_err = 0.0;
3155   double kf_group_err = 0.0;
3156   double sr_accumulator = 0.0;
3157   double abs_mv_in_out_accumulator = 0.0;
3158   const double av_err = get_distribution_av_err(cpi, twopass);
3159   const double mean_mod_score = twopass->mean_mod_score;
3160   vp9_zero(next_frame);
3161 
3162   cpi->common.frame_type = KEY_FRAME;
3163   rc->frames_since_key = 0;
3164 
3165   // Reset the GF group data structures.
3166   vp9_zero(*gf_group);
3167 
3168   // Is this a forced key frame by interval.
3169   rc->this_key_frame_forced = rc->next_key_frame_forced;
3170 
3171   // Clear the alt ref active flag and last group multi arf flags as they
3172   // can never be set for a key frame.
3173   rc->source_alt_ref_active = 0;
3174 
3175   // KF is always a GF so clear frames till next gf counter.
3176   rc->frames_till_gf_update_due = 0;
3177 
3178   rc->frames_to_key = 1;
3179 
3180   twopass->kf_group_bits = 0;          // Total bits available to kf group
3181   twopass->kf_group_error_left = 0.0;  // Group modified error score.
3182 
3183   kf_raw_err = keyframe_stats->intra_error;
3184   kf_mod_err = calc_norm_frame_score(oxcf, frame_info, keyframe_stats,
3185                                      mean_mod_score, av_err);
3186 
3187   rc->frames_to_key = vp9_get_frames_to_next_key(
3188       oxcf, frame_info, first_pass_info, kf_show_idx, rc->min_gf_interval);
3189 
3190   // If there is a max kf interval set by the user we must obey it.
3191   // We already breakout of the loop above at 2x max.
3192   // This code centers the extra kf if the actual natural interval
3193   // is between 1x and 2x.
3194   if (rc->frames_to_key >= cpi->oxcf.key_freq) {
3195     rc->next_key_frame_forced = 1;
3196   } else {
3197     rc->next_key_frame_forced = 0;
3198   }
3199 
3200   for (i = 0; i < rc->frames_to_key; ++i) {
3201     const FIRSTPASS_STATS *frame_stats =
3202         fps_get_frame_stats(first_pass_info, kf_show_idx + i);
3203     // Accumulate kf group error.
3204     kf_group_err += calc_norm_frame_score(oxcf, frame_info, frame_stats,
3205                                           mean_mod_score, av_err);
3206   }
3207 
3208   // Calculate the number of bits that should be assigned to the kf group.
3209   if (twopass->bits_left > 0 && twopass->normalized_score_left > 0.0) {
3210     // Maximum number of bits for a single normal frame (not key frame).
3211     const int max_bits = frame_max_bits(rc, &cpi->oxcf);
3212 
3213     // Maximum number of bits allocated to the key frame group.
3214     int64_t max_grp_bits;
3215 
3216     // Default allocation based on bits left and relative
3217     // complexity of the section.
3218     twopass->kf_group_bits = (int64_t)(
3219         twopass->bits_left * (kf_group_err / twopass->normalized_score_left));
3220 
3221     // Clip based on maximum per frame rate defined by the user.
3222     max_grp_bits = (int64_t)max_bits * (int64_t)rc->frames_to_key;
3223     if (twopass->kf_group_bits > max_grp_bits)
3224       twopass->kf_group_bits = max_grp_bits;
3225   } else {
3226     twopass->kf_group_bits = 0;
3227   }
3228   twopass->kf_group_bits = VPXMAX(0, twopass->kf_group_bits);
3229 
3230   // Scan through the kf group collating various stats used to determine
3231   // how many bits to spend on it.
3232   boost_score = 0.0;
3233 
3234   for (i = 0; i < VPXMIN(MAX_SCAN_FRAMES_FOR_KF_BOOST, (rc->frames_to_key - 1));
3235        ++i) {
3236     if (EOF == input_stats(twopass, &next_frame)) break;
3237 
3238     zero_motion_sum += next_frame.pcnt_inter - next_frame.pcnt_motion;
3239     motion_compensable_sum +=
3240         1 - (double)next_frame.coded_error / next_frame.intra_error;
3241     num_frames++;
3242   }
3243 
3244   if (num_frames >= MIN_SCAN_FRAMES_FOR_KF_BOOST) {
3245     zero_motion_avg = zero_motion_sum / num_frames;
3246     motion_compensable_avg = motion_compensable_sum / num_frames;
3247     kf_boost_scan_frames = (int)(VPXMAX(64 * zero_motion_avg - 16,
3248                                         160 * motion_compensable_avg - 112));
3249     kf_boost_scan_frames =
3250         VPXMAX(VPXMIN(kf_boost_scan_frames, MAX_SCAN_FRAMES_FOR_KF_BOOST),
3251                MIN_SCAN_FRAMES_FOR_KF_BOOST);
3252   }
3253   reset_fpf_position(twopass, start_position);
3254 
3255   for (i = 0; i < (rc->frames_to_key - 1); ++i) {
3256     if (EOF == input_stats(twopass, &next_frame)) break;
3257 
3258     // The zero motion test here insures that if we mark a kf group as static
3259     // it is static throughout not just the first KF_BOOST_SCAN_MAX_FRAMES.
3260     // It also allows for a larger boost on long static groups.
3261     if ((i <= kf_boost_scan_frames) || (zero_motion_accumulator >= 0.99)) {
3262       double frame_boost;
3263       double zm_factor;
3264 
3265       // Monitor for static sections.
3266       // First frame in kf group the second ref indicator is invalid.
3267       if (i > 0) {
3268         zero_motion_accumulator =
3269             VPXMIN(zero_motion_accumulator,
3270                    get_zero_motion_factor(&cpi->frame_info, &next_frame));
3271       } else {
3272         zero_motion_accumulator =
3273             next_frame.pcnt_inter - next_frame.pcnt_motion;
3274       }
3275 
3276       // Factor 0.75-1.25 based on how much of frame is static.
3277       zm_factor = (0.75 + (zero_motion_accumulator / 2.0));
3278 
3279       // The second (lagging) ref error is not valid immediately after
3280       // a key frame because either the lag has not built up (in the case of
3281       // the first key frame or it points to a refernce before the new key
3282       // frame.
3283       if (i < 2) sr_accumulator = 0.0;
3284       frame_boost = calc_kf_frame_boost(cpi, &next_frame, &sr_accumulator, 0,
3285                                         KF_MAX_FRAME_BOOST * zm_factor);
3286 
3287       boost_score += frame_boost;
3288 
3289       // Measure of zoom. Large zoom tends to indicate reduced boost.
3290       abs_mv_in_out_accumulator +=
3291           fabs(next_frame.mv_in_out_count * next_frame.pcnt_motion);
3292 
3293       if ((frame_boost < 25.00) ||
3294           (abs_mv_in_out_accumulator > KF_ABS_ZOOM_THRESH) ||
3295           (sr_accumulator > (kf_raw_err * 1.50)))
3296         break;
3297     } else {
3298       break;
3299     }
3300   }
3301 
3302   reset_fpf_position(twopass, start_position);
3303 
3304   // Store the zero motion percentage
3305   twopass->kf_zeromotion_pct = (int)(zero_motion_accumulator * 100.0);
3306 
3307   // Calculate a section intra ratio used in setting max loop filter.
3308   twopass->key_frame_section_intra_rating = calculate_section_intra_ratio(
3309       start_position, twopass->stats_in_end, rc->frames_to_key);
3310 
3311   // Special case for static / slide show content but dont apply
3312   // if the kf group is very short.
3313   if ((zero_motion_accumulator > 0.99) && (rc->frames_to_key > 8)) {
3314     rc->kf_boost = MAX_KF_TOT_BOOST;
3315   } else {
3316     // Apply various clamps for min and max boost
3317     rc->kf_boost = VPXMAX((int)boost_score, (rc->frames_to_key * 3));
3318     rc->kf_boost = VPXMAX(rc->kf_boost, MIN_KF_TOT_BOOST);
3319     rc->kf_boost = VPXMIN(rc->kf_boost, MAX_KF_TOT_BOOST);
3320   }
3321 
3322   // Work out how many bits to allocate for the key frame itself.
3323   kf_bits = calculate_boost_bits((rc->frames_to_key - 1), rc->kf_boost,
3324                                  twopass->kf_group_bits);
3325   // Based on the spatial complexity, increase the bits allocated to key frame.
3326   kf_bits +=
3327       (int)((twopass->kf_group_bits - kf_bits) * (kf_mod_err / kf_group_err));
3328   max_kf_bits =
3329       twopass->kf_group_bits - (rc->frames_to_key - 1) * FRAME_OVERHEAD_BITS;
3330   max_kf_bits = lclamp(max_kf_bits, 0, INT_MAX);
3331   kf_bits = VPXMIN(kf_bits, (int)max_kf_bits);
3332 
3333   twopass->kf_group_bits -= kf_bits;
3334 
3335   // Save the bits to spend on the key frame.
3336   gf_group->bit_allocation[0] = kf_bits;
3337   gf_group->update_type[0] = KF_UPDATE;
3338   gf_group->rf_level[0] = KF_STD;
3339   gf_group->layer_depth[0] = 0;
3340 
3341   // Note the total error score of the kf group minus the key frame itself.
3342   twopass->kf_group_error_left = (kf_group_err - kf_mod_err);
3343 
3344   // Adjust the count of total modified error left.
3345   // The count of bits left is adjusted elsewhere based on real coded frame
3346   // sizes.
3347   twopass->normalized_score_left -= kf_group_err;
3348 
3349   if (oxcf->resize_mode == RESIZE_DYNAMIC) {
3350     // Default to normal-sized frame on keyframes.
3351     cpi->rc.next_frame_size_selector = UNSCALED;
3352   }
3353 }
3354 
is_skippable_frame(const VP9_COMP * cpi)3355 static int is_skippable_frame(const VP9_COMP *cpi) {
3356   // If the current frame does not have non-zero motion vector detected in the
3357   // first  pass, and so do its previous and forward frames, then this frame
3358   // can be skipped for partition check, and the partition size is assigned
3359   // according to the variance
3360   const TWO_PASS *const twopass = &cpi->twopass;
3361 
3362   return (!frame_is_intra_only(&cpi->common) &&
3363           twopass->stats_in - 2 > twopass->stats_in_start &&
3364           twopass->stats_in < twopass->stats_in_end &&
3365           (twopass->stats_in - 1)->pcnt_inter -
3366                   (twopass->stats_in - 1)->pcnt_motion ==
3367               1 &&
3368           (twopass->stats_in - 2)->pcnt_inter -
3369                   (twopass->stats_in - 2)->pcnt_motion ==
3370               1 &&
3371           twopass->stats_in->pcnt_inter - twopass->stats_in->pcnt_motion == 1);
3372 }
3373 
vp9_rc_get_second_pass_params(VP9_COMP * cpi)3374 void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
3375   VP9_COMMON *const cm = &cpi->common;
3376   RATE_CONTROL *const rc = &cpi->rc;
3377   TWO_PASS *const twopass = &cpi->twopass;
3378   GF_GROUP *const gf_group = &twopass->gf_group;
3379   FIRSTPASS_STATS this_frame;
3380   const int show_idx = cm->current_video_frame;
3381 
3382   if (!twopass->stats_in) return;
3383 
3384   // If this is an arf frame then we dont want to read the stats file or
3385   // advance the input pointer as we already have what we need.
3386   if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
3387     int target_rate;
3388 
3389     vp9_zero(this_frame);
3390     this_frame =
3391         cpi->twopass.stats_in_start[cm->current_video_frame +
3392                                     gf_group->arf_src_offset[gf_group->index]];
3393 
3394     vp9_configure_buffer_updates(cpi, gf_group->index);
3395 
3396     target_rate = gf_group->bit_allocation[gf_group->index];
3397     target_rate = vp9_rc_clamp_pframe_target_size(cpi, target_rate);
3398     rc->base_frame_target = target_rate;
3399 
3400     cm->frame_type = INTER_FRAME;
3401 
3402     // Do the firstpass stats indicate that this frame is skippable for the
3403     // partition search?
3404     if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
3405         !cpi->use_svc) {
3406       cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
3407     }
3408 
3409     // The multiplication by 256 reverses a scaling factor of (>> 8)
3410     // applied when combining MB error values for the frame.
3411     twopass->mb_av_energy = log((this_frame.intra_error * 256.0) + 1.0);
3412     twopass->mb_smooth_pct = this_frame.intra_smooth_pct;
3413 
3414     return;
3415   }
3416 
3417   vpx_clear_system_state();
3418 
3419   if (cpi->oxcf.rc_mode == VPX_Q) {
3420     twopass->active_worst_quality = cpi->oxcf.cq_level;
3421   } else if (cm->current_video_frame == 0) {
3422     const int frames_left =
3423         (int)(twopass->total_stats.count - cm->current_video_frame);
3424     // Special case code for first frame.
3425     const int section_target_bandwidth =
3426         (int)(twopass->bits_left / frames_left);
3427     const double section_length = twopass->total_left_stats.count;
3428     const double section_error =
3429         twopass->total_left_stats.coded_error / section_length;
3430     const double section_intra_skip =
3431         twopass->total_left_stats.intra_skip_pct / section_length;
3432     const double section_inactive_zone =
3433         (twopass->total_left_stats.inactive_zone_rows * 2) /
3434         ((double)cm->mb_rows * section_length);
3435     const double section_noise =
3436         twopass->total_left_stats.frame_noise_energy / section_length;
3437     int tmp_q;
3438 
3439     tmp_q = get_twopass_worst_quality(
3440         cpi, section_error, section_intra_skip + section_inactive_zone,
3441         section_noise, section_target_bandwidth);
3442 
3443     twopass->active_worst_quality = tmp_q;
3444     twopass->baseline_active_worst_quality = tmp_q;
3445     rc->ni_av_qi = tmp_q;
3446     rc->last_q[INTER_FRAME] = tmp_q;
3447     rc->avg_q = vp9_convert_qindex_to_q(tmp_q, cm->bit_depth);
3448     rc->avg_frame_qindex[INTER_FRAME] = tmp_q;
3449     rc->last_q[KEY_FRAME] = (tmp_q + cpi->oxcf.best_allowed_q) / 2;
3450     rc->avg_frame_qindex[KEY_FRAME] = rc->last_q[KEY_FRAME];
3451   }
3452   vp9_zero(this_frame);
3453   if (EOF == input_stats(twopass, &this_frame)) return;
3454 
3455   // Set the frame content type flag.
3456   if (this_frame.intra_skip_pct >= FC_ANIMATION_THRESH)
3457     twopass->fr_content_type = FC_GRAPHICS_ANIMATION;
3458   else
3459     twopass->fr_content_type = FC_NORMAL;
3460 
3461   // Keyframe and section processing.
3462   if (rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY)) {
3463     // Define next KF group and assign bits to it.
3464     find_next_key_frame(cpi, show_idx);
3465   } else {
3466     cm->frame_type = INTER_FRAME;
3467   }
3468 
3469   // Define a new GF/ARF group. (Should always enter here for key frames).
3470   if (rc->frames_till_gf_update_due == 0) {
3471     define_gf_group(cpi, show_idx);
3472 
3473     rc->frames_till_gf_update_due = rc->baseline_gf_interval;
3474 
3475 #if ARF_STATS_OUTPUT
3476     {
3477       FILE *fpfile;
3478       fpfile = fopen("arf.stt", "a");
3479       ++arf_count;
3480       fprintf(fpfile, "%10d %10ld %10d %10d %10ld %10ld\n",
3481               cm->current_video_frame, rc->frames_till_gf_update_due,
3482               rc->kf_boost, arf_count, rc->gfu_boost, cm->frame_type);
3483 
3484       fclose(fpfile);
3485     }
3486 #endif
3487   }
3488 
3489   vp9_configure_buffer_updates(cpi, gf_group->index);
3490 
3491   // Do the firstpass stats indicate that this frame is skippable for the
3492   // partition search?
3493   if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2 &&
3494       !cpi->use_svc) {
3495     cpi->partition_search_skippable_frame = is_skippable_frame(cpi);
3496   }
3497 
3498   rc->base_frame_target = gf_group->bit_allocation[gf_group->index];
3499 
3500   // The multiplication by 256 reverses a scaling factor of (>> 8)
3501   // applied when combining MB error values for the frame.
3502   twopass->mb_av_energy = log((this_frame.intra_error * 256.0) + 1.0);
3503   twopass->mb_smooth_pct = this_frame.intra_smooth_pct;
3504 
3505   // Update the total stats remaining structure.
3506   subtract_stats(&twopass->total_left_stats, &this_frame);
3507 }
3508 
3509 #define MINQ_ADJ_LIMIT 48
3510 #define MINQ_ADJ_LIMIT_CQ 20
3511 #define HIGH_UNDERSHOOT_RATIO 2
vp9_twopass_postencode_update(VP9_COMP * cpi)3512 void vp9_twopass_postencode_update(VP9_COMP *cpi) {
3513   TWO_PASS *const twopass = &cpi->twopass;
3514   RATE_CONTROL *const rc = &cpi->rc;
3515   VP9_COMMON *const cm = &cpi->common;
3516   const int bits_used = rc->base_frame_target;
3517 
3518   // VBR correction is done through rc->vbr_bits_off_target. Based on the
3519   // sign of this value, a limited % adjustment is made to the target rate
3520   // of subsequent frames, to try and push it back towards 0. This method
3521   // is designed to prevent extreme behaviour at the end of a clip
3522   // or group of frames.
3523   rc->vbr_bits_off_target += rc->base_frame_target - rc->projected_frame_size;
3524   twopass->bits_left = VPXMAX(twopass->bits_left - bits_used, 0);
3525 
3526   // Target vs actual bits for this arf group.
3527   twopass->rolling_arf_group_target_bits += rc->this_frame_target;
3528   twopass->rolling_arf_group_actual_bits += rc->projected_frame_size;
3529 
3530   // Calculate the pct rc error.
3531   if (rc->total_actual_bits) {
3532     rc->rate_error_estimate =
3533         (int)((rc->vbr_bits_off_target * 100) / rc->total_actual_bits);
3534     rc->rate_error_estimate = clamp(rc->rate_error_estimate, -100, 100);
3535   } else {
3536     rc->rate_error_estimate = 0;
3537   }
3538 
3539   if (cpi->common.frame_type != KEY_FRAME) {
3540     twopass->kf_group_bits -= bits_used;
3541     twopass->last_kfgroup_zeromotion_pct = twopass->kf_zeromotion_pct;
3542   }
3543   twopass->kf_group_bits = VPXMAX(twopass->kf_group_bits, 0);
3544 
3545   // Increment the gf group index ready for the next frame.
3546   ++twopass->gf_group.index;
3547 
3548   // If the rate control is drifting consider adjustment to min or maxq.
3549   if ((cpi->oxcf.rc_mode != VPX_Q) && !cpi->rc.is_src_frame_alt_ref) {
3550     const int maxq_adj_limit =
3551         rc->worst_quality - twopass->active_worst_quality;
3552     const int minq_adj_limit =
3553         (cpi->oxcf.rc_mode == VPX_CQ ? MINQ_ADJ_LIMIT_CQ : MINQ_ADJ_LIMIT);
3554     int aq_extend_min = 0;
3555     int aq_extend_max = 0;
3556 
3557     // Extend min or Max Q range to account for imbalance from the base
3558     // value when using AQ.
3559     if (cpi->oxcf.aq_mode != NO_AQ && cpi->oxcf.aq_mode != PSNR_AQ &&
3560         cpi->oxcf.aq_mode != PERCEPTUAL_AQ) {
3561       if (cm->seg.aq_av_offset < 0) {
3562         // The balance of the AQ map tends towarda lowering the average Q.
3563         aq_extend_min = 0;
3564         aq_extend_max = VPXMIN(maxq_adj_limit, -cm->seg.aq_av_offset);
3565       } else {
3566         // The balance of the AQ map tends towards raising the average Q.
3567         aq_extend_min = VPXMIN(minq_adj_limit, cm->seg.aq_av_offset);
3568         aq_extend_max = 0;
3569       }
3570     }
3571 
3572     // Undershoot.
3573     if (rc->rate_error_estimate > cpi->oxcf.under_shoot_pct) {
3574       --twopass->extend_maxq;
3575       if (rc->rolling_target_bits >= rc->rolling_actual_bits)
3576         ++twopass->extend_minq;
3577       // Overshoot.
3578     } else if (rc->rate_error_estimate < -cpi->oxcf.over_shoot_pct) {
3579       --twopass->extend_minq;
3580       if (rc->rolling_target_bits < rc->rolling_actual_bits)
3581         ++twopass->extend_maxq;
3582     } else {
3583       // Adjustment for extreme local overshoot.
3584       if (rc->projected_frame_size > (2 * rc->base_frame_target) &&
3585           rc->projected_frame_size > (2 * rc->avg_frame_bandwidth))
3586         ++twopass->extend_maxq;
3587 
3588       // Unwind undershoot or overshoot adjustment.
3589       if (rc->rolling_target_bits < rc->rolling_actual_bits)
3590         --twopass->extend_minq;
3591       else if (rc->rolling_target_bits > rc->rolling_actual_bits)
3592         --twopass->extend_maxq;
3593     }
3594 
3595     twopass->extend_minq =
3596         clamp(twopass->extend_minq, aq_extend_min, minq_adj_limit);
3597     twopass->extend_maxq =
3598         clamp(twopass->extend_maxq, aq_extend_max, maxq_adj_limit);
3599 
3600     // If there is a big and undexpected undershoot then feed the extra
3601     // bits back in quickly. One situation where this may happen is if a
3602     // frame is unexpectedly almost perfectly predicted by the ARF or GF
3603     // but not very well predcited by the previous frame.
3604     if (!frame_is_kf_gf_arf(cpi) && !cpi->rc.is_src_frame_alt_ref) {
3605       int fast_extra_thresh = rc->base_frame_target / HIGH_UNDERSHOOT_RATIO;
3606       if (rc->projected_frame_size < fast_extra_thresh) {
3607         rc->vbr_bits_off_target_fast +=
3608             fast_extra_thresh - rc->projected_frame_size;
3609         rc->vbr_bits_off_target_fast =
3610             VPXMIN(rc->vbr_bits_off_target_fast, (4 * rc->avg_frame_bandwidth));
3611 
3612         // Fast adaptation of minQ if necessary to use up the extra bits.
3613         if (rc->avg_frame_bandwidth) {
3614           twopass->extend_minq_fast =
3615               (int)(rc->vbr_bits_off_target_fast * 8 / rc->avg_frame_bandwidth);
3616         }
3617         twopass->extend_minq_fast = VPXMIN(
3618             twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
3619       } else if (rc->vbr_bits_off_target_fast) {
3620         twopass->extend_minq_fast = VPXMIN(
3621             twopass->extend_minq_fast, minq_adj_limit - twopass->extend_minq);
3622       } else {
3623         twopass->extend_minq_fast = 0;
3624       }
3625     }
3626   }
3627 }
3628 
3629 #if CONFIG_RATE_CTRL
3630 // Under CONFIG_RATE_CTRL, once the first_pass_info is ready, the number of
3631 // coding frames (including show frame and alt ref) can be determined.
vp9_get_coding_frame_num(const struct VP9EncoderConfig * oxcf,const FRAME_INFO * frame_info,const FIRST_PASS_INFO * first_pass_info,int multi_layer_arf,int allow_alt_ref)3632 int vp9_get_coding_frame_num(const struct VP9EncoderConfig *oxcf,
3633                              const FRAME_INFO *frame_info,
3634                              const FIRST_PASS_INFO *first_pass_info,
3635                              int multi_layer_arf, int allow_alt_ref) {
3636   int coding_frame_num = 0;
3637   RATE_CONTROL rc;
3638   RANGE active_gf_interval;
3639   int arf_layers;
3640   double gop_intra_factor;
3641   int use_alt_ref;
3642   int gop_coding_frames;
3643   int gop_show_frames;
3644   int show_idx = 0;
3645   int arf_active_or_kf = 1;
3646   rc.static_scene_max_gf_interval = 250;
3647   vp9_rc_init(oxcf, 1, &rc);
3648 
3649   while (show_idx < first_pass_info->num_frames) {
3650     if (rc.frames_to_key == 0) {
3651       rc.frames_to_key = vp9_get_frames_to_next_key(
3652           oxcf, frame_info, first_pass_info, show_idx, rc.min_gf_interval);
3653       arf_active_or_kf = 1;
3654     } else {
3655     }
3656 
3657     {
3658       int dummy = 0;
3659       active_gf_interval = get_active_gf_inverval_range(
3660           frame_info, &rc, arf_active_or_kf, show_idx, dummy, dummy);
3661     }
3662 
3663     arf_layers = get_arf_layers(multi_layer_arf, oxcf->enable_auto_arf,
3664                                 active_gf_interval.max);
3665     if (multi_layer_arf) {
3666       gop_intra_factor = 1.0 + 0.25 * arf_layers;
3667     } else {
3668       gop_intra_factor = 1.0;
3669     }
3670 
3671     gop_coding_frames = get_gop_coding_frame_num(
3672         &use_alt_ref, frame_info, first_pass_info, &rc, show_idx,
3673         &active_gf_interval, gop_intra_factor, oxcf->lag_in_frames);
3674 
3675     use_alt_ref &= allow_alt_ref;
3676 
3677     rc.source_alt_ref_active = use_alt_ref;
3678     arf_active_or_kf = use_alt_ref;
3679     gop_show_frames = gop_coding_frames - use_alt_ref;
3680     rc.frames_to_key -= gop_show_frames;
3681     rc.frames_since_key += gop_show_frames;
3682     show_idx += gop_show_frames;
3683     coding_frame_num += gop_show_frames + use_alt_ref;
3684   }
3685   return coding_frame_num;
3686 }
3687 #endif
3688 
vp9_get_frame_stats(const TWO_PASS * twopass)3689 FIRSTPASS_STATS vp9_get_frame_stats(const TWO_PASS *twopass) {
3690   return twopass->this_frame_stats;
3691 }
vp9_get_total_stats(const TWO_PASS * twopass)3692 FIRSTPASS_STATS vp9_get_total_stats(const TWO_PASS *twopass) {
3693   return twopass->total_stats;
3694 }
3695