Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / thirdparty / src / ffmpeg-4.4.patch2
1 diff -git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
2 --- a/libavformat/mpegtsenc.c   2021-04-08 15:28:40.000000000 -0600
3 +++ b/libavformat/mpegtsenc.c   2021-05-29 11:47:43.834039463 -0600
4 @@ -81,9 +81,11 @@
5      int64_t sdt_period; /* SDT period in PCR time base */
6      int64_t pat_period; /* PAT/PMT period in PCR time base */
7      int nb_services;
8 -    int64_t first_pcr;
9      int first_dts_checked;
10 -    int64_t next_pcr;
11 +    int64_t pcr_pos, pcr;
12 +    int64_t first_pcr, next_pcr;
13 +    int64_t delay;
14 +    int pcr_stream_pid;
15      int mux_rate; ///< set to 1 when VBR
16      int pes_payload_size;
17      int64_t total_size;
18 @@ -243,7 +245,7 @@
19      int data_st_warning;
20  
21      int64_t pcr_period; /* PCR period in PCR time base */
22 -    int64_t last_pcr;
23 +    int64_t pcr_timer;
24  
25      /* For Opus */
26      int opus_queued_samples;
27 @@ -833,18 +835,18 @@
28      return 0;
29  }
30  
31 -static int64_t get_pcr(const MpegTSWrite *ts)
32 +static int64_t get_pcr(const MpegTSWrite *ts, AVIOContext *pb)
33  {
34 -    return av_rescale(ts->total_size + 11, 8 * PCR_TIME_BASE, ts->mux_rate) +
35 -           ts->first_pcr;
36 +    int64_t pos = avio_tell(pb) + 11;
37 +    return ts->pcr + (ts->mux_rate == 1 ? (pos - ts->pcr_pos) * 8 :
38 +        av_rescale(pos - ts->pcr_pos, 8 * PCR_TIME_BASE, ts->mux_rate));
39  }
40  
41  static void write_packet(AVFormatContext *s, const uint8_t *packet)
42  {
43      MpegTSWrite *ts = s->priv_data;
44      if (ts->m2ts_mode) {
45 -        int64_t pcr = get_pcr(s->priv_data);
46 -        uint32_t tp_extra_header = pcr % 0x3fffffff;
47 +        uint32_t tp_extra_header = get_pcr(ts, s->pb) % 0x3fffffff;
48          tp_extra_header = AV_RB32(&tp_extra_header);
49          avio_write(s->pb, (unsigned char *) &tp_extra_header,
50                     sizeof(tp_extra_header));
51 @@ -930,9 +932,6 @@
52          else
53              ts_st->pcr_period = 1;
54      }
55 -
56 -    // output a PCR as soon as possible
57 -    ts_st->last_pcr   = ts->first_pcr - ts_st->pcr_period;
58  }
59  
60  static void select_pcr_streams(AVFormatContext *s)
61 @@ -993,6 +992,7 @@
62  
63      if (s->max_delay < 0) /* Not set by the caller */
64          s->max_delay = 0;
65 +    ts->delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
66  
67      // round up to a whole number of TS packets
68      ts->pes_payload_size = (ts->pes_payload_size + 14 + 183) / 184 * 184 - 14;
69 @@ -1048,7 +1048,9 @@
70          /* MPEG pid values < 16 are reserved. Applications which set st->id in
71           * this range are assigned a calculated pid. */
72          if (st->id < 16) {
73 -            if (ts->m2ts_mode) {
74 +            if (ts->start_pid >= 0)
75 +                ts_st->pid = ts->start_pid + i;
76 +            else if (ts->m2ts_mode) {
77                  switch (st->codecpar->codec_type) {
78                  case AVMEDIA_TYPE_VIDEO:
79                      ts_st->pid = ts->m2ts_video_pid++;
80 @@ -1075,9 +1077,9 @@
81                      av_log(s, AV_LOG_ERROR, "Cannot automatically assign PID for stream %d\n", st->index);
82                      return AVERROR(EINVAL);
83                  }
84 -            } else {
85 -                ts_st->pid = ts->start_pid + i;
86              }
87 +            else
88 +                ts_st->pid = START_PID + i;
89          } else {
90              ts_st->pid = st->id;
91          }
92 @@ -1144,8 +1146,12 @@
93  
94      ts->last_pat_ts = AV_NOPTS_VALUE;
95      ts->last_sdt_ts = AV_NOPTS_VALUE;
96 -    ts->pat_period = av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE);
97 -    ts->sdt_period = av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE);
98 +    ts->pat_period = ts->pat_period_us < 0 ? -1 :
99 +        av_rescale(ts->pat_period_us, PCR_TIME_BASE, AV_TIME_BASE);
100 +    ts->sdt_period = ts->sdt_period_us < 0 ? -1 :
101 +        av_rescale(ts->sdt_period_us, PCR_TIME_BASE, AV_TIME_BASE);
102 +    ts->pcr = 0;
103 +    ts->pcr_pos = 0;
104  
105      if (ts->mux_rate == 1)
106          av_log(s, AV_LOG_VERBOSE, "muxrate VBR, ");
107 @@ -1153,34 +1159,37 @@
108          av_log(s, AV_LOG_VERBOSE, "muxrate %d, ", ts->mux_rate);
109      av_log(s, AV_LOG_VERBOSE,
110             "sdt every %"PRId64" ms, pat/pmt every %"PRId64" ms\n",
111 -           av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
112 -           av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
113 +           ts->sdt_period < 0 ? -1 : av_rescale(ts->sdt_period, 1000, PCR_TIME_BASE),
114 +           ts->pat_period < 0 ? -1 : av_rescale(ts->pat_period, 1000, PCR_TIME_BASE));
115  
116      return 0;
117  }
118  
119  /* send SDT, PAT and PMT tables regularly */
120 -static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt, int64_t pcr)
121 +static void retransmit_si_info(AVFormatContext *s, int force_pat, int force_sdt)
122  {
123      MpegTSWrite *ts = s->priv_data;
124      int i;
125  
126 -    if ((pcr != AV_NOPTS_VALUE && ts->last_sdt_ts == AV_NOPTS_VALUE) ||
127 -        (pcr != AV_NOPTS_VALUE && pcr - ts->last_sdt_ts >= ts->sdt_period) ||
128 -        force_sdt
129 -    ) {
130 -        if (pcr != AV_NOPTS_VALUE)
131 -            ts->last_sdt_ts = FFMAX(pcr, ts->last_sdt_ts);
132 -        mpegts_write_sdt(s);
133 -    }
134 -    if ((pcr != AV_NOPTS_VALUE && ts->last_pat_ts == AV_NOPTS_VALUE) ||
135 -        (pcr != AV_NOPTS_VALUE && pcr - ts->last_pat_ts >= ts->pat_period) ||
136 -        force_pat) {
137 -        if (pcr != AV_NOPTS_VALUE)
138 -            ts->last_pat_ts = FFMAX(pcr, ts->last_pat_ts);
139 -        mpegts_write_pat(s);
140 -        for (i = 0; i < ts->nb_services; i++)
141 -            mpegts_write_pmt(s, ts->services[i]);
142 +    if (ts->sdt_period >= 0) {
143 +        int64_t pcr = get_pcr(ts, s->pb);
144 +        if (ts->last_sdt_ts == AV_NOPTS_VALUE || pcr >= ts->last_sdt_ts + ts->sdt_period)
145 +            force_sdt = 1;
146 +        if (force_sdt) {
147 +            ts->last_sdt_ts = pcr;
148 +            mpegts_write_sdt(s);
149 +        }
150 +    }
151 +    if (ts->pat_period >= 0) {
152 +        int64_t pcr = get_pcr(ts, s->pb);
153 +        if (ts->last_pat_ts == AV_NOPTS_VALUE || pcr >= ts->last_pat_ts + ts->pat_period)
154 +            force_pat = 1;
155 +        if (force_pat) {
156 +            ts->last_pat_ts = pcr;
157 +            mpegts_write_pat(s);
158 +            for (i = 0; i < ts->nb_services; i++)
159 +                mpegts_write_pmt(s, ts->services[i]);
160 +        }
161      }
162  }
163  
164 @@ -1217,25 +1226,29 @@
165  static void mpegts_insert_pcr_only(AVFormatContext *s, AVStream *st)
166  {
167      MpegTSWrite *ts = s->priv_data;
168 -    MpegTSWriteStream *ts_st = st->priv_data;
169 +    int64_t pcr = get_pcr(ts, s->pb);
170 +    MpegTSWriteStream *ts_st = st ? st->priv_data : 0;
171 +    uint32_t pcr_pid = ts_st ? ts_st->pid : ts->pcr_stream_pid;
172      uint8_t *q;
173      uint8_t buf[TS_PACKET_SIZE];
174  
175      q    = buf;
176      *q++ = 0x47;
177 -    *q++ = ts_st->pid >> 8;
178 -    *q++ = ts_st->pid;
179 -    *q++ = 0x20 | ts_st->cc;   /* Adaptation only */
180 +    *q++ = pcr_pid >> 8;
181 +    *q++ = pcr_pid;
182 +    uint32_t flags =  0x20;    /* Adaptation only */
183      /* Continuity Count field does not increment (see 13818-1 section 2.4.3.3) */
184 +    if(ts_st) flags |= ts_st->cc;
185 +    *q++ = flags;
186      *q++ = TS_PACKET_SIZE - 5; /* Adaptation Field Length */
187      *q++ = 0x10;               /* Adaptation flags: PCR present */
188 -    if (ts_st->discontinuity) {
189 +    if (ts_st && ts_st->discontinuity) {
190          q[-1] |= 0x80;
191          ts_st->discontinuity = 0;
192      }
193  
194      /* PCR coded into 6 bytes */
195 -    q += write_pcr_bits(q, get_pcr(ts));
196 +    q += write_pcr_bits(q, pcr);
197  
198      /* stuffing bytes */
199      memset(q, 0xFF, TS_PACKET_SIZE - (q - buf));
200 @@ -1303,9 +1316,9 @@
201      uint8_t *q;
202      int val, is_start, len, header_len, write_pcr, is_dvb_subtitle, is_dvb_teletext, flags;
203      int afc_len, stuffing_len;
204 -    int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
205      int force_pat = st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && key && !ts_st->prev_payload_key;
206      int force_sdt = 0;
207 +    int64_t pcr;
208  
209      av_assert0(ts_st->payload != buf || st->codecpar->codec_type != AVMEDIA_TYPE_VIDEO);
210      if (ts->flags & MPEGTS_FLAG_PAT_PMT_AT_FRAMES && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
211 @@ -1320,20 +1333,19 @@
212  
213      is_start = 1;
214      while (payload_size > 0) {
215 -        int64_t pcr = AV_NOPTS_VALUE;
216 -        if (ts->mux_rate > 1)
217 -            pcr = get_pcr(ts);
218 -        else if (dts != AV_NOPTS_VALUE)
219 -            pcr = (dts - delay) * 300;
220 -
221 -        retransmit_si_info(s, force_pat, force_sdt, pcr);
222 -        force_pat = 0;
223 -        force_sdt = 0;
224 +        // add 11, pcr references the last byte of program clock reference base
225 +        ts->pcr_pos = avio_tell(s->pb) + 11;
226 +        pcr = ts->pcr = ts->mux_rate != 1 ?
227 +            av_rescale(ts->pcr_pos, 8 * PCR_TIME_BASE, ts->mux_rate) :
228 +            (dts == AV_NOPTS_VALUE ? 0 : (dts - ts->delay) * 300);
229 +        if (force_pat || force_sdt) {
230 +            retransmit_si_info(s, force_pat, force_sdt);
231 +            force_pat = force_sdt = 0;
232 +        }
233  
234          write_pcr = 0;
235          if (ts->mux_rate > 1) {
236              /* Send PCR packets for all PCR streams if needed */
237 -            pcr = get_pcr(ts);
238              if (pcr >= ts->next_pcr) {
239                  int64_t next_pcr = INT64_MAX;
240                  for (int i = 0; i < s->nb_streams; i++) {
241 @@ -1343,36 +1355,43 @@
242                      AVStream *st2 = s->streams[st2_index];
243                      MpegTSWriteStream *ts_st2 = st2->priv_data;
244                      if (ts_st2->pcr_period) {
245 -                        if (pcr - ts_st2->last_pcr >= ts_st2->pcr_period) {
246 -                            ts_st2->last_pcr = FFMAX(pcr - ts_st2->pcr_period, ts_st2->last_pcr + ts_st2->pcr_period);
247 -                            if (st2 != st) {
248 +                        if (pcr >= ts_st2->pcr_timer) {
249 +                            ts_st2->pcr_timer = pcr + ts_st2->pcr_period;
250 +                           if (st2 != st) {
251                                  mpegts_insert_pcr_only(s, st2);
252 -                                pcr = get_pcr(ts);
253                              } else {
254                                  write_pcr = 1;
255                              }
256                          }
257 -                        next_pcr = FFMIN(next_pcr, ts_st2->last_pcr + ts_st2->pcr_period);
258 +                        next_pcr = FFMIN(next_pcr, ts_st2->pcr_timer);
259                      }
260                  }
261                  ts->next_pcr = next_pcr;
262              }
263 -            if (dts != AV_NOPTS_VALUE && (dts - pcr / 300) > delay) {
264 -                /* pcr insert gets priority over null packet insert */
265 -                if (write_pcr)
266 -                    mpegts_insert_pcr_only(s, st);
267 -                else
268 -                    mpegts_insert_null_packet(s);
269 -                /* recalculate write_pcr and possibly retransmit si_info */
270 -                continue;
271 -            }
272 -        } else if (ts_st->pcr_period && pcr != AV_NOPTS_VALUE) {
273 -            if (pcr - ts_st->last_pcr >= ts_st->pcr_period && is_start) {
274 -                ts_st->last_pcr = FFMAX(pcr - ts_st->pcr_period, ts_st->last_pcr + ts_st->pcr_period);
275 +        }
276 +        else if (ts_st->pcr_period) {
277 +            if (pcr >= ts_st->pcr_timer) {
278 +                ts_st->pcr_timer = pcr + ts_st->pcr_period;
279                  write_pcr = 1;
280              }
281          }
282  
283 +        if (write_pcr && ts->pcr_stream_pid >= 0) {
284 +           mpegts_insert_pcr_only(s, 0);
285 +           continue;
286 +        }
287 +
288 +        if (ts->mux_rate > 1 && dts != AV_NOPTS_VALUE &&
289 +               (dts - pcr / 300) > ts->delay) {
290 +           /* pcr insert gets priority over null packet insert */
291 +           if (write_pcr)
292 +               mpegts_insert_pcr_only(s, st);
293 +            else
294 +               mpegts_insert_null_packet(s);
295 +            /* recalculate write_pcr and possibly retransimit si_info */
296 +            continue;
297 +        }
298 +
299          /* prepare packet header */
300          q    = buf;
301          *q++ = 0x47;
302 @@ -1400,7 +1419,6 @@
303          if (write_pcr) {
304              set_af_flag(buf, 0x10);
305              q = get_ts_payload_start(buf);
306 -            // add 11, pcr references the last byte of program clock reference base
307              if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
308                  av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
309              extend_af(buf, write_pcr_bits(q, pcr));
310 @@ -1678,8 +1696,8 @@
311      uint8_t *data = NULL;
312      MpegTSWrite *ts = s->priv_data;
313      MpegTSWriteStream *ts_st = st->priv_data;
314 -    const int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) * 2;
315 -    const int64_t max_audio_delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE) / 2;
316 +    const int64_t delay_ticks2 = ts->delay * 2;
317 +    const int64_t max_audio_delay = ts->delay / 2;
318      int64_t dts = pkt->dts, pts = pkt->pts;
319      int opus_samples = 0;
320      buffer_size_t side_data_size;
321 @@ -1699,9 +1717,9 @@
322          }
323  
324          if (pts != AV_NOPTS_VALUE)
325 -            pts += delay;
326 +            pts += delay_ticks2;
327          if (dts != AV_NOPTS_VALUE)
328 -            dts += delay;
329 +            dts += delay_ticks2;
330      }
331  
332      if (!ts_st->first_timestamp_checked && (pts == AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE)) {
333 @@ -2098,8 +2116,10 @@
334        0, AV_OPT_TYPE_CONST, { .i64 = MPEGTS_SERVICE_TYPE_HEVC_DIGITAL_HDTV }, 0x01, 0xff, ENC, "mpegts_service_type" },
335      { "mpegts_pmt_start_pid", "Set the first pid of the PMT.",
336        OFFSET(pmt_start_pid), AV_OPT_TYPE_INT, { .i64 = 0x1000 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
337 +    { "mpegts_pcr_stream_pid", "create seperate PCR stream on this pid.",
338 +      OFFSET(pcr_stream_pid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, 0x1f00, ENC },
339      { "mpegts_start_pid", "Set the first pid.",
340 -      OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = 0x0100 }, FIRST_OTHER_PID, LAST_OTHER_PID, ENC },
341 +      OFFSET(start_pid), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, LAST_OTHER_PID, ENC },
342      { "mpegts_m2ts_mode", "Enable m2ts mode.", OFFSET(m2ts_mode), AV_OPT_TYPE_BOOL, { .i64 = -1 }, -1, 1, ENC },
343      { "muxrate", NULL, OFFSET(mux_rate), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, INT_MAX, ENC },
344      { "pes_payload_size", "Minimum PES packet payload in bytes",
345 @@ -2121,10 +2141,10 @@
346        OFFSET(omit_video_pes_length), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, ENC },
347      { "pcr_period", "PCR retransmission time in milliseconds",
348        OFFSET(pcr_period_ms), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, ENC },
349 -    { "pat_period", "PAT/PMT retransmission time limit in seconds",
350 +    { "pat_period", "PAT/PMT retransmission time limit in ms, -1 no pat",
351        OFFSET(pat_period_us), AV_OPT_TYPE_DURATION, { .i64 = PAT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
352 -    { "sdt_period", "SDT retransmission time limit in seconds",
353 -      OFFSET(sdt_period_us), AV_OPT_TYPE_DURATION, { .i64 = SDT_RETRANS_TIME * 1000LL }, 0, INT64_MAX, ENC },
354 +    { "sdt_period", "SDT retransmission time limit in ms, -1 no sdt",
355 +      OFFSET(sdt_period_us), AV_OPT_TYPE_INT64, { .i64 = SDT_RETRANS_TIME * 1000LL }, -1, INT64_MAX, ENC },
356      { NULL },
357  };
358
359 diff -git a/libavformat/mpegts.h b/libavformat/mpegts.h
360 --- a/libavformat/mpegts.h      2020-10-15 12:32:22.417967898 -0600
361 +++ b/libavformat/mpegts.h      2020-10-15 12:32:46.463055553 -0600
362 @@ -64,6 +64,7 @@
363  /* PID from 0x1FFC to 0x1FFE may be assigned as needed to PMT, elementary
364   * streams and other data tables */
365  #define NULL_PID        0x1FFF /* Null packet (used for fixed bandwidth padding) */
366 +#define START_PID       0x0400
367
368  /* m2ts pids */
369  #define M2TS_PMT_PID                      0x0100