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