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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "gstatscmux.h"
28
29 GST_DEBUG_CATEGORY (gst_atsc_mux_debug);
30 #define GST_CAT_DEFAULT gst_atsc_mux_debug
31
32 G_DEFINE_TYPE (GstATSCMux, gst_atsc_mux, GST_TYPE_BASE_TS_MUX);
33 GST_ELEMENT_REGISTER_DEFINE (atscmux, "atscmux", GST_RANK_PRIMARY,
34 gst_atsc_mux_get_type ());
35
36 #define parent_class gst_atsc_mux_parent_class
37 #define ATSCMUX_ST_PS_AUDIO_EAC3 0x87
38
39 static GstStaticPadTemplate gst_atsc_mux_src_factory =
40 GST_STATIC_PAD_TEMPLATE ("src",
41 GST_PAD_SRC,
42 GST_PAD_ALWAYS,
43 GST_STATIC_CAPS ("video/mpegts, "
44 "systemstream = (boolean) true, " "packetsize = (int) 188 ")
45 );
46
47 static GstStaticPadTemplate gst_atsc_mux_sink_factory =
48 GST_STATIC_PAD_TEMPLATE ("sink_%d",
49 GST_PAD_SINK,
50 GST_PAD_REQUEST,
51 GST_STATIC_CAPS ("video/mpeg, "
52 "parsed = (boolean) TRUE, "
53 "mpegversion = (int) 2, "
54 "systemstream = (boolean) false; "
55 "video/x-h264,stream-format=(string)byte-stream,"
56 "alignment=(string){au, nal}; "
57 "audio/x-ac3, framed = (boolean) TRUE;"
58 "audio/x-eac3, framed = (boolean) TRUE;"));
59
60 /* Internals */
61
62 static void
gst_atsc_mux_stream_get_es_descrs(TsMuxStream * stream,GstMpegtsPMTStream * pmt_stream,GstBaseTsMux * mpegtsmux)63 gst_atsc_mux_stream_get_es_descrs (TsMuxStream * stream,
64 GstMpegtsPMTStream * pmt_stream, GstBaseTsMux * mpegtsmux)
65 {
66 GstMpegtsDescriptor *descriptor;
67
68 tsmux_stream_default_get_es_descrs (stream, pmt_stream);
69
70 if (stream->stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
71 guint8 add_info[4];
72 guint8 *pos;
73
74 pos = add_info;
75
76 /* audio_stream_descriptor () | ATSC A/52-2018 Annex G
77 *
78 * descriptor_tag 8 uimsbf
79 * descriptor_length 8 uimsbf
80 * reserved 1 '1'
81 * bsid_flag 1 bslbf
82 * mainid_flag 1 bslbf
83 * asvc_flag 1 bslbf
84 * mixinfoexists 1 bslbf
85 * substream1_flag 1 bslbf
86 * substream2_flag 1 bslbf
87 * substream3_flag 1 bslbf
88 * reserved 1 '1'
89 * full_service_flag 1 bslbf
90 * audio_service_type 3 uimsbf
91 * number_of_channels 3 uimsbf
92 * [...]
93 */
94
95 *pos++ = 0xCC;
96 *pos++ = 2;
97
98 /* 1 bit reserved, all other flags unset */
99 *pos++ = 0x80;
100
101 /* 1 bit reserved,
102 * 1 bit set for full_service_flag,
103 * 3 bits hardcoded audio_service_type "Complete Main",
104 * 3 bits number_of_channels
105 */
106 switch (stream->audio_channels) {
107 case 1:
108 *pos++ = 0xC0; /* Mono */
109 break;
110 case 2:
111 *pos++ = 0xC0 | 0x2; /* 2-channel (stereo) */
112 break;
113 case 3:
114 case 4:
115 case 5:
116 *pos++ = 0xC0 | 0x4; /* Multichannel audio (> 2 channels; <= 3/2 + LFE channels) */
117 break;
118 case 6:
119 default:
120 *pos++ = 0xC0 | 0x5; /* Multichannel audio(> 3/2 + LFE channels) */
121 }
122
123 descriptor = gst_mpegts_descriptor_from_registration ("EAC3", add_info, 4);
124 g_ptr_array_add (pmt_stream->descriptors, descriptor);
125
126 descriptor =
127 gst_mpegts_descriptor_from_custom (GST_MTS_DESC_ATSC_EAC3, add_info, 4);
128 g_ptr_array_add (pmt_stream->descriptors, descriptor);
129 }
130 }
131
132 static TsMuxStream *
gst_atsc_mux_create_new_stream(guint16 new_pid,TsMuxStreamType stream_type,GstBaseTsMux * mpegtsmux)133 gst_atsc_mux_create_new_stream (guint16 new_pid,
134 TsMuxStreamType stream_type, GstBaseTsMux * mpegtsmux)
135 {
136 TsMuxStream *ret = tsmux_stream_new (new_pid, stream_type);
137
138 if (stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
139 ret->id = 0xBD;
140 ret->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
141 ret->is_audio = TRUE;
142 } else if (stream_type == TSMUX_ST_PS_AUDIO_AC3) {
143 ret->id = 0xBD;
144 ret->id_extended = 0;
145 }
146
147 tsmux_stream_set_get_es_descriptors_func (ret,
148 (TsMuxStreamGetESDescriptorsFunc) gst_atsc_mux_stream_get_es_descrs,
149 mpegtsmux);
150
151 return ret;
152 }
153
154 /* GstBaseTsMux implementation */
155
156 static TsMux *
gst_atsc_mux_create_ts_mux(GstBaseTsMux * mpegtsmux)157 gst_atsc_mux_create_ts_mux (GstBaseTsMux * mpegtsmux)
158 {
159 TsMux *ret = ((GstBaseTsMuxClass *) parent_class)->create_ts_mux (mpegtsmux);
160 GstMpegtsAtscMGT *mgt;
161 GstMpegtsAtscSTT *stt;
162 GstMpegtsAtscRRT *rrt;
163 GstMpegtsSection *section;
164
165 mgt = gst_mpegts_atsc_mgt_new ();
166 section = gst_mpegts_section_from_atsc_mgt (mgt);
167 tsmux_add_mpegts_si_section (ret, section);
168
169 stt = gst_mpegts_atsc_stt_new ();
170 section = gst_mpegts_section_from_atsc_stt (stt);
171 tsmux_add_mpegts_si_section (ret, section);
172
173 rrt = gst_mpegts_atsc_rrt_new ();
174 section = gst_mpegts_section_from_atsc_rrt (rrt);
175 tsmux_add_mpegts_si_section (ret, section);
176
177 tsmux_set_new_stream_func (ret,
178 (TsMuxNewStreamFunc) gst_atsc_mux_create_new_stream, mpegtsmux);
179
180 return ret;
181 }
182
183 static guint
gst_atsc_mux_handle_media_type(GstBaseTsMux * mux,const gchar * media_type,GstBaseTsMuxPad * pad)184 gst_atsc_mux_handle_media_type (GstBaseTsMux * mux, const gchar * media_type,
185 GstBaseTsMuxPad * pad)
186 {
187 guint ret = TSMUX_ST_RESERVED;
188
189 if (!g_strcmp0 (media_type, "audio/x-eac3")) {
190 ret = ATSCMUX_ST_PS_AUDIO_EAC3;
191 }
192
193 return ret;
194 }
195
196 static void
gst_atsc_mux_class_init(GstATSCMuxClass * klass)197 gst_atsc_mux_class_init (GstATSCMuxClass * klass)
198 {
199 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
200 GstBaseTsMuxClass *mpegtsmux_class = (GstBaseTsMuxClass *) klass;
201
202 GST_DEBUG_CATEGORY_INIT (gst_atsc_mux_debug, "atscmux", 0, "ATSC muxer");
203
204 gst_element_class_set_static_metadata (gstelement_class,
205 "ATSC Transport Stream Muxer", "Codec/Muxer",
206 "Multiplexes media streams into an ATSC-compliant Transport Stream",
207 "Mathieu Duponchelle <mathieu@centricular.com>");
208
209 mpegtsmux_class->create_ts_mux = gst_atsc_mux_create_ts_mux;
210 mpegtsmux_class->handle_media_type = gst_atsc_mux_handle_media_type;
211
212 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
213 &gst_atsc_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
214
215 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
216 &gst_atsc_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
217 }
218
219 static void
gst_atsc_mux_init(GstATSCMux * mux)220 gst_atsc_mux_init (GstATSCMux * mux)
221 {
222 }
223