static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)

{

    int64_t offset = avio_tell(pb) + atom.size, pts, timestamp;

    uint8_t version;

    unsigned i, j, track_id, item_count;

    AVStream *st = NULL;

    AVStream *ref_st = NULL;

    MOVStreamContext *sc, *ref_sc = NULL;

    AVRational timescale;



    version = avio_r8(pb);

    if (version > 1) {

        avpriv_request_sample(c->fc, "sidx version %u", version);

        return 0;

    }



    avio_rb24(pb); // flags



    track_id = avio_rb32(pb); // Reference ID

    for (i = 0; i < c->fc->nb_streams; i++) {

        if (c->fc->streams[i]->id == track_id) {

            st = c->fc->streams[i];

            break;

        }

    }

    if (!st) {

        av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);

        return 0;

    }



    sc = st->priv_data;



    timescale = av_make_q(1, avio_rb32(pb));



    if (timescale.den <= 0) {

        av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);

        return AVERROR_INVALIDDATA;

    }



    if (version == 0) {

        pts = avio_rb32(pb);

        offset += avio_rb32(pb);

    } else {

        pts = avio_rb64(pb);

        offset += avio_rb64(pb);

    }



    avio_rb16(pb); // reserved



    item_count = avio_rb16(pb);



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

        int index;

        MOVFragmentStreamInfo * frag_stream_info;

        uint32_t size = avio_rb32(pb);

        uint32_t duration = avio_rb32(pb);

        if (size & 0x80000000) {

            avpriv_request_sample(c->fc, "sidx reference_type 1");

            return AVERROR_PATCHWELCOME;

        }

        avio_rb32(pb); // sap_flags

        timestamp = av_rescale_q(pts, st->time_base, timescale);



        index = update_frag_index(c, offset);

        frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);

        if (frag_stream_info)

            frag_stream_info->sidx_pts = timestamp;



        offset += size;

        pts += duration;

    }



    st->duration = sc->track_end = pts;



    sc->has_sidx = 1;



    if (offset == avio_size(pb)) {

        // Find first entry in fragment index that came from an sidx.

        // This will pretty much always be the first entry.

        for (i = 0; i < c->frag_index.nb_items; i++) {

            MOVFragmentIndexItem * item = &c->frag_index.item[i];

            for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {

                MOVFragmentStreamInfo * si;

                si = &item->stream_info[j];

                if (si->sidx_pts != AV_NOPTS_VALUE) {

                    ref_st = c->fc->streams[i];

                    ref_sc = ref_st->priv_data;

                    break;

                }

            }

        }

        for (i = 0; i < c->fc->nb_streams; i++) {

            st = c->fc->streams[i];

            sc = st->priv_data;

            if (!sc->has_sidx) {

                st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);

            }

        }



        c->frag_index.complete = 1;

    }



    return 0;

}

static int mov_read_sidx(MOVContext *c, AVIOContext *pb, MOVAtom atom)

