2 * AVS2 encoding using the xavs2 library
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>
8 * This file is part of FFmpeg.
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.
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.
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
27 #include "libavutil/avstring.h"
29 #define xavs2_opt_set2(name, format, ...) do{ \
30 char opt_str[16] = {0}; \
32 av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \
33 err = cae->api->opt_set2(cae->param, name, opt_str); \
35 av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str);\
39 typedef struct XAVS2EContext {
49 int hierarchical_reference;
54 xavs2_outpacket_t packet;
57 const xavs2_api_t *api;
61 static av_cold int xavs2_init(AVCodecContext *avctx)
63 XAVS2EContext *cae= avctx->priv_data;
66 bit_depth = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 8 : 10;
69 cae->api = xavs2_api_get(bit_depth);
71 av_log(avctx, AV_LOG_ERROR, "api get failed\n");
72 return AVERROR_EXTERNAL;
75 cae->param = cae->api->opt_alloc();
77 av_log(avctx, AV_LOG_ERROR, "param alloc failed\n");
78 return AVERROR(ENOMEM);
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);
88 xavs2_opt_set2("IntraPeriodMax", "%d", avctx->gop_size);
89 xavs2_opt_set2("IntraPeriodMin", "%d", avctx->gop_size);
91 xavs2_opt_set2("ThreadFrames", "%d", avctx->thread_count);
92 xavs2_opt_set2("ThreadRows", "%d", cae->lcu_row_threads);
94 xavs2_opt_set2("OpenGOP", "%d", !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
96 if (cae->xavs2_opts) {
97 AVDictionary *dict = NULL;
98 AVDictionaryEntry *en = NULL;
100 if (!av_dict_parse_string(&dict, cae->xavs2_opts, "=", ":", 0)) {
101 while ((en = av_dict_get(dict, "", en, AV_DICT_IGNORE_SUFFIX))) {
102 xavs2_opt_set2(en->key, "%s", en->value);
109 if (avctx->bit_rate > 0) {
110 xavs2_opt_set2("RateControl", "%d", 1);
111 xavs2_opt_set2("TargetBitRate", "%"PRId64"", avctx->bit_rate);
112 xavs2_opt_set2("InitialQP", "%d", cae->initial_qp);
113 xavs2_opt_set2("MaxQP", "%d", cae->max_qp);
114 xavs2_opt_set2("MinQP", "%d", cae->min_qp);
116 xavs2_opt_set2("InitialQP", "%d", cae->qp);
120 ff_mpeg12_find_best_frame_rate(avctx->framerate, &code, NULL, NULL, 0);
122 xavs2_opt_set2("FrameRate", "%d", code);
124 cae->encoder = cae->api->encoder_create(cae->param);
127 av_log(avctx,AV_LOG_ERROR, "Can not create encoder. Null pointer returned\n");
128 return AVERROR(EINVAL);
134 static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
137 for (k = 0; k < 3; k++) {
138 int i_stride = pic->img.i_stride[k];
139 for (j = 0; j < pic->img.i_lines[k]; j++) {
140 uint16_t *p_plane = (uint16_t *)&pic->img.img_planes[k][j * i_stride];
142 uint8_t *p_buffer = frame->data[k] + frame->linesize[k] * j;
143 memset(p_plane, 0, i_stride);
144 for (i = 0; i < pic->img.i_width[k]; i++) {
145 p_plane[i] = p_buffer[i] << shift_in;
151 static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
154 for (k = 0; k < 3; k++) {
155 for (j = 0; j < pic->img.i_lines[k]; j++) {
156 memcpy( pic->img.img_planes[k] + pic->img.i_stride[k] * j,
157 frame->data[k]+frame->linesize[k] * j,
158 pic->img.i_width[k] * pic->img.in_sample_size);
163 static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
164 const AVFrame *frame, int *got_packet)
166 XAVS2EContext *cae = avctx->priv_data;
170 /* create the XAVS2 video encoder */
171 /* read frame data and send to the XAVS2 video encoder */
172 if (cae->api->encoder_get_buffer(cae->encoder, &pic) < 0) {
173 av_log(avctx,AV_LOG_ERROR, "failed to get frame buffer\n");
174 return AVERROR_EXTERNAL;
177 switch (frame->format) {
178 case AV_PIX_FMT_YUV420P:
179 if (pic.img.in_sample_size == pic.img.enc_sample_size) {
180 xavs2_copy_frame(&pic, frame);
182 const int shift_in = atoi(cae->api->opt_get(cae->param, "SampleShift"));
183 xavs2_copy_frame_with_shift(&pic, frame, shift_in);
186 case AV_PIX_FMT_YUV420P10:
187 if (pic.img.in_sample_size == pic.img.enc_sample_size) {
188 xavs2_copy_frame(&pic, frame);
192 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
193 return AVERROR(EINVAL);
198 pic.i_pts = frame->pts;
199 pic.i_type = XAVS2_TYPE_AUTO;
201 ret = cae->api->encoder_encode(cae->encoder, &pic, &cae->packet);
204 av_log(avctx, AV_LOG_ERROR, "encode failed\n");
205 return AVERROR_EXTERNAL;
209 cae->api->encoder_encode(cae->encoder, NULL, &cae->packet);
212 if ((cae->packet.len) && (cae->packet.state != XAVS2_STATE_FLUSH_END)){
214 if (av_new_packet(pkt, cae->packet.len) < 0){
215 av_log(avctx, AV_LOG_ERROR, "packet alloc failed\n");
216 cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
217 return AVERROR(ENOMEM);
220 pkt->pts = cae->packet.pts;
221 pkt->dts = cae->packet.dts;
223 memcpy(pkt->data, cae->packet.stream, cae->packet.len);
224 pkt->size = cae->packet.len;
226 cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
236 static av_cold int xavs2_close(AVCodecContext *avctx)
238 XAVS2EContext *cae = avctx->priv_data;
239 /* destroy the encoder */
241 cae->api->encoder_destroy(cae->encoder);
244 cae->api->opt_destroy(cae->param);
250 #define OFFSET(x) offsetof(XAVS2EContext, x)
251 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
253 static const AVOption options[] = {
254 { "lcu_row_threads" , "number of parallel threads for rows" , OFFSET(lcu_row_threads) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, VE },
255 { "initial_qp" , "Quantization initial parameter" , OFFSET(initial_qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
256 { "qp" , "Quantization parameter" , OFFSET(qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
257 { "max_qp" , "max qp for rate control" , OFFSET(max_qp) , AV_OPT_TYPE_INT, {.i64 = 55 }, 0, 63, VE },
258 { "min_qp" , "min qp for rate control" , OFFSET(min_qp) , AV_OPT_TYPE_INT, {.i64 = 20 }, 0, 63, VE },
259 { "speed_level" , "Speed level, higher is better but slower", OFFSET(preset_level) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 9, VE },
260 { "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 },
261 { "xavs2-params" , "set the xavs2 configuration using a :-separated list of key=value parameters", OFFSET(xavs2_opts), AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
265 static const AVClass libxavs2 = {
266 .class_name = "XAVS2EContext",
267 .item_name = av_default_item_name,
269 .version = LIBAVUTIL_VERSION_INT,
272 static const AVCodecDefault xavs2_defaults[] = {
279 AVCodec ff_libxavs2_encoder = {
281 .long_name = NULL_IF_CONFIG_SMALL("libxavs2 AVS2-P2/IEEE1857.4"),
282 .type = AVMEDIA_TYPE_VIDEO,
283 .id = AV_CODEC_ID_AVS2,
284 .priv_data_size = sizeof(XAVS2EContext),
286 .encode2 = xavs2_encode_frame,
287 .close = xavs2_close,
288 .capabilities = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AUTO_THREADS,
289 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_NONE },
290 .priv_class = &libxavs2,
291 .defaults = xavs2_defaults,
292 .wrapper_name = "libxavs2",