static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)

{

    DiracContext *s     = avctx->priv_data;

    AVFrame *picture    = data;

    uint8_t *buf        = pkt->data;

    int buf_size        = pkt->size;

    int i, data_unit_size, buf_idx = 0;

    int ret;



    /* release unused frames */

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

        if (s->all_frames[i].avframe->data[0] && !s->all_frames[i].avframe->reference) {

            av_frame_unref(s->all_frames[i].avframe);

            memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));

        }



    s->current_picture = NULL;

    *got_frame = 0;



    /* end of stream, so flush delayed pics */

    if (buf_size == 0)

        return get_delayed_pic(s, (AVFrame *)data, got_frame);



    for (;;) {

        /*[DIRAC_STD] Here starts the code from parse_info() defined in 9.6

          [DIRAC_STD] PARSE_INFO_PREFIX = "BBCD" as defined in ISO/IEC 646

          BBCD start code search */

        for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) {

            if (buf[buf_idx  ] == 'B' && buf[buf_idx+1] == 'B' &&

                buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D')

                break;

        }

        /* BBCD found or end of data */

        if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)

            break;



        data_unit_size = AV_RB32(buf+buf_idx+5);

        if (data_unit_size > buf_size - buf_idx || !data_unit_size) {

            if(data_unit_size > buf_size - buf_idx)

            av_log(s->avctx, AV_LOG_ERROR,

                   "Data unit with size %d is larger than input buffer, discarding\n",

                   data_unit_size);

            buf_idx += 4;

            continue;

        }

        /* [DIRAC_STD] dirac_decode_data_unit makes reference to the while defined in 9.3 inside the function parse_sequence() */

        ret = dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size);

        if (ret < 0)

        {

            av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");

            return ret;

        }

        buf_idx += data_unit_size;

    }



    if (!s->current_picture)

        return buf_size;



    if (s->current_picture->avframe->display_picture_number > s->frame_number) {

        DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);



        s->current_picture->avframe->reference |= DELAYED_PIC_REF;



        if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {

            int min_num = s->delay_frames[0]->avframe->display_picture_number;

            /* Too many delayed frames, so we display the frame with the lowest pts */

            av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n");



            for (i = 1; s->delay_frames[i]; i++)

                if (s->delay_frames[i]->avframe->display_picture_number < min_num)

                    min_num = s->delay_frames[i]->avframe->display_picture_number;



            delayed_frame = remove_frame(s->delay_frames, min_num);

            add_frame(s->delay_frames, MAX_DELAY, s->current_picture);

        }



        if (delayed_frame) {

            delayed_frame->avframe->reference ^= DELAYED_PIC_REF;

            if((ret=av_frame_ref(data, delayed_frame->avframe)) < 0)

                return ret;

            *got_frame = 1;

        }

    } else if (s->current_picture->avframe->display_picture_number == s->frame_number) {

        /* The right frame at the right time :-) */

        if((ret=av_frame_ref(data, s->current_picture->avframe)) < 0)

            return ret;

        *got_frame = 1;

    }



    if (*got_frame)

        s->frame_number = picture->display_picture_number + 1;



    return buf_idx;

}

static int dirac_decode_frame(AVCodecContext *avctx, void *data, int *got_frame, AVPacket *pkt)

