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>
44 #include "kate_categories.h"
48 /*****************************************************************************
50 *****************************************************************************/
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
55 set_shortname ( "OGG" )
56 set_description( N_("OGG demuxer" ) )
57 set_category( CAT_INPUT )
58 set_subcategory( SUBCAT_INPUT_DEMUX )
59 set_capability( "demux", 50 )
60 set_callbacks( Open, Close )
65 /*****************************************************************************
66 * Definitions of structures and functions used by this plugins
67 *****************************************************************************/
69 /* OggDS headers for the new header format (used in ogm files) */
74 } stream_header_video_t;
80 ogg_int16_t blockalign;
81 ogg_int32_t avgbytespersec;
82 } stream_header_audio_t;
89 ogg_int32_t size; /* size of the structure */
91 ogg_int64_t time_unit; /* in reference time */
92 ogg_int64_t samples_per_unit;
93 ogg_int32_t default_len; /* in media time */
95 ogg_int32_t buffersize;
96 ogg_int16_t bits_per_sample;
102 stream_header_video_t video;
104 stream_header_audio_t audio;
109 /* Some defines from OggDS */
110 #define PACKET_TYPE_HEADER 0x01
111 #define PACKET_TYPE_BITS 0x07
112 #define PACKET_LEN_BITS01 0xc0
113 #define PACKET_LEN_BITS2 0x02
114 #define PACKET_IS_SYNCPOINT 0x08
116 /*****************************************************************************
118 *****************************************************************************/
119 static int Demux ( demux_t * );
120 static int Control( demux_t *, int, va_list );
122 /* Bitstream manipulation */
123 static int Ogg_ReadPage ( demux_t *, ogg_page * );
124 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
125 static void Ogg_DecodePacket ( demux_t *, logical_stream_t *, ogg_packet * );
127 static int Ogg_BeginningOfStream( demux_t *p_demux );
128 static int Ogg_FindLogicalStreams( demux_t *p_demux );
129 static void Ogg_EndOfStream( demux_t *p_demux );
132 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
133 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
136 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers );
138 /* Logical bitstream headers */
139 static void Ogg_ReadTheoraHeader( logical_stream_t *, ogg_packet * );
140 static void Ogg_ReadVorbisHeader( logical_stream_t *, ogg_packet * );
141 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
142 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
143 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
144 static void Ogg_ReadAnnodexHeader( vlc_object_t *, logical_stream_t *, ogg_packet * );
145 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
147 /*****************************************************************************
148 * Open: initializes ogg demux structures
149 *****************************************************************************/
150 static int Open( vlc_object_t * p_this )
152 demux_t *p_demux = (demux_t *)p_this;
154 const uint8_t *p_peek;
157 /* Check if we are dealing with an ogg stream */
158 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
159 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
164 /* Set exported functions */
165 p_demux->pf_demux = Demux;
166 p_demux->pf_control = Control;
167 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
171 memset( p_sys, 0, sizeof( demux_sys_t ) );
172 p_sys->i_bitrate = 0;
173 p_sys->pp_stream = NULL;
174 p_sys->p_old_stream = NULL;
176 /* Begnning of stream, tell the demux to look for elementary streams. */
180 /* Initialize the Ogg physical bitstream parser */
181 ogg_sync_init( &p_sys->oy );
182 p_sys->b_page_waiting = false;
185 p_sys->p_meta = NULL;
190 /*****************************************************************************
191 * Close: frees unused data
192 *****************************************************************************/
193 static void Close( vlc_object_t *p_this )
195 demux_t *p_demux = (demux_t *)p_this;
196 demux_sys_t *p_sys = p_demux->p_sys ;
198 /* Cleanup the bitstream parser */
199 ogg_sync_clear( &p_sys->oy );
201 Ogg_EndOfStream( p_demux );
203 if( p_sys->p_old_stream )
204 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
209 /*****************************************************************************
210 * Demux: reads and demuxes data packets
211 *****************************************************************************
212 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
213 *****************************************************************************/
214 static int Demux( demux_t * p_demux )
216 demux_sys_t *p_sys = p_demux->p_sys;
217 ogg_packet oggpacket;
219 bool b_skipping = false;
222 if( p_sys->i_eos == p_sys->i_streams )
226 msg_Dbg( p_demux, "end of a group of logical streams" );
227 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
228 * only 1 ES is supported (common case for ogg web radio) */
229 if( p_sys->i_streams == 1 )
231 p_sys->p_old_stream = p_sys->pp_stream[0];
232 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
234 Ogg_EndOfStream( p_demux );
238 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
241 msg_Dbg( p_demux, "beginning of a group of logical streams" );
242 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
246 * The first data page of a physical stream is stored in the relevant logical stream
247 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
248 * stream it belongs to if we haven't processed this first page yet. If we do, we
249 * will only process that first page whenever we find the second page for this stream.
250 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
251 * the second page will arrive real quick, this is not fine for Kate, whose second
252 * data page will typically arrive much later.
253 * This means it is now possible to seek right at the start of a stream where the last
254 * logical stream is Kate, without having to wait for the second data page to unblock
255 * the first one, which is the one that triggers the 'no more headers to backup' code.
256 * And, as we all know, seeking without having backed up all headers is bad, since the
257 * codec will fail to initialize if it's missing its headers.
259 if( !p_sys->b_page_waiting)
262 * Demux an ogg page from the stream
264 if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
267 /* Test for End of Stream */
268 if( ogg_page_eos( &p_sys->current_page ) )
273 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
275 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
277 /* if we've just pulled page, look for the right logical stream */
278 if( !p_sys->b_page_waiting )
280 if( p_sys->i_streams == 1 &&
281 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
283 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
284 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
286 p_stream->b_reinit = true;
287 p_stream->i_pcr = -1;
288 p_stream->i_interpolated_pcr = -1;
289 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
292 if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
299 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
301 /* Read info from any secondary header packets, if there are any */
302 if( p_stream->i_secondary_header_packets > 0 )
304 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
305 oggpacket.bytes >= 7 &&
306 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
308 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
309 p_stream->i_secondary_header_packets = 0;
311 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
312 oggpacket.bytes >= 7 &&
313 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
315 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
316 p_stream->i_secondary_header_packets = 0;
318 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
320 p_stream->i_secondary_header_packets = 0;
323 /* update start of data pointer */
324 p_stream->i_data_start = stream_Tell( p_demux->s );
328 /* If any streams have i_skip_frames, only decode (pre-roll)
329 * for those streams */
330 if ( b_skipping && p_stream->i_skip_frames == 0 ) continue;
333 if( p_stream->b_reinit )
335 /* If synchro is re-initialized we need to drop all the packets
336 * until we find a new dated one. */
337 Ogg_UpdatePCR( p_stream, &oggpacket );
339 if( p_stream->i_pcr >= 0 )
341 p_stream->b_reinit = false;
345 p_stream->i_interpolated_pcr = -1;
349 /* An Ogg/vorbis packet contains an end date granulepos */
350 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
351 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
352 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
354 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
356 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
360 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
361 VLC_TS_0 + p_stream->i_pcr );
367 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
370 if( !p_sys->b_page_waiting )
374 /* if a page was waiting, it's now processed */
375 p_sys->b_page_waiting = false;
378 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
380 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
382 if( p_stream->fmt.i_cat == SPU_ES )
384 if( p_stream->i_interpolated_pcr < 0 )
387 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
388 p_sys->i_pcr = p_stream->i_interpolated_pcr;
391 if( p_sys->i_pcr >= 0 && ! b_skipping )
392 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
397 /*****************************************************************************
399 *****************************************************************************/
400 static int Control( demux_t *p_demux, int i_query, va_list args )
402 demux_sys_t *p_sys = p_demux->p_sys;
411 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
413 vlc_meta_Merge( p_meta, p_sys->p_meta );
416 case DEMUX_HAS_UNSUPPORTED_META:
417 pb_bool = (bool*)va_arg( args, bool* );
422 pi64 = (int64_t*)va_arg( args, int64_t * );
423 *pi64 = p_sys->i_pcr;
429 case DEMUX_GET_ATTACHMENTS:
431 input_attachment_t ***ppp_attach =
432 (input_attachment_t***)va_arg( args, input_attachment_t*** );
433 int *pi_int = (int*)va_arg( args, int * );
435 if( p_sys->i_attachments <= 0 )
438 *pi_int = p_sys->i_attachments;
439 *ppp_attach = xmalloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
440 for( int i = 0; i < p_sys->i_attachments; i++ )
441 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
445 case DEMUX_SET_POSITION:
446 /* forbid seeking if we haven't initialized all logical bitstreams yet;
447 if we allowed, some headers would not get backed up and decoder init
448 would fail, making that logical stream unusable */
449 if( p_sys->i_bos > 0 )
454 for( i = 0; i < p_sys->i_streams; i++ )
456 logical_stream_t *p_stream = p_sys->pp_stream[i];
458 /* we'll trash all the data until we find the next pcr */
459 p_stream->b_reinit = true;
460 p_stream->i_pcr = -1;
461 p_stream->i_interpolated_pcr = -1;
462 ogg_stream_reset( &p_stream->os );
464 ogg_sync_reset( &p_sys->oy );
465 /* XXX The break/return is missing on purpose as
466 * demux_vaControlHelper will do the last part of the job */
469 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
474 /****************************************************************************
475 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
476 ****************************************************************************
477 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
478 * are at the end of stream.
479 ****************************************************************************/
480 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
482 demux_sys_t *p_ogg = p_demux->p_sys ;
486 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
488 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
490 i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
494 ogg_sync_wrote( &p_ogg->oy, i_read );
500 /****************************************************************************
501 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
503 ****************************************************************************/
504 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
505 ogg_packet *p_oggpacket )
507 /* Convert the granulepos into a pcr */
508 if( p_oggpacket->granulepos >= 0 )
510 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
511 p_stream->fmt.i_codec == VLC_CODEC_KATE )
513 ogg_int64_t iframe = p_oggpacket->granulepos >>
514 p_stream->i_granule_shift;
515 ogg_int64_t pframe = p_oggpacket->granulepos -
516 ( iframe << p_stream->i_granule_shift );
518 p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
519 * INT64_C(1000000) / p_stream->f_rate;
521 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
523 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
524 /* NB, OggDirac granulepos values are in units of 2*picturerate */
525 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
529 p_stream->i_pcr = p_oggpacket->granulepos * INT64_C(1000000)
533 p_stream->i_pcr += 1;
534 p_stream->i_interpolated_pcr = p_stream->i_pcr;
538 p_stream->i_pcr = -1;
540 /* no granulepos available, try to interpolate the pcr.
541 * If we can't then don't touch the old value. */
542 if( p_stream->fmt.i_cat == VIDEO_ES )
543 /* 1 frame per packet */
544 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
545 else if( p_stream->fmt.i_bitrate )
546 p_stream->i_interpolated_pcr +=
547 ( p_oggpacket->bytes * INT64_C(1000000) /
548 p_stream->fmt.i_bitrate / 8 );
552 /****************************************************************************
553 * Ogg_DecodePacket: Decode an Ogg packet.
554 ****************************************************************************/
555 static void Ogg_DecodePacket( demux_t *p_demux,
556 logical_stream_t *p_stream,
557 ogg_packet *p_oggpacket )
561 int i_header_len = 0;
562 mtime_t i_pts = -1, i_interpolated_pts;
563 demux_sys_t *p_ogg = p_demux->p_sys;
566 if( !p_oggpacket->bytes )
568 msg_Dbg( p_demux, "discarding 0 sized packet" );
572 if( p_oggpacket->bytes >= 7 &&
573 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
575 /* it's an Annodex packet -- skip it (do nothing) */
578 else if( p_oggpacket->bytes >= 7 &&
579 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
581 /* it's an AnxData packet -- skip it (do nothing) */
585 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT &&
586 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
588 /* Check the ES is selected */
589 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
590 p_stream->p_es, &b_selected );
592 if( p_stream->b_force_backup )
595 p_stream->i_packets_backup++;
596 switch( p_stream->fmt.i_codec )
598 case VLC_CODEC_VORBIS:
599 case VLC_CODEC_SPEEX:
600 case VLC_CODEC_THEORA:
601 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
606 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
608 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
609 p_stream->b_force_backup = 0;
611 else if( p_stream->fmt.audio.i_rate )
613 p_stream->b_force_backup = 0;
614 if( p_oggpacket->bytes >= 9 )
616 p_oggpacket->packet += 9;
617 p_oggpacket->bytes -= 9;
624 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
629 p_stream->b_force_backup = 0;
634 /* Backup the ogg packet (likely an header packet) */
637 void *p_org = p_stream->p_headers;
638 p_stream->i_headers += p_oggpacket->bytes;
639 p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
640 if( p_stream->p_headers )
642 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
643 p_oggpacket->packet, p_oggpacket->bytes );
648 p_stream->i_headers = 0;
649 p_stream->p_headers = NULL;
653 else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
654 p_oggpacket->bytes, p_oggpacket->packet ) )
656 p_stream->i_headers = 0;
657 p_stream->p_headers = NULL;
659 if( p_stream->i_headers > 0 )
661 if( !p_stream->b_force_backup )
663 /* Last header received, commit changes */
664 free( p_stream->fmt.p_extra );
666 p_stream->fmt.i_extra = p_stream->i_headers;
667 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
668 if( p_stream->fmt.p_extra )
669 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
670 p_stream->i_headers );
672 p_stream->fmt.i_extra = 0;
674 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
675 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
676 p_stream->p_es, &p_stream->fmt );
678 if( p_stream->i_headers > 0 )
679 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
680 p_stream->p_headers, p_stream->i_headers );
682 /* we're not at BOS anymore for this logical stream */
687 b_selected = false; /* Discard the header packet */
690 /* Convert the pcr into a pts */
691 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
692 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
693 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
695 if( p_stream->i_pcr >= 0 )
697 /* This is for streams where the granulepos of the header packets
698 * doesn't match these of the data packets (eg. ogg web radios). */
699 if( p_stream->i_previous_pcr == 0 &&
700 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
702 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
704 /* Call the pace control */
705 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
706 VLC_TS_0 + p_stream->i_pcr );
709 p_stream->i_previous_pcr = p_stream->i_pcr;
711 /* The granulepos is the end date of the sample */
712 i_pts = p_stream->i_pcr;
716 /* Convert the granulepos into the next pcr */
717 i_interpolated_pts = p_stream->i_interpolated_pcr;
718 Ogg_UpdatePCR( p_stream, p_oggpacket );
720 /* SPU streams are typically discontinuous, do not mind large gaps */
721 if( p_stream->fmt.i_cat != SPU_ES )
723 if( p_stream->i_pcr >= 0 )
725 /* This is for streams where the granulepos of the header packets
726 * doesn't match these of the data packets (eg. ogg web radios). */
727 if( p_stream->i_previous_pcr == 0 &&
728 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
730 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
732 /* Call the pace control */
733 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_stream->i_pcr );
738 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
739 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
740 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
741 p_stream->i_pcr >= 0 )
743 p_stream->i_previous_pcr = p_stream->i_pcr;
745 /* The granulepos is the start date of the sample */
746 i_pts = p_stream->i_pcr;
751 /* This stream isn't currently selected so we don't need to decode it,
752 * but we did need to store its pcr as it might be selected later on */
756 if( p_oggpacket->bytes <= 0 )
759 if( !( p_block = block_New( p_demux, p_oggpacket->bytes ) ) ) return;
762 /* may need to preroll video frames after a seek */
763 if ( p_stream->i_skip_frames > 0 )
765 p_block->i_flags |= BLOCK_FLAG_PREROLL;
766 p_stream->i_skip_frames--;
771 if( i_pts == 0 ) i_pts = VLC_TS_0;
772 else if( i_pts == -1 && i_interpolated_pts == 0 ) i_pts = VLC_TS_0;
773 else if( i_pts == -1 ) i_pts = VLC_TS_INVALID;
775 if( p_stream->fmt.i_cat == AUDIO_ES )
776 p_block->i_dts = p_block->i_pts = i_pts;
777 else if( p_stream->fmt.i_cat == SPU_ES )
779 p_block->i_dts = p_block->i_pts = i_pts;
780 p_block->i_length = 0;
782 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
784 p_block->i_dts = p_block->i_pts = i_pts;
785 if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
787 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
790 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
792 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
793 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
795 uint64_t u_pnum = dts + delay;
797 p_block->i_dts = p_stream->i_pcr;
798 p_block->i_pts = VLC_TS_INVALID;
799 /* NB, OggDirac granulepos values are in units of 2*picturerate */
801 /* granulepos for dirac is possibly broken, this value should be ignored */
802 if( -1 != p_oggpacket->granulepos )
803 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
807 p_block->i_dts = i_pts;
808 p_block->i_pts = VLC_TS_INVALID;
811 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
812 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
813 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
814 p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
815 p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
816 p_stream->fmt.i_codec != VLC_CODEC_CMML &&
817 p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
818 p_stream->fmt.i_codec != VLC_CODEC_KATE )
820 /* We remove the header from the packet */
821 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
822 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
824 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
826 /* But with subtitles we need to retrieve the duration first */
829 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
831 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
833 lenbytes = lenbytes << 8;
834 lenbytes += *(p_oggpacket->packet + i_header_len - i);
837 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
838 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
839 p_oggpacket->packet[i_header_len + 1] != 0 &&
840 p_oggpacket->packet[i_header_len + 1] != '\n' &&
841 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
843 p_block->i_length = (mtime_t)lenbytes * 1000;
848 if( p_block->i_buffer >= (unsigned int)i_header_len )
849 p_block->i_buffer -= i_header_len;
851 p_block->i_buffer = 0;
854 if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
856 /* FIXME: the biggest hack I've ever done */
857 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
858 p_block->i_pts, p_block->i_dts );
862 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
863 p_oggpacket->bytes - i_header_len );
865 es_out_Send( p_demux->out, p_stream->p_es, p_block );
868 /****************************************************************************
869 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
870 * stream and fill p_ogg.
871 *****************************************************************************
872 * The initial page of a logical stream is marked as a 'bos' page.
873 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
874 * together and all of the initial pages must appear before any data pages.
876 * On success this function returns VLC_SUCCESS.
877 ****************************************************************************/
878 static int Ogg_FindLogicalStreams( demux_t *p_demux )
880 demux_sys_t *p_ogg = p_demux->p_sys ;
881 ogg_packet oggpacket;
884 p_ogg->i_total_length = stream_Size ( p_demux->s );
885 msg_Dbg( p_demux, "File length is %"PRId64" bytes", p_ogg->i_total_length );
888 while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
891 if( ogg_page_bos( &p_ogg->current_page ) )
894 /* All is wonderful in our fine fine little world.
895 * We found the beginning of our first logical stream. */
896 while( ogg_page_bos( &p_ogg->current_page ) )
898 logical_stream_t *p_stream;
900 p_stream = malloc( sizeof(logical_stream_t) );
904 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
906 memset( p_stream, 0, sizeof(logical_stream_t) );
907 p_stream->p_headers = 0;
908 p_stream->i_secondary_header_packets = 0;
910 p_stream->i_keyframe_offset = 0;
911 p_stream->i_skip_frames = 0;
913 p_stream->i_data_start = 0;
915 es_format_Init( &p_stream->fmt, 0, 0 );
916 es_format_Init( &p_stream->fmt_old, 0, 0 );
918 /* Setup the logical stream */
919 p_stream->i_serial_no = ogg_page_serialno( &p_ogg->current_page );
920 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
922 /* Extract the initial header from the first page and verify
923 * the codec type of this Ogg bitstream */
924 if( ogg_stream_pagein( &p_stream->os, &p_ogg->current_page ) < 0 )
926 /* error. stream version mismatch perhaps */
927 msg_Err( p_demux, "error reading first page of "
928 "Ogg bitstream data" );
932 /* FIXME: check return value */
933 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
935 /* Check for Vorbis header */
936 if( oggpacket.bytes >= 7 &&
937 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
939 Ogg_ReadVorbisHeader( p_stream, &oggpacket );
940 msg_Dbg( p_demux, "found vorbis header" );
942 /* Check for Speex header */
943 else if( oggpacket.bytes >= 5 &&
944 ! memcmp( oggpacket.packet, "Speex", 5 ) )
946 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
947 msg_Dbg( p_demux, "found speex header, channels: %i, "
948 "rate: %i, bitrate: %i",
949 p_stream->fmt.audio.i_channels,
950 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
952 /* Check for Flac header (< version 1.1.1) */
953 else if( oggpacket.bytes >= 4 &&
954 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
956 msg_Dbg( p_demux, "found FLAC header" );
958 /* Grrrr!!!! Did they really have to put all the
959 * important info in the second header packet!!!
960 * (STREAMINFO metadata is in the following packet) */
961 p_stream->b_force_backup = 1;
963 p_stream->fmt.i_cat = AUDIO_ES;
964 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
966 /* Check for Flac header (>= version 1.1.1) */
967 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
968 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
969 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
971 int i_packets = ((int)oggpacket.packet[7]) << 8 |
973 msg_Dbg( p_demux, "found FLAC header version %i.%i "
974 "(%i header packets)",
975 oggpacket.packet[5], oggpacket.packet[6],
978 p_stream->b_force_backup = 1;
980 p_stream->fmt.i_cat = AUDIO_ES;
981 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
982 oggpacket.packet += 13; oggpacket.bytes -= 13;
983 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
985 /* Check for Theora header */
986 else if( oggpacket.bytes >= 7 &&
987 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
989 Ogg_ReadTheoraHeader( p_stream, &oggpacket );
992 "found theora header, bitrate: %i, rate: %f",
993 p_stream->fmt.i_bitrate, p_stream->f_rate );
995 /* Check for Dirac header */
996 else if( ( oggpacket.bytes >= 5 &&
997 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
998 ( oggpacket.bytes >= 9 &&
999 ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
1001 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1002 msg_Dbg( p_demux, "found dirac header" );
1005 msg_Warn( p_demux, "found dirac header isn't decodable" );
1010 /* Check for Tarkin header */
1011 else if( oggpacket.bytes >= 7 &&
1012 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1016 msg_Dbg( p_demux, "found tarkin header" );
1017 p_stream->fmt.i_cat = VIDEO_ES;
1018 p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1020 /* Cheat and get additionnal info ;) */
1021 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1022 oggpack_adv( &opb, 88 );
1023 oggpack_adv( &opb, 104 );
1024 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1025 p_stream->f_rate = 2; /* FIXME */
1027 "found tarkin header, bitrate: %i, rate: %f",
1028 p_stream->fmt.i_bitrate, p_stream->f_rate );
1030 /* Check for Annodex header */
1031 else if( oggpacket.bytes >= 7 &&
1032 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1034 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1036 /* kill annodex track */
1040 /* Check for Annodex header */
1041 else if( oggpacket.bytes >= 7 &&
1042 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1044 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux), p_stream,
1047 /* Check for Kate header */
1048 else if( oggpacket.bytes >= 8 &&
1049 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1051 Ogg_ReadKateHeader( p_stream, &oggpacket );
1052 msg_Dbg( p_demux, "found kate header" );
1054 else if( oggpacket.bytes >= 142 &&
1055 !memcmp( &oggpacket.packet[1],
1056 "Direct Show Samples embedded in Ogg", 35 ))
1058 /* Old header type */
1060 /* Check for video header (old format) */
1061 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1062 oggpacket.bytes >= 184 )
1064 p_stream->fmt.i_cat = VIDEO_ES;
1065 p_stream->fmt.i_codec =
1066 VLC_FOURCC( oggpacket.packet[68],
1067 oggpacket.packet[69],
1068 oggpacket.packet[70],
1069 oggpacket.packet[71] );
1070 msg_Dbg( p_demux, "found video header of type: %.4s",
1071 (char *)&p_stream->fmt.i_codec );
1073 p_stream->fmt.video.i_frame_rate = 10000000;
1074 p_stream->fmt.video.i_frame_rate_base =
1075 GetQWLE((oggpacket.packet+164));
1076 p_stream->f_rate = 10000000.0 /
1077 GetQWLE((oggpacket.packet+164));
1078 p_stream->fmt.video.i_bits_per_pixel =
1079 GetWLE((oggpacket.packet+182));
1080 if( !p_stream->fmt.video.i_bits_per_pixel )
1082 p_stream->fmt.video.i_bits_per_pixel = 24;
1083 p_stream->fmt.video.i_width =
1084 GetDWLE((oggpacket.packet+176));
1085 p_stream->fmt.video.i_height =
1086 GetDWLE((oggpacket.packet+180));
1089 "fps: %f, width:%i; height:%i, bitcount:%i",
1091 p_stream->fmt.video.i_width,
1092 p_stream->fmt.video.i_height,
1093 p_stream->fmt.video.i_bits_per_pixel);
1096 /* Check for audio header (old format) */
1097 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1100 unsigned int i_format_tag;
1102 p_stream->fmt.i_cat = AUDIO_ES;
1104 i_extra_size = GetWLE((oggpacket.packet+140));
1105 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1107 p_stream->fmt.i_extra = i_extra_size;
1108 p_stream->fmt.p_extra = malloc( i_extra_size );
1109 if( p_stream->fmt.p_extra )
1110 memcpy( p_stream->fmt.p_extra,
1111 oggpacket.packet + 142, i_extra_size );
1113 p_stream->fmt.i_extra = 0;
1116 i_format_tag = GetWLE((oggpacket.packet+124));
1117 p_stream->fmt.audio.i_channels =
1118 GetWLE((oggpacket.packet+126));
1119 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1120 GetDWLE((oggpacket.packet+128));
1121 p_stream->fmt.i_bitrate =
1122 GetDWLE((oggpacket.packet+132)) * 8;
1123 p_stream->fmt.audio.i_blockalign =
1124 GetWLE((oggpacket.packet+136));
1125 p_stream->fmt.audio.i_bitspersample =
1126 GetWLE((oggpacket.packet+138));
1128 wf_tag_to_fourcc( i_format_tag,
1129 &p_stream->fmt.i_codec, 0 );
1131 if( p_stream->fmt.i_codec ==
1132 VLC_FOURCC('u','n','d','f') )
1134 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1135 ( i_format_tag >> 8 ) & 0xff,
1136 i_format_tag & 0xff );
1139 msg_Dbg( p_demux, "found audio header of type: %.4s",
1140 (char *)&p_stream->fmt.i_codec );
1141 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1142 "%dbits/sample %dkb/s",
1144 p_stream->fmt.audio.i_channels,
1145 p_stream->fmt.audio.i_rate,
1146 p_stream->fmt.audio.i_bitspersample,
1147 p_stream->fmt.i_bitrate / 1024 );
1152 msg_Dbg( p_demux, "stream %d has an old header "
1153 "but is of an unknown type", p_ogg->i_streams-1 );
1158 else if( (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER &&
1159 oggpacket.bytes >= 44+1 )
1161 stream_header_t tmp;
1162 stream_header_t *st = &tmp;
1164 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1165 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1166 st->size = GetDWLE( &oggpacket.packet[1+12] );
1167 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1168 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1169 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1170 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1171 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1173 /* Check for video header (new format) */
1174 if( !strncmp( st->streamtype, "video", 5 ) &&
1175 oggpacket.bytes >= 52+1 )
1177 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1178 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1180 p_stream->fmt.i_cat = VIDEO_ES;
1182 /* We need to get rid of the header packet */
1183 ogg_stream_packetout( &p_stream->os, &oggpacket );
1185 p_stream->fmt.i_codec =
1186 VLC_FOURCC( st->subtype[0], st->subtype[1],
1187 st->subtype[2], st->subtype[3] );
1188 msg_Dbg( p_demux, "found video header of type: %.4s",
1189 (char *)&p_stream->fmt.i_codec );
1191 p_stream->fmt.video.i_frame_rate = 10000000;
1192 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1193 if( st->time_unit <= 0 )
1194 st->time_unit = 400000;
1195 p_stream->f_rate = 10000000.0 / st->time_unit;
1196 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1197 p_stream->fmt.video.i_width = st->sh.video.width;
1198 p_stream->fmt.video.i_height = st->sh.video.height;
1201 "fps: %f, width:%i; height:%i, bitcount:%i",
1203 p_stream->fmt.video.i_width,
1204 p_stream->fmt.video.i_height,
1205 p_stream->fmt.video.i_bits_per_pixel );
1207 /* Check for audio header (new format) */
1208 else if( !strncmp( st->streamtype, "audio", 5 ) &&
1209 oggpacket.bytes >= 56+1 )
1215 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1216 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1217 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1219 p_stream->fmt.i_cat = AUDIO_ES;
1221 /* We need to get rid of the header packet */
1222 ogg_stream_packetout( &p_stream->os, &oggpacket );
1224 i_extra_size = st->size - 56;
1226 if( i_extra_size > 0 &&
1227 i_extra_size < oggpacket.bytes - 1 - 56 )
1229 p_stream->fmt.i_extra = i_extra_size;
1230 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1231 if( p_stream->fmt.p_extra )
1232 memcpy( p_stream->fmt.p_extra, st + 1,
1233 p_stream->fmt.i_extra );
1235 p_stream->fmt.i_extra = 0;
1238 memcpy( p_buffer, st->subtype, 4 );
1240 i_format_tag = strtol(p_buffer,NULL,16);
1241 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1242 if( st->time_unit <= 0 )
1243 st->time_unit = 10000000;
1244 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1245 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1246 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1247 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1249 wf_tag_to_fourcc( i_format_tag,
1250 &p_stream->fmt.i_codec, 0 );
1252 if( p_stream->fmt.i_codec ==
1253 VLC_FOURCC('u','n','d','f') )
1255 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1256 ( i_format_tag >> 8 ) & 0xff,
1257 i_format_tag & 0xff );
1260 msg_Dbg( p_demux, "found audio header of type: %.4s",
1261 (char *)&p_stream->fmt.i_codec );
1262 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1263 "%dbits/sample %dkb/s",
1265 p_stream->fmt.audio.i_channels,
1266 p_stream->fmt.audio.i_rate,
1267 p_stream->fmt.audio.i_bitspersample,
1268 p_stream->fmt.i_bitrate / 1024 );
1270 /* Check for text (subtitles) header */
1271 else if( !strncmp(st->streamtype, "text", 4) )
1273 /* We need to get rid of the header packet */
1274 ogg_stream_packetout( &p_stream->os, &oggpacket );
1276 msg_Dbg( p_demux, "found text subtitles header" );
1277 p_stream->fmt.i_cat = SPU_ES;
1278 p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1279 p_stream->f_rate = 1000; /* granulepos is in millisec */
1283 msg_Dbg( p_demux, "stream %d has a header marker "
1284 "but is of an unknown type", p_ogg->i_streams-1 );
1289 else if( oggpacket.bytes >= 7 &&
1290 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1294 msg_Dbg( p_demux, "stream %d is a skeleton",
1295 p_ogg->i_streams-1 );
1296 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1300 msg_Dbg( p_demux, "stream %d is of unknown type",
1301 p_ogg->i_streams-1 );
1306 if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
1307 return VLC_EGENERIC;
1310 /* we'll need to get all headers for all of those streams
1311 that we have to backup headers for */
1313 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1315 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1320 /* This is the first data page, which means we are now finished
1321 * with the initial pages. We just need to store it in the relevant
1323 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1325 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1326 &p_ogg->current_page ) == 0 )
1328 p_ogg->b_page_waiting = true;
1337 return VLC_EGENERIC;
1340 /****************************************************************************
1341 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1342 * Elementary streams.
1343 ****************************************************************************/
1344 static int Ogg_BeginningOfStream( demux_t *p_demux )
1346 demux_sys_t *p_ogg = p_demux->p_sys ;
1347 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1350 /* Find the logical streams embedded in the physical stream and
1351 * initialize our p_ogg structure. */
1352 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1354 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1355 return VLC_EGENERIC;
1358 p_ogg->i_bitrate = 0;
1360 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1362 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1364 p_stream->p_es = NULL;
1366 /* initialise kframe index */
1369 /* Try first to reuse an old ES */
1371 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1372 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1374 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1376 p_stream->p_es = p_old_stream->p_es;
1377 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1379 p_old_stream->p_es = NULL;
1380 p_old_stream = NULL;
1383 if( !p_stream->p_es )
1385 /* Better be safe than sorry when possible with ogm */
1386 if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1387 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1388 p_stream->fmt.b_packetized = false;
1390 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1393 // TODO: something to do here ?
1394 if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1396 /* Set the CMML stream active */
1397 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1400 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1402 p_stream->i_pcr = p_stream->i_previous_pcr =
1403 p_stream->i_interpolated_pcr = -1;
1404 p_stream->b_reinit = false;
1407 if( p_ogg->p_old_stream )
1409 if( p_ogg->p_old_stream->p_es )
1410 msg_Dbg( p_demux, "old stream not reused" );
1411 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1412 p_ogg->p_old_stream = NULL;
1416 /* get total frame count for video stream; we will need this for seeking */
1417 p_ogg->i_total_frames = 0;
1422 /****************************************************************************
1423 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1424 ****************************************************************************/
1425 static void Ogg_EndOfStream( demux_t *p_demux )
1427 demux_sys_t *p_ogg = p_demux->p_sys ;
1430 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1431 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1432 free( p_ogg->pp_stream );
1435 p_ogg->i_bitrate = 0;
1436 p_ogg->i_streams = 0;
1437 p_ogg->pp_stream = NULL;
1441 vlc_meta_Delete( p_ogg->p_meta );
1442 p_ogg->p_meta = NULL;
1446 * This function delete and release all data associated to a logical_stream_t
1448 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1450 if( p_stream->p_es )
1451 es_out_Del( p_demux->out, p_stream->p_es );
1453 ogg_stream_clear( &p_stream->os );
1454 free( p_stream->p_headers );
1456 es_format_Clean( &p_stream->fmt_old );
1457 es_format_Clean( &p_stream->fmt );
1459 if ( p_stream->idx != NULL)
1461 oggseek_index_entries_free( p_stream->idx );
1467 * This function check if a we need to reset a decoder in case we are
1470 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1472 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1473 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1474 unsigned i_new_count;
1475 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1478 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1479 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1480 unsigned i_old_count;
1481 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1484 bool b_match = i_new_count == i_old_count;
1485 for( unsigned i = 0; i < i_new_count && b_match; i++ )
1487 /* Ignore vorbis comment */
1490 if( pi_new_size[i] != pi_old_size[i] ||
1491 memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
1495 for( unsigned i = 0; i < i_new_count; i++ )
1496 free( pp_new_data[i] );
1497 for( unsigned i = 0; i < i_old_count; i++ )
1498 free( pp_old_data[i] );
1501 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1503 bool b_compatible = false;
1504 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1507 /* Only vorbis is supported */
1508 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
1509 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1512 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1514 return !b_compatible;
1516 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const void *p_headers, unsigned i_headers, unsigned i_skip )
1518 demux_sys_t *p_ogg = p_demux->p_sys;
1520 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1521 void *pp_data[XIPH_MAX_HEADER_COUNT];
1523 if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
1526 /* TODO how to handle multiple comments properly ? */
1527 if( i_count >= 2 && pi_size[1] > i_skip )
1528 vorbis_ParseComment( &p_ogg->p_meta, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip,
1529 &p_ogg->i_attachments, &p_ogg->attachments );
1531 for( unsigned i = 0; i < i_count; i++ )
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_CODEC_VORBIS:
1542 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1544 case VLC_CODEC_THEORA:
1545 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1547 case VLC_CODEC_SPEEX:
1548 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
1551 /* N headers with the 2° one being the comments */
1552 case VLC_CODEC_KATE:
1553 /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
1554 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1 );
1558 case VLC_CODEC_FLAC:
1559 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1563 case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1564 case VLC_CODEC_DIRAC:
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;
1584 p_stream->fmt.i_cat = VIDEO_ES;
1585 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1587 /* Signal that we want to keep a backup of the theora
1588 * stream headers. They will be used when switching between
1590 p_stream->b_force_backup = 1;
1592 /* Cheat and get additionnal info ;) */
1593 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1594 bs_skip( &bitstream, 56 );
1596 i_major = bs_read( &bitstream, 8 ); /* major version num */
1597 i_minor = bs_read( &bitstream, 8 ); /* minor version num */
1598 i_subminor = bs_read( &bitstream, 8 ); /* subminor version num */
1600 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1601 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1602 bs_read( &bitstream, 24 ); /* frame width */
1603 bs_read( &bitstream, 24 ); /* frame height */
1604 bs_read( &bitstream, 8 ); /* x offset */
1605 bs_read( &bitstream, 8 ); /* y offset */
1607 i_fps_numerator = bs_read( &bitstream, 32 );
1608 i_fps_denominator = bs_read( &bitstream, 32 );
1609 bs_read( &bitstream, 24 ); /* aspect_numerator */
1610 bs_read( &bitstream, 24 ); /* aspect_denominator */
1612 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1613 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1615 bs_read( &bitstream, 8 ); /* colorspace */
1616 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1617 bs_read( &bitstream, 6 ); /* quality */
1619 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1621 /* granule_shift = i_log( frequency_force -1 ) */
1622 p_stream->i_granule_shift = 0;
1623 i_keyframe_frequency_force--;
1624 while( i_keyframe_frequency_force )
1626 p_stream->i_granule_shift++;
1627 i_keyframe_frequency_force >>= 1;
1630 i_version = i_major * 1000000 + i_minor * 1000 + i_subminor;
1631 p_stream->i_keyframe_offset = 0;
1633 if ( i_version >= 3002001 )
1635 p_stream->i_keyframe_offset = 1;
1638 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1641 static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
1642 ogg_packet *p_oggpacket )
1646 p_stream->fmt.i_cat = AUDIO_ES;
1647 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1649 /* Signal that we want to keep a backup of the vorbis
1650 * stream headers. They will be used when switching between
1652 p_stream->b_force_backup = 1;
1654 /* Cheat and get additionnal info ;) */
1655 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1656 oggpack_adv( &opb, 88 );
1657 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1658 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1659 oggpack_read( &opb, 32 );
1660 oggpack_adv( &opb, 32 );
1661 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1664 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1665 ogg_packet *p_oggpacket )
1669 p_stream->fmt.i_cat = AUDIO_ES;
1670 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1672 /* Signal that we want to keep a backup of the speex
1673 * stream headers. They will be used when switching between
1675 p_stream->b_force_backup = 1;
1677 /* Cheat and get additionnal info ;) */
1678 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1679 oggpack_adv( &opb, 224 );
1680 oggpack_adv( &opb, 32 ); /* speex_version_id */
1681 oggpack_adv( &opb, 32 ); /* header_size */
1682 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
1683 oggpack_adv( &opb, 32 ); /* mode */
1684 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
1685 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
1686 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1689 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
1690 ogg_packet *p_oggpacket )
1692 /* Parse the STREAMINFO metadata */
1695 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
1698 if( bs_read( &s, 7 ) == 0 )
1700 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
1703 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
1704 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
1706 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
1707 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
1711 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
1714 /* Fake this as the last metadata block */
1715 *((uint8_t*)p_oggpacket->packet) |= 0x80;
1719 /* This ain't a STREAMINFO metadata */
1720 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
1724 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
1725 ogg_packet *p_oggpacket )
1733 p_stream->fmt.i_cat = SPU_ES;
1734 p_stream->fmt.i_codec = VLC_CODEC_KATE;
1736 /* Signal that we want to keep a backup of the kate
1737 * stream headers. They will be used when switching between
1739 p_stream->b_force_backup = 1;
1741 /* Cheat and get additionnal info ;) */
1742 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1743 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
1744 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
1745 oggpack_adv( &opb, 3*8 );
1746 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
1747 oggpack_adv( &opb, 8*8 ); /* reserved */
1748 gnum = oggpack_read( &opb, 32 );
1749 gden = oggpack_read( &opb, 32 );
1750 p_stream->f_rate = (double)gnum/gden;
1752 p_stream->fmt.psz_language = malloc(16);
1753 if( p_stream->fmt.psz_language )
1755 for( n = 0; n < 16; n++ )
1756 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
1757 p_stream->fmt.psz_language[15] = 0; /* just in case */
1761 for( n = 0; n < 16; n++ )
1762 oggpack_read(&opb,8);
1764 p_stream->fmt.psz_description = malloc(16);
1765 if( p_stream->fmt.psz_description )
1767 for( n = 0; n < 16; n++ )
1768 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
1769 p_stream->fmt.psz_description[15] = 0; /* just in case */
1771 /* Now find a localized user readable description for this category */
1772 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
1775 free( p_stream->fmt.psz_description );
1776 p_stream->fmt.psz_description = psz_desc;
1781 for( n = 0; n < 16; n++ )
1782 oggpack_read(&opb,8);
1786 static void Ogg_ReadAnnodexHeader( vlc_object_t *p_this,
1787 logical_stream_t *p_stream,
1788 ogg_packet *p_oggpacket )
1790 if( p_oggpacket->bytes >= 28 &&
1791 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
1795 uint16_t major_version;
1796 uint16_t minor_version;
1797 uint64_t timebase_numerator;
1798 uint64_t timebase_denominator;
1800 Ogg_ReadTheoraHeader( p_stream, p_oggpacket );
1802 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1803 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
1804 major_version = oggpack_read( &opb, 2*8 ); /* major version */
1805 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
1806 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
1807 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
1809 else if( p_oggpacket->bytes >= 42 &&
1810 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
1812 uint64_t granule_rate_numerator;
1813 uint64_t granule_rate_denominator;
1814 char content_type_string[1024];
1816 /* Read in Annodex header fields */
1818 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
1819 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
1820 p_stream->i_secondary_header_packets =
1821 GetDWLE( &p_oggpacket->packet[24] );
1823 /* we are guaranteed that the first header field will be
1824 * the content-type (by the Annodex standard) */
1825 content_type_string[0] = '\0';
1826 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
1828 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
1829 p_oggpacket->bytes - 1 );
1830 if( p && p[0] == '\r' && p[1] == '\n' )
1831 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
1832 content_type_string );
1835 msg_Dbg( p_this, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
1836 granule_rate_numerator, granule_rate_denominator,
1837 p_stream->i_secondary_header_packets, content_type_string );
1839 p_stream->f_rate = (float) granule_rate_numerator /
1840 (float) granule_rate_denominator;
1842 /* What type of file do we have?
1843 * strcmp is safe to use here because we've extracted
1844 * content_type_string from the stream manually */
1845 if( !strncmp(content_type_string, "audio/x-wav", 11) )
1847 /* n.b. WAVs are unsupported right now */
1848 p_stream->fmt.i_cat = UNKNOWN_ES;
1850 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
1852 p_stream->fmt.i_cat = AUDIO_ES;
1853 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1855 p_stream->b_force_backup = 1;
1857 else if( !strncmp(content_type_string, "audio/x-speex", 13) )
1859 p_stream->fmt.i_cat = AUDIO_ES;
1860 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
1862 p_stream->b_force_backup = 1;
1864 else if( !strncmp(content_type_string, "video/x-theora", 14) )
1866 p_stream->fmt.i_cat = VIDEO_ES;
1867 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1869 p_stream->b_force_backup = 1;
1871 else if( !strncmp(content_type_string, "video/x-xvid", 12) )
1873 p_stream->fmt.i_cat = VIDEO_ES;
1874 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
1876 p_stream->b_force_backup = 1;
1878 else if( !strncmp(content_type_string, "video/mpeg", 10) )
1880 /* n.b. MPEG streams are unsupported right now */
1881 p_stream->fmt.i_cat = VIDEO_ES;
1882 p_stream->fmt.i_codec = VLC_CODEC_MPGV;
1884 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
1886 ogg_stream_packetout( &p_stream->os, p_oggpacket );
1887 p_stream->fmt.i_cat = SPU_ES;
1888 p_stream->fmt.i_codec = VLC_CODEC_CMML;
1893 static uint32_t dirac_uint( bs_t *p_bs )
1895 uint32_t u_count = 0, u_value = 0;
1897 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
1901 u_value |= bs_read( p_bs, 1 );
1904 return (1<<u_count) - 1 + u_value;
1907 static int dirac_bool( bs_t *p_bs )
1909 return bs_read( p_bs, 1 );
1912 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
1913 ogg_packet *p_oggpacket )
1915 static const struct {
1916 uint32_t u_n /* numerator */, u_d /* denominator */;
1917 } p_dirac_frate_tbl[] = { /* table 10.3 */
1918 {1,1}, /* this first value is never used */
1919 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1920 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1922 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
1924 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
1925 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1927 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
1931 p_stream->i_granule_shift = 22; /* not 32 */
1933 /* Backing up stream headers is not required -- seqhdrs are repeated
1934 * thoughout the stream at suitable decoding start points */
1935 p_stream->b_force_backup = 0;
1937 /* read in useful bits from sequence header */
1938 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
1939 bs_skip( &bs, 13*8); /* parse_info_header */
1940 dirac_uint( &bs ); /* major_version */
1941 dirac_uint( &bs ); /* minor_version */
1942 dirac_uint( &bs ); /* profile */
1943 dirac_uint( &bs ); /* level */
1945 uint32_t u_video_format = dirac_uint( &bs ); /* index */
1946 if( u_video_format >= u_dirac_vidfmt_frate )
1948 /* don't know how to parse this ogg dirac stream */
1952 if( dirac_bool( &bs ) )
1954 dirac_uint( &bs ); /* frame_width */
1955 dirac_uint( &bs ); /* frame_height */
1958 if( dirac_bool( &bs ) )
1960 dirac_uint( &bs ); /* chroma_format */
1963 if( dirac_bool( &bs ) )
1965 dirac_uint( &bs ); /* scan_format */
1968 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
1969 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
1970 if( dirac_bool( &bs ) )
1972 uint32_t u_frame_rate_index = dirac_uint( &bs );
1973 if( u_frame_rate_index >= u_dirac_frate_tbl )
1975 /* something is wrong with this stream */
1978 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
1979 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
1980 if( u_frame_rate_index == 0 )
1982 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
1983 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
1986 p_stream->f_rate = (float) u_n / u_d;
1988 /* probably is an ogg dirac es */
1989 p_stream->fmt.i_cat = VIDEO_ES;
1990 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;