ogg.c 49.1 KB
Newer Older
Gildas Bazin's avatar
 
Gildas Bazin committed
1 2 3
/*****************************************************************************
 * ogg.c : ogg stream input module for vlc
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
4 5
 * Copyright (C) 2001-2003 VideoLAN
 * $Id: ogg.c,v 1.45 2003/11/22 12:41:32 gbazin Exp $
Gildas Bazin's avatar
 
Gildas Bazin committed
6 7
 *
 * Authors: Gildas Bazin <gbazin@netcourrier.com>
Sam Hocevar's avatar
Sam Hocevar committed
8
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
9 10 11 12
 * 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
13
 *
Gildas Bazin's avatar
 
Gildas Bazin committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * 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
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <vlc/vlc.h>
#include <vlc/input.h>

#include <ogg/ogg.h>

Gildas Bazin's avatar
 
Gildas Bazin committed
32 33
#include "codecs.h"
#include "vlc_bits.h"
Gildas Bazin's avatar
 
Gildas Bazin committed
34

Gildas Bazin's avatar
 
Gildas Bazin committed
35 36 37
#define OGG_BLOCK_SIZE 4096

/*****************************************************************************
Sam Hocevar's avatar
Sam Hocevar committed
38
 * Definitions of structures and functions used by this plugins
Gildas Bazin's avatar
 
Gildas Bazin committed
39 40 41 42 43
 *****************************************************************************/
typedef struct logical_stream_s
{
    ogg_stream_state os;                        /* logical stream of packets */

Gildas Bazin's avatar
 
Gildas Bazin committed
44 45 46
    es_format_t      fmt;
    es_out_id_t      *p_es;
    double           f_rate;
Gildas Bazin's avatar
 
Gildas Bazin committed
47

Gildas Bazin's avatar
 
Gildas Bazin committed
48 49
    int              i_serial_no;
    int              b_activated;
Gildas Bazin's avatar
 
Gildas Bazin committed
50

Gildas Bazin's avatar
 
Gildas Bazin committed
51 52 53 54 55 56 57
    /* 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;
    ogg_packet       *p_packets_backup;

Gildas Bazin's avatar
 
Gildas Bazin committed
58 59
    /* program clock reference (in units of 90kHz) derived from the previous
     * granulepos */
Gildas Bazin's avatar
 
Gildas Bazin committed
60
    mtime_t          i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
61
    mtime_t          i_interpolated_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
62
    mtime_t          i_previous_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
63

Gildas Bazin's avatar
 
Gildas Bazin committed
64
    /* Misc */
Gildas Bazin's avatar
 
Gildas Bazin committed
65
    int b_reinit;
Gildas Bazin's avatar
 
Gildas Bazin committed
66
    int i_theora_keyframe_granule_shift;
Gildas Bazin's avatar
 
Gildas Bazin committed
67

Gildas Bazin's avatar
 
Gildas Bazin committed
68 69 70 71 72 73 74 75 76
} 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
77
    /* program clock reference (in units of 90kHz) derived from the pcr of
Gildas Bazin's avatar
 
Gildas Bazin committed
78
     * the sub-streams */
Gildas Bazin's avatar
 
Gildas Bazin committed
79
    mtime_t i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
80
    int     b_reinit;
Gildas Bazin's avatar
 
Gildas Bazin committed
81
    int     i_prev_sync_state;
Gildas Bazin's avatar
 
Gildas Bazin committed
82 83

    /* stream state */
Gildas Bazin's avatar
 
Gildas Bazin committed
84
    int     i_eos;
Gildas Bazin's avatar
 
Gildas Bazin committed
85 86
};

Gildas Bazin's avatar
 
Gildas Bazin committed
87 88 89 90 91 92
/* 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
93

Gildas Bazin's avatar
 
Gildas Bazin committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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;

/* 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
131 132 133 134 135 136
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Activate  ( vlc_object_t * );
static void Deactivate( vlc_object_t * );
static int  Demux     ( input_thread_t * );
Gildas Bazin's avatar
 
Gildas Bazin committed
137
static int  Control   ( input_thread_t *, int, va_list );
Gildas Bazin's avatar
 
Gildas Bazin committed
138 139 140 141

/* Bitstream manipulation */
static int  Ogg_Check        ( input_thread_t *p_input );
static int  Ogg_ReadPage     ( input_thread_t *, demux_sys_t *, ogg_page * );
Gildas Bazin's avatar
 
Gildas Bazin committed
142
static void Ogg_UpdatePCR    ( logical_stream_t *, ogg_packet * );
Gildas Bazin's avatar
 
Gildas Bazin committed
143
static void Ogg_DecodePacket ( input_thread_t *p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
144
                               logical_stream_t *p_stream, ogg_packet * );
Gildas Bazin's avatar
 
Gildas Bazin committed
145 146 147 148

static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg);
static int Ogg_FindLogicalStreams( input_thread_t *p_input,demux_sys_t *p_ogg);
static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg );
Gildas Bazin's avatar
 
Gildas Bazin committed
149 150 151 152 153

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
Gildas Bazin's avatar
 
