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 #include <EbSvtAv1Metadata.h>
27
37
44
49 }EOS_STATUS;
50
53
56
60
62
64
66
68
69 // User options.
75
76 static const struct {
81 { EB_ErrorNone, 0, "success" },
82 { EB_ErrorInsufficientResources,
AVERROR(ENOMEM),
"insufficient resources" },
83 { EB_ErrorUndefined,
AVERROR(EINVAL),
"undefined error" },
84 { EB_ErrorInvalidComponent,
AVERROR(EINVAL),
"invalid component" },
85 { EB_ErrorBadParameter,
AVERROR(EINVAL),
"bad parameter" },
86 { EB_ErrorDestroyThreadFailed,
AVERROR_EXTERNAL,
"failed to destroy thread" },
87 { EB_ErrorSemaphoreUnresponsive,
AVERROR_EXTERNAL,
"semaphore unresponsive" },
88 { EB_ErrorDestroySemaphoreFailed,
AVERROR_EXTERNAL,
"failed to destroy semaphore"},
92 { EB_NoErrorEmptyQueue,
AVERROR(EAGAIN),
"empty queue" },
93 };
94
96 {
98
104 }
105 }
106 *
desc =
"unknown error";
108 }
109
111 const char *error_string)
112 {
115
117
119 }
120
122 {
123 const size_t luma_size =
config->source_width *
config->source_height *
124 (
config->encoder_bit_depth > 8 ? 2 : 1);
125
126 EbSvtIOFormat *in_data;
127
128 svt_enc->
raw_size = luma_size * 3 / 2;
129
130 // allocate buffer for in and out
134
136 if (!svt_enc->
in_buf->p_buffer)
138
140
141 return 0;
142
143 }
144
147 {
149 const struct EbSvtAv1ChromaPoints *const points[] = {
153 };
154
155 for (
int i = 0;
i < 3;
i++) {
156 const struct EbSvtAv1ChromaPoints *
dst = points[
i];
158
163 }
164
171 }
172
180 }
181 }
182
184 EbSvtAv1EncConfiguration *param)
185 {
193
194 if (cll_sd) {
197
200 }
201
202 if (mdcv_sd) {
205 }
206 }
207
210 {
214
215 // Update param from options
217 param->enc_mode = svt_enc->
enc_mode;
218
220 param->target_bit_rate = avctx->
bit_rate;
222 param->rate_control_mode = 1;
223 else
224 param->rate_control_mode = 2;
225
226 param->max_qp_allowed = avctx->
qmax;
227 param->min_qp_allowed = avctx->
qmin;
228 }
231 param->maximum_buffer_size_ms =
234
235 if (svt_enc->
crf > 0) {
236 param->qp = svt_enc->
crf;
237 param->rate_control_mode = 0;
238 }
else if (svt_enc->
qp > 0) {
239 param->qp = svt_enc->
qp;
240 param->rate_control_mode = 0;
241 param->enable_adaptive_quantization = 0;
242 }
243
248 param->transfer_characteristics = avctx->
color_trc;
249
252 else
254
255 #if SVT_AV1_CHECK_VERSION(1, 0, 0)
259
262 param->chroma_sample_position = EB_CSP_VERTICAL;
263 break;
265 param->chroma_sample_position = EB_CSP_COLOCATED;
266 break;
267 default:
269 break;
270
272 "Specified chroma sample location %s is unsupported "
273 "on the AV1 bit stream level. Usage of a container that "
274 "allows passing this information - such as Matroska - "
275 "is recommended.\n",
277 break;
278 }
279 }
280 #endif
281
283 param->profile = avctx->
profile;
284
286 param->level = avctx->
level;
287
288 // gop_size == 1 case is handled when encoding each frame by setting
289 // pic_type to EB_AV1_KEY_PICTURE. For gop_size > 1, set the
290 // intra_period_length. Even though setting intra_period_length to 0 should
291 // work in this case, it does not.
292 // See: https://gitlab.com/AOMediaCodec/SVT-AV1/-/issues/2076
294 param->intra_period_length = avctx->
gop_size - 1;
295
296 #if SVT_AV1_CHECK_VERSION(1, 1, 0)
297 // In order for SVT-AV1 to force keyframes by setting pic_type to
298 // EB_AV1_KEY_PICTURE on any frame, force_key_frames has to be set. Note
299 // that this does not force all frames to be keyframes (it only forces a
300 // keyframe with pic_type is set to EB_AV1_KEY_PICTURE). As of now, SVT-AV1
301 // does not support arbitrary keyframe requests by setting pic_type to
302 // EB_AV1_KEY_PICTURE, so it is done only when gop_size == 1.
303 // FIXME: When SVT-AV1 supports arbitrary keyframe requests, this code needs
304 // to be updated to set force_key_frames accordingly.
306 param->force_key_frames = 1;
307 #endif
308
312 } else {
315 }
316
317 /* 2 = IDR, closed GOP, 1 = CRA, open GOP */
319
321
322 #if SVT_AV1_CHECK_VERSION(0, 9, 1)
324 EbErrorType
ret = svt_av1_enc_parse_parameter(param, en->key, en->value);
325 if (
ret != EB_ErrorNone) {
327 av_log(avctx,
level,
"Error parsing option %s: %s.\n", en->key, en->value);
330 }
331 }
332 #else
335 av_log(avctx,
level,
"svt-params needs libavcodec to be compiled with SVT-AV1 "
336 "headers >= 0.9.1.\n");
339 }
340 #endif
341
342 param->source_width = avctx->
width;
343 param->source_height = avctx->
height;
344
345 param->encoder_bit_depth =
desc->comp[0].depth;
346
347 if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 1)
348 param->encoder_color_format = EB_YUV420;
349 else if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 0)
350 param->encoder_color_format = EB_YUV422;
351 else if (!
desc->log2_chroma_w && !
desc->log2_chroma_h)
352 param->encoder_color_format = EB_YUV444;
353 else {
356 }
357
358 if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
362 }
else if (param->encoder_color_format == EB_YUV444 && param->profile !=
AV_PROFILE_AV1_HIGH) {
365 }
366
367 avctx->
bit_rate = param->rate_control_mode > 0 ?
368 param->target_bit_rate : 0;
372
375 if (!cpb_props)
377
381 }
382
383 return 0;
384 }
385
387 EbBufferHeaderType *header_ptr)
388 {
389 EbSvtIOFormat *in_data = (EbSvtIOFormat *)header_ptr->p_buffer;
390 ptrdiff_t linesizes[4];
392 int bytes_shift = param->encoder_bit_depth > 8 ? 1 : 0;
394
395 for (
int i = 0;
i < 4;
i++)
396 linesizes[
i] =
frame->linesize[
i];
397
399 linesizes);
402
404 for (
int i = 0;
i < 4;
i++) {
408 }
409
410 in_data->luma =
frame->data[0];
411 in_data->cb =
frame->data[1];
412 in_data->cr =
frame->data[2];
413
417
419 svt_metadata_array_free(&header_ptr->metadata);
420
421 return 0;
422 }
423
425 {
427 EbErrorType svt_ret;
429
431
432 #if SVT_AV1_CHECK_VERSION(3, 0, 0)
434 #else
436 #endif
437 if (svt_ret != EB_ErrorNone) {
438 return svt_print_error(avctx, svt_ret,
"Error initializing encoder handle");
439 }
440
445 }
446
448 if (svt_ret != EB_ErrorNone) {
449 return svt_print_error(avctx, svt_ret,
"Error setting encoder parameters");
450 }
451
452 svt_ret = svt_av1_enc_init(svt_enc->
svt_handle);
453 if (svt_ret != EB_ErrorNone) {
455 }
456
461
463 EbBufferHeaderType *headerPtr =
NULL;
464
465 svt_ret = svt_av1_enc_stream_header(svt_enc->
svt_handle, &headerPtr);
466 if (svt_ret != EB_ErrorNone) {
467 return svt_print_error(avctx, svt_ret,
"Error building stream header");
468 }
469
474 "Cannot allocate AV1 header of size %d.\n", avctx->
extradata_size);
476 }
477
479
480 svt_ret = svt_av1_enc_stream_header_release(headerPtr);
481 if (svt_ret != EB_ErrorNone) {
483 }
484 }
485
489
491 }
492
494 {
496 EbBufferHeaderType *headerPtr = svt_enc->
in_buf;
498 EbErrorType svt_ret;
500
502 EbBufferHeaderType headerPtrLast;
503
505 return 0;
506
507 memset(&headerPtrLast, 0, sizeof(headerPtrLast));
508 headerPtrLast.pic_type = EB_AV1_INVALID_PICTURE;
509 headerPtrLast.flags = EB_BUFFERFLAG_EOS;
510
511 svt_av1_enc_send_picture(svt_enc->
svt_handle, &headerPtrLast);
513 return 0;
514 }
515
519
520 headerPtr->flags = 0;
521 headerPtr->p_app_private =
NULL;
522 headerPtr->pts =
frame->pts;
523
524 switch (
frame->pict_type) {
526 headerPtr->pic_type = EB_AV1_KEY_PICTURE;
527 break;
528 default:
529 // Actually means auto, or default.
530 headerPtr->pic_type = EB_AV1_INVALID_PICTURE;
531 break;
532 }
533
535 headerPtr->pic_type = EB_AV1_KEY_PICTURE;
536
540 uint8_t *t35;
545 ret = svt_add_metadata(headerPtr, EB_AV1_METADATA_TYPE_ITUT_T35, t35,
size);
551 "without AV_FRAME_DATA_DOVI_METADATA\n");
553 }
554
555
556 svt_ret = svt_av1_enc_send_picture(svt_enc->
svt_handle, headerPtr);
557 if (svt_ret != EB_ErrorNone)
558 return svt_print_error(avctx, svt_ret,
"Error sending a frame to encoder");
559
560 return 0;
561 }
562
564 {
566 const int max_frames = 8;
567 int max_tu_size;
568
569 if (filled_len > svt_enc->
raw_size * max_frames) {
572 }
573
579
581 }
583
585 }
586
588 {
590 EbBufferHeaderType *headerPtr;
592 EbErrorType svt_ret;
594 int ret = 0, pict_type;
595
598
604
609
611 if (svt_ret == EB_NoErrorEmptyQueue)
613 else if (svt_ret != EB_ErrorNone)
614 return svt_print_error(avctx, svt_ret,
"Error getting an output packet from encoder");
615
616 #if SVT_AV1_CHECK_VERSION(2, 0, 0)
617 if (headerPtr->flags & EB_BUFFERFLAG_EOS) {
619 svt_av1_enc_release_out_buffer(&headerPtr);
621 }
622 #endif
623
627 svt_av1_enc_release_out_buffer(&headerPtr);
629 }
632
633 memcpy(
pkt->
data, headerPtr->p_buffer, headerPtr->n_filled_len);
635
636 pkt->
size = headerPtr->n_filled_len;
637 pkt->
pts = headerPtr->pts;
638 pkt->
dts = headerPtr->dts;
639
640 switch (headerPtr->pic_type) {
641 case EB_AV1_KEY_PICTURE:
643 // fall-through
644 case EB_AV1_INTRA_ONLY_PICTURE:
646 break;
647 case EB_AV1_INVALID_PICTURE:
649 break;
650 default:
652 break;
653 }
654
655 if (headerPtr->pic_type == EB_AV1_NON_REF_PICTURE)
657
658 #if !(SVT_AV1_CHECK_VERSION(2, 0, 0))
659 if (headerPtr->flags & EB_BUFFERFLAG_EOS)
661 #endif
662
664
665 svt_av1_enc_release_out_buffer(&headerPtr);
666
667 return 0;
668 }
669
671 {
673
676 svt_av1_enc_deinit_handle(svt_enc->
svt_handle);
677 }
680 svt_metadata_array_free(&svt_enc->
in_buf->metadata);
682 }
683
687
688 return 0;
689 }
690
691 #define OFFSET(x) offsetof(SvtContext, x)
692 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
694 { "preset", "Encoding preset",
696
698
699 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
700 { .i64 = value }, 0, 0, VE, .unit = "avctx.level"
701 {
LEVEL(
"2.0", 20) },
702 {
LEVEL(
"2.1", 21) },
703 {
LEVEL(
"2.2", 22) },
704 {
LEVEL(
"2.3", 23) },
705 {
LEVEL(
"3.0", 30) },
706 {
LEVEL(
"3.1", 31) },
707 {
LEVEL(
"3.2", 32) },
708 {
LEVEL(
"3.3", 33) },
709 {
LEVEL(
"4.0", 40) },
710 {
LEVEL(
"4.1", 41) },
711 {
LEVEL(
"4.2", 42) },
712 {
LEVEL(
"4.3", 43) },
713 {
LEVEL(
"5.0", 50) },
714 {
LEVEL(
"5.1", 51) },
715 {
LEVEL(
"5.2", 52) },
716 {
LEVEL(
"5.3", 53) },
717 {
LEVEL(
"6.0", 60) },
718 {
LEVEL(
"6.1", 61) },
719 {
LEVEL(
"6.2", 62) },
720 {
LEVEL(
"6.3", 63) },
721 {
LEVEL(
"7.0", 70) },
722 {
LEVEL(
"7.1", 71) },
723 {
LEVEL(
"7.2", 72) },
724 {
LEVEL(
"7.3", 73) },
725 #undef LEVEL
726
727 {
"crf",
"Constant Rate Factor value",
OFFSET(crf),
729 {
"qp",
"Initial Quantizer level value",
OFFSET(qp),
731 {
"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 },
732
735
737 };
738
744 };
745
747 { "b", "0" },
748 { "flags", "+cgop" },
749 { "g", "-1" },
750 { "qmin", "1" },
751 { "qmax", "63" },
753 };
754
756 .
p.
name =
"libsvtav1",
769 .p.priv_class = &class,
771 .p.wrapper_name = "libsvtav1",
772 };