1 /*****************************************************************************
2 * ps.c: Program Stream demux module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004 the VideoLAN team
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 int OpenAlt( vlc_object_t * );
44 static void Close ( vlc_object_t * );
47 set_description( _("PS demuxer") );
48 set_category( CAT_INPUT );
49 set_subcategory( SUBCAT_INPUT_DEMUX );
50 set_capability( "demux2", 1 );
51 set_callbacks( Open, Close );
55 set_description( _("PS demuxer") );
56 set_capability( "demux2", 9 );
57 set_callbacks( OpenAlt, Close );
60 /*****************************************************************************
62 *****************************************************************************/
67 ps_track_t tk[PS_TK_COUNT];
72 vlc_bool_t b_lost_sync;
75 static int Demux ( demux_t *p_demux );
76 static int Control( demux_t *p_demux, int i_query, va_list args );
78 static int ps_pkt_resynch( stream_t *, uint32_t *pi_code );
79 static block_t *ps_pkt_read ( stream_t *, uint32_t i_code );
81 /*****************************************************************************
83 *****************************************************************************/
84 static int Open( vlc_object_t *p_this )
86 demux_t *p_demux = (demux_t*)p_this;
91 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
93 msg_Err( p_demux, "cannot peek" );
97 if( p_peek[0] != 0 || p_peek[1] != 0 ||
98 p_peek[2] != 1 || p_peek[3] < 0xb9 )
100 msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
101 "continuing anyway" );
104 /* Fill p_demux field */
105 p_demux->pf_demux = Demux;
106 p_demux->pf_control = Control;
107 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
110 p_sys->i_mux_rate = 0;
112 p_sys->b_lost_sync = VLC_FALSE;
114 ps_psm_init( &p_sys->psm );
115 ps_track_init( p_sys->tk );
117 /* TODO prescanning of ES */
122 static int OpenAlt( vlc_object_t *p_this )
124 demux_t *p_demux = (demux_t*)p_this;
127 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
129 msg_Err( p_demux, "cannot peek" );
133 if( p_peek[0] != 0 || p_peek[1] != 0 ||
134 p_peek[2] != 1 || p_peek[3] < 0xb9 )
136 if( !p_demux->b_force ) return VLC_EGENERIC;
139 return Open( p_this );
142 /*****************************************************************************
144 *****************************************************************************/
145 static void Close( vlc_object_t *p_this )
147 demux_t *p_demux = (demux_t*)p_this;
148 demux_sys_t *p_sys = p_demux->p_sys;
151 for( i = 0; i < PS_TK_COUNT; i++ )
153 ps_track_t *tk = &p_sys->tk[i];
156 es_format_Clean( &tk->fmt );
157 if( tk->es ) es_out_Del( p_demux->out, tk->es );
161 ps_psm_destroy( &p_sys->psm );
166 /*****************************************************************************
168 *****************************************************************************/
169 static int Demux( demux_t *p_demux )
171 demux_sys_t *p_sys = p_demux->p_sys;
172 int i_ret, i_id, i_mux_rate;
176 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
181 else if( i_ret == 0 )
183 if( !p_sys->b_lost_sync )
184 msg_Warn( p_demux, "garbage at input, trying to resync..." );
186 p_sys->b_lost_sync = VLC_TRUE;
190 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
191 p_sys->b_lost_sync = VLC_FALSE;
193 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
201 block_Release( p_pkt );
205 if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
207 /* done later on to work around bad vcd/svcd streams */
208 /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
209 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
211 block_Release( p_pkt );
215 if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
218 for( i = 0; i < PS_TK_COUNT; i++ )
220 ps_track_t *tk = &p_sys->tk[i];
222 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
224 tk->es = es_out_Add( p_demux->out, &tk->fmt );
228 block_Release( p_pkt );
232 if( p_sys->psm.i_version == 0xFFFF )
233 msg_Dbg( p_demux, "contains a PSM");
235 ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
236 block_Release( p_pkt );
240 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
242 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
246 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
248 tk->es = es_out_Add( p_demux->out, &tk->fmt );
252 msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
254 tk->b_seen = VLC_TRUE;
257 /* The popular VCD/SVCD subtitling WinSubMux does not
258 * renumber the SCRs when merging subtitles into the PES */
260 ( tk->fmt.i_codec == VLC_FOURCC('o','g','t',' ') ||
261 tk->fmt.i_codec == VLC_FOURCC('c','v','d',' ') ) )
266 if( p_sys->i_scr > 0 )
267 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr );
271 if( tk->b_seen && tk->es &&
272 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
274 es_out_Send( p_demux->out, tk->es, p_pkt );
278 block_Release( p_pkt );
283 block_Release( p_pkt );
291 /*****************************************************************************
293 *****************************************************************************/
294 static int Control( demux_t *p_demux, int i_query, va_list args )
296 demux_sys_t *p_sys = p_demux->p_sys;
302 case DEMUX_GET_POSITION:
303 pf = (double*) va_arg( args, double* );
304 i64 = stream_Size( p_demux->s );
307 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
315 case DEMUX_SET_POSITION:
316 f = (double) va_arg( args, double );
317 i64 = stream_Size( p_demux->s );
319 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
321 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
324 pi64 = (int64_t*)va_arg( args, int64_t * );
325 if( p_sys->i_mux_rate > 0 )
327 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
334 case DEMUX_GET_LENGTH:
335 pi64 = (int64_t*)va_arg( args, int64_t * );
336 if( p_sys->i_mux_rate > 0 )
338 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
352 /*****************************************************************************
354 *****************************************************************************/
356 /* PSResynch: resynch on a system starcode
357 * It doesn't skip more than 512 bytes
358 * -1 -> error, 0 -> not synch, 1 -> ok
360 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
366 if( stream_Peek( s, &p_peek, 4 ) < 4 )
370 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
373 *pi_code = 0x100 | p_peek[3];
377 if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
389 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
392 *pi_code = 0x100 | p_peek[3];
393 return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
400 return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
403 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
406 int i_peek = stream_Peek( s, &p_peek, 14 );
407 int i_size = ps_pkt_size( p_peek, i_peek );
409 if( i_size <= 6 && p_peek[3] > 0xba )
411 /* Special case, search the next start code */
415 i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
416 if( i_peek <= i_size + 4 )
420 while( i_size <= i_peek - 4 )
422 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
423 p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
425 return stream_Block( s, i_size );
434 return stream_Block( s, i_size );