2 * Copyright 2006 BBC and Fluendo S.A.
4 * This library is licensed under 4 different licenses and you
5 * can choose to use it under the terms of any one of them. The
6 * four licenses are the MPL 1.1, the LGPL, the GPL and the MIT
11 * The contents of this file are subject to the Mozilla Public License
12 * Version 1.1 (the "License"); you may not use this file except in
13 * compliance with the License. You may obtain a copy of the License at
14 * http://www.mozilla.org/MPL/.
16 * Software distributed under the License is distributed on an "AS IS"
17 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 * License for the specific language governing rights and limitations
23 * This library is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU Library General Public
25 * License as published by the Free Software Foundation; either
26 * version 2 of the License, or (at your option) any later version.
28 * This library is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
31 * Library General Public License for more details.
33 * You should have received a copy of the GNU Library General Public
34 * License along with this library; if not, write to the
35 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
36 * Boston, MA 02111-1307, USA.
40 * This program is free software; you can redistribute it and/or modify
41 * it under the terms of the GNU General Public License as published by
42 * the Free Software Foundation; either version 2 of the License, or
43 * (at your option) any later version.
45 * This program is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU General Public License for more details.
50 * You should have received a copy of the GNU General Public License
51 * along with this program; if not, write to the Free Software
52 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
56 * Unless otherwise indicated, Source Code is licensed under MIT license.
57 * See further explanation attached in License Statement (distributed in the file
60 * Permission is hereby granted, free of charge, to any person obtaining a copy of
61 * this software and associated documentation files (the "Software"), to deal in
62 * the Software without restriction, including without limitation the rights to
63 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
64 * of the Software, and to permit persons to whom the Software is furnished to do
65 * so, subject to the following conditions:
67 * The above copyright notice and this permission notice shall be included in all
68 * copies or substantial portions of the Software.
70 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
71 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
72 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
73 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
74 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
75 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
86 #include "tsmuxcommon.h"
87 #include "tsmuxstream.h"
89 static guint8 tsmux_stream_pes_header_length (TsMuxStream * stream);
90 static void tsmux_stream_write_pes_header (TsMuxStream * stream, guint8 * data);
91 static void tsmux_stream_find_pts_dts_within (TsMuxStream * stream, guint bound,
92 gint64 * pts, gint64 * dts);
94 struct TsMuxStreamBuffer
99 /* PTS & DTS associated with the contents of this buffer */
109 * @stream_type: the stream type
111 * Create a new stream with PID of @pid and @stream_type.
113 * Returns: a new #TsMuxStream.
116 tsmux_stream_new (guint16 pid, TsMuxStreamType stream_type)
118 TsMuxStream *stream = g_slice_new0 (TsMuxStream);
120 stream->state = TSMUX_STREAM_STATE_HEADER;
121 stream->pi.pid = pid;
122 stream->stream_type = stream_type;
124 stream->pes_payload_size = 0;
125 stream->cur_pes_payload_size = 0;
126 stream->pes_bytes_written = 0;
128 switch (stream_type) {
129 case TSMUX_ST_VIDEO_MPEG1:
130 case TSMUX_ST_VIDEO_MPEG2:
131 case TSMUX_ST_VIDEO_MPEG4:
132 case TSMUX_ST_VIDEO_H264:
133 /* FIXME: Assign sequential IDs? */
135 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
136 stream->is_video_stream = TRUE;
138 case TSMUX_ST_AUDIO_AAC:
139 case TSMUX_ST_AUDIO_MPEG1:
140 case TSMUX_ST_AUDIO_MPEG2:
141 /* FIXME: Assign sequential IDs? */
143 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
145 case TSMUX_ST_VIDEO_DIRAC:
146 case TSMUX_ST_PS_AUDIO_LPCM:
147 case TSMUX_ST_PS_AUDIO_AC3:
148 case TSMUX_ST_PS_AUDIO_DTS:
150 /* FIXME: assign sequential extended IDs? */
151 switch (stream_type) {
152 case TSMUX_ST_VIDEO_DIRAC:
153 stream->id_extended = 0x60;
154 stream->is_video_stream = TRUE;
156 case TSMUX_ST_PS_AUDIO_LPCM:
157 stream->id_extended = 0x80;
159 case TSMUX_ST_PS_AUDIO_AC3:
160 stream->id_extended = 0x71;
162 case TSMUX_ST_PS_AUDIO_DTS:
163 stream->id_extended = 0x82;
169 TSMUX_PACKET_FLAG_PES_FULL_HEADER |
170 TSMUX_PACKET_FLAG_PES_EXT_STREAMID;
173 g_critical ("Stream type 0x%0x not yet implemented", stream_type);
177 stream->last_pts = -1;
178 stream->last_dts = -1;
181 stream->last_pcr = -1;
187 * tsmux_stream_get_pid:
188 * @stream: a #TsMuxStream
190 * Get the PID of @stream.
192 * Returns: The PID of @stream. 0xffff on error.
195 tsmux_stream_get_pid (TsMuxStream * stream)
197 g_return_val_if_fail (stream != NULL, G_MAXUINT16);
199 return stream->pi.pid;
204 * @stream: a #TsMuxStream
206 * Free the resources of @stream.
209 tsmux_stream_free (TsMuxStream * stream)
211 g_return_if_fail (stream != NULL);
213 g_slice_free (TsMuxStream, stream);
217 * tsmux_stream_set_buffer_release_func:
218 * @stream: a #TsMuxStream
219 * @func: the new #TsMuxStreamBufferReleaseFunc
221 * Set the function that will be called when a a piece of data fed to @stream
222 * with tsmux_stream_add_data() can be freed. @func will be called with user
223 * data as provided with the call to tsmux_stream_add_data().
226 tsmux_stream_set_buffer_release_func (TsMuxStream * stream,
227 TsMuxStreamBufferReleaseFunc func)
229 g_return_if_fail (stream != NULL);
231 stream->buffer_release = func;
234 /* Advance the current packet stream position by len bytes.
235 * Mustn't consume more than available in the current packet */
237 tsmux_stream_consume (TsMuxStream * stream, guint len)
239 g_assert (stream->cur_buffer != NULL);
240 g_assert (len <= stream->cur_buffer->size - stream->cur_buffer_consumed);
242 stream->cur_buffer_consumed += len;
243 stream->bytes_avail -= len;
245 if (stream->cur_buffer_consumed == 0)
248 if (stream->cur_buffer->pts != -1) {
249 stream->last_pts = stream->cur_buffer->pts;
250 stream->last_dts = stream->cur_buffer->dts;
251 } else if (stream->cur_buffer->dts != -1)
252 stream->last_dts = stream->cur_buffer->dts;
254 if (stream->cur_buffer_consumed == stream->cur_buffer->size) {
255 /* Current packet is completed, move along */
256 stream->buffers = g_list_delete_link (stream->buffers, stream->buffers);
258 if (stream->buffer_release) {
259 stream->buffer_release (stream->cur_buffer->data,
260 stream->cur_buffer->user_data);
263 g_slice_free (TsMuxStreamBuffer, stream->cur_buffer);
264 stream->cur_buffer = NULL;
265 /* FIXME: As a hack, for unbounded streams, start a new PES packet for each
266 * incoming packet we receive. This assumes that incoming data is
267 * packetised sensibly - ie, every video frame */
268 if (stream->cur_pes_payload_size == 0)
269 stream->state = TSMUX_STREAM_STATE_HEADER;
274 * tsmux_stream_at_pes_start:
275 * @stream: a #TsMuxStream
277 * Check if @stream is at the start of a PES packet.
279 * Returns: TRUE if @stream is at a PES header packet.
282 tsmux_stream_at_pes_start (TsMuxStream * stream)
284 g_return_val_if_fail (stream != NULL, FALSE);
286 return stream->state == TSMUX_STREAM_STATE_HEADER;
290 * tsmux_stream_bytes_avail:
291 * @stream: a #TsMuxStream
293 * Calculate how much bytes are available.
295 * Returns: The number of bytes available.
298 tsmux_stream_bytes_avail (TsMuxStream * stream)
302 g_return_val_if_fail (stream != NULL, 0);
304 if (stream->cur_pes_payload_size != 0)
305 bytes_avail = stream->cur_pes_payload_size - stream->pes_bytes_written;
307 bytes_avail = tsmux_stream_bytes_in_buffer (stream);
309 bytes_avail = MIN (bytes_avail, tsmux_stream_bytes_in_buffer (stream));
311 /* Calculate the number of bytes available in the current PES */
312 if (stream->state == TSMUX_STREAM_STATE_HEADER)
313 bytes_avail += tsmux_stream_pes_header_length (stream);
319 * tsmux_stream_bytes_in_buffer:
320 * @stream: a #TsMuxStream
322 * Calculate how much bytes are in the buffer.
324 * Returns: The number of bytes in the buffer.
327 tsmux_stream_bytes_in_buffer (TsMuxStream * stream)
329 g_return_val_if_fail (stream != NULL, 0);
331 return stream->bytes_avail;
335 * tsmux_stream_initialize_pes_packet:
336 * @stream: a #TsMuxStream
338 * Initializes the PES packet.
340 * Returns: TRUE if we the packet was initialized.
343 tsmux_stream_initialize_pes_packet (TsMuxStream * stream)
345 if (stream->state != TSMUX_STREAM_STATE_HEADER)
348 if (stream->pes_payload_size != 0) {
349 /* Use prescribed fixed PES payload size */
350 stream->cur_pes_payload_size = stream->pes_payload_size;
351 tsmux_stream_find_pts_dts_within (stream, stream->cur_pes_payload_size,
352 &stream->pts, &stream->dts);
353 } else if (stream->is_video_stream) {
354 /* Unbounded for video streams */
355 stream->cur_pes_payload_size = 0;
356 tsmux_stream_find_pts_dts_within (stream,
357 tsmux_stream_bytes_in_buffer (stream), &stream->pts, &stream->dts);
359 /* Output a PES packet of all currently available bytes otherwise */
360 stream->cur_pes_payload_size = tsmux_stream_bytes_in_buffer (stream);
361 tsmux_stream_find_pts_dts_within (stream, stream->cur_pes_payload_size,
362 &stream->pts, &stream->dts);
365 stream->pi.flags &= ~(TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS |
366 TSMUX_PACKET_FLAG_PES_WRITE_PTS);
368 if (stream->pts != -1 && stream->dts != -1)
369 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS;
371 if (stream->pts != -1)
372 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS;
379 * tsmux_stream_get_data:
380 * @stream: a #TsMuxStream
381 * @buf: a buffer to hold the result
382 * @len: the length of @buf
384 * Copy up to @len available data in @stream into the buffer @buf.
386 * Returns: TRUE if @len bytes could be retrieved.
389 tsmux_stream_get_data (TsMuxStream * stream, guint8 * buf, guint len)
391 g_return_val_if_fail (stream != NULL, FALSE);
392 g_return_val_if_fail (buf != NULL, FALSE);
394 if (stream->state == TSMUX_STREAM_STATE_HEADER) {
395 guint8 pes_hdr_length;
397 pes_hdr_length = tsmux_stream_pes_header_length (stream);
399 /* Submitted buffer must be at least as large as the PES header */
400 if (len < pes_hdr_length)
403 TS_DEBUG ("Writing PES header of length %u and payload %d",
404 pes_hdr_length, stream->cur_pes_payload_size);
405 tsmux_stream_write_pes_header (stream, buf);
407 len -= pes_hdr_length;
408 buf += pes_hdr_length;
410 stream->state = TSMUX_STREAM_STATE_PACKET;
413 if (len > (guint) tsmux_stream_bytes_avail (stream))
416 stream->pes_bytes_written += len;
418 if (stream->cur_pes_payload_size != 0 &&
419 stream->pes_bytes_written == stream->cur_pes_payload_size) {
420 TS_DEBUG ("Finished PES packet");
421 stream->state = TSMUX_STREAM_STATE_HEADER;
422 stream->pes_bytes_written = 0;
429 if (stream->cur_buffer == NULL) {
430 /* Start next packet */
431 if (stream->buffers == NULL)
433 stream->cur_buffer = (TsMuxStreamBuffer *) (stream->buffers->data);
434 stream->cur_buffer_consumed = 0;
437 /* Take as much as we can from the current buffer */
438 avail = stream->cur_buffer->size - stream->cur_buffer_consumed;
439 cur = stream->cur_buffer->data + stream->cur_buffer_consumed;
441 memcpy (buf, cur, avail);
442 tsmux_stream_consume (stream, avail);
447 memcpy (buf, cur, len);
448 tsmux_stream_consume (stream, len);
458 tsmux_stream_pes_header_length (TsMuxStream * stream)
462 /* Calculate the length of the header for this stream */
464 /* start_code prefix + stream_id + pes_packet_length = 6 bytes */
467 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_FULL_HEADER) {
468 /* For a PES 'full header' we have at least 3 more bytes,
469 * and then more based on flags */
471 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS) {
473 } else if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS) {
476 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_EXT_STREAMID) {
477 /* Need basic extension flags (1 byte), plus 2 more bytes for the
478 * length + extended stream id */
486 /* Find a PTS/DTS to write into the pes header within the next bound bytes
489 tsmux_stream_find_pts_dts_within (TsMuxStream * stream, guint bound,
490 gint64 * pts, gint64 * dts)
497 for (cur = g_list_first (stream->buffers); cur != NULL;
498 cur = g_list_next (cur)) {
499 TsMuxStreamBuffer *curbuf = cur->data;
501 /* FIXME: This isn't quite correct - if the 'bound' is within this
502 * buffer, we don't know if the timestamp is before or after the split
503 * so we shouldn't return it */
504 if (bound <= curbuf->size) {
510 /* Have we found a buffer with pts/dts set? */
511 if (curbuf->pts != -1 || curbuf->dts != -1) {
517 bound -= curbuf->size;
522 tsmux_stream_write_pes_header (TsMuxStream * stream, guint8 * data)
524 guint16 length_to_write;
525 guint8 hdr_len = tsmux_stream_pes_header_length (stream);
527 /* start_code prefix + stream_id + pes_packet_length = 6 bytes */
531 data[3] = stream->id;
534 /* Write 2 byte PES packet length here. 0 (unbounded) is only
535 * valid for video packets */
536 if (stream->cur_pes_payload_size != 0) {
537 length_to_write = hdr_len + stream->cur_pes_payload_size - 6;
542 tsmux_put16 (&data, length_to_write);
544 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_FULL_HEADER) {
547 /* Not scrambled, original, not-copyrighted, data_alignment not specified */
551 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS)
553 else if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS)
555 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_EXT_STREAMID)
556 flags |= 0x01; /* Enable PES_extension_flag */
559 /* Header length is the total pes length,
560 * minus the 9 bytes of start codes, flags + hdr_len */
561 g_return_if_fail (hdr_len >= 9);
562 *data++ = (hdr_len - 9);
564 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS) {
565 tsmux_put_ts (&data, 0x3, stream->pts);
566 tsmux_put_ts (&data, 0x1, stream->dts);
567 } else if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS) {
568 tsmux_put_ts (&data, 0x2, stream->pts);
570 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_EXT_STREAMID) {
573 flags = 0x0f; /* (reserved bits) | PES_extension_flag_2 */
576 ext_len = 1; /* Only writing 1 byte into the extended fields */
577 *data++ = 0x80 | ext_len;
578 /* Write the extended streamID */
579 *data++ = stream->id_extended;
585 * tsmux_stream_add_data:
586 * @stream: a #TsMuxStream
588 * @len: length of @data
589 * @user_data: user data to pass to release func
590 * @pts: PTS of access unit in @data
591 * @dts: DTS of access unit in @data
593 * Submit @len bytes of @data into @stream. @pts and @dts can be set to the
594 * timestamp (against a 90Hz clock) of the first access unit in @data. A
595 * timestamp of -1 for @pts or @dts means unknown.
597 * @user_data will be passed to the release function as set with
598 * tsmux_stream_set_buffer_release_func() when @data can be freed.
601 tsmux_stream_add_data (TsMuxStream * stream, guint8 * data, guint len,
602 void *user_data, gint64 pts, gint64 dts)
604 TsMuxStreamBuffer *packet;
606 g_return_if_fail (stream != NULL);
608 packet = g_slice_new (TsMuxStreamBuffer);
611 packet->user_data = user_data;
616 if (stream->bytes_avail == 0)
617 stream->last_pts = pts;
619 stream->bytes_avail += len;
620 stream->buffers = g_list_append (stream->buffers, packet);
624 * tsmux_stream_get_es_descrs:
625 * @stream: a #TsMuxStream
626 * @buf: a buffer to hold the ES descriptor
627 * @len: the length used in @buf
629 * Write an Elementary Stream Descriptor for @stream into @buf. the number of
630 * bytes consumed in @buf will be updated in @len.
632 * @buf and @len must be at least #TSMUX_MIN_ES_DESC_LEN.
635 tsmux_stream_get_es_descrs (TsMuxStream * stream, guint8 * buf, guint16 * len)
639 g_return_if_fail (stream != NULL);
647 /* Based on the stream type, write out any descriptors to go in the
648 * PMT ES_info field */
651 /* tag (registration_descriptor), length, format_identifier */
652 switch (stream->stream_type) {
653 case TSMUX_ST_AUDIO_AAC:
656 case TSMUX_ST_VIDEO_MPEG4:
659 case TSMUX_ST_VIDEO_H264:
662 *pos++ = 0x48; /* 'H' */
663 *pos++ = 0x44; /* 'D' */
664 *pos++ = 0x4D; /* 'M' */
665 *pos++ = 0x56; /* 'V' */
666 /* FIXME : Not sure about this additional_identification_info */
672 case TSMUX_ST_VIDEO_DIRAC:
675 *pos++ = 0x64; /* 'd' */
676 *pos++ = 0x72; /* 'r' */
677 *pos++ = 0x61; /* 'a' */
678 *pos++ = 0x63; /* 'c' */
680 case TSMUX_ST_PS_AUDIO_AC3:
684 *pos++ = 0x41; /* 'A' */
685 *pos++ = 0x43; /* 'C' */
686 *pos++ = 0x2D; /* '-' */
687 *pos++ = 0x33; /* '3' */
689 /* audio_stream_descriptor () | ATSC A/52-2001 Annex A
691 * descriptor_tag 8 uimsbf
692 * descriptor_length 8 uimsbf
693 * sample_rate_code 3 bslbf
695 * bit_rate_code 6 bslbf
696 * surround_mode 2 bslbf
698 * num_channels 4 bslbf
706 /* 3 bits sample_rate_code, 5 bits hardcoded bsid (default ver 8) */
707 switch (stream->audio_sampling) {
719 break; /* 48, 44.1 or 32 Khz */
722 /* 1 bit bit_rate_limit, 5 bits bit_rate_code, 2 bits suround_mode */
723 switch (stream->audio_bitrate) {
783 break; /* 640 Kb/s upper limit */
786 /* 3 bits bsmod, 4 bits num_channels, 1 bit full_svc */
787 switch (stream->audio_channels) {
813 case TSMUX_ST_PS_AUDIO_DTS:
816 case TSMUX_ST_PS_AUDIO_LPCM:
828 * tsmux_stream_pcr_ref:
829 * @stream: a #TsMuxStream
831 * Mark the stream as being used as the PCR for some program.
834 tsmux_stream_pcr_ref (TsMuxStream * stream)
836 g_return_if_fail (stream != NULL);
842 * tsmux_stream_pcr_unref:
843 * @stream: a #TsMuxStream
845 * Mark the stream as no longer being used as the PCR for some program.
848 tsmux_stream_pcr_unref (TsMuxStream * stream)
850 g_return_if_fail (stream != NULL);
856 * tsmux_stream_is_pcr:
857 * @stream: a #TsMuxStream
859 * Check if @stream is used as the PCR for some program.
861 * Returns: TRUE if the stream is in use as the PCR for some program.
864 tsmux_stream_is_pcr (TsMuxStream * stream)
866 return stream->pcr_ref != 0;
870 * tsmux_stream_get_pts:
871 * @stream: a #TsMuxStream
873 * Return the PTS of the last buffer that has had bytes written and
874 * which _had_ a PTS in @stream.
876 * Returns: the PTS of the last buffer in @stream.
879 tsmux_stream_get_pts (TsMuxStream * stream)
881 g_return_val_if_fail (stream != NULL, -1);
883 return stream->last_pts;