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"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_shortname ( "OGG" )
54 set_description( N_("OGG demuxer" ) )
55 set_category( CAT_INPUT )
56 set_subcategory( SUBCAT_INPUT_DEMUX )
57 set_capability( "demux", 50 )
58 set_callbacks( Open, Close )
63 /*****************************************************************************
64 * Definitions of structures and functions used by this plugins
65 *****************************************************************************/
66 typedef struct logical_stream_s
68 ogg_stream_state os; /* logical stream of packets */
71 es_format_t fmt_old; /* format of old ES is reused */
77 /* the header of some logical streams (eg vorbis) contain essential
78 * data for the decoder. We back them up here in case we need to re-feed
79 * them to the decoder. */
85 /* program clock reference (in units of 90kHz) derived from the previous
88 mtime_t i_interpolated_pcr;
89 mtime_t i_previous_pcr;
95 /* kate streams have the number of headers in the ID header */
96 int i_kate_num_headers;
98 /* for Annodex logical bitstreams */
99 int i_secondary_header_packets;
105 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
107 int i_streams; /* number of logical bitstreams */
108 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
110 logical_stream_t *p_old_stream; /* pointer to a old logical stream to avoid recreating it */
112 /* program clock reference (in units of 90kHz) derived from the pcr of
123 /* after reading all headers, the first data page is stuffed into the relevant stream, ready to use */
130 /* OggDS headers for the new header format (used in ogm files) */
135 } stream_header_video_t;
139 ogg_int16_t channels;
141 ogg_int16_t blockalign;
142 ogg_int32_t avgbytespersec;
143 } stream_header_audio_t;
150 ogg_int32_t size; /* size of the structure */
152 ogg_int64_t time_unit; /* in reference time */
153 ogg_int64_t samples_per_unit;
154 ogg_int32_t default_len; /* in media time */
156 ogg_int32_t buffersize;
157 ogg_int16_t bits_per_sample;
163 stream_header_video_t video;
165 stream_header_audio_t audio;
169 #define OGG_BLOCK_SIZE 4096
171 /* Some defines from OggDS */
172 #define PACKET_TYPE_HEADER 0x01
173 #define PACKET_TYPE_BITS 0x07
174 #define PACKET_LEN_BITS01 0xc0
175 #define PACKET_LEN_BITS2 0x02
176 #define PACKET_IS_SYNCPOINT 0x08
178 /*****************************************************************************
180 *****************************************************************************/
181 static int Demux ( demux_t * );
182 static int Control( demux_t *, int, va_list );
184 /* Bitstream manipulation */
185 static int Ogg_ReadPage ( demux_t *, ogg_page * );
186 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
187 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
189 static int Ogg_BeginningOfStream( demux_t *p_demux );
190 static int Ogg_FindLogicalStreams( demux_t *p_demux );
191 static void Ogg_EndOfStream( demux_t *p_demux );
194 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
195 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
198 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers );
200 /* Logical bitstream headers */
201 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
202 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
203 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
204 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
205 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
206 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
207 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
209 /*****************************************************************************
210 * Open: initializes ogg demux structures
211 *****************************************************************************/
212 static int Open( vlc_object_t * p_this )
214 demux_t *p_demux = (demux_t *)p_this;
216 const uint8_t *p_peek;
219 /* Check if we are dealing with an ogg stream */
220 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
221 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
226 /* Set exported functions */
227 p_demux->pf_demux = Demux;
228 p_demux->pf_control = Control;
229 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
233 memset( p_sys, 0, sizeof( demux_sys_t ) );
234 p_sys->i_bitrate = 0;
235 p_sys->pp_stream = NULL;
236 p_sys->p_old_stream = NULL;
238 /* Begnning of stream, tell the demux to look for elementary streams. */
242 /* Initialize the Ogg physical bitstream parser */
243 ogg_sync_init( &p_sys->oy );
244 p_sys->b_page_waiting = false;
247 p_sys->p_meta = NULL;
252 /*****************************************************************************
253 * Close: frees unused data
254 *****************************************************************************/
255 static void Close( vlc_object_t *p_this )
257 demux_t *p_demux = (demux_t *)p_this;
258 demux_sys_t *p_sys = p_demux->p_sys ;
260 /* Cleanup the bitstream parser */
261 ogg_sync_clear( &p_sys->oy );
263 Ogg_EndOfStream( p_demux );
265 if( p_sys->p_old_stream )
266 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
271 /*****************************************************************************
272 * Demux: reads and demuxes data packets
273 *****************************************************************************
274 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
275 *****************************************************************************/
276 static int Demux( demux_t * p_demux )
278 demux_sys_t *p_sys = p_demux->p_sys;
280 ogg_packet oggpacket;
284 if( p_sys->i_eos == p_sys->i_streams )
288 msg_Dbg( p_demux, "end of a group of logical streams" );
289 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
290 * only 1 ES is supported (common case for ogg web radio) */
291 if( p_sys->i_streams == 1 )
293 p_sys->p_old_stream = p_sys->pp_stream[0];
294 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
296 Ogg_EndOfStream( p_demux );
300 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
303 msg_Dbg( p_demux, "beginning of a group of logical streams" );
304 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
308 * The first data page of a physical stream is stored in the relevant logical stream
309 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
310 * stream it belongs to if we haven't processed this first page yet. If we do, we
311 * will only process that first page whenever we find the second page for this stream.
312 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
313 * the second page will arrive real quick, this is not fine for Kate, whose second
314 * data page will typically arrive much later.
315 * This means it is now possible to seek right at the start of a stream where the last
316 * logical stream is Kate, without having to wait for the second data page to unblock
317 * the first one, which is the one that triggers the 'no more headers to backup' code.
318 * And, as we all know, seeking without having backed up all headers is bad, since the
319 * codec will fail to initialize if it's missing its headers.
321 if( !p_sys->b_page_waiting)
324 * Demux an ogg page from the stream
326 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
329 /* Test for End of Stream */
330 if( ogg_page_eos( &oggpage ) )
335 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
337 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
339 /* if we've just pulled page, look for the right logical stream */
340 if( !p_sys->b_page_waiting )
342 if( p_sys->i_streams == 1 &&
343 ogg_page_serialno( &oggpage ) != p_stream->os.serialno )
345 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
346 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &oggpage ) );
348 p_stream->b_reinit = true;
349 p_stream->i_pcr = -1;
350 p_stream->i_interpolated_pcr = -1;
351 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
354 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
358 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
360 /* Read info from any secondary header packets, if there are any */
361 if( p_stream->i_secondary_header_packets > 0 )
363 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
364 oggpacket.bytes >= 7 &&
365 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
367 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
368 p_stream->i_secondary_header_packets = 0;
370 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
371 oggpacket.bytes >= 7 &&
372 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
374 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
375 p_stream->i_secondary_header_packets = 0;
377 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
379 p_stream->i_secondary_header_packets = 0;
383 if( p_stream->b_reinit )
385 /* If synchro is re-initialized we need to drop all the packets
386 * until we find a new dated one. */
387 Ogg_UpdatePCR( p_stream, &oggpacket );
389 if( p_stream->i_pcr >= 0 )
391 p_stream->b_reinit = false;
395 p_stream->i_interpolated_pcr = -1;
399 /* An Ogg/vorbis packet contains an end date granulepos */
400 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
401 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
402 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
404 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
406 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
410 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
411 VLC_TS_0 + p_stream->i_pcr );
417 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
420 if( !p_sys->b_page_waiting )
424 /* if a page was waiting, it's now processed */
425 p_sys->b_page_waiting = false;
428 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
430 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
432 if( p_stream->fmt.i_cat == SPU_ES )
434 if( p_stream->i_interpolated_pcr < 0 )
437 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
438 p_sys->i_pcr = p_stream->i_interpolated_pcr;
441 if( p_sys->i_pcr >= 0 )
442 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
447 /*****************************************************************************
449 *****************************************************************************/
450 static int Control( demux_t *p_demux, int i_query, va_list args )
452 demux_sys_t *p_sys = p_demux->p_sys;
461 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
463 vlc_meta_Merge( p_meta, p_sys->p_meta );
466 case DEMUX_HAS_UNSUPPORTED_META:
467 pb_bool = (bool*)va_arg( args, bool* );
472 pi64 = (int64_t*)va_arg( args, int64_t * );
473 *pi64 = p_sys->i_pcr;
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 ogg_stream_reset( &p_stream->os );
498 ogg_sync_reset( &p_sys->oy );
499 /* XXX The break/return is missing on purpose as
500 * demux_vaControlHelper will do the last part of the job */
503 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
508 /****************************************************************************
509 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
510 ****************************************************************************
511 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
512 * are at the end of stream.
513 ****************************************************************************/
514 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
516 demux_sys_t *p_ogg = p_demux->p_sys ;
520 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
522 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
524 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
528 ogg_sync_wrote( &p_ogg->oy, i_read );
534 /****************************************************************************
535 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
537 ****************************************************************************/
538 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
539 ogg_packet *p_oggpacket )
541 /* Convert the granulepos into a pcr */
542 if( p_oggpacket->granulepos >= 0 )
544 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
545 p_stream->fmt.i_codec == VLC_CODEC_KATE )
547 ogg_int64_t iframe = p_oggpacket->granulepos >>
548 p_stream->i_granule_shift;
549 ogg_int64_t pframe = p_oggpacket->granulepos -
550 ( iframe << p_stream->i_granule_shift );
552 p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
555 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
557 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
558 /* NB, OggDirac granulepos values are in units of 2*picturerate */
559 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
563 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
567 p_stream->i_pcr += 1;
568 p_stream->i_interpolated_pcr = p_stream->i_pcr;
572 p_stream->i_pcr = -1;
574 /* no granulepos available, try to interpolate the pcr.
575 * If we can't then don't touch the old value. */
576 if( p_stream->fmt.i_cat == VIDEO_ES )
577 /* 1 frame per packet */
578 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
579 else if( p_stream->fmt.i_bitrate )
580 p_stream->i_interpolated_pcr +=
581 ( p_oggpacket->bytes * INT64_C(1000000) /
582 p_stream->fmt.i_bitrate / 8 );
586 /****************************************************************************
587 * Ogg_DecodePacket: Decode an Ogg packet.
588 ****************************************************************************/
589 static void Ogg_DecodePacket( demux_t *p_demux,
590 logical_stream_t *p_stream,
591 ogg_packet *p_oggpacket )
595 int i_header_len = 0;
596 mtime_t i_pts = -1, i_interpolated_pts;
597 demux_sys_t *p_ogg = p_demux->p_sys;
600 if( !p_oggpacket->bytes )
602 msg_Dbg( p_demux, "discarding 0 sized packet" );
606 if( p_oggpacket->bytes >= 7 &&
607 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
609 /* it's an Annodex packet -- skip it (do nothing) */
612 else if( p_oggpacket->bytes >= 7 &&
613 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
615 /* it's an AnxData packet -- skip it (do nothing) */
619 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT &&
620 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
622 /* Check the ES is selected */
623 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
624 p_stream->p_es, &b_selected );
626 if( p_stream->b_force_backup )
629 p_stream->i_packets_backup++;
630 switch( p_stream->fmt.i_codec )
632 case VLC_CODEC_VORBIS:
633 case VLC_CODEC_SPEEX:
634 case VLC_CODEC_THEORA:
635 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
640 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
642 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
643 p_stream->b_force_backup = 0;
645 else if( p_stream->fmt.audio.i_rate )
647 p_stream->b_force_backup = 0;
648 if( p_oggpacket->bytes >= 9 )
650 p_oggpacket->packet += 9;
651 p_oggpacket->bytes -= 9;
658 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
663 p_stream->b_force_backup = 0;
668 /* Backup the ogg packet (likely an header packet) */
671 void *p_org = p_stream->p_headers;
672 p_stream->i_headers += p_oggpacket->bytes;
673 p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
674 if( p_stream->p_headers )
676 memcpy( p_stream->p_headers, p_oggpacket->packet, p_stream->i_headers );
680 p_stream->i_headers = 0;
681 p_stream->p_headers = NULL;
685 else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
686 p_oggpacket->bytes, p_oggpacket->packet ) )
688 p_stream->i_headers = 0;
689 p_stream->p_headers = NULL;
691 if( p_stream->i_headers > 0 )
693 if( !p_stream->b_force_backup )
695 /* Last header received, commit changes */
696 free( p_stream->fmt.p_extra );
698 p_stream->fmt.i_extra = p_stream->i_headers;
699 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
700 if( p_stream->fmt.p_extra )
701 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
702 p_stream->i_headers );
704 p_stream->fmt.i_extra = 0;
706 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
707 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
708 p_stream->p_es, &p_stream->fmt );
710 if( p_stream->i_headers > 0 )
711 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
712 p_stream->p_headers, p_stream->i_headers );
714 /* we're not at BOS anymore for this logical stream */
719 b_selected = false; /* Discard the header packet */
722 /* Convert the pcr into a pts */
723 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
724 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
725 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
727 if( p_stream->i_pcr >= 0 )
729 /* This is for streams where the granulepos of the header packets
730 * doesn't match these of the data packets (eg. ogg web radios). */
731 if( p_stream->i_previous_pcr == 0 &&
732 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
734 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
736 /* Call the pace control */
737 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
738 VLC_TS_0 + p_stream->i_pcr );
741 p_stream->i_previous_pcr = p_stream->i_pcr;
743 /* The granulepos is the end date of the sample */
744 i_pts = p_stream->i_pcr;
748 /* Convert the granulepos into the next pcr */
749 i_interpolated_pts = p_stream->i_interpolated_pcr;
750 Ogg_UpdatePCR( p_stream, p_oggpacket );
752 /* SPU streams are typically discontinuous, do not mind large gaps */
753 if( p_stream->fmt.i_cat != SPU_ES )
755 if( p_stream->i_pcr >= 0 )
757 /* This is for streams where the granulepos of the header packets
758 * doesn't match these of the data packets (eg. ogg web radios). */
759 if( p_stream->i_previous_pcr == 0 &&
760 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
762 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
764 /* Call the pace control */
765 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_stream->i_pcr );
770 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
771 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
772 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
773 p_stream->i_pcr >= 0 )
775 p_stream->i_previous_pcr = p_stream->i_pcr;
777 /* The granulepos is the start date of the sample */
778 i_pts = p_stream->i_pcr;
783 /* This stream isn't currently selected so we don't need to decode it,
784 * but we did need to store its pcr as it might be selected later on */
788 if( p_oggpacket->bytes <= 0 )
791 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
794 if( i_pts == 0 ) i_pts = VLC_TS_0;
795 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = VLC_TS_0;
796 else if( i_pts == -1 ) i_pts = VLC_TS_INVALID;
798 if( p_stream->fmt.i_cat == AUDIO_ES )
799 p_block->i_dts = p_block->i_pts = i_pts;
800 else if( p_stream->fmt.i_cat == SPU_ES )
802 p_block->i_dts = p_block->i_pts = i_pts;
803 p_block->i_length = 0;
805 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
807 p_block->i_dts = p_block->i_pts = i_pts;
808 if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
810 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
813 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
815 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
816 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
818 uint64_t u_pnum = dts + delay;
820 p_block->i_dts = p_stream->i_pcr;
821 p_block->i_pts = VLC_TS_INVALID;
822 /* NB, OggDirac granulepos values are in units of 2*picturerate */
823 if( -1 != p_oggpacket->granulepos )
824 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
828 p_block->i_dts = i_pts;
829 p_block->i_pts = VLC_TS_INVALID;
832 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
833 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
834 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
835 p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
836 p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
837 p_stream->fmt.i_codec != VLC_CODEC_CMML &&
838 p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
839 p_stream->fmt.i_codec != VLC_CODEC_KATE )
841 /* We remove the header from the packet */
842 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
843 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
845 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
847 /* But with subtitles we need to retrieve the duration first */
850 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
852 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
854 lenbytes = lenbytes << 8;
855 lenbytes += *(p_oggpacket->packet + i_header_len - i);
858 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
859 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
860 p_oggpacket->packet[i_header_len + 1] != 0 &&
861 p_oggpacket->packet[i_header_len + 1] != '\n' &&
862 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
864 p_block->i_length = (mtime_t)lenbytes * 1000;
869 if( p_block->i_buffer >= (unsigned int)i_header_len )
870 p_block->i_buffer -= i_header_len;
872 p_block->i_buffer = 0;
875 if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
877 /* FIXME: the biggest hack I've ever done */
878 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
879 p_block->i_pts, p_block->i_dts );
883 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
884 p_oggpacket->bytes - i_header_len );
886 es_out_Send( p_demux->out, p_stream->p_es, p_block );
889 /****************************************************************************
890 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
891 * stream and fill p_ogg.
892 *****************************************************************************
893 * The initial page of a logical stream is marked as a 'bos' page.
894 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
895 * together and all of the initial pages must appear before any data pages.
897 * On success this function returns VLC_SUCCESS.
898 ****************************************************************************/
899 static int Ogg_FindLogicalStreams( demux_t *p_demux )
901 demux_sys_t *p_ogg = p_demux->p_sys ;
902 ogg_packet oggpacket;
906 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
908 if( ogg_page_bos( &oggpage ) )
911 /* All is wonderful in our fine fine little world.
912 * We found the beginning of our first logical stream. */
913 while( ogg_page_bos( &oggpage ) )
915 logical_stream_t *p_stream;
917 p_stream = malloc( sizeof(logical_stream_t) );
921 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
923 memset( p_stream, 0, sizeof(logical_stream_t) );
924 p_stream->p_headers = 0;
925 p_stream->i_secondary_header_packets = 0;
927 es_format_Init( &p_stream->fmt, 0, 0 );
928 es_format_Init( &p_stream->fmt_old, 0, 0 );
930 /* Setup the logical stream */
931 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
932 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
934 /* Extract the initial header from the first page and verify
935 * the codec type of this Ogg bitstream */
936 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
938 /* error. stream version mismatch perhaps */
939 msg_Err( p_demux, "error reading first page of "
940 "Ogg bitstream data" );
944 /* FIXME: check return value */
945 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
947 /* Check for Vorbis header */
948 if( oggpacket.bytes >= 7 &&
949 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
951 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
952 msg_Dbg( p_demux, "found vorbis header" );
954 /* Check for Speex header */
955 else if( oggpacket.bytes >= 5 &&
956 ! memcmp( oggpacket.packet, "Speex", 5 ) )
958 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
959 msg_Dbg( p_demux, "found speex header, channels: %i, "
960 "rate: %i, bitrate: %i",
961 p_stream->fmt.audio.i_channels,
962 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
964 /* Check for Flac header (< version 1.1.1) */
965 else if( oggpacket.bytes >= 4 &&
966 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
968 msg_Dbg( p_demux, "found FLAC header" );
970 /* Grrrr!!!! Did they really have to put all the
971 * important info in the second header packet!!!
972 * (STREAMINFO metadata is in the following packet) */
973 p_stream->b_force_backup = 1;
975 p_stream->fmt.i_cat = AUDIO_ES;
976 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
978 /* Check for Flac header (>= version 1.1.1) */
979 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
980 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
981 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
983 int i_packets = ((int)oggpacket.packet[7]) << 8 |
985 msg_Dbg( p_demux, "found FLAC header version %i.%i "
986 "(%i header packets)",
987 oggpacket.packet[5], oggpacket.packet[6],
990 p_stream->b_force_backup = 1;
992 p_stream->fmt.i_cat = AUDIO_ES;
993 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
994 oggpacket.packet += 13; oggpacket.bytes -= 13;
995 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
997 /* Check for Theora header */
998 else if( oggpacket.bytes >= 7 &&
999 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
1001 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
1004 "found theora header, bitrate: %i, rate: %f",
1005 p_stream->fmt.i_bitrate, p_stream->f_rate );
1007 /* Check for Dirac header */
1008 else if( oggpacket.bytes >= 5 &&
1009 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
1011 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1012 msg_Dbg( p_demux, "found dirac header" );
1015 msg_Warn( p_demux, "found dirac header isn't decodable" );
1020 /* Check for Tarkin header */
1021 else if( oggpacket.bytes >= 7 &&
1022 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1026 msg_Dbg( p_demux, "found tarkin header" );
1027 p_stream->fmt.i_cat = VIDEO_ES;
1028 p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1030 /* Cheat and get additionnal info ;) */
1031 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1032 oggpack_adv( &opb, 88 );
1033 oggpack_adv( &opb, 104 );
1034 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1035 p_stream->f_rate = 2; /* FIXME */
1037 "found tarkin header, bitrate: %i, rate: %f",
1038 p_stream->fmt.i_bitrate, p_stream->f_rate );
1040 /* Check for Annodex header */
1041 else if( oggpacket.bytes >= 7 &&
1042 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1044 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1046 /* kill annodex track */
1050 /* Check for Annodex header */
1051 else if( oggpacket.bytes >= 7 &&
1052 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1054 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1057 /* Check for Kate header */
1058 else if( oggpacket.bytes >= 8 &&
1059 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1061 Ogg_ReadKateHeader( p_stream, &oggpacket );
1062 msg_Dbg( p_demux, "found kate header" );
1064 else if( oggpacket.bytes >= 142 &&
1065 !memcmp( &oggpacket.packet[1],
1066 "Direct Show Samples embedded in Ogg", 35 ))
1068 /* Old header type */
1070 /* Check for video header (old format) */
1071 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1072 oggpacket.bytes >= 184 )
1074 p_stream->fmt.i_cat = VIDEO_ES;
1075 p_stream->fmt.i_codec =
1076 VLC_FOURCC( oggpacket.packet[68],
1077 oggpacket.packet[69],
1078 oggpacket.packet[70],
1079 oggpacket.packet[71] );
1080 msg_Dbg( p_demux, "found video header of type: %.4s",
1081 (char *)&p_stream->fmt.i_codec );
1083 p_stream->fmt.video.i_frame_rate = 10000000;
1084 p_stream->fmt.video.i_frame_rate_base =
1085 GetQWLE((oggpacket.packet+164));
1086 p_stream->f_rate = 10000000.0 /
1087 GetQWLE((oggpacket.packet+164));
1088 p_stream->fmt.video.i_bits_per_pixel =
1089 GetWLE((oggpacket.packet+182));
1090 if( !p_stream->fmt.video.i_bits_per_pixel )
1092 p_stream->fmt.video.i_bits_per_pixel = 24;
1093 p_stream->fmt.video.i_width =
1094 GetDWLE((oggpacket.packet+176));
1095 p_stream->fmt.video.i_height =
1096 GetDWLE((oggpacket.packet+180));
1099 "fps: %f, width:%i; height:%i, bitcount:%i",
1101 p_stream->fmt.video.i_width,
1102 p_stream->fmt.video.i_height,
1103 p_stream->fmt.video.i_bits_per_pixel);
1106 /* Check for audio header (old format) */
1107 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1110 unsigned int i_format_tag;
1112 p_stream->fmt.i_cat = AUDIO_ES;
1114 i_extra_size = GetWLE((oggpacket.packet+140));
1115 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1117 p_stream->fmt.i_extra = i_extra_size;
1118 p_stream->fmt.p_extra = malloc( i_extra_size );
1119 if( p_stream->fmt.p_extra )
1120 memcpy( p_stream->fmt.p_extra,
1121 oggpacket.packet + 142, i_extra_size );
1123 p_stream->fmt.i_extra = 0;
1126 i_format_tag = GetWLE((oggpacket.packet+124));
1127 p_stream->fmt.audio.i_channels =
1128 GetWLE((oggpacket.packet+126));
1129 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1130 GetDWLE((oggpacket.packet+128));
1131 p_stream->fmt.i_bitrate =
1132 GetDWLE((oggpacket.packet+132)) * 8;
1133 p_stream->fmt.audio.i_blockalign =
1134 GetWLE((oggpacket.packet+136));
1135 p_stream->fmt.audio.i_bitspersample =
1136 GetWLE((oggpacket.packet+138));
1138 wf_tag_to_fourcc( i_format_tag,
1139 &p_stream->fmt.i_codec, 0 );
1141 if( p_stream->fmt.i_codec ==
1142 VLC_FOURCC('u','n','d','f') )
1144 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1145 ( i_format_tag >> 8 ) & 0xff,
1146 i_format_tag & 0xff );
1149 msg_Dbg( p_demux, "found audio header of type: %.4s",
1150 (char *)&p_stream->fmt.i_codec );
1151 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1152 "%dbits/sample %dkb/s",
1154 p_stream->fmt.audio.i_channels,
1155 p_stream->fmt.audio.i_rate,
1156 p_stream->fmt.audio.i_bitspersample,
1157 p_stream->fmt.i_bitrate / 1024 );
1162 msg_Dbg( p_demux, "stream %d has an old header "
1163 "but is of an unknown type", p_ogg->i_streams-1 );
1168 else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1169 oggpacket.bytes >= 56+1 )
1171 stream_header_t tmp;
1172 stream_header_t *st = &tmp;
1174 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1175 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1176 st->size = GetDWLE( &oggpacket.packet[1+12] );
1177 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1178 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1179 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1180 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1181 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1183 /* Check for video header (new format) */
1184 if( !strncmp( st->streamtype, "video", 5 ) )
1186 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1187 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1189 p_stream->fmt.i_cat = VIDEO_ES;
1191 /* We need to get rid of the header packet */
1192 ogg_stream_packetout( &p_stream->os, &oggpacket );
1194 p_stream->fmt.i_codec =
1195 VLC_FOURCC( st->subtype[0], st->subtype[1],
1196 st->subtype[2], st->subtype[3] );
1197 msg_Dbg( p_demux, "found video header of type: %.4s",
1198 (char *)&p_stream->fmt.i_codec );
1200 p_stream->fmt.video.i_frame_rate = 10000000;
1201 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1202 if( st->time_unit <= 0 )
1203 st->time_unit = 400000;
1204 p_stream->f_rate = 10000000.0 / st->time_unit;
1205 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1206 p_stream->fmt.video.i_width = st->sh.video.width;
1207 p_stream->fmt.video.i_height = st->sh.video.height;
1210 "fps: %f, width:%i; height:%i, bitcount:%i",
1212 p_stream->fmt.video.i_width,
1213 p_stream->fmt.video.i_height,
1214 p_stream->fmt.video.i_bits_per_pixel );
1216 /* Check for audio header (new format) */
1217 else if( !strncmp( st->streamtype, "audio", 5 ) )
1223 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1224 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1225 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1227 p_stream->fmt.i_cat = AUDIO_ES;
1229 /* We need to get rid of the header packet */
1230 ogg_stream_packetout( &p_stream->os, &oggpacket );
1232 i_extra_size = st->size - 56;
1234 if( i_extra_size > 0 &&
1235 i_extra_size < oggpacket.bytes - 1 - 56 )
1237 p_stream->fmt.i_extra = i_extra_size;
1238 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1239 if( p_stream->fmt.p_extra )
1240 memcpy( p_stream->fmt.p_extra, st + 1,
1241 p_stream->fmt.i_extra );
1243 p_stream->fmt.i_extra = 0;
1246 memcpy( p_buffer, st->subtype, 4 );
1248 i_format_tag = strtol(p_buffer,NULL,16);
1249 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1250 if( st->time_unit <= 0 )
1251 st->time_unit = 10000000;
1252 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1253 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1254 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1255 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1257 wf_tag_to_fourcc( i_format_tag,
1258 &p_stream->fmt.i_codec, 0 );
1260 if( p_stream->fmt.i_codec ==
1261 VLC_FOURCC('u','n','d','f') )
1263 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1264 ( i_format_tag >> 8 ) & 0xff,
1265 i_format_tag & 0xff );
1268 msg_Dbg( p_demux, "found audio header of type: %.4s",
1269 (char *)&p_stream->fmt.i_codec );
1270 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1271 "%dbits/sample %dkb/s",
1273 p_stream->fmt.audio.i_channels,
1274 p_stream->fmt.audio.i_rate,
1275 p_stream->fmt.audio.i_bitspersample,
1276 p_stream->fmt.i_bitrate / 1024 );
1278 /* Check for text (subtitles) header */
1279 else if( !strncmp(st->streamtype, "text", 4) )
1281 /* We need to get rid of the header packet */
1282 ogg_stream_packetout( &p_stream->os, &oggpacket );
1284 msg_Dbg( p_demux, "found text subtitles header" );
1285 p_stream->fmt.i_cat = SPU_ES;
1286 p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1287 p_stream->f_rate = 1000; /* granulepos is in millisec */
1291 msg_Dbg( p_demux, "stream %d has a header marker "
1292 "but is of an unknown type", p_ogg->i_streams-1 );
1297 else if( oggpacket.bytes >= 7 &&
1298 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1302 msg_Dbg( p_demux, "stream %d is a skeleton",
1303 p_ogg->i_streams-1 );
1304 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1308 msg_Dbg( p_demux, "stream %d is of unknown type",
1309 p_ogg->i_streams-1 );
1314 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1315 return VLC_EGENERIC;
1318 /* we'll need to get all headers for all of those streams
1319 that we have to backup headers for */
1321 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1323 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1328 /* This is the first data page, which means we are now finished
1329 * with the initial pages. We just need to store it in the relevant
1331 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1333 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1336 p_ogg->b_page_waiting = true;
1345 return VLC_EGENERIC;
1348 /****************************************************************************
1349 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1350 * Elementary streams.
1351 ****************************************************************************/
1352 static int Ogg_BeginningOfStream( demux_t *p_demux )
1354 demux_sys_t *p_ogg = p_demux->p_sys ;
1355 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1358 /* Find the logical streams embedded in the physical stream and
1359 * initialize our p_ogg structure. */
1360 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1362 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1363 return VLC_EGENERIC;
1366 p_ogg->i_bitrate = 0;
1368 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1370 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1372 p_stream->p_es = NULL;
1374 /* Try first to reuse an old ES */
1376 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1377 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1379 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1381 p_stream->p_es = p_old_stream->p_es;
1382 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1384 p_old_stream->p_es = NULL;
1385 p_old_stream = NULL;
1388 if( !p_stream->p_es )
1390 /* Better be safe than sorry when possible with ogm */
1391 if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1392 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1393 p_stream->fmt.b_packetized = false;
1395 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1398 // TODO: something to do here ?
1399 if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1401 /* Set the CMML stream active */
1402 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1405 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1407 p_stream->i_pcr = p_stream->i_previous_pcr =
1408 p_stream->i_interpolated_pcr = -1;
1409 p_stream->b_reinit = false;
1412 if( p_ogg->p_old_stream )
1414 if( p_ogg->p_old_stream->p_es )
1415 msg_Dbg( p_demux, "old stream not reused" );
1416 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1417 p_ogg->p_old_stream = NULL;
1422 /****************************************************************************
1423 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1424 ****************************************************************************/
1425 static void Ogg_EndOfStream( demux_t *p_demux )
1427 demux_sys_t *p_ogg = p_demux->p_sys ;
1430 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1431 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1432 free( p_ogg->pp_stream );
1435 p_ogg->i_bitrate = 0;
1436 p_ogg->i_streams = 0;
1437 p_ogg->pp_stream = NULL;
1441 vlc_meta_Delete( p_ogg->p_meta );
1442 p_ogg->p_meta = NULL;
1446 * This function delete and release all data associated to a logical_stream_t
1448 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1450 if( p_stream->p_es )
1451 es_out_Del( p_demux->out, p_stream->p_es );
1453 ogg_stream_clear( &p_stream->os );
1454 free( p_stream->p_headers );
1456 es_format_Clean( &p_stream->fmt_old );
1457 es_format_Clean( &p_stream->fmt );
1462 * This function check if a we need to reset a decoder in case we are
1465 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1467 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1468 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1469 unsigned i_new_count;
1470 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1473 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1474 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1475 unsigned i_old_count;
1476 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1479 bool b_match = i_new_count == i_old_count;
1480 for( unsigned i = 0; i < i_new_count && b_match; i++ )
1482 /* Ignore vorbis comment */
1485 if( pi_new_size[i] != pi_old_size[i] ||
1486 memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
1490 for( unsigned i = 0; i < i_new_count; i++ )
1491 free( pp_new_data[i] );
1492 for( unsigned i = 0; i < i_old_count; i++ )
1493 free( pp_old_data[i] );
1496 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1498 bool b_compatible = false;
1499 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1502 /* Only vorbis is supported */
1503 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
1504 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1507 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1509 return !b_compatible;
1511 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const void *p_headers, unsigned i_headers, unsigned i_skip )
1513 demux_sys_t *p_ogg = p_demux->p_sys;
1515 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1516 void *pp_data[XIPH_MAX_HEADER_COUNT];
1518 if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
1521 /* TODO how to handle multiple comments properly ? */
1522 if( i_count >= 2 && pi_size[1] > i_skip )
1523 vorbis_ParseComment( &p_ogg->p_meta, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip );
1525 for( unsigned i = 0; i < i_count; i++ )
1528 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1530 demux_sys_t *p_ogg = p_demux->p_sys;
1534 /* 3 headers with the 2° one being the comments */
1535 case VLC_CODEC_VORBIS:
1536 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1538 case VLC_CODEC_THEORA:
1539 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1541 case VLC_CODEC_SPEEX:
1542 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
1545 /* N headers with the 2° one being the comments */
1546 case VLC_CODEC_KATE:
1547 /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
1548 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1 );
1552 case VLC_CODEC_FLAC:
1553 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1557 case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1558 case VLC_CODEC_DIRAC:
1563 p_demux->info.i_update |= INPUT_UPDATE_META;
1566 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1567 ogg_packet *p_oggpacket )
1570 int i_fps_numerator;
1571 int i_fps_denominator;
1572 int i_keyframe_frequency_force;
1574 p_stream->fmt.i_cat = VIDEO_ES;
1575 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1577 /* Signal that we want to keep a backup of the theora
1578 * stream headers. They will be used when switching between
1580 p_stream->b_force_backup = 1;
1582 /* Cheat and get additionnal info ;) */
1583 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1584 bs_skip( &bitstream, 56 );
1585 bs_read( &bitstream, 8 ); /* major version num */
1586 bs_read( &bitstream, 8 ); /* minor version num */
1587 bs_read( &bitstream, 8 ); /* subminor version num */
1588 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1589 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1590 bs_read( &bitstream, 24 ); /* frame width */
1591 bs_read( &bitstream, 24 ); /* frame height */
1592 bs_read( &bitstream, 8 ); /* x offset */
1593 bs_read( &bitstream, 8 ); /* y offset */
1595 i_fps_numerator = bs_read( &bitstream, 32 );
1596 i_fps_denominator = bs_read( &bitstream, 32 );
1597 bs_read( &bitstream, 24 ); /* aspect_numerator */
1598 bs_read( &bitstream, 24 ); /* aspect_denominator */
1600 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1601 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1603 bs_read( &bitstream, 8 ); /* colorspace */
1604 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1605 bs_read( &bitstream, 6 ); /* quality */
1607 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1609 /* granule_shift = i_log( frequency_force -1 ) */
1610 p_stream->i_granule_shift = 0;
1611 i_keyframe_frequency_force--;
1612 while( i_keyframe_frequency_force )
1614 p_stream->i_granule_shift++;
1615 i_keyframe_frequency_force >>= 1;
1618 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1621 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1622 ogg_packet *p_oggpacket )
1626 p_stream->fmt.i_cat = AUDIO_ES;
1627 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1629 /* Signal that we want to keep a backup of the vorbis
1630 * stream headers. They will be used when switching between
1632 p_stream->b_force_backup = 1;
1634 /* Cheat and get additionnal info ;) */
1635 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1636 oggpack_adv( &opb, 88 );
1637 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1638 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1639 oggpack_read( &opb, 32 );
1640 oggpack_adv( &opb, 32 );
1641 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1644 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1645 ogg_packet *p_oggpacket )
1649 p_stream->fmt.i_cat = AUDIO_ES;
1650 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1652 /* Signal that we want to keep a backup of the speex
1653 * stream headers. They will be used when switching between
1655 p_stream->b_force_backup = 1;
1657 /* Cheat and get additionnal info ;) */
1658 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1659 oggpack_adv( &opb, 224 );
1660 oggpack_adv( &opb, 32 ); /* speex_version_id */
1661 oggpack_adv( &opb, 32 ); /* header_size */
1662 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1663 oggpack_adv( &opb, 32 ); /* mode */
1664 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1665 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1666 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1669 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1670 ogg_packet *p_oggpacket )
1672 /* Parse the STREAMINFO metadata */
1675 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1678 if( bs_read( &s, 7 ) == 0 )
1680 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1683 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1684 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1686 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1687 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1691 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1694 /* Fake this as the last metadata block */
1695 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1699 /* This ain't a STREAMINFO metadata */
1700 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1704 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1705 ogg_packet *p_oggpacket )
1713 p_stream->fmt.i_cat = SPU_ES;
1714 p_stream->fmt.i_codec = VLC_CODEC_KATE;
1716 /* Signal that we want to keep a backup of the kate
1717 * stream headers. They will be used when switching between
1719 p_stream->b_force_backup = 1;
1721 /* Cheat and get additionnal info ;) */
1722 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1723 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1724 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1725 oggpack_adv( &opb, 3*8 );
1726 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1727 oggpack_adv( &opb, 8*8 ); /* reserved */
1728 gnum = oggpack_read( &opb, 32 );
1729 gden = oggpack_read( &opb, 32 );
1730 p_stream->f_rate = (double)gnum/gden;
1732 p_stream->fmt.psz_language = malloc(16);
1733 if( p_stream->fmt.psz_language )
1735 for( n = 0; n < 16; n++ )
1736 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1737 p_stream->fmt.psz_language[15] = 0; /* just in case */
1741 for( n = 0; n < 16; n++ )
1742 oggpack_read(&opb,8);
1744 p_stream->fmt.psz_description = malloc(16);
1745 if( p_stream->fmt.psz_description )
1747 for( n = 0; n < 16; n++ )
1748 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1749 p_stream->fmt.psz_description[15] = 0; /* just in case */
1751 /* Now find a localized user readable description for this category */
1752 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
1755 free( p_stream->fmt.psz_description );
1756 p_stream->fmt.psz_description = psz_desc;
1761 for( n = 0; n < 16; n++ )
1762 oggpack_read(&opb,8);
1766 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1767 logical_stream_t *p_stream,
1768 ogg_packet *p_oggpacket )
1770 if( p_oggpacket->bytes >= 28 &&
1771 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1775 uint16_t major_version;
1776 uint16_t minor_version;
1777 uint64_t timebase_numerator;
1778 uint64_t timebase_denominator;
1780 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1782 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1783 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1784 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1785 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1786 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1787 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1789 else if( p_oggpacket->bytes >= 42 &&
1790 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1792 uint64_t granule_rate_numerator;
1793 uint64_t granule_rate_denominator;
1794 char content_type_string[1024];
1796 /* Read in Annodex header fields */
1798 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1799 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1800 p_stream->i_secondary_header_packets =
1801 GetDWLE( &p_oggpacket->packet[24] );
1803 /* we are guaranteed that the first header field will be
1804 * the content-type (by the Annodex standard) */
1805 content_type_string[0] = '\0';
1806 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1808 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1809 p_oggpacket->bytes - 1 );
1810 if( p && p[0] == '\r' && p[1] == '\n' )
1811 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
1812 content_type_string );
1815 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1816 granule_rate_numerator, granule_rate_denominator,
1817 p_stream->i_secondary_header_packets, content_type_string );
1819 p_stream->f_rate = (float) granule_rate_numerator /
1820 (float) granule_rate_denominator;
1822 /* What type of file do we have?
1823 * strcmp is safe to use here because we've extracted
1824 * content_type_string from the stream manually */
1825 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1827 /* n.b. WAVs are unsupported right now */
1828 p_stream->fmt.i_cat = UNKNOWN_ES;
1830 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1832 p_stream->fmt.i_cat = AUDIO_ES;
1833 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1835 p_stream->b_force_backup = 1;
1837 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1839 p_stream->fmt.i_cat = AUDIO_ES;
1840 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1842 p_stream->b_force_backup = 1;
1844 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1846 p_stream->fmt.i_cat = VIDEO_ES;
1847 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1849 p_stream->b_force_backup = 1;
1851 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1853 p_stream->fmt.i_cat = VIDEO_ES;
1854 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1856 p_stream->b_force_backup = 1;
1858 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1860 /* n.b. MPEG streams are unsupported right now */
1861 p_stream->fmt.i_cat = VIDEO_ES;
1862 p_stream->fmt.i_codec = VLC_CODEC_MPGV;
1864 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1866 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1867 p_stream->fmt.i_cat = SPU_ES;
1868 p_stream->fmt.i_codec = VLC_CODEC_CMML;
1873 static uint32_t dirac_uint( bs_t *p_bs )
1875 uint32_t u_count = 0, u_value = 0;
1877 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
1881 u_value |= bs_read( p_bs, 1 );
1884 return (1<<u_count) - 1 + u_value;
1887 static int dirac_bool( bs_t *p_bs )
1889 return bs_read( p_bs, 1 );
1892 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1893 ogg_packet *p_oggpacket )
1895 static const struct {
1896 uint32_t u_n /* numerator */, u_d /* denominator */;
1897 } p_dirac_frate_tbl[] = { /* table 10.3 */
1898 {1,1}, /* this first value is never used */
1899 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1900 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1902 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
1904 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
1905 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1907 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
1911 p_stream->i_granule_shift = 22; /* not 32 */
1913 /* Backing up stream headers is not required -- seqhdrs are repeated
1914 * thoughout the stream at suitable decoding start points */
1915 p_stream->b_force_backup = 0;
1917 /* read in useful bits from sequence header */
1918 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1919 bs_skip( &bs, 13*8); /* parse_info_header */
1920 dirac_uint( &bs ); /* major_version */
1921 dirac_uint( &bs ); /* minor_version */
1922 dirac_uint( &bs ); /* profile */
1923 dirac_uint( &bs ); /* level */
1925 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1926 if( u_video_format >= u_dirac_vidfmt_frate )
1928 /* don't know how to parse this ogg dirac stream */
1932 if( dirac_bool( &bs ) )
1934 dirac_uint( &bs ); /* frame_width */
1935 dirac_uint( &bs ); /* frame_height */
1938 if( dirac_bool( &bs ) )
1940 dirac_uint( &bs ); /* chroma_format */
1943 if( dirac_bool( &bs ) )
1945 dirac_uint( &bs ); /* scan_format */
1948 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
1949 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
1950 if( dirac_bool( &bs ) )
1952 uint32_t u_frame_rate_index = dirac_uint( &bs );
1953 if( u_frame_rate_index >= u_dirac_frate_tbl )
1955 /* something is wrong with this stream */
1958 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
1959 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
1960 if( u_frame_rate_index == 0 )
1962 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1963 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1966 p_stream->f_rate = (float) u_n / u_d;
1968 /* probably is an ogg dirac es */
1969 p_stream->fmt.i_cat = VIDEO_ES;
1970 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;