ogg.c 51.1 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
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
4
 * Copyright (C) 2001-2003 VideoLAN
Gildas Bazin's avatar
 
Gildas Bazin committed
5
 * $Id: ogg.c,v 1.58 2004/02/15 13:16:43 gbazin Exp $
Gildas Bazin's avatar
 
Gildas Bazin committed
6
 *
7
 * Author: 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     i_old_synchro_state;
Gildas Bazin's avatar
 
Gildas Bazin committed
81 82

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

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

Gildas Bazin's avatar
 
Gildas Bazin committed
93 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
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
130 131 132 133 134 135
/*****************************************************************************
 * 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
136
static int  Control   ( input_thread_t *, int, va_list );
Gildas Bazin's avatar
 
Gildas Bazin committed
137 138 139 140

/* 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
141
static void Ogg_UpdatePCR    ( logical_stream_t *, ogg_packet * );
Gildas Bazin's avatar
 
Gildas Bazin committed
142
static void Ogg_DecodePacket ( input_thread_t *p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
143
                               logical_stream_t *p_stream, ogg_packet * );
Gildas Bazin's avatar
 
Gildas Bazin committed
144 145 146 147

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
148 149 150 151 152

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
vlc_module_begin();
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
153
    set_description( _("Ogg stream demuxer" ) );
Gildas Bazin's avatar
 
Gildas Bazin committed
154 155 156 157 158 159 160 161 162 163
    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
164
    uint8_t *p_peek;
Gildas Bazin's avatar
 
Gildas Bazin committed
165 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
    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
206 207 208 209 210 211 212
/****************************************************************************
 * 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
213
    /* Convert the granulepos into a pcr */
Gildas Bazin's avatar
 
Gildas Bazin committed
214 215
    if( p_oggpacket->granulepos >= 0 )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
