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
00353 static av_cold int xvid_encode_init(AVCodecContext *avctx) {
00354 int xerr, i;
00355 int xvid_flags = avctx->flags;
00356 struct xvid_context *x = avctx->priv_data;
00357 uint16_t *intra, *inter;
00358 int fd;
00359
00360 xvid_plugin_single_t single = { 0 };
00361 struct xvid_ff_pass1 rc2pass1 = { 0 };
00362 xvid_plugin_2pass2_t rc2pass2 = { 0 };
00363 xvid_gbl_init_t xvid_gbl_init = { 0 };
00364 xvid_enc_create_t xvid_enc_create = { 0 };
00365 xvid_enc_plugin_t plugins[7];
00366
00367
00368 x->vop_flags = XVID_VOP_HALFPEL;
00369 if( xvid_flags & CODEC_FLAG_4MV )
00370 x->vop_flags |= XVID_VOP_INTER4V;
00371 if( avctx->trellis
00372 )
00373 x->vop_flags |= XVID_VOP_TRELLISQUANT;
00374 if( xvid_flags & CODEC_FLAG_AC_PRED )
00375 x->vop_flags |= XVID_VOP_HQACPRED;
00376 if( xvid_flags & CODEC_FLAG_GRAY )
00377 x->vop_flags |= XVID_VOP_GREYSCALE;
00378
00379
00380 x->me_flags = 0;
00381 switch( avctx->me_method ) {
00382 case ME_FULL:
00383 x->me_flags |= XVID_ME_EXTSEARCH16
00384 | XVID_ME_EXTSEARCH8;
00385
00386 case ME_EPZS:
00387 x->me_flags |= XVID_ME_ADVANCEDDIAMOND8
00388 | XVID_ME_HALFPELREFINE8
00389 | XVID_ME_CHROMA_PVOP
00390 | XVID_ME_CHROMA_BVOP;
00391
00392 case ME_LOG:
00393 case ME_PHODS:
00394 case ME_X1:
00395 x->me_flags |= XVID_ME_ADVANCEDDIAMOND16
00396 | XVID_ME_HALFPELREFINE16;
00397
00398 case ME_ZERO:
00399 default:
00400 break;
00401 }
00402
00403
00404 switch( avctx->mb_decision ) {
00405 case 2:
00406 x->vop_flags |= XVID_VOP_MODEDECISION_RD;
00407 x->me_flags |= XVID_ME_HALFPELREFINE8_RD
00408 | XVID_ME_QUARTERPELREFINE8_RD
00409 | XVID_ME_EXTSEARCH_RD
00410 | XVID_ME_CHECKPREDICTION_RD;
00411 case 1:
00412 if( !(x->vop_flags & XVID_VOP_MODEDECISION_RD) )
00413 x->vop_flags |= XVID_VOP_FAST_MODEDECISION_RD;
00414 x->me_flags |= XVID_ME_HALFPELREFINE16_RD
00415 | XVID_ME_QUARTERPELREFINE16_RD;
00416
00417 default:
00418 break;
00419 }
00420
00421
00422 x->vol_flags = 0;
00423 if( xvid_flags & CODEC_FLAG_GMC ) {
00424 x->vol_flags |= XVID_VOL_GMC;
00425 x->me_flags |= XVID_ME_GME_REFINE;
00426 }
00427 if( xvid_flags & CODEC_FLAG_QPEL ) {
00428 x->vol_flags |= XVID_VOL_QUARTERPEL;
00429 x->me_flags |= XVID_ME_QUARTERPELREFINE16;
00430 if( x->vop_flags & XVID_VOP_INTER4V )
00431 x->me_flags |= XVID_ME_QUARTERPELREFINE8;
00432 }
00433
00434 xvid_gbl_init.version = XVID_VERSION;
00435 xvid_gbl_init.debug = 0;
00436
00437 #if ARCH_PPC
00438
00439 #if HAVE_ALTIVEC
00440 if (av_get_cpu_flags() & AV_CPU_FLAG_ALTIVEC) {
00441 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ALTIVEC;
00442 } else
00443 #endif
00444 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
00445 #else
00446
00447 xvid_gbl_init.cpu_flags = 0;
00448 #endif
00449
00450
00451 xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
00452
00453
00454 xvid_enc_create.version = XVID_VERSION;
00455
00456
00457 xvid_enc_create.width = x->xsize = avctx->width;
00458 xvid_enc_create.height = x->ysize = avctx->height;
00459
00460
00461
00462
00463
00464 xvid_enc_create.zones = NULL;
00465 xvid_enc_create.num_zones = 0;
00466
00467 xvid_enc_create.num_threads = avctx->thread_count;
00468
00469 xvid_enc_create.plugins = plugins;
00470 xvid_enc_create.num_plugins = 0;
00471
00472
00473 x->twopassbuffer = NULL;
00474 x->old_twopassbuffer = NULL;
00475 x->twopassfile = NULL;
00476
00477 if( xvid_flags & CODEC_FLAG_PASS1 ) {
00478 rc2pass1.version = XVID_VERSION;
00479 rc2pass1.context = x;
00480 x->twopassbuffer = av_malloc(BUFFER_SIZE);
00481 x->old_twopassbuffer = av_malloc(BUFFER_SIZE);
00482 if( x->twopassbuffer == NULL || x->old_twopassbuffer == NULL ) {
00483 av_log(avctx, AV_LOG_ERROR,
00484 "Xvid: Cannot allocate 2-pass log buffers\n");
00485 return -1;
00486 }
00487 x->twopassbuffer[0] = x->old_twopassbuffer[0] = 0;
00488
00489 plugins[xvid_enc_create.num_plugins].func = xvid_ff_2pass;
00490 plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
00491 xvid_enc_create.num_plugins++;
00492 } else if( xvid_flags & CODEC_FLAG_PASS2 ) {
00493 rc2pass2.version = XVID_VERSION;
00494 rc2pass2.bitrate = avctx->bit_rate;
00495
00496 fd = av_tempfile("xvidff.", &x->twopassfile, 0, avctx);
00497 if( fd == -1 ) {
00498 av_log(avctx, AV_LOG_ERROR,
00499 "Xvid: Cannot write 2-pass pipe\n");
00500 return -1;
00501 }
00502
00503 if( avctx->stats_in == NULL ) {
00504 av_log(avctx, AV_LOG_ERROR,
00505 "Xvid: No 2-pass information loaded for second pass\n");
00506 return -1;
00507 }
00508
00509 if( strlen(avctx->stats_in) >
00510 write(fd, avctx->stats_in, strlen(avctx->stats_in)) ) {
00511 close(fd);
00512 av_log(avctx, AV_LOG_ERROR,
00513 "Xvid: Cannot write to 2-pass pipe\n");
00514 return -1;
00515 }
00516
00517 close(fd);
00518 rc2pass2.filename = x->twopassfile;
00519 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
00520 plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
00521 xvid_enc_create.num_plugins++;
00522 } else if( !(xvid_flags & CODEC_FLAG_QSCALE) ) {
00523
00524 single.version = XVID_VERSION;
00525 single.bitrate = avctx->bit_rate;
00526
00527 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
00528 plugins[xvid_enc_create.num_plugins].param = &single;
00529 xvid_enc_create.num_plugins++;
00530 }
00531
00532
00533 if( 0.0 != avctx->lumi_masking ) {
00534 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
00535 plugins[xvid_enc_create.num_plugins].param = NULL;
00536 xvid_enc_create.num_plugins++;
00537 }
00538
00539
00540 xvid_correct_framerate(avctx);
00541 xvid_enc_create.fincr = avctx->time_base.num;
00542 xvid_enc_create.fbase = avctx->time_base.den;
00543 if( avctx->gop_size > 0 )
00544 xvid_enc_create.max_key_interval = avctx->gop_size;
00545 else
00546 xvid_enc_create.max_key_interval = 240;
00547
00548
00549 if( xvid_flags & CODEC_FLAG_QSCALE ) x->qscale = 1;
00550 else x->qscale = 0;
00551
00552 xvid_enc_create.min_quant[0] = avctx->qmin;
00553 xvid_enc_create.min_quant[1] = avctx->qmin;
00554 xvid_enc_create.min_quant[2] = avctx->qmin;
00555 xvid_enc_create.max_quant[0] = avctx->qmax;
00556 xvid_enc_create.max_quant[1] = avctx->qmax;
00557 xvid_enc_create.max_quant[2] = avctx->qmax;
00558
00559
00560 x->intra_matrix = x->inter_matrix = NULL;
00561 if( avctx->mpeg_quant )
00562 x->vol_flags |= XVID_VOL_MPEGQUANT;
00563 if( (avctx->intra_matrix || avctx->inter_matrix) ) {
00564 x->vol_flags |= XVID_VOL_MPEGQUANT;
00565
00566 if( avctx->intra_matrix ) {
00567 intra = avctx->intra_matrix;
00568 x->intra_matrix = av_malloc(sizeof(unsigned char) * 64);
00569 } else
00570 intra = NULL;
00571 if( avctx->inter_matrix ) {
00572 inter = avctx->inter_matrix;
00573 x->inter_matrix = av_malloc(sizeof(unsigned char) * 64);
00574 } else
00575 inter = NULL;
00576
00577 for( i = 0; i < 64; i++ ) {
00578 if( intra )
00579 x->intra_matrix[i] = (unsigned char)intra[i];
00580 if( inter )
00581 x->inter_matrix[i] = (unsigned char)inter[i];
00582 }
00583 }
00584
00585
00586 xvid_enc_create.frame_drop_ratio = 0;
00587 xvid_enc_create.global = 0;
00588 if( xvid_flags & CODEC_FLAG_CLOSED_GOP )
00589 xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
00590
00591
00592 avctx->extradata = NULL;
00593 avctx->extradata_size = 0;
00594 if( xvid_flags & CODEC_FLAG_GLOBAL_HEADER ) {
00595
00596 x->quicktime_format = 1;
00597 avctx->codec_id = CODEC_ID_MPEG4;
00598 } else {
00599
00600 x->quicktime_format = 0;
00601 if(!avctx->codec_tag)
00602 avctx->codec_tag = AV_RL32("xvid");
00603 }
00604
00605
00606 xvid_enc_create.max_bframes = avctx->max_b_frames;
00607 xvid_enc_create.bquant_offset = 100 * avctx->b_quant_offset;
00608 xvid_enc_create.bquant_ratio = 100 * avctx->b_quant_factor;
00609 if( avctx->max_b_frames > 0 && !x->quicktime_format ) xvid_enc_create.global |= XVID_GLOBAL_PACKED;
00610
00611
00612 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
00613 if( xerr ) {
00614 av_log(avctx, AV_LOG_ERROR, "Xvid: Could not create encoder reference\n");
00615 return -1;
00616 }
00617
00618 x->encoder_handle = xvid_enc_create.handle;
00619 avctx->coded_frame = &x->encoded_picture;
00620
00621 return 0;
00622 }
00623
00633 static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00634 const AVFrame *picture, int *got_packet)
00635 {
00636 int xerr, i, ret, user_packet = !!pkt->data;
00637 char *tmp;
00638 struct xvid_context *x = avctx->priv_data;
00639 AVFrame *p = &x->encoded_picture;
00640 int mb_width = (avctx->width + 15) / 16;
00641 int mb_height = (avctx->height + 15) / 16;
00642
00643 xvid_enc_frame_t xvid_enc_frame = { 0 };
00644 xvid_enc_stats_t xvid_enc_stats = { 0 };
00645
00646 if ((ret = ff_alloc_packet2(avctx, pkt, mb_width*mb_height*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0)
00647 return ret;
00648
00649
00650 xvid_enc_frame.version = XVID_VERSION;
00651 xvid_enc_stats.version = XVID_VERSION;
00652 *p = *picture;
00653
00654
00655 xvid_enc_frame.bitstream = pkt->data;
00656 xvid_enc_frame.length = pkt->size;
00657
00658
00659 if( avctx->pix_fmt != PIX_FMT_YUV420P ) {
00660 av_log(avctx, AV_LOG_ERROR, "Xvid: Color spaces other than 420p not supported\n");
00661 return -1;
00662 }
00663
00664 xvid_enc_frame.input.csp = XVID_CSP_PLANAR;
00665
00666 for( i = 0; i < 4; i++ ) {
00667 xvid_enc_frame.input.plane[i] = picture->data[i];
00668 xvid_enc_frame.input.stride[i] = picture->linesize[i];
00669 }
00670
00671
00672 xvid_enc_frame.vop_flags = x->vop_flags;
00673 xvid_enc_frame.vol_flags = x->vol_flags;
00674 xvid_enc_frame.motion = x->me_flags;
00675 xvid_enc_frame.type =
00676 picture->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :
00677 picture->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :
00678 picture->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :
00679 XVID_TYPE_AUTO;
00680
00681
00682 if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.num > 255 ||
00683 avctx->sample_aspect_ratio.den < 0 || avctx->sample_aspect_ratio.den > 255) {
00684 av_log(avctx, AV_LOG_ERROR, "Invalid pixel aspect ratio %i/%i\n",
00685 avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);
00686 return -1;
00687 }
00688 xvid_enc_frame.par = XVID_PAR_EXT;
00689 xvid_enc_frame.par_width = avctx->sample_aspect_ratio.num;
00690 xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;
00691
00692
00693 if( x->qscale ) xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;
00694 else xvid_enc_frame.quant = 0;
00695
00696
00697 xvid_enc_frame.quant_intra_matrix = x->intra_matrix;
00698 xvid_enc_frame.quant_inter_matrix = x->inter_matrix;
00699
00700
00701 xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,
00702 &xvid_enc_frame, &xvid_enc_stats);
00703
00704
00705 avctx->stats_out = NULL;
00706 if( x->twopassbuffer ) {
00707 tmp = x->old_twopassbuffer;
00708 x->old_twopassbuffer = x->twopassbuffer;
00709 x->twopassbuffer = tmp;
00710 x->twopassbuffer[0] = 0;
00711 if( x->old_twopassbuffer[0] != 0 ) {
00712 avctx->stats_out = x->old_twopassbuffer;
00713 }
00714 }
00715
00716 if (xerr > 0) {
00717 *got_packet = 1;
00718
00719 p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;
00720 if( xvid_enc_stats.type == XVID_TYPE_PVOP )
00721 p->pict_type = AV_PICTURE_TYPE_P;
00722 else if( xvid_enc_stats.type == XVID_TYPE_BVOP )
00723 p->pict_type = AV_PICTURE_TYPE_B;
00724 else if( xvid_enc_stats.type == XVID_TYPE_SVOP )
00725 p->pict_type = AV_PICTURE_TYPE_S;
00726 else
00727 p->pict_type = AV_PICTURE_TYPE_I;
00728 if( xvid_enc_frame.out_flags & XVID_KEYFRAME ) {
00729 p->key_frame = 1;
00730 pkt->flags |= AV_PKT_FLAG_KEY;
00731 if( x->quicktime_format )
00732 return xvid_strip_vol_header(avctx, pkt,
00733 xvid_enc_stats.hlength, xerr);
00734 } else
00735 p->key_frame = 0;
00736
00737 pkt->size = xerr;
00738
00739 return 0;
00740 } else {
00741 if (!user_packet)
00742 av_free_packet(pkt);
00743 if (!xerr)
00744 return 0;
00745 av_log(avctx, AV_LOG_ERROR, "Xvid: Encoding Error Occurred: %i\n", xerr);
00746 return -1;
00747 }
00748 }
00749
00757 static av_cold int xvid_encode_close(AVCodecContext *avctx) {
00758 struct xvid_context *x = avctx->priv_data;
00759
00760 xvid_encore(x->encoder_handle, XVID_ENC_DESTROY, NULL, NULL);
00761
00762 av_freep(&avctx->extradata);
00763 if( x->twopassbuffer != NULL ) {
00764 av_free(x->twopassbuffer);
00765 av_free(x->old_twopassbuffer);
00766 avctx->stats_out = NULL;
00767 }
00768 av_free(x->twopassfile);
00769 av_free(x->intra_matrix);
00770 av_free(x->inter_matrix);
00771
00772 return 0;
00773 }
00774
00778 AVCodec ff_libxvid_encoder = {
00779 .name = "libxvid",
00780 .type = AVMEDIA_TYPE_VIDEO,
00781 .id = CODEC_ID_MPEG4,
00782 .priv_data_size = sizeof(struct xvid_context),
00783 .init = xvid_encode_init,
00784 .encode2 = xvid_encode_frame,
00785 .close = xvid_encode_close,
00786 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_YUV420P, PIX_FMT_NONE },
00787 .long_name = NULL_IF_CONFIG_SMALL("libxvidcore MPEG-4 part 2"),
00788 };