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