static int encode_mode(CinepakEncContext *s, CinepakMode mode, int h, int v1_size, int v4_size, int v4, AVPicture *scratch_pict, strip_info *info, unsigned char *buf)

{

    int x, y, z, flags, bits, temp_size, header_ofs, ret = 0, mb_count = s->w * h / MB_AREA;

    int needs_extra_bit, should_write_temp;

    unsigned char temp[64]; //32/2 = 16 V4 blocks at 4 B each -> 64 B

    mb_info *mb;

    AVPicture sub_scratch;



    //encode codebooks

    if(v1_size)

        ret += encode_codebook(s, info->v1_codebook, v1_size, 0x22, 0x26, buf + ret);



    if(v4_size)

        ret += encode_codebook(s, info->v4_codebook, v4_size, 0x20, 0x24, buf + ret);



    //update scratch picture

    for(z = y = 0; y < h; y += MB_SIZE) {

        for(x = 0; x < s->w; x += MB_SIZE, z++) {

            mb = &s->mb[z];



            if(mode == MODE_MC && mb->best_encoding == ENC_SKIP)

                continue;



            get_sub_picture(s, x, y, scratch_pict, &sub_scratch);



            if(mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)

                decode_v1_vector(s, &sub_scratch, mb, info);

            else if(mode != MODE_V1_ONLY && mb->best_encoding == ENC_V4)

                decode_v4_vector(s, &sub_scratch, mb->v4_vector[v4], info);

        }

    }



    switch(mode) {

    case MODE_V1_ONLY:

        //av_log(s->avctx, AV_LOG_INFO, "mb_count = %i\n", mb_count);

        ret += write_chunk_header(buf + ret, 0x32, mb_count);



        for(x = 0; x < mb_count; x++)

            buf[ret++] = s->mb[x].v1_vector;



        break;

    case MODE_V1_V4:

        //remember header position

        header_ofs = ret;

        ret += CHUNK_HEADER_SIZE;



        for(x = 0; x < mb_count; x += 32) {

            flags = 0;

            for(y = x; y < FFMIN(x+32, mb_count); y++)

                if(s->mb[y].best_encoding == ENC_V4)

                    flags |= 1 << (31 - y + x);



            AV_WB32(&buf[ret], flags);

            ret += 4;



            for(y = x; y < FFMIN(x+32, mb_count); y++) {

                mb = &s->mb[y];



                if(mb->best_encoding == ENC_V1)

                    buf[ret++] = mb->v1_vector;

                else

                    for(z = 0; z < 4; z++)

                        buf[ret++] = mb->v4_vector[v4][z];

            }

        }



        write_chunk_header(buf + header_ofs, 0x30, ret - header_ofs - CHUNK_HEADER_SIZE);



        break;

    case MODE_MC:

        //remember header position

        header_ofs = ret;

        ret += CHUNK_HEADER_SIZE;

        flags = bits = temp_size = 0;



        for(x = 0; x < mb_count; x++) {

            mb = &s->mb[x];

            flags |= (mb->best_encoding != ENC_SKIP) << (31 - bits++);

            needs_extra_bit = 0;

            should_write_temp = 0;



            if(mb->best_encoding != ENC_SKIP) {

                if(bits < 32)

                    flags |= (mb->best_encoding == ENC_V4) << (31 - bits++);

                else

                    needs_extra_bit = 1;

            }



            if(bits == 32) {

                AV_WB32(&buf[ret], flags);

                ret += 4;

                flags = bits = 0;



                if(mb->best_encoding == ENC_SKIP || needs_extra_bit) {

                    memcpy(&buf[ret], temp, temp_size);

                    ret += temp_size;

                    temp_size = 0;

                } else

                    should_write_temp = 1;

            }



            if(needs_extra_bit) {

                flags = (mb->best_encoding == ENC_V4) << 31;

                bits = 1;

            }



            if(mb->best_encoding == ENC_V1)

                temp[temp_size++] = mb->v1_vector;

            else if(mb->best_encoding == ENC_V4)

                for(z = 0; z < 4; z++)

                    temp[temp_size++] = mb->v4_vector[v4][z];



            if(should_write_temp) {

                memcpy(&buf[ret], temp, temp_size);

                ret += temp_size;

                temp_size = 0;

            }

        }



        if(bits > 0) {

            AV_WB32(&buf[ret], flags);

            ret += 4;

            memcpy(&buf[ret], temp, temp_size);

            ret += temp_size;

        }



        write_chunk_header(buf + header_ofs, 0x31, ret - header_ofs - CHUNK_HEADER_SIZE);



        break;

    }



    return ret;

}

