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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
30 #include <vlc/input.h>
35 * - re-add pre-scanning.
39 #define TIME_TEXT N_("Trust MPEG timestamps")
40 #define TIME_LONGTEXT N_("Normally we use the timestamps of the MPEG files " \
41 "to calculate position and duration. However sometimes this might not " \
42 "be usable. Disable this option to calculate from the bitrate instead." )
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static int OpenAlt( vlc_object_t * );
49 static void Close ( vlc_object_t * );
52 set_description( _("MPEG-PS demuxer") );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_DEMUX );
55 set_capability( "demux2", 1 );
56 set_callbacks( Open, Close );
59 add_integer( "ps-trust-timestamps", VLC_TRUE, NULL, TIME_TEXT,
60 TIME_LONGTEXT, VLC_TRUE );
63 set_description( _("MPEG-PS demuxer") );
64 set_capability( "demux2", 9 );
65 set_callbacks( OpenAlt, Close );
68 /*****************************************************************************
70 *****************************************************************************/
75 ps_track_t tk[PS_TK_COUNT];
81 int64_t i_current_pts;
83 vlc_bool_t b_lost_sync;
84 vlc_bool_t b_have_pack;
85 vlc_bool_t b_seekable;
88 static int Demux ( demux_t *p_demux );
89 static int Control( demux_t *p_demux, int i_query, va_list args );
91 static int ps_pkt_resynch( stream_t *, uint32_t *pi_code );
92 static block_t *ps_pkt_read ( stream_t *, uint32_t i_code );
94 /*****************************************************************************
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 demux_t *p_demux = (demux_t*)p_this;
104 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
106 msg_Err( p_demux, "cannot peek" );
110 if( p_peek[0] != 0 || p_peek[1] != 0 ||
111 p_peek[2] != 1 || p_peek[3] < 0xb9 )
113 msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
114 "continuing anyway" );
117 /* Fill p_demux field */
118 p_demux->pf_demux = Demux;
119 p_demux->pf_control = Control;
120 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
123 p_sys->i_mux_rate = 0;
125 p_sys->i_length = -1;
126 p_sys->i_current_pts = (mtime_t) 0;
128 p_sys->b_lost_sync = VLC_FALSE;
129 p_sys->b_have_pack = VLC_FALSE;
130 p_sys->b_seekable = VLC_FALSE;
132 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
134 ps_psm_init( &p_sys->psm );
135 ps_track_init( p_sys->tk );
137 /* TODO prescanning of ES */
142 static int OpenAlt( vlc_object_t *p_this )
144 demux_t *p_demux = (demux_t*)p_this;
147 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
149 msg_Err( p_demux, "cannot peek" );
153 if( p_peek[0] != 0 || p_peek[1] != 0 ||
154 p_peek[2] != 1 || p_peek[3] < 0xb9 )
156 if( !p_demux->b_force ) return VLC_EGENERIC;
159 return Open( p_this );
162 /*****************************************************************************
164 *****************************************************************************/
165 static void Close( vlc_object_t *p_this )
167 demux_t *p_demux = (demux_t*)p_this;
168 demux_sys_t *p_sys = p_demux->p_sys;
171 for( i = 0; i < PS_TK_COUNT; i++ )
173 ps_track_t *tk = &p_sys->tk[i];
176 es_format_Clean( &tk->fmt );
177 if( tk->es ) es_out_Del( p_demux->out, tk->es );
181 ps_psm_destroy( &p_sys->psm );
186 static int Demux2( demux_t *p_demux )
188 demux_sys_t *p_sys = p_demux->p_sys;
193 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
198 else if( i_ret == 0 )
200 if( !p_sys->b_lost_sync )
201 msg_Warn( p_demux, "garbage at input, trying to resync..." );
203 p_sys->b_lost_sync = VLC_TRUE;
207 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
208 p_sys->b_lost_sync = VLC_FALSE;
210 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
214 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
216 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
217 if( !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
219 tk->i_last_pts = p_pkt->i_pts;
222 block_Release( p_pkt );
226 static void FindLength( demux_t *p_demux )
228 demux_sys_t *p_sys = p_demux->p_sys;
229 int64_t i_current_pos = -1, i_size = 0, i_end = 0;
232 if( !var_CreateGetInteger( p_demux, "ps-trust-timestamps" ) )
235 if( p_sys->i_length == -1 ) /* First time */
237 i_current_pos = stream_Tell( p_demux->s );
238 i_size = stream_Size( p_demux->s );
239 i_end = __MAX( 0, __MIN( 100000, i_size ) );
240 stream_Seek( p_demux->s, stream_Size( p_demux->s ) - i_end );
242 while( Demux2( p_demux ) > 0 && !p_demux->b_die ) ;
245 for( i = 0; i < PS_TK_COUNT; i++ )
247 ps_track_t *tk = &p_sys->tk[i];
248 if( tk->b_seen && tk->i_first_pts > 0 && tk->i_last_pts > 0 )
249 if( tk->i_last_pts > tk->i_first_pts )
251 int64_t i_length = (int64_t)tk->i_last_pts - tk->i_first_pts;
252 if( i_length > p_sys->i_length )
254 p_sys->i_length = i_length;
255 p_sys->i_time_track = i;
259 if( i_current_pos >= 0 ) stream_Seek( p_demux->s, i_current_pos );
262 /*****************************************************************************
264 *****************************************************************************/
265 static int Demux( demux_t *p_demux )
267 demux_sys_t *p_sys = p_demux->p_sys;
268 int i_ret, i_id, i_mux_rate;
272 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
277 else if( i_ret == 0 )
279 if( !p_sys->b_lost_sync )
280 msg_Warn( p_demux, "garbage at input, trying to resync..." );
282 p_sys->b_lost_sync = VLC_TRUE;
286 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
287 p_sys->b_lost_sync = VLC_FALSE;
289 if( p_sys->b_seekable && p_sys->i_length <= 0 )
290 FindLength( p_demux );
292 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
300 block_Release( p_pkt );
304 if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
306 if( !p_sys->b_have_pack ) p_sys->b_have_pack = VLC_TRUE;
307 /* done later on to work around bad vcd/svcd streams */
308 /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
309 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
311 block_Release( p_pkt );
315 if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
318 for( i = 0; i < PS_TK_COUNT; i++ )
320 ps_track_t *tk = &p_sys->tk[i];
322 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
324 tk->es = es_out_Add( p_demux->out, &tk->fmt );
328 block_Release( p_pkt );
332 if( p_sys->psm.i_version == 0xFFFF )
333 msg_Dbg( p_demux, "contains a PSM");
335 ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
336 block_Release( p_pkt );
340 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
342 vlc_bool_t b_new = VLC_FALSE;
343 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
347 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
349 tk->es = es_out_Add( p_demux->out, &tk->fmt );
354 msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
356 tk->b_seen = VLC_TRUE;
359 /* The popular VCD/SVCD subtitling WinSubMux does not
360 * renumber the SCRs when merging subtitles into the PES */
362 ( tk->fmt.i_codec == VLC_FOURCC('o','g','t',' ') ||
363 tk->fmt.i_codec == VLC_FOURCC('c','v','d',' ') ) )
368 if( p_sys->i_scr > 0 )
369 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr );
373 if( tk->b_seen && tk->es &&
374 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
376 if( !b_new && !p_sys->b_have_pack && tk->fmt.i_cat == AUDIO_ES && p_pkt->i_pts > 0 )
378 /* A hack to sync the A/V on PES files. */
379 msg_Dbg( p_demux, "force SCR: %lld", p_pkt->i_pts );
380 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_pkt->i_pts );
383 if( !tk->i_first_pts && p_pkt->i_pts > 0 )
385 tk->i_first_pts = p_pkt->i_pts;
388 if( (int64_t)p_pkt->i_pts > p_sys->i_current_pts )
390 p_sys->i_current_pts = (int64_t)p_pkt->i_pts;
393 es_out_Send( p_demux->out, tk->es, p_pkt );
397 block_Release( p_pkt );
402 block_Release( p_pkt );
410 /*****************************************************************************
412 *****************************************************************************/
413 static int Control( demux_t *p_demux, int i_query, va_list args )
415 demux_sys_t *p_sys = p_demux->p_sys;
421 case DEMUX_GET_POSITION:
422 pf = (double*) va_arg( args, double* );
423 i64 = stream_Size( p_demux->s );
426 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
434 case DEMUX_SET_POSITION:
435 f = (double) va_arg( args, double );
436 i64 = stream_Size( p_demux->s );
437 p_sys->i_current_pts = 0;
438 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
440 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
443 pi64 = (int64_t*)va_arg( args, int64_t * );
444 if( p_sys->i_current_pts > 0 )
446 *pi64 = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
449 if( p_sys->i_mux_rate > 0 )
451 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
458 case DEMUX_GET_LENGTH:
459 pi64 = (int64_t*)va_arg( args, int64_t * );
460 if( p_sys->i_length > 0 )
462 *pi64 = p_sys->i_length;
465 else if( p_sys->i_mux_rate > 0 )
467 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
481 /*****************************************************************************
483 *****************************************************************************/
485 /* PSResynch: resynch on a system starcode
486 * It doesn't skip more than 512 bytes
487 * -1 -> error, 0 -> not synch, 1 -> ok
489 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
495 if( stream_Peek( s, &p_peek, 4 ) < 4 )
499 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
502 *pi_code = 0x100 | p_peek[3];
506 if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
518 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
521 *pi_code = 0x100 | p_peek[3];
522 return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
529 return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
532 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
535 int i_peek = stream_Peek( s, &p_peek, 14 );
536 int i_size = ps_pkt_size( p_peek, i_peek );
538 if( i_size <= 6 && p_peek[3] > 0xba )
540 /* Special case, search the next start code */
544 i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
545 if( i_peek <= i_size + 4 )
549 while( i_size <= i_peek - 4 )
551 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
552 p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
554 return stream_Block( s, i_size );
563 return stream_Block( s, i_size );