FFmpeg
Loading...
Searching...
No Matches
libaomenc.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010, Google, Inc.
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21/**
22 * @file
23 * AV1 encoder support via libaom
24 */
25
26#include <limits.h>
27
28#define AOM_DISABLE_CTRL_TYPECHECKS 1
29#include <aom/aom_encoder.h>
30#include <aom/aomcx.h>
31
32#include "libavutil/avassert.h"
33#include "libavutil/base64.h"
34#include "libavutil/cpu.h"
36#include "libavutil/imgutils.h"
38#include "libavutil/mem.h"
39#include "libavutil/opt.h"
40#include "libavutil/pixdesc.h"
41
42#include "av1.h"
43#include "avcodec.h"
44#include "bsf.h"
45#include "bytestream.h"
46#include "codec_internal.h"
47#include "dovi_rpu.h"
48#include "encode.h"
49#include "internal.h"
50#include "itut35.h"
51#include "libaom.h"
52#include "profiles.h"
53
54/*
55 * Portion of struct aom_codec_cx_pkt from aom_encoder.h.
56 * One encoded frame returned from the library.
57 */
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 unsigned long duration; /**< duration to show frame
64 (in timebase units) */
65 uint32_t flags; /**< flags for this frame */
66 uint64_t sse[4];
67 int have_sse; /**< true if we have pending sse[] */
68 uint64_t frame_number;
70};
71
72typedef struct AOMEncoderContext {
73 AVClass *class;
76 struct aom_codec_ctx encoder;
77 struct aom_codec_enc_cfg enccfg;
78 struct aom_image rawimg;
79 aom_codec_flags_t flags;
80 aom_img_fmt_t img_fmt;
81 struct aom_fixed_buf twopass_stats;
91 int crf;
96 uint64_t sse[4];
97 int have_sse; /**< true if we have pending sse[] */
98 uint64_t frame_number;
106 aom_superblock_size_t superblock_size;
113 int usage;
114 int tune;
145} AOMContext;
146
147#define OFFSET(x) offsetof(AOMContext, x)
148
149static const char *const ctlidstr[] = {
150 [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED",
151 [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL",
152 [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF",
153 [AOME_SET_ARNR_MAXFRAMES] = "AOME_SET_ARNR_MAXFRAMES",
154 [AOME_SET_ARNR_STRENGTH] = "AOME_SET_ARNR_STRENGTH",
155 [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD",
156 [AV1E_SET_COLOR_RANGE] = "AV1E_SET_COLOR_RANGE",
157 [AV1E_SET_COLOR_PRIMARIES] = "AV1E_SET_COLOR_PRIMARIES",
158 [AV1E_SET_MATRIX_COEFFICIENTS] = "AV1E_SET_MATRIX_COEFFICIENTS",
159 [AV1E_SET_TRANSFER_CHARACTERISTICS] = "AV1E_SET_TRANSFER_CHARACTERISTICS",
160 [AV1E_SET_AQ_MODE] = "AV1E_SET_AQ_MODE",
161 [AV1E_SET_FRAME_PARALLEL_DECODING] = "AV1E_SET_FRAME_PARALLEL_DECODING",
162 [AV1E_SET_SUPERBLOCK_SIZE] = "AV1E_SET_SUPERBLOCK_SIZE",
163 [AV1E_SET_TILE_COLUMNS] = "AV1E_SET_TILE_COLUMNS",
164 [AV1E_SET_TILE_ROWS] = "AV1E_SET_TILE_ROWS",
165 [AV1E_SET_ENABLE_RESTORATION] = "AV1E_SET_ENABLE_RESTORATION",
166 [AV1E_SET_ROW_MT] = "AV1E_SET_ROW_MT",
167 [AV1E_SET_DENOISE_NOISE_LEVEL] = "AV1E_SET_DENOISE_NOISE_LEVEL",
168 [AV1E_SET_DENOISE_BLOCK_SIZE] = "AV1E_SET_DENOISE_BLOCK_SIZE",
169 [AV1E_SET_MAX_REFERENCE_FRAMES] = "AV1E_SET_MAX_REFERENCE_FRAMES",
170 [AV1E_SET_ENABLE_GLOBAL_MOTION] = "AV1E_SET_ENABLE_GLOBAL_MOTION",
171 [AV1E_SET_ENABLE_INTRABC] = "AV1E_SET_ENABLE_INTRABC",
172 [AV1E_SET_ENABLE_CDEF] = "AV1E_SET_ENABLE_CDEF",
173 [AOME_SET_TUNING] = "AOME_SET_TUNING",
174 [AV1E_SET_ENABLE_1TO4_PARTITIONS] = "AV1E_SET_ENABLE_1TO4_PARTITIONS",
175 [AV1E_SET_ENABLE_AB_PARTITIONS] = "AV1E_SET_ENABLE_AB_PARTITIONS",
176 [AV1E_SET_ENABLE_RECT_PARTITIONS] = "AV1E_SET_ENABLE_RECT_PARTITIONS",
177 [AV1E_SET_ENABLE_ANGLE_DELTA] = "AV1E_SET_ENABLE_ANGLE_DELTA",
178 [AV1E_SET_ENABLE_CFL_INTRA] = "AV1E_SET_ENABLE_CFL_INTRA",
179 [AV1E_SET_ENABLE_FILTER_INTRA] = "AV1E_SET_ENABLE_FILTER_INTRA",
180 [AV1E_SET_ENABLE_INTRA_EDGE_FILTER] = "AV1E_SET_ENABLE_INTRA_EDGE_FILTER",
181 [AV1E_SET_ENABLE_PAETH_INTRA] = "AV1E_SET_ENABLE_PAETH_INTRA",
182 [AV1E_SET_ENABLE_SMOOTH_INTRA] = "AV1E_SET_ENABLE_SMOOTH_INTRA",
183 [AV1E_SET_ENABLE_PALETTE] = "AV1E_SET_ENABLE_PALETTE",
184 [AV1E_SET_ENABLE_FLIP_IDTX] = "AV1E_SET_ENABLE_FLIP_IDTX",
185 [AV1E_SET_ENABLE_TX64] = "AV1E_SET_ENABLE_TX64",
186 [AV1E_SET_INTRA_DCT_ONLY] = "AV1E_SET_INTRA_DCT_ONLY",
187 [AV1E_SET_INTER_DCT_ONLY] = "AV1E_SET_INTER_DCT_ONLY",
188 [AV1E_SET_INTRA_DEFAULT_TX_ONLY] = "AV1E_SET_INTRA_DEFAULT_TX_ONLY",
189 [AV1E_SET_REDUCED_TX_TYPE_SET] = "AV1E_SET_REDUCED_TX_TYPE_SET",
190 [AV1E_SET_ENABLE_DIFF_WTD_COMP] = "AV1E_SET_ENABLE_DIFF_WTD_COMP",
191 [AV1E_SET_ENABLE_DIST_WTD_COMP] = "AV1E_SET_ENABLE_DIST_WTD_COMP",
192 [AV1E_SET_ENABLE_DUAL_FILTER] = "AV1E_SET_ENABLE_DUAL_FILTER",
193 [AV1E_SET_ENABLE_INTERINTER_WEDGE] = "AV1E_SET_ENABLE_INTERINTER_WEDGE",
194 [AV1E_SET_ENABLE_INTERINTRA_WEDGE] = "AV1E_SET_ENABLE_INTERINTRA_WEDGE",
195 [AV1E_SET_ENABLE_MASKED_COMP] = "AV1E_SET_ENABLE_MASKED_COMP",
196 [AV1E_SET_ENABLE_INTERINTRA_COMP] = "AV1E_SET_ENABLE_INTERINTRA_COMP",
197 [AV1E_SET_ENABLE_OBMC] = "AV1E_SET_ENABLE_OBMC",
198 [AV1E_SET_ENABLE_ONESIDED_COMP] = "AV1E_SET_ENABLE_ONESIDED_COMP",
199 [AV1E_SET_REDUCED_REFERENCE_SET] = "AV1E_SET_REDUCED_REFERENCE_SET",
200 [AV1E_SET_ENABLE_SMOOTH_INTERINTRA] = "AV1E_SET_ENABLE_SMOOTH_INTERINTRA",
201 [AV1E_SET_ENABLE_REF_FRAME_MVS] = "AV1E_SET_ENABLE_REF_FRAME_MVS",
202#ifdef AOM_CTRL_AV1E_GET_NUM_OPERATING_POINTS
203 [AV1E_GET_NUM_OPERATING_POINTS] = "AV1E_GET_NUM_OPERATING_POINTS",
204#endif
205 [AV1E_GET_SEQ_LEVEL_IDX] = "AV1E_GET_SEQ_LEVEL_IDX",
206#ifdef AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX
207 [AV1E_GET_TARGET_SEQ_LEVEL_IDX] = "AV1E_GET_TARGET_SEQ_LEVEL_IDX",
208#endif
209 [AV1_GET_NEW_FRAME_IMAGE] = "AV1_GET_NEW_FRAME_IMAGE",
210};
211
212static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
213{
214 AOMContext *ctx = avctx->priv_data;
215 const char *error = aom_codec_error(&ctx->encoder);
216 const char *detail = aom_codec_error_detail(&ctx->encoder);
217
218 av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
219 if (detail)
220 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
221}
222
224 const struct aom_codec_enc_cfg *cfg,
225 int level)
226{
227 int width = -30;
228
229 av_log(avctx, level, "aom_codec_enc_cfg\n");
230 av_log(avctx, level, "generic settings\n"
231 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
232 " %*s%u\n %*s%u\n"
233 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
234 width, "g_usage:", cfg->g_usage,
235 width, "g_threads:", cfg->g_threads,
236 width, "g_profile:", cfg->g_profile,
237 width, "g_w:", cfg->g_w,
238 width, "g_h:", cfg->g_h,
239 width, "g_bit_depth:", cfg->g_bit_depth,
240 width, "g_input_bit_depth:", cfg->g_input_bit_depth,
241 width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
242 width, "g_error_resilient:", cfg->g_error_resilient,
243 width, "g_pass:", cfg->g_pass,
244 width, "g_lag_in_frames:", cfg->g_lag_in_frames);
245 av_log(avctx, level, "rate control settings\n"
246 " %*s%u\n %*s%d\n %*s%p(%zu)\n %*s%u\n",
247 width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
248 width, "rc_end_usage:", cfg->rc_end_usage,
249 width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
250 width, "rc_target_bitrate:", cfg->rc_target_bitrate);
251 av_log(avctx, level, "quantizer settings\n"
252 " %*s%u\n %*s%u\n",
253 width, "rc_min_quantizer:", cfg->rc_min_quantizer,
254 width, "rc_max_quantizer:", cfg->rc_max_quantizer);
255 av_log(avctx, level, "bitrate tolerance\n"
256 " %*s%u\n %*s%u\n",
257 width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
258 width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
259 av_log(avctx, level, "decoder buffer model\n"
260 " %*s%u\n %*s%u\n %*s%u\n",
261 width, "rc_buf_sz:", cfg->rc_buf_sz,
262 width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
263 width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
264 av_log(avctx, level, "2 pass rate control settings\n"
265 " %*s%u\n %*s%u\n %*s%u\n",
266 width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
267 width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
268 width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
269 av_log(avctx, level, "keyframing settings\n"
270 " %*s%d\n %*s%u\n %*s%u\n",
271 width, "kf_mode:", cfg->kf_mode,
272 width, "kf_min_dist:", cfg->kf_min_dist,
273 width, "kf_max_dist:", cfg->kf_max_dist);
274 av_log(avctx, level, "tile settings\n"
275 " %*s%d\n %*s%d\n",
276 width, "tile_width_count:", cfg->tile_width_count,
277 width, "tile_height_count:", cfg->tile_height_count);
278 av_log(avctx, level, "\n");
279}
280
281static void coded_frame_add(void *list, struct FrameListData *cx_frame)
282{
283 struct FrameListData **p = list;
284
285 while (*p)
286 p = &(*p)->next;
287 *p = cx_frame;
288 cx_frame->next = NULL;
289}
290
291static av_cold void free_coded_frame(struct FrameListData *cx_frame)
292{
293 av_freep(&cx_frame->buf);
294 av_freep(&cx_frame);
295}
296
297static av_cold void free_frame_list(struct FrameListData *list)
298{
299 struct FrameListData *p = list;
300
301 while (p) {
302 list = list->next;
304 p = list;
305 }
306}
307
309 int id,
310 int val)
311{
312 AOMContext *ctx = avctx->priv_data;
313 char buf[80];
314 int width = -30;
315 int res;
316
317 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
318 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
319
320 res = aom_codec_control(&ctx->encoder, id, val);
321 if (res != AOM_CODEC_OK) {
322 snprintf(buf, sizeof(buf), "Failed to set %s codec control",
323 ctlidstr[id]);
324 log_encoder_error(avctx, buf);
325 return AVERROR(EINVAL);
326 }
327
328 return 0;
329}
330
331static int add_hdr_plus(AVCodecContext *avctx, struct aom_image *img, const AVFrame *frame)
332{
333 // Check for HDR10+
334 AVFrameSideData *side_data =
336 if (!side_data)
337 return 0;
338
339 size_t payload_size;
340 AVDynamicHDRPlus *hdr_plus = (AVDynamicHDRPlus *)side_data->buf->data;
341 int res = av_dynamic_hdr_plus_to_t35(hdr_plus, NULL, &payload_size);
342 if (res < 0) {
343 log_encoder_error(avctx, "Error finding the size of HDR10+");
344 return res;
345 }
346
347 uint8_t *hdr_plus_buf;
348 // Extra bytes for the country code, provider code, provider oriented code and app id.
349 const size_t hdr_plus_buf_size = payload_size + 6;
350 hdr_plus_buf = av_malloc(hdr_plus_buf_size);
351 if (!hdr_plus_buf)
352 return AVERROR(ENOMEM);
353
354 uint8_t *payload = hdr_plus_buf;
355 // See "HDR10+ AV1 Metadata Handling Specification" v1.0.1, Section 2.1.
356 bytestream_put_byte(&payload, ITU_T_T35_COUNTRY_CODE_US);
357 bytestream_put_be16(&payload, ITU_T_T35_PROVIDER_CODE_SAMSUNG);
358 bytestream_put_be16(&payload, 0x0001); // provider_oriented_code
359 bytestream_put_byte(&payload, 0x04); // application_identifier
360
361 res = av_dynamic_hdr_plus_to_t35(hdr_plus, &payload, &payload_size);
362 if (res < 0) {
363 av_free(hdr_plus_buf);
364 log_encoder_error(avctx, "Error encoding HDR10+ from side data");
365 return res;
366 }
367
368 res = aom_img_add_metadata(img, OBU_METADATA_TYPE_ITUT_T35,
369 hdr_plus_buf, hdr_plus_buf_size, AOM_MIF_ANY_FRAME);
370 av_free(hdr_plus_buf);
371 if (res < 0) {
372 log_encoder_error(avctx, "Error adding HDR10+ to aom_img");
373 return res;
374 }
375 return 0;
376}
377
378static int add_hdr_smpte2094_app5(AVCodecContext *avctx, struct aom_image *img,
379 const AVFrame *frame)
380{
381 AVFrameSideData *side_data =
383 if (!side_data)
384 return 0;
385
386 size_t payload_size;
388 int res = av_dynamic_hdr_smpte2094_app5_to_t35(hdr, NULL, &payload_size);
389 if (res < 0) {
390 log_encoder_error(avctx, "Error finding the size of HDR SMPTE-2094-50");
391 return res;
392 }
393
394 uint8_t *hdr_buf;
395 // Extra bytes for the country code, provider code, provider oriented code.
396 const size_t hdr_buf_size = payload_size + 5;
397 hdr_buf = av_malloc(hdr_buf_size);
398 if (!hdr_buf)
399 return AVERROR(ENOMEM);
400
401 uint8_t *payload = hdr_buf;
402 bytestream_put_byte(&payload, ITU_T_T35_COUNTRY_CODE_US);
403 bytestream_put_be16(&payload, ITU_T_T35_PROVIDER_CODE_SMPTE);
404 bytestream_put_be16(&payload, 0x0001); // provider_oriented_code
405
406 res = av_dynamic_hdr_smpte2094_app5_to_t35(hdr, &payload, &payload_size);
407 if (res < 0) {
408 av_free(hdr_buf);
409 log_encoder_error(avctx, "Error encoding HDR SMPTE-2094-50 from side data");
410 return res;
411 }
412
413 res = aom_img_add_metadata(img, OBU_METADATA_TYPE_ITUT_T35,
414 hdr_buf, hdr_buf_size, AOM_MIF_ANY_FRAME);
415 av_free(hdr_buf);
416 if (res < 0) {
417 log_encoder_error(avctx, "Error adding HDR SMPTE-2094-50 to aom_img");
418 return res;
419 }
420 return 0;
421}
422
423#if defined(AOM_CTRL_AV1E_GET_NUM_OPERATING_POINTS) && \
424 defined(AOM_CTRL_AV1E_GET_SEQ_LEVEL_IDX) && \
425 defined(AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX)
426static av_cold int codecctl_intp(AVCodecContext *avctx,
427 int id,
428 int* ptr)
429{
430 AOMContext *ctx = avctx->priv_data;
431 char buf[80];
432 int width = -30;
433 int res;
434
435 res = aom_codec_control(&ctx->encoder, id, ptr);
436 if (res != AOM_CODEC_OK) {
437 snprintf(buf, sizeof(buf), "Failed to get %s codec control",
438 ctlidstr[id]);
439 log_encoder_error(avctx, buf);
440 return AVERROR(EINVAL);
441 }
442
443 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
444 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, *ptr);
445
446 return 0;
447}
448#endif
449
451 int id,
452 struct aom_image *img)
453{
454 AOMContext *ctx = avctx->priv_data;
455 char buf[80];
456 int res;
457
458 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
459
460 res = aom_codec_control(&ctx->encoder, id, img);
461 if (res != AOM_CODEC_OK) {
462 snprintf(buf, sizeof(buf), "Failed to get %s codec control",
463 ctlidstr[id]);
464 log_encoder_error(avctx, buf);
465 return AVERROR(EINVAL);
466 }
467
468 return 0;
469}
470
472{
473 AOMContext *ctx = avctx->priv_data;
474
475#if defined(AOM_CTRL_AV1E_GET_NUM_OPERATING_POINTS) && \
476 defined(AOM_CTRL_AV1E_GET_SEQ_LEVEL_IDX) && \
477 defined(AOM_CTRL_AV1E_GET_TARGET_SEQ_LEVEL_IDX)
478 if (ctx->encoder.iface && !(avctx->flags & AV_CODEC_FLAG_PASS1)) {
479 int num_operating_points;
480 int levels[32];
481 int target_levels[32];
482
483 if (!codecctl_intp(avctx, AV1E_GET_NUM_OPERATING_POINTS,
484 &num_operating_points) &&
485 !codecctl_intp(avctx, AV1E_GET_SEQ_LEVEL_IDX, levels) &&
486 !codecctl_intp(avctx, AV1E_GET_TARGET_SEQ_LEVEL_IDX,
487 target_levels)) {
488 for (int i = 0; i < num_operating_points; i++) {
489 if (levels[i] > target_levels[i]) {
490 // Warn when the target level was not met
491 av_log(avctx, AV_LOG_WARNING,
492 "Could not encode to target level %d.%d for "
493 "operating point %d. The output level is %d.%d.\n",
494 2 + (target_levels[i] >> 2), target_levels[i] & 3,
495 i, 2 + (levels[i] >> 2), levels[i] & 3);
496 } else if (target_levels[i] < 31) {
497 // Log the encoded level if a target level was given
498 av_log(avctx, AV_LOG_INFO,
499 "Output level for operating point %d is %d.%d.\n",
500 i, 2 + (levels[i] >> 2), levels[i] & 3);
501 }
502 }
503 }
504 }
505#endif
506
507 aom_codec_destroy(&ctx->encoder);
508 aom_img_remove_metadata(&ctx->rawimg);
509 av_freep(&ctx->twopass_stats.buf);
510 av_freep(&avctx->stats_out);
511 free_frame_list(ctx->coded_frame_list);
512 av_bsf_free(&ctx->bsf);
513 ff_dovi_ctx_unref(&ctx->dovi);
514 return 0;
515}
516
517static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps,
518 struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags,
519 aom_img_fmt_t *img_fmt)
520{
522 enccfg->g_bit_depth = enccfg->g_input_bit_depth = desc->comp[0].depth;
523 switch (avctx->pix_fmt) {
524 case AV_PIX_FMT_GRAY8:
525 enccfg->monochrome = 1;
528 enccfg->g_profile = AV_PROFILE_AV1_MAIN;
529 *img_fmt = AOM_IMG_FMT_I420;
530 return 0;
532 enccfg->g_profile = AV_PROFILE_AV1_PROFESSIONAL;
533 *img_fmt = AOM_IMG_FMT_I422;
534 return 0;
536 case AV_PIX_FMT_GBRP:
537 enccfg->g_profile = AV_PROFILE_AV1_HIGH;
538 *img_fmt = AOM_IMG_FMT_I444;
539 return 0;
542 enccfg->monochrome = 1;
546 if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
547 enccfg->g_profile =
548 enccfg->g_bit_depth == 10 ? AV_PROFILE_AV1_MAIN : AV_PROFILE_AV1_PROFESSIONAL;
549 *img_fmt = AOM_IMG_FMT_I42016;
550 *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
551 return 0;
552 }
553 break;
556 if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
557 enccfg->g_profile = AV_PROFILE_AV1_PROFESSIONAL;
558 *img_fmt = AOM_IMG_FMT_I42216;
559 *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
560 return 0;
561 }
562 break;
567 if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
568 enccfg->g_profile =
569 enccfg->g_bit_depth == 10 ? AV_PROFILE_AV1_HIGH : AV_PROFILE_AV1_PROFESSIONAL;
570 *img_fmt = AOM_IMG_FMT_I44416;
571 *flags |= AOM_CODEC_USE_HIGHBITDEPTH;
572 return 0;
573 }
574 break;
575 default:
576 break;
577 }
578 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format.\n");
579 return AVERROR_INVALIDDATA;
580}
581
583{
584 aom_color_range_t aom_cr;
585 switch (avctx->color_range) {
587 case AVCOL_RANGE_MPEG: aom_cr = AOM_CR_STUDIO_RANGE; break;
588 case AVCOL_RANGE_JPEG: aom_cr = AOM_CR_FULL_RANGE; break;
589 default:
590 av_log(avctx, AV_LOG_WARNING, "Unsupported color range (%d)\n",
591 avctx->color_range);
592 return;
593 }
594
595 codecctl_int(avctx, AV1E_SET_COLOR_RANGE, aom_cr);
596}
597
598static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
599{
600 int sb_dim = (dim + sb_size - 1) / sb_size;
601 int tile_dim = (sb_dim + (1 << tiles_log2) - 1) >> tiles_log2;
602 av_assert0(tile_dim > 0);
603 return (sb_dim + tile_dim - 1) / tile_dim;
604}
605
607 struct aom_codec_enc_cfg *enccfg)
608{
609 AOMContext *ctx = avctx->priv_data;
610 int sb_128x128_possible, sb_size, sb_width, sb_height;
611 int uniform_rows, uniform_cols;
612 int uniform_64x64_possible, uniform_128x128_possible;
613 int tile_size, rounding, i;
614
615 if (ctx->tile_cols_log2 >= 0)
616 ctx->tile_cols = 1 << ctx->tile_cols_log2;
617 if (ctx->tile_rows_log2 >= 0)
618 ctx->tile_rows = 1 << ctx->tile_rows_log2;
619
620 if (ctx->tile_cols == 0) {
621 ctx->tile_cols = (avctx->width + AV1_MAX_TILE_WIDTH - 1) /
623 if (ctx->tile_cols > 1) {
624 av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
625 "columns to fill width.\n", ctx->tile_cols);
626 }
627 }
628 av_assert0(ctx->tile_cols > 0);
629 if (ctx->tile_rows == 0) {
630 int max_tile_width =
631 FFALIGN((FFALIGN(avctx->width, 128) +
632 ctx->tile_cols - 1) / ctx->tile_cols, 128);
633 ctx->tile_rows =
634 (max_tile_width * FFALIGN(avctx->height, 128) +
636 if (ctx->tile_rows > 1) {
637 av_log(avctx, AV_LOG_DEBUG, "Automatically using %d tile "
638 "rows to fill area.\n", ctx->tile_rows);
639 }
640 }
641 av_assert0(ctx->tile_rows > 0);
642
643 if ((avctx->width + 63) / 64 < ctx->tile_cols ||
644 (avctx->height + 63) / 64 < ctx->tile_rows) {
645 av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: frame not "
646 "large enough to fit specified tile arrangement.\n");
647 return AVERROR(EINVAL);
648 }
649 if (ctx->tile_cols > AV1_MAX_TILE_COLS ||
650 ctx->tile_rows > AV1_MAX_TILE_ROWS) {
651 av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
652 "not allow more than %dx%d tiles.\n",
654 return AVERROR(EINVAL);
655 }
656 if (avctx->width / ctx->tile_cols > AV1_MAX_TILE_WIDTH) {
657 av_log(avctx, AV_LOG_ERROR, "Invalid tile sizing: AV1 does "
658 "not allow tiles of width greater than %d.\n",
660 return AVERROR(EINVAL);
661 }
662
663 ctx->superblock_size = AOM_SUPERBLOCK_SIZE_DYNAMIC;
664
665 if (ctx->tile_cols == 1 && ctx->tile_rows == 1) {
666 av_log(avctx, AV_LOG_DEBUG, "Using a single tile.\n");
667 return 0;
668 }
669
670 sb_128x128_possible =
671 (avctx->width + 127) / 128 >= ctx->tile_cols &&
672 (avctx->height + 127) / 128 >= ctx->tile_rows;
673
674 ctx->tile_cols_log2 = ctx->tile_cols == 1 ? 0 :
675 av_log2(ctx->tile_cols - 1) + 1;
676 ctx->tile_rows_log2 = ctx->tile_rows == 1 ? 0 :
677 av_log2(ctx->tile_rows - 1) + 1;
678
679 uniform_cols = count_uniform_tiling(avctx->width,
680 64, ctx->tile_cols_log2);
681 uniform_rows = count_uniform_tiling(avctx->height,
682 64, ctx->tile_rows_log2);
683 av_log(avctx, AV_LOG_DEBUG, "Uniform with 64x64 superblocks "
684 "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
685 uniform_64x64_possible = uniform_cols == ctx->tile_cols &&
686 uniform_rows == ctx->tile_rows;
687
688 if (sb_128x128_possible) {
689 uniform_cols = count_uniform_tiling(avctx->width,
690 128, ctx->tile_cols_log2);
691 uniform_rows = count_uniform_tiling(avctx->height,
692 128, ctx->tile_rows_log2);
693 av_log(avctx, AV_LOG_DEBUG, "Uniform with 128x128 superblocks "
694 "-> %dx%d tiles.\n", uniform_cols, uniform_rows);
695 uniform_128x128_possible = uniform_cols == ctx->tile_cols &&
696 uniform_rows == ctx->tile_rows;
697 } else {
698 av_log(avctx, AV_LOG_DEBUG, "128x128 superblocks not possible.\n");
699 uniform_128x128_possible = 0;
700 }
701
702 ctx->uniform_tiles = 1;
703 if (uniform_64x64_possible && uniform_128x128_possible) {
704 av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with dynamic "
705 "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
706 ctx->tile_cols_log2, ctx->tile_rows_log2);
707 return 0;
708 }
709 if (uniform_64x64_possible && !sb_128x128_possible) {
710 av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 64x64 "
711 "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
712 ctx->tile_cols_log2, ctx->tile_rows_log2);
713 ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
714 return 0;
715 }
716 if (uniform_128x128_possible) {
717 av_log(avctx, AV_LOG_DEBUG, "Using uniform tiling with 128x128 "
718 "superblocks (tile_cols_log2 = %d, tile_rows_log2 = %d).\n",
719 ctx->tile_cols_log2, ctx->tile_rows_log2);
720 ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
721 return 0;
722 }
723 ctx->uniform_tiles = 0;
724
725 if (sb_128x128_possible) {
726 sb_size = 128;
727 ctx->superblock_size = AOM_SUPERBLOCK_SIZE_128X128;
728 } else {
729 sb_size = 64;
730 ctx->superblock_size = AOM_SUPERBLOCK_SIZE_64X64;
731 }
732 av_log(avctx, AV_LOG_DEBUG, "Using fixed tiling with %dx%d "
733 "superblocks (tile_cols = %d, tile_rows = %d).\n",
734 sb_size, sb_size, ctx->tile_cols, ctx->tile_rows);
735
736 enccfg->tile_width_count = ctx->tile_cols;
737 enccfg->tile_height_count = ctx->tile_rows;
738
739 sb_width = (avctx->width + sb_size - 1) / sb_size;
740 sb_height = (avctx->height + sb_size - 1) / sb_size;
741
742 tile_size = sb_width / ctx->tile_cols;
743 rounding = sb_width % ctx->tile_cols;
744 for (i = 0; i < ctx->tile_cols; i++) {
745 enccfg->tile_widths[i] = tile_size +
746 (i < rounding / 2 ||
747 i > ctx->tile_cols - 1 - (rounding + 1) / 2);
748 }
749
750 tile_size = sb_height / ctx->tile_rows;
751 rounding = sb_height % ctx->tile_rows;
752 for (i = 0; i < ctx->tile_rows; i++) {
753 enccfg->tile_heights[i] = tile_size +
754 (i < rounding / 2 ||
755 i > ctx->tile_rows - 1 - (rounding + 1) / 2);
756 }
757
758 return 0;
759}
760
761static const struct {
763 unsigned offset;
764} option_map[] = {
765 { AOME_SET_ENABLEAUTOALTREF, OFFSET(auto_alt_ref) },
766 { AOME_SET_ARNR_MAXFRAMES, OFFSET(arnr_max_frames) },
767 { AOME_SET_ARNR_STRENGTH, OFFSET(arnr_strength) },
768 { AV1E_SET_ENABLE_CDEF, OFFSET(enable_cdef) },
769 { AV1E_SET_ENABLE_RESTORATION, OFFSET(enable_restoration) },
770 { AV1E_SET_ENABLE_RECT_PARTITIONS, OFFSET(enable_rect_partitions) },
771 { AV1E_SET_ENABLE_1TO4_PARTITIONS, OFFSET(enable_1to4_partitions) },
772 { AV1E_SET_ENABLE_AB_PARTITIONS, OFFSET(enable_ab_partitions) },
773 { AV1E_SET_ENABLE_ANGLE_DELTA, OFFSET(enable_angle_delta) },
774 { AV1E_SET_ENABLE_CFL_INTRA, OFFSET(enable_cfl_intra) },
775 { AV1E_SET_ENABLE_FILTER_INTRA, OFFSET(enable_filter_intra) },
776 { AV1E_SET_ENABLE_INTRA_EDGE_FILTER, OFFSET(enable_intra_edge_filter) },
777 { AV1E_SET_ENABLE_PAETH_INTRA, OFFSET(enable_paeth_intra) },
778 { AV1E_SET_ENABLE_SMOOTH_INTRA, OFFSET(enable_smooth_intra) },
779 { AV1E_SET_ENABLE_PALETTE, OFFSET(enable_palette) },
780 { AV1E_SET_ENABLE_TX64, OFFSET(enable_tx64) },
781 { AV1E_SET_ENABLE_FLIP_IDTX, OFFSET(enable_flip_idtx) },
782 { AV1E_SET_INTRA_DCT_ONLY, OFFSET(use_intra_dct_only) },
783 { AV1E_SET_INTER_DCT_ONLY, OFFSET(use_inter_dct_only) },
784 { AV1E_SET_INTRA_DEFAULT_TX_ONLY, OFFSET(use_intra_default_tx_only) },
785 { AV1E_SET_REDUCED_TX_TYPE_SET, OFFSET(reduced_tx_type_set) },
786 { AV1E_SET_ENABLE_REF_FRAME_MVS, OFFSET(enable_ref_frame_mvs) },
787 { AV1E_SET_REDUCED_REFERENCE_SET, OFFSET(enable_reduced_reference_set) },
788 { AV1E_SET_ENABLE_DIFF_WTD_COMP, OFFSET(enable_diff_wtd_comp) },
789 { AV1E_SET_ENABLE_DIST_WTD_COMP, OFFSET(enable_dist_wtd_comp) },
790 { AV1E_SET_ENABLE_DUAL_FILTER, OFFSET(enable_dual_filter) },
791 { AV1E_SET_ENABLE_INTERINTER_WEDGE, OFFSET(enable_interinter_wedge) },
792 { AV1E_SET_ENABLE_MASKED_COMP, OFFSET(enable_masked_comp) },
793 { AV1E_SET_ENABLE_INTERINTRA_COMP, OFFSET(enable_interintra_comp) },
794 { AV1E_SET_ENABLE_INTERINTRA_WEDGE, OFFSET(enable_interintra_wedge) },
795 { AV1E_SET_ENABLE_OBMC, OFFSET(enable_obmc) },
796 { AV1E_SET_ENABLE_ONESIDED_COMP, OFFSET(enable_onesided_comp) },
797 { AV1E_SET_ENABLE_SMOOTH_INTERINTRA, OFFSET(enable_smooth_interintra) },
799
801 const struct aom_codec_iface *iface)
802{
803 AOMContext *ctx = avctx->priv_data;
804 int res;
805 aom_codec_caps_t codec_caps = aom_codec_get_caps(iface);
806
807 ctx->flags = (avctx->flags & AV_CODEC_FLAG_PSNR) ? AOM_CODEC_USE_PSNR : 0;
808
809 if ((res = aom_codec_enc_config_default(iface, &ctx->enccfg, ctx->usage)) != AOM_CODEC_OK) {
810 av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
811 aom_codec_err_to_string(res));
812 return AVERROR(EINVAL);
813 }
814
815 if (set_pix_fmt(avctx, codec_caps, &ctx->enccfg, &ctx->flags, &ctx->img_fmt))
816 return AVERROR(EINVAL);
817
818 if(!avctx->bit_rate)
819 if(avctx->rc_max_rate || avctx->rc_buffer_size || avctx->rc_initial_buffer_occupancy) {
820 av_log( avctx, AV_LOG_ERROR, "Rate control parameters set without a bitrate\n");
821 return AVERROR(EINVAL);
822 }
823
824 ctx->enccfg.g_w = avctx->width;
825 ctx->enccfg.g_h = avctx->height;
826 ctx->enccfg.g_timebase.num = avctx->time_base.num;
827 ctx->enccfg.g_timebase.den = avctx->time_base.den;
828 ctx->enccfg.g_threads =
829 FFMIN(avctx->thread_count ? avctx->thread_count : av_cpu_count(), 64);
830
831 if (ctx->lag_in_frames >= 0)
832 ctx->enccfg.g_lag_in_frames = ctx->lag_in_frames;
833
834 if (avctx->rc_min_rate == avctx->rc_max_rate &&
835 avctx->rc_min_rate == avctx->bit_rate && avctx->bit_rate) {
836 ctx->enccfg.rc_end_usage = AOM_CBR;
837 } else if (ctx->crf >= 0) {
838 ctx->enccfg.rc_end_usage = AOM_CQ;
839 if (!avctx->bit_rate)
840 ctx->enccfg.rc_end_usage = AOM_Q;
841 }
842
843 if (avctx->bit_rate) {
844 ctx->enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
846 } else if (ctx->enccfg.rc_end_usage != AOM_Q) {
847 ctx->enccfg.rc_end_usage = AOM_Q;
848 ctx->crf = 32;
849 av_log(avctx, AV_LOG_WARNING,
850 "Neither bitrate nor constrained quality specified, using default CRF of %d\n",
851 ctx->crf);
852 }
853
854 if (avctx->qmin >= 0)
855 ctx->enccfg.rc_min_quantizer = avctx->qmin;
856 if (avctx->qmax >= 0) {
857 ctx->enccfg.rc_max_quantizer = avctx->qmax;
858 } else if (!ctx->crf) {
859 ctx->enccfg.rc_max_quantizer = 0;
860 }
861
862 if (ctx->enccfg.rc_end_usage == AOM_CQ || ctx->enccfg.rc_end_usage == AOM_Q) {
863 if (ctx->crf < ctx->enccfg.rc_min_quantizer || ctx->crf > ctx->enccfg.rc_max_quantizer) {
864 av_log(avctx, AV_LOG_ERROR,
865 "CQ level %d must be between minimum and maximum quantizer value (%d-%d)\n",
866 ctx->crf, ctx->enccfg.rc_min_quantizer, ctx->enccfg.rc_max_quantizer);
867 return AVERROR(EINVAL);
868 }
869 }
870
871 ctx->enccfg.rc_dropframe_thresh = ctx->drop_threshold;
872
873 // 0-100 (0 => CBR, 100 => VBR)
874 ctx->enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
875 if (ctx->minsection_pct >= 0)
876 ctx->enccfg.rc_2pass_vbr_minsection_pct = ctx->minsection_pct;
877 else if (avctx->bit_rate)
878 ctx->enccfg.rc_2pass_vbr_minsection_pct =
879 avctx->rc_min_rate * 100LL / avctx->bit_rate;
880 if (ctx->maxsection_pct >= 0)
881 ctx->enccfg.rc_2pass_vbr_maxsection_pct = ctx->maxsection_pct;
882 else if (avctx->rc_max_rate)
883 ctx->enccfg.rc_2pass_vbr_maxsection_pct =
884 avctx->rc_max_rate * 100LL / avctx->bit_rate;
885
886 if (avctx->rc_buffer_size)
887 ctx->enccfg.rc_buf_sz =
888 avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
890 ctx->enccfg.rc_buf_initial_sz =
891 avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
892 ctx->enccfg.rc_buf_optimal_sz = ctx->enccfg.rc_buf_sz * 5 / 6;
893
894 if (ctx->rc_undershoot_pct >= 0)
895 ctx->enccfg.rc_undershoot_pct = ctx->rc_undershoot_pct;
896 if (ctx->rc_overshoot_pct >= 0)
897 ctx->enccfg.rc_overshoot_pct = ctx->rc_overshoot_pct;
898
899 // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO
900 if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
901 ctx->enccfg.kf_min_dist = avctx->keyint_min;
902 if (avctx->gop_size >= 0)
903 ctx->enccfg.kf_max_dist = avctx->gop_size;
904
905 /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
906 * complexity playback on low powered devices at the expense of encode
907 * quality. */
908 if (avctx->profile != AV_PROFILE_UNKNOWN)
909 ctx->enccfg.g_profile = avctx->profile;
910
911 ctx->enccfg.g_error_resilient = ctx->error_resilient;
912
913 res = choose_tiling(avctx, &ctx->enccfg);
914 if (res < 0)
915 return res;
916
917 if (ctx->still_picture) {
918 // Set the maximum number of frames to 1. This will let libaom set
919 // still_picture and reduced_still_picture_header to 1 in the Sequence
920 // Header as required by AVIF still images.
921 ctx->enccfg.g_limit = 1;
922 // Reduce memory usage for still images.
923 ctx->enccfg.g_lag_in_frames = 0;
924 // All frames will be key frames.
925 ctx->enccfg.kf_max_dist = 0;
926 ctx->enccfg.kf_mode = AOM_KF_DISABLED;
927 }
928
929 return 0;
930}
931
933{
934 AOMContext *ctx = avctx->priv_data;
936 aom_codec_caps_t codec_caps = aom_codec_get_caps(ctx->encoder.iface);
937
938 // codec control failures are currently treated only as warnings
939 av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n");
940 codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used);
941
942 for (size_t i = 0; i < FF_ARRAY_ELEMS(option_map); ++i) {
943 int val = *(int*)((char*)ctx + option_map[i].offset);
944 if (val >= 0)
946 }
947 codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh);
948 if (ctx->crf >= 0)
949 codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf);
950 if (ctx->tune >= 0)
951 codecctl_int(avctx, AOME_SET_TUNING, ctx->tune);
952
953 if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
954 codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, AVCOL_PRI_BT709);
955 codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, AVCOL_SPC_RGB);
956 codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, AVCOL_TRC_IEC61966_2_1);
957 } else {
958 codecctl_int(avctx, AV1E_SET_COLOR_PRIMARIES, avctx->color_primaries);
959 codecctl_int(avctx, AV1E_SET_MATRIX_COEFFICIENTS, avctx->colorspace);
960 codecctl_int(avctx, AV1E_SET_TRANSFER_CHARACTERISTICS, avctx->color_trc);
961 }
962 if (ctx->aq_mode >= 0)
963 codecctl_int(avctx, AV1E_SET_AQ_MODE, ctx->aq_mode);
964 if (ctx->frame_parallel >= 0)
965 codecctl_int(avctx, AV1E_SET_FRAME_PARALLEL_DECODING, ctx->frame_parallel);
966 set_color_range(avctx);
967
968 codecctl_int(avctx, AV1E_SET_SUPERBLOCK_SIZE, ctx->superblock_size);
969 if (ctx->uniform_tiles) {
970 codecctl_int(avctx, AV1E_SET_TILE_COLUMNS, ctx->tile_cols_log2);
971 codecctl_int(avctx, AV1E_SET_TILE_ROWS, ctx->tile_rows_log2);
972 }
973
974 if (ctx->denoise_noise_level >= 0)
975 codecctl_int(avctx, AV1E_SET_DENOISE_NOISE_LEVEL, ctx->denoise_noise_level);
976 if (ctx->denoise_block_size >= 0)
977 codecctl_int(avctx, AV1E_SET_DENOISE_BLOCK_SIZE, ctx->denoise_block_size);
978 if (ctx->enable_global_motion >= 0)
979 codecctl_int(avctx, AV1E_SET_ENABLE_GLOBAL_MOTION, ctx->enable_global_motion);
980 if (avctx->refs >= 3) {
981 codecctl_int(avctx, AV1E_SET_MAX_REFERENCE_FRAMES, avctx->refs);
982 }
983 if (ctx->row_mt >= 0)
984 codecctl_int(avctx, AV1E_SET_ROW_MT, ctx->row_mt);
985 if (ctx->enable_intrabc >= 0)
986 codecctl_int(avctx, AV1E_SET_ENABLE_INTRABC, ctx->enable_intrabc);
987
988#if AOM_ENCODER_ABI_VERSION >= 23
989 {
990 const AVDictionaryEntry *en = NULL;
991
992 while ((en = av_dict_iterate(ctx->aom_params, en))) {
993 int ret = aom_codec_set_option(&ctx->encoder, en->key, en->value);
994 if (ret != AOM_CODEC_OK) {
995 log_encoder_error(avctx, en->key);
996 return AVERROR_EXTERNAL;
997 }
998 }
999 }
1000#endif
1001
1002 // provide dummy value to initialize wrapper, values will be updated each _encode()
1003 aom_img_wrap(&ctx->rawimg, ctx->img_fmt, avctx->width, avctx->height, 1,
1004 (unsigned char*)1);
1005
1006 if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH)
1007 ctx->rawimg.bit_depth = ctx->enccfg.g_bit_depth;
1008
1009 return 0;
1010}
1011
1013 const struct aom_codec_iface *iface)
1014{
1015 AOMContext *ctx = avctx->priv_data;
1016 int res;
1017
1018 av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
1019 av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
1020
1021 res = aom_config(avctx, iface);
1022 if (res < 0)
1023 return res;
1024
1025 if (avctx->flags & AV_CODEC_FLAG_PASS1)
1026 ctx->enccfg.g_pass = AOM_RC_FIRST_PASS;
1027 else if (avctx->flags & AV_CODEC_FLAG_PASS2)
1028 ctx->enccfg.g_pass = AOM_RC_LAST_PASS;
1029 else
1030 ctx->enccfg.g_pass = AOM_RC_ONE_PASS;
1031
1032 if (ctx->enccfg.g_pass == AOM_RC_FIRST_PASS)
1033 ctx->enccfg.g_lag_in_frames = 0;
1034 else if (ctx->enccfg.g_pass == AOM_RC_LAST_PASS) {
1035 int decode_size, ret;
1036
1037 if (!avctx->stats_in) {
1038 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
1039 return AVERROR_INVALIDDATA;
1040 }
1041
1042 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
1043 ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
1044 if (ret < 0) {
1045 av_log(avctx, AV_LOG_ERROR,
1046 "Stat buffer alloc (%zu bytes) failed\n",
1047 ctx->twopass_stats.sz);
1048 ctx->twopass_stats.sz = 0;
1049 return ret;
1050 }
1051 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
1052 ctx->twopass_stats.sz);
1053 if (decode_size < 0) {
1054 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
1055 return AVERROR_INVALIDDATA;
1056 }
1057
1058 ctx->twopass_stats.sz = decode_size;
1059 ctx->enccfg.rc_twopass_stats_in = ctx->twopass_stats;
1060 }
1061
1062 /* Construct Encoder Context */
1063 res = aom_codec_enc_init(&ctx->encoder, iface, &ctx->enccfg, ctx->flags);
1064 if (res != AOM_CODEC_OK) {
1065 dump_enc_cfg(avctx, &ctx->enccfg, AV_LOG_WARNING);
1066 log_encoder_error(avctx, "Failed to initialize encoder");
1067 return AVERROR(EINVAL);
1068 }
1069 dump_enc_cfg(avctx, &ctx->enccfg, AV_LOG_DEBUG);
1070
1071 res = aom_codecctl(avctx);
1072 if (res < 0)
1073 return res;
1074
1075 ctx->dovi.logctx = avctx;
1076 if ((res = ff_dovi_configure(&ctx->dovi, avctx)) < 0)
1077 return res;
1078
1079 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1080 const AVBitStreamFilter *filter = av_bsf_get_by_name("extract_extradata");
1081 int ret;
1082
1083 if (!filter) {
1084 av_log(avctx, AV_LOG_ERROR, "extract_extradata bitstream filter "
1085 "not found. This is a bug, please report it.\n");
1086 return AVERROR_BUG;
1087 }
1088 ret = av_bsf_alloc(filter, &ctx->bsf);
1089 if (ret < 0)
1090 return ret;
1091
1092 ret = avcodec_parameters_from_context(ctx->bsf->par_in, avctx);
1093 if (ret < 0)
1094 return ret;
1095
1096 ret = av_bsf_init(ctx->bsf);
1097 if (ret < 0)
1098 return ret;
1099 }
1100
1101 AVCPBProperties *cpb_props = ff_encode_add_cpb_side_data(avctx);
1102 if (!cpb_props)
1103 return AVERROR(ENOMEM);
1104
1105 if (ctx->enccfg.rc_end_usage == AOM_CBR ||
1106 ctx->enccfg.g_pass != AOM_RC_ONE_PASS) {
1107 cpb_props->max_bitrate = avctx->rc_max_rate;
1108 cpb_props->min_bitrate = avctx->rc_min_rate;
1109 cpb_props->avg_bitrate = avctx->bit_rate;
1110 }
1111 cpb_props->buffer_size = avctx->rc_buffer_size;
1112
1113 return 0;
1114}
1115
1116static inline void cx_pktcpy(AOMContext *ctx,
1117 struct FrameListData *dst,
1118 const struct aom_codec_cx_pkt *src)
1119{
1120 dst->pts = src->data.frame.pts;
1121 dst->duration = src->data.frame.duration;
1122 dst->flags = src->data.frame.flags;
1123 dst->sz = src->data.frame.sz;
1124 dst->buf = src->data.frame.buf;
1125 dst->frame_number = ++ctx->frame_number;
1126 dst->have_sse = ctx->have_sse;
1127 if (ctx->have_sse) {
1128 /* associate last-seen SSE to the frame. */
1129 /* Transfers ownership from ctx to dst. */
1130 memcpy(dst->sse, ctx->sse, sizeof(dst->sse));
1131 ctx->have_sse = 0;
1132 }
1133}
1134
1135/**
1136 * Store coded frame information in format suitable for return from encode2().
1137 *
1138 * Write information from @a cx_frame to @a pkt
1139 * @return packet data size on success
1140 * @return a negative AVERROR on error
1141 */
1142static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
1143 AVPacket *pkt)
1144{
1145 AOMContext *ctx = avctx->priv_data;
1146 enum AVPictureType pict_type;
1147 int ret = ff_get_encode_buffer(avctx, pkt, cx_frame->sz, 0);
1148 if (ret < 0) {
1149 av_log(avctx, AV_LOG_ERROR,
1150 "Error getting output packet of size %zu.\n", cx_frame->sz);
1151 return ret;
1152 }
1153 memcpy(pkt->data, cx_frame->buf, pkt->size);
1154 pkt->pts = pkt->dts = cx_frame->pts;
1155 pkt->duration = cx_frame->duration;
1156
1157 if (!!(cx_frame->flags & AOM_FRAME_IS_KEY)) {
1158 pkt->flags |= AV_PKT_FLAG_KEY;
1159 pict_type = AV_PICTURE_TYPE_I;
1160 } else if (cx_frame->flags & AOM_FRAME_IS_INTRAONLY) {
1161 pict_type = AV_PICTURE_TYPE_I;
1162 } else {
1163 pict_type = AV_PICTURE_TYPE_P;
1164 }
1165
1166 ff_encode_add_stats_side_data(pkt, 0, cx_frame->sse + 1,
1167 cx_frame->have_sse ? 3 : 0, pict_type);
1168
1169 if (cx_frame->have_sse) {
1170 int i;
1171 for (i = 0; i < 3; ++i) {
1172 avctx->error[i] += cx_frame->sse[i + 1];
1173 }
1174 cx_frame->have_sse = 0;
1175 }
1176
1177 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
1178 ret = av_bsf_send_packet(ctx->bsf, pkt);
1179 if (ret < 0) {
1180 av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
1181 "failed to send input packet\n");
1182 return ret;
1183 }
1184 ret = av_bsf_receive_packet(ctx->bsf, pkt);
1185
1186 if (ret < 0) {
1187 av_log(avctx, AV_LOG_ERROR, "extract_extradata filter "
1188 "failed to receive output packet\n");
1189 return ret;
1190 }
1191 }
1192 return pkt->size;
1193}
1194
1195/**
1196 * Queue multiple output frames from the encoder, returning the front-most.
1197 * In cases where aom_codec_get_cx_data() returns more than 1 frame append
1198 * the frame queue. Return the head frame if available.
1199 * @return Stored frame size
1200 * @return AVERROR(EINVAL) on output size error
1201 * @return AVERROR(ENOMEM) on coded frame queue data allocation error
1202 */
1203static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
1204{
1205 AOMContext *ctx = avctx->priv_data;
1206 const struct aom_codec_cx_pkt *pkt;
1207 const void *iter = NULL;
1208 int size = 0;
1209
1210 if (ctx->coded_frame_list) {
1211 struct FrameListData *cx_frame = ctx->coded_frame_list;
1212 /* return the leading frame if we've already begun queueing */
1213 size = storeframe(avctx, cx_frame, pkt_out);
1214 if (size < 0)
1215 return size;
1216 ctx->coded_frame_list = cx_frame->next;
1217 free_coded_frame(cx_frame);
1218 }
1219
1220 /* consume all available output from the encoder before returning. buffers
1221 * are only good through the next aom_codec call */
1222 while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) {
1223 switch (pkt->kind) {
1224 case AOM_CODEC_CX_FRAME_PKT:
1225 if (!size) {
1226 struct FrameListData cx_frame;
1227
1228 /* avoid storing the frame when the list is empty and we haven't yet
1229 * provided a frame for output */
1230 av_assert0(!ctx->coded_frame_list);
1231 cx_pktcpy(ctx, &cx_frame, pkt);
1232 size = storeframe(avctx, &cx_frame, pkt_out);
1233 if (size < 0)
1234 return size;
1235 } else {
1236 struct FrameListData *cx_frame =
1237 av_malloc(sizeof(struct FrameListData));
1238
1239 if (!cx_frame) {
1240 av_log(avctx, AV_LOG_ERROR,
1241 "Frame queue element alloc failed\n");
1242 return AVERROR(ENOMEM);
1243 }
1244 cx_pktcpy(ctx, cx_frame, pkt);
1245 cx_frame->buf = av_malloc(cx_frame->sz);
1246
1247 if (!cx_frame->buf) {
1248 av_log(avctx, AV_LOG_ERROR,
1249 "Data buffer alloc (%zu bytes) failed\n",
1250 cx_frame->sz);
1251 av_freep(&cx_frame);
1252 return AVERROR(ENOMEM);
1253 }
1254 memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
1255 coded_frame_add(&ctx->coded_frame_list, cx_frame);
1256 }
1257 break;
1258 case AOM_CODEC_STATS_PKT:
1259 {
1260 struct aom_fixed_buf *stats = &ctx->twopass_stats;
1261 uint8_t *tmp = av_fast_realloc(stats->buf,
1262 &ctx->twopass_stats_size,
1263 stats->sz +
1264 pkt->data.twopass_stats.sz);
1265 if (!tmp) {
1266 av_freep(&stats->buf);
1267 stats->sz = 0;
1268 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
1269 return AVERROR(ENOMEM);
1270 }
1271 stats->buf = tmp;
1272 memcpy((uint8_t *)stats->buf + stats->sz,
1273 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
1274 stats->sz += pkt->data.twopass_stats.sz;
1275 break;
1276 }
1277 case AOM_CODEC_PSNR_PKT:
1278 {
1279 av_assert0(!ctx->have_sse);
1280 ctx->sse[0] = pkt->data.psnr.sse[0];
1281 ctx->sse[1] = pkt->data.psnr.sse[1];
1282 ctx->sse[2] = pkt->data.psnr.sse[2];
1283 ctx->sse[3] = pkt->data.psnr.sse[3];
1284 ctx->have_sse = 1;
1285 break;
1286 }
1287 case AOM_CODEC_CUSTOM_PKT:
1288 // ignore unsupported/unrecognized packet types
1289 break;
1290 }
1291 }
1292
1293 return size;
1294}
1295
1296static enum AVPixelFormat aomfmt_to_pixfmt(struct aom_image *img)
1297{
1298 switch (img->fmt) {
1299 case AOM_IMG_FMT_I420:
1300 case AOM_IMG_FMT_I42016:
1301 if (img->bit_depth == 8)
1302 return img->monochrome ? AV_PIX_FMT_GRAY8 : AV_PIX_FMT_YUV420P;
1303 else if (img->bit_depth == 10)
1304 return img->monochrome ? AV_PIX_FMT_GRAY10 : AV_PIX_FMT_YUV420P10;
1305 else
1306 return img->monochrome ? AV_PIX_FMT_GRAY12 : AV_PIX_FMT_YUV420P12;
1307 case AOM_IMG_FMT_I422:
1308 case AOM_IMG_FMT_I42216:
1309 if (img->bit_depth == 8)
1310 return AV_PIX_FMT_YUV422P;
1311 else if (img->bit_depth == 10)
1312 return AV_PIX_FMT_YUV422P10;
1313 else
1314 return AV_PIX_FMT_YUV422P12;
1315 case AOM_IMG_FMT_I444:
1316 case AOM_IMG_FMT_I44416:
1317 if (img->bit_depth == 8)
1318 return AV_PIX_FMT_YUV444P;
1319 else if (img->bit_depth == 10)
1320 return AV_PIX_FMT_YUV444P10;
1321 else
1322 return AV_PIX_FMT_YUV444P12;
1323 };
1324 return AV_PIX_FMT_NONE;
1325}
1326
1328 const AVFrame *frame, int *got_packet)
1329{
1330 AOMContext *ctx = avctx->priv_data;
1331 struct aom_image *rawimg = NULL;
1332 int64_t timestamp = 0;
1333 unsigned long duration = 0;
1334 int res, coded_size;
1335 aom_enc_frame_flags_t flags = 0;
1336 AVFrameSideData *sd;
1337
1338 if (frame) {
1339 rawimg = &ctx->rawimg;
1340 aom_img_remove_metadata(rawimg);
1341 rawimg->planes[AOM_PLANE_Y] = frame->data[0];
1342 rawimg->planes[AOM_PLANE_U] = frame->data[1];
1343 rawimg->planes[AOM_PLANE_V] = frame->data[2];
1344 rawimg->stride[AOM_PLANE_Y] = frame->linesize[0];
1345 rawimg->stride[AOM_PLANE_U] = frame->linesize[1];
1346 rawimg->stride[AOM_PLANE_V] = frame->linesize[2];
1347 timestamp = frame->pts;
1348
1349 if (frame->duration > ULONG_MAX) {
1350 av_log(avctx, AV_LOG_WARNING,
1351 "Frame duration too large: %"PRId64"\n", frame->duration);
1352 } else if (frame->duration)
1353 duration = frame->duration;
1354 else if (avctx->framerate.num > 0 && avctx->framerate.den > 0)
1355 duration = av_rescale_q(1, av_inv_q(avctx->framerate), avctx->time_base);
1356 else {
1357 duration = 1;
1358 }
1359
1360 switch (frame->color_range) {
1361 case AVCOL_RANGE_MPEG:
1362 rawimg->range = AOM_CR_STUDIO_RANGE;
1363 break;
1364 case AVCOL_RANGE_JPEG:
1365 rawimg->range = AOM_CR_FULL_RANGE;
1366 break;
1367 }
1368
1369 aom_img_remove_metadata(rawimg);
1371 if (ctx->dovi.cfg.dv_profile && sd) {
1372 const AVDOVIMetadata *metadata = (const AVDOVIMetadata *)sd->data;
1373 uint8_t *t35;
1374 int size;
1376 &t35, &size)) < 0)
1377 return res;
1378 res = aom_img_add_metadata(rawimg, OBU_METADATA_TYPE_ITUT_T35,
1379 t35, size, AOM_MIF_ANY_FRAME);
1380 av_free(t35);
1381 if (res != AOM_CODEC_OK)
1382 return AVERROR(ENOMEM);
1383 } else if (ctx->dovi.cfg.dv_profile) {
1384 av_log(avctx, AV_LOG_ERROR, "Dolby Vision enabled, but received frame "
1385 "without AV_FRAME_DATA_DOVI_METADATA\n");
1386 return AVERROR_INVALIDDATA;
1387 }
1388
1389 if (frame->pict_type == AV_PICTURE_TYPE_I)
1390 flags |= AOM_EFLAG_FORCE_KF;
1391
1392 res = add_hdr_plus(avctx, rawimg, frame);
1393 if (res < 0)
1394 return res;
1395
1396 res = add_hdr_smpte2094_app5(avctx, rawimg, frame);
1397 if (res < 0)
1398 return res;
1399 }
1400
1401 res = aom_codec_encode(&ctx->encoder, rawimg, timestamp, duration, flags);
1402 if (res != AOM_CODEC_OK) {
1403 log_encoder_error(avctx, "Error encoding frame");
1404 return AVERROR_INVALIDDATA;
1405 }
1406 coded_size = queue_frames(avctx, pkt);
1407 if (coded_size < 0)
1408 return coded_size;
1409
1410 if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
1411 size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
1412
1413 avctx->stats_out = av_malloc(b64_size);
1414 if (!avctx->stats_out) {
1415 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%zu bytes) failed\n",
1416 b64_size);
1417 return AVERROR(ENOMEM);
1418 }
1419 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
1420 ctx->twopass_stats.sz);
1421 }
1422
1423 *got_packet = !!coded_size;
1424
1425 if (*got_packet && avctx->flags & AV_CODEC_FLAG_RECON_FRAME) {
1426 AVCodecInternal *avci = avctx->internal;
1427 struct aom_image img;
1428
1430
1431 res = codecctl_imgp(avctx, AV1_GET_NEW_FRAME_IMAGE, &img);
1432 if (res < 0)
1433 return res;
1434
1436 if (avci->recon_frame->format == AV_PIX_FMT_NONE) {
1438 "Unhandled reconstructed frame colorspace: %d\n",
1439 img.fmt);
1440 return AVERROR(ENOSYS);
1441 }
1442
1443 avci->recon_frame->width = img.d_w;
1444 avci->recon_frame->height = img.d_h;
1445
1446 res = av_frame_get_buffer(avci->recon_frame, 0);
1447 if (res < 0)
1448 return res;
1449
1450 if ((img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) && img.bit_depth == 8)
1452 else {
1453 const uint8_t *planes[4] = { img.planes[0], img.planes[1], img.planes[2] };
1454 const int stride[4] = { img.stride[0], img.stride[1], img.stride[2] };
1455
1457 stride, avci->recon_frame->format, img.d_w, img.d_h);
1458 }
1459 }
1460
1461 return 0;
1462}
1463
1471
1480
1496
1515
1517 const AVCodec *codec,
1518 enum AVCodecConfig config,
1519 unsigned flags, const void **out,
1520 int *out_num)
1521{
1522 if (config == AV_CODEC_CONFIG_PIX_FORMAT) {
1523 int supports_monochrome = aom_codec_version() >= 20001;
1524 aom_codec_caps_t codec_caps = aom_codec_get_caps(aom_codec_av1_cx());
1525 if (codec_caps & AOM_CODEC_CAP_HIGHBITDEPTH) {
1526 if (supports_monochrome) {
1529 } else {
1531 *out_num = FF_ARRAY_ELEMS(av1_pix_fmts_highbd) - 1;
1532 }
1533 } else {
1534 if (supports_monochrome) {
1536 *out_num = FF_ARRAY_ELEMS(av1_pix_fmts_with_gray) - 1;
1537 } else {
1538 *out = av1_pix_fmts;
1539 *out_num = FF_ARRAY_ELEMS(av1_pix_fmts) - 1;
1540 }
1541 }
1542 return 0;
1543 }
1544
1545 return ff_default_get_supported_config(avctx, codec, config, flags, out, out_num);
1546}
1547
1549{
1550 return aom_init(avctx, aom_codec_av1_cx());
1551}
1552
1554{
1555 AOMContext *ctx = avctx->priv_data;
1556 int loglevel;
1557 int res;
1558
1559 res = ff_encode_reconf_parse_dict(avctx, dict);
1560 if (res < 0)
1561 return res;
1562
1563 res = aom_config(avctx, ctx->encoder.iface);
1564 if (res < 0)
1565 return res;
1566
1567 res = aom_codec_enc_config_set(&ctx->encoder, &ctx->enccfg);
1568 loglevel = res != AOM_CODEC_OK ? AV_LOG_WARNING : AV_LOG_DEBUG;
1569 av_log(avctx, loglevel, "Reconfigure options:\n");
1570 dump_enc_cfg(avctx, &ctx->enccfg, loglevel);
1571 if (res != AOM_CODEC_OK) {
1572 log_encoder_error(avctx, "Failed to reconfigure encoder");
1573 return AVERROR(EINVAL);
1574 }
1575
1576 res = aom_codecctl(avctx);
1577 if (res < 0)
1578 return res;
1579
1580 return 0;
1581}
1582
1583#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
1584#define VER VE | AV_OPT_FLAG_RUNTIME_PARAM
1585static const AVOption options[] = {
1586 { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 8, VER},
1587 { "auto-alt-ref", "Enable use of alternate reference "
1588 "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 2, VE},
1589 { "lag-in-frames", "Number of frames to look ahead at for "
1590 "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1591 { "arnr-max-frames", "altref noise reduction max frame count", OFFSET(arnr_max_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1592 { "arnr-strength", "altref noise reduction filter strength", OFFSET(arnr_strength), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1593 { "aq-mode", "adaptive quantization mode", OFFSET(aq_mode), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 4, VE, .unit = "aq_mode"},
1594 { "none", "Aq not used", 0, AV_OPT_TYPE_CONST, {.i64 = 0}, 0, 0, VE, .unit = "aq_mode"},
1595 { "variance", "Variance based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 1}, 0, 0, VE, .unit = "aq_mode"},
1596 { "complexity", "Complexity based Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 2}, 0, 0, VE, .unit = "aq_mode"},
1597 { "cyclic", "Cyclic Refresh Aq", 0, AV_OPT_TYPE_CONST, {.i64 = 3}, 0, 0, VE, .unit = "aq_mode"},
1598 { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, .unit = "er"},
1599 { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, .unit = "er"},
1600 { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, VER },
1601 { "static-thresh", "A change threshold on blocks below which they will be skipped by the encoder", OFFSET(static_thresh), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, VE },
1602 { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
1603 { "denoise-noise-level", "Amount of noise to be removed", OFFSET(denoise_noise_level), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1604 { "denoise-block-size", "Denoise block size ", OFFSET(denoise_block_size), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
1605 { "undershoot-pct", "Datarate undershoot (min) target (%)", OFFSET(rc_undershoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
1606 { "overshoot-pct", "Datarate overshoot (max) target (%)", OFFSET(rc_overshoot_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1000, VE},
1607 { "minsection-pct", "GOP min bitrate (% of target)", OFFSET(minsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 100, VE},
1608 { "maxsection-pct", "GOP max bitrate (% of target)", OFFSET(maxsection_pct), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 5000, VE},
1609 { "frame-parallel", "Enable frame parallel decodability features", OFFSET(frame_parallel), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1610 { "tiles", "Tile columns x rows", OFFSET(tile_cols), AV_OPT_TYPE_IMAGE_SIZE, { .str = NULL }, 0, 0, VE },
1611 { "tile-columns", "Log2 of number of tile columns to use", OFFSET(tile_cols_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1612 { "tile-rows", "Log2 of number of tile rows to use", OFFSET(tile_rows_log2), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 6, VE},
1613 { "row-mt", "Enable row based multi-threading", OFFSET(row_mt), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1614 { "enable-cdef", "Enable CDEF filtering", OFFSET(enable_cdef), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1615 { "enable-global-motion", "Enable global motion", OFFSET(enable_global_motion), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1616 { "enable-intrabc", "Enable intra block copy prediction mode", OFFSET(enable_intrabc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1617 { "enable-restoration", "Enable Loop Restoration filtering", OFFSET(enable_restoration), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1618 { "usage", "Quality and compression efficiency vs speed trade-off", OFFSET(usage), AV_OPT_TYPE_INT, {.i64 = 0}, 0, INT_MAX, VE, .unit = "usage"},
1619 { "good", "Good quality", 0, AV_OPT_TYPE_CONST, {.i64 = 0 /* AOM_USAGE_GOOD_QUALITY */}, 0, 0, VE, .unit = "usage"},
1620 { "realtime", "Realtime encoding", 0, AV_OPT_TYPE_CONST, {.i64 = 1 /* AOM_USAGE_REALTIME */}, 0, 0, VE, .unit = "usage"},
1621 { "allintra", "All Intra encoding", 0, AV_OPT_TYPE_CONST, {.i64 = 2 /* AOM_USAGE_ALL_INTRA */}, 0, 0, VE, .unit = "usage"},
1622 { "tune", "The metric that the encoder tunes for. Automatically chosen by the encoder by default", OFFSET(tune), AV_OPT_TYPE_INT, {.i64 = -1}, -1, AOM_TUNE_SSIM, VE, .unit = "tune"},
1623 { "psnr", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_PSNR}, 0, 0, VE, .unit = "tune"},
1624 { "ssim", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = AOM_TUNE_SSIM}, 0, 0, VE, .unit = "tune"},
1626 { "still-picture", "Encode in single frame mode (typically used for still AVIF images).", OFFSET(still_picture), AV_OPT_TYPE_BOOL, {.i64 = 0}, -1, 1, VE },
1627 { "dolbyvision", "Enable Dolby Vision RPU coding", OFFSET(dovi.enable), AV_OPT_TYPE_BOOL, {.i64 = FF_DOVI_AUTOMATIC }, -1, 1, VE, .unit = "dovi" },
1628 { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_DOVI_AUTOMATIC}, .flags = VE, .unit = "dovi" },
1629 { "enable-rect-partitions", "Enable rectangular partitions", OFFSET(enable_rect_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1630 { "enable-1to4-partitions", "Enable 1:4/4:1 partitions", OFFSET(enable_1to4_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1631 { "enable-ab-partitions", "Enable ab shape partitions", OFFSET(enable_ab_partitions), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1632 { "enable-angle-delta", "Enable angle delta intra prediction", OFFSET(enable_angle_delta), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1633 { "enable-cfl-intra", "Enable chroma predicted from luma intra prediction", OFFSET(enable_cfl_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1634 { "enable-filter-intra", "Enable filter intra predictor", OFFSET(enable_filter_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1635 { "enable-intra-edge-filter", "Enable intra edge filter", OFFSET(enable_intra_edge_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1636 { "enable-smooth-intra", "Enable smooth intra prediction mode", OFFSET(enable_smooth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1637 { "enable-paeth-intra", "Enable paeth predictor in intra prediction", OFFSET(enable_paeth_intra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1638 { "enable-palette", "Enable palette prediction mode", OFFSET(enable_palette), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1639 { "enable-flip-idtx", "Enable extended transform type", OFFSET(enable_flip_idtx), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1640 { "enable-tx64", "Enable 64-pt transform", OFFSET(enable_tx64), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1641 { "reduced-tx-type-set", "Use reduced set of transform types", OFFSET(reduced_tx_type_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1642 { "use-intra-dct-only", "Use DCT only for INTRA modes", OFFSET(use_intra_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1643 { "use-inter-dct-only", "Use DCT only for INTER modes", OFFSET(use_inter_dct_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1644 { "use-intra-default-tx-only", "Use default-transform only for INTRA modes", OFFSET(use_intra_default_tx_only), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1645 { "enable-ref-frame-mvs", "Enable temporal mv prediction", OFFSET(enable_ref_frame_mvs), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1646 { "enable-reduced-reference-set", "Use reduced set of single and compound references", OFFSET(enable_reduced_reference_set), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1647 { "enable-obmc", "Enable obmc", OFFSET(enable_obmc), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1648 { "enable-dual-filter", "Enable dual filter", OFFSET(enable_dual_filter), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1649 { "enable-diff-wtd-comp", "Enable difference-weighted compound", OFFSET(enable_diff_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1650 { "enable-dist-wtd-comp", "Enable distance-weighted compound", OFFSET(enable_dist_wtd_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1651 { "enable-onesided-comp", "Enable one sided compound", OFFSET(enable_onesided_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1652 { "enable-interinter-wedge", "Enable interinter wedge compound", OFFSET(enable_interinter_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1653 { "enable-interintra-wedge", "Enable interintra wedge compound", OFFSET(enable_interintra_wedge), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1654 { "enable-masked-comp", "Enable masked compound", OFFSET(enable_masked_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1655 { "enable-interintra-comp", "Enable interintra compound", OFFSET(enable_interintra_comp), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1656 { "enable-smooth-interintra", "Enable smooth interintra mode", OFFSET(enable_smooth_interintra), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, VE},
1657#if AOM_ENCODER_ABI_VERSION >= 23
1658 { "aom-params", "Set libaom options using a :-separated list of key=value pairs", OFFSET(aom_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
1659#endif
1660 { NULL },
1661};
1662
1663static const FFCodecDefault defaults[] = {
1664 { "b", "0", AV_OPT_FLAG_RUNTIME_PARAM },
1665 { "bufsize", "0", AV_OPT_FLAG_RUNTIME_PARAM },
1666 { "maxrate", "0", AV_OPT_FLAG_RUNTIME_PARAM },
1667 { "minrate", "0", AV_OPT_FLAG_RUNTIME_PARAM },
1668 { "qmin", "-1", AV_OPT_FLAG_RUNTIME_PARAM },
1669 { "qmax", "-1", AV_OPT_FLAG_RUNTIME_PARAM },
1670 { "sar", "0", AV_OPT_FLAG_RUNTIME_PARAM },
1671 { "g", "-1" },
1672 { "keyint_min", "-1" },
1673 { NULL },
1674};
1675
1676static const AVClass class_aom = {
1677 .class_name = "libaom-av1 encoder",
1678 .item_name = av_default_item_name,
1679 .option = options,
1680 .version = LIBAVUTIL_VERSION_INT,
1681};
1682
1684 .p.name = "libaom-av1",
1685 CODEC_LONG_NAME("libaom AV1"),
1686 .p.type = AVMEDIA_TYPE_VIDEO,
1687 .p.id = AV_CODEC_ID_AV1,
1688 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
1692 .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG,
1694 .p.priv_class = &class_aom,
1695 .p.wrapper_name = "libaom",
1696 .priv_data_size = sizeof(AOMContext),
1697 .init = av1_init,
1698 .reconf = av1_reconf,
1700 .close = aom_free,
1701 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
1704 .defaults = defaults,
1705 .get_supported_config = av1_get_supported_config,
1706};
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
static double val(void *priv, double ch)
Definition aeval.c:77
const FFCodec ff_libaom_av1_encoder
Definition libaomenc.c:1683
#define VE
Definition amfenc_av1.c:30
static const FFCodecDefault defaults[]
Definition amfenc_av1.c:723
static FILE * out
static AVFormatContext * ctx
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
int ff_default_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec, enum AVCodecConfig config, unsigned flags, const void **out_configs, int *out_num_configs)
Definition avcodec.c:765
Libavcodec external API header.
static int FUNC metadata(CodedBitstreamContext *ctx, RWContext *rw, APVRawMetadata *current)
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Definition codec_par.c:138
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
#define AV_PROFILE_AV1_HIGH
Definition defs.h:170
#define AV_PROFILE_UNKNOWN
Definition defs.h:65
#define AV_PROFILE_AV1_MAIN
Definition defs.h:169
#define AV_PROFILE_AV1_PROFESSIONAL
Definition defs.h:171
static AVPacket * pkt
static AVFrame * frame
void ff_dovi_ctx_unref(DOVIContext *s)
Completely reset a DOVIContext, preserving only logctx.
Definition dovi_rpu.c:30
#define FF_DOVI_AUTOMATIC
Enable tri-state.
Definition dovi_rpu.h:49
int ff_dovi_rpu_generate(DOVIContext *s, const AVDOVIMetadata *metadata, int flags, uint8_t **out_rpu, int *out_size)
Synthesize a Dolby Vision RPU reflecting the current state.
@ FF_DOVI_WRAP_T35
wrap inside T.35+EMDF
Definition dovi_rpu.h:160
int ff_dovi_configure(DOVIContext *s, AVCodecContext *avctx)
Variant of ff_dovi_configure_from_codedpar which infers the codec parameters from an AVCodecContext.
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
AVCPBProperties * ff_encode_add_cpb_side_data(AVCodecContext *avctx)
Add a CPB properties side data to an encoding context.
Definition encode.c:1039
int ff_encode_add_stats_side_data(AVPacket *pkt, int quality, const int64_t error[], int error_count, enum AVPictureType pict_type)
Definition encode.c:1070
av_cold int ff_encode_reconf_parse_dict(AVCodecContext *avctx, AVDictionary **dict)
Definition encode.c:599
static CheckasmStats stats
Definition checkasm.c:75
static CheckasmConfig cfg
Definition checkasm.c:74
static int64_t duration
Definition ffplay.c:330
const char * usage
#define AV_OPT_FLAG_RUNTIME_PARAM
A generic parameter which can be set by the user at runtime.
Definition opt.h:376
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition opt.h:298
@ AV_OPT_TYPE_FLAGS
Underlying C type is unsigned int.
Definition opt.h:254
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition bsf.c:47
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition bsf.c:147
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition bsf.c:99
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition bsf.c:228
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition bsf.c:200
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
#define AV_CODEC_CAP_ENCODER_RECONF
Encoder can be reconfigured by passing new initialization parameters.
Definition codec.h:54
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition avcodec.h:294
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition codec.h:112
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition avcodec.h:290
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition avcodec.h:318
#define AV_CODEC_FLAG_PSNR
error[?
Definition avcodec.h:306
#define AV_CODEC_CAP_ENCODER_RECON_FRAME
The encoder is able to output reconstructed frame data, i.e.
Definition codec.h:162
#define AV_CODEC_FLAG_RECON_FRAME
Request the encoder to output reconstructed frames, i.e. frames that would be produced by decoding th...
Definition avcodec.h:244
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
AVCodecConfig
Definition avcodec.h:2572
@ AV_CODEC_CONFIG_PIX_FORMAT
AVPixelFormat, terminated by AV_PIX_FMT_NONE.
Definition avcodec.h:2573
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition base64.c:147
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition base64.h:66
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition base64.c:81
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
Definition error.h:52
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
AVFrameSideData * av_frame_get_side_data(const AVFrame *frame, enum AVFrameSideDataType type)
Definition frame.c:659
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
Definition frame.c:206
@ AV_FRAME_DATA_DOVI_METADATA
Parsed Dolby Vision metadata, suitable for passing to a software implementation.
Definition frame.h:208
@ AV_FRAME_DATA_DYNAMIC_HDR_PLUS
HDR dynamic metadata associated with a video frame.
Definition frame.h:159
@ AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5
HDR dynamic metadata associated with a video frame.
Definition frame.h:270
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_INFO
Standard information.
Definition log.h:221
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition mathematics.c:58
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AV_ROUND_NEAR_INF
Round to nearest and halfway cases away from zero.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition mem.c:495
int av_reallocp(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory through a pointer to a pointer.
Definition mem.c:188
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_image_copy(uint8_t *const dst_data[4], const int dst_linesizes[4], const uint8_t *const src_data[4], const int src_linesizes[4], enum AVPixelFormat pix_fmt, int width, int height)
Copy image in src_data to dst_data.
Definition imgutils.c:422
AVPictureType
Definition avutil.h:276
@ AV_PICTURE_TYPE_I
Intra.
Definition avutil.h:278
@ AV_PICTURE_TYPE_P
Predicted.
Definition avutil.h:279
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
int av_dynamic_hdr_smpte2094_app5_to_t35(const AVDynamicHDRSmpte2094App5 *s, uint8_t **data, size_t *size)
Serialize dynamic SMPTE-2094-50 metadata to a ITU-T T.35 message.
int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size)
Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer, excluding the first 48...
misc image utilities
#define av_log2
Definition intmath.h:84
#define ITU_T_T35_PROVIDER_CODE_SMPTE
Definition itut35.h:48
#define ITU_T_T35_COUNTRY_CODE_US
Definition itut35.h:32
#define ITU_T_T35_PROVIDER_CODE_SAMSUNG
Definition itut35.h:47
void ff_aom_image_copy_16_to_8(AVFrame *pic, struct aom_image *img)
Definition libaom.c:27
AOM common functions.
static av_cold int av1_init(AVCodecContext *avctx)
Definition libaomdec.c:284
static av_cold int aom_free(AVCodecContext *avctx)
Definition libaomdec.c:277
static int add_hdr_smpte2094_app5(AVCodecContext *avctx, struct aom_image *img, const AVFrame *frame)
Definition libaomenc.c:378
static const char *const ctlidstr[]
Definition libaomenc.c:149
static av_cold int av1_reconf(AVCodecContext *avctx, AVDictionary **dict)
Definition libaomenc.c:1553
unsigned offset
Definition libaomenc.c:763
static enum AVPixelFormat av1_pix_fmts_highbd_with_gray[]
Definition libaomenc.c:1497
static enum AVPixelFormat aomfmt_to_pixfmt(struct aom_image *img)
Definition libaomenc.c:1296
static enum AVPixelFormat av1_pix_fmts_highbd[]
Definition libaomenc.c:1481
static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
Queue multiple output frames from the encoder, returning the front-most.
Definition libaomenc.c:1203
static void cx_pktcpy(AOMContext *ctx, struct FrameListData *dst, const struct aom_codec_cx_pkt *src)
Definition libaomenc.c:1116
static int add_hdr_plus(AVCodecContext *avctx, struct aom_image *img, const AVFrame *frame)
Definition libaomenc.c:331
static enum AVPixelFormat av1_pix_fmts_with_gray[]
Definition libaomenc.c:1472
static av_cold int codecctl_imgp(AVCodecContext *avctx, int id, struct aom_image *img)
Definition libaomenc.c:450
static void set_color_range(AVCodecContext *avctx)
Definition libaomenc.c:582
static av_cold int av1_init(AVCodecContext *avctx)
Definition libaomenc.c:1548
static int av1_get_supported_config(const AVCodecContext *avctx, const AVCodec *codec, enum AVCodecConfig config, unsigned flags, const void **out, int *out_num)
Definition libaomenc.c:1516
static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
Definition libaomenc.c:212
static int count_uniform_tiling(int dim, int sb_size, int tiles_log2)
Definition libaomenc.c:598
static av_cold void free_coded_frame(struct FrameListData *cx_frame)
Definition libaomenc.c:291
int aom_enum
Definition libaomenc.c:762
static av_cold int codecctl_int(AVCodecContext *avctx, int id, int val)
Definition libaomenc.c:308
static av_cold int aom_free(AVCodecContext *avctx)
Definition libaomenc.c:471
static int aom_encode(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition libaomenc.c:1327
static av_cold int aom_init(AVCodecContext *avctx, const struct aom_codec_iface *iface)
Definition libaomenc.c:1012
#define VER
Definition libaomenc.c:1584
static av_cold int aom_config(AVCodecContext *avctx, const struct aom_codec_iface *iface)
Definition libaomenc.c:800
static const struct @177033264217101044054267161301277114055300120044 option_map[]
static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame, AVPacket *pkt)
Store coded frame information in format suitable for return from encode2().
Definition libaomenc.c:1142
static enum AVPixelFormat av1_pix_fmts[]
Definition libaomenc.c:1464
static int set_pix_fmt(AVCodecContext *avctx, aom_codec_caps_t codec_caps, struct aom_codec_enc_cfg *enccfg, aom_codec_flags_t *flags, aom_img_fmt_t *img_fmt)
Definition libaomenc.c:517
#define OFFSET(x)
Definition libaomenc.c:147
static void coded_frame_add(void *list, struct FrameListData *cx_frame)
Definition libaomenc.c:281
static av_cold int aom_codecctl(AVCodecContext *avctx)
Definition libaomenc.c:932
static av_cold void free_frame_list(struct FrameListData *list)
Definition libaomenc.c:297
static const AVClass class_aom
Definition libaomenc.c:1676
static av_cold void dump_enc_cfg(AVCodecContext *avctx, const struct aom_codec_enc_cfg *cfg, int level)
Definition libaomenc.c:223
static int choose_tiling(AVCodecContext *avctx, struct aom_codec_enc_cfg *enccfg)
Definition libaomenc.c:606
AV1 common definitions.
@ AV1_MAX_TILE_WIDTH
Definition av1.h:79
@ AV1_MAX_TILE_COLS
Definition av1.h:82
@ AV1_MAX_TILE_ROWS
Definition av1.h:81
@ AV1_MAX_TILE_AREA
Definition av1.h:80
common internal api header.
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
int av_cpu_count(void)
Definition cpu.c:228
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
static av_always_inline av_const double round(double x)
Definition libm.h:446
const char * desc
Definition libsvtav1.c:83
static const struct @257111027162314367033347246032313251342043035002 planes[]
#define FFMIN(a, b)
Definition macros.h:49
#define FFALIGN(x, a)
Definition macros.h:78
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition pixdesc.c:3460
#define AV_PIX_FMT_FLAG_RGB
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
Definition pixdesc.h:136
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_UNSPECIFIED
Definition pixfmt.h:749
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_GBRP10
Definition pixfmt.h:564
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
#define AV_PIX_FMT_GRAY12
Definition pixfmt.h:526
#define AV_PIX_FMT_GBRP12
Definition pixfmt.h:565
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_NONE
Definition pixfmt.h:72
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_GRAY8
Y , 8bpp.
Definition pixfmt.h:81
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
@ AV_PIX_FMT_GBRP
planar GBR 4:4:4 24bpp
Definition pixfmt.h:165
#define AV_PIX_FMT_GRAY10
Definition pixfmt.h:525
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition pixfmt.h:644
@ AVCOL_TRC_IEC61966_2_1
IEC 61966-2-1 (sRGB or sYCC)
Definition pixfmt.h:686
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
@ AVCOL_SPC_RGB
order of coefficients is actually GBR, also IEC 61966-2-1 (sRGB), YZX and ST 428-1
Definition pixfmt.h:707
const AVProfile ff_av1_profiles[]
Definition profiles.c:163
#define FF_AV1_PROFILE_OPTS
Definition profiles.h:56
#define FF_ARRAY_ELEMS(a)
#define snprintf
Definition snprintf.h:34
aom_img_fmt_t img_fmt
Definition libaomenc.c:80
struct aom_image rawimg
Definition libaomenc.c:78
int use_intra_default_tx_only
Definition libaomenc.c:131
int auto_alt_ref
Definition libaomenc.c:85
AVDictionary * aom_params
Definition libaomenc.c:144
int error_resilient
Definition libaomenc.c:90
int tile_rows
Definition libaomenc.c:104
int enable_onesided_comp
Definition libaomenc.c:138
int reduced_tx_type_set
Definition libaomenc.c:128
aom_codec_flags_t flags
Definition libaomenc.c:79
int enable_interintra_wedge
Definition libaomenc.c:134
int enable_global_motion
Definition libaomenc.c:110
int use_intra_dct_only
Definition libaomenc.c:129
int enable_restoration
Definition libaomenc.c:112
int enable_masked_comp
Definition libaomenc.c:136
int enable_smooth_interintra
Definition libaomenc.c:140
int minsection_pct
Definition libaomenc.c:101
int use_inter_dct_only
Definition libaomenc.c:130
int tile_cols
Definition libaomenc.c:104
int enable_diff_wtd_comp
Definition libaomenc.c:141
struct aom_fixed_buf twopass_stats
Definition libaomenc.c:81
int uniform_tiles
Definition libaomenc.c:107
int rc_overshoot_pct
Definition libaomenc.c:100
int enable_reduced_reference_set
Definition libaomenc.c:139
uint64_t frame_number
Definition libaomenc.c:98
int enable_1to4_partitions
Definition libaomenc.c:117
int enable_paeth_intra
Definition libaomenc.c:121
int enable_rect_partitions
Definition libaomenc.c:116
aom_superblock_size_t superblock_size
Definition libaomenc.c:106
int tile_cols_log2
Definition libaomenc.c:105
int enable_tx64
Definition libaomenc.c:127
int have_sse
true if we have pending sse[]
Definition libaomenc.c:97
int arnr_strength
Definition libaomenc.c:87
int still_picture
Definition libaomenc.c:115
int enable_dual_filter
Definition libaomenc.c:143
struct FrameListData * coded_frame_list
Definition libaomenc.c:83
int lag_in_frames
Definition libaomenc.c:89
int static_thresh
Definition libaomenc.c:92
int enable_obmc
Definition libaomenc.c:137
int enable_interintra_comp
Definition libaomenc.c:135
int enable_flip_idtx
Definition libaomenc.c:126
int enable_palette
Definition libaomenc.c:124
int enable_intrabc
Definition libaomenc.c:111
int maxsection_pct
Definition libaomenc.c:102
int denoise_noise_level
Definition libaomenc.c:94
int tile_rows_log2
Definition libaomenc.c:105
struct aom_codec_enc_cfg enccfg
Definition libaomenc.c:77
int denoise_block_size
Definition libaomenc.c:95
int enable_filter_intra
Definition libaomenc.c:125
int rc_undershoot_pct
Definition libaomenc.c:99
unsigned twopass_stats_size
Definition libaomenc.c:82
int enable_interinter_wedge
Definition libaomenc.c:133
int enable_ab_partitions
Definition libaomenc.c:118
int enable_dist_wtd_comp
Definition libaomenc.c:142
int frame_parallel
Definition libaomenc.c:103
int enable_smooth_intra
Definition libaomenc.c:122
int enable_intra_edge_filter
Definition libaomenc.c:123
int enable_cdef
Definition libaomenc.c:109
int arnr_max_frames
Definition libaomenc.c:86
int aq_mode
Definition libaomenc.c:88
int enable_cfl_intra
Definition libaomenc.c:120
int enable_angle_delta
Definition libaomenc.c:119
struct aom_codec_ctx encoder
Definition libaomenc.c:76
AVBSFContext * bsf
Definition libaomenc.c:74
int drop_threshold
Definition libaomenc.c:93
int enable_ref_frame_mvs
Definition libaomenc.c:132
DOVIContext dovi
Definition libaomenc.c:75
int cpu_used
Definition libaomenc.c:84
uint64_t sse[4]
Definition libaomenc.c:96
The bitstream filter state.
Definition bsf.h:68
uint8_t * data
The data buffer.
Definition buffer.h:90
This structure describes the bitrate properties of an encoded bitstream.
Definition defs.h:282
int64_t avg_bitrate
Average bitrate of the stream, in bits per second.
Definition defs.h:297
int64_t max_bitrate
Maximum bitrate of the stream, in bits per second.
Definition defs.h:287
int64_t buffer_size
The size of the buffer to which the ratecontrol is applied, in bits.
Definition defs.h:303
int64_t min_bitrate
Minimum bitrate of the stream, in bits per second.
Definition defs.h:292
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
char * stats_out
pass1 encoding statistics output buffer
Definition avcodec.h:1330
int rc_buffer_size
decoder bitstream buffer size
Definition avcodec.h:1273
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition avcodec.h:681
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition avcodec.h:657
int qmin
minimum quantizer
Definition avcodec.h:1252
int keyint_min
minimum GOP size
Definition avcodec.h:1014
AVRational framerate
Definition avcodec.h:563
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition avcodec.h:1338
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
Definition avcodec.h:1316
int profile
profile
Definition avcodec.h:1636
enum AVColorSpace colorspace
YUV colorspace type.
Definition avcodec.h:671
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition avcodec.h:1021
int refs
number of reference frames
Definition avcodec.h:701
int64_t rc_max_rate
maximum bitrate
Definition avcodec.h:1288
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1579
int qmax
maximum quantizer
Definition avcodec.h:1259
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition avcodec.h:664
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
int64_t rc_min_rate
minimum bitrate
Definition avcodec.h:1295
uint64_t error[AV_NUM_DATA_POINTERS]
error
Definition avcodec.h:1523
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
Definition avcodec.h:1244
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
AVFrame * recon_frame
When the AV_CODEC_FLAG_RECON_FRAME flag is used.
Definition internal.h:114
AVCodec.
Definition codec.h:175
Combined struct representing a combination of header, mapping and color metadata, for attaching to fr...
Definition dovi_meta.h:345
char * key
Definition dict.h:91
char * value
Definition dict.h:92
This struct represents dynamic metadata for color volume transform - application 4 of SMPTE 2094-40:2...
This struct represents dynamic metadata for color volume transform as specified in the SMPTE 2094-50 ...
Structure to hold side data for an AVFrame.
Definition frame.h:327
uint8_t * data
Definition frame.h:329
AVBufferRef * buf
Definition frame.h:332
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
int width
Definition frame.h:544
int height
Definition frame.h:544
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition frame.h:559
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition pixdesc.h:69
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
Portion of struct vpx_codec_cx_pkt from vpx_encoder.h.
Definition libaomenc.c:58
int64_t pts
time stamp to show frame (in timebase units)
Definition libaomenc.c:61
uint64_t sse[4]
Definition libaomenc.c:66
struct FrameListData * next
Definition libaomenc.c:69
size_t sz
length of compressed data
Definition libaomenc.c:60
void * buf
compressed data buffer
Definition libaomenc.c:59
unsigned long duration
duration to show frame (in timebase units)
Definition libaomenc.c:63
int have_sse
true if we have pending sse[]
Definition libaomenc.c:67
uint64_t frame_number
Definition libaomenc.c:68
uint32_t flags
flags for this frame
Definition libaomenc.c:65
uint8_t level
Definition svq3.c:208
#define stride
#define av_free(p)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
int tile_cols
Definition av1_levels.c:73
void(* filter)(uint8_t *src, ptrdiff_t stride, int qscale)
Definition h263dsp.c:29
#define src
Definition vp8dsp.c:248
#define width
Definition dsp.h:89
int size
#define img
int dim