static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue)

{

    MatroskaMuxContext *mkv = s->priv_data;

    AVIOContext *pb         = s->pb;

    AVCodecParameters *par  = s->streams[pkt->stream_index]->codecpar;

    int keyframe            = !!(pkt->flags & AV_PKT_FLAG_KEY);

    int duration            = pkt->duration;

    int ret;

    int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;

    int64_t relative_packet_pos;

    int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;



    if (ts == AV_NOPTS_VALUE) {

        av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");

        return AVERROR(EINVAL);

    }

    ts += mkv->tracks[pkt->stream_index].ts_offset;



    if (mkv->cluster_pos != -1) {

        int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;

        if ((int16_t)cluster_time != cluster_time) {

            av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");

            mkv_start_new_cluster(s, pkt);

        }

    }



    if (mkv->cluster_pos == -1) {

        mkv->cluster_pos = avio_tell(s->pb);

        ret = start_ebml_master_crc32(s->pb, &mkv->dyn_bc, &mkv->cluster, MATROSKA_ID_CLUSTER, 0);

        if (ret < 0)

            return ret;

        put_ebml_uint(mkv->dyn_bc, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));

        mkv->cluster_pts = FFMAX(0, ts);

    }

    pb = mkv->dyn_bc;



    relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos + avio_tell(pb);



    if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {

        mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe);

        if (s->pb->seekable && (par->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) {

            ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);

            if (ret < 0) return ret;

        }

    } else {

        if (par->codec_id == AV_CODEC_ID_WEBVTT) {

            duration = mkv_write_vtt_blocks(s, pb, pkt);

        } else {

            ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,

                                                       mkv_blockgroup_size(pkt->size));



#if FF_API_CONVERGENCE_DURATION

FF_DISABLE_DEPRECATION_WARNINGS

            /* For backward compatibility, prefer convergence_duration. */

            if (pkt->convergence_duration > 0) {

                duration = pkt->convergence_duration;

            }

FF_ENABLE_DEPRECATION_WARNINGS

#endif

            /* All subtitle blocks are considered to be keyframes. */

            mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 1);

            put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);

            end_ebml_master(pb, blockgroup);

        }



        if (s->pb->seekable) {

            ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,

                                   mkv->cluster_pos, relative_packet_pos, duration);

            if (ret < 0)

                return ret;

        }

    }



    mkv->duration = FFMAX(mkv->duration, ts + duration);



    if (mkv->stream_durations)

        mkv->stream_durations[pkt->stream_index] =

            FFMAX(mkv->stream_durations[pkt->stream_index], ts + duration);



    return 0;

}

static int mkv_write_packet_internal(AVFormatContext *s, AVPacket *pkt, int add_cue)

{

    MatroskaMuxContext *mkv = s->priv_data;

    AVIOContext *pb         = s->pb;

    AVCodecParameters *par  = s->streams[pkt->stream_index]->codecpar;

    int keyframe            = !!(pkt->flags & AV_PKT_FLAG_KEY);

    int duration            = pkt->duration;

    int ret;

    int64_t ts = mkv->tracks[pkt->stream_index].write_dts ? pkt->dts : pkt->pts;

    int64_t relative_packet_pos;

    int dash_tracknum = mkv->is_dash ? mkv->dash_track_number : pkt->stream_index + 1;



    if (ts == AV_NOPTS_VALUE) {

        av_log(s, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");

        return AVERROR(EINVAL);

    }

    ts += mkv->tracks[pkt->stream_index].ts_offset;



    if (mkv->cluster_pos != -1) {

        int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[pkt->stream_index].ts_offset;

        if ((int16_t)cluster_time != cluster_time) {

            av_log(s, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");

            mkv_start_new_cluster(s, pkt);

        }

    }



    if (mkv->cluster_pos == -1) {

        mkv->cluster_pos = avio_tell(s->pb);

        ret = start_ebml_master_crc32(s->pb, &mkv->dyn_bc, &mkv->cluster, MATROSKA_ID_CLUSTER, 0);

        if (ret < 0)

            return ret;

        put_ebml_uint(mkv->dyn_bc, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));

        mkv->cluster_pts = FFMAX(0, ts);

    }

    pb = mkv->dyn_bc;



    relative_packet_pos = avio_tell(s->pb) - mkv->cluster.pos + avio_tell(pb);



    if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {

        mkv_write_block(s, pb, MATROSKA_ID_SIMPLEBLOCK, pkt, keyframe);

        if (s->pb->seekable && (par->codec_type == AVMEDIA_TYPE_VIDEO && keyframe || add_cue)) {

            ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts, mkv->cluster_pos, relative_packet_pos, -1);

            if (ret < 0) return ret;

        }

    } else {

        if (par->codec_id == AV_CODEC_ID_WEBVTT) {

            duration = mkv_write_vtt_blocks(s, pb, pkt);

        } else {

            ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,

                                                       mkv_blockgroup_size(pkt->size));



#if FF_API_CONVERGENCE_DURATION

FF_DISABLE_DEPRECATION_WARNINGS

            

            if (pkt->convergence_duration > 0) {

                duration = pkt->convergence_duration;

            }

FF_ENABLE_DEPRECATION_WARNINGS

#endif

            

            mkv_write_block(s, pb, MATROSKA_ID_BLOCK, pkt, 1);

            put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, duration);

            end_ebml_master(pb, blockgroup);

        }



        if (s->pb->seekable) {

            ret = mkv_add_cuepoint(mkv->cues, pkt->stream_index, dash_tracknum, ts,

                                   mkv->cluster_pos, relative_packet_pos, duration);

            if (ret < 0)

                return ret;

        }

    }



    mkv->duration = FFMAX(mkv->duration, ts + duration);



    if (mkv->stream_durations)

        mkv->stream_durations[pkt->stream_index] =

            FFMAX(mkv->stream_durations[pkt->stream_index], ts + duration);



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1, int VAR_2)

