1 /*****************************************************************************
2 * ps.c: Program Stream demux module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
35 * - re-add pre-scanning.
39 /*****************************************************************************
41 *****************************************************************************/
42 static int Open ( vlc_object_t * );
43 static void Close( vlc_object_t * );
46 set_description( _("PS demuxer") );
47 set_capability( "demux2", 1 );
48 set_callbacks( Open, Close );
52 /*****************************************************************************
54 *****************************************************************************/
59 ps_track_t tk[PS_TK_COUNT];
65 static int Demux ( demux_t *p_demux );
66 static int Control( demux_t *p_demux, int i_query, va_list args );
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 );
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open( vlc_object_t *p_this )
76 demux_t *p_demux = (demux_t*)p_this;
81 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
83 msg_Err( p_demux, "cannot peek" );
87 if( p_peek[0] != 0 || p_peek[1] != 0 || p_peek[2] != 1 ||
90 msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
91 "continuing anyway" );
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 ) );
100 p_sys->i_mux_rate = 0;
103 ps_psm_init( &p_sys->psm );
104 ps_track_init( p_sys->tk );
106 /* TODO prescanning of ES */
111 /*****************************************************************************
113 *****************************************************************************/
114 static void Close( vlc_object_t *p_this )
116 demux_t *p_demux = (demux_t*)p_this;
117 demux_sys_t *p_sys = p_demux->p_sys;
120 for( i = 0; i < PS_TK_COUNT; i++ )
122 ps_track_t *tk = &p_sys->tk[i];
125 es_format_Clean( &tk->fmt );
126 if( tk->es ) es_out_Del( p_demux->out, tk->es );
130 ps_psm_destroy( &p_sys->psm );
135 /*****************************************************************************
137 *****************************************************************************/
138 static int Demux( demux_t *p_demux )
140 demux_sys_t *p_sys = p_demux->p_sys;
141 int i_ret, i_id, i_mux_rate;
145 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
150 else if( i_ret == 0 )
152 msg_Warn( p_demux, "garbage at input" );
156 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
164 block_Release( p_pkt );
168 if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
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;
173 block_Release( p_pkt );
177 if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
180 for( i = 0; i < PS_TK_COUNT; i++ )
182 ps_track_t *tk = &p_sys->tk[i];
184 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
186 tk->es = es_out_Add( p_demux->out, &tk->fmt );
190 block_Release( p_pkt );
194 msg_Dbg( p_demux, "received PSM");
195 ps_psm_fill( &p_sys->psm, p_pkt );
196 block_Release( p_pkt );
200 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
202 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
206 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
208 tk->es = es_out_Add( p_demux->out, &tk->fmt );
210 tk->b_seen = VLC_TRUE;
212 if( tk->b_seen && tk->es &&
213 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
215 es_out_Send( p_demux->out, tk->es, p_pkt );
219 block_Release( p_pkt );
224 block_Release( p_pkt );
232 /*****************************************************************************
234 *****************************************************************************/
235 static int Control( demux_t *p_demux, int i_query, va_list args )
237 demux_sys_t *p_sys = p_demux->p_sys;
243 case DEMUX_GET_POSITION:
244 pf = (double*) va_arg( args, double* );
245 i64 = stream_Size( p_demux->s );
248 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
256 case DEMUX_SET_POSITION:
257 f = (double) va_arg( args, double );
258 i64 = stream_Size( p_demux->s );
260 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
262 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
265 pi64 = (int64_t*)va_arg( args, int64_t * );
266 if( p_sys->i_mux_rate > 0 )
268 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
275 case DEMUX_GET_LENGTH:
276 pi64 = (int64_t*)va_arg( args, int64_t * );
277 if( p_sys->i_mux_rate > 0 )
279 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
293 /*****************************************************************************
295 *****************************************************************************/
297 /* PSResynch: resynch on a system starcode
298 * It doesn't skip more than 512 bytes
299 * -1 -> error, 0 -> not synch, 1 -> ok
301 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
307 if( stream_Peek( s, &p_peek, 4 ) < 4 )
311 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
314 *pi_code = 0x100 | p_peek[3];
318 if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
330 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
333 *pi_code = 0x100 | p_peek[3];
334 return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
341 return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
344 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
347 int i_peek = stream_Peek( s, &p_peek, 14 );
348 int i_size = ps_pkt_size( p_peek, i_peek );
350 if( i_size <= 6 && p_peek[3] > 0xba )
352 /* Special case, search the next start code */
356 i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
357 if( i_peek <= i_size + 4 )
361 while( i_size <= i_peek - 4 )
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 )
366 return stream_Block( s, i_size );
375 return stream_Block( s, i_size );