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>
34
35 #define END_OF_STREAM 0x001
36
37 #define XAVS_PART_I8X8 0x002 /* Analyze i8x8 (requires 8x8 transform) */
38 #define XAVS_PART_P8X8 0x010 /* Analyze p16x8, p8x16 and p8x8 */
39 #define XAVS_PART_B8X8 0x100 /* Analyze b16x8, b*/
40
58
62
64 {
65 static const int level_map[] = {
70 };
71
72 if (level < 0 || level > XAVS_LOG_DEBUG)
73 return;
74
75 av_vlog(p, level_map[level], fmt, args);
76 }
77
79 xavs_nal_t *nals, int nnal)
80 {
84
85 if (!nnal)
86 return 0;
87
88 for (i = 0; i < nnal; i++)
89 size += nals[i].i_payload;
90
94
95 /* Write the SEI as part of the first frame. */
100 }
101
102 for (i = 0; i < nnal; i++) {
103 s = xavs_nal_encode(p, &size, 1, nals + i);
104 if (s < 0)
105 return -1;
107 }
109
110 return 1;
111 }
112
115 {
117 xavs_nal_t *nal;
119 xavs_picture_t pic_out;
120
121 x4->
pic.img.i_csp = XAVS_CSP_I420;
122 x4->
pic.img.i_plane = 3;
123
124 if (frame) {
125 for (i = 0; i < 3; i++) {
126 x4->
pic.img.plane[i] = frame->
data[i];
128 }
129
130 x4->
pic.i_pts = frame->
pts;
131 x4->
pic.i_type = XAVS_TYPE_AUTO;
133 }
134
135 if (xavs_encoder_encode(x4->
enc, &nal, &nnal,
136 frame? &x4->
pic:
NULL, &pic_out) < 0)
137 return -1;
138
140
141 if (ret < 0)
142 return -1;
143
144 if (!ret) {
148
156 *got_packet = 1;
157 }
158 return 0;
159 }
160
162 pkt->
pts = pic_out.i_pts;
166 else
168 } else
170
171 switch (pic_out.i_type) {
172 case XAVS_TYPE_IDR:
173 case XAVS_TYPE_I:
175 break;
176 case XAVS_TYPE_P:
178 break;
179 case XAVS_TYPE_B:
180 case XAVS_TYPE_BREF:
182 break;
183 }
184
185 /* There is no IDR frame in AVS JiZhun */
186 /* Sequence header is used as a flag */
187 if (pic_out.i_type == XAVS_TYPE_I) {
190 }
191
193
196 return 0;
197 }
198
200 {
202
206
208 xavs_encoder_close(x4->
enc);
209
211
212 return 0;
213 }
214
216 {
218
220 xavs_param_default(&x4->
params);
221
223 x4->
params.p_log_private = avctx;
227 x4->
params.rc.i_rc_method = XAVS_RC_ABR;
228 }
233 x4->
params.rc.b_stat_read = 1;
234 } else {
236 x4->
params.rc.i_rc_method = XAVS_RC_CRF;
238 }
else if (x4->
cqp >= 0) {
239 x4->
params.rc.i_rc_method = XAVS_RC_CQP;
241 }
242 }
243
254 if (x4->
b_bias != INT_MIN)
258
260 /* cabac is not included in AVS JiZhun Profile */
262
264
266
267 /* AVS doesn't allow B picture as reference */
268 /* The max allowed reference frame number of B is 2 */
272
274
275 // x4->params.b_deblocking_filter = avctx->flags & CODEC_FLAG_LOOP_FILTER;
276
280
281 x4->
params.rc.f_qcompress = avctx->
qcompress;
/* 0.0 => cbr, 1.0 => constant qp */
282 x4->
params.rc.f_qblur = avctx->
qblur;
/* temporally blur quants */
283
285
290 /* This is only used for counting the fps */
293 x4->
params.analyse.inter = XAVS_ANALYSE_I8x8 |XAVS_ANALYSE_PSUB16x16| XAVS_ANALYSE_BSUB16x16;
294
297 x4->
params.analyse.i_me_method = XAVS_ME_DIA;
298 break;
300 x4->
params.analyse.i_me_method = XAVS_ME_HEX;
301 break;
303 x4->
params.analyse.i_me_method = XAVS_ME_UMH;
304 break;
306 x4->
params.analyse.i_me_method = XAVS_ME_ESA;
307 break;
309 x4->
params.analyse.i_me_method = XAVS_ME_TESA;
310 break;
311 default:
312 x4->
params.analyse.i_me_method = XAVS_ME_HEX;
313 }
314
317
319 /* AVS P2 only enables 8x8 transform */
320 x4->
params.analyse.b_transform_8x8 = 1;
//avctx->flags2 & CODEC_FLAG2_8X8DCT;
321
324
325 if (avctx->
level > 0)
327
329 x4->
params.rc.f_rate_tolerance =
331
334 x4->
params.rc.f_vbv_buffer_init =
336 } else
337 x4->
params.rc.f_vbv_buffer_init = 0.9;
338
339 /* TAG:do we have MB tree RC method */
340 /* what is the RC method we are now using? Default NO */
344
346 x4->
params.i_log_level = XAVS_LOG_DEBUG;
349
351 x4->
params.b_repeat_headers = 0;
352
353 x4->
enc = xavs_encoder_open(&x4->
params);
355 return -1;
356
359
363
364 /* TAG: Do we have GLOBAL HEADER in AVS */
365 /* We Have PPS and SPS in AVS */
367 xavs_nal_t *nal;
368 int nnal,
s, i,
size;
370
371 s = xavs_encoder_headers(x4->
enc, &nal, &nnal);
372
374 for (i = 0; i < nnal; i++) {
375 /* Don't put the SEI in extradata. */
376 if (nal[i].i_type ==
NAL_SEI) {
378 if (xavs_nal_encode(x4->
sei, &x4->
sei_size, 1, nal + i) < 0)
379 return -1;
380
381 continue;
382 }
383 size = xavs_nal_encode(p, &s, 1, nal + i);
384 if (size < 0)
385 return -1;
387 }
389 }
390 return 0;
391 }
392
393 #define OFFSET(x) offsetof(XavsContext, x)
394 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
396 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
397 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX,
VE },
398 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, {.i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
399 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
400 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
407 {
"mixed-refs",
"One reference per partition, as opposed to one reference per macroblock",
OFFSET(mixed_refs),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, 1,
VE },
410 };
411
417 };
418
420 { "b", "0" },
422 };
423
437 };