static int decode_ref_pic_list_reordering(H264Context *h){

    MpegEncContext * const s = &h->s;

    int list, index;



    print_short_term(h);

    print_long_term(h);

    if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; //FIXME move before func



    for(list=0; list<2; list++){

        memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);



        if(get_bits1(&s->gb)){

            int pred= h->curr_pic_num;



            for(index=0; ; index++){

                int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);

                int pic_id;

                int i;

                Picture *ref = NULL;



                if(reordering_of_pic_nums_idc==3)

                    break;



                if(index >= h->ref_count[list]){

                    av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");

                    return -1;

                }



                if(reordering_of_pic_nums_idc<3){

                    if(reordering_of_pic_nums_idc<2){

                        const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;



                        if(abs_diff_pic_num >= h->max_pic_num){

                            av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");

                            return -1;

                        }



                        if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;

                        else                                pred+= abs_diff_pic_num;

                        pred &= h->max_pic_num - 1;



                        for(i= h->short_ref_count-1; i>=0; i--){

                            ref = h->short_ref[i];

                            assert(ref->reference == 3);

                            assert(!ref->long_ref);

                            if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) // ignore non existing pictures by testing data[0] pointer

                                break;

                        }

                        if(i>=0)

                            ref->pic_id= ref->frame_num;

                    }else{

                        pic_id= get_ue_golomb(&s->gb); //long_term_pic_idx

                        ref = h->long_ref[pic_id];

                        if(ref){

                            ref->pic_id= pic_id;

                            assert(ref->reference == 3);

                            assert(ref->long_ref);

                            i=0;

                        }else{

                            i=-1;

                        }

                    }



                    if (i < 0) {

                        av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");

                        memset(&h->ref_list[list][index], 0, sizeof(Picture)); //FIXME

                    } else {

                        for(i=index; i+1<h->ref_count[list]; i++){

                            if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)

                                break;

                        }

                        for(; i > index; i--){

                            h->ref_list[list][i]= h->ref_list[list][i-1];

                        }

                        h->ref_list[list][index]= *ref;

                    }

                }else{

                    av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");

                    return -1;

                }

            }

        }



        if(h->slice_type!=B_TYPE) break;

    }

    for(list=0; list<2; list++){

        for(index= 0; index < h->ref_count[list]; index++){

            if(!h->ref_list[list][index].data[0])

                h->ref_list[list][index]= s->current_picture;

        }

        if(h->slice_type!=B_TYPE) break;

    }



    if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)

        direct_dist_scale_factor(h);

    direct_ref_list_init(h);

    return 0;

}

static int decode_ref_pic_list_reordering(H264Context *h){

    MpegEncContext * const s = &h->s;

    int list, index;



    print_short_term(h);

    print_long_term(h);

    if(h->slice_type==I_TYPE || h->slice_type==SI_TYPE) return 0; 



    for(list=0; list<2; list++){

        memcpy(h->ref_list[list], h->default_ref_list[list], sizeof(Picture)*h->ref_count[list]);



        if(get_bits1(&s->gb)){

            int pred= h->curr_pic_num;



            for(index=0; ; index++){

                int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);

                int pic_id;

                int i;

                Picture *ref = NULL;



                if(reordering_of_pic_nums_idc==3)

                    break;



                if(index >= h->ref_count[list]){

                    av_log(h->s.avctx, AV_LOG_ERROR, "reference count overflow\n");

                    return -1;

                }



                if(reordering_of_pic_nums_idc<3){

                    if(reordering_of_pic_nums_idc<2){

                        const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;



                        if(abs_diff_pic_num >= h->max_pic_num){

                            av_log(h->s.avctx, AV_LOG_ERROR, "abs_diff_pic_num overflow\n");

                            return -1;

                        }



                        if(reordering_of_pic_nums_idc == 0) pred-= abs_diff_pic_num;

                        else                                pred+= abs_diff_pic_num;

                        pred &= h->max_pic_num - 1;



                        for(i= h->short_ref_count-1; i>=0; i--){

                            ref = h->short_ref[i];

                            assert(ref->reference == 3);

                            assert(!ref->long_ref);

                            if(ref->data[0] != NULL && ref->frame_num == pred && ref->long_ref == 0) 

                                break;

                        }

                        if(i>=0)

                            ref->pic_id= ref->frame_num;

                    }else{

                        pic_id= get_ue_golomb(&s->gb); 

                        ref = h->long_ref[pic_id];

                        if(ref){

                            ref->pic_id= pic_id;

                            assert(ref->reference == 3);

                            assert(ref->long_ref);

                            i=0;

                        }else{

                            i=-1;

                        }

                    }



                    if (i < 0) {

                        av_log(h->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");

                        memset(&h->ref_list[list][index], 0, sizeof(Picture)); 

                    } else {

                        for(i=index; i+1<h->ref_count[list]; i++){

                            if(ref->long_ref == h->ref_list[list][i].long_ref && ref->pic_id == h->ref_list[list][i].pic_id)

                                break;

                        }

                        for(; i > index; i--){

                            h->ref_list[list][i]= h->ref_list[list][i-1];

                        }

                        h->ref_list[list][index]= *ref;

                    }

                }else{

                    av_log(h->s.avctx, AV_LOG_ERROR, "illegal reordering_of_pic_nums_idc\n");

                    return -1;

                }

            }

        }



        if(h->slice_type!=B_TYPE) break;

    }

    for(list=0; list<2; list++){

        for(index= 0; index < h->ref_count[list]; index++){

            if(!h->ref_list[list][index].data[0])

                h->ref_list[list][index]= s->current_picture;

        }

        if(h->slice_type!=B_TYPE) break;

    }



    if(h->slice_type==B_TYPE && !h->direct_spatial_mv_pred)

        direct_dist_scale_factor(h);

    direct_ref_list_init(h);

    return 0;

}

                int reordering_of_pic_nums_idc= get_ue_golomb(&s->gb);                 int pic_id;                         const int abs_diff_pic_num= get_ue_golomb(&s->gb) + 1;
