ogg.c 53.7 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1
/*****************************************************************************
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
2
 * ogg.c : ogg stream demux module for vlc
Gildas Bazin's avatar
 
Gildas Bazin committed
3
 *****************************************************************************
4
 * Copyright (C) 2001-2003 the VideoLAN team
5
 * $Id$
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7 8
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
 *          Andre Pang <Andre.Pang@csiro.au> (Annodex support)
Sam Hocevar's avatar
Sam Hocevar committed
9
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
10 11 12 13
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
Sam Hocevar's avatar
Sam Hocevar committed
14
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
15 16 17 18 19 20 21
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
Gildas Bazin's avatar
 
Gildas Bazin committed
23 24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
Clément Stenac's avatar
Clément Stenac committed
29 30
#include <vlc_input.h>
#include <vlc_demux.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
31 32 33

#include <ogg/ogg.h>

Clément Stenac's avatar
Clément Stenac committed
34 35
#include <vlc_codecs.h>
#include <vlc_bits.h>
Gildas Bazin's avatar
 
Gildas Bazin committed
36

37 38 39 40 41 42 43
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

vlc_module_begin();
Clément Stenac's avatar
Clément Stenac committed
44 45
    set_shortname ( "OGG" );
    set_description( _("OGG demuxer" ) );
Clément Stenac's avatar
Clément Stenac committed
46 47
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_DEMUX );
48
    set_capability( "demux2", 50 );
49 50 51 52
    set_callbacks( Open, Close );
    add_shortcut( "ogg" );
vlc_module_end();

Gildas Bazin's avatar
 
Gildas Bazin committed
53 54

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
55
 * Definitions of structures and functions used by this plugins
Gildas Bazin's avatar
 
Gildas Bazin committed
56 57 58 59 60
 *****************************************************************************/
typedef struct logical_stream_s
{
    ogg_stream_state os;                        /* logical stream of packets */

Gildas Bazin's avatar
 
Gildas Bazin committed
61 62 63
    es_format_t      fmt;
    es_out_id_t      *p_es;
    double           f_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
64

Gildas Bazin's avatar
 
Gildas Bazin committed
65
    int              i_serial_no;
Gildas Bazin's avatar
 
Gildas Bazin committed
66

Gildas Bazin's avatar
 
Gildas Bazin committed
67 68 69 70 71
    /* the header of some logical streams (eg vorbis) contain essential
     * data for the decoder. We back them up here in case we need to re-feed
     * them to the decoder. */
    int              b_force_backup;
    int              i_packets_backup;
72 73
    uint8_t          *p_headers;
    int              i_headers;
Gildas Bazin's avatar
 
Gildas Bazin committed
74

Gildas Bazin's avatar
 
Gildas Bazin committed
75 76
    /* program clock reference (in units of 90kHz) derived from the previous
     * granulepos */
Gildas Bazin's avatar
 
Gildas Bazin committed
77
    mtime_t          i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
78
    mtime_t          i_interpolated_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
79
    mtime_t          i_previous_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
80

Gildas Bazin's avatar
 
Gildas Bazin committed
81
    /* Misc */
Gildas Bazin's avatar
 
Gildas Bazin committed
82
    int b_reinit;
Gildas Bazin's avatar
 
Gildas Bazin committed
83
    int i_theora_keyframe_granule_shift;
Gildas Bazin's avatar
 
Gildas Bazin committed
84

85 86 87
    /* for Annodex logical bitstreams */
    int secondary_header_packets;

Gildas Bazin's avatar
 
Gildas Bazin committed
88 89 90 91 92 93 94 95 96
} logical_stream_t;

struct demux_sys_t
{
    ogg_sync_state oy;        /* sync and verify incoming physical bitstream */

    int i_streams;                           /* number of logical bitstreams */
    logical_stream_t **pp_stream;  /* pointer to an array of logical streams */

Gildas Bazin's avatar
 
Gildas Bazin committed
97
    /* program clock reference (in units of 90kHz) derived from the pcr of
Gildas Bazin's avatar
 
Gildas Bazin committed
98
     * the sub-streams */
Gildas Bazin's avatar
 
Gildas Bazin committed
99
    mtime_t i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
100 101

    /* stream state */
Gildas Bazin's avatar
 
Gildas Bazin committed
102
    int     i_eos;
103 104 105

    /* bitrate */
    int     i_bitrate;
Gildas Bazin's avatar
 
Gildas Bazin committed
106 107
};

Gildas Bazin's avatar
 
Gildas Bazin committed
108 109 110 111 112 113
/* OggDS headers for the new header format (used in ogm files) */
typedef struct stream_header_video
{
    ogg_int32_t width;
    ogg_int32_t height;
} stream_header_video;
Sam Hocevar's avatar
Sam Hocevar committed
114

Gildas Bazin's avatar
 
Gildas Bazin committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
typedef struct stream_header_audio
{
    ogg_int16_t channels;
    ogg_int16_t blockalign;
    ogg_int32_t avgbytespersec;
} stream_header_audio;

typedef struct stream_header
{
    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;

    union
    {
        /* Video specific */
        stream_header_video video;
        /* Audio specific */
        stream_header_audio audio;
    } sh;
} stream_header;

145 146
#define OGG_BLOCK_SIZE 4096

Gildas Bazin's avatar
 
Gildas Bazin committed
147 148 149 150 151 152 153
/* 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

Gildas Bazin's avatar
 
Gildas Bazin committed
154 155 156
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
157 158
static int  Demux  ( demux_t * );
static int  Control( demux_t *, int, va_list );
Gildas Bazin's avatar
 
Gildas Bazin committed
159 160

/* Bitstream manipulation */
161
static int  Ogg_ReadPage     ( demux_t *, ogg_page * );
Gildas Bazin's avatar
 
