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>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_shortname ( "OGG" )
51 set_description( N_("OGG demuxer" ) )
52 set_category( CAT_INPUT )
53 set_subcategory( SUBCAT_INPUT_DEMUX )
54 set_capability( "demux", 50 )
55 set_callbacks( Open, Close )
60 /*****************************************************************************
61 * Definitions of structures and functions used by this plugins
62 *****************************************************************************/
63 typedef struct logical_stream_s
65 ogg_stream_state os; /* logical stream of packets */
68 es_format_t fmt_old; /* format of old ES is reused */
74 /* the header of some logical streams (eg vorbis) contain essential
75 * data for the decoder. We back them up here in case we need to re-feed
76 * them to the decoder. */
82 /* program clock reference (in units of 90kHz) derived from the previous
85 mtime_t i_interpolated_pcr;
86 mtime_t i_previous_pcr;
92 /* kate streams have the number of headers in the ID header */
93 int i_kate_num_headers;
95 /* for Annodex logical bitstreams */
96 int secondary_header_packets;
102 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
104 int i_streams; /* number of logical bitstreams */
105 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
107 logical_stream_t *p_old_stream; /* pointer to a old logical stream to avoid recreating it */
109 /* program clock reference (in units of 90kHz) derived from the pcr of
120 /* after reading all headers, the first data page is stuffed into the relevant stream, ready to use */
124 /* OggDS headers for the new header format (used in ogm files) */
129 } stream_header_video_t;
133 ogg_int16_t channels;
135 ogg_int16_t blockalign;
136 ogg_int32_t avgbytespersec;
137 } stream_header_audio_t;
144 ogg_int32_t size; /* size of the structure */
146 ogg_int64_t time_unit; /* in reference time */
147 ogg_int64_t samples_per_unit;
148 ogg_int32_t default_len; /* in media time */
150 ogg_int32_t buffersize;
151 ogg_int16_t bits_per_sample;
157 stream_header_video_t video;
159 stream_header_audio_t audio;
163 #define OGG_BLOCK_SIZE 4096
165 /* Some defines from OggDS */
166 #define PACKET_TYPE_HEADER 0x01
167 #define PACKET_TYPE_BITS 0x07
168 #define PACKET_LEN_BITS01 0xc0
169 #define PACKET_LEN_BITS2 0x02
170 #define PACKET_IS_SYNCPOINT 0x08
172 /*****************************************************************************
174 *****************************************************************************/
175 static int Demux ( demux_t * );
176 static int Control( demux_t *, int, va_list );
178 /* Bitstream manipulation */
179 static int Ogg_ReadPage ( demux_t *, ogg_page * );
180 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
181 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
183 static int Ogg_BeginningOfStream( demux_t *p_demux );
184 static int Ogg_FindLogicalStreams( demux_t *p_demux );
185 static void Ogg_EndOfStream( demux_t *p_demux );
188 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
189 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
191 /* Logical bitstream headers */
192 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
193 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
194 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
195 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
196 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
197 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
198 static void Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
200 /*****************************************************************************
201 * Open: initializes ogg demux structures
202 *****************************************************************************/
203 static int Open( vlc_object_t * p_this )
205 demux_t *p_demux = (demux_t *)p_this;
207 const uint8_t *p_peek;
210 /* Check if we are dealing with an ogg stream */
211 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
212 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
217 /* Set exported functions */
218 p_demux->pf_demux = Demux;
219 p_demux->pf_control = Control;
220 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
224 memset( p_sys, 0, sizeof( demux_sys_t ) );
225 p_sys->i_bitrate = 0;
226 p_sys->pp_stream = NULL;
227 p_sys->p_old_stream = NULL;
229 /* Begnning of stream, tell the demux to look for elementary streams. */
233 /* Initialize the Ogg physical bitstream parser */
234 ogg_sync_init( &p_sys->oy );
235 p_sys->b_page_waiting = false;
240 /*****************************************************************************
241 * Close: frees unused data
242 *****************************************************************************/
243 static void Close( vlc_object_t *p_this )
245 demux_t *p_demux = (demux_t *)p_this;
246 demux_sys_t *p_sys = p_demux->p_sys ;
248 /* Cleanup the bitstream parser */
249 ogg_sync_clear( &p_sys->oy );
251 Ogg_EndOfStream( p_demux );
253 if( p_sys->p_old_stream )
254 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
259 /*****************************************************************************
260 * Demux: reads and demuxes data packets
261 *****************************************************************************
262 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
263 *****************************************************************************/
264 static int Demux( demux_t * p_demux )
266 demux_sys_t *p_sys = p_demux->p_sys;
268 ogg_packet oggpacket;
272 if( p_sys->i_eos == p_sys->i_streams )
276 msg_Dbg( p_demux, "end of a group of logical streams" );
277 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
278 * only 1 ES is supported (common case for ogg web radio) */
279 if( p_sys->i_streams == 1 )
281 p_sys->p_old_stream = p_sys->pp_stream[0];
282 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
284 Ogg_EndOfStream( p_demux );
288 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
291 msg_Dbg( p_demux, "beginning of a group of logical streams" );
292 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
296 * The first data page of a physical stream is stored in the relevant logical stream
297 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
298 * stream it belongs to if we haven't processed this first page yet. If we do, we
299 * will only process that first page whenever we find the second page for this stream.
300 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
301 * the second page will arrive real quick, this is not fine for Kate, whose second
302 * data page will typically arrive much later.
303 * This means it is now possible to seek right at the start of a stream where the last
304 * logical stream is Kate, without having to wait for the second data page to unblock
305 * the first one, which is the one that triggers the 'no more headers to backup' code.
306 * And, as we all know, seeking without having backed up all headers is bad, since the
307 * codec will fail to initialize if it's missing its headers.
309 if( !p_sys->b_page_waiting)
312 * Demux an ogg page from the stream
314 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
319 /* Test for End of Stream */
320 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
324 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
326 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
328 /* if we've just pulled page, look for the right logical stream */
329 if( !p_sys->b_page_waiting )
331 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
335 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
337 /* Read info from any secondary header packets, if there are any */
338 if( p_stream->secondary_header_packets > 0 )
340 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
341 oggpacket.bytes >= 7 &&
342 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
344 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
345 p_stream->secondary_header_packets = 0;
347 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
348 oggpacket.bytes >= 7 &&
349 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
351 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
352 p_stream->secondary_header_packets = 0;
354 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
356 p_stream->secondary_header_packets = 0;
360 if( p_stream->b_reinit )
362 /* If synchro is re-initialized we need to drop all the packets
363 * until we find a new dated one. */
364 Ogg_UpdatePCR( p_stream, &oggpacket );
366 if( p_stream->i_pcr >= 0 )
368 p_stream->b_reinit = 0;
372 p_stream->i_interpolated_pcr = -1;
376 /* An Ogg/vorbis packet contains an end date granulepos */
377 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
378 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
379 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
381 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
383 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
387 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
394 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
397 if( !p_sys->b_page_waiting )
403 /* if a page was waiting, it's now processed */
404 p_sys->b_page_waiting = false;
406 i_stream = 0; p_sys->i_pcr = -1;
407 for( ; i_stream < p_sys->i_streams; i_stream++ )
409 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
411 if( p_stream->fmt.i_cat == SPU_ES )
413 if( p_stream->i_interpolated_pcr < 0 )
416 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
417 p_sys->i_pcr = p_stream->i_interpolated_pcr;
420 if( p_sys->i_pcr >= 0 )
422 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
428 /*****************************************************************************
430 *****************************************************************************/
431 static int Control( demux_t *p_demux, int i_query, va_list args )
433 demux_sys_t *p_sys = p_demux->p_sys;
440 case DEMUX_HAS_UNSUPPORTED_META:
441 pb_bool = (bool*)va_arg( args, bool* );
446 pi64 = (int64_t*)va_arg( args, int64_t * );
447 *pi64 = p_sys->i_pcr;
453 case DEMUX_SET_POSITION:
454 /* forbid seeking if we haven't initialized all logical bitstreams yet;
455 if we allowed, some headers would not get backed up and decoder init
456 would fail, making that logical stream unusable */
457 if( p_sys->i_bos > 0 )
462 for( i = 0; i < p_sys->i_streams; i++ )
464 logical_stream_t *p_stream = p_sys->pp_stream[i];
466 /* we'll trash all the data until we find the next pcr */
467 p_stream->b_reinit = 1;
468 p_stream->i_pcr = -1;
469 p_stream->i_interpolated_pcr = -1;
470 ogg_stream_reset( &p_stream->os );
472 ogg_sync_reset( &p_sys->oy );
473 /* suspicious lack of break - reading the code, I believe it's intended though */
476 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
481 /****************************************************************************
482 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
483 ****************************************************************************
484 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
485 * are at the end of stream.
486 ****************************************************************************/
487 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
489 demux_sys_t *p_ogg = p_demux->p_sys ;
493 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
495 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
497 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
501 ogg_sync_wrote( &p_ogg->oy, i_read );
507 /****************************************************************************
508 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
510 ****************************************************************************/
511 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
512 ogg_packet *p_oggpacket )
514 /* Convert the granulepos into a pcr */
515 if( p_oggpacket->granulepos >= 0 )
517 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
518 p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
520 ogg_int64_t iframe = p_oggpacket->granulepos >>
521 p_stream->i_granule_shift;
522 ogg_int64_t pframe = p_oggpacket->granulepos -
523 ( iframe << p_stream->i_granule_shift );
525 p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
528 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
530 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
531 /* NB, OggDirac granulepos values are in units of 2*picturerate */
532 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
536 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
540 p_stream->i_interpolated_pcr = p_stream->i_pcr;
544 p_stream->i_pcr = -1;
546 /* no granulepos available, try to interpolate the pcr.
547 * If we can't then don't touch the old value. */
548 if( p_stream->fmt.i_cat == VIDEO_ES )
549 /* 1 frame per packet */
550 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
551 else if( p_stream->fmt.i_bitrate )
552 p_stream->i_interpolated_pcr +=
553 ( p_oggpacket->bytes * INT64_C(1000000) /
554 p_stream->fmt.i_bitrate / 8 );
558 /****************************************************************************
559 * Ogg_DecodePacket: Decode an Ogg packet.
560 ****************************************************************************/
561 static void Ogg_DecodePacket( demux_t *p_demux,
562 logical_stream_t *p_stream,
563 ogg_packet *p_oggpacket )
567 int i_header_len = 0;
568 mtime_t i_pts = -1, i_interpolated_pts;
569 demux_sys_t *p_ogg = p_demux->p_sys;
572 if( !p_oggpacket->bytes )
574 msg_Dbg( p_demux, "discarding 0 sized packet" );
578 if( p_oggpacket->bytes >= 7 &&
579 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
581 /* it's an Annodex packet -- skip it (do nothing) */
584 else if( p_oggpacket->bytes >= 7 &&
585 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
587 /* it's an AnxData packet -- skip it (do nothing) */
591 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
592 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
594 /* Check the ES is selected */
595 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
596 p_stream->p_es, &b_selected );
598 if( p_stream->b_force_backup )
601 bool b_store_size = true;
602 bool b_store_num_headers = false;
604 p_stream->i_packets_backup++;
605 switch( p_stream->fmt.i_codec )
607 case VLC_FOURCC( 'v','o','r','b' ):
608 case VLC_FOURCC( 's','p','x',' ' ):
609 case VLC_FOURCC( 't','h','e','o' ):
610 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
613 case VLC_FOURCC( 'f','l','a','c' ):
614 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
616 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
617 p_stream->b_force_backup = 0;
619 else if( p_stream->fmt.audio.i_rate )
621 p_stream->b_force_backup = 0;
622 if( p_oggpacket->bytes >= 9 )
624 p_oggpacket->packet += 9;
625 p_oggpacket->bytes -= 9;
628 b_store_size = false;
631 case VLC_FOURCC( 'k','a','t','e' ):
632 if( p_stream->i_packets_backup == 1)
633 b_store_num_headers = true;
634 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
638 p_stream->b_force_backup = 0;
642 /* Backup the ogg packet (likely an header packet) */
643 p_stream->p_headers =
644 realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
645 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
646 if( p_stream->p_headers )
648 uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
650 if( b_store_num_headers )
652 /* Kate streams store the number of headers in the first header,
653 so we can't just test for 3 as Vorbis/Theora */
654 *(p_extra++) = p_stream->i_kate_num_headers;
658 *(p_extra++) = p_oggpacket->bytes >> 8;
659 *(p_extra++) = p_oggpacket->bytes & 0xFF;
661 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
662 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
664 if( !p_stream->b_force_backup )
666 /* Last header received, commit changes */
667 free( p_stream->fmt.p_extra );
669 p_stream->fmt.i_extra = p_stream->i_headers;
670 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
671 if( p_stream->fmt.p_extra )
672 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
673 p_stream->i_headers );
675 p_stream->fmt.i_extra = 0;
677 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
678 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
679 p_stream->p_es, &p_stream->fmt );
681 /* we're not at BOS anymore for this logical stream */
687 p_stream->p_headers = p_sav;
690 b_selected = false; /* Discard the header packet */
693 /* Convert the pcr into a pts */
694 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
695 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
696 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
698 if( p_stream->i_pcr >= 0 )
700 /* This is for streams where the granulepos of the header packets
701 * doesn't match these of the data packets (eg. ogg web radios). */
702 if( p_stream->i_previous_pcr == 0 &&
703 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
705 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
707 /* Call the pace control */
708 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
712 p_stream->i_previous_pcr = p_stream->i_pcr;
714 /* The granulepos is the end date of the sample */
715 i_pts = p_stream->i_pcr;
719 /* Convert the granulepos into the next pcr */
720 i_interpolated_pts = p_stream->i_interpolated_pcr;
721 Ogg_UpdatePCR( p_stream, p_oggpacket );
723 /* SPU streams are typically discontinuous, do not mind large gaps */
724 if( p_stream->fmt.i_cat != SPU_ES )
726 if( p_stream->i_pcr >= 0 )
728 /* This is for streams where the granulepos of the header packets
729 * doesn't match these of the data packets (eg. ogg web radios). */
730 if( p_stream->i_previous_pcr == 0 &&
731 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
733 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
735 /* Call the pace control */
736 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
741 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
742 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
743 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
744 p_stream->i_pcr >= 0 )
746 p_stream->i_previous_pcr = p_stream->i_pcr;
748 /* The granulepos is the start date of the sample */
749 i_pts = p_stream->i_pcr;
754 /* This stream isn't currently selected so we don't need to decode it,
755 * but we did need to store its pcr as it might be selected later on */
759 if( p_oggpacket->bytes <= 0 )
762 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
765 if( i_pts == 0 ) i_pts = 1;
766 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
767 else if( i_pts == -1 ) i_pts = 0;
769 if( p_stream->fmt.i_cat == AUDIO_ES )
770 p_block->i_dts = p_block->i_pts = i_pts;
771 else if( p_stream->fmt.i_cat == SPU_ES )
773 p_block->i_dts = p_block->i_pts = i_pts;
774 p_block->i_length = 0;
776 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
777 p_block->i_dts = p_block->i_pts = i_pts;
778 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
780 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
781 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
783 uint64_t u_pnum = dts + delay;
785 p_block->i_dts = p_stream->i_pcr;
787 /* NB, OggDirac granulepos values are in units of 2*picturerate */
788 if( -1 != p_oggpacket->granulepos )
789 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
793 p_block->i_dts = i_pts;
797 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
798 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
799 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
800 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
801 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
802 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
803 p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
804 p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
806 /* We remove the header from the packet */
807 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
808 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
810 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
812 /* But with subtitles we need to retrieve the duration first */
815 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
817 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
819 lenbytes = lenbytes << 8;
820 lenbytes += *(p_oggpacket->packet + i_header_len - i);
823 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
824 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
825 p_oggpacket->packet[i_header_len + 1] != 0 &&
826 p_oggpacket->packet[i_header_len + 1] != '\n' &&
827 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
829 p_block->i_length = (mtime_t)lenbytes * 1000;
834 if( p_block->i_buffer >= (unsigned int)i_header_len )
835 p_block->i_buffer -= i_header_len;
837 p_block->i_buffer = 0;
840 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
842 /* FIXME: the biggest hack I've ever done */
843 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
844 p_block->i_pts, p_block->i_dts );
848 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
849 p_oggpacket->bytes - i_header_len );
851 es_out_Send( p_demux->out, p_stream->p_es, p_block );
854 /****************************************************************************
855 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
856 * stream and fill p_ogg.
857 *****************************************************************************
858 * The initial page of a logical stream is marked as a 'bos' page.
859 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
860 * together and all of the initial pages must appear before any data pages.
862 * On success this function returns VLC_SUCCESS.
863 ****************************************************************************/
864 static int Ogg_FindLogicalStreams( demux_t *p_demux )
866 demux_sys_t *p_ogg = p_demux->p_sys ;
867 ogg_packet oggpacket;
871 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
873 if( ogg_page_bos( &oggpage ) )
876 /* All is wonderful in our fine fine little world.
877 * We found the beginning of our first logical stream. */
878 while( ogg_page_bos( &oggpage ) )
880 logical_stream_t **pp_sav = p_ogg->pp_stream;
881 logical_stream_t *p_stream;
883 p_stream = malloc( sizeof(logical_stream_t) );
887 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
889 memset( p_stream, 0, sizeof(logical_stream_t) );
890 p_stream->p_headers = 0;
891 p_stream->secondary_header_packets = 0;
893 es_format_Init( &p_stream->fmt, 0, 0 );
894 es_format_Init( &p_stream->fmt_old, 0, 0 );
896 /* Setup the logical stream */
897 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
898 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
900 /* Extract the initial header from the first page and verify
901 * the codec type of this Ogg bitstream */
902 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
904 /* error. stream version mismatch perhaps */
905 msg_Err( p_demux, "error reading first page of "
906 "Ogg bitstream data" );
910 /* FIXME: check return value */
911 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
913 /* Check for Vorbis header */
914 if( oggpacket.bytes >= 7 &&
915 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
917 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
918 msg_Dbg( p_demux, "found vorbis header" );
920 /* Check for Speex header */
921 else if( oggpacket.bytes >= 5 &&
922 ! memcmp( oggpacket.packet, "Speex", 5 ) )
924 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
925 msg_Dbg( p_demux, "found speex header, channels: %i, "
926 "rate: %i, bitrate: %i",
927 p_stream->fmt.audio.i_channels,
928 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
930 /* Check for Flac header (< version 1.1.1) */
931 else if( oggpacket.bytes >= 4 &&
932 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
934 msg_Dbg( p_demux, "found FLAC header" );
936 /* Grrrr!!!! Did they really have to put all the
937 * important info in the second header packet!!!
938 * (STREAMINFO metadata is in the following packet) */
939 p_stream->b_force_backup = 1;
941 p_stream->fmt.i_cat = AUDIO_ES;
942 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
944 /* Check for Flac header (>= version 1.1.1) */
945 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
946 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
947 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
949 int i_packets = ((int)oggpacket.packet[7]) << 8 |
951 msg_Dbg( p_demux, "found FLAC header version %i.%i "
952 "(%i header packets)",
953 oggpacket.packet[5], oggpacket.packet[6],
956 p_stream->b_force_backup = 1;
958 p_stream->fmt.i_cat = AUDIO_ES;
959 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
960 oggpacket.packet += 13; oggpacket.bytes -= 13;
961 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
963 /* Check for Theora header */
964 else if( oggpacket.bytes >= 7 &&
965 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
967 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
970 "found theora header, bitrate: %i, rate: %f",
971 p_stream->fmt.i_bitrate, p_stream->f_rate );
973 /* Check for Dirac header */
974 else if( oggpacket.bytes >= 5 &&
975 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
977 Ogg_ReadDiracHeader( p_stream, &oggpacket );
978 msg_Dbg( p_demux, "found dirac header" );
980 /* Check for Tarkin header */
981 else if( oggpacket.bytes >= 7 &&
982 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
986 msg_Dbg( p_demux, "found tarkin header" );
987 p_stream->fmt.i_cat = VIDEO_ES;
988 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
990 /* Cheat and get additionnal info ;) */
991 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
992 oggpack_adv( &opb, 88 );
993 oggpack_adv( &opb, 104 );
994 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
995 p_stream->f_rate = 2; /* FIXME */
997 "found tarkin header, bitrate: %i, rate: %f",
998 p_stream->fmt.i_bitrate, p_stream->f_rate );
1000 /* Check for Annodex header */
1001 else if( oggpacket.bytes >= 7 &&
1002 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1004 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1006 /* kill annodex track */
1010 /* Check for Annodex header */
1011 else if( oggpacket.bytes >= 7 &&
1012 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1014 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1017 /* Check for Kate header */
1018 else if( oggpacket.bytes >= 8 &&
1019 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1021 Ogg_ReadKateHeader( p_stream, &oggpacket );
1022 msg_Dbg( p_demux, "found kate header" );
1024 else if( oggpacket.bytes >= 142 &&
1025 !memcmp( &oggpacket.packet[1],
1026 "Direct Show Samples embedded in Ogg", 35 ))
1028 /* Old header type */
1030 /* Check for video header (old format) */
1031 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1032 oggpacket.bytes >= 184 )
1034 p_stream->fmt.i_cat = VIDEO_ES;
1035 p_stream->fmt.i_codec =
1036 VLC_FOURCC( oggpacket.packet[68],
1037 oggpacket.packet[69],
1038 oggpacket.packet[70],
1039 oggpacket.packet[71] );
1040 msg_Dbg( p_demux, "found video header of type: %.4s",
1041 (char *)&p_stream->fmt.i_codec );
1043 p_stream->fmt.video.i_frame_rate = 10000000;
1044 p_stream->fmt.video.i_frame_rate_base =
1045 GetQWLE((oggpacket.packet+164));
1046 p_stream->f_rate = 10000000.0 /
1047 GetQWLE((oggpacket.packet+164));
1048 p_stream->fmt.video.i_bits_per_pixel =
1049 GetWLE((oggpacket.packet+182));
1050 if( !p_stream->fmt.video.i_bits_per_pixel )
1052 p_stream->fmt.video.i_bits_per_pixel = 24;
1053 p_stream->fmt.video.i_width =
1054 GetDWLE((oggpacket.packet+176));
1055 p_stream->fmt.video.i_height =
1056 GetDWLE((oggpacket.packet+180));
1059 "fps: %f, width:%i; height:%i, bitcount:%i",
1061 p_stream->fmt.video.i_width,
1062 p_stream->fmt.video.i_height,
1063 p_stream->fmt.video.i_bits_per_pixel);
1066 /* Check for audio header (old format) */
1067 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1069 unsigned int i_extra_size;
1070 unsigned int i_format_tag;
1072 p_stream->fmt.i_cat = AUDIO_ES;
1074 i_extra_size = GetWLE((oggpacket.packet+140));
1075 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1077 p_stream->fmt.i_extra = i_extra_size;
1078 p_stream->fmt.p_extra = malloc( i_extra_size );
1079 if( p_stream->fmt.p_extra )
1080 memcpy( p_stream->fmt.p_extra,
1081 oggpacket.packet + 142, i_extra_size );
1083 p_stream->fmt.i_extra = 0;
1086 i_format_tag = GetWLE((oggpacket.packet+124));
1087 p_stream->fmt.audio.i_channels =
1088 GetWLE((oggpacket.packet+126));
1089 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1090 GetDWLE((oggpacket.packet+128));
1091 p_stream->fmt.i_bitrate =
1092 GetDWLE((oggpacket.packet+132)) * 8;
1093 p_stream->fmt.audio.i_blockalign =
1094 GetWLE((oggpacket.packet+136));
1095 p_stream->fmt.audio.i_bitspersample =
1096 GetWLE((oggpacket.packet+138));
1098 wf_tag_to_fourcc( i_format_tag,
1099 &p_stream->fmt.i_codec, 0 );
1101 if( p_stream->fmt.i_codec ==
1102 VLC_FOURCC('u','n','d','f') )
1104 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1105 ( i_format_tag >> 8 ) & 0xff,
1106 i_format_tag & 0xff );
1109 msg_Dbg( p_demux, "found audio header of type: %.4s",
1110 (char *)&p_stream->fmt.i_codec );
1111 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1112 "%dbits/sample %dkb/s",
1114 p_stream->fmt.audio.i_channels,
1115 p_stream->fmt.audio.i_rate,
1116 p_stream->fmt.audio.i_bitspersample,
1117 p_stream->fmt.i_bitrate / 1024 );
1122 msg_Dbg( p_demux, "stream %d has an old header "
1123 "but is of an unknown type", p_ogg->i_streams-1 );
1128 else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1129 oggpacket.bytes >= 56+1 )
1131 stream_header_t tmp;
1132 stream_header_t *st = &tmp;
1134 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1135 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1136 st->size = GetDWLE( &oggpacket.packet[1+12] );
1137 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1138 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1139 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1140 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1141 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1143 /* Check for video header (new format) */
1144 if( !strncmp( st->streamtype, "video", 5 ) )
1146 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1147 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1149 p_stream->fmt.i_cat = VIDEO_ES;
1151 /* We need to get rid of the header packet */
1152 ogg_stream_packetout( &p_stream->os, &oggpacket );
1154 p_stream->fmt.i_codec =
1155 VLC_FOURCC( st->subtype[0], st->subtype[1],
1156 st->subtype[2], st->subtype[3] );
1157 msg_Dbg( p_demux, "found video header of type: %.4s",
1158 (char *)&p_stream->fmt.i_codec );
1160 p_stream->fmt.video.i_frame_rate = 10000000;
1161 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1162 if( st->time_unit <= 0 )
1163 st->time_unit = 400000;
1164 p_stream->f_rate = 10000000.0 / st->time_unit;
1165 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1166 p_stream->fmt.video.i_width = st->sh.video.width;
1167 p_stream->fmt.video.i_height = st->sh.video.height;
1170 "fps: %f, width:%i; height:%i, bitcount:%i",
1172 p_stream->fmt.video.i_width,
1173 p_stream->fmt.video.i_height,
1174 p_stream->fmt.video.i_bits_per_pixel );
1176 /* Check for audio header (new format) */
1177 else if( !strncmp( st->streamtype, "audio", 5 ) )
1180 unsigned int i_extra_size;
1183 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1184 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1185 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1187 p_stream->fmt.i_cat = AUDIO_ES;
1189 /* We need to get rid of the header packet */
1190 ogg_stream_packetout( &p_stream->os, &oggpacket );
1192 i_extra_size = st->size - 56;
1194 if( i_extra_size > 0 &&
1195 i_extra_size < oggpacket.bytes - 1 - 56 )
1197 p_stream->fmt.i_extra = i_extra_size;
1198 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1199 if( p_stream->fmt.p_extra )
1200 memcpy( p_stream->fmt.p_extra, st + 1,
1201 p_stream->fmt.i_extra );
1203 p_stream->fmt.i_extra = 0;
1206 memcpy( p_buffer, st->subtype, 4 );
1208 i_format_tag = strtol(p_buffer,NULL,16);
1209 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1210 if( st->time_unit <= 0 )
1211 st->time_unit = 10000000;
1212 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1213 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1214 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1215 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1217 wf_tag_to_fourcc( i_format_tag,
1218 &p_stream->fmt.i_codec, 0 );
1220 if( p_stream->fmt.i_codec ==
1221 VLC_FOURCC('u','n','d','f') )
1223 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1224 ( i_format_tag >> 8 ) & 0xff,
1225 i_format_tag & 0xff );
1228 msg_Dbg( p_demux, "found audio header of type: %.4s",
1229 (char *)&p_stream->fmt.i_codec );
1230 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1231 "%dbits/sample %dkb/s",
1233 p_stream->fmt.audio.i_channels,
1234 p_stream->fmt.audio.i_rate,
1235 p_stream->fmt.audio.i_bitspersample,
1236 p_stream->fmt.i_bitrate / 1024 );
1238 /* Check for text (subtitles) header */
1239 else if( !strncmp(st->streamtype, "text", 4) )
1241 /* We need to get rid of the header packet */
1242 ogg_stream_packetout( &p_stream->os, &oggpacket );
1244 msg_Dbg( p_demux, "found text subtitles header" );
1245 p_stream->fmt.i_cat = SPU_ES;
1246 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1247 p_stream->f_rate = 1000; /* granulepos is in millisec */
1251 msg_Dbg( p_demux, "stream %d has a header marker "
1252 "but is of an unknown type", p_ogg->i_streams-1 );
1257 else if( oggpacket.bytes >= 7 &&
1258 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1262 msg_Dbg( p_demux, "stream %d is a skeleton",
1263 p_ogg->i_streams-1 );
1264 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1268 msg_Dbg( p_demux, "stream %d is of unknown type",
1269 p_ogg->i_streams-1 );
1274 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1275 return VLC_EGENERIC;
1278 /* we'll need to get all headers for all of those streams
1279 that we have to backup headers for */
1281 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1283 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1288 /* This is the first data page, which means we are now finished
1289 * with the initial pages. We just need to store it in the relevant
1291 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1293 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1296 p_ogg->b_page_waiting = true;
1305 return VLC_EGENERIC;
1308 /****************************************************************************
1309 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1310 * Elementary streams.
1311 ****************************************************************************/
1312 static int Ogg_BeginningOfStream( demux_t *p_demux )
1314 demux_sys_t *p_ogg = p_demux->p_sys ;
1315 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1318 /* Find the logical streams embedded in the physical stream and
1319 * initialize our p_ogg structure. */
1320 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1322 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1323 return VLC_EGENERIC;
1326 p_ogg->i_bitrate = 0;
1328 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1330 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1332 p_stream->p_es = NULL;
1334 /* Try first to reuse an old ES */
1336 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1337 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1339 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1341 p_stream->p_es = p_old_stream->p_es;
1342 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1344 p_old_stream->p_es = NULL;
1345 p_old_stream = NULL;
1348 if( !p_stream->p_es )
1349 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1351 // TODO: something to do here ?
1352 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1354 /* Set the CMML stream active */
1355 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1358 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1360 p_stream->i_pcr = p_stream->i_previous_pcr =
1361 p_stream->i_interpolated_pcr = -1;
1362 p_stream->b_reinit = 0;
1365 if( p_ogg->p_old_stream )
1367 if( p_ogg->p_old_stream->p_es )
1368 msg_Dbg( p_demux, "old stream not reused" );
1369 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1370 p_ogg->p_old_stream = NULL;
1375 /****************************************************************************
1376 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1377 ****************************************************************************/
1378 static void Ogg_EndOfStream( demux_t *p_demux )
1380 demux_sys_t *p_ogg = p_demux->p_sys ;
1383 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1384 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1385 free( p_ogg->pp_stream );
1388 p_ogg->i_bitrate = 0;
1389 p_ogg->i_streams = 0;
1390 p_ogg->pp_stream = NULL;
1394 * This function delete and release all data associated to a logical_stream_t
1396 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1398 if( p_stream->p_es )
1399 es_out_Del( p_demux->out, p_stream->p_es );
1401 ogg_stream_clear( &p_stream->os );
1402 free( p_stream->p_headers );
1404 es_format_Clean( &p_stream->fmt_old );
1405 es_format_Clean( &p_stream->fmt );
1410 * This function check if a we need to reset a decoder in case we are
1413 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1419 for( i = 0; i < 3; i++ )
1421 const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1422 const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1424 if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1427 const int i_new_size = GetWBE( &p_new_extra[0] );
1428 const int i_old_size = GetWBE( &p_old_extra[0] );
1430 if( i != 1 ) /* Ignore vorbis comment */
1432 if( i_new_size != i_old_size )
1434 if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1438 i_new += 2 + i_new_size;
1439 i_old += 2 + i_old_size;
1443 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1445 bool b_compatible = false;
1446 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1449 /* Only vorbis is supported */
1450 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) )
1451 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1454 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1456 return !b_compatible;
1459 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1460 ogg_packet *p_oggpacket )
1463 int i_fps_numerator;
1464 int i_fps_denominator;
1465 int i_keyframe_frequency_force;
1467 p_stream->fmt.i_cat = VIDEO_ES;
1468 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1470 /* Signal that we want to keep a backup of the theora
1471 * stream headers. They will be used when switching between
1473 p_stream->b_force_backup = 1;
1475 /* Cheat and get additionnal info ;) */
1476 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1477 bs_skip( &bitstream, 56 );
1478 bs_read( &bitstream, 8 ); /* major version num */
1479 bs_read( &bitstream, 8 ); /* minor version num */
1480 bs_read( &bitstream, 8 ); /* subminor version num */
1481 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1482 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1483 bs_read( &bitstream, 24 ); /* frame width */
1484 bs_read( &bitstream, 24 ); /* frame height */
1485 bs_read( &bitstream, 8 ); /* x offset */
1486 bs_read( &bitstream, 8 ); /* y offset */
1488 i_fps_numerator = bs_read( &bitstream, 32 );
1489 i_fps_denominator = bs_read( &bitstream, 32 );
1490 bs_read( &bitstream, 24 ); /* aspect_numerator */
1491 bs_read( &bitstream, 24 ); /* aspect_denominator */
1493 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1494 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1496 bs_read( &bitstream, 8 ); /* colorspace */
1497 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1498 bs_read( &bitstream, 6 ); /* quality */
1500 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1502 /* granule_shift = i_log( frequency_force -1 ) */
1503 p_stream->i_granule_shift = 0;
1504 i_keyframe_frequency_force--;
1505 while( i_keyframe_frequency_force )
1507 p_stream->i_granule_shift++;
1508 i_keyframe_frequency_force >>= 1;
1511 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1514 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1515 ogg_packet *p_oggpacket )
1519 p_stream->fmt.i_cat = AUDIO_ES;
1520 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1522 /* Signal that we want to keep a backup of the vorbis
1523 * stream headers. They will be used when switching between
1525 p_stream->b_force_backup = 1;
1527 /* Cheat and get additionnal info ;) */
1528 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1529 oggpack_adv( &opb, 88 );
1530 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1531 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1532 oggpack_read( &opb, 32 );
1533 oggpack_adv( &opb, 32 );
1534 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1537 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1538 ogg_packet *p_oggpacket )
1542 p_stream->fmt.i_cat = AUDIO_ES;
1543 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1545 /* Signal that we want to keep a backup of the speex
1546 * stream headers. They will be used when switching between
1548 p_stream->b_force_backup = 1;
1550 /* Cheat and get additionnal info ;) */
1551 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1552 oggpack_adv( &opb, 224 );
1553 oggpack_adv( &opb, 32 ); /* speex_version_id */
1554 oggpack_adv( &opb, 32 ); /* header_size */
1555 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1556 oggpack_adv( &opb, 32 ); /* mode */
1557 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1558 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1559 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1562 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1563 ogg_packet *p_oggpacket )
1565 /* Parse the STREAMINFO metadata */
1568 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1571 if( bs_read( &s, 7 ) == 0 )
1573 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1576 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1577 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1579 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1580 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1582 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1584 /* Fake this as the last metadata block */
1585 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1589 /* This ain't a STREAMINFO metadata */
1590 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1594 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1595 ogg_packet *p_oggpacket )
1602 p_stream->fmt.i_cat = SPU_ES;
1603 p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1605 /* Signal that we want to keep a backup of the kate
1606 * stream headers. They will be used when switching between
1608 p_stream->b_force_backup = 1;
1610 /* Cheat and get additionnal info ;) */
1611 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1612 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1613 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1614 oggpack_adv( &opb, 3*8 );
1615 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1616 oggpack_adv( &opb, 8*8 ); /* reserved */
1617 gnum = oggpack_read( &opb, 32 );
1618 gden = oggpack_read( &opb, 32 );
1619 p_stream->f_rate = (double)gnum/gden;
1621 p_stream->fmt.psz_language = malloc(16);
1622 if (p_stream->fmt.psz_language)
1625 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1626 p_stream->fmt.psz_language[15] = 0; /* just in case */
1631 oggpack_read(&opb,8);
1633 p_stream->fmt.psz_description = malloc(16);
1634 if (p_stream->fmt.psz_description)
1637 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1638 p_stream->fmt.psz_description[15] = 0; /* just in case */
1643 oggpack_read(&opb,8);
1647 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1648 logical_stream_t *p_stream,
1649 ogg_packet *p_oggpacket )
1651 if( p_oggpacket->bytes >= 28 &&
1652 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1656 uint16_t major_version;
1657 uint16_t minor_version;
1658 uint64_t timebase_numerator;
1659 uint64_t timebase_denominator;
1661 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1663 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1664 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1665 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1666 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1667 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1668 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1670 else if( p_oggpacket->bytes >= 42 &&
1671 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1673 uint64_t granule_rate_numerator;
1674 uint64_t granule_rate_denominator;
1675 char content_type_string[1024];
1677 /* Read in Annodex header fields */
1679 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1680 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1681 p_stream->secondary_header_packets =
1682 GetDWLE( &p_oggpacket->packet[24] );
1684 /* we are guaranteed that the first header field will be
1685 * the content-type (by the Annodex standard) */
1686 content_type_string[0] = '\0';
1687 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1689 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1690 p_oggpacket->bytes - 1 );
1691 if( p && p[0] == '\r' && p[1] == '\n' )
1692 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1693 content_type_string );
1696 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1697 granule_rate_numerator, granule_rate_denominator,
1698 p_stream->secondary_header_packets, content_type_string );
1700 p_stream->f_rate = (float) granule_rate_numerator /
1701 (float) granule_rate_denominator;
1703 /* What type of file do we have?
1704 * strcmp is safe to use here because we've extracted
1705 * content_type_string from the stream manually */
1706 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1708 /* n.b. WAVs are unsupported right now */
1709 p_stream->fmt.i_cat = UNKNOWN_ES;
1711 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1713 p_stream->fmt.i_cat = AUDIO_ES;
1714 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1716 p_stream->b_force_backup = 1;
1718 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1720 p_stream->fmt.i_cat = AUDIO_ES;
1721 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1723 p_stream->b_force_backup = 1;
1725 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1727 p_stream->fmt.i_cat = VIDEO_ES;
1728 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1730 p_stream->b_force_backup = 1;
1732 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1734 p_stream->fmt.i_cat = VIDEO_ES;
1735 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1737 p_stream->b_force_backup = 1;
1739 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1741 /* n.b. MPEG streams are unsupported right now */
1742 p_stream->fmt.i_cat = VIDEO_ES;
1743 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1745 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1747 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1748 p_stream->fmt.i_cat = SPU_ES;
1749 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );
1754 static uint32_t dirac_uint( bs_t *p_bs )
1756 uint32_t count = 0, value = 0;
1757 while( !bs_read ( p_bs, 1 ) ) {
1760 value |= bs_read ( p_bs, 1 );
1763 return (1<<count) - 1 + value;
1766 static int dirac_bool( bs_t *p_bs )
1768 return bs_read ( p_bs, 1 );
1771 static void Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1772 ogg_packet *p_oggpacket )
1776 p_stream->fmt.i_cat = VIDEO_ES;
1777 p_stream->fmt.i_codec = VLC_FOURCC( 'd','r','a','c' );
1778 p_stream->i_granule_shift = 32;
1780 /* Backing up stream headers is not required -- seqhdrs are repeated
1781 * thoughout the stream at suitable decoding start points */
1782 p_stream->b_force_backup = 0;
1784 /* read in useful bits from sequence header */
1785 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1786 bs_skip( &bs, 13*8); /* parse_info_header */
1787 dirac_uint( &bs ); /* major_version */
1788 dirac_uint( &bs ); /* minor_version */
1789 dirac_uint( &bs ); /* profile */
1790 dirac_uint( &bs ); /* level */
1792 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1794 if (dirac_bool( &bs )) {
1795 dirac_uint( &bs ); /* frame_width */
1796 dirac_uint( &bs ); /* frame_height */
1799 if (dirac_bool( &bs )) {
1800 dirac_uint( &bs ); /* chroma_format */
1803 if (dirac_bool( &bs )) {
1804 dirac_uint( &bs ); /* scan_format */
1807 static const struct {
1808 uint32_t u_n /* numerator */, u_d /* denominator */;
1809 } dirac_frate_tbl[] = { /* table 10.3 */
1810 {1,1}, /* this first value is never used */
1811 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1812 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1815 static const uint32_t dirac_vidfmt_frate[] = { /* table C.1 */
1816 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1819 uint32_t u_n = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_n;
1820 uint32_t u_d = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_d;
1821 if (dirac_bool( &bs )) {
1822 uint32_t frame_rate_index = dirac_uint( &bs );
1823 u_n = dirac_frate_tbl[frame_rate_index].u_n;
1824 u_d = dirac_frate_tbl[frame_rate_index].u_d;
1825 if (frame_rate_index == 0) {
1826 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1827 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1830 p_stream->f_rate = (float) u_n / u_d;