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"
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
44
52
55
58
59 const xavs2_api_t *
api;
60
62
64 {
67
69
70 /* get API handler */
75 }
76
81 }
82
89
92
95
97
98 {
102 }
103
104 /* Rate control */
111 } else {
113 }
114
117
119
123 }
124
125 return 0;
126 }
127
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
151 {
152 uint8_t *p_plane;
153 uint8_t *p_buffer;
154 int plane;
155 int hIdx;
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
172 {
174 xavs2_picture_t pic;
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) {
182 }
184 switch (
frame->format) {
186 if (pic.img.in_sample_size == pic.img.enc_sample_size) {
188 } else {
189 const int shift_in = atoi(cae->
api->opt_get(cae->
param,
"SampleShift"));
191 }
192 break;
194 if (pic.img.in_sample_size == pic.img.enc_sample_size) {
196 break;
197 }
198 default:
201 break;
202 }
203
204 pic.i_state = 0;
205 pic.i_pts =
frame->pts;
206 pic.i_type = XAVS2_TYPE_AUTO;
207
209
213 }
214
215 } else {
217 }
218
219 if ((cae->
packet.len) && (cae->
packet.state != XAVS2_STATE_FLUSH_END)) {
223 }
224
227
228 if (cae->
packet.type == XAVS2_TYPE_IDR ||
229 cae->
packet.type == XAVS2_TYPE_I ||
230 cae->
packet.type == XAVS2_TYPE_KEYFRAME) {
232 }
233
235
237
238 *got_packet = 1;
239 } else {
240 *got_packet = 0;
241 }
242
243 return 0;
244 }
245
247 {
249 /* destroy the encoder */
252
255 }
256 }
257 return 0;
258 }
259
260 #define OFFSET(x) offsetof(XAVS2EContext, x)
261 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
262
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 },
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 },
273 };
274
280 };
281
283 { "b", "0" },
284 { "g", "48"},
285 { "bf", "7" },
287 };
288
290 .
p.
name =
"libxavs2",
306 .p.wrapper_name = "libxavs2",
307 } ;