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
24 #include "gstatscmux.h"
26 GST_DEBUG_CATEGORY (gst_atsc_mux_debug);
27 #define GST_CAT_DEFAULT gst_atsc_mux_debug
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 ());
33 #define parent_class gst_atsc_mux_parent_class
34 #define ATSCMUX_ST_PS_AUDIO_EAC3 0x87
36 static GstStaticPadTemplate gst_atsc_mux_src_factory =
37 GST_STATIC_PAD_TEMPLATE ("src",
40 GST_STATIC_CAPS ("video/mpegts, "
41 "systemstream = (boolean) true, " "packetsize = (int) 188 ")
44 static GstStaticPadTemplate gst_atsc_mux_sink_factory =
45 GST_STATIC_PAD_TEMPLATE ("sink_%d",
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;"));
60 gst_atsc_mux_stream_get_es_descrs (TsMuxStream * stream,
61 GstMpegtsPMTStream * pmt_stream, GstBaseTsMux * mpegtsmux)
63 GstMpegtsDescriptor *descriptor;
65 tsmux_stream_default_get_es_descrs (stream, pmt_stream);
67 if (stream->stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
73 /* audio_stream_descriptor () | ATSC A/52-2018 Annex G
75 * descriptor_tag 8 uimsbf
76 * descriptor_length 8 uimsbf
81 * mixinfoexists 1 bslbf
82 * substream1_flag 1 bslbf
83 * substream2_flag 1 bslbf
84 * substream3_flag 1 bslbf
86 * full_service_flag 1 bslbf
87 * audio_service_type 3 uimsbf
88 * number_of_channels 3 uimsbf
95 /* 1 bit reserved, all other flags unset */
99 * 1 bit set for full_service_flag,
100 * 3 bits hardcoded audio_service_type "Complete Main",
101 * 3 bits number_of_channels
103 switch (stream->audio_channels) {
105 *pos++ = 0xC0; /* Mono */
108 *pos++ = 0xC0 | 0x2; /* 2-channel (stereo) */
113 *pos++ = 0xC0 | 0x4; /* Multichannel audio (> 2 channels; <= 3/2 + LFE channels) */
117 *pos++ = 0xC0 | 0x5; /* Multichannel audio(> 3/2 + LFE channels) */
120 descriptor = gst_mpegts_descriptor_from_registration ("EAC3", add_info, 4);
121 g_ptr_array_add (pmt_stream->descriptors, descriptor);
124 gst_mpegts_descriptor_from_custom (GST_MTS_DESC_ATSC_EAC3, add_info, 4);
125 g_ptr_array_add (pmt_stream->descriptors, descriptor);
130 gst_atsc_mux_create_new_stream (guint16 new_pid,
131 TsMuxStreamType stream_type, GstBaseTsMux * mpegtsmux)
133 TsMuxStream *ret = tsmux_stream_new (new_pid, stream_type);
135 if (stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
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) {
141 ret->id_extended = 0;
144 tsmux_stream_set_get_es_descriptors_func (ret,
145 (TsMuxStreamGetESDescriptorsFunc) gst_atsc_mux_stream_get_es_descrs,
151 /* GstBaseTsMux implementation */
154 gst_atsc_mux_create_ts_mux (GstBaseTsMux * mpegtsmux)
156 TsMux *ret = ((GstBaseTsMuxClass *) parent_class)->create_ts_mux (mpegtsmux);
157 GstMpegtsAtscMGT *mgt;
158 GstMpegtsAtscSTT *stt;
159 GstMpegtsAtscRRT *rrt;
160 GstMpegtsSection *section;
162 mgt = gst_mpegts_atsc_mgt_new ();
163 section = gst_mpegts_section_from_atsc_mgt (mgt);
164 tsmux_add_mpegts_si_section (ret, section);
166 stt = gst_mpegts_atsc_stt_new ();
167 section = gst_mpegts_section_from_atsc_stt (stt);
168 tsmux_add_mpegts_si_section (ret, section);
170 rrt = gst_mpegts_atsc_rrt_new ();
171 section = gst_mpegts_section_from_atsc_rrt (rrt);
172 tsmux_add_mpegts_si_section (ret, section);
174 tsmux_set_new_stream_func (ret,
175 (TsMuxNewStreamFunc) gst_atsc_mux_create_new_stream, mpegtsmux);
181 gst_atsc_mux_handle_media_type (GstBaseTsMux * mux, const gchar * media_type,
182 GstBaseTsMuxPad * pad)
184 guint ret = TSMUX_ST_RESERVED;
186 if (!g_strcmp0 (media_type, "audio/x-eac3")) {
187 ret = ATSCMUX_ST_PS_AUDIO_EAC3;
194 gst_atsc_mux_class_init (GstATSCMuxClass * klass)
196 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
197 GstBaseTsMuxClass *mpegtsmux_class = (GstBaseTsMuxClass *) klass;
199 GST_DEBUG_CATEGORY_INIT (gst_atsc_mux_debug, "atscmux", 0, "ATSC muxer");
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>");
206 mpegtsmux_class->create_ts_mux = gst_atsc_mux_create_ts_mux;
207 mpegtsmux_class->handle_media_type = gst_atsc_mux_handle_media_type;
209 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
210 &gst_atsc_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
212 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
213 &gst_atsc_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
217 gst_atsc_mux_init (GstATSCMux * mux)