aom_temporal_svc_encoder.c 31.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3
 *
4
5
6
7
8
9
 * This source code is subject to the terms of the BSD 2 Clause License and
 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
 * was not distributed with this source code in the LICENSE file, you can
 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
 * Media Patent License 1.0 was not distributed with this source code in the
 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10
11
 */

12
//  This is an example demonstrating how to implement a multi-layer VPx
13
14
15
//  encoding scheme based on temporal scalability for video applications
//  that benefit from a scalable bitstream.

16
#include <assert.h>
17
#include <math.h>
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Adrian Grange's avatar
Adrian Grange committed
22
23
#include "./aom_config.h"
#include "../aom_ports/aom_timer.h"
Adrian Grange's avatar
Adrian Grange committed
24
#include "aom/aomcx.h"
Adrian Grange's avatar
Adrian Grange committed
25
#include "aom/aom_encoder.h"
26

Tom Finegan's avatar
Tom Finegan committed
27
28
#include "../tools_common.h"
#include "../video_writer.h"
29

30
31
static const char *exec_name;

32
void usage_exit(void) { exit(EXIT_FAILURE); }
33

34
35
36
37
38
// Denoiser states, for temporal denoising.
enum denoiserState {
  kDenoiserOff,
  kDenoiserOnYOnly,
  kDenoiserOnYUV,
39
40
  kDenoiserOnYUVAggressive,
  kDenoiserOnAdaptive
41
42
};

43
static int mode_to_num_layers[12] = { 1, 2, 2, 3, 3, 3, 3, 5, 2, 3, 3, 3 };
44

45
46
47
// For rate control encoding stats.
struct RateControlMetrics {
  // Number of input frames per layer.
Adrian Grange's avatar
Adrian Grange committed
48
  int layer_input_frames[AOM_TS_MAX_LAYERS];
49
  // Total (cumulative) number of encoded frames per layer.
Adrian Grange's avatar
Adrian Grange committed
50
  int layer_tot_enc_frames[AOM_TS_MAX_LAYERS];
51
  // Number of encoded non-key frames per layer.
Adrian Grange's avatar
Adrian Grange committed
52
  int layer_enc_frames[AOM_TS_MAX_LAYERS];
53
  // Framerate per layer layer (cumulative).
Adrian Grange's avatar
Adrian Grange committed
54
  double layer_framerate[AOM_TS_MAX_LAYERS];
55
  // Target average frame size per layer (per-frame-bandwidth per layer).
Adrian Grange's avatar
Adrian Grange committed
56
  double layer_pfb[AOM_TS_MAX_LAYERS];
57
  // Actual average frame size per layer.
Adrian Grange's avatar
Adrian Grange committed
58
  double layer_avg_frame_size[AOM_TS_MAX_LAYERS];
59
  // Average rate mismatch per layer (|target - actual| / target).
Adrian Grange's avatar
Adrian Grange committed
60
  double layer_avg_rate_mismatch[AOM_TS_MAX_LAYERS];
61
  // Actual encoding bitrate per layer (cumulative).
Adrian Grange's avatar
Adrian Grange committed
62
  double layer_encoding_bitrate[AOM_TS_MAX_LAYERS];
63
64
65
66
67
68
69
70
71
  // Average of the short-time encoder actual bitrate.
  // TODO(marpan): Should we add these short-time stats for each layer?
  double avg_st_encoding_bitrate;
  // Variance of the short-time encoder actual bitrate.
  double variance_st_encoding_bitrate;
  // Window (number of frames) for computing short-timee encoding bitrate.
  int window_size;
  // Number of window measurements.
  int window_count;
Adrian Grange's avatar
Adrian Grange committed
72
  int layer_target_bitrate[AOM_MAX_LAYERS];
73
74
};

75
76
77
78
79
80
// Note: these rate control metrics assume only 1 key frame in the
// sequence (i.e., first frame only). So for temporal pattern# 7
// (which has key frame for every frame on base layer), the metrics
// computation will be off/wrong.
// TODO(marpan): Update these metrics to account for multiple key frames
// in the stream.
81
static void set_rate_control_metrics(struct RateControlMetrics *rc,
Adrian Grange's avatar
Adrian Grange committed
82
                                     aom_codec_enc_cfg_t *cfg) {
83
  unsigned int i = 0;
84
85
  // Set the layer (cumulative) framerate and the target layer (non-cumulative)
  // per-frame-bandwidth, for the rate control encoding stats below.
86
  const double framerate = cfg->g_timebase.den / cfg->g_timebase.num;
87
  rc->layer_framerate[0] = framerate / cfg->ts_rate_decimator[0];
88
89
  rc->layer_pfb[0] =
      1000.0 * rc->layer_target_bitrate[0] / rc->layer_framerate[0];
90
91
92
  for (i = 0; i < cfg->ts_number_layers; ++i) {
    if (i > 0) {
      rc->layer_framerate[i] = framerate / cfg->ts_rate_decimator[i];
93
94
95
      rc->layer_pfb[i] = 1000.0 * (rc->layer_target_bitrate[i] -
                                   rc->layer_target_bitrate[i - 1]) /
                         (rc->layer_framerate[i] - rc->layer_framerate[i - 1]);
96
97
98
99
100
101
102
103
    }
    rc->layer_input_frames[i] = 0;
    rc->layer_enc_frames[i] = 0;
    rc->layer_tot_enc_frames[i] = 0;
    rc->layer_encoding_bitrate[i] = 0.0;
    rc->layer_avg_frame_size[i] = 0.0;
    rc->layer_avg_rate_mismatch[i] = 0.0;
  }
104
105
106
107
  rc->window_count = 0;
  rc->window_size = 15;
  rc->avg_st_encoding_bitrate = 0.0;
  rc->variance_st_encoding_bitrate = 0.0;
108
109
110
}