{

    int64_t offset = avio_tell(pb) + atom.size, pts, timestamp;

    uint8_t version;

    unsigned i, j, track_id, item_count;

    AVStream *st = NULL;

    AVStream *ref_st = NULL;

    MOVStreamContext *sc, *ref_sc = NULL;

    AVRational timescale;



    version = avio_r8(pb);

    if (version > 1) {

        avpriv_request_sample(c->fc, "sidx version %u", version);

        return 0;

    }



    avio_rb24(pb); 



    track_id = avio_rb32(pb); 

    for (i = 0; i < c->fc->nb_streams; i++) {

        if (c->fc->streams[i]->id == track_id) {

            st = c->fc->streams[i];

            break;

        }

    }

    if (!st) {

        av_log(c->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", track_id);

        return 0;

    }



    sc = st->priv_data;



    timescale = av_make_q(1, avio_rb32(pb));



    if (timescale.den <= 0) {

        av_log(c->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);

        return AVERROR_INVALIDDATA;

    }



    if (version == 0) {

        pts = avio_rb32(pb);

        offset += avio_rb32(pb);

    } else {

        pts = avio_rb64(pb);

        offset += avio_rb64(pb);

    }



    avio_rb16(pb); 



    item_count = avio_rb16(pb);



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

        int index;

        MOVFragmentStreamInfo * frag_stream_info;

        uint32_t size = avio_rb32(pb);

        uint32_t duration = avio_rb32(pb);

        if (size & 0x80000000) {

            avpriv_request_sample(c->fc, "sidx reference_type 1");

            return AVERROR_PATCHWELCOME;

        }

        avio_rb32(pb); 

        timestamp = av_rescale_q(pts, st->time_base, timescale);



        index = update_frag_index(c, offset);

        frag_stream_info = get_frag_stream_info(&c->frag_index, index, track_id);

        if (frag_stream_info)

            frag_stream_info->sidx_pts = timestamp;



        offset += size;

        pts += duration;

    }



    st->duration = sc->track_end = pts;



    sc->has_sidx = 1;



    if (offset == avio_size(pb)) {

        

        

        for (i = 0; i < c->frag_index.nb_items; i++) {

            MOVFragmentIndexItem * item = &c->frag_index.item[i];

            for (j = 0; ref_st == NULL && j < item->nb_stream_info; j++) {

                MOVFragmentStreamInfo * si;

                si = &item->stream_info[j];

                if (si->sidx_pts != AV_NOPTS_VALUE) {

                    ref_st = c->fc->streams[i];

                    ref_sc = ref_st->priv_data;

                    break;

                }

            }

        }

        for (i = 0; i < c->fc->nb_streams; i++) {

            st = c->fc->streams[i];

            sc = st->priv_data;

            if (!sc->has_sidx) {

                st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);

            }

        }



        c->frag_index.complete = 1;

    }



    return 0;

}

                    ref_st = c->fc->streams[i];
static int FUNC_0(MOVContext *VAR_0, AVIOContext *VAR_1, MOVAtom VAR_2)

{

    int64_t offset = avio_tell(VAR_1) + VAR_2.size, pts, timestamp;

    uint8_t version;

    unsigned VAR_3, VAR_4, VAR_5, VAR_6;

    AVStream *st = NULL;

    AVStream *ref_st = NULL;

    MOVStreamContext *sc, *ref_sc = NULL;

    AVRational timescale;



    version = avio_r8(VAR_1);

    if (version > 1) {

        avpriv_request_sample(VAR_0->fc, "sidx version %u", version);

        return 0;

    }



    avio_rb24(VAR_1); 



    VAR_5 = avio_rb32(VAR_1); 

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

        if (VAR_0->fc->streams[VAR_3]->id == VAR_5) {

            st = VAR_0->fc->streams[VAR_3];

            break;

        }

    }

    if (!st) {

        av_log(VAR_0->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", VAR_5);

        return 0;

    }



    sc = st->priv_data;



    timescale = av_make_q(1, avio_rb32(VAR_1));



    if (timescale.den <= 0) {

        av_log(VAR_0->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den);

        return AVERROR_INVALIDDATA;

    }



    if (version == 0) {

        pts = avio_rb32(VAR_1);

        offset += avio_rb32(VAR_1);

    } else {

        pts = avio_rb64(VAR_1);

        offset += avio_rb64(VAR_1);

    }



    avio_rb16(VAR_1); 



    VAR_6 = avio_rb16(VAR_1);



    for (VAR_3 = 0; VAR_3 < VAR_6; VAR_3++) {

        int VAR_7;

        MOVFragmentStreamInfo * frag_stream_info;

        uint32_t size = avio_rb32(VAR_1);

        uint32_t duration = avio_rb32(VAR_1);

        if (size & 0x80000000) {

            avpriv_request_sample(VAR_0->fc, "sidx reference_type 1");

            return AVERROR_PATCHWELCOME;

        }

        avio_rb32(VAR_1); 

        timestamp = av_rescale_q(pts, st->time_base, timescale);



        VAR_7 = update_frag_index(VAR_0, offset);

        frag_stream_info = get_frag_stream_info(&VAR_0->frag_index, VAR_7, VAR_5);

        if (frag_stream_info)

            frag_stream_info->sidx_pts = timestamp;



        offset += size;

        pts += duration;

    }



    st->duration = sc->track_end = pts;



    sc->has_sidx = 1;



    if (offset == avio_size(VAR_1)) {

        

        

        for (VAR_3 = 0; VAR_3 < VAR_0->frag_index.nb_items; VAR_3++) {

            MOVFragmentIndexItem * item = &VAR_0->frag_index.item[VAR_3];

            for (VAR_4 = 0; ref_st == NULL && VAR_4 < item->nb_stream_info; VAR_4++) {

                MOVFragmentStreamInfo * si;

                si = &item->stream_info[VAR_4];

                if (si->sidx_pts != AV_NOPTS_VALUE) {

                    ref_st = VAR_0->fc->streams[VAR_3];

                    ref_sc = ref_st->priv_data;

                    break;

                }

            }

        }

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

            st = VAR_0->fc->streams[VAR_3];

            sc = st->priv_data;

            if (!sc->has_sidx) {

                st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale);

            }

        }



        VAR_0->frag_index.complete = 1;

    }



    return 0;

}

