1 /* MPEG-PS muxer plugin for GStreamer
2 * Copyright 2008 Lin YANG <oxcsnicho@gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 * Unless otherwise indicated, Source Code is licensed under MIT license.
21 * See further explanation attached in License Statement (distributed in the file
24 * Permission is hereby granted, free of charge, to any person obtaining a copy of
25 * this software and associated documentation files (the "Software"), to deal in
26 * the Software without restriction, including without limitation the rights to
27 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
28 * of the Software, and to permit persons to whom the Software is furnished to do
29 * so, subject to the following conditions:
31 * The above copyright notice and this permission notice shall be included in all
32 * copies or substantial portions of the Software.
34 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
49 #include "psmuxcommon.h"
50 #include "psmuxstream.h"
53 static guint8 psmux_stream_pes_header_length (PsMuxStream * stream);
54 static void psmux_stream_write_pes_header (PsMuxStream * stream, guint8 * data);
55 static void psmux_stream_find_pts_dts_within (PsMuxStream * stream, guint bound,
56 gint64 * pts, gint64 * dts);
61 * @stream_type: the stream type
63 * Create a new stream with type = @stream_type, assign stream id accordingly
65 * Returns: a new #PsMuxStream.
68 psmux_stream_new (PsMux * mux, PsMuxStreamType stream_type)
70 PsMuxStream *stream = g_slice_new0 (PsMuxStream);
71 PsMuxStreamIdInfo *info = &(mux->id_info);
73 stream->stream_type = stream_type;
74 stream->is_audio_stream = FALSE;
75 stream->is_video_stream = FALSE;
76 stream->stream_id = 0;
77 stream->max_buffer_size = 0;
79 switch (stream_type) {
81 case PSMUX_ST_AUDIO_MPEG1:
82 case PSMUX_ST_AUDIO_MPEG2:
83 stream->max_buffer_size = 2484; /* ISO/IEC 13818 2.5.2.4 */
84 case PSMUX_ST_AUDIO_AAC:
85 if (info->id_mpga > PSMUX_STREAM_ID_MPGA_MAX)
87 stream->stream_id = info->id_mpga++;
88 stream->stream_id_ext = 0;
89 stream->is_audio_stream = TRUE;
92 case PSMUX_ST_VIDEO_MPEG1:
93 case PSMUX_ST_VIDEO_MPEG2:
94 case PSMUX_ST_VIDEO_MPEG4:
95 case PSMUX_ST_VIDEO_H264:
96 if (info->id_mpgv > PSMUX_STREAM_ID_MPGV_MAX)
98 stream->stream_id = info->id_mpgv++;
99 stream->stream_id_ext = 0;
100 stream->is_video_stream = TRUE;
103 case PSMUX_ST_PS_AUDIO_AC3:
104 if (info->id_ac3 > PSMUX_STREAM_ID_AC3_MAX)
106 stream->stream_id = PSMUX_PRIVATE_STREAM_1;
107 stream->stream_id_ext = info->id_ac3++;
108 stream->is_audio_stream = TRUE;
109 /* AC3 requires data alignment */
110 stream->pi.flags |= PSMUX_PACKET_FLAG_PES_DATA_ALIGN;
112 /* TODO: SPU missing */
115 if (info->id_spu > PSMUX_STREAM_ID_SPU_MAX)
117 return info->id_spu++;
120 case PSMUX_ST_PS_AUDIO_DTS:
121 if (info->id_dts > PSMUX_STREAM_ID_DTS_MAX)
123 stream->stream_id = PSMUX_PRIVATE_STREAM_1;
124 stream->stream_id_ext = info->id_dts++;
125 stream->is_audio_stream = TRUE;
128 case PSMUX_ST_PS_AUDIO_LPCM:
129 if (info->id_lpcm > PSMUX_STREAM_ID_LPCM_MAX)
131 stream->stream_id = PSMUX_PRIVATE_STREAM_1;
132 stream->stream_id_ext = info->id_lpcm++;
133 stream->is_audio_stream = TRUE;
135 case PSMUX_ST_VIDEO_DIRAC:
136 if (info->id_dirac > PSMUX_STREAM_ID_DIRAC_MAX)
138 stream->stream_id = PSMUX_EXTENDED_STREAM;
139 stream->stream_id_ext = info->id_dirac++;
140 stream->is_video_stream = TRUE;
143 g_critical ("Stream type 0x%0x not yet implemented", stream_type);
147 if (stream->stream_id == 0) {
148 g_critical ("Number of elementary streams of type %04x exceeds maximum",
149 stream->stream_type);
150 g_slice_free (PsMuxStream, stream);
154 /* XXX: Are private streams also using stream_id_ext? */
155 if (stream->stream_id == PSMUX_EXTENDED_STREAM)
156 stream->pi.flags |= PSMUX_PACKET_FLAG_PES_EXT_STREAMID;
158 /* Are these useful at all? */
159 if (stream->stream_id == PSMUX_PROGRAM_STREAM_MAP ||
160 stream->stream_id == PSMUX_PADDING_STREAM ||
161 stream->stream_id == PSMUX_PRIVATE_STREAM_2 ||
162 stream->stream_id == PSMUX_ECM ||
163 stream->stream_id == PSMUX_EMM ||
164 stream->stream_id == PSMUX_PROGRAM_STREAM_DIRECTORY ||
165 stream->stream_id == PSMUX_DSMCC_STREAM ||
166 stream->stream_id == PSMUX_ITU_T_H222_1_TYPE_E)
167 stream->pi.flags &= ~PSMUX_PACKET_FLAG_PES_FULL_HEADER;
169 stream->pi.flags |= PSMUX_PACKET_FLAG_PES_FULL_HEADER;
171 stream->buffers = NULL;
172 stream->bytes_avail = 0;
173 stream->cur_buffer = NULL;
174 stream->cur_buffer_consumed = 0;
176 stream->cur_pes_payload_size = 0;
180 stream->last_pts = -1;
182 /* These fields are set by gstreamer */
183 stream->audio_sampling = 0;
184 stream->audio_channels = 0;
185 stream->audio_bitrate = 0;
187 if (stream->max_buffer_size == 0) {
188 /* XXX: VLC'S VALUE. Better default? */
189 if (stream->is_video_stream)
190 stream->max_buffer_size = 400 * 1024;
191 else if (stream->is_audio_stream)
192 stream->max_buffer_size = 4 * 1024;
194 g_assert_not_reached ();
202 * @stream: a #PsMuxStream
204 * Free the resources of @stream.
207 psmux_stream_free (PsMuxStream * stream)
209 g_return_if_fail (stream != NULL);
211 if (psmux_stream_bytes_in_buffer (stream)) {
212 g_warning ("Freeing stream with data not yet processed");
214 g_slice_free (PsMuxStream, stream);
217 /* Advance the current packet stream position by len bytes.
218 * Mustn't consume more than available in the current packet */
220 psmux_stream_consume (PsMuxStream * stream, guint len)
222 g_assert (stream->cur_buffer != NULL);
223 g_assert (len <= stream->cur_buffer->map.size - stream->cur_buffer_consumed);
225 stream->cur_buffer_consumed += len;
226 stream->bytes_avail -= len;
228 if (stream->cur_buffer_consumed == 0)
231 if (stream->cur_buffer->pts != -1)
232 stream->last_pts = stream->cur_buffer->pts;
234 if (stream->cur_buffer_consumed == stream->cur_buffer->map.size) {
235 /* Current packet is completed, move along */
236 stream->buffers = g_list_delete_link (stream->buffers, stream->buffers);
238 gst_buffer_unmap (stream->cur_buffer->buf, &stream->cur_buffer->map);
239 gst_buffer_unref (stream->cur_buffer->buf);
240 g_slice_free (PsMuxStreamBuffer, stream->cur_buffer);
241 stream->cur_buffer = NULL;
247 * psmux_stream_bytes_in_buffer:
248 * @stream: a #PsMuxStream
250 * Calculate how much bytes are in the buffer.
252 * Returns: The number of bytes in the buffer.
255 psmux_stream_bytes_in_buffer (PsMuxStream * stream)
257 g_return_val_if_fail (stream != NULL, 0);
259 return stream->bytes_avail;
263 * psmux_stream_get_data:
264 * @stream: a #PsMuxStream
265 * @buf: a buffer to hold the result
266 * @len: the length of @buf
268 * Write a PES packet to @buf, up to @len bytes
270 * Returns: number of bytes having been written, 0 if error
273 psmux_stream_get_data (PsMuxStream * stream, guint8 * buf, guint len)
275 guint8 pes_hdr_length;
278 g_return_val_if_fail (stream != NULL, FALSE);
279 g_return_val_if_fail (buf != NULL, FALSE);
280 g_return_val_if_fail (len >= PSMUX_PES_MAX_HDR_LEN, FALSE);
282 stream->cur_pes_payload_size =
283 MIN (psmux_stream_bytes_in_buffer (stream), len - PSMUX_PES_MAX_HDR_LEN);
284 /* Note that we cannot make a better estimation of the header length for the
285 * time being; because the header length is dependent on whether we can find a
286 * timestamp in the upcomming buffers, which in turn depends on
287 * cur_pes_payload_size, which is exactly what we want to decide.
290 psmux_stream_find_pts_dts_within (stream, stream->cur_pes_payload_size,
291 &stream->pts, &stream->dts);
293 /* clear pts/dts flag */
294 stream->pi.flags &= ~(PSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS |
295 PSMUX_PACKET_FLAG_PES_WRITE_PTS);
296 /* update pts/dts flag */
297 if (stream->pts != -1 && stream->dts != -1)
298 stream->pi.flags |= PSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS;
300 if (stream->pts != -1)
301 stream->pi.flags |= PSMUX_PACKET_FLAG_PES_WRITE_PTS;
304 pes_hdr_length = psmux_stream_pes_header_length (stream);
306 /* write pes header */
307 GST_LOG ("Writing PES header of length %u and payload %d",
308 pes_hdr_length, stream->cur_pes_payload_size);
309 psmux_stream_write_pes_header (stream, buf);
311 buf += pes_hdr_length;
312 w = stream->cur_pes_payload_size; /* number of bytes of payload to write */
318 if (stream->cur_buffer == NULL) {
319 /* Start next packet */
320 if (stream->buffers == NULL)
322 stream->cur_buffer = (PsMuxStreamBuffer *) (stream->buffers->data);
323 stream->cur_buffer_consumed = 0;
326 /* Take as much as we can from the current buffer */
327 avail = stream->cur_buffer->map.size - stream->cur_buffer_consumed;
328 cur = stream->cur_buffer->map.data + stream->cur_buffer_consumed;
330 memcpy (buf, cur, avail);
331 psmux_stream_consume (stream, avail);
336 memcpy (buf, cur, w);
337 psmux_stream_consume (stream, w);
343 return pes_hdr_length + stream->cur_pes_payload_size;
347 psmux_stream_pes_header_length (PsMuxStream * stream)
351 /* Calculate the length of the header for this stream */
353 /* start_code prefix + stream_id + pes_packet_length = 6 bytes */
356 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_FULL_HEADER) {
357 /* For a PES 'full header' we have at least 3 more bytes,
358 * and then more based on flags */
360 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS) {
362 } else if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_WRITE_PTS) {
365 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_EXT_STREAMID) {
366 /* Need basic extension flags (1 byte), plus 2 more bytes for the
367 * length + extended stream id */
375 /* Find a PTS/DTS to write into the pes header within the next bound bytes
378 psmux_stream_find_pts_dts_within (PsMuxStream * stream, guint bound,
379 gint64 * pts, gint64 * dts)
381 /* 1. When there are at least one buffer then output the first buffer with
383 * 2. If the bound is too small to include even one buffer, output the pts/dts
391 for (cur = g_list_first (stream->buffers); cur != NULL;
392 cur = g_list_next (cur)) {
393 PsMuxStreamBuffer *curbuf = cur->data;
395 /* FIXME: This isn't quite correct - if the 'bound' is within this
396 * buffer, we don't know if the timestamp is before or after the split
397 * so we shouldn't return it */
398 if (bound <= curbuf->map.size) {
404 /* Have we found a buffer with pts/dts set? */
405 if (curbuf->pts != -1 || curbuf->dts != -1) {
411 bound -= curbuf->map.size;
416 psmux_stream_write_pes_header (PsMuxStream * stream, guint8 * data)
418 guint16 length_to_write;
419 guint8 hdr_len = psmux_stream_pes_header_length (stream);
421 /* start_code prefix + stream_id + pes_packet_length = 6 bytes */
425 data[3] = stream->stream_id;
428 length_to_write = hdr_len - 6 + stream->cur_pes_payload_size;
430 psmux_put16 (&data, length_to_write);
432 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_FULL_HEADER) {
435 /* Not scrambled, original, not-copyrighted, data_alignment specified by flag */
437 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_DATA_ALIGN)
438 flags |= 0x04; /* Enable data_alignment_indicator */
443 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS)
445 else if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_WRITE_PTS)
447 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_EXT_STREAMID)
448 flags |= 0x01; /* Enable PES_extension_flag */
451 /* Header length is the total pes length,
452 * minus the 9 bytes of start codes, flags + hdr_len */
453 g_return_if_fail (hdr_len >= 9);
454 *data++ = (hdr_len - 9);
456 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS) {
457 psmux_put_ts (&data, 0x3, stream->pts);
458 psmux_put_ts (&data, 0x1, stream->dts);
459 } else if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_WRITE_PTS) {
460 psmux_put_ts (&data, 0x2, stream->pts);
463 if (stream->pi.flags & PSMUX_PACKET_FLAG_PES_EXT_STREAMID) {
466 flags = 0x0f; /* preceeding flags all 0 | (reserved bits) | PES_extension_flag_2 */
469 ext_len = 1; /* Only writing 1 byte into the extended fields */
470 *data++ = 0x80 | ext_len; /* marker | PES_extension_field_length */
471 *data++ = 0x80 | stream->stream_id_ext; /* stream_id_extension_flag | extended_stream_id */
477 * psmux_stream_add_data:
478 * @stream: a #PsMuxStream
479 * @buffer: (transfer full): buffer with data to add
480 * @pts: PTS of access unit in @data
481 * @dts: DTS of access unit in @data
483 * Submit @len bytes of @data into @stream. @pts and @dts can be set to the
484 * timestamp (against a 90Hz clock) of the first access unit in @data. A
485 * timestamp of -1 for @pts or @dts means unknown.
487 * This function takes ownership of @buffer.
490 psmux_stream_add_data (PsMuxStream * stream, GstBuffer * buffer,
491 gint64 pts, gint64 dts, gboolean keyunit)
493 PsMuxStreamBuffer *packet;
495 g_return_if_fail (stream != NULL);
497 packet = g_slice_new (PsMuxStreamBuffer);
498 packet->buf = buffer;
500 if (!gst_buffer_map (packet->buf, &packet->map, GST_MAP_READ)) {
501 GST_ERROR ("Failed to map buffer for reading");
502 gst_buffer_unref (packet->buf);
503 g_slice_free (PsMuxStreamBuffer, packet);
507 packet->keyunit = keyunit;
511 if (stream->bytes_avail == 0)
512 stream->last_pts = pts;
514 stream->bytes_avail += packet->map.size;
515 /* FIXME: perhaps use GstQueueArray instead? */
516 stream->buffers = g_list_append (stream->buffers, packet);
521 * psmux_stream_get_es_descrs:
522 * @stream: a #PsMuxStream
523 * @buf: a buffer to hold the ES descriptor
524 * @len: the length used in @buf
526 * Write an Elementary Stream Descriptor for @stream into @buf. the number of
527 * bytes consumed in @buf will be updated in @len.
529 * @buf and @len must be at least #PSMUX_MIN_ES_DESC_LEN.
532 psmux_stream_get_es_descrs (PsMuxStream * stream, guint8 * buf, guint16 * len)
536 g_return_if_fail (stream != NULL);
544 /* Based on the stream type, write out any descriptors to go in the
545 * PMT ES_info field */
548 /* tag (registration_descriptor), length, format_identifier */
549 switch (stream->stream_type) {
550 case PSMUX_ST_AUDIO_AAC:
553 case PSMUX_ST_VIDEO_MPEG4:
556 case PSMUX_ST_VIDEO_H264:
559 *pos++ = 0x48; /* 'H' */
560 *pos++ = 0x44; /* 'D' */
561 *pos++ = 0x4D; /* 'M' */
562 *pos++ = 0x56; /* 'V' */
563 /* FIXME : Not sure about this additional_identification_info */
569 case PSMUX_ST_VIDEO_DIRAC:
572 *pos++ = 0x64; /* 'd' */
573 *pos++ = 0x72; /* 'r' */
574 *pos++ = 0x61; /* 'a' */
575 *pos++ = 0x63; /* 'c' */
577 case PSMUX_ST_PS_AUDIO_AC3:
581 *pos++ = 0x41; /* 'A' */
582 *pos++ = 0x43; /* 'C' */
583 *pos++ = 0x2D; /* '-' */
584 *pos++ = 0x33; /* '3' */
586 /* audio_stream_descriptor () | ATSC A/52-2001 Annex A
588 * descriptor_tag 8 uimsbf
589 * descriptor_length 8 uimsbf
590 * sample_rate_code 3 bslbf
592 * bit_rate_code 6 bslbf
593 * surround_mode 2 bslbf
595 * num_channels 4 bslbf
603 /* 3 bits sample_rate_code, 5 bits hardcoded bsid (default ver 8) */
604 switch (stream->audio_sampling) {
616 break; /* 48, 44.1 or 32 Khz */
619 /* 1 bit bit_rate_limit, 5 bits bit_rate_code, 2 bits suround_mode */
620 switch (stream->audio_bitrate) {
680 break; /* 640 Kb/s upper limit */
683 /* 3 bits bsmod, 4 bits num_channels, 1 bit full_svc */
684 switch (stream->audio_channels) {
710 case PSMUX_ST_PS_AUDIO_DTS:
713 case PSMUX_ST_PS_AUDIO_LPCM:
725 * psmux_stream_get_pts:
726 * @stream: a #PsMuxStream
728 * Return the PTS of the last buffer that has had bytes written and
729 * which _had_ a PTS in @stream.
731 * Returns: the PTS of the last buffer in @stream.
734 psmux_stream_get_pts (PsMuxStream * stream)
736 g_return_val_if_fail (stream != NULL, -1);
738 return stream->last_pts;