static void printout_rate_control_summary(struct RateControlMetrics *rc,
Adrian Grange's avatar
Adrian Grange committed
111
                                          aom_codec_enc_cfg_t *cfg,
112
                                          int frame_cnt) {
113
  unsigned int i = 0;
114
  int tot_num_frames = 0;
115
  double perc_fluctuation = 0.0;
116
  printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
117
  printf("Rate control layer stats for %d layer(s):\n\n",
118
         cfg->ts_number_layers);
119
  for (i = 0; i < cfg->ts_number_layers; ++i) {
120
121
122
    const int num_dropped =
        (i > 0) ? (rc->layer_input_frames[i] - rc->layer_enc_frames[i])
                : (rc->layer_input_frames[i] - rc->layer_enc_frames[i] - 1);
123
    tot_num_frames += rc->layer_input_frames[i];
124
    rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[i] *
125
126
127
128
129
130
                                    rc->layer_encoding_bitrate[i] /
                                    tot_num_frames;
    rc->layer_avg_frame_size[i] =
        rc->layer_avg_frame_size[i] / rc->layer_enc_frames[i];
    rc->layer_avg_rate_mismatch[i] =
        100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[i];
131
    printf("For layer#: %d \n", i);
132
    printf("Bitrate (target vs actual): %d %f \n", rc->layer_target_bitrate[i],
133
134
135
136
           rc->layer_encoding_bitrate[i]);
    printf("Average frame size (target vs actual): %f %f \n", rc->layer_pfb[i],
           rc->layer_avg_frame_size[i]);
    printf("Average rate_mismatch: %f \n", rc->layer_avg_rate_mismatch[i]);
137
138
139
140
    printf(
        "Number of input frames, encoded (non-key) frames, "
        "and perc dropped frames: %d %d %f \n",
        rc->layer_input_frames[i], rc->layer_enc_frames[i],
141
142
143
        100.0 * num_dropped / rc->layer_input_frames[i]);
    printf("\n");
  }
144
145
146
147
148
  rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
  rc->variance_st_encoding_bitrate =
      rc->variance_st_encoding_bitrate / rc->window_count -
      (rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
  perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
149
150
                     rc->avg_st_encoding_bitrate;
  printf("Short-time stats, for window of %d frames: \n", rc->window_size);
151
  printf("Average, rms-variance, and percent-fluct: %f %f %f \n",
152
         rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
153
         perc_fluctuation);
154
  if ((frame_cnt - 1) != tot_num_frames)
155
156
157
    die("Error: Number of input frames not equal to output! \n");
}

158
159
160
161
162
163
// Temporal scaling parameters:
// NOTE: The 3 prediction frames cannot be used interchangeably due to
// differences in the way they are handled throughout the code. The
// frames should be allocated to layers in the order LAST, GF, ARF.
// Other combinations work, but may produce slightly inferior results.
static void set_temporal_layer_pattern(int layering_mode,
Adrian Grange's avatar
Adrian Grange committed
164
                                       aom_codec_enc_cfg_t *cfg,
165
166
167
168
169
                                       int *layer_flags,
                                       int *flag_periodicity) {
  switch (layering_mode) {
    case 0: {
      // 1-layer.
170
      int ids[1] = { 0 };
171
172
173
174
175
176
      cfg->ts_periodicity = 1;
      *flag_periodicity = 1;
      cfg->ts_number_layers = 1;
      cfg->ts_rate_decimator[0] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // Update L only.
177
      layer_flags[0] =
Adrian Grange's avatar
Adrian Grange committed
178
          AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF;
179
180
181
182
      break;
    }
    case 1: {
      // 2-layers, 2-frame period.
183
      int ids[2] = { 0, 1 };
184
185
186
187
188
189
190
191
      cfg->ts_periodicity = 2;
      *flag_periodicity = 2;
      cfg->ts_number_layers = 2;
      cfg->ts_rate_decimator[0] = 2;
      cfg->ts_rate_decimator[1] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
#if 1
      // 0=L, 1=GF, Intra-layer prediction enabled.
Adrian Grange's avatar
Adrian Grange committed
192
193
194
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF;
195
      layer_flags[1] =
Adrian Grange's avatar
Adrian Grange committed
196
          AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_REF_ARF;
197
#else
198
      // 0=L, 1=GF, Intra-layer prediction disabled.
Adrian Grange's avatar
Adrian Grange committed
199
200
201
202
203
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF;
      layer_flags[1] = AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST |
                       AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_LAST;
204
205
206
207
208
#endif
      break;
    }
    case 2: {
      // 2-layers, 3-frame period.
209
      int ids[3] = { 0, 1, 1 };
210
211
212
213
214
215
216
      cfg->ts_periodicity = 3;
      *flag_periodicity = 3;
      cfg->ts_number_layers = 2;
      cfg->ts_rate_decimator[0] = 3;
      cfg->ts_rate_decimator[1] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, Intra-layer prediction enabled.
Adrian Grange's avatar
Adrian Grange committed
217
218
219
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF;
220
      layer_flags[1] = layer_flags[2] =
Adrian Grange's avatar
Adrian Grange committed
221
222
          AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_ARF |
          AOM_EFLAG_NO_UPD_LAST;
223
224
225
226
      break;
    }
    case 3: {
      // 3-layers, 6-frame period.
227
      int ids[6] = { 0, 2, 2, 1, 2, 2 };
228
229
230
231
232
233
234
235
      cfg->ts_periodicity = 6;
      *flag_periodicity = 6;
      cfg->ts_number_layers = 3;
      cfg->ts_rate_decimator[0] = 6;
      cfg->ts_rate_decimator[1] = 3;
      cfg->ts_rate_decimator[2] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
Adrian Grange's avatar
Adrian Grange committed
236
237
238
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF;
239
      layer_flags[3] =
Adrian Grange's avatar
Adrian Grange committed
240
          AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST;
241
      layer_flags[1] = layer_flags[2] = layer_flags[4] = layer_flags[5] =
Adrian Grange's avatar
Adrian Grange committed
242
          AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_LAST;
243
244
245
246
      break;
    }
    case 4: {
      // 3-layers, 4-frame period.
247
      int ids[4] = { 0, 2, 1, 2 };
248
249
250
251
252
253
254
255
      cfg->ts_periodicity = 4;
      *flag_periodicity = 4;
      cfg->ts_number_layers = 3;
      cfg->ts_rate_decimator[0] = 4;
      cfg->ts_rate_decimator[1] = 2;
      cfg->ts_rate_decimator[2] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled.
Adrian Grange's avatar
Adrian Grange committed
256
257
258
259
260
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF;
      layer_flags[2] = AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
                       AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST;
261
      layer_flags[1] = layer_flags[3] =
Adrian Grange's avatar
Adrian Grange committed
262
263
          AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
          AOM_EFLAG_NO_UPD_ARF;
264
265
266
267
      break;
    }
    case 5: {
      // 3-layers, 4-frame period.
268
      int ids[4] = { 0, 2, 1, 2 };
269
270
      cfg->ts_periodicity = 4;
      *flag_periodicity = 4;
271
      cfg->ts_number_layers = 3;
272
273
274
275
276
277
      cfg->ts_rate_decimator[0] = 4;
      cfg->ts_rate_decimator[1] = 2;
      cfg->ts_rate_decimator[2] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled in layer 1, disabled
      // in layer 2.
Adrian Grange's avatar
Adrian Grange committed
278
279
280
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF;
281
      layer_flags[2] =
Adrian Grange's avatar
Adrian Grange committed
282
          AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_ARF;
283
      layer_flags[1] = layer_flags[3] =
Adrian Grange's avatar
Adrian Grange committed
284
285
          AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
          AOM_EFLAG_NO_UPD_ARF;
286
287
288
289
      break;
    }
    case 6: {
      // 3-layers, 4-frame period.
290
      int ids[4] = { 0, 2, 1, 2 };
291
292
293
294
295
296
297
298
      cfg->ts_periodicity = 4;
      *flag_periodicity = 4;
      cfg->ts_number_layers = 3;
      cfg->ts_rate_decimator[0] = 4;
      cfg->ts_rate_decimator[1] = 2;
      cfg->ts_rate_decimator[2] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, 2=ARF, Intra-layer prediction enabled.
Adrian Grange's avatar
Adrian Grange committed
299
300
301
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF;
302
      layer_flags[2] =
Adrian Grange's avatar
Adrian Grange committed
303
          AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_ARF;
304
      layer_flags[1] = layer_flags[3] =
Adrian Grange's avatar
Adrian Grange committed
305
          AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF;
306
307
308
309
310
      break;
    }
    case 7: {
      // NOTE: Probably of academic interest only.
      // 5-layers, 16-frame period.
311
      int ids[16] = { 0, 4, 3, 4, 2, 4, 3, 4, 1, 4, 3, 4, 2, 4, 3, 4 };
312
313
314
315
316
317
318
319
320
      cfg->ts_periodicity = 16;
      *flag_periodicity = 16;
      cfg->ts_number_layers = 5;
      cfg->ts_rate_decimator[0] = 16;
      cfg->ts_rate_decimator[1] = 8;
      cfg->ts_rate_decimator[2] = 4;
      cfg->ts_rate_decimator[3] = 2;
      cfg->ts_rate_decimator[4] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
Adrian Grange's avatar
Adrian Grange committed
321
      layer_flags[0] = AOM_EFLAG_FORCE_KF;
322
323
      layer_flags[1] = layer_flags[3] = layer_flags[5] = layer_flags[7] =
          layer_flags[9] = layer_flags[11] = layer_flags[13] = layer_flags[15] =
Adrian Grange's avatar
Adrian Grange committed
324
325
              AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
              AOM_EFLAG_NO_UPD_ARF;
326
      layer_flags[2] = layer_flags[6] = layer_flags[10] = layer_flags[14] =
Adrian Grange's avatar
Adrian Grange committed
327
          AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_GF;
328
      layer_flags[4] = layer_flags[12] =
Adrian Grange's avatar
Adrian Grange committed
329
330
          AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_UPD_ARF;
      layer_flags[8] = AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF;
331
332
333
334
      break;
    }
    case 8: {
      // 2-layers, with sync point at first frame of layer 1.
335
      int ids[2] = { 0, 1 };
336
337
338
339
340
341
342
343
344
345
346
      cfg->ts_periodicity = 2;
      *flag_periodicity = 8;
      cfg->ts_number_layers = 2;
      cfg->ts_rate_decimator[0] = 2;
      cfg->ts_rate_decimator[1] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF.
      // ARF is used as predictor for all frames, and is only updated on
      // key frame. Sync point every 8 frames.

      // Layer 0: predict from L and ARF, update L and G.
347
      layer_flags[0] =
Adrian Grange's avatar
Adrian Grange committed
348
          AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_UPD_ARF;
349
      // Layer 1: sync point: predict from L and ARF, and update G.
350
      layer_flags[1] =
Adrian Grange's avatar
Adrian Grange committed
351
          AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_ARF;
352
      // Layer 0, predict from L and ARF, update L.
353
      layer_flags[2] =
Adrian Grange's avatar
Adrian Grange committed
354
          AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF;
355
      // Layer 1: predict from L, G and ARF, and update G.
Adrian Grange's avatar
Adrian Grange committed
356
357
      layer_flags[3] = AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST |
                       AOM_EFLAG_NO_UPD_ENTROPY;
358
359
360
361
362
363
364
365
      // Layer 0.
      layer_flags[4] = layer_flags[2];
      // Layer 1.
      layer_flags[5] = layer_flags[3];
      // Layer 0.
      layer_flags[6] = layer_flags[4];
      // Layer 1.
      layer_flags[7] = layer_flags[5];
366
      break;
367
368
369
    }
    case 9: {
      // 3-layers: Sync points for layer 1 and 2 every 8 frames.
370
      int ids[4] = { 0, 2, 1, 2 };
371
372
373
374
375
376
377
378
      cfg->ts_periodicity = 4;
      *flag_periodicity = 8;
      cfg->ts_number_layers = 3;
      cfg->ts_rate_decimator[0] = 4;
      cfg->ts_rate_decimator[1] = 2;
      cfg->ts_rate_decimator[2] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, 2=ARF.
Adrian Grange's avatar
Adrian Grange committed
379
380
381
382
383
384
385
      layer_flags[0] = AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_REF_GF |
                       AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF;
      layer_flags[1] = AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
                       AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF;
      layer_flags[2] = AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
                       AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_ARF;
386
      layer_flags[3] = layer_flags[5] =
Adrian Grange's avatar
Adrian Grange committed
387
388
389
          AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF;
      layer_flags[4] = AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF |
                       AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF;
390
      layer_flags[6] =
Adrian Grange's avatar
Adrian Grange committed
391
392
393
          AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_ARF;
      layer_flags[7] = AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_ENTROPY;
394
395
396
397
398
399
400
      break;
    }
    case 10: {
      // 3-layers structure where ARF is used as predictor for all frames,
      // and is only updated on key frame.
      // Sync points for layer 1 and 2 every 8 frames.

401
      int ids[4] = { 0, 2, 1, 2 };
402
403
404
405
406
407
408
409
410
      cfg->ts_periodicity = 4;
      *flag_periodicity = 8;
      cfg->ts_number_layers = 3;
      cfg->ts_rate_decimator[0] = 4;
      cfg->ts_rate_decimator[1] = 2;
      cfg->ts_rate_decimator[2] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, 2=ARF.
      // Layer 0: predict from L and ARF; update L and G.
411
      layer_flags[0] =
Adrian Grange's avatar
Adrian Grange committed
412
          AOM_EFLAG_FORCE_KF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_REF_GF;
413
      // Layer 2: sync point: predict from L and ARF; update none.
Adrian Grange's avatar
Adrian Grange committed
414
415
416
      layer_flags[1] = AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_UPD_GF |
                       AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST |
                       AOM_EFLAG_NO_UPD_ENTROPY;
417
      // Layer 1: sync point: predict from L and ARF; update G.
418
      layer_flags[2] =
Adrian Grange's avatar
Adrian Grange committed
419
          AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST;
420
      // Layer 2: predict from L, G, ARF; update none.
Adrian Grange's avatar
Adrian Grange committed
421
422
      layer_flags[3] = AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
                       AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_ENTROPY;
423
      // Layer 0: predict from L and ARF; update L.
424
      layer_flags[4] =
Adrian Grange's avatar
Adrian Grange committed
425
          AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_REF_GF;
426
427
428
      // Layer 2: predict from L, G, ARF; update none.
      layer_flags[5] = layer_flags[3];
      // Layer 1: predict from L, G, ARF; update G.
Adrian Grange's avatar
Adrian Grange committed
429
      layer_flags[6] = AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST;
430
431
432
433
434
435
436
437
      // Layer 2: predict from L, G, ARF; update none.
      layer_flags[7] = layer_flags[3];
      break;
    }
    case 11:
    default: {
      // 3-layers structure as in case 10, but no sync/refresh points for
      // layer 1 and 2.
438
      int ids[4] = { 0, 2, 1, 2 };
439
440
441
442
443
444
445
446
447
      cfg->ts_periodicity = 4;
      *flag_periodicity = 8;
      cfg->ts_number_layers = 3;
      cfg->ts_rate_decimator[0] = 4;
      cfg->ts_rate_decimator[1] = 2;
      cfg->ts_rate_decimator[2] = 1;
      memcpy(cfg->ts_layer_id, ids, sizeof(ids));
      // 0=L, 1=GF, 2=ARF.
      // Layer 0: predict from L and ARF; update L.
448
      layer_flags[0] =
Adrian Grange's avatar
Adrian Grange committed
449
          AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_REF_GF;
450
451
      layer_flags[4] = layer_flags[0];
      // Layer 1: predict from L, G, ARF; update G.
Adrian Grange's avatar
Adrian Grange committed
452
      layer_flags[2] = AOM_EFLAG_NO_UPD_ARF | AOM_EFLAG_NO_UPD_LAST;
453
454
      layer_flags[6] = layer_flags[2];
      // Layer 2: predict from L, G, ARF; update none.
Adrian Grange's avatar
Adrian Grange committed
455
456
      layer_flags[1] = AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
                       AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_ENTROPY;
457
458
459
460
461
462
463
464
465
      layer_flags[3] = layer_flags[1];
      layer_flags[5] = layer_flags[1];
      layer_flags[7] = layer_flags[1];
      break;
    }
  }
}

int main(int argc, char **argv) {
Adrian Grange's avatar
Adrian Grange committed
466
  VpxVideoWriter *outfile[AOM_TS_MAX_LAYERS] = { NULL };
Adrian Grange's avatar
Adrian Grange committed
467
468
  aom_codec_ctx_t codec;
  aom_codec_enc_cfg_t cfg;
469
  int frame_cnt = 0;
Adrian Grange's avatar
Adrian Grange committed
470
471
  aom_image_t raw;
  aom_codec_err_t res;
472
473
  unsigned int width;
  unsigned int height;
474
  int speed;
475
476
477
  int frame_avail;
  int got_data;
  int flags = 0;
478
  unsigned int i;
479
  int pts = 0;             // PTS starts at 0.
480
481
  int frame_duration = 1;  // 1 timebase tick per frame.
  int layering_mode = 0;
Adrian Grange's avatar
Adrian Grange committed
482
  int layer_flags[AOM_TS_MAX_PERIODICITY] = { 0 };
483
  int flag_periodicity = 1;
Adrian Grange's avatar
Adrian Grange committed
484
#if AOM_ENCODER_ABI_VERSION > (4 + AOM_CODEC_ABI_VERSION)
Adrian Grange's avatar
Adrian Grange committed
485
  aom_svc_layer_id_t layer_id = { 0, 0 };
486
#else
Adrian Grange's avatar
Adrian Grange committed
487
  aom_svc_layer_id_t layer_id = { 0 };
488
#endif
489
  const VpxInterface *encoder = NULL;
490
  FILE *infile = NULL;
491
  struct RateControlMetrics rc;
492
  int64_t cx_time = 0;
493
  const int min_args_base = 11;
494
#if CONFIG_AOM_HIGHBITDEPTH
Adrian Grange's avatar
Adrian Grange committed
495
  aom_bit_depth_t bit_depth = AOM_BITS_8;
496
497
498
499
  int input_bit_depth = 8;
  const int min_args = min_args_base + 1;
#else
  const int min_args = min_args_base;
500
#endif  // CONFIG_AOM_HIGHBITDEPTH
501
502
  double sum_bitrate = 0.0;
  double sum_bitrate2 = 0.0;
503
  double framerate = 30.0;
504
505
506

  exec_name = argv[0];
  // Check usage and arguments.
507
  if (argc < min_args) {
508
#if CONFIG_AOM_HIGHBITDEPTH
509
    die(
Adrian Grange's avatar
Adrian Grange committed
510
        "Usage: %s <infile> <outfile> <codec_type(aom/av1)> <width> <height> "
511
        "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
512
513
        "<Rate_0> ... <Rate_nlayers-1> <bit-depth> \n",
        argv[0]);
514
#else
515
    die(
Adrian Grange's avatar
Adrian Grange committed
516
        "Usage: %s <infile> <outfile> <codec_type(aom/av1)> <width> <height> "
517
        "<rate_num> <rate_den> <speed> <frame_drop_threshold> <mode> "
518
519
        "<Rate_0> ... <Rate_nlayers-1> \n",
        argv[0]);
520
#endif  // CONFIG_AOM_HIGHBITDEPTH
521
522
  }

Adrian Grange's avatar
Adrian Grange committed
523
  encoder = get_aom_encoder_by_name(argv[3]);
524
  if (!encoder) die("Unsupported codec.");
525

Adrian Grange's avatar
Adrian Grange committed
526
  printf("Using %s\n", aom_codec_iface_name(encoder->codec_interface()));
527
528
529
530
531
532
533

  width = strtol(argv[4], NULL, 0);
  height = strtol(argv[5], NULL, 0);
  if (width < 16 || width % 2 || height < 16 || height % 2) {
    die("Invalid resolution: %d x %d", width, height);
  }

534
  layering_mode = strtol(argv[10], NULL, 0);
535
  if (layering_mode < 0 || layering_mode > 12) {
536
    die("Invalid layering mode (0..12) %s", argv[10]);
537
538
  }

539
  if (argc != min_args + mode_to_num_layers[layering_mode]) {
540
541
542
    die("Invalid number of arguments");
  }

543
#if CONFIG_AOM_HIGHBITDEPTH
544
  switch (strtol(argv[argc - 1], NULL, 0)) {
545
    case 8:
Adrian Grange's avatar
Adrian Grange committed
546
      bit_depth = AOM_BITS_8;
547
548
549
      input_bit_depth = 8;
      break;
    case 10:
Adrian Grange's avatar
Adrian Grange committed
550
      bit_depth = AOM_BITS_10;
551
552
553
      input_bit_depth = 10;
      break;
    case 12:
Adrian Grange's avatar
Adrian Grange committed
554
      bit_depth = AOM_BITS_12;
555
556
      input_bit_depth = 12;
      break;
557
    default: die("Invalid bit depth (8, 10, 12) %s", argv[argc - 1]);
558
  }
Adrian Grange's avatar
Adrian Grange committed
559
  if (!aom_img_alloc(
Adrian Grange's avatar
Adrian Grange committed
560
          &raw, bit_depth == AOM_BITS_8 ? AOM_IMG_FMT_I420 : AOM_IMG_FMT_I42016,
561
          width, height, 32)) {
562
563
564
    die("Failed to allocate image", width, height);
  }
#else
Adrian Grange's avatar
Adrian Grange committed
565
  if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, width, height, 32)) {
566
567
    die("Failed to allocate image", width, height);
  }
