-+ if (pkt->dts != AV_NOPTS_VALUE) {
-+ int i;
-+ for(i=0; i<s->nb_streams; i++) {
-+ AVStream *st2 = s->streams[i];
-+ MpegTSWriteStream *ts_st2 = st2->priv_data;
-+ if ( ts_st2->payload_size
-+ && (ts_st2->payload_dts == AV_NOPTS_VALUE || dts - ts_st2->payload_dts > ts->delay)) {
-+ mpegts_write_pes(s, st2, ts_st2->payload, ts_st2->payload_size,
-+ ts_st2->payload_pts, ts_st2->payload_dts,
-+ ts_st2->payload_flags & AV_PKT_FLAG_KEY, stream_id);
-+ ts_st2->payload_size = 0;
-+ }
-+ }
-+ }
-+
- if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
- (dts != AV_NOPTS_VALUE && ts_st->payload_dts != AV_NOPTS_VALUE &&
-- dts - ts_st->payload_dts >= max_audio_delay) ||
-+ av_compare_ts(dts - ts_st->payload_dts, st->time_base,
-+ s->max_delay, AV_TIME_BASE_Q) >= 0) ||
- ts_st->opus_queued_samples + opus_samples >= 5760 /* 120ms */)) {
- mpegts_write_pes(s, st, ts_st->payload, ts_st->payload_size,
- ts_st->payload_pts, ts_st->payload_dts,
-@@ -1881,7 +1758,6 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
-
- static void mpegts_write_flush(AVFormatContext *s)
- {
-- MpegTSWrite *ts = s->priv_data;
- int i;
-
- /* flush current packets */
-@@ -1896,12 +1772,6 @@ static void mpegts_write_flush(AVFormatContext *s)
- ts_st->opus_queued_samples = 0;
- }
- }
--
-- if (ts->m2ts_mode) {
-- int packets = (avio_tell(s->pb) / (TS_PACKET_SIZE + 4)) % 32;
-- while (packets++ < 32)
-- mpegts_insert_null_packet(s);
-- }
- }
-
- static int mpegts_write_packet(AVFormatContext *s, AVPacket *pkt)
-@@ -1969,62 +1839,104 @@ static int mpegts_check_bitstream(struct AVFormatContext *s, const AVPacket *pkt
- return ret;
- }
-
--#define OFFSET(x) offsetof(MpegTSWrite, x)
--#define ENC AV_OPT_FLAG_ENCODING_PARAM
- static const AVOption options[] = {
- { "mpegts_transport_stream_id", "Set transport_stream_id field.",
-- OFFSET(transport_stream_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
-+ offsetof(MpegTSWrite, transport_stream_id), AV_OPT_TYPE_INT,
-+ { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
- { "mpegts_original_network_id", "Set original_network_id field.",
-- OFFSET(original_network_id), AV_OPT_TYPE_INT, { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, ENC },
-+ offsetof(MpegTSWrite, original_network_id), AV_OPT_TYPE_INT,
-+ { .i64 = DVB_PRIVATE_NETWORK_START }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
- { "mpegts_service_id", "Set service_id field.",
-- OFFSET(service_id), AV_OPT_TYPE_INT, { .i64 = 0x0001 }, 0x0001, 0xffff, ENC },
-+ offsetof(MpegTSWrite, service_id), AV_OPT_TYPE_INT,
-+ { .i64 = 0x0001 }, 0x0001, 0xffff, AV_OPT_FLAG_ENCODING_PARAM },
- { "mpegts_service_type", "Set service_type field.",
-- OFFSET(service_type), AV_OPT_TYPE_INT, { .i64 = 0x01 }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ offsetof(MpegTSWrite, service_type), AV_OPT_TYPE_INT,
-+ { .i64 = 0x01 }, 0x01, 0xff, AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "digital_tv", "Digital Television.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_TV }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "digital_radio", "Digital Radio.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_DIGITAL_RADIO }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "teletext", "Teletext.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_TELETEXT }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "advanced_codec_digital_radio", "Advanced Codec Digital Radio.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_RADIO }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "mpeg2_digital_hdtv", "MPEG2 Digital HDTV.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_MPEG2_DIGITAL_HDTV }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "advanced_codec_digital_sdtv", "Advanced Codec Digital SDTV.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_SDTV }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "advanced_codec_digital_hdtv", "Advanced Codec Digital HDTV.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_ADVANCED_CODEC_DIGITAL_HDTV }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },
- { "hevc_digital_hdtv", "HEVC Digital Television Service.",
-- 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
-+ 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff,
-+ AV_OPT_FLAG_ENCODING_PARAM, "mpegts_service_type" },