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