1 /*
2 * AVS encoding using the xavs library
3 * Copyright (C) 2010 Amanda, Y.N. Wu <amanda11192003@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <math.h>
26 #include <stdint.h>
28 #include <xavs.h>
36
37 #define END_OF_STREAM 0x001
38
39 #define XAVS_PART_I8X8 0x002 /* Analyze i8x8 (requires 8x8 transform) */
40 #define XAVS_PART_P8X8 0x010 /* Analyze p16x8, p8x16 and p8x8 */
41 #define XAVS_PART_B8X8 0x100 /* Analyze b16x8, b*/
42
65
69
71 {
72 static const int level_map[] = {
77 };
78
79 if (level < 0 || level > XAVS_LOG_DEBUG)
80 return;
81
83 }
84
86 xavs_nal_t *nals, int nnal)
87 {
90 uint8_t *p, *p_end;
92
93 if (!nnal)
94 return 0;
95
96 for (
i = 0;
i < nnal;
i++)
97 size += 3
U + nals[
i].i_payload;
98
103
104 /* Write the SEI as part of the first frame. */
109 }
110
111 for (
i = 0;
i < nnal;
i++) {
112 int size = p_end - p;
113 s = xavs_nal_encode(p, &
size, 1, nals +
i);
116 if (
s != 3
U + nals[
i].i_payload)
119 }
120
121 return 1;
122 }
123
126 {
128 xavs_nal_t *nal;
130 xavs_picture_t pic_out;
131 int pict_type;
132
133 x4->
pic.img.i_csp = XAVS_CSP_I420;
134 x4->
pic.img.i_plane = 3;
135
137 for (
i = 0;
i < 3;
i++) {
140 }
141
143 x4->
pic.i_type = XAVS_TYPE_AUTO;
145 }
146
147 if (xavs_encoder_encode(x4->
enc, &nal, &nnal,
150
152
155
160
168 *got_packet = 1;
169 }
170 return 0;
171 }
172
177 else
179 } else
181
182 switch (pic_out.i_type) {
183 case XAVS_TYPE_IDR:
184 case XAVS_TYPE_I:
186 break;
187 case XAVS_TYPE_P:
189 break;
190 case XAVS_TYPE_B:
191 case XAVS_TYPE_BREF:
193 break;
194 default:
196 }
197
198 /* There is no IDR frame in AVS JiZhun */
199 /* Sequence header is used as a flag */
200 if (pic_out.i_type == XAVS_TYPE_I) {
202 }
203
205
208 return 0;
209 }
210
212 {
214
217
219 xavs_encoder_close(x4->
enc);
220
221 return 0;
222 }
223
225 {
227
229 xavs_param_default(&x4->
params);
230
232 x4->
params.p_log_private = avctx;
236 x4->
params.rc.i_rc_method = XAVS_RC_ABR;
237 }
242 x4->
params.rc.b_stat_read = 1;
243 } else {
245 x4->
params.rc.i_rc_method = XAVS_RC_CRF;
247 }
else if (x4->
cqp >= 0) {
248 x4->
params.rc.i_rc_method = XAVS_RC_CQP;
250 }
251 }
252
265 if (x4->
b_bias != INT_MIN)
269
271 /* cabac is not included in AVS JiZhun Profile */
273
275
277
278 /* AVS doesn't allow B picture as reference */
279 /* The max allowed reference frame number of B is 2 */
283
285
286 // x4->params.b_deblocking_filter = avctx->flags & AV_CODEC_FLAG_LOOP_FILTER;
287
291
292 x4->
params.rc.f_qcompress = avctx->
qcompress;
/* 0.0 => cbr, 1.0 => constant qp */
293 x4->
params.rc.f_qblur = avctx->
qblur;
/* temporally blur quants */
294
296
301 /* This is only used for counting the fps */
304 x4->
params.analyse.inter = XAVS_ANALYSE_I8x8 |XAVS_ANALYSE_PSUB16x16| XAVS_ANALYSE_BSUB16x16;
305
308
310 /* AVS P2 only enables 8x8 transform */
311 x4->
params.analyse.b_transform_8x8 = 1;
//avctx->flags2 & AV_CODEC_FLAG2_8X8DCT;
312
314
316
317 if (avctx->
level > 0)
319
321 x4->
params.rc.f_rate_tolerance =
323
326 x4->
params.rc.f_vbv_buffer_init =
328 } else
329 x4->
params.rc.f_vbv_buffer_init = 0.9;
330
331 /* TAG:do we have MB tree RC method */
332 /* what is the RC method we are now using? Default NO */
335
337
339 x4->
params.i_log_level = XAVS_LOG_DEBUG;
342
344 x4->
params.b_repeat_headers = 0;
345
346 x4->
enc = xavs_encoder_open(&x4->
params);
349
352
353 /* TAG: Do we have GLOBAL HEADER in AVS */
354 /* We Have PPS and SPS in AVS */
356 xavs_nal_t *nal;
358 uint8_t *p;
359
360 s = xavs_encoder_headers(x4->
enc, &nal, &nnal);
361
363 for (
i = 0;
i < nnal;
i++) {
364 /* Don't put the SEI in extradata. */
365 if (nal[
i].i_type == NAL_SEI) {
367 if (xavs_nal_encode(x4->
sei, &x4->
sei_size, 1, nal +
i) < 0)
368 return -1;
369
370 continue;
371 }
372 size = xavs_nal_encode(p, &
s, 1, nal +
i);
374 return -1;
376 }
378 }
379 return 0;
380 }
381
382 #define OFFSET(x) offsetof(XavsContext, x)
383 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
385 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
386 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX,
VE },
387 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, {.i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
388 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
389 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
396 {
"mixed-refs",
"One reference per partition, as opposed to one reference per macroblock",
OFFSET(mixed_refs),
AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1,
VE },
398 {
"motion-est",
"Set motion estimation method",
OFFSET(motion_est),
AV_OPT_TYPE_INT, { .i64 = XAVS_ME_DIA }, -1, XAVS_ME_TESA,
VE,
"motion-est"},
404 {
"b_strategy",
"Strategy to choose between I/P/B-frames",
OFFSET(b_frame_strategy),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2,
VE},
405 {
"chromaoffset",
"QP difference between chroma and luma",
OFFSET(chroma_offset),
AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX,
VE},
406 {
"sc_threshold",
"Scene change threshold",
OFFSET(scenechange_threshold),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX,
VE},
407 {
"noise_reduction",
"Noise reduction",
OFFSET(noise_reduction),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX,
VE},
408
410 };
411
417 };
418
420 { "b", "0" },
422 };
423
440 .p.wrapper_name = "libxavs",
441 };