42 #define BUFFER_SIZE 1024
43 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
44 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
88 *filename = tempnam(
".", prefix);
90 size_t len = strlen(prefix) + 12;
94 if (*filename ==
NULL) {
99 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
101 snprintf(*filename, len,
"/tmp/%sXXXXXX", prefix);
102 fd = mkstemp(*filename);
104 snprintf(*filename, len,
"./%sXXXXXX", prefix);
105 fd = mkstemp(*filename);
116 #if CONFIG_LIBXVID_ENCODER
128 int xvid_flags = avctx->
flags;
130 uint16_t *intra, *inter;
133 xvid_plugin_single_t single;
135 xvid_plugin_2pass2_t rc2pass2;
136 xvid_gbl_init_t xvid_gbl_init;
137 xvid_enc_create_t xvid_enc_create;
138 xvid_enc_plugin_t plugins[7];
157 | XVID_ME_EXTSEARCH8;
160 x->
me_flags |= XVID_ME_ADVANCEDDIAMOND8
161 | XVID_ME_HALFPELREFINE8
162 | XVID_ME_CHROMA_PVOP
163 | XVID_ME_CHROMA_BVOP;
168 x->
me_flags |= XVID_ME_ADVANCEDDIAMOND16
169 | XVID_ME_HALFPELREFINE16;
179 x->
vop_flags |= XVID_VOP_MODEDECISION_RD;
180 x->
me_flags |= XVID_ME_HALFPELREFINE8_RD
181 | XVID_ME_QUARTERPELREFINE8_RD
182 | XVID_ME_EXTSEARCH_RD
183 | XVID_ME_CHECKPREDICTION_RD;
185 if( !(x->
vop_flags & XVID_VOP_MODEDECISION_RD) )
186 x->
vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
187 x->
me_flags |= XVID_ME_HALFPELREFINE16_RD
188 | XVID_ME_QUARTERPELREFINE16_RD;
202 x->
me_flags |= XVID_ME_QUARTERPELREFINE16;
204 x->
me_flags |= XVID_ME_QUARTERPELREFINE8;
207 memset(&xvid_gbl_init, 0,
sizeof(xvid_gbl_init));
208 xvid_gbl_init.version = XVID_VERSION;
209 xvid_gbl_init.debug = 0;
215 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
218 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
221 xvid_gbl_init.cpu_flags = 0;
225 xvid_global(
NULL, XVID_GBL_INIT, &xvid_gbl_init,
NULL);
228 memset(&xvid_enc_create, 0,
sizeof(xvid_enc_create));
229 xvid_enc_create.version = XVID_VERSION;
232 xvid_enc_create.width = x->
xsize = avctx->
width;
239 xvid_enc_create.zones =
NULL;
240 xvid_enc_create.num_zones = 0;
244 xvid_enc_create.plugins = plugins;
245 xvid_enc_create.num_plugins = 0;
254 rc2pass1.version = XVID_VERSION;
255 rc2pass1.context = x;
260 "Xvid: Cannot allocate 2-pass log buffers\n");
266 plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
267 xvid_enc_create.num_plugins++;
269 memset(&rc2pass2, 0,
sizeof(xvid_plugin_2pass2_t));
270 rc2pass2.version = XVID_VERSION;
276 "Xvid: Cannot write 2-pass pipe\n");
282 "Xvid: No 2-pass information loaded for second pass\n");
290 "Xvid: Cannot write to 2-pass pipe\n");
296 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
297 plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
298 xvid_enc_create.num_plugins++;
301 memset(&single, 0,
sizeof(xvid_plugin_single_t));
302 single.version = XVID_VERSION;
305 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
306 plugins[xvid_enc_create.num_plugins].param = &single;
307 xvid_enc_create.num_plugins++;
312 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
313 plugins[xvid_enc_create.num_plugins].param =
NULL;
314 xvid_enc_create.num_plugins++;
322 xvid_enc_create.max_key_interval = avctx->
gop_size;
324 xvid_enc_create.max_key_interval = 240;
327 if( xvid_flags & CODEC_FLAG_QSCALE ) x->
qscale = 1;
330 xvid_enc_create.min_quant[0] = avctx->
qmin;
331 xvid_enc_create.min_quant[1] = avctx->
qmin;
332 xvid_enc_create.min_quant[2] = avctx->
qmin;
333 xvid_enc_create.max_quant[0] = avctx->
qmax;
334 xvid_enc_create.max_quant[1] = avctx->
qmax;
335 xvid_enc_create.max_quant[2] = avctx->
qmax;
355 for( i = 0; i < 64; i++ ) {
364 xvid_enc_create.frame_drop_ratio = 0;
365 xvid_enc_create.global = 0;
367 xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
390 xerr = xvid_encore(
NULL, XVID_ENC_CREATE, &xvid_enc_create,
NULL);
412 unsigned char *frame,
int buf_size,
void *
data) {
419 xvid_enc_frame_t xvid_enc_frame;
420 xvid_enc_stats_t xvid_enc_stats;
423 memset(&xvid_enc_frame, 0,
sizeof(xvid_enc_frame));
424 xvid_enc_frame.version = XVID_VERSION;
425 memset(&xvid_enc_stats, 0,
sizeof(xvid_enc_stats));
426 xvid_enc_stats.version = XVID_VERSION;
430 xvid_enc_frame.bitstream = frame;
431 xvid_enc_frame.length = buf_size;
439 xvid_enc_frame.input.csp = XVID_CSP_PLANAR;
441 for( i = 0; i < 4; i++ ) {
442 xvid_enc_frame.input.plane[i] = picture->
data[i];
443 xvid_enc_frame.input.stride[i] = picture->
linesize[i];
449 xvid_enc_frame.motion = x->
me_flags;
450 xvid_enc_frame.type =
463 xvid_enc_frame.par = XVID_PAR_EXT;
469 else xvid_enc_frame.quant = 0;
477 &xvid_enc_frame, &xvid_enc_stats);
493 if( xvid_enc_stats.type == XVID_TYPE_PVOP )
495 else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
497 else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
501 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
505 xvid_enc_stats.hlength, xerr);
554 unsigned char *frame,
555 unsigned int header_len,
556 unsigned int frame_len) {
559 for( i = 0; i < header_len - 3; i++ ) {
560 if( frame[i] == 0x00 &&
561 frame[i+1] == 0x00 &&
562 frame[i+2] == 0x01 &&
563 frame[i+3] == 0xB6 ) {
578 memmove(frame, &frame[vo_len], frame_len - vo_len);
579 return frame_len - vo_len;
595 int est_frate, est_fbase;
602 gcd =
av_gcd(frate, fbase);
608 if( frate <= 65000 && fbase <= 65000 ) {
614 fps = (float)frate / (
float)fbase;
615 est_fps =
roundf(fps * 1000.0) / 1000.0;
617 est_frate = (int)est_fps;
618 if( est_fps > (
int)est_fps ) {
619 est_frate = (est_frate + 1) * 1000;
620 est_fbase = (int)
roundf((
float)est_frate / est_fps);
624 gcd =
av_gcd(est_frate, est_fbase);
630 if( fbase > est_fbase ) {
634 "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
635 est_fps, (((est_fps - fps)/fps) * 100.0));
659 static int xvid_ff_2pass_create(xvid_plg_create_t * param,
666 return XVID_ERR_FAIL;
672 "# avconv 2-pass log file, using xvid codec\n");
674 "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
675 XVID_VERSION_MAJOR(XVID_VERSION),
676 XVID_VERSION_MINOR(XVID_VERSION),
677 XVID_VERSION_PATCH(XVID_VERSION));
690 static int xvid_ff_2pass_destroy(
struct xvid_context *ref,
691 xvid_plg_destroy_t *param) {
706 static int xvid_ff_2pass_before(
struct xvid_context *ref,
707 xvid_plg_data_t *param) {
709 int motion_replacements;
713 if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
720 motion_remove = ~XVID_ME_CHROMA_PVOP &
721 ~XVID_ME_CHROMA_BVOP &
722 ~XVID_ME_EXTSEARCH16 &
723 ~XVID_ME_ADVANCEDDIAMOND16;
724 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
725 XVID_ME_SKIP_DELTASEARCH |
726 XVID_ME_FASTREFINE16 |
727 XVID_ME_BFRAME_EARLYSTOP;
728 vop_remove = ~XVID_VOP_MODEDECISION_RD &
729 ~XVID_VOP_FAST_MODEDECISION_RD &
730 ~XVID_VOP_TRELLISQUANT &
734 param->vol_flags &= ~XVID_VOL_GMC;
735 param->vop_flags &= vop_remove;
736 param->motion_flags &= motion_remove;
737 param->motion_flags |= motion_replacements;
749 static int xvid_ff_2pass_after(
struct xvid_context *ref,
750 xvid_plg_data_t *param) {
752 const char *frame_types =
" ipbs";
757 return XVID_ERR_FAIL;
760 if( param->type < 5 && param->type > 0 ) {
761 frame_type = frame_types[param->type];
763 return XVID_ERR_FAIL;
767 "%c %d %d %d %d %d %d\n",
768 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
769 param->stats.ublks, param->stats.length, param->stats.hlength);
791 case XVID_PLG_BEFORE:
792 return xvid_ff_2pass_before(ref, p1);
794 case XVID_PLG_CREATE:
795 return xvid_ff_2pass_create(p1, p2);
798 return xvid_ff_2pass_after(ref, p1);
800 case XVID_PLG_DESTROY:
801 return xvid_ff_2pass_destroy(ref, p1);
804 return XVID_ERR_FAIL;
816 .
init = xvid_encode_init,
817 .encode = xvid_encode_frame,
818 .close = xvid_encode_close,
#define CODEC_FLAG_GMC
Use GMC.
enum PixelFormat pix_fmt
Pixel format, see PIX_FMT_xxx.
#define AV_CPU_FLAG_ALTIVEC
standard
int mpeg_quant
0-> h263 quant 1-> mpeg quant
char * old_twopassbuffer
Old character buffer (two-pass)
AVFrame * coded_frame
the picture in the bitstream
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...
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
char * stats_in
pass2 encoding statistics input buffer Concatenated stuff from stats_out of pass1 should be placed he...
unsigned char * intra_matrix
P-Frame Quant Matrix.
int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len)
#define BUFFER_REMAINING(x)
#define CODEC_FLAG_PASS1
Use internal 2pass ratecontrol in first pass mode.
int me_flags
Motion Estimation flags.
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
common functions for use with the Xvid wrappers
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...
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
int quicktime_format
Are we in a QT-based format?
#define CODEC_FLAG_4MV
4 MV per MB allowed / advanced prediction for H.263.
enhanced predictive zonal search
struct xvid_context * context
Pointer to private context.
float lumi_masking
luminance masking (0-> disabled)
char * stats_out
pass1 encoding statistics output buffer
static int init(AVCodecParserContext *s)
#define CODEC_FLAG_GRAY
Only decode/encode grayscale.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
#define CODEC_FLAG_PASS2
Use internal 2pass ratecontrol in second pass mode.
int qmax
maximum quantizer
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void xvid_correct_framerate(AVCodecContext *avctx)
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
void av_log(void *avcl, int level, const char *fmt,...)
const char * name
Name of the codec implementation.
#define CODEC_FLAG_AC_PRED
H.263 advanced intra coding / MPEG-4 AC prediction.
char * twopassfile
second pass temp file name
AVFrame encoded_picture
Encoded frame information.
int vop_flags
VOP flags for Xvid encoder.
int bit_rate
the average bitrate
Structure for the private first-pass plugin.
enum AVPictureType pict_type
Picture type of the frame, see ?_TYPE below.
Structure for the private Xvid context.
int width
picture width / height.
int ff_tempfile(const char *prefix, char **filename)
int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2)
unsigned char * inter_matrix
I-Frame Quant Matrix.
int quality
quality (between 1 (good) and FF_LAMBDA_MAX (bad))
int mb_decision
macroblock decision mode
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
#define BUFFER_SIZE
Buffer management macros.
int linesize[AV_NUM_DATA_POINTERS]
Size, in bytes, of the data for each picture/channel plane.
main external API structure.
static void close(AVCodecParserContext *s)
#define CODEC_FLAG_QSCALE
Use fixed qscale.
int qmin
minimum quantizer
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
#define CODEC_FLAG_QPEL
Use qpel MC.
uint16_t * intra_matrix
custom intra quantization matrix
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
char * twopassbuffer
Character buffer for two-pass.
float b_quant_offset
qscale offset between IP and B-frames
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
static av_always_inline av_const float roundf(float x)
uint16_t * inter_matrix
custom inter quantization matrix
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
int gop_size
the number of pictures in a group of pictures, or 0 for intra_only
#define CODEC_FLAG_CLOSED_GOP
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int vol_flags
VOL flags for Xvid encoder.
no search, that is use 0,0 vector whenever one is needed
int trellis
trellis RD quantization
void * encoder_handle
Handle for Xvid encoder.
int key_frame
1 -> keyframe, 0-> not
#define FF_QP2LAMBDA
factor to convert from H.263 QP to lambda
int me_method
Motion estimation algorithm used for video coding.
int qscale
Do we use constant scale?