FFmpeg  4.4
libxavs2.c
Go to the documentation of this file.
1 /*
2  * AVS2 encoding using the xavs2 library
3  *
4  * Copyright (C) 2018 Yiqun Xu, <yiqun.xu@vipl.ict.ac.cn>
5  * Falei Luo, <falei.luo@gmail.com>
6  * Huiwen Ren, <hwrenx@gmail.com>
7  *
8  * This file is part of FFmpeg.
9  *
10  * FFmpeg is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * FFmpeg is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with FFmpeg; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  */
24 
25 #include "xavs2.h"
26 #include "mpeg12.h"
27 #include "libavutil/avstring.h"
28 
29 #define xavs2_opt_set2(name, format, ...) do{ \
30  char opt_str[16] = {0}; \
31  int err; \
32  av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \
33  err = cae->api->opt_set2(cae->param, name, opt_str); \
34  if (err < 0) {\
35  av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str);\
36  }\
37 } while(0);
38 
39 typedef struct XAVS2EContext {
40  AVClass *class;
41 
44  int qp;
45  int max_qp;
46  int min_qp;
48  int log_level;
49 
50  void *encoder;
52 
53  xavs2_outpacket_t packet;
54  xavs2_param_t *param;
55 
56  const xavs2_api_t *api;
57 
59 
60 static av_cold int xavs2_init(AVCodecContext *avctx)
61 {
62  XAVS2EContext *cae = avctx->priv_data;
63  int bit_depth, code;
64 
65  bit_depth = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 8 : 10;
66 
67  /* get API handler */
68  cae->api = xavs2_api_get(bit_depth);
69  if (!cae->api) {
70  av_log(avctx, AV_LOG_ERROR, "Failed to get xavs2 api context\n");
71  return AVERROR_EXTERNAL;
72  }
73 
74  cae->param = cae->api->opt_alloc();
75  if (!cae->param) {
76  av_log(avctx, AV_LOG_ERROR, "Failed to alloc xavs2 parameters\n");
77  return AVERROR(ENOMEM);
78  }
79 
80  xavs2_opt_set2("Width", "%d", avctx->width);
81  xavs2_opt_set2("Height", "%d", avctx->height);
82  xavs2_opt_set2("BFrames", "%d", avctx->max_b_frames);
83  xavs2_opt_set2("BitDepth", "%d", bit_depth);
84  xavs2_opt_set2("Log", "%d", cae->log_level);
85  xavs2_opt_set2("Preset", "%d", cae->preset_level);
86 
87  xavs2_opt_set2("IntraPeriodMax", "%d", avctx->gop_size);
88  xavs2_opt_set2("IntraPeriodMin", "%d", avctx->gop_size);
89 
90  xavs2_opt_set2("ThreadFrames", "%d", avctx->thread_count);
91  xavs2_opt_set2("ThreadRows", "%d", cae->lcu_row_threads);
92 
93  xavs2_opt_set2("OpenGOP", "%d", !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
94 
95  {
96  AVDictionaryEntry *en = NULL;
97  while ((en = av_dict_get(cae->xavs2_opts, "", en, AV_DICT_IGNORE_SUFFIX)))
98  xavs2_opt_set2(en->key, "%s", en->value);
99  }
100 
101  /* Rate control */
102  if (avctx->bit_rate > 0) {
103  xavs2_opt_set2("RateControl", "%d", 1);
104  xavs2_opt_set2("TargetBitRate", "%"PRId64"", avctx->bit_rate);
105  xavs2_opt_set2("InitialQP", "%d", cae->initial_qp);
106  xavs2_opt_set2("MaxQP", "%d", avctx->qmax >= 0 ? avctx->qmax : cae->max_qp);
107  xavs2_opt_set2("MinQP", "%d", avctx->qmin >= 0 ? avctx->qmin : cae->min_qp);
108  } else {
109  xavs2_opt_set2("InitialQP", "%d", cae->qp);
110  }
111 
113  xavs2_opt_set2("FrameRate", "%d", code);
114 
115  cae->encoder = cae->api->encoder_create(cae->param);
116 
117  if (!cae->encoder) {
118  av_log(avctx, AV_LOG_ERROR, "Failed to create xavs2 encoder instance.\n");
119  return AVERROR(EINVAL);
120  }
121 
122  return 0;
123 }
124 
125 static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
126 {
127  uint16_t *p_plane;
128  uint8_t *p_buffer;
129  int plane;
130  int hIdx;
131  int wIdx;
132 
133  for (plane = 0; plane < 3; plane++) {
134  p_plane = (uint16_t *)pic->img.img_planes[plane];
135  p_buffer = frame->data[plane];
136  for (hIdx = 0; hIdx < pic->img.i_lines[plane]; hIdx++) {
137  memset(p_plane, 0, pic->img.i_stride[plane]);
138  for (wIdx = 0; wIdx < pic->img.i_width[plane]; wIdx++) {
139  p_plane[wIdx] = p_buffer[wIdx] << shift_in;
140  }
141  p_plane += pic->img.i_stride[plane];
142  p_buffer += frame->linesize[plane];
143  }
144  }
145 }
146 
147 static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
148 {
149  uint8_t *p_plane;
150  uint8_t *p_buffer;
151  int plane;
152  int hIdx;
153  int stride;
154 
155  for (plane = 0; plane < 3; plane++) {
156  p_plane = pic->img.img_planes[plane];
157  p_buffer = frame->data[plane];
158  stride = pic->img.i_width[plane] * pic->img.in_sample_size;
159  for (hIdx = 0; hIdx < pic->img.i_lines[plane]; hIdx++) {
160  memcpy(p_plane, p_buffer, stride);
161  p_plane += pic->img.i_stride[plane];
162  p_buffer += frame->linesize[plane];
163  }
164  }
165 }
166 
168  const AVFrame *frame, int *got_packet)
169 {
170  XAVS2EContext *cae = avctx->priv_data;
171  xavs2_picture_t pic;
172  int ret;
173 
174  /* create the XAVS2 video encoder */
175  /* read frame data and send to the XAVS2 video encoder */
176  if (cae->api->encoder_get_buffer(cae->encoder, &pic) < 0) {
177  av_log(avctx, AV_LOG_ERROR, "Failed to get xavs2 frame buffer\n");
178  return AVERROR_EXTERNAL;
179  }
180  if (frame) {
181  switch (frame->format) {
182  case AV_PIX_FMT_YUV420P:
183  if (pic.img.in_sample_size == pic.img.enc_sample_size) {
184  xavs2_copy_frame(&pic, frame);
185  } else {
186  const int shift_in = atoi(cae->api->opt_get(cae->param, "SampleShift"));
187  xavs2_copy_frame_with_shift(&pic, frame, shift_in);
188  }
189  break;
191  if (pic.img.in_sample_size == pic.img.enc_sample_size) {
192  xavs2_copy_frame(&pic, frame);
193  break;
194  }
195  default:
196  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
197  return AVERROR(EINVAL);
198  break;
199  }
200 
201  pic.i_state = 0;
202  pic.i_pts = frame->pts;
203  pic.i_type = XAVS2_TYPE_AUTO;
204 
205  ret = cae->api->encoder_encode(cae->encoder, &pic, &cae->packet);
206 
207  if (ret) {
208  av_log(avctx, AV_LOG_ERROR, "Encoding error occured.\n");
209  return AVERROR_EXTERNAL;
210  }
211 
212  } else {
213  cae->api->encoder_encode(cae->encoder, NULL, &cae->packet);
214  }
215 
216  if ((cae->packet.len) && (cae->packet.state != XAVS2_STATE_FLUSH_END)) {
217  if (av_new_packet(pkt, cae->packet.len) < 0) {
218  av_log(avctx, AV_LOG_ERROR, "Failed to alloc xavs2 packet.\n");
219  cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
220  return AVERROR(ENOMEM);
221  }
222 
223  pkt->pts = cae->packet.pts;
224  pkt->dts = cae->packet.dts;
225 
226  if (cae->packet.type == XAVS2_TYPE_IDR ||
227  cae->packet.type == XAVS2_TYPE_I ||
228  cae->packet.type == XAVS2_TYPE_KEYFRAME) {
230  }
231 
232  memcpy(pkt->data, cae->packet.stream, cae->packet.len);
233  pkt->size = cae->packet.len;
234 
235  cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
236 
237  *got_packet = 1;
238  } else {
239  *got_packet = 0;
240  }
241 
242  return 0;
243 }
244 
246 {
247  XAVS2EContext *cae = avctx->priv_data;
248  /* destroy the encoder */
249  if (cae->api) {
250  cae->api->encoder_destroy(cae->encoder);
251 
252  if (cae->param) {
253  cae->api->opt_destroy(cae->param);
254  }
255  }
256  return 0;
257 }
258 
259 #define OFFSET(x) offsetof(XAVS2EContext, x)
260 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
261 
262 static const AVOption options[] = {
263  { "lcu_row_threads" , "number of parallel threads for rows" , OFFSET(lcu_row_threads) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, VE },
264  { "initial_qp" , "Quantization initial parameter" , OFFSET(initial_qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
265  { "qp" , "Quantization parameter" , OFFSET(qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
266  { "max_qp" , "max qp for rate control" , OFFSET(max_qp) , AV_OPT_TYPE_INT, {.i64 = 55 }, 0, 63, VE },
267  { "min_qp" , "min qp for rate control" , OFFSET(min_qp) , AV_OPT_TYPE_INT, {.i64 = 20 }, 0, 63, VE },
268  { "speed_level" , "Speed level, higher is better but slower", OFFSET(preset_level) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 9, VE },
269  { "log_level" , "log level: -1: none, 0: error, 1: warning, 2: info, 3: debug", OFFSET(log_level) , AV_OPT_TYPE_INT, {.i64 = 0 }, -1, 3, VE },
270  { "xavs2-params" , "set the xavs2 configuration using a :-separated list of key=value parameters", OFFSET(xavs2_opts), AV_OPT_TYPE_DICT, { 0 }, 0, 0, VE },
271  { NULL },
272 };
273 
274 static const AVClass libxavs2 = {
275  .class_name = "XAVS2EContext",
276  .item_name = av_default_item_name,
277  .option = options,
278  .version = LIBAVUTIL_VERSION_INT,
279 };
280 
281 static const AVCodecDefault xavs2_defaults[] = {
282  { "b", "0" },
283  { "g", "48"},
284  { "bf", "7" },
285  { NULL },
286 };
287 
289  .name = "libxavs2",
290  .long_name = NULL_IF_CONFIG_SMALL("libxavs2 AVS2-P2/IEEE1857.4"),
291  .type = AVMEDIA_TYPE_VIDEO,
292  .id = AV_CODEC_ID_AVS2,
293  .priv_data_size = sizeof(XAVS2EContext),
294  .init = xavs2_init,
295  .encode2 = xavs2_encode_frame,
296  .close = xavs2_close,
298  .caps_internal = FF_CODEC_CAP_AUTO_THREADS,
299  .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P,
300  AV_PIX_FMT_NONE },
301  .priv_class = &libxavs2,
302  .defaults = xavs2_defaults,
303  .wrapper_name = "libxavs2",
304 } ;
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:254
#define av_cold
Definition: attributes.h:88
uint8_t
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
#define NULL
Definition: coverity.c:32
static AVFrame * frame
@ AV_OPT_TYPE_INT
Definition: opt.h:225
@ AV_OPT_TYPE_DICT
Definition: opt.h:232
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition: codec.h:122
#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:77
#define AV_CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:343
@ AV_CODEC_ID_AVS2
Definition: codec_id.h:243
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:99
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:70
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition: error.h:57
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
for(j=16;j >0;--j)
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: internal.h:80
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:117
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:303
static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
Definition: libxavs2.c:147
AVCodec ff_libxavs2_encoder
Definition: libxavs2.c:288
static const AVOption options[]
Definition: libxavs2.c:262
#define VE
Definition: libxavs2.c:260
static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
Definition: libxavs2.c:125
static av_cold int xavs2_init(AVCodecContext *avctx)
Definition: libxavs2.c:60
#define xavs2_opt_set2(name, format,...)
Definition: libxavs2.c:29
static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libxavs2.c:167
static av_cold int xavs2_close(AVCodecContext *avctx)
Definition: libxavs2.c:245
#define OFFSET(x)
Definition: libxavs2.c:259
static const AVCodecDefault xavs2_defaults[]
Definition: libxavs2.c:281
static const AVClass libxavs2
Definition: libxavs2.c:274
int stride
Definition: mace.c:144
void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, int *code, int *ext_n, int *ext_d, int nonstandard)
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:399
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:66
const uint8_t * code
Definition: spdifenc.c:413
Describe the class of an AVClass context structure.
Definition: log.h:67
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:72
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int width
picture width / height.
Definition: avcodec.h:709
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:796
int qmin
minimum quantizer
Definition: avcodec.h:1380
AVRational framerate
Definition: avcodec.h:2071
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:731
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1773
int qmax
maximum quantizer
Definition: avcodec.h:1387
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
void * priv_data
Definition: avcodec.h:563
AVCodec.
Definition: codec.h:197
const char * name
Name of the codec implementation.
Definition: codec.h:204
char * key
Definition: dict.h:82
char * value
Definition: dict.h:83
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:411
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
AVOption.
Definition: opt.h:248
This structure stores compressed data.
Definition: packet.h:346
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
int size
Definition: packet.h:370
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
uint8_t * data
Definition: packet.h:369
const xavs2_api_t * api
Definition: libxavs2.c:56
xavs2_outpacket_t packet
Definition: libxavs2.c:53
int initial_qp
Definition: libxavs2.c:43
xavs2_param_t * param
Definition: libxavs2.c:54
void * encoder
Definition: libxavs2.c:50
AVDictionary * xavs2_opts
Definition: libxavs2.c:51
int lcu_row_threads
Definition: libxavs2.c:42
int log_level
Definition: libxavs2.c:48
int preset_level
Definition: libxavs2.c:47
#define av_log(a,...)
AVPacket * pkt
Definition: movenc.c:59