1 /*
2 * Scalable Video Technology for AV1 encoder library plugin
3 *
4 * Copyright (c) 2018 Intel Corporation
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <stdint.h>
24 #include <EbSvtAv1ErrorCodes.h>
25 #include <EbSvtAv1Enc.h>
26
27 #include "libavutil/common.h"
28 #include "libavutil/frame.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/pixdesc.h"
32 #include "libavutil/avassert.h"
33
34 #include "codec_internal.h"
35 #include "internal.h"
36 #include "encode.h"
37 #include "packet_internal.h"
38 #include "avcodec.h"
39 #include "profiles.h"
40
41 typedef enum eos_status {
42 EOS_NOT_REACHED = 0,
43 EOS_SENT,
44 EOS_RECEIVED
45 }EOS_STATUS;
46
47 typedef struct SvtContext {
48 const AVClass *class;
49
50 EbSvtAv1EncConfiguration enc_params;
51 EbComponentType *svt_handle;
52
53 EbBufferHeaderType *in_buf;
54 int raw_size;
55 int max_tu_size;
56
57 AVFrame *frame;
58
59 AVBufferPool *pool;
60
61 EOS_STATUS eos_flag;
62
63 // User options.
64 AVDictionary *svtav1_opts;
65 #if FF_API_SVTAV1_OPTS
66 int hierarchical_level;
67 int la_depth;
68 int scd;
69
70 int tier;
71
72 int tile_columns;
73 int tile_rows;
74 #endif
75 int enc_mode;
76 int crf;
77 int qp;
78 } SvtContext;
79
80 static const struct {
81 EbErrorType eb_err;
82 int av_err;
83 const char *desc;
84 } svt_errors[] = {
85 { EB_ErrorNone, 0, "success" },
86 { EB_ErrorInsufficientResources, AVERROR(ENOMEM), "insufficient resources" },
87 { EB_ErrorUndefined, AVERROR(EINVAL), "undefined error" },
88 { EB_ErrorInvalidComponent, AVERROR(EINVAL), "invalid component" },
89 { EB_ErrorBadParameter, AVERROR(EINVAL), "bad parameter" },
90 { EB_ErrorDestroyThreadFailed, AVERROR_EXTERNAL, "failed to destroy thread" },
91 { EB_ErrorSemaphoreUnresponsive, AVERROR_EXTERNAL, "semaphore unresponsive" },
92 { EB_ErrorDestroySemaphoreFailed, AVERROR_EXTERNAL, "failed to destroy semaphore"},
93 { EB_ErrorCreateMutexFailed, AVERROR_EXTERNAL, "failed to create mutex" },
94 { EB_ErrorMutexUnresponsive, AVERROR_EXTERNAL, "mutex unresponsive" },
95 { EB_ErrorDestroyMutexFailed, AVERROR_EXTERNAL, "failed to destroy mutex" },
96 { EB_NoErrorEmptyQueue, AVERROR(EAGAIN), "empty queue" },
97 };
98
svt_map_error(EbErrorType eb_err, const char **desc)99 static int svt_map_error(EbErrorType eb_err, const char **desc)
100 {
101 int i;
102
103 av_assert0(desc);
104 for (i = 0; i < FF_ARRAY_ELEMS(svt_errors); i++) {
105 if (svt_errors[i].eb_err == eb_err) {
106 *desc = svt_errors[i].desc;
107 return svt_errors[i].av_err;
108 }
109 }
110 *desc = "unknown error";
111 return AVERROR_UNKNOWN;
112 }
113
svt_print_error(void *log_ctx, EbErrorType err, const char *error_string)114 static int svt_print_error(void *log_ctx, EbErrorType err,
115 const char *error_string)
116 {
117 const char *desc;
118 int ret = svt_map_error(err, &desc);
119
120 av_log(log_ctx, AV_LOG_ERROR, "%s: %s (0x%x)\n", error_string, desc, err);
121
122 return ret;
123 }
124
alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)125 static int alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)
126 {
127 const size_t luma_size = config->source_width * config->source_height *
128 (config->encoder_bit_depth > 8 ? 2 : 1);
129
130 EbSvtIOFormat *in_data;
131
132 svt_enc->raw_size = luma_size * 3 / 2;
133
134 // allocate buffer for in and out
135 svt_enc->in_buf = av_mallocz(sizeof(*svt_enc->in_buf));
136 if (!svt_enc->in_buf)
137 return AVERROR(ENOMEM);
138
139 svt_enc->in_buf->p_buffer = av_mallocz(sizeof(*in_data));
140 if (!svt_enc->in_buf->p_buffer)
141 return AVERROR(ENOMEM);
142
143 svt_enc->in_buf->size = sizeof(*svt_enc->in_buf);
144
145 return 0;
146
147 }
148
config_enc_params(EbSvtAv1EncConfiguration *param, AVCodecContext *avctx)149 static int config_enc_params(EbSvtAv1EncConfiguration *param,
150 AVCodecContext *avctx)
151 {
152 SvtContext *svt_enc = avctx->priv_data;
153 const AVPixFmtDescriptor *desc;
154 AVDictionaryEntry *en = NULL;
155
156 // Update param from options
157 #if FF_API_SVTAV1_OPTS
158 param->hierarchical_levels = svt_enc->hierarchical_level;
159 param->tier = svt_enc->tier;
160 param->scene_change_detection = svt_enc->scd;
161 param->tile_columns = svt_enc->tile_columns;
162 param->tile_rows = svt_enc->tile_rows;
163
164 if (svt_enc->la_depth >= 0)
165 param->look_ahead_distance = svt_enc->la_depth;
166 #endif
167
168 if (svt_enc->enc_mode >= 0)
169 param->enc_mode = svt_enc->enc_mode;
170
171 if (avctx->bit_rate) {
172 param->target_bit_rate = avctx->bit_rate;
173 if (avctx->rc_max_rate != avctx->bit_rate)
174 param->rate_control_mode = 1;
175 else
176 param->rate_control_mode = 2;
177
178 param->max_qp_allowed = avctx->qmax;
179 param->min_qp_allowed = avctx->qmin;
180 }
181 param->max_bit_rate = avctx->rc_max_rate;
182 if ((avctx->bit_rate > 0 || avctx->rc_max_rate > 0) && avctx->rc_buffer_size)
183 param->maximum_buffer_size_ms =
184 avctx->rc_buffer_size * 1000LL /
185 FFMAX(avctx->bit_rate, avctx->rc_max_rate);
186
187 if (svt_enc->crf > 0) {
188 param->qp = svt_enc->crf;
189 param->rate_control_mode = 0;
190 } else if (svt_enc->qp > 0) {
191 param->qp = svt_enc->qp;
192 param->rate_control_mode = 0;
193 param->enable_adaptive_quantization = 0;
194 }
195
196 desc = av_pix_fmt_desc_get(avctx->pix_fmt);
197 param->color_primaries = avctx->color_primaries;
198 param->matrix_coefficients = (desc->flags & AV_PIX_FMT_FLAG_RGB) ?
199 AVCOL_SPC_RGB : avctx->colorspace;
200 param->transfer_characteristics = avctx->color_trc;
201
202 if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED)
203 param->color_range = avctx->color_range == AVCOL_RANGE_JPEG;
204 else
205 param->color_range = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
206
207 #if SVT_AV1_CHECK_VERSION(1, 0, 0)
208 if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
209 const char *name =
210 av_chroma_location_name(avctx->chroma_sample_location);
211
212 switch (avctx->chroma_sample_location) {
213 case AVCHROMA_LOC_LEFT:
214 param->chroma_sample_position = EB_CSP_VERTICAL;
215 break;
216 case AVCHROMA_LOC_TOPLEFT:
217 param->chroma_sample_position = EB_CSP_COLOCATED;
218 break;
219 default:
220 if (!name)
221 break;
222
223 av_log(avctx, AV_LOG_WARNING,
224 "Specified chroma sample location %s is unsupported "
225 "on the AV1 bit stream level. Usage of a container that "
226 "allows passing this information - such as Matroska - "
227 "is recommended.\n",
228 name);
229 break;
230 }
231 }
232 #endif
233
234 if (avctx->profile != FF_PROFILE_UNKNOWN)
235 param->profile = avctx->profile;
236
237 if (avctx->level != FF_LEVEL_UNKNOWN)
238 param->level = avctx->level;
239
240 if (avctx->gop_size > 0)
241 param->intra_period_length = avctx->gop_size - 1;
242
243 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
244 param->frame_rate_numerator = avctx->framerate.num;
245 param->frame_rate_denominator = avctx->framerate.den;
246 } else {
247 param->frame_rate_numerator = avctx->time_base.den;
248 param->frame_rate_denominator = avctx->time_base.num * avctx->ticks_per_frame;
249 }
250
251 /* 2 = IDR, closed GOP, 1 = CRA, open GOP */
252 param->intra_refresh_type = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ? 2 : 1;
253
254 #if SVT_AV1_CHECK_VERSION(0, 9, 1)
255 while ((en = av_dict_get(svt_enc->svtav1_opts, "", en, AV_DICT_IGNORE_SUFFIX))) {
256 EbErrorType ret = svt_av1_enc_parse_parameter(param, en->key, en->value);
257 if (ret != EB_ErrorNone) {
258 int level = (avctx->err_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
259 av_log(avctx, level, "Error parsing option %s: %s.\n", en->key, en->value);
260 if (avctx->err_recognition & AV_EF_EXPLODE)
261 return AVERROR(EINVAL);
262 }
263 }
264 #else
265 if ((en = av_dict_get(svt_enc->svtav1_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
266 int level = (avctx->err_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
267 av_log(avctx, level, "svt-params needs libavcodec to be compiled with SVT-AV1 "
268 "headers >= 0.9.1.\n");
269 if (avctx->err_recognition & AV_EF_EXPLODE)
270 return AVERROR(ENOSYS);
271 }
272 #endif
273
274 param->source_width = avctx->width;
275 param->source_height = avctx->height;
276
277 param->encoder_bit_depth = desc->comp[0].depth;
278
279 if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1)
280 param->encoder_color_format = EB_YUV420;
281 else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0)
282 param->encoder_color_format = EB_YUV422;
283 else if (!desc->log2_chroma_w && !desc->log2_chroma_h)
284 param->encoder_color_format = EB_YUV444;
285 else {
286 av_log(avctx, AV_LOG_ERROR , "Unsupported pixel format\n");
287 return AVERROR(EINVAL);
288 }
289
290 if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
291 && param->profile != FF_PROFILE_AV1_PROFESSIONAL ) {
292 av_log(avctx, AV_LOG_WARNING, "Forcing Professional profile\n");
293 param->profile = FF_PROFILE_AV1_PROFESSIONAL;
294 } else if (param->encoder_color_format == EB_YUV444 && param->profile != FF_PROFILE_AV1_HIGH) {
295 av_log(avctx, AV_LOG_WARNING, "Forcing High profile\n");
296 param->profile = FF_PROFILE_AV1_HIGH;
297 }
298
299 avctx->bit_rate = param->rate_control_mode > 0 ?
300 param->target_bit_rate : 0;
301 avctx->rc_max_rate = param->max_bit_rate;
302 avctx->rc_buffer_size = param->maximum_buffer_size_ms *
303 FFMAX(avctx->bit_rate, avctx->rc_max_rate) / 1000LL;
304
305 if (avctx->bit_rate || avctx->rc_max_rate || avctx->rc_buffer_size) {
306 AVCPBProperties *cpb_props = ff_add_cpb_side_data(avctx);
307 if (!cpb_props)
308 return AVERROR(ENOMEM);
309
310 cpb_props->buffer_size = avctx->rc_buffer_size;
311 cpb_props->max_bitrate = avctx->rc_max_rate;
312 cpb_props->avg_bitrate = avctx->bit_rate;
313 }
314
315 return 0;
316 }
317
read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame, EbBufferHeaderType *header_ptr)318 static int read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame,
319 EbBufferHeaderType *header_ptr)
320 {
321 EbSvtIOFormat *in_data = (EbSvtIOFormat *)header_ptr->p_buffer;
322 ptrdiff_t linesizes[4];
323 size_t sizes[4];
324 int bytes_shift = param->encoder_bit_depth > 8 ? 1 : 0;
325 int ret, frame_size;
326
327 for (int i = 0; i < 4; i++)
328 linesizes[i] = frame->linesize[i];
329
330 ret = av_image_fill_plane_sizes(sizes, frame->format, frame->height,
331 linesizes);
332 if (ret < 0)
333 return ret;
334
335 frame_size = 0;
336 for (int i = 0; i < 4; i++) {
337 if (sizes[i] > INT_MAX - frame_size)
338 return AVERROR(EINVAL);
339 frame_size += sizes[i];
340 }
341
342 in_data->luma = frame->data[0];
343 in_data->cb = frame->data[1];
344 in_data->cr = frame->data[2];
345
346 in_data->y_stride = AV_CEIL_RSHIFT(frame->linesize[0], bytes_shift);
347 in_data->cb_stride = AV_CEIL_RSHIFT(frame->linesize[1], bytes_shift);
348 in_data->cr_stride = AV_CEIL_RSHIFT(frame->linesize[2], bytes_shift);
349
350 header_ptr->n_filled_len = frame_size;
351
352 return 0;
353 }
354
eb_enc_init(AVCodecContext *avctx)355 static av_cold int eb_enc_init(AVCodecContext *avctx)
356 {
357 SvtContext *svt_enc = avctx->priv_data;
358 EbErrorType svt_ret;
359 int ret;
360
361 svt_enc->eos_flag = EOS_NOT_REACHED;
362
363 svt_ret = svt_av1_enc_init_handle(&svt_enc->svt_handle, svt_enc, &svt_enc->enc_params);
364 if (svt_ret != EB_ErrorNone) {
365 return svt_print_error(avctx, svt_ret, "Error initializing encoder handle");
366 }
367
368 ret = config_enc_params(&svt_enc->enc_params, avctx);
369 if (ret < 0) {
370 av_log(avctx, AV_LOG_ERROR, "Error configuring encoder parameters\n");
371 return ret;
372 }
373
374 svt_ret = svt_av1_enc_set_parameter(svt_enc->svt_handle, &svt_enc->enc_params);
375 if (svt_ret != EB_ErrorNone) {
376 return svt_print_error(avctx, svt_ret, "Error setting encoder parameters");
377 }
378
379 svt_ret = svt_av1_enc_init(svt_enc->svt_handle);
380 if (svt_ret != EB_ErrorNone) {
381 return svt_print_error(avctx, svt_ret, "Error initializing encoder");
382 }
383
384 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
385 EbBufferHeaderType *headerPtr = NULL;
386
387 svt_ret = svt_av1_enc_stream_header(svt_enc->svt_handle, &headerPtr);
388 if (svt_ret != EB_ErrorNone) {
389 return svt_print_error(avctx, svt_ret, "Error building stream header");
390 }
391
392 avctx->extradata_size = headerPtr->n_filled_len;
393 avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
394 if (!avctx->extradata) {
395 av_log(avctx, AV_LOG_ERROR,
396 "Cannot allocate AV1 header of size %d.\n", avctx->extradata_size);
397 return AVERROR(ENOMEM);
398 }
399
400 memcpy(avctx->extradata, headerPtr->p_buffer, avctx->extradata_size);
401
402 svt_ret = svt_av1_enc_stream_header_release(headerPtr);
403 if (svt_ret != EB_ErrorNone) {
404 return svt_print_error(avctx, svt_ret, "Error freeing stream header");
405 }
406 }
407
408 svt_enc->frame = av_frame_alloc();
409 if (!svt_enc->frame)
410 return AVERROR(ENOMEM);
411
412 return alloc_buffer(&svt_enc->enc_params, svt_enc);
413 }
414
eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)415 static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
416 {
417 SvtContext *svt_enc = avctx->priv_data;
418 EbBufferHeaderType *headerPtr = svt_enc->in_buf;
419 int ret;
420
421 if (!frame) {
422 EbBufferHeaderType headerPtrLast;
423
424 if (svt_enc->eos_flag == EOS_SENT)
425 return 0;
426
427 memset(&headerPtrLast, 0, sizeof(headerPtrLast));
428 headerPtrLast.pic_type = EB_AV1_INVALID_PICTURE;
429 headerPtrLast.flags = EB_BUFFERFLAG_EOS;
430
431 svt_av1_enc_send_picture(svt_enc->svt_handle, &headerPtrLast);
432 svt_enc->eos_flag = EOS_SENT;
433 return 0;
434 }
435
436 ret = read_in_data(&svt_enc->enc_params, frame, headerPtr);
437 if (ret < 0)
438 return ret;
439
440 headerPtr->flags = 0;
441 headerPtr->p_app_private = NULL;
442 headerPtr->pts = frame->pts;
443
444 switch (frame->pict_type) {
445 case AV_PICTURE_TYPE_I:
446 headerPtr->pic_type = EB_AV1_KEY_PICTURE;
447 break;
448 default:
449 // Actually means auto, or default.
450 headerPtr->pic_type = EB_AV1_INVALID_PICTURE;
451 break;
452 }
453
454 svt_av1_enc_send_picture(svt_enc->svt_handle, headerPtr);
455
456 return 0;
457 }
458
get_output_ref(AVCodecContext *avctx, SvtContext *svt_enc, int filled_len)459 static AVBufferRef *get_output_ref(AVCodecContext *avctx, SvtContext *svt_enc, int filled_len)
460 {
461 if (filled_len > svt_enc->max_tu_size) {
462 const int max_frames = 8;
463 int max_tu_size;
464
465 if (filled_len > svt_enc->raw_size * max_frames) {
466 av_log(avctx, AV_LOG_ERROR, "TU size > %d raw frame size.\n", max_frames);
467 return NULL;
468 }
469
470 max_tu_size = 1 << av_ceil_log2(filled_len);
471 av_buffer_pool_uninit(&svt_enc->pool);
472 svt_enc->pool = av_buffer_pool_init(max_tu_size + AV_INPUT_BUFFER_PADDING_SIZE, NULL);
473 if (!svt_enc->pool)
474 return NULL;
475
476 svt_enc->max_tu_size = max_tu_size;
477 }
478 av_assert0(svt_enc->pool);
479
480 return av_buffer_pool_get(svt_enc->pool);
481 }
482
eb_receive_packet(AVCodecContext *avctx, AVPacket *pkt)483 static int eb_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
484 {
485 SvtContext *svt_enc = avctx->priv_data;
486 EbBufferHeaderType *headerPtr;
487 AVFrame *frame = svt_enc->frame;
488 EbErrorType svt_ret;
489 AVBufferRef *ref;
490 int ret = 0, pict_type;
491
492 if (svt_enc->eos_flag == EOS_RECEIVED)
493 return AVERROR_EOF;
494
495 ret = ff_encode_get_frame(avctx, frame);
496 if (ret < 0 && ret != AVERROR_EOF)
497 return ret;
498 if (ret == AVERROR_EOF)
499 frame = NULL;
500
501 ret = eb_send_frame(avctx, frame);
502 if (ret < 0)
503 return ret;
504 av_frame_unref(svt_enc->frame);
505
506 svt_ret = svt_av1_enc_get_packet(svt_enc->svt_handle, &headerPtr, svt_enc->eos_flag);
507 if (svt_ret == EB_NoErrorEmptyQueue)
508 return AVERROR(EAGAIN);
509
510 ref = get_output_ref(avctx, svt_enc, headerPtr->n_filled_len);
511 if (!ref) {
512 av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
513 svt_av1_enc_release_out_buffer(&headerPtr);
514 return AVERROR(ENOMEM);
515 }
516 pkt->buf = ref;
517 pkt->data = ref->data;
518
519 memcpy(pkt->data, headerPtr->p_buffer, headerPtr->n_filled_len);
520 memset(pkt->data + headerPtr->n_filled_len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
521
522 pkt->size = headerPtr->n_filled_len;
523 pkt->pts = headerPtr->pts;
524 pkt->dts = headerPtr->dts;
525
526 switch (headerPtr->pic_type) {
527 case EB_AV1_KEY_PICTURE:
528 pkt->flags |= AV_PKT_FLAG_KEY;
529 // fall-through
530 case EB_AV1_INTRA_ONLY_PICTURE:
531 pict_type = AV_PICTURE_TYPE_I;
532 break;
533 case EB_AV1_INVALID_PICTURE:
534 pict_type = AV_PICTURE_TYPE_NONE;
535 break;
536 default:
537 pict_type = AV_PICTURE_TYPE_P;
538 break;
539 }
540
541 if (headerPtr->pic_type == EB_AV1_NON_REF_PICTURE)
542 pkt->flags |= AV_PKT_FLAG_DISPOSABLE;
543
544 if (headerPtr->flags & EB_BUFFERFLAG_EOS)
545 svt_enc->eos_flag = EOS_RECEIVED;
546
547 ff_side_data_set_encoder_stats(pkt, headerPtr->qp * FF_QP2LAMBDA, NULL, 0, pict_type);
548
549 svt_av1_enc_release_out_buffer(&headerPtr);
550
551 return 0;
552 }
553
eb_enc_close(AVCodecContext *avctx)554 static av_cold int eb_enc_close(AVCodecContext *avctx)
555 {
556 SvtContext *svt_enc = avctx->priv_data;
557
558 if (svt_enc->svt_handle) {
559 svt_av1_enc_deinit(svt_enc->svt_handle);
560 svt_av1_enc_deinit_handle(svt_enc->svt_handle);
561 }
562 if (svt_enc->in_buf) {
563 av_free(svt_enc->in_buf->p_buffer);
564 av_freep(&svt_enc->in_buf);
565 }
566
567 av_buffer_pool_uninit(&svt_enc->pool);
568 av_frame_free(&svt_enc->frame);
569
570 return 0;
571 }
572
573 #define OFFSET(x) offsetof(SvtContext, x)
574 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
575 static const AVOption options[] = {
576 #if FF_API_SVTAV1_OPTS
577 { "hielevel", "Hierarchical prediction levels setting (Deprecated, use svtav1-params)", OFFSET(hierarchical_level),
578 AV_OPT_TYPE_INT, { .i64 = 4 }, 3, 4, VE | AV_OPT_FLAG_DEPRECATED , "hielevel"},
579 { "3level", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "hielevel" },
580 { "4level", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 4 }, INT_MIN, INT_MAX, VE, "hielevel" },
581
582 { "la_depth", "Look ahead distance [0, 120] (Deprecated, use svtav1-params)", OFFSET(la_depth),
583 AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 120, VE | AV_OPT_FLAG_DEPRECATED },
584
585 { "tier", "Set operating point tier (Deprecated, use svtav1-params)", OFFSET(tier),
586 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE | AV_OPT_FLAG_DEPRECATED, "tier" },
587 { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, VE, "tier" },
588 { "high", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, VE, "tier" },
589 #endif
590 { "preset", "Encoding preset",
591 OFFSET(enc_mode), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, MAX_ENC_PRESET, VE },
592
593 FF_AV1_PROFILE_OPTS
594
595 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
596 { .i64 = value }, 0, 0, VE, "avctx.level"
597 { LEVEL("2.0", 20) },
598 { LEVEL("2.1", 21) },
599 { LEVEL("2.2", 22) },
600 { LEVEL("2.3", 23) },
601 { LEVEL("3.0", 30) },
602 { LEVEL("3.1", 31) },
603 { LEVEL("3.2", 32) },
604 { LEVEL("3.3", 33) },
605 { LEVEL("4.0", 40) },
606 { LEVEL("4.1", 41) },
607 { LEVEL("4.2", 42) },
608 { LEVEL("4.3", 43) },
609 { LEVEL("5.0", 50) },
610 { LEVEL("5.1", 51) },
611 { LEVEL("5.2", 52) },
612 { LEVEL("5.3", 53) },
613 { LEVEL("6.0", 60) },
614 { LEVEL("6.1", 61) },
615 { LEVEL("6.2", 62) },
616 { LEVEL("6.3", 63) },
617 { LEVEL("7.0", 70) },
618 { LEVEL("7.1", 71) },
619 { LEVEL("7.2", 72) },
620 { LEVEL("7.3", 73) },
621 #undef LEVEL
622
623 { "crf", "Constant Rate Factor value", OFFSET(crf),
624 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 63, VE },
625 { "qp", "Initial Quantizer level value", OFFSET(qp),
626 AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 63, VE },
627 #if FF_API_SVTAV1_OPTS
628 { "sc_detection", "Scene change detection (Deprecated, use svtav1-params)", OFFSET(scd),
629 AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE | AV_OPT_FLAG_DEPRECATED },
630
631 { "tile_columns", "Log2 of number of tile columns to use (Deprecated, use svtav1-params)", OFFSET(tile_columns), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 4, VE | AV_OPT_FLAG_DEPRECATED },
632 { "tile_rows", "Log2 of number of tile rows to use (Deprecated, use svtav1-params)", OFFSET(tile_rows), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 6, VE | AV_OPT_FLAG_DEPRECATED },
633 #endif
634
635 { "svtav1-params", "Set the SVT-AV1 configuration using a :-separated list of key=value parameters", OFFSET(svtav1_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
636
637 {NULL},
638 };
639
640 static const AVClass class = {
641 .class_name = "libsvtav1",
642 .item_name = av_default_item_name,
643 .option = options,
644 .version = LIBAVUTIL_VERSION_INT,
645 };
646
647 static const FFCodecDefault eb_enc_defaults[] = {
648 { "b", "0" },
649 { "flags", "+cgop" },
650 { "g", "-1" },
651 { "qmin", "1" },
652 { "qmax", "63" },
653 { NULL },
654 };
655
656 const FFCodec ff_libsvtav1_encoder = {
657 .p.name = "libsvtav1",
658 .p.long_name = NULL_IF_CONFIG_SMALL("SVT-AV1(Scalable Video Technology for AV1) encoder"),
659 .priv_data_size = sizeof(SvtContext),
660 .p.type = AVMEDIA_TYPE_VIDEO,
661 .p.id = AV_CODEC_ID_AV1,
662 .init = eb_enc_init,
663 FF_CODEC_RECEIVE_PACKET_CB(eb_receive_packet),
664 .close = eb_enc_close,
665 .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
666 .caps_internal = FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP,
667 .p.pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
668 AV_PIX_FMT_YUV420P10,
669 AV_PIX_FMT_NONE },
670 .p.priv_class = &class,
671 .defaults = eb_enc_defaults,
672 .p.wrapper_name = "libsvtav1",
673 };
674