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