static int mov_write_sidx_tag(AVIOContext *pb,

                              MOVTrack *track, int ref_size, int total_sidx_size)

{

    int64_t pos = avio_tell(pb), offset_pos, end_pos;

    int64_t presentation_time, duration, offset;

    int starts_with_SAP, i, entries;



    if (track->entry) {

        entries = 1;

        presentation_time = track->start_dts + track->frag_start +

                            track->cluster[0].cts;

        duration = track->end_pts -

                   (track->cluster[0].dts + track->cluster[0].cts);

        starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;



        // pts<0 should be cut away using edts

        if (presentation_time < 0) {

            duration += presentation_time;

            presentation_time = 0;

        }

    } else {

        entries = track->nb_frag_info;



        presentation_time = track->frag_info[0].time;

    }



    avio_wb32(pb, 0); /* size */

    ffio_wfourcc(pb, "sidx");

    avio_w8(pb, 1); /* version */

    avio_wb24(pb, 0);

    avio_wb32(pb, track->track_id); /* reference_ID */

    avio_wb32(pb, track->timescale); /* timescale */

    avio_wb64(pb, presentation_time); /* earliest_presentation_time */

    offset_pos = avio_tell(pb);

    avio_wb64(pb, 0); /* first_offset (offset to referenced moof) */

    avio_wb16(pb, 0); /* reserved */



    avio_wb16(pb, entries); /* reference_count */

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

        if (!track->entry) {

            if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {

               av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");

            }

            duration = track->frag_info[i].duration;

            ref_size = track->frag_info[i].size;

            starts_with_SAP = 1;

        }

        avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); /* reference_type (0 = media) | referenced_size */

        avio_wb32(pb, duration); /* subsegment_duration */

        avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); /* starts_with_SAP | SAP_type | SAP_delta_time */

    }



    end_pos = avio_tell(pb);

    offset = pos + total_sidx_size - end_pos;

    avio_seek(pb, offset_pos, SEEK_SET);

    avio_wb64(pb, offset);

    avio_seek(pb, end_pos, SEEK_SET);

    return update_size(pb, pos);

}
static int mov_write_sidx_tag(AVIOContext *pb,

                              MOVTrack *track, int ref_size, int total_sidx_size)

{

    int64_t pos = avio_tell(pb), offset_pos, end_pos;

    int64_t presentation_time, duration, offset;

    int starts_with_SAP, i, entries;



    if (track->entry) {

        entries = 1;

        presentation_time = track->start_dts + track->frag_start +

                            track->cluster[0].cts;

        duration = track->end_pts -

                   (track->cluster[0].dts + track->cluster[0].cts);

        starts_with_SAP = track->cluster[0].flags & MOV_SYNC_SAMPLE;



        

        if (presentation_time < 0) {

            duration += presentation_time;

            presentation_time = 0;

        }

    } else {

        entries = track->nb_frag_info;



        presentation_time = track->frag_info[0].time;

    }



    avio_wb32(pb, 0); 

    ffio_wfourcc(pb, "sidx");

    avio_w8(pb, 1); 

    avio_wb24(pb, 0);

    avio_wb32(pb, track->track_id); 

    avio_wb32(pb, track->timescale); 

    avio_wb64(pb, presentation_time); 

    offset_pos = avio_tell(pb);

    avio_wb64(pb, 0); 

    avio_wb16(pb, 0); 



    avio_wb16(pb, entries); 

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

        if (!track->entry) {

            if (i > 1 && track->frag_info[i].offset != track->frag_info[i - 1].offset + track->frag_info[i - 1].size) {

               av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");

            }

            duration = track->frag_info[i].duration;

            ref_size = track->frag_info[i].size;

            starts_with_SAP = 1;

        }

        avio_wb32(pb, (0 << 31) | (ref_size & 0x7fffffff)); 

        avio_wb32(pb, duration); 

        avio_wb32(pb, (starts_with_SAP << 31) | (0 << 28) | 0); 

    }



    end_pos = avio_tell(pb);

    offset = pos + total_sidx_size - end_pos;

    avio_seek(pb, offset_pos, SEEK_SET);

    avio_wb64(pb, offset);

    avio_seek(pb, end_pos, SEEK_SET);

    return update_size(pb, pos);

}
static int FUNC_0(AVIOContext *VAR_0,

                              MOVTrack *VAR_1, int VAR_2, int VAR_3)

