static int roq_encode_video(RoqContext *enc)

{

    RoqTempdata *tempData = enc->tmpData;

    int i, ret;



    memset(tempData, 0, sizeof(*tempData));



    ret = create_cel_evals(enc, tempData);

    if (ret < 0)

        return ret;



    ret = generate_new_codebooks(enc, tempData);

    if (ret < 0)

        return ret;



    if (enc->framesSinceKeyframe >= 1) {

        motion_search(enc, 8);

        motion_search(enc, 4);

    }



 retry_encode:

    for (i=0; i<enc->width*enc->height/64; i++)

        gather_data_for_cel(tempData->cel_evals + i, enc, tempData);



    /* Quake 3 can't handle chunks bigger than 65535 bytes */

    if (tempData->mainChunkSize/8 > 65535) {

        av_log(enc->avctx, AV_LOG_ERROR,

               "Warning, generated a frame too big (%d > 65535), "

               "try using a smaller qscale value.\n",

               tempData->mainChunkSize/8);

        enc->lambda *= 1.5;

        tempData->mainChunkSize = 0;

        memset(tempData->used_option, 0, sizeof(tempData->used_option));

        memset(tempData->codebooks.usedCB4, 0,

               sizeof(tempData->codebooks.usedCB4));

        memset(tempData->codebooks.usedCB2, 0,

               sizeof(tempData->codebooks.usedCB2));



        goto retry_encode;

    }



    remap_codebooks(enc, tempData);



    write_codebooks(enc, tempData);



    reconstruct_and_encode_image(enc, tempData, enc->width, enc->height,

                                 enc->width*enc->height/64);



    enc->avctx->coded_frame = enc->current_frame;



    /* Rotate frame history */

    FFSWAP(AVFrame *, enc->current_frame, enc->last_frame);

    FFSWAP(motion_vect *, enc->last_motion4, enc->this_motion4);

    FFSWAP(motion_vect *, enc->last_motion8, enc->this_motion8);



    av_free(tempData->cel_evals);

    av_free(tempData->closest_cb2);



    enc->framesSinceKeyframe++;



    return 0;

}

static int roq_encode_video(RoqContext *enc)

{

    RoqTempdata *tempData = enc->tmpData;

    int i, ret;



    memset(tempData, 0, sizeof(*tempData));



    ret = create_cel_evals(enc, tempData);

    if (ret < 0)

        return ret;



    ret = generate_new_codebooks(enc, tempData);

    if (ret < 0)

        return ret;



    if (enc->framesSinceKeyframe >= 1) {

        motion_search(enc, 8);

        motion_search(enc, 4);

    }



 retry_encode:

    for (i=0; i<enc->width*enc->height/64; i++)

        gather_data_for_cel(tempData->cel_evals + i, enc, tempData);



    

    if (tempData->mainChunkSize/8 > 65535) {

        av_log(enc->avctx, AV_LOG_ERROR,

               "Warning, generated a frame too big (%d > 65535), "

               "try using a smaller qscale value.\n",

               tempData->mainChunkSize/8);

        enc->lambda *= 1.5;

        tempData->mainChunkSize = 0;

        memset(tempData->used_option, 0, sizeof(tempData->used_option));

        memset(tempData->codebooks.usedCB4, 0,

               sizeof(tempData->codebooks.usedCB4));

        memset(tempData->codebooks.usedCB2, 0,

               sizeof(tempData->codebooks.usedCB2));



        goto retry_encode;

    }



    remap_codebooks(enc, tempData);



    write_codebooks(enc, tempData);



    reconstruct_and_encode_image(enc, tempData, enc->width, enc->height,

                                 enc->width*enc->height/64);



    enc->avctx->coded_frame = enc->current_frame;



    

    FFSWAP(AVFrame *, enc->current_frame, enc->last_frame);

    FFSWAP(motion_vect *, enc->last_motion4, enc->this_motion4);

    FFSWAP(motion_vect *, enc->last_motion8, enc->this_motion8);



    av_free(tempData->cel_evals);

    av_free(tempData->closest_cb2);



    enc->framesSinceKeyframe++;



    return 0;

}

static int FUNC_0(RoqContext *VAR_0)

