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