1 /*****************************************************************************
2 * ogg.c : ogg stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 #include <vlc/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
39 static int Open ( vlc_object_t * );
40 static void Close( vlc_object_t * );
43 set_description( _("Ogg stream demuxer" ) );
44 set_capability( "demux2", 50 );
45 set_callbacks( Open, Close );
46 add_shortcut( "ogg" );
50 /*****************************************************************************
51 * Definitions of structures and functions used by this plugins
52 *****************************************************************************/
53 typedef struct logical_stream_s
55 ogg_stream_state os; /* logical stream of packets */
64 /* the header of some logical streams (eg vorbis) contain essential
65 * data for the decoder. We back them up here in case we need to re-feed
66 * them to the decoder. */
69 ogg_packet *p_packets_backup;
71 /* program clock reference (in units of 90kHz) derived from the previous
74 mtime_t i_interpolated_pcr;
75 mtime_t i_previous_pcr;
79 int i_theora_keyframe_granule_shift;
81 /* for Annodex logical bitstreams */
82 int secondary_header_packets;
88 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
90 int i_streams; /* number of logical bitstreams */
91 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
93 /* program clock reference (in units of 90kHz) derived from the pcr of
104 /* OggDS headers for the new header format (used in ogm files) */
105 typedef struct stream_header_video
109 } stream_header_video;
111 typedef struct stream_header_audio
113 ogg_int16_t channels;
114 ogg_int16_t blockalign;
115 ogg_int32_t avgbytespersec;
116 } stream_header_audio;
118 typedef struct stream_header
123 ogg_int32_t size; /* size of the structure */
125 ogg_int64_t time_unit; /* in reference time */
126 ogg_int64_t samples_per_unit;
127 ogg_int32_t default_len; /* in media time */
129 ogg_int32_t buffersize;
130 ogg_int16_t bits_per_sample;
135 stream_header_video video;
137 stream_header_audio audio;
141 #define OGG_BLOCK_SIZE 4096
143 /* Some defines from OggDS */
144 #define PACKET_TYPE_HEADER 0x01
145 #define PACKET_TYPE_BITS 0x07
146 #define PACKET_LEN_BITS01 0xc0
147 #define PACKET_LEN_BITS2 0x02
148 #define PACKET_IS_SYNCPOINT 0x08
150 /*****************************************************************************
152 *****************************************************************************/
153 static int Demux ( demux_t * );
154 static int Control( demux_t *, int, va_list );
156 /* Bitstream manipulation */
157 static int Ogg_ReadPage ( demux_t *, ogg_page * );
158 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
159 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
161 static int Ogg_BeginningOfStream( demux_t *p_demux );
162 static int Ogg_FindLogicalStreams( demux_t *p_demux );
163 static void Ogg_EndOfStream( demux_t *p_demux );
165 /* Logical bitstream headers */
166 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
167 ogg_packet *p_oggpacket );
168 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
169 ogg_packet *p_oggpacket );
170 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *p_stream,
171 ogg_packet *p_oggpacket );
173 /*****************************************************************************
174 * Open: initializes ogg demux structures
175 *****************************************************************************/
176 static int Open( vlc_object_t * p_this )
178 demux_t *p_demux = (demux_t *)p_this;
183 /* Check if we are dealing with an ogg stream */
184 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
186 msg_Err( p_demux, "cannot peek" );
189 if( strcmp( p_demux->psz_demux, "ogg" ) && strncmp( p_peek, "OggS", 4 ) )
191 msg_Warn( p_demux, "ogg module discarded (invalid header)" );
195 /* Set exported functions */
196 p_demux->pf_demux = Demux;
197 p_demux->pf_control = Control;
198 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
200 memset( p_sys, 0, sizeof( demux_sys_t ) );
201 p_sys->i_bitrate = 0;
202 p_sys->pp_stream = NULL;
204 /* Begnning of stream, tell the demux to look for elementary streams. */
207 /* Initialize the Ogg physical bitstream parser */
208 ogg_sync_init( &p_sys->oy );
213 /*****************************************************************************
214 * Close: frees unused data
215 *****************************************************************************/
216 static void Close( vlc_object_t *p_this )
218 demux_t *p_demux = (demux_t *)p_this;
219 demux_sys_t *p_sys = p_demux->p_sys ;
221 /* Cleanup the bitstream parser */
222 ogg_sync_clear( &p_sys->oy );
224 Ogg_EndOfStream( p_demux );
229 /*****************************************************************************
230 * Demux: reads and demuxes data packets
231 *****************************************************************************
232 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
233 *****************************************************************************/
234 static int Demux( demux_t * p_demux )
236 demux_sys_t *p_sys = p_demux->p_sys;
238 ogg_packet oggpacket;
242 if( p_sys->i_eos == p_sys->i_streams )
246 msg_Dbg( p_demux, "end of a group of logical streams" );
247 Ogg_EndOfStream( p_demux );
250 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS ) return 0;
253 msg_Dbg( p_demux, "beginning of a group of logical streams" );
254 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
258 * Demux an ogg page from the stream
260 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
265 /* Test for End of Stream */
266 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
269 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
271 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
273 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
276 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
278 /* Read info from any secondary header packets, if there are any */
279 if( p_stream->secondary_header_packets > 0 )
281 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) &&
282 oggpacket.bytes >= 7 &&
283 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
285 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
286 p_stream->secondary_header_packets = 0;
288 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) &&
289 oggpacket.bytes >= 7 &&
290 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
292 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
293 p_stream->secondary_header_packets = 0;
295 else if ( p_stream->fmt.i_codec == VLC_FOURCC( 'c','m','m','l' ) )
297 p_stream->secondary_header_packets = 0;
300 p_stream->secondary_header_packets--;
303 if( p_stream->b_reinit )
305 /* If synchro is re-initialized we need to drop all the packets
306 * until we find a new dated one. */
307 Ogg_UpdatePCR( p_stream, &oggpacket );
309 if( p_stream->i_pcr >= 0 )
311 p_stream->b_reinit = 0;
315 p_stream->i_interpolated_pcr = -1;
319 /* An Ogg/vorbis packet contains an end date granulepos */
320 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
321 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
322 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
324 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
326 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
330 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
336 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
341 i_stream = 0; p_sys->i_pcr = -1;
342 for( ; i_stream < p_sys->i_streams; i_stream++ )
344 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
346 if( p_stream->fmt.i_cat == SPU_ES )
348 if( p_stream->i_interpolated_pcr < 0 )
351 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
352 p_sys->i_pcr = p_stream->i_interpolated_pcr;
355 if( p_sys->i_pcr >= 0 )
357 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
364 /*****************************************************************************
366 *****************************************************************************/
367 static int Control( demux_t *p_demux, int i_query, va_list args )
369 demux_sys_t *p_sys = p_demux->p_sys;
376 pi64 = (int64_t*)va_arg( args, int64_t * );
377 *pi64 = p_sys->i_pcr;
383 case DEMUX_SET_POSITION:
384 for( i = 0; i < p_sys->i_streams; i++ )
386 logical_stream_t *p_stream = p_sys->pp_stream[i];
388 /* we'll trash all the data until we find the next pcr */
389 p_stream->b_reinit = 1;
390 p_stream->i_pcr = -1;
391 p_stream->i_interpolated_pcr = -1;
392 ogg_stream_reset( &p_stream->os );
394 ogg_sync_reset( &p_sys->oy );
397 return demux2_vaControlHelper( p_demux->s,
399 p_sys->i_bitrate, 1, i_query, args );
403 /****************************************************************************
404 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
405 ****************************************************************************
406 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
407 * are at the end of stream.
408 ****************************************************************************/
409 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
411 demux_sys_t *p_ogg = p_demux->p_sys ;
415 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
417 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
419 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
423 ogg_sync_wrote( &p_ogg->oy, i_read );
429 /****************************************************************************
430 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
432 ****************************************************************************/
433 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
434 ogg_packet *p_oggpacket )
436 /* Convert the granulepos into a pcr */
437 if( p_oggpacket->granulepos >= 0 )
439 if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
441 p_stream->i_pcr = p_oggpacket->granulepos * I64C(1000000)
446 ogg_int64_t iframe = p_oggpacket->granulepos >>
447 p_stream->i_theora_keyframe_granule_shift;
448 ogg_int64_t pframe = p_oggpacket->granulepos -
449 ( iframe << p_stream->i_theora_keyframe_granule_shift );
451 p_stream->i_pcr = ( iframe + pframe ) * I64C(1000000)
455 p_stream->i_interpolated_pcr = p_stream->i_pcr;
459 p_stream->i_pcr = -1;
461 /* no granulepos available, try to interpolate the pcr.
462 * If we can't then don't touch the old value. */
463 if( p_stream->fmt.i_cat == VIDEO_ES )
464 /* 1 frame per packet */
465 p_stream->i_interpolated_pcr += (I64C(1000000) / p_stream->f_rate);
466 else if( p_stream->fmt.i_bitrate )
467 p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * I64C(1000000)
468 / p_stream->fmt.i_bitrate / 8 );
472 /****************************************************************************
473 * Ogg_DecodePacket: Decode an Ogg packet.
474 ****************************************************************************/
475 static void Ogg_DecodePacket( demux_t *p_demux,
476 logical_stream_t *p_stream,
477 ogg_packet *p_oggpacket )
480 vlc_bool_t b_selected;
481 int i_header_len = 0;
485 if( !p_oggpacket->bytes )
487 msg_Dbg( p_demux, "discarding 0 sized packet" );
491 if( p_oggpacket->bytes >= 7 &&
492 ! strncmp ( &p_oggpacket->packet[0], "Annodex", 7 ) )
494 /* it's an Annodex packet -- skip it (do nothing) */
497 else if( p_oggpacket->bytes >= 7 &&
498 ! strncmp ( &p_oggpacket->packet[0], "AnxData", 7 ) )
500 /* it's an AnxData packet -- skip it (do nothing) */
504 if( p_stream->b_force_backup )
506 ogg_packet *p_packet_backup;
507 p_stream->i_packets_backup++;
508 switch( p_stream->fmt.i_codec )
510 case VLC_FOURCC( 'v','o','r','b' ):
511 case VLC_FOURCC( 's','p','x',' ' ):
512 case VLC_FOURCC( 't','h','e','o' ):
513 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
516 case VLC_FOURCC( 'f','l','a','c' ):
517 if( p_stream->i_packets_backup == 1 ) return;
518 else if( p_stream->i_packets_backup == 2 )
520 /* Parse the STREAMINFO metadata */
522 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
524 if( bs_read( &s, 7 ) == 0 )
526 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
529 p_stream->f_rate = p_stream->fmt.audio.i_rate =
531 p_stream->fmt.audio.i_channels =
532 bs_read( &s, 3 ) + 1;
534 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
535 p_stream->fmt.audio.i_channels,
536 (int)p_stream->f_rate );
540 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
543 /* Store STREAMINFO for the decoder and packetizer */
544 p_stream->fmt.i_extra = p_oggpacket->bytes + 4;
545 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
546 memcpy( p_stream->fmt.p_extra, "fLaC", 4);
547 memcpy( ((uint8_t *)p_stream->fmt.p_extra) + 4,
548 p_oggpacket->packet, p_oggpacket->bytes );
550 /* Fake this as the last metadata block */
551 ((uint8_t*)p_stream->fmt.p_extra)[4] |= 0x80;
553 p_stream->p_es = es_out_Add( p_demux->out,
558 /* This ain't a STREAMINFO metadata */
559 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
561 p_stream->b_force_backup = 0;
562 p_stream->i_packets_backup = 0;
564 if( p_oggpacket->granulepos >= 0 )
565 Ogg_UpdatePCR( p_stream, p_oggpacket );
567 p_stream->i_previous_pcr = 0;
573 p_stream->b_force_backup = 0;
577 /* Backup the ogg packet (likely an header packet) */
578 p_stream->p_packets_backup =
579 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
580 sizeof(ogg_packet) );
583 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
585 p_packet_backup->bytes = p_oggpacket->bytes;
586 p_packet_backup->granulepos = p_oggpacket->granulepos;
588 if( p_oggpacket->granulepos >= 0 )
590 /* Because of vorbis granulepos scheme we must set the pcr for the
591 * 1st header packet so it doesn't get discarded in the
593 Ogg_UpdatePCR( p_stream, p_oggpacket );
596 p_packet_backup->packet = malloc( p_oggpacket->bytes );
597 if( !p_packet_backup->packet ) return;
598 memcpy( p_packet_backup->packet, p_oggpacket->packet,
599 p_oggpacket->bytes );
602 /* Check the ES is selected */
603 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
604 p_stream->p_es, &b_selected );
606 if( b_selected && !p_stream->b_activated )
608 p_stream->b_activated = VLC_TRUE;
610 /* Newly activated stream, feed the backup headers to the decoder */
611 if( !p_stream->b_force_backup )
614 for( i = 0; i < p_stream->i_packets_backup; i++ )
616 /* Set correct starting date in header packets */
617 p_stream->p_packets_backup[i].granulepos =
618 p_stream->i_interpolated_pcr * p_stream->f_rate / I64C(1000000);
620 Ogg_DecodePacket( p_demux, p_stream,
621 &p_stream->p_packets_backup[i] );
626 /* Convert the pcr into a pts */
627 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
628 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
629 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
631 if( p_stream->i_pcr >= 0 )
633 /* This is for streams where the granulepos of the header packets
634 * doesn't match these of the data packets (eg. ogg web radios). */
635 if( p_stream->i_previous_pcr == 0 &&
636 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
638 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
640 /* Call the pace control */
641 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
644 p_stream->i_previous_pcr = p_stream->i_pcr;
646 /* The granulepos is the end date of the sample */
647 i_pts = p_stream->i_pcr;
651 /* Convert the granulepos into the next pcr */
652 Ogg_UpdatePCR( p_stream, p_oggpacket );
654 if( p_stream->i_pcr >= 0 )
656 /* This is for streams where the granulepos of the header packets
657 * doesn't match these of the data packets (eg. ogg web radios). */
658 if( p_stream->i_previous_pcr == 0 &&
659 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
661 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
663 /* Call the pace control */
664 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
668 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
669 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
670 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
671 p_stream->i_pcr >= 0 )
673 p_stream->i_previous_pcr = p_stream->i_pcr;
675 /* The granulepos is the start date of the sample */
676 i_pts = p_stream->i_pcr;
681 /* This stream isn't currently selected so we don't need to decode it,
682 * but we did need to store its pcr as it might be selected later on */
683 p_stream->b_activated = VLC_FALSE;
687 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
689 if( p_stream->fmt.i_cat == AUDIO_ES )
690 p_block->i_dts = p_block->i_pts = i_pts;
691 else if( p_stream->fmt.i_cat == SPU_ES )
693 p_block->i_pts = i_pts;
696 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
697 p_block->i_dts = p_block->i_pts = i_pts;
700 p_block->i_dts = i_pts;
704 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
705 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
706 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
707 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
708 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
709 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) )
711 /* We remove the header from the packet */
712 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
713 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
715 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
717 /* But with subtitles we need to retrieve the duration first */
720 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
722 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
724 lenbytes = lenbytes << 8;
725 lenbytes += *(p_oggpacket->packet + i_header_len - i);
728 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
729 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
730 p_oggpacket->packet[i_header_len + 1] != 0 &&
731 p_oggpacket->packet[i_header_len + 1] != '\n' &&
732 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
734 p_block->i_dts = p_block->i_pts + (mtime_t)lenbytes * 1000;
739 p_block->i_buffer -= i_header_len;
742 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
744 /* FIXME: the biggest hack I've ever done */
745 msg_Warn( p_demux, "tarkin pts: "I64Fd", granule: "I64Fd,
746 p_block->i_pts, p_block->i_dts );
750 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
751 p_oggpacket->bytes - i_header_len );
753 es_out_Send( p_demux->out, p_stream->p_es, p_block );
756 /****************************************************************************
757 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
758 * stream and fill p_ogg.
759 *****************************************************************************
760 * The initial page of a logical stream is marked as a 'bos' page.
761 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
762 * together and all of the initial pages must appear before any data pages.
764 * On success this function returns VLC_SUCCESS.
765 ****************************************************************************/
766 static int Ogg_FindLogicalStreams( demux_t *p_demux )
768 demux_sys_t *p_ogg = p_demux->p_sys ;
769 ogg_packet oggpacket;
773 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
775 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
777 if( ogg_page_bos( &oggpage ) )
780 /* All is wonderful in our fine fine little world.
781 * We found the beginning of our first logical stream. */
782 while( ogg_page_bos( &oggpage ) )
786 realloc( p_ogg->pp_stream, p_ogg->i_streams *
787 sizeof(logical_stream_t *) );
789 p_stream = malloc( sizeof(logical_stream_t) );
790 memset( p_stream, 0, sizeof(logical_stream_t) );
792 es_format_Init( &p_stream->fmt, 0, 0 );
793 p_stream->b_activated = VLC_TRUE;
795 /* Setup the logical stream */
796 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
797 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
799 /* Extract the initial header from the first page and verify
800 * the codec type of tis Ogg bitstream */
801 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
803 /* error. stream version mismatch perhaps */
804 msg_Err( p_demux, "error reading first page of "
805 "Ogg bitstream data" );
809 /* FIXME: check return value */
810 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
812 /* Check for Vorbis header */
813 if( oggpacket.bytes >= 7 &&
814 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
816 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
817 msg_Dbg( p_demux, "found vorbis header" );
819 /* Check for Speex header */
820 else if( oggpacket.bytes >= 7 &&
821 ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
825 p_stream->fmt.i_cat = AUDIO_ES;
826 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
828 /* Signal that we want to keep a backup of the vorbis
829 * stream headers. They will be used when switching between
831 p_stream->b_force_backup = 1;
833 /* Cheat and get additionnal info ;) */
834 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
835 oggpack_adv( &opb, 224 );
836 oggpack_adv( &opb, 32 ); /* speex_version_id */
837 oggpack_adv( &opb, 32 ); /* header_size */
838 p_stream->f_rate = p_stream->fmt.audio.i_rate =
839 oggpack_read( &opb, 32 );
840 oggpack_adv( &opb, 32 ); /* mode */
841 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
842 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
843 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
845 msg_Dbg( p_demux, "found speex header, channels: %i, "
846 "rate: %i, bitrate: %i",
847 p_stream->fmt.audio.i_channels,
848 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
850 /* Check for Flac header */
851 else if( oggpacket.bytes >= 4 &&
852 ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
854 msg_Dbg( p_demux, "found FLAC header" );
856 /* Grrrr!!!! Did they really have to put all the
857 * important info in the second header packet!!!
858 * (STREAMINFO metadata is in the following packet) */
859 p_stream->b_force_backup = 1;
861 p_stream->fmt.i_cat = AUDIO_ES;
862 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
864 /* Check for Theora header */
865 else if( oggpacket.bytes >= 7 &&
866 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
868 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
871 "found theora header, bitrate: %i, rate: %f",
872 p_stream->fmt.i_bitrate, p_stream->f_rate );
874 /* Check for Tarkin header */
875 else if( oggpacket.bytes >= 7 &&
876 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
880 msg_Dbg( p_demux, "found tarkin header" );
881 p_stream->fmt.i_cat = VIDEO_ES;
882 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
884 /* Cheat and get additionnal info ;) */
885 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
886 oggpack_adv( &opb, 88 );
887 oggpack_adv( &opb, 104 );
888 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
889 p_stream->f_rate = 2; /* FIXME */
891 "found tarkin header, bitrate: %i, rate: %f",
892 p_stream->fmt.i_bitrate, p_stream->f_rate );
894 /* Check for Annodex header */
895 else if( oggpacket.bytes >= 7 &&
896 ! strncmp( &oggpacket.packet[0], "Annodex", 7 ) )
898 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
900 /* kill annodex track */
904 /* Check for Annodex header */
905 else if( oggpacket.bytes >= 7 &&
906 ! strncmp( &oggpacket.packet[0], "AnxData", 7 ) )
908 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
911 else if( oggpacket.bytes >= 142 &&
912 !strncmp( &oggpacket.packet[1],
913 "Direct Show Samples embedded in Ogg", 35 ))
915 /* Old header type */
917 /* Check for video header (old format) */
918 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
919 oggpacket.bytes >= 184 )
921 p_stream->fmt.i_cat = VIDEO_ES;
922 p_stream->fmt.i_codec =
923 VLC_FOURCC( oggpacket.packet[68],
924 oggpacket.packet[69],
925 oggpacket.packet[70],
926 oggpacket.packet[71] );
927 msg_Dbg( p_demux, "found video header of type: %.4s",
928 (char *)&p_stream->fmt.i_codec );
930 p_stream->f_rate = 10000000.0 /
931 GetQWLE((oggpacket.packet+164));
932 p_stream->fmt.video.i_bits_per_pixel =
933 GetWLE((oggpacket.packet+182));
934 if( !p_stream->fmt.video.i_bits_per_pixel )
936 p_stream->fmt.video.i_bits_per_pixel = 24;
937 p_stream->fmt.video.i_width =
938 GetDWLE((oggpacket.packet+176));
939 p_stream->fmt.video.i_height =
940 GetDWLE((oggpacket.packet+180));
943 "fps: %f, width:%i; height:%i, bitcount:%i",
945 p_stream->fmt.video.i_width,
946 p_stream->fmt.video.i_height,
947 p_stream->fmt.video.i_bits_per_pixel);
950 /* Check for audio header (old format) */
951 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
953 unsigned int i_extra_size;
954 unsigned int i_format_tag;
956 p_stream->fmt.i_cat = AUDIO_ES;
958 i_extra_size = GetWLE((oggpacket.packet+140));
961 p_stream->fmt.i_extra = i_extra_size;
962 p_stream->fmt.p_extra = malloc( i_extra_size );
963 memcpy( p_stream->fmt.p_extra,
964 oggpacket.packet + 142, i_extra_size );
967 i_format_tag = GetWLE((oggpacket.packet+124));
968 p_stream->fmt.audio.i_channels =
969 GetWLE((oggpacket.packet+126));
970 p_stream->f_rate = p_stream->fmt.audio.i_rate =
971 GetDWLE((oggpacket.packet+128));
972 p_stream->fmt.i_bitrate =
973 GetDWLE((oggpacket.packet+132)) * 8;
974 p_stream->fmt.audio.i_blockalign =
975 GetWLE((oggpacket.packet+136));
976 p_stream->fmt.audio.i_bitspersample =
977 GetWLE((oggpacket.packet+138));
979 switch( i_format_tag )
981 case WAVE_FORMAT_PCM:
982 p_stream->fmt.i_codec =
983 VLC_FOURCC( 'a', 'r', 'a', 'w' );
985 case WAVE_FORMAT_MPEG:
986 case WAVE_FORMAT_MPEGLAYER3:
987 p_stream->fmt.i_codec =
988 VLC_FOURCC( 'm', 'p', 'g', 'a' );
990 case WAVE_FORMAT_A52:
991 p_stream->fmt.i_codec =
992 VLC_FOURCC( 'a', '5', '2', ' ' );
994 case WAVE_FORMAT_WMA1:
995 p_stream->fmt.i_codec =
996 VLC_FOURCC( 'w', 'm', 'a', '1' );
998 case WAVE_FORMAT_WMA2:
999 p_stream->fmt.i_codec =
1000 VLC_FOURCC( 'w', 'm', 'a', '2' );
1003 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1004 ( i_format_tag >> 8 ) & 0xff,
1005 i_format_tag & 0xff );
1008 msg_Dbg( p_demux, "found audio header of type: %.4s",
1009 (char *)&p_stream->fmt.i_codec );
1010 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1011 "%dbits/sample %dkb/s",
1013 p_stream->fmt.audio.i_channels,
1014 p_stream->fmt.audio.i_rate,
1015 p_stream->fmt.audio.i_bitspersample,
1016 p_stream->fmt.i_bitrate / 1024 );
1021 msg_Dbg( p_demux, "stream %d has an old header "
1022 "but is of an unknown type", p_ogg->i_streams-1 );
1027 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
1028 == PACKET_TYPE_HEADER &&
1029 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
1031 stream_header *st = (stream_header *)(oggpacket.packet+1);
1033 /* Check for video header (new format) */
1034 if( !strncmp( st->streamtype, "video", 5 ) )
1036 p_stream->fmt.i_cat = VIDEO_ES;
1038 /* We need to get rid of the header packet */
1039 ogg_stream_packetout( &p_stream->os, &oggpacket );
1041 p_stream->fmt.i_codec =
1042 VLC_FOURCC( st->subtype[0], st->subtype[1],
1043 st->subtype[2], st->subtype[3] );
1044 msg_Dbg( p_demux, "found video header of type: %.4s",
1045 (char *)&p_stream->fmt.i_codec );
1047 p_stream->f_rate = 10000000.0 /
1048 GetQWLE(&st->time_unit);
1049 p_stream->fmt.video.i_bits_per_pixel =
1050 GetWLE(&st->bits_per_sample);
1051 p_stream->fmt.video.i_width =
1052 GetDWLE(&st->sh.video.width);
1053 p_stream->fmt.video.i_height =
1054 GetDWLE(&st->sh.video.height);
1057 "fps: %f, width:%i; height:%i, bitcount:%i",
1059 p_stream->fmt.video.i_width,
1060 p_stream->fmt.video.i_height,
1061 p_stream->fmt.video.i_bits_per_pixel );
1063 /* Check for audio header (new format) */
1064 else if( !strncmp( st->streamtype, "audio", 5 ) )
1069 p_stream->fmt.i_cat = AUDIO_ES;
1071 /* We need to get rid of the header packet */
1072 ogg_stream_packetout( &p_stream->os, &oggpacket );
1074 memcpy( p_buffer, st->subtype, 4 );
1076 i_format_tag = strtol(p_buffer,NULL,16);
1077 p_stream->fmt.audio.i_channels =
1078 GetWLE(&st->sh.audio.channels);
1079 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1080 GetQWLE(&st->samples_per_unit);
1081 p_stream->fmt.i_bitrate =
1082 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
1083 p_stream->fmt.audio.i_blockalign =
1084 GetWLE(&st->sh.audio.blockalign);
1085 p_stream->fmt.audio.i_bitspersample =
1086 GetWLE(&st->bits_per_sample);
1088 switch( i_format_tag )
1090 case WAVE_FORMAT_PCM:
1091 p_stream->fmt.i_codec =
1092 VLC_FOURCC( 'a', 'r', 'a', 'w' );
1094 case WAVE_FORMAT_MPEG:
1095 case WAVE_FORMAT_MPEGLAYER3:
1096 p_stream->fmt.i_codec =
1097 VLC_FOURCC( 'm', 'p', 'g', 'a' );
1099 case WAVE_FORMAT_A52:
1100 p_stream->fmt.i_codec =
1101 VLC_FOURCC( 'a', '5', '2', ' ' );
1103 case WAVE_FORMAT_WMA1:
1104 p_stream->fmt.i_codec =
1105 VLC_FOURCC( 'w', 'm', 'a', '1' );
1107 case WAVE_FORMAT_WMA2:
1108 p_stream->fmt.i_codec =
1109 VLC_FOURCC( 'w', 'm', 'a', '2' );
1112 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1113 ( i_format_tag >> 8 ) & 0xff,
1114 i_format_tag & 0xff );
1117 msg_Dbg( p_demux, "found audio header of type: %.4s",
1118 (char *)&p_stream->fmt.i_codec );
1119 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1120 "%dbits/sample %dkb/s",
1122 p_stream->fmt.audio.i_channels,
1123 p_stream->fmt.audio.i_rate,
1124 p_stream->fmt.audio.i_bitspersample,
1125 p_stream->fmt.i_bitrate / 1024 );
1127 /* Check for text (subtitles) header */
1128 else if( !strncmp(st->streamtype, "text", 4) )
1130 /* We need to get rid of the header packet */
1131 ogg_stream_packetout( &p_stream->os, &oggpacket );
1133 msg_Dbg( p_demux, "found text subtitles header" );
1134 p_stream->fmt.i_cat = SPU_ES;
1135 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1136 p_stream->f_rate = 1000; /* granulepos is in milisec */
1140 msg_Dbg( p_demux, "stream %d has a header marker "
1141 "but is of an unknown type", p_ogg->i_streams-1 );
1148 msg_Dbg( p_demux, "stream %d is of unknown type",
1149 p_ogg->i_streams-1 );
1154 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1155 return VLC_EGENERIC;
1158 /* This is the first data page, which means we are now finished
1159 * with the initial pages. We just need to store it in the relevant
1161 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1163 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1175 return VLC_EGENERIC;
1178 /****************************************************************************
1179 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1180 * Elementary streams.
1181 ****************************************************************************/
1182 static int Ogg_BeginningOfStream( demux_t *p_demux )
1184 demux_sys_t *p_ogg = p_demux->p_sys ;
1187 /* Find the logical streams embedded in the physical stream and
1188 * initialize our p_ogg structure. */
1189 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1191 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1192 return VLC_EGENERIC;
1195 p_ogg->i_bitrate = 0;
1197 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1199 #define p_stream p_ogg->pp_stream[i_stream]
1200 if( p_stream->fmt.i_codec != VLC_FOURCC('f','l','a','c') )
1201 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1203 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1205 /* Set the CMML stream active */
1206 es_out_Control( p_demux->out, ES_OUT_SET_ES,
1210 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1212 p_stream->i_pcr = p_stream->i_previous_pcr =
1213 p_stream->i_interpolated_pcr = -1;
1214 p_stream->b_reinit = 0;
1221 /****************************************************************************
1222 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1223 ****************************************************************************/
1224 static void Ogg_EndOfStream( demux_t *p_demux )
1226 demux_sys_t *p_ogg = p_demux->p_sys ;
1229 #define p_stream p_ogg->pp_stream[i_stream]
1230 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1232 if( p_stream->p_es )
1233 es_out_Del( p_demux->out, p_stream->p_es );
1235 p_ogg->i_bitrate -= p_stream->fmt.i_bitrate;
1237 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1238 for( j = 0; j < p_ogg->pp_stream[i_stream]->i_packets_backup; j++ )
1240 free( p_ogg->pp_stream[i_stream]->p_packets_backup[j].packet );
1242 if( p_ogg->pp_stream[i_stream]->p_packets_backup)
1243 free( p_ogg->pp_stream[i_stream]->p_packets_backup );
1245 es_format_Clean( &p_stream->fmt );
1247 free( p_ogg->pp_stream[i_stream] );
1252 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1253 p_ogg->pp_stream = NULL;
1254 p_ogg->i_streams = 0;
1257 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1258 ogg_packet *p_oggpacket )
1261 int i_fps_numerator;
1262 int i_fps_denominator;
1263 int i_keyframe_frequency_force;
1265 p_stream->fmt.i_cat = VIDEO_ES;
1266 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1268 /* Signal that we want to keep a backup of the vorbis
1269 * stream headers. They will be used when switching between
1271 p_stream->b_force_backup = 1;
1273 /* Cheat and get additionnal info ;) */
1274 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1275 bs_skip( &bitstream, 56 );
1276 bs_read( &bitstream, 8 ); /* major version num */
1277 bs_read( &bitstream, 8 ); /* minor version num */
1278 bs_read( &bitstream, 8 ); /* subminor version num */
1279 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1280 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1281 bs_read( &bitstream, 24 ); /* frame width */
1282 bs_read( &bitstream, 24 ); /* frame height */
1283 bs_read( &bitstream, 8 ); /* x offset */
1284 bs_read( &bitstream, 8 ); /* y offset */
1286 i_fps_numerator = bs_read( &bitstream, 32 );
1287 i_fps_denominator = bs_read( &bitstream, 32 );
1288 bs_read( &bitstream, 24 ); /* aspect_numerator */
1289 bs_read( &bitstream, 24 ); /* aspect_denominator */
1290 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1291 bs_read( &bitstream, 8 ); /* colorspace */
1292 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1293 bs_read( &bitstream, 6 ); /* quality */
1295 /* granule_shift = i_log( frequency_force -1 ) */
1296 p_stream->i_theora_keyframe_granule_shift = 0;
1297 i_keyframe_frequency_force--;
1298 while( i_keyframe_frequency_force )
1300 p_stream->i_theora_keyframe_granule_shift++;
1301 i_keyframe_frequency_force >>= 1;
1304 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1306 /* Save this data in p_extra for ffmpeg */
1307 p_stream->fmt.i_extra = p_oggpacket->bytes;
1308 p_stream->fmt.p_extra = malloc( p_oggpacket->bytes );
1309 memcpy( p_stream->fmt.p_extra, p_oggpacket->packet, p_oggpacket->bytes );
1313 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1314 ogg_packet *p_oggpacket )
1318 p_stream->fmt.i_cat = AUDIO_ES;
1319 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1321 /* Signal that we want to keep a backup of the vorbis
1322 * stream headers. They will be used when switching between
1324 p_stream->b_force_backup = 1;
1326 /* Cheat and get additionnal info ;) */
1327 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1328 oggpack_adv( &opb, 88 );
1329 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1330 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1331 oggpack_read( &opb, 32 );
1332 oggpack_adv( &opb, 32 );
1333 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1336 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1337 logical_stream_t *p_stream,
1338 ogg_packet *p_oggpacket )
1340 if( ! strncmp( &p_oggpacket->packet[0], "Annodex", 7 ) )
1344 uint16_t major_version;
1345 uint16_t minor_version;
1346 uint64_t timebase_numerator;
1347 uint64_t timebase_denominator;
1349 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1351 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1352 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1353 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1354 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1355 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1356 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1358 else if( ! strncmp( &p_oggpacket->packet[0], "AnxData", 7 ) )
1360 uint64_t granule_rate_numerator;
1361 uint64_t granule_rate_denominator;
1362 char content_type_string[1024];
1364 /* Read in Annodex header fields */
1366 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1367 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1368 p_stream->secondary_header_packets =
1369 GetDWLE( &p_oggpacket->packet[24] );
1371 msg_Dbg( p_this, "anxdata packet info: %qd/%qd, %d",
1372 granule_rate_numerator, granule_rate_denominator,
1373 p_stream->secondary_header_packets);
1375 /* we are guaranteed that the first header field will be
1376 * the content-type (by the Annodex standard) */
1377 sscanf( &p_oggpacket->packet[28], "Content-Type: %1024s\r\n",
1378 content_type_string );
1380 p_stream->f_rate = (float) granule_rate_numerator /
1381 (float) granule_rate_denominator;
1383 /* What type of file do we have?
1384 * strcmp is safe to use here because we've extracted
1385 * content_type_string from the stream manually */
1386 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1388 /* n.b. WAVs are unsupported right now */
1389 p_stream->fmt.i_cat = UNKNOWN_ES;
1391 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1393 p_stream->fmt.i_cat = AUDIO_ES;
1394 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1396 p_stream->b_force_backup = 1;
1398 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1400 p_stream->fmt.i_cat = VIDEO_ES;
1401 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1403 p_stream->b_force_backup = 1;
1405 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1407 p_stream->fmt.i_cat = VIDEO_ES;
1408 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1410 p_stream->b_force_backup = 1;
1412 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1414 /* n.b. MPEG streams are unsupported right now */
1415 p_stream->fmt.i_cat = VIDEO_ES;
1416 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1418 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1420 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1421 p_stream->fmt.i_cat = SPU_ES;
1422 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );