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 * );
193 static uint32_t Ogg_ReadDiracPictureNumber( ogg_packet *p_oggpacket );
195 /*****************************************************************************
196 * Open: initializes ogg demux structures
197 *****************************************************************************/
198 static int Open( vlc_object_t * p_this )
200 demux_t *p_demux = (demux_t *)p_this;
202 const uint8_t *p_peek;
205 /* Check if we are dealing with an ogg stream */
206 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
207 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
212 /* Set exported functions */
213 p_demux->pf_demux = Demux;
214 p_demux->pf_control = Control;
215 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
217 memset( p_sys, 0, sizeof( demux_sys_t ) );
218 p_sys->i_bitrate = 0;
219 p_sys->pp_stream = NULL;
220 p_sys->p_old_stream = NULL;
222 /* Begnning of stream, tell the demux to look for elementary streams. */
225 /* Initialize the Ogg physical bitstream parser */
226 ogg_sync_init( &p_sys->oy );
231 /*****************************************************************************
232 * Close: frees unused data
233 *****************************************************************************/
234 static void Close( vlc_object_t *p_this )
236 demux_t *p_demux = (demux_t *)p_this;
237 demux_sys_t *p_sys = p_demux->p_sys ;
239 /* Cleanup the bitstream parser */
240 ogg_sync_clear( &p_sys->oy );
242 Ogg_EndOfStream( p_demux );
244 if( p_sys->p_old_stream )
245 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
250 /*****************************************************************************
251 * Demux: reads and demuxes data packets
252 *****************************************************************************
253 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
254 *****************************************************************************/
255 static int Demux( demux_t * p_demux )
257 demux_sys_t *p_sys = p_demux->p_sys;
259 ogg_packet oggpacket;
263 if( p_sys->i_eos == p_sys->i_streams )
267 msg_Dbg( p_demux, "end of a group of logical streams" );
268 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
269 * only 1 ES is supported (common case for ogg web radio) */
270 if( p_sys->i_streams == 1 )
272 p_sys->p_old_stream = p_sys->pp_stream[0];
273 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
275 Ogg_EndOfStream( p_demux );
279 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
282 msg_Dbg( p_demux, "beginning of a group of logical streams" );
283 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
287 * Demux an ogg page from the stream
289 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
294 /* Test for End of Stream */
295 if( ogg_page_eos( &oggpage ) ) p_sys->i_eos++;
298 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
300 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
302 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
305 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
307 /* Read info from any secondary header packets, if there are any */
308 if( p_stream->secondary_header_packets > 0 )
310 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
311 oggpacket.bytes >= 7 &&
312 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
314 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
315 p_stream->secondary_header_packets = 0;
317 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
318 oggpacket.bytes >= 7 &&
319 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
321 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
322 p_stream->secondary_header_packets = 0;
324 else if ( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
326 p_stream->secondary_header_packets = 0;
330 if( p_stream->b_reinit )
332 /* If synchro is re-initialized we need to drop all the packets
333 * until we find a new dated one. */
334 Ogg_UpdatePCR( p_stream, &oggpacket );
336 if( p_stream->i_pcr >= 0 )
338 p_stream->b_reinit = 0;
342 p_stream->i_interpolated_pcr = -1;
346 /* An Ogg/vorbis packet contains an end date granulepos */
347 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
348 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
349 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
351 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
353 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
357 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
364 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
369 i_stream = 0; p_sys->i_pcr = -1;
370 for( ; i_stream < p_sys->i_streams; i_stream++ )
372 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
374 if( p_stream->fmt.i_cat == SPU_ES )
376 if( p_stream->i_interpolated_pcr < 0 )
379 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
380 p_sys->i_pcr = p_stream->i_interpolated_pcr;
383 if( p_sys->i_pcr >= 0 )
385 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
391 /*****************************************************************************
393 *****************************************************************************/
394 static int Control( demux_t *p_demux, int i_query, va_list args )
396 demux_sys_t *p_sys = p_demux->p_sys;
403 case DEMUX_HAS_UNSUPPORTED_META:
404 pb_bool = (bool*)va_arg( args, bool* );
409 pi64 = (int64_t*)va_arg( args, int64_t * );
410 *pi64 = p_sys->i_pcr;
416 case DEMUX_SET_POSITION:
417 for( i = 0; i < p_sys->i_streams; i++ )
419 logical_stream_t *p_stream = p_sys->pp_stream[i];
421 /* we'll trash all the data until we find the next pcr */
422 p_stream->b_reinit = 1;
423 p_stream->i_pcr = -1;
424 p_stream->i_interpolated_pcr = -1;
425 ogg_stream_reset( &p_stream->os );
427 ogg_sync_reset( &p_sys->oy );
430 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
435 /****************************************************************************
436 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
437 ****************************************************************************
438 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
439 * are at the end of stream.
440 ****************************************************************************/
441 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
443 demux_sys_t *p_ogg = p_demux->p_sys ;
447 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
449 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
451 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
455 ogg_sync_wrote( &p_ogg->oy, i_read );
461 /****************************************************************************
462 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
464 ****************************************************************************/
465 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
466 ogg_packet *p_oggpacket )
468 /* Convert the granulepos into a pcr */
469 if( p_oggpacket->granulepos >= 0 )
471 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
472 p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) ||
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)
485 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
489 p_stream->i_interpolated_pcr = p_stream->i_pcr;
493 p_stream->i_pcr = -1;
495 /* no granulepos available, try to interpolate the pcr.
496 * If we can't then don't touch the old value. */
497 if( p_stream->fmt.i_cat == VIDEO_ES )
498 /* 1 frame per packet */
499 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
500 else if( p_stream->fmt.i_bitrate )
501 p_stream->i_interpolated_pcr +=
502 ( p_oggpacket->bytes * INT64_C(1000000) /
503 p_stream->fmt.i_bitrate / 8 );
507 /****************************************************************************
508 * Ogg_DecodePacket: Decode an Ogg packet.
509 ****************************************************************************/
510 static void Ogg_DecodePacket( demux_t *p_demux,
511 logical_stream_t *p_stream,
512 ogg_packet *p_oggpacket )
516 int i_header_len = 0;
517 mtime_t i_pts = -1, i_interpolated_pts;
520 if( !p_oggpacket->bytes )
522 msg_Dbg( p_demux, "discarding 0 sized packet" );
526 if( p_oggpacket->bytes >= 7 &&
527 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
529 /* it's an Annodex packet -- skip it (do nothing) */
532 else if( p_oggpacket->bytes >= 7 &&
533 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
535 /* it's an AnxData packet -- skip it (do nothing) */
539 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
540 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
542 /* Check the ES is selected */
543 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
544 p_stream->p_es, &b_selected );
546 if( p_stream->b_force_backup )
549 bool b_store_size = true;
550 bool b_store_num_headers = false;
552 p_stream->i_packets_backup++;
553 switch( p_stream->fmt.i_codec )
555 case VLC_FOURCC( 'v','o','r','b' ):
556 case VLC_FOURCC( 's','p','x',' ' ):
557 case VLC_FOURCC( 't','h','e','o' ):
558 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
561 case VLC_FOURCC( 'f','l','a','c' ):
562 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
564 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
565 p_stream->b_force_backup = 0;
567 else if( p_stream->fmt.audio.i_rate )
569 p_stream->b_force_backup = 0;
570 if( p_oggpacket->bytes >= 9 )
572 p_oggpacket->packet += 9;
573 p_oggpacket->bytes -= 9;
576 b_store_size = false;
579 case VLC_FOURCC( 'k','a','t','e' ):
580 if( p_stream->i_packets_backup == 1)
581 b_store_num_headers = true;
582 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
586 p_stream->b_force_backup = 0;
590 /* Backup the ogg packet (likely an header packet) */
591 p_stream->p_headers =
592 realloc( p_stream->p_headers, p_stream->i_headers +
593 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
594 p_extra = p_stream->p_headers + p_stream->i_headers;
595 if( b_store_num_headers )
597 /* Kate streams store the number of headers in the first header,
598 so we can't just test for 3 as Vorbis/Theora */
599 *(p_extra++) = p_stream->i_kate_num_headers;
603 *(p_extra++) = p_oggpacket->bytes >> 8;
604 *(p_extra++) = p_oggpacket->bytes & 0xFF;
606 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
607 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
609 if( !p_stream->b_force_backup )
611 /* Last header received, commit changes */
612 p_stream->fmt.i_extra = p_stream->i_headers;
613 p_stream->fmt.p_extra =
614 realloc( p_stream->fmt.p_extra, p_stream->i_headers );
615 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
616 p_stream->i_headers );
618 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
619 es_out_Control( p_demux->out, ES_OUT_SET_FMT,
620 p_stream->p_es, &p_stream->fmt );
623 b_selected = false; /* Discard the header packet */
626 /* Convert the pcr into a pts */
627 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
628 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
629 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
631 if( p_stream->i_pcr >= 0 )
633 /* This is for streams where the granulepos of the header packets
634 * doesn't match these of the data packets (eg. ogg web radios). */
635 if( p_stream->i_previous_pcr == 0 &&
636 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
638 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
640 /* Call the pace control */
641 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
645 p_stream->i_previous_pcr = p_stream->i_pcr;
647 /* The granulepos is the end date of the sample */
648 i_pts = p_stream->i_pcr;
652 /* Convert the granulepos into the next pcr */
653 i_interpolated_pts = p_stream->i_interpolated_pcr;
654 Ogg_UpdatePCR( p_stream, p_oggpacket );
656 if( p_stream->i_pcr >= 0 )
658 /* This is for streams where the granulepos of the header packets
659 * doesn't match these of the data packets (eg. ogg web radios). */
660 if( p_stream->i_previous_pcr == 0 &&
661 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
663 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
665 /* Call the pace control */
666 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
670 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
671 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
672 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
673 p_stream->i_pcr >= 0 )
675 p_stream->i_previous_pcr = p_stream->i_pcr;
677 /* The granulepos is the start date of the sample */
678 i_pts = p_stream->i_pcr;
683 /* This stream isn't currently selected so we don't need to decode it,
684 * but we did need to store its pcr as it might be selected later on */
688 if( p_oggpacket->bytes <= 0 )
691 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
694 if( i_pts == 0 ) i_pts = 1;
695 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
696 else if( i_pts == -1 ) i_pts = 0;
698 if( p_stream->fmt.i_cat == AUDIO_ES )
699 p_block->i_dts = p_block->i_pts = i_pts;
700 else if( p_stream->fmt.i_cat == SPU_ES )
702 p_block->i_dts = p_block->i_pts = i_pts;
703 p_block->i_length = 0;
705 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
706 p_block->i_dts = p_block->i_pts = i_pts;
707 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
709 /* every packet[1] in the stream contains a picture, there may
710 * be header cruft infront of it though
711 * [1] EXCEPT the BOS page/packet */
712 uint32_t u_pnum = Ogg_ReadDiracPictureNumber( p_oggpacket );
714 if ( u_pnum != 0xffffffff ) {
716 p_block->i_pts = (u_pnum * INT64_C(1000000) / p_stream->f_rate);
721 p_block->i_dts = i_pts;
725 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
726 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
727 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
728 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
729 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
730 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
731 p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
732 p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
734 /* We remove the header from the packet */
735 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
736 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
738 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
740 /* But with subtitles we need to retrieve the duration first */
743 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
745 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
747 lenbytes = lenbytes << 8;
748 lenbytes += *(p_oggpacket->packet + i_header_len - i);
751 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
752 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
753 p_oggpacket->packet[i_header_len + 1] != 0 &&
754 p_oggpacket->packet[i_header_len + 1] != '\n' &&
755 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
757 p_block->i_length = (mtime_t)lenbytes * 1000;
762 if( p_block->i_buffer >= (unsigned int)i_header_len )
763 p_block->i_buffer -= i_header_len;
765 p_block->i_buffer = 0;
768 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
770 /* FIXME: the biggest hack I've ever done */
771 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
772 p_block->i_pts, p_block->i_dts );
776 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
777 p_oggpacket->bytes - i_header_len );
779 es_out_Send( p_demux->out, p_stream->p_es, p_block );
782 /****************************************************************************
783 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
784 * stream and fill p_ogg.
785 *****************************************************************************
786 * The initial page of a logical stream is marked as a 'bos' page.
787 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
788 * together and all of the initial pages must appear before any data pages.
790 * On success this function returns VLC_SUCCESS.
791 ****************************************************************************/
792 static int Ogg_FindLogicalStreams( demux_t *p_demux )
794 demux_sys_t *p_ogg = p_demux->p_sys ;
795 ogg_packet oggpacket;
799 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
801 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
803 if( ogg_page_bos( &oggpage ) )
806 /* All is wonderful in our fine fine little world.
807 * We found the beginning of our first logical stream. */
808 while( ogg_page_bos( &oggpage ) )
812 realloc( p_ogg->pp_stream, p_ogg->i_streams *
813 sizeof(logical_stream_t *) );
815 p_stream = malloc( sizeof(logical_stream_t) );
816 memset( p_stream, 0, sizeof(logical_stream_t) );
817 p_stream->p_headers = 0;
818 p_stream->secondary_header_packets = 0;
820 es_format_Init( &p_stream->fmt, 0, 0 );
821 es_format_Init( &p_stream->fmt_old, 0, 0 );
823 /* Setup the logical stream */
824 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
825 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
827 /* Extract the initial header from the first page and verify
828 * the codec type of tis Ogg bitstream */
829 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
831 /* error. stream version mismatch perhaps */
832 msg_Err( p_demux, "error reading first page of "
833 "Ogg bitstream data" );
837 /* FIXME: check return value */
838 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
840 /* Check for Vorbis header */
841 if( oggpacket.bytes >= 7 &&
842 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
844 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
845 msg_Dbg( p_demux, "found vorbis header" );
847 /* Check for Speex header */
848 else if( oggpacket.bytes >= 5 &&
849 ! memcmp( oggpacket.packet, "Speex", 5 ) )
851 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
852 msg_Dbg( p_demux, "found speex header, channels: %i, "
853 "rate: %i, bitrate: %i",
854 p_stream->fmt.audio.i_channels,
855 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
857 /* Check for Flac header (< version 1.1.1) */
858 else if( oggpacket.bytes >= 4 &&
859 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
861 msg_Dbg( p_demux, "found FLAC header" );
863 /* Grrrr!!!! Did they really have to put all the
864 * important info in the second header packet!!!
865 * (STREAMINFO metadata is in the following packet) */
866 p_stream->b_force_backup = 1;
868 p_stream->fmt.i_cat = AUDIO_ES;
869 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
871 /* Check for Flac header (>= version 1.1.1) */
872 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
873 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
874 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
876 int i_packets = ((int)oggpacket.packet[7]) << 8 |
878 msg_Dbg( p_demux, "found FLAC header version %i.%i "
879 "(%i header packets)",
880 oggpacket.packet[5], oggpacket.packet[6],
883 p_stream->b_force_backup = 1;
885 p_stream->fmt.i_cat = AUDIO_ES;
886 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
887 oggpacket.packet += 13; oggpacket.bytes -= 13;
888 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
890 /* Check for Theora header */
891 else if( oggpacket.bytes >= 7 &&
892 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
894 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
897 "found theora header, bitrate: %i, rate: %f",
898 p_stream->fmt.i_bitrate, p_stream->f_rate );
900 /* Check for Dirac header */
901 else if( oggpacket.bytes >= 5 &&
902 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
904 Ogg_ReadDiracHeader( p_stream, &oggpacket );
905 msg_Dbg( p_demux, "found dirac header" );
907 /* Check for Tarkin header */
908 else if( oggpacket.bytes >= 7 &&
909 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
913 msg_Dbg( p_demux, "found tarkin header" );
914 p_stream->fmt.i_cat = VIDEO_ES;
915 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
917 /* Cheat and get additionnal info ;) */
918 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
919 oggpack_adv( &opb, 88 );
920 oggpack_adv( &opb, 104 );
921 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
922 p_stream->f_rate = 2; /* FIXME */
924 "found tarkin header, bitrate: %i, rate: %f",
925 p_stream->fmt.i_bitrate, p_stream->f_rate );
927 /* Check for Annodex header */
928 else if( oggpacket.bytes >= 7 &&
929 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
931 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
933 /* kill annodex track */
937 /* Check for Annodex header */
938 else if( oggpacket.bytes >= 7 &&
939 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
941 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
944 /* Check for Kate header */
945 else if( oggpacket.bytes >= 8 &&
946 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
948 Ogg_ReadKateHeader( p_stream, &oggpacket );
949 msg_Dbg( p_demux, "found kate header" );
951 else if( oggpacket.bytes >= 142 &&
952 !memcmp( &oggpacket.packet[1],
953 "Direct Show Samples embedded in Ogg", 35 ))
955 /* Old header type */
957 /* Check for video header (old format) */
958 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
959 oggpacket.bytes >= 184 )
961 p_stream->fmt.i_cat = VIDEO_ES;
962 p_stream->fmt.i_codec =
963 VLC_FOURCC( oggpacket.packet[68],
964 oggpacket.packet[69],
965 oggpacket.packet[70],
966 oggpacket.packet[71] );
967 msg_Dbg( p_demux, "found video header of type: %.4s",
968 (char *)&p_stream->fmt.i_codec );
970 p_stream->fmt.video.i_frame_rate = 10000000;
971 p_stream->fmt.video.i_frame_rate_base =
972 GetQWLE((oggpacket.packet+164));
973 p_stream->f_rate = 10000000.0 /
974 GetQWLE((oggpacket.packet+164));
975 p_stream->fmt.video.i_bits_per_pixel =
976 GetWLE((oggpacket.packet+182));
977 if( !p_stream->fmt.video.i_bits_per_pixel )
979 p_stream->fmt.video.i_bits_per_pixel = 24;
980 p_stream->fmt.video.i_width =
981 GetDWLE((oggpacket.packet+176));
982 p_stream->fmt.video.i_height =
983 GetDWLE((oggpacket.packet+180));
986 "fps: %f, width:%i; height:%i, bitcount:%i",
988 p_stream->fmt.video.i_width,
989 p_stream->fmt.video.i_height,
990 p_stream->fmt.video.i_bits_per_pixel);
993 /* Check for audio header (old format) */
994 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
996 unsigned int i_extra_size;
997 unsigned int i_format_tag;
999 p_stream->fmt.i_cat = AUDIO_ES;
1001 i_extra_size = GetWLE((oggpacket.packet+140));
1004 p_stream->fmt.i_extra = i_extra_size;
1005 p_stream->fmt.p_extra = malloc( i_extra_size );
1006 memcpy( p_stream->fmt.p_extra,
1007 oggpacket.packet + 142, i_extra_size );
1010 i_format_tag = GetWLE((oggpacket.packet+124));
1011 p_stream->fmt.audio.i_channels =
1012 GetWLE((oggpacket.packet+126));
1013 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1014 GetDWLE((oggpacket.packet+128));
1015 p_stream->fmt.i_bitrate =
1016 GetDWLE((oggpacket.packet+132)) * 8;
1017 p_stream->fmt.audio.i_blockalign =
1018 GetWLE((oggpacket.packet+136));
1019 p_stream->fmt.audio.i_bitspersample =
1020 GetWLE((oggpacket.packet+138));
1022 wf_tag_to_fourcc( i_format_tag,
1023 &p_stream->fmt.i_codec, 0 );
1025 if( p_stream->fmt.i_codec ==
1026 VLC_FOURCC('u','n','d','f') )
1028 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1029 ( i_format_tag >> 8 ) & 0xff,
1030 i_format_tag & 0xff );
1033 msg_Dbg( p_demux, "found audio header of type: %.4s",
1034 (char *)&p_stream->fmt.i_codec );
1035 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1036 "%dbits/sample %dkb/s",
1038 p_stream->fmt.audio.i_channels,
1039 p_stream->fmt.audio.i_rate,
1040 p_stream->fmt.audio.i_bitspersample,
1041 p_stream->fmt.i_bitrate / 1024 );
1046 msg_Dbg( p_demux, "stream %d has an old header "
1047 "but is of an unknown type", p_ogg->i_streams-1 );
1052 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
1053 == PACKET_TYPE_HEADER &&
1054 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
1056 stream_header *st = (stream_header *)(oggpacket.packet+1);
1058 /* Check for video header (new format) */
1059 if( !strncmp( st->streamtype, "video", 5 ) )
1061 p_stream->fmt.i_cat = VIDEO_ES;
1063 /* We need to get rid of the header packet */
1064 ogg_stream_packetout( &p_stream->os, &oggpacket );
1066 p_stream->fmt.i_codec =
1067 VLC_FOURCC( st->subtype[0], st->subtype[1],
1068 st->subtype[2], st->subtype[3] );
1069 msg_Dbg( p_demux, "found video header of type: %.4s",
1070 (char *)&p_stream->fmt.i_codec );
1072 p_stream->fmt.video.i_frame_rate = 10000000;
1073 p_stream->fmt.video.i_frame_rate_base =
1074 GetQWLE(&st->time_unit);
1075 p_stream->f_rate = 10000000.0 /
1076 GetQWLE(&st->time_unit);
1077 p_stream->fmt.video.i_bits_per_pixel =
1078 GetWLE(&st->bits_per_sample);
1079 p_stream->fmt.video.i_width =
1080 GetDWLE(&st->sh.video.width);
1081 p_stream->fmt.video.i_height =
1082 GetDWLE(&st->sh.video.height);
1085 "fps: %f, width:%i; height:%i, bitcount:%i",
1087 p_stream->fmt.video.i_width,
1088 p_stream->fmt.video.i_height,
1089 p_stream->fmt.video.i_bits_per_pixel );
1091 /* Check for audio header (new format) */
1092 else if( !strncmp( st->streamtype, "audio", 5 ) )
1097 p_stream->fmt.i_cat = AUDIO_ES;
1099 /* We need to get rid of the header packet */
1100 ogg_stream_packetout( &p_stream->os, &oggpacket );
1102 p_stream->fmt.i_extra = GetQWLE(&st->size) -
1103 sizeof(stream_header);
1104 if( p_stream->fmt.i_extra )
1106 p_stream->fmt.p_extra =
1107 malloc( p_stream->fmt.i_extra );
1108 memcpy( p_stream->fmt.p_extra, st + 1,
1109 p_stream->fmt.i_extra );
1112 memcpy( p_buffer, st->subtype, 4 );
1114 i_format_tag = strtol(p_buffer,NULL,16);
1115 p_stream->fmt.audio.i_channels =
1116 GetWLE(&st->sh.audio.channels);
1117 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1118 GetQWLE(&st->samples_per_unit);
1119 p_stream->fmt.i_bitrate =
1120 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
1121 p_stream->fmt.audio.i_blockalign =
1122 GetWLE(&st->sh.audio.blockalign);
1123 p_stream->fmt.audio.i_bitspersample =
1124 GetWLE(&st->bits_per_sample);
1126 wf_tag_to_fourcc( i_format_tag,
1127 &p_stream->fmt.i_codec, 0 );
1129 if( p_stream->fmt.i_codec ==
1130 VLC_FOURCC('u','n','d','f') )
1132 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1133 ( i_format_tag >> 8 ) & 0xff,
1134 i_format_tag & 0xff );
1137 msg_Dbg( p_demux, "found audio header of type: %.4s",
1138 (char *)&p_stream->fmt.i_codec );
1139 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1140 "%dbits/sample %dkb/s",
1142 p_stream->fmt.audio.i_channels,
1143 p_stream->fmt.audio.i_rate,
1144 p_stream->fmt.audio.i_bitspersample,
1145 p_stream->fmt.i_bitrate / 1024 );
1147 /* Check for text (subtitles) header */
1148 else if( !strncmp(st->streamtype, "text", 4) )
1150 /* We need to get rid of the header packet */
1151 ogg_stream_packetout( &p_stream->os, &oggpacket );
1153 msg_Dbg( p_demux, "found text subtitles header" );
1154 p_stream->fmt.i_cat = SPU_ES;
1155 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1156 p_stream->f_rate = 1000; /* granulepos is in milisec */
1160 msg_Dbg( p_demux, "stream %d has a header marker "
1161 "but is of an unknown type", p_ogg->i_streams-1 );
1166 else if( oggpacket.bytes >= 7 &&
1167 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1171 msg_Dbg( p_demux, "stream %d is a skeleton",
1172 p_ogg->i_streams-1 );
1173 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1177 msg_Dbg( p_demux, "stream %d is of unknown type",
1178 p_ogg->i_streams-1 );
1183 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1184 return VLC_EGENERIC;
1187 /* This is the first data page, which means we are now finished
1188 * with the initial pages. We just need to store it in the relevant
1190 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1192 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1204 return VLC_EGENERIC;
1207 /****************************************************************************
1208 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1209 * Elementary streams.
1210 ****************************************************************************/
1211 static int Ogg_BeginningOfStream( demux_t *p_demux )
1213 demux_sys_t *p_ogg = p_demux->p_sys ;
1214 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1217 /* Find the logical streams embedded in the physical stream and
1218 * initialize our p_ogg structure. */
1219 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1221 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1222 return VLC_EGENERIC;
1225 p_ogg->i_bitrate = 0;
1227 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1229 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1231 p_stream->p_es = NULL;
1233 /* Try first to reuse an old ES */
1235 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1236 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1238 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1240 p_stream->p_es = p_old_stream->p_es;
1241 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1243 p_old_stream->p_es = NULL;
1244 p_old_stream = NULL;
1247 if( !p_stream->p_es )
1248 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1250 // TODO: something to do here ?
1251 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1253 /* Set the CMML stream active */
1254 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1257 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1259 p_stream->i_pcr = p_stream->i_previous_pcr =
1260 p_stream->i_interpolated_pcr = -1;
1261 p_stream->b_reinit = 0;
1264 if( p_ogg->p_old_stream )
1266 if( p_ogg->p_old_stream->p_es )
1267 msg_Dbg( p_demux, "old stream not reused" );
1268 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1269 p_ogg->p_old_stream = NULL;
1274 /****************************************************************************
1275 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1276 ****************************************************************************/
1277 static void Ogg_EndOfStream( demux_t *p_demux )
1279 demux_sys_t *p_ogg = p_demux->p_sys ;
1282 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1283 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1284 free( p_ogg->pp_stream );
1287 p_ogg->i_bitrate = 0;
1288 p_ogg->i_streams = 0;
1289 p_ogg->pp_stream = NULL;
1293 * This function delete and release all data associated to a logical_stream_t
1295 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1297 if( p_stream->p_es )
1298 es_out_Del( p_demux->out, p_stream->p_es );
1300 ogg_stream_clear( &p_stream->os );
1301 free( p_stream->p_headers );
1303 es_format_Clean( &p_stream->fmt_old );
1304 es_format_Clean( &p_stream->fmt );
1309 * This function check if a we need to reset a decoder in case we are
1312 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1318 for( i = 0; i < 3; i++ )
1320 const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1321 const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1323 if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1326 const int i_new_size = GetWBE( &p_new_extra[0] );
1327 const int i_old_size = GetWBE( &p_old_extra[0] );
1329 if( i != 1 ) /* Ignore vorbis comment */
1331 if( i_new_size != i_old_size )
1333 if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1337 i_new += 2 + i_new_size;
1338 i_old += 2 + i_old_size;
1342 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1345 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1348 /* Only vorbis is supported */
1349 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) )
1350 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1353 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1355 return !b_compatible;
1358 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1359 ogg_packet *p_oggpacket )
1362 int i_fps_numerator;
1363 int i_fps_denominator;
1364 int i_keyframe_frequency_force;
1366 p_stream->fmt.i_cat = VIDEO_ES;
1367 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1369 /* Signal that we want to keep a backup of the theora
1370 * stream headers. They will be used when switching between
1372 p_stream->b_force_backup = 1;
1374 /* Cheat and get additionnal info ;) */
1375 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1376 bs_skip( &bitstream, 56 );
1377 bs_read( &bitstream, 8 ); /* major version num */
1378 bs_read( &bitstream, 8 ); /* minor version num */
1379 bs_read( &bitstream, 8 ); /* subminor version num */
1380 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1381 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1382 bs_read( &bitstream, 24 ); /* frame width */
1383 bs_read( &bitstream, 24 ); /* frame height */
1384 bs_read( &bitstream, 8 ); /* x offset */
1385 bs_read( &bitstream, 8 ); /* y offset */
1387 i_fps_numerator = bs_read( &bitstream, 32 );
1388 i_fps_denominator = bs_read( &bitstream, 32 );
1389 bs_read( &bitstream, 24 ); /* aspect_numerator */
1390 bs_read( &bitstream, 24 ); /* aspect_denominator */
1392 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1393 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1395 bs_read( &bitstream, 8 ); /* colorspace */
1396 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1397 bs_read( &bitstream, 6 ); /* quality */
1399 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1401 /* granule_shift = i_log( frequency_force -1 ) */
1402 p_stream->i_granule_shift = 0;
1403 i_keyframe_frequency_force--;
1404 while( i_keyframe_frequency_force )
1406 p_stream->i_granule_shift++;
1407 i_keyframe_frequency_force >>= 1;
1410 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1413 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1414 ogg_packet *p_oggpacket )
1418 p_stream->fmt.i_cat = AUDIO_ES;
1419 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1421 /* Signal that we want to keep a backup of the vorbis
1422 * stream headers. They will be used when switching between
1424 p_stream->b_force_backup = 1;
1426 /* Cheat and get additionnal info ;) */
1427 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1428 oggpack_adv( &opb, 88 );
1429 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1430 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1431 oggpack_read( &opb, 32 );
1432 oggpack_adv( &opb, 32 );
1433 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1436 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1437 ogg_packet *p_oggpacket )
1441 p_stream->fmt.i_cat = AUDIO_ES;
1442 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1444 /* Signal that we want to keep a backup of the speex
1445 * stream headers. They will be used when switching between
1447 p_stream->b_force_backup = 1;
1449 /* Cheat and get additionnal info ;) */
1450 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1451 oggpack_adv( &opb, 224 );
1452 oggpack_adv( &opb, 32 ); /* speex_version_id */
1453 oggpack_adv( &opb, 32 ); /* header_size */
1454 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1455 oggpack_adv( &opb, 32 ); /* mode */
1456 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1457 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1458 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1461 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1462 ogg_packet *p_oggpacket )
1464 /* Parse the STREAMINFO metadata */
1467 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1470 if( bs_read( &s, 7 ) == 0 )
1472 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1475 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1476 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1478 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1479 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1481 else msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1483 /* Fake this as the last metadata block */
1484 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1488 /* This ain't a STREAMINFO metadata */
1489 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1493 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1494 ogg_packet *p_oggpacket )
1501 p_stream->fmt.i_cat = SPU_ES;
1502 p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1504 /* Signal that we want to keep a backup of the kate
1505 * stream headers. They will be used when switching between
1507 p_stream->b_force_backup = 1;
1509 /* Cheat and get additionnal info ;) */
1510 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1511 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1512 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1513 oggpack_adv( &opb, 3*8 );
1514 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1515 oggpack_adv( &opb, 8*8 ); /* reserved */
1516 gnum = oggpack_read( &opb, 32 );
1517 gden = oggpack_read( &opb, 32 );
1518 p_stream->f_rate = (double)gnum/gden;
1520 p_stream->fmt.psz_language = malloc(16);
1521 if (p_stream->fmt.psz_language)
1524 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1525 p_stream->fmt.psz_language[15] = 0; /* just in case */
1530 oggpack_read(&opb,8);
1532 p_stream->fmt.psz_description = malloc(16);
1533 if (p_stream->fmt.psz_description)
1536 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1537 p_stream->fmt.psz_description[15] = 0; /* just in case */
1542 oggpack_read(&opb,8);
1546 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1547 logical_stream_t *p_stream,
1548 ogg_packet *p_oggpacket )
1550 if( p_oggpacket->bytes >= 28 &&
1551 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1555 uint16_t major_version;
1556 uint16_t minor_version;
1557 uint64_t timebase_numerator;
1558 uint64_t timebase_denominator;
1560 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1562 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1563 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1564 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1565 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1566 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1567 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1569 else if( p_oggpacket->bytes >= 42 &&
1570 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1572 uint64_t granule_rate_numerator;
1573 uint64_t granule_rate_denominator;
1574 char content_type_string[1024];
1576 /* Read in Annodex header fields */
1578 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1579 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1580 p_stream->secondary_header_packets =
1581 GetDWLE( &p_oggpacket->packet[24] );
1583 /* we are guaranteed that the first header field will be
1584 * the content-type (by the Annodex standard) */
1585 content_type_string[0] = '\0';
1586 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1588 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1589 p_oggpacket->bytes - 1 );
1590 if( p && p[0] == '\r' && p[1] == '\n' )
1591 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1592 content_type_string );
1595 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1596 granule_rate_numerator, granule_rate_denominator,
1597 p_stream->secondary_header_packets, content_type_string );
1599 p_stream->f_rate = (float) granule_rate_numerator /
1600 (float) granule_rate_denominator;
1602 /* What type of file do we have?
1603 * strcmp is safe to use here because we've extracted
1604 * content_type_string from the stream manually */
1605 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1607 /* n.b. WAVs are unsupported right now */
1608 p_stream->fmt.i_cat = UNKNOWN_ES;
1610 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1612 p_stream->fmt.i_cat = AUDIO_ES;
1613 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1615 p_stream->b_force_backup = 1;
1617 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1619 p_stream->fmt.i_cat = AUDIO_ES;
1620 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1622 p_stream->b_force_backup = 1;
1624 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1626 p_stream->fmt.i_cat = VIDEO_ES;
1627 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1629 p_stream->b_force_backup = 1;
1631 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1633 p_stream->fmt.i_cat = VIDEO_ES;
1634 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1636 p_stream->b_force_backup = 1;
1638 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1640 /* n.b. MPEG streams are unsupported right now */
1641 p_stream->fmt.i_cat = VIDEO_ES;
1642 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1644 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1646 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1647 p_stream->fmt.i_cat = SPU_ES;
1648 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );
1653 /* every packet[1] in the stream contains a picture, there may
1654 * be header cruft infront of it though
1655 * [1] EXCEPT the BOS page/packet */
1656 static uint32_t Ogg_ReadDiracPictureNumber( ogg_packet *p_oggpacket )
1659 /* protect against falling off the edge */
1660 while ( u_pos + 13 < p_oggpacket->bytes ) {
1661 /* find the picture startcode */
1662 if ( p_oggpacket->packet[u_pos] & 0x08 ) {
1663 return GetDWBE( p_oggpacket->packet + u_pos + 9 );
1665 /* skip to the next dirac parse unit */
1666 uint32_t u_npo = GetDWBE( p_oggpacket->packet + u_pos + 1 );
1674 static uint32_t dirac_uint( bs_t *p_bs )
1676 uint32_t count = 0, value = 0;
1677 while( !bs_read ( p_bs, 1 ) ) {
1680 value |= bs_read ( p_bs, 1 );
1683 return (1<<count) - 1 + value;
1686 static int dirac_bool( bs_t *p_bs )
1688 return bs_read ( p_bs, 1 );
1691 static void Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1692 ogg_packet *p_oggpacket )
1696 p_stream->fmt.i_cat = VIDEO_ES;
1697 p_stream->fmt.i_codec = VLC_FOURCC( 'd','r','a','c' );
1698 p_stream->i_granule_shift = 32;
1700 /* Backing up stream headers is not required -- seqhdrs are repeated
1701 * thoughout the stream at suitable decoding start points */
1702 p_stream->b_force_backup = 0;
1704 /* read in useful bits from sequence header */
1705 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1706 bs_skip( &bs, 13*8); /* parse_info_header */
1707 dirac_uint( &bs ); /* major_version */
1708 dirac_uint( &bs ); /* minor_version */
1709 dirac_uint( &bs ); /* profile */
1710 dirac_uint( &bs ); /* level */
1712 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1714 if (dirac_bool( &bs )) {
1715 dirac_uint( &bs ); /* frame_width */
1716 dirac_uint( &bs ); /* frame_height */
1719 if (dirac_bool( &bs )) {
1720 dirac_uint( &bs ); /* chroma_format */
1722 if (dirac_bool( &bs )) {
1723 if (dirac_bool( &bs )) { /* interlaced */
1724 dirac_bool( &bs ); /* top_field_first */
1728 static const struct {
1729 uint32_t u_n /* numerator */, u_d /* denominator */;
1730 } dirac_frate_tbl[] = { /* table 10.3 */
1731 {1,1}, /* this first value is never used */
1732 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1733 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1736 static const uint32_t dirac_vidfmt_frate[] = { /* table C.1 */
1737 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1740 uint32_t u_n = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_n;
1741 uint32_t u_d = dirac_frate_tbl[dirac_vidfmt_frate[u_video_format]].u_d;
1742 if (dirac_bool( &bs )) {
1743 uint32_t frame_rate_index = dirac_uint( &bs );
1744 u_n = dirac_frate_tbl[frame_rate_index].u_n;
1745 u_d = dirac_frate_tbl[frame_rate_index].u_d;
1746 if (frame_rate_index == 0) {
1747 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1748 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1751 p_stream->f_rate = (float) u_n / u_d;