{

    int64_t pos = avio_tell(VAR_0), offset_pos, end_pos;

    int64_t presentation_time, duration, offset;

    int VAR_4, VAR_5, VAR_6;



    if (VAR_1->entry) {

        VAR_6 = 1;

        presentation_time = VAR_1->start_dts + VAR_1->frag_start +

                            VAR_1->cluster[0].cts;

        duration = VAR_1->end_pts -

                   (VAR_1->cluster[0].dts + VAR_1->cluster[0].cts);

        VAR_4 = VAR_1->cluster[0].flags & MOV_SYNC_SAMPLE;



        

        if (presentation_time < 0) {

            duration += presentation_time;

            presentation_time = 0;

        }

    } else {

        VAR_6 = VAR_1->nb_frag_info;



        presentation_time = VAR_1->frag_info[0].time;

    }



    avio_wb32(VAR_0, 0); 

    ffio_wfourcc(VAR_0, "sidx");

    avio_w8(VAR_0, 1); 

    avio_wb24(VAR_0, 0);

    avio_wb32(VAR_0, VAR_1->track_id); 

    avio_wb32(VAR_0, VAR_1->timescale); 

    avio_wb64(VAR_0, presentation_time); 

    offset_pos = avio_tell(VAR_0);

    avio_wb64(VAR_0, 0); 

    avio_wb16(VAR_0, 0); 



    avio_wb16(VAR_0, VAR_6); 

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

        if (!VAR_1->entry) {

            if (VAR_5 > 1 && VAR_1->frag_info[VAR_5].offset != VAR_1->frag_info[VAR_5 - 1].offset + VAR_1->frag_info[VAR_5 - 1].size) {

               av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n");

            }

            duration = VAR_1->frag_info[VAR_5].duration;

            VAR_2 = VAR_1->frag_info[VAR_5].size;

            VAR_4 = 1;

        }

        avio_wb32(VAR_0, (0 << 31) | (VAR_2 & 0x7fffffff)); 

        avio_wb32(VAR_0, duration); 

        avio_wb32(VAR_0, (VAR_4 << 31) | (0 << 28) | 0); 

    }



    end_pos = avio_tell(VAR_0);

    offset = pos + VAR_3 - end_pos;

    avio_seek(VAR_0, offset_pos, SEEK_SET);

    avio_wb64(VAR_0, offset);

    avio_seek(VAR_0, end_pos, SEEK_SET);

    return update_size(VAR_0, pos);

}
static int FUNC_0(AVIOContext *VAR_0,
MOVTrack *VAR_1, int VAR_2, int VAR_3)
{ int64_t pos = avio_tell(VAR_0), offset_pos, end_pos; int64_t presentation_time, duration, offset; int VAR_4, VAR_5, VAR_6; if (VAR_1->entry) { VAR_6 = 1; presentation_time = VAR_1->start_dts + VAR_1->frag_start +
VAR_1->cluster[0].cts; duration = VAR_1->end_pts -
(VAR_1->cluster[0].dts + VAR_1->cluster[0].cts); VAR_4 = VAR_1->cluster[0].flags & MOV_SYNC_SAMPLE; if (presentation_time < 0) { duration += presentation_time; presentation_time = 0; } } else { VAR_6 = VAR_1->nb_frag_info; presentation_time = VAR_1->frag_info[0].time; } avio_wb32(VAR_0, 0); ffio_wfourcc(VAR_0, "sidx"); avio_w8(VAR_0, 1); avio_wb24(VAR_0, 0); avio_wb32(VAR_0, VAR_1->track_id); avio_wb32(VAR_0, VAR_1->timescale); avio_wb64(VAR_0, presentation_time); offset_pos = avio_tell(VAR_0); avio_wb64(VAR_0, 0); avio_wb16(VAR_0, 0); avio_wb16(VAR_0, VAR_6); for (VAR_5 = 0; VAR_5 < VAR_6; VAR_5++) { if (!VAR_1->entry) { if (VAR_5 > 1 && VAR_1->frag_info[VAR_5].offset != VAR_1->frag_info[VAR_5 - 1].offset + VAR_1->frag_info[VAR_5 - 1].size) { av_log(NULL, AV_LOG_ERROR, "Non-consecutive fragments, writing incorrect sidx\n"); } duration = VAR_1->frag_info[VAR_5].duration; VAR_2 = VAR_1->frag_info[VAR_5].size; VAR_4 = 1; } avio_wb32(VAR_0, (0 << 31) | (VAR_2 & 0x7fffffff)); avio_wb32(VAR_0, duration); avio_wb32(VAR_0, (VAR_4 << 31) | (0 << 28) | 0); } end_pos = avio_tell(VAR_0); offset = pos + VAR_3 - end_pos; avio_seek(VAR_0, offset_pos, SEEK_SET); avio_wb64(VAR_0, offset); avio_seek(VAR_0, end_pos, SEEK_SET); return update_size(VAR_0, pos); }