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