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 i_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 )
317 /* Test for End of Stream */
318 if( ogg_page_eos( &oggpage ) )
323 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
325 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
327 /* if we've just pulled page, look for the right logical stream */
328 if( !p_sys->b_page_waiting )
330 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
334 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
336 /* Read info from any secondary header packets, if there are any */
337 if( p_stream->i_secondary_header_packets > 0 )
339 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
340 oggpacket.bytes >= 7 &&
341 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
343 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
344 p_stream->i_secondary_header_packets = 0;
346 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
347 oggpacket.bytes >= 7 &&
348 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
350 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
351 p_stream->i_secondary_header_packets = 0;
353 else if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
355 p_stream->i_secondary_header_packets = 0;
359 if( p_stream->b_reinit )
361 /* If synchro is re-initialized we need to drop all the packets
362 * until we find a new dated one. */
363 Ogg_UpdatePCR( p_stream, &oggpacket );
365 if( p_stream->i_pcr >= 0 )
367 p_stream->b_reinit = false;
371 p_stream->i_interpolated_pcr = -1;
375 /* An Ogg/vorbis packet contains an end date granulepos */
376 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
377 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
378 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
380 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
382 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
386 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
393 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
396 if( !p_sys->b_page_waiting )
400 /* if a page was waiting, it's now processed */
401 p_sys->b_page_waiting = false;
404 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
406 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
408 if( p_stream->fmt.i_cat == SPU_ES )
410 if( p_stream->i_interpolated_pcr < 0 )
413 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
414 p_sys->i_pcr = p_stream->i_interpolated_pcr;
417 if( p_sys->i_pcr >= 0 )
418 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
423 /*****************************************************************************
425 *****************************************************************************/
426 static int Control( demux_t *p_demux, int i_query, va_list args )
428 demux_sys_t *p_sys = p_demux->p_sys;
435 case DEMUX_HAS_UNSUPPORTED_META:
436 pb_bool = (bool*)va_arg( args, bool* );
441 pi64 = (int64_t*)va_arg( args, int64_t * );
442 *pi64 = p_sys->i_pcr;
448 case DEMUX_SET_POSITION:
449 /* forbid seeking if we haven't initialized all logical bitstreams yet;
450 if we allowed, some headers would not get backed up and decoder init
451 would fail, making that logical stream unusable */
452 if( p_sys->i_bos > 0 )
457 for( i = 0; i < p_sys->i_streams; i++ )
459 logical_stream_t *p_stream = p_sys->pp_stream[i];
461 /* we'll trash all the data until we find the next pcr */
462 p_stream->b_reinit = true;
463 p_stream->i_pcr = -1;
464 p_stream->i_interpolated_pcr = -1;
465 ogg_stream_reset( &p_stream->os );
467 ogg_sync_reset( &p_sys->oy );
468 /* XXX The break/return is missing on purpose as
469 * demux_vaControlHelper will do the last part of the job */
472 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
477 /****************************************************************************
478 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
479 ****************************************************************************
480 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
481 * are at the end of stream.
482 ****************************************************************************/
483 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
485 demux_sys_t *p_ogg = p_demux->p_sys ;
489 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
491 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
493 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
497 ogg_sync_wrote( &p_ogg->oy, i_read );
503 /****************************************************************************
504 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
506 ****************************************************************************/
507 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
508 ogg_packet *p_oggpacket )
510 /* Convert the granulepos into a pcr */
511 if( p_oggpacket->granulepos >= 0 )
513 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
514 p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
516 ogg_int64_t iframe = p_oggpacket->granulepos >>
517 p_stream->i_granule_shift;
518 ogg_int64_t pframe = p_oggpacket->granulepos -
519 ( iframe << p_stream->i_granule_shift );
521 p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
524 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
526 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
527 /* NB, OggDirac granulepos values are in units of 2*picturerate */
528 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
532 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
536 p_stream->i_interpolated_pcr = p_stream->i_pcr;
540 p_stream->i_pcr = -1;
542 /* no granulepos available, try to interpolate the pcr.
543 * If we can't then don't touch the old value. */
544 if( p_stream->fmt.i_cat == VIDEO_ES )
545 /* 1 frame per packet */
546 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
547 else if( p_stream->fmt.i_bitrate )
548 p_stream->i_interpolated_pcr +=
549 ( p_oggpacket->bytes * INT64_C(1000000) /
550 p_stream->fmt.i_bitrate / 8 );
554 /****************************************************************************
555 * Ogg_DecodePacket: Decode an Ogg packet.
556 ****************************************************************************/
557 static void Ogg_DecodePacket( demux_t *p_demux,
558 logical_stream_t *p_stream,
559 ogg_packet *p_oggpacket )
563 int i_header_len = 0;
564 mtime_t i_pts = -1, i_interpolated_pts;
565 demux_sys_t *p_ogg = p_demux->p_sys;
568 if( !p_oggpacket->bytes )
570 msg_Dbg( p_demux, "discarding 0 sized packet" );
574 if( p_oggpacket->bytes >= 7 &&
575 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
577 /* it's an Annodex packet -- skip it (do nothing) */
580 else if( p_oggpacket->bytes >= 7 &&
581 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
583 /* it's an AnxData packet -- skip it (do nothing) */
587 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
588 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
590 /* Check the ES is selected */
591 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
592 p_stream->p_es, &b_selected );
594 if( p_stream->b_force_backup )
597 bool b_store_size = true;
598 bool b_store_num_headers = false;
600 p_stream->i_packets_backup++;
601 switch( p_stream->fmt.i_codec )
603 case VLC_FOURCC( 'v','o','r','b' ):
604 case VLC_FOURCC( 's','p','x',' ' ):
605 case VLC_FOURCC( 't','h','e','o' ):
606 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
609 case VLC_FOURCC( 'f','l','a','c' ):
610 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
612 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
613 p_stream->b_force_backup = 0;
615 else if( p_stream->fmt.audio.i_rate )
617 p_stream->b_force_backup = 0;
618 if( p_oggpacket->bytes >= 9 )
620 p_oggpacket->packet += 9;
621 p_oggpacket->bytes -= 9;
624 b_store_size = false;
627 case VLC_FOURCC( 'k','a','t','e' ):
628 if( p_stream->i_packets_backup == 1)
629 b_store_num_headers = true;
630 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
634 p_stream->b_force_backup = 0;
638 /* Backup the ogg packet (likely an header packet) */
639 p_stream->p_headers =
640 realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
641 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
642 if( p_stream->p_headers )
644 uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
646 if( b_store_num_headers )
648 /* Kate streams store the number of headers in the first header,
649 so we can't just test for 3 as Vorbis/Theora */
650 *(p_extra++) = p_stream->i_kate_num_headers;
654 *(p_extra++) = p_oggpacket->bytes >> 8;
655 *(p_extra++) = p_oggpacket->bytes & 0xFF;
657 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
658 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
660 if( !p_stream->b_force_backup )
662 /* Last header received, commit changes */
663 free( p_stream->fmt.p_extra );
665 p_stream->fmt.i_extra = p_stream->i_headers;
666 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
667 if( p_stream->fmt.p_extra )
668 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
669 p_stream->i_headers );
671 p_stream->fmt.i_extra = 0;
673 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
674 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
675 p_stream->p_es, &p_stream->fmt );
677 /* we're not at BOS anymore for this logical stream */
683 p_stream->p_headers = p_sav;
686 b_selected = false; /* Discard the header packet */
689 /* Convert the pcr into a pts */
690 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
691 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
692 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
694 if( p_stream->i_pcr >= 0 )
696 /* This is for streams where the granulepos of the header packets
697 * doesn't match these of the data packets (eg. ogg web radios). */
698 if( p_stream->i_previous_pcr == 0 &&
699 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
701 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
703 /* Call the pace control */
704 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
708 p_stream->i_previous_pcr = p_stream->i_pcr;
710 /* The granulepos is the end date of the sample */
711 i_pts = p_stream->i_pcr;
715 /* Convert the granulepos into the next pcr */
716 i_interpolated_pts = p_stream->i_interpolated_pcr;
717 Ogg_UpdatePCR( p_stream, p_oggpacket );
719 /* SPU streams are typically discontinuous, do not mind large gaps */
720 if( p_stream->fmt.i_cat != SPU_ES )
722 if( p_stream->i_pcr >= 0 )
724 /* This is for streams where the granulepos of the header packets
725 * doesn't match these of the data packets (eg. ogg web radios). */
726 if( p_stream->i_previous_pcr == 0 &&
727 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
729 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
731 /* Call the pace control */
732 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
737 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
738 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
739 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
740 p_stream->i_pcr >= 0 )
742 p_stream->i_previous_pcr = p_stream->i_pcr;
744 /* The granulepos is the start date of the sample */
745 i_pts = p_stream->i_pcr;
750 /* This stream isn't currently selected so we don't need to decode it,
751 * but we did need to store its pcr as it might be selected later on */
755 if( p_oggpacket->bytes <= 0 )
758 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
761 if( i_pts == 0 ) i_pts = 1;
762 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
763 else if( i_pts == -1 ) i_pts = 0;
765 if( p_stream->fmt.i_cat == AUDIO_ES )
766 p_block->i_dts = p_block->i_pts = i_pts;
767 else if( p_stream->fmt.i_cat == SPU_ES )
769 p_block->i_dts = p_block->i_pts = i_pts;
770 p_block->i_length = 0;
772 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
773 p_block->i_dts = p_block->i_pts = i_pts;
774 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
776 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
777 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
779 uint64_t u_pnum = dts + delay;
781 p_block->i_dts = p_stream->i_pcr;
783 /* NB, OggDirac granulepos values are in units of 2*picturerate */
784 if( -1 != p_oggpacket->granulepos )
785 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
789 p_block->i_dts = i_pts;
793 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
794 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
795 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
796 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
797 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
798 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
799 p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
800 p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
802 /* We remove the header from the packet */
803 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
804 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
806 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
808 /* But with subtitles we need to retrieve the duration first */
811 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
813 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
815 lenbytes = lenbytes << 8;
816 lenbytes += *(p_oggpacket->packet + i_header_len - i);
819 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
820 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
821 p_oggpacket->packet[i_header_len + 1] != 0 &&
822 p_oggpacket->packet[i_header_len + 1] != '\n' &&
823 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
825 p_block->i_length = (mtime_t)lenbytes * 1000;
830 if( p_block->i_buffer >= (unsigned int)i_header_len )
831 p_block->i_buffer -= i_header_len;
833 p_block->i_buffer = 0;
836 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
838 /* FIXME: the biggest hack I've ever done */
839 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
840 p_block->i_pts, p_block->i_dts );
844 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
845 p_oggpacket->bytes - i_header_len );
847 es_out_Send( p_demux->out, p_stream->p_es, p_block );
850 /****************************************************************************
851 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
852 * stream and fill p_ogg.
853 *****************************************************************************
854 * The initial page of a logical stream is marked as a 'bos' page.
855 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
856 * together and all of the initial pages must appear before any data pages.
858 * On success this function returns VLC_SUCCESS.
859 ****************************************************************************/
860 static int Ogg_FindLogicalStreams( demux_t *p_demux )
862 demux_sys_t *p_ogg = p_demux->p_sys ;
863 ogg_packet oggpacket;
867 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
869 if( ogg_page_bos( &oggpage ) )
872 /* All is wonderful in our fine fine little world.
873 * We found the beginning of our first logical stream. */
874 while( ogg_page_bos( &oggpage ) )
876 logical_stream_t **pp_sav = p_ogg->pp_stream;
877 logical_stream_t *p_stream;
879 p_stream = malloc( sizeof(logical_stream_t) );
883 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
885 memset( p_stream, 0, sizeof(logical_stream_t) );
886 p_stream->p_headers = 0;
887 p_stream->i_secondary_header_packets = 0;
889 es_format_Init( &p_stream->fmt, 0, 0 );
890 es_format_Init( &p_stream->fmt_old, 0, 0 );
892 /* Setup the logical stream */
893 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
894 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
896 /* Extract the initial header from the first page and verify
897 * the codec type of this Ogg bitstream */
898 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
900 /* error. stream version mismatch perhaps */
901 msg_Err( p_demux, "error reading first page of "
902 "Ogg bitstream data" );
906 /* FIXME: check return value */
907 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
909 /* Check for Vorbis header */
910 if( oggpacket.bytes >= 7 &&
911 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
913 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
914 msg_Dbg( p_demux, "found vorbis header" );
916 /* Check for Speex header */
917 else if( oggpacket.bytes >= 5 &&
918 ! memcmp( oggpacket.packet, "Speex", 5 ) )
920 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
921 msg_Dbg( p_demux, "found speex header, channels: %i, "
922 "rate: %i, bitrate: %i",
923 p_stream->fmt.audio.i_channels,
924 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
926 /* Check for Flac header (< version 1.1.1) */
927 else if( oggpacket.bytes >= 4 &&
928 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
930 msg_Dbg( p_demux, "found FLAC header" );
932 /* Grrrr!!!! Did they really have to put all the
933 * important info in the second header packet!!!
934 * (STREAMINFO metadata is in the following packet) */
935 p_stream->b_force_backup = 1;
937 p_stream->fmt.i_cat = AUDIO_ES;
938 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
940 /* Check for Flac header (>= version 1.1.1) */
941 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
942 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
943 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
945 int i_packets = ((int)oggpacket.packet[7]) << 8 |
947 msg_Dbg( p_demux, "found FLAC header version %i.%i "
948 "(%i header packets)",
949 oggpacket.packet[5], oggpacket.packet[6],
952 p_stream->b_force_backup = 1;
954 p_stream->fmt.i_cat = AUDIO_ES;
955 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
956 oggpacket.packet += 13; oggpacket.bytes -= 13;
957 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
959 /* Check for Theora header */
960 else if( oggpacket.bytes >= 7 &&
961 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
963 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
966 "found theora header, bitrate: %i, rate: %f",
967 p_stream->fmt.i_bitrate, p_stream->f_rate );
969 /* Check for Dirac header */
970 else if( oggpacket.bytes >= 5 &&
971 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
973 Ogg_ReadDiracHeader( p_stream, &oggpacket );
974 msg_Dbg( p_demux, "found dirac header" );
976 /* Check for Tarkin header */
977 else if( oggpacket.bytes >= 7 &&
978 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
982 msg_Dbg( p_demux, "found tarkin header" );
983 p_stream->fmt.i_cat = VIDEO_ES;
984 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
986 /* Cheat and get additionnal info ;) */
987 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
988 oggpack_adv( &opb, 88 );
989 oggpack_adv( &opb, 104 );
990 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
991 p_stream->f_rate = 2; /* FIXME */
993 "found tarkin header, bitrate: %i, rate: %f",
994 p_stream->fmt.i_bitrate, p_stream->f_rate );
996 /* Check for Annodex header */
997 else if( oggpacket.bytes >= 7 &&
998 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1000 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1002 /* kill annodex track */
1006 /* Check for Annodex header */
1007 else if( oggpacket.bytes >= 7 &&
1008 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1010 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1013 /* Check for Kate header */
1014 else if( oggpacket.bytes >= 8 &&
1015 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1017 Ogg_ReadKateHeader( p_stream, &oggpacket );
1018 msg_Dbg( p_demux, "found kate header" );
1020 else if( oggpacket.bytes >= 142 &&
1021 !memcmp( &oggpacket.packet[1],
1022 "Direct Show Samples embedded in Ogg", 35 ))
1024 /* Old header type */
1026 /* Check for video header (old format) */
1027 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1028 oggpacket.bytes >= 184 )
1030 p_stream->fmt.i_cat = VIDEO_ES;
1031 p_stream->fmt.i_codec =
1032 VLC_FOURCC( oggpacket.packet[68],
1033 oggpacket.packet[69],
1034 oggpacket.packet[70],
1035 oggpacket.packet[71] );
1036 msg_Dbg( p_demux, "found video header of type: %.4s",
1037 (char *)&p_stream->fmt.i_codec );
1039 p_stream->fmt.video.i_frame_rate = 10000000;
1040 p_stream->fmt.video.i_frame_rate_base =
1041 GetQWLE((oggpacket.packet+164));
1042 p_stream->f_rate = 10000000.0 /
1043 GetQWLE((oggpacket.packet+164));
1044 p_stream->fmt.video.i_bits_per_pixel =
1045 GetWLE((oggpacket.packet+182));
1046 if( !p_stream->fmt.video.i_bits_per_pixel )
1048 p_stream->fmt.video.i_bits_per_pixel = 24;
1049 p_stream->fmt.video.i_width =
1050 GetDWLE((oggpacket.packet+176));
1051 p_stream->fmt.video.i_height =
1052 GetDWLE((oggpacket.packet+180));
1055 "fps: %f, width:%i; height:%i, bitcount:%i",
1057 p_stream->fmt.video.i_width,
1058 p_stream->fmt.video.i_height,
1059 p_stream->fmt.video.i_bits_per_pixel);
1062 /* Check for audio header (old format) */
1063 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1065 unsigned int i_extra_size;
1066 unsigned int i_format_tag;
1068 p_stream->fmt.i_cat = AUDIO_ES;
1070 i_extra_size = GetWLE((oggpacket.packet+140));
1071 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1073 p_stream->fmt.i_extra = i_extra_size;
1074 p_stream->fmt.p_extra = malloc( i_extra_size );
1075 if( p_stream->fmt.p_extra )
1076 memcpy( p_stream->fmt.p_extra,
1077 oggpacket.packet + 142, i_extra_size );
1079 p_stream->fmt.i_extra = 0;
1082 i_format_tag = GetWLE((oggpacket.packet+124));
1083 p_stream->fmt.audio.i_channels =
1084 GetWLE((oggpacket.packet+126));
1085 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1086 GetDWLE((oggpacket.packet+128));
1087 p_stream->fmt.i_bitrate =
1088 GetDWLE((oggpacket.packet+132)) * 8;
1089 p_stream->fmt.audio.i_blockalign =
1090 GetWLE((oggpacket.packet+136));
1091 p_stream->fmt.audio.i_bitspersample =
1092 GetWLE((oggpacket.packet+138));
1094 wf_tag_to_fourcc( i_format_tag,
1095 &p_stream->fmt.i_codec, 0 );
1097 if( p_stream->fmt.i_codec ==
1098 VLC_FOURCC('u','n','d','f') )
1100 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1101 ( i_format_tag >> 8 ) & 0xff,
1102 i_format_tag & 0xff );
1105 msg_Dbg( p_demux, "found audio header of type: %.4s",
1106 (char *)&p_stream->fmt.i_codec );
1107 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1108 "%dbits/sample %dkb/s",
1110 p_stream->fmt.audio.i_channels,
1111 p_stream->fmt.audio.i_rate,
1112 p_stream->fmt.audio.i_bitspersample,
1113 p_stream->fmt.i_bitrate / 1024 );
1118 msg_Dbg( p_demux, "stream %d has an old header "
1119 "but is of an unknown type", p_ogg->i_streams-1 );
1124 else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1125 oggpacket.bytes >= 56+1 )
1127 stream_header_t tmp;
1128 stream_header_t *st = &tmp;
1130 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1131 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1132 st->size = GetDWLE( &oggpacket.packet[1+12] );
1133 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1134 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1135 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1136 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1137 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1139 /* Check for video header (new format) */
1140 if( !strncmp( st->streamtype, "video", 5 ) )
1142 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1143 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1145 p_stream->fmt.i_cat = VIDEO_ES;
1147 /* We need to get rid of the header packet */
1148 ogg_stream_packetout( &p_stream->os, &oggpacket );
1150 p_stream->fmt.i_codec =
1151 VLC_FOURCC( st->subtype[0], st->subtype[1],
1152 st->subtype[2], st->subtype[3] );
1153 msg_Dbg( p_demux, "found video header of type: %.4s",
1154 (char *)&p_stream->fmt.i_codec );
1156 p_stream->fmt.video.i_frame_rate = 10000000;
1157 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1158 if( st->time_unit <= 0 )
1159 st->time_unit = 400000;
1160 p_stream->f_rate = 10000000.0 / st->time_unit;
1161 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1162 p_stream->fmt.video.i_width = st->sh.video.width;
1163 p_stream->fmt.video.i_height = st->sh.video.height;
1166 "fps: %f, width:%i; height:%i, bitcount:%i",
1168 p_stream->fmt.video.i_width,
1169 p_stream->fmt.video.i_height,
1170 p_stream->fmt.video.i_bits_per_pixel );
1172 /* Check for audio header (new format) */
1173 else if( !strncmp( st->streamtype, "audio", 5 ) )
1176 unsigned int i_extra_size;
1179 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1180 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1181 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1183 p_stream->fmt.i_cat = AUDIO_ES;
1185 /* We need to get rid of the header packet */
1186 ogg_stream_packetout( &p_stream->os, &oggpacket );
1188 i_extra_size = st->size - 56;
1190 if( i_extra_size > 0 &&
1191 i_extra_size < oggpacket.bytes - 1 - 56 )
1193 p_stream->fmt.i_extra = i_extra_size;
1194 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1195 if( p_stream->fmt.p_extra )
1196 memcpy( p_stream->fmt.p_extra, st + 1,
1197 p_stream->fmt.i_extra );
1199 p_stream->fmt.i_extra = 0;
1202 memcpy( p_buffer, st->subtype, 4 );
1204 i_format_tag = strtol(p_buffer,NULL,16);
1205 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1206 if( st->time_unit <= 0 )
1207 st->time_unit = 10000000;
1208 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1209 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1210 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1211 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1213 wf_tag_to_fourcc( i_format_tag,
1214 &p_stream->fmt.i_codec, 0 );
1216 if( p_stream->fmt.i_codec ==
1217 VLC_FOURCC('u','n','d','f') )
1219 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1220 ( i_format_tag >> 8 ) & 0xff,
1221 i_format_tag & 0xff );
1224 msg_Dbg( p_demux, "found audio header of type: %.4s",
1225 (char *)&p_stream->fmt.i_codec );
1226 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1227 "%dbits/sample %dkb/s",
1229 p_stream->fmt.audio.i_channels,
1230 p_stream->fmt.audio.i_rate,
1231 p_stream->fmt.audio.i_bitspersample,
1232 p_stream->fmt.i_bitrate / 1024 );
1234 /* Check for text (subtitles) header */
1235 else if( !strncmp(st->streamtype, "text", 4) )
1237 /* We need to get rid of the header packet */
1238 ogg_stream_packetout( &p_stream->os, &oggpacket );
1240 msg_Dbg( p_demux, "found text subtitles header" );
1241 p_stream->fmt.i_cat = SPU_ES;
1242 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1243 p_stream->f_rate = 1000; /* granulepos is in millisec */
1247 msg_Dbg( p_demux, "stream %d has a header marker "
1248 "but is of an unknown type", p_ogg->i_streams-1 );
1253 else if( oggpacket.bytes >= 7 &&
1254 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1258 msg_Dbg( p_demux, "stream %d is a skeleton",
1259 p_ogg->i_streams-1 );
1260 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1264 msg_Dbg( p_demux, "stream %d is of unknown type",
1265 p_ogg->i_streams-1 );
1270 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1271 return VLC_EGENERIC;
1274 /* we'll need to get all headers for all of those streams
1275 that we have to backup headers for */
1277 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1279 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1284 /* This is the first data page, which means we are now finished
1285 * with the initial pages. We just need to store it in the relevant
1287 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1289 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1292 p_ogg->b_page_waiting = true;
1301 return VLC_EGENERIC;
1304 /****************************************************************************
1305 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1306 * Elementary streams.
1307 ****************************************************************************/
1308 static int Ogg_BeginningOfStream( demux_t *p_demux )
1310 demux_sys_t *p_ogg = p_demux->p_sys ;
1311 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1314 /* Find the logical streams embedded in the physical stream and
1315 * initialize our p_ogg structure. */
1316 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1318 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1319 return VLC_EGENERIC;
1322 p_ogg->i_bitrate = 0;
1324 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1326 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1328 p_stream->p_es = NULL;
1330 /* Try first to reuse an old ES */
1332 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1333 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1335 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1337 p_stream->p_es = p_old_stream->p_es;
1338 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1340 p_old_stream->p_es = NULL;
1341 p_old_stream = NULL;
1344 if( !p_stream->p_es )
1345 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1347 // TODO: something to do here ?
1348 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1350 /* Set the CMML stream active */
1351 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1354 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1356 p_stream->i_pcr = p_stream->i_previous_pcr =
1357 p_stream->i_interpolated_pcr = -1;
1358 p_stream->b_reinit = false;
1361 if( p_ogg->p_old_stream )
1363 if( p_ogg->p_old_stream->p_es )
1364 msg_Dbg( p_demux, "old stream not reused" );
1365 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1366 p_ogg->p_old_stream = NULL;
1371 /****************************************************************************
1372 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1373 ****************************************************************************/
1374 static void Ogg_EndOfStream( demux_t *p_demux )
1376 demux_sys_t *p_ogg = p_demux->p_sys ;
1379 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1380 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1381 free( p_ogg->pp_stream );
1384 p_ogg->i_bitrate = 0;
1385 p_ogg->i_streams = 0;
1386 p_ogg->pp_stream = NULL;
1390 * This function delete and release all data associated to a logical_stream_t
1392 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1394 if( p_stream->p_es )
1395 es_out_Del( p_demux->out, p_stream->p_es );
1397 ogg_stream_clear( &p_stream->os );
1398 free( p_stream->p_headers );
1400 es_format_Clean( &p_stream->fmt_old );
1401 es_format_Clean( &p_stream->fmt );
1406 * This function check if a we need to reset a decoder in case we are
1409 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1415 for( i = 0; i < 3; i++ )
1417 const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1418 const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1420 if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1423 const int i_new_size = GetWBE( &p_new_extra[0] );
1424 const int i_old_size = GetWBE( &p_old_extra[0] );
1426 if( i != 1 ) /* Ignore vorbis comment */
1428 if( i_new_size != i_old_size )
1430 if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1434 i_new += 2 + i_new_size;
1435 i_old += 2 + i_old_size;
1439 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1441 bool b_compatible = false;
1442 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1445 /* Only vorbis is supported */
1446 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) )
1447 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1450 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1452 return !b_compatible;
1455 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1456 ogg_packet *p_oggpacket )
1459 int i_fps_numerator;
1460 int i_fps_denominator;
1461 int i_keyframe_frequency_force;
1463 p_stream->fmt.i_cat = VIDEO_ES;
1464 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1466 /* Signal that we want to keep a backup of the theora
1467 * stream headers. They will be used when switching between
1469 p_stream->b_force_backup = 1;
1471 /* Cheat and get additionnal info ;) */
1472 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1473 bs_skip( &bitstream, 56 );
1474 bs_read( &bitstream, 8 ); /* major version num */
1475 bs_read( &bitstream, 8 ); /* minor version num */
1476 bs_read( &bitstream, 8 ); /* subminor version num */
1477 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1478 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1479 bs_read( &bitstream, 24 ); /* frame width */
1480 bs_read( &bitstream, 24 ); /* frame height */
1481 bs_read( &bitstream, 8 ); /* x offset */
1482 bs_read( &bitstream, 8 ); /* y offset */
1484 i_fps_numerator = bs_read( &bitstream, 32 );
1485 i_fps_denominator = bs_read( &bitstream, 32 );
1486 bs_read( &bitstream, 24 ); /* aspect_numerator */
1487 bs_read( &bitstream, 24 ); /* aspect_denominator */
1489 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1490 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1492 bs_read( &bitstream, 8 ); /* colorspace */
1493 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1494 bs_read( &bitstream, 6 ); /* quality */
1496 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1498 /* granule_shift = i_log( frequency_force -1 ) */
1499 p_stream->i_granule_shift = 0;
1500 i_keyframe_frequency_force--;
1501 while( i_keyframe_frequency_force )
1503 p_stream->i_granule_shift++;
1504 i_keyframe_frequency_force >>= 1;
1507 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1510 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1511 ogg_packet *p_oggpacket )
1515 p_stream->fmt.i_cat = AUDIO_ES;
1516 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1518 /* Signal that we want to keep a backup of the vorbis
1519 * stream headers. They will be used when switching between
1521 p_stream->b_force_backup = 1;
1523 /* Cheat and get additionnal info ;) */
1524 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1525 oggpack_adv( &opb, 88 );
1526 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1527 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1528 oggpack_read( &opb, 32 );
1529 oggpack_adv( &opb, 32 );
1530 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1533 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1534 ogg_packet *p_oggpacket )
1538 p_stream->fmt.i_cat = AUDIO_ES;
1539 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1541 /* Signal that we want to keep a backup of the speex
1542 * stream headers. They will be used when switching between
1544 p_stream->b_force_backup = 1;
1546 /* Cheat and get additionnal info ;) */
1547 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1548 oggpack_adv( &opb, 224 );
1549 oggpack_adv( &opb, 32 ); /* speex_version_id */
1550 oggpack_adv( &opb, 32 ); /* header_size */
1551 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1552 oggpack_adv( &opb, 32 ); /* mode */
1553 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1554 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1555 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1558 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1559 ogg_packet *p_oggpacket )
1561 /* Parse the STREAMINFO metadata */
1564 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1567 if( bs_read( &s, 7 ) == 0 )
1569 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1572 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1573 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1575 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1576 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1580 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1583 /* Fake this as the last metadata block */
1584 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1588 /* This ain't a STREAMINFO metadata */
1589 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1593 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1594 ogg_packet *p_oggpacket )
1601 p_stream->fmt.i_cat = SPU_ES;
1602 p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1604 /* Signal that we want to keep a backup of the kate
1605 * stream headers. They will be used when switching between
1607 p_stream->b_force_backup = 1;
1609 /* Cheat and get additionnal info ;) */
1610 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1611 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1612 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1613 oggpack_adv( &opb, 3*8 );
1614 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1615 oggpack_adv( &opb, 8*8 ); /* reserved */
1616 gnum = oggpack_read( &opb, 32 );
1617 gden = oggpack_read( &opb, 32 );
1618 p_stream->f_rate = (double)gnum/gden;
1620 p_stream->fmt.psz_language = malloc(16);
1621 if( p_stream->fmt.psz_language )
1623 for( n = 0; n < 16; ++n )
1624 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1625 p_stream->fmt.psz_language[15] = 0; /* just in case */
1629 for( n = 0; n < 16; ++n )
1630 oggpack_read(&opb,8);
1632 p_stream->fmt.psz_description = malloc(16);
1633 if( p_stream->fmt.psz_description )
1635 for( n = 0; n < 16; ++n )
1636 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1637 p_stream->fmt.psz_description[15] = 0; /* just in case */
1641 for( n = 0; n < 16; ++n )
1642 oggpack_read(&opb,8);
1646 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1647 logical_stream_t *p_stream,
1648 ogg_packet *p_oggpacket )
1650 if( p_oggpacket->bytes >= 28 &&
1651 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1655 uint16_t major_version;
1656 uint16_t minor_version;
1657 uint64_t timebase_numerator;
1658 uint64_t timebase_denominator;
1660 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1662 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1663 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1664 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1665 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1666 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1667 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1669 else if( p_oggpacket->bytes >= 42 &&
1670 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1672 uint64_t granule_rate_numerator;
1673 uint64_t granule_rate_denominator;
1674 char content_type_string[1024];
1676 /* Read in Annodex header fields */
1678 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1679 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1680 p_stream->i_secondary_header_packets =
1681 GetDWLE( &p_oggpacket->packet[24] );
1683 /* we are guaranteed that the first header field will be
1684 * the content-type (by the Annodex standard) */
1685 content_type_string[0] = '\0';
1686 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1688 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1689 p_oggpacket->bytes - 1 );
1690 if( p && p[0] == '\r' && p[1] == '\n' )
1691 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1692 content_type_string );
1695 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1696 granule_rate_numerator, granule_rate_denominator,
1697 p_stream->i_secondary_header_packets, content_type_string );
1699 p_stream->f_rate = (float) granule_rate_numerator /
1700 (float) granule_rate_denominator;
1702 /* What type of file do we have?
1703 * strcmp is safe to use here because we've extracted
1704 * content_type_string from the stream manually */
1705 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1707 /* n.b. WAVs are unsupported right now */
1708 p_stream->fmt.i_cat = UNKNOWN_ES;
1710 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1712 p_stream->fmt.i_cat = AUDIO_ES;
1713 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1715 p_stream->b_force_backup = 1;
1717 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1719 p_stream->fmt.i_cat = AUDIO_ES;
1720 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1722 p_stream->b_force_backup = 1;
1724 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1726 p_stream->fmt.i_cat = VIDEO_ES;
1727 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1729 p_stream->b_force_backup = 1;
1731 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1733 p_stream->fmt.i_cat = VIDEO_ES;
1734 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1736 p_stream->b_force_backup = 1;
1738 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1740 /* n.b. MPEG streams are unsupported right now */
1741 p_stream->fmt.i_cat = VIDEO_ES;
1742 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1744 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1746 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1747 p_stream->fmt.i_cat = SPU_ES;
1748 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );
1753 static uint32_t dirac_uint( bs_t *p_bs )
1755 uint32_t u_count = 0, u_value = 0;
1756 /* FIXME possible infinite loop */
1757 while( !bs_read( p_bs, 1 ) )
1761 u_value |= bs_read( p_bs, 1 );
1764 return (1<<u_count) - 1 + u_value;
1767 static int dirac_bool( bs_t *p_bs )
1769 return bs_read( p_bs, 1 );
1772 static void Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1773 ogg_packet *p_oggpacket )
1777 p_stream->fmt.i_cat = VIDEO_ES;
1778 p_stream->fmt.i_codec = VLC_FOURCC( 'd','r','a','c' );
1779 p_stream->i_granule_shift = 32;
1781 /* Backing up stream headers is not required -- seqhdrs are repeated
1782 * thoughout the stream at suitable decoding start points */
1783 p_stream->b_force_backup = 0;
1785 /* read in useful bits from sequence header */
1786 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1787 bs_skip( &bs, 13*8); /* parse_info_header */
1788 dirac_uint( &bs ); /* major_version */
1789 dirac_uint( &bs ); /* minor_version */
1790 dirac_uint( &bs ); /* profile */
1791 dirac_uint( &bs ); /* level */
1793 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1795 if( dirac_bool( &bs ) )
1797 dirac_uint( &bs ); /* frame_width */
1798 dirac_uint( &bs ); /* frame_height */
1801 if( dirac_bool( &bs ) )
1803 dirac_uint( &bs ); /* chroma_format */
1806 if( dirac_bool( &bs ) )
1808 dirac_uint( &bs ); /* scan_format */
1811 static const struct {
1812 uint32_t u_n /* numerator */, u_d /* denominator */;
1813 } dirac_frate_tbl[] = { /* table 10.3 */
1814 {1,1}, /* this first value is never used */
1815 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1816 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1819 static const uint32_t dirac_vidfmt_frate[] = { /* table C.1 */
1820 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1823 /* FIXME possible out of bound access */
1824 uint32_t u_n = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_n;
1825 uint32_t u_d = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_d;
1826 if( dirac_bool( &bs ) )
1828 /* FIXME possible out of bound access */
1829 uint32_t frame_rate_index = dirac_uint( &bs );
1830 u_n = dirac_frate_tbl[frame_rate_index].u_n;
1831 u_d = dirac_frate_tbl[frame_rate_index].u_d;
1832 if( frame_rate_index == 0 )
1834 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1835 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1838 p_stream->f_rate = (float) u_n / u_d;