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,