1 /*****************************************************************************
2  * pmt.h: ISO/IEC 13818-1 Program Map Table (PMT)
3  *****************************************************************************
4  * Copyright (C) 2009-2010 VideoLAN
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject
14  * to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *****************************************************************************/
27 
28 /*
29  * Normative references:
30  *  - ISO/IEC 13818-1:2007(E) (MPEG-2 Systems)
31  */
32 
33 #ifndef __BITSTREAM_MPEG_PMT_H__
34 #define __BITSTREAM_MPEG_PMT_H__
35 
36 #include <bitstream/common.h>
37 #include <bitstream/mpeg/psi/psi.h>
38 #include <bitstream/mpeg/psi/descriptors.h>
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 /*****************************************************************************
46  * Program Map Table
47  *****************************************************************************/
48 #define PMT_TABLE_ID            0x2
49 #define PMT_HEADER_SIZE         (PSI_HEADER_SIZE_SYNTAX1 + 4)
50 #define PMT_ES_SIZE             5
51 
52 #define pmt_set_program psi_set_tableidext
53 #define pmt_get_program psi_get_tableidext
54 
55 #define PMT_STREAMTYPE_VIDEO_MPEG1      0x1
56 #define PMT_STREAMTYPE_VIDEO_MPEG2      0x2
57 #define PMT_STREAMTYPE_AUDIO_MPEG1      0x3
58 #define PMT_STREAMTYPE_AUDIO_MPEG2      0x4
59 #define PMT_STREAMTYPE_PRIVATE_PSI      0x5
60 #define PMT_STREAMTYPE_PRIVATE_PES      0x6
61 #define PMT_STREAMTYPE_MHEG             0x7
62 #define PMT_STREAMTYPE_DSM_CC           0x8
63 #define PMT_STREAMTYPE_H222_1           0x9
64 #define PMT_STREAMTYPE_13818_6_A        0xa
65 #define PMT_STREAMTYPE_13818_6_B        0xb
66 #define PMT_STREAMTYPE_13818_6_C        0xc
67 #define PMT_STREAMTYPE_13818_6_D        0xd
68 #define PMT_STREAMTYPE_H222_0_AUX       0xe
69 #define PMT_STREAMTYPE_AUDIO_ADTS       0xf
70 #define PMT_STREAMTYPE_VIDEO_MPEG4      0x10
71 #define PMT_STREAMTYPE_AUDIO_LATM       0x11
72 #define PMT_STREAMTYPE_SL_PES           0x12
73 #define PMT_STREAMTYPE_SL_14496         0x13
74 #define PMT_STREAMTYPE_SDP              0x14
75 #define PMT_STREAMTYPE_META_PES         0x15
76 #define PMT_STREAMTYPE_META_PSI         0x16
77 #define PMT_STREAMTYPE_META_DC          0x17
78 #define PMT_STREAMTYPE_META_OC          0x18
79 #define PMT_STREAMTYPE_META_SDP         0x19
80 #define PMT_STREAMTYPE_IPMP_13818_11    0x1a
81 #define PMT_STREAMTYPE_VIDEO_AVC        0x1b
82 #define PMT_STREAMTYPE_VIDEO_HEVC       0x24
83 #define PMT_STREAMTYPE_VIDEO_AVS        0x42
84 #define PMT_STREAMTYPE_IPMP             0x7f
85 #define PMT_STREAMTYPE_ATSC_A52         0x81
86 #define PMT_STREAMTYPE_SCTE_35          0x86
87 #define PMT_STREAMTYPE_ATSC_A52E        0x87
88 
pmt_init(uint8_t * p_pmt)89 static inline void pmt_init(uint8_t *p_pmt)
90 {
91     psi_init(p_pmt, true);
92     psi_set_tableid(p_pmt, PMT_TABLE_ID);
93     p_pmt[1] &= ~0x40;
94     psi_set_section(p_pmt, 0);
95     psi_set_lastsection(p_pmt, 0);
96     p_pmt[8] = 0xe0;
97     p_pmt[10] = 0xf0;
98 }
99 
pmt_set_length(uint8_t * p_pmt,uint16_t i_pmt_length)100 static inline void pmt_set_length(uint8_t *p_pmt, uint16_t i_pmt_length)
101 {
102     psi_set_length(p_pmt, PMT_HEADER_SIZE + PSI_CRC_SIZE - PSI_HEADER_SIZE
103                     + i_pmt_length);
104 }
105 
pmt_set_pcrpid(uint8_t * p_pmt,uint16_t i_pcr_pid)106 static inline void pmt_set_pcrpid(uint8_t *p_pmt, uint16_t i_pcr_pid)
107 {
108     p_pmt[8] &= ~0x1f;
109     p_pmt[8] |= i_pcr_pid >> 8;
110     p_pmt[9] = i_pcr_pid & 0xff;
111 }
112 
pmt_get_pcrpid(const uint8_t * p_pmt)113 static inline uint16_t pmt_get_pcrpid(const uint8_t *p_pmt)
114 {
115     return ((p_pmt[8] & 0x1f) << 8) | p_pmt[9];
116 }
117 
pmt_set_desclength(uint8_t * p_pmt,uint16_t i_length)118 static inline void pmt_set_desclength(uint8_t *p_pmt, uint16_t i_length)
119 {
120     p_pmt[10] &= ~0xf;
121     p_pmt[10] |= i_length >> 8;
122     p_pmt[11] = i_length & 0xff;
123 }
124 
pmt_get_desclength(const uint8_t * p_pmt)125 static inline uint16_t pmt_get_desclength(const uint8_t *p_pmt)
126 {
127     return ((p_pmt[10] & 0xf) << 8) | p_pmt[11];
128 }
129 
pmt_get_descs(uint8_t * p_pmt)130 static inline uint8_t *pmt_get_descs(uint8_t *p_pmt)
131 {
132     return &p_pmt[10];
133 }
134 
pmtn_init(uint8_t * p_pmt_n)135 static inline void pmtn_init(uint8_t *p_pmt_n)
136 {
137     p_pmt_n[1] = 0xe0;
138     p_pmt_n[3] = 0xf0;
139 }
140 
pmtn_set_streamtype(uint8_t * p_pmt_n,uint8_t i_stream_type)141 static inline void pmtn_set_streamtype(uint8_t *p_pmt_n, uint8_t i_stream_type)
142 {
143     p_pmt_n[0] = i_stream_type;
144 }
145 
pmtn_get_streamtype(const uint8_t * p_pmt_n)146 static inline uint8_t pmtn_get_streamtype(const uint8_t *p_pmt_n)
147 {
148     return p_pmt_n[0];
149 }
150 
pmt_get_streamtype_txt(uint8_t i_stream_type)151 static inline const char *pmt_get_streamtype_txt(uint8_t i_stream_type) {
152     /* ISO/IEC 13818-1 | Table 2-36 - Stream type assignments */
153     if (i_stream_type == 0)
154         return "Reserved";
155     switch (i_stream_type) {
156         case 0x01: return "11172-2 video (MPEG-1)";
157         case 0x02: return "13818-2 video (MPEG-2)";
158         case 0x03: return "11172-3 audio (MPEG-1)";
159         case 0x04: return "13818-3 audio (MPEG-2)";
160         case 0x05: return "13818-1 private sections";
161         case 0x06: return "13818-1 PES private data";
162         case 0x07: return "13522 MHEG";
163         case 0x08: return "H.222.0/13818-1 Annex A - DSM CC";
164         case 0x09: return "H.222.1";
165         case 0x0A: return "13818-6 type A";
166         case 0x0B: return "13818-6 type B";
167         case 0x0C: return "13818-6 type C";
168         case 0x0D: return "13818-6 type D";
169         case 0x0E: return "H.222.0/13818-1 auxiliary";
170         case 0x0F: return "13818-7 Audio with ADTS transport syntax";
171         case 0x10: return "14496-2 Visual (MPEG-4 part 2 video)";
172         case 0x11: return "14496-3 Audio with LATM transport syntax (14496-3/AMD 1)";
173         case 0x12: return "14496-1 SL-packetized or FlexMux stream in PES packets";
174         case 0x13: return "14496-1 SL-packetized or FlexMux stream in 14496 sections";
175         case 0x14: return "ISO/IEC 13818-6 Synchronized Download Protocol";
176         case 0x15: return "Metadata in PES packets";
177         case 0x16: return "Metadata in metadata_sections";
178         case 0x17: return "Metadata in 13818-6 Data Carousel";
179         case 0x18: return "Metadata in 13818-6 Object Carousel";
180         case 0x19: return "Metadata in 13818-6 Synchronized Download Protocol";
181         case 0x1A: return "13818-11 MPEG-2 IPMP stream";
182         case 0x1B: return "H.264/14496-10 video (MPEG-4/AVC)";
183         case 0x24: return "H.265 video (MPEG-H/HEVC)";
184         case 0x42: return "AVS Video";
185         case 0x7F: return "IPMP stream";
186         case 0x81: return "ATSC A/52";
187         case 0x86: return "SCTE 35 Splice Information Table";
188         case 0x87: return "ATSC A/52e";
189         default  : return "Unknown";
190     }
191 }
192 
pmtn_set_pid(uint8_t * p_pmt_n,uint16_t i_pid)193 static inline void pmtn_set_pid(uint8_t *p_pmt_n, uint16_t i_pid)
194 {
195     p_pmt_n[1] &= ~0x1f;
196     p_pmt_n[1] |= i_pid >> 8;
197     p_pmt_n[2] = i_pid & 0xff;
198 }
199 
pmtn_get_pid(const uint8_t * p_pmt_n)200 static inline uint16_t pmtn_get_pid(const uint8_t *p_pmt_n)
201 {
202     return ((p_pmt_n[1] & 0x1f) << 8) | p_pmt_n[2];
203 }
204 
pmtn_set_desclength(uint8_t * p_pmt_n,uint16_t i_length)205 static inline void pmtn_set_desclength(uint8_t *p_pmt_n, uint16_t i_length)
206 {
207     p_pmt_n[3] &= ~0xf;
208     p_pmt_n[3] |= i_length >> 8;
209     p_pmt_n[4] = i_length & 0xff;
210 }
211 
pmtn_get_desclength(const uint8_t * p_pmt_n)212 static inline uint16_t pmtn_get_desclength(const uint8_t *p_pmt_n)
213 {
214     return ((p_pmt_n[3] & 0xf) << 8) | p_pmt_n[4];
215 }
216 
pmtn_get_descs(uint8_t * p_pmt_n)217 static inline uint8_t *pmtn_get_descs(uint8_t *p_pmt_n)
218 {
219     return &p_pmt_n[3];
220 }
221 
pmt_get_es(uint8_t * p_pmt,uint8_t n)222 static inline uint8_t *pmt_get_es(uint8_t *p_pmt, uint8_t n)
223 {
224     uint16_t i_section_size = psi_get_length(p_pmt) + PSI_HEADER_SIZE
225                                - PSI_CRC_SIZE;
226     uint8_t *p_pmt_n = p_pmt + PMT_HEADER_SIZE + pmt_get_desclength(p_pmt);
227     if (p_pmt_n - p_pmt > i_section_size) return NULL;
228 
229     while (n) {
230         if (p_pmt_n + PMT_ES_SIZE - p_pmt > i_section_size) return NULL;
231         p_pmt_n += PMT_ES_SIZE + pmtn_get_desclength(p_pmt_n);
232         n--;
233     }
234     if (p_pmt_n - p_pmt >= i_section_size) return NULL;
235     return p_pmt_n;
236 }
237 
pmt_validate_es(const uint8_t * p_pmt,const uint8_t * p_pmt_n,uint16_t i_desclength)238 static inline bool pmt_validate_es(const uint8_t *p_pmt, const uint8_t *p_pmt_n,
239                                    uint16_t i_desclength)
240 {
241     uint16_t i_section_size = psi_get_length(p_pmt) + PSI_HEADER_SIZE
242                                - PSI_CRC_SIZE;
243     return (p_pmt_n + PMT_ES_SIZE + i_desclength
244              <= p_pmt + i_section_size);
245 }
246 
pmt_validate(const uint8_t * p_pmt)247 static inline bool pmt_validate(const uint8_t *p_pmt)
248 {
249     uint16_t i_section_size = psi_get_length(p_pmt) + PSI_HEADER_SIZE
250                                - PSI_CRC_SIZE;
251     const uint8_t *p_pmt_n;
252 
253     if (!psi_get_syntax(p_pmt) || psi_get_section(p_pmt)
254          || psi_get_lastsection(p_pmt)
255          || psi_get_tableid(p_pmt) != PMT_TABLE_ID)
256         return false;
257 
258     if (!psi_check_crc(p_pmt))
259         return false;
260 
261     if (i_section_size < PMT_HEADER_SIZE
262          || i_section_size < PMT_HEADER_SIZE + pmt_get_desclength(p_pmt))
263         return false;
264 
265     if (!descs_validate(p_pmt + 10))
266         return false;
267 
268     p_pmt_n = p_pmt + PMT_HEADER_SIZE + pmt_get_desclength(p_pmt);
269 
270     while (p_pmt_n + PMT_ES_SIZE - p_pmt <= i_section_size
271             && p_pmt_n + PMT_ES_SIZE + pmtn_get_desclength(p_pmt_n) - p_pmt
272                 <= i_section_size) {
273         if (!descs_validate(p_pmt_n + 3))
274             return false;
275 
276         p_pmt_n += PMT_ES_SIZE + pmtn_get_desclength(p_pmt_n);
277     }
278 
279     return (p_pmt_n - p_pmt == i_section_size);
280 }
281 
pmt_find_es(uint8_t * p_pmt,uint16_t i_pid)282 static inline uint8_t *pmt_find_es(uint8_t *p_pmt, uint16_t i_pid)
283 {
284     uint8_t *p_es;
285     uint8_t j = 0;
286 
287     while ((p_es = pmt_get_es(p_pmt, j)) != NULL) {
288         j++;
289         if (pmtn_get_pid(p_es) == i_pid)
290             return p_es;
291     }
292 
293     return NULL;
294 }
295 
296 #ifdef __cplusplus
297 }
298 #endif
299 
300 #endif
301