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, ogg_packet *p_oggpacket, 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;
160 /* Check if we are dealing with an ogg stream */
161 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
162 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
167 /* Set exported functions */
168 p_demux->pf_demux = Demux;
169 p_demux->pf_control = Control;
170 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
174 memset( p_sys, 0, sizeof( demux_sys_t ) );
175 p_sys->i_bitrate = 0;
176 p_sys->pp_stream = NULL;
177 p_sys->p_old_stream = NULL;
179 /* Begnning of stream, tell the demux to look for elementary streams. */
183 p_sys->i_length = -1;
185 /* Initialize the Ogg physical bitstream parser */
186 ogg_sync_init( &p_sys->oy );
187 p_sys->b_page_waiting = false;
190 p_sys->p_meta = NULL;
195 /*****************************************************************************
196 * Close: frees unused data
197 *****************************************************************************/
198 static void Close( vlc_object_t *p_this )
200 demux_t *p_demux = (demux_t *)p_this;
201 demux_sys_t *p_sys = p_demux->p_sys ;
203 /* Cleanup the bitstream parser */
204 ogg_sync_clear( &p_sys->oy );
206 Ogg_EndOfStream( p_demux );
208 if( p_sys->p_old_stream )
209 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
214 /*****************************************************************************
215 * Demux: reads and demuxes data packets
216 *****************************************************************************
217 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
218 *****************************************************************************/
219 static int Demux( demux_t * p_demux )
221 demux_sys_t *p_sys = p_demux->p_sys;
222 ogg_packet oggpacket;
224 bool b_skipping = false;
227 if( p_sys->i_eos == p_sys->i_streams )
231 msg_Dbg( p_demux, "end of a group of logical streams" );
232 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
233 * only 1 ES is supported (common case for ogg web radio) */
234 if( p_sys->i_streams == 1 )
236 p_sys->p_old_stream = p_sys->pp_stream[0];
237 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
239 Ogg_EndOfStream( p_demux );
243 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
246 msg_Dbg( p_demux, "beginning of a group of logical streams" );
247 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 );
251 * The first data page of a physical stream is stored in the relevant logical stream
252 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
253 * stream it belongs to if we haven't processed this first page yet. If we do, we
254 * will only process that first page whenever we find the second page for this stream.
255 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
256 * the second page will arrive real quick, this is not fine for Kate, whose second
257 * data page will typically arrive much later.
258 * This means it is now possible to seek right at the start of a stream where the last
259 * logical stream is Kate, without having to wait for the second data page to unblock
260 * the first one, which is the one that triggers the 'no more headers to backup' code.
261 * And, as we all know, seeking without having backed up all headers is bad, since the
262 * codec will fail to initialize if it's missing its headers.
264 if( !p_sys->b_page_waiting)
267 * Demux an ogg page from the stream
269 if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
272 /* Test for End of Stream */
273 if( ogg_page_eos( &p_sys->current_page ) )
278 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
280 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
282 /* if we've just pulled page, look for the right logical stream */
283 if( !p_sys->b_page_waiting )
285 if( p_sys->i_streams == 1 &&
286 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
288 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
289 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
291 p_stream->b_reinit = true;
292 p_stream->i_pcr = VLC_TS_0;
293 p_stream->i_interpolated_pcr = VLC_TS_0;
294 p_stream->i_previous_granulepos = -1;
295 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0);
298 if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
305 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
307 /* Read info from any secondary header packets, if there are any */
308 if( p_stream->i_secondary_header_packets > 0 )
310 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
311 oggpacket.bytes >= 7 &&
312 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
314 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
315 p_stream->i_secondary_header_packets = 0;
317 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
318 oggpacket.bytes >= 7 &&
319 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
321 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
322 p_stream->i_secondary_header_packets = 0;
324 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
326 p_stream->i_secondary_header_packets = 0;
329 /* update start of data pointer */
330 p_stream->i_data_start = stream_Tell( p_demux->s );
334 /* If any streams have i_skip_frames, only decode (pre-roll)
335 * for those streams */
336 if ( b_skipping && p_stream->i_skip_frames == 0 ) continue;
339 if( p_stream->b_reinit )
341 /* If synchro is re-initialized we need to drop all the packets
342 * until we find a new dated one. */
343 Ogg_UpdatePCR( p_stream, &oggpacket );
345 if( p_stream->i_pcr >= 0 )
347 p_stream->b_reinit = false;
348 /* For Opus, trash the first 80 ms of decoded output as
349 well, to avoid blowing out speakers if we get unlucky.
350 Opus predicts content from prior frames, which can go
351 badly if we seek right where the stream goes from very
352 quiet to very loud. It will converge after a bit. */
353 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
355 ogg_int64_t start_time;
357 p_stream->i_skip_frames = 80*48;
358 /* Make sure we never play audio from within the
359 pre-skip at the beginning of the stream. */
361 Ogg_OpusPacketDuration( p_stream, &oggpacket );
362 start_time = p_stream->i_previous_granulepos;
365 start_time = start_time > duration ?
366 start_time - duration : 0;
368 if( p_stream->i_pre_skip > start_time )
370 p_stream->i_skip_frames +=
371 p_stream->i_pre_skip - start_time;
377 p_stream->i_interpolated_pcr = -1;
378 p_stream->i_previous_granulepos = -1;
382 /* An Ogg/vorbis packet contains an end date granulepos */
383 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
384 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
385 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
386 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
388 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
390 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
394 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
395 VLC_TS_0 + p_stream->i_pcr );
401 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
404 if( !p_sys->b_page_waiting )
408 /* if a page was waiting, it's now processed */
409 p_sys->b_page_waiting = false;
412 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
414 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
416 if( p_stream->fmt.i_cat == SPU_ES )
418 if( p_stream->i_interpolated_pcr < 0 )
421 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
422 p_sys->i_pcr = p_stream->i_interpolated_pcr;
425 if( p_sys->i_pcr >= 0 && ! b_skipping )
426 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
431 /*****************************************************************************
433 *****************************************************************************/
434 static int Control( demux_t *p_demux, int i_query, va_list args )
436 demux_sys_t *p_sys = p_demux->p_sys;
445 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
447 vlc_meta_Merge( p_meta, p_sys->p_meta );
450 case DEMUX_HAS_UNSUPPORTED_META:
451 pb_bool = (bool*)va_arg( args, bool* );
456 pi64 = (int64_t*)va_arg( args, int64_t * );
457 *pi64 = p_sys->i_pcr;
463 case DEMUX_GET_ATTACHMENTS:
465 input_attachment_t ***ppp_attach =
466 (input_attachment_t***)va_arg( args, input_attachment_t*** );
467 int *pi_int = (int*)va_arg( args, int * );
469 if( p_sys->i_attachments <= 0 )
472 *pi_int = p_sys->i_attachments;
473 *ppp_attach = xmalloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
474 for( int i = 0; i < p_sys->i_attachments; i++ )
475 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
479 case DEMUX_SET_POSITION:
480 /* forbid seeking if we haven't initialized all logical bitstreams yet;
481 if we allowed, some headers would not get backed up and decoder init
482 would fail, making that logical stream unusable */
483 if( p_sys->i_bos > 0 )
488 for( i = 0; i < p_sys->i_streams; i++ )
490 logical_stream_t *p_stream = p_sys->pp_stream[i];
492 /* we'll trash all the data until we find the next pcr */
493 p_stream->b_reinit = true;
494 p_stream->i_pcr = -1;
495 p_stream->i_interpolated_pcr = -1;
496 p_stream->i_previous_granulepos = -1;
497 ogg_stream_reset( &p_stream->os );
499 ogg_sync_reset( &p_sys->oy );
500 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
502 case DEMUX_GET_LENGTH:
503 if ( p_sys->i_length < 0 )
504 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
506 pi64 = (int64_t*)va_arg( args, int64_t * );
507 *pi64 = p_sys->i_length * 1000000;
512 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
517 /****************************************************************************
518 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
519 ****************************************************************************
520 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
521 * are at the end of stream.
522 ****************************************************************************/
523 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
525 demux_sys_t *p_ogg = p_demux->p_sys ;
529 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
531 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
533 i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
537 ogg_sync_wrote( &p_ogg->oy, i_read );
543 /****************************************************************************
544 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
546 ****************************************************************************/
547 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
548 ogg_packet *p_oggpacket )
550 p_stream->i_end_trim = 0;
551 /* Convert the granulepos into a pcr */
552 if( p_oggpacket->granulepos >= 0 )
554 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
555 p_stream->fmt.i_codec == VLC_CODEC_KATE )
557 ogg_int64_t iframe = p_oggpacket->granulepos >>
558 p_stream->i_granule_shift;
559 ogg_int64_t pframe = p_oggpacket->granulepos -
560 ( iframe << p_stream->i_granule_shift );
562 p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
563 * INT64_C(1000000) / p_stream->f_rate;
565 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
567 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
568 /* NB, OggDirac granulepos values are in units of 2*picturerate */
569 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
574 sample = p_oggpacket->granulepos;
575 if( p_oggpacket->e_o_s &&
576 p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
577 p_stream->i_previous_granulepos >= 0 )
580 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
583 ogg_int64_t end_sample;
584 end_sample = p_stream->i_previous_granulepos + duration;
585 if( end_sample > sample )
586 p_stream->i_end_trim = (int)(end_sample - sample);
589 if (sample >= p_stream->i_pre_skip)
590 sample -= p_stream->i_pre_skip;
593 p_stream->i_pcr = sample * INT64_C(1000000) / p_stream->f_rate;
596 p_stream->i_pcr += VLC_TS_0;
597 p_stream->i_interpolated_pcr = p_stream->i_pcr;
602 p_stream->i_pcr = -1;
604 /* no granulepos available, try to interpolate the pcr.
605 * If we can't then don't touch the old value. */
606 if( p_stream->fmt.i_cat == VIDEO_ES )
607 /* 1 frame per packet */
608 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
609 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
610 p_stream->i_previous_granulepos >= 0 &&
612 Ogg_OpusPacketDuration( p_stream, p_oggpacket ) ) > 0 )
615 p_oggpacket->granulepos =
616 p_stream->i_previous_granulepos + duration;
617 sample = p_oggpacket->granulepos;
618 if (sample >= p_stream->i_pre_skip)
619 sample -= p_stream->i_pre_skip;
622 p_stream->i_interpolated_pcr =
623 VLC_TS_0 + sample * INT64_C(1000000) / p_stream->f_rate;
625 else if( p_stream->fmt.i_bitrate )
627 p_stream->i_interpolated_pcr +=
628 ( p_oggpacket->bytes * INT64_C(1000000) /
629 p_stream->fmt.i_bitrate / 8 );
632 p_stream->i_previous_granulepos = p_oggpacket->granulepos;
635 /****************************************************************************
636 * Ogg_DecodePacket: Decode an Ogg packet.
637 ****************************************************************************/
638 static void Ogg_DecodePacket( demux_t *p_demux,
639 logical_stream_t *p_stream,
640 ogg_packet *p_oggpacket )
644 int i_header_len = 0;
645 mtime_t i_pts = -1, i_interpolated_pts;
646 demux_sys_t *p_ogg = p_demux->p_sys;
648 if( p_oggpacket->bytes >= 7 &&
649 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
651 /* it's an Annodex packet -- skip it (do nothing) */
654 else if( p_oggpacket->bytes >= 7 &&
655 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
657 /* it's an AnxData packet -- skip it (do nothing) */
661 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT && p_oggpacket->bytes > 0 &&
662 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
664 /* Check the ES is selected */
665 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
666 p_stream->p_es, &b_selected );
668 if( p_stream->b_force_backup )
671 p_stream->i_packets_backup++;
672 switch( p_stream->fmt.i_codec )
674 case VLC_CODEC_VORBIS:
675 case VLC_CODEC_SPEEX:
676 case VLC_CODEC_THEORA:
677 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
682 if( p_stream->i_packets_backup == 2 ) p_stream->b_force_backup = 0;
687 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
689 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
690 p_stream->b_force_backup = 0;
692 else if( p_stream->fmt.audio.i_rate )
694 p_stream->b_force_backup = 0;
695 if( p_oggpacket->bytes >= 9 )
697 p_oggpacket->packet += 9;
698 p_oggpacket->bytes -= 9;
705 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
710 p_stream->b_force_backup = 0;
715 /* Backup the ogg packet (likely an header packet) */
718 void *p_org = p_stream->p_headers;
719 p_stream->i_headers += p_oggpacket->bytes;
720 p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
721 if( p_stream->p_headers )
723 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
724 p_oggpacket->packet, p_oggpacket->bytes );
729 p_stream->i_headers = 0;
730 p_stream->p_headers = NULL;
734 else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
735 p_oggpacket->bytes, p_oggpacket->packet ) )
737 p_stream->i_headers = 0;
738 p_stream->p_headers = NULL;
740 if( p_stream->i_headers > 0 )
742 if( !p_stream->b_force_backup )
744 /* Last header received, commit changes */
745 free( p_stream->fmt.p_extra );
747 p_stream->fmt.i_extra = p_stream->i_headers;
748 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
749 if( p_stream->fmt.p_extra )
750 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
751 p_stream->i_headers );
753 p_stream->fmt.i_extra = 0;
755 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
756 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
757 p_stream->p_es, &p_stream->fmt );
759 if( p_stream->i_headers > 0 )
760 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
761 p_stream->p_headers, p_stream->i_headers );
763 /* we're not at BOS anymore for this logical stream */
768 b_selected = false; /* Discard the header packet */
771 /* Convert the pcr into a pts */
772 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
773 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
774 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
775 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
777 if( p_stream->i_pcr >= 0 )
779 /* This is for streams where the granulepos of the header packets
780 * doesn't match these of the data packets (eg. ogg web radios). */
781 if( p_stream->i_previous_pcr == 0 &&
782 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
785 /* Call the pace control */
786 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
787 VLC_TS_0 + p_stream->i_pcr );
790 p_stream->i_previous_pcr = p_stream->i_pcr;
792 /* The granulepos is the end date of the sample */
793 i_pts = p_stream->i_pcr;
797 /* Convert the granulepos into the next pcr */
798 i_interpolated_pts = p_stream->i_interpolated_pcr;
799 Ogg_UpdatePCR( p_stream, p_oggpacket );
801 /* SPU streams are typically discontinuous, do not mind large gaps */
802 if( p_stream->fmt.i_cat != SPU_ES )
804 if( p_stream->i_pcr >= 0 )
806 /* This is for streams where the granulepos of the header packets
807 * doesn't match these of the data packets (eg. ogg web radios). */
808 if( p_stream->i_previous_pcr == 0 &&
809 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
812 /* Call the pace control */
813 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_stream->i_pcr );
818 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
819 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
820 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
821 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
822 p_stream->i_pcr >= 0 )
824 p_stream->i_previous_pcr = p_stream->i_pcr;
826 /* The granulepos is the start date of the sample */
827 i_pts = p_stream->i_pcr;
832 /* This stream isn't currently selected so we don't need to decode it,
833 * but we did need to store its pcr as it might be selected later on */
837 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
840 /* may need to preroll after a seek */
841 if ( p_stream->i_skip_frames > 0 )
843 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
846 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
847 if( p_stream->i_skip_frames > duration )
849 p_block->i_flags |= BLOCK_FLAG_PREROLL;
850 p_block->i_nb_samples = 0;
851 p_stream->i_skip_frames -= duration;
855 p_block->i_nb_samples = duration - p_stream->i_skip_frames;
856 if( p_stream->i_previous_granulepos >=
857 p_block->i_nb_samples + p_stream->i_pre_skip )
859 i_pts = VLC_TS_0 + (p_stream->i_previous_granulepos
860 - p_block->i_nb_samples - p_stream->i_pre_skip) *
861 INT64_C(1000000) / p_stream->f_rate;
863 p_stream->i_skip_frames = 0;
868 p_block->i_flags |= BLOCK_FLAG_PREROLL;
869 p_stream->i_skip_frames--;
872 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
873 p_block->i_nb_samples = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
877 if( i_pts == VLC_TS_INVALID ) i_pts = VLC_TS_0;
878 else if( i_pts == -1 && i_interpolated_pts == VLC_TS_INVALID )
880 else if( i_pts == -1 && p_stream->fmt.i_cat == VIDEO_ES )
881 i_pts = i_interpolated_pts;
882 else if( i_pts == -1 ) i_pts = VLC_TS_INVALID;
884 if( p_stream->fmt.i_cat == AUDIO_ES )
886 p_block->i_dts = p_block->i_pts = i_pts;
887 /* Blatant abuse of the i_length field. */
888 p_block->i_length = p_stream->i_end_trim;
890 else if( p_stream->fmt.i_cat == SPU_ES )
892 p_block->i_dts = p_block->i_pts = i_pts;
893 p_block->i_length = 0;
895 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
897 p_block->i_dts = p_block->i_pts = i_pts;
898 if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
900 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
903 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
905 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
906 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
908 uint64_t u_pnum = dts + delay;
910 p_block->i_dts = p_stream->i_pcr;
911 p_block->i_pts = VLC_TS_INVALID;
912 /* NB, OggDirac granulepos values are in units of 2*picturerate */
914 /* granulepos for dirac is possibly broken, this value should be ignored */
915 if( -1 != p_oggpacket->granulepos )
916 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
920 p_block->i_dts = i_pts;
921 p_block->i_pts = VLC_TS_INVALID;
924 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
925 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
926 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
927 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
928 p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
929 p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
930 p_stream->fmt.i_codec != VLC_CODEC_CMML &&
931 p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
932 p_stream->fmt.i_codec != VLC_CODEC_KATE )
934 if( p_oggpacket->bytes <= 0 )
936 msg_Dbg( p_demux, "discarding 0 sized packet" );
937 block_Release( p_block );
940 /* We remove the header from the packet */
941 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
942 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
944 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
946 /* But with subtitles we need to retrieve the duration first */
949 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
951 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
953 lenbytes = lenbytes << 8;
954 lenbytes += *(p_oggpacket->packet + i_header_len - i);
957 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
958 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
959 p_oggpacket->packet[i_header_len + 1] != 0 &&
960 p_oggpacket->packet[i_header_len + 1] != '\n' &&
961 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
963 p_block->i_length = (mtime_t)lenbytes * 1000;
968 if( p_block->i_buffer >= (unsigned int)i_header_len )
969 p_block->i_buffer -= i_header_len;
971 p_block->i_buffer = 0;
974 if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
976 /* FIXME: the biggest hack I've ever done */
977 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
978 p_block->i_pts, p_block->i_dts );
982 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
983 p_oggpacket->bytes - i_header_len );
985 es_out_Send( p_demux->out, p_stream->p_es, p_block );
988 /* Re-implemented to avoid linking against libopus from the demuxer. */
989 static int Ogg_OpusPacketDuration( logical_stream_t *p_stream,
990 ogg_packet *p_oggpacket )
992 static const int silk_fs_div[4] = { 6000, 3000, 1500, 1000 };
998 if( p_oggpacket->bytes < 1 )
1000 toc = p_oggpacket->packet[0];
1011 if( p_oggpacket->bytes < 2 )
1012 return VLC_EGENERIC;
1013 nframes = p_oggpacket->packet[1]&0x3F;
1016 i_rate = (int)p_stream->fmt.audio.i_rate;
1018 frame_size = (i_rate << (toc >> 3 & 3)) / 400;
1019 else if( ( toc&0x60 ) == 0x60 )
1020 frame_size = i_rate/(100 >> (toc >> 3 & 1));
1022 frame_size = i_rate*60 / silk_fs_div[toc >> 3 & 3];
1023 nsamples = nframes*frame_size;
1024 if( nsamples*25 > i_rate*3 )
1025 return VLC_EGENERIC;
1029 /****************************************************************************
1030 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
1031 * stream and fill p_ogg.
1032 *****************************************************************************
1033 * The initial page of a logical stream is marked as a 'bos' page.
1034 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
1035 * together and all of the initial pages must appear before any data pages.
1037 * On success this function returns VLC_SUCCESS.
1038 ****************************************************************************/
1039 static int Ogg_FindLogicalStreams( demux_t *p_demux )
1041 demux_sys_t *p_ogg = p_demux->p_sys ;
1042 ogg_packet oggpacket;
1045 p_ogg->i_total_length = stream_Size ( p_demux->s );
1046 msg_Dbg( p_demux, "File length is %"PRId64" bytes", p_ogg->i_total_length );
1049 while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
1052 if( ogg_page_bos( &p_ogg->current_page ) )
1055 /* All is wonderful in our fine fine little world.
1056 * We found the beginning of our first logical stream. */
1057 while( ogg_page_bos( &p_ogg->current_page ) )
1059 logical_stream_t *p_stream;
1061 p_stream = malloc( sizeof(logical_stream_t) );
1062 if( unlikely( !p_stream ) )
1065 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
1067 memset( p_stream, 0, sizeof(logical_stream_t) );
1068 p_stream->p_headers = 0;
1069 p_stream->i_secondary_header_packets = 0;
1071 p_stream->i_keyframe_offset = 0;
1072 p_stream->i_skip_frames = 0;
1074 p_stream->i_data_start = 0;
1076 es_format_Init( &p_stream->fmt, 0, 0 );
1077 es_format_Init( &p_stream->fmt_old, 0, 0 );
1079 /* Setup the logical stream */
1080 p_stream->i_serial_no = ogg_page_serialno( &p_ogg->current_page );
1081 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
1083 /* Extract the initial header from the first page and verify
1084 * the codec type of this Ogg bitstream */
1085 if( ogg_stream_pagein( &p_stream->os, &p_ogg->current_page ) < 0 )
1087 /* error. stream version mismatch perhaps */
1088 msg_Err( p_demux, "error reading first page of "
1089 "Ogg bitstream data" );
1090 return VLC_EGENERIC;
1093 /* FIXME: check return value */
1094 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
1096 /* Check for Vorbis header */
1097 if( oggpacket.bytes >= 7 &&
1098 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
1100 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
1101 msg_Dbg( p_demux, "found vorbis header" );
1103 /* Check for Speex header */
1104 else if( oggpacket.bytes >= 5 &&
1105 ! memcmp( oggpacket.packet, "Speex", 5 ) )
1107 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
1108 msg_Dbg( p_demux, "found speex header, channels: %i, "
1109 "rate: %i, bitrate: %i",
1110 p_stream->fmt.audio.i_channels,
1111 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
1113 /* Check for Opus header */
1114 else if( oggpacket.bytes >= 8 &&
1115 ! memcmp( oggpacket.packet, "OpusHead", 8 ) )
1117 Ogg_ReadOpusHeader( p_demux, p_stream, &oggpacket );
1118 msg_Dbg( p_demux, "found opus header, channels: %i, "
1120 p_stream->fmt.audio.i_channels,
1121 (int)p_stream->i_pre_skip);
1122 p_stream->i_skip_frames = p_stream->i_pre_skip;
1124 /* Check for Flac header (< version 1.1.1) */
1125 else if( oggpacket.bytes >= 4 &&
1126 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
1128 msg_Dbg( p_demux, "found FLAC header" );
1130 /* Grrrr!!!! Did they really have to put all the
1131 * important info in the second header packet!!!
1132 * (STREAMINFO metadata is in the following packet) */
1133 p_stream->b_force_backup = 1;
1135 p_stream->fmt.i_cat = AUDIO_ES;
1136 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1138 /* Check for Flac header (>= version 1.1.1) */
1139 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
1140 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
1141 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
1143 int i_packets = ((int)oggpacket.packet[7]) << 8 |
1144 oggpacket.packet[8];
1145 msg_Dbg( p_demux, "found FLAC header version %i.%i "
1146 "(%i header packets)",
1147 oggpacket.packet[5], oggpacket.packet[6],
1150 p_stream->b_force_backup = 1;
1152 p_stream->fmt.i_cat = AUDIO_ES;
1153 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1154 oggpacket.packet += 13; oggpacket.bytes -= 13;
1155 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
1157 /* Check for Theora header */
1158 else if( oggpacket.bytes >= 7 &&
1159 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
1161 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
1164 "found theora header, bitrate: %i, rate: %f",
1165 p_stream->fmt.i_bitrate, p_stream->f_rate );
1167 /* Check for Dirac header */
1168 else if( ( oggpacket.bytes >= 5 &&
1169 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
1170 ( oggpacket.bytes >= 9 &&
1171 ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
1173 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1174 msg_Dbg( p_demux, "found dirac header" );
1177 msg_Warn( p_demux, "found dirac header isn't decodable" );
1182 /* Check for Tarkin header */
1183 else if( oggpacket.bytes >= 7 &&
1184 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1188 msg_Dbg( p_demux, "found tarkin header" );
1189 p_stream->fmt.i_cat = VIDEO_ES;
1190 p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1192 /* Cheat and get additionnal info ;) */
1193 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1194 oggpack_adv( &opb, 88 );
1195 oggpack_adv( &opb, 104 );
1196 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1197 p_stream->f_rate = 2; /* FIXME */
1199 "found tarkin header, bitrate: %i, rate: %f",
1200 p_stream->fmt.i_bitrate, p_stream->f_rate );
1202 /* Check for Annodex header */
1203 else if( oggpacket.bytes >= 7 &&
1204 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1206 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1207 /* kill annodex track */
1211 /* Check for Annodex header */
1212 else if( oggpacket.bytes >= 7 &&
1213 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1215 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1217 /* Check for Kate header */
1218 else if( oggpacket.bytes >= 8 &&
1219 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1221 Ogg_ReadKateHeader( p_stream, &oggpacket );
1222 msg_Dbg( p_demux, "found kate header" );
1224 else if( oggpacket.bytes >= 142 &&
1225 !memcmp( &oggpacket.packet[1],
1226 "Direct Show Samples embedded in Ogg", 35 ))
1228 /* Old header type */
1230 /* Check for video header (old format) */
1231 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1232 oggpacket.bytes >= 184 )
1234 p_stream->fmt.i_cat = VIDEO_ES;
1235 p_stream->fmt.i_codec =
1236 VLC_FOURCC( oggpacket.packet[68],
1237 oggpacket.packet[69],
1238 oggpacket.packet[70],
1239 oggpacket.packet[71] );
1240 msg_Dbg( p_demux, "found video header of type: %.4s",
1241 (char *)&p_stream->fmt.i_codec );
1243 p_stream->fmt.video.i_frame_rate = 10000000;
1244 p_stream->fmt.video.i_frame_rate_base =
1245 GetQWLE((oggpacket.packet+164));
1246 p_stream->f_rate = 10000000.0 /
1247 GetQWLE((oggpacket.packet+164));
1248 p_stream->fmt.video.i_bits_per_pixel =
1249 GetWLE((oggpacket.packet+182));
1250 if( !p_stream->fmt.video.i_bits_per_pixel )
1252 p_stream->fmt.video.i_bits_per_pixel = 24;
1253 p_stream->fmt.video.i_width =
1254 GetDWLE((oggpacket.packet+176));
1255 p_stream->fmt.video.i_height =
1256 GetDWLE((oggpacket.packet+180));
1259 "fps: %f, width:%i; height:%i, bitcount:%i",
1261 p_stream->fmt.video.i_width,
1262 p_stream->fmt.video.i_height,
1263 p_stream->fmt.video.i_bits_per_pixel);
1266 /* Check for audio header (old format) */
1267 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1270 unsigned int i_format_tag;
1272 p_stream->fmt.i_cat = AUDIO_ES;
1274 i_extra_size = GetWLE((oggpacket.packet+140));
1275 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1277 p_stream->fmt.i_extra = i_extra_size;
1278 p_stream->fmt.p_extra = malloc( i_extra_size );
1279 if( p_stream->fmt.p_extra )
1280 memcpy( p_stream->fmt.p_extra,
1281 oggpacket.packet + 142, i_extra_size );
1283 p_stream->fmt.i_extra = 0;
1286 i_format_tag = GetWLE((oggpacket.packet+124));
1287 p_stream->fmt.audio.i_channels =
1288 GetWLE((oggpacket.packet+126));
1289 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1290 GetDWLE((oggpacket.packet+128));
1291 p_stream->fmt.i_bitrate =
1292 GetDWLE((oggpacket.packet+132)) * 8;
1293 p_stream->fmt.audio.i_blockalign =
1294 GetWLE((oggpacket.packet+136));
1295 p_stream->fmt.audio.i_bitspersample =
1296 GetWLE((oggpacket.packet+138));
1298 wf_tag_to_fourcc( i_format_tag,
1299 &p_stream->fmt.i_codec, 0 );
1301 if( p_stream->fmt.i_codec ==
1302 VLC_FOURCC('u','n','d','f') )
1304 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1305 ( i_format_tag >> 8 ) & 0xff,
1306 i_format_tag & 0xff );
1309 msg_Dbg( p_demux, "found audio header of type: %.4s",
1310 (char *)&p_stream->fmt.i_codec );
1311 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1312 "%dbits/sample %dkb/s",
1314 p_stream->fmt.audio.i_channels,
1315 p_stream->fmt.audio.i_rate,
1316 p_stream->fmt.audio.i_bitspersample,
1317 p_stream->fmt.i_bitrate / 1024 );
1322 msg_Dbg( p_demux, "stream %d has an old header "
1323 "but is of an unknown type", p_ogg->i_streams-1 );
1328 else if( oggpacket.bytes >= 44+1 &&
1329 (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER )
1331 stream_header_t tmp;
1332 stream_header_t *st = &tmp;
1334 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1335 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1336 st->size = GetDWLE( &oggpacket.packet[1+12] );
1337 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1338 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1339 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1340 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1341 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1343 /* Check for video header (new format) */
1344 if( !strncmp( st->streamtype, "video", 5 ) &&
1345 oggpacket.bytes >= 52+1 )
1347 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1348 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1350 p_stream->fmt.i_cat = VIDEO_ES;
1352 /* We need to get rid of the header packet */
1353 ogg_stream_packetout( &p_stream->os, &oggpacket );
1355 p_stream->fmt.i_codec =
1356 VLC_FOURCC( st->subtype[0], st->subtype[1],
1357 st->subtype[2], st->subtype[3] );
1358 msg_Dbg( p_demux, "found video header of type: %.4s",
1359 (char *)&p_stream->fmt.i_codec );
1361 p_stream->fmt.video.i_frame_rate = 10000000;
1362 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1363 if( st->time_unit <= 0 )
1364 st->time_unit = 400000;
1365 p_stream->f_rate = 10000000.0 / st->time_unit;
1366 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1367 p_stream->fmt.video.i_width = st->sh.video.width;
1368 p_stream->fmt.video.i_height = st->sh.video.height;
1371 "fps: %f, width:%i; height:%i, bitcount:%i",
1373 p_stream->fmt.video.i_width,
1374 p_stream->fmt.video.i_height,
1375 p_stream->fmt.video.i_bits_per_pixel );
1377 /* Check for audio header (new format) */
1378 else if( !strncmp( st->streamtype, "audio", 5 ) &&
1379 oggpacket.bytes >= 56+1 )
1385 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1386 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1387 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1389 p_stream->fmt.i_cat = AUDIO_ES;
1391 /* We need to get rid of the header packet */
1392 ogg_stream_packetout( &p_stream->os, &oggpacket );
1394 i_extra_size = st->size - 56;
1396 if( i_extra_size > 0 &&
1397 i_extra_size < oggpacket.bytes - 1 - 56 )
1399 p_stream->fmt.i_extra = i_extra_size;
1400 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1401 if( p_stream->fmt.p_extra )
1402 memcpy( p_stream->fmt.p_extra, st + 1,
1403 p_stream->fmt.i_extra );
1405 p_stream->fmt.i_extra = 0;
1408 memcpy( p_buffer, st->subtype, 4 );
1410 i_format_tag = strtol(p_buffer,NULL,16);
1411 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1412 if( st->time_unit <= 0 )
1413 st->time_unit = 10000000;
1414 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1415 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1416 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1417 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1419 wf_tag_to_fourcc( i_format_tag,
1420 &p_stream->fmt.i_codec, 0 );
1422 if( p_stream->fmt.i_codec ==
1423 VLC_FOURCC('u','n','d','f') )
1425 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1426 ( i_format_tag >> 8 ) & 0xff,
1427 i_format_tag & 0xff );
1430 msg_Dbg( p_demux, "found audio header of type: %.4s",
1431 (char *)&p_stream->fmt.i_codec );
1432 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1433 "%dbits/sample %dkb/s",
1435 p_stream->fmt.audio.i_channels,
1436 p_stream->fmt.audio.i_rate,
1437 p_stream->fmt.audio.i_bitspersample,
1438 p_stream->fmt.i_bitrate / 1024 );
1440 /* Check for text (subtitles) header */
1441 else if( !strncmp(st->streamtype, "text", 4) )
1443 /* We need to get rid of the header packet */
1444 ogg_stream_packetout( &p_stream->os, &oggpacket );
1446 msg_Dbg( p_demux, "found text subtitles header" );
1447 p_stream->fmt.i_cat = SPU_ES;
1448 p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1449 p_stream->f_rate = 1000; /* granulepos is in millisec */
1453 msg_Dbg( p_demux, "stream %d has a header marker "
1454 "but is of an unknown type", p_ogg->i_streams-1 );
1459 else if( oggpacket.bytes >= 7 &&
1460 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1464 msg_Dbg( p_demux, "stream %d is a skeleton",
1465 p_ogg->i_streams-1 );
1466 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1470 msg_Dbg( p_demux, "stream %d is of unknown type",
1471 p_ogg->i_streams-1 );
1476 if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
1477 return VLC_EGENERIC;
1480 /* we'll need to get all headers for all of those streams
1481 that we have to backup headers for */
1483 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1485 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1490 /* This is the first data page, which means we are now finished
1491 * with the initial pages. We just need to store it in the relevant
1493 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1495 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1496 &p_ogg->current_page ) == 0 )
1498 p_ogg->b_page_waiting = true;
1507 return VLC_EGENERIC;
1510 /****************************************************************************
1511 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1512 * Elementary streams.
1513 ****************************************************************************/
1514 static int Ogg_BeginningOfStream( demux_t *p_demux )
1516 demux_sys_t *p_ogg = p_demux->p_sys ;
1517 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1520 /* Find the logical streams embedded in the physical stream and
1521 * initialize our p_ogg structure. */
1522 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1524 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1525 return VLC_EGENERIC;
1528 p_ogg->i_bitrate = 0;
1530 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1532 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1534 p_stream->p_es = NULL;
1536 /* initialise kframe index */
1539 /* Try first to reuse an old ES */
1541 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1542 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1544 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1546 p_stream->p_es = p_old_stream->p_es;
1547 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1549 p_old_stream->p_es = NULL;
1550 p_old_stream = NULL;
1553 if( !p_stream->p_es )
1555 /* Better be safe than sorry when possible with ogm */
1556 if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1557 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1558 p_stream->fmt.b_packetized = false;
1560 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1563 // TODO: something to do here ?
1564 if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1566 /* Set the CMML stream active */
1567 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1570 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1572 p_stream->i_pcr = p_stream->i_previous_pcr =
1573 p_stream->i_interpolated_pcr = -1;
1574 p_stream->b_reinit = false;
1577 if( p_ogg->p_old_stream )
1579 if( p_ogg->p_old_stream->p_es )
1580 msg_Dbg( p_demux, "old stream not reused" );
1581 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1582 p_ogg->p_old_stream = NULL;
1586 /* get total frame count for video stream; we will need this for seeking */
1587 p_ogg->i_total_frames = 0;
1592 /****************************************************************************
1593 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1594 ****************************************************************************/
1595 static void Ogg_EndOfStream( demux_t *p_demux )
1597 demux_sys_t *p_ogg = p_demux->p_sys ;
1600 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1601 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1602 free( p_ogg->pp_stream );
1605 p_ogg->i_bitrate = 0;
1606 p_ogg->i_streams = 0;
1607 p_ogg->pp_stream = NULL;
1611 vlc_meta_Delete( p_ogg->p_meta );
1612 p_ogg->p_meta = NULL;
1616 * This function delete and release all data associated to a logical_stream_t
1618 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1620 if( p_stream->p_es )
1621 es_out_Del( p_demux->out, p_stream->p_es );
1623 ogg_stream_clear( &p_stream->os );
1624 free( p_stream->p_headers );
1626 es_format_Clean( &p_stream->fmt_old );
1627 es_format_Clean( &p_stream->fmt );
1629 if ( p_stream->idx != NULL)
1631 oggseek_index_entries_free( p_stream->idx );
1637 * This function check if a we need to reset a decoder in case we are
1640 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1642 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1643 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1644 unsigned i_new_count;
1645 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1648 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1649 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1650 unsigned i_old_count;
1651 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1654 bool b_match = i_new_count == i_old_count;
1655 for( unsigned i = 0; i < i_new_count && b_match; i++ )
1657 /* Ignore vorbis comment */
1660 if( pi_new_size[i] != pi_old_size[i] ||
1661 memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
1665 for( unsigned i = 0; i < i_new_count; i++ )
1666 free( pp_new_data[i] );
1667 for( unsigned i = 0; i < i_old_count; i++ )
1668 free( pp_old_data[i] );
1672 static bool Ogg_IsOpusFormatCompatible( const es_format_t *p_new,
1673 const es_format_t *p_old )
1675 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1676 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1677 unsigned i_new_count;
1678 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1680 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1681 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1682 unsigned i_old_count;
1683 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1685 bool b_match = false;
1686 if( i_new_count == i_old_count && i_new_count > 0 )
1688 static const unsigned char default_map[2] = { 0, 1 };
1689 unsigned char *p_old_head;
1690 unsigned char *p_new_head;
1691 const unsigned char *p_old_map;
1692 const unsigned char *p_new_map;
1693 int i_old_channel_count;
1694 int i_new_channel_count;
1695 int i_old_stream_count;
1696 int i_new_stream_count;
1697 int i_old_coupled_count;
1698 int i_new_coupled_count;
1699 p_old_head = (unsigned char *)pp_old_data[0];
1700 i_old_channel_count = i_old_stream_count = i_old_coupled_count = 0;
1701 p_old_map = default_map;
1702 if( pi_old_size[0] >= 19 && p_old_head[8] <= 15 )
1704 i_old_channel_count = p_old_head[9];
1705 switch( p_old_head[18] )
1708 i_old_stream_count = 1;
1709 i_old_coupled_count = i_old_channel_count - 1;
1712 if( pi_old_size[0] >= 21U + i_old_channel_count )
1714 i_old_stream_count = p_old_head[19];
1715 i_old_coupled_count = p_old_head[20];
1716 p_old_map = p_old_head + 21;
1721 p_new_head = (unsigned char *)pp_new_data[0];
1722 i_new_channel_count = i_new_stream_count = i_new_coupled_count = 0;
1723 p_new_map = default_map;
1724 if( pi_new_size[0] >= 19 && p_new_head[8] <= 15 )
1726 i_new_channel_count = p_new_head[9];
1727 switch( p_new_head[18] )
1730 i_new_stream_count = 1;
1731 i_new_coupled_count = i_new_channel_count - 1;
1734 if( pi_new_size[0] >= 21U + i_new_channel_count )
1736 i_new_stream_count = p_new_head[19];
1737 i_new_coupled_count = p_new_head[20];
1738 p_new_map = p_new_head+21;
1743 b_match = i_old_channel_count == i_new_channel_count &&
1744 i_old_stream_count == i_new_stream_count &&
1745 i_old_coupled_count == i_new_coupled_count &&
1746 memcmp(p_old_map, p_new_map,
1747 i_new_channel_count*sizeof(*p_new_map)) == 0;
1749 for( unsigned i = 0; i < i_new_count; i++ )
1750 free( pp_new_data[i] );
1751 for( unsigned i = 0; i < i_old_count; i++ )
1752 free( pp_old_data[i] );
1756 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1758 bool b_compatible = false;
1759 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1762 /* Only Vorbis and Opus are supported. */
1763 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
1764 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1765 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
1766 b_compatible = Ogg_IsOpusFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1769 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1771 return !b_compatible;
1773 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const void *p_headers, unsigned i_headers, unsigned i_skip )
1775 demux_sys_t *p_ogg = p_demux->p_sys;
1777 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1778 void *pp_data[XIPH_MAX_HEADER_COUNT];
1780 if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
1783 /* TODO how to handle multiple comments properly ? */
1784 if( i_count >= 2 && pi_size[1] > i_skip )
1785 vorbis_ParseComment( &p_ogg->p_meta, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip,
1786 &p_ogg->i_attachments, &p_ogg->attachments );
1788 for( unsigned i = 0; i < i_count; i++ )
1791 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1793 demux_sys_t *p_ogg = p_demux->p_sys;
1797 /* 3 headers with the 2° one being the comments */
1798 case VLC_CODEC_VORBIS:
1799 case VLC_CODEC_THEORA:
1800 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1802 case VLC_CODEC_OPUS:
1803 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 8 );
1805 case VLC_CODEC_SPEEX:
1806 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
1809 /* N headers with the 2° one being the comments */
1810 case VLC_CODEC_KATE:
1811 /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
1812 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1 );
1816 case VLC_CODEC_FLAC:
1817 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1821 case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1822 case VLC_CODEC_DIRAC:
1827 p_demux->info.i_update |= INPUT_UPDATE_META;
1830 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream,
1831 ogg_packet *p_oggpacket, double f_rate )
1833 int64_t last_packet = oggseek_get_last_frame( p_demux, p_stream );
1835 * Since there's quite a good chance that ogg_stream_packetout was called,
1836 * the given p_oggpacket may point to invalid data. Fill it with some valid ones
1838 ogg_stream_packetpeek( &p_stream->os, p_oggpacket );
1840 return ( last_packet >= 0 ) ? last_packet / f_rate : -1;
1843 static void Ogg_ReadTheoraHeader( demux_t *p_demux, logical_stream_t *p_stream,
1844 ogg_packet *p_oggpacket )
1847 int i_fps_numerator;
1848 int i_fps_denominator;
1849 int i_keyframe_frequency_force;
1855 p_stream->fmt.i_cat = VIDEO_ES;
1856 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1858 /* Signal that we want to keep a backup of the theora
1859 * stream headers. They will be used when switching between
1861 p_stream->b_force_backup = 1;
1863 /* Cheat and get additionnal info ;) */
1864 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1865 bs_skip( &bitstream, 56 );
1867 i_major = bs_read( &bitstream, 8 ); /* major version num */
1868 i_minor = bs_read( &bitstream, 8 ); /* minor version num */
1869 i_subminor = bs_read( &bitstream, 8 ); /* subminor version num */
1871 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1872 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1873 bs_read( &bitstream, 24 ); /* frame width */
1874 bs_read( &bitstream, 24 ); /* frame height */
1875 bs_read( &bitstream, 8 ); /* x offset */
1876 bs_read( &bitstream, 8 ); /* y offset */
1878 i_fps_numerator = bs_read( &bitstream, 32 );
1879 i_fps_denominator = bs_read( &bitstream, 32 );
1880 bs_read( &bitstream, 24 ); /* aspect_numerator */
1881 bs_read( &bitstream, 24 ); /* aspect_denominator */
1883 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1884 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1886 bs_read( &bitstream, 8 ); /* colorspace */
1887 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1888 bs_read( &bitstream, 6 ); /* quality */
1890 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1892 /* granule_shift = i_log( frequency_force -1 ) */
1893 p_stream->i_granule_shift = 0;
1894 i_keyframe_frequency_force--;
1895 while( i_keyframe_frequency_force )
1897 p_stream->i_granule_shift++;
1898 i_keyframe_frequency_force >>= 1;
1901 i_version = i_major * 1000000 + i_minor * 1000 + i_subminor;
1902 p_stream->i_keyframe_offset = 0;
1903 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1905 if ( i_version >= 3002001 )
1907 p_stream->i_keyframe_offset = 1;
1909 if ( p_demux->p_sys->i_length < 0 )
1911 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
1912 if ( last_packet >= 0 )
1913 p_demux->p_sys->i_length = last_packet;
1918 static void Ogg_ReadVorbisHeader( demux_t *p_demux, logical_stream_t *p_stream,
1919 ogg_packet *p_oggpacket )
1923 p_stream->fmt.i_cat = AUDIO_ES;
1924 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1926 /* Signal that we want to keep a backup of the vorbis
1927 * stream headers. They will be used when switching between
1929 p_stream->b_force_backup = 1;
1931 /* Cheat and get additionnal info ;) */
1932 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1933 oggpack_adv( &opb, 88 );
1934 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1935 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1936 oggpack_read( &opb, 32 );
1937 oggpack_adv( &opb, 32 );
1938 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1940 if ( p_demux->p_sys->i_length < 0 )
1942 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
1943 if ( last_packet >= 0 )
1944 p_demux->p_sys->i_length = last_packet;
1948 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1949 ogg_packet *p_oggpacket )
1953 p_stream->fmt.i_cat = AUDIO_ES;
1954 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1956 /* Signal that we want to keep a backup of the speex
1957 * stream headers. They will be used when switching between
1959 p_stream->b_force_backup = 1;
1961 /* Cheat and get additionnal info ;) */
1962 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1963 oggpack_adv( &opb, 224 );
1964 oggpack_adv( &opb, 32 ); /* speex_version_id */
1965 oggpack_adv( &opb, 32 ); /* header_size */
1966 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1967 oggpack_adv( &opb, 32 ); /* mode */
1968 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1969 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1970 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1973 static void Ogg_ReadOpusHeader( demux_t *p_demux,
1974 logical_stream_t *p_stream,
1975 ogg_packet *p_oggpacket )
1979 p_stream->fmt.i_cat = AUDIO_ES;
1980 p_stream->fmt.i_codec = VLC_CODEC_OPUS;
1982 /* Signal that we want to keep a backup of the opus
1983 * stream headers. They will be used when switching between
1985 p_stream->b_force_backup = 1;
1987 /* All OggOpus streams are timestamped at 48kHz and
1988 * can be played at 48kHz. */
1989 p_stream->f_rate = p_stream->fmt.audio.i_rate = 48000;
1990 p_stream->fmt.i_bitrate = 0;
1992 /* Cheat and get additional info ;) */
1993 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1994 oggpack_adv( &opb, 64 );
1995 oggpack_adv( &opb, 8 ); /* version_id */
1996 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1997 p_stream->i_pre_skip = oggpack_read( &opb, 16 );
1999 if ( p_demux->p_sys->i_length < 0 )
2001 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
2002 if ( last_packet >= 0 )
2003 p_demux->p_sys->i_length = last_packet;
2007 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
2008 ogg_packet *p_oggpacket )
2010 /* Parse the STREAMINFO metadata */
2013 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
2016 if( p_oggpacket->bytes > 0 && bs_read( &s, 7 ) == 0 )
2018 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
2021 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
2022 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
2024 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
2025 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
2029 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
2032 /* Fake this as the last metadata block */
2033 *((uint8_t*)p_oggpacket->packet) |= 0x80;
2037 /* This ain't a STREAMINFO metadata */
2038 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
2042 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
2043 ogg_packet *p_oggpacket )
2051 p_stream->fmt.i_cat = SPU_ES;
2052 p_stream->fmt.i_codec = VLC_CODEC_KATE;
2054 /* Signal that we want to keep a backup of the kate
2055 * stream headers. They will be used when switching between
2057 p_stream->b_force_backup = 1;
2059 /* Cheat and get additionnal info ;) */
2060 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2061 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
2062 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
2063 oggpack_adv( &opb, 3*8 );
2064 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
2065 oggpack_adv( &opb, 8*8 ); /* reserved */
2066 gnum = oggpack_read( &opb, 32 );
2067 gden = oggpack_read( &opb, 32 );
2068 p_stream->f_rate = (double)gnum/gden;
2070 p_stream->fmt.psz_language = malloc(16);
2071 if( p_stream->fmt.psz_language )
2073 for( n = 0; n < 16; n++ )
2074 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
2075 p_stream->fmt.psz_language[15] = 0; /* just in case */
2079 for( n = 0; n < 16; n++ )
2080 oggpack_read(&opb,8);
2082 p_stream->fmt.psz_description = malloc(16);
2083 if( p_stream->fmt.psz_description )
2085 for( n = 0; n < 16; n++ )
2086 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
2087 p_stream->fmt.psz_description[15] = 0; /* just in case */
2089 /* Now find a localized user readable description for this category */
2090 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
2093 free( p_stream->fmt.psz_description );
2094 p_stream->fmt.psz_description = psz_desc;
2099 for( n = 0; n < 16; n++ )
2100 oggpack_read(&opb,8);
2104 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,
2105 logical_stream_t *p_stream,
2106 ogg_packet *p_oggpacket )
2108 if( p_oggpacket->bytes >= 28 &&
2109 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
2113 uint16_t major_version;
2114 uint16_t minor_version;
2115 uint64_t timebase_numerator;
2116 uint64_t timebase_denominator;
2118 Ogg_ReadTheoraHeader( p_demux, p_stream, p_oggpacket );
2120 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2121 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
2122 major_version = oggpack_read( &opb, 2*8 ); /* major version */
2123 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
2124 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
2125 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
2127 else if( p_oggpacket->bytes >= 42 &&
2128 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
2130 uint64_t granule_rate_numerator;
2131 uint64_t granule_rate_denominator;
2132 char content_type_string[1024];
2134 /* Read in Annodex header fields */
2136 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
2137 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
2138 p_stream->i_secondary_header_packets =
2139 GetDWLE( &p_oggpacket->packet[24] );
2141 /* we are guaranteed that the first header field will be
2142 * the content-type (by the Annodex standard) */
2143 content_type_string[0] = '\0';
2144 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
2146 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
2147 p_oggpacket->bytes - 1 );
2148 if( p && p[0] == '\r' && p[1] == '\n' )
2149 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
2150 content_type_string );
2153 msg_Dbg( p_demux, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
2154 granule_rate_numerator, granule_rate_denominator,
2155 p_stream->i_secondary_header_packets, content_type_string );
2157 p_stream->f_rate = (float) granule_rate_numerator /
2158 (float) granule_rate_denominator;
2160 /* What type of file do we have?
2161 * strcmp is safe to use here because we've extracted
2162 * content_type_string from the stream manually */
2163 if( !strncmp(content_type_string, "audio/x-wav", 11) )
2165 /* n.b. WAVs are unsupported right now */
2166 p_stream->fmt.i_cat = UNKNOWN_ES;
2168 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
2170 p_stream->fmt.i_cat = AUDIO_ES;
2171 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2173 p_stream->b_force_backup = 1;
2175 else if( !strncmp(content_type_string, "audio/x-speex", 13) )
2177 p_stream->fmt.i_cat = AUDIO_ES;
2178 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2180 p_stream->b_force_backup = 1;
2182 else if( !strncmp(content_type_string, "video/x-theora", 14) )
2184 p_stream->fmt.i_cat = VIDEO_ES;
2185 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2187 p_stream->b_force_backup = 1;
2189 else if( !strncmp(content_type_string, "video/x-xvid", 12) )
2191 p_stream->fmt.i_cat = VIDEO_ES;
2192 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
2194 p_stream->b_force_backup = 1;
2196 else if( !strncmp(content_type_string, "video/mpeg", 10) )
2198 /* n.b. MPEG streams are unsupported right now */
2199 p_stream->fmt.i_cat = VIDEO_ES;
2200 p_stream->fmt.i_codec = VLC_CODEC_MPGV;
2202 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
2204 ogg_stream_packetout( &p_stream->os, p_oggpacket );
2205 p_stream->fmt.i_cat = SPU_ES;
2206 p_stream->fmt.i_codec = VLC_CODEC_CMML;
2211 static uint32_t dirac_uint( bs_t *p_bs )
2213 uint32_t u_count = 0, u_value = 0;
2215 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
2219 u_value |= bs_read( p_bs, 1 );
2222 return (1<<u_count) - 1 + u_value;
2225 static int dirac_bool( bs_t *p_bs )
2227 return bs_read( p_bs, 1 );
2230 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
2231 ogg_packet *p_oggpacket )
2233 static const struct {
2234 uint32_t u_n /* numerator */, u_d /* denominator */;
2235 } p_dirac_frate_tbl[] = { /* table 10.3 */
2236 {1,1}, /* this first value is never used */
2237 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
2238 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
2240 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
2242 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
2243 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
2245 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
2249 p_stream->i_granule_shift = 22; /* not 32 */
2251 /* Backing up stream headers is not required -- seqhdrs are repeated
2252 * thoughout the stream at suitable decoding start points */
2253 p_stream->b_force_backup = 0;
2255 /* read in useful bits from sequence header */
2256 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
2257 bs_skip( &bs, 13*8); /* parse_info_header */
2258 dirac_uint( &bs ); /* major_version */
2259 dirac_uint( &bs ); /* minor_version */
2260 dirac_uint( &bs ); /* profile */
2261 dirac_uint( &bs ); /* level */
2263 uint32_t u_video_format = dirac_uint( &bs ); /* index */
2264 if( u_video_format >= u_dirac_vidfmt_frate )
2266 /* don't know how to parse this ogg dirac stream */
2270 if( dirac_bool( &bs ) )
2272 dirac_uint( &bs ); /* frame_width */
2273 dirac_uint( &bs ); /* frame_height */
2276 if( dirac_bool( &bs ) )
2278 dirac_uint( &bs ); /* chroma_format */
2281 if( dirac_bool( &bs ) )
2283 dirac_uint( &bs ); /* scan_format */
2286 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
2287 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
2288 if( dirac_bool( &bs ) )
2290 uint32_t u_frame_rate_index = dirac_uint( &bs );
2291 if( u_frame_rate_index >= u_dirac_frate_tbl )
2293 /* something is wrong with this stream */
2296 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
2297 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
2298 if( u_frame_rate_index == 0 )
2300 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
2301 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
2304 p_stream->f_rate = (float) u_n / u_d;
2306 /* probably is an ogg dirac es */
2307 p_stream->fmt.i_cat = VIDEO_ES;
2308 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;