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