Gildas Bazin committed
154
    set_description( _("ogg stream demuxer" ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
155 156 157 158 159 160 161 162 163 164
    set_capability( "demux", 50 );
    set_callbacks( Activate, Deactivate );
    add_shortcut( "ogg" );
vlc_module_end();

/****************************************************************************
 * Ogg_Check: Check we are dealing with an ogg stream.
 ****************************************************************************/
static int Ogg_Check( input_thread_t *p_input )
{
Sam Hocevar's avatar
Sam Hocevar committed
165
    uint8_t *p_peek;
Gildas Bazin's avatar
 
Gildas Bazin committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    int i_size = input_Peek( p_input, &p_peek, 4 );

    /* Check for the Ogg capture pattern */
    if( !(i_size>3) || !(p_peek[0] == 'O') || !(p_peek[1] == 'g') ||
        !(p_peek[2] == 'g') || !(p_peek[3] == 'S') )
        return VLC_EGENERIC;

    /* FIXME: Capture pattern might not be enough so we can also check for the
     * the first complete page */

    return VLC_SUCCESS;
}

/****************************************************************************
 * 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.
 ****************************************************************************/
static int Ogg_ReadPage( input_thread_t *p_input, demux_sys_t *p_ogg,
                         ogg_page *p_oggpage )
{
    int i_read = 0;
    data_packet_t *p_data;
    byte_t *p_buffer;

    while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
    {
        i_read = input_SplitBuffer( p_input, &p_data, OGG_BLOCK_SIZE );
        if( i_read <= 0 )
            return VLC_EGENERIC;

        p_buffer = ogg_sync_buffer( &p_ogg->oy, i_read );
        p_input->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, i_read );
        ogg_sync_wrote( &p_ogg->oy, i_read );
        input_DeletePacket( p_input->p_method_data, p_data );
    }

    return VLC_SUCCESS;
}

Gildas Bazin's avatar
 
Gildas Bazin committed
207 208 209 210 211 212 213
/****************************************************************************
 * 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
214
    /* Convert the granulepos into a pcr */
Gildas Bazin's avatar
 
Gildas Bazin committed
215 216
    if( p_oggpacket->granulepos >= 0 )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
217
        if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
        {
            p_stream->i_pcr = p_oggpacket->granulepos * 90000
                              / 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 );

            p_stream->i_pcr = ( iframe + pframe ) * 90000
                              / p_stream->f_rate;
        }

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

Gildas Bazin's avatar
 
Gildas Bazin committed
239 240
        /* 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
241
        if( p_stream->fmt.i_cat == VIDEO_ES )
Gildas Bazin's avatar
 
Gildas Bazin committed
242 243
            /* 1 frame per packet */
            p_stream->i_interpolated_pcr += (90000 / p_stream->f_rate);
Gildas Bazin's avatar
 
Gildas Bazin committed
244
        else if( p_stream->fmt.i_bitrate )
Gildas Bazin's avatar
 
Gildas Bazin committed
245
            p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
Gildas Bazin's avatar
 
Gildas Bazin committed
246
                                              / p_stream->fmt.i_bitrate / 8 );
Gildas Bazin's avatar
 
Gildas Bazin committed
247 248 249
    }
}

Gildas Bazin's avatar
 
Gildas Bazin committed
250 251 252 253 254 255 256
/****************************************************************************
 * Ogg_DecodePacket: Decode an Ogg packet.
 ****************************************************************************/
static void Ogg_DecodePacket( input_thread_t *p_input,
                              logical_stream_t *p_stream,
                              ogg_packet *p_oggpacket )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
257 258
    block_t *p_block;
    vlc_bool_t b_selected;
Gildas Bazin's avatar
 
Gildas Bazin committed
259
    int i_header_len = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
260
    mtime_t i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
261

Gildas Bazin's avatar
 
