static int dirac_decode_picture_header(DiracContext *s)

{

    unsigned retire, picnum;

    int i, j, ret;

    int64_t refdist, refnum;

    GetBitContext *gb = &s->gb;



    /* [DIRAC_STD] 11.1.1 Picture Header. picture_header() PICTURE_NUM */

    picnum = s->current_picture->avframe->display_picture_number = get_bits_long(gb, 32);





    av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);



    /* if this is the first keyframe after a sequence header, start our

       reordering from here */

    if (s->frame_number < 0)

        s->frame_number = picnum;



    s->ref_pics[0] = s->ref_pics[1] = NULL;

    for (i = 0; i < s->num_refs; i++) {

        refnum = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;

        refdist = INT64_MAX;



        /* find the closest reference to the one we want */

        /* Jordi: this is needed if the referenced picture hasn't yet arrived */

        for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++)

            if (s->ref_frames[j]

                && FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum) < refdist) {

                s->ref_pics[i] = s->ref_frames[j];

                refdist = FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum);

            }



        if (!s->ref_pics[i] || refdist)

            av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");



        /* if there were no references at all, allocate one */

        if (!s->ref_pics[i])

            for (j = 0; j < MAX_FRAMES; j++)

                if (!s->all_frames[j].avframe->data[0]) {

                    s->ref_pics[i] = &s->all_frames[j];

                    get_buffer_with_edge(s->avctx, s->ref_pics[i]->avframe, AV_GET_BUFFER_FLAG_REF);

                    break;

                }



        if (!s->ref_pics[i]) {

            av_log(s->avctx, AV_LOG_ERROR, "Reference could not be allocated\n");

            return AVERROR_INVALIDDATA;

        }



    }



    /* retire the reference frames that are not used anymore */

    if (s->current_picture->reference) {

        retire = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;

        if (retire != picnum) {

            DiracFrame *retire_pic = remove_frame(s->ref_frames, retire);



            if (retire_pic)

                retire_pic->reference &= DELAYED_PIC_REF;

            else

                av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");

        }



        /* if reference array is full, remove the oldest as per the spec */

        while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) {

            av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");

            remove_frame(s->ref_frames, s->ref_frames[0]->avframe->display_picture_number)->reference &= DELAYED_PIC_REF;

        }

    }



    if (s->num_refs) {

        ret = dirac_unpack_prediction_parameters(s);  /* [DIRAC_STD] 11.2 Picture Prediction Data. picture_prediction() */

        if (ret < 0)

            return ret;

        ret = dirac_unpack_block_motion_data(s);      /* [DIRAC_STD] 12. Block motion data syntax                       */

        if (ret < 0)

            return ret;

    }

    ret = dirac_unpack_idwt_params(s);                /* [DIRAC_STD] 11.3 Wavelet transform data                        */

    if (ret < 0)

        return ret;



    init_planes(s);

    return 0;

}

static int dirac_decode_picture_header(DiracContext *s)