216
        if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
        {
            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
236
        p_stream->i_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
237

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

Gildas Bazin's avatar
 
Gildas Bazin committed
249 250 251 252 253 254 255
/****************************************************************************
 * 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
256 257
    block_t *p_block;
    vlc_bool_t b_selected;
Gildas Bazin's avatar
 
Gildas Bazin committed
258
    int i_header_len = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
259
    mtime_t i_pts = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
260

Gildas Bazin's avatar
 
Gildas Bazin committed
261 262 263 264 265 266 267
    /* Sanity check */
    if( !p_oggpacket->bytes )
    {
        msg_Dbg( p_input, "discarding 0 sized packet" );
        return;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
268 269 270 271
    if( p_stream->b_force_backup )
    {
        ogg_packet *p_packet_backup;
        p_stream->i_packets_backup++;
Gildas Bazin's avatar
 
Gildas Bazin committed
272
        switch( p_stream->fmt.i_codec )
Gildas Bazin's avatar
 
Gildas Bazin committed
273 274
        {
        case VLC_FOURCC( 'v','o','r','b' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
275
        case VLC_FOURCC( 's','p','x',' ' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
276 277 278 279
        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
280
        case VLC_FOURCC( 'f','l','a','c' ):
Gildas Bazin's avatar
 
Gildas Bazin committed
281 282
          if( p_stream->i_packets_backup == 1 ) return;
          else if( p_stream->i_packets_backup == 2 )
Gildas Bazin's avatar
 
Gildas Bazin committed
283 284
          {
              /* Parse the STREAMINFO metadata */
Gildas Bazin's avatar
 
Gildas Bazin committed
285 286 287 288
              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
289
              {
Gildas Bazin's avatar
 
Gildas Bazin committed
290
                  if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
Gildas Bazin's avatar
 
Gildas Bazin committed
291
                  {
Gildas Bazin's avatar
 
Gildas Bazin committed
292 293 294 295 296 297
                      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;

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
298
                      msg_Dbg( p_input, "FLAC header, channels: %i, rate: %i",
Gildas Bazin's avatar
 
Gildas Bazin committed
299 300
                               p_stream->fmt.audio.i_channels,
                               (int)p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
301 302 303
                  }
                  else
                  {
Gildas Bazin's avatar
 
Gildas Bazin committed
304
                      msg_Dbg( p_input, "FLAC STREAMINFO metadata too short" );
Gildas Bazin's avatar
 
Gildas Bazin committed
305
                  }
Gildas Bazin's avatar
 
Gildas Bazin committed
306 307 308 309 310 311 312 313 314 315 316 317 318

                  /* 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
319 320 321 322
              }
              else
              {
                  /* This ain't a STREAMINFO metadata */
Gildas Bazin's avatar
 
Gildas Bazin committed
323
                  msg_Dbg( p_input, "Invalid FLAC STREAMINFO metadata" );
Gildas Bazin's avatar
 
Gildas Bazin committed
324 325
              }
              p_stream->b_force_backup = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
326
              p_stream->i_packets_backup = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
327 328 329 330 331

              if( p_oggpacket->granulepos >= 0 )
                  Ogg_UpdatePCR( p_stream, p_oggpacket );

              p_stream->i_previous_pcr = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
332
              return;
Gildas Bazin's avatar
 
Gildas Bazin committed
333 334 335
          }
          break;

Gildas Bazin's avatar
 
Gildas Bazin committed
336 337 338 339 340 341
        default:
          p_stream->b_force_backup = 0;
          break;
        }

        /* Backup the ogg packet (likely an header packet) */
Gildas Bazin's avatar
 
Gildas Bazin committed
342 343 344 345 346 347 348 349 350
        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
351 352 353 354 355 356 357 358 359

        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
360 361 362 363 364 365
        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
366
    /* Check the ES is selected */
367
    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE,
Gildas Bazin's avatar
 
Gildas Bazin committed
368 369 370
                    p_stream->p_es, &b_selected );

    if( b_selected && !p_stream->b_activated )
Gildas Bazin's avatar
 
Gildas Bazin committed
371
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
        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
388 389
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
390
    /* Convert the pcr into a pts */
Gildas Bazin's avatar
 
Gildas Bazin committed
391 392 393
    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
394
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
        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 );

Gildas Bazin's avatar
 
Gildas Bazin committed
413 414 415 416 417
            /* The granulepos is the end date of the sample */
            i_pts =  input_ClockGetTS( p_input,
                                       p_input->stream.p_selected_program,
                                       p_stream->i_pcr );
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
418
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
419

Gildas Bazin's avatar
 
Gildas Bazin committed
420 421
    /* Convert the granulepos into the next pcr */
    Ogg_UpdatePCR( p_stream, p_oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
422

Gildas Bazin's avatar
 
Gildas Bazin committed
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
    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;

        /* 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 );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
438

Gildas Bazin's avatar
 
Gildas Bazin committed
439 440 441
    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
442 443 444
        p_stream->i_pcr >= 0 )
    {
        p_stream->i_previous_pcr = p_stream->i_pcr;
Gildas Bazin's avatar
 
Gildas Bazin committed
445 446

        /* The granulepos is the start date of the sample */
Gildas Bazin's avatar
 
Gildas Bazin committed
447 448
        i_pts = input_ClockGetTS( p_input, p_input->stream.p_selected_program,
                                  p_stream->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
449
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
450

Gildas Bazin's avatar
 
Gildas Bazin committed
451
    if( !b_selected )
Gildas Bazin's avatar
 
Gildas Bazin committed
452 453 454
    {
        /* 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
455
        p_stream->b_activated = VLC_FALSE;
Gildas Bazin's avatar
 
Gildas Bazin committed
456 457 458
        return;
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
459
    if( !( p_block = block_New( p_input, p_oggpacket->bytes ) ) ) return;
Gildas Bazin's avatar
 
Gildas Bazin committed
460

Gildas Bazin's avatar
 
Gildas Bazin committed
461 462 463 464 465 466 467 468 469 470 471 472 473 474
    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 )
    {
        p_block->i_pts = i_pts;
        p_block->i_dts = 0;
    }
    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
475

Gildas Bazin's avatar
 
Gildas Bazin committed
476 477 478 479 480
    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
481
    {
482
        /* We remove the header from the packet */
Gildas Bazin's avatar
 
Gildas Bazin committed
483 484
        i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
        i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
485 486 487 488
        
        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
489
            int i, lenbytes = 0;
490
        
Gildas Bazin's avatar
 
Gildas Bazin committed
491
            if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
492 493 494 495
            {
                for( i = 0, lenbytes = 0; i < i_header_len; i++ )
                {
                    lenbytes = lenbytes << 8;
Gildas Bazin's avatar
 
Gildas Bazin committed
496
                    lenbytes += *(p_oggpacket->packet + i_header_len - i);
497 498
                }
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
499 500 501 502 503
            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' ) )
504
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
505
                p_block->i_dts = p_block->i_pts + (mtime_t)lenbytes * 1000;
506 507
            }
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
508

509
        i_header_len++;
Gildas Bazin's avatar
 
Gildas Bazin committed
510
        p_block->i_buffer -= i_header_len;
Gildas Bazin's avatar
 
Gildas Bazin committed
511 512
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
513
    if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
514 515
    {
        /* FIXME: the biggest hack I've ever done */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
516
        msg_Warn( p_input, "tarkin pts: "I64Fd", granule: "I64Fd,
Gildas Bazin's avatar
 
Gildas Bazin committed
517
                  p_block->i_pts, p_block->i_dts );
Gildas Bazin's avatar
 
Gildas Bazin committed
518
        msleep(10000);
Gildas Bazin's avatar
 
Gildas Bazin committed
519 520
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
521
    memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
Gildas Bazin's avatar
 
Gildas Bazin committed
522 523
            p_oggpacket->bytes - i_header_len );

Gildas Bazin's avatar
 
Gildas Bazin committed
524
    es_out_Send( p_input->p_es_out, p_stream->p_es, p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
}

/****************************************************************************
 * 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
543 544
#define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]

Gildas Bazin's avatar
 
Gildas Bazin committed
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    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
562 563 564
                es_format_Init( &p_stream->fmt, 0, 0 );
                p_stream->b_activated = VLC_TRUE;

Gildas Bazin's avatar
 
Gildas Bazin committed
565 566 567 568 569 570 571 572 573
                /* 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 */
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
574
                    msg_Err( p_input, "error reading first page of "
Gildas Bazin's avatar
 
Gildas Bazin committed
575 576 577 578 579 580 581 582
                             "Ogg bitstream data" );
                    return VLC_EGENERIC;
                }

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

                /* Check for Vorbis header */
583
                if( oggpacket.bytes >= 7 &&
Gildas Bazin's avatar
 
Gildas Bazin committed
584 585 586 587 588
                    ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
                {
                    oggpack_buffer opb;

                    msg_Dbg( p_input, "found vorbis header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
589 590
                    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
591

Gildas Bazin's avatar
 
Gildas Bazin committed
592 593 594 595 596
                    /* 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
597 598
                    /* Cheat and get additionnal info ;) */
                    oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
Gildas Bazin's avatar
 
Gildas Bazin committed
599
                    oggpack_adv( &opb, 88 );
Gildas Bazin's avatar
 
Gildas Bazin committed
600 601 602
                    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
603
                    oggpack_adv( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
604
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
605
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
606
                /* Check for Speex header */
Gildas Bazin's avatar
 
Gildas Bazin committed
607
                else if( oggpacket.bytes >= 7 &&
Gildas Bazin's avatar
 
Gildas Bazin committed
608 609 610 611
                    ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
                {
                    oggpack_buffer opb;

Gildas Bazin's avatar
 
Gildas Bazin committed
612 613
                    p_stream->fmt.i_cat = AUDIO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
Gildas Bazin's avatar
 
Gildas Bazin committed
614 615 616 617 618 619 620 621 622 623 624

                    /* 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
625 626
                    p_stream->f_rate = p_stream->fmt.audio.i_rate =
                        oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
627 628
                    oggpack_adv( &opb, 32 ); /* mode */
                    oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
Gildas Bazin's avatar
 
Gildas Bazin committed
629 630 631 632 633 634 635
                    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
636
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
637 638 639 640
                /* Check for Flac header */
                else if( oggpacket.bytes >= 4 &&
                    ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
                {
Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
641
                    msg_Dbg( p_input, "found FLAC header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
642 643 644 645 646 647

                    /* 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
648 649
                    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
650
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
651 652 653 654
                /* Check for Theora header */
                else if( oggpacket.bytes >= 7 &&
                         ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
655
                    bs_t bitstream;
Gildas Bazin's avatar
 
Gildas Bazin committed
656 657 658 659 660
                    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
661 662
                    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
663

Gildas Bazin's avatar
 
Gildas Bazin committed
664 665 666 667 668
                    /* 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
669
                    /* Cheat and get additionnal info ;) */
Gildas Bazin's avatar
 
Gildas Bazin committed
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689
                    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
690 691 692 693 694 695 696 697 698 699

                    /* 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
700
                    p_stream->f_rate = ((float)i_fps_numerator) /
Gildas Bazin's avatar
 
Gildas Bazin committed
701 702 703
                                                i_fps_denominator;
                    msg_Dbg( p_input,
                             "found theora header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
704
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
705 706 707 708 709 710

                    /* Save this data in p_extra for ffmpeg */
                    p_stream->fmt.i_extra = oggpacket.bytes;
                    p_stream->fmt.p_extra = malloc( oggpacket.bytes );
                    memcpy( p_stream->fmt.p_extra,
                            oggpacket.packet, oggpacket.bytes );
Gildas Bazin's avatar
 
Gildas Bazin committed
711 712 713 714 715 716 717 718
                }
                /* 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
719 720
                    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
721 722 723 724 725

                    /* 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
726
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
727 728 729
                    p_stream->f_rate = 2; /* FIXME */
                    msg_Dbg( p_input,
                             "found tarkin header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
730
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
731
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
732 733 734 735 736 737 738 739 740 741
                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
742 743
                        p_stream->fmt.i_cat = VIDEO_ES;
                        p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
744 745 746 747 748
                            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
749
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
750 751 752

                        p_stream->f_rate = 10000000.0 /
                            GetQWLE((oggpacket.packet+164));
Gildas Bazin's avatar
 
Gildas Bazin committed
753
                        p_stream->fmt.video.i_bits_per_pixel =
Gildas Bazin's avatar
 
Gildas Bazin committed
754
                            GetWLE((oggpacket.packet+182));
Gildas Bazin's avatar
 
Gildas Bazin committed
755 756 757 758
                        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
759
                            GetDWLE((oggpacket.packet+176));
Gildas Bazin's avatar
 
Gildas Bazin committed
760
                        p_stream->fmt.video.i_height =
Gildas Bazin's avatar
 
Gildas Bazin committed
761 762 763
                            GetDWLE((oggpacket.packet+180));

                        msg_Dbg( p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
764 765 766 767 768 769
                                 "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
770 771 772 773 774
                    }
                    /* 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
775
                        unsigned int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
776

Gildas Bazin's avatar
 
Gildas Bazin committed
777
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
778 779

                        i_extra_size = GetWLE((oggpacket.packet+140));
Gildas Bazin's avatar
 
Gildas Bazin committed
780
                        if( i_extra_size )
Gildas Bazin's avatar
 
Gildas Bazin committed
781
                        {
Gildas Bazin's avatar
 
Gildas Bazin committed
782 783 784 785
                            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
786 787
                        }

Gildas Bazin's avatar
 
Gildas Bazin committed
788 789
                        i_format_tag = GetWLE((oggpacket.packet+124));
                        p_stream->fmt.audio.i_channels =
Gildas Bazin's avatar
 
Gildas Bazin committed
790
                            GetWLE((oggpacket.packet+126));
Gildas Bazin's avatar
 
Gildas Bazin committed
791
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
Gildas Bazin's avatar
 
Gildas Bazin committed
792
                            GetDWLE((oggpacket.packet+128));
Gildas Bazin's avatar
 
Gildas Bazin committed
793 794 795
                        p_stream->fmt.i_bitrate =
                            GetDWLE((oggpacket.packet+132)) * 8;
                        p_stream->fmt.audio.i_blockalign =
Gildas Bazin's avatar
 
Gildas Bazin committed
796
                            GetWLE((oggpacket.packet+136));
Gildas Bazin's avatar
 
Gildas Bazin committed
797
                        p_stream->fmt.audio.i_bitspersample =
Gildas Bazin's avatar
 
Gildas Bazin committed
798 799
                            GetWLE((oggpacket.packet+138));

Gildas Bazin's avatar
 
Gildas Bazin committed
800
                        switch( i_format_tag )
Gildas Bazin's avatar
 
Gildas Bazin committed
801 802
                        {
                        case WAVE_FORMAT_PCM:
Gildas Bazin's avatar
 
Gildas Bazin committed
803
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
804 805 806 807
                                VLC_FOURCC( 'a', 'r', 'a', 'w' );
                            break;
                        case WAVE_FORMAT_MPEG:
                        case WAVE_FORMAT_MPEGLAYER3:
Gildas Bazin's avatar
 
Gildas Bazin committed
808
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
809 810 811
                                VLC_FOURCC( 'm', 'p', 'g', 'a' );
                            break;
                        case WAVE_FORMAT_A52:
Gildas Bazin's avatar
 
Gildas Bazin committed
812
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
813 814 815
                                VLC_FOURCC( 'a', '5', '2', ' ' );
                            break;
                        case WAVE_FORMAT_WMA1:
Gildas Bazin's avatar
 
Gildas Bazin committed
816
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
817 818 819
                                VLC_FOURCC( 'w', 'm', 'a', '1' );
                            break;
                        case WAVE_FORMAT_WMA2:
Gildas Bazin's avatar
 
Gildas Bazin committed
820
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
821 822 823
                                VLC_FOURCC( 'w', 'm', 'a', '2' );
                            break;
                        default:
Gildas Bazin's avatar
 
Gildas Bazin committed
824 825 826
                            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
827 828 829
                        }

                        msg_Dbg( p_input, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
830
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
831 832
                        msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
833 834 835 836 837
                                 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 );
838

Gildas Bazin's avatar
 
Gildas Bazin committed
839 840 841 842 843 844 845 846 847
                    }
                    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
848
                else if( (*oggpacket.packet & PACKET_TYPE_BITS )
Sam Hocevar's avatar
Sam Hocevar committed
849
                         == PACKET_TYPE_HEADER &&
Gildas Bazin's avatar
 
Gildas Bazin committed
850 851 852 853 854 855 856
                         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
857
                        p_stream->fmt.i_cat = VIDEO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
858 859 860 861

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

Gildas Bazin's avatar
 
Gildas Bazin committed
862 863 864
                        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
865
                        msg_Dbg( p_input, "found video header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
866
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
867

Gildas Bazin's avatar
 
Gildas Bazin committed
868
                        p_stream->f_rate = 10000000.0 /
869
                            GetQWLE(&st->time_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
870
                        p_stream->fmt.video.i_bits_per_pixel =
871
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
872
                        p_stream->fmt.video.i_width =
873
                            GetDWLE(&st->sh.video.width);
Gildas Bazin's avatar
 
Gildas Bazin committed
874
                        p_stream->fmt.video.i_height =
875
                            GetDWLE(&st->sh.video.height);
Gildas Bazin's avatar
 
Gildas Bazin committed
876 877

                        msg_Dbg( p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
878 879 880 881 882
                                 "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
883 884 885 886 887
                    }
                    /* Check for audio header (new format) */
                    else if( !strncmp( st->streamtype, "audio", 5 ) )
                    {
                        char p_buffer[5];
Gildas Bazin's avatar
 
Gildas Bazin committed
888
                        int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
889

Gildas Bazin's avatar
 
Gildas Bazin committed
890
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
891 892 893 894

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

Gildas Bazin's avatar
 
Gildas Bazin committed
895 896
                        memcpy( p_buffer, st->subtype, 4 );
                        p_buffer[4] = '\0';
Gildas Bazin's avatar
 
Gildas Bazin committed
897 898
                        i_format_tag = strtol(p_buffer,NULL,16);
                        p_stream->fmt.audio.i_channels =
899
                            GetWLE(&st->sh.audio.channels);
Gildas Bazin's avatar
 
Gildas Bazin committed
900
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
901
                            GetQWLE(&st->samples_per_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
902 903 904
                        p_stream->fmt.i_bitrate =
                            GetDWLE(&st->sh.audio.avgbytespersec) * 8;
                        p_stream->fmt.audio.i_blockalign =
905
                            GetWLE(&st->sh.audio.blockalign);
Gildas Bazin's avatar
 
Gildas Bazin committed
906
                        p_stream->fmt.audio.i_bitspersample =
907
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
908

Gildas Bazin's avatar
 
Gildas Bazin committed
909
                        switch( i_format_tag )
Gildas Bazin's avatar
 
Gildas Bazin committed
910 911
                        {
                        case WAVE_FORMAT_PCM:
Gildas Bazin's avatar
 
Gildas Bazin committed
912
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
913 914 915 916
                                VLC_FOURCC( 'a', 'r', 'a', 'w' );
                            break;
                        case WAVE_FORMAT_MPEG:
                        case WAVE_FORMAT_MPEGLAYER3:
Gildas Bazin's avatar
 
Gildas Bazin committed
917
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
918 919 920
                                VLC_FOURCC( 'm', 'p', 'g', 'a' );
                            break;
                        case WAVE_FORMAT_A52:
Gildas Bazin's avatar
 
Gildas Bazin committed
921
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
922 923 924
                                VLC_FOURCC( 'a', '5', '2', ' ' );
                            break;
                        case WAVE_FORMAT_WMA1:
Gildas Bazin's avatar
 
Gildas Bazin committed
925
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
926 927 928
                                VLC_FOURCC( 'w', 'm', 'a', '1' );
                            break;
                        case WAVE_FORMAT_WMA2:
Gildas Bazin's avatar
 
Gildas Bazin committed
929
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
930 931 932
                                VLC_FOURCC( 'w', 'm', 'a', '2' );
                            break;
                        default:
Gildas Bazin's avatar
 
Gildas Bazin committed
933 934 935
                            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
936 937 938
                        }

                        msg_Dbg( p_input, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
939
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
940 941
                        msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
942 943 944 945 946
                                 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
947
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
948 949 950
                    /* Check for text (subtitles) header */
                    else if( !strncmp(st->streamtype, "text", 4) )
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
951 952 953
                        /* We need to get rid of the header packet */
                        ogg_stream_packetout( &p_stream->os, &oggpacket );

Gildas Bazin's avatar
 
Gildas Bazin committed
954
                        msg_Dbg( p_input, "found text subtitles header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
955 956
                        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
957
                        p_stream->f_rate = 1000; /* granulepos is in milisec */
Gildas Bazin's avatar
 
Gildas Bazin committed
958
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
959 960 961 962 963 964 965 966
                    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
967 968
                else
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
969 970
                    msg_Dbg( p_input, "stream %d is of unknown type",
                             p_ogg->i_streams-1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
971 972
                    free( p_stream );
                    p_ogg->i_streams--;
Gildas Bazin's avatar
 
Gildas Bazin committed
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
                }

                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
990

Gildas Bazin's avatar
 
Gildas Bazin committed
991 992 993
            return VLC_SUCCESS;
        }
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
994 995
#undef p_stream

Gildas Bazin's avatar
 
Gildas Bazin committed
996 997 998 999 1000 1001 1002 1003 1004
    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
1005 1006
    demux_sys_t    *p_ogg;
    int            b_forced;
Gildas Bazin's avatar
 
Gildas Bazin committed
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

    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
1024
    p_ogg->pp_stream = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
1025 1026 1027 1028

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

Gildas Bazin's avatar
 
Gildas Bazin committed
1029
    /* Set exported functions */
Gildas Bazin's avatar
 
Gildas Bazin committed
1030
    p_input->pf_demux = Demux;
Gildas Bazin's avatar
 
Gildas Bazin committed
1031
    p_input->pf_demux_control = Control;
Gildas Bazin's avatar
 
Gildas Bazin committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

    /* 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
1047
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1048 1049 1050 1051 1052 1053 1054
    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
1055 1056 1057
    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
1058 1059
    p_ogg->i_eos = 0;

Gildas Bazin's avatar
 
Gildas Bazin committed
1060 1061
    p_ogg->i_old_synchro_state = !SYNCHRO_REINIT;

Gildas Bazin's avatar
 
Gildas Bazin committed
1062 1063 1064 1065 1066 1067 1068 1069 1070
    return 0;

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

}

/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
1071 1072
 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
 *                        Elementary streams.
Gildas Bazin's avatar
 
Gildas Bazin committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
 ****************************************************************************/
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
1087 1088
    p_input->stream.i_mux_rate = 0;
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1089 1090 1091 1092

    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
1093 1094 1095
        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
1096
        vlc_mutex_lock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1097
        p_input->stream.i_mux_rate += (p_stream->fmt.i_bitrate / ( 8 * 50 ));
Gildas Bazin's avatar
 
Gildas Bazin committed
1098
        vlc_mutex_unlock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1099

Gildas Bazin's avatar
 
Gildas Bazin committed
1100 1101
        p_stream->i_pcr = p_stream->i_previous_pcr =
            p_stream->i_interpolated_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1102
        p_stream->b_reinit = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1103 1104 1105
#undef p_stream
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1106 1107
    return VLC_SUCCESS;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
1108

Gildas Bazin's avatar
 
Gildas Bazin committed
1109 1110 1111 1112 1113 1114
/****************************************************************************
 * 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
1115

Gildas Bazin's avatar
 
Gildas Bazin committed
1116 1117 1118
#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
1119 1120
        if( p_stream->p_es )
            es_out_Del( p_input->p_es_out, p_stream->p_es );
Gildas Bazin's avatar
 
Gildas Bazin committed
1121

Gildas Bazin's avatar
 
Gildas Bazin committed
1122
        vlc_mutex_lock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1123
        p_input->stream.i_mux_rate -= (p_stream->fmt.i_bitrate / ( 8 * 50 ));
Gildas Bazin's avatar
 
Gildas Bazin committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
        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 );

Gildas Bazin's avatar
 
Gildas Bazin committed
1134
        es_format_Clean( &p_stream->fmt );
Gildas Bazin's avatar
 
Gildas Bazin committed
1135

Gildas Bazin's avatar
 
Gildas Bazin committed
1136 1137
        free( p_ogg->pp_stream[i_stream] );
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1138
#undef p_stream
Gildas Bazin's avatar
 
Gildas Bazin committed
1139 1140 1141 1142 1143

    /* 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
1144 1145 1146 1147 1148 1149 1150 1151
}

/*****************************************************************************
 * 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
1152
    demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data  ;
Gildas Bazin's avatar
 
Gildas Bazin committed
1153 1154 1155 1156 1157 1158

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

Gildas Bazin's avatar
 
Gildas Bazin committed
1159
        Ogg_EndOfStream( p_input, p_ogg );
Gildas Bazin's avatar
 
Gildas Bazin committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171

        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
1172
    demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
1173 1174
    ogg_page    oggpage;
    ogg_packet  oggpacket;
Gildas Bazin's avatar
 
Gildas Bazin committed
1175 1176
    int         i_stream;

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

Gildas Bazin's avatar
 
Gildas Bazin committed
1179
    if( p_ogg->i_eos == p_ogg->i_streams )
Gildas Bazin's avatar
 
Gildas Bazin committed
1180
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1181 1182 1183 1184 1185 1186
        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
1187
        if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1188
        p_ogg->i_eos = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1189 1190 1191 1192

        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
1193
        input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
Gildas Bazin's avatar
 
Gildas Bazin committed
1194
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1195

Gildas Bazin's avatar
 
Gildas Bazin committed
1196
    if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
Gildas Bazin's avatar
 
Gildas Bazin committed
1197 1198
    {
        msg_Warn( p_input, "synchro reinit" );
Gildas Bazin's avatar
 
Gildas Bazin committed
1199

Gildas Bazin's avatar
 
Gildas Bazin committed
1200
        if( p_ogg->i_old_synchro_state != SYNCHRO_REINIT )
Gildas Bazin's avatar
 
Gildas Bazin committed
1201 1202 1203 1204
        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
1205 1206
            p_stream->i_pcr = -1;
            p_stream->i_interpolated_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1207
            ogg_stream_reset( &p_stream->os );
Gildas Bazin's avatar
 
Gildas Bazin committed
1208
        }
Gildas Bazin's avatar
 
Gildas Bazin committed
1209
        if( p_ogg->i_old_synchro_state != SYNCHRO_REINIT )
Gildas Bazin's avatar
 
Gildas Bazin committed
1210
        ogg_sync_reset( &p_ogg->oy );
Gildas Bazin's avatar
 
Gildas Bazin committed
1211 1212
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1213
    /*
Gildas Bazin's avatar
 
Gildas Bazin committed
1214
     * Demux an ogg page from the stream
Gildas Bazin's avatar
 
Gildas Bazin committed
1215
     */
Gildas Bazin's avatar
 
Gildas Bazin committed
1216
    if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
Gildas Bazin's avatar
 
Gildas Bazin committed
1217
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1218 1219
        return 0; /* EOF */
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1220

Gildas Bazin's avatar
 
Gildas Bazin committed
1221
    /* Test for End of Stream */
Gildas Bazin's avatar
 
Gildas Bazin committed
1222
    if( ogg_page_eos( &oggpage ) ) p_ogg->i_eos++;
Gildas Bazin's avatar
 
Gildas Bazin committed
1223 1224 1225 1226 1227 1228 1229 1230


    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
1231
        {
Gildas Bazin's avatar
 
Gildas Bazin committed
1232
            if( p_stream->b_reinit )
Gildas Bazin's avatar
 
Gildas Bazin committed
1233
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
1234 1235 1236 1237 1238
                /* 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
1239
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
1240
                    p_stream->b_reinit = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1241
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
1242
                else
Gildas Bazin's avatar
 
Gildas Bazin committed
1243
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
1244 1245 1246
                    p_stream->i_interpolated_pcr = -1;
                    continue;
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
1247

Gildas Bazin's avatar
 
Gildas Bazin committed
1248
                /* An Ogg/vorbis packet contains an end date granulepos */
Gildas Bazin's avatar
 
Gildas Bazin committed
1249 1250 1251
                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
1252 1253
                {
                    if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
Gildas Bazin's avatar
 
Gildas Bazin committed
1254
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
1255
                        Ogg_DecodePacket( p_input, p_stream, &oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
1256
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1257 1258 1259
                    else
                    {
                        input_ClockManageRef( p_input,
1260 1261
                                      p_input->stream.p_selected_program,
                                      p_stream->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
1262
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1263
                    continue;
Gildas Bazin's avatar
 
Gildas Bazin committed
1264
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
1265
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
1266 1267

            Ogg_DecodePacket( p_input, p_stream, &am