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