{

    MatroskaMuxContext *mkv = VAR_0->priv_data;

    AVIOContext *pb         = VAR_0->pb;

    AVCodecParameters *par  = VAR_0->streams[VAR_1->stream_index]->codecpar;

    int VAR_3            = !!(VAR_1->flags & AV_PKT_FLAG_KEY);

    int VAR_4            = VAR_1->VAR_4;

    int VAR_5;

    int64_t ts = mkv->tracks[VAR_1->stream_index].write_dts ? VAR_1->dts : VAR_1->pts;

    int64_t relative_packet_pos;

    int VAR_6 = mkv->is_dash ? mkv->dash_track_number : VAR_1->stream_index + 1;



    if (ts == AV_NOPTS_VALUE) {

        av_log(VAR_0, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n");

        return AVERROR(EINVAL);

    }

    ts += mkv->tracks[VAR_1->stream_index].ts_offset;



    if (mkv->cluster_pos != -1) {

        int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[VAR_1->stream_index].ts_offset;

        if ((int16_t)cluster_time != cluster_time) {

            av_log(VAR_0, AV_LOG_WARNING, "Starting new cluster due to timestamp\n");

            mkv_start_new_cluster(VAR_0, VAR_1);

        }

    }



    if (mkv->cluster_pos == -1) {

        mkv->cluster_pos = avio_tell(VAR_0->pb);

        VAR_5 = start_ebml_master_crc32(VAR_0->pb, &mkv->dyn_bc, &mkv->cluster, MATROSKA_ID_CLUSTER, 0);

        if (VAR_5 < 0)

            return VAR_5;

        put_ebml_uint(mkv->dyn_bc, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts));

        mkv->cluster_pts = FFMAX(0, ts);

    }

    pb = mkv->dyn_bc;



    relative_packet_pos = avio_tell(VAR_0->pb) - mkv->cluster.pos + avio_tell(pb);



    if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) {

        mkv_write_block(VAR_0, pb, MATROSKA_ID_SIMPLEBLOCK, VAR_1, VAR_3);

        if (VAR_0->pb->seekable && (par->codec_type == AVMEDIA_TYPE_VIDEO && VAR_3 || VAR_2)) {

            VAR_5 = mkv_add_cuepoint(mkv->cues, VAR_1->stream_index, VAR_6, ts, mkv->cluster_pos, relative_packet_pos, -1);

            if (VAR_5 < 0) return VAR_5;

        }

    } else {

        if (par->codec_id == AV_CODEC_ID_WEBVTT) {

            VAR_4 = mkv_write_vtt_blocks(VAR_0, pb, VAR_1);

        } else {

            ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,

                                                       mkv_blockgroup_size(VAR_1->size));



#if FF_API_CONVERGENCE_DURATION

FF_DISABLE_DEPRECATION_WARNINGS

            

            if (VAR_1->convergence_duration > 0) {

                VAR_4 = VAR_1->convergence_duration;

            }

FF_ENABLE_DEPRECATION_WARNINGS

#endif

            

            mkv_write_block(VAR_0, pb, MATROSKA_ID_BLOCK, VAR_1, 1);

            put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, VAR_4);

            end_ebml_master(pb, blockgroup);

        }



        if (VAR_0->pb->seekable) {

            VAR_5 = mkv_add_cuepoint(mkv->cues, VAR_1->stream_index, VAR_6, ts,

                                   mkv->cluster_pos, relative_packet_pos, VAR_4);

            if (VAR_5 < 0)

                return VAR_5;

        }

    }



    mkv->VAR_4 = FFMAX(mkv->VAR_4, ts + VAR_4);



    if (mkv->stream_durations)

        mkv->stream_durations[VAR_1->stream_index] =

            FFMAX(mkv->stream_durations[VAR_1->stream_index], ts + VAR_4);



    return 0;

}

