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 */
63 /* the header of some logical streams (eg vorbis) contain essential
64 * data for the decoder. We back them up here in case we need to re-feed
65 * them to the decoder. */
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 *, ogg_packet * );
167 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
168 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
169 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
170 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
172 /*****************************************************************************
173 * Open: initializes ogg demux structures
174 *****************************************************************************/
175 static int Open( vlc_object_t * p_this )
177 demux_t *p_demux = (demux_t *)p_this;
182 /* Check if we are dealing with an ogg stream */
183 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
185 msg_Err( p_demux, "cannot peek" );
188 if( strcmp( p_demux->psz_demux, "ogg" ) && strncmp( p_peek, "OggS", 4 ) )
190 msg_Warn( p_demux, "ogg module discarded (invalid header)" );
194 /* Set exported functions */
195 p_demux->pf_demux = Demux;
196 p_demux->pf_control = Control;
197 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
199 memset( p_sys, 0, sizeof( demux_sys_t ) );
200 p_sys->i_bitrate = 0;
201 p_sys->pp_stream = NULL;
203 /* Begnning of stream, tell the demux to look for elementary streams. */
206 /* Initialize the Ogg physical bitstream parser */
207 ogg_sync_init( &p_sys->oy );
212 /*****************************************************************************
213 * Close: frees unused data
214 *****************************************************************************/
215 static void Close( vlc_object_t *p_this )
217 demux_t *p_demux = (demux_t *)p_this;
218 demux_sys_t *p_sys = p_demux->p_sys ;
220 /* Cleanup the bitstream parser */
221 ogg_sync_clear( &p_sys->oy );
223 Ogg_EndOfStream( p_demux );
228 /*****************************************************************************
229 * Demux: reads and demuxes data packets
230 *****************************************************************************
231 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
232 *****************************************************************************/
233 static int Demux( demux_t * p_demux )
235 demux_sys_t *p_sys = p_demux->p_sys;
237 ogg_packet oggpacket;
241 if( p_sys->i_eos == p_sys->i_streams )
245 msg_Dbg( p_demux, "end of a group of logical streams" );
246 Ogg_EndOfStream( p_demux );
250 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS ) return 0;
252 msg_Dbg( p_demux, "beginning of a group of logical streams" );
253 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
257 * Demux an ogg page from the stream
259 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
264 /* Test for End of Stream */
265 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
268 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
270 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
272 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
275 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
277 /* Read info from any secondary header packets, if there are any */
278 if( p_stream->secondary_header_packets > 0 )
280 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
281 oggpacket.bytes >= 7 &&
282 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
284 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
285 p_stream->secondary_header_packets = 0;
287 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
288 oggpacket.bytes >= 7 &&
289 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
291 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
292 p_stream->secondary_header_packets = 0;
294 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
296 p_stream->secondary_header_packets = 0;
299 p_stream->secondary_header_packets--;
302 if( p_stream->b_reinit )
304 /* If synchro is re-initialized we need to drop all the packets
305 * until we find a new dated one. */
306 Ogg_UpdatePCR( p_stream, &oggpacket );
308 if( p_stream->i_pcr >= 0 )
310 p_stream->b_reinit = 0;
314 p_stream->i_interpolated_pcr = -1;
318 /* An Ogg/vorbis packet contains an end date granulepos */
319 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
320 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
321 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
323 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
325 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
329 es_out_Control( p_demux->out, ES_OUT_SET_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, 0, -1, p_sys->i_bitrate,
402 /****************************************************************************
403 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
404 ****************************************************************************
405 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
406 * are at the end of stream.
407 ****************************************************************************/
408 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
410 demux_sys_t *p_ogg = p_demux->p_sys ;
414 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
416 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
418 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
422 ogg_sync_wrote( &p_ogg->oy, i_read );
428 /****************************************************************************
429 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
431 ****************************************************************************/
432 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
433 ogg_packet *p_oggpacket )
435 /* Convert the granulepos into a pcr */
436 if( p_oggpacket->granulepos >= 0 )
438 if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
440 p_stream->i_pcr = p_oggpacket->granulepos * I64C(1000000)
445 ogg_int64_t iframe = p_oggpacket->granulepos >>
446 p_stream->i_theora_keyframe_granule_shift;
447 ogg_int64_t pframe = p_oggpacket->granulepos -
448 ( iframe << p_stream->i_theora_keyframe_granule_shift );
450 p_stream->i_pcr = ( iframe + pframe ) * I64C(1000000)
454 p_stream->i_interpolated_pcr = p_stream->i_pcr;
458 p_stream->i_pcr = -1;
460 /* no granulepos available, try to interpolate the pcr.
461 * If we can't then don't touch the old value. */
462 if( p_stream->fmt.i_cat == VIDEO_ES )
463 /* 1 frame per packet */
464 p_stream->i_interpolated_pcr += (I64C(1000000) / p_stream->f_rate);
465 else if( p_stream->fmt.i_bitrate )
466 p_stream->i_interpolated_pcr +=
467 ( 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 /* Check the ES is selected */
505 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
506 p_stream->p_es, &b_selected );
508 if( p_stream->b_force_backup )
511 vlc_bool_t b_store_size = VLC_TRUE;
513 p_stream->i_packets_backup++;
514 switch( p_stream->fmt.i_codec )
516 case VLC_FOURCC( 'v','o','r','b' ):
517 case VLC_FOURCC( 's','p','x',' ' ):
518 case VLC_FOURCC( 't','h','e','o' ):
519 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
522 case VLC_FOURCC( 'f','l','a','c' ):
523 if( p_stream->i_packets_backup == 2 )
525 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
526 p_stream->b_force_backup = 0;
528 b_store_size = VLC_FALSE;
532 p_stream->b_force_backup = 0;
536 /* Backup the ogg packet (likely an header packet) */
537 p_stream->p_headers =
538 realloc( p_stream->p_headers, p_stream->i_headers +
539 p_oggpacket->bytes + (b_store_size ? 2 : 0) );
540 p_extra = p_stream->p_headers + p_stream->i_headers;
543 *(p_extra++) = p_oggpacket->bytes >> 8;
544 *(p_extra++) = p_oggpacket->bytes & 0xFF;
546 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
547 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0);
549 if( !p_stream->b_force_backup )
551 /* Last header received, commit changes */
552 p_stream->fmt.i_extra = p_stream->i_headers;
553 p_stream->fmt.p_extra =
554 realloc( p_stream->fmt.p_extra, p_stream->i_headers );
555 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
556 p_stream->i_headers );
557 es_out_Control( p_demux->out, ES_OUT_SET_FMT,
558 p_stream->p_es, &p_stream->fmt );
561 b_selected = VLC_FALSE; /* Discard the header packet */
564 /* Convert the pcr into a pts */
565 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
566 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
567 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
569 if( p_stream->i_pcr >= 0 )
571 /* This is for streams where the granulepos of the header packets
572 * doesn't match these of the data packets (eg. ogg web radios). */
573 if( p_stream->i_previous_pcr == 0 &&
574 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
576 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
578 /* Call the pace control */
579 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
583 p_stream->i_previous_pcr = p_stream->i_pcr;
585 /* The granulepos is the end date of the sample */
586 i_pts = p_stream->i_pcr;
590 /* Convert the granulepos into the next pcr */
591 Ogg_UpdatePCR( p_stream, p_oggpacket );
593 if( p_stream->i_pcr >= 0 )
595 /* This is for streams where the granulepos of the header packets
596 * doesn't match these of the data packets (eg. ogg web radios). */
597 if( p_stream->i_previous_pcr == 0 &&
598 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
600 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
602 /* Call the pace control */
603 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
607 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
608 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
609 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
610 p_stream->i_pcr >= 0 )
612 p_stream->i_previous_pcr = p_stream->i_pcr;
614 /* The granulepos is the start date of the sample */
615 i_pts = p_stream->i_pcr;
620 /* This stream isn't currently selected so we don't need to decode it,
621 * but we did need to store its pcr as it might be selected later on */
625 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
627 if( p_stream->fmt.i_cat == AUDIO_ES )
628 p_block->i_dts = p_block->i_pts = i_pts;
629 else if( p_stream->fmt.i_cat == SPU_ES )
631 p_block->i_dts = p_block->i_pts = i_pts;
632 p_block->i_length = 0;
634 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
635 p_block->i_dts = p_block->i_pts = i_pts;
638 p_block->i_dts = i_pts;
642 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
643 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
644 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
645 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
646 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
647 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) )
649 /* We remove the header from the packet */
650 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
651 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
653 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
655 /* But with subtitles we need to retrieve the duration first */
658 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
660 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
662 lenbytes = lenbytes << 8;
663 lenbytes += *(p_oggpacket->packet + i_header_len - i);
666 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
667 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
668 p_oggpacket->packet[i_header_len + 1] != 0 &&
669 p_oggpacket->packet[i_header_len + 1] != '\n' &&
670 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
672 p_block->i_length = (mtime_t)lenbytes * 1000;
677 p_block->i_buffer -= i_header_len;
680 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
682 /* FIXME: the biggest hack I've ever done */
683 msg_Warn( p_demux, "tarkin pts: "I64Fd", granule: "I64Fd,
684 p_block->i_pts, p_block->i_dts );
688 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
689 p_oggpacket->bytes - i_header_len );
691 es_out_Send( p_demux->out, p_stream->p_es, p_block );
694 /****************************************************************************
695 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
696 * stream and fill p_ogg.
697 *****************************************************************************
698 * The initial page of a logical stream is marked as a 'bos' page.
699 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
700 * together and all of the initial pages must appear before any data pages.
702 * On success this function returns VLC_SUCCESS.
703 ****************************************************************************/
704 static int Ogg_FindLogicalStreams( demux_t *p_demux )
706 demux_sys_t *p_ogg = p_demux->p_sys ;
707 ogg_packet oggpacket;
711 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
713 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
715 if( ogg_page_bos( &oggpage ) )
718 /* All is wonderful in our fine fine little world.
719 * We found the beginning of our first logical stream. */
720 while( ogg_page_bos( &oggpage ) )
724 realloc( p_ogg->pp_stream, p_ogg->i_streams *
725 sizeof(logical_stream_t *) );
727 p_stream = malloc( sizeof(logical_stream_t) );
728 memset( p_stream, 0, sizeof(logical_stream_t) );
729 p_stream->p_headers = 0;
731 es_format_Init( &p_stream->fmt, 0, 0 );
733 /* Setup the logical stream */
734 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
735 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
737 /* Extract the initial header from the first page and verify
738 * the codec type of tis Ogg bitstream */
739 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
741 /* error. stream version mismatch perhaps */
742 msg_Err( p_demux, "error reading first page of "
743 "Ogg bitstream data" );
747 /* FIXME: check return value */
748 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
750 /* Check for Vorbis header */
751 if( oggpacket.bytes >= 7 &&
752 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
754 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
755 msg_Dbg( p_demux, "found vorbis header" );
757 /* Check for Speex header */
758 else if( oggpacket.bytes >= 7 &&
759 ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
761 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
762 msg_Dbg( p_demux, "found speex header, channels: %i, "
763 "rate: %i, bitrate: %i",
764 p_stream->fmt.audio.i_channels,
765 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
767 /* Check for Flac header */
768 else if( oggpacket.bytes >= 4 &&
769 ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
771 msg_Dbg( p_demux, "found FLAC header" );
773 /* Grrrr!!!! Did they really have to put all the
774 * important info in the second header packet!!!
775 * (STREAMINFO metadata is in the following packet) */
776 p_stream->b_force_backup = 1;
778 p_stream->fmt.i_cat = AUDIO_ES;
779 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
781 /* Check for Theora header */
782 else if( oggpacket.bytes >= 7 &&
783 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
785 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
788 "found theora header, bitrate: %i, rate: %f",
789 p_stream->fmt.i_bitrate, p_stream->f_rate );
791 /* Check for Tarkin header */
792 else if( oggpacket.bytes >= 7 &&
793 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
797 msg_Dbg( p_demux, "found tarkin header" );
798 p_stream->fmt.i_cat = VIDEO_ES;
799 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
801 /* Cheat and get additionnal info ;) */
802 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
803 oggpack_adv( &opb, 88 );
804 oggpack_adv( &opb, 104 );
805 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
806 p_stream->f_rate = 2; /* FIXME */
808 "found tarkin header, bitrate: %i, rate: %f",
809 p_stream->fmt.i_bitrate, p_stream->f_rate );
811 /* Check for Annodex header */
812 else if( oggpacket.bytes >= 7 &&
813 ! strncmp( &oggpacket.packet[0], "Annodex", 7 ) )
815 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
817 /* kill annodex track */
821 /* Check for Annodex header */
822 else if( oggpacket.bytes >= 7 &&
823 ! strncmp( &oggpacket.packet[0], "AnxData", 7 ) )
825 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
828 else if( oggpacket.bytes >= 142 &&
829 !strncmp( &oggpacket.packet[1],
830 "Direct Show Samples embedded in Ogg", 35 ))
832 /* Old header type */
834 /* Check for video header (old format) */
835 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
836 oggpacket.bytes >= 184 )
838 p_stream->fmt.i_cat = VIDEO_ES;
839 p_stream->fmt.i_codec =
840 VLC_FOURCC( oggpacket.packet[68],
841 oggpacket.packet[69],
842 oggpacket.packet[70],
843 oggpacket.packet[71] );
844 msg_Dbg( p_demux, "found video header of type: %.4s",
845 (char *)&p_stream->fmt.i_codec );
847 p_stream->f_rate = 10000000.0 /
848 GetQWLE((oggpacket.packet+164));
849 p_stream->fmt.video.i_bits_per_pixel =
850 GetWLE((oggpacket.packet+182));
851 if( !p_stream->fmt.video.i_bits_per_pixel )
853 p_stream->fmt.video.i_bits_per_pixel = 24;
854 p_stream->fmt.video.i_width =
855 GetDWLE((oggpacket.packet+176));
856 p_stream->fmt.video.i_height =
857 GetDWLE((oggpacket.packet+180));
860 "fps: %f, width:%i; height:%i, bitcount:%i",
862 p_stream->fmt.video.i_width,
863 p_stream->fmt.video.i_height,
864 p_stream->fmt.video.i_bits_per_pixel);
867 /* Check for audio header (old format) */
868 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
870 unsigned int i_extra_size;
871 unsigned int i_format_tag;
873 p_stream->fmt.i_cat = AUDIO_ES;
875 i_extra_size = GetWLE((oggpacket.packet+140));
878 p_stream->fmt.i_extra = i_extra_size;
879 p_stream->fmt.p_extra = malloc( i_extra_size );
880 memcpy( p_stream->fmt.p_extra,
881 oggpacket.packet + 142, i_extra_size );
884 i_format_tag = GetWLE((oggpacket.packet+124));
885 p_stream->fmt.audio.i_channels =
886 GetWLE((oggpacket.packet+126));
887 p_stream->f_rate = p_stream->fmt.audio.i_rate =
888 GetDWLE((oggpacket.packet+128));
889 p_stream->fmt.i_bitrate =
890 GetDWLE((oggpacket.packet+132)) * 8;
891 p_stream->fmt.audio.i_blockalign =
892 GetWLE((oggpacket.packet+136));
893 p_stream->fmt.audio.i_bitspersample =
894 GetWLE((oggpacket.packet+138));
896 wf_tag_to_fourcc( i_format_tag,
897 &p_stream->fmt.i_codec, 0 );
899 if( p_stream->fmt.i_codec ==
900 VLC_FOURCC('u','n','d','f') )
902 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
903 ( i_format_tag >> 8 ) & 0xff,
904 i_format_tag & 0xff );
907 msg_Dbg( p_demux, "found audio header of type: %.4s",
908 (char *)&p_stream->fmt.i_codec );
909 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
910 "%dbits/sample %dkb/s",
912 p_stream->fmt.audio.i_channels,
913 p_stream->fmt.audio.i_rate,
914 p_stream->fmt.audio.i_bitspersample,
915 p_stream->fmt.i_bitrate / 1024 );
920 msg_Dbg( p_demux, "stream %d has an old header "
921 "but is of an unknown type", p_ogg->i_streams-1 );
926 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
927 == PACKET_TYPE_HEADER &&
928 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
930 stream_header *st = (stream_header *)(oggpacket.packet+1);
932 /* Check for video header (new format) */
933 if( !strncmp( st->streamtype, "video", 5 ) )
935 p_stream->fmt.i_cat = VIDEO_ES;
937 /* We need to get rid of the header packet */
938 ogg_stream_packetout( &p_stream->os, &oggpacket );
940 p_stream->fmt.i_codec =
941 VLC_FOURCC( st->subtype[0], st->subtype[1],
942 st->subtype[2], st->subtype[3] );
943 msg_Dbg( p_demux, "found video header of type: %.4s",
944 (char *)&p_stream->fmt.i_codec );
946 p_stream->f_rate = 10000000.0 /
947 GetQWLE(&st->time_unit);
948 p_stream->fmt.video.i_bits_per_pixel =
949 GetWLE(&st->bits_per_sample);
950 p_stream->fmt.video.i_width =
951 GetDWLE(&st->sh.video.width);
952 p_stream->fmt.video.i_height =
953 GetDWLE(&st->sh.video.height);
956 "fps: %f, width:%i; height:%i, bitcount:%i",
958 p_stream->fmt.video.i_width,
959 p_stream->fmt.video.i_height,
960 p_stream->fmt.video.i_bits_per_pixel );
962 /* Check for audio header (new format) */
963 else if( !strncmp( st->streamtype, "audio", 5 ) )
968 p_stream->fmt.i_cat = AUDIO_ES;
970 /* We need to get rid of the header packet */
971 ogg_stream_packetout( &p_stream->os, &oggpacket );
973 p_stream->fmt.i_extra = GetQWLE(&st->size) -
974 sizeof(stream_header);
975 if( p_stream->fmt.i_extra )
977 p_stream->fmt.p_extra =
978 malloc( p_stream->fmt.i_extra );
979 memcpy( p_stream->fmt.p_extra, st + 1,
980 p_stream->fmt.i_extra );
983 memcpy( p_buffer, st->subtype, 4 );
985 i_format_tag = strtol(p_buffer,NULL,16);
986 p_stream->fmt.audio.i_channels =
987 GetWLE(&st->sh.audio.channels);
988 p_stream->f_rate = p_stream->fmt.audio.i_rate =
989 GetQWLE(&st->samples_per_unit);
990 p_stream->fmt.i_bitrate =
991 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
992 p_stream->fmt.audio.i_blockalign =
993 GetWLE(&st->sh.audio.blockalign);
994 p_stream->fmt.audio.i_bitspersample =
995 GetWLE(&st->bits_per_sample);
997 wf_tag_to_fourcc( i_format_tag,
998 &p_stream->fmt.i_codec, 0 );
1000 if( p_stream->fmt.i_codec ==
1001 VLC_FOURCC('u','n','d','f') )
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 );
1018 /* Check for text (subtitles) header */
1019 else if( !strncmp(st->streamtype, "text", 4) )
1021 /* We need to get rid of the header packet */
1022 ogg_stream_packetout( &p_stream->os, &oggpacket );
1024 msg_Dbg( p_demux, "found text subtitles header" );
1025 p_stream->fmt.i_cat = SPU_ES;
1026 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1027 p_stream->f_rate = 1000; /* granulepos is in milisec */
1031 msg_Dbg( p_demux, "stream %d has a header marker "
1032 "but is of an unknown type", p_ogg->i_streams-1 );
1039 msg_Dbg( p_demux, "stream %d is of unknown type",
1040 p_ogg->i_streams-1 );
1045 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1046 return VLC_EGENERIC;
1049 /* This is the first data page, which means we are now finished
1050 * with the initial pages. We just need to store it in the relevant
1052 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1054 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1066 return VLC_EGENERIC;
1069 /****************************************************************************
1070 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1071 * Elementary streams.
1072 ****************************************************************************/
1073 static int Ogg_BeginningOfStream( demux_t *p_demux )
1075 demux_sys_t *p_ogg = p_demux->p_sys ;
1078 /* Find the logical streams embedded in the physical stream and
1079 * initialize our p_ogg structure. */
1080 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1082 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1083 return VLC_EGENERIC;
1086 p_ogg->i_bitrate = 0;
1088 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1090 #define p_stream p_ogg->pp_stream[i_stream]
1091 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1093 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1095 /* Set the CMML stream active */
1096 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1099 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1101 p_stream->i_pcr = p_stream->i_previous_pcr =
1102 p_stream->i_interpolated_pcr = -1;
1103 p_stream->b_reinit = 0;
1110 /****************************************************************************
1111 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1112 ****************************************************************************/
1113 static void Ogg_EndOfStream( demux_t *p_demux )
1115 demux_sys_t *p_ogg = p_demux->p_sys ;
1118 #define p_stream p_ogg->pp_stream[i_stream]
1119 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1121 if( p_stream->p_es )
1122 es_out_Del( p_demux->out, p_stream->p_es );
1124 p_ogg->i_bitrate -= p_stream->fmt.i_bitrate;
1126 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1127 if( p_ogg->pp_stream[i_stream]->p_headers)
1128 free( p_ogg->pp_stream[i_stream]->p_headers );
1130 es_format_Clean( &p_stream->fmt );
1132 free( p_ogg->pp_stream[i_stream] );
1137 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1138 p_ogg->pp_stream = NULL;
1139 p_ogg->i_streams = 0;
1142 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1143 ogg_packet *p_oggpacket )
1146 int i_fps_numerator;
1147 int i_fps_denominator;
1148 int i_keyframe_frequency_force;
1150 p_stream->fmt.i_cat = VIDEO_ES;
1151 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1153 /* Signal that we want to keep a backup of the theora
1154 * stream headers. They will be used when switching between
1156 p_stream->b_force_backup = 1;
1158 /* Cheat and get additionnal info ;) */
1159 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1160 bs_skip( &bitstream, 56 );
1161 bs_read( &bitstream, 8 ); /* major version num */
1162 bs_read( &bitstream, 8 ); /* minor version num */
1163 bs_read( &bitstream, 8 ); /* subminor version num */
1164 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1165 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1166 bs_read( &bitstream, 24 ); /* frame width */
1167 bs_read( &bitstream, 24 ); /* frame height */
1168 bs_read( &bitstream, 8 ); /* x offset */
1169 bs_read( &bitstream, 8 ); /* y offset */
1171 i_fps_numerator = bs_read( &bitstream, 32 );
1172 i_fps_denominator = bs_read( &bitstream, 32 );
1173 bs_read( &bitstream, 24 ); /* aspect_numerator */
1174 bs_read( &bitstream, 24 ); /* aspect_denominator */
1176 bs_read( &bitstream, 8 ); /* colorspace */
1177 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1178 bs_read( &bitstream, 6 ); /* quality */
1180 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1182 /* granule_shift = i_log( frequency_force -1 ) */
1183 p_stream->i_theora_keyframe_granule_shift = 0;
1184 i_keyframe_frequency_force--;
1185 while( i_keyframe_frequency_force )
1187 p_stream->i_theora_keyframe_granule_shift++;
1188 i_keyframe_frequency_force >>= 1;
1191 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1194 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1195 ogg_packet *p_oggpacket )
1199 p_stream->fmt.i_cat = AUDIO_ES;
1200 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1202 /* Signal that we want to keep a backup of the vorbis
1203 * stream headers. They will be used when switching between
1205 p_stream->b_force_backup = 1;
1207 /* Cheat and get additionnal info ;) */
1208 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1209 oggpack_adv( &opb, 88 );
1210 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1211 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1212 oggpack_read( &opb, 32 );
1213 oggpack_adv( &opb, 32 );
1214 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1217 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1218 ogg_packet *p_oggpacket )
1222 p_stream->fmt.i_cat = AUDIO_ES;
1223 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1225 /* Signal that we want to keep a backup of the speex
1226 * stream headers. They will be used when switching between
1228 p_stream->b_force_backup = 1;
1230 /* Cheat and get additionnal info ;) */
1231 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1232 oggpack_adv( &opb, 224 );
1233 oggpack_adv( &opb, 32 ); /* speex_version_id */
1234 oggpack_adv( &opb, 32 ); /* header_size */
1235 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1236 oggpack_adv( &opb, 32 ); /* mode */
1237 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1238 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1239 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1242 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1243 ogg_packet *p_oggpacket )
1245 /* Parse the STREAMINFO metadata */
1248 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1250 if( bs_read( &s, 7 ) == 0 )
1252 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1255 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1256 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1258 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1259 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1261 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1263 /* Fake this as the last metadata block */
1264 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1268 /* This ain't a STREAMINFO metadata */
1269 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1273 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1274 logical_stream_t *p_stream,
1275 ogg_packet *p_oggpacket )
1277 if( ! strncmp( &p_oggpacket->packet[0], "Annodex", 7 ) )
1281 uint16_t major_version;
1282 uint16_t minor_version;
1283 uint64_t timebase_numerator;
1284 uint64_t timebase_denominator;
1286 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1288 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1289 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1290 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1291 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1292 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1293 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1295 else if( ! strncmp( &p_oggpacket->packet[0], "AnxData", 7 ) )
1297 uint64_t granule_rate_numerator;
1298 uint64_t granule_rate_denominator;
1299 char content_type_string[1024];
1301 /* Read in Annodex header fields */
1303 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1304 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1305 p_stream->secondary_header_packets =
1306 GetDWLE( &p_oggpacket->packet[24] );
1308 msg_Dbg( p_this, "anxdata packet info: %qd/%qd, %d",
1309 granule_rate_numerator, granule_rate_denominator,
1310 p_stream->secondary_header_packets);
1312 /* we are guaranteed that the first header field will be
1313 * the content-type (by the Annodex standard) */
1314 sscanf( &p_oggpacket->packet[28], "Content-Type: %1024s\r\n",
1315 content_type_string );
1317 p_stream->f_rate = (float) granule_rate_numerator /
1318 (float) granule_rate_denominator;
1320 /* What type of file do we have?
1321 * strcmp is safe to use here because we've extracted
1322 * content_type_string from the stream manually */
1323 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1325 /* n.b. WAVs are unsupported right now */
1326 p_stream->fmt.i_cat = UNKNOWN_ES;
1328 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1330 p_stream->fmt.i_cat = AUDIO_ES;
1331 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1333 p_stream->b_force_backup = 1;
1335 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1337 p_stream->fmt.i_cat = VIDEO_ES;
1338 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1340 p_stream->b_force_backup = 1;
1342 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1344 p_stream->fmt.i_cat = VIDEO_ES;
1345 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1347 p_stream->b_force_backup = 1;
1349 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1351 /* n.b. MPEG streams are unsupported right now */
1352 p_stream->fmt.i_cat = VIDEO_ES;
1353 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1355 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1357 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1358 p_stream->fmt.i_cat = SPU_ES;
1359 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );