Wasabi ExpressPlay SDK for Embedded Systems  1.10
Ts2Packet.h
Go to the documentation of this file.
1 /*****************************************************************
2 |
3 | MPEG2 Transport Stream - Packet
4 |
5 | $Id: Ts2Packet.h 196 2012-01-05 14:18:55Z jebaseelir $
6 | Original author: Julien Boeuf
7 |
8 | This software is provided to you pursuant to your agreement
9 | with Intertrust Technologies Corporation ("Intertrust").
10 | This software may be used only in accordance with the terms
11 | of the agreement.
12 |
13 | Copyright (c) 2009-2012 Intertrust. All rights reserved.
14 |
15 ****************************************************************/
16 
17 #ifndef _TS2_PACKET_H_
18 #define _TS2_PACKET_H_
19 
20 /*----------------------------------------------------------------------
21 | includes
22 +---------------------------------------------------------------------*/
23 #include "Atomix.h"
24 #include "Ts2Config.h"
25 
26 /*----------------------------------------------------------------------
27 | constants
28 +---------------------------------------------------------------------*/
29 /* pids */
30 #define TS2_NULL_PACKET_PID 0x1FFF
31 #define TS2_INVALID_PID 0xFFFF
32 
33 /* frequency/PCR */
34 #define TS2_SYSTEM_CLOCK_FREQUENCY 27000000.0 /* 27 MHz */
35 #define TS2_INVALID_PCR_IN_SECS -1.0
36 #define TS2_MAX_PCR_IN_SECS 95443.717696703708 /* ((2**33-1)*300+(2**9-1)) / 27000000.0 */
37 
38 /* packet size */
39 #define TS2_PACKET_SIZE 188
40 #define TS2_PACKET_HEADER_SIZE 4
41 #define TS2_PACKET_PAYLOAD_SIZE (TS2_PACKET_SIZE - TS2_PACKET_HEADER_SIZE)
42 
43 #define TS2_PACKET_SYNC_BYTE 0x47
44 
45 #define TS2_PACKET_AF_CLOCK_REFERENCE_DATA_SIZE 6
46 #define TS2_PACKET_AF_EXTENSION_SEAMLESS_SIZE 5
47 
48 #define TS2_STUFFING_BYTE 0xFF
49 
50 /* flags */
51 #define TS2_PACKET_TRANSPORT_ERROR_INDICATOR_FLAG_MASK 0x04
52 #define TS2_PACKET_PAYLOAD_UNIT_START_INDICATOR_FLAG_MASK 0x02
53 #define TS2_PACKET_TRANSPORT_PRIORITY_FLAG_MASK 0x01
54 
55 /*----------------------------------------------------------------------
56 | types
57 +---------------------------------------------------------------------*/
58 typedef struct {
59  ATX_UInt8 length;
60  ATX_UInt8 flags;
61  ATX_UInt16 ltw;
62  ATX_UInt32 piecewise;
63  ATX_UInt8 seamless_splice[TS2_PACKET_AF_EXTENSION_SEAMLESS_SIZE];
65 
66 typedef struct {
67  ATX_UInt8 length;
68  ATX_UInt8 flags;
71  ATX_UInt8 splice_countdown;
76 
77 typedef enum {
83 
84 typedef enum {
90 
91 typedef struct {
92  ATX_UInt8 flags;
93  ATX_UInt16 pid;
96  ATX_UInt8 continuity_counter;
99  ATX_Size payload_size;
100 } TS2_Packet;
101 
103 
104 typedef struct {
105  void* instance;
106  ATX_Result (*HandlePacket)(void* handler_instance,
107  TS2_PacketDispatcher* dispatcher,
108  TS2_Packet* packet);
110 
112 
113 #ifdef __cplusplus
114 extern "C" {
115 #endif /* __cplusplus */
116 
117 /*----------------------------------------------------------------------
118 | TS2_AdaptationField functions and macros
119 +---------------------------------------------------------------------*/
120 ATX_Float
122 
123 #define TS2_AF_Flags_DiscontinuityIndicator(flags) ((flags) & 0x80)
124 #define TS2_AF_Flags_RandomAccessIndicator(flags) ((flags) & 0x40)
125 #define TS2_AF_Flags_EsPriorityIndicator(flags) ((flags) & 0x20)
126 #define TS2_AF_Flags_PcrFlag(flags) ((flags) & 0x10)
127 #define TS2_AF_Flags_OpcrFlag(flags) ((flags) & 0x08)
128 #define TS2_AF_Flags_SplicingPointFlag(flags) ((flags) & 0x04)
129 #define TS2_AF_Flags_TransportPrivateDataFlag(flags) ((flags) & 0x02)
130 #define TS2_AF_Flags_ExtensionFlag(flags) ((flags) & 0x01)
131 
132 #define TS2_AF_Extension_Flags_LtwFlag(flags) ((flags) & 0x80)
133 #define TS2_AF_Extension_Flags_PiecewiseRateFlag(flags) ((flags) & 0x40)
134 #define TS2_AF_Extension_Flags_SeamlessSpliceFlag(flags) ((flags) & 0x20)
135 
136 /*----------------------------------------------------------------------
137 | TS2_Packet functions and macros
138 +---------------------------------------------------------------------*/
139 ATX_Result
140 TS2_Packet_InitFromInputStream(ATX_InputStream* stream,
141  TS2_Packet* packet);
142 
143 ATX_Result
144 TS2_Packet_InitFromBuffer(const ATX_Byte* buffer,
145  TS2_Packet* packet);
146 
148 ATX_Result
150  ATX_Byte* buffer);
151 
152 void
154 
155 void
156 TS2_FormatPacketHeader(ATX_Byte* buffer, /* TS2_PACKET_HEADER_SIZE */
157  ATX_UInt32 transport_error_indicator,
158  ATX_UInt32 payload_unit_start_indicator,
159  ATX_UInt32 transport_priority,
160  ATX_UInt16 pid,
161  TS2_ScramblingControl scrambling_control,
162  TS2_AfControl af_control,
163  ATX_UInt32 continuity_counter);
164 
165 
166 #define TS2_Packet_HasAdaptationField(packet) ((packet)->af_control & 0x02)
167 #define TS2_Packet_HasPayload(packet) ((packet)->af_control & 0x01)
168 #define TS2_Packet_HasPcr(packet) ( \
169  TS2_Packet_HasAdaptationField(packet) && \
170  packet->adaptation_field != NULL && \
171  TS2_AF_Flags_PcrFlag(packet->adaptation_field->flags) \
172 )
173 
174 #define TS2_Packet_Flags_TransportError(pflags) (((pflags) & 0x04) >> 2)
175 #define TS2_Packet_Flags_PayloadUnitStart(pflags) (((pflags) & 0x02) >> 1)
176 #define TS2_Packet_Flags_TransportPriority(pflags) (((pflags) & 0x01) )
177 
178 /*----------------------------------------------------------------------
179 | TS2_PacketDispatcher functions
180 +---------------------------------------------------------------------*/
181 ATX_Result
183 
184 TS2_EXPORT ATX_Result
186  ATX_UInt16 pid,
187  TS2_PacketHandler handler);
188 
189 TS2_EXPORT ATX_Result
191  ATX_UInt16 pid);
192 
193 ATX_Result
195  TS2_PacketHandler handler);
196 
197 ATX_Result
199 
200 ATX_Result
202  TS2_Packet* packet);
203 
204 ATX_UInt16
206 
207 ATX_Boolean
209 
210 void
212 
213 /*----------------------------------------------------------------------
214 | TS2_Packet functions
215 +---------------------------------------------------------------------*/
216 TS2_EXPORT ATX_Result
217 TS2_PacketPump_Create(ATX_InputStream* stream,
218  TS2_PacketDispatcher* dispatcher, /* takes ownership. If NULL, will create one */
219  TS2_PacketPump** pump);
220 
223 
224 TS2_EXPORT ATX_Result
226 
227 TS2_EXPORT ATX_Result
229  ATX_InputStream* stream);
230 
231 TS2_EXPORT void
233 
234 #ifdef __cplusplus
235 }
236 #endif /* __cplusplus */
237 
238 #endif /* _TS2_PACKET_H_ */