| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * @file | 12 * @file |
| 13 * VP9 SVC encoding support via libvpx | 13 * VP9 SVC encoding support via libvpx |
| 14 */ | 14 */ |
| 15 | 15 |
| 16 #include <assert.h> | 16 #include <assert.h> |
| 17 #include <math.h> | 17 #include <math.h> |
| 18 #include <stdarg.h> | 18 #include <stdarg.h> |
| 19 #include <stdio.h> | 19 #include <stdio.h> |
| 20 #include <stdlib.h> | 20 #include <stdlib.h> |
| 21 #include <string.h> | 21 #include <string.h> |
| 22 #define VPX_DISABLE_CTRL_TYPECHECKS 1 | 22 #define VPX_DISABLE_CTRL_TYPECHECKS 1 |
| 23 #define VPX_CODEC_DISABLE_COMPAT 1 | 23 #define VPX_CODEC_DISABLE_COMPAT 1 |
| 24 #include "./vpx_config.h" |
| 24 #include "vpx/svc_context.h" | 25 #include "vpx/svc_context.h" |
| 25 #include "vpx/vp8cx.h" | 26 #include "vpx/vp8cx.h" |
| 26 #include "vpx/vpx_encoder.h" | 27 #include "vpx/vpx_encoder.h" |
| 27 #include "vpx_mem/vpx_mem.h" | 28 #include "vpx_mem/vpx_mem.h" |
| 29 #include "vp9/common/vp9_onyxc_int.h" |
| 28 | 30 |
| 29 #ifdef __MINGW32__ | 31 #ifdef __MINGW32__ |
| 30 #define strtok_r strtok_s | 32 #define strtok_r strtok_s |
| 31 #ifndef MINGW_HAS_SECURE_API | 33 #ifndef MINGW_HAS_SECURE_API |
| 32 // proto from /usr/x86_64-w64-mingw32/include/sec_api/string_s.h | 34 // proto from /usr/x86_64-w64-mingw32/include/sec_api/string_s.h |
| 33 _CRTIMP char *__cdecl strtok_s(char *str, const char *delim, char **context); | 35 _CRTIMP char *__cdecl strtok_s(char *str, const char *delim, char **context); |
| 34 #endif /* MINGW_HAS_SECURE_API */ | 36 #endif /* MINGW_HAS_SECURE_API */ |
| 35 #endif /* __MINGW32__ */ | 37 #endif /* __MINGW32__ */ |
| 36 | 38 |
| 37 #ifdef _MSC_VER | 39 #ifdef _MSC_VER |
| (...skipping 20 matching lines...) Expand all Loading... |
| 58 | 60 |
| 59 typedef struct SvcInternal { | 61 typedef struct SvcInternal { |
| 60 char options[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_options | 62 char options[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_options |
| 61 char quantizers[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_quantizers | 63 char quantizers[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_quantizers |
| 62 char scale_factors[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_scale_factors | 64 char scale_factors[OPTION_BUFFER_SIZE]; // set by vpx_svc_set_scale_factors |
| 63 | 65 |
| 64 // values extracted from option, quantizers | 66 // values extracted from option, quantizers |
| 65 int scaling_factor_num[VPX_SS_MAX_LAYERS]; | 67 int scaling_factor_num[VPX_SS_MAX_LAYERS]; |
| 66 int scaling_factor_den[VPX_SS_MAX_LAYERS]; | 68 int scaling_factor_den[VPX_SS_MAX_LAYERS]; |
| 67 int quantizer[VPX_SS_MAX_LAYERS]; | 69 int quantizer[VPX_SS_MAX_LAYERS]; |
| 70 int enable_auto_alt_ref[VPX_SS_MAX_LAYERS]; |
| 68 | 71 |
| 69 // accumulated statistics | 72 // accumulated statistics |
| 70 double psnr_sum[VPX_SS_MAX_LAYERS][COMPONENTS]; // total/Y/U/V | 73 double psnr_sum[VPX_SS_MAX_LAYERS][COMPONENTS]; // total/Y/U/V |
| 71 uint64_t sse_sum[VPX_SS_MAX_LAYERS][COMPONENTS]; | 74 uint64_t sse_sum[VPX_SS_MAX_LAYERS][COMPONENTS]; |
| 72 uint32_t bytes_sum[VPX_SS_MAX_LAYERS]; | 75 uint32_t bytes_sum[VPX_SS_MAX_LAYERS]; |
| 73 | 76 |
| 74 // codec encoding values | 77 // codec encoding values |
| 75 int width; // width of highest layer | 78 int width; // width of highest layer |
| 76 int height; // height of highest layer | 79 int height; // height of highest layer |
| 77 int kf_dist; // distance between keyframes | 80 int kf_dist; // distance between keyframes |
| 78 | 81 |
| 79 // state variables | 82 // state variables |
| 80 int encode_frame_count; | 83 int encode_frame_count; |
| 81 int frame_received; | 84 int frame_received; |
| 82 int frame_within_gop; | 85 int frame_within_gop; |
| 83 vpx_enc_frame_flags_t enc_frame_flags; | |
| 84 int layers; | 86 int layers; |
| 85 int layer; | 87 int layer; |
| 86 int is_keyframe; | 88 int is_keyframe; |
| 87 | 89 |
| 88 FrameData *frame_list; | 90 FrameData *frame_list; |
| 89 FrameData *frame_temp; | 91 FrameData *frame_temp; |
| 90 | 92 |
| 91 char *rc_stats_buf; | 93 char *rc_stats_buf; |
| 92 size_t rc_stats_buf_size; | 94 size_t rc_stats_buf_size; |
| 93 size_t rc_stats_buf_used; | 95 size_t rc_stats_buf_used; |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 if (res == VPX_CODEC_OK && found != svc_ctx->spatial_layers) { | 233 if (res == VPX_CODEC_OK && found != svc_ctx->spatial_layers) { |
| 232 svc_log(svc_ctx, SVC_LOG_ERROR, | 234 svc_log(svc_ctx, SVC_LOG_ERROR, |
| 233 "svc: quantizers: %d values required, but only %d specified\n", | 235 "svc: quantizers: %d values required, but only %d specified\n", |
| 234 svc_ctx->spatial_layers, found); | 236 svc_ctx->spatial_layers, found); |
| 235 res = VPX_CODEC_INVALID_PARAM; | 237 res = VPX_CODEC_INVALID_PARAM; |
| 236 } | 238 } |
| 237 free(input_string); | 239 free(input_string); |
| 238 return res; | 240 return res; |
| 239 } | 241 } |
| 240 | 242 |
| 243 static vpx_codec_err_t parse_auto_alt_ref(SvcContext *svc_ctx, |
| 244 const char *alt_ref_options) { |
| 245 char *input_string; |
| 246 char *token; |
| 247 const char *delim = ","; |
| 248 char *save_ptr; |
| 249 int found = 0, enabled = 0; |
| 250 int i, value; |
| 251 vpx_codec_err_t res = VPX_CODEC_OK; |
| 252 SvcInternal *const si = get_svc_internal(svc_ctx); |
| 253 |
| 254 if (alt_ref_options == NULL || strlen(alt_ref_options) == 0) { |
| 255 return VPX_CODEC_INVALID_PARAM; |
| 256 } else { |
| 257 input_string = strdup(alt_ref_options); |
| 258 } |
| 259 |
| 260 token = strtok_r(input_string, delim, &save_ptr); |
| 261 for (i = 0; i < svc_ctx->spatial_layers; ++i) { |
| 262 if (token != NULL) { |
| 263 value = atoi(token); |
| 264 if (value < 0 || value > 1) { |
| 265 svc_log(svc_ctx, SVC_LOG_ERROR, |
| 266 "enable auto alt ref values: invalid value %s\n", token); |
| 267 res = VPX_CODEC_INVALID_PARAM; |
| 268 break; |
| 269 } |
| 270 token = strtok_r(NULL, delim, &save_ptr); |
| 271 found = i + 1; |
| 272 } else { |
| 273 value = 0; |
| 274 } |
| 275 si->enable_auto_alt_ref[i] = value; |
| 276 if (value > 0) |
| 277 ++enabled; |
| 278 } |
| 279 if (res == VPX_CODEC_OK && found != svc_ctx->spatial_layers) { |
| 280 svc_log(svc_ctx, SVC_LOG_ERROR, |
| 281 "svc: quantizers: %d values required, but only %d specified\n", |
| 282 svc_ctx->spatial_layers, found); |
| 283 res = VPX_CODEC_INVALID_PARAM; |
| 284 } |
| 285 if (enabled > REF_FRAMES - svc_ctx->spatial_layers) { |
| 286 svc_log(svc_ctx, SVC_LOG_ERROR, |
| 287 "svc: auto alt ref: Maxinum %d(REF_FRAMES - layers) layers could" |
| 288 "enabled auto alt reference frame, but % layers are enabled\n", |
| 289 REF_FRAMES - svc_ctx->spatial_layers, enabled); |
| 290 res = VPX_CODEC_INVALID_PARAM; |
| 291 } |
| 292 free(input_string); |
| 293 return res; |
| 294 } |
| 295 |
| 241 static void log_invalid_scale_factor(SvcContext *svc_ctx, const char *value) { | 296 static void log_invalid_scale_factor(SvcContext *svc_ctx, const char *value) { |
| 242 svc_log(svc_ctx, SVC_LOG_ERROR, "svc scale-factors: invalid value %s\n", | 297 svc_log(svc_ctx, SVC_LOG_ERROR, "svc scale-factors: invalid value %s\n", |
| 243 value); | 298 value); |
| 244 } | 299 } |
| 245 | 300 |
| 246 static vpx_codec_err_t parse_scale_factors(SvcContext *svc_ctx, | 301 static vpx_codec_err_t parse_scale_factors(SvcContext *svc_ctx, |
| 247 const char *scale_factors) { | 302 const char *scale_factors) { |
| 248 char *input_string; | 303 char *input_string; |
| 249 char *token; | 304 char *token; |
| 250 const char *delim = ","; | 305 const char *delim = ","; |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 break; | 383 break; |
| 329 } | 384 } |
| 330 if (strcmp("layers", option_name) == 0) { | 385 if (strcmp("layers", option_name) == 0) { |
| 331 svc_ctx->spatial_layers = atoi(option_value); | 386 svc_ctx->spatial_layers = atoi(option_value); |
| 332 } else if (strcmp("scale-factors", option_name) == 0) { | 387 } else if (strcmp("scale-factors", option_name) == 0) { |
| 333 res = parse_scale_factors(svc_ctx, option_value); | 388 res = parse_scale_factors(svc_ctx, option_value); |
| 334 if (res != VPX_CODEC_OK) break; | 389 if (res != VPX_CODEC_OK) break; |
| 335 } else if (strcmp("quantizers", option_name) == 0) { | 390 } else if (strcmp("quantizers", option_name) == 0) { |
| 336 res = parse_quantizer_values(svc_ctx, option_value); | 391 res = parse_quantizer_values(svc_ctx, option_value); |
| 337 if (res != VPX_CODEC_OK) break; | 392 if (res != VPX_CODEC_OK) break; |
| 393 } else if (strcmp("auto-alt-refs", option_name) == 0) { |
| 394 res = parse_auto_alt_ref(svc_ctx, option_value); |
| 395 if (res != VPX_CODEC_OK) break; |
| 338 } else { | 396 } else { |
| 339 svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name); | 397 svc_log(svc_ctx, SVC_LOG_ERROR, "invalid option: %s\n", option_name); |
| 340 res = VPX_CODEC_INVALID_PARAM; | 398 res = VPX_CODEC_INVALID_PARAM; |
| 341 break; | 399 break; |
| 342 } | 400 } |
| 343 option_name = strtok_r(NULL, "=", &input_ptr); | 401 option_name = strtok_r(NULL, "=", &input_ptr); |
| 344 } | 402 } |
| 345 free(input_string); | 403 free(input_string); |
| 346 return res; | 404 return res; |
| 347 } | 405 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 375 } | 433 } |
| 376 strncpy(si->scale_factors, scale_factors, sizeof(si->scale_factors)); | 434 strncpy(si->scale_factors, scale_factors, sizeof(si->scale_factors)); |
| 377 si->scale_factors[sizeof(si->scale_factors) - 1] = '\0'; | 435 si->scale_factors[sizeof(si->scale_factors) - 1] = '\0'; |
| 378 return VPX_CODEC_OK; | 436 return VPX_CODEC_OK; |
| 379 } | 437 } |
| 380 | 438 |
| 381 vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx, | 439 vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx, |
| 382 vpx_codec_iface_t *iface, | 440 vpx_codec_iface_t *iface, |
| 383 vpx_codec_enc_cfg_t *enc_cfg) { | 441 vpx_codec_enc_cfg_t *enc_cfg) { |
| 384 vpx_codec_err_t res; | 442 vpx_codec_err_t res; |
| 443 int i; |
| 385 SvcInternal *const si = get_svc_internal(svc_ctx); | 444 SvcInternal *const si = get_svc_internal(svc_ctx); |
| 386 if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL || | 445 if (svc_ctx == NULL || codec_ctx == NULL || iface == NULL || |
| 387 enc_cfg == NULL) { | 446 enc_cfg == NULL) { |
| 388 return VPX_CODEC_INVALID_PARAM; | 447 return VPX_CODEC_INVALID_PARAM; |
| 389 } | 448 } |
| 390 if (si == NULL) return VPX_CODEC_MEM_ERROR; | 449 if (si == NULL) return VPX_CODEC_MEM_ERROR; |
| 391 | 450 |
| 392 si->codec_ctx = codec_ctx; | 451 si->codec_ctx = codec_ctx; |
| 393 | 452 |
| 394 si->width = enc_cfg->g_w; | 453 si->width = enc_cfg->g_w; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 421 res = parse_options(svc_ctx, si->options); | 480 res = parse_options(svc_ctx, si->options); |
| 422 if (res != VPX_CODEC_OK) return res; | 481 if (res != VPX_CODEC_OK) return res; |
| 423 | 482 |
| 424 si->layers = svc_ctx->spatial_layers; | 483 si->layers = svc_ctx->spatial_layers; |
| 425 | 484 |
| 426 // Assign target bitrate for each layer. We calculate the ratio | 485 // Assign target bitrate for each layer. We calculate the ratio |
| 427 // from the resolution for now. | 486 // from the resolution for now. |
| 428 // TODO(Minghai): Optimize the mechanism of allocating bits after | 487 // TODO(Minghai): Optimize the mechanism of allocating bits after |
| 429 // implementing svc two pass rate control. | 488 // implementing svc two pass rate control. |
| 430 if (si->layers > 1) { | 489 if (si->layers > 1) { |
| 431 int i; | |
| 432 float total = 0; | 490 float total = 0; |
| 433 float alloc_ratio[VPX_SS_MAX_LAYERS] = {0}; | 491 float alloc_ratio[VPX_SS_MAX_LAYERS] = {0}; |
| 434 | 492 |
| 435 assert(si->layers <= VPX_SS_MAX_LAYERS); | 493 assert(si->layers <= VPX_SS_MAX_LAYERS); |
| 436 for (i = 0; i < si->layers; ++i) { | 494 for (i = 0; i < si->layers; ++i) { |
| 437 int pos = i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers; | 495 int pos = i + VPX_SS_MAX_LAYERS - svc_ctx->spatial_layers; |
| 438 if (pos < VPX_SS_MAX_LAYERS && si->scaling_factor_den[pos] > 0) { | 496 if (pos < VPX_SS_MAX_LAYERS && si->scaling_factor_den[pos] > 0) { |
| 439 alloc_ratio[i] = (float)(si->scaling_factor_num[pos] * 1.0 / | 497 alloc_ratio[i] = (float)(si->scaling_factor_num[pos] * 1.0 / |
| 440 si->scaling_factor_den[pos]); | 498 si->scaling_factor_den[pos]); |
| 441 | 499 |
| 442 alloc_ratio[i] *= alloc_ratio[i]; | 500 alloc_ratio[i] *= alloc_ratio[i]; |
| 443 total += alloc_ratio[i]; | 501 total += alloc_ratio[i]; |
| 444 } | 502 } |
| 445 } | 503 } |
| 446 | 504 |
| 447 for (i = 0; i < si->layers; ++i) { | 505 for (i = 0; i < si->layers; ++i) { |
| 448 if (total > 0) { | 506 if (total > 0) { |
| 449 enc_cfg->ss_target_bitrate[i] = (unsigned int) | 507 enc_cfg->ss_target_bitrate[i] = (unsigned int) |
| 450 (enc_cfg->rc_target_bitrate * alloc_ratio[i] / total); | 508 (enc_cfg->rc_target_bitrate * alloc_ratio[i] / total); |
| 451 } | 509 } |
| 452 } | 510 } |
| 453 } | 511 } |
| 454 | 512 |
| 513 for (i = 0; i < si->layers; ++i) |
| 514 enc_cfg->ss_enable_auto_alt_ref[i] = si->enable_auto_alt_ref[i]; |
| 515 |
| 455 // modify encoder configuration | 516 // modify encoder configuration |
| 456 enc_cfg->ss_number_layers = si->layers; | 517 enc_cfg->ss_number_layers = si->layers; |
| 457 enc_cfg->ts_number_layers = 1; // Temporal layers not used in this encoder. | 518 enc_cfg->ts_number_layers = 1; // Temporal layers not used in this encoder. |
| 458 | 519 |
| 459 // TODO(ivanmaltz): determine if these values need to be set explicitly for | 520 // TODO(ivanmaltz): determine if these values need to be set explicitly for |
| 460 // svc, or if the normal default/override mechanism can be used | 521 // svc, or if the normal default/override mechanism can be used |
| 461 enc_cfg->rc_dropframe_thresh = 0; | 522 enc_cfg->rc_dropframe_thresh = 0; |
| 462 enc_cfg->rc_resize_allowed = 0; | 523 enc_cfg->rc_resize_allowed = 0; |
| 463 | 524 |
| 464 if (enc_cfg->g_pass == VPX_RC_ONE_PASS) { | 525 if (enc_cfg->g_pass == VPX_RC_ONE_PASS) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 475 | 536 |
| 476 // Initialize codec | 537 // Initialize codec |
| 477 res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR); | 538 res = vpx_codec_enc_init(codec_ctx, iface, enc_cfg, VPX_CODEC_USE_PSNR); |
| 478 if (res != VPX_CODEC_OK) { | 539 if (res != VPX_CODEC_OK) { |
| 479 svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n"); | 540 svc_log(svc_ctx, SVC_LOG_ERROR, "svc_enc_init error\n"); |
| 480 return res; | 541 return res; |
| 481 } | 542 } |
| 482 | 543 |
| 483 vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1); | 544 vpx_codec_control(codec_ctx, VP9E_SET_SVC, 1); |
| 484 vpx_codec_control(codec_ctx, VP8E_SET_TOKEN_PARTITIONS, 1); | 545 vpx_codec_control(codec_ctx, VP8E_SET_TOKEN_PARTITIONS, 1); |
| 485 vpx_codec_control(codec_ctx, VP8E_SET_ENABLEAUTOALTREF, 0); | |
| 486 | 546 |
| 487 return VPX_CODEC_OK; | 547 return VPX_CODEC_OK; |
| 488 } | 548 } |
| 489 | 549 |
| 490 static void accumulate_frame_size_for_each_layer(SvcInternal *const si, | |
| 491 const uint8_t *const buf, | |
| 492 const size_t size) { | |
| 493 uint8_t marker = buf[size - 1]; | |
| 494 if ((marker & 0xe0) == 0xc0) { | |
| 495 const uint32_t frames = (marker & 0x7) + 1; | |
| 496 const uint32_t mag = ((marker >> 3) & 0x3) + 1; | |
| 497 const size_t index_sz = 2 + mag * frames; | |
| 498 | |
| 499 uint8_t marker2 = buf[size - index_sz]; | |
| 500 | |
| 501 if (size >= index_sz && marker2 == marker) { | |
| 502 // found a valid superframe index | |
| 503 uint32_t i, j; | |
| 504 const uint8_t *x = &buf[size - index_sz + 1]; | |
| 505 | |
| 506 // frames has a maximum of 8 and mag has a maximum of 4. | |
| 507 for (i = 0; i < frames; i++) { | |
| 508 uint32_t this_sz = 0; | |
| 509 | |
| 510 for (j = 0; j < mag; j++) | |
| 511 this_sz |= (*x++) << (j * 8); | |
| 512 si->bytes_sum[i] += this_sz; | |
| 513 } | |
| 514 } | |
| 515 } | |
| 516 } | |
| 517 | |
| 518 // SVC Algorithm flags - these get mapped to VP8_EFLAG_* defined in vp8cx.h | |
| 519 | |
| 520 // encoder should reference the last frame | |
| 521 #define USE_LAST (1 << 0) | |
| 522 | |
| 523 // encoder should reference the alt ref frame | |
| 524 #define USE_ARF (1 << 1) | |
| 525 | |
| 526 // encoder should reference the golden frame | |
| 527 #define USE_GF (1 << 2) | |
| 528 | |
| 529 // encoder should copy current frame to the last frame buffer | |
| 530 #define UPDATE_LAST (1 << 3) | |
| 531 | |
| 532 // encoder should copy current frame to the alt ref frame buffer | |
| 533 #define UPDATE_ARF (1 << 4) | |
| 534 | |
| 535 // encoder should copy current frame to the golden frame | |
| 536 #define UPDATE_GF (1 << 5) | |
| 537 | |
| 538 static int map_vp8_flags(int svc_flags) { | |
| 539 int flags = 0; | |
| 540 | |
| 541 if (!(svc_flags & USE_LAST)) flags |= VP8_EFLAG_NO_REF_LAST; | |
| 542 if (!(svc_flags & USE_ARF)) flags |= VP8_EFLAG_NO_REF_ARF; | |
| 543 if (!(svc_flags & USE_GF)) flags |= VP8_EFLAG_NO_REF_GF; | |
| 544 | |
| 545 if (svc_flags & UPDATE_LAST) { | |
| 546 // last is updated automatically | |
| 547 } else { | |
| 548 flags |= VP8_EFLAG_NO_UPD_LAST; | |
| 549 } | |
| 550 if (svc_flags & UPDATE_ARF) { | |
| 551 flags |= VP8_EFLAG_FORCE_ARF; | |
| 552 } else { | |
| 553 flags |= VP8_EFLAG_NO_UPD_ARF; | |
| 554 } | |
| 555 if (svc_flags & UPDATE_GF) { | |
| 556 flags |= VP8_EFLAG_FORCE_GF; | |
| 557 } else { | |
| 558 flags |= VP8_EFLAG_NO_UPD_GF; | |
| 559 } | |
| 560 return flags; | |
| 561 } | |
| 562 | |
| 563 static void calculate_enc_frame_flags(SvcContext *svc_ctx) { | |
| 564 vpx_enc_frame_flags_t flags = VPX_EFLAG_FORCE_KF; | |
| 565 SvcInternal *const si = get_svc_internal(svc_ctx); | |
| 566 const int is_keyframe = (si->frame_within_gop == 0); | |
| 567 | |
| 568 // keyframe layer zero is identical for all modes | |
| 569 if (is_keyframe && si->layer == 0) { | |
| 570 si->enc_frame_flags = VPX_EFLAG_FORCE_KF; | |
| 571 return; | |
| 572 } | |
| 573 | |
| 574 if (si->layer == 0) { | |
| 575 flags = map_vp8_flags(USE_LAST | UPDATE_LAST); | |
| 576 } else if (is_keyframe) { | |
| 577 flags = map_vp8_flags(USE_ARF | UPDATE_LAST); | |
| 578 } else { | |
| 579 flags = map_vp8_flags(USE_LAST | USE_ARF | UPDATE_LAST); | |
| 580 } | |
| 581 | |
| 582 si->enc_frame_flags = flags; | |
| 583 } | |
| 584 | |
| 585 vpx_codec_err_t vpx_svc_get_layer_resolution(const SvcContext *svc_ctx, | 550 vpx_codec_err_t vpx_svc_get_layer_resolution(const SvcContext *svc_ctx, |
| 586 int layer, | 551 int layer, |
| 587 unsigned int *width, | 552 unsigned int *width, |
| 588 unsigned int *height) { | 553 unsigned int *height) { |
| 589 int w, h, index, num, den; | 554 int w, h, index, num, den; |
| 590 const SvcInternal *const si = get_const_svc_internal(svc_ctx); | 555 const SvcInternal *const si = get_const_svc_internal(svc_ctx); |
| 591 | 556 |
| 592 if (svc_ctx == NULL || si == NULL || width == NULL || height == NULL) { | 557 if (svc_ctx == NULL || si == NULL || width == NULL || height == NULL) { |
| 593 return VPX_CODEC_INVALID_PARAM; | 558 return VPX_CODEC_INVALID_PARAM; |
| 594 } | 559 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 614 | 579 |
| 615 static void set_svc_parameters(SvcContext *svc_ctx, | 580 static void set_svc_parameters(SvcContext *svc_ctx, |
| 616 vpx_codec_ctx_t *codec_ctx) { | 581 vpx_codec_ctx_t *codec_ctx) { |
| 617 int layer, layer_index; | 582 int layer, layer_index; |
| 618 vpx_svc_parameters_t svc_params; | 583 vpx_svc_parameters_t svc_params; |
| 619 SvcInternal *const si = get_svc_internal(svc_ctx); | 584 SvcInternal *const si = get_svc_internal(svc_ctx); |
| 620 | 585 |
| 621 memset(&svc_params, 0, sizeof(svc_params)); | 586 memset(&svc_params, 0, sizeof(svc_params)); |
| 622 svc_params.temporal_layer = 0; | 587 svc_params.temporal_layer = 0; |
| 623 svc_params.spatial_layer = si->layer; | 588 svc_params.spatial_layer = si->layer; |
| 624 svc_params.flags = si->enc_frame_flags; | |
| 625 | 589 |
| 626 layer = si->layer; | 590 layer = si->layer; |
| 627 if (VPX_CODEC_OK != vpx_svc_get_layer_resolution(svc_ctx, layer, | 591 if (VPX_CODEC_OK != vpx_svc_get_layer_resolution(svc_ctx, layer, |
| 628 &svc_params.width, | 592 &svc_params.width, |
| 629 &svc_params.height)) { | 593 &svc_params.height)) { |
| 630 svc_log(svc_ctx, SVC_LOG_ERROR, "vpx_svc_get_layer_resolution failed\n"); | 594 svc_log(svc_ctx, SVC_LOG_ERROR, "vpx_svc_get_layer_resolution failed\n"); |
| 631 } | 595 } |
| 632 layer_index = layer + VPX_SS_MAX_LAYERS - si->layers; | 596 layer_index = layer + VPX_SS_MAX_LAYERS - si->layers; |
| 633 | 597 |
| 634 if (codec_ctx->config.enc->g_pass == VPX_RC_ONE_PASS) { | 598 if (codec_ctx->config.enc->g_pass == VPX_RC_ONE_PASS) { |
| 635 svc_params.min_quantizer = si->quantizer[layer_index]; | 599 svc_params.min_quantizer = si->quantizer[layer_index]; |
| 636 svc_params.max_quantizer = si->quantizer[layer_index]; | 600 svc_params.max_quantizer = si->quantizer[layer_index]; |
| 637 } else { | 601 } else { |
| 638 svc_params.min_quantizer = codec_ctx->config.enc->rc_min_quantizer; | 602 svc_params.min_quantizer = codec_ctx->config.enc->rc_min_quantizer; |
| 639 svc_params.max_quantizer = codec_ctx->config.enc->rc_max_quantizer; | 603 svc_params.max_quantizer = codec_ctx->config.enc->rc_max_quantizer; |
| 640 } | 604 } |
| 641 | 605 |
| 642 svc_params.distance_from_i_frame = si->frame_within_gop; | 606 svc_params.distance_from_i_frame = si->frame_within_gop; |
| 643 | |
| 644 // Use buffer i for layer i LST | |
| 645 svc_params.lst_fb_idx = si->layer; | |
| 646 | |
| 647 // Use buffer i-1 for layer i Alt (Inter-layer prediction) | |
| 648 svc_params.alt_fb_idx = (si->layer > 0) ? si->layer - 1 : 0; | |
| 649 svc_params.gld_fb_idx = svc_params.lst_fb_idx; | |
| 650 | |
| 651 svc_log(svc_ctx, SVC_LOG_DEBUG, "SVC frame: %d, layer: %d, %dx%d, q: %d\n", | |
| 652 si->encode_frame_count, si->layer, svc_params.width, | |
| 653 svc_params.height, svc_params.min_quantizer); | |
| 654 | |
| 655 if (svc_params.flags == VPX_EFLAG_FORCE_KF) { | |
| 656 svc_log(svc_ctx, SVC_LOG_DEBUG, "flags == VPX_EFLAG_FORCE_KF\n"); | |
| 657 } else { | |
| 658 svc_log( | |
| 659 svc_ctx, SVC_LOG_DEBUG, "Using: LST/GLD/ALT [%2d|%2d|%2d]\n", | |
| 660 svc_params.flags & VP8_EFLAG_NO_REF_LAST ? -1 : svc_params.lst_fb_idx, | |
| 661 svc_params.flags & VP8_EFLAG_NO_REF_GF ? -1 : svc_params.gld_fb_idx, | |
| 662 svc_params.flags & VP8_EFLAG_NO_REF_ARF ? -1 : svc_params.alt_fb_idx); | |
| 663 svc_log( | |
| 664 svc_ctx, SVC_LOG_DEBUG, "Updating: LST/GLD/ALT [%2d|%2d|%2d]\n", | |
| 665 svc_params.flags & VP8_EFLAG_NO_UPD_LAST ? -1 : svc_params.lst_fb_idx, | |
| 666 svc_params.flags & VP8_EFLAG_NO_UPD_GF ? -1 : svc_params.gld_fb_idx, | |
| 667 svc_params.flags & VP8_EFLAG_NO_UPD_ARF ? -1 : svc_params.alt_fb_idx); | |
| 668 } | |
| 669 | |
| 670 vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &svc_params); | 607 vpx_codec_control(codec_ctx, VP9E_SET_SVC_PARAMETERS, &svc_params); |
| 671 } | 608 } |
| 672 | 609 |
| 673 /** | 610 /** |
| 674 * Encode a frame into multiple layers | 611 * Encode a frame into multiple layers |
| 675 * Create a superframe containing the individual layers | 612 * Create a superframe containing the individual layers |
| 676 */ | 613 */ |
| 677 vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx, | 614 vpx_codec_err_t vpx_svc_encode(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx, |
| 678 struct vpx_image *rawimg, vpx_codec_pts_t pts, | 615 struct vpx_image *rawimg, vpx_codec_pts_t pts, |
| 679 int64_t duration, int deadline) { | 616 int64_t duration, int deadline) { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 698 if (rawimg != NULL) { | 635 if (rawimg != NULL) { |
| 699 svc_log(svc_ctx, SVC_LOG_DEBUG, | 636 svc_log(svc_ctx, SVC_LOG_DEBUG, |
| 700 "vpx_svc_encode layers: %d, frame_count: %d, " | 637 "vpx_svc_encode layers: %d, frame_count: %d, " |
| 701 "frame_within_gop: %d\n", si->layers, si->encode_frame_count, | 638 "frame_within_gop: %d\n", si->layers, si->encode_frame_count, |
| 702 si->frame_within_gop); | 639 si->frame_within_gop); |
| 703 } | 640 } |
| 704 | 641 |
| 705 if (rawimg != NULL) { | 642 if (rawimg != NULL) { |
| 706 // encode each layer | 643 // encode each layer |
| 707 for (si->layer = 0; si->layer < si->layers; ++si->layer) { | 644 for (si->layer = 0; si->layer < si->layers; ++si->layer) { |
| 708 calculate_enc_frame_flags(svc_ctx); | |
| 709 set_svc_parameters(svc_ctx, codec_ctx); | 645 set_svc_parameters(svc_ctx, codec_ctx); |
| 710 } | 646 } |
| 711 } | 647 } |
| 712 | 648 |
| 713 res = vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0, | 649 res = vpx_codec_encode(codec_ctx, rawimg, pts, (uint32_t)duration, 0, |
| 714 deadline); | 650 deadline); |
| 715 if (res != VPX_CODEC_OK) { | 651 if (res != VPX_CODEC_OK) { |
| 716 return res; | 652 return res; |
| 717 } | 653 } |
| 718 // save compressed data | 654 // save compressed data |
| 719 iter = NULL; | 655 iter = NULL; |
| 720 while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) { | 656 while ((cx_pkt = vpx_codec_get_cx_data(codec_ctx, &iter))) { |
| 721 switch (cx_pkt->kind) { | 657 switch (cx_pkt->kind) { |
| 722 case VPX_CODEC_CX_FRAME_PKT: { | 658 case VPX_CODEC_CX_FRAME_PKT: { |
| 723 fd_list_add(&si->frame_list, fd_create(cx_pkt->data.frame.buf, | 659 fd_list_add(&si->frame_list, fd_create(cx_pkt->data.frame.buf, |
| 724 cx_pkt->data.frame.sz, | 660 cx_pkt->data.frame.sz, |
| 725 cx_pkt->data.frame.flags)); | 661 cx_pkt->data.frame.flags)); |
| 726 accumulate_frame_size_for_each_layer(si, cx_pkt->data.frame.buf, | |
| 727 cx_pkt->data.frame.sz); | |
| 728 | 662 |
| 729 svc_log(svc_ctx, SVC_LOG_DEBUG, "SVC frame: %d, kf: %d, size: %d, " | 663 svc_log(svc_ctx, SVC_LOG_DEBUG, "SVC frame: %d, kf: %d, size: %d, " |
| 730 "pts: %d\n", si->frame_received, | 664 "pts: %d\n", si->frame_received, |
| 731 (cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? 1 : 0, | 665 (cx_pkt->data.frame.flags & VPX_FRAME_IS_KEY) ? 1 : 0, |
| 732 (int)cx_pkt->data.frame.sz, (int)cx_pkt->data.frame.pts); | 666 (int)cx_pkt->data.frame.sz, (int)cx_pkt->data.frame.pts); |
| 733 | 667 |
| 734 ++si->frame_received; | 668 ++si->frame_received; |
| 735 layer_for_psnr = 0; | 669 layer_for_psnr = 0; |
| 736 break; | 670 break; |
| 737 } | 671 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 768 } | 702 } |
| 769 si->rc_stats_buf = p; | 703 si->rc_stats_buf = p; |
| 770 si->rc_stats_buf_size = new_size; | 704 si->rc_stats_buf_size = new_size; |
| 771 } | 705 } |
| 772 | 706 |
| 773 memcpy(si->rc_stats_buf + si->rc_stats_buf_used, | 707 memcpy(si->rc_stats_buf + si->rc_stats_buf_used, |
| 774 cx_pkt->data.twopass_stats.buf, cx_pkt->data.twopass_stats.sz); | 708 cx_pkt->data.twopass_stats.buf, cx_pkt->data.twopass_stats.sz); |
| 775 si->rc_stats_buf_used += cx_pkt->data.twopass_stats.sz; | 709 si->rc_stats_buf_used += cx_pkt->data.twopass_stats.sz; |
| 776 break; | 710 break; |
| 777 } | 711 } |
| 712 case VPX_CODEC_SPATIAL_SVC_LAYER_SIZES: { |
| 713 int i; |
| 714 for (i = 0; i < si->layers; ++i) |
| 715 si->bytes_sum[i] += cx_pkt->data.layer_sizes[i]; |
| 716 break; |
| 717 } |
| 778 default: { | 718 default: { |
| 779 break; | 719 break; |
| 780 } | 720 } |
| 781 } | 721 } |
| 782 } | 722 } |
| 783 | 723 |
| 784 if (rawimg != NULL) { | 724 if (rawimg != NULL) { |
| 785 ++si->frame_within_gop; | 725 ++si->frame_within_gop; |
| 786 ++si->encode_frame_count; | 726 ++si->encode_frame_count; |
| 787 } | 727 } |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 return si->rc_stats_buf_used; | 865 return si->rc_stats_buf_used; |
| 926 } | 866 } |
| 927 | 867 |
| 928 char *vpx_svc_get_rc_stats_buffer(const SvcContext *svc_ctx) { | 868 char *vpx_svc_get_rc_stats_buffer(const SvcContext *svc_ctx) { |
| 929 const SvcInternal *const si = get_const_svc_internal(svc_ctx); | 869 const SvcInternal *const si = get_const_svc_internal(svc_ctx); |
| 930 if (svc_ctx == NULL || si == NULL) return NULL; | 870 if (svc_ctx == NULL || si == NULL) return NULL; |
| 931 return si->rc_stats_buf; | 871 return si->rc_stats_buf; |
| 932 } | 872 } |
| 933 | 873 |
| 934 | 874 |
| OLD | NEW |