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