1 /* ATSC Transport Stream muxer
2 * Copyright (C) 2019 Mathieu Duponchelle <mathieu@centricular.com>
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.
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.
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.
21 * SPDX-License-Identifier: LGPL-2.0-or-later
27 #include "gstatscmux.h"
29 GST_DEBUG_CATEGORY (gst_atsc_mux_debug);
30 #define GST_CAT_DEFAULT gst_atsc_mux_debug
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 ());
36 #define parent_class gst_atsc_mux_parent_class
37 #define ATSCMUX_ST_PS_AUDIO_EAC3 0x87
39 static GstStaticPadTemplate gst_atsc_mux_src_factory =
40 GST_STATIC_PAD_TEMPLATE ("src",
43 GST_STATIC_CAPS ("video/mpegts, "
44 "systemstream = (boolean) true, " "packetsize = (int) 188 ")
47 static GstStaticPadTemplate gst_atsc_mux_sink_factory =
48 GST_STATIC_PAD_TEMPLATE ("sink_%d",
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;"));
63 gst_atsc_mux_stream_get_es_descrs (TsMuxStream * stream,
64 GstMpegtsPMTStream * pmt_stream, GstBaseTsMux * mpegtsmux)
66 GstMpegtsDescriptor *descriptor;
68 tsmux_stream_default_get_es_descrs (stream, pmt_stream);
70 if (stream->stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
76 /* audio_stream_descriptor () | ATSC A/52-2018 Annex G
78 * descriptor_tag 8 uimsbf
79 * descriptor_length 8 uimsbf
84 * mixinfoexists 1 bslbf
85 * substream1_flag 1 bslbf
86 * substream2_flag 1 bslbf
87 * substream3_flag 1 bslbf
89 * full_service_flag 1 bslbf
90 * audio_service_type 3 uimsbf
91 * number_of_channels 3 uimsbf
98 /* 1 bit reserved, all other flags unset */
102 * 1 bit set for full_service_flag,
103 * 3 bits hardcoded audio_service_type "Complete Main",
104 * 3 bits number_of_channels
106 switch (stream->audio_channels) {
108 *pos++ = 0xC0; /* Mono */
111 *pos++ = 0xC0 | 0x2; /* 2-channel (stereo) */
116 *pos++ = 0xC0 | 0x4; /* Multichannel audio (> 2 channels; <= 3/2 + LFE channels) */
120 *pos++ = 0xC0 | 0x5; /* Multichannel audio(> 3/2 + LFE channels) */
123 descriptor = gst_mpegts_descriptor_from_registration ("EAC3", add_info, 4);
124 g_ptr_array_add (pmt_stream->descriptors, descriptor);
127 gst_mpegts_descriptor_from_custom (GST_MTS_DESC_ATSC_EAC3, add_info, 4);
128 g_ptr_array_add (pmt_stream->descriptors, descriptor);
133 gst_atsc_mux_create_new_stream (guint16 new_pid,
134 TsMuxStreamType stream_type, GstBaseTsMux * mpegtsmux)
136 TsMuxStream *ret = tsmux_stream_new (new_pid, stream_type);
138 if (stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
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) {
144 ret->id_extended = 0;
147 tsmux_stream_set_get_es_descriptors_func (ret,
148 (TsMuxStreamGetESDescriptorsFunc) gst_atsc_mux_stream_get_es_descrs,
154 /* GstBaseTsMux implementation */
157 gst_atsc_mux_create_ts_mux (GstBaseTsMux * mpegtsmux)
159 TsMux *ret = ((GstBaseTsMuxClass *) parent_class)->create_ts_mux (mpegtsmux);
160 GstMpegtsAtscMGT *mgt;
161 GstMpegtsAtscSTT *stt;
162 GstMpegtsAtscRRT *rrt;
163 GstMpegtsSection *section;
165 mgt = gst_mpegts_atsc_mgt_new ();
166 section = gst_mpegts_section_from_atsc_mgt (mgt);
167 tsmux_add_mpegts_si_section (ret, section);
169 stt = gst_mpegts_atsc_stt_new ();
170 section = gst_mpegts_section_from_atsc_stt (stt);
171 tsmux_add_mpegts_si_section (ret, section);
173 rrt = gst_mpegts_atsc_rrt_new ();
174 section = gst_mpegts_section_from_atsc_rrt (rrt);
175 tsmux_add_mpegts_si_section (ret, section);
177 tsmux_set_new_stream_func (ret,
178 (TsMuxNewStreamFunc) gst_atsc_mux_create_new_stream, mpegtsmux);
184 gst_atsc_mux_handle_media_type (GstBaseTsMux * mux, const gchar * media_type,
185 GstBaseTsMuxPad * pad)
187 guint ret = TSMUX_ST_RESERVED;
189 if (!g_strcmp0 (media_type, "audio/x-eac3")) {
190 ret = ATSCMUX_ST_PS_AUDIO_EAC3;
197 gst_atsc_mux_class_init (GstATSCMuxClass * klass)
199 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
200 GstBaseTsMuxClass *mpegtsmux_class = (GstBaseTsMuxClass *) klass;
202 GST_DEBUG_CATEGORY_INIT (gst_atsc_mux_debug, "atscmux", 0, "ATSC muxer");
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>");
209 mpegtsmux_class->create_ts_mux = gst_atsc_mux_create_ts_mux;
210 mpegtsmux_class->handle_media_type = gst_atsc_mux_handle_media_type;
212 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
213 &gst_atsc_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
215 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
216 &gst_atsc_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
220 gst_atsc_mux_init (GstATSCMux * mux)