00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028
00029 #define _XOPEN_SOURCE 600
00030
00031 #include <xvid.h>
00032 #include <unistd.h>
00033 #include "avcodec.h"
00034 #include "libavutil/cpu.h"
00035 #include "libavutil/intreadwrite.h"
00036 #include "libxvid_internal.h"
00037 #if !HAVE_MKSTEMP
00038 #include <fcntl.h>
00039 #endif
00040
00044 #define BUFFER_SIZE 1024
00045 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
00046 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
00047
00052 struct xvid_context {
00053 void *encoder_handle;
00054 int xsize;
00055 int ysize;
00056 int vop_flags;
00057 int vol_flags;
00058 int me_flags;
00059 int qscale;
00060 int quicktime_format;
00061 AVFrame encoded_picture;
00062 char *twopassbuffer;
00063 char *old_twopassbuffer;
00064 char *twopassfile;
00065 unsigned char *intra_matrix;
00066 unsigned char *inter_matrix;
00067 };
00068
00072 struct xvid_ff_pass1 {
00073 int version;
00074 struct xvid_context *context;
00075 };
00076
00077
00078 int xvid_strip_vol_header(AVCodecContext *avctx, unsigned char *frame, unsigned int header_len, unsigned int frame_len);
00079 int xvid_ff_2pass(void *ref, int opt, void *p1, void *p2);
00080 void xvid_correct_framerate(AVCodecContext *avctx);
00081
00082
00083
00084
00085
00086
00087 int ff_tempfile(const char *prefix, char **filename) {
00088 int fd=-1;
00089 #if !HAVE_MKSTEMP
00090 *filename = tempnam(".", prefix);
00091 #else
00092 size_t len = strlen(prefix) + 12;
00093 *filename = av_malloc(len);
00094 #endif
00095
00096 if (*filename == NULL) {
00097 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot allocate file name\n");
00098 return -1;
00099 }
00100 #if !HAVE_MKSTEMP
00101 fd = open(*filename, O_RDWR | O_BINARY | O_CREAT, 0444);
00102 #else
00103 snprintf(*filename, len, "/tmp/%sXXXXXX", prefix);
00104 fd = mkstemp(*filename);
00105 if (fd < 0) {
00106 snprintf(*filename, len, "./%sXXXXXX", prefix);
00107 fd = mkstemp(*filename);
00108 }
00109 #endif
00110
00111 if (fd < 0) {
00112 av_log(NULL, AV_LOG_ERROR, "ff_tempfile: Cannot open temporary file %s\n", *filename);
00113 return -1;
00114 }
00115 return fd;
00116 }
00117
00118 #if CONFIG_LIBXVID_ENCODER
00119
00128 static av_cold int xvid_encode_init(AVCodecContext *avctx) {
00129 int xerr, i;
00130 int xvid_flags = avctx->flags;
00131 struct xvid_context *x = avctx->priv_data;
00132 uint16_t *intra, *inter;
00133 int fd;
00134
00135 xvid_plugin_single_t single;
00136 struct xvid_ff_pass1 rc2pass1;
00137 xvid_plugin_2pass2_t rc2pass2;
00138 xvid_gbl_init_t xvid_gbl_init;
00139 xvid_enc_create_t xvid_enc_create;
00140 xvid_enc_plugin_t plugins[7];
00141
00142
00143 x->vop_flags = XVID_VOP_HALFPEL;
00144 if( xvid_flags & CODEC_FLAG_4MV )
00145 x->vop_flags |= XVID_VOP_INTER4V;
00146 if( avctx->trellis
00147 )
00148 x->vop_flags |= XVID_VOP_TRELLISQUANT;
00149 if( xvid_flags & CODEC_FLAG_AC_PRED )
00150 x->vop_flags |= XVID_VOP_HQACPRED;
00151 if( xvid_flags & CODEC_FLAG_GRAY )
00152 x->vop_flags |= XVID_VOP_GREYSCALE;
00153
00154
00155 x->me_flags = 0;
00156 switch( avctx->me_method ) {
00157 case ME_FULL:
00158 x->me_flags |= XVID_ME_EXTSEARCH16
00159 | XVID_ME_EXTSEARCH8;
00160
00161 case ME_EPZS:
00162 x->me_flags |= XVID_ME_ADVANCEDDIAMOND8
00163 | XVID_ME_HALFPELREFINE8
00164 | XVID_ME_CHROMA_PVOP
00165 | XVID_ME_CHROMA_BVOP;
00166
00167 case ME_LOG:
00168 case ME_PHODS:
00169 case ME_X1:
00170 x->me_flags |= XVID_ME_ADVANCEDDIAMOND16
00171 | XVID_ME_HALFPELREFINE16;
00172
00173 case ME_ZERO:
00174 default:
00175 break;
00176 }
00177
00178
00179 switch( avctx->mb_decision ) {
00180 case 2:
00181 x->vop_flags |= XVID_VOP_MODEDECISION_RD;
00182 x->me_flags |= XVID_ME_HALFPELREFINE8_RD
00183 | XVID_ME_QUARTERPELREFINE8_RD
00184 | XVID_ME_EXTSEARCH_RD
00185 | XVID_ME_CHECKPREDICTION_RD;
00186 case 1:
00187 if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) )
00188 x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
00189 x->me_flags |= XVID_ME_HALFPELREFINE16_RD
00190 | XVID_ME_QUARTERPELREFINE16_RD;
00191
00192 default:
00193 break;
00194 }
00195
00196
00197 x->vol_flags = 0;
00198 if( xvid_flags & CODEC_FLAG_GMC ) {
00199 x->vol_flags |= XVID_VOL_GMC;
00200 x->me_flags |= XVID_ME_GME_REFINE;
00201 }
00202 if( xvid_flags & CODEC_FLAG_QPEL ) {
00203 x->vol_flags |= XVID_VOL_QUARTERPEL;
00204 x->me_flags |= XVID_ME_QUARTERPELREFINE16;
00205 if( x->vop_flags & XVID_VOP_INTER4V )
00206 x->me_flags |= XVID_ME_QUARTERPELREFINE8;
00207 }
00208
00209 memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
00210 xvid_gbl_init.version = XVID_VERSION;
00211 xvid_gbl_init.debug = 0;
00212
00213 #if ARCH_PPC
00214
00215 #if HAVE_ALTIVEC
00216 if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
00217 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
00218 } else
00219 #endif
00220 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
00221 #else
00222
00223 xvid_gbl_init.cpu_flags = 0;
00224 #endif
00225
00226
00227 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
00228
00229
00230 memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
00231 xvid_enc_create.version = XVID_VERSION;
00232
00233
00234 xvid_enc_create.width = x->xsize = avctx->width;
00235 xvid_enc_create.height = x->ysize = avctx->height;
00236
00237
00238
00239
00240
00241 xvid_enc_create.zones = NULL;
00242 xvid_enc_create.num_zones = 0;
00243
00244 xvid_enc_create.num_threads = avctx->thread_count;
00245
00246 xvid_enc_create.plugins = plugins;
00247 xvid_enc_create.num_plugins = 0;
00248
00249
00250 x->twopassbuffer = NULL;
00251 x->old_twopassbuffer = NULL;
00252 x->twopassfile = NULL;
00253
00254 if( xvid_flags & CODEC_FLAG_PASS1 ) {
00255 memset(&rc2pass1, 0, sizeof(struct xvid_ff_pass1));
00256 rc2pass1.version = XVID_VERSION;
00257 rc2pass1.context = x;
00258 x->twopassbuffer = av_malloc(BUFFER_SIZE);
00259 x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
00260 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
00261 av_log(avctx, AV_LOG_ERROR,
00262 "Xvid: Cannot allocate 2-pass log buffers\n");
00263 return -1;
00264 }
00265 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
00266
00267 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
00268 plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
00269 xvid_enc_create.num_plugins++;
00270 } else if( xvid_flags & CODEC_FLAG_PASS2 ) {
00271 memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
00272 rc2pass2.version = XVID_VERSION;
00273 rc2pass2.bitrate = avctx->bit_rate;
00274
00275 fd = ff_tempfile("xvidff.", &(x->twopassfile));
00276 if( fd == -1 ) {
00277 av_log(avctx, AV_LOG_ERROR,
00278 "Xvid: Cannot write 2-pass pipe\n");
00279 return -1;
00280 }
00281
00282 if( avctx->stats_in == NULL ) {
00283 av_log(avctx, AV_LOG_ERROR,
00284 "Xvid: No 2-pass information loaded for second pass\n");
00285 return -1;
00286 }
00287
00288 if( strlen(avctx->stats_in) >
00289 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
00290 close(fd);
00291 av_log(avctx, AV_LOG_ERROR,
00292 "Xvid: Cannot write to 2-pass pipe\n");
00293 return -1;
00294 }
00295
00296 close(fd);
00297 rc2pass2.filename = x->twopassfile;
00298 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
00299 plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
00300 xvid_enc_create.num_plugins++;
00301 } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
00302
00303 memset(&single, 0, sizeof(xvid_plugin_single_t));
00304 single.version = XVID_VERSION;
00305 single.bitrate = avctx->bit_rate;
00306
00307 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
00308 plugins[xvid_enc_create.num_plugins].param = &single;
00309 xvid_enc_create.num_plugins++;
00310 }
00311
00312
00313 if( 0.0 != avctx->lumi_masking ) {
00314 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
00315 plugins[xvid_enc_create.num_plugins].param = NULL;
00316 xvid_enc_create.num_plugins++;
00317 }
00318
00319
00320 xvid_correct_framerate(avctx);
00321 xvid_enc_create.fincr = avctx->time_base.num;
00322 xvid_enc_create.fbase = avctx->time_base.den;
00323 if( avctx->gop_size > 0 )
00324 xvid_enc_create.max_key_interval = avctx->gop_size;
00325 else
00326 xvid_enc_create.max_key_interval = 240;
00327
00328
00329 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
00330 else x->qscale = 0;
00331
00332 xvid_enc_create.min_quant[0] = avctx->qmin;
00333 xvid_enc_create.min_quant[1] = avctx->qmin;
00334 xvid_enc_create.min_quant[2] = avctx->qmin;
00335 xvid_enc_create.max_quant[0] = avctx->qmax;
00336 xvid_enc_create.max_quant[1] = avctx->qmax;
00337 xvid_enc_create.max_quant[2] = avctx->qmax;
00338
00339
00340 x->intra_matrix = x->inter_matrix = NULL;
00341 if( avctx->mpeg_quant )
00342 x->vol_flags |= XVID_VOL_MPEGQUANT;
00343 if( (avctx->intra_matrix || avctx->inter_matrix) ) {
00344 x->vol_flags |= XVID_VOL_MPEGQUANT;
00345
00346 if( avctx->intra_matrix ) {
00347 intra = avctx->intra_matrix;
00348 x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
00349 } else
00350 intra = NULL;
00351 if( avctx->inter_matrix ) {
00352 inter = avctx->inter_matrix;
00353 x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
00354 } else
00355 inter = NULL;
00356
00357 for( i = 0; i < 64; i++ ) {
00358 if( intra )
00359 x->intra_matrix[i] = (unsigned char)intra[i];
00360 if( inter )
00361 x->inter_matrix[i] = (unsigned char)inter[i];
00362 }
00363 }
00364
00365
00366 xvid_enc_create.frame_drop_ratio = 0;
00367 xvid_enc_create.global = 0;
00368 if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
00369 xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
00370
00371
00372 avctx->extradata = NULL;
00373 avctx->extradata_size = 0;
00374 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
00375
00376 x->quicktime_format = 1;
00377 avctx->codec_id = CODEC_ID_MPEG4;
00378 } else {
00379
00380 x->quicktime_format = 0;
00381 if(!avctx->codec_tag)
00382 avctx->codec_tag = AV_RL32("xvid");
00383 }
00384
00385
00386 xvid_enc_create.max_bframes = avctx->max_b_frames;
00387 xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
00388 xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
00389 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
00390
00391
00392 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
00393 if( xerr ) {
00394 av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
00395 return -1;
00396 }
00397
00398 x->encoder_handle = xvid_enc_create.handle;
00399 avctx->coded_frame = &x->encoded_picture;
00400
00401 return 0;
00402 }
00403
00413 static int xvid_encode_frame(AVCodecContext *avctx,
00414 unsigned char *frame, int buf_size, void *data) {
00415 int xerr, i;
00416 char *tmp;
00417 struct xvid_context *x = avctx->priv_data;
00418 AVFrame *picture = data;
00419 AVFrame *p = &(x->encoded_picture);
00420
00421 xvid_enc_frame_t xvid_enc_frame;
00422 xvid_enc_stats_t xvid_enc_stats;
00423
00424
00425 memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
00426 xvid_enc_frame.version = XVID_VERSION;
00427 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
00428 xvid_enc_stats.version = XVID_VERSION;
00429 *p = *picture;
00430
00431
00432 xvid_enc_frame.bitstream = frame;
00433 xvid_enc_frame.length = buf_size;
00434
00435
00436 if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
00437 av_log(avctx, AV_LOG_ERROR, "Xvid: Color spaces other than 420p not supported\n");
00438 return -1;
00439 }
00440
00441 xvid_enc_frame.input.csp = XVID_CSP_PLANAR;
00442
00443 for( i = 0; i < 4; i++ ) {
00444 xvid_enc_frame.input.plane[i] = picture->data[i];
00445 xvid_enc_frame.input.stride[i] = picture->linesize[i];
00446 }
00447
00448
00449 xvid_enc_frame.vop_flags = x->vop_flags;
00450 xvid_enc_frame.vol_flags = x->vol_flags;
00451 xvid_enc_frame.motion = x->me_flags;
00452 xvid_enc_frame.type =
00453 picture->pict_type == FF_I_TYPE ? XVID_TYPE_IVOP :
00454 picture->pict_type == FF_P_TYPE ? XVID_TYPE_PVOP :
00455 picture->pict_type == FF_B_TYPE ? XVID_TYPE_BVOP :
00456 XVID_TYPE_AUTO;
00457
00458
00459 if (avctx->sample_aspect_ratio.num < 1 || avctx->sample_aspect_ratio.num > 255 ||
00460 avctx->sample_aspect_ratio.den < 1 || avctx->sample_aspect_ratio.den > 255) {
00461 av_log(avctx, AV_LOG_ERROR, "Invalid pixel aspect ratio %i/%i\n",
00462 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
00463 return -1;
00464 }
00465 xvid_enc_frame.par = XVID_PAR_EXT;
00466 xvid_enc_frame.par_width = avctx->sample_aspect_ratio.num;
00467 xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;
00468
00469
00470 if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
00471 else xvid_enc_frame.quant = 0;
00472
00473
00474 xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
00475 xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
00476
00477
00478 xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
00479 &xvid_enc_frame, &xvid_enc_stats);
00480
00481
00482 avctx->stats_out = NULL;
00483 if( x->twopassbuffer ) {
00484 tmp = x->old_twopassbuffer;
00485 x->old_twopassbuffer = x->twopassbuffer;
00486 x->twopassbuffer = tmp;
00487 x->twopassbuffer[0] = 0;
00488 if( x->old_twopassbuffer[0] != 0 ) {
00489 avctx->stats_out = x->old_twopassbuffer;
00490 }
00491 }
00492
00493 if( 0 <= xerr ) {
00494 p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
00495 if( xvid_enc_stats.type == XVID_TYPE_PVOP )
00496 p->pict_type = FF_P_TYPE;
00497 else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
00498 p->pict_type = FF_B_TYPE;
00499 else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
00500 p->pict_type = FF_S_TYPE;
00501 else
00502 p->pict_type = FF_I_TYPE;
00503 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
00504 p->key_frame = 1;
00505 if( x->quicktime_format )
00506 return xvid_strip_vol_header(avctx, frame,
00507 xvid_enc_stats.hlength, xerr);
00508 } else
00509 p->key_frame = 0;
00510
00511 return xerr;
00512 } else {
00513 av_log(avctx, AV_LOG_ERROR, "Xvid: Encoding Error Occurred: %i\n", xerr);
00514 return -1;
00515 }
00516 }
00517
00525 static av_cold int xvid_encode_close(AVCodecContext *avctx) {
00526 struct xvid_context *x = avctx->priv_data;
00527
00528 xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
00529
00530 if( avctx->extradata != NULL )
00531 av_freep(&avctx->extradata);
00532 if( x->twopassbuffer != NULL ) {
00533 av_free(x->twopassbuffer);
00534 av_free(x->old_twopassbuffer);
00535 }
00536 if( x->twopassfile != NULL )
00537 av_free(x->twopassfile);
00538 if( x->intra_matrix != NULL )
00539 av_free(x->intra_matrix);
00540 if( x->inter_matrix != NULL )
00541 av_free(x->inter_matrix);
00542
00543 return 0;
00544 }
00545
00559 int xvid_strip_vol_header(AVCodecContext *avctx,
00560 unsigned char *frame,
00561 unsigned int header_len,
00562 unsigned int frame_len) {
00563 int vo_len = 0, i;
00564
00565 for( i = 0; i < header_len - 3; i++ ) {
00566 if( frame[i] == 0x00 &&
00567 frame[i+1] == 0x00 &&
00568 frame[i+2] == 0x01 &&
00569 frame[i+3] == 0xB6 ) {
00570 vo_len = i;
00571 break;
00572 }
00573 }
00574
00575 if( vo_len > 0 ) {
00576
00577 if( avctx->extradata == NULL ) {
00578 avctx->extradata = av_malloc(vo_len);
00579 memcpy(avctx->extradata, frame, vo_len);
00580 avctx->extradata_size = vo_len;
00581 }
00582
00583
00584 memmove(frame, &(frame[vo_len]), frame_len - vo_len);
00585 return frame_len - vo_len;
00586 } else
00587 return frame_len;
00588 }
00589
00599 void xvid_correct_framerate(AVCodecContext *avctx) {
00600 int frate, fbase;
00601 int est_frate, est_fbase;
00602 int gcd;
00603 float est_fps, fps;
00604
00605 frate = avctx->time_base.den;
00606 fbase = avctx->time_base.num;
00607
00608 gcd = av_gcd(frate, fbase);
00609 if( gcd > 1 ) {
00610 frate /= gcd;
00611 fbase /= gcd;
00612 }
00613
00614 if( frate <= 65000 && fbase <= 65000 ) {
00615 avctx->time_base.den = frate;
00616 avctx->time_base.num = fbase;
00617 return;
00618 }
00619
00620 fps = (float)frate / (float)fbase;
00621 est_fps = roundf(fps * 1000.0) / 1000.0;
00622
00623 est_frate = (int)est_fps;
00624 if( est_fps > (int)est_fps ) {
00625 est_frate = (est_frate + 1) * 1000;
00626 est_fbase = (int)roundf((float)est_frate / est_fps);
00627 } else
00628 est_fbase = 1;
00629
00630 gcd = av_gcd(est_frate, est_fbase);
00631 if( gcd > 1 ) {
00632 est_frate /= gcd;
00633 est_fbase /= gcd;
00634 }
00635
00636 if( fbase > est_fbase ) {
00637 avctx->time_base.den = est_frate;
00638 avctx->time_base.num = est_fbase;
00639 av_log(avctx, AV_LOG_DEBUG,
00640 "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
00641 est_fps, (((est_fps - fps)/fps) * 100.0));
00642 } else {
00643 avctx->time_base.den = frate;
00644 avctx->time_base.num = fbase;
00645 }
00646 }
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00665 static int xvid_ff_2pass_create(xvid_plg_create_t * param,
00666 void ** handle) {
00667 struct xvid_ff_pass1 *x = (struct xvid_ff_pass1 *)param->param;
00668 char *log = x->context->twopassbuffer;
00669
00670
00671 if( log == NULL )
00672 return XVID_ERR_FAIL;
00673
00674
00675
00676 log[0] = 0;
00677 snprintf(log, BUFFER_REMAINING(log),
00678 "# ffmpeg 2-pass log file, using xvid codec\n");
00679 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
00680 "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
00681 XVID_VERSION_MAJOR(XVID_VERSION),
00682 XVID_VERSION_MINOR(XVID_VERSION),
00683 XVID_VERSION_PATCH(XVID_VERSION));
00684
00685 *handle = x->context;
00686 return 0;
00687 }
00688
00696 static int xvid_ff_2pass_destroy(struct xvid_context *ref,
00697 xvid_plg_destroy_t *param) {
00698
00699
00700 if( ref->twopassbuffer != NULL )
00701 ref->twopassbuffer[0] = 0;
00702 return 0;
00703 }
00704
00712 static int xvid_ff_2pass_before(struct xvid_context *ref,
00713 xvid_plg_data_t *param) {
00714 int motion_remove;
00715 int motion_replacements;
00716 int vop_remove;
00717
00718
00719 if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
00720 return 0;
00721
00722
00723 param->quant = 2;
00724
00725
00726 motion_remove = ~XVID_ME_CHROMA_PVOP &
00727 ~XVID_ME_CHROMA_BVOP &
00728 ~XVID_ME_EXTSEARCH16 &
00729 ~XVID_ME_ADVANCEDDIAMOND16;
00730 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
00731 XVID_ME_SKIP_DELTASEARCH |
00732 XVID_ME_FASTREFINE16 |
00733 XVID_ME_BFRAME_EARLYSTOP;
00734 vop_remove = ~XVID_VOP_MODEDECISION_RD &
00735 ~XVID_VOP_FAST_MODEDECISION_RD &
00736 ~XVID_VOP_TRELLISQUANT &
00737 ~XVID_VOP_INTER4V &
00738 ~XVID_VOP_HQACPRED;
00739
00740 param->vol_flags &= ~XVID_VOL_GMC;
00741 param->vop_flags &= vop_remove;
00742 param->motion_flags &= motion_remove;
00743 param->motion_flags |= motion_replacements;
00744
00745 return 0;
00746 }
00747
00755 static int xvid_ff_2pass_after(struct xvid_context *ref,
00756 xvid_plg_data_t *param) {
00757 char *log = ref->twopassbuffer;
00758 char *frame_types = " ipbs";
00759 char frame_type;
00760
00761
00762 if( log == NULL )
00763 return XVID_ERR_FAIL;
00764
00765
00766 if( param->type < 5 && param->type > 0 ) {
00767 frame_type = frame_types[param->type];
00768 } else {
00769 return XVID_ERR_FAIL;
00770 }
00771
00772 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
00773 "%c %d %d %d %d %d %d\n",
00774 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
00775 param->stats.ublks, param->stats.length, param->stats.hlength);
00776
00777 return 0;
00778 }
00779
00791 int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) {
00792 switch( cmd ) {
00793 case XVID_PLG_INFO:
00794 case XVID_PLG_FRAME:
00795 return 0;
00796
00797 case XVID_PLG_BEFORE:
00798 return xvid_ff_2pass_before(ref, p1);
00799
00800 case XVID_PLG_CREATE:
00801 return xvid_ff_2pass_create(p1, p2);
00802
00803 case XVID_PLG_AFTER:
00804 return xvid_ff_2pass_after(ref, p1);
00805
00806 case XVID_PLG_DESTROY:
00807 return xvid_ff_2pass_destroy(ref, p1);
00808
00809 default:
00810 return XVID_ERR_FAIL;
00811 }
00812 }
00813
00817 AVCodec libxvid_encoder = {
00818 "libxvid",
00819 AVMEDIA_TYPE_VIDEO,
00820 CODEC_ID_MPEG4,
00821 sizeof(struct xvid_context),
00822 xvid_encode_init,
00823 xvid_encode_frame,
00824 xvid_encode_close,
00825 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00826 .long_name= NULL_IF_CONFIG_SMALL("libxvidcore MPEG-4 part 2"),
00827 };
00828
00829 #endif