static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,

                             const AVFrame *picture, int *got_packet)

{

    int xerr, i, ret, user_packet = !!pkt->data;

    struct xvid_context *x = avctx->priv_data;

    AVFrame *p             = avctx->coded_frame;

    int mb_width  = (avctx->width  + 15) / 16;

    int mb_height = (avctx->height + 15) / 16;

    char *tmp;



    xvid_enc_frame_t xvid_enc_frame = { 0 };

    xvid_enc_stats_t xvid_enc_stats = { 0 };



    if ((ret = ff_alloc_packet2(avctx, pkt, mb_width*mb_height*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0)

        return ret;



    /* Start setting up the frame */

    xvid_enc_frame.version = XVID_VERSION;

    xvid_enc_stats.version = XVID_VERSION;



    /* Let Xvid know where to put the frame. */

    xvid_enc_frame.bitstream = pkt->data;

    xvid_enc_frame.length    = pkt->size;



    /* Initialize input image fields */

    if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {

        av_log(avctx, AV_LOG_ERROR,

               "Xvid: Color spaces other than 420P not supported\n");

        return AVERROR(EINVAL);

    }



    xvid_enc_frame.input.csp = XVID_CSP_PLANAR; /* YUV420P */



    for (i = 0; i < 4; i++) {

        xvid_enc_frame.input.plane[i]  = picture->data[i];

        xvid_enc_frame.input.stride[i] = picture->linesize[i];

    }



    /* Encoder Flags */

    xvid_enc_frame.vop_flags = x->vop_flags;

    xvid_enc_frame.vol_flags = x->vol_flags;

    xvid_enc_frame.motion    = x->me_flags;

    xvid_enc_frame.type      =

        picture->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :

        picture->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :

        picture->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :

                                                  XVID_TYPE_AUTO;



    /* Pixel aspect ratio setting */

    if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.num > 255 ||

        avctx->sample_aspect_ratio.den < 0 || avctx->sample_aspect_ratio.den > 255) {

        av_log(avctx, AV_LOG_WARNING,

               "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",

               avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);

        av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,

                   avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den, 255);

    }

    xvid_enc_frame.par        = XVID_PAR_EXT;

    xvid_enc_frame.par_width  = avctx->sample_aspect_ratio.num;

    xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;



    /* Quant Setting */

    if (x->qscale)

        xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;

    else

        xvid_enc_frame.quant = 0;



    /* Matrices */

    xvid_enc_frame.quant_intra_matrix = x->intra_matrix;

    xvid_enc_frame.quant_inter_matrix = x->inter_matrix;



    /* Encode */

    xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,

                       &xvid_enc_frame, &xvid_enc_stats);



    /* Two-pass log buffer swapping */

    avctx->stats_out = NULL;

    if (x->twopassbuffer) {

        tmp                  = x->old_twopassbuffer;

        x->old_twopassbuffer = x->twopassbuffer;

        x->twopassbuffer     = tmp;

        x->twopassbuffer[0]  = 0;

        if (x->old_twopassbuffer[0] != 0) {

            avctx->stats_out = x->old_twopassbuffer;

        }

    }



    if (xerr > 0) {

        *got_packet = 1;



        p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;

        if (xvid_enc_stats.type == XVID_TYPE_PVOP)

            p->pict_type = AV_PICTURE_TYPE_P;

        else if (xvid_enc_stats.type == XVID_TYPE_BVOP)

            p->pict_type = AV_PICTURE_TYPE_B;

        else if (xvid_enc_stats.type == XVID_TYPE_SVOP)

            p->pict_type = AV_PICTURE_TYPE_S;

        else

            p->pict_type = AV_PICTURE_TYPE_I;

        if (xvid_enc_frame.out_flags & XVID_KEYFRAME) {

            p->key_frame = 1;

            pkt->flags  |= AV_PKT_FLAG_KEY;

            if (x->quicktime_format)

                return xvid_strip_vol_header(avctx, pkt,

                                             xvid_enc_stats.hlength, xerr);

        } else

            p->key_frame = 0;



        pkt->size = xerr;



        return 0;

    } else {

        if (!user_packet)

            av_free_packet(pkt);

        if (!xerr)

            return 0;

        av_log(avctx, AV_LOG_ERROR,

               "Xvid: Encoding Error Occurred: %i\n", xerr);

        return AVERROR_EXTERNAL;

    }

}

