d5515a087c9664a1b5052b9337317b695426b7f4
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst / mpegtsmux / gstmpegtsmux.c
1 /*
2  * Copyright 2006, 2007, 2008, 2009, 2010 Fluendo S.A.
3  *  Authors: Jan Schmidt <jan@fluendo.com>
4  *           Kapil Agrawal <kapil@fluendo.com>
5  *           Julien Moutte <julien@fluendo.com>
6  *
7  * Copyright (C) 2011 Jan Schmidt <thaytan@noraisin.net>
8  *
9  * This library is licensed under 3 different licenses and you
10  * can choose to use it under the terms of any one of them. The
11  * three licenses are the MPL 1.1, the LGPL and the MIT license.
12  *
13  * MPL:
14  *
15  * The contents of this file are subject to the Mozilla Public License
16  * Version 1.1 (the "License"); you may not use this file except in
17  * compliance with the License. You may obtain a copy of the License at
18  * http://www.mozilla.org/MPL/.
19  *
20  * Software distributed under the License is distributed on an "AS IS"
21  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
22  * License for the specific language governing rights and limitations
23  * under the License.
24  *
25  * LGPL:
26  *
27  * This library is free software; you can redistribute it and/or
28  * modify it under the terms of the GNU Library General Public
29  * License as published by the Free Software Foundation; either
30  * version 2 of the License, or (at your option) any later version.
31  *
32  * This library is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
35  * Library General Public License for more details.
36  *
37  * You should have received a copy of the GNU Library General Public
38  * License along with this library; if not, write to the
39  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
40  * Boston, MA 02110-1301, USA.
41  *
42  * MIT:
43  *
44  * Unless otherwise indicated, Source Code is licensed under MIT license.
45  * See further explanation attached in License Statement (distributed in the file
46  * LICENSE).
47  *
48  * Permission is hereby granted, free of charge, to any person obtaining a copy of
49  * this software and associated documentation files (the "Software"), to deal in
50  * the Software without restriction, including without limitation the rights to
51  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
52  * of the Software, and to permit persons to whom the Software is furnished to do
53  * so, subject to the following conditions:
54  *
55  * The above copyright notice and this permission notice shall be included in all
56  * copies or substantial portions of the Software.
57  *
58  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
59  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
60  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
61  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
62  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
63  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
64  * SOFTWARE.
65  *
66  * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
67  */
68
69 /**
70  * SECTION: element-mpegtsmux
71  * @title: MPEG Transport Stream muxer
72  *
73  * mpegtsmux muxes different streams into an MPEG Transport stream
74  *
75  * SI sections can be specified through a custom event:
76  *
77  * {{ tests/examples/mpegts/ts-section-writer.c }}
78  */
79
80 #include "gstmpegtsmux.h"
81 #include <string.h>
82
83 #define MPEGTSMUX_DEFAULT_M2TS         FALSE
84
85 #define M2TS_PACKET_LENGTH      192
86
87 enum
88 {
89   PROP_0,
90   PROP_M2TS_MODE,
91 };
92
93 static GstStaticPadTemplate gst_mpeg_ts_mux_sink_factory =
94     GST_STATIC_PAD_TEMPLATE ("sink_%d",
95     GST_PAD_SINK,
96     GST_PAD_REQUEST,
97     GST_STATIC_CAPS ("video/mpeg, "
98         "parsed = (boolean) TRUE, "
99         "mpegversion = (int) { 1, 2, 4 }, "
100         "systemstream = (boolean) false; "
101         "video/x-dirac;"
102         "image/x-jpc, alignment = (string) frame;"
103         "video/x-h264,stream-format=(string)byte-stream,"
104         "alignment=(string){au, nal}; "
105         "video/x-h265,stream-format=(string)byte-stream,"
106         "alignment=(string){au, nal}; "
107         "audio/mpeg, "
108         "parsed = (boolean) TRUE, "
109         "mpegversion = (int) 1;"
110         "audio/mpeg, "
111         "framed = (boolean) TRUE, "
112         "mpegversion = (int) {2, 4}, stream-format = (string) { adts, raw };"
113         "audio/x-lpcm, "
114         "width = (int) { 16, 20, 24 }, "
115         "rate = (int) { 48000, 96000 }, "
116         "channels = (int) [ 1, 8 ], "
117         "dynamic_range = (int) [ 0, 255 ], "
118         "emphasis = (boolean) { FALSE, TRUE }, "
119         "mute = (boolean) { FALSE, TRUE }; "
120         "audio/x-ac3, framed = (boolean) TRUE;"
121         "audio/x-dts, framed = (boolean) TRUE;"
122         "audio/x-opus, "
123         "channels = (int) [1, 8], "
124         "channel-mapping-family = (int) {0, 1};"
125         "subpicture/x-dvb; application/x-teletext; meta/x-klv, parsed=true;"
126         "image/x-jpc, alignment = (string) frame, profile = (int)[0, 49151];"));
127
128 static GstStaticPadTemplate gst_mpeg_ts_mux_src_factory =
129 GST_STATIC_PAD_TEMPLATE ("src",
130     GST_PAD_SRC,
131     GST_PAD_ALWAYS,
132     GST_STATIC_CAPS ("video/mpegts, "
133         "systemstream = (boolean) true, " "packetsize = (int) { 188, 192} ")
134     );
135
136 GST_DEBUG_CATEGORY (gst_mpeg_ts_mux_debug);
137 #define GST_CAT_DEFAULT gst_mpeg_ts_mux_debug
138
139 #define parent_class gst_mpeg_ts_mux_parent_class
140 G_DEFINE_TYPE (GstMpegTsMux, gst_mpeg_ts_mux, GST_TYPE_BASE_TS_MUX);
141 GST_ELEMENT_REGISTER_DEFINE (mpegtsmux, "mpegtsmux", GST_RANK_PRIMARY,
142     gst_mpeg_ts_mux_get_type ());
143
144 /* Internals */
145
146 static gboolean
147 new_packet_m2ts (GstMpegTsMux * mux, GstBuffer * buf, gint64 new_pcr)
148 {
149   GstBuffer *out_buf;
150   int chunk_bytes;
151   GstMapInfo map;
152
153   GST_LOG_OBJECT (mux, "Have buffer %p with new_pcr=%" G_GINT64_FORMAT,
154       buf, new_pcr);
155
156   chunk_bytes = gst_adapter_available (mux->adapter);
157
158   if (G_LIKELY (buf)) {
159     if (new_pcr < 0) {
160       /* If there is no pcr in current ts packet then just add the packet
161          to the adapter for later output when we see a PCR */
162       GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
163       gst_adapter_push (mux->adapter, buf);
164       goto exit;
165     }
166
167     /* no first interpolation point yet, then this is the one,
168      * otherwise it is the second interpolation point */
169     if (mux->previous_pcr < 0 && chunk_bytes) {
170       mux->previous_pcr = new_pcr;
171       mux->previous_offset = chunk_bytes;
172       GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
173       gst_adapter_push (mux->adapter, buf);
174       goto exit;
175     }
176   } else {
177     g_assert (new_pcr == -1);
178   }
179
180   /* interpolate if needed, and 2 points available */
181   if (chunk_bytes && (new_pcr != mux->previous_pcr)) {
182     gint64 offset = 0;
183
184     GST_LOG_OBJECT (mux, "Processing pending packets; "
185         "previous pcr %" G_GINT64_FORMAT ", previous offset %d, "
186         "current pcr %" G_GINT64_FORMAT ", current offset %d",
187         mux->previous_pcr, (gint) mux->previous_offset,
188         new_pcr, (gint) chunk_bytes);
189
190     g_assert (chunk_bytes > mux->previous_offset);
191     /* if draining, use previous rate */
192     if (G_LIKELY (new_pcr > 0)) {
193       mux->pcr_rate_num = new_pcr - mux->previous_pcr;
194       mux->pcr_rate_den = chunk_bytes - mux->previous_offset;
195     }
196
197     while (offset < chunk_bytes) {
198       guint64 cur_pcr, ts;
199
200       /* Loop, pulling packets of the adapter, updating their 4 byte
201        * timestamp header and pushing */
202
203       /* interpolate PCR */
204       if (G_LIKELY (offset >= mux->previous_offset))
205         cur_pcr = mux->previous_pcr +
206             gst_util_uint64_scale (offset - mux->previous_offset,
207             mux->pcr_rate_num, mux->pcr_rate_den);
208       else
209         cur_pcr = mux->previous_pcr -
210             gst_util_uint64_scale (mux->previous_offset - offset,
211             mux->pcr_rate_num, mux->pcr_rate_den);
212
213       /* FIXME: what about DTS here? */
214       ts = gst_adapter_prev_pts (mux->adapter, NULL);
215       out_buf = gst_adapter_take_buffer (mux->adapter, M2TS_PACKET_LENGTH);
216       g_assert (out_buf);
217       offset += M2TS_PACKET_LENGTH;
218
219       GST_BUFFER_PTS (out_buf) = ts;
220
221       gst_buffer_map (out_buf, &map, GST_MAP_WRITE);
222
223       /* The header is the bottom 30 bits of the PCR, apparently not
224        * encoded into base + ext as in the packets themselves */
225       GST_WRITE_UINT32_BE (map.data, cur_pcr & 0x3FFFFFFF);
226       gst_buffer_unmap (out_buf, &map);
227
228       GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
229           G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, cur_pcr);
230       ((GstBaseTsMuxClass *)
231           parent_class)->output_packet (GST_BASE_TS_MUX (mux), out_buf, -1);
232     }
233   }
234
235   if (G_UNLIKELY (!buf))
236     goto exit;
237
238   gst_buffer_map (buf, &map, GST_MAP_WRITE);
239
240   /* Finally, output the passed in packet */
241   /* Only write the bottom 30 bits of the PCR */
242   GST_WRITE_UINT32_BE (map.data, new_pcr & 0x3FFFFFFF);
243
244   gst_buffer_unmap (buf, &map);
245
246   GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
247       G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, new_pcr);
248
249   ((GstBaseTsMuxClass *) parent_class)->output_packet (GST_BASE_TS_MUX (mux),
250       buf, -1);
251
252   if (new_pcr != mux->previous_pcr) {
253     mux->previous_pcr = new_pcr;
254     mux->previous_offset = -M2TS_PACKET_LENGTH;
255   }
256
257 exit:
258   return TRUE;
259 }
260
261 /* GstBaseTsMux implementation */
262
263 static void
264 gst_mpeg_ts_mux_allocate_packet (GstBaseTsMux * mux, GstBuffer ** buffer)
265 {
266   ((GstBaseTsMuxClass *) parent_class)->allocate_packet (mux, buffer);
267
268   gst_buffer_set_size (*buffer, GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH);
269 }
270
271 static gboolean
272 gst_mpeg_ts_mux_output_packet (GstBaseTsMux * base_tsmux, GstBuffer * buffer,
273     gint64 new_pcr)
274 {
275   GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
276   GstMapInfo map;
277
278   if (!mux->m2ts_mode)
279     return ((GstBaseTsMuxClass *) parent_class)->output_packet (base_tsmux,
280         buffer, new_pcr);
281
282   gst_buffer_set_size (buffer, M2TS_PACKET_LENGTH);
283
284   gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
285
286   /* there should be a better way to do this */
287   memmove (map.data + 4, map.data, map.size - 4);
288
289   gst_buffer_unmap (buffer, &map);
290
291   return new_packet_m2ts (mux, buffer, new_pcr);
292 }
293
294 static void
295 gst_mpeg_ts_mux_reset (GstBaseTsMux * base_tsmux)
296 {
297   GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
298
299   if (mux->adapter)
300     gst_adapter_clear (mux->adapter);
301
302   mux->previous_pcr = -1;
303   mux->previous_offset = 0;
304   mux->pcr_rate_num = mux->pcr_rate_den = 1;
305 }
306
307 static gboolean
308 gst_mpeg_ts_mux_drain (GstBaseTsMux * mux)
309 {
310   return new_packet_m2ts (GST_MPEG_TS_MUX (mux), NULL, -1);
311 }
312
313 /* GObject implementation */
314
315 static void
316 gst_mpeg_ts_mux_set_property (GObject * object, guint prop_id,
317     const GValue * value, GParamSpec * pspec)
318 {
319   GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
320
321   switch (prop_id) {
322     case PROP_M2TS_MODE:
323       /* set in case if the output stream need to be of 192 bytes */
324       mux->m2ts_mode = g_value_get_boolean (value);
325       gst_base_ts_mux_set_packet_size (GST_BASE_TS_MUX (mux),
326           mux->m2ts_mode ? M2TS_PACKET_LENGTH :
327           GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH);
328       gst_base_ts_mux_set_automatic_alignment (GST_BASE_TS_MUX (mux),
329           mux->m2ts_mode ? 32 : 0);
330       break;
331     default:
332       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
333       break;
334   }
335 }
336
337 static void
338 gst_mpeg_ts_mux_get_property (GObject * object, guint prop_id,
339     GValue * value, GParamSpec * pspec)
340 {
341   GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
342
343   switch (prop_id) {
344     case PROP_M2TS_MODE:
345       g_value_set_boolean (value, mux->m2ts_mode);
346       break;
347     default:
348       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
349       break;
350   }
351 }
352
353 static void
354 gst_mpeg_ts_mux_dispose (GObject * object)
355 {
356   GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
357
358   if (mux->adapter) {
359     g_object_unref (mux->adapter);
360     mux->adapter = NULL;
361   }
362
363   GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
364 }
365
366 static void
367 gst_mpeg_ts_mux_class_init (GstMpegTsMuxClass * klass)
368 {
369   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
370   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
371   GstBaseTsMuxClass *base_tsmux_class = GST_BASE_TS_MUX_CLASS (klass);
372
373   GST_DEBUG_CATEGORY_INIT (gst_mpeg_ts_mux_debug, "mpegtsmux", 0,
374       "MPEG Transport Stream muxer");
375
376   gobject_class->set_property =
377       GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_set_property);
378   gobject_class->get_property =
379       GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_get_property);
380   gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_dispose);
381
382   base_tsmux_class->allocate_packet =
383       GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_allocate_packet);
384   base_tsmux_class->output_packet =
385       GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_output_packet);
386   base_tsmux_class->reset = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_reset);
387   base_tsmux_class->drain = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_drain);
388
389   gst_element_class_set_static_metadata (gstelement_class,
390       "MPEG Transport Stream Muxer", "Codec/Muxer",
391       "Multiplexes media streams into an MPEG Transport Stream",
392       "Fluendo <contact@fluendo.com>");
393
394   gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
395       &gst_mpeg_ts_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
396
397   gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
398       &gst_mpeg_ts_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
399
400   g_object_class_install_property (gobject_class, PROP_M2TS_MODE,
401       g_param_spec_boolean ("m2ts-mode", "M2TS(192 bytes) Mode",
402           "Set to TRUE to output Blu-Ray disc format with 192 byte packets. "
403           "FALSE for standard TS format with 188 byte packets.",
404           MPEGTSMUX_DEFAULT_M2TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405 }
406
407 static void
408 gst_mpeg_ts_mux_init (GstMpegTsMux * mux)
409 {
410   mux->m2ts_mode = MPEGTSMUX_DEFAULT_M2TS;
411   mux->adapter = gst_adapter_new ();
412 }