1 /*
2  * pesparse.h : MPEG PES parsing utility
3  * Copyright (C) 2011 Edward Hervey <bilboed@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef __PES_PARSE_H__
22 #define __PES_PARSE_H__
23 
24 #include <gst/gst.h>
25 #include "gstmpegdefs.h"
26 
27 G_BEGIN_DECLS
28 
29 
30 /*
31  * PES stream_id assignments:
32  *
33  * 1011 1100                program_stream_map
34  * 1011 1101                private_stream_1
35  * 1011 1110                padding_stream
36  * 1011 1111                private_stream_2
37  * 110x xxxx                ISO/IEC 13818-3 or ISO/IEC 11172-3 audio stream number x xxxx
38  * 1110 xxxx                ITU-T Rec. H.262 | ISO/IEC 13818-2 or ISO/IEC 11172-2 video stream number xxxx
39  * 1111 0000                ECM_stream
40  * 1111 0001                EMM_stream
41  * 1111 0010                ITU-T Rec. H.222.0 | ISO/IEC 13818-1 Annex A or ISO/IEC 13818-6_DSMCC_stream
42  * 1111 0011                ISO/IEC_13522_stream
43  * 1111 0100                ITU-T Rec. H.222.1 type A
44  * 1111 0101                ITU-T Rec. H.222.1 type B
45  * 1111 0110                ITU-T Rec. H.222.1 type C
46  * 1111 0111                ITU-T Rec. H.222.1 type D
47  * 1111 1000                ITU-T Rec. H.222.1 type E
48  * 1111 1001                ancillary_stream
49  * 1111 1010                ISO/IEC 14496-1_SL-packetized_stream
50  * 1111 1011                ISO/IEC 14496-1_FlexMux_stream
51  * 1111 1100                metadata stream
52  * 1111 1101                extended_stream_id
53  * 1111 1110                reserved data stream
54  * 1111 1111                program_stream_directory
55  */
56 
57 #define ID_PS_END_CODE                          0xB9
58 #define ID_PS_PACK_START_CODE                   0xBA
59 #define ID_PS_SYSTEM_HEADER_START_CODE          0xBB
60 #define ID_PS_PROGRAM_STREAM_MAP                0xBC
61 #define ID_PRIVATE_STREAM_1                     0xBD
62 #define ID_PADDING_STREAM                       0xBE
63 #define ID_PRIVATE_STREAM_2                     0xBF
64 #define ID_ISO_IEC_MPEG12_AUDIO_STREAM_0        0xC0
65 #define ID_ISO_IEC_MPEG12_AUDIO_STREAM_32       0xDF
66 #define ID_ISO_IEC_MPEG12_VIDEO_STREAM_0        0xE0
67 #define ID_ISO_IEC_MPEG12_VIDEO_STREAM_16       0xEF
68 #define ID_ECM_STREAM                           0xF0
69 #define ID_EMM_STREAM                           0xF1
70 #define ID_DSMCC_STREAM                         0xF2
71 #define ID_ISO_IEC_13522_STREAM                 0xF3
72 #define ID_ITU_TREC_H222_TYPE_A_STREAM          0xF4
73 #define ID_ITU_TREC_H222_TYPE_B_STREAM          0xF5
74 #define ID_ITU_TREC_H222_TYPE_C_STREAM          0xF6
75 #define ID_ITU_TREC_H222_TYPE_D_STREAM          0xF7
76 #define ID_ITU_TREC_H222_TYPE_E_STREAM          0xF8
77 #define ID_ANCILLARY_STREAM                     0xF9
78 #define ID_14496_1_SL_PACKETIZED_STREAM         0xFA
79 #define ID_14496_1_SL_FLEXMUX_STREAM            0xFB
80 #define ID_METADATA_STREAM                      0xFC
81 #define ID_EXTENDED_STREAM_ID                   0xFD
82 #define ID_RESERVED_STREAM_3                    0xFE
83 #define ID_PROGRAM_STREAM_DIRECTORY             0xFF
84 
85 /*
86  * PES stream_id_extension assignments (if stream_id == ID_EXTENDED_STREAM_ID)
87  *
88  *  000 0000             IPMP Control Information stream
89  *  000 0001             IPMP Stream
90  *  000 0010 - 001 0001  ISO/IEC 14496-17 text Streams
91  *  001 0010 - 010 0001  ISO/IEC 23002-3 auxiliary video data Streams
92  *  ... .... - 011 1111  Reserved
93  *
94  *  PRIVATE STREAM RANGES (But known as used)
95  *  101 0101 - 101 1111  VC-1
96  *  110 0000 - 110 1111  Dirac (VC-1)
97  *
98  *  111 0001             AC3 or independent sub-stream 0 of EAC3/DD+
99  *                       DTS or core sub-stream
100  *  111 0010             dependent sub-stream of EAC3/DD+
101  *                       DTS extension sub-stream
102  *                       Secondary EAC3/DD+
103  *                       Secondary DTS-HD LBR
104  *  111 0110             AC3 in MLP/TrueHD
105  *  1xx xxxx    private_stream
106  */
107 #define EXT_ID_IPMP_CONTORL_INFORMATION_STREAM  0x00
108 #define EXT_ID_IPMP_STREAM			0x01
109 
110 /* VC-1 */
111 #define EXT_ID_VC1_FIRST			0x55
112 #define EXT_ID_VC1_LAST 			0x5F
113 
114 typedef enum {
115   PES_FLAG_PRIORITY		= 1 << 3,	/* PES_priority (present: high-priority) */
116   PES_FLAG_DATA_ALIGNMENT	= 1 << 2,	/* data_alignment_indicator */
117   PES_FLAG_COPYRIGHT		= 1 << 1,	/* copyright */
118   PES_FLAG_ORIGINAL_OR_COPY	= 1 << 0	/* original_or_copy */
119 } PESHeaderFlags;
120 
121 typedef enum {
122   PES_TRICK_MODE_FAST_FORWARD	= 0x000,
123   PES_TRICK_MODE_SLOW_MOTION    = 0x001,
124   PES_TRICK_MODE_FREEZE_FRAME	= 0x010,
125   PES_TRICK_MODE_FAST_REVERSE	= 0x011,
126   PES_TRICK_MODE_SLOW_REVERSE	= 0x100,
127   /* ... */
128   PES_TRICK_MODE_INVALID	= 0xfff	/* Not present or invalid */
129 } PESTrickModeControl;
130 
131 typedef enum {
132   PES_FIELD_ID_TOP_ONLY		= 0x00, /* Display from top field only */
133   PES_FIELD_ID_BOTTOM_ONLY	= 0x01, /* Display from bottom field only */
134   PES_FIELD_ID_COMPLETE_FRAME	= 0x10, /* Display complete frame */
135   PES_FIELD_ID_INVALID		= 0x11	/* Reserved/Invalid */
136 } PESFieldID;
137 
138 typedef enum {
139   PES_PARSING_OK	= 0,	/* Header fully parsed and valid */
140   PES_PARSING_BAD	= 1,	/* Header invalid (CRC error for ex) */
141   PES_PARSING_NEED_MORE = 2	/* Not enough data to parse header */
142 } PESParsingResult;
143 
144 typedef struct {
145   guint8	stream_id;	/* See ID_* above */
146   guint32	packet_length;	/* The size of the PES header and PES data
147 				 * (if 0 => unbounded packet) */
148   guint16	header_size;	/* The complete size of the PES header */
149 
150   /* All remaining entries in this structure are optional */
151   guint8	scrambling_control; /* 0x00  : Not scrambled/unspecified,
152 				     * The following are according to ETSI TS 101 154
153 				     * 0x01  : reserved for future DVB use
154 				     * 0x10  : PES packet scrambled with Even key
155 				     * 0x11  : PES packet scrambled with Odd key
156 				     */
157   PESHeaderFlags flags;
158 
159   guint64	PTS;		/* PTS (-1 if not present or invalid) */
160   guint64	DTS;		/* DTS (-1 if not present or invalid) */
161   guint64	ESCR;		/* ESCR (-1 if not present or invalid) */
162 
163   guint32	ES_rate;	/* in bytes/seconds (0 if not present or invalid) */
164   PESTrickModeControl	trick_mode;
165 
166   /* Only valid for _FAST_FORWARD, _FAST_REVERSE and _FREEZE_FRAME */
167   PESFieldID	field_id;
168   /* Only valid for _FAST_FORWARD and _FAST_REVERSE */
169   gboolean	intra_slice_refresh;
170   guint8	frequency_truncation;
171   /* Only valid for _SLOW_FORWARD and _SLOW_REVERSE */
172   guint8	rep_cntrl;
173 
174   guint8	additional_copy_info; /* Private data */
175   guint16	previous_PES_packet_CRC;
176 
177   /* Extension fields */
178   const guint8*	private_data;			/* PES_private_data, 16 bytes long */
179   guint8	pack_header_size;		/* Size of pack_header in bytes */
180   const guint8*	pack_header;
181   gint8		program_packet_sequence_counter; /* -1 if not present or invalid */
182   gboolean	MPEG1_MPEG2_identifier;
183   guint8	original_stuff_length;
184 
185   guint32	P_STD_buffer_size; /* P-STD buffer size in bytes (0 if invalid
186 				    * or not present */
187 
188   guint8	stream_id_extension; /* Public range (0x00 - 0x3f) only valid if stream_id == ID_EXTENDED_STREAM_ID
189 				      * Private range (0x40 - 0xff) can be present in any stream type */
190 
191   gsize		extension_field_length;   /* Length of remaining extension field data */
192   const guint8*	stream_id_extension_data; /* Valid if extension_field_length != 0 */
193 } PESHeader;
194 
195 G_GNUC_INTERNAL PESParsingResult mpegts_parse_pes_header (const guint8* data,
196 							  gsize size,
197 							  PESHeader *res);
198 G_GNUC_INTERNAL void init_pes_parser (void);
199 
200 G_END_DECLS
201 #endif /* __PES_PARSE_H__ */
202