[FFmpeg-devel] [PATCH] Allow to recognize SCTE-35 stream in MPEG TS if Registration descriptor put on stream section instead of program. Some muxers (Harmonic for example) do it

Leonid V.Panoff l.v.panoff at gmail.com
Wed Jan 26 17:25:51 EET 2022


Signed-off-by: Leonid V.Panoff <l.v.panoff at gmail.com>
---
  libavformat/mpegts.c | 48 ++++++++++++++++++++++++++++++++++++++++----
  1 file changed, 44 insertions(+), 4 deletions(-)

diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 2479cb6f7d..0774b53b18 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2301,6 +2301,42 @@ static int is_pes_stream(int stream_type, 
uint32_t prog_reg_desc)
               (stream_type == 0x86 && prog_reg_desc == AV_RL32("CUEI")) );
  }
  +static int is_scte35_descr_in_stream(const uint8_t *p, const uint8_t 
*p_end, int stream_type)
+{
+    int desc_list_len, desc_tag, desc_len;
+    const uint8_t *desc_list_end, *desc_end;
+
+    const uint8_t *ppp = p;
+    desc_list_len = get16(&ppp, p_end);
+    if (desc_list_len < 0)
+        return 0;
+    desc_list_len &= 0xfff;
+    desc_list_end = ppp + desc_list_len;
+    while (desc_list_end <= p_end)
+    {
+        desc_tag = get8(&ppp, desc_list_end);
+        if (desc_tag < 0)
+            return 0;
+
+        desc_len = get8(&ppp, desc_list_end);
+        if (desc_len < 0)
+            return 0;
+
+        desc_end = ppp + desc_len;
+        if (desc_end > desc_list_end)
+            return 0;
+
+        if (desc_tag == REGISTRATION_DESCRIPTOR)
+        {
+            uint32_t codec_tag = bytestream_get_le32(&ppp);
+            if (stream_type == 0x86 && codec_tag == AV_RL32("CUEI"))
+                return 1;
+        }
+        ppp = desc_end;
+    }
+    return 0;
+}
+
  static void pmt_cb(MpegTSFilter *filter, const uint8_t *section, int 
section_len)
  {
      MpegTSContext *ts = filter->u.section_filter.opaque;
@@ -2316,6 +2352,7 @@ static void pmt_cb(MpegTSFilter *filter, const 
uint8_t *section, int section_len
      int stream_identifier = -1;
      struct Program *prg;
  +    int is_scte35_hack = 0;
      int mp4_descr_count = 0;
      Mp4Descr mp4_descr[MAX_MP4_DESCR_COUNT] = { { 0 } };
      int i;
@@ -2404,6 +2441,7 @@ static void pmt_cb(MpegTSFilter *filter, const 
uint8_t *section, int section_len
      for (i = 0; i < MAX_STREAMS_PER_PROGRAM; i++) {
          st = 0;
          pes = NULL;
+
          stream_type = get8(&p, p_end);
          if (stream_type < 0)
              break;
@@ -2416,10 +2454,12 @@ static void pmt_cb(MpegTSFilter *filter, const 
uint8_t *section, int section_len
           stream_identifier = parse_stream_identifier_desc(p, p_end) + 1;
  +        is_scte35_hack = prog_reg_desc == 0 ? 
is_scte35_descr_in_stream(p,p_end, stream_type) : 0;
+
          /* now create stream */
-        if (ts->pids[pid] && ts->pids[pid]->type == MPEGTS_PES) {
+        if ( ! is_scte35_hack && ts->pids[pid] && ts->pids[pid]->type 
== MPEGTS_PES) {
              pes = ts->pids[pid]->u.pes_filter.opaque;
-            if (ts->merge_pmt_versions && !pes->st) {
+            if (!is_scte35_hack && ts->merge_pmt_versions && !pes->st) {
                  st = find_matching_stream(ts, pid, h->id, 
stream_identifier, i, &old_program);
                  if (st) {
                      pes->st = st;
@@ -2434,7 +2474,7 @@ static void pmt_cb(MpegTSFilter *filter, const 
uint8_t *section, int section_len
                  pes->st->id = pes->pid;
              }
              st = pes->st;
-        } else if (is_pes_stream(stream_type, prog_reg_desc)) {
+        } else if (! is_scte35_hack && is_pes_stream(stream_type, 
prog_reg_desc)) {
              if (ts->pids[pid])
                  mpegts_close_filter(ts, ts->pids[pid]); // wrongly 
added sdt filter probably
              pes = add_pes_stream(ts, pid, pcr_pid);
@@ -2466,7 +2506,7 @@ static void pmt_cb(MpegTSFilter *filter, const 
uint8_t *section, int section_len
                      goto out;
                  st->id = pid;
                  st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
-                if (stream_type == 0x86 && prog_reg_desc == 
AV_RL32("CUEI")) {
+                if (is_scte35_hack || ( stream_type == 0x86 && 
prog_reg_desc == AV_RL32("CUEI"))) {
                      mpegts_find_stream_type(st, stream_type, SCTE_types);
                      mpegts_open_section_filter(ts, pid, scte_data_cb, 
ts, 1);
                  }
-- 
2.30.2



More information about the ffmpeg-devel mailing list