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