FFmpeg
Loading...
Searching...
No Matches
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 "codec_internal.h"
27#include "encode.h"
28#include "mpeg12.h"
29#include "libavutil/avstring.h"
30#include "libavutil/opt.h"
31
32#define xavs2_opt_set2(name, format, ...) do{ \
33 char opt_str[16] = {0}; \
34 int err; \
35 av_strlcatf(opt_str, sizeof(opt_str), format, __VA_ARGS__); \
36 err = cae->api->opt_set2(cae->param, name, opt_str); \
37 if (err < 0) {\
38 av_log(avctx, AV_LOG_WARNING, "Invalid value for %s: %s\n", name, opt_str);\
39 }\
40} while(0);
41
42typedef struct XAVS2EContext {
43 AVClass *class;
44
47 int qp;
48 int max_qp;
49 int min_qp;
52
53 void *encoder;
55
56 xavs2_outpacket_t packet;
57 xavs2_param_t *param;
58
59 const xavs2_api_t *api;
60
62
64{
65 XAVS2EContext *cae = avctx->priv_data;
66 int bit_depth, code;
67
68 bit_depth = avctx->pix_fmt == AV_PIX_FMT_YUV420P ? 8 : 10;
69
70 /* get API handler */
71 cae->api = xavs2_api_get(bit_depth);
72 if (!cae->api) {
73 av_log(avctx, AV_LOG_ERROR, "Failed to get xavs2 api context\n");
74 return AVERROR_EXTERNAL;
75 }
76
77 cae->param = cae->api->opt_alloc();
78 if (!cae->param) {
79 av_log(avctx, AV_LOG_ERROR, "Failed to alloc xavs2 parameters\n");
80 return AVERROR(ENOMEM);
81 }
82
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);
89
90 xavs2_opt_set2("IntraPeriodMax", "%d", avctx->gop_size);
91 xavs2_opt_set2("IntraPeriodMin", "%d", avctx->gop_size);
92
93 xavs2_opt_set2("ThreadFrames", "%d", avctx->thread_count);
94 xavs2_opt_set2("ThreadRows", "%d", cae->lcu_row_threads);
95
96 xavs2_opt_set2("OpenGOP", "%d", !(avctx->flags & AV_CODEC_FLAG_CLOSED_GOP));
97
98 {
99 const AVDictionaryEntry *en = NULL;
100 while ((en = av_dict_iterate(cae->xavs2_opts, en)))
101 xavs2_opt_set2(en->key, "%s", en->value);
102 }
103
104 /* Rate control */
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);
111 } else {
112 xavs2_opt_set2("InitialQP", "%d", cae->qp);
113 }
114
116 xavs2_opt_set2("FrameRate", "%d", code);
117
118 cae->encoder = cae->api->encoder_create(cae->param);
119
120 if (!cae->encoder) {
121 av_log(avctx, AV_LOG_ERROR, "Failed to create xavs2 encoder instance.\n");
122 return AVERROR(EINVAL);
123 }
124
125 return 0;
126}
127
128static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
129{
130 uint16_t *p_plane;
131 uint8_t *p_buffer;
132 int plane;
133 int hIdx;
134 int wIdx;
135
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;
143 }
144 p_plane += pic->img.i_stride[plane];
145 p_buffer += frame->linesize[plane];
146 }
147 }
148}
149
150static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
151{
152 uint8_t *p_plane;
153 uint8_t *p_buffer;
154 int plane;
155 int hIdx;
156 int stride;
157
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];
166 }
167 }
168}
169
171 const AVFrame *frame, int *got_packet)
172{
173 XAVS2EContext *cae = avctx->priv_data;
174 xavs2_picture_t pic;
175 int ret;
176
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;
182 }
183 if (frame) {
184 switch (frame->format) {
186 if (pic.img.in_sample_size == pic.img.enc_sample_size) {
187 xavs2_copy_frame(&pic, frame);
188 } else {
189 const int shift_in = atoi(cae->api->opt_get(cae->param, "SampleShift"));
190 xavs2_copy_frame_with_shift(&pic, frame, shift_in);
191 }
192 break;
194 if (pic.img.in_sample_size == pic.img.enc_sample_size) {
195 xavs2_copy_frame(&pic, frame);
196 break;
197 }
199 default:
200 av_log(avctx, AV_LOG_ERROR, "Unsupported pixel format\n");
201 return AVERROR(EINVAL);
202 break;
203 }
204
205 pic.i_state = 0;
206 pic.i_pts = frame->pts;
207 pic.i_type = XAVS2_TYPE_AUTO;
208
209 ret = cae->api->encoder_encode(cae->encoder, &pic, &cae->packet);
210
211 if (ret) {
212 av_log(avctx, AV_LOG_ERROR, "Encoding error occurred.\n");
213 return AVERROR_EXTERNAL;
214 }
215
216 } else {
217 cae->api->encoder_encode(cae->encoder, NULL, &cae->packet);
218 }
219
220 if ((cae->packet.len) && (cae->packet.state != XAVS2_STATE_FLUSH_END)) {
221 if ((ret = ff_get_encode_buffer(avctx, pkt, cae->packet.len, 0)) < 0) {
222 cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
223 return ret;
224 }
225
226 pkt->pts = cae->packet.pts;
227 pkt->dts = cae->packet.dts;
228
229 if (cae->packet.type == XAVS2_TYPE_IDR ||
230 cae->packet.type == XAVS2_TYPE_I ||
231 cae->packet.type == XAVS2_TYPE_KEYFRAME) {
232 pkt->flags |= AV_PKT_FLAG_KEY;
233 }
234
235 memcpy(pkt->data, cae->packet.stream, cae->packet.len);
236
237 cae->api->encoder_packet_unref(cae->encoder, &cae->packet);
238
239 *got_packet = 1;
240 } else {
241 *got_packet = 0;
242 }
243
244 return 0;
245}
246
248{
249 XAVS2EContext *cae = avctx->priv_data;
250 /* destroy the encoder */
251 if (cae->api) {
252 cae->api->encoder_destroy(cae->encoder);
253
254 if (cae->param) {
255 cae->api->opt_destroy(cae->param);
256 }
257 }
258 return 0;
259}
260
261#define OFFSET(x) offsetof(XAVS2EContext, x)
262#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
263
264static const AVOption options[] = {
265 { "lcu_row_threads" , "number of parallel threads for rows" , OFFSET(lcu_row_threads) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX, VE },
266 { "initial_qp" , "Quantization initial parameter" , OFFSET(initial_qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
267 { "qp" , "Quantization parameter" , OFFSET(qp) , AV_OPT_TYPE_INT, {.i64 = 34 }, 1, 63, VE },
268 { "max_qp" , "max qp for rate control" , OFFSET(max_qp) , AV_OPT_TYPE_INT, {.i64 = 55 }, 0, 63, VE },
269 { "min_qp" , "min qp for rate control" , OFFSET(min_qp) , AV_OPT_TYPE_INT, {.i64 = 20 }, 0, 63, VE },
270 { "speed_level" , "Speed level, higher is better but slower", OFFSET(preset_level) , AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 9, VE },
271 { "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 },
272 { "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 },
273 { NULL },
274};
275
276static const AVClass libxavs2 = {
277 .class_name = "XAVS2EContext",
278 .item_name = av_default_item_name,
279 .option = options,
280 .version = LIBAVUTIL_VERSION_INT,
281};
282
284 { "b", "0" },
285 { "g", "48"},
286 { "bf", "7" },
287 { NULL },
288};
289
291 .p.name = "libxavs2",
292 CODEC_LONG_NAME("libxavs2 AVS2-P2/IEEE1857.4"),
293 .p.type = AVMEDIA_TYPE_VIDEO,
294 .p.id = AV_CODEC_ID_AVS2,
295 .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_DELAY |
297 .priv_data_size = sizeof(XAVS2EContext),
298 .init = xavs2_init,
300 .close = xavs2_close,
301 .caps_internal = FF_CODEC_CAP_NOT_INIT_THREADSAFE |
304 .color_ranges = AVCOL_RANGE_MPEG,
305 .p.priv_class = &libxavs2,
306 .defaults = xavs2_defaults,
307 .p.wrapper_name = "libxavs2",
308} ;
static void bit_depth(AudioStatsContext *s, const uint64_t *const mask, uint8_t *depth)
Definition af_astats.c:246
const FFCodec ff_libxavs2_encoder
Definition libxavs2.c:290
#define VE
Definition amfenc_av1.c:30
#define CODEC_PIXFMTS(...)
#define FF_CODEC_CAP_NOT_INIT_THREADSAFE
The codec is not known to be init-threadsafe (i.e.
#define FF_CODEC_ENCODE_CB(func)
#define CODEC_LONG_NAME(str)
#define FF_CODEC_CAP_AUTO_THREADS
Codec handles avctx->thread_count == 0 (auto) internally.
#define NULL
Definition coverity.c:32
static AVPacket * pkt
static AVFrame * frame
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
int ff_get_encode_buffer(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int flags)
Get a buffer for a packet.
Definition encode.c:106
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DICT
Underlying C type is AVDictionary*.
Definition opt.h:289
#define AV_CODEC_CAP_OTHER_THREADS
Codec supports multithreading through a method other than slice- or frame-level multithreading.
Definition codec.h:112
#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:79
#define AV_CODEC_FLAG_CLOSED_GOP
Definition avcodec.h:332
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition codec.h:49
@ AV_CODEC_ID_AVS2
Definition codec_id.h:243
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition packet.h:650
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
Definition dict.c:42
#define AVERROR_EXTERNAL
Generic error in an external library.
Definition error.h:59
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
const char * av_default_item_name(void *ptr)
Return the context name.
Definition log.c:241
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
#define LIBAVUTIL_VERSION_INT
Definition version.h:85
#define av_fallthrough
Definition attributes.h:67
#define av_cold
Definition attributes.h:117
static void xavs2_copy_frame(xavs2_picture_t *pic, const AVFrame *frame)
Definition libxavs2.c:150
static void xavs2_copy_frame_with_shift(xavs2_picture_t *pic, const AVFrame *frame, const int shift_in)
Definition libxavs2.c:128
static av_cold int xavs2_init(AVCodecContext *avctx)
Definition libxavs2.c:63
static const FFCodecDefault xavs2_defaults[]
Definition libxavs2.c:283
#define xavs2_opt_set2(name, format,...)
Definition libxavs2.c:32
static int xavs2_encode_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
Definition libxavs2.c:170
static av_cold int xavs2_close(AVCodecContext *avctx)
Definition libxavs2.c:247
#define OFFSET(x)
Definition libxavs2.c:261
static const AVClass libxavs2
Definition libxavs2.c:276
void ff_mpeg12_find_best_frame_rate(AVRational frame_rate, int *code, int *ext_n, int *ext_d, int nonstandard)
AVOptions.
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
const uint8_t * code
Definition spdifenc.c:433
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition avcodec.h:643
int width
picture width / height.
Definition avcodec.h:604
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:781
int qmin
minimum quantizer
Definition avcodec.h:1252
AVRational framerate
Definition avcodec.h:563
int64_t bit_rate
the average bitrate
Definition avcodec.h:493
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
Definition avcodec.h:1021
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1579
int qmax
maximum quantizer
Definition avcodec.h:1259
int flags
AV_CODEC_FLAG_*.
Definition avcodec.h:500
void * priv_data
Definition avcodec.h:470
char * key
Definition dict.h:91
char * value
Definition dict.h:92
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
AVOption.
Definition opt.h:428
This structure stores compressed data.
Definition packet.h:580
const xavs2_api_t * api
Definition libxavs2.c:59
xavs2_outpacket_t packet
Definition libxavs2.c:56
xavs2_param_t * param
Definition libxavs2.c:57
void * encoder
Definition libxavs2.c:53
AVDictionary * xavs2_opts
Definition libxavs2.c:54
int lcu_row_threads
Definition libxavs2.c:45
int preset_level
Definition libxavs2.c:50
#define stride
#define av_log(a,...)