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