1 /*****************************************************************************
2 * ps.c: Program Stream demux module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2009 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * 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, TIME_TEXT,
69 set_description( N_("MPEG-PS demuxer") )
70 set_capability( "demux", 8 )
71 set_callbacks( Open, Close )
75 /*****************************************************************************
77 *****************************************************************************/
82 ps_track_t tk[PS_TK_COUNT];
89 int64_t i_current_pts;
98 static int Demux ( demux_t *p_demux );
99 static int Control( demux_t *p_demux, int i_query, va_list args );
101 static int ps_pkt_resynch( stream_t *, uint32_t *pi_code );
102 static block_t *ps_pkt_read ( stream_t *, uint32_t i_code );
104 /*****************************************************************************
106 *****************************************************************************/
107 static int OpenCommon( vlc_object_t *p_this, bool b_force )
109 demux_t *p_demux = (demux_t*)p_this;
112 const uint8_t *p_peek;
114 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
116 msg_Err( p_demux, "cannot peek" );
120 if( memcmp( p_peek, "\x00\x00\x01", 3 ) || ( p_peek[3] < 0xb9 ) )
125 msg_Warn( p_demux, "this does not look like an MPEG PS stream, "
126 "continuing anyway" );
129 /* Fill p_demux field */
130 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
131 if( !p_sys ) return VLC_ENOMEM;
133 p_demux->pf_demux = Demux;
134 p_demux->pf_control = Control;
137 p_sys->i_mux_rate = 0;
139 p_sys->i_last_scr = -1;
140 p_sys->i_length = -1;
141 p_sys->i_current_pts = (mtime_t) 0;
142 p_sys->i_time_track = -1;
143 p_sys->i_aob_mlp_count = 0;
145 p_sys->b_lost_sync = false;
146 p_sys->b_have_pack = false;
147 p_sys->b_seekable = false;
149 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
151 ps_psm_init( &p_sys->psm );
152 ps_track_init( p_sys->tk );
154 /* TODO prescanning of ES */
159 static int OpenForce( vlc_object_t *p_this )
161 return OpenCommon( p_this, true );
164 static int Open( vlc_object_t *p_this )
166 return OpenCommon( p_this, ((demux_t *)p_this)->b_force );
169 /*****************************************************************************
171 *****************************************************************************/
172 static void Close( vlc_object_t *p_this )
174 demux_t *p_demux = (demux_t*)p_this;
175 demux_sys_t *p_sys = p_demux->p_sys;
178 for( i = 0; i < PS_TK_COUNT; i++ )
180 ps_track_t *tk = &p_sys->tk[i];
183 es_format_Clean( &tk->fmt );
184 if( tk->es ) es_out_Del( p_demux->out, tk->es );
188 ps_psm_destroy( &p_sys->psm );
193 static int Demux2( demux_t *p_demux, bool b_end )
195 demux_sys_t *p_sys = p_demux->p_sys;
200 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
205 else if( i_ret == 0 )
207 if( !p_sys->b_lost_sync )
208 msg_Warn( p_demux, "garbage at input, trying to resync..." );
210 p_sys->b_lost_sync = true;
214 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
215 p_sys->b_lost_sync = false;
217 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
221 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
223 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
224 if( !ps_pkt_parse_pes( p_pkt, tk->i_skip ) && p_pkt->i_pts > VLC_TS_INVALID )
226 if( b_end && p_pkt->i_pts > tk->i_last_pts )
228 tk->i_last_pts = p_pkt->i_pts;
230 else if ( tk->i_first_pts == -1 )
232 tk->i_first_pts = p_pkt->i_pts;
236 block_Release( p_pkt );
240 static void FindLength( demux_t *p_demux )
242 demux_sys_t *p_sys = p_demux->p_sys;
243 int64_t i_current_pos = -1, i_size = 0, i_end = 0;
245 if( !var_CreateGetBool( p_demux, "ps-trust-timestamps" ) )
248 if( p_sys->i_length == -1 ) /* First time */
251 /* Check beginning */
253 i_current_pos = stream_Tell( p_demux->s );
254 while( vlc_object_alive (p_demux) && i < 40 && Demux2( p_demux, false ) > 0 ) i++;
257 i_size = stream_Size( p_demux->s );
258 i_end = VLC_CLIP( i_size, 0, 200000 );
259 stream_Seek( p_demux->s, i_size - i_end );
262 while( vlc_object_alive (p_demux) && i < 400 && Demux2( p_demux, true ) > 0 ) i++;
263 if( i_current_pos >= 0 ) stream_Seek( p_demux->s, i_current_pos );
266 /* Find the longest track */
267 for( int i = 0; i < PS_TK_COUNT; i++ )
269 ps_track_t *tk = &p_sys->tk[i];
270 if( tk->i_last_pts > 0 &&
271 tk->i_last_pts > tk->i_first_pts )
273 int64_t i_length = (int64_t)tk->i_last_pts - tk->i_first_pts;
274 if( i_length > p_sys->i_length )
276 p_sys->i_length = i_length;
277 p_sys->i_time_track = i;
278 msg_Dbg( p_demux, "we found a length of: %"PRId64 "s", p_sys->i_length / CLOCK_FREQ );
284 /*****************************************************************************
286 *****************************************************************************/
287 static int Demux( demux_t *p_demux )
289 demux_sys_t *p_sys = p_demux->p_sys;
290 int i_ret, i_id, i_mux_rate;
294 i_ret = ps_pkt_resynch( p_demux->s, &i_code );
299 else if( i_ret == 0 )
301 if( !p_sys->b_lost_sync )
302 msg_Warn( p_demux, "garbage at input, trying to resync..." );
304 p_sys->b_lost_sync = true;
308 if( p_sys->b_lost_sync ) msg_Warn( p_demux, "found sync code" );
309 p_sys->b_lost_sync = false;
311 if( p_sys->i_length < 0 && p_sys->b_seekable )
312 FindLength( p_demux );
314 if( ( p_pkt = ps_pkt_read( p_demux->s, i_code ) ) == NULL )
322 block_Release( p_pkt );
326 if( !ps_pkt_parse_pack( p_pkt, &p_sys->i_scr, &i_mux_rate ) )
328 p_sys->i_last_scr = p_sys->i_scr;
329 if( !p_sys->b_have_pack ) p_sys->b_have_pack = true;
330 /* done later on to work around bad vcd/svcd streams */
331 /* es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_scr ); */
332 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
334 block_Release( p_pkt );
338 if( !ps_pkt_parse_system( p_pkt, &p_sys->psm, p_sys->tk ) )
341 for( i = 0; i < PS_TK_COUNT; i++ )
343 ps_track_t *tk = &p_sys->tk[i];
345 if( tk->b_seen && !tk->es && tk->fmt.i_cat != UNKNOWN_ES )
347 tk->es = es_out_Add( p_demux->out, &tk->fmt );
351 block_Release( p_pkt );
355 if( p_sys->psm.i_version == 0xFFFF )
356 msg_Dbg( p_demux, "contains a PSM");
358 ps_psm_fill( &p_sys->psm, p_pkt, p_sys->tk, p_demux->out );
359 block_Release( p_pkt );
363 if( (i_id = ps_pkt_id( p_pkt )) >= 0xc0 )
365 /* Small heuristic to improve MLP detection from AOB */
366 if( i_id == 0xa001 &&
367 p_sys->i_aob_mlp_count < 500 )
369 p_sys->i_aob_mlp_count++;
371 else if( i_id == 0xbda1 &&
372 p_sys->i_aob_mlp_count > 0 )
374 p_sys->i_aob_mlp_count--;
379 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
383 if( !ps_track_fill( tk, &p_sys->psm, i_id, p_pkt ) )
385 tk->es = es_out_Add( p_demux->out, &tk->fmt );
390 msg_Dbg( p_demux, "es id=0x%x format unknown", i_id );
395 /* The popular VCD/SVCD subtitling WinSubMux does not
396 * renumber the SCRs when merging subtitles into the PES */
398 ( tk->fmt.i_codec == VLC_CODEC_OGT ||
399 tk->fmt.i_codec == VLC_CODEC_CVD ) )
402 p_sys->i_last_scr = -1;
405 if( p_sys->i_scr >= 0 )
406 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_scr );
410 if( tk->b_seen && tk->es &&
411 !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
413 if( !b_new && !p_sys->b_have_pack &&
414 (tk->fmt.i_cat == AUDIO_ES) &&
415 (p_pkt->i_pts > VLC_TS_INVALID) )
417 /* A hack to sync the A/V on PES files. */
418 msg_Dbg( p_demux, "force SCR: %"PRId64, p_pkt->i_pts );
419 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_pkt->i_pts );
421 if( tk->fmt.i_codec == VLC_CODEC_TELETEXT &&
422 p_pkt->i_pts <= VLC_TS_INVALID && p_sys->i_last_scr >= 0 )
424 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
425 * In this case use the last SCR + 40ms */
426 p_pkt->i_pts = VLC_TS_0 + p_sys->i_last_scr + 40000;
429 if( (int64_t)p_pkt->i_pts > p_sys->i_current_pts )
431 p_sys->i_current_pts = (int64_t)p_pkt->i_pts;
434 es_out_Send( p_demux->out, tk->es, p_pkt );
438 block_Release( p_pkt );
443 block_Release( p_pkt );
448 demux_UpdateTitleFromStream( p_demux );
452 /*****************************************************************************
454 *****************************************************************************/
455 static int Control( demux_t *p_demux, int i_query, va_list args )
457 demux_sys_t *p_sys = p_demux->p_sys;
463 case DEMUX_GET_POSITION:
464 pf = (double*) va_arg( args, double* );
465 i64 = stream_Size( p_demux->s );
468 double current = stream_Tell( p_demux->s );
469 *pf = current / (double)i64;
477 case DEMUX_SET_POSITION:
478 f = (double) va_arg( args, double );
479 i64 = stream_Size( p_demux->s );
480 p_sys->i_current_pts = 0;
481 p_sys->i_last_scr = -1;
483 return stream_Seek( p_demux->s, (int64_t)(i64 * f) );
486 pi64 = (int64_t*)va_arg( args, int64_t * );
487 if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
489 *pi64 = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
492 if( p_sys->i_mux_rate > 0 )
494 *pi64 = (int64_t)1000000 * ( stream_Tell( p_demux->s ) / 50 ) /
501 case DEMUX_GET_LENGTH:
502 pi64 = (int64_t*)va_arg( args, int64_t * );
503 if( p_sys->i_length > 0 )
505 *pi64 = p_sys->i_length;
508 else if( p_sys->i_mux_rate > 0 )
510 *pi64 = (int64_t)1000000 * ( stream_Size( p_demux->s ) / 50 ) /
518 i64 = (int64_t)va_arg( args, int64_t );
519 if( p_sys->i_time_track >= 0 && p_sys->i_current_pts > 0 )
521 int64_t i_now = p_sys->i_current_pts - p_sys->tk[p_sys->i_time_track].i_first_pts;
522 int64_t i_pos = stream_Tell( p_demux->s );
525 return i64 ? VLC_EGENERIC : VLC_SUCCESS;
527 p_sys->i_current_pts = 0;
528 p_sys->i_last_scr = -1;
529 i_pos *= (float)i64 / (float)i_now;
530 stream_Seek( p_demux->s, i_pos );
535 case DEMUX_GET_TITLE_INFO:
537 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
538 int *c = va_arg( args, int * );
540 *va_arg( args, int* ) = 0; /* Title offset */
541 *va_arg( args, int* ) = 0; /* Chapter offset */
542 return stream_Control( p_demux->s, STREAM_GET_TITLE_INFO, v, c );
545 case DEMUX_SET_TITLE:
546 return stream_vaControl( p_demux->s, STREAM_SET_TITLE, args );
548 case DEMUX_SET_SEEKPOINT:
549 return stream_vaControl( p_demux->s, STREAM_SET_SEEKPOINT, args );
552 return stream_vaControl( p_demux->s, STREAM_GET_META, args );
560 /*****************************************************************************
562 *****************************************************************************/
564 /* PSResynch: resynch on a system startcode
565 * It doesn't skip more than 512 bytes
566 * -1 -> error, 0 -> not synch, 1 -> ok
568 static int ps_pkt_resynch( stream_t *s, uint32_t *pi_code )
570 const uint8_t *p_peek;
574 if( stream_Peek( s, &p_peek, 4 ) < 4 )
578 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
581 *pi_code = 0x100 | p_peek[3];
585 if( ( i_peek = stream_Peek( s, &p_peek, 512 ) ) < 4 )
597 if( p_peek[0] == 0 && p_peek[1] == 0 && p_peek[2] == 1 &&
600 *pi_code = 0x100 | p_peek[3];
601 return stream_Read( s, NULL, i_skip ) == i_skip ? 1 : -1;
608 return stream_Read( s, NULL, i_skip ) == i_skip ? 0 : -1;
611 static block_t *ps_pkt_read( stream_t *s, uint32_t i_code )
613 const uint8_t *p_peek;
614 int i_peek = stream_Peek( s, &p_peek, 14 );
618 int i_size = ps_pkt_size( p_peek, i_peek );
619 if( i_size <= 6 && p_peek[3] > 0xba )
621 /* Special case, search the next start code */
625 i_peek = stream_Peek( s, &p_peek, i_size + 1024 );
626 if( i_peek <= i_size + 4 )
630 while( i_size <= i_peek - 4 )
632 if( p_peek[i_size] == 0x00 && p_peek[i_size+1] == 0x00 &&
633 p_peek[i_size+2] == 0x01 && p_peek[i_size+3] >= 0xb9 )
635 return stream_Block( s, i_size );
644 return stream_Block( s, i_size );