static int decode_pic(AVSContext *h) {

    MpegEncContext *s = &h->s;

    int skip_count;

    enum cavs_mb mb_type;



    if (!s->context_initialized) {

        s->avctx->idct_algo = FF_IDCT_CAVS;

        if (MPV_common_init(s) < 0)

            return -1;

        ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);

    }

    skip_bits(&s->gb,16);//bbv_dwlay

    if(h->stc == PIC_PB_START_CODE) {

        h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;

        if(h->pic_type > FF_B_TYPE) {

            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");

            return -1;

        }

        /* make sure we have the reference frames we need */

        if(!h->DPB[0].data[0] ||

          (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))

            return -1;

    } else {

        h->pic_type = FF_I_TYPE;

        if(get_bits1(&s->gb))

            skip_bits(&s->gb,24);//time_code

    }

    /* release last B frame */

    if(h->picture.data[0])

        s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);



    s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);

    ff_cavs_init_pic(h);

    h->picture.poc = get_bits(&s->gb,8)*2;



    /* get temporal distances and MV scaling factors */

    if(h->pic_type != FF_B_TYPE) {

        h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;

    } else {

        h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;

    }

    h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;

    h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;

    h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;

    if(h->pic_type == FF_B_TYPE) {

        h->sym_factor = h->dist[0]*h->scale_den[1];

    } else {

        h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;

        h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;

    }



    if(s->low_delay)

        get_ue_golomb(&s->gb); //bbv_check_times

    h->progressive             = get_bits1(&s->gb);

    h->pic_structure = 1;

    if(!h->progressive)

        h->pic_structure = get_bits1(&s->gb);

    if(!h->pic_structure && h->stc == PIC_PB_START_CODE)

        skip_bits1(&s->gb);     //advanced_pred_mode_disable

    skip_bits1(&s->gb);        //top_field_first

    skip_bits1(&s->gb);        //repeat_first_field

    h->qp_fixed                = get_bits1(&s->gb);

    h->qp                      = get_bits(&s->gb,6);

    if(h->pic_type == FF_I_TYPE) {

        if(!h->progressive && !h->pic_structure)

            skip_bits1(&s->gb);//what is this?

        skip_bits(&s->gb,4);   //reserved bits

    } else {

        if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))

            h->ref_flag        = get_bits1(&s->gb);

        skip_bits(&s->gb,4);   //reserved bits

        h->skip_mode_flag      = get_bits1(&s->gb);

    }

    h->loop_filter_disable     = get_bits1(&s->gb);

    if(!h->loop_filter_disable && get_bits1(&s->gb)) {

        h->alpha_offset        = get_se_golomb(&s->gb);

        h->beta_offset         = get_se_golomb(&s->gb);

    } else {

        h->alpha_offset = h->beta_offset  = 0;

    }

    if(h->pic_type == FF_I_TYPE) {

        do {

            check_for_slice(h);

            decode_mb_i(h, 0);

        } while(ff_cavs_next_mb(h));

    } else if(h->pic_type == FF_P_TYPE) {

        do {

            check_for_slice(h);

            if(h->skip_mode_flag) {

                skip_count = get_ue_golomb(&s->gb);

                while(skip_count--) {

                    decode_mb_p(h,P_SKIP);

                    if(!ff_cavs_next_mb(h))

                        goto done;

                }

                check_for_slice(h);

                mb_type = get_ue_golomb(&s->gb) + P_16X16;

            } else

                mb_type = get_ue_golomb(&s->gb) + P_SKIP;

            if(mb_type > P_8X8) {

                decode_mb_i(h, mb_type - P_8X8 - 1);

            } else

                decode_mb_p(h,mb_type);

        } while(ff_cavs_next_mb(h));

    } else { /* FF_B_TYPE */

        do {

            check_for_slice(h);

            if(h->skip_mode_flag) {

                skip_count = get_ue_golomb(&s->gb);

                while(skip_count--) {

                    decode_mb_b(h,B_SKIP);

                    if(!ff_cavs_next_mb(h))

                        goto done;

                }

                check_for_slice(h);

                mb_type = get_ue_golomb(&s->gb) + B_DIRECT;

            } else

                mb_type = get_ue_golomb(&s->gb) + B_SKIP;

            if(mb_type > B_8X8) {

                decode_mb_i(h, mb_type - B_8X8 - 1);

            } else

                decode_mb_b(h,mb_type);

        } while(ff_cavs_next_mb(h));

    }

 done:

    if(h->pic_type != FF_B_TYPE) {

        if(h->DPB[1].data[0])

            s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);

        h->DPB[1] = h->DPB[0];

        h->DPB[0] = h->picture;

        memset(&h->picture,0,sizeof(Picture));

    }

    return 0;

}

