]> git.sesse.net Git - vlc/blob - modules/demux/ogg.c
Remove recursion into modules/demux/mkv/
[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 #include "xiph.h"
43 #include "vorbis.h"
44 #include "kate_categories.h"
45 #include "ogg.h"
46 #include "oggseek.h"
47
48 /*****************************************************************************
49  * Module descriptor
50  *****************************************************************************/
51 static int  Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
53
54 vlc_module_begin ()
55     set_shortname ( "OGG" )
56     set_description( N_("OGG demuxer" ) )
57     set_category( CAT_INPUT )
58     set_subcategory( SUBCAT_INPUT_DEMUX )
59     set_capability( "demux", 50 )
60     set_callbacks( Open, Close )
61     add_shortcut( "ogg" )
62 vlc_module_end ()
63
64
65 /*****************************************************************************
66  * Definitions of structures and functions used by this plugins
67  *****************************************************************************/
68
69 /* OggDS headers for the new header format (used in ogm files) */
70 typedef struct
71 {
72     ogg_int32_t width;
73     ogg_int32_t height;
74 } stream_header_video_t;
75
76 typedef struct
77 {
78     ogg_int16_t channels;
79     ogg_int16_t padding;
80     ogg_int16_t blockalign;
81     ogg_int32_t avgbytespersec;
82 } stream_header_audio_t;
83
84 typedef struct
85 {
86     char        streamtype[8];
87     char        subtype[4];
88
89     ogg_int32_t size;                               /* size of the structure */
90
91     ogg_int64_t time_unit;                              /* in reference time */
92     ogg_int64_t samples_per_unit;
93     ogg_int32_t default_len;                                /* in media time */
94
95     ogg_int32_t buffersize;
96     ogg_int16_t bits_per_sample;
97     ogg_int16_t padding;
98
99     union
100     {
101         /* Video specific */
102         stream_header_video_t video;
103         /* Audio specific */
104         stream_header_audio_t audio;
105     } sh;
106 } stream_header_t;
107
108
109 /* Some defines from OggDS */
110 #define PACKET_TYPE_HEADER   0x01
111 #define PACKET_TYPE_BITS     0x07
112 #define PACKET_LEN_BITS01    0xc0
113 #define PACKET_LEN_BITS2     0x02
114 #define PACKET_IS_SYNCPOINT  0x08
115
116 /*****************************************************************************
117  * Local prototypes
118  *****************************************************************************/
119 static int  Demux  ( demux_t * );
120 static int  Control( demux_t *, int, va_list );
121
122 /* Bitstream manipulation */
123 static int  Ogg_ReadPage     ( demux_t *, ogg_page * );
124 static void Ogg_UpdatePCR    ( logical_stream_t *, ogg_packet * );
125 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
126 static int  Ogg_OpusPacketDuration( logical_stream_t *, ogg_packet * );
127
128 static int Ogg_BeginningOfStream( demux_t *p_demux );
129 static int Ogg_FindLogicalStreams( demux_t *p_demux );
130 static void Ogg_EndOfStream( demux_t *p_demux );
131
132 /* */
133 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
134 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
135
136 /* */
137 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers );
138 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream, ogg_packet *p_oggpacket, double f_rate );
139
140 /* Logical bitstream headers */
141 static void Ogg_ReadTheoraHeader( demux_t *, logical_stream_t *, ogg_packet * );
142 static void Ogg_ReadVorbisHeader( demux_t *, logical_stream_t *, ogg_packet * );
143 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
144 static void Ogg_ReadOpusHeader( demux_t *, logical_stream_t *, ogg_packet * );
145 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
146 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
147 static void Ogg_ReadAnnodexHeader( demux_t *, logical_stream_t *, ogg_packet * );
148 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
149
150 /*****************************************************************************
151  * Open: initializes ogg demux structures
152  *****************************************************************************/
153 static int Open( vlc_object_t * p_this )
154 {
155     demux_t *p_demux = (demux_t *)p_this;
156     demux_sys_t    *p_sys;
157     const uint8_t  *p_peek;
158
159
160     /* Check if we are dealing with an ogg stream */
161     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
162     if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
163     {
164         return VLC_EGENERIC;
165     }
166
167     /* Set exported functions */
168     p_demux->pf_demux = Demux;
169     p_demux->pf_control = Control;
170     p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
171     if( !p_sys )
172         return VLC_ENOMEM;
173
174     memset( p_sys, 0, sizeof( demux_sys_t ) );
175     p_sys->i_bitrate = 0;
176     p_sys->pp_stream = NULL;
177     p_sys->p_old_stream = NULL;
178
179     /* Begnning of stream, tell the demux to look for elementary streams. */
180     p_sys->i_bos = 0;
181     p_sys->i_eos = 0;
182
183     p_sys->i_length = -1;
184
185     /* Initialize the Ogg physical bitstream parser */
186     ogg_sync_init( &p_sys->oy );
187     p_sys->b_page_waiting = false;
188
189     /* */
190     p_sys->p_meta = NULL;
191
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * Close: frees unused data
197  *****************************************************************************/
198 static void Close( vlc_object_t *p_this )
199 {
200     demux_t *p_demux = (demux_t *)p_this;
201     demux_sys_t *p_sys = p_demux->p_sys  ;
202
203     /* Cleanup the bitstream parser */
204     ogg_sync_clear( &p_sys->oy );
205
206     Ogg_EndOfStream( p_demux );
207
208     if( p_sys->p_old_stream )
209         Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
210
211     free( p_sys );
212 }
213
214 /*****************************************************************************
215  * Demux: reads and demuxes data packets
216  *****************************************************************************
217  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
218  *****************************************************************************/
219 static int Demux( demux_t * p_demux )
220 {
221     demux_sys_t *p_sys = p_demux->p_sys;
222     ogg_packet  oggpacket;
223     int         i_stream;
224     bool b_skipping = false;
225
226
227     if( p_sys->i_eos == p_sys->i_streams )
228     {
229         if( p_sys->i_eos )
230         {
231             msg_Dbg( p_demux, "end of a group of logical streams" );
232             /* We keep the ES to try reusing it in Ogg_BeginningOfStream
233              * only 1 ES is supported (common case for ogg web radio) */
234             if( p_sys->i_streams == 1 )
235             {
236                 p_sys->p_old_stream = p_sys->pp_stream[0];
237                 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
238             }
239             Ogg_EndOfStream( p_demux );
240         }
241
242         p_sys->i_eos = 0;
243         if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
244             return 0;
245
246         msg_Dbg( p_demux, "beginning of a group of logical streams" );
247         es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 );
248     }
249
250     /*
251      * The first data page of a physical stream is stored in the relevant logical stream
252      * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
253      * stream it belongs to if we haven't processed this first page yet. If we do, we
254      * will only process that first page whenever we find the second page for this stream.
255      * While this is fine for Vorbis and Theora, which are continuous codecs, which means
256      * the second page will arrive real quick, this is not fine for Kate, whose second
257      * data page will typically arrive much later.
258      * This means it is now possible to seek right at the start of a stream where the last
259      * logical stream is Kate, without having to wait for the second data page to unblock
260      * the first one, which is the one that triggers the 'no more headers to backup' code.
261      * And, as we all know, seeking without having backed up all headers is bad, since the
262      * codec will fail to initialize if it's missing its headers.
263      */
264     if( !p_sys->b_page_waiting)
265     {
266         /*
267          * Demux an ogg page from the stream
268          */
269         if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
270             return 0; /* EOF */
271
272         /* Test for End of Stream */
273         if( ogg_page_eos( &p_sys->current_page ) )
274             p_sys->i_eos++;
275     }
276
277
278     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
279     {
280         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
281
282         /* if we've just pulled page, look for the right logical stream */
283         if( !p_sys->b_page_waiting )
284         {
285             if( p_sys->i_streams == 1 &&
286                 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
287             {
288                 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
289                 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
290
291                 p_stream->b_reinit = true;
292                 p_stream->i_pcr = VLC_TS_0;
293                 p_stream->i_interpolated_pcr = VLC_TS_0;
294                 p_stream->i_previous_granulepos = -1;
295                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0);
296             }
297
298             if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
299             {
300                 continue;
301             }
302
303         }
304
305         while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
306         {
307             /* Read info from any secondary header packets, if there are any */
308             if( p_stream->i_secondary_header_packets > 0 )
309             {
310                 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
311                         oggpacket.bytes >= 7 &&
312                         ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
313                 {
314                     Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
315                     p_stream->i_secondary_header_packets = 0;
316                 }
317                 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
318                         oggpacket.bytes >= 7 &&
319                         ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
320                 {
321                     Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
322                     p_stream->i_secondary_header_packets = 0;
323                 }
324                 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
325                 {
326                     p_stream->i_secondary_header_packets = 0;
327                 }
328
329                 /* update start of data pointer */
330                 p_stream->i_data_start = stream_Tell( p_demux->s );
331
332             }
333
334             /* If any streams have i_skip_frames, only decode (pre-roll)
335              *  for those streams */
336             if ( b_skipping && p_stream->i_skip_frames == 0 ) continue;
337
338
339             if( p_stream->b_reinit )
340             {
341                 /* If synchro is re-initialized we need to drop all the packets
342                  * until we find a new dated one. */
343                 Ogg_UpdatePCR( p_stream, &oggpacket );
344
345                 if( p_stream->i_pcr >= 0 )
346                 {
347                     p_stream->b_reinit = false;
348                     /* For Opus, trash the first 80 ms of decoded output as
349                        well, to avoid blowing out speakers if we get unlucky.
350                        Opus predicts content from prior frames, which can go
351                        badly if we seek right where the stream goes from very
352                        quiet to very loud. It will converge after a bit. */
353                     if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
354                     {
355                         ogg_int64_t start_time;
356                         int duration;
357                         p_stream->i_skip_frames = 80*48;
358                         /* Make sure we never play audio from within the
359                            pre-skip at the beginning of the stream. */
360                         duration =
361                             Ogg_OpusPacketDuration( p_stream, &oggpacket );
362                         start_time = p_stream->i_previous_granulepos;
363                         if( duration > 0 )
364                         {
365                             start_time = start_time > duration ?
366                                 start_time - duration : 0;
367                         }
368                         if( p_stream->i_pre_skip > start_time )
369                         {
370                             p_stream->i_skip_frames +=
371                                 p_stream->i_pre_skip - start_time;
372                         }
373                     }
374                 }
375                 else
376                 {
377                     p_stream->i_interpolated_pcr = -1;
378                     p_stream->i_previous_granulepos = -1;
379                     continue;
380                 }
381
382                 /* An Ogg/vorbis packet contains an end date granulepos */
383                 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
384                     p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
385                     p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
386                     p_stream->fmt.i_codec == VLC_CODEC_FLAC )
387                 {
388                     if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
389                     {
390                         Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
391                     }
392                     else
393                     {
394                         es_out_Control( p_demux->out, ES_OUT_SET_PCR,
395                                         VLC_TS_0 + p_stream->i_pcr );
396                     }
397                     continue;
398                 }
399             }
400
401             Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
402         }
403
404         if( !p_sys->b_page_waiting )
405             break;
406     }
407
408     /* if a page was waiting, it's now processed */
409     p_sys->b_page_waiting = false;
410
411     p_sys->i_pcr = -1;
412     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
413     {
414         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
415
416         if( p_stream->fmt.i_cat == SPU_ES )
417             continue;
418         if( p_stream->i_interpolated_pcr < 0 )
419             continue;
420
421         if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
422             p_sys->i_pcr = p_stream->i_interpolated_pcr;
423     }
424
425     if( p_sys->i_pcr >= 0 && ! b_skipping )
426         es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
427
428     return 1;
429 }
430
431 /*****************************************************************************
432  * Control:
433  *****************************************************************************/
434 static int Control( demux_t *p_demux, int i_query, va_list args )
435 {
436     demux_sys_t *p_sys  = p_demux->p_sys;
437     vlc_meta_t *p_meta;
438     int64_t *pi64;
439     bool *pb_bool;
440     int i;
441
442     switch( i_query )
443     {
444         case DEMUX_GET_META:
445             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
446             if( p_sys->p_meta )
447                 vlc_meta_Merge( p_meta, p_sys->p_meta );
448             return VLC_SUCCESS;
449
450         case DEMUX_HAS_UNSUPPORTED_META:
451             pb_bool = (bool*)va_arg( args, bool* );
452             *pb_bool = true;
453             return VLC_SUCCESS;
454
455         case DEMUX_GET_TIME:
456             pi64 = (int64_t*)va_arg( args, int64_t * );
457             *pi64 = p_sys->i_pcr;
458             return VLC_SUCCESS;
459
460         case DEMUX_SET_TIME:
461             return VLC_EGENERIC;
462
463         case DEMUX_GET_ATTACHMENTS:
464         {
465             input_attachment_t ***ppp_attach =
466                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
467             int *pi_int = (int*)va_arg( args, int * );
468
469             if( p_sys->i_attachments <= 0 )
470                 return VLC_EGENERIC;
471
472             *pi_int = p_sys->i_attachments;
473             *ppp_attach = xmalloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
474             for( int i = 0; i < p_sys->i_attachments; i++ )
475                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
476             return VLC_SUCCESS;
477         }
478
479         case DEMUX_SET_POSITION:
480             /* forbid seeking if we haven't initialized all logical bitstreams yet;
481                if we allowed, some headers would not get backed up and decoder init
482                would fail, making that logical stream unusable */
483             if( p_sys->i_bos > 0 )
484             {
485                 return VLC_EGENERIC;
486             }
487
488             for( i = 0; i < p_sys->i_streams; i++ )
489             {
490                 logical_stream_t *p_stream = p_sys->pp_stream[i];
491
492                 /* we'll trash all the data until we find the next pcr */
493                 p_stream->b_reinit = true;
494                 p_stream->i_pcr = -1;
495                 p_stream->i_interpolated_pcr = -1;
496                 p_stream->i_previous_granulepos = -1;
497                 ogg_stream_reset( &p_stream->os );
498             }
499             ogg_sync_reset( &p_sys->oy );
500             return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
501                                           1, i_query, args );
502         case DEMUX_GET_LENGTH:
503             if ( p_sys->i_length < 0 )
504                 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
505                                               1, i_query, args );
506             pi64 = (int64_t*)va_arg( args, int64_t * );
507             *pi64 = p_sys->i_length * 1000000;
508             return VLC_SUCCESS;
509
510
511         default:
512             return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
513                                            1, i_query, args );
514     }
515 }
516
517 /****************************************************************************
518  * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
519  ****************************************************************************
520  * Returns VLC_SUCCESS if a page has been read. An error might happen if we
521  * are at the end of stream.
522  ****************************************************************************/
523 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
524 {
525     demux_sys_t *p_ogg = p_demux->p_sys  ;
526     int i_read = 0;
527     char *p_buffer;
528
529     while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
530     {
531         p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
532
533         i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
534         if( i_read <= 0 )
535             return VLC_EGENERIC;
536
537         ogg_sync_wrote( &p_ogg->oy, i_read );
538     }
539
540     return VLC_SUCCESS;
541 }
542
543 /****************************************************************************
544  * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
545  *                current stream.
546  ****************************************************************************/
547 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
548                            ogg_packet *p_oggpacket )
549 {
550     p_stream->i_end_trim = 0;
551     /* Convert the granulepos into a pcr */
552     if( p_oggpacket->granulepos >= 0 )
553     {
554         if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
555             p_stream->fmt.i_codec == VLC_CODEC_KATE )
556         {
557             ogg_int64_t iframe = p_oggpacket->granulepos >>
558               p_stream->i_granule_shift;
559             ogg_int64_t pframe = p_oggpacket->granulepos -
560               ( iframe << p_stream->i_granule_shift );
561
562             p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
563               * INT64_C(1000000) / p_stream->f_rate;
564         }
565         else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
566         {
567             ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
568             /* NB, OggDirac granulepos values are in units of 2*picturerate */
569             p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
570         }
571         else
572         {
573             ogg_int64_t sample;
574             sample = p_oggpacket->granulepos;
575             if( p_oggpacket->e_o_s &&
576                 p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
577                 p_stream->i_previous_granulepos >= 0 )
578             {
579                 int duration;
580                 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
581                 if( duration > 0 )
582                 {
583                     ogg_int64_t end_sample;
584                     end_sample = p_stream->i_previous_granulepos + duration;
585                     if( end_sample > sample )
586                         p_stream->i_end_trim = (int)(end_sample - sample);
587                 }
588             }
589             if (sample >= p_stream->i_pre_skip)
590                 sample -= p_stream->i_pre_skip;
591             else
592                 sample = 0;
593             p_stream->i_pcr = sample * INT64_C(1000000) / p_stream->f_rate;
594         }
595
596         p_stream->i_pcr += VLC_TS_0;
597         p_stream->i_interpolated_pcr = p_stream->i_pcr;
598     }
599     else
600     {
601         int duration;
602         p_stream->i_pcr = -1;
603
604         /* no granulepos available, try to interpolate the pcr.
605          * If we can't then don't touch the old value. */
606         if( p_stream->fmt.i_cat == VIDEO_ES )
607             /* 1 frame per packet */
608             p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
609         else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
610                  p_stream->i_previous_granulepos >= 0 &&
611                  ( duration =
612                      Ogg_OpusPacketDuration( p_stream, p_oggpacket ) ) > 0 )
613         {
614             ogg_int64_t sample;
615             p_oggpacket->granulepos =
616                 p_stream->i_previous_granulepos + duration;
617             sample = p_oggpacket->granulepos;
618             if (sample >= p_stream->i_pre_skip)
619                 sample -= p_stream->i_pre_skip;
620             else
621                 sample = 0;
622             p_stream->i_interpolated_pcr =
623                 VLC_TS_0 + sample * INT64_C(1000000) / p_stream->f_rate;
624         }
625         else if( p_stream->fmt.i_bitrate )
626         {
627             p_stream->i_interpolated_pcr +=
628                 ( p_oggpacket->bytes * INT64_C(1000000) /
629                   p_stream->fmt.i_bitrate / 8 );
630         }
631     }
632     p_stream->i_previous_granulepos = p_oggpacket->granulepos;
633 }
634
635 /****************************************************************************
636  * Ogg_DecodePacket: Decode an Ogg packet.
637  ****************************************************************************/
638 static void Ogg_DecodePacket( demux_t *p_demux,
639                               logical_stream_t *p_stream,
640                               ogg_packet *p_oggpacket )
641 {
642     block_t *p_block;
643     bool b_selected;
644     int i_header_len = 0;
645     mtime_t i_pts = -1, i_interpolated_pts;
646     demux_sys_t *p_ogg = p_demux->p_sys;
647
648     if( p_oggpacket->bytes >= 7 &&
649         ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
650     {
651         /* it's an Annodex packet -- skip it (do nothing) */
652         return;
653     }
654     else if( p_oggpacket->bytes >= 7 &&
655         ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
656     {
657         /* it's an AnxData packet -- skip it (do nothing) */
658         return;
659     }
660
661     if( p_stream->fmt.i_codec == VLC_CODEC_SUBT && p_oggpacket->bytes > 0 &&
662         p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
663
664     /* Check the ES is selected */
665     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
666                     p_stream->p_es, &b_selected );
667
668     if( p_stream->b_force_backup )
669     {
670         bool b_xiph;
671         p_stream->i_packets_backup++;
672         switch( p_stream->fmt.i_codec )
673         {
674         case VLC_CODEC_VORBIS:
675         case VLC_CODEC_SPEEX:
676         case VLC_CODEC_THEORA:
677             if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
678             b_xiph = true;
679             break;
680
681         case VLC_CODEC_OPUS:
682             if( p_stream->i_packets_backup == 2 ) p_stream->b_force_backup = 0;
683             b_xiph = true;
684             break;
685
686         case VLC_CODEC_FLAC:
687             if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
688             {
689                 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
690                 p_stream->b_force_backup = 0;
691             }
692             else if( p_stream->fmt.audio.i_rate )
693             {
694                 p_stream->b_force_backup = 0;
695                 if( p_oggpacket->bytes >= 9 )
696                 {
697                     p_oggpacket->packet += 9;
698                     p_oggpacket->bytes -= 9;
699                 }
700             }
701             b_xiph = false;
702             break;
703
704         case VLC_CODEC_KATE:
705             if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
706             b_xiph = true;
707             break;
708
709         default:
710             p_stream->b_force_backup = 0;
711             b_xiph = false;
712             break;
713         }
714
715         /* Backup the ogg packet (likely an header packet) */
716         if( !b_xiph )
717         {
718             void *p_org = p_stream->p_headers;
719             p_stream->i_headers += p_oggpacket->bytes;
720             p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
721             if( p_stream->p_headers )
722             {
723                 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
724                         p_oggpacket->packet, p_oggpacket->bytes );
725             }
726             else
727             {
728 #warning Memory leak
729                 p_stream->i_headers = 0;
730                 p_stream->p_headers = NULL;
731                 free( p_org );
732             }
733         }
734         else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
735                                      p_oggpacket->bytes, p_oggpacket->packet ) )
736         {
737             p_stream->i_headers = 0;
738             p_stream->p_headers = NULL;
739         }
740         if( p_stream->i_headers > 0 )
741         {
742             if( !p_stream->b_force_backup )
743             {
744                 /* Last header received, commit changes */
745                 free( p_stream->fmt.p_extra );
746
747                 p_stream->fmt.i_extra = p_stream->i_headers;
748                 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
749                 if( p_stream->fmt.p_extra )
750                     memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
751                             p_stream->i_headers );
752                 else
753                     p_stream->fmt.i_extra = 0;
754
755                 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
756                     es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
757                                     p_stream->p_es, &p_stream->fmt );
758
759                 if( p_stream->i_headers > 0 )
760                     Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
761                                      p_stream->p_headers, p_stream->i_headers );
762
763                 /* we're not at BOS anymore for this logical stream */
764                 p_ogg->i_bos--;
765             }
766         }
767
768         b_selected = false; /* Discard the header packet */
769     }
770
771     /* Convert the pcr into a pts */
772     if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
773         p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
774         p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
775         p_stream->fmt.i_codec == VLC_CODEC_FLAC )
776     {
777         if( p_stream->i_pcr >= 0 )
778         {
779             /* This is for streams where the granulepos of the header packets
780              * doesn't match these of the data packets (eg. ogg web radios). */
781             if( p_stream->i_previous_pcr == 0 &&
782                 p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
783             {
784
785                 /* Call the pace control */
786                 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
787                                 VLC_TS_0 + p_stream->i_pcr );
788             }
789
790             p_stream->i_previous_pcr = p_stream->i_pcr;
791
792             /* The granulepos is the end date of the sample */
793             i_pts = p_stream->i_pcr;
794         }
795     }
796
797     /* Convert the granulepos into the next pcr */
798     i_interpolated_pts = p_stream->i_interpolated_pcr;
799     Ogg_UpdatePCR( p_stream, p_oggpacket );
800
801     /* SPU streams are typically discontinuous, do not mind large gaps */
802     if( p_stream->fmt.i_cat != SPU_ES )
803     {
804         if( p_stream->i_pcr >= 0 )
805         {
806             /* This is for streams where the granulepos of the header packets
807              * doesn't match these of the data packets (eg. ogg web radios). */
808             if( p_stream->i_previous_pcr == 0 &&
809                 p_stream->i_pcr  > 3 * DEFAULT_PTS_DELAY )
810             {
811
812                 /* Call the pace control */
813                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_stream->i_pcr );
814             }
815         }
816     }
817
818     if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
819         p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
820         p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
821         p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
822         p_stream->i_pcr >= 0 )
823     {
824         p_stream->i_previous_pcr = p_stream->i_pcr;
825
826         /* The granulepos is the start date of the sample */
827         i_pts = p_stream->i_pcr;
828     }
829
830     if( !b_selected )
831     {
832         /* This stream isn't currently selected so we don't need to decode it,
833          * but we did need to store its pcr as it might be selected later on */
834         return;
835     }
836
837     if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
838
839
840     /* may need to preroll after a seek */
841     if ( p_stream->i_skip_frames > 0 )
842     {
843         if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
844         {
845             int duration;
846             duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
847             if( p_stream->i_skip_frames > duration )
848             {
849                 p_block->i_flags |= BLOCK_FLAG_PREROLL;
850                 p_block->i_nb_samples = 0;
851                 p_stream->i_skip_frames -= duration;
852             }
853             else
854             {
855                 p_block->i_nb_samples = duration - p_stream->i_skip_frames;
856                 if( p_stream->i_previous_granulepos >=
857                     p_block->i_nb_samples + p_stream->i_pre_skip )
858                 {
859                     i_pts = VLC_TS_0 + (p_stream->i_previous_granulepos
860                         - p_block->i_nb_samples - p_stream->i_pre_skip) *
861                         INT64_C(1000000) / p_stream->f_rate;
862                 }
863                 p_stream->i_skip_frames = 0;
864             }
865         }
866         else
867         {
868             p_block->i_flags |= BLOCK_FLAG_PREROLL;
869             p_stream->i_skip_frames--;
870         }
871     }
872     else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
873         p_block->i_nb_samples = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
874
875
876     /* Normalize PTS */
877     if( i_pts == VLC_TS_INVALID ) i_pts = VLC_TS_0;
878     else if( i_pts == -1 && i_interpolated_pts == VLC_TS_INVALID )
879         i_pts = VLC_TS_0;
880     else if( i_pts == -1 && p_stream->fmt.i_cat == VIDEO_ES )
881         i_pts = i_interpolated_pts;
882     else if( i_pts == -1 ) i_pts = VLC_TS_INVALID;
883
884     if( p_stream->fmt.i_cat == AUDIO_ES )
885     {
886         p_block->i_dts = p_block->i_pts = i_pts;
887         /* Blatant abuse of the i_length field. */
888         p_block->i_length = p_stream->i_end_trim;
889     }
890     else if( p_stream->fmt.i_cat == SPU_ES )
891     {
892         p_block->i_dts = p_block->i_pts = i_pts;
893         p_block->i_length = 0;
894     }
895     else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
896     {
897         p_block->i_dts = p_block->i_pts = i_pts;
898         if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
899         {
900             p_block->i_flags |= BLOCK_FLAG_TYPE_I;
901         }
902     }
903     else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
904     {
905         ogg_int64_t dts = p_oggpacket->granulepos >> 31;
906         ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
907
908         uint64_t u_pnum = dts + delay;
909
910         p_block->i_dts = p_stream->i_pcr;
911         p_block->i_pts = VLC_TS_INVALID;
912         /* NB, OggDirac granulepos values are in units of 2*picturerate */
913
914         /* granulepos for dirac is possibly broken, this value should be ignored */
915         if( -1 != p_oggpacket->granulepos )
916             p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
917     }
918     else
919     {
920         p_block->i_dts = i_pts;
921         p_block->i_pts = VLC_TS_INVALID;
922     }
923
924     if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
925         p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
926         p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
927         p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
928         p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
929         p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
930         p_stream->fmt.i_codec != VLC_CODEC_CMML &&
931         p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
932         p_stream->fmt.i_codec != VLC_CODEC_KATE )
933     {
934         if( p_oggpacket->bytes <= 0 )
935         {
936             msg_Dbg( p_demux, "discarding 0 sized packet" );
937             block_Release( p_block );
938             return;
939         }
940         /* We remove the header from the packet */
941         i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
942         i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
943
944         if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
945         {
946             /* But with subtitles we need to retrieve the duration first */
947             int i, lenbytes = 0;
948
949             if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
950             {
951                 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
952                 {
953                     lenbytes = lenbytes << 8;
954                     lenbytes += *(p_oggpacket->packet + i_header_len - i);
955                 }
956             }
957             if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
958                 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
959                   p_oggpacket->packet[i_header_len + 1] != 0 &&
960                   p_oggpacket->packet[i_header_len + 1] != '\n' &&
961                   p_oggpacket->packet[i_header_len + 1] != '\r' ) )
962             {
963                 p_block->i_length = (mtime_t)lenbytes * 1000;
964             }
965         }
966
967         i_header_len++;
968         if( p_block->i_buffer >= (unsigned int)i_header_len )
969             p_block->i_buffer -= i_header_len;
970         else
971             p_block->i_buffer = 0;
972     }
973
974     if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
975     {
976         /* FIXME: the biggest hack I've ever done */
977         msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
978                   p_block->i_pts, p_block->i_dts );
979         msleep(10000);
980     }
981
982     memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
983             p_oggpacket->bytes - i_header_len );
984
985     es_out_Send( p_demux->out, p_stream->p_es, p_block );
986 }
987
988 /* Re-implemented to avoid linking against libopus from the demuxer. */
989 static int Ogg_OpusPacketDuration( logical_stream_t *p_stream,
990                                    ogg_packet *p_oggpacket )
991 {
992     static const int silk_fs_div[4] = { 6000, 3000, 1500, 1000 };
993     int toc;
994     int nframes;
995     int frame_size;
996     int nsamples;
997     int i_rate;
998     if( p_oggpacket->bytes < 1 )
999         return VLC_EGENERIC;
1000     toc = p_oggpacket->packet[0];
1001     switch( toc&3 )
1002     {
1003         case 0:
1004             nframes = 1;
1005             break;
1006         case 1:
1007         case 2:
1008             nframes = 2;
1009             break;
1010         default:
1011             if( p_oggpacket->bytes < 2 )
1012                 return VLC_EGENERIC;
1013             nframes = p_oggpacket->packet[1]&0x3F;
1014             break;
1015     }
1016     i_rate = (int)p_stream->fmt.audio.i_rate;
1017     if( toc&0x80 )
1018         frame_size = (i_rate << (toc >> 3 & 3)) / 400;
1019     else if( ( toc&0x60 ) == 0x60 )
1020         frame_size = i_rate/(100 >> (toc >> 3 & 1));
1021     else
1022         frame_size = i_rate*60 / silk_fs_div[toc >> 3 & 3];
1023     nsamples = nframes*frame_size;
1024     if( nsamples*25 > i_rate*3 )
1025         return VLC_EGENERIC;
1026     return nsamples;
1027 }
1028
1029 /****************************************************************************
1030  * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
1031  *                         stream and fill p_ogg.
1032  *****************************************************************************
1033  * The initial page of a logical stream is marked as a 'bos' page.
1034  * Furthermore, the Ogg specification mandates that grouped bitstreams begin
1035  * together and all of the initial pages must appear before any data pages.
1036  *
1037  * On success this function returns VLC_SUCCESS.
1038  ****************************************************************************/
1039 static int Ogg_FindLogicalStreams( demux_t *p_demux )
1040 {
1041     demux_sys_t *p_ogg = p_demux->p_sys  ;
1042     ogg_packet oggpacket;
1043     int i_stream;
1044
1045     p_ogg->i_total_length = stream_Size ( p_demux->s );
1046     msg_Dbg( p_demux, "File length is %"PRId64" bytes", p_ogg->i_total_length );
1047
1048
1049     while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
1050     {
1051
1052         if( ogg_page_bos( &p_ogg->current_page ) )
1053         {
1054
1055             /* All is wonderful in our fine fine little world.
1056              * We found the beginning of our first logical stream. */
1057             while( ogg_page_bos( &p_ogg->current_page ) )
1058             {
1059                 logical_stream_t *p_stream;
1060
1061                 p_stream = malloc( sizeof(logical_stream_t) );
1062                 if( unlikely( !p_stream ) )
1063                     return VLC_ENOMEM;
1064
1065                 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
1066
1067                 memset( p_stream, 0, sizeof(logical_stream_t) );
1068                 p_stream->p_headers = 0;
1069                 p_stream->i_secondary_header_packets = 0;
1070
1071                 p_stream->i_keyframe_offset = 0;
1072                 p_stream->i_skip_frames = 0;
1073
1074                 p_stream->i_data_start = 0;
1075
1076                 es_format_Init( &p_stream->fmt, 0, 0 );
1077                 es_format_Init( &p_stream->fmt_old, 0, 0 );
1078
1079                 /* Setup the logical stream */
1080                 p_stream->i_serial_no = ogg_page_serialno( &p_ogg->current_page );
1081                 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
1082
1083                 /* Extract the initial header from the first page and verify
1084                  * the codec type of this Ogg bitstream */
1085                 if( ogg_stream_pagein( &p_stream->os, &p_ogg->current_page ) < 0 )
1086                 {
1087                     /* error. stream version mismatch perhaps */
1088                     msg_Err( p_demux, "error reading first page of "
1089                              "Ogg bitstream data" );
1090                     return VLC_EGENERIC;
1091                 }
1092
1093                 /* FIXME: check return value */
1094                 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
1095
1096                 /* Check for Vorbis header */
1097                 if( oggpacket.bytes >= 7 &&
1098                     ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
1099                 {
1100                     Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
1101                     msg_Dbg( p_demux, "found vorbis header" );
1102                 }
1103                 /* Check for Speex header */
1104                 else if( oggpacket.bytes >= 5 &&
1105                     ! memcmp( oggpacket.packet, "Speex", 5 ) )
1106                 {
1107                     Ogg_ReadSpeexHeader( p_stream, &oggpacket );
1108                     msg_Dbg( p_demux, "found speex header, channels: %i, "
1109                              "rate: %i,  bitrate: %i",
1110                              p_stream->fmt.audio.i_channels,
1111                              (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
1112                 }
1113                 /* Check for Opus header */
1114                 else if( oggpacket.bytes >= 8 &&
1115                     ! memcmp( oggpacket.packet, "OpusHead", 8 ) )
1116                 {
1117                     Ogg_ReadOpusHeader( p_demux, p_stream, &oggpacket );
1118                     msg_Dbg( p_demux, "found opus header, channels: %i, "
1119                              "pre-skip: %i",
1120                              p_stream->fmt.audio.i_channels,
1121                              (int)p_stream->i_pre_skip);
1122                     p_stream->i_skip_frames = p_stream->i_pre_skip;
1123                 }
1124                 /* Check for Flac header (< version 1.1.1) */
1125                 else if( oggpacket.bytes >= 4 &&
1126                     ! memcmp( oggpacket.packet, "fLaC", 4 ) )
1127                 {
1128                     msg_Dbg( p_demux, "found FLAC header" );
1129
1130                     /* Grrrr!!!! Did they really have to put all the
1131                      * important info in the second header packet!!!
1132                      * (STREAMINFO metadata is in the following packet) */
1133                     p_stream->b_force_backup = 1;
1134
1135                     p_stream->fmt.i_cat = AUDIO_ES;
1136                     p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1137                 }
1138                 /* Check for Flac header (>= version 1.1.1) */
1139                 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
1140                     ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
1141                     ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
1142                 {
1143                     int i_packets = ((int)oggpacket.packet[7]) << 8 |
1144                         oggpacket.packet[8];
1145                     msg_Dbg( p_demux, "found FLAC header version %i.%i "
1146                              "(%i header packets)",
1147                              oggpacket.packet[5], oggpacket.packet[6],
1148                              i_packets );
1149
1150                     p_stream->b_force_backup = 1;
1151
1152                     p_stream->fmt.i_cat = AUDIO_ES;
1153                     p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1154                     oggpacket.packet += 13; oggpacket.bytes -= 13;
1155                     Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
1156                 }
1157                 /* Check for Theora header */
1158                 else if( oggpacket.bytes >= 7 &&
1159                          ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
1160                 {
1161                     Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
1162
1163                     msg_Dbg( p_demux,
1164                              "found theora header, bitrate: %i, rate: %f",
1165                              p_stream->fmt.i_bitrate, p_stream->f_rate );
1166                 }
1167                 /* Check for Dirac header */
1168                 else if( ( oggpacket.bytes >= 5 &&
1169                            ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
1170                          ( oggpacket.bytes >= 9 &&
1171                            ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
1172                 {
1173                     if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1174                         msg_Dbg( p_demux, "found dirac header" );
1175                     else
1176                     {
1177                         msg_Warn( p_demux, "found dirac header isn't decodable" );
1178                         free( p_stream );
1179                         p_ogg->i_streams--;
1180                     }
1181                 }
1182                 /* Check for Tarkin header */
1183                 else if( oggpacket.bytes >= 7 &&
1184                          ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1185                 {
1186                     oggpack_buffer opb;
1187
1188                     msg_Dbg( p_demux, "found tarkin header" );
1189                     p_stream->fmt.i_cat = VIDEO_ES;
1190                     p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1191
1192                     /* Cheat and get additionnal info ;) */
1193                     oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1194                     oggpack_adv( &opb, 88 );
1195                     oggpack_adv( &opb, 104 );
1196                     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1197                     p_stream->f_rate = 2; /* FIXME */
1198                     msg_Dbg( p_demux,
1199                              "found tarkin header, bitrate: %i, rate: %f",
1200                              p_stream->fmt.i_bitrate, p_stream->f_rate );
1201                 }
1202                 /* Check for Annodex header */
1203                 else if( oggpacket.bytes >= 7 &&
1204                          ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1205                 {
1206                     Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1207                     /* kill annodex track */
1208                     free( p_stream );
1209                     p_ogg->i_streams--;
1210                 }
1211                 /* Check for Annodex header */
1212                 else if( oggpacket.bytes >= 7 &&
1213                          ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1214                 {
1215                     Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1216                 }
1217                 /* Check for Kate header */
1218                 else if( oggpacket.bytes >= 8 &&
1219                     ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1220                 {
1221                     Ogg_ReadKateHeader( p_stream, &oggpacket );
1222                     msg_Dbg( p_demux, "found kate header" );
1223                 }
1224                 else if( oggpacket.bytes >= 142 &&
1225                          !memcmp( &oggpacket.packet[1],
1226                                    "Direct Show Samples embedded in Ogg", 35 ))
1227                 {
1228                     /* Old header type */
1229
1230                     /* Check for video header (old format) */
1231                     if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1232                         oggpacket.bytes >= 184 )
1233                     {
1234                         p_stream->fmt.i_cat = VIDEO_ES;
1235                         p_stream->fmt.i_codec =
1236                             VLC_FOURCC( oggpacket.packet[68],
1237                                         oggpacket.packet[69],
1238                                         oggpacket.packet[70],
1239                                         oggpacket.packet[71] );
1240                         msg_Dbg( p_demux, "found video header of type: %.4s",
1241                                  (char *)&p_stream->fmt.i_codec );
1242
1243                         p_stream->fmt.video.i_frame_rate = 10000000;
1244                         p_stream->fmt.video.i_frame_rate_base =
1245                             GetQWLE((oggpacket.packet+164));
1246                         p_stream->f_rate = 10000000.0 /
1247                             GetQWLE((oggpacket.packet+164));
1248                         p_stream->fmt.video.i_bits_per_pixel =
1249                             GetWLE((oggpacket.packet+182));
1250                         if( !p_stream->fmt.video.i_bits_per_pixel )
1251                             /* hack, FIXME */
1252                             p_stream->fmt.video.i_bits_per_pixel = 24;
1253                         p_stream->fmt.video.i_width =
1254                             GetDWLE((oggpacket.packet+176));
1255                         p_stream->fmt.video.i_height =
1256                             GetDWLE((oggpacket.packet+180));
1257
1258                         msg_Dbg( p_demux,
1259                                  "fps: %f, width:%i; height:%i, bitcount:%i",
1260                                  p_stream->f_rate,
1261                                  p_stream->fmt.video.i_width,
1262                                  p_stream->fmt.video.i_height,
1263                                  p_stream->fmt.video.i_bits_per_pixel);
1264
1265                     }
1266                     /* Check for audio header (old format) */
1267                     else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1268                     {
1269                         int i_extra_size;
1270                         unsigned int i_format_tag;
1271
1272                         p_stream->fmt.i_cat = AUDIO_ES;
1273
1274                         i_extra_size = GetWLE((oggpacket.packet+140));
1275                         if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1276                         {
1277                             p_stream->fmt.i_extra = i_extra_size;
1278                             p_stream->fmt.p_extra = malloc( i_extra_size );
1279                             if( p_stream->fmt.p_extra )
1280                                 memcpy( p_stream->fmt.p_extra,
1281                                         oggpacket.packet + 142, i_extra_size );
1282                             else
1283                                 p_stream->fmt.i_extra = 0;
1284                         }
1285
1286                         i_format_tag = GetWLE((oggpacket.packet+124));
1287                         p_stream->fmt.audio.i_channels =
1288                             GetWLE((oggpacket.packet+126));
1289                         p_stream->f_rate = p_stream->fmt.audio.i_rate =
1290                             GetDWLE((oggpacket.packet+128));
1291                         p_stream->fmt.i_bitrate =
1292                             GetDWLE((oggpacket.packet+132)) * 8;
1293                         p_stream->fmt.audio.i_blockalign =
1294                             GetWLE((oggpacket.packet+136));
1295                         p_stream->fmt.audio.i_bitspersample =
1296                             GetWLE((oggpacket.packet+138));
1297
1298                         wf_tag_to_fourcc( i_format_tag,
1299                                           &p_stream->fmt.i_codec, 0 );
1300
1301                         if( p_stream->fmt.i_codec ==
1302                             VLC_FOURCC('u','n','d','f') )
1303                         {
1304                             p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1305                                 ( i_format_tag >> 8 ) & 0xff,
1306                                 i_format_tag & 0xff );
1307                         }
1308
1309                         msg_Dbg( p_demux, "found audio header of type: %.4s",
1310                                  (char *)&p_stream->fmt.i_codec );
1311                         msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1312                                  "%dbits/sample %dkb/s",
1313                                  i_format_tag,
1314                                  p_stream->fmt.audio.i_channels,
1315                                  p_stream->fmt.audio.i_rate,
1316                                  p_stream->fmt.audio.i_bitspersample,
1317                                  p_stream->fmt.i_bitrate / 1024 );
1318
1319                     }
1320                     else
1321                     {
1322                         msg_Dbg( p_demux, "stream %d has an old header "
1323                             "but is of an unknown type", p_ogg->i_streams-1 );
1324                         free( p_stream );
1325                         p_ogg->i_streams--;
1326                     }
1327                 }
1328                 else if( oggpacket.bytes >= 44+1 &&
1329                          (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER )
1330                 {
1331                     stream_header_t tmp;
1332                     stream_header_t *st = &tmp;
1333
1334                     memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1335                     memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1336                     st->size = GetDWLE( &oggpacket.packet[1+12] );
1337                     st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1338                     st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1339                     st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1340                     st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1341                     st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1342
1343                     /* Check for video header (new format) */
1344                     if( !strncmp( st->streamtype, "video", 5 ) &&
1345                         oggpacket.bytes >= 52+1 )
1346                     {
1347                         st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1348                         st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1349
1350                         p_stream->fmt.i_cat = VIDEO_ES;
1351
1352                         /* We need to get rid of the header packet */
1353                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1354
1355                         p_stream->fmt.i_codec =
1356                             VLC_FOURCC( st->subtype[0], st->subtype[1],
1357                                         st->subtype[2], st->subtype[3] );
1358                         msg_Dbg( p_demux, "found video header of type: %.4s",
1359                                  (char *)&p_stream->fmt.i_codec );
1360
1361                         p_stream->fmt.video.i_frame_rate = 10000000;
1362                         p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1363                         if( st->time_unit <= 0 )
1364                             st->time_unit = 400000;
1365                         p_stream->f_rate = 10000000.0 / st->time_unit;
1366                         p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1367                         p_stream->fmt.video.i_width = st->sh.video.width;
1368                         p_stream->fmt.video.i_height = st->sh.video.height;
1369
1370                         msg_Dbg( p_demux,
1371                                  "fps: %f, width:%i; height:%i, bitcount:%i",
1372                                  p_stream->f_rate,
1373                                  p_stream->fmt.video.i_width,
1374                                  p_stream->fmt.video.i_height,
1375                                  p_stream->fmt.video.i_bits_per_pixel );
1376                     }
1377                     /* Check for audio header (new format) */
1378                     else if( !strncmp( st->streamtype, "audio", 5 ) &&
1379                              oggpacket.bytes >= 56+1 )
1380                     {
1381                         char p_buffer[5];
1382                         int i_extra_size;
1383                         int i_format_tag;
1384
1385                         st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1386                         st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1387                         st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1388
1389                         p_stream->fmt.i_cat = AUDIO_ES;
1390
1391                         /* We need to get rid of the header packet */
1392                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1393
1394                         i_extra_size = st->size - 56;
1395
1396                         if( i_extra_size > 0 &&
1397                             i_extra_size < oggpacket.bytes - 1 - 56 )
1398                         {
1399                             p_stream->fmt.i_extra = i_extra_size;
1400                             p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1401                             if( p_stream->fmt.p_extra )
1402                                 memcpy( p_stream->fmt.p_extra, st + 1,
1403                                         p_stream->fmt.i_extra );
1404                             else
1405                                 p_stream->fmt.i_extra = 0;
1406                         }
1407
1408                         memcpy( p_buffer, st->subtype, 4 );
1409                         p_buffer[4] = '\0';
1410                         i_format_tag = strtol(p_buffer,NULL,16);
1411                         p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1412                         if( st->time_unit <= 0 )
1413                             st->time_unit = 10000000;
1414                         p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1415                         p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1416                         p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1417                         p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1418
1419                         wf_tag_to_fourcc( i_format_tag,
1420                                           &p_stream->fmt.i_codec, 0 );
1421
1422                         if( p_stream->fmt.i_codec ==
1423                             VLC_FOURCC('u','n','d','f') )
1424                         {
1425                             p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1426                                 ( i_format_tag >> 8 ) & 0xff,
1427                                 i_format_tag & 0xff );
1428                         }
1429
1430                         msg_Dbg( p_demux, "found audio header of type: %.4s",
1431                                  (char *)&p_stream->fmt.i_codec );
1432                         msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1433                                  "%dbits/sample %dkb/s",
1434                                  i_format_tag,
1435                                  p_stream->fmt.audio.i_channels,
1436                                  p_stream->fmt.audio.i_rate,
1437                                  p_stream->fmt.audio.i_bitspersample,
1438                                  p_stream->fmt.i_bitrate / 1024 );
1439                     }
1440                     /* Check for text (subtitles) header */
1441                     else if( !strncmp(st->streamtype, "text", 4) )
1442                     {
1443                         /* We need to get rid of the header packet */
1444                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1445
1446                         msg_Dbg( p_demux, "found text subtitles header" );
1447                         p_stream->fmt.i_cat = SPU_ES;
1448                         p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1449                         p_stream->f_rate = 1000; /* granulepos is in millisec */
1450                     }
1451                     else
1452                     {
1453                         msg_Dbg( p_demux, "stream %d has a header marker "
1454                             "but is of an unknown type", p_ogg->i_streams-1 );
1455                         free( p_stream );
1456                         p_ogg->i_streams--;
1457                     }
1458                 }
1459                 else if( oggpacket.bytes >= 7 &&
1460                              ! memcmp( oggpacket.packet, "fishead", 7 ) )
1461
1462                 {
1463                     /* Skeleton */
1464                     msg_Dbg( p_demux, "stream %d is a skeleton",
1465                                 p_ogg->i_streams-1 );
1466                     /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1467                 }
1468                 else
1469                 {
1470                     msg_Dbg( p_demux, "stream %d is of unknown type",
1471                              p_ogg->i_streams-1 );
1472                     free( p_stream );
1473                     p_ogg->i_streams--;
1474                 }
1475
1476                 if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
1477                     return VLC_EGENERIC;
1478             }
1479
1480             /* we'll need to get all headers for all of those streams
1481                that we have to backup headers for */
1482             p_ogg->i_bos = 0;
1483             for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1484             {
1485                 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1486                     p_ogg->i_bos++;
1487             }
1488
1489
1490             /* This is the first data page, which means we are now finished
1491              * with the initial pages. We just need to store it in the relevant
1492              * bitstream. */
1493             for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1494             {
1495                 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1496                                        &p_ogg->current_page ) == 0 )
1497                 {
1498                     p_ogg->b_page_waiting = true;
1499                     break;
1500                 }
1501             }
1502
1503             return VLC_SUCCESS;
1504         }
1505     }
1506
1507     return VLC_EGENERIC;
1508 }
1509
1510 /****************************************************************************
1511  * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1512  *                        Elementary streams.
1513  ****************************************************************************/
1514 static int Ogg_BeginningOfStream( demux_t *p_demux )
1515 {
1516     demux_sys_t *p_ogg = p_demux->p_sys  ;
1517     logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1518     int i_stream;
1519
1520     /* Find the logical streams embedded in the physical stream and
1521      * initialize our p_ogg structure. */
1522     if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1523     {
1524         msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1525         return VLC_EGENERIC;
1526     }
1527
1528     p_ogg->i_bitrate = 0;
1529
1530     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1531     {
1532         logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1533
1534         p_stream->p_es = NULL;
1535
1536         /* initialise kframe index */
1537         p_stream->idx=NULL;
1538
1539         /* Try first to reuse an old ES */
1540         if( p_old_stream &&
1541             p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1542             p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1543         {
1544             msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1545
1546             p_stream->p_es = p_old_stream->p_es;
1547             es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1548
1549             p_old_stream->p_es = NULL;
1550             p_old_stream = NULL;
1551         }
1552
1553         if( !p_stream->p_es )
1554         {
1555             /* Better be safe than sorry when possible with ogm */
1556             if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1557                 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1558                 p_stream->fmt.b_packetized = false;
1559
1560             p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1561         }
1562
1563         // TODO: something to do here ?
1564         if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1565         {
1566             /* Set the CMML stream active */
1567             es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1568         }
1569
1570         p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1571
1572         p_stream->i_pcr = p_stream->i_previous_pcr =
1573             p_stream->i_interpolated_pcr = -1;
1574         p_stream->b_reinit = false;
1575     }
1576
1577     if( p_ogg->p_old_stream )
1578     {
1579         if( p_ogg->p_old_stream->p_es )
1580             msg_Dbg( p_demux, "old stream not reused" );
1581         Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1582         p_ogg->p_old_stream = NULL;
1583     }
1584
1585
1586     /* get total frame count for video stream; we will need this for seeking */
1587     p_ogg->i_total_frames = 0;
1588
1589     return VLC_SUCCESS;
1590 }
1591
1592 /****************************************************************************
1593  * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1594  ****************************************************************************/
1595 static void Ogg_EndOfStream( demux_t *p_demux )
1596 {
1597     demux_sys_t *p_ogg = p_demux->p_sys  ;
1598     int i_stream;
1599
1600     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1601         Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1602     free( p_ogg->pp_stream );
1603
1604     /* Reinit p_ogg */
1605     p_ogg->i_bitrate = 0;
1606     p_ogg->i_streams = 0;
1607     p_ogg->pp_stream = NULL;
1608
1609     /* */
1610     if( p_ogg->p_meta )
1611         vlc_meta_Delete( p_ogg->p_meta );
1612     p_ogg->p_meta = NULL;
1613 }
1614
1615 /**
1616  * This function delete and release all data associated to a logical_stream_t
1617  */
1618 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1619 {
1620     if( p_stream->p_es )
1621         es_out_Del( p_demux->out, p_stream->p_es );
1622
1623     ogg_stream_clear( &p_stream->os );
1624     free( p_stream->p_headers );
1625
1626     es_format_Clean( &p_stream->fmt_old );
1627     es_format_Clean( &p_stream->fmt );
1628
1629     if ( p_stream->idx != NULL)
1630     {
1631         oggseek_index_entries_free( p_stream->idx );
1632     }
1633
1634     free( p_stream );
1635 }
1636 /**
1637  * This function check if a we need to reset a decoder in case we are
1638  * reusing an old ES
1639  */
1640 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1641 {
1642     unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1643     void     *pp_new_data[XIPH_MAX_HEADER_COUNT];
1644     unsigned i_new_count;
1645     if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1646         i_new_count = 0;
1647
1648     unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1649     void     *pp_old_data[XIPH_MAX_HEADER_COUNT];
1650     unsigned i_old_count;
1651     if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1652         i_old_count = 0;
1653
1654     bool b_match = i_new_count == i_old_count;
1655     for( unsigned i = 0; i < i_new_count && b_match; i++ )
1656     {
1657         /* Ignore vorbis comment */
1658         if( i == 1 )
1659             continue;
1660         if( pi_new_size[i] != pi_old_size[i] ||
1661             memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
1662             b_match = false;
1663     }
1664
1665     for( unsigned i = 0; i < i_new_count; i++ )
1666         free( pp_new_data[i] );
1667     for( unsigned i = 0; i < i_old_count; i++ )
1668         free( pp_old_data[i] );
1669     return b_match;
1670 }
1671
1672 static bool Ogg_IsOpusFormatCompatible( const es_format_t *p_new,
1673                                         const es_format_t *p_old )
1674 {
1675     unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1676     void     *pp_new_data[XIPH_MAX_HEADER_COUNT];
1677     unsigned i_new_count;
1678     if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1679         i_new_count = 0;
1680     unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1681     void     *pp_old_data[XIPH_MAX_HEADER_COUNT];
1682     unsigned i_old_count;
1683     if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1684         i_old_count = 0;
1685     bool b_match = false;
1686     if( i_new_count == i_old_count && i_new_count > 0 )
1687     {
1688         static const unsigned char default_map[2] = { 0, 1 };
1689         unsigned char *p_old_head;
1690         unsigned char *p_new_head;
1691         const unsigned char *p_old_map;
1692         const unsigned char *p_new_map;
1693         int i_old_channel_count;
1694         int i_new_channel_count;
1695         int i_old_stream_count;
1696         int i_new_stream_count;
1697         int i_old_coupled_count;
1698         int i_new_coupled_count;
1699         p_old_head = (unsigned char *)pp_old_data[0];
1700         i_old_channel_count = i_old_stream_count = i_old_coupled_count = 0;
1701         p_old_map = default_map;
1702         if( pi_old_size[0] >= 19 && p_old_head[8] <= 15 )
1703         {
1704             i_old_channel_count = p_old_head[9];
1705             switch( p_old_head[18] )
1706             {
1707                 case 0:
1708                     i_old_stream_count = 1;
1709                     i_old_coupled_count = i_old_channel_count - 1;
1710                     break;
1711                 case 1:
1712                     if( pi_old_size[0] >= 21U + i_old_channel_count )
1713                     {
1714                         i_old_stream_count = p_old_head[19];
1715                         i_old_coupled_count = p_old_head[20];
1716                         p_old_map = p_old_head + 21;
1717                     }
1718                     break;
1719             }
1720         }
1721         p_new_head = (unsigned char *)pp_new_data[0];
1722         i_new_channel_count = i_new_stream_count = i_new_coupled_count = 0;
1723         p_new_map = default_map;
1724         if( pi_new_size[0] >= 19 && p_new_head[8] <= 15 )
1725         {
1726             i_new_channel_count = p_new_head[9];
1727             switch( p_new_head[18] )
1728             {
1729                 case 0:
1730                     i_new_stream_count = 1;
1731                     i_new_coupled_count = i_new_channel_count - 1;
1732                     break;
1733                 case 1:
1734                     if( pi_new_size[0] >= 21U + i_new_channel_count )
1735                     {
1736                         i_new_stream_count = p_new_head[19];
1737                         i_new_coupled_count = p_new_head[20];
1738                         p_new_map = p_new_head+21;
1739                     }
1740                     break;
1741             }
1742         }
1743         b_match = i_old_channel_count == i_new_channel_count &&
1744                   i_old_stream_count == i_new_stream_count &&
1745                   i_old_coupled_count == i_new_coupled_count &&
1746                   memcmp(p_old_map, p_new_map,
1747                       i_new_channel_count*sizeof(*p_new_map)) == 0;
1748     }
1749     for( unsigned i = 0; i < i_new_count; i++ )
1750         free( pp_new_data[i] );
1751     for( unsigned i = 0; i < i_old_count; i++ )
1752         free( pp_old_data[i] );
1753     return b_match;
1754 }
1755
1756 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1757 {
1758     bool b_compatible = false;
1759     if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1760         return true;
1761
1762     /* Only Vorbis and Opus are supported. */
1763     if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
1764         b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1765     else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
1766         b_compatible = Ogg_IsOpusFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1767
1768     if( !b_compatible )
1769         msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1770
1771     return !b_compatible;
1772 }
1773 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const void *p_headers, unsigned i_headers, unsigned i_skip )
1774 {
1775     demux_sys_t *p_ogg = p_demux->p_sys;
1776
1777     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1778     void     *pp_data[XIPH_MAX_HEADER_COUNT];
1779     unsigned i_count;
1780     if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
1781         return;
1782
1783     /* TODO how to handle multiple comments properly ? */
1784     if( i_count >= 2 && pi_size[1] > i_skip )
1785         vorbis_ParseComment( &p_ogg->p_meta, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip,
1786                              &p_ogg->i_attachments, &p_ogg->attachments );
1787
1788     for( unsigned i = 0; i < i_count; i++ )
1789         free( pp_data[i] );
1790 }
1791 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1792 {
1793     demux_sys_t *p_ogg = p_demux->p_sys;
1794
1795     switch( i_codec )
1796     {
1797     /* 3 headers with the 2° one being the comments */
1798     case VLC_CODEC_VORBIS:
1799     case VLC_CODEC_THEORA:
1800         Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1801         break;
1802     case VLC_CODEC_OPUS:
1803         Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 8 );
1804         break;
1805     case VLC_CODEC_SPEEX:
1806         Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
1807         break;
1808
1809     /* N headers with the 2° one being the comments */
1810     case VLC_CODEC_KATE:
1811         /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
1812         Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1 );
1813         break;
1814
1815     /* TODO */
1816     case VLC_CODEC_FLAC:
1817         msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1818         break;
1819
1820     /* No meta data */
1821     case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1822     case VLC_CODEC_DIRAC:
1823     default:
1824         break;
1825     }
1826     if( p_ogg->p_meta )
1827         p_demux->info.i_update |= INPUT_UPDATE_META;
1828 }
1829
1830 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream,
1831                                   ogg_packet *p_oggpacket, double f_rate )
1832 {
1833     int64_t last_packet = oggseek_get_last_frame( p_demux, p_stream );
1834     /*
1835      * Since there's quite a good chance that ogg_stream_packetout was called,
1836      * the given p_oggpacket may point to invalid data. Fill it with some valid ones
1837      */
1838     ogg_stream_packetpeek( &p_stream->os, p_oggpacket );
1839
1840     return ( last_packet >= 0 ) ? last_packet / f_rate : -1;
1841 }
1842
1843 static void Ogg_ReadTheoraHeader( demux_t *p_demux, logical_stream_t *p_stream,
1844                                   ogg_packet *p_oggpacket )
1845 {
1846     bs_t bitstream;
1847     int i_fps_numerator;
1848     int i_fps_denominator;
1849     int i_keyframe_frequency_force;
1850     int i_major;
1851     int i_minor;
1852     int i_subminor;
1853     int i_version;
1854
1855     p_stream->fmt.i_cat = VIDEO_ES;
1856     p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1857
1858     /* Signal that we want to keep a backup of the theora
1859      * stream headers. They will be used when switching between
1860      * audio streams. */
1861     p_stream->b_force_backup = 1;
1862
1863     /* Cheat and get additionnal info ;) */
1864     bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1865     bs_skip( &bitstream, 56 );
1866
1867     i_major = bs_read( &bitstream, 8 ); /* major version num */
1868     i_minor = bs_read( &bitstream, 8 ); /* minor version num */
1869     i_subminor = bs_read( &bitstream, 8 ); /* subminor version num */
1870
1871     bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1872     bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1873     bs_read( &bitstream, 24 ); /* frame width */
1874     bs_read( &bitstream, 24 ); /* frame height */
1875     bs_read( &bitstream, 8 ); /* x offset */
1876     bs_read( &bitstream, 8 ); /* y offset */
1877
1878     i_fps_numerator = bs_read( &bitstream, 32 );
1879     i_fps_denominator = bs_read( &bitstream, 32 );
1880     bs_read( &bitstream, 24 ); /* aspect_numerator */
1881     bs_read( &bitstream, 24 ); /* aspect_denominator */
1882
1883     p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1884     p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1885
1886     bs_read( &bitstream, 8 ); /* colorspace */
1887     p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1888     bs_read( &bitstream, 6 ); /* quality */
1889
1890     i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1891
1892     /* granule_shift = i_log( frequency_force -1 ) */
1893     p_stream->i_granule_shift = 0;
1894     i_keyframe_frequency_force--;
1895     while( i_keyframe_frequency_force )
1896     {
1897         p_stream->i_granule_shift++;
1898         i_keyframe_frequency_force >>= 1;
1899     }
1900
1901     i_version = i_major * 1000000 + i_minor * 1000 + i_subminor;
1902     p_stream->i_keyframe_offset = 0;
1903     p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1904
1905     if ( i_version >= 3002001 )
1906     {
1907         p_stream->i_keyframe_offset = 1;
1908     }
1909     if ( p_demux->p_sys->i_length < 0 )
1910     {
1911         int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
1912         if ( last_packet >= 0 )
1913             p_demux->p_sys->i_length = last_packet;
1914     }
1915
1916 }
1917
1918 static void Ogg_ReadVorbisHeader( demux_t *p_demux, logical_stream_t *p_stream,
1919                                   ogg_packet *p_oggpacket )
1920 {
1921     oggpack_buffer opb;
1922
1923     p_stream->fmt.i_cat = AUDIO_ES;
1924     p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1925
1926     /* Signal that we want to keep a backup of the vorbis
1927      * stream headers. They will be used when switching between
1928      * audio streams. */
1929     p_stream->b_force_backup = 1;
1930
1931     /* Cheat and get additionnal info ;) */
1932     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1933     oggpack_adv( &opb, 88 );
1934     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1935     p_stream->f_rate = p_stream->fmt.audio.i_rate =
1936         oggpack_read( &opb, 32 );
1937     oggpack_adv( &opb, 32 );
1938     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1939
1940     if ( p_demux->p_sys->i_length < 0 )
1941     {
1942         int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
1943         if ( last_packet >= 0 )
1944             p_demux->p_sys->i_length = last_packet;
1945     }
1946 }
1947
1948 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1949                                  ogg_packet *p_oggpacket )
1950 {
1951     oggpack_buffer opb;
1952
1953     p_stream->fmt.i_cat = AUDIO_ES;
1954     p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1955
1956     /* Signal that we want to keep a backup of the speex
1957      * stream headers. They will be used when switching between
1958      * audio streams. */
1959     p_stream->b_force_backup = 1;
1960
1961     /* Cheat and get additionnal info ;) */
1962     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1963     oggpack_adv( &opb, 224 );
1964     oggpack_adv( &opb, 32 ); /* speex_version_id */
1965     oggpack_adv( &opb, 32 ); /* header_size */
1966     p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1967     oggpack_adv( &opb, 32 ); /* mode */
1968     oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1969     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1970     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1971 }
1972
1973 static void Ogg_ReadOpusHeader( demux_t *p_demux,
1974                                 logical_stream_t *p_stream,
1975                                 ogg_packet *p_oggpacket )
1976 {
1977     oggpack_buffer opb;
1978
1979     p_stream->fmt.i_cat = AUDIO_ES;
1980     p_stream->fmt.i_codec = VLC_CODEC_OPUS;
1981
1982     /* Signal that we want to keep a backup of the opus
1983      * stream headers. They will be used when switching between
1984      * audio streams. */
1985     p_stream->b_force_backup = 1;
1986
1987     /* All OggOpus streams are timestamped at 48kHz and
1988      * can be played at 48kHz. */
1989     p_stream->f_rate = p_stream->fmt.audio.i_rate = 48000;
1990     p_stream->fmt.i_bitrate = 0;
1991
1992     /* Cheat and get additional info ;) */
1993     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1994     oggpack_adv( &opb, 64 );
1995     oggpack_adv( &opb, 8 ); /* version_id */
1996     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1997     p_stream->i_pre_skip = oggpack_read( &opb, 16 );
1998
1999     if ( p_demux->p_sys->i_length < 0 )
2000     {
2001         int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
2002         if ( last_packet >= 0 )
2003             p_demux->p_sys->i_length = last_packet;
2004     }
2005 }
2006
2007 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
2008                                 ogg_packet *p_oggpacket )
2009 {
2010     /* Parse the STREAMINFO metadata */
2011     bs_t s;
2012
2013     bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
2014
2015     bs_read( &s, 1 );
2016     if( p_oggpacket->bytes > 0 && bs_read( &s, 7 ) == 0 )
2017     {
2018         if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
2019         {
2020             bs_skip( &s, 80 );
2021             p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
2022             p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
2023
2024             msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
2025                      p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
2026         }
2027         else
2028         {
2029             msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
2030         }
2031
2032         /* Fake this as the last metadata block */
2033         *((uint8_t*)p_oggpacket->packet) |= 0x80;
2034     }
2035     else
2036     {
2037         /* This ain't a STREAMINFO metadata */
2038         msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
2039     }
2040 }
2041
2042 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
2043                                 ogg_packet *p_oggpacket )
2044 {
2045     oggpack_buffer opb;
2046     int32_t gnum;
2047     int32_t gden;
2048     int n;
2049     char *psz_desc;
2050
2051     p_stream->fmt.i_cat = SPU_ES;
2052     p_stream->fmt.i_codec = VLC_CODEC_KATE;
2053
2054     /* Signal that we want to keep a backup of the kate
2055      * stream headers. They will be used when switching between
2056      * kate streams. */
2057     p_stream->b_force_backup = 1;
2058
2059     /* Cheat and get additionnal info ;) */
2060     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2061     oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
2062     p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
2063     oggpack_adv( &opb, 3*8 );
2064     p_stream->i_granule_shift = oggpack_read( &opb, 8 );
2065     oggpack_adv( &opb, 8*8 ); /* reserved */
2066     gnum = oggpack_read( &opb, 32 );
2067     gden = oggpack_read( &opb, 32 );
2068     p_stream->f_rate = (double)gnum/gden;
2069
2070     p_stream->fmt.psz_language = malloc(16);
2071     if( p_stream->fmt.psz_language )
2072     {
2073         for( n = 0; n < 16; n++ )
2074             p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
2075         p_stream->fmt.psz_language[15] = 0; /* just in case */
2076     }
2077     else
2078     {
2079         for( n = 0; n < 16; n++ )
2080             oggpack_read(&opb,8);
2081     }
2082     p_stream->fmt.psz_description = malloc(16);
2083     if( p_stream->fmt.psz_description )
2084     {
2085         for( n = 0; n < 16; n++ )
2086             p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
2087         p_stream->fmt.psz_description[15] = 0; /* just in case */
2088
2089         /* Now find a localized user readable description for this category */
2090         psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
2091         if( psz_desc )
2092         {
2093             free( p_stream->fmt.psz_description );
2094             p_stream->fmt.psz_description = psz_desc;
2095         }
2096     }
2097     else
2098     {
2099         for( n = 0; n < 16; n++ )
2100             oggpack_read(&opb,8);
2101     }
2102 }
2103
2104 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,
2105                                    logical_stream_t *p_stream,
2106                                    ogg_packet *p_oggpacket )
2107 {
2108     if( p_oggpacket->bytes >= 28 &&
2109         !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
2110     {
2111         oggpack_buffer opb;
2112
2113         uint16_t major_version;
2114         uint16_t minor_version;
2115         uint64_t timebase_numerator;
2116         uint64_t timebase_denominator;
2117
2118         Ogg_ReadTheoraHeader( p_demux, p_stream, p_oggpacket );
2119
2120         oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2121         oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
2122         major_version = oggpack_read( &opb, 2*8 ); /* major version */
2123         minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
2124         timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
2125         timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
2126     }
2127     else if( p_oggpacket->bytes >= 42 &&
2128              !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
2129     {
2130         uint64_t granule_rate_numerator;
2131         uint64_t granule_rate_denominator;
2132         char content_type_string[1024];
2133
2134         /* Read in Annodex header fields */
2135
2136         granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
2137         granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
2138         p_stream->i_secondary_header_packets =
2139             GetDWLE( &p_oggpacket->packet[24] );
2140
2141         /* we are guaranteed that the first header field will be
2142          * the content-type (by the Annodex standard) */
2143         content_type_string[0] = '\0';
2144         if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
2145         {
2146             uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
2147                                  p_oggpacket->bytes - 1 );
2148             if( p && p[0] == '\r' && p[1] == '\n' )
2149                 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
2150                         content_type_string );
2151         }
2152
2153         msg_Dbg( p_demux, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
2154                  granule_rate_numerator, granule_rate_denominator,
2155                  p_stream->i_secondary_header_packets, content_type_string );
2156
2157         p_stream->f_rate = (float) granule_rate_numerator /
2158             (float) granule_rate_denominator;
2159
2160         /* What type of file do we have?
2161          * strcmp is safe to use here because we've extracted
2162          * content_type_string from the stream manually */
2163         if( !strncmp(content_type_string, "audio/x-wav", 11) )
2164         {
2165             /* n.b. WAVs are unsupported right now */
2166             p_stream->fmt.i_cat = UNKNOWN_ES;
2167         }
2168         else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
2169         {
2170             p_stream->fmt.i_cat = AUDIO_ES;
2171             p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2172
2173             p_stream->b_force_backup = 1;
2174         }
2175         else if( !strncmp(content_type_string, "audio/x-speex", 13) )
2176         {
2177             p_stream->fmt.i_cat = AUDIO_ES;
2178             p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2179
2180             p_stream->b_force_backup = 1;
2181         }
2182         else if( !strncmp(content_type_string, "video/x-theora", 14) )
2183         {
2184             p_stream->fmt.i_cat = VIDEO_ES;
2185             p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2186
2187             p_stream->b_force_backup = 1;
2188         }
2189         else if( !strncmp(content_type_string, "video/x-xvid", 12) )
2190         {
2191             p_stream->fmt.i_cat = VIDEO_ES;
2192             p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
2193
2194             p_stream->b_force_backup = 1;
2195         }
2196         else if( !strncmp(content_type_string, "video/mpeg", 10) )
2197         {
2198             /* n.b. MPEG streams are unsupported right now */
2199             p_stream->fmt.i_cat = VIDEO_ES;
2200             p_stream->fmt.i_codec = VLC_CODEC_MPGV;
2201         }
2202         else if( !strncmp(content_type_string, "text/x-cmml", 11) )
2203         {
2204             ogg_stream_packetout( &p_stream->os, p_oggpacket );
2205             p_stream->fmt.i_cat = SPU_ES;
2206             p_stream->fmt.i_codec = VLC_CODEC_CMML;
2207         }
2208     }
2209 }
2210
2211 static uint32_t dirac_uint( bs_t *p_bs )
2212 {
2213     uint32_t u_count = 0, u_value = 0;
2214
2215     while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
2216     {
2217         u_count++;
2218         u_value <<= 1;
2219         u_value |= bs_read( p_bs, 1 );
2220     }
2221
2222     return (1<<u_count) - 1 + u_value;
2223 }
2224
2225 static int dirac_bool( bs_t *p_bs )
2226 {
2227     return bs_read( p_bs, 1 );
2228 }
2229
2230 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
2231                                  ogg_packet *p_oggpacket )
2232 {
2233     static const struct {
2234         uint32_t u_n /* numerator */, u_d /* denominator */;
2235     } p_dirac_frate_tbl[] = { /* table 10.3 */
2236         {1,1}, /* this first value is never used */
2237         {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
2238         {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
2239     };
2240     static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
2241
2242     static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
2243         1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
2244     };
2245     static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
2246
2247     bs_t bs;
2248
2249     p_stream->i_granule_shift = 22; /* not 32 */
2250
2251     /* Backing up stream headers is not required -- seqhdrs are repeated
2252      * thoughout the stream at suitable decoding start points */
2253     p_stream->b_force_backup = 0;
2254
2255     /* read in useful bits from sequence header */
2256     bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
2257     bs_skip( &bs, 13*8); /* parse_info_header */
2258     dirac_uint( &bs ); /* major_version */
2259     dirac_uint( &bs ); /* minor_version */
2260     dirac_uint( &bs ); /* profile */
2261     dirac_uint( &bs ); /* level */
2262
2263     uint32_t u_video_format = dirac_uint( &bs ); /* index */
2264     if( u_video_format >= u_dirac_vidfmt_frate )
2265     {
2266         /* don't know how to parse this ogg dirac stream */
2267         return false;
2268     }
2269
2270     if( dirac_bool( &bs ) )
2271     {
2272         dirac_uint( &bs ); /* frame_width */
2273         dirac_uint( &bs ); /* frame_height */
2274     }
2275
2276     if( dirac_bool( &bs ) )
2277     {
2278         dirac_uint( &bs ); /* chroma_format */
2279     }
2280
2281     if( dirac_bool( &bs ) )
2282     {
2283         dirac_uint( &bs ); /* scan_format */
2284     }
2285
2286     uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
2287     uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
2288     if( dirac_bool( &bs ) )
2289     {
2290         uint32_t u_frame_rate_index = dirac_uint( &bs );
2291         if( u_frame_rate_index >= u_dirac_frate_tbl )
2292         {
2293             /* something is wrong with this stream */
2294             return false;
2295         }
2296         u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
2297         u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
2298         if( u_frame_rate_index == 0 )
2299         {
2300             u_n = dirac_uint( &bs ); /* frame_rate_numerator */
2301             u_d = dirac_uint( &bs ); /* frame_rate_denominator */
2302         }
2303     }
2304     p_stream->f_rate = (float) u_n / u_d;
2305
2306     /* probably is an ogg dirac es */
2307     p_stream->fmt.i_cat = VIDEO_ES;
2308     p_stream->fmt.i_codec = VLC_CODEC_DIRAC;
2309
2310     return true;
2311 }