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
35
42
47 }EOS_STATUS;
48
51
54
58
60
62
64
65 // User options.
71
72 static const struct {
77 { EB_ErrorNone, 0, "success" },
78 { EB_ErrorInsufficientResources,
AVERROR(ENOMEM),
"insufficient resources" },
79 { EB_ErrorUndefined,
AVERROR(EINVAL),
"undefined error" },
80 { EB_ErrorInvalidComponent,
AVERROR(EINVAL),
"invalid component" },
81 { EB_ErrorBadParameter,
AVERROR(EINVAL),
"bad parameter" },
82 { EB_ErrorDestroyThreadFailed,
AVERROR_EXTERNAL,
"failed to destroy thread" },
83 { EB_ErrorSemaphoreUnresponsive,
AVERROR_EXTERNAL,
"semaphore unresponsive" },
84 { EB_ErrorDestroySemaphoreFailed,
AVERROR_EXTERNAL,
"failed to destroy semaphore"},
88 { EB_NoErrorEmptyQueue,
AVERROR(EAGAIN),
"empty queue" },
89 };
90
92 {
94
100 }
101 }
102 *
desc =
"unknown error";
104 }
105
107 const char *error_string)
108 {
111
113
115 }
116
118 {
119 const size_t luma_size =
config->source_width *
config->source_height *
120 (
config->encoder_bit_depth > 8 ? 2 : 1);
121
122 EbSvtIOFormat *in_data;
123
124 svt_enc->
raw_size = luma_size * 3 / 2;
125
126 // allocate buffer for in and out
130
132 if (!svt_enc->
in_buf->p_buffer)
134
136
137 return 0;
138
139 }
140
143 {
145 const struct EbSvtAv1ChromaPoints *const points[] = {
146 &dst->r,
147 &dst->g,
148 &dst->b,
149 };
150
151 for (
int i = 0;
i < 3;
i++) {
152 const struct EbSvtAv1ChromaPoints *dst = points[
i];
154
159 }
160
167 }
168
176 }
177 }
178
180 EbSvtAv1EncConfiguration *param)
181 {
189
190 if (cll_sd) {
193
196 }
197
198 if (mdcv_sd) {
201 }
202 }
203
206 {
210
211 // Update param from options
213 param->enc_mode = svt_enc->
enc_mode;
214
216 param->target_bit_rate = avctx->
bit_rate;
218 param->rate_control_mode = 1;
219 else
220 param->rate_control_mode = 2;
221
222 param->max_qp_allowed = avctx->
qmax;
223 param->min_qp_allowed = avctx->
qmin;
224 }
227 param->maximum_buffer_size_ms =
230
231 if (svt_enc->
crf > 0) {
232 param->qp = svt_enc->
crf;
233 param->rate_control_mode = 0;
234 }
else if (svt_enc->
qp > 0) {
235 param->qp = svt_enc->
qp;
236 param->rate_control_mode = 0;
237 param->enable_adaptive_quantization = 0;
238 }
239
244 param->transfer_characteristics = avctx->
color_trc;
245
248 else
250
251 #if SVT_AV1_CHECK_VERSION(1, 0, 0)
255
258 param->chroma_sample_position = EB_CSP_VERTICAL;
259 break;
261 param->chroma_sample_position = EB_CSP_COLOCATED;
262 break;
263 default:
265 break;
266
268 "Specified chroma sample location %s is unsupported "
269 "on the AV1 bit stream level. Usage of a container that "
270 "allows passing this information - such as Matroska - "
271 "is recommended.\n",
273 break;
274 }
275 }
276 #endif
277
279 param->profile = avctx->
profile;
280
282 param->level = avctx->
level;
283
284 // gop_size == 1 case is handled when encoding each frame by setting
285 // pic_type to EB_AV1_KEY_PICTURE. For gop_size > 1, set the
286 // intra_period_length. Even though setting intra_period_length to 0 should
287 // work in this case, it does not.
288 // See: https://gitlab.com/AOMediaCodec/SVT-AV1/-/issues/2076
290 param->intra_period_length = avctx->
gop_size - 1;
291
292 #if SVT_AV1_CHECK_VERSION(1, 1, 0)
293 // In order for SVT-AV1 to force keyframes by setting pic_type to
294 // EB_AV1_KEY_PICTURE on any frame, force_key_frames has to be set. Note
295 // that this does not force all frames to be keyframes (it only forces a
296 // keyframe with pic_type is set to EB_AV1_KEY_PICTURE). As of now, SVT-AV1
297 // does not support arbitrary keyframe requests by setting pic_type to
298 // EB_AV1_KEY_PICTURE, so it is done only when gop_size == 1.
299 // FIXME: When SVT-AV1 supports arbitrary keyframe requests, this code needs
300 // to be updated to set force_key_frames accordingly.
302 param->force_key_frames = 1;
303 #endif
304
308 } else {
312 #if FF_API_TICKS_PER_FRAME
314 #endif
315 ;
317 }
318
319 /* 2 = IDR, closed GOP, 1 = CRA, open GOP */
321
323
324 #if SVT_AV1_CHECK_VERSION(0, 9, 1)
326 EbErrorType
ret = svt_av1_enc_parse_parameter(param, en->
key, en->
value);
327 if (
ret != EB_ErrorNone) {
332 }
333 }
334 #else
337 av_log(avctx,
level,
"svt-params needs libavcodec to be compiled with SVT-AV1 "
338 "headers >= 0.9.1.\n");
341 }
342 #endif
343
344 param->source_width = avctx->
width;
345 param->source_height = avctx->
height;
346
347 param->encoder_bit_depth =
desc->comp[0].depth;
348
349 if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 1)
350 param->encoder_color_format = EB_YUV420;
351 else if (
desc->log2_chroma_w == 1 &&
desc->log2_chroma_h == 0)
352 param->encoder_color_format = EB_YUV422;
353 else if (!
desc->log2_chroma_w && !
desc->log2_chroma_h)
354 param->encoder_color_format = EB_YUV444;
355 else {
358 }
359
360 if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
364 }
else if (param->encoder_color_format == EB_YUV444 && param->profile !=
AV_PROFILE_AV1_HIGH) {
367 }
368
369 avctx->
bit_rate = param->rate_control_mode > 0 ?
370 param->target_bit_rate : 0;
374
377 if (!cpb_props)
379
383 }
384
385 return 0;
386 }
387
389 EbBufferHeaderType *header_ptr)
390 {
391 EbSvtIOFormat *in_data = (EbSvtIOFormat *)header_ptr->p_buffer;
392 ptrdiff_t linesizes[4];
394 int bytes_shift = param->encoder_bit_depth > 8 ? 1 : 0;
396
397 for (
int i = 0;
i < 4;
i++)
399
401 linesizes);
404
406 for (
int i = 0;
i < 4;
i++) {
410 }
411
415
419
421
422 return 0;
423 }
424
426 {
428 EbErrorType svt_ret;
430
432
434 if (svt_ret != EB_ErrorNone) {
435 return svt_print_error(avctx, svt_ret,
"Error initializing encoder handle");
436 }
437
442 }
443
445 if (svt_ret != EB_ErrorNone) {
446 return svt_print_error(avctx, svt_ret,
"Error setting encoder parameters");
447 }
448
449 svt_ret = svt_av1_enc_init(svt_enc->
svt_handle);
450 if (svt_ret != EB_ErrorNone) {
452 }
453
455 EbBufferHeaderType *headerPtr =
NULL;
456
457 svt_ret = svt_av1_enc_stream_header(svt_enc->
svt_handle, &headerPtr);
458 if (svt_ret != EB_ErrorNone) {
459 return svt_print_error(avctx, svt_ret,
"Error building stream header");
460 }
461
466 "Cannot allocate AV1 header of size %d.\n", avctx->
extradata_size);
468 }
469
471
472 svt_ret = svt_av1_enc_stream_header_release(headerPtr);
473 if (svt_ret != EB_ErrorNone) {
475 }
476 }
477
481
483 }
484
486 {
488 EbBufferHeaderType *headerPtr = svt_enc->
in_buf;
490
492 EbBufferHeaderType headerPtrLast;
493
495 return 0;
496
497 memset(&headerPtrLast, 0, sizeof(headerPtrLast));
498 headerPtrLast.pic_type = EB_AV1_INVALID_PICTURE;
499 headerPtrLast.flags = EB_BUFFERFLAG_EOS;
500
501 svt_av1_enc_send_picture(svt_enc->
svt_handle, &headerPtrLast);
503 return 0;
504 }
505
509
510 headerPtr->flags = 0;
511 headerPtr->p_app_private =
NULL;
513
516 headerPtr->pic_type = EB_AV1_KEY_PICTURE;
517 break;
518 default:
519 // Actually means auto, or default.
520 headerPtr->pic_type = EB_AV1_INVALID_PICTURE;
521 break;
522 }
523
525 headerPtr->pic_type = EB_AV1_KEY_PICTURE;
526
527 svt_av1_enc_send_picture(svt_enc->
svt_handle, headerPtr);
528
529 return 0;
530 }
531
533 {
535 const int max_frames = 8;
536 int max_tu_size;
537
538 if (filled_len > svt_enc->
raw_size * max_frames) {
541 }
542
548
550 }
552
554 }
555
557 {
559 EbBufferHeaderType *headerPtr;
561 EbErrorType svt_ret;
563 int ret = 0, pict_type;
564
567
573
578
580 if (svt_ret == EB_NoErrorEmptyQueue)
582
583 #if SVT_AV1_CHECK_VERSION(2, 0, 0)
584 if (headerPtr->flags & EB_BUFFERFLAG_EOS) {
586 svt_av1_enc_release_out_buffer(&headerPtr);
588 }
589 #endif
590
594 svt_av1_enc_release_out_buffer(&headerPtr);
596 }
599
600 memcpy(
pkt->
data, headerPtr->p_buffer, headerPtr->n_filled_len);
602
603 pkt->
size = headerPtr->n_filled_len;
604 pkt->
pts = headerPtr->pts;
605 pkt->
dts = headerPtr->dts;
606
607 switch (headerPtr->pic_type) {
608 case EB_AV1_KEY_PICTURE:
610 // fall-through
611 case EB_AV1_INTRA_ONLY_PICTURE:
613 break;
614 case EB_AV1_INVALID_PICTURE:
616 break;
617 default:
619 break;
620 }
621
622 if (headerPtr->pic_type == EB_AV1_NON_REF_PICTURE)
624
625 #if !(SVT_AV1_CHECK_VERSION(2, 0, 0))
626 if (headerPtr->flags & EB_BUFFERFLAG_EOS)
628 #endif
629
631
632 svt_av1_enc_release_out_buffer(&headerPtr);
633
634 return 0;
635 }
636
638 {
640
643 svt_av1_enc_deinit_handle(svt_enc->
svt_handle);
644 }
648 }
649
652
653 return 0;
654 }
655
656 #define OFFSET(x) offsetof(SvtContext, x)
657 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
659 { "preset", "Encoding preset",
661
663
664 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
665 { .i64 = value }, 0, 0, VE, .unit = "avctx.level"
666 {
LEVEL(
"2.0", 20) },
667 {
LEVEL(
"2.1", 21) },
668 {
LEVEL(
"2.2", 22) },
669 {
LEVEL(
"2.3", 23) },
670 {
LEVEL(
"3.0", 30) },
671 {
LEVEL(
"3.1", 31) },
672 {
LEVEL(
"3.2", 32) },
673 {
LEVEL(
"3.3", 33) },
674 {
LEVEL(
"4.0", 40) },
675 {
LEVEL(
"4.1", 41) },
676 {
LEVEL(
"4.2", 42) },
677 {
LEVEL(
"4.3", 43) },
678 {
LEVEL(
"5.0", 50) },
679 {
LEVEL(
"5.1", 51) },
680 {
LEVEL(
"5.2", 52) },
681 {
LEVEL(
"5.3", 53) },
682 {
LEVEL(
"6.0", 60) },
683 {
LEVEL(
"6.1", 61) },
684 {
LEVEL(
"6.2", 62) },
685 {
LEVEL(
"6.3", 63) },
686 {
LEVEL(
"7.0", 70) },
687 {
LEVEL(
"7.1", 71) },
688 {
LEVEL(
"7.2", 72) },
689 {
LEVEL(
"7.3", 73) },
690 #undef LEVEL
691
692 {
"crf",
"Constant Rate Factor value",
OFFSET(crf),
694 {
"qp",
"Initial Quantizer level value",
OFFSET(qp),
696 {
"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 },
697
699 };
700
706 };
707
709 { "b", "0" },
710 { "flags", "+cgop" },
711 { "g", "-1" },
712 { "qmin", "1" },
713 { "qmax", "63" },
715 };
716
718 .
p.
name =
"libsvtav1",
732 .p.priv_class = &class,
734 .p.wrapper_name = "libsvtav1",
735 };