static int encode_mode(CinepakEncContext *s, CinepakMode mode, int h, int v1_size, int v4_size, int v4, AVPicture *scratch_pict, strip_info *info, unsigned char *buf)

{

    int x, y, z, flags, bits, temp_size, header_ofs, ret = 0, mb_count = s->w * h / MB_AREA;

    int needs_extra_bit, should_write_temp;

    unsigned char temp[64]; 

    mb_info *mb;

    AVPicture sub_scratch;



    

    if(v1_size)

        ret += encode_codebook(s, info->v1_codebook, v1_size, 0x22, 0x26, buf + ret);



    if(v4_size)

        ret += encode_codebook(s, info->v4_codebook, v4_size, 0x20, 0x24, buf + ret);



    

    for(z = y = 0; y < h; y += MB_SIZE) {

        for(x = 0; x < s->w; x += MB_SIZE, z++) {

            mb = &s->mb[z];



            if(mode == MODE_MC && mb->best_encoding == ENC_SKIP)

                continue;



            get_sub_picture(s, x, y, scratch_pict, &sub_scratch);



            if(mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)

                decode_v1_vector(s, &sub_scratch, mb, info);

            else if(mode != MODE_V1_ONLY && mb->best_encoding == ENC_V4)

                decode_v4_vector(s, &sub_scratch, mb->v4_vector[v4], info);

        }

    }



    switch(mode) {

    case MODE_V1_ONLY:

        

        ret += write_chunk_header(buf + ret, 0x32, mb_count);



        for(x = 0; x < mb_count; x++)

            buf[ret++] = s->mb[x].v1_vector;



        break;

    case MODE_V1_V4:

        

        header_ofs = ret;

        ret += CHUNK_HEADER_SIZE;



        for(x = 0; x < mb_count; x += 32) {

            flags = 0;

            for(y = x; y < FFMIN(x+32, mb_count); y++)

                if(s->mb[y].best_encoding == ENC_V4)

                    flags |= 1 << (31 - y + x);



            AV_WB32(&buf[ret], flags);

            ret += 4;



            for(y = x; y < FFMIN(x+32, mb_count); y++) {

                mb = &s->mb[y];



                if(mb->best_encoding == ENC_V1)

                    buf[ret++] = mb->v1_vector;

                else

                    for(z = 0; z < 4; z++)

                        buf[ret++] = mb->v4_vector[v4][z];

            }

        }



        write_chunk_header(buf + header_ofs, 0x30, ret - header_ofs - CHUNK_HEADER_SIZE);



        break;

    case MODE_MC:

        

        header_ofs = ret;

        ret += CHUNK_HEADER_SIZE;

        flags = bits = temp_size = 0;



        for(x = 0; x < mb_count; x++) {

            mb = &s->mb[x];

            flags |= (mb->best_encoding != ENC_SKIP) << (31 - bits++);

            needs_extra_bit = 0;

            should_write_temp = 0;



            if(mb->best_encoding != ENC_SKIP) {

                if(bits < 32)

                    flags |= (mb->best_encoding == ENC_V4) << (31 - bits++);

                else

                    needs_extra_bit = 1;

            }



            if(bits == 32) {

                AV_WB32(&buf[ret], flags);

                ret += 4;

                flags = bits = 0;



                if(mb->best_encoding == ENC_SKIP || needs_extra_bit) {

                    memcpy(&buf[ret], temp, temp_size);

                    ret += temp_size;

                    temp_size = 0;

                } else

                    should_write_temp = 1;

            }



            if(needs_extra_bit) {

                flags = (mb->best_encoding == ENC_V4) << 31;

                bits = 1;

            }



            if(mb->best_encoding == ENC_V1)

                temp[temp_size++] = mb->v1_vector;

            else if(mb->best_encoding == ENC_V4)

                for(z = 0; z < 4; z++)

                    temp[temp_size++] = mb->v4_vector[v4][z];



            if(should_write_temp) {

                memcpy(&buf[ret], temp, temp_size);

                ret += temp_size;

                temp_size = 0;

            }

        }



        if(bits > 0) {

            AV_WB32(&buf[ret], flags);

            ret += 4;

            memcpy(&buf[ret], temp, temp_size);

            ret += temp_size;

        }



        write_chunk_header(buf + header_ofs, 0x31, ret - header_ofs - CHUNK_HEADER_SIZE);



        break;

    }



    return ret;

}

    switch(mode) {         for(x = 0; x < mb_count; x++) {             mb = &s->mb[x];         for(x = 0; x < mb_count; x++) {             mb = &s->mb[x]; static int encode_mode(CinepakEncContext *s, CinepakMode mode, int h, int v1_size, int v4_size, int v4, AVPicture *scratch_pict, strip_info *info, unsigned char *buf)     AVPicture sub_scratch;     if(v1_size)         ret += encode_codebook(s, info->v1_codebook, v1_size, 0x22, 0x26, buf + ret);     if(v4_size)         ret += encode_codebook(s, info->v4_codebook, v4_size, 0x20, 0x24, buf + ret);             if(mode == MODE_MC && mb->best_encoding == ENC_SKIP)                 continue;             if(mode == MODE_V1_ONLY || mb->best_encoding == ENC_V1)                 decode_v1_vector(s, &sub_scratch, mb, info);             else if(mode != MODE_V1_ONLY && mb->best_encoding == ENC_V4)                 decode_v4_vector(s, &sub_scratch, mb->v4_vector[v4], info);     switch(mode) {                         buf[ret++] = mb->v4_vector[v4][z];                     temp[temp_size++] = mb->v4_vector[v4][z];     return ret;
static int FUNC_0(CinepakEncContext *VAR_0, CinepakMode VAR_1, int VAR_2, int VAR_3, int VAR_4, int VAR_5, AVPicture *VAR_6, strip_info *VAR_7, unsigned char *VAR_8)

{

    int VAR_9, VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15, VAR_16 = 0, VAR_17 = VAR_0->w * VAR_2 / MB_AREA;

    int VAR_18, VAR_19;

    unsigned char VAR_20[64]; 

    mb_info *mb;

    AVPicture sub_scratch;



    

    if(VAR_3)

        VAR_16 += encode_codebook(VAR_0, VAR_7->v1_codebook, VAR_3, 0x22, 0x26, VAR_8 + VAR_16);



    if(VAR_4)

        VAR_16 += encode_codebook(VAR_0, VAR_7->v4_codebook, VAR_4, 0x20, 0x24, VAR_8 + VAR_16);



    

    for(VAR_11 = VAR_10 = 0; VAR_10 < VAR_2; VAR_10 += MB_SIZE) {

        for(VAR_9 = 0; VAR_9 < VAR_0->w; VAR_9 += MB_SIZE, VAR_11++) {

            mb = &VAR_0->mb[VAR_11];



            if(VAR_1 == MODE_MC && mb->best_encoding == ENC_SKIP)

                continue;



            get_sub_picture(VAR_0, VAR_9, VAR_10, VAR_6, &sub_scratch);



            if(VAR_1 == MODE_V1_ONLY || mb->best_encoding == ENC_V1)

                decode_v1_vector(VAR_0, &sub_scratch, mb, VAR_7);

            else if(VAR_1 != MODE_V1_ONLY && mb->best_encoding == ENC_V4)

                decode_v4_vector(VAR_0, &sub_scratch, mb->v4_vector[VAR_5], VAR_7);

        }

    }



    switch(VAR_1) {

    case MODE_V1_ONLY:

        

        VAR_16 += write_chunk_header(VAR_8 + VAR_16, 0x32, VAR_17);



        for(VAR_9 = 0; VAR_9 < VAR_17; VAR_9++)

            VAR_8[VAR_16++] = VAR_0->mb[VAR_9].v1_vector;



        break;

    case MODE_V1_V4:

        

        VAR_15 = VAR_16;

        VAR_16 += CHUNK_HEADER_SIZE;



        for(VAR_9 = 0; VAR_9 < VAR_17; VAR_9 += 32) {

            VAR_12 = 0;

            for(VAR_10 = VAR_9; VAR_10 < FFMIN(VAR_9+32, VAR_17); VAR_10++)

                if(VAR_0->mb[VAR_10].best_encoding == ENC_V4)

                    VAR_12 |= 1 << (31 - VAR_10 + VAR_9);



            AV_WB32(&VAR_8[VAR_16], VAR_12);

            VAR_16 += 4;



            for(VAR_10 = VAR_9; VAR_10 < FFMIN(VAR_9+32, VAR_17); VAR_10++) {

                mb = &VAR_0->mb[VAR_10];



                if(mb->best_encoding == ENC_V1)

                    VAR_8[VAR_16++] = mb->v1_vector;

                else

                    for(VAR_11 = 0; VAR_11 < 4; VAR_11++)

                        VAR_8[VAR_16++] = mb->v4_vector[VAR_5][VAR_11];

            }

        }



        write_chunk_header(VAR_8 + VAR_15, 0x30, VAR_16 - VAR_15 - CHUNK_HEADER_SIZE);



        break;

    case MODE_MC:

        

        VAR_15 = VAR_16;

        VAR_16 += CHUNK_HEADER_SIZE;

        VAR_12 = VAR_13 = VAR_14 = 0;



        for(VAR_9 = 0; VAR_9 < VAR_17; VAR_9++) {

            mb = &VAR_0->mb[VAR_9];

            VAR_12 |= (mb->best_encoding != ENC_SKIP) << (31 - VAR_13++);

            VAR_18 = 0;

            VAR_19 = 0;



            if(mb->best_encoding != ENC_SKIP) {

                if(VAR_13 < 32)

                    VAR_12 |= (mb->best_encoding == ENC_V4) << (31 - VAR_13++);

                else

                    VAR_18 = 1;

            }



            if(VAR_13 == 32) {

                AV_WB32(&VAR_8[VAR_16], VAR_12);

                VAR_16 += 4;

                VAR_12 = VAR_13 = 0;



                if(mb->best_encoding == ENC_SKIP || VAR_18) {

                    memcpy(&VAR_8[VAR_16], VAR_20, VAR_14);

                    VAR_16 += VAR_14;

                    VAR_14 = 0;

                } else

                    VAR_19 = 1;

            }



            if(VAR_18) {

                VAR_12 = (mb->best_encoding == ENC_V4) << 31;

                VAR_13 = 1;

            }



            if(mb->best_encoding == ENC_V1)

                VAR_20[VAR_14++] = mb->v1_vector;

            else if(mb->best_encoding == ENC_V4)

                for(VAR_11 = 0; VAR_11 < 4; VAR_11++)

                    VAR_20[VAR_14++] = mb->v4_vector[VAR_5][VAR_11];



            if(VAR_19) {

                memcpy(&VAR_8[VAR_16], VAR_20, VAR_14);

                VAR_16 += VAR_14;

                VAR_14 = 0;

            }

        }



        if(VAR_13 > 0) {

            AV_WB32(&VAR_8[VAR_16], VAR_12);

            VAR_16 += 4;

            memcpy(&VAR_8[VAR_16], VAR_20, VAR_14);

            VAR_16 += VAR_14;

        }



        write_chunk_header(VAR_8 + VAR_15, 0x31, VAR_16 - VAR_15 - CHUNK_HEADER_SIZE);



        break;

    }



    return VAR_16;

}

static int FUNC_0(CinepakEncContext *VAR_0, CinepakMode VAR_1, int VAR_2, int VAR_3, int VAR_4, int VAR_5, AVPicture *VAR_6, strip_info *VAR_7, unsigned char *VAR_8)
{ int VAR_9, VAR_10, VAR_11, VAR_12, VAR_13, VAR_14, VAR_15, VAR_16 = 0, VAR_17 = VAR_0->w * VAR_2 / MB_AREA; int VAR_18, VAR_19; unsigned char VAR_20[64]; mb_info *mb; AVPicture sub_scratch; if(VAR_3)
VAR_16 += encode_codebook(VAR_0, VAR_7->v1_codebook, VAR_3, 0x22, 0x26, VAR_8 + VAR_16); if(VAR_4)
VAR_16 += encode_codebook(VAR_0, VAR_7->v4_codebook, VAR_4, 0x20, 0x24, VAR_8 + VAR_16); for(VAR_11 = VAR_10 = 0; VAR_10 < VAR_2; VAR_10 += MB_SIZE) { for(VAR_9 = 0; VAR_9 < VAR_0->w; VAR_9 += MB_SIZE, VAR_11++) { mb = &VAR_0->mb[VAR_11]; if(VAR_1 == MODE_MC && mb->best_encoding == ENC_SKIP)
continue; get_sub_picture(VAR_0, VAR_9, VAR_10, VAR_6, &sub_scratch); if(VAR_1 == MODE_V1_ONLY || mb->best_encoding == ENC_V1)
decode_v1_vector(VAR_0, &sub_scratch, mb, VAR_7); else if(VAR_1 != MODE_V1_ONLY && mb->best_encoding == ENC_V4)
decode_v4_vector(VAR_0, &sub_scratch, mb->v4_vector[VAR_5], VAR_7); } } switch(VAR_1) { case MODE_V1_ONLY:
VAR_16 += write_chunk_header(VAR_8 + VAR_16, 0x32, VAR_17); for(VAR_9 = 0; VAR_9 < VAR_17; VAR_9++) VAR_8[VAR_16++] = VAR_0->mb[VAR_9].v1_vector; break; case MODE_V1_V4:
VAR_15 = VAR_16; VAR_16 += CHUNK_HEADER_SIZE; for(VAR_9 = 0; VAR_9 < VAR_17; VAR_9 += 32) { VAR_12 = 0; for(VAR_10 = VAR_9; VAR_10 < FFMIN(VAR_9+32, VAR_17); VAR_10++) if(VAR_0->mb[VAR_10].best_encoding == ENC_V4)
VAR_12 |= 1 << (31 - VAR_10 + VAR_9); AV_WB32(&VAR_8[VAR_16], VAR_12); VAR_16 += 4; for(VAR_10 = VAR_9; VAR_10 < FFMIN(VAR_9+32, VAR_17); VAR_10++) { mb = &VAR_0->mb[VAR_10]; if(mb->best_encoding == ENC_V1)
VAR_8[VAR_16++] = mb->v1_vector; else
for(VAR_11 = 0; VAR_11 < 4; VAR_11++) VAR_8[VAR_16++] = mb->v4_vector[VAR_5][VAR_11]; } } write_chunk_header(VAR_8 + VAR_15, 0x30, VAR_16 - VAR_15 - CHUNK_HEADER_SIZE); break; case MODE_MC:
VAR_15 = VAR_16; VAR_16 += CHUNK_HEADER_SIZE; VAR_12 = VAR_13 = VAR_14 = 0; for(VAR_9 = 0; VAR_9 < VAR_17; VAR_9++) { mb = &VAR_0->mb[VAR_9]; VAR_12 |= (mb->best_encoding != ENC_SKIP) << (31 - VAR_13++); VAR_18 = 0; VAR_19 = 0; if(mb->best_encoding != ENC_SKIP) { if(VAR_13 < 32)
VAR_12 |= (mb->best_encoding == ENC_V4) << (31 - VAR_13++); else
VAR_18 = 1; } if(VAR_13 == 32) { AV_WB32(&VAR_8[VAR_16], VAR_12); VAR_16 += 4; VAR_12 = VAR_13 = 0; if(mb->best_encoding == ENC_SKIP || VAR_18) { memcpy(&VAR_8[VAR_16], VAR_20, VAR_14); VAR_16 += VAR_14; VAR_14 = 0; } else VAR_19 = 1; } if(VAR_18) { VAR_12 = (mb->best_encoding == ENC_V4) << 31; VAR_13 = 1; } if(mb->best_encoding == ENC_V1)
VAR_20[VAR_14++] = mb->v1_vector; else if(mb->best_encoding == ENC_V4)
for(VAR_11 = 0; VAR_11 < 4; VAR_11++) VAR_20[VAR_14++] = mb->v4_vector[VAR_5][VAR_11]; if(VAR_19) { memcpy(&VAR_8[VAR_16], VAR_20, VAR_14); VAR_16 += VAR_14; VAR_14 = 0; } } if(VAR_13 > 0) { AV_WB32(&VAR_8[VAR_16], VAR_12); VAR_16 += 4; memcpy(&VAR_8[VAR_16], VAR_20, VAR_14); VAR_16 += VAR_14; } write_chunk_header(VAR_8 + VAR_15, 0x31, VAR_16 - VAR_15 - CHUNK_HEADER_SIZE); break; } return VAR_16; }