568
#endif  // CONFIG_AOM_HIGHBITDEPTH
569
570

  // Populate encoder configuration.
Adrian Grange's avatar
Adrian Grange committed
571
  res = aom_codec_enc_config_default(encoder->codec_interface(), &cfg, 0);
572
  if (res) {
Adrian Grange's avatar
Adrian Grange committed
573
    printf("Failed to get config: %s\n", aom_codec_err_to_string(res));
574
575
576
577
578
579
580
    return EXIT_FAILURE;
  }

  // Update the default configuration with our settings.
  cfg.g_w = width;
  cfg.g_h = height;

581
#if CONFIG_AOM_HIGHBITDEPTH
Adrian Grange's avatar
Adrian Grange committed
582
  if (bit_depth != AOM_BITS_8) {
583
584
585
586
    cfg.g_bit_depth = bit_depth;
    cfg.g_input_bit_depth = input_bit_depth;
    cfg.g_profile = 2;
  }
587
#endif  // CONFIG_AOM_HIGHBITDEPTH
588

589
590
591
592
  // Timebase format e.g. 30fps: numerator=1, demoninator = 30.
  cfg.g_timebase.num = strtol(argv[6], NULL, 0);
  cfg.g_timebase.den = strtol(argv[7], NULL, 0);

593
594
595
596
597
  speed = strtol(argv[8], NULL, 0);
  if (speed < 0) {
    die("Invalid speed setting: must be positive");
  }

