avcodec/libaomenc: export the Sequence Header OBU as extradata
[libav.git] / libavcodec / libaomenc.c
CommitLineData
43778a50
LB
1/*
2 * Copyright (c) 2010, Google, Inc.
3 *
4 * This file is part of Libav.
5 *
6 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#define AOM_DISABLE_CTRL_TYPECHECKS 1
22#include <aom/aom_encoder.h>
23#include <aom/aomcx.h>
24
25#include "libavutil/base64.h"
26#include "libavutil/common.h"
27#include "libavutil/mathematics.h"
28#include "libavutil/opt.h"
29#include "libavutil/pixdesc.h"
30
31#include "avcodec.h"
32#include "internal.h"
33#include "libaom.h"
34
35/*
36 * Portion of struct aom_codec_cx_pkt from aom_encoder.h.
37 * One encoded frame returned from the library.
38 */
39struct FrameListData {
40 void *buf; /* compressed data buffer */
41 size_t sz; /* length of compressed data */
42 int64_t pts; /* time stamp to show frame
43 * (in timebase units) */
44 unsigned long duration; /* duration to show frame
45 * (in timebase units) */
46 uint32_t flags; /* flags for this frame */
47 struct FrameListData *next;
48};
49
50typedef struct AOMEncoderContext {
51 AVClass *class;
52 struct aom_codec_ctx encoder;
53 struct aom_image rawimg;
54 struct aom_fixed_buf twopass_stats;
55 struct FrameListData *coded_frame_list;
56 int cpu_used;
57 int auto_alt_ref;
58 int lag_in_frames;
59 int error_resilient;
60 int crf;
61 int static_thresh;
62 int drop_threshold;
63 int noise_sensitivity;
64} AOMContext;
65
66static const char *const ctlidstr[] = {
67 [AOME_SET_CPUUSED] = "AOME_SET_CPUUSED",
68 [AOME_SET_CQ_LEVEL] = "AOME_SET_CQ_LEVEL",
69 [AOME_SET_ENABLEAUTOALTREF] = "AOME_SET_ENABLEAUTOALTREF",
70 [AOME_SET_STATIC_THRESHOLD] = "AOME_SET_STATIC_THRESHOLD",
04e8b8b0
JA
71 [AV1E_SET_CHROMA_SUBSAMPLING_X] = "AV1E_SET_CHROMA_SUBSAMPLING_X",
72 [AV1E_SET_CHROMA_SUBSAMPLING_Y] = "AV1E_SET_CHROMA_SUBSAMPLING_Y",
43778a50
LB
73};
74
75static av_cold void log_encoder_error(AVCodecContext *avctx, const char *desc)
76{
77 AOMContext *ctx = avctx->priv_data;
78 const char *error = aom_codec_error(&ctx->encoder);
79 const char *detail = aom_codec_error_detail(&ctx->encoder);
80
81 av_log(avctx, AV_LOG_ERROR, "%s: %s\n", desc, error);
82 if (detail)
83 av_log(avctx, AV_LOG_ERROR, " Additional information: %s\n", detail);
84}
85
86static av_cold void dump_enc_cfg(AVCodecContext *avctx,
87 const struct aom_codec_enc_cfg *cfg)
88{
89 int width = -30;
90 int level = AV_LOG_DEBUG;
91
92 av_log(avctx, level, "aom_codec_enc_cfg\n");
93 av_log(avctx, level, "generic settings\n"
94 " %*s%u\n %*s%u\n %*s%u\n %*s%u\n %*s%u\n"
95 " %*s{%u/%u}\n %*s%u\n %*s%d\n %*s%u\n",
96 width, "g_usage:", cfg->g_usage,
97 width, "g_threads:", cfg->g_threads,
98 width, "g_profile:", cfg->g_profile,
99 width, "g_w:", cfg->g_w,
100 width, "g_h:", cfg->g_h,
101 width, "g_timebase:", cfg->g_timebase.num, cfg->g_timebase.den,
102 width, "g_error_resilient:", cfg->g_error_resilient,
103 width, "g_pass:", cfg->g_pass,
104 width, "g_lag_in_frames:", cfg->g_lag_in_frames);
105 av_log(avctx, level, "rate control settings\n"
106 " %*s%u\n %*s%d\n %*s%p(%zu)\n %*s%u\n",
107 width, "rc_dropframe_thresh:", cfg->rc_dropframe_thresh,
108 width, "rc_end_usage:", cfg->rc_end_usage,
109 width, "rc_twopass_stats_in:", cfg->rc_twopass_stats_in.buf, cfg->rc_twopass_stats_in.sz,
110 width, "rc_target_bitrate:", cfg->rc_target_bitrate);
111 av_log(avctx, level, "quantizer settings\n"
112 " %*s%u\n %*s%u\n",
113 width, "rc_min_quantizer:", cfg->rc_min_quantizer,
114 width, "rc_max_quantizer:", cfg->rc_max_quantizer);
115 av_log(avctx, level, "bitrate tolerance\n"
116 " %*s%u\n %*s%u\n",
117 width, "rc_undershoot_pct:", cfg->rc_undershoot_pct,
118 width, "rc_overshoot_pct:", cfg->rc_overshoot_pct);
119 av_log(avctx, level, "decoder buffer model\n"
120 " %*s%u\n %*s%u\n %*s%u\n",
121 width, "rc_buf_sz:", cfg->rc_buf_sz,
122 width, "rc_buf_initial_sz:", cfg->rc_buf_initial_sz,
123 width, "rc_buf_optimal_sz:", cfg->rc_buf_optimal_sz);
124 av_log(avctx, level, "2 pass rate control settings\n"
125 " %*s%u\n %*s%u\n %*s%u\n",
126 width, "rc_2pass_vbr_bias_pct:", cfg->rc_2pass_vbr_bias_pct,
127 width, "rc_2pass_vbr_minsection_pct:", cfg->rc_2pass_vbr_minsection_pct,
128 width, "rc_2pass_vbr_maxsection_pct:", cfg->rc_2pass_vbr_maxsection_pct);
129 av_log(avctx, level, "keyframing settings\n"
130 " %*s%d\n %*s%u\n %*s%u\n",
131 width, "kf_mode:", cfg->kf_mode,
132 width, "kf_min_dist:", cfg->kf_min_dist,
133 width, "kf_max_dist:", cfg->kf_max_dist);
134 av_log(avctx, level, "\n");
135}
136
137static void coded_frame_add(void *list, struct FrameListData *cx_frame)
138{
139 struct FrameListData **p = list;
140
141 while (*p)
142 p = &(*p)->next;
143 *p = cx_frame;
144 cx_frame->next = NULL;
145}
146
147static av_cold void free_coded_frame(struct FrameListData *cx_frame)
148{
149 av_freep(&cx_frame->buf);
150 av_freep(&cx_frame);
151}
152
153static av_cold void free_frame_list(struct FrameListData *list)
154{
155 struct FrameListData *p = list;
156
157 while (p) {
158 list = list->next;
159 free_coded_frame(p);
160 p = list;
161 }
162}
163
164static av_cold int codecctl_int(AVCodecContext *avctx,
165 enum aome_enc_control_id id, int val)
166{
167 AOMContext *ctx = avctx->priv_data;
168 char buf[80];
169 int width = -30;
170 int res;
171
172 snprintf(buf, sizeof(buf), "%s:", ctlidstr[id]);
173 av_log(avctx, AV_LOG_DEBUG, " %*s%d\n", width, buf, val);
174
175 res = aom_codec_control(&ctx->encoder, id, val);
176 if (res != AOM_CODEC_OK) {
177 snprintf(buf, sizeof(buf), "Failed to set %s codec control",
178 ctlidstr[id]);
179 log_encoder_error(avctx, buf);
180 return AVERROR(EINVAL);
181 }
182
183 return 0;
184}
185
186static av_cold int aom_free(AVCodecContext *avctx)
187{
188 AOMContext *ctx = avctx->priv_data;
189
190 aom_codec_destroy(&ctx->encoder);
191 av_freep(&ctx->twopass_stats.buf);
192 av_freep(&avctx->stats_out);
193 free_frame_list(ctx->coded_frame_list);
194 return 0;
195}
196
197static av_cold int aom_init(AVCodecContext *avctx)
198{
199 AOMContext *ctx = avctx->priv_data;
200 struct aom_codec_enc_cfg enccfg = { 0 };
201 AVCPBProperties *cpb_props;
04e8b8b0 202 int res, h_shift, v_shift;
43778a50
LB
203 const struct aom_codec_iface *iface = &aom_codec_av1_cx_algo;
204
205 av_log(avctx, AV_LOG_INFO, "%s\n", aom_codec_version_str());
206 av_log(avctx, AV_LOG_VERBOSE, "%s\n", aom_codec_build_config());
207
208 if ((res = aom_codec_enc_config_default(iface, &enccfg, 0)) != AOM_CODEC_OK) {
209 av_log(avctx, AV_LOG_ERROR, "Failed to get config: %s\n",
210 aom_codec_err_to_string(res));
211 return AVERROR(EINVAL);
212 }
213 dump_enc_cfg(avctx, &enccfg);
214
215 enccfg.g_w = avctx->width;
216 enccfg.g_h = avctx->height;
217 enccfg.g_timebase.num = avctx->time_base.num;
218 enccfg.g_timebase.den = avctx->time_base.den;
219 enccfg.g_threads = avctx->thread_count;
220
221 if (ctx->lag_in_frames >= 0)
222 enccfg.g_lag_in_frames = ctx->lag_in_frames;
223
224 if (avctx->flags & AV_CODEC_FLAG_PASS1)
225 enccfg.g_pass = AOM_RC_FIRST_PASS;
226 else if (avctx->flags & AV_CODEC_FLAG_PASS2)
227 enccfg.g_pass = AOM_RC_LAST_PASS;
228 else
229 enccfg.g_pass = AOM_RC_ONE_PASS;
230
231 if (!avctx->bit_rate)
232 avctx->bit_rate = enccfg.rc_target_bitrate * 1000;
233 else
234 enccfg.rc_target_bitrate = av_rescale_rnd(avctx->bit_rate, 1, 1000,
235 AV_ROUND_NEAR_INF);
236
237 if (ctx->crf)
238 enccfg.rc_end_usage = AOM_CQ;
239 else if (avctx->rc_min_rate == avctx->rc_max_rate &&
240 avctx->rc_min_rate == avctx->bit_rate)
241 enccfg.rc_end_usage = AOM_CBR;
242
243 if (avctx->qmin > 0)
244 enccfg.rc_min_quantizer = avctx->qmin;
245 if (avctx->qmax > 0)
246 enccfg.rc_max_quantizer = avctx->qmax;
247
248 enccfg.rc_dropframe_thresh = ctx->drop_threshold;
249
250 // 0-100 (0 => CBR, 100 => VBR)
251 enccfg.rc_2pass_vbr_bias_pct = round(avctx->qcompress * 100);
252 enccfg.rc_2pass_vbr_minsection_pct =
253 avctx->rc_min_rate * 100LL / avctx->bit_rate;
254 if (avctx->rc_max_rate)
255 enccfg.rc_2pass_vbr_maxsection_pct =
256 avctx->rc_max_rate * 100LL / avctx->bit_rate;
257
258 if (avctx->rc_buffer_size)
259 enccfg.rc_buf_sz =
260 avctx->rc_buffer_size * 1000LL / avctx->bit_rate;
261 if (avctx->rc_initial_buffer_occupancy)
262 enccfg.rc_buf_initial_sz =
263 avctx->rc_initial_buffer_occupancy * 1000LL / avctx->bit_rate;
264 enccfg.rc_buf_optimal_sz = enccfg.rc_buf_sz * 5 / 6;
265
266 // _enc_init() will balk if kf_min_dist differs from max w/AOM_KF_AUTO
267 if (avctx->keyint_min >= 0 && avctx->keyint_min == avctx->gop_size)
268 enccfg.kf_min_dist = avctx->keyint_min;
269 if (avctx->gop_size >= 0)
270 enccfg.kf_max_dist = avctx->gop_size;
271
272 if (enccfg.g_pass == AOM_RC_FIRST_PASS)
273 enccfg.g_lag_in_frames = 0;
274 else if (enccfg.g_pass == AOM_RC_LAST_PASS) {
275 int decode_size, ret;
276
277 if (!avctx->stats_in) {
278 av_log(avctx, AV_LOG_ERROR, "No stats file for second pass\n");
279 return AVERROR_INVALIDDATA;
280 }
281
282 ctx->twopass_stats.sz = strlen(avctx->stats_in) * 3 / 4;
283 ret = av_reallocp(&ctx->twopass_stats.buf, ctx->twopass_stats.sz);
284 if (ret < 0) {
285 av_log(avctx, AV_LOG_ERROR,
286 "Stat buffer alloc (%zu bytes) failed\n",
287 ctx->twopass_stats.sz);
288 return ret;
289 }
290 decode_size = av_base64_decode(ctx->twopass_stats.buf, avctx->stats_in,
291 ctx->twopass_stats.sz);
292 if (decode_size < 0) {
293 av_log(avctx, AV_LOG_ERROR, "Stat buffer decode failed\n");
294 return AVERROR_INVALIDDATA;
295 }
296
297 ctx->twopass_stats.sz = decode_size;
298 enccfg.rc_twopass_stats_in = ctx->twopass_stats;
299 }
300
301 /* 0-3: For non-zero values the encoder increasingly optimizes for reduced
302 * complexity playback on low powered devices at the expense of encode
303 * quality. */
304 if (avctx->profile != FF_PROFILE_UNKNOWN)
305 enccfg.g_profile = avctx->profile;
ea3320bb
JA
306 else if (avctx->pix_fmt == AV_PIX_FMT_YUV420P ||
307 avctx->pix_fmt == AV_PIX_FMT_YUV420P10)
f6171471 308 avctx->profile = enccfg.g_profile = FF_PROFILE_AV1_MAIN;
ea3320bb
JA
309 else if (avctx->pix_fmt == AV_PIX_FMT_YUV444P ||
310 avctx->pix_fmt == AV_PIX_FMT_YUV444P10)
311 avctx->profile = enccfg.g_profile = FF_PROFILE_AV1_HIGH;
43778a50 312 else {
ea3320bb 313 avctx->profile = enccfg.g_profile = FF_PROFILE_AV1_PROFESSIONAL;
43778a50
LB
314 }
315
316
317 enccfg.g_error_resilient = ctx->error_resilient;
318
319 dump_enc_cfg(avctx, &enccfg);
320 /* Construct Encoder Context */
321 res = aom_codec_enc_init(&ctx->encoder, iface, &enccfg, 0);
322 if (res != AOM_CODEC_OK) {
323 log_encoder_error(avctx, "Failed to initialize encoder");
324 return AVERROR(EINVAL);
325 }
326
327 // codec control failures are currently treated only as warnings
328 av_log(avctx, AV_LOG_DEBUG, "aom_codec_control\n");
329 if (ctx->cpu_used != INT_MIN)
330 codecctl_int(avctx, AOME_SET_CPUUSED, ctx->cpu_used);
331 if (ctx->auto_alt_ref >= 0)
332 codecctl_int(avctx, AOME_SET_ENABLEAUTOALTREF, ctx->auto_alt_ref);
333
334 codecctl_int(avctx, AOME_SET_STATIC_THRESHOLD, ctx->static_thresh);
335 codecctl_int(avctx, AOME_SET_CQ_LEVEL, ctx->crf);
336
04e8b8b0
JA
337 res = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &h_shift, &v_shift);
338 if (res < 0)
339 return res;
340
341 codecctl_int(avctx, AV1E_SET_CHROMA_SUBSAMPLING_X, h_shift);
342 codecctl_int(avctx, AV1E_SET_CHROMA_SUBSAMPLING_Y, v_shift);
343
43778a50
LB
344 // provide dummy value to initialize wrapper, values will be updated each _encode()
345 aom_img_wrap(&ctx->rawimg, ff_aom_pixfmt_to_imgfmt(avctx->pix_fmt),
346 avctx->width, avctx->height, 1, (unsigned char *)1);
347
348 cpb_props = ff_add_cpb_side_data(avctx);
349 if (!cpb_props)
350 return AVERROR(ENOMEM);
351
04e8b8b0
JA
352 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
353 aom_fixed_buf_t *seq = aom_codec_get_global_headers(&ctx->encoder);
354 if (!seq)
355 return AVERROR_UNKNOWN;
356
357 avctx->extradata = av_malloc(seq->sz + AV_INPUT_BUFFER_PADDING_SIZE);
358 if (!avctx->extradata) {
359 free(seq->buf);
360 free(seq);
361 return AVERROR(ENOMEM);
362 }
363 avctx->extradata_size = seq->sz;
364 memcpy(avctx->extradata, seq->buf, seq->sz);
365 memset(avctx->extradata + seq->sz, 0, AV_INPUT_BUFFER_PADDING_SIZE);
366
367 /* Doxy says: "The caller owns the memory associated with this buffer.
368 * Memory is allocated using malloc(), and should be freed
369 * via call to free()"
370 */
371 free(seq->buf);
372 free(seq);
373 }
374
43778a50
LB
375 if (enccfg.rc_end_usage == AOM_CBR ||
376 enccfg.g_pass != AOM_RC_ONE_PASS) {
377 cpb_props->max_bitrate = avctx->rc_max_rate;
378 cpb_props->min_bitrate = avctx->rc_min_rate;
379 cpb_props->avg_bitrate = avctx->bit_rate;
380 }
381 cpb_props->buffer_size = avctx->rc_buffer_size;
382
383 return 0;
384}
385
386static inline void cx_pktcpy(struct FrameListData *dst,
387 const struct aom_codec_cx_pkt *src)
388{
389 dst->pts = src->data.frame.pts;
390 dst->duration = src->data.frame.duration;
391 dst->flags = src->data.frame.flags;
392 dst->sz = src->data.frame.sz;
393 dst->buf = src->data.frame.buf;
394}
395
396/**
397 * Store coded frame information in format suitable for return from encode2().
398 *
399 * Write information from @a cx_frame to @a pkt
400 * @return packet data size on success
401 * @return a negative AVERROR on error
402 */
403static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
404 AVPacket *pkt)
405{
406 int ret = ff_alloc_packet(pkt, cx_frame->sz);
407 if (ret < 0) {
408 av_log(avctx, AV_LOG_ERROR,
409 "Error getting output packet of size %zu.\n", cx_frame->sz);
410 return ret;
411 }
412 memcpy(pkt->data, cx_frame->buf, pkt->size);
413 pkt->pts = pkt->dts = cx_frame->pts;
414
415 if (!!(cx_frame->flags & AOM_FRAME_IS_KEY))
416 pkt->flags |= AV_PKT_FLAG_KEY;
417 return pkt->size;
418}
419
420/**
421 * Queue multiple output frames from the encoder, returning the front-most.
422 * In cases where aom_codec_get_cx_data() returns more than 1 frame append
423 * the frame queue. Return the head frame if available.
424 * @return Stored frame size
425 * @return AVERROR(EINVAL) on output size error
426 * @return AVERROR(ENOMEM) on coded frame queue data allocation error
427 */
428static int queue_frames(AVCodecContext *avctx, AVPacket *pkt_out)
429{
430 AOMContext *ctx = avctx->priv_data;
431 const struct aom_codec_cx_pkt *pkt;
432 const void *iter = NULL;
433 int size = 0;
434
435 if (ctx->coded_frame_list) {
436 struct FrameListData *cx_frame = ctx->coded_frame_list;
437 /* return the leading frame if we've already begun queueing */
438 size = storeframe(avctx, cx_frame, pkt_out);
439 if (size < 0)
440 return size;
441 ctx->coded_frame_list = cx_frame->next;
442 free_coded_frame(cx_frame);
443 }
444
445 /* consume all available output from the encoder before returning. buffers
446 * are only good through the next aom_codec call */
447 while ((pkt = aom_codec_get_cx_data(&ctx->encoder, &iter))) {
448 switch (pkt->kind) {
449 case AOM_CODEC_CX_FRAME_PKT:
450 if (!size) {
451 struct FrameListData cx_frame;
452
453 /* avoid storing the frame when the list is empty and we haven't yet
454 * provided a frame for output */
455 assert(!ctx->coded_frame_list);
456 cx_pktcpy(&cx_frame, pkt);
457 size = storeframe(avctx, &cx_frame, pkt_out);
458 if (size < 0)
459 return size;
460 } else {
461 struct FrameListData *cx_frame =
462 av_malloc(sizeof(struct FrameListData));
463
464 if (!cx_frame) {
465 av_log(avctx, AV_LOG_ERROR,
466 "Frame queue element alloc failed\n");
467 return AVERROR(ENOMEM);
468 }
469 cx_pktcpy(cx_frame, pkt);
470 cx_frame->buf = av_malloc(cx_frame->sz);
471
472 if (!cx_frame->buf) {
473 av_log(avctx, AV_LOG_ERROR,
474 "Data buffer alloc (%zu bytes) failed\n",
475 cx_frame->sz);
476 av_freep(&cx_frame);
477 return AVERROR(ENOMEM);
478 }
479 memcpy(cx_frame->buf, pkt->data.frame.buf, pkt->data.frame.sz);
480 coded_frame_add(&ctx->coded_frame_list, cx_frame);
481 }
482 break;
483 case AOM_CODEC_STATS_PKT:
484 {
485 struct aom_fixed_buf *stats = &ctx->twopass_stats;
486 int err;
487 if ((err = av_reallocp(&stats->buf,
488 stats->sz +
489 pkt->data.twopass_stats.sz)) < 0) {
490 stats->sz = 0;
491 av_log(avctx, AV_LOG_ERROR, "Stat buffer realloc failed\n");
492 return err;
493 }
494 memcpy((uint8_t *)stats->buf + stats->sz,
495 pkt->data.twopass_stats.buf, pkt->data.twopass_stats.sz);
496 stats->sz += pkt->data.twopass_stats.sz;
497 break;
498 }
499 case AOM_CODEC_PSNR_PKT: // FIXME add support for AV_CODEC_FLAG_PSNR
500 case AOM_CODEC_CUSTOM_PKT:
501 // ignore unsupported/unrecognized packet types
502 break;
503 }
504 }
505
506 return size;
507}
508
509static int aom_encode(AVCodecContext *avctx, AVPacket *pkt,
510 const AVFrame *frame, int *got_packet)
511{
512 AOMContext *ctx = avctx->priv_data;
513 struct aom_image *rawimg = NULL;
514 int64_t timestamp = 0;
515 int res, coded_size;
516 aom_enc_frame_flags_t flags = 0;
517
518 if (frame) {
519 rawimg = &ctx->rawimg;
520 rawimg->planes[AOM_PLANE_Y] = frame->data[0];
521 rawimg->planes[AOM_PLANE_U] = frame->data[1];
522 rawimg->planes[AOM_PLANE_V] = frame->data[2];
523 rawimg->stride[AOM_PLANE_Y] = frame->linesize[0];
524 rawimg->stride[AOM_PLANE_U] = frame->linesize[1];
525 rawimg->stride[AOM_PLANE_V] = frame->linesize[2];
526 timestamp = frame->pts;
527 switch (frame->color_range) {
528 case AVCOL_RANGE_MPEG:
529 rawimg->range = AOM_CR_STUDIO_RANGE;
530 break;
531 case AVCOL_RANGE_JPEG:
532 rawimg->range = AOM_CR_FULL_RANGE;
533 break;
534 }
535
536 if (frame->pict_type == AV_PICTURE_TYPE_I)
537 flags |= AOM_EFLAG_FORCE_KF;
538 }
539
540 res = aom_codec_encode(&ctx->encoder, rawimg, timestamp,
541 avctx->ticks_per_frame, flags);
542 if (res != AOM_CODEC_OK) {
543 log_encoder_error(avctx, "Error encoding frame");
544 return AVERROR_INVALIDDATA;
545 }
546 coded_size = queue_frames(avctx, pkt);
547
548 if (!frame && avctx->flags & AV_CODEC_FLAG_PASS1) {
549 size_t b64_size = AV_BASE64_SIZE(ctx->twopass_stats.sz);
550
551 avctx->stats_out = av_malloc(b64_size);
552 if (!avctx->stats_out) {
553 av_log(avctx, AV_LOG_ERROR, "Stat buffer alloc (%zu bytes) failed\n",
554 b64_size);
555 return AVERROR(ENOMEM);
556 }
557 av_base64_encode(avctx->stats_out, b64_size, ctx->twopass_stats.buf,
558 ctx->twopass_stats.sz);
559 }
560
561 *got_packet = !!coded_size;
562 return 0;
563}
564
565#define OFFSET(x) offsetof(AOMContext, x)
566#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
567static const AVOption options[] = {
568 { "cpu-used", "Quality/Speed ratio modifier", OFFSET(cpu_used), AV_OPT_TYPE_INT, {.i64 = 1}, INT_MIN, INT_MAX, VE},
569 { "auto-alt-ref", "Enable use of alternate reference "
570 "frames (2-pass only)", OFFSET(auto_alt_ref), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1, VE},
571 { "lag-in-frames", "Number of frames to look ahead at for "
572 "alternate reference frame selection", OFFSET(lag_in_frames), AV_OPT_TYPE_INT, {.i64 = -1}, -1, INT_MAX, VE},
573 { "error-resilience", "Error resilience configuration", OFFSET(error_resilient), AV_OPT_TYPE_FLAGS, {.i64 = 0}, INT_MIN, INT_MAX, VE, "er"},
574 { "default", "Improve resiliency against losses of whole frames", 0, AV_OPT_TYPE_CONST, {.i64 = AOM_ERROR_RESILIENT_DEFAULT}, 0, 0, VE, "er"},
43778a50
LB
575 { "crf", "Select the quality for constant quality mode", offsetof(AOMContext, crf), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 63, VE },
576 { "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 },
577 { "drop-threshold", "Frame drop threshold", offsetof(AOMContext, drop_threshold), AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX, VE },
578 { "noise-sensitivity", "Noise sensitivity", OFFSET(noise_sensitivity), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 4, VE},
579 { NULL }
580};
581
582static const AVCodecDefault defaults[] = {
583 { "qmin", "-1" },
584 { "qmax", "-1" },
585 { "g", "-1" },
586 { "keyint_min", "-1" },
587 { NULL },
588};
589
590static const AVClass class_aom = {
591 .class_name = "libaom encoder",
592 .item_name = av_default_item_name,
593 .option = options,
594 .version = LIBAVUTIL_VERSION_INT,
595};
596
597AVCodec ff_libaom_av1_encoder = {
598 .name = "libaom-av1",
599 .long_name = NULL_IF_CONFIG_SMALL("libaom AV1"),
600 .type = AVMEDIA_TYPE_VIDEO,
601 .id = AV_CODEC_ID_AV1,
602 .priv_data_size = sizeof(AOMContext),
603 .init = aom_init,
604 .encode2 = aom_encode,
605 .close = aom_free,
606 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS | AV_CODEC_CAP_EXPERIMENTAL,
607 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
608 .priv_class = &class_aom,
609 .defaults = defaults,
610 .wrapper_name = "libaom",
611};