static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)

{

    MOVFragment *frag = &c->fragment;

    AVStream *st = c->fc->streams[frag->track_id-1];

    MOVStreamContext *sc = st->priv_data;

    uint64_t offset;

    int64_t dts;

    int data_offset = 0;

    unsigned entries, first_sample_flags = frag->flags;

    int flags, distance, i;



    if (sc->pseudo_stream_id+1 != frag->stsd_id)

        return 0;

    if (!st->nb_index_entries)

        return -1;

    get_byte(pb); /* version */

    flags = get_be24(pb);

    entries = get_be32(pb);

    dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);

    if (flags & 0x001) data_offset        = get_be32(pb);

    if (flags & 0x004) first_sample_flags = get_be32(pb);

    if (flags & 0x800) {

        if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))

            return -1;

        sc->ctts_data = av_realloc(sc->ctts_data,

                                   (entries+sc->ctts_count)*sizeof(*sc->ctts_data));

        if (!sc->ctts_data)

            return AVERROR(ENOMEM);

    }

    dts = st->duration;

    offset = frag->base_data_offset + data_offset;

    distance = 0;

    dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);

    for (i = 0; i < entries; i++) {

        unsigned sample_size = frag->size;

        int sample_flags = i ? frag->flags : first_sample_flags;

        unsigned sample_duration = frag->duration;

        int keyframe;



        if (flags & 0x100) sample_duration = get_be32(pb);

        if (flags & 0x200) sample_size     = get_be32(pb);

        if (flags & 0x400) sample_flags    = get_be32(pb);

        if (flags & 0x800) {

            sc->ctts_data[sc->ctts_count].count = 1;

            sc->ctts_data[sc->ctts_count].duration = get_be32(pb);

            sc->ctts_count++;

        }

        if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||

             (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))

            distance = 0;

        av_add_index_entry(st, offset, dts, sample_size, distance,

                           keyframe ? AVINDEX_KEYFRAME : 0);

        dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "

                "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,

                offset, dts, sample_size, distance, keyframe);

        distance++;

        assert(sample_duration % sc->time_rate == 0);

        dts += sample_duration / sc->time_rate;

        offset += sample_size;

    }

    frag->moof_offset = offset;

    sc->sample_count = st->nb_index_entries;

    st->duration = dts;

    return 0;

}

static int mov_read_trun(MOVContext *c, ByteIOContext *pb, MOV_atom_t atom)

{

    MOVFragment *frag = &c->fragment;

    AVStream *st = c->fc->streams[frag->track_id-1];

    MOVStreamContext *sc = st->priv_data;

    uint64_t offset;

    int64_t dts;

    int data_offset = 0;

    unsigned entries, first_sample_flags = frag->flags;

    int flags, distance, i;



    if (sc->pseudo_stream_id+1 != frag->stsd_id)

        return 0;

    if (!st->nb_index_entries)

        return -1;

    get_byte(pb); 

    flags = get_be24(pb);

    entries = get_be32(pb);

    dprintf(c->fc, "flags 0x%x entries %d\n", flags, entries);

    if (flags & 0x001) data_offset        = get_be32(pb);

    if (flags & 0x004) first_sample_flags = get_be32(pb);

    if (flags & 0x800) {

        if ((uint64_t)entries+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))

            return -1;

        sc->ctts_data = av_realloc(sc->ctts_data,

                                   (entries+sc->ctts_count)*sizeof(*sc->ctts_data));

        if (!sc->ctts_data)

            return AVERROR(ENOMEM);

    }

    dts = st->duration;

    offset = frag->base_data_offset + data_offset;

    distance = 0;

    dprintf(c->fc, "first sample flags 0x%x\n", first_sample_flags);

    for (i = 0; i < entries; i++) {

        unsigned sample_size = frag->size;

        int sample_flags = i ? frag->flags : first_sample_flags;

        unsigned sample_duration = frag->duration;

        int keyframe;



        if (flags & 0x100) sample_duration = get_be32(pb);

        if (flags & 0x200) sample_size     = get_be32(pb);

        if (flags & 0x400) sample_flags    = get_be32(pb);

        if (flags & 0x800) {

            sc->ctts_data[sc->ctts_count].count = 1;

            sc->ctts_data[sc->ctts_count].duration = get_be32(pb);

            sc->ctts_count++;

        }

        if ((keyframe = st->codec->codec_type == CODEC_TYPE_AUDIO ||

             (flags & 0x004 && !i && !sample_flags) || sample_flags & 0x2000000))

            distance = 0;

        av_add_index_entry(st, offset, dts, sample_size, distance,

                           keyframe ? AVINDEX_KEYFRAME : 0);

        dprintf(c->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "

                "size %d, distance %d, keyframe %d\n", st->index, sc->sample_count+i,

                offset, dts, sample_size, distance, keyframe);

        distance++;

        assert(sample_duration % sc->time_rate == 0);

        dts += sample_duration / sc->time_rate;

        offset += sample_size;

    }

    frag->moof_offset = offset;

    sc->sample_count = st->nb_index_entries;

    st->duration = dts;

    return 0;

}

