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
63
67
69 {
70 static const int level_map[] = {
75 };
76
77 if (level < 0 || level > XAVS_LOG_DEBUG)
78 return;
79
80 av_vlog(p, level_map[level], fmt, args);
81 }
82
84 xavs_nal_t *nals, int nnal)
85 {
89
90 if (!nnal)
91 return 0;
92
93 for (i = 0; i < nnal; i++)
94 size += nals[i].i_payload;
95
97 return ret;
99
100 /* Write the SEI as part of the first frame. */
105 }
106
107 for (i = 0; i < nnal; i++) {
108 s = xavs_nal_encode(p, &size, 1, nals + i);
109 if (s < 0)
110 return -1;
112 }
114
115 return 1;
116 }
117
120 {
122 xavs_nal_t *nal;
123 int nnal, i, ret;
124 xavs_picture_t pic_out;
125 int pict_type;
126
127 x4->
pic.img.i_csp = XAVS_CSP_I420;
128 x4->
pic.img.i_plane = 3;
129
130 if (frame) {
131 for (i = 0; i < 3; i++) {
132 x4->
pic.img.plane[i] = frame->
data[i];
134 }
135
136 x4->
pic.i_pts = frame->
pts;
137 x4->
pic.i_type = XAVS_TYPE_AUTO;
139 }
140
141 if (xavs_encoder_encode(x4->
enc, &nal, &nnal,
142 frame? &x4->
pic:
NULL, &pic_out) < 0)
143 return -1;
144
146
147 if (ret < 0)
148 return -1;
149
150 if (!ret) {
153 return ret;
154
162 *got_packet = 1;
163 }
164 return 0;
165 }
166
167 #if FF_API_CODED_FRAME
171 #endif
172 pkt->
pts = pic_out.i_pts;
176 else
178 } else
180
181 switch (pic_out.i_type) {
182 case XAVS_TYPE_IDR:
183 case XAVS_TYPE_I:
185 break;
186 case XAVS_TYPE_P:
188 break;
189 case XAVS_TYPE_B:
190 case XAVS_TYPE_BREF:
192 break;
193 default:
195 }
196 #if FF_API_CODED_FRAME
200 #endif
201
202 /* There is no IDR frame in AVS JiZhun */
203 /* Sequence header is used as a flag */
204 if (pic_out.i_type == XAVS_TYPE_I) {
205 #if FF_API_CODED_FRAME
209 #endif
211 }
212
213 #if FF_API_CODED_FRAME
217 #endif
218
220
222 *got_packet = ret;
223 return 0;
224 }
225
227 {
229
233
235 xavs_encoder_close(x4->
enc);
236
237 return 0;
238 }
239
241 {
243
245 xavs_param_default(&x4->
params);
246
248 x4->
params.p_log_private = avctx;
252 x4->
params.rc.i_rc_method = XAVS_RC_ABR;
253 }
258 x4->
params.rc.b_stat_read = 1;
259 } else {
261 x4->
params.rc.i_rc_method = XAVS_RC_CRF;
263 }
else if (x4->
cqp >= 0) {
264 x4->
params.rc.i_rc_method = XAVS_RC_CQP;
266 }
267 }
268
281 if (x4->
b_bias != INT_MIN)
285
286 #if FF_API_MOTION_EST
291 x4->
params.analyse.i_me_method = XAVS_ME_DIA;
292 break;
294 x4->
params.analyse.i_me_method = XAVS_ME_HEX;
295 break;
297 x4->
params.analyse.i_me_method = XAVS_ME_UMH;
298 break;
300 x4->
params.analyse.i_me_method = XAVS_ME_ESA;
301 break;
303 x4->
params.analyse.i_me_method = XAVS_ME_TESA;
304 break;
305 default:
306 x4->
params.analyse.i_me_method = XAVS_ME_HEX;
307 }
308 }
310 #endif
311
313 /* cabac is not included in AVS JiZhun Profile */
315
316 #if FF_API_PRIVATE_OPT
321 #endif
322
324
326
327 /* AVS doesn't allow B picture as reference */
328 /* The max allowed reference frame number of B is 2 */
332
333 #if FF_API_PRIVATE_OPT
338 #endif
339
341
342 // x4->params.b_deblocking_filter = avctx->flags & AV_CODEC_FLAG_LOOP_FILTER;
343
347
348 x4->
params.rc.f_qcompress = avctx->
qcompress;
/* 0.0 => cbr, 1.0 => constant qp */
349 x4->
params.rc.f_qblur = avctx->
qblur;
/* temporally blur quants */
350
352
357 /* This is only used for counting the fps */
360 x4->
params.analyse.inter = XAVS_ANALYSE_I8x8 |XAVS_ANALYSE_PSUB16x16| XAVS_ANALYSE_BSUB16x16;
361
364
366 /* AVS P2 only enables 8x8 transform */
367 x4->
params.analyse.b_transform_8x8 = 1;
//avctx->flags2 & AV_CODEC_FLAG2_8X8DCT;
368
370
371 #if FF_API_PRIVATE_OPT
376 #endif
377
379
380 if (avctx->
level > 0)
382
384 x4->
params.rc.f_rate_tolerance =
386
389 x4->
params.rc.f_vbv_buffer_init =
391 } else
392 x4->
params.rc.f_vbv_buffer_init = 0.9;
393
394 /* TAG:do we have MB tree RC method */
395 /* what is the RC method we are now using? Default NO */
398
399 #if FF_API_PRIVATE_OPT
404 #endif
405
407
409 x4->
params.i_log_level = XAVS_LOG_DEBUG;
412
414 x4->
params.b_repeat_headers = 0;
415
416 x4->
enc = xavs_encoder_open(&x4->
params);
418 return -1;
419
422
423 /* TAG: Do we have GLOBAL HEADER in AVS */
424 /* We Have PPS and SPS in AVS */
426 xavs_nal_t *nal;
427 int nnal,
s, i,
size;
429
430 s = xavs_encoder_headers(x4->
enc, &nal, &nnal);
431
433 for (i = 0; i < nnal; i++) {
434 /* Don't put the SEI in extradata. */
435 if (nal[i].i_type ==
NAL_SEI) {
437 if (xavs_nal_encode(x4->
sei, &x4->
sei_size, 1, nal + i) < 0)
438 return -1;
439
440 continue;
441 }
442 size = xavs_nal_encode(p, &s, 1, nal + i);
443 if (size < 0)
444 return -1;
446 }
448 }
449 return 0;
450 }
451
452 #define OFFSET(x) offsetof(XavsContext, x)
453 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
455 {
"crf",
"Select the quality for constant quality mode",
OFFSET(crf),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE },
456 {
"qp",
"Constant quantization parameter rate control method",
OFFSET(cqp),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX,
VE },
457 {
"b-bias",
"Influences how often B-frames are used",
OFFSET(b_bias),
AV_OPT_TYPE_INT, {.i64 = INT_MIN}, INT_MIN, INT_MAX,
VE },
458 {
"cplxblur",
"Reduce fluctuations in QP (before curve compression)",
OFFSET(cplxblur),
AV_OPT_TYPE_FLOAT, {.dbl = -1 }, -1, FLT_MAX,
VE},
459 {
"direct-pred",
"Direct MV prediction mode",
OFFSET(direct_pred),
AV_OPT_TYPE_INT, {.i64 = -1 }, -1, INT_MAX,
VE,
"direct-pred" },
466 {
"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 },
468 {
"motion-est",
"Set motion estimation method",
OFFSET(motion_est),
AV_OPT_TYPE_INT, { .i64 = -1 }, -1, XAVS_ME_TESA,
VE,
"motion-est"},
474 {
"b_strategy",
"Strategy to choose between I/P/B-frames",
OFFSET(b_frame_strategy),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 2,
VE},
475 {
"chromaoffset",
"QP difference between chroma and luma",
OFFSET(chroma_offset),
AV_OPT_TYPE_INT, {.i64 = 0 }, INT_MIN, INT_MAX,
VE},
476 {
"sc_threshold",
"Scene change threshold",
OFFSET(scenechange_threshold),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX,
VE},
477 {
"noise_reduction",
"Noise reduction",
OFFSET(noise_reduction),
AV_OPT_TYPE_INT, {.i64 = 0 }, 0, INT_MAX,
VE},
478
480 };
481
487 };
488
490 { "b", "0" },
492 };
493
507 };
This structure describes decoded (raw) audio or video data.
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
#define AV_CODEC_FLAG_INTERLACED_DCT
Use interlaced DCT.
float qblur
amount of qscale smoothing over time (0.0-1.0)
#define AV_LOG_WARNING
Something somehow does not look correct.
int64_t bit_rate
the average bitrate
#define LIBAVUTIL_VERSION_INT
memory handling functions
static av_cold int init(AVCodecContext *avctx)
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...
int rc_initial_buffer_occupancy
Number of bits which should be loaded into the rc buffer before decoding starts.
enhanced predictive zonal search
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
static void XAVS_log(void *p, int level, const char *fmt, va_list args)
#define AV_CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
static const AVOption options[]
attribute_deprecated int me_method
This option does nothing.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
static av_cold int XAVS_close(AVCodecContext *avctx)
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
int bit_rate_tolerance
number of bits the bitstream is allowed to diverge from the reference.
int me_range
maximum motion estimation search range in subpel units If 0 then no limit.
float b_quant_factor
qscale factor between IP and B-frames If > 0 then the last P-frame quantizer will be used (q= lastp_q...
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int me_cmp
motion estimation comparison function
#define AV_INPUT_BUFFER_MIN_SIZE
minimum encoding buffer size Used to avoid some checks during header writing.
static int encode_nals(AVCodecContext *ctx, AVPacket *pkt, xavs_nal_t *nals, int nnal)
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int has_b_frames
Size of the frame reordering buffer in the decoder.
int qmax
maximum quantizer
static av_cold int XAVS_init(AVCodecContext *avctx)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int flags
AV_CODEC_FLAG_*.
const char * name
Name of the codec implementation.
float i_quant_factor
qscale factor between P and I-frames If > 0 then the last p frame quantizer will be used (q= lastp_q*...
transformed exhaustive search algorithm
int flags
A combination of AV_PKT_FLAG values.
int rc_buffer_size
decoder bitstream buffer size
common internal API header
int refs
number of reference frames
static const AVCodecDefault xavs_defaults[]
enum AVPictureType pict_type
Picture type of the frame.
int width
picture width / height.
attribute_deprecated int noise_reduction
#define AV_CODEC_FLAG_PSNR
error[?] variables will be set during encoding.
#define AV_CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
static const AVClass xavs_class
int max_qdiff
maximum quantizer difference between frames
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
static int XAVS_frame(AVCodecContext *avctx, AVPacket *pkt, const AVFrame *frame, int *got_packet)
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
attribute_deprecated int scenechange_threshold
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
attribute_deprecated int b_frame_strategy
main external API structure.
int qmin
minimum quantizer
int scenechange_threshold
Describe the class of an AVClass context structure.
void av_vlog(void *avcl, int level, const char *fmt, va_list vl)
Send the specified message to the log if the level is less than or equal to the current av_log_level...
attribute_deprecated int chromaoffset
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
float qcompress
amount of qscale change between easy & hard scenes (0.0-1.0)
static enum AVPixelFormat pix_fmts[]
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
int trellis
trellis RD quantization
#define AV_CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
#define FF_ENABLE_DEPRECATION_WARNINGS
uneven multi-hexagon search
AVCodec ff_libxavs_encoder
int key_frame
1 -> keyframe, 0-> not
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static void * av_mallocz_array(size_t nmemb, size_t size)
int frame_number
Frame counter, set by libavcodec.
AVPixelFormat
Pixel format.
This structure stores compressed data.
int me_subpel_quality
subpel ME quality
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
int64_t rc_max_rate
maximum bitrate
int keyint_min
minimum GOP size