diff mbox

[FFmpeg-devel,PATCHv2,06/10] avformat/mpegtsenc: use standard pids for m2ts

Message ID 20191113214756.14892-1-cus@passwd.hu
State New
Headers show

Commit Message

Marton Balint Nov. 13, 2019, 9:47 p.m. UTC
Signed-off-by: Marton Balint <cus@passwd.hu>
---
 doc/muxers.texi         |  6 ++++--
 libavformat/mpegtsenc.c | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 3 deletions(-)
diff mbox

Patch

diff --git a/doc/muxers.texi b/doc/muxers.texi
index 5d7ff1ab3b..4e76b40151 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -1583,11 +1583,13 @@  Advanced Codec Digital HDTV service.
 
 @item mpegts_pmt_start_pid @var{integer}
 Set the first PID for PMTs. Default is @code{0x1000}, minimum is @code{0x0020},
-maximum is @code{0x1ffa}.
+maximum is @code{0x1ffa}. This option has no effect in m2ts mode where the PMT
+PID is fixed @code{0x0100}.
 
 @item mpegts_start_pid @var{integer}
 Set the first PID for elementary streams. Default is @code{0x0100}, minimum is
-@code{0x0020}, maximum is @code{0x1ffa}.
+@code{0x0020}, maximum is @code{0x1ffa}. This option has no effect in m2ts mode
+where the elementary stream PIDs are fixed.
 
 @item mpegts_m2ts_mode @var{boolean}
 Enable m2ts mode if set to @code{1}. Default value is @code{-1} which
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index e8dd8b7d56..3d98eb79a3 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -94,6 +94,10 @@  typedef struct MpegTSWrite {
     int pmt_start_pid;
     int start_pid;
     int m2ts_mode;
+    int m2ts_video_pid;
+    int m2ts_audio_pid;
+    int m2ts_subtitle_pid;
+    int m2ts_other_pid;
 
     int pcr_period_ms;
 #define MPEGTS_FLAG_REEMIT_PAT_PMT  0x01
@@ -858,6 +862,14 @@  static int mpegts_init(AVFormatContext *s)
         }
     }
 
+    ts->m2ts_video_pid    = 0x1011;
+    ts->m2ts_audio_pid    = 0x1100;
+    ts->m2ts_subtitle_pid = 0x1200;
+    ts->m2ts_other_pid    = 0x1300; // should not be needed
+
+    if (ts->m2ts_mode)
+        ts->pmt_start_pid = 0x0100;
+
     if (s->max_delay < 0) /* Not set by the caller */
         s->max_delay = 0;
 
@@ -921,7 +933,24 @@  static int mpegts_init(AVFormatContext *s)
         /* MPEG pid values < 16 are reserved. Applications which set st->id in
          * this range are assigned a calculated pid. */
         if (st->id < 16) {
-            ts_st->pid = ts->start_pid + i;
+            if (ts->m2ts_mode) {
+               switch (st->codecpar->codec_type) {
+                   case AVMEDIA_TYPE_VIDEO:
+                       ts_st->pid = ts->m2ts_video_pid++;
+                       break;
+                   case AVMEDIA_TYPE_AUDIO:
+                       ts_st->pid = ts->m2ts_audio_pid++;
+                       break;
+                   case AVMEDIA_TYPE_SUBTITLE:
+                       ts_st->pid = ts->m2ts_subtitle_pid++;
+                       break;
+                   default:
+                       ts_st->pid = ts->m2ts_other_pid++;
+                       break;
+               }
+            } else {
+                ts_st->pid = ts->start_pid + i;
+            }
         } else {
             ts_st->pid = st->id;
         }