{

    RoqTempdata *tempData = VAR_0->tmpData;

    int VAR_1, VAR_2;



    memset(tempData, 0, sizeof(*tempData));



    VAR_2 = create_cel_evals(VAR_0, tempData);

    if (VAR_2 < 0)

        return VAR_2;



    VAR_2 = generate_new_codebooks(VAR_0, tempData);

    if (VAR_2 < 0)

        return VAR_2;



    if (VAR_0->framesSinceKeyframe >= 1) {

        motion_search(VAR_0, 8);

        motion_search(VAR_0, 4);

    }



 retry_encode:

    for (VAR_1=0; VAR_1<VAR_0->width*VAR_0->height/64; VAR_1++)

        gather_data_for_cel(tempData->cel_evals + VAR_1, VAR_0, tempData);



    

    if (tempData->mainChunkSize/8 > 65535) {

        av_log(VAR_0->avctx, AV_LOG_ERROR,

               "Warning, generated a frame too big (%d > 65535), "

               "try using a smaller qscale value.\n",

               tempData->mainChunkSize/8);

        VAR_0->lambda *= 1.5;

        tempData->mainChunkSize = 0;

        memset(tempData->used_option, 0, sizeof(tempData->used_option));

        memset(tempData->codebooks.usedCB4, 0,

               sizeof(tempData->codebooks.usedCB4));

        memset(tempData->codebooks.usedCB2, 0,

               sizeof(tempData->codebooks.usedCB2));



        goto retry_encode;

    }



    remap_codebooks(VAR_0, tempData);



    write_codebooks(VAR_0, tempData);



    reconstruct_and_encode_image(VAR_0, tempData, VAR_0->width, VAR_0->height,

                                 VAR_0->width*VAR_0->height/64);



    VAR_0->avctx->coded_frame = VAR_0->current_frame;



    

    FFSWAP(AVFrame *, VAR_0->current_frame, VAR_0->last_frame);

    FFSWAP(motion_vect *, VAR_0->last_motion4, VAR_0->this_motion4);

    FFSWAP(motion_vect *, VAR_0->last_motion8, VAR_0->this_motion8);



    av_free(tempData->cel_evals);

    av_free(tempData->closest_cb2);



    VAR_0->framesSinceKeyframe++;



    return 0;

}

static int FUNC_0(RoqContext *VAR_0)
{ RoqTempdata *tempData = VAR_0->tmpData; int VAR_1, VAR_2; memset(tempData, 0, sizeof(*tempData)); VAR_2 = create_cel_evals(VAR_0, tempData); if (VAR_2 < 0)
return VAR_2; VAR_2 = generate_new_codebooks(VAR_0, tempData); if (VAR_2 < 0)
return VAR_2; if (VAR_0->framesSinceKeyframe >= 1) { motion_search(VAR_0, 8); motion_search(VAR_0, 4); } retry_encode:
for (VAR_1=0; VAR_1<VAR_0->width*VAR_0->height/64; VAR_1++) gather_data_for_cel(tempData->cel_evals + VAR_1, VAR_0, tempData); if (tempData->mainChunkSize/8 > 65535) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Warning, generated a frame too big (%d > 65535), "
"try using a smaller qscale value.\n",
tempData->mainChunkSize/8); VAR_0->lambda *= 1.5; tempData->mainChunkSize = 0; memset(tempData->used_option, 0, sizeof(tempData->used_option)); memset(tempData->codebooks.usedCB4, 0,
sizeof(tempData->codebooks.usedCB4)); memset(tempData->codebooks.usedCB2, 0,
sizeof(tempData->codebooks.usedCB2)); goto retry_encode; } remap_codebooks(VAR_0, tempData); write_codebooks(VAR_0, tempData); reconstruct_and_encode_image(VAR_0, tempData, VAR_0->width, VAR_0->height,
VAR_0->width*VAR_0->height/64); VAR_0->avctx->coded_frame = VAR_0->current_frame; FFSWAP(AVFrame *, VAR_0->current_frame, VAR_0->last_frame); FFSWAP(motion_vect *, VAR_0->last_motion4, VAR_0->this_motion4); FFSWAP(motion_vect *, VAR_0->last_motion8, VAR_0->this_motion8); av_free(tempData->cel_evals); av_free(tempData->closest_cb2); VAR_0->framesSinceKeyframe++; return 0; }