2002-03-16 14:32:03 +00:00
# include "config.h"
# include <stdlib.h>
# include <stdio.h>
2002-10-22 15:41:41 +00:00
# include <string.h>
2003-05-11 16:39:16 +00:00
# include <assert.h>
# include <math.h>
2006-06-04 22:41:27 +00:00
# include <inttypes.h>
2002-03-16 14:32:03 +00:00
2005-11-18 14:39:25 +00:00
# include "mp_msg.h"
# include "help_mp.h"
2007-03-15 18:36:36 +00:00
# include "stream/stream.h"
2002-03-16 14:32:03 +00:00
# include "demuxer.h"
# include "stheader.h"
2005-02-27 18:11:17 +00:00
# define FOURCC_VORBIS mmioFOURCC('v', 'r', 'b', 's')
2005-11-05 11:58:25 +00:00
# define FOURCC_SPEEX mmioFOURCC('s', 'p', 'x', ' ')
2005-02-27 18:11:17 +00:00
# define FOURCC_THEORA mmioFOURCC('t', 'h', 'e', 'o')
2002-12-04 20:27:36 +00:00
# ifdef TREMOR
# include <tremor/ogg.h>
# include <tremor/ivorbiscodec.h>
# else
2002-03-16 14:32:03 +00:00
# include <ogg/ogg.h>
# include <vorbis/codec.h>
2002-12-04 20:27:36 +00:00
# endif
2002-03-16 14:32:03 +00:00
2003-05-11 16:39:16 +00:00
# ifdef HAVE_OGGTHEORA
# include <theora/theora.h>
2005-12-05 01:25:31 +00:00
extern int _ilog ( unsigned int ) ; /* defined in many places in theora/lib/ */
2003-05-11 16:39:16 +00:00
# endif
2002-04-24 12:28:03 +00:00
# define BLOCK_SIZE 4096
2002-03-16 14:32:03 +00:00
2003-05-11 16:39:16 +00:00
/* Theora decoder context : we won't be able to interpret granule positions
* without using theora_granule_time with the theora_state of the stream .
* This is duplicated in ` vd_theora . c ' ; put this in a common header ?
*/
# ifdef HAVE_OGGTHEORA
typedef struct theora_struct_st {
theora_state st ;
2003-08-18 13:13:41 +00:00
theora_comment cc ;
2003-05-11 16:39:16 +00:00
theora_info inf ;
} theora_struct_t ;
# endif
2002-03-16 14:32:03 +00:00
//// OggDS headers
// Header for the new header format
typedef struct stream_header_video
{
ogg_int32_t width ;
ogg_int32_t height ;
} stream_header_video ;
typedef struct stream_header_audio
{
ogg_int16_t channels ;
ogg_int16_t blockalign ;
ogg_int32_t avgbytespersec ;
} stream_header_audio ;
2004-11-05 21:40:34 +00:00
typedef struct __attribute__ ( ( __packed__ ) ) stream_header
2002-03-16 14:32:03 +00:00
{
char streamtype [ 8 ] ;
char subtype [ 4 ] ;
ogg_int32_t size ; // size of the structure
ogg_int64_t time_unit ; // in reference time
ogg_int64_t samples_per_unit ;
ogg_int32_t default_len ; // in media time
ogg_int32_t buffersize ;
ogg_int16_t bits_per_sample ;
2004-11-05 21:40:34 +00:00
ogg_int16_t padding ;
2002-03-16 14:32:03 +00:00
union
{
// Video specific
stream_header_video video ;
// Audio specific
stream_header_audio audio ;
} sh ;
} stream_header ;
/// Our private datas
2002-04-20 17:50:11 +00:00
typedef struct ogg_syncpoint {
int64_t granulepos ;
off_t page_pos ;
} ogg_syncpoint_t ;
2002-03-16 14:32:03 +00:00
/// A logical stream
typedef struct ogg_stream {
/// Timestamping stuff
float samplerate ; /// granulpos 2 time
int64_t lastpos ;
int32_t lastsize ;
// Logical stream state
ogg_stream_state stream ;
2002-04-20 17:50:11 +00:00
int hdr_packets ;
int vorbis ;
2005-11-05 11:58:25 +00:00
int speex ;
2003-05-11 16:39:16 +00:00
int theora ;
2003-10-04 22:00:25 +00:00
int flac ;
2004-04-02 19:20:34 +00:00
int text ;
2004-11-25 22:24:00 +00:00
int id ;
2006-05-10 22:54:24 +00:00
2006-12-07 20:53:05 +00:00
vorbis_info vi ;
int vi_inited ;
2006-05-10 22:54:24 +00:00
void * ogg_d ;
2002-03-16 14:32:03 +00:00
} ogg_stream_t ;
typedef struct ogg_demuxer {
/// Physical stream state
ogg_sync_state sync ;
/// Current page
ogg_page page ;
/// Logical streams
ogg_stream_t * subs ;
int num_sub ;
2002-04-20 17:50:11 +00:00
ogg_syncpoint_t * syncpoints ;
int num_syncpoint ;
2002-04-24 12:28:03 +00:00
off_t pos , last_size ;
2003-12-08 09:47:15 +00:00
int64_t final_granulepos ;
2004-08-24 21:20:24 +00:00
/* Used for subtitle switching. */
int n_text ;
int * text_ids ;
2004-09-28 14:00:49 +00:00
char * * text_langs ;
2002-03-16 14:32:03 +00:00
} ogg_demuxer_t ;
2002-04-20 17:50:11 +00:00
# define NUM_VORBIS_HDR_PACKETS 3
2002-03-16 14:32:03 +00:00
/// Some defines from OggDS
# define PACKET_TYPE_HEADER 0x01
# define PACKET_TYPE_BITS 0x07
# define PACKET_LEN_BITS01 0xc0
# define PACKET_LEN_BITS2 0x02
# define PACKET_IS_SYNCPOINT 0x08
2002-04-24 12:28:03 +00:00
extern int index_mode ;
2004-04-02 19:20:34 +00:00
extern char * dvdsub_lang , * audio_lang ;
2004-09-28 13:51:20 +00:00
extern int dvdsub_id ;
2004-04-02 19:20:34 +00:00
2002-08-14 23:41:19 +00:00
//-------- subtitle support - should be moved to decoder layer, and queue
// - subtitles up in demuxer buffer...
2005-11-18 14:39:25 +00:00
# include "subreader.h"
# include "libvo/sub.h"
2002-08-14 23:41:19 +00:00
# define OGG_SUB_MAX_LINE 128
static subtitle ogg_sub ;
extern subtitle * vo_sub ;
//FILE* subout;
2002-10-22 15:41:41 +00:00
static
uint16_t get_uint16 ( const void * buf )
{
uint16_t ret ;
unsigned char * tmp ;
tmp = ( unsigned char * ) buf ;
ret = tmp [ 1 ] & 0xff ;
ret = ( ret < < 8 ) + ( tmp [ 0 ] & 0xff ) ;
return ( ret ) ;
}
static
uint32_t get_uint32 ( const void * buf )
{
uint32_t ret ;
unsigned char * tmp ;
tmp = ( unsigned char * ) buf ;
ret = tmp [ 3 ] & 0xff ;
ret = ( ret < < 8 ) + ( tmp [ 2 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 1 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 0 ] & 0xff ) ;
return ( ret ) ;
}
static
uint64_t get_uint64 ( const void * buf )
{
uint64_t ret ;
unsigned char * tmp ;
tmp = ( unsigned char * ) buf ;
ret = tmp [ 7 ] & 0xff ;
ret = ( ret < < 8 ) + ( tmp [ 6 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 5 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 4 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 3 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 2 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 1 ] & 0xff ) ;
ret = ( ret < < 8 ) + ( tmp [ 0 ] & 0xff ) ;
return ( ret ) ;
}
2003-01-04 22:56:56 +00:00
void demux_ogg_add_sub ( ogg_stream_t * os , ogg_packet * pack ) {
2002-08-14 23:41:19 +00:00
int lcv ;
int line_pos = 0 ;
int ignoring = 0 ;
2003-01-04 22:56:56 +00:00
char * packet = pack - > packet ;
2002-08-14 23:41:19 +00:00
2006-10-29 13:17:26 +00:00
if ( pack - > bytes < 4 )
return ;
2002-08-14 23:41:19 +00:00
mp_msg ( MSGT_DEMUX , MSGL_DBG2 , " \n demux_ogg_add_sub %02X %02X %02X '%s' \n " ,
( unsigned char ) packet [ 0 ] ,
( unsigned char ) packet [ 1 ] ,
( unsigned char ) packet [ 2 ] ,
& packet [ 3 ] ) ;
if ( ( ( unsigned char ) packet [ 0 ] ) = = 0x88 ) { // some subtitle text
2003-01-04 22:56:56 +00:00
// Find data start
2007-01-06 19:19:15 +00:00
double endpts = MP_NOPTS_VALUE ;
2003-01-04 22:56:56 +00:00
int32_t duration = 0 ;
int16_t hdrlen = ( * packet & PACKET_LEN_BITS01 ) > > 6 , i ;
hdrlen | = ( * packet & PACKET_LEN_BITS2 ) < < 1 ;
lcv = 1 + hdrlen ;
2006-10-29 13:17:26 +00:00
if ( pack - > bytes < lcv )
return ;
2003-01-04 22:56:56 +00:00
for ( i = hdrlen ; i > 0 ; i - - ) {
duration < < = 8 ;
duration | = ( unsigned char ) packet [ i ] ;
}
if ( ( hdrlen > 0 ) & & ( duration > 0 ) ) {
float pts ;
if ( pack - > granulepos = = - 1 )
pack - > granulepos = os - > lastpos + os - > lastsize ;
pts = ( float ) pack - > granulepos / ( float ) os - > samplerate ;
2007-01-06 19:19:15 +00:00
endpts = 1.0 + pts + ( float ) duration / 1000.0 ;
2002-08-14 23:41:19 +00:00
}
2007-01-06 19:19:15 +00:00
sub_clear_text ( & ogg_sub , MP_NOPTS_VALUE ) ;
sub_add_text ( & ogg_sub , & packet [ lcv ] , pack - > bytes - lcv , endpts ) ;
2002-08-14 23:41:19 +00:00
}
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_DBG2 , " Ogg sub lines: %d first: '%s' \n " ,
2002-08-14 23:41:19 +00:00
ogg_sub . lines , ogg_sub . text [ 0 ] ) ;
2002-12-28 15:19:41 +00:00
# ifdef USE_ICONV
2006-10-18 16:09:59 +00:00
subcp_recode ( & ogg_sub ) ;
2002-12-28 15:19:41 +00:00
# endif
2002-08-14 23:41:19 +00:00
vo_sub = & ogg_sub ;
vo_osd_changed ( OSDTYPE_SUBTITLE ) ;
}
2002-03-16 14:32:03 +00:00
// get the logical stream of the current page
// fill os if non NULL and return the stream id
static int demux_ogg_get_page_stream ( ogg_demuxer_t * ogg_d , ogg_stream_state * * os ) {
int id , s_no ;
ogg_page * page = & ogg_d - > page ;
s_no = ogg_page_serialno ( page ) ;
for ( id = 0 ; id < ogg_d - > num_sub ; id + + ) {
if ( s_no = = ogg_d - > subs [ id ] . stream . serialno )
break ;
}
2003-07-02 20:43:07 +00:00
if ( id = = ogg_d - > num_sub ) {
// If we have only one vorbis stream allow the stream id to change
// it's normal on radio stream (each song have an different id).
// But we (or the codec?) should check that the samplerate, etc
// doesn't change (for radio stream it's ok)
if ( ogg_d - > num_sub = = 1 & & ogg_d - > subs [ 0 ] . vorbis ) {
ogg_stream_reset ( & ogg_d - > subs [ 0 ] . stream ) ;
ogg_stream_init ( & ogg_d - > subs [ 0 ] . stream , s_no ) ;
id = 0 ;
} else
return - 1 ;
}
2002-03-16 14:32:03 +00:00
if ( os )
* os = & ogg_d - > subs [ id ] . stream ;
return id ;
}
2004-06-27 13:55:40 +00:00
static unsigned char * demux_ogg_read_packet ( ogg_stream_t * os , ogg_packet * pack , void * context , float * pts , int * flags , int samplesize ) {
2006-03-08 22:23:21 +00:00
unsigned char * data = pack - > packet ;
2006-05-10 22:54:24 +00:00
ogg_demuxer_t * ogg_d = os - > ogg_d ;
2002-03-16 14:32:03 +00:00
2002-04-20 17:50:11 +00:00
* pts = 0 ;
* flags = 0 ;
2002-03-16 14:32:03 +00:00
2002-04-20 17:50:11 +00:00
if ( os - > vorbis ) {
2002-03-16 14:32:03 +00:00
if ( * pack - > packet & PACKET_TYPE_HEADER )
2002-04-20 17:50:11 +00:00
os - > hdr_packets + + ;
2006-12-07 20:53:05 +00:00
else if ( os - > vi_inited )
2003-05-11 16:39:16 +00:00
{
2006-05-10 22:54:24 +00:00
vorbis_info * vi ;
2006-05-13 05:48:16 +00:00
int32_t blocksize ;
2006-05-10 22:54:24 +00:00
2003-10-04 17:29:08 +00:00
// When we dump the audio, there is no vi, but we don't care of timestamp in this case
2006-12-07 20:53:05 +00:00
vi = & ( os - > vi ) ;
2006-05-13 05:48:16 +00:00
blocksize = vorbis_packet_blocksize ( vi , pack ) / samplesize ;
2003-05-11 16:39:16 +00:00
// Calculate the timestamp if the packet don't have any
if ( pack - > granulepos = = - 1 ) {
pack - > granulepos = os - > lastpos ;
if ( os - > lastsize > 0 )
pack - > granulepos + = os - > lastsize ;
}
* pts = pack - > granulepos / ( float ) vi - > rate ;
os - > lastsize = blocksize ;
os - > lastpos = pack - > granulepos ;
2002-03-16 14:32:03 +00:00
}
2005-11-05 11:58:25 +00:00
} else if ( os - > speex ) {
2006-03-08 22:23:21 +00:00
// whole packet (default)
2003-05-11 16:39:16 +00:00
# ifdef HAVE_OGGTHEORA
} else if ( os - > theora ) {
/* we pass complete packets to theora, mustn't strip the header! */
os - > lastsize = 1 ;
2003-08-28 17:32:28 +00:00
/* header packets beginn on 1-bit: thus check (*data&0x80). We don't
have theora_state st , until all header packets were passed to the
decoder . */
if ( context ! = NULL & & ! ( * data & 0x80 ) )
2003-05-11 16:39:16 +00:00
{
2004-04-23 16:31:30 +00:00
theora_info * thi = ( ( theora_struct_t * ) context ) - > st . i ;
int keyframe_granule_shift = _ilog ( thi - > keyframe_frequency_force - 1 ) ;
int64_t iframemask = ( 1 < < keyframe_granule_shift ) - 1 ;
2003-05-11 16:39:16 +00:00
2004-04-23 16:31:30 +00:00
if ( pack - > granulepos > = 0 )
2003-05-11 16:39:16 +00:00
{
2004-04-23 16:31:30 +00:00
os - > lastpos = pack - > granulepos > > keyframe_granule_shift ;
os - > lastpos + = pack - > granulepos & iframemask ;
* flags = ( ( pack - > granulepos & iframemask ) = = 0 ) ;
2003-05-11 16:39:16 +00:00
}
else
{
os - > lastpos + + ;
}
2004-04-23 16:31:30 +00:00
pack - > granulepos = os - > lastpos ;
* pts = ( double ) os - > lastpos / ( double ) os - > samplerate ;
2003-05-11 16:39:16 +00:00
}
# endif /* HAVE_OGGTHEORA */
2003-10-04 22:00:25 +00:00
} else if ( os - > flac ) {
/* we pass complete packets to flac, mustn't strip the header! */
2006-10-29 11:05:49 +00:00
if ( os - > flac = = 2 & & pack - > packet [ 0 ] ! = 0xff )
return NULL ;
2002-03-16 14:32:03 +00:00
} else {
2004-04-23 16:31:30 +00:00
if ( * pack - > packet & PACKET_TYPE_HEADER )
os - > hdr_packets + + ;
else {
2002-03-16 14:32:03 +00:00
// Find data start
int16_t hdrlen = ( * pack - > packet & PACKET_LEN_BITS01 ) > > 6 ;
hdrlen | = ( * pack - > packet & PACKET_LEN_BITS2 ) < < 1 ;
data = pack - > packet + 1 + hdrlen ;
// Calculate the timestamp
if ( pack - > granulepos = = - 1 )
2004-04-23 16:31:30 +00:00
pack - > granulepos = os - > lastpos + ( os - > lastsize ? os - > lastsize : 1 ) ;
2002-03-16 14:32:03 +00:00
// If we alredy have a timestamp it can be a syncpoint
2002-04-20 17:50:11 +00:00
if ( * pack - > packet & PACKET_IS_SYNCPOINT )
* flags = 1 ;
* pts = pack - > granulepos / os - > samplerate ;
2002-03-16 14:32:03 +00:00
// Save the packet length and timestamp
os - > lastsize = 0 ;
while ( hdrlen ) {
os - > lastsize < < = 8 ;
os - > lastsize | = pack - > packet [ hdrlen ] ;
hdrlen - - ;
}
os - > lastpos = pack - > granulepos ;
}
2004-04-23 16:31:30 +00:00
}
2002-04-20 17:50:11 +00:00
return data ;
}
2004-04-02 19:20:34 +00:00
// check if clang has substring from comma separated langlist
2006-07-13 05:03:43 +00:00
static int demux_ogg_check_lang ( const char * clang , char * langlist )
2004-04-02 19:20:34 +00:00
{
char * c ;
if ( ! langlist | | ! * langlist )
return 0 ;
while ( ( c = strchr ( langlist , ' , ' ) ) )
{
if ( ! strncasecmp ( clang , langlist , c - langlist ) )
return 1 ;
langlist = & c [ 1 ] ;
}
if ( ! strncasecmp ( clang , langlist , strlen ( langlist ) ) )
return 1 ;
return 0 ;
}
2004-10-30 08:42:25 +00:00
static int demux_ogg_sub_reverse_id ( demuxer_t * demuxer , int id ) ;
2004-04-02 19:20:34 +00:00
/// Try to print out comments and also check for LANGUAGE= tag
static void demux_ogg_check_comments ( demuxer_t * d , ogg_stream_t * os , int id , vorbis_comment * vc )
{
2006-07-16 01:40:24 +00:00
const char * hdr , * val ;
2004-04-02 19:20:34 +00:00
char * * cmt = vc - > user_comments ;
2005-09-19 22:52:03 +00:00
int index , i ;
2004-09-28 14:00:49 +00:00
ogg_demuxer_t * ogg_d = ( ogg_demuxer_t * ) d - > priv ;
2005-09-19 22:52:03 +00:00
struct table {
2006-07-16 01:40:24 +00:00
const char * ogg ;
const char * mp ;
2005-09-19 22:52:03 +00:00
} table [ ] = {
{ " ENCODED_USING " , " Software " } ,
{ " ENCODER_URL " , " Encoder URL " } ,
{ " TITLE " , " Name " } ,
{ " ARTIST " , " Artist " } ,
{ " COMMENT " , " Comments " } ,
{ " DATE " , " Creation Date " } ,
{ " GENRE " , " Genre " } ,
{ " ALBUM " , " Album " } ,
{ " TRACKNUMBER " , " Track " } ,
{ NULL , NULL } ,
} ;
2004-04-02 19:20:34 +00:00
while ( * cmt )
{
hdr = NULL ;
2005-09-19 22:52:03 +00:00
if ( ! strncasecmp ( * cmt , " LANGUAGE= " , 9 ) )
2004-04-02 19:20:34 +00:00
{
val = * cmt + 9 ;
2006-04-24 07:20:34 +00:00
if ( ogg_d - > subs [ id ] . text )
mp_msg ( MSGT_IDENTIFY , MSGL_INFO , " ID_SID_%d_LANG=%s \n " , ogg_d - > subs [ id ] . id , val ) ;
else if ( id ! = d - > video - > id )
mp_msg ( MSGT_IDENTIFY , MSGL_INFO , " ID_AID_%d_LANG=%s \n " , ogg_d - > subs [ id ] . id , val ) ;
2005-01-21 18:55:01 +00:00
if ( ogg_d - > subs [ id ] . text )
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] Language for -sid %d is '-slang \" %s \" ' \n " , ogg_d - > subs [ id ] . id , val ) ;
2004-09-28 14:00:49 +00:00
// copy this language name into the array
index = demux_ogg_sub_reverse_id ( d , id ) ;
if ( index > = 0 ) {
// in case of malicious files with more than one lang per track:
if ( ogg_d - > text_langs [ index ] ) free ( ogg_d - > text_langs [ index ] ) ;
ogg_d - > text_langs [ index ] = strdup ( val ) ;
}
2004-04-02 19:20:34 +00:00
// check for -slang if subs are uninitialized yet
2006-11-19 13:38:37 +00:00
if ( os - > text & & d - > sub - > id < 0 & & demux_ogg_check_lang ( val , dvdsub_lang ) )
2004-04-02 19:20:34 +00:00
{
2006-11-19 14:23:54 +00:00
d - > sub - > id = index ;
2004-09-28 13:51:20 +00:00
dvdsub_id = index ;
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " Ogg demuxer: Displaying subtitle stream id %d which matched -slang %s \n " , id , val ) ;
2004-04-02 19:20:34 +00:00
}
else
hdr = " Language " ;
}
2005-09-19 22:52:03 +00:00
else {
for ( i = 0 ; table [ i ] . ogg ; i + + )
{
if ( ! strncasecmp ( * cmt , table [ i ] . ogg , strlen ( table [ i ] . ogg ) ) )
{
hdr = table [ i ] . mp ;
val = * cmt + strlen ( table [ i ] . ogg ) + 1 ;
}
}
2004-04-02 19:20:34 +00:00
}
if ( hdr )
2005-09-26 20:13:10 +00:00
demux_info_add ( d , hdr , val ) ;
2005-09-19 22:52:03 +00:00
mp_dbg ( MSGT_DEMUX , MSGL_DBG2 , " %s: %s \n " , hdr , val ) ;
2004-04-02 19:20:34 +00:00
cmt + + ;
}
}
2002-04-20 17:50:11 +00:00
/// Calculate the timestamp and add the packet to the demux stream
// return 1 if the packet was added, 0 otherwise
2004-04-02 19:20:34 +00:00
static int demux_ogg_add_packet ( demux_stream_t * ds , ogg_stream_t * os , int id , ogg_packet * pack ) {
2002-04-20 17:50:11 +00:00
demuxer_t * d = ds - > demuxer ;
demux_packet_t * dp ;
unsigned char * data ;
float pts = 0 ;
int flags = 0 ;
2003-05-11 16:39:16 +00:00
void * context = NULL ;
2004-06-27 13:55:40 +00:00
int samplesize = 1 ;
2002-04-20 17:50:11 +00:00
2004-04-02 19:20:34 +00:00
// If packet is an comment header then we try to get comments at first
if ( pack - > bytes > = 7 & & ! memcmp ( pack - > packet , " \003 vorbis " , 7 ) )
{
vorbis_info vi ;
vorbis_comment vc ;
vorbis_info_init ( & vi ) ;
vorbis_comment_init ( & vc ) ;
vi . rate = 1L ; // it's checked by vorbis_synthesis_headerin()
if ( vorbis_synthesis_headerin ( & vi , & vc , pack ) = = 0 ) // if no errors
demux_ogg_check_comments ( d , os , id , & vc ) ;
vorbis_comment_clear ( & vc ) ;
vorbis_info_clear ( & vi ) ;
}
if ( os - > text ) {
2006-11-19 14:35:39 +00:00
if ( id = = demux_ogg_sub_id ( d , d - > sub - > id ) ) // don't want to add subtitles to the demuxer for now
2004-04-02 19:20:34 +00:00
demux_ogg_add_sub ( os , pack ) ;
2002-08-14 23:41:19 +00:00
return 0 ;
}
2005-11-05 11:58:25 +00:00
if ( os - > speex ) {
// discard first two packets, they contain the header and comment
if ( os - > hdr_packets < 2 ) {
os - > hdr_packets + + ;
return 0 ;
}
} else
2003-05-11 16:39:16 +00:00
// If packet is an header we jump it except for vorbis and theora
// (PACKET_TYPE_HEADER bit doesn't even exist for theora ?!)
2003-11-15 20:00:31 +00:00
// We jump nothing for FLAC. Ain't this great? Packet contents have to be
// handled differently for each and every stream type. The joy! The joy!
if ( ! os - > flac & & ( ( * pack - > packet & PACKET_TYPE_HEADER ) & &
2005-02-27 18:11:17 +00:00
( ds ! = d - > audio | | ( ( ( sh_audio_t * ) ds - > sh ) - > format ! = FOURCC_VORBIS | | os - > hdr_packets > = NUM_VORBIS_HDR_PACKETS ) ) & &
( ds ! = d - > video | | ( ( ( sh_video_t * ) ds - > sh ) - > format ! = FOURCC_THEORA ) ) ) )
2002-04-20 17:50:11 +00:00
return 0 ;
2003-05-11 16:39:16 +00:00
// For vorbis packet the packet is the data, for other codec we must jump
// the header
2005-02-27 18:11:17 +00:00
if ( ds = = d - > audio & & ( ( sh_audio_t * ) ds - > sh ) - > format = = FOURCC_VORBIS ) {
2003-05-11 16:39:16 +00:00
context = ( ( sh_audio_t * ) ds - > sh ) - > context ;
2004-06-27 13:55:40 +00:00
samplesize = ( ( sh_audio_t * ) ds - > sh ) - > samplesize ;
}
2005-02-27 18:11:17 +00:00
if ( ds = = d - > video & & ( ( sh_audio_t * ) ds - > sh ) - > format = = FOURCC_THEORA )
2003-05-11 16:39:16 +00:00
context = ( ( sh_video_t * ) ds - > sh ) - > context ;
2004-06-27 13:55:40 +00:00
data = demux_ogg_read_packet ( os , pack , context , & pts , & flags , samplesize ) ;
2006-10-29 11:05:49 +00:00
if ( ! data )
return 0 ;
2002-04-20 17:50:11 +00:00
2003-01-04 22:56:56 +00:00
/// Clear subtitles if necessary (for broken files)
2007-01-06 19:19:15 +00:00
if ( sub_clear_text ( & ogg_sub , pts ) ) {
2003-01-04 22:56:56 +00:00
vo_sub = & ogg_sub ;
vo_osd_changed ( OSDTYPE_SUBTITLE ) ;
}
2002-03-16 14:32:03 +00:00
/// Send the packet
dp = new_demux_packet ( pack - > bytes - ( data - pack - > packet ) ) ;
memcpy ( dp - > buffer , data , pack - > bytes - ( data - pack - > packet ) ) ;
2002-03-31 20:09:17 +00:00
dp - > pts = pts ;
dp - > flags = flags ;
2002-03-16 14:32:03 +00:00
ds_add_packet ( ds , dp ) ;
2004-04-23 16:31:30 +00:00
mp_msg ( MSGT_DEMUX , MSGL_DBG2 , " New dp: %p ds=%p pts=%5.3f len=%d flag=%d \n " ,
2002-03-31 20:09:17 +00:00
dp , ds , pts , dp - > len , flags ) ;
2002-03-16 14:32:03 +00:00
return 1 ;
}
2004-04-23 16:31:30 +00:00
/// if -forceidx build a table of all syncpoints to make seeking easier
/// otherwise try to get at least the final_granulepos
2003-12-08 09:47:15 +00:00
void demux_ogg_scan_stream ( demuxer_t * demuxer ) {
2002-04-20 17:50:11 +00:00
ogg_demuxer_t * ogg_d = demuxer - > priv ;
stream_t * s = demuxer - > stream ;
ogg_sync_state * sync = & ogg_d - > sync ;
ogg_page * page = & ogg_d - > page ;
ogg_stream_state * oss ;
ogg_stream_t * os ;
ogg_packet op ;
2004-06-27 13:55:40 +00:00
int np , sid , p , samplesize = 1 ;
2003-05-11 16:39:16 +00:00
void * context = NULL ;
2002-04-20 17:50:11 +00:00
off_t pos , last_pos ;
pos = last_pos = demuxer - > movi_start ;
// Reset the stream
2003-12-08 09:47:15 +00:00
if ( index_mode = = 2 ) {
2002-04-20 17:50:11 +00:00
stream_seek ( s , demuxer - > movi_start ) ;
2004-04-23 16:31:30 +00:00
} else {
//the 270000 are just a wild guess
2006-12-09 12:24:11 +00:00
stream_seek ( s , FFMAX ( ogg_d - > pos , demuxer - > movi_end - 270000 ) ) ;
2003-12-08 09:47:15 +00:00
}
2002-04-20 17:50:11 +00:00
ogg_sync_reset ( sync ) ;
// Get the serial number of the stream we use
2003-05-11 16:39:16 +00:00
if ( demuxer - > video - > id > = 0 ) {
2002-04-20 17:50:11 +00:00
sid = demuxer - > video - > id ;
2003-05-11 16:39:16 +00:00
/* demux_ogg_read_packet needs decoder context for Theora streams */
2005-02-27 18:11:17 +00:00
if ( ( ( sh_video_t * ) demuxer - > video - > sh ) - > format = = FOURCC_THEORA )
2003-05-11 16:39:16 +00:00
context = ( ( sh_video_t * ) demuxer - > video - > sh ) - > context ;
}
2005-12-18 11:55:47 +00:00
else if ( demuxer - > audio - > id > = 0 ) {
2002-04-20 17:50:11 +00:00
sid = demuxer - > audio - > id ;
2003-05-11 16:39:16 +00:00
/* demux_ogg_read_packet needs decoder context for Vorbis streams */
2005-02-27 18:11:17 +00:00
if ( ( ( sh_audio_t * ) demuxer - > audio - > sh ) - > format = = FOURCC_VORBIS ) {
2003-05-11 16:39:16 +00:00
context = ( ( sh_audio_t * ) demuxer - > audio - > sh ) - > context ;
2004-06-27 13:55:40 +00:00
samplesize = ( ( sh_audio_t * ) demuxer - > audio - > sh ) - > samplesize ;
}
2002-04-20 17:50:11 +00:00
}
2005-12-18 11:55:47 +00:00
else return ;
2002-04-20 17:50:11 +00:00
os = & ogg_d - > subs [ sid ] ;
oss = & os - > stream ;
while ( 1 ) {
np = ogg_sync_pageseek ( sync , page ) ;
if ( np < 0 ) { // We had to skip some bytes
2003-12-08 09:47:15 +00:00
if ( index_mode = = 2 ) mp_msg ( MSGT_DEMUX , MSGL_ERR , " Bad page sync while building syncpoints table (%d) \n " , - np ) ;
2002-04-20 17:50:11 +00:00
pos + = - np ;
continue ;
}
if ( np < = 0 ) { // We need more data
char * buf = ogg_sync_buffer ( sync , BLOCK_SIZE ) ;
int len = stream_read ( s , buf , BLOCK_SIZE ) ;
if ( len = = 0 & & s - > eof )
break ;
ogg_sync_wrote ( sync , len ) ;
continue ;
}
// The page is ready
//ogg_sync_pageout(sync,page);
if ( ogg_page_serialno ( page ) ! = os - > stream . serialno ) { // It isn't a page from the stream we want
pos + = np ;
continue ;
}
if ( ogg_stream_pagein ( oss , page ) ! = 0 ) {
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Pagein error ???? \n " ) ;
pos + = np ;
continue ;
}
p = 0 ;
while ( ogg_stream_packetout ( oss , & op ) = = 1 ) {
float pts ;
int flags ;
2004-06-27 13:55:40 +00:00
demux_ogg_read_packet ( os , & op , context , & pts , & flags , samplesize ) ;
2004-04-23 16:31:30 +00:00
if ( op . granulepos > = 0 ) ogg_d - > final_granulepos = op . granulepos ;
if ( index_mode = = 2 & & ( flags | | ( os - > vorbis & & op . granulepos > = 0 ) ) ) {
2006-05-13 06:12:25 +00:00
if ( ogg_d - > num_syncpoint > SIZE_MAX / sizeof ( ogg_syncpoint_t ) - 1 ) break ;
2006-05-13 05:48:16 +00:00
ogg_d - > syncpoints = realloc_struct ( ogg_d - > syncpoints , ( ogg_d - > num_syncpoint + 1 ) , sizeof ( ogg_syncpoint_t ) ) ;
2002-04-20 17:50:11 +00:00
ogg_d - > syncpoints [ ogg_d - > num_syncpoint ] . granulepos = op . granulepos ;
ogg_d - > syncpoints [ ogg_d - > num_syncpoint ] . page_pos = ( ogg_page_continued ( page ) & & p = = 0 ) ? last_pos : pos ;
ogg_d - > num_syncpoint + + ;
}
p + + ;
}
2002-04-24 12:28:03 +00:00
if ( p > 1 | | ( p = = 1 & & ! ogg_page_continued ( page ) ) )
2002-04-20 17:50:11 +00:00
last_pos = pos ;
pos + = np ;
2003-12-08 09:47:15 +00:00
if ( index_mode = = 2 ) mp_msg ( MSGT_DEMUX , MSGL_INFO , " Building syncpoint table %d%% \r " , ( int ) ( pos * 100 / s - > end_pos ) ) ;
2002-04-20 17:50:11 +00:00
}
2003-12-08 09:47:15 +00:00
if ( index_mode = = 2 ) mp_msg ( MSGT_DEMUX , MSGL_INFO , " \n " ) ;
2002-04-20 17:50:11 +00:00
2003-12-08 09:47:15 +00:00
if ( index_mode = = 2 ) mp_msg ( MSGT_DEMUX , MSGL_V , " Ogg syncpoints table builed: %d syncpoints \n " , ogg_d - > num_syncpoint ) ;
2005-10-13 18:33:56 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " Ogg stream length (granulepos): % " PRId64 " \n " , ogg_d - > final_granulepos ) ;
2002-04-20 17:50:11 +00:00
stream_reset ( s ) ;
stream_seek ( s , demuxer - > movi_start ) ;
ogg_sync_reset ( sync ) ;
for ( np = 0 ; np < ogg_d - > num_sub ; np + + ) {
ogg_stream_reset ( & ogg_d - > subs [ np ] . stream ) ;
ogg_d - > subs [ np ] . lastpos = ogg_d - > subs [ np ] . lastsize = ogg_d - > subs [ np ] . hdr_packets = 0 ;
}
// Get the first page
while ( 1 ) {
np = ogg_sync_pageout ( sync , page ) ;
if ( np < = 0 ) { // We need more data
char * buf = ogg_sync_buffer ( sync , BLOCK_SIZE ) ;
int len = stream_read ( s , buf , BLOCK_SIZE ) ;
if ( len = = 0 & & s - > eof ) {
mp_msg ( MSGT_DEMUX , MSGL_ERR , " EOF while trying to get the first page !!!! \n " ) ;
break ;
}
ogg_sync_wrote ( sync , len ) ;
continue ;
}
demux_ogg_get_page_stream ( ogg_d , & oss ) ;
ogg_stream_pagein ( oss , page ) ;
break ;
}
}
2006-03-27 17:25:41 +00:00
extern void print_wave_header ( WAVEFORMATEX * h , int verbose_level ) ;
extern void print_video_header ( BITMAPINFOHEADER * h , int verbose_level ) ;
2002-11-06 23:54:29 +00:00
2006-08-04 21:19:06 +00:00
/* defined in demux_mov.c */
extern unsigned int store_ughvlc ( unsigned char * s , unsigned int v ) ;
2004-08-24 21:20:24 +00:00
/** \brief Change the current subtitle stream and return its ID.
\ param demuxer The demuxer whose subtitle stream will be changed .
\ param new_num The number of the new subtitle track . The number must be
between 0 and ogg_d - > n_text - 1.
2004-08-22 18:47:48 +00:00
2004-08-24 21:20:24 +00:00
\ returns The Ogg stream number ( = page serial number ) of the newly selected
track .
*/
int demux_ogg_sub_id ( demuxer_t * demuxer , int index ) {
ogg_demuxer_t * ogg_d = ( ogg_demuxer_t * ) demuxer - > priv ;
return ( index < 0 ) ? index : ( index > = ogg_d - > n_text ) ? - 1 : ogg_d - > text_ids [ index ] ;
}
2004-08-22 18:47:48 +00:00
2004-09-28 14:00:49 +00:00
/** \brief Translate the ogg track number into the subtitle number.
* \ param demuxer The demuxer about whose subtitles we are inquiring .
* \ param id The ogg track number of the subtitle track .
*/
static int demux_ogg_sub_reverse_id ( demuxer_t * demuxer , int id ) {
ogg_demuxer_t * ogg_d = ( ogg_demuxer_t * ) demuxer - > priv ;
int i ;
for ( i = 0 ; i < ogg_d - > n_text ; i + + )
if ( ogg_d - > text_ids [ i ] = = id ) return i ;
return - 1 ;
}
/** \brief Lookup the subtitle language by the subtitle number. Returns NULL on out-of-bounds input.
* \ param demuxer The demuxer about whose subtitles we are inquiring .
* \ param index The subtitle number .
*/
char * demux_ogg_sub_lang ( demuxer_t * demuxer , int index ) {
ogg_demuxer_t * ogg_d = ( ogg_demuxer_t * ) demuxer - > priv ;
return ( index < 0 ) ? NULL : ( index > = ogg_d - > n_text ) ? NULL : ogg_d - > text_langs [ index ] ;
}
2005-08-05 19:57:47 +00:00
static void demux_close_ogg ( demuxer_t * demuxer ) ;
2005-02-06 10:30:10 +00:00
2006-05-10 22:54:24 +00:00
static void fixup_vorbis_wf ( sh_audio_t * sh , ogg_demuxer_t * od )
2005-05-12 17:35:58 +00:00
{
2005-05-12 23:12:27 +00:00
int i , offset ;
2006-05-10 22:54:24 +00:00
int ris , init_error = 0 ;
2005-05-12 17:35:58 +00:00
ogg_packet op [ 3 ] ;
unsigned char * buf [ 3 ] ;
unsigned char * ptr ;
2005-05-13 00:00:54 +00:00
unsigned int len ;
2006-12-07 20:53:05 +00:00
ogg_stream_t * os = & od - > subs [ sh - > ds - > id ] ;
2006-12-07 12:02:51 +00:00
vorbis_comment vc ;
2005-05-12 17:35:58 +00:00
2006-12-07 20:53:05 +00:00
vorbis_info_init ( & os - > vi ) ;
2006-12-07 12:02:51 +00:00
vorbis_comment_init ( & vc ) ;
2005-05-12 17:35:58 +00:00
for ( i = 0 ; i < 3 ; i + + ) {
op [ i ] . bytes = ds_get_packet ( sh - > ds , & ( op [ i ] . packet ) ) ;
2005-05-12 23:12:27 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " fixup_vorbis_wf: i=%d, size=%ld \n " , i , op [ i ] . bytes ) ;
2005-05-12 17:35:58 +00:00
if ( op [ i ] . bytes < 0 ) {
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Ogg demuxer error!, fixup_vorbis_wf: bad packet n. %d \n " , i ) ;
return ;
}
buf [ i ] = malloc ( op [ i ] . bytes ) ;
if ( ! buf [ i ] )
return ;
memcpy ( buf [ i ] , op [ i ] . packet , op [ i ] . bytes ) ;
2006-05-10 22:54:24 +00:00
2006-06-07 22:40:40 +00:00
op [ i ] . b_o_s = ( i = = 0 ) ;
2006-12-07 20:53:05 +00:00
ris = vorbis_synthesis_headerin ( & ( os - > vi ) , & vc , & ( op [ i ] ) ) ;
2006-05-10 22:54:24 +00:00
if ( ris < 0 ) {
init_error = 1 ;
mp_msg ( MSGT_DECAUDIO , MSGL_ERR , " DEMUX_OGG: header n. %d broken! len=%ld, code: %d \n " , i , op [ i ] . bytes , ris ) ;
}
2005-05-12 17:35:58 +00:00
}
2006-12-07 12:02:51 +00:00
vorbis_comment_clear ( & vc ) ;
2006-05-10 22:54:24 +00:00
if ( ! init_error )
2006-12-07 20:53:05 +00:00
os - > vi_inited = 1 ;
2005-05-12 17:35:58 +00:00
2005-05-13 00:00:54 +00:00
len = op [ 0 ] . bytes + op [ 1 ] . bytes + op [ 2 ] . bytes ;
2006-07-03 13:21:54 +00:00
sh - > wf = calloc ( 1 , sizeof ( WAVEFORMATEX ) + len + len / 255 + 64 ) ;
2005-05-12 17:35:58 +00:00
ptr = ( unsigned char * ) ( sh - > wf + 1 ) ;
ptr [ 0 ] = 2 ;
offset = 1 ;
offset + = store_ughvlc ( & ptr [ offset ] , op [ 0 ] . bytes ) ;
2005-05-12 23:12:27 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " demux_ogg, offset after 1st len = %u \n " , offset ) ;
2005-05-12 17:35:58 +00:00
offset + = store_ughvlc ( & ptr [ offset ] , op [ 1 ] . bytes ) ;
2005-05-12 23:12:27 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " demux_ogg, offset after 2nd len = %u \n " , offset ) ;
2005-05-12 17:35:58 +00:00
for ( i = 0 ; i < 3 ; i + + ) {
2005-05-12 23:12:27 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " demux_ogg, i=%d, bytes: %ld, offset: %u \n " , i , op [ i ] . bytes , offset ) ;
2005-05-12 17:35:58 +00:00
memcpy ( & ptr [ offset ] , buf [ i ] , op [ i ] . bytes ) ;
offset + = op [ i ] . bytes ;
}
sh - > wf - > cbSize = offset ;
mp_msg ( MSGT_DEMUX , MSGL_V , " demux_ogg, extradata size: %d \n " , sh - > wf - > cbSize ) ;
sh - > wf = ( WAVEFORMATEX * ) realloc ( sh - > wf , sizeof ( WAVEFORMATEX ) + sh - > wf - > cbSize ) ;
if ( op [ 0 ] . bytes > = 29 ) {
2006-09-05 20:18:42 +00:00
unsigned int br ;
int nombr , minbr , maxbr ;
2005-05-12 17:35:58 +00:00
ptr = buf [ 0 ] ;
sh - > channels = ptr [ 11 ] ;
sh - > samplerate = sh - > wf - > nSamplesPerSec = get_uint32 ( & ptr [ 12 ] ) ;
maxbr = get_uint32 ( & ptr [ 16 ] ) ; //max
nombr = get_uint32 ( & ptr [ 20 ] ) ; //nominal
minbr = get_uint32 ( & ptr [ 24 ] ) ; //minimum
2006-09-05 20:18:42 +00:00
if ( maxbr = = - 1 )
maxbr = 0 ;
if ( nombr = = - 1 )
nombr = 0 ;
if ( minbr = = - 1 )
minbr = 0 ;
2005-05-12 17:35:58 +00:00
br = maxbr / 8 ;
if ( ! br )
br = nombr / 8 ;
if ( ! br )
br = minbr / 8 ;
sh - > wf - > nAvgBytesPerSec = br ;
sh - > wf - > wBitsPerSample = 16 ;
sh - > samplesize = ( sh - > wf - > wBitsPerSample + 7 ) / 8 ;
mp_msg ( MSGT_DEMUX , MSGL_V , " demux_ogg, vorbis stream features are: channels: %d, srate: %d, bitrate: %d, max: %u, nominal: %u, min: %u \n " ,
sh - > channels , sh - > samplerate , sh - > wf - > nAvgBytesPerSec , maxbr , nombr , minbr ) ;
}
free ( buf [ 2 ] ) ;
free ( buf [ 1 ] ) ;
free ( buf [ 0 ] ) ;
}
2002-03-16 14:32:03 +00:00
/// Open an ogg physical stream
2005-08-05 19:57:47 +00:00
// Not static because it's used also in demuxer_avi.c
2002-03-16 14:32:03 +00:00
int demux_ogg_open ( demuxer_t * demuxer ) {
ogg_demuxer_t * ogg_d ;
stream_t * s ;
char * buf ;
2004-08-22 18:47:48 +00:00
int np , s_no , n_audio = 0 , n_video = 0 ;
2004-04-06 06:36:46 +00:00
int audio_id = - 1 , video_id = - 1 , text_id = - 1 ;
2002-03-16 14:32:03 +00:00
ogg_sync_state * sync ;
ogg_page * page ;
ogg_packet pack ;
sh_audio_t * sh_a ;
sh_video_t * sh_v ;
2002-12-28 15:19:41 +00:00
# ifdef USE_ICONV
2004-07-28 12:40:35 +00:00
subcp_open ( NULL ) ;
2002-12-28 15:19:41 +00:00
# endif
2002-03-16 14:32:03 +00:00
s = demuxer - > stream ;
2005-05-02 07:59:12 +00:00
demuxer - > priv =
2006-07-03 13:21:54 +00:00
ogg_d = calloc ( 1 , sizeof ( ogg_demuxer_t ) ) ;
2002-03-16 14:32:03 +00:00
sync = & ogg_d - > sync ;
page = & ogg_d - > page ;
2002-04-24 12:28:03 +00:00
ogg_sync_init ( sync ) ;
2002-03-16 14:32:03 +00:00
while ( 1 ) {
/// Try to get a page
2002-04-24 12:28:03 +00:00
ogg_d - > pos + = ogg_d - > last_size ;
np = ogg_sync_pageseek ( sync , page ) ;
2002-03-16 14:32:03 +00:00
/// Error
if ( np < 0 ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_DBG2 , " Ogg demuxer : Bad page sync \n " ) ;
2005-02-06 10:30:10 +00:00
goto err_out ;
2002-03-16 14:32:03 +00:00
}
/// Need some more data
2002-04-24 12:28:03 +00:00
if ( np = = 0 ) {
2002-03-16 14:32:03 +00:00
int len ;
buf = ogg_sync_buffer ( sync , BLOCK_SIZE ) ;
len = stream_read ( s , buf , BLOCK_SIZE ) ;
if ( len = = 0 & & s - > eof ) {
2005-02-06 10:30:10 +00:00
goto err_out ;
2002-03-16 14:32:03 +00:00
}
ogg_sync_wrote ( sync , len ) ;
continue ;
}
2002-04-24 12:28:03 +00:00
ogg_d - > last_size = np ;
2002-03-16 14:32:03 +00:00
// We got one page now
if ( ! ogg_page_bos ( page ) ) { // It's not a begining page
// Header parsing end here, we need to get the page otherwise it will be lost
int id = demux_ogg_get_page_stream ( ogg_d , NULL ) ;
if ( id > = 0 )
ogg_stream_pagein ( & ogg_d - > subs [ id ] . stream , page ) ;
else
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Ogg : Warning found none bos page from unknown stream %d \n " , ogg_page_serialno ( page ) ) ;
2002-03-16 14:32:03 +00:00
break ;
}
/// Init the data structure needed for a logical stream
ogg_d - > subs = ( ogg_stream_t * ) realloc ( ogg_d - > subs , ( ogg_d - > num_sub + 1 ) * sizeof ( ogg_stream_t ) ) ;
memset ( & ogg_d - > subs [ ogg_d - > num_sub ] , 0 , sizeof ( ogg_stream_t ) ) ;
/// Get the stream serial number
s_no = ogg_page_serialno ( page ) ;
ogg_stream_init ( & ogg_d - > subs [ ogg_d - > num_sub ] . stream , s_no ) ;
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_DBG2 , " Ogg : Found a stream with serial=%d \n " , s_no ) ;
2002-03-16 14:32:03 +00:00
// Take the first page
ogg_stream_pagein ( & ogg_d - > subs [ ogg_d - > num_sub ] . stream , page ) ;
// Get first packet of the page
ogg_stream_packetout ( & ogg_d - > subs [ ogg_d - > num_sub ] . stream , & pack ) ;
// Reset our vars
sh_a = NULL ;
sh_v = NULL ;
2006-05-10 22:54:24 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . ogg_d = ogg_d ;
2004-11-25 22:24:00 +00:00
2002-03-16 14:32:03 +00:00
// Check for Vorbis
if ( pack . bytes > = 7 & & ! strncmp ( & pack . packet [ 1 ] , " vorbis " , 6 ) ) {
2006-08-26 19:17:04 +00:00
sh_a = new_sh_audio_aid ( demuxer , ogg_d - > num_sub , n_audio ) ;
2005-02-27 18:11:17 +00:00
sh_a - > format = FOURCC_VORBIS ;
2002-04-20 17:50:11 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . vorbis = 1 ;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_audio ;
2002-03-16 14:32:03 +00:00
n_audio + + ;
2005-01-22 14:28:23 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: audio (Vorbis), -aid %d \n " , ogg_d - > num_sub , n_audio - 1 ) ;
2005-11-05 11:58:25 +00:00
} else if ( pack . bytes > = 80 & & ! strncmp ( pack . packet , " Speex " , 5 ) ) {
2006-08-26 19:17:04 +00:00
sh_a = new_sh_audio_aid ( demuxer , ogg_d - > num_sub , n_audio ) ;
2006-07-03 13:21:54 +00:00
sh_a - > wf = calloc ( 1 , sizeof ( WAVEFORMATEX ) + pack . bytes ) ;
2005-11-05 11:58:25 +00:00
sh_a - > format = FOURCC_SPEEX ;
sh_a - > samplerate = sh_a - > wf - > nSamplesPerSec = get_uint32 ( & pack . packet [ 36 ] ) ;
sh_a - > channels = sh_a - > wf - > nChannels = get_uint32 ( & pack . packet [ 48 ] ) ;
sh_a - > wf - > wFormatTag = sh_a - > format ;
sh_a - > wf - > nAvgBytesPerSec = get_uint32 ( & pack . packet [ 52 ] ) ;
sh_a - > wf - > nBlockAlign = 0 ;
sh_a - > wf - > wBitsPerSample = 16 ;
sh_a - > samplesize = 2 ;
sh_a - > wf - > cbSize = pack . bytes ;
memcpy ( & sh_a - > wf [ 1 ] , pack . packet , pack . bytes ) ;
ogg_d - > subs [ ogg_d - > num_sub ] . samplerate = sh_a - > samplerate ;
ogg_d - > subs [ ogg_d - > num_sub ] . speex = 1 ;
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_audio ;
n_audio + + ;
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: audio (Speex), -aid %d \n " , ogg_d - > num_sub , n_audio - 1 ) ;
2002-03-16 14:32:03 +00:00
2003-05-11 16:39:16 +00:00
// check for Theora
# ifdef HAVE_OGGTHEORA
} else if ( pack . bytes > = 7 & & ! strncmp ( & pack . packet [ 1 ] , " theora " , 6 ) ) {
int errorCode = 0 ;
theora_info inf ;
2003-08-18 13:13:41 +00:00
theora_comment cc ;
theora_info_init ( & inf ) ;
theora_comment_init ( & cc ) ;
errorCode = theora_decode_header ( & inf , & cc , & pack ) ;
2003-05-11 16:39:16 +00:00
if ( errorCode )
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Theora header parsing failed: %i \n " ,
errorCode ) ;
else
{
2006-08-26 19:17:04 +00:00
sh_v = new_sh_video_vid ( demuxer , ogg_d - > num_sub , n_video ) ;
2003-05-11 16:39:16 +00:00
sh_v - > context = NULL ;
2006-07-03 13:21:54 +00:00
sh_v - > bih = calloc ( 1 , sizeof ( BITMAPINFOHEADER ) ) ;
2003-05-11 16:39:16 +00:00
sh_v - > bih - > biSize = sizeof ( BITMAPINFOHEADER ) ;
2005-02-27 18:11:17 +00:00
sh_v - > bih - > biCompression = sh_v - > format = FOURCC_THEORA ;
2003-05-11 16:39:16 +00:00
sh_v - > fps = ( ( double ) inf . fps_numerator ) /
( double ) inf . fps_denominator ;
sh_v - > frametime = ( ( double ) inf . fps_denominator ) /
( double ) inf . fps_numerator ;
2005-02-22 19:18:45 +00:00
sh_v - > disp_w = sh_v - > bih - > biWidth = inf . frame_width ;
sh_v - > disp_h = sh_v - > bih - > biHeight = inf . frame_height ;
2003-05-11 16:39:16 +00:00
sh_v - > bih - > biBitCount = 24 ;
sh_v - > bih - > biPlanes = 3 ;
sh_v - > bih - > biSizeImage = ( ( sh_v - > bih - > biBitCount / 8 ) *
sh_v - > bih - > biWidth * sh_v - > bih - > biHeight ) ;
ogg_d - > subs [ ogg_d - > num_sub ] . samplerate = sh_v - > fps ;
ogg_d - > subs [ ogg_d - > num_sub ] . theora = 1 ;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_video ;
2003-05-11 16:39:16 +00:00
n_video + + ;
2005-01-22 14:28:23 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO ,
" [Ogg] stream %d: video (Theora v%d.%d.%d), -vid %d \n " ,
ogg_d - > num_sub ,
2003-05-11 16:39:16 +00:00
( int ) inf . version_major ,
( int ) inf . version_minor ,
( int ) inf . version_subminor ,
2005-01-22 14:28:23 +00:00
n_video - 1 ) ;
2006-03-27 17:25:41 +00:00
if ( mp_msg_test ( MSGT_HEADER , MSGL_V ) ) print_video_header ( sh_v - > bih , MSGL_V ) ;
2003-05-11 16:39:16 +00:00
}
2006-12-06 10:46:50 +00:00
theora_comment_clear ( & cc ) ;
theora_info_clear ( & inf ) ;
2003-05-11 16:39:16 +00:00
# endif /* HAVE_OGGTHEORA */
2003-10-04 22:00:25 +00:00
} else if ( pack . bytes > = 4 & & ! strncmp ( & pack . packet [ 0 ] , " fLaC " , 4 ) ) {
2006-08-26 19:17:04 +00:00
sh_a = new_sh_audio_aid ( demuxer , ogg_d - > num_sub , n_audio ) ;
2003-10-04 22:00:25 +00:00
sh_a - > format = mmioFOURCC ( ' f ' , ' L ' , ' a ' , ' C ' ) ;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_audio ;
2003-10-04 22:00:25 +00:00
n_audio + + ;
ogg_d - > subs [ ogg_d - > num_sub ] . flac = 1 ;
sh_a - > wf = NULL ;
2005-01-22 14:28:23 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: audio (FLAC), -aid %d \n " , ogg_d - > num_sub , n_audio - 1 ) ;
2006-10-29 11:05:49 +00:00
} else if ( pack . bytes > = 51 & & ! strncmp ( & pack . packet [ 1 ] , " FLAC " , 4 ) ) {
sh_a = new_sh_audio_aid ( demuxer , ogg_d - > num_sub , n_audio ) ;
sh_a - > format = mmioFOURCC ( ' f ' , ' L ' , ' a ' , ' C ' ) ;
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_audio ;
n_audio + + ;
ogg_d - > subs [ ogg_d - > num_sub ] . flac = 2 ;
sh_a - > wf = calloc ( 1 , sizeof ( WAVEFORMATEX ) + 34 ) ;
sh_a - > wf - > wFormatTag = sh_a - > format ;
sh_a - > wf - > cbSize = 34 ;
memcpy ( & sh_a - > wf [ 1 ] , & pack . packet [ 17 ] , 34 ) ;
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: audio (FLAC, try 2), -aid %d \n " , ogg_d - > num_sub , n_audio - 1 ) ;
2003-10-04 22:00:25 +00:00
2002-03-16 14:32:03 +00:00
/// Check for old header
} else if ( pack . bytes > = 142 & & ! strncmp ( & pack . packet [ 1 ] , " Direct Show Samples embedded in Ogg " , 35 ) ) {
// Old video header
2002-10-22 15:41:41 +00:00
if ( get_uint32 ( pack . packet + 96 ) = = 0x05589f80 & & pack . bytes > = 184 ) {
2006-08-26 19:17:04 +00:00
sh_v = new_sh_video_vid ( demuxer , ogg_d - > num_sub , n_video ) ;
2006-07-03 13:21:54 +00:00
sh_v - > bih = calloc ( 1 , sizeof ( BITMAPINFOHEADER ) ) ;
2002-03-31 20:17:27 +00:00
sh_v - > bih - > biSize = sizeof ( BITMAPINFOHEADER ) ;
sh_v - > bih - > biCompression =
2002-03-16 14:32:03 +00:00
sh_v - > format = mmioFOURCC ( pack . packet [ 68 ] , pack . packet [ 69 ] ,
pack . packet [ 70 ] , pack . packet [ 71 ] ) ;
2002-10-22 15:41:41 +00:00
sh_v - > frametime = get_uint64 ( pack . packet + 164 ) * 0.0000001 ;
2002-03-16 14:32:03 +00:00
sh_v - > fps = 1 / sh_v - > frametime ;
2002-10-22 15:41:41 +00:00
sh_v - > disp_w = sh_v - > bih - > biWidth = get_uint32 ( pack . packet + 176 ) ;
sh_v - > disp_h = sh_v - > bih - > biHeight = get_uint32 ( pack . packet + 180 ) ;
sh_v - > bih - > biBitCount = get_uint16 ( pack . packet + 182 ) ;
2002-03-31 20:17:27 +00:00
if ( ! sh_v - > bih - > biBitCount ) sh_v - > bih - > biBitCount = 24 ; // hack, FIXME
sh_v - > bih - > biPlanes = 1 ;
sh_v - > bih - > biSizeImage = ( sh_v - > bih - > biBitCount > > 3 ) * sh_v - > bih - > biWidth * sh_v - > bih - > biHeight ;
2002-03-16 14:32:03 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . samplerate = sh_v - > fps ;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_video ;
2002-03-16 14:32:03 +00:00
n_video + + ;
2005-01-21 18:55:01 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: video (FOURCC %c%c%c%c), -vid %d \n " ,
ogg_d - > num_sub , pack . packet [ 68 ] , pack . packet [ 69 ] , pack . packet [ 70 ] , pack . packet [ 71 ] , n_video - 1 ) ;
2006-03-27 17:25:41 +00:00
if ( mp_msg_test ( MSGT_HEADER , MSGL_V ) ) print_video_header ( sh_v - > bih , MSGL_V ) ;
2002-03-16 14:32:03 +00:00
// Old audio header
2002-10-22 15:41:41 +00:00
} else if ( get_uint32 ( pack . packet + 96 ) = = 0x05589F81 ) {
2002-03-16 14:32:03 +00:00
unsigned int extra_size ;
2006-08-26 19:17:04 +00:00
sh_a = new_sh_audio_aid ( demuxer , ogg_d - > num_sub , n_audio ) ;
2002-10-22 15:41:41 +00:00
extra_size = get_uint16 ( pack . packet + 140 ) ;
2006-07-03 13:21:54 +00:00
sh_a - > wf = calloc ( 1 , sizeof ( WAVEFORMATEX ) + extra_size ) ;
2002-10-22 15:41:41 +00:00
sh_a - > format = sh_a - > wf - > wFormatTag = get_uint16 ( pack . packet + 124 ) ;
sh_a - > channels = sh_a - > wf - > nChannels = get_uint16 ( pack . packet + 126 ) ;
sh_a - > samplerate = sh_a - > wf - > nSamplesPerSec = get_uint32 ( pack . packet + 128 ) ;
sh_a - > wf - > nAvgBytesPerSec = get_uint32 ( pack . packet + 132 ) ;
sh_a - > wf - > nBlockAlign = get_uint16 ( pack . packet + 136 ) ;
sh_a - > wf - > wBitsPerSample = get_uint16 ( pack . packet + 138 ) ;
2002-03-16 14:32:03 +00:00
sh_a - > samplesize = ( sh_a - > wf - > wBitsPerSample + 7 ) / 8 ;
sh_a - > wf - > cbSize = extra_size ;
if ( extra_size > 0 )
2005-01-19 12:12:36 +00:00
memcpy ( ( ( char * ) sh_a - > wf ) + sizeof ( WAVEFORMATEX ) , pack . packet + 142 , extra_size ) ;
2002-03-16 14:32:03 +00:00
2002-09-14 23:21:22 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . samplerate = sh_a - > samplerate ; // * sh_a->channels;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_audio ;
2002-03-16 14:32:03 +00:00
n_audio + + ;
2005-01-21 18:55:01 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: audio (format 0x%04x), -aid %d \n " , ogg_d - > num_sub , sh_a - > format , n_audio - 1 ) ;
2006-03-27 17:25:41 +00:00
if ( mp_msg_test ( MSGT_HEADER , MSGL_V ) ) print_wave_header ( sh_a - > wf , MSGL_V ) ;
2002-03-16 14:32:03 +00:00
} else
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_WARN , " Ogg stream %d contains an old header but the header type is unknown \n " , ogg_d - > num_sub ) ;
2002-03-16 14:32:03 +00:00
// Check new header
} else if ( ( * pack . packet & PACKET_TYPE_BITS ) = = PACKET_TYPE_HEADER & &
pack . bytes > = ( int ) sizeof ( stream_header ) + 1 ) {
stream_header * st = ( stream_header * ) ( pack . packet + 1 ) ;
/// New video header
if ( strncmp ( st - > streamtype , " video " , 5 ) = = 0 ) {
2006-08-26 19:17:04 +00:00
sh_v = new_sh_video_vid ( demuxer , ogg_d - > num_sub , n_video ) ;
2006-07-03 13:21:54 +00:00
sh_v - > bih = calloc ( 1 , sizeof ( BITMAPINFOHEADER ) ) ;
2002-03-31 20:27:52 +00:00
sh_v - > bih - > biSize = sizeof ( BITMAPINFOHEADER ) ;
sh_v - > bih - > biCompression =
2002-03-16 14:32:03 +00:00
sh_v - > format = mmioFOURCC ( st - > subtype [ 0 ] , st - > subtype [ 1 ] ,
st - > subtype [ 2 ] , st - > subtype [ 3 ] ) ;
2002-10-22 15:41:41 +00:00
sh_v - > frametime = get_uint64 ( & st - > time_unit ) * 0.0000001 ;
sh_v - > fps = 1.0 / sh_v - > frametime ;
sh_v - > bih - > biBitCount = get_uint16 ( & st - > bits_per_sample ) ;
sh_v - > disp_w = sh_v - > bih - > biWidth = get_uint32 ( & st - > sh . video . width ) ;
sh_v - > disp_h = sh_v - > bih - > biHeight = get_uint32 ( & st - > sh . video . height ) ;
2002-03-31 20:27:52 +00:00
if ( ! sh_v - > bih - > biBitCount ) sh_v - > bih - > biBitCount = 24 ; // hack, FIXME
sh_v - > bih - > biPlanes = 1 ;
sh_v - > bih - > biSizeImage = ( sh_v - > bih - > biBitCount > > 3 ) * sh_v - > bih - > biWidth * sh_v - > bih - > biHeight ;
2002-03-16 14:32:03 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . samplerate = sh_v - > fps ;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_video ;
2002-03-16 14:32:03 +00:00
n_video + + ;
2005-01-21 18:55:01 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: video (FOURCC %c%c%c%c), -vid %d \n " ,
ogg_d - > num_sub , st - > subtype [ 0 ] , st - > subtype [ 1 ] , st - > subtype [ 2 ] , st - > subtype [ 3 ] , n_video - 1 ) ;
2006-03-27 17:25:41 +00:00
if ( mp_msg_test ( MSGT_HEADER , MSGL_V ) ) print_video_header ( sh_v - > bih , MSGL_V ) ;
2002-03-16 14:32:03 +00:00
/// New audio header
} else if ( strncmp ( st - > streamtype , " audio " , 5 ) = = 0 ) {
char buffer [ 5 ] ;
2002-10-22 15:41:41 +00:00
unsigned int extra_size = get_uint32 ( & st - > size ) - sizeof ( stream_header ) ;
2005-05-01 10:40:42 +00:00
unsigned int extra_offset = 0 ;
2002-03-16 14:32:03 +00:00
memcpy ( buffer , st - > subtype , 4 ) ;
buffer [ 4 ] = ' \0 ' ;
2005-05-01 10:40:42 +00:00
/* Nasty workaround. stream_header.size seems not to contain the real
size in all cases . There are four extra bytes that are unaccounted
for in front of the real codec initialization data _at least_ for
AAC . So far I ' ve only seen those bytes being all 0 , so we can
just skip them here . */
if ( ( strtol ( buffer , NULL , 16 ) = = 0xff ) & & ( extra_size > = 4 ) ) {
extra_size - = 4 ;
extra_offset = 4 ;
}
2006-08-26 19:17:04 +00:00
sh_a = new_sh_audio_aid ( demuxer , ogg_d - > num_sub , n_audio ) ;
2006-07-03 13:21:54 +00:00
sh_a - > wf = calloc ( 1 , sizeof ( WAVEFORMATEX ) + extra_size ) ;
2002-07-30 18:14:02 +00:00
sh_a - > format = sh_a - > wf - > wFormatTag = strtol ( buffer , NULL , 16 ) ;
2002-10-22 15:41:41 +00:00
sh_a - > channels = sh_a - > wf - > nChannels = get_uint16 ( & st - > sh . audio . channels ) ;
sh_a - > samplerate = sh_a - > wf - > nSamplesPerSec = get_uint64 ( & st - > samples_per_unit ) ;
sh_a - > wf - > nAvgBytesPerSec = get_uint32 ( & st - > sh . audio . avgbytespersec ) ;
sh_a - > wf - > nBlockAlign = get_uint16 ( & st - > sh . audio . blockalign ) ;
sh_a - > wf - > wBitsPerSample = get_uint16 ( & st - > bits_per_sample ) ;
2002-03-16 14:32:03 +00:00
sh_a - > samplesize = ( sh_a - > wf - > wBitsPerSample + 7 ) / 8 ;
sh_a - > wf - > cbSize = extra_size ;
if ( extra_size )
2005-05-01 10:40:42 +00:00
memcpy ( ( ( char * ) sh_a - > wf ) + sizeof ( WAVEFORMATEX ) , ( ( char * ) ( st + 1 ) ) + extra_offset , extra_size ) ;
2002-03-16 14:32:03 +00:00
2002-09-14 23:21:22 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . samplerate = sh_a - > samplerate ; // * sh_a->channels;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = n_audio ;
2002-03-16 14:32:03 +00:00
n_audio + + ;
2005-01-21 18:55:01 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: audio (format 0x%04x), -aid %d \n " , ogg_d - > num_sub , sh_a - > format , n_audio - 1 ) ;
2006-03-27 17:25:41 +00:00
if ( mp_msg_test ( MSGT_HEADER , MSGL_V ) ) print_wave_header ( sh_a - > wf , MSGL_V ) ;
2002-03-16 14:32:03 +00:00
/// Check for text (subtitles) header
2002-08-14 23:41:19 +00:00
} else if ( strncmp ( st - > streamtype , " text " , 4 ) = = 0 ) {
2005-01-21 18:55:01 +00:00
mp_msg ( MSGT_DEMUX , MSGL_INFO , " [Ogg] stream %d: subtitles (SRT-like text subtitles), -sid %d \n " , ogg_d - > num_sub , ogg_d - > n_text ) ;
2003-01-04 22:56:56 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . samplerate = get_uint64 ( & st - > time_unit ) / 10 ;
2004-04-02 19:20:34 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . text = 1 ;
2004-11-25 22:24:00 +00:00
ogg_d - > subs [ ogg_d - > num_sub ] . id = ogg_d - > n_text ;
2004-08-24 21:20:24 +00:00
if ( demuxer - > sub - > id = = ogg_d - > n_text )
2004-04-06 06:36:46 +00:00
text_id = ogg_d - > num_sub ;
2006-11-19 13:12:46 +00:00
new_sh_sub ( demuxer , ogg_d - > n_text ) ;
2004-08-24 21:20:24 +00:00
ogg_d - > n_text + + ;
ogg_d - > text_ids = ( int * ) realloc ( ogg_d - > text_ids , sizeof ( int ) * ogg_d - > n_text ) ;
ogg_d - > text_ids [ ogg_d - > n_text - 1 ] = ogg_d - > num_sub ;
2004-09-28 14:00:49 +00:00
ogg_d - > text_langs = ( char * * ) realloc ( ogg_d - > text_langs , sizeof ( char * ) * ogg_d - > n_text ) ;
ogg_d - > text_langs [ ogg_d - > n_text - 1 ] = NULL ;
2003-07-09 01:30:24 +00:00
//// Unknown header type
2002-03-16 14:32:03 +00:00
} else
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Ogg stream %d has a header marker but is of an unknown type \n " , ogg_d - > num_sub ) ;
2003-08-14 12:15:44 +00:00
/// Unknown (invalid ?) header
2002-03-16 14:32:03 +00:00
} else
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Ogg stream %d is of an unknown type \n " , ogg_d - > num_sub ) ;
2002-03-16 14:32:03 +00:00
if ( sh_a | | sh_v ) {
demux_stream_t * ds = NULL ;
if ( sh_a ) {
// If the audio stream is not defined we took the first one
if ( demuxer - > audio - > id = = - 1 ) {
2004-04-06 06:36:46 +00:00
demuxer - > audio - > id = n_audio - 1 ;
2006-03-27 17:25:41 +00:00
// if(sh_a->wf) print_wave_header(sh_a->wf,MSGL_INFO);
2002-03-16 14:32:03 +00:00
}
/// Is it the stream we want
2004-04-06 06:36:46 +00:00
if ( demuxer - > audio - > id = = ( n_audio - 1 ) ) {
2002-05-19 23:25:40 +00:00
demuxer - > audio - > sh = sh_a ;
sh_a - > ds = demuxer - > audio ;
2002-03-16 14:32:03 +00:00
ds = demuxer - > audio ;
2004-04-06 06:36:46 +00:00
audio_id = ogg_d - > num_sub ;
2002-05-23 09:47:18 +00:00
}
2002-03-16 14:32:03 +00:00
}
if ( sh_v ) {
/// Also for video
if ( demuxer - > video - > id = = - 1 ) {
2004-04-06 06:36:46 +00:00
demuxer - > video - > id = n_video - 1 ;
2006-03-27 17:25:41 +00:00
// if(sh_v->bih) print_video_header(sh_v->bih,MSGL_INFO);
2002-03-16 14:32:03 +00:00
}
2004-04-06 06:36:46 +00:00
if ( demuxer - > video - > id = = ( n_video - 1 ) ) {
2002-05-19 23:25:40 +00:00
demuxer - > video - > sh = sh_v ;
sh_v - > ds = demuxer - > video ;
2002-03-16 14:32:03 +00:00
ds = demuxer - > video ;
2004-04-06 06:36:46 +00:00
video_id = ogg_d - > num_sub ;
2002-05-23 09:47:18 +00:00
}
2002-03-16 14:32:03 +00:00
}
2002-04-20 17:50:11 +00:00
/// Add the header packets if the stream isn't seekable
2003-12-08 09:47:15 +00:00
if ( ds & & ! s - > end_pos ) {
2002-04-20 17:50:11 +00:00
/// Finish the page, otherwise packets will be lost
do {
2004-04-02 19:20:34 +00:00
demux_ogg_add_packet ( ds , & ogg_d - > subs [ ogg_d - > num_sub ] , ogg_d - > num_sub , & pack ) ;
2002-04-20 17:50:11 +00:00
} while ( ogg_stream_packetout ( & ogg_d - > subs [ ogg_d - > num_sub ] . stream , & pack ) = = 1 ) ;
}
2002-03-16 14:32:03 +00:00
}
ogg_d - > num_sub + + ;
}
2004-04-23 16:31:30 +00:00
if ( ! n_video & & ! n_audio ) {
2005-02-06 10:30:10 +00:00
goto err_out ;
2004-04-23 16:31:30 +00:00
}
2004-04-06 06:36:46 +00:00
if ( ! n_video | | ( video_id < 0 ) )
2002-03-16 14:32:03 +00:00
demuxer - > video - > id = - 2 ;
2004-04-06 06:36:46 +00:00
else
demuxer - > video - > id = video_id ;
if ( ! n_audio | | ( audio_id < 0 ) )
2002-03-16 14:32:03 +00:00
demuxer - > audio - > id = - 2 ;
2004-04-06 06:36:46 +00:00
else
demuxer - > audio - > id = audio_id ;
2004-04-29 21:01:09 +00:00
/* Disable the subs only if there are no text streams at all.
Otherwise the stream to display might be chosen later when the comment
packet is encountered and the user used - slang instead of - sid . */
2004-08-24 21:20:24 +00:00
if ( ! ogg_d - > n_text )
2002-08-14 23:41:19 +00:00
demuxer - > sub - > id = - 2 ;
2004-05-05 21:05:36 +00:00
else if ( text_id > = 0 ) {
2004-04-06 06:36:46 +00:00
demuxer - > sub - > id = text_id ;
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " Ogg demuxer: Displaying subtitle stream id %d \n " , text_id ) ;
2004-05-05 21:05:36 +00:00
}
2002-03-16 14:32:03 +00:00
2003-12-08 09:47:15 +00:00
ogg_d - > final_granulepos = 0 ;
2002-04-20 17:50:11 +00:00
if ( ! s - > end_pos )
demuxer - > seekable = 0 ;
else {
2003-01-05 13:48:13 +00:00
demuxer - > movi_start = s - > start_pos ; // Needed for XCD (Ogg written in MODE2)
2002-04-20 17:50:11 +00:00
demuxer - > movi_end = s - > end_pos ;
2002-04-24 12:28:03 +00:00
demuxer - > seekable = 1 ;
2003-12-08 09:47:15 +00:00
demux_ogg_scan_stream ( demuxer ) ;
2002-04-20 17:50:11 +00:00
}
2004-08-24 21:20:24 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " Ogg demuxer : found %d audio stream%s, %d video stream%s and %d text stream%s \n " , n_audio , n_audio > 1 ? " s " : " " , n_video , n_video > 1 ? " s " : " " , ogg_d - > n_text , ogg_d - > n_text > 1 ? " s " : " " ) ;
2005-02-27 18:11:17 +00:00
2005-05-12 23:27:22 +00:00
sh_a = demuxer - > audio - > sh ;
2005-05-12 23:01:52 +00:00
if ( sh_a )
if ( sh_a - > format = = FOURCC_VORBIS )
2006-05-10 22:54:24 +00:00
fixup_vorbis_wf ( sh_a , ogg_d ) ;
2005-05-12 17:35:58 +00:00
2005-08-05 19:57:47 +00:00
return DEMUXER_TYPE_OGG ;
2005-02-06 10:30:10 +00:00
err_out :
return 0 ;
2002-03-16 14:32:03 +00:00
}
2005-08-05 19:57:47 +00:00
static int demux_ogg_fill_buffer ( demuxer_t * d , demux_stream_t * dsds ) {
2002-03-16 14:32:03 +00:00
ogg_demuxer_t * ogg_d ;
stream_t * s ;
demux_stream_t * ds ;
ogg_sync_state * sync ;
ogg_stream_state * os ;
ogg_page * page ;
ogg_packet pack ;
int np = 0 , id = 0 ;
s = d - > stream ;
ogg_d = d - > priv ;
sync = & ogg_d - > sync ;
page = & ogg_d - > page ;
/// Find the stream we are working on
if ( ( id = demux_ogg_get_page_stream ( ogg_d , & os ) ) < 0 ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Ogg demuxer : can't get current stream \n " ) ;
2002-03-16 14:32:03 +00:00
return 0 ;
}
while ( 1 ) {
np = 0 ;
ds = NULL ;
/// Try to get some packet from the current page
while ( ( np = ogg_stream_packetout ( os , & pack ) ) ! = 1 ) {
/// No packet we go the next page
if ( np = = 0 ) {
while ( 1 ) {
int pa , len ;
char * buf ;
2002-04-24 12:28:03 +00:00
ogg_d - > pos + = ogg_d - > last_size ;
2002-03-16 14:32:03 +00:00
/// Get the next page from the physical stream
2002-04-24 12:28:03 +00:00
while ( ( pa = ogg_sync_pageseek ( sync , page ) ) < = 0 ) {
2002-03-16 14:32:03 +00:00
/// Error : we skip some bytes
if ( pa < 0 ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_WARN , " Ogg : Page out not synced, we skip some bytes \n " ) ;
2002-04-24 12:28:03 +00:00
ogg_d - > pos - = pa ;
2002-03-16 14:32:03 +00:00
continue ;
}
/// We need more data
buf = ogg_sync_buffer ( sync , BLOCK_SIZE ) ;
len = stream_read ( s , buf , BLOCK_SIZE ) ;
if ( len = = 0 & & s - > eof ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_DBG2 , " Ogg : Stream EOF !!!! \n " ) ;
2002-03-16 14:32:03 +00:00
return 0 ;
}
ogg_sync_wrote ( sync , len ) ;
} /// Page loop
2002-04-24 12:28:03 +00:00
ogg_d - > last_size = pa ;
2002-03-16 14:32:03 +00:00
/// Find the page's logical stream
if ( ( id = demux_ogg_get_page_stream ( ogg_d , & os ) ) < 0 ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Ogg demuxer error : we met an unknown stream \n " ) ;
2002-03-16 14:32:03 +00:00
return 0 ;
}
/// Take the page
if ( ogg_stream_pagein ( os , page ) = = 0 )
break ;
/// Page was invalid => retry
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_WARN , " Ogg demuxer : got invalid page !!!!! \n " ) ;
2002-04-24 12:28:03 +00:00
ogg_d - > pos + = ogg_d - > last_size ;
2002-03-16 14:32:03 +00:00
}
} else /// Packet was corrupted
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_WARN , " Ogg : bad packet in stream %d \n " , id ) ;
2002-03-16 14:32:03 +00:00
} /// Packet loop
/// Is the actual logical stream in use ?
if ( id = = d - > audio - > id )
ds = d - > audio ;
else if ( id = = d - > video - > id )
ds = d - > video ;
2004-04-02 19:20:34 +00:00
else if ( ogg_d - > subs [ id ] . text )
2002-08-14 23:41:19 +00:00
ds = d - > sub ;
2002-03-16 14:32:03 +00:00
if ( ds ) {
2004-04-02 19:20:34 +00:00
if ( ! demux_ogg_add_packet ( ds , & ogg_d - > subs [ id ] , id , & pack ) )
2002-03-16 14:32:03 +00:00
continue ; /// Unuseful packet, get another
2002-04-24 12:28:03 +00:00
d - > filepos = ogg_d - > pos ;
2002-03-16 14:32:03 +00:00
return 1 ;
}
} /// while(1)
}
/// For avi with Ogg audio stream we have to create an ogg demuxer for this
// stream, then we join the avi and ogg demuxer with a demuxers demuxer
demuxer_t * init_avi_with_ogg ( demuxer_t * demuxer ) {
demuxer_t * od ;
ogg_demuxer_t * ogg_d ;
stream_t * s ;
uint32_t hdrsizes [ 3 ] ;
demux_packet_t * dp ;
sh_audio_t * sh_audio = demuxer - > audio - > sh ;
int np ;
unsigned char * p = NULL , * buf ;
int plen ;
/// Check that the cbSize is enouth big for the following reads
2003-01-29 21:53:56 +00:00
if ( sh_audio - > wf - > cbSize < 22 + 3 * sizeof ( uint32_t ) ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " AVI Ogg : Initial audio header is too small !!!!! \n " ) ;
2002-03-16 14:32:03 +00:00
goto fallback ;
}
/// Get the size of the 3 header packet
2003-01-29 21:53:56 +00:00
memcpy ( hdrsizes , ( ( unsigned char * ) sh_audio - > wf ) + 22 + sizeof ( WAVEFORMATEX ) , 3 * sizeof ( uint32_t ) ) ;
// printf("\n!!!!!! hdr sizes: %d %d %d \n",hdrsizes[0],hdrsizes[1],hdrsizes[2]);
2002-03-16 14:32:03 +00:00
/// Check the size
2003-01-29 21:53:56 +00:00
if ( sh_audio - > wf - > cbSize < 22 + 3 * sizeof ( uint32_t ) + hdrsizes [ 0 ] + hdrsizes [ 1 ] + hdrsizes [ 2 ] ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " AVI Ogg : Audio header is too small !!!!! \n " ) ;
2002-03-16 14:32:03 +00:00
goto fallback ;
}
// Build the ogg demuxer private datas
2006-07-03 13:21:54 +00:00
ogg_d = calloc ( 1 , sizeof ( ogg_demuxer_t ) ) ;
2002-03-16 14:32:03 +00:00
ogg_d - > num_sub = 1 ;
2006-07-03 13:21:54 +00:00
ogg_d - > subs = malloc ( sizeof ( ogg_stream_t ) ) ;
2002-04-24 17:28:23 +00:00
ogg_d - > subs [ 0 ] . vorbis = 1 ;
2002-03-16 14:32:03 +00:00
// Init the ogg physical stream
ogg_sync_init ( & ogg_d - > sync ) ;
// Get the first page of the stream : we assume there only 1 logical stream
while ( ( np = ogg_sync_pageout ( & ogg_d - > sync , & ogg_d - > page ) ) < = 0 ) {
if ( np < 0 ) {
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_ERR , " AVI Ogg error : Can't init using first stream packets \n " ) ;
2002-03-16 14:32:03 +00:00
free ( ogg_d ) ;
goto fallback ;
}
// Add some data
plen = ds_get_packet ( demuxer - > audio , & p ) ;
buf = ogg_sync_buffer ( & ogg_d - > sync , plen ) ;
memcpy ( buf , p , plen ) ;
ogg_sync_wrote ( & ogg_d - > sync , plen ) ;
}
// Init the logical stream
2004-05-07 08:31:39 +00:00
mp_msg ( MSGT_DEMUX , MSGL_DBG2 , " AVI Ogg found page with serial %d \n " , ogg_page_serialno ( & ogg_d - > page ) ) ;
2002-03-16 14:32:03 +00:00
ogg_stream_init ( & ogg_d - > subs [ 0 ] . stream , ogg_page_serialno ( & ogg_d - > page ) ) ;
// Write the page
ogg_stream_pagein ( & ogg_d - > subs [ 0 ] . stream , & ogg_d - > page ) ;
// Create the ds_stream and the ogg demuxer
s = new_ds_stream ( demuxer - > audio ) ;
2005-08-05 19:57:47 +00:00
od = new_demuxer ( s , DEMUXER_TYPE_OGG , 0 , - 2 , - 2 , NULL ) ;
2002-03-16 14:32:03 +00:00
/// Add the header packets in the ogg demuxer audio stream
// Initial header
dp = new_demux_packet ( hdrsizes [ 0 ] ) ;
2003-01-29 21:53:56 +00:00
memcpy ( dp - > buffer , ( ( unsigned char * ) sh_audio - > wf ) + 22 + sizeof ( WAVEFORMATEX ) + 3 * sizeof ( uint32_t ) , hdrsizes [ 0 ] ) ;
2002-03-16 14:32:03 +00:00
ds_add_packet ( od - > audio , dp ) ;
/// Comments
dp = new_demux_packet ( hdrsizes [ 1 ] ) ;
2003-01-29 21:53:56 +00:00
memcpy ( dp - > buffer , ( ( unsigned char * ) sh_audio - > wf ) + 22 + sizeof ( WAVEFORMATEX ) + 3 * sizeof ( uint32_t ) + hdrsizes [ 0 ] , hdrsizes [ 1 ] ) ;
2002-03-16 14:32:03 +00:00
ds_add_packet ( od - > audio , dp ) ;
/// Code book
dp = new_demux_packet ( hdrsizes [ 2 ] ) ;
2003-01-29 21:53:56 +00:00
memcpy ( dp - > buffer , ( ( unsigned char * ) sh_audio - > wf ) + 22 + sizeof ( WAVEFORMATEX ) + 3 * sizeof ( uint32_t ) + hdrsizes [ 0 ] + hdrsizes [ 1 ] , hdrsizes [ 2 ] ) ;
2002-03-16 14:32:03 +00:00
ds_add_packet ( od - > audio , dp ) ;
// Finish setting up the ogg demuxer
od - > priv = ogg_d ;
sh_audio = new_sh_audio ( od , 0 ) ;
od - > audio - > id = 0 ;
od - > video - > id = - 2 ;
od - > audio - > sh = sh_audio ;
sh_audio - > ds = od - > audio ;
2005-02-27 18:11:17 +00:00
sh_audio - > format = FOURCC_VORBIS ;
2002-03-16 14:32:03 +00:00
/// Return the joined demuxers
return new_demuxers_demuxer ( demuxer , od , demuxer ) ;
fallback :
demuxer - > audio - > id = - 2 ;
return demuxer ;
}
2006-02-17 01:57:41 +00:00
static void demux_ogg_seek ( demuxer_t * demuxer , float rel_seek_secs , float audio_delay , int flags ) {
2002-04-20 17:50:11 +00:00
ogg_demuxer_t * ogg_d = demuxer - > priv ;
ogg_sync_state * sync = & ogg_d - > sync ;
ogg_page * page = & ogg_d - > page ;
ogg_stream_state * oss ;
ogg_stream_t * os ;
demux_stream_t * ds ;
ogg_packet op ;
2002-04-24 12:28:03 +00:00
float rate ;
2004-04-23 16:31:30 +00:00
int i , sp , first , precision = 1 , do_seek = 1 ;
2002-04-20 17:50:11 +00:00
vorbis_info * vi = NULL ;
2004-04-23 16:31:30 +00:00
int64_t gp = 0 , old_gp ;
void * context = NULL ;
off_t pos , old_pos ;
2004-04-23 22:30:18 +00:00
int np ;
int is_gp_valid ;
float pts ;
int is_keyframe ;
2004-06-27 13:55:40 +00:00
int samplesize = 1 ;
2006-09-12 09:00:10 +00:00
ogg_int64_t granulepos_orig ;
2002-04-20 17:50:11 +00:00
if ( demuxer - > video - > id > = 0 ) {
ds = demuxer - > video ;
2004-04-23 16:31:30 +00:00
/* demux_ogg_read_packet needs decoder context for Theora streams */
2005-02-27 18:11:17 +00:00
if ( ( ( sh_video_t * ) demuxer - > video - > sh ) - > format = = FOURCC_THEORA )
2004-04-23 16:31:30 +00:00
context = ( ( sh_video_t * ) demuxer - > video - > sh ) - > context ;
2002-04-20 17:50:11 +00:00
rate = ogg_d - > subs [ ds - > id ] . samplerate ;
} else {
ds = demuxer - > audio ;
2006-05-10 22:54:24 +00:00
os = & ogg_d - > subs [ ds - > id ] ;
2004-04-23 16:31:30 +00:00
/* demux_ogg_read_packet needs decoder context for Vorbis streams */
2005-02-27 18:11:17 +00:00
if ( ( ( sh_audio_t * ) demuxer - > audio - > sh ) - > format = = FOURCC_VORBIS )
2004-04-23 16:31:30 +00:00
context = ( ( sh_audio_t * ) demuxer - > audio - > sh ) - > context ;
2006-12-07 20:53:05 +00:00
vi = & ( os - > vi ) ;
2002-04-20 17:50:11 +00:00
rate = ( float ) vi - > rate ;
2004-06-27 13:55:40 +00:00
samplesize = ( ( sh_audio_t * ) ds - > sh ) - > samplesize ;
2002-04-20 17:50:11 +00:00
}
os = & ogg_d - > subs [ ds - > id ] ;
oss = & os - > stream ;
2004-04-23 16:31:30 +00:00
old_gp = os - > lastpos ;
old_pos = ogg_d - > pos ;
//calculate the granulepos to seek to
2004-01-31 09:38:01 +00:00
gp = flags & 1 ? 0 : os - > lastpos ;
2004-04-23 16:31:30 +00:00
if ( flags & 2 ) {
if ( ogg_d - > final_granulepos > 0 )
gp + = ogg_d - > final_granulepos * rel_seek_secs ;
2002-04-24 12:28:03 +00:00
else
2004-04-23 16:31:30 +00:00
gp + = rel_seek_secs * ( demuxer - > movi_end - demuxer - > movi_start ) * os - > lastpos / ogg_d - > pos ;
} else
2004-01-31 09:38:01 +00:00
gp + = rel_seek_secs * rate ;
2004-04-23 16:31:30 +00:00
if ( gp < 0 ) gp = 0 ;
2002-04-20 17:50:11 +00:00
2004-04-23 16:31:30 +00:00
//calculate the filepos to seek to
if ( ogg_d - > syncpoints ) {
2002-04-24 12:28:03 +00:00
for ( sp = 0 ; sp < ogg_d - > num_syncpoint ; sp + + ) {
2004-01-31 09:38:01 +00:00
if ( ogg_d - > syncpoints [ sp ] . granulepos > = gp ) break ;
2002-04-24 12:28:03 +00:00
}
2002-04-20 17:50:11 +00:00
2004-04-23 16:31:30 +00:00
if ( sp > = ogg_d - > num_syncpoint ) return ;
if ( sp > 0 & & ogg_d - > syncpoints [ sp ] . granulepos - gp > gp - ogg_d - > syncpoints [ sp - 1 ] . granulepos )
sp - - ;
if ( ogg_d - > syncpoints [ sp ] . granulepos = = os - > lastpos ) {
if ( sp > 0 & & gp < os - > lastpos ) sp - - ;
if ( sp < ogg_d - > num_syncpoint - 1 & & gp > os - > lastpos ) sp + + ;
}
2002-04-24 12:28:03 +00:00
pos = ogg_d - > syncpoints [ sp ] . page_pos ;
2004-04-23 16:31:30 +00:00
precision = 0 ;
2002-04-24 12:28:03 +00:00
} else {
2003-01-05 13:48:13 +00:00
pos = flags & 1 ? 0 : ogg_d - > pos ;
2002-04-24 12:28:03 +00:00
if ( flags & 2 )
pos + = ( demuxer - > movi_end - demuxer - > movi_start ) * rel_seek_secs ;
2004-01-31 09:38:01 +00:00
else {
2004-04-23 16:31:30 +00:00
if ( ogg_d - > final_granulepos > 0 ) {
2004-01-31 09:38:01 +00:00
pos + = rel_seek_secs * ( demuxer - > movi_end - demuxer - > movi_start ) / ( ogg_d - > final_granulepos / rate ) ;
2004-04-23 16:31:30 +00:00
} else if ( os - > lastpos > 0 ) {
2002-04-24 12:28:03 +00:00
pos + = rel_seek_secs * ogg_d - > pos / ( os - > lastpos / rate ) ;
2004-01-31 09:38:01 +00:00
}
2002-04-24 12:28:03 +00:00
}
2004-04-23 16:31:30 +00:00
if ( pos < 0 ) pos = 0 ;
2005-11-01 16:12:53 +00:00
if ( pos > ( demuxer - > movi_end - demuxer - > movi_start ) )
pos = demuxer - > movi_end - demuxer - > movi_start ;
2004-04-23 16:31:30 +00:00
} // if(ogg_d->syncpoints)
2002-04-20 17:50:11 +00:00
2004-04-23 16:31:30 +00:00
while ( 1 ) {
if ( do_seek ) {
2003-01-05 13:48:13 +00:00
stream_seek ( demuxer - > stream , pos + demuxer - > movi_start ) ;
2002-04-20 17:50:11 +00:00
ogg_sync_reset ( sync ) ;
2002-04-24 12:28:03 +00:00
for ( i = 0 ; i < ogg_d - > num_sub ; i + + ) {
2002-04-20 17:50:11 +00:00
ogg_stream_reset ( & ogg_d - > subs [ i ] . stream ) ;
2002-04-24 12:28:03 +00:00
ogg_d - > subs [ i ] . lastpos = ogg_d - > subs [ i ] . lastsize = 0 ;
}
ogg_d - > pos = pos ;
ogg_d - > last_size = 0 ;
2004-04-23 16:31:30 +00:00
/* we just guess that we reached correct granulepos, in case a
subsequent search occurs before we read a valid granulepos */
os - > lastpos = gp ;
first = ! ( ogg_d - > syncpoints ) ;
do_seek = 0 ;
}
2002-04-24 12:28:03 +00:00
ogg_d - > pos + = ogg_d - > last_size ;
ogg_d - > last_size = 0 ;
np = ogg_sync_pageseek ( sync , page ) ;
if ( np < 0 )
ogg_d - > pos - = np ;
2002-04-20 17:50:11 +00:00
if ( np < = 0 ) { // We need more data
char * buf = ogg_sync_buffer ( sync , BLOCK_SIZE ) ;
int len = stream_read ( demuxer - > stream , buf , BLOCK_SIZE ) ;
if ( len = = 0 & & demuxer - > stream - > eof ) {
2005-11-01 16:12:53 +00:00
mp_msg ( MSGT_DEMUX , MSGL_V , " EOF while trying to seek !!!! \n " ) ;
return ;
2002-04-20 17:50:11 +00:00
}
ogg_sync_wrote ( sync , len ) ;
continue ;
}
2002-04-24 12:28:03 +00:00
ogg_d - > last_size = np ;
if ( ogg_page_serialno ( page ) ! = oss - > serialno )
2002-04-20 17:50:11 +00:00
continue ;
2002-04-24 12:28:03 +00:00
2002-04-20 17:50:11 +00:00
if ( ogg_stream_pagein ( oss , page ) ! = 0 )
continue ;
2002-04-24 12:28:03 +00:00
while ( 1 ) {
np = ogg_stream_packetout ( oss , & op ) ;
if ( np < 0 )
2002-04-20 17:50:11 +00:00
continue ;
2002-04-24 12:28:03 +00:00
else if ( np = = 0 )
2002-04-20 17:50:11 +00:00
break ;
2002-12-30 08:59:33 +00:00
if ( first ) { /* Discard the first packet as it's probably broken,
and we don ' t have any other means to decide whether it is
complete or not . */
first = 0 ;
break ;
}
2004-04-23 22:30:18 +00:00
is_gp_valid = ( op . granulepos > = 0 ) ;
2006-09-12 09:00:10 +00:00
granulepos_orig = op . granulepos ;
2004-06-27 13:55:40 +00:00
demux_ogg_read_packet ( os , & op , context , & pts , & is_keyframe , samplesize ) ;
2004-04-23 16:31:30 +00:00
if ( precision & & is_gp_valid ) {
precision - - ;
if ( abs ( gp - op . granulepos ) > rate & & ( op . granulepos ! = old_gp ) ) {
//prepare another seek because we are off by more than 1s
pos + = ( gp - op . granulepos ) * ( pos - old_pos ) / ( op . granulepos - old_gp ) ;
if ( pos < 0 ) pos = 0 ;
if ( pos < ( demuxer - > movi_end - demuxer - > movi_start ) ) {
do_seek = 1 ;
break ;
}
}
}
if ( is_gp_valid & & ( pos > 0 ) & & ( old_gp > gp )
& & ( 2 * ( old_gp - op . granulepos ) < old_gp - gp ) ) {
/* prepare another seek because looking for a syncpoint
destroyed the backward search */
pos = old_pos - 1.5 * ( old_pos - pos ) ;
if ( pos < 0 ) pos = 0 ;
if ( pos < ( demuxer - > movi_end - demuxer - > movi_start ) ) {
do_seek = 1 ;
break ;
}
}
2005-11-05 11:58:25 +00:00
if ( ! precision & & ( is_keyframe | | os - > vorbis | | os - > speex ) ) {
2007-01-06 19:19:15 +00:00
if ( sub_clear_text ( & ogg_sub , MP_NOPTS_VALUE ) ) {
vo_sub = & ogg_sub ;
vo_osd_changed ( OSDTYPE_SUBTITLE ) ;
}
2006-09-12 09:00:10 +00:00
op . granulepos = granulepos_orig ;
2004-04-02 19:20:34 +00:00
demux_ogg_add_packet ( ds , os , ds - > id , & op ) ;
2002-04-24 12:28:03 +00:00
return ;
2002-04-20 17:50:11 +00:00
}
2002-04-24 12:28:03 +00:00
}
2002-04-20 17:50:11 +00:00
}
mp_msg ( MSGT_DEMUX , MSGL_ERR , " Can't find the good packet :( \n " ) ;
}
2005-08-05 19:57:47 +00:00
static void demux_close_ogg ( demuxer_t * demuxer ) {
2002-04-24 17:28:23 +00:00
ogg_demuxer_t * ogg_d = demuxer - > priv ;
2006-12-07 20:53:05 +00:00
ogg_stream_t * os = NULL ;
2004-09-28 14:00:49 +00:00
int i ;
2002-04-24 17:28:23 +00:00
if ( ! ogg_d )
return ;
2002-12-28 15:19:41 +00:00
# ifdef USE_ICONV
subcp_close ( ) ;
# endif
2004-10-14 15:09:35 +00:00
ogg_sync_clear ( & ogg_d - > sync ) ;
2002-04-24 17:28:23 +00:00
if ( ogg_d - > subs )
2004-10-14 15:09:35 +00:00
{
for ( i = 0 ; i < ogg_d - > num_sub ; i + + )
2007-01-05 18:02:18 +00:00
{
os = & ogg_d - > subs [ i ] ;
2007-01-05 18:03:58 +00:00
ogg_stream_clear ( & os - > stream ) ;
2007-01-05 18:02:18 +00:00
if ( os - > vi_inited )
vorbis_info_clear ( & os - > vi ) ;
}
2002-04-24 17:28:23 +00:00
free ( ogg_d - > subs ) ;
2004-10-14 15:09:35 +00:00
}
2002-04-24 17:28:23 +00:00
if ( ogg_d - > syncpoints )
free ( ogg_d - > syncpoints ) ;
2004-08-24 21:20:24 +00:00
if ( ogg_d - > text_ids )
free ( ogg_d - > text_ids ) ;
2004-09-28 14:00:49 +00:00
if ( ogg_d - > text_langs ) {
for ( i = 0 ; i < ogg_d - > n_text ; i + + )
if ( ogg_d - > text_langs [ i ] ) free ( ogg_d - > text_langs [ i ] ) ;
free ( ogg_d - > text_langs ) ;
}
2002-04-24 17:28:23 +00:00
free ( ogg_d ) ;
}
2005-08-05 19:57:47 +00:00
static int demux_ogg_control ( demuxer_t * demuxer , int cmd , void * arg ) {
2003-12-08 09:47:15 +00:00
ogg_demuxer_t * ogg_d = demuxer - > priv ;
ogg_stream_t * os ;
float rate ;
2003-12-08 11:11:32 +00:00
2003-12-08 09:47:15 +00:00
if ( demuxer - > video - > id > = 0 ) {
os = & ogg_d - > subs [ demuxer - > video - > id ] ;
rate = os - > samplerate ;
} else {
os = & ogg_d - > subs [ demuxer - > audio - > id ] ;
2006-12-07 20:53:05 +00:00
rate = os - > vi . rate ;
2003-12-08 09:47:15 +00:00
}
switch ( cmd ) {
case DEMUXER_CTRL_GET_TIME_LENGTH :
if ( ogg_d - > final_granulepos < = 0 ) return DEMUXER_CTRL_DONTKNOW ;
2005-09-02 08:32:32 +00:00
* ( ( double * ) arg ) = ( double ) ogg_d - > final_granulepos / rate ;
2003-12-08 09:47:15 +00:00
return DEMUXER_CTRL_GUESS ;
case DEMUXER_CTRL_GET_PERCENT_POS :
if ( ogg_d - > final_granulepos < = 0 ) return DEMUXER_CTRL_DONTKNOW ;
* ( ( int * ) arg ) = ( int ) ( ( os - > lastpos * 100 ) / ogg_d - > final_granulepos ) ;
return DEMUXER_CTRL_OK ;
default :
return DEMUXER_CTRL_NOTIMPL ;
}
}
2005-08-05 19:57:47 +00:00
demuxer_desc_t demuxer_desc_ogg = {
" Ogg demuxer " ,
" ogg " ,
" Ogg " ,
" ? " ,
" " ,
DEMUXER_TYPE_OGG ,
1 , // safe autodetect
demux_ogg_open ,
demux_ogg_fill_buffer ,
NULL ,
demux_close_ogg ,
demux_ogg_seek ,
demux_ogg_control
} ;