[FFmpeg-devel] mpegts_write_header(), the buffering and thus reordering of audio samples.

Carl Eugen Hoyos cehoyos at ag.or.at
Thu Nov 3 16:55:02 CET 2011


On Thursday 03 November 2011 04:28:54 pm Michael Niedermayer wrote:
> On Wed, Nov 02, 2011 at 11:28:14PM +0000, Carl Eugen Hoyos wrote:
> > Will Korbe <wkorbe <at> gmail.com> writes:
> > > --- mpegtsenc.c	2011-11-01 11:00:36.000000000 -0700
> > > +++ mpegtsenc.c.new	2011-11-02 15:56:00.000000000 -0700
> >
> > I have no idea how this was made, but please use
> > git clone git://git.videolan.org/ffmpeg.git
> > to get a current snapshot, make your patch with git diff >patch.diff (or
> > a more sophisticated method) and attach the result (your patch was
> > mangled by the mailer and does not apply).
> 
> its word wraped.

That wasn't the only problem iirc.

Attached patch fixes both ticket 542 and 566 for me.

Carl Eugen
-------------- next part --------------
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index f072f80..3f3c4c6 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -778,6 +778,9 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
     int afc_len, stuffing_len;
     int64_t pcr = -1; /* avoid warning */
     int64_t delay = av_rescale(s->max_delay, 90000, AV_TIME_BASE);
+    AVStream *st2;
+    MpegTSWriteStream *ts_st2;
+    int payload_index;
 
     is_start = 1;
     while (payload_size > 0) {
@@ -794,6 +797,37 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
             }
         }
 
+        if (write_pcr) {
+            int i;
+            if (ts->mux_rate > 1) {
+                pcr = get_pcr(ts, s->pb);
+            } else {
+                pcr = (dts - delay)*300;
+            }
+
+            if (dts != AV_NOPTS_VALUE && dts < pcr / 300)
+                av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
+
+            /* if other streams are buffering data before this pcr, write that
+               data to the stream first */
+            for (i = 0; i < s->nb_streams; ++i) {
+                st2 = s->streams[i];
+
+                if (st2 == st)
+                    continue;
+
+                ts_st2 = st2->priv_data;
+
+                if (ts_st2->payload_index && pcr / 300 >= ts_st2->payload_dts) {
+                    payload_index = ts_st2->payload_index;
+                    ts_st2->payload_index = 0;
+                    mpegts_write_pes(s, st2, ts_st2->payload, payload_index,
+                                     ts_st2->payload_pts, ts_st2->payload_dts,
+                                     ts_st2->payload_flags & AV_PKT_FLAG_KEY);
+                }
+            }
+        }
+
         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 */
@@ -825,12 +859,6 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
             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)
-                av_log(s, AV_LOG_WARNING, "dts < pcr, TS is invalid\n");
             extend_af(buf, write_pcr_bits(q, pcr));
             q = get_ts_payload_start(buf);
         }


More information about the ffmpeg-devel mailing list