demux: ps: always shift 0xbd es id
[vlc.git] / modules / demux / mpeg / ps.h
blob2e135d636246416b55bef739323540cef619442e
1 /*****************************************************************************
2 * ps.h: Program Stream demuxer helper
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <assert.h>
25 #include <vlc_demux.h>
26 #include <vlc_memory.h>
27 #include "timestamps.h"
29 #define PS_STREAM_ID_END_STREAM 0xB9
30 #define PS_STREAM_ID_PACK_HEADER 0xBA
31 #define PS_STREAM_ID_SYSTEM_HEADER 0xBB
32 #define PS_STREAM_ID_MAP 0xBC
33 #define PS_STREAM_ID_PRIVATE_STREAM1 0xBD
34 #define PS_STREAM_ID_PADDING 0xBE
35 #define PS_STREAM_ID_EXTENDED 0xFD
36 #define PS_STREAM_ID_DIRECTORY 0xFF
38 /* 256-0xC0 for normal stream, 256 for 0xbd stream, 256 for 0xfd stream, 8 for 0xa0 AOB stream */
39 #define PS_TK_COUNT (256+256+256+8 - 0xc0)
40 #if 0
41 #define PS_ID_TO_TK( id ) ((id) <= 0xff ? (id) - 0xc0 : \
42 ((id)&0xff) + (((id)&0xff00) == 0xbd00 ? 256-0xC0 : 512-0xc0) )
43 #else
44 static inline int ps_id_to_tk( unsigned i_id )
46 if( i_id <= 0xff )
47 return i_id - 0xc0;
48 else if( (i_id & 0xff00) == 0xbd00 )
49 return 256-0xC0 + (i_id & 0xff);
50 else if( (i_id & 0xff00) == 0xfd00 )
51 return 512-0xc0 + (i_id & 0xff);
52 else
53 return 768-0xc0 + (i_id & 0x07);
55 #define PS_ID_TO_TK( id ) ps_id_to_tk( id )
56 #endif
58 typedef struct ps_psm_t ps_psm_t;
59 static inline int ps_id_to_type( const ps_psm_t *, int );
60 static inline const uint8_t *ps_id_to_lang( const ps_psm_t *, int );
62 typedef struct
64 bool b_configured;
65 bool b_seen;
66 int i_skip;
67 int i_id;
68 int i_next_block_flags;
69 es_out_id_t *es;
70 es_format_t fmt;
71 mtime_t i_first_pts;
72 mtime_t i_last_pts;
74 } ps_track_t;
76 /* Init a set of track */
77 static inline void ps_track_init( ps_track_t tk[PS_TK_COUNT] )
79 int i;
80 for( i = 0; i < PS_TK_COUNT; i++ )
82 tk[i].b_configured = false;
83 tk[i].b_seen = false;
84 tk[i].i_skip = 0;
85 tk[i].i_id = 0;
86 tk[i].i_next_block_flags = 0;
87 tk[i].es = NULL;
88 tk[i].i_first_pts = -1;
89 tk[i].i_last_pts = -1;
90 es_format_Init( &tk[i].fmt, UNKNOWN_ES, 0 );
94 static inline bool ps_is_H264( const uint8_t *p_data, size_t i_data )
96 const uint8_t startcode[3] = { 0, 0, 1 };
97 int i_flags = 0;
99 if( i_data < 9 ||
100 (!memcmp( p_data, startcode, 3 ) &&
101 !memcmp( &p_data[1], startcode, 3 )) )
102 return false;
104 /* Shitty H264 probing. We need a centralized way do to this */
105 while( i_data > 5 )
107 if( !memcmp( p_data, startcode, 3 ) )
109 if(p_data[3] == 0x67)
110 i_flags ^= 0x01;
111 else if(p_data[3] == 0x68)
112 i_flags ^= 0x02;
113 else if( p_data[3] & 0x80 )
114 return false;
115 else if( (p_data[3] & 0x1F) > 23 || (p_data[3] & 0x1F) < 1 )
116 return false;
117 else if( (p_data[3] & 0x1F) < 6 )
118 return (i_flags == 0x03);
120 p_data++;
121 i_data--;
124 return false;
127 /* From id fill i_skip and es_format_t */
128 static inline int ps_track_fill( ps_track_t *tk, ps_psm_t *p_psm,
129 int i_id, block_t *p_pkt, bool b_mpeg2only )
131 tk->i_skip = 0;
132 tk->i_id = i_id;
134 if( ( i_id&0xff00 ) == 0xbd00 ) /* 0xBD00 -> 0xBDFF, Private Stream 1 */
136 if( ( i_id&0xf8 ) == 0x88 || /* 0x88 -> 0x8f - Can be DTS-HD primary audio in evob */
137 ( i_id&0xf8 ) == 0x98 ) /* 0x98 -> 0x9f - Can be DTS-HD secondary audio in evob */
139 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DTS );
140 tk->i_skip = 4;
142 else if( ( i_id&0xf8 ) == 0x80 || /* 0x80 -> 0x87 */
143 ( i_id&0xf0 ) == 0xc0 ) /* 0xc0 -> 0xcf AC-3, Can also be DD+/E-AC3 in evob */
145 bool b_eac3 = false;
146 if( ( i_id&0xf0 ) == 0xc0 )
148 if( p_pkt == NULL || p_pkt->i_buffer < 9 )
149 return VLC_EGENERIC;
151 unsigned i_start = 9 + p_pkt->p_buffer[8];
152 if( i_start + 9 < p_pkt->i_buffer )
154 /* AC-3 marking, see vlc_a52_header_Parse */
155 if( p_pkt->p_buffer[i_start + 4] == 0x0b ||
156 p_pkt->p_buffer[i_start + 5] == 0x77 )
158 int bsid = p_pkt->p_buffer[i_start + 9] >> 3;
159 if( bsid > 10 )
160 b_eac3 = true;
165 es_format_Change( &tk->fmt, AUDIO_ES, b_eac3 ? VLC_CODEC_EAC3 : VLC_CODEC_A52 );
166 tk->i_skip = 4;
168 else if( ( i_id&0xfc ) == 0x00 ) /* 0x00 -> 0x03 */
170 es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_CVD );
172 else if( ( i_id&0xff ) == 0x10 ) /* 0x10 */
174 es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_TELETEXT );
176 else if( ( i_id&0xe0 ) == 0x20 ) /* 0x20 -> 0x3f */
178 es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_SPU );
179 tk->i_skip = 1;
181 else if( ( i_id&0xff ) == 0x70 ) /* 0x70 */
183 es_format_Change( &tk->fmt, SPU_ES, VLC_CODEC_OGT );
185 else if( ( i_id&0xf0 ) == 0xa0 ) /* 0xa0 -> 0xaf */
187 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
188 tk->i_skip = 1;
190 else if( ( i_id&0xf0 ) == 0xb0 ) /* 0xb0 -> 0xbf */
192 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_TRUEHD );
193 tk->i_skip = 5;
195 else
197 es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
198 return VLC_EGENERIC;
201 else if( (i_id&0xff00) == 0xfd00 ) /* 0xFD00 -> 0xFDFF */
203 uint8_t i_sub_id = i_id & 0xff;
204 if( ( i_sub_id >= 0x55 && i_sub_id <= 0x5f ) || /* Can be primary VC-1 in evob */
205 ( i_sub_id >= 0x75 && i_sub_id <= 0x7f ) ) /* Secondary VC-1 */
207 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_VC1 );
209 else
211 es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
212 return VLC_EGENERIC;
215 else if( (i_id&0xff00) == 0xa000 ) /* 0xA000 -> 0xA0FF */
217 uint8_t i_sub_id = i_id & 0x07;
218 if( i_sub_id == 0 )
220 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_DVDA_LPCM );
221 tk->i_skip = 1;
223 else if( i_sub_id == 1 )
225 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MLP );
226 tk->i_skip = -1; /* It's a hack for variable skip value */
228 else
230 es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
231 return VLC_EGENERIC;
234 else
236 int i_type = ps_id_to_type( p_psm , i_id );
238 es_format_Change( &tk->fmt, UNKNOWN_ES, 0 );
240 if( (i_id&0xf0) == 0xe0 ) /* 0xe0 -> 0xef */
242 if( i_type == 0x1b )
244 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 );
246 else if( i_type == 0x10 )
248 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MP4V );
250 else if( i_type == 0x01 )
252 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
253 tk->fmt.i_original_fourcc = VLC_CODEC_MP1V;
255 else if( i_type == 0x02 )
257 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
259 else if( i_id == 0xe2 || /* Primary H.264 in evob */
260 i_id == 0xe3 ) /* Seconday H.264 in evob */
262 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 );
264 else if( p_pkt && i_type == 0x00 && /* Not from PSM */
265 p_pkt->i_buffer > 9 + 5 &&
266 p_pkt->i_buffer > 9U + 5 + p_pkt->p_buffer[8] &&
267 ps_is_H264( &p_pkt->p_buffer[ 9 + p_pkt->p_buffer[8] ],
268 p_pkt->i_buffer - 9 - p_pkt->p_buffer[8] ) )
270 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_H264 );
272 else if( tk->fmt.i_cat == UNKNOWN_ES &&
273 ( p_pkt != NULL /* Not system */ || b_mpeg2only ) )
275 es_format_Change( &tk->fmt, VIDEO_ES, VLC_CODEC_MPGV );
278 else if( ( i_id&0xe0 ) == 0xc0 ) /* 0xc0 -> 0xdf */
280 if( i_type == 0x03 ||
281 i_type == 0x04 )
283 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA );
285 else if( i_type == 0x0f )
287 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A );
288 tk->fmt.i_original_fourcc = VLC_FOURCC('A','D','T','S');
290 else if( i_type == 0x11 )
292 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MP4A );
293 tk->fmt.i_original_fourcc = VLC_FOURCC('L','A','T','M');
295 else if( tk->fmt.i_cat == UNKNOWN_ES )
297 es_format_Change( &tk->fmt, AUDIO_ES, VLC_CODEC_MPGA );
300 else if( tk->fmt.i_cat == UNKNOWN_ES ) return VLC_EGENERIC;
303 /* PES packets usually contain truncated frames */
304 tk->fmt.b_packetized = false;
305 tk->fmt.i_priority = ~i_id & 0x0F;
307 if( ps_id_to_lang( p_psm, i_id ) )
309 tk->fmt.psz_language = malloc( 4 );
310 if( tk->fmt.psz_language )
312 memcpy( tk->fmt.psz_language, ps_id_to_lang( p_psm , i_id ), 3 );
313 tk->fmt.psz_language[3] = 0;
317 return (tk->fmt.i_cat != UNKNOWN_ES || p_pkt) ? VLC_SUCCESS : VLC_EGENERIC;
320 /* return the id of a PES (should be valid) */
321 static inline int ps_pkt_id( block_t *p_pkt )
323 if( p_pkt->p_buffer[3] == 0xbd )
325 uint8_t i_sub_id = 0;
326 if( p_pkt->i_buffer >= 9 &&
327 p_pkt->i_buffer >= 9 + (size_t)p_pkt->p_buffer[8] )
329 const unsigned i_start = 9 + p_pkt->p_buffer[8];
330 i_sub_id = p_pkt->p_buffer[i_start];
332 if( (i_sub_id & 0xfe) == 0xa0 &&
333 p_pkt->i_buffer >= i_start + 7 &&
334 ( p_pkt->p_buffer[i_start + 5] >= 0xc0 ||
335 p_pkt->p_buffer[i_start + 6] != 0x80 ) )
337 /* AOB LPCM/MLP extension
338 * XXX for MLP I think that the !=0x80 test is not good and
339 * will fail for some valid files */
340 return 0xa000 | (i_sub_id & 0x01);
344 /* VOB extension */
345 return 0xbd00 | i_sub_id;
347 else if( p_pkt->p_buffer[3] == 0xfd &&
348 p_pkt->i_buffer >= 9 &&
349 (p_pkt->p_buffer[6]&0xC0) == 0x80 && /* mpeg2 */
350 (p_pkt->p_buffer[7]&0x01) == 0x01 ) /* extension_flag */
352 /* ISO 13818 amendment 2 and SMPTE RP 227 */
353 const uint8_t i_flags = p_pkt->p_buffer[7];
354 unsigned int i_skip = 9;
356 /* Find PES extension */
357 if( (i_flags & 0x80 ) )
359 i_skip += 5; /* pts */
360 if( (i_flags & 0x40) )
361 i_skip += 5; /* dts */
363 if( (i_flags & 0x20 ) )
364 i_skip += 6;
365 if( (i_flags & 0x10 ) )
366 i_skip += 3;
367 if( (i_flags & 0x08 ) )
368 i_skip += 1;
369 if( (i_flags & 0x04 ) )
370 i_skip += 1;
371 if( (i_flags & 0x02 ) )
372 i_skip += 2;
374 if( i_skip < p_pkt->i_buffer && (p_pkt->p_buffer[i_skip]&0x01) )
376 const uint8_t i_flags2 = p_pkt->p_buffer[i_skip];
378 /* Find PES extension 2 */
379 i_skip += 1;
380 if( i_flags2 & 0x80 )
381 i_skip += 16;
382 if( (i_flags2 & 0x40) && i_skip < p_pkt->i_buffer )
383 i_skip += 1 + p_pkt->p_buffer[i_skip];
384 if( i_flags2 & 0x20 )
385 i_skip += 2;
386 if( i_flags2 & 0x10 )
387 i_skip += 2;
389 if( i_skip + 1 < p_pkt->i_buffer )
391 const int i_extension_field_length = p_pkt->p_buffer[i_skip]&0x7f;
392 if( i_extension_field_length >=1 )
394 int i_stream_id_extension_flag = (p_pkt->p_buffer[i_skip+1] >> 7)&0x1;
395 if( i_stream_id_extension_flag == 0 )
396 return 0xfd00 | (p_pkt->p_buffer[i_skip+1]&0x7f);
401 return p_pkt->p_buffer[3];
404 /* return the size of the next packet */
405 static inline int ps_pkt_size( const uint8_t *p, int i_peek )
407 if( unlikely(i_peek < 4) )
408 return -1;
410 switch( p[3] )
412 case PS_STREAM_ID_END_STREAM:
413 return 4;
415 case PS_STREAM_ID_PACK_HEADER:
416 if( i_peek > 4 )
418 if( i_peek >= 14 && (p[4] >> 6) == 0x01 )
419 return 14 + (p[13]&0x07);
420 else if( i_peek >= 12 && (p[4] >> 4) == 0x02 )
421 return 12;
423 break;
425 case PS_STREAM_ID_SYSTEM_HEADER:
426 case PS_STREAM_ID_MAP:
427 case PS_STREAM_ID_DIRECTORY:
428 default:
429 if( i_peek >= 6 )
430 return 6 + ((p[4]<<8) | p[5] );
432 return -1;
435 /* parse a PACK PES */
436 static inline int ps_pkt_parse_pack( block_t *p_pkt, int64_t *pi_scr,
437 int *pi_mux_rate )
439 uint8_t *p = p_pkt->p_buffer;
440 if( p_pkt->i_buffer >= 14 && (p[4] >> 6) == 0x01 )
442 *pi_scr = FROM_SCALE_NZ( ExtractPackHeaderTimestamp( &p[4] ) );
443 *pi_mux_rate = ( p[10] << 14 )|( p[11] << 6 )|( p[12] >> 2);
445 else if( p_pkt->i_buffer >= 12 && (p[4] >> 4) == 0x02 ) /* MPEG-1 Pack SCR, same bits as PES/PTS */
447 if(!ExtractPESTimestamp( &p[4], 0x02, pi_scr ))
448 return VLC_EGENERIC;
449 *pi_scr = FROM_SCALE_NZ( *pi_scr );
450 *pi_mux_rate = ( ( p[9]&0x7f )<< 15 )|( p[10] << 7 )|( p[11] >> 1);
452 else
454 return VLC_EGENERIC;
456 return VLC_SUCCESS;
459 /* Parse a SYSTEM PES */
460 static inline int ps_pkt_parse_system( block_t *p_pkt, ps_psm_t *p_psm,
461 ps_track_t tk[PS_TK_COUNT] )
463 uint8_t *p = &p_pkt->p_buffer[6 + 3 + 1 + 1 + 1];
465 /* System header is not useable if it references private streams (0xBD)
466 * or 'all audio streams' (0xB8) or 'all video streams' (0xB9) */
467 while( p < &p_pkt->p_buffer[p_pkt->i_buffer] && (p[0] & 0x80) )
469 int i_id = p[0];
470 switch( i_id )
472 case 0xB7:
473 if( &p_pkt->p_buffer[p_pkt->i_buffer] - p < 6 )
474 return VLC_EGENERIC;
475 i_id = ((int)PS_STREAM_ID_EXTENDED << 8) | (p[2] & 0x7F);
476 p += 6;
477 break;
478 default:
479 if( &p_pkt->p_buffer[p_pkt->i_buffer] - p < 3 )
480 return VLC_EGENERIC;
481 p += 3;
482 break;
485 if( i_id < 0xc0 )
486 continue;
488 int i_tk = PS_ID_TO_TK( i_id );
489 if( !tk[i_tk].b_configured )
490 ps_track_fill( &tk[i_tk], p_psm, i_id, NULL, false );
492 return VLC_SUCCESS;
495 /* Parse a PES (and skip i_skip_extra in the payload) */
496 static inline int ps_pkt_parse_pes( vlc_object_t *p_object, block_t *p_pes, int i_skip_extra )
498 unsigned int i_skip = 0;
499 mtime_t i_pts = -1;
500 mtime_t i_dts = -1;
501 uint8_t i_stream_id = 0;
502 bool b_pes_scrambling = false;
504 if( ParsePESHeader( p_object, p_pes->p_buffer, p_pes->i_buffer,
505 &i_skip, &i_dts, &i_pts, &i_stream_id, &b_pes_scrambling ) != VLC_SUCCESS )
506 return VLC_EGENERIC;
508 if( b_pes_scrambling )
509 p_pes->i_flags |= BLOCK_FLAG_SCRAMBLED;
511 if( i_skip_extra >= 0 )
512 i_skip += i_skip_extra;
513 else if( p_pes->i_buffer > i_skip + 3 &&
514 ( ps_pkt_id( p_pes ) == 0xa001 || ps_pkt_id( p_pes ) == 0xbda1 ) )
515 i_skip += 4 + p_pes->p_buffer[i_skip+3];
517 if( p_pes->i_buffer <= i_skip )
519 return VLC_EGENERIC;
522 p_pes->p_buffer += i_skip;
523 p_pes->i_buffer -= i_skip;
525 if( i_dts >= 0 )
526 p_pes->i_dts = FROM_SCALE( i_dts );
527 if( i_pts >= 0 )
528 p_pes->i_pts = FROM_SCALE( i_pts );
530 return VLC_SUCCESS;
533 typedef struct
535 /* Language is iso639-2T */
536 uint8_t lang[3];
537 } ps_descriptors_t;
539 /* Program stream map handling */
540 typedef struct ps_es_t
542 int i_type;
543 int i_id;
545 ps_descriptors_t desc;
547 } ps_es_t;
549 struct ps_psm_t
551 int i_version;
553 size_t i_es;
554 ps_es_t *es;
556 ps_descriptors_t uniqueextdesc;
559 static inline int ps_id_to_type( const ps_psm_t *p_psm, int i_id )
561 size_t i;
562 for( i = 0; p_psm && i < p_psm->i_es; i++ )
564 if( p_psm->es[i].i_id == i_id ) return p_psm->es[i].i_type;
566 return 0;
569 static inline const uint8_t *ps_id_to_lang( const ps_psm_t *p_psm, int i_id )
571 size_t i;
572 for( i = 0; p_psm && i < p_psm->i_es; i++ )
574 if( p_psm->es[i].i_id == i_id )
575 return p_psm->es[i].desc.lang;
577 return 0;
580 static inline void ps_psm_init( ps_psm_t *p_psm )
582 p_psm->i_version = 0xFFFF;
583 p_psm->i_es = 0;
584 p_psm->es = 0;
585 memset( &p_psm->uniqueextdesc, 0, 3 );
588 static inline void ps_psm_destroy( ps_psm_t *p_psm )
590 free( p_psm->es );
591 p_psm->es = NULL;
592 p_psm->i_es = 0;
595 static inline void ps_parse_descriptors( const uint8_t *p_data, size_t i_data,
596 ps_descriptors_t *p_desc )
598 while( i_data > 3 && i_data > 2u + p_data[1] )
600 switch( p_data[0] )
602 case 0x0A: /* ISO_639_language_descriptor */
603 if( i_data >= 6 )
604 memcpy( p_desc->lang, &p_data[2], 3 );
605 break;
607 default:
608 break;
610 p_data += 2 + p_data[1];
611 i_data -= 2 + p_data[1];
615 static inline int ps_psm_fill( ps_psm_t *p_psm, block_t *p_pkt,
616 ps_track_t tk[PS_TK_COUNT], es_out_t *out )
618 size_t i_buffer = p_pkt->i_buffer;
619 uint8_t *p_buffer = p_pkt->p_buffer;
620 size_t i_length, i_info_length, i_es_base;
621 int i_version;
622 bool b_single_extension;
624 if( !p_psm || p_buffer[3] != PS_STREAM_ID_MAP )
625 return VLC_EGENERIC;
627 i_length = GetWBE(&p_buffer[4]) + 6;
628 if( i_length > i_buffer ) return VLC_EGENERIC;
630 if((p_buffer[6] & 0x80) == 0) /* current_next_indicator */
631 return VLC_EGENERIC;
633 b_single_extension = p_buffer[6] & 0x40;
634 i_version = (p_buffer[6] & 0xf8);
636 if( p_psm->i_version == i_version ) return VLC_EGENERIC;
638 ps_psm_destroy( p_psm );
640 i_info_length = GetWBE(&p_buffer[8]);
641 if( i_info_length + 10 > i_length )
642 return VLC_EGENERIC;
644 /* Elementary stream map */
645 /* int i_esm_length = (uint16_t)(p_buffer[ 10 + i_info_length ] << 8) +
646 p_buffer[ 11 + i_info_length]; */
647 i_es_base = 12 + i_info_length;
649 while( i_es_base + 4 < i_length )
651 ps_es_t *tmp_es = realloc( p_psm->es, sizeof(ps_es_t) * (p_psm->i_es+1) );
652 if( tmp_es == NULL )
653 break;
654 p_psm->es = tmp_es;
656 ps_es_t *p_es = &p_psm->es[ p_psm->i_es++ ];
657 p_es->i_type = p_buffer[ i_es_base ];
658 p_es->i_id = p_buffer[ i_es_base + 1 ];
660 i_info_length = GetWBE(&p_buffer[ i_es_base + 2 ]);
662 if( i_es_base + 4 + i_info_length > i_length )
663 break;
665 /* TODO Add support for VC-1 stream:
666 * stream_type=0xea, stream_id=0xfd AND registration
667 * descriptor 0x5 with format_identifier == 0x56432D31 (VC-1)
668 * (I need a sample that use PSM with VC-1) */
670 if( p_es->i_id == PS_STREAM_ID_EXTENDED && b_single_extension == 0 )
672 if( i_info_length < 3 )
673 break;
674 p_es->i_id = (p_es->i_id << 8) | (p_buffer[i_es_base + 6] & 0x7F);
675 ps_parse_descriptors( &p_buffer[i_es_base + 4 + 3],
676 i_info_length - 3,
677 &p_psm->uniqueextdesc );
679 else
681 ps_parse_descriptors( &p_buffer[i_es_base + 4],
682 i_info_length, &p_es->desc );
685 i_es_base += 4 + i_info_length;
688 /* TODO: CRC */
690 p_psm->i_version = i_version;
692 /* Check/Modify our existing tracks */
693 for( int i = 0; i < PS_TK_COUNT; i++ )
695 ps_track_t tk_tmp;
697 if( !tk[i].b_configured || !tk[i].es ) continue;
699 if( ps_track_fill( &tk_tmp, p_psm, tk[i].i_id, p_pkt, false ) != VLC_SUCCESS )
700 continue;
702 if( tk_tmp.fmt.i_codec == tk[i].fmt.i_codec )
704 es_format_Clean( &tk_tmp.fmt );
705 continue;
708 es_out_Del( out, tk[i].es );
709 es_format_Clean( &tk[i].fmt );
711 tk_tmp.b_configured = true;
712 tk[i] = tk_tmp;
713 tk[i].es = es_out_Add( out, &tk[i].fmt );
716 return VLC_SUCCESS;