diff mbox series

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

Message ID 1597415717-22817-1-git-send-email-lance.lmwang@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/2] avformat/mpegtsenc: support DVB 6A descriptor for AC-3 | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

Limin Wang Aug. 14, 2020, 2:35 p.m. UTC
From: Limin Wang <lance.lmwang@gmail.com>

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

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

Comments

Marton Balint Aug. 14, 2020, 8:29 p.m. UTC | #1
On Fri, 14 Aug 2020, lance.lmwang@gmail.com wrote:

> From: Limin Wang <lance.lmwang@gmail.com>
>
> Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> ---
> Sorry, fix for the patch order for fate test
>
> libavformat/mpegts.h    | 16 +++++++++++
> libavformat/mpegtsenc.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++

I think the ac3_parser dependency of the mpegts muxer should be added to 
configure.

> 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;

As others suggested, remove AV prefix.

> +
> /**
>  * 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;

Sorry, can you name the variable dvb_ac3_desc or something? This 6a still 
does not speak for itself...

> } 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...

I think these two lines are small enough to reindent in the same patch.

> +                    }
>                 } 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;

push most of these initializers one level down

> +
> +        if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) >= 0) {

Aren't you leaking hdr in the return path and at the end of this block?

> +            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);
> --

Regards,
Marton
Limin Wang Aug. 15, 2020, 1:01 p.m. UTC | #2
On Fri, Aug 14, 2020 at 10:29:45PM +0200, Marton Balint wrote:
> 
> 
> On Fri, 14 Aug 2020, lance.lmwang@gmail.com wrote:
> 
> > From: Limin Wang <lance.lmwang@gmail.com>
> > 
> > Signed-off-by: Limin Wang <lance.lmwang@gmail.com>
> > ---
> > Sorry, fix for the patch order for fate test
> > 
> > libavformat/mpegts.h    | 16 +++++++++++
> > libavformat/mpegtsenc.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++
> 
> I think the ac3_parser dependency of the mpegts muxer should be added to
> configure.

sure, will add the dependency to configure

> 
> > 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;
> 
> As others suggested, remove AV prefix.
OK

> 
> > +
> > /**
> >  * 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;
> 
> Sorry, can you name the variable dvb_ac3_desc or something? This 6a still
> does not speak for itself...

sure, will fix it.

> 
> > } 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...
> 
> I think these two lines are small enough to reindent in the same patch.

OK, will fix it

> 
> > +                    }
> >                 } 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;
> 
> push most of these initializers one level down

OK

> 
> > +
> > +        if (avpriv_ac3_parse_header(&hdr, pkt->data, pkt->size) >= 0) {
> 
> Aren't you leaking hdr in the return path and at the end of this block?

Haven't notice the hdr will allocate memory, will fix it.

> 
> > +            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);
> > --
> 
> Regards,
> Marton
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

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);