1 /*****************************************************************************
2 * ogg.c : ogg stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Andre Pang <Andre.Pang@csiro.au> (Annodex support)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 #include <vlc_input.h>
40 #include <vlc_codecs.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t * );
47 static void Close( vlc_object_t * );
50 set_shortname ( "OGG" );
51 set_description( N_("OGG demuxer" ) );
52 set_category( CAT_INPUT );
53 set_subcategory( SUBCAT_INPUT_DEMUX );
54 set_capability( "demux", 50 );
55 set_callbacks( Open, Close );
56 add_shortcut( "ogg" );
60 /*****************************************************************************
61 * Definitions of structures and functions used by this plugins
62 *****************************************************************************/
63 typedef struct logical_stream_s
65 ogg_stream_state os; /* logical stream of packets */
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) */
121 typedef struct stream_header_video
125 } stream_header_video;
127 typedef struct stream_header_audio
129 ogg_int16_t channels;
130 ogg_int16_t blockalign;
131 ogg_int32_t avgbytespersec;
132 } stream_header_audio;
134 typedef struct stream_header
139 ogg_int32_t size; /* size of the structure */
141 ogg_int64_t time_unit; /* in reference time */
142 ogg_int64_t samples_per_unit;
143 ogg_int32_t default_len; /* in media time */
145 ogg_int32_t buffersize;
146 ogg_int16_t bits_per_sample;
151 stream_header_video video;
153 stream_header_audio audio;
157 #define OGG_BLOCK_SIZE 4096
159 /* Some defines from OggDS */
160 #define PACKET_TYPE_HEADER 0x01
161 #define PACKET_TYPE_BITS 0x07
162 #define PACKET_LEN_BITS01 0xc0
163 #define PACKET_LEN_BITS2 0x02
164 #define PACKET_IS_SYNCPOINT 0x08
166 /*****************************************************************************
168 *****************************************************************************/
169 static int Demux ( demux_t * );
170 static int Control( demux_t *, int, va_list );
172 /* Bitstream manipulation */
173 static int Ogg_ReadPage ( demux_t *, ogg_page * );
174 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
175 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
177 static int Ogg_BeginningOfStream( demux_t *p_demux );
178 static int Ogg_FindLogicalStreams( demux_t *p_demux );
179 static void Ogg_EndOfStream( demux_t *p_demux );
182 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
183 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
185 /* Logical bitstream headers */
186 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
187 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
188 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
189 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
190 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
191 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
192 static void Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
194 /*****************************************************************************
195 * Open: initializes ogg demux structures
196 *****************************************************************************/
197 static int Open( vlc_object_t * p_this )
199 demux_t *p_demux = (demux_t *)p_this;
201 const uint8_t *p_peek;
204 /* Check if we are dealing with an ogg stream */
205 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
206 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
211 /* Set exported functions */
212 p_demux->pf_demux = Demux;
213 p_demux->pf_control = Control;
214 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
218 memset( p_sys, 0, sizeof( demux_sys_t ) );
219 p_sys->i_bitrate = 0;
220 p_sys->pp_stream = NULL;
221 p_sys->p_old_stream = NULL;
223 /* Begnning of stream, tell the demux to look for elementary streams. */
226 /* Initialize the Ogg physical bitstream parser */
227 ogg_sync_init( &p_sys->oy );
232 /*****************************************************************************
233 * Close: frees unused data
234 *****************************************************************************/
235 static void Close( vlc_object_t *p_this )
237 demux_t *p_demux = (demux_t *)p_this;
238 demux_sys_t *p_sys = p_demux->p_sys ;
240 /* Cleanup the bitstream parser */
241 ogg_sync_clear( &p_sys->oy );
243 Ogg_EndOfStream( p_demux );
245 if( p_sys->p_old_stream )
246 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
251 /*****************************************************************************
252 * Demux: reads and demuxes data packets
253 *****************************************************************************
254 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
255 *****************************************************************************/
256 static int Demux( demux_t * p_demux )
258 demux_sys_t *p_sys = p_demux->p_sys;
260 ogg_packet oggpacket;
264 if( p_sys->i_eos == p_sys->i_streams )
268 msg_Dbg( p_demux, "end of a group of logical streams" );
269 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
270 * only 1 ES is supported (common case for ogg web radio) */
271 if( p_sys->i_streams == 1 )
273 p_sys->p_old_stream = p_sys->pp_stream[0];
274 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
276 Ogg_EndOfStream( p_demux );
280 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
283 msg_Dbg( p_demux, "beginning of a group of logical streams" );
284 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
288 * Demux an ogg page from the stream
290 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
295 /* Test for End of Stream */
296 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
299 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
301 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
303 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
306 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
308 /* Read info from any secondary header packets, if there are any */
309 if( p_stream->secondary_header_packets > 0 )
311 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
312 oggpacket.bytes >= 7 &&
313 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
315 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
316 p_stream->secondary_header_packets = 0;
318 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
319 oggpacket.bytes >= 7 &&
320 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
322 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
323 p_stream->secondary_header_packets = 0;
325 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
327 p_stream->secondary_header_packets = 0;
331 if( p_stream->b_reinit )
333 /* If synchro is re-initialized we need to drop all the packets
334 * until we find a new dated one. */
335 Ogg_UpdatePCR( p_stream, &oggpacket );
337 if( p_stream->i_pcr >= 0 )
339 p_stream->b_reinit = 0;
343 p_stream->i_interpolated_pcr = -1;
347 /* An Ogg/vorbis packet contains an end date granulepos */
348 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
349 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
350 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
352 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
354 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
358 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
365 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
370 i_stream = 0; p_sys->i_pcr = -1;
371 for( ; i_stream < p_sys->i_streams; i_stream++ )
373 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
375 if( p_stream->fmt.i_cat == SPU_ES )
377 if( p_stream->i_interpolated_pcr < 0 )
380 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
381 p_sys->i_pcr = p_stream->i_interpolated_pcr;
384 if( p_sys->i_pcr >= 0 )
386 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
392 /*****************************************************************************
394 *****************************************************************************/
395 static int Control( demux_t *p_demux, int i_query, va_list args )
397 demux_sys_t *p_sys = p_demux->p_sys;
404 case DEMUX_HAS_UNSUPPORTED_META:
405 pb_bool = (bool*)va_arg( args, bool* );
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 );
431 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
436 /****************************************************************************
437 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
438 ****************************************************************************
439 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
440 * are at the end of stream.
441 ****************************************************************************/
442 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
444 demux_sys_t *p_ogg = p_demux->p_sys ;
448 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
450 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
452 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
456 ogg_sync_wrote( &p_ogg->oy, i_read );
462 /****************************************************************************
463 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
465 ****************************************************************************/
466 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
467 ogg_packet *p_oggpacket )
469 /* Convert the granulepos into a pcr */
470 if( p_oggpacket->granulepos >= 0 )
472 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
473 p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
475 ogg_int64_t iframe = p_oggpacket->granulepos >>
476 p_stream->i_granule_shift;
477 ogg_int64_t pframe = p_oggpacket->granulepos -
478 ( iframe << p_stream->i_granule_shift );
480 p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
483 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
485 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
486 /* NB, OggDirac granulepos values are in units of 2*picturerate */
487 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
491 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
495 p_stream->i_interpolated_pcr = p_stream->i_pcr;
499 p_stream->i_pcr = -1;
501 /* no granulepos available, try to interpolate the pcr.
502 * If we can't then don't touch the old value. */
503 if( p_stream->fmt.i_cat == VIDEO_ES )
504 /* 1 frame per packet */
505 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
506 else if( p_stream->fmt.i_bitrate )
507 p_stream->i_interpolated_pcr +=
508 ( p_oggpacket->bytes * INT64_C(1000000) /
509 p_stream->fmt.i_bitrate / 8 );
513 /****************************************************************************
514 * Ogg_DecodePacket: Decode an Ogg packet.
515 ****************************************************************************/
516 static void Ogg_DecodePacket( demux_t *p_demux,
517 logical_stream_t *p_stream,
518 ogg_packet *p_oggpacket )
522 int i_header_len = 0;
523 mtime_t i_pts = -1, i_interpolated_pts;
526 if( !p_oggpacket->bytes )
528 msg_Dbg( p_demux, "discarding 0 sized packet" );
532 if( p_oggpacket->bytes >= 7 &&
533 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
535 /* it's an Annodex packet -- skip it (do nothing) */
538 else if( p_oggpacket->bytes >= 7 &&
539 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
541 /* it's an AnxData packet -- skip it (do nothing) */
545 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
546 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
548 /* Check the ES is selected */
549 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
550 p_stream->p_es, &b_selected );
552 if( p_stream->b_force_backup )
555 bool b_store_size = true;
556 bool b_store_num_headers = false;
558 p_stream->i_packets_backup++;
559 switch( p_stream->fmt.i_codec )
561 case VLC_FOURCC( 'v','o','r','b' ):
562 case VLC_FOURCC( 's','p','x',' ' ):
563 case VLC_FOURCC( 't','h','e','o' ):
564 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
567 case VLC_FOURCC( 'f','l','a','c' ):
568 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
570 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
571 p_stream->b_force_backup = 0;
573 else if( p_stream->fmt.audio.i_rate )
575 p_stream->b_force_backup = 0;
576 if( p_oggpacket->bytes >= 9 )
578 p_oggpacket->packet += 9;
579 p_oggpacket->bytes -= 9;
582 b_store_size = false;
585 case VLC_FOURCC( 'k','a','t','e' ):
586 if( p_stream->i_packets_backup == 1)
587 b_store_num_headers = true;
588 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
592 p_stream->b_force_backup = 0;
596 /* Backup the ogg packet (likely an header packet) */
597 p_stream->p_headers =
598 realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
599 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
600 if( p_stream->p_headers )
602 uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
604 if( b_store_num_headers )
606 /* Kate streams store the number of headers in the first header,
607 so we can't just test for 3 as Vorbis/Theora */
608 *(p_extra++) = p_stream->i_kate_num_headers;
612 *(p_extra++) = p_oggpacket->bytes >> 8;
613 *(p_extra++) = p_oggpacket->bytes & 0xFF;
615 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
616 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
618 if( !p_stream->b_force_backup )
620 /* Last header received, commit changes */
621 free( p_stream->fmt.p_extra );
623 p_stream->fmt.i_extra = p_stream->i_headers;
624 p_stream->fmt.p_extra =
625 realloc( p_stream->fmt.p_extra, p_stream->i_headers );
626 if( p_stream->fmt.p_extra )
627 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
628 p_stream->i_headers );
630 p_stream->fmt.i_extra = 0;
632 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
633 es_out_Control( p_demux->out, ES_OUT_SET_FMT,
634 p_stream->p_es, &p_stream->fmt );
639 p_stream->p_headers = p_sav;
642 b_selected = false; /* Discard the header packet */
645 /* Convert the pcr into a pts */
646 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
647 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
648 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
650 if( p_stream->i_pcr >= 0 )
652 /* This is for streams where the granulepos of the header packets
653 * doesn't match these of the data packets (eg. ogg web radios). */
654 if( p_stream->i_previous_pcr == 0 &&
655 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
657 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
659 /* Call the pace control */
660 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
664 p_stream->i_previous_pcr = p_stream->i_pcr;
666 /* The granulepos is the end date of the sample */
667 i_pts = p_stream->i_pcr;
671 /* Convert the granulepos into the next pcr */
672 i_interpolated_pts = p_stream->i_interpolated_pcr;
673 Ogg_UpdatePCR( p_stream, p_oggpacket );
675 if( p_stream->i_pcr >= 0 )
677 /* This is for streams where the granulepos of the header packets
678 * doesn't match these of the data packets (eg. ogg web radios). */
679 if( p_stream->i_previous_pcr == 0 &&
680 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
682 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
684 /* Call the pace control */
685 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
689 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
690 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
691 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
692 p_stream->i_pcr >= 0 )
694 p_stream->i_previous_pcr = p_stream->i_pcr;
696 /* The granulepos is the start date of the sample */
697 i_pts = p_stream->i_pcr;
702 /* This stream isn't currently selected so we don't need to decode it,
703 * but we did need to store its pcr as it might be selected later on */
707 if( p_oggpacket->bytes <= 0 )
710 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
713 if( i_pts == 0 ) i_pts = 1;
714 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
715 else if( i_pts == -1 ) i_pts = 0;
717 if( p_stream->fmt.i_cat == AUDIO_ES )
718 p_block->i_dts = p_block->i_pts = i_pts;
719 else if( p_stream->fmt.i_cat == SPU_ES )
721 p_block->i_dts = p_block->i_pts = i_pts;
722 p_block->i_length = 0;
724 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
725 p_block->i_dts = p_block->i_pts = i_pts;
726 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
728 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
729 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
731 uint64_t u_pnum = dts + delay;
733 p_block->i_dts = p_stream->i_pcr;
735 /* NB, OggDirac granulepos values are in units of 2*picturerate */
736 if( -1 != p_oggpacket->granulepos )
737 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
741 p_block->i_dts = i_pts;
745 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
746 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
747 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
748 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
749 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
750 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
751 p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
752 p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
754 /* We remove the header from the packet */
755 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
756 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
758 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
760 /* But with subtitles we need to retrieve the duration first */
763 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
765 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
767 lenbytes = lenbytes << 8;
768 lenbytes += *(p_oggpacket->packet + i_header_len - i);
771 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
772 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
773 p_oggpacket->packet[i_header_len + 1] != 0 &&
774 p_oggpacket->packet[i_header_len + 1] != '\n' &&
775 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
777 p_block->i_length = (mtime_t)lenbytes * 1000;
782 if( p_block->i_buffer >= (unsigned int)i_header_len )
783 p_block->i_buffer -= i_header_len;
785 p_block->i_buffer = 0;
788 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
790 /* FIXME: the biggest hack I've ever done */
791 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
792 p_block->i_pts, p_block->i_dts );
796 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
797 p_oggpacket->bytes - i_header_len );
799 es_out_Send( p_demux->out, p_stream->p_es, p_block );
802 /****************************************************************************
803 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
804 * stream and fill p_ogg.
805 *****************************************************************************
806 * The initial page of a logical stream is marked as a 'bos' page.
807 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
808 * together and all of the initial pages must appear before any data pages.
810 * On success this function returns VLC_SUCCESS.
811 ****************************************************************************/
812 static int Ogg_FindLogicalStreams( demux_t *p_demux )
814 demux_sys_t *p_ogg = p_demux->p_sys ;
815 ogg_packet oggpacket;
819 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
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;
834 realloc( p_ogg->pp_stream, p_ogg->i_streams *
835 sizeof(logical_stream_t *) );
836 if( !p_ogg->pp_stream )
838 p_ogg->pp_stream = pp_sav;
843 p_stream = malloc( sizeof(logical_stream_t) );
846 memset( p_stream, 0, sizeof(logical_stream_t) );
847 p_stream->p_headers = 0;
848 p_stream->secondary_header_packets = 0;
850 es_format_Init( &p_stream->fmt, 0, 0 );
851 es_format_Init( &p_stream->fmt_old, 0, 0 );
853 /* Setup the logical stream */
854 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
855 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
857 /* Extract the initial header from the first page and verify
858 * the codec type of tis Ogg bitstream */
859 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
861 /* error. stream version mismatch perhaps */
862 msg_Err( p_demux, "error reading first page of "
863 "Ogg bitstream data" );
867 /* FIXME: check return value */
868 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
870 /* Check for Vorbis header */
871 if( oggpacket.bytes >= 7 &&
872 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
874 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
875 msg_Dbg( p_demux, "found vorbis header" );
877 /* Check for Speex header */
878 else if( oggpacket.bytes >= 5 &&
879 ! memcmp( oggpacket.packet, "Speex", 5 ) )
881 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
882 msg_Dbg( p_demux, "found speex header, channels: %i, "
883 "rate: %i, bitrate: %i",
884 p_stream->fmt.audio.i_channels,
885 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
887 /* Check for Flac header (< version 1.1.1) */
888 else if( oggpacket.bytes >= 4 &&
889 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
891 msg_Dbg( p_demux, "found FLAC header" );
893 /* Grrrr!!!! Did they really have to put all the
894 * important info in the second header packet!!!
895 * (STREAMINFO metadata is in the following packet) */
896 p_stream->b_force_backup = 1;
898 p_stream->fmt.i_cat = AUDIO_ES;
899 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
901 /* Check for Flac header (>= version 1.1.1) */
902 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
903 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
904 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
906 int i_packets = ((int)oggpacket.packet[7]) << 8 |
908 msg_Dbg( p_demux, "found FLAC header version %i.%i "
909 "(%i header packets)",
910 oggpacket.packet[5], oggpacket.packet[6],
913 p_stream->b_force_backup = 1;
915 p_stream->fmt.i_cat = AUDIO_ES;
916 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
917 oggpacket.packet += 13; oggpacket.bytes -= 13;
918 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
920 /* Check for Theora header */
921 else if( oggpacket.bytes >= 7 &&
922 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
924 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
927 "found theora header, bitrate: %i, rate: %f",
928 p_stream->fmt.i_bitrate, p_stream->f_rate );
930 /* Check for Dirac header */
931 else if( oggpacket.bytes >= 5 &&
932 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
934 Ogg_ReadDiracHeader( p_stream, &oggpacket );
935 msg_Dbg( p_demux, "found dirac header" );
937 /* Check for Tarkin header */
938 else if( oggpacket.bytes >= 7 &&
939 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
943 msg_Dbg( p_demux, "found tarkin header" );
944 p_stream->fmt.i_cat = VIDEO_ES;
945 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
947 /* Cheat and get additionnal info ;) */
948 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
949 oggpack_adv( &opb, 88 );
950 oggpack_adv( &opb, 104 );
951 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
952 p_stream->f_rate = 2; /* FIXME */
954 "found tarkin header, bitrate: %i, rate: %f",
955 p_stream->fmt.i_bitrate, p_stream->f_rate );
957 /* Check for Annodex header */
958 else if( oggpacket.bytes >= 7 &&
959 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
961 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
963 /* kill annodex track */
967 /* Check for Annodex header */
968 else if( oggpacket.bytes >= 7 &&
969 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
971 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
974 /* Check for Kate header */
975 else if( oggpacket.bytes >= 8 &&
976 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
978 Ogg_ReadKateHeader( p_stream, &oggpacket );
979 msg_Dbg( p_demux, "found kate header" );
981 else if( oggpacket.bytes >= 142 &&
982 !memcmp( &oggpacket.packet[1],
983 "Direct Show Samples embedded in Ogg", 35 ))
985 /* Old header type */
987 /* Check for video header (old format) */
988 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
989 oggpacket.bytes >= 184 )
991 p_stream->fmt.i_cat = VIDEO_ES;
992 p_stream->fmt.i_codec =
993 VLC_FOURCC( oggpacket.packet[68],
994 oggpacket.packet[69],
995 oggpacket.packet[70],
996 oggpacket.packet[71] );
997 msg_Dbg( p_demux, "found video header of type: %.4s",
998 (char *)&p_stream->fmt.i_codec );
1000 p_stream->fmt.video.i_frame_rate = 10000000;
1001 p_stream->fmt.video.i_frame_rate_base =
1002 GetQWLE((oggpacket.packet+164));
1003 p_stream->f_rate = 10000000.0 /
1004 GetQWLE((oggpacket.packet+164));
1005 p_stream->fmt.video.i_bits_per_pixel =
1006 GetWLE((oggpacket.packet+182));
1007 if( !p_stream->fmt.video.i_bits_per_pixel )
1009 p_stream->fmt.video.i_bits_per_pixel = 24;
1010 p_stream->fmt.video.i_width =
1011 GetDWLE((oggpacket.packet+176));
1012 p_stream->fmt.video.i_height =
1013 GetDWLE((oggpacket.packet+180));
1016 "fps: %f, width:%i; height:%i, bitcount:%i",
1018 p_stream->fmt.video.i_width,
1019 p_stream->fmt.video.i_height,
1020 p_stream->fmt.video.i_bits_per_pixel);
1023 /* Check for audio header (old format) */
1024 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1026 unsigned int i_extra_size;
1027 unsigned int i_format_tag;
1029 p_stream->fmt.i_cat = AUDIO_ES;
1031 i_extra_size = GetWLE((oggpacket.packet+140));
1032 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1034 p_stream->fmt.i_extra = i_extra_size;
1035 p_stream->fmt.p_extra = malloc( i_extra_size );
1036 if( p_stream->fmt.p_extra )
1037 memcpy( p_stream->fmt.p_extra,
1038 oggpacket.packet + 142, i_extra_size );
1040 p_stream->fmt.i_extra = 0;
1043 i_format_tag = GetWLE((oggpacket.packet+124));
1044 p_stream->fmt.audio.i_channels =
1045 GetWLE((oggpacket.packet+126));
1046 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1047 GetDWLE((oggpacket.packet+128));
1048 p_stream->fmt.i_bitrate =
1049 GetDWLE((oggpacket.packet+132)) * 8;
1050 p_stream->fmt.audio.i_blockalign =
1051 GetWLE((oggpacket.packet+136));
1052 p_stream->fmt.audio.i_bitspersample =
1053 GetWLE((oggpacket.packet+138));
1055 wf_tag_to_fourcc( i_format_tag,
1056 &p_stream->fmt.i_codec, 0 );
1058 if( p_stream->fmt.i_codec ==
1059 VLC_FOURCC('u','n','d','f') )
1061 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1062 ( i_format_tag >> 8 ) & 0xff,
1063 i_format_tag & 0xff );
1066 msg_Dbg( p_demux, "found audio header of type: %.4s",
1067 (char *)&p_stream->fmt.i_codec );
1068 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1069 "%dbits/sample %dkb/s",
1071 p_stream->fmt.audio.i_channels,
1072 p_stream->fmt.audio.i_rate,
1073 p_stream->fmt.audio.i_bitspersample,
1074 p_stream->fmt.i_bitrate / 1024 );
1079 msg_Dbg( p_demux, "stream %d has an old header "
1080 "but is of an unknown type", p_ogg->i_streams-1 );
1085 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
1086 == PACKET_TYPE_HEADER &&
1087 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
1089 stream_header *st = (stream_header *)(oggpacket.packet+1);
1091 /* Check for video header (new format) */
1092 if( !strncmp( st->streamtype, "video", 5 ) )
1094 p_stream->fmt.i_cat = VIDEO_ES;
1096 /* We need to get rid of the header packet */
1097 ogg_stream_packetout( &p_stream->os, &oggpacket );
1099 p_stream->fmt.i_codec =
1100 VLC_FOURCC( st->subtype[0], st->subtype[1],
1101 st->subtype[2], st->subtype[3] );
1102 msg_Dbg( p_demux, "found video header of type: %.4s",
1103 (char *)&p_stream->fmt.i_codec );
1105 p_stream->fmt.video.i_frame_rate = 10000000;
1106 p_stream->fmt.video.i_frame_rate_base =
1107 GetQWLE(&st->time_unit);
1108 p_stream->f_rate = 10000000.0 /
1109 GetQWLE(&st->time_unit);
1110 p_stream->fmt.video.i_bits_per_pixel =
1111 GetWLE(&st->bits_per_sample);
1112 p_stream->fmt.video.i_width =
1113 GetDWLE(&st->sh.video.width);
1114 p_stream->fmt.video.i_height =
1115 GetDWLE(&st->sh.video.height);
1118 "fps: %f, width:%i; height:%i, bitcount:%i",
1120 p_stream->fmt.video.i_width,
1121 p_stream->fmt.video.i_height,
1122 p_stream->fmt.video.i_bits_per_pixel );
1124 /* Check for audio header (new format) */
1125 else if( !strncmp( st->streamtype, "audio", 5 ) )
1130 p_stream->fmt.i_cat = AUDIO_ES;
1132 /* We need to get rid of the header packet */
1133 ogg_stream_packetout( &p_stream->os, &oggpacket );
1135 p_stream->fmt.i_extra = GetQWLE(&st->size) - sizeof(stream_header);
1136 if( p_stream->fmt.i_extra > 0 &&
1137 p_stream->fmt.i_extra < oggpacket.bytes - 1 - sizeof(stream_header) )
1139 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1140 if( p_stream->fmt.p_extra )
1141 memcpy( p_stream->fmt.p_extra, st + 1,
1142 p_stream->fmt.i_extra );
1144 p_stream->fmt.i_extra = 0;
1147 memcpy( p_buffer, st->subtype, 4 );
1149 i_format_tag = strtol(p_buffer,NULL,16);
1150 p_stream->fmt.audio.i_channels =
1151 GetWLE(&st->sh.audio.channels);
1152 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1153 GetQWLE(&st->samples_per_unit);
1154 p_stream->fmt.i_bitrate =
1155 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
1156 p_stream->fmt.audio.i_blockalign =
1157 GetWLE(&st->sh.audio.blockalign);
1158 p_stream->fmt.audio.i_bitspersample =
1159 GetWLE(&st->bits_per_sample);
1161 wf_tag_to_fourcc( i_format_tag,
1162 &p_stream->fmt.i_codec, 0 );
1164 if( p_stream->fmt.i_codec ==
1165 VLC_FOURCC('u','n','d','f') )
1167 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1168 ( i_format_tag >> 8 ) & 0xff,
1169 i_format_tag & 0xff );
1172 msg_Dbg( p_demux, "found audio header of type: %.4s",
1173 (char *)&p_stream->fmt.i_codec );
1174 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1175 "%dbits/sample %dkb/s",
1177 p_stream->fmt.audio.i_channels,
1178 p_stream->fmt.audio.i_rate,
1179 p_stream->fmt.audio.i_bitspersample,
1180 p_stream->fmt.i_bitrate / 1024 );
1182 /* Check for text (subtitles) header */
1183 else if( !strncmp(st->streamtype, "text", 4) )
1185 /* We need to get rid of the header packet */
1186 ogg_stream_packetout( &p_stream->os, &oggpacket );
1188 msg_Dbg( p_demux, "found text subtitles header" );
1189 p_stream->fmt.i_cat = SPU_ES;
1190 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1191 p_stream->f_rate = 1000; /* granulepos is in milisec */
1195 msg_Dbg( p_demux, "stream %d has a header marker "
1196 "but is of an unknown type", p_ogg->i_streams-1 );
1201 else if( oggpacket.bytes >= 7 &&
1202 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1206 msg_Dbg( p_demux, "stream %d is a skeleton",
1207 p_ogg->i_streams-1 );
1208 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1212 msg_Dbg( p_demux, "stream %d is of unknown type",
1213 p_ogg->i_streams-1 );
1218 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1219 return VLC_EGENERIC;
1222 /* This is the first data page, which means we are now finished
1223 * with the initial pages. We just need to store it in the relevant
1225 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1227 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1239 return VLC_EGENERIC;
1242 /****************************************************************************
1243 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1244 * Elementary streams.
1245 ****************************************************************************/
1246 static int Ogg_BeginningOfStream( demux_t *p_demux )
1248 demux_sys_t *p_ogg = p_demux->p_sys ;
1249 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1252 /* Find the logical streams embedded in the physical stream and
1253 * initialize our p_ogg structure. */
1254 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1256 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1257 return VLC_EGENERIC;
1260 p_ogg->i_bitrate = 0;
1262 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1264 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1266 p_stream->p_es = NULL;
1268 /* Try first to reuse an old ES */
1270 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1271 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1273 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1275 p_stream->p_es = p_old_stream->p_es;
1276 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1278 p_old_stream->p_es = NULL;
1279 p_old_stream = NULL;
1282 if( !p_stream->p_es )
1283 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1285 // TODO: something to do here ?
1286 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1288 /* Set the CMML stream active */
1289 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1292 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1294 p_stream->i_pcr = p_stream->i_previous_pcr =
1295 p_stream->i_interpolated_pcr = -1;
1296 p_stream->b_reinit = 0;
1299 if( p_ogg->p_old_stream )
1301 if( p_ogg->p_old_stream->p_es )
1302 msg_Dbg( p_demux, "old stream not reused" );
1303 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1304 p_ogg->p_old_stream = NULL;
1309 /****************************************************************************
1310 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1311 ****************************************************************************/
1312 static void Ogg_EndOfStream( demux_t *p_demux )
1314 demux_sys_t *p_ogg = p_demux->p_sys ;
1317 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1318 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1319 free( p_ogg->pp_stream );
1322 p_ogg->i_bitrate = 0;
1323 p_ogg->i_streams = 0;
1324 p_ogg->pp_stream = NULL;
1328 * This function delete and release all data associated to a logical_stream_t
1330 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1332 if( p_stream->p_es )
1333 es_out_Del( p_demux->out, p_stream->p_es );
1335 ogg_stream_clear( &p_stream->os );
1336 free( p_stream->p_headers );
1338 es_format_Clean( &p_stream->fmt_old );
1339 es_format_Clean( &p_stream->fmt );
1344 * This function check if a we need to reset a decoder in case we are
1347 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1353 for( i = 0; i < 3; i++ )
1355 const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1356 const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1358 if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1361 const int i_new_size = GetWBE( &p_new_extra[0] );
1362 const int i_old_size = GetWBE( &p_old_extra[0] );
1364 if( i != 1 ) /* Ignore vorbis comment */
1366 if( i_new_size != i_old_size )
1368 if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1372 i_new += 2 + i_new_size;
1373 i_old += 2 + i_old_size;
1377 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1380 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1383 /* Only vorbis is supported */
1384 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) )
1385 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1388 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1390 return !b_compatible;
1393 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1394 ogg_packet *p_oggpacket )
1397 int i_fps_numerator;
1398 int i_fps_denominator;
1399 int i_keyframe_frequency_force;
1401 p_stream->fmt.i_cat = VIDEO_ES;
1402 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1404 /* Signal that we want to keep a backup of the theora
1405 * stream headers. They will be used when switching between
1407 p_stream->b_force_backup = 1;
1409 /* Cheat and get additionnal info ;) */
1410 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1411 bs_skip( &bitstream, 56 );
1412 bs_read( &bitstream, 8 ); /* major version num */
1413 bs_read( &bitstream, 8 ); /* minor version num */
1414 bs_read( &bitstream, 8 ); /* subminor version num */
1415 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1416 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1417 bs_read( &bitstream, 24 ); /* frame width */
1418 bs_read( &bitstream, 24 ); /* frame height */
1419 bs_read( &bitstream, 8 ); /* x offset */
1420 bs_read( &bitstream, 8 ); /* y offset */
1422 i_fps_numerator = bs_read( &bitstream, 32 );
1423 i_fps_denominator = bs_read( &bitstream, 32 );
1424 bs_read( &bitstream, 24 ); /* aspect_numerator */
1425 bs_read( &bitstream, 24 ); /* aspect_denominator */
1427 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1428 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1430 bs_read( &bitstream, 8 ); /* colorspace */
1431 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1432 bs_read( &bitstream, 6 ); /* quality */
1434 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1436 /* granule_shift = i_log( frequency_force -1 ) */
1437 p_stream->i_granule_shift = 0;
1438 i_keyframe_frequency_force--;
1439 while( i_keyframe_frequency_force )
1441 p_stream->i_granule_shift++;
1442 i_keyframe_frequency_force >>= 1;
1445 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1448 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1449 ogg_packet *p_oggpacket )
1453 p_stream->fmt.i_cat = AUDIO_ES;
1454 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1456 /* Signal that we want to keep a backup of the vorbis
1457 * stream headers. They will be used when switching between
1459 p_stream->b_force_backup = 1;
1461 /* Cheat and get additionnal info ;) */
1462 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1463 oggpack_adv( &opb, 88 );
1464 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1465 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1466 oggpack_read( &opb, 32 );
1467 oggpack_adv( &opb, 32 );
1468 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1471 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1472 ogg_packet *p_oggpacket )
1476 p_stream->fmt.i_cat = AUDIO_ES;
1477 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1479 /* Signal that we want to keep a backup of the speex
1480 * stream headers. They will be used when switching between
1482 p_stream->b_force_backup = 1;
1484 /* Cheat and get additionnal info ;) */
1485 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1486 oggpack_adv( &opb, 224 );
1487 oggpack_adv( &opb, 32 ); /* speex_version_id */
1488 oggpack_adv( &opb, 32 ); /* header_size */
1489 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1490 oggpack_adv( &opb, 32 ); /* mode */
1491 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1492 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1493 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1496 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1497 ogg_packet *p_oggpacket )
1499 /* Parse the STREAMINFO metadata */
1502 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1505 if( bs_read( &s, 7 ) == 0 )
1507 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1510 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1511 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1513 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1514 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1516 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1518 /* Fake this as the last metadata block */
1519 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1523 /* This ain't a STREAMINFO metadata */
1524 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1528 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1529 ogg_packet *p_oggpacket )
1536 p_stream->fmt.i_cat = SPU_ES;
1537 p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1539 /* Signal that we want to keep a backup of the kate
1540 * stream headers. They will be used when switching between
1542 p_stream->b_force_backup = 1;
1544 /* Cheat and get additionnal info ;) */
1545 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1546 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1547 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1548 oggpack_adv( &opb, 3*8 );
1549 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1550 oggpack_adv( &opb, 8*8 ); /* reserved */
1551 gnum = oggpack_read( &opb, 32 );
1552 gden = oggpack_read( &opb, 32 );
1553 p_stream->f_rate = (double)gnum/gden;
1555 p_stream->fmt.psz_language = malloc(16);
1556 if (p_stream->fmt.psz_language)
1559 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1560 p_stream->fmt.psz_language[15] = 0; /* just in case */
1565 oggpack_read(&opb,8);
1567 p_stream->fmt.psz_description = malloc(16);
1568 if (p_stream->fmt.psz_description)
1571 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1572 p_stream->fmt.psz_description[15] = 0; /* just in case */
1577 oggpack_read(&opb,8);
1581 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1582 logical_stream_t *p_stream,
1583 ogg_packet *p_oggpacket )
1585 if( p_oggpacket->bytes >= 28 &&
1586 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1590 uint16_t major_version;
1591 uint16_t minor_version;
1592 uint64_t timebase_numerator;
1593 uint64_t timebase_denominator;
1595 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1597 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1598 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1599 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1600 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1601 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1602 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1604 else if( p_oggpacket->bytes >= 42 &&
1605 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1607 uint64_t granule_rate_numerator;
1608 uint64_t granule_rate_denominator;
1609 char content_type_string[1024];
1611 /* Read in Annodex header fields */
1613 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1614 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1615 p_stream->secondary_header_packets =
1616 GetDWLE( &p_oggpacket->packet[24] );
1618 /* we are guaranteed that the first header field will be
1619 * the content-type (by the Annodex standard) */
1620 content_type_string[0] = '\0';
1621 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1623 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1624 p_oggpacket->bytes - 1 );
1625 if( p && p[0] == '\r' && p[1] == '\n' )
1626 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1627 content_type_string );
1630 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1631 granule_rate_numerator, granule_rate_denominator,
1632 p_stream->secondary_header_packets, content_type_string );
1634 p_stream->f_rate = (float) granule_rate_numerator /
1635 (float) granule_rate_denominator;
1637 /* What type of file do we have?
1638 * strcmp is safe to use here because we've extracted
1639 * content_type_string from the stream manually */
1640 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1642 /* n.b. WAVs are unsupported right now */
1643 p_stream->fmt.i_cat = UNKNOWN_ES;
1645 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1647 p_stream->fmt.i_cat = AUDIO_ES;
1648 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1650 p_stream->b_force_backup = 1;
1652 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1654 p_stream->fmt.i_cat = AUDIO_ES;
1655 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1657 p_stream->b_force_backup = 1;
1659 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1661 p_stream->fmt.i_cat = VIDEO_ES;
1662 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1664 p_stream->b_force_backup = 1;
1666 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1668 p_stream->fmt.i_cat = VIDEO_ES;
1669 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1671 p_stream->b_force_backup = 1;
1673 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1675 /* n.b. MPEG streams are unsupported right now */
1676 p_stream->fmt.i_cat = VIDEO_ES;
1677 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1679 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1681 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1682 p_stream->fmt.i_cat = SPU_ES;
1683 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );
1688 static uint32_t dirac_uint( bs_t *p_bs )
1690 uint32_t count = 0, value = 0;
1691 while( !bs_read ( p_bs, 1 ) ) {
1694 value |= bs_read ( p_bs, 1 );
1697 return (1<<count) - 1 + value;
1700 static int dirac_bool( bs_t *p_bs )
1702 return bs_read ( p_bs, 1 );
1705 static void Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1706 ogg_packet *p_oggpacket )
1710 p_stream->fmt.i_cat = VIDEO_ES;
1711 p_stream->fmt.i_codec = VLC_FOURCC( 'd','r','a','c' );
1712 p_stream->i_granule_shift = 32;
1714 /* Backing up stream headers is not required -- seqhdrs are repeated
1715 * thoughout the stream at suitable decoding start points */
1716 p_stream->b_force_backup = 0;
1718 /* read in useful bits from sequence header */
1719 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1720 bs_skip( &bs, 13*8); /* parse_info_header */
1721 dirac_uint( &bs ); /* major_version */
1722 dirac_uint( &bs ); /* minor_version */
1723 dirac_uint( &bs ); /* profile */
1724 dirac_uint( &bs ); /* level */
1726 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1728 if (dirac_bool( &bs )) {
1729 dirac_uint( &bs ); /* frame_width */
1730 dirac_uint( &bs ); /* frame_height */
1733 if (dirac_bool( &bs )) {
1734 dirac_uint( &bs ); /* chroma_format */
1736 if (dirac_bool( &bs )) {
1737 if (dirac_bool( &bs )) { /* interlaced */
1738 dirac_bool( &bs ); /* top_field_first */
1742 static const struct {
1743 uint32_t u_n /* numerator */, u_d /* denominator */;
1744 } dirac_frate_tbl[] = { /* table 10.3 */
1745 {1,1}, /* this first value is never used */
1746 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1747 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1750 static const uint32_t dirac_vidfmt_frate[] = { /* table C.1 */
1751 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1754 uint32_t u_n = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_n;
1755 uint32_t u_d = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_d;
1756 if (dirac_bool( &bs )) {
1757 uint32_t frame_rate_index = dirac_uint( &bs );
1758 u_n = dirac_frate_tbl[frame_rate_index].u_n;
1759 u_d = dirac_frate_tbl[frame_rate_index].u_d;
1760 if (frame_rate_index == 0) {
1761 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1762 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1765 p_stream->f_rate = (float) u_n / u_d;