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 *****************************************************************************/
29 #include <vlc_demux.h>
34 * - re-add pre-scanning.
38 #define TIME_TEXT N_("Trust MPEG timestamps")
39 #define TIME_LONGTEXT N_("Normally we use the timestamps of the MPEG files " \
40 "to calculate position and duration. However sometimes this might not " \
41 "be usable. Disable this option to calculate from the bitrate instead." )
43 /*****************************************************************************
45 *****************************************************************************/
46 static int OpenForce( vlc_object_t * );
47 static int Open ( vlc_object_t * );
48 static void Close ( vlc_object_t * );
51 set_description( _("MPEG-PS demuxer") );
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_DEMUX );
54 set_capability( "demux2", 1 );
55 set_callbacks( OpenForce, Close );
58 add_bool( "ps-trust-timestamps", VLC_TRUE, NULL, TIME_TEXT,
59 TIME_LONGTEXT, VLC_TRUE );
63 set_description( _("MPEG-PS demuxer") );
64 set_capability( "demux2", 8 );
65 set_callbacks( Open, 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 OpenCommon( vlc_object_t *p_this, vlc_bool_t b_force )
99 demux_t *p_demux = (demux_t*)p_this;
102 const uint8_t *p_peek;
104 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
106 msg_Err( p_demux, "cannot peek" );
110 if( memcmp( p_peek, "\x00\x00\x01", 3 ) || ( p_peek[3] < 0xb9 ) )
115 msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
116 "continuing anyway" );
119 /* Fill p_demux field */
120 p_demux->pf_demux = Demux;
121 p_demux->pf_control = Control;
122 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
125 p_sys->i_mux_rate = 0;
127 p_sys->i_length = -1;
128 p_sys->i_current_pts = (mtime_t) 0;
129 p_sys->i_time_track = -1;
131 p_sys->b_lost_sync = VLC_FALSE;
132 p_sys->b_have_pack = VLC_FALSE;
133 p_sys->b_seekable = VLC_FALSE;
135 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
137 ps_psm_init( &p_sys->psm );
138 ps_track_init( p_sys->tk );
140 /* TODO prescanning of ES */
145 static int OpenForce( vlc_object_t *p_this )
147 return OpenCommon( p_this, VLC_TRUE );
150 static int Open( vlc_object_t *p_this )
152 return OpenCommon( p_this, ((demux_t *)p_this)->b_force );
155 /*****************************************************************************
157 *****************************************************************************/
158 static void Close( vlc_object_t *p_this )
160 demux_t *p_demux = (demux_t*)p_this;
161 demux_sys_t *p_sys = p_demux->p_sys;
164 for( i = 0; i < PS_TK_COUNT; i++ )
166 ps_track_t *tk = &p_sys->tk[i];
169 es_format_Clean( &tk->fmt );
170 if( tk->es ) es_out_Del( p_demux->out, tk->es );
174 ps_psm_destroy( &p_sys->psm );
179 static int Demux2( demux_t *p_demux, vlc_bool_t b_end )
181 demux_sys_t *p_sys = p_demux->p_sys;
186 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
191 else if( i_ret == 0 )
193 if( !p_sys->b_lost_sync )
194 msg_Warn( p_demux, "garbage at input, trying to resync..." );
196 p_sys->b_lost_sync = VLC_TRUE;
200 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
201 p_sys->b_lost_sync = VLC_FALSE;
203 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
207 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
209 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
210 if( !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
212 if( b_end && p_pkt->i_pts > tk->i_last_pts )
214 tk->i_last_pts = p_pkt->i_pts;
216 else if ( tk->i_first_pts == -1 )
218 tk->i_first_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 */
238 /* Check beginning */
240 i_current_pos = stream_Tell( p_demux->s );
241 while( !p_demux->b_die && i < 40 && Demux2( p_demux, VLC_FALSE ) > 0 ) i++;
244 i_size = stream_Size( p_demux->s );
245 i_end = __MAX( 0, __MIN( 200000, i_size ) );
246 stream_Seek( p_demux->s, i_size - i_end );
248 while( !p_demux->b_die && Demux2( p_demux, VLC_TRUE ) > 0 );
249 if( i_current_pos >= 0 ) stream_Seek( p_demux->s, i_current_pos );
252 for( i = 0; i < PS_TK_COUNT; i++ )
254 ps_track_t *tk = &p_sys->tk[i];
255 if( tk->i_first_pts >= 0 && tk->i_last_pts > 0 )
256 if( tk->i_last_pts > tk->i_first_pts )
258 int64_t i_length = (int64_t)tk->i_last_pts - tk->i_first_pts;
259 if( i_length > p_sys->i_length )
261 p_sys->i_length = i_length;
262 p_sys->i_time_track = i;
263 msg_Dbg( p_demux, "we found a length of: "I64Fd, p_sys->i_length );
269 /*****************************************************************************
271 *****************************************************************************/
272 static int Demux( demux_t *p_demux )
274 demux_sys_t *p_sys = p_demux->p_sys;
275 int i_ret, i_id, i_mux_rate;
279 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
284 else if( i_ret == 0 )
286 if( !p_sys->b_lost_sync )
287 msg_Warn( p_demux, "garbage at input, trying to resync..." );
289 p_sys->b_lost_sync = VLC_TRUE;
293 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
294 p_sys->b_lost_sync = VLC_FALSE;
296 if( p_sys->i_length < 0 && p_sys->b_seekable )
297 FindLength( p_demux );
299 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
307 block_Release( p_pkt );
311 if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
313 if( !p_sys->b_have_pack ) p_sys->b_have_pack = VLC_TRUE;
314 /* done later on to work around bad vcd/svcd streams */
315 /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
316 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
318 block_Release( p_pkt );
322 if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
325 for( i = 0; i < PS_TK_COUNT; i++ )
327 ps_track_t *tk = &p_sys->tk[i];
329 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
331 tk->es = es_out_Add( p_demux->out, &tk->fmt );
335 block_Release( p_pkt );
339 if( p_sys->psm.i_version == 0xFFFF )
340 msg_Dbg( p_demux, "contains a PSM");
342 ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
343 block_Release( p_pkt );
347 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
349 vlc_bool_t b_new = VLC_FALSE;
350 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
354 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
356 tk->es = es_out_Add( p_demux->out, &tk->fmt );
361 msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
363 tk->b_seen = VLC_TRUE;
366 /* The popular VCD/SVCD subtitling WinSubMux does not
367 * renumber the SCRs when merging subtitles into the PES */
369 ( tk->fmt.i_codec == VLC_FOURCC('o','g','t',' ') ||
370 tk->fmt.i_codec == VLC_FOURCC('c','v','d',' ') ) )
375 if( p_sys->i_scr > 0 )
376 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr );
380 if( tk->b_seen && tk->es &&
381 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
383 if( !b_new && !p_sys->b_have_pack && tk->fmt.i_cat == AUDIO_ES && p_pkt->i_pts > 0 )
385 /* A hack to sync the A/V on PES files. */
386 msg_Dbg( p_demux, "force SCR: "I64Fd, p_pkt->i_pts );
387 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_pkt->i_pts );
390 if( (int64_t)p_pkt->i_pts > p_sys->i_current_pts )
392 p_sys->i_current_pts = (int64_t)p_pkt->i_pts;
395 es_out_Send( p_demux->out, tk->es, p_pkt );
399 block_Release( p_pkt );
404 block_Release( p_pkt );
412 /*****************************************************************************
414 *****************************************************************************/
415 static int Control( demux_t *p_demux, int i_query, va_list args )
417 demux_sys_t *p_sys = p_demux->p_sys;
423 case DEMUX_GET_POSITION:
424 pf = (double*) va_arg( args, double* );
425 i64 = stream_Size( p_demux->s );
428 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
436 case DEMUX_SET_POSITION:
437 f = (double) va_arg( args, double );
438 i64 = stream_Size( p_demux->s );
439 p_sys->i_current_pts = 0;
440 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
442 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
445 pi64 = (int64_t*)va_arg( args, int64_t * );
446 if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
448 *pi64 = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
451 if( p_sys->i_mux_rate > 0 )
453 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
460 case DEMUX_GET_LENGTH:
461 pi64 = (int64_t*)va_arg( args, int64_t * );
462 if( p_sys->i_length > 0 )
464 *pi64 = p_sys->i_length;
467 else if( p_sys->i_mux_rate > 0 )
469 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
477 i64 = (int64_t)va_arg( args, int64_t );
478 if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
480 int64_t i_now = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
481 int64_t i_pos = stream_Tell( p_demux->s );
482 int64_t i_offset = i_pos / (i_now / 1000000) * ((i64 - i_now) / 1000000);
483 stream_Seek( p_demux->s, i_pos + i_offset);
485 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
496 /*****************************************************************************
498 *****************************************************************************/
500 /* PSResynch: resynch on a system startcode
501 * It doesn't skip more than 512 bytes
502 * -1 -> error, 0 -> not synch, 1 -> ok
504 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
506 const uint8_t *p_peek;
510 if( stream_Peek( s, &p_peek, 4 ) < 4 )
514 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
517 *pi_code = 0x100 | p_peek[3];
521 if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
533 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
536 *pi_code = 0x100 | p_peek[3];
537 return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
544 return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
547 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
549 const uint8_t *p_peek;
550 int i_peek = stream_Peek( s, &p_peek, 14 );
551 int i_size = ps_pkt_size( p_peek, i_peek );
553 if( i_size <= 6 && p_peek[3] > 0xba )
555 /* Special case, search the next start code */
559 i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
560 if( i_peek <= i_size + 4 )
564 while( i_size <= i_peek - 4 )
566 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
567 p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
569 return stream_Block( s, i_size );
578 return stream_Block( s, i_size );