-/*****************************************************************************
- * Deactivate: frees unused data
- *****************************************************************************/
-static void Deactivate( vlc_object_t *p_this )
-{
- input_thread_t *p_input = (input_thread_t *)p_this;
- demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
-
- if( p_ogg )
- {
- /* Cleanup the bitstream parser */
- ogg_sync_clear( &p_ogg->oy );
-
- Ogg_EndOfStream( p_input, p_ogg );
-
- free( p_ogg );
- }
-}
-
-/*****************************************************************************
- * Demux: reads and demuxes data packets
- *****************************************************************************
- * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
- *****************************************************************************/
-static int Demux( input_thread_t * p_input )
-{
- demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
- ogg_page oggpage;
- ogg_packet oggpacket;
- int i_stream;
-
-#define p_stream p_ogg->pp_stream[i_stream]
-
- if( p_ogg->i_eos == p_ogg->i_streams )
- {
- if( p_ogg->i_eos )
- {
- msg_Dbg( p_input, "end of a group of logical streams" );
- Ogg_EndOfStream( p_input, p_ogg );
- }
-
- if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
- p_ogg->i_eos = 0;
-
- msg_Dbg( p_input, "beginning of a group of logical streams" );
-
- p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
- input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
- }
-
- if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
- {
- msg_Warn( p_input, "synchro reinit" );
-
- if( p_ogg->i_old_synchro_state != SYNCHRO_REINIT )
- for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
- {
- /* we'll trash all the data until we find the next pcr */
- p_stream->b_reinit = 1;
- p_stream->i_pcr = -1;
- p_stream->i_interpolated_pcr = -1;
- ogg_stream_reset( &p_stream->os );
- }
- if( p_ogg->i_old_synchro_state != SYNCHRO_REINIT )
- ogg_sync_reset( &p_ogg->oy );
- }
-
- /*
- * Demux an ogg page from the stream
- */
- if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
- {
- return 0; /* EOF */
- }
-
- /* Test for End of Stream */
- if( ogg_page_eos( &oggpage ) ) p_ogg->i_eos++;
-
-
- for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
- {
- if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
- continue;
-
- while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
- {
- /* Read info from any secondary header packets, if there are any */
- if( p_stream->secondary_header_packets > 0 )
- {
- if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) &&
- oggpacket.bytes >= 7 &&
- ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
- {
- Ogg_ReadTheoraHeader( p_stream, &oggpacket );
- p_stream->secondary_header_packets = 0;
- }
- else if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) &&
- oggpacket.bytes >= 7 &&
- ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
- {
- Ogg_ReadVorbisHeader( p_stream, &oggpacket );
- p_stream->secondary_header_packets = 0;
- }
- else if ( p_stream->fmt.i_codec == VLC_FOURCC( 'c','m','m','l' ) )
- {
- p_stream->secondary_header_packets = 0;
- }
-
- p_stream->secondary_header_packets--;
- }
-
- if( p_stream->b_reinit )
- {
- /* If synchro is re-initialized we need to drop all the packets
- * until we find a new dated one. */
- Ogg_UpdatePCR( p_stream, &oggpacket );
-
- if( p_stream->i_pcr >= 0 )
- {
- p_stream->b_reinit = 0;
- }
- else
- {
- p_stream->i_interpolated_pcr = -1;
- continue;
- }
-
- /* An Ogg/vorbis packet contains an end date granulepos */
- if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
- p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
- p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
- {
- if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
- {
- Ogg_DecodePacket( p_input, p_stream, &oggpacket );
- }
- else
- {
- input_ClockManageRef( p_input,
- p_input->stream.p_selected_program,
- p_stream->i_pcr );
- }
- continue;
- }
- }
-
- Ogg_DecodePacket( p_input, p_stream, &oggpacket );
- }
- break;
- }
-
- i_stream = 0; p_ogg->i_pcr = -1;
- for( ; i_stream < p_ogg->i_streams; i_stream++ )
- {
- if( p_stream->fmt.i_cat == SPU_ES )
- continue;
- if( p_stream->i_interpolated_pcr < 0 )
- continue;
-
- if( p_ogg->i_pcr < 0 || p_stream->i_interpolated_pcr < p_ogg->i_pcr )
- p_ogg->i_pcr = p_stream->i_interpolated_pcr;
- }
-
- if( p_ogg->i_pcr >= 0 )
- {
- input_ClockManageRef( p_input, p_input->stream.p_selected_program,
- p_ogg->i_pcr );
- }
-
-#undef p_stream
-
- p_ogg->i_old_synchro_state =
- p_input->stream.p_selected_program->i_synchro_state;
-
- return 1;
-}
-
-/*****************************************************************************
- * Control:
- *****************************************************************************/
-static int Control( input_thread_t *p_input, int i_query, va_list args )
-{
- demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
- int64_t *pi64;
-
- switch( i_query )
- {
- case DEMUX_GET_TIME:
- pi64 = (int64_t*)va_arg( args, int64_t * );
- *pi64 = p_ogg->i_pcr * 100 / 9;
- return VLC_SUCCESS;
-
- default:
- return demux_vaControlDefault( p_input, i_query, args );
- }
-}
-