static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,

                               uint8_t *header_idx, int frame_code)

{

    AVFormatContext *s = nut->avf;

    AVIOContext *bc    = s->pb;

    StreamContext *stc;

    int size, flags, size_mul, pts_delta, i, reserved_count;

    uint64_t tmp;



    if (avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {

        av_log(s, AV_LOG_ERROR,

               "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",

               avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);

        return AVERROR_INVALIDDATA;

    }



    flags          = nut->frame_code[frame_code].flags;

    size_mul       = nut->frame_code[frame_code].size_mul;

    size           = nut->frame_code[frame_code].size_lsb;

    *stream_id     = nut->frame_code[frame_code].stream_id;

    pts_delta      = nut->frame_code[frame_code].pts_delta;

    reserved_count = nut->frame_code[frame_code].reserved_count;

    *header_idx    = nut->frame_code[frame_code].header_idx;



    if (flags & FLAG_INVALID)

        return AVERROR_INVALIDDATA;

    if (flags & FLAG_CODED)

        flags ^= ffio_read_varlen(bc);

    if (flags & FLAG_STREAM_ID) {

        GET_V(*stream_id, tmp < s->nb_streams);

    }

    stc = &nut->stream[*stream_id];

    if (flags & FLAG_CODED_PTS) {

        int coded_pts = ffio_read_varlen(bc);

        // FIXME check last_pts validity?

        if (coded_pts < (1 << stc->msb_pts_shift)) {

            *pts = ff_lsb2full(stc, coded_pts);

        } else

            *pts = coded_pts - (1 << stc->msb_pts_shift);

    } else

        *pts = stc->last_pts + pts_delta;

    if (flags & FLAG_SIZE_MSB)

        size += size_mul * ffio_read_varlen(bc);

    if (flags & FLAG_MATCH_TIME)

        get_s(bc);

    if (flags & FLAG_HEADER_IDX)

        *header_idx = ffio_read_varlen(bc);

    if (flags & FLAG_RESERVED)

        reserved_count = ffio_read_varlen(bc);

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

        ffio_read_varlen(bc);



    if (*header_idx >= (unsigned)nut->header_count) {

        av_log(s, AV_LOG_ERROR, "header_idx invalid\n");

        return AVERROR_INVALIDDATA;

    }

    if (size > 4096)

        *header_idx = 0;

    size -= nut->header_len[*header_idx];



    if (flags & FLAG_CHECKSUM) {

        avio_rb32(bc); // FIXME check this

    } else if (size > 2 * nut->max_distance || FFABS(stc->last_pts - *pts) >

               stc->max_pts_distance) {

        av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");

        return AVERROR_INVALIDDATA;

    }



    stc->last_pts   = *pts;

    stc->last_flags = flags;



    return size;

}

static int decode_frame_header(NUTContext *nut, int64_t *pts, int *stream_id,

                               uint8_t *header_idx, int frame_code)

{

    AVFormatContext *s = nut->avf;

    AVIOContext *bc    = s->pb;

    StreamContext *stc;

    int size, flags, size_mul, pts_delta, i, reserved_count;

    uint64_t tmp;



    if (avio_tell(bc) > nut->last_syncpoint_pos + nut->max_distance) {

        av_log(s, AV_LOG_ERROR,

               "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",

               avio_tell(bc), nut->last_syncpoint_pos, nut->max_distance);

        return AVERROR_INVALIDDATA;

    }



    flags          = nut->frame_code[frame_code].flags;

    size_mul       = nut->frame_code[frame_code].size_mul;

    size           = nut->frame_code[frame_code].size_lsb;

    *stream_id     = nut->frame_code[frame_code].stream_id;

    pts_delta      = nut->frame_code[frame_code].pts_delta;

    reserved_count = nut->frame_code[frame_code].reserved_count;

    *header_idx    = nut->frame_code[frame_code].header_idx;



    if (flags & FLAG_INVALID)

        return AVERROR_INVALIDDATA;

    if (flags & FLAG_CODED)

        flags ^= ffio_read_varlen(bc);

    if (flags & FLAG_STREAM_ID) {

        GET_V(*stream_id, tmp < s->nb_streams);

    }

    stc = &nut->stream[*stream_id];

    if (flags & FLAG_CODED_PTS) {

        int coded_pts = ffio_read_varlen(bc);

        

        if (coded_pts < (1 << stc->msb_pts_shift)) {

            *pts = ff_lsb2full(stc, coded_pts);

        } else

            *pts = coded_pts - (1 << stc->msb_pts_shift);

    } else

        *pts = stc->last_pts + pts_delta;

    if (flags & FLAG_SIZE_MSB)

        size += size_mul * ffio_read_varlen(bc);

    if (flags & FLAG_MATCH_TIME)

        get_s(bc);

    if (flags & FLAG_HEADER_IDX)

        *header_idx = ffio_read_varlen(bc);

    if (flags & FLAG_RESERVED)

        reserved_count = ffio_read_varlen(bc);

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

        ffio_read_varlen(bc);



    if (*header_idx >= (unsigned)nut->header_count) {

        av_log(s, AV_LOG_ERROR, "header_idx invalid\n");

        return AVERROR_INVALIDDATA;

    }

    if (size > 4096)

        *header_idx = 0;

    size -= nut->header_len[*header_idx];



    if (flags & FLAG_CHECKSUM) {

        avio_rb32(bc); 

    } else if (size > 2 * nut->max_distance || FFABS(stc->last_pts - *pts) >

               stc->max_pts_distance) {

        av_log(s, AV_LOG_ERROR, "frame size > 2max_distance and no checksum\n");

        return AVERROR_INVALIDDATA;

    }



    stc->last_pts   = *pts;

    stc->last_flags = flags;



    return size;

}

            *pts = coded_pts - (1 << stc->msb_pts_shift);
static int FUNC_0(NUTContext *VAR_0, int64_t *VAR_1, int *VAR_2,

                               uint8_t *VAR_3, int VAR_4)

{

    AVFormatContext *s = VAR_0->avf;

    AVIOContext *bc    = s->pb;

    StreamContext *stc;

    int VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10;

    uint64_t tmp;



    if (avio_tell(bc) > VAR_0->last_syncpoint_pos + VAR_0->max_distance) {

        av_log(s, AV_LOG_ERROR,

               "Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",

               avio_tell(bc), VAR_0->last_syncpoint_pos, VAR_0->max_distance);

        return AVERROR_INVALIDDATA;

    }



    VAR_6          = VAR_0->VAR_4[VAR_4].VAR_6;

    VAR_7       = VAR_0->VAR_4[VAR_4].VAR_7;

    VAR_5           = VAR_0->VAR_4[VAR_4].size_lsb;

    *VAR_2     = VAR_0->VAR_4[VAR_4].VAR_2;

    VAR_8      = VAR_0->VAR_4[VAR_4].VAR_8;

    VAR_10 = VAR_0->VAR_4[VAR_4].VAR_10;

    *VAR_3    = VAR_0->VAR_4[VAR_4].VAR_3;



    if (VAR_6 & FLAG_INVALID)

        return AVERROR_INVALIDDATA;

    if (VAR_6 & FLAG_CODED)

        VAR_6 ^= ffio_read_varlen(bc);

    if (VAR_6 & FLAG_STREAM_ID) {

        GET_V(*VAR_2, tmp < s->nb_streams);

    }

    stc = &VAR_0->stream[*VAR_2];

    if (VAR_6 & FLAG_CODED_PTS) {

        int VAR_11 = ffio_read_varlen(bc);

        

        if (VAR_11 < (1 << stc->msb_pts_shift)) {

            *VAR_1 = ff_lsb2full(stc, VAR_11);

        } else

            *VAR_1 = VAR_11 - (1 << stc->msb_pts_shift);

    } else

        *VAR_1 = stc->last_pts + VAR_8;

    if (VAR_6 & FLAG_SIZE_MSB)

        VAR_5 += VAR_7 * ffio_read_varlen(bc);

    if (VAR_6 & FLAG_MATCH_TIME)

        get_s(bc);

    if (VAR_6 & FLAG_HEADER_IDX)

        *VAR_3 = ffio_read_varlen(bc);

    if (VAR_6 & FLAG_RESERVED)

        VAR_10 = ffio_read_varlen(bc);

    for (VAR_9 = 0; VAR_9 < VAR_10; VAR_9++)

        ffio_read_varlen(bc);



    if (*VAR_3 >= (unsigned)VAR_0->header_count) {

        av_log(s, AV_LOG_ERROR, "VAR_3 invalid\n");

        return AVERROR_INVALIDDATA;

    }

    if (VAR_5 > 4096)

        *VAR_3 = 0;

    VAR_5 -= VAR_0->header_len[*VAR_3];



    if (VAR_6 & FLAG_CHECKSUM) {

        avio_rb32(bc); 

    } else if (VAR_5 > 2 * VAR_0->max_distance || FFABS(stc->last_pts - *VAR_1) >

               stc->max_pts_distance) {

        av_log(s, AV_LOG_ERROR, "frame VAR_5 > 2max_distance and no checksum\n");

        return AVERROR_INVALIDDATA;

    }



    stc->last_pts   = *VAR_1;

    stc->last_flags = VAR_6;



    return VAR_5;

}

static int FUNC_0(NUTContext *VAR_0, int64_t *VAR_1, int *VAR_2,
uint8_t *VAR_3, int VAR_4)
{ AVFormatContext *s = VAR_0->avf; AVIOContext *bc    = s->pb; StreamContext *stc; int VAR_5, VAR_6, VAR_7, VAR_8, VAR_9, VAR_10; uint64_t tmp; if (avio_tell(bc) > VAR_0->last_syncpoint_pos + VAR_0->max_distance) { av_log(s, AV_LOG_ERROR,
"Last frame must have been damaged %"PRId64" > %"PRId64" + %d\n",
avio_tell(bc), VAR_0->last_syncpoint_pos, VAR_0->max_distance); return AVERROR_INVALIDDATA; } VAR_6          = VAR_0->VAR_4[VAR_4].VAR_6; VAR_7       = VAR_0->VAR_4[VAR_4].VAR_7; VAR_5           = VAR_0->VAR_4[VAR_4].size_lsb; *VAR_2     = VAR_0->VAR_4[VAR_4].VAR_2; VAR_8      = VAR_0->VAR_4[VAR_4].VAR_8; VAR_10 = VAR_0->VAR_4[VAR_4].VAR_10; *VAR_3    = VAR_0->VAR_4[VAR_4].VAR_3; if (VAR_6 & FLAG_INVALID)
return AVERROR_INVALIDDATA; if (VAR_6 & FLAG_CODED)
VAR_6 ^= ffio_read_varlen(bc); if (VAR_6 & FLAG_STREAM_ID) { GET_V(*VAR_2, tmp < s->nb_streams); } stc = &VAR_0->stream[*VAR_2]; if (VAR_6 & FLAG_CODED_PTS) { int VAR_11 = ffio_read_varlen(bc); if (VAR_11 < (1 << stc->msb_pts_shift)) { *VAR_1 = ff_lsb2full(stc, VAR_11); } else *VAR_1 = VAR_11 - (1 << stc->msb_pts_shift); } else *VAR_1 = stc->last_pts + VAR_8; if (VAR_6 & FLAG_SIZE_MSB)
VAR_5 += VAR_7 * ffio_read_varlen(bc); if (VAR_6 & FLAG_MATCH_TIME)
get_s(bc); if (VAR_6 & FLAG_HEADER_IDX)
*VAR_3 = ffio_read_varlen(bc); if (VAR_6 & FLAG_RESERVED)
VAR_10 = ffio_read_varlen(bc); for (VAR_9 = 0; VAR_9 < VAR_10; VAR_9++) ffio_read_varlen(bc); if (*VAR_3 >= (unsigned)VAR_0->header_count) { av_log(s, AV_LOG_ERROR, "VAR_3 invalid\n"); return AVERROR_INVALIDDATA; } if (VAR_5 > 4096)
*VAR_3 = 0; VAR_5 -= VAR_0->header_len[*VAR_3]; if (VAR_6 & FLAG_CHECKSUM) { avio_rb32(bc); } else if (VAR_5 > 2 * VAR_0->max_distance || FFABS(stc->last_pts - *VAR_1) > stc->max_pts_distance) { av_log(s, AV_LOG_ERROR, "frame VAR_5 > 2max_distance and no checksum\n"); return AVERROR_INVALIDDATA; } stc->last_pts   = *VAR_1; stc->last_flags = VAR_6; return VAR_5; }