Debian Codegraph @

gst/mpegtsmux/tsmux/tsmuxstream.c

raw symbols SCIP
1 /*
2 * Copyright 2006 BBC and Fluendo S.A.
3 *
4 * This library is licensed under 3 different licenses and you
5 * can choose to use it under the terms of any one of them. The
6 * three licenses are the MPL 1.1, the LGPL, and the MIT license.
7 *
8 * MPL:
9 *
10 * The contents of this file are subject to the Mozilla Public License
11 * Version 1.1 (the "License"); you may not use this file except in
12 * compliance with the License. You may obtain a copy of the License at
13 * http://www.mozilla.org/MPL/.
14 *
15 * Software distributed under the License is distributed on an "AS IS"
16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * LGPL:
21 *
22 * This library is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU Library General Public
24 * License as published by the Free Software Foundation; either
25 * version 2 of the License, or (at your option) any later version.
26 *
27 * This library is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 * Library General Public License for more details.
31 *
32 * You should have received a copy of the GNU Library General Public
33 * License along with this library; if not, write to the
34 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
35 * Boston, MA 02110-1301, USA.
36 *
37 * MIT:
38 *
39 * Unless otherwise indicated, Source Code is licensed under MIT license.
40 * See further explanation attached in License Statement (distributed in the file
41 * LICENSE).
42 *
43 * Permission is hereby granted, free of charge, to any person obtaining a copy of
44 * this software and associated documentation files (the "Software"), to deal in
45 * the Software without restriction, including without limitation the rights to
46 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
47 * of the Software, and to permit persons to whom the Software is furnished to do
48 * so, subject to the following conditions:
49 *
50 * The above copyright notice and this permission notice shall be included in all
51 * copies or substantial portions of the Software.
52 *
53 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
54 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
55 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
56 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
57 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
58 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
59 * SOFTWARE.
60 *
61 * SPDX-License-Identifier: MPL-1.1 OR MIT OR LGPL-2.0-or-later
62 */
63
64 #ifdef HAVE_CONFIG_H
65 #include "config.h"
66 #endif
67
68 #include <string.h>
69
70 #include <gst/mpegts/mpegts.h>
71 #include <gst/base/gstbytewriter.h>
72 #include <gst/mpegtsdemux/gstmpegdesc.h>
73
74 #include "tsmuxcommon.h"
75 #include "tsmuxstream.h"
76
77 #define GST_CAT_DEFAULT gst_base_ts_mux_debug
78
79 /* start_code prefix + stream_id + pes_packet_length = 6 bytes */
80 #define PES_PACKET_PREFIX_LEN 6
81
82 static guint8 tsmux_stream_pes_header_length (TsMuxStream * stream);
83 static void tsmux_stream_write_pes_header (TsMuxStream * stream, guint8 * data);
84 static void tsmux_stream_find_pts_dts_within (TsMuxStream * stream, guint bound,
85 gint64 * pts, gint64 * dts);
86
87 struct TsMuxStreamBuffer
88 {
89 guint8 *data;
90 guint32 size;
91
92 /* PTS & DTS associated with the contents of this buffer */
93 gint64 pts;
94 gint64 dts;
95
96 gboolean pts_dts_consumed;
97
98 /* data represents random access point */
99 gboolean random_access;
100
101 /* user_data for release function */
102 void *user_data;
103 };
104
105 static void
106 tsmux_stream_get_es_descrs_default (TsMuxStream * stream,
107 GstMpegtsPMTStream * pmt_stream, gpointer user_data)
108 {
109 tsmux_stream_default_get_es_descrs (stream, pmt_stream);
110 }
111
112 /**
113 * tsmux_stream_new:
114 * @pid: a PID
115 * @stream_type: the stream type
116 * @stream_number: stream number
117 *
118 * Create a new stream with PID of @pid and @stream_type,
119 * with stream number @stream_number.
120 *
121 * Returns: a new #TsMuxStream.
122 */
123 TsMuxStream *
124 tsmux_stream_new (guint16 pid, guint stream_type, guint stream_number)
125 {
126 TsMuxStream *stream = g_new0 (TsMuxStream, 1);
127 gboolean supports_user_specified_stream_number = FALSE;
128
129 stream->state = TSMUX_STREAM_STATE_HEADER;
130 stream->pi.pid = pid;
131 stream->stream_type = stream_type;
132 stream->internal_stream_type = stream_type;
133
134 stream->pes_payload_size = 0;
135 stream->cur_pes_payload_size = 0;
136 stream->pes_bytes_written = 0;
137 stream->pmt_index = -1;
138
139 switch (stream_type) {
140 case TSMUX_ST_VIDEO_MPEG1:
141 case TSMUX_ST_VIDEO_MPEG2:
142 case TSMUX_ST_VIDEO_MPEG4:
143 case TSMUX_ST_VIDEO_H264:
144 case TSMUX_ST_VIDEO_HEVC:
145 case TSMUX_ST_VIDEO_VVC:
146 if (stream_number > 0xF) {
147 GST_WARNING
148 ("video stream number %d is greater than 0xF. Setting to 0.",
149 stream_number);
150 stream_number = 0;
151 }
152 stream->id = 0xE0 | stream_number;
153 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
154 stream->gst_stream_type = GST_STREAM_TYPE_VIDEO;
155 supports_user_specified_stream_number = TRUE;
156 break;
157 case TSMUX_ST_VIDEO_JP2K:
158 stream->id = 0xBD;
159 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
160 stream->gst_stream_type = GST_STREAM_TYPE_VIDEO;
161 break;
162 case TSMUX_ST_VIDEO_JPEG_XS:
163 stream->id = 0xBD; /* Private Stream 1 */
164 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
165 stream->gst_stream_type = GST_STREAM_TYPE_VIDEO;
166 break;
167 case TSMUX_ST_PS_VP9:
168 stream->id = 0xBD; /* Private Stream 1 */
169 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
170 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
171 stream->gst_stream_type = GST_STREAM_TYPE_VIDEO;
172 break;
173 case TSMUX_ST_AUDIO_AAC:
174 case TSMUX_ST_AUDIO_MPEG1:
175 case TSMUX_ST_AUDIO_MPEG2:
176 if (stream_number > 0x1F) {
177 GST_WARNING
178 ("audio stream number %d is greater than 0x1F. Setting to 0.",
179 stream_number);
180 stream_number = 0;
181 }
182 stream->gst_stream_type = GST_STREAM_TYPE_AUDIO;
183 stream->id = 0xC0 | stream_number;
184 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
185 supports_user_specified_stream_number = TRUE;
186 break;
187 case TSMUX_ST_VIDEO_DIRAC:
188 case TSMUX_ST_PS_AUDIO_LPCM:
189 case TSMUX_ST_PS_AUDIO_AC3:
190 case TSMUX_ST_PS_AUDIO_DTS:
191 stream->id = 0xFD;
192 /* FIXME: assign sequential extended IDs? */
193 switch (stream_type) {
194 case TSMUX_ST_VIDEO_DIRAC:
195 stream->id_extended = 0x60;
196 stream->gst_stream_type = GST_STREAM_TYPE_VIDEO;
197 break;
198 case TSMUX_ST_PS_AUDIO_LPCM:
199 stream->gst_stream_type = GST_STREAM_TYPE_AUDIO;
200 stream->id_extended = 0x80;
201 break;
202 case TSMUX_ST_PS_AUDIO_AC3:
203 stream->gst_stream_type = GST_STREAM_TYPE_AUDIO;
204 stream->id_extended = 0x71;
205 break;
206 case TSMUX_ST_PS_AUDIO_DTS:
207 stream->gst_stream_type = GST_STREAM_TYPE_AUDIO;
208 stream->id_extended = 0x82;
209 break;
210 default:
211 break;
212 }
213 stream->pi.flags |=
214 TSMUX_PACKET_FLAG_PES_FULL_HEADER |
215 TSMUX_PACKET_FLAG_PES_EXT_STREAMID;
216 break;
217 case TSMUX_ST_PS_TELETEXT:
218 /* needs fixed PES header length */
219 stream->pi.pes_header_length = 36;
220 stream->id = 0xBD;
221 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
222 stream->pi.flags |=
223 TSMUX_PACKET_FLAG_PES_FULL_HEADER |
224 TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT;
225 break;
226 case TSMUX_ST_PS_DVB_SUBPICTURE:
227 /* private stream 1 */
228 stream->id = 0xBD;
229 stream->is_dvb_sub = TRUE;
230 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
231 stream->pi.flags |=
232 TSMUX_PACKET_FLAG_PES_FULL_HEADER |
233 TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT;
234
235 break;
236 case TSMUX_ST_PS_KLV:
237 /* FIXME: assign sequential extended IDs? */
238 stream->id = 0xBD;
239 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
240 stream->pi.flags |=
241 TSMUX_PACKET_FLAG_PES_FULL_HEADER |
242 TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT;
243 break;
244 case TSMUX_ST_PS_ST_2038:
245 /* FIXME: assign sequential extended IDs? */
246 stream->id = 0xBD;
247 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
248 stream->pi.flags |=
249 TSMUX_PACKET_FLAG_PES_FULL_HEADER |
250 TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT;
251 break;
252 case TSMUX_ST_PS_ID3:
253 stream->id = 0xBD; // private stream
254 stream->stream_type = TSMUX_ST_PES_METADATA;
255 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER |
256 TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT;
257 break;
258 case TSMUX_ST_PS_OPUS:
259 /* FIXME: assign sequential extended IDs? */
260 stream->id = 0xBD;
261 stream->gst_stream_type = GST_STREAM_TYPE_AUDIO;
262 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
263 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
264 break;
265 case TSMUX_ST_PS_S302M:
266 /* FIXME: assign sequential extended IDs? */
267 stream->id = 0xBD;
268 stream->gst_stream_type = GST_STREAM_TYPE_AUDIO;
269 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
270 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
271 break;
272 case TSMUX_ST_PS_VIDEO_AV1:
273 /* FIXME: assign sequential extended IDs? */
274 stream->id = 0xBD;
275 stream->gst_stream_type = GST_STREAM_TYPE_VIDEO;
276 stream->stream_type = TSMUX_ST_PRIVATE_DATA;
277 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_FULL_HEADER;
278 break;
279 default:
280 /* Might be a custom stream type implemented by a subclass */
281 break;
282 }
283
284 if (!supports_user_specified_stream_number && stream_number != 0) {
285 GST_WARNING
286 ("Attempt to set stream number %d for unsupported stream type %d",
287 stream_number, stream_type);
288 }
289
290 stream->first_ts = GST_CLOCK_STIME_NONE;
291
292 stream->last_pts = GST_CLOCK_STIME_NONE;
293 stream->last_dts = GST_CLOCK_STIME_NONE;
294
295 stream->pcr_ref = 0;
296 stream->next_pcr = -1;
297
298 stream->get_es_descrs = tsmux_stream_get_es_descrs_default;
299 stream->get_es_descrs_data = NULL;
300
301 return stream;
302 }
303
304 /**
305 * tsmux_stream_get_pid:
306 * @stream: a #TsMuxStream
307 *
308 * Get the PID of @stream.
309 *
310 * Returns: The PID of @stream. 0xffff on error.
311 */
312 guint16
313 tsmux_stream_get_pid (TsMuxStream * stream)
314 {
315 g_return_val_if_fail (stream != NULL, G_MAXUINT16);
316
317 return stream->pi.pid;
318 }
319
320 /**
321 * tsmux_stream_free:
322 * @stream: a #TsMuxStream
323 *
324 * Free the resources of @stream.
325 */
326 void
327 tsmux_stream_free (TsMuxStream * stream)
328 {
329 GList *cur;
330
331 g_return_if_fail (stream != NULL);
332
333 /* free buffers */
334 for (cur = stream->buffers; cur; cur = cur->next) {
335 TsMuxStreamBuffer *tmbuf = (TsMuxStreamBuffer *) cur->data;
336
337 if (stream->buffer_release)
338 stream->buffer_release (tmbuf->data, tmbuf->user_data);
339 g_free (tmbuf);
340 }
341 g_list_free (stream->buffers);
342
343 if (stream->pmt_descriptor)
344 gst_mpegts_descriptor_free (stream->pmt_descriptor);
345 g_free (stream);
346 }
347
348 /**
349 * tsmux_stream_set_buffer_release_func:
350 * @stream: a #TsMuxStream
351 * @func: the new #TsMuxStreamBufferReleaseFunc
352 *
353 * Set the function that will be called when a a piece of data fed to @stream
354 * with tsmux_stream_add_data() can be freed. @func will be called with user
355 * data as provided with the call to tsmux_stream_add_data().
356 */
357 void
358 tsmux_stream_set_buffer_release_func (TsMuxStream * stream,
359 TsMuxStreamBufferReleaseFunc func)
360 {
361 g_return_if_fail (stream != NULL);
362
363 stream->buffer_release = func;
364 }
365
366 /**
367 * tsmux_stream_set_get_es_descriptors_func:
368 * @stream: a #TsMuxStream
369 * @func: a user callback function
370 * @user_data: user data passed to @func
371 *
372 * Set the callback function and user data to be called when @stream has
373 * to create Elementary Stream Descriptors.
374 */
375 void
376 tsmux_stream_set_get_es_descriptors_func (TsMuxStream * stream,
377 TsMuxStreamGetESDescriptorsFunc func, void *user_data)
378 {
379 g_return_if_fail (stream != NULL);
380
381 stream->get_es_descrs = func;
382 stream->get_es_descrs_data = user_data;
383 }
384
385 /* Advance the current packet stream position by len bytes.
386 * Mustn't consume more than available in the current packet */
387 static void
388 tsmux_stream_consume (TsMuxStream * stream, guint len)
389 {
390 g_assert (stream->cur_buffer != NULL);
391 g_assert (len <= stream->cur_buffer->size - stream->cur_buffer_consumed);
392
393 stream->cur_buffer_consumed += len;
394 stream->bytes_avail -= len;
395
396 if (stream->cur_buffer_consumed == 0 && stream->cur_buffer->size != 0)
397 return;
398
399 if (GST_CLOCK_STIME_IS_VALID (stream->cur_buffer->pts))
400 stream->last_pts = stream->cur_buffer->pts;
401 if (GST_CLOCK_STIME_IS_VALID (stream->cur_buffer->dts))
402 stream->last_dts = stream->cur_buffer->dts;
403
404 if (stream->cur_buffer_consumed == stream->cur_buffer->size) {
405 /* Current packet is completed, move along */
406 stream->buffers = g_list_delete_link (stream->buffers, stream->buffers);
407
408 if (stream->buffer_release) {
409 stream->buffer_release (stream->cur_buffer->data,
410 stream->cur_buffer->user_data);
411 }
412
413 g_free (stream->cur_buffer);
414 stream->cur_buffer = NULL;
415 /* FIXME: As a hack, for unbounded streams, start a new PES packet for each
416 * incoming packet we receive. This assumes that incoming data is
417 * packetised sensibly - ie, every video frame */
418 if (stream->cur_pes_payload_size == 0) {
419 stream->state = TSMUX_STREAM_STATE_HEADER;
420 stream->pes_bytes_written = 0;
421 }
422 }
423 }
424
425 /**
426 * tsmux_stream_at_pes_start:
427 * @stream: a #TsMuxStream
428 *
429 * Check if @stream is at the start of a PES packet.
430 *
431 * Returns: TRUE if @stream is at a PES header packet.
432 */
433 gboolean
434 tsmux_stream_at_pes_start (TsMuxStream * stream)
435 {
436 g_return_val_if_fail (stream != NULL, FALSE);
437
438 return stream->state == TSMUX_STREAM_STATE_HEADER;
439 }
440
441 /**
442 * tsmux_stream_bytes_avail:
443 * @stream: a #TsMuxStream
444 *
445 * Calculate how many bytes are currently available for
446 * writing from this stream, including a PES header
447 * if one needs writing.
448 *
449 * Returns: The number of bytes available.
450 */
451 static inline gint
452 _tsmux_stream_bytes_avail (TsMuxStream * stream)
453 {
454 gint bytes_avail;
455
456 g_return_val_if_fail (stream != NULL, 0);
457
458 if (stream->cur_pes_payload_size != 0)
459 bytes_avail = stream->cur_pes_payload_size - stream->pes_bytes_written;
460 else
461 bytes_avail = stream->bytes_avail;
462
463 bytes_avail = MIN (bytes_avail, stream->bytes_avail);
464
465 /* Calculate the number of bytes available in the current PES */
466 if (stream->state == TSMUX_STREAM_STATE_HEADER)
467 bytes_avail += tsmux_stream_pes_header_length (stream);
468
469 return bytes_avail;
470 }
471
472 gint
473 tsmux_stream_bytes_avail (TsMuxStream * stream)
474 {
475 g_return_val_if_fail (stream != NULL, 0);
476
477 return _tsmux_stream_bytes_avail (stream);
478 }
479
480 /**
481 * tsmux_stream_bytes_in_buffer:
482 * @stream: a #TsMuxStream
483 *
484 * Calculate how much bytes are in the buffer.
485 *
486 * Returns: The number of bytes in the buffer.
487 */
488 gint
489 tsmux_stream_bytes_in_buffer (TsMuxStream * stream)
490 {
491 g_return_val_if_fail (stream != NULL, 0);
492
493 return stream->bytes_avail;
494 }
495
496 /**
497 * tsmux_stream_initialize_pes_packet:
498 * @stream: a #TsMuxStream
499 *
500 * Initializes the PES packet.
501 *
502 * Returns: TRUE if the packet was initialized.
503 */
504 gboolean
505 tsmux_stream_initialize_pes_packet (TsMuxStream * stream)
506 {
507 if (stream->state != TSMUX_STREAM_STATE_HEADER)
508 return TRUE;
509
510 if (stream->pes_payload_size != 0) {
511 /* Use prescribed fixed PES payload size */
512 stream->cur_pes_payload_size = stream->pes_payload_size;
513 tsmux_stream_find_pts_dts_within (stream, stream->cur_pes_payload_size,
514 &stream->pts, &stream->dts);
515 } else {
516 /* Output a PES packet of all currently available bytes otherwise */
517 stream->cur_pes_payload_size = stream->bytes_avail;
518 tsmux_stream_find_pts_dts_within (stream, stream->cur_pes_payload_size,
519 &stream->pts, &stream->dts);
520 }
521
522 stream->pi.flags &= ~(TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS |
523 TSMUX_PACKET_FLAG_PES_WRITE_PTS);
524
525 if (GST_CLOCK_STIME_IS_VALID (stream->pts)
526 && GST_CLOCK_STIME_IS_VALID (stream->dts)
527 && stream->pts != stream->dts)
528 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS;
529 else {
530 if (GST_CLOCK_STIME_IS_VALID (stream->pts))
531 stream->pi.flags |= TSMUX_PACKET_FLAG_PES_WRITE_PTS;
532 }
533
534 if (stream->buffers) {
535 TsMuxStreamBuffer *buf = (TsMuxStreamBuffer *) (stream->buffers->data);
536 if (buf->random_access) {
537 stream->pi.flags |= TSMUX_PACKET_FLAG_RANDOM_ACCESS;
538 stream->pi.flags |= TSMUX_PACKET_FLAG_ADAPTATION;
539 }
540 }
541
542 guint8 hdr_len = tsmux_stream_pes_header_length (stream);
543 guint max_payload_len = G_MAXUINT16 - (hdr_len - PES_PACKET_PREFIX_LEN);
544
545 if (stream->cur_pes_payload_size > max_payload_len) {
546 if (stream->gst_stream_type == GST_STREAM_TYPE_VIDEO) {
547 /* Unbounded for video streams if pes packet length is over 16 bit */
548 stream->cur_pes_payload_size = 0;
549 } else {
550 /* Else, clamp this PES packet to maximum size and put the rest
551 * in another PES */
552 TS_DEBUG
553 ("Splitting input packet to multiple PES. Writing %u bytes of incoming payload",
554 max_payload_len);
555 stream->cur_pes_payload_size = max_payload_len;
556 }
557 }
558
559 return TRUE;
560 }
561
562 /**
563 * tsmux_stream_get_data:
564 * @stream: a #TsMuxStream
565 * @buf: a buffer to hold the result
566 * @len: the length of @buf
567 *
568 * Copy up to @len available data in @stream into the buffer @buf.
569 *
570 * Returns: TRUE if @len bytes could be retrieved.
571 */
572 gboolean
573 tsmux_stream_get_data (TsMuxStream * stream, guint8 * buf, guint len)
574 {
575 g_return_val_if_fail (stream != NULL, FALSE);
576 g_return_val_if_fail (buf != NULL, FALSE);
577
578 if (stream->state == TSMUX_STREAM_STATE_HEADER) {
579 guint8 pes_hdr_length;
580
581 pes_hdr_length = tsmux_stream_pes_header_length (stream);
582
583 /* Submitted buffer must be at least as large as the PES header */
584 if (len < pes_hdr_length)
585 return FALSE;
586
587 TS_DEBUG ("Writing PES header of length %u and payload %d",
588 pes_hdr_length, stream->cur_pes_payload_size);
589 tsmux_stream_write_pes_header (stream, buf);
590
591 len -= pes_hdr_length;
592 buf += pes_hdr_length;
593
594 stream->state = TSMUX_STREAM_STATE_PACKET;
595 }
596
597 if (len > (guint) _tsmux_stream_bytes_avail (stream))
598 return FALSE;
599
600 stream->pes_bytes_written += len;
601
602 if (stream->cur_pes_payload_size != 0 &&
603 stream->pes_bytes_written == stream->cur_pes_payload_size) {
604 TS_DEBUG ("Finished PES packet");
605 stream->state = TSMUX_STREAM_STATE_HEADER;
606 stream->pes_bytes_written = 0;
607 }
608
609 while (len > 0) {
610 guint32 avail;
611 guint8 *cur;
612
613 if (stream->cur_buffer == NULL) {
614 /* Start next packet */
615 if (stream->buffers == NULL)
616 return FALSE;
617 stream->cur_buffer = (TsMuxStreamBuffer *) (stream->buffers->data);
618 stream->cur_buffer_consumed = 0;
619 }
620
621 /* Take as much as we can from the current buffer */
622 avail = stream->cur_buffer->size - stream->cur_buffer_consumed;
623 cur = stream->cur_buffer->data + stream->cur_buffer_consumed;
624 if (avail < len) {
625 memcpy (buf, cur, avail);
626 tsmux_stream_consume (stream, avail);
627
628 buf += avail;
629 len -= avail;
630 } else {
631 memcpy (buf, cur, len);
632 tsmux_stream_consume (stream, len);
633
634 len = 0;
635 }
636 }
637
638 return TRUE;
639 }
640
641 static guint8
642 tsmux_stream_pes_header_length (TsMuxStream * stream)
643 {
644 guint8 packet_len;
645
646 /* Calculate the length of the header for this stream */
647
648 /* start_code prefix + stream_id + pes_packet_length = 6 bytes */
649 packet_len = PES_PACKET_PREFIX_LEN;
650
651 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_FULL_HEADER) {
652 /* For a PES 'full header' we have at least 3 more bytes,
653 * and then more based on flags */
654 packet_len += 3;
655 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS) {
656 packet_len += 10;
657 } else if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS) {
658 packet_len += 5;
659 }
660 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_EXT_STREAMID) {
661 /* Need basic extension flags (1 byte), plus 2 more bytes for the
662 * length + extended stream id */
663 packet_len += 3;
664 }
665 if (stream->pi.pes_header_length) {
666 /* check for consistency, then we can add stuffing */
667 g_assert (packet_len <=
668 stream->pi.pes_header_length + PES_PACKET_PREFIX_LEN + 3);
669 packet_len = stream->pi.pes_header_length + PES_PACKET_PREFIX_LEN + 3;
670 }
671 }
672
673 return packet_len;
674 }
675
676 /* Find a PTS/DTS to write into the pes header within the next bound bytes
677 * of the data */
678 static void
679 tsmux_stream_find_pts_dts_within (TsMuxStream * stream, guint bound,
680 gint64 * pts, gint64 * dts)
681 {
682 GList *cur;
683
684 *pts = GST_CLOCK_STIME_NONE;
685 *dts = GST_CLOCK_STIME_NONE;
686
687 for (cur = stream->buffers; cur; cur = cur->next) {
688 TsMuxStreamBuffer *curbuf = cur->data;
689
690 if (curbuf->pts_dts_consumed) {
691 /* Don't repeat PTS/DTS when partially consuming buffers */
692 continue;
693 }
694
695 /* FIXME: This isn't quite correct - if the 'bound' is within this
696 * buffer, we don't know if the timestamp is before or after the split
697 * so we shouldn't return it */
698 if (bound <= curbuf->size) {
699 *pts = curbuf->pts;
700 *dts = curbuf->dts;
701 curbuf->pts_dts_consumed = TRUE;
702 return;
703 }
704
705 /* Have we found a buffer with pts/dts set? */
706 if (GST_CLOCK_STIME_IS_VALID (curbuf->pts)
707 || GST_CLOCK_STIME_IS_VALID (curbuf->dts)) {
708 *pts = curbuf->pts;
709 *dts = curbuf->dts;
710 curbuf->pts_dts_consumed = TRUE;
711 return;
712 }
713
714 bound -= curbuf->size;
715 }
716 }
717
718 static void
719 tsmux_stream_write_pes_header (TsMuxStream * stream, guint8 * data)
720 {
721 guint16 length_to_write;
722 guint8 hdr_len = tsmux_stream_pes_header_length (stream);
723 guint8 *orig_data = data;
724
725 /* start_code prefix + stream_id + pes_packet_length = 6 bytes */
726 data[0] = 0x00;
727 data[1] = 0x00;
728 data[2] = 0x01;
729 data[3] = stream->id;
730 data += 4;
731
732 /* Write 2 byte PES packet length here. 0 (unbounded) is only
733 * valid for video packets */
734 if (stream->cur_pes_payload_size != 0) {
735 length_to_write =
736 hdr_len + stream->cur_pes_payload_size - PES_PACKET_PREFIX_LEN;
737 } else {
738 length_to_write = 0;
739 }
740
741 tsmux_put16 (&data, length_to_write);
742
743 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_FULL_HEADER) {
744 guint8 flags = 0;
745
746 /* Not scrambled, original, not-copyrighted, data_alignment not specified */
747 flags = 0x81;
748
749 /* If this stream has PES data alignment *and* this PES is the start of
750 * an input buffer, set the data aligment mark */
751 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT &&
752 stream->cur_buffer_consumed == 0) {
753 TS_DEBUG ("Marking data_alignment flag for this PES");
754 flags |= 0x4;
755 }
756 *data++ = flags;
757 flags = 0;
758
759 /* Flags */
760 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS)
761 flags |= 0xC0;
762 else if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS)
763 flags |= 0x80;
764 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_EXT_STREAMID)
765 flags |= 0x01; /* Enable PES_extension_flag */
766 *data++ = flags;
767
768 /* Header length is the total pes length,
769 * minus the 9 bytes of start codes, flags + hdr_len */
770 g_return_if_fail (hdr_len >= 9);
771 *data++ = (hdr_len - 9);
772
773 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS) {
774 tsmux_put_ts (&data, 0x3, stream->pts);
775 tsmux_put_ts (&data, 0x1, stream->dts);
776 } else if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_WRITE_PTS) {
777 tsmux_put_ts (&data, 0x2, stream->pts);
778 }
779 if (stream->pi.flags & TSMUX_PACKET_FLAG_PES_EXT_STREAMID) {
780 guint8 ext_len;
781
782 flags = 0x0f; /* (reserved bits) | PES_extension_flag_2 */
783 *data++ = flags;
784
785 ext_len = 1; /* Only writing 1 byte into the extended fields */
786 *data++ = 0x80 | ext_len;
787 /* Write the extended streamID */
788 *data++ = stream->id_extended;
789 }
790 /* write stuffing bytes if fixed PES header length requested */
791 if (stream->pi.pes_header_length)
792 while (data < orig_data + stream->pi.pes_header_length + 9)
793 *data++ = 0xff;
794 }
795 }
796
797 static GstMpegtsDescriptor *
798 j2k_descriptor (TsMuxStream * stream)
799 {
800 GstMpegtsDescriptor *descriptor;
801 /* J2K video descriptor
802 * descriptor_tag 8 uimsbf
803 * descriptor_length 8 uimsbf
804 * profile_and_level 16 uimsbf
805 * horizontal_size 32 uimsbf
806 * vertical_size 32 uimsbf
807 * max_bit_rate 32 uimsbf
808 * max_buffer_size 32 uimsbf
809 * DEN_frame_rate 16 uimsbf
810 * NUM_frame_rate 16 uimsbf
811 * color_specification 8 bslbf
812 * still_mode 1 bslbf
813 * interlace_video 1 bslbf
814 * reserved 6 bslbf
815 * private_data_byte 8 bslbf
816 */
817 gint8 still_interlace_reserved = 0x00;
818 int wr_size = 0;
819 guint8 *add_info = NULL;
820 guint8 level = stream->profile_and_level & 0xF;
821 guint32 max_buffer_size = 0;
822 GstByteWriter writer;
823 gst_byte_writer_init_with_size (&writer, 32, FALSE);
824
825 switch (level) {
826 case 1:
827 case 2:
828 case 3:
829 max_buffer_size = 1250000;
830 break;
831 case 4:
832 max_buffer_size = 2500000;
833 break;
834 case 5:
835 max_buffer_size = 5000000;
836 break;
837 case 6:
838 max_buffer_size = 10000000;
839 break;
840 default:
841 break;
842 }
843
844 gst_byte_writer_put_uint16_be (&writer, stream->profile_and_level);
845 gst_byte_writer_put_uint32_be (&writer, stream->horizontal_size);
846 gst_byte_writer_put_uint32_be (&writer, stream->vertical_size);
847 gst_byte_writer_put_uint32_be (&writer, max_buffer_size);
848 gst_byte_writer_put_uint32_be (&writer, stream->max_bitrate);
849 gst_byte_writer_put_uint16_be (&writer, stream->den);
850 gst_byte_writer_put_uint16_be (&writer, stream->num);
851 gst_byte_writer_put_uint8 (&writer, stream->color_spec);
852
853 if (stream->interlace_mode)
854 still_interlace_reserved |= 0x40;
855
856 gst_byte_writer_put_uint8 (&writer, still_interlace_reserved);
857 gst_byte_writer_put_uint8 (&writer, 0x00); /* private data byte */
858
859 wr_size = gst_byte_writer_get_size (&writer);
860 add_info = gst_byte_writer_reset_and_get_data (&writer);
861
862 descriptor =
863 gst_mpegts_descriptor_from_custom (GST_MTS_DESC_J2K_VIDEO, add_info,
864 wr_size);
865 g_free (add_info);
866
867 return descriptor;
868 }
869
870 /**
871 * tsmux_stream_add_data:
872 * @stream: a #TsMuxStream
873 * @data: data to add
874 * @len: length of @data
875 * @user_data: user data to pass to release func
876 * @pts: PTS of access unit in @data
877 * @dts: DTS of access unit in @data
878 * @random_access: TRUE if random access point (keyframe)
879 *
880 * Submit @len bytes of @data into @stream. @pts and @dts can be set to the
881 * timestamp (against a 90Hz clock) of the first access unit in @data. A
882 * timestamp of GST_CLOCK_STIME_NONE for @pts or @dts means unknown.
883 *
884 * @user_data will be passed to the release function as set with
885 * tsmux_stream_set_buffer_release_func() when @data can be freed.
886 */
887 void
888 tsmux_stream_add_data (TsMuxStream * stream, guint8 * data, guint len,
889 void *user_data, gint64 pts, gint64 dts, gboolean random_access)
890 {
891 TsMuxStreamBuffer *packet;
892
893 g_return_if_fail (stream != NULL);
894
895 packet = g_new (TsMuxStreamBuffer, 1);
896 packet->data = data;
897 packet->size = len;
898 packet->user_data = user_data;
899 packet->random_access = random_access;
900
901 packet->pts = pts;
902 packet->dts = dts;
903 packet->pts_dts_consumed = FALSE;
904
905 if (stream->bytes_avail == 0) {
906 stream->last_pts = pts;
907 stream->last_dts = dts;
908 }
909
910 stream->bytes_avail += len;
911 stream->buffers = g_list_append (stream->buffers, packet);
912 }
913
914 /**
915 * tsmux_stream_default_get_es_descrs:
916 * @stream: a #TsMuxStream
917 * @buf: a buffer to hold the ES descriptor
918 * @len: the length used in @buf
919 *
920 * Write an Elementary Stream Descriptor for @stream into @buf. the number of
921 * bytes consumed in @buf will be updated in @len.
922 *
923 * @buf and @len must be at least #TSMUX_MIN_ES_DESC_LEN.
924 */
925 void
926 tsmux_stream_default_get_es_descrs (TsMuxStream * stream,
927 GstMpegtsPMTStream * pmt_stream)
928 {
929 GstMpegtsDescriptor *descriptor;
930
931 g_return_if_fail (stream != NULL);
932 g_return_if_fail (pmt_stream != NULL);
933
934 if (stream->gst_stream_type == GST_STREAM_TYPE_AUDIO
935 && stream->language[0] != '\0') {
936 descriptor = gst_mpegts_descriptor_from_iso_639_language (stream->language);
937 g_ptr_array_add (pmt_stream->descriptors, descriptor);
938 descriptor = NULL;
939 }
940
941 /* Based on the stream type, write out any descriptors to go in the
942 * PMT ES_info field */
943 /* tag (registration_descriptor), length, format_identifier */
944 switch (stream->stream_type) {
945 case TSMUX_ST_AUDIO_AAC:
946 /* FIXME */
947 break;
948 case TSMUX_ST_VIDEO_MPEG4:
949 /* FIXME */
950 break;
951 case TSMUX_ST_VIDEO_H264:
952 {
953 /* FIXME : Not sure about this additional_identification_info */
954 guint8 add_info[] = { 0xFF, 0x1B, 0x44, 0x3F };
955
956 descriptor = gst_mpegts_descriptor_from_registration ("HDMV",
957 add_info, 4);
958
959 g_ptr_array_add (pmt_stream->descriptors, descriptor);
960 break;
961 }
962 case TSMUX_ST_VIDEO_DIRAC:
963 descriptor = gst_mpegts_descriptor_from_registration ("drac", NULL, 0);
964 g_ptr_array_add (pmt_stream->descriptors, descriptor);
965 break;
966 case TSMUX_ST_VIDEO_JP2K:
967 {
968 descriptor = j2k_descriptor (stream);
969 g_ptr_array_add (pmt_stream->descriptors, descriptor);
970 }
971 break;
972 case TSMUX_ST_VIDEO_JPEG_XS:
973 {
974 g_ptr_array_add (pmt_stream->descriptors,
975 gst_mpegts_descriptor_copy (stream->pmt_descriptor));
976 }
977 break;
978 case TSMUX_ST_PS_AUDIO_AC3:
979 {
980 /* This is only called for DVB, ATSC ignores this case in favour of its
981 * special handling for the 0x81 descriptor */
982 /* We choose to provide no optional information */
983 guint8 add_info[1] = { 0x00 };
984
985 descriptor = gst_mpegts_descriptor_from_registration ("AC-3", NULL, 0);
986 g_ptr_array_add (pmt_stream->descriptors, descriptor);
987
988 descriptor =
989 gst_mpegts_descriptor_from_custom (GST_MTS_DESC_DVB_AC3, add_info, 1);
990 g_ptr_array_add (pmt_stream->descriptors, descriptor);
991 break;
992 }
993 case TSMUX_ST_PS_AUDIO_DTS:
994 /* FIXME */
995 break;
996 case TSMUX_ST_PS_AUDIO_LPCM:
997 /* FIXME */
998 break;
999 case TSMUX_ST_PES_METADATA:
1000 if (stream->internal_stream_type == TSMUX_ST_PS_ID3) {
1001 // metadata_descriptor
1002 GstMpegtsMetadataDescriptor metadata_descriptor;
1003
1004 metadata_descriptor.metadata_application_format =
1005 GST_MPEGTS_METADATA_APPLICATION_FORMAT_IDENTIFIER_FIELD;
1006 metadata_descriptor.metadata_format =
1007 GST_MPEGTS_METADATA_FORMAT_IDENTIFIER_FIELD;
1008 metadata_descriptor.metadata_format_identifier = DRF_ID_ID3;
1009 metadata_descriptor.metadata_service_id = 0;
1010 metadata_descriptor.decoder_config_flags = 0x00;
1011 metadata_descriptor.dsm_cc_flag = FALSE;
1012
1013 descriptor = gst_mpegts_descriptor_from_metadata (&metadata_descriptor);
1014 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1015
1016 // registration_descriptor
1017 guint32 format_identifier = 0;
1018 GST_WRITE_UINT32_BE (&format_identifier,
1019 metadata_descriptor.metadata_format_identifier);
1020
1021 descriptor = gst_mpegts_descriptor_from_registration ((const char *)
1022 &format_identifier, NULL, 0);
1023 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1024 }
1025 break;
1026 case TSMUX_ST_PS_DVB_SUBPICTURE:
1027 /* fallthrough ...
1028 * that should never happen anyway as
1029 * dvb subtitles are private data */
1030 case TSMUX_ST_PRIVATE_DATA:
1031 if (stream->is_dvb_sub) {
1032 GST_DEBUG ("Stream language %s", stream->language);
1033 /* Simple DVB subtitles with no monitor aspect ratio critical
1034 FIXME, how do we make it settable? */
1035 /* Default composition page ID */
1036 /* Default ancillary_page_id */
1037 descriptor =
1038 gst_mpegts_descriptor_from_dvb_subtitling (stream->language, 0x10,
1039 0x0001, 0x0152);
1040
1041 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1042 break;
1043 }
1044 if (stream->internal_stream_type == TSMUX_ST_PS_OPUS) {
1045 descriptor = gst_mpegts_descriptor_from_registration ("Opus", NULL, 0);
1046 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1047
1048 descriptor =
1049 gst_mpegts_descriptor_from_custom_with_extension
1050 (GST_MTS_DESC_DVB_EXTENSION, 0x80,
1051 stream->opus_channel_config, stream->opus_channel_config_len);
1052
1053 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1054 }
1055 if (stream->internal_stream_type == TSMUX_ST_PS_KLV) {
1056 descriptor = gst_mpegts_descriptor_from_registration ("KLVA", NULL, 0);
1057 GST_DEBUG ("adding KLVA registration descriptor");
1058 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1059 }
1060 if (stream->internal_stream_type == TSMUX_ST_PS_ST_2038) {
1061 descriptor = gst_mpegts_descriptor_from_registration ("VANC", NULL, 0);
1062 GST_DEBUG ("adding VANC registration descriptor");
1063 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1064 descriptor = gst_mpegts_descriptor_from_custom (0xc4, NULL, 0);
1065 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1066 }
1067 if (stream->internal_stream_type == TSMUX_ST_PS_S302M) {
1068 descriptor = gst_mpegts_descriptor_from_registration ("BSSD", NULL, 0);
1069 GST_DEBUG ("adding SMPTE 302M registration descriptor");
1070 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1071 }
1072 if (stream->internal_stream_type == TSMUX_ST_PS_VP9) {
1073 descriptor = gst_mpegts_descriptor_from_registration ("VP09", NULL, 0);
1074 GST_DEBUG ("adding VP09 registration descriptor");
1075 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1076 }
1077 if (stream->internal_stream_type == TSMUX_ST_PS_VIDEO_AV1) {
1078 /* AV1G is our custom non-bytestream format ! */
1079 descriptor = gst_mpegts_descriptor_from_registration ("AV1G", NULL, 0);
1080 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1081 if (stream->pmt_descriptor)
1082 g_ptr_array_add (pmt_stream->descriptors,
1083 gst_mpegts_descriptor_copy (stream->pmt_descriptor));
1084 }
1085 if (stream->internal_stream_type == TSMUX_ST_PS_TELETEXT) {
1086 // FIXME empty descriptor for now;
1087 // How should this be signalled from upstream?
1088 // Definitions in EN300468 - 6.2.43 Teletext descriptor
1089 descriptor =
1090 gst_mpegts_descriptor_from_custom (GST_MTS_DESC_DVB_TELETEXT, 0, 1);
1091 g_ptr_array_add (pmt_stream->descriptors, descriptor);
1092 }
1093 default:
1094 break;
1095 }
1096 }
1097
1098 /**
1099 * tsmux_stream_get_es_descrs:
1100 * @stream: a #TsMuxStream
1101 * @buf: a buffer to hold the ES descriptor
1102 * @len: the length used in @buf
1103 *
1104 * Write an Elementary Stream Descriptor for @stream into @buf. the number of
1105 * bytes consumed in @buf will be updated in @len.
1106 *
1107 * @buf and @len must be at least #TSMUX_MIN_ES_DESC_LEN.
1108 */
1109 void
1110 tsmux_stream_get_es_descrs (TsMuxStream * stream,
1111 GstMpegtsPMTStream * pmt_stream)
1112 {
1113 g_return_if_fail (stream->get_es_descrs != NULL);
1114
1115 stream->get_es_descrs (stream, pmt_stream, stream->get_es_descrs_data);
1116 }
1117
1118 /**
1119 * tsmux_stream_pcr_ref:
1120 * @stream: a #TsMuxStream
1121 *
1122 * Mark the stream as being used as the PCR for some program.
1123 */
1124 void
1125 tsmux_stream_pcr_ref (TsMuxStream * stream)
1126 {
1127 g_return_if_fail (stream != NULL);
1128
1129 stream->pcr_ref++;
1130 }
1131
1132 /**
1133 * tsmux_stream_pcr_unref:
1134 * @stream: a #TsMuxStream
1135 *
1136 * Mark the stream as no longer being used as the PCR for some program.
1137 */
1138 void
1139 tsmux_stream_pcr_unref (TsMuxStream * stream)
1140 {
1141 g_return_if_fail (stream != NULL);
1142
1143 stream->pcr_ref--;
1144 }
1145
1146 /**
1147 * tsmux_stream_is_pcr:
1148 * @stream: a #TsMuxStream
1149 *
1150 * Check if @stream is used as the PCR for some program.
1151 *
1152 * Returns: TRUE if the stream is in use as the PCR for some program.
1153 */
1154 gboolean
1155 tsmux_stream_is_pcr (TsMuxStream * stream)
1156 {
1157 return stream->pcr_ref != 0;
1158 }
1159
1160 /**
1161 * tsmux_stream_get_pts:
1162 * @stream: a #TsMuxStream
1163 *
1164 * Return the PTS of the last buffer that has had bytes written and
1165 * which _had_ a PTS in @stream.
1166 *
1167 * Returns: the PTS of the last buffer in @stream.
1168 */
1169 gint64
1170 tsmux_stream_get_pts (TsMuxStream * stream)
1171 {
1172 g_return_val_if_fail (stream != NULL, GST_CLOCK_STIME_NONE);
1173
1174 return stream->last_pts;
1175 }
1176
1177 /**
1178 * tsmux_stream_get_dts:
1179 * @stream: a #TsMuxStream
1180 *
1181 * Return the DTS of the last buffer that has had bytes written and
1182 * which _had_ a DTS in @stream.
1183 *
1184 * Returns: the DTS of the last buffer in @stream.
1185 */
1186 gint64
1187 tsmux_stream_get_dts (TsMuxStream * stream)
1188 {
1189 g_return_val_if_fail (stream != NULL, GST_CLOCK_STIME_NONE);
1190
1191 return stream->last_dts;
1192 }