1 /*****************************************************************************
2 * ogg.c : ogg stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Andre Pang <Andre.Pang@csiro.au> (Annodex support)
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 #include <vlc_input.h>
40 #include <vlc_codecs.h>
43 #include "xiph_metadata.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
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 )
64 /*****************************************************************************
65 * Definitions of structures and functions used by this plugins
66 *****************************************************************************/
68 /* OggDS headers for the new header format (used in ogm files) */
73 } stream_header_video_t;
79 ogg_int16_t blockalign;
80 ogg_int32_t avgbytespersec;
81 } stream_header_audio_t;
88 ogg_int32_t size; /* size of the structure */
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 */
94 ogg_int32_t buffersize;
95 ogg_int16_t bits_per_sample;
101 stream_header_video_t video;
103 stream_header_audio_t audio;
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
115 /*****************************************************************************
117 *****************************************************************************/
118 static int Demux ( demux_t * );
119 static int Control( demux_t *, int, va_list );
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 * );
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 );
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 );
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 );
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 * );
149 static void fill_channels_info(audio_format_t *audio)
151 static const int pi_channels_map[9] =
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
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];
177 /*****************************************************************************
178 * Open: initializes ogg demux structures
179 *****************************************************************************/
180 static int Open( vlc_object_t * p_this )
182 demux_t *p_demux = (demux_t *)p_this;
184 const uint8_t *p_peek;
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 ) )
194 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
198 p_sys->i_length = -1;
200 /* Set exported functions */
201 p_demux->pf_demux = Demux;
202 p_demux->pf_control = Control;
204 /* Initialize the Ogg physical bitstream parser */
205 ogg_sync_init( &p_sys->oy );
208 TAB_INIT( p_sys->i_seekpoints, p_sys->pp_seekpoints );
213 /*****************************************************************************
214 * Close: frees unused data
215 *****************************************************************************/
216 static void Close( vlc_object_t *p_this )
218 demux_t *p_demux = (demux_t *)p_this;
219 demux_sys_t *p_sys = p_demux->p_sys ;
221 /* Cleanup the bitstream parser */
222 ogg_sync_clear( &p_sys->oy );
224 Ogg_EndOfStream( p_demux );
226 if( p_sys->p_old_stream )
227 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
229 TAB_CLEAN( p_sys->i_seekpoints, p_sys->pp_seekpoints );
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 )
241 demux_sys_t *p_sys = p_demux->p_sys;
242 ogg_packet oggpacket;
244 bool b_skipping = false;
247 if( p_sys->i_eos == p_sys->i_streams )
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 )
256 p_sys->p_old_stream = p_sys->pp_stream[0];
257 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
259 Ogg_EndOfStream( p_demux );
263 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
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 );
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.
284 if( !p_sys->b_page_waiting)
287 * Demux an ogg page from the stream
289 if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
292 /* Test for End of Stream */
293 if( ogg_page_eos( &p_sys->current_page ) )
298 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
300 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
302 /* if we've just pulled page, look for the right logical stream */
303 if( !p_sys->b_page_waiting )
305 if( p_sys->i_streams == 1 &&
306 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
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 ) );
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);
318 if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
325 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
327 /* Read info from any secondary header packets, if there are any */
328 if( p_stream->i_secondary_header_packets > 0 )
330 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
331 oggpacket.bytes >= 7 &&
332 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
334 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
335 p_stream->i_secondary_header_packets = 0;
337 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
338 oggpacket.bytes >= 7 &&
339 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
341 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
342 p_stream->i_secondary_header_packets = 0;
344 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
346 p_stream->i_secondary_header_packets = 0;
349 /* update start of data pointer */
350 p_stream->i_data_start = stream_Tell( p_demux->s );
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;
359 if( p_stream->b_reinit )
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 );
365 if( p_stream->i_pcr >= 0 )
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 )
375 ogg_int64_t start_time;
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. */
381 Ogg_OpusPacketDuration( p_stream, &oggpacket );
382 start_time = p_stream->i_previous_granulepos;
385 start_time = start_time > duration ?
386 start_time - duration : 0;
388 if( p_stream->i_pre_skip > start_time )
390 p_stream->i_skip_frames +=
391 p_stream->i_pre_skip - start_time;
397 p_stream->i_interpolated_pcr = -1;
398 p_stream->i_previous_granulepos = -1;
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 )
408 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
410 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
414 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
415 VLC_TS_0 + p_stream->i_pcr );
421 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
424 if( !p_sys->b_page_waiting )
428 /* if a page was waiting, it's now processed */
429 p_sys->b_page_waiting = false;
432 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
434 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
436 if( p_stream->fmt.i_cat == SPU_ES )
438 if( p_stream->i_interpolated_pcr < 0 )
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;
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 );
451 static void Ogg_ResetStreamHelper( demux_sys_t *p_sys )
453 for( int i = 0; i < p_sys->i_streams; i++ )
455 logical_stream_t *p_stream = p_sys->pp_stream[i];
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 );
464 ogg_sync_reset( &p_sys->oy );
467 /*****************************************************************************
469 *****************************************************************************/
470 static int Control( demux_t *p_demux, int i_query, va_list args )
472 demux_sys_t *p_sys = p_demux->p_sys;
480 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
482 vlc_meta_Merge( p_meta, p_sys->p_meta );
485 case DEMUX_HAS_UNSUPPORTED_META:
486 pb_bool = (bool*)va_arg( args, bool* );
491 pi64 = (int64_t*)va_arg( args, int64_t * );
492 *pi64 = p_sys->i_pcr;
498 case DEMUX_GET_ATTACHMENTS:
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 * );
504 if( p_sys->i_attachments <= 0 )
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] );
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 )
523 Ogg_ResetStreamHelper( p_sys );
524 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
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,
530 pi64 = (int64_t*)va_arg( args, int64_t * );
531 *pi64 = p_sys->i_length * 1000000;
534 case DEMUX_GET_TITLE_INFO:
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* );
541 if( p_sys->i_seekpoints > 0 )
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++ )
548 TAB_APPEND( p_title->i_seekpoint, p_title->seekpoint, p_sys->pp_seekpoints[i] );
550 *pi_title_offset = 0;
551 *pi_seekpoint_offset = 0;
555 case DEMUX_SET_TITLE:
557 const int i_title = (int)va_arg( args, int );
562 case DEMUX_SET_SEEKPOINT:
564 const int i_seekpoint = (int)va_arg( args, int );
565 if( i_seekpoint > p_sys->i_seekpoints )
567 if( p_sys->i_bos > 0 )
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 ) )
580 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
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 )
593 demux_sys_t *p_ogg = p_demux->p_sys ;
597 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
599 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
601 i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
605 ogg_sync_wrote( &p_ogg->oy, i_read );
611 /****************************************************************************
612 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
614 ****************************************************************************/
615 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
616 ogg_packet *p_oggpacket )
618 p_stream->i_end_trim = 0;
619 /* Convert the granulepos into a pcr */
620 if( p_oggpacket->granulepos >= 0 )
622 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
623 p_stream->fmt.i_codec == VLC_CODEC_KATE )
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 );
630 p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
631 * INT64_C(1000000) / p_stream->f_rate;
633 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
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;
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 )
648 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
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);
657 if (sample >= p_stream->i_pre_skip)
658 sample -= p_stream->i_pre_skip;
661 p_stream->i_pcr = sample * INT64_C(1000000) / p_stream->f_rate;
664 p_stream->i_pcr += VLC_TS_0;
665 p_stream->i_interpolated_pcr = p_stream->i_pcr;
670 p_stream->i_pcr = -1;
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 &&
680 Ogg_OpusPacketDuration( p_stream, p_oggpacket ) ) > 0 )
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;
690 p_stream->i_interpolated_pcr =
691 VLC_TS_0 + sample * INT64_C(1000000) / p_stream->f_rate;
693 else if( p_stream->fmt.i_bitrate )
695 p_stream->i_interpolated_pcr +=
696 ( p_oggpacket->bytes * INT64_C(1000000) /
697 p_stream->fmt.i_bitrate / 8 );
700 p_stream->i_previous_granulepos = p_oggpacket->granulepos;
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 )
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;
716 if( p_oggpacket->bytes >= 7 &&
717 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
719 /* it's an Annodex packet -- skip it (do nothing) */
722 else if( p_oggpacket->bytes >= 7 &&
723 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
725 /* it's an AnxData packet -- skip it (do nothing) */
729 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT && p_oggpacket->bytes > 0 &&
730 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
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 );
736 if( p_stream->b_force_backup )
739 p_stream->i_packets_backup++;
740 switch( p_stream->fmt.i_codec )
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;
751 if( p_stream->i_packets_backup == 2 )
752 p_stream->b_force_backup = false;
757 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
759 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
760 p_stream->b_force_backup = false;
762 else if( p_stream->fmt.audio.i_rate )
764 p_stream->b_force_backup = false;
765 if( p_oggpacket->bytes >= 9 )
767 p_oggpacket->packet += 9;
768 p_oggpacket->bytes -= 9;
775 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers )
776 p_stream->b_force_backup = false;
781 p_stream->b_force_backup = false;
786 /* Backup the ogg packet (likely an header packet) */
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 )
794 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
795 p_oggpacket->packet, p_oggpacket->bytes );
800 p_stream->i_headers = 0;
801 p_stream->p_headers = NULL;
805 else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
806 p_oggpacket->bytes, p_oggpacket->packet ) )
808 p_stream->i_headers = 0;
809 p_stream->p_headers = NULL;
811 if( p_stream->i_headers > 0 )
813 if( !p_stream->b_force_backup )
815 /* Last header received, commit changes */
816 free( p_stream->fmt.p_extra );
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 );
824 p_stream->fmt.i_extra = 0;
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 );
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 );
834 /* we're not at BOS anymore for this logical stream */
839 b_selected = false; /* Discard the header packet */
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 )
848 if( p_stream->i_pcr >= 0 )
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 )
856 /* Call the pace control */
857 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
858 VLC_TS_0 + p_stream->i_pcr );
861 p_stream->i_previous_pcr = p_stream->i_pcr;
863 /* The granulepos is the end date of the sample */
864 i_pts = p_stream->i_pcr;
868 /* Convert the granulepos into the next pcr */
869 i_interpolated_pts = p_stream->i_interpolated_pcr;
870 Ogg_UpdatePCR( p_stream, p_oggpacket );
872 /* SPU streams are typically discontinuous, do not mind large gaps */
873 if( p_stream->fmt.i_cat != SPU_ES )
875 if( p_stream->i_pcr >= 0 )
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 )
883 /* Call the pace control */
884 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_stream->i_pcr );
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 )
895 p_stream->i_previous_pcr = p_stream->i_pcr;
897 /* The granulepos is the start date of the sample */
898 i_pts = p_stream->i_pcr;
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 */
908 if( !( p_block = block_Alloc( p_oggpacket->bytes ) ) ) return;
911 /* may need to preroll after a seek */
912 if ( p_stream->i_skip_frames > 0 )
914 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
917 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
918 if( p_stream->i_skip_frames > duration )
920 p_block->i_flags |= BLOCK_FLAG_PREROLL;
921 p_block->i_nb_samples = 0;
922 p_stream->i_skip_frames -= duration;
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 )
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;
934 p_stream->i_skip_frames = 0;
939 p_block->i_flags |= BLOCK_FLAG_PREROLL;
940 p_stream->i_skip_frames--;
943 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
944 p_block->i_nb_samples = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
948 if( i_pts == VLC_TS_INVALID ) i_pts = VLC_TS_0;
949 else if( i_pts == -1 && i_interpolated_pts == VLC_TS_INVALID )
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;
955 if( p_stream->fmt.i_cat == AUDIO_ES )
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;
961 else if( p_stream->fmt.i_cat == SPU_ES )
963 p_block->i_dts = p_block->i_pts = i_pts;
964 p_block->i_length = 0;
966 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
968 p_block->i_dts = p_block->i_pts = i_pts;
969 if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
971 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
974 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
976 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
977 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
979 uint64_t u_pnum = dts + delay;
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 */
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;
991 p_block->i_dts = i_pts;
992 p_block->i_pts = VLC_TS_INVALID;
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 )
1005 if( p_oggpacket->bytes <= 0 )
1007 msg_Dbg( p_demux, "discarding 0 sized packet" );
1008 block_Release( p_block );
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;
1015 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
1017 /* But with subtitles we need to retrieve the duration first */
1018 int i, lenbytes = 0;
1020 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
1022 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
1024 lenbytes = lenbytes << 8;
1025 lenbytes += *(p_oggpacket->packet + i_header_len - i);
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' ) )
1034 p_block->i_length = (mtime_t)lenbytes * 1000;
1039 if( p_block->i_buffer >= (unsigned int)i_header_len )
1040 p_block->i_buffer -= i_header_len;
1042 p_block->i_buffer = 0;
1045 if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
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 );
1053 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
1054 p_oggpacket->bytes - i_header_len );
1056 es_out_Send( p_demux->out, p_stream->p_es, p_block );
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 )
1063 static const int silk_fs_div[4] = { 6000, 3000, 1500, 1000 };
1069 if( p_oggpacket->bytes < 1 )
1070 return VLC_EGENERIC;
1071 toc = p_oggpacket->packet[0];
1082 if( p_oggpacket->bytes < 2 )
1083 return VLC_EGENERIC;
1084 nframes = p_oggpacket->packet[1]&0x3F;
1087 i_rate = (int)p_stream->fmt.audio.i_rate;
1089 frame_size = (i_rate << (toc >> 3 & 3)) / 400;
1090 else if( ( toc&0x60 ) == 0x60 )
1091 frame_size = i_rate/(100 >> (toc >> 3 & 1));
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;
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.
1108 * On success this function returns VLC_SUCCESS.
1109 ****************************************************************************/
1110 static int Ogg_FindLogicalStreams( demux_t *p_demux )
1112 demux_sys_t *p_ogg = p_demux->p_sys ;
1113 ogg_packet oggpacket;
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 );
1120 while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
1123 if( ogg_page_bos( &p_ogg->current_page ) )
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 ) )
1130 logical_stream_t *p_stream;
1132 p_stream = malloc( sizeof(logical_stream_t) );
1133 if( unlikely( !p_stream ) )
1136 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
1138 memset( p_stream, 0, sizeof(logical_stream_t) );
1139 p_stream->p_headers = 0;
1140 p_stream->i_secondary_header_packets = 0;
1142 p_stream->i_keyframe_offset = 0;
1143 p_stream->i_skip_frames = 0;
1145 p_stream->i_data_start = 0;
1147 es_format_Init( &p_stream->fmt, 0, 0 );
1148 es_format_Init( &p_stream->fmt_old, 0, 0 );
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 );
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 )
1158 /* error. stream version mismatch perhaps */
1159 msg_Err( p_demux, "error reading first page of "
1160 "Ogg bitstream data" );
1161 return VLC_EGENERIC;
1164 /* FIXME: check return value */
1165 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
1167 /* Check for Vorbis header */
1168 if( oggpacket.bytes >= 7 &&
1169 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
1171 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
1172 msg_Dbg( p_demux, "found vorbis header" );
1174 /* Check for Speex header */
1175 else if( oggpacket.bytes >= 5 &&
1176 ! memcmp( oggpacket.packet, "Speex", 5 ) )
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 );
1184 /* Check for Opus header */
1185 else if( oggpacket.bytes >= 8 &&
1186 ! memcmp( oggpacket.packet, "OpusHead", 8 ) )
1188 Ogg_ReadOpusHeader( p_demux, p_stream, &oggpacket );
1189 msg_Dbg( p_demux, "found opus header, channels: %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;
1195 /* Check for Flac header (< version 1.1.1) */
1196 else if( oggpacket.bytes >= 4 &&
1197 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
1199 msg_Dbg( p_demux, "found FLAC header" );
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;
1206 p_stream->fmt.i_cat = AUDIO_ES;
1207 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
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 ) )
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],
1221 p_stream->b_force_backup = true;
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 );
1228 /* Check for Theora header */
1229 else if( oggpacket.bytes >= 7 &&
1230 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
1232 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
1235 "found theora header, bitrate: %i, rate: %f",
1236 p_stream->fmt.i_bitrate, p_stream->f_rate );
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 ) ) )
1244 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1245 msg_Dbg( p_demux, "found dirac header" );
1248 msg_Warn( p_demux, "found dirac header isn't decodable" );
1253 /* Check for Tarkin header */
1254 else if( oggpacket.bytes >= 7 &&
1255 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
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;
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 */
1270 "found tarkin header, bitrate: %i, rate: %f",
1271 p_stream->fmt.i_bitrate, p_stream->f_rate );
1273 /* Check for Annodex header */
1274 else if( oggpacket.bytes >= 7 &&
1275 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1277 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1278 /* kill annodex track */
1282 /* Check for Annodex header */
1283 else if( oggpacket.bytes >= 7 &&
1284 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1286 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1288 /* Check for Kate header */
1289 else if( oggpacket.bytes >= 8 &&
1290 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1292 Ogg_ReadKateHeader( p_stream, &oggpacket );
1293 msg_Dbg( p_demux, "found kate header" );
1295 else if( oggpacket.bytes >= 142 &&
1296 !memcmp( &oggpacket.packet[1],
1297 "Direct Show Samples embedded in Ogg", 35 ))
1299 /* Old header type */
1301 /* Check for video header (old format) */
1302 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1303 oggpacket.bytes >= 184 )
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 );
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 )
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));
1330 "fps: %f, width:%i; height:%i, bitcount:%i",
1332 p_stream->fmt.video.i_width,
1333 p_stream->fmt.video.i_height,
1334 p_stream->fmt.video.i_bits_per_pixel);
1337 /* Check for audio header (old format) */
1338 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1341 unsigned int i_format_tag;
1343 p_stream->fmt.i_cat = AUDIO_ES;
1345 i_extra_size = GetWLE((oggpacket.packet+140));
1346 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
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 );
1354 p_stream->fmt.i_extra = 0;
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));
1370 wf_tag_to_fourcc( i_format_tag,
1371 &p_stream->fmt.i_codec, 0 );
1373 if( p_stream->fmt.i_codec ==
1374 VLC_FOURCC('u','n','d','f') )
1376 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1377 ( i_format_tag >> 8 ) & 0xff,
1378 i_format_tag & 0xff );
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",
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 );
1394 msg_Dbg( p_demux, "stream %d has an old header "
1395 "but is of an unknown type", p_ogg->i_streams-1 );
1400 else if( oggpacket.bytes >= 44+1 &&
1401 (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER )
1403 stream_header_t tmp;
1404 stream_header_t *st = &tmp;
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)
1415 /* Check for video header (new format) */
1416 if( !strncmp( st->streamtype, "video", 5 ) &&
1417 oggpacket.bytes >= 52+1 )
1419 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1420 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1422 p_stream->fmt.i_cat = VIDEO_ES;
1424 /* We need to get rid of the header packet */
1425 ogg_stream_packetout( &p_stream->os, &oggpacket );
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 );
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;
1443 "fps: %f, width:%i; height:%i, bitcount:%i",
1445 p_stream->fmt.video.i_width,
1446 p_stream->fmt.video.i_height,
1447 p_stream->fmt.video.i_bits_per_pixel );
1449 /* Check for audio header (new format) */
1450 else if( !strncmp( st->streamtype, "audio", 5 ) &&
1451 oggpacket.bytes >= 56+1 )
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] );
1461 p_stream->fmt.i_cat = AUDIO_ES;
1463 /* We need to get rid of the header packet */
1464 ogg_stream_packetout( &p_stream->os, &oggpacket );
1466 i_extra_size = st->size - 56;
1468 if( i_extra_size > 0 &&
1469 i_extra_size < oggpacket.bytes - 1 - 56 )
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 );
1477 p_stream->fmt.i_extra = 0;
1480 memcpy( p_buffer, st->subtype, 4 );
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;
1492 wf_tag_to_fourcc( i_format_tag,
1493 &p_stream->fmt.i_codec, 0 );
1495 if( p_stream->fmt.i_codec ==
1496 VLC_FOURCC('u','n','d','f') )
1498 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1499 ( i_format_tag >> 8 ) & 0xff,
1500 i_format_tag & 0xff );
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",
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 );
1513 /* Check for text (subtitles) header */
1514 else if( !strncmp(st->streamtype, "text", 4) )
1516 /* We need to get rid of the header packet */
1517 ogg_stream_packetout( &p_stream->os, &oggpacket );
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 */
1526 msg_Dbg( p_demux, "stream %d has a header marker "
1527 "but is of an unknown type", p_ogg->i_streams-1 );
1532 else if( oggpacket.bytes >= 7 &&
1533 ! memcmp( oggpacket.packet, "fishead", 7 ) )
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 */
1543 msg_Dbg( p_demux, "stream %d is of unknown type",
1544 p_ogg->i_streams-1 );
1549 if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
1550 return VLC_EGENERIC;
1553 /* we'll need to get all headers for all of those streams
1554 that we have to backup headers for */
1556 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1558 if( p_ogg->pp_stream[i_stream]->b_force_backup )
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
1566 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1568 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1569 &p_ogg->current_page ) == 0 )
1571 p_ogg->b_page_waiting = true;
1580 return VLC_EGENERIC;
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 )
1589 demux_sys_t *p_ogg = p_demux->p_sys ;
1590 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
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 )
1597 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1598 return VLC_EGENERIC;
1601 p_ogg->i_bitrate = 0;
1603 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1605 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1607 p_stream->p_es = NULL;
1609 /* initialise kframe index */
1612 /* Try first to reuse an old ES */
1614 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1615 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1617 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1619 p_stream->p_es = p_old_stream->p_es;
1620 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1622 p_old_stream->p_es = NULL;
1623 p_old_stream = NULL;
1626 if( !p_stream->p_es )
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;
1633 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1636 // TODO: something to do here ?
1637 if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1639 /* Set the CMML stream active */
1640 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1643 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1645 p_stream->i_pcr = p_stream->i_previous_pcr =
1646 p_stream->i_interpolated_pcr = -1;
1647 p_stream->b_reinit = false;
1650 if( p_ogg->p_old_stream )
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;
1659 /* get total frame count for video stream; we will need this for seeking */
1660 p_ogg->i_total_frames = 0;
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 )
1670 demux_sys_t *p_ogg = p_demux->p_sys ;
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 );
1678 p_ogg->i_bitrate = 0;
1679 p_ogg->i_streams = 0;
1680 p_ogg->pp_stream = NULL;
1684 vlc_meta_Delete( p_ogg->p_meta );
1685 p_ogg->p_meta = NULL;
1689 * This function delete and release all data associated to a logical_stream_t
1691 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1693 if( p_stream->p_es )
1694 es_out_Del( p_demux->out, p_stream->p_es );
1696 ogg_stream_clear( &p_stream->os );
1697 free( p_stream->p_headers );
1699 es_format_Clean( &p_stream->fmt_old );
1700 es_format_Clean( &p_stream->fmt );
1702 if ( p_stream->idx != NULL)
1704 oggseek_index_entries_free( p_stream->idx );
1710 * This function check if a we need to reset a decoder in case we are
1713 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
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 ) )
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 ) )
1727 bool b_match = i_new_count == i_old_count;
1728 for( unsigned i = 0; i < i_new_count && b_match; i++ )
1730 /* Ignore vorbis comment */
1733 if( pi_new_size[i] != pi_old_size[i] ||
1734 memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
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] );
1745 static bool Ogg_IsOpusFormatCompatible( const es_format_t *p_new,
1746 const es_format_t *p_old )
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 ) )
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 ) )
1758 bool b_match = false;
1759 if( i_new_count == i_old_count && i_new_count > 0 )
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 )
1777 i_old_channel_count = p_old_head[9];
1778 switch( p_old_head[18] )
1781 i_old_stream_count = 1;
1782 i_old_coupled_count = i_old_channel_count - 1;
1785 if( pi_old_size[0] >= 21U + i_old_channel_count )
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;
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 )
1799 i_new_channel_count = p_new_head[9];
1800 switch( p_new_head[18] )
1803 i_new_stream_count = 1;
1804 i_new_coupled_count = i_new_channel_count - 1;
1807 if( pi_new_size[0] >= 21U + i_new_channel_count )
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;
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;
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] );
1829 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1831 bool b_compatible = false;
1832 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
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 );
1842 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1844 return !b_compatible;
1846 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const void *p_headers, unsigned i_headers, unsigned i_skip )
1848 demux_sys_t *p_ogg = p_demux->p_sys;
1850 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1851 void *pp_data[XIPH_MAX_HEADER_COUNT];
1853 if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
1856 /* TODO how to handle multiple comments properly ? */
1857 if( i_count >= 2 && pi_size[1] > i_skip )
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 )
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 );
1874 if( p_ogg->i_seekpoints > 1 )
1876 p_demux->info.i_update |= INPUT_UPDATE_TITLE_LIST;
1879 for( unsigned i = 0; i < i_count; i++ )
1882 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1884 demux_sys_t *p_ogg = p_demux->p_sys;
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 );
1893 case VLC_CODEC_OPUS:
1894 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 8 );
1896 case VLC_CODEC_SPEEX:
1897 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
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 );
1907 case VLC_CODEC_FLAC:
1908 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1912 case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1913 case VLC_CODEC_DIRAC:
1918 p_demux->info.i_update |= INPUT_UPDATE_META;
1921 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream,
1924 int64_t last_packet = oggseek_get_last_frame( p_demux, p_stream );
1925 return ( last_packet >= 0 ) ? last_packet / f_rate : -1;
1928 static void Ogg_ReadTheoraHeader( demux_t *p_demux, logical_stream_t *p_stream,
1929 ogg_packet *p_oggpacket )
1932 int i_fps_numerator;
1933 int i_fps_denominator;
1934 int i_keyframe_frequency_force;
1940 p_stream->fmt.i_cat = VIDEO_ES;
1941 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1943 /* Signal that we want to keep a backup of the theora
1944 * stream headers. They will be used when switching between
1946 p_stream->b_force_backup = true;
1948 /* Cheat and get additionnal info ;) */
1949 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1950 bs_skip( &bitstream, 56 );
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 */
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 */
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 */
1968 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1969 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1971 bs_read( &bitstream, 8 ); /* colorspace */
1972 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1973 bs_read( &bitstream, 6 ); /* quality */
1975 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
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 )
1982 p_stream->i_granule_shift++;
1983 i_keyframe_frequency_force >>= 1;
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;
1990 if ( i_version >= 3002001 )
1992 p_stream->i_keyframe_offset = 1;
1994 if ( p_demux->p_sys->i_length < 0 )
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;
2003 static void Ogg_ReadVorbisHeader( demux_t *p_demux, logical_stream_t *p_stream,
2004 ogg_packet *p_oggpacket )
2008 p_stream->fmt.i_cat = AUDIO_ES;
2009 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2011 /* Signal that we want to keep a backup of the vorbis
2012 * stream headers. They will be used when switching between
2014 p_stream->b_force_backup = true;
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 );
2026 if ( p_demux->p_sys->i_length < 0 )
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;
2034 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
2035 ogg_packet *p_oggpacket )
2039 p_stream->fmt.i_cat = AUDIO_ES;
2040 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2042 /* Signal that we want to keep a backup of the speex
2043 * stream headers. They will be used when switching between
2045 p_stream->b_force_backup = true;
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 );
2060 static void Ogg_ReadOpusHeader( demux_t *p_demux,
2061 logical_stream_t *p_stream,
2062 ogg_packet *p_oggpacket )
2066 p_stream->fmt.i_cat = AUDIO_ES;
2067 p_stream->fmt.i_codec = VLC_CODEC_OPUS;
2069 /* Signal that we want to keep a backup of the opus
2070 * stream headers. They will be used when switching between
2072 p_stream->b_force_backup = true;
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;
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 );
2087 if ( p_demux->p_sys->i_length < 0 )
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;
2095 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
2096 ogg_packet *p_oggpacket )
2098 /* Parse the STREAMINFO metadata */
2101 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
2104 if( p_oggpacket->bytes > 0 && bs_read( &s, 7 ) != 0 )
2106 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
2110 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
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);
2117 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
2118 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
2122 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
2125 /* Fake this as the last metadata block */
2126 *((uint8_t*)p_oggpacket->packet) |= 0x80;
2129 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
2130 ogg_packet *p_oggpacket )
2138 p_stream->fmt.i_cat = SPU_ES;
2139 p_stream->fmt.i_codec = VLC_CODEC_KATE;
2141 /* Signal that we want to keep a backup of the kate
2142 * stream headers. They will be used when switching between
2144 p_stream->b_force_backup = true;
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;
2157 p_stream->fmt.psz_language = malloc(16);
2158 if( p_stream->fmt.psz_language )
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 */
2166 for( n = 0; n < 16; n++ )
2167 oggpack_read(&opb,8);
2169 p_stream->fmt.psz_description = malloc(16);
2170 if( p_stream->fmt.psz_description )
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 */
2176 /* Now find a localized user readable description for this category */
2177 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
2180 free( p_stream->fmt.psz_description );
2181 p_stream->fmt.psz_description = psz_desc;
2186 for( n = 0; n < 16; n++ )
2187 oggpack_read(&opb,8);
2191 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,
2192 logical_stream_t *p_stream,
2193 ogg_packet *p_oggpacket )
2195 if( p_oggpacket->bytes >= 28 &&
2196 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
2200 uint16_t major_version;
2201 uint16_t minor_version;
2202 uint64_t timebase_numerator;
2203 uint64_t timebase_denominator;
2205 Ogg_ReadTheoraHeader( p_demux, p_stream, p_oggpacket );
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] );
2214 msg_Dbg( p_demux, "Annodex info: version %"PRIu16".%"PRIu16" "
2215 "Timebase %"PRId64" / %"PRId64,
2216 major_version, minor_version,
2217 timebase_numerator, timebase_denominator );
2219 else if( p_oggpacket->bytes >= 42 &&
2220 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
2222 uint64_t granule_rate_numerator;
2223 uint64_t granule_rate_denominator;
2224 char content_type_string[1024];
2226 /* Read in Annodex header fields */
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] );
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 ) )
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 );
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 );
2249 p_stream->f_rate = (float) granule_rate_numerator /
2250 (float) granule_rate_denominator;
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) )
2257 /* n.b. WAVs are unsupported right now */
2258 p_stream->fmt.i_cat = UNKNOWN_ES;
2260 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
2262 p_stream->fmt.i_cat = AUDIO_ES;
2263 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2265 p_stream->b_force_backup = true;
2267 else if( !strncmp(content_type_string, "audio/x-speex", 13) )
2269 p_stream->fmt.i_cat = AUDIO_ES;
2270 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2272 p_stream->b_force_backup = true;
2274 else if( !strncmp(content_type_string, "video/x-theora", 14) )
2276 p_stream->fmt.i_cat = VIDEO_ES;
2277 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2279 p_stream->b_force_backup = true;
2281 else if( !strncmp(content_type_string, "video/x-xvid", 12) )
2283 p_stream->fmt.i_cat = VIDEO_ES;
2284 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
2286 p_stream->b_force_backup = true;
2288 else if( !strncmp(content_type_string, "video/mpeg", 10) )
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;
2294 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
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;
2303 static uint32_t dirac_uint( bs_t *p_bs )
2305 uint32_t u_count = 0, u_value = 0;
2307 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
2311 u_value |= bs_read( p_bs, 1 );
2314 return (1<<u_count) - 1 + u_value;
2317 static int dirac_bool( bs_t *p_bs )
2319 return bs_read( p_bs, 1 );
2322 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
2323 ogg_packet *p_oggpacket )
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},
2332 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
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,
2337 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
2341 p_stream->i_granule_shift = 22; /* not 32 */
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;
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 */
2355 uint32_t u_video_format = dirac_uint( &bs ); /* index */
2356 if( u_video_format >= u_dirac_vidfmt_frate )
2358 /* don't know how to parse this ogg dirac stream */
2362 if( dirac_bool( &bs ) )
2364 dirac_uint( &bs ); /* frame_width */
2365 dirac_uint( &bs ); /* frame_height */
2368 if( dirac_bool( &bs ) )
2370 dirac_uint( &bs ); /* chroma_format */
2373 if( dirac_bool( &bs ) )
2375 dirac_uint( &bs ); /* scan_format */
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 ) )
2382 uint32_t u_frame_rate_index = dirac_uint( &bs );
2383 if( u_frame_rate_index >= u_dirac_frate_tbl )
2385 /* something is wrong with this stream */
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 )
2392 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
2393 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
2396 p_stream->f_rate = (float) u_n / u_d;
2398 /* probably is an ogg dirac es */
2399 p_stream->fmt.i_cat = VIDEO_ES;
2400 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;