• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 BBC and Fluendo S.A.
3  *
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
7  * license.
8  *
9  * MPL:
10  *
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/.
15  *
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
19  * under the License.
20  *
21  * LGPL:
22  *
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.
27  *
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.
32  *
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., 51 Franklin St, Fifth Floor,
36  * Boston, MA 02110-1301, USA.
37  *
38  * GPL:
39  *
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.
44  *
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.
49  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
53  *
54  * MIT:
55  *
56  * Unless otherwise indicated, Source Code is licensed under MIT license.
57  * See further explanation attached in License Statement (distributed in the file
58  * LICENSE).
59  *
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:
66  *
67  * The above copyright notice and this permission notice shall be included in all
68  * copies or substantial portions of the Software.
69  *
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
76  * SOFTWARE.
77  *
78  */
79 
80 #ifndef __TSMUX_COMMON_H__
81 #define __TSMUX_COMMON_H__
82 
83 #include <glib.h>
84 #include <gst/gst.h>
85 
86 G_BEGIN_DECLS
87 
88 #define TSMUX_SYNC_BYTE 0x47
89 #define TSMUX_PACKET_LENGTH 188
90 #define TSMUX_HEADER_LENGTH 4
91 #define TSMUX_PAYLOAD_LENGTH (TSMUX_PACKET_LENGTH - TSMUX_HEADER_LENGTH)
92 
93 #define TSMUX_MIN_ES_DESC_LEN 8
94 
95 /* Frequency for PCR representation */
96 #define TSMUX_SYS_CLOCK_FREQ ((gint64) 27000000)
97 /* Frequency for PTS values */
98 #define TSMUX_CLOCK_FREQ (TSMUX_SYS_CLOCK_FREQ / 300)
99 
100 #define TSMUX_PACKET_FLAG_NONE            (0)
101 #define TSMUX_PACKET_FLAG_ADAPTATION      (1 << 0)
102 #define TSMUX_PACKET_FLAG_DISCONT         (1 << 1)
103 #define TSMUX_PACKET_FLAG_RANDOM_ACCESS   (1 << 2)
104 #define TSMUX_PACKET_FLAG_PRIORITY        (1 << 3)
105 #define TSMUX_PACKET_FLAG_WRITE_PCR       (1 << 4)
106 #define TSMUX_PACKET_FLAG_WRITE_OPCR      (1 << 5)
107 #define TSMUX_PACKET_FLAG_WRITE_SPLICE    (1 << 6)
108 #define TSMUX_PACKET_FLAG_WRITE_ADAPT_EXT (1 << 7)
109 
110 /* PES stream specific flags */
111 #define TSMUX_PACKET_FLAG_PES_FULL_HEADER   (1 << 8)
112 #define TSMUX_PACKET_FLAG_PES_WRITE_PTS     (1 << 9)
113 #define TSMUX_PACKET_FLAG_PES_WRITE_PTS_DTS (1 << 10)
114 #define TSMUX_PACKET_FLAG_PES_WRITE_ESCR    (1 << 11)
115 #define TSMUX_PACKET_FLAG_PES_EXT_STREAMID  (1 << 12)
116 #define TSMUX_PACKET_FLAG_PES_DATA_ALIGNMENT (1 << 13)
117 
118 /* PAT interval (1/10th sec) */
119 #define TSMUX_DEFAULT_PAT_INTERVAL (TSMUX_CLOCK_FREQ / 10)
120 /* PMT interval (1/10th sec) */
121 #define TSMUX_DEFAULT_PMT_INTERVAL (TSMUX_CLOCK_FREQ / 10)
122 /* SI  interval (1/10th sec) */
123 #define TSMUX_DEFAULT_SI_INTERVAL  (TSMUX_CLOCK_FREQ / 10)
124 
125 typedef struct TsMuxPacketInfo TsMuxPacketInfo;
126 typedef struct TsMuxProgram TsMuxProgram;
127 typedef struct TsMuxStream TsMuxStream;
128 
129 struct TsMuxPacketInfo {
130   guint16 pid;
131   guint32 flags;
132   guint32 pes_header_length;
133 
134   gboolean packet_start_unit_indicator;
135 
136   /* continuity counter */
137   guint8 packet_count;
138 
139   /* payload bytes available
140    * (including PES header if applicable) */
141   guint stream_avail;
142 
143   /* optional PCR */
144   guint64 pcr;
145 
146   /* following not really actively used */
147 
148   guint64 opcr;
149 
150   guint8 splice_countdown;
151 
152   guint8 private_data_len;
153   guint8 private_data[256];
154 };
155 
156 static inline void
tsmux_put16(guint8 ** pos,guint16 val)157 tsmux_put16 (guint8 **pos, guint16 val)
158 {
159   *(*pos)++ = (val >> 8) & 0xff;
160   *(*pos)++ = val & 0xff;
161 }
162 
163 static inline void
tsmux_put32(guint8 ** pos,guint32 val)164 tsmux_put32 (guint8 **pos, guint32 val)
165 {
166   *(*pos)++ = (val >> 24) & 0xff;
167   *(*pos)++ = (val >> 16) & 0xff;
168   *(*pos)++ = (val >> 8) & 0xff;
169   *(*pos)++ = val & 0xff;
170 }
171 
172 static inline void
tsmux_put_ts(guint8 ** pos,guint8 id,gint64 ts)173 tsmux_put_ts (guint8 **pos, guint8 id, gint64 ts)
174 {
175   /* 1: 4 bit id value | TS [32..30] | marker_bit */
176   *(*pos)++ = ((id << 4) | ((ts >> 29) & 0x0E) | 0x01) & 0xff;
177   /* 2, 3: TS[29..15] | marker_bit */
178   tsmux_put16 (pos, ((ts >> 14) & 0xfffe) | 0x01);
179   /* 4, 5: TS[14..0] | marker_bit */
180   tsmux_put16 (pos, ((ts << 1) & 0xfffe) | 0x01);
181 }
182 
183 GST_DEBUG_CATEGORY_EXTERN (mpegtsmux_debug);
184 #define TS_DEBUG GST_DEBUG
185 
186 G_END_DECLS
187 
188 #endif
189