Bug Summary

File:root/firefox-clang/media/ffvpx/libavcodec/libvpxenc.c
Warning:line 1170, column 44
Division by zero

Annotated Source Code

Press '?' to see keyboard shortcuts

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