FFmpeg
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 "encode.h"
27 #include "mpeg12.h"
28 #include "libavutil/avstring.h"
29 
30 #define xavs2_opt_set2(name, format, ...) do{ \
31  char opt_str[16] = {0}; \
32  int err; \
33  av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \
34  err = cae->api->opt_set2(cae->param, name, opt_str); \
35  if (err < 0) {\
36  av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str);\
37  }\
38 } while(0);
39 
40 typedef struct XAVS2EContext {
41  AVClass *class;
42 
45  int qp;
46  int max_qp;
47  int min_qp;
49  int log_level;
50 
51  void *encoder;
53 
54  xavs2_outpacket_t packet;
55  xavs2_param_t *param;
56 
57  const xavs2_api_t *api;
58 
60 
61 static av_cold int xavs2_init(AVCodecContext *avctx)
62 {
63  XAVS2EContext *cae = avctx->priv_data;
64  int bit_depth, code;
65 
66  bit_depth = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 8 : 10;
67 
68  /* get API handler */
69  cae->api = xavs2_api_get(bit_depth);
70  if (!cae->api) {
71  av_log(avctx, AV_LOG_ERROR, "Failed to get xavs2 api context\n");
72  return AVERROR_EXTERNAL;
73  }
74 
75  cae->param = cae->api->opt_alloc();
76  if (!cae->param) {
77  av_log(avctx, AV_LOG_ERROR, "Failed to alloc xavs2 parameters\n");
78  return AVERROR(ENOMEM);
79  }
80 
81  xavs2_opt_set2("Width", "%d", avctx->width);
82  xavs2_opt_set2("Height", "%d", avctx->height);
83  xavs2_opt_set2("BFrames", "%d", avctx->max_b_frames);
84  xavs2_opt_set2("BitDepth", "%d", bit_depth);
85  xavs2_opt_set2("Log", "%d", cae->log_level);
86  xavs2_opt_set2("Preset", "%d", cae->preset_level);
87 
88  xavs2_opt_set2("IntraPeriodMax", "%d", avctx->gop_size);
89  xavs2_opt_set2("IntraPeriodMin", "%d", avctx->gop_size);
90 
91  xavs2_opt_set2("ThreadFrames", "%d", avctx->thread_count);
92  xavs2_opt_set2("ThreadRows", "%d", cae->lcu_row_threads);
93 
94  xavs2_opt_set2("OpenGOP", "%d", !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
95 
96  {
97  AVDictionaryEntry *en = NULL;
98  while ((en = av_dict_get(cae->xavs2_opts, "", en, AV_DICT_IGNORE_SUFFIX)))
99  xavs2_opt_set2(en->key, "%s", en->value);
100  }
101 
102  /* Rate control */
103  if (avctx->bit_rate > 0) {
104  xavs2_opt_set2("RateControl", "%d", 1);
105  xavs2_opt_set2("TargetBitRate", "%"PRId64"", avctx->bit_rate);
106  xavs2_opt_set2("InitialQP", "%d", cae->initial_qp);
107  xavs2_opt_set2("MaxQP", "%d", avctx->qmax >= 0 ? avctx->qmax : cae->max_qp);
108  xavs2_opt_set2("MinQP", "%d", avctx->qmin >= 0 ? avctx->qmin : cae->min_qp);
109  } else {
110  xavs2_opt_set2("InitialQP", "%d", cae->qp);
111  }
112 
114  xavs2_opt_set2("FrameRate", "%d", code);
115 
116  cae->encoder = cae->api->encoder_create(cae->param);
117 
118  if (!cae->encoder) {
119  av_log(avctx, AV_LOG_ERROR, "Failed to create xavs2 encoder instance.\n");
120  return AVERROR(EINVAL);
121  }
122 
123  return 0;
124 }
125 
126 static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
127 {
128  uint16_t *p_plane;
129  uint8_t *p_buffer;
130  int plane;
131  int hIdx;
132  int wIdx;
133 
134  for (plane = 0; plane < 3; plane++) {
135  p_plane = (uint16_t *)pic->img.img_planes[plane];
136  p_buffer = frame->data[plane];
137  for (hIdx = 0; hIdx < pic->img.i_lines[plane]; hIdx++) {
138  memset(p_plane, 0, pic->img.i_stride[plane]);
139  for (wIdx = 0; wIdx < pic->img.i_width[plane]; wIdx++) {
140  p_plane[wIdx] = p_buffer[wIdx] << shift_in;
141  }
142  p_plane += pic->img.i_stride[plane];
143  p_buffer += frame->linesize[plane];
144  }
145  }
146 }
147 
148 static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
149 {
150  uint8_t *p_plane;
151  uint8_t *p_buffer;
152  int plane;
153  int hIdx;
154  int stride;
155 
156  for (plane = 0; plane < 3; plane++) {
157  p_plane = pic->img.img_planes[plane];
158  p_buffer = frame->data[plane];
159  stride = pic->img.i_width[plane] * pic->img.in_sample_size;
160  for (hIdx = 0; hIdx < pic->img.i_lines[plane]; hIdx++) {
161  memcpy(p_plane, p_buffer, stride);
162  p_plane += pic->img.i_stride[plane];
163  p_buffer += frame->linesize[plane];
164  }
165  }
166 }
167 
169  const AVFrame *frame, int *got_packet)
170 {
171  XAVS2EContext *cae = avctx->priv_data;
172  xavs2_picture_t pic;
173  int ret;
174 
175  /* create the XAVS2 video encoder */
176  /* read frame data and send to the XAVS2 video encoder */
177  if (cae->api->encoder_get_buffer(cae->encoder, &pic) < 0) {
178  av_log(avctx, AV_LOG_ERROR, "Failed to get xavs2 frame buffer\n");
179  return AVERROR_EXTERNAL;
180  }
181  if (frame) {
182  switch (frame->format) {
183  case AV_PIX_FMT_YUV420P:
184  if (pic.img.in_sample_size == pic.img.enc_sample_size) {
185  xavs2_copy_frame(&pic, frame);
186  } else {
187  const int shift_in = atoi(cae->api->opt_get(cae->param, "SampleShift"));
188  xavs2_copy_frame_with_shift(&pic, frame, shift_in);
189  }
190  break;
192  if (pic.img.in_sample_size == pic.img.enc_sample_size) {
193  xavs2_copy_frame(&pic, frame);
194  break;
195  }
196  default:
197  av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
198  return AVERROR(EINVAL);
199  break;
200  }
201 
202  pic.i_state = 0;
203  pic.i_pts = frame->pts;
204  pic.i_type = XAVS2_TYPE_AUTO;
205 
206  ret = cae->api->encoder_encode(cae->encoder, &pic, &cae->packet);
207 
208  if (ret) {
209  av_log(avctx, AV_LOG_ERROR, "Encoding error occured.\n");
210  return AVERROR_EXTERNAL;
211  }
212 
213  } else {
214  cae->api->encoder_encode(cae->encoder, NULL, &cae->packet);
215  }
216 
217  if ((cae->packet.len) && (cae->packet.state != XAVS2_STATE_FLUSH_END)) {
218  if ((ret = ff_get_encode_buffer(avctx, pkt, cae->packet.len, 0)) < 0) {
219  cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
220  return ret;
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 
234  cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
235 
236  *got_packet = 1;
237  } else {
238  *got_packet = 0;
239  }
240 
241  return 0;
242 }
243 
245 {
246  XAVS2EContext *cae = avctx->priv_data;
247  /* destroy the encoder */
248  if (cae->api) {
249  cae->api->encoder_destroy(cae->encoder);
250 
251  if (cae->param) {
252  cae->api->opt_destroy(cae->param);
253  }
254  }
255  return 0;
256 }
257 
258 #define OFFSET(x) offsetof(XAVS2EContext, x)
259 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
260 
261 static const AVOption options[] = {
262  { "lcu_row_threads" , "number of parallel threads for rows" , OFFSET(lcu_row_threads) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, VE },
263  { "initial_qp" , "Quantization initial parameter" , OFFSET(initial_qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
264  { "qp" , "Quantization parameter" , OFFSET(qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
265  { "max_qp" , "max qp for rate control" , OFFSET(max_qp) , AV_OPT_TYPE_INT, {.i64 = 55 }, 0, 63, VE },
266  { "min_qp" , "min qp for rate control" , OFFSET(min_qp) , AV_OPT_TYPE_INT, {.i64 = 20 }, 0, 63, VE },
267  { "speed_level" , "Speed level, higher is better but slower", OFFSET(preset_level) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 9, VE },
268  { "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 },
269  { "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 },
270  { NULL },
271 };
272 
273 static const AVClass libxavs2 = {
274  .class_name = "XAVS2EContext",
275  .item_name = av_default_item_name,
276  .option = options,
277  .version = LIBAVUTIL_VERSION_INT,
278 };
279 
280 static const AVCodecDefault xavs2_defaults[] = {
281  { "b", "0" },
282  { "g", "48"},
283  { "bf", "7" },
284  { NULL },
285 };
286 
288  .name = "libxavs2",
289  .long_name = NULL_IF_CONFIG_SMALL("libxavs2 AVS2-P2/IEEE1857.4"),
290  .type = AVMEDIA_TYPE_VIDEO,
291  .id = AV_CODEC_ID_AVS2,
292  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
294  .priv_data_size = sizeof(XAVS2EContext),
295  .init = xavs2_init,
296  .encode2 = xavs2_encode_frame,
297  .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 } ;
xavs2_copy_frame
static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
Definition: libxavs2.c:148
AVCodec
AVCodec.
Definition: codec.h:197
stride
int stride
Definition: mace.c:144
bit_depth
static void bit_depth(AudioStatsContext *s, uint64_t mask, uint64_t imask, AVRational *depth)
Definition: af_astats.c:247
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
init
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:31
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
XAVS2EContext::initial_qp
int initial_qp
Definition: libxavs2.c:44
XAVS2EContext::min_qp
int min_qp
Definition: libxavs2.c:47
XAVS2EContext::lcu_row_threads
int lcu_row_threads
Definition: libxavs2.c:43
xavs2_copy_frame_with_shift
static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
Definition: libxavs2.c:126
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:303
AVPacket::data
uint8_t * data
Definition: packet.h:373
AVOption
AVOption.
Definition: opt.h:247
encode.h
AV_CODEC_ID_AVS2
@ AV_CODEC_ID_AVS2
Definition: codec_id.h:244
XAVS2EContext::xavs2_opts
AVDictionary * xavs2_opts
Definition: libxavs2.c:52
AV_PIX_FMT_YUV420P10
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:389
AV_DICT_IGNORE_SUFFIX
#define AV_DICT_IGNORE_SUFFIX
Return first entry in a dictionary whose first part corresponds to the search key,...
Definition: dict.h:68
libxavs2
static const AVClass libxavs2
Definition: libxavs2.c:273
AVDictionary
Definition: dict.c:30
XAVS2EContext::encoder
void * encoder
Definition: libxavs2.c:51
AVCodecContext::qmax
int qmax
maximum quantizer
Definition: avcodec.h:1161
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:425
ff_mpeg12_find_best_frame_rate
void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, int *code, int *ext_n, int *ext_d, int nonstandard)
Definition: mpeg12framerate.c:44
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1706
XAVS2EContext::log_level
int log_level
Definition: libxavs2.c:49
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1436
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:459
XAVS2EContext::api
const xavs2_api_t * api
Definition: libxavs2.c:57
mpeg12.h
pkt
AVPacket * pkt
Definition: movenc.c:59
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_dict_get
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
XAVS2EContext
Definition: libxavs2.c:40
AVDictionaryEntry::key
char * key
Definition: dict.h:80
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:122
pix_fmts
static enum AVPixelFormat pix_fmts[]
Definition: libkvazaar.c:290
XAVS2EContext::packet
xavs2_outpacket_t packet
Definition: libxavs2.c:54
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:66
XAVS2EContext::preset_level
int preset_level
Definition: libxavs2.c:48
ff_libxavs2_encoder
const AVCodec ff_libxavs2_encoder
Definition: libxavs2.c:287
AVCodecDefault
Definition: internal.h:206
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
xavs2_encode_frame
static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition: libxavs2.c:168
XAVS2EContext::qp
int qp
Definition: libxavs2.c:45
xavs2_init
static av_cold int xavs2_init(AVCodecContext *avctx)
Definition: libxavs2.c:61
VE
#define VE
Definition: libxavs2.c:259
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:429
AV_OPT_TYPE_DICT
@ AV_OPT_TYPE_DICT
Definition: opt.h:231
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:235
for
for(j=16;j >0;--j)
Definition: h264pred_template.c:469
FF_CODEC_CAP_AUTO_THREADS
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
Definition: internal.h:81
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
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:117
AVCodecContext::gop_size
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition: avcodec.h:574
xavs2_defaults
static const AVCodecDefault xavs2_defaults[]
Definition: libxavs2.c:280
xavs2_opt_set2
#define xavs2_opt_set2(name, format,...)
Definition: libxavs2.c:30
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:372
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:379
XAVS2EContext::param
xavs2_param_t * param
Definition: libxavs2.c:55
XAVS2EContext::max_qp
int max_qp
Definition: libxavs2.c:46
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:366
code
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some it can consider them to be part of the FIFO and delay acknowledging a status change accordingly Example code
Definition: filter_design.txt:178
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
AVCodecContext::height
int height
Definition: avcodec.h:552
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:589
OFFSET
#define OFFSET(x)
Definition: libxavs2.c:258
AV_CODEC_FLAG_CLOSED_GOP
#define AV_CODEC_FLAG_CLOSED_GOP
Definition: avcodec.h:278
ret
ret
Definition: filter_design.txt:187
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
AVCodecContext
main external API structure.
Definition: avcodec.h:379
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:78
AVCodecContext::qmin
int qmin
minimum quantizer
Definition: avcodec.h:1154
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:224
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:77
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:651
AVDictionaryEntry
Definition: dict.h:79
AVPacket
This structure stores compressed data.
Definition: packet.h:350
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:406
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:552
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVDictionaryEntry::value
char * value
Definition: dict.h:81
avstring.h
options
static const AVOption options[]
Definition: libxavs2.c:261
xavs2_close
static av_cold int xavs2_close(AVCodecContext *avctx)
Definition: libxavs2.c:244