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 }}
80 #include "gstmpegtsmux.h"
83 #define MPEGTSMUX_DEFAULT_M2TS FALSE
85 #define M2TS_PACKET_LENGTH 192
93 static GstStaticPadTemplate gst_mpeg_ts_mux_sink_factory =
94 GST_STATIC_PAD_TEMPLATE ("sink_%d",
97 GST_STATIC_CAPS ("video/mpeg, "
98 "parsed = (boolean) TRUE, "
99 "mpegversion = (int) { 1, 2, 4 }, "
100 "systemstream = (boolean) false; "
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}; "
108 "parsed = (boolean) TRUE, "
109 "mpegversion = (int) 1;"
111 "framed = (boolean) TRUE, "
112 "mpegversion = (int) {2, 4}, stream-format = (string) { adts, raw };"
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;"
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];"));
128 static GstStaticPadTemplate gst_mpeg_ts_mux_src_factory =
129 GST_STATIC_PAD_TEMPLATE ("src",
132 GST_STATIC_CAPS ("video/mpegts, "
133 "systemstream = (boolean) true, " "packetsize = (int) { 188, 192} ")
136 GST_DEBUG_CATEGORY (gst_mpeg_ts_mux_debug);
137 #define GST_CAT_DEFAULT gst_mpeg_ts_mux_debug
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 ());
147 new_packet_m2ts (GstMpegTsMux * mux, GstBuffer * buf, gint64 new_pcr)
153 GST_LOG_OBJECT (mux, "Have buffer %p with new_pcr=%" G_GINT64_FORMAT,
156 chunk_bytes = gst_adapter_available (mux->adapter);
158 if (G_LIKELY (buf)) {
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);
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);
177 g_assert (new_pcr == -1);
180 /* interpolate if needed, and 2 points available */
181 if (chunk_bytes && (new_pcr != mux->previous_pcr)) {
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);
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;
197 while (offset < chunk_bytes) {
200 /* Loop, pulling packets of the adapter, updating their 4 byte
201 * timestamp header and pushing */
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);
209 cur_pcr = mux->previous_pcr -
210 gst_util_uint64_scale (mux->previous_offset - offset,
211 mux->pcr_rate_num, mux->pcr_rate_den);
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);
217 offset += M2TS_PACKET_LENGTH;
219 GST_BUFFER_PTS (out_buf) = ts;
221 gst_buffer_map (out_buf, &map, GST_MAP_WRITE);
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);
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);
235 if (G_UNLIKELY (!buf))
238 gst_buffer_map (buf, &map, GST_MAP_WRITE);
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);
244 gst_buffer_unmap (buf, &map);
246 GST_LOG_OBJECT (mux, "Outputting a packet of length %d PCR %"
247 G_GUINT64_FORMAT, M2TS_PACKET_LENGTH, new_pcr);
249 ((GstBaseTsMuxClass *) parent_class)->output_packet (GST_BASE_TS_MUX (mux),
252 if (new_pcr != mux->previous_pcr) {
253 mux->previous_pcr = new_pcr;
254 mux->previous_offset = -M2TS_PACKET_LENGTH;
261 /* GstBaseTsMux implementation */
264 gst_mpeg_ts_mux_allocate_packet (GstBaseTsMux * mux, GstBuffer ** buffer)
266 ((GstBaseTsMuxClass *) parent_class)->allocate_packet (mux, buffer);
268 gst_buffer_set_size (*buffer, GST_BASE_TS_MUX_NORMAL_PACKET_LENGTH);
272 gst_mpeg_ts_mux_output_packet (GstBaseTsMux * base_tsmux, GstBuffer * buffer,
275 GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
279 return ((GstBaseTsMuxClass *) parent_class)->output_packet (base_tsmux,
282 gst_buffer_set_size (buffer, M2TS_PACKET_LENGTH);
284 gst_buffer_map (buffer, &map, GST_MAP_READWRITE);
286 /* there should be a better way to do this */
287 memmove (map.data + 4, map.data, map.size - 4);
289 gst_buffer_unmap (buffer, &map);
291 return new_packet_m2ts (mux, buffer, new_pcr);
295 gst_mpeg_ts_mux_reset (GstBaseTsMux * base_tsmux)
297 GstMpegTsMux *mux = GST_MPEG_TS_MUX (base_tsmux);
300 gst_adapter_clear (mux->adapter);
302 mux->previous_pcr = -1;
303 mux->previous_offset = 0;
304 mux->pcr_rate_num = mux->pcr_rate_den = 1;
308 gst_mpeg_ts_mux_drain (GstBaseTsMux * mux)
310 return new_packet_m2ts (GST_MPEG_TS_MUX (mux), NULL, -1);
313 /* GObject implementation */
316 gst_mpeg_ts_mux_set_property (GObject * object, guint prop_id,
317 const GValue * value, GParamSpec * pspec)
319 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
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);
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
338 gst_mpeg_ts_mux_get_property (GObject * object, guint prop_id,
339 GValue * value, GParamSpec * pspec)
341 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
345 g_value_set_boolean (value, mux->m2ts_mode);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gst_mpeg_ts_mux_dispose (GObject * object)
356 GstMpegTsMux *mux = GST_MPEG_TS_MUX (object);
359 g_object_unref (mux->adapter);
363 GST_CALL_PARENT (G_OBJECT_CLASS, dispose, (object));
367 gst_mpeg_ts_mux_class_init (GstMpegTsMuxClass * klass)
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);
373 GST_DEBUG_CATEGORY_INIT (gst_mpeg_ts_mux_debug, "mpegtsmux", 0,
374 "MPEG Transport Stream muxer");
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);
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);
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>");
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);
397 gst_element_class_add_static_pad_template_with_gtype (gstelement_class,
398 &gst_mpeg_ts_mux_src_factory, GST_TYPE_AGGREGATOR_PAD);
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));
408 gst_mpeg_ts_mux_init (GstMpegTsMux * mux)
410 mux->m2ts_mode = MPEGTSMUX_DEFAULT_M2TS;
411 mux->adapter = gst_adapter_new ();