63d32f405bf67b990ed917b55e3de5c92fb2e5c0
[vlc.git] / 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_bool( "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     p_sys->i_time_track = -1;
128     
129     p_sys->b_lost_sync = VLC_FALSE;
130     p_sys->b_have_pack = VLC_FALSE;
131     p_sys->b_seekable  = VLC_FALSE;
132
133     stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
134
135     ps_psm_init( &p_sys->psm );
136     ps_track_init( p_sys->tk );
137
138     /* TODO prescanning of ES */
139
140     return VLC_SUCCESS;
141 }
142
143 static int OpenAlt( vlc_object_t *p_this )
144 {
145     demux_t *p_demux = (demux_t*)p_this;
146     uint8_t *p_peek;
147
148     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
149     {
150         msg_Err( p_demux, "cannot peek" );
151         return VLC_EGENERIC;
152     }
153
154     if( p_peek[0] != 0 || p_peek[1] != 0 ||
155         p_peek[2] != 1 || p_peek[3] < 0xb9 )
156     {
157         if( !p_demux->b_force ) return VLC_EGENERIC;
158     }
159
160     return Open( p_this );
161 }
162
163 /*****************************************************************************
164  * Close
165  *****************************************************************************/
166 static void Close( vlc_object_t *p_this )
167 {
168     demux_t     *p_demux = (demux_t*)p_this;
169     demux_sys_t *p_sys = p_demux->p_sys;
170     int i;
171
172     for( i = 0; i < PS_TK_COUNT; i++ )
173     {
174         ps_track_t *tk = &p_sys->tk[i];
175         if( tk->b_seen )
176         {
177             es_format_Clean( &tk->fmt );
178             if( tk->es ) es_out_Del( p_demux->out, tk->es );
179         }
180     }
181
182     ps_psm_destroy( &p_sys->psm );
183
184     free( p_sys );
185 }
186
187 static int Demux2( demux_t *p_demux, vlc_bool_t b_end )
188 {
189     demux_sys_t *p_sys = p_demux->p_sys;
190     int i_ret, i_id;
191     uint32_t i_code;
192     block_t *p_pkt;
193
194     i_ret = ps_pkt_resynch( p_demux->s, &i_code );
195     if( i_ret < 0 )
196     {
197         return 0;
198     }
199     else if( i_ret == 0 )
200     {
201         if( !p_sys->b_lost_sync )
202             msg_Warn( p_demux, "garbage at input, trying to resync..." );
203
204         p_sys->b_lost_sync = VLC_TRUE;
205         return 1;
206     }
207
208     if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
209     p_sys->b_lost_sync = VLC_FALSE;
210
211     if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
212     {
213         return 0;
214     }
215     if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
216     {
217         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
218         if( !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
219         {
220             if( b_end && p_pkt->i_pts > tk->i_last_pts )
221             {
222                 tk->i_last_pts = p_pkt->i_pts;
223             }
224             else if ( tk->i_first_pts == -1 )
225             {
226                 tk->i_first_pts = p_pkt->i_pts;
227             }
228         }
229     }
230     block_Release( p_pkt );
231     return 1;
232 }
233
234 static void FindLength( demux_t *p_demux )
235 {
236     demux_sys_t *p_sys = p_demux->p_sys;
237     int64_t i_current_pos = -1, i_size = 0, i_end = 0;
238     int i;
239
240     if( !var_CreateGetInteger( p_demux, "ps-trust-timestamps" ) )
241         return;
242
243     if( p_sys->i_length == -1 ) /* First time */
244     {
245         p_sys->i_length = 0;
246         /* Check beginning */
247         i = 0;
248         i_current_pos = stream_Tell( p_demux->s );
249         while( !p_demux->b_die && i < 40 && Demux2( p_demux, VLC_FALSE ) > 0 ) i++;
250
251         /* Check end */
252         i_size = stream_Size( p_demux->s );
253         i_end = __MAX( 0, __MIN( 200000, i_size ) );
254         stream_Seek( p_demux->s, i_size - i_end );
255     
256         while( !p_demux->b_die && Demux2( p_demux, VLC_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: %lld", 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 = VLC_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 = VLC_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 = VLC_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             vlc_bool_t b_new = VLC_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 = VLC_TRUE;
366                 }
367                 else
368                 {
369                     msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
370                 }
371                 tk->b_seen = VLC_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                 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
390             {
391                 if( !b_new && !p_sys->b_have_pack && tk->fmt.i_cat == AUDIO_ES && p_pkt->i_pts > 0 )
392                 {
393                     /* A hack to sync the A/V on PES files. */
394                     msg_Dbg( p_demux, "force SCR: %lld", p_pkt->i_pts );
395                     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_pkt->i_pts );
396                 }
397
398                 if( (int64_t)p_pkt->i_pts > p_sys->i_current_pts )
399                 {
400                     p_sys->i_current_pts = (int64_t)p_pkt->i_pts;
401                 }
402
403                 es_out_Send( p_demux->out, tk->es, p_pkt );
404             }
405             else
406             {
407                 block_Release( p_pkt );
408             }
409         }
410         else
411         {
412             block_Release( p_pkt );
413         }
414         break;
415     }
416
417     return 1;
418 }
419
420 /*****************************************************************************
421  * Control:
422  *****************************************************************************/
423 static int Control( demux_t *p_demux, int i_query, va_list args )
424 {
425     demux_sys_t *p_sys = p_demux->p_sys;
426     double f, *pf;
427     int64_t i64, *pi64;
428
429     switch( i_query )
430     {
431         case DEMUX_GET_POSITION:
432             pf = (double*) va_arg( args, double* );
433             i64 = stream_Size( p_demux->s );
434             if( i64 > 0 )
435             {
436                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
437             }
438             else
439             {
440                 *pf = 0.0;
441             }
442             return VLC_SUCCESS;
443
444         case DEMUX_SET_POSITION:
445             f = (double) va_arg( args, double );
446             i64 = stream_Size( p_demux->s );
447             p_sys->i_current_pts = 0;
448             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
449
450             return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
451
452         case DEMUX_GET_TIME:
453             pi64 = (int64_t*)va_arg( args, int64_t * );
454             if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
455             {
456                 *pi64 = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
457                 return VLC_SUCCESS;
458             }
459             if( p_sys->i_mux_rate > 0 )
460             {
461                 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
462                     p_sys->i_mux_rate;
463                 return VLC_SUCCESS;
464             }
465             *pi64 = 0;
466             return VLC_EGENERIC;
467
468         case DEMUX_GET_LENGTH:
469             pi64 = (int64_t*)va_arg( args, int64_t * );
470             if( p_sys->i_length > 0 )
471             {
472                 *pi64 = p_sys->i_length;
473                 return VLC_SUCCESS;
474             }
475             else if( p_sys->i_mux_rate > 0 )
476             {
477                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
478                     p_sys->i_mux_rate;
479                 return VLC_SUCCESS;
480             }
481             *pi64 = 0;
482             return VLC_EGENERIC;
483
484         case DEMUX_SET_TIME:
485         case DEMUX_GET_FPS:
486         default:
487             return VLC_EGENERIC;
488     }
489 }
490
491 /*****************************************************************************
492  * Divers:
493  *****************************************************************************/
494
495 /* PSResynch: resynch on a system starcode
496  *  It doesn't skip more than 512 bytes
497  *  -1 -> error, 0 -> not synch, 1 -> ok
498  */
499 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
500 {
501     uint8_t *p_peek;
502     int     i_peek;
503     int     i_skip;
504
505     if( stream_Peek( s, &p_peek, 4 ) < 4 )
506     {
507         return -1;
508     }
509     if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
510         p_peek[3] >= 0xb9 )
511     {
512         *pi_code = 0x100 | p_peek[3];
513         return 1;
514     }
515
516     if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
517     {
518         return -1;
519     }
520     i_skip = 0;
521
522     for( ;; )
523     {
524         if( i_peek < 4 )
525         {
526             break;
527         }
528         if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
529             p_peek[3] >= 0xb9 )
530         {
531             *pi_code = 0x100 | p_peek[3];
532             return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
533         }
534
535         p_peek++;
536         i_peek--;
537         i_skip++;
538     }
539     return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
540 }
541
542 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
543 {
544     uint8_t *p_peek;
545     int      i_peek = stream_Peek( s, &p_peek, 14 );
546     int      i_size = ps_pkt_size( p_peek, i_peek );
547
548     if( i_size <= 6 && p_peek[3] > 0xba )
549     {
550         /* Special case, search the next start code */
551         i_size = 6;
552         for( ;; )
553         {
554             i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
555             if( i_peek <= i_size + 4 )
556             {
557                 return NULL;
558             }
559             while( i_size <= i_peek - 4 )
560             {
561                 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
562                     p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
563                 {
564                     return stream_Block( s, i_size );
565                 }
566                 i_size++;
567             }
568         }
569     }
570     else
571     {
572         /* Normal case */
573         return stream_Block( s, i_size );
574     }
575
576     return NULL;
577 }