]> git.sesse.net Git - vlc/blob - modules/demux/ps.h
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / demux / ps.h
1 /*****************************************************************************
2  * ps.h: Program Stream demuxer helper
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
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 General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <vlc_demux.h>
25 #include <assert.h>
26
27 /* 256-0xC0 for normal stream, 256 for 0xbd stream, 256 for 0xfd stream */
28 #define PS_TK_COUNT (768 - 0xc0)
29 #define PS_ID_TO_TK( id ) ((id) <= 0xff ? (id) - 0xc0 : \
30             ((id)&0xff) + (((id)&0xff00) == 0xbd00 ? 256-0xC0 : 512-0xc0) )
31
32 typedef struct ps_psm_t ps_psm_t;
33 static inline int ps_id_to_type( const ps_psm_t *, int );
34 static inline const uint8_t *ps_id_to_lang( const ps_psm_t *, int );
35
36 typedef struct
37 {
38     bool  b_seen;
39     int         i_skip;
40     int         i_id;
41     es_out_id_t *es;
42     es_format_t fmt;
43     mtime_t     i_first_pts;
44     mtime_t     i_last_pts;
45
46 } ps_track_t;
47
48 /* Init a set of track */
49 static inline void ps_track_init( ps_track_t tk[PS_TK_COUNT] )
50 {
51     int i;
52     for( i = 0; i < PS_TK_COUNT; i++ )
53     {
54         tk[i].b_seen = false;
55         tk[i].i_skip = 0;
56         tk[i].i_id   = 0;
57         tk[i].es     = NULL;
58         tk[i].i_first_pts = -1;
59         tk[i].i_last_pts = -1;
60         es_format_Init( &tk[i].fmt, UNKNOWN_ES, 0 );
61     }
62 }
63
64 /* From id fill i_skip and es_format_t */
65 static inline int ps_track_fill( ps_track_t *tk, ps_psm_t *p_psm, int i_id )
66 {
67     tk->i_skip = 0;
68     tk->i_id = i_id;
69     if( ( i_id&0xff00 ) == 0xbd00 )
70     {
71         if( ( i_id&0xf8 ) == 0x88 || (i_id&0xf8) == 0x98 )
72         {
73             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('d','t','s',' ') );
74             tk->i_skip = 4;
75         }
76         else if( ( i_id&0xf0 ) == 0x80
77                ||  (i_id&0xf0) == 0xc0 ) /* AC-3, Can also be used for DD+/E-AC-3 */
78         {
79             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('a','5','2',' ') );
80             tk->i_skip = 4;
81         }
82         else if( (i_id&0xf0) == 0xb0 )
83         {
84             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('m','l','p',' ') );
85             /* FIXME / untested ... no known decoder (at least not in VLC/ffmpeg) */
86         }
87         else if( ( i_id&0xe0 ) == 0x20 )
88         {
89             es_format_Init( &tk->fmt, SPU_ES, VLC_FOURCC('s','p','u',' ') );
90             tk->i_skip = 1;
91         }
92         else if( ( i_id&0xf0 ) == 0xa0 )
93         {
94             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('l','p','c','m') );
95             tk->i_skip = 1;
96         }
97         else if( ( i_id&0xff ) == 0x70 )
98         {
99             es_format_Init( &tk->fmt, SPU_ES, VLC_FOURCC('o','g','t',' ') );
100         }
101         else if( ( i_id&0xfc ) == 0x00 )
102         {
103             es_format_Init( &tk->fmt, SPU_ES, VLC_FOURCC('c','v','d',' ') );
104         }
105         else if( ( i_id&0xff ) == 0x10 )
106         {
107             es_format_Init( &tk->fmt, SPU_ES, VLC_FOURCC('t','e','l','x') );
108         }
109         else
110         {
111             es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
112             return VLC_EGENERIC;
113         }
114     }
115     else if( (i_id&0xff00) == 0xfd00 )
116     {
117         uint8_t i_sub_id = i_id & 0xff;
118         if( i_sub_id >= 0x55 && i_sub_id <= 0x5f )
119         {
120             es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('W','V','C','1') );
121         }
122         else
123         {
124             es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
125             return VLC_EGENERIC;
126         }
127     }
128     else
129     {
130         int i_type = ps_id_to_type( p_psm , i_id );
131
132         es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
133
134         if( (i_id&0xf0) == 0xe0 && i_type == 0x1b )
135         {
136             es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('h','2','6','4') );
137         }
138         else if( (i_id&0xf0) == 0xe0 && i_type == 0x10 )
139         {
140             es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('m','p','4','v') );
141         }
142         else if( (i_id&0xf0) == 0xe0 && i_type == 0x02 )
143         {
144             es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
145         }
146         else if( ( i_id&0xe0 ) == 0xc0 && i_type == 0x0f )
147         {
148             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('m','p','4','a') );
149         }
150         else if( ( i_id&0xe0 ) == 0xc0 && i_type == 0x11 )
151         {
152             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('m','p','4','a') );
153         }
154         else if( ( i_id&0xe0 ) == 0xc0 && i_type == 0x03 )
155         {
156             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('m','p','g','a') );
157         }
158
159         if( tk->fmt.i_cat == UNKNOWN_ES && ( i_id&0xf0 ) == 0xe0 )
160         {
161             es_format_Init( &tk->fmt, VIDEO_ES, VLC_FOURCC('m','p','g','v') );
162         }
163         else if( tk->fmt.i_cat == UNKNOWN_ES && ( i_id&0xe0 ) == 0xc0 )
164         {
165             es_format_Init( &tk->fmt, AUDIO_ES, VLC_FOURCC('m','p','g','a') );
166         }
167         else if( tk->fmt.i_cat == UNKNOWN_ES ) return VLC_EGENERIC;
168     }
169
170     /* PES packets usually contain truncated frames */
171     tk->fmt.b_packetized = false;
172
173     if( ps_id_to_lang( p_psm, i_id ) )
174     {
175         tk->fmt.psz_language = malloc( 4 );
176         if( tk->fmt.psz_language )
177         {
178             memcpy( tk->fmt.psz_language, ps_id_to_lang( p_psm , i_id ), 3 );
179             tk->fmt.psz_language[3] = 0;
180         }
181     }
182
183     return VLC_SUCCESS;
184 }
185
186 /* return the id of a PES (should be valid) */
187 static inline int ps_pkt_id( block_t *p_pkt )
188 {
189     if( p_pkt->p_buffer[3] == 0xbd &&
190         p_pkt->i_buffer >= 9 &&
191         p_pkt->i_buffer >= 9 + (size_t)p_pkt->p_buffer[8] )
192     {
193         /* VOB extension */
194         return 0xbd00 | p_pkt->p_buffer[9+p_pkt->p_buffer[8]];
195     }
196     else if( p_pkt->p_buffer[3] == 0xfd &&
197              p_pkt->i_buffer >= 9 &&
198              (p_pkt->p_buffer[6]&0xC0) == 0x80 &&   /* mpeg2 */
199              (p_pkt->p_buffer[7]&0x01) == 0x01 )    /* extension_flag */
200     {
201         /* ISO 13818 amendment 2 and SMPTE RP 227 */
202         const uint8_t i_flags = p_pkt->p_buffer[7];
203         unsigned int i_skip = 9;
204
205         /* Find PES extension */
206         if( (i_flags & 0x80 ) )
207         {
208             i_skip += 5;        /* pts */
209             if( (i_flags & 0x40) )
210                 i_skip += 5;    /* dts */
211         }
212         if( (i_flags & 0x20 ) )
213             i_skip += 6;
214         if( (i_flags & 0x10 ) )
215             i_skip += 3;
216         if( (i_flags & 0x08 ) )
217             i_skip += 1;
218         if( (i_flags & 0x04 ) )
219             i_skip += 1;
220         if( (i_flags & 0x02 ) )
221             i_skip += 2;
222
223         if( i_skip < p_pkt->i_buffer && (p_pkt->p_buffer[i_skip]&0x01) )
224         {
225             const uint8_t i_flags2 = p_pkt->p_buffer[i_skip];
226
227             /* Find PES extension 2 */
228             i_skip += 1;
229             if( i_flags2 & 0x80 )
230                 i_skip += 16;
231             if( (i_flags2 & 0x40) && i_skip < p_pkt->i_buffer )
232                 i_skip += 1 + p_pkt->p_buffer[i_skip];
233             if( i_flags2 & 0x20 )
234                 i_skip += 2;
235             if( i_flags2 & 0x10 )
236                 i_skip += 2;
237
238             if( i_skip + 1 < p_pkt->i_buffer )
239             {
240                 const int i_extension_field_length = p_pkt->p_buffer[i_skip]&0x7f;
241                 if( i_extension_field_length >=1 )
242                 {
243                     int i_stream_id_extension_flag = (p_pkt->p_buffer[i_skip+1] >> 7)&0x1;
244                     if( i_stream_id_extension_flag == 0 )
245                         return 0xfd00 | (p_pkt->p_buffer[i_skip+1]&0x7f);
246                 }
247             }
248         }
249     }
250     return p_pkt->p_buffer[3];
251 }
252
253 /* return the size of the next packet
254  * You need to give him at least 14 bytes (and it need to start as a
255  * valid packet) It does not handle less than 6 bytes */
256 static inline int ps_pkt_size( const uint8_t *p, int i_peek )
257 {
258     assert( i_peek >= 6 );
259     if( p[3] == 0xb9 && i_peek >= 4 )
260     {
261         return 4;
262     }
263     else if( p[3] == 0xba )
264     {
265         if( (p[4] >> 6) == 0x01 && i_peek >= 14 )
266         {
267             return 14 + (p[13]&0x07);
268         }
269         else if( (p[4] >> 4) == 0x02 && i_peek >= 12 )
270         {
271             return 12;
272         }
273         return -1;
274     }
275     else if( i_peek >= 6 )
276     {
277         return 6 + ((p[4]<<8) | p[5] );
278     }
279     return -1;
280 }
281
282 /* parse a PACK PES */
283 static inline int ps_pkt_parse_pack( block_t *p_pkt, int64_t *pi_scr,
284                                      int *pi_mux_rate )
285 {
286     uint8_t *p = p_pkt->p_buffer;
287     if( p_pkt->i_buffer >= 14 && (p[4] >> 6) == 0x01 )
288     {
289         *pi_scr =((((int64_t)p[4]&0x38) << 27 )|
290                   (((int64_t)p[4]&0x03) << 28 )|
291                    ((int64_t)p[5] << 20 )|
292                   (((int64_t)p[6]&0xf8) << 12 )|
293                   (((int64_t)p[6]&0x03) << 13 )|
294                    ((int64_t)p[7] << 5 )|
295                    ((int64_t)p[8] >> 3 )) * 100 / 9;
296
297         *pi_mux_rate = ( p[10] << 14 )|( p[11] << 6 )|( p[12] >> 2);
298     }
299     else if( p_pkt->i_buffer >= 12 && (p[4] >> 4) == 0x02 )
300     {
301         *pi_scr =((((int64_t)p[4]&0x0e) << 29 )|
302                    ((int64_t)p[5] << 22 )|
303                   (((int64_t)p[6]&0xfe) << 14 )|
304                    ((int64_t)p[7] <<  7 )|
305                    ((int64_t)p[8] >> 1 )) * 100 / 9;
306
307         *pi_mux_rate = ( ( p[9]&0x7f )<< 15 )|( p[10] << 7 )|( p[11] >> 1);
308     }
309     else
310     {
311         return VLC_EGENERIC;
312     }
313     return VLC_SUCCESS;
314 }
315
316 /* Parse a SYSTEM PES */
317 static inline int ps_pkt_parse_system( block_t *p_pkt, ps_psm_t *p_psm,
318                                        ps_track_t tk[PS_TK_COUNT] )
319 {
320     uint8_t *p = &p_pkt->p_buffer[6 + 3 + 1 + 1 + 1];
321
322     /* System header is not useable if it references private streams (0xBD)
323      * or 'all audio streams' (0xB8) or 'all video streams' (0xB9) */
324     while( p < &p_pkt->p_buffer[p_pkt->i_buffer] )
325     {
326         int i_id = p[0];
327
328         /* fprintf( stderr, "   SYSTEM_START_CODEEE: id=0x%x\n", p[0] ); */
329         if( p[0] >= 0xBC || p[0] == 0xB8 || p[0] == 0xB9 ) p += 2;
330         p++;
331
332         if( i_id >= 0xc0 )
333         {
334             int i_tk = PS_ID_TO_TK( i_id );
335
336             if( !tk[i_tk].b_seen )
337             {
338                 if( !ps_track_fill( &tk[i_tk], p_psm, i_id ) )
339                 {
340                     tk[i_tk].b_seen = true;
341                 }
342             }
343         }
344     }
345     return VLC_SUCCESS;
346 }
347
348 /* Parse a PES (and skip i_skip_extra in the payload) */
349 static inline int ps_pkt_parse_pes( block_t *p_pes, int i_skip_extra )
350 {
351     uint8_t header[34];
352     unsigned int i_skip  = 0;
353
354     memcpy( header, p_pes->p_buffer, __MIN( p_pes->i_buffer, 34 ) );
355
356     switch( header[3] )
357     {
358         case 0xBC:  /* Program stream map */
359         case 0xBE:  /* Padding */
360         case 0xBF:  /* Private stream 2 */
361         case 0xB0:  /* ECM */
362         case 0xB1:  /* EMM */
363         case 0xFF:  /* Program stream directory */
364         case 0xF2:  /* DSMCC stream */
365         case 0xF8:  /* ITU-T H.222.1 type E stream */
366             i_skip = 6;
367             break;
368
369         default:
370             if( ( header[6]&0xC0 ) == 0x80 )
371             {
372                 /* mpeg2 PES */
373                 i_skip = header[8] + 9;
374
375                 if( header[7]&0x80 )    /* has pts */
376                 {
377                     p_pes->i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
378                                     (mtime_t)(header[10] << 22)|
379                                    ((mtime_t)(header[11]&0xfe) << 14)|
380                                     (mtime_t)(header[12] << 7)|
381                                     (mtime_t)(header[13] >> 1);
382
383                     if( header[7]&0x40 )    /* has dts */
384                     {
385                          p_pes->i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
386                                          (mtime_t)(header[15] << 22)|
387                                         ((mtime_t)(header[16]&0xfe) << 14)|
388                                          (mtime_t)(header[17] << 7)|
389                                          (mtime_t)(header[18] >> 1);
390                     }
391                 }
392             }
393             else
394             {
395                 i_skip = 6;
396                 while( i_skip < 23 && header[i_skip] == 0xff )
397                 {
398                     i_skip++;
399                 }
400                 if( i_skip == 23 )
401                 {
402                     /* msg_Err( p_demux, "too much MPEG-1 stuffing" ); */
403                     return VLC_EGENERIC;
404                 }
405                 if( ( header[i_skip] & 0xC0 ) == 0x40 )
406                 {
407                     i_skip += 2;
408                 }
409
410                 if(  header[i_skip]&0x20 )
411                 {
412                      p_pes->i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
413                                      (mtime_t)(header[i_skip+1] << 22)|
414                                     ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
415                                      (mtime_t)(header[i_skip+3] << 7)|
416                                      (mtime_t)(header[i_skip+4] >> 1);
417
418                     if( header[i_skip]&0x10 )    /* has dts */
419                     {
420                          p_pes->i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
421                                          (mtime_t)(header[i_skip+6] << 22)|
422                                         ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
423                                          (mtime_t)(header[i_skip+8] << 7)|
424                                          (mtime_t)(header[i_skip+9] >> 1);
425                          i_skip += 10;
426                     }
427                     else
428                     {
429                         i_skip += 5;
430                     }
431                 }
432                 else
433                 {
434                     i_skip += 1;
435                 }
436             }
437     }
438
439     i_skip += i_skip_extra;
440
441     if( p_pes->i_buffer <= i_skip )
442     {
443         return VLC_EGENERIC;
444     }
445
446     p_pes->p_buffer += i_skip;
447     p_pes->i_buffer -= i_skip;
448
449     p_pes->i_dts = 100 * p_pes->i_dts / 9;
450     p_pes->i_pts = 100 * p_pes->i_pts / 9;
451
452     return VLC_SUCCESS;
453 }
454
455 /* Program stream map handling */
456 typedef struct ps_es_t
457 {
458     int i_type;
459     int i_id;
460
461     int i_descriptor;
462     uint8_t *p_descriptor;
463
464     /* Language is iso639-2T */
465     uint8_t lang[3];
466
467 } ps_es_t;
468
469 struct ps_psm_t
470 {
471     int i_version;
472
473     int     i_es;
474     ps_es_t **es;
475 };
476
477 static inline int ps_id_to_type( const ps_psm_t *p_psm, int i_id )
478 {
479     int i;
480     for( i = 0; p_psm && i < p_psm->i_es; i++ )
481     {
482         if( p_psm->es[i]->i_id == i_id ) return p_psm->es[i]->i_type;
483     }
484     return 0;
485 }
486
487 static inline const uint8_t *ps_id_to_lang( const ps_psm_t *p_psm, int i_id )
488 {
489     int i;
490     for( i = 0; p_psm && i < p_psm->i_es; i++ )
491     {
492         if( p_psm->es[i]->i_id == i_id ) return p_psm->es[i]->lang;
493     }
494     return 0;
495 }
496
497 static inline void ps_psm_init( ps_psm_t *p_psm )
498 {
499     p_psm->i_version = 0xFFFF;
500     p_psm->i_es = 0;
501     p_psm->es = 0;
502 }
503
504 static inline void ps_psm_destroy( ps_psm_t *p_psm )
505 {
506     while( p_psm->i_es-- )
507     {
508         free( p_psm->es[p_psm->i_es]->p_descriptor );
509         free( p_psm->es[p_psm->i_es] );
510     }
511     free( p_psm->es );
512
513     p_psm->es = 0;
514     p_psm->i_es = 0;
515 }
516
517 static inline int ps_psm_fill( ps_psm_t *p_psm, block_t *p_pkt,
518                                ps_track_t tk[PS_TK_COUNT], es_out_t *out )
519 {
520     int i_buffer = p_pkt->i_buffer;
521     uint8_t *p_buffer = p_pkt->p_buffer;
522     int i_length, i_version, i_info_length, i_esm_length, i_es_base;
523
524     if( !p_psm || p_buffer[3] != 0xbc ) return VLC_EGENERIC;
525
526     i_length = (uint16_t)(p_buffer[4] << 8) + p_buffer[5] + 6;
527     if( i_length > i_buffer ) return VLC_EGENERIC;
528
529     //i_current_next_indicator = (p_buffer[6] && 0x01);
530     i_version = (p_buffer[6] && 0xf8);
531
532     if( p_psm->i_version == i_version ) return VLC_EGENERIC;
533
534     ps_psm_destroy( p_psm );
535
536     i_info_length = (uint16_t)(p_buffer[8] << 8) + p_buffer[9];
537     if( i_info_length + 10 > i_length ) return VLC_EGENERIC;
538
539     /* Elementary stream map */
540     i_esm_length = (uint16_t)(p_buffer[ 10 + i_info_length ] << 8) +
541         p_buffer[ 11 + i_info_length];
542     i_es_base = 12 + i_info_length;
543
544     while( i_es_base + 4 < i_length )
545     {
546         ps_es_t **tmp_es;
547         ps_es_t es;
548         es.lang[0] = es.lang[1] = es.lang[2] = 0;
549
550         es.i_type = p_buffer[ i_es_base  ];
551         es.i_id = p_buffer[ i_es_base + 1 ];
552         i_info_length = (uint16_t)(p_buffer[ i_es_base + 2 ] << 8) +
553             p_buffer[ i_es_base + 3 ];
554
555         if( i_es_base + 4 + i_info_length > i_length ) break;
556
557         /* TODO Add support for VC-1 stream:
558          *      stream_type=0xea, stream_id=0xfd AND registration
559          *      descriptor 0x5 with format_identifier == 0x56432D31 (VC-1)
560          *      (I need a sample that use PSM with VC-1) */
561
562         es.p_descriptor = 0;
563         es.i_descriptor = i_info_length;
564         if( i_info_length > 0 )
565         {
566             int i = 0;
567
568             es.p_descriptor = malloc( i_info_length );
569             if( es.p_descriptor )
570             {
571                 memcpy( es.p_descriptor, p_buffer + i_es_base + 4, i_info_length);
572
573                 while( i <= es.i_descriptor - 2 )
574                 {
575                     /* Look for the ISO639 language descriptor */
576                     if( es.p_descriptor[i] != 0x0a )
577                     {
578                         i += es.p_descriptor[i+1] + 2;
579                         continue;
580                     }
581
582                     if( i <= es.i_descriptor - 6 )
583                     {
584                         es.lang[0] = es.p_descriptor[i+2];
585                         es.lang[1] = es.p_descriptor[i+3];
586                         es.lang[2] = es.p_descriptor[i+4];
587                     }
588                     break;
589                 }
590             }
591         }
592
593         tmp_es = realloc( p_psm->es, sizeof(ps_es_t *) * (p_psm->i_es+1) );
594         if( tmp_es )
595         {
596             p_psm->es = tmp_es;
597             p_psm->es[p_psm->i_es] = malloc( sizeof(ps_es_t) );
598             if( p_psm->es[p_psm->i_es] )
599             {
600                 *p_psm->es[p_psm->i_es++] = es;
601                 i_es_base += 4 + i_info_length;
602             }
603         }
604     }
605
606     /* TODO: CRC */
607
608     p_psm->i_version = i_version;
609
610     /* Check/Modify our existing tracks */
611     for( int i = 0; i < PS_TK_COUNT; i++ )
612     {
613         ps_track_t tk_tmp;
614
615         if( !tk[i].b_seen || !tk[i].es ) continue;
616
617         if( ps_track_fill( &tk_tmp, p_psm, tk[i].i_id ) != VLC_SUCCESS )
618             continue;
619
620         if( tk_tmp.fmt.i_codec == tk[i].fmt.i_codec ) continue;
621
622         es_out_Del( out, tk[i].es );
623         tk[i] = tk_tmp;
624         tk[i].b_seen = true;
625         tk[i].es = es_out_Add( out, &tk[i].fmt );
626     }
627
628     return VLC_SUCCESS;
629 }