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.
22 #include "gstatscmux.h"
24 GST_DEBUG_CATEGORY (gst_atsc_mux_debug);
25 #define GST_CAT_DEFAULT gst_atsc_mux_debug
27 G_DEFINE_TYPE (GstATSCMux, gst_atsc_mux, GST_TYPE_BASE_TS_MUX);
29 #define parent_class gst_atsc_mux_parent_class
30 #define ATSCMUX_ST_PS_AUDIO_EAC3 0x87
32 static GstStaticPadTemplate gst_atsc_mux_src_factory =
33 GST_STATIC_PAD_TEMPLATE ("src",
36 GST_STATIC_CAPS ("video/mpegts, "
37 "systemstream = (boolean) true, " "packetsize = (int) 188 ")
40 static GstStaticPadTemplate gst_atsc_mux_sink_factory =
41 GST_STATIC_PAD_TEMPLATE ("sink_%d",
44 GST_STATIC_CAPS ("video/mpeg, "
45 "parsed = (boolean) TRUE, "
46 "mpegversion = (int) 2, "
47 "systemstream = (boolean) false; "
48 "video/x-h264,stream-format=(string)byte-stream,"
49 "alignment=(string){au, nal}; "
50 "audio/x-ac3, framed = (boolean) TRUE;"
51 "audio/x-eac3, framed = (boolean) TRUE;"));
56 gst_atsc_mux_stream_get_es_descrs (TsMuxStream * stream,
57 GstMpegtsPMTStream * pmt_stream, GstBaseTsMux * mpegtsmux)
59 GstMpegtsDescriptor *descriptor;
61 tsmux_stream_default_get_es_descrs (stream, pmt_stream);
63 if (stream->stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
69 /* audio_stream_descriptor () | ATSC A/52-2018 Annex G
71 * descriptor_tag 8 uimsbf
72 * descriptor_length 8 uimsbf
77 * mixinfoexists 1 bslbf
78 * substream1_flag 1 bslbf
79 * substream2_flag 1 bslbf
80 * substream3_flag 1 bslbf
82 * full_service_flag 1 bslbf
83 * audio_service_type 3 uimsbf
84 * number_of_channels 3 uimsbf
91 /* 1 bit reserved, all other flags unset */
95 * 1 bit set for full_service_flag,
96 * 3 bits hardcoded audio_service_type "Complete Main",
97 * 3 bits number_of_channels
99 switch (stream->audio_channels) {
101 *pos++ = 0xC0; /* Mono */
104 *pos++ = 0xC0 | 0x2; /* 2-channel (stereo) */
109 *pos++ = 0xC0 | 0x4; /* Multichannel audio (> 2 channels; <= 3/2 + LFE channels) */
113 *pos++ = 0xC0 | 0x5; /* Multichannel audio(> 3/2 + LFE channels) */
116 descriptor = gst_mpegts_descriptor_from_registration ("EAC3", add_info, 4);
117 g_ptr_array_add (pmt_stream->descriptors, descriptor);
120 gst_mpegts_descriptor_from_custom (GST_MTS_DESC_ATSC_EAC3, add_info, 4);
121 g_ptr_array_add (pmt_stream->descriptors, descriptor);
126 gst_atsc_mux_create_new_stream (guint16 new_pid,
127 TsMuxStreamType stream_type, GstBaseTsMux * mpegtsmux)
129 TsMuxStream *ret = tsmux_stream_new (new_pid, stream_type);
131 if (stream_type == ATSCMUX_ST_PS_AUDIO_EAC3) {
133 ret->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
134 ret->is_audio = TRUE;
135 } else if (stream_type == TSMUX_ST_PS_AUDIO_AC3) {
137 ret->id_extended = 0;
140 tsmux_stream_set_get_es_descriptors_func (ret,
141 (TsMuxStreamGetESDescriptorsFunc) gst_atsc_mux_stream_get_es_descrs,
147 /* GstBaseTsMux implementation */
150 gst_atsc_mux_create_ts_mux (GstBaseTsMux * mpegtsmux)
152 TsMux *ret = ((GstBaseTsMuxClass *) parent_class)->create_ts_mux (mpegtsmux);
153 GstMpegtsAtscMGT *mgt;
154 GstMpegtsAtscSTT *stt;
155 GstMpegtsAtscRRT *rrt;
156 GstMpegtsSection *section;
158 mgt = gst_mpegts_atsc_mgt_new ();
159 section = gst_mpegts_section_from_atsc_mgt (mgt);
160 tsmux_add_mpegts_si_section (ret, section);
162 stt = gst_mpegts_atsc_stt_new ();
163 section = gst_mpegts_section_from_atsc_stt (stt);
164 tsmux_add_mpegts_si_section (ret, section);
166 rrt = gst_mpegts_atsc_rrt_new ();
167 section = gst_mpegts_section_from_atsc_rrt (rrt);
168 tsmux_add_mpegts_si_section (ret, section);
170 tsmux_set_new_stream_func (ret,
171 (TsMuxNewStreamFunc) gst_atsc_mux_create_new_stream, mpegtsmux);
177 gst_atsc_mux_handle_media_type (GstBaseTsMux * mux, const gchar * media_type,
178 GstBaseTsMuxPad * pad)
180 guint ret = TSMUX_ST_RESERVED;
182 if (!g_strcmp0 (media_type, "audio/x-eac3")) {
183 ret = ATSCMUX_ST_PS_AUDIO_EAC3;
190 gst_atsc_mux_class_init (GstATSCMuxClass * klass)
192 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
193 GstBaseTsMuxClass *mpegtsmux_class = (GstBaseTsMuxClass *) klass;
195 GST_DEBUG_CATEGORY_INIT (gst_atsc_mux_debug, "atscmux", 0, "ATSC muxer");
197 gst_element_class_set_static_metadata (gstelement_class,
198 "ATSC Transport Stream Muxer", "Codec/Muxer",
199 "Multiplexes media streams into an ATSC-compliant Transport Stream",
200 "Mathieu Duponchelle <mathieu@centricular.com>");
202 mpegtsmux_class->create_ts_mux = gst_atsc_mux_create_ts_mux;
203 mpegtsmux_class->handle_media_type = gst_atsc_mux_handle_media_type;
205 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
206 &gst_atsc_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
208 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
209 &gst_atsc_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
213 gst_atsc_mux_init (GstATSCMux * mux)