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 = lrintf(av_q2d(avctx->framerate));
201  }
202 
203  // GOP size (key-frame interval, I-picture period)
204  cdsc->param.keyint = avctx->gop_size; // 0: only one I-frame at the first time; 1: every frame is coded in I-frame
205 
206  if (avctx->max_b_frames == 0 || avctx->max_b_frames == 1 || avctx->max_b_frames == 3 ||
207  avctx->max_b_frames == 7 || avctx->max_b_frames == 15) // number of b-frames
208  cdsc->param.bframes = avctx->max_b_frames;
209  else {
210  av_log(avctx, AV_LOG_ERROR, "Incorrect value for maximum number of B frames: (%d) \n"
211  "Acceptable values for bf option (maximum number of B frames) are 0,1,3,7 or 15\n", avctx->max_b_frames);
212  return AVERROR_INVALIDDATA;
213  }
214 
215  cdsc->param.level_idc = avctx->level;
216 
217  if (avctx->rc_buffer_size) // VBV buf size
218  cdsc->param.vbv_bufsize = (int)(avctx->rc_buffer_size / 1000);
219 
220  cdsc->param.rc_type = xectx->rc_mode;
221 
222  if (xectx->rc_mode == XEVE_RC_CQP)
223  cdsc->param.qp = xectx->qp;
224  else if (xectx->rc_mode == XEVE_RC_ABR) {
225  if (avctx->bit_rate / 1000 > INT_MAX || avctx->rc_max_rate / 1000 > INT_MAX) {
226  av_log(avctx, AV_LOG_ERROR, "Not supported bitrate bit_rate and rc_max_rate > %d000\n", INT_MAX);
227  return AVERROR_INVALIDDATA;
228  }
229  cdsc->param.bitrate = (int)(avctx->bit_rate / 1000);
230  } else if (xectx->rc_mode == XEVE_RC_CRF)
231  cdsc->param.crf = xectx->crf;
232  else {
233  av_log(avctx, AV_LOG_ERROR, "Not supported rate control type: %d\n", xectx->rc_mode);
234  return AVERROR_INVALIDDATA;
235  }
236 
237  if (avctx->thread_count <= 0) {
238  int cpu_count = av_cpu_count();
239  cdsc->param.threads = (cpu_count < XEVE_MAX_THREADS) ? cpu_count : XEVE_MAX_THREADS;
240  } else if (avctx->thread_count > XEVE_MAX_THREADS)
241  cdsc->param.threads = XEVE_MAX_THREADS;
242  else
243  cdsc->param.threads = avctx->thread_count;
244 
245 
246  libxeve_color_fmt(avctx->pix_fmt, &xectx->color_format);
247 
248  cdsc->param.cs = XEVE_CS_SET(xectx->color_format, cdsc->param.codec_bit_depth, AV_HAVE_BIGENDIAN);
249 
250  cdsc->max_bs_buf_size = MAX_BS_BUF;
251 
252  ret = xeve_param_ppt(&cdsc->param, xectx->profile_id, xectx->preset_id, xectx->tune_id);
253  if (XEVE_FAILED(ret)) {
254  av_log(avctx, AV_LOG_ERROR, "Cannot set profile(%d), preset(%d), tune(%d)\n", xectx->profile_id, xectx->preset_id, xectx->tune_id);
255  return AVERROR_EXTERNAL;
256  }
257 
258  return 0;
259 }
260 
261 /**
262  * Set XEVE_CFG_SET_USE_PIC_SIGNATURE for encoder
263  *
264  * @param[in] logger context
265  * @param[in] id XEVE encodec instance identifier
266  * @param[in] ctx the structure stores all the states associated with the instance of Xeve MPEG-5 EVC encoder
267  *
268  * @return 0 on success, negative error code on failure
269  */
270 static int set_extra_config(AVCodecContext *avctx, XEVE id, XeveContext *ctx)
271 {
272  int ret, size;
273  size = 4;
274 
275  // embed SEI messages identifying encoder parameters and command line arguments
276  // - 0: off\n"
277  // - 1: emit sei info"
278  //
279  // SEI - Supplemental enhancement information contains information
280  // that is not necessary to decode the samples of coded pictures from VCL NAL units.
281  // Some SEI message information is required to check bitstream conformance
282  // and for output timing decoder conformance.
283  // @see ISO_IEC_23094-1_2020 7.4.3.5
284  // @see ISO_IEC_23094-1_2020 Annex D
285  ret = xeve_config(id, XEVE_CFG_SET_SEI_CMD, &ctx->sei_info, &size); // sei_cmd_info
286  if (XEVE_FAILED(ret)) {
287  av_log(avctx, AV_LOG_ERROR, "Failed to set config for sei command info messages\n");
288  return AVERROR_EXTERNAL;
289  }
290 
291  ret = xeve_config(id, XEVE_CFG_SET_USE_PIC_SIGNATURE, &ctx->hash, &size);
292  if (XEVE_FAILED(ret)) {
293  av_log(avctx, AV_LOG_ERROR, "Failed to set config for picture signature\n");
294  return AVERROR_EXTERNAL;
295  }
296 
297  return 0;
298 }
299 
300 /**
301  * @brief Switch encoder to bumping mode
302  *
303  * @param id XEVE encodec instance identifier
304  * @return 0 on success, negative error code on failure
305  */
306 static int setup_bumping(XEVE id)
307 {
308  int val = 1;
309  int size = sizeof(int);
310  if (XEVE_FAILED(xeve_config(id, XEVE_CFG_SET_FORCE_OUT, (void *)(&val), &size)))
311  return AVERROR_EXTERNAL;
312 
313  return 0;
314 }
315 
316 /**
317  * @brief Initialize eXtra-fast Essential Video Encoder codec
318  * Create an encoder instance and allocate all the needed resources
319  *
320  * @param avctx codec context
321  * @return 0 on success, negative error code on failure
322  */
324 {
325  XeveContext *xectx = avctx->priv_data;
326  unsigned char *bs_buf = NULL;
327  int i;
328  int shift_h = 0;
329  int shift_v = 0;
330  int width_chroma = 0;
331  int height_chroma = 0;
332  XEVE_IMGB *imgb = NULL;
333  int ret = 0;
334 
335  XEVE_CDSC *cdsc = &(xectx->cdsc);
336 
337  /* allocate bitstream buffer */
338  bs_buf = av_malloc(MAX_BS_BUF);
339  if (bs_buf == NULL) {
340  av_log(avctx, AV_LOG_ERROR, "Cannot allocate bitstream buffer\n");
341  return AVERROR(ENOMEM);
342  }
343  xectx->bitb.addr = bs_buf;
344  xectx->bitb.bsize = MAX_BS_BUF;
345 
346  /* read configurations and set values for created descriptor (XEVE_CDSC) */
347  if ((ret = get_conf(avctx, cdsc)) != 0) {
348  av_log(avctx, AV_LOG_ERROR, "Cannot get configuration\n");
349  return AVERROR(EINVAL);
350  }
351 
352  if ((ret = xeve_param_check(&cdsc->param)) != 0) {
353  av_log(avctx, AV_LOG_ERROR, "Invalid configuration\n");
354  return AVERROR(EINVAL);
355  }
356 
357  {
358  const AVDictionaryEntry *en = NULL;
359  while (en = av_dict_iterate(xectx->xeve_params, en)) {
360  if ((ret = xeve_param_parse(&cdsc->param, en->key, en->value)) < 0) {
361  av_log(avctx, AV_LOG_WARNING,
362  "Error parsing option '%s = %s'.\n",
363  en->key, en->value);
364  }
365  }
366  }
367 
368  /* create encoder */
369  xectx->id = xeve_create(cdsc, NULL);
370  if (xectx->id == NULL) {
371  av_log(avctx, AV_LOG_ERROR, "Cannot create XEVE encoder\n");
372  return AVERROR_EXTERNAL;
373  }
374 
375  if ((ret = set_extra_config(avctx, xectx->id, xectx)) != 0) {
376  av_log(avctx, AV_LOG_ERROR, "Cannot set extra configuration\n");
377  return AVERROR(EINVAL);
378  }
379 
380  if ((ret = av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &shift_h, &shift_v)) != 0) {
381  av_log(avctx, AV_LOG_ERROR, "Failed to get chroma shift\n");
382  return AVERROR(EINVAL);
383  }
384 
385  // Chroma subsampling
386  //
387  // YUV format explanation
388  // shift_h == 1 && shift_v == 1 : YUV420
389  // shift_h == 1 && shift_v == 0 : YUV422
390  // shift_h == 0 && shift_v == 0 : YUV444
391  //
392  width_chroma = AV_CEIL_RSHIFT(avctx->width, shift_h);
393  height_chroma = AV_CEIL_RSHIFT(avctx->height, shift_v);
394 
395  /* set default values for input image buffer */
396  imgb = &xectx->imgb;
397  imgb->cs = libxeve_color_space(avctx->pix_fmt);
398  imgb->np = 3; /* only for yuv420p, yuv420ple */
399 
400  for (i = 0; i < imgb->np; i++)
401  imgb->x[i] = imgb->y[i] = 0;
402 
403  imgb->w[0] = imgb->aw[0] = avctx->width; // width luma
404  imgb->w[1] = imgb->w[2] = imgb->aw[1] = imgb->aw[2] = width_chroma;
405  imgb->h[0] = imgb->ah[0] = avctx->height; // height luma
406  imgb->h[1] = imgb->h[2] = imgb->ah[1] = imgb->ah[2] = height_chroma;
407 
408  xectx->state = STATE_ENCODING;
409 
410  return 0;
411 }
412 
413 /**
414  * Encode raw data frame into EVC packet
415  *
416  * @param[in] avctx codec context
417  * @param[out] avpkt output AVPacket containing encoded data
418  * @param[in] frame AVFrame containing the raw data to be encoded
419  * @param[out] got_packet encoder sets to 0 or 1 to indicate that a
420  * non-empty packet was returned in pkt
421  *
422  * @return 0 on success, negative error code on failure
423  */
424 static int libxeve_encode(AVCodecContext *avctx, AVPacket *avpkt,
425  const AVFrame *frame, int *got_packet)
426 {
427  XeveContext *xectx = avctx->priv_data;
428  int ret = -1;
429 
430  // No more input frames are available but encoder still can have some data in its internal buffer to process
431  // and some frames to dump.
432  if (xectx->state == STATE_ENCODING && frame == NULL) {
433  if (setup_bumping(xectx->id) == 0)
434  xectx->state = STATE_BUMPING; // Entering bumping process
435  else {
436  av_log(avctx, AV_LOG_ERROR, "Failed to setup bumping\n");
437  return 0;
438  }
439  }
440 
441  if (xectx->state == STATE_ENCODING) {
442  int i;
443  XEVE_IMGB *imgb = NULL;
444 
445  imgb = &xectx->imgb;
446 
447  for (i = 0; i < imgb->np; i++) {
448  imgb->a[i] = frame->data[i];
449  imgb->s[i] = frame->linesize[i];
450  }
451 
452  imgb->ts[XEVE_TS_PTS] = frame->pts;
453 
454  /* push image to encoder */
455  ret = xeve_push(xectx->id, imgb);
456  if (XEVE_FAILED(ret)) {
457  av_log(avctx, AV_LOG_ERROR, "xeve_push() failed\n");
458  return AVERROR_EXTERNAL;
459  }
460  }
461  if (xectx->state == STATE_ENCODING || xectx->state == STATE_BUMPING) {
462  /* encoding */
463  ret = xeve_encode(xectx->id, &(xectx->bitb), &(xectx->stat));
464  if (XEVE_FAILED(ret)) {
465  av_log(avctx, AV_LOG_ERROR, "xeve_encode() failed\n");
466  return AVERROR_EXTERNAL;
467  }
468 
469  /* store bitstream */
470  if (ret == XEVE_OK_OUT_NOT_AVAILABLE) { // Return OK but picture is not available yet
471  *got_packet = 0;
472  return 0;
473  } else if (ret == XEVE_OK) {
474  int av_pic_type;
475 
476  if (xectx->stat.write > 0) {
477 
478  ret = ff_get_encode_buffer(avctx, avpkt, xectx->stat.write, 0);
479  if (ret < 0)
480  return ret;
481 
482  memcpy(avpkt->data, xectx->bitb.addr, xectx->stat.write);
483 
484  avpkt->time_base.num = 1;
485  avpkt->time_base.den = xectx->cdsc.param.fps;
486 
487  avpkt->pts = xectx->bitb.ts[XEVE_TS_PTS];
488  avpkt->dts = xectx->bitb.ts[XEVE_TS_DTS];
489 
490  switch(xectx->stat.stype) {
491  case XEVE_ST_I:
492  av_pic_type = AV_PICTURE_TYPE_I;
493  avpkt->flags |= AV_PKT_FLAG_KEY;
494  break;
495  case XEVE_ST_P:
496  av_pic_type = AV_PICTURE_TYPE_P;
497  break;
498  case XEVE_ST_B:
499  av_pic_type = AV_PICTURE_TYPE_B;
500  break;
501  case XEVE_ST_UNKNOWN:
502  av_log(avctx, AV_LOG_ERROR, "Unknown slice type\n");
503  return AVERROR_INVALIDDATA;
504  }
505 
506  ff_side_data_set_encoder_stats(avpkt, xectx->stat.qp * FF_QP2LAMBDA, NULL, 0, av_pic_type);
507 
508  *got_packet = 1;
509  }
510  } else if (ret == XEVE_OK_NO_MORE_FRM) {
511  // Return OK but no more frames
512  return 0;
513  } else {
514  av_log(avctx, AV_LOG_ERROR, "Invalid return value: %d\n", ret);
515  return AVERROR_EXTERNAL;
516  }
517  } else {
518  av_log(avctx, AV_LOG_ERROR, "Udefined encoder state\n");
519  return AVERROR_INVALIDDATA;
520  }
521 
522  return 0;
523 }
524 
525 /**
526  * Destroy the encoder and release all the allocated resources
527  *
528  * @param avctx codec context
529  * @return 0 on success, negative error code on failure
530  */
532 {
533  XeveContext *xectx = avctx->priv_data;
534 
535  if (xectx->id) {
536  xeve_delete(xectx->id);
537  xectx->id = NULL;
538  }
539 
540  av_free(xectx->bitb.addr); /* release bitstream buffer */
541 
542  return 0;
543 }
544 
545 #define OFFSET(x) offsetof(XeveContext, x)
546 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
547 
548 static const enum AVPixelFormat supported_pixel_formats[] = {
552 };
553 
554 // Consider using following options (./ffmpeg --help encoder=libxeve)
555 //
556 static const AVOption libxeve_options[] = {
557  { "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" },
558  { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_DEFAULT }, INT_MIN, INT_MAX, VE, .unit = "preset" },
559  { "fast", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_FAST }, INT_MIN, INT_MAX, VE, .unit = "preset" },
560  { "medium", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_MEDIUM }, INT_MIN, INT_MAX, VE, .unit = "preset" },
561  { "slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_SLOW }, INT_MIN, INT_MAX, VE, .unit = "preset" },
562  { "placebo", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PRESET_PLACEBO }, INT_MIN, INT_MAX, VE, .unit = "preset" },
563  { "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"},
564  { "none", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_NONE }, INT_MIN, INT_MAX, VE, .unit = "tune" },
565  { "zerolatency", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_ZEROLATENCY }, INT_MIN, INT_MAX, VE, .unit = "tune" },
566  { "psnr", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_TUNE_PSNR }, INT_MIN, INT_MAX, VE, .unit = "tune" },
567  { "profile", "Encoding profile", OFFSET(profile_id), AV_OPT_TYPE_INT, { .i64 = XEVE_PROFILE_BASELINE }, XEVE_PROFILE_BASELINE, XEVE_PROFILE_MAIN, VE, .unit = "profile" },
568  { "baseline", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_BASELINE }, INT_MIN, INT_MAX, VE, .unit = "profile" },
569  { "main", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_PROFILE_MAIN }, INT_MIN, INT_MAX, VE, .unit = "profile" },
570  { "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" },
571  { "CQP", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CQP }, INT_MIN, INT_MAX, VE, .unit = "rc_mode" },
572  { "ABR", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_ABR }, INT_MIN, INT_MAX, VE, .unit = "rc_mode" },
573  { "CRF", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = XEVE_RC_CRF }, INT_MIN, INT_MAX, VE, .unit = "rc_mode" },
574  { "qp", "Quantization parameter value for CQP rate control mode", OFFSET(qp), AV_OPT_TYPE_INT, { .i64 = 32 }, 0, 51, VE },
575  { "crf", "Constant rate factor value for CRF rate control mode", OFFSET(crf), AV_OPT_TYPE_INT, { .i64 = 32 }, 10, 49, VE },
576  { "hash", "Embed picture signature (HASH) for conformance checking in decoding", OFFSET(hash), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
577  { "sei_info", "Embed SEI messages identifying encoder parameters and command line arguments", OFFSET(sei_info), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
578  { "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 },
579  { NULL }
580 };
581 
582 static const AVClass libxeve_class = {
583  .class_name = "libxeve",
584  .item_name = av_default_item_name,
585  .option = libxeve_options,
586  .version = LIBAVUTIL_VERSION_INT,
587 };
588 
589 /**
590  * libavcodec generic global options, which can be set on all the encoders and decoders
591  * @see https://www.ffmpeg.org/ffmpeg-codecs.html#Codec-Options
592  */
594  { "b", "0" }, // bitrate in terms of kilo-bits per second
595  { "g", "0" }, // gop_size (key-frame interval 0: only one I-frame at the first time; 1: every frame is coded in I-frame)
596  { "bf", "15"}, // maximum number of B frames (0: no B-frames, 1,3,7,15)
597  { "threads", "0"}, // number of threads to be used (0: automatically select the number of threads to set)
598  { NULL },
599 };
600 
602  .p.name = "libxeve",
603  .p.long_name = NULL_IF_CONFIG_SMALL("libxeve MPEG-5 EVC"),
604  .p.type = AVMEDIA_TYPE_VIDEO,
605  .p.id = AV_CODEC_ID_EVC,
606  .init = libxeve_init,
608  .close = libxeve_close,
609  .priv_data_size = sizeof(XeveContext),
610  .p.priv_class = &libxeve_class,
611  .defaults = libxeve_defaults,
613  .p.profiles = NULL_IF_CONFIG_SMALL(ff_evc_profiles),
614  .p.wrapper_name = "libxeve",
615  .p.pix_fmts = supported_pixel_formats,
617 };
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:186
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: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
libxeve_class
static const AVClass libxeve_class
Definition: libxeve.c:582
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:374
pixdesc.h
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:524
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:126
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:579
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:88
FFCodecDefault
Definition: codec_internal.h:96
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
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:2993
FF_CODEC_ENCODE_CB
#define FF_CODEC_ENCODE_CB(func)
Definition: codec_internal.h:295
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:270
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:59
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:198
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:1292
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:80
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:46
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:104
XeveContext::cdsc
XEVE_CDSC cdsc
Definition: libxeve.c:72
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1783
State
State
Encoder states.
Definition: libxeve.c:60
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: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:1031
codec_internal.h
cpu.h
libxeve_options
static const AVOption libxeve_options[]
Definition: libxeve.c:556
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:424
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:523
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:530
XeveContext::id
XEVE id
Definition: libxeve.c:71
XeveContext::bitb
XEVE_BITB bitb
Definition: libxeve.c:73
lrintf
#define lrintf(x)
Definition: libm_mips.h:72
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:517
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:546
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: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
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:531
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: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
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:306
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:323
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:795
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:501
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
OFFSET
#define OFFSET(x)
Definition: libxeve.c:545
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
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:607
supported_pixel_formats
static enum AVPixelFormat supported_pixel_formats[]
Definition: libxeve.c:548
libxeve_defaults
static const FFCodecDefault libxeve_defaults[]
libavcodec generic global options, which can be set on all the encoders and decoders
Definition: libxeve.c:593
AVDictionaryEntry::value
char * value
Definition: dict.h:91
avstring.h
ff_libxeve_encoder
const FFCodec ff_libxeve_encoder
Definition: libxeve.c:601
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:142
int
int
Definition: ffmpeg_filter.c:424
AV_OPT_TYPE_CONST
@ AV_OPT_TYPE_CONST
Definition: opt.h:244
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:568