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 av_freep(&avctx->extradata);
00531 if( x->twopassbuffer != NULL ) {
00532 av_free(x->twopassbuffer);
00533 av_free(x->old_twopassbuffer);
00534 }
00535 av_free(x->twopassfile);
00536 av_free(x->intra_matrix);
00537 av_free(x->inter_matrix);
00538
00539 return 0;
00540 }
00541
00555 int xvid_strip_vol_header(AVCodecContext *avctx,
00556 unsigned char *frame,
00557 unsigned int header_len,
00558 unsigned int frame_len) {
00559 int vo_len = 0, i;
00560
00561 for( i = 0; i < header_len - 3; i++ ) {
00562 if( frame[i] == 0x00 &&
00563 frame[i+1] == 0x00 &&
00564 frame[i+2] == 0x01 &&
00565 frame[i+3] == 0xB6 ) {
00566 vo_len = i;
00567 break;
00568 }
00569 }
00570
00571 if( vo_len > 0 ) {
00572
00573 if( avctx->extradata == NULL ) {
00574 avctx->extradata = av_malloc(vo_len);
00575 memcpy(avctx->extradata, frame, vo_len);
00576 avctx->extradata_size = vo_len;
00577 }
00578
00579
00580 memmove(frame, &(frame[vo_len]), frame_len - vo_len);
00581 return frame_len - vo_len;
00582 } else
00583 return frame_len;
00584 }
00585
00595 void xvid_correct_framerate(AVCodecContext *avctx) {
00596 int frate, fbase;
00597 int est_frate, est_fbase;
00598 int gcd;
00599 float est_fps, fps;
00600
00601 frate = avctx->time_base.den;
00602 fbase = avctx->time_base.num;
00603
00604 gcd = av_gcd(frate, fbase);
00605 if( gcd > 1 ) {
00606 frate /= gcd;
00607 fbase /= gcd;
00608 }
00609
00610 if( frate <= 65000 && fbase <= 65000 ) {
00611 avctx->time_base.den = frate;
00612 avctx->time_base.num = fbase;
00613 return;
00614 }
00615
00616 fps = (float)frate / (float)fbase;
00617 est_fps = roundf(fps * 1000.0) / 1000.0;
00618
00619 est_frate = (int)est_fps;
00620 if( est_fps > (int)est_fps ) {
00621 est_frate = (est_frate + 1) * 1000;
00622 est_fbase = (int)roundf((float)est_frate / est_fps);
00623 } else
00624 est_fbase = 1;
00625
00626 gcd = av_gcd(est_frate, est_fbase);
00627 if( gcd > 1 ) {
00628 est_frate /= gcd;
00629 est_fbase /= gcd;
00630 }
00631
00632 if( fbase > est_fbase ) {
00633 avctx->time_base.den = est_frate;
00634 avctx->time_base.num = est_fbase;
00635 av_log(avctx, AV_LOG_DEBUG,
00636 "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
00637 est_fps, (((est_fps - fps)/fps) * 100.0));
00638 } else {
00639 avctx->time_base.den = frate;
00640 avctx->time_base.num = fbase;
00641 }
00642 }
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00661 static int xvid_ff_2pass_create(xvid_plg_create_t * param,
00662 void ** handle) {
00663 struct xvid_ff_pass1 *x = (struct xvid_ff_pass1 *)param->param;
00664 char *log = x->context->twopassbuffer;
00665
00666
00667 if( log == NULL )
00668 return XVID_ERR_FAIL;
00669
00670
00671
00672 log[0] = 0;
00673 snprintf(log, BUFFER_REMAINING(log),
00674 "# ffmpeg 2-pass log file, using xvid codec\n");
00675 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
00676 "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
00677 XVID_VERSION_MAJOR(XVID_VERSION),
00678 XVID_VERSION_MINOR(XVID_VERSION),
00679 XVID_VERSION_PATCH(XVID_VERSION));
00680
00681 *handle = x->context;
00682 return 0;
00683 }
00684
00692 static int xvid_ff_2pass_destroy(struct xvid_context *ref,
00693 xvid_plg_destroy_t *param) {
00694
00695
00696 if( ref->twopassbuffer != NULL )
00697 ref->twopassbuffer[0] = 0;
00698 return 0;
00699 }
00700
00708 static int xvid_ff_2pass_before(struct xvid_context *ref,
00709 xvid_plg_data_t *param) {
00710 int motion_remove;
00711 int motion_replacements;
00712 int vop_remove;
00713
00714
00715 if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
00716 return 0;
00717
00718
00719 param->quant = 2;
00720
00721
00722 motion_remove = ~XVID_ME_CHROMA_PVOP &
00723 ~XVID_ME_CHROMA_BVOP &
00724 ~XVID_ME_EXTSEARCH16 &
00725 ~XVID_ME_ADVANCEDDIAMOND16;
00726 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
00727 XVID_ME_SKIP_DELTASEARCH |
00728 XVID_ME_FASTREFINE16 |
00729 XVID_ME_BFRAME_EARLYSTOP;
00730 vop_remove = ~XVID_VOP_MODEDECISION_RD &
00731 ~XVID_VOP_FAST_MODEDECISION_RD &
00732 ~XVID_VOP_TRELLISQUANT &
00733 ~XVID_VOP_INTER4V &
00734 ~XVID_VOP_HQACPRED;
00735
00736 param->vol_flags &= ~XVID_VOL_GMC;
00737 param->vop_flags &= vop_remove;
00738 param->motion_flags &= motion_remove;
00739 param->motion_flags |= motion_replacements;
00740
00741 return 0;
00742 }
00743
00751 static int xvid_ff_2pass_after(struct xvid_context *ref,
00752 xvid_plg_data_t *param) {
00753 char *log = ref->twopassbuffer;
00754 char *frame_types = " ipbs";
00755 char frame_type;
00756
00757
00758 if( log == NULL )
00759 return XVID_ERR_FAIL;
00760
00761
00762 if( param->type < 5 && param->type > 0 ) {
00763 frame_type = frame_types[param->type];
00764 } else {
00765 return XVID_ERR_FAIL;
00766 }
00767
00768 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
00769 "%c %d %d %d %d %d %d\n",
00770 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
00771 param->stats.ublks, param->stats.length, param->stats.hlength);
00772
00773 return 0;
00774 }
00775
00787 int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2) {
00788 switch( cmd ) {
00789 case XVID_PLG_INFO:
00790 case XVID_PLG_FRAME:
00791 return 0;
00792
00793 case XVID_PLG_BEFORE:
00794 return xvid_ff_2pass_before(ref, p1);
00795
00796 case XVID_PLG_CREATE:
00797 return xvid_ff_2pass_create(p1, p2);
00798
00799 case XVID_PLG_AFTER:
00800 return xvid_ff_2pass_after(ref, p1);
00801
00802 case XVID_PLG_DESTROY:
00803 return xvid_ff_2pass_destroy(ref, p1);
00804
00805 default:
00806 return XVID_ERR_FAIL;
00807 }
00808 }
00809
00813 AVCodec ff_libxvid_encoder = {
00814 "libxvid",
00815 AVMEDIA_TYPE_VIDEO,
00816 CODEC_ID_MPEG4,
00817 sizeof(struct xvid_context),
00818 xvid_encode_init,
00819 xvid_encode_frame,
00820 xvid_encode_close,
00821 .pix_fmts= (const enum PixelFormat[]){PIX_FMT_YUV420P, PIX_FMT_NONE},
00822 .long_name= NULL_IF_CONFIG_SMALL("libxvidcore MPEG-4 part 2"),
00823 };
00824
00825 #endif