{

    unsigned retire, picnum;

    int i, j, ret;

    int64_t refdist, refnum;

    GetBitContext *gb = &s->gb;



    

    picnum = s->current_picture->avframe->display_picture_number = get_bits_long(gb, 32);





    av_log(s->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",picnum);



    



    if (s->frame_number < 0)

        s->frame_number = picnum;



    s->ref_pics[0] = s->ref_pics[1] = NULL;

    for (i = 0; i < s->num_refs; i++) {

        refnum = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;

        refdist = INT64_MAX;



        

        

        for (j = 0; j < MAX_REFERENCE_FRAMES && refdist; j++)

            if (s->ref_frames[j]

                && FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum) < refdist) {

                s->ref_pics[i] = s->ref_frames[j];

                refdist = FFABS(s->ref_frames[j]->avframe->display_picture_number - refnum);

            }



        if (!s->ref_pics[i] || refdist)

            av_log(s->avctx, AV_LOG_DEBUG, "Reference not found\n");



        

        if (!s->ref_pics[i])

            for (j = 0; j < MAX_FRAMES; j++)

                if (!s->all_frames[j].avframe->data[0]) {

                    s->ref_pics[i] = &s->all_frames[j];

                    get_buffer_with_edge(s->avctx, s->ref_pics[i]->avframe, AV_GET_BUFFER_FLAG_REF);

                    break;

                }



        if (!s->ref_pics[i]) {

            av_log(s->avctx, AV_LOG_ERROR, "Reference could not be allocated\n");

            return AVERROR_INVALIDDATA;

        }



    }



    

    if (s->current_picture->reference) {

        retire = (picnum + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;

        if (retire != picnum) {

            DiracFrame *retire_pic = remove_frame(s->ref_frames, retire);



            if (retire_pic)

                retire_pic->reference &= DELAYED_PIC_REF;

            else

                av_log(s->avctx, AV_LOG_DEBUG, "Frame to retire not found\n");

        }



        

        while (add_frame(s->ref_frames, MAX_REFERENCE_FRAMES, s->current_picture)) {

            av_log(s->avctx, AV_LOG_ERROR, "Reference frame overflow\n");

            remove_frame(s->ref_frames, s->ref_frames[0]->avframe->display_picture_number)->reference &= DELAYED_PIC_REF;

        }

    }



    if (s->num_refs) {

        ret = dirac_unpack_prediction_parameters(s);  

        if (ret < 0)

            return ret;

        ret = dirac_unpack_block_motion_data(s);      

        if (ret < 0)

            return ret;

    }

    ret = dirac_unpack_idwt_params(s);                

    if (ret < 0)

        return ret;



    init_planes(s);

    return 0;

}

                    get_buffer_with_edge(s->avctx, s->ref_pics[i]->avframe, AV_GET_BUFFER_FLAG_REF);
static int FUNC_0(DiracContext *VAR_0)

{

    unsigned VAR_1, VAR_2;

    int VAR_3, VAR_4, VAR_5;

    int64_t refdist, refnum;

    GetBitContext *gb = &VAR_0->gb;



    

    VAR_2 = VAR_0->current_picture->avframe->display_picture_number = get_bits_long(gb, 32);





    av_log(VAR_0->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",VAR_2);



    



    if (VAR_0->frame_number < 0)

        VAR_0->frame_number = VAR_2;



    VAR_0->ref_pics[0] = VAR_0->ref_pics[1] = NULL;

    for (VAR_3 = 0; VAR_3 < VAR_0->num_refs; VAR_3++) {

        refnum = (VAR_2 + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;

        refdist = INT64_MAX;



        

        

        for (VAR_4 = 0; VAR_4 < MAX_REFERENCE_FRAMES && refdist; VAR_4++)

            if (VAR_0->ref_frames[VAR_4]

                && FFABS(VAR_0->ref_frames[VAR_4]->avframe->display_picture_number - refnum) < refdist) {

                VAR_0->ref_pics[VAR_3] = VAR_0->ref_frames[VAR_4];

                refdist = FFABS(VAR_0->ref_frames[VAR_4]->avframe->display_picture_number - refnum);

            }



        if (!VAR_0->ref_pics[VAR_3] || refdist)

            av_log(VAR_0->avctx, AV_LOG_DEBUG, "Reference not found\n");



        

        if (!VAR_0->ref_pics[VAR_3])

            for (VAR_4 = 0; VAR_4 < MAX_FRAMES; VAR_4++)

                if (!VAR_0->all_frames[VAR_4].avframe->data[0]) {

                    VAR_0->ref_pics[VAR_3] = &VAR_0->all_frames[VAR_4];

                    get_buffer_with_edge(VAR_0->avctx, VAR_0->ref_pics[VAR_3]->avframe, AV_GET_BUFFER_FLAG_REF);

                    break;

                }



        if (!VAR_0->ref_pics[VAR_3]) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Reference could not be allocated\n");

            return AVERROR_INVALIDDATA;

        }



    }



    

    if (VAR_0->current_picture->reference) {

        VAR_1 = (VAR_2 + dirac_get_se_golomb(gb)) & 0xFFFFFFFF;

        if (VAR_1 != VAR_2) {

            DiracFrame *retire_pic = remove_frame(VAR_0->ref_frames, VAR_1);



            if (retire_pic)

                retire_pic->reference &= DELAYED_PIC_REF;

            else

                av_log(VAR_0->avctx, AV_LOG_DEBUG, "Frame to VAR_1 not found\n");

        }



        

        while (add_frame(VAR_0->ref_frames, MAX_REFERENCE_FRAMES, VAR_0->current_picture)) {

            av_log(VAR_0->avctx, AV_LOG_ERROR, "Reference frame overflow\n");

            remove_frame(VAR_0->ref_frames, VAR_0->ref_frames[0]->avframe->display_picture_number)->reference &= DELAYED_PIC_REF;

        }

    }



    if (VAR_0->num_refs) {

        VAR_5 = dirac_unpack_prediction_parameters(VAR_0);  

        if (VAR_5 < 0)

            return VAR_5;

        VAR_5 = dirac_unpack_block_motion_data(VAR_0);      

        if (VAR_5 < 0)

            return VAR_5;

    }

    VAR_5 = dirac_unpack_idwt_params(VAR_0);                

    if (VAR_5 < 0)

        return VAR_5;



    init_planes(VAR_0);

    return 0;

}

static int FUNC_0(DiracContext *VAR_0)
{ unsigned VAR_1, VAR_2; int VAR_3, VAR_4, VAR_5; int64_t refdist, refnum; GetBitContext *gb = &VAR_0->gb; VAR_2 = VAR_0->current_picture->avframe->display_picture_number = get_bits_long(gb, 32); av_log(VAR_0->avctx,AV_LOG_DEBUG,"PICTURE_NUM: %d\n",VAR_2); if (VAR_0->frame_number < 0)
VAR_0->frame_number = VAR_2; VAR_0->ref_pics[0] = VAR_0->ref_pics[1] = NULL; for (VAR_3 = 0; VAR_3 < VAR_0->num_refs; VAR_3++) { refnum = (VAR_2 + dirac_get_se_golomb(gb)) & 0xFFFFFFFF; refdist = INT64_MAX; for (VAR_4 = 0; VAR_4 < MAX_REFERENCE_FRAMES && refdist; VAR_4++) if (VAR_0->ref_frames[VAR_4]
&& FFABS(VAR_0->ref_frames[VAR_4]->avframe->display_picture_number - refnum) < refdist) { VAR_0->ref_pics[VAR_3] = VAR_0->ref_frames[VAR_4]; refdist = FFABS(VAR_0->ref_frames[VAR_4]->avframe->display_picture_number - refnum); } if (!VAR_0->ref_pics[VAR_3] || refdist)
av_log(VAR_0->avctx, AV_LOG_DEBUG, "Reference not found\n"); if (!VAR_0->ref_pics[VAR_3])
for (VAR_4 = 0; VAR_4 < MAX_FRAMES; VAR_4++) if (!VAR_0->all_frames[VAR_4].avframe->data[0]) { VAR_0->ref_pics[VAR_3] = &VAR_0->all_frames[VAR_4]; get_buffer_with_edge(VAR_0->avctx, VAR_0->ref_pics[VAR_3]->avframe, AV_GET_BUFFER_FLAG_REF); break; } if (!VAR_0->ref_pics[VAR_3]) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Reference could not be allocated\n"); return AVERROR_INVALIDDATA; } } if (VAR_0->current_picture->reference) { VAR_1 = (VAR_2 + dirac_get_se_golomb(gb)) & 0xFFFFFFFF; if (VAR_1 != VAR_2) { DiracFrame *retire_pic = remove_frame(VAR_0->ref_frames, VAR_1); if (retire_pic)
retire_pic->reference &= DELAYED_PIC_REF; else
av_log(VAR_0->avctx, AV_LOG_DEBUG, "Frame to VAR_1 not found\n"); } while (add_frame(VAR_0->ref_frames, MAX_REFERENCE_FRAMES, VAR_0->current_picture)) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Reference frame overflow\n"); remove_frame(VAR_0->ref_frames, VAR_0->ref_frames[0]->avframe->display_picture_number)->reference &= DELAYED_PIC_REF; } } if (VAR_0->num_refs) { VAR_5 = dirac_unpack_prediction_parameters(VAR_0); if (VAR_5 < 0)
return VAR_5; VAR_5 = dirac_unpack_block_motion_data(VAR_0); if (VAR_5 < 0)
return VAR_5; } VAR_5 = dirac_unpack_idwt_params(VAR_0); if (VAR_5 < 0)
return VAR_5; init_planes(VAR_0); return 0; }