static int FUNC_0(H264Context *VAR_0){

    MpegEncContext * const s = &VAR_0->s;

    int VAR_1, VAR_2;



    print_short_term(VAR_0);

    print_long_term(VAR_0);

    if(VAR_0->slice_type==I_TYPE || VAR_0->slice_type==SI_TYPE) return 0; 



    for(VAR_1=0; VAR_1<2; VAR_1++){

        memcpy(VAR_0->ref_list[VAR_1], VAR_0->default_ref_list[VAR_1], sizeof(Picture)*VAR_0->ref_count[VAR_1]);



        if(get_bits1(&s->gb)){

            int VAR_3= VAR_0->curr_pic_num;



            for(VAR_2=0; ; VAR_2++){

                int VAR_4= get_ue_golomb(&s->gb);

                int VAR_5;

                int VAR_6;

                Picture *ref = NULL;



                if(VAR_4==3)

                    break;



                if(VAR_2 >= VAR_0->ref_count[VAR_1]){

                    av_log(VAR_0->s.avctx, AV_LOG_ERROR, "reference count overflow\n");

                    return -1;

                }



                if(VAR_4<3){

                    if(VAR_4<2){

                        const int VAR_7= get_ue_golomb(&s->gb) + 1;



                        if(VAR_7 >= VAR_0->max_pic_num){

                            av_log(VAR_0->s.avctx, AV_LOG_ERROR, "VAR_7 overflow\n");

                            return -1;

                        }



                        if(VAR_4 == 0) VAR_3-= VAR_7;

                        else                                VAR_3+= VAR_7;

                        VAR_3 &= VAR_0->max_pic_num - 1;



                        for(VAR_6= VAR_0->short_ref_count-1; VAR_6>=0; VAR_6--){

                            ref = VAR_0->short_ref[VAR_6];

                            assert(ref->reference == 3);

                            assert(!ref->long_ref);

                            if(ref->data[0] != NULL && ref->frame_num == VAR_3 && ref->long_ref == 0) 

                                break;

                        }

                        if(VAR_6>=0)

                            ref->VAR_5= ref->frame_num;

                    }else{

                        VAR_5= get_ue_golomb(&s->gb); 

                        ref = VAR_0->long_ref[VAR_5];

                        if(ref){

                            ref->VAR_5= VAR_5;

                            assert(ref->reference == 3);

                            assert(ref->long_ref);

                            VAR_6=0;

                        }else{

                            VAR_6=-1;

                        }

                    }



                    if (VAR_6 < 0) {

                        av_log(VAR_0->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n");

                        memset(&VAR_0->ref_list[VAR_1][VAR_2], 0, sizeof(Picture)); 

                    } else {

                        for(VAR_6=VAR_2; VAR_6+1<VAR_0->ref_count[VAR_1]; VAR_6++){

                            if(ref->long_ref == VAR_0->ref_list[VAR_1][VAR_6].long_ref && ref->VAR_5 == VAR_0->ref_list[VAR_1][VAR_6].VAR_5)

                                break;

                        }

                        for(; VAR_6 > VAR_2; VAR_6--){

                            VAR_0->ref_list[VAR_1][VAR_6]= VAR_0->ref_list[VAR_1][VAR_6-1];

                        }

                        VAR_0->ref_list[VAR_1][VAR_2]= *ref;

                    }

                }else{

                    av_log(VAR_0->s.avctx, AV_LOG_ERROR, "illegal VAR_4\n");

                    return -1;

                }

            }

        }



        if(VAR_0->slice_type!=B_TYPE) break;

    }

    for(VAR_1=0; VAR_1<2; VAR_1++){

        for(VAR_2= 0; VAR_2 < VAR_0->ref_count[VAR_1]; VAR_2++){

            if(!VAR_0->ref_list[VAR_1][VAR_2].data[0])

                VAR_0->ref_list[VAR_1][VAR_2]= s->current_picture;

        }

        if(VAR_0->slice_type!=B_TYPE) break;

    }



    if(VAR_0->slice_type==B_TYPE && !VAR_0->direct_spatial_mv_pred)

        direct_dist_scale_factor(VAR_0);

    direct_ref_list_init(VAR_0);

    return 0;

}

static int FUNC_0(H264Context *VAR_0){ MpegEncContext * const s = &VAR_0->s; int VAR_1, VAR_2; print_short_term(VAR_0); print_long_term(VAR_0); if(VAR_0->slice_type==I_TYPE || VAR_0->slice_type==SI_TYPE) return 0; for(VAR_1=0; VAR_1<2; VAR_1++){ memcpy(VAR_0->ref_list[VAR_1], VAR_0->default_ref_list[VAR_1], sizeof(Picture)*VAR_0->ref_count[VAR_1]); if(get_bits1(&s->gb)){ int VAR_3= VAR_0->curr_pic_num; for(VAR_2=0; ; VAR_2++){ int VAR_4= get_ue_golomb(&s->gb); int VAR_5; int VAR_6; Picture *ref = NULL; if(VAR_4==3)
break; if(VAR_2 >= VAR_0->ref_count[VAR_1]){ av_log(VAR_0->s.avctx, AV_LOG_ERROR, "reference count overflow\n"); return -1; } if(VAR_4<3){ if(VAR_4<2){ const int VAR_7= get_ue_golomb(&s->gb) + 1; if(VAR_7 >= VAR_0->max_pic_num){ av_log(VAR_0->s.avctx, AV_LOG_ERROR, "VAR_7 overflow\n"); return -1; } if(VAR_4 == 0) VAR_3-= VAR_7; else                                VAR_3+= VAR_7; VAR_3 &= VAR_0->max_pic_num - 1; for(VAR_6= VAR_0->short_ref_count-1; VAR_6>=0; VAR_6--){ ref = VAR_0->short_ref[VAR_6]; assert(ref->reference == 3); assert(!ref->long_ref); if(ref->data[0] != NULL && ref->frame_num == VAR_3 && ref->long_ref == 0)
break; } if(VAR_6>=0)
ref->VAR_5= ref->frame_num; }else{ VAR_5= get_ue_golomb(&s->gb); ref = VAR_0->long_ref[VAR_5]; if(ref){ ref->VAR_5= VAR_5; assert(ref->reference == 3); assert(ref->long_ref); VAR_6=0; }else{ VAR_6=-1; } } if (VAR_6 < 0) { av_log(VAR_0->s.avctx, AV_LOG_ERROR, "reference picture missing during reorder\n"); memset(&VAR_0->ref_list[VAR_1][VAR_2], 0, sizeof(Picture)); } else { for(VAR_6=VAR_2; VAR_6+1<VAR_0->ref_count[VAR_1]; VAR_6++){ if(ref->long_ref == VAR_0->ref_list[VAR_1][VAR_6].long_ref && ref->VAR_5 == VAR_0->ref_list[VAR_1][VAR_6].VAR_5)
break; } for(; VAR_6 > VAR_2; VAR_6--){ VAR_0->ref_list[VAR_1][VAR_6]= VAR_0->ref_list[VAR_1][VAR_6-1]; } VAR_0->ref_list[VAR_1][VAR_2]= *ref; } }else{ av_log(VAR_0->s.avctx, AV_LOG_ERROR, "illegal VAR_4\n"); return -1; } } } if(VAR_0->slice_type!=B_TYPE) break; } for(VAR_1=0; VAR_1<2; VAR_1++){ for(VAR_2= 0; VAR_2 < VAR_0->ref_count[VAR_1]; VAR_2++){ if(!VAR_0->ref_list[VAR_1][VAR_2].data[0])
VAR_0->ref_list[VAR_1][VAR_2]= s->current_picture; } if(VAR_0->slice_type!=B_TYPE) break; } if(VAR_0->slice_type==B_TYPE && !VAR_0->direct_spatial_mv_pred)
direct_dist_scale_factor(VAR_0); direct_ref_list_init(VAR_0); return 0; }