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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
39 * - re-add pre-scanning.
43 #define TIME_TEXT N_("Trust MPEG timestamps")
44 #define TIME_LONGTEXT N_("Normally we use the timestamps of the MPEG files " \
45 "to calculate position and duration. However sometimes this might not " \
46 "be usable. Disable this option to calculate from the bitrate instead." )
48 /*****************************************************************************
50 *****************************************************************************/
51 static int OpenForce( vlc_object_t * );
52 static int Open ( vlc_object_t * );
53 static void Close ( vlc_object_t * );
56 set_description( N_("MPEG-PS demuxer") )
57 set_shortname( N_("PS") )
58 set_category( CAT_INPUT )
59 set_subcategory( SUBCAT_INPUT_DEMUX )
60 set_capability( "demux", 1 )
61 set_callbacks( OpenForce, Close )
64 add_bool( "ps-trust-timestamps", true, NULL, TIME_TEXT,
68 set_description( N_("MPEG-PS demuxer") )
69 set_capability( "demux", 8 )
70 set_callbacks( Open, Close )
74 /*****************************************************************************
76 *****************************************************************************/
81 ps_track_t tk[PS_TK_COUNT];
87 int64_t i_current_pts;
94 static int Demux ( demux_t *p_demux );
95 static int Control( demux_t *p_demux, int i_query, va_list args );
97 static int ps_pkt_resynch( stream_t *, uint32_t *pi_code );
98 static block_t *ps_pkt_read ( stream_t *, uint32_t i_code );
100 /*****************************************************************************
102 *****************************************************************************/
103 static int OpenCommon( vlc_object_t *p_this, bool b_force )
105 demux_t *p_demux = (demux_t*)p_this;
108 const uint8_t *p_peek;
110 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
112 msg_Err( p_demux, "cannot peek" );
116 if( memcmp( p_peek, "\x00\x00\x01", 3 ) || ( p_peek[3] < 0xb9 ) )
121 msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
122 "continuing anyway" );
125 /* Fill p_demux field */
126 p_demux->pf_demux = Demux;
127 p_demux->pf_control = Control;
128 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
129 if( !p_sys ) return VLC_ENOMEM;
132 p_sys->i_mux_rate = 0;
134 p_sys->i_length = -1;
135 p_sys->i_current_pts = (mtime_t) 0;
136 p_sys->i_time_track = -1;
138 p_sys->b_lost_sync = false;
139 p_sys->b_have_pack = false;
140 p_sys->b_seekable = false;
142 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
144 ps_psm_init( &p_sys->psm );
145 ps_track_init( p_sys->tk );
147 /* TODO prescanning of ES */
152 static int OpenForce( vlc_object_t *p_this )
154 return OpenCommon( p_this, true );
157 static int Open( vlc_object_t *p_this )
159 return OpenCommon( p_this, ((demux_t *)p_this)->b_force );
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, bool b_end )
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 = true;
207 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
208 p_sys->b_lost_sync = 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 if( b_end && p_pkt->i_pts > tk->i_last_pts )
221 tk->i_last_pts = p_pkt->i_pts;
223 else if ( tk->i_first_pts == -1 )
225 tk->i_first_pts = p_pkt->i_pts;
229 block_Release( p_pkt );
233 static void FindLength( demux_t *p_demux )
235 demux_sys_t *p_sys = p_demux->p_sys;
236 int64_t i_current_pos = -1, i_size = 0, i_end = 0;
239 if( !var_CreateGetInteger( p_demux, "ps-trust-timestamps" ) )
242 if( p_sys->i_length == -1 ) /* First time */
245 /* Check beginning */
247 i_current_pos = stream_Tell( p_demux->s );
248 while( vlc_object_alive (p_demux) && i < 40 && Demux2( p_demux, false ) > 0 ) i++;
251 i_size = stream_Size( p_demux->s );
252 i_end = __MAX( 0, __MIN( 200000, i_size ) );
253 stream_Seek( p_demux->s, i_size - i_end );
256 while( vlc_object_alive (p_demux) && i < 40 && Demux2( p_demux, true ) > 0 );
257 if( i_current_pos >= 0 ) stream_Seek( p_demux->s, i_current_pos );
260 for( i = 0; i < PS_TK_COUNT; i++ )
262 ps_track_t *tk = &p_sys->tk[i];
263 if( tk->i_first_pts >= 0 && tk->i_last_pts > 0 )
264 if( tk->i_last_pts > tk->i_first_pts )
266 int64_t i_length = (int64_t)tk->i_last_pts - tk->i_first_pts;
267 if( i_length > p_sys->i_length )
269 p_sys->i_length = i_length;
270 p_sys->i_time_track = i;
271 msg_Dbg( p_demux, "we found a length of: %"PRId64, p_sys->i_length );
277 /*****************************************************************************
279 *****************************************************************************/
280 static int Demux( demux_t *p_demux )
282 demux_sys_t *p_sys = p_demux->p_sys;
283 int i_ret, i_id, i_mux_rate;
287 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
292 else if( i_ret == 0 )
294 if( !p_sys->b_lost_sync )
295 msg_Warn( p_demux, "garbage at input, trying to resync..." );
297 p_sys->b_lost_sync = true;
301 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
302 p_sys->b_lost_sync = false;
304 if( p_sys->i_length < 0 && p_sys->b_seekable )
305 FindLength( p_demux );
307 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
315 block_Release( p_pkt );
319 if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
321 if( !p_sys->b_have_pack ) p_sys->b_have_pack = true;
322 /* done later on to work around bad vcd/svcd streams */
323 /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
324 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
326 block_Release( p_pkt );
330 if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
333 for( i = 0; i < PS_TK_COUNT; i++ )
335 ps_track_t *tk = &p_sys->tk[i];
337 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
339 tk->es = es_out_Add( p_demux->out, &tk->fmt );
343 block_Release( p_pkt );
347 if( p_sys->psm.i_version == 0xFFFF )
348 msg_Dbg( p_demux, "contains a PSM");
350 ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
351 block_Release( p_pkt );
355 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
358 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
362 if( !ps_track_fill( tk, &p_sys->psm, i_id ) )
364 tk->es = es_out_Add( p_demux->out, &tk->fmt );
369 msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
374 /* The popular VCD/SVCD subtitling WinSubMux does not
375 * renumber the SCRs when merging subtitles into the PES */
377 ( tk->fmt.i_codec == VLC_FOURCC('o','g','t',' ') ||
378 tk->fmt.i_codec == VLC_FOURCC('c','v','d',' ') ) )
383 if( p_sys->i_scr > 0 )
384 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr );
388 if( tk->b_seen && tk->es &&
390 #ifdef ZVBI_COMPILED /* FIXME!! */
391 tk->fmt.i_codec == VLC_FOURCC('t','e','l','x') ||
393 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) ) )
395 if( !b_new && !p_sys->b_have_pack &&
396 (tk->fmt.i_cat == AUDIO_ES) &&
399 /* A hack to sync the A/V on PES files. */
400 msg_Dbg( p_demux, "force SCR: %"PRId64, p_pkt->i_pts );
401 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_pkt->i_pts );
404 if( (int64_t)p_pkt->i_pts > p_sys->i_current_pts )
406 p_sys->i_current_pts = (int64_t)p_pkt->i_pts;
409 es_out_Send( p_demux->out, tk->es, p_pkt );
413 block_Release( p_pkt );
418 block_Release( p_pkt );
426 /*****************************************************************************
428 *****************************************************************************/
429 static int Control( demux_t *p_demux, int i_query, va_list args )
431 demux_sys_t *p_sys = p_demux->p_sys;
437 case DEMUX_GET_POSITION:
438 pf = (double*) va_arg( args, double* );
439 i64 = stream_Size( p_demux->s );
442 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
450 case DEMUX_SET_POSITION:
451 f = (double) va_arg( args, double );
452 i64 = stream_Size( p_demux->s );
453 p_sys->i_current_pts = 0;
455 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
458 pi64 = (int64_t*)va_arg( args, int64_t * );
459 if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
461 *pi64 = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
464 if( p_sys->i_mux_rate > 0 )
466 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
473 case DEMUX_GET_LENGTH:
474 pi64 = (int64_t*)va_arg( args, int64_t * );
475 if( p_sys->i_length > 0 )
477 *pi64 = p_sys->i_length;
480 else if( p_sys->i_mux_rate > 0 )
482 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
490 i64 = (int64_t)va_arg( args, int64_t );
491 if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
493 int64_t i_now = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
494 int64_t i_pos = stream_Tell( p_demux->s );
497 return i64 ? VLC_EGENERIC : VLC_SUCCESS;
499 i_pos *= (float)i64 / (float)i_now;
500 stream_Seek( p_demux->s, i_pos );
511 /*****************************************************************************
513 *****************************************************************************/
515 /* PSResynch: resynch on a system startcode
516 * It doesn't skip more than 512 bytes
517 * -1 -> error, 0 -> not synch, 1 -> ok
519 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
521 const uint8_t *p_peek;
525 if( stream_Peek( s, &p_peek, 4 ) < 4 )
529 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
532 *pi_code = 0x100 | p_peek[3];
536 if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
548 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
551 *pi_code = 0x100 | p_peek[3];
552 return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
559 return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
562 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
564 const uint8_t *p_peek;
565 int i_peek = stream_Peek( s, &p_peek, 14 );
569 /* Smallest valid packet */
570 if( i_peek < 6 ) return NULL;
572 i_size = ps_pkt_size( p_peek, i_peek );
574 if( i_size < 0 || ( i_size <= 6 && p_peek[3] > 0xba ) )
576 /* Special case, search the next start code */
580 i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
581 if( i_peek <= i_size + 4 )
585 while( i_size <= i_peek - 4 )
587 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
588 p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
590 return stream_Block( s, i_size );
599 return stream_Block( s, i_size );