598
  for (i = min_args_base;
599
       (int)i < min_args_base + mode_to_num_layers[layering_mode]; ++i) {
600
    rc.layer_target_bitrate[i - 11] = strtol(argv[i], NULL, 0);
Adrian Grange's avatar
Adrian Grange committed
601
    if (strncmp(encoder->name, "aom", 3) == 0)
602
      cfg.ts_target_bitrate[i - 11] = rc.layer_target_bitrate[i - 11];
Adrian Grange's avatar
Adrian Grange committed
603
    else if (strncmp(encoder->name, "av1", 3) == 0)
604
      cfg.layer_target_bitrate[i - 11] = rc.layer_target_bitrate[i - 11];
605
606
607
  }

  // Real time parameters.
608
  cfg.rc_dropframe_thresh = strtol(argv[9], NULL, 0);
Adrian Grange's avatar
Adrian Grange committed
609
  cfg.rc_end_usage = AOM_CBR;
610
611
  cfg.rc_min_quantizer = 2;
  cfg.rc_max_quantizer = 56;
Adrian Grange's avatar
Adrian Grange committed
612
  if (strncmp(encoder->name, "av1", 3) == 0) cfg.rc_max_quantizer = 52;
613
614
  cfg.rc_undershoot_pct = 50;
  cfg.rc_overshoot_pct = 50;
