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
26 #include "codec_internal.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/opt.h"
32 #define xavs2_opt_set2(name, format, ...) do{ \
33 char opt_str[16] = {0}; \
35 av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \
36 err = cae->api->opt_set2(cae->param, name, opt_str); \
38 av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str);\
42 typedef struct XAVS2EContext
{
54 AVDictionary
*xavs2_opts
;
56 xavs2_outpacket_t packet
;
59 const xavs2_api_t
*api
;
63 static av_cold
int xavs2_init(AVCodecContext
*avctx
)
65 XAVS2EContext
*cae
= avctx
->priv_data
;
68 bit_depth
= avctx
->pix_fmt
== AV_PIX_FMT_YUV420P
? 8 : 10;
71 cae
->api
= xavs2_api_get(bit_depth
);
73 av_log(avctx
, AV_LOG_ERROR
, "Failed to get xavs2 api context\n");
74 return AVERROR_EXTERNAL
;
77 cae
->param
= cae
->api
->opt_alloc();
79 av_log(avctx
, AV_LOG_ERROR
, "Failed to alloc xavs2 parameters\n");
80 return AVERROR(ENOMEM
);
83 xavs2_opt_set2("Width", "%d", avctx
->width
);
84 xavs2_opt_set2("Height", "%d", avctx
->height
);
85 xavs2_opt_set2("BFrames", "%d", avctx
->max_b_frames
);
86 xavs2_opt_set2("BitDepth", "%d", bit_depth
);
87 xavs2_opt_set2("Log", "%d", cae
->log_level
);
88 xavs2_opt_set2("Preset", "%d", cae
->preset_level
);
90 xavs2_opt_set2("IntraPeriodMax", "%d", avctx
->gop_size
);
91 xavs2_opt_set2("IntraPeriodMin", "%d", avctx
->gop_size
);
93 xavs2_opt_set2("ThreadFrames", "%d", avctx
->thread_count
);
94 xavs2_opt_set2("ThreadRows", "%d", cae
->lcu_row_threads
);
96 xavs2_opt_set2("OpenGOP", "%d", !(avctx
->flags
& AV_CODEC_FLAG_CLOSED_GOP
));
99 const AVDictionaryEntry
*en
= NULL
;
100 while ((en
= av_dict_iterate(cae
->xavs2_opts
, en
)))
101 xavs2_opt_set2(en
->key
, "%s", en
->value
);
105 if (avctx
->bit_rate
> 0) {
106 xavs2_opt_set2("RateControl", "%d", 1);
107 xavs2_opt_set2("TargetBitRate", "%"PRId64
"", avctx
->bit_rate
);
108 xavs2_opt_set2("InitialQP", "%d", cae
->initial_qp
);
109 xavs2_opt_set2("MaxQP", "%d", avctx
->qmax
>= 0 ? avctx
->qmax
: cae
->max_qp
);
110 xavs2_opt_set2("MinQP", "%d", avctx
->qmin
>= 0 ? avctx
->qmin
: cae
->min_qp
);
112 xavs2_opt_set2("InitialQP", "%d", cae
->qp
);
115 ff_mpeg12_find_best_frame_rate(avctx
->framerate
, &code
, NULL
, NULL
, 0);
116 xavs2_opt_set2("FrameRate", "%d", code
);
118 cae
->encoder
= cae
->api
->encoder_create(cae
->param
);
121 av_log(avctx
, AV_LOG_ERROR
, "Failed to create xavs2 encoder instance.\n");
122 return AVERROR(EINVAL
);
128 static void xavs2_copy_frame_with_shift(xavs2_picture_t
*pic
, const AVFrame
*frame
, const int shift_in
)
136 for (plane
= 0; plane
< 3; plane
++) {
137 p_plane
= (uint16_t *)pic
->img
.img_planes
[plane
];
138 p_buffer
= frame
->data
[plane
];
139 for (hIdx
= 0; hIdx
< pic
->img
.i_lines
[plane
]; hIdx
++) {
140 memset(p_plane
, 0, pic
->img
.i_stride
[plane
]);
141 for (wIdx
= 0; wIdx
< pic
->img
.i_width
[plane
]; wIdx
++) {
142 p_plane
[wIdx
] = p_buffer
[wIdx
] << shift_in
;
144 p_plane
+= pic
->img
.i_stride
[plane
];
145 p_buffer
+= frame
->linesize
[plane
];
150 static void xavs2_copy_frame(xavs2_picture_t
*pic
, const AVFrame
*frame
)
158 for (plane
= 0; plane
< 3; plane
++) {
159 p_plane
= pic
->img
.img_planes
[plane
];
160 p_buffer
= frame
->data
[plane
];
161 stride
= pic
->img
.i_width
[plane
] * pic
->img
.in_sample_size
;
162 for (hIdx
= 0; hIdx
< pic
->img
.i_lines
[plane
]; hIdx
++) {
163 memcpy(p_plane
, p_buffer
, stride
);
164 p_plane
+= pic
->img
.i_stride
[plane
];
165 p_buffer
+= frame
->linesize
[plane
];
170 static int xavs2_encode_frame(AVCodecContext
*avctx
, AVPacket
*pkt
,
171 const AVFrame
*frame
, int *got_packet
)
173 XAVS2EContext
*cae
= avctx
->priv_data
;
177 /* create the XAVS2 video encoder */
178 /* read frame data and send to the XAVS2 video encoder */
179 if (cae
->api
->encoder_get_buffer(cae
->encoder
, &pic
) < 0) {
180 av_log(avctx
, AV_LOG_ERROR
, "Failed to get xavs2 frame buffer\n");
181 return AVERROR_EXTERNAL
;
184 switch (frame
->format
) {
185 case AV_PIX_FMT_YUV420P
:
186 if (pic
.img
.in_sample_size
== pic
.img
.enc_sample_size
) {
187 xavs2_copy_frame(&pic
, frame
);
189 const int shift_in
= atoi(cae
->api
->opt_get(cae
->param
, "SampleShift"));
190 xavs2_copy_frame_with_shift(&pic
, frame
, shift_in
);
193 case AV_PIX_FMT_YUV420P10
:
194 if (pic
.img
.in_sample_size
== pic
.img
.enc_sample_size
) {
195 xavs2_copy_frame(&pic
, frame
);
199 av_log(avctx
, AV_LOG_ERROR
, "Unsupported pixel format\n");
200 return AVERROR(EINVAL
);
205 pic
.i_pts
= frame
->pts
;
206 pic
.i_type
= XAVS2_TYPE_AUTO
;
208 ret
= cae
->api
->encoder_encode(cae
->encoder
, &pic
, &cae
->packet
);
211 av_log(avctx
, AV_LOG_ERROR
, "Encoding error occurred.\n");
212 return AVERROR_EXTERNAL
;
216 cae
->api
->encoder_encode(cae
->encoder
, NULL
, &cae
->packet
);
219 if ((cae
->packet
.len
) && (cae
->packet
.state
!= XAVS2_STATE_FLUSH_END
)) {
220 if ((ret
= ff_get_encode_buffer(avctx
, pkt
, cae
->packet
.len
, 0)) < 0) {
221 cae
->api
->encoder_packet_unref(cae
->encoder
, &cae
->packet
);
225 pkt
->pts
= cae
->packet
.pts
;
226 pkt
->dts
= cae
->packet
.dts
;
228 if (cae
->packet
.type
== XAVS2_TYPE_IDR
||
229 cae
->packet
.type
== XAVS2_TYPE_I
||
230 cae
->packet
.type
== XAVS2_TYPE_KEYFRAME
) {
231 pkt
->flags
|= AV_PKT_FLAG_KEY
;
234 memcpy(pkt
->data
, cae
->packet
.stream
, cae
->packet
.len
);
236 cae
->api
->encoder_packet_unref(cae
->encoder
, &cae
->packet
);
246 static av_cold
int xavs2_close(AVCodecContext
*avctx
)
248 XAVS2EContext
*cae
= avctx
->priv_data
;
249 /* destroy the encoder */
251 cae
->api
->encoder_destroy(cae
->encoder
);
254 cae
->api
->opt_destroy(cae
->param
);
260 #define OFFSET(x) offsetof(XAVS2EContext, x)
261 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
263 static const AVOption options
[] = {
264 { "lcu_row_threads" , "number of parallel threads for rows" , OFFSET(lcu_row_threads
) , AV_OPT_TYPE_INT
, {.i64
= 0 }, 0, INT_MAX
, VE
},
265 { "initial_qp" , "Quantization initial parameter" , OFFSET(initial_qp
) , AV_OPT_TYPE_INT
, {.i64
= 34 }, 1, 63, VE
},
266 { "qp" , "Quantization parameter" , OFFSET(qp
) , AV_OPT_TYPE_INT
, {.i64
= 34 }, 1, 63, VE
},
267 { "max_qp" , "max qp for rate control" , OFFSET(max_qp
) , AV_OPT_TYPE_INT
, {.i64
= 55 }, 0, 63, VE
},
268 { "min_qp" , "min qp for rate control" , OFFSET(min_qp
) , AV_OPT_TYPE_INT
, {.i64
= 20 }, 0, 63, VE
},
269 { "speed_level" , "Speed level, higher is better but slower", OFFSET(preset_level
) , AV_OPT_TYPE_INT
, {.i64
= 0 }, 0, 9, VE
},
270 { "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
},
271 { "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
},
275 static const AVClass libxavs2
= {
276 .class_name
= "XAVS2EContext",
277 .item_name
= av_default_item_name
,
279 .version
= LIBAVUTIL_VERSION_INT
,
282 static const FFCodecDefault xavs2_defaults
[] = {
289 const FFCodec ff_libxavs2_encoder
= {
290 .p
.name
= "libxavs2",
291 CODEC_LONG_NAME("libxavs2 AVS2-P2/IEEE1857.4"),
292 .p
.type
= AVMEDIA_TYPE_VIDEO
,
293 .p
.id
= AV_CODEC_ID_AVS2
,
294 .p
.capabilities
= AV_CODEC_CAP_DR1
| AV_CODEC_CAP_DELAY
|
295 AV_CODEC_CAP_OTHER_THREADS
,
296 .priv_data_size
= sizeof(XAVS2EContext
),
298 FF_CODEC_ENCODE_CB(xavs2_encode_frame
),
299 .close
= xavs2_close
,
300 .caps_internal
= FF_CODEC_CAP_NOT_INIT_THREADSAFE
|
301 FF_CODEC_CAP_AUTO_THREADS
,
302 .p
.pix_fmts
= (const enum AVPixelFormat
[]) { AV_PIX_FMT_YUV420P
,
304 .color_ranges
= AVCOL_RANGE_MPEG
,
305 .p
.priv_class
= &libxavs2
,
306 .defaults
= xavs2_defaults
,
307 .p
.wrapper_name
= "libxavs2",