#include "vorbis.h"
#include "kate_categories.h"
#include "ogg.h"
+#include "oggseek.h"
/*****************************************************************************
* Module descriptor
} sh;
} stream_header_t;
-#define OGG_BLOCK_SIZE 4096
/* Some defines from OggDS */
#define PACKET_TYPE_HEADER 0x01
static int Demux( demux_t * p_demux )
{
demux_sys_t *p_sys = p_demux->p_sys;
- ogg_page oggpage;
ogg_packet oggpacket;
int i_stream;
+ bool b_skipping = false;
if( p_sys->i_eos == p_sys->i_streams )
/*
* Demux an ogg page from the stream
*/
- if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
+ if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
return 0; /* EOF */
/* Test for End of Stream */
- if( ogg_page_eos( &oggpage ) )
+ if( ogg_page_eos( &p_sys->current_page ) )
p_sys->i_eos++;
}
if( !p_sys->b_page_waiting )
{
if( p_sys->i_streams == 1 &&
- ogg_page_serialno( &oggpage ) != p_stream->os.serialno )
+ ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
{
msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
- ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &oggpage ) );
+ ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
p_stream->b_reinit = true;
p_stream->i_pcr = -1;
es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
}
- if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
+ if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
+ {
continue;
+ }
+
}
while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
{
p_stream->i_secondary_header_packets = 0;
}
+
+ /* update start of data pointer */
+ p_stream->i_data_start = stream_Tell( p_demux->s );
+
}
+ /* If any streams have i_skip_frames, only decode (pre-roll)
+ * for those streams */
+ if ( b_skipping && p_stream->i_skip_frames == 0 ) continue;
+
+
if( p_stream->b_reinit )
{
/* If synchro is re-initialized we need to drop all the packets
p_sys->i_pcr = p_stream->i_interpolated_pcr;
}
- if( p_sys->i_pcr >= 0 )
+ if( p_sys->i_pcr >= 0 && ! b_skipping )
es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
return 1;
while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
{
- p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
+ p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
- i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
+ i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
if( i_read <= 0 )
return VLC_EGENERIC;
( iframe << p_stream->i_granule_shift );
p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
- * INT64_C(1000000) / p_stream->f_rate;
+ * INT64_C(1000000) / p_stream->f_rate;
}
else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
{
}
else
{
+#warning Memory leak
p_stream->i_headers = 0;
p_stream->p_headers = NULL;
free( p_org );
if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
+
+ /* may need to preroll video frames after a seek */
+ if ( p_stream->i_skip_frames > 0 )
+ {
+ p_block->i_flags |= BLOCK_FLAG_PREROLL;
+ p_stream->i_skip_frames--;
+ }
+
+
/* Normalize PTS */
if( i_pts == 0 ) i_pts = VLC_TS_0;
else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = VLC_TS_0;
p_block->i_dts = p_stream->i_pcr;
p_block->i_pts = VLC_TS_INVALID;
/* NB, OggDirac granulepos values are in units of 2*picturerate */
+
+ /* granulepos for dirac is possibly broken, this value should be ignored */
if( -1 != p_oggpacket->granulepos )
p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
}
{
demux_sys_t *p_ogg = p_demux->p_sys ;
ogg_packet oggpacket;
- ogg_page oggpage;
int i_stream;
- while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
+ p_ogg->i_total_length = stream_Size ( p_demux->s );
+ msg_Dbg( p_demux, "File length is %"PRId64" bytes", p_ogg->i_total_length );
+
+
+ while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
{
- if( ogg_page_bos( &oggpage ) )
+
+ if( ogg_page_bos( &p_ogg->current_page ) )
{
/* All is wonderful in our fine fine little world.
* We found the beginning of our first logical stream. */
- while( ogg_page_bos( &oggpage ) )
+ while( ogg_page_bos( &p_ogg->current_page ) )
{
logical_stream_t *p_stream;
p_stream->i_secondary_header_packets = 0;
p_stream->i_keyframe_offset = 0;
+ p_stream->i_skip_frames = 0;
+
+ p_stream->i_data_start = 0;
es_format_Init( &p_stream->fmt, 0, 0 );
es_format_Init( &p_stream->fmt_old, 0, 0 );
/* Setup the logical stream */
- p_stream->i_serial_no = ogg_page_serialno( &oggpage );
+ p_stream->i_serial_no = ogg_page_serialno( &p_ogg->current_page );
ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
/* Extract the initial header from the first page and verify
* the codec type of this Ogg bitstream */
- if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
+ if( ogg_stream_pagein( &p_stream->os, &p_ogg->current_page ) < 0 )
{
/* error. stream version mismatch perhaps */
msg_Err( p_demux, "error reading first page of "
}
/* Check for Dirac header */
else if( ( oggpacket.bytes >= 5 &&
- ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
- ( oggpacket.bytes >= 9 &&
- ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
- {
- if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
- msg_Dbg( p_demux, "found dirac header" );
+ ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
+ ( oggpacket.bytes >= 9 &&
+ ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
+ {
+ if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
+ msg_Dbg( p_demux, "found dirac header" );
else
{
msg_Warn( p_demux, "found dirac header isn't decodable" );
}
}
else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
- oggpacket.bytes >= 56+1 )
+ oggpacket.bytes >= 44+1 )
{
stream_header_t tmp;
stream_header_t *st = &tmp;
st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
/* Check for video header (new format) */
- if( !strncmp( st->streamtype, "video", 5 ) )
+ if( !strncmp( st->streamtype, "video", 5 ) &&
+ oggpacket.bytes >= 52+1 )
{
st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
p_stream->fmt.video.i_bits_per_pixel );
}
/* Check for audio header (new format) */
- else if( !strncmp( st->streamtype, "audio", 5 ) )
+ else if( !strncmp( st->streamtype, "audio", 5 ) &&
+ oggpacket.bytes >= 56+1 )
{
char p_buffer[5];
int i_extra_size;
p_ogg->i_streams--;
}
- if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
+ if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
return VLC_EGENERIC;
}
for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
{
if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
- &oggpage ) == 0 )
+ &p_ogg->current_page ) == 0 )
{
p_ogg->b_page_waiting = true;
break;
p_stream->p_es = NULL;
+ /* initialise kframe index */
+ p_stream->idx=NULL;
+
/* Try first to reuse an old ES */
if( p_old_stream &&
p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
p_ogg->p_old_stream = NULL;
}
+
+
+ /* get total frame count for video stream; we will need this for seeking */
+ p_ogg->i_total_frames = 0;
+
return VLC_SUCCESS;
}
es_format_Clean( &p_stream->fmt_old );
es_format_Clean( &p_stream->fmt );
+ if ( p_stream->idx != NULL)
+ {
+ oggseek_index_entries_free( p_stream->idx );
+ }
+
free( p_stream );
}
/**
p_stream->b_force_backup = 1;
}
- else if( !strncmp(content_type_string, "audio/x-speex", 14) )
+ else if( !strncmp(content_type_string, "audio/x-speex", 13) )
{
p_stream->fmt.i_cat = AUDIO_ES;
p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
p_stream->b_force_backup = 1;
}
- else if( !strncmp(content_type_string, "video/x-xvid", 14) )
+ else if( !strncmp(content_type_string, "video/x-xvid", 12) )
{
p_stream->fmt.i_cat = VIDEO_ES;
p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
p_stream->b_force_backup = 1;
}
- else if( !strncmp(content_type_string, "video/mpeg", 14) )
+ else if( !strncmp(content_type_string, "video/mpeg", 10) )
{
/* n.b. MPEG streams are unsupported right now */
p_stream->fmt.i_cat = VIDEO_ES;