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 )
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 */
68 es_format_t fmt_old; /* format of old ES is reused */
74 /* the header of some logical streams (eg vorbis) contain essential
75 * data for the decoder. We back them up here in case we need to re-feed
76 * them to the decoder. */
82 /* program clock reference (in units of 90kHz) derived from the previous
85 mtime_t i_interpolated_pcr;
86 mtime_t i_previous_pcr;
92 /* kate streams have the number of headers in the ID header */
93 int i_kate_num_headers;
95 /* for Annodex logical bitstreams */
96 int secondary_header_packets;
102 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
104 int i_streams; /* number of logical bitstreams */
105 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
107 logical_stream_t *p_old_stream; /* pointer to a old logical stream to avoid recreating it */
109 /* program clock reference (in units of 90kHz) derived from the pcr of
120 /* OggDS headers for the new header format (used in ogm files) */
125 } stream_header_video_t;
129 ogg_int16_t channels;
131 ogg_int16_t blockalign;
132 ogg_int32_t avgbytespersec;
133 } stream_header_audio_t;
140 ogg_int32_t size; /* size of the structure */
142 ogg_int64_t time_unit; /* in reference time */
143 ogg_int64_t samples_per_unit;
144 ogg_int32_t default_len; /* in media time */
146 ogg_int32_t buffersize;
147 ogg_int16_t bits_per_sample;
153 stream_header_video_t video;
155 stream_header_audio_t audio;
159 #define OGG_BLOCK_SIZE 4096
161 /* Some defines from OggDS */
162 #define PACKET_TYPE_HEADER 0x01
163 #define PACKET_TYPE_BITS 0x07
164 #define PACKET_LEN_BITS01 0xc0
165 #define PACKET_LEN_BITS2 0x02
166 #define PACKET_IS_SYNCPOINT 0x08
168 /*****************************************************************************
170 *****************************************************************************/
171 static int Demux ( demux_t * );
172 static int Control( demux_t *, int, va_list );
174 /* Bitstream manipulation */
175 static int Ogg_ReadPage ( demux_t *, ogg_page * );
176 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
177 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
179 static int Ogg_BeginningOfStream( demux_t *p_demux );
180 static int Ogg_FindLogicalStreams( demux_t *p_demux );
181 static void Ogg_EndOfStream( demux_t *p_demux );
184 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
185 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
187 /* Logical bitstream headers */
188 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
189 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
190 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
191 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
192 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
193 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
194 static void Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
196 /*****************************************************************************
197 * Open: initializes ogg demux structures
198 *****************************************************************************/
199 static int Open( vlc_object_t * p_this )
201 demux_t *p_demux = (demux_t *)p_this;
203 const uint8_t *p_peek;
206 /* Check if we are dealing with an ogg stream */
207 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
208 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
213 /* Set exported functions */
214 p_demux->pf_demux = Demux;
215 p_demux->pf_control = Control;
216 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
220 memset( p_sys, 0, sizeof( demux_sys_t ) );
221 p_sys->i_bitrate = 0;
222 p_sys->pp_stream = NULL;
223 p_sys->p_old_stream = NULL;
225 /* Begnning of stream, tell the demux to look for elementary streams. */
228 /* Initialize the Ogg physical bitstream parser */
229 ogg_sync_init( &p_sys->oy );
234 /*****************************************************************************
235 * Close: frees unused data
236 *****************************************************************************/
237 static void Close( vlc_object_t *p_this )
239 demux_t *p_demux = (demux_t *)p_this;
240 demux_sys_t *p_sys = p_demux->p_sys ;
242 /* Cleanup the bitstream parser */
243 ogg_sync_clear( &p_sys->oy );
245 Ogg_EndOfStream( p_demux );
247 if( p_sys->p_old_stream )
248 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
253 /*****************************************************************************
254 * Demux: reads and demuxes data packets
255 *****************************************************************************
256 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
257 *****************************************************************************/
258 static int Demux( demux_t * p_demux )
260 demux_sys_t *p_sys = p_demux->p_sys;
262 ogg_packet oggpacket;
266 if( p_sys->i_eos == p_sys->i_streams )
270 msg_Dbg( p_demux, "end of a group of logical streams" );
271 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
272 * only 1 ES is supported (common case for ogg web radio) */
273 if( p_sys->i_streams == 1 )
275 p_sys->p_old_stream = p_sys->pp_stream[0];
276 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
278 Ogg_EndOfStream( p_demux );
282 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
285 msg_Dbg( p_demux, "beginning of a group of logical streams" );
286 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
290 * Demux an ogg page from the stream
292 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
297 /* Test for End of Stream */
298 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
301 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
303 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
305 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
308 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
310 /* Read info from any secondary header packets, if there are any */
311 if( p_stream->secondary_header_packets > 0 )
313 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
314 oggpacket.bytes >= 7 &&
315 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
317 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
318 p_stream->secondary_header_packets = 0;
320 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
321 oggpacket.bytes >= 7 &&
322 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
324 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
325 p_stream->secondary_header_packets = 0;
327 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
329 p_stream->secondary_header_packets = 0;
333 if( p_stream->b_reinit )
335 /* If synchro is re-initialized we need to drop all the packets
336 * until we find a new dated one. */
337 Ogg_UpdatePCR( p_stream, &oggpacket );
339 if( p_stream->i_pcr >= 0 )
341 p_stream->b_reinit = 0;
345 p_stream->i_interpolated_pcr = -1;
349 /* An Ogg/vorbis packet contains an end date granulepos */
350 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
351 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
352 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
354 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
356 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
360 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
367 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
372 i_stream = 0; p_sys->i_pcr = -1;
373 for( ; i_stream < p_sys->i_streams; i_stream++ )
375 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
377 if( p_stream->fmt.i_cat == SPU_ES )
379 if( p_stream->i_interpolated_pcr < 0 )
382 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
383 p_sys->i_pcr = p_stream->i_interpolated_pcr;
386 if( p_sys->i_pcr >= 0 )
388 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
394 /*****************************************************************************
396 *****************************************************************************/
397 static int Control( demux_t *p_demux, int i_query, va_list args )
399 demux_sys_t *p_sys = p_demux->p_sys;
406 case DEMUX_HAS_UNSUPPORTED_META:
407 pb_bool = (bool*)va_arg( args, bool* );
412 pi64 = (int64_t*)va_arg( args, int64_t * );
413 *pi64 = p_sys->i_pcr;
419 case DEMUX_SET_POSITION:
420 for( i = 0; i < p_sys->i_streams; i++ )
422 logical_stream_t *p_stream = p_sys->pp_stream[i];
424 /* we'll trash all the data until we find the next pcr */
425 p_stream->b_reinit = 1;
426 p_stream->i_pcr = -1;
427 p_stream->i_interpolated_pcr = -1;
428 ogg_stream_reset( &p_stream->os );
430 ogg_sync_reset( &p_sys->oy );
433 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
438 /****************************************************************************
439 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
440 ****************************************************************************
441 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
442 * are at the end of stream.
443 ****************************************************************************/
444 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
446 demux_sys_t *p_ogg = p_demux->p_sys ;
450 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
452 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
454 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
458 ogg_sync_wrote( &p_ogg->oy, i_read );
464 /****************************************************************************
465 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
467 ****************************************************************************/
468 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
469 ogg_packet *p_oggpacket )
471 /* Convert the granulepos into a pcr */
472 if( p_oggpacket->granulepos >= 0 )
474 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
475 p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
477 ogg_int64_t iframe = p_oggpacket->granulepos >>
478 p_stream->i_granule_shift;
479 ogg_int64_t pframe = p_oggpacket->granulepos -
480 ( iframe << p_stream->i_granule_shift );
482 p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
485 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
487 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
488 /* NB, OggDirac granulepos values are in units of 2*picturerate */
489 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
493 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
497 p_stream->i_interpolated_pcr = p_stream->i_pcr;
501 p_stream->i_pcr = -1;
503 /* no granulepos available, try to interpolate the pcr.
504 * If we can't then don't touch the old value. */
505 if( p_stream->fmt.i_cat == VIDEO_ES )
506 /* 1 frame per packet */
507 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
508 else if( p_stream->fmt.i_bitrate )
509 p_stream->i_interpolated_pcr +=
510 ( p_oggpacket->bytes * INT64_C(1000000) /
511 p_stream->fmt.i_bitrate / 8 );
515 /****************************************************************************
516 * Ogg_DecodePacket: Decode an Ogg packet.
517 ****************************************************************************/
518 static void Ogg_DecodePacket( demux_t *p_demux,
519 logical_stream_t *p_stream,
520 ogg_packet *p_oggpacket )
524 int i_header_len = 0;
525 mtime_t i_pts = -1, i_interpolated_pts;
528 if( !p_oggpacket->bytes )
530 msg_Dbg( p_demux, "discarding 0 sized packet" );
534 if( p_oggpacket->bytes >= 7 &&
535 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
537 /* it's an Annodex packet -- skip it (do nothing) */
540 else if( p_oggpacket->bytes >= 7 &&
541 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
543 /* it's an AnxData packet -- skip it (do nothing) */
547 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
548 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
550 /* Check the ES is selected */
551 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
552 p_stream->p_es, &b_selected );
554 if( p_stream->b_force_backup )
557 bool b_store_size = true;
558 bool b_store_num_headers = false;
560 p_stream->i_packets_backup++;
561 switch( p_stream->fmt.i_codec )
563 case VLC_FOURCC( 'v','o','r','b' ):
564 case VLC_FOURCC( 's','p','x',' ' ):
565 case VLC_FOURCC( 't','h','e','o' ):
566 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
569 case VLC_FOURCC( 'f','l','a','c' ):
570 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
572 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
573 p_stream->b_force_backup = 0;
575 else if( p_stream->fmt.audio.i_rate )
577 p_stream->b_force_backup = 0;
578 if( p_oggpacket->bytes >= 9 )
580 p_oggpacket->packet += 9;
581 p_oggpacket->bytes -= 9;
584 b_store_size = false;
587 case VLC_FOURCC( 'k','a','t','e' ):
588 if( p_stream->i_packets_backup == 1)
589 b_store_num_headers = true;
590 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
594 p_stream->b_force_backup = 0;
598 /* Backup the ogg packet (likely an header packet) */
599 p_stream->p_headers =
600 realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
601 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
602 if( p_stream->p_headers )
604 uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
606 if( b_store_num_headers )
608 /* Kate streams store the number of headers in the first header,
609 so we can't just test for 3 as Vorbis/Theora */
610 *(p_extra++) = p_stream->i_kate_num_headers;
614 *(p_extra++) = p_oggpacket->bytes >> 8;
615 *(p_extra++) = p_oggpacket->bytes & 0xFF;
617 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
618 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
620 if( !p_stream->b_force_backup )
622 /* Last header received, commit changes */
623 free( p_stream->fmt.p_extra );
625 p_stream->fmt.i_extra = p_stream->i_headers;
626 p_stream->fmt.p_extra =
627 realloc( p_stream->fmt.p_extra, p_stream->i_headers );
628 if( p_stream->fmt.p_extra )
629 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
630 p_stream->i_headers );
632 p_stream->fmt.i_extra = 0;
634 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
635 es_out_Control( p_demux->out, ES_OUT_SET_FMT,
636 p_stream->p_es, &p_stream->fmt );
641 p_stream->p_headers = p_sav;
644 b_selected = false; /* Discard the header packet */
647 /* Convert the pcr into a 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' ) )
652 if( p_stream->i_pcr >= 0 )
654 /* This is for streams where the granulepos of the header packets
655 * doesn't match these of the data packets (eg. ogg web radios). */
656 if( p_stream->i_previous_pcr == 0 &&
657 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
659 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
661 /* Call the pace control */
662 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
666 p_stream->i_previous_pcr = p_stream->i_pcr;
668 /* The granulepos is the end date of the sample */
669 i_pts = p_stream->i_pcr;
673 /* Convert the granulepos into the next pcr */
674 i_interpolated_pts = p_stream->i_interpolated_pcr;
675 Ogg_UpdatePCR( p_stream, p_oggpacket );
677 if( p_stream->i_pcr >= 0 )
679 /* This is for streams where the granulepos of the header packets
680 * doesn't match these of the data packets (eg. ogg web radios). */
681 if( p_stream->i_previous_pcr == 0 &&
682 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
684 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
686 /* Call the pace control */
687 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
691 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
692 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
693 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
694 p_stream->i_pcr >= 0 )
696 p_stream->i_previous_pcr = p_stream->i_pcr;
698 /* The granulepos is the start date of the sample */
699 i_pts = p_stream->i_pcr;
704 /* This stream isn't currently selected so we don't need to decode it,
705 * but we did need to store its pcr as it might be selected later on */
709 if( p_oggpacket->bytes <= 0 )
712 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
715 if( i_pts == 0 ) i_pts = 1;
716 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
717 else if( i_pts == -1 ) i_pts = 0;
719 if( p_stream->fmt.i_cat == AUDIO_ES )
720 p_block->i_dts = p_block->i_pts = i_pts;
721 else if( p_stream->fmt.i_cat == SPU_ES )
723 p_block->i_dts = p_block->i_pts = i_pts;
724 p_block->i_length = 0;
726 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
727 p_block->i_dts = p_block->i_pts = i_pts;
728 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
730 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
731 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
733 uint64_t u_pnum = dts + delay;
735 p_block->i_dts = p_stream->i_pcr;
737 /* NB, OggDirac granulepos values are in units of 2*picturerate */
738 if( -1 != p_oggpacket->granulepos )
739 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
743 p_block->i_dts = i_pts;
747 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
748 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
749 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
750 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
751 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
752 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
753 p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
754 p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
756 /* We remove the header from the packet */
757 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
758 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
760 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
762 /* But with subtitles we need to retrieve the duration first */
765 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
767 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
769 lenbytes = lenbytes << 8;
770 lenbytes += *(p_oggpacket->packet + i_header_len - i);
773 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
774 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
775 p_oggpacket->packet[i_header_len + 1] != 0 &&
776 p_oggpacket->packet[i_header_len + 1] != '\n' &&
777 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
779 p_block->i_length = (mtime_t)lenbytes * 1000;
784 if( p_block->i_buffer >= (unsigned int)i_header_len )
785 p_block->i_buffer -= i_header_len;
787 p_block->i_buffer = 0;
790 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
792 /* FIXME: the biggest hack I've ever done */
793 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
794 p_block->i_pts, p_block->i_dts );
798 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
799 p_oggpacket->bytes - i_header_len );
801 es_out_Send( p_demux->out, p_stream->p_es, p_block );
804 /****************************************************************************
805 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
806 * stream and fill p_ogg.
807 *****************************************************************************
808 * The initial page of a logical stream is marked as a 'bos' page.
809 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
810 * together and all of the initial pages must appear before any data pages.
812 * On success this function returns VLC_SUCCESS.
813 ****************************************************************************/
814 static int Ogg_FindLogicalStreams( demux_t *p_demux )
816 demux_sys_t *p_ogg = p_demux->p_sys ;
817 ogg_packet oggpacket;
821 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
823 if( ogg_page_bos( &oggpage ) )
826 /* All is wonderful in our fine fine little world.
827 * We found the beginning of our first logical stream. */
828 while( ogg_page_bos( &oggpage ) )
830 logical_stream_t **pp_sav = p_ogg->pp_stream;
831 logical_stream_t *p_stream;
833 p_stream = malloc( sizeof(logical_stream_t) );
837 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
839 memset( p_stream, 0, sizeof(logical_stream_t) );
840 p_stream->p_headers = 0;
841 p_stream->secondary_header_packets = 0;
843 es_format_Init( &p_stream->fmt, 0, 0 );
844 es_format_Init( &p_stream->fmt_old, 0, 0 );
846 /* Setup the logical stream */
847 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
848 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
850 /* Extract the initial header from the first page and verify
851 * the codec type of tis Ogg bitstream */
852 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
854 /* error. stream version mismatch perhaps */
855 msg_Err( p_demux, "error reading first page of "
856 "Ogg bitstream data" );
860 /* FIXME: check return value */
861 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
863 /* Check for Vorbis header */
864 if( oggpacket.bytes >= 7 &&
865 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
867 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
868 msg_Dbg( p_demux, "found vorbis header" );
870 /* Check for Speex header */
871 else if( oggpacket.bytes >= 5 &&
872 ! memcmp( oggpacket.packet, "Speex", 5 ) )
874 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
875 msg_Dbg( p_demux, "found speex header, channels: %i, "
876 "rate: %i, bitrate: %i",
877 p_stream->fmt.audio.i_channels,
878 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
880 /* Check for Flac header (< version 1.1.1) */
881 else if( oggpacket.bytes >= 4 &&
882 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
884 msg_Dbg( p_demux, "found FLAC header" );
886 /* Grrrr!!!! Did they really have to put all the
887 * important info in the second header packet!!!
888 * (STREAMINFO metadata is in the following packet) */
889 p_stream->b_force_backup = 1;
891 p_stream->fmt.i_cat = AUDIO_ES;
892 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
894 /* Check for Flac header (>= version 1.1.1) */
895 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
896 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
897 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
899 int i_packets = ((int)oggpacket.packet[7]) << 8 |
901 msg_Dbg( p_demux, "found FLAC header version %i.%i "
902 "(%i header packets)",
903 oggpacket.packet[5], oggpacket.packet[6],
906 p_stream->b_force_backup = 1;
908 p_stream->fmt.i_cat = AUDIO_ES;
909 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
910 oggpacket.packet += 13; oggpacket.bytes -= 13;
911 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
913 /* Check for Theora header */
914 else if( oggpacket.bytes >= 7 &&
915 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
917 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
920 "found theora header, bitrate: %i, rate: %f",
921 p_stream->fmt.i_bitrate, p_stream->f_rate );
923 /* Check for Dirac header */
924 else if( oggpacket.bytes >= 5 &&
925 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
927 Ogg_ReadDiracHeader( p_stream, &oggpacket );
928 msg_Dbg( p_demux, "found dirac header" );
930 /* Check for Tarkin header */
931 else if( oggpacket.bytes >= 7 &&
932 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
936 msg_Dbg( p_demux, "found tarkin header" );
937 p_stream->fmt.i_cat = VIDEO_ES;
938 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
940 /* Cheat and get additionnal info ;) */
941 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
942 oggpack_adv( &opb, 88 );
943 oggpack_adv( &opb, 104 );
944 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
945 p_stream->f_rate = 2; /* FIXME */
947 "found tarkin header, bitrate: %i, rate: %f",
948 p_stream->fmt.i_bitrate, p_stream->f_rate );
950 /* Check for Annodex header */
951 else if( oggpacket.bytes >= 7 &&
952 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
954 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
956 /* kill annodex track */
960 /* Check for Annodex header */
961 else if( oggpacket.bytes >= 7 &&
962 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
964 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
967 /* Check for Kate header */
968 else if( oggpacket.bytes >= 8 &&
969 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
971 Ogg_ReadKateHeader( p_stream, &oggpacket );
972 msg_Dbg( p_demux, "found kate header" );
974 else if( oggpacket.bytes >= 142 &&
975 !memcmp( &oggpacket.packet[1],
976 "Direct Show Samples embedded in Ogg", 35 ))
978 /* Old header type */
980 /* Check for video header (old format) */
981 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
982 oggpacket.bytes >= 184 )
984 p_stream->fmt.i_cat = VIDEO_ES;
985 p_stream->fmt.i_codec =
986 VLC_FOURCC( oggpacket.packet[68],
987 oggpacket.packet[69],
988 oggpacket.packet[70],
989 oggpacket.packet[71] );
990 msg_Dbg( p_demux, "found video header of type: %.4s",
991 (char *)&p_stream->fmt.i_codec );
993 p_stream->fmt.video.i_frame_rate = 10000000;
994 p_stream->fmt.video.i_frame_rate_base =
995 GetQWLE((oggpacket.packet+164));
996 p_stream->f_rate = 10000000.0 /
997 GetQWLE((oggpacket.packet+164));
998 p_stream->fmt.video.i_bits_per_pixel =
999 GetWLE((oggpacket.packet+182));
1000 if( !p_stream->fmt.video.i_bits_per_pixel )
1002 p_stream->fmt.video.i_bits_per_pixel = 24;
1003 p_stream->fmt.video.i_width =
1004 GetDWLE((oggpacket.packet+176));
1005 p_stream->fmt.video.i_height =
1006 GetDWLE((oggpacket.packet+180));
1009 "fps: %f, width:%i; height:%i, bitcount:%i",
1011 p_stream->fmt.video.i_width,
1012 p_stream->fmt.video.i_height,
1013 p_stream->fmt.video.i_bits_per_pixel);
1016 /* Check for audio header (old format) */
1017 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1019 unsigned int i_extra_size;
1020 unsigned int i_format_tag;
1022 p_stream->fmt.i_cat = AUDIO_ES;
1024 i_extra_size = GetWLE((oggpacket.packet+140));
1025 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1027 p_stream->fmt.i_extra = i_extra_size;
1028 p_stream->fmt.p_extra = malloc( i_extra_size );
1029 if( p_stream->fmt.p_extra )
1030 memcpy( p_stream->fmt.p_extra,
1031 oggpacket.packet + 142, i_extra_size );
1033 p_stream->fmt.i_extra = 0;
1036 i_format_tag = GetWLE((oggpacket.packet+124));
1037 p_stream->fmt.audio.i_channels =
1038 GetWLE((oggpacket.packet+126));
1039 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1040 GetDWLE((oggpacket.packet+128));
1041 p_stream->fmt.i_bitrate =
1042 GetDWLE((oggpacket.packet+132)) * 8;
1043 p_stream->fmt.audio.i_blockalign =
1044 GetWLE((oggpacket.packet+136));
1045 p_stream->fmt.audio.i_bitspersample =
1046 GetWLE((oggpacket.packet+138));
1048 wf_tag_to_fourcc( i_format_tag,
1049 &p_stream->fmt.i_codec, 0 );
1051 if( p_stream->fmt.i_codec ==
1052 VLC_FOURCC('u','n','d','f') )
1054 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1055 ( i_format_tag >> 8 ) & 0xff,
1056 i_format_tag & 0xff );
1059 msg_Dbg( p_demux, "found audio header of type: %.4s",
1060 (char *)&p_stream->fmt.i_codec );
1061 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1062 "%dbits/sample %dkb/s",
1064 p_stream->fmt.audio.i_channels,
1065 p_stream->fmt.audio.i_rate,
1066 p_stream->fmt.audio.i_bitspersample,
1067 p_stream->fmt.i_bitrate / 1024 );
1072 msg_Dbg( p_demux, "stream %d has an old header "
1073 "but is of an unknown type", p_ogg->i_streams-1 );
1078 else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1079 oggpacket.bytes >= 56+1 )
1081 stream_header_t tmp;
1082 stream_header_t *st = &tmp;
1084 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1085 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1086 st->size = GetDWLE( &oggpacket.packet[1+12] );
1087 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1088 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1089 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1090 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1091 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1093 /* Check for video header (new format) */
1094 if( !strncmp( st->streamtype, "video", 5 ) )
1096 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1097 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1099 p_stream->fmt.i_cat = VIDEO_ES;
1101 /* We need to get rid of the header packet */
1102 ogg_stream_packetout( &p_stream->os, &oggpacket );
1104 p_stream->fmt.i_codec =
1105 VLC_FOURCC( st->subtype[0], st->subtype[1],
1106 st->subtype[2], st->subtype[3] );
1107 msg_Dbg( p_demux, "found video header of type: %.4s",
1108 (char *)&p_stream->fmt.i_codec );
1110 p_stream->fmt.video.i_frame_rate = 10000000;
1111 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1112 if( st->time_unit <= 0 )
1113 st->time_unit = 400000;
1114 p_stream->f_rate = 10000000.0 / st->time_unit;
1115 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1116 p_stream->fmt.video.i_width = st->sh.video.width;
1117 p_stream->fmt.video.i_height = st->sh.video.height;
1120 "fps: %f, width:%i; height:%i, bitcount:%i",
1122 p_stream->fmt.video.i_width,
1123 p_stream->fmt.video.i_height,
1124 p_stream->fmt.video.i_bits_per_pixel );
1126 /* Check for audio header (new format) */
1127 else if( !strncmp( st->streamtype, "audio", 5 ) )
1130 unsigned int i_extra_size;
1133 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1134 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1135 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1137 p_stream->fmt.i_cat = AUDIO_ES;
1139 /* We need to get rid of the header packet */
1140 ogg_stream_packetout( &p_stream->os, &oggpacket );
1142 i_extra_size = st->size - 56;
1144 if( i_extra_size > 0 &&
1145 i_extra_size < oggpacket.bytes - 1 - 56 )
1147 p_stream->fmt.i_extra = i_extra_size;
1148 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1149 if( p_stream->fmt.p_extra )
1150 memcpy( p_stream->fmt.p_extra, st + 1,
1151 p_stream->fmt.i_extra );
1153 p_stream->fmt.i_extra = 0;
1156 memcpy( p_buffer, st->subtype, 4 );
1158 i_format_tag = strtol(p_buffer,NULL,16);
1159 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1160 if( st->time_unit <= 0 )
1161 st->time_unit = 10000000;
1162 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1163 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1164 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1165 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1167 wf_tag_to_fourcc( i_format_tag,
1168 &p_stream->fmt.i_codec, 0 );
1170 if( p_stream->fmt.i_codec ==
1171 VLC_FOURCC('u','n','d','f') )
1173 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1174 ( i_format_tag >> 8 ) & 0xff,
1175 i_format_tag & 0xff );
1178 msg_Dbg( p_demux, "found audio header of type: %.4s",
1179 (char *)&p_stream->fmt.i_codec );
1180 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1181 "%dbits/sample %dkb/s",
1183 p_stream->fmt.audio.i_channels,
1184 p_stream->fmt.audio.i_rate,
1185 p_stream->fmt.audio.i_bitspersample,
1186 p_stream->fmt.i_bitrate / 1024 );
1188 /* Check for text (subtitles) header */
1189 else if( !strncmp(st->streamtype, "text", 4) )
1191 /* We need to get rid of the header packet */
1192 ogg_stream_packetout( &p_stream->os, &oggpacket );
1194 msg_Dbg( p_demux, "found text subtitles header" );
1195 p_stream->fmt.i_cat = SPU_ES;
1196 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1197 p_stream->f_rate = 1000; /* granulepos is in millisec */
1201 msg_Dbg( p_demux, "stream %d has a header marker "
1202 "but is of an unknown type", p_ogg->i_streams-1 );
1207 else if( oggpacket.bytes >= 7 &&
1208 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1212 msg_Dbg( p_demux, "stream %d is a skeleton",
1213 p_ogg->i_streams-1 );
1214 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1218 msg_Dbg( p_demux, "stream %d is of unknown type",
1219 p_ogg->i_streams-1 );
1224 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1225 return VLC_EGENERIC;
1228 /* This is the first data page, which means we are now finished
1229 * with the initial pages. We just need to store it in the relevant
1231 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1233 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1244 return VLC_EGENERIC;
1247 /****************************************************************************
1248 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1249 * Elementary streams.
1250 ****************************************************************************/
1251 static int Ogg_BeginningOfStream( demux_t *p_demux )
1253 demux_sys_t *p_ogg = p_demux->p_sys ;
1254 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1257 /* Find the logical streams embedded in the physical stream and
1258 * initialize our p_ogg structure. */
1259 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1261 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1262 return VLC_EGENERIC;
1265 p_ogg->i_bitrate = 0;
1267 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1269 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1271 p_stream->p_es = NULL;
1273 /* Try first to reuse an old ES */
1275 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1276 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1278 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1280 p_stream->p_es = p_old_stream->p_es;
1281 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1283 p_old_stream->p_es = NULL;
1284 p_old_stream = NULL;
1287 if( !p_stream->p_es )
1288 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1290 // TODO: something to do here ?
1291 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1293 /* Set the CMML stream active */
1294 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1297 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1299 p_stream->i_pcr = p_stream->i_previous_pcr =
1300 p_stream->i_interpolated_pcr = -1;
1301 p_stream->b_reinit = 0;
1304 if( p_ogg->p_old_stream )
1306 if( p_ogg->p_old_stream->p_es )
1307 msg_Dbg( p_demux, "old stream not reused" );
1308 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1309 p_ogg->p_old_stream = NULL;
1314 /****************************************************************************
1315 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1316 ****************************************************************************/
1317 static void Ogg_EndOfStream( demux_t *p_demux )
1319 demux_sys_t *p_ogg = p_demux->p_sys ;
1322 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1323 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1324 free( p_ogg->pp_stream );
1327 p_ogg->i_bitrate = 0;
1328 p_ogg->i_streams = 0;
1329 p_ogg->pp_stream = NULL;
1333 * This function delete and release all data associated to a logical_stream_t
1335 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1337 if( p_stream->p_es )
1338 es_out_Del( p_demux->out, p_stream->p_es );
1340 ogg_stream_clear( &p_stream->os );
1341 free( p_stream->p_headers );
1343 es_format_Clean( &p_stream->fmt_old );
1344 es_format_Clean( &p_stream->fmt );
1349 * This function check if a we need to reset a decoder in case we are
1352 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1358 for( i = 0; i < 3; i++ )
1360 const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1361 const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1363 if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1366 const int i_new_size = GetWBE( &p_new_extra[0] );
1367 const int i_old_size = GetWBE( &p_old_extra[0] );
1369 if( i != 1 ) /* Ignore vorbis comment */
1371 if( i_new_size != i_old_size )
1373 if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1377 i_new += 2 + i_new_size;
1378 i_old += 2 + i_old_size;
1382 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1384 bool b_compatible = false;
1385 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1388 /* Only vorbis is supported */
1389 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) )
1390 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1393 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1395 return !b_compatible;
1398 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1399 ogg_packet *p_oggpacket )
1402 int i_fps_numerator;
1403 int i_fps_denominator;
1404 int i_keyframe_frequency_force;
1406 p_stream->fmt.i_cat = VIDEO_ES;
1407 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1409 /* Signal that we want to keep a backup of the theora
1410 * stream headers. They will be used when switching between
1412 p_stream->b_force_backup = 1;
1414 /* Cheat and get additionnal info ;) */
1415 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1416 bs_skip( &bitstream, 56 );
1417 bs_read( &bitstream, 8 ); /* major version num */
1418 bs_read( &bitstream, 8 ); /* minor version num */
1419 bs_read( &bitstream, 8 ); /* subminor version num */
1420 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1421 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1422 bs_read( &bitstream, 24 ); /* frame width */
1423 bs_read( &bitstream, 24 ); /* frame height */
1424 bs_read( &bitstream, 8 ); /* x offset */
1425 bs_read( &bitstream, 8 ); /* y offset */
1427 i_fps_numerator = bs_read( &bitstream, 32 );
1428 i_fps_denominator = bs_read( &bitstream, 32 );
1429 bs_read( &bitstream, 24 ); /* aspect_numerator */
1430 bs_read( &bitstream, 24 ); /* aspect_denominator */
1432 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1433 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1435 bs_read( &bitstream, 8 ); /* colorspace */
1436 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1437 bs_read( &bitstream, 6 ); /* quality */
1439 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1441 /* granule_shift = i_log( frequency_force -1 ) */
1442 p_stream->i_granule_shift = 0;
1443 i_keyframe_frequency_force--;
1444 while( i_keyframe_frequency_force )
1446 p_stream->i_granule_shift++;
1447 i_keyframe_frequency_force >>= 1;
1450 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1453 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1454 ogg_packet *p_oggpacket )
1458 p_stream->fmt.i_cat = AUDIO_ES;
1459 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1461 /* Signal that we want to keep a backup of the vorbis
1462 * stream headers. They will be used when switching between
1464 p_stream->b_force_backup = 1;
1466 /* Cheat and get additionnal info ;) */
1467 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1468 oggpack_adv( &opb, 88 );
1469 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1470 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1471 oggpack_read( &opb, 32 );
1472 oggpack_adv( &opb, 32 );
1473 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1476 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1477 ogg_packet *p_oggpacket )
1481 p_stream->fmt.i_cat = AUDIO_ES;
1482 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1484 /* Signal that we want to keep a backup of the speex
1485 * stream headers. They will be used when switching between
1487 p_stream->b_force_backup = 1;
1489 /* Cheat and get additionnal info ;) */
1490 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1491 oggpack_adv( &opb, 224 );
1492 oggpack_adv( &opb, 32 ); /* speex_version_id */
1493 oggpack_adv( &opb, 32 ); /* header_size */
1494 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1495 oggpack_adv( &opb, 32 ); /* mode */
1496 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1497 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1498 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1501 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1502 ogg_packet *p_oggpacket )
1504 /* Parse the STREAMINFO metadata */
1507 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1510 if( bs_read( &s, 7 ) == 0 )
1512 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1515 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1516 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1518 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1519 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1521 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1523 /* Fake this as the last metadata block */
1524 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1528 /* This ain't a STREAMINFO metadata */
1529 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1533 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1534 ogg_packet *p_oggpacket )
1541 p_stream->fmt.i_cat = SPU_ES;
1542 p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1544 /* Signal that we want to keep a backup of the kate
1545 * stream headers. They will be used when switching between
1547 p_stream->b_force_backup = 1;
1549 /* Cheat and get additionnal info ;) */
1550 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1551 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1552 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1553 oggpack_adv( &opb, 3*8 );
1554 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1555 oggpack_adv( &opb, 8*8 ); /* reserved */
1556 gnum = oggpack_read( &opb, 32 );
1557 gden = oggpack_read( &opb, 32 );
1558 p_stream->f_rate = (double)gnum/gden;
1560 p_stream->fmt.psz_language = malloc(16);
1561 if (p_stream->fmt.psz_language)
1564 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1565 p_stream->fmt.psz_language[15] = 0; /* just in case */
1570 oggpack_read(&opb,8);
1572 p_stream->fmt.psz_description = malloc(16);
1573 if (p_stream->fmt.psz_description)
1576 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1577 p_stream->fmt.psz_description[15] = 0; /* just in case */
1582 oggpack_read(&opb,8);
1586 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1587 logical_stream_t *p_stream,
1588 ogg_packet *p_oggpacket )
1590 if( p_oggpacket->bytes >= 28 &&
1591 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1595 uint16_t major_version;
1596 uint16_t minor_version;
1597 uint64_t timebase_numerator;
1598 uint64_t timebase_denominator;
1600 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1602 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1603 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1604 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1605 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1606 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1607 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1609 else if( p_oggpacket->bytes >= 42 &&
1610 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1612 uint64_t granule_rate_numerator;
1613 uint64_t granule_rate_denominator;
1614 char content_type_string[1024];
1616 /* Read in Annodex header fields */
1618 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1619 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1620 p_stream->secondary_header_packets =
1621 GetDWLE( &p_oggpacket->packet[24] );
1623 /* we are guaranteed that the first header field will be
1624 * the content-type (by the Annodex standard) */
1625 content_type_string[0] = '\0';
1626 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1628 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1629 p_oggpacket->bytes - 1 );
1630 if( p && p[0] == '\r' && p[1] == '\n' )
1631 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1632 content_type_string );
1635 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1636 granule_rate_numerator, granule_rate_denominator,
1637 p_stream->secondary_header_packets, content_type_string );
1639 p_stream->f_rate = (float) granule_rate_numerator /
1640 (float) granule_rate_denominator;
1642 /* What type of file do we have?
1643 * strcmp is safe to use here because we've extracted
1644 * content_type_string from the stream manually */
1645 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1647 /* n.b. WAVs are unsupported right now */
1648 p_stream->fmt.i_cat = UNKNOWN_ES;
1650 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1652 p_stream->fmt.i_cat = AUDIO_ES;
1653 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1655 p_stream->b_force_backup = 1;
1657 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1659 p_stream->fmt.i_cat = AUDIO_ES;
1660 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1662 p_stream->b_force_backup = 1;
1664 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1666 p_stream->fmt.i_cat = VIDEO_ES;
1667 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1669 p_stream->b_force_backup = 1;
1671 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1673 p_stream->fmt.i_cat = VIDEO_ES;
1674 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1676 p_stream->b_force_backup = 1;
1678 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1680 /* n.b. MPEG streams are unsupported right now */
1681 p_stream->fmt.i_cat = VIDEO_ES;
1682 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1684 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1686 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1687 p_stream->fmt.i_cat = SPU_ES;
1688 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );
1693 static uint32_t dirac_uint( bs_t *p_bs )
1695 uint32_t count = 0, value = 0;
1696 while( !bs_read ( p_bs, 1 ) ) {
1699 value |= bs_read ( p_bs, 1 );
1702 return (1<<count) - 1 + value;
1705 static int dirac_bool( bs_t *p_bs )
1707 return bs_read ( p_bs, 1 );
1710 static void Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1711 ogg_packet *p_oggpacket )
1715 p_stream->fmt.i_cat = VIDEO_ES;
1716 p_stream->fmt.i_codec = VLC_FOURCC( 'd','r','a','c' );
1717 p_stream->i_granule_shift = 32;
1719 /* Backing up stream headers is not required -- seqhdrs are repeated
1720 * thoughout the stream at suitable decoding start points */
1721 p_stream->b_force_backup = 0;
1723 /* read in useful bits from sequence header */
1724 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1725 bs_skip( &bs, 13*8); /* parse_info_header */
1726 dirac_uint( &bs ); /* major_version */
1727 dirac_uint( &bs ); /* minor_version */
1728 dirac_uint( &bs ); /* profile */
1729 dirac_uint( &bs ); /* level */
1731 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1733 if (dirac_bool( &bs )) {
1734 dirac_uint( &bs ); /* frame_width */
1735 dirac_uint( &bs ); /* frame_height */
1738 if (dirac_bool( &bs )) {
1739 dirac_uint( &bs ); /* chroma_format */
1741 if (dirac_bool( &bs )) {
1742 if (dirac_bool( &bs )) { /* interlaced */
1743 dirac_bool( &bs ); /* top_field_first */
1747 static const struct {
1748 uint32_t u_n /* numerator */, u_d /* denominator */;
1749 } dirac_frate_tbl[] = { /* table 10.3 */
1750 {1,1}, /* this first value is never used */
1751 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1752 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1755 static const uint32_t dirac_vidfmt_frate[] = { /* table C.1 */
1756 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1759 uint32_t u_n = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_n;
1760 uint32_t u_d = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_d;
1761 if (dirac_bool( &bs )) {
1762 uint32_t frame_rate_index = dirac_uint( &bs );
1763 u_n = dirac_frate_tbl[frame_rate_index].u_n;
1764 u_d = dirac_frate_tbl[frame_rate_index].u_d;
1765 if (frame_rate_index == 0) {
1766 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1767 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1770 p_stream->f_rate = (float) u_n / u_d;