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