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>
7 * Copyright (C) 2011 Jan Schmidt <thaytan@noraisin.net>
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.
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/.
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
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.
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.
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.
44 * Unless otherwise indicated, Source Code is licensed under MIT license.
45 * See further explanation attached in License Statement (distributed in the file
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:
55 * The above copyright notice and this permission notice shall be included in all
56 * copies or substantial portions of the Software.
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
66 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
70 * SECTION: element-mpegtsmux
71 * @title: MPEG Transport Stream muxer
73 * mpegtsmux muxes different streams into an MPEG Transport stream
75 * SI sections can be specified through a custom event:
77 * {{ tests/examples/mpegts/ts-section-writer.c }}
83 #include "gstmpegtsmux.h"
86 #define MPEGTSMUX_DEFAULT_M2TS FALSE
88 #define M2TS_PACKET_LENGTH 192
96 static GstStaticPadTemplate gst_mpeg_ts_mux_sink_factory =
97 GST_STATIC_PAD_TEMPLATE ("sink_%d",
100 GST_STATIC_CAPS ("video/mpeg, "
101 "parsed = (boolean) TRUE, "
102 "mpegversion = (int) { 1, 2, 4 }, "
103 "systemstream = (boolean) false; "
105 "image/x-jpc, alignment = (string) frame;"
106 "video/x-h264,stream-format=(string)byte-stream,"
107 "alignment=(string){au, nal}; "
108 "video/x-h265,stream-format=(string)byte-stream,"
109 "alignment=(string){au, nal}; "
111 "parsed = (boolean) TRUE, "
112 "mpegversion = (int) 1;"
114 "framed = (boolean) TRUE, "
115 "mpegversion = (int) {2, 4}, stream-format = (string) { adts, raw };"
117 "width = (int) { 16, 20, 24 }, "
118 "rate = (int) { 48000, 96000 }, "
119 "channels = (int) [ 1, 8 ], "
120 "dynamic_range = (int) [ 0, 255 ], "
121 "emphasis = (boolean) { FALSE, TRUE }, "
122 "mute = (boolean) { FALSE, TRUE }; "
123 "audio/x-ac3, framed = (boolean) TRUE;"
124 "audio/x-dts, framed = (boolean) TRUE;"
126 "channels = (int) [1, 8], "
127 "channel-mapping-family = (int) {0, 1};"
128 "subpicture/x-dvb; application/x-teletext; meta/x-klv, parsed=true;"
129 "image/x-jpc, alignment = (string) frame, profile = (int)[0, 49151];"));
131 static GstStaticPadTemplate gst_mpeg_ts_mux_src_factory =
132 GST_STATIC_PAD_TEMPLATE ("src",
135 GST_STATIC_CAPS ("video/mpegts, "
136 "systemstream = (boolean) true, " "packetsize = (int) { 188, 192} ")
139 GST_DEBUG_CATEGORY (gst_mpeg_ts_mux_debug);
140 #define GST_CAT_DEFAULT gst_mpeg_ts_mux_debug
142 #define parent_class gst_mpeg_ts_mux_parent_class
143 G_DEFINE_TYPE (GstMpegTsMux, gst_mpeg_ts_mux, GST_TYPE_BASE_TS_MUX);
144 GST_ELEMENT_REGISTER_DEFINE (mpegtsmux, "mpegtsmux", GST_RANK_PRIMARY,
145 gst_mpeg_ts_mux_get_type ());
150 new_packet_m2ts (GstMpegTsMux * mux, GstBuffer * buf, gint64 new_pcr)
156 GST_LOG_OBJECT (mux, "Have buffer %p with new_pcr=%" G_GINT64_FORMAT,
159 chunk_bytes = gst_adapter_available (mux->adapter);
161 if (G_LIKELY (buf)) {
163 /* If there is no pcr in current ts packet then just add the packet
164 to the adapter for later output when we see a PCR */
165 GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
166 gst_adapter_push (mux->adapter, buf);
170 /* no first interpolation point yet, then this is the one,
171 * otherwise it is the second interpolation point */
172 if (mux->previous_pcr < 0 && chunk_bytes) {
173 mux->previous_pcr = new_pcr;
174 mux->previous_offset = chunk_bytes;
175 GST_LOG_OBJECT (mux, "Accumulating non-PCR packet");
176 gst_adapter_push (mux->adapter, buf);
180 g_assert (new_pcr == -1);
183 /* interpolate if needed, and 2 points available */
184 if (chunk_bytes && (new_pcr != mux->previous_pcr)) {
187 GST_LOG_OBJECT (mux, "Processing pending packets; "
188 "previous pcr %" G_GINT64_FORMAT ", previous offset %d, "
189 "current pcr %" G_GINT64_FORMAT ", current offset %d",
190 mux->previous_pcr, (gint) mux->previous_offset,
191 new_pcr, (gint) chunk_bytes);
193 g_assert (chunk_bytes > mux->previous_offset);
194 /* if draining, use previous rate */
195 if (G_LIKELY (new_pcr > 0)) {
196 mux->pcr_rate_num = new_pcr - mux->previous_pcr;
197 mux->pcr_rate_den = chunk_bytes - mux->previous_offset;
200 while (offset < chunk_bytes) {
203 /* Loop, pulling packets of the adapter, updating their 4 byte
204 * timestamp header and pushing */
206 /* interpolate PCR */
207 if (G_LIKELY (offset >= mux->previous_offset))
208 cur_pcr = mux->previous_pcr +
209 gst_util_uint64_scale (offset - mux->previous_offset,
210 mux->pcr_rate_num, mux->pcr_rate_den);
212 cur_pcr = mux->previous_pcr -
213 gst_util_uint64_scale (mux->previous_offset - offset,
214 mux->pcr_rate_num, mux->pcr_rate_den);
216 /* FIXME: what about DTS here? */
217 ts = gst_adapter_prev_pts (mux->adapter, NULL);
218 out_buf = gst_adapter_take_buffer (mux->adapter, M2TS_PACKET_LENGTH);
220 offset += M2TS_PACKET_LENGTH;
222 GST_BUFFER_PTS (out_buf) = ts;
224 gst_buffer_map (out_buf, &map, GST_MAP_WRITE);
226 /* The header is the bottom 30 bits of the PCR, apparently not
227 * encoded into base + ext as in the packets themselves */
228 GST_WRITE_UINT32_BE (map.data, cur_pcr & 0x3FFFFFFF);
229 gst_buffer_unmap (out_buf, &map);
231 GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
232 G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, cur_pcr);
233 ((GstBaseTsMuxClass *)
234 parent_class)->output_packet (GST_BASE_TS_MUX (mux), out_buf, -1);
238 if (G_UNLIKELY (!buf))
241 gst_buffer_map (buf, &map, GST_MAP_WRITE);
243 /* Finally, output the passed in packet */
244 /* Only write the bottom 30 bits of the PCR */
245 GST_WRITE_UINT32_BE (map.data, new_pcr & 0x3FFFFFFF);
247 gst_buffer_unmap (buf, &map);
249 GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
250 G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, new_pcr);
252 ((GstBaseTsMuxClass *) parent_class)->output_packet (GST_BASE_TS_MUX (mux),
255 if (new_pcr != mux->previous_pcr) {
256 mux->previous_pcr = new_pcr;
257 mux->previous_offset = -M2TS_PACKET_LENGTH;
264 /* GstBaseTsMux implementation */
267 gst_mpeg_ts_mux_allocate_packet (GstBaseTsMux * mux, GstBuffer ** buffer)
269 ((GstBaseTsMuxClass *) parent_class)->allocate_packet (mux, buffer);
271 gst_buffer_set_size (*buffer, GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH);
275 gst_mpeg_ts_mux_output_packet (GstBaseTsMux * base_tsmux, GstBuffer * buffer,
278 GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
282 return ((GstBaseTsMuxClass *) parent_class)->output_packet (base_tsmux,
285 gst_buffer_set_size (buffer, M2TS_PACKET_LENGTH);
287 gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
289 /* there should be a better way to do this */
290 memmove (map.data + 4, map.data, map.size - 4);
292 gst_buffer_unmap (buffer, &map);
294 return new_packet_m2ts (mux, buffer, new_pcr);
298 gst_mpeg_ts_mux_reset (GstBaseTsMux * base_tsmux)
300 GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
303 gst_adapter_clear (mux->adapter);
305 mux->previous_pcr = -1;
306 mux->previous_offset = 0;
307 mux->pcr_rate_num = mux->pcr_rate_den = 1;
311 gst_mpeg_ts_mux_drain (GstBaseTsMux * mux)
313 return new_packet_m2ts (GST_MPEG_TS_MUX (mux), NULL, -1);
316 /* GObject implementation */
319 gst_mpeg_ts_mux_set_property (GObject * object, guint prop_id,
320 const GValue * value, GParamSpec * pspec)
322 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
326 /* set in case if the output stream need to be of 192 bytes */
327 mux->m2ts_mode = g_value_get_boolean (value);
328 gst_base_ts_mux_set_packet_size (GST_BASE_TS_MUX (mux),
329 mux->m2ts_mode ? M2TS_PACKET_LENGTH :
330 GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH);
331 gst_base_ts_mux_set_automatic_alignment (GST_BASE_TS_MUX (mux),
332 mux->m2ts_mode ? 32 : 0);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 gst_mpeg_ts_mux_get_property (GObject * object, guint prop_id,
342 GValue * value, GParamSpec * pspec)
344 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
348 g_value_set_boolean (value, mux->m2ts_mode);
351 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
357 gst_mpeg_ts_mux_dispose (GObject * object)
359 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
362 g_object_unref (mux->adapter);
366 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
370 gst_mpeg_ts_mux_class_init (GstMpegTsMuxClass * klass)
372 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
373 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
374 GstBaseTsMuxClass *base_tsmux_class = GST_BASE_TS_MUX_CLASS (klass);
376 GST_DEBUG_CATEGORY_INIT (gst_mpeg_ts_mux_debug, "mpegtsmux", 0,
377 "MPEG Transport Stream muxer");
379 gobject_class->set_property =
380 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_set_property);
381 gobject_class->get_property =
382 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_get_property);
383 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_dispose);
385 base_tsmux_class->allocate_packet =
386 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_allocate_packet);
387 base_tsmux_class->output_packet =
388 GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_output_packet);
389 base_tsmux_class->reset = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_reset);
390 base_tsmux_class->drain = GST_DEBUG_FUNCPTR (gst_mpeg_ts_mux_drain);
392 gst_element_class_set_static_metadata (gstelement_class,
393 "MPEG Transport Stream Muxer", "Codec/Muxer",
394 "Multiplexes media streams into an MPEG Transport Stream",
395 "Fluendo <contact@fluendo.com>");
397 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
398 &gst_mpeg_ts_mux_sink_factory, GST_TYPE_BASE_TS_MUX_PAD);
400 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
401 &gst_mpeg_ts_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
403 g_object_class_install_property (gobject_class, PROP_M2TS_MODE,
404 g_param_spec_boolean ("m2ts-mode", "M2TS(192 bytes) Mode",
405 "Set to TRUE to output Blu-Ray disc format with 192 byte packets. "
406 "FALSE for standard TS format with 188 byte packets.",
407 MPEGTSMUX_DEFAULT_M2TS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
411 gst_mpeg_ts_mux_init (GstMpegTsMux * mux)
413 mux->m2ts_mode = MPEGTSMUX_DEFAULT_M2TS;
414 mux->adapter = gst_adapter_new ();