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