static int decode_pic(AVSContext *h) {

    MpegEncContext *s = &h->s;

    int skip_count;

    enum cavs_mb mb_type;



    if (!s->context_initialized) {

        s->avctx->idct_algo = FF_IDCT_CAVS;

        if (MPV_common_init(s) < 0)

            return -1;

        ff_init_scantable(s->dsp.idct_permutation,&h->scantable,ff_zigzag_direct);

    }

    skip_bits(&s->gb,16);

    if(h->stc == PIC_PB_START_CODE) {

        h->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;

        if(h->pic_type > FF_B_TYPE) {

            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");

            return -1;

        }

        

        if(!h->DPB[0].data[0] ||

          (!h->DPB[1].data[0] && h->pic_type == FF_B_TYPE))

            return -1;

    } else {

        h->pic_type = FF_I_TYPE;

        if(get_bits1(&s->gb))

            skip_bits(&s->gb,24);

    }

    

    if(h->picture.data[0])

        s->avctx->release_buffer(s->avctx, (AVFrame *)&h->picture);



    s->avctx->get_buffer(s->avctx, (AVFrame *)&h->picture);

    ff_cavs_init_pic(h);

    h->picture.poc = get_bits(&s->gb,8)*2;



    

    if(h->pic_type != FF_B_TYPE) {

        h->dist[0] = (h->picture.poc - h->DPB[0].poc  + 512) % 512;

    } else {

        h->dist[0] = (h->DPB[0].poc  - h->picture.poc + 512) % 512;

    }

    h->dist[1] = (h->picture.poc - h->DPB[1].poc  + 512) % 512;

    h->scale_den[0] = h->dist[0] ? 512/h->dist[0] : 0;

    h->scale_den[1] = h->dist[1] ? 512/h->dist[1] : 0;

    if(h->pic_type == FF_B_TYPE) {

        h->sym_factor = h->dist[0]*h->scale_den[1];

    } else {

        h->direct_den[0] = h->dist[0] ? 16384/h->dist[0] : 0;

        h->direct_den[1] = h->dist[1] ? 16384/h->dist[1] : 0;

    }



    if(s->low_delay)

        get_ue_golomb(&s->gb); 

    h->progressive             = get_bits1(&s->gb);

    h->pic_structure = 1;

    if(!h->progressive)

        h->pic_structure = get_bits1(&s->gb);

    if(!h->pic_structure && h->stc == PIC_PB_START_CODE)

        skip_bits1(&s->gb);     

    skip_bits1(&s->gb);        

    skip_bits1(&s->gb);        

    h->qp_fixed                = get_bits1(&s->gb);

    h->qp                      = get_bits(&s->gb,6);

    if(h->pic_type == FF_I_TYPE) {

        if(!h->progressive && !h->pic_structure)

            skip_bits1(&s->gb);

        skip_bits(&s->gb,4);   

    } else {

        if(!(h->pic_type == FF_B_TYPE && h->pic_structure == 1))

            h->ref_flag        = get_bits1(&s->gb);

        skip_bits(&s->gb,4);   

        h->skip_mode_flag      = get_bits1(&s->gb);

    }

    h->loop_filter_disable     = get_bits1(&s->gb);

    if(!h->loop_filter_disable && get_bits1(&s->gb)) {

        h->alpha_offset        = get_se_golomb(&s->gb);

        h->beta_offset         = get_se_golomb(&s->gb);

    } else {

        h->alpha_offset = h->beta_offset  = 0;

    }

    if(h->pic_type == FF_I_TYPE) {

        do {

            check_for_slice(h);

            decode_mb_i(h, 0);

        } while(ff_cavs_next_mb(h));

    } else if(h->pic_type == FF_P_TYPE) {

        do {

            check_for_slice(h);

            if(h->skip_mode_flag) {

                skip_count = get_ue_golomb(&s->gb);

                while(skip_count--) {

                    decode_mb_p(h,P_SKIP);

                    if(!ff_cavs_next_mb(h))

                        goto done;

                }

                check_for_slice(h);

                mb_type = get_ue_golomb(&s->gb) + P_16X16;

            } else

                mb_type = get_ue_golomb(&s->gb) + P_SKIP;

            if(mb_type > P_8X8) {

                decode_mb_i(h, mb_type - P_8X8 - 1);

            } else

                decode_mb_p(h,mb_type);

        } while(ff_cavs_next_mb(h));

    } else { 

        do {

            check_for_slice(h);

            if(h->skip_mode_flag) {

                skip_count = get_ue_golomb(&s->gb);

                while(skip_count--) {

                    decode_mb_b(h,B_SKIP);

                    if(!ff_cavs_next_mb(h))

                        goto done;

                }

                check_for_slice(h);

                mb_type = get_ue_golomb(&s->gb) + B_DIRECT;

            } else

                mb_type = get_ue_golomb(&s->gb) + B_SKIP;

            if(mb_type > B_8X8) {

                decode_mb_i(h, mb_type - B_8X8 - 1);

            } else

                decode_mb_b(h,mb_type);

        } while(ff_cavs_next_mb(h));

    }

 done:

    if(h->pic_type != FF_B_TYPE) {

        if(h->DPB[1].data[0])

            s->avctx->release_buffer(s->avctx, (AVFrame *)&h->DPB[1]);

        h->DPB[1] = h->DPB[0];

        h->DPB[0] = h->picture;

        memset(&h->picture,0,sizeof(Picture));

    }

    return 0;

}

