FFmpeg
libsvtav1.c
Go to the documentation of this file.
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 "internal.h"
35 #include "encode.h"
36 #include "packet_internal.h"
37 #include "avcodec.h"
38 #include "profiles.h"
39 
40 typedef enum eos_status {
44 }EOS_STATUS;
45 
46 typedef struct SvtContext {
47  const AVClass *class;
48 
49  EbSvtAv1EncConfiguration enc_params;
50  EbComponentType *svt_handle;
51 
52  EbBufferHeaderType *in_buf;
53  int raw_size;
55 
57 
59 
60  EOS_STATUS eos_flag;
61 
62  // User options.
64  int la_depth;
65  int enc_mode;
66  int rc_mode;
67  int scd;
68  int qp;
69 
70  int tier;
71 
73  int tile_rows;
74 } SvtContext;
75 
76 static const struct {
77  EbErrorType eb_err;
78  int av_err;
79  const char *desc;
80 } svt_errors[] = {
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"},
89  { EB_ErrorCreateMutexFailed, AVERROR_EXTERNAL, "failed to create mutex" },
90  { EB_ErrorMutexUnresponsive, AVERROR_EXTERNAL, "mutex unresponsive" },
91  { EB_ErrorDestroyMutexFailed, AVERROR_EXTERNAL, "failed to destroy mutex" },
92  { EB_NoErrorEmptyQueue, AVERROR(EAGAIN), "empty queue" },
93 };
94 
95 static int svt_map_error(EbErrorType eb_err, const char **desc)
96 {
97  int i;
98 
100  for (i = 0; i < FF_ARRAY_ELEMS(svt_errors); i++) {
101  if (svt_errors[i].eb_err == eb_err) {
102  *desc = svt_errors[i].desc;
103  return svt_errors[i].av_err;
104  }
105  }
106  *desc = "unknown error";
107  return AVERROR_UNKNOWN;
108 }
109 
110 static int svt_print_error(void *log_ctx, EbErrorType err,
111  const char *error_string)
112 {
113  const char *desc;
114  int ret = svt_map_error(err, &desc);
115 
116  av_log(log_ctx, AV_LOG_ERROR, "%s: %s (0x%x)\n", error_string, desc, err);
117 
118  return ret;
119 }
120 
121 static int alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)
122 {
123  const int pack_mode_10bit =
124  (config->encoder_bit_depth > 8) && (config->compressed_ten_bit_format == 0) ? 1 : 0;
125  const size_t luma_size_8bit =
126  config->source_width * config->source_height * (1 << pack_mode_10bit);
127  const size_t luma_size_10bit =
128  (config->encoder_bit_depth > 8 && pack_mode_10bit == 0) ? luma_size_8bit : 0;
129 
130  EbSvtIOFormat *in_data;
131 
132  svt_enc->raw_size = (luma_size_8bit + luma_size_10bit) * 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 
149 static int config_enc_params(EbSvtAv1EncConfiguration *param,
150  AVCodecContext *avctx)
151 {
152  SvtContext *svt_enc = avctx->priv_data;
153  const AVPixFmtDescriptor *desc;
154 
155  param->source_width = avctx->width;
156  param->source_height = avctx->height;
157 
158  desc = av_pix_fmt_desc_get(avctx->pix_fmt);
159  param->encoder_bit_depth = desc->comp[0].depth;
160 
161  if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 1)
162  param->encoder_color_format = EB_YUV420;
163  else if (desc->log2_chroma_w == 1 && desc->log2_chroma_h == 0)
164  param->encoder_color_format = EB_YUV422;
165  else if (!desc->log2_chroma_w && !desc->log2_chroma_h)
166  param->encoder_color_format = EB_YUV444;
167  else {
168  av_log(avctx, AV_LOG_ERROR , "Unsupported pixel format\n");
169  return AVERROR(EINVAL);
170  }
171 
172  if (avctx->profile != FF_PROFILE_UNKNOWN)
173  param->profile = avctx->profile;
174 
175  if (avctx->level != FF_LEVEL_UNKNOWN)
176  param->level = avctx->level;
177 
178  if ((param->encoder_color_format == EB_YUV422 || param->encoder_bit_depth > 10)
179  && param->profile != FF_PROFILE_AV1_PROFESSIONAL ) {
180  av_log(avctx, AV_LOG_WARNING, "Forcing Professional profile\n");
181  param->profile = FF_PROFILE_AV1_PROFESSIONAL;
182  } else if (param->encoder_color_format == EB_YUV444 && param->profile != FF_PROFILE_AV1_HIGH) {
183  av_log(avctx, AV_LOG_WARNING, "Forcing High profile\n");
184  param->profile = FF_PROFILE_AV1_HIGH;
185  }
186 
187  // Update param from options
188  param->hierarchical_levels = svt_enc->hierarchical_level;
189  param->enc_mode = svt_enc->enc_mode;
190  param->tier = svt_enc->tier;
191  param->rate_control_mode = svt_enc->rc_mode;
192  param->scene_change_detection = svt_enc->scd;
193  param->qp = svt_enc->qp;
194 
195  param->target_bit_rate = avctx->bit_rate;
196 
197  if (avctx->gop_size > 0)
198  param->intra_period_length = avctx->gop_size - 1;
199 
200  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
201  param->frame_rate_numerator = avctx->framerate.num;
202  param->frame_rate_denominator = avctx->framerate.den;
203  } else {
204  param->frame_rate_numerator = avctx->time_base.den;
205  param->frame_rate_denominator = avctx->time_base.num * avctx->ticks_per_frame;
206  }
207 
208  param->enable_tpl_la = !!param->rate_control_mode;
209  if (param->rate_control_mode) {
210  param->max_qp_allowed = avctx->qmax;
211  param->min_qp_allowed = avctx->qmin;
212  }
213 
214  /* 2 = IDR, closed GOP, 1 = CRA, open GOP */
215  param->intra_refresh_type = avctx->flags & AV_CODEC_FLAG_CLOSED_GOP ? 2 : 1;
216 
217  if (svt_enc->la_depth >= 0)
218  param->look_ahead_distance = svt_enc->la_depth;
219 
220  param->tile_columns = svt_enc->tile_columns;
221  param->tile_rows = svt_enc->tile_rows;
222 
223  return 0;
224 }
225 
226 static int read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame,
227  EbBufferHeaderType *header_ptr)
228 {
229  EbSvtIOFormat *in_data = (EbSvtIOFormat *)header_ptr->p_buffer;
230  ptrdiff_t linesizes[4];
231  size_t sizes[4];
232  int bytes_shift = param->encoder_bit_depth > 8 ? 1 : 0;
233  int ret, frame_size;
234 
235  for (int i = 0; i < 4; i++)
236  linesizes[i] = frame->linesize[i];
237 
238  ret = av_image_fill_plane_sizes(sizes, frame->format, frame->height,
239  linesizes);
240  if (ret < 0)
241  return ret;
242 
243  frame_size = 0;
244  for (int i = 0; i < 4; i++) {
245  if (sizes[i] > INT_MAX - frame_size)
246  return AVERROR(EINVAL);
247  frame_size += sizes[i];
248  }
249 
250  in_data->luma = frame->data[0];
251  in_data->cb = frame->data[1];
252  in_data->cr = frame->data[2];
253 
254  in_data->y_stride = AV_CEIL_RSHIFT(frame->linesize[0], bytes_shift);
255  in_data->cb_stride = AV_CEIL_RSHIFT(frame->linesize[1], bytes_shift);
256  in_data->cr_stride = AV_CEIL_RSHIFT(frame->linesize[2], bytes_shift);
257 
258  header_ptr->n_filled_len = frame_size;
259 
260  return 0;
261 }
262 
264 {
265  SvtContext *svt_enc = avctx->priv_data;
266  EbErrorType svt_ret;
267  int ret;
268 
269  svt_enc->eos_flag = EOS_NOT_REACHED;
270 
271  svt_ret = svt_av1_enc_init_handle(&svt_enc->svt_handle, svt_enc, &svt_enc->enc_params);
272  if (svt_ret != EB_ErrorNone) {
273  return svt_print_error(avctx, svt_ret, "Error initializing encoder handle");
274  }
275 
276  ret = config_enc_params(&svt_enc->enc_params, avctx);
277  if (ret < 0) {
278  av_log(avctx, AV_LOG_ERROR, "Error configuring encoder parameters\n");
279  return ret;
280  }
281 
282  svt_ret = svt_av1_enc_set_parameter(svt_enc->svt_handle, &svt_enc->enc_params);
283  if (svt_ret != EB_ErrorNone) {
284  return svt_print_error(avctx, svt_ret, "Error setting encoder parameters");
285  }
286 
287  svt_ret = svt_av1_enc_init(svt_enc->svt_handle);
288  if (svt_ret != EB_ErrorNone) {
289  return svt_print_error(avctx, svt_ret, "Error initializing encoder");
290  }
291 
292  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
293  EbBufferHeaderType *headerPtr = NULL;
294 
295  svt_ret = svt_av1_enc_stream_header(svt_enc->svt_handle, &headerPtr);
296  if (svt_ret != EB_ErrorNone) {
297  return svt_print_error(avctx, svt_ret, "Error building stream header");
298  }
299 
300  avctx->extradata_size = headerPtr->n_filled_len;
302  if (!avctx->extradata) {
303  av_log(avctx, AV_LOG_ERROR,
304  "Cannot allocate AV1 header of size %d.\n", avctx->extradata_size);
305  return AVERROR(ENOMEM);
306  }
307 
308  memcpy(avctx->extradata, headerPtr->p_buffer, avctx->extradata_size);
309 
310  svt_ret = svt_av1_enc_stream_header_release(headerPtr);
311  if (svt_ret != EB_ErrorNone) {
312  return svt_print_error(avctx, svt_ret, "Error freeing stream header");
313  }
314  }
315 
316  svt_enc->frame = av_frame_alloc();
317  if (!svt_enc->frame)
318  return AVERROR(ENOMEM);
319 
320  return alloc_buffer(&svt_enc->enc_params, svt_enc);
321 }
322 
323 static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
324 {
325  SvtContext *svt_enc = avctx->priv_data;
326  EbBufferHeaderType *headerPtr = svt_enc->in_buf;
327  int ret;
328 
329  if (!frame) {
330  EbBufferHeaderType headerPtrLast;
331 
332  if (svt_enc->eos_flag == EOS_SENT)
333  return 0;
334 
335  headerPtrLast.n_alloc_len = 0;
336  headerPtrLast.n_filled_len = 0;
337  headerPtrLast.n_tick_count = 0;
338  headerPtrLast.p_app_private = NULL;
339  headerPtrLast.p_buffer = NULL;
340  headerPtrLast.flags = EB_BUFFERFLAG_EOS;
341 
342  svt_av1_enc_send_picture(svt_enc->svt_handle, &headerPtrLast);
343  svt_enc->eos_flag = EOS_SENT;
344  return 0;
345  }
346 
347  ret = read_in_data(&svt_enc->enc_params, frame, headerPtr);
348  if (ret < 0)
349  return ret;
350 
351  headerPtr->flags = 0;
352  headerPtr->p_app_private = NULL;
353  headerPtr->pts = frame->pts;
354 
355  svt_av1_enc_send_picture(svt_enc->svt_handle, headerPtr);
356 
357  return 0;
358 }
359 
360 static AVBufferRef *get_output_ref(AVCodecContext *avctx, SvtContext *svt_enc, int filled_len)
361 {
362  if (filled_len > svt_enc->max_tu_size) {
363  const int max_frames = 8;
364  int max_tu_size;
365 
366  if (filled_len > svt_enc->raw_size * max_frames) {
367  av_log(avctx, AV_LOG_ERROR, "TU size > %d raw frame size.\n", max_frames);
368  return NULL;
369  }
370 
371  max_tu_size = 1 << av_ceil_log2(filled_len);
372  av_buffer_pool_uninit(&svt_enc->pool);
373  svt_enc->pool = av_buffer_pool_init(max_tu_size + AV_INPUT_BUFFER_PADDING_SIZE, NULL);
374  if (!svt_enc->pool)
375  return NULL;
376 
377  svt_enc->max_tu_size = max_tu_size;
378  }
379  av_assert0(svt_enc->pool);
380 
381  return av_buffer_pool_get(svt_enc->pool);
382 }
383 
385 {
386  SvtContext *svt_enc = avctx->priv_data;
387  EbBufferHeaderType *headerPtr;
388  AVFrame *frame = svt_enc->frame;
389  EbErrorType svt_ret;
390  AVBufferRef *ref;
391  int ret = 0, pict_type;
392 
393  if (svt_enc->eos_flag == EOS_RECEIVED)
394  return AVERROR_EOF;
395 
396  ret = ff_encode_get_frame(avctx, frame);
397  if (ret < 0 && ret != AVERROR_EOF)
398  return ret;
399  if (ret == AVERROR_EOF)
400  frame = NULL;
401 
402  ret = eb_send_frame(avctx, frame);
403  if (ret < 0)
404  return ret;
405  av_frame_unref(svt_enc->frame);
406 
407  svt_ret = svt_av1_enc_get_packet(svt_enc->svt_handle, &headerPtr, svt_enc->eos_flag);
408  if (svt_ret == EB_NoErrorEmptyQueue)
409  return AVERROR(EAGAIN);
410 
411  ref = get_output_ref(avctx, svt_enc, headerPtr->n_filled_len);
412  if (!ref) {
413  av_log(avctx, AV_LOG_ERROR, "Failed to allocate output packet.\n");
414  svt_av1_enc_release_out_buffer(&headerPtr);
415  return AVERROR(ENOMEM);
416  }
417  pkt->buf = ref;
418  pkt->data = ref->data;
419 
420  memcpy(pkt->data, headerPtr->p_buffer, headerPtr->n_filled_len);
421  memset(pkt->data + headerPtr->n_filled_len, 0, AV_INPUT_BUFFER_PADDING_SIZE);
422 
423  pkt->size = headerPtr->n_filled_len;
424  pkt->pts = headerPtr->pts;
425  pkt->dts = headerPtr->dts;
426 
427  switch (headerPtr->pic_type) {
428  case EB_AV1_KEY_PICTURE:
430  // fall-through
431  case EB_AV1_INTRA_ONLY_PICTURE:
432  pict_type = AV_PICTURE_TYPE_I;
433  break;
434  case EB_AV1_INVALID_PICTURE:
435  pict_type = AV_PICTURE_TYPE_NONE;
436  break;
437  default:
438  pict_type = AV_PICTURE_TYPE_P;
439  break;
440  }
441 
442  if (headerPtr->pic_type == EB_AV1_NON_REF_PICTURE)
444 
445  if (headerPtr->flags & EB_BUFFERFLAG_EOS)
446  svt_enc->eos_flag = EOS_RECEIVED;
447 
448  ff_side_data_set_encoder_stats(pkt, headerPtr->qp * FF_QP2LAMBDA, NULL, 0, pict_type);
449 
450  svt_av1_enc_release_out_buffer(&headerPtr);
451 
452  return 0;
453 }
454 
456 {
457  SvtContext *svt_enc = avctx->priv_data;
458 
459  if (svt_enc->svt_handle) {
460  svt_av1_enc_deinit(svt_enc->svt_handle);
461  svt_av1_enc_deinit_handle(svt_enc->svt_handle);
462  }
463  if (svt_enc->in_buf) {
464  av_free(svt_enc->in_buf->p_buffer);
465  av_freep(&svt_enc->in_buf);
466  }
467 
468  av_buffer_pool_uninit(&svt_enc->pool);
469  av_frame_free(&svt_enc->frame);
470 
471  return 0;
472 }
473 
474 #define OFFSET(x) offsetof(SvtContext, x)
475 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
476 static const AVOption options[] = {
477  { "hielevel", "Hierarchical prediction levels setting", OFFSET(hierarchical_level),
478  AV_OPT_TYPE_INT, { .i64 = 4 }, 3, 4, VE , "hielevel"},
479  { "3level", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 3 }, INT_MIN, INT_MAX, VE, "hielevel" },
480  { "4level", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 4 }, INT_MIN, INT_MAX, VE, "hielevel" },
481 
482  { "la_depth", "Look ahead distance [0, 120]", OFFSET(la_depth),
483  AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 120, VE },
484 
485  { "preset", "Encoding preset [0, 8]",
486  OFFSET(enc_mode), AV_OPT_TYPE_INT, { .i64 = MAX_ENC_PRESET }, 0, MAX_ENC_PRESET, VE },
487 
488  { "tier", "Set operating point tier", OFFSET(tier),
489  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE, "tier" },
490  { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, 0, 0, VE, "tier" },
491  { "high", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, 0, 0, VE, "tier" },
492 
494 
495 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
496  { .i64 = value }, 0, 0, VE, "avctx.level"
497  { LEVEL("2.0", 20) },
498  { LEVEL("2.1", 21) },
499  { LEVEL("2.2", 22) },
500  { LEVEL("2.3", 23) },
501  { LEVEL("3.0", 30) },
502  { LEVEL("3.1", 31) },
503  { LEVEL("3.2", 32) },
504  { LEVEL("3.3", 33) },
505  { LEVEL("4.0", 40) },
506  { LEVEL("4.1", 41) },
507  { LEVEL("4.2", 42) },
508  { LEVEL("4.3", 43) },
509  { LEVEL("5.0", 50) },
510  { LEVEL("5.1", 51) },
511  { LEVEL("5.2", 52) },
512  { LEVEL("5.3", 53) },
513  { LEVEL("6.0", 60) },
514  { LEVEL("6.1", 61) },
515  { LEVEL("6.2", 62) },
516  { LEVEL("6.3", 63) },
517  { LEVEL("7.0", 70) },
518  { LEVEL("7.1", 71) },
519  { LEVEL("7.2", 72) },
520  { LEVEL("7.3", 73) },
521 #undef LEVEL
522 
523  { "rc", "Bit rate control mode", OFFSET(rc_mode),
524  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 2, VE , "rc"},
525  { "cqp", "Constant quantizer", 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "rc" },
526  { "vbr", "Variable Bit Rate, use a target bitrate for the entire stream", 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "rc" },
527  { "cvbr", "Constrained Variable Bit Rate, use a target bitrate for each GOP", 0, AV_OPT_TYPE_CONST,{ .i64 = 2 }, INT_MIN, INT_MAX, VE, "rc" },
528 
529  { "qp", "Quantizer to use with cqp rate control mode", OFFSET(qp),
530  AV_OPT_TYPE_INT, { .i64 = 50 }, 0, 63, VE },
531 
532  { "sc_detection", "Scene change detection", OFFSET(scd),
533  AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE },
534 
535  { "tile_columns", "Log2 of number of tile columns to use", OFFSET(tile_columns), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 4, VE},
536  { "tile_rows", "Log2 of number of tile rows to use", OFFSET(tile_rows), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 6, VE},
537 
538  {NULL},
539 };
540 
541 static const AVClass class = {
542  .class_name = "libsvtav1",
543  .item_name = av_default_item_name,
544  .option = options,
546 };
547 
548 static const AVCodecDefault eb_enc_defaults[] = {
549  { "b", "7M" },
550  { "flags", "+cgop" },
551  { "g", "-1" },
552  { "qmin", "0" },
553  { "qmax", "63" },
554  { NULL },
555 };
556 
558  .name = "libsvtav1",
559  .long_name = NULL_IF_CONFIG_SMALL("SVT-AV1(Scalable Video Technology for AV1) encoder"),
560  .priv_data_size = sizeof(SvtContext),
562  .id = AV_CODEC_ID_AV1,
563  .init = eb_enc_init,
564  .receive_packet = eb_receive_packet,
565  .close = eb_enc_close,
568  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P,
570  AV_PIX_FMT_NONE },
571  .priv_class = &class,
572  .defaults = eb_enc_defaults,
573  .wrapper_name = "libsvtav1",
574 };
AVCodec
AVCodec.
Definition: codec.h:202
av_buffer_pool_init
AVBufferPool * av_buffer_pool_init(size_t size, AVBufferRef *(*alloc)(size_t size))
Allocate and initialize a buffer pool.
Definition: buffer.c:280
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
get_output_ref
static AVBufferRef * get_output_ref(AVCodecContext *avctx, SvtContext *svt_enc, int filled_len)
Definition: libsvtav1.c:360
AVBufferPool
The buffer pool.
Definition: buffer_internal.h:89
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2564
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
ff_side_data_set_encoder_stats
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
Definition: avpacket.c:601
SvtContext
Definition: libsvtav1.c:46
FF_AV1_PROFILE_OPTS
#define FF_AV1_PROFILE_OPTS
Definition: profiles.h:54
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:112
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
pixdesc.h
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
encode.h
SvtContext::frame
AVFrame * frame
Definition: libsvtav1.c:56
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:391
eb_receive_packet
static int eb_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: libsvtav1.c:384
eb_enc_init
static av_cold int eb_enc_init(AVCodecContext *avctx)
Definition: libsvtav1.c:263
FF_LEVEL_UNKNOWN
#define FF_LEVEL_UNKNOWN
Definition: avcodec.h:1652
eb_enc_close
static av_cold int eb_enc_close(AVCodecContext *avctx)
Definition: libsvtav1.c:455
AV_PKT_FLAG_DISPOSABLE
#define AV_PKT_FLAG_DISPOSABLE
Flag is used to indicate packets that contain frames that can be discarded by the decoder.
Definition: packet.h:444
AVERROR_UNKNOWN
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:73
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1165
tf_sess_config.config
config
Definition: tf_sess_config.py:33
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:425
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:268
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1710
eb_enc_defaults
static const AVCodecDefault eb_enc_defaults[]
Definition: libsvtav1.c:548
av_ceil_log2
#define av_ceil_log2
Definition: common.h:93
eb_err
EbErrorType eb_err
Definition: libsvtav1.c:77
eb_send_frame
static int eb_send_frame(AVCodecContext *avctx, const AVFrame *frame)
Definition: libsvtav1.c:323
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:463
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVRational::num
int num
Numerator.
Definition: rational.h:59
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
SvtContext::tile_rows
int tile_rows
Definition: libsvtav1.c:73
avassert.h
EOS_RECEIVED
@ EOS_RECEIVED
Definition: libsvtav1.c:43
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
FF_ARRAY_ELEMS
#define FF_ARRAY_ELEMS(a)
Definition: sinewin_tablegen.c:29
av_cold
#define av_cold
Definition: attributes.h:90
av_buffer_pool_get
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:387
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:485
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:51
frame_size
int frame_size
Definition: mxfenc.c:2205
AVCodecContext::ticks_per_frame
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:515
AV_CODEC_CAP_OTHER_THREADS
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:127
tile_rows
int tile_rows
Definition: h265_levels.c:217
FF_PROFILE_UNKNOWN
#define FF_PROFILE_UNKNOWN
Definition: avcodec.h:1526
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:290
SvtContext::enc_mode
int enc_mode
Definition: libsvtav1.c:65
SvtContext::hierarchical_level
int hierarchical_level
Definition: libsvtav1.c:63
AV_PIX_FMT_YUV420P
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
AVCodecDefault
Definition: internal.h:211
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:356
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:53
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:279
av_image_fill_plane_sizes
int av_image_fill_plane_sizes(size_t sizes[4], enum AVPixelFormat pix_fmt, int height, const ptrdiff_t linesizes[4])
Fill plane sizes for an image with pixel format pix_fmt and height height.
Definition: imgutils.c:111
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:433
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
profiles.h
av_buffer_pool_uninit
void av_buffer_pool_uninit(AVBufferPool **ppool)
Mark the pool as being available for freeing.
Definition: buffer.c:322
SvtContext::pool
AVBufferPool * pool
Definition: libsvtav1.c:58
AVCodecContext::level
int level
level
Definition: avcodec.h:1651
LEVEL
#define LEVEL(name, value)
SvtContext::tile_columns
int tile_columns
Definition: libsvtav1.c:72
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
SvtContext::scd
int scd
Definition: libsvtav1.c:67
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:506
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: internal.h:81
AVPacket::size
int size
Definition: packet.h:374
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:578
EOS_NOT_REACHED
@ EOS_NOT_REACHED
Definition: libsvtav1.c:41
config_enc_params
static int config_enc_params(EbSvtAv1EncConfiguration *param, AVCodecContext *avctx)
Definition: libsvtav1.c:149
SvtContext::svt_handle
EbComponentType * svt_handle
Definition: libsvtav1.c:50
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
alloc_buffer
static int alloc_buffer(EbSvtAv1EncConfiguration *config, SvtContext *svt_enc)
Definition: libsvtav1.c:121
frame.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
options
static const AVOption options[]
Definition: libsvtav1.c:476
VE
#define VE
Definition: libsvtav1.c:475
AVERROR_EXTERNAL
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:59
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:379
svt_map_error
static int svt_map_error(EbErrorType eb_err, const char **desc)
Definition: libsvtav1.c:95
SvtContext::eos_flag
EOS_STATUS eos_flag
Definition: libsvtav1.c:60
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
eos_status
eos_status
Definition: libsvtav1.c:40
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:484
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:50
OFFSET
#define OFFSET(x)
Definition: libsvtav1.c:474
av_err
int av_err
Definition: libsvtav1.c:78
SvtContext::max_tu_size
int max_tu_size
Definition: libsvtav1.c:54
common.h
SvtContext::raw_size
int raw_size
Definition: libsvtav1.c:53
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:437
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:263
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:209
AVCodecContext::height
int height
Definition: avcodec.h:556
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:593
SvtContext::enc_params
EbSvtAv1EncConfiguration enc_params
Definition: libsvtav1.c:49
FF_PROFILE_AV1_PROFESSIONAL
#define FF_PROFILE_AV1_PROFESSIONAL
Definition: avcodec.h:1623
avcodec.h
AV_CODEC_FLAG_CLOSED_GOP
#define AV_CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:282
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
svt_print_error
static int svt_print_error(void *log_ctx, EbErrorType err, const char *error_string)
Definition: libsvtav1.c:110
AVCodecContext
main external API structure.
Definition: avcodec.h:383
SvtContext::la_depth
int la_depth
Definition: libsvtav1.c:64
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1158
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1525
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:82
SvtContext::in_buf
EbBufferHeaderType * in_buf
Definition: libsvtav1.c:52
desc
const char * desc
Definition: libsvtav1.c:79
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:275
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
ff_encode_get_frame
int ff_encode_get_frame(AVCodecContext *avctx, AVFrame *frame)
Called by encoders to get the next frame for encoding.
Definition: encode.c:157
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
packet_internal.h
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
SvtContext::tier
int tier
Definition: libsvtav1.c:70
SvtContext::rc_mode
int rc_mode
Definition: libsvtav1.c:66
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:410
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:241
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
ff_libsvtav1_encoder
const AVCodec ff_libsvtav1_encoder
Definition: libsvtav1.c:557
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:556
svt_errors
static const struct @85 svt_errors[]
EOS_SENT
@ EOS_SENT
Definition: libsvtav1.c:42
imgutils.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
SvtContext::qp
int qp
Definition: libsvtav1.c:68
FF_PROFILE_AV1_HIGH
#define FF_PROFILE_AV1_HIGH
Definition: avcodec.h:1622
FF_QP2LAMBDA
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
Definition: avutil.h:227
rc_mode
mfxU16 rc_mode
Definition: qsvenc.c:84
read_in_data
static int read_in_data(EbSvtAv1EncConfiguration *param, const AVFrame *frame, EbBufferHeaderType *header_ptr)
Definition: libsvtav1.c:226
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:233