ogg.c 49.3 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
 * Copyright (C) 2001-2003 VideoLAN
5
 * $Id: ogg.c,v 1.48 2003/11/27 04:11:40 fenrir 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 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();
Gildas Bazin's avatar
 
Gildas Bazin 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;
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;

Gildas Bazin's avatar
 
Gildas Bazin 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 327
              p_stream->i_packets_backup = 0;
              return;
Gildas Bazin's avatar
 
Gildas Bazin committed
328 329 330
          }
          break;

Gildas Bazin's avatar
 
Gildas Bazin committed
331 332 333 334 335 336
        default:
          p_stream->b_force_backup = 0;
          break;
        }

        /* Backup the ogg packet (likely an header packet) */
Gildas Bazin's avatar
 
Gildas Bazin committed
337 338 339 340 341 342 343 344 345
        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
346 347 348 349 350 351 352 353 354

        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
355 356 357 358 359 360
        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
361
    /* Check the ES is selected */
362
    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE,
Gildas Bazin's avatar
 
Gildas Bazin committed
363 364 365
                    p_stream->p_es, &b_selected );

    if( b_selected && !p_stream->b_activated )
Gildas Bazin's avatar
 
Gildas Bazin committed
366
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
        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
383 384
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
385
    /* Convert the pcr into a pts */
Gildas Bazin's avatar
 
Gildas Bazin committed
386 387 388
    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
389
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
        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
410
        i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
Gildas Bazin's avatar
 
Gildas Bazin committed
411 412
            input_ClockGetTS( p_input, p_input->stream.p_selected_program,
                              p_stream->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
413 414 415

        /* Convert the granulepos into the next pcr */
        Ogg_UpdatePCR( p_stream, p_oggpacket );
Gildas Bazin's avatar
 
Gildas Bazin committed
416 417 418
    }
    else
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        /* 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
442
        i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
Gildas Bazin's avatar
 
Gildas Bazin committed
443
            input_ClockGetTS( p_input, p_input->stream.p_selected_program,
Gildas Bazin's avatar
 
Gildas Bazin committed
444
                              p_stream->i_pcr );
Gildas Bazin's avatar
 
Gildas Bazin committed
445
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
446

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

Gildas Bazin's avatar
 
Gildas Bazin committed
455
    if( !( p_block = block_New( p_input, p_oggpacket->bytes ) ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
456 457 458
    {
        return;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
459

Gildas Bazin's avatar
 
Gildas Bazin committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473
    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
474

Gildas Bazin's avatar
 
Gildas Bazin committed
475 476 477 478 479
    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
480 481 482 483 484 485
    {
        /* 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
486
        p_block->i_buffer -= i_header_len;
Gildas Bazin's avatar
 
Gildas Bazin committed
487 488
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
489
    if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
Gildas Bazin's avatar
 
Gildas Bazin committed
490 491
    {
        /* FIXME: the biggest hack I've ever done */
492
        msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
Gildas Bazin's avatar
 
Gildas Bazin committed
493
                  p_block->i_pts, p_block->i_dts );
Gildas Bazin's avatar
 
Gildas Bazin committed
494
        msleep(10000);
Gildas Bazin's avatar
 
Gildas Bazin committed
495 496
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
497
    memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
Gildas Bazin's avatar
 
Gildas Bazin committed
498 499
            p_oggpacket->bytes - i_header_len );

Gildas Bazin's avatar
 
Gildas Bazin committed
500
    es_out_Send( p_input->p_es_out, p_stream->p_es, p_block );
Gildas Bazin's avatar
 
Gildas Bazin committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
}

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

Gildas Bazin's avatar
 
Gildas Bazin committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
    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
538 539 540
                es_format_Init( &p_stream->fmt, 0, 0 );
                p_stream->b_activated = VLC_TRUE;

Gildas Bazin's avatar
 
Gildas Bazin committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
                /* 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 */
