1 /*****************************************************************************
2 * ogg.c : ogg stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Andre Pang <Andre.Pang@csiro.au> (Annodex support)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 #include <vlc_input.h>
40 #include <vlc_codecs.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_shortname ( "OGG" );
51 set_description( N_("OGG demuxer" ) );
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_DEMUX );
54 set_capability( "demux", 50 );
55 set_callbacks( Open, Close );
56 add_shortcut( "ogg" );
60 /*****************************************************************************
61 * Definitions of structures and functions used by this plugins
62 *****************************************************************************/
63 typedef struct logical_stream_s
65 ogg_stream_state os; /* logical stream of packets */
73 /* the header of some logical streams (eg vorbis) contain essential
74 * data for the decoder. We back them up here in case we need to re-feed
75 * them to the decoder. */
81 /* program clock reference (in units of 90kHz) derived from the previous
84 mtime_t i_interpolated_pcr;
85 mtime_t i_previous_pcr;
91 /* kate streams have the number of headers in the ID header */
92 int i_kate_num_headers;
94 /* for Annodex logical bitstreams */
95 int secondary_header_packets;
101 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
103 int i_streams; /* number of logical bitstreams */
104 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
106 /* program clock reference (in units of 90kHz) derived from the pcr of
117 /* OggDS headers for the new header format (used in ogm files) */
118 typedef struct stream_header_video
122 } stream_header_video;
124 typedef struct stream_header_audio
126 ogg_int16_t channels;
127 ogg_int16_t blockalign;
128 ogg_int32_t avgbytespersec;
129 } stream_header_audio;
131 typedef struct stream_header
136 ogg_int32_t size; /* size of the structure */
138 ogg_int64_t time_unit; /* in reference time */
139 ogg_int64_t samples_per_unit;
140 ogg_int32_t default_len; /* in media time */
142 ogg_int32_t buffersize;
143 ogg_int16_t bits_per_sample;
148 stream_header_video video;
150 stream_header_audio audio;
154 #define OGG_BLOCK_SIZE 4096
156 /* Some defines from OggDS */
157 #define PACKET_TYPE_HEADER 0x01
158 #define PACKET_TYPE_BITS 0x07
159 #define PACKET_LEN_BITS01 0xc0
160 #define PACKET_LEN_BITS2 0x02
161 #define PACKET_IS_SYNCPOINT 0x08
163 /*****************************************************************************
165 *****************************************************************************/
166 static int Demux ( demux_t * );
167 static int Control( demux_t *, int, va_list );
169 /* Bitstream manipulation */
170 static int Ogg_ReadPage ( demux_t *, ogg_page * );
171 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
172 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
174 static int Ogg_BeginningOfStream( demux_t *p_demux );
175 static int Ogg_FindLogicalStreams( demux_t *p_demux );
176 static void Ogg_EndOfStream( demux_t *p_demux );
178 /* Logical bitstream headers */
179 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
180 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
181 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
182 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
183 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
184 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
186 /*****************************************************************************
187 * Open: initializes ogg demux structures
188 *****************************************************************************/
189 static int Open( vlc_object_t * p_this )
191 demux_t *p_demux = (demux_t *)p_this;
193 const uint8_t *p_peek;
196 /* Check if we are dealing with an ogg stream */
197 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
198 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
203 /* Set exported functions */
204 p_demux->pf_demux = Demux;
205 p_demux->pf_control = Control;
206 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
208 memset( p_sys, 0, sizeof( demux_sys_t ) );
209 p_sys->i_bitrate = 0;
210 p_sys->pp_stream = NULL;
212 /* Begnning of stream, tell the demux to look for elementary streams. */
215 /* Initialize the Ogg physical bitstream parser */
216 ogg_sync_init( &p_sys->oy );
221 /*****************************************************************************
222 * Close: frees unused data
223 *****************************************************************************/
224 static void Close( vlc_object_t *p_this )
226 demux_t *p_demux = (demux_t *)p_this;
227 demux_sys_t *p_sys = p_demux->p_sys ;
229 /* Cleanup the bitstream parser */
230 ogg_sync_clear( &p_sys->oy );
232 Ogg_EndOfStream( p_demux );
237 /*****************************************************************************
238 * Demux: reads and demuxes data packets
239 *****************************************************************************
240 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
241 *****************************************************************************/
242 static int Demux( demux_t * p_demux )
244 demux_sys_t *p_sys = p_demux->p_sys;
246 ogg_packet oggpacket;
250 if( p_sys->i_eos == p_sys->i_streams )
254 msg_Dbg( p_demux, "end of a group of logical streams" );
255 Ogg_EndOfStream( p_demux );
259 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS ) return 0;
261 msg_Dbg( p_demux, "beginning of a group of logical streams" );
262 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
266 * Demux an ogg page from the stream
268 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
273 /* Test for End of Stream */
274 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
277 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
279 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
281 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
284 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
286 /* Read info from any secondary header packets, if there are any */
287 if( p_stream->secondary_header_packets > 0 )
289 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
290 oggpacket.bytes >= 7 &&
291 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
293 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
294 p_stream->secondary_header_packets = 0;
296 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
297 oggpacket.bytes >= 7 &&
298 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
300 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
301 p_stream->secondary_header_packets = 0;
303 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
305 p_stream->secondary_header_packets = 0;
309 if( p_stream->b_reinit )
311 /* If synchro is re-initialized we need to drop all the packets
312 * until we find a new dated one. */
313 Ogg_UpdatePCR( p_stream, &oggpacket );
315 if( p_stream->i_pcr >= 0 )
317 p_stream->b_reinit = 0;
321 p_stream->i_interpolated_pcr = -1;
325 /* An Ogg/vorbis packet contains an end date granulepos */
326 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
327 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
328 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
330 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
332 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
336 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
343 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
348 i_stream = 0; p_sys->i_pcr = -1;
349 for( ; i_stream < p_sys->i_streams; i_stream++ )
351 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
353 if( p_stream->fmt.i_cat == SPU_ES )
355 if( p_stream->i_interpolated_pcr < 0 )
358 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
359 p_sys->i_pcr = p_stream->i_interpolated_pcr;
362 if( p_sys->i_pcr >= 0 )
364 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
371 /*****************************************************************************
373 *****************************************************************************/
374 static int Control( demux_t *p_demux, int i_query, va_list args )
376 demux_sys_t *p_sys = p_demux->p_sys;
383 case DEMUX_HAS_UNSUPPORTED_META:
384 pb_bool = (bool*)va_arg( args, bool* );
389 pi64 = (int64_t*)va_arg( args, int64_t * );
390 *pi64 = p_sys->i_pcr;
396 case DEMUX_SET_POSITION:
397 for( i = 0; i < p_sys->i_streams; i++ )
399 logical_stream_t *p_stream = p_sys->pp_stream[i];
401 /* we'll trash all the data until we find the next pcr */
402 p_stream->b_reinit = 1;
403 p_stream->i_pcr = -1;
404 p_stream->i_interpolated_pcr = -1;
405 ogg_stream_reset( &p_stream->os );
407 ogg_sync_reset( &p_sys->oy );
410 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
415 /****************************************************************************
416 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
417 ****************************************************************************
418 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
419 * are at the end of stream.
420 ****************************************************************************/
421 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
423 demux_sys_t *p_ogg = p_demux->p_sys ;
427 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
429 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
431 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
435 ogg_sync_wrote( &p_ogg->oy, i_read );
441 /****************************************************************************
442 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
444 ****************************************************************************/
445 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
446 ogg_packet *p_oggpacket )
448 /* Convert the granulepos into a pcr */
449 if( p_oggpacket->granulepos >= 0 )
451 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
452 p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
454 ogg_int64_t iframe = p_oggpacket->granulepos >>
455 p_stream->i_granule_shift;
456 ogg_int64_t pframe = p_oggpacket->granulepos -
457 ( iframe << p_stream->i_granule_shift );
459 p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
464 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
468 p_stream->i_interpolated_pcr = p_stream->i_pcr;
472 p_stream->i_pcr = -1;
474 /* no granulepos available, try to interpolate the pcr.
475 * If we can't then don't touch the old value. */
476 if( p_stream->fmt.i_cat == VIDEO_ES )
477 /* 1 frame per packet */
478 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
479 else if( p_stream->fmt.i_bitrate )
480 p_stream->i_interpolated_pcr +=
481 ( p_oggpacket->bytes * INT64_C(1000000) /
482 p_stream->fmt.i_bitrate / 8 );
486 /****************************************************************************
487 * Ogg_DecodePacket: Decode an Ogg packet.
488 ****************************************************************************/
489 static void Ogg_DecodePacket( demux_t *p_demux,
490 logical_stream_t *p_stream,
491 ogg_packet *p_oggpacket )
495 int i_header_len = 0;
496 mtime_t i_pts = -1, i_interpolated_pts;
499 if( !p_oggpacket->bytes )
501 msg_Dbg( p_demux, "discarding 0 sized packet" );
505 if( p_oggpacket->bytes >= 7 &&
506 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
508 /* it's an Annodex packet -- skip it (do nothing) */
511 else if( p_oggpacket->bytes >= 7 &&
512 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
514 /* it's an AnxData packet -- skip it (do nothing) */
518 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
519 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
521 /* Check the ES is selected */
522 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
523 p_stream->p_es, &b_selected );
525 if( p_stream->b_force_backup )
528 bool b_store_size = true;
529 bool b_store_num_headers = false;
531 p_stream->i_packets_backup++;
532 switch( p_stream->fmt.i_codec )
534 case VLC_FOURCC( 'v','o','r','b' ):
535 case VLC_FOURCC( 's','p','x',' ' ):
536 case VLC_FOURCC( 't','h','e','o' ):
537 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
540 case VLC_FOURCC( 'f','l','a','c' ):
541 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
543 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
544 p_stream->b_force_backup = 0;
546 else if( p_stream->fmt.audio.i_rate )
548 p_stream->b_force_backup = 0;
549 if( p_oggpacket->bytes >= 9 )
551 p_oggpacket->packet += 9;
552 p_oggpacket->bytes -= 9;
555 b_store_size = false;
558 case VLC_FOURCC( 'k','a','t','e' ):
559 if( p_stream->i_packets_backup == 1)
560 b_store_num_headers = true;
561 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
565 p_stream->b_force_backup = 0;
569 /* Backup the ogg packet (likely an header packet) */
570 p_stream->p_headers =
571 realloc( p_stream->p_headers, p_stream->i_headers +
572 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
573 p_extra = p_stream->p_headers + p_stream->i_headers;
574 if( b_store_num_headers )
576 /* Kate streams store the number of headers in the first header,
577 so we can't just test for 3 as Vorbis/Theora */
578 *(p_extra++) = p_stream->i_kate_num_headers;
582 *(p_extra++) = p_oggpacket->bytes >> 8;
583 *(p_extra++) = p_oggpacket->bytes & 0xFF;
585 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
586 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
588 if( !p_stream->b_force_backup )
590 /* Last header received, commit changes */
591 p_stream->fmt.i_extra = p_stream->i_headers;
592 p_stream->fmt.p_extra =
593 realloc( p_stream->fmt.p_extra, p_stream->i_headers );
594 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
595 p_stream->i_headers );
596 es_out_Control( p_demux->out, ES_OUT_SET_FMT,
597 p_stream->p_es, &p_stream->fmt );
600 b_selected = false; /* Discard the header packet */
603 /* Convert the pcr into a pts */
604 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
605 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
606 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
608 if( p_stream->i_pcr >= 0 )
610 /* This is for streams where the granulepos of the header packets
611 * doesn't match these of the data packets (eg. ogg web radios). */
612 if( p_stream->i_previous_pcr == 0 &&
613 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
615 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
617 /* Call the pace control */
618 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
622 p_stream->i_previous_pcr = p_stream->i_pcr;
624 /* The granulepos is the end date of the sample */
625 i_pts = p_stream->i_pcr;
629 /* Convert the granulepos into the next pcr */
630 i_interpolated_pts = p_stream->i_interpolated_pcr;
631 Ogg_UpdatePCR( p_stream, p_oggpacket );
633 if( p_stream->i_pcr >= 0 )
635 /* This is for streams where the granulepos of the header packets
636 * doesn't match these of the data packets (eg. ogg web radios). */
637 if( p_stream->i_previous_pcr == 0 &&
638 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
640 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
642 /* Call the pace control */
643 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
647 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
648 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
649 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
650 p_stream->i_pcr >= 0 )
652 p_stream->i_previous_pcr = p_stream->i_pcr;
654 /* The granulepos is the start date of the sample */
655 i_pts = p_stream->i_pcr;
660 /* This stream isn't currently selected so we don't need to decode it,
661 * but we did need to store its pcr as it might be selected later on */
665 if( p_oggpacket->bytes <= 0 )
668 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
671 if( i_pts == 0 ) i_pts = 1;
672 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
673 else if( i_pts == -1 ) i_pts = 0;
675 if( p_stream->fmt.i_cat == AUDIO_ES )
676 p_block->i_dts = p_block->i_pts = i_pts;
677 else if( p_stream->fmt.i_cat == SPU_ES )
679 p_block->i_dts = p_block->i_pts = i_pts;
680 p_block->i_length = 0;
682 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
683 p_block->i_dts = p_block->i_pts = i_pts;
686 p_block->i_dts = i_pts;
690 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
691 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
692 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
693 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
694 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
695 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
696 p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
698 /* We remove the header from the packet */
699 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
700 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
702 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
704 /* But with subtitles we need to retrieve the duration first */
707 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
709 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
711 lenbytes = lenbytes << 8;
712 lenbytes += *(p_oggpacket->packet + i_header_len - i);
715 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
716 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
717 p_oggpacket->packet[i_header_len + 1] != 0 &&
718 p_oggpacket->packet[i_header_len + 1] != '\n' &&
719 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
721 p_block->i_length = (mtime_t)lenbytes * 1000;
726 if( p_block->i_buffer >= (unsigned int)i_header_len )
727 p_block->i_buffer -= i_header_len;
729 p_block->i_buffer = 0;
732 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
734 /* FIXME: the biggest hack I've ever done */
735 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
736 p_block->i_pts, p_block->i_dts );
740 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
741 p_oggpacket->bytes - i_header_len );
743 es_out_Send( p_demux->out, p_stream->p_es, p_block );
746 /****************************************************************************
747 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
748 * stream and fill p_ogg.
749 *****************************************************************************
750 * The initial page of a logical stream is marked as a 'bos' page.
751 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
752 * together and all of the initial pages must appear before any data pages.
754 * On success this function returns VLC_SUCCESS.
755 ****************************************************************************/
756 static int Ogg_FindLogicalStreams( demux_t *p_demux )
758 demux_sys_t *p_ogg = p_demux->p_sys ;
759 ogg_packet oggpacket;
763 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
765 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
767 if( ogg_page_bos( &oggpage ) )
770 /* All is wonderful in our fine fine little world.
771 * We found the beginning of our first logical stream. */
772 while( ogg_page_bos( &oggpage ) )
776 realloc( p_ogg->pp_stream, p_ogg->i_streams *
777 sizeof(logical_stream_t *) );
779 p_stream = malloc( sizeof(logical_stream_t) );
780 memset( p_stream, 0, sizeof(logical_stream_t) );
781 p_stream->p_headers = 0;
782 p_stream->secondary_header_packets = 0;
784 es_format_Init( &p_stream->fmt, 0, 0 );
786 /* Setup the logical stream */
787 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
788 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
790 /* Extract the initial header from the first page and verify
791 * the codec type of tis Ogg bitstream */
792 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
794 /* error. stream version mismatch perhaps */
795 msg_Err( p_demux, "error reading first page of "
796 "Ogg bitstream data" );
800 /* FIXME: check return value */
801 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
803 /* Check for Vorbis header */
804 if( oggpacket.bytes >= 7 &&
805 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
807 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
808 msg_Dbg( p_demux, "found vorbis header" );
810 /* Check for Speex header */
811 else if( oggpacket.bytes >= 5 &&
812 ! memcmp( oggpacket.packet, "Speex", 5 ) )
814 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
815 msg_Dbg( p_demux, "found speex header, channels: %i, "
816 "rate: %i, bitrate: %i",
817 p_stream->fmt.audio.i_channels,
818 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
820 /* Check for Flac header (< version 1.1.1) */
821 else if( oggpacket.bytes >= 4 &&
822 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
824 msg_Dbg( p_demux, "found FLAC header" );
826 /* Grrrr!!!! Did they really have to put all the
827 * important info in the second header packet!!!
828 * (STREAMINFO metadata is in the following packet) */
829 p_stream->b_force_backup = 1;
831 p_stream->fmt.i_cat = AUDIO_ES;
832 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
834 /* Check for Flac header (>= version 1.1.1) */
835 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
836 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
837 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
839 int i_packets = ((int)oggpacket.packet[7]) << 8 |
841 msg_Dbg( p_demux, "found FLAC header version %i.%i "
842 "(%i header packets)",
843 oggpacket.packet[5], oggpacket.packet[6],
846 p_stream->b_force_backup = 1;
848 p_stream->fmt.i_cat = AUDIO_ES;
849 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
850 oggpacket.packet += 13; oggpacket.bytes -= 13;
851 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
853 /* Check for Theora header */
854 else if( oggpacket.bytes >= 7 &&
855 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
857 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
860 "found theora header, bitrate: %i, rate: %f",
861 p_stream->fmt.i_bitrate, p_stream->f_rate );
863 /* Check for Tarkin header */
864 else if( oggpacket.bytes >= 7 &&
865 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
869 msg_Dbg( p_demux, "found tarkin header" );
870 p_stream->fmt.i_cat = VIDEO_ES;
871 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
873 /* Cheat and get additionnal info ;) */
874 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
875 oggpack_adv( &opb, 88 );
876 oggpack_adv( &opb, 104 );
877 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
878 p_stream->f_rate = 2; /* FIXME */
880 "found tarkin header, bitrate: %i, rate: %f",
881 p_stream->fmt.i_bitrate, p_stream->f_rate );
883 /* Check for Annodex header */
884 else if( oggpacket.bytes >= 7 &&
885 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
887 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
889 /* kill annodex track */
893 /* Check for Annodex header */
894 else if( oggpacket.bytes >= 7 &&
895 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
897 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
900 /* Check for Kate header */
901 else if( oggpacket.bytes >= 9 &&
902 ! memcmp( &oggpacket.packet[1], "kate\0\0\0\0", 8 ) )
904 Ogg_ReadKateHeader( p_stream, &oggpacket );
905 msg_Dbg( p_demux, "found kate header" );
907 else if( oggpacket.bytes >= 142 &&
908 !memcmp( &oggpacket.packet[1],
909 "Direct Show Samples embedded in Ogg", 35 ))
911 /* Old header type */
913 /* Check for video header (old format) */
914 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
915 oggpacket.bytes >= 184 )
917 p_stream->fmt.i_cat = VIDEO_ES;
918 p_stream->fmt.i_codec =
919 VLC_FOURCC( oggpacket.packet[68],
920 oggpacket.packet[69],
921 oggpacket.packet[70],
922 oggpacket.packet[71] );
923 msg_Dbg( p_demux, "found video header of type: %.4s",
924 (char *)&p_stream->fmt.i_codec );
926 p_stream->fmt.video.i_frame_rate = 10000000;
927 p_stream->fmt.video.i_frame_rate_base =
928 GetQWLE((oggpacket.packet+164));
929 p_stream->f_rate = 10000000.0 /
930 GetQWLE((oggpacket.packet+164));
931 p_stream->fmt.video.i_bits_per_pixel =
932 GetWLE((oggpacket.packet+182));
933 if( !p_stream->fmt.video.i_bits_per_pixel )
935 p_stream->fmt.video.i_bits_per_pixel = 24;
936 p_stream->fmt.video.i_width =
937 GetDWLE((oggpacket.packet+176));
938 p_stream->fmt.video.i_height =
939 GetDWLE((oggpacket.packet+180));
942 "fps: %f, width:%i; height:%i, bitcount:%i",
944 p_stream->fmt.video.i_width,
945 p_stream->fmt.video.i_height,
946 p_stream->fmt.video.i_bits_per_pixel);
949 /* Check for audio header (old format) */
950 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
952 unsigned int i_extra_size;
953 unsigned int i_format_tag;
955 p_stream->fmt.i_cat = AUDIO_ES;
957 i_extra_size = GetWLE((oggpacket.packet+140));
960 p_stream->fmt.i_extra = i_extra_size;
961 p_stream->fmt.p_extra = malloc( i_extra_size );
962 memcpy( p_stream->fmt.p_extra,
963 oggpacket.packet + 142, i_extra_size );
966 i_format_tag = GetWLE((oggpacket.packet+124));
967 p_stream->fmt.audio.i_channels =
968 GetWLE((oggpacket.packet+126));
969 p_stream->f_rate = p_stream->fmt.audio.i_rate =
970 GetDWLE((oggpacket.packet+128));
971 p_stream->fmt.i_bitrate =
972 GetDWLE((oggpacket.packet+132)) * 8;
973 p_stream->fmt.audio.i_blockalign =
974 GetWLE((oggpacket.packet+136));
975 p_stream->fmt.audio.i_bitspersample =
976 GetWLE((oggpacket.packet+138));
978 wf_tag_to_fourcc( i_format_tag,
979 &p_stream->fmt.i_codec, 0 );
981 if( p_stream->fmt.i_codec ==
982 VLC_FOURCC('u','n','d','f') )
984 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
985 ( i_format_tag >> 8 ) & 0xff,
986 i_format_tag & 0xff );
989 msg_Dbg( p_demux, "found audio header of type: %.4s",
990 (char *)&p_stream->fmt.i_codec );
991 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
992 "%dbits/sample %dkb/s",
994 p_stream->fmt.audio.i_channels,
995 p_stream->fmt.audio.i_rate,
996 p_stream->fmt.audio.i_bitspersample,
997 p_stream->fmt.i_bitrate / 1024 );
1002 msg_Dbg( p_demux, "stream %d has an old header "
1003 "but is of an unknown type", p_ogg->i_streams-1 );
1008 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
1009 == PACKET_TYPE_HEADER &&
1010 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
1012 stream_header *st = (stream_header *)(oggpacket.packet+1);
1014 /* Check for video header (new format) */
1015 if( !strncmp( st->streamtype, "video", 5 ) )
1017 p_stream->fmt.i_cat = VIDEO_ES;
1019 /* We need to get rid of the header packet */
1020 ogg_stream_packetout( &p_stream->os, &oggpacket );
1022 p_stream->fmt.i_codec =
1023 VLC_FOURCC( st->subtype[0], st->subtype[1],
1024 st->subtype[2], st->subtype[3] );
1025 msg_Dbg( p_demux, "found video header of type: %.4s",
1026 (char *)&p_stream->fmt.i_codec );
1028 p_stream->fmt.video.i_frame_rate = 10000000;
1029 p_stream->fmt.video.i_frame_rate_base =
1030 GetQWLE(&st->time_unit);
1031 p_stream->f_rate = 10000000.0 /
1032 GetQWLE(&st->time_unit);
1033 p_stream->fmt.video.i_bits_per_pixel =
1034 GetWLE(&st->bits_per_sample);
1035 p_stream->fmt.video.i_width =
1036 GetDWLE(&st->sh.video.width);
1037 p_stream->fmt.video.i_height =
1038 GetDWLE(&st->sh.video.height);
1041 "fps: %f, width:%i; height:%i, bitcount:%i",
1043 p_stream->fmt.video.i_width,
1044 p_stream->fmt.video.i_height,
1045 p_stream->fmt.video.i_bits_per_pixel );
1047 /* Check for audio header (new format) */
1048 else if( !strncmp( st->streamtype, "audio", 5 ) )
1053 p_stream->fmt.i_cat = AUDIO_ES;
1055 /* We need to get rid of the header packet */
1056 ogg_stream_packetout( &p_stream->os, &oggpacket );
1058 p_stream->fmt.i_extra = GetQWLE(&st->size) -
1059 sizeof(stream_header);
1060 if( p_stream->fmt.i_extra )
1062 p_stream->fmt.p_extra =
1063 malloc( p_stream->fmt.i_extra );
1064 memcpy( p_stream->fmt.p_extra, st + 1,
1065 p_stream->fmt.i_extra );
1068 memcpy( p_buffer, st->subtype, 4 );
1070 i_format_tag = strtol(p_buffer,NULL,16);
1071 p_stream->fmt.audio.i_channels =
1072 GetWLE(&st->sh.audio.channels);
1073 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1074 GetQWLE(&st->samples_per_unit);
1075 p_stream->fmt.i_bitrate =
1076 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
1077 p_stream->fmt.audio.i_blockalign =
1078 GetWLE(&st->sh.audio.blockalign);
1079 p_stream->fmt.audio.i_bitspersample =
1080 GetWLE(&st->bits_per_sample);
1082 wf_tag_to_fourcc( i_format_tag,
1083 &p_stream->fmt.i_codec, 0 );
1085 if( p_stream->fmt.i_codec ==
1086 VLC_FOURCC('u','n','d','f') )
1088 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1089 ( i_format_tag >> 8 ) & 0xff,
1090 i_format_tag & 0xff );
1093 msg_Dbg( p_demux, "found audio header of type: %.4s",
1094 (char *)&p_stream->fmt.i_codec );
1095 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1096 "%dbits/sample %dkb/s",
1098 p_stream->fmt.audio.i_channels,
1099 p_stream->fmt.audio.i_rate,
1100 p_stream->fmt.audio.i_bitspersample,
1101 p_stream->fmt.i_bitrate / 1024 );
1103 /* Check for text (subtitles) header */
1104 else if( !strncmp(st->streamtype, "text", 4) )
1106 /* We need to get rid of the header packet */
1107 ogg_stream_packetout( &p_stream->os, &oggpacket );
1109 msg_Dbg( p_demux, "found text subtitles header" );
1110 p_stream->fmt.i_cat = SPU_ES;
1111 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1112 p_stream->f_rate = 1000; /* granulepos is in milisec */
1116 msg_Dbg( p_demux, "stream %d has a header marker "
1117 "but is of an unknown type", p_ogg->i_streams-1 );
1122 else if( oggpacket.bytes >= 7 &&
1123 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1127 msg_Dbg( p_demux, "stream %d is a skeleton",
1128 p_ogg->i_streams-1 );
1129 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1133 msg_Dbg( p_demux, "stream %d is of unknown type",
1134 p_ogg->i_streams-1 );
1139 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1140 return VLC_EGENERIC;
1143 /* This is the first data page, which means we are now finished
1144 * with the initial pages. We just need to store it in the relevant
1146 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1148 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1160 return VLC_EGENERIC;
1163 /****************************************************************************
1164 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1165 * Elementary streams.
1166 ****************************************************************************/
1167 static int Ogg_BeginningOfStream( demux_t *p_demux )
1169 demux_sys_t *p_ogg = p_demux->p_sys ;
1172 /* Find the logical streams embedded in the physical stream and
1173 * initialize our p_ogg structure. */
1174 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1176 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1177 return VLC_EGENERIC;
1180 p_ogg->i_bitrate = 0;
1182 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1184 #define p_stream p_ogg->pp_stream[i_stream]
1185 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1187 // TODO: something to do here ?
1188 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1190 /* Set the CMML stream active */
1191 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1194 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1196 p_stream->i_pcr = p_stream->i_previous_pcr =
1197 p_stream->i_interpolated_pcr = -1;
1198 p_stream->b_reinit = 0;
1205 /****************************************************************************
1206 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1207 ****************************************************************************/
1208 static void Ogg_EndOfStream( demux_t *p_demux )
1210 demux_sys_t *p_ogg = p_demux->p_sys ;
1213 #define p_stream p_ogg->pp_stream[i_stream]
1214 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1216 if( p_stream->p_es )
1217 es_out_Del( p_demux->out, p_stream->p_es );
1219 p_ogg->i_bitrate -= p_stream->fmt.i_bitrate;
1221 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1222 free( p_ogg->pp_stream[i_stream]->p_headers );
1224 es_format_Clean( &p_stream->fmt );
1226 free( p_ogg->pp_stream[i_stream] );
1231 free( p_ogg->pp_stream );
1232 p_ogg->pp_stream = NULL;
1233 p_ogg->i_streams = 0;
1236 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1237 ogg_packet *p_oggpacket )
1240 int i_fps_numerator;
1241 int i_fps_denominator;
1242 int i_keyframe_frequency_force;
1244 p_stream->fmt.i_cat = VIDEO_ES;
1245 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1247 /* Signal that we want to keep a backup of the theora
1248 * stream headers. They will be used when switching between
1250 p_stream->b_force_backup = 1;
1252 /* Cheat and get additionnal info ;) */
1253 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1254 bs_skip( &bitstream, 56 );
1255 bs_read( &bitstream, 8 ); /* major version num */
1256 bs_read( &bitstream, 8 ); /* minor version num */
1257 bs_read( &bitstream, 8 ); /* subminor version num */
1258 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1259 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1260 bs_read( &bitstream, 24 ); /* frame width */
1261 bs_read( &bitstream, 24 ); /* frame height */
1262 bs_read( &bitstream, 8 ); /* x offset */
1263 bs_read( &bitstream, 8 ); /* y offset */
1265 i_fps_numerator = bs_read( &bitstream, 32 );
1266 i_fps_denominator = bs_read( &bitstream, 32 );
1267 bs_read( &bitstream, 24 ); /* aspect_numerator */
1268 bs_read( &bitstream, 24 ); /* aspect_denominator */
1270 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1271 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1273 bs_read( &bitstream, 8 ); /* colorspace */
1274 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1275 bs_read( &bitstream, 6 ); /* quality */
1277 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1279 /* granule_shift = i_log( frequency_force -1 ) */
1280 p_stream->i_granule_shift = 0;
1281 i_keyframe_frequency_force--;
1282 while( i_keyframe_frequency_force )
1284 p_stream->i_granule_shift++;
1285 i_keyframe_frequency_force >>= 1;
1288 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1291 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1292 ogg_packet *p_oggpacket )
1296 p_stream->fmt.i_cat = AUDIO_ES;
1297 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1299 /* Signal that we want to keep a backup of the vorbis
1300 * stream headers. They will be used when switching between
1302 p_stream->b_force_backup = 1;
1304 /* Cheat and get additionnal info ;) */
1305 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1306 oggpack_adv( &opb, 88 );
1307 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1308 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1309 oggpack_read( &opb, 32 );
1310 oggpack_adv( &opb, 32 );
1311 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1314 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1315 ogg_packet *p_oggpacket )
1319 p_stream->fmt.i_cat = AUDIO_ES;
1320 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1322 /* Signal that we want to keep a backup of the speex
1323 * stream headers. They will be used when switching between
1325 p_stream->b_force_backup = 1;
1327 /* Cheat and get additionnal info ;) */
1328 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1329 oggpack_adv( &opb, 224 );
1330 oggpack_adv( &opb, 32 ); /* speex_version_id */
1331 oggpack_adv( &opb, 32 ); /* header_size */
1332 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1333 oggpack_adv( &opb, 32 ); /* mode */
1334 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1335 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1336 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1339 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1340 ogg_packet *p_oggpacket )
1342 /* Parse the STREAMINFO metadata */
1345 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1348 if( bs_read( &s, 7 ) == 0 )
1350 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1353 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1354 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1356 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1357 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1359 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1361 /* Fake this as the last metadata block */
1362 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1366 /* This ain't a STREAMINFO metadata */
1367 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1371 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1372 ogg_packet *p_oggpacket )
1379 p_stream->fmt.i_cat = SPU_ES;
1380 p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1382 /* Signal that we want to keep a backup of the kate
1383 * stream headers. They will be used when switching between
1385 p_stream->b_force_backup = 1;
1387 /* Cheat and get additionnal info ;) */
1388 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1389 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1390 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1391 oggpack_adv( &opb, 3*8 );
1392 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1393 oggpack_adv( &opb, 8*8 ); /* reserved */
1394 gnum = oggpack_read( &opb, 32 );
1395 gden = oggpack_read( &opb, 32 );
1396 p_stream->f_rate = (double)gnum/gden;
1398 p_stream->fmt.psz_language = malloc(16);
1399 if (p_stream->fmt.psz_language)
1402 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1403 p_stream->fmt.psz_language[15] = 0; /* just in case */
1408 oggpack_read(&opb,8);
1410 p_stream->fmt.psz_description = malloc(16);
1411 if (p_stream->fmt.psz_description)
1414 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1415 p_stream->fmt.psz_description[15] = 0; /* just in case */
1420 oggpack_read(&opb,8);
1424 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1425 logical_stream_t *p_stream,
1426 ogg_packet *p_oggpacket )
1428 if( p_oggpacket->bytes >= 28 &&
1429 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1433 uint16_t major_version;
1434 uint16_t minor_version;
1435 uint64_t timebase_numerator;
1436 uint64_t timebase_denominator;
1438 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1440 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1441 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1442 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1443 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1444 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1445 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1447 else if( p_oggpacket->bytes >= 42 &&
1448 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1450 uint64_t granule_rate_numerator;
1451 uint64_t granule_rate_denominator;
1452 char content_type_string[1024];
1454 /* Read in Annodex header fields */
1456 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1457 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1458 p_stream->secondary_header_packets =
1459 GetDWLE( &p_oggpacket->packet[24] );
1461 /* we are guaranteed that the first header field will be
1462 * the content-type (by the Annodex standard) */
1463 content_type_string[0] = '\0';
1464 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1466 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1467 p_oggpacket->bytes - 1 );
1468 if( p && p[0] == '\r' && p[1] == '\n' )
1469 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1470 content_type_string );
1473 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1474 granule_rate_numerator, granule_rate_denominator,
1475 p_stream->secondary_header_packets, content_type_string );
1477 p_stream->f_rate = (float) granule_rate_numerator /
1478 (float) granule_rate_denominator;
1480 /* What type of file do we have?
1481 * strcmp is safe to use here because we've extracted
1482 * content_type_string from the stream manually */
1483 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1485 /* n.b. WAVs are unsupported right now */
1486 p_stream->fmt.i_cat = UNKNOWN_ES;
1488 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1490 p_stream->fmt.i_cat = AUDIO_ES;
1491 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1493 p_stream->b_force_backup = 1;
1495 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1497 p_stream->fmt.i_cat = AUDIO_ES;
1498 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1500 p_stream->b_force_backup = 1;
1502 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1504 p_stream->fmt.i_cat = VIDEO_ES;
1505 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1507 p_stream->b_force_backup = 1;
1509 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1511 p_stream->fmt.i_cat = VIDEO_ES;
1512 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1514 p_stream->b_force_backup = 1;
1516 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1518 /* n.b. MPEG streams are unsupported right now */
1519 p_stream->fmt.i_cat = VIDEO_ES;
1520 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1522 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1524 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1525 p_stream->fmt.i_cat = SPU_ES;
1526 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );