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