static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)

{

    int superblock_starts[3] = {

        0, s->u_superblock_start, s->v_superblock_start

    };

    int bit = 0;

    int current_superblock = 0;

    int current_run = 0;

    int num_partial_superblocks = 0;



    int i, j;

    int current_fragment;

    int plane;



    if (s->keyframe) {

        memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);

    } else {

        /* unpack the list of partially-coded superblocks */

        bit         = get_bits1(gb) ^ 1;

        current_run = 0;



        while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {

            if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)

                bit = get_bits1(gb);

            else

                bit ^= 1;



            current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,

                                   6, 2) + 1;

            if (current_run == 34)

                current_run += get_bits(gb, 12);



            if (current_superblock + current_run > s->superblock_count) {

                av_log(s->avctx, AV_LOG_ERROR,

                       "Invalid partially coded superblock run length\n");

                return -1;

            }



            memset(s->superblock_coding + current_superblock, bit, current_run);



            current_superblock += current_run;

            if (bit)

                num_partial_superblocks += current_run;

        }



        /* unpack the list of fully coded superblocks if any of the blocks were

         * not marked as partially coded in the previous step */

        if (num_partial_superblocks < s->superblock_count) {

            int superblocks_decoded = 0;



            current_superblock = 0;

            bit                = get_bits1(gb) ^ 1;

            current_run        = 0;



            while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&

                   get_bits_left(gb) > 0) {

                if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)

                    bit = get_bits1(gb);

                else

                    bit ^= 1;



                current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,

                                       6, 2) + 1;

                if (current_run == 34)

                    current_run += get_bits(gb, 12);



                for (j = 0; j < current_run; current_superblock++) {

                    if (current_superblock >= s->superblock_count) {

                        av_log(s->avctx, AV_LOG_ERROR,

                               "Invalid fully coded superblock run length\n");

                        return -1;

                    }



                    /* skip any superblocks already marked as partially coded */

                    if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {

                        s->superblock_coding[current_superblock] = 2 * bit;

                        j++;

                    }

                }

                superblocks_decoded += current_run;

            }

        }



        /* if there were partial blocks, initialize bitstream for

         * unpacking fragment codings */

        if (num_partial_superblocks) {

            current_run = 0;

            bit         = get_bits1(gb);

            /* toggle the bit because as soon as the first run length is

             * fetched the bit will be toggled again */

            bit ^= 1;

        }

    }



    /* figure out which fragments are coded; iterate through each

     * superblock (all planes) */

    s->total_num_coded_frags = 0;

    memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);



    for (plane = 0; plane < 3; plane++) {

        int sb_start = superblock_starts[plane];

        int sb_end   = sb_start + (plane ? s->c_superblock_count

                                         : s->y_superblock_count);

        int num_coded_frags = 0;



        for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {

            /* iterate through all 16 fragments in a superblock */

            for (j = 0; j < 16; j++) {

                /* if the fragment is in bounds, check its coding status */

                current_fragment = s->superblock_fragments[i * 16 + j];

                if (current_fragment != -1) {

                    int coded = s->superblock_coding[i];



                    if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {

                        /* fragment may or may not be coded; this is the case

                         * that cares about the fragment coding runs */

                        if (current_run-- == 0) {

                            bit        ^= 1;

                            current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);

                        }

                        coded = bit;

                    }



                    if (coded) {

                        /* default mode; actual mode will be decoded in

                         * the next phase */

                        s->all_fragments[current_fragment].coding_method =

                            MODE_INTER_NO_MV;

                        s->coded_fragment_list[plane][num_coded_frags++] =

                            current_fragment;

                    } else {

                        /* not coded; copy this fragment from the prior frame */

                        s->all_fragments[current_fragment].coding_method =

                            MODE_COPY;

                    }

                }

            }

        }

        s->total_num_coded_frags += num_coded_frags;

        for (i = 0; i < 64; i++)

            s->num_coded_frags[plane][i] = num_coded_frags;

        if (plane < 2)

            s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +

                                                num_coded_frags;

    }

    return 0;

}

static int unpack_superblocks(Vp3DecodeContext *s, GetBitContext *gb)

