static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov,

                              MOVTrack *track, AVStream *st)

{

    int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,

                                      track->timescale, AV_ROUND_UP);

    int version = duration < INT32_MAX ? 0 : 1;

    int flags   = MOV_TKHD_FLAG_IN_MOVIE;

    int rotation = 0;

    int group   = 0;



    uint32_t *display_matrix = NULL;

    int      display_matrix_size, i;



    if (st) {

        if (mov->per_stream_grouping)

            group = st->index;

        else

            group = st->codecpar->codec_type;



        display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,

                                                            &display_matrix_size);

        if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))

            display_matrix = NULL;

    }



    if (track->flags & MOV_TRACK_ENABLED)

        flags |= MOV_TKHD_FLAG_ENABLED;



    if (track->mode == MODE_ISM)

        version = 1;



    (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); /* size */

    ffio_wfourcc(pb, "tkhd");

    avio_w8(pb, version);

    avio_wb24(pb, flags);

    if (version == 1) {

        avio_wb64(pb, track->time);

        avio_wb64(pb, track->time);

    } else {

        avio_wb32(pb, track->time); /* creation time */

        avio_wb32(pb, track->time); /* modification time */

    }

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

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

    if (!track->entry && mov->mode == MODE_ISM)

        (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);

    else if (!track->entry)

        (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);

    else

        (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);



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

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

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

    avio_wb16(pb, group); /* alternate group) */

    /* Volume, only for audio */

    if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)

        avio_wb16(pb, 0x0100);

    else

        avio_wb16(pb, 0);

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



    /* Matrix structure */


    if (st && st->metadata) {

        AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);

        rotation = (rot && rot->value) ? atoi(rot->value) : 0;

    }


    if (display_matrix) {

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

            avio_wb32(pb, display_matrix[i]);


    } else if (rotation == 90) {

        write_matrix(pb,  0,  1, -1,  0, track->par->height, 0);

    } else if (rotation == 180) {

        write_matrix(pb, -1,  0,  0, -1, track->par->width, track->par->height);

    } else if (rotation == 270) {

        write_matrix(pb,  0, -1,  1,  0, 0, track->par->width);


    } else {

        write_matrix(pb,  1,  0,  0,  1, 0, 0);

    }

    /* Track width and height, for visual only */

    if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||

               track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {

        int64_t track_width_1616;

        if (track->mode == MODE_MOV) {

            track_width_1616 = track->par->width * 0x10000ULL;

        } else {

            track_width_1616 = av_rescale(st->sample_aspect_ratio.num,

                                                  track->par->width * 0x10000LL,

                                                  st->sample_aspect_ratio.den);

            if (!track_width_1616 ||

                track->height != track->par->height ||

                track_width_1616 > UINT32_MAX)

                track_width_1616 = track->par->width * 0x10000ULL;

        }

        if (track_width_1616 > UINT32_MAX) {

            av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");

            track_width_1616 = 0;

        }

        avio_wb32(pb, track_width_1616);

        if (track->height > 0xFFFF) {

            av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");

            avio_wb32(pb, 0);

        } else

            avio_wb32(pb, track->height * 0x10000U);

    } else {

        avio_wb32(pb, 0);

        avio_wb32(pb, 0);

    }

    return 0x5c;

}
static int mov_write_tkhd_tag(AVIOContext *pb, MOVMuxContext *mov,

                              MOVTrack *track, AVStream *st)