615
616
617
618
  cfg.rc_buf_initial_sz = 500;
  cfg.rc_buf_optimal_sz = 600;
  cfg.rc_buf_sz = 1000;

619
620
621
  // Disable dynamic resizing by default.
  cfg.rc_resize_allowed = 0;

622
623
624
  // Use 1 thread as default.
  cfg.g_threads = 1;

625
626
  // Enable error resilient mode.
  cfg.g_error_resilient = 1;
627
  cfg.g_lag_in_frames = 0;
Adrian Grange's avatar
Adrian Grange committed
628
  cfg.kf_mode = AOM_KF_AUTO;
629
630
631
632

  // Disable automatic keyframe placement.
  cfg.kf_min_dist = cfg.kf_max_dist = 3000;

Adrian Grange's avatar
Adrian Grange committed
633
  cfg.temporal_layering_mode = AV1E_TEMPORAL_LAYERING_MODE_BYPASS;
634

635
  set_temporal_layer_pattern(layering_mode, &cfg, layer_flags,
636
637
                             &flag_periodicity);

638
639
  set_rate_control_metrics(&rc, &cfg);

640
  // Target bandwidth for the whole stream.
641
  // Set to layer_target_bitrate for highest layer (total bitrate).
642
  cfg.rc_target_bitrate = rc.layer_target_bitrate[cfg.ts_number_layers - 1];