{

    DiracContext *s     = avctx->priv_data;

    AVFrame *picture    = data;

    uint8_t *buf        = pkt->data;

    int buf_size        = pkt->size;

    int i, data_unit_size, buf_idx = 0;

    int ret;



    

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

        if (s->all_frames[i].avframe->data[0] && !s->all_frames[i].avframe->reference) {

            av_frame_unref(s->all_frames[i].avframe);

            memset(s->all_frames[i].interpolated, 0, sizeof(s->all_frames[i].interpolated));

        }



    s->current_picture = NULL;

    *got_frame = 0;



    

    if (buf_size == 0)

        return get_delayed_pic(s, (AVFrame *)data, got_frame);



    for (;;) {

        





        for (; buf_idx + DATA_UNIT_HEADER_SIZE < buf_size; buf_idx++) {

            if (buf[buf_idx  ] == 'B' && buf[buf_idx+1] == 'B' &&

                buf[buf_idx+2] == 'C' && buf[buf_idx+3] == 'D')

                break;

        }

        

        if (buf_idx + DATA_UNIT_HEADER_SIZE >= buf_size)

            break;



        data_unit_size = AV_RB32(buf+buf_idx+5);

        if (data_unit_size > buf_size - buf_idx || !data_unit_size) {

            if(data_unit_size > buf_size - buf_idx)

            av_log(s->avctx, AV_LOG_ERROR,

                   "Data unit with size %d is larger than input buffer, discarding\n",

                   data_unit_size);

            buf_idx += 4;

            continue;

        }

        

        ret = dirac_decode_data_unit(avctx, buf+buf_idx, data_unit_size);

        if (ret < 0)

        {

            av_log(s->avctx, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");

            return ret;

        }

        buf_idx += data_unit_size;

    }



    if (!s->current_picture)

        return buf_size;



    if (s->current_picture->avframe->display_picture_number > s->frame_number) {

        DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);



        s->current_picture->avframe->reference |= DELAYED_PIC_REF;



        if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {

            int min_num = s->delay_frames[0]->avframe->display_picture_number;

            

            av_log(avctx, AV_LOG_ERROR, "Delay frame overflow\n");



            for (i = 1; s->delay_frames[i]; i++)

                if (s->delay_frames[i]->avframe->display_picture_number < min_num)

                    min_num = s->delay_frames[i]->avframe->display_picture_number;



            delayed_frame = remove_frame(s->delay_frames, min_num);

            add_frame(s->delay_frames, MAX_DELAY, s->current_picture);

        }



        if (delayed_frame) {

            delayed_frame->avframe->reference ^= DELAYED_PIC_REF;

            if((ret=av_frame_ref(data, delayed_frame->avframe)) < 0)

                return ret;

            *got_frame = 1;

        }

    } else if (s->current_picture->avframe->display_picture_number == s->frame_number) {

        

        if((ret=av_frame_ref(data, s->current_picture->avframe)) < 0)

            return ret;

        *got_frame = 1;

    }



    if (*got_frame)

        s->frame_number = picture->display_picture_number + 1;



    return buf_idx;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2, AVPacket *VAR_3)

{

    DiracContext *s     = VAR_0->priv_data;

    AVFrame *picture    = VAR_1;

    uint8_t *buf        = VAR_3->VAR_1;

    int VAR_4        = VAR_3->size;

    int VAR_5, VAR_6, VAR_7 = 0;

    int VAR_8;



    

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

        if (s->all_frames[VAR_5].avframe->VAR_1[0] && !s->all_frames[VAR_5].avframe->reference) {

            av_frame_unref(s->all_frames[VAR_5].avframe);

            memset(s->all_frames[VAR_5].interpolated, 0, sizeof(s->all_frames[VAR_5].interpolated));

        }



    s->current_picture = NULL;

    *VAR_2 = 0;



    

    if (VAR_4 == 0)

        return get_delayed_pic(s, (AVFrame *)VAR_1, VAR_2);



    for (;;) {

        





        for (; VAR_7 + DATA_UNIT_HEADER_SIZE < VAR_4; VAR_7++) {

            if (buf[VAR_7  ] == 'B' && buf[VAR_7+1] == 'B' &&

                buf[VAR_7+2] == 'C' && buf[VAR_7+3] == 'D')

                break;

        }

        

        if (VAR_7 + DATA_UNIT_HEADER_SIZE >= VAR_4)

            break;



        VAR_6 = AV_RB32(buf+VAR_7+5);

        if (VAR_6 > VAR_4 - VAR_7 || !VAR_6) {

            if(VAR_6 > VAR_4 - VAR_7)

            av_log(s->VAR_0, AV_LOG_ERROR,

                   "Data unit with size %d is larger than input buffer, discarding\n",

                   VAR_6);

            VAR_7 += 4;

            continue;

        }

        

        VAR_8 = dirac_decode_data_unit(VAR_0, buf+VAR_7, VAR_6);

        if (VAR_8 < 0)

        {

            av_log(s->VAR_0, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n");

            return VAR_8;

        }

        VAR_7 += VAR_6;

    }



    if (!s->current_picture)

        return VAR_4;



    if (s->current_picture->avframe->display_picture_number > s->frame_number) {

        DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number);



        s->current_picture->avframe->reference |= DELAYED_PIC_REF;



        if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) {

            int VAR_9 = s->delay_frames[0]->avframe->display_picture_number;

            

            av_log(VAR_0, AV_LOG_ERROR, "Delay frame overflow\n");



            for (VAR_5 = 1; s->delay_frames[VAR_5]; VAR_5++)

                if (s->delay_frames[VAR_5]->avframe->display_picture_number < VAR_9)

                    VAR_9 = s->delay_frames[VAR_5]->avframe->display_picture_number;



            delayed_frame = remove_frame(s->delay_frames, VAR_9);

            add_frame(s->delay_frames, MAX_DELAY, s->current_picture);

        }



        if (delayed_frame) {

            delayed_frame->avframe->reference ^= DELAYED_PIC_REF;

            if((VAR_8=av_frame_ref(VAR_1, delayed_frame->avframe)) < 0)

                return VAR_8;

            *VAR_2 = 1;

        }

    } else if (s->current_picture->avframe->display_picture_number == s->frame_number) {

        

        if((VAR_8=av_frame_ref(VAR_1, s->current_picture->avframe)) < 0)

            return VAR_8;

        *VAR_2 = 1;

    }



    if (*VAR_2)

        s->frame_number = picture->display_picture_number + 1;



    return VAR_7;

}