559
                if( oggpacket.bytes >= 7 &&
Gildas Bazin's avatar
 
Gildas Bazin committed
560 561 562 563 564
                    ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
                {
                    oggpack_buffer opb;

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

Gildas Bazin's avatar
 
Gildas Bazin committed
568 569 570 571 572
                    /* 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
573 574
                    /* Cheat and get additionnal info ;) */
                    oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
Gildas Bazin's avatar
 
Gildas Bazin committed
575
                    oggpack_adv( &opb, 88 );
Gildas Bazin's avatar
 
Gildas Bazin committed
576 577 578
                    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
579
                    oggpack_adv( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
580
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
581
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
582
                /* Check for Speex header */
Gildas Bazin's avatar
 
Gildas Bazin committed
583
                else if( oggpacket.bytes >= 7 &&
Gildas Bazin's avatar
 
Gildas Bazin committed
584 585 586 587
                    ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
                {
                    oggpack_buffer opb;

Gildas Bazin's avatar
 
Gildas Bazin committed
588 589
                    p_stream->fmt.i_cat = AUDIO_ES;
                    p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
Gildas Bazin's avatar
 
Gildas Bazin committed
590 591 592 593 594 595 596 597 598 599 600

                    /* 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
601 602
                    p_stream->f_rate = p_stream->fmt.audio.i_rate =
                        oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
603 604
                    oggpack_adv( &opb, 32 ); /* mode */
                    oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
Gildas Bazin's avatar
 
Gildas Bazin committed
605 606 607 608 609 610 611
                    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
612
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
613 614 615 616 617 618 619 620 621 622 623
                /* 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
624 625
                    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
626
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
627 628 629 630
                /* Check for Theora header */
                else if( oggpacket.bytes >= 7 &&
                         ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
631
                    bs_t bitstream;
Gildas Bazin's avatar
 
Gildas Bazin committed
632 633 634 635 636
                    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
637 638
                    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
639

Gildas Bazin's avatar
 
Gildas Bazin committed
640 641 642 643 644
                    /* 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
645
                    /* Cheat and get additionnal info ;) */
Gildas Bazin's avatar
 
Gildas Bazin committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
                    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
666 667 668 669 670 671 672 673 674 675

                    /* 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
676
                    p_stream->f_rate = ((float)i_fps_numerator) /
Gildas Bazin's avatar
 
Gildas Bazin committed
677 678 679
                                                i_fps_denominator;
                    msg_Dbg( p_input,
                             "found theora header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
680
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
681 682 683 684 685 686 687 688
                }
                /* 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
689 690
                    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
691 692 693 694 695

                    /* 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
696
                    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
Gildas Bazin's avatar
 
Gildas Bazin committed
697 698 699
                    p_stream->f_rate = 2; /* FIXME */
                    msg_Dbg( p_input,
                             "found tarkin header, bitrate: %i, rate: %f",
Gildas Bazin's avatar
 
Gildas Bazin committed
700
                             p_stream->fmt.i_bitrate, p_stream->f_rate );
Gildas Bazin's avatar
 
Gildas Bazin committed
701
                }
Gildas Bazin's avatar
 
Gildas Bazin committed
702 703 704 705 706 707 708 709 710 711
                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
712 713
                        p_stream->fmt.i_cat = VIDEO_ES;
                        p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
714 715 716 717 718
                            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
719
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
720 721 722

                        p_stream->f_rate = 10000000.0 /
                            GetQWLE((oggpacket.packet+164));
Gildas Bazin's avatar
 
Gildas Bazin committed
723
                        p_stream->fmt.video.i_bits_per_pixel =
Gildas Bazin's avatar
 
Gildas Bazin committed
724
                            GetWLE((oggpacket.packet+182));
Gildas Bazin's avatar
 
Gildas Bazin committed
725 726 727 728
                        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
729
                            GetDWLE((oggpacket.packet+176));
Gildas Bazin's avatar
 
Gildas Bazin committed
730
                        p_stream->fmt.video.i_height =
Gildas Bazin's avatar
 
Gildas Bazin committed
731 732 733
                            GetDWLE((oggpacket.packet+180));

                        msg_Dbg( p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
734 735 736 737 738 739
                                 "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
740 741 742 743 744
                    }
                    /* 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
745
                        unsigned int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
746

Gildas Bazin's avatar
 
Gildas Bazin committed
747
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
748 749

                        i_extra_size = GetWLE((oggpacket.packet+140));
Gildas Bazin's avatar
 
Gildas Bazin committed
750
                        if( i_extra_size )
Gildas Bazin's avatar
 
Gildas Bazin committed
751
                        {
Gildas Bazin's avatar
 
Gildas Bazin committed
752 753 754 755
                            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
756 757
                        }

Gildas Bazin's avatar
 
Gildas Bazin committed
758 759
                        i_format_tag = GetWLE((oggpacket.packet+124));
                        p_stream->fmt.audio.i_channels =
Gildas Bazin's avatar
 
Gildas Bazin committed
760
                            GetWLE((oggpacket.packet+126));
Gildas Bazin's avatar
 
Gildas Bazin committed
761
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
Gildas Bazin's avatar
 
Gildas Bazin committed
762
                            GetDWLE((oggpacket.packet+128));
Gildas Bazin's avatar
 
Gildas Bazin committed
763 764 765
                        p_stream->fmt.i_bitrate =
                            GetDWLE((oggpacket.packet+132)) * 8;
                        p_stream->fmt.audio.i_blockalign =
Gildas Bazin's avatar
 
Gildas Bazin committed
766
                            GetWLE((oggpacket.packet+136));
Gildas Bazin's avatar
 
Gildas Bazin committed
767
                        p_stream->fmt.audio.i_bitspersample =
Gildas Bazin's avatar
 
Gildas Bazin committed
768 769
                            GetWLE((oggpacket.packet+138));

Gildas Bazin's avatar
 
Gildas Bazin committed
770
                        switch( i_format_tag )
Gildas Bazin's avatar
 
Gildas Bazin committed
771 772
                        {
                        case WAVE_FORMAT_PCM:
Gildas Bazin's avatar
 
Gildas Bazin committed
773
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
774 775 776 777
                                VLC_FOURCC( 'a', 'r', 'a', 'w' );
                            break;
                        case WAVE_FORMAT_MPEG:
                        case WAVE_FORMAT_MPEGLAYER3:
Gildas Bazin's avatar
 
Gildas Bazin committed
778
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
779 780 781
                                VLC_FOURCC( 'm', 'p', 'g', 'a' );
                            break;
                        case WAVE_FORMAT_A52:
Gildas Bazin's avatar
 
Gildas Bazin committed
782
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
783 784 785
                                VLC_FOURCC( 'a', '5', '2', ' ' );
                            break;
                        case WAVE_FORMAT_WMA1:
Gildas Bazin's avatar
 
Gildas Bazin committed
786
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
787 788 789
                                VLC_FOURCC( 'w', 'm', 'a', '1' );
                            break;
                        case WAVE_FORMAT_WMA2:
Gildas Bazin's avatar
 
Gildas Bazin committed
790
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
791 792 793
                                VLC_FOURCC( 'w', 'm', 'a', '2' );
                            break;
                        default:
Gildas Bazin's avatar
 
Gildas Bazin committed
794 795 796
                            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
797 798 799
                        }

                        msg_Dbg( p_input, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
800
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
801 802
                        msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
803 804 805 806 807
                                 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 );
808

Gildas Bazin's avatar
 
Gildas Bazin committed
809 810 811 812 813 814 815 816 817
                    }
                    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
818
                else if( (*oggpacket.packet & PACKET_TYPE_BITS )
Sam Hocevar's avatar
Sam Hocevar committed
819
                         == PACKET_TYPE_HEADER &&
Gildas Bazin's avatar
 
Gildas Bazin committed
820 821 822 823 824 825 826
                         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
827
                        p_stream->fmt.i_cat = VIDEO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
828 829 830 831

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

Gildas Bazin's avatar
 
Gildas Bazin committed
832 833 834
                        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
835
                        msg_Dbg( p_input, "found video header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
836
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
837

Gildas Bazin's avatar
 
Gildas Bazin committed
838
                        p_stream->f_rate = 10000000.0 /
839
                            GetQWLE(&st->time_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
840
                        p_stream->fmt.video.i_bits_per_pixel =
841
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
842
                        p_stream->fmt.video.i_width =
843
                            GetDWLE(&st->sh.video.width);
Gildas Bazin's avatar
 
Gildas Bazin committed
844
                        p_stream->fmt.video.i_height =
845
                            GetDWLE(&st->sh.video.height);
Gildas Bazin's avatar
 
Gildas Bazin committed
846 847

                        msg_Dbg( p_input,
Gildas Bazin's avatar
 
Gildas Bazin committed
848 849 850 851 852
                                 "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
853 854 855 856 857
                    }
                    /* Check for audio header (new format) */
                    else if( !strncmp( st->streamtype, "audio", 5 ) )
                    {
                        char p_buffer[5];
Gildas Bazin's avatar
 
Gildas Bazin committed
858
                        int i_format_tag;
Gildas Bazin's avatar
 
Gildas Bazin committed
859

Gildas Bazin's avatar
 
Gildas Bazin committed
860
                        p_stream->fmt.i_cat = AUDIO_ES;
Gildas Bazin's avatar
 
Gildas Bazin committed
861 862 863 864

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

Gildas Bazin's avatar
 
Gildas Bazin committed
865 866
                        memcpy( p_buffer, st->subtype, 4 );
                        p_buffer[4] = '\0';
Gildas Bazin's avatar
 
Gildas Bazin committed
867 868
                        i_format_tag = strtol(p_buffer,NULL,16);
                        p_stream->fmt.audio.i_channels =
869
                            GetWLE(&st->sh.audio.channels);
Gildas Bazin's avatar
 
Gildas Bazin committed
870
                        p_stream->f_rate = p_stream->fmt.audio.i_rate =
871
                            GetQWLE(&st->samples_per_unit);
Gildas Bazin's avatar
 
Gildas Bazin committed
872 873 874
                        p_stream->fmt.i_bitrate =
                            GetDWLE(&st->sh.audio.avgbytespersec) * 8;
                        p_stream->fmt.audio.i_blockalign =
875
                            GetWLE(&st->sh.audio.blockalign);
Gildas Bazin's avatar
 
Gildas Bazin committed
876
                        p_stream->fmt.audio.i_bitspersample =
877
                            GetWLE(&st->bits_per_sample);
Gildas Bazin's avatar
 
Gildas Bazin committed
878

Gildas Bazin's avatar
 
Gildas Bazin committed
879
                        switch( i_format_tag )
Gildas Bazin's avatar
 
Gildas Bazin committed
880 881
                        {
                        case WAVE_FORMAT_PCM:
Gildas Bazin's avatar
 
Gildas Bazin committed
882
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
883 884 885 886
                                VLC_FOURCC( 'a', 'r', 'a', 'w' );
                            break;
                        case WAVE_FORMAT_MPEG:
                        case WAVE_FORMAT_MPEGLAYER3:
Gildas Bazin's avatar
 
Gildas Bazin committed
887
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
888 889 890
                                VLC_FOURCC( 'm', 'p', 'g', 'a' );
                            break;
                        case WAVE_FORMAT_A52:
Gildas Bazin's avatar
 
Gildas Bazin committed
891
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
892 893 894
                                VLC_FOURCC( 'a', '5', '2', ' ' );
                            break;
                        case WAVE_FORMAT_WMA1:
Gildas Bazin's avatar
 
Gildas Bazin committed
895
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
896 897 898
                                VLC_FOURCC( 'w', 'm', 'a', '1' );
                            break;
                        case WAVE_FORMAT_WMA2:
Gildas Bazin's avatar
 
Gildas Bazin committed
899
                            p_stream->fmt.i_codec =
Gildas Bazin's avatar
 
Gildas Bazin committed
900 901 902
                                VLC_FOURCC( 'w', 'm', 'a', '2' );
                            break;
                        default:
Gildas Bazin's avatar
 
Gildas Bazin committed
903 904 905
                            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
906 907 908
                        }

                        msg_Dbg( p_input, "found audio header of type: %.4s",
Gildas Bazin's avatar
 
Gildas Bazin committed
909
                                 (char *)&p_stream->fmt.i_codec );
Gildas Bazin's avatar
 
Gildas Bazin committed
910 911
                        msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
                                 "%dbits/sample %dkb/s",
Gildas Bazin's avatar
 
Gildas Bazin committed
912 913 914 915 916
                                 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
917
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
918 919 920
                    /* Check for text (subtitles) header */
                    else if( !strncmp(st->streamtype, "text", 4) )
                    {
Gildas Bazin's avatar
 
Gildas Bazin committed
921 922 923
                        /* We need to get rid of the header packet */
                        ogg_stream_packetout( &p_stream->os, &oggpacket );

Gildas Bazin's avatar
 
Gildas Bazin committed
924
                        msg_Dbg( p_input, "found text subtitles header" );
Gildas Bazin's avatar
 
Gildas Bazin committed
925 926
                        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
927
                        p_stream->f_rate = 1000; /* granulepos is in milisec */
Gildas Bazin's avatar
 
Gildas Bazin committed
928
                    }
Gildas Bazin's avatar
 
Gildas Bazin committed
929 930 931 932 933 934 935 936
                    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
937 938
                else
                {
Gildas Bazin's avatar
 
Gildas Bazin committed
939 940
                    msg_Dbg( p_input, "stream %d is of unknown type",
                             p_ogg->i_streams-1 );
Gildas Bazin's avatar
 
Gildas Bazin committed
941 942
                    free( p_stream );
                    p_ogg->i_streams--;
Gildas Bazin's avatar
 
Gildas Bazin committed
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
                }

                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
960

Gildas Bazin's avatar
 
Gildas Bazin committed
961 962 963
            return VLC_SUCCESS;
        }
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
964 965
#undef p_stream

Gildas Bazin's avatar
 
Gildas Bazin committed
966 967 968 969 970 971 972 973 974
    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
975 976
    demux_sys_t    *p_ogg;
    int            b_forced;
Gildas Bazin's avatar
 
Gildas Bazin committed
977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993

    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
994
    p_ogg->pp_stream = NULL;
Gildas Bazin's avatar
 
Gildas Bazin committed
995 996 997 998

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

Gildas Bazin's avatar
 
Gildas Bazin committed
999
    /* Set exported functions */
Gildas Bazin's avatar
 
Gildas Bazin committed
1000
    p_input->pf_demux = Demux;
Gildas Bazin's avatar
 
Gildas Bazin committed
1001
    p_input->pf_demux_control = Control;
Gildas Bazin's avatar
 
Gildas Bazin committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016

    /* 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
1017
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
1018 1019 1020 1021 1022 1023 1024
    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
1025 1026 1027
    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
1028 1029
    p_ogg->i_eos = 0;

Gildas Bazin's avatar
 
Gildas Bazin committed
1030 1031 1032 1033 1034 1035 1036 1037 1038
    return 0;

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

}

/****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
1039 1040
 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
 *                        Elementary streams.
Gildas Bazin's avatar
 
Gildas Bazin committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
 ****************************************************************************/
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
1055 1056
    p_input->stream.i_mux_rate = 0;
    vlc_mutex_unlock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1057 1058 1059 1060

    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
1061 1062 1063
        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
1064
        vlc_mutex_lock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1065
        p_input->stream.i_mux_rate += (p_stream->fmt.i_bitrate / ( 8 * 50 ));
Gildas Bazin's avatar
 
Gildas Bazin committed
1066
        vlc_mutex_unlock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1067 1068

        p_stream->i_pcr  = p_stream->i_previous_pcr = -1;
Gildas Bazin's avatar
 
Gildas Bazin committed
1069
        p_stream->b_reinit = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
1070 1071 1072
#undef p_stream
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
1073 1074
    return VLC_SUCCESS;
}
Gildas Bazin's avatar
 
Gildas Bazin committed
1075

Gildas Bazin's avatar
 
Gildas Bazin committed
1076 1077 1078 1079 1080 1081
/****************************************************************************
 * 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
1082

Gildas Bazin's avatar
 
Gildas Bazin committed
1083 1084 1085
#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
1086 1087
        es_out_Del( p_input->p_es_out, p_stream->p_es );

Gildas Bazin's avatar
 
Gildas Bazin committed
1088
        vlc_mutex_lock( &p_input->stream.stream_lock );
Gildas Bazin's avatar
 
Gildas Bazin committed
1089
        p_input->stream.i_mux_rate -= (p_stream->fmt.i_bitrate / ( 8 * 50 ));
Gildas Bazin's avatar
 
Gildas Bazin committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
        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
1102
#undef p_stream
Gildas Bazin's avatar
 
Gildas Bazin committed
1103 1104 1105 1106 1107

    /* 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
1108 1109 1110 1111 1112 1113 1114 1115
}

/*****************************************************************************
 * 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
1116
    demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data  ;
Gildas Bazin's avatar
 
Gildas Bazin committed
1117 1118 1119 1120 1121 1122

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

Gildas Bazin's avatar
 
Gildas Bazin committed
1123
        Ogg_EndOfStream( p_input, p_ogg );
Gildas Bazin's avatar
 
Gildas Bazin committed
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

        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
1136
    demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
1137 1138
    ogg_page    oggpage;
    ogg_packet  oggpacket;
Gildas Bazin's avatar
 
Gildas Bazin committed
1139 1140
    int         i_stream;

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

Gildas Bazin's avatar
 
Gildas Bazin committed