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>
42 #include <vlc_charset.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,
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, 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 bool b_store_size = true;
630 bool b_store_num_headers = false;
632 p_stream->i_packets_backup++;
633 switch( p_stream->fmt.i_codec )
635 case VLC_CODEC_VORBIS:
636 case VLC_CODEC_SPEEX:
637 case VLC_CODEC_THEORA:
638 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
642 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
644 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
645 p_stream->b_force_backup = 0;
647 else if( p_stream->fmt.audio.i_rate )
649 p_stream->b_force_backup = 0;
650 if( p_oggpacket->bytes >= 9 )
652 p_oggpacket->packet += 9;
653 p_oggpacket->bytes -= 9;
656 b_store_size = false;
660 if( p_stream->i_packets_backup == 1)
661 b_store_num_headers = true;
662 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
666 p_stream->b_force_backup = 0;
670 /* Backup the ogg packet (likely an header packet) */
671 p_stream->p_headers =
672 realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
673 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
674 if( p_stream->p_headers )
676 uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
678 if( b_store_num_headers )
680 /* Kate streams store the number of headers in the first header,
681 so we can't just test for 3 as Vorbis/Theora */
682 *(p_extra++) = p_stream->i_kate_num_headers;
686 *(p_extra++) = p_oggpacket->bytes >> 8;
687 *(p_extra++) = p_oggpacket->bytes & 0xFF;
689 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
690 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
692 if( !p_stream->b_force_backup )
694 /* Last header received, commit changes */
695 free( p_stream->fmt.p_extra );
697 p_stream->fmt.i_extra = p_stream->i_headers;
698 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
699 if( p_stream->fmt.p_extra )
700 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
701 p_stream->i_headers );
703 p_stream->fmt.i_extra = 0;
705 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
706 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
707 p_stream->p_es, &p_stream->fmt );
709 if( p_stream->i_headers > 0 )
710 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
711 p_stream->p_headers, p_stream->i_headers );
713 /* we're not at BOS anymore for this logical stream */
719 p_stream->p_headers = p_sav;
722 b_selected = false; /* Discard the header packet */
725 /* Convert the pcr into a pts */
726 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
727 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
728 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
730 if( p_stream->i_pcr >= 0 )
732 /* This is for streams where the granulepos of the header packets
733 * doesn't match these of the data packets (eg. ogg web radios). */
734 if( p_stream->i_previous_pcr == 0 &&
735 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
737 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
739 /* Call the pace control */
740 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
744 p_stream->i_previous_pcr = p_stream->i_pcr;
746 /* The granulepos is the end date of the sample */
747 i_pts = p_stream->i_pcr;
751 /* Convert the granulepos into the next pcr */
752 i_interpolated_pts = p_stream->i_interpolated_pcr;
753 Ogg_UpdatePCR( p_stream, p_oggpacket );
755 /* SPU streams are typically discontinuous, do not mind large gaps */
756 if( p_stream->fmt.i_cat != SPU_ES )
758 if( p_stream->i_pcr >= 0 )
760 /* This is for streams where the granulepos of the header packets
761 * doesn't match these of the data packets (eg. ogg web radios). */
762 if( p_stream->i_previous_pcr == 0 &&
763 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
765 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
767 /* Call the pace control */
768 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
773 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
774 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
775 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
776 p_stream->i_pcr >= 0 )
778 p_stream->i_previous_pcr = p_stream->i_pcr;
780 /* The granulepos is the start date of the sample */
781 i_pts = p_stream->i_pcr;
786 /* This stream isn't currently selected so we don't need to decode it,
787 * but we did need to store its pcr as it might be selected later on */
791 if( p_oggpacket->bytes <= 0 )
794 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
797 if( i_pts == 0 ) i_pts = 1;
798 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
799 else if( i_pts == -1 ) i_pts = 0;
801 if( p_stream->fmt.i_cat == AUDIO_ES )
802 p_block->i_dts = p_block->i_pts = i_pts;
803 else if( p_stream->fmt.i_cat == SPU_ES )
805 p_block->i_dts = p_block->i_pts = i_pts;
806 p_block->i_length = 0;
808 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
809 p_block->i_dts = p_block->i_pts = i_pts;
810 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
812 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
813 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
815 uint64_t u_pnum = dts + delay;
817 p_block->i_dts = p_stream->i_pcr;
819 /* NB, OggDirac granulepos values are in units of 2*picturerate */
820 if( -1 != p_oggpacket->granulepos )
821 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
825 p_block->i_dts = i_pts;
829 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
830 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
831 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
832 p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
833 p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
834 p_stream->fmt.i_codec != VLC_CODEC_CMML &&
835 p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
836 p_stream->fmt.i_codec != VLC_CODEC_KATE )
838 /* We remove the header from the packet */
839 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
840 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
842 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
844 /* But with subtitles we need to retrieve the duration first */
847 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
849 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
851 lenbytes = lenbytes << 8;
852 lenbytes += *(p_oggpacket->packet + i_header_len - i);
855 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
856 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
857 p_oggpacket->packet[i_header_len + 1] != 0 &&
858 p_oggpacket->packet[i_header_len + 1] != '\n' &&
859 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
861 p_block->i_length = (mtime_t)lenbytes * 1000;
866 if( p_block->i_buffer >= (unsigned int)i_header_len )
867 p_block->i_buffer -= i_header_len;
869 p_block->i_buffer = 0;
872 if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
874 /* FIXME: the biggest hack I've ever done */
875 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
876 p_block->i_pts, p_block->i_dts );
880 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
881 p_oggpacket->bytes - i_header_len );
883 es_out_Send( p_demux->out, p_stream->p_es, p_block );
886 /****************************************************************************
887 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
888 * stream and fill p_ogg.
889 *****************************************************************************
890 * The initial page of a logical stream is marked as a 'bos' page.
891 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
892 * together and all of the initial pages must appear before any data pages.
894 * On success this function returns VLC_SUCCESS.
895 ****************************************************************************/
896 static int Ogg_FindLogicalStreams( demux_t *p_demux )
898 demux_sys_t *p_ogg = p_demux->p_sys ;
899 ogg_packet oggpacket;
903 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
905 if( ogg_page_bos( &oggpage ) )
908 /* All is wonderful in our fine fine little world.
909 * We found the beginning of our first logical stream. */
910 while( ogg_page_bos( &oggpage ) )
912 logical_stream_t *p_stream;
914 p_stream = malloc( sizeof(logical_stream_t) );
918 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
920 memset( p_stream, 0, sizeof(logical_stream_t) );
921 p_stream->p_headers = 0;
922 p_stream->i_secondary_header_packets = 0;
924 es_format_Init( &p_stream->fmt, 0, 0 );
925 es_format_Init( &p_stream->fmt_old, 0, 0 );
927 /* Setup the logical stream */
928 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
929 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
931 /* Extract the initial header from the first page and verify
932 * the codec type of this Ogg bitstream */
933 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
935 /* error. stream version mismatch perhaps */
936 msg_Err( p_demux, "error reading first page of "
937 "Ogg bitstream data" );
941 /* FIXME: check return value */
942 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
944 /* Check for Vorbis header */
945 if( oggpacket.bytes >= 7 &&
946 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
948 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
949 msg_Dbg( p_demux, "found vorbis header" );
951 /* Check for Speex header */
952 else if( oggpacket.bytes >= 5 &&
953 ! memcmp( oggpacket.packet, "Speex", 5 ) )
955 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
956 msg_Dbg( p_demux, "found speex header, channels: %i, "
957 "rate: %i, bitrate: %i",
958 p_stream->fmt.audio.i_channels,
959 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
961 /* Check for Flac header (< version 1.1.1) */
962 else if( oggpacket.bytes >= 4 &&
963 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
965 msg_Dbg( p_demux, "found FLAC header" );
967 /* Grrrr!!!! Did they really have to put all the
968 * important info in the second header packet!!!
969 * (STREAMINFO metadata is in the following packet) */
970 p_stream->b_force_backup = 1;
972 p_stream->fmt.i_cat = AUDIO_ES;
973 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
975 /* Check for Flac header (>= version 1.1.1) */
976 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
977 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
978 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
980 int i_packets = ((int)oggpacket.packet[7]) << 8 |
982 msg_Dbg( p_demux, "found FLAC header version %i.%i "
983 "(%i header packets)",
984 oggpacket.packet[5], oggpacket.packet[6],
987 p_stream->b_force_backup = 1;
989 p_stream->fmt.i_cat = AUDIO_ES;
990 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
991 oggpacket.packet += 13; oggpacket.bytes -= 13;
992 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
994 /* Check for Theora header */
995 else if( oggpacket.bytes >= 7 &&
996 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
998 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
1001 "found theora header, bitrate: %i, rate: %f",
1002 p_stream->fmt.i_bitrate, p_stream->f_rate );
1004 /* Check for Dirac header */
1005 else if( oggpacket.bytes >= 5 &&
1006 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
1008 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1009 msg_Dbg( p_demux, "found dirac header" );
1012 msg_Warn( p_demux, "found dirac header isn't decodable" );
1017 /* Check for Tarkin header */
1018 else if( oggpacket.bytes >= 7 &&
1019 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1023 msg_Dbg( p_demux, "found tarkin header" );
1024 p_stream->fmt.i_cat = VIDEO_ES;
1025 p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1027 /* Cheat and get additionnal info ;) */
1028 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1029 oggpack_adv( &opb, 88 );
1030 oggpack_adv( &opb, 104 );
1031 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1032 p_stream->f_rate = 2; /* FIXME */
1034 "found tarkin header, bitrate: %i, rate: %f",
1035 p_stream->fmt.i_bitrate, p_stream->f_rate );
1037 /* Check for Annodex header */
1038 else if( oggpacket.bytes >= 7 &&
1039 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1041 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1043 /* kill annodex track */
1047 /* Check for Annodex header */
1048 else if( oggpacket.bytes >= 7 &&
1049 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1051 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1054 /* Check for Kate header */
1055 else if( oggpacket.bytes >= 8 &&
1056 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1058 Ogg_ReadKateHeader( p_stream, &oggpacket );
1059 msg_Dbg( p_demux, "found kate header" );
1061 else if( oggpacket.bytes >= 142 &&
1062 !memcmp( &oggpacket.packet[1],
1063 "Direct Show Samples embedded in Ogg", 35 ))
1065 /* Old header type */
1067 /* Check for video header (old format) */
1068 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1069 oggpacket.bytes >= 184 )
1071 p_stream->fmt.i_cat = VIDEO_ES;
1072 p_stream->fmt.i_codec =
1073 VLC_FOURCC( oggpacket.packet[68],
1074 oggpacket.packet[69],
1075 oggpacket.packet[70],
1076 oggpacket.packet[71] );
1077 msg_Dbg( p_demux, "found video header of type: %.4s",
1078 (char *)&p_stream->fmt.i_codec );
1080 p_stream->fmt.video.i_frame_rate = 10000000;
1081 p_stream->fmt.video.i_frame_rate_base =
1082 GetQWLE((oggpacket.packet+164));
1083 p_stream->f_rate = 10000000.0 /
1084 GetQWLE((oggpacket.packet+164));
1085 p_stream->fmt.video.i_bits_per_pixel =
1086 GetWLE((oggpacket.packet+182));
1087 if( !p_stream->fmt.video.i_bits_per_pixel )
1089 p_stream->fmt.video.i_bits_per_pixel = 24;
1090 p_stream->fmt.video.i_width =
1091 GetDWLE((oggpacket.packet+176));
1092 p_stream->fmt.video.i_height =
1093 GetDWLE((oggpacket.packet+180));
1096 "fps: %f, width:%i; height:%i, bitcount:%i",
1098 p_stream->fmt.video.i_width,
1099 p_stream->fmt.video.i_height,
1100 p_stream->fmt.video.i_bits_per_pixel);
1103 /* Check for audio header (old format) */
1104 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1106 unsigned int i_extra_size;
1107 unsigned int i_format_tag;
1109 p_stream->fmt.i_cat = AUDIO_ES;
1111 i_extra_size = GetWLE((oggpacket.packet+140));
1112 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1114 p_stream->fmt.i_extra = i_extra_size;
1115 p_stream->fmt.p_extra = malloc( i_extra_size );
1116 if( p_stream->fmt.p_extra )
1117 memcpy( p_stream->fmt.p_extra,
1118 oggpacket.packet + 142, i_extra_size );
1120 p_stream->fmt.i_extra = 0;
1123 i_format_tag = GetWLE((oggpacket.packet+124));
1124 p_stream->fmt.audio.i_channels =
1125 GetWLE((oggpacket.packet+126));
1126 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1127 GetDWLE((oggpacket.packet+128));
1128 p_stream->fmt.i_bitrate =
1129 GetDWLE((oggpacket.packet+132)) * 8;
1130 p_stream->fmt.audio.i_blockalign =
1131 GetWLE((oggpacket.packet+136));
1132 p_stream->fmt.audio.i_bitspersample =
1133 GetWLE((oggpacket.packet+138));
1135 wf_tag_to_fourcc( i_format_tag,
1136 &p_stream->fmt.i_codec, 0 );
1138 if( p_stream->fmt.i_codec ==
1139 VLC_FOURCC('u','n','d','f') )
1141 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1142 ( i_format_tag >> 8 ) & 0xff,
1143 i_format_tag & 0xff );
1146 msg_Dbg( p_demux, "found audio header of type: %.4s",
1147 (char *)&p_stream->fmt.i_codec );
1148 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1149 "%dbits/sample %dkb/s",
1151 p_stream->fmt.audio.i_channels,
1152 p_stream->fmt.audio.i_rate,
1153 p_stream->fmt.audio.i_bitspersample,
1154 p_stream->fmt.i_bitrate / 1024 );
1159 msg_Dbg( p_demux, "stream %d has an old header "
1160 "but is of an unknown type", p_ogg->i_streams-1 );
1165 else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1166 oggpacket.bytes >= 56+1 )
1168 stream_header_t tmp;
1169 stream_header_t *st = &tmp;
1171 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1172 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1173 st->size = GetDWLE( &oggpacket.packet[1+12] );
1174 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1175 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1176 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1177 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1178 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1180 /* Check for video header (new format) */
1181 if( !strncmp( st->streamtype, "video", 5 ) )
1183 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1184 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1186 p_stream->fmt.i_cat = VIDEO_ES;
1188 /* We need to get rid of the header packet */
1189 ogg_stream_packetout( &p_stream->os, &oggpacket );
1191 p_stream->fmt.i_codec =
1192 VLC_FOURCC( st->subtype[0], st->subtype[1],
1193 st->subtype[2], st->subtype[3] );
1194 msg_Dbg( p_demux, "found video header of type: %.4s",
1195 (char *)&p_stream->fmt.i_codec );
1197 p_stream->fmt.video.i_frame_rate = 10000000;
1198 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1199 if( st->time_unit <= 0 )
1200 st->time_unit = 400000;
1201 p_stream->f_rate = 10000000.0 / st->time_unit;
1202 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1203 p_stream->fmt.video.i_width = st->sh.video.width;
1204 p_stream->fmt.video.i_height = st->sh.video.height;
1207 "fps: %f, width:%i; height:%i, bitcount:%i",
1209 p_stream->fmt.video.i_width,
1210 p_stream->fmt.video.i_height,
1211 p_stream->fmt.video.i_bits_per_pixel );
1213 /* Check for audio header (new format) */
1214 else if( !strncmp( st->streamtype, "audio", 5 ) )
1217 unsigned int i_extra_size;
1220 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1221 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1222 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1224 p_stream->fmt.i_cat = AUDIO_ES;
1226 /* We need to get rid of the header packet */
1227 ogg_stream_packetout( &p_stream->os, &oggpacket );
1229 i_extra_size = st->size - 56;
1231 if( i_extra_size > 0 &&
1232 i_extra_size < oggpacket.bytes - 1 - 56 )
1234 p_stream->fmt.i_extra = i_extra_size;
1235 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1236 if( p_stream->fmt.p_extra )
1237 memcpy( p_stream->fmt.p_extra, st + 1,
1238 p_stream->fmt.i_extra );
1240 p_stream->fmt.i_extra = 0;
1243 memcpy( p_buffer, st->subtype, 4 );
1245 i_format_tag = strtol(p_buffer,NULL,16);
1246 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1247 if( st->time_unit <= 0 )
1248 st->time_unit = 10000000;
1249 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1250 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1251 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1252 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1254 wf_tag_to_fourcc( i_format_tag,
1255 &p_stream->fmt.i_codec, 0 );
1257 if( p_stream->fmt.i_codec ==
1258 VLC_FOURCC('u','n','d','f') )
1260 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1261 ( i_format_tag >> 8 ) & 0xff,
1262 i_format_tag & 0xff );
1265 msg_Dbg( p_demux, "found audio header of type: %.4s",
1266 (char *)&p_stream->fmt.i_codec );
1267 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1268 "%dbits/sample %dkb/s",
1270 p_stream->fmt.audio.i_channels,
1271 p_stream->fmt.audio.i_rate,
1272 p_stream->fmt.audio.i_bitspersample,
1273 p_stream->fmt.i_bitrate / 1024 );
1275 /* Check for text (subtitles) header */
1276 else if( !strncmp(st->streamtype, "text", 4) )
1278 /* We need to get rid of the header packet */
1279 ogg_stream_packetout( &p_stream->os, &oggpacket );
1281 msg_Dbg( p_demux, "found text subtitles header" );
1282 p_stream->fmt.i_cat = SPU_ES;
1283 p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1284 p_stream->f_rate = 1000; /* granulepos is in millisec */
1288 msg_Dbg( p_demux, "stream %d has a header marker "
1289 "but is of an unknown type", p_ogg->i_streams-1 );
1294 else if( oggpacket.bytes >= 7 &&
1295 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1299 msg_Dbg( p_demux, "stream %d is a skeleton",
1300 p_ogg->i_streams-1 );
1301 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1305 msg_Dbg( p_demux, "stream %d is of unknown type",
1306 p_ogg->i_streams-1 );
1311 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1312 return VLC_EGENERIC;
1315 /* we'll need to get all headers for all of those streams
1316 that we have to backup headers for */
1318 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1320 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1325 /* This is the first data page, which means we are now finished
1326 * with the initial pages. We just need to store it in the relevant
1328 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1330 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1333 p_ogg->b_page_waiting = true;
1342 return VLC_EGENERIC;
1345 /****************************************************************************
1346 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1347 * Elementary streams.
1348 ****************************************************************************/
1349 static int Ogg_BeginningOfStream( demux_t *p_demux )
1351 demux_sys_t *p_ogg = p_demux->p_sys ;
1352 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1355 /* Find the logical streams embedded in the physical stream and
1356 * initialize our p_ogg structure. */
1357 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1359 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1360 return VLC_EGENERIC;
1363 p_ogg->i_bitrate = 0;
1365 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1367 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1369 p_stream->p_es = NULL;
1371 /* Try first to reuse an old ES */
1373 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1374 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1376 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1378 p_stream->p_es = p_old_stream->p_es;
1379 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1381 p_old_stream->p_es = NULL;
1382 p_old_stream = NULL;
1385 if( !p_stream->p_es )
1387 /* Better be safe than sorry when possible with ogm */
1388 if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1389 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1390 p_stream->fmt.b_packetized = false;
1392 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1395 // TODO: something to do here ?
1396 if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1398 /* Set the CMML stream active */
1399 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1402 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1404 p_stream->i_pcr = p_stream->i_previous_pcr =
1405 p_stream->i_interpolated_pcr = -1;
1406 p_stream->b_reinit = false;
1409 if( p_ogg->p_old_stream )
1411 if( p_ogg->p_old_stream->p_es )
1412 msg_Dbg( p_demux, "old stream not reused" );
1413 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1414 p_ogg->p_old_stream = NULL;
1419 /****************************************************************************
1420 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1421 ****************************************************************************/
1422 static void Ogg_EndOfStream( demux_t *p_demux )
1424 demux_sys_t *p_ogg = p_demux->p_sys ;
1427 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1428 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1429 free( p_ogg->pp_stream );
1432 p_ogg->i_bitrate = 0;
1433 p_ogg->i_streams = 0;
1434 p_ogg->pp_stream = NULL;
1438 vlc_meta_Delete( p_ogg->p_meta );
1439 p_ogg->p_meta = NULL;
1443 * This function delete and release all data associated to a logical_stream_t
1445 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1447 if( p_stream->p_es )
1448 es_out_Del( p_demux->out, p_stream->p_es );
1450 ogg_stream_clear( &p_stream->os );
1451 free( p_stream->p_headers );
1453 es_format_Clean( &p_stream->fmt_old );
1454 es_format_Clean( &p_stream->fmt );
1459 * This function check if a we need to reset a decoder in case we are
1462 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1468 for( i = 0; i < 3; i++ )
1470 const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1471 const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1473 if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1476 const int i_new_size = GetWBE( &p_new_extra[0] );
1477 const int i_old_size = GetWBE( &p_old_extra[0] );
1479 if( i != 1 ) /* Ignore vorbis comment */
1481 if( i_new_size != i_old_size )
1483 if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1487 i_new += 2 + i_new_size;
1488 i_old += 2 + i_old_size;
1492 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1494 bool b_compatible = false;
1495 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1498 /* Only vorbis is supported */
1499 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
1500 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1503 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1505 return !b_compatible;
1507 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const uint8_t *p_headers, int i_headers, int i_skip, bool b_has_num_headers )
1509 demux_sys_t *p_ogg = p_demux->p_sys;
1511 if (b_has_num_headers)
1515 /* number of headers on a byte, we're interested in the second header, so should be at least 2 to go on */
1516 if (*p_headers++ < 2)
1521 if( i_headers <= 2 )
1524 /* Skip first packet */
1525 const int i_tmp = GetWBE( &p_headers[0] );
1526 if( i_tmp > i_headers-2 )
1528 p_headers += 2 + i_tmp;
1529 i_headers -= 2 + i_tmp;
1531 if( i_headers <= 2 )
1535 int i_comment = GetWBE( &p_headers[0] );
1536 const uint8_t *p_comment = &p_headers[2];
1537 if( i_comment > i_headers - 2 )
1540 if( i_comment <= i_skip )
1543 /* TODO how to handle multiple comments properly ? */
1544 vorbis_ParseComment( &p_ogg->p_meta, &p_comment[i_skip], i_comment - i_skip );
1546 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1548 demux_sys_t *p_ogg = p_demux->p_sys;
1552 /* 3 headers with the 2° one being the comments */
1553 case VLC_CODEC_VORBIS:
1554 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6, false );
1556 case VLC_CODEC_THEORA:
1557 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6, false );
1559 case VLC_CODEC_SPEEX:
1560 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0, false );
1563 /* N headers with the 2° one being the comments */
1564 case VLC_CODEC_KATE:
1565 /* 1 byte for header type, 7 bit for magic, 1 reserved zero byte */
1566 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1, true );
1570 case VLC_CODEC_FLAC:
1571 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1575 case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1576 case VLC_CODEC_DIRAC:
1581 p_demux->info.i_update |= INPUT_UPDATE_META;
1584 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1585 ogg_packet *p_oggpacket )
1588 int i_fps_numerator;
1589 int i_fps_denominator;
1590 int i_keyframe_frequency_force;
1592 p_stream->fmt.i_cat = VIDEO_ES;
1593 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1595 /* Signal that we want to keep a backup of the theora
1596 * stream headers. They will be used when switching between
1598 p_stream->b_force_backup = 1;
1600 /* Cheat and get additionnal info ;) */
1601 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1602 bs_skip( &bitstream, 56 );
1603 bs_read( &bitstream, 8 ); /* major version num */
1604 bs_read( &bitstream, 8 ); /* minor version num */
1605 bs_read( &bitstream, 8 ); /* subminor version num */
1606 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1607 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1608 bs_read( &bitstream, 24 ); /* frame width */
1609 bs_read( &bitstream, 24 ); /* frame height */
1610 bs_read( &bitstream, 8 ); /* x offset */
1611 bs_read( &bitstream, 8 ); /* y offset */
1613 i_fps_numerator = bs_read( &bitstream, 32 );
1614 i_fps_denominator = bs_read( &bitstream, 32 );
1615 bs_read( &bitstream, 24 ); /* aspect_numerator */
1616 bs_read( &bitstream, 24 ); /* aspect_denominator */
1618 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1619 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1621 bs_read( &bitstream, 8 ); /* colorspace */
1622 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1623 bs_read( &bitstream, 6 ); /* quality */
1625 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1627 /* granule_shift = i_log( frequency_force -1 ) */
1628 p_stream->i_granule_shift = 0;
1629 i_keyframe_frequency_force--;
1630 while( i_keyframe_frequency_force )
1632 p_stream->i_granule_shift++;
1633 i_keyframe_frequency_force >>= 1;
1636 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1639 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1640 ogg_packet *p_oggpacket )
1644 p_stream->fmt.i_cat = AUDIO_ES;
1645 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1647 /* Signal that we want to keep a backup of the vorbis
1648 * stream headers. They will be used when switching between
1650 p_stream->b_force_backup = 1;
1652 /* Cheat and get additionnal info ;) */
1653 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1654 oggpack_adv( &opb, 88 );
1655 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1656 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1657 oggpack_read( &opb, 32 );
1658 oggpack_adv( &opb, 32 );
1659 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1662 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1663 ogg_packet *p_oggpacket )
1667 p_stream->fmt.i_cat = AUDIO_ES;
1668 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1670 /* Signal that we want to keep a backup of the speex
1671 * stream headers. They will be used when switching between
1673 p_stream->b_force_backup = 1;
1675 /* Cheat and get additionnal info ;) */
1676 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1677 oggpack_adv( &opb, 224 );
1678 oggpack_adv( &opb, 32 ); /* speex_version_id */
1679 oggpack_adv( &opb, 32 ); /* header_size */
1680 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1681 oggpack_adv( &opb, 32 ); /* mode */
1682 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1683 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1684 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1687 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1688 ogg_packet *p_oggpacket )
1690 /* Parse the STREAMINFO metadata */
1693 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1696 if( bs_read( &s, 7 ) == 0 )
1698 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1701 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1702 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1704 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1705 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1709 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1712 /* Fake this as the last metadata block */
1713 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1717 /* This ain't a STREAMINFO metadata */
1718 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1722 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1723 ogg_packet *p_oggpacket )
1731 p_stream->fmt.i_cat = SPU_ES;
1732 p_stream->fmt.i_codec = VLC_CODEC_KATE;
1734 /* Signal that we want to keep a backup of the kate
1735 * stream headers. They will be used when switching between
1737 p_stream->b_force_backup = 1;
1739 /* Cheat and get additionnal info ;) */
1740 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1741 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1742 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1743 oggpack_adv( &opb, 3*8 );
1744 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1745 oggpack_adv( &opb, 8*8 ); /* reserved */
1746 gnum = oggpack_read( &opb, 32 );
1747 gden = oggpack_read( &opb, 32 );
1748 p_stream->f_rate = (double)gnum/gden;
1750 p_stream->fmt.psz_language = malloc(16);
1751 if( p_stream->fmt.psz_language )
1753 for( n = 0; n < 16; n++ )
1754 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1755 p_stream->fmt.psz_language[15] = 0; /* just in case */
1759 for( n = 0; n < 16; n++ )
1760 oggpack_read(&opb,8);
1762 p_stream->fmt.psz_description = malloc(16);
1763 if( p_stream->fmt.psz_description )
1765 for( n = 0; n < 16; n++ )
1766 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1767 p_stream->fmt.psz_description[15] = 0; /* just in case */
1769 /* Now find a localized user readable description for this category */
1770 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
1773 free( p_stream->fmt.psz_description );
1774 p_stream->fmt.psz_description = psz_desc;
1779 for( n = 0; n < 16; n++ )
1780 oggpack_read(&opb,8);
1784 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1785 logical_stream_t *p_stream,
1786 ogg_packet *p_oggpacket )
1788 if( p_oggpacket->bytes >= 28 &&
1789 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1793 uint16_t major_version;
1794 uint16_t minor_version;
1795 uint64_t timebase_numerator;
1796 uint64_t timebase_denominator;
1798 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1800 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1801 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1802 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1803 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1804 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1805 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1807 else if( p_oggpacket->bytes >= 42 &&
1808 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1810 uint64_t granule_rate_numerator;
1811 uint64_t granule_rate_denominator;
1812 char content_type_string[1024];
1814 /* Read in Annodex header fields */
1816 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1817 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1818 p_stream->i_secondary_header_packets =
1819 GetDWLE( &p_oggpacket->packet[24] );
1821 /* we are guaranteed that the first header field will be
1822 * the content-type (by the Annodex standard) */
1823 content_type_string[0] = '\0';
1824 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1826 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1827 p_oggpacket->bytes - 1 );
1828 if( p && p[0] == '\r' && p[1] == '\n' )
1829 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1830 content_type_string );
1833 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1834 granule_rate_numerator, granule_rate_denominator,
1835 p_stream->i_secondary_header_packets, content_type_string );
1837 p_stream->f_rate = (float) granule_rate_numerator /
1838 (float) granule_rate_denominator;
1840 /* What type of file do we have?
1841 * strcmp is safe to use here because we've extracted
1842 * content_type_string from the stream manually */
1843 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1845 /* n.b. WAVs are unsupported right now */
1846 p_stream->fmt.i_cat = UNKNOWN_ES;
1848 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1850 p_stream->fmt.i_cat = AUDIO_ES;
1851 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1853 p_stream->b_force_backup = 1;
1855 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1857 p_stream->fmt.i_cat = AUDIO_ES;
1858 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1860 p_stream->b_force_backup = 1;
1862 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1864 p_stream->fmt.i_cat = VIDEO_ES;
1865 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1867 p_stream->b_force_backup = 1;
1869 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1871 p_stream->fmt.i_cat = VIDEO_ES;
1872 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1874 p_stream->b_force_backup = 1;
1876 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1878 /* n.b. MPEG streams are unsupported right now */
1879 p_stream->fmt.i_cat = VIDEO_ES;
1880 p_stream->fmt.i_codec = VLC_CODEC_MPGV;
1882 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1884 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1885 p_stream->fmt.i_cat = SPU_ES;
1886 p_stream->fmt.i_codec = VLC_CODEC_CMML;
1891 static uint32_t dirac_uint( bs_t *p_bs )
1893 uint32_t u_count = 0, u_value = 0;
1895 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
1899 u_value |= bs_read( p_bs, 1 );
1902 return (1<<u_count) - 1 + u_value;
1905 static int dirac_bool( bs_t *p_bs )
1907 return bs_read( p_bs, 1 );
1910 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1911 ogg_packet *p_oggpacket )
1913 static const struct {
1914 uint32_t u_n /* numerator */, u_d /* denominator */;
1915 } p_dirac_frate_tbl[] = { /* table 10.3 */
1916 {1,1}, /* this first value is never used */
1917 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1918 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1920 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
1922 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
1923 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1925 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
1929 p_stream->i_granule_shift = 22; /* not 32 */
1931 /* Backing up stream headers is not required -- seqhdrs are repeated
1932 * thoughout the stream at suitable decoding start points */
1933 p_stream->b_force_backup = 0;
1935 /* read in useful bits from sequence header */
1936 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1937 bs_skip( &bs, 13*8); /* parse_info_header */
1938 dirac_uint( &bs ); /* major_version */
1939 dirac_uint( &bs ); /* minor_version */
1940 dirac_uint( &bs ); /* profile */
1941 dirac_uint( &bs ); /* level */
1943 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1944 if( u_video_format >= u_dirac_vidfmt_frate )
1946 /* don't know how to parse this ogg dirac stream */
1950 if( dirac_bool( &bs ) )
1952 dirac_uint( &bs ); /* frame_width */
1953 dirac_uint( &bs ); /* frame_height */
1956 if( dirac_bool( &bs ) )
1958 dirac_uint( &bs ); /* chroma_format */
1961 if( dirac_bool( &bs ) )
1963 dirac_uint( &bs ); /* scan_format */
1966 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
1967 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
1968 if( dirac_bool( &bs ) )
1970 uint32_t u_frame_rate_index = dirac_uint( &bs );
1971 if( u_frame_rate_index >= u_dirac_frate_tbl )
1973 /* something is wrong with this stream */
1976 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
1977 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
1978 if( u_frame_rate_index == 0 )
1980 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1981 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1984 p_stream->f_rate = (float) u_n / u_d;
1986 /* probably is an ogg dirac es */
1987 p_stream->fmt.i_cat = VIDEO_ES;
1988 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;