Gildas Bazin committed
162
static void Ogg_UpdatePCR    ( logical_stream_t *, ogg_packet * );
163
static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
Gildas Bazin's avatar
 
Gildas Bazin committed
164

165 166 167
static int Ogg_BeginningOfStream( demux_t *p_demux );
static int Ogg_FindLogicalStreams( demux_t *p_demux );
static void Ogg_EndOfStream( demux_t *p_demux );
Gildas Bazin's avatar
 
Gildas Bazin committed
168

169
/* Logical bitstream headers */
170 171 172 173 174
static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
175

Gildas Bazin's avatar
 
Gildas Bazin committed
176
/*****************************************************************************
177
 * Open: initializes ogg demux structures
Gildas Bazin's avatar
 
Gildas Bazin committed
178
 *****************************************************************************/
179
static int Open( vlc_object_t * p_this )
Gildas Bazin's avatar
 
Gildas Bazin committed
180
{
181
    demux_t *p_demux = (demux_t *)p_this;
182 183
    demux_sys_t    *p_sys;
    uint8_t        *p_peek;
Gildas Bazin's avatar
 
Gildas Bazin committed
184

185 186

    /* Check if we are dealing with an ogg stream */
187
    if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
188
    if( strcmp( p_demux->psz_demux, "ogg" ) && memcmp( p_peek, "OggS", 4 ) )
189 190 191
    {
        return VLC_EGENERIC;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
192

193
    /* Set exported functions */
194 195 196
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
197 198

    memset( p_sys, 0, sizeof( demux_sys_t ) );
199
    p_sys->i_bitrate = 0;
200 201 202 203 204
    p_sys->pp_stream = NULL;

    /* Begnning of stream, tell the demux to look for elementary streams. */
    p_sys->i_eos = 0;

205 206 207 208 209 210

    if( ((input_thread_t* )(p_demux->p_parent ))->b_preparsing == VLC_TRUE )
    {
        module_t *p_meta = module_Need( p_demux, "meta reader", NULL, 0 );
        if( p_meta )
        {
Clément Stenac's avatar
Clément Stenac committed
211
            vlc_meta_Merge( input_GetItem((input_thread_t* )(p_demux->p_parent ))->p_meta,
212 213 214 215 216 217
                                (vlc_meta_t*)(p_demux->p_private ) );
            module_Unneed( p_demux, p_meta );
        }
        return VLC_SUCCESS;
    }

218 219
    /* Initialize the Ogg physical bitstream parser */
    ogg_sync_init( &p_sys->oy );
Gildas Bazin's avatar
 
Gildas Bazin committed
220 221 222 223

    return VLC_SUCCESS;
}

224 225 226 227 228
/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
229 230
    demux_t *p_demux = (demux_t *)p_this;
    demux_sys_t *p_sys = p_demux->p_sys  ;
231 232 233 234

    /* Cleanup the bitstream parser */
    ogg_sync_clear( &p_sys->oy );

235
    Ogg_EndOfStream( p_demux );
236 237 238 239 240 241 242 243 244

    free( p_sys );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
245
static int Demux( demux_t * p_demux )
246
{
247
    demux_sys_t *p_sys = p_demux->p_sys;
248 249 250 251 252 253 254 255 256
    ogg_page    oggpage;
    ogg_packet  oggpacket;
    int         i_stream;


    if( p_sys->i_eos == p_sys->i_streams )
    {
        if( p_sys->i_eos )
        {
257 258
            msg_Dbg( p_demux, "end of a group of logical streams" );
            Ogg_EndOfStream( p_demux );
259 260 261
        }

        p_sys->i_eos = 0;
262
        if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS ) return 0;
263

264 265
        msg_Dbg( p_demux, "beginning of a group of logical streams" );
        es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
266 267 268 269 270
    }

    /*
     * Demux an ogg page from the stream
     */
271
    if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    {
        return 0; /* EOF */
    }

    /* Test for End of Stream */
    if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;


    for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
    {
        logical_stream_t *p_stream = p_sys->pp_stream[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 )
            {
292
                if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
293
                        oggpacket.bytes >= 7 &&
294
                        ! memcmp( &oggpacket.packet[1], "theora", 6 ) )
295 296 297 298
                {
                    Ogg_ReadTheoraHeader( p_stream, &oggpacket );
                    p_stream->secondary_header_packets = 0;
                }
299
                else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
300
                        oggpacket.bytes >= 7 &&
301
                        ! memcmp( &oggpacket.packet[1], "vorbis", 6 ) )
302 303 304 305
                {
                    Ogg_ReadVorbisHeader( p_stream, &oggpacket );
                    p_stream->secondary_header_packets = 0;
                }
306
                else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                {
                    p_stream->secondary_header_packets = 0;
                }
            }

            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 )
                    {
335
                        Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
336 337 338
                    }
                    else
                    {
339 340
                        es_out_Control( p_demux->out, ES_OUT_SET_PCR,
                                        p_stream->i_pcr );
341 342 343 344 345
                    }
                    continue;
                }
            }

346
            Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
        }
        break;
    }

    i_stream = 0; p_sys->i_pcr = -1;
    for( ; i_stream < p_sys->i_streams; i_stream++ )
    {
        logical_stream_t *p_stream = p_sys->pp_stream[i_stream];

        if( p_stream->fmt.i_cat == SPU_ES )
            continue;
        if( p_stream->i_interpolated_pcr < 0 )
            continue;

        if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
            p_sys->i_pcr = p_stream->i_interpolated_pcr;
    }

    if( p_sys->i_pcr >= 0 )
    {
367
        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
368 369 370 371 372 373 374 375 376
    }


    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