{

    int superblock_starts[3] = {

        0, s->u_superblock_start, s->v_superblock_start

    };

    int bit = 0;

    int current_superblock = 0;

    int current_run = 0;

    int num_partial_superblocks = 0;



    int i, j;

    int current_fragment;

    int plane;



    if (s->keyframe) {

        memset(s->superblock_coding, SB_FULLY_CODED, s->superblock_count);

    } else {

        

        bit         = get_bits1(gb) ^ 1;

        current_run = 0;



        while (current_superblock < s->superblock_count && get_bits_left(gb) > 0) {

            if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)

                bit = get_bits1(gb);

            else

                bit ^= 1;



            current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,

                                   6, 2) + 1;

            if (current_run == 34)

                current_run += get_bits(gb, 12);



            if (current_superblock + current_run > s->superblock_count) {

                av_log(s->avctx, AV_LOG_ERROR,

                       "Invalid partially coded superblock run length\n");

                return -1;

            }



            memset(s->superblock_coding + current_superblock, bit, current_run);



            current_superblock += current_run;

            if (bit)

                num_partial_superblocks += current_run;

        }



        



        if (num_partial_superblocks < s->superblock_count) {

            int superblocks_decoded = 0;



            current_superblock = 0;

            bit                = get_bits1(gb) ^ 1;

            current_run        = 0;



            while (superblocks_decoded < s->superblock_count - num_partial_superblocks &&

                   get_bits_left(gb) > 0) {

                if (s->theora && current_run == MAXIMUM_LONG_BIT_RUN)

                    bit = get_bits1(gb);

                else

                    bit ^= 1;



                current_run = get_vlc2(gb, s->superblock_run_length_vlc.table,

                                       6, 2) + 1;

                if (current_run == 34)

                    current_run += get_bits(gb, 12);



                for (j = 0; j < current_run; current_superblock++) {

                    if (current_superblock >= s->superblock_count) {

                        av_log(s->avctx, AV_LOG_ERROR,

                               "Invalid fully coded superblock run length\n");

                        return -1;

                    }



                    

                    if (s->superblock_coding[current_superblock] == SB_NOT_CODED) {

                        s->superblock_coding[current_superblock] = 2 * bit;

                        j++;

                    }

                }

                superblocks_decoded += current_run;

            }

        }



        



        if (num_partial_superblocks) {

            current_run = 0;

            bit         = get_bits1(gb);

            



            bit ^= 1;

        }

    }



    



    s->total_num_coded_frags = 0;

    memset(s->macroblock_coding, MODE_COPY, s->macroblock_count);



    for (plane = 0; plane < 3; plane++) {

        int sb_start = superblock_starts[plane];

        int sb_end   = sb_start + (plane ? s->c_superblock_count

                                         : s->y_superblock_count);

        int num_coded_frags = 0;



        for (i = sb_start; i < sb_end && get_bits_left(gb) > 0; i++) {

            

            for (j = 0; j < 16; j++) {

                

                current_fragment = s->superblock_fragments[i * 16 + j];

                if (current_fragment != -1) {

                    int coded = s->superblock_coding[i];



                    if (s->superblock_coding[i] == SB_PARTIALLY_CODED) {

                        



                        if (current_run-- == 0) {

                            bit        ^= 1;

                            current_run = get_vlc2(gb, s->fragment_run_length_vlc.table, 5, 2);

                        }

                        coded = bit;

                    }



                    if (coded) {

                        



                        s->all_fragments[current_fragment].coding_method =

                            MODE_INTER_NO_MV;

                        s->coded_fragment_list[plane][num_coded_frags++] =

                            current_fragment;

                    } else {

                        

                        s->all_fragments[current_fragment].coding_method =

                            MODE_COPY;

                    }

                }

            }

        }

        s->total_num_coded_frags += num_coded_frags;

        for (i = 0; i < 64; i++)

            s->num_coded_frags[plane][i] = num_coded_frags;

        if (plane < 2)

            s->coded_fragment_list[plane + 1] = s->coded_fragment_list[plane] +

                                                num_coded_frags;

    }

    return 0;

}

static int FUNC_0(Vp3DecodeContext *VAR_0, GetBitContext *VAR_1)

