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