643

644
  // Open input file.
645
  if (!(infile = fopen(argv[1], "rb"))) {
646
647
648
    die("Failed to open %s for reading", argv[1]);
  }

649
  framerate = cfg.g_timebase.den / cfg.g_timebase.num;
650
651
  // Open an output file for each stream.
  for (i = 0; i < cfg.ts_number_layers; ++i) {
652
653
    char file_name[PATH_MAX];
    VpxVideoInfo info;
654
    info.codec_fourcc = encoder->fourcc;
655
656
657
658
659
    info.frame_width = cfg.g_w;
    info.frame_height = cfg.g_h;
    info.time_base.numerator = cfg.g_timebase.num;
    info.time_base.denominator = cfg.g_timebase.den;

660
    snprintf(file_name, sizeof(file_name), "%s_%d.ivf", argv[2], i);
Adrian Grange's avatar
Adrian Grange committed
661
    outfile[i] = aom_video_writer_open(file_name, kContainerIVF, &info);
662
    if (!outfile[i]) die("Failed to open %s for writing", file_name);
663
664

    assert(outfile[i] != NULL);
665
666
667
668
  }
  // No spatial layers in this encoder.
  cfg.ss_number_layers = 1;

669
// Initialize codec.
670
#if CONFIG_AOM_HIGHBITDEPTH
Adrian Grange's avatar
Adrian Grange committed
671
  if (aom_codec_enc_init(
672
          &codec, encoder->codec_interface(), &cfg,
Adrian Grange's avatar
Adrian Grange committed
673
          bit_depth == AOM_BITS_8 ? 0 : AOM_CODEC_USE_HIGHBITDEPTH))
