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