FFmpeg
librav1e.c
Go to the documentation of this file.
1 /*
2  * librav1e encoder
3  *
4  * Copyright (c) 2019 Derek Buitenhuis
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 FFmpeg; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #include <rav1e.h>
24 
25 #include "libavutil/buffer.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/avassert.h"
28 #include "libavutil/base64.h"
29 #include "libavutil/common.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "avcodec.h"
35 #include "codec_internal.h"
36 #include "encode.h"
37 #include "internal.h"
38 
39 typedef struct librav1eContext {
40  const AVClass *class;
41 
42  RaContext *ctx;
44  RaFrame *rframe;
45 
46  uint8_t *pass_data;
47  size_t pass_pos;
48  int pass_size;
49 
51  int quantizer;
52  int speed;
53  int tiles;
54  int tile_rows;
55  int tile_cols;
57 
58 typedef struct FrameData {
59  int64_t pts;
61 
62  void *frame_opaque;
64 } FrameData;
65 
66 static inline RaPixelRange range_map(enum AVPixelFormat pix_fmt, enum AVColorRange range)
67 {
68  switch (pix_fmt) {
72  return RA_PIXEL_RANGE_FULL;
73  }
74 
75  switch (range) {
76  case AVCOL_RANGE_JPEG:
77  return RA_PIXEL_RANGE_FULL;
78  case AVCOL_RANGE_MPEG:
79  default:
80  return RA_PIXEL_RANGE_LIMITED;
81  }
82 }
83 
84 static inline RaChromaSampling pix_fmt_map(enum AVPixelFormat pix_fmt)
85 {
86  switch (pix_fmt) {
87  case AV_PIX_FMT_YUV420P:
91  return RA_CHROMA_SAMPLING_CS420;
92  case AV_PIX_FMT_YUV422P:
96  return RA_CHROMA_SAMPLING_CS422;
97  case AV_PIX_FMT_YUV444P:
101  return RA_CHROMA_SAMPLING_CS444;
102  default:
103  av_assert0(0);
104  }
105 }
106 
107 static inline RaChromaSamplePosition chroma_loc_map(enum AVChromaLocation chroma_loc)
108 {
109  switch (chroma_loc) {
110  case AVCHROMA_LOC_LEFT:
111  return RA_CHROMA_SAMPLE_POSITION_VERTICAL;
113  return RA_CHROMA_SAMPLE_POSITION_COLOCATED;
114  default:
115  return RA_CHROMA_SAMPLE_POSITION_UNKNOWN;
116  }
117 }
118 
119 static int get_stats(AVCodecContext *avctx, int eos)
120 {
121  librav1eContext *ctx = avctx->priv_data;
122  RaData* buf = rav1e_twopass_out(ctx->ctx);
123  if (!buf)
124  return 0;
125 
126  if (!eos) {
127  uint8_t *tmp = av_fast_realloc(ctx->pass_data, &ctx->pass_size,
128  ctx->pass_pos + buf->len);
129  if (!tmp) {
130  rav1e_data_unref(buf);
131  return AVERROR(ENOMEM);
132  }
133 
134  ctx->pass_data = tmp;
135  memcpy(ctx->pass_data + ctx->pass_pos, buf->data, buf->len);
136  ctx->pass_pos += buf->len;
137  } else {
138  size_t b64_size = AV_BASE64_SIZE(ctx->pass_pos);
139 
140  memcpy(ctx->pass_data, buf->data, buf->len);
141 
142  avctx->stats_out = av_malloc(b64_size);
143  if (!avctx->stats_out) {
144  rav1e_data_unref(buf);
145  return AVERROR(ENOMEM);
146  }
147 
148  av_base64_encode(avctx->stats_out, b64_size, ctx->pass_data, ctx->pass_pos);
149 
150  av_freep(&ctx->pass_data);
151  }
152 
153  rav1e_data_unref(buf);
154 
155  return 0;
156 }
157 
158 static int set_stats(AVCodecContext *avctx)
159 {
160  librav1eContext *ctx = avctx->priv_data;
161  int ret = 1;
162 
163  while (ret > 0 && ctx->pass_size - ctx->pass_pos > 0) {
164  ret = rav1e_twopass_in(ctx->ctx, ctx->pass_data + ctx->pass_pos, ctx->pass_size);
165  if (ret < 0)
166  return AVERROR_EXTERNAL;
167  ctx->pass_pos += ret;
168  }
169 
170  return 0;
171 }
172 
174 {
175  librav1eContext *ctx = avctx->priv_data;
176 
177  if (ctx->ctx) {
178  rav1e_context_unref(ctx->ctx);
179  ctx->ctx = NULL;
180  }
181  if (ctx->rframe) {
182  rav1e_frame_unref(ctx->rframe);
183  ctx->rframe = NULL;
184  }
185 
186  av_frame_free(&ctx->frame);
187  av_freep(&ctx->pass_data);
188 
189  return 0;
190 }
191 
193 {
194  librav1eContext *ctx = avctx->priv_data;
196  RaConfig *cfg = NULL;
197  int rret;
198  int ret = 0;
199 
200  ctx->frame = av_frame_alloc();
201  if (!ctx->frame)
202  return AVERROR(ENOMEM);
203 
204  cfg = rav1e_config_default();
205  if (!cfg) {
206  av_log(avctx, AV_LOG_ERROR, "Could not allocate rav1e config.\n");
207  return AVERROR_EXTERNAL;
208  }
209 
210  /*
211  * Rav1e currently uses the time base given to it only for ratecontrol... where
212  * the inverse is taken and used as a framerate. So, do what we do in other wrappers
213  * and use the framerate if we can.
214  */
215  if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
216  rav1e_config_set_time_base(cfg, (RaRational) {
217  avctx->framerate.den, avctx->framerate.num
218  });
219  } else {
220  rav1e_config_set_time_base(cfg, (RaRational) {
221  avctx->time_base.num, avctx->time_base.den
222  });
223  }
224 
225  if ((avctx->flags & AV_CODEC_FLAG_PASS1 || avctx->flags & AV_CODEC_FLAG_PASS2) && !avctx->bit_rate) {
226  av_log(avctx, AV_LOG_ERROR, "A bitrate must be set to use two pass mode.\n");
228  goto end;
229  }
230 
231  if (avctx->flags & AV_CODEC_FLAG_PASS2) {
232  if (!avctx->stats_in) {
233  av_log(avctx, AV_LOG_ERROR, "No stats file provided for second pass.\n");
234  ret = AVERROR(EINVAL);
235  goto end;
236  }
237 
238  ctx->pass_size = (strlen(avctx->stats_in) * 3) / 4;
239  ctx->pass_data = av_malloc(ctx->pass_size);
240  if (!ctx->pass_data) {
241  av_log(avctx, AV_LOG_ERROR, "Could not allocate stats buffer.\n");
242  ret = AVERROR(ENOMEM);
243  goto end;
244  }
245 
246  ctx->pass_size = av_base64_decode(ctx->pass_data, avctx->stats_in, ctx->pass_size);
247  if (ctx->pass_size < 0) {
248  av_log(avctx, AV_LOG_ERROR, "Invalid pass file.\n");
249  ret = AVERROR(EINVAL);
250  goto end;
251  }
252  }
253 
254  {
255  const AVDictionaryEntry *en = NULL;
256  while ((en = av_dict_iterate(ctx->rav1e_opts, en))) {
257  if (rav1e_config_parse(cfg, en->key, en->value) < 0)
258  av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s.\n", en->key, en->value);
259  }
260  }
261 
262  rret = rav1e_config_parse_int(cfg, "width", avctx->width);
263  if (rret < 0) {
264  av_log(avctx, AV_LOG_ERROR, "Invalid width passed to rav1e.\n");
266  goto end;
267  }
268 
269  rret = rav1e_config_parse_int(cfg, "height", avctx->height);
270  if (rret < 0) {
271  av_log(avctx, AV_LOG_ERROR, "Invalid height passed to rav1e.\n");
273  goto end;
274  }
275 
276  if (avctx->sample_aspect_ratio.num > 0 && avctx->sample_aspect_ratio.den > 0)
277  rav1e_config_set_sample_aspect_ratio(cfg, (RaRational) {
278  avctx->sample_aspect_ratio.num,
279  avctx->sample_aspect_ratio.den
280  });
281 
282  rret = rav1e_config_parse_int(cfg, "threads", avctx->thread_count);
283  if (rret < 0)
284  av_log(avctx, AV_LOG_WARNING, "Invalid number of threads, defaulting to auto.\n");
285 
286  if (ctx->speed >= 0) {
287  rret = rav1e_config_parse_int(cfg, "speed", ctx->speed);
288  if (rret < 0) {
289  av_log(avctx, AV_LOG_ERROR, "Could not set speed preset.\n");
291  goto end;
292  }
293  }
294 
295  /* rav1e handles precedence between 'tiles' and cols/rows for us. */
296  if (ctx->tiles > 0) {
297  rret = rav1e_config_parse_int(cfg, "tiles", ctx->tiles);
298  if (rret < 0) {
299  av_log(avctx, AV_LOG_ERROR, "Could not set number of tiles to encode with.\n");
301  goto end;
302  }
303  }
304  if (ctx->tile_rows > 0) {
305  rret = rav1e_config_parse_int(cfg, "tile_rows", ctx->tile_rows);
306  if (rret < 0) {
307  av_log(avctx, AV_LOG_ERROR, "Could not set number of tile rows to encode with.\n");
309  goto end;
310  }
311  }
312  if (ctx->tile_cols > 0) {
313  rret = rav1e_config_parse_int(cfg, "tile_cols", ctx->tile_cols);
314  if (rret < 0) {
315  av_log(avctx, AV_LOG_ERROR, "Could not set number of tile cols to encode with.\n");
317  goto end;
318  }
319  }
320 
321  if (avctx->gop_size > 0) {
322  rret = rav1e_config_parse_int(cfg, "key_frame_interval", avctx->gop_size);
323  if (rret < 0) {
324  av_log(avctx, AV_LOG_ERROR, "Could not set max keyint.\n");
326  goto end;
327  }
328  }
329 
330  if (avctx->keyint_min > 0) {
331  rret = rav1e_config_parse_int(cfg, "min_key_frame_interval", avctx->keyint_min);
332  if (rret < 0) {
333  av_log(avctx, AV_LOG_ERROR, "Could not set min keyint.\n");
335  goto end;
336  }
337  }
338 
339  if (avctx->bit_rate && ctx->quantizer < 0) {
340  int max_quantizer = avctx->qmax >= 0 ? avctx->qmax : 255;
341 
342  rret = rav1e_config_parse_int(cfg, "quantizer", max_quantizer);
343  if (rret < 0) {
344  av_log(avctx, AV_LOG_ERROR, "Could not set max quantizer.\n");
346  goto end;
347  }
348 
349  if (avctx->qmin >= 0) {
350  rret = rav1e_config_parse_int(cfg, "min_quantizer", avctx->qmin);
351  if (rret < 0) {
352  av_log(avctx, AV_LOG_ERROR, "Could not set min quantizer.\n");
354  goto end;
355  }
356  }
357 
358  rret = rav1e_config_parse_int(cfg, "bitrate", avctx->bit_rate);
359  if (rret < 0) {
360  av_log(avctx, AV_LOG_ERROR, "Could not set bitrate.\n");
362  goto end;
363  }
364  } else if (ctx->quantizer >= 0) {
365  if (avctx->bit_rate)
366  av_log(avctx, AV_LOG_WARNING, "Both bitrate and quantizer specified. Using quantizer mode.");
367 
368  rret = rav1e_config_parse_int(cfg, "quantizer", ctx->quantizer);
369  if (rret < 0) {
370  av_log(avctx, AV_LOG_ERROR, "Could not set quantizer.\n");
372  goto end;
373  }
374  }
375 
376  rret = rav1e_config_set_pixel_format(cfg, desc->comp[0].depth,
377  pix_fmt_map(avctx->pix_fmt),
379  range_map(avctx->pix_fmt, avctx->color_range));
380  if (rret < 0) {
381  av_log(avctx, AV_LOG_ERROR, "Failed to set pixel format properties.\n");
383  goto end;
384  }
385 
386  /* rav1e's colorspace enums match standard values. */
387  rret = rav1e_config_set_color_description(cfg, (RaMatrixCoefficients) avctx->colorspace,
388  (RaColorPrimaries) avctx->color_primaries,
389  (RaTransferCharacteristics) avctx->color_trc);
390  if (rret < 0) {
391  av_log(avctx, AV_LOG_WARNING, "Failed to set color properties.\n");
392  if (avctx->err_recognition & AV_EF_EXPLODE) {
394  goto end;
395  }
396  }
397 
398  ctx->ctx = rav1e_context_new(cfg);
399  if (!ctx->ctx) {
400  av_log(avctx, AV_LOG_ERROR, "Failed to create rav1e encode context.\n");
402  goto end;
403  }
404 
405  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER) {
406  RaData *seq_hdr = rav1e_container_sequence_header(ctx->ctx);
407 
408  if (seq_hdr)
409  avctx->extradata = av_mallocz(seq_hdr->len + AV_INPUT_BUFFER_PADDING_SIZE);
410  if (!seq_hdr || !avctx->extradata) {
411  rav1e_data_unref(seq_hdr);
412  av_log(avctx, AV_LOG_ERROR, "Failed to get extradata.\n");
413  ret = seq_hdr ? AVERROR(ENOMEM) : AVERROR_EXTERNAL;
414  goto end;
415  }
416 
417  memcpy(avctx->extradata, seq_hdr->data, seq_hdr->len);
418  avctx->extradata_size = seq_hdr->len;
419  rav1e_data_unref(seq_hdr);
420  }
421 
422  ret = 0;
423 
424 end:
425 
426  rav1e_config_unref(cfg);
427 
428  return ret;
429 }
430 
431 static void frame_data_free(void *data)
432 {
433  FrameData *fd = data;
434 
435  if (!fd)
436  return;
437 
439  av_free(data);
440 }
441 
443 {
444  librav1eContext *ctx = avctx->priv_data;
445  RaFrame *rframe = ctx->rframe;
446  RaPacket *rpkt = NULL;
447  FrameData *fd;
448  int ret;
449 
450  if (!rframe) {
451  AVFrame *frame = ctx->frame;
452 
453  ret = ff_encode_get_frame(avctx, frame);
454  if (ret < 0 && ret != AVERROR_EOF)
455  return ret;
456 
457  if (frame->buf[0]) {
459 
460  fd = av_mallocz(sizeof(*fd));
461  if (!fd) {
462  av_log(avctx, AV_LOG_ERROR, "Could not allocate PTS buffer.\n");
463  return AVERROR(ENOMEM);
464  }
465  fd->pts = frame->pts;
466  fd->duration = frame->duration;
467 
468  if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
469  fd->frame_opaque = frame->opaque;
470  fd->frame_opaque_ref = frame->opaque_ref;
471  frame->opaque_ref = NULL;
472  }
473 
474  rframe = rav1e_frame_new(ctx->ctx);
475  if (!rframe) {
476  av_log(avctx, AV_LOG_ERROR, "Could not allocate new rav1e frame.\n");
478  frame_data_free(fd);
479  return AVERROR(ENOMEM);
480  }
481 
482  for (int i = 0; i < desc->nb_components; i++) {
483  int shift = i ? desc->log2_chroma_h : 0;
484  int bytes = desc->comp[0].depth == 8 ? 1 : 2;
485  rav1e_frame_fill_plane(rframe, i, frame->data[i],
486  (frame->height >> shift) * frame->linesize[i],
487  frame->linesize[i], bytes);
488  }
490  rav1e_frame_set_opaque(rframe, fd, frame_data_free);
491  }
492  }
493 
494  ret = rav1e_send_frame(ctx->ctx, rframe);
495  if (rframe)
496  if (ret == RA_ENCODER_STATUS_ENOUGH_DATA) {
497  ctx->rframe = rframe; /* Queue is full. Store the RaFrame to retry next call */
498  } else {
499  rav1e_frame_unref(rframe); /* No need to unref if flushing. */
500  ctx->rframe = NULL;
501  }
502 
503  switch (ret) {
504  case RA_ENCODER_STATUS_SUCCESS:
505  case RA_ENCODER_STATUS_ENOUGH_DATA:
506  break;
507  case RA_ENCODER_STATUS_FAILURE:
508  av_log(avctx, AV_LOG_ERROR, "Could not send frame: %s\n", rav1e_status_to_str(ret));
509  return AVERROR_EXTERNAL;
510  default:
511  av_log(avctx, AV_LOG_ERROR, "Unknown return code %d from rav1e_send_frame: %s\n", ret, rav1e_status_to_str(ret));
512  return AVERROR_UNKNOWN;
513  }
514 
515 retry:
516 
517  if (avctx->flags & AV_CODEC_FLAG_PASS1) {
518  int sret = get_stats(avctx, 0);
519  if (sret < 0)
520  return sret;
521  } else if (avctx->flags & AV_CODEC_FLAG_PASS2) {
522  int sret = set_stats(avctx);
523  if (sret < 0)
524  return sret;
525  }
526 
527  ret = rav1e_receive_packet(ctx->ctx, &rpkt);
528  switch (ret) {
529  case RA_ENCODER_STATUS_SUCCESS:
530  break;
531  case RA_ENCODER_STATUS_LIMIT_REACHED:
532  if (avctx->flags & AV_CODEC_FLAG_PASS1) {
533  int sret = get_stats(avctx, 1);
534  if (sret < 0)
535  return sret;
536  }
537  return AVERROR_EOF;
538  case RA_ENCODER_STATUS_ENCODED:
539  goto retry;
540  case RA_ENCODER_STATUS_NEED_MORE_DATA:
541  if (avctx->internal->draining) {
542  av_log(avctx, AV_LOG_ERROR, "Unexpected error when receiving packet after EOF.\n");
543  return AVERROR_EXTERNAL;
544  }
545  return AVERROR(EAGAIN);
546  case RA_ENCODER_STATUS_FAILURE:
547  av_log(avctx, AV_LOG_ERROR, "Could not encode frame: %s\n", rav1e_status_to_str(ret));
548  return AVERROR_EXTERNAL;
549  default:
550  av_log(avctx, AV_LOG_ERROR, "Unknown return code %d from rav1e_receive_packet: %s\n", ret, rav1e_status_to_str(ret));
551  return AVERROR_UNKNOWN;
552  }
553 
554  ret = ff_get_encode_buffer(avctx, pkt, rpkt->len, 0);
555  if (ret < 0) {
556  av_log(avctx, AV_LOG_ERROR, "Could not allocate packet.\n");
557  rav1e_packet_unref(rpkt);
558  return ret;
559  }
560 
561  memcpy(pkt->data, rpkt->data, rpkt->len);
562 
563  if (rpkt->frame_type == RA_FRAME_TYPE_KEY)
565 
566  fd = rpkt->opaque;
567  pkt->pts = pkt->dts = fd->pts;
568  pkt->duration = fd->duration;
569 
570  if (avctx->flags & AV_CODEC_FLAG_COPY_OPAQUE) {
571  pkt->opaque = fd->frame_opaque;
573  fd->frame_opaque_ref = NULL;
574  }
575 
576  frame_data_free(fd);
577 
578  if (avctx->flags & AV_CODEC_FLAG_RECON_FRAME) {
579  AVCodecInternal *avci = avctx->internal;
580  AVFrame *frame = avci->recon_frame;
582 
584 
585  frame->format = avctx->pix_fmt;
586  frame->width = avctx->width;
587  frame->height = avctx->height;
588 
589  ret = ff_encode_alloc_frame(avctx, frame);
590  if (ret < 0) {
591  rav1e_packet_unref(rpkt);
592  return ret;
593  }
594 
595  for (int i = 0; i < desc->nb_components; i++) {
596  int shift = i ? desc->log2_chroma_h : 0;
597  rav1e_frame_extract_plane(rpkt->rec, i, frame->data[i],
598  (frame->height >> shift) * frame->linesize[i],
599  frame->linesize[i], desc->comp[i].step);
600  }
601  }
602 
603  rav1e_packet_unref(rpkt);
604 
605  return 0;
606 }
607 
608 #define OFFSET(x) offsetof(librav1eContext, x)
609 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
610 
611 static const AVOption options[] = {
612  { "qp", "use constant quantizer mode", OFFSET(quantizer), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 255, VE },
613  { "speed", "what speed preset to use", OFFSET(speed), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 10, VE },
614  { "tiles", "number of tiles encode with", OFFSET(tiles), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, INT64_MAX, VE },
615  { "tile-rows", "number of tiles rows to encode with", OFFSET(tile_rows), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, INT64_MAX, VE },
616  { "tile-columns", "number of tiles columns to encode with", OFFSET(tile_cols), AV_OPT_TYPE_INT, { .i64 = 0 }, -1, INT64_MAX, VE },
617  { "rav1e-params", "set the rav1e configuration using a :-separated list of key=value parameters", OFFSET(rav1e_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
618  { NULL }
619 };
620 
622  { "b", "0" },
623  { "g", "0" },
624  { "keyint_min", "0" },
625  { "qmax", "-1" },
626  { "qmin", "-1" },
627  { NULL }
628 };
629 
644 };
645 
646 static const AVClass class = {
647  .class_name = "librav1e",
648  .item_name = av_default_item_name,
649  .option = options,
651 };
652 
654  .p.name = "librav1e",
655  CODEC_LONG_NAME("librav1e AV1"),
656  .p.type = AVMEDIA_TYPE_VIDEO,
657  .p.id = AV_CODEC_ID_AV1,
658  .init = librav1e_encode_init,
660  .close = librav1e_encode_close,
661  .priv_data_size = sizeof(librav1eContext),
662  .p.priv_class = &class,
663  .defaults = librav1e_defaults,
665  .color_ranges = AVCOL_RANGE_MPEG | AVCOL_RANGE_JPEG,
666  .p.capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_OTHER_THREADS |
669  .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
671  .p.wrapper_name = "librav1e",
672 };
OFFSET
#define OFFSET(x)
Definition: librav1e.c:608
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
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: codec_internal.h:42
AVCodecContext::keyint_min
int keyint_min
minimum GOP size
Definition: avcodec.h:998
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
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:659
FrameData
Definition: ffmpeg.h:658
av_pix_fmt_desc_get
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:3341
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AVCodecContext::err_recognition
int err_recognition
Error recognition; may misdetect some more or less valid parts as errors.
Definition: avcodec.h:1398
int64_t
long long int64_t
Definition: coverity.c:34
AV_CODEC_CAP_ENCODER_RECON_FRAME
#define AV_CODEC_CAP_ENCODER_RECON_FRAME
The encoder is able to output reconstructed frame data, i.e.
Definition: codec.h:159
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:63
librav1eContext::rframe
RaFrame * rframe
Definition: librav1e.c:44
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:410
pixdesc.h
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:652
AVCOL_RANGE_JPEG
@ AVCOL_RANGE_JPEG
Full range content.
Definition: pixfmt.h:750
set_stats
static int set_stats(AVCodecContext *avctx)
Definition: librav1e.c:158
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:535
AVOption
AVOption.
Definition: opt.h:429
encode.h
librav1eContext::frame
AVFrame * frame
Definition: librav1e.c:43
data
const char data[16]
Definition: mxf.c:149
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:528
FF_CODEC_CAP_NOT_INIT_THREADSAFE
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
Definition: codec_internal.h:34
FFCodec
Definition: codec_internal.h:127
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:553
mathematics.h
AVDictionary
Definition: dict.c:32
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:1241
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:590
ff_librav1e_encoder
const FFCodec ff_librav1e_encoder
Definition: librav1e.c:653
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
AV_CODEC_FLAG_GLOBAL_HEADER
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:318
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:551
librav1eContext::tiles
int tiles
Definition: librav1e.c:53
AV_CODEC_FLAG_COPY_OPAQUE
#define AV_CODEC_FLAG_COPY_OPAQUE
Definition: avcodec.h:279
FFCodecDefault
Definition: codec_internal.h:96
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
librav1eContext::ctx
RaContext * ctx
Definition: librav1e.c:42
AVPacket::opaque_ref
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
Definition: packet.h:571
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1561
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:488
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:51
AV_PIX_FMT_YUV444P10
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:531
tiles
int tiles
Definition: av1_levels.c:72
avassert.h
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:645
pkt
AVPacket * pkt
Definition: movenc.c:60
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
av_cold
#define av_cold
Definition: attributes.h:90
AV_PIX_FMT_YUVJ422P
@ AV_PIX_FMT_YUVJ422P
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:86
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:515
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:497
librav1eContext
Definition: librav1e.c:39
AVCodecContext::stats_in
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
Definition: avcodec.h:1320
librav1e_encode_init
static av_cold int librav1e_encode_init(AVCodecContext *avctx)
Definition: librav1e.c:192
pix_fmt
static enum AVPixelFormat pix_fmt
Definition: demux_decode.c:41
librav1eContext::pass_pos
size_t pass_pos
Definition: librav1e.c:47
AVDictionaryEntry::key
char * key
Definition: dict.h:91
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:109
librav1eContext::pass_size
int pass_size
Definition: librav1e.c:48
AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
#define AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE
This encoder can reorder user opaque values from input AVFrames and return them with corresponding ou...
Definition: codec.h:144
tile_rows
int tile_rows
Definition: h265_levels.c:217
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
tile_cols
int tile_cols
Definition: av1_levels.c:73
ctx
AVFormatContext * ctx
Definition: movenc.c:49
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:73
AVPacket::opaque
void * opaque
for some private data of the user
Definition: packet.h:560
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:326
AV_PIX_FMT_YUVJ444P
@ AV_PIX_FMT_YUVJ444P
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:87
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:669
CODEC_PIXFMTS_ARRAY
#define CODEC_PIXFMTS_ARRAY(array)
Definition: codec_internal.h:387
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
AV_CODEC_ID_AV1
@ AV_CODEC_ID_AV1
Definition: codec_id.h:284
tmp
static uint8_t tmp[20]
Definition: aes_ctr.c:47
AVCHROMA_LOC_LEFT
@ AVCHROMA_LOC_LEFT
MPEG-2/4 4:2:0, H.264 default for 4:2:0.
Definition: pixfmt.h:771
librav1eContext::rav1e_opts
AVDictionary * rav1e_opts
Definition: librav1e.c:50
AVCHROMA_LOC_TOPLEFT
@ AVCHROMA_LOC_TOPLEFT
ITU-R 601, SMPTE 274M 296M S314M(DV 4:1:1), mpeg2 4:2:2.
Definition: pixfmt.h:773
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:466
AV_PIX_FMT_YUVJ420P
@ AV_PIX_FMT_YUVJ420P
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:85
FF_CODEC_RECEIVE_PACKET_CB
#define FF_CODEC_RECEIVE_PACKET_CB(func)
Definition: codec_internal.h:361
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:481
AV_OPT_TYPE_DICT
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition: opt.h:290
FrameData::duration
int64_t duration
Definition: librav1e.c:60
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:239
options
Definition: swscale.c:43
FrameData::frame_opaque
void * frame_opaque
Definition: librav1e.c:62
AV_PIX_FMT_YUV422P10
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:529
librav1eContext::tile_rows
int tile_rows
Definition: librav1e.c:54
av_base64_decode
int av_base64_decode(uint8_t *out, const char *in_str, int out_size)
Decode a base64-encoded string.
Definition: base64.c:81
base64.h
librav1e_receive_packet
static int librav1e_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
Definition: librav1e.c:442
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:535
ff_encode_alloc_frame
int ff_encode_alloc_frame(AVCodecContext *avctx, AVFrame *frame)
Allocate buffers for a frame.
Definition: encode.c:818
AVCodecContext::stats_out
char * stats_out
pass1 encoding statistics output buffer
Definition: avcodec.h:1312
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1005
codec_internal.h
shift
static int shift(int a, int b)
Definition: bonk.c:261
AV_PIX_FMT_YUV422P12
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:533
AV_PIX_FMT_YUV444P12
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:535
librav1eContext::pass_data
uint8_t * pass_data
Definition: librav1e.c:46
range
enum AVColorRange range
Definition: mediacodec_wrapper.c:2594
range_map
static RaPixelRange range_map(enum AVPixelFormat pix_fmt, enum AVColorRange range)
Definition: librav1e.c:66
buffer.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:534
AV_CODEC_FLAG_PASS2
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
Definition: avcodec.h:294
FrameData::pts
int64_t pts
Definition: ffmpeg.h:667
pix_fmt_map
static RaChromaSampling pix_fmt_map(enum AVPixelFormat pix_fmt)
Definition: librav1e.c:84
librav1eContext::tile_cols
int tile_cols
Definition: librav1e.c:55
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:541
AVCodecInternal
Definition: internal.h:49
FrameData::frame_opaque_ref
AVBufferRef * frame_opaque_ref
Definition: librav1e.c:63
AVChromaLocation
AVChromaLocation
Location of chroma samples.
Definition: pixfmt.h:769
AV_BASE64_SIZE
#define AV_BASE64_SIZE(x)
Calculate the output size needed to base64-encode x bytes to a null-terminated string.
Definition: base64.h:66
AV_CODEC_FLAG_RECON_FRAME
#define AV_CODEC_FLAG_RECON_FRAME
Request the encoder to output reconstructed frames, i.e. frames that would be produced by decoding th...
Definition: avcodec.h:244
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:528
librav1eContext::speed
int speed
Definition: librav1e.c:52
AVCodecContext::extradata
uint8_t * extradata
Out-of-band global headers that may be used by some codecs.
Definition: avcodec.h:514
internal.h
VE
#define VE
Definition: librav1e.c:609
common.h
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:498
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:256
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:179
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:676
AVCodecContext::height
int height
Definition: avcodec.h:592
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:631
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:733
librav1eContext::quantizer
int quantizer
Definition: librav1e.c:51
avcodec.h
options
static const AVOption options[]
Definition: librav1e.c:611
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:80
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
AVCodecInternal::recon_frame
AVFrame * recon_frame
When the AV_CODEC_FLAG_RECON_FRAME flag is used.
Definition: internal.h:114
AV_PIX_FMT_YUV420P12
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:532
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: defs.h:40
frame_data_free
static void frame_data_free(void *data)
Definition: librav1e.c:431
AVCodecContext
main external API structure.
Definition: avcodec.h:431
ff_get_encode_buffer
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition: encode.c:106
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1234
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:72
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:259
librav1e_defaults
static const FFCodecDefault librav1e_defaults[]
Definition: librav1e.c:621
av_base64_encode
char * av_base64_encode(char *out, int out_size, const uint8_t *in, int in_size)
Encode data to base64 and null-terminate.
Definition: base64.c:147
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:76
chroma_loc_map
static RaChromaSamplePosition chroma_loc_map(enum AVChromaLocation chroma_loc)
Definition: librav1e.c:107
AV_PIX_FMT_YUV444P
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:78
AVCodecInternal::draining
int draining
decoding: AVERROR_EOF has been returned from ff_decode_get_packet(); must not be used by decoders tha...
Definition: internal.h:139
desc
const char * desc
Definition: libsvtav1.c:79
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
AV_PIX_FMT_YUV422P
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:77
mem.h
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:205
AVBufferRef
A reference to a data buffer.
Definition: buffer.h:82
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: codec_internal.h:72
librav1e_pix_fmts
enum AVPixelFormat librav1e_pix_fmts[]
Definition: librav1e.c:630
AVPixFmtDescriptor
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:69
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVDictionaryEntry
Definition: dict.h:90
AVPacket
This structure stores compressed data.
Definition: packet.h:512
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:458
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:592
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
AVDictionaryEntry::value
char * value
Definition: dict.h:92
AVColorRange
AVColorRange
Visual content value range.
Definition: pixfmt.h:715
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:616
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:42
librav1e_encode_close
static av_cold int librav1e_encode_close(AVCodecContext *avctx)
Definition: librav1e.c:173
AV_CODEC_FLAG_PASS1
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
Definition: avcodec.h:290
get_stats
static int get_stats(AVCodecContext *avctx, int eos)
Definition: librav1e.c:119