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>
44 #include "kate_categories.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_shortname ( "OGG" )
56 set_description( N_("OGG demuxer" ) )
57 set_category( CAT_INPUT )
58 set_subcategory( SUBCAT_INPUT_DEMUX )
59 set_capability( "demux", 50 )
60 set_callbacks( Open, Close )
65 /*****************************************************************************
66 * Definitions of structures and functions used by this plugins
67 *****************************************************************************/
69 /* OggDS headers for the new header format (used in ogm files) */
74 } stream_header_video_t;
80 ogg_int16_t blockalign;
81 ogg_int32_t avgbytespersec;
82 } stream_header_audio_t;
89 ogg_int32_t size; /* size of the structure */
91 ogg_int64_t time_unit; /* in reference time */
92 ogg_int64_t samples_per_unit;
93 ogg_int32_t default_len; /* in media time */
95 ogg_int32_t buffersize;
96 ogg_int16_t bits_per_sample;
102 stream_header_video_t video;
104 stream_header_audio_t audio;
109 /* Some defines from OggDS */
110 #define PACKET_TYPE_HEADER 0x01
111 #define PACKET_TYPE_BITS 0x07
112 #define PACKET_LEN_BITS01 0xc0
113 #define PACKET_LEN_BITS2 0x02
114 #define PACKET_IS_SYNCPOINT 0x08
116 /*****************************************************************************
118 *****************************************************************************/
119 static int Demux ( demux_t * );
120 static int Control( demux_t *, int, va_list );
122 /* Bitstream manipulation */
123 static int Ogg_ReadPage ( demux_t *, ogg_page * );
124 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
125 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
126 static int Ogg_OpusPacketDuration( logical_stream_t *, ogg_packet * );
128 static int Ogg_BeginningOfStream( demux_t *p_demux );
129 static int Ogg_FindLogicalStreams( demux_t *p_demux );
130 static void Ogg_EndOfStream( demux_t *p_demux );
133 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
134 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
137 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers );
138 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream, double f_rate );
140 /* Logical bitstream headers */
141 static void Ogg_ReadTheoraHeader( demux_t *, logical_stream_t *, ogg_packet * );
142 static void Ogg_ReadVorbisHeader( demux_t *, logical_stream_t *, ogg_packet * );
143 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
144 static void Ogg_ReadOpusHeader( demux_t *, logical_stream_t *, ogg_packet * );
145 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
146 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
147 static void Ogg_ReadAnnodexHeader( demux_t *, logical_stream_t *, ogg_packet * );
148 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
150 /*****************************************************************************
151 * Open: initializes ogg demux structures
152 *****************************************************************************/
153 static int Open( vlc_object_t * p_this )
155 demux_t *p_demux = (demux_t *)p_this;
157 const uint8_t *p_peek;
159 /* Check if we are dealing with an ogg stream */
160 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
161 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
167 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
171 p_sys->i_length = -1;
173 /* Set exported functions */
174 p_demux->pf_demux = Demux;
175 p_demux->pf_control = Control;
177 /* Initialize the Ogg physical bitstream parser */
178 ogg_sync_init( &p_sys->oy );
181 TAB_INIT( p_sys->i_seekpoints, p_sys->pp_seekpoints );
186 /*****************************************************************************
187 * Close: frees unused data
188 *****************************************************************************/
189 static void Close( vlc_object_t *p_this )
191 demux_t *p_demux = (demux_t *)p_this;
192 demux_sys_t *p_sys = p_demux->p_sys ;
194 /* Cleanup the bitstream parser */
195 ogg_sync_clear( &p_sys->oy );
197 Ogg_EndOfStream( p_demux );
199 if( p_sys->p_old_stream )
200 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
202 TAB_CLEAN( p_sys->i_seekpoints, p_sys->pp_seekpoints );
207 /*****************************************************************************
208 * Demux: reads and demuxes data packets
209 *****************************************************************************
210 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
211 *****************************************************************************/
212 static int Demux( demux_t * p_demux )
214 demux_sys_t *p_sys = p_demux->p_sys;
215 ogg_packet oggpacket;
217 bool b_skipping = false;
220 if( p_sys->i_eos == p_sys->i_streams )
224 msg_Dbg( p_demux, "end of a group of logical streams" );
225 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
226 * only 1 ES is supported (common case for ogg web radio) */
227 if( p_sys->i_streams == 1 )
229 p_sys->p_old_stream = p_sys->pp_stream[0];
230 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
232 Ogg_EndOfStream( p_demux );
236 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
239 msg_Dbg( p_demux, "beginning of a group of logical streams" );
240 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 );
244 * The first data page of a physical stream is stored in the relevant logical stream
245 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
246 * stream it belongs to if we haven't processed this first page yet. If we do, we
247 * will only process that first page whenever we find the second page for this stream.
248 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
249 * the second page will arrive real quick, this is not fine for Kate, whose second
250 * data page will typically arrive much later.
251 * This means it is now possible to seek right at the start of a stream where the last
252 * logical stream is Kate, without having to wait for the second data page to unblock
253 * the first one, which is the one that triggers the 'no more headers to backup' code.
254 * And, as we all know, seeking without having backed up all headers is bad, since the
255 * codec will fail to initialize if it's missing its headers.
257 if( !p_sys->b_page_waiting)
260 * Demux an ogg page from the stream
262 if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
265 /* Test for End of Stream */
266 if( ogg_page_eos( &p_sys->current_page ) )
271 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
273 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
275 /* if we've just pulled page, look for the right logical stream */
276 if( !p_sys->b_page_waiting )
278 if( p_sys->i_streams == 1 &&
279 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
281 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
282 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
284 p_stream->b_reinit = true;
285 p_stream->i_pcr = VLC_TS_0;
286 p_stream->i_interpolated_pcr = VLC_TS_0;
287 p_stream->i_previous_granulepos = -1;
288 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0);
291 if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
298 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
300 /* Read info from any secondary header packets, if there are any */
301 if( p_stream->i_secondary_header_packets > 0 )
303 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
304 oggpacket.bytes >= 7 &&
305 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
307 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
308 p_stream->i_secondary_header_packets = 0;
310 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
311 oggpacket.bytes >= 7 &&
312 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
314 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
315 p_stream->i_secondary_header_packets = 0;
317 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
319 p_stream->i_secondary_header_packets = 0;
322 /* update start of data pointer */
323 p_stream->i_data_start = stream_Tell( p_demux->s );
327 /* If any streams have i_skip_frames, only decode (pre-roll)
328 * for those streams */
329 if ( b_skipping && p_stream->i_skip_frames == 0 ) continue;
332 if( p_stream->b_reinit )
334 /* If synchro is re-initialized we need to drop all the packets
335 * until we find a new dated one. */
336 Ogg_UpdatePCR( p_stream, &oggpacket );
338 if( p_stream->i_pcr >= 0 )
340 p_stream->b_reinit = false;
341 /* For Opus, trash the first 80 ms of decoded output as
342 well, to avoid blowing out speakers if we get unlucky.
343 Opus predicts content from prior frames, which can go
344 badly if we seek right where the stream goes from very
345 quiet to very loud. It will converge after a bit. */
346 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
348 ogg_int64_t start_time;
350 p_stream->i_skip_frames = 80*48;
351 /* Make sure we never play audio from within the
352 pre-skip at the beginning of the stream. */
354 Ogg_OpusPacketDuration( p_stream, &oggpacket );
355 start_time = p_stream->i_previous_granulepos;
358 start_time = start_time > duration ?
359 start_time - duration : 0;
361 if( p_stream->i_pre_skip > start_time )
363 p_stream->i_skip_frames +=
364 p_stream->i_pre_skip - start_time;
370 p_stream->i_interpolated_pcr = -1;
371 p_stream->i_previous_granulepos = -1;
375 /* An Ogg/vorbis packet contains an end date granulepos */
376 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
377 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
378 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
379 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
381 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
383 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
387 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
388 VLC_TS_0 + p_stream->i_pcr );
394 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
397 if( !p_sys->b_page_waiting )
401 /* if a page was waiting, it's now processed */
402 p_sys->b_page_waiting = false;
405 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
407 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
409 if( p_stream->fmt.i_cat == SPU_ES )
411 if( p_stream->i_interpolated_pcr < 0 )
414 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
415 p_sys->i_pcr = p_stream->i_interpolated_pcr;
418 if( p_sys->i_pcr >= 0 && ! b_skipping )
419 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
424 static void Ogg_ResetStreamHelper( demux_sys_t *p_sys )
426 for( int i = 0; i < p_sys->i_streams; i++ )
428 logical_stream_t *p_stream = p_sys->pp_stream[i];
430 /* we'll trash all the data until we find the next pcr */
431 p_stream->b_reinit = true;
432 p_stream->i_pcr = -1;
433 p_stream->i_interpolated_pcr = -1;
434 p_stream->i_previous_granulepos = -1;
435 ogg_stream_reset( &p_stream->os );
437 ogg_sync_reset( &p_sys->oy );
440 /*****************************************************************************
442 *****************************************************************************/
443 static int Control( demux_t *p_demux, int i_query, va_list args )
445 demux_sys_t *p_sys = p_demux->p_sys;
453 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
455 vlc_meta_Merge( p_meta, p_sys->p_meta );
458 case DEMUX_HAS_UNSUPPORTED_META:
459 pb_bool = (bool*)va_arg( args, bool* );
464 pi64 = (int64_t*)va_arg( args, int64_t * );
465 *pi64 = p_sys->i_pcr;
471 case DEMUX_GET_ATTACHMENTS:
473 input_attachment_t ***ppp_attach =
474 (input_attachment_t***)va_arg( args, input_attachment_t*** );
475 int *pi_int = (int*)va_arg( args, int * );
477 if( p_sys->i_attachments <= 0 )
480 *pi_int = p_sys->i_attachments;
481 *ppp_attach = xmalloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
482 for( int i = 0; i < p_sys->i_attachments; i++ )
483 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
487 case DEMUX_SET_POSITION:
488 /* forbid seeking if we haven't initialized all logical bitstreams yet;
489 if we allowed, some headers would not get backed up and decoder init
490 would fail, making that logical stream unusable */
491 if( p_sys->i_bos > 0 )
496 Ogg_ResetStreamHelper( p_sys );
497 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
499 case DEMUX_GET_LENGTH:
500 if ( p_sys->i_length < 0 )
501 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
503 pi64 = (int64_t*)va_arg( args, int64_t * );
504 *pi64 = p_sys->i_length * 1000000;
507 case DEMUX_GET_TITLE_INFO:
509 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
510 int *pi_int = (int*)va_arg( args, int* );
511 int *pi_title_offset = (int*)va_arg( args, int* );
512 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
514 if( p_sys->i_seekpoints > 0 )
517 *ppp_title = malloc( sizeof( input_title_t**) );
518 input_title_t *p_title = (*ppp_title)[0] = vlc_input_title_New();
519 for( int i = 0; i < p_sys->i_seekpoints; i++ )
521 TAB_APPEND( p_title->i_seekpoint, p_title->seekpoint, p_sys->pp_seekpoints[i] );
523 *pi_title_offset = 0;
524 *pi_seekpoint_offset = 0;
528 case DEMUX_SET_TITLE:
530 const int i_title = (int)va_arg( args, int );
535 case DEMUX_SET_SEEKPOINT:
537 const int i_seekpoint = (int)va_arg( args, int );
538 if( i_seekpoint > p_sys->i_seekpoints )
540 if( p_sys->i_bos > 0 )
545 Ogg_ResetStreamHelper( p_sys );
546 int64_t i_block = p_sys->pp_seekpoints[i_seekpoint]->i_time_offset * p_sys->i_bitrate / INT64_C(8000000);
547 if( stream_Seek( p_demux->s, i_block ) )
553 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
558 /****************************************************************************
559 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
560 ****************************************************************************
561 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
562 * are at the end of stream.
563 ****************************************************************************/
564 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
566 demux_sys_t *p_ogg = p_demux->p_sys ;
570 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
572 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
574 i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
578 ogg_sync_wrote( &p_ogg->oy, i_read );
584 /****************************************************************************
585 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
587 ****************************************************************************/
588 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
589 ogg_packet *p_oggpacket )
591 p_stream->i_end_trim = 0;
592 /* Convert the granulepos into a pcr */
593 if( p_oggpacket->granulepos >= 0 )
595 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
596 p_stream->fmt.i_codec == VLC_CODEC_KATE )
598 ogg_int64_t iframe = p_oggpacket->granulepos >>
599 p_stream->i_granule_shift;
600 ogg_int64_t pframe = p_oggpacket->granulepos -
601 ( iframe << p_stream->i_granule_shift );
603 p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
604 * INT64_C(1000000) / p_stream->f_rate;
606 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
608 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
609 /* NB, OggDirac granulepos values are in units of 2*picturerate */
610 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
615 sample = p_oggpacket->granulepos;
616 if( p_oggpacket->e_o_s &&
617 p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
618 p_stream->i_previous_granulepos >= 0 )
621 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
624 ogg_int64_t end_sample;
625 end_sample = p_stream->i_previous_granulepos + duration;
626 if( end_sample > sample )
627 p_stream->i_end_trim = (int)(end_sample - sample);
630 if (sample >= p_stream->i_pre_skip)
631 sample -= p_stream->i_pre_skip;
634 p_stream->i_pcr = sample * INT64_C(1000000) / p_stream->f_rate;
637 p_stream->i_pcr += VLC_TS_0;
638 p_stream->i_interpolated_pcr = p_stream->i_pcr;
643 p_stream->i_pcr = -1;
645 /* no granulepos available, try to interpolate the pcr.
646 * If we can't then don't touch the old value. */
647 if( p_stream->fmt.i_cat == VIDEO_ES )
648 /* 1 frame per packet */
649 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
650 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
651 p_stream->i_previous_granulepos >= 0 &&
653 Ogg_OpusPacketDuration( p_stream, p_oggpacket ) ) > 0 )
656 p_oggpacket->granulepos =
657 p_stream->i_previous_granulepos + duration;
658 sample = p_oggpacket->granulepos;
659 if (sample >= p_stream->i_pre_skip)
660 sample -= p_stream->i_pre_skip;
663 p_stream->i_interpolated_pcr =
664 VLC_TS_0 + sample * INT64_C(1000000) / p_stream->f_rate;
666 else if( p_stream->fmt.i_bitrate )
668 p_stream->i_interpolated_pcr +=
669 ( p_oggpacket->bytes * INT64_C(1000000) /
670 p_stream->fmt.i_bitrate / 8 );
673 p_stream->i_previous_granulepos = p_oggpacket->granulepos;
676 /****************************************************************************
677 * Ogg_DecodePacket: Decode an Ogg packet.
678 ****************************************************************************/
679 static void Ogg_DecodePacket( demux_t *p_demux,
680 logical_stream_t *p_stream,
681 ogg_packet *p_oggpacket )
685 int i_header_len = 0;
686 mtime_t i_pts = -1, i_interpolated_pts;
687 demux_sys_t *p_ogg = p_demux->p_sys;
689 if( p_oggpacket->bytes >= 7 &&
690 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
692 /* it's an Annodex packet -- skip it (do nothing) */
695 else if( p_oggpacket->bytes >= 7 &&
696 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
698 /* it's an AnxData packet -- skip it (do nothing) */
702 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT && p_oggpacket->bytes > 0 &&
703 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
705 /* Check the ES is selected */
706 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
707 p_stream->p_es, &b_selected );
709 if( p_stream->b_force_backup )
712 p_stream->i_packets_backup++;
713 switch( p_stream->fmt.i_codec )
715 case VLC_CODEC_VORBIS:
716 case VLC_CODEC_SPEEX:
717 case VLC_CODEC_THEORA:
718 if( p_stream->i_packets_backup == 3 )
719 p_stream->b_force_backup = false;
724 if( p_stream->i_packets_backup == 2 )
725 p_stream->b_force_backup = false;
730 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
732 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
733 p_stream->b_force_backup = false;
735 else if( p_stream->fmt.audio.i_rate )
737 p_stream->b_force_backup = false;
738 if( p_oggpacket->bytes >= 9 )
740 p_oggpacket->packet += 9;
741 p_oggpacket->bytes -= 9;
748 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers )
749 p_stream->b_force_backup = false;
754 p_stream->b_force_backup = false;
759 /* Backup the ogg packet (likely an header packet) */
762 void *p_org = p_stream->p_headers;
763 p_stream->i_headers += p_oggpacket->bytes;
764 p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
765 if( p_stream->p_headers )
767 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
768 p_oggpacket->packet, p_oggpacket->bytes );
773 p_stream->i_headers = 0;
774 p_stream->p_headers = NULL;
778 else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
779 p_oggpacket->bytes, p_oggpacket->packet ) )
781 p_stream->i_headers = 0;
782 p_stream->p_headers = NULL;
784 if( p_stream->i_headers > 0 )
786 if( !p_stream->b_force_backup )
788 /* Last header received, commit changes */
789 free( p_stream->fmt.p_extra );
791 p_stream->fmt.i_extra = p_stream->i_headers;
792 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
793 if( p_stream->fmt.p_extra )
794 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
795 p_stream->i_headers );
797 p_stream->fmt.i_extra = 0;
799 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
800 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
801 p_stream->p_es, &p_stream->fmt );
803 if( p_stream->i_headers > 0 )
804 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
805 p_stream->p_headers, p_stream->i_headers );
807 /* we're not at BOS anymore for this logical stream */
812 b_selected = false; /* Discard the header packet */
815 /* Convert the pcr into a pts */
816 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
817 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
818 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
819 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
821 if( p_stream->i_pcr >= 0 )
823 /* This is for streams where the granulepos of the header packets
824 * doesn't match these of the data packets (eg. ogg web radios). */
825 if( p_stream->i_previous_pcr == 0 &&
826 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
829 /* Call the pace control */
830 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
831 VLC_TS_0 + p_stream->i_pcr );
834 p_stream->i_previous_pcr = p_stream->i_pcr;
836 /* The granulepos is the end date of the sample */
837 i_pts = p_stream->i_pcr;
841 /* Convert the granulepos into the next pcr */
842 i_interpolated_pts = p_stream->i_interpolated_pcr;
843 Ogg_UpdatePCR( p_stream, p_oggpacket );
845 /* SPU streams are typically discontinuous, do not mind large gaps */
846 if( p_stream->fmt.i_cat != SPU_ES )
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, VLC_TS_0 + p_stream->i_pcr );
862 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
863 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
864 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
865 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
866 p_stream->i_pcr >= 0 )
868 p_stream->i_previous_pcr = p_stream->i_pcr;
870 /* The granulepos is the start date of the sample */
871 i_pts = p_stream->i_pcr;
876 /* This stream isn't currently selected so we don't need to decode it,
877 * but we did need to store its pcr as it might be selected later on */
881 if( !( p_block = block_Alloc( p_oggpacket->bytes ) ) ) return;
884 /* may need to preroll after a seek */
885 if ( p_stream->i_skip_frames > 0 )
887 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
890 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
891 if( p_stream->i_skip_frames > duration )
893 p_block->i_flags |= BLOCK_FLAG_PREROLL;
894 p_block->i_nb_samples = 0;
895 p_stream->i_skip_frames -= duration;
899 p_block->i_nb_samples = duration - p_stream->i_skip_frames;
900 if( p_stream->i_previous_granulepos >=
901 p_block->i_nb_samples + p_stream->i_pre_skip )
903 i_pts = VLC_TS_0 + (p_stream->i_previous_granulepos
904 - p_block->i_nb_samples - p_stream->i_pre_skip) *
905 INT64_C(1000000) / p_stream->f_rate;
907 p_stream->i_skip_frames = 0;
912 p_block->i_flags |= BLOCK_FLAG_PREROLL;
913 p_stream->i_skip_frames--;
916 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
917 p_block->i_nb_samples = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
921 if( i_pts == VLC_TS_INVALID ) i_pts = VLC_TS_0;
922 else if( i_pts == -1 && i_interpolated_pts == VLC_TS_INVALID )
924 else if( i_pts == -1 && p_stream->fmt.i_cat == VIDEO_ES )
925 i_pts = i_interpolated_pts;
926 else if( i_pts == -1 ) i_pts = VLC_TS_INVALID;
928 if( p_stream->fmt.i_cat == AUDIO_ES )
930 p_block->i_dts = p_block->i_pts = i_pts;
931 /* Blatant abuse of the i_length field. */
932 p_block->i_length = p_stream->i_end_trim;
934 else if( p_stream->fmt.i_cat == SPU_ES )
936 p_block->i_dts = p_block->i_pts = i_pts;
937 p_block->i_length = 0;
939 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
941 p_block->i_dts = p_block->i_pts = i_pts;
942 if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
944 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
947 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
949 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
950 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
952 uint64_t u_pnum = dts + delay;
954 p_block->i_dts = p_stream->i_pcr;
955 p_block->i_pts = VLC_TS_INVALID;
956 /* NB, OggDirac granulepos values are in units of 2*picturerate */
958 /* granulepos for dirac is possibly broken, this value should be ignored */
959 if( -1 != p_oggpacket->granulepos )
960 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
964 p_block->i_dts = i_pts;
965 p_block->i_pts = VLC_TS_INVALID;
968 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
969 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
970 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
971 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
972 p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
973 p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
974 p_stream->fmt.i_codec != VLC_CODEC_CMML &&
975 p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
976 p_stream->fmt.i_codec != VLC_CODEC_KATE )
978 if( p_oggpacket->bytes <= 0 )
980 msg_Dbg( p_demux, "discarding 0 sized packet" );
981 block_Release( p_block );
984 /* We remove the header from the packet */
985 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
986 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
988 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
990 /* But with subtitles we need to retrieve the duration first */
993 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
995 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
997 lenbytes = lenbytes << 8;
998 lenbytes += *(p_oggpacket->packet + i_header_len - i);
1001 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
1002 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
1003 p_oggpacket->packet[i_header_len + 1] != 0 &&
1004 p_oggpacket->packet[i_header_len + 1] != '\n' &&
1005 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
1007 p_block->i_length = (mtime_t)lenbytes * 1000;
1012 if( p_block->i_buffer >= (unsigned int)i_header_len )
1013 p_block->i_buffer -= i_header_len;
1015 p_block->i_buffer = 0;
1018 if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
1020 /* FIXME: the biggest hack I've ever done */
1021 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
1022 p_block->i_pts, p_block->i_dts );
1026 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
1027 p_oggpacket->bytes - i_header_len );
1029 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1032 /* Re-implemented to avoid linking against libopus from the demuxer. */
1033 static int Ogg_OpusPacketDuration( logical_stream_t *p_stream,
1034 ogg_packet *p_oggpacket )
1036 static const int silk_fs_div[4] = { 6000, 3000, 1500, 1000 };
1042 if( p_oggpacket->bytes < 1 )
1043 return VLC_EGENERIC;
1044 toc = p_oggpacket->packet[0];
1055 if( p_oggpacket->bytes < 2 )
1056 return VLC_EGENERIC;
1057 nframes = p_oggpacket->packet[1]&0x3F;
1060 i_rate = (int)p_stream->fmt.audio.i_rate;
1062 frame_size = (i_rate << (toc >> 3 & 3)) / 400;
1063 else if( ( toc&0x60 ) == 0x60 )
1064 frame_size = i_rate/(100 >> (toc >> 3 & 1));
1066 frame_size = i_rate*60 / silk_fs_div[toc >> 3 & 3];
1067 nsamples = nframes*frame_size;
1068 if( nsamples*25 > i_rate*3 )
1069 return VLC_EGENERIC;
1073 /****************************************************************************
1074 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
1075 * stream and fill p_ogg.
1076 *****************************************************************************
1077 * The initial page of a logical stream is marked as a 'bos' page.
1078 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
1079 * together and all of the initial pages must appear before any data pages.
1081 * On success this function returns VLC_SUCCESS.
1082 ****************************************************************************/
1083 static int Ogg_FindLogicalStreams( demux_t *p_demux )
1085 demux_sys_t *p_ogg = p_demux->p_sys ;
1086 ogg_packet oggpacket;
1089 p_ogg->i_total_length = stream_Size ( p_demux->s );
1090 msg_Dbg( p_demux, "File length is %"PRId64" bytes", p_ogg->i_total_length );
1093 while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
1096 if( ogg_page_bos( &p_ogg->current_page ) )
1099 /* All is wonderful in our fine fine little world.
1100 * We found the beginning of our first logical stream. */
1101 while( ogg_page_bos( &p_ogg->current_page ) )
1103 logical_stream_t *p_stream;
1105 p_stream = malloc( sizeof(logical_stream_t) );
1106 if( unlikely( !p_stream ) )
1109 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
1111 memset( p_stream, 0, sizeof(logical_stream_t) );
1112 p_stream->p_headers = 0;
1113 p_stream->i_secondary_header_packets = 0;
1115 p_stream->i_keyframe_offset = 0;
1116 p_stream->i_skip_frames = 0;
1118 p_stream->i_data_start = 0;
1120 es_format_Init( &p_stream->fmt, 0, 0 );
1121 es_format_Init( &p_stream->fmt_old, 0, 0 );
1123 /* Setup the logical stream */
1124 p_stream->i_serial_no = ogg_page_serialno( &p_ogg->current_page );
1125 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
1127 /* Extract the initial header from the first page and verify
1128 * the codec type of this Ogg bitstream */
1129 if( ogg_stream_pagein( &p_stream->os, &p_ogg->current_page ) < 0 )
1131 /* error. stream version mismatch perhaps */
1132 msg_Err( p_demux, "error reading first page of "
1133 "Ogg bitstream data" );
1134 return VLC_EGENERIC;
1137 /* FIXME: check return value */
1138 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
1140 /* Check for Vorbis header */
1141 if( oggpacket.bytes >= 7 &&
1142 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
1144 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
1145 msg_Dbg( p_demux, "found vorbis header" );
1147 /* Check for Speex header */
1148 else if( oggpacket.bytes >= 5 &&
1149 ! memcmp( oggpacket.packet, "Speex", 5 ) )
1151 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
1152 msg_Dbg( p_demux, "found speex header, channels: %i, "
1153 "rate: %i, bitrate: %i",
1154 p_stream->fmt.audio.i_channels,
1155 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
1157 /* Check for Opus header */
1158 else if( oggpacket.bytes >= 8 &&
1159 ! memcmp( oggpacket.packet, "OpusHead", 8 ) )
1161 Ogg_ReadOpusHeader( p_demux, p_stream, &oggpacket );
1162 msg_Dbg( p_demux, "found opus header, channels: %i, "
1164 p_stream->fmt.audio.i_channels,
1165 (int)p_stream->i_pre_skip);
1166 p_stream->i_skip_frames = p_stream->i_pre_skip;
1168 /* Check for Flac header (< version 1.1.1) */
1169 else if( oggpacket.bytes >= 4 &&
1170 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
1172 msg_Dbg( p_demux, "found FLAC header" );
1174 /* Grrrr!!!! Did they really have to put all the
1175 * important info in the second header packet!!!
1176 * (STREAMINFO metadata is in the following packet) */
1177 p_stream->b_force_backup = true;
1179 p_stream->fmt.i_cat = AUDIO_ES;
1180 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1182 /* Check for Flac header (>= version 1.1.1) */
1183 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
1184 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
1185 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
1187 int i_packets = ((int)oggpacket.packet[7]) << 8 |
1188 oggpacket.packet[8];
1189 msg_Dbg( p_demux, "found FLAC header version %i.%i "
1190 "(%i header packets)",
1191 oggpacket.packet[5], oggpacket.packet[6],
1194 p_stream->b_force_backup = true;
1196 p_stream->fmt.i_cat = AUDIO_ES;
1197 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1198 oggpacket.packet += 13; oggpacket.bytes -= 13;
1199 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
1201 /* Check for Theora header */
1202 else if( oggpacket.bytes >= 7 &&
1203 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
1205 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
1208 "found theora header, bitrate: %i, rate: %f",
1209 p_stream->fmt.i_bitrate, p_stream->f_rate );
1211 /* Check for Dirac header */
1212 else if( ( oggpacket.bytes >= 5 &&
1213 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
1214 ( oggpacket.bytes >= 9 &&
1215 ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
1217 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1218 msg_Dbg( p_demux, "found dirac header" );
1221 msg_Warn( p_demux, "found dirac header isn't decodable" );
1226 /* Check for Tarkin header */
1227 else if( oggpacket.bytes >= 7 &&
1228 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1232 msg_Dbg( p_demux, "found tarkin header" );
1233 p_stream->fmt.i_cat = VIDEO_ES;
1234 p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1236 /* Cheat and get additionnal info ;) */
1237 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1238 oggpack_adv( &opb, 88 );
1239 oggpack_adv( &opb, 104 );
1240 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1241 p_stream->f_rate = 2; /* FIXME */
1243 "found tarkin header, bitrate: %i, rate: %f",
1244 p_stream->fmt.i_bitrate, p_stream->f_rate );
1246 /* Check for Annodex header */
1247 else if( oggpacket.bytes >= 7 &&
1248 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1250 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1251 /* kill annodex track */
1255 /* Check for Annodex header */
1256 else if( oggpacket.bytes >= 7 &&
1257 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1259 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1261 /* Check for Kate header */
1262 else if( oggpacket.bytes >= 8 &&
1263 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1265 Ogg_ReadKateHeader( p_stream, &oggpacket );
1266 msg_Dbg( p_demux, "found kate header" );
1268 else if( oggpacket.bytes >= 142 &&
1269 !memcmp( &oggpacket.packet[1],
1270 "Direct Show Samples embedded in Ogg", 35 ))
1272 /* Old header type */
1274 /* Check for video header (old format) */
1275 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1276 oggpacket.bytes >= 184 )
1278 p_stream->fmt.i_cat = VIDEO_ES;
1279 p_stream->fmt.i_codec =
1280 VLC_FOURCC( oggpacket.packet[68],
1281 oggpacket.packet[69],
1282 oggpacket.packet[70],
1283 oggpacket.packet[71] );
1284 msg_Dbg( p_demux, "found video header of type: %.4s",
1285 (char *)&p_stream->fmt.i_codec );
1287 p_stream->fmt.video.i_frame_rate = 10000000;
1288 p_stream->fmt.video.i_frame_rate_base =
1289 GetQWLE((oggpacket.packet+164));
1290 p_stream->f_rate = 10000000.0 /
1291 GetQWLE((oggpacket.packet+164));
1292 p_stream->fmt.video.i_bits_per_pixel =
1293 GetWLE((oggpacket.packet+182));
1294 if( !p_stream->fmt.video.i_bits_per_pixel )
1296 p_stream->fmt.video.i_bits_per_pixel = 24;
1297 p_stream->fmt.video.i_width =
1298 GetDWLE((oggpacket.packet+176));
1299 p_stream->fmt.video.i_height =
1300 GetDWLE((oggpacket.packet+180));
1303 "fps: %f, width:%i; height:%i, bitcount:%i",
1305 p_stream->fmt.video.i_width,
1306 p_stream->fmt.video.i_height,
1307 p_stream->fmt.video.i_bits_per_pixel);
1310 /* Check for audio header (old format) */
1311 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1314 unsigned int i_format_tag;
1316 p_stream->fmt.i_cat = AUDIO_ES;
1318 i_extra_size = GetWLE((oggpacket.packet+140));
1319 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1321 p_stream->fmt.i_extra = i_extra_size;
1322 p_stream->fmt.p_extra = malloc( i_extra_size );
1323 if( p_stream->fmt.p_extra )
1324 memcpy( p_stream->fmt.p_extra,
1325 oggpacket.packet + 142, i_extra_size );
1327 p_stream->fmt.i_extra = 0;
1330 i_format_tag = GetWLE((oggpacket.packet+124));
1331 p_stream->fmt.audio.i_channels =
1332 GetWLE((oggpacket.packet+126));
1333 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1334 GetDWLE((oggpacket.packet+128));
1335 p_stream->fmt.i_bitrate =
1336 GetDWLE((oggpacket.packet+132)) * 8;
1337 p_stream->fmt.audio.i_blockalign =
1338 GetWLE((oggpacket.packet+136));
1339 p_stream->fmt.audio.i_bitspersample =
1340 GetWLE((oggpacket.packet+138));
1342 wf_tag_to_fourcc( i_format_tag,
1343 &p_stream->fmt.i_codec, 0 );
1345 if( p_stream->fmt.i_codec ==
1346 VLC_FOURCC('u','n','d','f') )
1348 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1349 ( i_format_tag >> 8 ) & 0xff,
1350 i_format_tag & 0xff );
1353 msg_Dbg( p_demux, "found audio header of type: %.4s",
1354 (char *)&p_stream->fmt.i_codec );
1355 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1356 "%dbits/sample %dkb/s",
1358 p_stream->fmt.audio.i_channels,
1359 p_stream->fmt.audio.i_rate,
1360 p_stream->fmt.audio.i_bitspersample,
1361 p_stream->fmt.i_bitrate / 1024 );
1366 msg_Dbg( p_demux, "stream %d has an old header "
1367 "but is of an unknown type", p_ogg->i_streams-1 );
1372 else if( oggpacket.bytes >= 44+1 &&
1373 (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER )
1375 stream_header_t tmp;
1376 stream_header_t *st = &tmp;
1378 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1379 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1380 st->size = GetDWLE( &oggpacket.packet[1+12] );
1381 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1382 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1383 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1384 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1385 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1387 /* Check for video header (new format) */
1388 if( !strncmp( st->streamtype, "video", 5 ) &&
1389 oggpacket.bytes >= 52+1 )
1391 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1392 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1394 p_stream->fmt.i_cat = VIDEO_ES;
1396 /* We need to get rid of the header packet */
1397 ogg_stream_packetout( &p_stream->os, &oggpacket );
1399 p_stream->fmt.i_codec =
1400 VLC_FOURCC( st->subtype[0], st->subtype[1],
1401 st->subtype[2], st->subtype[3] );
1402 msg_Dbg( p_demux, "found video header of type: %.4s",
1403 (char *)&p_stream->fmt.i_codec );
1405 p_stream->fmt.video.i_frame_rate = 10000000;
1406 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1407 if( st->time_unit <= 0 )
1408 st->time_unit = 400000;
1409 p_stream->f_rate = 10000000.0 / st->time_unit;
1410 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1411 p_stream->fmt.video.i_width = st->sh.video.width;
1412 p_stream->fmt.video.i_height = st->sh.video.height;
1415 "fps: %f, width:%i; height:%i, bitcount:%i",
1417 p_stream->fmt.video.i_width,
1418 p_stream->fmt.video.i_height,
1419 p_stream->fmt.video.i_bits_per_pixel );
1421 /* Check for audio header (new format) */
1422 else if( !strncmp( st->streamtype, "audio", 5 ) &&
1423 oggpacket.bytes >= 56+1 )
1429 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1430 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1431 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1433 p_stream->fmt.i_cat = AUDIO_ES;
1435 /* We need to get rid of the header packet */
1436 ogg_stream_packetout( &p_stream->os, &oggpacket );
1438 i_extra_size = st->size - 56;
1440 if( i_extra_size > 0 &&
1441 i_extra_size < oggpacket.bytes - 1 - 56 )
1443 p_stream->fmt.i_extra = i_extra_size;
1444 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1445 if( p_stream->fmt.p_extra )
1446 memcpy( p_stream->fmt.p_extra, st + 1,
1447 p_stream->fmt.i_extra );
1449 p_stream->fmt.i_extra = 0;
1452 memcpy( p_buffer, st->subtype, 4 );
1454 i_format_tag = strtol(p_buffer,NULL,16);
1455 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1456 if( st->time_unit <= 0 )
1457 st->time_unit = 10000000;
1458 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1459 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1460 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1461 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1463 wf_tag_to_fourcc( i_format_tag,
1464 &p_stream->fmt.i_codec, 0 );
1466 if( p_stream->fmt.i_codec ==
1467 VLC_FOURCC('u','n','d','f') )
1469 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1470 ( i_format_tag >> 8 ) & 0xff,
1471 i_format_tag & 0xff );
1474 msg_Dbg( p_demux, "found audio header of type: %.4s",
1475 (char *)&p_stream->fmt.i_codec );
1476 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1477 "%dbits/sample %dkb/s",
1479 p_stream->fmt.audio.i_channels,
1480 p_stream->fmt.audio.i_rate,
1481 p_stream->fmt.audio.i_bitspersample,
1482 p_stream->fmt.i_bitrate / 1024 );
1484 /* Check for text (subtitles) header */
1485 else if( !strncmp(st->streamtype, "text", 4) )
1487 /* We need to get rid of the header packet */
1488 ogg_stream_packetout( &p_stream->os, &oggpacket );
1490 msg_Dbg( p_demux, "found text subtitles header" );
1491 p_stream->fmt.i_cat = SPU_ES;
1492 p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1493 p_stream->f_rate = 1000; /* granulepos is in millisec */
1497 msg_Dbg( p_demux, "stream %d has a header marker "
1498 "but is of an unknown type", p_ogg->i_streams-1 );
1503 else if( oggpacket.bytes >= 7 &&
1504 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1508 msg_Dbg( p_demux, "stream %d is a skeleton",
1509 p_ogg->i_streams-1 );
1510 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1514 msg_Dbg( p_demux, "stream %d is of unknown type",
1515 p_ogg->i_streams-1 );
1520 if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
1521 return VLC_EGENERIC;
1524 /* we'll need to get all headers for all of those streams
1525 that we have to backup headers for */
1527 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1529 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1534 /* This is the first data page, which means we are now finished
1535 * with the initial pages. We just need to store it in the relevant
1537 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1539 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1540 &p_ogg->current_page ) == 0 )
1542 p_ogg->b_page_waiting = true;
1551 return VLC_EGENERIC;
1554 /****************************************************************************
1555 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1556 * Elementary streams.
1557 ****************************************************************************/
1558 static int Ogg_BeginningOfStream( demux_t *p_demux )
1560 demux_sys_t *p_ogg = p_demux->p_sys ;
1561 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1564 /* Find the logical streams embedded in the physical stream and
1565 * initialize our p_ogg structure. */
1566 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1568 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1569 return VLC_EGENERIC;
1572 p_ogg->i_bitrate = 0;
1574 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1576 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1578 p_stream->p_es = NULL;
1580 /* initialise kframe index */
1583 /* Try first to reuse an old ES */
1585 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1586 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1588 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1590 p_stream->p_es = p_old_stream->p_es;
1591 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1593 p_old_stream->p_es = NULL;
1594 p_old_stream = NULL;
1597 if( !p_stream->p_es )
1599 /* Better be safe than sorry when possible with ogm */
1600 if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1601 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1602 p_stream->fmt.b_packetized = false;
1604 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1607 // TODO: something to do here ?
1608 if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1610 /* Set the CMML stream active */
1611 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1614 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1616 p_stream->i_pcr = p_stream->i_previous_pcr =
1617 p_stream->i_interpolated_pcr = -1;
1618 p_stream->b_reinit = false;
1621 if( p_ogg->p_old_stream )
1623 if( p_ogg->p_old_stream->p_es )
1624 msg_Dbg( p_demux, "old stream not reused" );
1625 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1626 p_ogg->p_old_stream = NULL;
1630 /* get total frame count for video stream; we will need this for seeking */
1631 p_ogg->i_total_frames = 0;
1636 /****************************************************************************
1637 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1638 ****************************************************************************/
1639 static void Ogg_EndOfStream( demux_t *p_demux )
1641 demux_sys_t *p_ogg = p_demux->p_sys ;
1644 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1645 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1646 free( p_ogg->pp_stream );
1649 p_ogg->i_bitrate = 0;
1650 p_ogg->i_streams = 0;
1651 p_ogg->pp_stream = NULL;
1655 vlc_meta_Delete( p_ogg->p_meta );
1656 p_ogg->p_meta = NULL;
1660 * This function delete and release all data associated to a logical_stream_t
1662 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1664 if( p_stream->p_es )
1665 es_out_Del( p_demux->out, p_stream->p_es );
1667 ogg_stream_clear( &p_stream->os );
1668 free( p_stream->p_headers );
1670 es_format_Clean( &p_stream->fmt_old );
1671 es_format_Clean( &p_stream->fmt );
1673 if ( p_stream->idx != NULL)
1675 oggseek_index_entries_free( p_stream->idx );
1681 * This function check if a we need to reset a decoder in case we are
1684 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1686 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1687 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1688 unsigned i_new_count;
1689 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1692 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1693 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1694 unsigned i_old_count;
1695 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1698 bool b_match = i_new_count == i_old_count;
1699 for( unsigned i = 0; i < i_new_count && b_match; i++ )
1701 /* Ignore vorbis comment */
1704 if( pi_new_size[i] != pi_old_size[i] ||
1705 memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
1709 for( unsigned i = 0; i < i_new_count; i++ )
1710 free( pp_new_data[i] );
1711 for( unsigned i = 0; i < i_old_count; i++ )
1712 free( pp_old_data[i] );
1716 static bool Ogg_IsOpusFormatCompatible( const es_format_t *p_new,
1717 const es_format_t *p_old )
1719 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1720 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1721 unsigned i_new_count;
1722 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1724 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1725 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1726 unsigned i_old_count;
1727 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1729 bool b_match = false;
1730 if( i_new_count == i_old_count && i_new_count > 0 )
1732 static const unsigned char default_map[2] = { 0, 1 };
1733 unsigned char *p_old_head;
1734 unsigned char *p_new_head;
1735 const unsigned char *p_old_map;
1736 const unsigned char *p_new_map;
1737 int i_old_channel_count;
1738 int i_new_channel_count;
1739 int i_old_stream_count;
1740 int i_new_stream_count;
1741 int i_old_coupled_count;
1742 int i_new_coupled_count;
1743 p_old_head = (unsigned char *)pp_old_data[0];
1744 i_old_channel_count = i_old_stream_count = i_old_coupled_count = 0;
1745 p_old_map = default_map;
1746 if( pi_old_size[0] >= 19 && p_old_head[8] <= 15 )
1748 i_old_channel_count = p_old_head[9];
1749 switch( p_old_head[18] )
1752 i_old_stream_count = 1;
1753 i_old_coupled_count = i_old_channel_count - 1;
1756 if( pi_old_size[0] >= 21U + i_old_channel_count )
1758 i_old_stream_count = p_old_head[19];
1759 i_old_coupled_count = p_old_head[20];
1760 p_old_map = p_old_head + 21;
1765 p_new_head = (unsigned char *)pp_new_data[0];
1766 i_new_channel_count = i_new_stream_count = i_new_coupled_count = 0;
1767 p_new_map = default_map;
1768 if( pi_new_size[0] >= 19 && p_new_head[8] <= 15 )
1770 i_new_channel_count = p_new_head[9];
1771 switch( p_new_head[18] )
1774 i_new_stream_count = 1;
1775 i_new_coupled_count = i_new_channel_count - 1;
1778 if( pi_new_size[0] >= 21U + i_new_channel_count )
1780 i_new_stream_count = p_new_head[19];
1781 i_new_coupled_count = p_new_head[20];
1782 p_new_map = p_new_head+21;
1787 b_match = i_old_channel_count == i_new_channel_count &&
1788 i_old_stream_count == i_new_stream_count &&
1789 i_old_coupled_count == i_new_coupled_count &&
1790 memcmp(p_old_map, p_new_map,
1791 i_new_channel_count*sizeof(*p_new_map)) == 0;
1793 for( unsigned i = 0; i < i_new_count; i++ )
1794 free( pp_new_data[i] );
1795 for( unsigned i = 0; i < i_old_count; i++ )
1796 free( pp_old_data[i] );
1800 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1802 bool b_compatible = false;
1803 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1806 /* Only Vorbis and Opus are supported. */
1807 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
1808 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1809 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
1810 b_compatible = Ogg_IsOpusFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1813 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1815 return !b_compatible;
1817 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const void *p_headers, unsigned i_headers, unsigned i_skip )
1819 demux_sys_t *p_ogg = p_demux->p_sys;
1821 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1822 void *pp_data[XIPH_MAX_HEADER_COUNT];
1824 if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
1827 /* TODO how to handle multiple comments properly ? */
1828 if( i_count >= 2 && pi_size[1] > i_skip )
1829 vorbis_ParseComment( &p_ogg->p_meta, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip,
1830 &p_ogg->i_attachments, &p_ogg->attachments,
1831 &p_ogg->i_seekpoints, &p_ogg->pp_seekpoints );
1833 if( p_ogg->i_seekpoints > 1 )
1835 p_demux->info.i_update |= INPUT_UPDATE_TITLE_LIST;
1838 for( unsigned i = 0; i < i_count; i++ )
1841 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1843 demux_sys_t *p_ogg = p_demux->p_sys;
1847 /* 3 headers with the 2° one being the comments */
1848 case VLC_CODEC_VORBIS:
1849 case VLC_CODEC_THEORA:
1850 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1852 case VLC_CODEC_OPUS:
1853 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 8 );
1855 case VLC_CODEC_SPEEX:
1856 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
1859 /* N headers with the 2° one being the comments */
1860 case VLC_CODEC_KATE:
1861 /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
1862 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1 );
1866 case VLC_CODEC_FLAC:
1867 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1871 case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1872 case VLC_CODEC_DIRAC:
1877 p_demux->info.i_update |= INPUT_UPDATE_META;
1880 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream,
1883 int64_t last_packet = oggseek_get_last_frame( p_demux, p_stream );
1884 return ( last_packet >= 0 ) ? last_packet / f_rate : -1;
1887 static void Ogg_ReadTheoraHeader( demux_t *p_demux, logical_stream_t *p_stream,
1888 ogg_packet *p_oggpacket )
1891 int i_fps_numerator;
1892 int i_fps_denominator;
1893 int i_keyframe_frequency_force;
1899 p_stream->fmt.i_cat = VIDEO_ES;
1900 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1902 /* Signal that we want to keep a backup of the theora
1903 * stream headers. They will be used when switching between
1905 p_stream->b_force_backup = true;
1907 /* Cheat and get additionnal info ;) */
1908 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1909 bs_skip( &bitstream, 56 );
1911 i_major = bs_read( &bitstream, 8 ); /* major version num */
1912 i_minor = bs_read( &bitstream, 8 ); /* minor version num */
1913 i_subminor = bs_read( &bitstream, 8 ); /* subminor version num */
1915 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1916 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1917 bs_read( &bitstream, 24 ); /* frame width */
1918 bs_read( &bitstream, 24 ); /* frame height */
1919 bs_read( &bitstream, 8 ); /* x offset */
1920 bs_read( &bitstream, 8 ); /* y offset */
1922 i_fps_numerator = bs_read( &bitstream, 32 );
1923 i_fps_denominator = bs_read( &bitstream, 32 );
1924 bs_read( &bitstream, 24 ); /* aspect_numerator */
1925 bs_read( &bitstream, 24 ); /* aspect_denominator */
1927 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1928 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1930 bs_read( &bitstream, 8 ); /* colorspace */
1931 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1932 bs_read( &bitstream, 6 ); /* quality */
1934 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1936 /* granule_shift = i_log( frequency_force -1 ) */
1937 p_stream->i_granule_shift = 0;
1938 i_keyframe_frequency_force--;
1939 while( i_keyframe_frequency_force )
1941 p_stream->i_granule_shift++;
1942 i_keyframe_frequency_force >>= 1;
1945 i_version = i_major * 1000000 + i_minor * 1000 + i_subminor;
1946 p_stream->i_keyframe_offset = 0;
1947 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1949 if ( i_version >= 3002001 )
1951 p_stream->i_keyframe_offset = 1;
1953 if ( p_demux->p_sys->i_length < 0 )
1955 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_stream->f_rate );
1956 if ( last_packet >= 0 )
1957 p_demux->p_sys->i_length = last_packet;
1962 static void Ogg_ReadVorbisHeader( demux_t *p_demux, logical_stream_t *p_stream,
1963 ogg_packet *p_oggpacket )
1967 p_stream->fmt.i_cat = AUDIO_ES;
1968 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1970 /* Signal that we want to keep a backup of the vorbis
1971 * stream headers. They will be used when switching between
1973 p_stream->b_force_backup = true;
1975 /* Cheat and get additionnal info ;) */
1976 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1977 oggpack_adv( &opb, 88 );
1978 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1979 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1980 oggpack_read( &opb, 32 );
1981 oggpack_adv( &opb, 32 );
1982 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1984 if ( p_demux->p_sys->i_length < 0 )
1986 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_stream->f_rate );
1987 if ( last_packet >= 0 )
1988 p_demux->p_sys->i_length = last_packet;
1992 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1993 ogg_packet *p_oggpacket )
1997 p_stream->fmt.i_cat = AUDIO_ES;
1998 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2000 /* Signal that we want to keep a backup of the speex
2001 * stream headers. They will be used when switching between
2003 p_stream->b_force_backup = true;
2005 /* Cheat and get additionnal info ;) */
2006 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2007 oggpack_adv( &opb, 224 );
2008 oggpack_adv( &opb, 32 ); /* speex_version_id */
2009 oggpack_adv( &opb, 32 ); /* header_size */
2010 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
2011 oggpack_adv( &opb, 32 ); /* mode */
2012 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
2013 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
2014 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
2017 static void Ogg_ReadOpusHeader( demux_t *p_demux,
2018 logical_stream_t *p_stream,
2019 ogg_packet *p_oggpacket )
2023 p_stream->fmt.i_cat = AUDIO_ES;
2024 p_stream->fmt.i_codec = VLC_CODEC_OPUS;
2026 /* Signal that we want to keep a backup of the opus
2027 * stream headers. They will be used when switching between
2029 p_stream->b_force_backup = true;
2031 /* All OggOpus streams are timestamped at 48kHz and
2032 * can be played at 48kHz. */
2033 p_stream->f_rate = p_stream->fmt.audio.i_rate = 48000;
2034 p_stream->fmt.i_bitrate = 0;
2036 /* Cheat and get additional info ;) */
2037 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2038 oggpack_adv( &opb, 64 );
2039 oggpack_adv( &opb, 8 ); /* version_id */
2040 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
2041 p_stream->i_pre_skip = oggpack_read( &opb, 16 );
2043 if ( p_demux->p_sys->i_length < 0 )
2045 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_stream->f_rate );
2046 if ( last_packet >= 0 )
2047 p_demux->p_sys->i_length = last_packet;
2051 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
2052 ogg_packet *p_oggpacket )
2054 /* Parse the STREAMINFO metadata */
2057 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
2060 if( p_oggpacket->bytes > 0 && bs_read( &s, 7 ) == 0 )
2062 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
2065 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
2066 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
2068 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
2069 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
2073 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
2076 /* Fake this as the last metadata block */
2077 *((uint8_t*)p_oggpacket->packet) |= 0x80;
2081 /* This ain't a STREAMINFO metadata */
2082 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
2086 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
2087 ogg_packet *p_oggpacket )
2095 p_stream->fmt.i_cat = SPU_ES;
2096 p_stream->fmt.i_codec = VLC_CODEC_KATE;
2098 /* Signal that we want to keep a backup of the kate
2099 * stream headers. They will be used when switching between
2101 p_stream->b_force_backup = true;
2103 /* Cheat and get additionnal info ;) */
2104 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2105 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
2106 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
2107 oggpack_adv( &opb, 3*8 );
2108 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
2109 oggpack_adv( &opb, 8*8 ); /* reserved */
2110 gnum = oggpack_read( &opb, 32 );
2111 gden = oggpack_read( &opb, 32 );
2112 p_stream->f_rate = (double)gnum/gden;
2114 p_stream->fmt.psz_language = malloc(16);
2115 if( p_stream->fmt.psz_language )
2117 for( n = 0; n < 16; n++ )
2118 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
2119 p_stream->fmt.psz_language[15] = 0; /* just in case */
2123 for( n = 0; n < 16; n++ )
2124 oggpack_read(&opb,8);
2126 p_stream->fmt.psz_description = malloc(16);
2127 if( p_stream->fmt.psz_description )
2129 for( n = 0; n < 16; n++ )
2130 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
2131 p_stream->fmt.psz_description[15] = 0; /* just in case */
2133 /* Now find a localized user readable description for this category */
2134 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
2137 free( p_stream->fmt.psz_description );
2138 p_stream->fmt.psz_description = psz_desc;
2143 for( n = 0; n < 16; n++ )
2144 oggpack_read(&opb,8);
2148 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,
2149 logical_stream_t *p_stream,
2150 ogg_packet *p_oggpacket )
2152 if( p_oggpacket->bytes >= 28 &&
2153 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
2157 uint16_t major_version;
2158 uint16_t minor_version;
2159 uint64_t timebase_numerator;
2160 uint64_t timebase_denominator;
2162 Ogg_ReadTheoraHeader( p_demux, p_stream, p_oggpacket );
2164 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2165 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
2166 major_version = oggpack_read( &opb, 2*8 ); /* major version */
2167 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
2168 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
2169 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
2171 msg_Dbg( p_demux, "Annodex info: version %"PRIu16".%"PRIu16" "
2172 "Timebase %"PRId64" / %"PRId64,
2173 major_version, minor_version,
2174 timebase_numerator, timebase_denominator );
2176 else if( p_oggpacket->bytes >= 42 &&
2177 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
2179 uint64_t granule_rate_numerator;
2180 uint64_t granule_rate_denominator;
2181 char content_type_string[1024];
2183 /* Read in Annodex header fields */
2185 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
2186 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
2187 p_stream->i_secondary_header_packets =
2188 GetDWLE( &p_oggpacket->packet[24] );
2190 /* we are guaranteed that the first header field will be
2191 * the content-type (by the Annodex standard) */
2192 content_type_string[0] = '\0';
2193 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
2195 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
2196 p_oggpacket->bytes - 1 );
2197 if( p && p[0] == '\r' && p[1] == '\n' )
2198 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
2199 content_type_string );
2202 msg_Dbg( p_demux, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
2203 granule_rate_numerator, granule_rate_denominator,
2204 p_stream->i_secondary_header_packets, content_type_string );
2206 p_stream->f_rate = (float) granule_rate_numerator /
2207 (float) granule_rate_denominator;
2209 /* What type of file do we have?
2210 * strcmp is safe to use here because we've extracted
2211 * content_type_string from the stream manually */
2212 if( !strncmp(content_type_string, "audio/x-wav", 11) )
2214 /* n.b. WAVs are unsupported right now */
2215 p_stream->fmt.i_cat = UNKNOWN_ES;
2217 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
2219 p_stream->fmt.i_cat = AUDIO_ES;
2220 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2222 p_stream->b_force_backup = true;
2224 else if( !strncmp(content_type_string, "audio/x-speex", 13) )
2226 p_stream->fmt.i_cat = AUDIO_ES;
2227 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2229 p_stream->b_force_backup = true;
2231 else if( !strncmp(content_type_string, "video/x-theora", 14) )
2233 p_stream->fmt.i_cat = VIDEO_ES;
2234 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2236 p_stream->b_force_backup = true;
2238 else if( !strncmp(content_type_string, "video/x-xvid", 12) )
2240 p_stream->fmt.i_cat = VIDEO_ES;
2241 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
2243 p_stream->b_force_backup = true;
2245 else if( !strncmp(content_type_string, "video/mpeg", 10) )
2247 /* n.b. MPEG streams are unsupported right now */
2248 p_stream->fmt.i_cat = VIDEO_ES;
2249 p_stream->fmt.i_codec = VLC_CODEC_MPGV;
2251 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
2253 ogg_stream_packetout( &p_stream->os, p_oggpacket );
2254 p_stream->fmt.i_cat = SPU_ES;
2255 p_stream->fmt.i_codec = VLC_CODEC_CMML;
2260 static uint32_t dirac_uint( bs_t *p_bs )
2262 uint32_t u_count = 0, u_value = 0;
2264 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
2268 u_value |= bs_read( p_bs, 1 );
2271 return (1<<u_count) - 1 + u_value;
2274 static int dirac_bool( bs_t *p_bs )
2276 return bs_read( p_bs, 1 );
2279 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
2280 ogg_packet *p_oggpacket )
2282 static const struct {
2283 uint32_t u_n /* numerator */, u_d /* denominator */;
2284 } p_dirac_frate_tbl[] = { /* table 10.3 */
2285 {1,1}, /* this first value is never used */
2286 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
2287 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
2289 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
2291 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
2292 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
2294 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
2298 p_stream->i_granule_shift = 22; /* not 32 */
2300 /* Backing up stream headers is not required -- seqhdrs are repeated
2301 * thoughout the stream at suitable decoding start points */
2302 p_stream->b_force_backup = false;
2304 /* read in useful bits from sequence header */
2305 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
2306 bs_skip( &bs, 13*8); /* parse_info_header */
2307 dirac_uint( &bs ); /* major_version */
2308 dirac_uint( &bs ); /* minor_version */
2309 dirac_uint( &bs ); /* profile */
2310 dirac_uint( &bs ); /* level */
2312 uint32_t u_video_format = dirac_uint( &bs ); /* index */
2313 if( u_video_format >= u_dirac_vidfmt_frate )
2315 /* don't know how to parse this ogg dirac stream */
2319 if( dirac_bool( &bs ) )
2321 dirac_uint( &bs ); /* frame_width */
2322 dirac_uint( &bs ); /* frame_height */
2325 if( dirac_bool( &bs ) )
2327 dirac_uint( &bs ); /* chroma_format */
2330 if( dirac_bool( &bs ) )
2332 dirac_uint( &bs ); /* scan_format */
2335 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
2336 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
2337 if( dirac_bool( &bs ) )
2339 uint32_t u_frame_rate_index = dirac_uint( &bs );
2340 if( u_frame_rate_index >= u_dirac_frate_tbl )
2342 /* something is wrong with this stream */
2345 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
2346 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
2347 if( u_frame_rate_index == 0 )
2349 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
2350 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
2353 p_stream->f_rate = (float) u_n / u_d;
2355 /* probably is an ogg dirac es */
2356 p_stream->fmt.i_cat = VIDEO_ES;
2357 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;