377
static int Control( demux_t *p_demux, int i_query, va_list args )
378
{
379
    demux_sys_t *p_sys  = p_demux->p_sys;
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    int64_t *pi64;
    int i;

    switch( i_query )
    {
        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            *pi64 = p_sys->i_pcr;
            return VLC_SUCCESS;

        case DEMUX_SET_TIME:
            return VLC_EGENERIC;

        case DEMUX_SET_POSITION:
            for( i = 0; i < p_sys->i_streams; i++ )
            {
                logical_stream_t *p_stream = p_sys->pp_stream[i];

                /* 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 );
            }
            ogg_sync_reset( &p_sys->oy );

        default:
407 408
            return demux2_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
                                           1, i_query, args );
409 410 411
    }
}

Gildas Bazin's avatar
 
Gildas Bazin committed
412 413 414 415 416 417
/****************************************************************************
 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
 ****************************************************************************
 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
 * are at the end of stream.
 ****************************************************************************/
418
static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
Gildas Bazin's avatar
 
Gildas Bazin committed
419
{
420
    demux_sys_t *p_ogg = p_demux->p_sys  ;
Gildas Bazin's avatar
 
Gildas Bazin committed
421
    int i_read = 0;
422
    char *p_buffer;
Gildas Bazin's avatar
 
Gildas Bazin committed
423 424 425

    while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
    {
426 427 428
        p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );

        i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
Gildas Bazin's avatar
 
Gildas Bazin committed
429 430 431 432 433 434 435 436 437
        if( i_read <= 0 )
            return VLC_EGENERIC;

        ogg_sync_wrote( &p_ogg->oy, i_read );
    }

    return VLC_SUCCESS;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
438 439 440 441 442 443 444
/****************************************************************************
 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
 *                current stream.
 ****************************************************************************/
static void Ogg_UpdatePCR( logical_stream_t *p_stream,
                           ogg_packet *p_oggpacket )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
445
    /* Convert the granulepos into a pcr */
Gildas Bazin's avatar
 
Gildas Bazin committed
446 447
    if( p_oggpacket->granulepos >= 0 )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
448
        if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
449
        {
450
            p_stream->i_pcr = p_oggpacket->granulepos * I64C(1000000)
Gildas Bazin's avatar
 
Gildas Bazin committed
451 452 453 454 455 456 457 458 459
                              / p_stream->f_rate;
        }
        else
        {
            ogg_int64_t iframe = p_oggpacket->granulepos >>
              p_stream->i_theora_keyframe_granule_shift;
            ogg_int64_t pframe = p_oggpacket->granulepos -
              ( iframe << p_stream->i_theora_keyframe_granule_shift );

460
            p_stream->i_pcr = ( iframe + pframe ) * I64C(1000000)
Gildas Bazin's avatar
 
Gildas Bazin committed
461 462 463 464 465 466 467
                              / p_stream->f_rate;
        }

        p_stream->i_interpolated_pcr = p_stream->i_pcr;
    }
    else
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
468
        p_stream->i_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
469

Gildas Bazin's avatar
 
Gildas Bazin committed
470 471
        /* no granulepos available, try to interpolate the pcr.
         * If we can't then don't touch the old value. */
Gildas Bazin's avatar
 
Gildas Bazin committed
472
        if( p_stream->fmt.i_cat == VIDEO_ES )
Gildas Bazin's avatar
 
Gildas Bazin committed
473
            /* 1 frame per packet */
474
            p_stream->i_interpolated_pcr += (I64C(1000000) / p_stream->f_rate);
Gildas Bazin's avatar
 
Gildas Bazin committed
475
        else if( p_stream->fmt.i_bitrate )
476 477 478
            p_stream->i_interpolated_pcr +=
                ( p_oggpacket->bytes * I64C(1000000) /
                  p_stream->fmt.i_bitrate / 8 );
Gildas Bazin's avatar
 
Gildas Bazin committed
479 480 481
    }
}

Gildas Bazin's avatar
 
Gildas Bazin committed
482 483 484
/****************************************************************************
 * Ogg_DecodePacket: Decode an Ogg packet.
 ****************************************************************************/
485
static void Ogg_DecodePacket( demux_t *p_demux,
Gildas Bazin's avatar
 
Gildas Bazin committed
486 487 488
                              logical_stream_t *p_stream,
                              ogg_packet *p_oggpacket )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
489 490
    block_t *p_block;
    vlc_bool_t b_selected;
Gildas Bazin's avatar
 
Gildas Bazin committed
491
    int i_header_len = 0;
492
    mtime_t i_pts = -1, i_interpolated_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
493

Gildas Bazin's avatar
 
Gildas Bazin committed
494 495 496
    /* Sanity check */
    if( !p_oggpacket->bytes )
    {
497
        msg_Dbg( p_demux, "discarding 0 sized packet" );
Gildas Bazin's avatar
 
Gildas Bazin committed
498 499 500
        return;
    }

501
    if( p_oggpacket->bytes >= 7 &&
502
        ! memcmp ( &p_oggpacket->packet[0], "Annodex", 7 ) )
503 504 505 506 507
    {
        /* it's an Annodex packet -- skip it (do nothing) */
        return; 
    }
    else if( p_oggpacket->bytes >= 7 &&
508
        ! memcmp ( &p_oggpacket->packet[0], "AnxData", 7 ) )
509 510 511 512 513
    {
        /* it's an AnxData packet -- skip it (do nothing) */
        return; 
    }

514 515 516
    if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
        p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;

517 518 519 520
    /* Check the ES is selected */
    es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
                    p_stream->p_es, &b_selected );