static int FUNC_0(MOVContext *VAR_0, ByteIOContext *VAR_1, MOV_atom_t VAR_2)

{

    MOVFragment *frag = &VAR_0->fragment;

    AVStream *st = VAR_0->fc->streams[frag->track_id-1];

    MOVStreamContext *sc = st->priv_data;

    uint64_t offset;

    int64_t dts;

    int VAR_3 = 0;

    unsigned VAR_4, VAR_5 = frag->VAR_6;

    int VAR_6, VAR_7, VAR_8;



    if (sc->pseudo_stream_id+1 != frag->stsd_id)

        return 0;

    if (!st->nb_index_entries)

        return -1;

    get_byte(VAR_1); 

    VAR_6 = get_be24(VAR_1);

    VAR_4 = get_be32(VAR_1);

    dprintf(VAR_0->fc, "VAR_6 0x%x VAR_4 %d\n", VAR_6, VAR_4);

    if (VAR_6 & 0x001) VAR_3        = get_be32(VAR_1);

    if (VAR_6 & 0x004) VAR_5 = get_be32(VAR_1);

    if (VAR_6 & 0x800) {

        if ((uint64_t)VAR_4+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))

            return -1;

        sc->ctts_data = av_realloc(sc->ctts_data,

                                   (VAR_4+sc->ctts_count)*sizeof(*sc->ctts_data));

        if (!sc->ctts_data)

            return AVERROR(ENOMEM);

    }

    dts = st->duration;

    offset = frag->base_data_offset + VAR_3;

    VAR_7 = 0;

    dprintf(VAR_0->fc, "first sample VAR_6 0x%x\n", VAR_5);

    for (VAR_8 = 0; VAR_8 < VAR_4; VAR_8++) {

        unsigned VAR_9 = frag->size;

        int VAR_10 = VAR_8 ? frag->VAR_6 : VAR_5;

        unsigned VAR_11 = frag->duration;

        int VAR_12;



        if (VAR_6 & 0x100) VAR_11 = get_be32(VAR_1);

        if (VAR_6 & 0x200) VAR_9     = get_be32(VAR_1);

        if (VAR_6 & 0x400) VAR_10    = get_be32(VAR_1);

        if (VAR_6 & 0x800) {

            sc->ctts_data[sc->ctts_count].count = 1;

            sc->ctts_data[sc->ctts_count].duration = get_be32(VAR_1);

            sc->ctts_count++;

        }

        if ((VAR_12 = st->codec->codec_type == CODEC_TYPE_AUDIO ||

             (VAR_6 & 0x004 && !VAR_8 && !VAR_10) || VAR_10 & 0x2000000))

            VAR_7 = 0;

        av_add_index_entry(st, offset, dts, VAR_9, VAR_7,

                           VAR_12 ? AVINDEX_KEYFRAME : 0);

        dprintf(VAR_0->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "

                "size %d, VAR_7 %d, VAR_12 %d\n", st->index, sc->sample_count+VAR_8,

                offset, dts, VAR_9, VAR_7, VAR_12);

        VAR_7++;

        assert(VAR_11 % sc->time_rate == 0);

        dts += VAR_11 / sc->time_rate;

        offset += VAR_9;

    }

    frag->moof_offset = offset;

    sc->sample_count = st->nb_index_entries;

    st->duration = dts;

    return 0;

}

