]> git.sesse.net Git - vlc/blob - modules/demux/ps.c
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / demux / ps.c
1 /*****************************************************************************
2  * ps.c: Program Stream demux module for VLC.
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 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35
36 #include "ps.h"
37
38 /* TODO:
39  *  - re-add pre-scanning.
40  *  - ...
41  */
42
43 #define TIME_TEXT N_("Trust MPEG timestamps")
44 #define TIME_LONGTEXT N_("Normally we use the timestamps of the MPEG files " \
45     "to calculate position and duration. However sometimes this might not " \
46     "be usable. Disable this option to calculate from the bitrate instead." )
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  OpenForce( vlc_object_t * );
52 static int  Open   ( vlc_object_t * );
53 static void Close  ( vlc_object_t * );
54
55 vlc_module_begin ()
56     set_description( N_("MPEG-PS demuxer") )
57     set_shortname( N_("PS") )
58     set_category( CAT_INPUT )
59     set_subcategory( SUBCAT_INPUT_DEMUX )
60     set_capability( "demux", 1 )
61     set_callbacks( OpenForce, Close )
62     add_shortcut( "ps" )
63
64     add_bool( "ps-trust-timestamps", true, NULL, TIME_TEXT,
65                  TIME_LONGTEXT, true )
66
67     add_submodule ()
68     set_description( N_("MPEG-PS demuxer") )
69     set_capability( "demux", 8 )
70     set_callbacks( Open, Close )
71     add_shortcut( "ps" )
72 vlc_module_end ()
73
74 /*****************************************************************************
75  * Local prototypes
76  *****************************************************************************/
77
78 struct demux_sys_t
79 {
80     ps_psm_t    psm;
81     ps_track_t  tk[PS_TK_COUNT];
82
83     int64_t     i_scr;
84     int         i_mux_rate;
85     int64_t     i_length;
86     int         i_time_track;
87     int64_t     i_current_pts;
88
89     bool  b_lost_sync;
90     bool  b_have_pack;
91     bool  b_seekable;
92 };
93
94 static int Demux  ( demux_t *p_demux );
95 static int Control( demux_t *p_demux, int i_query, va_list args );
96
97 static int      ps_pkt_resynch( stream_t *, uint32_t *pi_code );
98 static block_t *ps_pkt_read   ( stream_t *, uint32_t i_code );
99
100 /*****************************************************************************
101  * Open
102  *****************************************************************************/
103 static int OpenCommon( vlc_object_t *p_this, bool b_force )
104 {
105     demux_t     *p_demux = (demux_t*)p_this;
106     demux_sys_t *p_sys;
107
108     const uint8_t *p_peek;
109
110     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
111     {
112         msg_Err( p_demux, "cannot peek" );
113         return VLC_EGENERIC;
114     }
115
116     if( memcmp( p_peek, "\x00\x00\x01", 3 ) || ( p_peek[3] < 0xb9 ) )
117     {
118         if( !b_force )
119             return VLC_EGENERIC;
120
121         msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
122                   "continuing anyway" );
123     }
124
125     /* Fill p_demux field */
126     p_demux->pf_demux = Demux;
127     p_demux->pf_control = Control;
128     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
129     if( !p_sys ) return VLC_ENOMEM;
130
131     /* Init p_sys */
132     p_sys->i_mux_rate = 0;
133     p_sys->i_scr      = -1;
134     p_sys->i_length   = -1;
135     p_sys->i_current_pts = (mtime_t) 0;
136     p_sys->i_time_track = -1;
137
138     p_sys->b_lost_sync = false;
139     p_sys->b_have_pack = false;
140     p_sys->b_seekable  = false;
141
142     stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
143
144     ps_psm_init( &p_sys->psm );
145     ps_track_init( p_sys->tk );
146
147     /* TODO prescanning of ES */
148
149     return VLC_SUCCESS;
150 }
151
152 static int OpenForce( vlc_object_t *p_this )
153 {
154     return OpenCommon( p_this, true );
155 }
156
157 static int Open( vlc_object_t *p_this )
158 {
159     return OpenCommon( p_this, ((demux_t *)p_this)->b_force );
160 }
161
162 /*****************************************************************************
163  * Close
164  *****************************************************************************/
165 static void Close( vlc_object_t *p_this )
166 {
167     demux_t     *p_demux = (demux_t*)p_this;
168     demux_sys_t *p_sys = p_demux->p_sys;
169     int i;
170
171     for( i = 0; i < PS_TK_COUNT; i++ )
172     {
173         ps_track_t *tk = &p_sys->tk[i];
174         if( tk->b_seen )
175         {
176             es_format_Clean( &tk->fmt );
177             if( tk->es ) es_out_Del( p_demux->out, tk->es );
178         }
179     }
180
181     ps_psm_destroy( &p_sys->psm );
182
183     free( p_sys );
184 }
185
186 static int Demux2( demux_t *p_demux, bool b_end )
187 {
188     demux_sys_t *p_sys = p_demux->p_sys;
189     int i_ret, i_id;
190     uint32_t i_code;
191     block_t *p_pkt;
192
193     i_ret = ps_pkt_resynch( p_demux->s, &i_code );
194     if( i_ret < 0 )
195     {
196         return 0;
197     }
198     else if( i_ret == 0 )
199     {
200         if( !p_sys->b_lost_sync )
201             msg_Warn( p_demux, "garbage at input, trying to resync..." );
202
203         p_sys->b_lost_sync = true;
204         return 1;
205     }
206
207     if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
208     p_sys->b_lost_sync = false;
209
210     if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
211     {
212         return 0;
213     }
214     if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
215     {
216         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
217         if( !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
218         {
219             if( b_end && p_pkt->i_pts > tk->i_last_pts )
220             {
221                 tk->i_last_pts = p_pkt->i_pts;
222             }
223             else if ( tk->i_first_pts == -1 )
224             {
225                 tk->i_first_pts = p_pkt->i_pts;
226             }
227         }
228     }
229     block_Release( p_pkt );
230     return 1;
231 }
232
233 static void FindLength( demux_t *p_demux )
234 {
235     demux_sys_t *p_sys = p_demux->p_sys;
236     int64_t i_current_pos = -1, i_size = 0, i_end = 0;
237     int i;
238
239     if( !var_CreateGetInteger( p_demux, "ps-trust-timestamps" ) )
240         return;
241
242     if( p_sys->i_length == -1 ) /* First time */
243     {
244         p_sys->i_length = 0;
245         /* Check beginning */
246         i = 0;
247         i_current_pos = stream_Tell( p_demux->s );
248         while( vlc_object_alive (p_demux) && i < 40 && Demux2( p_demux, false ) > 0 ) i++;
249
250         /* Check end */
251         i_size = stream_Size( p_demux->s );
252         i_end = __MAX( 0, __MIN( 200000, i_size ) );
253         stream_Seek( p_demux->s, i_size - i_end );
254         i = 0;
255
256         while( vlc_object_alive (p_demux) && i < 40 && Demux2( p_demux, true ) > 0 );
257         if( i_current_pos >= 0 ) stream_Seek( p_demux->s, i_current_pos );
258     }
259
260     for( i = 0; i < PS_TK_COUNT; i++ )
261     {
262         ps_track_t *tk = &p_sys->tk[i];
263         if( tk->i_first_pts >= 0 && tk->i_last_pts > 0 )
264             if( tk->i_last_pts > tk->i_first_pts )
265             {
266                 int64_t i_length = (int64_t)tk->i_last_pts - tk->i_first_pts;
267                 if( i_length > p_sys->i_length )
268                 {
269                     p_sys->i_length = i_length;
270                     p_sys->i_time_track = i;
271                     msg_Dbg( p_demux, "we found a length of: %"PRId64, p_sys->i_length );
272                 }
273             }
274     }
275 }
276
277 /*****************************************************************************
278  * Demux:
279  *****************************************************************************/
280 static int Demux( demux_t *p_demux )
281 {
282     demux_sys_t *p_sys = p_demux->p_sys;
283     int i_ret, i_id, i_mux_rate;
284     uint32_t i_code;
285     block_t *p_pkt;
286
287     i_ret = ps_pkt_resynch( p_demux->s, &i_code );
288     if( i_ret < 0 )
289     {
290         return 0;
291     }
292     else if( i_ret == 0 )
293     {
294         if( !p_sys->b_lost_sync )
295             msg_Warn( p_demux, "garbage at input, trying to resync..." );
296
297         p_sys->b_lost_sync = true;
298         return 1;
299     }
300
301     if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
302     p_sys->b_lost_sync = false;
303
304     if( p_sys->i_length < 0 && p_sys->b_seekable )
305         FindLength( p_demux );
306
307     if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
308     {
309         return 0;
310     }
311
312     switch( i_code )
313     {
314     case 0x1b9:
315         block_Release( p_pkt );
316         break;
317
318     case 0x1ba:
319         if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
320         {
321             if( !p_sys->b_have_pack ) p_sys->b_have_pack = true;
322             /* done later on to work around bad vcd/svcd streams */
323             /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
324             if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
325         }
326         block_Release( p_pkt );
327         break;
328
329     case 0x1bb:
330         if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
331         {
332             int i;
333             for( i = 0; i < PS_TK_COUNT; i++ )
334             {
335                 ps_track_t *tk = &p_sys->tk[i];
336
337                 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
338                 {
339                     tk->es = es_out_Add( p_demux->out, &tk->fmt );
340                 }
341             }
342         }
343         block_Release( p_pkt );
344         break;
345
346     case 0x1bc:
347         if( p_sys->psm.i_version == 0xFFFF )
348             msg_Dbg( p_demux, "contains a PSM");
349
350         ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
351         block_Release( p_pkt );
352         break;
353
354     default:
355         if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
356         {
357             bool b_new = false;
358             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
359
360             if( !tk->b_seen )
361             {
362                 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
363                 {
364                     tk->es = es_out_Add( p_demux->out, &tk->fmt );
365                     b_new = true;
366                 }
367                 else
368                 {
369                     msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
370                 }
371                 tk->b_seen = true;
372             }
373
374             /* The popular VCD/SVCD subtitling WinSubMux does not
375              * renumber the SCRs when merging subtitles into the PES */
376             if( tk->b_seen &&
377                 ( tk->fmt.i_codec == VLC_FOURCC('o','g','t',' ') ||
378                   tk->fmt.i_codec == VLC_FOURCC('c','v','d',' ') ) )
379             {
380                 p_sys->i_scr = -1;
381             }
382
383             if( p_sys->i_scr > 0 )
384                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr );
385
386             p_sys->i_scr = -1;
387
388             if( tk->b_seen && tk->es &&
389                 (
390 #ifdef ZVBI_COMPILED /* FIXME!! */
391                 tk->fmt.i_codec == VLC_FOURCC('t','e','l','x') ||
392 #endif
393                 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) ) )
394             {
395                 if( !b_new && !p_sys->b_have_pack &&
396                     (tk->fmt.i_cat == AUDIO_ES) &&
397                     (p_pkt->i_pts > 0) )
398                 {
399                     /* A hack to sync the A/V on PES files. */
400                     msg_Dbg( p_demux, "force SCR: %"PRId64, p_pkt->i_pts );
401                     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_pkt->i_pts );
402                 }
403
404                 if( (int64_t)p_pkt->i_pts > p_sys->i_current_pts )
405                 {
406                     p_sys->i_current_pts = (int64_t)p_pkt->i_pts;
407                 }
408
409                 es_out_Send( p_demux->out, tk->es, p_pkt );
410             }
411             else
412             {
413                 block_Release( p_pkt );
414             }
415         }
416         else
417         {
418             block_Release( p_pkt );
419         }
420         break;
421     }
422
423     return 1;
424 }
425
426 /*****************************************************************************
427  * Control:
428  *****************************************************************************/
429 static int Control( demux_t *p_demux, int i_query, va_list args )
430 {
431     demux_sys_t *p_sys = p_demux->p_sys;
432     double f, *pf;
433     int64_t i64, *pi64;
434
435     switch( i_query )
436     {
437         case DEMUX_GET_POSITION:
438             pf = (double*) va_arg( args, double* );
439             i64 = stream_Size( p_demux->s );
440             if( i64 > 0 )
441             {
442                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
443             }
444             else
445             {
446                 *pf = 0.0;
447             }
448             return VLC_SUCCESS;
449
450         case DEMUX_SET_POSITION:
451             f = (double) va_arg( args, double );
452             i64 = stream_Size( p_demux->s );
453             p_sys->i_current_pts = 0;
454
455             return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
456
457         case DEMUX_GET_TIME:
458             pi64 = (int64_t*)va_arg( args, int64_t * );
459             if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
460             {
461                 *pi64 = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
462                 return VLC_SUCCESS;
463             }
464             if( p_sys->i_mux_rate > 0 )
465             {
466                 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
467                     p_sys->i_mux_rate;
468                 return VLC_SUCCESS;
469             }
470             *pi64 = 0;
471             return VLC_EGENERIC;
472
473         case DEMUX_GET_LENGTH:
474             pi64 = (int64_t*)va_arg( args, int64_t * );
475             if( p_sys->i_length > 0 )
476             {
477                 *pi64 = p_sys->i_length;
478                 return VLC_SUCCESS;
479             }
480             else if( p_sys->i_mux_rate > 0 )
481             {
482                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
483                     p_sys->i_mux_rate;
484                 return VLC_SUCCESS;
485             }
486             *pi64 = 0;
487             return VLC_EGENERIC;
488
489         case DEMUX_SET_TIME:
490             i64 = (int64_t)va_arg( args, int64_t );
491             if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
492             {
493                 int64_t i_now = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
494                 int64_t i_pos = stream_Tell( p_demux->s );
495
496                 if( !i_now )
497                     return i64 ? VLC_EGENERIC : VLC_SUCCESS;
498
499                 i_pos *= (float)i64 / (float)i_now;
500                 stream_Seek( p_demux->s, i_pos );
501                 return VLC_SUCCESS;
502             }
503             return VLC_EGENERIC;
504
505         case DEMUX_GET_FPS:
506         default:
507             return VLC_EGENERIC;
508     }
509 }
510
511 /*****************************************************************************
512  * Divers:
513  *****************************************************************************/
514
515 /* PSResynch: resynch on a system startcode
516  *  It doesn't skip more than 512 bytes
517  *  -1 -> error, 0 -> not synch, 1 -> ok
518  */
519 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
520 {
521     const uint8_t *p_peek;
522     int     i_peek;
523     int     i_skip;
524
525     if( stream_Peek( s, &p_peek, 4 ) < 4 )
526     {
527         return -1;
528     }
529     if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
530         p_peek[3] >= 0xb9 )
531     {
532         *pi_code = 0x100 | p_peek[3];
533         return 1;
534     }
535
536     if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
537     {
538         return -1;
539     }
540     i_skip = 0;
541
542     for( ;; )
543     {
544         if( i_peek < 4 )
545         {
546             break;
547         }
548         if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
549             p_peek[3] >= 0xb9 )
550         {
551             *pi_code = 0x100 | p_peek[3];
552             return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
553         }
554
555         p_peek++;
556         i_peek--;
557         i_skip++;
558     }
559     return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
560 }
561
562 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
563 {
564     const uint8_t *p_peek;
565     int      i_peek = stream_Peek( s, &p_peek, 14 );
566     int      i_size;
567     VLC_UNUSED(i_code);
568
569     /* Smallest valid packet */
570     if( i_peek < 6 ) return NULL;
571
572     i_size = ps_pkt_size( p_peek, i_peek );
573
574     if( i_size < 0 || ( i_size <= 6 && p_peek[3] > 0xba ) )
575     {
576         /* Special case, search the next start code */
577         i_size = 6;
578         for( ;; )
579         {
580             i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
581             if( i_peek <= i_size + 4 )
582             {
583                 return NULL;
584             }
585             while( i_size <= i_peek - 4 )
586             {
587                 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
588                     p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
589                 {
590                     return stream_Block( s, i_size );
591                 }
592                 i_size++;
593             }
594         }
595     }
596     else
597     {
598         /* Normal case */
599         return stream_Block( s, i_size );
600     }
601
602     return NULL;
603 }