{

    int VAR_2[3] = {

        0, VAR_0->u_superblock_start, VAR_0->v_superblock_start

    };

    int VAR_3 = 0;

    int VAR_4 = 0;

    int VAR_5 = 0;

    int VAR_6 = 0;



    int VAR_7, VAR_8;

    int VAR_9;

    int VAR_10;



    if (VAR_0->keyframe) {

        memset(VAR_0->superblock_coding, SB_FULLY_CODED, VAR_0->superblock_count);

    } else {

        

        VAR_3         = get_bits1(VAR_1) ^ 1;

        VAR_5 = 0;



        while (VAR_4 < VAR_0->superblock_count && get_bits_left(VAR_1) > 0) {

            if (VAR_0->theora && VAR_5 == MAXIMUM_LONG_BIT_RUN)

                VAR_3 = get_bits1(VAR_1);

            else

                VAR_3 ^= 1;



            VAR_5 = get_vlc2(VAR_1, VAR_0->superblock_run_length_vlc.table,

                                   6, 2) + 1;

            if (VAR_5 == 34)

                VAR_5 += get_bits(VAR_1, 12);



            if (VAR_4 + VAR_5 > VAR_0->superblock_count) {

                av_log(VAR_0->avctx, AV_LOG_ERROR,

                       "Invalid partially coded superblock run length\n");

                return -1;

            }



            memset(VAR_0->superblock_coding + VAR_4, VAR_3, VAR_5);



            VAR_4 += VAR_5;

            if (VAR_3)

                VAR_6 += VAR_5;

        }



        



        if (VAR_6 < VAR_0->superblock_count) {

            int VAR_11 = 0;



            VAR_4 = 0;

            VAR_3                = get_bits1(VAR_1) ^ 1;

            VAR_5        = 0;



            while (VAR_11 < VAR_0->superblock_count - VAR_6 &&

                   get_bits_left(VAR_1) > 0) {

                if (VAR_0->theora && VAR_5 == MAXIMUM_LONG_BIT_RUN)

                    VAR_3 = get_bits1(VAR_1);

                else

                    VAR_3 ^= 1;



                VAR_5 = get_vlc2(VAR_1, VAR_0->superblock_run_length_vlc.table,

                                       6, 2) + 1;

                if (VAR_5 == 34)

                    VAR_5 += get_bits(VAR_1, 12);



                for (VAR_8 = 0; VAR_8 < VAR_5; VAR_4++) {

                    if (VAR_4 >= VAR_0->superblock_count) {

                        av_log(VAR_0->avctx, AV_LOG_ERROR,

                               "Invalid fully coded superblock run length\n");

                        return -1;

                    }



                    

                    if (VAR_0->superblock_coding[VAR_4] == SB_NOT_CODED) {

                        VAR_0->superblock_coding[VAR_4] = 2 * VAR_3;

                        VAR_8++;

                    }

                }

                VAR_11 += VAR_5;

            }

        }



        



        if (VAR_6) {

            VAR_5 = 0;

            VAR_3         = get_bits1(VAR_1);

            



            VAR_3 ^= 1;

        }

    }



    



    VAR_0->total_num_coded_frags = 0;

    memset(VAR_0->macroblock_coding, MODE_COPY, VAR_0->macroblock_count);



    for (VAR_10 = 0; VAR_10 < 3; VAR_10++) {

        int VAR_12 = VAR_2[VAR_10];

        int VAR_13   = VAR_12 + (VAR_10 ? VAR_0->c_superblock_count

                                         : VAR_0->y_superblock_count);

        int VAR_14 = 0;



        for (VAR_7 = VAR_12; VAR_7 < VAR_13 && get_bits_left(VAR_1) > 0; VAR_7++) {

            

            for (VAR_8 = 0; VAR_8 < 16; VAR_8++) {

                

                VAR_9 = VAR_0->superblock_fragments[VAR_7 * 16 + VAR_8];

                if (VAR_9 != -1) {

                    int coded = VAR_0->superblock_coding[VAR_7];



                    if (VAR_0->superblock_coding[VAR_7] == SB_PARTIALLY_CODED) {

                        



                        if (VAR_5-- == 0) {

                            VAR_3        ^= 1;

                            VAR_5 = get_vlc2(VAR_1, VAR_0->fragment_run_length_vlc.table, 5, 2);

                        }

                        coded = VAR_3;

                    }



                    if (coded) {

                        



                        VAR_0->all_fragments[VAR_9].coding_method =

                            MODE_INTER_NO_MV;

                        VAR_0->coded_fragment_list[VAR_10][VAR_14++] =

                            VAR_9;

                    } else {

                        

                        VAR_0->all_fragments[VAR_9].coding_method =

                            MODE_COPY;

                    }

                }

            }

        }

        VAR_0->total_num_coded_frags += VAR_14;

        for (VAR_7 = 0; VAR_7 < 64; VAR_7++)

            VAR_0->VAR_14[VAR_10][VAR_7] = VAR_14;

        if (VAR_10 < 2)

            VAR_0->coded_fragment_list[VAR_10 + 1] = VAR_0->coded_fragment_list[VAR_10] +

                                                VAR_14;

    }

    return 0;

}