674
#else
Adrian Grange's avatar
Adrian Grange committed
675
  if (aom_codec_enc_init(&codec, encoder->codec_interface(), &cfg, 0))
676
#endif  // CONFIG_AOM_HIGHBITDEPTH
677
678
    die_codec(&codec, "Failed to initialize encoder");

Adrian Grange's avatar
Adrian Grange committed
679
  if (strncmp(encoder->name, "aom", 3) == 0) {
Adrian Grange's avatar
Adrian Grange committed
680
681
682
    aom_codec_control(&codec, AOME_SET_CPUUSED, -speed);
    aom_codec_control(&codec, AOME_SET_NOISE_SENSITIVITY, kDenoiserOff);
    aom_codec_control(&codec, AOME_SET_STATIC_THRESHOLD, 1);
Adrian Grange's avatar
Adrian Grange committed
683
  } else if (strncmp(encoder->name, "av1", 3) == 0) {
Adrian Grange's avatar
Adrian Grange committed
684
    aom_svc_extra_cfg_t svc_params;
Adrian Grange's avatar
Adrian Grange committed
685
    aom_codec_control(&codec, AOME_SET_CPUUSED, speed);
Adrian Grange's avatar
Adrian Grange committed
686
687
688
    aom_codec_control(&codec, AV1E_SET_AQ_MODE, 3);
    aom_codec_control(&codec, AV1E_SET_FRAME_PERIODIC_BOOST, 0);
    aom_codec_control(&codec, AV1E_SET_NOISE_SENSITIVITY, 0);
Adrian Grange's avatar
Adrian Grange committed
689
    aom_codec_control(&codec, AOME_SET_STATIC_THRESHOLD, 1);
Adrian Grange's avatar
Adrian Grange committed
690
691
692
    aom_codec_control(&codec, AV1E_SET_TUNE_CONTENT, 0);
    aom_codec_control(&codec, AV1E_SET_TILE_COLUMNS, (cfg.g_threads >> 1));
    if (aom_codec_control(&codec, AV1E_SET_SVC, layering_mode > 0 ? 1 : 0))
Marco's avatar
Marco committed
693
694
695
696
      die_codec(&codec, "Failed to set SVC");
    for (i = 0; i < cfg.ts_number_layers; ++i) {
      svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
      svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
697
    }
Marco's avatar
Marco committed
698
699
    svc_params.scaling_factor_num[0] = cfg.g_h;
    svc_params.scaling_factor_den[0] = cfg.g_h;
Adrian Grange's avatar
Adrian Grange committed
700
    aom_codec_control(&codec, AV1E_SET_SVC_PARAMETERS, &svc_params);
701
  }
Adrian Grange's avatar
Adrian Grange committed
702
  if (strncmp(encoder->name, "aom", 3) == 0) {
Adrian Grange's avatar
Adrian Grange committed
703
    aom_codec_control(&codec, AOME_SET_SCREEN_CONTENT_MODE, 0);
Marco's avatar
Marco committed
704
  }
Adrian Grange's avatar
Adrian Grange committed
705
  aom_codec_control(&codec, AOME_SET_TOKEN_PARTITIONS, 1);
706
707
708
  // This controls the maximum target size of the key frame.
  // For generating smaller key frames, use a smaller max_intra_size_pct
  // value, like 100 or 200.
709
  {
710
    const int max_intra_size_pct = 900;
Adrian Grange's avatar
Adrian Grange committed
711
    aom_codec_control(&codec, AOME_SET_MAX_INTRA_BITRATE_PCT,
712
713
                      max_intra_size_pct);
  }
714
715
716

  frame_avail = 1;
  while (frame_avail || got_data) {
Adrian Grange's avatar
Adrian Grange committed
717
718
719
    struct aom_usec_timer timer;
    aom_codec_iter_t iter = NULL;
    const aom_codec_cx_pkt_t *pkt;
Adrian Grange's avatar
Adrian Grange committed
720
#if AOM_ENCODER_ABI_VERSION > (4 + AOM_CODEC_ABI_VERSION)
721
722
    // Update the temporal layer_id. No spatial layers in this test.
    layer_id.spatial_layer_id = 0;
723
#endif
724
725
    layer_id.temporal_layer_id =
        cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
Adrian Grange's avatar
Adrian Grange committed
726
    if (strncmp(encoder->name, "av1", 3) == 0) {
Adrian Grange's avatar
Adrian Grange committed
727
      aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
Adrian Grange's avatar
Adrian Grange committed
728
    } else if (strncmp(encoder->name, "aom", 3) == 0) {
Adrian Grange's avatar
Adrian Grange committed
729
      aom_codec_control(&codec, AOME_SET_TEMPORAL_LAYER_ID,
Marco's avatar
Marco committed
730
                        layer_id.temporal_layer_id);
731
    }
732
    flags = layer_flags[frame_cnt % flag_periodicity];
733
    if (layering_mode == 0) flags = 0;
Adrian Grange's avatar
Adrian Grange committed
734
    frame_avail = aom_img_read(&raw, infile);
735
    if (frame_avail) ++rc.layer_input_frames[layer_id.temporal_layer_id];
Adrian Grange's avatar
Adrian Grange committed
736
737
    aom_usec_timer_start(&timer);
    if (aom_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags,
Adrian Grange's avatar
Adrian Grange committed
738
                         AOM_DL_REALTIME)) {
739
740
      die_codec(&codec, "Failed to encode frame");
    }
Adrian Grange's avatar
Adrian Grange committed
741
742
    aom_usec_timer_mark(&timer);
    cx_time += aom_usec_timer_elapsed(&timer);
743
744
    // Reset KF flag.
    if (layering_mode != 7) {
Adrian Grange's avatar
Adrian Grange committed
745
      layer_flags[0] &= ~AOM_EFLAG_FORCE_KF;
746
747
    }
    got_data = 0;
Adrian Grange's avatar
Adrian Grange committed
748
    while ((pkt = aom_codec_get_cx_data(&codec, &iter))) {
749
750
      got_data = 1;
      switch (pkt->kind) {
Adrian Grange's avatar
Adrian Grange committed
751
        case AOM_CODEC_CX_FRAME_PKT:
752
          for (i = cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity];
753
               i < cfg.ts_number_layers; ++i) {
Adrian Grange's avatar
Adrian Grange committed
754
            aom_video_writer_write_frame(outfile[i], pkt->data.frame.buf,
755
                                         pkt->data.frame.sz, pts);
756
757
758
759
            ++rc.layer_tot_enc_frames[i];
            rc.layer_encoding_bitrate[i] += 8.0 * pkt->data.frame.sz;
            // Keep count of rate control stats per layer (for non-key frames).
            if (i == cfg.ts_layer_id[frame_cnt % cfg.ts_periodicity] &&
Adrian Grange's avatar
Adrian Grange committed
760
                !(pkt->data.frame.flags & AOM_FRAME_IS_KEY)) {
761
762
763
764
765
766
              rc.layer_avg_frame_size[i] += 8.0 * pkt->data.frame.sz;
              rc.layer_avg_rate_mismatch[i] +=
                  fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[i]) /
                  rc.layer_pfb[i];
              ++rc.layer_enc_frames[i];
            }
767
          }
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
          // Update for short-time encoding bitrate states, for moving window
          // of size rc->window, shifted by rc->window / 2.
          // Ignore first window segment, due to key frame.
          if (frame_cnt > rc.window_size) {
            sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
            if (frame_cnt % rc.window_size == 0) {
              rc.window_count += 1;
              rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
              rc.variance_st_encoding_bitrate +=
                  (sum_bitrate / rc.window_size) *
                  (sum_bitrate / rc.window_size);
              sum_bitrate = 0.0;
            }
          }
          // Second shifted window.
          if (frame_cnt > rc.window_size + rc.window_size / 2) {
            sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
            if (frame_cnt > 2 * rc.window_size &&
                frame_cnt % rc.window_size == 0) {
              rc.window_count += 1;
              rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
              rc.variance_st_encoding_bitrate +=
                  (sum_bitrate2 / rc.window_size) *
                  (sum_bitrate2 / rc.window_size);
              sum_bitrate2 = 0.0;
            }
          }
795
          break;
796
        default: break;
797
798
799
800
801
      }
    }
    ++frame_cnt;
    pts += frame_duration;
  }
802
  fclose(infile);
803
  printout_rate_control_summary(&rc, &cfg, frame_cnt);
804
805
  printf("\n");
  printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f \n",
806
807
         frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
         1000000 * (double)frame_cnt / (double)cx_time);
808

Adrian Grange's avatar
Adrian Grange committed
809
  if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy codec");
810

811
  // Try to rewrite the output file headers with the actual frame count.
Adrian Grange's avatar
Adrian Grange committed
812
  for (i = 0; i < cfg.ts_number_layers; ++i) aom_video_writer_close(outfile[i]);
813

Adrian Grange's avatar
Adrian Grange committed
814
  aom_img_free(&raw);
815
816
  return EXIT_SUCCESS;
}