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