]> git.sesse.net Git - vlc/blob - modules/demux/ogg.c
demux: ogg: add ogg/VP8 support
[vlc] / modules / demux / ogg.c
1 /*****************************************************************************
2  * ogg.c : ogg stream demux module for vlc
3  *****************************************************************************
4  * Copyright (C) 2001-2007 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * 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 "xiph_metadata.h"
44 #include "ogg.h"
45 #include "oggseek.h"
46
47 /*****************************************************************************
48  * Module descriptor
49  *****************************************************************************/
50 static int  Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
52
53 vlc_module_begin ()
54     set_shortname ( "OGG" )
55     set_description( N_("OGG demuxer" ) )
56     set_category( CAT_INPUT )
57     set_subcategory( SUBCAT_INPUT_DEMUX )
58     set_capability( "demux", 50 )
59     set_callbacks( Open, Close )
60     add_shortcut( "ogg" )
61 vlc_module_end ()
62
63
64 /*****************************************************************************
65  * Definitions of structures and functions used by this plugins
66  *****************************************************************************/
67
68 /* OggDS headers for the new header format (used in ogm files) */
69 typedef struct
70 {
71     ogg_int32_t width;
72     ogg_int32_t height;
73 } stream_header_video_t;
74
75 typedef struct
76 {
77     ogg_int16_t channels;
78     ogg_int16_t padding;
79     ogg_int16_t blockalign;
80     ogg_int32_t avgbytespersec;
81 } stream_header_audio_t;
82
83 typedef struct
84 {
85     char        streamtype[8];
86     char        subtype[4];
87
88     ogg_int32_t size;                               /* size of the structure */
89
90     ogg_int64_t time_unit;                              /* in reference time */
91     ogg_int64_t samples_per_unit;
92     ogg_int32_t default_len;                                /* in media time */
93
94     ogg_int32_t buffersize;
95     ogg_int16_t bits_per_sample;
96     ogg_int16_t padding;
97
98     union
99     {
100         /* Video specific */
101         stream_header_video_t video;
102         /* Audio specific */
103         stream_header_audio_t audio;
104     } sh;
105 } stream_header_t;
106
107 /*****************************************************************************
108  * Local prototypes
109  *****************************************************************************/
110 static int  Demux  ( demux_t * );
111 static int  Control( demux_t *, int, va_list );
112
113 /* Bitstream manipulation */
114 static int  Ogg_ReadPage     ( demux_t *, ogg_page * );
115 static void Ogg_UpdatePCR    ( demux_t *, logical_stream_t *, ogg_packet * );
116 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
117 static int  Ogg_OpusPacketDuration( logical_stream_t *, ogg_packet * );
118
119 static void Ogg_CreateES( demux_t *p_demux );
120 static int Ogg_BeginningOfStream( demux_t *p_demux );
121 static int Ogg_FindLogicalStreams( demux_t *p_demux );
122 static void Ogg_EndOfStream( demux_t *p_demux );
123
124 /* */
125 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
126 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
127
128 /* */
129 static void Ogg_ExtractMeta( demux_t *p_demux, es_format_t *p_fmt, const uint8_t *p_headers, int i_headers );
130
131 /* Logical bitstream headers */
132 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
133 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
134 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
135 static void Ogg_ReadOpusHeader( logical_stream_t *, ogg_packet * );
136 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
137 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
138 static void Ogg_ReadAnnodexHeader( demux_t *, logical_stream_t *, ogg_packet * );
139 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
140 static bool Ogg_ReadVP8Header( demux_t *, logical_stream_t *, ogg_packet * );
141 static void Ogg_ReadSkeletonHeader( demux_t *, logical_stream_t *, ogg_packet * );
142
143 /* Skeleton */
144 static void Ogg_ReadSkeletonBones( demux_t *, ogg_packet * );
145 static void Ogg_ReadSkeletonIndex( demux_t *, ogg_packet * );
146 static void Ogg_FreeSkeleton( ogg_skeleton_t * );
147 static void Ogg_ApplySkeleton( logical_stream_t * );
148
149 static void fill_channels_info(audio_format_t *audio)
150 {
151     static const int pi_channels_map[9] =
152     {
153         0,
154         AOUT_CHAN_CENTER,
155         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
156         AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
157         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
158             | AOUT_CHAN_REARRIGHT,
159         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
160             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
161         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
162             | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
163         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
164             | AOUT_CHAN_REARCENTER | AOUT_CHAN_MIDDLELEFT
165             | AOUT_CHAN_MIDDLERIGHT | AOUT_CHAN_LFE,
166         AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
167             | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
168             | AOUT_CHAN_LFE,
169     };
170
171     unsigned chans = audio->i_channels;
172     if (chans < sizeof(pi_channels_map) / sizeof(pi_channels_map[0]))
173         audio->i_physical_channels =
174         audio->i_original_channels = pi_channels_map[chans];
175 }
176
177 /* Special TS value: don't send or derive any pts/pcr from it.
178    Represents TS state prior first known valid timestamp */
179 #define VLC_TS_UNKNOWN (VLC_TS_INVALID - 1)
180
181 /*****************************************************************************
182  * Open: initializes ogg demux structures
183  *****************************************************************************/
184 static int Open( vlc_object_t * p_this )
185 {
186     demux_t *p_demux = (demux_t *)p_this;
187     demux_sys_t    *p_sys;
188     const uint8_t  *p_peek;
189
190     /* Check if we are dealing with an ogg stream */
191     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
192     if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
193     {
194         return VLC_EGENERIC;
195     }
196
197     /* */
198     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
199     if( !p_sys )
200         return VLC_ENOMEM;
201
202     p_sys->i_length = -1;
203     p_sys->b_preparsing_done = false;
204
205     /* Set exported functions */
206     p_demux->pf_demux = Demux;
207     p_demux->pf_control = Control;
208
209     /* Initialize the Ogg physical bitstream parser */
210     ogg_sync_init( &p_sys->oy );
211
212     /* */
213     TAB_INIT( p_sys->i_seekpoints, p_sys->pp_seekpoints );
214
215
216     while ( !p_sys->b_preparsing_done && p_demux->pf_demux( p_demux ) > 0 )
217     {}
218
219     return VLC_SUCCESS;
220 }
221
222 /*****************************************************************************
223  * Close: frees unused data
224  *****************************************************************************/
225 static void Close( vlc_object_t *p_this )
226 {
227     demux_t *p_demux = (demux_t *)p_this;
228     demux_sys_t *p_sys = p_demux->p_sys  ;
229
230     /* Cleanup the bitstream parser */
231     ogg_sync_clear( &p_sys->oy );
232
233     Ogg_EndOfStream( p_demux );
234
235     if( p_sys->p_old_stream )
236         Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
237
238     free( p_sys );
239 }
240
241 /*****************************************************************************
242  * Demux: reads and demuxes data packets
243  *****************************************************************************
244  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
245  *****************************************************************************/
246 static int Demux( demux_t * p_demux )
247 {
248     demux_sys_t *p_sys = p_demux->p_sys;
249     ogg_packet  oggpacket;
250     int         i_stream;
251     bool b_skipping = false;
252     bool b_canseek;
253
254     int i_active_streams = p_sys->i_streams;
255     for ( int i=0; i < p_sys->i_streams; i++ )
256     {
257         if ( p_sys->pp_stream[i]->b_finished )
258             i_active_streams--;
259     }
260
261     if ( i_active_streams == 0 )
262     {
263         if ( p_sys->i_streams ) /* All finished */
264         {
265             msg_Dbg( p_demux, "end of a group of logical streams" );
266             /* We keep the ES to try reusing it in Ogg_BeginningOfStream
267              * only 1 ES is supported (common case for ogg web radio) */
268             if( p_sys->i_streams == 1 )
269             {
270                 p_sys->p_old_stream = p_sys->pp_stream[0];
271                 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
272             }
273             Ogg_EndOfStream( p_demux );
274             p_sys->b_chained_boundary = true;
275         }
276
277         if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
278             return 0;
279
280         msg_Dbg( p_demux, "beginning of a group of logical streams" );
281
282         if ( !p_sys->b_chained_boundary )
283         {
284             /* Find the real duration */
285             stream_Control( p_demux->s, STREAM_CAN_SEEK, &b_canseek );
286             if ( b_canseek )
287                 Oggseek_ProbeEnd( p_demux );
288         }
289         else
290         {
291             p_sys->b_chained_boundary = false;
292         }
293     }
294
295     if ( p_sys->b_preparsing_done && !p_sys->b_es_created )
296     {
297         Ogg_CreateES( p_demux );
298         p_sys->b_es_created = true;
299     }
300
301     /*
302      * The first data page of a physical stream is stored in the relevant logical stream
303      * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
304      * stream it belongs to if we haven't processed this first page yet. If we do, we
305      * will only process that first page whenever we find the second page for this stream.
306      * While this is fine for Vorbis and Theora, which are continuous codecs, which means
307      * the second page will arrive real quick, this is not fine for Kate, whose second
308      * data page will typically arrive much later.
309      * This means it is now possible to seek right at the start of a stream where the last
310      * logical stream is Kate, without having to wait for the second data page to unblock
311      * the first one, which is the one that triggers the 'no more headers to backup' code.
312      * And, as we all know, seeking without having backed up all headers is bad, since the
313      * codec will fail to initialize if it's missing its headers.
314      */
315     if( !p_sys->b_page_waiting)
316     {
317         /*
318          * Demux an ogg page from the stream
319          */
320         if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
321             return 0; /* EOF */
322         /* Test for End of Stream */
323         if( ogg_page_eos( &p_sys->current_page ) )
324         {
325             /* If we delayed restarting encoders/SET_ES_FMT for more
326              * skeleton provided configuration */
327             if ( p_sys->p_skelstream )
328             {
329                 if ( p_sys->p_skelstream->i_serial_no == ogg_page_serialno(&p_sys->current_page) )
330                 {
331                     msg_Dbg( p_demux, "End of Skeleton" );
332                     p_sys->b_preparsing_done = true;
333                     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
334                     {
335                         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
336                         Ogg_ApplySkeleton( p_stream );
337                     }
338                 }
339             }
340
341             for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
342             {
343                 if ( p_sys->pp_stream[i_stream]->i_serial_no == ogg_page_serialno( &p_sys->current_page ) )
344                 {
345                     p_sys->pp_stream[i_stream]->b_finished = true;
346                     break;
347                 }
348             }
349         }
350     }
351
352     b_skipping = false;
353     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
354     {
355         b_skipping |= p_sys->pp_stream[i_stream]->i_skip_frames;
356     }
357
358     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
359     {
360         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
361
362         /* if we've just pulled page, look for the right logical stream */
363         if( !p_sys->b_page_waiting )
364         {
365             if( p_sys->i_streams == 1 &&
366                 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
367             {
368                 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
369                 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
370
371                 p_stream->b_reinit = true;
372                 p_stream->i_pcr = VLC_TS_UNKNOWN;
373                 p_stream->i_interpolated_pcr = VLC_TS_UNKNOWN;
374                 p_stream->i_previous_granulepos = -1;
375                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0);
376             }
377
378             /* Does fail if serialno differs */
379             if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
380             {
381                 continue;
382             }
383
384         }
385
386         /* clear the finished flag if pages after eos (ex: after a seek) */
387         if ( ! ogg_page_eos( &p_sys->current_page ) ) p_stream->b_finished = false;
388
389         DemuxDebug(
390             if ( p_stream->fmt.i_cat == VIDEO_ES )
391                 msg_Dbg(p_demux, "DEMUX READ pageno %ld g%"PRId64" (%d packets) cont %d %ld bytes",
392                     ogg_page_pageno( &p_sys->current_page ),
393                     ogg_page_granulepos( &p_sys->current_page ),
394                     ogg_page_packets( &p_sys->current_page ),
395                     ogg_page_continued(&p_sys->current_page),
396                     p_sys->current_page.body_len )
397         );
398
399         while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
400         {
401             /* Read info from any secondary header packets, if there are any */
402             if( p_stream->i_secondary_header_packets > 0 )
403             {
404                 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
405                         oggpacket.bytes >= 7 &&
406                         ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
407                 {
408                     Ogg_ReadTheoraHeader( p_stream, &oggpacket );
409                     p_stream->i_secondary_header_packets = 0;
410                 }
411                 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
412                         oggpacket.bytes >= 7 &&
413                         ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
414                 {
415                     Ogg_ReadVorbisHeader( p_stream, &oggpacket );
416                     p_stream->i_secondary_header_packets = 0;
417                 }
418                 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
419                 {
420                     p_stream->i_secondary_header_packets = 0;
421                 }
422
423                 /* update start of data pointer */
424                 p_stream->i_data_start = stream_Tell( p_demux->s );
425             }
426
427             /* If any streams have i_skip_frames, only decode (pre-roll)
428              *  for those streams, but don't skip headers */
429             if ( b_skipping && p_stream->i_skip_frames == 0
430                  && p_stream->i_secondary_header_packets ) continue;
431
432             if( p_stream->b_reinit )
433             {
434                 if ( Oggseek_PacketPCRFixup( p_stream, &p_sys->current_page,
435                                          &oggpacket ) )
436                 {
437                     DemuxDebug( msg_Dbg( p_demux, "PCR fixup for %"PRId64,
438                              ogg_page_granulepos( &p_sys->current_page ) ) );
439                 }
440                 else
441                 {
442                     /* If synchro is re-initialized we need to drop all the packets
443                          * until we find a new dated one. */
444                     Ogg_UpdatePCR( p_demux, p_stream, &oggpacket );
445                 }
446
447                 if( p_stream->i_pcr >= 0 )
448                 {
449                     p_stream->b_reinit = false;
450                     /* For Opus, trash the first 80 ms of decoded output as
451                        well, to avoid blowing out speakers if we get unlucky.
452                        Opus predicts content from prior frames, which can go
453                        badly if we seek right where the stream goes from very
454                        quiet to very loud. It will converge after a bit. */
455                     if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
456                     {
457                         ogg_int64_t start_time;
458                         int duration;
459                         p_stream->i_skip_frames = 80*48;
460                         /* Make sure we never play audio from within the
461                            pre-skip at the beginning of the stream. */
462                         duration =
463                             Ogg_OpusPacketDuration( p_stream, &oggpacket );
464                         start_time = p_stream->i_previous_granulepos;
465                         if( duration > 0 )
466                         {
467                             start_time = start_time > duration ?
468                                 start_time - duration : 0;
469                         }
470                         if( p_stream->i_pre_skip > start_time )
471                         {
472                             p_stream->i_skip_frames +=
473                                 p_stream->i_pre_skip - start_time;
474                         }
475                     }
476                 }
477                 else
478                 {
479                     DemuxDebug(
480                         msg_Dbg(p_demux, "DEMUX DROPS PACKET (? / %d) pageno %ld granule %"PRId64,
481                             ogg_page_packets( &p_sys->current_page ),
482                             ogg_page_pageno( &p_sys->current_page ), oggpacket.granulepos );
483                     );
484
485                     p_stream->i_interpolated_pcr = -1;
486                     p_stream->i_previous_granulepos = -1;
487                     continue;
488                 }
489
490                 /* An Ogg/vorbis packet contains an end date granulepos */
491                 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
492                     p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
493                     p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
494                     p_stream->fmt.i_codec == VLC_CODEC_VP8 ||
495                     p_stream->fmt.i_codec == VLC_CODEC_FLAC )
496                 {
497                     if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
498                     {
499                         Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
500                     }
501                     else
502                     {
503                         es_out_Control( p_demux->out, ES_OUT_SET_PCR,
504                                         VLC_TS_0 + p_stream->i_pcr );
505                     }
506                     continue;
507                 }
508             }
509
510             DemuxDebug( if ( p_sys->b_seeked )
511             {
512                 if ( Ogg_IsKeyFrame( p_stream, &oggpacket ) )
513                      msg_Dbg(p_demux, "** DEMUX ON KEYFRAME **" );
514
515                 ogg_int64_t iframe = ogg_page_granulepos( &p_sys->current_page ) >> p_stream->i_granule_shift;
516                 ogg_int64_t pframe = ogg_page_granulepos( &p_sys->current_page ) - ( iframe << p_stream->i_granule_shift );
517
518                 msg_Dbg(p_demux, "DEMUX PACKET (size %d) IS at iframe %"PRId64" pageno %ld pframe %"PRId64" OFFSET %"PRId64" PACKET NO %"PRId64" skipleft=%d",
519                         ogg_page_packets( &p_sys->current_page ),
520                         iframe, ogg_page_pageno( &p_sys->current_page ), pframe, p_sys->i_input_position, oggpacket.packetno, p_stream->i_skip_frames );
521             })
522
523             Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
524         }
525
526         DemuxDebug( p_sys->b_seeked = false; )
527
528         if( !p_sys->b_page_waiting )
529             break;
530     }
531
532     /* if a page was waiting, it's now processed */
533     p_sys->b_page_waiting = false;
534
535     if ( p_sys->p_skelstream && !p_sys->p_skelstream->b_finished )
536         p_sys->b_preparsing_done = false;
537     else
538         p_sys->b_preparsing_done = true;
539
540     /* We will consider the lowest PCR among tracks, because the audio core badly
541      * handles PCR rewind (mute)
542      */
543     mtime_t i_pcr_candidate = VLC_TS_INVALID;
544     for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
545     {
546         logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
547
548         if ( p_sys->b_preparsing_done && p_stream->b_initializing )
549         {
550             /* We have 1 or more streams needing more than 1 page for preparsing */
551             p_sys->b_preparsing_done = false;
552         }
553
554         if( p_stream->fmt.i_cat == SPU_ES )
555             continue;
556         if( p_stream->i_interpolated_pcr < VLC_TS_0 )
557             continue;
558         if ( p_stream->b_finished || p_stream->b_initializing )
559             continue;
560
561         if( i_pcr_candidate < VLC_TS_0
562             || p_stream->i_interpolated_pcr <= i_pcr_candidate )
563         {
564             i_pcr_candidate = p_stream->i_interpolated_pcr;
565         }
566     }
567
568     if ( i_pcr_candidate > VLC_TS_INVALID && p_sys->i_pcr != i_pcr_candidate )
569     {
570         p_sys->i_pcr = i_pcr_candidate;
571         if( ! b_skipping )
572             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
573     }
574
575     return 1;
576 }
577
578 static void Ogg_ResetStreamHelper( demux_sys_t *p_sys )
579 {
580     for( int i = 0; i < p_sys->i_streams; i++ )
581     {
582         logical_stream_t *p_stream = p_sys->pp_stream[i];
583
584         /* we'll trash all the data until we find the next pcr */
585         p_stream->b_reinit = true;
586         p_stream->i_pcr = VLC_TS_UNKNOWN;
587         p_stream->i_interpolated_pcr = VLC_TS_UNKNOWN;
588         p_stream->i_previous_granulepos = -1;
589         ogg_stream_reset( &p_stream->os );
590     }
591     ogg_sync_reset( &p_sys->oy );
592 }
593
594 static logical_stream_t * Ogg_GetSelectedStream( demux_t *p_demux )
595 {
596     demux_sys_t *p_sys = p_demux->p_sys;
597     logical_stream_t *p_stream = NULL;
598     for( int i=0; i<p_sys->i_streams; i++ )
599     {
600         logical_stream_t *p_candidate = p_sys->pp_stream[i];
601         if ( !p_candidate->p_es ) continue;
602
603         bool b_selected = false;
604         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
605                         p_candidate->p_es, &b_selected );
606         if ( !b_selected ) continue;
607
608         if ( !p_stream && p_candidate->fmt.i_cat == AUDIO_ES )
609         {
610             p_stream = p_candidate;
611             continue; /* Try to find video anyway */
612         }
613
614         if ( p_candidate->fmt.i_cat == VIDEO_ES )
615         {
616             p_stream = p_candidate;
617             break;
618         }
619     }
620     return p_stream;
621 }
622
623 /*****************************************************************************
624  * Control:
625  *****************************************************************************/
626 static int Control( demux_t *p_demux, int i_query, va_list args )
627 {
628     demux_sys_t *p_sys  = p_demux->p_sys;
629     vlc_meta_t *p_meta;
630     int64_t *pi64, i64;
631     double *pf, f;
632     bool *pb_bool, b;
633
634     switch( i_query )
635     {
636         case DEMUX_GET_META:
637             p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
638             if( p_sys->p_meta )
639                 vlc_meta_Merge( p_meta, p_sys->p_meta );
640             return VLC_SUCCESS;
641
642         case DEMUX_HAS_UNSUPPORTED_META:
643             pb_bool = (bool*)va_arg( args, bool* );
644             *pb_bool = true;
645             return VLC_SUCCESS;
646
647         case DEMUX_GET_TIME:
648             pi64 = (int64_t*)va_arg( args, int64_t * );
649             *pi64 = p_sys->i_pcr;
650             return VLC_SUCCESS;
651
652         case DEMUX_SET_TIME:
653             i64 = (int64_t)va_arg( args, int64_t );
654             logical_stream_t *p_stream = Ogg_GetSelectedStream( p_demux );
655             if ( !p_stream )
656             {
657                 msg_Err( p_demux, "No selected seekable stream found" );
658                 return VLC_EGENERIC;
659             }
660             stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b );
661             if ( Oggseek_BlindSeektoAbsoluteTime( p_demux, p_stream, i64, b ) )
662             {
663                 Ogg_ResetStreamHelper( p_sys );
664                 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
665                                 VLC_TS_0 + i64 );
666                 return VLC_SUCCESS;
667             }
668             else
669                 return VLC_EGENERIC;
670
671         case DEMUX_GET_ATTACHMENTS:
672         {
673             input_attachment_t ***ppp_attach =
674                 (input_attachment_t***)va_arg( args, input_attachment_t*** );
675             int *pi_int = (int*)va_arg( args, int * );
676
677             if( p_sys->i_attachments <= 0 )
678                 return VLC_EGENERIC;
679
680             *pi_int = p_sys->i_attachments;
681             *ppp_attach = xmalloc( sizeof(input_attachment_t*) * p_sys->i_attachments );
682             for( int i = 0; i < p_sys->i_attachments; i++ )
683                 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
684             return VLC_SUCCESS;
685         }
686
687         case DEMUX_GET_POSITION:
688             pf = (double*)va_arg( args, double * );
689             if( p_sys->i_length > 0 )
690             {
691                 *pf =  (double) p_sys->i_pcr /
692                        (double) ( p_sys->i_length * (mtime_t)1000000 );
693             }
694             else if( stream_Size( p_demux->s ) > 0 )
695             {
696                 i64 = stream_Tell( p_demux->s );
697                 *pf = (double) i64 / stream_Size( p_demux->s );
698             }
699             else *pf = 0.0;
700             return VLC_SUCCESS;
701
702         case DEMUX_SET_POSITION:
703             /* forbid seeking if we haven't initialized all logical bitstreams yet;
704                if we allowed, some headers would not get backed up and decoder init
705                would fail, making that logical stream unusable */
706             for ( int i=0; i< p_sys->i_streams; i++ )
707             {
708                 if ( p_sys->pp_stream[i]->b_initializing )
709                     return VLC_EGENERIC;
710             }
711
712             p_stream = Ogg_GetSelectedStream( p_demux );
713             if ( !p_stream )
714             {
715                 msg_Err( p_demux, "No selected seekable stream found" );
716                 return VLC_EGENERIC;
717             }
718
719             stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b );
720
721             f = (double)va_arg( args, double );
722             if ( p_sys->i_length <= 0 || !b /* || ! ACCESS_CAN_FASTSEEK */ )
723             {
724                 Ogg_ResetStreamHelper( p_sys );
725                 Oggseek_BlindSeektoPosition( p_demux, p_stream, f, b );
726                 return VLC_SUCCESS;
727             }
728
729             assert( p_sys->i_length > 0 );
730             i64 = CLOCK_FREQ * p_sys->i_length * f;
731             Ogg_ResetStreamHelper( p_sys );
732             if ( Oggseek_SeektoAbsolutetime( p_demux, p_stream, i64 ) >= 0 )
733             {
734                 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
735                                 VLC_TS_0 + i64 );
736                 return VLC_SUCCESS;
737             }
738
739             return VLC_EGENERIC;
740
741         case DEMUX_GET_LENGTH:
742             if ( p_sys->i_length < 0 )
743                 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
744                                               1, i_query, args );
745             pi64 = (int64_t*)va_arg( args, int64_t * );
746             *pi64 = p_sys->i_length * 1000000;
747             return VLC_SUCCESS;
748
749         case DEMUX_GET_TITLE_INFO:
750         {
751             input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
752             int *pi_int    = (int*)va_arg( args, int* );
753             int *pi_title_offset = (int*)va_arg( args, int* );
754             int *pi_seekpoint_offset = (int*)va_arg( args, int* );
755
756             if( p_sys->i_seekpoints > 0 )
757             {
758                 *pi_int = 1;
759                 *ppp_title = malloc( sizeof( input_title_t* ) );
760                 input_title_t *p_title = (*ppp_title)[0] = vlc_input_title_New();
761                 for( int i = 0; i < p_sys->i_seekpoints; i++ )
762                 {
763                     seekpoint_t *p_seekpoint_copy = vlc_seekpoint_Duplicate( p_sys->pp_seekpoints[i] );
764                     if ( likely( p_seekpoint_copy ) )
765                         TAB_APPEND( p_title->i_seekpoint, p_title->seekpoint, p_seekpoint_copy );
766                 }
767                 *pi_title_offset = 0;
768                 *pi_seekpoint_offset = 0;
769             }
770             return VLC_SUCCESS;
771         }
772         case DEMUX_SET_TITLE:
773         {
774             const int i_title = (int)va_arg( args, int );
775             if( i_title > 1 )
776                 return VLC_EGENERIC;
777             return VLC_SUCCESS;
778         }
779         case DEMUX_SET_SEEKPOINT:
780         {
781             const int i_seekpoint = (int)va_arg( args, int );
782             if( i_seekpoint > p_sys->i_seekpoints )
783                 return VLC_EGENERIC;
784
785             for ( int i=0; i< p_sys->i_streams; i++ )
786             {
787                 if ( p_sys->pp_stream[i]->b_initializing )
788                     return VLC_EGENERIC;
789             }
790
791             i64 = p_sys->pp_seekpoints[i_seekpoint]->i_time_offset;
792
793             p_stream = Ogg_GetSelectedStream( p_demux );
794             if ( !p_stream )
795             {
796                 msg_Err( p_demux, "No selected seekable stream found" );
797                 return VLC_EGENERIC;
798             }
799
800             stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b );
801             if ( Oggseek_BlindSeektoAbsoluteTime( p_demux, p_stream, i64, b ) )
802             {
803                 Ogg_ResetStreamHelper( p_sys );
804                 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
805                                 VLC_TS_0 + i64 );
806                 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
807                 p_demux->info.i_seekpoint = i_seekpoint;
808                 return VLC_SUCCESS;
809             }
810             else
811                 return VLC_EGENERIC;
812         }
813
814         default:
815             return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
816                                            1, i_query, args );
817     }
818 }
819
820 /****************************************************************************
821  * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
822  ****************************************************************************
823  * Returns VLC_SUCCESS if a page has been read. An error might happen if we
824  * are at the end of stream.
825  ****************************************************************************/
826 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
827 {
828     demux_sys_t *p_ogg = p_demux->p_sys  ;
829     int i_read = 0;
830     char *p_buffer;
831
832     while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
833     {
834         p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
835
836         i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
837         if( i_read <= 0 )
838             return VLC_EGENERIC;
839
840         ogg_sync_wrote( &p_ogg->oy, i_read );
841     }
842
843     return VLC_SUCCESS;
844 }
845
846 /****************************************************************************
847  * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
848  *                current stream.
849  ****************************************************************************/
850 static void Ogg_UpdatePCR( demux_t *p_demux, logical_stream_t *p_stream,
851                            ogg_packet *p_oggpacket )
852 {
853     demux_sys_t *p_ogg = p_demux->p_sys;
854     p_stream->i_end_trim = 0;
855
856     /* Convert the granulepos into a pcr */
857     if ( p_oggpacket->granulepos == 0 )
858     {
859         /* We're in headers, and we haven't parsed 1st data packet yet */
860         p_stream->i_pcr = VLC_TS_UNKNOWN;
861         p_stream->i_interpolated_pcr = VLC_TS_UNKNOWN;
862     }
863     else if( p_oggpacket->granulepos > 0 )
864     {
865         if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
866             p_stream->fmt.i_codec == VLC_CODEC_KATE ||
867             p_stream->fmt.i_codec == VLC_CODEC_VP8 ||
868             p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
869         {
870             p_stream->i_pcr = Oggseek_GranuleToAbsTimestamp( p_stream,
871                                                 p_oggpacket->granulepos, true );
872         }
873         else
874         {
875             ogg_int64_t sample;
876             sample = p_oggpacket->granulepos;
877             if( p_oggpacket->e_o_s &&
878                 p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
879                 p_stream->i_previous_granulepos >= 0 )
880             {
881                 int duration;
882                 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
883                 if( duration > 0 )
884                 {
885                     ogg_int64_t end_sample;
886                     end_sample = p_stream->i_previous_granulepos + duration;
887                     if( end_sample > sample )
888                         p_stream->i_end_trim = (int)(end_sample - sample);
889                 }
890             }
891             if (sample >= p_stream->i_pre_skip)
892                 sample -= p_stream->i_pre_skip;
893             else
894                 sample = 0;
895             p_stream->i_pcr = sample * CLOCK_FREQ / p_stream->f_rate;
896         }
897
898         if ( !p_ogg->i_pcr_offset )
899             p_stream->i_pcr += VLC_TS_0;
900         else
901             p_stream->i_pcr += p_ogg->i_pcr_offset;
902         p_stream->i_interpolated_pcr = p_stream->i_pcr;
903     }
904     else
905     {
906         int duration;
907         p_stream->i_pcr = VLC_TS_INVALID;
908
909         /* no granulepos available, try to interpolate the pcr.
910          * If we can't then don't touch the old value. */
911         if( p_stream->fmt.i_cat == VIDEO_ES )
912             /* 1 frame per packet */
913             p_stream->i_interpolated_pcr += (CLOCK_FREQ / p_stream->f_rate);
914         else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
915                  p_stream->i_previous_granulepos >= 0 &&
916                  ( duration =
917                      Ogg_OpusPacketDuration( p_stream, p_oggpacket ) ) > 0 )
918         {
919             ogg_int64_t sample;
920             p_oggpacket->granulepos =
921                 p_stream->i_previous_granulepos + duration;
922             sample = p_oggpacket->granulepos;
923             if (sample >= p_stream->i_pre_skip)
924                 sample -= p_stream->i_pre_skip;
925             else
926                 sample = 0;
927             p_stream->i_interpolated_pcr =
928                 VLC_TS_0 + sample * CLOCK_FREQ / p_stream->f_rate;
929             p_stream->i_interpolated_pcr += p_ogg->i_pcr_offset;
930         }
931         else if( p_stream->fmt.i_bitrate && p_stream->i_pcr > VLC_TS_UNKNOWN )
932         {
933             p_stream->i_interpolated_pcr +=
934                 ( p_oggpacket->bytes * CLOCK_FREQ /
935                   p_stream->fmt.i_bitrate / 8 );
936             p_stream->i_interpolated_pcr += p_ogg->i_pcr_offset;
937         }
938     }
939     p_stream->i_previous_granulepos = p_oggpacket->granulepos;
940 }
941
942 /****************************************************************************
943  * Ogg_DecodePacket: Decode an Ogg packet.
944  ****************************************************************************/
945 static void Ogg_DecodePacket( demux_t *p_demux,
946                               logical_stream_t *p_stream,
947                               ogg_packet *p_oggpacket )
948 {
949     block_t *p_block;
950     bool b_selected;
951     int i_header_len = 0;
952     mtime_t i_pts = VLC_TS_UNKNOWN, i_interpolated_pts;
953     demux_sys_t *p_ogg = p_demux->p_sys;
954
955     if( p_oggpacket->bytes >= 7 &&
956         ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
957     {
958         /* it's an Annodex packet -- skip it (do nothing) */
959         return;
960     }
961     else if( p_oggpacket->bytes >= 7 &&
962         ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
963     {
964         /* it's an AnxData packet -- skip it (do nothing) */
965         return;
966     }
967     else if( p_oggpacket->bytes >= 8 &&
968         ! memcmp ( p_oggpacket->packet, "fisbone", 8 ) )
969     {
970         Ogg_ReadSkeletonBones( p_demux, p_oggpacket );
971         return;
972     }
973     else if( p_oggpacket->bytes >= 6 &&
974         ! memcmp ( p_oggpacket->packet, "index", 6 ) )
975     {
976         Ogg_ReadSkeletonIndex( p_demux, p_oggpacket );
977         return;
978     }
979     else if( p_stream->fmt.i_codec == VLC_CODEC_VP8 &&
980              p_oggpacket->bytes >= 7 &&
981              !memcmp( p_oggpacket->packet, "OVP80\x02\x20", 7 ) )
982     {
983         Ogg_ReadVP8Header( p_demux, p_stream, p_oggpacket );
984         return;
985     }
986
987     if( p_stream->fmt.i_codec == VLC_CODEC_SUBT && p_oggpacket->bytes > 0 &&
988         p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
989
990     /* Check the ES is selected */
991     if ( !p_stream->p_es )
992         b_selected = true;
993     else
994         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
995                         p_stream->p_es, &b_selected );
996
997     if( p_stream->b_force_backup )
998     {
999         bool b_xiph;
1000         p_stream->i_packets_backup++;
1001         switch( p_stream->fmt.i_codec )
1002         {
1003         case VLC_CODEC_VORBIS:
1004         case VLC_CODEC_THEORA:
1005             if( p_stream->i_packets_backup == 3 )
1006                 p_stream->b_force_backup = false;
1007             b_xiph = true;
1008             break;
1009
1010         case VLC_CODEC_SPEEX:
1011             if( p_stream->i_packets_backup == 2 + p_stream->i_extra_headers_packets )
1012                 p_stream->b_force_backup = false;
1013             b_xiph = true;
1014             break;
1015
1016         case VLC_CODEC_OPUS:
1017             if( p_stream->i_packets_backup == 2 )
1018                 p_stream->b_force_backup = false;
1019             b_xiph = true;
1020             break;
1021
1022         case VLC_CODEC_FLAC:
1023             if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
1024             {
1025                 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
1026                 p_stream->b_force_backup = false;
1027             }
1028             else if( p_stream->fmt.audio.i_rate )
1029             {
1030                 p_stream->b_force_backup = false;
1031                 if( p_oggpacket->bytes >= 9 )
1032                 {
1033                     p_oggpacket->packet += 9;
1034                     p_oggpacket->bytes -= 9;
1035                 }
1036             }
1037             b_xiph = false;
1038             break;
1039
1040         case VLC_CODEC_KATE:
1041             if( p_stream->i_packets_backup == p_stream->i_kate_num_headers )
1042                 p_stream->b_force_backup = false;
1043             b_xiph = true;
1044             break;
1045
1046         default:
1047             p_stream->b_force_backup = false;
1048             b_xiph = false;
1049             break;
1050         }
1051
1052         /* Backup the ogg packet (likely an header packet) */
1053         if( !b_xiph )
1054         {
1055             void *p_org = p_stream->p_headers;
1056             p_stream->i_headers += p_oggpacket->bytes;
1057             p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
1058             if( p_stream->p_headers )
1059             {
1060                 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
1061                         p_oggpacket->packet, p_oggpacket->bytes );
1062             }
1063             else
1064             {
1065 #warning Memory leak
1066                 p_stream->i_headers = 0;
1067                 p_stream->p_headers = NULL;
1068                 free( p_org );
1069             }
1070         }
1071         else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
1072                                      p_oggpacket->bytes, p_oggpacket->packet ) )
1073         {
1074             p_stream->i_headers = 0;
1075             p_stream->p_headers = NULL;
1076         }
1077         if( p_stream->i_headers > 0 )
1078         {
1079             if( !p_stream->b_force_backup )
1080             {
1081                 /* Last header received, commit changes */
1082                 free( p_stream->fmt.p_extra );
1083
1084                 p_stream->fmt.i_extra = p_stream->i_headers;
1085                 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
1086                 if( p_stream->fmt.p_extra )
1087                     memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
1088                             p_stream->i_headers );
1089                 else
1090                     p_stream->fmt.i_extra = 0;
1091
1092                 if( p_stream->i_headers > 0 )
1093                     Ogg_ExtractMeta( p_demux, & p_stream->fmt,
1094                                      p_stream->p_headers, p_stream->i_headers );
1095
1096                 /* we're not at BOS anymore for this logical stream */
1097                 p_stream->b_initializing = false;
1098             }
1099         }
1100
1101         b_selected = false; /* Discard the header packet */
1102     }
1103     else
1104         p_stream->b_initializing = false;
1105
1106     /* Convert the pcr into a pts */
1107     if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
1108         p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
1109         p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
1110         p_stream->fmt.i_codec == VLC_CODEC_VP8 ||
1111         p_stream->fmt.i_codec == VLC_CODEC_FLAC )
1112     {
1113         if( p_stream->i_pcr > VLC_TS_INVALID )
1114         {
1115             p_stream->i_previous_pcr = p_stream->i_pcr;
1116             /* The granulepos is the end date of the sample */
1117             i_pts = p_stream->i_pcr;
1118         }
1119     }
1120
1121     /* Convert the granulepos into the next pcr */
1122     i_interpolated_pts = p_stream->i_interpolated_pcr;
1123     Ogg_UpdatePCR( p_demux, p_stream, p_oggpacket );
1124
1125     if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
1126         p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
1127         p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
1128         p_stream->fmt.i_codec != VLC_CODEC_VP8 &&
1129         p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
1130         p_stream->i_pcr >= 0 )
1131     {
1132         p_stream->i_previous_pcr = p_stream->i_pcr;
1133
1134         /* The granulepos is the start date of the sample */
1135         i_pts = p_stream->i_pcr;
1136     }
1137
1138     if( !b_selected )
1139     {
1140         /* This stream isn't currently selected so we don't need to decode it,
1141          * but we did need to store its pcr as it might be selected later on */
1142         return;
1143     }
1144
1145     if( !( p_block = block_Alloc( p_oggpacket->bytes ) ) ) return;
1146
1147
1148     /* may need to preroll after a seek */
1149     if ( p_stream->i_skip_frames > 0 )
1150     {
1151         if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
1152         {
1153             int duration;
1154             duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
1155             if( p_stream->i_skip_frames > duration )
1156             {
1157                 p_block->i_flags |= BLOCK_FLAG_PREROLL;
1158                 p_block->i_nb_samples = 0;
1159                 p_stream->i_skip_frames -= duration;
1160             }
1161             else
1162             {
1163                 p_block->i_nb_samples = duration - p_stream->i_skip_frames;
1164                 if( p_stream->i_previous_granulepos >=
1165                     p_block->i_nb_samples + p_stream->i_pre_skip )
1166                 {
1167                     i_pts = VLC_TS_0 + (p_stream->i_previous_granulepos
1168                         - p_block->i_nb_samples - p_stream->i_pre_skip) *
1169                         CLOCK_FREQ / p_stream->f_rate;
1170                 }
1171                 p_stream->i_skip_frames = 0;
1172             }
1173         }
1174         else
1175         {
1176             p_block->i_flags |= BLOCK_FLAG_PREROLL;
1177             p_stream->i_skip_frames--;
1178         }
1179     }
1180     else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
1181         p_block->i_nb_samples = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
1182
1183     /* Normalize PTS */
1184     if( i_pts == VLC_TS_UNKNOWN )
1185         i_pts = VLC_TS_INVALID;
1186
1187     if( p_stream->fmt.i_cat == AUDIO_ES )
1188     {
1189         p_block->i_dts = p_block->i_pts = i_pts;
1190         /* Blatant abuse of the i_length field. */
1191         p_block->i_length = p_stream->i_end_trim;
1192     }
1193     else if( p_stream->fmt.i_cat == SPU_ES )
1194     {
1195         p_block->i_dts = p_block->i_pts = i_pts;
1196         p_block->i_length = 0;
1197     }
1198     else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
1199     {
1200         p_block->i_dts = p_block->i_pts = i_pts;
1201         if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
1202         {
1203             p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1204         }
1205     }
1206     else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
1207     {
1208         ogg_int64_t dts = p_oggpacket->granulepos >> 31;
1209         ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
1210
1211         uint64_t u_pnum = dts + delay;
1212
1213         p_block->i_dts = p_stream->i_pcr;
1214         p_block->i_pts = VLC_TS_INVALID;
1215         /* NB, OggDirac granulepos values are in units of 2*picturerate */
1216
1217         /* granulepos for dirac is possibly broken, this value should be ignored */
1218         if( -1 != p_oggpacket->granulepos )
1219             p_block->i_pts = u_pnum * CLOCK_FREQ / p_stream->f_rate / 2;
1220     }
1221     else if( p_stream->fmt.i_codec == VLC_CODEC_VP8 )
1222     {
1223         p_block->i_pts = p_stream->i_interpolated_pcr;
1224         p_block->i_dts = p_block->i_pts;
1225         if( p_oggpacket->granulepos > 0 && Ogg_IsKeyFrame( p_stream, p_oggpacket ) )
1226         {
1227             p_block->i_flags |= BLOCK_FLAG_TYPE_I;
1228         }
1229     }
1230     else
1231     {
1232         p_block->i_dts = i_pts;
1233         p_block->i_pts = VLC_TS_INVALID;
1234     }
1235
1236     if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
1237         p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
1238         p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
1239         p_stream->fmt.i_codec != VLC_CODEC_VP8 &&
1240         p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
1241         p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
1242         p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
1243         p_stream->fmt.i_codec != VLC_CODEC_CMML &&
1244         p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
1245         p_stream->fmt.i_codec != VLC_CODEC_KATE )
1246     {
1247         if( p_oggpacket->bytes <= 0 )
1248         {
1249             msg_Dbg( p_demux, "discarding 0 sized packet" );
1250             block_Release( p_block );
1251             return;
1252         }
1253         /* We remove the header from the packet */
1254         i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
1255         i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
1256
1257         if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
1258         {
1259             /* But with subtitles we need to retrieve the duration first */
1260             int i, lenbytes = 0;
1261
1262             if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
1263             {
1264                 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
1265                 {
1266                     lenbytes = lenbytes << 8;
1267                     lenbytes += *(p_oggpacket->packet + i_header_len - i);
1268                 }
1269             }
1270             if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
1271                 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
1272                   p_oggpacket->packet[i_header_len + 1] != 0 &&
1273                   p_oggpacket->packet[i_header_len + 1] != '\n' &&
1274                   p_oggpacket->packet[i_header_len + 1] != '\r' ) )
1275             {
1276                 p_block->i_length = (mtime_t)lenbytes * 1000;
1277             }
1278         }
1279
1280         i_header_len++;
1281         if( p_block->i_buffer >= (unsigned int)i_header_len )
1282             p_block->i_buffer -= i_header_len;
1283         else
1284             p_block->i_buffer = 0;
1285     }
1286
1287
1288     if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
1289     {
1290         /* FIXME: the biggest hack I've ever done */
1291         msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
1292                   p_block->i_pts, p_block->i_dts );
1293         msleep(10000);
1294     }
1295
1296     memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
1297             p_oggpacket->bytes - i_header_len );
1298
1299     if ( p_ogg->i_streams == 1 && p_block->i_pts > VLC_TS_INVALID && p_ogg->i_pcr < VLC_TS_0 )
1300     {
1301         p_ogg->i_pcr = p_block->i_pts;
1302         es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_ogg->i_pcr );
1303     }
1304
1305     if ( p_stream->p_es )
1306     {
1307         /* Because ES creation is delayed for preparsing */
1308         if ( p_stream->p_preparse_block )
1309         {
1310             es_out_Send( p_demux->out, p_stream->p_es, p_stream->p_preparse_block );
1311             p_stream->p_preparse_block = NULL;
1312         }
1313         es_out_Send( p_demux->out, p_stream->p_es, p_block );
1314     }
1315     else
1316         block_ChainAppend( & p_stream->p_preparse_block, p_block );
1317 }
1318
1319 /* Re-implemented to avoid linking against libopus from the demuxer. */
1320 static int Ogg_OpusPacketDuration( logical_stream_t *p_stream,
1321                                    ogg_packet *p_oggpacket )
1322 {
1323     static const int silk_fs_div[4] = { 6000, 3000, 1500, 1000 };
1324     int toc;
1325     int nframes;
1326     int frame_size;
1327     int nsamples;
1328     int i_rate;
1329     if( p_oggpacket->bytes < 1 )
1330         return VLC_EGENERIC;
1331     toc = p_oggpacket->packet[0];
1332     switch( toc&3 )
1333     {
1334         case 0:
1335             nframes = 1;
1336             break;
1337         case 1:
1338         case 2:
1339             nframes = 2;
1340             break;
1341         default:
1342             if( p_oggpacket->bytes < 2 )
1343                 return VLC_EGENERIC;
1344             nframes = p_oggpacket->packet[1]&0x3F;
1345             break;
1346     }
1347     i_rate = (int)p_stream->fmt.audio.i_rate;
1348     if( toc&0x80 )
1349         frame_size = (i_rate << (toc >> 3 & 3)) / 400;
1350     else if( ( toc&0x60 ) == 0x60 )
1351         frame_size = i_rate/(100 >> (toc >> 3 & 1));
1352     else
1353         frame_size = i_rate*60 / silk_fs_div[toc >> 3 & 3];
1354     nsamples = nframes*frame_size;
1355     if( nsamples*25 > i_rate*3 )
1356         return VLC_EGENERIC;
1357     return nsamples;
1358 }
1359
1360 /****************************************************************************
1361  * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
1362  *                         stream and fill p_ogg.
1363  *****************************************************************************
1364  * The initial page of a logical stream is marked as a 'bos' page.
1365  * Furthermore, the Ogg specification mandates that grouped bitstreams begin
1366  * together and all of the initial pages must appear before any data pages.
1367  *
1368  * On success this function returns VLC_SUCCESS.
1369  ****************************************************************************/
1370 static int Ogg_FindLogicalStreams( demux_t *p_demux )
1371 {
1372     demux_sys_t *p_ogg = p_demux->p_sys  ;
1373     ogg_packet oggpacket;
1374     int i_stream = 0;
1375
1376     p_ogg->i_total_length = stream_Size ( p_demux->s );
1377     msg_Dbg( p_demux, "File length is %"PRId64" bytes", p_ogg->i_total_length );
1378
1379
1380     while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
1381     {
1382
1383         if( ogg_page_bos( &p_ogg->current_page ) )
1384         {
1385
1386             /* All is wonderful in our fine fine little world.
1387              * We found the beginning of our first logical stream. */
1388             while( ogg_page_bos( &p_ogg->current_page ) )
1389             {
1390                 logical_stream_t *p_stream;
1391
1392                 p_stream = malloc( sizeof(logical_stream_t) );
1393                 if( unlikely( !p_stream ) )
1394                     return VLC_ENOMEM;
1395
1396                 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
1397
1398                 memset( p_stream, 0, sizeof(logical_stream_t) );
1399
1400                 es_format_Init( &p_stream->fmt, 0, 0 );
1401                 es_format_Init( &p_stream->fmt_old, 0, 0 );
1402                 p_stream->b_initializing = true;
1403
1404                 /* Setup the logical stream */
1405                 p_stream->i_serial_no = ogg_page_serialno( &p_ogg->current_page );
1406                 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
1407
1408                 /* Extract the initial header from the first page and verify
1409                  * the codec type of this Ogg bitstream */
1410                 if( ogg_stream_pagein( &p_stream->os, &p_ogg->current_page ) < 0 )
1411                 {
1412                     /* error. stream version mismatch perhaps */
1413                     msg_Err( p_demux, "error reading first page of "
1414                              "Ogg bitstream data" );
1415                     return VLC_EGENERIC;
1416                 }
1417
1418                 /* FIXME: check return value */
1419                 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
1420
1421                 /* Check for Vorbis header */
1422                 if( oggpacket.bytes >= 7 &&
1423                     ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
1424                 {
1425                     Ogg_ReadVorbisHeader( p_stream, &oggpacket );
1426                     msg_Dbg( p_demux, "found vorbis header" );
1427                 }
1428                 /* Check for Speex header */
1429                 else if( oggpacket.bytes >= 5 &&
1430                     ! memcmp( oggpacket.packet, "Speex", 5 ) )
1431                 {
1432                     Ogg_ReadSpeexHeader( p_stream, &oggpacket );
1433                     msg_Dbg( p_demux, "found speex header, channels: %i, "
1434                              "rate: %i,  bitrate: %i",
1435                              p_stream->fmt.audio.i_channels,
1436                              (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
1437                 }
1438                 /* Check for Opus header */
1439                 else if( oggpacket.bytes >= 8 &&
1440                     ! memcmp( oggpacket.packet, "OpusHead", 8 ) )
1441                 {
1442                     Ogg_ReadOpusHeader( p_stream, &oggpacket );
1443                     msg_Dbg( p_demux, "found opus header, channels: %i, "
1444                              "pre-skip: %i",
1445                              p_stream->fmt.audio.i_channels,
1446                              (int)p_stream->i_pre_skip);
1447                     p_stream->i_skip_frames = p_stream->i_pre_skip;
1448                 }
1449                 /* Check for Flac header (< version 1.1.1) */
1450                 else if( oggpacket.bytes >= 4 &&
1451                     ! memcmp( oggpacket.packet, "fLaC", 4 ) )
1452                 {
1453                     msg_Dbg( p_demux, "found FLAC header" );
1454
1455                     /* Grrrr!!!! Did they really have to put all the
1456                      * important info in the second header packet!!!
1457                      * (STREAMINFO metadata is in the following packet) */
1458                     p_stream->b_force_backup = true;
1459
1460                     p_stream->fmt.i_cat = AUDIO_ES;
1461                     p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1462                 }
1463                 /* Check for Flac header (>= version 1.1.1) */
1464                 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
1465                     ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
1466                     ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
1467                 {
1468                     int i_packets = ((int)oggpacket.packet[7]) << 8 |
1469                         oggpacket.packet[8];
1470                     msg_Dbg( p_demux, "found FLAC header version %i.%i "
1471                              "(%i header packets)",
1472                              oggpacket.packet[5], oggpacket.packet[6],
1473                              i_packets );
1474
1475                     p_stream->b_force_backup = true;
1476
1477                     p_stream->fmt.i_cat = AUDIO_ES;
1478                     p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1479                     oggpacket.packet += 13; oggpacket.bytes -= 13;
1480                     Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
1481                 }
1482                 /* Check for Theora header */
1483                 else if( oggpacket.bytes >= 7 &&
1484                          ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
1485                 {
1486                     Ogg_ReadTheoraHeader( p_stream, &oggpacket );
1487
1488                     msg_Dbg( p_demux,
1489                              "found theora header, bitrate: %i, rate: %f",
1490                              p_stream->fmt.i_bitrate, p_stream->f_rate );
1491                 }
1492                 /* Check for Dirac header */
1493                 else if( ( oggpacket.bytes >= 5 &&
1494                            ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
1495                          ( oggpacket.bytes >= 9 &&
1496                            ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
1497                 {
1498                     if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1499                         msg_Dbg( p_demux, "found dirac header" );
1500                     else
1501                     {
1502                         msg_Warn( p_demux, "found dirac header isn't decodable" );
1503                         free( p_stream );
1504                         p_ogg->i_streams--;
1505                     }
1506                 }
1507                 /* Check for Tarkin header */
1508                 else if( oggpacket.bytes >= 7 &&
1509                          ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1510                 {
1511                     oggpack_buffer opb;
1512
1513                     msg_Dbg( p_demux, "found tarkin header" );
1514                     p_stream->fmt.i_cat = VIDEO_ES;
1515                     p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1516
1517                     /* Cheat and get additionnal info ;) */
1518                     oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1519                     oggpack_adv( &opb, 88 );
1520                     oggpack_adv( &opb, 104 );
1521                     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1522                     p_stream->f_rate = 2; /* FIXME */
1523                     msg_Dbg( p_demux,
1524                              "found tarkin header, bitrate: %i, rate: %f",
1525                              p_stream->fmt.i_bitrate, p_stream->f_rate );
1526                 }
1527                 /* Check for VP8 header */
1528                 else if( oggpacket.bytes >= 26 &&
1529                          ! memcmp( oggpacket.packet, "OVP80", 5 ) )
1530                 {
1531                     if ( Ogg_ReadVP8Header( p_demux, p_stream, &oggpacket ) )
1532                         msg_Dbg( p_demux, "found VP8 header "
1533                              "fps: %f, width:%i; height:%i",
1534                              p_stream->f_rate,
1535                              p_stream->fmt.video.i_width,
1536                              p_stream->fmt.video.i_height );
1537                     else
1538                         p_stream->fmt.i_cat = UNKNOWN_ES;
1539                 }
1540                 /* Check for Annodex header */
1541                 else if( oggpacket.bytes >= 7 &&
1542                          ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1543                 {
1544                     Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1545                     /* kill annodex track */
1546                     free( p_stream );
1547                     p_ogg->i_streams--;
1548                 }
1549                 /* Check for Annodex header */
1550                 else if( oggpacket.bytes >= 7 &&
1551                          ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1552                 {
1553                     Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1554                 }
1555                 /* Check for Kate header */
1556                 else if( oggpacket.bytes >= 8 &&
1557                     ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1558                 {
1559                     Ogg_ReadKateHeader( p_stream, &oggpacket );
1560                     msg_Dbg( p_demux, "found kate header" );
1561                 }
1562                 /* Check for OggDS */
1563                 else if( oggpacket.bytes >= 142 &&
1564                          !memcmp( &oggpacket.packet[1],
1565                                    "Direct Show Samples embedded in Ogg", 35 ))
1566                 {
1567                     /* Old header type */
1568                     p_stream->b_oggds = true;
1569                     /* Check for video header (old format) */
1570                     if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1571                         oggpacket.bytes >= 184 )
1572                     {
1573                         p_stream->fmt.i_cat = VIDEO_ES;
1574                         p_stream->fmt.i_codec =
1575                             VLC_FOURCC( oggpacket.packet[68],
1576                                         oggpacket.packet[69],
1577                                         oggpacket.packet[70],
1578                                         oggpacket.packet[71] );
1579                         msg_Dbg( p_demux, "found video header of type: %.4s",
1580                                  (char *)&p_stream->fmt.i_codec );
1581
1582                         p_stream->fmt.video.i_frame_rate = 10000000;
1583                         p_stream->fmt.video.i_frame_rate_base =
1584                             GetQWLE((oggpacket.packet+164));
1585                         p_stream->f_rate = 10000000.0 /
1586                             GetQWLE((oggpacket.packet+164));
1587                         p_stream->fmt.video.i_bits_per_pixel =
1588                             GetWLE((oggpacket.packet+182));
1589                         if( !p_stream->fmt.video.i_bits_per_pixel )
1590                             /* hack, FIXME */
1591                             p_stream->fmt.video.i_bits_per_pixel = 24;
1592                         p_stream->fmt.video.i_width =
1593                             GetDWLE((oggpacket.packet+176));
1594                         p_stream->fmt.video.i_height =
1595                             GetDWLE((oggpacket.packet+180));
1596
1597                         msg_Dbg( p_demux,
1598                                  "fps: %f, width:%i; height:%i, bitcount:%i",
1599                                  p_stream->f_rate,
1600                                  p_stream->fmt.video.i_width,
1601                                  p_stream->fmt.video.i_height,
1602                                  p_stream->fmt.video.i_bits_per_pixel);
1603
1604                     }
1605                     /* Check for audio header (old format) */
1606                     else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1607                     {
1608                         int i_extra_size;
1609                         unsigned int i_format_tag;
1610
1611                         p_stream->fmt.i_cat = AUDIO_ES;
1612
1613                         i_extra_size = GetWLE((oggpacket.packet+140));
1614                         if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1615                         {
1616                             p_stream->fmt.i_extra = i_extra_size;
1617                             p_stream->fmt.p_extra = malloc( i_extra_size );
1618                             if( p_stream->fmt.p_extra )
1619                                 memcpy( p_stream->fmt.p_extra,
1620                                         oggpacket.packet + 142, i_extra_size );
1621                             else
1622                                 p_stream->fmt.i_extra = 0;
1623                         }
1624
1625                         i_format_tag = GetWLE((oggpacket.packet+124));
1626                         p_stream->fmt.audio.i_channels =
1627                             GetWLE((oggpacket.packet+126));
1628                         fill_channels_info(&p_stream->fmt.audio);
1629                         p_stream->f_rate = p_stream->fmt.audio.i_rate =
1630                             GetDWLE((oggpacket.packet+128));
1631                         p_stream->fmt.i_bitrate =
1632                             GetDWLE((oggpacket.packet+132)) * 8;
1633                         p_stream->fmt.audio.i_blockalign =
1634                             GetWLE((oggpacket.packet+136));
1635                         p_stream->fmt.audio.i_bitspersample =
1636                             GetWLE((oggpacket.packet+138));
1637
1638                         wf_tag_to_fourcc( i_format_tag,
1639                                           &p_stream->fmt.i_codec, 0 );
1640
1641                         if( p_stream->fmt.i_codec ==
1642                             VLC_FOURCC('u','n','d','f') )
1643                         {
1644                             p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1645                                 ( i_format_tag >> 8 ) & 0xff,
1646                                 i_format_tag & 0xff );
1647                         }
1648
1649                         msg_Dbg( p_demux, "found audio header of type: %.4s",
1650                                  (char *)&p_stream->fmt.i_codec );
1651                         msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1652                                  "%dbits/sample %dkb/s",
1653                                  i_format_tag,
1654                                  p_stream->fmt.audio.i_channels,
1655                                  p_stream->fmt.audio.i_rate,
1656                                  p_stream->fmt.audio.i_bitspersample,
1657                                  p_stream->fmt.i_bitrate / 1024 );
1658
1659                     }
1660                     else
1661                     {
1662                         msg_Dbg( p_demux, "stream %d has an old header "
1663                             "but is of an unknown type", p_ogg->i_streams-1 );
1664                         free( p_stream );
1665                         p_ogg->i_streams--;
1666                     }
1667                 }
1668                 /* Check for OggDS */
1669                 else if( oggpacket.bytes >= 44+1 &&
1670                          (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER )
1671                 {
1672                     stream_header_t tmp;
1673                     stream_header_t *st = &tmp;
1674
1675                     p_stream->b_oggds = true;
1676
1677                     memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1678                     memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1679                     st->size = GetDWLE( &oggpacket.packet[1+12] );
1680                     st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1681                     st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1682                     st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1683                     st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1684                     st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1685
1686                     /* Check for video header (new format) */
1687                     if( !strncmp( st->streamtype, "video", 5 ) &&
1688                         oggpacket.bytes >= 52+1 )
1689                     {
1690                         st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1691                         st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1692
1693                         p_stream->fmt.i_cat = VIDEO_ES;
1694
1695                         /* We need to get rid of the header packet */
1696                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1697
1698                         p_stream->fmt.i_codec =
1699                             VLC_FOURCC( st->subtype[0], st->subtype[1],
1700                                         st->subtype[2], st->subtype[3] );
1701                         msg_Dbg( p_demux, "found video header of type: %.4s",
1702                                  (char *)&p_stream->fmt.i_codec );
1703
1704                         p_stream->fmt.video.i_frame_rate = 10000000;
1705                         p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1706                         if( st->time_unit <= 0 )
1707                             st->time_unit = 400000;
1708                         p_stream->f_rate = 10000000.0 / st->time_unit;
1709                         p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1710                         p_stream->fmt.video.i_width = st->sh.video.width;
1711                         p_stream->fmt.video.i_height = st->sh.video.height;
1712
1713                         msg_Dbg( p_demux,
1714                                  "fps: %f, width:%i; height:%i, bitcount:%i",
1715                                  p_stream->f_rate,
1716                                  p_stream->fmt.video.i_width,
1717                                  p_stream->fmt.video.i_height,
1718                                  p_stream->fmt.video.i_bits_per_pixel );
1719                     }
1720                     /* Check for audio header (new format) */
1721                     else if( !strncmp( st->streamtype, "audio", 5 ) &&
1722                              oggpacket.bytes >= 56+1 )
1723                     {
1724                         char p_buffer[5];
1725                         int i_extra_size;
1726                         int i_format_tag;
1727
1728                         st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1729                         st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1730                         st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1731
1732                         p_stream->fmt.i_cat = AUDIO_ES;
1733
1734                         /* We need to get rid of the header packet */
1735                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1736
1737                         i_extra_size = st->size - 56;
1738
1739                         if( i_extra_size > 0 &&
1740                             i_extra_size < oggpacket.bytes - 1 - 56 )
1741                         {
1742                             p_stream->fmt.i_extra = i_extra_size;
1743                             p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1744                             if( p_stream->fmt.p_extra )
1745                                 memcpy( p_stream->fmt.p_extra, oggpacket.packet + 57,
1746                                         p_stream->fmt.i_extra );
1747                             else
1748                                 p_stream->fmt.i_extra = 0;
1749                         }
1750
1751                         memcpy( p_buffer, st->subtype, 4 );
1752                         p_buffer[4] = '\0';
1753                         i_format_tag = strtol(p_buffer,NULL,16);
1754                         p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1755                         fill_channels_info(&p_stream->fmt.audio);
1756                         if( st->time_unit <= 0 )
1757                             st->time_unit = 10000000;
1758                         p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1759                         p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1760                         p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1761                         p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1762
1763                         wf_tag_to_fourcc( i_format_tag,
1764                                           &p_stream->fmt.i_codec, 0 );
1765
1766                         if( p_stream->fmt.i_codec ==
1767                             VLC_FOURCC('u','n','d','f') )
1768                         {
1769                             p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1770                                 ( i_format_tag >> 8 ) & 0xff,
1771                                 i_format_tag & 0xff );
1772                         }
1773
1774                         msg_Dbg( p_demux, "found audio header of type: %.4s",
1775                                  (char *)&p_stream->fmt.i_codec );
1776                         msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1777                                  "%dbits/sample %dkb/s",
1778                                  i_format_tag,
1779                                  p_stream->fmt.audio.i_channels,
1780                                  p_stream->fmt.audio.i_rate,
1781                                  p_stream->fmt.audio.i_bitspersample,
1782                                  p_stream->fmt.i_bitrate / 1024 );
1783                     }
1784                     /* Check for text (subtitles) header */
1785                     else if( !strncmp(st->streamtype, "text", 4) )
1786                     {
1787                         /* We need to get rid of the header packet */
1788                         ogg_stream_packetout( &p_stream->os, &oggpacket );
1789
1790                         msg_Dbg( p_demux, "found text subtitle header" );
1791                         p_stream->fmt.i_cat = SPU_ES;
1792                         p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1793                         p_stream->f_rate = 1000; /* granulepos is in millisec */
1794                     }
1795                     else
1796                     {
1797                         msg_Dbg( p_demux, "stream %d has a header marker "
1798                             "but is of an unknown type", p_ogg->i_streams-1 );
1799                         free( p_stream );
1800                         p_ogg->i_streams--;
1801                     }
1802                 }
1803                 else if( oggpacket.bytes >= 8 &&
1804                              ! memcmp( oggpacket.packet, "fishead\0", 8 ) )
1805
1806                 {
1807                     /* Skeleton */
1808                     msg_Dbg( p_demux, "stream %d is a skeleton",
1809                                 p_ogg->i_streams-1 );
1810                     Ogg_ReadSkeletonHeader( p_demux, p_stream, &oggpacket );
1811                 }
1812                 else
1813                 {
1814                     msg_Dbg( p_demux, "stream %d is of unknown type",
1815                              p_ogg->i_streams-1 );
1816                     free( p_stream );
1817                     p_ogg->i_streams--;
1818                 }
1819
1820                 /* we'll need to get all headers */
1821                 p_ogg->pp_stream[i_stream]->b_initializing &= p_ogg->pp_stream[i_stream]->b_force_backup;
1822
1823                 if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
1824                     return VLC_EGENERIC;
1825             }
1826
1827             /* This is the first data page, which means we are now finished
1828              * with the initial pages. We just need to store it in the relevant
1829              * bitstream. */
1830             for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1831             {
1832                 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1833                                        &p_ogg->current_page ) == 0 )
1834                 {
1835                     p_ogg->b_page_waiting = true;
1836                     break;
1837                 }
1838             }
1839
1840             return VLC_SUCCESS;
1841         }
1842     }
1843
1844     return VLC_EGENERIC;
1845 }
1846
1847 /****************************************************************************
1848  * Ogg_CreateES: Creates all Elementary streams once headers are parsed
1849  ****************************************************************************/
1850 static void Ogg_CreateES( demux_t *p_demux )
1851 {
1852     demux_sys_t *p_ogg = p_demux->p_sys;
1853     logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1854     int i_stream;
1855
1856     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1857     {
1858         logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1859
1860         if ( p_stream->p_es == NULL && !p_stream->b_finished )
1861         {
1862             /* Better be safe than sorry when possible with ogm */
1863             if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1864                 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1865                 p_stream->fmt.b_packetized = false;
1866
1867             /* Try first to reuse an old ES */
1868             if( p_old_stream &&
1869                 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1870                 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1871             {
1872                 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1873
1874                 p_stream->p_es = p_old_stream->p_es;
1875                 p_stream->b_finished = false;
1876                 p_stream->b_reinit = false;
1877                 p_stream->b_initializing = false;
1878                 bool b_resetdecoder = Ogg_LogicalStreamResetEsFormat( p_demux, p_stream );
1879                 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1880
1881                 p_old_stream->p_es = NULL;
1882                 p_old_stream = NULL;
1883                 if ( b_resetdecoder )
1884                 {
1885                     es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
1886                                     p_stream->p_es, &p_stream->fmt );
1887                 }
1888             }
1889             else
1890             {
1891                 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1892             }
1893
1894             // TODO: something to do here ?
1895             if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1896             {
1897                 /* Set the CMML stream active */
1898                 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1899             }
1900         }
1901     }
1902
1903     if( p_ogg->p_old_stream )
1904     {
1905         if( p_ogg->p_old_stream->p_es )
1906             msg_Dbg( p_demux, "old stream not reused" );
1907         Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1908         p_ogg->p_old_stream = NULL;
1909     }
1910 }
1911
1912 /****************************************************************************
1913  * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1914  *                        Elementary streams.
1915  ****************************************************************************/
1916 static int Ogg_BeginningOfStream( demux_t *p_demux )
1917 {
1918     demux_sys_t *p_ogg = p_demux->p_sys  ;
1919     int i_stream;
1920
1921     /* Find the logical streams embedded in the physical stream and
1922      * initialize our p_ogg structure. */
1923     if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1924     {
1925         msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1926         return VLC_EGENERIC;
1927     }
1928
1929     p_ogg->i_bitrate = 0;
1930
1931     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1932     {
1933         logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1934
1935         p_stream->p_es = NULL;
1936
1937         /* initialise kframe index */
1938         p_stream->idx=NULL;
1939
1940         if ( p_stream->fmt.i_bitrate == 0  &&
1941              ( p_stream->fmt.i_cat == VIDEO_ES ||
1942                p_stream->fmt.i_cat == AUDIO_ES ) )
1943             p_ogg->b_partial_bitrate = true;
1944         else
1945             p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1946
1947         p_stream->i_pcr = p_stream->i_previous_pcr =
1948             p_stream->i_interpolated_pcr = VLC_TS_UNKNOWN;
1949         p_stream->b_reinit = false;
1950     }
1951
1952     /* get total frame count for video stream; we will need this for seeking */
1953     p_ogg->i_total_frames = 0;
1954
1955     return VLC_SUCCESS;
1956 }
1957
1958 /****************************************************************************
1959  * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1960  ****************************************************************************/
1961 static void Ogg_EndOfStream( demux_t *p_demux )
1962 {
1963     demux_sys_t *p_ogg = p_demux->p_sys  ;
1964     int i_stream;
1965
1966     for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1967         Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1968     free( p_ogg->pp_stream );
1969
1970     /* Reinit p_ogg */
1971     p_ogg->i_bitrate = 0;
1972     p_ogg->i_streams = 0;
1973     p_ogg->pp_stream = NULL;
1974     p_ogg->skeleton.major = 0;
1975     p_ogg->skeleton.minor = 0;
1976     p_ogg->b_preparsing_done = false;
1977     p_ogg->b_es_created = false;
1978     p_ogg->i_pcr_offset = p_ogg->i_pcr;
1979
1980     /* */
1981     if( p_ogg->p_meta )
1982         vlc_meta_Delete( p_ogg->p_meta );
1983     p_ogg->p_meta = NULL;
1984
1985     for ( int i=0; i < p_ogg->i_seekpoints; i++ )
1986     {
1987         if ( p_ogg->pp_seekpoints[i] )
1988             vlc_seekpoint_Delete( p_ogg->pp_seekpoints[i] );
1989     }
1990     TAB_CLEAN( p_ogg->i_seekpoints, p_ogg->pp_seekpoints );
1991     p_ogg->i_seekpoints = 0;
1992 }
1993
1994 /**
1995  * This function delete and release all data associated to a logical_stream_t
1996  */
1997 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1998 {
1999     if( p_stream->p_es )
2000         es_out_Del( p_demux->out, p_stream->p_es );
2001
2002     ogg_stream_clear( &p_stream->os );
2003     free( p_stream->p_headers );
2004
2005     es_format_Clean( &p_stream->fmt_old );
2006     es_format_Clean( &p_stream->fmt );
2007
2008     if ( p_stream->idx != NULL)
2009     {
2010         oggseek_index_entries_free( p_stream->idx );
2011     }
2012
2013     Ogg_FreeSkeleton( p_stream->p_skel );
2014     p_stream->p_skel = NULL;
2015     if ( p_demux->p_sys->p_skelstream == p_stream )
2016         p_demux->p_sys->p_skelstream = NULL;
2017
2018     /* Shouldn't happen */
2019     if ( unlikely( p_stream->p_preparse_block ) )
2020     {
2021         block_ChainRelease( p_stream->p_preparse_block );
2022         p_stream->p_preparse_block = NULL;
2023     }
2024
2025     free( p_stream );
2026 }
2027 /**
2028  * This function check if a we need to reset a decoder in case we are
2029  * reusing an old ES
2030  */
2031 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
2032 {
2033     unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
2034     void     *pp_new_data[XIPH_MAX_HEADER_COUNT];
2035     unsigned i_new_count;
2036     if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
2037         i_new_count = 0;
2038
2039     unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
2040     void     *pp_old_data[XIPH_MAX_HEADER_COUNT];
2041     unsigned i_old_count;
2042     if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
2043         i_old_count = 0;
2044
2045     bool b_match = i_new_count == i_old_count;
2046     for( unsigned i = 0; i < i_new_count && b_match; i++ )
2047     {
2048         /* Ignore vorbis comment */
2049         if( i == 1 )
2050             continue;
2051         if( pi_new_size[i] != pi_old_size[i] ||
2052             memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
2053             b_match = false;
2054     }
2055
2056     return b_match;
2057 }
2058
2059 static bool Ogg_IsOpusFormatCompatible( const es_format_t *p_new,
2060                                         const es_format_t *p_old )
2061 {
2062     unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
2063     void     *pp_new_data[XIPH_MAX_HEADER_COUNT];
2064     unsigned i_new_count;
2065     if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
2066         i_new_count = 0;
2067     unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
2068     void     *pp_old_data[XIPH_MAX_HEADER_COUNT];
2069     unsigned i_old_count;
2070     if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
2071         i_old_count = 0;
2072     bool b_match = false;
2073     if( i_new_count == i_old_count && i_new_count > 0 )
2074     {
2075         static const unsigned char default_map[2] = { 0, 1 };
2076         unsigned char *p_old_head;
2077         unsigned char *p_new_head;
2078         const unsigned char *p_old_map;
2079         const unsigned char *p_new_map;
2080         int i_old_channel_count;
2081         int i_new_channel_count;
2082         int i_old_stream_count;
2083         int i_new_stream_count;
2084         int i_old_coupled_count;
2085         int i_new_coupled_count;
2086         p_old_head = (unsigned char *)pp_old_data[0];
2087         i_old_channel_count = i_old_stream_count = i_old_coupled_count = 0;
2088         p_old_map = default_map;
2089         if( pi_old_size[0] >= 19 && p_old_head[8] <= 15 )
2090         {
2091             i_old_channel_count = p_old_head[9];
2092             switch( p_old_head[18] )
2093             {
2094                 case 0:
2095                     i_old_stream_count = 1;
2096                     i_old_coupled_count = i_old_channel_count - 1;
2097                     break;
2098                 case 1:
2099                     if( pi_old_size[0] >= 21U + i_old_channel_count )
2100                     {
2101                         i_old_stream_count = p_old_head[19];
2102                         i_old_coupled_count = p_old_head[20];
2103                         p_old_map = p_old_head + 21;
2104                     }
2105                     break;
2106             }
2107         }
2108         p_new_head = (unsigned char *)pp_new_data[0];
2109         i_new_channel_count = i_new_stream_count = i_new_coupled_count = 0;
2110         p_new_map = default_map;
2111         if( pi_new_size[0] >= 19 && p_new_head[8] <= 15 )
2112         {
2113             i_new_channel_count = p_new_head[9];
2114             switch( p_new_head[18] )
2115             {
2116                 case 0:
2117                     i_new_stream_count = 1;
2118                     i_new_coupled_count = i_new_channel_count - 1;
2119                     break;
2120                 case 1:
2121                     if( pi_new_size[0] >= 21U + i_new_channel_count )
2122                     {
2123                         i_new_stream_count = p_new_head[19];
2124                         i_new_coupled_count = p_new_head[20];
2125                         p_new_map = p_new_head+21;
2126                     }
2127                     break;
2128             }
2129         }
2130         b_match = i_old_channel_count == i_new_channel_count &&
2131                   i_old_stream_count == i_new_stream_count &&
2132                   i_old_coupled_count == i_new_coupled_count &&
2133                   memcmp(p_old_map, p_new_map,
2134                       i_new_channel_count*sizeof(*p_new_map)) == 0;
2135     }
2136
2137     return b_match;
2138 }
2139
2140 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
2141 {
2142     bool b_compatible = false;
2143     if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
2144         return true;
2145
2146     /* Only Vorbis and Opus are supported. */
2147     if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
2148         b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
2149     else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
2150         b_compatible = Ogg_IsOpusFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
2151
2152     if( !b_compatible )
2153         msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
2154
2155     return !b_compatible;
2156 }
2157
2158 static void Ogg_ExtractComments( demux_t *p_demux, es_format_t *p_fmt,
2159                                  const void *p_headers, unsigned i_headers )
2160 {
2161     demux_sys_t *p_ogg = p_demux->p_sys;
2162     int i_cover_score = 0;
2163     int i_cover_idx = 0;
2164     float pf_replay_gain[AUDIO_REPLAY_GAIN_MAX];
2165     float pf_replay_peak[AUDIO_REPLAY_GAIN_MAX];
2166     for(int i=0; i< AUDIO_REPLAY_GAIN_MAX; i++ )
2167     {
2168         pf_replay_gain[i] = 0;
2169         pf_replay_peak[i] = 0;
2170     }
2171     vorbis_ParseComment( &p_ogg->p_meta, p_headers, i_headers,
2172                          &p_ogg->i_attachments, &p_ogg->attachments,
2173                          &i_cover_score, &i_cover_idx,
2174                          &p_ogg->i_seekpoints, &p_ogg->pp_seekpoints,
2175                          &pf_replay_gain, &pf_replay_peak );
2176     if( p_ogg->p_meta != NULL && i_cover_idx < p_ogg->i_attachments )
2177     {
2178         char psz_url[128];
2179         snprintf( psz_url, sizeof(psz_url), "attachment://%s",
2180                   p_ogg->attachments[i_cover_idx]->psz_name );
2181         vlc_meta_Set( p_ogg->p_meta, vlc_meta_ArtworkURL, psz_url );
2182     }
2183
2184     for ( int i=0; i<AUDIO_REPLAY_GAIN_MAX;i++ )
2185     {
2186         if ( pf_replay_gain[i] != 0 )
2187         {
2188             p_fmt->audio_replay_gain.pb_gain[i] = true;
2189             p_fmt->audio_replay_gain.pf_gain[i] = pf_replay_gain[i];
2190             msg_Dbg( p_demux, "setting replay gain %d to %f", i, pf_replay_gain[i] );
2191         }
2192         if ( pf_replay_peak[i] != 0 )
2193         {
2194             p_fmt->audio_replay_gain.pb_peak[i] = true;
2195             p_fmt->audio_replay_gain.pf_peak[i] = pf_replay_peak[i];
2196             msg_Dbg( p_demux, "setting replay peak %d to %f", i, pf_replay_gain[i] );
2197         }
2198     }
2199
2200     if( p_ogg->i_seekpoints > 1 )
2201     {
2202         p_demux->info.i_update |= INPUT_UPDATE_TITLE_LIST;
2203     }
2204 }
2205
2206 static void Ogg_ExtractXiphMeta( demux_t *p_demux, es_format_t *p_fmt,
2207                                  const void *p_headers, unsigned i_headers, unsigned i_skip )
2208 {
2209     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
2210     void     *pp_data[XIPH_MAX_HEADER_COUNT];
2211     unsigned i_count;
2212
2213     if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
2214         return;
2215     /* TODO how to handle multiple comments properly ? */
2216     if( i_count >= 2 && pi_size[1] > i_skip )
2217     {
2218         Ogg_ExtractComments( p_demux, p_fmt, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip );
2219     }
2220 }
2221
2222 static void Ogg_ExtractMeta( demux_t *p_demux, es_format_t *p_fmt, const uint8_t *p_headers, int i_headers )
2223 {
2224     demux_sys_t *p_ogg = p_demux->p_sys;
2225
2226     switch( p_fmt->i_codec )
2227     {
2228     /* 3 headers with the 2° one being the comments */
2229     case VLC_CODEC_VORBIS:
2230     case VLC_CODEC_THEORA:
2231         Ogg_ExtractXiphMeta( p_demux, p_fmt, p_headers, i_headers, 1+6 );
2232         break;
2233     case VLC_CODEC_OPUS:
2234         Ogg_ExtractXiphMeta( p_demux, p_fmt, p_headers, i_headers, 8 );
2235         break;
2236     case VLC_CODEC_SPEEX:
2237         Ogg_ExtractXiphMeta( p_demux, p_fmt, p_headers, i_headers, 0 );
2238         break;
2239     case VLC_CODEC_VP8:
2240         Ogg_ExtractComments( p_demux, p_fmt, p_headers, i_headers );
2241         break;
2242     /* N headers with the 2° one being the comments */
2243     case VLC_CODEC_KATE:
2244         /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
2245         Ogg_ExtractXiphMeta( p_demux, p_fmt, p_headers, i_headers, 1+7+1 );
2246         break;
2247
2248     /* TODO */
2249     case VLC_CODEC_FLAC:
2250         msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&p_fmt->i_codec );
2251         break;
2252
2253     /* No meta data */
2254     case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
2255     case VLC_CODEC_DIRAC:
2256     default:
2257         break;
2258     }
2259     if( p_ogg->p_meta )
2260         p_demux->info.i_update |= INPUT_UPDATE_META;
2261 }
2262
2263 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
2264                                   ogg_packet *p_oggpacket )
2265 {
2266     bs_t bitstream;
2267     int i_fps_numerator;
2268     int i_fps_denominator;
2269     int i_keyframe_frequency_force;
2270     int i_major;
2271     int i_minor;
2272     int i_subminor;
2273     int i_version;
2274
2275     p_stream->fmt.i_cat = VIDEO_ES;
2276     p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2277
2278     /* Signal that we want to keep a backup of the theora
2279      * stream headers. They will be used when switching between
2280      * audio streams. */
2281     p_stream->b_force_backup = true;
2282
2283     /* Cheat and get additionnal info ;) */
2284     bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
2285     bs_skip( &bitstream, 56 );
2286
2287     i_major = bs_read( &bitstream, 8 ); /* major version num */
2288     i_minor = bs_read( &bitstream, 8 ); /* minor version num */
2289     i_subminor = bs_read( &bitstream, 8 ); /* subminor version num */
2290
2291     bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
2292     bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
2293     bs_read( &bitstream, 24 ); /* frame width */
2294     bs_read( &bitstream, 24 ); /* frame height */
2295     bs_read( &bitstream, 8 ); /* x offset */
2296     bs_read( &bitstream, 8 ); /* y offset */
2297
2298     i_fps_numerator = bs_read( &bitstream, 32 );
2299     i_fps_denominator = bs_read( &bitstream, 32 );
2300     i_fps_denominator = __MAX( i_fps_denominator, 1 );
2301     bs_read( &bitstream, 24 ); /* aspect_numerator */
2302     bs_read( &bitstream, 24 ); /* aspect_denominator */
2303
2304     p_stream->fmt.video.i_frame_rate = i_fps_numerator;
2305     p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
2306
2307     bs_read( &bitstream, 8 ); /* colorspace */
2308     p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
2309     bs_read( &bitstream, 6 ); /* quality */
2310
2311     i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
2312
2313     /* granule_shift = i_log( frequency_force -1 ) */
2314     p_stream->i_granule_shift = 0;
2315     i_keyframe_frequency_force--;
2316     while( i_keyframe_frequency_force )
2317     {
2318         p_stream->i_granule_shift++;
2319         i_keyframe_frequency_force >>= 1;
2320     }
2321
2322     i_version = i_major * 1000000 + i_minor * 1000 + i_subminor;
2323     p_stream->i_keyframe_offset = 0;
2324     p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
2325
2326     if ( i_version >= 3002001 )
2327     {
2328         p_stream->i_keyframe_offset = 1;
2329     }
2330 }
2331
2332 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
2333                                   ogg_packet *p_oggpacket )
2334 {
2335     oggpack_buffer opb;
2336
2337     p_stream->fmt.i_cat = AUDIO_ES;
2338     p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2339
2340     /* Signal that we want to keep a backup of the vorbis
2341      * stream headers. They will be used when switching between
2342      * audio streams. */
2343     p_stream->b_force_backup = true;
2344
2345     /* Cheat and get additionnal info ;) */
2346     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2347     oggpack_adv( &opb, 88 );
2348     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
2349     fill_channels_info(&p_stream->fmt.audio);
2350     p_stream->f_rate = p_stream->fmt.audio.i_rate =
2351         oggpack_read( &opb, 32 );
2352     oggpack_adv( &opb, 32 );
2353     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
2354 }
2355
2356 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
2357                                  ogg_packet *p_oggpacket )
2358 {
2359     oggpack_buffer opb;
2360
2361     p_stream->fmt.i_cat = AUDIO_ES;
2362     p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2363
2364     /* Signal that we want to keep a backup of the speex
2365      * stream headers. They will be used when switching between
2366      * audio streams. */
2367     p_stream->b_force_backup = true;
2368
2369     /* Cheat and get additionnal info ;) */
2370     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2371     oggpack_adv( &opb, 224 );
2372     oggpack_adv( &opb, 32 ); /* speex_version_id */
2373     oggpack_adv( &opb, 32 ); /* header_size */
2374     p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
2375     oggpack_adv( &opb, 32 ); /* mode */
2376     oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
2377     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
2378     fill_channels_info(&p_stream->fmt.audio);
2379     p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
2380     oggpack_adv( &opb, 32 ); /* frame_size */
2381     oggpack_adv( &opb, 32 ); /* vbr */
2382     oggpack_adv( &opb, 32 ); /* frames_per_packet */
2383     p_stream->i_extra_headers_packets = oggpack_read( &opb, 32 ); /* extra_headers */
2384 }
2385
2386 static void Ogg_ReadOpusHeader( logical_stream_t *p_stream,
2387                                 ogg_packet *p_oggpacket )
2388 {
2389     oggpack_buffer opb;
2390
2391     p_stream->fmt.i_cat = AUDIO_ES;
2392     p_stream->fmt.i_codec = VLC_CODEC_OPUS;
2393
2394     /* Signal that we want to keep a backup of the opus
2395      * stream headers. They will be used when switching between
2396      * audio streams. */
2397     p_stream->b_force_backup = true;
2398
2399     /* All OggOpus streams are timestamped at 48kHz and
2400      * can be played at 48kHz. */
2401     p_stream->f_rate = p_stream->fmt.audio.i_rate = 48000;
2402     p_stream->fmt.i_bitrate = 0;
2403
2404     /* Cheat and get additional info ;) */
2405     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2406     oggpack_adv( &opb, 64 );
2407     oggpack_adv( &opb, 8 ); /* version_id */
2408     p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
2409     fill_channels_info(&p_stream->fmt.audio);
2410     p_stream->i_pre_skip = oggpack_read( &opb, 16 );
2411 }
2412
2413 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
2414                                 ogg_packet *p_oggpacket )
2415 {
2416     /* Parse the STREAMINFO metadata */
2417     bs_t s;
2418
2419     bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
2420
2421     bs_read( &s, 1 );
2422     if( p_oggpacket->bytes > 0 && bs_read( &s, 7 ) != 0 )
2423     {
2424         msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
2425         return;
2426     }
2427
2428     if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
2429     {
2430         bs_skip( &s, 80 );
2431         p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
2432         p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
2433         fill_channels_info(&p_stream->fmt.audio);
2434
2435         msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
2436                  p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
2437     }
2438     else
2439     {
2440         msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
2441     }
2442
2443     /* Fake this as the last metadata block */
2444     *((uint8_t*)p_oggpacket->packet) |= 0x80;
2445 }
2446
2447 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
2448                                 ogg_packet *p_oggpacket )
2449 {
2450     oggpack_buffer opb;
2451     int32_t gnum;
2452     int32_t gden;
2453     int n;
2454     char *psz_desc;
2455
2456     p_stream->fmt.i_cat = SPU_ES;
2457     p_stream->fmt.i_codec = VLC_CODEC_KATE;
2458
2459     /* Signal that we want to keep a backup of the kate
2460      * stream headers. They will be used when switching between
2461      * kate streams. */
2462     p_stream->b_force_backup = true;
2463
2464     /* Cheat and get additionnal info ;) */
2465     oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2466     oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
2467     p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
2468     oggpack_adv( &opb, 3*8 );
2469     p_stream->i_granule_shift = oggpack_read( &opb, 8 );
2470     oggpack_adv( &opb, 8*8 ); /* reserved */
2471     gnum = oggpack_read( &opb, 32 );
2472     gden = oggpack_read( &opb, 32 );
2473     p_stream->f_rate = (double)gnum/gden;
2474
2475     p_stream->fmt.psz_language = malloc(16);
2476     if( p_stream->fmt.psz_language )
2477     {
2478         for( n = 0; n < 16; n++ )
2479             p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
2480         p_stream->fmt.psz_language[15] = 0; /* just in case */
2481     }
2482     else
2483     {
2484         for( n = 0; n < 16; n++ )
2485             oggpack_read(&opb,8);
2486     }
2487     p_stream->fmt.psz_description = malloc(16);
2488     if( p_stream->fmt.psz_description )
2489     {
2490         for( n = 0; n < 16; n++ )
2491             p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
2492         p_stream->fmt.psz_description[15] = 0; /* just in case */
2493
2494         /* Now find a localized user readable description for this category */
2495         psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
2496         if( psz_desc )
2497         {
2498             free( p_stream->fmt.psz_description );
2499             p_stream->fmt.psz_description = psz_desc;
2500         }
2501     }
2502     else
2503     {
2504         for( n = 0; n < 16; n++ )
2505             oggpack_read(&opb,8);
2506     }
2507 }
2508
2509 static bool Ogg_ReadVP8Header( demux_t *p_demux, logical_stream_t *p_stream,
2510                                ogg_packet *p_oggpacket )
2511 {
2512     switch( p_oggpacket->packet[5] )
2513     {
2514     /* STREAMINFO */
2515     case 0x01:
2516         /* Mapping version */
2517         if ( p_oggpacket->packet[6] != 0x01 || p_oggpacket->packet[7] != 0x00 )
2518             return false;
2519         p_stream->fmt.i_cat = VIDEO_ES;
2520         p_stream->fmt.i_codec = VLC_CODEC_VP8;
2521         p_stream->i_granule_shift = 32;
2522         p_stream->fmt.video.i_width = GetWBE( &p_oggpacket->packet[8] );
2523         p_stream->fmt.video.i_height = GetWBE( &p_oggpacket->packet[10] );
2524         p_stream->fmt.video.i_sar_num = GetDWBE( &p_oggpacket->packet[12 - 1] ) & 0x0FFF;
2525         p_stream->fmt.video.i_sar_den = GetDWBE( &p_oggpacket->packet[15 - 1] ) & 0x0FFF;
2526         p_stream->fmt.video.i_frame_rate = GetDWBE( &p_oggpacket->packet[18] );
2527         p_stream->fmt.video.i_frame_rate_base = GetDWBE( &p_oggpacket->packet[22] );
2528         p_stream->f_rate = (double) p_stream->fmt.video.i_frame_rate / p_stream->fmt.video.i_frame_rate_base;
2529         return true;
2530     /* METADATA */
2531     case 0x02:
2532         Ogg_ExtractMeta( p_demux, & p_stream->fmt,
2533                          p_oggpacket->packet + 7, p_oggpacket->bytes - 7 );
2534         return true;
2535     default:
2536         return false;
2537     }
2538 }
2539
2540 static void Ogg_ApplyContentType( logical_stream_t *p_stream, const char* psz_value,
2541                                   bool *b_force_backup, bool *b_packet_out )
2542 {
2543     if( !strncmp(psz_value, "audio/x-wav", 11) )
2544     {
2545         /* n.b. WAVs are unsupported right now */
2546         p_stream->fmt.i_cat = UNKNOWN_ES;
2547         free( p_stream->fmt.psz_description );
2548         p_stream->fmt.psz_description = strdup("WAV Audio (Unsupported)");
2549     }
2550     else if( !strncmp(psz_value, "audio/x-vorbis", 14) ||
2551              !strncmp(psz_value, "audio/vorbis", 12) )
2552     {
2553         p_stream->fmt.i_cat = AUDIO_ES;
2554         p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2555
2556         *b_force_backup = true;
2557     }
2558     else if( !strncmp(psz_value, "audio/x-speex", 13) ||
2559              !strncmp(psz_value, "audio/speex", 11) )
2560     {
2561         p_stream->fmt.i_cat = AUDIO_ES;
2562         p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2563
2564         *b_force_backup = true;
2565     }
2566     else if( !strncmp(psz_value, "audio/flac", 10) )
2567     {
2568         p_stream->fmt.i_cat = AUDIO_ES;
2569         p_stream->fmt.i_codec = VLC_CODEC_FLAC;
2570
2571         *b_force_backup = true;
2572     }
2573     else if( !strncmp(psz_value, "video/x-theora", 14) ||
2574              !strncmp(psz_value, "video/theora", 12) )
2575     {
2576         p_stream->fmt.i_cat = VIDEO_ES;
2577         p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2578
2579         *b_force_backup = true;
2580     }
2581     else if( !strncmp(psz_value, "video/x-xvid", 12) )
2582     {
2583         p_stream->fmt.i_cat = VIDEO_ES;
2584         p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
2585
2586         *b_force_backup = true;
2587     }
2588     else if( !strncmp(psz_value, "video/mpeg", 10) )
2589     {
2590         /* n.b. MPEG streams are unsupported right now */
2591         p_stream->fmt.i_cat = VIDEO_ES;
2592         p_stream->fmt.i_codec = VLC_CODEC_MPGV;
2593     }
2594     else if( !strncmp(psz_value, "text/x-cmml", 11) ||
2595              !strncmp(psz_value, "text/cmml", 9) )
2596     {
2597         p_stream->fmt.i_cat = SPU_ES;
2598         p_stream->fmt.i_codec = VLC_CODEC_CMML;
2599         *b_packet_out = true;
2600     }
2601     else if( !strncmp(psz_value, "application/kate", 16) )
2602     {
2603         /* ??? */
2604         p_stream->fmt.i_cat = UNKNOWN_ES;
2605         free( p_stream->fmt.psz_description );
2606         p_stream->fmt.psz_description = strdup("OGG Kate Overlay (Unsupported)");
2607     }
2608     else if( !strncmp(psz_value, "video/x-vp8", 11) )
2609     {
2610         p_stream->fmt.i_cat = VIDEO_ES;
2611         p_stream->fmt.i_codec = VLC_CODEC_VP8;
2612     }
2613 }
2614
2615 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,
2616                                    logical_stream_t *p_stream,
2617                                    ogg_packet *p_oggpacket )
2618 {
2619     if( p_oggpacket->bytes >= 28 &&
2620         !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
2621     {
2622         oggpack_buffer opb;
2623
2624         uint16_t major_version;
2625         uint16_t minor_version;
2626         uint64_t timebase_numerator;
2627         uint64_t timebase_denominator;
2628
2629         Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
2630
2631         oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2632         oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
2633         major_version = oggpack_read( &opb, 2*8 ); /* major version */
2634         minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
2635         timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
2636         timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
2637
2638         msg_Dbg( p_demux, "Annodex info: version %"PRIu16".%"PRIu16" "
2639                           "Timebase  %"PRId64" / %"PRId64,
2640                           major_version, minor_version,
2641                           timebase_numerator, timebase_denominator );
2642     }
2643     else if( p_oggpacket->bytes >= 42 &&
2644              !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
2645     {
2646         uint64_t granule_rate_numerator;
2647         uint64_t granule_rate_denominator;
2648         char content_type_string[1024];
2649
2650         /* Read in Annodex header fields */
2651
2652         granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
2653         granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
2654         p_stream->i_secondary_header_packets =
2655             GetDWLE( &p_oggpacket->packet[24] );
2656
2657         /* we are guaranteed that the first header field will be
2658          * the content-type (by the Annodex standard) */
2659         content_type_string[0] = '\0';
2660         if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
2661         {
2662             uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
2663                                  p_oggpacket->bytes - 1 );
2664             if( p && p[0] == '\r' && p[1] == '\n' )
2665                 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
2666                         content_type_string );
2667         }
2668
2669         msg_Dbg( p_demux, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
2670                  granule_rate_numerator, granule_rate_denominator,
2671                  p_stream->i_secondary_header_packets, content_type_string );
2672
2673         p_stream->f_rate = (float) granule_rate_numerator /
2674             (float) granule_rate_denominator;
2675
2676         /* What type of file do we have?
2677          * strcmp is safe to use here because we've extracted
2678          * content_type_string from the stream manually */
2679         bool b_dopacketout = false;
2680         Ogg_ApplyContentType( p_stream, content_type_string,
2681                               &p_stream->b_force_backup, &b_dopacketout );
2682         if ( b_dopacketout ) ogg_stream_packetout( &p_stream->os, p_oggpacket );
2683     }
2684 }
2685
2686 static void Ogg_ReadSkeletonHeader( demux_t *p_demux, logical_stream_t *p_stream,
2687                                     ogg_packet *p_oggpacket )
2688 {
2689     p_demux->p_sys->p_skelstream = p_stream;
2690     /* There can be only 1 skeleton for streams */
2691     p_demux->p_sys->skeleton.major = GetWLE( &p_oggpacket->packet[8] );
2692     p_demux->p_sys->skeleton.minor = GetWLE( &p_oggpacket->packet[10] );
2693     if ( asprintf( & p_stream->fmt.psz_description,
2694                         "OGG Skeleton version %" PRIu16 ".%" PRIu16,
2695                         p_demux->p_sys->skeleton.major,
2696                         p_demux->p_sys->skeleton.minor ) < 0 )
2697         p_stream->fmt.psz_description = NULL;
2698 }
2699
2700 static void Ogg_ReadSkeletonBones( demux_t *p_demux, ogg_packet *p_oggpacket )
2701 {
2702     if ( p_demux->p_sys->skeleton.major < 3 || p_oggpacket->bytes < 52 ) return;
2703
2704     /* Find the matching stream for this skeleton data */
2705     ogg_int32_t i_serialno = GetDWLE( &p_oggpacket->packet[12] );
2706     logical_stream_t *p_target_stream = NULL;
2707     for ( int i=0; i< p_demux->p_sys->i_streams; i++ )
2708     {
2709         if ( p_demux->p_sys->pp_stream[i]->i_serial_no == i_serialno )
2710         {
2711             p_target_stream = p_demux->p_sys->pp_stream[i];
2712             break;
2713         }
2714     }
2715     if ( !p_target_stream ) return;
2716
2717     ogg_skeleton_t *p_skel = p_target_stream->p_skel;
2718     if ( !p_skel )
2719     {
2720         p_skel = malloc( sizeof( ogg_skeleton_t ) );
2721         if ( !p_skel ) return;
2722         TAB_INIT( p_skel->i_messages, p_skel->ppsz_messages );
2723         p_skel->p_index = NULL;
2724         p_target_stream->p_skel = p_skel;
2725     }
2726
2727     const unsigned char *p_messages = p_oggpacket->packet + 8 + GetDWLE( &p_oggpacket->packet[8] );
2728     const unsigned char *p_boundary = p_oggpacket->packet + p_oggpacket->bytes;
2729     const unsigned char *p = p_messages;
2730     while ( p <= p_boundary - 1 && p > p_oggpacket->packet )
2731     {
2732         if ( *p == 0x0D && *(p+1) == 0x0A )
2733         {
2734             char *psz_message = strndup( (const char *) p_messages,
2735                                          p - p_messages );
2736             if ( psz_message )
2737             {
2738                 msg_Dbg( p_demux, "stream %" PRId32 " [%s]", i_serialno, psz_message );
2739                 TAB_APPEND( p_skel->i_messages, p_skel->ppsz_messages, psz_message );
2740             }
2741             if ( p < p_boundary - 1 ) p_messages = p + 2;
2742         }
2743         p++;
2744     }
2745
2746 }
2747
2748 /* Unpacks the 7bit variable encoding used in skeleton indexes */
2749 unsigned const char * Read7BitsVariableLE( unsigned const char *p_begin,
2750                                            unsigned const char *p_end,
2751                                            uint64_t *pi_value )
2752 {
2753     int i_shift = 0;
2754     int64_t i_read = 0;
2755     *pi_value = 0;
2756
2757     while ( p_begin < p_end )
2758     {
2759         i_read = *p_begin & 0x7F; /* High bit is start of integer */
2760         *pi_value = *pi_value | ( i_read << i_shift );
2761         i_shift += 7;
2762         if ( (*p_begin++ & 0x80) == 0x80 ) break; /* see prev */
2763     }
2764
2765     *pi_value = GetQWLE( pi_value );
2766     return p_begin;
2767 }
2768
2769 static void Ogg_ReadSkeletonIndex( demux_t *p_demux, ogg_packet *p_oggpacket )
2770 {
2771     if ( p_demux->p_sys->skeleton.major < 4
2772          || p_oggpacket->bytes < 44 /* Need at least 1 index value (42+1+1) */
2773     ) return;
2774
2775     /* Find the matching stream for this skeleton data */
2776     int32_t i_serialno = GetDWLE( &p_oggpacket->packet[6] );
2777     logical_stream_t *p_stream = NULL;
2778     for ( int i=0; i< p_demux->p_sys->i_streams; i++ )
2779     {
2780         if ( p_demux->p_sys->pp_stream[i]->i_serial_no == i_serialno )
2781         {
2782             p_stream = p_demux->p_sys->pp_stream[i];
2783             break;
2784         }
2785     }
2786     if ( !p_stream ) return;
2787     uint64_t i_keypoints = GetQWLE( &p_oggpacket->packet[10] );
2788     msg_Dbg( p_demux, "%" PRIi64 " index data for %" PRIi32, i_keypoints, i_serialno );
2789     if ( !i_keypoints ) return;
2790
2791     p_stream->p_skel->i_indexstampden = GetQWLE( &p_oggpacket->packet[18] );
2792     p_stream->p_skel->i_indexfirstnum = GetQWLE( &p_oggpacket->packet[24] );
2793     p_stream->p_skel->i_indexlastnum = GetQWLE( &p_oggpacket->packet[32] );
2794     unsigned const char *p_fwdbyte = &p_oggpacket->packet[42];
2795     unsigned const char *p_boundary = p_oggpacket->packet + p_oggpacket->bytes;
2796     uint64_t i_offset = 0;
2797     uint64_t i_time = 0;
2798     uint64_t i_keypoints_found = 0;
2799
2800     while( p_fwdbyte < p_boundary && i_keypoints_found < i_keypoints )
2801     {
2802         uint64_t i_val;
2803         p_fwdbyte = Read7BitsVariableLE( p_fwdbyte, p_boundary, &i_val );
2804         i_offset += i_val;
2805         p_fwdbyte = Read7BitsVariableLE( p_fwdbyte, p_boundary, &i_val );
2806         i_time += i_val * p_stream->p_skel->i_indexstampden;
2807         i_keypoints_found++;
2808     }
2809
2810     if ( i_keypoints_found != i_keypoints )
2811     {
2812         msg_Warn( p_demux, "Invalid Index: missing entries" );
2813         return;
2814     }
2815
2816     p_stream->p_skel->p_index = malloc( p_oggpacket->bytes - 42 );
2817     if ( !p_stream->p_skel->p_index ) return;
2818     memcpy( p_stream->p_skel->p_index, &p_oggpacket->packet[42],
2819             p_oggpacket->bytes - 42 );
2820     p_stream->p_skel->i_index = i_keypoints_found;
2821     p_stream->p_skel->i_index_size = p_oggpacket->bytes - 42;
2822 }
2823
2824 static void Ogg_FreeSkeleton( ogg_skeleton_t *p_skel )
2825 {
2826     if ( !p_skel ) return;
2827     for ( int i=0; i< p_skel->i_messages; i++ )
2828         free( p_skel->ppsz_messages[i] );
2829     TAB_CLEAN( p_skel->i_messages, p_skel->ppsz_messages );
2830     free( p_skel->p_index );
2831     free( p_skel );
2832 }
2833
2834 static void Ogg_ApplySkeleton( logical_stream_t *p_stream )
2835 {
2836     if ( !p_stream->p_skel ) return;
2837     for ( int i=0; i< p_stream->p_skel->i_messages; i++ )
2838     {
2839         const char *psz_message = p_stream->p_skel->ppsz_messages[i];
2840         if ( ! strncmp( "Name: ", psz_message, 6 ) )
2841         {
2842             free( p_stream->fmt.psz_description );
2843             p_stream->fmt.psz_description = strdup( psz_message + 6 );
2844         }
2845         else if ( ! strncmp("Content-Type: ", psz_message, 14 ) )
2846         {
2847             bool b_foo;
2848             Ogg_ApplyContentType( p_stream, psz_message + 14, &b_foo, &b_foo );
2849         }
2850     }
2851 }
2852
2853 /* Return true if there's a skeleton exact match */
2854 bool Ogg_GetBoundsUsingSkeletonIndex( logical_stream_t *p_stream, int64_t i_time,
2855                                       int64_t *pi_lower, int64_t *pi_upper )
2856 {
2857     if ( !p_stream || !p_stream->p_skel || !p_stream->p_skel->p_index )
2858         return false;
2859
2860     /* Validate range */
2861     if ( i_time < p_stream->p_skel->i_indexfirstnum
2862                 * p_stream->p_skel->i_indexstampden ||
2863          i_time > p_stream->p_skel->i_indexlastnum
2864                 * p_stream->p_skel->i_indexstampden ) return false;
2865
2866     /* Then Lookup its index */
2867     unsigned const char *p_fwdbyte = p_stream->p_skel->p_index;
2868     struct
2869     {
2870         int64_t i_pos;
2871         int64_t i_time;
2872     } current = { 0, 0 }, prev = { -1, -1 };
2873
2874     uint64_t i_keypoints_found = 0;
2875
2876     while( p_fwdbyte < p_fwdbyte + p_stream->p_skel->i_index_size
2877            && i_keypoints_found < p_stream->p_skel->i_index )
2878     {
2879         uint64_t i_val;
2880         p_fwdbyte = Read7BitsVariableLE( p_fwdbyte,
2881                         p_fwdbyte + p_stream->p_skel->i_index_size, &i_val );
2882         current.i_pos += i_val;
2883         p_fwdbyte = Read7BitsVariableLE( p_fwdbyte,
2884                         p_fwdbyte + p_stream->p_skel->i_index_size, &i_val );
2885         current.i_time += i_val * p_stream->p_skel->i_indexstampden;
2886         if ( current.i_pos < 0 || current.i_time < 0 ) break;
2887
2888         i_keypoints_found++;
2889
2890         if ( i_time <= current.i_time )
2891         {
2892             *pi_lower = prev.i_pos;
2893             *pi_upper = current.i_pos;
2894             return ( i_time == current.i_time );
2895         }
2896         prev = current;
2897     }
2898     return false;
2899 }
2900
2901 static uint32_t dirac_uint( bs_t *p_bs )
2902 {
2903     uint32_t u_count = 0, u_value = 0;
2904
2905     while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
2906     {
2907         u_count++;
2908         u_value <<= 1;
2909         u_value |= bs_read( p_bs, 1 );
2910     }
2911
2912     return (1<<u_count) - 1 + u_value;
2913 }
2914
2915 static int dirac_bool( bs_t *p_bs )
2916 {
2917     return bs_read( p_bs, 1 );
2918 }
2919
2920 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
2921                                  ogg_packet *p_oggpacket )
2922 {
2923     static const struct {
2924         uint32_t u_n /* numerator */, u_d /* denominator */;
2925     } p_dirac_frate_tbl[] = { /* table 10.3 */
2926         {1,1}, /* this first value is never used */
2927         {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
2928         {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
2929     };
2930     static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
2931
2932     static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
2933         1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
2934     };
2935     static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
2936
2937     bs_t bs;
2938
2939     p_stream->i_granule_shift = 22; /* not 32 */
2940
2941     /* Backing up stream headers is not required -- seqhdrs are repeated
2942      * thoughout the stream at suitable decoding start points */
2943     p_stream->b_force_backup = false;
2944
2945     /* read in useful bits from sequence header */
2946     bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
2947     bs_skip( &bs, 13*8); /* parse_info_header */
2948     dirac_uint( &bs ); /* major_version */
2949     dirac_uint( &bs ); /* minor_version */
2950     dirac_uint( &bs ); /* profile */
2951     dirac_uint( &bs ); /* level */
2952
2953     uint32_t u_video_format = dirac_uint( &bs ); /* index */
2954     if( u_video_format >= u_dirac_vidfmt_frate )
2955     {
2956         /* don't know how to parse this ogg dirac stream */
2957         return false;
2958     }
2959
2960     if( dirac_bool( &bs ) )
2961     {
2962         dirac_uint( &bs ); /* frame_width */
2963         dirac_uint( &bs ); /* frame_height */
2964     }
2965
2966     if( dirac_bool( &bs ) )
2967     {
2968         dirac_uint( &bs ); /* chroma_format */
2969     }
2970
2971     if( dirac_bool( &bs ) )
2972     {
2973         dirac_uint( &bs ); /* scan_format */
2974     }
2975
2976     uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
2977     uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
2978     if( dirac_bool( &bs ) )
2979     {
2980         uint32_t u_frame_rate_index = dirac_uint( &bs );
2981         if( u_frame_rate_index >= u_dirac_frate_tbl )
2982         {
2983             /* something is wrong with this stream */
2984             return false;
2985         }
2986         u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
2987         u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
2988         if( u_frame_rate_index == 0 )
2989         {
2990             u_n = dirac_uint( &bs ); /* frame_rate_numerator */
2991             u_d = dirac_uint( &bs ); /* frame_rate_denominator */
2992         }
2993     }
2994     p_stream->f_rate = (float) u_n / u_d;
2995
2996     /* probably is an ogg dirac es */
2997     p_stream->fmt.i_cat = VIDEO_ES;
2998     p_stream->fmt.i_codec = VLC_CODEC_DIRAC;
2999
3000     return true;
3001 }