libsvtav1.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. #include <stdint.h>
  23. #include <EbSvtAv1ErrorCodes.h>
  24. #include <EbSvtAv1Enc.h>
  25. #include <EbSvtAv1Metadata.h>
  26. #include "libavutil/common.h"
  27. #include "libavutil/frame.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/intreadwrite.h"
  30. #include "libavutil/mastering_display_metadata.h"
  31. #include "libavutil/mem.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/avassert.h"
  35. #include "codec_internal.h"
  36. #include "dovi_rpu.h"
  37. #include "encode.h"
  38. #include "packet_internal.h"
  39. #include "avcodec.h"
  40. #include "profiles.h"
  41. typedef enum eos_status {
  42. EOS_NOT_REACHED = 0,
  43. EOS_SENT,
  44. EOS_RECEIVED
  45. }EOS_STATUS;
  46. typedef struct SvtContext {
  47. const AVClass *class;
  48. EbSvtAv1EncConfiguration enc_params;
  49. EbComponentType *svt_handle;
  50. EbBufferHeaderType *in_buf;
  51. int raw_size;
  52. int max_tu_size;
  53. AVFrame *frame;
  54. AVBufferPool *pool;
  55. EOS_STATUS eos_flag;
  56. DOVIContext dovi;
  57. // User options.
  58. AVDictionary *svtav1_opts;
  59. int enc_mode;
  60. int crf;
  61. int qp;
  62. } SvtContext;
  63. static const struct {
  64. EbErrorType eb_err;
  65. int av_err;
  66. const char *desc;
  67. } svt_errors[] = {
  68. { EB_ErrorNone, 0, "success" },
  69. { EB_ErrorInsufficientResources, AVERROR(ENOMEM), "insufficient resources" },
  70. { EB_ErrorUndefined, AVERROR(EINVAL), "undefined error" },
  71. { EB_ErrorInvalidComponent, AVERROR(EINVAL), "invalid component" },
  72. { EB_ErrorBadParameter, AVERROR(EINVAL), "bad parameter" },
  73. { EB_ErrorDestroyThreadFailed, AVERROR_EXTERNAL, "failed to destroy thread" },
  74. { EB_ErrorSemaphoreUnresponsive, AVERROR_EXTERNAL, "semaphore unresponsive" },
  75. { EB_ErrorDestroySemaphoreFailed, AVERROR_EXTERNAL, "failed to destroy semaphore"},
  76. { EB_ErrorCreateMutexFailed, AVERROR_EXTERNAL, "failed to create mutex" },
  77. { EB_ErrorMutexUnresponsive, AVERROR_EXTERNAL, "mutex unresponsive" },
  78. { EB_ErrorDestroyMutexFailed, AVERROR_EXTERNAL, "failed to destroy mutex" },
  79. { EB_NoErrorEmptyQueue, AVERROR(EAGAIN), "empty queue" },
  80. };
  81. static int svt_map_error(EbErrorType eb_err, const char **desc)
  82. {
  83. int i;
  84. av_assert0(desc);
  85. for (i = 0; i < FF_ARRAY_ELEMS(svt_errors); i++) {
  86. if (svt_errors[i].eb_err == eb_err) {
  87. *desc = svt_errors[i].desc;
  88. return svt_errors[i].av_err;
  89. }
  90. }
  91. *desc = "unknown error";
  92. return AVERROR_UNKNOWN;
  93. }
  94. static int svt_print_error(void *log_ctx, EbErrorType err,
  95. const char *error_string)
  96. {
  97. const char *desc;
  98. int ret = svt_map_error(err, &desc);
  99. av_log(log_ctx, AV_LOG_ERROR, "%s: %s (0x%x)\n", error_string, desc, err);
  100. return ret;
  101. }
  102. static int alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)
  103. {
  104. const size_t luma_size = config->source_width * config->source_height *
  105. (config->encoder_bit_depth > 8 ? 2 : 1);
  106. EbSvtIOFormat *in_data;
  107. svt_enc->raw_size = luma_size * 3 / 2;
  108. // allocate buffer for in and out
  109. svt_enc->in_buf = av_mallocz(sizeof(*svt_enc->in_buf));
  110. if (!svt_enc->in_buf)
  111. return AVERROR(ENOMEM);
  112. svt_enc->in_buf->p_buffer = av_mallocz(sizeof(*in_data));
  113. if (!svt_enc->in_buf->p_buffer)
  114. return AVERROR(ENOMEM);
  115. svt_enc->in_buf->size = sizeof(*svt_enc->in_buf);
  116. return 0;
  117. }
  118. static void handle_mdcv(struct EbSvtAv1MasteringDisplayInfo *dst,
  119. const AVMasteringDisplayMetadata *mdcv)
  120. {
  121. if (mdcv->has_primaries) {
  122. const struct EbSvtAv1ChromaPoints *const points[] = {
  123. &dst->r,
  124. &dst->g,
  125. &dst->b,
  126. };
  127. for (int i = 0; i < 3; i++) {
  128. const struct EbSvtAv1ChromaPoints *dst = points[i];
  129. const AVRational *src = mdcv->display_primaries[i];
  130. AV_WB16(&dst->x,
  131. av_rescale_q(1, src[0], (AVRational){ 1, (1 << 16) }));
  132. AV_WB16(&dst->y,
  133. av_rescale_q(1, src[1], (AVRational){ 1, (1 << 16) }));
  134. }
  135. AV_WB16(&dst->white_point.x,
  136. av_rescale_q(1, mdcv->white_point[0],
  137. (AVRational){ 1, (1 << 16) }));
  138. AV_WB16(&dst->white_point.y,
  139. av_rescale_q(1, mdcv->white_point[1],
  140. (AVRational){ 1, (1 << 16) }));
  141. }
  142. if (mdcv->has_luminance) {
  143. AV_WB32(&dst->max_luma,
  144. av_rescale_q(1, mdcv->max_luminance,
  145. (AVRational){ 1, (1 << 8) }));
  146. AV_WB32(&dst->min_luma,
  147. av_rescale_q(1, mdcv->min_luminance,
  148. (AVRational){ 1, (1 << 14) }));
  149. }
  150. }
  151. static void handle_side_data(AVCodecContext *avctx,
  152. EbSvtAv1EncConfiguration *param)
  153. {
  154. const AVFrameSideData *cll_sd =
  155. av_frame_side_data_get(avctx->decoded_side_data,
  156. avctx->nb_decoded_side_data, AV_FRAME_DATA_CONTENT_LIGHT_LEVEL);
  157. const AVFrameSideData *mdcv_sd =
  158. av_frame_side_data_get(avctx->decoded_side_data,
  159. avctx->nb_decoded_side_data,
  160. AV_FRAME_DATA_MASTERING_DISPLAY_METADATA);
  161. if (cll_sd) {
  162. const AVContentLightMetadata *cll =
  163. (AVContentLightMetadata *)cll_sd->data;
  164. AV_WB16(&param->content_light_level.max_cll, cll->MaxCLL);
  165. AV_WB16(&param->content_light_level.max_fall, cll->MaxFALL);
  166. }
  167. if (mdcv_sd) {
  168. handle_mdcv(&param->mastering_display,
  169. (AVMasteringDisplayMetadata *)mdcv_sd->data);
  170. }
  171. }
  172. static int config_enc_params(EbSvtAv1EncConfiguration *param,
  173. AVCodecContext *avctx)
  174. {
  175. SvtContext *svt_enc = avctx->priv_data;
  176. const AVPixFmtDescriptor *desc;
  177. av_unused const AVDictionaryEntry *en = NULL;
  178. // Update param from options
  179. if (svt_enc->enc_mode >= -1)
  180. param->enc_mode = svt_enc->enc_mode;
  181. if (avctx->bit_rate) {
  182. param->target_bit_rate = avctx->bit_rate;
  183. if (avctx->rc_max_rate != avctx->bit_rate)
  184. param->rate_control_mode = 1;
  185. else
  186. param->rate_control_mode = 2;
  187. param->max_qp_allowed = avctx->qmax;
  188. param->min_qp_allowed = avctx->qmin;
  189. }
  190. param->max_bit_rate = avctx->rc_max_rate;
  191. if ((avctx->bit_rate > 0 || avctx->rc_max_rate > 0) && avctx->rc_buffer_size)
  192. param->maximum_buffer_size_ms =
  193. avctx->rc_buffer_size * 1000LL /
  194. FFMAX(avctx->bit_rate, avctx->rc_max_rate);
  195. if (svt_enc->crf > 0) {
  196. param->qp = svt_enc->crf;
  197. param->rate_control_mode = 0;
  198. } else if (svt_enc->qp > 0) {
  199. param->qp = svt_enc->qp;
  200. param->rate_control_mode = 0;
  201. param->enable_adaptive_quantization = 0;
  202. }
  203. desc = av_pix_fmt_desc_get(avctx->pix_fmt);
  204. param->color_primaries = avctx->color_primaries;
  205. param->matrix_coefficients = (desc->flags & AV_PIX_FMT_FLAG_RGB) ?
  206. AVCOL_SPC_RGB : avctx->colorspace;
  207. param->transfer_characteristics = avctx->color_trc;
  208. if (avctx->color_range != AVCOL_RANGE_UNSPECIFIED)
  209. param->color_range = avctx->color_range == AVCOL_RANGE_JPEG;
  210. else
  211. param->color_range = !!(desc->flags & AV_PIX_FMT_FLAG_RGB);
  212. #if SVT_AV1_CHECK_VERSION(1, 0, 0)
  213. if (avctx->chroma_sample_location != AVCHROMA_LOC_UNSPECIFIED) {
  214. const char *name =
  215. av_chroma_location_name(avctx->chroma_sample_location);
  216. switch (avctx->chroma_sample_location) {
  217. case AVCHROMA_LOC_LEFT:
  218. param->chroma_sample_position = EB_CSP_VERTICAL;
  219. break;
  220. case AVCHROMA_LOC_TOPLEFT:
  221. param->chroma_sample_position = EB_CSP_COLOCATED;
  222. break;
  223. default:
  224. if (!name)
  225. break;
  226. av_log(avctx, AV_LOG_WARNING,
  227. "Specified chroma sample location %s is unsupported "
  228. "on the AV1 bit stream level. Usage of a container that "
  229. "allows passing this information - such as Matroska - "
  230. "is recommended.\n",
  231. name);
  232. break;
  233. }
  234. }
  235. #endif
  236. if (avctx->profile != AV_PROFILE_UNKNOWN)
  237. param->profile = avctx->profile;
  238. if (avctx->level != AV_LEVEL_UNKNOWN)
  239. param->level = avctx->level;
  240. // gop_size == 1 case is handled when encoding each frame by setting
  241. // pic_type to EB_AV1_KEY_PICTURE. For gop_size > 1, set the
  242. // intra_period_length. Even though setting intra_period_length to 0 should
  243. // work in this case, it does not.
  244. // See: https://gitlab.com/AOMediaCodec/SVT-AV1/-/issues/2076
  245. if (avctx->gop_size > 1)
  246. param->intra_period_length = avctx->gop_size - 1;
  247. #if SVT_AV1_CHECK_VERSION(1, 1, 0)
  248. // In order for SVT-AV1 to force keyframes by setting pic_type to
  249. // EB_AV1_KEY_PICTURE on any frame, force_key_frames has to be set. Note
  250. // that this does not force all frames to be keyframes (it only forces a
  251. // keyframe with pic_type is set to EB_AV1_KEY_PICTURE). As of now, SVT-AV1
  252. // does not support arbitrary keyframe requests by setting pic_type to
  253. // EB_AV1_KEY_PICTURE, so it is done only when gop_size == 1.
  254. // FIXME: When SVT-AV1 supports arbitrary keyframe requests, this code needs
  255. // to be updated to set force_key_frames accordingly.
  256. if (avctx->gop_size == 1)
  257. param->force_key_frames = 1;
  258. #endif
  259. if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
  260. param->frame_rate_numerator = avctx->framerate.num;
  261. param->frame_rate_denominator = avctx->framerate.den;
  262. } else {
  263. param->frame_rate_numerator = avctx->time_base.den;
  264. param->frame_rate_denominator = avctx->time_base.num;
  265. }
  266. /* 2 = IDR, closed GOP, 1 = CRA, open GOP */
  267. param->intra_refresh_type = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ? 2 : 1;
  268. handle_side_data(avctx, param);
  269. #if SVT_AV1_CHECK_VERSION(0, 9, 1)
  270. while ((en = av_dict_iterate(svt_enc->svtav1_opts, en))) {
  271. EbErrorType ret = svt_av1_enc_parse_parameter(param, en->key, en->value);
  272. if (ret != EB_ErrorNone) {
  273. int level = (avctx->err_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
  274. av_log(avctx, level, "Error parsing option %s: %s.\n", en->key, en->value);
  275. if (avctx->err_recognition & AV_EF_EXPLODE)
  276. return AVERROR(EINVAL);
  277. }
  278. }
  279. #else
  280. if (av_dict_count(svt_enc->svtav1_opts)) {
  281. int level = (avctx->err_recognition & AV_EF_EXPLODE) ? AV_LOG_ERROR : AV_LOG_WARNING;
  282. av_log(avctx, level, "svt-params needs libavcodec to be compiled with SVT-AV1 "
  283. "headers >= 0.9.1.\n");
  284. if (avctx->err_recognition & AV_EF_EXPLODE)
  285. return AVERROR(ENOSYS);
  286. }
  287. #endif
  288. param->source_width = avctx->width;
  289. param->source_height = avctx->height;
  290. param->encoder_bit_depth = desc->comp[0].depth;
  291. if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1)
  292. param->encoder_color_format = EB_YUV420;
  293. else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0)
  294. param->encoder_color_format = EB_YUV422;
  295. else if (!desc->log2_chroma_w && !desc->log2_chroma_h)
  296. param->encoder_color_format = EB_YUV444;
  297. else {
  298. av_log(avctx, AV_LOG_ERROR , "Unsupported pixel format\n");
  299. return AVERROR(EINVAL);
  300. }
  301. if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
  302. && param->profile != AV_PROFILE_AV1_PROFESSIONAL ) {
  303. av_log(avctx, AV_LOG_WARNING, "Forcing Professional profile\n");
  304. param->profile = AV_PROFILE_AV1_PROFESSIONAL;
  305. } else if (param->encoder_color_format == EB_YUV444 && param->profile != AV_PROFILE_AV1_HIGH) {
  306. av_log(avctx, AV_LOG_WARNING, "Forcing High profile\n");
  307. param->profile = AV_PROFILE_AV1_HIGH;
  308. }
  309. avctx->bit_rate = param->rate_control_mode > 0 ?
  310. param->target_bit_rate : 0;
  311. avctx->rc_max_rate = param->max_bit_rate;
  312. avctx->rc_buffer_size = param->maximum_buffer_size_ms *
  313. FFMAX(avctx->bit_rate, avctx->rc_max_rate) / 1000LL;
  314. if (avctx->bit_rate || avctx->rc_max_rate || avctx->rc_buffer_size) {
  315. AVCPBProperties *cpb_props = ff_encode_add_cpb_side_data(avctx);
  316. if (!cpb_props)
  317. return AVERROR(ENOMEM);
  318. cpb_props->buffer_size = avctx->rc_buffer_size;
  319. cpb_props->max_bitrate = avctx->rc_max_rate;
  320. cpb_props->avg_bitrate = avctx->bit_rate;
  321. }
  322. return 0;
  323. }
  324. static int read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame,
  325. EbBufferHeaderType *header_ptr)
  326. {
  327. EbSvtIOFormat *in_data = (EbSvtIOFormat *)header_ptr->p_buffer;
  328. ptrdiff_t linesizes[4];
  329. size_t sizes[4];
  330. int bytes_shift = param->encoder_bit_depth > 8 ? 1 : 0;
  331. int ret, frame_size;
  332. for (int i = 0; i < 4; i++)
  333. linesizes[i] = frame->linesize[i];
  334. ret = av_image_fill_plane_sizes(sizes, frame->format, frame->height,
  335. linesizes);
  336. if (ret < 0)
  337. return ret;
  338. frame_size = 0;
  339. for (int i = 0; i < 4; i++) {
  340. if (sizes[i] > INT_MAX - frame_size)
  341. return AVERROR(EINVAL);
  342. frame_size += sizes[i];
  343. }
  344. in_data->luma = frame->data[0];
  345. in_data->cb = frame->data[1];
  346. in_data->cr = frame->data[2];
  347. in_data->y_stride = AV_CEIL_RSHIFT(frame->linesize[0], bytes_shift);
  348. in_data->cb_stride = AV_CEIL_RSHIFT(frame->linesize[1], bytes_shift);
  349. in_data->cr_stride = AV_CEIL_RSHIFT(frame->linesize[2], bytes_shift);
  350. header_ptr->n_filled_len = frame_size;
  351. svt_metadata_array_free(&header_ptr->metadata);
  352. return 0;
  353. }
  354. static av_cold int eb_enc_init(AVCodecContext *avctx)
  355. {
  356. SvtContext *svt_enc = avctx->priv_data;
  357. EbErrorType svt_ret;
  358. int ret;
  359. svt_enc->eos_flag = EOS_NOT_REACHED;
  360. #if SVT_AV1_CHECK_VERSION(3, 0, 0)
  361. svt_ret = svt_av1_enc_init_handle(&svt_enc->svt_handle, &svt_enc->enc_params);
  362. #else
  363. svt_ret = svt_av1_enc_init_handle(&svt_enc->svt_handle, svt_enc, &svt_enc->enc_params);
  364. #endif
  365. if (svt_ret != EB_ErrorNone) {
  366. return svt_print_error(avctx, svt_ret, "Error initializing encoder handle");
  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. svt_ret = svt_av1_enc_set_parameter(svt_enc->svt_handle, &svt_enc->enc_params);
  374. if (svt_ret != EB_ErrorNone) {
  375. return svt_print_error(avctx, svt_ret, "Error setting encoder parameters");
  376. }
  377. svt_ret = svt_av1_enc_init(svt_enc->svt_handle);
  378. if (svt_ret != EB_ErrorNone) {
  379. return svt_print_error(avctx, svt_ret, "Error initializing encoder");
  380. }
  381. svt_enc->dovi.logctx = avctx;
  382. ret = ff_dovi_configure(&svt_enc->dovi, avctx);
  383. if (ret < 0)
  384. return ret;
  385. if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
  386. EbBufferHeaderType *headerPtr = NULL;
  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. avctx->extradata_size = headerPtr->n_filled_len;
  392. avctx->extradata = av_mallocz(avctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
  393. if (!avctx->extradata) {
  394. av_log(avctx, AV_LOG_ERROR,
  395. "Cannot allocate AV1 header of size %d.\n", avctx->extradata_size);
  396. return AVERROR(ENOMEM);
  397. }
  398. memcpy(avctx->extradata, headerPtr->p_buffer, avctx->extradata_size);
  399. svt_ret = svt_av1_enc_stream_header_release(headerPtr);
  400. if (svt_ret != EB_ErrorNone) {
  401. return svt_print_error(avctx, svt_ret, "Error freeing stream header");
  402. }
  403. }
  404. svt_enc->frame = av_frame_alloc();
  405. if (!svt_enc->frame)
  406. return AVERROR(ENOMEM);
  407. return alloc_buffer(&svt_enc->enc_params, svt_enc);
  408. }
  409. static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
  410. {
  411. SvtContext *svt_enc = avctx->priv_data;
  412. EbBufferHeaderType *headerPtr = svt_enc->in_buf;
  413. AVFrameSideData *sd;
  414. EbErrorType svt_ret;
  415. int ret;
  416. if (!frame) {
  417. EbBufferHeaderType headerPtrLast;
  418. if (svt_enc->eos_flag == EOS_SENT)
  419. return 0;
  420. memset(&headerPtrLast, 0, sizeof(headerPtrLast));
  421. headerPtrLast.pic_type = EB_AV1_INVALID_PICTURE;
  422. headerPtrLast.flags = EB_BUFFERFLAG_EOS;
  423. svt_av1_enc_send_picture(svt_enc->svt_handle, &headerPtrLast);
  424. svt_enc->eos_flag = EOS_SENT;
  425. return 0;
  426. }
  427. ret = read_in_data(&svt_enc->enc_params, frame, headerPtr);
  428. if (ret < 0)
  429. return ret;
  430. headerPtr->flags = 0;
  431. headerPtr->p_app_private = NULL;
  432. headerPtr->pts = frame->pts;
  433. switch (frame->pict_type) {
  434. case AV_PICTURE_TYPE_I:
  435. headerPtr->pic_type = EB_AV1_KEY_PICTURE;
  436. break;
  437. default:
  438. // Actually means auto, or default.
  439. headerPtr->pic_type = EB_AV1_INVALID_PICTURE;
  440. break;
  441. }
  442. if (avctx->gop_size == 1)
  443. headerPtr->pic_type = EB_AV1_KEY_PICTURE;
  444. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DOVI_METADATA);
  445. if (svt_enc->dovi.cfg.dv_profile && sd) {
  446. const AVDOVIMetadata *metadata = (const AVDOVIMetadata *)sd->data;
  447. uint8_t *t35;
  448. int size;
  449. if ((ret = ff_dovi_rpu_generate(&svt_enc->dovi, metadata, FF_DOVI_WRAP_T35,
  450. &t35, &size)) < 0)
  451. return ret;
  452. ret = svt_add_metadata(headerPtr, EB_AV1_METADATA_TYPE_ITUT_T35, t35, size);
  453. av_free(t35);
  454. if (ret < 0)
  455. return AVERROR(ENOMEM);
  456. } else if (svt_enc->dovi.cfg.dv_profile) {
  457. av_log(avctx, AV_LOG_ERROR, "Dolby Vision enabled, but received frame "
  458. "without AV_FRAME_DATA_DOVI_METADATA\n");
  459. return AVERROR_INVALIDDATA;
  460. }
  461. svt_ret = svt_av1_enc_send_picture(svt_enc->svt_handle, headerPtr);
  462. if (svt_ret != EB_ErrorNone)
  463. return svt_print_error(avctx, svt_ret, "Error sending a frame to encoder");
  464. return 0;
  465. }
  466. static AVBufferRef *get_output_ref(AVCodecContext *avctx, SvtContext *svt_enc, int filled_len)
  467. {
  468. if (filled_len > svt_enc->max_tu_size) {
  469. const int max_frames = 8;
  470. int max_tu_size;
  471. if (filled_len > svt_enc->raw_size * max_frames) {
  472. av_log(avctx, AV_LOG_ERROR, "TU size > %d raw frame size.\n", max_frames);
  473. return NULL;
  474. }
  475. max_tu_size = 1 << av_ceil_log2(filled_len);
  476. av_buffer_pool_uninit(&svt_enc->pool);
  477. svt_enc->pool = av_buffer_pool_init(max_tu_size + AV_INPUT_BUFFER_PADDING_SIZE, NULL);
  478. if (!svt_enc->pool)
  479. return NULL;
  480. svt_enc->max_tu_size = max_tu_size;
  481. }
  482. av_assert0(svt_enc->pool);
  483. return av_buffer_pool_get(svt_enc->pool);
  484. }
  485. static int eb_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
  486. {
  487. SvtContext *svt_enc = avctx->priv_data;
  488. EbBufferHeaderType *headerPtr;
  489. AVFrame *frame = svt_enc->frame;
  490. EbErrorType svt_ret;
  491. AVBufferRef *ref;
  492. int ret = 0, pict_type;
  493. if (svt_enc->eos_flag == EOS_RECEIVED)
  494. return AVERROR_EOF;
  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. ret = eb_send_frame(avctx, frame);
  501. if (ret < 0)
  502. return ret;
  503. av_frame_unref(svt_enc->frame);
  504. svt_ret = svt_av1_enc_get_packet(svt_enc->svt_handle, &headerPtr, svt_enc->eos_flag);
  505. if (svt_ret == EB_NoErrorEmptyQueue)
  506. return AVERROR(EAGAIN);
  507. else if (svt_ret != EB_ErrorNone)
  508. return svt_print_error(avctx, svt_ret, "Error getting an output packet from encoder");
  509. #if SVT_AV1_CHECK_VERSION(2, 0, 0)
  510. if (headerPtr->flags & EB_BUFFERFLAG_EOS) {
  511. svt_enc->eos_flag = EOS_RECEIVED;
  512. svt_av1_enc_release_out_buffer(&headerPtr);
  513. return AVERROR_EOF;
  514. }
  515. #endif
  516. ref = get_output_ref(avctx, svt_enc, headerPtr->n_filled_len);
  517. if (!ref) {
  518. av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
  519. svt_av1_enc_release_out_buffer(&headerPtr);
  520. return AVERROR(ENOMEM);
  521. }
  522. pkt->buf = ref;
  523. pkt->data = ref->data;
  524. memcpy(pkt->data, headerPtr->p_buffer, headerPtr->n_filled_len);
  525. memset(pkt->data + headerPtr->n_filled_len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
  526. pkt->size = headerPtr->n_filled_len;
  527. pkt->pts = headerPtr->pts;
  528. pkt->dts = headerPtr->dts;
  529. switch (headerPtr->pic_type) {
  530. case EB_AV1_KEY_PICTURE:
  531. pkt->flags |= AV_PKT_FLAG_KEY;
  532. // fall-through
  533. case EB_AV1_INTRA_ONLY_PICTURE:
  534. pict_type = AV_PICTURE_TYPE_I;
  535. break;
  536. case EB_AV1_INVALID_PICTURE:
  537. pict_type = AV_PICTURE_TYPE_NONE;
  538. break;
  539. default:
  540. pict_type = AV_PICTURE_TYPE_P;
  541. break;
  542. }
  543. if (headerPtr->pic_type == EB_AV1_NON_REF_PICTURE)
  544. pkt->flags |= AV_PKT_FLAG_DISPOSABLE;
  545. #if !(SVT_AV1_CHECK_VERSION(2, 0, 0))
  546. if (headerPtr->flags & EB_BUFFERFLAG_EOS)
  547. svt_enc->eos_flag = EOS_RECEIVED;
  548. #endif
  549. ff_side_data_set_encoder_stats(pkt, headerPtr->qp * FF_QP2LAMBDA, NULL, 0, pict_type);
  550. svt_av1_enc_release_out_buffer(&headerPtr);
  551. return 0;
  552. }
  553. static av_cold int eb_enc_close(AVCodecContext *avctx)
  554. {
  555. SvtContext *svt_enc = avctx->priv_data;
  556. if (svt_enc->svt_handle) {
  557. svt_av1_enc_deinit(svt_enc->svt_handle);
  558. svt_av1_enc_deinit_handle(svt_enc->svt_handle);
  559. }
  560. if (svt_enc->in_buf) {
  561. av_free(svt_enc->in_buf->p_buffer);
  562. svt_metadata_array_free(&svt_enc->in_buf->metadata);
  563. av_freep(&svt_enc->in_buf);
  564. }
  565. av_buffer_pool_uninit(&svt_enc->pool);
  566. av_frame_free(&svt_enc->frame);
  567. ff_dovi_ctx_unref(&svt_enc->dovi);
  568. return 0;
  569. }
  570. #define OFFSET(x) offsetof(SvtContext, x)
  571. #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
  572. static const AVOption options[] = {
  573. { "preset", "Encoding preset",
  574. OFFSET(enc_mode), AV_OPT_TYPE_INT, { .i64 = -2 }, -2, MAX_ENC_PRESET, VE },
  575. FF_AV1_PROFILE_OPTS
  576. #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
  577. { .i64 = value }, 0, 0, VE, .unit = "avctx.level"
  578. { LEVEL("2.0", 20) },
  579. { LEVEL("2.1", 21) },
  580. { LEVEL("2.2", 22) },
  581. { LEVEL("2.3", 23) },
  582. { LEVEL("3.0", 30) },
  583. { LEVEL("3.1", 31) },
  584. { LEVEL("3.2", 32) },
  585. { LEVEL("3.3", 33) },
  586. { LEVEL("4.0", 40) },
  587. { LEVEL("4.1", 41) },
  588. { LEVEL("4.2", 42) },
  589. { LEVEL("4.3", 43) },
  590. { LEVEL("5.0", 50) },
  591. { LEVEL("5.1", 51) },
  592. { LEVEL("5.2", 52) },
  593. { LEVEL("5.3", 53) },
  594. { LEVEL("6.0", 60) },
  595. { LEVEL("6.1", 61) },
  596. { LEVEL("6.2", 62) },
  597. { LEVEL("6.3", 63) },
  598. { LEVEL("7.0", 70) },
  599. { LEVEL("7.1", 71) },
  600. { LEVEL("7.2", 72) },
  601. { LEVEL("7.3", 73) },
  602. #undef LEVEL
  603. { "crf", "Constant Rate Factor value", OFFSET(crf),
  604. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 63, VE },
  605. { "qp", "Initial Quantizer level value", OFFSET(qp),
  606. AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 63, VE },
  607. { "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 },
  608. { "dolbyvision", "Enable Dolby Vision RPU coding", OFFSET(dovi.enable), AV_OPT_TYPE_BOOL, {.i64 = FF_DOVI_AUTOMATIC }, -1, 1, VE, .unit = "dovi" },
  609. { "auto", NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_DOVI_AUTOMATIC}, .flags = VE, .unit = "dovi" },
  610. {NULL},
  611. };
  612. static const AVClass class = {
  613. .class_name = "libsvtav1",
  614. .item_name = av_default_item_name,
  615. .option = options,
  616. .version = LIBAVUTIL_VERSION_INT,
  617. };
  618. static const FFCodecDefault eb_enc_defaults[] = {
  619. { "b", "0" },
  620. { "flags", "+cgop" },
  621. { "g", "-1" },
  622. { "qmin", "1" },
  623. { "qmax", "63" },
  624. { NULL },
  625. };
  626. const FFCodec ff_libsvtav1_encoder = {
  627. .p.name = "libsvtav1",
  628. CODEC_LONG_NAME("SVT-AV1(Scalable Video Technology for AV1) encoder"),
  629. .priv_data_size = sizeof(SvtContext),
  630. .p.type = AVMEDIA_TYPE_VIDEO,
  631. .p.id = AV_CODEC_ID_AV1,
  632. .init = eb_enc_init,
  633. FF_CODEC_RECEIVE_PACKET_CB(eb_receive_packet),
  634. .close = eb_enc_close,
  635. .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS,
  636. .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
  637. FF_CODEC_CAP_AUTO_THREADS | FF_CODEC_CAP_INIT_CLEANUP,
  638. CODEC_PIXFMTS(AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10),
  639. .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG,
  640. .p.priv_class = &class,
  641. .defaults = eb_enc_defaults,
  642. .p.wrapper_name = "libsvtav1",
  643. };