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_category( CAT_INPUT );
45 set_subcategory( SUBCAT_INPUT_DEMUX );
46 set_capability( "demux2", 50 );
47 set_callbacks( Open, Close );
48 add_shortcut( "ogg" );
52 /*****************************************************************************
53 * Definitions of structures and functions used by this plugins
54 *****************************************************************************/
55 typedef struct logical_stream_s
57 ogg_stream_state os; /* logical stream of packets */
65 /* the header of some logical streams (eg vorbis) contain essential
66 * data for the decoder. We back them up here in case we need to re-feed
67 * them to the decoder. */
73 /* program clock reference (in units of 90kHz) derived from the previous
76 mtime_t i_interpolated_pcr;
77 mtime_t i_previous_pcr;
81 int i_theora_keyframe_granule_shift;
83 /* for Annodex logical bitstreams */
84 int secondary_header_packets;
90 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
92 int i_streams; /* number of logical bitstreams */
93 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
95 /* program clock reference (in units of 90kHz) derived from the pcr of
106 /* OggDS headers for the new header format (used in ogm files) */
107 typedef struct stream_header_video
111 } stream_header_video;
113 typedef struct stream_header_audio
115 ogg_int16_t channels;
116 ogg_int16_t blockalign;
117 ogg_int32_t avgbytespersec;
118 } stream_header_audio;
120 typedef struct stream_header
125 ogg_int32_t size; /* size of the structure */
127 ogg_int64_t time_unit; /* in reference time */
128 ogg_int64_t samples_per_unit;
129 ogg_int32_t default_len; /* in media time */
131 ogg_int32_t buffersize;
132 ogg_int16_t bits_per_sample;
137 stream_header_video video;
139 stream_header_audio audio;
143 #define OGG_BLOCK_SIZE 4096
145 /* Some defines from OggDS */
146 #define PACKET_TYPE_HEADER 0x01
147 #define PACKET_TYPE_BITS 0x07
148 #define PACKET_LEN_BITS01 0xc0
149 #define PACKET_LEN_BITS2 0x02
150 #define PACKET_IS_SYNCPOINT 0x08
152 /*****************************************************************************
154 *****************************************************************************/
155 static int Demux ( demux_t * );
156 static int Control( demux_t *, int, va_list );
158 /* Bitstream manipulation */
159 static int Ogg_ReadPage ( demux_t *, ogg_page * );
160 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
161 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
163 static int Ogg_BeginningOfStream( demux_t *p_demux );
164 static int Ogg_FindLogicalStreams( demux_t *p_demux );
165 static void Ogg_EndOfStream( demux_t *p_demux );
167 /* Logical bitstream headers */
168 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
169 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
170 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
171 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
172 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
174 /*****************************************************************************
175 * Open: initializes ogg demux structures
176 *****************************************************************************/
177 static int Open( vlc_object_t * p_this )
179 demux_t *p_demux = (demux_t *)p_this;
184 /* Check if we are dealing with an ogg stream */
185 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
187 msg_Err( p_demux, "cannot peek" );
190 if( strcmp( p_demux->psz_demux, "ogg" ) && strncmp( p_peek, "OggS", 4 ) )
192 msg_Warn( p_demux, "ogg module discarded (invalid header)" );
196 /* Set exported functions */
197 p_demux->pf_demux = Demux;
198 p_demux->pf_control = Control;
199 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
201 memset( p_sys, 0, sizeof( demux_sys_t ) );
202 p_sys->i_bitrate = 0;
203 p_sys->pp_stream = NULL;
205 /* Begnning of stream, tell the demux to look for elementary streams. */
208 /* Initialize the Ogg physical bitstream parser */
209 ogg_sync_init( &p_sys->oy );
214 /*****************************************************************************
215 * Close: frees unused data
216 *****************************************************************************/
217 static void Close( vlc_object_t *p_this )
219 demux_t *p_demux = (demux_t *)p_this;
220 demux_sys_t *p_sys = p_demux->p_sys ;
222 /* Cleanup the bitstream parser */
223 ogg_sync_clear( &p_sys->oy );
225 Ogg_EndOfStream( p_demux );
230 /*****************************************************************************
231 * Demux: reads and demuxes data packets
232 *****************************************************************************
233 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
234 *****************************************************************************/
235 static int Demux( demux_t * p_demux )
237 demux_sys_t *p_sys = p_demux->p_sys;
239 ogg_packet oggpacket;
243 if( p_sys->i_eos == p_sys->i_streams )
247 msg_Dbg( p_demux, "end of a group of logical streams" );
248 Ogg_EndOfStream( p_demux );
252 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS ) return 0;
254 msg_Dbg( p_demux, "beginning of a group of logical streams" );
255 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
259 * Demux an ogg page from the stream
261 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
266 /* Test for End of Stream */
267 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
270 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
272 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
274 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
277 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
279 /* Read info from any secondary header packets, if there are any */
280 if( p_stream->secondary_header_packets > 0 )
282 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
283 oggpacket.bytes >= 7 &&
284 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
286 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
287 p_stream->secondary_header_packets = 0;
289 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
290 oggpacket.bytes >= 7 &&
291 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
293 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
294 p_stream->secondary_header_packets = 0;
296 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
298 p_stream->secondary_header_packets = 0;
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;
482 mtime_t i_pts = -1, i_interpolated_pts;
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 i_interpolated_pts = p_stream->i_interpolated_pcr;
592 Ogg_UpdatePCR( p_stream, p_oggpacket );
594 if( p_stream->i_pcr >= 0 )
596 /* This is for streams where the granulepos of the header packets
597 * doesn't match these of the data packets (eg. ogg web radios). */
598 if( p_stream->i_previous_pcr == 0 &&
599 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
601 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
603 /* Call the pace control */
604 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
608 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
609 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
610 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
611 p_stream->i_pcr >= 0 )
613 p_stream->i_previous_pcr = p_stream->i_pcr;
615 /* The granulepos is the start date of the sample */
616 i_pts = p_stream->i_pcr;
621 /* This stream isn't currently selected so we don't need to decode it,
622 * but we did need to store its pcr as it might be selected later on */
626 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
629 if( i_pts == 0 ) i_pts = 1;
630 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
631 else if( i_pts == -1 ) i_pts = 0;
633 if( p_stream->fmt.i_cat == AUDIO_ES )
634 p_block->i_dts = p_block->i_pts = i_pts;
635 else if( p_stream->fmt.i_cat == SPU_ES )
637 p_block->i_dts = p_block->i_pts = i_pts;
638 p_block->i_length = 0;
640 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
641 p_block->i_dts = p_block->i_pts = i_pts;
644 p_block->i_dts = i_pts;
648 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
649 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
650 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
651 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
652 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
653 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) )
655 /* We remove the header from the packet */
656 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
657 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
659 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
661 /* But with subtitles we need to retrieve the duration first */
664 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
666 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
668 lenbytes = lenbytes << 8;
669 lenbytes += *(p_oggpacket->packet + i_header_len - i);
672 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
673 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
674 p_oggpacket->packet[i_header_len + 1] != 0 &&
675 p_oggpacket->packet[i_header_len + 1] != '\n' &&
676 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
678 p_block->i_length = (mtime_t)lenbytes * 1000;
683 p_block->i_buffer -= i_header_len;
686 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
688 /* FIXME: the biggest hack I've ever done */
689 msg_Warn( p_demux, "tarkin pts: "I64Fd", granule: "I64Fd,
690 p_block->i_pts, p_block->i_dts );
694 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
695 p_oggpacket->bytes - i_header_len );
697 es_out_Send( p_demux->out, p_stream->p_es, p_block );
700 /****************************************************************************
701 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
702 * stream and fill p_ogg.
703 *****************************************************************************
704 * The initial page of a logical stream is marked as a 'bos' page.
705 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
706 * together and all of the initial pages must appear before any data pages.
708 * On success this function returns VLC_SUCCESS.
709 ****************************************************************************/
710 static int Ogg_FindLogicalStreams( demux_t *p_demux )
712 demux_sys_t *p_ogg = p_demux->p_sys ;
713 ogg_packet oggpacket;
717 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
719 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
721 if( ogg_page_bos( &oggpage ) )
724 /* All is wonderful in our fine fine little world.
725 * We found the beginning of our first logical stream. */
726 while( ogg_page_bos( &oggpage ) )
730 realloc( p_ogg->pp_stream, p_ogg->i_streams *
731 sizeof(logical_stream_t *) );
733 p_stream = malloc( sizeof(logical_stream_t) );
734 memset( p_stream, 0, sizeof(logical_stream_t) );
735 p_stream->p_headers = 0;
736 p_stream->secondary_header_packets = 0;
738 es_format_Init( &p_stream->fmt, 0, 0 );
740 /* Setup the logical stream */
741 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
742 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
744 /* Extract the initial header from the first page and verify
745 * the codec type of tis Ogg bitstream */
746 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
748 /* error. stream version mismatch perhaps */
749 msg_Err( p_demux, "error reading first page of "
750 "Ogg bitstream data" );
754 /* FIXME: check return value */
755 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
757 /* Check for Vorbis header */
758 if( oggpacket.bytes >= 7 &&
759 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
761 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
762 msg_Dbg( p_demux, "found vorbis header" );
764 /* Check for Speex header */
765 else if( oggpacket.bytes >= 7 &&
766 ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
768 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
769 msg_Dbg( p_demux, "found speex header, channels: %i, "
770 "rate: %i, bitrate: %i",
771 p_stream->fmt.audio.i_channels,
772 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
774 /* Check for Flac header */
775 else if( oggpacket.bytes >= 4 &&
776 ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
778 msg_Dbg( p_demux, "found FLAC header" );
780 /* Grrrr!!!! Did they really have to put all the
781 * important info in the second header packet!!!
782 * (STREAMINFO metadata is in the following packet) */
783 p_stream->b_force_backup = 1;
785 p_stream->fmt.i_cat = AUDIO_ES;
786 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
788 /* Check for Theora header */
789 else if( oggpacket.bytes >= 7 &&
790 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
792 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
795 "found theora header, bitrate: %i, rate: %f",
796 p_stream->fmt.i_bitrate, p_stream->f_rate );
798 /* Check for Tarkin header */
799 else if( oggpacket.bytes >= 7 &&
800 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
804 msg_Dbg( p_demux, "found tarkin header" );
805 p_stream->fmt.i_cat = VIDEO_ES;
806 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
808 /* Cheat and get additionnal info ;) */
809 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
810 oggpack_adv( &opb, 88 );
811 oggpack_adv( &opb, 104 );
812 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
813 p_stream->f_rate = 2; /* FIXME */
815 "found tarkin header, bitrate: %i, rate: %f",
816 p_stream->fmt.i_bitrate, p_stream->f_rate );
818 /* Check for Annodex header */
819 else if( oggpacket.bytes >= 7 &&
820 ! strncmp( &oggpacket.packet[0], "Annodex", 7 ) )
822 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
824 /* kill annodex track */
828 /* Check for Annodex header */
829 else if( oggpacket.bytes >= 7 &&
830 ! strncmp( &oggpacket.packet[0], "AnxData", 7 ) )
832 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
835 else if( oggpacket.bytes >= 142 &&
836 !strncmp( &oggpacket.packet[1],
837 "Direct Show Samples embedded in Ogg", 35 ))
839 /* Old header type */
841 /* Check for video header (old format) */
842 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
843 oggpacket.bytes >= 184 )
845 p_stream->fmt.i_cat = VIDEO_ES;
846 p_stream->fmt.i_codec =
847 VLC_FOURCC( oggpacket.packet[68],
848 oggpacket.packet[69],
849 oggpacket.packet[70],
850 oggpacket.packet[71] );
851 msg_Dbg( p_demux, "found video header of type: %.4s",
852 (char *)&p_stream->fmt.i_codec );
854 p_stream->f_rate = 10000000.0 /
855 GetQWLE((oggpacket.packet+164));
856 p_stream->fmt.video.i_bits_per_pixel =
857 GetWLE((oggpacket.packet+182));
858 if( !p_stream->fmt.video.i_bits_per_pixel )
860 p_stream->fmt.video.i_bits_per_pixel = 24;
861 p_stream->fmt.video.i_width =
862 GetDWLE((oggpacket.packet+176));
863 p_stream->fmt.video.i_height =
864 GetDWLE((oggpacket.packet+180));
867 "fps: %f, width:%i; height:%i, bitcount:%i",
869 p_stream->fmt.video.i_width,
870 p_stream->fmt.video.i_height,
871 p_stream->fmt.video.i_bits_per_pixel);
874 /* Check for audio header (old format) */
875 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
877 unsigned int i_extra_size;
878 unsigned int i_format_tag;
880 p_stream->fmt.i_cat = AUDIO_ES;
882 i_extra_size = GetWLE((oggpacket.packet+140));
885 p_stream->fmt.i_extra = i_extra_size;
886 p_stream->fmt.p_extra = malloc( i_extra_size );
887 memcpy( p_stream->fmt.p_extra,
888 oggpacket.packet + 142, i_extra_size );
891 i_format_tag = GetWLE((oggpacket.packet+124));
892 p_stream->fmt.audio.i_channels =
893 GetWLE((oggpacket.packet+126));
894 p_stream->f_rate = p_stream->fmt.audio.i_rate =
895 GetDWLE((oggpacket.packet+128));
896 p_stream->fmt.i_bitrate =
897 GetDWLE((oggpacket.packet+132)) * 8;
898 p_stream->fmt.audio.i_blockalign =
899 GetWLE((oggpacket.packet+136));
900 p_stream->fmt.audio.i_bitspersample =
901 GetWLE((oggpacket.packet+138));
903 wf_tag_to_fourcc( i_format_tag,
904 &p_stream->fmt.i_codec, 0 );
906 if( p_stream->fmt.i_codec ==
907 VLC_FOURCC('u','n','d','f') )
909 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
910 ( i_format_tag >> 8 ) & 0xff,
911 i_format_tag & 0xff );
914 msg_Dbg( p_demux, "found audio header of type: %.4s",
915 (char *)&p_stream->fmt.i_codec );
916 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
917 "%dbits/sample %dkb/s",
919 p_stream->fmt.audio.i_channels,
920 p_stream->fmt.audio.i_rate,
921 p_stream->fmt.audio.i_bitspersample,
922 p_stream->fmt.i_bitrate / 1024 );
927 msg_Dbg( p_demux, "stream %d has an old header "
928 "but is of an unknown type", p_ogg->i_streams-1 );
933 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
934 == PACKET_TYPE_HEADER &&
935 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
937 stream_header *st = (stream_header *)(oggpacket.packet+1);
939 /* Check for video header (new format) */
940 if( !strncmp( st->streamtype, "video", 5 ) )
942 p_stream->fmt.i_cat = VIDEO_ES;
944 /* We need to get rid of the header packet */
945 ogg_stream_packetout( &p_stream->os, &oggpacket );
947 p_stream->fmt.i_codec =
948 VLC_FOURCC( st->subtype[0], st->subtype[1],
949 st->subtype[2], st->subtype[3] );
950 msg_Dbg( p_demux, "found video header of type: %.4s",
951 (char *)&p_stream->fmt.i_codec );
953 p_stream->f_rate = 10000000.0 /
954 GetQWLE(&st->time_unit);
955 p_stream->fmt.video.i_bits_per_pixel =
956 GetWLE(&st->bits_per_sample);
957 p_stream->fmt.video.i_width =
958 GetDWLE(&st->sh.video.width);
959 p_stream->fmt.video.i_height =
960 GetDWLE(&st->sh.video.height);
963 "fps: %f, width:%i; height:%i, bitcount:%i",
965 p_stream->fmt.video.i_width,
966 p_stream->fmt.video.i_height,
967 p_stream->fmt.video.i_bits_per_pixel );
969 /* Check for audio header (new format) */
970 else if( !strncmp( st->streamtype, "audio", 5 ) )
975 p_stream->fmt.i_cat = AUDIO_ES;
977 /* We need to get rid of the header packet */
978 ogg_stream_packetout( &p_stream->os, &oggpacket );
980 p_stream->fmt.i_extra = GetQWLE(&st->size) -
981 sizeof(stream_header);
982 if( p_stream->fmt.i_extra )
984 p_stream->fmt.p_extra =
985 malloc( p_stream->fmt.i_extra );
986 memcpy( p_stream->fmt.p_extra, st + 1,
987 p_stream->fmt.i_extra );
990 memcpy( p_buffer, st->subtype, 4 );
992 i_format_tag = strtol(p_buffer,NULL,16);
993 p_stream->fmt.audio.i_channels =
994 GetWLE(&st->sh.audio.channels);
995 p_stream->f_rate = p_stream->fmt.audio.i_rate =
996 GetQWLE(&st->samples_per_unit);
997 p_stream->fmt.i_bitrate =
998 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
999 p_stream->fmt.audio.i_blockalign =
1000 GetWLE(&st->sh.audio.blockalign);
1001 p_stream->fmt.audio.i_bitspersample =
1002 GetWLE(&st->bits_per_sample);
1004 wf_tag_to_fourcc( i_format_tag,
1005 &p_stream->fmt.i_codec, 0 );
1007 if( p_stream->fmt.i_codec ==
1008 VLC_FOURCC('u','n','d','f') )
1010 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1011 ( i_format_tag >> 8 ) & 0xff,
1012 i_format_tag & 0xff );
1015 msg_Dbg( p_demux, "found audio header of type: %.4s",
1016 (char *)&p_stream->fmt.i_codec );
1017 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1018 "%dbits/sample %dkb/s",
1020 p_stream->fmt.audio.i_channels,
1021 p_stream->fmt.audio.i_rate,
1022 p_stream->fmt.audio.i_bitspersample,
1023 p_stream->fmt.i_bitrate / 1024 );
1025 /* Check for text (subtitles) header */
1026 else if( !strncmp(st->streamtype, "text", 4) )
1028 /* We need to get rid of the header packet */
1029 ogg_stream_packetout( &p_stream->os, &oggpacket );
1031 msg_Dbg( p_demux, "found text subtitles header" );
1032 p_stream->fmt.i_cat = SPU_ES;
1033 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1034 p_stream->f_rate = 1000; /* granulepos is in milisec */
1038 msg_Dbg( p_demux, "stream %d has a header marker "
1039 "but is of an unknown type", p_ogg->i_streams-1 );
1046 msg_Dbg( p_demux, "stream %d is of unknown type",
1047 p_ogg->i_streams-1 );
1052 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1053 return VLC_EGENERIC;
1056 /* This is the first data page, which means we are now finished
1057 * with the initial pages. We just need to store it in the relevant
1059 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1061 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1073 return VLC_EGENERIC;
1076 /****************************************************************************
1077 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1078 * Elementary streams.
1079 ****************************************************************************/
1080 static int Ogg_BeginningOfStream( demux_t *p_demux )
1082 demux_sys_t *p_ogg = p_demux->p_sys ;
1085 /* Find the logical streams embedded in the physical stream and
1086 * initialize our p_ogg structure. */
1087 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1089 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1090 return VLC_EGENERIC;
1093 p_ogg->i_bitrate = 0;
1095 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1097 #define p_stream p_ogg->pp_stream[i_stream]
1098 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1100 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1102 /* Set the CMML stream active */
1103 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1106 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1108 p_stream->i_pcr = p_stream->i_previous_pcr =
1109 p_stream->i_interpolated_pcr = -1;
1110 p_stream->b_reinit = 0;
1117 /****************************************************************************
1118 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1119 ****************************************************************************/
1120 static void Ogg_EndOfStream( demux_t *p_demux )
1122 demux_sys_t *p_ogg = p_demux->p_sys ;
1125 #define p_stream p_ogg->pp_stream[i_stream]
1126 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1128 if( p_stream->p_es )
1129 es_out_Del( p_demux->out, p_stream->p_es );
1131 p_ogg->i_bitrate -= p_stream->fmt.i_bitrate;
1133 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1134 if( p_ogg->pp_stream[i_stream]->p_headers)
1135 free( p_ogg->pp_stream[i_stream]->p_headers );
1137 es_format_Clean( &p_stream->fmt );
1139 free( p_ogg->pp_stream[i_stream] );
1144 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1145 p_ogg->pp_stream = NULL;
1146 p_ogg->i_streams = 0;
1149 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1150 ogg_packet *p_oggpacket )
1153 int i_fps_numerator;
1154 int i_fps_denominator;
1155 int i_keyframe_frequency_force;
1157 p_stream->fmt.i_cat = VIDEO_ES;
1158 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1160 /* Signal that we want to keep a backup of the theora
1161 * stream headers. They will be used when switching between
1163 p_stream->b_force_backup = 1;
1165 /* Cheat and get additionnal info ;) */
1166 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1167 bs_skip( &bitstream, 56 );
1168 bs_read( &bitstream, 8 ); /* major version num */
1169 bs_read( &bitstream, 8 ); /* minor version num */
1170 bs_read( &bitstream, 8 ); /* subminor version num */
1171 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1172 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1173 bs_read( &bitstream, 24 ); /* frame width */
1174 bs_read( &bitstream, 24 ); /* frame height */
1175 bs_read( &bitstream, 8 ); /* x offset */
1176 bs_read( &bitstream, 8 ); /* y offset */
1178 i_fps_numerator = bs_read( &bitstream, 32 );
1179 i_fps_denominator = bs_read( &bitstream, 32 );
1180 bs_read( &bitstream, 24 ); /* aspect_numerator */
1181 bs_read( &bitstream, 24 ); /* aspect_denominator */
1183 bs_read( &bitstream, 8 ); /* colorspace */
1184 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1185 bs_read( &bitstream, 6 ); /* quality */
1187 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1189 /* granule_shift = i_log( frequency_force -1 ) */
1190 p_stream->i_theora_keyframe_granule_shift = 0;
1191 i_keyframe_frequency_force--;
1192 while( i_keyframe_frequency_force )
1194 p_stream->i_theora_keyframe_granule_shift++;
1195 i_keyframe_frequency_force >>= 1;
1198 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1201 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1202 ogg_packet *p_oggpacket )
1206 p_stream->fmt.i_cat = AUDIO_ES;
1207 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1209 /* Signal that we want to keep a backup of the vorbis
1210 * stream headers. They will be used when switching between
1212 p_stream->b_force_backup = 1;
1214 /* Cheat and get additionnal info ;) */
1215 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1216 oggpack_adv( &opb, 88 );
1217 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1218 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1219 oggpack_read( &opb, 32 );
1220 oggpack_adv( &opb, 32 );
1221 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1224 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1225 ogg_packet *p_oggpacket )
1229 p_stream->fmt.i_cat = AUDIO_ES;
1230 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1232 /* Signal that we want to keep a backup of the speex
1233 * stream headers. They will be used when switching between
1235 p_stream->b_force_backup = 1;
1237 /* Cheat and get additionnal info ;) */
1238 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1239 oggpack_adv( &opb, 224 );
1240 oggpack_adv( &opb, 32 ); /* speex_version_id */
1241 oggpack_adv( &opb, 32 ); /* header_size */
1242 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1243 oggpack_adv( &opb, 32 ); /* mode */
1244 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1245 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1246 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1249 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1250 ogg_packet *p_oggpacket )
1252 /* Parse the STREAMINFO metadata */
1255 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1257 if( bs_read( &s, 7 ) == 0 )
1259 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1262 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1263 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1265 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1266 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1268 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1270 /* Fake this as the last metadata block */
1271 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1275 /* This ain't a STREAMINFO metadata */
1276 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1280 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1281 logical_stream_t *p_stream,
1282 ogg_packet *p_oggpacket )
1284 if( ! strncmp( &p_oggpacket->packet[0], "Annodex", 7 ) )
1288 uint16_t major_version;
1289 uint16_t minor_version;
1290 uint64_t timebase_numerator;
1291 uint64_t timebase_denominator;
1293 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1295 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1296 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1297 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1298 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1299 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1300 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1302 else if( ! strncmp( &p_oggpacket->packet[0], "AnxData", 7 ) )
1304 uint64_t granule_rate_numerator;
1305 uint64_t granule_rate_denominator;
1306 char content_type_string[1024];
1308 /* Read in Annodex header fields */
1310 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1311 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1312 p_stream->secondary_header_packets =
1313 GetDWLE( &p_oggpacket->packet[24] );
1315 /* we are guaranteed that the first header field will be
1316 * the content-type (by the Annodex standard) */
1317 if( !strncasecmp( &p_oggpacket->packet[28], "Content-Type: ", 14 ) )
1319 sscanf( &p_oggpacket->packet[42], "%1024s\r\n",
1320 content_type_string );
1323 msg_Dbg( p_this, "AnxData packet info: "I64Fd" / "I64Fd", %d, ``%s''",
1324 granule_rate_numerator, granule_rate_denominator,
1325 p_stream->secondary_header_packets, content_type_string );
1327 p_stream->f_rate = (float) granule_rate_numerator /
1328 (float) granule_rate_denominator;
1330 /* What type of file do we have?
1331 * strcmp is safe to use here because we've extracted
1332 * content_type_string from the stream manually */
1333 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1335 /* n.b. WAVs are unsupported right now */
1336 p_stream->fmt.i_cat = UNKNOWN_ES;
1338 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1340 p_stream->fmt.i_cat = AUDIO_ES;
1341 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1343 p_stream->b_force_backup = 1;
1345 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1347 p_stream->fmt.i_cat = AUDIO_ES;
1348 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1350 p_stream->b_force_backup = 1;
1352 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1354 p_stream->fmt.i_cat = VIDEO_ES;
1355 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1357 p_stream->b_force_backup = 1;
1359 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1361 p_stream->fmt.i_cat = VIDEO_ES;
1362 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1364 p_stream->b_force_backup = 1;
1366 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1368 /* n.b. MPEG streams are unsupported right now */
1369 p_stream->fmt.i_cat = VIDEO_ES;
1370 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1372 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1374 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1375 p_stream->fmt.i_cat = SPU_ES;
1376 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );