svc_encodeframe.c 22.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

/**
 * @file
 * VP9 SVC encoding support via libvpx
 */

16
#include <assert.h>
17
#include <math.h>
18
#include <limits.h>
19 20 21 22 23
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define VPX_DISABLE_CTRL_TYPECHECKS 1
24
#include "./vpx_config.h"
Yaowu Xu's avatar
Yaowu Xu committed
25 26 27 28
#include "aom/svc_context.h"
#include "aom/vp8cx.h"
#include "aom/vpx_encoder.h"
#include "aom_mem/vpx_mem.h"
29
#include "vp10/common/onyxc_int.h"
30

31
#ifdef __MINGW32__
32
#define strtok_r strtok_s
33
#ifndef MINGW_HAS_SECURE_API
34 35
// 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
36 37
#endif /* MINGW_HAS_SECURE_API */
#endif /* __MINGW32__ */
38 39 40 41 42 43 44 45 46 47

#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)

48
#define MAX_QUANTIZER 63
49

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

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

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

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

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

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

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

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

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

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

  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;
}

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

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

148
static vpx_codec_err_t parse_layer_options_from_string(SvcContext *svc_ctx,
149 150 151 152 153 154
                                                       LAYER_OPTION_TYPE type,
                                                       const char *input,
                                                       int *option0,
                                                       int *option1) {
  int i;
  vpx_codec_err_t res = VPX_CODEC_OK;
155 156 157 158 159
  char *input_string;
  char *token;
  const char *delim = ",";
  char *save_ptr;

160 161
  if (input == NULL || option0 == NULL ||
      (option1 == NULL && type == SCALE_FACTOR))
162 163
    return VPX_CODEC_INVALID_PARAM;

164
  input_string = strdup(input);
165 166 167
  token = strtok_r(input_string, delim, &save_ptr);
  for (i = 0; i < svc_ctx->spatial_layers; ++i) {
    if (token != NULL) {
168
      res = extract_option(type, token, option0 + i, option1 + i);
clang-format's avatar
clang-format committed
169
      if (res != VPX_CODEC_OK) break;
170 171
      token = strtok_r(NULL, delim, &save_ptr);
    } else {
172
      break;
173 174
    }
  }
175
  if (res == VPX_CODEC_OK && i != svc_ctx->spatial_layers) {
176
    svc_log(svc_ctx, SVC_LOG_ERROR,
177
            "svc: layer params type: %d    %d values required, "
clang-format's avatar
clang-format committed
178 179
            "but only %d specified\n",
            type, svc_ctx->spatial_layers, i);
180 181 182 183 184 185 186 187 188 189 190 191 192
    res = VPX_CODEC_INVALID_PARAM;
  }
  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]
 */
193
static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
194 195 196 197
  char *input_string;
  char *option_name;
  char *option_value;
  char *input_ptr;
198
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
199
  vpx_codec_err_t res = VPX_CODEC_OK;
200
  int i, alt_ref_enabled = 0;
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

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

254
  for (i = 0; i < svc_ctx->spatial_layers; ++i) {
255 256 257 258
    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)
259 260 261
      res = VPX_CODEC_INVALID_PARAM;
  }

262 263 264
  if (si->use_multiple_frame_contexts &&
      (svc_ctx->spatial_layers > 3 ||
       svc_ctx->spatial_layers * svc_ctx->temporal_layers > 4))
265 266
    res = VPX_CODEC_INVALID_PARAM;

267 268 269 270 271 272 273 274 275 276
  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);
    res = VPX_CODEC_INVALID_PARAM;
  }

277 278 279
  return res;
}

clang-format's avatar
clang-format committed
280
vpx_codec_err_t vpx_svc_set_options(SvcContext *svc_ctx, const char *options) {
281
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
282 283 284 285 286 287 288 289
  if (svc_ctx == NULL || options == NULL || si == NULL) {
    return VPX_CODEC_INVALID_PARAM;
  }
  strncpy(si->options, options, sizeof(si->options));
  si->options[sizeof(si->options) - 1] = '\0';
  return VPX_CODEC_OK;
}

290
void assign_layer_bitrates(const SvcContext *svc_ctx,
291 292
                           vpx_codec_enc_cfg_t *const enc_cfg) {
  int i;
293
  const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
294 295 296 297 298 299
  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
300
        enc_cfg->ss_target_bitrate[sl * svc_ctx->temporal_layers] = 0;
301
        for (tl = 0; tl < svc_ctx->temporal_layers; ++tl) {
clang-format's avatar
clang-format committed
302 303 304 305
          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];
306 307 308 309
        }
      }
    } else {
      float total = 0;
clang-format's avatar
clang-format committed
310
      float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
311 312 313

      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
314 315 316
          alloc_ratio[sl] =
              (float)(si->svc_params.scaling_factor_num[sl] * 1.0 /
                      si->svc_params.scaling_factor_den[sl]);
317 318 319
          total += alloc_ratio[sl];
        }
      }
320

321 322
      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
323 324
            (unsigned int)(enc_cfg->rc_target_bitrate * alloc_ratio[sl] /
                           total);
325 326 327 328 329 330 331
        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;
332 333
        } else if (svc_ctx->temporal_layering_mode == 2 ||
                   svc_ctx->temporal_layering_mode == 1) {
334 335 336 337 338 339 340 341 342
          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);
        }
      }
343 344
    }
  } else {
345 346 347 348 349 350 351 352
    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;
clang-format's avatar
clang-format committed
353
      float alloc_ratio[VPX_MAX_LAYERS] = { 0 };
354

355 356 357 358
      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]);
359

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

375
vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
376 377 378
                             vpx_codec_iface_t *iface,
                             vpx_codec_enc_cfg_t *enc_cfg) {
  vpx_codec_err_t res;
clang-format's avatar
clang-format committed
379
  int i, sl, tl;
380
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
  if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL ||
      enc_cfg == NULL) {
    return VPX_CODEC_INVALID_PARAM;
  }
  if (si == NULL) return VPX_CODEC_MEM_ERROR;

  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);
    return VPX_CODEC_INVALID_PARAM;
  }
  si->kf_dist = enc_cfg->kf_max_dist;

  if (svc_ctx->spatial_layers == 0)
    svc_ctx->spatial_layers = VPX_SS_DEFAULT_LAYERS;
  if (svc_ctx->spatial_layers < 1 ||
      svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS) {
    svc_log(svc_ctx, SVC_LOG_ERROR, "spatial layers: invalid value: %d\n",
            svc_ctx->spatial_layers);
    return VPX_CODEC_INVALID_PARAM;
  }

408 409 410 411
  // 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;
412 413
  } else if (svc_ctx->temporal_layering_mode == 2 ||
             svc_ctx->temporal_layering_mode == 1) {
414 415 416
    svc_ctx->temporal_layers = 2;
  }

417 418 419 420
  for (sl = 0; sl < VPX_SS_MAX_LAYERS; ++sl) {
    si->svc_params.scaling_factor_num[sl] = DEFAULT_SCALE_FACTORS_NUM[sl];
    si->svc_params.scaling_factor_den[sl] = DEFAULT_SCALE_FACTORS_DEN[sl];
  }
421 422 423 424 425 426
  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;
    }
427
  }
428

429 430
  // Parse aggregate command line options. Options must start with
  // "layers=xx" then followed by other options
431 432 433
  res = parse_options(svc_ctx, si->options);
  if (res != VPX_CODEC_OK) return res;

clang-format's avatar
clang-format committed
434
  if (svc_ctx->spatial_layers < 1) svc_ctx->spatial_layers = 1;
435 436 437
  if (svc_ctx->spatial_layers > VPX_SS_MAX_LAYERS)
    svc_ctx->spatial_layers = VPX_SS_MAX_LAYERS;

clang-format's avatar
clang-format committed
438
  if (svc_ctx->temporal_layers < 1) svc_ctx->temporal_layers = 1;
439 440 441
  if (svc_ctx->temporal_layers > VPX_TS_MAX_LAYERS)
    svc_ctx->temporal_layers = VPX_TS_MAX_LAYERS;

442
  if (svc_ctx->temporal_layers * svc_ctx->spatial_layers > VPX_MAX_LAYERS) {
clang-format's avatar
clang-format committed
443 444 445 446 447 448
    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,
            (int)VPX_MAX_LAYERS);
    return VPX_CODEC_INVALID_PARAM;
449
  }
450
  assign_layer_bitrates(svc_ctx, enc_cfg);
451

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

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

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

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

472 473 474
  if (enc_cfg->rc_end_usage == VPX_CBR) {
    enc_cfg->rc_resize_allowed = 0;
    enc_cfg->rc_min_quantizer = 2;
475
    enc_cfg->rc_max_quantizer = 56;
476 477
    enc_cfg->rc_undershoot_pct = 50;
    enc_cfg->rc_overshoot_pct = 50;
478
    enc_cfg->rc_buf_initial_sz = 500;
479 480
    enc_cfg->rc_buf_optimal_sz = 600;
    enc_cfg->rc_buf_sz = 1000;
481
    enc_cfg->rc_dropframe_thresh = 0;
482 483
  }

484 485
  if (enc_cfg->g_error_resilient == 0 && si->use_multiple_frame_contexts == 0)
    enc_cfg->g_error_resilient = 1;
486 487 488 489 490 491 492

  // Initialize codec
  res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR);
  if (res != VPX_CODEC_OK) {
    svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n");
    return res;
  }
493 494 495 496
  if (svc_ctx->spatial_layers > 1 || svc_ctx->temporal_layers > 1) {
    vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1);
    vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &si->svc_params);
  }
497 498 499 500 501 502 503
  return VPX_CODEC_OK;
}

/**
 * Encode a frame into multiple layers
 * Create a superframe containing the individual layers
 */
clang-format's avatar
clang-format committed
504 505
vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
                               struct vpx_image *rawimg, vpx_codec_pts_t pts,
506 507 508 509
                               int64_t duration, int deadline) {
  vpx_codec_err_t res;
  vpx_codec_iter_t iter;
  const vpx_codec_cx_pkt_t *cx_pkt;
510
  SvcInternal_t *const si = get_svc_internal(svc_ctx);
511
  if (svc_ctx == NULL || codec_ctx == NULL || si == NULL) {
512 513 514 515 516
    return VPX_CODEC_INVALID_PARAM;
  }

  svc_log_reset(svc_ctx);

clang-format's avatar
clang-format committed
517 518
  res =
      vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0, deadline);
519 520 521 522 523 524 525
  if (res != VPX_CODEC_OK) {
    return res;
  }
  // save compressed data
  iter = NULL;
  while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) {
    switch (cx_pkt->kind) {
Marco's avatar
Marco committed
526
#if VPX_ENCODER_ABI_VERSION > (5 + VPX_CODEC_ABI_VERSION)
527 528
#if CONFIG_SPATIAL_SVC
      case VPX_CODEC_SPATIAL_SVC_LAYER_PSNR: {
529
        int i;
530 531 532 533 534
        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
535
                  si->psnr_pkt_received, i, cx_pkt->data.layer_psnr[i].psnr[0],
536 537 538 539 540 541
                  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
542
                  si->psnr_pkt_received, i, cx_pkt->data.layer_psnr[i].sse[0],
543 544 545 546 547
                  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
548
            si->psnr_sum[i][j] += cx_pkt->data.layer_psnr[i].psnr[j];
549 550
            si->sse_sum[i][j] += cx_pkt->data.layer_psnr[i].sse[j];
          }
551
        }
552
        ++si->psnr_pkt_received;
553
        break;
554
      }
555 556
      case VPX_CODEC_SPATIAL_SVC_LAYER_SIZES: {
        int i;
557
        for (i = 0; i < svc_ctx->spatial_layers; ++i)
558 559 560
          si->bytes_sum[i] += cx_pkt->data.layer_sizes[i];
        break;
      }
Marco's avatar
Marco committed
561
#endif
562
#endif
clang-format's avatar
clang-format committed
563
      default: { break; }
564 565
    }
  }
566

567 568 569
  return VPX_CODEC_OK;
}

570
const char *vpx_svc_get_message(const SvcContext *svc_ctx) {
571
  const SvcInternal_t *const si = get_const_svc_internal(svc_ctx);
572 573 574 575
  if (svc_ctx == NULL || si == NULL) return NULL;
  return si->message_buffer;
}

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

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

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

  svc_log_reset(svc_ctx);

596 597
  number_of_frames = si->psnr_pkt_received;
  if (number_of_frames <= 0) return vpx_svc_get_message(svc_ctx);
598 599

  svc_log(svc_ctx, SVC_LOG_INFO, "\n");
600
  for (i = 0; i < svc_ctx->spatial_layers; ++i) {
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
    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];
625
    // Clear sums for next time.
626 627 628 629 630
    si->bytes_sum[i] = 0;
    for (j = 0; j < COMPONENTS; ++j) {
      si->psnr_sum[i][j] = 0;
      si->sse_sum[i][j] = 0;
    }
631 632 633
  }

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

  svc_log(svc_ctx, SVC_LOG_INFO, "Total Bytes=[%u]\n", bytes_total);
  return vpx_svc_get_message(svc_ctx);
}

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