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