static int xvid_encode_frame(AVCodecContext *avctx, AVPacket *pkt,

                             const AVFrame *picture, int *got_packet)

{

    int xerr, i, ret, user_packet = !!pkt->data;

    struct xvid_context *x = avctx->priv_data;

    AVFrame *p             = avctx->coded_frame;

    int mb_width  = (avctx->width  + 15) / 16;

    int mb_height = (avctx->height + 15) / 16;

    char *tmp;



    xvid_enc_frame_t xvid_enc_frame = { 0 };

    xvid_enc_stats_t xvid_enc_stats = { 0 };



    if ((ret = ff_alloc_packet2(avctx, pkt, mb_width*mb_height*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0)

        return ret;



    

    xvid_enc_frame.version = XVID_VERSION;

    xvid_enc_stats.version = XVID_VERSION;



    

    xvid_enc_frame.bitstream = pkt->data;

    xvid_enc_frame.length    = pkt->size;



    

    if (avctx->pix_fmt != AV_PIX_FMT_YUV420P) {

        av_log(avctx, AV_LOG_ERROR,

               "Xvid: Color spaces other than 420P not supported\n");

        return AVERROR(EINVAL);

    }



    xvid_enc_frame.input.csp = XVID_CSP_PLANAR; 



    for (i = 0; i < 4; i++) {

        xvid_enc_frame.input.plane[i]  = picture->data[i];

        xvid_enc_frame.input.stride[i] = picture->linesize[i];

    }



    

    xvid_enc_frame.vop_flags = x->vop_flags;

    xvid_enc_frame.vol_flags = x->vol_flags;

    xvid_enc_frame.motion    = x->me_flags;

    xvid_enc_frame.type      =

        picture->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :

        picture->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :

        picture->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :

                                                  XVID_TYPE_AUTO;



    

    if (avctx->sample_aspect_ratio.num < 0 || avctx->sample_aspect_ratio.num > 255 ||

        avctx->sample_aspect_ratio.den < 0 || avctx->sample_aspect_ratio.den > 255) {

        av_log(avctx, AV_LOG_WARNING,

               "Invalid pixel aspect ratio %i/%i, limit is 255/255 reducing\n",

               avctx->sample_aspect_ratio.num, avctx->sample_aspect_ratio.den);

        av_reduce(&avctx->sample_aspect_ratio.num, &avctx->sample_aspect_ratio.den,

                   avctx->sample_aspect_ratio.num,  avctx->sample_aspect_ratio.den, 255);

    }

    xvid_enc_frame.par        = XVID_PAR_EXT;

    xvid_enc_frame.par_width  = avctx->sample_aspect_ratio.num;

    xvid_enc_frame.par_height = avctx->sample_aspect_ratio.den;



    

    if (x->qscale)

        xvid_enc_frame.quant = picture->quality / FF_QP2LAMBDA;

    else

        xvid_enc_frame.quant = 0;



    

    xvid_enc_frame.quant_intra_matrix = x->intra_matrix;

    xvid_enc_frame.quant_inter_matrix = x->inter_matrix;



    

    xerr = xvid_encore(x->encoder_handle, XVID_ENC_ENCODE,

                       &xvid_enc_frame, &xvid_enc_stats);



    

    avctx->stats_out = NULL;

    if (x->twopassbuffer) {

        tmp                  = x->old_twopassbuffer;

        x->old_twopassbuffer = x->twopassbuffer;

        x->twopassbuffer     = tmp;

        x->twopassbuffer[0]  = 0;

        if (x->old_twopassbuffer[0] != 0) {

            avctx->stats_out = x->old_twopassbuffer;

        }

    }



    if (xerr > 0) {

        *got_packet = 1;



        p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;

        if (xvid_enc_stats.type == XVID_TYPE_PVOP)

            p->pict_type = AV_PICTURE_TYPE_P;

        else if (xvid_enc_stats.type == XVID_TYPE_BVOP)

            p->pict_type = AV_PICTURE_TYPE_B;

        else if (xvid_enc_stats.type == XVID_TYPE_SVOP)

            p->pict_type = AV_PICTURE_TYPE_S;

        else

            p->pict_type = AV_PICTURE_TYPE_I;

        if (xvid_enc_frame.out_flags & XVID_KEYFRAME) {

            p->key_frame = 1;

            pkt->flags  |= AV_PKT_FLAG_KEY;

            if (x->quicktime_format)

                return xvid_strip_vol_header(avctx, pkt,

                                             xvid_enc_stats.hlength, xerr);

        } else

            p->key_frame = 0;



        pkt->size = xerr;



        return 0;

    } else {

        if (!user_packet)

            av_free_packet(pkt);

        if (!xerr)

            return 0;

        av_log(avctx, AV_LOG_ERROR,

               "Xvid: Encoding Error Occurred: %i\n", xerr);

        return AVERROR_EXTERNAL;

    }

}

    if ((ret = ff_alloc_packet2(avctx, pkt, mb_width*mb_height*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0)
static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,

                             const AVFrame *VAR_2, int *VAR_3)

{

    int VAR_4, VAR_5, VAR_6, VAR_7 = !!VAR_1->data;

    struct xvid_context *VAR_8 = VAR_0->priv_data;

    AVFrame *p             = VAR_0->coded_frame;

    int VAR_9  = (VAR_0->width  + 15) / 16;

    int VAR_10 = (VAR_0->height + 15) / 16;

    char *VAR_11;



    xvid_enc_frame_t xvid_enc_frame = { 0 };

    xvid_enc_stats_t xvid_enc_stats = { 0 };



    if ((VAR_6 = ff_alloc_packet2(VAR_0, VAR_1, VAR_9*VAR_10*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0)

        return VAR_6;



    

    xvid_enc_frame.version = XVID_VERSION;

    xvid_enc_stats.version = XVID_VERSION;



    

    xvid_enc_frame.bitstream = VAR_1->data;

    xvid_enc_frame.length    = VAR_1->size;



    

    if (VAR_0->pix_fmt != AV_PIX_FMT_YUV420P) {

        av_log(VAR_0, AV_LOG_ERROR,

               "Xvid: Color spaces other than 420P not supported\n");

        return AVERROR(EINVAL);

    }



    xvid_enc_frame.input.csp = XVID_CSP_PLANAR; 



    for (VAR_5 = 0; VAR_5 < 4; VAR_5++) {

        xvid_enc_frame.input.plane[VAR_5]  = VAR_2->data[VAR_5];

        xvid_enc_frame.input.stride[VAR_5] = VAR_2->linesize[VAR_5];

    }



    

    xvid_enc_frame.vop_flags = VAR_8->vop_flags;

    xvid_enc_frame.vol_flags = VAR_8->vol_flags;

    xvid_enc_frame.motion    = VAR_8->me_flags;

    xvid_enc_frame.type      =

        VAR_2->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :

        VAR_2->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :

        VAR_2->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :

                                                  XVID_TYPE_AUTO;



    

    if (VAR_0->sample_aspect_ratio.num < 0 || VAR_0->sample_aspect_ratio.num > 255 ||

        VAR_0->sample_aspect_ratio.den < 0 || VAR_0->sample_aspect_ratio.den > 255) {

        av_log(VAR_0, AV_LOG_WARNING,

               "Invalid pixel aspect ratio %VAR_5/%VAR_5, limit is 255/255 reducing\n",

               VAR_0->sample_aspect_ratio.num, VAR_0->sample_aspect_ratio.den);

        av_reduce(&VAR_0->sample_aspect_ratio.num, &VAR_0->sample_aspect_ratio.den,

                   VAR_0->sample_aspect_ratio.num,  VAR_0->sample_aspect_ratio.den, 255);

    }

    xvid_enc_frame.par        = XVID_PAR_EXT;

    xvid_enc_frame.par_width  = VAR_0->sample_aspect_ratio.num;

    xvid_enc_frame.par_height = VAR_0->sample_aspect_ratio.den;



    

    if (VAR_8->qscale)

        xvid_enc_frame.quant = VAR_2->quality / FF_QP2LAMBDA;

    else

        xvid_enc_frame.quant = 0;



    

    xvid_enc_frame.quant_intra_matrix = VAR_8->intra_matrix;

    xvid_enc_frame.quant_inter_matrix = VAR_8->inter_matrix;



    

    VAR_4 = xvid_encore(VAR_8->encoder_handle, XVID_ENC_ENCODE,

                       &xvid_enc_frame, &xvid_enc_stats);



    

    VAR_0->stats_out = NULL;

    if (VAR_8->twopassbuffer) {

        VAR_11                  = VAR_8->old_twopassbuffer;

        VAR_8->old_twopassbuffer = VAR_8->twopassbuffer;

        VAR_8->twopassbuffer     = VAR_11;

        VAR_8->twopassbuffer[0]  = 0;

        if (VAR_8->old_twopassbuffer[0] != 0) {

            VAR_0->stats_out = VAR_8->old_twopassbuffer;

        }

    }



    if (VAR_4 > 0) {

        *VAR_3 = 1;



        p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA;

        if (xvid_enc_stats.type == XVID_TYPE_PVOP)

            p->pict_type = AV_PICTURE_TYPE_P;

        else if (xvid_enc_stats.type == XVID_TYPE_BVOP)

            p->pict_type = AV_PICTURE_TYPE_B;

        else if (xvid_enc_stats.type == XVID_TYPE_SVOP)

            p->pict_type = AV_PICTURE_TYPE_S;

        else

            p->pict_type = AV_PICTURE_TYPE_I;

        if (xvid_enc_frame.out_flags & XVID_KEYFRAME) {

            p->key_frame = 1;

            VAR_1->flags  |= AV_PKT_FLAG_KEY;

            if (VAR_8->quicktime_format)

                return xvid_strip_vol_header(VAR_0, VAR_1,

                                             xvid_enc_stats.hlength, VAR_4);

        } else

            p->key_frame = 0;



        VAR_1->size = VAR_4;



        return 0;

    } else {

        if (!VAR_7)

            av_free_packet(VAR_1);

        if (!VAR_4)

            return 0;

        av_log(VAR_0, AV_LOG_ERROR,

               "Xvid: Encoding Error Occurred: %VAR_5\n", VAR_4);

        return AVERROR_EXTERNAL;

    }

}

static int FUNC_0(AVCodecContext *VAR_0, AVPacket *VAR_1,
const AVFrame *VAR_2, int *VAR_3)
{ int VAR_4, VAR_5, VAR_6, VAR_7 = !!VAR_1->data; struct xvid_context *VAR_8 = VAR_0->priv_data; AVFrame *p             = VAR_0->coded_frame; int VAR_9  = (VAR_0->width  + 15) / 16; int VAR_10 = (VAR_0->height + 15) / 16; char *VAR_11; xvid_enc_frame_t xvid_enc_frame = { 0 }; xvid_enc_stats_t xvid_enc_stats = { 0 }; if ((VAR_6 = ff_alloc_packet2(VAR_0, VAR_1, VAR_9*VAR_10*MAX_MB_BYTES + FF_MIN_BUFFER_SIZE)) < 0)
return VAR_6; xvid_enc_frame.version = XVID_VERSION; xvid_enc_stats.version = XVID_VERSION; xvid_enc_frame.bitstream = VAR_1->data; xvid_enc_frame.length    = VAR_1->size; if (VAR_0->pix_fmt != AV_PIX_FMT_YUV420P) { av_log(VAR_0, AV_LOG_ERROR,
"Xvid: Color spaces other than 420P not supported\n"); return AVERROR(EINVAL); } xvid_enc_frame.input.csp = XVID_CSP_PLANAR; for (VAR_5 = 0; VAR_5 < 4; VAR_5++) { xvid_enc_frame.input.plane[VAR_5]  = VAR_2->data[VAR_5]; xvid_enc_frame.input.stride[VAR_5] = VAR_2->linesize[VAR_5]; } xvid_enc_frame.vop_flags = VAR_8->vop_flags; xvid_enc_frame.vol_flags = VAR_8->vol_flags; xvid_enc_frame.motion    = VAR_8->me_flags; xvid_enc_frame.type      =
VAR_2->pict_type == AV_PICTURE_TYPE_I ? XVID_TYPE_IVOP :
VAR_2->pict_type == AV_PICTURE_TYPE_P ? XVID_TYPE_PVOP :
VAR_2->pict_type == AV_PICTURE_TYPE_B ? XVID_TYPE_BVOP :
XVID_TYPE_AUTO; if (VAR_0->sample_aspect_ratio.num < 0 || VAR_0->sample_aspect_ratio.num > 255 ||
VAR_0->sample_aspect_ratio.den < 0 || VAR_0->sample_aspect_ratio.den > 255) { av_log(VAR_0, AV_LOG_WARNING,
"Invalid pixel aspect ratio %VAR_5/%VAR_5, limit is 255/255 reducing\n",
VAR_0->sample_aspect_ratio.num, VAR_0->sample_aspect_ratio.den); av_reduce(&VAR_0->sample_aspect_ratio.num, &VAR_0->sample_aspect_ratio.den,
VAR_0->sample_aspect_ratio.num,  VAR_0->sample_aspect_ratio.den, 255); } xvid_enc_frame.par        = XVID_PAR_EXT; xvid_enc_frame.par_width  = VAR_0->sample_aspect_ratio.num; xvid_enc_frame.par_height = VAR_0->sample_aspect_ratio.den; if (VAR_8->qscale)
xvid_enc_frame.quant = VAR_2->quality / FF_QP2LAMBDA; else
xvid_enc_frame.quant = 0; xvid_enc_frame.quant_intra_matrix = VAR_8->intra_matrix; xvid_enc_frame.quant_inter_matrix = VAR_8->inter_matrix; VAR_4 = xvid_encore(VAR_8->encoder_handle, XVID_ENC_ENCODE,
&xvid_enc_frame, &xvid_enc_stats); VAR_0->stats_out = NULL; if (VAR_8->twopassbuffer) { VAR_11                  = VAR_8->old_twopassbuffer; VAR_8->old_twopassbuffer = VAR_8->twopassbuffer; VAR_8->twopassbuffer     = VAR_11; VAR_8->twopassbuffer[0]  = 0; if (VAR_8->old_twopassbuffer[0] != 0) { VAR_0->stats_out = VAR_8->old_twopassbuffer; } } if (VAR_4 > 0) { *VAR_3 = 1; p->quality = xvid_enc_stats.quant * FF_QP2LAMBDA; if (xvid_enc_stats.type == XVID_TYPE_PVOP)
p->pict_type = AV_PICTURE_TYPE_P; else if (xvid_enc_stats.type == XVID_TYPE_BVOP)
p->pict_type = AV_PICTURE_TYPE_B; else if (xvid_enc_stats.type == XVID_TYPE_SVOP)
p->pict_type = AV_PICTURE_TYPE_S; else
p->pict_type = AV_PICTURE_TYPE_I; if (xvid_enc_frame.out_flags & XVID_KEYFRAME) { p->key_frame = 1; VAR_1->flags  |= AV_PKT_FLAG_KEY; if (VAR_8->quicktime_format)
return xvid_strip_vol_header(VAR_0, VAR_1,
xvid_enc_stats.hlength, VAR_4); } else p->key_frame = 0; VAR_1->size = VAR_4; return 0; } else { if (!VAR_7)
av_free_packet(VAR_1); if (!VAR_4)
return 0; av_log(VAR_0, AV_LOG_ERROR,
"Xvid: Encoding Error Occurred: %VAR_5\n", VAR_4); return AVERROR_EXTERNAL; } }