static int FUNC_0(Vp3DecodeContext *VAR_0, GetBitContext *VAR_1)
{ int VAR_2[3] = { 0, VAR_0->u_superblock_start, VAR_0->v_superblock_start
}; int VAR_3 = 0; int VAR_4 = 0; int VAR_5 = 0; int VAR_6 = 0; int VAR_7, VAR_8; int VAR_9; int VAR_10; if (VAR_0->keyframe) { memset(VAR_0->superblock_coding, SB_FULLY_CODED, VAR_0->superblock_count); } else { VAR_3         = get_bits1(VAR_1) ^ 1; VAR_5 = 0; while (VAR_4 < VAR_0->superblock_count && get_bits_left(VAR_1) > 0) { if (VAR_0->theora && VAR_5 == MAXIMUM_LONG_BIT_RUN)
VAR_3 = get_bits1(VAR_1); else
VAR_3 ^= 1; VAR_5 = get_vlc2(VAR_1, VAR_0->superblock_run_length_vlc.table,
6, 2) + 1; if (VAR_5 == 34)
VAR_5 += get_bits(VAR_1, 12); if (VAR_4 + VAR_5 > VAR_0->superblock_count) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Invalid partially coded superblock run length\n"); return -1; } memset(VAR_0->superblock_coding + VAR_4, VAR_3, VAR_5); VAR_4 += VAR_5; if (VAR_3)
VAR_6 += VAR_5; } if (VAR_6 < VAR_0->superblock_count) { int VAR_11 = 0; VAR_4 = 0; VAR_3                = get_bits1(VAR_1) ^ 1; VAR_5        = 0; while (VAR_11 < VAR_0->superblock_count - VAR_6 &&
get_bits_left(VAR_1) > 0) { if (VAR_0->theora && VAR_5 == MAXIMUM_LONG_BIT_RUN)
VAR_3 = get_bits1(VAR_1); else
VAR_3 ^= 1; VAR_5 = get_vlc2(VAR_1, VAR_0->superblock_run_length_vlc.table,
6, 2) + 1; if (VAR_5 == 34)
VAR_5 += get_bits(VAR_1, 12); for (VAR_8 = 0; VAR_8 < VAR_5; VAR_4++) { if (VAR_4 >= VAR_0->superblock_count) { av_log(VAR_0->avctx, AV_LOG_ERROR,
"Invalid fully coded superblock run length\n"); return -1; } if (VAR_0->superblock_coding[VAR_4] == SB_NOT_CODED) { VAR_0->superblock_coding[VAR_4] = 2 * VAR_3; VAR_8++; } } VAR_11 += VAR_5; } } if (VAR_6) { VAR_5 = 0; VAR_3         = get_bits1(VAR_1); VAR_3 ^= 1; } } VAR_0->total_num_coded_frags = 0; memset(VAR_0->macroblock_coding, MODE_COPY, VAR_0->macroblock_count); for (VAR_10 = 0; VAR_10 < 3; VAR_10++) { int VAR_12 = VAR_2[VAR_10]; int VAR_13   = VAR_12 + (VAR_10 ? VAR_0->c_superblock_count
: VAR_0->y_superblock_count); int VAR_14 = 0; for (VAR_7 = VAR_12; VAR_7 < VAR_13 && get_bits_left(VAR_1) > 0; VAR_7++) { for (VAR_8 = 0; VAR_8 < 16; VAR_8++) { VAR_9 = VAR_0->superblock_fragments[VAR_7 * 16 + VAR_8]; if (VAR_9 != -1) { int coded = VAR_0->superblock_coding[VAR_7]; if (VAR_0->superblock_coding[VAR_7] == SB_PARTIALLY_CODED) { if (VAR_5-- == 0) { VAR_3        ^= 1; VAR_5 = get_vlc2(VAR_1, VAR_0->fragment_run_length_vlc.table, 5, 2); } coded = VAR_3; } if (coded) { VAR_0->all_fragments[VAR_9].coding_method =
MODE_INTER_NO_MV; VAR_0->coded_fragment_list[VAR_10][VAR_14++] =
VAR_9; } else { VAR_0->all_fragments[VAR_9].coding_method =
MODE_COPY; } } } } VAR_0->total_num_coded_frags += VAR_14; for (VAR_7 = 0; VAR_7 < 64; VAR_7++) VAR_0->VAR_14[VAR_10][VAR_7] = VAR_14; if (VAR_10 < 2)
VAR_0->coded_fragment_list[VAR_10 + 1] = VAR_0->coded_fragment_list[VAR_10] +
VAR_14; } return 0; }