/src/vlc/modules/demux/mpeg/ps.h
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * ps.h: Program Stream demuxer helper |
3 | | ***************************************************************************** |
4 | | * Copyright (C) 2004-2009 VLC authors and VideoLAN |
5 | | * |
6 | | * Authors: Laurent Aimar <fenrir@via.ecp.fr> |
7 | | * |
8 | | * This program is free software; you can redistribute it and/or modify it |
9 | | * under the terms of the GNU Lesser General Public License as published by |
10 | | * the Free Software Foundation; either version 2.1 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * This program is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU Lesser General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program; if not, write to the Free Software Foundation, |
20 | | * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. |
21 | | *****************************************************************************/ |
22 | | |
23 | | #include <assert.h> |
24 | | #include <vlc_demux.h> |
25 | | #include "timestamps.h" |
26 | | |
27 | 5.52M | #define PS_STREAM_ID_END_STREAM 0xB9 |
28 | 2.95k | #define PS_STREAM_ID_PACK_HEADER 0xBA |
29 | 3 | #define PS_STREAM_ID_SYSTEM_HEADER 0xBB |
30 | 14.0k | #define PS_STREAM_ID_MAP 0xBC |
31 | 2.06k | #define PS_STREAM_ID_PRIVATE_STREAM1 0xBD |
32 | 1 | #define PS_STREAM_ID_PADDING 0xBE |
33 | 10.4k | #define PS_STREAM_ID_EXTENDED 0xFD |
34 | 4.88k | #define PS_STREAM_ID_DIRECTORY 0xFF |
35 | | |
36 | | /* 256-0xC0 for normal stream, 256 for 0xbd stream, 256 for 0xfd stream, 8 for 0xa0 AOB stream */ |
37 | 2.21M | #define PS_TK_COUNT (256+256+256+8 - 0xc0) |
38 | | static inline unsigned ps_id_to_tk( unsigned i_id ) |
39 | 2.24k | { |
40 | 2.24k | assert(i_id >= 0xc0); |
41 | 2.24k | if(unlikely(i_id < 0xc0)) |
42 | 0 | return 0; |
43 | 2.24k | else if( i_id <= 0xff ) |
44 | 2.13k | return i_id - 0xc0; |
45 | 111 | else if( (i_id & 0xff00) == 0xbd00 ) |
46 | 106 | return 256-0xC0 + (i_id & 0xff); |
47 | 5 | else if( (i_id & 0xff00) == 0xfd00 ) |
48 | 0 | return 512-0xc0 + (i_id & 0xff); |
49 | 5 | else |
50 | 5 | return 768-0xc0 + (i_id & 0x07); |
51 | 2.24k | } |
52 | | |
53 | | typedef struct ps_psm_t ps_psm_t; |
54 | | static inline uint8_t ps_id_to_type( const ps_psm_t *, uint16_t ); |
55 | | static inline const uint8_t *ps_id_to_lang( const ps_psm_t *, uint16_t ); |
56 | | |
57 | | typedef struct |
58 | | { |
59 | | bool b_configured; |
60 | | bool b_updated; |
61 | | int i_skip; |
62 | | int i_id; |
63 | | int i_next_block_flags; |
64 | | es_out_id_t *es; |
65 | | es_format_t fmt; |
66 | | vlc_tick_t i_first_pts; |
67 | | vlc_tick_t i_last_pts; |
68 | | |
69 | | } ps_track_t; |
70 | | |
71 | | /* Init a set of track */ |
72 | | static inline void ps_track_init( ps_track_t tk[PS_TK_COUNT] ) |
73 | 7 | { |
74 | 7 | int i; |
75 | 4.09k | for( i = 0; i < PS_TK_COUNT; i++ ) |
76 | 4.08k | { |
77 | 4.08k | tk[i].b_configured = false; |
78 | 4.08k | tk[i].b_updated = false; |
79 | 4.08k | tk[i].i_skip = 0; |
80 | 4.08k | tk[i].i_id = 0; |
81 | 4.08k | tk[i].i_next_block_flags = 0; |
82 | 4.08k | tk[i].es = NULL; |
83 | 4.08k | tk[i].i_first_pts = VLC_TICK_INVALID; |
84 | 4.08k | tk[i].i_last_pts = VLC_TICK_INVALID; |
85 | 4.08k | es_format_Init( &tk[i].fmt, UNKNOWN_ES, 0 ); |
86 | 4.08k | } |
87 | 7 | } |
88 | | |
89 | | static inline bool ps_is_H264( const uint8_t *p_data, size_t i_data ) |
90 | 1.47k | { |
91 | 1.47k | const uint8_t startcode[3] = { 0, 0, 1 }; |
92 | 1.47k | int i_flags = 0; |
93 | | |
94 | 1.47k | if( i_data < 9 || |
95 | 1.47k | (!memcmp( p_data, startcode, 3 ) && |
96 | 1.47k | !memcmp( &p_data[1], startcode, 3 )) ) |
97 | 0 | return false; |
98 | | |
99 | | /* Shitty H264 probing. We need a centralized way do to this */ |
100 | 11.6k | while( i_data > 5 ) |
101 | 10.7k | { |
102 | 10.7k | if( !memcmp( p_data, startcode, 3 ) ) |
103 | 591 | { |
104 | 591 | if(p_data[3] == 0x67) |
105 | 0 | i_flags ^= 0x01; |
106 | 591 | else if(p_data[3] == 0x68) |
107 | 0 | i_flags ^= 0x02; |
108 | 591 | else if( p_data[3] & 0x80 ) |
109 | 316 | return false; |
110 | 275 | else if( (p_data[3] & 0x1F) > 23 || (p_data[3] & 0x1F) < 1 ) |
111 | 23 | return false; |
112 | 252 | else if( (p_data[3] & 0x1F) < 6 ) |
113 | 252 | return (i_flags == 0x03); |
114 | 591 | } |
115 | 10.1k | p_data++; |
116 | 10.1k | i_data--; |
117 | 10.1k | } |
118 | | |
119 | 882 | return false; |
120 | 1.47k | } |
121 | | |
122 | | static inline bool ps_is_EAC3( const uint8_t *p_data, size_t i_data ) |
123 | 2 | { |
124 | | /* AC-3 marking, see vlc_a52_header_Parse */ |
125 | 2 | if( i_data < 8 || p_data[0] != 0x0b || p_data[1] != 0x77 ) |
126 | 2 | return false; |
127 | 0 | int bsid = p_data[5] >> 3; |
128 | 0 | return bsid > 10 && bsid <= 16; |
129 | 2 | } |
130 | | |
131 | | /* From id fill i_skip and es_format_t */ |
132 | | static inline int ps_track_fill( ps_track_t *tk, ps_psm_t *p_psm, |
133 | | int i_id, |
134 | | const uint8_t *p_pkt, size_t i_pkt, |
135 | | bool b_mpeg2only ) |
136 | 18.6k | { |
137 | 18.6k | tk->i_skip = 0; |
138 | 18.6k | tk->i_id = i_id; |
139 | | |
140 | 18.6k | if( ( i_id&0xff00 ) == 0xbd00 ) /* 0xBD00 -> 0xBDFF, Private Stream 1 */ |
141 | 8.40k | { |
142 | 8.40k | if( ( i_id&0xf8 ) == 0x88 || /* 0x88 -> 0x8f - Can be DTS-HD primary audio in evob */ |
143 | 8.40k | ( i_id&0xf8 ) == 0x98 ) /* 0x98 -> 0x9f - Can be DTS-HD secondary audio in evob */ |
144 | 5 | { |
145 | 5 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DTS ); |
146 | 5 | tk->i_skip = 4; |
147 | 5 | } |
148 | 8.40k | else if( ( i_id&0xf8 ) == 0x80 || /* 0x80 -> 0x87 */ |
149 | 8.40k | ( i_id&0xf0 ) == 0xc0 ) /* 0xc0 -> 0xcf AC-3, Can also be DD+/E-AC3 in evob */ |
150 | 10 | { |
151 | 10 | bool b_eac3 = false; |
152 | 10 | if( ( i_id&0xf0 ) == 0xc0 ) |
153 | 5 | { |
154 | 5 | if( p_pkt == NULL || i_pkt < 9 ) |
155 | 0 | return VLC_EGENERIC; |
156 | | |
157 | 5 | unsigned i_start = 9 + p_pkt[8]; |
158 | 5 | if( i_start + 9 < i_pkt ) |
159 | 2 | b_eac3 = ps_is_EAC3( &p_pkt[i_start + 4], i_pkt - i_start - 4 ); |
160 | 5 | } |
161 | | |
162 | 10 | es_format_Change( &tk->fmt, AUDIO_ES, b_eac3 ? VLC_CODEC_EAC3 : VLC_CODEC_A52 ); |
163 | 10 | tk->i_skip = 4; |
164 | 10 | } |
165 | 8.39k | else if( ( i_id&0xfc ) == 0x00 ) /* 0x00 -> 0x03 */ |
166 | 2.78k | { |
167 | 2.78k | es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_CVD ); |
168 | 2.78k | } |
169 | 5.60k | else if( ( i_id&0xff ) == 0x0b ) /* 0x0b */ |
170 | 0 | { |
171 | 0 | bool b_eac3 = i_pkt > 8 && ps_is_EAC3( &p_pkt[9], i_pkt - 9 ); |
172 | 0 | es_format_Change( &tk->fmt, AUDIO_ES, b_eac3 ? VLC_CODEC_EAC3 : VLC_CODEC_A52 ); |
173 | 0 | } |
174 | 5.60k | else if( ( i_id&0xff ) == 0x10 ) /* 0x10 */ |
175 | 2.78k | { |
176 | 2.78k | es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_TELETEXT ); |
177 | 2.78k | } |
178 | 2.82k | else if( ( i_id&0xe0 ) == 0x20 ) /* 0x20 -> 0x3f */ |
179 | 2.80k | { |
180 | 2.80k | es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_SPU ); |
181 | 2.80k | tk->i_skip = 1; |
182 | 2.80k | } |
183 | 20 | else if( ( i_id&0xff ) == 0x70 ) /* 0x70 */ |
184 | 0 | { |
185 | 0 | es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_OGT ); |
186 | 0 | } |
187 | 20 | else if( ( i_id&0xf0 ) == 0xa0 ) /* 0xa0 -> 0xaf */ |
188 | 10 | { |
189 | 10 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM ); |
190 | 10 | tk->i_skip = 1; |
191 | 10 | } |
192 | 10 | else if( ( i_id&0xf0 ) == 0xb0 ) /* 0xb0 -> 0xbf */ |
193 | 3 | { |
194 | 3 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_TRUEHD ); |
195 | 3 | tk->i_skip = 5; |
196 | 3 | } |
197 | 7 | else |
198 | 7 | { |
199 | 7 | es_format_Change( &tk->fmt, UNKNOWN_ES, 0 ); |
200 | 7 | return VLC_EGENERIC; |
201 | 7 | } |
202 | 8.40k | } |
203 | 10.2k | else if( (i_id&0xff00) == 0xfd00 ) /* 0xFD00 -> 0xFDFF */ |
204 | 0 | { |
205 | 0 | uint8_t i_sub_id = i_id & 0xff; |
206 | 0 | if( ( i_sub_id >= 0x55 && i_sub_id <= 0x5f ) || /* Can be primary VC-1 in evob */ |
207 | 0 | ( i_sub_id >= 0x75 && i_sub_id <= 0x7f ) ) /* Secondary VC-1 */ |
208 | 0 | { |
209 | 0 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_VC1 ); |
210 | 0 | } |
211 | 0 | else |
212 | 0 | { |
213 | 0 | es_format_Change( &tk->fmt, UNKNOWN_ES, 0 ); |
214 | 0 | return VLC_EGENERIC; |
215 | 0 | } |
216 | 0 | } |
217 | 10.2k | else if( (i_id&0xff00) == 0xa000 ) /* 0xA000 -> 0xA0FF */ |
218 | 2.78k | { |
219 | 2.78k | uint8_t i_sub_id = i_id & 0x07; |
220 | 2.78k | if( i_sub_id == 0 ) |
221 | 0 | { |
222 | 0 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DVDA_LPCM ); |
223 | 0 | tk->i_skip = 1; |
224 | 0 | } |
225 | 2.78k | else if( i_sub_id == 1 ) |
226 | 2.78k | { |
227 | 2.78k | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MLP ); |
228 | 2.78k | tk->i_skip = -1; /* It's a hack for variable skip value */ |
229 | 2.78k | } |
230 | 0 | else |
231 | 0 | { |
232 | 0 | es_format_Change( &tk->fmt, UNKNOWN_ES, 0 ); |
233 | 0 | return VLC_EGENERIC; |
234 | 0 | } |
235 | 2.78k | } |
236 | 7.42k | else |
237 | 7.42k | { |
238 | 7.42k | int i_type = ps_id_to_type( p_psm , i_id ); |
239 | | |
240 | 7.42k | es_format_Change( &tk->fmt, UNKNOWN_ES, 0 ); |
241 | | |
242 | 7.42k | if( (i_id&0xf0) == 0xe0 ) /* 0xe0 -> 0xef */ |
243 | 2.79k | { |
244 | 2.79k | if( i_type == 0x01 ) |
245 | 427 | { |
246 | 427 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV ); |
247 | 427 | tk->fmt.i_original_fourcc = VLC_CODEC_MP1V; |
248 | 427 | } |
249 | 2.36k | else if( i_type == 0x02 ) |
250 | 0 | { |
251 | 0 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV ); |
252 | 0 | } |
253 | 2.36k | else if( i_type == 0x10 ) |
254 | 887 | { |
255 | 887 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MP4V ); |
256 | 887 | } |
257 | 1.47k | else if( i_type == 0x1b ) |
258 | 0 | { |
259 | 0 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 ); |
260 | 0 | } |
261 | 1.47k | else if( i_type == 0x24 ) |
262 | 0 | { |
263 | 0 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_HEVC ); |
264 | 0 | } |
265 | 1.47k | else if( i_id == 0xe2 || /* Primary H.264 in evob */ |
266 | 1.47k | i_id == 0xe3 ) /* Secondary H.264 in evob */ |
267 | 0 | { |
268 | 0 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 ); |
269 | 0 | } |
270 | 1.47k | else if( p_pkt && i_type == 0x00 && /* Not from PSM */ |
271 | 1.47k | i_pkt > 9 + 5 && |
272 | 1.47k | i_pkt > 9U + 5 + p_pkt[8] && |
273 | 1.47k | ps_is_H264( &p_pkt[ 9 + p_pkt[8] ], |
274 | 1.47k | i_pkt - 9 - p_pkt[8] ) ) |
275 | 0 | { |
276 | 0 | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 ); |
277 | 0 | } |
278 | 1.47k | else if( tk->fmt.i_cat == UNKNOWN_ES && |
279 | 1.47k | ( p_pkt != NULL /* Not system */ || b_mpeg2only ) ) |
280 | 1.47k | { |
281 | 1.47k | es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV ); |
282 | 1.47k | } |
283 | 2.79k | } |
284 | 4.63k | else if( ( i_id&0xe0 ) == 0xc0 ) /* 0xc0 -> 0xdf */ |
285 | 4.63k | { |
286 | 4.63k | if( i_type == 0x03 || |
287 | 4.63k | i_type == 0x04 ) |
288 | 0 | { |
289 | 0 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA ); |
290 | 0 | } |
291 | 4.63k | else if( i_type == 0x0f ) |
292 | 0 | { |
293 | 0 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A ); |
294 | 0 | tk->fmt.i_original_fourcc = VLC_FOURCC('A','D','T','S'); |
295 | 0 | } |
296 | 4.63k | else if( i_type == 0x11 ) |
297 | 142 | { |
298 | 142 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A ); |
299 | 142 | tk->fmt.i_original_fourcc = VLC_FOURCC('L','A','T','M'); |
300 | 142 | } |
301 | 4.48k | else if( i_type == 0x2d ) |
302 | 0 | { |
303 | 0 | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPEGH ); |
304 | 0 | } |
305 | 4.48k | else if( tk->fmt.i_cat == UNKNOWN_ES ) |
306 | 4.48k | { |
307 | 4.48k | es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA ); |
308 | 4.48k | } |
309 | 4.63k | } |
310 | 4 | else if( tk->fmt.i_cat == UNKNOWN_ES ) return VLC_EGENERIC; |
311 | 7.42k | } |
312 | | |
313 | | /* PES packets usually contain truncated frames */ |
314 | 18.6k | tk->fmt.b_packetized = false; |
315 | 18.6k | tk->fmt.i_priority = ~i_id & 0x0F; |
316 | | |
317 | 18.6k | if( ps_id_to_lang( p_psm, i_id ) ) |
318 | 1.45k | { |
319 | 1.45k | tk->fmt.psz_language = malloc( 4 ); |
320 | 1.45k | if( tk->fmt.psz_language ) |
321 | 1.45k | { |
322 | 1.45k | memcpy( tk->fmt.psz_language, ps_id_to_lang( p_psm , i_id ), 3 ); |
323 | 1.45k | tk->fmt.psz_language[3] = 0; |
324 | 1.45k | } |
325 | 1.45k | } |
326 | | |
327 | 18.6k | return (tk->fmt.i_cat != UNKNOWN_ES || p_pkt) ? VLC_SUCCESS : VLC_EGENERIC; |
328 | 18.6k | } |
329 | | |
330 | | /* return the id of a PES (should be valid) */ |
331 | | static inline int ps_pkt_id( const uint8_t *p_pkt, size_t i_pkt ) |
332 | 2.54k | { |
333 | 2.54k | if(unlikely(i_pkt < 4)) |
334 | 0 | return 0; |
335 | 2.54k | if( p_pkt[3] == 0xbd ) |
336 | 112 | { |
337 | 112 | uint8_t i_sub_id = 0; |
338 | 112 | if( i_pkt >= 9 && |
339 | 112 | i_pkt > 9 + (size_t)p_pkt[8] ) |
340 | 99 | { |
341 | 99 | const unsigned i_start = 9 + p_pkt[8]; |
342 | 99 | i_sub_id = p_pkt[i_start]; |
343 | | |
344 | 99 | if( (i_sub_id & 0xfe) == 0xa0 && |
345 | 99 | i_pkt >= i_start + 7 && |
346 | 99 | ( p_pkt[i_start + 5] >= 0xc0 || |
347 | 5 | p_pkt[i_start + 6] != 0x80 ) ) |
348 | 5 | { |
349 | | /* AOB LPCM/MLP extension |
350 | | * XXX for MLP I think that the !=0x80 test is not good and |
351 | | * will fail for some valid files */ |
352 | 5 | return 0xa000 | (i_sub_id & 0x01); |
353 | 5 | } |
354 | 99 | } |
355 | | |
356 | | /* VOB extension */ |
357 | 107 | return 0xbd00 | i_sub_id; |
358 | 112 | } |
359 | 2.43k | else if( i_pkt >= 9 && |
360 | 2.43k | p_pkt[3] == 0xfd && |
361 | 2.43k | (p_pkt[6]&0xC0) == 0x80 && /* mpeg2 */ |
362 | 2.43k | (p_pkt[7]&0x01) == 0x01 ) /* extension_flag */ |
363 | 0 | { |
364 | | /* ISO 13818 amendment 2 and SMPTE RP 227 */ |
365 | 0 | const uint8_t i_flags = p_pkt[7]; |
366 | 0 | unsigned int i_skip = 9; |
367 | | |
368 | | /* Find PES extension */ |
369 | 0 | if( (i_flags & 0x80 ) ) |
370 | 0 | { |
371 | 0 | i_skip += 5; /* pts */ |
372 | 0 | if( (i_flags & 0x40) ) |
373 | 0 | i_skip += 5; /* dts */ |
374 | 0 | } |
375 | 0 | if( (i_flags & 0x20 ) ) |
376 | 0 | i_skip += 6; |
377 | 0 | if( (i_flags & 0x10 ) ) |
378 | 0 | i_skip += 3; |
379 | 0 | if( (i_flags & 0x08 ) ) |
380 | 0 | i_skip += 1; |
381 | 0 | if( (i_flags & 0x04 ) ) |
382 | 0 | i_skip += 1; |
383 | 0 | if( (i_flags & 0x02 ) ) |
384 | 0 | i_skip += 2; |
385 | |
|
386 | 0 | if( i_skip < i_pkt && (p_pkt[i_skip]&0x01) ) |
387 | 0 | { |
388 | 0 | const uint8_t i_flags2 = p_pkt[i_skip]; |
389 | | |
390 | | /* Find PES extension 2 */ |
391 | 0 | i_skip += 1; |
392 | 0 | if( i_flags2 & 0x80 ) |
393 | 0 | i_skip += 16; |
394 | 0 | if( (i_flags2 & 0x40) && i_skip < i_pkt ) |
395 | 0 | i_skip += 1 + p_pkt[i_skip]; |
396 | 0 | if( i_flags2 & 0x20 ) |
397 | 0 | i_skip += 2; |
398 | 0 | if( i_flags2 & 0x10 ) |
399 | 0 | i_skip += 2; |
400 | |
|
401 | 0 | if( i_skip + 1 < i_pkt ) |
402 | 0 | { |
403 | 0 | const int i_extension_field_length = p_pkt[i_skip]&0x7f; |
404 | 0 | if( i_extension_field_length >=1 ) |
405 | 0 | { |
406 | 0 | int i_stream_id_extension_flag = (p_pkt[i_skip+1] >> 7)&0x1; |
407 | 0 | if( i_stream_id_extension_flag == 0 ) |
408 | 0 | return 0xfd00 | (p_pkt[i_skip+1]&0x7f); |
409 | 0 | } |
410 | 0 | } |
411 | 0 | } |
412 | 0 | } |
413 | 2.43k | return p_pkt[3]; |
414 | 2.54k | } |
415 | | |
416 | | /* return the size of the next packet */ |
417 | | static inline int ps_pkt_size( const uint8_t *p, int i_peek ) |
418 | 7.13k | { |
419 | 7.13k | if( unlikely(i_peek < 4) ) |
420 | 0 | return -1; |
421 | | |
422 | 7.13k | switch( p[3] ) |
423 | 7.13k | { |
424 | 2 | case PS_STREAM_ID_END_STREAM: |
425 | 2 | return 4; |
426 | | |
427 | 0 | case PS_STREAM_ID_PACK_HEADER: |
428 | 0 | if( i_peek > 4 ) |
429 | 0 | { |
430 | 0 | if( i_peek >= 14 && (p[4] >> 6) == 0x01 ) |
431 | 0 | return 14 + (p[13]&0x07); |
432 | 0 | else if( i_peek >= 12 && (p[4] >> 4) == 0x02 ) |
433 | 0 | return 12; |
434 | 0 | } |
435 | 0 | break; |
436 | | |
437 | 2 | case PS_STREAM_ID_SYSTEM_HEADER: |
438 | 4.88k | case PS_STREAM_ID_MAP: |
439 | 4.88k | case PS_STREAM_ID_DIRECTORY: |
440 | 7.13k | default: |
441 | 7.13k | if( i_peek >= 6 ) |
442 | 7.13k | return 6 + ((p[4]<<8) | p[5] ); |
443 | 7.13k | } |
444 | 0 | return -1; |
445 | 7.13k | } |
446 | | |
447 | | /* parse a PACK PES */ |
448 | | static inline int ps_pkt_parse_pack( const uint8_t *p_pkt, size_t i_pkt, |
449 | | vlc_tick_t *pi_scr, int *pi_mux_rate ) |
450 | 0 | { |
451 | 0 | const uint8_t *p = p_pkt; |
452 | 0 | if( i_pkt >= 14 && (p[4] >> 6) == 0x01 ) |
453 | 0 | { |
454 | 0 | *pi_scr = FROM_SCALE( ExtractPackHeaderTimestamp( &p[4] ) ); |
455 | 0 | *pi_mux_rate = ( p[10] << 14 )|( p[11] << 6 )|( p[12] >> 2); |
456 | 0 | } |
457 | 0 | else if( i_pkt >= 12 && (p[4] >> 4) == 0x02 ) /* MPEG-1 Pack SCR, same bits as PES/PTS */ |
458 | 0 | { |
459 | 0 | stime_t i_scr; |
460 | 0 | if(!ExtractPESTimestamp( &p[4], 0x02, &i_scr )) |
461 | 0 | return VLC_EGENERIC; |
462 | 0 | *pi_scr = FROM_SCALE( i_scr ); |
463 | 0 | *pi_mux_rate = ( ( p[9]&0x7f )<< 15 )|( p[10] << 7 )|( p[11] >> 1); |
464 | 0 | } |
465 | 0 | else |
466 | 0 | { |
467 | 0 | return VLC_EGENERIC; |
468 | 0 | } |
469 | 0 | return VLC_SUCCESS; |
470 | 0 | } |
471 | | |
472 | | /* Parse a SYSTEM PES */ |
473 | | static inline int ps_pkt_parse_system( const uint8_t *p_pkt, size_t i_pkt, |
474 | | ps_psm_t *p_psm, |
475 | | ps_track_t tk[PS_TK_COUNT] ) |
476 | 1 | { |
477 | 1 | const uint8_t *p = &p_pkt[6 + 3 + 1 + 1 + 1]; |
478 | 1 | const uint8_t *p_pktend = &p_pkt[i_pkt]; |
479 | | |
480 | | /* System header is not usable if it references private streams (0xBD) |
481 | | * or 'all audio streams' (0xB8) or 'all video streams' (0xB9) */ |
482 | 1 | while( p < p_pktend && (p[0] & 0x80) ) |
483 | 0 | { |
484 | 0 | int i_id = p[0]; |
485 | 0 | switch( i_id ) |
486 | 0 | { |
487 | 0 | case 0xB7: |
488 | 0 | if( p_pktend - p < 6 ) |
489 | 0 | return VLC_EGENERIC; |
490 | 0 | i_id = ((int)PS_STREAM_ID_EXTENDED << 8) | (p[2] & 0x7F); |
491 | 0 | p += 6; |
492 | 0 | break; |
493 | 0 | default: |
494 | 0 | if( p_pktend - p < 3 ) |
495 | 0 | return VLC_EGENERIC; |
496 | 0 | p += 3; |
497 | 0 | break; |
498 | 0 | } |
499 | | |
500 | 0 | if( i_id < 0xc0 ) |
501 | 0 | continue; |
502 | | |
503 | 0 | unsigned i_tk = ps_id_to_tk( i_id ); |
504 | 0 | if( !tk[i_tk].b_configured ) |
505 | 0 | ps_track_fill( &tk[i_tk], p_psm, i_id, NULL, 0, false ); |
506 | 0 | } |
507 | 1 | return VLC_SUCCESS; |
508 | 1 | } |
509 | | |
510 | | /* Parse a PES (and skip i_skip_extra in the payload) */ |
511 | | static inline int ps_pkt_parse_pes( vlc_object_t *p_object, block_t *p_pes, int i_skip_extra ) |
512 | 2.23k | { |
513 | 2.23k | unsigned int i_skip = 0; |
514 | 2.23k | stime_t i_pts = -1; |
515 | 2.23k | stime_t i_dts = -1; |
516 | 2.23k | uint8_t i_stream_id = 0; |
517 | 2.23k | bool b_pes_scrambling = false; |
518 | | |
519 | 2.23k | if( ParsePESHeader( p_object, p_pes->p_buffer, p_pes->i_buffer, |
520 | 2.23k | &i_skip, &i_dts, &i_pts, &i_stream_id, &b_pes_scrambling ) != VLC_SUCCESS ) |
521 | 374 | return VLC_EGENERIC; |
522 | | |
523 | 1.85k | if( b_pes_scrambling ) |
524 | 21 | p_pes->i_flags |= BLOCK_FLAG_SCRAMBLED; |
525 | | |
526 | 1.85k | if( i_skip_extra >= 0 ) |
527 | 1.85k | i_skip += i_skip_extra; |
528 | 3 | else if( p_pes->i_buffer > i_skip + 3 && |
529 | 3 | ( ps_pkt_id( p_pes->p_buffer, p_pes->i_buffer ) == 0xa001 || |
530 | 1 | ps_pkt_id( p_pes->p_buffer, p_pes->i_buffer ) == 0xbda1 ) ) |
531 | 1 | i_skip += 4 + p_pes->p_buffer[i_skip+3]; |
532 | | |
533 | 1.85k | if( p_pes->i_buffer <= i_skip ) |
534 | 198 | { |
535 | 198 | return VLC_EGENERIC; |
536 | 198 | } |
537 | | |
538 | 1.65k | p_pes->p_buffer += i_skip; |
539 | 1.65k | p_pes->i_buffer -= i_skip; |
540 | | |
541 | | /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */ |
542 | 1.65k | if( i_pts >= 0 && i_dts < 0 ) |
543 | 48 | i_dts = i_pts; |
544 | | |
545 | 1.65k | if( i_dts >= 0 ) |
546 | 48 | p_pes->i_dts = FROM_SCALE( i_dts ); |
547 | 1.65k | if( i_pts >= 0 ) |
548 | 48 | p_pes->i_pts = FROM_SCALE( i_pts ); |
549 | | |
550 | 1.65k | return VLC_SUCCESS; |
551 | 1.85k | } |
552 | | |
553 | | typedef struct |
554 | | { |
555 | | /* Language is iso639-2T */ |
556 | | uint8_t lang[3]; |
557 | | } ps_descriptors_t; |
558 | | |
559 | | /* Program stream map handling */ |
560 | | typedef struct ps_es_t |
561 | | { |
562 | | uint8_t i_type; |
563 | | uint16_t i_id; |
564 | | |
565 | | ps_descriptors_t desc; |
566 | | |
567 | | } ps_es_t; |
568 | | |
569 | | struct ps_psm_t |
570 | | { |
571 | | uint8_t i_version; |
572 | | |
573 | | size_t i_es; |
574 | | ps_es_t *es; |
575 | | |
576 | | ps_descriptors_t uniqueextdesc; |
577 | | }; |
578 | | |
579 | | static inline uint8_t ps_id_to_type( const ps_psm_t *p_psm, uint16_t i_id ) |
580 | 7.42k | { |
581 | 7.42k | size_t i; |
582 | 18.2k | for( i = 0; p_psm && i < p_psm->i_es; i++ ) |
583 | 12.3k | { |
584 | 12.3k | if( p_psm->es[i].i_id == i_id ) return p_psm->es[i].i_type; |
585 | 12.3k | } |
586 | 5.97k | return 0; |
587 | 7.42k | } |
588 | | |
589 | | static inline const uint8_t *ps_id_to_lang( const ps_psm_t *p_psm, uint16_t i_id ) |
590 | 20.0k | { |
591 | 20.0k | size_t i; |
592 | 50.9k | for( i = 0; p_psm && i < p_psm->i_es; i++ ) |
593 | 33.8k | { |
594 | 33.8k | if( p_psm->es[i].i_id == i_id ) |
595 | 2.91k | return p_psm->es[i].desc.lang; |
596 | 33.8k | } |
597 | 17.1k | return 0; |
598 | 20.0k | } |
599 | | |
600 | | static inline void ps_psm_init( ps_psm_t *p_psm ) |
601 | 7 | { |
602 | 7 | p_psm->i_version = 0xFF; |
603 | 7 | p_psm->i_es = 0; |
604 | 7 | p_psm->es = 0; |
605 | 7 | memset( &p_psm->uniqueextdesc, 0, 3 ); |
606 | 7 | } |
607 | | |
608 | | static inline void ps_psm_destroy( ps_psm_t *p_psm ) |
609 | 2.79k | { |
610 | 2.79k | free( p_psm->es ); |
611 | 2.79k | p_psm->es = NULL; |
612 | 2.79k | p_psm->i_es = 0; |
613 | 2.79k | } |
614 | | |
615 | | static inline void ps_parse_descriptors( const uint8_t *p_data, size_t i_data, |
616 | | ps_descriptors_t *p_desc ) |
617 | 4.21k | { |
618 | 4.21k | while( i_data > 3 && i_data > 2u + p_data[1] ) |
619 | 0 | { |
620 | 0 | switch( p_data[0] ) |
621 | 0 | { |
622 | 0 | case 0x0A: /* ISO_639_language_descriptor */ |
623 | 0 | if( i_data >= 6 ) |
624 | 0 | memcpy( p_desc->lang, &p_data[2], 3 ); |
625 | 0 | break; |
626 | | |
627 | 0 | default: |
628 | 0 | break; |
629 | 0 | } |
630 | 0 | uint8_t i_desc_size = p_data[1]; |
631 | 0 | p_data += 2 + i_desc_size; |
632 | 0 | i_data -= 2 + i_desc_size; |
633 | 0 | } |
634 | 4.21k | } |
635 | | |
636 | | static inline int ps_psm_fill( ps_psm_t *p_psm, |
637 | | const uint8_t *p_buffer, size_t i_pkt, |
638 | | ps_track_t tk[PS_TK_COUNT]) |
639 | 4.58k | { |
640 | 4.58k | size_t i_length, i_info_length, i_es_base; |
641 | 4.58k | uint8_t i_version; |
642 | 4.58k | bool b_single_extension; |
643 | | |
644 | | // Demux() checks that we have at least 4 bytes, but we need |
645 | | // at least 10 to read up to the info_length field |
646 | 4.58k | assert(i_pkt >= 4); |
647 | 4.58k | if( !p_psm || i_pkt < 10 || p_buffer[3] != PS_STREAM_ID_MAP) |
648 | 3 | return VLC_EGENERIC; |
649 | | |
650 | 4.58k | i_length = GetWBE(&p_buffer[4]) + 6; |
651 | 4.58k | if( i_length > i_pkt ) return VLC_EGENERIC; |
652 | | |
653 | 4.57k | if((p_buffer[6] & 0x80) == 0) /* current_next_indicator */ |
654 | 407 | return VLC_EGENERIC; |
655 | | |
656 | 4.17k | b_single_extension = p_buffer[6] & 0x40; |
657 | 4.17k | i_version = (p_buffer[6] & 0xf8); |
658 | | |
659 | 4.17k | if( p_psm->i_version == i_version ) return VLC_EGENERIC; |
660 | | |
661 | 2.79k | ps_psm_destroy( p_psm ); |
662 | | |
663 | 2.79k | i_info_length = GetWBE(&p_buffer[8]); |
664 | 2.79k | if( i_info_length + 10 > i_length ) |
665 | 8 | return VLC_EGENERIC; |
666 | | |
667 | | /* Elementary stream map */ |
668 | | /* int i_esm_length = (uint16_t)(p_buffer[ 10 + i_info_length ] << 8) + |
669 | | p_buffer[ 11 + i_info_length]; */ |
670 | 2.78k | i_es_base = 12 + i_info_length; |
671 | | |
672 | 6.99k | while( i_es_base + 4 < i_length ) |
673 | 4.98k | { |
674 | 4.98k | ps_es_t *tmp_es = realloc( p_psm->es, sizeof(ps_es_t) * (p_psm->i_es+1) ); |
675 | 4.98k | if( tmp_es == NULL ) |
676 | 0 | break; |
677 | 4.98k | p_psm->es = tmp_es; |
678 | | |
679 | 4.98k | ps_es_t *p_es = &p_psm->es[ p_psm->i_es++ ]; |
680 | 4.98k | p_es->i_type = p_buffer[ i_es_base ]; |
681 | 4.98k | p_es->i_id = p_buffer[ i_es_base + 1 ]; |
682 | | |
683 | 4.98k | i_info_length = GetWBE(&p_buffer[ i_es_base + 2 ]); |
684 | | |
685 | 4.98k | if( i_es_base + 4 + i_info_length > i_length ) |
686 | 770 | break; |
687 | | |
688 | | /* TODO Add support for VC-1 stream: |
689 | | * stream_type=0xea, stream_id=0xfd AND registration |
690 | | * descriptor 0x5 with format_identifier == 0x56432D31 (VC-1) |
691 | | * (I need a sample that use PSM with VC-1) */ |
692 | | |
693 | 4.21k | if( p_es->i_id == PS_STREAM_ID_EXTENDED && b_single_extension == 0 ) |
694 | 0 | { |
695 | 0 | if( i_info_length < 3 ) |
696 | 0 | break; |
697 | 0 | p_es->i_id = (p_es->i_id << 8) | (p_buffer[i_es_base + 6] & 0x7F); |
698 | 0 | ps_parse_descriptors( &p_buffer[i_es_base + 4 + 3], |
699 | 0 | i_info_length - 3, |
700 | 0 | &p_psm->uniqueextdesc ); |
701 | 0 | } |
702 | 4.21k | else |
703 | 4.21k | { |
704 | 4.21k | ps_parse_descriptors( &p_buffer[i_es_base + 4], |
705 | 4.21k | i_info_length, &p_es->desc ); |
706 | 4.21k | } |
707 | | |
708 | 4.21k | i_es_base += 4 + i_info_length; |
709 | 4.21k | } |
710 | | |
711 | | /* TODO: CRC */ |
712 | | |
713 | 2.78k | p_psm->i_version = i_version; |
714 | | |
715 | | /* Check/Modify our existing tracks */ |
716 | 1.62M | for( int i = 0; i < PS_TK_COUNT; i++ ) |
717 | 1.62M | { |
718 | 1.62M | if( !tk[i].b_configured ) |
719 | 1.60M | continue; |
720 | | |
721 | 18.5k | ps_track_t tk_tmp; |
722 | 18.5k | es_format_Init( &tk_tmp.fmt, UNKNOWN_ES, 0 ); |
723 | | |
724 | 18.5k | if( ps_track_fill( &tk_tmp, p_psm, tk[i].i_id, |
725 | 18.5k | p_buffer, i_pkt, false ) != VLC_SUCCESS ) |
726 | 0 | continue; |
727 | | |
728 | 18.5k | if( tk_tmp.fmt.i_codec == tk[i].fmt.i_codec ) |
729 | 17.5k | { |
730 | 17.5k | es_format_Clean( &tk_tmp.fmt ); |
731 | 17.5k | continue; |
732 | 17.5k | } |
733 | | |
734 | | /* replace with new version */ |
735 | 996 | tk_tmp.b_configured = true; |
736 | 996 | tk_tmp.b_updated = true; |
737 | 996 | tk_tmp.es = tk[i].es; |
738 | 996 | es_format_Clean( &tk[i].fmt ); |
739 | 996 | tk[i] = tk_tmp; |
740 | 996 | } |
741 | | |
742 | 2.78k | return VLC_SUCCESS; |
743 | 2.79k | } |