f50f5df2da83099d6abb8dad68272f0de49c7f6d
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / mpegtsmux / gstatscmux.c
1 /* ATSC Transport Stream muxer
2  * Copyright (C) 2019 Mathieu Duponchelle <mathieu@centricular.com>
3  *
4  * atscmux.c:
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * SPDX-License-Identifier: LGPL-2.0-or-later
22  */
23
24 #include "gstatscmux.h"
25
26 GST_DEBUG_CATEGORY (gst_atsc_mux_debug);
27 #define GST_CAT_DEFAULT gst_atsc_mux_debug
28
29 G_DEFINE_TYPE (GstATSCMux, gst_atsc_mux, GST_TYPE_BASE_TS_MUX);
30 GST_ELEMENT_REGISTER_DEFINE (atscmux, "atscmux", GST_RANK_PRIMARY,
31     gst_atsc_mux_get_type ());
32
33 #define parent_class gst_atsc_mux_parent_class
34 #define ATSCMUX_ST_PS_AUDIO_EAC3 0x87
35
36 static GstStaticPadTemplate gst_atsc_mux_src_factory =
37 GST_STATIC_PAD_TEMPLATE ("src",
38     GST_PAD_SRC,
39     GST_PAD_ALWAYS,
40     GST_STATIC_CAPS ("video/mpegts, "
41         "systemstream = (boolean) true, " "packetsize = (int) 188 ")
42     );
43
44 static GstStaticPadTemplate gst_atsc_mux_sink_factory =
45     GST_STATIC_PAD_TEMPLATE ("sink_%d",
46     GST_PAD_SINK,
47     GST_PAD_REQUEST,
48     GST_STATIC_CAPS ("video/mpeg, "
49         "parsed = (boolean) TRUE, "
50         "mpegversion = (int) 2, "
51         "systemstream = (boolean) false; "
52         "video/x-h264,stream-format=(string)byte-stream,"
53         "alignment=(string){au, nal}; "
54         "audio/x-ac3, framed = (boolean) TRUE;"
55         "audio/x-eac3, framed = (boolean) TRUE;"));
56
57 /* Internals */
58
59 static void
60 gst_atsc_mux_stream_get_es_descrs (TsMuxStream * stream,
61     GstMpegtsPMTStream * pmt_stream, GstBaseTsMux * mpegtsmux)
62 {
63   GstMpegtsDescriptor *descriptor;
64
65   tsmux_stream_default_get_es_descrs (stream, pmt_stream);
66
67   if (stream->stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
68     guint8 add_info[4];
69     guint8 *pos;
70
71     pos = add_info;
72
73     /* audio_stream_descriptor () | ATSC A/52-2018 Annex G
74      *
75      * descriptor_tag     8 uimsbf
76      * descriptor_length  8 uimsbf
77      * reserved           1 '1'
78      * bsid_flag          1 bslbf
79      * mainid_flag        1 bslbf
80      * asvc_flag          1 bslbf
81      * mixinfoexists      1 bslbf
82      * substream1_flag    1 bslbf
83      * substream2_flag    1 bslbf
84      * substream3_flag    1 bslbf
85      * reserved           1 '1'
86      * full_service_flag  1 bslbf
87      * audio_service_type 3 uimsbf
88      * number_of_channels 3 uimsbf
89      * [...]
90      */
91
92     *pos++ = 0xCC;
93     *pos++ = 2;
94
95     /* 1 bit reserved, all other flags unset */
96     *pos++ = 0x80;
97
98     /* 1 bit reserved,
99      * 1 bit set for full_service_flag,
100      * 3 bits hardcoded audio_service_type "Complete Main",
101      * 3 bits number_of_channels
102      */
103     switch (stream->audio_channels) {
104       case 1:
105         *pos++ = 0xC0;          /* Mono */
106         break;
107       case 2:
108         *pos++ = 0xC0 | 0x2;    /* 2-channel (stereo) */
109         break;
110       case 3:
111       case 4:
112       case 5:
113         *pos++ = 0xC0 | 0x4;    /* Multichannel audio (> 2 channels; <= 3/2 + LFE channels) */
114         break;
115       case 6:
116       default:
117         *pos++ = 0xC0 | 0x5;    /* Multichannel audio(> 3/2 + LFE channels) */
118     }
119
120     descriptor = gst_mpegts_descriptor_from_registration ("EAC3", add_info, 4);
121     g_ptr_array_add (pmt_stream->descriptors, descriptor);
122
123     descriptor =
124         gst_mpegts_descriptor_from_custom (GST_MTS_DESC_ATSC_EAC3, add_info, 4);
125     g_ptr_array_add (pmt_stream->descriptors, descriptor);
126   }
127 }
128
129 static TsMuxStream *
130 gst_atsc_mux_create_new_stream (guint16 new_pid,
131     TsMuxStreamType stream_type, GstBaseTsMux * mpegtsmux)
132 {
133   TsMuxStream *ret = tsmux_stream_new (new_pid, stream_type);
134
135   if (stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
136     ret->id = 0xBD;
137     ret->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
138     ret->is_audio = TRUE;
139   } else if (stream_type == TSMUX_ST_PS_AUDIO_AC3) {
140     ret->id = 0xBD;
141     ret->id_extended = 0;
142   }
143
144   tsmux_stream_set_get_es_descriptors_func (ret,
145       (TsMuxStreamGetESDescriptorsFunc) gst_atsc_mux_stream_get_es_descrs,
146       mpegtsmux);
147
148   return ret;
149 }
150
151 /* GstBaseTsMux implementation */
152
153 static TsMux *
154 gst_atsc_mux_create_ts_mux (GstBaseTsMux * mpegtsmux)
155 {
156   TsMux *ret = ((GstBaseTsMuxClass *) parent_class)->create_ts_mux (mpegtsmux);
157   GstMpegtsAtscMGT *mgt;
158   GstMpegtsAtscSTT *stt;
159   GstMpegtsAtscRRT *rrt;
160   GstMpegtsSection *section;
161
162   mgt = gst_mpegts_atsc_mgt_new ();
163   section = gst_mpegts_section_from_atsc_mgt (mgt);
164   tsmux_add_mpegts_si_section (ret, section);
165
166   stt = gst_mpegts_atsc_stt_new ();
167   section = gst_mpegts_section_from_atsc_stt (stt);
168   tsmux_add_mpegts_si_section (ret, section);
169
170   rrt = gst_mpegts_atsc_rrt_new ();
171   section = gst_mpegts_section_from_atsc_rrt (rrt);
172   tsmux_add_mpegts_si_section (ret, section);
173
174   tsmux_set_new_stream_func (ret,
175       (TsMuxNewStreamFunc) gst_atsc_mux_create_new_stream, mpegtsmux);
176
177   return ret;
178 }
179
180 static guint
181 gst_atsc_mux_handle_media_type (GstBaseTsMux * mux, const gchar * media_type,
182     GstBaseTsMuxPad * pad)
183 {
184   guint ret = TSMUX_ST_RESERVED;
185
186   if (!g_strcmp0 (media_type, "audio/x-eac3")) {
187     ret = ATSCMUX_ST_PS_AUDIO_EAC3;
188   }
189
190   return ret;
191 }
192
193 static void
194 gst_atsc_mux_class_init (GstATSCMuxClass * klass)
195 {
196   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
197   GstBaseTsMuxClass *mpegtsmux_class = (GstBaseTsMuxClass *) klass;
198
199   GST_DEBUG_CATEGORY_INIT (gst_atsc_mux_debug, "atscmux", 0, "ATSC muxer");
200
201   gst_element_class_set_static_metadata (gstelement_class,
202       "ATSC Transport Stream Muxer", "Codec/Muxer",
203       "Multiplexes media streams into an ATSC-compliant Transport Stream",
204       "Mathieu Duponchelle <mathieu@centricular.com>");
205
206   mpegtsmux_class->create_ts_mux = gst_atsc_mux_create_ts_mux;
207   mpegtsmux_class->handle_media_type = gst_atsc_mux_handle_media_type;
208
209   gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
210       &gst_atsc_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
211
212   gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
213       &gst_atsc_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
214 }
215
216 static void
217 gst_atsc_mux_init (GstATSCMux * mux)
218 {
219 }