X-Git-Url: https://git.cinelerra-gg.org/git/?p=goodguy%2Fcinelerra.git;a=blobdiff_plain;f=cinelerra-5.1%2Fthirdparty%2Fsrc%2Fffmpeg.git.patch2;h=1d312469b1991d6b24373faa0d34638b743eda80;hp=1c1e293af6eabec3c3fbb93c388973779bd27b48;hb=HEAD;hpb=413642aafb5e96e9c72b53312176838526fbfd97 diff --git a/cinelerra-5.1/thirdparty/src/ffmpeg.git.patch2 b/cinelerra-5.1/thirdparty/src/ffmpeg.git.patch2 index 1c1e293a..fc9c74b7 100644 --- a/cinelerra-5.1/thirdparty/src/ffmpeg.git.patch2 +++ b/cinelerra-5.1/thirdparty/src/ffmpeg.git.patch2 @@ -1,292 +1,195 @@ -diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c -index fc0ea225c6..c702bc5f07 100644 --- a/libavformat/mpegtsenc.c +++ b/libavformat/mpegtsenc.c -@@ -56,9 +56,8 @@ typedef struct MpegTSService { - int sid; /* service ID */ - uint8_t name[256]; - uint8_t provider_name[256]; -- int pcr_pid; -- int pcr_packet_count; -- int pcr_packet_period; -+ int64_t pcr, pcr_packet_timer, pcr_packet_period; -+ int pcr_sid, pcr_pid; - AVProgram *program; - } MpegTSService; - -@@ -78,14 +77,12 @@ typedef struct MpegTSWrite { - MpegTSSection pat; /* MPEG-2 PAT table */ - MpegTSSection sdt; /* MPEG-2 SDT table context */ - MpegTSService **services; -- int sdt_packet_count; -- int sdt_packet_period; -- int pat_packet_count; -- int pat_packet_period; -+ int64_t sdt_packet_timer, sdt_packet_period; -+ int64_t pat_packet_timer, pat_packet_period; +@@ -89,9 +89,11 @@ + int64_t pat_period; /* PAT/PMT period in PCR time base */ + int64_t nit_period; /* NIT period in PCR time base */ int nb_services; - int onid; - int tsid; - int64_t first_pcr; -+ int64_t pcr, first_pcr, delay; + int first_dts_checked; +- int64_t next_pcr; ++ int64_t pcr_pos, pcr; ++ int64_t first_pcr, next_pcr; ++ int64_t delay; ++ int pcr_stream_pid; int mux_rate; ///< set to 1 when VBR int pes_payload_size; + int64_t total_size; +@@ -258,7 +260,7 @@ + int data_st_warning; -@@ -95,12 +92,14 @@ typedef struct MpegTSWrite { - int service_type; - - int pmt_start_pid; -+ int pcr_start_pid; - int start_pid; - int m2ts_mode; -+ int64_t ts_offset; - - int reemit_pat_pmt; // backward compatibility - -- int pcr_period; -+ double pcr_period; - #define MPEGTS_FLAG_REEMIT_PAT_PMT 0x01 - #define MPEGTS_FLAG_AAC_LATM 0x02 - #define MPEGTS_FLAG_PAT_PMT_AT_FRAMES 0x04 -@@ -111,8 +110,6 @@ typedef struct MpegTSWrite { - int tables_version; - double pat_period; - double sdt_period; -- int64_t last_pat_ts; -- int64_t last_sdt_ts; - - int omit_video_pes_length; - } MpegTSWrite; -@@ -222,10 +219,10 @@ static int mpegts_write_section1(MpegTSSection *s, int tid, int id, - #define DEFAULT_PROVIDER_NAME "FFmpeg" - #define DEFAULT_SERVICE_NAME "Service01" - --/* we retransmit the SI info at this rate */ -+/* we retransmit the SI info at this rate (ms) */ - #define SDT_RETRANS_TIME 500 - #define PAT_RETRANS_TIME 100 --#define PCR_RETRANS_TIME 20 -+#define PCR_RETRANS_TIME 50 + int64_t pcr_period; /* PCR period in PCR time base */ +- int64_t last_pcr; ++ int64_t pcr_timer; - typedef struct MpegTSWriteStream { - struct MpegTSService *service; -@@ -730,6 +727,7 @@ static MpegTSService *mpegts_add_service(AVFormatContext *s, int sid, - service->pmt.pid = ts->pmt_start_pid + ts->nb_services; - service->sid = sid; - service->pcr_pid = 0x1fff; -+ service->pcr_sid = 0x1fff; - if (encode_str8(service->provider_name, provider_name) < 0 || - encode_str8(service->name, name) < 0) { - av_log(s, AV_LOG_ERROR, "Too long service or provider name\n"); -@@ -744,18 +742,11 @@ fail: - return NULL; + /* For Opus */ + int opus_queued_samples; +@@ -959,18 +961,18 @@ + return 0; } --static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb) --{ -- return av_rescale(avio_tell(pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate) + +-static int64_t get_pcr(const MpegTSWrite *ts) ++static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb) + { +- return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) + - ts->first_pcr; --} -- - static void mpegts_prefix_m2ts_header(AVFormatContext *s) ++ int64_t pos = avio_tell(pb) + 11; ++ return ts->pcr + (ts->mux_rate == 1 ? (pos - ts->pcr_pos) * 8 : ++ av_rescale(pos - ts->pcr_pos, 8 * PCR_TIME_BASE, ts->mux_rate)); + } + + static void write_packet(AVFormatContext *s, const uint8_t *packet) { MpegTSWrite *ts = s->priv_data; if (ts->m2ts_mode) { -- int64_t pcr = get_pcr(s->priv_data, s->pb); +- int64_t pcr = get_pcr(s->priv_data); - uint32_t tp_extra_header = pcr % 0x3fffffff; -+ uint32_t tp_extra_header = ts->pcr % 0x3fffffff; ++ uint32_t tp_extra_header = get_pcr(ts, s->pb) % 0x3fffffff; tp_extra_header = AV_RB32(&tp_extra_header); avio_write(s->pb, (unsigned char *) &tp_extra_header, sizeof(tp_extra_header)); -@@ -776,6 +767,7 @@ static int mpegts_init(AVFormatContext *s) - MpegTSService *service; - AVStream *st, *pcr_st = NULL; - AVDictionaryEntry *title, *provider; -+ double clk_rate; - int i, j; - const char *service_name; - const char *provider_name; -@@ -784,6 +776,15 @@ static int mpegts_init(AVFormatContext *s) +@@ -1056,9 +1058,6 @@ + else + ts_st->pcr_period = 1; + } +- +- // output a PCR as soon as possible +- ts_st->last_pcr = ts->first_pcr - ts_st->pcr_period; + } + + static void select_pcr_streams(AVFormatContext *s) +@@ -1121,6 +1120,7 @@ if (s->max_delay < 0) /* Not set by the caller */ s->max_delay = 0; + ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE); -+ -+ if (ts->m2ts_mode == -1) { -+ if (av_match_ext(s->url, "m2ts")) { -+ ts->m2ts_mode = 1; -+ } else { -+ ts->m2ts_mode = 0; -+ } -+ } // round up to a whole number of TS packets ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14; -@@ -830,6 +831,8 @@ static int mpegts_init(AVFormatContext *s) - service->program = program; - } - } -+ if (ts->m2ts_mode > 1) -+ service->pmt.pid = 0x00ff + ts->service_id; - - ts->pat.pid = PAT_PID; - /* Initialize at 15 so that it wraps and is equal to 0 for the -@@ -915,10 +918,9 @@ static int mpegts_init(AVFormatContext *s) - ts_st->discontinuity = ts->flags & MPEGTS_FLAG_DISCONT; - /* update PCR pid by using the first video stream */ - if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && -- service->pcr_pid == 0x1fff) { -- service->pcr_pid = ts_st->pid; -+ service->pcr_sid == 0x1fff) - pcr_st = st; -- } -+ - if (st->codecpar->codec_id == AV_CODEC_ID_AAC && - st->codecpar->extradata_size > 0) { - AVStream *ast; -@@ -954,78 +956,47 @@ static int mpegts_init(AVFormatContext *s) - av_freep(&pids); - - /* if no video stream, use the first stream as PCR */ -- if (service->pcr_pid == 0x1fff && s->nb_streams > 0) { -- pcr_st = s->streams[0]; -- ts_st = pcr_st->priv_data; -- service->pcr_pid = ts_st->pid; -- } else -- ts_st = pcr_st->priv_data; -- -- if (ts->mux_rate > 1) { -- service->pcr_packet_period = (int64_t)ts->mux_rate * ts->pcr_period / -- (TS_PACKET_SIZE * 8 * 1000); -- ts->sdt_packet_period = (int64_t)ts->mux_rate * SDT_RETRANS_TIME / -- (TS_PACKET_SIZE * 8 * 1000); -- ts->pat_packet_period = (int64_t)ts->mux_rate * PAT_RETRANS_TIME / -- (TS_PACKET_SIZE * 8 * 1000); -- -- if (ts->copyts < 1) -- ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE); -- } else { -- /* Arbitrary values, PAT/PMT will also be written on video key frames */ -- ts->sdt_packet_period = 200; -- ts->pat_packet_period = 40; -- if (pcr_st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) { -- int frame_size = av_get_audio_frame_duration2(pcr_st->codecpar, 0); -- if (!frame_size) { -- av_log(s, AV_LOG_WARNING, "frame size not set\n"); -- service->pcr_packet_period = -- pcr_st->codecpar->sample_rate / (10 * 512); +@@ -1180,7 +1180,9 @@ + /* MPEG pid values < 16 are reserved. Applications which set st->id in + * this range are assigned a calculated pid. */ + if (st->id < 16) { +- if (ts->m2ts_mode) { ++ if (ts->start_pid >= 0) ++ ts_st->pid = ts->start_pid + i; ++ else if (ts->m2ts_mode) { + switch (st->codecpar->codec_type) { + case AVMEDIA_TYPE_VIDEO: + ts_st->pid = ts->m2ts_video_pid++; +@@ -1207,9 +1209,9 @@ + av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index); + return AVERROR(EINVAL); + } - } else { -- service->pcr_packet_period = -- pcr_st->codecpar->sample_rate / (10 * frame_size); -- } -- } else { -- // max delta PCR 0.1s -- // TODO: should be avg_frame_rate -- service->pcr_packet_period = -- ts_st->user_tb.den / (10 * ts_st->user_tb.num); -- } -- if (!service->pcr_packet_period) -- service->pcr_packet_period = 1; -- } -- -- ts->last_pat_ts = AV_NOPTS_VALUE; -- ts->last_sdt_ts = AV_NOPTS_VALUE; -- // The user specified a period, use only it -- if (ts->pat_period < INT_MAX/2) { -- ts->pat_packet_period = INT_MAX; -+ if (!pcr_st && s->nb_streams > 0) -+ pcr_st = s->streams[0]; -+ if (!pcr_st) { -+ av_log(s, AV_LOG_ERROR, "no streams\n"); -+ ret = AVERROR(EINVAL); -+ goto fail; - } -- if (ts->sdt_period < INT_MAX/2) { -- ts->sdt_packet_period = INT_MAX; -+ ts_st = pcr_st->priv_data; -+ if (service->pcr_sid == 0x1fff) -+ service->pcr_sid = ts_st->pid; -+ if (service->pcr_pid == 0x1fff) -+ service->pcr_pid = ts->m2ts_mode > 1 ? -+ 0x1000 + ts->service_id : service->pcr_sid ; -+ if (service->pmt.pid == service->pcr_pid) { -+ av_log(s, AV_LOG_ERROR, "Duplicate stream id %d\n", service->pcr_pid); -+ ret = AVERROR(EINVAL); -+ goto fail; - } - -+ clk_rate = ts->mux_rate > 1 ? ts->mux_rate : PCR_TIME_BASE; -+ ts->sdt_packet_period = ts->sdt_period < 0 ? -1 : ts->sdt_period/1000 * clk_rate; -+ ts->pat_packet_period = ts->pat_period/1000 * clk_rate; -+ service->pcr_packet_period = ts->pcr_period/1000 * clk_rate; -+ if (service->pcr_packet_period < (TS_PACKET_SIZE*8*10)) -+ service->pcr_packet_period = (TS_PACKET_SIZE*8*10); -+ av_log(s, AV_LOG_VERBOSE, "clk_rate %f: ticks/pkt %d pcr, %d sdt, %d pmt\n", clk_rate, -+ (int)service->pcr_packet_period, (int)ts->sdt_packet_period, (int)ts->pat_packet_period); -+ -+ if (ts->copyts < 1) -+ ts->first_pcr = av_rescale(s->max_delay, PCR_TIME_BASE, AV_TIME_BASE); -+ - // output a PCR as soon as possible -- service->pcr_packet_count = service->pcr_packet_period; -- ts->pat_packet_count = ts->pat_packet_period - 1; -- ts->sdt_packet_count = ts->sdt_packet_period - 1; +- ts_st->pid = ts->start_pid + i; + } ++ else ++ ts_st->pid = START_PID + i; + } else { + ts_st->pid = st->id; + } +@@ -1277,9 +1279,14 @@ + ts->last_pat_ts = AV_NOPTS_VALUE; + ts->last_sdt_ts = AV_NOPTS_VALUE; + ts->last_nit_ts = AV_NOPTS_VALUE; +- ts->pat_period = av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE); +- ts->sdt_period = av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE); +- ts->nit_period = av_rescale(ts->nit_period_us, PCR_TIME_BASE, AV_TIME_BASE); ++ ts->pat_period = ts->pat_period_us < 0 ? -1 : ++ av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE); ++ ts->sdt_period = ts->sdt_period_us < 0 ? -1 : ++ av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE); ++ ts->nit_period = ts->nit_period_us < 0 ? -1 : ++ av_rescale(ts->nit_period_us, PCR_TIME_BASE, AV_TIME_BASE); + ts->pcr = 0; -+ service->pcr_packet_timer = 0; -+ ts->pat_packet_timer = 0; -+ ts->sdt_packet_timer = 0; ++ ts->pcr_pos = 0; - if (ts->mux_rate == 1) - av_log(s, AV_LOG_VERBOSE, "muxrate VBR, "); - else + /* assign provider name */ + provider = av_dict_get(s->metadata, "service_provider", NULL, 0); +@@ -1295,8 +1302,8 @@ av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate); -- av_log(s, AV_LOG_VERBOSE, -- "pcr every %d pkts, sdt every %d, pat/pmt every %d pkts\n", -- service->pcr_packet_period, -- ts->sdt_packet_period, ts->pat_packet_period); -- -- if (ts->m2ts_mode == -1) { -- if (av_match_ext(s->url, "m2ts")) { -- ts->m2ts_mode = 1; -- } else { -- ts->m2ts_mode = 0; -- } -- } - - return 0; + av_log(s, AV_LOG_VERBOSE, + "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms", +- av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE), +- av_rescale(ts->pat_period, 1000, PCR_TIME_BASE)); ++ ts->sdt_period < 0 ? -1 : av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE), ++ ts->pat_period < 0 ? -1 : av_rescale(ts->pat_period, 1000, PCR_TIME_BASE)); + if (ts->flags & MPEGTS_FLAG_NIT) + av_log(s, AV_LOG_VERBOSE, ", nit every %"PRId64" ms", av_rescale(ts->nit_period, 1000, PCR_TIME_BASE)); + av_log(s, AV_LOG_VERBOSE, "\n"); +@@ -1305,36 +1312,40 @@ + } -@@ -1040,22 +1011,12 @@ static void retransmit_si_info(AVFormatContext *s, int force_pat, int64_t dts) + /* send SDT, NIT, PAT and PMT tables regularly */ +-static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int force_nit, int64_t pcr) ++static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int force_nit) + { MpegTSWrite *ts = s->priv_data; int i; -- if (++ts->sdt_packet_count == ts->sdt_packet_period || -- (dts != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) || -- (dts != AV_NOPTS_VALUE && dts - ts->last_sdt_ts >= ts->sdt_period*90000.0) +- if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) || +- (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) || +- force_sdt - ) { -- ts->sdt_packet_count = 0; -- if (dts != AV_NOPTS_VALUE) -- ts->last_sdt_ts = FFMAX(dts, ts->last_sdt_ts); -+ if ( ts->sdt_packet_period >= 0 && ts->pcr >= ts->sdt_packet_timer ) { -+ ts->sdt_packet_timer = ts->pcr + ts->sdt_packet_period; - mpegts_write_sdt(s); - } -- if (++ts->pat_packet_count == ts->pat_packet_period || -- (dts != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) || -- (dts != AV_NOPTS_VALUE && dts - ts->last_pat_ts >= ts->pat_period*90000.0) || +- if (pcr != AV_NOPTS_VALUE) +- ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts); +- mpegts_write_sdt(s); +- } +- if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) || +- (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) || - force_pat) { -- ts->pat_packet_count = 0; -- if (dts != AV_NOPTS_VALUE) -- ts->last_pat_ts = FFMAX(dts, ts->last_pat_ts); -+ if (ts->pcr >= ts->pat_packet_timer || force_pat) { -+ ts->pat_packet_timer = ts->pcr + ts->pat_packet_period; - mpegts_write_pat(s); - for (i = 0; i < ts->nb_services; i++) - mpegts_write_pmt(s, ts->services[i]); -@@ -1097,13 +1058,14 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) +- if (pcr != AV_NOPTS_VALUE) +- ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts); +- mpegts_write_pat(s); +- for (i = 0; i < ts->nb_services; i++) +- mpegts_write_pmt(s, ts->services[i]); +- } +- if ((pcr != AV_NOPTS_VALUE && ts->last_nit_ts == AV_NOPTS_VALUE) || +- (pcr != AV_NOPTS_VALUE && pcr - ts->last_nit_ts >= ts->nit_period) || +- force_nit +- ) { +- if (pcr != AV_NOPTS_VALUE) +- ts->last_nit_ts = FFMAX(pcr, ts->last_nit_ts); ++ if (ts->sdt_period >= 0) { ++ int64_t pcr = get_pcr(ts, s->pb); ++ if (ts->last_sdt_ts == AV_NOPTS_VALUE || pcr >= ts->last_sdt_ts + ts->sdt_period) ++ force_sdt = 1; ++ if (force_sdt) { ++ ts->last_sdt_ts = pcr; ++ mpegts_write_sdt(s); ++ } ++ } ++ if (ts->pat_period >= 0) { ++ int64_t pcr = get_pcr(ts, s->pb); ++ if (ts->last_pat_ts == AV_NOPTS_VALUE || pcr >= ts->last_pat_ts + ts->pat_period) ++ force_pat = 1; ++ if (force_pat) { ++ ts->last_pat_ts = pcr; ++ mpegts_write_pat(s); ++ for (i = 0; i < ts->nb_services; i++) ++ mpegts_write_pmt(s, ts->services[i]); ++ } ++ } ++ if (ts->nit_period >= 0) { ++ int64_t pcr = get_pcr(ts, s->pb); ++ if (ts->last_nit_ts == AV_NOPTS_VALUE || pcr >= ts->last_nit_ts + ts->nit_period) ++ force_nit = 1; ++ if (force_nit) { ++ ts->last_nit_ts = pcr; + if (ts->flags & MPEGTS_FLAG_NIT) + mpegts_write_nit(s); ++ } + } + } + +@@ -1371,25 +1382,29 @@ + static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) { MpegTSWrite *ts = s->priv_data; - MpegTSWriteStream *ts_st = st->priv_data; -+ uint32_t pcr_pid = ts_st->service->pcr_pid; +- MpegTSWriteStream *ts_st = st->priv_data; ++ int64_t pcr = get_pcr(ts, s->pb); ++ MpegTSWriteStream *ts_st = st ? st->priv_data : 0; ++ uint32_t pcr_pid = ts_st ? ts_st->pid : ts->pcr_stream_pid; uint8_t *q; uint8_t buf[TS_PACKET_SIZE]; @@ -294,206 +197,218 @@ index fc0ea225c6..c702bc5f07 100644 *q++ = 0x47; - *q++ = ts_st->pid >> 8; - *q++ = ts_st->pid; +- *q++ = 0x20 | ts_st->cc; /* Adaptation only */ + *q++ = pcr_pid >> 8; + *q++ = pcr_pid; - *q++ = 0x20 | ts_st->cc; /* Adaptation only */ ++ uint32_t flags = 0x20; /* Adaptation only */ /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */ ++ if(ts_st) flags |= ts_st->cc; ++ *q++ = flags; *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */ -@@ -1114,7 +1076,7 @@ static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st) + *q++ = 0x10; /* Adaptation flags: PCR present */ +- if (ts_st->discontinuity) { ++ if (ts_st && ts_st->discontinuity) { + q[-1] |= 0x80; + ts_st->discontinuity = 0; } /* PCR coded into 6 bytes */ -- q += write_pcr_bits(q, get_pcr(ts, s->pb)); -+ q += write_pcr_bits(q, ts->pcr); +- q += write_pcr_bits(q, get_pcr(ts)); ++ q += write_pcr_bits(q, pcr); /* stuffing bytes */ memset(q, 0xFF, TS_PACKET_SIZE - (q - buf)); -@@ -1183,8 +1145,6 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, - uint8_t *q; - int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags; +@@ -1490,9 +1505,9 @@ int afc_len, stuffing_len; -- int64_t pcr = -1; /* avoid warning */ + int is_dvb_subtitle = (st->codecpar->codec_id == AV_CODEC_ID_DVB_SUBTITLE); + int is_dvb_teletext = (st->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT); - int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE); int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key; + int force_sdt = 0; ++ int64_t pcr; + int force_nit = 0; av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO); -@@ -1194,28 +1154,33 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, +@@ -1509,21 +1524,19 @@ is_start = 1; while (payload_size > 0) { -+ ts->pcr = ts->first_pcr + (ts->mux_rate == 1 ? -+ (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300) : -+ // add 11, pcr references the last byte of program clock reference base -+ av_rescale(avio_tell(s->pb) + 11, 8 * PCR_TIME_BASE, ts->mux_rate)); -+ - retransmit_si_info(s, force_pat, dts); - force_pat = 0; +- int64_t pcr = AV_NOPTS_VALUE; +- if (ts->mux_rate > 1) +- pcr = get_pcr(ts); +- else if (dts != AV_NOPTS_VALUE) +- pcr = (dts - delay) * 300; +- +- retransmit_si_info(s, force_pat, force_sdt, force_nit, pcr); +- force_pat = 0; +- force_sdt = 0; +- force_nit = 0; ++ // add 11, pcr references the last byte of program clock reference base ++ ts->pcr_pos = avio_tell(s->pb) + 11; ++ pcr = ts->pcr = ts->mux_rate != 1 ? ++ av_rescale(ts->pcr_pos, 8 * PCR_TIME_BASE, ts->mux_rate) : ++ (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300); ++ if (force_pat || force_sdt || force_nit) { ++ retransmit_si_info(s, force_pat, force_sdt, force_nit); ++ force_pat = force_sdt = force_nit = 0; ++ } write_pcr = 0; -- if (ts_st->pid == ts_st->service->pcr_pid) { -- if (ts->mux_rate > 1 || is_start) // VBR pcr period is based on frames -- ts_st->service->pcr_packet_count++; -- if (ts_st->service->pcr_packet_count >= -- ts_st->service->pcr_packet_period) { -- ts_st->service->pcr_packet_count = 0; -+ if (ts_st->pid == ts_st->service->pcr_sid) { -+ if( ts->pcr >= ts_st->service->pcr_packet_timer ) { -+ ts_st->service->pcr_packet_timer = ts->pcr + ts_st->service->pcr_packet_period; + if (ts->mux_rate > 1) { + /* Send PCR packets for all PCR streams if needed */ +- pcr = get_pcr(ts); + if (pcr >= ts->next_pcr) { + int64_t next_pcr = INT64_MAX; + for (int i = 0; i < s->nb_streams; i++) { +@@ -1533,36 +1546,43 @@ + AVStream *st2 = s->streams[st2_index]; + MpegTSWriteStream *ts_st2 = st2->priv_data; + if (ts_st2->pcr_period) { +- if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) { +- ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period); +- if (st2 != st) { ++ if (pcr >= ts_st2->pcr_timer) { ++ ts_st2->pcr_timer = pcr + ts_st2->pcr_period; ++ if (st2 != st) { + mpegts_insert_pcr_only(s, st2); +- pcr = get_pcr(ts); + } else { + write_pcr = 1; + } + } +- next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period); ++ next_pcr = FFMIN(next_pcr, ts_st2->pcr_timer); + } + } + ts->next_pcr = next_pcr; + } +- if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) { +- /* pcr insert gets priority over null packet insert */ +- if (write_pcr) +- mpegts_insert_pcr_only(s, st); +- else +- mpegts_insert_null_packet(s); +- /* recalculate write_pcr and possibly retransmit si_info */ +- continue; +- } +- } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) { +- if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) { +- ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period); ++ } ++ else if (ts_st->pcr_period) { ++ if (pcr >= ts_st->pcr_timer) { ++ ts_st->pcr_timer = pcr + ts_st->pcr_period; write_pcr = 1; } } -- -+ if (write_pcr && ts_st->service->pcr_sid != ts_st->service->pcr_pid) { -+ mpegts_insert_pcr_only(s, st); + ++ if (write_pcr && ts->pcr_stream_pid >= 0) { ++ mpegts_insert_pcr_only(s, 0); + continue; + } - if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE && -- (dts - get_pcr(ts, s->pb) / 300) > delay) { -- /* pcr insert gets priority over null packet insert */ -- if (write_pcr) -- mpegts_insert_pcr_only(s, st); -+ (dts - ts->pcr / 300) > ts->delay) { ++ ++ if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE && ++ (dts - pcr / 300) > ts->delay) { + /* pcr insert gets priority over null packet insert */ + if (write_pcr) + mpegts_insert_pcr_only(s, st); - else -- mpegts_insert_null_packet(s); -- /* recalculate write_pcr and possibly retransmit si_info */ ++ else + mpegts_insert_null_packet(s); + /* recalculate write_pcr and possibly retransimit si_info */ - continue; - } - -@@ -1225,6 +1190,10 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, - val = ts_st->pid >> 8; - if (is_start) - val |= 0x40; -+ if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && -+ st->codecpar->codec_id == AV_CODEC_ID_AC3 && -+ ts->m2ts_mode > 1) -+ val |= 0x20; - *q++ = val; - *q++ = ts_st->pid; - ts_st->cc = ts_st->cc + 1 & 0xf; -@@ -1236,7 +1205,7 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, - } - if (key && is_start && pts != AV_NOPTS_VALUE) { - // set Random Access for key frames -- if (ts_st->pid == ts_st->service->pcr_pid) -+ if (ts_st->pid == ts_st->service->pcr_sid) - write_pcr = 1; - set_af_flag(buf, 0x40); - q = get_ts_payload_start(buf); -@@ -1244,14 +1213,10 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, ++ continue; ++ } ++ + /* prepare packet header */ + q = buf; + *q++ = 0x47; +@@ -1592,7 +1612,6 @@ if (write_pcr) { set_af_flag(buf, 0x10); q = get_ts_payload_start(buf); - // add 11, pcr references the last byte of program clock reference base - if (ts->mux_rate > 1) -- pcr = get_pcr(ts, s->pb); -- else -- pcr = (dts - delay) * 300; -- if (dts != AV_NOPTS_VALUE && dts < pcr / 300) -+ if (dts != AV_NOPTS_VALUE && dts < ts->pcr / 300) + if (dts != AV_NOPTS_VALUE && dts < pcr / 300) av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n"); -- extend_af(buf, write_pcr_bits(q, pcr)); -+ extend_af(buf, write_pcr_bits(q, ts->pcr)); - q = get_ts_payload_start(buf); - } - if (is_start) { -@@ -1352,11 +1317,13 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st, - *q++ = flags; - *q++ = header_len; - if (pts != AV_NOPTS_VALUE) { -- write_pts(q, flags >> 6, pts); -+ int64_t ts_pts = pts + ts->ts_offset; -+ write_pts(q, flags >> 6, ts_pts); - q += 5; - } - if (dts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE && dts != pts) { -- write_pts(q, 1, dts); -+ int64_t ts_dts = dts + ts->ts_offset; -+ write_pts(q, 1, ts_dts); - q += 5; - } - if (pes_extension && st->codecpar->codec_id == AV_CODEC_ID_DIRAC) { -@@ -1527,7 +1494,6 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) + extend_af(buf, write_pcr_bits(q, pcr)); +@@ -1864,8 +1883,8 @@ uint8_t *data = NULL; MpegTSWrite *ts = s->priv_data; MpegTSWriteStream *ts_st = st->priv_data; - const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2; +- const int64_t max_audio_delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) / 2; ++ const int64_t delay_ticks2 = ts->delay * 2; ++ const int64_t max_audio_delay = ts->delay / 2; int64_t dts = pkt->dts, pts = pkt->pts; int opus_samples = 0; - int side_data_size; -@@ -1548,16 +1514,15 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) - } - - if (ts->flags & MPEGTS_FLAG_REEMIT_PAT_PMT) { -- ts->pat_packet_count = ts->pat_packet_period - 1; -- ts->sdt_packet_count = ts->sdt_packet_period - 1; -+ ts->pat_packet_timer = ts->sdt_packet_timer = 0; - ts->flags &= ~MPEGTS_FLAG_REEMIT_PAT_PMT; - } + size_t side_data_size; +@@ -1885,9 +1904,9 @@ if (ts->copyts < 1) { if (pts != AV_NOPTS_VALUE) - pts += delay; -+ pts += 2*ts->delay; ++ pts += delay_ticks2; if (dts != AV_NOPTS_VALUE) - dts += delay; -+ dts += 2*ts->delay; ++ dts += delay_ticks2; } - if (ts_st->first_pts_check && pts == AV_NOPTS_VALUE) { -@@ -1745,7 +1710,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt) - 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 > delay/2)) { -+ && (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); -@@ -1914,12 +1879,18 @@ static const AVOption options[] = { + if (!ts_st->first_timestamp_checked && (pts == AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)) { +@@ -2354,8 +2373,10 @@ + 0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff, ENC, .unit = "mpegts_service_type" }, { "mpegts_pmt_start_pid", "Set the first pid of the PMT.", - offsetof(MpegTSWrite, pmt_start_pid), AV_OPT_TYPE_INT, - { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM }, -+ { "mpegts_pcr_start_pid", "Set the first pid of the PCR.", -+ offsetof(MpegTSWrite, pcr_start_pid), AV_OPT_TYPE_INT, -+ { .i64 = 0x1000 }, 0x0010, 0x1f00, AV_OPT_FLAG_ENCODING_PARAM }, + OFFSET(pmt_start_pid), AV_OPT_TYPE_INT, { .i64 = 0x1000 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC }, ++ { "mpegts_pcr_stream_pid", "create seperate PCR stream on this pid.", ++ OFFSET(pcr_stream_pid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 0x1f00, ENC }, { "mpegts_start_pid", "Set the first pid.", - offsetof(MpegTSWrite, start_pid), AV_OPT_TYPE_INT, - { .i64 = 0x0100 }, 0x0010, 0x0f00, AV_OPT_FLAG_ENCODING_PARAM }, - { "mpegts_m2ts_mode", "Enable m2ts mode.", - offsetof(MpegTSWrite, m2ts_mode), AV_OPT_TYPE_BOOL, -- { .i64 = -1 }, -1, 1, AV_OPT_FLAG_ENCODING_PARAM }, -+ { .i64 = -1 }, -1, 2, AV_OPT_FLAG_ENCODING_PARAM }, -+ { "mpegts_pcr_offset", "clock offset.", -+ offsetof(MpegTSWrite, ts_offset), AV_OPT_TYPE_BOOL, -+ { .i64 = 0 }, INT_MIN, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, - { "muxrate", NULL, - offsetof(MpegTSWrite, mux_rate), AV_OPT_TYPE_INT, - { .i64 = 1 }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, -@@ -1957,15 +1928,15 @@ static const AVOption options[] = { - { "omit_video_pes_length", "Omit the PES packet length for video packets", - offsetof(MpegTSWrite, omit_video_pes_length), AV_OPT_TYPE_BOOL, - { .i64 = 1 }, 0, 1, AV_OPT_FLAG_ENCODING_PARAM }, -- { "pcr_period", "PCR retransmission time in milliseconds", -- offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_INT, -- { .i64 = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, +- OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = 0x0100 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC }, ++ OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, LAST_OTHER_PID, ENC }, + { "mpegts_m2ts_mode", "Enable m2ts mode.", OFFSET(m2ts_mode), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC }, + { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC }, + { "pes_payload_size", "Minimum PES packet payload in bytes", +@@ -2381,10 +2402,10 @@ + OFFSET(omit_video_pes_length), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC }, + { "pcr_period", "PCR retransmission time in milliseconds", + OFFSET(pcr_period_ms), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ENC }, - { "pat_period", "PAT/PMT retransmission time limit in seconds", -+ { "pcr_period", "PCR retransmission time limit in msecs", -+ offsetof(MpegTSWrite, pcr_period), AV_OPT_TYPE_DOUBLE, -+ { .dbl = PCR_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, -+ { "pat_period", "PAT/PMT retransmission time limit in msecs", - offsetof(MpegTSWrite, pat_period), AV_OPT_TYPE_DOUBLE, -- { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, ++ { "pat_period", "PAT/PMT retransmission time limit in ms, -1 no pat", + OFFSET(pat_period_us), AV_OPT_TYPE_DURATION, { .i64 = PAT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC }, - { "sdt_period", "SDT retransmission time limit in seconds", -+ { .dbl = PAT_RETRANS_TIME }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, -+ { "sdt_period", "SDT retransmission time limit in msecs", - offsetof(MpegTSWrite, sdt_period), AV_OPT_TYPE_DOUBLE, -- { .dbl = INT_MAX }, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, -+ { .dbl = SDT_RETRANS_TIME }, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM }, +- OFFSET(sdt_period_us), AV_OPT_TYPE_DURATION, { .i64 = SDT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC }, ++ { "sdt_period", "SDT retransmission time limit in ms, -1 no sdt", ++ OFFSET(sdt_period_us), AV_OPT_TYPE_INT64, { .i64 = SDT_RETRANS_TIME * 1000LL }, -1, INT64_MAX, ENC }, + { "nit_period", "NIT retransmission time limit in seconds", + OFFSET(nit_period_us), AV_OPT_TYPE_DURATION, { .i64 = NIT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC }, { NULL }, - }; +--- a/libavformat/mpegts.h ++++ b/libavformat/mpegts.h +@@ -64,6 +64,7 @@ + /* PID from 0x1FFC to 0x1FFE may be assigned as needed to PMT, elementary + * streams and other data tables */ + #define NULL_PID 0x1FFF /* Null packet (used for fixed bandwidth padding) */ ++#define START_PID 0x0400 + + /* m2ts pids */ + #define M2TS_PMT_PID 0x0100 +--- a/libavformat/bluray.c ++++ b/libavformat/bluray.c +@@ -27,7 +27,7 @@ + #include "libavutil/opt.h" + + #define BLURAY_PROTO_PREFIX "bluray:" +-#define MIN_PLAYLIST_LENGTH 180 /* 3 min */ ++#define MIN_PLAYLIST_LENGTH 0 + + typedef struct { + const AVClass *class; + +--- a/doc/muxers.texi ++++ b/doc/muxers.texi +@@ -2920,7 +2920,8 @@ + Maximum time in seconds between PAT/PMT tables. Default is @code{0.1}. + + @item sdt_period @var{duration} +-Maximum time in seconds between SDT tables. Default is @code{0.5}. ++Maximum time in seconds between SDT tables. Default is @code{0.5}. Regardless ++of this setting no SDT is written in m2ts mode. + @item nit_period @var{duration} + Maximum time in seconds between NIT tables. Default is @code{0.5}.