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