]> git.sesse.net Git - vlc/blob - modules/demux/ps.c
* src/input/clock.c, input_internal.h: backport of 13165.
[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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/input.h>
31
32 #include "ps.h"
33
34 /* TODO:
35  *  - re-add pre-scanning.
36  *  - ...
37  */
38
39 /*****************************************************************************
40  * Module descriptor
41  *****************************************************************************/
42 static int  Open   ( vlc_object_t * );
43 static int  OpenAlt( vlc_object_t * );
44 static void Close  ( vlc_object_t * );
45
46 vlc_module_begin();
47     set_description( _("PS demuxer") );
48     set_category( CAT_INPUT );
49     set_subcategory( SUBCAT_INPUT_DEMUX );
50     set_capability( "demux2", 1 );
51     set_callbacks( Open, Close );
52     add_shortcut( "ps" );
53
54     add_submodule();
55     set_description( _("PS demuxer") );
56     set_capability( "demux2", 9 );
57     set_callbacks( OpenAlt, Close );
58 vlc_module_end();
59
60 /*****************************************************************************
61  * Local prototypes
62  *****************************************************************************/
63
64 struct demux_sys_t
65 {
66     ps_psm_t    psm;
67     ps_track_t  tk[PS_TK_COUNT];
68
69     int64_t     i_scr;
70     int         i_mux_rate;
71 };
72
73 static int Demux  ( demux_t *p_demux );
74 static int Control( demux_t *p_demux, int i_query, va_list args );
75
76 static int      ps_pkt_resynch( stream_t *, uint32_t *pi_code );
77 static block_t *ps_pkt_read   ( stream_t *, uint32_t i_code );
78
79 /*****************************************************************************
80  * Open
81  *****************************************************************************/
82 static int Open( vlc_object_t *p_this )
83 {
84     demux_t     *p_demux = (demux_t*)p_this;
85     demux_sys_t *p_sys;
86
87     uint8_t     *p_peek;
88
89     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
90     {
91         msg_Err( p_demux, "cannot peek" );
92         return VLC_EGENERIC;
93     }
94
95     if( p_peek[0] != 0 || p_peek[1] != 0 ||
96         p_peek[2] != 1 || p_peek[3] < 0xb9 )
97     {
98         msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
99                   "continuing anyway" );
100     }
101
102     /* Fill p_demux field */
103     p_demux->pf_demux = Demux;
104     p_demux->pf_control = Control;
105     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
106
107     /* Init p_sys */
108     p_sys->i_mux_rate = 0;
109     p_sys->i_scr      = -1;
110
111     ps_psm_init( &p_sys->psm );
112     ps_track_init( p_sys->tk );
113
114     /* TODO prescanning of ES */
115
116     return VLC_SUCCESS;
117 }
118
119 static int OpenAlt( vlc_object_t *p_this )
120 {
121     demux_t *p_demux = (demux_t*)p_this;
122     uint8_t *p_peek;
123
124     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
125     {
126         msg_Err( p_demux, "cannot peek" );
127         return VLC_EGENERIC;
128     }
129
130     if( p_peek[0] != 0 || p_peek[1] != 0 ||
131         p_peek[2] != 1 || p_peek[3] < 0xb9 )
132     {
133         if( !p_demux->b_force ) return VLC_EGENERIC;
134     }
135
136     return Open( p_this );
137 }
138
139 /*****************************************************************************
140  * Close
141  *****************************************************************************/
142 static void Close( vlc_object_t *p_this )
143 {
144     demux_t     *p_demux = (demux_t*)p_this;
145     demux_sys_t *p_sys = p_demux->p_sys;
146     int i;
147
148     for( i = 0; i < PS_TK_COUNT; i++ )
149     {
150         ps_track_t *tk = &p_sys->tk[i];
151         if( tk->b_seen )
152         {
153             es_format_Clean( &tk->fmt );
154             if( tk->es ) es_out_Del( p_demux->out, tk->es );
155         }
156     }
157
158     ps_psm_destroy( &p_sys->psm );
159
160     free( p_sys );
161 }
162
163 /*****************************************************************************
164  * Demux:
165  *****************************************************************************/
166 static int Demux( demux_t *p_demux )
167 {
168     demux_sys_t *p_sys = p_demux->p_sys;
169     int i_ret, i_id, i_mux_rate;
170     uint32_t i_code;
171     block_t *p_pkt;
172
173     i_ret = ps_pkt_resynch( p_demux->s, &i_code );
174     if( i_ret < 0 )
175     {
176         return 0;
177     }
178     else if( i_ret == 0 )
179     {
180         msg_Warn( p_demux, "garbage at input" );
181         return 1;
182     }
183
184     if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
185     {
186         return 0;
187     }
188
189     switch( i_code )
190     {
191     case 0x1b9:
192         block_Release( p_pkt );
193         break;
194
195     case 0x1ba:
196         if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
197         {
198             /* done later on to work around bad vcd/svcd streams */
199             /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
200             if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
201         }
202         block_Release( p_pkt );
203         break;
204
205     case 0x1bb:
206         if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
207         {
208             int i;
209             for( i = 0; i < PS_TK_COUNT; i++ )
210             {
211                 ps_track_t *tk = &p_sys->tk[i];
212
213                 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
214                 {
215                     tk->es = es_out_Add( p_demux->out, &tk->fmt );
216                 }
217             }
218         }
219         block_Release( p_pkt );
220         break;
221
222     case 0x1bc:
223         if( p_sys->psm.i_version == 0xFFFF )
224             msg_Dbg( p_demux, "contains a PSM");
225
226         ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
227         block_Release( p_pkt );
228         break;
229
230     default:
231         if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
232         {
233             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
234
235             if( !tk->b_seen )
236             {
237                 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
238                 {
239                     tk->es = es_out_Add( p_demux->out, &tk->fmt );
240                 }
241                 else
242                 {
243                     msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
244                 }
245                 tk->b_seen = VLC_TRUE;
246             }
247
248             /* The popular VCD/SVCD subtitling WinSubMux does not
249              * renumber the SCRs when merging subtitles into the PES */
250             if( tk->b_seen &&
251                 ( tk->fmt.i_codec == VLC_FOURCC('o','g','t',' ') ||
252                   tk->fmt.i_codec == VLC_FOURCC('c','v','d',' ') ) )
253             {
254                 p_sys->i_scr = -1;
255             }
256
257             if( p_sys->i_scr > 0 )
258                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr );
259
260             p_sys->i_scr = -1;
261
262             if( tk->b_seen && tk->es &&
263                 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
264             {
265                 es_out_Send( p_demux->out, tk->es, p_pkt );
266             }
267             else
268             {
269                 block_Release( p_pkt );
270             }
271         }
272         else
273         {
274             block_Release( p_pkt );
275         }
276         break;
277     }
278
279     return 1;
280 }
281
282 /*****************************************************************************
283  * Control:
284  *****************************************************************************/
285 static int Control( demux_t *p_demux, int i_query, va_list args )
286 {
287     demux_sys_t *p_sys = p_demux->p_sys;
288     double f, *pf;
289     int64_t i64, *pi64;
290
291     switch( i_query )
292     {
293         case DEMUX_GET_POSITION:
294             pf = (double*) va_arg( args, double* );
295             i64 = stream_Size( p_demux->s );
296             if( i64 > 0 )
297             {
298                 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
299             }
300             else
301             {
302                 *pf = 0.0;
303             }
304             return VLC_SUCCESS;
305
306         case DEMUX_SET_POSITION:
307             f = (double) va_arg( args, double );
308             i64 = stream_Size( p_demux->s );
309
310             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
311
312             return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
313
314         case DEMUX_GET_TIME:
315             pi64 = (int64_t*)va_arg( args, int64_t * );
316             if( p_sys->i_mux_rate > 0 )
317             {
318                 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
319                     p_sys->i_mux_rate;
320                 return VLC_SUCCESS;
321             }
322             *pi64 = 0;
323             return VLC_EGENERIC;
324
325         case DEMUX_GET_LENGTH:
326             pi64 = (int64_t*)va_arg( args, int64_t * );
327             if( p_sys->i_mux_rate > 0 )
328             {
329                 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
330                     p_sys->i_mux_rate;
331                 return VLC_SUCCESS;
332             }
333             *pi64 = 0;
334             return VLC_EGENERIC;
335
336         case DEMUX_SET_TIME:
337         case DEMUX_GET_FPS:
338         default:
339             return VLC_EGENERIC;
340     }
341 }
342
343 /*****************************************************************************
344  * Divers:
345  *****************************************************************************/
346
347 /* PSResynch: resynch on a system starcode
348  *  It doesn't skip more than 512 bytes
349  *  -1 -> error, 0 -> not synch, 1 -> ok
350  */
351 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
352 {
353     uint8_t *p_peek;
354     int     i_peek;
355     int     i_skip;
356
357     if( stream_Peek( s, &p_peek, 4 ) < 4 )
358     {
359         return -1;
360     }
361     if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
362         p_peek[3] >= 0xb9 )
363     {
364         *pi_code = 0x100 | p_peek[3];
365         return 1;
366     }
367
368     if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
369     {
370         return -1;
371     }
372     i_skip = 0;
373
374     for( ;; )
375     {
376         if( i_peek < 4 )
377         {
378             break;
379         }
380         if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
381             p_peek[3] >= 0xb9 )
382         {
383             *pi_code = 0x100 | p_peek[3];
384             return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
385         }
386
387         p_peek++;
388         i_peek--;
389         i_skip++;
390     }
391     return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
392 }
393
394 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
395 {
396     uint8_t *p_peek;
397     int      i_peek = stream_Peek( s, &p_peek, 14 );
398     int      i_size = ps_pkt_size( p_peek, i_peek );
399
400     if( i_size <= 6 && p_peek[3] > 0xba )
401     {
402         /* Special case, search the next start code */
403         i_size = 6;
404         for( ;; )
405         {
406             i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
407             if( i_peek <= i_size + 4 )
408             {
409                 return NULL;
410             }
411             while( i_size <= i_peek - 4 )
412             {
413                 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
414                     p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
415                 {
416                     return stream_Block( s, i_size );
417                 }
418                 i_size++;
419             }
420         }
421     }
422     else
423     {
424         /* Normal case */
425         return stream_Block( s, i_size );
426     }
427
428     return NULL;
429 }