00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include <xvid.h>
00029 #include <unistd.h>
00030 #include "avcodec.h"
00031 #include "internal.h"
00032 #include "libavutil/file.h"
00033 #include "libavutil/cpu.h"
00034 #include "libavutil/intreadwrite.h"
00035 #include "libavutil/mathematics.h"
00036 #include "libxvid.h"
00037 #include "mpegvideo.h"
00038
00042 #define BUFFER_SIZE 1024
00043 #define BUFFER_REMAINING(x) (BUFFER_SIZE - strlen(x))
00044 #define BUFFER_CAT(x) (&((x)[strlen(x)]))
00045
00050 struct xvid_context {
00051 void *encoder_handle;
00052 int xsize;
00053 int ysize;
00054 int vop_flags;
00055 int vol_flags;
00056 int me_flags;
00057 int qscale;
00058 int quicktime_format;
00059 AVFrame encoded_picture;
00060 char *twopassbuffer;
00061 char *old_twopassbuffer;
00062 char *twopassfile;
00063 unsigned char *intra_matrix;
00064 unsigned char *inter_matrix;
00065 };
00066
00070 struct xvid_ff_pass1 {
00071 int version;
00072 struct xvid_context *context;
00073 };
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00092 static int xvid_ff_2pass_create(xvid_plg_create_t * param,
00093 void ** handle) {
00094 struct xvid_ff_pass1 *x = (struct xvid_ff_pass1 *)param->param;
00095 char *log = x->context->twopassbuffer;
00096
00097
00098 if( log == NULL )
00099 return XVID_ERR_FAIL;
00100
00101
00102
00103 log[0] = 0;
00104 snprintf(log, BUFFER_REMAINING(log),
00105 "# ffmpeg 2-pass log file, using xvid codec\n");
00106 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
00107 "# Do not modify. libxvidcore version: %d.%d.%d\n\n",
00108 XVID_VERSION_MAJOR(XVID_VERSION),
00109 XVID_VERSION_MINOR(XVID_VERSION),
00110 XVID_VERSION_PATCH(XVID_VERSION));
00111
00112 *handle = x->context;
00113 return 0;
00114 }
00115
00123 static int xvid_ff_2pass_destroy(struct xvid_context *ref,
00124 xvid_plg_destroy_t *param) {
00125
00126
00127 if( ref->twopassbuffer != NULL )
00128 ref->twopassbuffer[0] = 0;
00129 return 0;
00130 }
00131
00139 static int xvid_ff_2pass_before(struct xvid_context *ref,
00140 xvid_plg_data_t *param) {
00141 int motion_remove;
00142 int motion_replacements;
00143 int vop_remove;
00144
00145
00146 if( param->zone && param->zone->mode == XVID_ZONE_QUANT )
00147 return 0;
00148
00149
00150 param->quant = 2;
00151
00152
00153 motion_remove = ~XVID_ME_CHROMA_PVOP &
00154 ~XVID_ME_CHROMA_BVOP &
00155 ~XVID_ME_EXTSEARCH16 &
00156 ~XVID_ME_ADVANCEDDIAMOND16;
00157 motion_replacements = XVID_ME_FAST_MODEINTERPOLATE |
00158 XVID_ME_SKIP_DELTASEARCH |
00159 XVID_ME_FASTREFINE16 |
00160 XVID_ME_BFRAME_EARLYSTOP;
00161 vop_remove = ~XVID_VOP_MODEDECISION_RD &
00162 ~XVID_VOP_FAST_MODEDECISION_RD &
00163 ~XVID_VOP_TRELLISQUANT &
00164 ~XVID_VOP_INTER4V &
00165 ~XVID_VOP_HQACPRED;
00166
00167 param->vol_flags &= ~XVID_VOL_GMC;
00168 param->vop_flags &= vop_remove;
00169 param->motion_flags &= motion_remove;
00170 param->motion_flags |= motion_replacements;
00171
00172 return 0;
00173 }
00174
00182 static int xvid_ff_2pass_after(struct xvid_context *ref,
00183 xvid_plg_data_t *param) {
00184 char *log = ref->twopassbuffer;
00185 const char *frame_types = " ipbs";
00186 char frame_type;
00187
00188
00189 if( log == NULL )
00190 return XVID_ERR_FAIL;
00191
00192
00193 if( param->type < 5 && param->type > 0 ) {
00194 frame_type = frame_types[param->type];
00195 } else {
00196 return XVID_ERR_FAIL;
00197 }
00198
00199 snprintf(BUFFER_CAT(log), BUFFER_REMAINING(log),
00200 "%c %d %d %d %d %d %d\n",
00201 frame_type, param->stats.quant, param->stats.kblks, param->stats.mblks,
00202 param->stats.ublks, param->stats.length, param->stats.hlength);
00203
00204 return 0;
00205 }
00206
00218 static int xvid_ff_2pass(void *ref, int cmd, void *p1, void *p2)
00219 {
00220 switch( cmd ) {
00221 case XVID_PLG_INFO:
00222 case XVID_PLG_FRAME:
00223 return 0;
00224
00225 case XVID_PLG_BEFORE:
00226 return xvid_ff_2pass_before(ref, p1);
00227
00228 case XVID_PLG_CREATE:
00229 return xvid_ff_2pass_create(p1, p2);
00230
00231 case XVID_PLG_AFTER:
00232 return xvid_ff_2pass_after(ref, p1);
00233
00234 case XVID_PLG_DESTROY:
00235 return xvid_ff_2pass_destroy(ref, p1);
00236
00237 default:
00238 return XVID_ERR_FAIL;
00239 }
00240 }
00241
00255 static int xvid_strip_vol_header(AVCodecContext *avctx,
00256 AVPacket *pkt,
00257 unsigned int header_len,
00258 unsigned int frame_len) {
00259 int vo_len = 0, i;
00260
00261 for( i = 0; i < header_len - 3; i++ ) {
00262 if( pkt->data[i] == 0x00 &&
00263 pkt->data[i+1] == 0x00 &&
00264 pkt->data[i+2] == 0x01 &&
00265 pkt->data[i+3] == 0xB6 ) {
00266 vo_len = i;
00267 break;
00268 }
00269 }
00270
00271 if( vo_len > 0 ) {
00272
00273 if( avctx->extradata == NULL ) {
00274 avctx->extradata = av_malloc(vo_len);
00275 memcpy(avctx->extradata, pkt->data, vo_len);
00276 avctx->extradata_size = vo_len;
00277 }
00278
00279
00280 memmove(pkt->data, &pkt->data[vo_len], frame_len - vo_len);
00281 pkt->size = frame_len - vo_len;
00282 }
00283 return 0;
00284 }
00285
00295 static void xvid_correct_framerate(AVCodecContext *avctx)
00296 {
00297 int frate, fbase;
00298 int est_frate, est_fbase;
00299 int gcd;
00300 float est_fps, fps;
00301
00302 frate = avctx->time_base.den;
00303 fbase = avctx->time_base.num;
00304
00305 gcd = av_gcd(frate, fbase);
00306 if( gcd > 1 ) {
00307 frate /= gcd;
00308 fbase /= gcd;
00309 }
00310
00311 if( frate <= 65000 && fbase <= 65000 ) {
00312 avctx->time_base.den = frate;
00313 avctx->time_base.num = fbase;
00314 return;
00315 }
00316
00317 fps = (float)frate / (float)fbase;
00318 est_fps = roundf(fps * 1000.0) / 1000.0;
00319
00320 est_frate = (int)est_fps;
00321 if( est_fps > (int)est_fps ) {
00322 est_frate = (est_frate + 1) * 1000;
00323 est_fbase = (int)roundf((float)est_frate / est_fps);
00324 } else
00325 est_fbase = 1;
00326
00327 gcd = av_gcd(est_frate, est_fbase);
00328 if( gcd > 1 ) {
00329 est_frate /= gcd;
00330 est_fbase /= gcd;
00331 }
00332
00333 if( fbase > est_fbase ) {
00334 avctx->time_base.den = est_frate;
00335 avctx->time_base.num = est_fbase;
00336 av_log(avctx, AV_LOG_DEBUG,
00337 "Xvid: framerate re-estimated: %.2f, %.3f%% correction\n",
00338 est_fps, (((est_fps - fps)/fps) * 100.0));
00339 } else {
00340 avctx->time_base.den = frate;
00341 avctx->time_base.num = fbase;
00342 }
00343 }
00344
00345 static av_cold int xvid_encode_init(AVCodecContext *avctx) {
00346 int xerr, i;
00347 int xvid_flags = avctx->flags;
00348 struct xvid_context *x = avctx->priv_data;
00349 uint16_t *intra, *inter;
00350 int fd;
00351
00352 xvid_plugin_single_t single = { 0 };
00353 struct xvid_ff_pass1 rc2pass1 = { 0 };
00354 xvid_plugin_2pass2_t rc2pass2 = { 0 };
00355 xvid_gbl_init_t xvid_gbl_init = { 0 };
00356 xvid_enc_create_t xvid_enc_create = { 0 };
00357 xvid_enc_plugin_t plugins[7];
00358
00359
00360 x->vop_flags = XVID_VOP_HALFPEL;
00361 if( xvid_flags & CODEC_FLAG_4MV )
00362 x->vop_flags |= XVID_VOP_INTER4V;
00363 if( avctx->trellis
00364 )
00365 x->vop_flags |= XVID_VOP_TRELLISQUANT;
00366 if( xvid_flags & CODEC_FLAG_AC_PRED )
00367 x->vop_flags |= XVID_VOP_HQACPRED;
00368 if( xvid_flags & CODEC_FLAG_GRAY )
00369 x->vop_flags |= XVID_VOP_GREYSCALE;
00370
00371
00372 x->me_flags = 0;
00373 switch( avctx->me_method ) {
00374 case ME_FULL:
00375 x->me_flags |= XVID_ME_EXTSEARCH16
00376 | XVID_ME_EXTSEARCH8;
00377
00378 case ME_EPZS:
00379 x->me_flags |= XVID_ME_ADVANCEDDIAMOND8
00380 | XVID_ME_HALFPELREFINE8
00381 | XVID_ME_CHROMA_PVOP
00382 | XVID_ME_CHROMA_BVOP;
00383
00384 case ME_LOG:
00385 case ME_PHODS:
00386 case ME_X1:
00387 x->me_flags |= XVID_ME_ADVANCEDDIAMOND16
00388 | XVID_ME_HALFPELREFINE16;
00389
00390 case ME_ZERO:
00391 default:
00392 break;
00393 }
00394
00395
00396 switch( avctx->mb_decision ) {
00397 case 2:
00398 x->vop_flags |= XVID_VOP_MODEDECISION_RD;
00399 x->me_flags |= XVID_ME_HALFPELREFINE8_RD
00400 | XVID_ME_QUARTERPELREFINE8_RD
00401 | XVID_ME_EXTSEARCH_RD
00402 | XVID_ME_CHECKPREDICTION_RD;
00403 case 1:
00404 if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) )
00405 x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
00406 x->me_flags |= XVID_ME_HALFPELREFINE16_RD
00407 | XVID_ME_QUARTERPELREFINE16_RD;
00408
00409 default:
00410 break;
00411 }
00412
00413
00414 x->vol_flags = 0;
00415 if( xvid_flags & CODEC_FLAG_GMC ) {
00416 x->vol_flags |= XVID_VOL_GMC;
00417 x->me_flags |= XVID_ME_GME_REFINE;
00418 }
00419 if( xvid_flags & CODEC_FLAG_QPEL ) {
00420 x->vol_flags |= XVID_VOL_QUARTERPEL;
00421 x->me_flags |= XVID_ME_QUARTERPELREFINE16;
00422 if( x->vop_flags & XVID_VOP_INTER4V )
00423 x->me_flags |= XVID_ME_QUARTERPELREFINE8;
00424 }
00425
00426 xvid_gbl_init.version = XVID_VERSION;
00427 xvid_gbl_init.debug = 0;
00428
00429 #if ARCH_PPC
00430
00431 #if HAVE_ALTIVEC
00432 if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
00433 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
00434 } else
00435 #endif
00436 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
00437 #else
00438
00439 xvid_gbl_init.cpu_flags = 0;
00440 #endif
00441
00442
00443 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
00444
00445
00446 xvid_enc_create.version = XVID_VERSION;
00447
00448
00449 xvid_enc_create.width = x->xsize = avctx->width;
00450 xvid_enc_create.height = x->ysize = avctx->height;
00451
00452
00453
00454
00455
00456 xvid_enc_create.zones = NULL;
00457 xvid_enc_create.num_zones = 0;
00458
00459 xvid_enc_create.num_threads = avctx->thread_count;
00460
00461 xvid_enc_create.plugins = plugins;
00462 xvid_enc_create.num_plugins = 0;
00463
00464
00465 x->twopassbuffer = NULL;
00466 x->old_twopassbuffer = NULL;
00467 x->twopassfile = NULL;
00468
00469 if( xvid_flags & CODEC_FLAG_PASS1 ) {
00470 rc2pass1.version = XVID_VERSION;
00471 rc2pass1.context = x;
00472 x->twopassbuffer = av_malloc(BUFFER_SIZE);
00473 x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
00474 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
00475 av_log(avctx, AV_LOG_ERROR,
00476 "Xvid: Cannot allocate 2-pass log buffers\n");
00477 return -1;
00478 }
00479 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
00480
00481 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
00482 plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
00483 xvid_enc_create.num_plugins++;
00484 } else if( xvid_flags & CODEC_FLAG_PASS2 ) {
00485 rc2pass2.version = XVID_VERSION;
00486 rc2pass2.bitrate = avctx->bit_rate;
00487
00488 fd = av_tempfile("xvidff.", &x->twopassfile, 0, avctx);
00489 if( fd == -1 ) {
00490 av_log(avctx, AV_LOG_ERROR,
00491 "Xvid: Cannot write 2-pass pipe\n");
00492 return -1;
00493 }
00494
00495 if( avctx->stats_in == NULL ) {
00496 av_log(avctx, AV_LOG_ERROR,
00497 "Xvid: No 2-pass information loaded for second pass\n");
00498 return -1;
00499 }
00500
00501 if( strlen(avctx->stats_in) >
00502 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
00503 close(fd);
00504 av_log(avctx, AV_LOG_ERROR,
00505 "Xvid: Cannot write to 2-pass pipe\n");
00506 return -1;
00507 }
00508
00509 close(fd);
00510 rc2pass2.filename = x->twopassfile;
00511 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
00512 plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
00513 xvid_enc_create.num_plugins++;
00514 } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
00515
00516 single.version = XVID_VERSION;
00517 single.bitrate = avctx->bit_rate;
00518
00519 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
00520 plugins[xvid_enc_create.num_plugins].param = &single;
00521 xvid_enc_create.num_plugins++;
00522 }
00523
00524
00525 if( 0.0 != avctx->lumi_masking ) {
00526 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
00527 plugins[xvid_enc_create.num_plugins].param = NULL;
00528 xvid_enc_create.num_plugins++;
00529 }
00530
00531
00532 xvid_correct_framerate(avctx);
00533 xvid_enc_create.fincr = avctx->time_base.num;
00534 xvid_enc_create.fbase = avctx->time_base.den;
00535 if( avctx->gop_size > 0 )
00536 xvid_enc_create.max_key_interval = avctx->gop_size;
00537 else
00538 xvid_enc_create.max_key_interval = 240;
00539
00540
00541 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
00542 else x->qscale = 0;
00543
00544 xvid_enc_create.min_quant[0] = avctx->qmin;
00545 xvid_enc_create.min_quant[1] = avctx->qmin;
00546 xvid_enc_create.min_quant[2] = avctx->qmin;
00547 xvid_enc_create.max_quant[0] = avctx->qmax;
00548 xvid_enc_create.max_quant[1] = avctx->qmax;
00549 xvid_enc_create.max_quant[2] = avctx->qmax;
00550
00551
00552 x->intra_matrix = x->inter_matrix = NULL;
00553 if( avctx->mpeg_quant )
00554 x->vol_flags |= XVID_VOL_MPEGQUANT;
00555 if( (avctx->intra_matrix || avctx->inter_matrix) ) {
00556 x->vol_flags |= XVID_VOL_MPEGQUANT;
00557
00558 if( avctx->intra_matrix ) {
00559 intra = avctx->intra_matrix;
00560 x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
00561 } else
00562 intra = NULL;
00563 if( avctx->inter_matrix ) {
00564 inter = avctx->inter_matrix;
00565 x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
00566 } else
00567 inter = NULL;
00568
00569 for( i = 0; i < 64; i++ ) {
00570 if( intra )
00571 x->intra_matrix[i] = (unsigned char)intra[i];
00572 if( inter )
00573 x->inter_matrix[i] = (unsigned char)inter[i];
00574 }
00575 }
00576
00577
00578 xvid_enc_create.frame_drop_ratio = 0;
00579 xvid_enc_create.global = 0;
00580 if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
00581 xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
00582
00583
00584 avctx->extradata = NULL;
00585 avctx->extradata_size = 0;
00586 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
00587
00588 x->quicktime_format = 1;
00589 avctx->codec_id = AV_CODEC_ID_MPEG4;
00590 } else {
00591
00592 x->quicktime_format = 0;
00593 if(!avctx->codec_tag)
00594 avctx->codec_tag = AV_RL32("xvid");
00595 }
00596
00597
00598 xvid_enc_create.max_bframes = avctx->max_b_frames;
00599 xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
00600 xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
00601 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
00602
00603
00604 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
00605 if( xerr ) {
00606 av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
00607 return -1;
00608 }
00609
00610 x->encoder_handle = xvid_enc_create.handle;
00611 avctx->coded_frame = &x->encoded_picture;
00612
00613 return 0;
00614 }
00615
00616 static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00617 const AVFrame *picture, int *got_packet)
00618 {
00619 int xerr, i, ret, user_packet = !!pkt->data;
00620 char *tmp;
00621 struct xvid_context *x = avctx->priv_data;
00622 AVFrame *p = &x->encoded_picture;
00623 int mb_width = (avctx->width + 15) / 16;
00624 int mb_height = (avctx->height + 15) / 16;
00625
00626 xvid_enc_frame_t xvid_enc_frame = { 0 };
00627 xvid_enc_stats_t xvid_enc_stats = { 0 };
00628
00629 if ((ret = ff_alloc_packet2(avctx, pkt, mb_width*mb_height*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0)
00630 return ret;
00631
00632
00633 xvid_enc_frame.version = XVID_VERSION;
00634 xvid_enc_stats.version = XVID_VERSION;
00635 *p = *picture;
00636
00637
00638 xvid_enc_frame.bitstream = pkt->data;
00639 xvid_enc_frame.length = pkt->size;
00640
00641
00642 if( avctx->pix_fmt != AV_PIX_FMT_YUV420P ) {
00643 av_log(avctx, AV_LOG_ERROR, "Xvid: Color spaces other than 420p not supported\n");
00644 return -1;
00645 }
00646
00647 xvid_enc_frame.input.csp = XVID_CSP_PLANAR;
00648
00649 for( i = 0; i < 4; i++ ) {
00650 xvid_enc_frame.input.plane[i] = picture->data[i];
00651 xvid_enc_frame.input.stride[i] = picture->linesize[i];
00652 }
00653
00654
00655 xvid_enc_frame.vop_flags = x->vop_flags;
00656 xvid_enc_frame.vol_flags = x->vol_flags;
00657 xvid_enc_frame.motion = x->me_flags;
00658 xvid_enc_frame.type =
00659 picture->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :
00660 picture->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :
00661 picture->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :
00662 XVID_TYPE_AUTO;
00663
00664
00665 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.num > 255 ||
00666 avctx->sample_aspect_ratio.den < 0 || avctx->sample_aspect_ratio.den > 255) {
00667 av_log(avctx, AV_LOG_ERROR, "Invalid pixel aspect ratio %i/%i\n",
00668 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
00669 return -1;
00670 }
00671 xvid_enc_frame.par = XVID_PAR_EXT;
00672 xvid_enc_frame.par_width = avctx->sample_aspect_ratio.num;
00673 xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;
00674
00675
00676 if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
00677 else xvid_enc_frame.quant = 0;
00678
00679
00680 xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
00681 xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
00682
00683
00684 xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
00685 &xvid_enc_frame, &xvid_enc_stats);
00686
00687
00688 avctx->stats_out = NULL;
00689 if( x->twopassbuffer ) {
00690 tmp = x->old_twopassbuffer;
00691 x->old_twopassbuffer = x->twopassbuffer;
00692 x->twopassbuffer = tmp;
00693 x->twopassbuffer[0] = 0;
00694 if( x->old_twopassbuffer[0] != 0 ) {
00695 avctx->stats_out = x->old_twopassbuffer;
00696 }
00697 }
00698
00699 if (xerr > 0) {
00700 *got_packet = 1;
00701
00702 p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
00703 if( xvid_enc_stats.type == XVID_TYPE_PVOP )
00704 p->pict_type = AV_PICTURE_TYPE_P;
00705 else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
00706 p->pict_type = AV_PICTURE_TYPE_B;
00707 else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
00708 p->pict_type = AV_PICTURE_TYPE_S;
00709 else
00710 p->pict_type = AV_PICTURE_TYPE_I;
00711 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
00712 p->key_frame = 1;
00713 pkt->flags |= AV_PKT_FLAG_KEY;
00714 if( x->quicktime_format )
00715 return xvid_strip_vol_header(avctx, pkt,
00716 xvid_enc_stats.hlength, xerr);
00717 } else
00718 p->key_frame = 0;
00719
00720 pkt->size = xerr;
00721
00722 return 0;
00723 } else {
00724 if (!user_packet)
00725 av_free_packet(pkt);
00726 if (!xerr)
00727 return 0;
00728 av_log(avctx, AV_LOG_ERROR, "Xvid: Encoding Error Occurred: %i\n", xerr);
00729 return -1;
00730 }
00731 }
00732
00733 static av_cold int xvid_encode_close(AVCodecContext *avctx) {
00734 struct xvid_context *x = avctx->priv_data;
00735
00736 xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
00737
00738 av_freep(&avctx->extradata);
00739 if( x->twopassbuffer != NULL ) {
00740 av_free(x->twopassbuffer);
00741 av_free(x->old_twopassbuffer);
00742 avctx->stats_out = NULL;
00743 }
00744 av_free(x->twopassfile);
00745 av_free(x->intra_matrix);
00746 av_free(x->inter_matrix);
00747
00748 return 0;
00749 }
00750
00751 AVCodec ff_libxvid_encoder = {
00752 .name = "libxvid",
00753 .type = AVMEDIA_TYPE_VIDEO,
00754 .id = AV_CODEC_ID_MPEG4,
00755 .priv_data_size = sizeof(struct xvid_context),
00756 .init = xvid_encode_init,
00757 .encode2 = xvid_encode_frame,
00758 .close = xvid_encode_close,
00759 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE },
00760 .long_name = NULL_IF_CONFIG_SMALL("libxvidcore MPEG-4 part 2"),
00761 };