1/* 2 * Copyright (c) 2010, Google, Inc. 3 * 4 * This file is part of FFmpeg. 5 * 6 * FFmpeg is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * FFmpeg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with FFmpeg; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21/** 22 * @file 23 * VP8/9 encoder support via libvpx 24 */ 25 26#include "config_components.h" 27 28#define VPX_DISABLE_CTRL_TYPECHECKS 1 29#define VPX_CODEC_DISABLE_COMPAT 1 30#include <vpx/vpx_encoder.h> 31#include <vpx/vp8cx.h> 32 33#include "avcodec.h" 34#include "codec_internal.h" 35#include "encode.h" 36#include "internal.h" 37#include "libavutil/avassert.h" 38#include "libvpx.h" 39#include "packet_internal.h" 40#include "profiles.h" 41#include "libavutil/avstring.h" 42#include "libavutil/base64.h" 43#include "libavutil/common.h" 44#include "libavutil/cpu.h" 45#include "libavutil/fifo.h" 46#include "libavutil/internal.h" 47#include "libavutil/intreadwrite.h" 48#include "libavutil/mathematics.h" 49#include "libavutil/opt.h" 50#include "libavutil/pixdesc.h" 51 52/** 53 * Portion of struct vpx_codec_cx_pkt from vpx_encoder.h. 54 * One encoded frame returned from the library. 55 */ 56struct FrameListData { 57 void *buf; /**< compressed data buffer */ 58 size_t sz; /**< length of compressed data */ 59 int64_t pts; /**< time stamp to show frame 60 (in timebase units) */ 61 unsigned long duration; /**< duration to show frame 62 (in timebase units) */ 63 uint32_t flags; /**< flags for this frame */ 64 uint64_t sse[4]; 65 int have_sse; /**< true if we have pending sse[] */ 66 uint64_t frame_number; 67 struct FrameListData *next; 68}; 69 70typedef struct FrameHDR10Plus { 71 int64_t pts; 72 AVBufferRef *hdr10_plus; 73} FrameHDR10Plus; 74 75typedef struct VPxEncoderContext { 76 AVClass *class; 77 struct vpx_codec_ctx encoder; 78 struct vpx_image rawimg; 79 struct vpx_codec_ctx encoder_alpha; 80 struct vpx_image rawimg_alpha; 81 uint8_t is_alpha; 82 struct vpx_fixed_buf twopass_stats; 83 int deadline; //i.e., RT/GOOD/BEST 84 uint64_t sse[4]; 85 int have_sse; /**< true if we have pending sse[] */ 86 uint64_t frame_number; 87 struct FrameListData *coded_frame_list; 88 struct FrameListData *alpha_coded_frame_list; 89 90 int cpu_used; 91 int sharpness; 92 /** 93 * VP8 specific flags, see VP8F_* below. 94 */ 95 int flags; 96#define VP8F_ERROR_RESILIENT 0x00000001 ///< Enable measures appropriate for streaming over lossy links 97#define VP8F_AUTO_ALT_REF 0x00000002 ///< Enable automatic alternate reference frame generation 98 99 int auto_alt_ref; 100 101 int arnr_max_frames; 102 int arnr_strength; 103 int arnr_type; 104 105 int tune; 106 107 int lag_in_frames; 108 int error_resilient; 109 int crf; 110 int static_thresh; 111 int max_intra_rate; 112 int rc_undershoot_pct; 113 int rc_overshoot_pct; 114 115 AVDictionary *vpx_ts_parameters; 116 int *ts_layer_flags; 117 int current_temporal_idx; 118 119 // VP9-only 120 int lossless; 121 int tile_columns; 122 int tile_rows; 123 int frame_parallel; 124 int aq_mode; 125 int drop_threshold; 126 int noise_sensitivity; 127 int vpx_cs; 128 float level; 129 int row_mt; 130 int tune_content; 131 int corpus_complexity; 132 int tpl_model; 133 AVFifo *hdr10_plus_fifo; 134 /** 135 * If the driver does not support ROI then warn the first time we 136 * encounter a frame with ROI side data. 137 */ 138 int roi_warned; 139#if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT) 140 vpx_svc_ref_frame_config_t ref_frame_config; 141#endif 142} VPxContext; 143 144/** String mappings for enum vp8e_enc_control_id */ 145static const char *const ctlidstr[] = { 146 [VP8E_SET_CPUUSED] = "VP8E_SET_CPUUSED", 147 [VP8E_SET_ENABLEAUTOALTREF] = "VP8E_SET_ENABLEAUTOALTREF", 148 [VP8E_SET_NOISE_SENSITIVITY] = "VP8E_SET_NOISE_SENSITIVITY", 149 [VP8E_SET_STATIC_THRESHOLD] = "VP8E_SET_STATIC_THRESHOLD", 150 [VP8E_SET_TOKEN_PARTITIONS] = "VP8E_SET_TOKEN_PARTITIONS", 151 [VP8E_SET_ARNR_MAXFRAMES] = "VP8E_SET_ARNR_MAXFRAMES", 152 [VP8E_SET_ARNR_STRENGTH] = "VP8E_SET_ARNR_STRENGTH", 153 [VP8E_SET_ARNR_TYPE] = "VP8E_SET_ARNR_TYPE", 154 [VP8E_SET_TUNING] = "VP8E_SET_TUNING", 155 [VP8E_SET_CQ_LEVEL] = "VP8E_SET_CQ_LEVEL", 156 [VP8E_SET_MAX_INTRA_BITRATE_PCT] = "VP8E_SET_MAX_INTRA_BITRATE_PCT", 157 [VP8E_SET_SHARPNESS] = "VP8E_SET_SHARPNESS", 158 [VP8E_SET_TEMPORAL_LAYER_ID] = "VP8E_SET_TEMPORAL_LAYER_ID", 159#if CONFIG_LIBVPX_VP9_ENCODER 160 [VP9E_SET_LOSSLESS] = "VP9E_SET_LOSSLESS", 161 [VP9E_SET_TILE_COLUMNS] = "VP9E_SET_TILE_COLUMNS", 162 [VP9E_SET_TILE_ROWS] = "VP9E_SET_TILE_ROWS", 163 [VP9E_SET_FRAME_PARALLEL_DECODING] = "VP9E_SET_FRAME_PARALLEL_DECODING", 164 [VP9E_SET_AQ_MODE] = "VP9E_SET_AQ_MODE", 165 [VP9E_SET_COLOR_SPACE] = "VP9E_SET_COLOR_SPACE", 166 [VP9E_SET_SVC_LAYER_ID] = "VP9E_SET_SVC_LAYER_ID", 167#if VPX_ENCODER_ABI_VERSION >= 12 168 [VP9E_SET_SVC_PARAMETERS] = "VP9E_SET_SVC_PARAMETERS", 169 [VP9E_SET_SVC_REF_FRAME_CONFIG] = "VP9E_SET_SVC_REF_FRAME_CONFIG", 170#endif 171 [VP9E_SET_SVC] = "VP9E_SET_SVC", 172#if VPX_ENCODER_ABI_VERSION >= 11 173 [VP9E_SET_COLOR_RANGE] = "VP9E_SET_COLOR_RANGE", 174#endif 175#if VPX_ENCODER_ABI_VERSION >= 12 176 [VP9E_SET_TARGET_LEVEL] = "VP9E_SET_TARGET_LEVEL", 177 [VP9E_GET_LEVEL] = "VP9E_GET_LEVEL", 178#endif 179#ifdef VPX_CTRL_VP9E_SET_ROW_MT 180 [VP9E_SET_ROW_MT] = "VP9E_SET_ROW_MT", 181#endif 182#ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT 183 [VP9E_SET_TUNE_CONTENT] = "VP9E_SET_TUNE_CONTENT", 184#endif 185#ifdef VPX_CTRL_VP9E_SET_TPL 186 [VP9E_SET_TPL] = "VP9E_SET_TPL", 187#endif 188#endif 189}; 190 191static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc) 192{ 193 VPxContext *ctx = avctx->priv_data; 194 const char *error = vpx_codec_error(&ctx->encoder); 195 const char *detail = vpx_codec_error_detail(&ctx->encoder); 196 197 av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error); 198 if (detail) 199 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail); 200} 201 202static av_cold void dump_enc_cfg(AVCodecContext *avctx, 203 const struct vpx_codec_enc_cfg *cfg, 204 int level) 205{ 206 int width = -30; 207 int i; 208 209 av_log(avctx, level, "vpx_codec_enc_cfg\n"); 210 av_log(avctx, level, "generic settings\n" 211 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n" 212#if CONFIG_LIBVPX_VP9_ENCODER 213 " %*s%u\n %*s%u\n" 214#endif 215 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n", 216 width, "g_usage:", cfg->g_usage, 217 width, "g_threads:", cfg->g_threads, 218 width, "g_profile:", cfg->g_profile, 219 width, "g_w:", cfg->g_w, 220 width, "g_h:", cfg->g_h, 221#if CONFIG_LIBVPX_VP9_ENCODER 222 width, "g_bit_depth:", cfg->g_bit_depth, 223 width, "g_input_bit_depth:", cfg->g_input_bit_depth, 224#endif 225 width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den, 226 width, "g_error_resilient:", cfg->g_error_resilient, 227 width, "g_pass:", cfg->g_pass, 228 width, "g_lag_in_frames:", cfg->g_lag_in_frames); 229 av_log(avctx, level, "rate control settings\n" 230 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n" 231 " %*s%d\n %*s%p(%"SIZE_SPECIFIER")\n %*s%u\n", 232 width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh, 233 width, "rc_resize_allowed:", cfg->rc_resize_allowed, 234 width, "rc_resize_up_thresh:", cfg->rc_resize_up_thresh, 235 width, "rc_resize_down_thresh:", cfg->rc_resize_down_thresh, 236 width, "rc_end_usage:", cfg->rc_end_usage, 237 width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz, 238 width, "rc_target_bitrate:", cfg->rc_target_bitrate); 239 av_log(avctx, level, "quantizer settings\n" 240 " %*s%u\n %*s%u\n", 241 width, "rc_min_quantizer:", cfg->rc_min_quantizer, 242 width, "rc_max_quantizer:", cfg->rc_max_quantizer); 243 av_log(avctx, level, "bitrate tolerance\n" 244 " %*s%u\n %*s%u\n", 245 width, "rc_undershoot_pct:", cfg->rc_undershoot_pct, 246 width, "rc_overshoot_pct:", cfg->rc_overshoot_pct); 247 av_log(avctx, level, "temporal layering settings\n" 248 " %*s%u\n", width, "ts_number_layers:", cfg->ts_number_layers); 249 if (avctx->codec_id == AV_CODEC_ID_VP8) { 250 av_log(avctx, level, 251 "\n %*s", width, "ts_target_bitrate:"); 252 for (i = 0; i < VPX_TS_MAX_LAYERS; i++) 253 av_log(avctx, level, 254 "%u ", cfg->ts_target_bitrate[i]); 255 } 256#if (VPX_ENCODER_ABI_VERSION >= 12) && CONFIG_LIBVPX_VP9_ENCODER 257 if (avctx->codec_id == AV_CODEC_ID_VP9) { 258 av_log(avctx, level, 259 "\n %*s", width, "layer_target_bitrate:"); 260 for (i = 0; i < VPX_TS_MAX_LAYERS; i++) 261 av_log(avctx, level, 262 "%u ", cfg->layer_target_bitrate[i]); 263 } 264#endif 265 av_log(avctx, level, "\n"); 266 av_log(avctx, level, 267 "\n %*s", width, "ts_rate_decimator:"); 268 for (i = 0; i < VPX_TS_MAX_LAYERS; i++) 269 av_log(avctx, level, "%u ", cfg->ts_rate_decimator[i]); 270 av_log(avctx, level, "\n"); 271 av_log(avctx, level, 272 "\n %*s%u\n", width, "ts_periodicity:", cfg->ts_periodicity); 273 av_log(avctx, level, 274 "\n %*s", width, "ts_layer_id:"); 275 for (i = 0; i < VPX_TS_MAX_PERIODICITY; i++) 276 av_log(avctx, level, "%u ", cfg->ts_layer_id[i]); 277 av_log(avctx, level, "\n"); 278 av_log(avctx, level, "decoder buffer model\n" 279 " %*s%u\n %*s%u\n %*s%u\n", 280 width, "rc_buf_sz:", cfg->rc_buf_sz, 281 width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz, 282 width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz); 283 av_log(avctx, level, "2 pass rate control settings\n" 284 " %*s%u\n %*s%u\n %*s%u\n", 285 width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct, 286 width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct, 287 width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct); 288#if VPX_ENCODER_ABI_VERSION >= 14 289 av_log(avctx, level, " %*s%u\n", 290 width, "rc_2pass_vbr_corpus_complexity:", cfg->rc_2pass_vbr_corpus_complexity); 291#endif 292 av_log(avctx, level, "keyframing settings\n" 293 " %*s%d\n %*s%u\n %*s%u\n", 294 width, "kf_mode:", cfg->kf_mode, 295 width, "kf_min_dist:", cfg->kf_min_dist, 296 width, "kf_max_dist:", cfg->kf_max_dist); 297 av_log(avctx, level, "\n"); 298} 299 300static void coded_frame_add(void *list, struct FrameListData *cx_frame) 301{ 302 struct FrameListData **p = list; 303 304 while (*p) 305 p = &(*p)->next; 306 *p = cx_frame; 307 cx_frame->next = NULL; 308} 309 310static av_cold void free_coded_frame(struct FrameListData *cx_frame) 311{ 312 av_freep(&cx_frame->buf); 313 av_freep(&cx_frame); 314} 315 316static av_cold void free_frame_list(struct FrameListData *list) 317{ 318 struct FrameListData *p = list; 319 320 while (p) { 321 list = list->next; 322 free_coded_frame(p); 323 p = list; 324 } 325} 326 327static av_cold void free_hdr10_plus_fifo(AVFifo **fifo) 328{ 329 FrameHDR10Plus frame_hdr10_plus; 330 while (av_fifo_read(*fifo, &frame_hdr10_plus, 1) >= 0) 331 av_buffer_unref(&frame_hdr10_plus.hdr10_plus); 332 av_fifo_freep2(fifo); 333} 334 335static int copy_hdr10_plus_to_pkt(AVFifo *fifo, AVPacket *pkt) 336{ 337 FrameHDR10Plus frame_hdr10_plus; 338 uint8_t *data; 339 if (!pkt || av_fifo_peek(fifo, &frame_hdr10_plus, 1, 0) < 0) 340 return 0; 341 if (!frame_hdr10_plus.hdr10_plus || frame_hdr10_plus.pts != pkt->pts) 342 return 0; 343 av_fifo_drain2(fifo, 1); 344 345 data = av_packet_new_side_data(pkt, AV_PKT_DATA_DYNAMIC_HDR10_PLUS, frame_hdr10_plus.hdr10_plus->size); 346 if (!data) { 347 av_buffer_unref(&frame_hdr10_plus.hdr10_plus); 348 return AVERROR(ENOMEM); 349 } 350 351 memcpy(data, frame_hdr10_plus.hdr10_plus->data, frame_hdr10_plus.hdr10_plus->size); 352 av_buffer_unref(&frame_hdr10_plus.hdr10_plus); 353 return 0; 354} 355 356static av_cold int codecctl_int(AVCodecContext *avctx, 357 enum vp8e_enc_control_id id, int val) 358{ 359 VPxContext *ctx = avctx->priv_data; 360 char buf[80]; 361 int width = -30; 362 int res; 363 364 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]); 365 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val); 366 367 res = vpx_codec_control(&ctx->encoder, id, val); 368 if (res != VPX_CODEC_OK) { 369 snprintf(buf, sizeof(buf), "Failed to set %s codec control", 370 ctlidstr[id]); 371 log_encoder_error(avctx, buf); 372 return AVERROR(EINVAL); 373 } 374 375 if (ctx->is_alpha) { 376 int res_alpha = vpx_codec_control(&ctx->encoder_alpha, id, val); 377 if (res_alpha != VPX_CODEC_OK) { 378 snprintf(buf, sizeof(buf), "Failed to set %s alpha codec control", 379 ctlidstr[id]); 380 log_encoder_error(avctx, buf); 381 return AVERROR(EINVAL); 382 } 383 } 384 385 return 0; 386} 387 388#if VPX_ENCODER_ABI_VERSION >= 12 389static av_cold int codecctl_intp(AVCodecContext *avctx, 390 enum vp8e_enc_control_id id, int *val) 391{ 392 VPxContext *ctx = avctx->priv_data; 393 char buf[80]; 394 int width = -30; 395 int res; 396 397 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]); 398 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, *val); 399 400 res = vpx_codec_control(&ctx->encoder, id, val); 401 if (res != VPX_CODEC_OK) { 402 snprintf(buf, sizeof(buf), "Failed to set %s codec control", 403 ctlidstr[id]); 404 log_encoder_error(avctx, buf); 405 return AVERROR(EINVAL); 406 } 407 408 if (ctx->is_alpha) { 409 int res_alpha = vpx_codec_control(&ctx->encoder_alpha, id, val); 410 if (res_alpha != VPX_CODEC_OK) { 411 snprintf(buf, sizeof(buf), "Failed to set %s alpha codec control", 412 ctlidstr[id]); 413 log_encoder_error(avctx, buf); 414 return AVERROR(EINVAL); 415 } 416 } 417 418 return 0; 419} 420#endif 421 422static av_cold int vpx_free(AVCodecContext *avctx) 423{ 424 VPxContext *ctx = avctx->priv_data; 425 426#if VPX_ENCODER_ABI_VERSION >= 12 427 if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->level >= 0 && 428 !(avctx->flags & AV_CODEC_FLAG_PASS1)) { 429 int level_out = 0; 430 if (!codecctl_intp(avctx, VP9E_GET_LEVEL, &level_out)) 431 av_log(avctx, AV_LOG_INFO, "Encoded level %.1f\n", level_out * 0.1); 432 } 433#endif 434 435 av_freep(&ctx->ts_layer_flags); 436 437 vpx_codec_destroy(&ctx->encoder); 438 if (ctx->is_alpha) { 439 vpx_codec_destroy(&ctx->encoder_alpha); 440 av_freep(&ctx->rawimg_alpha.planes[VPX_PLANE_U]); 441 av_freep(&ctx->rawimg_alpha.planes[VPX_PLANE_V]); 442 } 443 av_freep(&ctx->twopass_stats.buf); 444 av_freep(&avctx->stats_out); 445 free_frame_list(ctx->coded_frame_list); 446 free_frame_list(ctx->alpha_coded_frame_list); 447 if (ctx->hdr10_plus_fifo) 448 free_hdr10_plus_fifo(&ctx->hdr10_plus_fifo); 449 return 0; 450} 451 452static void vp8_ts_parse_int_array(int *dest, char *value, size_t value_len, int max_entries) 453{ 454 int dest_idx = 0; 455 char *saveptr = NULL; 456 char *token = av_strtok(value, ",", &saveptr); 457 458 while (token && dest_idx < max_entries) { 459 dest[dest_idx++] = strtoul(token, NULL, 10); 460 token = av_strtok(NULL, ",", &saveptr); 461 } 462} 463 464#if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT) 465static void vp8_ts_parse_int64_array(int64_t *dest, char *value, size_t value_len, int max_entries) 466{ 467 int dest_idx = 0; 468 char *saveptr = NULL; 469 char *token = av_strtok(value, ",", &saveptr); 470 471 while (token && dest_idx < max_entries) { 472 dest[dest_idx++] = strtoull(token, NULL, 10); 473 token = av_strtok(NULL, ",", &saveptr); 474 } 475} 476#endif 477 478static void set_temporal_layer_pattern(int layering_mode, vpx_codec_enc_cfg_t *cfg, 479 int *layer_flags, int *flag_periodicity) 480{ 481 switch (layering_mode) { 482 case 2: { 483 /** 484 * 2-layers, 2-frame period. 485 */ 486 static const int ids[2] = { 0, 1 }; 487 cfg->ts_periodicity = 2; 488 *flag_periodicity = 2; 489 cfg->ts_number_layers = 2; 490 cfg->ts_rate_decimator[0] = 2; 491 cfg->ts_rate_decimator[1] = 1; 492 memcpy(cfg->ts_layer_id, ids, sizeof(ids)); 493 494 layer_flags[0] = 495 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | 496 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF; 497 layer_flags[1] = 498 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_GF | 499 VP8_EFLAG_NO_UPD_LAST | 500 VP8_EFLAG_NO_REF_ARF | VP8_EFLAG_NO_REF_GF; 501 break; 502 } 503 case 3: { 504 /** 505 * 3-layers structure with one reference frame. 506 * This works same as temporal_layering_mode 3. 507 * 508 * 3-layers, 4-frame period. 509 */ 510 static const int ids[4] = { 0, 2, 1, 2 }; 511 cfg->ts_periodicity = 4; 512 *flag_periodicity = 4; 513 cfg->ts_number_layers = 3; 514 cfg->ts_rate_decimator[0] = 4; 515 cfg->ts_rate_decimator[1] = 2; 516 cfg->ts_rate_decimator[2] = 1; 517 memcpy(cfg->ts_layer_id, ids, sizeof(ids)); 518 519 /** 520 * 0=L, 1=GF, 2=ARF, 521 * Intra-layer prediction disabled. 522 */ 523 layer_flags[0] = 524 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | 525 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF; 526 layer_flags[1] = 527 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | 528 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | 529 VP8_EFLAG_NO_UPD_ARF; 530 layer_flags[2] = 531 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | 532 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST; 533 layer_flags[3] = 534 VP8_EFLAG_NO_REF_LAST | VP8_EFLAG_NO_REF_ARF | 535 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | 536 VP8_EFLAG_NO_UPD_ARF; 537 break; 538 } 539 case 4: { 540 /** 541 * 3-layers structure. 542 * added dependency between the two TL2 frames (on top of case 3). 543 * 3-layers, 4-frame period. 544 */ 545 static const int ids[4] = { 0, 2, 1, 2 }; 546 cfg->ts_periodicity = 4; 547 *flag_periodicity = 4; 548 cfg->ts_number_layers = 3; 549 cfg->ts_rate_decimator[0] = 4; 550 cfg->ts_rate_decimator[1] = 2; 551 cfg->ts_rate_decimator[2] = 1; 552 memcpy(cfg->ts_layer_id, ids, sizeof(ids)); 553 554 /** 555 * 0=L, 1=GF, 2=ARF, Intra-layer prediction disabled. 556 */ 557 layer_flags[0] = 558 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | 559 VP8_EFLAG_NO_UPD_GF | VP8_EFLAG_NO_UPD_ARF; 560 layer_flags[1] = 561 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | 562 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF; 563 layer_flags[2] = 564 VP8_EFLAG_NO_REF_GF | VP8_EFLAG_NO_REF_ARF | 565 VP8_EFLAG_NO_UPD_ARF | VP8_EFLAG_NO_UPD_LAST; 566 layer_flags[3] = 567 VP8_EFLAG_NO_REF_LAST | 568 VP8_EFLAG_NO_UPD_LAST | VP8_EFLAG_NO_UPD_GF | 569 VP8_EFLAG_NO_UPD_ARF; 570 break; 571 } 572 default: 573 /** 574 * do not change the layer_flags or the flag_periodicity in this case; 575 * it might be that the code is using external flags to be used. 576 */ 577 break; 578 579 } 580} 581 582static int vpx_ts_param_parse(VPxContext *ctx, struct vpx_codec_enc_cfg *enccfg, 583 char *key, char *value, enum AVCodecID codec_id) 584{ 585 size_t value_len = strlen(value); 586 int ts_layering_mode = 0; 587 588 if (!value_len) 589 return -1; 590 591 if (!strcmp(key, "ts_number_layers")) 592 enccfg->ts_number_layers = strtoul(value, &value, 10); 593 else if (!strcmp(key, "ts_target_bitrate")) { 594 if (codec_id == AV_CODEC_ID_VP8) 595 vp8_ts_parse_int_array(enccfg->ts_target_bitrate, value, value_len, VPX_TS_MAX_LAYERS); 596#if (VPX_ENCODER_ABI_VERSION >= 12) && CONFIG_LIBVPX_VP9_ENCODER 597 if (codec_id == AV_CODEC_ID_VP9) 598 vp8_ts_parse_int_array(enccfg->layer_target_bitrate, value, value_len, VPX_TS_MAX_LAYERS); 599#endif 600 } else if (!strcmp(key, "ts_rate_decimator")) { 601 vp8_ts_parse_int_array(enccfg->ts_rate_decimator, value, value_len, VPX_TS_MAX_LAYERS); 602 } else if (!strcmp(key, "ts_periodicity")) { 603 enccfg->ts_periodicity = strtoul(value, &value, 10); 604 } else if (!strcmp(key, "ts_layer_id")) { 605 vp8_ts_parse_int_array(enccfg->ts_layer_id, value, value_len, VPX_TS_MAX_PERIODICITY); 606 } else if (!strcmp(key, "ts_layering_mode")) { 607 /* option for pre-defined temporal structures in function set_temporal_layer_pattern. */ 608 ts_layering_mode = strtoul(value, &value, 4); 609 } 610 611#if (VPX_ENCODER_ABI_VERSION >= 12) && CONFIG_LIBVPX_VP9_ENCODER 612 enccfg->temporal_layering_mode = VP9E_TEMPORAL_LAYERING_MODE_BYPASS; // only bypass mode is supported for now. 613 enccfg->ss_number_layers = 1; // TODO: add spatial scalability support. 614#endif 615 if (ts_layering_mode) { 616 // make sure the ts_layering_mode comes at the end of the ts_parameter string to ensure that 617 // correct configuration is done. 618 ctx->ts_layer_flags = av_malloc_array(VPX_TS_MAX_PERIODICITY, sizeof(*ctx->ts_layer_flags)); 619 set_temporal_layer_pattern(ts_layering_mode, enccfg, ctx->ts_layer_flags, &enccfg->ts_periodicity); 620 } 621 622 return 0; 623} 624 625#if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT) 626static int vpx_ref_frame_config_set_value(vpx_svc_ref_frame_config_t *ref_frame_config, 627 int ss_number_layers, char *key, char *value) 628{ 629 size_t value_len = strlen(value); 630 631 if (!value_len) 632 return AVERROR(EINVAL); 633 634 if (!strcmp(key, "rfc_update_buffer_slot")) { 635 vp8_ts_parse_int_array(ref_frame_config->update_buffer_slot, value, value_len, ss_number_layers); 636 } else if (!strcmp(key, "rfc_update_last")) { 637 vp8_ts_parse_int_array(ref_frame_config->update_last, value, value_len, ss_number_layers); 638 } else if (!strcmp(key, "rfc_update_golden")) { 639 vp8_ts_parse_int_array(ref_frame_config->update_golden, value, value_len, ss_number_layers); 640 } else if (!strcmp(key, "rfc_update_alt_ref")) { 641 vp8_ts_parse_int_array(ref_frame_config->update_alt_ref, value, value_len, ss_number_layers); 642 } else if (!strcmp(key, "rfc_lst_fb_idx")) { 643 vp8_ts_parse_int_array(ref_frame_config->lst_fb_idx, value, value_len, ss_number_layers); 644 } else if (!strcmp(key, "rfc_gld_fb_idx")) { 645 vp8_ts_parse_int_array(ref_frame_config->gld_fb_idx, value, value_len, ss_number_layers); 646 } else if (!strcmp(key, "rfc_alt_fb_idx")) { 647 vp8_ts_parse_int_array(ref_frame_config->alt_fb_idx, value, value_len, ss_number_layers); 648 } else if (!strcmp(key, "rfc_reference_last")) { 649 vp8_ts_parse_int_array(ref_frame_config->reference_last, value, value_len, ss_number_layers); 650 } else if (!strcmp(key, "rfc_reference_golden")) { 651 vp8_ts_parse_int_array(ref_frame_config->reference_golden, value, value_len, ss_number_layers); 652 } else if (!strcmp(key, "rfc_reference_alt_ref")) { 653 vp8_ts_parse_int_array(ref_frame_config->reference_alt_ref, value, value_len, ss_number_layers); 654 } else if (!strcmp(key, "rfc_reference_duration")) { 655 vp8_ts_parse_int64_array(ref_frame_config->duration, value, value_len, ss_number_layers); 656 } 657 658 return 0; 659} 660 661static int vpx_parse_ref_frame_config_element(vpx_svc_ref_frame_config_t *ref_frame_config, 662 int ss_number_layers, const char **buf) 663{ 664 const char key_val_sep[] = "="; 665 const char pairs_sep[] = ":"; 666 char *key = av_get_token(buf, key_val_sep); 667 char *val = NULL; 668 int ret; 669 670 if (key && *key && strspn(*buf, key_val_sep)) { 671 (*buf)++; 672 val = av_get_token(buf, pairs_sep); 673 } 674 675 if (key && *key && val && *val) 676 ret = vpx_ref_frame_config_set_value(ref_frame_config, ss_number_layers, key, val); 677 else 678 ret = AVERROR(EINVAL); 679 680 av_freep(&key); 681 av_freep(&val); 682 683 return ret; 684} 685 686static int vpx_parse_ref_frame_config(vpx_svc_ref_frame_config_t *ref_frame_config, 687 int ss_number_layers, const char *str) 688{ 689 int ret = 0; 690 691 while (*str) { 692 ret = 693 vpx_parse_ref_frame_config_element(ref_frame_config, ss_number_layers, &str); 694 if (ret < 0) 695 return ret; 696 697 if (*str) 698 str++; 699 } 700 701 return ret; 702} 703#endif 704 705#if CONFIG_LIBVPX_VP9_ENCODER 706static int set_pix_fmt(AVCodecContext *avctx, vpx_codec_caps_t codec_caps, 707 struct vpx_codec_enc_cfg *enccfg, vpx_codec_flags_t *flags, 708 vpx_img_fmt_t *img_fmt) 709{ 710 VPxContext av_unused *ctx = avctx->priv_data; 711 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(avctx->pix_fmt); 712 enccfg->g_bit_depth = enccfg->g_input_bit_depth = desc->comp[0].depth; 713 switch (avctx->pix_fmt) { 714 case AV_PIX_FMT_YUV420P: 715 case AV_PIX_FMT_YUVA420P: 716 enccfg->g_profile = 0; 717 *img_fmt = VPX_IMG_FMT_I420; 718 return 0; 719 case AV_PIX_FMT_YUV422P: 720 enccfg->g_profile = 1; 721 *img_fmt = VPX_IMG_FMT_I422; 722 return 0; 723 case AV_PIX_FMT_YUV440P: 724 enccfg->g_profile = 1; 725 *img_fmt = VPX_IMG_FMT_I440; 726 return 0; 727 case AV_PIX_FMT_GBRP: 728 ctx->vpx_cs = VPX_CS_SRGB; 729 case AV_PIX_FMT_YUV444P: 730 enccfg->g_profile = 1; 731 *img_fmt = VPX_IMG_FMT_I444; 732 return 0; 733 case AV_PIX_FMT_YUV420P10: 734 case AV_PIX_FMT_YUV420P12: 735 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) { 736 enccfg->g_profile = 2; 737 *img_fmt = VPX_IMG_FMT_I42016; 738 *flags |= VPX_CODEC_USE_HIGHBITDEPTH; 739 return 0; 740 } 741 break; 742 case AV_PIX_FMT_YUV422P10: 743 case AV_PIX_FMT_YUV422P12: 744 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) { 745 enccfg->g_profile = 3; 746 *img_fmt = VPX_IMG_FMT_I42216; 747 *flags |= VPX_CODEC_USE_HIGHBITDEPTH; 748 return 0; 749 } 750 break; 751 case AV_PIX_FMT_YUV440P10: 752 case AV_PIX_FMT_YUV440P12: 753 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) { 754 enccfg->g_profile = 3; 755 *img_fmt = VPX_IMG_FMT_I44016; 756 *flags |= VPX_CODEC_USE_HIGHBITDEPTH; 757 return 0; 758 } 759 break; 760 case AV_PIX_FMT_GBRP10: 761 case AV_PIX_FMT_GBRP12: 762 ctx->vpx_cs = VPX_CS_SRGB; 763 case AV_PIX_FMT_YUV444P10: 764 case AV_PIX_FMT_YUV444P12: 765 if (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH) { 766 enccfg->g_profile = 3; 767 *img_fmt = VPX_IMG_FMT_I44416; 768 *flags |= VPX_CODEC_USE_HIGHBITDEPTH; 769 return 0; 770 } 771 break; 772 default: 773 break; 774 } 775 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n"); 776 return AVERROR_INVALIDDATA; 777} 778 779static void set_colorspace(AVCodecContext *avctx) 780{ 781 enum vpx_color_space vpx_cs; 782 VPxContext *ctx = avctx->priv_data; 783 784 if (ctx->vpx_cs) { 785 vpx_cs = ctx->vpx_cs; 786 } else { 787 switch (avctx->colorspace) { 788 case AVCOL_SPC_RGB: vpx_cs = VPX_CS_SRGB; break; 789 case AVCOL_SPC_BT709: vpx_cs = VPX_CS_BT_709; break; 790 case AVCOL_SPC_UNSPECIFIED: vpx_cs = VPX_CS_UNKNOWN; break; 791 case AVCOL_SPC_RESERVED: vpx_cs = VPX_CS_RESERVED; break; 792 case AVCOL_SPC_BT470BG: vpx_cs = VPX_CS_BT_601; break; 793 case AVCOL_SPC_SMPTE170M: vpx_cs = VPX_CS_SMPTE_170; break; 794 case AVCOL_SPC_SMPTE240M: vpx_cs = VPX_CS_SMPTE_240; break; 795 case AVCOL_SPC_BT2020_NCL: vpx_cs = VPX_CS_BT_2020; break; 796 default: 797 av_log(avctx, AV_LOG_WARNING, "Unsupported colorspace (%d)\n", 798 avctx->colorspace); 799 return; 800 } 801 } 802 codecctl_int(avctx, VP9E_SET_COLOR_SPACE, vpx_cs); 803} 804 805#if VPX_ENCODER_ABI_VERSION >= 11 806static void set_color_range(AVCodecContext *avctx) 807{ 808 enum vpx_color_range vpx_cr; 809 switch (avctx->color_range) { 810 case AVCOL_RANGE_UNSPECIFIED: 811 case AVCOL_RANGE_MPEG: vpx_cr = VPX_CR_STUDIO_RANGE; break; 812 case AVCOL_RANGE_JPEG: vpx_cr = VPX_CR_FULL_RANGE; break; 813 default: 814 av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n", 815 avctx->color_range); 816 return; 817 } 818 819 codecctl_int(avctx, VP9E_SET_COLOR_RANGE, vpx_cr); 820} 821#endif 822#endif 823 824/** 825 * Set the target bitrate to VPX library default. Also set CRF to 32 if needed. 826 */ 827static void set_vp8_defaults(AVCodecContext *avctx, 828 struct vpx_codec_enc_cfg *enccfg) 829{ 830 VPxContext *ctx = avctx->priv_data; 831 av_assert0(!avctx->bit_rate); 832 avctx->bit_rate = enccfg->rc_target_bitrate * 1000; 833 if (enccfg->rc_end_usage == VPX_CQ) { 834 av_log(avctx, AV_LOG_WARNING, 835 "Bitrate not specified for constrained quality mode, using default of %dkbit/sec\n", 836 enccfg->rc_target_bitrate); 837 } else { 838 enccfg->rc_end_usage = VPX_CQ; 839 ctx->crf = 32; 840 av_log(avctx, AV_LOG_WARNING, 841 "Neither bitrate nor constrained quality specified, using default CRF of %d and bitrate of %dkbit/sec\n", 842 ctx->crf, enccfg->rc_target_bitrate); 843 } 844} 845 846 847#if CONFIG_LIBVPX_VP9_ENCODER 848/** 849 * Keep the target bitrate at 0 to engage constant quality mode. If CRF is not 850 * set, use 32. 851 */ 852static void set_vp9_defaults(AVCodecContext *avctx, 853 struct vpx_codec_enc_cfg *enccfg) 854{ 855 VPxContext *ctx = avctx->priv_data; 856 av_assert0(!avctx->bit_rate); 857 if (enccfg->rc_end_usage != VPX_Q && ctx->lossless < 0) { 858 enccfg->rc_end_usage = VPX_Q; 859 ctx->crf = 32; 860 av_log(avctx, AV_LOG_WARNING, 861 "Neither bitrate nor constrained quality specified, using default CRF of %d\n", 862 ctx->crf); 863 } 864} 865#endif 866 867/** 868 * Called when the bitrate is not set. It sets appropriate default values for 869 * bitrate and CRF. 870 */ 871static void set_vpx_defaults(AVCodecContext *avctx, 872 struct vpx_codec_enc_cfg *enccfg) 873{ 874 av_assert0(!avctx->bit_rate); 875#if CONFIG_LIBVPX_VP9_ENCODER 876 if (avctx->codec_id == AV_CODEC_ID_VP9) { 877 set_vp9_defaults(avctx, enccfg); 878 return; 879 } 880#endif 881 set_vp8_defaults(avctx, enccfg); 882} 883 884static av_cold int vpx_init(AVCodecContext *avctx, 885 const struct vpx_codec_iface *iface) 886{ 887 VPxContext *ctx = avctx->priv_data; 888 struct vpx_codec_enc_cfg enccfg = { 0 }; 889 struct vpx_codec_enc_cfg enccfg_alpha; 890 vpx_codec_flags_t flags = (avctx->flags & AV_CODEC_FLAG_PSNR) ? VPX_CODEC_USE_PSNR : 0; 891 AVCPBProperties *cpb_props; 892 int res; 893 vpx_img_fmt_t img_fmt = VPX_IMG_FMT_I420; 894#if CONFIG_LIBVPX_VP9_ENCODER 895 vpx_codec_caps_t codec_caps = vpx_codec_get_caps(iface); 896 vpx_svc_extra_cfg_t svc_params; 897#endif 898 AVDictionaryEntry* en = NULL; 899 900 av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str()); 901 av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config()); 902 903 if (avctx->pix_fmt == AV_PIX_FMT_YUVA420P) 904 ctx->is_alpha = 1; 905 906 if ((res = vpx_codec_enc_config_default(iface, &enccfg, 0)) != VPX_CODEC_OK) { 907 av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n", 908 vpx_codec_err_to_string(res)); 909 return AVERROR(EINVAL); 910 } 911 912#if CONFIG_LIBVPX_VP9_ENCODER 913 if (avctx->codec_id == AV_CODEC_ID_VP9) { 914 if (set_pix_fmt(avctx, codec_caps, &enccfg, &flags, &img_fmt)) 915 return AVERROR(EINVAL); 916 // Keep HDR10+ if it has bit depth higher than 8 and 917 // it has PQ trc (SMPTE2084). 918 if (enccfg.g_bit_depth > 8 && avctx->color_trc == AVCOL_TRC_SMPTE2084) { 919 ctx->hdr10_plus_fifo = av_fifo_alloc2(1, sizeof(FrameHDR10Plus), 920 AV_FIFO_FLAG_AUTO_GROW); 921 if (!ctx->hdr10_plus_fifo) 922 return AVERROR(ENOMEM); 923 } 924 } 925#endif 926 927 if(!avctx->bit_rate) 928 if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) { 929 av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n"); 930 return AVERROR(EINVAL); 931 } 932 933 dump_enc_cfg(avctx, &enccfg, AV_LOG_DEBUG); 934 935 enccfg.g_w = avctx->width; 936 enccfg.g_h = avctx->height; 937 enccfg.g_timebase.num = avctx->time_base.num; 938 enccfg.g_timebase.den = avctx->time_base.den; 939 enccfg.g_threads = 940 FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 16); 941 enccfg.g_lag_in_frames= ctx->lag_in_frames; 942 943 if (avctx->flags & AV_CODEC_FLAG_PASS1) 944 enccfg.g_pass = VPX_RC_FIRST_PASS; 945 else if (avctx->flags & AV_CODEC_FLAG_PASS2) 946 enccfg.g_pass = VPX_RC_LAST_PASS; 947 else 948 enccfg.g_pass = VPX_RC_ONE_PASS; 949 950 if (avctx->rc_min_rate == avctx->rc_max_rate && 951 avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) { 952 enccfg.rc_end_usage = VPX_CBR; 953 } else if (ctx->crf >= 0) { 954 enccfg.rc_end_usage = VPX_CQ; 955#if CONFIG_LIBVPX_VP9_ENCODER 956 if (!avctx->bit_rate && avctx->codec_id == AV_CODEC_ID_VP9) 957 enccfg.rc_end_usage = VPX_Q; 958#endif 959 } 960 961 if (avctx->bit_rate) { 962 enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000, 963 AV_ROUND_NEAR_INF); 964#if CONFIG_LIBVPX_VP9_ENCODER 965 enccfg.ss_target_bitrate[0] = enccfg.rc_target_bitrate; 966#endif 967 } else { 968 // Set bitrate to default value. Also sets CRF to default if needed. 969 set_vpx_defaults(avctx, &enccfg); 970 } 971 972 if (avctx->codec_id == AV_CODEC_ID_VP9 && ctx->lossless == 1) { 973 enccfg.rc_min_quantizer = 974 enccfg.rc_max_quantizer = 0; 975 } else { 976 if (avctx->qmin >= 0) 977 enccfg.rc_min_quantizer = avctx->qmin; 978 if (avctx->qmax >= 0) 979 enccfg.rc_max_quantizer = avctx->qmax; 980 } 981 982 if (enccfg.rc_end_usage == VPX_CQ 983#if CONFIG_LIBVPX_VP9_ENCODER 984 || enccfg.rc_end_usage == VPX_Q 985#endif 986 ) { 987 if (ctx->crf < enccfg.rc_min_quantizer || ctx->crf > enccfg.rc_max_quantizer) { 988 av_log(avctx, AV_LOG_ERROR, 989 "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n", 990 ctx->crf, enccfg.rc_min_quantizer, enccfg.rc_max_quantizer); 991 return AVERROR(EINVAL); 992 } 993 } 994 995 enccfg.rc_dropframe_thresh = ctx->drop_threshold; 996 997 //0-100 (0 => CBR, 100 => VBR) 998 enccfg.rc_2pass_vbr_bias_pct = lrint(avctx->qcompress * 100); 999 if (avctx->bit_rate) 1000 enccfg.rc_2pass_vbr_minsection_pct = 1001 avctx->rc_min_rate * 100LL / avctx->bit_rate; 1002 if (avctx->rc_max_rate) 1003 enccfg.rc_2pass_vbr_maxsection_pct = 1004 avctx->rc_max_rate * 100LL / avctx->bit_rate; 1005#if CONFIG_LIBVPX_VP9_ENCODER 1006 if (avctx->codec_id == AV_CODEC_ID_VP9) { 1007#if VPX_ENCODER_ABI_VERSION >= 14 1008 if (ctx->corpus_complexity >= 0) 1009 enccfg.rc_2pass_vbr_corpus_complexity = ctx->corpus_complexity; 1010#endif 1011 } 1012#endif 1013 1014 if (avctx->rc_buffer_size) 1015 enccfg.rc_buf_sz = 1016 avctx->rc_buffer_size * 1000LL / avctx->bit_rate; 1017 if (avctx->rc_initial_buffer_occupancy) 1018 enccfg.rc_buf_initial_sz = 1019 avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate; 1020 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6; 1021 if (ctx->rc_undershoot_pct >= 0) 1022 enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct; 1023 if (ctx->rc_overshoot_pct >= 0) 1024 enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct; 1025 1026 //_enc_init() will balk if kf_min_dist differs from max w/VPX_KF_AUTO 1027 if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size) 1028 enccfg.kf_min_dist = avctx->keyint_min; 1029 if (avctx->gop_size >= 0) 1030 enccfg.kf_max_dist = avctx->gop_size; 1031 1032 if (enccfg.g_pass == VPX_RC_FIRST_PASS) 1033 enccfg.g_lag_in_frames = 0; 1034 else if (enccfg.g_pass == VPX_RC_LAST_PASS) { 1035 int decode_size, ret; 1036 1037 if (!avctx->stats_in) { 1038 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n"); 1039 return AVERROR_INVALIDDATA; 1040 } 1041 1042 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4; 1043 ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz); 1044 if (ret < 0) { 1045 av_log(avctx, AV_LOG_ERROR, 1046 "Stat buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", 1047 ctx->twopass_stats.sz); 1048 ctx->twopass_stats.sz = 0; 1049 return ret; 1050 } 1051 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in, 1052 ctx->twopass_stats.sz); 1053 if (decode_size < 0) { 1054 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n"); 1055 return AVERROR_INVALIDDATA; 1056 } 1057 1058 ctx->twopass_stats.sz = decode_size; 1059 enccfg.rc_twopass_stats_in = ctx->twopass_stats; 1060 } 1061 1062 /* 0-3: For non-zero values the encoder increasingly optimizes for reduced 1063 complexity playback on low powered devices at the expense of encode 1064 quality. */ 1065 if (avctx->profile != FF_PROFILE_UNKNOWN) 1066 enccfg.g_profile = avctx->profile; 1067 1068 enccfg.g_error_resilient = ctx->error_resilient || ctx->flags & VP8F_ERROR_RESILIENT; 1069 1070 while ((en = av_dict_get(ctx->vpx_ts_parameters, "", en, AV_DICT_IGNORE_SUFFIX))) { 1071 if (vpx_ts_param_parse(ctx, &enccfg, en->key, en->value, avctx->codec_id) < 0) 1072 av_log(avctx, AV_LOG_WARNING, 1073 "Error parsing option '%s = %s'.\n", 1074 en->key, en->value); 1075 } 1076 1077 /* Construct Encoder Context */ 1078 res = vpx_codec_enc_init(&ctx->encoder, iface, &enccfg, flags); 1079 if (res != VPX_CODEC_OK) { 1080 dump_enc_cfg(avctx, &enccfg, AV_LOG_WARNING); 1081 log_encoder_error(avctx, "Failed to initialize encoder"); 1082 return AVERROR(EINVAL); 1083 } 1084 dump_enc_cfg(avctx, &enccfg, AV_LOG_DEBUG); 1085 1086#if CONFIG_LIBVPX_VP9_ENCODER 1087 if (avctx->codec_id == AV_CODEC_ID_VP9 && enccfg.ts_number_layers > 1) { 1088 memset(&svc_params, 0, sizeof(svc_params)); 1089 for (int i = 0; i < enccfg.ts_number_layers; ++i) { 1090 svc_params.max_quantizers[i] = enccfg.rc_max_quantizer; 1091 svc_params.min_quantizers[i] = enccfg.rc_min_quantizer; 1092 } 1093 svc_params.scaling_factor_num[0] = enccfg.g_h; 1094 svc_params.scaling_factor_den[0] = enccfg.g_h; 1095#if VPX_ENCODER_ABI_VERSION >= 12 1096 codecctl_int(avctx, VP9E_SET_SVC, 1); 1097 codecctl_intp(avctx, VP9E_SET_SVC_PARAMETERS, (int *)&svc_params); 1098#endif 1099 } 1100#endif 1101 if (ctx->is_alpha) { 1102 enccfg_alpha = enccfg; 1103 res = vpx_codec_enc_init(&ctx->encoder_alpha, iface, &enccfg_alpha, flags); 1104 if (res != VPX_CODEC_OK) { 1105 log_encoder_error(avctx, "Failed to initialize alpha encoder"); 1106 return AVERROR(EINVAL); 1107 } 1108 } 1109 1110 //codec control failures are currently treated only as warnings 1111 av_log(avctx, AV_LOG_DEBUG, "vpx_codec_control\n"); 1112 codecctl_int(avctx, VP8E_SET_CPUUSED, ctx->cpu_used); 1113 if (ctx->flags & VP8F_AUTO_ALT_REF) 1114 ctx->auto_alt_ref = 1; 1115 if (ctx->auto_alt_ref >= 0) 1116 codecctl_int(avctx, VP8E_SET_ENABLEAUTOALTREF, 1117 avctx->codec_id == AV_CODEC_ID_VP8 ? !!ctx->auto_alt_ref : ctx->auto_alt_ref); 1118 if (ctx->arnr_max_frames >= 0) 1119 codecctl_int(avctx, VP8E_SET_ARNR_MAXFRAMES, ctx->arnr_max_frames); 1120 if (ctx->arnr_strength >= 0) 1121 codecctl_int(avctx, VP8E_SET_ARNR_STRENGTH, ctx->arnr_strength); 1122 if (ctx->arnr_type >= 0) 1123 codecctl_int(avctx, VP8E_SET_ARNR_TYPE, ctx->arnr_type); 1124 if (ctx->tune >= 0) 1125 codecctl_int(avctx, VP8E_SET_TUNING, ctx->tune); 1126 1127 if (ctx->auto_alt_ref && ctx->is_alpha && avctx->codec_id == AV_CODEC_ID_VP8) { 1128 av_log(avctx, AV_LOG_ERROR, "Transparency encoding with auto_alt_ref does not work\n"); 1129 return AVERROR(EINVAL); 1130 } 1131 1132 if (ctx->sharpness >= 0) 1133 codecctl_int(avctx, VP8E_SET_SHARPNESS, ctx->sharpness); 1134 1135 if (CONFIG_LIBVPX_VP8_ENCODER && avctx->codec_id == AV_CODEC_ID_VP8) { 1136 codecctl_int(avctx, VP8E_SET_NOISE_SENSITIVITY, ctx->noise_sensitivity); 1137 codecctl_int(avctx, VP8E_SET_TOKEN_PARTITIONS, av_log2(avctx->slices)); 1138 } 1139 codecctl_int(avctx, VP8E_SET_STATIC_THRESHOLD, ctx->static_thresh); 1140 if (ctx->crf >= 0) 1141 codecctl_int(avctx, VP8E_SET_CQ_LEVEL, ctx->crf); 1142 if (ctx->max_intra_rate >= 0) 1143 codecctl_int(avctx, VP8E_SET_MAX_INTRA_BITRATE_PCT, ctx->max_intra_rate); 1144 1145#if CONFIG_LIBVPX_VP9_ENCODER 1146 if (avctx->codec_id == AV_CODEC_ID_VP9) { 1147 if (ctx->lossless >= 0) 1148 codecctl_int(avctx, VP9E_SET_LOSSLESS, ctx->lossless); 1149 if (ctx->tile_columns >= 0) 1150 codecctl_int(avctx, VP9E_SET_TILE_COLUMNS, ctx->tile_columns); 1151 if (ctx->tile_rows >= 0) 1152 codecctl_int(avctx, VP9E_SET_TILE_ROWS, ctx->tile_rows); 1153 if (ctx->frame_parallel >= 0) 1154 codecctl_int(avctx, VP9E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel); 1155 if (ctx->aq_mode >= 0) 1156 codecctl_int(avctx, VP9E_SET_AQ_MODE, ctx->aq_mode); 1157 set_colorspace(avctx); 1158#if VPX_ENCODER_ABI_VERSION >= 11 1159 set_color_range(avctx); 1160#endif 1161#if VPX_ENCODER_ABI_VERSION >= 12 1162 codecctl_int(avctx, VP9E_SET_TARGET_LEVEL, ctx->level < 0 ? 255 : lrint(ctx->level * 10)); 1163#endif 1164#ifdef VPX_CTRL_VP9E_SET_ROW_MT 1165 if (ctx->row_mt >= 0) 1166 codecctl_int(avctx, VP9E_SET_ROW_MT, ctx->row_mt); 1167#endif 1168#ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT 1169 if (ctx->tune_content >= 0) 1170 codecctl_int(avctx, VP9E_SET_TUNE_CONTENT, ctx->tune_content); 1171#endif 1172#ifdef VPX_CTRL_VP9E_SET_TPL 1173 if (ctx->tpl_model >= 0) 1174 codecctl_int(avctx, VP9E_SET_TPL, ctx->tpl_model); 1175#endif 1176 } 1177#endif 1178 1179 av_log(avctx, AV_LOG_DEBUG, "Using deadline: %d\n", ctx->deadline); 1180 1181 //provide dummy value to initialize wrapper, values will be updated each _encode() 1182 vpx_img_wrap(&ctx->rawimg, img_fmt, avctx->width, avctx->height, 1, 1183 (unsigned char*)1); 1184#if CONFIG_LIBVPX_VP9_ENCODER 1185 if (avctx->codec_id == AV_CODEC_ID_VP9 && (codec_caps & VPX_CODEC_CAP_HIGHBITDEPTH)) 1186 ctx->rawimg.bit_depth = enccfg.g_bit_depth; 1187#endif 1188 1189 cpb_props = ff_add_cpb_side_data(avctx); 1190 if (!cpb_props) 1191 return AVERROR(ENOMEM); 1192 1193 if (enccfg.rc_end_usage == VPX_CBR || 1194 enccfg.g_pass != VPX_RC_ONE_PASS) { 1195 cpb_props->max_bitrate = avctx->rc_max_rate; 1196 cpb_props->min_bitrate = avctx->rc_min_rate; 1197 cpb_props->avg_bitrate = avctx->bit_rate; 1198 } 1199 cpb_props->buffer_size = avctx->rc_buffer_size; 1200 1201 return 0; 1202} 1203 1204static inline void cx_pktcpy(struct FrameListData *dst, 1205 const struct vpx_codec_cx_pkt *src, 1206 VPxContext *ctx) 1207{ 1208 dst->pts = src->data.frame.pts; 1209 dst->duration = src->data.frame.duration; 1210 dst->flags = src->data.frame.flags; 1211 dst->sz = src->data.frame.sz; 1212 dst->buf = src->data.frame.buf; 1213 dst->have_sse = 0; 1214 /* For alt-ref frame, don't store PSNR or increment frame_number */ 1215 if (!(dst->flags & VPX_FRAME_IS_INVISIBLE)) { 1216 dst->frame_number = ++ctx->frame_number; 1217 dst->have_sse = ctx->have_sse; 1218 if (ctx->have_sse) { 1219 /* associate last-seen SSE to the frame. */ 1220 /* Transfers ownership from ctx to dst. */ 1221 /* WARNING! This makes the assumption that PSNR_PKT comes 1222 just before the frame it refers to! */ 1223 memcpy(dst->sse, ctx->sse, sizeof(dst->sse)); 1224 ctx->have_sse = 0; 1225 } 1226 } else { 1227 dst->frame_number = -1; /* sanity marker */ 1228 } 1229} 1230 1231/** 1232 * Store coded frame information in format suitable for return from encode2(). 1233 * 1234 * Write information from @a cx_frame to @a pkt 1235 * @return packet data size on success 1236 * @return a negative AVERROR on error 1237 */ 1238static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, 1239 struct FrameListData *alpha_cx_frame, AVPacket *pkt) 1240{ 1241 VPxContext *ctx = avctx->priv_data; 1242 int ret = ff_get_encode_buffer(avctx, pkt, cx_frame->sz, 0); 1243 uint8_t *side_data; 1244 int pict_type; 1245 int quality; 1246 1247 if (ret < 0) 1248 return ret; 1249 1250 memcpy(pkt->data, cx_frame->buf, pkt->size); 1251 pkt->pts = pkt->dts = cx_frame->pts; 1252 1253 if (!!(cx_frame->flags & VPX_FRAME_IS_KEY)) { 1254 pict_type = AV_PICTURE_TYPE_I; 1255 pkt->flags |= AV_PKT_FLAG_KEY; 1256 } else { 1257 pict_type = AV_PICTURE_TYPE_P; 1258 } 1259 1260 ret = vpx_codec_control(&ctx->encoder, VP8E_GET_LAST_QUANTIZER_64, &quality); 1261 if (ret != VPX_CODEC_OK) 1262 quality = 0; 1263 ff_side_data_set_encoder_stats(pkt, quality * FF_QP2LAMBDA, cx_frame->sse + 1, 1264 cx_frame->have_sse ? 3 : 0, pict_type); 1265 1266 if (cx_frame->have_sse) { 1267 /* Beware of the Y/U/V/all order! */ 1268 for (int i = 0; i < 3; ++i) 1269 avctx->error[i] += cx_frame->sse[i + 1]; 1270 cx_frame->have_sse = 0; 1271 } 1272 if (alpha_cx_frame) { 1273 side_data = av_packet_new_side_data(pkt, 1274 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL, 1275 alpha_cx_frame->sz + 8); 1276 if (!side_data) { 1277 av_packet_unref(pkt); 1278 return AVERROR(ENOMEM); 1279 } 1280 AV_WB64(side_data, 1); 1281 memcpy(side_data + 8, alpha_cx_frame->buf, alpha_cx_frame->sz); 1282 } 1283 if (cx_frame->frame_number != -1) { 1284 if (ctx->hdr10_plus_fifo) { 1285 int err = copy_hdr10_plus_to_pkt(ctx->hdr10_plus_fifo, pkt); 1286 if (err < 0) 1287 return err; 1288 } 1289 } 1290 1291 return pkt->size; 1292} 1293 1294/** 1295 * Queue multiple output frames from the encoder, returning the front-most. 1296 * In cases where vpx_codec_get_cx_data() returns more than 1 frame append 1297 * the frame queue. Return the head frame if available. 1298 * @return Stored frame size 1299 * @return AVERROR(EINVAL) on output size error 1300 * @return AVERROR(ENOMEM) on coded frame queue data allocation error 1301 */ 1302static int queue_frames(AVCodecContext *avctx, struct vpx_codec_ctx *encoder, 1303 struct FrameListData **frame_list, AVPacket *pkt_out) 1304{ 1305 VPxContext *ctx = avctx->priv_data; 1306 const struct vpx_codec_cx_pkt *pkt; 1307 const void *iter = NULL; 1308 int size = 0; 1309 1310 if (!ctx->is_alpha && *frame_list) { 1311 struct FrameListData *cx_frame = *frame_list; 1312 /* return the leading frame if we've already begun queueing */ 1313 size = storeframe(avctx, cx_frame, NULL, pkt_out); 1314 if (size < 0) 1315 return size; 1316 *frame_list = cx_frame->next; 1317 free_coded_frame(cx_frame); 1318 } 1319 1320 /* consume all available output from the encoder before returning. buffers 1321 are only good through the next vpx_codec call */ 1322 while (pkt = vpx_codec_get_cx_data(encoder, &iter)) { 1323 switch (pkt->kind) { 1324 case VPX_CODEC_CX_FRAME_PKT: 1325 if (!ctx->is_alpha && !size) { 1326 struct FrameListData cx_frame; 1327 1328 /* avoid storing the frame when the list is empty and we haven't yet 1329 provided a frame for output */ 1330 av_assert0(!ctx->coded_frame_list); 1331 cx_pktcpy(&cx_frame, pkt, ctx); 1332 size = storeframe(avctx, &cx_frame, NULL, pkt_out); 1333 if (size < 0) 1334 return size; 1335 } else { 1336 struct FrameListData *cx_frame = av_malloc(sizeof(*cx_frame)); 1337 1338 if (!cx_frame) { 1339 av_log(avctx, AV_LOG_ERROR, 1340 "Frame queue element alloc failed\n"); 1341 return AVERROR(ENOMEM); 1342 } 1343 cx_pktcpy(cx_frame, pkt, ctx); 1344 cx_frame->buf = av_malloc(cx_frame->sz); 1345 1346 if (!cx_frame->buf) { 1347 av_log(avctx, AV_LOG_ERROR, 1348 "Data buffer alloc (%"SIZE_SPECIFIER" bytes) failed\n", 1349 cx_frame->sz); 1350 av_freep(&cx_frame); 1351 return AVERROR(ENOMEM); 1352 } 1353 memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz); 1354 coded_frame_add(frame_list, cx_frame); 1355 } 1356 break; 1357 case VPX_CODEC_STATS_PKT: { 1358 struct vpx_fixed_buf *stats = &ctx->twopass_stats; 1359 int err; 1360 if (!pkt_out) 1361 break; 1362 if ((err = av_reallocp(&stats->buf, 1363 stats->sz + 1364 pkt->data.twopass_stats.sz)) < 0) { 1365 stats->sz = 0; 1366 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n"); 1367 return err; 1368 } 1369 memcpy((uint8_t*)stats->buf + stats->sz, 1370 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz); 1371 stats->sz += pkt->data.twopass_stats.sz; 1372 break; 1373 } 1374 case VPX_CODEC_PSNR_PKT: 1375 if (!pkt_out) 1376 break; 1377 av_assert0(!ctx->have_sse); 1378 ctx->sse[0] = pkt->data.psnr.sse[0]; 1379 ctx->sse[1] = pkt->data.psnr.sse[1]; 1380 ctx->sse[2] = pkt->data.psnr.sse[2]; 1381 ctx->sse[3] = pkt->data.psnr.sse[3]; 1382 ctx->have_sse = 1; 1383 break; 1384 case VPX_CODEC_CUSTOM_PKT: 1385 //ignore unsupported/unrecognized packet types 1386 break; 1387 } 1388 } 1389 1390 return size; 1391} 1392 1393static int set_roi_map(AVCodecContext *avctx, const AVFrameSideData *sd, int frame_width, int frame_height, 1394 vpx_roi_map_t *roi_map, int block_size, int segment_cnt) 1395{ 1396 /** 1397 * range of vpx_roi_map_t.delta_q[i] is [-63, 63] 1398 */ 1399#define MAX_DELTA_Q 63 1400 1401 const AVRegionOfInterest *roi = NULL; 1402 int nb_rois; 1403 uint32_t self_size; 1404 int segment_id; 1405 1406 /* record the mapping from delta_q to "segment id + 1" in segment_mapping[]. 1407 * the range of delta_q is [-MAX_DELTA_Q, MAX_DELTA_Q], 1408 * and its corresponding array index is [0, 2 * MAX_DELTA_Q], 1409 * and so the length of the mapping array is 2 * MAX_DELTA_Q + 1. 1410 * "segment id + 1", so we can say there's no mapping if the value of array element is zero. 1411 */ 1412 int segment_mapping[2 * MAX_DELTA_Q + 1] = { 0 }; 1413 1414 memset(roi_map, 0, sizeof(*roi_map)); 1415 1416 /* segment id 0 in roi_map is reserved for the areas not covered by AVRegionOfInterest. 1417 * segment id 0 in roi_map is also for the areas with AVRegionOfInterest.qoffset near 0. 1418 * (delta_q of segment id 0 is 0). 1419 */ 1420 segment_mapping[MAX_DELTA_Q] = 1; 1421 segment_id = 1; 1422 1423 roi = (const AVRegionOfInterest*)sd->data; 1424 self_size = roi->self_size; 1425 if (!self_size || sd->size % self_size) { 1426 av_log(avctx, AV_LOG_ERROR, "Invalid AVRegionOfInterest.self_size.\n"); 1427 return AVERROR(EINVAL); 1428 } 1429 nb_rois = sd->size / self_size; 1430 1431 /* This list must be iterated from zero because regions are 1432 * defined in order of decreasing importance. So discard less 1433 * important areas if they exceed the segment count. 1434 */ 1435 for (int i = 0; i < nb_rois; i++) { 1436 int delta_q; 1437 int mapping_index; 1438 1439 roi = (const AVRegionOfInterest*)(sd->data + self_size * i); 1440 if (!roi->qoffset.den) { 1441 av_log(avctx, AV_LOG_ERROR, "AVRegionOfInterest.qoffset.den must not be zero.\n"); 1442 return AVERROR(EINVAL); 1443 } 1444 1445 delta_q = (int)(roi->qoffset.num * 1.0f / roi->qoffset.den * MAX_DELTA_Q); 1446 delta_q = av_clip(delta_q, -MAX_DELTA_Q, MAX_DELTA_Q); 1447 1448 mapping_index = delta_q + MAX_DELTA_Q; 1449 if (!segment_mapping[mapping_index]) { 1450 if (segment_id == segment_cnt) { 1451 av_log(avctx, AV_LOG_WARNING, 1452 "ROI only supports %d segments (and segment 0 is reserved for non-ROIs), skipping the left ones.\n", 1453 segment_cnt); 1454 break; 1455 } 1456 1457 segment_mapping[mapping_index] = segment_id + 1; 1458 roi_map->delta_q[segment_id] = delta_q; 1459 segment_id++; 1460 } 1461 } 1462 1463 roi_map->rows = (frame_height + block_size - 1) / block_size; 1464 roi_map->cols = (frame_width + block_size - 1) / block_size; 1465 roi_map->roi_map = av_calloc(roi_map->rows * roi_map->cols, sizeof(*roi_map->roi_map)); 1466 if (!roi_map->roi_map) { 1467 av_log(avctx, AV_LOG_ERROR, "roi_map alloc failed.\n"); 1468 return AVERROR(ENOMEM); 1469 } 1470 1471 /* This list must be iterated in reverse, so for the case that 1472 * two regions are overlapping, the more important area takes effect. 1473 */ 1474 for (int i = nb_rois - 1; i >= 0; i--) { 1475 int delta_q; 1476 int mapping_value; 1477 int starty, endy, startx, endx; 1478 1479 roi = (const AVRegionOfInterest*)(sd->data + self_size * i); 1480 1481 starty = av_clip(roi->top / block_size, 0, roi_map->rows); 1482 endy = av_clip((roi->bottom + block_size - 1) / block_size, 0, roi_map->rows); 1483 startx = av_clip(roi->left / block_size, 0, roi_map->cols); 1484 endx = av_clip((roi->right + block_size - 1) / block_size, 0, roi_map->cols); 1485 1486 delta_q = (int)(roi->qoffset.num * 1.0f / roi->qoffset.den * MAX_DELTA_Q); 1487 delta_q = av_clip(delta_q, -MAX_DELTA_Q, MAX_DELTA_Q); 1488 1489 mapping_value = segment_mapping[delta_q + MAX_DELTA_Q]; 1490 if (mapping_value) { 1491 for (int y = starty; y < endy; y++) 1492 for (int x = startx; x < endx; x++) 1493 roi_map->roi_map[x + y * roi_map->cols] = mapping_value - 1; 1494 } 1495 } 1496 1497 return 0; 1498} 1499 1500static int vp9_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd) 1501{ 1502 VPxContext *ctx = avctx->priv_data; 1503 1504#ifdef VPX_CTRL_VP9E_SET_ROI_MAP 1505 int version = vpx_codec_version(); 1506 int major = VPX_VERSION_MAJOR(version); 1507 int minor = VPX_VERSION_MINOR(version); 1508 int patch = VPX_VERSION_PATCH(version); 1509 1510 if (major > 1 || (major == 1 && minor > 8) || (major == 1 && minor == 8 && patch >= 1)) { 1511 vpx_roi_map_t roi_map; 1512 const int segment_cnt = 8; 1513 const int block_size = 8; 1514 int ret; 1515 1516 if (ctx->aq_mode > 0 || ctx->cpu_used < 5 || ctx->deadline != VPX_DL_REALTIME) { 1517 if (!ctx->roi_warned) { 1518 ctx->roi_warned = 1; 1519 av_log(avctx, AV_LOG_WARNING, "ROI is only enabled when aq_mode is 0, cpu_used >= 5 " 1520 "and deadline is REALTIME, so skipping ROI.\n"); 1521 return AVERROR(EINVAL); 1522 } 1523 } 1524 1525 ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt); 1526 if (ret) { 1527 log_encoder_error(avctx, "Failed to set_roi_map.\n"); 1528 return ret; 1529 } 1530 1531 memset(roi_map.ref_frame, -1, sizeof(roi_map.ref_frame)); 1532 1533 if (vpx_codec_control(&ctx->encoder, VP9E_SET_ROI_MAP, &roi_map)) { 1534 log_encoder_error(avctx, "Failed to set VP9E_SET_ROI_MAP codec control.\n"); 1535 ret = AVERROR_INVALIDDATA; 1536 } 1537 av_freep(&roi_map.roi_map); 1538 return ret; 1539 } 1540#endif 1541 1542 if (!ctx->roi_warned) { 1543 ctx->roi_warned = 1; 1544 av_log(avctx, AV_LOG_WARNING, "ROI is not supported, please upgrade libvpx to version >= 1.8.1. " 1545 "You may need to rebuild ffmpeg.\n"); 1546 } 1547 return 0; 1548} 1549 1550static int vp8_encode_set_roi(AVCodecContext *avctx, int frame_width, int frame_height, const AVFrameSideData *sd) 1551{ 1552 vpx_roi_map_t roi_map; 1553 const int segment_cnt = 4; 1554 const int block_size = 16; 1555 VPxContext *ctx = avctx->priv_data; 1556 1557 int ret = set_roi_map(avctx, sd, frame_width, frame_height, &roi_map, block_size, segment_cnt); 1558 if (ret) { 1559 log_encoder_error(avctx, "Failed to set_roi_map.\n"); 1560 return ret; 1561 } 1562 1563 if (vpx_codec_control(&ctx->encoder, VP8E_SET_ROI_MAP, &roi_map)) { 1564 log_encoder_error(avctx, "Failed to set VP8E_SET_ROI_MAP codec control.\n"); 1565 ret = AVERROR_INVALIDDATA; 1566 } 1567 1568 av_freep(&roi_map.roi_map); 1569 return ret; 1570} 1571 1572static int realloc_alpha_uv(AVCodecContext *avctx, int width, int height) 1573{ 1574 VPxContext *ctx = avctx->priv_data; 1575 struct vpx_image *rawimg_alpha = &ctx->rawimg_alpha; 1576 unsigned char **planes = rawimg_alpha->planes; 1577 int *stride = rawimg_alpha->stride; 1578 1579 if (!planes[VPX_PLANE_U] || 1580 !planes[VPX_PLANE_V] || 1581 width != (int)rawimg_alpha->d_w || 1582 height != (int)rawimg_alpha->d_h) { 1583 av_freep(&planes[VPX_PLANE_U]); 1584 av_freep(&planes[VPX_PLANE_V]); 1585 1586 vpx_img_wrap(rawimg_alpha, VPX_IMG_FMT_I420, width, height, 1, 1587 (unsigned char*)1); 1588 planes[VPX_PLANE_U] = av_malloc_array(stride[VPX_PLANE_U], height); 1589 planes[VPX_PLANE_V] = av_malloc_array(stride[VPX_PLANE_V], height); 1590 if (!planes[VPX_PLANE_U] || !planes[VPX_PLANE_V]) 1591 return AVERROR(ENOMEM); 1592 1593 memset(planes[VPX_PLANE_U], 0x80, stride[VPX_PLANE_U] * height); 1594 memset(planes[VPX_PLANE_V], 0x80, stride[VPX_PLANE_V] * height); 1595 } 1596 1597 return 0; 1598} 1599 1600static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt, 1601 const AVFrame *frame, int *got_packet) 1602{ 1603 VPxContext *ctx = avctx->priv_data; 1604 struct vpx_image *rawimg = NULL; 1605 struct vpx_image *rawimg_alpha = NULL; 1606 int64_t timestamp = 0; 1607 int res, coded_size; 1608 vpx_enc_frame_flags_t flags = 0; 1609 const struct vpx_codec_enc_cfg *enccfg = ctx->encoder.config.enc; 1610 vpx_svc_layer_id_t layer_id; 1611 int layer_id_valid = 0; 1612 1613 if (avctx->qmax >= 0 && enccfg->rc_max_quantizer != avctx->qmax) { 1614 struct vpx_codec_enc_cfg cfg = *enccfg; 1615 cfg.rc_max_quantizer = avctx->qmax; 1616 res = vpx_codec_enc_config_set(&ctx->encoder, &cfg); 1617 if (res != VPX_CODEC_OK) { 1618 log_encoder_error(avctx, "Error reconfiguring encoder"); 1619 return AVERROR_INVALIDDATA; 1620 } 1621 } 1622 1623 if (frame) { 1624 const AVFrameSideData *sd = av_frame_get_side_data(frame, AV_FRAME_DATA_REGIONS_OF_INTEREST); 1625 rawimg = &ctx->rawimg; 1626 rawimg->planes[VPX_PLANE_Y] = frame->data[0]; 1627 rawimg->planes[VPX_PLANE_U] = frame->data[1]; 1628 rawimg->planes[VPX_PLANE_V] = frame->data[2]; 1629 rawimg->stride[VPX_PLANE_Y] = frame->linesize[0]; 1630 rawimg->stride[VPX_PLANE_U] = frame->linesize[1]; 1631 rawimg->stride[VPX_PLANE_V] = frame->linesize[2]; 1632 if (ctx->is_alpha) { 1633 rawimg_alpha = &ctx->rawimg_alpha; 1634 res = realloc_alpha_uv(avctx, frame->width, frame->height); 1635 if (res < 0) 1636 return res; 1637 rawimg_alpha->planes[VPX_PLANE_Y] = frame->data[3]; 1638 rawimg_alpha->stride[VPX_PLANE_Y] = frame->linesize[3]; 1639 } 1640 timestamp = frame->pts; 1641#if VPX_IMAGE_ABI_VERSION >= 4 1642 switch (frame->color_range) { 1643 case AVCOL_RANGE_MPEG: 1644 rawimg->range = VPX_CR_STUDIO_RANGE; 1645 break; 1646 case AVCOL_RANGE_JPEG: 1647 rawimg->range = VPX_CR_FULL_RANGE; 1648 break; 1649 } 1650#endif 1651 if (frame->pict_type == AV_PICTURE_TYPE_I) 1652 flags |= VPX_EFLAG_FORCE_KF; 1653 if (frame->metadata) { 1654 AVDictionaryEntry* en = av_dict_get(frame->metadata, "vp8-flags", NULL, 0); 1655 if (en) { 1656 flags |= strtoul(en->value, NULL, 10); 1657 } 1658 1659 memset(&layer_id, 0, sizeof(layer_id)); 1660 1661 en = av_dict_get(frame->metadata, "temporal_id", NULL, 0); 1662 if (en) { 1663 layer_id.temporal_layer_id = strtoul(en->value, NULL, 10); 1664#ifdef VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT 1665 layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id; 1666#endif 1667 layer_id_valid = 1; 1668 } 1669#if CONFIG_LIBVPX_VP9_ENCODER && defined(VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT) 1670 en = av_dict_get(frame->metadata, "ref-frame-config", NULL, 0); 1671 1672 if (en) { 1673 if (avctx->codec_id == AV_CODEC_ID_VP9) { 1674 int ret = vpx_parse_ref_frame_config(&ctx->ref_frame_config, 1675 enccfg->ss_number_layers, en->value); 1676 if (ret < 0) { 1677 av_log(avctx, AV_LOG_WARNING, 1678 "Error parsing ref_frame_config option %s.\n", en->value); 1679 return ret; 1680 } 1681 1682 codecctl_intp(avctx, VP9E_SET_SVC_REF_FRAME_CONFIG, (int *)&ctx->ref_frame_config); 1683 } else { 1684 av_log(avctx, AV_LOG_WARNING, 1685 "Ignoring ref-frame-config for a non-VP9 codec\n"); 1686 } 1687 } 1688#endif 1689 } 1690 1691 if (sd) { 1692 if (avctx->codec_id == AV_CODEC_ID_VP8) { 1693 vp8_encode_set_roi(avctx, frame->width, frame->height, sd); 1694 } else { 1695 vp9_encode_set_roi(avctx, frame->width, frame->height, sd); 1696 } 1697 } 1698 1699 if (ctx->hdr10_plus_fifo) { 1700 AVFrameSideData *hdr10_plus_metadata; 1701 // Add HDR10+ metadata to queue. 1702 hdr10_plus_metadata = av_frame_get_side_data(frame, AV_FRAME_DATA_DYNAMIC_HDR_PLUS); 1703 if (hdr10_plus_metadata) { 1704 int err; 1705 struct FrameHDR10Plus data; 1706 data.pts = frame->pts; 1707 data.hdr10_plus = av_buffer_ref(hdr10_plus_metadata->buf); 1708 if (!data.hdr10_plus) 1709 return AVERROR(ENOMEM); 1710 err = av_fifo_write(ctx->hdr10_plus_fifo, &data, 1); 1711 if (err < 0) { 1712 av_buffer_unref(&data.hdr10_plus); 1713 return err; 1714 } 1715 } 1716 } 1717 } 1718 1719 // this is for encoding with preset temporal layering patterns defined in 1720 // set_temporal_layer_pattern function. 1721 if (enccfg->ts_number_layers > 1 && ctx->ts_layer_flags) { 1722 if (flags & VPX_EFLAG_FORCE_KF) { 1723 // keyframe, reset temporal layering. 1724 ctx->current_temporal_idx = 0; 1725 flags = VPX_EFLAG_FORCE_KF; 1726 } else { 1727 flags = 0; 1728 } 1729 1730 /* get the flags from the temporal layer configuration. */ 1731 flags |= ctx->ts_layer_flags[ctx->current_temporal_idx]; 1732 1733 memset(&layer_id, 0, sizeof(layer_id)); 1734#if VPX_ENCODER_ABI_VERSION >= 12 1735 layer_id.spatial_layer_id = 0; 1736#endif 1737 layer_id.temporal_layer_id = enccfg->ts_layer_id[ctx->current_temporal_idx]; 1738#ifdef VPX_CTRL_VP9E_SET_MAX_INTER_BITRATE_PCT 1739 layer_id.temporal_layer_id_per_spatial[0] = layer_id.temporal_layer_id; 1740#endif 1741 layer_id_valid = 1; 1742 } 1743 1744 if (layer_id_valid) { 1745 if (avctx->codec_id == AV_CODEC_ID_VP8) { 1746 codecctl_int(avctx, VP8E_SET_TEMPORAL_LAYER_ID, layer_id.temporal_layer_id); 1747 } 1748#if CONFIG_LIBVPX_VP9_ENCODER && VPX_ENCODER_ABI_VERSION >= 12 1749 else if (avctx->codec_id == AV_CODEC_ID_VP9) { 1750 codecctl_intp(avctx, VP9E_SET_SVC_LAYER_ID, (int *)&layer_id); 1751 } 1752#endif 1753 } 1754 1755 res = vpx_codec_encode(&ctx->encoder, rawimg, timestamp, 1756 avctx->ticks_per_frame, flags, ctx->deadline); 1757 if (res != VPX_CODEC_OK) { 1758 log_encoder_error(avctx, "Error encoding frame"); 1759 return AVERROR_INVALIDDATA; 1760 } 1761 1762 if (ctx->is_alpha) { 1763 res = vpx_codec_encode(&ctx->encoder_alpha, rawimg_alpha, timestamp, 1764 avctx->ticks_per_frame, flags, ctx->deadline); 1765 if (res != VPX_CODEC_OK) { 1766 log_encoder_error(avctx, "Error encoding alpha frame"); 1767 return AVERROR_INVALIDDATA; 1768 } 1769 } 1770 1771 coded_size = queue_frames(avctx, &ctx->encoder, &ctx->coded_frame_list, pkt); 1772 if (ctx->is_alpha) { 1773 queue_frames(avctx, &ctx->encoder_alpha, &ctx->alpha_coded_frame_list, NULL); 1774 1775 if (ctx->coded_frame_list && ctx->alpha_coded_frame_list) { 1776 struct FrameListData *cx_frame = ctx->coded_frame_list; 1777 struct FrameListData *alpha_cx_frame = ctx->alpha_coded_frame_list; 1778 av_assert0(!coded_size); 1779 /* return the leading frame if we've already begun queueing */ 1780 coded_size = storeframe(avctx, cx_frame, alpha_cx_frame, pkt); 1781 if (coded_size < 0) 1782 return coded_size; 1783 ctx->coded_frame_list = cx_frame->next; 1784 ctx->alpha_coded_frame_list = alpha_cx_frame->next; 1785 free_coded_frame(cx_frame); 1786 free_coded_frame(alpha_cx_frame); 1787 } 1788 } 1789 1790 if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) { 1791 unsigned int b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz); 1792 1793 avctx->stats_out = av_malloc(b64_size); 1794 if (!avctx->stats_out) { 1795 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%d bytes) failed\n", 1796 b64_size); 1797 return AVERROR(ENOMEM); 1798 } 1799 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf, 1800 ctx->twopass_stats.sz); 1801 } else if (enccfg->ts_number_layers > 1 && ctx->ts_layer_flags) { 1802 ctx->current_temporal_idx = (ctx->current_temporal_idx + 1) % enccfg->ts_periodicity; 1803 } 1804 1805 *got_packet = !!coded_size; 1806 return 0; 1807} 1808 1809#define OFFSET(x) offsetof(VPxContext, x) 1810#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM 1811 1812#define COMMON_OPTIONS \ 1813 { "lag-in-frames", "Number of frames to look ahead for " \ 1814 "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \ 1815 { "arnr-maxframes", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \ 1816 { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \ 1817 { "arnr-type", "altref noise reduction filter type", OFFSET(arnr_type), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "arnr_type"}, \ 1818 { "backward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "arnr_type" }, \ 1819 { "forward", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "arnr_type" }, \ 1820 { "centered", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "arnr_type" }, \ 1821 { "tune", "Tune the encoding to a specific scenario", OFFSET(tune), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE, "tune"}, \ 1822 { "psnr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VP8_TUNE_PSNR}, 0, 0, VE, "tune"}, \ 1823 { "ssim", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VP8_TUNE_SSIM}, 0, 0, VE, "tune"}, \ 1824 { "deadline", "Time to spend encoding, in microseconds.", OFFSET(deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \ 1825 { "best", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_BEST_QUALITY}, 0, 0, VE, "quality"}, \ 1826 { "good", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_GOOD_QUALITY}, 0, 0, VE, "quality"}, \ 1827 { "realtime", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = VPX_DL_REALTIME}, 0, 0, VE, "quality"}, \ 1828 { "error-resilient", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"}, \ 1829 { "max-intra-rate", "Maximum I-frame bitrate (pct) 0=unlimited", OFFSET(max_intra_rate), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE}, \ 1830 { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"}, \ 1831 { "partitions", "The frame partitions are independently decodable " \ 1832 "by the bool decoder, meaning that partitions can be decoded even " \ 1833 "though earlier partitions have been lost. Note that intra prediction" \ 1834 " is still done over the partition boundary.", 0, AV_OPT_TYPE_CONST, {.i64 = VPX_ERROR_RESILIENT_PARTITIONS}, 0, 0, VE, "er"}, \ 1835 { "crf", "Select the quality for constant quality mode", offsetof(VPxContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VE }, \ 1836 { "static-thresh", "A change threshold on blocks below which they will be skipped by the encoder", OFFSET(static_thresh), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE }, \ 1837 { "drop-threshold", "Frame drop threshold", offsetof(VPxContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE }, \ 1838 { "noise-sensitivity", "Noise sensitivity", OFFSET(noise_sensitivity), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE}, \ 1839 { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 100, VE }, \ 1840 { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 1000, VE }, \ 1841 { "ts-parameters", "Temporal scaling configuration using a :-separated list of key=value parameters", OFFSET(vpx_ts_parameters), AV_OPT_TYPE_DICT, {.str=NULL}, 0, 0, VE}, \ 1842 1843#define LEGACY_OPTIONS \ 1844 {"speed", "", offsetof(VPxContext, cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, \ 1845 {"quality", "", offsetof(VPxContext, deadline), AV_OPT_TYPE_INT, {.i64 = VPX_DL_GOOD_QUALITY}, INT_MIN, INT_MAX, VE, "quality"}, \ 1846 {"vp8flags", "", offsetof(VPxContext, flags), AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, UINT_MAX, VE, "flags"}, \ 1847 {"error_resilient", "enable error resilience", 0, AV_OPT_TYPE_CONST, {.i64 = VP8F_ERROR_RESILIENT}, INT_MIN, INT_MAX, VE, "flags"}, \ 1848 {"altref", "enable use of alternate reference frames (VP8/2-pass only)", 0, AV_OPT_TYPE_CONST, {.i64 = VP8F_AUTO_ALT_REF}, INT_MIN, INT_MAX, VE, "flags"}, \ 1849 {"arnr_max_frames", "altref noise reduction max frame count", offsetof(VPxContext, arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 15, VE}, \ 1850 {"arnr_strength", "altref noise reduction filter strength", offsetof(VPxContext, arnr_strength), AV_OPT_TYPE_INT, {.i64 = 3}, 0, 6, VE}, \ 1851 {"arnr_type", "altref noise reduction filter type", offsetof(VPxContext, arnr_type), AV_OPT_TYPE_INT, {.i64 = 3}, 1, 3, VE}, \ 1852 {"rc_lookahead", "Number of frames to look ahead for alternate reference frame selection", offsetof(VPxContext, lag_in_frames), AV_OPT_TYPE_INT, {.i64 = 25}, 0, 25, VE}, \ 1853 {"sharpness", "Increase sharpness at the expense of lower PSNR", offsetof(VPxContext, sharpness), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 7, VE}, 1854 1855#if CONFIG_LIBVPX_VP8_ENCODER 1856static const AVOption vp8_options[] = { 1857 COMMON_OPTIONS 1858 { "auto-alt-ref", "Enable use of alternate reference " 1859 "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE}, 1860 { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -16, 16, VE}, 1861 LEGACY_OPTIONS 1862 { NULL } 1863}; 1864#endif 1865 1866#if CONFIG_LIBVPX_VP9_ENCODER 1867static const AVOption vp9_options[] = { 1868 COMMON_OPTIONS 1869 { "auto-alt-ref", "Enable use of alternate reference " 1870 "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE}, 1871 { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, -8, 8, VE}, 1872 { "lossless", "Lossless mode", OFFSET(lossless), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE}, 1873 { "tile-columns", "Number of tile columns to use, log2", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE}, 1874 { "tile-rows", "Number of tile rows to use, log2", OFFSET(tile_rows), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE}, 1875 { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_BOOL,{.i64 = -1}, -1, 1, VE}, 1876#if VPX_ENCODER_ABI_VERSION >= 12 1877 { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 4, VE, "aq_mode"}, 1878#else 1879 { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 3, VE, "aq_mode"}, 1880#endif 1881 { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "aq_mode" }, 1882 { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "aq_mode" }, 1883 { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "aq_mode" }, 1884 { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, "aq_mode" }, 1885#if VPX_ENCODER_ABI_VERSION >= 12 1886 { "equator360", "360 video Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 4}, 0, 0, VE, "aq_mode" }, 1887 {"level", "Specify level", OFFSET(level), AV_OPT_TYPE_FLOAT, {.dbl=-1}, -1, 6.2, VE}, 1888#endif 1889#ifdef VPX_CTRL_VP9E_SET_ROW_MT 1890 {"row-mt", "Row based multi-threading", OFFSET(row_mt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE}, 1891#endif 1892#ifdef VPX_CTRL_VP9E_SET_TUNE_CONTENT 1893#if VPX_ENCODER_ABI_VERSION >= 14 1894 { "tune-content", "Tune content type", OFFSET(tune_content), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE, "tune_content" }, 1895#else 1896 { "tune-content", "Tune content type", OFFSET(tune_content), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE, "tune_content" }, 1897#endif 1898 { "default", "Regular video content", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, "tune_content" }, 1899 { "screen", "Screen capture content", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, "tune_content" }, 1900#if VPX_ENCODER_ABI_VERSION >= 14 1901 { "film", "Film content; improves grain retention", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, "tune_content" }, 1902#endif 1903#endif 1904#if VPX_ENCODER_ABI_VERSION >= 14 1905 { "corpus-complexity", "corpus vbr complexity midpoint", OFFSET(corpus_complexity), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 10000, VE }, 1906#endif 1907#ifdef VPX_CTRL_VP9E_SET_TPL 1908 { "enable-tpl", "Enable temporal dependency model", OFFSET(tpl_model), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE }, 1909#endif 1910 LEGACY_OPTIONS 1911 { NULL } 1912}; 1913#endif 1914 1915#undef COMMON_OPTIONS 1916#undef LEGACY_OPTIONS 1917 1918static const FFCodecDefault defaults[] = { 1919 { "b", "0" }, 1920 { "qmin", "-1" }, 1921 { "qmax", "-1" }, 1922 { "g", "-1" }, 1923 { "keyint_min", "-1" }, 1924 { NULL }, 1925}; 1926 1927#if CONFIG_LIBVPX_VP8_ENCODER 1928static av_cold int vp8_init(AVCodecContext *avctx) 1929{ 1930 return vpx_init(avctx, vpx_codec_vp8_cx()); 1931} 1932 1933static const AVClass class_vp8 = { 1934 .class_name = "libvpx-vp8 encoder", 1935 .item_name = av_default_item_name, 1936 .option = vp8_options, 1937 .version = LIBAVUTIL_VERSION_INT, 1938}; 1939 1940const FFCodec ff_libvpx_vp8_encoder = { 1941 .p.name = "libvpx", 1942 .p.long_name = NULL_IF_CONFIG_SMALL("libvpx VP8"), 1943 .p.type = AVMEDIA_TYPE_VIDEO, 1944 .p.id = AV_CODEC_ID_VP8, 1945 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | 1946 AV_CODEC_CAP_OTHER_THREADS, 1947 .priv_data_size = sizeof(VPxContext), 1948 .init = vp8_init, 1949 FF_CODEC_ENCODE_CB(vpx_encode), 1950 .close = vpx_free, 1951 .caps_internal = FF_CODEC_CAP_AUTO_THREADS, 1952 .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUVA420P, AV_PIX_FMT_NONE }, 1953 .p.priv_class = &class_vp8, 1954 .defaults = defaults, 1955 .p.wrapper_name = "libvpx", 1956}; 1957#endif /* CONFIG_LIBVPX_VP8_ENCODER */ 1958 1959#if CONFIG_LIBVPX_VP9_ENCODER 1960static av_cold int vp9_init(AVCodecContext *avctx) 1961{ 1962 return vpx_init(avctx, vpx_codec_vp9_cx()); 1963} 1964 1965static const AVClass class_vp9 = { 1966 .class_name = "libvpx-vp9 encoder", 1967 .item_name = av_default_item_name, 1968 .option = vp9_options, 1969 .version = LIBAVUTIL_VERSION_INT, 1970}; 1971 1972FFCodec ff_libvpx_vp9_encoder = { 1973 .p.name = "libvpx-vp9", 1974 .p.long_name = NULL_IF_CONFIG_SMALL("libvpx VP9"), 1975 .p.type = AVMEDIA_TYPE_VIDEO, 1976 .p.id = AV_CODEC_ID_VP9, 1977 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY | 1978 AV_CODEC_CAP_OTHER_THREADS, 1979 .p.profiles = NULL_IF_CONFIG_SMALL(ff_vp9_profiles), 1980 .p.priv_class = &class_vp9, 1981 .p.wrapper_name = "libvpx", 1982 .priv_data_size = sizeof(VPxContext), 1983 .init = vp9_init, 1984 FF_CODEC_ENCODE_CB(vpx_encode), 1985 .close = vpx_free, 1986 .caps_internal = FF_CODEC_CAP_AUTO_THREADS, 1987 .defaults = defaults, 1988 .init_static_data = ff_vp9_init_static, 1989}; 1990#endif /* CONFIG_LIBVPX_VP9_ENCODER */ 1991