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>
42 #include <vlc_charset.h>
44 #include "kate_categories.h"
46 /*****************************************************************************
48 *****************************************************************************/
49 static int Open ( vlc_object_t * );
50 static void Close( vlc_object_t * );
53 set_shortname ( "OGG" )
54 set_description( N_("OGG demuxer" ) )
55 set_category( CAT_INPUT )
56 set_subcategory( SUBCAT_INPUT_DEMUX )
57 set_capability( "demux", 50 )
58 set_callbacks( Open, Close )
63 /*****************************************************************************
64 * Definitions of structures and functions used by this plugins
65 *****************************************************************************/
66 typedef struct logical_stream_s
68 ogg_stream_state os; /* logical stream of packets */
71 es_format_t fmt_old; /* format of old ES is reused */
77 /* the header of some logical streams (eg vorbis) contain essential
78 * data for the decoder. We back them up here in case we need to re-feed
79 * them to the decoder. */
85 /* program clock reference (in units of 90kHz) derived from the previous
88 mtime_t i_interpolated_pcr;
89 mtime_t i_previous_pcr;
95 /* kate streams have the number of headers in the ID header */
96 int i_kate_num_headers;
98 /* for Annodex logical bitstreams */
99 int i_secondary_header_packets;
105 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
107 int i_streams; /* number of logical bitstreams */
108 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
110 logical_stream_t *p_old_stream; /* pointer to a old logical stream to avoid recreating it */
112 /* program clock reference (in units of 90kHz) derived from the pcr of
123 /* after reading all headers, the first data page is stuffed into the relevant stream, ready to use */
130 /* OggDS headers for the new header format (used in ogm files) */
135 } stream_header_video_t;
139 ogg_int16_t channels;
141 ogg_int16_t blockalign;
142 ogg_int32_t avgbytespersec;
143 } stream_header_audio_t;
150 ogg_int32_t size; /* size of the structure */
152 ogg_int64_t time_unit; /* in reference time */
153 ogg_int64_t samples_per_unit;
154 ogg_int32_t default_len; /* in media time */
156 ogg_int32_t buffersize;
157 ogg_int16_t bits_per_sample;
163 stream_header_video_t video;
165 stream_header_audio_t audio;
169 #define OGG_BLOCK_SIZE 4096
171 /* Some defines from OggDS */
172 #define PACKET_TYPE_HEADER 0x01
173 #define PACKET_TYPE_BITS 0x07
174 #define PACKET_LEN_BITS01 0xc0
175 #define PACKET_LEN_BITS2 0x02
176 #define PACKET_IS_SYNCPOINT 0x08
178 /*****************************************************************************
180 *****************************************************************************/
181 static int Demux ( demux_t * );
182 static int Control( demux_t *, int, va_list );
184 /* Bitstream manipulation */
185 static int Ogg_ReadPage ( demux_t *, ogg_page * );
186 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
187 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
189 static int Ogg_BeginningOfStream( demux_t *p_demux );
190 static int Ogg_FindLogicalStreams( demux_t *p_demux );
191 static void Ogg_EndOfStream( demux_t *p_demux );
194 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
195 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
198 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers );
200 /* Logical bitstream headers */
201 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
202 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
203 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
204 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
205 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
206 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
207 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
209 /*****************************************************************************
210 * Open: initializes ogg demux structures
211 *****************************************************************************/
212 static int Open( vlc_object_t * p_this )
214 demux_t *p_demux = (demux_t *)p_this;
216 const uint8_t *p_peek;
219 /* Check if we are dealing with an ogg stream */
220 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
221 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
226 /* Set exported functions */
227 p_demux->pf_demux = Demux;
228 p_demux->pf_control = Control;
229 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
233 memset( p_sys, 0, sizeof( demux_sys_t ) );
234 p_sys->i_bitrate = 0;
235 p_sys->pp_stream = NULL;
236 p_sys->p_old_stream = NULL;
238 /* Begnning of stream, tell the demux to look for elementary streams. */
242 /* Initialize the Ogg physical bitstream parser */
243 ogg_sync_init( &p_sys->oy );
244 p_sys->b_page_waiting = false;
247 p_sys->p_meta = NULL;
252 /*****************************************************************************
253 * Close: frees unused data
254 *****************************************************************************/
255 static void Close( vlc_object_t *p_this )
257 demux_t *p_demux = (demux_t *)p_this;
258 demux_sys_t *p_sys = p_demux->p_sys ;
260 /* Cleanup the bitstream parser */
261 ogg_sync_clear( &p_sys->oy );
263 Ogg_EndOfStream( p_demux );
265 if( p_sys->p_old_stream )
266 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
271 /*****************************************************************************
272 * Demux: reads and demuxes data packets
273 *****************************************************************************
274 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
275 *****************************************************************************/
276 static int Demux( demux_t * p_demux )
278 demux_sys_t *p_sys = p_demux->p_sys;
280 ogg_packet oggpacket;
284 if( p_sys->i_eos == p_sys->i_streams )
288 msg_Dbg( p_demux, "end of a group of logical streams" );
289 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
290 * only 1 ES is supported (common case for ogg web radio) */
291 if( p_sys->i_streams == 1 )
293 p_sys->p_old_stream = p_sys->pp_stream[0];
294 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
296 Ogg_EndOfStream( p_demux );
300 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
303 msg_Dbg( p_demux, "beginning of a group of logical streams" );
304 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
308 * The first data page of a physical stream is stored in the relevant logical stream
309 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
310 * stream it belongs to if we haven't processed this first page yet. If we do, we
311 * will only process that first page whenever we find the second page for this stream.
312 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
313 * the second page will arrive real quick, this is not fine for Kate, whose second
314 * data page will typically arrive much later.
315 * This means it is now possible to seek right at the start of a stream where the last
316 * logical stream is Kate, without having to wait for the second data page to unblock
317 * the first one, which is the one that triggers the 'no more headers to backup' code.
318 * And, as we all know, seeking without having backed up all headers is bad, since the
319 * codec will fail to initialize if it's missing its headers.
321 if( !p_sys->b_page_waiting)
324 * Demux an ogg page from the stream
326 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
329 /* Test for End of Stream */
330 if( ogg_page_eos( &oggpage ) )
335 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
337 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
339 /* if we've just pulled page, look for the right logical stream */
340 if( !p_sys->b_page_waiting )
342 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
346 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
348 /* Read info from any secondary header packets, if there are any */
349 if( p_stream->i_secondary_header_packets > 0 )
351 if( p_stream->fmt.i_codec == VLC_FOURCC('t','h','e','o') &&
352 oggpacket.bytes >= 7 &&
353 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
355 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
356 p_stream->i_secondary_header_packets = 0;
358 else if( p_stream->fmt.i_codec == VLC_FOURCC('v','o','r','b') &&
359 oggpacket.bytes >= 7 &&
360 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
362 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
363 p_stream->i_secondary_header_packets = 0;
365 else if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
367 p_stream->i_secondary_header_packets = 0;
371 if( p_stream->b_reinit )
373 /* If synchro is re-initialized we need to drop all the packets
374 * until we find a new dated one. */
375 Ogg_UpdatePCR( p_stream, &oggpacket );
377 if( p_stream->i_pcr >= 0 )
379 p_stream->b_reinit = false;
383 p_stream->i_interpolated_pcr = -1;
387 /* An Ogg/vorbis packet contains an end date granulepos */
388 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
389 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
390 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
392 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
394 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
398 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
405 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
408 if( !p_sys->b_page_waiting )
412 /* if a page was waiting, it's now processed */
413 p_sys->b_page_waiting = false;
416 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
418 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
420 if( p_stream->fmt.i_cat == SPU_ES )
422 if( p_stream->i_interpolated_pcr < 0 )
425 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
426 p_sys->i_pcr = p_stream->i_interpolated_pcr;
429 if( p_sys->i_pcr >= 0 )
430 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
435 /*****************************************************************************
437 *****************************************************************************/
438 static int Control( demux_t *p_demux, int i_query, va_list args )
440 demux_sys_t *p_sys = p_demux->p_sys;
449 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
451 vlc_meta_Merge( p_meta, p_sys->p_meta );
454 case DEMUX_HAS_UNSUPPORTED_META:
455 pb_bool = (bool*)va_arg( args, bool* );
460 pi64 = (int64_t*)va_arg( args, int64_t * );
461 *pi64 = p_sys->i_pcr;
467 case DEMUX_SET_POSITION:
468 /* forbid seeking if we haven't initialized all logical bitstreams yet;
469 if we allowed, some headers would not get backed up and decoder init
470 would fail, making that logical stream unusable */
471 if( p_sys->i_bos > 0 )
476 for( i = 0; i < p_sys->i_streams; i++ )
478 logical_stream_t *p_stream = p_sys->pp_stream[i];
480 /* we'll trash all the data until we find the next pcr */
481 p_stream->b_reinit = true;
482 p_stream->i_pcr = -1;
483 p_stream->i_interpolated_pcr = -1;
484 ogg_stream_reset( &p_stream->os );
486 ogg_sync_reset( &p_sys->oy );
487 /* XXX The break/return is missing on purpose as
488 * demux_vaControlHelper will do the last part of the job */
491 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
496 /****************************************************************************
497 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
498 ****************************************************************************
499 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
500 * are at the end of stream.
501 ****************************************************************************/
502 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
504 demux_sys_t *p_ogg = p_demux->p_sys ;
508 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
510 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGG_BLOCK_SIZE );
512 i_read = stream_Read( p_demux->s, p_buffer, OGG_BLOCK_SIZE );
516 ogg_sync_wrote( &p_ogg->oy, i_read );
522 /****************************************************************************
523 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
525 ****************************************************************************/
526 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
527 ogg_packet *p_oggpacket )
529 /* Convert the granulepos into a pcr */
530 if( p_oggpacket->granulepos >= 0 )
532 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) ||
533 p_stream->fmt.i_codec == VLC_FOURCC( 'k','a','t','e' ) )
535 ogg_int64_t iframe = p_oggpacket->granulepos >>
536 p_stream->i_granule_shift;
537 ogg_int64_t pframe = p_oggpacket->granulepos -
538 ( iframe << p_stream->i_granule_shift );
540 p_stream->i_pcr = ( iframe + pframe ) * INT64_C(1000000)
543 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
545 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
546 /* NB, OggDirac granulepos values are in units of 2*picturerate */
547 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
551 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
555 p_stream->i_pcr += 1;
556 p_stream->i_interpolated_pcr = p_stream->i_pcr;
560 p_stream->i_pcr = -1;
562 /* no granulepos available, try to interpolate the pcr.
563 * If we can't then don't touch the old value. */
564 if( p_stream->fmt.i_cat == VIDEO_ES )
565 /* 1 frame per packet */
566 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
567 else if( p_stream->fmt.i_bitrate )
568 p_stream->i_interpolated_pcr +=
569 ( p_oggpacket->bytes * INT64_C(1000000) /
570 p_stream->fmt.i_bitrate / 8 );
574 /****************************************************************************
575 * Ogg_DecodePacket: Decode an Ogg packet.
576 ****************************************************************************/
577 static void Ogg_DecodePacket( demux_t *p_demux,
578 logical_stream_t *p_stream,
579 ogg_packet *p_oggpacket )
583 int i_header_len = 0;
584 mtime_t i_pts = -1, i_interpolated_pts;
585 demux_sys_t *p_ogg = p_demux->p_sys;
588 if( !p_oggpacket->bytes )
590 msg_Dbg( p_demux, "discarding 0 sized packet" );
594 if( p_oggpacket->bytes >= 7 &&
595 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
597 /* it's an Annodex packet -- skip it (do nothing) */
600 else if( p_oggpacket->bytes >= 7 &&
601 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
603 /* it's an AnxData packet -- skip it (do nothing) */
607 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ) &&
608 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
610 /* Check the ES is selected */
611 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
612 p_stream->p_es, &b_selected );
614 if( p_stream->b_force_backup )
617 bool b_store_size = true;
618 bool b_store_num_headers = false;
620 p_stream->i_packets_backup++;
621 switch( p_stream->fmt.i_codec )
623 case VLC_FOURCC( 'v','o','r','b' ):
624 case VLC_FOURCC( 's','p','x',' ' ):
625 case VLC_FOURCC( 't','h','e','o' ):
626 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
629 case VLC_FOURCC( 'f','l','a','c' ):
630 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
632 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
633 p_stream->b_force_backup = 0;
635 else if( p_stream->fmt.audio.i_rate )
637 p_stream->b_force_backup = 0;
638 if( p_oggpacket->bytes >= 9 )
640 p_oggpacket->packet += 9;
641 p_oggpacket->bytes -= 9;
644 b_store_size = false;
647 case VLC_FOURCC( 'k','a','t','e' ):
648 if( p_stream->i_packets_backup == 1)
649 b_store_num_headers = true;
650 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
654 p_stream->b_force_backup = 0;
658 /* Backup the ogg packet (likely an header packet) */
659 p_stream->p_headers =
660 realloc( p_sav = p_stream->p_headers, p_stream->i_headers +
661 p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0) );
662 if( p_stream->p_headers )
664 uint8_t *p_extra = p_stream->p_headers + p_stream->i_headers;
666 if( b_store_num_headers )
668 /* Kate streams store the number of headers in the first header,
669 so we can't just test for 3 as Vorbis/Theora */
670 *(p_extra++) = p_stream->i_kate_num_headers;
674 *(p_extra++) = p_oggpacket->bytes >> 8;
675 *(p_extra++) = p_oggpacket->bytes & 0xFF;
677 memcpy( p_extra, p_oggpacket->packet, p_oggpacket->bytes );
678 p_stream->i_headers += p_oggpacket->bytes + (b_store_size ? 2 : 0) + (b_store_num_headers ? 1 : 0);
680 if( !p_stream->b_force_backup )
682 /* Last header received, commit changes */
683 free( p_stream->fmt.p_extra );
685 p_stream->fmt.i_extra = p_stream->i_headers;
686 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
687 if( p_stream->fmt.p_extra )
688 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
689 p_stream->i_headers );
691 p_stream->fmt.i_extra = 0;
693 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
694 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
695 p_stream->p_es, &p_stream->fmt );
697 if( p_stream->i_headers > 0 )
698 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
699 p_stream->p_headers, p_stream->i_headers );
701 /* we're not at BOS anymore for this logical stream */
707 p_stream->p_headers = p_sav;
710 b_selected = false; /* Discard the header packet */
713 /* Convert the pcr into a pts */
714 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
715 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
716 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
718 if( p_stream->i_pcr >= 0 )
720 /* This is for streams where the granulepos of the header packets
721 * doesn't match these of the data packets (eg. ogg web radios). */
722 if( p_stream->i_previous_pcr == 0 &&
723 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
725 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
727 /* Call the pace control */
728 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
732 p_stream->i_previous_pcr = p_stream->i_pcr;
734 /* The granulepos is the end date of the sample */
735 i_pts = p_stream->i_pcr;
739 /* Convert the granulepos into the next pcr */
740 i_interpolated_pts = p_stream->i_interpolated_pcr;
741 Ogg_UpdatePCR( p_stream, p_oggpacket );
743 /* SPU streams are typically discontinuous, do not mind large gaps */
744 if( p_stream->fmt.i_cat != SPU_ES )
746 if( p_stream->i_pcr >= 0 )
748 /* This is for streams where the granulepos of the header packets
749 * doesn't match these of the data packets (eg. ogg web radios). */
750 if( p_stream->i_previous_pcr == 0 &&
751 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
753 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
755 /* Call the pace control */
756 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_stream->i_pcr );
761 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
762 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
763 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
764 p_stream->i_pcr >= 0 )
766 p_stream->i_previous_pcr = p_stream->i_pcr;
768 /* The granulepos is the start date of the sample */
769 i_pts = p_stream->i_pcr;
774 /* This stream isn't currently selected so we don't need to decode it,
775 * but we did need to store its pcr as it might be selected later on */
779 if( p_oggpacket->bytes <= 0 )
782 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
785 if( i_pts == 0 ) i_pts = 1;
786 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = 1;
787 else if( i_pts == -1 ) i_pts = 0;
789 if( p_stream->fmt.i_cat == AUDIO_ES )
790 p_block->i_dts = p_block->i_pts = i_pts;
791 else if( p_stream->fmt.i_cat == SPU_ES )
793 p_block->i_dts = p_block->i_pts = i_pts;
794 p_block->i_length = 0;
796 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
797 p_block->i_dts = p_block->i_pts = i_pts;
798 else if( p_stream->fmt.i_codec == VLC_FOURCC( 'd','r','a','c' ) )
800 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
801 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
803 uint64_t u_pnum = dts + delay;
805 p_block->i_dts = p_stream->i_pcr;
807 /* NB, OggDirac granulepos values are in units of 2*picturerate */
808 if( -1 != p_oggpacket->granulepos )
809 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
813 p_block->i_dts = i_pts;
817 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
818 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
819 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
820 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
821 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) &&
822 p_stream->fmt.i_codec != VLC_FOURCC( 'c','m','m','l' ) &&
823 p_stream->fmt.i_codec != VLC_FOURCC( 'd','r','a','c' ) &&
824 p_stream->fmt.i_codec != VLC_FOURCC( 'k','a','t','e' ) )
826 /* We remove the header from the packet */
827 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
828 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
830 if( p_stream->fmt.i_codec == VLC_FOURCC( 's','u','b','t' ))
832 /* But with subtitles we need to retrieve the duration first */
835 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
837 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
839 lenbytes = lenbytes << 8;
840 lenbytes += *(p_oggpacket->packet + i_header_len - i);
843 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
844 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
845 p_oggpacket->packet[i_header_len + 1] != 0 &&
846 p_oggpacket->packet[i_header_len + 1] != '\n' &&
847 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
849 p_block->i_length = (mtime_t)lenbytes * 1000;
854 if( p_block->i_buffer >= (unsigned int)i_header_len )
855 p_block->i_buffer -= i_header_len;
857 p_block->i_buffer = 0;
860 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
862 /* FIXME: the biggest hack I've ever done */
863 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
864 p_block->i_pts, p_block->i_dts );
868 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
869 p_oggpacket->bytes - i_header_len );
871 es_out_Send( p_demux->out, p_stream->p_es, p_block );
874 /****************************************************************************
875 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
876 * stream and fill p_ogg.
877 *****************************************************************************
878 * The initial page of a logical stream is marked as a 'bos' page.
879 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
880 * together and all of the initial pages must appear before any data pages.
882 * On success this function returns VLC_SUCCESS.
883 ****************************************************************************/
884 static int Ogg_FindLogicalStreams( demux_t *p_demux )
886 demux_sys_t *p_ogg = p_demux->p_sys ;
887 ogg_packet oggpacket;
891 while( Ogg_ReadPage( p_demux, &oggpage ) == VLC_SUCCESS )
893 if( ogg_page_bos( &oggpage ) )
896 /* All is wonderful in our fine fine little world.
897 * We found the beginning of our first logical stream. */
898 while( ogg_page_bos( &oggpage ) )
900 logical_stream_t *p_stream;
902 p_stream = malloc( sizeof(logical_stream_t) );
906 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
908 memset( p_stream, 0, sizeof(logical_stream_t) );
909 p_stream->p_headers = 0;
910 p_stream->i_secondary_header_packets = 0;
912 es_format_Init( &p_stream->fmt, 0, 0 );
913 es_format_Init( &p_stream->fmt_old, 0, 0 );
915 /* Setup the logical stream */
916 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
917 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
919 /* Extract the initial header from the first page and verify
920 * the codec type of this Ogg bitstream */
921 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
923 /* error. stream version mismatch perhaps */
924 msg_Err( p_demux, "error reading first page of "
925 "Ogg bitstream data" );
929 /* FIXME: check return value */
930 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
932 /* Check for Vorbis header */
933 if( oggpacket.bytes >= 7 &&
934 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
936 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
937 msg_Dbg( p_demux, "found vorbis header" );
939 /* Check for Speex header */
940 else if( oggpacket.bytes >= 5 &&
941 ! memcmp( oggpacket.packet, "Speex", 5 ) )
943 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
944 msg_Dbg( p_demux, "found speex header, channels: %i, "
945 "rate: %i, bitrate: %i",
946 p_stream->fmt.audio.i_channels,
947 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
949 /* Check for Flac header (< version 1.1.1) */
950 else if( oggpacket.bytes >= 4 &&
951 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
953 msg_Dbg( p_demux, "found FLAC header" );
955 /* Grrrr!!!! Did they really have to put all the
956 * important info in the second header packet!!!
957 * (STREAMINFO metadata is in the following packet) */
958 p_stream->b_force_backup = 1;
960 p_stream->fmt.i_cat = AUDIO_ES;
961 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
963 /* Check for Flac header (>= version 1.1.1) */
964 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
965 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
966 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
968 int i_packets = ((int)oggpacket.packet[7]) << 8 |
970 msg_Dbg( p_demux, "found FLAC header version %i.%i "
971 "(%i header packets)",
972 oggpacket.packet[5], oggpacket.packet[6],
975 p_stream->b_force_backup = 1;
977 p_stream->fmt.i_cat = AUDIO_ES;
978 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
979 oggpacket.packet += 13; oggpacket.bytes -= 13;
980 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
982 /* Check for Theora header */
983 else if( oggpacket.bytes >= 7 &&
984 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
986 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
989 "found theora header, bitrate: %i, rate: %f",
990 p_stream->fmt.i_bitrate, p_stream->f_rate );
992 /* Check for Dirac header */
993 else if( oggpacket.bytes >= 5 &&
994 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) )
996 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
997 msg_Dbg( p_demux, "found dirac header" );
1000 msg_Warn( p_demux, "found dirac header isn't decodable" );
1005 /* Check for Tarkin header */
1006 else if( oggpacket.bytes >= 7 &&
1007 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1011 msg_Dbg( p_demux, "found tarkin header" );
1012 p_stream->fmt.i_cat = VIDEO_ES;
1013 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
1015 /* Cheat and get additionnal info ;) */
1016 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1017 oggpack_adv( &opb, 88 );
1018 oggpack_adv( &opb, 104 );
1019 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1020 p_stream->f_rate = 2; /* FIXME */
1022 "found tarkin header, bitrate: %i, rate: %f",
1023 p_stream->fmt.i_bitrate, p_stream->f_rate );
1025 /* Check for Annodex header */
1026 else if( oggpacket.bytes >= 7 &&
1027 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1029 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1031 /* kill annodex track */
1035 /* Check for Annodex header */
1036 else if( oggpacket.bytes >= 7 &&
1037 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1039 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1042 /* Check for Kate header */
1043 else if( oggpacket.bytes >= 8 &&
1044 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1046 Ogg_ReadKateHeader( p_stream, &oggpacket );
1047 msg_Dbg( p_demux, "found kate header" );
1049 else if( oggpacket.bytes >= 142 &&
1050 !memcmp( &oggpacket.packet[1],
1051 "Direct Show Samples embedded in Ogg", 35 ))
1053 /* Old header type */
1055 /* Check for video header (old format) */
1056 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1057 oggpacket.bytes >= 184 )
1059 p_stream->fmt.i_cat = VIDEO_ES;
1060 p_stream->fmt.i_codec =
1061 VLC_FOURCC( oggpacket.packet[68],
1062 oggpacket.packet[69],
1063 oggpacket.packet[70],
1064 oggpacket.packet[71] );
1065 msg_Dbg( p_demux, "found video header of type: %.4s",
1066 (char *)&p_stream->fmt.i_codec );
1068 p_stream->fmt.video.i_frame_rate = 10000000;
1069 p_stream->fmt.video.i_frame_rate_base =
1070 GetQWLE((oggpacket.packet+164));
1071 p_stream->f_rate = 10000000.0 /
1072 GetQWLE((oggpacket.packet+164));
1073 p_stream->fmt.video.i_bits_per_pixel =
1074 GetWLE((oggpacket.packet+182));
1075 if( !p_stream->fmt.video.i_bits_per_pixel )
1077 p_stream->fmt.video.i_bits_per_pixel = 24;
1078 p_stream->fmt.video.i_width =
1079 GetDWLE((oggpacket.packet+176));
1080 p_stream->fmt.video.i_height =
1081 GetDWLE((oggpacket.packet+180));
1084 "fps: %f, width:%i; height:%i, bitcount:%i",
1086 p_stream->fmt.video.i_width,
1087 p_stream->fmt.video.i_height,
1088 p_stream->fmt.video.i_bits_per_pixel);
1091 /* Check for audio header (old format) */
1092 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1094 unsigned int i_extra_size;
1095 unsigned int i_format_tag;
1097 p_stream->fmt.i_cat = AUDIO_ES;
1099 i_extra_size = GetWLE((oggpacket.packet+140));
1100 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1102 p_stream->fmt.i_extra = i_extra_size;
1103 p_stream->fmt.p_extra = malloc( i_extra_size );
1104 if( p_stream->fmt.p_extra )
1105 memcpy( p_stream->fmt.p_extra,
1106 oggpacket.packet + 142, i_extra_size );
1108 p_stream->fmt.i_extra = 0;
1111 i_format_tag = GetWLE((oggpacket.packet+124));
1112 p_stream->fmt.audio.i_channels =
1113 GetWLE((oggpacket.packet+126));
1114 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1115 GetDWLE((oggpacket.packet+128));
1116 p_stream->fmt.i_bitrate =
1117 GetDWLE((oggpacket.packet+132)) * 8;
1118 p_stream->fmt.audio.i_blockalign =
1119 GetWLE((oggpacket.packet+136));
1120 p_stream->fmt.audio.i_bitspersample =
1121 GetWLE((oggpacket.packet+138));
1123 wf_tag_to_fourcc( i_format_tag,
1124 &p_stream->fmt.i_codec, 0 );
1126 if( p_stream->fmt.i_codec ==
1127 VLC_FOURCC('u','n','d','f') )
1129 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1130 ( i_format_tag >> 8 ) & 0xff,
1131 i_format_tag & 0xff );
1134 msg_Dbg( p_demux, "found audio header of type: %.4s",
1135 (char *)&p_stream->fmt.i_codec );
1136 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1137 "%dbits/sample %dkb/s",
1139 p_stream->fmt.audio.i_channels,
1140 p_stream->fmt.audio.i_rate,
1141 p_stream->fmt.audio.i_bitspersample,
1142 p_stream->fmt.i_bitrate / 1024 );
1147 msg_Dbg( p_demux, "stream %d has an old header "
1148 "but is of an unknown type", p_ogg->i_streams-1 );
1153 else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1154 oggpacket.bytes >= 56+1 )
1156 stream_header_t tmp;
1157 stream_header_t *st = &tmp;
1159 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1160 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1161 st->size = GetDWLE( &oggpacket.packet[1+12] );
1162 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1163 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1164 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1165 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1166 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1168 /* Check for video header (new format) */
1169 if( !strncmp( st->streamtype, "video", 5 ) )
1171 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1172 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1174 p_stream->fmt.i_cat = VIDEO_ES;
1176 /* We need to get rid of the header packet */
1177 ogg_stream_packetout( &p_stream->os, &oggpacket );
1179 p_stream->fmt.i_codec =
1180 VLC_FOURCC( st->subtype[0], st->subtype[1],
1181 st->subtype[2], st->subtype[3] );
1182 msg_Dbg( p_demux, "found video header of type: %.4s",
1183 (char *)&p_stream->fmt.i_codec );
1185 p_stream->fmt.video.i_frame_rate = 10000000;
1186 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1187 if( st->time_unit <= 0 )
1188 st->time_unit = 400000;
1189 p_stream->f_rate = 10000000.0 / st->time_unit;
1190 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1191 p_stream->fmt.video.i_width = st->sh.video.width;
1192 p_stream->fmt.video.i_height = st->sh.video.height;
1195 "fps: %f, width:%i; height:%i, bitcount:%i",
1197 p_stream->fmt.video.i_width,
1198 p_stream->fmt.video.i_height,
1199 p_stream->fmt.video.i_bits_per_pixel );
1201 /* Check for audio header (new format) */
1202 else if( !strncmp( st->streamtype, "audio", 5 ) )
1205 unsigned int i_extra_size;
1208 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1209 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1210 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1212 p_stream->fmt.i_cat = AUDIO_ES;
1214 /* We need to get rid of the header packet */
1215 ogg_stream_packetout( &p_stream->os, &oggpacket );
1217 i_extra_size = st->size - 56;
1219 if( i_extra_size > 0 &&
1220 i_extra_size < oggpacket.bytes - 1 - 56 )
1222 p_stream->fmt.i_extra = i_extra_size;
1223 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1224 if( p_stream->fmt.p_extra )
1225 memcpy( p_stream->fmt.p_extra, st + 1,
1226 p_stream->fmt.i_extra );
1228 p_stream->fmt.i_extra = 0;
1231 memcpy( p_buffer, st->subtype, 4 );
1233 i_format_tag = strtol(p_buffer,NULL,16);
1234 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1235 if( st->time_unit <= 0 )
1236 st->time_unit = 10000000;
1237 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1238 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1239 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1240 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1242 wf_tag_to_fourcc( i_format_tag,
1243 &p_stream->fmt.i_codec, 0 );
1245 if( p_stream->fmt.i_codec ==
1246 VLC_FOURCC('u','n','d','f') )
1248 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1249 ( i_format_tag >> 8 ) & 0xff,
1250 i_format_tag & 0xff );
1253 msg_Dbg( p_demux, "found audio header of type: %.4s",
1254 (char *)&p_stream->fmt.i_codec );
1255 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1256 "%dbits/sample %dkb/s",
1258 p_stream->fmt.audio.i_channels,
1259 p_stream->fmt.audio.i_rate,
1260 p_stream->fmt.audio.i_bitspersample,
1261 p_stream->fmt.i_bitrate / 1024 );
1263 /* Check for text (subtitles) header */
1264 else if( !strncmp(st->streamtype, "text", 4) )
1266 /* We need to get rid of the header packet */
1267 ogg_stream_packetout( &p_stream->os, &oggpacket );
1269 msg_Dbg( p_demux, "found text subtitles header" );
1270 p_stream->fmt.i_cat = SPU_ES;
1271 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
1272 p_stream->f_rate = 1000; /* granulepos is in millisec */
1276 msg_Dbg( p_demux, "stream %d has a header marker "
1277 "but is of an unknown type", p_ogg->i_streams-1 );
1282 else if( oggpacket.bytes >= 7 &&
1283 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1287 msg_Dbg( p_demux, "stream %d is a skeleton",
1288 p_ogg->i_streams-1 );
1289 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1293 msg_Dbg( p_demux, "stream %d is of unknown type",
1294 p_ogg->i_streams-1 );
1299 if( Ogg_ReadPage( p_demux, &oggpage ) != VLC_SUCCESS )
1300 return VLC_EGENERIC;
1303 /* we'll need to get all headers for all of those streams
1304 that we have to backup headers for */
1306 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1308 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1313 /* This is the first data page, which means we are now finished
1314 * with the initial pages. We just need to store it in the relevant
1316 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1318 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1321 p_ogg->b_page_waiting = true;
1330 return VLC_EGENERIC;
1333 /****************************************************************************
1334 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1335 * Elementary streams.
1336 ****************************************************************************/
1337 static int Ogg_BeginningOfStream( demux_t *p_demux )
1339 demux_sys_t *p_ogg = p_demux->p_sys ;
1340 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1343 /* Find the logical streams embedded in the physical stream and
1344 * initialize our p_ogg structure. */
1345 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1347 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1348 return VLC_EGENERIC;
1351 p_ogg->i_bitrate = 0;
1353 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1355 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1357 p_stream->p_es = NULL;
1359 /* Try first to reuse an old ES */
1361 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1362 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1364 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1366 p_stream->p_es = p_old_stream->p_es;
1367 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1369 p_old_stream->p_es = NULL;
1370 p_old_stream = NULL;
1373 if( !p_stream->p_es )
1375 /* Better be safe than sorry when possible with ogm */
1376 if( p_stream->fmt.i_codec == VLC_FOURCC( 'm', 'p', 'g', 'a' ) ||
1377 p_stream->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', ' ' ) )
1378 p_stream->fmt.b_packetized = false;
1380 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1383 // TODO: something to do here ?
1384 if( p_stream->fmt.i_codec == VLC_FOURCC('c','m','m','l') )
1386 /* Set the CMML stream active */
1387 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1390 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1392 p_stream->i_pcr = p_stream->i_previous_pcr =
1393 p_stream->i_interpolated_pcr = -1;
1394 p_stream->b_reinit = false;
1397 if( p_ogg->p_old_stream )
1399 if( p_ogg->p_old_stream->p_es )
1400 msg_Dbg( p_demux, "old stream not reused" );
1401 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1402 p_ogg->p_old_stream = NULL;
1407 /****************************************************************************
1408 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1409 ****************************************************************************/
1410 static void Ogg_EndOfStream( demux_t *p_demux )
1412 demux_sys_t *p_ogg = p_demux->p_sys ;
1415 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1416 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1417 free( p_ogg->pp_stream );
1420 p_ogg->i_bitrate = 0;
1421 p_ogg->i_streams = 0;
1422 p_ogg->pp_stream = NULL;
1426 vlc_meta_Delete( p_ogg->p_meta );
1427 p_ogg->p_meta = NULL;
1431 * This function delete and release all data associated to a logical_stream_t
1433 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1435 if( p_stream->p_es )
1436 es_out_Del( p_demux->out, p_stream->p_es );
1438 ogg_stream_clear( &p_stream->os );
1439 free( p_stream->p_headers );
1441 es_format_Clean( &p_stream->fmt_old );
1442 es_format_Clean( &p_stream->fmt );
1447 * This function check if a we need to reset a decoder in case we are
1450 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1456 for( i = 0; i < 3; i++ )
1458 const uint8_t *p_new_extra = ( const uint8_t*)p_new->p_extra + i_new;
1459 const uint8_t *p_old_extra = ( const uint8_t*)p_old->p_extra + i_old;
1461 if( p_new->i_extra < i_new+2 || p_old->i_extra < i_old+2 )
1464 const int i_new_size = GetWBE( &p_new_extra[0] );
1465 const int i_old_size = GetWBE( &p_old_extra[0] );
1467 if( i != 1 ) /* Ignore vorbis comment */
1469 if( i_new_size != i_old_size )
1471 if( memcmp( &p_new_extra[2], &p_old_extra[2], i_new_size ) )
1475 i_new += 2 + i_new_size;
1476 i_old += 2 + i_old_size;
1480 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1482 bool b_compatible = false;
1483 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1486 /* Only vorbis is supported */
1487 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) )
1488 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1491 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1493 return !b_compatible;
1495 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const uint8_t *p_headers, int i_headers, int i_skip, bool b_has_num_headers )
1497 demux_sys_t *p_ogg = p_demux->p_sys;
1499 if (b_has_num_headers)
1503 /* number of headers on a byte, we're interested in the second header, so should be at least 2 to go on */
1504 if (*p_headers++ < 2)
1509 if( i_headers <= 2 )
1512 /* Skip first packet */
1513 const int i_tmp = GetWBE( &p_headers[0] );
1514 if( i_tmp > i_headers-2 )
1516 p_headers += 2 + i_tmp;
1517 i_headers -= 2 + i_tmp;
1519 if( i_headers <= 2 )
1523 int i_comment = GetWBE( &p_headers[0] );
1524 const uint8_t *p_comment = &p_headers[2];
1525 if( i_comment > i_headers - 2 )
1528 if( i_comment <= i_skip )
1531 /* TODO how to handle multiple comments properly ? */
1532 vorbis_ParseComment( &p_ogg->p_meta, &p_comment[i_skip], i_comment - i_skip );
1534 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1536 demux_sys_t *p_ogg = p_demux->p_sys;
1540 /* 3 headers with the 2° one being the comments */
1541 case VLC_FOURCC( 'v','o','r','b' ):
1542 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6, false );
1544 case VLC_FOURCC( 't','h','e','o' ):
1545 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6, false );
1547 case VLC_FOURCC( 's','p','x',' ' ):
1548 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0, false );
1551 /* N headers with the 2° one being the comments */
1552 case VLC_FOURCC( 'k','a','t','e' ):
1553 /* 1 byte for header type, 7 bit for magic, 1 reserved zero byte */
1554 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1, true );
1558 case VLC_FOURCC( 'f','l','a','c' ):
1559 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1563 case VLC_FOURCC( 'c','m','m','l' ): /* CMML is XML text, doesn't have Vorbis comments */
1564 case VLC_FOURCC( 'd','r','a','c' ):
1569 p_demux->info.i_update |= INPUT_UPDATE_META;
1572 static void Ogg_ReadTheoraHeader( logical_stream_t *p_stream,
1573 ogg_packet *p_oggpacket )
1576 int i_fps_numerator;
1577 int i_fps_denominator;
1578 int i_keyframe_frequency_force;
1580 p_stream->fmt.i_cat = VIDEO_ES;
1581 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1583 /* Signal that we want to keep a backup of the theora
1584 * stream headers. They will be used when switching between
1586 p_stream->b_force_backup = 1;
1588 /* Cheat and get additionnal info ;) */
1589 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1590 bs_skip( &bitstream, 56 );
1591 bs_read( &bitstream, 8 ); /* major version num */
1592 bs_read( &bitstream, 8 ); /* minor version num */
1593 bs_read( &bitstream, 8 ); /* subminor version num */
1594 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1595 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1596 bs_read( &bitstream, 24 ); /* frame width */
1597 bs_read( &bitstream, 24 ); /* frame height */
1598 bs_read( &bitstream, 8 ); /* x offset */
1599 bs_read( &bitstream, 8 ); /* y offset */
1601 i_fps_numerator = bs_read( &bitstream, 32 );
1602 i_fps_denominator = bs_read( &bitstream, 32 );
1603 bs_read( &bitstream, 24 ); /* aspect_numerator */
1604 bs_read( &bitstream, 24 ); /* aspect_denominator */
1606 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1607 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1609 bs_read( &bitstream, 8 ); /* colorspace */
1610 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1611 bs_read( &bitstream, 6 ); /* quality */
1613 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1615 /* granule_shift = i_log( frequency_force -1 ) */
1616 p_stream->i_granule_shift = 0;
1617 i_keyframe_frequency_force--;
1618 while( i_keyframe_frequency_force )
1620 p_stream->i_granule_shift++;
1621 i_keyframe_frequency_force >>= 1;
1624 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1627 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1628 ogg_packet *p_oggpacket )
1632 p_stream->fmt.i_cat = AUDIO_ES;
1633 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1635 /* Signal that we want to keep a backup of the vorbis
1636 * stream headers. They will be used when switching between
1638 p_stream->b_force_backup = 1;
1640 /* Cheat and get additionnal info ;) */
1641 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1642 oggpack_adv( &opb, 88 );
1643 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1644 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1645 oggpack_read( &opb, 32 );
1646 oggpack_adv( &opb, 32 );
1647 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1650 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1651 ogg_packet *p_oggpacket )
1655 p_stream->fmt.i_cat = AUDIO_ES;
1656 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1658 /* Signal that we want to keep a backup of the speex
1659 * stream headers. They will be used when switching between
1661 p_stream->b_force_backup = 1;
1663 /* Cheat and get additionnal info ;) */
1664 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1665 oggpack_adv( &opb, 224 );
1666 oggpack_adv( &opb, 32 ); /* speex_version_id */
1667 oggpack_adv( &opb, 32 ); /* header_size */
1668 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1669 oggpack_adv( &opb, 32 ); /* mode */
1670 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1671 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1672 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1675 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1676 ogg_packet *p_oggpacket )
1678 /* Parse the STREAMINFO metadata */
1681 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1684 if( bs_read( &s, 7 ) == 0 )
1686 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1689 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1690 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1692 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1693 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1697 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1700 /* Fake this as the last metadata block */
1701 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1705 /* This ain't a STREAMINFO metadata */
1706 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1710 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1711 ogg_packet *p_oggpacket )
1719 p_stream->fmt.i_cat = SPU_ES;
1720 p_stream->fmt.i_codec = VLC_FOURCC( 'k','a','t','e' );
1722 /* Signal that we want to keep a backup of the kate
1723 * stream headers. They will be used when switching between
1725 p_stream->b_force_backup = 1;
1727 /* Cheat and get additionnal info ;) */
1728 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1729 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1730 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1731 oggpack_adv( &opb, 3*8 );
1732 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1733 oggpack_adv( &opb, 8*8 ); /* reserved */
1734 gnum = oggpack_read( &opb, 32 );
1735 gden = oggpack_read( &opb, 32 );
1736 p_stream->f_rate = (double)gnum/gden;
1738 p_stream->fmt.psz_language = malloc(16);
1739 if( p_stream->fmt.psz_language )
1741 for( n = 0; n < 16; n++ )
1742 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1743 p_stream->fmt.psz_language[15] = 0; /* just in case */
1747 for( n = 0; n < 16; n++ )
1748 oggpack_read(&opb,8);
1750 p_stream->fmt.psz_description = malloc(16);
1751 if( p_stream->fmt.psz_description )
1753 for( n = 0; n < 16; n++ )
1754 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1755 p_stream->fmt.psz_description[15] = 0; /* just in case */
1757 /* Now find a localized user readable description for this category */
1758 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
1761 free( p_stream->fmt.psz_description );
1762 p_stream->fmt.psz_description = psz_desc;
1767 for( n = 0; n < 16; n++ )
1768 oggpack_read(&opb,8);
1772 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1773 logical_stream_t *p_stream,
1774 ogg_packet *p_oggpacket )
1776 if( p_oggpacket->bytes >= 28 &&
1777 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1781 uint16_t major_version;
1782 uint16_t minor_version;
1783 uint64_t timebase_numerator;
1784 uint64_t timebase_denominator;
1786 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1788 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1789 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1790 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1791 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1792 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1793 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1795 else if( p_oggpacket->bytes >= 42 &&
1796 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1798 uint64_t granule_rate_numerator;
1799 uint64_t granule_rate_denominator;
1800 char content_type_string[1024];
1802 /* Read in Annodex header fields */
1804 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1805 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1806 p_stream->i_secondary_header_packets =
1807 GetDWLE( &p_oggpacket->packet[24] );
1809 /* we are guaranteed that the first header field will be
1810 * the content-type (by the Annodex standard) */
1811 content_type_string[0] = '\0';
1812 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1814 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1815 p_oggpacket->bytes - 1 );
1816 if( p && p[0] == '\r' && p[1] == '\n' )
1817 sscanf( (char*)(&p_oggpacket->packet[42]), "%1024s\r\n",
1818 content_type_string );
1821 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1822 granule_rate_numerator, granule_rate_denominator,
1823 p_stream->i_secondary_header_packets, content_type_string );
1825 p_stream->f_rate = (float) granule_rate_numerator /
1826 (float) granule_rate_denominator;
1828 /* What type of file do we have?
1829 * strcmp is safe to use here because we've extracted
1830 * content_type_string from the stream manually */
1831 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1833 /* n.b. WAVs are unsupported right now */
1834 p_stream->fmt.i_cat = UNKNOWN_ES;
1836 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1838 p_stream->fmt.i_cat = AUDIO_ES;
1839 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
1841 p_stream->b_force_backup = 1;
1843 else if( !strncmp(content_type_string, "audio/x-speex", 14) )
1845 p_stream->fmt.i_cat = AUDIO_ES;
1846 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
1848 p_stream->b_force_backup = 1;
1850 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1852 p_stream->fmt.i_cat = VIDEO_ES;
1853 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
1855 p_stream->b_force_backup = 1;
1857 else if( !strncmp(content_type_string, "video/x-xvid", 14) )
1859 p_stream->fmt.i_cat = VIDEO_ES;
1860 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1862 p_stream->b_force_backup = 1;
1864 else if( !strncmp(content_type_string, "video/mpeg", 14) )
1866 /* n.b. MPEG streams are unsupported right now */
1867 p_stream->fmt.i_cat = VIDEO_ES;
1868 p_stream->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
1870 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1872 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1873 p_stream->fmt.i_cat = SPU_ES;
1874 p_stream->fmt.i_codec = VLC_FOURCC( 'c','m','m','l' );
1879 static uint32_t dirac_uint( bs_t *p_bs )
1881 uint32_t u_count = 0, u_value = 0;
1883 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
1887 u_value |= bs_read( p_bs, 1 );
1890 return (1<<u_count) - 1 + u_value;
1893 static int dirac_bool( bs_t *p_bs )
1895 return bs_read( p_bs, 1 );
1898 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1899 ogg_packet *p_oggpacket )
1901 static const struct {
1902 uint32_t u_n /* numerator */, u_d /* denominator */;
1903 } p_dirac_frate_tbl[] = { /* table 10.3 */
1904 {1,1}, /* this first value is never used */
1905 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1906 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1908 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
1910 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
1911 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1913 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
1917 p_stream->i_granule_shift = 22; /* not 32 */
1919 /* Backing up stream headers is not required -- seqhdrs are repeated
1920 * thoughout the stream at suitable decoding start points */
1921 p_stream->b_force_backup = 0;
1923 /* read in useful bits from sequence header */
1924 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1925 bs_skip( &bs, 13*8); /* parse_info_header */
1926 dirac_uint( &bs ); /* major_version */
1927 dirac_uint( &bs ); /* minor_version */
1928 dirac_uint( &bs ); /* profile */
1929 dirac_uint( &bs ); /* level */
1931 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1932 if( u_video_format >= u_dirac_vidfmt_frate )
1934 /* don't know how to parse this ogg dirac stream */
1938 if( dirac_bool( &bs ) )
1940 dirac_uint( &bs ); /* frame_width */
1941 dirac_uint( &bs ); /* frame_height */
1944 if( dirac_bool( &bs ) )
1946 dirac_uint( &bs ); /* chroma_format */
1949 if( dirac_bool( &bs ) )
1951 dirac_uint( &bs ); /* scan_format */
1954 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
1955 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
1956 if( dirac_bool( &bs ) )
1958 uint32_t u_frame_rate_index = dirac_uint( &bs );
1959 if( u_frame_rate_index >= u_dirac_frate_tbl )
1961 /* something is wrong with this stream */
1964 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
1965 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
1966 if( u_frame_rate_index == 0 )
1968 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1969 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1972 p_stream->f_rate = (float) u_n / u_d;
1974 /* probably is an ogg dirac es */
1975 p_stream->fmt.i_cat = VIDEO_ES;
1976 p_stream->fmt.i_codec = VLC_FOURCC( 'd','r','a','c' );