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