Coverage Report

Created: 2024-09-06 07:53

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