static int FUNC_0(MOVContext *VAR_0, ByteIOContext *VAR_1, MOV_atom_t VAR_2)
{ MOVFragment *frag = &VAR_0->fragment; AVStream *st = VAR_0->fc->streams[frag->track_id-1]; MOVStreamContext *sc = st->priv_data; uint64_t offset; int64_t dts; int VAR_3 = 0; unsigned VAR_4, VAR_5 = frag->VAR_6; int VAR_6, VAR_7, VAR_8; if (sc->pseudo_stream_id+1 != frag->stsd_id)
return 0; if (!st->nb_index_entries)
return -1; get_byte(VAR_1); VAR_6 = get_be24(VAR_1); VAR_4 = get_be32(VAR_1); dprintf(VAR_0->fc, "VAR_6 0x%x VAR_4 %d\n", VAR_6, VAR_4); if (VAR_6 & 0x001) VAR_3        = get_be32(VAR_1); if (VAR_6 & 0x004) VAR_5 = get_be32(VAR_1); if (VAR_6 & 0x800) { if ((uint64_t)VAR_4+sc->ctts_count >= UINT_MAX/sizeof(*sc->ctts_data))
return -1; sc->ctts_data = av_realloc(sc->ctts_data,
(VAR_4+sc->ctts_count)*sizeof(*sc->ctts_data)); if (!sc->ctts_data)
return AVERROR(ENOMEM); } dts = st->duration; offset = frag->base_data_offset + VAR_3; VAR_7 = 0; dprintf(VAR_0->fc, "first sample VAR_6 0x%x\n", VAR_5); for (VAR_8 = 0; VAR_8 < VAR_4; VAR_8++) { unsigned VAR_9 = frag->size; int VAR_10 = VAR_8 ? frag->VAR_6 : VAR_5; unsigned VAR_11 = frag->duration; int VAR_12; if (VAR_6 & 0x100) VAR_11 = get_be32(VAR_1); if (VAR_6 & 0x200) VAR_9     = get_be32(VAR_1); if (VAR_6 & 0x400) VAR_10    = get_be32(VAR_1); if (VAR_6 & 0x800) { sc->ctts_data[sc->ctts_count].count = 1; sc->ctts_data[sc->ctts_count].duration = get_be32(VAR_1); sc->ctts_count++; } if ((VAR_12 = st->codec->codec_type == CODEC_TYPE_AUDIO ||
(VAR_6 & 0x004 && !VAR_8 && !VAR_10) || VAR_10 & 0x2000000))
VAR_7 = 0; av_add_index_entry(st, offset, dts, VAR_9, VAR_7,
VAR_12 ? AVINDEX_KEYFRAME : 0); dprintf(VAR_0->fc, "AVIndex stream %d, sample %d, offset %"PRIx64", dts %"PRId64", "
"size %d, VAR_7 %d, VAR_12 %d\n", st->index, sc->sample_count+VAR_8,
offset, dts, VAR_9, VAR_7, VAR_12); VAR_7++; assert(VAR_11 % sc->time_rate == 0); dts += VAR_11 / sc->time_rate; offset += VAR_9; } frag->moof_offset = offset; sc->sample_count = st->nb_index_entries; st->duration = dts; return 0; }