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