1 /*****************************************************************************
2 * ps.h: Program Stream demuxer helper
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
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 *****************************************************************************/
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)
41 #define PS_ID_TO_TK( id ) ((id) <= 0xff ? (id) - 0xc0 : \
42 ((id)&0xff) + (((id)&0xff00) == 0xbd00 ? 256-0xC0 : 512-0xc0) )
44 static inline int ps_id_to_tk( unsigned i_id
)
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);
53 return 768-0xc0 + (i_id
& 0x07);
55 #define PS_ID_TO_TK( id ) ps_id_to_tk( id )
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 );
68 int i_next_block_flags
;
76 /* Init a set of track */
77 static inline void ps_track_init( ps_track_t tk
[PS_TK_COUNT
] )
80 for( i
= 0; i
< PS_TK_COUNT
; i
++ )
82 tk
[i
].b_configured
= false;
86 tk
[i
].i_next_block_flags
= 0;
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 };
100 (!memcmp( p_data
, startcode
, 3 ) &&
101 !memcmp( &p_data
[1], startcode
, 3 )) )
104 /* Shitty H264 probing. We need a centralized way do to this */
107 if( !memcmp( p_data
, startcode
, 3 ) )
109 if(p_data
[3] == 0x67)
111 else if(p_data
[3] == 0x68)
113 else if( p_data
[3] & 0x80 )
115 else if( (p_data
[3] & 0x1F) > 23 || (p_data
[3] & 0x1F) < 1 )
117 else if( (p_data
[3] & 0x1F) < 6 )
118 return (i_flags
== 0x03);
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
)
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
);
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 */
146 if( ( i_id
&0xf0 ) == 0xc0 )
148 if( p_pkt
== NULL
|| p_pkt
->i_buffer
< 9 )
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;
165 es_format_Change( &tk
->fmt
, AUDIO_ES
, b_eac3
? VLC_CODEC_EAC3
: VLC_CODEC_A52
);
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
);
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
);
190 else if( ( i_id
&0xf0 ) == 0xb0 ) /* 0xb0 -> 0xbf */
192 es_format_Change( &tk
->fmt
, AUDIO_ES
, VLC_CODEC_TRUEHD
);
197 es_format_Change( &tk
->fmt
, UNKNOWN_ES
, 0 );
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
);
211 es_format_Change( &tk
->fmt
, UNKNOWN_ES
, 0 );
215 else if( (i_id
&0xff00) == 0xa000 ) /* 0xA000 -> 0xA0FF */
217 uint8_t i_sub_id
= i_id
& 0x07;
220 es_format_Change( &tk
->fmt
, AUDIO_ES
, VLC_CODEC_DVDA_LPCM
);
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 */
230 es_format_Change( &tk
->fmt
, UNKNOWN_ES
, 0 );
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 */
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 ||
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);
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 ) )
365 if( (i_flags
& 0x10 ) )
367 if( (i_flags
& 0x08 ) )
369 if( (i_flags
& 0x04 ) )
371 if( (i_flags
& 0x02 ) )
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 */
380 if( i_flags2
& 0x80 )
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 )
386 if( i_flags2
& 0x10 )
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) )
412 case PS_STREAM_ID_END_STREAM
:
415 case PS_STREAM_ID_PACK_HEADER
:
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 )
425 case PS_STREAM_ID_SYSTEM_HEADER
:
426 case PS_STREAM_ID_MAP
:
427 case PS_STREAM_ID_DIRECTORY
:
430 return 6 + ((p
[4]<<8) | p
[5] );
435 /* parse a PACK PES */
436 static inline int ps_pkt_parse_pack( block_t
*p_pkt
, int64_t *pi_scr
,
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
))
449 *pi_scr
= FROM_SCALE_NZ( *pi_scr
);
450 *pi_mux_rate
= ( ( p
[9]&0x7f )<< 15 )|( p
[10] << 7 )|( p
[11] >> 1);
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) )
473 if( &p_pkt
->p_buffer
[p_pkt
->i_buffer
] - p
< 6 )
475 i_id
= ((int)PS_STREAM_ID_EXTENDED
<< 8) | (p
[2] & 0x7F);
479 if( &p_pkt
->p_buffer
[p_pkt
->i_buffer
] - p
< 3 )
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 );
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;
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
)
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
)
522 p_pes
->p_buffer
+= i_skip
;
523 p_pes
->i_buffer
-= i_skip
;
526 p_pes
->i_dts
= FROM_SCALE( i_dts
);
528 p_pes
->i_pts
= FROM_SCALE( i_pts
);
535 /* Language is iso639-2T */
539 /* Program stream map handling */
540 typedef struct ps_es_t
545 ps_descriptors_t desc
;
556 ps_descriptors_t uniqueextdesc
;
559 static inline int ps_id_to_type( const ps_psm_t
*p_psm
, int i_id
)
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
;
569 static inline const uint8_t *ps_id_to_lang( const ps_psm_t
*p_psm
, int i_id
)
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
;
580 static inline void ps_psm_init( ps_psm_t
*p_psm
)
582 p_psm
->i_version
= 0xFFFF;
585 memset( &p_psm
->uniqueextdesc
, 0, 3 );
588 static inline void ps_psm_destroy( ps_psm_t
*p_psm
)
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] )
602 case 0x0A: /* ISO_639_language_descriptor */
604 memcpy( p_desc
->lang
, &p_data
[2], 3 );
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
;
622 bool b_single_extension
;
624 if( !p_psm
|| p_buffer
[3] != PS_STREAM_ID_MAP
)
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 */
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
)
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) );
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
)
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 )
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],
677 &p_psm
->uniqueextdesc
);
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
;
690 p_psm
->i_version
= i_version
;
692 /* Check/Modify our existing tracks */
693 for( int i
= 0; i
< PS_TK_COUNT
; i
++ )
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
)
702 if( tk_tmp
.fmt
.i_codec
== tk
[i
].fmt
.i_codec
)
704 es_format_Clean( &tk_tmp
.fmt
);
708 es_out_Del( out
, tk
[i
].es
);
709 es_format_Clean( &tk
[i
].fmt
);
711 tk_tmp
.b_configured
= true;
713 tk
[i
].es
= es_out_Add( out
, &tk
[i
].fmt
);