static int FUNC_0(AVSContext *VAR_0) {

    MpegEncContext *s = &VAR_0->s;

    int VAR_1;

    enum cavs_mb VAR_2;



    if (!s->context_initialized) {

        s->avctx->idct_algo = FF_IDCT_CAVS;

        if (MPV_common_init(s) < 0)

            return -1;

        ff_init_scantable(s->dsp.idct_permutation,&VAR_0->scantable,ff_zigzag_direct);

    }

    skip_bits(&s->gb,16);

    if(VAR_0->stc == PIC_PB_START_CODE) {

        VAR_0->pic_type = get_bits(&s->gb,2) + FF_I_TYPE;

        if(VAR_0->pic_type > FF_B_TYPE) {

            av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n");

            return -1;

        }

        

        if(!VAR_0->DPB[0].data[0] ||

          (!VAR_0->DPB[1].data[0] && VAR_0->pic_type == FF_B_TYPE))

            return -1;

    } else {

        VAR_0->pic_type = FF_I_TYPE;

        if(get_bits1(&s->gb))

            skip_bits(&s->gb,24);

    }

    

    if(VAR_0->picture.data[0])

        s->avctx->release_buffer(s->avctx, (AVFrame *)&VAR_0->picture);



    s->avctx->get_buffer(s->avctx, (AVFrame *)&VAR_0->picture);

    ff_cavs_init_pic(VAR_0);

    VAR_0->picture.poc = get_bits(&s->gb,8)*2;



    

    if(VAR_0->pic_type != FF_B_TYPE) {

        VAR_0->dist[0] = (VAR_0->picture.poc - VAR_0->DPB[0].poc  + 512) % 512;

    } else {

        VAR_0->dist[0] = (VAR_0->DPB[0].poc  - VAR_0->picture.poc + 512) % 512;

    }

    VAR_0->dist[1] = (VAR_0->picture.poc - VAR_0->DPB[1].poc  + 512) % 512;

    VAR_0->scale_den[0] = VAR_0->dist[0] ? 512/VAR_0->dist[0] : 0;

    VAR_0->scale_den[1] = VAR_0->dist[1] ? 512/VAR_0->dist[1] : 0;

    if(VAR_0->pic_type == FF_B_TYPE) {

        VAR_0->sym_factor = VAR_0->dist[0]*VAR_0->scale_den[1];

    } else {

        VAR_0->direct_den[0] = VAR_0->dist[0] ? 16384/VAR_0->dist[0] : 0;

        VAR_0->direct_den[1] = VAR_0->dist[1] ? 16384/VAR_0->dist[1] : 0;

    }



    if(s->low_delay)

        get_ue_golomb(&s->gb); 

    VAR_0->progressive             = get_bits1(&s->gb);

    VAR_0->pic_structure = 1;

    if(!VAR_0->progressive)

        VAR_0->pic_structure = get_bits1(&s->gb);

    if(!VAR_0->pic_structure && VAR_0->stc == PIC_PB_START_CODE)

        skip_bits1(&s->gb);     

    skip_bits1(&s->gb);        

    skip_bits1(&s->gb);        

    VAR_0->qp_fixed                = get_bits1(&s->gb);

    VAR_0->qp                      = get_bits(&s->gb,6);

    if(VAR_0->pic_type == FF_I_TYPE) {

        if(!VAR_0->progressive && !VAR_0->pic_structure)

            skip_bits1(&s->gb);

        skip_bits(&s->gb,4);   

    } else {

        if(!(VAR_0->pic_type == FF_B_TYPE && VAR_0->pic_structure == 1))

            VAR_0->ref_flag        = get_bits1(&s->gb);

        skip_bits(&s->gb,4);   

        VAR_0->skip_mode_flag      = get_bits1(&s->gb);

    }

    VAR_0->loop_filter_disable     = get_bits1(&s->gb);

    if(!VAR_0->loop_filter_disable && get_bits1(&s->gb)) {

        VAR_0->alpha_offset        = get_se_golomb(&s->gb);

        VAR_0->beta_offset         = get_se_golomb(&s->gb);

    } else {

        VAR_0->alpha_offset = VAR_0->beta_offset  = 0;

    }

    if(VAR_0->pic_type == FF_I_TYPE) {

        do {

            check_for_slice(VAR_0);

            decode_mb_i(VAR_0, 0);

        } while(ff_cavs_next_mb(VAR_0));

    } else if(VAR_0->pic_type == FF_P_TYPE) {

        do {

            check_for_slice(VAR_0);

            if(VAR_0->skip_mode_flag) {

                VAR_1 = get_ue_golomb(&s->gb);

                while(VAR_1--) {

                    decode_mb_p(VAR_0,P_SKIP);

                    if(!ff_cavs_next_mb(VAR_0))

                        goto done;

                }

                check_for_slice(VAR_0);

                VAR_2 = get_ue_golomb(&s->gb) + P_16X16;

            } else

                VAR_2 = get_ue_golomb(&s->gb) + P_SKIP;

            if(VAR_2 > P_8X8) {

                decode_mb_i(VAR_0, VAR_2 - P_8X8 - 1);

            } else

                decode_mb_p(VAR_0,VAR_2);

        } while(ff_cavs_next_mb(VAR_0));

    } else { 

        do {

            check_for_slice(VAR_0);

            if(VAR_0->skip_mode_flag) {

                VAR_1 = get_ue_golomb(&s->gb);

                while(VAR_1--) {

                    decode_mb_b(VAR_0,B_SKIP);

                    if(!ff_cavs_next_mb(VAR_0))

                        goto done;

                }

                check_for_slice(VAR_0);

                VAR_2 = get_ue_golomb(&s->gb) + B_DIRECT;

            } else

                VAR_2 = get_ue_golomb(&s->gb) + B_SKIP;

            if(VAR_2 > B_8X8) {

                decode_mb_i(VAR_0, VAR_2 - B_8X8 - 1);

            } else

                decode_mb_b(VAR_0,VAR_2);

        } while(ff_cavs_next_mb(VAR_0));

    }

 done:

    if(VAR_0->pic_type != FF_B_TYPE) {

        if(VAR_0->DPB[1].data[0])

            s->avctx->release_buffer(s->avctx, (AVFrame *)&VAR_0->DPB[1]);

        VAR_0->DPB[1] = VAR_0->DPB[0];

        VAR_0->DPB[0] = VAR_0->picture;

        memset(&VAR_0->picture,0,sizeof(Picture));

    }

    return 0;

}

