[FFmpeg-devel] [PATCH 1/2] avformat/mpegtsenc: support DVB 6A descriptor for AC-3

lance.lmwang at gmail.com lance.lmwang at gmail.com
Fri Aug 14 17:35:16 EEST 2020


From: Limin Wang <lance.lmwang at gmail.com>

Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
---
Sorry, fix for the patch order for fate test

 libavformat/mpegts.h    | 16 +++++++++++
 libavformat/mpegtsenc.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git a/libavformat/mpegts.h b/libavformat/mpegts.h
index fe10b38..951aa61 100644
--- a/libavformat/mpegts.h
+++ b/libavformat/mpegts.h
@@ -175,6 +175,22 @@ typedef struct Mp4Descr {
     SLConfigDescr sl;
 } Mp4Descr;
 
+/*
+ * ETSI 300 468 descriptor 0x6A(AC-3)
+ * Refer to: ETSI EN 300 468 V1.11.1 (2010-04) (SI in DVB systems)
+ */
+typedef struct AVDVBAC3Descriptor {
+    uint8_t  component_type_flag;
+    uint8_t  bsid_flag;
+    uint8_t  mainid_flag;
+    uint8_t  asvc_flag;
+    uint8_t  reserved_flags;
+    uint8_t  component_type;
+    uint8_t  bsid;
+    uint8_t  mainid;
+    uint8_t  asvc;
+} AVDVBAC3Descriptor;
+
 /**
  * Parse an MPEG-2 descriptor
  * @param[in] fc                    Format context (used for logging only)
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 718ddab..e9ac08d 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -27,6 +27,7 @@
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 
+#include "libavcodec/ac3_parser_internal.h"
 #include "libavcodec/internal.h"
 
 #include "avformat.h"
@@ -244,6 +245,8 @@ typedef struct MpegTSWriteStream {
     /* For Opus */
     int opus_queued_samples;
     int opus_pending_trim_start;
+
+    AVDVBAC3Descriptor *desc6a;
 } MpegTSWriteStream;
 
 static void mpegts_write_pat(AVFormatContext *s)
@@ -486,9 +489,28 @@ static int mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
         case AVMEDIA_TYPE_AUDIO:
             if (ts->flags & MPEGTS_FLAG_SYSTEM_B) {
                 if (codec_id == AV_CODEC_ID_AC3) {
+                    AVDVBAC3Descriptor *desc6a = ts_st->desc6a;
+
                     *q++=0x6a; // AC3 descriptor see A038 DVB SI
+                    if (desc6a) {
+                        int len = 1 +
+                                  !!(desc6a->component_type_flag) +
+                                  !!(desc6a->bsid_flag) +
+                                  !!(desc6a->mainid_flag) +
+                                  !!(desc6a->asvc_flag);
+
+                        *q++ = len;
+                        *q++ = desc6a->component_type_flag << 7 | desc6a->bsid_flag << 6 |
+                               desc6a->mainid_flag         << 5 | desc6a->asvc_flag << 4;
+
+                        if (desc6a->component_type_flag) *q++ = desc6a->component_type;
+                        if (desc6a->bsid_flag)           *q++ = desc6a->bsid;
+                        if (desc6a->mainid_flag)         *q++ = desc6a->mainid;
+                        if (desc6a->asvc_flag)           *q++ = desc6a->asvc;
+                    } else {
                     *q++=1; // 1 byte, all flags sets to 0
                     *q++=0; // omit all fields...
+                    }
                 } else if (codec_id == AV_CODEC_ID_EAC3) {
                     *q++=0x7a; // EAC3 descriptor see A038 DVB SI
                     *q++=1; // 1 byte, all flags sets to 0
@@ -1843,6 +1865,59 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
              * need to count the samples of that too! */
             av_log(s, AV_LOG_WARNING, "Got MPEG-TS formatted Opus data, unhandled");
         }
+    } else if (st->codecpar->codec_id == AV_CODEC_ID_AC3 && !ts_st->desc6a) {
+        uint8_t number_of_channels_flag;
+        uint8_t service_type_flag;
+        uint8_t full_service_flag = 1;
+        AC3HeaderInfo *hdr = NULL;
+        AVDVBAC3Descriptor *desc6a;
+
+        if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) >= 0) {
+            desc6a = av_mallocz(sizeof(*desc6a));
+            if (!desc6a)
+                return AVERROR(ENOMEM);
+
+            service_type_flag = hdr->bitstream_mode;
+            switch (hdr->channel_mode) {
+                case AC3_CHMODE_DUALMONO:
+                    number_of_channels_flag = 1;
+                    break;
+                case AC3_CHMODE_MONO:
+                    number_of_channels_flag = 0;
+                    break;
+                case AC3_CHMODE_STEREO:
+                    if (hdr->dolby_surround_mode == AC3_DSURMOD_ON)
+                        number_of_channels_flag = 3;
+                    else
+                        number_of_channels_flag = 2;
+                    break;
+                case AC3_CHMODE_3F:
+                case AC3_CHMODE_2F1R:
+                case AC3_CHMODE_3F1R:
+                case AC3_CHMODE_2F2R:
+                case AC3_CHMODE_3F2R:
+                    number_of_channels_flag = 4;
+                    break;
+                default: /* reserved */
+                    number_of_channels_flag = 7;
+                    break;
+            }
+
+            if (service_type_flag  == 1 || service_type_flag == 4 ||
+                (service_type_flag == 7 && !number_of_channels_flag))
+                full_service_flag = 0;
+
+            desc6a->component_type_flag = 1;
+            desc6a->component_type = (full_service_flag << 6)              |
+                                     ((service_type_flag      & 0x7) << 3) |
+                                     (number_of_channels_flag & 0x7);
+            desc6a->bsid_flag   = 1;
+            desc6a->bsid        = hdr->bitstream_id;
+            desc6a->mainid_flag = 0;
+            desc6a->asvc_flag   = 0;
+
+            ts_st->desc6a = desc6a;
+        }
     }
 
     if (ts_st->payload_size && (ts_st->payload_size + size > ts->pes_payload_size ||
@@ -1934,6 +2009,7 @@ static void mpegts_deinit(AVFormatContext *s)
         AVStream *st = s->streams[i];
         MpegTSWriteStream *ts_st = st->priv_data;
         if (ts_st) {
+            av_freep(&ts_st->desc6a);
             av_freep(&ts_st->payload);
             if (ts_st->amux) {
                 avformat_free_context(ts_st->amux);
-- 
1.8.3.1



More information about the ffmpeg-devel mailing list