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