{

    int64_t duration = av_rescale_rnd(track->track_duration, MOV_TIMESCALE,

                                      track->timescale, AV_ROUND_UP);

    int version = duration < INT32_MAX ? 0 : 1;

    int flags   = MOV_TKHD_FLAG_IN_MOVIE;

    int rotation = 0;

    int group   = 0;



    uint32_t *display_matrix = NULL;

    int      display_matrix_size, i;



    if (st) {

        if (mov->per_stream_grouping)

            group = st->index;

        else

            group = st->codecpar->codec_type;



        display_matrix = (uint32_t*)av_stream_get_side_data(st, AV_PKT_DATA_DISPLAYMATRIX,

                                                            &display_matrix_size);

        if (display_matrix && display_matrix_size < 9 * sizeof(*display_matrix))

            display_matrix = NULL;

    }



    if (track->flags & MOV_TRACK_ENABLED)

        flags |= MOV_TKHD_FLAG_ENABLED;



    if (track->mode == MODE_ISM)

        version = 1;



    (version == 1) ? avio_wb32(pb, 104) : avio_wb32(pb, 92); 

    ffio_wfourcc(pb, "tkhd");

    avio_w8(pb, version);

    avio_wb24(pb, flags);

    if (version == 1) {

        avio_wb64(pb, track->time);

        avio_wb64(pb, track->time);

    } else {

        avio_wb32(pb, track->time); 

        avio_wb32(pb, track->time); 

    }

    avio_wb32(pb, track->track_id); 

    avio_wb32(pb, 0); 

    if (!track->entry && mov->mode == MODE_ISM)

        (version == 1) ? avio_wb64(pb, UINT64_C(0xffffffffffffffff)) : avio_wb32(pb, 0xffffffff);

    else if (!track->entry)

        (version == 1) ? avio_wb64(pb, 0) : avio_wb32(pb, 0);

    else

        (version == 1) ? avio_wb64(pb, duration) : avio_wb32(pb, duration);



    avio_wb32(pb, 0); 

    avio_wb32(pb, 0); 

    avio_wb16(pb, 0); 

    avio_wb16(pb, group); 

    

    if (track->par->codec_type == AVMEDIA_TYPE_AUDIO)

        avio_wb16(pb, 0x0100);

    else

        avio_wb16(pb, 0);

    avio_wb16(pb, 0); 



    


    if (st && st->metadata) {

        AVDictionaryEntry *rot = av_dict_get(st->metadata, "rotate", NULL, 0);

        rotation = (rot && rot->value) ? atoi(rot->value) : 0;

    }


    if (display_matrix) {

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

            avio_wb32(pb, display_matrix[i]);


    } else if (rotation == 90) {

        write_matrix(pb,  0,  1, -1,  0, track->par->height, 0);

    } else if (rotation == 180) {

        write_matrix(pb, -1,  0,  0, -1, track->par->width, track->par->height);

    } else if (rotation == 270) {

        write_matrix(pb,  0, -1,  1,  0, 0, track->par->width);


    } else {

        write_matrix(pb,  1,  0,  0,  1, 0, 0);

    }

    

    if (st && (track->par->codec_type == AVMEDIA_TYPE_VIDEO ||

               track->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {

        int64_t track_width_1616;

        if (track->mode == MODE_MOV) {

            track_width_1616 = track->par->width * 0x10000ULL;

        } else {

            track_width_1616 = av_rescale(st->sample_aspect_ratio.num,

                                                  track->par->width * 0x10000LL,

                                                  st->sample_aspect_ratio.den);

            if (!track_width_1616 ||

                track->height != track->par->height ||

                track_width_1616 > UINT32_MAX)

                track_width_1616 = track->par->width * 0x10000ULL;

        }

        if (track_width_1616 > UINT32_MAX) {

            av_log(mov->fc, AV_LOG_WARNING, "track width is too large\n");

            track_width_1616 = 0;

        }

        avio_wb32(pb, track_width_1616);

        if (track->height > 0xFFFF) {

            av_log(mov->fc, AV_LOG_WARNING, "track height is too large\n");

            avio_wb32(pb, 0);

        } else

            avio_wb32(pb, track->height * 0x10000U);

    } else {

        avio_wb32(pb, 0);

        avio_wb32(pb, 0);

    }

    return 0x5c;

}
static int FUNC_0(AVIOContext *VAR_0, MOVMuxContext *VAR_1,

                              MOVTrack *VAR_2, AVStream *VAR_3)

{

    int64_t duration = av_rescale_rnd(VAR_2->track_duration, MOV_TIMESCALE,

                                      VAR_2->timescale, AV_ROUND_UP);

    int VAR_4 = duration < INT32_MAX ? 0 : 1;

    int VAR_5   = MOV_TKHD_FLAG_IN_MOVIE;

    int VAR_6 = 0;

    int VAR_7   = 0;



    uint32_t *display_matrix = NULL;

    int      VAR_8, VAR_9;



    if (VAR_3) {

        if (VAR_1->per_stream_grouping)

            VAR_7 = VAR_3->index;

        else

            VAR_7 = VAR_3->codecpar->codec_type;



        display_matrix = (uint32_t*)av_stream_get_side_data(VAR_3, AV_PKT_DATA_DISPLAYMATRIX,

                                                            &VAR_8);

        if (display_matrix && VAR_8 < 9 * sizeof(*display_matrix))

            display_matrix = NULL;

    }



    if (VAR_2->VAR_5 & MOV_TRACK_ENABLED)

        VAR_5 |= MOV_TKHD_FLAG_ENABLED;



    if (VAR_2->mode == MODE_ISM)

        VAR_4 = 1;



    (VAR_4 == 1) ? avio_wb32(VAR_0, 104) : avio_wb32(VAR_0, 92); 

    ffio_wfourcc(VAR_0, "tkhd");

    avio_w8(VAR_0, VAR_4);

    avio_wb24(VAR_0, VAR_5);

    if (VAR_4 == 1) {

        avio_wb64(VAR_0, VAR_2->time);

        avio_wb64(VAR_0, VAR_2->time);

    } else {

        avio_wb32(VAR_0, VAR_2->time); 

        avio_wb32(VAR_0, VAR_2->time); 

    }

    avio_wb32(VAR_0, VAR_2->track_id); 

    avio_wb32(VAR_0, 0); 

    if (!VAR_2->entry && VAR_1->mode == MODE_ISM)

        (VAR_4 == 1) ? avio_wb64(VAR_0, UINT64_C(0xffffffffffffffff)) : avio_wb32(VAR_0, 0xffffffff);

    else if (!VAR_2->entry)

        (VAR_4 == 1) ? avio_wb64(VAR_0, 0) : avio_wb32(VAR_0, 0);

    else

        (VAR_4 == 1) ? avio_wb64(VAR_0, duration) : avio_wb32(VAR_0, duration);



    avio_wb32(VAR_0, 0); 

    avio_wb32(VAR_0, 0); 

    avio_wb16(VAR_0, 0); 

    avio_wb16(VAR_0, VAR_7); 

    

    if (VAR_2->par->codec_type == AVMEDIA_TYPE_AUDIO)

        avio_wb16(VAR_0, 0x0100);

    else

        avio_wb16(VAR_0, 0);

    avio_wb16(VAR_0, 0); 



    


    if (VAR_3 && VAR_3->metadata) {

        AVDictionaryEntry *rot = av_dict_get(VAR_3->metadata, "rotate", NULL, 0);

        VAR_6 = (rot && rot->value) ? atoi(rot->value) : 0;

    }


    if (display_matrix) {

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

            avio_wb32(VAR_0, display_matrix[VAR_9]);


    } else if (VAR_6 == 90) {

        write_matrix(VAR_0,  0,  1, -1,  0, VAR_2->par->height, 0);

    } else if (VAR_6 == 180) {

        write_matrix(VAR_0, -1,  0,  0, -1, VAR_2->par->width, VAR_2->par->height);

    } else if (VAR_6 == 270) {

        write_matrix(VAR_0,  0, -1,  1,  0, 0, VAR_2->par->width);


    } else {

        write_matrix(VAR_0,  1,  0,  0,  1, 0, 0);

    }

    

    if (VAR_3 && (VAR_2->par->codec_type == AVMEDIA_TYPE_VIDEO ||

               VAR_2->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) {

        int64_t track_width_1616;

        if (VAR_2->mode == MODE_MOV) {

            track_width_1616 = VAR_2->par->width * 0x10000ULL;

        } else {

            track_width_1616 = av_rescale(VAR_3->sample_aspect_ratio.num,

                                                  VAR_2->par->width * 0x10000LL,

                                                  VAR_3->sample_aspect_ratio.den);

            if (!track_width_1616 ||

                VAR_2->height != VAR_2->par->height ||

                track_width_1616 > UINT32_MAX)

                track_width_1616 = VAR_2->par->width * 0x10000ULL;

        }

        if (track_width_1616 > UINT32_MAX) {

            av_log(VAR_1->fc, AV_LOG_WARNING, "VAR_2 width is too large\n");

            track_width_1616 = 0;

        }

        avio_wb32(VAR_0, track_width_1616);

        if (VAR_2->height > 0xFFFF) {

            av_log(VAR_1->fc, AV_LOG_WARNING, "VAR_2 height is too large\n");

            avio_wb32(VAR_0, 0);

        } else

            avio_wb32(VAR_0, VAR_2->height * 0x10000U);

    } else {

        avio_wb32(VAR_0, 0);

        avio_wb32(VAR_0, 0);

    }

    return 0x5c;

}
static int FUNC_0(AVIOContext *VAR_0, MOVMuxContext *VAR_1,
MOVTrack *VAR_2, AVStream *VAR_3)
{ int64_t duration = av_rescale_rnd(VAR_2->track_duration, MOV_TIMESCALE,
VAR_2->timescale, AV_ROUND_UP); int VAR_4 = duration < INT32_MAX ? 0 : 1; int VAR_5   = MOV_TKHD_FLAG_IN_MOVIE; int VAR_6 = 0; int VAR_7   = 0; uint32_t *display_matrix = NULL; int      VAR_8, VAR_9; if (VAR_3) { if (VAR_1->per_stream_grouping)
VAR_7 = VAR_3->index; else
VAR_7 = VAR_3->codecpar->codec_type; display_matrix = (uint32_t*)av_stream_get_side_data(VAR_3, AV_PKT_DATA_DISPLAYMATRIX,
&VAR_8); if (display_matrix && VAR_8 < 9 * sizeof(*display_matrix))
display_matrix = NULL; } if (VAR_2->VAR_5 & MOV_TRACK_ENABLED)
VAR_5 |= MOV_TKHD_FLAG_ENABLED; if (VAR_2->mode == MODE_ISM)
VAR_4 = 1; (VAR_4 == 1) ? avio_wb32(VAR_0, 104) : avio_wb32(VAR_0, 92); ffio_wfourcc(VAR_0, "tkhd"); avio_w8(VAR_0, VAR_4); avio_wb24(VAR_0, VAR_5); if (VAR_4 == 1) { avio_wb64(VAR_0, VAR_2->time); avio_wb64(VAR_0, VAR_2->time); } else { avio_wb32(VAR_0, VAR_2->time); avio_wb32(VAR_0, VAR_2->time); } avio_wb32(VAR_0, VAR_2->track_id); avio_wb32(VAR_0, 0); if (!VAR_2->entry && VAR_1->mode == MODE_ISM)
(VAR_4 == 1) ? avio_wb64(VAR_0, UINT64_C(0xffffffffffffffff)) : avio_wb32(VAR_0, 0xffffffff); else if (!VAR_2->entry)
(VAR_4 == 1) ? avio_wb64(VAR_0, 0) : avio_wb32(VAR_0, 0); else
(VAR_4 == 1) ? avio_wb64(VAR_0, duration) : avio_wb32(VAR_0, duration); avio_wb32(VAR_0, 0); avio_wb32(VAR_0, 0); avio_wb16(VAR_0, 0); avio_wb16(VAR_0, VAR_7); if (VAR_2->par->codec_type == AVMEDIA_TYPE_AUDIO)
avio_wb16(VAR_0, 0x0100); else
avio_wb16(VAR_0, 0); avio_wb16(VAR_0, 0); if (VAR_3 && VAR_3->metadata) { AVDictionaryEntry *rot = av_dict_get(VAR_3->metadata, "rotate", NULL, 0); VAR_6 = (rot && rot->value) ? atoi(rot->value) : 0; } if (display_matrix) { for (VAR_9 = 0; VAR_9 < 9; VAR_9++) avio_wb32(VAR_0, display_matrix[VAR_9]); } else if (VAR_6 == 90) { write_matrix(VAR_0,  0,  1, -1,  0, VAR_2->par->height, 0); } else if (VAR_6 == 180) { write_matrix(VAR_0, -1,  0,  0, -1, VAR_2->par->width, VAR_2->par->height); } else if (VAR_6 == 270) { write_matrix(VAR_0,  0, -1,  1,  0, 0, VAR_2->par->width); } else { write_matrix(VAR_0,  1,  0,  0,  1, 0, 0); } if (VAR_3 && (VAR_2->par->codec_type == AVMEDIA_TYPE_VIDEO ||
VAR_2->par->codec_type == AVMEDIA_TYPE_SUBTITLE)) { int64_t track_width_1616; if (VAR_2->mode == MODE_MOV) { track_width_1616 = VAR_2->par->width * 0x10000ULL; } else { track_width_1616 = av_rescale(VAR_3->sample_aspect_ratio.num,
VAR_2->par->width * 0x10000LL,
VAR_3->sample_aspect_ratio.den); if (!track_width_1616 ||
VAR_2->height != VAR_2->par->height ||
track_width_1616 > UINT32_MAX)
track_width_1616 = VAR_2->par->width * 0x10000ULL; } if (track_width_1616 > UINT32_MAX) { av_log(VAR_1->fc, AV_LOG_WARNING, "VAR_2 width is too large\n"); track_width_1616 = 0; } avio_wb32(VAR_0, track_width_1616); if (VAR_2->height > 0xFFFF) { av_log(VAR_1->fc, AV_LOG_WARNING, "VAR_2 height is too large\n"); avio_wb32(VAR_0, 0); } else avio_wb32(VAR_0, VAR_2->height * 0x10000U); } else { avio_wb32(VAR_0, 0); avio_wb32(VAR_0, 0); } return 0x5c; }