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