static int FUNC_0(AVSContext *VAR_0) { MpegEncContext *s = &VAR_0->s; int VAR_1; enum cavs_mb VAR_2; if (!s->context_initialized) { s->avctx->idct_algo = FF_IDCT_CAVS; if (MPV_common_init(s) < 0)
return -1; ff_init_scantable(s->dsp.idct_permutation,&VAR_0->scantable,ff_zigzag_direct); } skip_bits(&s->gb,16); if(VAR_0->stc == PIC_PB_START_CODE) { VAR_0->pic_type = get_bits(&s->gb,2) + FF_I_TYPE; if(VAR_0->pic_type > FF_B_TYPE) { av_log(s->avctx, AV_LOG_ERROR, "illegal picture type\n"); return -1; } if(!VAR_0->DPB[0].data[0] ||
(!VAR_0->DPB[1].data[0] && VAR_0->pic_type == FF_B_TYPE))
return -1; } else { VAR_0->pic_type = FF_I_TYPE; if(get_bits1(&s->gb))
skip_bits(&s->gb,24); } if(VAR_0->picture.data[0])
s->avctx->release_buffer(s->avctx, (AVFrame *)&VAR_0->picture); s->avctx->get_buffer(s->avctx, (AVFrame *)&VAR_0->picture); ff_cavs_init_pic(VAR_0); VAR_0->picture.poc = get_bits(&s->gb,8)*2; if(VAR_0->pic_type != FF_B_TYPE) { VAR_0->dist[0] = (VAR_0->picture.poc - VAR_0->DPB[0].poc  + 512) % 512; } else { VAR_0->dist[0] = (VAR_0->DPB[0].poc  - VAR_0->picture.poc + 512) % 512; } VAR_0->dist[1] = (VAR_0->picture.poc - VAR_0->DPB[1].poc  + 512) % 512; VAR_0->scale_den[0] = VAR_0->dist[0] ? 512/VAR_0->dist[0] : 0; VAR_0->scale_den[1] = VAR_0->dist[1] ? 512/VAR_0->dist[1] : 0; if(VAR_0->pic_type == FF_B_TYPE) { VAR_0->sym_factor = VAR_0->dist[0]*VAR_0->scale_den[1]; } else { VAR_0->direct_den[0] = VAR_0->dist[0] ? 16384/VAR_0->dist[0] : 0; VAR_0->direct_den[1] = VAR_0->dist[1] ? 16384/VAR_0->dist[1] : 0; } if(s->low_delay)
get_ue_golomb(&s->gb); VAR_0->progressive             = get_bits1(&s->gb); VAR_0->pic_structure = 1; if(!VAR_0->progressive)
VAR_0->pic_structure = get_bits1(&s->gb); if(!VAR_0->pic_structure && VAR_0->stc == PIC_PB_START_CODE)
skip_bits1(&s->gb); skip_bits1(&s->gb); skip_bits1(&s->gb); VAR_0->qp_fixed                = get_bits1(&s->gb); VAR_0->qp                      = get_bits(&s->gb,6); if(VAR_0->pic_type == FF_I_TYPE) { if(!VAR_0->progressive && !VAR_0->pic_structure)
skip_bits1(&s->gb); skip_bits(&s->gb,4); } else { if(!(VAR_0->pic_type == FF_B_TYPE && VAR_0->pic_structure == 1))
VAR_0->ref_flag        = get_bits1(&s->gb); skip_bits(&s->gb,4); VAR_0->skip_mode_flag      = get_bits1(&s->gb); } VAR_0->loop_filter_disable     = get_bits1(&s->gb); if(!VAR_0->loop_filter_disable && get_bits1(&s->gb)) { VAR_0->alpha_offset        = get_se_golomb(&s->gb); VAR_0->beta_offset         = get_se_golomb(&s->gb); } else { VAR_0->alpha_offset = VAR_0->beta_offset  = 0; } if(VAR_0->pic_type == FF_I_TYPE) { do { check_for_slice(VAR_0); decode_mb_i(VAR_0, 0); } while(ff_cavs_next_mb(VAR_0)); } else if(VAR_0->pic_type == FF_P_TYPE) { do { check_for_slice(VAR_0); if(VAR_0->skip_mode_flag) { VAR_1 = get_ue_golomb(&s->gb); while(VAR_1--) { decode_mb_p(VAR_0,P_SKIP); if(!ff_cavs_next_mb(VAR_0))
goto done; } check_for_slice(VAR_0); VAR_2 = get_ue_golomb(&s->gb) + P_16X16; } else VAR_2 = get_ue_golomb(&s->gb) + P_SKIP; if(VAR_2 > P_8X8) { decode_mb_i(VAR_0, VAR_2 - P_8X8 - 1); } else decode_mb_p(VAR_0,VAR_2); } while(ff_cavs_next_mb(VAR_0)); } else { do { check_for_slice(VAR_0); if(VAR_0->skip_mode_flag) { VAR_1 = get_ue_golomb(&s->gb); while(VAR_1--) { decode_mb_b(VAR_0,B_SKIP); if(!ff_cavs_next_mb(VAR_0))
goto done; } check_for_slice(VAR_0); VAR_2 = get_ue_golomb(&s->gb) + B_DIRECT; } else VAR_2 = get_ue_golomb(&s->gb) + B_SKIP; if(VAR_2 > B_8X8) { decode_mb_i(VAR_0, VAR_2 - B_8X8 - 1); } else decode_mb_b(VAR_0,VAR_2); } while(ff_cavs_next_mb(VAR_0)); } done:
if(VAR_0->pic_type != FF_B_TYPE) { if(VAR_0->DPB[1].data[0])
s->avctx->release_buffer(s->avctx, (AVFrame *)&VAR_0->DPB[1]); VAR_0->DPB[1] = VAR_0->DPB[0]; VAR_0->DPB[0] = VAR_0->picture; memset(&VAR_0->picture,0,sizeof(Picture)); } return 0; }