static int FUNC_0(AVCodecContext *VAR_0, void *VAR_1, int *VAR_2, AVPacket *VAR_3)
{ DiracContext *s     = VAR_0->priv_data; AVFrame *picture    = VAR_1; uint8_t *buf        = VAR_3->VAR_1; int VAR_4        = VAR_3->size; int VAR_5, VAR_6, VAR_7 = 0; int VAR_8; for (VAR_5 = 0; VAR_5 < MAX_FRAMES; VAR_5++) if (s->all_frames[VAR_5].avframe->VAR_1[0] && !s->all_frames[VAR_5].avframe->reference) { av_frame_unref(s->all_frames[VAR_5].avframe); memset(s->all_frames[VAR_5].interpolated, 0, sizeof(s->all_frames[VAR_5].interpolated)); } s->current_picture = NULL; *VAR_2 = 0; if (VAR_4 == 0)
return get_delayed_pic(s, (AVFrame *)VAR_1, VAR_2); for (;;) { for (; VAR_7 + DATA_UNIT_HEADER_SIZE < VAR_4; VAR_7++) { if (buf[VAR_7  ] == 'B' && buf[VAR_7+1] == 'B' &&
buf[VAR_7+2] == 'C' && buf[VAR_7+3] == 'D')
break; } if (VAR_7 + DATA_UNIT_HEADER_SIZE >= VAR_4)
break; VAR_6 = AV_RB32(buf+VAR_7+5); if (VAR_6 > VAR_4 - VAR_7 || !VAR_6) { if(VAR_6 > VAR_4 - VAR_7)
av_log(s->VAR_0, AV_LOG_ERROR,
"Data unit with size %d is larger than input buffer, discarding\n",
VAR_6); VAR_7 += 4; continue; } VAR_8 = dirac_decode_data_unit(VAR_0, buf+VAR_7, VAR_6); if (VAR_8 < 0)
{ av_log(s->VAR_0, AV_LOG_ERROR,"Error in dirac_decode_data_unit\n"); return VAR_8; } VAR_7 += VAR_6; } if (!s->current_picture)
return VAR_4; if (s->current_picture->avframe->display_picture_number > s->frame_number) { DiracFrame *delayed_frame = remove_frame(s->delay_frames, s->frame_number); s->current_picture->avframe->reference |= DELAYED_PIC_REF; if (add_frame(s->delay_frames, MAX_DELAY, s->current_picture)) { int VAR_9 = s->delay_frames[0]->avframe->display_picture_number; av_log(VAR_0, AV_LOG_ERROR, "Delay frame overflow\n"); for (VAR_5 = 1; s->delay_frames[VAR_5]; VAR_5++) if (s->delay_frames[VAR_5]->avframe->display_picture_number < VAR_9)
VAR_9 = s->delay_frames[VAR_5]->avframe->display_picture_number; delayed_frame = remove_frame(s->delay_frames, VAR_9); add_frame(s->delay_frames, MAX_DELAY, s->current_picture); } if (delayed_frame) { delayed_frame->avframe->reference ^= DELAYED_PIC_REF; if((VAR_8=av_frame_ref(VAR_1, delayed_frame->avframe)) < 0)
return VAR_8; *VAR_2 = 1; } } else if (s->current_picture->avframe->display_picture_number == s->frame_number) { if((VAR_8=av_frame_ref(VAR_1, s->current_picture->avframe)) < 0)
return VAR_8; *VAR_2 = 1; } if (*VAR_2)
s->frame_number = picture->display_picture_number + 1; return VAR_7; }