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