]> git.sesse.net Git - vlc/blob - modules/demux/ogg.c
Fixed a buffer overread with broken oggs and check alloc values.
[vlc] / modules / demux / ogg.c
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 stream_header_video
122 {
123     ogg_int32_t width;
124     ogg_int32_t height;
125 } stream_header_video;
126
127 typedef struct stream_header_audio
128 {
129     ogg_int16_t channels;
130     ogg_int16_t blockalign;
131     ogg_int32_t avgbytespersec;
132 } stream_header_audio;
133
134 typedef struct stream_header
135 {
136     char        streamtype[8];
137     char        subtype[4];
138
139     ogg_int32_t size;                               /* size of the structure */
140
141     ogg_int64_t time_unit;                              /* in reference time */
142     ogg_int64_t samples_per_unit;
143     ogg_int32_t default_len;                                /* in media time */
144
145     ogg_int32_t buffersize;
146     ogg_int16_t bits_per_sample;
147
148     union
149     {
150         /* Video specific */
151         stream_header_video video;
152         /* Audio specific */
153         stream_header_audio audio;
154     } sh;
155 } stream_header;
156
157 #define OGG_BLOCK_SIZE 4096
158
159 /* Some defines from OggDS */
160 #define PACKET_TYPE_HEADER   0x01
161 #define PACKET_TYPE_BITS     0x07
162 #define PACKET_LEN_BITS01    0xc0
163 #define PACKET_LEN_BITS2     0x02
164 #define PACKET_IS_SYNCPOINT  0x08
165
166 /*****************************************************************************
167  * Local prototypes
168  *****************************************************************************/
169 static int  Demux  ( demux_t * );
170 static int  Control( demux_t *, int, va_list );
171
172 /* Bitstream manipulation */
173 static int  Ogg_ReadPage     ( demux_t *, ogg_page * );
174 static void Ogg_UpdatePCR    ( logical_stream_t *, ogg_packet * );
175 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
176
177 static int Ogg_BeginningOfStream( demux_t *p_demux );
178 static int Ogg_FindLogicalStreams( demux_t *p_demux );
179 static void Ogg_EndOfStream( demux_t *p_demux );
180
181 /* */
182 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
183 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
184
185 /* Logical bitstream headers */
186 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
187 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
188 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
189 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
190 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
191 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
192 static void Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
193
194 /*****************************************************************************
195  * Open: initializes ogg demux structures
196  *****************************************************************************/
197 static int Open( vlc_object_t * p_this )
198 {
199     demux_t *p_demux = (demux_t *)p_this;
200     demux_sys_t    *p_sys;
201     const uint8_t  *p_peek;
202
203
204     /* Check if we are dealing with an ogg stream */
205     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
206     if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
207     {
208         return VLC_EGENERIC;
209     }
210
211     /* Set exported functions */
212     p_demux->pf_demux = Demux;
213     p_demux->pf_control = Control;
214     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
215     if( !p_sys )
216         return VLC_ENOMEM;
217
218     memset( p_sys, 0, sizeof( demux_sys_t ) );
219     p_sys->i_bitrate = 0;
220     p_sys->pp_stream = NULL;
221     p_sys->p_old_stream = NULL;
222
223     /* Begnning of stream, tell the demux to look for elementary streams. */
224     p_sys->i_eos = 0;
225
226     /* Initialize the Ogg physical bitstream parser */
227     ogg_sync_init( &p_sys->oy );
228
229     return VLC_SUCCESS;
230 }
231
232 /*****************************************************************************
233  * Close: frees unused data
234  *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
236 {
237     demux_t *p_demux = (demux_t *)p_this;
238     demux_sys_t *p_sys = p_demux->p_sys  ;
239
240     /* Cleanup the bitstream parser */
241     ogg_sync_clear( &p_sys->oy );
242
243     Ogg_EndOfStream( p_demux );
244
245     if( p_sys->p_old_stream )
246         Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
247
248     free( p_sys );
249 }
250
251 /*****************************************************************************
252  * Demux: reads and demuxes data packets
253  *****************************************************************************
254  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
255  *****************************************************************************/
256 static int Demux( demux_t * p_demux )
257 {
258     demux_sys_t *p_sys = p_demux->p_sys;
259     ogg_page    oggpage;
260     ogg_packet  oggpacket;
261     int         i_stream;
262
263
264     if( p_sys->i_eos == p_sys->i_streams )
265     {
266         if( p_sys->i_eos )
267         {
268             msg_Dbg( p_demux, "end of a group of logical streams" );
269             /* We keep the ES to try reusing it in Ogg_BeginningOfStream
270              * only 1 ES is supported (common case for ogg web radio) */
271             if( p_sys->i_streams == 1 )
272             {
273                 p_sys->p_old_stream = p_sys->pp_stream[0];
274                 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
275             }
276             Ogg_EndOfStream( p_demux );
277         }
278
279         p_sys->i_eos = 0;
280         if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
281             return 0;
282
283         msg_Dbg( p_demux, "beginning of a group of logical streams" );
284         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
285     }
286
287     /*
288      * Demux an ogg page from the stream
289      */
290     if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
291     {
292         return 0; /* EOF */
293     }
294
295     /* Test for End of Stream */
296     if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
297
298
299     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
300     {
301         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
302
303         if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
304             continue;
305
306         while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
307         {
308             /* Read info from any secondary header packets, if there are any */
309             if( p_stream->secondary_header_packets > 0 )
310             {
311                 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
312                         oggpacket.bytes >= 7 &&
313                         ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
314                 {
315                     Ogg_ReadTheoraHeader( p_stream, &oggpacket );
316                     p_stream->secondary_header_packets = 0;
317                 }
318                 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
319                         oggpacket.bytes >= 7 &&
320                         ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
321                 {
322                     Ogg_ReadVorbisHeader( p_stream, &oggpacket );
323                     p_stream->secondary_header_packets = 0;
324                 }
325                 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
326                 {
327                     p_stream->secondary_header_packets = 0;
328                 }
329             }
330
331             if( p_stream->b_reinit )
332             {
333                 /* If synchro is re-initialized we need to drop all the packets
334                  * until we find a new dated one. */
335                 Ogg_UpdatePCR( p_stream, &oggpacket );
336
337                 if( p_stream->i_pcr >= 0 )
338                 {
339                     p_stream->b_reinit = 0;
340                 }
341                 else
342                 {
343                     p_stream->i_interpolated_pcr = -1;
344                     continue;
345                 }
346
347                 /* An Ogg/vorbis packet contains an end date granulepos */
348                 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
349                     p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
350                     p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
351                 {
352                     if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
353                     {
354                         Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
355                     }
356                     else
357                     {
358                         es_out_Control( p_demux->out, ES_OUT_SET_PCR,
359                                         p_stream->i_pcr );
360                     }
361                     continue;
362                 }
363             }
364
365             Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
366         }
367         break;
368     }
369
370     i_stream = 0; p_sys->i_pcr = -1;
371     for( ; i_stream < p_sys->i_streams; i_stream++ )
372     {
373         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
374
375         if( p_stream->fmt.i_cat == SPU_ES )
376             continue;
377         if( p_stream->i_interpolated_pcr < 0 )
378             continue;
379
380         if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
381             p_sys->i_pcr = p_stream->i_interpolated_pcr;
382     }
383
384     if( p_sys->i_pcr >= 0 )
385     {
386         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
387     }
388
389     return 1;
390 }
391
392 /*****************************************************************************
393  * Control:
394  *****************************************************************************/
395 static int Control( demux_t *p_demux, int i_query, va_list args )
396 {
397     demux_sys_t *p_sys  = p_demux->p_sys;
398     int64_t *pi64;
399     bool *pb_bool;
400     int i;
401
402     switch( i_query )
403     {
404         case DEMUX_HAS_UNSUPPORTED_META:
405             pb_bool = (bool*)va_arg( args, bool* );
406             *pb_bool = true;
407             return VLC_SUCCESS;
408
409         case DEMUX_GET_TIME:
410             pi64 = (int64_t*)va_arg( args, int64_t * );
411             *pi64 = p_sys->i_pcr;
412             return VLC_SUCCESS;
413
414         case DEMUX_SET_TIME:
415             return VLC_EGENERIC;
416
417         case DEMUX_SET_POSITION:
418             for( i = 0; i < p_sys->i_streams; i++ )
419             {
420                 logical_stream_t *p_stream = p_sys->pp_stream[i];
421
422                 /* we'll trash all the data until we find the next pcr */
423                 p_stream->b_reinit = 1;
424                 p_stream->i_pcr = -1;
425                 p_stream->i_interpolated_pcr = -1;
426                 ogg_stream_reset( &p_stream->os );
427             }
428             ogg_sync_reset( &p_sys->oy );
429
430         default:
431             return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
432                                            1, i_query, args );
433     }
434 }
435
436 /****************************************************************************
437  * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
438  ****************************************************************************
439  * Returns VLC_SUCCESS if a page has been read. An error might happen if we
440  * are at the end of stream.
441  ****************************************************************************/
442 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
443 {
444     demux_sys_t *p_ogg = p_demux->p_sys  ;
445     int i_read = 0;
446     char *p_buffer;
447
448     while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
449     {
450         p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
451
452         i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
453         if( i_read <= 0 )
454             return VLC_EGENERIC;
455
456         ogg_sync_wrote( &p_ogg->oy, i_read );
457     }
458
459     return VLC_SUCCESS;
460 }
461
462 /****************************************************************************
463  * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
464  *                current stream.
465  ****************************************************************************/
466 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
467                            ogg_packet *p_oggpacket )
468 {
469     /* Convert the granulepos into a pcr */
470     if( p_oggpacket->granulepos >= 0 )
471     {
472         if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
473             p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
474         {
475             ogg_int64_t iframe = p_oggpacket->granulepos >>
476               p_stream->i_granule_shift;
477             ogg_int64_t pframe = p_oggpacket->granulepos -
478               ( iframe << p_stream->i_granule_shift );
479
480             p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
481                               / p_stream->f_rate;
482         }
483         else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
484         {
485             ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
486             /* NB, OggDirac granulepos values are in units of 2*picturerate */
487             p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
488         }
489         else
490         {
491             p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
492                               / p_stream->f_rate;
493         }
494
495         p_stream->i_interpolated_pcr = p_stream->i_pcr;
496     }
497     else
498     {
499         p_stream->i_pcr = -1;
500
501         /* no granulepos available, try to interpolate the pcr.
502          * If we can't then don't touch the old value. */
503         if( p_stream->fmt.i_cat == VIDEO_ES )
504             /* 1 frame per packet */
505             p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
506         else if( p_stream->fmt.i_bitrate )
507             p_stream->i_interpolated_pcr +=
508                 ( p_oggpacket->bytes * INT64_C(1000000) /
509                   p_stream->fmt.i_bitrate / 8 );
510     }
511 }
512
513 /****************************************************************************
514  * Ogg_DecodePacket: Decode an Ogg packet.
515  ****************************************************************************/
516 static void Ogg_DecodePacket( demux_t *p_demux,
517                               logical_stream_t *p_stream,
518                               ogg_packet *p_oggpacket )
519 {
520     block_t *p_block;
521     bool b_selected;
522     int i_header_len = 0;
523     mtime_t i_pts = -1, i_interpolated_pts;
524
525     /* Sanity check */
526     if( !p_oggpacket->bytes )
527     {
528         msg_Dbg( p_demux, "discarding 0 sized packet" );
529         return;
530     }
531
532     if( p_oggpacket->bytes >= 7 &&
533         ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
534     {
535         /* it's an Annodex packet -- skip it (do nothing) */
536         return;
537     }
538     else if( p_oggpacket->bytes >= 7 &&
539         ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
540     {
541         /* it's an AnxData packet -- skip it (do nothing) */
542         return;
543     }
544
545     if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
546         p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
547
548     /* Check the ES is selected */
549     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
550                     p_stream->p_es, &b_selected );
551
552     if( p_stream->b_force_backup )
553     {
554         uint8_t *p_sav;
555         bool b_store_size = true;
556         bool b_store_num_headers = false;
557
558         p_stream->i_packets_backup++;
559         switch( p_stream->fmt.i_codec )
560         {
561         case VLC_FOURCC( 'v','o','r','b' ):
562         case VLC_FOURCC( 's','p','x',' ' ):
563         case VLC_FOURCC( 't','h','e','o' ):
564             if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
565             break;
566
567         case VLC_FOURCC( 'f','l','a','c' ):
568             if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
569             {
570                 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
571                 p_stream->b_force_backup = 0;
572             }
573             else if( p_stream->fmt.audio.i_rate )
574             {
575                 p_stream->b_force_backup = 0;
576                 if( p_oggpacket->bytes >= 9 )
577                 {
578                     p_oggpacket->packet += 9;
579                     p_oggpacket->bytes -= 9;
580                 }
581             }
582             b_store_size = false;
583             break;
584
585         case VLC_FOURCC( 'k','a','t','e' ):
586             if( p_stream->i_packets_backup == 1)
587                 b_store_num_headers = true;
588             if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
589             break;
590
591         default:
592             p_stream->b_force_backup = 0;
593             break;
594         }
595
596         /* Backup the ogg packet (likely an header packet) */
597         p_stream->p_headers =
598             realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
599                      p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
600         if( p_stream->p_headers )
601         {
602             uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
603
604             if( b_store_num_headers )
605             {
606                 /* Kate streams store the number of headers in the first header,
607                    so we can't just test for 3 as Vorbis/Theora */
608                 *(p_extra++) = p_stream->i_kate_num_headers;
609             }
610             if( b_store_size )
611             {
612                 *(p_extra++) = p_oggpacket->bytes >> 8;
613                 *(p_extra++) = p_oggpacket->bytes & 0xFF;
614             }
615             memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
616             p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
617
618             if( !p_stream->b_force_backup )
619             {
620                 /* Last header received, commit changes */
621                 free( p_stream->fmt.p_extra );
622
623                 p_stream->fmt.i_extra = p_stream->i_headers;
624                 p_stream->fmt.p_extra =
625                     realloc( p_stream->fmt.p_extra, p_stream->i_headers );
626                 if( p_stream->fmt.p_extra )
627                     memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
628                             p_stream->i_headers );
629                 else
630                     p_stream->fmt.i_extra = 0;
631
632                 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
633                     es_out_Control( p_demux->out, ES_OUT_SET_FMT,
634                                     p_stream->p_es, &p_stream->fmt );
635             }
636         }
637         else
638         {
639                 p_stream->p_headers = p_sav;
640         }
641
642         b_selected = false; /* Discard the header packet */
643     }
644
645     /* Convert the pcr into a pts */
646     if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
647         p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
648         p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
649     {
650         if( p_stream->i_pcr >= 0 )
651         {
652             /* This is for streams where the granulepos of the header packets
653              * doesn't match these of the data packets (eg. ogg web radios). */
654             if( p_stream->i_previous_pcr == 0 &&
655                 p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
656             {
657                 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
658
659                 /* Call the pace control */
660                 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
661                                 p_stream->i_pcr );
662             }
663
664             p_stream->i_previous_pcr = p_stream->i_pcr;
665
666             /* The granulepos is the end date of the sample */
667             i_pts =  p_stream->i_pcr;
668         }
669     }
670
671     /* Convert the granulepos into the next pcr */
672     i_interpolated_pts = p_stream->i_interpolated_pcr;
673     Ogg_UpdatePCR( p_stream, p_oggpacket );
674
675     if( p_stream->i_pcr >= 0 )
676     {
677         /* This is for streams where the granulepos of the header packets
678          * doesn't match these of the data packets (eg. ogg web radios). */
679         if( p_stream->i_previous_pcr == 0 &&
680             p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
681         {
682             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
683
684             /* Call the pace control */
685             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
686         }
687     }
688
689     if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
690         p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
691         p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
692         p_stream->i_pcr >= 0 )
693     {
694         p_stream->i_previous_pcr = p_stream->i_pcr;
695
696         /* The granulepos is the start date of the sample */
697         i_pts = p_stream->i_pcr;
698     }
699
700     if( !b_selected )
701     {
702         /* This stream isn't currently selected so we don't need to decode it,
703          * but we did need to store its pcr as it might be selected later on */
704         return;
705     }
706
707     if( p_oggpacket->bytes <= 0 )
708         return;
709
710     if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
711
712     /* Normalize PTS */
713     if( i_pts == 0 ) i_pts = 1;
714     else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
715     else if( i_pts == -1 ) i_pts = 0;
716
717     if( p_stream->fmt.i_cat == AUDIO_ES )
718         p_block->i_dts = p_block->i_pts = i_pts;
719     else if( p_stream->fmt.i_cat == SPU_ES )
720     {
721         p_block->i_dts = p_block->i_pts = i_pts;
722         p_block->i_length = 0;
723     }
724     else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
725         p_block->i_dts = p_block->i_pts = i_pts;
726     else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
727     {
728         ogg_int64_t dts = p_oggpacket->granulepos >> 31;
729         ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
730
731         uint64_t u_pnum = dts + delay;
732
733         p_block->i_dts = p_stream->i_pcr;
734         p_block->i_pts = 0;
735         /* NB, OggDirac granulepos values are in units of 2*picturerate */
736         if( -1 != p_oggpacket->granulepos )
737             p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
738     }
739     else
740     {
741         p_block->i_dts = i_pts;
742         p_block->i_pts = 0;
743     }
744
745     if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
746         p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
747         p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
748         p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
749         p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
750         p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
751         p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
752         p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
753     {
754         /* We remove the header from the packet */
755         i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
756         i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
757
758         if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
759         {
760             /* But with subtitles we need to retrieve the duration first */
761             int i, lenbytes = 0;
762
763             if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
764             {
765                 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
766                 {
767                     lenbytes = lenbytes << 8;
768                     lenbytes += *(p_oggpacket->packet + i_header_len - i);
769                 }
770             }
771             if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
772                 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
773                   p_oggpacket->packet[i_header_len + 1] != 0 &&
774                   p_oggpacket->packet[i_header_len + 1] != '\n' &&
775                   p_oggpacket->packet[i_header_len + 1] != '\r' ) )
776             {
777                 p_block->i_length = (mtime_t)lenbytes * 1000;
778             }
779         }
780
781         i_header_len++;
782         if( p_block->i_buffer >= (unsigned int)i_header_len )
783             p_block->i_buffer -= i_header_len;
784         else
785             p_block->i_buffer = 0;
786     }
787
788     if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
789     {
790         /* FIXME: the biggest hack I've ever done */
791         msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
792                   p_block->i_pts, p_block->i_dts );
793         msleep(10000);
794     }
795
796     memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
797             p_oggpacket->bytes - i_header_len );
798
799     es_out_Send( p_demux->out, p_stream->p_es, p_block );
800 }
801
802 /****************************************************************************
803  * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
804  *                         stream and fill p_ogg.
805  *****************************************************************************
806  * The initial page of a logical stream is marked as a 'bos' page.
807  * Furthermore, the Ogg specification mandates that grouped bitstreams begin
808  * together and all of the initial pages must appear before any data pages.
809  *
810  * On success this function returns VLC_SUCCESS.
811  ****************************************************************************/
812 static int Ogg_FindLogicalStreams( demux_t *p_demux )
813 {
814     demux_sys_t *p_ogg = p_demux->p_sys  ;
815     ogg_packet oggpacket;
816     ogg_page oggpage;
817     int i_stream;
818
819 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
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
832                 p_ogg->i_streams++;
833                 p_ogg->pp_stream =
834                     realloc( p_ogg->pp_stream, p_ogg->i_streams *
835                              sizeof(logical_stream_t *) );
836                 if( !p_ogg->pp_stream )
837                 {
838                     p_ogg->pp_stream = pp_sav;
839                     p_ogg->i_streams--;
840                     return VLC_ENOMEM;
841                 }
842
843                 p_stream = malloc( sizeof(logical_stream_t) );
844                 if( !p_stream )
845                     return VLC_ENOMEM;
846                 memset( p_stream, 0, sizeof(logical_stream_t) );
847                 p_stream->p_headers = 0;
848                 p_stream->secondary_header_packets = 0;
849
850                 es_format_Init( &p_stream->fmt, 0, 0 );
851                 es_format_Init( &p_stream->fmt_old, 0, 0 );
852
853                 /* Setup the logical stream */
854                 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
855                 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
856
857                 /* Extract the initial header from the first page and verify
858                  * the codec type of tis Ogg bitstream */
859                 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
860                 {
861                     /* error. stream version mismatch perhaps */
862                     msg_Err( p_demux, "error reading first page of "
863                              "Ogg bitstream data" );
864                     return VLC_EGENERIC;
865                 }
866
867                 /* FIXME: check return value */
868                 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
869
870                 /* Check for Vorbis header */
871                 if( oggpacket.bytes >= 7 &&
872                     ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
873                 {
874                     Ogg_ReadVorbisHeader( p_stream, &oggpacket );
875                     msg_Dbg( p_demux, "found vorbis header" );
876                 }
877                 /* Check for Speex header */
878                 else if( oggpacket.bytes >= 5 &&
879                     ! memcmp( oggpacket.packet, "Speex", 5 ) )
880                 {
881                     Ogg_ReadSpeexHeader( p_stream, &oggpacket );
882                     msg_Dbg( p_demux, "found speex header, channels: %i, "
883                              "rate: %i,  bitrate: %i",
884                              p_stream->fmt.audio.i_channels,
885                              (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
886                 }
887                 /* Check for Flac header (< version 1.1.1) */
888                 else if( oggpacket.bytes >= 4 &&
889                     ! memcmp( oggpacket.packet, "fLaC", 4 ) )
890                 {
891                     msg_Dbg( p_demux, "found FLAC header" );
892
893                     /* Grrrr!!!! Did they really have to put all the
894                      * important info in the second header packet!!!
895                      * (STREAMINFO metadata is in the following packet) */
896                     p_stream->b_force_backup = 1;
897
898                     p_stream->fmt.i_cat = AUDIO_ES;
899                     p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
900                 }
901                 /* Check for Flac header (>= version 1.1.1) */
902                 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
903                     ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
904                     ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
905                 {
906                     int i_packets = ((int)oggpacket.packet[7]) << 8 |
907                         oggpacket.packet[8];
908                     msg_Dbg( p_demux, "found FLAC header version %i.%i "
909                              "(%i header packets)",
910                              oggpacket.packet[5], oggpacket.packet[6],
911                              i_packets );
912
913                     p_stream->b_force_backup = 1;
914
915                     p_stream->fmt.i_cat = AUDIO_ES;
916                     p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
917                     oggpacket.packet += 13; oggpacket.bytes -= 13;
918                     Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
919                 }
920                 /* Check for Theora header */
921                 else if( oggpacket.bytes >= 7 &&
922                          ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
923                 {
924                     Ogg_ReadTheoraHeader( p_stream, &oggpacket );
925
926                     msg_Dbg( p_demux,
927                              "found theora header, bitrate: %i, rate: %f",
928                              p_stream->fmt.i_bitrate, p_stream->f_rate );
929                 }
930                 /* Check for Dirac header */
931                 else if( oggpacket.bytes >= 5 &&
932                          ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
933                 {
934                     Ogg_ReadDiracHeader( p_stream, &oggpacket );
935                     msg_Dbg( p_demux, "found dirac header" );
936                 }
937                 /* Check for Tarkin header */
938                 else if( oggpacket.bytes >= 7 &&
939                          ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
940                 {
941                     oggpack_buffer opb;
942
943                     msg_Dbg( p_demux, "found tarkin header" );
944                     p_stream->fmt.i_cat = VIDEO_ES;
945                     p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
946
947                     /* Cheat and get additionnal info ;) */
948                     oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
949                     oggpack_adv( &opb, 88 );
950                     oggpack_adv( &opb, 104 );
951                     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
952                     p_stream->f_rate = 2; /* FIXME */
953                     msg_Dbg( p_demux,
954                              "found tarkin header, bitrate: %i, rate: %f",
955                              p_stream->fmt.i_bitrate, p_stream->f_rate );
956                 }
957                 /* Check for Annodex header */
958                 else if( oggpacket.bytes >= 7 &&
959                          ! memcmp( oggpacket.packet, "Annodex", 7 ) )
960                 {
961                     Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
962                                            &oggpacket );
963                     /* kill annodex track */
964                     free( p_stream );
965                     p_ogg->i_streams--;
966                 }
967                 /* Check for Annodex header */
968                 else if( oggpacket.bytes >= 7 &&
969                          ! memcmp( oggpacket.packet, "AnxData", 7 ) )
970                 {
971                     Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
972                                            &oggpacket );
973                 }
974                 /* Check for Kate header */
975                 else if( oggpacket.bytes >= 8 &&
976                     ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
977                 {
978                     Ogg_ReadKateHeader( p_stream, &oggpacket );
979                     msg_Dbg( p_demux, "found kate header" );
980                 }
981                 else if( oggpacket.bytes >= 142 &&
982                          !memcmp( &oggpacket.packet[1],
983                                    "Direct Show Samples embedded in Ogg", 35 ))
984                 {
985                     /* Old header type */
986
987                     /* Check for video header (old format) */
988                     if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
989                         oggpacket.bytes >= 184 )
990                     {
991                         p_stream->fmt.i_cat = VIDEO_ES;
992                         p_stream->fmt.i_codec =
993                             VLC_FOURCC( oggpacket.packet[68],
994                                         oggpacket.packet[69],
995                                         oggpacket.packet[70],
996                                         oggpacket.packet[71] );
997                         msg_Dbg( p_demux, "found video header of type: %.4s",
998                                  (char *)&p_stream->fmt.i_codec );
999
1000                         p_stream->fmt.video.i_frame_rate = 10000000;
1001                         p_stream->fmt.video.i_frame_rate_base =
1002                             GetQWLE((oggpacket.packet+164));
1003                         p_stream->f_rate = 10000000.0 /
1004                             GetQWLE((oggpacket.packet+164));
1005                         p_stream->fmt.video.i_bits_per_pixel =
1006                             GetWLE((oggpacket.packet+182));
1007                         if( !p_stream->fmt.video.i_bits_per_pixel )
1008                             /* hack, FIXME */
1009                             p_stream->fmt.video.i_bits_per_pixel = 24;
1010                         p_stream->fmt.video.i_width =
1011                             GetDWLE((oggpacket.packet+176));
1012                         p_stream->fmt.video.i_height =
1013                             GetDWLE((oggpacket.packet+180));
1014
1015                         msg_Dbg( p_demux,
1016                                  "fps: %f, width:%i; height:%i, bitcount:%i",
1017                                  p_stream->f_rate,
1018                                  p_stream->fmt.video.i_width,
1019                                  p_stream->fmt.video.i_height,
1020                                  p_stream->fmt.video.i_bits_per_pixel);
1021
1022                     }
1023                     /* Check for audio header (old format) */
1024                     else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1025                     {
1026                         unsigned int i_extra_size;
1027                         unsigned int i_format_tag;
1028
1029                         p_stream->fmt.i_cat = AUDIO_ES;
1030
1031                         i_extra_size = GetWLE((oggpacket.packet+140));
1032                         if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1033                         {
1034                             p_stream->fmt.i_extra = i_extra_size;
1035                             p_stream->fmt.p_extra = malloc( i_extra_size );
1036                             if( p_stream->fmt.p_extra )
1037                                 memcpy( p_stream->fmt.p_extra,
1038                                         oggpacket.packet + 142, i_extra_size );
1039                             else
1040                                 p_stream->fmt.i_extra = 0;
1041                         }
1042
1043                         i_format_tag = GetWLE((oggpacket.packet+124));
1044                         p_stream->fmt.audio.i_channels =
1045                             GetWLE((oggpacket.packet+126));
1046                         p_stream->f_rate = p_stream->fmt.audio.i_rate =
1047                             GetDWLE((oggpacket.packet+128));
1048                         p_stream->fmt.i_bitrate =
1049                             GetDWLE((oggpacket.packet+132)) * 8;
1050                         p_stream->fmt.audio.i_blockalign =
1051                             GetWLE((oggpacket.packet+136));
1052                         p_stream->fmt.audio.i_bitspersample =
1053                             GetWLE((oggpacket.packet+138));
1054
1055                         wf_tag_to_fourcc( i_format_tag,
1056                                           &p_stream->fmt.i_codec, 0 );
1057
1058                         if( p_stream->fmt.i_codec ==
1059                             VLC_FOURCC('u','n','d','f') )
1060                         {
1061                             p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1062                                 ( i_format_tag >> 8 ) & 0xff,
1063                                 i_format_tag & 0xff );
1064                         }
1065
1066                         msg_Dbg( p_demux, "found audio header of type: %.4s",
1067                                  (char *)&p_stream->fmt.i_codec );
1068                         msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1069                                  "%dbits/sample %dkb/s",
1070                                  i_format_tag,
1071                                  p_stream->fmt.audio.i_channels,
1072                                  p_stream->fmt.audio.i_rate,
1073                                  p_stream->fmt.audio.i_bitspersample,
1074                                  p_stream->fmt.i_bitrate / 1024 );
1075
1076                     }
1077                     else
1078                     {
1079                         msg_Dbg( p_demux, "stream %d has an old header "
1080                             "but is of an unknown type", p_ogg->i_streams-1 );
1081                         free( p_stream );
1082                         p_ogg->i_streams--;
1083                     }
1084                 }
1085                 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
1086                          == PACKET_TYPE_HEADER &&
1087                          oggpacket.bytes >= (int)sizeof(stream_header)+1 )
1088                 {
1089                     stream_header *st = (stream_header *)(oggpacket.packet+1);
1090
1091                     /* Check for video header (new format) */
1092                     if( !strncmp( st->streamtype, "video", 5 ) )
1093                     {
1094                         p_stream->fmt.i_cat = VIDEO_ES;
1095
1096                         /* We need to get rid of the header packet */
1097                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1098
1099                         p_stream->fmt.i_codec =
1100                             VLC_FOURCC( st->subtype[0], st->subtype[1],
1101                                         st->subtype[2], st->subtype[3] );
1102                         msg_Dbg( p_demux, "found video header of type: %.4s",
1103                                  (char *)&p_stream->fmt.i_codec );
1104
1105                         p_stream->fmt.video.i_frame_rate = 10000000;
1106                         p_stream->fmt.video.i_frame_rate_base =
1107                             GetQWLE(&st->time_unit);
1108                         p_stream->f_rate = 10000000.0 /
1109                             GetQWLE(&st->time_unit);
1110                         p_stream->fmt.video.i_bits_per_pixel =
1111                             GetWLE(&st->bits_per_sample);
1112                         p_stream->fmt.video.i_width =
1113                             GetDWLE(&st->sh.video.width);
1114                         p_stream->fmt.video.i_height =
1115                             GetDWLE(&st->sh.video.height);
1116
1117                         msg_Dbg( p_demux,
1118                                  "fps: %f, width:%i; height:%i, bitcount:%i",
1119                                  p_stream->f_rate,
1120                                  p_stream->fmt.video.i_width,
1121                                  p_stream->fmt.video.i_height,
1122                                  p_stream->fmt.video.i_bits_per_pixel );
1123                     }
1124                     /* Check for audio header (new format) */
1125                     else if( !strncmp( st->streamtype, "audio", 5 ) )
1126                     {
1127                         char p_buffer[5];
1128                         int i_format_tag;
1129
1130                         p_stream->fmt.i_cat = AUDIO_ES;
1131
1132                         /* We need to get rid of the header packet */
1133                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1134
1135                         p_stream->fmt.i_extra = GetQWLE(&st->size) - sizeof(stream_header);
1136                         if( p_stream->fmt.i_extra > 0 &&
1137                             p_stream->fmt.i_extra < oggpacket.bytes - 1 - sizeof(stream_header) )
1138                         {
1139                             p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1140                             if( p_stream->fmt.p_extra )
1141                                 memcpy( p_stream->fmt.p_extra, st + 1,
1142                                         p_stream->fmt.i_extra );
1143                             else
1144                                 p_stream->fmt.i_extra = 0;
1145                         }
1146
1147                         memcpy( p_buffer, st->subtype, 4 );
1148                         p_buffer[4] = '\0';
1149                         i_format_tag = strtol(p_buffer,NULL,16);
1150                         p_stream->fmt.audio.i_channels =
1151                             GetWLE(&st->sh.audio.channels);
1152                         p_stream->f_rate = p_stream->fmt.audio.i_rate =
1153                             GetQWLE(&st->samples_per_unit);
1154                         p_stream->fmt.i_bitrate =
1155                             GetDWLE(&st->sh.audio.avgbytespersec) * 8;
1156                         p_stream->fmt.audio.i_blockalign =
1157                             GetWLE(&st->sh.audio.blockalign);
1158                         p_stream->fmt.audio.i_bitspersample =
1159                             GetWLE(&st->bits_per_sample);
1160
1161                         wf_tag_to_fourcc( i_format_tag,
1162                                           &p_stream->fmt.i_codec, 0 );
1163
1164                         if( p_stream->fmt.i_codec ==
1165                             VLC_FOURCC('u','n','d','f') )
1166                         {
1167                             p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1168                                 ( i_format_tag >> 8 ) & 0xff,
1169                                 i_format_tag & 0xff );
1170                         }
1171
1172                         msg_Dbg( p_demux, "found audio header of type: %.4s",
1173                                  (char *)&p_stream->fmt.i_codec );
1174                         msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1175                                  "%dbits/sample %dkb/s",
1176                                  i_format_tag,
1177                                  p_stream->fmt.audio.i_channels,
1178                                  p_stream->fmt.audio.i_rate,
1179                                  p_stream->fmt.audio.i_bitspersample,
1180                                  p_stream->fmt.i_bitrate / 1024 );
1181                     }
1182                     /* Check for text (subtitles) header */
1183                     else if( !strncmp(st->streamtype, "text", 4) )
1184                     {
1185                         /* We need to get rid of the header packet */
1186                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1187
1188                         msg_Dbg( p_demux, "found text subtitles header" );
1189                         p_stream->fmt.i_cat = SPU_ES;
1190                         p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1191                         p_stream->f_rate = 1000; /* granulepos is in milisec */
1192                     }
1193                     else
1194                     {
1195                         msg_Dbg( p_demux, "stream %d has a header marker "
1196                             "but is of an unknown type", p_ogg->i_streams-1 );
1197                         free( p_stream );
1198                         p_ogg->i_streams--;
1199                     }
1200                 }
1201                 else if( oggpacket.bytes >= 7 &&
1202                              ! memcmp( oggpacket.packet, "fishead", 7 ) )
1203
1204                 {
1205                     /* Skeleton */
1206                     msg_Dbg( p_demux, "stream %d is a skeleton",
1207                                 p_ogg->i_streams-1 );
1208                     /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1209                 }
1210                 else
1211                 {
1212                     msg_Dbg( p_demux, "stream %d is of unknown type",
1213                              p_ogg->i_streams-1 );
1214                     free( p_stream );
1215                     p_ogg->i_streams--;
1216                 }
1217
1218                 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1219                     return VLC_EGENERIC;
1220             }
1221
1222             /* This is the first data page, which means we are now finished
1223              * with the initial pages. We just need to store it in the relevant
1224              * bitstream. */
1225             for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1226             {
1227                 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1228                                        &oggpage ) == 0 )
1229                 {
1230                     break;
1231                 }
1232             }
1233
1234             return VLC_SUCCESS;
1235         }
1236     }
1237 #undef p_stream
1238
1239     return VLC_EGENERIC;
1240 }
1241
1242 /****************************************************************************
1243  * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1244  *                        Elementary streams.
1245  ****************************************************************************/
1246 static int Ogg_BeginningOfStream( demux_t *p_demux )
1247 {
1248     demux_sys_t *p_ogg = p_demux->p_sys  ;
1249     logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1250     int i_stream;
1251
1252     /* Find the logical streams embedded in the physical stream and
1253      * initialize our p_ogg structure. */
1254     if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1255     {
1256         msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1257         return VLC_EGENERIC;
1258     }
1259
1260     p_ogg->i_bitrate = 0;
1261
1262     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1263     {
1264         logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1265
1266         p_stream->p_es = NULL;
1267
1268         /* Try first to reuse an old ES */
1269         if( p_old_stream &&
1270             p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1271             p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1272         {
1273             msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1274
1275             p_stream->p_es = p_old_stream->p_es;
1276             es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1277
1278             p_old_stream->p_es = NULL;
1279             p_old_stream = NULL;
1280         }
1281
1282         if( !p_stream->p_es )
1283             p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1284
1285         // TODO: something to do here ?
1286         if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1287         {
1288             /* Set the CMML stream active */
1289             es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1290         }
1291
1292         p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1293
1294         p_stream->i_pcr = p_stream->i_previous_pcr =
1295             p_stream->i_interpolated_pcr = -1;
1296         p_stream->b_reinit = 0;
1297     }
1298
1299     if( p_ogg->p_old_stream )
1300     {
1301         if( p_ogg->p_old_stream->p_es )
1302             msg_Dbg( p_demux, "old stream not reused" );
1303         Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1304         p_ogg->p_old_stream = NULL;
1305     }
1306     return VLC_SUCCESS;
1307 }
1308
1309 /****************************************************************************
1310  * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1311  ****************************************************************************/
1312 static void Ogg_EndOfStream( demux_t *p_demux )
1313 {
1314     demux_sys_t *p_ogg = p_demux->p_sys  ;
1315     int i_stream;
1316
1317     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1318         Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1319     free( p_ogg->pp_stream );
1320
1321     /* Reinit p_ogg */
1322     p_ogg->i_bitrate = 0;
1323     p_ogg->i_streams = 0;
1324     p_ogg->pp_stream = NULL;
1325 }
1326
1327 /**
1328  * This function delete and release all data associated to a logical_stream_t
1329  */
1330 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1331 {
1332     if( p_stream->p_es )
1333         es_out_Del( p_demux->out, p_stream->p_es );
1334
1335     ogg_stream_clear( &p_stream->os );
1336     free( p_stream->p_headers );
1337
1338     es_format_Clean( &p_stream->fmt_old );
1339     es_format_Clean( &p_stream->fmt );
1340
1341     free( p_stream );
1342 }
1343 /**
1344  * This function check if a we need to reset a decoder in case we are
1345  * reusing an old ES
1346  */
1347 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1348 {
1349     int i_new = 0;
1350     int i_old = 0;
1351     int i;
1352
1353     for( i = 0; i < 3; i++ )
1354     {
1355         const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1356         const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1357
1358         if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1359             return false;
1360
1361         const int i_new_size = GetWBE( &p_new_extra[0] );
1362         const int i_old_size = GetWBE( &p_old_extra[0] );
1363
1364         if( i != 1 ) /* Ignore vorbis comment */
1365         {
1366             if( i_new_size != i_old_size )
1367                 return false;
1368             if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1369                 return false;
1370         }
1371
1372         i_new += 2 + i_new_size;
1373         i_old += 2 + i_old_size;
1374     }
1375     return true;
1376 }
1377 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1378 {
1379     bool b_compatible;
1380     if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1381         return true;
1382
1383     /* Only vorbis is supported */
1384     if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) )
1385         b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1386
1387     if( !b_compatible )
1388         msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1389
1390     return !b_compatible;
1391 }
1392
1393 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1394                                   ogg_packet *p_oggpacket )
1395 {
1396     bs_t bitstream;
1397     int i_fps_numerator;
1398     int i_fps_denominator;
1399     int i_keyframe_frequency_force;
1400
1401     p_stream->fmt.i_cat = VIDEO_ES;
1402     p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1403
1404     /* Signal that we want to keep a backup of the theora
1405      * stream headers. They will be used when switching between
1406      * audio streams. */
1407     p_stream->b_force_backup = 1;
1408
1409     /* Cheat and get additionnal info ;) */
1410     bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1411     bs_skip( &bitstream, 56 );
1412     bs_read( &bitstream, 8 ); /* major version num */
1413     bs_read( &bitstream, 8 ); /* minor version num */
1414     bs_read( &bitstream, 8 ); /* subminor version num */
1415     bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1416     bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1417     bs_read( &bitstream, 24 ); /* frame width */
1418     bs_read( &bitstream, 24 ); /* frame height */
1419     bs_read( &bitstream, 8 ); /* x offset */
1420     bs_read( &bitstream, 8 ); /* y offset */
1421
1422     i_fps_numerator = bs_read( &bitstream, 32 );
1423     i_fps_denominator = bs_read( &bitstream, 32 );
1424     bs_read( &bitstream, 24 ); /* aspect_numerator */
1425     bs_read( &bitstream, 24 ); /* aspect_denominator */
1426
1427     p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1428     p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1429
1430     bs_read( &bitstream, 8 ); /* colorspace */
1431     p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1432     bs_read( &bitstream, 6 ); /* quality */
1433
1434     i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1435
1436     /* granule_shift = i_log( frequency_force -1 ) */
1437     p_stream->i_granule_shift = 0;
1438     i_keyframe_frequency_force--;
1439     while( i_keyframe_frequency_force )
1440     {
1441         p_stream->i_granule_shift++;
1442         i_keyframe_frequency_force >>= 1;
1443     }
1444
1445     p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1446 }
1447
1448 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1449                                   ogg_packet *p_oggpacket )
1450 {
1451     oggpack_buffer opb;
1452
1453     p_stream->fmt.i_cat = AUDIO_ES;
1454     p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1455
1456     /* Signal that we want to keep a backup of the vorbis
1457      * stream headers. They will be used when switching between
1458      * audio streams. */
1459     p_stream->b_force_backup = 1;
1460
1461     /* Cheat and get additionnal info ;) */
1462     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1463     oggpack_adv( &opb, 88 );
1464     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1465     p_stream->f_rate = p_stream->fmt.audio.i_rate =
1466         oggpack_read( &opb, 32 );
1467     oggpack_adv( &opb, 32 );
1468     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1469 }
1470
1471 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1472                                  ogg_packet *p_oggpacket )
1473 {
1474     oggpack_buffer opb;
1475
1476     p_stream->fmt.i_cat = AUDIO_ES;
1477     p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1478
1479     /* Signal that we want to keep a backup of the speex
1480      * stream headers. They will be used when switching between
1481      * audio streams. */
1482     p_stream->b_force_backup = 1;
1483
1484     /* Cheat and get additionnal info ;) */
1485     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1486     oggpack_adv( &opb, 224 );
1487     oggpack_adv( &opb, 32 ); /* speex_version_id */
1488     oggpack_adv( &opb, 32 ); /* header_size */
1489     p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1490     oggpack_adv( &opb, 32 ); /* mode */
1491     oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1492     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1493     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1494 }
1495
1496 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1497                                 ogg_packet *p_oggpacket )
1498 {
1499     /* Parse the STREAMINFO metadata */
1500     bs_t s;
1501
1502     bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1503
1504     bs_read( &s, 1 );
1505     if( bs_read( &s, 7 ) == 0 )
1506     {
1507         if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1508         {
1509             bs_skip( &s, 80 );
1510             p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1511             p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1512
1513             msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1514                      p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1515         }
1516         else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1517
1518         /* Fake this as the last metadata block */
1519         *((uint8_t*)p_oggpacket->packet) |= 0x80;
1520     }
1521     else
1522     {
1523         /* This ain't a STREAMINFO metadata */
1524         msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1525     }
1526 }
1527
1528 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1529                                 ogg_packet *p_oggpacket )
1530 {
1531     oggpack_buffer opb;
1532     int32_t gnum;
1533     int32_t gden;
1534     int n;
1535
1536     p_stream->fmt.i_cat = SPU_ES;
1537     p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1538
1539     /* Signal that we want to keep a backup of the kate
1540      * stream headers. They will be used when switching between
1541      * kate streams. */
1542     p_stream->b_force_backup = 1;
1543
1544     /* Cheat and get additionnal info ;) */
1545     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1546     oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1547     p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1548     oggpack_adv( &opb, 3*8 );
1549     p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1550     oggpack_adv( &opb, 8*8 ); /* reserved */
1551     gnum = oggpack_read( &opb, 32 );
1552     gden = oggpack_read( &opb, 32 );
1553     p_stream->f_rate = (double)gnum/gden;
1554
1555     p_stream->fmt.psz_language = malloc(16);
1556     if (p_stream->fmt.psz_language)
1557     {
1558         for (n=0;n<16;++n)
1559             p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1560         p_stream->fmt.psz_language[15] = 0; /* just in case */
1561     }
1562     else
1563     {
1564         for (n=0;n<16;++n)
1565             oggpack_read(&opb,8);
1566     }
1567     p_stream->fmt.psz_description = malloc(16);
1568     if (p_stream->fmt.psz_description)
1569     {
1570         for (n=0;n<16;++n)
1571             p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1572         p_stream->fmt.psz_description[15] = 0; /* just in case */
1573     }
1574     else
1575     {
1576         for (n=0;n<16;++n)
1577             oggpack_read(&opb,8);
1578     }
1579 }
1580
1581 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1582                                    logical_stream_t *p_stream,
1583                                    ogg_packet *p_oggpacket )
1584 {
1585     if( p_oggpacket->bytes >= 28 &&
1586         !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1587     {
1588         oggpack_buffer opb;
1589
1590         uint16_t major_version;
1591         uint16_t minor_version;
1592         uint64_t timebase_numerator;
1593         uint64_t timebase_denominator;
1594
1595         Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1596
1597         oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1598         oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1599         major_version = oggpack_read( &opb, 2*8 ); /* major version */
1600         minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1601         timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1602         timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1603     }
1604     else if( p_oggpacket->bytes >= 42 &&
1605              !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1606     {
1607         uint64_t granule_rate_numerator;
1608         uint64_t granule_rate_denominator;
1609         char content_type_string[1024];
1610
1611         /* Read in Annodex header fields */
1612
1613         granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1614         granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1615         p_stream->secondary_header_packets =
1616             GetDWLE( &p_oggpacket->packet[24] );
1617
1618         /* we are guaranteed that the first header field will be
1619          * the content-type (by the Annodex standard) */
1620         content_type_string[0] = '\0';
1621         if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1622         {
1623             uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1624                                  p_oggpacket->bytes - 1 );
1625             if( p && p[0] == '\r' && p[1] == '\n' )
1626                 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1627                         content_type_string );
1628         }
1629
1630         msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1631                  granule_rate_numerator, granule_rate_denominator,
1632                  p_stream->secondary_header_packets, content_type_string );
1633
1634         p_stream->f_rate = (float) granule_rate_numerator /
1635             (float) granule_rate_denominator;
1636
1637         /* What type of file do we have?
1638          * strcmp is safe to use here because we've extracted
1639          * content_type_string from the stream manually */
1640         if( !strncmp(content_type_string, "audio/x-wav", 11) )
1641         {
1642             /* n.b. WAVs are unsupported right now */
1643             p_stream->fmt.i_cat = UNKNOWN_ES;
1644         }
1645         else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1646         {
1647             p_stream->fmt.i_cat = AUDIO_ES;
1648             p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1649
1650             p_stream->b_force_backup = 1;
1651         }
1652         else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1653         {
1654             p_stream->fmt.i_cat = AUDIO_ES;
1655             p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1656
1657             p_stream->b_force_backup = 1;
1658         }
1659         else if( !strncmp(content_type_string, "video/x-theora", 14) )
1660         {
1661             p_stream->fmt.i_cat = VIDEO_ES;
1662             p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1663
1664             p_stream->b_force_backup = 1;
1665         }
1666         else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1667         {
1668             p_stream->fmt.i_cat = VIDEO_ES;
1669             p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1670
1671             p_stream->b_force_backup = 1;
1672         }
1673         else if( !strncmp(content_type_string, "video/mpeg", 14) )
1674         {
1675             /* n.b. MPEG streams are unsupported right now */
1676             p_stream->fmt.i_cat = VIDEO_ES;
1677             p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1678         }
1679         else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1680         {
1681             ogg_stream_packetout( &p_stream->os, p_oggpacket );
1682             p_stream->fmt.i_cat = SPU_ES;
1683             p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );
1684         }
1685     }
1686 }
1687
1688 static uint32_t dirac_uint( bs_t *p_bs )
1689 {
1690   uint32_t count = 0, value = 0;
1691   while( !bs_read ( p_bs, 1 ) ) {
1692     count++;
1693     value <<= 1;
1694     value |= bs_read ( p_bs, 1 );
1695   }
1696
1697   return (1<<count) - 1 + value;
1698 }
1699
1700 static int dirac_bool( bs_t *p_bs )
1701 {
1702     return bs_read ( p_bs, 1 );
1703 }
1704
1705 static void Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1706                                  ogg_packet *p_oggpacket )
1707 {
1708     bs_t bs;
1709
1710     p_stream->fmt.i_cat = VIDEO_ES;
1711     p_stream->fmt.i_codec = VLC_FOURCC( 'd','r','a','c' );
1712     p_stream->i_granule_shift = 32;
1713
1714     /* Backing up stream headers is not required -- seqhdrs are repeated
1715      * thoughout the stream at suitable decoding start points */
1716     p_stream->b_force_backup = 0;
1717
1718     /* read in useful bits from sequence header */
1719     bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1720     bs_skip( &bs, 13*8); /* parse_info_header */
1721     dirac_uint( &bs ); /* major_version */
1722     dirac_uint( &bs ); /* minor_version */
1723     dirac_uint( &bs ); /* profile */
1724     dirac_uint( &bs ); /* level */
1725
1726     uint32_t u_video_format = dirac_uint( &bs ); /* index */
1727
1728     if (dirac_bool( &bs )) {
1729         dirac_uint( &bs ); /* frame_width */
1730         dirac_uint( &bs ); /* frame_height */
1731     }
1732
1733     if (dirac_bool( &bs )) {
1734         dirac_uint( &bs ); /* chroma_format */
1735     }
1736     if (dirac_bool( &bs )) {
1737         if (dirac_bool( &bs )) { /* interlaced */
1738             dirac_bool( &bs ); /* top_field_first */
1739         }
1740     }
1741
1742     static const struct {
1743         uint32_t u_n /* numerator */, u_d /* denominator */;
1744     } dirac_frate_tbl[] = { /* table 10.3 */
1745         {1,1}, /* this first value is never used */
1746         {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1747         {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1748     };
1749
1750     static const uint32_t dirac_vidfmt_frate[] = { /* table C.1 */
1751         1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1752     };
1753
1754     uint32_t u_n = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_n;
1755     uint32_t u_d = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_d;
1756     if (dirac_bool( &bs )) {
1757         uint32_t frame_rate_index = dirac_uint( &bs );
1758         u_n = dirac_frate_tbl[frame_rate_index].u_n;
1759         u_d = dirac_frate_tbl[frame_rate_index].u_d;
1760         if (frame_rate_index == 0) {
1761             u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1762             u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1763         }
1764     }
1765     p_stream->f_rate = (float) u_n / u_d;
1766 }