FFmpeg
libxeve.c
Go to the documentation of this file.
1 /*
2  * libxeve encoder
3  * EVC (MPEG-5 Essential Video Coding) encoding using XEVE MPEG-5 EVC encoder library
4  *
5  * Copyright (C) 2021 Dawid Kozinski <d.kozinski@samsung.com>
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <float.h>
25 #include <stdlib.h>
26 
27 #include <xeve.h>
28 
29 #include "libavutil/internal.h"
30 #include "libavutil/common.h"
31 #include "libavutil/mem.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/pixfmt.h"
35 #include "libavutil/time.h"
36 #include "libavutil/cpu.h"
37 #include "libavutil/avstring.h"
38 
39 #include "avcodec.h"
40 #include "internal.h"
41 #include "packet_internal.h"
42 #include "codec_internal.h"
43 #include "profiles.h"
44 #include "encode.h"
45 
46 #define MAX_BS_BUF (16*1024*1024)
47 
48 /**
49  * Error codes
50  */
51 #define XEVE_PARAM_BAD_NAME -100
52 #define XEVE_PARAM_BAD_VALUE -200
53 
54 /**
55  * Encoder states
56  *
57  * STATE_ENCODING - the encoder receives and processes input frames
58  * STATE_BUMPING - there are no more input frames, however the encoder still processes previously received data
59  */
60 typedef enum State {
63 } State;
64 
65 /**
66  * The structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder
67  */
68 typedef struct XeveContext {
69  const AVClass *class;
70 
71  XEVE id; // XEVE instance identifier
72  XEVE_CDSC cdsc; // coding parameters i.e profile, width & height of input frame, num of therads, frame rate ...
73  XEVE_BITB bitb; // bitstream buffer (output)
74  XEVE_STAT stat; // encoding status (output)
75  XEVE_IMGB imgb; // image buffer (input)
76 
77  State state; // encoder state (skipping, encoding, bumping)
78 
79  int profile_id; // encoder profile (main, baseline)
80  int preset_id; // preset of xeve ( fast, medium, slow, placebo)
81  int tune_id; // tune of xeve (psnr, zerolatency)
82 
83  // variables for rate control modes
84  int rc_mode; // Rate control mode [ 0(CQP) / 1(ABR) / 2(CRF) ]
85  int qp; // quantization parameter (QP) [0,51]
86  int crf; // constant rate factor (CRF) [10,49]
87 
88  int hash; // embed picture signature (HASH) for conformance checking in decoding
89  int sei_info; // embed Supplemental enhancement information while encoding
90 
91  int color_format; // input data color format: currently only XEVE_CF_YCBCR420 is supported
92 
94 } XeveContext;
95 
96 /**
97  * Convert FFmpeg pixel format (AVPixelFormat) to XEVE pre-defined color format
98  *
99  * @param[in] av_pix_fmt pixel format (@see https://ffmpeg.org/doxygen/trunk/pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5)
100  * @param[out] xeve_col_fmt XEVE pre-defined color format (@see xeve.h)
101  *
102  * @return 0 on success, negative value on failure
103  */
104 static int libxeve_color_fmt(enum AVPixelFormat av_pix_fmt, int *xeve_col_fmt)
105 {
106  switch (av_pix_fmt) {
107  case AV_PIX_FMT_YUV420P:
108  *xeve_col_fmt = XEVE_CF_YCBCR420;
109  break;
111  *xeve_col_fmt = XEVE_CF_YCBCR420;
112  break;
113  default:
114  *xeve_col_fmt = XEVE_CF_UNKNOWN;
115  return AVERROR_INVALIDDATA;
116  }
117 
118  return 0;
119 }
120 
121 /**
122  * Convert FFmpeg pixel format (AVPixelFormat) into XEVE pre-defined color space
123  *
124  * @param[in] px_fmt pixel format (@see https://ffmpeg.org/doxygen/trunk/pixfmt_8h.html#a9a8e335cf3be472042bc9f0cf80cd4c5)
125  *
126  * @return XEVE pre-defined color space (@see xeve.h) on success, XEVE_CF_UNKNOWN on failure
127  */
128 static int libxeve_color_space(enum AVPixelFormat av_pix_fmt)
129 {
130  /* color space of input image */
131  int cs = XEVE_CF_UNKNOWN;
132 
133  switch (av_pix_fmt) {
134  case AV_PIX_FMT_YUV420P:
135  cs = XEVE_CS_YCBCR420;
136  break;
138 #if AV_HAVE_BIGENDIAN
139  cs = XEVE_CS_SET(XEVE_CF_YCBCR420, 10, 1);
140 #else
141  cs = XEVE_CS_YCBCR420_10LE;
142 #endif
143 
144  break;
145  default:
146  cs = XEVE_CF_UNKNOWN;
147  break;
148  }
149 
150  return cs;
151 }
152 
153 /**
154  * The function returns a pointer to the object of the XEVE_CDSC type.
155  * XEVE_CDSC contains all encoder parameters that should be initialized before the encoder is used.
156  *
157  * The field values of the XEVE_CDSC structure are populated based on:
158  * - the corresponding field values of the AvCodecConetxt structure,
159  * - the xeve encoder specific option values,
160  * (the full list of options available for xeve encoder is displayed after executing the command ./ffmpeg --help encoder = libxeve)
161  *
162  * The order of processing input data and populating the XEVE_CDSC structure
163  * 1) first, the fields of the AVCodecContext structure corresponding to the provided input options are processed,
164  * (i.e -pix_fmt yuv420p -s:v 1920x1080 -r 30 -profile:v 0)
165  * 2) then xeve-specific options added as AVOption to the xeve AVCodec implementation
166  * (i.e -preset 0)
167  *
168  * Keep in mind that, there are options that can be set in different ways.
169  * In this case, please follow the above-mentioned order of processing.
170  * The most recent assignments overwrite the previous values.
171  *
172  * @param[in] avctx codec context (AVCodecContext)
173  * @param[out] cdsc contains all Xeve MPEG-5 EVC encoder encoder parameters that should be initialized before the encoder is use
174  *
175  * @return 0 on success, negative error code on failure
176  */
177 static int get_conf(AVCodecContext *avctx, XEVE_CDSC *cdsc)
178 {
179  XeveContext *xectx = NULL;
180  int ret;
181 
182  xectx = avctx->priv_data;
183 
184  /* initialize xeve_param struct with default values */
185  ret = xeve_param_default(&cdsc->param);
186  if (XEVE_FAILED(ret)) {
187  av_log(avctx, AV_LOG_ERROR, "Cannot set_default parameter\n");
188  return AVERROR_EXTERNAL;
189  }
190 
191  /* read options from AVCodecContext */
192  if (avctx->width > 0)
193  cdsc->param.w = avctx->width;
194 
195  if (avctx->height > 0)
196  cdsc->param.h = avctx->height;
197 
198  if (avctx->framerate.num > 0) {
199  // fps can be float number, but xeve API doesn't support it
200  cdsc->param.fps.num = avctx->framerate.num;
201  cdsc->param.fps.den = avctx->framerate.den;
202  }
203 
204  // GOP size (key-frame interval, I-picture period)
205  cdsc->param.keyint = avctx->gop_size; // 0: only one I-frame at the first time; 1: every frame is coded in I-frame
206 
207  if (avctx->max_b_frames == 0 || avctx->max_b_frames == 1 || avctx->max_b_frames == 3 ||
208  avctx->max_b_frames == 7 || avctx->max_b_frames == 15) // number of b-frames
209  cdsc->param.bframes = avctx->max_b_frames;
210  else {
211  av_log(avctx, AV_LOG_ERROR, "Incorrect value for maximum number of B frames: (%d) \n"
212  "Acceptable values for bf option (maximum number of B frames) are 0,1,3,7 or 15\n", avctx->max_b_frames);
213  return AVERROR_INVALIDDATA;
214  }
215 
216  cdsc->param.level_idc = avctx->level;
217 
218  if (avctx->rc_buffer_size) // VBV buf size
219  cdsc->param.vbv_bufsize = (int)(avctx->rc_buffer_size / 1000);
220 
221  cdsc->param.rc_type = xectx->rc_mode;
222 
223  if (xectx->rc_mode == XEVE_RC_CQP)
224  cdsc->param.qp = xectx->qp;
225  else if (xectx->rc_mode == XEVE_RC_ABR) {
226  if (avctx->bit_rate / 1000 > INT_MAX || avctx->rc_max_rate / 1000 > INT_MAX) {
227  av_log(avctx, AV_LOG_ERROR, "Not supported bitrate bit_rate and rc_max_rate > %d000\n", INT_MAX);
228  return AVERROR_INVALIDDATA;
229  }
230  cdsc->param.bitrate = (int)(avctx->bit_rate / 1000);
231  } else if (xectx->rc_mode == XEVE_RC_CRF)
232  cdsc->param.crf = xectx->crf;
233  else {
234  av_log(avctx, AV_LOG_ERROR, "Not supported rate control type: %d\n", xectx->rc_mode);
235  return AVERROR_INVALIDDATA;
236  }
237 
238  if (avctx->thread_count <= 0) {
239  int cpu_count = av_cpu_count();
240  cdsc->param.threads = (cpu_count < XEVE_MAX_THREADS) ? cpu_count : XEVE_MAX_THREADS;
241  } else if (avctx->thread_count > XEVE_MAX_THREADS)
242  cdsc->param.threads = XEVE_MAX_THREADS;
243  else
244  cdsc->param.threads = avctx->thread_count;
245 
246 
247  libxeve_color_fmt(avctx->pix_fmt, &xectx->color_format);
248 
249  cdsc->param.cs = XEVE_CS_SET(xectx->color_format, cdsc->param.codec_bit_depth, AV_HAVE_BIGENDIAN);
250 
251  cdsc->max_bs_buf_size = MAX_BS_BUF;
252 
253  ret = xeve_param_ppt(&cdsc->param, xectx->profile_id, xectx->preset_id, xectx->tune_id);
254  if (XEVE_FAILED(ret)) {
255  av_log(avctx, AV_LOG_ERROR, "Cannot set profile(%d), preset(%d), tune(%d)\n", xectx->profile_id, xectx->preset_id, xectx->tune_id);
256  return AVERROR_EXTERNAL;
257  }
258 
259  return 0;
260 }
261 
262 /**
263  * Set XEVE_CFG_SET_USE_PIC_SIGNATURE for encoder
264  *
265  * @param[in] logger context
266  * @param[in] id XEVE encodec instance identifier
267  * @param[in] ctx the structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder
268  *
269  * @return 0 on success, negative error code on failure
270  */
271 static int set_extra_config(AVCodecContext *avctx, XEVE id, XeveContext *ctx)
272 {
273  int ret, size;
274  size = 4;
275 
276  // embed SEI messages identifying encoder parameters and command line arguments
277  // - 0: off\n"
278  // - 1: emit sei info"
279  //
280  // SEI - Supplemental enhancement information contains information
281  // that is not necessary to decode the samples of coded pictures from VCL NAL units.
282  // Some SEI message information is required to check bitstream conformance
283  // and for output timing decoder conformance.
284  // @see ISO_IEC_23094-1_2020 7.4.3.5
285  // @see ISO_IEC_23094-1_2020 Annex D
286  ret = xeve_config(id, XEVE_CFG_SET_SEI_CMD, &ctx->sei_info, &size); // sei_cmd_info
287  if (XEVE_FAILED(ret)) {
288  av_log(avctx, AV_LOG_ERROR, "Failed to set config for sei command info messages\n");
289  return AVERROR_EXTERNAL;
290  }
291 
292  ret = xeve_config(id, XEVE_CFG_SET_USE_PIC_SIGNATURE, &ctx->hash, &size);
293  if (XEVE_FAILED(ret)) {
294  av_log(avctx, AV_LOG_ERROR, "Failed to set config for picture signature\n");
295  return AVERROR_EXTERNAL;
296  }
297 
298  return 0;
299 }
300 
301 /**
302  * @brief Switch encoder to bumping mode
303  *
304  * @param id XEVE encodec instance identifier
305  * @return 0 on success, negative error code on failure
306  */
307 static int setup_bumping(XEVE id)
308 {
309  int val = 1;
310  int size = sizeof(int);
311  if (XEVE_FAILED(xeve_config(id, XEVE_CFG_SET_FORCE_OUT, (void *)(&val), &size)))
312  return AVERROR_EXTERNAL;
313 
314  return 0;
315 }
316 
317 /**
318  * @brief Initialize eXtra-fast Essential Video Encoder codec
319  * Create an encoder instance and allocate all the needed resources
320  *
321  * @param avctx codec context
322  * @return 0 on success, negative error code on failure
323  */
325 {
326  XeveContext *xectx = avctx->priv_data;
327  unsigned char *bs_buf = NULL;
328  int i;
329  int shift_h = 0;
330  int shift_v = 0;
331  int width_chroma = 0;
332  int height_chroma = 0;
333  XEVE_IMGB *imgb = NULL;
334  int ret = 0;
335 
336  XEVE_CDSC *cdsc = &(xectx->cdsc);
337 
338  /* allocate bitstream buffer */
339  bs_buf = av_malloc(MAX_BS_BUF);
340  if (bs_buf == NULL) {
341  av_log(avctx, AV_LOG_ERROR, "Cannot allocate bitstream buffer\n");
342  return AVERROR(ENOMEM);
343  }
344  xectx->bitb.addr = bs_buf;
345  xectx->bitb.bsize = MAX_BS_BUF;
346 
347  /* read configurations and set values for created descriptor (XEVE_CDSC) */
348  if ((ret = get_conf(avctx, cdsc)) != 0) {
349  av_log(avctx, AV_LOG_ERROR, "Cannot get configuration\n");
350  return AVERROR(EINVAL);
351  }
352 
353  if ((ret = xeve_param_check(&cdsc->param)) != 0) {
354  av_log(avctx, AV_LOG_ERROR, "Invalid configuration\n");
355  return AVERROR(EINVAL);
356  }
357 
358  {
359  const AVDictionaryEntry *en = NULL;
360  while (en = av_dict_iterate(xectx->xeve_params, en)) {
361  if ((ret = xeve_param_parse(&cdsc->param, en->key, en->value)) < 0) {
362  av_log(avctx, AV_LOG_WARNING,
363  "Error parsing option '%s = %s'.\n",
364  en->key, en->value);
365  }
366  }
367  }
368 
369  /* create encoder */
370  xectx->id = xeve_create(cdsc, NULL);
371  if (xectx->id == NULL) {
372  av_log(avctx, AV_LOG_ERROR, "Cannot create XEVE encoder\n");
373  return AVERROR_EXTERNAL;
374  }
375 
376  if ((ret = set_extra_config(avctx, xectx->id, xectx)) != 0) {
377  av_log(avctx, AV_LOG_ERROR, "Cannot set extra configuration\n");
378  return AVERROR(EINVAL);
379  }
380 
381  if ((ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v)) != 0) {
382  av_log(avctx, AV_LOG_ERROR, "Failed to get chroma shift\n");
383  return AVERROR(EINVAL);
384  }
385 
386  // Chroma subsampling
387  //
388  // YUV format explanation
389  // shift_h == 1 && shift_v == 1 : YUV420
390  // shift_h == 1 && shift_v == 0 : YUV422
391  // shift_h == 0 && shift_v == 0 : YUV444
392  //
393  width_chroma = AV_CEIL_RSHIFT(avctx->width, shift_h);
394  height_chroma = AV_CEIL_RSHIFT(avctx->height, shift_v);
395 
396  /* set default values for input image buffer */
397  imgb = &xectx->imgb;
398  imgb->cs = libxeve_color_space(avctx->pix_fmt);
399  imgb->np = 3; /* only for yuv420p, yuv420ple */
400 
401  for (i = 0; i < imgb->np; i++)
402  imgb->x[i] = imgb->y[i] = 0;
403 
404  imgb->w[0] = imgb->aw[0] = avctx->width; // width luma
405  imgb->w[1] = imgb->w[2] = imgb->aw[1] = imgb->aw[2] = width_chroma;
406  imgb->h[0] = imgb->ah[0] = avctx->height; // height luma
407  imgb->h[1] = imgb->h[2] = imgb->ah[1] = imgb->ah[2] = height_chroma;
408 
409  xectx->state = STATE_ENCODING;
410 
411  return 0;
412 }
413 
414 /**
415  * Encode raw data frame into EVC packet
416  *
417  * @param[in] avctx codec context
418  * @param[out] avpkt output AVPacket containing encoded data
419  * @param[in] frame AVFrame containing the raw data to be encoded
420  * @param[out] got_packet encoder sets to 0 or 1 to indicate that a
421  * non-empty packet was returned in pkt
422  *
423  * @return 0 on success, negative error code on failure
424  */
425 static int libxeve_encode(AVCodecContext *avctx, AVPacket *avpkt,
426  const AVFrame *frame, int *got_packet)
427 {
428  XeveContext *xectx = avctx->priv_data;
429  int ret = -1;
430 
431  // No more input frames are available but encoder still can have some data in its internal buffer to process
432  // and some frames to dump.
433  if (xectx->state == STATE_ENCODING && frame == NULL) {
434  if (setup_bumping(xectx->id) == 0)
435  xectx->state = STATE_BUMPING; // Entering bumping process
436  else {
437  av_log(avctx, AV_LOG_ERROR, "Failed to setup bumping\n");
438  return 0;
439  }
440  }
441 
442  if (xectx->state == STATE_ENCODING) {
443  int i;
444  XEVE_IMGB *imgb = NULL;
445 
446  imgb = &xectx->imgb;
447 
448  for (i = 0; i < imgb->np; i++) {
449  imgb->a[i] = frame->data[i];
450  imgb->s[i] = frame->linesize[i];
451  }
452 
453  imgb->ts[XEVE_TS_PTS] = frame->pts;
454 
455  /* push image to encoder */
456  ret = xeve_push(xectx->id, imgb);
457  if (XEVE_FAILED(ret)) {
458  av_log(avctx, AV_LOG_ERROR, "xeve_push() failed\n");
459  return AVERROR_EXTERNAL;
460  }
461  }
462  if (xectx->state == STATE_ENCODING || xectx->state == STATE_BUMPING) {
463  /* encoding */
464  ret = xeve_encode(xectx->id, &(xectx->bitb), &(xectx->stat));
465  if (XEVE_FAILED(ret)) {
466  av_log(avctx, AV_LOG_ERROR, "xeve_encode() failed\n");
467  return AVERROR_EXTERNAL;
468  }
469 
470  /* store bitstream */
471  if (ret == XEVE_OK_OUT_NOT_AVAILABLE) { // Return OK but picture is not available yet
472  *got_packet = 0;
473  return 0;
474  } else if (ret == XEVE_OK) {
475  int av_pic_type;
476 
477  if (xectx->stat.write > 0) {
478 
479  ret = ff_get_encode_buffer(avctx, avpkt, xectx->stat.write, 0);
480  if (ret < 0)
481  return ret;
482 
483  memcpy(avpkt->data, xectx->bitb.addr, xectx->stat.write);
484 
485  avpkt->time_base.num = xectx->cdsc.param.fps.den;
486  avpkt->time_base.den = xectx->cdsc.param.fps.num;
487 
488  avpkt->pts = xectx->bitb.ts[XEVE_TS_PTS];
489  avpkt->dts = xectx->bitb.ts[XEVE_TS_DTS];
490 
491  switch(xectx->stat.stype) {
492  case XEVE_ST_I:
493  av_pic_type = AV_PICTURE_TYPE_I;
494  avpkt->flags |= AV_PKT_FLAG_KEY;
495  break;
496  case XEVE_ST_P:
497  av_pic_type = AV_PICTURE_TYPE_P;
498  break;
499  case XEVE_ST_B:
500  av_pic_type = AV_PICTURE_TYPE_B;
501  break;
502  case XEVE_ST_UNKNOWN:
503  av_log(avctx, AV_LOG_ERROR, "Unknown slice type\n");
504  return AVERROR_INVALIDDATA;
505  }
506 
507  ff_side_data_set_encoder_stats(avpkt, xectx->stat.qp * FF_QP2LAMBDA, NULL, 0, av_pic_type);
508 
509  *got_packet = 1;
510  }
511  } else if (ret == XEVE_OK_NO_MORE_FRM) {
512  // Return OK but no more frames
513  return 0;
514  } else {
515  av_log(avctx, AV_LOG_ERROR, "Invalid return value: %d\n", ret);
516  return AVERROR_EXTERNAL;
517  }
518  } else {
519  av_log(avctx, AV_LOG_ERROR, "Udefined encoder state\n");
520  return AVERROR_INVALIDDATA;
521  }
522 
523  return 0;
524 }
525 
526 /**
527  * Destroy the encoder and release all the allocated resources
528  *
529  * @param avctx codec context
530  * @return 0 on success, negative error code on failure
531  */
533 {
534  XeveContext *xectx = avctx->priv_data;
535 
536  if (xectx->id) {
537  xeve_delete(xectx->id);
538  xectx->id = NULL;
539  }
540 
541  av_free(xectx->bitb.addr); /* release bitstream buffer */
542 
543  return 0;
544 }
545 
546 #define OFFSET(x) offsetof(XeveContext, x)
547 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
548 
549 static const enum AVPixelFormat supported_pixel_formats[] = {
553 };
554 
555 // Consider using following options (./ffmpeg --help encoder=libxeve)
556 //
557 static const AVOption libxeve_options[] = {
558  { "preset", "Encoding preset for setting encoding speed", OFFSET(preset_id), AV_OPT_TYPE_INT, { .i64 = XEVE_PRESET_MEDIUM }, XEVE_PRESET_DEFAULT, XEVE_PRESET_PLACEBO, VE, .unit = "preset" },
559  { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "preset" },
560  { "fast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_FAST }, INT_MIN, INT_MAX, VE, .unit = "preset" },
561  { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_MEDIUM }, INT_MIN, INT_MAX, VE, .unit = "preset" },
562  { "slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_SLOW }, INT_MIN, INT_MAX, VE, .unit = "preset" },
563  { "placebo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_PLACEBO }, INT_MIN, INT_MAX, VE, .unit = "preset" },
564  { "tune", "Tuning parameter for special purpose operation", OFFSET(tune_id), AV_OPT_TYPE_INT, { .i64 = XEVE_TUNE_NONE }, XEVE_TUNE_NONE, XEVE_TUNE_PSNR, VE, .unit = "tune"},
565  { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_NONE }, INT_MIN, INT_MAX, VE, .unit = "tune" },
566  { "zerolatency", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_ZEROLATENCY }, INT_MIN, INT_MAX, VE, .unit = "tune" },
567  { "psnr", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_PSNR }, INT_MIN, INT_MAX, VE, .unit = "tune" },
568  { "profile", "Encoding profile", OFFSET(profile_id), AV_OPT_TYPE_INT, { .i64 = XEVE_PROFILE_BASELINE }, XEVE_PROFILE_BASELINE, XEVE_PROFILE_MAIN, VE, .unit = "profile" },
569  { "baseline", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_BASELINE }, INT_MIN, INT_MAX, VE, .unit = "profile" },
570  { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_MAIN }, INT_MIN, INT_MAX, VE, .unit = "profile" },
571  { "rc_mode", "Rate control mode", OFFSET(rc_mode), AV_OPT_TYPE_INT, { .i64 = XEVE_RC_CQP }, XEVE_RC_CQP, XEVE_RC_CRF, VE, .unit = "rc_mode" },
572  { "CQP", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CQP }, INT_MIN, INT_MAX, VE, .unit = "rc_mode" },
573  { "ABR", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_ABR }, INT_MIN, INT_MAX, VE, .unit = "rc_mode" },
574  { "CRF", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CRF }, INT_MIN, INT_MAX, VE, .unit = "rc_mode" },
575  { "qp", "Quantization parameter value for CQP rate control mode", OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 51, VE },
576  { "crf", "Constant rate factor value for CRF rate control mode", OFFSET(crf), AV_OPT_TYPE_INT, { .i64 = 32 }, 10, 49, VE },
577  { "hash", "Embed picture signature (HASH) for conformance checking in decoding", OFFSET(hash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
578  { "sei_info", "Embed SEI messages identifying encoder parameters and command line arguments", OFFSET(sei_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
579  { "xeve-params", "Override the xeve configuration using a :-separated list of key=value parameters", OFFSET(xeve_params), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
580  { NULL }
581 };
582 
583 static const AVClass libxeve_class = {
584  .class_name = "libxeve",
585  .item_name = av_default_item_name,
586  .option = libxeve_options,
587  .version = LIBAVUTIL_VERSION_INT,
588 };
589 
590 /**
591  * libavcodec generic global options, which can be set on all the encoders and decoders
592  * @see https://www.ffmpeg.org/ffmpeg-codecs.html#Codec-Options
593  */
595  { "b", "0" }, // bitrate in terms of kilo-bits per second
596  { "g", "0" }, // gop_size (key-frame interval 0: only one I-frame at the first time; 1: every frame is coded in I-frame)
597  { "bf", "15"}, // maximum number of B frames (0: no B-frames, 1,3,7,15)
598  { "threads", "0"}, // number of threads to be used (0: automatically select the number of threads to set)
599  { NULL },
600 };
601 
603  .p.name = "libxeve",
604  .p.long_name = NULL_IF_CONFIG_SMALL("libxeve MPEG-5 EVC"),
605  .p.type = AVMEDIA_TYPE_VIDEO,
606  .p.id = AV_CODEC_ID_EVC,
607  .init = libxeve_init,
609  .close = libxeve_close,
610  .priv_data_size = sizeof(XeveContext),
611  .p.priv_class = &libxeve_class,
612  .defaults = libxeve_defaults,
614  .p.profiles = NULL_IF_CONFIG_SMALL(ff_evc_profiles),
615  .p.wrapper_name = "libxeve",
616  .p.pix_fmts = supported_pixel_formats,
617  .color_ranges = AVCOL_RANGE_MPEG, /* FIXME: implement tagging */
619 };
libxeve_color_space
static int libxeve_color_space(enum AVPixelFormat av_pix_fmt)
Convert FFmpeg pixel format (AVPixelFormat) into XEVE pre-defined color space.
Definition: libxeve.c:128
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
XeveContext::profile_id
int profile_id
Definition: libxeve.c:79
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:71
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:43
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
cpu_count
static atomic_int cpu_count
Definition: cpu.c:57
libxeve_class
static const AVClass libxeve_class
Definition: libxeve.c:583
XeveContext::crf
int crf
Definition: libxeve.c:86
XeveContext::imgb
XEVE_IMGB imgb
Definition: libxeve.c:75
XeveContext::tune_id
int tune_id
Definition: libxeve.c:81
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:389
pixdesc.h
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:539
AVOption
AVOption.
Definition: opt.h:429
encode.h
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:502
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:35
FFCodec
Definition: codec_internal.h:127
float.h
AVDictionary
Definition: dict.c:34
hash
uint8_t hash[HASH_SIZE]
Definition: movenc.c:58
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:594
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:566
XeveContext::hash
int hash
Definition: libxeve.c:88
FFCodecDefault
Definition: codec_internal.h:97
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1593
val
static double val(void *priv, double ch)
Definition: aeval.c:77
av_pix_fmt_get_chroma_sub_sample
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: pixdesc.c:3198
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:320
AVRational::num
int num
Numerator.
Definition: rational.h:59
set_extra_config
static int set_extra_config(AVCodecContext *avctx, XEVE id, XeveContext *ctx)
Set XEVE_CFG_SET_USE_PIC_SIGNATURE for encoder.
Definition: libxeve.c:271
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_CODEC_ID_EVC
@ AV_CODEC_ID_EVC
Definition: codec_id.h:325
AV_CEIL_RSHIFT
#define AV_CEIL_RSHIFT(a, b)
Definition: common.h:60
AVDictionaryEntry::key
char * key
Definition: dict.h:90
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:124
ff_evc_profiles
const AVProfile ff_evc_profiles[]
Definition: profiles.c:200
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
AVCodecContext::rc_max_rate
int64_t rc_max_rate
maximum bitrate
Definition: avcodec.h:1302
AVCodecContext::rc_buffer_size
int rc_buffer_size
decoder bitstream buffer size
Definition: avcodec.h:1287
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
XeveContext::preset_id
int preset_id
Definition: libxeve.c:80
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:75
NULL
#define NULL
Definition: coverity.c:32
MAX_BS_BUF
#define MAX_BS_BUF
Definition: libxeve.c:46
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:501
AV_OPT_TYPE_DICT
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition: opt.h:290
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
profiles.h
time.h
libxeve_color_fmt
static int libxeve_color_fmt(enum AVPixelFormat av_pix_fmt, int *xeve_col_fmt)
Convert FFmpeg pixel format (AVPixelFormat) to XEVE pre-defined color format.
Definition: libxeve.c:104
XeveContext::cdsc
XEVE_CDSC cdsc
Definition: libxeve.c:72
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1794
State
State
Encoder states.
Definition: libxeve.c:60
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:217
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
XeveContext::rc_mode
int rc_mode
Definition: libxeve.c:84
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:94
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:1037
codec_internal.h
cpu.h
libxeve_options
static const AVOption libxeve_options[]
Definition: libxeve.c:557
size
int size
Definition: twinvq_data.h:10344
STATE_ENCODING
@ STATE_ENCODING
Definition: libxeve.c:61
libxeve_encode
static int libxeve_encode(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet)
Encode raw data frame into EVC packet.
Definition: libxeve.c:425
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:538
XeveContext::qp
int qp
Definition: libxeve.c:85
XeveContext
The structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder.
Definition: libxeve.c:68
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:545
XeveContext::id
XEVE id
Definition: libxeve.c:71
XeveContext::bitb
XEVE_BITB bitb
Definition: libxeve.c:73
State
Definition: gemdec.c:59
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:532
internal.h
XeveContext::xeve_params
AVDictionary * xeve_params
Definition: libxeve.c:93
STATE_BUMPING
@ STATE_BUMPING
Definition: libxeve.c:62
common.h
VE
#define VE
Definition: libxeve.c:547
XeveContext::stat
XEVE_STAT stat
Definition: libxeve.c:74
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::height
int height
Definition: avcodec.h:624
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:663
AVCOL_RANGE_MPEG
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition: pixfmt.h:700
avcodec.h
ret
ret
Definition: filter_design.txt:187
pixfmt.h
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
get_conf
static int get_conf(AVCodecContext *avctx, XEVE_CDSC *cdsc)
The function returns a pointer to the object of the XEVE_CDSC type.
Definition: libxeve.c:177
libxeve_close
static av_cold int libxeve_close(AVCodecContext *avctx)
Destroy the encoder and release all the allocated resources.
Definition: libxeve.c:532
AVCodecContext
main external API structure.
Definition: avcodec.h:451
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:281
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
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
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
setup_bumping
static int setup_bumping(XEVE id)
Switch encoder to bumping mode.
Definition: libxeve.c:307
libxeve_init
static av_cold int libxeve_init(AVCodecContext *avctx)
Initialize eXtra-fast Essential Video Encoder codec Create an encoder instance and allocate all the n...
Definition: libxeve.c:324
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:280
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
mem.h
AVCodecContext::max_b_frames
int max_b_frames
maximum number of B-frames between non-B-frames Note: The output will be delayed by max_b_frames+1 re...
Definition: avcodec.h:801
packet_internal.h
XeveContext::color_format
int color_format
Definition: libxeve.c:91
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVDictionaryEntry
Definition: dict.h:89
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:478
OFFSET
#define OFFSET(x)
Definition: libxeve.c:546
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:624
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
XeveContext::state
State state
Definition: libxeve.c:77
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
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: packet.c:609
supported_pixel_formats
static enum AVPixelFormat supported_pixel_formats[]
Definition: libxeve.c:549
libxeve_defaults
static const FFCodecDefault libxeve_defaults[]
libavcodec generic global options, which can be set on all the encoders and decoders
Definition: libxeve.c:594
AVDictionaryEntry::value
char * value
Definition: dict.h:91
avstring.h
ff_libxeve_encoder
const FFCodec ff_libxeve_encoder
Definition: libxeve.c:602
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:143
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Special option type for declaring named constants.
Definition: opt.h:299
XeveContext::sei_info
int sei_info
Definition: libxeve.c:89
av_dict_iterate
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition: dict.c:44
AVPacket::time_base
AVRational time_base
Time base of the packet's timestamps.
Definition: packet.h:583