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