Gildas Bazin's avatar
 
Gildas Bazin committed
521 522
    if( p_stream->b_force_backup )
    {
523
        uint8_t *p_extra;
524
        vlc_bool_t b_store_size = VLC_TRUE;
525

Gildas Bazin's avatar
 
Gildas Bazin committed
526
        p_stream->i_packets_backup++;
Gildas Bazin's avatar
 
Gildas Bazin committed
527
        switch( p_stream->fmt.i_codec )
Gildas Bazin's avatar
 
Gildas Bazin committed
528 529
        {
        case VLC_FOURCC( 'v','o','r','b' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
530
        case VLC_FOURCC( 's','p','x',' ' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
531
        case VLC_FOURCC( 't','h','e','o' ):
532 533
            if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
            break;
Gildas Bazin's avatar
 
Gildas Bazin committed
534

Gildas Bazin's avatar
 
Gildas Bazin committed
535
        case VLC_FOURCC( 'f','l','a','c' ):
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
            if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
            {
                Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
                p_stream->b_force_backup = 0;
            }
            else if( p_stream->fmt.audio.i_rate )
            {
                p_stream->b_force_backup = 0;
                if( p_oggpacket->bytes >= 9 )
                {
                    p_oggpacket->packet += 9;
                    p_oggpacket->bytes -= 9;
                }
            }
            b_store_size = VLC_FALSE;
            break;
Gildas Bazin's avatar
 
Gildas Bazin committed
552

Gildas Bazin's avatar
 
Gildas Bazin committed
553
        default:
554 555
            p_stream->b_force_backup = 0;
            break;
Gildas Bazin's avatar
 
Gildas Bazin committed
556 557 558
        }

        /* Backup the ogg packet (likely an header packet) */
559 560 561 562 563
        p_stream->p_headers =
            realloc( p_stream->p_headers, p_stream->i_headers +
                     p_oggpacket->bytes + (b_store_size ? 2 : 0) );
        p_extra = p_stream->p_headers + p_stream->i_headers;
        if( b_store_size )
Gildas Bazin's avatar
 
Gildas Bazin committed
564
        {
565 566
            *(p_extra++) = p_oggpacket->bytes >> 8;
            *(p_extra++) = p_oggpacket->bytes & 0xFF;
Gildas Bazin's avatar
 
Gildas Bazin committed
567
        }
568 569
        memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
        p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0);
Gildas Bazin's avatar
 
Gildas Bazin committed
570

Gildas Bazin's avatar
 
Gildas Bazin committed
571 572
        if( !p_stream->b_force_backup )
        {
573 574 575 576
            /* Last header received, commit changes */
            p_stream->fmt.i_extra = p_stream->i_headers;
            p_stream->fmt.p_extra =
                realloc( p_stream->fmt.p_extra, p_stream->i_headers );
577 578
            memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
                    p_stream->i_headers );
579 580
            es_out_Control( p_demux->out, ES_OUT_SET_FMT,
                            p_stream->p_es, &p_stream->fmt );
Gildas Bazin's avatar
 
Gildas Bazin committed
581
        }
582 583

        b_selected = VLC_FALSE; /* Discard the header packet */
Gildas Bazin's avatar
 
Gildas Bazin committed
584 585
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
586
    /* Convert the pcr into a pts */
Gildas Bazin's avatar
 
Gildas Bazin committed
587 588 589
    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' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
590
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
591 592 593 594 595
        if( p_stream->i_pcr >= 0 )
        {
            /* This is for streams where the granulepos of the header packets
             * doesn't match these of the data packets (eg. ogg web radios). */
            if( p_stream->i_previous_pcr == 0 &&
596 597
                p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
            {
598
                es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
Gildas Bazin's avatar
 
Gildas Bazin committed
599

600
                /* Call the pace control */
601 602
                es_out_Control( p_demux->out, ES_OUT_SET_PCR,
                                p_stream->i_pcr );
603
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
604

605
            p_stream->i_previous_pcr = p_stream->i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
606

Gildas Bazin's avatar
 
Gildas Bazin committed
607
            /* The granulepos is the end date of the sample */
608
            i_pts =  p_stream->i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
609
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
610
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
611

Gildas Bazin's avatar
 
Gildas Bazin committed
612
    /* Convert the granulepos into the next pcr */
613
    i_interpolated_pts = p_stream->i_interpolated_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
614
    Ogg_UpdatePCR( p_stream, p_oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
615

Gildas Bazin's avatar
 
Gildas Bazin committed
616 617 618 619 620
    if( p_stream->i_pcr >= 0 )
    {
        /* This is for streams where the granulepos of the header packets
         * doesn't match these of the data packets (eg. ogg web radios). */
        if( p_stream->i_previous_pcr == 0 &&
621 622
            p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
        {
623
            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
624 625

            /* Call the pace control */
626
            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
627
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
628
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
629

Gildas Bazin's avatar
 
Gildas Bazin committed
630 631 632
    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' ) &&
Gildas Bazin's avatar
 
Gildas Bazin committed
633 634 635
        p_stream->i_pcr >= 0 )
    {
        p_stream->i_previous_pcr = p_stream->i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
636 637

        /* The granulepos is the start date of the sample */
638
        i_pts = p_stream->i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
639
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
640

Gildas Bazin's avatar
 
Gildas Bazin committed
641
    if( !b_selected )
Gildas Bazin's avatar
 
Gildas Bazin committed
642 643 644 645 646 647
    {
        /* This stream isn't currently selected so we don't need to decode it,
         * but we did need to store its pcr as it might be selected later on */
        return;
    }

648 649 650
    if( p_oggpacket->bytes <= 0 )
        return;

651
    if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
Gildas Bazin's avatar
 
Gildas Bazin committed
652

653 654 655 656 657
    /* Normalize PTS */
    if( i_pts == 0 ) i_pts = 1;
    else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
    else if( i_pts == -1 ) i_pts = 0;

Gildas Bazin's avatar
 
Gildas Bazin committed
658 659 660 661
    if( p_stream->fmt.i_cat == AUDIO_ES )
        p_block->i_dts = p_block->i_pts = i_pts;
    else if( p_stream->fmt.i_cat == SPU_ES )
    {
662 663
        p_block->i_dts = p_block->i_pts = i_pts;
        p_block->i_length = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
664 665 666 667 668 669 670 671
    }
    else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
        p_block->i_dts = p_block->i_pts = i_pts;
    else
    {
        p_block->i_dts = i_pts;
        p_block->i_pts = 0;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
672

Gildas Bazin's avatar
 
Gildas Bazin committed
673 674 675 676
    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' ) &&
        p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
677 678
        p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
        p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
679
    {
680
        /* We remove the header from the packet */
Gildas Bazin's avatar
 
Gildas Bazin committed
681 682
        i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
        i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
683

684 685 686
        if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
        {
            /* But with subtitles we need to retrieve the duration first */
Gildas Bazin's avatar
 
Gildas Bazin committed
687
            int i, lenbytes = 0;
688

Gildas Bazin's avatar
 
Gildas Bazin committed
689
            if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
690 691 692 693
            {
                for( i = 0, lenbytes = 0; i < i_header_len; i++ )
                {
                    lenbytes = lenbytes << 8;
Gildas Bazin's avatar
 
Gildas Bazin committed
694
                    lenbytes += *(p_oggpacket->packet + i_header_len - i);
695 696
                }
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
697 698 699 700 701
            if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
                ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
                  p_oggpacket->packet[i_header_len + 1] != 0 && 
                  p_oggpacket->packet[i_header_len + 1] != '\n' &&
                  p_oggpacket->packet[i_header_len + 1] != '\r' ) )
702
            {
703
                p_block->i_length = (mtime_t)lenbytes * 1000;
704 705
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
706

707
        i_header_len++;
708 709 710 711
        if( p_block->i_buffer >= i_header_len )
            p_block->i_buffer -= i_header_len;
        else
            p_block->i_buffer = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
712 713
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
714
    if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
715 716
    {
        /* FIXME: the biggest hack I've ever done */
717
        msg_Warn( p_demux, "tarkin pts: "I64Fd", granule: "I64Fd,
Gildas Bazin's avatar
 
Gildas Bazin committed
718
                  p_block->i_pts, p_block->i_dts );
Gildas Bazin's avatar
 
Gildas Bazin committed
719
        msleep(10000);
Gildas Bazin's avatar
 
Gildas Bazin committed
720 721
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
722
    memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
Gildas Bazin's avatar
 
Gildas Bazin committed
723 724
            p_oggpacket->bytes - i_header_len );

725
    es_out_Send( p_demux->out, p_stream->p_es, p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
726 727 728 729 730 731 732 733 734 735 736 737
}

/****************************************************************************
 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
 *                         stream and fill p_ogg.
 *****************************************************************************
 * The initial page of a logical stream is marked as a 'bos' page.
 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
 * together and all of the initial pages must appear before any data pages.
 *
 * On success this function returns VLC_SUCCESS.
 ****************************************************************************/
738
static int Ogg_FindLogicalStreams( demux_t *p_demux )
Gildas Bazin's avatar
 
Gildas Bazin committed
739
{
740
    demux_sys_t *p_ogg = p_demux->p_sys  ;
Gildas Bazin's avatar
 
Gildas Bazin committed
741 742 743 744
    ogg_packet oggpacket;
    ogg_page oggpage;
    int i_stream;

Gildas Bazin's avatar
 
Gildas Bazin committed
745 746
#define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]

747
    while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
    {
        if( ogg_page_bos( &oggpage ) )
        {

            /* All is wonderful in our fine fine little world.
             * We found the beginning of our first logical stream. */
            while( ogg_page_bos( &oggpage ) )
            {
                p_ogg->i_streams++;
                p_ogg->pp_stream =
                    realloc( p_ogg->pp_stream, p_ogg->i_streams *
                             sizeof(logical_stream_t *) );

                p_stream = malloc( sizeof(logical_stream_t) );
                memset( p_stream, 0, sizeof(logical_stream_t) );
763
                p_stream->p_headers = 0;
764
                p_stream->secondary_header_packets = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
765

Gildas Bazin's avatar
 
Gildas Bazin committed
766 767
                es_format_Init( &p_stream->fmt, 0, 0 );

Gildas Bazin's avatar
 
Gildas Bazin committed
768 769 770 771 772 773 774 775 776
                /* Setup the logical stream */
                p_stream->i_serial_no = ogg_page_serialno( &oggpage );
                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 tis Ogg bitstream */
                if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
                {
                    /* error. stream version mismatch perhaps */
777
                    msg_Err( p_demux, "error reading first page of "
Gildas Bazin's avatar
 
Gildas Bazin committed
778 779 780 781 782 783 784 785
                             "Ogg bitstream data" );
                    return VLC_EGENERIC;
                }

                /* FIXME: check return value */
                ogg_stream_packetpeek( &p_stream->os, &oggpacket );

                /* Check for Vorbis header */
786
                if( oggpacket.bytes >= 7 &&
787
                    ! memcmp( &oggpacket.packet[1], "vorbis", 6 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
788
                {
789
                    Ogg_ReadVorbisHeader( p_stream, &oggpacket );
790
                    msg_Dbg( p_demux, "found vorbis header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
791
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
792
                /* Check for Speex header */
Gildas Bazin's avatar
 
Gildas Bazin committed
793
                else if( oggpacket.bytes >= 7 &&
794
                    ! memcmp( &oggpacket.packet[0], "Speex", 5 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
795
                {
796
                    Ogg_ReadSpeexHeader( p_stream, &oggpacket );
797
                    msg_Dbg( p_demux, "found speex header, channels: %i, "
Gildas Bazin's avatar
 
Gildas Bazin committed
798 799 800
                             "rate: %i,  bitrate: %i",
                             p_stream->fmt.audio.i_channels,
                             (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
Gildas Bazin's avatar
 
Gildas Bazin committed
801
                }
802
                /* Check for Flac header (< version 1.1.1) */
Gildas Bazin's avatar
 
Gildas Bazin committed
803
                else if( oggpacket.bytes >= 4 &&
804
                    ! memcmp( &oggpacket.packet[0], "fLaC", 4 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
805
                {
806
                    msg_Dbg( p_demux, "found FLAC header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
807 808 809 810 811 812

                    /* Grrrr!!!! Did they really have to put all the
                     * important info in the second header packet!!!
                     * (STREAMINFO metadata is in the following packet) */
                    p_stream->b_force_backup = 1;

Gildas Bazin's avatar
 
Gildas Bazin committed
813 814
                    p_stream->fmt.i_cat = AUDIO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
Gildas Bazin's avatar
 
Gildas Bazin committed
815
                }
816 817
                /* Check for Flac header (>= version 1.1.1) */
                else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
818 819
                    ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
                    ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
                {
                    int i_packets = ((int)oggpacket.packet[7]) << 8 |
                        oggpacket.packet[8];
                    msg_Dbg( p_demux, "found FLAC header version %i.%i "
                             "(%i header packets)",
                             oggpacket.packet[5], oggpacket.packet[6],
                             i_packets );

                    p_stream->b_force_backup = 1;

                    p_stream->fmt.i_cat = AUDIO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
                    oggpacket.packet += 13; oggpacket.bytes -= 13;
                    Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
835 836
                /* Check for Theora header */
                else if( oggpacket.bytes >= 7 &&
837
                         ! memcmp( &oggpacket.packet[1], "theora", 6 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
838
                {
839
                    Ogg_ReadTheoraHeader( p_stream, &oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
840

841
                    msg_Dbg( p_demux,
Gildas Bazin's avatar
 
Gildas Bazin committed
842
                             "found theora header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
843
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
844 845 846
                }
                /* Check for Tarkin header */
                else if( oggpacket.bytes >= 7 &&
847
                         ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
848 849 850
                {
                    oggpack_buffer opb;

851
                    msg_Dbg( p_demux, "found tarkin header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
852 853
                    p_stream->fmt.i_cat = VIDEO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
Gildas Bazin's avatar
 
Gildas Bazin committed
854 855 856 857 858

                    /* Cheat and get additionnal info ;) */
                    oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
                    oggpack_adv( &opb, 88 );
                    oggpack_adv( &opb, 104 );
Gildas Bazin's avatar
 
Gildas Bazin committed
859
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
860
                    p_stream->f_rate = 2; /* FIXME */
861
                    msg_Dbg( p_demux,
Gildas Bazin's avatar
 
Gildas Bazin committed
862
                             "found tarkin header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
863
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
864
                }
865 866
                /* Check for Annodex header */
                else if( oggpacket.bytes >= 7 &&
867
                         ! memcmp( &oggpacket.packet[0], "Annodex", 7 ) )
868
                {
869
                    Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
870 871 872 873 874 875 876
                                           &oggpacket );
                    /* kill annodex track */
                    free( p_stream );
                    p_ogg->i_streams--;
                }
                /* Check for Annodex header */
                else if( oggpacket.bytes >= 7 &&
877
                         ! memcmp( &oggpacket.packet[0], "AnxData", 7 ) )
878
                {
879
                    Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
880 881
                                           &oggpacket );
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
882
                else if( oggpacket.bytes >= 142 &&
883
                         !memcmp( &oggpacket.packet[1],
Gildas Bazin's avatar
 
Gildas Bazin committed
884 885 886 887 888 889 890 891
                                   "Direct Show Samples embedded in Ogg", 35 ))
                {
                    /* Old header type */

                    /* Check for video header (old format) */
                    if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
                        oggpacket.bytes >= 184 )
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
892 893
                        p_stream->fmt.i_cat = VIDEO_ES;
                        p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
894 895 896 897
                            VLC_FOURCC( oggpacket.packet[68],
                                        oggpacket.packet[69],
                                        oggpacket.packet[70],
                                        oggpacket.packet[71] );
898
                        msg_Dbg( p_demux, "found video header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
899
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
900

901 902 903
                        p_stream->fmt.video.i_frame_rate = 10000000;
                        p_stream->fmt.video.i_frame_rate_base =
                            GetQWLE((oggpacket.packet+164));
Gildas Bazin's avatar
 
Gildas Bazin committed
904 905
                        p_stream->f_rate = 10000000.0 /
                            GetQWLE((oggpacket.packet+164));
Gildas Bazin's avatar
 
Gildas Bazin committed
906
                        p_stream->fmt.video.i_bits_per_pixel =
Gildas Bazin's avatar
 
Gildas Bazin committed
907
                            GetWLE((oggpacket.packet+182));
Gildas Bazin's avatar
 
Gildas Bazin committed
908 909 910 911
                        if( !p_stream->fmt.video.i_bits_per_pixel )
                            /* hack, FIXME */
                            p_stream->fmt.video.i_bits_per_pixel = 24;
                        p_stream->fmt.video.i_width =
Gildas Bazin's avatar
 
Gildas Bazin committed
912
                            GetDWLE((oggpacket.packet+176));
Gildas Bazin's avatar
 
Gildas Bazin committed
913
                        p_stream->fmt.video.i_height =
Gildas Bazin's avatar
 
Gildas Bazin committed
914 915
                            GetDWLE((oggpacket.packet+180));

916
                        msg_Dbg( p_demux,
Gildas Bazin's avatar
 
Gildas Bazin committed
917 918 919 920 921 922
                                 "fps: %f, width:%i; height:%i, bitcount:%i",
                                 p_stream->f_rate,
                                 p_stream->fmt.video.i_width,
                                 p_stream->fmt.video.i_height,
                                 p_stream->fmt.video.i_bits_per_pixel);

Gildas Bazin's avatar
 
Gildas Bazin committed
923 924 925 926 927
                    }
                    /* Check for audio header (old format) */
                    else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
                    {
                        unsigned int i_extra_size;
Gildas Bazin's avatar
 
Gildas Bazin committed
928
                        unsigned int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
929

Gildas Bazin's avatar
 
Gildas Bazin committed
930
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
931 932

                        i_extra_size = GetWLE((oggpacket.packet+140));
Gildas Bazin's avatar
 
Gildas Bazin committed
933
                        if( i_extra_size )
Gildas Bazin's avatar
 
Gildas Bazin committed
934
                        {
Gildas Bazin's avatar
 
Gildas Bazin committed
935 936 937 938
                            p_stream->fmt.i_extra = i_extra_size;
                            p_stream->fmt.p_extra = malloc( i_extra_size );
                            memcpy( p_stream->fmt.p_extra,
                                    oggpacket.packet + 142, i_extra_size );
Gildas Bazin's avatar
 
Gildas Bazin committed
939 940
                        }

Gildas Bazin's avatar
 
Gildas Bazin committed
941 942
                        i_format_tag = GetWLE((oggpacket.packet+124));
                        p_stream->fmt.audio.i_channels =
Gildas Bazin's avatar
 
Gildas Bazin committed
943
                            GetWLE((oggpacket.packet+126));
Gildas Bazin's avatar
 
Gildas Bazin committed
944
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
Gildas Bazin's avatar
 
Gildas Bazin committed
945
                            GetDWLE((oggpacket.packet+128));
Gildas Bazin's avatar
 
Gildas Bazin committed
946 947 948
                        p_stream->fmt.i_bitrate =
                            GetDWLE((oggpacket.packet+132)) * 8;
                        p_stream->fmt.audio.i_blockalign =
Gildas Bazin's avatar
 
Gildas Bazin committed
949
                            GetWLE((oggpacket.packet+136));
Gildas Bazin's avatar
 
Gildas Bazin committed
950
                        p_stream->fmt.audio.i_bitspersample =
Gildas Bazin's avatar
 
Gildas Bazin committed
951 952
                            GetWLE((oggpacket.packet+138));

953 954 955 956 957
                        wf_tag_to_fourcc( i_format_tag,
                                          &p_stream->fmt.i_codec, 0 );

                        if( p_stream->fmt.i_codec ==
                            VLC_FOURCC('u','n','d','f') )
Gildas Bazin's avatar
 
Gildas Bazin committed
958
                        {
Gildas Bazin's avatar
 
Gildas Bazin committed
959 960 961
                            p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
                                ( i_format_tag >> 8 ) & 0xff,
                                i_format_tag & 0xff );
Gildas Bazin's avatar
 
Gildas Bazin committed
962 963
                        }

964
                        msg_Dbg( p_demux, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
965
                                 (char *)&p_stream->fmt.i_codec );
966
                        msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
Gildas Bazin's avatar
 
Gildas Bazin committed
967
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
968 969 970 971 972
                                 i_format_tag,
                                 p_stream->fmt.audio.i_channels,
                                 p_stream->fmt.audio.i_rate,
                                 p_stream->fmt.audio.i_bitspersample,
                                 p_stream->fmt.i_bitrate / 1024 );
973

Gildas Bazin's avatar
 
Gildas Bazin committed
974 975 976
                    }
                    else
                    {
977
                        msg_Dbg( p_demux, "stream %d has an old header "
Gildas Bazin's avatar
 
Gildas Bazin committed
978 979 980 981 982
                            "but is of an unknown type", p_ogg->i_streams-1 );
                        free( p_stream );
                        p_ogg->i_streams--;
                    }
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
983
                else if( (*oggpacket.packet & PACKET_TYPE_BITS )
Sam Hocevar's avatar
Sam Hocevar committed
984
                         == PACKET_TYPE_HEADER &&
Gildas Bazin's avatar
 
Gildas Bazin committed
985 986 987 988 989 990 991
                         oggpacket.bytes >= (int)sizeof(stream_header)+1 )
                {
                    stream_header *st = (stream_header *)(oggpacket.packet+1);

                    /* Check for video header (new format) */
                    if( !strncmp( st->streamtype, "video", 5 ) )
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
992
                        p_stream->fmt.i_cat = VIDEO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
993 994 995 996

                        /* We need to get rid of the header packet */
                        ogg_stream_packetout( &p_stream->os, &oggpacket );

Gildas Bazin's avatar
 
Gildas Bazin committed
997 998 999
                        p_stream->fmt.i_codec =
                            VLC_FOURCC( st->subtype[0], st->subtype[1],
                                        st->subtype[2], st->subtype[3] );
1000
                        msg_Dbg( p_demux, "found video header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
1001
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
1002

1003 1004 1005
                        p_stream->fmt.video.i_frame_rate = 10000000;
                        p_stream->fmt.video.i_frame_rate_base =
                            GetQWLE(&st->time_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
1006
                        p_stream->f_rate = 10000000.0 /
1007
                            GetQWLE(&st->time_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
1008
                        p_stream->fmt.video.i_bits_per_pixel =
1009
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
1010
                        p_stream->fmt.video.i_width =
1011
                            GetDWLE(&st->sh.video.width);
Gildas Bazin's avatar
 
Gildas Bazin committed
1012
                        p_stream->fmt.video.i_height =
1013
                            GetDWLE(&st->sh.video.height);
Gildas Bazin's avatar
 
Gildas Bazin committed
1014

1015
                        msg_Dbg( p_demux,
Gildas Bazin's avatar
 
Gildas Bazin committed
1016 1017 1018 1019 1020
                                 "fps: %f, width:%i; height:%i, bitcount:%i",
                                 p_stream->f_rate,
                                 p_stream->fmt.video.i_width,
                                 p_stream->fmt.video.i_height,
                                 p_stream->fmt.video.i_bits_per_pixel );
Gildas Bazin's avatar
 
Gildas Bazin committed
1021 1022 1023 1024 1025
                    }
                    /* Check for audio header (new format) */
                    else if( !strncmp( st->streamtype, "audio", 5 ) )
                    {
                        char p_buffer[5];
Gildas Bazin's avatar
 
Gildas Bazin committed
1026
                        int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
1027

Gildas Bazin's avatar
 
Gildas Bazin committed
1028
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
1029 1030 1031 1032

                        /* We need to get rid of the header packet */
                        ogg_stream_packetout( &p_stream->os, &oggpacket );

1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
                        p_stream->fmt.i_extra = GetQWLE(&st->size) -
                            sizeof(stream_header);
                        if( p_stream->fmt.i_extra )
                        {
                            p_stream->fmt.p_extra =
                                malloc( p_stream->fmt.i_extra );
                            memcpy( p_stream->fmt.p_extra, st + 1,
                                    p_stream->fmt.i_extra );
                        }

Gildas Bazin's avatar
 
Gildas Bazin committed
1043 1044
                        memcpy( p_buffer, st->subtype, 4 );
                        p_buffer[4] = '\0';
Gildas Bazin's avatar
 
Gildas Bazin committed
1045 1046
                        i_format_tag = strtol(p_buffer,NULL,16);
                        p_stream->fmt.audio.i_channels =
1047
                            GetWLE(&st->sh.audio.channels);
Gildas Bazin's avatar
 
Gildas Bazin committed
1048
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
1049
                            GetQWLE(&st->samples_per_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
1050 1051 1052
                        p_stream->fmt.i_bitrate =
                            GetDWLE(&st->sh.audio.avgbytespersec) * 8;
                        p_stream->fmt.audio.i_blockalign =
1053
                            GetWLE(&st->sh.audio.blockalign);
Gildas Bazin's avatar
 
Gildas Bazin committed
1054
                        p_stream->fmt.audio.i_bitspersample =
1055
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
1056

1057 1058 1059 1060 1061
                        wf_tag_to_fourcc( i_format_tag,
                                          &p_stream->fmt.i_codec, 0 );

                        if( p_stream->fmt.i_codec ==
                            VLC_FOURCC('u','n','d','f') )
Gildas Bazin's avatar
 
Gildas Bazin committed
1062
                        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1063 1064 1065
                            p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
                                ( i_format_tag >> 8 ) & 0xff,
                                i_format_tag & 0xff );
Gildas Bazin's avatar
 
Gildas Bazin committed
1066 1067
                        }

1068
                        msg_Dbg( p_demux, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
1069
                                 (char *)&p_stream->fmt.i_codec );
1070
                        msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
Gildas Bazin's avatar
 
Gildas Bazin committed
1071
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
1072 1073 1074 1075 1076
                                 i_format_tag,
                                 p_stream->fmt.audio.i_channels,
                                 p_stream->fmt.audio.i_rate,
                                 p_stream->fmt.audio.i_bitspersample,
                                 p_stream->fmt.i_bitrate / 1024 );
Gildas Bazin's avatar
 
Gildas Bazin committed
1077
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1078 1079 1080
                    /* Check for text (subtitles) header */
                    else if( !strncmp(st->streamtype, "text", 4) )
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1081 1082 1083
                        /* We need to get rid of the header packet */
                        ogg_stream_packetout( &p_stream->os, &oggpacket );

1084
                        msg_Dbg( p_demux, "found text subtitles header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
1085 1086
                        p_stream->fmt.i_cat = SPU_ES;
                        p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
Gildas Bazin's avatar
 
Gildas Bazin committed
1087
                        p_stream->f_rate = 1000; /* granulepos is in milisec */
Gildas Bazin's avatar
 
Gildas Bazin committed
1088
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1089 1090
                    else
                    {
1091
                        msg_Dbg( p_demux, "stream %d has a header marker "
Gildas Bazin's avatar
 
Gildas Bazin committed
1092 1093 1094 1095 1096
                            "but is of an unknown type", p_ogg->i_streams-1 );
                        free( p_stream );
                        p_ogg->i_streams--;
                    }
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
1097 1098
                else
                {
1099
                    msg_Dbg( p_demux, "stream %d is of unknown type",
Gildas Bazin's avatar
 
Gildas Bazin committed
1100
                             p_ogg->i_streams-1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
1101 1102
                    free( p_stream );
                    p_ogg->i_streams--;
Gildas Bazin's avatar
 
Gildas Bazin committed
1103