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 #ifndef __TSMUX_COMMON_H__
65 #define __TSMUX_COMMON_H__
66
67 #include <glib.h>
68 #include <gst/gst.h>
69
70 G_BEGIN_DECLS
71
72 #define TSMUX_SYNC_BYTE 0x47
73 #define TSMUX_PACKET_LENGTH 188
74 #define TSMUX_HEADER_LENGTH 4
75 #define TSMUX_PAYLOAD_LENGTH (TSMUX_PACKET_LENGTH - TSMUX_HEADER_LENGTH)
76
77 #define TSMUX_MIN_ES_DESC_LEN 8
78
79 /* Frequency for PCR representation */
80 #define TSMUX_SYS_CLOCK_FREQ ((gint64) 27000000)
81 /* Frequency for PTS values */
82 #define TSMUX_CLOCK_FREQ (TSMUX_SYS_CLOCK_FREQ / 300)
83
84 #define TSMUX_PACKET_FLAG_NONE (0)
85 #define TSMUX_PACKET_FLAG_ADAPTATION (1 << 0)
86 #define TSMUX_PACKET_FLAG_DISCONT (1 << 1)
87 #define TSMUX_PACKET_FLAG_RANDOM_ACCESS (1 << 2)
88 #define TSMUX_PACKET_FLAG_PRIORITY (1 << 3)
89 #define TSMUX_PACKET_FLAG_WRITE_PCR (1 << 4)
90 #define TSMUX_PACKET_FLAG_WRITE_OPCR (1 << 5)
91 #define TSMUX_PACKET_FLAG_WRITE_SPLICE (1 << 6)
92 #define TSMUX_PACKET_FLAG_WRITE_ADAPT_EXT (1 << 7)
93
94 /* PES stream specific flags */
95 #define TSMUX_PACKET_FLAG_PES_FULL_HEADER (1 << 8)
96 #define TSMUX_PACKET_FLAG_PES_WRITE_PTS (1 << 9)
97 #define TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS (1 << 10)
98 #define TSMUX_PACKET_FLAG_PES_WRITE_ESCR (1 << 11)
99 #define TSMUX_PACKET_FLAG_PES_EXT_STREAMID (1 << 12)
100 #define TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT (1 << 13)
101
102 /* PAT interval (1/10th sec) */
103 #define TSMUX_DEFAULT_PAT_INTERVAL (TSMUX_CLOCK_FREQ / 10)
104 /* PMT interval (1/10th sec) */
105 #define TSMUX_DEFAULT_PMT_INTERVAL (TSMUX_CLOCK_FREQ / 10)
106 /* SI interval (1/10th sec) */
107 #define TSMUX_DEFAULT_SI_INTERVAL (TSMUX_CLOCK_FREQ / 10)
108 /* PCR interval (1/25th sec) */
109 #define TSMUX_DEFAULT_PCR_INTERVAL (TSMUX_CLOCK_FREQ / 25)
110 /* SCTE-35 NULL Interval (5mins) */
111 #define TSMUX_DEFAULT_SCTE_35_NULL_INTERVAL (TSMUX_CLOCK_FREQ * 300)
112 /* Bitrate (bits per second) */
113 #define TSMUX_DEFAULT_BITRATE 0
114
115 typedef struct TsMuxPacketInfo TsMuxPacketInfo;
116 typedef struct TsMuxProgram TsMuxProgram;
117 typedef struct TsMuxStream TsMuxStream;
118
119 struct TsMuxPacketInfo {
120 guint16 pid;
121 guint32 flags;
122 guint32 pes_header_length;
123
124 gboolean packet_start_unit_indicator;
125
126 /* payload bytes available
127 * (including PES header if applicable) */
128 guint stream_avail;
129
130 /* optional PCR */
131 guint64 pcr;
132
133 /* following not really actively used */
134
135 guint64 opcr;
136
137 guint8 splice_countdown;
138
139 guint8 private_data_len;
140 guint8 private_data[256];
141 };
142
143 static inline void
tsmux_put16(guint8 ** pos,guint16 val)144 tsmux_put16 (guint8 **pos, guint16 val)
145 {
146 *(*pos)++ = (val >> 8) & 0xff;
147 *(*pos)++ = val & 0xff;
148 }
149
150 static inline void
tsmux_put32(guint8 ** pos,guint32 val)151 tsmux_put32 (guint8 **pos, guint32 val)
152 {
153 *(*pos)++ = (val >> 24) & 0xff;
154 *(*pos)++ = (val >> 16) & 0xff;
155 *(*pos)++ = (val >> 8) & 0xff;
156 *(*pos)++ = val & 0xff;
157 }
158
159 static inline void
tsmux_put_ts(guint8 ** pos,guint8 id,gint64 ts)160 tsmux_put_ts (guint8 **pos, guint8 id, gint64 ts)
161 {
162 /* 1: 4 bit id value | TS [32..30] | marker_bit */
163 *(*pos)++ = ((id << 4) | ((ts >> 29) & 0x0E) | 0x01) & 0xff;
164 /* 2, 3: TS[29..15] | marker_bit */
165 tsmux_put16 (pos, ((ts >> 14) & 0xfffe) | 0x01);
166 /* 4, 5: TS[14..0] | marker_bit */
167 tsmux_put16 (pos, ((ts << 1) & 0xfffe) | 0x01);
168 }
169
170 GST_DEBUG_CATEGORY_EXTERN (gst_base_ts_mux_debug);
171 #define TS_DEBUG GST_DEBUG
172
173 G_END_DECLS
174
175 #endif
176