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 *****************************************************************************/
29 #include <vlc_input.h>
30 #include <vlc_demux.h>
34 #include <vlc_codecs.h>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int Open ( vlc_object_t * );
41 static void Close( vlc_object_t * );
44 set_shortname ( "OGG" );
45 set_description( _("OGG demuxer" ) );
46 set_category( CAT_INPUT );
47 set_subcategory( SUBCAT_INPUT_DEMUX );
48 set_capability( "demux2", 50 );
49 set_callbacks( Open, Close );
50 add_shortcut( "ogg" );
54 /*****************************************************************************
55 * Definitions of structures and functions used by this plugins
56 *****************************************************************************/
57 typedef struct logical_stream_s
59 ogg_stream_state os; /* logical stream of packets */
67 /* the header of some logical streams (eg vorbis) contain essential
68 * data for the decoder. We back them up here in case we need to re-feed
69 * them to the decoder. */
75 /* program clock reference (in units of 90kHz) derived from the previous
78 mtime_t i_interpolated_pcr;
79 mtime_t i_previous_pcr;
83 int i_theora_keyframe_granule_shift;
85 /* for Annodex logical bitstreams */
86 int secondary_header_packets;
92 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
94 int i_streams; /* number of logical bitstreams */
95 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
97 /* program clock reference (in units of 90kHz) derived from the pcr of
109 input_attachment_t **attachments;
112 /* OggDS headers for the new header format (used in ogm files) */
113 typedef struct stream_header_video
117 } stream_header_video;
119 typedef struct stream_header_audio
121 ogg_int16_t channels;
122 ogg_int16_t blockalign;
123 ogg_int32_t avgbytespersec;
124 } stream_header_audio;
126 typedef struct stream_header
131 ogg_int32_t size; /* size of the structure */
133 ogg_int64_t time_unit; /* in reference time */
134 ogg_int64_t samples_per_unit;
135 ogg_int32_t default_len; /* in media time */
137 ogg_int32_t buffersize;
138 ogg_int16_t bits_per_sample;
143 stream_header_video video;
145 stream_header_audio audio;
149 #define OGG_BLOCK_SIZE 4096
151 /* Some defines from OggDS */
152 #define PACKET_TYPE_HEADER 0x01
153 #define PACKET_TYPE_BITS 0x07
154 #define PACKET_LEN_BITS01 0xc0
155 #define PACKET_LEN_BITS2 0x02
156 #define PACKET_IS_SYNCPOINT 0x08
158 /*****************************************************************************
160 *****************************************************************************/
161 static int Demux ( demux_t * );
162 static int Control( demux_t *, int, va_list );
164 /* Bitstream manipulation */
165 static int Ogg_ReadPage ( demux_t *, ogg_page * );
166 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
167 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
169 static int Ogg_BeginningOfStream( demux_t *p_demux );
170 static int Ogg_FindLogicalStreams( demux_t *p_demux );
171 static void Ogg_EndOfStream( demux_t *p_demux );
173 /* Logical bitstream headers */
174 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
175 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
176 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
177 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
178 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
180 /*****************************************************************************
181 * Open: initializes ogg demux structures
182 *****************************************************************************/
183 static int Open( vlc_object_t * p_this )
185 demux_t *p_demux = (demux_t *)p_this;
186 input_thread_t *p_input;
188 const uint8_t *p_peek;
191 /* Check if we are dealing with an ogg stream */
192 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
193 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
198 /* Set exported functions */
199 p_demux->pf_demux = Demux;
200 p_demux->pf_control = Control;
201 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
203 memset( p_sys, 0, sizeof( demux_sys_t ) );
204 p_sys->i_bitrate = 0;
205 p_sys->pp_stream = NULL;
207 /* Begnning of stream, tell the demux to look for elementary streams. */
211 p_input = (input_thread_t *)vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
214 p_demux->p_private = malloc( sizeof( demux_meta_t ) );
215 if( !p_demux->p_private )
217 vlc_object_release( p_input );
220 module_t *p_meta = module_Need( p_demux, "meta reader", NULL, 0 );
223 demux_meta_t *p_demux_meta = (demux_meta_t *)p_demux->p_private;
224 vlc_meta_Merge( input_GetItem(p_input)->p_meta,
225 p_demux_meta->p_meta );
226 vlc_meta_Delete( p_demux_meta->p_meta );
227 module_Unneed( p_demux, p_meta );
228 p_sys->i_attachments = p_demux_meta->i_attachments;
229 p_sys->attachments = p_demux_meta->attachments;
231 vlc_object_release( p_input );
232 free( p_demux->p_private );
235 /* Initialize the Ogg physical bitstream parser */
236 ogg_sync_init( &p_sys->oy );
241 /*****************************************************************************
242 * Close: frees unused data
243 *****************************************************************************/
244 static void Close( vlc_object_t *p_this )
246 demux_t *p_demux = (demux_t *)p_this;
247 demux_sys_t *p_sys = p_demux->p_sys ;
249 /* Cleanup the bitstream parser */
250 ogg_sync_clear( &p_sys->oy );
252 Ogg_EndOfStream( p_demux );
255 for( i = 0; i < p_sys->i_attachments; i++ )
256 free( p_sys->attachments[i] );
257 TAB_CLEAN( p_sys->i_attachments, p_sys->attachments);
262 /*****************************************************************************
263 * Demux: reads and demuxes data packets
264 *****************************************************************************
265 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
266 *****************************************************************************/
267 static int Demux( demux_t * p_demux )
269 demux_sys_t *p_sys = p_demux->p_sys;
271 ogg_packet oggpacket;
275 if( p_sys->i_eos == p_sys->i_streams )
279 msg_Dbg( p_demux, "end of a group of logical streams" );
280 Ogg_EndOfStream( p_demux );
284 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS ) return 0;
286 msg_Dbg( p_demux, "beginning of a group of logical streams" );
287 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
291 * Demux an ogg page from the stream
293 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
298 /* Test for End of Stream */
299 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
302 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
304 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
306 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
309 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
311 /* Read info from any secondary header packets, if there are any */
312 if( p_stream->secondary_header_packets > 0 )
314 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
315 oggpacket.bytes >= 7 &&
316 ! memcmp( &oggpacket.packet[1], "theora", 6 ) )
318 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
319 p_stream->secondary_header_packets = 0;
321 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
322 oggpacket.bytes >= 7 &&
323 ! memcmp( &oggpacket.packet[1], "vorbis", 6 ) )
325 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
326 p_stream->secondary_header_packets = 0;
328 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
330 p_stream->secondary_header_packets = 0;
334 if( p_stream->b_reinit )
336 /* If synchro is re-initialized we need to drop all the packets
337 * until we find a new dated one. */
338 Ogg_UpdatePCR( p_stream, &oggpacket );
340 if( p_stream->i_pcr >= 0 )
342 p_stream->b_reinit = 0;
346 p_stream->i_interpolated_pcr = -1;
350 /* An Ogg/vorbis packet contains an end date granulepos */
351 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
352 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
353 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
355 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
357 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
361 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
368 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
373 i_stream = 0; p_sys->i_pcr = -1;
374 for( ; i_stream < p_sys->i_streams; i_stream++ )
376 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
378 if( p_stream->fmt.i_cat == SPU_ES )
380 if( p_stream->i_interpolated_pcr < 0 )
383 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
384 p_sys->i_pcr = p_stream->i_interpolated_pcr;
387 if( p_sys->i_pcr >= 0 )
389 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
396 /*****************************************************************************
398 *****************************************************************************/
399 static int Control( demux_t *p_demux, int i_query, va_list args )
401 demux_sys_t *p_sys = p_demux->p_sys;
404 input_attachment_t ***ppp_attach;
410 pi64 = (int64_t*)va_arg( args, int64_t * );
411 *pi64 = p_sys->i_pcr;
417 case DEMUX_SET_POSITION:
418 for( i = 0; i < p_sys->i_streams; i++ )
420 logical_stream_t *p_stream = p_sys->pp_stream[i];
422 /* we'll trash all the data until we find the next pcr */
423 p_stream->b_reinit = 1;
424 p_stream->i_pcr = -1;
425 p_stream->i_interpolated_pcr = -1;
426 ogg_stream_reset( &p_stream->os );
428 ogg_sync_reset( &p_sys->oy );
430 case DEMUX_GET_ATTACHMENTS:
432 (input_attachment_t***)va_arg( args, input_attachment_t*** );
433 pi_int = (int*)va_arg( args, int * );
435 if( p_sys->i_attachments <= 0 )
438 *pi_int = p_sys->i_attachments;
439 *ppp_attach = malloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
440 for( i = 0; i < p_sys->i_attachments; i++ )
441 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
445 return demux2_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
450 /****************************************************************************
451 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
452 ****************************************************************************
453 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
454 * are at the end of stream.
455 ****************************************************************************/
456 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
458 demux_sys_t *p_ogg = p_demux->p_sys ;
462 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
464 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
466 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
470 ogg_sync_wrote( &p_ogg->oy, i_read );
476 /****************************************************************************
477 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
479 ****************************************************************************/
480 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
481 ogg_packet *p_oggpacket )
483 /* Convert the granulepos into a pcr */
484 if( p_oggpacket->granulepos >= 0 )
486 if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
488 p_stream->i_pcr = p_oggpacket->granulepos * I64C(1000000)
493 ogg_int64_t iframe = p_oggpacket->granulepos >>
494 p_stream->i_theora_keyframe_granule_shift;
495 ogg_int64_t pframe = p_oggpacket->granulepos -
496 ( iframe << p_stream->i_theora_keyframe_granule_shift );
498 p_stream->i_pcr = ( iframe + pframe ) * I64C(1000000)
502 p_stream->i_interpolated_pcr = p_stream->i_pcr;
506 p_stream->i_pcr = -1;
508 /* no granulepos available, try to interpolate the pcr.
509 * If we can't then don't touch the old value. */
510 if( p_stream->fmt.i_cat == VIDEO_ES )
511 /* 1 frame per packet */
512 p_stream->i_interpolated_pcr += (I64C(1000000) / p_stream->f_rate);
513 else if( p_stream->fmt.i_bitrate )
514 p_stream->i_interpolated_pcr +=
515 ( p_oggpacket->bytes * I64C(1000000) /
516 p_stream->fmt.i_bitrate / 8 );
520 /****************************************************************************
521 * Ogg_DecodePacket: Decode an Ogg packet.
522 ****************************************************************************/
523 static void Ogg_DecodePacket( demux_t *p_demux,
524 logical_stream_t *p_stream,
525 ogg_packet *p_oggpacket )
528 vlc_bool_t b_selected;
529 int i_header_len = 0;
530 mtime_t i_pts = -1, i_interpolated_pts;
533 if( !p_oggpacket->bytes )
535 msg_Dbg( p_demux, "discarding 0 sized packet" );
539 if( p_oggpacket->bytes >= 7 &&
540 ! memcmp ( &p_oggpacket->packet[0], "Annodex", 7 ) )
542 /* it's an Annodex packet -- skip it (do nothing) */
545 else if( p_oggpacket->bytes >= 7 &&
546 ! memcmp ( &p_oggpacket->packet[0], "AnxData", 7 ) )
548 /* it's an AnxData packet -- skip it (do nothing) */
552 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
553 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
555 /* Check the ES is selected */
556 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
557 p_stream->p_es, &b_selected );
559 if( p_stream->b_force_backup )
562 vlc_bool_t b_store_size = VLC_TRUE;
564 p_stream->i_packets_backup++;
565 switch( p_stream->fmt.i_codec )
567 case VLC_FOURCC( 'v','o','r','b' ):
568 case VLC_FOURCC( 's','p','x',' ' ):
569 case VLC_FOURCC( 't','h','e','o' ):
570 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
573 case VLC_FOURCC( 'f','l','a','c' ):
574 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
576 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
577 p_stream->b_force_backup = 0;
579 else if( p_stream->fmt.audio.i_rate )
581 p_stream->b_force_backup = 0;
582 if( p_oggpacket->bytes >= 9 )
584 p_oggpacket->packet += 9;
585 p_oggpacket->bytes -= 9;
588 b_store_size = VLC_FALSE;
592 p_stream->b_force_backup = 0;
596 /* Backup the ogg packet (likely an header packet) */
597 p_stream->p_headers =
598 realloc( p_stream->p_headers, p_stream->i_headers +
599 p_oggpacket->bytes + (b_store_size ? 2 : 0) );
600 p_extra = p_stream->p_headers + p_stream->i_headers;
603 *(p_extra++) = p_oggpacket->bytes >> 8;
604 *(p_extra++) = p_oggpacket->bytes & 0xFF;
606 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
607 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0);
609 if( !p_stream->b_force_backup )
611 /* Last header received, commit changes */
612 p_stream->fmt.i_extra = p_stream->i_headers;
613 p_stream->fmt.p_extra =
614 realloc( p_stream->fmt.p_extra, p_stream->i_headers );
615 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
616 p_stream->i_headers );
617 es_out_Control( p_demux->out, ES_OUT_SET_FMT,
618 p_stream->p_es, &p_stream->fmt );
621 b_selected = VLC_FALSE; /* Discard the header packet */
624 /* Convert the pcr into a pts */
625 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
626 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
627 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
629 if( p_stream->i_pcr >= 0 )
631 /* This is for streams where the granulepos of the header packets
632 * doesn't match these of the data packets (eg. ogg web radios). */
633 if( p_stream->i_previous_pcr == 0 &&
634 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
636 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
638 /* Call the pace control */
639 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
643 p_stream->i_previous_pcr = p_stream->i_pcr;
645 /* The granulepos is the end date of the sample */
646 i_pts = p_stream->i_pcr;
650 /* Convert the granulepos into the next pcr */
651 i_interpolated_pts = p_stream->i_interpolated_pcr;
652 Ogg_UpdatePCR( p_stream, p_oggpacket );
654 if( p_stream->i_pcr >= 0 )
656 /* This is for streams where the granulepos of the header packets
657 * doesn't match these of the data packets (eg. ogg web radios). */
658 if( p_stream->i_previous_pcr == 0 &&
659 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
661 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
663 /* Call the pace control */
664 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
668 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
669 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
670 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
671 p_stream->i_pcr >= 0 )
673 p_stream->i_previous_pcr = p_stream->i_pcr;
675 /* The granulepos is the start date of the sample */
676 i_pts = p_stream->i_pcr;
681 /* This stream isn't currently selected so we don't need to decode it,
682 * but we did need to store its pcr as it might be selected later on */
686 if( p_oggpacket->bytes <= 0 )
689 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
692 if( i_pts == 0 ) i_pts = 1;
693 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
694 else if( i_pts == -1 ) i_pts = 0;
696 if( p_stream->fmt.i_cat == AUDIO_ES )
697 p_block->i_dts = p_block->i_pts = i_pts;
698 else if( p_stream->fmt.i_cat == SPU_ES )
700 p_block->i_dts = p_block->i_pts = i_pts;
701 p_block->i_length = 0;
703 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
704 p_block->i_dts = p_block->i_pts = i_pts;
707 p_block->i_dts = i_pts;
711 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
712 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
713 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
714 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
715 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
716 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) )
718 /* We remove the header from the packet */
719 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
720 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
722 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
724 /* But with subtitles we need to retrieve the duration first */
727 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
729 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
731 lenbytes = lenbytes << 8;
732 lenbytes += *(p_oggpacket->packet + i_header_len - i);
735 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
736 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
737 p_oggpacket->packet[i_header_len + 1] != 0 &&
738 p_oggpacket->packet[i_header_len + 1] != '\n' &&
739 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
741 p_block->i_length = (mtime_t)lenbytes * 1000;
746 if( p_block->i_buffer >= i_header_len )
747 p_block->i_buffer -= i_header_len;
749 p_block->i_buffer = 0;
752 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
754 /* FIXME: the biggest hack I've ever done */
755 msg_Warn( p_demux, "tarkin pts: "I64Fd", granule: "I64Fd,
756 p_block->i_pts, p_block->i_dts );
760 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
761 p_oggpacket->bytes - i_header_len );
763 es_out_Send( p_demux->out, p_stream->p_es, p_block );
766 /****************************************************************************
767 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
768 * stream and fill p_ogg.
769 *****************************************************************************
770 * The initial page of a logical stream is marked as a 'bos' page.
771 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
772 * together and all of the initial pages must appear before any data pages.
774 * On success this function returns VLC_SUCCESS.
775 ****************************************************************************/
776 static int Ogg_FindLogicalStreams( demux_t *p_demux )
778 demux_sys_t *p_ogg = p_demux->p_sys ;
779 ogg_packet oggpacket;
783 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
785 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
787 if( ogg_page_bos( &oggpage ) )
790 /* All is wonderful in our fine fine little world.
791 * We found the beginning of our first logical stream. */
792 while( ogg_page_bos( &oggpage ) )
796 realloc( p_ogg->pp_stream, p_ogg->i_streams *
797 sizeof(logical_stream_t *) );
799 p_stream = malloc( sizeof(logical_stream_t) );
800 memset( p_stream, 0, sizeof(logical_stream_t) );
801 p_stream->p_headers = 0;
802 p_stream->secondary_header_packets = 0;
804 es_format_Init( &p_stream->fmt, 0, 0 );
806 /* Setup the logical stream */
807 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
808 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
810 /* Extract the initial header from the first page and verify
811 * the codec type of tis Ogg bitstream */
812 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
814 /* error. stream version mismatch perhaps */
815 msg_Err( p_demux, "error reading first page of "
816 "Ogg bitstream data" );
820 /* FIXME: check return value */
821 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
823 /* Check for Vorbis header */
824 if( oggpacket.bytes >= 7 &&
825 ! memcmp( &oggpacket.packet[1], "vorbis", 6 ) )
827 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
828 msg_Dbg( p_demux, "found vorbis header" );
830 /* Check for Speex header */
831 else if( oggpacket.bytes >= 7 &&
832 ! memcmp( &oggpacket.packet[0], "Speex", 5 ) )
834 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
835 msg_Dbg( p_demux, "found speex header, channels: %i, "
836 "rate: %i, bitrate: %i",
837 p_stream->fmt.audio.i_channels,
838 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
840 /* Check for Flac header (< version 1.1.1) */
841 else if( oggpacket.bytes >= 4 &&
842 ! memcmp( &oggpacket.packet[0], "fLaC", 4 ) )
844 msg_Dbg( p_demux, "found FLAC header" );
846 /* Grrrr!!!! Did they really have to put all the
847 * important info in the second header packet!!!
848 * (STREAMINFO metadata is in the following packet) */
849 p_stream->b_force_backup = 1;
851 p_stream->fmt.i_cat = AUDIO_ES;
852 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
854 /* Check for Flac header (>= version 1.1.1) */
855 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
856 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
857 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
859 int i_packets = ((int)oggpacket.packet[7]) << 8 |
861 msg_Dbg( p_demux, "found FLAC header version %i.%i "
862 "(%i header packets)",
863 oggpacket.packet[5], oggpacket.packet[6],
866 p_stream->b_force_backup = 1;
868 p_stream->fmt.i_cat = AUDIO_ES;
869 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
870 oggpacket.packet += 13; oggpacket.bytes -= 13;
871 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
873 /* Check for Theora header */
874 else if( oggpacket.bytes >= 7 &&
875 ! memcmp( &oggpacket.packet[1], "theora", 6 ) )
877 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
880 "found theora header, bitrate: %i, rate: %f",
881 p_stream->fmt.i_bitrate, p_stream->f_rate );
883 /* Check for Tarkin header */
884 else if( oggpacket.bytes >= 7 &&
885 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
889 msg_Dbg( p_demux, "found tarkin header" );
890 p_stream->fmt.i_cat = VIDEO_ES;
891 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
893 /* Cheat and get additionnal info ;) */
894 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
895 oggpack_adv( &opb, 88 );
896 oggpack_adv( &opb, 104 );
897 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
898 p_stream->f_rate = 2; /* FIXME */
900 "found tarkin header, bitrate: %i, rate: %f",
901 p_stream->fmt.i_bitrate, p_stream->f_rate );
903 /* Check for Annodex header */
904 else if( oggpacket.bytes >= 7 &&
905 ! memcmp( &oggpacket.packet[0], "Annodex", 7 ) )
907 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
909 /* kill annodex track */
913 /* Check for Annodex header */
914 else if( oggpacket.bytes >= 7 &&
915 ! memcmp( &oggpacket.packet[0], "AnxData", 7 ) )
917 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
920 else if( oggpacket.bytes >= 142 &&
921 !memcmp( &oggpacket.packet[1],
922 "Direct Show Samples embedded in Ogg", 35 ))
924 /* Old header type */
926 /* Check for video header (old format) */
927 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
928 oggpacket.bytes >= 184 )
930 p_stream->fmt.i_cat = VIDEO_ES;
931 p_stream->fmt.i_codec =
932 VLC_FOURCC( oggpacket.packet[68],
933 oggpacket.packet[69],
934 oggpacket.packet[70],
935 oggpacket.packet[71] );
936 msg_Dbg( p_demux, "found video header of type: %.4s",
937 (char *)&p_stream->fmt.i_codec );
939 p_stream->fmt.video.i_frame_rate = 10000000;
940 p_stream->fmt.video.i_frame_rate_base =
941 GetQWLE((oggpacket.packet+164));
942 p_stream->f_rate = 10000000.0 /
943 GetQWLE((oggpacket.packet+164));
944 p_stream->fmt.video.i_bits_per_pixel =
945 GetWLE((oggpacket.packet+182));
946 if( !p_stream->fmt.video.i_bits_per_pixel )
948 p_stream->fmt.video.i_bits_per_pixel = 24;
949 p_stream->fmt.video.i_width =
950 GetDWLE((oggpacket.packet+176));
951 p_stream->fmt.video.i_height =
952 GetDWLE((oggpacket.packet+180));
955 "fps: %f, width:%i; height:%i, bitcount:%i",
957 p_stream->fmt.video.i_width,
958 p_stream->fmt.video.i_height,
959 p_stream->fmt.video.i_bits_per_pixel);
962 /* Check for audio header (old format) */
963 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
965 unsigned int i_extra_size;
966 unsigned int i_format_tag;
968 p_stream->fmt.i_cat = AUDIO_ES;
970 i_extra_size = GetWLE((oggpacket.packet+140));
973 p_stream->fmt.i_extra = i_extra_size;
974 p_stream->fmt.p_extra = malloc( i_extra_size );
975 memcpy( p_stream->fmt.p_extra,
976 oggpacket.packet + 142, i_extra_size );
979 i_format_tag = GetWLE((oggpacket.packet+124));
980 p_stream->fmt.audio.i_channels =
981 GetWLE((oggpacket.packet+126));
982 p_stream->f_rate = p_stream->fmt.audio.i_rate =
983 GetDWLE((oggpacket.packet+128));
984 p_stream->fmt.i_bitrate =
985 GetDWLE((oggpacket.packet+132)) * 8;
986 p_stream->fmt.audio.i_blockalign =
987 GetWLE((oggpacket.packet+136));
988 p_stream->fmt.audio.i_bitspersample =
989 GetWLE((oggpacket.packet+138));
991 wf_tag_to_fourcc( i_format_tag,
992 &p_stream->fmt.i_codec, 0 );
994 if( p_stream->fmt.i_codec ==
995 VLC_FOURCC('u','n','d','f') )
997 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
998 ( i_format_tag >> 8 ) & 0xff,
999 i_format_tag & 0xff );
1002 msg_Dbg( p_demux, "found audio header of type: %.4s",
1003 (char *)&p_stream->fmt.i_codec );
1004 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1005 "%dbits/sample %dkb/s",
1007 p_stream->fmt.audio.i_channels,
1008 p_stream->fmt.audio.i_rate,
1009 p_stream->fmt.audio.i_bitspersample,
1010 p_stream->fmt.i_bitrate / 1024 );
1015 msg_Dbg( p_demux, "stream %d has an old header "
1016 "but is of an unknown type", p_ogg->i_streams-1 );
1021 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
1022 == PACKET_TYPE_HEADER &&
1023 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
1025 stream_header *st = (stream_header *)(oggpacket.packet+1);
1027 /* Check for video header (new format) */
1028 if( !strncmp( st->streamtype, "video", 5 ) )
1030 p_stream->fmt.i_cat = VIDEO_ES;
1032 /* We need to get rid of the header packet */
1033 ogg_stream_packetout( &p_stream->os, &oggpacket );
1035 p_stream->fmt.i_codec =
1036 VLC_FOURCC( st->subtype[0], st->subtype[1],
1037 st->subtype[2], st->subtype[3] );
1038 msg_Dbg( p_demux, "found video header of type: %.4s",
1039 (char *)&p_stream->fmt.i_codec );
1041 p_stream->fmt.video.i_frame_rate = 10000000;
1042 p_stream->fmt.video.i_frame_rate_base =
1043 GetQWLE(&st->time_unit);
1044 p_stream->f_rate = 10000000.0 /
1045 GetQWLE(&st->time_unit);
1046 p_stream->fmt.video.i_bits_per_pixel =
1047 GetWLE(&st->bits_per_sample);
1048 p_stream->fmt.video.i_width =
1049 GetDWLE(&st->sh.video.width);
1050 p_stream->fmt.video.i_height =
1051 GetDWLE(&st->sh.video.height);
1054 "fps: %f, width:%i; height:%i, bitcount:%i",
1056 p_stream->fmt.video.i_width,
1057 p_stream->fmt.video.i_height,
1058 p_stream->fmt.video.i_bits_per_pixel );
1060 /* Check for audio header (new format) */
1061 else if( !strncmp( st->streamtype, "audio", 5 ) )
1066 p_stream->fmt.i_cat = AUDIO_ES;
1068 /* We need to get rid of the header packet */
1069 ogg_stream_packetout( &p_stream->os, &oggpacket );
1071 p_stream->fmt.i_extra = GetQWLE(&st->size) -
1072 sizeof(stream_header);
1073 if( p_stream->fmt.i_extra )
1075 p_stream->fmt.p_extra =
1076 malloc( p_stream->fmt.i_extra );
1077 memcpy( p_stream->fmt.p_extra, st + 1,
1078 p_stream->fmt.i_extra );
1081 memcpy( p_buffer, st->subtype, 4 );
1083 i_format_tag = strtol(p_buffer,NULL,16);
1084 p_stream->fmt.audio.i_channels =
1085 GetWLE(&st->sh.audio.channels);
1086 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1087 GetQWLE(&st->samples_per_unit);
1088 p_stream->fmt.i_bitrate =
1089 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
1090 p_stream->fmt.audio.i_blockalign =
1091 GetWLE(&st->sh.audio.blockalign);
1092 p_stream->fmt.audio.i_bitspersample =
1093 GetWLE(&st->bits_per_sample);
1095 wf_tag_to_fourcc( i_format_tag,
1096 &p_stream->fmt.i_codec, 0 );
1098 if( p_stream->fmt.i_codec ==
1099 VLC_FOURCC('u','n','d','f') )
1101 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1102 ( i_format_tag >> 8 ) & 0xff,
1103 i_format_tag & 0xff );
1106 msg_Dbg( p_demux, "found audio header of type: %.4s",
1107 (char *)&p_stream->fmt.i_codec );
1108 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1109 "%dbits/sample %dkb/s",
1111 p_stream->fmt.audio.i_channels,
1112 p_stream->fmt.audio.i_rate,
1113 p_stream->fmt.audio.i_bitspersample,
1114 p_stream->fmt.i_bitrate / 1024 );
1116 /* Check for text (subtitles) header */
1117 else if( !strncmp(st->streamtype, "text", 4) )
1119 /* We need to get rid of the header packet */
1120 ogg_stream_packetout( &p_stream->os, &oggpacket );
1122 msg_Dbg( p_demux, "found text subtitles header" );
1123 p_stream->fmt.i_cat = SPU_ES;
1124 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1125 p_stream->f_rate = 1000; /* granulepos is in milisec */
1129 msg_Dbg( p_demux, "stream %d has a header marker "
1130 "but is of an unknown type", p_ogg->i_streams-1 );
1137 msg_Dbg( p_demux, "stream %d is of unknown type",
1138 p_ogg->i_streams-1 );
1143 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1144 return VLC_EGENERIC;
1147 /* This is the first data page, which means we are now finished
1148 * with the initial pages. We just need to store it in the relevant
1150 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1152 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1164 return VLC_EGENERIC;
1167 /****************************************************************************
1168 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1169 * Elementary streams.
1170 ****************************************************************************/
1171 static int Ogg_BeginningOfStream( demux_t *p_demux )
1173 demux_sys_t *p_ogg = p_demux->p_sys ;
1176 /* Find the logical streams embedded in the physical stream and
1177 * initialize our p_ogg structure. */
1178 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1180 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1181 return VLC_EGENERIC;
1184 p_ogg->i_bitrate = 0;
1186 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1188 #define p_stream p_ogg->pp_stream[i_stream]
1189 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1191 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1193 /* Set the CMML stream active */
1194 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1197 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1199 p_stream->i_pcr = p_stream->i_previous_pcr =
1200 p_stream->i_interpolated_pcr = -1;
1201 p_stream->b_reinit = 0;
1208 /****************************************************************************
1209 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1210 ****************************************************************************/
1211 static void Ogg_EndOfStream( demux_t *p_demux )
1213 demux_sys_t *p_ogg = p_demux->p_sys ;
1216 #define p_stream p_ogg->pp_stream[i_stream]
1217 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1219 if( p_stream->p_es )
1220 es_out_Del( p_demux->out, p_stream->p_es );
1222 p_ogg->i_bitrate -= p_stream->fmt.i_bitrate;
1224 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1225 if( p_ogg->pp_stream[i_stream]->p_headers)
1226 free( p_ogg->pp_stream[i_stream]->p_headers );
1228 es_format_Clean( &p_stream->fmt );
1230 free( p_ogg->pp_stream[i_stream] );
1235 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1236 p_ogg->pp_stream = NULL;
1237 p_ogg->i_streams = 0;
1240 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1241 ogg_packet *p_oggpacket )
1244 int i_fps_numerator;
1245 int i_fps_denominator;
1246 int i_keyframe_frequency_force;
1248 p_stream->fmt.i_cat = VIDEO_ES;
1249 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1251 /* Signal that we want to keep a backup of the theora
1252 * stream headers. They will be used when switching between
1254 p_stream->b_force_backup = 1;
1256 /* Cheat and get additionnal info ;) */
1257 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1258 bs_skip( &bitstream, 56 );
1259 bs_read( &bitstream, 8 ); /* major version num */
1260 bs_read( &bitstream, 8 ); /* minor version num */
1261 bs_read( &bitstream, 8 ); /* subminor version num */
1262 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1263 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1264 bs_read( &bitstream, 24 ); /* frame width */
1265 bs_read( &bitstream, 24 ); /* frame height */
1266 bs_read( &bitstream, 8 ); /* x offset */
1267 bs_read( &bitstream, 8 ); /* y offset */
1269 i_fps_numerator = bs_read( &bitstream, 32 );
1270 i_fps_denominator = bs_read( &bitstream, 32 );
1271 bs_read( &bitstream, 24 ); /* aspect_numerator */
1272 bs_read( &bitstream, 24 ); /* aspect_denominator */
1274 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1275 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1277 bs_read( &bitstream, 8 ); /* colorspace */
1278 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1279 bs_read( &bitstream, 6 ); /* quality */
1281 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1283 /* granule_shift = i_log( frequency_force -1 ) */
1284 p_stream->i_theora_keyframe_granule_shift = 0;
1285 i_keyframe_frequency_force--;
1286 while( i_keyframe_frequency_force )
1288 p_stream->i_theora_keyframe_granule_shift++;
1289 i_keyframe_frequency_force >>= 1;
1292 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1295 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1296 ogg_packet *p_oggpacket )
1300 p_stream->fmt.i_cat = AUDIO_ES;
1301 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1303 /* Signal that we want to keep a backup of the vorbis
1304 * stream headers. They will be used when switching between
1306 p_stream->b_force_backup = 1;
1308 /* Cheat and get additionnal info ;) */
1309 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1310 oggpack_adv( &opb, 88 );
1311 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1312 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1313 oggpack_read( &opb, 32 );
1314 oggpack_adv( &opb, 32 );
1315 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1318 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1319 ogg_packet *p_oggpacket )
1323 p_stream->fmt.i_cat = AUDIO_ES;
1324 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1326 /* Signal that we want to keep a backup of the speex
1327 * stream headers. They will be used when switching between
1329 p_stream->b_force_backup = 1;
1331 /* Cheat and get additionnal info ;) */
1332 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1333 oggpack_adv( &opb, 224 );
1334 oggpack_adv( &opb, 32 ); /* speex_version_id */
1335 oggpack_adv( &opb, 32 ); /* header_size */
1336 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1337 oggpack_adv( &opb, 32 ); /* mode */
1338 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1339 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1340 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1343 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1344 ogg_packet *p_oggpacket )
1346 /* Parse the STREAMINFO metadata */
1349 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1352 if( bs_read( &s, 7 ) == 0 )
1354 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1357 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1358 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1360 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1361 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1363 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1365 /* Fake this as the last metadata block */
1366 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1370 /* This ain't a STREAMINFO metadata */
1371 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1375 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1376 logical_stream_t *p_stream,
1377 ogg_packet *p_oggpacket )
1379 if( p_oggpacket->bytes >= 28 &&
1380 !memcmp( &p_oggpacket->packet[0], "Annodex", 7 ) )
1384 uint16_t major_version;
1385 uint16_t minor_version;
1386 uint64_t timebase_numerator;
1387 uint64_t timebase_denominator;
1389 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1391 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1392 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1393 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1394 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1395 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1396 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1398 else if( p_oggpacket->bytes >= 42 &&
1399 !memcmp( &p_oggpacket->packet[0], "AnxData", 7 ) )
1401 uint64_t granule_rate_numerator;
1402 uint64_t granule_rate_denominator;
1403 char content_type_string[1024];
1405 /* Read in Annodex header fields */
1407 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1408 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1409 p_stream->secondary_header_packets =
1410 GetDWLE( &p_oggpacket->packet[24] );
1412 /* we are guaranteed that the first header field will be
1413 * the content-type (by the Annodex standard) */
1414 content_type_string[0] = '\0';
1415 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1417 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1418 p_oggpacket->bytes - 1 );
1419 if( p && p[0] == '\r' && p[1] == '\n' )
1420 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1421 content_type_string );
1424 msg_Dbg( p_this, "AnxData packet info: "I64Fd" / "I64Fd", %d, ``%s''",
1425 granule_rate_numerator, granule_rate_denominator,
1426 p_stream->secondary_header_packets, content_type_string );
1428 p_stream->f_rate = (float) granule_rate_numerator /
1429 (float) granule_rate_denominator;
1431 /* What type of file do we have?
1432 * strcmp is safe to use here because we've extracted
1433 * content_type_string from the stream manually */
1434 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1436 /* n.b. WAVs are unsupported right now */
1437 p_stream->fmt.i_cat = UNKNOWN_ES;
1439 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1441 p_stream->fmt.i_cat = AUDIO_ES;
1442 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1444 p_stream->b_force_backup = 1;
1446 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1448 p_stream->fmt.i_cat = AUDIO_ES;
1449 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1451 p_stream->b_force_backup = 1;
1453 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1455 p_stream->fmt.i_cat = VIDEO_ES;
1456 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1458 p_stream->b_force_backup = 1;
1460 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1462 p_stream->fmt.i_cat = VIDEO_ES;
1463 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1465 p_stream->b_force_backup = 1;
1467 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1469 /* n.b. MPEG streams are unsupported right now */
1470 p_stream->fmt.i_cat = VIDEO_ES;
1471 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1473 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1475 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1476 p_stream->fmt.i_cat = SPU_ES;
1477 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );