svc_encodeframe.c 22.5 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
13
 */

/**
 * @file
Yaowu Xu's avatar
Yaowu Xu committed
14
 * VP9 SVC encoding support via libaom
15
16
 */

17
#include <assert.h>
18
#include <math.h>
19
#include <limits.h>
20
21
22
23
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Adrian Grange's avatar
Adrian Grange committed
24
#define AOM_DISABLE_CTRL_TYPECHECKS 1
Adrian Grange's avatar
Adrian Grange committed
25
#include "./aom_config.h"
Yaowu Xu's avatar
Yaowu Xu committed
26
#include "aom/svc_context.h"
Adrian Grange's avatar
Adrian Grange committed
27
#include "aom/aomcx.h"
Adrian Grange's avatar
Adrian Grange committed
28
29
#include "aom/aom_encoder.h"
#include "aom_mem/aom_mem.h"
Yaowu Xu's avatar
Yaowu Xu committed
30
#include "av1/common/onyxc_int.h"
31

32
#ifdef __MINGW32__
33
#define strtok_r strtok_s
34
#ifndef MINGW_HAS_SECURE_API
35
36
// proto from /usr/x86_64-w64-mingw32/include/sec_api/string_s.h
_CRTIMP char *__cdecl strtok_s(char *str, const char *delim, char **context);
clang-format's avatar
clang-format committed
37
38
#endif /* MINGW_HAS_SECURE_API */
#endif /* __MINGW32__ */
39
40
41
42
43
44
45
46
47
48

#ifdef _MSC_VER
#define strdup _strdup
#define strtok_r strtok_s
#endif

#define SVC_REFERENCE_FRAMES 8
#define SUPERFRAME_SLOTS (8)
#define SUPERFRAME_BUFFER_SIZE (SUPERFRAME_SLOTS * sizeof(uint32_t) + 2)

49
#define MAX_QUANTIZER 63
50

Adrian Grange's avatar
Adrian Grange committed
51
static const int DEFAULT_SCALE_FACTORS_NUM[AOM_SS_MAX_LAYERS] = { 4, 5, 7, 11,
clang-format's avatar
clang-format committed
52
                                                                  16 };
53

Adrian Grange's avatar
Adrian Grange committed
54
static const int DEFAULT_SCALE_FACTORS_DEN[AOM_SS_MAX_LAYERS] = { 16, 16, 16,
clang-format's avatar
clang-format committed
55
                                                                  16, 16 };
56
57
58
59
60
61
62
63
64

typedef enum {
  QUANTIZER = 0,
  BITRATE,
  SCALE_FACTOR,
  AUTO_ALT_REF,
  ALL_OPTION_TYPES
} LAYER_OPTION_TYPE;

clang-format's avatar
clang-format committed
65
66
static const int option_max_values[ALL_OPTION_TYPES] = { 63, INT_MAX, INT_MAX,
                                                         1 };
67

clang-format's avatar
clang-format committed
68
static const int option_min_values[ALL_OPTION_TYPES] = { 0, 0, 1, 0 };
69

70
71
// One encoded frame
typedef struct FrameData {
clang-format's avatar
clang-format committed
72
73
  void *buf;                     // compressed data buffer
  size_t size;                   // length of compressed data
Adrian Grange's avatar
Adrian Grange committed
74
  aom_codec_frame_flags_t flags; /**< flags for this frame */
clang-format's avatar
clang-format committed
75
  struct FrameData *next;
76
77
} FrameData;

78
static SvcInternal_t *get_svc_internal(SvcContext *svc_ctx) {
79
80
  if (svc_ctx == NULL) return NULL;
  if (svc_ctx->internal == NULL) {
81
    SvcInternal_t *const si = (SvcInternal_t *)malloc(sizeof(*si));
82
83
84
85
86
    if (si != NULL) {
      memset(si, 0, sizeof(*si));
    }
    svc_ctx->internal = si;
  }
87
  return (SvcInternal_t *)svc_ctx->internal;
88
89
}

clang-format's avatar
clang-format committed
90
static const SvcInternal_t *get_const_svc_internal(const SvcContext *svc_ctx) {
91
  if (svc_ctx == NULL) return NULL;
92
  return (const SvcInternal_t *)svc_ctx->internal;
93
94
}

95
static void svc_log_reset(SvcContext *svc_ctx) {
96
  SvcInternal_t *const si = (SvcInternal_t *)svc_ctx->internal;
97
98
99
  si->message_buffer[0] = '\0';
}

clang-format's avatar
clang-format committed
100
101
static int svc_log(SvcContext *svc_ctx, SVC_LOG_LEVEL level, const char *fmt,
                   ...) {
102
103
104
  char buf[512];
  int retval = 0;
  va_list ap;
105
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

  if (level > svc_ctx->log_level) {
    return retval;
  }

  va_start(ap, fmt);
  retval = vsnprintf(buf, sizeof(buf), fmt, ap);
  va_end(ap);

  if (svc_ctx->log_print) {
    printf("%s", buf);
  } else {
    strncat(si->message_buffer, buf,
            sizeof(si->message_buffer) - strlen(si->message_buffer) - 1);
  }

  if (level == SVC_LOG_ERROR) {
    si->codec_ctx->err_detail = si->message_buffer;
  }
  return retval;
}

Adrian Grange's avatar
Adrian Grange committed
128
static aom_codec_err_t extract_option(LAYER_OPTION_TYPE type, char *input,
clang-format's avatar
clang-format committed
129
                                      int *value0, int *value1) {
130
131
  if (type == SCALE_FACTOR) {
    *value0 = strtol(input, &input, 10);
Adrian Grange's avatar
Adrian Grange committed
132
    if (*input++ != '/') return AOM_CODEC_INVALID_PARAM;
133
134
135
136
137
    *value1 = strtol(input, &input, 10);

    if (*value0 < option_min_values[SCALE_FACTOR] ||
        *value1 < option_min_values[SCALE_FACTOR] ||
        *value0 > option_max_values[SCALE_FACTOR] ||
138
139
        *value1 > option_max_values[SCALE_FACTOR] ||
        *value0 > *value1)  // num shouldn't be greater than den
Adrian Grange's avatar
Adrian Grange committed
140
      return AOM_CODEC_INVALID_PARAM;
141
  } else {
142
    *value0 = atoi(input);
clang-format's avatar
clang-format committed
143
    if (*value0 < option_min_values[type] || *value0 > option_max_values[type])
Adrian Grange's avatar
Adrian Grange committed
144
      return AOM_CODEC_INVALID_PARAM;
145
  }
Adrian Grange's avatar
Adrian Grange committed
146
  return AOM_CODEC_OK;
147
148
}

Adrian Grange's avatar
Adrian Grange committed
149
static aom_codec_err_t parse_layer_options_from_string(SvcContext *svc_ctx,
150
151
152
153
154
                                                       LAYER_OPTION_TYPE type,
                                                       const char *input,
                                                       int *option0,
                                                       int *option1) {
  int i;
Adrian Grange's avatar
Adrian Grange committed
155
  aom_codec_err_t res = AOM_CODEC_OK;
156
157
158
159
160
  char *input_string;
  char *token;
  const char *delim = ",";
  char *save_ptr;

161
162
  if (input == NULL || option0 == NULL ||
      (option1 == NULL && type == SCALE_FACTOR))
Adrian Grange's avatar
Adrian Grange committed
163
    return AOM_CODEC_INVALID_PARAM;
164

165
  input_string = strdup(input);
166
167
168
  token = strtok_r(input_string, delim, &save_ptr);
  for (i = 0; i < svc_ctx->spatial_layers; ++i) {
    if (token != NULL) {
169
      res = extract_option(type, token, option0 + i, option1 + i);
Adrian Grange's avatar
Adrian Grange committed
170
      if (res != AOM_CODEC_OK) break;
171
172
      token = strtok_r(NULL, delim, &save_ptr);
    } else {
173
      break;
174
175
    }
  }
Adrian Grange's avatar
Adrian Grange committed
176
  if (res == AOM_CODEC_OK && i != svc_ctx->spatial_layers) {
177
    svc_log(svc_ctx, SVC_LOG_ERROR,
178
            "svc: layer params type: %d    %d values required, "
clang-format's avatar
clang-format committed
179
180
            "but only %d specified\n",
            type, svc_ctx->spatial_layers, i);
Adrian Grange's avatar
Adrian Grange committed
181
    res = AOM_CODEC_INVALID_PARAM;
182
183
184
185
186
187
188
189
190
191
192
193
  }
  free(input_string);
  return res;
}

/**
 * Parse SVC encoding options
 * Format: encoding-mode=<svc_mode>,layers=<layer_count>
 *         scale-factors=<n1>/<d1>,<n2>/<d2>,...
 *         quantizers=<q1>,<q2>,...
 * svc_mode = [i|ip|alt_ip|gf]
 */
Adrian Grange's avatar
Adrian Grange committed
194
static aom_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
195
196
197
198
  char *input_string;
  char *option_name;
  char *option_value;
  char *input_ptr;
199
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
Adrian Grange's avatar
Adrian Grange committed
200
  aom_codec_err_t res = AOM_CODEC_OK;
201
  int i, alt_ref_enabled = 0;
202

Adrian Grange's avatar
Adrian Grange committed
203
  if (options == NULL) return AOM_CODEC_OK;
204
205
206
207
208
209
210
211
212
213
  input_string = strdup(options);

  // parse option name
  option_name = strtok_r(input_string, "=", &input_ptr);
  while (option_name != NULL) {
    // parse option value
    option_value = strtok_r(NULL, " ", &input_ptr);
    if (option_value == NULL) {
      svc_log(svc_ctx, SVC_LOG_ERROR, "option missing value: %s\n",
              option_name);
Adrian Grange's avatar
Adrian Grange committed
214
      res = AOM_CODEC_INVALID_PARAM;
215
216
      break;
    }
217
    if (strcmp("spatial-layers", option_name) == 0) {
218
      svc_ctx->spatial_layers = atoi(option_value);
219
220
    } else if (strcmp("temporal-layers", option_name) == 0) {
      svc_ctx->temporal_layers = atoi(option_value);
221
    } else if (strcmp("scale-factors", option_name) == 0) {
222
      res = parse_layer_options_from_string(svc_ctx, SCALE_FACTOR, option_value,
223
224
                                            si->svc_params.scaling_factor_num,
                                            si->svc_params.scaling_factor_den);
Adrian Grange's avatar
Adrian Grange committed
225
      if (res != AOM_CODEC_OK) break;
226
    } else if (strcmp("max-quantizers", option_name) == 0) {
clang-format's avatar
clang-format committed
227
228
229
      res =
          parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
                                          si->svc_params.max_quantizers, NULL);
Adrian Grange's avatar
Adrian Grange committed
230
      if (res != AOM_CODEC_OK) break;
231
    } else if (strcmp("min-quantizers", option_name) == 0) {
clang-format's avatar
clang-format committed
232
233
234
      res =
          parse_layer_options_from_string(svc_ctx, QUANTIZER, option_value,
                                          si->svc_params.min_quantizers, NULL);
Adrian Grange's avatar
Adrian Grange committed
235
      if (res != AOM_CODEC_OK) break;
236
    } else if (strcmp("auto-alt-refs", option_name) == 0) {
237
238
      res = parse_layer_options_from_string(svc_ctx, AUTO_ALT_REF, option_value,
                                            si->enable_auto_alt_ref, NULL);
Adrian Grange's avatar
Adrian Grange committed
239
      if (res != AOM_CODEC_OK) break;
240
241
242
    } else if (strcmp("bitrates", option_name) == 0) {
      res = parse_layer_options_from_string(svc_ctx, BITRATE, option_value,
                                            si->bitrates, NULL);
Adrian Grange's avatar
Adrian Grange committed
243
      if (res != AOM_CODEC_OK) break;
244
245
    } else if (strcmp("multi-frame-contexts", option_name) == 0) {
      si->use_multiple_frame_contexts = atoi(option_value);
246
247
    } else {
      svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name);
Adrian Grange's avatar
Adrian Grange committed
248
      res = AOM_CODEC_INVALID_PARAM;
249
250
251
252
253
      break;
    }
    option_name = strtok_r(NULL, "=", &input_ptr);
  }
  free(input_string);
254

255
  for (i = 0; i < svc_ctx->spatial_layers; ++i) {
256
257
258
259
    if (si->svc_params.max_quantizers[i] > MAX_QUANTIZER ||
        si->svc_params.max_quantizers[i] < 0 ||
        si->svc_params.min_quantizers[i] > si->svc_params.max_quantizers[i] ||
        si->svc_params.min_quantizers[i] < 0)
Adrian Grange's avatar
Adrian Grange committed
260
      res = AOM_CODEC_INVALID_PARAM;
261
262
  }

263
264
265
  if (si->use_multiple_frame_contexts &&
      (svc_ctx->spatial_layers > 3 ||
       svc_ctx->spatial_layers * svc_ctx->temporal_layers > 4))
Adrian Grange's avatar
Adrian Grange committed
266
    res = AOM_CODEC_INVALID_PARAM;
267

268
269
270
271
272
273
274
  for (i = 0; i < svc_ctx->spatial_layers; ++i)
    alt_ref_enabled += si->enable_auto_alt_ref[i];
  if (alt_ref_enabled > REF_FRAMES - svc_ctx->spatial_layers) {
    svc_log(svc_ctx, SVC_LOG_ERROR,
            "svc: auto alt ref: Maxinum %d(REF_FRAMES - layers) layers could"
            "enabled auto alt reference frame, but % layers are enabled\n",
            REF_FRAMES - svc_ctx->spatial_layers, alt_ref_enabled);
Adrian Grange's avatar
Adrian Grange committed
275
    res = AOM_CODEC_INVALID_PARAM;
276
277
  }

278
279
280
  return res;
}

Adrian Grange's avatar
Adrian Grange committed
281
aom_codec_err_t aom_svc_set_options(SvcContext *svc_ctx, const char *options) {
282
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
283
  if (svc_ctx == NULL || options == NULL || si == NULL) {
Adrian Grange's avatar
Adrian Grange committed
284
    return AOM_CODEC_INVALID_PARAM;
285
286
287
  }
  strncpy(si->options, options, sizeof(si->options));
  si->options[sizeof(si->options) - 1] = '\0';
Adrian Grange's avatar
Adrian Grange committed
288
  return AOM_CODEC_OK;
289
290
}

291
void assign_layer_bitrates(const SvcContext *svc_ctx,
Adrian Grange's avatar
Adrian Grange committed
292
                           aom_codec_enc_cfg_t *const enc_cfg) {
293
  int i;
294
  const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
295
296
297
298
299
300
  int sl, tl, spatial_layer_target;

  if (svc_ctx->temporal_layering_mode != 0) {
    if (si->bitrates[0] != 0) {
      enc_cfg->rc_target_bitrate = 0;
      for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
clang-format's avatar
clang-format committed
301
        enc_cfg->ss_target_bitrate[sl * svc_ctx->temporal_layers] = 0;
302
        for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
clang-format's avatar
clang-format committed
303
304
305
306
          enc_cfg->ss_target_bitrate[sl * svc_ctx->temporal_layers] +=
              (unsigned int)si->bitrates[sl * svc_ctx->temporal_layers + tl];
          enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + tl] =
              si->bitrates[sl * svc_ctx->temporal_layers + tl];
307
308
309
310
        }
      }
    } else {
      float total = 0;
Adrian Grange's avatar
Adrian Grange committed
311
      float alloc_ratio[AOM_MAX_LAYERS] = { 0 };
312
313
314

      for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
        if (si->svc_params.scaling_factor_den[sl] > 0) {
clang-format's avatar
clang-format committed
315
316
317
          alloc_ratio[sl] =
              (float)(si->svc_params.scaling_factor_num[sl] * 1.0 /
                      si->svc_params.scaling_factor_den[sl]);
318
319
320
          total += alloc_ratio[sl];
        }
      }
321

322
323
      for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
        enc_cfg->ss_target_bitrate[sl] = spatial_layer_target =
clang-format's avatar
clang-format committed
324
325
            (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[sl] /
                           total);
326
327
328
329
330
331
332
        if (svc_ctx->temporal_layering_mode == 3) {
          enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers] =
              spatial_layer_target >> 1;
          enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 1] =
              (spatial_layer_target >> 1) + (spatial_layer_target >> 2);
          enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 2] =
              spatial_layer_target;
333
334
        } else if (svc_ctx->temporal_layering_mode == 2 ||
                   svc_ctx->temporal_layering_mode == 1) {
335
336
337
338
339
340
341
342
343
          enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers] =
              spatial_layer_target * 2 / 3;
          enc_cfg->layer_target_bitrate[sl * svc_ctx->temporal_layers + 1] =
              spatial_layer_target;
        } else {
          // User should explicitly assign bitrates in this case.
          assert(0);
        }
      }
344
345
    }
  } else {
346
347
348
349
350
351
352
353
    if (si->bitrates[0] != 0) {
      enc_cfg->rc_target_bitrate = 0;
      for (i = 0; i < svc_ctx->spatial_layers; ++i) {
        enc_cfg->ss_target_bitrate[i] = (unsigned int)si->bitrates[i];
        enc_cfg->rc_target_bitrate += si->bitrates[i];
      }
    } else {
      float total = 0;
Adrian Grange's avatar
Adrian Grange committed
354
      float alloc_ratio[AOM_MAX_LAYERS] = { 0 };
355

356
357
358
359
      for (i = 0; i < svc_ctx->spatial_layers; ++i) {
        if (si->svc_params.scaling_factor_den[i] > 0) {
          alloc_ratio[i] = (float)(si->svc_params.scaling_factor_num[i] * 1.0 /
                                   si->svc_params.scaling_factor_den[i]);
360

361
362
363
          alloc_ratio[i] *= alloc_ratio[i];
          total += alloc_ratio[i];
        }
364
      }
Adrian Grange's avatar
Adrian Grange committed
365
      for (i = 0; i < AOM_SS_MAX_LAYERS; ++i) {
366
        if (total > 0) {
clang-format's avatar
clang-format committed
367
368
369
          enc_cfg->layer_target_bitrate[i] =
              (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[i] /
                             total);
370
        }
371
372
373
374
375
      }
    }
  }
}

Adrian Grange's avatar
Adrian Grange committed
376
377
378
379
aom_codec_err_t aom_svc_init(SvcContext *svc_ctx, aom_codec_ctx_t *codec_ctx,
                             aom_codec_iface_t *iface,
                             aom_codec_enc_cfg_t *enc_cfg) {
  aom_codec_err_t res;
clang-format's avatar
clang-format committed
380
  int i, sl, tl;
381
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
382
383
  if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL ||
      enc_cfg == NULL) {
Adrian Grange's avatar
Adrian Grange committed
384
    return AOM_CODEC_INVALID_PARAM;
385
  }
Adrian Grange's avatar
Adrian Grange committed
386
  if (si == NULL) return AOM_CODEC_MEM_ERROR;
387
388
389
390
391
392
393
394
395

  si->codec_ctx = codec_ctx;

  si->width = enc_cfg->g_w;
  si->height = enc_cfg->g_h;

  if (enc_cfg->kf_max_dist < 2) {
    svc_log(svc_ctx, SVC_LOG_ERROR, "key frame distance too small: %d\n",
            enc_cfg->kf_max_dist);
Adrian Grange's avatar
Adrian Grange committed
396
    return AOM_CODEC_INVALID_PARAM;
397
398
399
400
  }
  si->kf_dist = enc_cfg->kf_max_dist;

  if (svc_ctx->spatial_layers == 0)
Adrian Grange's avatar
Adrian Grange committed
401
    svc_ctx->spatial_layers = AOM_SS_DEFAULT_LAYERS;
402
  if (svc_ctx->spatial_layers < 1 ||
Adrian Grange's avatar
Adrian Grange committed
403
      svc_ctx->spatial_layers > AOM_SS_MAX_LAYERS) {
404
405
    svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n",
            svc_ctx->spatial_layers);
Adrian Grange's avatar
Adrian Grange committed
406
    return AOM_CODEC_INVALID_PARAM;
407
408
  }

409
410
411
412
  // Note: temporal_layering_mode only applies to one-pass CBR
  // si->svc_params.temporal_layering_mode = svc_ctx->temporal_layering_mode;
  if (svc_ctx->temporal_layering_mode == 3) {
    svc_ctx->temporal_layers = 3;
413
414
  } else if (svc_ctx->temporal_layering_mode == 2 ||
             svc_ctx->temporal_layering_mode == 1) {
415
416
417
    svc_ctx->temporal_layers = 2;
  }

Adrian Grange's avatar
Adrian Grange committed
418
  for (sl = 0; sl < AOM_SS_MAX_LAYERS; ++sl) {
419
420
421
    si->svc_params.scaling_factor_num[sl] = DEFAULT_SCALE_FACTORS_NUM[sl];
    si->svc_params.scaling_factor_den[sl] = DEFAULT_SCALE_FACTORS_DEN[sl];
  }
422
423
424
425
426
427
  for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
    for (sl = 0; sl < svc_ctx->spatial_layers; ++sl) {
      i = sl * svc_ctx->temporal_layers + tl;
      si->svc_params.max_quantizers[i] = MAX_QUANTIZER;
      si->svc_params.min_quantizers[i] = 0;
    }
428
  }
429

430
431
  // Parse aggregate command line options. Options must start with
  // "layers=xx" then followed by other options
432
  res = parse_options(svc_ctx, si->options);
Adrian Grange's avatar
Adrian Grange committed
433
  if (res != AOM_CODEC_OK) return res;
434

clang-format's avatar
clang-format committed
435
  if (svc_ctx->spatial_layers < 1) svc_ctx->spatial_layers = 1;
Adrian Grange's avatar
Adrian Grange committed
436
437
  if (svc_ctx->spatial_layers > AOM_SS_MAX_LAYERS)
    svc_ctx->spatial_layers = AOM_SS_MAX_LAYERS;
438

clang-format's avatar
clang-format committed
439
  if (svc_ctx->temporal_layers < 1) svc_ctx->temporal_layers = 1;
Adrian Grange's avatar
Adrian Grange committed
440
441
  if (svc_ctx->temporal_layers > AOM_TS_MAX_LAYERS)
    svc_ctx->temporal_layers = AOM_TS_MAX_LAYERS;
442

Adrian Grange's avatar
Adrian Grange committed
443
  if (svc_ctx->temporal_layers * svc_ctx->spatial_layers > AOM_MAX_LAYERS) {
clang-format's avatar
clang-format committed
444
445
446
447
    svc_log(svc_ctx, SVC_LOG_ERROR,
            "spatial layers * temporal layers exceeds the maximum number of "
            "allowed layers of %d\n",
            svc_ctx->spatial_layers * svc_ctx->temporal_layers,
Adrian Grange's avatar
Adrian Grange committed
448
449
            (int)AOM_MAX_LAYERS);
    return AOM_CODEC_INVALID_PARAM;
450
  }
451
  assign_layer_bitrates(svc_ctx, enc_cfg);
452

453
#if CONFIG_SPATIAL_SVC
454
  for (i = 0; i < svc_ctx->spatial_layers; ++i)
455
    enc_cfg->ss_enable_auto_alt_ref[i] = si->enable_auto_alt_ref[i];
456
#endif
457

458
459
460
  if (svc_ctx->temporal_layers > 1) {
    int i;
    for (i = 0; i < svc_ctx->temporal_layers; ++i) {
clang-format's avatar
clang-format committed
461
462
      enc_cfg->ts_target_bitrate[i] =
          enc_cfg->rc_target_bitrate / svc_ctx->temporal_layers;
463
464
465
466
      enc_cfg->ts_rate_decimator[i] = 1 << (svc_ctx->temporal_layers - 1 - i);
    }
  }

clang-format's avatar
clang-format committed
467
  if (svc_ctx->threads) enc_cfg->g_threads = svc_ctx->threads;
468
469

  // Modify encoder configuration
470
  enc_cfg->ss_number_layers = svc_ctx->spatial_layers;
471
  enc_cfg->ts_number_layers = svc_ctx->temporal_layers;
472

Adrian Grange's avatar
Adrian Grange committed
473
  if (enc_cfg->rc_end_usage == AOM_CBR) {
474
475
    enc_cfg->rc_resize_allowed = 0;
    enc_cfg->rc_min_quantizer = 2;
476
    enc_cfg->rc_max_quantizer = 56;
477
478
    enc_cfg->rc_undershoot_pct = 50;
    enc_cfg->rc_overshoot_pct = 50;
479
    enc_cfg->rc_buf_initial_sz = 500;
480
481
    enc_cfg->rc_buf_optimal_sz = 600;
    enc_cfg->rc_buf_sz = 1000;
482
    enc_cfg->rc_dropframe_thresh = 0;
483
484
  }

485
486
  if (enc_cfg->g_error_resilient == 0 && si->use_multiple_frame_contexts == 0)
    enc_cfg->g_error_resilient = 1;
487
488

  // Initialize codec
Adrian Grange's avatar
Adrian Grange committed
489
490
  res = aom_codec_enc_init(codec_ctx, iface, enc_cfg, AOM_CODEC_USE_PSNR);
  if (res != AOM_CODEC_OK) {
491
492
493
    svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n");
    return res;
  }
494
  if (svc_ctx->spatial_layers > 1 || svc_ctx->temporal_layers > 1) {
Adrian Grange's avatar
Adrian Grange committed
495
496
    aom_codec_control(codec_ctx, VP9E_SET_SVC, 1);
    aom_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &si->svc_params);
497
  }
Adrian Grange's avatar
Adrian Grange committed
498
  return AOM_CODEC_OK;
499
500
501
502
503
504
}

/**
 * Encode a frame into multiple layers
 * Create a superframe containing the individual layers
 */
Adrian Grange's avatar
Adrian Grange committed
505
506
aom_codec_err_t aom_svc_encode(SvcContext *svc_ctx, aom_codec_ctx_t *codec_ctx,
                               struct aom_image *rawimg, aom_codec_pts_t pts,
507
                               int64_t duration, int deadline) {
Adrian Grange's avatar
Adrian Grange committed
508
509
510
  aom_codec_err_t res;
  aom_codec_iter_t iter;
  const aom_codec_cx_pkt_t *cx_pkt;
511
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
512
  if (svc_ctx == NULL || codec_ctx == NULL || si == NULL) {
Adrian Grange's avatar
Adrian Grange committed
513
    return AOM_CODEC_INVALID_PARAM;
514
515
516
517
  }

  svc_log_reset(svc_ctx);

clang-format's avatar
clang-format committed
518
  res =
Adrian Grange's avatar
Adrian Grange committed
519
      aom_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0, deadline);
Adrian Grange's avatar
Adrian Grange committed
520
  if (res != AOM_CODEC_OK) {
521
522
523
524
    return res;
  }
  // save compressed data
  iter = NULL;
Adrian Grange's avatar
Adrian Grange committed
525
  while ((cx_pkt = aom_codec_get_cx_data(codec_ctx, &iter))) {
526
    switch (cx_pkt->kind) {
Adrian Grange's avatar
Adrian Grange committed
527
#if AOM_ENCODER_ABI_VERSION > (5 + AOM_CODEC_ABI_VERSION)
528
#if CONFIG_SPATIAL_SVC
Adrian Grange's avatar
Adrian Grange committed
529
      case AOM_CODEC_SPATIAL_SVC_LAYER_PSNR: {
530
        int i;
531
532
533
534
535
        for (i = 0; i < svc_ctx->spatial_layers; ++i) {
          int j;
          svc_log(svc_ctx, SVC_LOG_DEBUG,
                  "SVC frame: %d, layer: %d, PSNR(Total/Y/U/V): "
                  "%2.3f  %2.3f  %2.3f  %2.3f \n",
clang-format's avatar
clang-format committed
536
                  si->psnr_pkt_received, i, cx_pkt->data.layer_psnr[i].psnr[0],
537
538
539
540
541
542
                  cx_pkt->data.layer_psnr[i].psnr[1],
                  cx_pkt->data.layer_psnr[i].psnr[2],
                  cx_pkt->data.layer_psnr[i].psnr[3]);
          svc_log(svc_ctx, SVC_LOG_DEBUG,
                  "SVC frame: %d, layer: %d, SSE(Total/Y/U/V): "
                  "%2.3f  %2.3f  %2.3f  %2.3f \n",
clang-format's avatar
clang-format committed
543
                  si->psnr_pkt_received, i, cx_pkt->data.layer_psnr[i].sse[0],
544
545
546
547
548
                  cx_pkt->data.layer_psnr[i].sse[1],
                  cx_pkt->data.layer_psnr[i].sse[2],
                  cx_pkt->data.layer_psnr[i].sse[3]);

          for (j = 0; j < COMPONENTS; ++j) {
clang-format's avatar
clang-format committed
549
            si->psnr_sum[i][j] += cx_pkt->data.layer_psnr[i].psnr[j];
550
551
            si->sse_sum[i][j] += cx_pkt->data.layer_psnr[i].sse[j];
          }
552
        }
553
        ++si->psnr_pkt_received;
554
        break;
555
      }
Adrian Grange's avatar
Adrian Grange committed
556
      case AOM_CODEC_SPATIAL_SVC_LAYER_SIZES: {
557
        int i;
558
        for (i = 0; i < svc_ctx->spatial_layers; ++i)
559
560
561
          si->bytes_sum[i] += cx_pkt->data.layer_sizes[i];
        break;
      }
Marco's avatar
Marco committed
562
#endif
563
#endif
clang-format's avatar
clang-format committed
564
      default: { break; }
565
566
    }
  }
567

Adrian Grange's avatar
Adrian Grange committed
568
  return AOM_CODEC_OK;
569
570
}

Adrian Grange's avatar
Adrian Grange committed
571
const char *aom_svc_get_message(const SvcContext *svc_ctx) {
572
  const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
573
574
575
576
  if (svc_ctx == NULL || si == NULL) return NULL;
  return si->message_buffer;
}

577
578
579
580
581
static double calc_psnr(double d) {
  if (d == 0) return 100;
  return -10.0 * log(d) / log(10.0);
}

582
// dump accumulated statistics and reset accumulated values
Adrian Grange's avatar
Adrian Grange committed
583
const char *aom_svc_dump_statistics(SvcContext *svc_ctx) {
584
  int number_of_frames;
585
  int i, j;
586
  uint32_t bytes_total = 0;
587
588
589
590
591
  double scale[COMPONENTS];
  double psnr[COMPONENTS];
  double mse[COMPONENTS];
  double y_scale;

592
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
593
594
595
596
  if (svc_ctx == NULL || si == NULL) return NULL;

  svc_log_reset(svc_ctx);

597
  number_of_frames = si->psnr_pkt_received;
Adrian Grange's avatar
Adrian Grange committed
598
  if (number_of_frames <= 0) return aom_svc_get_message(svc_ctx);
599
600

  svc_log(svc_ctx, SVC_LOG_INFO, "\n");
601
  for (i = 0; i < svc_ctx->spatial_layers; ++i) {
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
    svc_log(svc_ctx, SVC_LOG_INFO,
            "Layer %d Average PSNR=[%2.3f, %2.3f, %2.3f, %2.3f], Bytes=[%u]\n",
            i, (double)si->psnr_sum[i][0] / number_of_frames,
            (double)si->psnr_sum[i][1] / number_of_frames,
            (double)si->psnr_sum[i][2] / number_of_frames,
            (double)si->psnr_sum[i][3] / number_of_frames, si->bytes_sum[i]);
    // the following psnr calculation is deduced from ffmpeg.c#print_report
    y_scale = si->width * si->height * 255.0 * 255.0 * number_of_frames;
    scale[1] = y_scale;
    scale[2] = scale[3] = y_scale / 4;  // U or V
    scale[0] = y_scale * 1.5;           // total

    for (j = 0; j < COMPONENTS; j++) {
      psnr[j] = calc_psnr(si->sse_sum[i][j] / scale[j]);
      mse[j] = si->sse_sum[i][j] * 255.0 * 255.0 / scale[j];
    }
    svc_log(svc_ctx, SVC_LOG_INFO,
            "Layer %d Overall PSNR=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, psnr[0],
            psnr[1], psnr[2], psnr[3]);
    svc_log(svc_ctx, SVC_LOG_INFO,
            "Layer %d Overall MSE=[%2.3f, %2.3f, %2.3f, %2.3f]\n", i, mse[0],
            mse[1], mse[2], mse[3]);

    bytes_total += si->bytes_sum[i];
626
    // Clear sums for next time.
627
628
629
630
631
    si->bytes_sum[i] = 0;
    for (j = 0; j < COMPONENTS; ++j) {
      si->psnr_sum[i][j] = 0;
      si->sse_sum[i][j] = 0;
    }
632
633
634
  }

  // only display statistics once
635
  si->psnr_pkt_received = 0;
636
637

  svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total);
Adrian Grange's avatar
Adrian Grange committed
638
  return aom_svc_get_message(svc_ctx);
639
640
}

Adrian Grange's avatar
Adrian Grange committed
641
void aom_svc_release(SvcContext *svc_ctx) {
642
  SvcInternal_t *si;
643
644
  if (svc_ctx == NULL) return;
  // do not use get_svc_internal as it will unnecessarily allocate an
645
646
  // SvcInternal_t if it was not already allocated
  si = (SvcInternal_t *)svc_ctx->internal;
647
648
649
650
651
  if (si != NULL) {
    free(si);
    svc_ctx->internal = NULL;
  }
}