static int FUNC_0(AVFormatContext *VAR_0, AVPacket *VAR_1, int VAR_2)
{ MatroskaMuxContext *mkv = VAR_0->priv_data; AVIOContext *pb         = VAR_0->pb; AVCodecParameters *par  = VAR_0->streams[VAR_1->stream_index]->codecpar; int VAR_3            = !!(VAR_1->flags & AV_PKT_FLAG_KEY); int VAR_4            = VAR_1->VAR_4; int VAR_5; int64_t ts = mkv->tracks[VAR_1->stream_index].write_dts ? VAR_1->dts : VAR_1->pts; int64_t relative_packet_pos; int VAR_6 = mkv->is_dash ? mkv->dash_track_number : VAR_1->stream_index + 1; if (ts == AV_NOPTS_VALUE) { av_log(VAR_0, AV_LOG_ERROR, "Can't write packet with unknown timestamp\n"); return AVERROR(EINVAL); } ts += mkv->tracks[VAR_1->stream_index].ts_offset; if (mkv->cluster_pos != -1) { int64_t cluster_time = ts - mkv->cluster_pts + mkv->tracks[VAR_1->stream_index].ts_offset; if ((int16_t)cluster_time != cluster_time) { av_log(VAR_0, AV_LOG_WARNING, "Starting new cluster due to timestamp\n"); mkv_start_new_cluster(VAR_0, VAR_1); } } if (mkv->cluster_pos == -1) { mkv->cluster_pos = avio_tell(VAR_0->pb); VAR_5 = start_ebml_master_crc32(VAR_0->pb, &mkv->dyn_bc, &mkv->cluster, MATROSKA_ID_CLUSTER, 0); if (VAR_5 < 0)
return VAR_5; put_ebml_uint(mkv->dyn_bc, MATROSKA_ID_CLUSTERTIMECODE, FFMAX(0, ts)); mkv->cluster_pts = FFMAX(0, ts); } pb = mkv->dyn_bc; relative_packet_pos = avio_tell(VAR_0->pb) - mkv->cluster.pos + avio_tell(pb); if (par->codec_type != AVMEDIA_TYPE_SUBTITLE) { mkv_write_block(VAR_0, pb, MATROSKA_ID_SIMPLEBLOCK, VAR_1, VAR_3); if (VAR_0->pb->seekable && (par->codec_type == AVMEDIA_TYPE_VIDEO && VAR_3 || VAR_2)) { VAR_5 = mkv_add_cuepoint(mkv->cues, VAR_1->stream_index, VAR_6, ts, mkv->cluster_pos, relative_packet_pos, -1); if (VAR_5 < 0) return VAR_5; } } else { if (par->codec_id == AV_CODEC_ID_WEBVTT) { VAR_4 = mkv_write_vtt_blocks(VAR_0, pb, VAR_1); } else { ebml_master blockgroup = start_ebml_master(pb, MATROSKA_ID_BLOCKGROUP,
mkv_blockgroup_size(VAR_1->size)); #if FF_API_CONVERGENCE_DURATION
FF_DISABLE_DEPRECATION_WARNINGS
if (VAR_1->convergence_duration > 0) { VAR_4 = VAR_1->convergence_duration; } FF_ENABLE_DEPRECATION_WARNINGS
#endif
mkv_write_block(VAR_0, pb, MATROSKA_ID_BLOCK, VAR_1, 1); put_ebml_uint(pb, MATROSKA_ID_BLOCKDURATION, VAR_4); end_ebml_master(pb, blockgroup); } if (VAR_0->pb->seekable) { VAR_5 = mkv_add_cuepoint(mkv->cues, VAR_1->stream_index, VAR_6, ts,
mkv->cluster_pos, relative_packet_pos, VAR_4); if (VAR_5 < 0)
return VAR_5; } } mkv->VAR_4 = FFMAX(mkv->VAR_4, ts + VAR_4); if (mkv->stream_durations)
mkv->stream_durations[VAR_1->stream_index] =
FFMAX(mkv->stream_durations[VAR_1->stream_index], ts + VAR_4); return 0; }