1 /*****************************************************************************
2 * ogg.c : ogg stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
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 );
139 /* Logical bitstream headers */
140 static void Ogg_ReadTheoraHeader( demux_t *, logical_stream_t *, ogg_packet * );
141 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
142 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
143 static void Ogg_ReadOpusHeader( logical_stream_t *, ogg_packet * );
144 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
145 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
146 static void Ogg_ReadAnnodexHeader( demux_t *, logical_stream_t *, ogg_packet * );
147 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
149 /*****************************************************************************
150 * Open: initializes ogg demux structures
151 *****************************************************************************/
152 static int Open( vlc_object_t * p_this )
154 demux_t *p_demux = (demux_t *)p_this;
156 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 ) )
166 /* Set exported functions */
167 p_demux->pf_demux = Demux;
168 p_demux->pf_control = Control;
169 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
173 memset( p_sys, 0, sizeof( demux_sys_t ) );
174 p_sys->i_bitrate = 0;
175 p_sys->pp_stream = NULL;
176 p_sys->p_old_stream = NULL;
178 /* Begnning of stream, tell the demux to look for elementary streams. */
182 p_sys->i_length = -1;
184 /* Initialize the Ogg physical bitstream parser */
185 ogg_sync_init( &p_sys->oy );
186 p_sys->b_page_waiting = false;
189 p_sys->p_meta = NULL;
194 /*****************************************************************************
195 * Close: frees unused data
196 *****************************************************************************/
197 static void Close( vlc_object_t *p_this )
199 demux_t *p_demux = (demux_t *)p_this;
200 demux_sys_t *p_sys = p_demux->p_sys ;
202 /* Cleanup the bitstream parser */
203 ogg_sync_clear( &p_sys->oy );
205 Ogg_EndOfStream( p_demux );
207 if( p_sys->p_old_stream )
208 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
213 /*****************************************************************************
214 * Demux: reads and demuxes data packets
215 *****************************************************************************
216 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
217 *****************************************************************************/
218 static int Demux( demux_t * p_demux )
220 demux_sys_t *p_sys = p_demux->p_sys;
221 ogg_packet oggpacket;
223 bool b_skipping = false;
226 if( p_sys->i_eos == p_sys->i_streams )
230 msg_Dbg( p_demux, "end of a group of logical streams" );
231 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
232 * only 1 ES is supported (common case for ogg web radio) */
233 if( p_sys->i_streams == 1 )
235 p_sys->p_old_stream = p_sys->pp_stream[0];
236 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
238 Ogg_EndOfStream( p_demux );
242 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
245 msg_Dbg( p_demux, "beginning of a group of logical streams" );
246 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 );
250 * The first data page of a physical stream is stored in the relevant logical stream
251 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
252 * stream it belongs to if we haven't processed this first page yet. If we do, we
253 * will only process that first page whenever we find the second page for this stream.
254 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
255 * the second page will arrive real quick, this is not fine for Kate, whose second
256 * data page will typically arrive much later.
257 * This means it is now possible to seek right at the start of a stream where the last
258 * logical stream is Kate, without having to wait for the second data page to unblock
259 * the first one, which is the one that triggers the 'no more headers to backup' code.
260 * And, as we all know, seeking without having backed up all headers is bad, since the
261 * codec will fail to initialize if it's missing its headers.
263 if( !p_sys->b_page_waiting)
266 * Demux an ogg page from the stream
268 if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
271 /* Test for End of Stream */
272 if( ogg_page_eos( &p_sys->current_page ) )
277 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
279 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
281 /* if we've just pulled page, look for the right logical stream */
282 if( !p_sys->b_page_waiting )
284 if( p_sys->i_streams == 1 &&
285 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
287 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
288 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
290 p_stream->b_reinit = true;
291 p_stream->i_pcr = VLC_TS_0;
292 p_stream->i_interpolated_pcr = VLC_TS_0;
293 p_stream->i_previous_granulepos = -1;
294 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0);
297 if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
304 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
306 /* Read info from any secondary header packets, if there are any */
307 if( p_stream->i_secondary_header_packets > 0 )
309 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
310 oggpacket.bytes >= 7 &&
311 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
313 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
314 p_stream->i_secondary_header_packets = 0;
316 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
317 oggpacket.bytes >= 7 &&
318 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
320 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
321 p_stream->i_secondary_header_packets = 0;
323 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
325 p_stream->i_secondary_header_packets = 0;
328 /* update start of data pointer */
329 p_stream->i_data_start = stream_Tell( p_demux->s );
333 /* If any streams have i_skip_frames, only decode (pre-roll)
334 * for those streams */
335 if ( b_skipping && p_stream->i_skip_frames == 0 ) continue;
338 if( p_stream->b_reinit )
340 /* If synchro is re-initialized we need to drop all the packets
341 * until we find a new dated one. */
342 Ogg_UpdatePCR( p_stream, &oggpacket );
344 if( p_stream->i_pcr >= 0 )
346 p_stream->b_reinit = false;
347 /* For Opus, trash the first 80 ms of decoded output as
348 well, to avoid blowing out speakers if we get unlucky.
349 Opus predicts content from prior frames, which can go
350 badly if we seek right where the stream goes from very
351 quiet to very loud. It will converge after a bit. */
352 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
354 ogg_int64_t start_time;
356 p_stream->i_skip_frames = 80*48;
357 /* Make sure we never play audio from within the
358 pre-skip at the beginning of the stream. */
360 Ogg_OpusPacketDuration( p_stream, &oggpacket );
361 start_time = p_stream->i_previous_granulepos;
364 start_time = start_time > duration ?
365 start_time - duration : 0;
367 if( p_stream->i_pre_skip > start_time )
369 p_stream->i_skip_frames +=
370 p_stream->i_pre_skip - start_time;
376 p_stream->i_interpolated_pcr = -1;
377 p_stream->i_previous_granulepos = -1;
381 /* An Ogg/vorbis packet contains an end date granulepos */
382 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
383 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
384 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
385 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
387 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
389 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
393 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
394 VLC_TS_0 + p_stream->i_pcr );
400 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
403 if( !p_sys->b_page_waiting )
407 /* if a page was waiting, it's now processed */
408 p_sys->b_page_waiting = false;
411 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
413 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
415 if( p_stream->fmt.i_cat == SPU_ES )
417 if( p_stream->i_interpolated_pcr < 0 )
420 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
421 p_sys->i_pcr = p_stream->i_interpolated_pcr;
424 if( p_sys->i_pcr >= 0 && ! b_skipping )
425 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
430 /*****************************************************************************
432 *****************************************************************************/
433 static int Control( demux_t *p_demux, int i_query, va_list args )
435 demux_sys_t *p_sys = p_demux->p_sys;
444 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
446 vlc_meta_Merge( p_meta, p_sys->p_meta );
449 case DEMUX_HAS_UNSUPPORTED_META:
450 pb_bool = (bool*)va_arg( args, bool* );
455 pi64 = (int64_t*)va_arg( args, int64_t * );
456 *pi64 = p_sys->i_pcr;
462 case DEMUX_GET_ATTACHMENTS:
464 input_attachment_t ***ppp_attach =
465 (input_attachment_t***)va_arg( args, input_attachment_t*** );
466 int *pi_int = (int*)va_arg( args, int * );
468 if( p_sys->i_attachments <= 0 )
471 *pi_int = p_sys->i_attachments;
472 *ppp_attach = xmalloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
473 for( int i = 0; i < p_sys->i_attachments; i++ )
474 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
478 case DEMUX_SET_POSITION:
479 /* forbid seeking if we haven't initialized all logical bitstreams yet;
480 if we allowed, some headers would not get backed up and decoder init
481 would fail, making that logical stream unusable */
482 if( p_sys->i_bos > 0 )
487 for( i = 0; i < p_sys->i_streams; i++ )
489 logical_stream_t *p_stream = p_sys->pp_stream[i];
491 /* we'll trash all the data until we find the next pcr */
492 p_stream->b_reinit = true;
493 p_stream->i_pcr = -1;
494 p_stream->i_interpolated_pcr = -1;
495 p_stream->i_previous_granulepos = -1;
496 ogg_stream_reset( &p_stream->os );
498 ogg_sync_reset( &p_sys->oy );
499 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
501 case DEMUX_GET_LENGTH:
502 if ( p_sys->i_length < 0 )
503 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
505 pi64 = (int64_t*)va_arg( args, int64_t * );
506 *pi64 = p_sys->i_length * 1000000;
511 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
516 /****************************************************************************
517 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
518 ****************************************************************************
519 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
520 * are at the end of stream.
521 ****************************************************************************/
522 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
524 demux_sys_t *p_ogg = p_demux->p_sys ;
528 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
530 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
532 i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
536 ogg_sync_wrote( &p_ogg->oy, i_read );
542 /****************************************************************************
543 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
545 ****************************************************************************/
546 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
547 ogg_packet *p_oggpacket )
549 p_stream->i_end_trim = 0;
550 /* Convert the granulepos into a pcr */
551 if( p_oggpacket->granulepos >= 0 )
553 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
554 p_stream->fmt.i_codec == VLC_CODEC_KATE )
556 ogg_int64_t iframe = p_oggpacket->granulepos >>
557 p_stream->i_granule_shift;
558 ogg_int64_t pframe = p_oggpacket->granulepos -
559 ( iframe << p_stream->i_granule_shift );
561 p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
562 * INT64_C(1000000) / p_stream->f_rate;
564 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
566 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
567 /* NB, OggDirac granulepos values are in units of 2*picturerate */
568 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
573 sample = p_oggpacket->granulepos;
574 if( p_oggpacket->e_o_s &&
575 p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
576 p_stream->i_previous_granulepos >= 0 )
579 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
582 ogg_int64_t end_sample;
583 end_sample = p_stream->i_previous_granulepos + duration;
584 if( end_sample > sample )
585 p_stream->i_end_trim = (int)(end_sample - sample);
588 if (sample >= p_stream->i_pre_skip)
589 sample -= p_stream->i_pre_skip;
592 p_stream->i_pcr = sample * INT64_C(1000000) / p_stream->f_rate;
595 p_stream->i_pcr += VLC_TS_0;
596 p_stream->i_interpolated_pcr = p_stream->i_pcr;
601 p_stream->i_pcr = -1;
603 /* no granulepos available, try to interpolate the pcr.
604 * If we can't then don't touch the old value. */
605 if( p_stream->fmt.i_cat == VIDEO_ES )
606 /* 1 frame per packet */
607 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
608 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
609 p_stream->i_previous_granulepos >= 0 &&
611 Ogg_OpusPacketDuration( p_stream, p_oggpacket ) ) > 0 )
614 p_oggpacket->granulepos =
615 p_stream->i_previous_granulepos + duration;
616 sample = p_oggpacket->granulepos;
617 if (sample >= p_stream->i_pre_skip)
618 sample -= p_stream->i_pre_skip;
621 p_stream->i_interpolated_pcr =
622 VLC_TS_0 + sample * INT64_C(1000000) / p_stream->f_rate;
624 else if( p_stream->fmt.i_bitrate )
626 p_stream->i_interpolated_pcr +=
627 ( p_oggpacket->bytes * INT64_C(1000000) /
628 p_stream->fmt.i_bitrate / 8 );
631 p_stream->i_previous_granulepos = p_oggpacket->granulepos;
634 /****************************************************************************
635 * Ogg_DecodePacket: Decode an Ogg packet.
636 ****************************************************************************/
637 static void Ogg_DecodePacket( demux_t *p_demux,
638 logical_stream_t *p_stream,
639 ogg_packet *p_oggpacket )
643 int i_header_len = 0;
644 mtime_t i_pts = -1, i_interpolated_pts;
645 demux_sys_t *p_ogg = p_demux->p_sys;
648 if( !p_oggpacket->bytes )
650 msg_Dbg( p_demux, "discarding 0 sized packet" );
654 if( p_oggpacket->bytes >= 7 &&
655 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
657 /* it's an Annodex packet -- skip it (do nothing) */
660 else if( p_oggpacket->bytes >= 7 &&
661 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
663 /* it's an AnxData packet -- skip it (do nothing) */
667 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT &&
668 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
670 /* Check the ES is selected */
671 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
672 p_stream->p_es, &b_selected );
674 if( p_stream->b_force_backup )
677 p_stream->i_packets_backup++;
678 switch( p_stream->fmt.i_codec )
680 case VLC_CODEC_VORBIS:
681 case VLC_CODEC_SPEEX:
682 case VLC_CODEC_THEORA:
683 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
688 if( p_stream->i_packets_backup == 2 ) p_stream->b_force_backup = 0;
693 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
695 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
696 p_stream->b_force_backup = 0;
698 else if( p_stream->fmt.audio.i_rate )
700 p_stream->b_force_backup = 0;
701 if( p_oggpacket->bytes >= 9 )
703 p_oggpacket->packet += 9;
704 p_oggpacket->bytes -= 9;
711 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
716 p_stream->b_force_backup = 0;
721 /* Backup the ogg packet (likely an header packet) */
724 void *p_org = p_stream->p_headers;
725 p_stream->i_headers += p_oggpacket->bytes;
726 p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
727 if( p_stream->p_headers )
729 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
730 p_oggpacket->packet, p_oggpacket->bytes );
735 p_stream->i_headers = 0;
736 p_stream->p_headers = NULL;
740 else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
741 p_oggpacket->bytes, p_oggpacket->packet ) )
743 p_stream->i_headers = 0;
744 p_stream->p_headers = NULL;
746 if( p_stream->i_headers > 0 )
748 if( !p_stream->b_force_backup )
750 /* Last header received, commit changes */
751 free( p_stream->fmt.p_extra );
753 p_stream->fmt.i_extra = p_stream->i_headers;
754 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
755 if( p_stream->fmt.p_extra )
756 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
757 p_stream->i_headers );
759 p_stream->fmt.i_extra = 0;
761 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
762 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
763 p_stream->p_es, &p_stream->fmt );
765 if( p_stream->i_headers > 0 )
766 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
767 p_stream->p_headers, p_stream->i_headers );
769 /* we're not at BOS anymore for this logical stream */
774 b_selected = false; /* Discard the header packet */
777 /* Convert the pcr into a pts */
778 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
779 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
780 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
781 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
783 if( p_stream->i_pcr >= 0 )
785 /* This is for streams where the granulepos of the header packets
786 * doesn't match these of the data packets (eg. ogg web radios). */
787 if( p_stream->i_previous_pcr == 0 &&
788 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
791 /* Call the pace control */
792 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
793 VLC_TS_0 + p_stream->i_pcr );
796 p_stream->i_previous_pcr = p_stream->i_pcr;
798 /* The granulepos is the end date of the sample */
799 i_pts = p_stream->i_pcr;
803 /* Convert the granulepos into the next pcr */
804 i_interpolated_pts = p_stream->i_interpolated_pcr;
805 Ogg_UpdatePCR( p_stream, p_oggpacket );
807 /* SPU streams are typically discontinuous, do not mind large gaps */
808 if( p_stream->fmt.i_cat != SPU_ES )
810 if( p_stream->i_pcr >= 0 )
812 /* This is for streams where the granulepos of the header packets
813 * doesn't match these of the data packets (eg. ogg web radios). */
814 if( p_stream->i_previous_pcr == 0 &&
815 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
818 /* Call the pace control */
819 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_stream->i_pcr );
824 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
825 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
826 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
827 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
828 p_stream->i_pcr >= 0 )
830 p_stream->i_previous_pcr = p_stream->i_pcr;
832 /* The granulepos is the start date of the sample */
833 i_pts = p_stream->i_pcr;
838 /* This stream isn't currently selected so we don't need to decode it,
839 * but we did need to store its pcr as it might be selected later on */
843 if( p_oggpacket->bytes <= 0 )
846 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
849 /* may need to preroll after a seek */
850 if ( p_stream->i_skip_frames > 0 )
852 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
855 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
856 if( p_stream->i_skip_frames > duration )
858 p_block->i_flags |= BLOCK_FLAG_PREROLL;
859 p_block->i_nb_samples = 0;
860 p_stream->i_skip_frames -= duration;
864 p_block->i_nb_samples = duration - p_stream->i_skip_frames;
865 if( p_stream->i_previous_granulepos >=
866 p_block->i_nb_samples + p_stream->i_pre_skip )
868 i_pts = VLC_TS_0 + (p_stream->i_previous_granulepos
869 - p_block->i_nb_samples - p_stream->i_pre_skip) *
870 INT64_C(1000000) / p_stream->f_rate;
872 p_stream->i_skip_frames = 0;
877 p_block->i_flags |= BLOCK_FLAG_PREROLL;
878 p_stream->i_skip_frames--;
881 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
882 p_block->i_nb_samples = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
886 if( i_pts == 0 ) i_pts = VLC_TS_0;
887 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = VLC_TS_0;
888 else if( i_pts == -1 ) i_pts = VLC_TS_INVALID;
890 if( p_stream->fmt.i_cat == AUDIO_ES )
892 p_block->i_dts = p_block->i_pts = i_pts;
893 /* Blatant abuse of the i_length field. */
894 p_block->i_length = p_stream->i_end_trim;
896 else if( p_stream->fmt.i_cat == SPU_ES )
898 p_block->i_dts = p_block->i_pts = i_pts;
899 p_block->i_length = 0;
901 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
903 p_block->i_dts = p_block->i_pts = i_pts;
904 if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
906 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
909 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
911 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
912 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
914 uint64_t u_pnum = dts + delay;
916 p_block->i_dts = p_stream->i_pcr;
917 p_block->i_pts = VLC_TS_INVALID;
918 /* NB, OggDirac granulepos values are in units of 2*picturerate */
920 /* granulepos for dirac is possibly broken, this value should be ignored */
921 if( -1 != p_oggpacket->granulepos )
922 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
926 p_block->i_dts = i_pts;
927 p_block->i_pts = VLC_TS_INVALID;
930 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
931 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
932 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
933 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
934 p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
935 p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
936 p_stream->fmt.i_codec != VLC_CODEC_CMML &&
937 p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
938 p_stream->fmt.i_codec != VLC_CODEC_KATE )
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_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_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.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1329 oggpacket.bytes >= 44+1 )
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 void Ogg_ReadTheoraHeader( demux_t *p_demux, logical_stream_t *p_stream,
1831 ogg_packet *p_oggpacket )
1834 int i_fps_numerator;
1835 int i_fps_denominator;
1836 int i_keyframe_frequency_force;
1842 p_stream->fmt.i_cat = VIDEO_ES;
1843 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1845 /* Signal that we want to keep a backup of the theora
1846 * stream headers. They will be used when switching between
1848 p_stream->b_force_backup = 1;
1850 /* Cheat and get additionnal info ;) */
1851 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1852 bs_skip( &bitstream, 56 );
1854 i_major = bs_read( &bitstream, 8 ); /* major version num */
1855 i_minor = bs_read( &bitstream, 8 ); /* minor version num */
1856 i_subminor = bs_read( &bitstream, 8 ); /* subminor version num */
1858 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1859 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1860 bs_read( &bitstream, 24 ); /* frame width */
1861 bs_read( &bitstream, 24 ); /* frame height */
1862 bs_read( &bitstream, 8 ); /* x offset */
1863 bs_read( &bitstream, 8 ); /* y offset */
1865 i_fps_numerator = bs_read( &bitstream, 32 );
1866 i_fps_denominator = bs_read( &bitstream, 32 );
1867 bs_read( &bitstream, 24 ); /* aspect_numerator */
1868 bs_read( &bitstream, 24 ); /* aspect_denominator */
1870 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1871 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1873 bs_read( &bitstream, 8 ); /* colorspace */
1874 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1875 bs_read( &bitstream, 6 ); /* quality */
1877 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1879 /* granule_shift = i_log( frequency_force -1 ) */
1880 p_stream->i_granule_shift = 0;
1881 i_keyframe_frequency_force--;
1882 while( i_keyframe_frequency_force )
1884 p_stream->i_granule_shift++;
1885 i_keyframe_frequency_force >>= 1;
1888 i_version = i_major * 1000000 + i_minor * 1000 + i_subminor;
1889 p_stream->i_keyframe_offset = 0;
1891 if ( i_version >= 3002001 )
1893 p_stream->i_keyframe_offset = 1;
1895 if ( p_demux->p_sys->i_length < 0 )
1897 int64_t last_frame = oggseek_get_last_frame( p_demux, p_stream );
1899 * Since there's quite a good chance that ogg_stream_packetout was called,
1900 * the given p_oggpacket may point to invalid data. Fill it with some valid ones
1902 ogg_stream_packetpeek( &p_stream->os, p_oggpacket );
1904 if ( last_frame >= 0 )
1906 p_demux->p_sys->i_length = last_frame / ((float)i_fps_numerator /
1907 (float)i_fps_denominator);
1911 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1914 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1915 ogg_packet *p_oggpacket )
1919 p_stream->fmt.i_cat = AUDIO_ES;
1920 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1922 /* Signal that we want to keep a backup of the vorbis
1923 * stream headers. They will be used when switching between
1925 p_stream->b_force_backup = 1;
1927 /* Cheat and get additionnal info ;) */
1928 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1929 oggpack_adv( &opb, 88 );
1930 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1931 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1932 oggpack_read( &opb, 32 );
1933 oggpack_adv( &opb, 32 );
1934 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1937 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1938 ogg_packet *p_oggpacket )
1942 p_stream->fmt.i_cat = AUDIO_ES;
1943 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1945 /* Signal that we want to keep a backup of the speex
1946 * stream headers. They will be used when switching between
1948 p_stream->b_force_backup = 1;
1950 /* Cheat and get additionnal info ;) */
1951 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1952 oggpack_adv( &opb, 224 );
1953 oggpack_adv( &opb, 32 ); /* speex_version_id */
1954 oggpack_adv( &opb, 32 ); /* header_size */
1955 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1956 oggpack_adv( &opb, 32 ); /* mode */
1957 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1958 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1959 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1962 static void Ogg_ReadOpusHeader( 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_OPUS;
1970 /* Signal that we want to keep a backup of the opus
1971 * stream headers. They will be used when switching between
1973 p_stream->b_force_backup = 1;
1975 /* All OggOpus streams are timestamped at 48kHz and
1976 * can be played at 48kHz. */
1977 p_stream->f_rate = p_stream->fmt.audio.i_rate = 48000;
1978 p_stream->fmt.i_bitrate = 0;
1980 /* Cheat and get additional info ;) */
1981 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1982 oggpack_adv( &opb, 64 );
1983 oggpack_adv( &opb, 8 ); /* version_id */
1984 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1985 p_stream->i_pre_skip = oggpack_read( &opb, 16 );
1988 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1989 ogg_packet *p_oggpacket )
1991 /* Parse the STREAMINFO metadata */
1994 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1997 if( bs_read( &s, 7 ) == 0 )
1999 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
2002 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
2003 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
2005 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
2006 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
2010 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
2013 /* Fake this as the last metadata block */
2014 *((uint8_t*)p_oggpacket->packet) |= 0x80;
2018 /* This ain't a STREAMINFO metadata */
2019 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
2023 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
2024 ogg_packet *p_oggpacket )
2032 p_stream->fmt.i_cat = SPU_ES;
2033 p_stream->fmt.i_codec = VLC_CODEC_KATE;
2035 /* Signal that we want to keep a backup of the kate
2036 * stream headers. They will be used when switching between
2038 p_stream->b_force_backup = 1;
2040 /* Cheat and get additionnal info ;) */
2041 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2042 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
2043 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
2044 oggpack_adv( &opb, 3*8 );
2045 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
2046 oggpack_adv( &opb, 8*8 ); /* reserved */
2047 gnum = oggpack_read( &opb, 32 );
2048 gden = oggpack_read( &opb, 32 );
2049 p_stream->f_rate = (double)gnum/gden;
2051 p_stream->fmt.psz_language = malloc(16);
2052 if( p_stream->fmt.psz_language )
2054 for( n = 0; n < 16; n++ )
2055 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
2056 p_stream->fmt.psz_language[15] = 0; /* just in case */
2060 for( n = 0; n < 16; n++ )
2061 oggpack_read(&opb,8);
2063 p_stream->fmt.psz_description = malloc(16);
2064 if( p_stream->fmt.psz_description )
2066 for( n = 0; n < 16; n++ )
2067 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
2068 p_stream->fmt.psz_description[15] = 0; /* just in case */
2070 /* Now find a localized user readable description for this category */
2071 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
2074 free( p_stream->fmt.psz_description );
2075 p_stream->fmt.psz_description = psz_desc;
2080 for( n = 0; n < 16; n++ )
2081 oggpack_read(&opb,8);
2085 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,
2086 logical_stream_t *p_stream,
2087 ogg_packet *p_oggpacket )
2089 if( p_oggpacket->bytes >= 28 &&
2090 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
2094 uint16_t major_version;
2095 uint16_t minor_version;
2096 uint64_t timebase_numerator;
2097 uint64_t timebase_denominator;
2099 Ogg_ReadTheoraHeader( p_demux, p_stream, p_oggpacket );
2101 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2102 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
2103 major_version = oggpack_read( &opb, 2*8 ); /* major version */
2104 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
2105 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
2106 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
2108 else if( p_oggpacket->bytes >= 42 &&
2109 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
2111 uint64_t granule_rate_numerator;
2112 uint64_t granule_rate_denominator;
2113 char content_type_string[1024];
2115 /* Read in Annodex header fields */
2117 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
2118 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
2119 p_stream->i_secondary_header_packets =
2120 GetDWLE( &p_oggpacket->packet[24] );
2122 /* we are guaranteed that the first header field will be
2123 * the content-type (by the Annodex standard) */
2124 content_type_string[0] = '\0';
2125 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
2127 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
2128 p_oggpacket->bytes - 1 );
2129 if( p && p[0] == '\r' && p[1] == '\n' )
2130 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
2131 content_type_string );
2134 msg_Dbg( p_demux, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
2135 granule_rate_numerator, granule_rate_denominator,
2136 p_stream->i_secondary_header_packets, content_type_string );
2138 p_stream->f_rate = (float) granule_rate_numerator /
2139 (float) granule_rate_denominator;
2141 /* What type of file do we have?
2142 * strcmp is safe to use here because we've extracted
2143 * content_type_string from the stream manually */
2144 if( !strncmp(content_type_string, "audio/x-wav", 11) )
2146 /* n.b. WAVs are unsupported right now */
2147 p_stream->fmt.i_cat = UNKNOWN_ES;
2149 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
2151 p_stream->fmt.i_cat = AUDIO_ES;
2152 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2154 p_stream->b_force_backup = 1;
2156 else if( !strncmp(content_type_string, "audio/x-speex", 13) )
2158 p_stream->fmt.i_cat = AUDIO_ES;
2159 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2161 p_stream->b_force_backup = 1;
2163 else if( !strncmp(content_type_string, "video/x-theora", 14) )
2165 p_stream->fmt.i_cat = VIDEO_ES;
2166 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2168 p_stream->b_force_backup = 1;
2170 else if( !strncmp(content_type_string, "video/x-xvid", 12) )
2172 p_stream->fmt.i_cat = VIDEO_ES;
2173 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
2175 p_stream->b_force_backup = 1;
2177 else if( !strncmp(content_type_string, "video/mpeg", 10) )
2179 /* n.b. MPEG streams are unsupported right now */
2180 p_stream->fmt.i_cat = VIDEO_ES;
2181 p_stream->fmt.i_codec = VLC_CODEC_MPGV;
2183 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
2185 ogg_stream_packetout( &p_stream->os, p_oggpacket );
2186 p_stream->fmt.i_cat = SPU_ES;
2187 p_stream->fmt.i_codec = VLC_CODEC_CMML;
2192 static uint32_t dirac_uint( bs_t *p_bs )
2194 uint32_t u_count = 0, u_value = 0;
2196 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
2200 u_value |= bs_read( p_bs, 1 );
2203 return (1<<u_count) - 1 + u_value;
2206 static int dirac_bool( bs_t *p_bs )
2208 return bs_read( p_bs, 1 );
2211 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
2212 ogg_packet *p_oggpacket )
2214 static const struct {
2215 uint32_t u_n /* numerator */, u_d /* denominator */;
2216 } p_dirac_frate_tbl[] = { /* table 10.3 */
2217 {1,1}, /* this first value is never used */
2218 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
2219 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
2221 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
2223 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
2224 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
2226 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
2230 p_stream->i_granule_shift = 22; /* not 32 */
2232 /* Backing up stream headers is not required -- seqhdrs are repeated
2233 * thoughout the stream at suitable decoding start points */
2234 p_stream->b_force_backup = 0;
2236 /* read in useful bits from sequence header */
2237 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
2238 bs_skip( &bs, 13*8); /* parse_info_header */
2239 dirac_uint( &bs ); /* major_version */
2240 dirac_uint( &bs ); /* minor_version */
2241 dirac_uint( &bs ); /* profile */
2242 dirac_uint( &bs ); /* level */
2244 uint32_t u_video_format = dirac_uint( &bs ); /* index */
2245 if( u_video_format >= u_dirac_vidfmt_frate )
2247 /* don't know how to parse this ogg dirac stream */
2251 if( dirac_bool( &bs ) )
2253 dirac_uint( &bs ); /* frame_width */
2254 dirac_uint( &bs ); /* frame_height */
2257 if( dirac_bool( &bs ) )
2259 dirac_uint( &bs ); /* chroma_format */
2262 if( dirac_bool( &bs ) )
2264 dirac_uint( &bs ); /* scan_format */
2267 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
2268 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
2269 if( dirac_bool( &bs ) )
2271 uint32_t u_frame_rate_index = dirac_uint( &bs );
2272 if( u_frame_rate_index >= u_dirac_frate_tbl )
2274 /* something is wrong with this stream */
2277 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
2278 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
2279 if( u_frame_rate_index == 0 )
2281 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
2282 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
2285 p_stream->f_rate = (float) u_n / u_d;
2287 /* probably is an ogg dirac es */
2288 p_stream->fmt.i_cat = VIDEO_ES;
2289 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;