* mpeg_system.c: TS, PS and PES management
*****************************************************************************
* Copyright (C) 1998-2001 VideoLAN
- * $Id: mpeg_system.c,v 1.81 2002/03/01 00:33:18 massiot Exp $
+ * $Id: mpeg_system.c,v 1.91 2002/04/23 14:16:20 sam Exp $
*
* Authors: Christophe Massiot <massiot@via.ecp.fr>
* Michel Lespinasse <walken@via.ecp.fr>
if( p_data->p_payload_end - p_data->p_payload_start
>= PES_HEADER_SIZE )
{
- p_es->i_pes_real_size =
- U16_AT(p_data->p_payload_start + 4) + 6;
-
+ p_es->i_pes_real_size = ((u16)p_data->p_payload_start[4] << 8)
+ + p_data->p_payload_start[5] + 6;
}
else
{
vlc_mutex_unlock( &p_input->stream.stream_lock );
}
+/*****************************************************************************
+ * input_ReadPS: store a PS packet into a data_buffer_t
+ *****************************************************************************/
+#define PEEK( SIZE ) \
+ i_error = input_Peek( p_input, &p_peek, SIZE ); \
+ if( i_error == -1 ) \
+ { \
+ return( -1 ); \
+ } \
+ else if( i_error < SIZE ) \
+ { \
+ /* EOF */ \
+ return( 0 ); \
+ }
+
+ssize_t input_ReadPS( input_thread_t * p_input, data_packet_t ** pp_data )
+{
+ byte_t * p_peek;
+ size_t i_packet_size;
+ ssize_t i_error, i_read;
+
+ /* Read what we believe to be a packet header. */
+ PEEK( 4 );
+
+ if( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
+ {
+ if( p_peek[0] || p_peek[1] || p_peek[2] )
+ {
+ /* It is common for MPEG-1 streams to pad with zeros
+ * (although it is forbidden by the recommendation), so
+ * don't bother everybody in this case. */
+ intf_WarnMsg( 3, "input warning: garbage (0x%.2x%.2x%.2x%.2x)",
+ p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
+ }
+
+ /* This is not the startcode of a packet. Read the stream
+ * until we find one. */
+ while( p_peek[0] || p_peek[1] || p_peek[2] != 1 || p_peek[3] < 0xB9 )
+ {
+ p_input->p_current_data++;
+ PEEK( 4 );
+ }
+ /* Packet found. */
+ }
+
+ /* 0x1B9 == SYSTEM_END_CODE, it is only 4 bytes long. */
+ if( p_peek[3] != 0xB9 )
+ {
+ /* The packet is at least 6 bytes long. */
+ PEEK( 6 );
+
+ if( p_peek[3] != 0xBA )
+ {
+ /* That's the case for all packets, except pack header. */
+ i_packet_size = (p_peek[4] << 8) | p_peek[5];
+ }
+ else
+ {
+ /* Pack header. */
+ if( (p_peek[4] & 0xC0) == 0x40 )
+ {
+ /* MPEG-2 */
+ i_packet_size = 8;
+ }
+ else if( (p_peek[4] & 0xF0) == 0x20 )
+ {
+ /* MPEG-1 */
+ i_packet_size = 6;
+ }
+ else
+ {
+ intf_ErrMsg( "Unable to determine stream type" );
+ return( -1 );
+ }
+ }
+ }
+ else
+ {
+ /* System End Code */
+ i_packet_size = -2;
+ }
+
+ /* Fetch a packet of the appropriate size. */
+ i_read = input_SplitBuffer( p_input, pp_data, i_packet_size + 6 );
+ if( i_read <= 0 )
+ {
+ return( i_read );
+ }
+
+ /* In MPEG-2 pack headers we still have to read stuffing bytes. */
+ if( ((*pp_data)->p_demux_start[3] == 0xBA) && (i_packet_size == 8) )
+ {
+ size_t i_stuffing = ((*pp_data)->p_demux_start[13] & 0x7);
+ /* Force refill of the input buffer - though we don't care
+ * about p_peek. Please note that this is unoptimized. */
+ PEEK( i_stuffing );
+ p_input->p_current_data += i_stuffing;
+ }
+
+ return( 1 );
+}
+
+#undef PEEK
+
/*****************************************************************************
* input_ParsePS: read the PS header
*****************************************************************************/
p_es->i_cat = AUDIO_ES;
#ifdef AUTO_SPAWN
if( !p_input->stream.b_seekable )
- if( config_GetIntVariable( INPUT_CHANNEL_VAR )
+ if( config_GetIntVariable( "audio-channel" )
== (p_es->i_id & 0x1F) ||
- ( config_GetIntVariable( INPUT_CHANNEL_VAR ) < 0
+ ( config_GetIntVariable( "audio-channel" ) < 0
&& !(p_es->i_id & 0x1F) ) )
- switch( config_GetIntVariable( INPUT_AUDIO_VAR ) )
+ switch( config_GetIntVariable( "audio-type" ) )
{
case -1:
case REQUESTED_MPEG:
p_es->i_cat = AUDIO_ES;
#ifdef AUTO_SPAWN
if( !p_input->stream.b_seekable )
- if( config_GetIntVariable( INPUT_CHANNEL_VAR )
+ if( config_GetIntVariable( "audio-channel" )
== ((p_es->i_id & 0xF00) >> 8) ||
- ( config_GetIntVariable( INPUT_CHANNEL_VAR ) < 0
+ ( config_GetIntVariable( "audio-channel" ) < 0
&& !((p_es->i_id & 0xF00) >> 8)) )
- switch( config_GetIntVariable( INPUT_AUDIO_VAR ) )
+ switch( config_GetIntVariable( "audio-type" ) )
{
case -1:
case REQUESTED_AC3:
p_es->i_type = DVD_SPU_ES;
p_es->i_cat = SPU_ES;
#ifdef AUTO_SPAWN
- if( config_GetIntVariable( INPUT_SUBTITLE_VAR )
+ if( config_GetIntVariable( "spu-channel" )
== ((p_es->i_id & 0x1F00) >> 8) )
{
if( !p_input->stream.b_seekable )
i_mux_rate = (U32_AT(p_header + 8) & 0x7FFFFE) >> 1;
}
/* Call the pace control. */
- input_ClockManageRef( p_input, p_input->stream.pp_programs[0],
+ input_ClockManageRef( p_input,
+ p_input->stream.p_selected_program,
scr_time );
if( i_mux_rate != p_input->stream.i_mux_rate
* TS Demultiplexing
*/
+/*****************************************************************************
+ * input_ReadTS: store a TS packet into a data_buffer_t
+ *****************************************************************************/
+#define PEEK( SIZE ) \
+ i_error = input_Peek( p_input, &p_peek, SIZE ); \
+ if( i_error == -1 ) \
+ { \
+ return( -1 ); \
+ } \
+ else if( i_error < SIZE ) \
+ { \
+ /* EOF */ \
+ return( 0 ); \
+ }
+
+ssize_t input_ReadTS( input_thread_t * p_input, data_packet_t ** pp_data )
+{
+ byte_t * p_peek;
+ ssize_t i_error, i_read;
+
+ PEEK( 1 );
+
+ if( *p_peek != TS_SYNC_CODE )
+ {
+ intf_WarnMsg( 3, "input warning: garbage at input (%x)", *p_peek );
+
+ if( p_input->i_mtu )
+ {
+ while( *p_peek != TS_SYNC_CODE )
+ {
+ /* Try to resync on next packet. */
+ PEEK( TS_PACKET_SIZE );
+ p_input->p_current_data += TS_PACKET_SIZE;
+ PEEK( 1 );
+ }
+ }
+ else
+ {
+ /* Move forward until we find 0x47 (and hope it's the good
+ * one... FIXME) */
+ while( *p_peek != TS_SYNC_CODE )
+ {
+ p_input->p_current_data++;
+ PEEK( 1 );
+ }
+ }
+ }
+
+ i_read = input_SplitBuffer( p_input, pp_data, TS_PACKET_SIZE );
+ if( i_read <= 0 )
+ {
+ return( i_read );
+ }
+
+ return( 1 );
+}
+
/*****************************************************************************
* input_DemuxTS: first step of demultiplexing: the TS header
*****************************************************************************/
p_stream_data->i_pat_version = p_psi->i_version_number;
}
-#undef p_psi
+#undef p_psi
- /* FIXME This has nothing to do here */
- p_input->stream.p_selected_program = p_input->stream.pp_programs[0] ;
}
/*****************************************************************************
int i_section_length,i_current_section;
int i_prog_info_length, i_loop;
int i_es_info_length, i_pid, i_stream_type;
- int i_audio_es, i_spu_es;
- int i_required_audio_es, i_required_spu_es;
p_current_section = p_psi->buffer;
p_current_data = p_psi->buffer;
p_pgrm_data->i_pcr_pid = ( ((u32)*(p_current_section + 8) & 0x1F) << 8 ) |
*(p_current_section + 9);
- i_audio_es = 0;
- i_spu_es = 0;
/* Lock stream information */
vlc_mutex_lock( &p_input->stream.stream_lock );
- /* Get the number of the required audio stream */
- if( p_main->b_audio )
- {
- /* Default is the first one */
- i_required_audio_es = config_GetIntVariable( INPUT_CHANNEL_VAR );
- if( i_required_audio_es < 0 )
- {
- i_required_audio_es = 1;
- }
- }
- else
- {
- i_required_audio_es = 0;
- }
-
- /* Same thing for subtitles */
- if( p_main->b_video )
- {
- /* for spu, default is none */
- i_required_spu_es = config_GetIntVariable( INPUT_SUBTITLE_VAR );
- if( i_required_spu_es < 0 )
- {
- i_required_spu_es = 0;
- }
- }
- else
- {
- i_required_spu_es = 0;
- }
-
/* Delete all ES in this program except the PSI. We start from the
* end because i_es_number gets decremented after each deletion. */
for( i_loop = p_es->p_pgrm->i_es_number ; i_loop ; )
/* Tell the interface what kind of stream it is and select
* the required ones */
- switch( i_stream_type )
{
- case MPEG1_VIDEO_ES:
- case MPEG2_VIDEO_ES:
- p_new_es->i_cat = VIDEO_ES;
- input_SelectES( p_input, p_new_es );
- break;
- case MPEG1_AUDIO_ES:
- case MPEG2_AUDIO_ES:
- p_new_es->i_cat = AUDIO_ES;
- i_audio_es += 1;
- if( i_audio_es == i_required_audio_es )
- input_SelectES( p_input, p_new_es );
- break;
- case LPCM_AUDIO_ES :
- case AC3_AUDIO_ES :
- p_new_es->i_stream_id = 0xBD;
- p_new_es->i_cat = AUDIO_ES;
- i_audio_es += 1;
- if( i_audio_es == i_required_audio_es )
- input_SelectES( p_input, p_new_es );
- break;
- /* Not sure this one is fully specification-compliant */
- case DVD_SPU_ES :
- p_new_es->i_stream_id = 0xBD;
- p_new_es->i_cat = SPU_ES;
- i_spu_es += 1;
- if( i_spu_es == i_required_spu_es )
- input_SelectES( p_input, p_new_es );
- break;
- default :
- p_new_es->i_cat = UNKNOWN_ES;
- break;
+ switch( i_stream_type )
+ {
+ case MPEG1_VIDEO_ES:
+ case MPEG2_VIDEO_ES:
+ p_new_es->i_cat = VIDEO_ES;
+ break;
+ case MPEG1_AUDIO_ES:
+ case MPEG2_AUDIO_ES:
+ p_new_es->i_cat = AUDIO_ES;
+ break;
+ case LPCM_AUDIO_ES :
+ case AC3_AUDIO_ES :
+ p_new_es->i_stream_id = 0xBD;
+ p_new_es->i_cat = AUDIO_ES;
+ break;
+ /* Not sure this one is fully specification-compliant */
+ case DVD_SPU_ES :
+ p_new_es->i_stream_id = 0xBD;
+ p_new_es->i_cat = SPU_ES;
+ break;
+ default :
+ p_new_es->i_cat = UNKNOWN_ES;
+ break;
+ }
}
p_current_data += 5 + i_es_info_length;
} while( i_current_section < p_psi->i_last_section_number );
- if( i_required_audio_es > i_audio_es )
- {
- intf_WarnMsg( 2, "input: non-existing audio ES required" );
- }
-
- if( i_required_spu_es > i_spu_es )
+ p_pgrm_data->i_pmt_version = p_psi->i_version_number;
+
+ /* if no program is selected :*/
+ if( !p_input->stream.p_selected_program )
{
- intf_WarnMsg( 2, "input: non-existing subtitles ES required" );
+ pgrm_descriptor_t * p_pgrm_to_select;
+ u16 i_id = (u16)config_GetIntVariable( "program" );
+
+ if( i_id != 0 ) /* if user specified a program */
+ {
+ p_pgrm_to_select = input_FindProgram( p_input, i_id );
+
+ if( p_pgrm_to_select || p_pgrm_to_select == p_es->p_pgrm )
+ p_input->pf_set_program( p_input, p_pgrm_to_select );
+ }
+ else
+ p_input->pf_set_program( p_input, p_es->p_pgrm );
}
- p_pgrm_data->i_pmt_version = p_psi->i_version_number;
-
/* inform interface that stream has changed */
p_input->stream.b_changed = 1;
/* Remove lock */