]> git.sesse.net Git - vlc/blob - modules/demux/ogg.c
* fixed several format string inconsistencies and deprecated C constructions.
[vlc] / modules / demux / ogg.c
1 /*****************************************************************************
2  * ogg.c : ogg stream input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: ogg.c,v 1.15 2002/12/18 14:17:10 sam Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/input.h>
32
33 #include <sys/types.h>
34
35 #include <ogg/ogg.h>
36
37 #include <codecs.h>                        /* BITMAPINFOHEADER, WAVEFORMATEX */
38
39 #define OGG_BLOCK_SIZE 4096
40 #define PAGES_READ_ONCE 1
41
42 /*****************************************************************************
43  * Definitions of structures and functions used by this plugins 
44  *****************************************************************************/
45 typedef struct logical_stream_s
46 {
47     ogg_stream_state os;                        /* logical stream of packets */
48
49     int              i_serial_no;
50     int              i_cat;                            /* AUDIO_ES, VIDEO_ES */
51     int              i_activated;
52     vlc_fourcc_t     i_fourcc;
53     vlc_fourcc_t     i_codec;
54
55     es_descriptor_t  *p_es;
56     int              b_selected;                           /* newly selected */
57
58     /* the header of some logical streams (eg vorbis) contain essential
59      * data for the decoder. We back them up here in case we need to re-feed
60      * them to the decoder. */
61     int              b_force_backup;
62     int              i_packets_backup;
63     ogg_packet       *p_packets_backup;
64
65     /* program clock reference (in units of 90kHz) derived from the previous
66      * granulepos */
67     mtime_t          i_pcr;
68     mtime_t          i_interpolated_pcr;
69
70     /* info from logical streams */
71     double f_rate;
72     int i_bitrate;
73     int b_reinit;
74
75     /* codec specific stuff */
76     BITMAPINFOHEADER *p_bih;
77     WAVEFORMATEX *p_wf;
78     int i_theora_keyframe_granule_shift;
79
80 } logical_stream_t;
81
82 struct demux_sys_t
83 {
84     ogg_sync_state oy;        /* sync and verify incoming physical bitstream */
85
86     int i_streams;                           /* number of logical bitstreams */
87     logical_stream_t **pp_stream;  /* pointer to an array of logical streams */
88
89     /* current audio and video es */
90     logical_stream_t *p_stream_video;
91     logical_stream_t *p_stream_audio;
92     logical_stream_t *p_stream_spu;
93
94     /* program clock reference (in units of 90kHz) derived from the pcr of
95      * the sub-streams */
96     mtime_t i_pcr;
97
98     mtime_t i_length;
99     int     b_seekable;
100     int     b_reinit;
101 };
102
103 /* OggDS headers for the new header format (used in ogm files) */
104 typedef struct stream_header_video
105 {
106     ogg_int32_t width;
107     ogg_int32_t height;
108 } stream_header_video;
109         
110 typedef struct stream_header_audio
111 {
112     ogg_int16_t channels;
113     ogg_int16_t blockalign;
114     ogg_int32_t avgbytespersec;
115 } stream_header_audio;
116
117 typedef struct stream_header
118 {
119     char        streamtype[8];
120     char        subtype[4];
121
122     ogg_int32_t size;                               /* size of the structure */
123
124     ogg_int64_t time_unit;                              /* in reference time */
125     ogg_int64_t samples_per_unit;
126     ogg_int32_t default_len;                                /* in media time */
127
128     ogg_int32_t buffersize;
129     ogg_int16_t bits_per_sample;
130
131     union
132     {
133         /* Video specific */
134         stream_header_video video;
135         /* Audio specific */
136         stream_header_audio audio;
137     } sh;
138 } stream_header;
139
140 /* Some defines from OggDS */
141 #define PACKET_TYPE_HEADER   0x01
142 #define PACKET_TYPE_BITS     0x07
143 #define PACKET_LEN_BITS01    0xc0
144 #define PACKET_LEN_BITS2     0x02
145 #define PACKET_IS_SYNCPOINT  0x08
146
147 /* Some functions to manipulate memory */
148 static uint16_t GetWLE( uint8_t *p_buff )
149 {
150     return( (p_buff[0]) + ( p_buff[1] <<8 ) );
151 }
152
153 static uint32_t GetDWLE( uint8_t *p_buff )
154 {
155     return( p_buff[0] + ( p_buff[1] <<8 ) +
156             ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
157 }
158
159 static uint64_t GetQWLE( uint8_t *p_buff )
160 {
161     return( GetDWLE( p_buff ) + ( ((uint64_t)GetDWLE( p_buff + 4 )) << 32 ) );
162 }
163 /*****************************************************************************
164  * Local prototypes
165  *****************************************************************************/
166 static int  Activate  ( vlc_object_t * );
167 static void Deactivate( vlc_object_t * );
168 static int  Demux     ( input_thread_t * );
169
170 /* Stream managment */
171 static int  Ogg_StreamStart  ( input_thread_t *, demux_sys_t *, int );
172 static void Ogg_StreamStop   ( input_thread_t *, demux_sys_t *, int );
173
174 /* Bitstream manipulation */
175 static int  Ogg_Check        ( input_thread_t *p_input );
176 static int  Ogg_ReadPage     ( input_thread_t *, demux_sys_t *, ogg_page * );
177 static void Ogg_UpdatePCR    ( logical_stream_t *, ogg_packet * );
178 static void Ogg_DecodePacket ( input_thread_t *p_input,
179                                logical_stream_t *p_stream, ogg_packet * );
180 static int  Ogg_FindLogicalStreams( input_thread_t *p_input,
181                                     demux_sys_t *p_ogg );
182
183 /*****************************************************************************
184  * Module descriptor
185  *****************************************************************************/
186 vlc_module_begin();
187     set_description( _("ogg stream demux" ) );
188     set_capability( "demux", 50 );
189     set_callbacks( Activate, Deactivate );
190     add_shortcut( "ogg" );
191 vlc_module_end();
192
193 /*****************************************************************************
194  * Stream managment
195  *****************************************************************************/
196 static int Ogg_StreamStart( input_thread_t *p_input,
197                             demux_sys_t *p_ogg, int i_stream )
198 {
199 #define p_stream p_ogg->pp_stream[i_stream]
200     if( !p_stream->p_es )
201     {
202         msg_Warn( p_input, "stream[%d] unselectable", i_stream );
203         return( 0 );
204     }
205     if( p_stream->i_activated )
206     {
207         msg_Warn( p_input, "stream[%d] already selected", i_stream );
208         return( 1 );
209     }
210
211     if( !p_stream->p_es->p_decoder_fifo )
212     {
213         vlc_mutex_lock( &p_input->stream.stream_lock );
214         input_SelectES( p_input, p_stream->p_es );
215         vlc_mutex_unlock( &p_input->stream.stream_lock );
216     }
217     p_stream->i_activated = p_stream->p_es->p_decoder_fifo ? 1 : 0;
218
219     /* Feed the backup header to the decoder */
220     if( !p_stream->b_force_backup )
221     {
222         int i;
223         for( i = 0; i < p_stream->i_packets_backup; i++ )
224         {
225             Ogg_DecodePacket( p_input, p_stream,
226                               &p_stream->p_packets_backup[i] );
227         }
228     }
229
230     return( p_stream->i_activated );
231 #undef  p_stream
232 }
233
234 static void Ogg_StreamStop( input_thread_t *p_input,
235                             demux_sys_t *p_ogg, int i_stream )
236 {
237 #define p_stream    p_ogg->pp_stream[i_stream]
238
239     if( !p_stream->i_activated )
240     {
241         msg_Warn( p_input, "stream[%d] already unselected", i_stream );
242         return;
243     }
244
245     if( p_stream->p_es->p_decoder_fifo )
246     {
247         vlc_mutex_lock( &p_input->stream.stream_lock );
248         input_UnselectES( p_input, p_stream->p_es );
249         vlc_mutex_unlock( &p_input->stream.stream_lock );
250     }
251
252     p_stream->i_activated = 0;
253
254 #undef  p_stream
255 }
256
257 /****************************************************************************
258  * Ogg_Check: Check we are dealing with an ogg stream.
259  ****************************************************************************/
260 static int Ogg_Check( input_thread_t *p_input )
261 {
262     u8 *p_peek;
263     int i_size = input_Peek( p_input, &p_peek, 4 );
264
265     /* Check for the Ogg capture pattern */
266     if( !(i_size>3) || !(p_peek[0] == 'O') || !(p_peek[1] == 'g') ||
267         !(p_peek[2] == 'g') || !(p_peek[3] == 'S') )
268         return VLC_EGENERIC;
269
270     /* FIXME: Capture pattern might not be enough so we can also check for the
271      * the first complete page */
272
273     return VLC_SUCCESS;
274 }
275
276 /****************************************************************************
277  * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
278  ****************************************************************************
279  * Returns VLC_SUCCESS if a page has been read. An error might happen if we
280  * are at the end of stream.
281  ****************************************************************************/
282 static int Ogg_ReadPage( input_thread_t *p_input, demux_sys_t *p_ogg,
283                          ogg_page *p_oggpage )
284 {
285     int i_read = 0;
286     data_packet_t *p_data;
287     byte_t *p_buffer;
288
289     while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
290     {
291         i_read = input_SplitBuffer( p_input, &p_data, OGG_BLOCK_SIZE );
292         if( i_read <= 0 )
293             return VLC_EGENERIC;
294
295         p_buffer = ogg_sync_buffer( &p_ogg->oy, i_read );
296         p_input->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, i_read );
297         ogg_sync_wrote( &p_ogg->oy, i_read );
298         input_DeletePacket( p_input->p_method_data, p_data );
299     }
300
301     return VLC_SUCCESS;
302 }
303
304 /****************************************************************************
305  * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
306  *                current stream.
307  ****************************************************************************/
308 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
309                            ogg_packet *p_oggpacket )
310 {
311
312     /* Convert the next granulepos into a pcr */
313     if( p_oggpacket->granulepos >= 0 )
314     {
315         if( p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
316         {
317             p_stream->i_pcr = p_oggpacket->granulepos * 90000
318                               / p_stream->f_rate;
319         }
320         else
321         {
322             ogg_int64_t iframe = p_oggpacket->granulepos >>
323               p_stream->i_theora_keyframe_granule_shift;
324             ogg_int64_t pframe = p_oggpacket->granulepos -
325               ( iframe << p_stream->i_theora_keyframe_granule_shift );
326
327             p_stream->i_pcr = ( iframe + pframe ) * 90000
328                               / p_stream->f_rate;
329         }
330
331         p_stream->i_interpolated_pcr = p_stream->i_pcr;
332     }
333     else
334     {
335         /* FIXME: ffmpeg doesn't like null pts */
336         if( p_stream->i_cat == VIDEO_ES )
337             /* 1 frame per packet */
338             p_stream->i_pcr += (90000 / p_stream->f_rate);
339         else
340             p_stream->i_pcr = -1;
341
342         /* no granulepos available, try to interpolate the pcr.
343          * If we can't then don't touch the old value. */
344         if( p_stream->i_bitrate )
345             p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
346                                               / p_stream->i_bitrate / 8 );
347     }
348 }
349
350 /****************************************************************************
351  * Ogg_DecodePacket: Decode an Ogg packet.
352  ****************************************************************************/
353 static void Ogg_DecodePacket( input_thread_t *p_input,
354                               logical_stream_t *p_stream,
355                               ogg_packet *p_oggpacket )
356 {
357     pes_packet_t  *p_pes;
358     data_packet_t *p_data;
359     vlc_bool_t b_trash = VLC_FALSE;
360     int i_header_len = 0;
361
362     if( p_stream->b_force_backup )
363     {
364         /* Backup the ogg packet (likely an header packet) */
365         ogg_packet *p_packet_backup;
366         p_stream->i_packets_backup++;
367         p_stream->p_packets_backup =
368             realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
369                      sizeof(ogg_packet) );
370
371         p_packet_backup =
372             &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
373
374         p_packet_backup->bytes = p_oggpacket->bytes;
375         p_packet_backup->granulepos = p_oggpacket->granulepos;
376         p_packet_backup->packet = malloc( p_oggpacket->bytes );
377         if( !p_packet_backup->packet ) return;
378         memcpy( p_packet_backup->packet, p_oggpacket->packet,
379                 p_oggpacket->bytes );
380
381         switch( p_stream->i_fourcc )
382         {
383         case VLC_FOURCC( 'v','o','r','b' ):
384           if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
385           break;
386
387         default:
388           p_stream->b_force_backup = 0;
389           break;
390         }
391     }
392
393     vlc_mutex_lock( &p_input->stream.control.control_lock );
394     if( p_stream->i_cat == AUDIO_ES && p_input->stream.control.b_mute )
395     {
396         b_trash = VLC_TRUE;
397     }
398     vlc_mutex_unlock( &p_input->stream.control.control_lock );
399
400     if( !p_stream->p_es->p_decoder_fifo || b_trash )
401     {
402         /* This stream isn't currently selected so we don't need to decode it,
403          * but we do need to store its pcr as it might be selected later on. */
404         Ogg_UpdatePCR( p_stream, p_oggpacket );
405
406         return;
407     }
408
409     if( !( p_pes = input_NewPES( p_input->p_method_data ) ) )
410     {
411         return;
412     }
413     if( !( p_data = input_NewPacket( p_input->p_method_data,
414                                      p_oggpacket->bytes ) ) )
415     {
416         input_DeletePES( p_input->p_method_data, p_pes );
417         return;
418     }
419
420     /* Convert the pcr into a pts */
421     if( p_stream->i_cat != SPU_ES )
422     {
423         p_pes->i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
424             input_ClockGetTS( p_input, p_input->stream.p_selected_program,
425                               p_stream->i_pcr );
426     }
427     else
428     {
429         /* Of course subtitles had to be different! */
430         p_pes->i_pts = ( p_oggpacket->granulepos < 0 ) ? 0 :
431             input_ClockGetTS( p_input, p_input->stream.p_selected_program,
432                               p_oggpacket->granulepos * 90000 /
433                               p_stream->f_rate );
434     }
435
436     /* Convert the next granulepos into a pcr */
437     Ogg_UpdatePCR( p_stream, p_oggpacket );
438
439     p_pes->i_nb_data = 1;
440     p_pes->i_dts = p_oggpacket->granulepos;
441     p_pes->p_first = p_pes->p_last = p_data;
442     p_pes->i_pes_size = p_oggpacket->bytes;
443
444     if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) &&
445         p_stream->i_fourcc != VLC_FOURCC( 't','a','r','k' ) &&
446         p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
447     {
448         /* Remove the header from the packet */
449         i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
450         i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
451         i_header_len++;
452
453         p_pes->i_pes_size -= i_header_len;
454         p_pes->i_dts = 0;
455     }
456
457     if( p_stream->i_fourcc == VLC_FOURCC( 't','a','r','k' ) )
458     {
459         /* FIXME: the biggest hack I've ever done */
460         msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
461                   p_pes->i_pts, p_pes->i_dts );
462         msleep(10000);
463     }
464
465     memcpy( p_data->p_payload_start,
466             p_oggpacket->packet + i_header_len,
467             p_oggpacket->bytes - i_header_len );
468
469     p_data->p_payload_end = p_data->p_payload_start + p_pes->i_pes_size;
470     p_data->b_discard_payload = 0;
471
472     input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
473 }
474
475 /****************************************************************************
476  * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
477  *                         stream and fill p_ogg.
478  *****************************************************************************
479  * The initial page of a logical stream is marked as a 'bos' page.
480  * Furthermore, the Ogg specification mandates that grouped bitstreams begin
481  * together and all of the initial pages must appear before any data pages.
482  *
483  * On success this function returns VLC_SUCCESS.
484  ****************************************************************************/
485 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
486 {
487     ogg_packet oggpacket;
488     ogg_page oggpage;
489     int i_stream;
490
491     while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
492     {
493         if( ogg_page_bos( &oggpage ) )
494         {
495
496             /* All is wonderful in our fine fine little world.
497              * We found the beginning of our first logical stream. */
498             while( ogg_page_bos( &oggpage ) )
499             {
500                 p_ogg->i_streams++;
501                 p_ogg->pp_stream =
502                     realloc( p_ogg->pp_stream, p_ogg->i_streams *
503                              sizeof(logical_stream_t *) );
504
505 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
506
507                 p_stream = malloc( sizeof(logical_stream_t) );
508                 memset( p_stream, 0, sizeof(logical_stream_t) );
509
510                 /* Setup the logical stream */
511                 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
512                 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
513
514                 /* Extract the initial header from the first page and verify
515                  * the codec type of tis Ogg bitstream */
516                 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
517                 {
518                     /* error. stream version mismatch perhaps */
519                     msg_Err( p_input, "Error reading first page of "
520                              "Ogg bitstream data" );
521                     return VLC_EGENERIC;
522                 }
523
524                 /* FIXME: check return value */
525                 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
526
527                 /* Check for Vorbis header */
528                 if( oggpacket.bytes >= 7 &&
529                     ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
530                 {
531                     oggpack_buffer opb;
532
533                     msg_Dbg( p_input, "found vorbis header" );
534                     p_stream->i_cat = AUDIO_ES;
535                     p_stream->i_fourcc = VLC_FOURCC( 'v','o','r','b' );
536
537                     /* Signal that we want to keep a backup of the vorbis
538                      * stream headers. They will be used when switching between
539                      * audio streams. */
540                     p_stream->b_force_backup = 1;
541
542                     /* Cheat and get additionnal info ;) */
543                     oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
544                     oggpack_adv( &opb, 96 );
545                     p_stream->f_rate = oggpack_read( &opb, 32 );
546                     oggpack_adv( &opb, 32 );
547                     p_stream->i_bitrate = oggpack_read( &opb, 32 );
548                 }
549                 /* Check for Theora header */
550                 else if( oggpacket.bytes >= 7 &&
551                          ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
552                 {
553 #ifdef HAVE_OGGPACKB
554                     oggpack_buffer opb;
555                     int i_fps_numerator;
556                     int i_fps_denominator;
557                     int i_keyframe_frequency_force;
558 #endif
559
560                     msg_Dbg( p_input, "found theora header" );
561 #ifdef HAVE_OGGPACKB
562                     p_stream->i_cat = VIDEO_ES;
563                     p_stream->i_fourcc = VLC_FOURCC( 't','h','e','o' );
564
565                     /* Cheat and get additionnal info ;) */
566                     oggpackB_readinit(&opb, oggpacket.packet, oggpacket.bytes);
567                     oggpackB_adv( &opb, 56 );
568                     oggpackB_read( &opb, 8 ); /* major version num */
569                     oggpackB_read( &opb, 8 ); /* minor version num */
570                     oggpackB_read( &opb, 8 ); /* subminor version num */
571                     oggpackB_read( &opb, 16 ) /*<< 4*/; /* width */
572                     oggpackB_read( &opb, 16 ) /*<< 4*/; /* height */
573                     i_fps_numerator = oggpackB_read( &opb, 32 );
574                     i_fps_denominator = oggpackB_read( &opb, 32 );
575                     oggpackB_read( &opb, 24 ); /* aspect_numerator */
576                     oggpackB_read( &opb, 24 ); /* aspect_denominator */
577                     i_keyframe_frequency_force = 1 << oggpackB_read( &opb, 5 );
578                     p_stream->i_bitrate = oggpackB_read( &opb, 24 );
579                     oggpackB_read(&opb,6); /* quality */
580
581                     /* granule_shift = i_log( frequency_force -1 ) */
582                     p_stream->i_theora_keyframe_granule_shift = 0;
583                     i_keyframe_frequency_force--;
584                     while( i_keyframe_frequency_force )
585                     {
586                         p_stream->i_theora_keyframe_granule_shift++;
587                         i_keyframe_frequency_force >>= 1;
588                     }
589
590                     p_stream->f_rate = (float)i_fps_numerator /
591                                                 i_fps_denominator;
592                     msg_Dbg( p_input,
593                              "found theora header, bitrate: %i, rate: %f",
594                              p_stream->i_bitrate, p_stream->f_rate );
595 #else /* HAVE_OGGPACKB */
596                     msg_Dbg( p_input, "the ogg demuxer has been compiled "
597                              "without support for the oggpackB extension."
598                              "The theora stream won't be decoded." );
599                     free( p_stream );
600                     p_ogg->i_streams--;
601                     continue;
602 #endif /* HAVE_OGGPACKB */
603                 }
604                 /* Check for Tarkin header */
605                 else if( oggpacket.bytes >= 7 &&
606                          ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
607                 {
608                     oggpack_buffer opb;
609
610                     msg_Dbg( p_input, "found tarkin header" );
611                     p_stream->i_cat = VIDEO_ES;
612                     p_stream->i_fourcc = VLC_FOURCC( 't','a','r','k' );
613
614                     /* Cheat and get additionnal info ;) */
615                     oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
616                     oggpack_adv( &opb, 88 );
617                     oggpack_adv( &opb, 104 );
618                     p_stream->i_bitrate = oggpack_read( &opb, 32 );
619                     p_stream->f_rate = 2; /* FIXME */
620                     msg_Dbg( p_input,
621                              "found tarkin header, bitrate: %i, rate: %f",
622                              p_stream->i_bitrate, p_stream->f_rate );
623                 }
624                 else if( oggpacket.bytes >= 142 &&
625                          !strncmp( &oggpacket.packet[1],
626                                    "Direct Show Samples embedded in Ogg", 35 ))
627                 {
628                     /* Old header type */
629
630                     /* Check for video header (old format) */
631                     if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
632                         oggpacket.bytes >= 184 )
633                     {
634                         p_stream->i_cat = VIDEO_ES;
635
636                         p_stream->p_bih = (BITMAPINFOHEADER *)
637                             malloc( sizeof(BITMAPINFOHEADER) );
638                         if( !p_stream->p_bih )
639                         {
640                             /* Mem allocation error, just ignore the stream */
641                             free( p_stream );
642                             p_ogg->i_streams--;
643                             continue;
644                         }
645                         p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
646                         p_stream->p_bih->biCompression= p_stream->i_fourcc =
647                             VLC_FOURCC( oggpacket.packet[68],
648                                         oggpacket.packet[69],
649                                         oggpacket.packet[70],
650                                         oggpacket.packet[71] );
651                         msg_Dbg( p_input, "found video header of type: %.4s",
652                                  (char *)&p_stream->i_fourcc );
653
654                         p_stream->f_rate = 10000000.0 /
655                             GetQWLE((oggpacket.packet+164));
656                         p_stream->p_bih->biBitCount =
657                             GetWLE((oggpacket.packet+182));
658                         if( !p_stream->p_bih->biBitCount )
659                             p_stream->p_bih->biBitCount=24; // hack, FIXME
660                         p_stream->p_bih->biWidth =
661                             GetDWLE((oggpacket.packet+176));
662                         p_stream->p_bih->biHeight =
663                             GetDWLE((oggpacket.packet+180));
664                         p_stream->p_bih->biPlanes= 1 ;
665                         p_stream->p_bih->biSizeImage =
666                             (p_stream->p_bih->biBitCount >> 3) *
667                             p_stream->p_bih->biWidth *
668                             p_stream->p_bih->biHeight;
669
670                         msg_Dbg( p_input,
671                              "fps: %f, width:%i; height:%i, bitcount:%i",
672                             p_stream->f_rate, p_stream->p_bih->biWidth,
673                             p_stream->p_bih->biHeight,
674                             p_stream->p_bih->biBitCount);
675
676                         p_stream->i_bitrate = 0;
677                     }
678                     /* Check for audio header (old format) */
679                     else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
680                     {
681                         unsigned int i_extra_size;
682
683                         p_stream->i_cat = AUDIO_ES;
684
685                         i_extra_size = GetWLE((oggpacket.packet+140));
686
687                         p_stream->p_wf = (WAVEFORMATEX *)
688                             malloc( sizeof(WAVEFORMATEX) + i_extra_size );
689                         if( !p_stream->p_wf )
690                         {
691                             /* Mem allocation error, just ignore the stream */
692                             free( p_stream );
693                             p_ogg->i_streams--;
694                             continue;
695                         }
696
697                         p_stream->p_wf->wFormatTag =
698                             GetWLE((oggpacket.packet+124));
699                         p_stream->p_wf->nChannels =
700                             GetWLE((oggpacket.packet+126));
701                         p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
702                             GetDWLE((oggpacket.packet+128));
703                         p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
704                             GetDWLE((oggpacket.packet+132));
705                         p_stream->i_bitrate *= 8;
706                         p_stream->p_wf->nBlockAlign =
707                             GetWLE((oggpacket.packet+136));
708                         p_stream->p_wf->wBitsPerSample =
709                             GetWLE((oggpacket.packet+138));
710                         p_stream->p_wf->cbSize = i_extra_size;
711
712                         if( i_extra_size > 0 )
713                             memcpy( p_stream->p_wf+sizeof(WAVEFORMATEX),
714                                     oggpacket.packet+142, i_extra_size );
715
716                         switch( p_stream->p_wf->wFormatTag )
717                         {
718                         case WAVE_FORMAT_PCM:
719                             p_stream->i_fourcc =
720                                 VLC_FOURCC( 'a', 'r', 'a', 'w' );
721                             break;
722                         case WAVE_FORMAT_MPEG:
723                         case WAVE_FORMAT_MPEGLAYER3:
724                             p_stream->i_fourcc =
725                                 VLC_FOURCC( 'm', 'p', 'g', 'a' );
726                             break;
727                         case WAVE_FORMAT_A52:
728                             p_stream->i_fourcc =
729                                 VLC_FOURCC( 'a', '5', '2', ' ' );
730                             break;
731                         case WAVE_FORMAT_WMA1:
732                             p_stream->i_fourcc =
733                                 VLC_FOURCC( 'w', 'm', 'a', '1' );
734                             break;
735                         case WAVE_FORMAT_WMA2:
736                             p_stream->i_fourcc =
737                                 VLC_FOURCC( 'w', 'm', 'a', '2' );
738                             break;
739                         default:
740                             p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
741                                 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
742                                 p_stream->p_wf->wFormatTag & 0xff );
743                         }
744
745                         msg_Dbg( p_input, "found audio header of type: %.4s",
746                                  (char *)&p_stream->i_fourcc );
747                         msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
748                                  "%dbits/sample %dkb/s",
749                                  p_stream->p_wf->wFormatTag,
750                                  p_stream->p_wf->nChannels,
751                                  p_stream->p_wf->nSamplesPerSec,
752                                  p_stream->p_wf->wBitsPerSample,
753                                  p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
754                     }
755                     else
756                     {
757                         msg_Dbg( p_input, "stream %d has an old header "
758                             "but is of an unknown type", p_ogg->i_streams-1 );
759                         free( p_stream );
760                         p_ogg->i_streams--;
761                     }
762                 }
763                 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
764                          == PACKET_TYPE_HEADER && 
765                          oggpacket.bytes >= (int)sizeof(stream_header)+1 )
766                 {
767                     stream_header *st = (stream_header *)(oggpacket.packet+1);
768
769                     /* Check for video header (new format) */
770                     if( !strncmp( st->streamtype, "video", 5 ) )
771                     {
772                         p_stream->i_cat = VIDEO_ES;
773
774                         /* We need to get rid of the header packet */
775                         ogg_stream_packetout( &p_stream->os, &oggpacket );
776
777                         p_stream->p_bih = (BITMAPINFOHEADER *)
778                             malloc( sizeof(BITMAPINFOHEADER) );
779                         if( !p_stream->p_bih )
780                         {
781                             /* Mem allocation error, just ignore the stream */
782                             free( p_stream );
783                             p_ogg->i_streams--;
784                             continue;
785                         }
786                         p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
787                         p_stream->p_bih->biCompression=
788                             p_stream->i_fourcc = VLC_FOURCC( st->subtype[0],
789                                                              st->subtype[1],
790                                                              st->subtype[2],
791                                                              st->subtype[3] );
792                         msg_Dbg( p_input, "found video header of type: %.4s",
793                                  (char *)&p_stream->i_fourcc );
794
795                         p_stream->f_rate = 10000000.0 /
796                             GetQWLE((uint8_t *)&st->time_unit);
797                         p_stream->p_bih->biBitCount =
798                             GetWLE((uint8_t *)&st->bits_per_sample);
799                         p_stream->p_bih->biWidth =
800                             GetDWLE((uint8_t *)&st->sh.video.width);
801                         p_stream->p_bih->biHeight =
802                             GetDWLE((uint8_t *)&st->sh.video.height);
803                         p_stream->p_bih->biPlanes= 1 ;
804                         p_stream->p_bih->biSizeImage =
805                             (p_stream->p_bih->biBitCount >> 3) *
806                             p_stream->p_bih->biWidth *
807                             p_stream->p_bih->biHeight;
808
809                         msg_Dbg( p_input,
810                              "fps: %f, width:%i; height:%i, bitcount:%i",
811                             p_stream->f_rate, p_stream->p_bih->biWidth,
812                             p_stream->p_bih->biHeight,
813                             p_stream->p_bih->biBitCount);
814
815                         p_stream->i_bitrate = 0;
816                     }
817                     /* Check for audio header (new format) */
818                     else if( !strncmp( st->streamtype, "audio", 5 ) )
819                     {
820                         char p_buffer[5];
821
822                         p_stream->i_cat = AUDIO_ES;
823
824                         /* We need to get rid of the header packet */
825                         ogg_stream_packetout( &p_stream->os, &oggpacket );
826
827                         p_stream->p_wf = (WAVEFORMATEX *)
828                             malloc( sizeof(WAVEFORMATEX) );
829                         if( !p_stream->p_wf )
830                         {
831                             /* Mem allocation error, just ignore the stream */
832                             free( p_stream );
833                             p_ogg->i_streams--;
834                             continue;
835                         }
836
837                         memcpy( p_buffer, st->subtype, 4 );
838                         p_buffer[4] = '\0';
839                         p_stream->p_wf->wFormatTag = strtol(p_buffer,NULL,16);
840                         p_stream->p_wf->nChannels =
841                             GetWLE((uint8_t *)&st->sh.audio.channels);
842                         p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
843                             GetQWLE((uint8_t *)&st->samples_per_unit);
844                         p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
845                             GetDWLE((uint8_t *)&st->sh.audio.avgbytespersec);
846                         p_stream->i_bitrate *= 8;
847                         p_stream->p_wf->nBlockAlign =
848                             GetWLE((uint8_t *)&st->sh.audio.blockalign);
849                         p_stream->p_wf->wBitsPerSample =
850                             GetWLE((uint8_t *)&st->bits_per_sample);
851                         p_stream->p_wf->cbSize = 0;
852
853                         switch( p_stream->p_wf->wFormatTag )
854                         {
855                         case WAVE_FORMAT_PCM:
856                             p_stream->i_fourcc =
857                                 VLC_FOURCC( 'a', 'r', 'a', 'w' );
858                             break;
859                         case WAVE_FORMAT_MPEG:
860                         case WAVE_FORMAT_MPEGLAYER3:
861                             p_stream->i_fourcc =
862                                 VLC_FOURCC( 'm', 'p', 'g', 'a' );
863                             break;
864                         case WAVE_FORMAT_A52:
865                             p_stream->i_fourcc =
866                                 VLC_FOURCC( 'a', '5', '2', ' ' );
867                             break;
868                         case WAVE_FORMAT_WMA1:
869                             p_stream->i_fourcc =
870                                 VLC_FOURCC( 'w', 'm', 'a', '1' );
871                             break;
872                         case WAVE_FORMAT_WMA2:
873                             p_stream->i_fourcc =
874                                 VLC_FOURCC( 'w', 'm', 'a', '2' );
875                             break;
876                         default:
877                             p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
878                                 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
879                                 p_stream->p_wf->wFormatTag & 0xff );
880                         }
881
882                         msg_Dbg( p_input, "found audio header of type: %.4s",
883                                  (char *)&p_stream->i_fourcc );
884                         msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
885                                  "%dbits/sample %dkb/s",
886                                  p_stream->p_wf->wFormatTag,
887                                  p_stream->p_wf->nChannels,
888                                  p_stream->p_wf->nSamplesPerSec,
889                                  p_stream->p_wf->wBitsPerSample,
890                                  p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
891                     }
892                     /* Check for text (subtitles) header */
893                     else if( !strncmp(st->streamtype, "text", 4) )
894                     {
895                         /* We need to get rid of the header packet */
896                         ogg_stream_packetout( &p_stream->os, &oggpacket );
897
898                         msg_Dbg( p_input, "found text subtitles header" );
899                         p_stream->i_cat = SPU_ES;
900                         p_stream->i_fourcc =
901                             VLC_FOURCC( 's', 'u', 'b', 't' );
902                         p_stream->f_rate = 1000; /* granulepos is in milisec */
903                     }
904                     else
905                     {
906                         msg_Dbg( p_input, "stream %d has a header marker "
907                             "but is of an unknown type", p_ogg->i_streams-1 );
908                         free( p_stream );
909                         p_ogg->i_streams--;
910                     }
911                 }
912                 else
913                 {
914                     msg_Dbg( p_input, "stream %d is of unknown type",
915                              p_ogg->i_streams-1 );
916                     free( p_stream );
917                     p_ogg->i_streams--;
918                 }
919
920 #undef p_stream
921
922                 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
923                     return VLC_EGENERIC;
924             }
925
926             /* This is the first data page, which means we are now finished
927              * with the initial pages. We just need to store it in the relevant
928              * bitstream. */
929             for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
930             {
931                 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
932                                        &oggpage ) == 0 )
933                 {
934                     break;
935                 }
936             }
937             return VLC_SUCCESS;
938         }
939     }
940     return VLC_EGENERIC;
941 }
942
943 /*****************************************************************************
944  * Activate: initializes ogg demux structures
945  *****************************************************************************/
946 static int Activate( vlc_object_t * p_this )
947 {
948     int i_stream, b_forced;
949     demux_sys_t    *p_ogg;
950     input_thread_t *p_input = (input_thread_t *)p_this;
951
952     p_input->p_demux_data = NULL;
953     b_forced = ( ( *p_input->psz_demux )&&
954                  ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
955
956     /* Check if we are dealing with an ogg stream */
957     if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
958         return -1;
959
960     /* Allocate p_ogg */
961     if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
962     {
963         msg_Err( p_input, "out of memory" );
964         goto error;
965     }
966     memset( p_ogg, 0, sizeof( demux_sys_t ) );
967     p_input->p_demux_data = p_ogg;
968
969     p_ogg->i_pcr  = 0;
970     p_ogg->b_seekable = ( ( p_input->stream.b_seekable )
971                         &&( p_input->stream.i_method == INPUT_METHOD_FILE ) );
972
973     /* Initialize the Ogg physical bitstream parser */
974     ogg_sync_init( &p_ogg->oy );
975
976     /* Find the logical streams embedded in the physical stream and
977      * initialize our p_ogg structure. */
978     if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
979     {
980         msg_Err( p_input, "couldn't find an ogg logical stream" );
981         goto error;
982     }
983
984     /* Set the demux function */
985     p_input->pf_demux = Demux;
986
987     /* Initialize access plug-in structures. */
988     if( p_input->i_mtu == 0 )
989     {
990         /* Improve speed. */
991         p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
992     }
993
994     /* Create one program */
995     vlc_mutex_lock( &p_input->stream.stream_lock );
996     if( input_InitStream( p_input, 0 ) == -1)
997     {
998         vlc_mutex_unlock( &p_input->stream.stream_lock );
999         msg_Err( p_input, "cannot init stream" );
1000         goto error;
1001     }
1002     if( input_AddProgram( p_input, 0, 0) == NULL )
1003     {
1004         vlc_mutex_unlock( &p_input->stream.stream_lock );
1005         msg_Err( p_input, "cannot add program" );
1006         goto error;
1007     }
1008     p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1009     p_input->stream.i_mux_rate = 0;
1010     vlc_mutex_unlock( &p_input->stream.stream_lock );
1011
1012     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1013     {
1014 #define p_stream p_ogg->pp_stream[i_stream]
1015         vlc_mutex_lock( &p_input->stream.stream_lock );
1016         p_stream->p_es = input_AddES( p_input,
1017                                       p_input->stream.p_selected_program,
1018                                       p_ogg->i_streams + 1, 0 );
1019         p_input->stream.i_mux_rate += (p_stream->i_bitrate / ( 8 * 50 ));
1020         vlc_mutex_unlock( &p_input->stream.stream_lock );
1021         p_stream->p_es->i_stream_id = p_stream->p_es->i_id = i_stream;
1022         p_stream->p_es->i_fourcc = p_stream->i_fourcc;
1023         p_stream->p_es->i_cat = p_stream->i_cat;
1024         p_stream->p_es->p_demux_data = p_stream->p_bih ?
1025             (void *)p_stream->p_bih : (void *)p_stream->p_wf;
1026 #undef p_stream
1027     }
1028
1029     for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1030     {
1031 #define p_stream  p_ogg->pp_stream[i_stream]
1032         switch( p_stream->p_es->i_cat )
1033         {
1034             case( VIDEO_ES ):
1035                 if( (p_ogg->p_stream_video == NULL) )
1036                 {
1037                     p_ogg->p_stream_video = p_stream;
1038                     /* TODO add test to see if a decoder has been found */
1039                     Ogg_StreamStart( p_input, p_ogg, i_stream );
1040                 }
1041                 break;
1042
1043             case( AUDIO_ES ):
1044                 if( (p_ogg->p_stream_audio == NULL) )
1045                 {
1046                     int i_audio = config_GetInt( p_input, "audio-channel" );
1047                     if( i_audio == i_stream || i_audio <= 0 ||
1048                         i_audio >= p_ogg->i_streams ||
1049                         p_ogg->pp_stream[i_audio]->p_es->i_cat != AUDIO_ES )
1050                     {
1051                         p_ogg->p_stream_audio = p_stream;
1052                         Ogg_StreamStart( p_input, p_ogg, i_stream );
1053                     }
1054                 }
1055                 break;
1056
1057             case( SPU_ES ):
1058                 if( (p_ogg->p_stream_spu == NULL) )
1059                 {
1060                     /* for spu, default is none */
1061                     int i_spu = config_GetInt( p_input, "spu-channel" );
1062                     if( i_spu < 0 || i_spu >= p_ogg->i_streams ||
1063                         p_ogg->pp_stream[i_spu]->p_es->i_cat != SPU_ES )
1064                     {
1065                         break;
1066                     }
1067                     else if( i_spu == i_stream )
1068                     {
1069                         p_ogg->p_stream_spu = p_stream;
1070                         Ogg_StreamStart( p_input, p_ogg, i_stream );
1071                     }
1072                 }
1073                 break;
1074
1075             default:
1076                 break;
1077         }
1078 #undef p_stream
1079     }
1080
1081     /* we select the first audio and video ES */
1082     vlc_mutex_lock( &p_input->stream.stream_lock );
1083     if( !p_ogg->p_stream_video )
1084     {
1085         msg_Warn( p_input, "no video stream found" );
1086     }
1087     if( !p_ogg->p_stream_audio )
1088     {
1089         msg_Warn( p_input, "no audio stream found!" );
1090     }
1091     p_input->stream.p_selected_program->b_is_ok = 1;
1092     vlc_mutex_unlock( &p_input->stream.stream_lock );
1093
1094     /* Call the pace control */
1095     input_ClockManageRef( p_input,
1096                           p_input->stream.p_selected_program,
1097                           p_ogg->i_pcr );
1098
1099     return 0;
1100
1101  error:
1102     Deactivate( (vlc_object_t *)p_input );
1103     return -1;
1104
1105 }
1106
1107 /*****************************************************************************
1108  * Deactivate: frees unused data
1109  *****************************************************************************/
1110 static void Deactivate( vlc_object_t *p_this )
1111 {
1112     input_thread_t *p_input = (input_thread_t *)p_this;
1113     demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data  ; 
1114     int i, j;
1115
1116     if( p_ogg )
1117     {
1118         /* Cleanup the bitstream parser */
1119         ogg_sync_clear( &p_ogg->oy );
1120
1121         for( i = 0; i < p_ogg->i_streams; i++ )
1122         {
1123             ogg_stream_clear( &p_ogg->pp_stream[i]->os );
1124             for( j = 0; j < p_ogg->pp_stream[i]->i_packets_backup; j++ )
1125             {
1126                 free( p_ogg->pp_stream[i]->p_packets_backup[j].packet );
1127             }
1128             if( p_ogg->pp_stream[i]->p_packets_backup)
1129                 free( p_ogg->pp_stream[i]->p_packets_backup );
1130 #if 0 /* hmmm, it's already freed in input_DelES() */
1131             if( p_ogg->pp_stream[i]->p_bih )
1132                 free( p_ogg->pp_stream[i]->p_bih );
1133             if( p_ogg->pp_stream[i]->p_wf )
1134                 free( p_ogg->pp_stream[i]->p_wf );
1135 #endif
1136             free( p_ogg->pp_stream[i] );
1137         }
1138         if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1139
1140         free( p_ogg );
1141     }
1142 }
1143
1144 /*****************************************************************************
1145  * Demux: reads and demuxes data packets
1146  *****************************************************************************
1147  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1148  *****************************************************************************/
1149 static int Demux( input_thread_t * p_input )
1150 {
1151     int i, i_stream, b_eos = 0;
1152     ogg_page    oggpage;
1153     ogg_packet  oggpacket;
1154     demux_sys_t *p_ogg  = (demux_sys_t *)p_input->p_demux_data;
1155
1156 #define p_stream p_ogg->pp_stream[i_stream]
1157     /* detect new selected/unselected streams */
1158     for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1159     {
1160         if( p_stream->p_es )
1161         {
1162             if( p_stream->p_es->p_decoder_fifo &&
1163                 !p_stream->i_activated )
1164             {
1165                 Ogg_StreamStart( p_input, p_ogg, i_stream );
1166             }
1167             else
1168             if( !p_stream->p_es->p_decoder_fifo &&
1169                 p_stream->i_activated )
1170             {
1171                 Ogg_StreamStop( p_input, p_ogg, i_stream );
1172             }
1173         }
1174     }
1175
1176     /* search for new video and audio stream to select
1177      * if current have been unselected */
1178     if( ( !p_ogg->p_stream_video )
1179             || ( !p_ogg->p_stream_video->p_es->p_decoder_fifo ) )
1180     {
1181         p_ogg->p_stream_video = NULL;
1182         for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1183         {
1184             if( ( p_stream->i_cat == VIDEO_ES )
1185                   &&( p_stream->p_es->p_decoder_fifo ) )
1186             {
1187                 p_ogg->p_stream_video = p_stream;
1188                 break;
1189             }
1190         }
1191     }
1192     if( ( !p_ogg->p_stream_audio )
1193             ||( !p_ogg->p_stream_audio->p_es->p_decoder_fifo ) )
1194     {
1195         p_ogg->p_stream_audio = NULL;
1196         for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1197         {
1198             if( ( p_stream->i_cat == AUDIO_ES )
1199                   &&( p_stream->p_es->p_decoder_fifo ) )
1200             {
1201                 p_ogg->p_stream_audio = p_stream;
1202                 break;
1203             }
1204         }
1205     }
1206
1207     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1208     {
1209         msg_Warn( p_input, "synchro reinit" );
1210
1211         /* An ogg packet does only contain the starting date of the next
1212          * packet, not its own starting date.
1213          * As a quick work around, we just skip an oggpage */
1214
1215         for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1216         {
1217             /* we'll trash all the data until we find the next pcr */
1218             p_stream->b_reinit = 1;
1219             p_stream->i_pcr = -1;
1220             p_stream->i_interpolated_pcr = -1;
1221         }
1222         p_ogg->b_reinit = 1;
1223     }
1224
1225
1226     /*
1227      * Demux ogg pages from the stream
1228      */
1229     for( i = 0; i < PAGES_READ_ONCE || p_ogg->b_reinit;  i++ )
1230     {
1231         if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1232         {
1233             b_eos = 1;
1234             break;
1235         }
1236
1237         for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1238         {
1239             if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1240                 continue;
1241
1242             while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1243             {
1244                 if( !p_stream->p_es )
1245                 {
1246                     break;
1247                 }
1248
1249                 if( p_stream->b_reinit )
1250                 {
1251                     if( oggpacket.granulepos >= 0 )
1252                     {
1253                         p_stream->b_reinit = 0;
1254
1255                         /* Convert the next granulepos into a pcr */
1256                         Ogg_UpdatePCR( p_stream, &oggpacket );
1257
1258                         /* Call the pace control to reinitialize
1259                          * the system clock */
1260                          input_ClockManageRef( p_input,
1261                              p_input->stream.p_selected_program,
1262                              p_stream->i_pcr );
1263
1264                          if( (!p_ogg->p_stream_video ||
1265                               !p_ogg->p_stream_video->b_reinit) &&
1266                              (!p_ogg->p_stream_audio ||
1267                               !p_ogg->p_stream_audio->b_reinit) )
1268                          {
1269                              p_ogg->b_reinit = 0;
1270                          }
1271                     }
1272                     continue;
1273                 }
1274
1275                 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1276
1277             }
1278         }
1279     }
1280
1281     i_stream = 0;
1282     p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1283     for( ; i_stream < p_ogg->i_streams; i_stream++ )
1284     {
1285         if( p_stream->i_cat == SPU_ES )
1286             continue;
1287
1288         if( p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1289             p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1290     }
1291 #undef p_stream
1292
1293
1294     /* Call the pace control */
1295     input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1296                           p_ogg->i_pcr );
1297
1298     /* Did we reach the end of stream ? */
1299     return( b_eos ? 0 : 1 );
1300 }