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