Gildas Bazin committed
262 263 264 265
    if( p_stream->b_force_backup )
    {
        ogg_packet *p_packet_backup;
        p_stream->i_packets_backup++;
Gildas Bazin's avatar
 
Gildas Bazin committed
266
        switch( p_stream->fmt.i_codec )
Gildas Bazin's avatar
 
Gildas Bazin committed
267 268
        {
        case VLC_FOURCC( 'v','o','r','b' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
269
        case VLC_FOURCC( 's','p','x',' ' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
270 271 272 273
        case VLC_FOURCC( 't','h','e','o' ):
          if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
          break;

Gildas Bazin's avatar
 
Gildas Bazin committed
274
        case VLC_FOURCC( 'f','l','a','c' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
275 276
          if( p_stream->i_packets_backup == 1 ) return;
          else if( p_stream->i_packets_backup == 2 )
Gildas Bazin's avatar
 
Gildas Bazin committed
277 278
          {
              /* Parse the STREAMINFO metadata */
Gildas Bazin's avatar
 
Gildas Bazin committed
279 280 281 282
              bs_t s;
              bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
              bs_read( &s, 1 );
              if( bs_read( &s, 7 ) == 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
283
              {
Gildas Bazin's avatar
 
Gildas Bazin committed
284
                  if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
Gildas Bazin's avatar
 
Gildas Bazin committed
285
                  {
Gildas Bazin's avatar
 
Gildas Bazin committed
286 287 288 289 290 291
                      bs_skip( &s, 80 );
                      p_stream->f_rate = p_stream->fmt.audio.i_rate =
                          bs_read( &s, 20 );
                      p_stream->fmt.audio.i_channels =
                          bs_read( &s, 3 ) + 1;

Gildas Bazin's avatar
 
Gildas Bazin committed
292
                      msg_Dbg( p_input, "Flac header, channels: %i, rate: %i",
Gildas Bazin's avatar
 
Gildas Bazin committed
293 294
                               p_stream->fmt.audio.i_channels,
                               (int)p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
295 296 297
                  }
                  else
                  {
Gildas Bazin's avatar
 
Gildas Bazin committed
298
                      msg_Dbg( p_input, "FLAC STREAMINFO metadata too short" );
Gildas Bazin's avatar
 
Gildas Bazin committed
299
                  }
Gildas Bazin's avatar
 
Gildas Bazin committed
300 301 302 303 304 305 306 307 308 309 310 311 312

                  /* Store STREAMINFO for the decoder and packetizer */
                  p_stream->fmt.i_extra = p_oggpacket->bytes + 4;
                  p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
                  memcpy( p_stream->fmt.p_extra, "fLaC", 4);
                  memcpy( ((uint8_t *)p_stream->fmt.p_extra) + 4,
                          p_oggpacket->packet, p_oggpacket->bytes );

                  /* Fake this as the last metadata block */
                  ((uint8_t*)p_stream->fmt.p_extra)[4] |= 0x80;

                  p_stream->p_es = es_out_Add( p_input->p_es_out,
                                               &p_stream->fmt );
Gildas Bazin's avatar
 
Gildas Bazin committed
313 314 315 316
              }
              else
              {
                  /* This ain't a STREAMINFO metadata */
Gildas Bazin's avatar
 
Gildas Bazin committed
317
                  msg_Dbg( p_input, "Invalid FLAC STREAMINFO metadata" );
Gildas Bazin's avatar
 
Gildas Bazin committed
318 319
              }
              p_stream->b_force_backup = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
320 321
              p_stream->i_packets_backup = 0;
              return;
Gildas Bazin's avatar
 
Gildas Bazin committed
322 323 324
          }
          break;

Gildas Bazin's avatar
 
Gildas Bazin committed
325 326 327 328 329 330
        default:
          p_stream->b_force_backup = 0;
          break;
        }

        /* Backup the ogg packet (likely an header packet) */
Gildas Bazin's avatar
 
Gildas Bazin committed
331 332 333 334 335 336 337 338 339
        p_stream->p_packets_backup =
            realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
                     sizeof(ogg_packet) );

        p_packet_backup =
            &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];

        p_packet_backup->bytes = p_oggpacket->bytes;
        p_packet_backup->granulepos = p_oggpacket->granulepos;
Gildas Bazin's avatar
 
Gildas Bazin committed
340 341 342 343 344 345 346 347 348

        if( p_oggpacket->granulepos >= 0 )
        {
            /* Because of vorbis granulepos scheme we must set the pcr for the
             * 1st header packet so it doesn't get discarded in the
             * packetizer */
            Ogg_UpdatePCR( p_stream, p_oggpacket );
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
349 350 351 352 353 354
        p_packet_backup->packet = malloc( p_oggpacket->bytes );
        if( !p_packet_backup->packet ) return;
        memcpy( p_packet_backup->packet, p_oggpacket->packet,
                p_oggpacket->bytes );
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
355 356 357 358 359
    /* Check the ES is selected */
    es_out_Control( p_input->p_es_out, ES_OUT_GET_SELECT,
                    p_stream->p_es, &b_selected );

    if( b_selected && !p_stream->b_activated )
Gildas Bazin's avatar
 
Gildas Bazin committed
360
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
        p_stream->b_activated = VLC_TRUE;

        /* Newly activated stream, feed the backup headers to the decoder */
        if( !p_stream->b_force_backup )
        {
            int i;
            for( i = 0; i < p_stream->i_packets_backup; i++ )
            {
                /* Set correct starting date in header packets */
                p_stream->p_packets_backup[i].granulepos =
                    p_stream->i_interpolated_pcr * p_stream->f_rate / 90000;

                Ogg_DecodePacket( p_input, p_stream,
                                  &p_stream->p_packets_backup[i] );
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
377 378
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
379
    /* Convert the pcr into a pts */
Gildas Bazin's avatar
 
Gildas Bazin committed
380 381 382
    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
383
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
        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 &&
                p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY * 9/100 )
                p_input->stream.p_selected_program->i_synchro_state =
                    SYNCHRO_REINIT;

            p_stream->i_previous_pcr = p_stream->i_pcr;

            /* Call the pace control */
            if( p_input->stream.p_selected_program->i_synchro_state ==
                SYNCHRO_REINIT )
            input_ClockManageRef( p_input,
                                  p_input->stream.p_selected_program,
                                  p_stream->i_pcr );
        }

        /* The granulepos is the end date of the sample */
Gildas Bazin's avatar
 
Gildas Bazin committed
404
        i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
Gildas Bazin's avatar
 
Gildas Bazin committed
405 406
            input_ClockGetTS( p_input, p_input->stream.p_selected_program,
                              p_stream->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
407 408 409

        /* Convert the granulepos into the next pcr */
        Ogg_UpdatePCR( p_stream, p_oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
410 411 412
    }
    else
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
        /* Convert the granulepos into the current pcr */
        Ogg_UpdatePCR( p_stream, p_oggpacket );

        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 &&
                p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY * 9/100 )
                p_input->stream.p_selected_program->i_synchro_state =
                    SYNCHRO_REINIT;

            p_stream->i_previous_pcr = p_stream->i_pcr;

            /* Call the pace control */
            if( p_input->stream.p_selected_program->i_synchro_state ==
                SYNCHRO_REINIT )
            input_ClockManageRef( p_input,
                                  p_input->stream.p_selected_program,
                                  p_stream->i_pcr );
        }

        /* The granulepos is the start date of the sample */
Gildas Bazin's avatar
 
Gildas Bazin committed
436
        i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
Gildas Bazin's avatar
 
Gildas Bazin committed
437
            input_ClockGetTS( p_input, p_input->stream.p_selected_program,
Gildas Bazin's avatar
 
Gildas Bazin committed
438
                              p_stream->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
439
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
440

Gildas Bazin's avatar
 
Gildas Bazin committed
441
    if( !b_selected )
Gildas Bazin's avatar
 
Gildas Bazin committed
442 443 444
    {
        /* 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 */
Gildas Bazin's avatar
 
Gildas Bazin committed
445
        p_stream->b_activated = VLC_FALSE;
Gildas Bazin's avatar
 
Gildas Bazin committed
446 447 448
        return;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
449
    if( !( p_block = block_New( p_input, p_oggpacket->bytes ) ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
450 451 452
    {
        return;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
453
    p_block->i_dts = p_block->i_pts = i_pts;
Gildas Bazin's avatar
 
Gildas Bazin committed
454

Gildas Bazin's avatar
 
Gildas Bazin committed
455
    if( p_stream->fmt.i_cat == SPU_ES ) p_block->i_dts = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
456

Gildas Bazin's avatar
 
Gildas Bazin committed
457 458 459 460 461
    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' ) &&
        p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
462 463 464 465 466 467
    {
        /* Remove the header from the packet */
        i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
        i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
        i_header_len++;

Gildas Bazin's avatar
 
Gildas Bazin committed
468
        p_block->i_buffer -= i_header_len;
Gildas Bazin's avatar
 
Gildas Bazin committed
469 470
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
471
    if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
472 473
    {
        /* FIXME: the biggest hack I've ever done */
474
        msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
Gildas Bazin's avatar
 
Gildas Bazin committed
475
                  p_block->i_pts, p_block->i_dts );
Gildas Bazin's avatar
 
Gildas Bazin committed
476
        msleep(10000);
Gildas Bazin's avatar
 
Gildas Bazin committed
477 478
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
479
    memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
Gildas Bazin's avatar
 
Gildas Bazin committed
480 481
            p_oggpacket->bytes - i_header_len );

Gildas Bazin's avatar
 
Gildas Bazin committed
482
    es_out_Send( p_input->p_es_out, p_stream->p_es, p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
}

/****************************************************************************
 * 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.
 ****************************************************************************/
static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
{
    ogg_packet oggpacket;
    ogg_page oggpage;
    int i_stream;

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

Gildas Bazin's avatar
 
Gildas Bazin committed
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
    while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
    {
        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) );

Gildas Bazin's avatar
 
Gildas Bazin committed
520 521 522
                es_format_Init( &p_stream->fmt, 0, 0 );
                p_stream->b_activated = VLC_TRUE;

Gildas Bazin's avatar
 
Gildas Bazin committed
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
                /* 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 */
                    msg_Err( p_input, "Error reading first page of "
                             "Ogg bitstream data" );
                    return VLC_EGENERIC;
                }

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

                /* Check for Vorbis header */
541
                if( oggpacket.bytes >= 7 &&
Gildas Bazin's avatar
 
Gildas Bazin committed
542 543 544 545 546
                    ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
                {
                    oggpack_buffer opb;

                    msg_Dbg( p_input, "found vorbis header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
547 548
                    p_stream->fmt.i_cat = AUDIO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
Gildas Bazin's avatar
 
Gildas Bazin committed
549

Gildas Bazin's avatar
 
Gildas Bazin committed
550 551 552 553 554
                    /* Signal that we want to keep a backup of the vorbis
                     * stream headers. They will be used when switching between
                     * audio streams. */
                    p_stream->b_force_backup = 1;

Gildas Bazin's avatar
 
Gildas Bazin committed
555 556
                    /* Cheat and get additionnal info ;) */
                    oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
Gildas Bazin's avatar
 
Gildas Bazin committed
557
                    oggpack_adv( &opb, 88 );
Gildas Bazin's avatar
 
Gildas Bazin committed
558 559 560
                    p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
                    p_stream->f_rate = p_stream->fmt.audio.i_rate =
                        oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
561
                    oggpack_adv( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
562
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
563
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
564
                /* Check for Speex header */
Gildas Bazin's avatar
 
Gildas Bazin committed
565
                else if( oggpacket.bytes >= 7 &&
Gildas Bazin's avatar
 
Gildas Bazin committed
566 567 568 569
                    ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
                {
                    oggpack_buffer opb;

Gildas Bazin's avatar
 
Gildas Bazin committed
570 571
                    p_stream->fmt.i_cat = AUDIO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
Gildas Bazin's avatar
 
Gildas Bazin committed
572 573 574 575 576 577 578 579 580 581 582

                    /* Signal that we want to keep a backup of the vorbis
                     * stream headers. They will be used when switching between
                     * audio streams. */
                    p_stream->b_force_backup = 1;

                    /* Cheat and get additionnal info ;) */
                    oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
                    oggpack_adv( &opb, 224 );
                    oggpack_adv( &opb, 32 ); /* speex_version_id */
                    oggpack_adv( &opb, 32 ); /* header_size */
Gildas Bazin's avatar
 
Gildas Bazin committed
583 584
                    p_stream->f_rate = p_stream->fmt.audio.i_rate =
                        oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
585 586
                    oggpack_adv( &opb, 32 ); /* mode */
                    oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
Gildas Bazin's avatar
 
Gildas Bazin committed
587 588 589 590 591 592 593
                    p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );

                    msg_Dbg( p_input, "found speex header, channels: %i, "
                             "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
594
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
595 596 597 598 599 600 601 602 603 604 605
                /* Check for Flac header */
                else if( oggpacket.bytes >= 4 &&
                    ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
                {
                    msg_Dbg( p_input, "found Flac header" );

                    /* 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
606 607
                    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
608
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
609 610 611 612
                /* Check for Theora header */
                else if( oggpacket.bytes >= 7 &&
                         ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
613
                    bs_t bitstream;
Gildas Bazin's avatar
 
Gildas Bazin committed
614 615 616 617 618
                    int i_fps_numerator;
                    int i_fps_denominator;
                    int i_keyframe_frequency_force;

                    msg_Dbg( p_input, "found theora header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
619 620
                    p_stream->fmt.i_cat = VIDEO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
Gildas Bazin's avatar
 
Gildas Bazin committed
621

Gildas Bazin's avatar
 
Gildas Bazin committed
622 623 624 625 626
                    /* Signal that we want to keep a backup of the vorbis
                     * stream headers. They will be used when switching between
                     * audio streams. */
                    p_stream->b_force_backup = 1;

Gildas Bazin's avatar
 
Gildas Bazin committed
627
                    /* Cheat and get additionnal info ;) */
Gildas Bazin's avatar
 
Gildas Bazin committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
                    bs_init( &bitstream, oggpacket.packet, oggpacket.bytes );
                    bs_skip( &bitstream, 56 );
                    bs_read( &bitstream, 8 ); /* major version num */
                    bs_read( &bitstream, 8 ); /* minor version num */
                    bs_read( &bitstream, 8 ); /* subminor version num */
                    bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
                    bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
                    bs_read( &bitstream, 24 ); /* frame width */
                    bs_read( &bitstream, 24 ); /* frame height */
                    bs_read( &bitstream, 8 ); /* x offset */
                    bs_read( &bitstream, 8 ); /* y offset */

                    i_fps_numerator = bs_read( &bitstream, 32 );
                    i_fps_denominator = bs_read( &bitstream, 32 );
                    bs_read( &bitstream, 24 ); /* aspect_numerator */
                    bs_read( &bitstream, 24 ); /* aspect_denominator */
                    i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
                    bs_read( &bitstream, 8 ); /* colorspace */
                    p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
                    bs_read( &bitstream, 6 ); /* quality */
Gildas Bazin's avatar
 
Gildas Bazin committed
648 649 650 651 652 653 654 655 656 657

                    /* granule_shift = i_log( frequency_force -1 ) */
                    p_stream->i_theora_keyframe_granule_shift = 0;
                    i_keyframe_frequency_force--;
                    while( i_keyframe_frequency_force )
                    {
                        p_stream->i_theora_keyframe_granule_shift++;
                        i_keyframe_frequency_force >>= 1;
                    }

Gildas Bazin's avatar
 
Gildas Bazin committed
658
                    p_stream->f_rate = ((float)i_fps_numerator) /
Gildas Bazin's avatar
 
Gildas Bazin committed
659 660 661
                                                i_fps_denominator;
                    msg_Dbg( p_input,
                             "found theora header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
662
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
663 664 665 666 667 668 669 670
                }
                /* Check for Tarkin header */
                else if( oggpacket.bytes >= 7 &&
                         ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
                {
                    oggpack_buffer opb;

                    msg_Dbg( p_input, "found tarkin header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
671 672
                    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
673 674 675 676 677

                    /* 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
678
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
679 680 681
                    p_stream->f_rate = 2; /* FIXME */
                    msg_Dbg( p_input,
                             "found tarkin header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
682
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
683
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
684 685 686 687 688 689 690 691 692 693
                else if( oggpacket.bytes >= 142 &&
                         !strncmp( &oggpacket.packet[1],
                                   "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
694 695
                        p_stream->fmt.i_cat = VIDEO_ES;
                        p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
696 697 698 699 700
                            VLC_FOURCC( oggpacket.packet[68],
                                        oggpacket.packet[69],
                                        oggpacket.packet[70],
                                        oggpacket.packet[71] );
                        msg_Dbg( p_input, "found video header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
701
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
702 703 704

                        p_stream->f_rate = 10000000.0 /
                            GetQWLE((oggpacket.packet+164));
Gildas Bazin's avatar
 
Gildas Bazin committed
705
                        p_stream->fmt.video.i_bits_per_pixel =
Gildas Bazin's avatar
 
Gildas Bazin committed
706
                            GetWLE((oggpacket.packet+182));
Gildas Bazin's avatar
 
Gildas Bazin committed
707 708 709 710
                        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
711
                            GetDWLE((oggpacket.packet+176));
Gildas Bazin's avatar
 
Gildas Bazin committed
712
                        p_stream->fmt.video.i_height =
Gildas Bazin's avatar
 
Gildas Bazin committed
713 714 715
                            GetDWLE((oggpacket.packet+180));

                        msg_Dbg( p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
716 717 718 719 720 721
                                 "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
722 723 724 725 726
                    }
                    /* 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
727
                        unsigned int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
728

Gildas Bazin's avatar
 
Gildas Bazin committed
729
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
730 731

                        i_extra_size = GetWLE((oggpacket.packet+140));
Gildas Bazin's avatar
 
Gildas Bazin committed
732
                        if( i_extra_size )
Gildas Bazin's avatar
 
Gildas Bazin committed
733
                        {
Gildas Bazin's avatar
 
Gildas Bazin committed
734 735 736 737
                            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
738 739
                        }

Gildas Bazin's avatar
 
Gildas Bazin committed
740 741
                        i_format_tag = GetWLE((oggpacket.packet+124));
                        p_stream->fmt.audio.i_channels =
Gildas Bazin's avatar
 
Gildas Bazin committed
742
                            GetWLE((oggpacket.packet+126));
Gildas Bazin's avatar
 
Gildas Bazin committed
743
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
Gildas Bazin's avatar
 
Gildas Bazin committed
744
                            GetDWLE((oggpacket.packet+128));
Gildas Bazin's avatar
 
Gildas Bazin committed
745 746 747
                        p_stream->fmt.i_bitrate =
                            GetDWLE((oggpacket.packet+132)) * 8;
                        p_stream->fmt.audio.i_blockalign =
Gildas Bazin's avatar
 
Gildas Bazin committed
748
                            GetWLE((oggpacket.packet+136));
Gildas Bazin's avatar
 
Gildas Bazin committed
749
                        p_stream->fmt.audio.i_bitspersample =
Gildas Bazin's avatar
 
Gildas Bazin committed
750 751
                            GetWLE((oggpacket.packet+138));

Gildas Bazin's avatar
 
Gildas Bazin committed
752
                        switch( i_format_tag )
Gildas Bazin's avatar
 
Gildas Bazin committed
753 754
                        {
                        case WAVE_FORMAT_PCM:
Gildas Bazin's avatar
 
Gildas Bazin committed
755
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
756 757 758 759
                                VLC_FOURCC( 'a', 'r', 'a', 'w' );
                            break;
                        case WAVE_FORMAT_MPEG:
                        case WAVE_FORMAT_MPEGLAYER3:
Gildas Bazin's avatar
 
Gildas Bazin committed
760
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
761 762 763
                                VLC_FOURCC( 'm', 'p', 'g', 'a' );
                            break;
                        case WAVE_FORMAT_A52:
Gildas Bazin's avatar
 
Gildas Bazin committed
764
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
765 766 767
                                VLC_FOURCC( 'a', '5', '2', ' ' );
                            break;
                        case WAVE_FORMAT_WMA1:
Gildas Bazin's avatar
 
Gildas Bazin committed
768
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
769 770 771
                                VLC_FOURCC( 'w', 'm', 'a', '1' );
                            break;
                        case WAVE_FORMAT_WMA2:
Gildas Bazin's avatar
 
Gildas Bazin committed
772
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
773 774 775
                                VLC_FOURCC( 'w', 'm', 'a', '2' );
                            break;
                        default:
Gildas Bazin's avatar
 
Gildas Bazin committed
776 777 778
                            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
779 780 781
                        }

                        msg_Dbg( p_input, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
782
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
783 784
                        msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
785 786 787 788 789
                                 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 );
790

Gildas Bazin's avatar
 
Gildas Bazin committed
791 792 793 794 795 796 797 798 799
                    }
                    else
                    {
                        msg_Dbg( p_input, "stream %d has an old header "
                            "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
800
                else if( (*oggpacket.packet & PACKET_TYPE_BITS )
Sam Hocevar's avatar
Sam Hocevar committed
801
                         == PACKET_TYPE_HEADER &&
Gildas Bazin's avatar
 
Gildas Bazin committed
802 803 804 805 806 807 808
                         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
809
                        p_stream->fmt.i_cat = VIDEO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
810 811 812 813

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

Gildas Bazin's avatar
 
Gildas Bazin committed
814 815 816
                        p_stream->fmt.i_codec =
                            VLC_FOURCC( st->subtype[0], st->subtype[1],
                                        st->subtype[2], st->subtype[3] );
Gildas Bazin's avatar
 
Gildas Bazin committed
817
                        msg_Dbg( p_input, "found video header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
818
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
819

Gildas Bazin's avatar
 
Gildas Bazin committed
820
                        p_stream->f_rate = 10000000.0 /
821
                            GetQWLE(&st->time_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
822
                        p_stream->fmt.video.i_bits_per_pixel =
823
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
824
                        p_stream->fmt.video.i_width =
825
                            GetDWLE(&st->sh.video.width);
Gildas Bazin's avatar
 
Gildas Bazin committed
826
                        p_stream->fmt.video.i_height =
827
                            GetDWLE(&st->sh.video.height);
Gildas Bazin's avatar
 
Gildas Bazin committed
828 829

                        msg_Dbg( p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
830 831 832 833 834
                                 "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
835 836 837 838 839
                    }
                    /* Check for audio header (new format) */
                    else if( !strncmp( st->streamtype, "audio", 5 ) )
                    {
                        char p_buffer[5];
Gildas Bazin's avatar
 
Gildas Bazin committed
840
                        int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
841

Gildas Bazin's avatar
 
Gildas Bazin committed
842
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
843 844 845 846

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

Gildas Bazin's avatar
 
Gildas Bazin committed
847 848
                        memcpy( p_buffer, st->subtype, 4 );
                        p_buffer[4] = '\0';
Gildas Bazin's avatar
 
Gildas Bazin committed
849 850
                        i_format_tag = strtol(p_buffer,NULL,16);
                        p_stream->fmt.audio.i_channels =
851
                            GetWLE(&st->sh.audio.channels);
Gildas Bazin's avatar
 
Gildas Bazin committed
852
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
853
                            GetQWLE(&st->samples_per_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
854 855 856
                        p_stream->fmt.i_bitrate =
                            GetDWLE(&st->sh.audio.avgbytespersec) * 8;
                        p_stream->fmt.audio.i_blockalign =
857
                            GetWLE(&st->sh.audio.blockalign);
Gildas Bazin's avatar
 
Gildas Bazin committed
858
                        p_stream->fmt.audio.i_bitspersample =
859
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
860

Gildas Bazin's avatar
 
Gildas Bazin committed
861
                        switch( i_format_tag )
Gildas Bazin's avatar
 
Gildas Bazin committed
862 863
                        {
                        case WAVE_FORMAT_PCM:
Gildas Bazin's avatar
 
Gildas Bazin committed
864
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
865 866 867 868
                                VLC_FOURCC( 'a', 'r', 'a', 'w' );
                            break;
                        case WAVE_FORMAT_MPEG:
                        case WAVE_FORMAT_MPEGLAYER3:
Gildas Bazin's avatar
 
Gildas Bazin committed
869
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
870 871 872
                                VLC_FOURCC( 'm', 'p', 'g', 'a' );
                            break;
                        case WAVE_FORMAT_A52:
Gildas Bazin's avatar
 
Gildas Bazin committed
873
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
874 875 876
                                VLC_FOURCC( 'a', '5', '2', ' ' );
                            break;
                        case WAVE_FORMAT_WMA1:
Gildas Bazin's avatar
 
Gildas Bazin committed
877
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
878 879 880
                                VLC_FOURCC( 'w', 'm', 'a', '1' );
                            break;
                        case WAVE_FORMAT_WMA2:
Gildas Bazin's avatar
 
Gildas Bazin committed
881
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
882 883 884
                                VLC_FOURCC( 'w', 'm', 'a', '2' );
                            break;
                        default:
Gildas Bazin's avatar
 
Gildas Bazin committed
885 886 887
                            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
888 889 890
                        }

                        msg_Dbg( p_input, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
891
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
892 893
                        msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
894 895 896 897 898
                                 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
899
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
900 901 902
                    /* Check for text (subtitles) header */
                    else if( !strncmp(st->streamtype, "text", 4) )
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
903 904 905
                        /* We need to get rid of the header packet */
                        ogg_stream_packetout( &p_stream->os, &oggpacket );

Gildas Bazin's avatar
 
Gildas Bazin committed
906
                        msg_Dbg( p_input, "found text subtitles header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
907 908
                        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
909
                        p_stream->f_rate = 1000; /* granulepos is in milisec */
Gildas Bazin's avatar
 
Gildas Bazin committed
910
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
911 912 913 914 915 916 917 918
                    else
                    {
                        msg_Dbg( p_input, "stream %d has a header marker "
                            "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
919 920
                else
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
921 922
                    msg_Dbg( p_input, "stream %d is of unknown type",
                             p_ogg->i_streams-1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
923 924
                    free( p_stream );
                    p_ogg->i_streams--;
Gildas Bazin's avatar
 
Gildas Bazin committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
                }

                if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
                    return VLC_EGENERIC;
            }

            /* This is the first data page, which means we are now finished
             * with the initial pages. We just need to store it in the relevant
             * bitstream. */
            for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
            {
                if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
                                       &oggpage ) == 0 )
                {
                    break;
                }
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
942

Gildas Bazin's avatar
 
Gildas Bazin committed
943 944 945
            return VLC_SUCCESS;
        }
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
946 947
#undef p_stream

Gildas Bazin's avatar
 
Gildas Bazin committed
948 949 950 951 952 953 954 955 956
    return VLC_EGENERIC;
}

/*****************************************************************************
 * Activate: initializes ogg demux structures
 *****************************************************************************/
static int Activate( vlc_object_t * p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
Gildas Bazin's avatar
 
Gildas Bazin committed
957 958
    demux_sys_t    *p_ogg;
    int            b_forced;
Gildas Bazin's avatar
 
Gildas Bazin committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975

    p_input->p_demux_data = NULL;
    b_forced = ( ( *p_input->psz_demux )&&
                 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;

    /* Check if we are dealing with an ogg stream */
    if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
        return -1;

    /* Allocate p_ogg */
    if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
    {
        msg_Err( p_input, "out of memory" );
        goto error;
    }
    memset( p_ogg, 0, sizeof( demux_sys_t ) );
    p_input->p_demux_data = p_ogg;
Gildas Bazin's avatar
 
Gildas Bazin committed
976
    p_ogg->pp_stream = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
977 978 979 980

    /* Initialize the Ogg physical bitstream parser */
    ogg_sync_init( &p_ogg->oy );

Gildas Bazin's avatar
 
Gildas Bazin committed
981
    /* Set exported functions */
Gildas Bazin's avatar
 
Gildas Bazin committed
982
    p_input->pf_demux = Demux;
Gildas Bazin's avatar
 
Gildas Bazin committed
983
    p_input->pf_demux_control = Control;
Gildas Bazin's avatar
 
Gildas Bazin committed
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998

    /* Initialize access plug-in structures. */
    if( p_input->i_mtu == 0 )
    {
        /* Improve speed. */
        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
    }

    /* Create one program */
    vlc_mutex_lock( &p_input->stream.stream_lock );
    if( input_InitStream( p_input, 0 ) == -1)
    {
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        msg_Err( p_input, "cannot init stream" );
        goto error;
Gildas Bazin's avatar
 
Gildas Bazin committed
999
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1000 1001 1002 1003 1004 1005 1006
    if( input_AddProgram( p_input, 0, 0) == NULL )
    {
        vlc_mutex_unlock( &p_input->stream.stream_lock );
        msg_Err( p_input, "cannot add program" );
        goto error;
    }
    p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
Gildas Bazin's avatar
 
Gildas Bazin committed
1007 1008 1009
    vlc_mutex_unlock( &p_input->stream.stream_lock );

    /* Begnning of stream, tell the demux to look for elementary streams. */
Gildas Bazin's avatar
 
Gildas Bazin committed
1010 1011 1012
    p_ogg->i_eos = 0;

    p_ogg->i_prev_sync_state = SYNCHRO_REINIT;
Gildas Bazin's avatar
 
Gildas Bazin committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022

    return 0;

 error:
    Deactivate( (vlc_object_t *)p_input );
    return -1;

}

/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
1023 1024
 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
 *                        Elementary streams.
Gildas Bazin's avatar
 
Gildas Bazin committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
 ****************************************************************************/
static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg)
{
    int i_stream;

    /* Find the logical streams embedded in the physical stream and
     * initialize our p_ogg structure. */
    if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
    {
        msg_Warn( p_input, "couldn't find any ogg logical stream" );
        return VLC_EGENERIC;
    }

    vlc_mutex_lock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1039 1040
    p_input->stream.i_mux_rate = 0;
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1041 1042 1043 1044

    for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
    {
#define p_stream p_ogg->pp_stream[i_stream]
Gildas Bazin's avatar
 
Gildas Bazin committed
1045 1046 1047
        if( p_stream->fmt.i_codec != VLC_FOURCC('f','l','a','c') )
            p_stream->p_es = es_out_Add( p_input->p_es_out, &p_stream->fmt );

Gildas Bazin's avatar
 
Gildas Bazin committed
1048
        vlc_mutex_lock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1049
        p_input->stream.i_mux_rate += (p_stream->fmt.i_bitrate / ( 8 * 50 ));
Gildas Bazin's avatar
 
Gildas Bazin committed
1050
        vlc_mutex_unlock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1051 1052

        p_stream->i_pcr  = p_stream->i_previous_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1053
        p_stream->b_reinit = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1054 1055 1056
#undef p_stream
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1057 1058
    return VLC_SUCCESS;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
1059

Gildas Bazin's avatar
 
Gildas Bazin committed
1060 1061 1062 1063 1064 1065
/****************************************************************************
 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
 ****************************************************************************/
static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg )
{
    int i_stream, j;
Gildas Bazin's avatar
 
Gildas Bazin committed
1066

Gildas Bazin's avatar
 
Gildas Bazin committed
1067 1068 1069
#define p_stream p_ogg->pp_stream[i_stream]
    for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1070 1071
        es_out_Del( p_input->p_es_out, p_stream->p_es );

Gildas Bazin's avatar
 
Gildas Bazin committed
1072
        vlc_mutex_lock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1073
        p_input->stream.i_mux_rate -= (p_stream->fmt.i_bitrate / ( 8 * 50 ));
Gildas Bazin's avatar
 
Gildas Bazin committed
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
        vlc_mutex_unlock( &p_input->stream.stream_lock );

        ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
        for( j = 0; j < p_ogg->pp_stream[i_stream]->i_packets_backup; j++ )
        {
            free( p_ogg->pp_stream[i_stream]->p_packets_backup[j].packet );
        }
        if( p_ogg->pp_stream[i_stream]->p_packets_backup)
            free( p_ogg->pp_stream[i_stream]->p_packets_backup );

        free( p_ogg->pp_stream[i_stream] );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1086
#undef p_stream
Gildas Bazin's avatar
 
Gildas Bazin committed
1087 1088 1089 1090 1091

    /* Reinit p_ogg */
    if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
    p_ogg->pp_stream = NULL;
    p_ogg->i_streams = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1092 1093 1094 1095 1096 1097 1098 1099
}

/*****************************************************************************
 * Deactivate: frees unused data
 *****************************************************************************/
static void Deactivate( vlc_object_t *p_this )
{
    input_thread_t *p_input = (input_thread_t *)p_this;
Sam Hocevar's avatar
Sam Hocevar committed
1100
    demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data  ;
Gildas Bazin's avatar
 
Gildas Bazin committed
1101 1102 1103 1104 1105 1106

    if( p_ogg )
    {
        /* Cleanup the bitstream parser */
        ogg_sync_clear( &p_ogg->oy );

Gildas Bazin's avatar
 
Gildas Bazin committed
1107
        Ogg_EndOfStream( p_input, p_ogg );
Gildas Bazin's avatar
 
Gildas Bazin committed
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119

        free( p_ogg );
    }
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( input_thread_t * p_input )
{
Gildas Bazin's avatar
 
Gildas Bazin committed
1120
    demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
1121 1122
    ogg_page    oggpage;
    ogg_packet  oggpacket;
Gildas Bazin's avatar
 
Gildas Bazin committed
1123 1124
    int         i_stream;

Gildas Bazin's avatar
 
Gildas Bazin committed
1125 1126
#define p_stream p_ogg->pp_stream[i_stream]

Gildas Bazin's avatar
 
Gildas Bazin committed
1127
    if( p_ogg->i_eos == p_ogg->i_streams )
Gildas Bazin's avatar
 
Gildas Bazin committed
1128
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1129 1130 1131 1132 1133 1134
        if( p_ogg->i_eos )
        {
            msg_Dbg( p_input, "end of a group of logical streams" );
            Ogg_EndOfStream( p_input, p_ogg );
        }

Gildas Bazin's avatar
 
Gildas Bazin committed
1135
        if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1136
        p_ogg->i_eos = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1137 1138 1139 1140

        msg_Dbg( p_input, "beginning of a group of logical streams" );

        p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
Gildas Bazin's avatar
 
Gildas Bazin committed
1141
        input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
1142
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1143

Gildas Bazin's avatar
 
Gildas Bazin committed
1144
    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
Gildas Bazin's avatar
 
Gildas Bazin committed
1145 1146
    {
        msg_Warn( p_input, "synchro reinit" );
Gildas Bazin's avatar
 
Gildas Bazin committed
1147 1148 1149 1150 1151

        for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
        {
            /* we'll trash all the data until we find the next pcr */
            p_stream->b_reinit = 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1152 1153
            p_stream->i_pcr = -1;
            p_stream->i_interpolated_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1154
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
1155 1156
        if( p_ogg->i_prev_sync_state != SYNCHRO_REINIT )
            ogg_sync_reset( &p_ogg->oy );
Gildas Bazin's avatar
 
Gildas Bazin committed
1157 1158
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1159 1160
    p_ogg->i_prev_sync_state =
        p_input->stream.p_selected_program->i_synchro_state;
Gildas Bazin's avatar
 
Gildas Bazin committed
1161

Gildas Bazin's avatar
 
Gildas Bazin committed
1162
    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
1163
     * Demux an ogg page from the stream
Gildas Bazin's avatar
 
Gildas Bazin committed
1164
     */
Gildas Bazin's avatar
 
Gildas Bazin committed
1165
    if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
1166
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1167 1168
        return 0; /* EOF */
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1169

Gildas Bazin's avatar
 
Gildas Bazin committed
1170
    /* Test for End of Stream */
Gildas Bazin's avatar
 
Gildas Bazin committed
1171
    if( ogg_page_eos( &oggpage ) ) p_ogg->i_eos++;
Gildas Bazin's avatar
 
Gildas Bazin committed
1172 1173 1174 1175 1176 1177 1178 1179


    for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
    {
        if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
            continue;

        while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
1180
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1181
            if( p_stream->b_reinit )
Gildas Bazin's avatar
 
Gildas Bazin committed
1182
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
1183 1184 1185 1186 1187
                /* 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 )
Gildas Bazin's avatar
 
Gildas Bazin committed
1188
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
1189
                    p_stream->b_reinit = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1190
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
1191
                else
Gildas Bazin's avatar
 
Gildas Bazin committed
1192
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
1193 1194 1195
                    p_stream->i_interpolated_pcr = -1;
                    continue;
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
1196

Gildas Bazin's avatar
 
Gildas Bazin committed
1197
                /* An Ogg/vorbis packet contains an end date granulepos */
Gildas Bazin's avatar
 
Gildas Bazin committed
1198 1199 1200
                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
1201 1202
                {
                    if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
1203
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1204
                        Ogg_DecodePacket( p_input, p_stream, &oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
1205
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1206 1207 1208 1209 1210
                    else
                    {
                        input_ClockManageRef( p_input,
                            p_input->stream.p_selected_program, p_ogg->i_pcr );
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1211
                    continue;
Gildas Bazin's avatar
 
Gildas Bazin committed
1212
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
1213
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
1214 1215

            Ogg_DecodePacket( p_input, p_stream, &oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
1216
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
1217
        break;
Gildas Bazin's avatar
 
Gildas Bazin committed
1218 1219
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1220
    i_stream = 0; p_ogg->i_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1221
    for( ; i_stream < p_ogg->i_streams; i_stream++ )
Gildas Bazin's avatar
 
Gildas Bazin committed
1222
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1223
        if( p_stream->fmt.i_cat == SPU_ES )
Gildas Bazin's avatar
 
Gildas Bazin committed
1224
            continue;
Gildas Bazin's avatar
 
Gildas Bazin committed
1225 1226
        if( p_stream->i_interpolated_pcr < 0 )
            continue;
Gildas Bazin's avatar
 
Gildas Bazin committed
1227

Gildas Bazin's avatar
 
Gildas Bazin committed
1228
        if( p_ogg->i_pcr < 0 || p_stream->i_interpolated_pcr < p_ogg->i_pcr )
Gildas Bazin's avatar
 
Gildas Bazin committed
1229 1230 1231
            p_ogg->i_pcr = p_stream->i_interpolated_pcr;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1232
    if( p_input->stream.p_selected_program->i_synchro_state != SYNCHRO_REINIT )
Gildas Bazin's avatar
 
Gildas Bazin committed
1233 1234 1235 1236
    {
        input_ClockManageRef( p_input, p_input->stream.p_selected_program,
                              p_ogg->i_pcr );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1237

Gildas Bazin's avatar
 
Gildas Bazin committed
1238
#undef p_stream
Gildas Bazin's avatar
 
Gildas Bazin committed
1239

Gildas Bazin's avatar
 
Gildas Bazin committed
1240
    return 1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1241
}
Gildas Bazin's avatar
 
Gildas Bazin committed
1242 1243 1244 1245 1246 1247 1248

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( input_thread_t *p_input, int i_query, va_list args )
{
    demux_sys_t *p_ogg  = (demux_sys_t *)p_input->p_demux_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
1249
    int64_t *pi64;
Gildas Bazin's avatar
 
Gildas Bazin committed
1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

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

        default:
            return demux_vaControlDefault( p_input, i_query, args );
    }
}