root/modules/demux/ogg.c

Revision 4bfe8808eb8513e840a68a47400af5ca16c72cee, 65.4 kB (checked in by Pierre d'Herbemont <pdherbemont@videolan.org>, 2 weeks ago)

ogg: Fix an unitialized value usage.

Please review.

Spotted by llvm/clang analyser.

  • Property mode set to 100644
Line 
1 /*****************************************************************************
2  * ogg.c : ogg stream demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2007 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *          Andre Pang <Andre.Pang@csiro.au> (Annodex support)
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_meta.h>
36 #include <vlc_input.h>
37
38 #include <ogg/ogg.h>
39
40 #include <vlc_codecs.h>
41 #include <vlc_bits.h>
42
43 /*****************************************************************************
44  * Module descriptor
45  *****************************************************************************/
46 static int  Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
48
49 vlc_module_begin();
50     set_shortname ( "OGG" );
51     set_description( N_("OGG demuxer" ) );
52     set_category( CAT_INPUT );
53     set_subcategory( SUBCAT_INPUT_DEMUX );
54     set_capability( "demux", 50 );
55     set_callbacks( Open, Close );
56     add_shortcut( "ogg" );
57 vlc_module_end();
58
59
60 /*****************************************************************************
61  * Definitions of structures and functions used by this plugins
62  *****************************************************************************/
63 typedef struct logical_stream_s
64 {
65     ogg_stream_state os;                        /* logical stream of packets */
66
67     es_format_t      fmt;
68     es_format_t      fmt_old;                  /* format of old ES is reused */
69     es_out_id_t      *p_es;
70     double           f_rate;
71
72     int              i_serial_no;
73
74     /* the header of some logical streams (eg vorbis) contain essential
75      * data for the decoder. We back them up here in case we need to re-feed
76      * them to the decoder. */
77     int              b_force_backup;
78     int              i_packets_backup;
79     uint8_t          *p_headers;
80     int              i_headers;
81
82     /* program clock reference (in units of 90kHz) derived from the previous
83      * granulepos */
84     mtime_t          i_pcr;
85     mtime_t          i_interpolated_pcr;
86     mtime_t          i_previous_pcr;
87
88     /* Misc */
89     int b_reinit;
90     int i_granule_shift;
91
92     /* kate streams have the number of headers in the ID header */
93     int i_kate_num_headers;
94
95     /* for Annodex logical bitstreams */
96     int secondary_header_packets;
97
98 } logical_stream_t;
99
100 struct demux_sys_t
101 {
102     ogg_sync_state oy;        /* sync and verify incoming physical bitstream */
103
104     int i_streams;                           /* number of logical bitstreams */
105     logical_stream_t **pp_stream;  /* pointer to an array of logical streams */
106
107     logical_stream_t *p_old_stream; /* pointer to a old logical stream to avoid recreating it */
108
109     /* program clock reference (in units of 90kHz) derived from the pcr of
110      * the sub-streams */
111     mtime_t i_pcr;
112
113     /* stream state */
114     int     i_eos;
115
116     /* bitrate */
117     int     i_bitrate;
118 };
119
120 /* OggDS headers for the new header format (used in ogm files) */
121 typedef struct
122 {
123     ogg_int32_t width;
124     ogg_int32_t height;
125 } stream_header_video_t;
126
127 typedef struct
128 {
129     ogg_int16_t channels;
130     ogg_int16_t padding;
131     ogg_int16_t blockalign;
132     ogg_int32_t avgbytespersec;
133 } stream_header_audio_t;
134
135 typedef struct
136 {
137     char        streamtype[8];
138     char        subtype[4];
139
140     ogg_int32_t size;                               /* size of the structure */
141
142     ogg_int64_t time_unit;                              /* in reference time */
143     ogg_int64_t samples_per_unit;
144     ogg_int32_t default_len;                                /* in media time */
145
146     ogg_int32_t buffersize;
147     ogg_int16_t bits_per_sample;
148     ogg_int16_t padding;
149
150     union
151     {
152         /* Video specific */
153         stream_header_video_t video;
154         /* Audio specific */
155         stream_header_audio_t audio;
156     } sh;
157 } stream_header_t;
158
159 #define OGG_BLOCK_SIZE 4096
160
161 /* Some defines from OggDS */
162 #define PACKET_TYPE_HEADER   0x01
163 #define PACKET_TYPE_BITS     0x07
164 #define PACKET_LEN_BITS01    0xc0
165 #define PACKET_LEN_BITS2     0x02
166 #define PACKET_IS_SYNCPOINT  0x08
167
168 /*****************************************************************************
169  * Local prototypes
170  *****************************************************************************/
171 static int  Demux  ( demux_t * );
172 static int  Control( demux_t *, int, va_list );
173
174 /* Bitstream manipulation */
175 static int  Ogg_ReadPage     ( demux_t *, ogg_page * );
176 static void Ogg_UpdatePCR    ( logical_stream_t *, ogg_packet * );
177 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
178
179 static int Ogg_BeginningOfStream( demux_t *p_demux );
180 static int Ogg_FindLogicalStreams( demux_t *p_demux );
181 static void Ogg_EndOfStream( demux_t *p_demux );
182
183 /* */
184 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
185 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
186
187 /* Logical bitstream headers */
188 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
189 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
190 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
191 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
192 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
193 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
194 static void Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
195
196 /*****************************************************************************
197  * Open: initializes ogg demux structures
198  *****************************************************************************/
199 static int Open( vlc_object_t * p_this )
200 {
201     demux_t *p_demux = (demux_t *)p_this;
202     demux_sys_t    *p_sys;
203     const uint8_t  *p_peek;
204
205
206     /* Check if we are dealing with an ogg stream */
207     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
208     if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
209     {
210         return VLC_EGENERIC;
211     }
212
213     /* Set exported functions */
214     p_demux->pf_demux = Demux;
215     p_demux->pf_control = Control;
216     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
217     if( !p_sys )
218         return VLC_ENOMEM;
219
220     memset( p_sys, 0, sizeof( demux_sys_t ) );
221     p_sys->i_bitrate = 0;
222     p_sys->pp_stream = NULL;
223     p_sys->p_old_stream = NULL;
224
225     /* Begnning of stream, tell the demux to look for elementary streams. */
226     p_sys->i_eos = 0;
227
228     /* Initialize the Ogg physical bitstream parser */
229     ogg_sync_init( &p_sys->oy );
230
231     return VLC_SUCCESS;
232 }
233
234 /*****************************************************************************
235  * Close: frees unused data
236  *****************************************************************************/
237 static void Close( vlc_object_t *p_this )
238 {
239     demux_t *p_demux = (demux_t *)p_this;
240     demux_sys_t *p_sys = p_demux->p_sys  ;
241
242     /* Cleanup the bitstream parser */
243     ogg_sync_clear( &p_sys->oy );
244
245     Ogg_EndOfStream( p_demux );
246
247     if( p_sys->p_old_stream )
248         Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
249
250     free( p_sys );
251 }
252
253 /*****************************************************************************
254  * Demux: reads and demuxes data packets
255  *****************************************************************************
256  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
257  *****************************************************************************/
258 static int Demux( demux_t * p_demux )
259 {
260     demux_sys_t *p_sys = p_demux->p_sys;
261     ogg_page    oggpage;
262     ogg_packet  oggpacket;
263     int         i_stream;
264
265
266     if( p_sys->i_eos == p_sys->i_streams )
267     {
268         if( p_sys->i_eos )
269         {
270             msg_Dbg( p_demux, "end of a group of logical streams" );
271             /* We keep the ES to try reusing it in Ogg_BeginningOfStream
272              * only 1 ES is supported (common case for ogg web radio) */
273             if( p_sys->i_streams == 1 )
274             {
275                 p_sys->p_old_stream = p_sys->pp_stream[0];
276                 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
277             }
278             Ogg_EndOfStream( p_demux );
279         }
280
281         p_sys->i_eos = 0;
282         if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
283             return 0;
284
285         msg_Dbg( p_demux, "beginning of a group of logical streams" );
286         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
287     }
288
289     /*
290      * Demux an ogg page from the stream
291      */
292     if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
293     {
294         return 0; /* EOF */
295     }
296
297     /* Test for End of Stream */
298     if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
299
300
301     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
302     {
303         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
304
305         if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
306             continue;
307
308         while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
309         {
310             /* Read info from any secondary header packets, if there are any */
311             if( p_stream->secondary_header_packets > 0 )
312             {
313                 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
314                         oggpacket.bytes >= 7 &&
315                         ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
316                 {
317                     Ogg_ReadTheoraHeader( p_stream, &oggpacket );
318                     p_stream->secondary_header_packets = 0;
319                 }
320                 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
321                         oggpacket.bytes >= 7 &&
322                         ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
323                 {
324                     Ogg_ReadVorbisHeader( p_stream, &oggpacket );
325                     p_stream->secondary_header_packets = 0;
326                 }
327                 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
328                 {
329                     p_stream->secondary_header_packets = 0;
330                 }
331             }
332
333             if( p_stream->b_reinit )
334             {
335                 /* If synchro is re-initialized we need to drop all the packets
336                  * until we find a new dated one. */
337                 Ogg_UpdatePCR( p_stream, &oggpacket );
338
339                 if( p_stream->i_pcr >= 0 )
340                 {
341                     p_stream->b_reinit = 0;
342                 }
343                 else
344                 {
345                     p_stream->i_interpolated_pcr = -1;
346                     continue;
347                 }
348
349                 /* An Ogg/vorbis packet contains an end date granulepos */
350                 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
351                     p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
352                     p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
353                 {
354                     if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
355                     {
356                         Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
357                     }
358                     else
359                     {
360                         es_out_Control( p_demux->out, ES_OUT_SET_PCR,
361                                         p_stream->i_pcr );
362                     }
363                     continue;
364                 }
365             }
366
367             Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
368         }
369         break;
370     }
371
372     i_stream = 0; p_sys->i_pcr = -1;
373     for( ; i_stream < p_sys->i_streams; i_stream++ )
374     {
375         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
376
377         if( p_stream->fmt.i_cat == SPU_ES )
378             continue;
379         if( p_stream->i_interpolated_pcr < 0 )
380             continue;
381
382         if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
383             p_sys->i_pcr = p_stream->i_interpolated_pcr;
384     }
385
386     if( p_sys->i_pcr >= 0 )
387     {
388         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
389     }
390
391     return 1;
392 }
393
394 /*****************************************************************************
395  * Control:
396  *****************************************************************************/
397 static int Control( demux_t *p_demux, int i_query, va_list args )
398 {
399     demux_sys_t *p_sys  = p_demux->p_sys;
400     int64_t *pi64;
401     bool *pb_bool;
402     int i;
403
404     switch( i_query )
405     {
406         case DEMUX_HAS_UNSUPPORTED_META:
407             pb_bool = (bool*)va_arg( args, bool* );
408             *pb_bool = true;
409             return VLC_SUCCESS;
410
411         case DEMUX_GET_TIME:
412             pi64 = (int64_t*)va_arg( args, int64_t * );
413             *pi64 = p_sys->i_pcr;
414             return VLC_SUCCESS;
415
416         case DEMUX_SET_TIME:
417             return VLC_EGENERIC;
418
419         case DEMUX_SET_POSITION:
420             for( i = 0; i < p_sys->i_streams; i++ )
421             {
422                 logical_stream_t *p_stream = p_sys->pp_stream[i];
423
424                 /* we'll trash all the data until we find the next pcr */
425                 p_stream->b_reinit = 1;
426                 p_stream->i_pcr = -1;
427                 p_stream->i_interpolated_pcr = -1;
428                 ogg_stream_reset( &p_stream->os );
429             }
430             ogg_sync_reset( &p_sys->oy );
431
432         default:
433             return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
434                                            1, i_query, args );
435     }
436 }
437
438 /****************************************************************************
439  * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
440  ****************************************************************************
441  * Returns VLC_SUCCESS if a page has been read. An error might happen if we
442  * are at the end of stream.
443  ****************************************************************************/
444 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
445 {
446     demux_sys_t *p_ogg = p_demux->p_sys  ;
447     int i_read = 0;
448     char *p_buffer;
449
450     while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
451     {
452         p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
453
454         i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
455         if( i_read <= 0 )
456             return VLC_EGENERIC;
457
458         ogg_sync_wrote( &p_ogg->oy, i_read );
459     }
460
461     return VLC_SUCCESS;
462 }
463
464 /****************************************************************************
465  * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
466  *                current stream.
467  ****************************************************************************/
468 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
469                            ogg_packet *p_oggpacket )
470 {
471     /* Convert the granulepos into a pcr */
472     if( p_oggpacket->granulepos >= 0 )
473     {
474         if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
475             p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
476         {
477             ogg_int64_t iframe = p_oggpacket->granulepos >>
478               p_stream->i_granule_shift;
479             ogg_int64_t pframe = p_oggpacket->granulepos -
480               ( iframe << p_stream->i_granule_shift );
481
482             p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
483                               / p_stream->f_rate;
484         }
485         else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
486         {
487             ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
488             /* NB, OggDirac granulepos values are in units of 2*picturerate */
489             p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
490         }
491         else
492         {
493             p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
494                               / p_stream->f_rate;
495         }
496
497         p_stream->i_interpolated_pcr = p_stream->i_pcr;
498     }
499     else
500     {
501         p_stream->i_pcr = -1;
502
503         /* no granulepos available, try to interpolate the pcr.
504          * If we can't then don't touch the old value. */
505         if( p_stream->fmt.i_cat == VIDEO_ES )
506             /* 1 frame per packet */
507             p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
508         else if( p_stream->fmt.i_bitrate )
509             p_stream->i_interpolated_pcr +=
510                 ( p_oggpacket->bytes * INT64_C(1000000) /
511                   p_stream->fmt.i_bitrate / 8 );
512     }
513 }
514
515 /****************************************************************************
516  * Ogg_DecodePacket: Decode an Ogg packet.
517  ****************************************************************************/
518 static void Ogg_DecodePacket( demux_t *p_demux,
519                               logical_stream_t *p_stream,
520                               ogg_packet *p_oggpacket )
521 {
522     block_t *p_block;
523     bool b_selected;
524     int i_header_len = 0;
525     mtime_t i_pts = -1, i_interpolated_pts;
526
527     /* Sanity check */
528     if( !p_oggpacket->bytes )
529     {
530         msg_Dbg( p_demux, "discarding 0 sized packet" );
531         return;
532     }
533
534     if( p_oggpacket->bytes >= 7 &&
535         ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
536     {
537         /* it's an Annodex packet -- skip it (do nothing) */
538         return;
539     }
540     else if( p_oggpacket->bytes >= 7 &&
541         ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
542     {
543         /* it's an AnxData packet -- skip it (do nothing) */
544         return;
545     }
546
547     if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
548         p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
549
550     /* Check the ES is selected */
551     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
552                     p_stream->p_es, &b_selected );
553
554     if( p_stream->b_force_backup )
555     {
556         uint8_t *p_sav;
557         bool b_store_size = true;
558         bool b_store_num_headers = false;
559
560         p_stream->i_packets_backup++;
561         switch( p_stream->fmt.i_codec )
562         {
563         case VLC_FOURCC( 'v','o','r','b' ):
564         case VLC_FOURCC( 's','p','x',' ' ):
565         case VLC_FOURCC( 't','h','e','o' ):
566             if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
567             break;
568
569         case VLC_FOURCC( 'f','l','a','c' ):
570             if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
571             {
572                 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
573                 p_stream->b_force_backup = 0;
574             }
575             else if( p_stream->fmt.audio.i_rate )
576             {
577                 p_stream->b_force_backup = 0;
578                 if( p_oggpacket->bytes >= 9 )
579                 {
580                     p_oggpacket->packet += 9;
581                     p_oggpacket->bytes -= 9;
582                 }
583             }
584             b_store_size = false;
585             break;
586
587         case VLC_FOURCC( 'k','a','t','e' ):
588             if( p_stream->i_packets_backup == 1)
589                 b_store_num_headers = true;
590             if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
591             break;
592
593         default:
594             p_stream->b_force_backup = 0;
595             break;
596         }
597
598         /* Backup the ogg packet (likely an header packet) */
599         p_stream->p_headers =
600             realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
601                      p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
602         if( p_stream->p_headers )
603         {
604             uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
605
606             if( b_store_num_headers )
607             {
608                 /* Kate streams store the number of headers in the first header,
609                    so we can't just test for 3 as Vorbis/Theora */
610                 *(p_extra++) = p_stream->i_kate_num_headers;
611             }
612             if( b_store_size )
613             {
614                 *(p_extra++) = p_oggpacket->bytes >> 8;
615                 *(p_extra++) = p_oggpacket->bytes & 0xFF;
616             }
617             memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
618             p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
619
620             if( !p_stream->b_force_backup )
621             {
622                 /* Last header received, commit changes */
623                 free( p_stream->fmt.p_extra );
624
625                 p_stream->fmt.i_extra = p_stream->i_headers;
626                 p_stream->fmt.p_extra =
627                     realloc( p_stream->fmt.p_extra, p_stream->i_headers );
628                 if( p_stream->fmt.p_extra )
629                     memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
630                             p_stream->i_headers );
631                 else
632                     p_stream->fmt.i_extra = 0;
633
634                 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
635                     es_out_Control( p_demux->out, ES_OUT_SET_FMT,
636                                     p_stream->p_es, &p_stream->fmt );
637             }
638         }
639         else
640         {
641                 p_stream->p_headers = p_sav;
642         }
643
644         b_selected = false; /* Discard the header packet */
645     }
646
647     /* Convert the pcr into a pts */
648     if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
649         p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
650         p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
651     {
652         if( p_stream->i_pcr >= 0 )
653         {
654             /* This is for streams where the granulepos of the header packets
655              * doesn't match these of the data packets (eg. ogg web radios). */
656             if( p_stream->i_previous_pcr == 0 &&
657                 p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
658             {
659                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
660
661                 /* Call the pace control */
662                 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
663                                 p_stream->i_pcr );
664             }
665
666             p_stream->i_previous_pcr = p_stream->i_pcr;
667
668             /* The granulepos is the end date of the sample */
669             i_pts =  p_stream->i_pcr;
670         }
671     }
672
673     /* Convert the granulepos into the next pcr */
674     i_interpolated_pts = p_stream->i_interpolated_pcr;
675     Ogg_UpdatePCR( p_stream, p_oggpacket );
676
677     if( p_stream->i_pcr >= 0 )
678     {
679         /* This is for streams where the granulepos of the header packets
680          * doesn't match these of the data packets (eg. ogg web radios). */
681         if( p_stream->i_previous_pcr == 0 &&
682             p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
683         {
684             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
685
686             /* Call the pace control */
687             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
688         }
689     }
690
691     if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
692         p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
693         p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
694         p_stream->i_pcr >= 0 )
695     {
696         p_stream->i_previous_pcr = p_stream->i_pcr;
697
698         /* The granulepos is the start date of the sample */
699         i_pts = p_stream->i_pcr;
700     }
701
702     if( !b_selected )
703     {
704         /* This stream isn't currently selected so we don't need to decode it,
705          * but we did need to store its pcr as it might be selected later on */
706         return;
707     }
708
709     if( p_oggpacket->bytes <= 0 )
710         return;
711
712     if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
713
714     /* Normalize PTS */
715     if( i_pts == 0 ) i_pts = 1;
716     else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
717     else if( i_pts == -1 ) i_pts = 0;
718
719     if( p_stream->fmt.i_cat == AUDIO_ES )
720         p_block->i_dts = p_block->i_pts = i_pts;
721     else if( p_stream->fmt.i_cat == SPU_ES )
722     {
723         p_block->i_dts = p_block->i_pts = i_pts;
724         p_block->i_length = 0;
725     }
726     else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
727         p_block->i_dts = p_block->i_pts = i_pts;
728     else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
729     {
730         ogg_int64_t dts = p_oggpacket->granulepos >> 31;
731         ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
732
733         uint64_t u_pnum = dts + delay;
734
735         p_block->i_dts = p_stream->i_pcr;
736         p_block->i_pts = 0;
737         /* NB, OggDirac granulepos values are in units of 2*picturerate */
738         if( -1 != p_oggpacket->granulepos )
739             p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
740     }
741     else
742     {
743         p_block->i_dts = i_pts;
744         p_block->i_pts = 0;
745     }
746
747     if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
748         p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
749         p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
750         p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
751         p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
752         p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
753         p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
754         p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
755     {
756         /* We remove the header from the packet */
757         i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
758         i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
759
760         if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
761         {
762             /* But with subtitles we need to retrieve the duration first */
763             int i, lenbytes = 0;
764
765             if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
766             {
767                 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
768                 {
769                     lenbytes = lenbytes << 8;
770                     lenbytes += *(p_oggpacket->packet + i_header_len - i);
771                 }
772             }
773             if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
774                 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
775                   p_oggpacket->packet[i_header_len + 1] != 0 &&
776                   p_oggpacket->packet[i_header_len + 1] != '\n' &&
777                   p_oggpacket->packet[i_header_len + 1] != '\r' ) )
778             {
779                 p_block->i_length = (mtime_t)lenbytes * 1000;
780             }
781         }
782
783         i_header_len++;
784         if( p_block->i_buffer >= (unsigned int)i_header_len )
785             p_block->i_buffer -= i_header_len;
786         else
787             p_block->i_buffer = 0;
788     }
789
790     if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
791     {
792         /* FIXME: the biggest hack I've ever done */
793         msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
794                   p_block->i_pts, p_block->i_dts );
795         msleep(10000);
796     }
797
798     memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
799             p_oggpacket->bytes - i_header_len );
800
801     es_out_Send( p_demux->out, p_stream->p_es, p_block );
802 }
803
804 /****************************************************************************
805  * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
806  *                         stream and fill p_ogg.
807  *****************************************************************************
808  * The initial page of a logical stream is marked as a 'bos' page.
809  * Furthermore, the Ogg specification mandates that grouped bitstreams begin
810  * together and all of the initial pages must appear before any data pages.
811  *
812  * On success this function returns VLC_SUCCESS.
813  ****************************************************************************/
814 static int Ogg_FindLogicalStreams( demux_t *p_demux )
815 {
816     demux_sys_t *p_ogg = p_demux->p_sys  ;
817     ogg_packet oggpacket;
818     ogg_page oggpage;
819     int i_stream;
820
821     while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
822     {
823         if( ogg_page_bos( &oggpage ) )
824         {
825
826             /* All is wonderful in our fine fine little world.
827              * We found the beginning of our first logical stream. */
828             while( ogg_page_bos( &oggpage ) )
829             {
830                 logical_stream_t **pp_sav = p_ogg->pp_stream;
831                 logical_stream_t *p_stream;
832
833                 p_stream = malloc( sizeof(logical_stream_t) );
834                 if( !p_stream )
835                     return VLC_ENOMEM;
836
837                 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
838
839                 memset( p_stream, 0, sizeof(logical_stream_t) );
840                 p_stream->p_headers = 0;
841                 p_stream->secondary_header_packets = 0;
842
843                 es_format_Init( &p_stream->fmt, 0, 0 );
844                 es_format_Init( &p_stream->fmt_old, 0, 0 );
845
846                 /* Setup the logical stream */
847                 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
848                 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
849
850                 /* Extract the initial header from the first page and verify
851                  * the codec type of tis Ogg bitstream */
852                 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
853                 {
854                     /* error. stream version mismatch perhaps */
855                     msg_Err( p_demux, "error reading first page of "
856                              "Ogg bitstream data" );
857                     return VLC_EGENERIC;
858                 }
859
860                 /* FIXME: check return value */
861                 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
862
863                 /* Check for Vorbis header */
864