static int FUNC_0(MOVContext *VAR_0, AVIOContext *VAR_1, MOVAtom VAR_2)
{ int64_t offset = avio_tell(VAR_1) + VAR_2.size, pts, timestamp; uint8_t version; unsigned VAR_3, VAR_4, VAR_5, VAR_6; AVStream *st = NULL; AVStream *ref_st = NULL; MOVStreamContext *sc, *ref_sc = NULL; AVRational timescale; version = avio_r8(VAR_1); if (version > 1) { avpriv_request_sample(VAR_0->fc, "sidx version %u", version); return 0; } avio_rb24(VAR_1); VAR_5 = avio_rb32(VAR_1); for (VAR_3 = 0; VAR_3 < VAR_0->fc->nb_streams; VAR_3++) { if (VAR_0->fc->streams[VAR_3]->id == VAR_5) { st = VAR_0->fc->streams[VAR_3]; break; } } if (!st) { av_log(VAR_0->fc, AV_LOG_WARNING, "could not find corresponding track id %d\n", VAR_5); return 0; } sc = st->priv_data; timescale = av_make_q(1, avio_rb32(VAR_1)); if (timescale.den <= 0) { av_log(VAR_0->fc, AV_LOG_ERROR, "Invalid sidx timescale 1/%d\n", timescale.den); return AVERROR_INVALIDDATA; } if (version == 0) { pts = avio_rb32(VAR_1); offset += avio_rb32(VAR_1); } else { pts = avio_rb64(VAR_1); offset += avio_rb64(VAR_1); } avio_rb16(VAR_1); VAR_6 = avio_rb16(VAR_1); for (VAR_3 = 0; VAR_3 < VAR_6; VAR_3++) { int VAR_7; MOVFragmentStreamInfo * frag_stream_info; uint32_t size = avio_rb32(VAR_1); uint32_t duration = avio_rb32(VAR_1); if (size & 0x80000000) { avpriv_request_sample(VAR_0->fc, "sidx reference_type 1"); return AVERROR_PATCHWELCOME; } avio_rb32(VAR_1); timestamp = av_rescale_q(pts, st->time_base, timescale); VAR_7 = update_frag_index(VAR_0, offset); frag_stream_info = get_frag_stream_info(&VAR_0->frag_index, VAR_7, VAR_5); if (frag_stream_info)
frag_stream_info->sidx_pts = timestamp; offset += size; pts += duration; } st->duration = sc->track_end = pts; sc->has_sidx = 1; if (offset == avio_size(VAR_1)) { for (VAR_3 = 0; VAR_3 < VAR_0->frag_index.nb_items; VAR_3++) { MOVFragmentIndexItem * item = &VAR_0->frag_index.item[VAR_3]; for (VAR_4 = 0; ref_st == NULL && VAR_4 < item->nb_stream_info; VAR_4++) { MOVFragmentStreamInfo * si; si = &item->stream_info[VAR_4]; if (si->sidx_pts != AV_NOPTS_VALUE) { ref_st = VAR_0->fc->streams[VAR_3]; ref_sc = ref_st->priv_data; break; } } } for (VAR_3 = 0; VAR_3 < VAR_0->fc->nb_streams; VAR_3++) { st = VAR_0->fc->streams[VAR_3]; sc = st->priv_data; if (!sc->has_sidx) { st->duration = sc->track_end = av_rescale(ref_st->duration, sc->time_scale, ref_sc->time_scale); } } VAR_0->frag_index.complete = 1; } return 0; }