1 /*****************************************************************************
2 * ogg.c : ogg stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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 * );
126 static int Ogg_OpusPacketDuration( logical_stream_t *, ogg_packet * );
128 static int Ogg_BeginningOfStream( demux_t *p_demux );
129 static int Ogg_FindLogicalStreams( demux_t *p_demux );
130 static void Ogg_EndOfStream( demux_t *p_demux );
133 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream );
134 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream );
137 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers );
138 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream, ogg_packet *p_oggpacket, double f_rate );
140 /* Logical bitstream headers */
141 static void Ogg_ReadTheoraHeader( demux_t *, logical_stream_t *, ogg_packet * );
142 static void Ogg_ReadVorbisHeader( demux_t *, logical_stream_t *, ogg_packet * );
143 static void Ogg_ReadSpeexHeader( logical_stream_t *, ogg_packet * );
144 static void Ogg_ReadOpusHeader( demux_t *, logical_stream_t *, ogg_packet * );
145 static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
146 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
147 static void Ogg_ReadAnnodexHeader( demux_t *, logical_stream_t *, ogg_packet * );
148 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
150 /*****************************************************************************
151 * Open: initializes ogg demux structures
152 *****************************************************************************/
153 static int Open( vlc_object_t * p_this )
155 demux_t *p_demux = (demux_t *)p_this;
157 const uint8_t *p_peek;
160 /* Check if we are dealing with an ogg stream */
161 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
162 if( !p_demux->b_force && memcmp( p_peek, "OggS", 4 ) )
167 /* Set exported functions */
168 p_demux->pf_demux = Demux;
169 p_demux->pf_control = Control;
170 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
174 memset( p_sys, 0, sizeof( demux_sys_t ) );
175 p_sys->i_bitrate = 0;
176 p_sys->pp_stream = NULL;
177 p_sys->p_old_stream = NULL;
179 /* Begnning of stream, tell the demux to look for elementary streams. */
183 p_sys->i_length = -1;
185 /* Initialize the Ogg physical bitstream parser */
186 ogg_sync_init( &p_sys->oy );
187 p_sys->b_page_waiting = false;
190 p_sys->p_meta = NULL;
191 TAB_INIT( p_sys->i_seekpoints, p_sys->pp_seekpoints );
196 /*****************************************************************************
197 * Close: frees unused data
198 *****************************************************************************/
199 static void Close( vlc_object_t *p_this )
201 demux_t *p_demux = (demux_t *)p_this;
202 demux_sys_t *p_sys = p_demux->p_sys ;
204 /* Cleanup the bitstream parser */
205 ogg_sync_clear( &p_sys->oy );
207 Ogg_EndOfStream( p_demux );
209 if( p_sys->p_old_stream )
210 Ogg_LogicalStreamDelete( p_demux, p_sys->p_old_stream );
212 TAB_CLEAN( p_sys->i_seekpoints, p_sys->pp_seekpoints );
217 /*****************************************************************************
218 * Demux: reads and demuxes data packets
219 *****************************************************************************
220 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
221 *****************************************************************************/
222 static int Demux( demux_t * p_demux )
224 demux_sys_t *p_sys = p_demux->p_sys;
225 ogg_packet oggpacket;
227 bool b_skipping = false;
230 if( p_sys->i_eos == p_sys->i_streams )
234 msg_Dbg( p_demux, "end of a group of logical streams" );
235 /* We keep the ES to try reusing it in Ogg_BeginningOfStream
236 * only 1 ES is supported (common case for ogg web radio) */
237 if( p_sys->i_streams == 1 )
239 p_sys->p_old_stream = p_sys->pp_stream[0];
240 TAB_CLEAN( p_sys->i_streams, p_sys->pp_stream );
242 Ogg_EndOfStream( p_demux );
246 if( Ogg_BeginningOfStream( p_demux ) != VLC_SUCCESS )
249 msg_Dbg( p_demux, "beginning of a group of logical streams" );
250 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 );
254 * The first data page of a physical stream is stored in the relevant logical stream
255 * in Ogg_FindLogicalStreams. Therefore, we must not read a page and only update the
256 * stream it belongs to if we haven't processed this first page yet. If we do, we
257 * will only process that first page whenever we find the second page for this stream.
258 * While this is fine for Vorbis and Theora, which are continuous codecs, which means
259 * the second page will arrive real quick, this is not fine for Kate, whose second
260 * data page will typically arrive much later.
261 * This means it is now possible to seek right at the start of a stream where the last
262 * logical stream is Kate, without having to wait for the second data page to unblock
263 * the first one, which is the one that triggers the 'no more headers to backup' code.
264 * And, as we all know, seeking without having backed up all headers is bad, since the
265 * codec will fail to initialize if it's missing its headers.
267 if( !p_sys->b_page_waiting)
270 * Demux an ogg page from the stream
272 if( Ogg_ReadPage( p_demux, &p_sys->current_page ) != VLC_SUCCESS )
275 /* Test for End of Stream */
276 if( ogg_page_eos( &p_sys->current_page ) )
281 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
283 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
285 /* if we've just pulled page, look for the right logical stream */
286 if( !p_sys->b_page_waiting )
288 if( p_sys->i_streams == 1 &&
289 ogg_page_serialno( &p_sys->current_page ) != p_stream->os.serialno )
291 msg_Err( p_demux, "Broken Ogg stream (serialno) mismatch" );
292 ogg_stream_reset_serialno( &p_stream->os, ogg_page_serialno( &p_sys->current_page ) );
294 p_stream->b_reinit = true;
295 p_stream->i_pcr = VLC_TS_0;
296 p_stream->i_interpolated_pcr = VLC_TS_0;
297 p_stream->i_previous_granulepos = -1;
298 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0);
301 if( ogg_stream_pagein( &p_stream->os, &p_sys->current_page ) != 0 )
308 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
310 /* Read info from any secondary header packets, if there are any */
311 if( p_stream->i_secondary_header_packets > 0 )
313 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA &&
314 oggpacket.bytes >= 7 &&
315 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
317 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
318 p_stream->i_secondary_header_packets = 0;
320 else if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS &&
321 oggpacket.bytes >= 7 &&
322 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
324 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
325 p_stream->i_secondary_header_packets = 0;
327 else if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
329 p_stream->i_secondary_header_packets = 0;
332 /* update start of data pointer */
333 p_stream->i_data_start = stream_Tell( p_demux->s );
337 /* If any streams have i_skip_frames, only decode (pre-roll)
338 * for those streams */
339 if ( b_skipping && p_stream->i_skip_frames == 0 ) continue;
342 if( p_stream->b_reinit )
344 /* If synchro is re-initialized we need to drop all the packets
345 * until we find a new dated one. */
346 Ogg_UpdatePCR( p_stream, &oggpacket );
348 if( p_stream->i_pcr >= 0 )
350 p_stream->b_reinit = false;
351 /* For Opus, trash the first 80 ms of decoded output as
352 well, to avoid blowing out speakers if we get unlucky.
353 Opus predicts content from prior frames, which can go
354 badly if we seek right where the stream goes from very
355 quiet to very loud. It will converge after a bit. */
356 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
358 ogg_int64_t start_time;
360 p_stream->i_skip_frames = 80*48;
361 /* Make sure we never play audio from within the
362 pre-skip at the beginning of the stream. */
364 Ogg_OpusPacketDuration( p_stream, &oggpacket );
365 start_time = p_stream->i_previous_granulepos;
368 start_time = start_time > duration ?
369 start_time - duration : 0;
371 if( p_stream->i_pre_skip > start_time )
373 p_stream->i_skip_frames +=
374 p_stream->i_pre_skip - start_time;
380 p_stream->i_interpolated_pcr = -1;
381 p_stream->i_previous_granulepos = -1;
385 /* An Ogg/vorbis packet contains an end date granulepos */
386 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
387 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
388 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
389 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
391 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
393 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
397 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
398 VLC_TS_0 + p_stream->i_pcr );
404 Ogg_DecodePacket( p_demux, p_stream, &oggpacket );
407 if( !p_sys->b_page_waiting )
411 /* if a page was waiting, it's now processed */
412 p_sys->b_page_waiting = false;
415 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
417 logical_stream_t *p_stream = p_sys->pp_stream[i_stream];
419 if( p_stream->fmt.i_cat == SPU_ES )
421 if( p_stream->i_interpolated_pcr < 0 )
424 if( p_sys->i_pcr < 0 || p_stream->i_interpolated_pcr < p_sys->i_pcr )
425 p_sys->i_pcr = p_stream->i_interpolated_pcr;
428 if( p_sys->i_pcr >= 0 && ! b_skipping )
429 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
434 /*****************************************************************************
436 *****************************************************************************/
437 static int Control( demux_t *p_demux, int i_query, va_list args )
439 demux_sys_t *p_sys = p_demux->p_sys;
448 p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
450 vlc_meta_Merge( p_meta, p_sys->p_meta );
453 case DEMUX_HAS_UNSUPPORTED_META:
454 pb_bool = (bool*)va_arg( args, bool* );
459 pi64 = (int64_t*)va_arg( args, int64_t * );
460 *pi64 = p_sys->i_pcr;
466 case DEMUX_GET_ATTACHMENTS:
468 input_attachment_t ***ppp_attach =
469 (input_attachment_t***)va_arg( args, input_attachment_t*** );
470 int *pi_int = (int*)va_arg( args, int * );
472 if( p_sys->i_attachments <= 0 )
475 *pi_int = p_sys->i_attachments;
476 *ppp_attach = xmalloc( sizeof(input_attachment_t**) * p_sys->i_attachments );
477 for( int i = 0; i < p_sys->i_attachments; i++ )
478 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachments[i] );
482 case DEMUX_SET_POSITION:
483 /* forbid seeking if we haven't initialized all logical bitstreams yet;
484 if we allowed, some headers would not get backed up and decoder init
485 would fail, making that logical stream unusable */
486 if( p_sys->i_bos > 0 )
491 for( i = 0; i < p_sys->i_streams; i++ )
493 logical_stream_t *p_stream = p_sys->pp_stream[i];
495 /* we'll trash all the data until we find the next pcr */
496 p_stream->b_reinit = true;
497 p_stream->i_pcr = -1;
498 p_stream->i_interpolated_pcr = -1;
499 p_stream->i_previous_granulepos = -1;
500 ogg_stream_reset( &p_stream->os );
502 ogg_sync_reset( &p_sys->oy );
503 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
505 case DEMUX_GET_LENGTH:
506 if ( p_sys->i_length < 0 )
507 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
509 pi64 = (int64_t*)va_arg( args, int64_t * );
510 *pi64 = p_sys->i_length * 1000000;
513 case DEMUX_GET_TITLE_INFO:
515 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
516 int *pi_int = (int*)va_arg( args, int* );
517 int *pi_title_offset = (int*)va_arg( args, int* );
518 int *pi_seekpoint_offset = (int*)va_arg( args, int* );
520 if( p_sys->i_seekpoints > 0 )
523 *ppp_title = malloc( sizeof( input_title_t**) );
524 input_title_t *p_title = (*ppp_title)[0] = vlc_input_title_New();
525 for( int i = 0; i < p_sys->i_seekpoints; i++ )
527 TAB_APPEND( p_title->i_seekpoint, p_title->seekpoint, p_sys->pp_seekpoints[i] );
529 *pi_title_offset = 0;
530 *pi_seekpoint_offset = 0;
534 case DEMUX_SET_TITLE:
536 const int i_title = (int)va_arg( args, int );
541 case DEMUX_SET_SEEKPOINT:
543 const int i_seekpoint = (int)va_arg( args, int );
544 if( i_seekpoint > p_sys->i_seekpoints )
546 return VLC_EGENERIC;// Seek( p_demux, p_sys->pp_seekpoints[i_seekpoint]->i_time_offset );
550 return demux_vaControlHelper( p_demux->s, 0, -1, p_sys->i_bitrate,
555 /****************************************************************************
556 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
557 ****************************************************************************
558 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
559 * are at the end of stream.
560 ****************************************************************************/
561 static int Ogg_ReadPage( demux_t *p_demux, ogg_page *p_oggpage )
563 demux_sys_t *p_ogg = p_demux->p_sys ;
567 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
569 p_buffer = ogg_sync_buffer( &p_ogg->oy, OGGSEEK_BYTES_TO_READ );
571 i_read = stream_Read( p_demux->s, p_buffer, OGGSEEK_BYTES_TO_READ );
575 ogg_sync_wrote( &p_ogg->oy, i_read );
581 /****************************************************************************
582 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
584 ****************************************************************************/
585 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
586 ogg_packet *p_oggpacket )
588 p_stream->i_end_trim = 0;
589 /* Convert the granulepos into a pcr */
590 if( p_oggpacket->granulepos >= 0 )
592 if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
593 p_stream->fmt.i_codec == VLC_CODEC_KATE )
595 ogg_int64_t iframe = p_oggpacket->granulepos >>
596 p_stream->i_granule_shift;
597 ogg_int64_t pframe = p_oggpacket->granulepos -
598 ( iframe << p_stream->i_granule_shift );
600 p_stream->i_pcr = ( iframe + pframe - p_stream->i_keyframe_offset )
601 * INT64_C(1000000) / p_stream->f_rate;
603 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
605 ogg_int64_t i_dts = p_oggpacket->granulepos >> 31;
606 /* NB, OggDirac granulepos values are in units of 2*picturerate */
607 p_stream->i_pcr = (i_dts/2) * INT64_C(1000000) / p_stream->f_rate;
612 sample = p_oggpacket->granulepos;
613 if( p_oggpacket->e_o_s &&
614 p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
615 p_stream->i_previous_granulepos >= 0 )
618 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
621 ogg_int64_t end_sample;
622 end_sample = p_stream->i_previous_granulepos + duration;
623 if( end_sample > sample )
624 p_stream->i_end_trim = (int)(end_sample - sample);
627 if (sample >= p_stream->i_pre_skip)
628 sample -= p_stream->i_pre_skip;
631 p_stream->i_pcr = sample * INT64_C(1000000) / p_stream->f_rate;
634 p_stream->i_pcr += VLC_TS_0;
635 p_stream->i_interpolated_pcr = p_stream->i_pcr;
640 p_stream->i_pcr = -1;
642 /* no granulepos available, try to interpolate the pcr.
643 * If we can't then don't touch the old value. */
644 if( p_stream->fmt.i_cat == VIDEO_ES )
645 /* 1 frame per packet */
646 p_stream->i_interpolated_pcr += (INT64_C(1000000) / p_stream->f_rate);
647 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS &&
648 p_stream->i_previous_granulepos >= 0 &&
650 Ogg_OpusPacketDuration( p_stream, p_oggpacket ) ) > 0 )
653 p_oggpacket->granulepos =
654 p_stream->i_previous_granulepos + duration;
655 sample = p_oggpacket->granulepos;
656 if (sample >= p_stream->i_pre_skip)
657 sample -= p_stream->i_pre_skip;
660 p_stream->i_interpolated_pcr =
661 VLC_TS_0 + sample * INT64_C(1000000) / p_stream->f_rate;
663 else if( p_stream->fmt.i_bitrate )
665 p_stream->i_interpolated_pcr +=
666 ( p_oggpacket->bytes * INT64_C(1000000) /
667 p_stream->fmt.i_bitrate / 8 );
670 p_stream->i_previous_granulepos = p_oggpacket->granulepos;
673 /****************************************************************************
674 * Ogg_DecodePacket: Decode an Ogg packet.
675 ****************************************************************************/
676 static void Ogg_DecodePacket( demux_t *p_demux,
677 logical_stream_t *p_stream,
678 ogg_packet *p_oggpacket )
682 int i_header_len = 0;
683 mtime_t i_pts = -1, i_interpolated_pts;
684 demux_sys_t *p_ogg = p_demux->p_sys;
686 if( p_oggpacket->bytes >= 7 &&
687 ! memcmp ( p_oggpacket->packet, "Annodex", 7 ) )
689 /* it's an Annodex packet -- skip it (do nothing) */
692 else if( p_oggpacket->bytes >= 7 &&
693 ! memcmp ( p_oggpacket->packet, "AnxData", 7 ) )
695 /* it's an AnxData packet -- skip it (do nothing) */
699 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT && p_oggpacket->bytes > 0 &&
700 p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
702 /* Check the ES is selected */
703 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
704 p_stream->p_es, &b_selected );
706 if( p_stream->b_force_backup )
709 p_stream->i_packets_backup++;
710 switch( p_stream->fmt.i_codec )
712 case VLC_CODEC_VORBIS:
713 case VLC_CODEC_SPEEX:
714 case VLC_CODEC_THEORA:
715 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
720 if( p_stream->i_packets_backup == 2 ) p_stream->b_force_backup = 0;
725 if( !p_stream->fmt.audio.i_rate && p_stream->i_packets_backup == 2 )
727 Ogg_ReadFlacHeader( p_demux, p_stream, p_oggpacket );
728 p_stream->b_force_backup = 0;
730 else if( p_stream->fmt.audio.i_rate )
732 p_stream->b_force_backup = 0;
733 if( p_oggpacket->bytes >= 9 )
735 p_oggpacket->packet += 9;
736 p_oggpacket->bytes -= 9;
743 if( p_stream->i_packets_backup == p_stream->i_kate_num_headers ) p_stream->b_force_backup = 0;
748 p_stream->b_force_backup = 0;
753 /* Backup the ogg packet (likely an header packet) */
756 void *p_org = p_stream->p_headers;
757 p_stream->i_headers += p_oggpacket->bytes;
758 p_stream->p_headers = realloc( p_stream->p_headers, p_stream->i_headers );
759 if( p_stream->p_headers )
761 memcpy( (unsigned char *)p_stream->p_headers + p_stream->i_headers - p_oggpacket->bytes,
762 p_oggpacket->packet, p_oggpacket->bytes );
767 p_stream->i_headers = 0;
768 p_stream->p_headers = NULL;
772 else if( xiph_AppendHeaders( &p_stream->i_headers, &p_stream->p_headers,
773 p_oggpacket->bytes, p_oggpacket->packet ) )
775 p_stream->i_headers = 0;
776 p_stream->p_headers = NULL;
778 if( p_stream->i_headers > 0 )
780 if( !p_stream->b_force_backup )
782 /* Last header received, commit changes */
783 free( p_stream->fmt.p_extra );
785 p_stream->fmt.i_extra = p_stream->i_headers;
786 p_stream->fmt.p_extra = malloc( p_stream->i_headers );
787 if( p_stream->fmt.p_extra )
788 memcpy( p_stream->fmt.p_extra, p_stream->p_headers,
789 p_stream->i_headers );
791 p_stream->fmt.i_extra = 0;
793 if( Ogg_LogicalStreamResetEsFormat( p_demux, p_stream ) )
794 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
795 p_stream->p_es, &p_stream->fmt );
797 if( p_stream->i_headers > 0 )
798 Ogg_ExtractMeta( p_demux, p_stream->fmt.i_codec,
799 p_stream->p_headers, p_stream->i_headers );
801 /* we're not at BOS anymore for this logical stream */
806 b_selected = false; /* Discard the header packet */
809 /* Convert the pcr into a pts */
810 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
811 p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
812 p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
813 p_stream->fmt.i_codec == VLC_CODEC_FLAC )
815 if( p_stream->i_pcr >= 0 )
817 /* This is for streams where the granulepos of the header packets
818 * doesn't match these of the data packets (eg. ogg web radios). */
819 if( p_stream->i_previous_pcr == 0 &&
820 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
823 /* Call the pace control */
824 es_out_Control( p_demux->out, ES_OUT_SET_PCR,
825 VLC_TS_0 + p_stream->i_pcr );
828 p_stream->i_previous_pcr = p_stream->i_pcr;
830 /* The granulepos is the end date of the sample */
831 i_pts = p_stream->i_pcr;
835 /* Convert the granulepos into the next pcr */
836 i_interpolated_pts = p_stream->i_interpolated_pcr;
837 Ogg_UpdatePCR( p_stream, p_oggpacket );
839 /* SPU streams are typically discontinuous, do not mind large gaps */
840 if( p_stream->fmt.i_cat != SPU_ES )
842 if( p_stream->i_pcr >= 0 )
844 /* This is for streams where the granulepos of the header packets
845 * doesn't match these of the data packets (eg. ogg web radios). */
846 if( p_stream->i_previous_pcr == 0 &&
847 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY )
850 /* Call the pace control */
851 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_stream->i_pcr );
856 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
857 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
858 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
859 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
860 p_stream->i_pcr >= 0 )
862 p_stream->i_previous_pcr = p_stream->i_pcr;
864 /* The granulepos is the start date of the sample */
865 i_pts = p_stream->i_pcr;
870 /* This stream isn't currently selected so we don't need to decode it,
871 * but we did need to store its pcr as it might be selected later on */
875 if( !( p_block = block_Alloc( p_oggpacket->bytes ) ) ) return;
878 /* may need to preroll after a seek */
879 if ( p_stream->i_skip_frames > 0 )
881 if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
884 duration = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
885 if( p_stream->i_skip_frames > duration )
887 p_block->i_flags |= BLOCK_FLAG_PREROLL;
888 p_block->i_nb_samples = 0;
889 p_stream->i_skip_frames -= duration;
893 p_block->i_nb_samples = duration - p_stream->i_skip_frames;
894 if( p_stream->i_previous_granulepos >=
895 p_block->i_nb_samples + p_stream->i_pre_skip )
897 i_pts = VLC_TS_0 + (p_stream->i_previous_granulepos
898 - p_block->i_nb_samples - p_stream->i_pre_skip) *
899 INT64_C(1000000) / p_stream->f_rate;
901 p_stream->i_skip_frames = 0;
906 p_block->i_flags |= BLOCK_FLAG_PREROLL;
907 p_stream->i_skip_frames--;
910 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
911 p_block->i_nb_samples = Ogg_OpusPacketDuration( p_stream, p_oggpacket );
915 if( i_pts == VLC_TS_INVALID ) i_pts = VLC_TS_0;
916 else if( i_pts == -1 && i_interpolated_pts == VLC_TS_INVALID )
918 else if( i_pts == -1 && p_stream->fmt.i_cat == VIDEO_ES )
919 i_pts = i_interpolated_pts;
920 else if( i_pts == -1 ) i_pts = VLC_TS_INVALID;
922 if( p_stream->fmt.i_cat == AUDIO_ES )
924 p_block->i_dts = p_block->i_pts = i_pts;
925 /* Blatant abuse of the i_length field. */
926 p_block->i_length = p_stream->i_end_trim;
928 else if( p_stream->fmt.i_cat == SPU_ES )
930 p_block->i_dts = p_block->i_pts = i_pts;
931 p_block->i_length = 0;
933 else if( p_stream->fmt.i_codec == VLC_CODEC_THEORA )
935 p_block->i_dts = p_block->i_pts = i_pts;
936 if( (p_oggpacket->granulepos & ((1<<p_stream->i_granule_shift)-1)) == 0 )
938 p_block->i_flags |= BLOCK_FLAG_TYPE_I;
941 else if( p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
943 ogg_int64_t dts = p_oggpacket->granulepos >> 31;
944 ogg_int64_t delay = (p_oggpacket->granulepos >> 9) & 0x1fff;
946 uint64_t u_pnum = dts + delay;
948 p_block->i_dts = p_stream->i_pcr;
949 p_block->i_pts = VLC_TS_INVALID;
950 /* NB, OggDirac granulepos values are in units of 2*picturerate */
952 /* granulepos for dirac is possibly broken, this value should be ignored */
953 if( -1 != p_oggpacket->granulepos )
954 p_block->i_pts = u_pnum * INT64_C(1000000) / p_stream->f_rate / 2;
958 p_block->i_dts = i_pts;
959 p_block->i_pts = VLC_TS_INVALID;
962 if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
963 p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
964 p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
965 p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
966 p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
967 p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
968 p_stream->fmt.i_codec != VLC_CODEC_CMML &&
969 p_stream->fmt.i_codec != VLC_CODEC_DIRAC &&
970 p_stream->fmt.i_codec != VLC_CODEC_KATE )
972 if( p_oggpacket->bytes <= 0 )
974 msg_Dbg( p_demux, "discarding 0 sized packet" );
975 block_Release( p_block );
978 /* We remove the header from the packet */
979 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
980 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
982 if( p_stream->fmt.i_codec == VLC_CODEC_SUBT)
984 /* But with subtitles we need to retrieve the duration first */
987 if( i_header_len > 0 && p_oggpacket->bytes >= i_header_len + 1 )
989 for( i = 0, lenbytes = 0; i < i_header_len; i++ )
991 lenbytes = lenbytes << 8;
992 lenbytes += *(p_oggpacket->packet + i_header_len - i);
995 if( p_oggpacket->bytes - 1 - i_header_len > 2 ||
996 ( p_oggpacket->packet[i_header_len + 1] != ' ' &&
997 p_oggpacket->packet[i_header_len + 1] != 0 &&
998 p_oggpacket->packet[i_header_len + 1] != '\n' &&
999 p_oggpacket->packet[i_header_len + 1] != '\r' ) )
1001 p_block->i_length = (mtime_t)lenbytes * 1000;
1006 if( p_block->i_buffer >= (unsigned int)i_header_len )
1007 p_block->i_buffer -= i_header_len;
1009 p_block->i_buffer = 0;
1012 if( p_stream->fmt.i_codec == VLC_CODEC_TARKIN )
1014 /* FIXME: the biggest hack I've ever done */
1015 msg_Warn( p_demux, "tarkin pts: %"PRId64", granule: %"PRId64,
1016 p_block->i_pts, p_block->i_dts );
1020 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
1021 p_oggpacket->bytes - i_header_len );
1023 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1026 /* Re-implemented to avoid linking against libopus from the demuxer. */
1027 static int Ogg_OpusPacketDuration( logical_stream_t *p_stream,
1028 ogg_packet *p_oggpacket )
1030 static const int silk_fs_div[4] = { 6000, 3000, 1500, 1000 };
1036 if( p_oggpacket->bytes < 1 )
1037 return VLC_EGENERIC;
1038 toc = p_oggpacket->packet[0];
1049 if( p_oggpacket->bytes < 2 )
1050 return VLC_EGENERIC;
1051 nframes = p_oggpacket->packet[1]&0x3F;
1054 i_rate = (int)p_stream->fmt.audio.i_rate;
1056 frame_size = (i_rate << (toc >> 3 & 3)) / 400;
1057 else if( ( toc&0x60 ) == 0x60 )
1058 frame_size = i_rate/(100 >> (toc >> 3 & 1));
1060 frame_size = i_rate*60 / silk_fs_div[toc >> 3 & 3];
1061 nsamples = nframes*frame_size;
1062 if( nsamples*25 > i_rate*3 )
1063 return VLC_EGENERIC;
1067 /****************************************************************************
1068 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
1069 * stream and fill p_ogg.
1070 *****************************************************************************
1071 * The initial page of a logical stream is marked as a 'bos' page.
1072 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
1073 * together and all of the initial pages must appear before any data pages.
1075 * On success this function returns VLC_SUCCESS.
1076 ****************************************************************************/
1077 static int Ogg_FindLogicalStreams( demux_t *p_demux )
1079 demux_sys_t *p_ogg = p_demux->p_sys ;
1080 ogg_packet oggpacket;
1083 p_ogg->i_total_length = stream_Size ( p_demux->s );
1084 msg_Dbg( p_demux, "File length is %"PRId64" bytes", p_ogg->i_total_length );
1087 while( Ogg_ReadPage( p_demux, &p_ogg->current_page ) == VLC_SUCCESS )
1090 if( ogg_page_bos( &p_ogg->current_page ) )
1093 /* All is wonderful in our fine fine little world.
1094 * We found the beginning of our first logical stream. */
1095 while( ogg_page_bos( &p_ogg->current_page ) )
1097 logical_stream_t *p_stream;
1099 p_stream = malloc( sizeof(logical_stream_t) );
1100 if( unlikely( !p_stream ) )
1103 TAB_APPEND( p_ogg->i_streams, p_ogg->pp_stream, p_stream );
1105 memset( p_stream, 0, sizeof(logical_stream_t) );
1106 p_stream->p_headers = 0;
1107 p_stream->i_secondary_header_packets = 0;
1109 p_stream->i_keyframe_offset = 0;
1110 p_stream->i_skip_frames = 0;
1112 p_stream->i_data_start = 0;
1114 es_format_Init( &p_stream->fmt, 0, 0 );
1115 es_format_Init( &p_stream->fmt_old, 0, 0 );
1117 /* Setup the logical stream */
1118 p_stream->i_serial_no = ogg_page_serialno( &p_ogg->current_page );
1119 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
1121 /* Extract the initial header from the first page and verify
1122 * the codec type of this Ogg bitstream */
1123 if( ogg_stream_pagein( &p_stream->os, &p_ogg->current_page ) < 0 )
1125 /* error. stream version mismatch perhaps */
1126 msg_Err( p_demux, "error reading first page of "
1127 "Ogg bitstream data" );
1128 return VLC_EGENERIC;
1131 /* FIXME: check return value */
1132 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
1134 /* Check for Vorbis header */
1135 if( oggpacket.bytes >= 7 &&
1136 ! memcmp( oggpacket.packet, "\x01vorbis", 7 ) )
1138 Ogg_ReadVorbisHeader( p_demux, p_stream, &oggpacket );
1139 msg_Dbg( p_demux, "found vorbis header" );
1141 /* Check for Speex header */
1142 else if( oggpacket.bytes >= 5 &&
1143 ! memcmp( oggpacket.packet, "Speex", 5 ) )
1145 Ogg_ReadSpeexHeader( p_stream, &oggpacket );
1146 msg_Dbg( p_demux, "found speex header, channels: %i, "
1147 "rate: %i, bitrate: %i",
1148 p_stream->fmt.audio.i_channels,
1149 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
1151 /* Check for Opus header */
1152 else if( oggpacket.bytes >= 8 &&
1153 ! memcmp( oggpacket.packet, "OpusHead", 8 ) )
1155 Ogg_ReadOpusHeader( p_demux, p_stream, &oggpacket );
1156 msg_Dbg( p_demux, "found opus header, channels: %i, "
1158 p_stream->fmt.audio.i_channels,
1159 (int)p_stream->i_pre_skip);
1160 p_stream->i_skip_frames = p_stream->i_pre_skip;
1162 /* Check for Flac header (< version 1.1.1) */
1163 else if( oggpacket.bytes >= 4 &&
1164 ! memcmp( oggpacket.packet, "fLaC", 4 ) )
1166 msg_Dbg( p_demux, "found FLAC header" );
1168 /* Grrrr!!!! Did they really have to put all the
1169 * important info in the second header packet!!!
1170 * (STREAMINFO metadata is in the following packet) */
1171 p_stream->b_force_backup = 1;
1173 p_stream->fmt.i_cat = AUDIO_ES;
1174 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1176 /* Check for Flac header (>= version 1.1.1) */
1177 else if( oggpacket.bytes >= 13 && oggpacket.packet[0] ==0x7F &&
1178 ! memcmp( &oggpacket.packet[1], "FLAC", 4 ) &&
1179 ! memcmp( &oggpacket.packet[9], "fLaC", 4 ) )
1181 int i_packets = ((int)oggpacket.packet[7]) << 8 |
1182 oggpacket.packet[8];
1183 msg_Dbg( p_demux, "found FLAC header version %i.%i "
1184 "(%i header packets)",
1185 oggpacket.packet[5], oggpacket.packet[6],
1188 p_stream->b_force_backup = 1;
1190 p_stream->fmt.i_cat = AUDIO_ES;
1191 p_stream->fmt.i_codec = VLC_CODEC_FLAC;
1192 oggpacket.packet += 13; oggpacket.bytes -= 13;
1193 Ogg_ReadFlacHeader( p_demux, p_stream, &oggpacket );
1195 /* Check for Theora header */
1196 else if( oggpacket.bytes >= 7 &&
1197 ! memcmp( oggpacket.packet, "\x80theora", 7 ) )
1199 Ogg_ReadTheoraHeader( p_demux, p_stream, &oggpacket );
1202 "found theora header, bitrate: %i, rate: %f",
1203 p_stream->fmt.i_bitrate, p_stream->f_rate );
1205 /* Check for Dirac header */
1206 else if( ( oggpacket.bytes >= 5 &&
1207 ! memcmp( oggpacket.packet, "BBCD\x00", 5 ) ) ||
1208 ( oggpacket.bytes >= 9 &&
1209 ! memcmp( oggpacket.packet, "KW-DIRAC\x00", 9 ) ) )
1211 if( Ogg_ReadDiracHeader( p_stream, &oggpacket ) )
1212 msg_Dbg( p_demux, "found dirac header" );
1215 msg_Warn( p_demux, "found dirac header isn't decodable" );
1220 /* Check for Tarkin header */
1221 else if( oggpacket.bytes >= 7 &&
1222 ! memcmp( &oggpacket.packet[1], "tarkin", 6 ) )
1226 msg_Dbg( p_demux, "found tarkin header" );
1227 p_stream->fmt.i_cat = VIDEO_ES;
1228 p_stream->fmt.i_codec = VLC_CODEC_TARKIN;
1230 /* Cheat and get additionnal info ;) */
1231 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
1232 oggpack_adv( &opb, 88 );
1233 oggpack_adv( &opb, 104 );
1234 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1235 p_stream->f_rate = 2; /* FIXME */
1237 "found tarkin header, bitrate: %i, rate: %f",
1238 p_stream->fmt.i_bitrate, p_stream->f_rate );
1240 /* Check for Annodex header */
1241 else if( oggpacket.bytes >= 7 &&
1242 ! memcmp( oggpacket.packet, "Annodex", 7 ) )
1244 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1245 /* kill annodex track */
1249 /* Check for Annodex header */
1250 else if( oggpacket.bytes >= 7 &&
1251 ! memcmp( oggpacket.packet, "AnxData", 7 ) )
1253 Ogg_ReadAnnodexHeader( p_demux, p_stream, &oggpacket );
1255 /* Check for Kate header */
1256 else if( oggpacket.bytes >= 8 &&
1257 ! memcmp( &oggpacket.packet[1], "kate\0\0\0", 7 ) )
1259 Ogg_ReadKateHeader( p_stream, &oggpacket );
1260 msg_Dbg( p_demux, "found kate header" );
1262 else if( oggpacket.bytes >= 142 &&
1263 !memcmp( &oggpacket.packet[1],
1264 "Direct Show Samples embedded in Ogg", 35 ))
1266 /* Old header type */
1268 /* Check for video header (old format) */
1269 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
1270 oggpacket.bytes >= 184 )
1272 p_stream->fmt.i_cat = VIDEO_ES;
1273 p_stream->fmt.i_codec =
1274 VLC_FOURCC( oggpacket.packet[68],
1275 oggpacket.packet[69],
1276 oggpacket.packet[70],
1277 oggpacket.packet[71] );
1278 msg_Dbg( p_demux, "found video header of type: %.4s",
1279 (char *)&p_stream->fmt.i_codec );
1281 p_stream->fmt.video.i_frame_rate = 10000000;
1282 p_stream->fmt.video.i_frame_rate_base =
1283 GetQWLE((oggpacket.packet+164));
1284 p_stream->f_rate = 10000000.0 /
1285 GetQWLE((oggpacket.packet+164));
1286 p_stream->fmt.video.i_bits_per_pixel =
1287 GetWLE((oggpacket.packet+182));
1288 if( !p_stream->fmt.video.i_bits_per_pixel )
1290 p_stream->fmt.video.i_bits_per_pixel = 24;
1291 p_stream->fmt.video.i_width =
1292 GetDWLE((oggpacket.packet+176));
1293 p_stream->fmt.video.i_height =
1294 GetDWLE((oggpacket.packet+180));
1297 "fps: %f, width:%i; height:%i, bitcount:%i",
1299 p_stream->fmt.video.i_width,
1300 p_stream->fmt.video.i_height,
1301 p_stream->fmt.video.i_bits_per_pixel);
1304 /* Check for audio header (old format) */
1305 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
1308 unsigned int i_format_tag;
1310 p_stream->fmt.i_cat = AUDIO_ES;
1312 i_extra_size = GetWLE((oggpacket.packet+140));
1313 if( i_extra_size > 0 && i_extra_size < oggpacket.bytes - 142 )
1315 p_stream->fmt.i_extra = i_extra_size;
1316 p_stream->fmt.p_extra = malloc( i_extra_size );
1317 if( p_stream->fmt.p_extra )
1318 memcpy( p_stream->fmt.p_extra,
1319 oggpacket.packet + 142, i_extra_size );
1321 p_stream->fmt.i_extra = 0;
1324 i_format_tag = GetWLE((oggpacket.packet+124));
1325 p_stream->fmt.audio.i_channels =
1326 GetWLE((oggpacket.packet+126));
1327 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1328 GetDWLE((oggpacket.packet+128));
1329 p_stream->fmt.i_bitrate =
1330 GetDWLE((oggpacket.packet+132)) * 8;
1331 p_stream->fmt.audio.i_blockalign =
1332 GetWLE((oggpacket.packet+136));
1333 p_stream->fmt.audio.i_bitspersample =
1334 GetWLE((oggpacket.packet+138));
1336 wf_tag_to_fourcc( i_format_tag,
1337 &p_stream->fmt.i_codec, 0 );
1339 if( p_stream->fmt.i_codec ==
1340 VLC_FOURCC('u','n','d','f') )
1342 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1343 ( i_format_tag >> 8 ) & 0xff,
1344 i_format_tag & 0xff );
1347 msg_Dbg( p_demux, "found audio header of type: %.4s",
1348 (char *)&p_stream->fmt.i_codec );
1349 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1350 "%dbits/sample %dkb/s",
1352 p_stream->fmt.audio.i_channels,
1353 p_stream->fmt.audio.i_rate,
1354 p_stream->fmt.audio.i_bitspersample,
1355 p_stream->fmt.i_bitrate / 1024 );
1360 msg_Dbg( p_demux, "stream %d has an old header "
1361 "but is of an unknown type", p_ogg->i_streams-1 );
1366 else if( oggpacket.bytes >= 44+1 &&
1367 (*oggpacket.packet & PACKET_TYPE_BITS ) == PACKET_TYPE_HEADER )
1369 stream_header_t tmp;
1370 stream_header_t *st = &tmp;
1372 memcpy( st->streamtype, &oggpacket.packet[1+0], 8 );
1373 memcpy( st->subtype, &oggpacket.packet[1+8], 4 );
1374 st->size = GetDWLE( &oggpacket.packet[1+12] );
1375 st->time_unit = GetQWLE( &oggpacket.packet[1+16] );
1376 st->samples_per_unit = GetQWLE( &oggpacket.packet[1+24] );
1377 st->default_len = GetDWLE( &oggpacket.packet[1+32] );
1378 st->buffersize = GetDWLE( &oggpacket.packet[1+36] );
1379 st->bits_per_sample = GetWLE( &oggpacket.packet[1+40] ); // (padding 2)
1381 /* Check for video header (new format) */
1382 if( !strncmp( st->streamtype, "video", 5 ) &&
1383 oggpacket.bytes >= 52+1 )
1385 st->sh.video.width = GetDWLE( &oggpacket.packet[1+44] );
1386 st->sh.video.height = GetDWLE( &oggpacket.packet[1+48] );
1388 p_stream->fmt.i_cat = VIDEO_ES;
1390 /* We need to get rid of the header packet */
1391 ogg_stream_packetout( &p_stream->os, &oggpacket );
1393 p_stream->fmt.i_codec =
1394 VLC_FOURCC( st->subtype[0], st->subtype[1],
1395 st->subtype[2], st->subtype[3] );
1396 msg_Dbg( p_demux, "found video header of type: %.4s",
1397 (char *)&p_stream->fmt.i_codec );
1399 p_stream->fmt.video.i_frame_rate = 10000000;
1400 p_stream->fmt.video.i_frame_rate_base = st->time_unit;
1401 if( st->time_unit <= 0 )
1402 st->time_unit = 400000;
1403 p_stream->f_rate = 10000000.0 / st->time_unit;
1404 p_stream->fmt.video.i_bits_per_pixel = st->bits_per_sample;
1405 p_stream->fmt.video.i_width = st->sh.video.width;
1406 p_stream->fmt.video.i_height = st->sh.video.height;
1409 "fps: %f, width:%i; height:%i, bitcount:%i",
1411 p_stream->fmt.video.i_width,
1412 p_stream->fmt.video.i_height,
1413 p_stream->fmt.video.i_bits_per_pixel );
1415 /* Check for audio header (new format) */
1416 else if( !strncmp( st->streamtype, "audio", 5 ) &&
1417 oggpacket.bytes >= 56+1 )
1423 st->sh.audio.channels = GetWLE( &oggpacket.packet[1+44] );
1424 st->sh.audio.blockalign = GetWLE( &oggpacket.packet[1+48] );
1425 st->sh.audio.avgbytespersec = GetDWLE( &oggpacket.packet[1+52] );
1427 p_stream->fmt.i_cat = AUDIO_ES;
1429 /* We need to get rid of the header packet */
1430 ogg_stream_packetout( &p_stream->os, &oggpacket );
1432 i_extra_size = st->size - 56;
1434 if( i_extra_size > 0 &&
1435 i_extra_size < oggpacket.bytes - 1 - 56 )
1437 p_stream->fmt.i_extra = i_extra_size;
1438 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
1439 if( p_stream->fmt.p_extra )
1440 memcpy( p_stream->fmt.p_extra, st + 1,
1441 p_stream->fmt.i_extra );
1443 p_stream->fmt.i_extra = 0;
1446 memcpy( p_buffer, st->subtype, 4 );
1448 i_format_tag = strtol(p_buffer,NULL,16);
1449 p_stream->fmt.audio.i_channels = st->sh.audio.channels;
1450 if( st->time_unit <= 0 )
1451 st->time_unit = 10000000;
1452 p_stream->f_rate = p_stream->fmt.audio.i_rate = st->samples_per_unit * 10000000 / st->time_unit;
1453 p_stream->fmt.i_bitrate = st->sh.audio.avgbytespersec * 8;
1454 p_stream->fmt.audio.i_blockalign = st->sh.audio.blockalign;
1455 p_stream->fmt.audio.i_bitspersample = st->bits_per_sample;
1457 wf_tag_to_fourcc( i_format_tag,
1458 &p_stream->fmt.i_codec, 0 );
1460 if( p_stream->fmt.i_codec ==
1461 VLC_FOURCC('u','n','d','f') )
1463 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
1464 ( i_format_tag >> 8 ) & 0xff,
1465 i_format_tag & 0xff );
1468 msg_Dbg( p_demux, "found audio header of type: %.4s",
1469 (char *)&p_stream->fmt.i_codec );
1470 msg_Dbg( p_demux, "audio:0x%4.4x channels:%d %dHz "
1471 "%dbits/sample %dkb/s",
1473 p_stream->fmt.audio.i_channels,
1474 p_stream->fmt.audio.i_rate,
1475 p_stream->fmt.audio.i_bitspersample,
1476 p_stream->fmt.i_bitrate / 1024 );
1478 /* Check for text (subtitles) header */
1479 else if( !strncmp(st->streamtype, "text", 4) )
1481 /* We need to get rid of the header packet */
1482 ogg_stream_packetout( &p_stream->os, &oggpacket );
1484 msg_Dbg( p_demux, "found text subtitles header" );
1485 p_stream->fmt.i_cat = SPU_ES;
1486 p_stream->fmt.i_codec = VLC_CODEC_SUBT;
1487 p_stream->f_rate = 1000; /* granulepos is in millisec */
1491 msg_Dbg( p_demux, "stream %d has a header marker "
1492 "but is of an unknown type", p_ogg->i_streams-1 );
1497 else if( oggpacket.bytes >= 7 &&
1498 ! memcmp( oggpacket.packet, "fishead", 7 ) )
1502 msg_Dbg( p_demux, "stream %d is a skeleton",
1503 p_ogg->i_streams-1 );
1504 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1508 msg_Dbg( p_demux, "stream %d is of unknown type",
1509 p_ogg->i_streams-1 );
1514 if( Ogg_ReadPage( p_demux, &p_ogg->current_page ) != VLC_SUCCESS )
1515 return VLC_EGENERIC;
1518 /* we'll need to get all headers for all of those streams
1519 that we have to backup headers for */
1521 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1523 if( p_ogg->pp_stream[i_stream]->b_force_backup )
1528 /* This is the first data page, which means we are now finished
1529 * with the initial pages. We just need to store it in the relevant
1531 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1533 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1534 &p_ogg->current_page ) == 0 )
1536 p_ogg->b_page_waiting = true;
1545 return VLC_EGENERIC;
1548 /****************************************************************************
1549 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1550 * Elementary streams.
1551 ****************************************************************************/
1552 static int Ogg_BeginningOfStream( demux_t *p_demux )
1554 demux_sys_t *p_ogg = p_demux->p_sys ;
1555 logical_stream_t *p_old_stream = p_ogg->p_old_stream;
1558 /* Find the logical streams embedded in the physical stream and
1559 * initialize our p_ogg structure. */
1560 if( Ogg_FindLogicalStreams( p_demux ) != VLC_SUCCESS )
1562 msg_Warn( p_demux, "couldn't find any ogg logical stream" );
1563 return VLC_EGENERIC;
1566 p_ogg->i_bitrate = 0;
1568 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1570 logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
1572 p_stream->p_es = NULL;
1574 /* initialise kframe index */
1577 /* Try first to reuse an old ES */
1579 p_old_stream->fmt.i_cat == p_stream->fmt.i_cat &&
1580 p_old_stream->fmt.i_codec == p_stream->fmt.i_codec )
1582 msg_Dbg( p_demux, "will reuse old stream to avoid glitch" );
1584 p_stream->p_es = p_old_stream->p_es;
1585 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
1587 p_old_stream->p_es = NULL;
1588 p_old_stream = NULL;
1591 if( !p_stream->p_es )
1593 /* Better be safe than sorry when possible with ogm */
1594 if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
1595 p_stream->fmt.i_codec == VLC_CODEC_A52 )
1596 p_stream->fmt.b_packetized = false;
1598 p_stream->p_es = es_out_Add( p_demux->out, &p_stream->fmt );
1601 // TODO: something to do here ?
1602 if( p_stream->fmt.i_codec == VLC_CODEC_CMML )
1604 /* Set the CMML stream active */
1605 es_out_Control( p_demux->out, ES_OUT_SET_ES, p_stream->p_es );
1608 p_ogg->i_bitrate += p_stream->fmt.i_bitrate;
1610 p_stream->i_pcr = p_stream->i_previous_pcr =
1611 p_stream->i_interpolated_pcr = -1;
1612 p_stream->b_reinit = false;
1615 if( p_ogg->p_old_stream )
1617 if( p_ogg->p_old_stream->p_es )
1618 msg_Dbg( p_demux, "old stream not reused" );
1619 Ogg_LogicalStreamDelete( p_demux, p_ogg->p_old_stream );
1620 p_ogg->p_old_stream = NULL;
1624 /* get total frame count for video stream; we will need this for seeking */
1625 p_ogg->i_total_frames = 0;
1630 /****************************************************************************
1631 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1632 ****************************************************************************/
1633 static void Ogg_EndOfStream( demux_t *p_demux )
1635 demux_sys_t *p_ogg = p_demux->p_sys ;
1638 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1639 Ogg_LogicalStreamDelete( p_demux, p_ogg->pp_stream[i_stream] );
1640 free( p_ogg->pp_stream );
1643 p_ogg->i_bitrate = 0;
1644 p_ogg->i_streams = 0;
1645 p_ogg->pp_stream = NULL;
1649 vlc_meta_Delete( p_ogg->p_meta );
1650 p_ogg->p_meta = NULL;
1654 * This function delete and release all data associated to a logical_stream_t
1656 static void Ogg_LogicalStreamDelete( demux_t *p_demux, logical_stream_t *p_stream )
1658 if( p_stream->p_es )
1659 es_out_Del( p_demux->out, p_stream->p_es );
1661 ogg_stream_clear( &p_stream->os );
1662 free( p_stream->p_headers );
1664 es_format_Clean( &p_stream->fmt_old );
1665 es_format_Clean( &p_stream->fmt );
1667 if ( p_stream->idx != NULL)
1669 oggseek_index_entries_free( p_stream->idx );
1675 * This function check if a we need to reset a decoder in case we are
1678 static bool Ogg_IsVorbisFormatCompatible( const es_format_t *p_new, const es_format_t *p_old )
1680 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1681 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1682 unsigned i_new_count;
1683 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1686 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1687 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1688 unsigned i_old_count;
1689 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1692 bool b_match = i_new_count == i_old_count;
1693 for( unsigned i = 0; i < i_new_count && b_match; i++ )
1695 /* Ignore vorbis comment */
1698 if( pi_new_size[i] != pi_old_size[i] ||
1699 memcmp( pp_new_data[i], pp_old_data[i], pi_new_size[i] ) )
1703 for( unsigned i = 0; i < i_new_count; i++ )
1704 free( pp_new_data[i] );
1705 for( unsigned i = 0; i < i_old_count; i++ )
1706 free( pp_old_data[i] );
1710 static bool Ogg_IsOpusFormatCompatible( const es_format_t *p_new,
1711 const es_format_t *p_old )
1713 unsigned pi_new_size[XIPH_MAX_HEADER_COUNT];
1714 void *pp_new_data[XIPH_MAX_HEADER_COUNT];
1715 unsigned i_new_count;
1716 if( xiph_SplitHeaders(pi_new_size, pp_new_data, &i_new_count, p_new->i_extra, p_new->p_extra ) )
1718 unsigned pi_old_size[XIPH_MAX_HEADER_COUNT];
1719 void *pp_old_data[XIPH_MAX_HEADER_COUNT];
1720 unsigned i_old_count;
1721 if( xiph_SplitHeaders(pi_old_size, pp_old_data, &i_old_count, p_old->i_extra, p_old->p_extra ) )
1723 bool b_match = false;
1724 if( i_new_count == i_old_count && i_new_count > 0 )
1726 static const unsigned char default_map[2] = { 0, 1 };
1727 unsigned char *p_old_head;
1728 unsigned char *p_new_head;
1729 const unsigned char *p_old_map;
1730 const unsigned char *p_new_map;
1731 int i_old_channel_count;
1732 int i_new_channel_count;
1733 int i_old_stream_count;
1734 int i_new_stream_count;
1735 int i_old_coupled_count;
1736 int i_new_coupled_count;
1737 p_old_head = (unsigned char *)pp_old_data[0];
1738 i_old_channel_count = i_old_stream_count = i_old_coupled_count = 0;
1739 p_old_map = default_map;
1740 if( pi_old_size[0] >= 19 && p_old_head[8] <= 15 )
1742 i_old_channel_count = p_old_head[9];
1743 switch( p_old_head[18] )
1746 i_old_stream_count = 1;
1747 i_old_coupled_count = i_old_channel_count - 1;
1750 if( pi_old_size[0] >= 21U + i_old_channel_count )
1752 i_old_stream_count = p_old_head[19];
1753 i_old_coupled_count = p_old_head[20];
1754 p_old_map = p_old_head + 21;
1759 p_new_head = (unsigned char *)pp_new_data[0];
1760 i_new_channel_count = i_new_stream_count = i_new_coupled_count = 0;
1761 p_new_map = default_map;
1762 if( pi_new_size[0] >= 19 && p_new_head[8] <= 15 )
1764 i_new_channel_count = p_new_head[9];
1765 switch( p_new_head[18] )
1768 i_new_stream_count = 1;
1769 i_new_coupled_count = i_new_channel_count - 1;
1772 if( pi_new_size[0] >= 21U + i_new_channel_count )
1774 i_new_stream_count = p_new_head[19];
1775 i_new_coupled_count = p_new_head[20];
1776 p_new_map = p_new_head+21;
1781 b_match = i_old_channel_count == i_new_channel_count &&
1782 i_old_stream_count == i_new_stream_count &&
1783 i_old_coupled_count == i_new_coupled_count &&
1784 memcmp(p_old_map, p_new_map,
1785 i_new_channel_count*sizeof(*p_new_map)) == 0;
1787 for( unsigned i = 0; i < i_new_count; i++ )
1788 free( pp_new_data[i] );
1789 for( unsigned i = 0; i < i_old_count; i++ )
1790 free( pp_old_data[i] );
1794 static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *p_stream )
1796 bool b_compatible = false;
1797 if( !p_stream->fmt_old.i_cat || !p_stream->fmt_old.i_codec )
1800 /* Only Vorbis and Opus are supported. */
1801 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS )
1802 b_compatible = Ogg_IsVorbisFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1803 else if( p_stream->fmt.i_codec == VLC_CODEC_OPUS )
1804 b_compatible = Ogg_IsOpusFormatCompatible( &p_stream->fmt, &p_stream->fmt_old );
1807 msg_Warn( p_demux, "cannot reuse old stream, resetting the decoder" );
1809 return !b_compatible;
1811 static void Ogg_ExtractXiphMeta( demux_t *p_demux, const void *p_headers, unsigned i_headers, unsigned i_skip )
1813 demux_sys_t *p_ogg = p_demux->p_sys;
1815 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
1816 void *pp_data[XIPH_MAX_HEADER_COUNT];
1818 if( xiph_SplitHeaders( pi_size, pp_data, &i_count, i_headers, p_headers ) )
1821 /* TODO how to handle multiple comments properly ? */
1822 if( i_count >= 2 && pi_size[1] > i_skip )
1823 vorbis_ParseComment( &p_ogg->p_meta, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip,
1824 &p_ogg->i_attachments, &p_ogg->attachments,
1825 &p_ogg->i_seekpoints, &p_ogg->pp_seekpoints );
1827 if( p_ogg->i_seekpoints > 1 )
1829 p_demux->info.i_update |= INPUT_UPDATE_TITLE_LIST;
1832 for( unsigned i = 0; i < i_count; i++ )
1835 static void Ogg_ExtractMeta( demux_t *p_demux, vlc_fourcc_t i_codec, const uint8_t *p_headers, int i_headers )
1837 demux_sys_t *p_ogg = p_demux->p_sys;
1841 /* 3 headers with the 2° one being the comments */
1842 case VLC_CODEC_VORBIS:
1843 case VLC_CODEC_THEORA:
1844 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+6 );
1846 case VLC_CODEC_OPUS:
1847 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 8 );
1849 case VLC_CODEC_SPEEX:
1850 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 0 );
1853 /* N headers with the 2° one being the comments */
1854 case VLC_CODEC_KATE:
1855 /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
1856 Ogg_ExtractXiphMeta( p_demux, p_headers, i_headers, 1+7+1 );
1860 case VLC_CODEC_FLAC:
1861 msg_Warn( p_demux, "Ogg_ExtractMeta does not support %4.4s", (const char*)&i_codec );
1865 case VLC_CODEC_CMML: /* CMML is XML text, doesn't have Vorbis comments */
1866 case VLC_CODEC_DIRAC:
1871 p_demux->info.i_update |= INPUT_UPDATE_META;
1874 static int64_t Ogg_GetLastPacket( demux_t *p_demux, logical_stream_t *p_stream,
1875 ogg_packet *p_oggpacket, double f_rate )
1877 int64_t last_packet = oggseek_get_last_frame( p_demux, p_stream );
1879 * Since there's quite a good chance that ogg_stream_packetout was called,
1880 * the given p_oggpacket may point to invalid data. Fill it with some valid ones
1882 ogg_stream_packetpeek( &p_stream->os, p_oggpacket );
1884 return ( last_packet >= 0 ) ? last_packet / f_rate : -1;
1887 static void Ogg_ReadTheoraHeader( demux_t *p_demux, logical_stream_t *p_stream,
1888 ogg_packet *p_oggpacket )
1891 int i_fps_numerator;
1892 int i_fps_denominator;
1893 int i_keyframe_frequency_force;
1899 p_stream->fmt.i_cat = VIDEO_ES;
1900 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
1902 /* Signal that we want to keep a backup of the theora
1903 * stream headers. They will be used when switching between
1905 p_stream->b_force_backup = 1;
1907 /* Cheat and get additionnal info ;) */
1908 bs_init( &bitstream, p_oggpacket->packet, p_oggpacket->bytes );
1909 bs_skip( &bitstream, 56 );
1911 i_major = bs_read( &bitstream, 8 ); /* major version num */
1912 i_minor = bs_read( &bitstream, 8 ); /* minor version num */
1913 i_subminor = bs_read( &bitstream, 8 ); /* subminor version num */
1915 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
1916 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
1917 bs_read( &bitstream, 24 ); /* frame width */
1918 bs_read( &bitstream, 24 ); /* frame height */
1919 bs_read( &bitstream, 8 ); /* x offset */
1920 bs_read( &bitstream, 8 ); /* y offset */
1922 i_fps_numerator = bs_read( &bitstream, 32 );
1923 i_fps_denominator = bs_read( &bitstream, 32 );
1924 bs_read( &bitstream, 24 ); /* aspect_numerator */
1925 bs_read( &bitstream, 24 ); /* aspect_denominator */
1927 p_stream->fmt.video.i_frame_rate = i_fps_numerator;
1928 p_stream->fmt.video.i_frame_rate_base = i_fps_denominator;
1930 bs_read( &bitstream, 8 ); /* colorspace */
1931 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
1932 bs_read( &bitstream, 6 ); /* quality */
1934 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
1936 /* granule_shift = i_log( frequency_force -1 ) */
1937 p_stream->i_granule_shift = 0;
1938 i_keyframe_frequency_force--;
1939 while( i_keyframe_frequency_force )
1941 p_stream->i_granule_shift++;
1942 i_keyframe_frequency_force >>= 1;
1945 i_version = i_major * 1000000 + i_minor * 1000 + i_subminor;
1946 p_stream->i_keyframe_offset = 0;
1947 p_stream->f_rate = ((float)i_fps_numerator) / i_fps_denominator;
1949 if ( i_version >= 3002001 )
1951 p_stream->i_keyframe_offset = 1;
1953 if ( p_demux->p_sys->i_length < 0 )
1955 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
1956 if ( last_packet >= 0 )
1957 p_demux->p_sys->i_length = last_packet;
1962 static void Ogg_ReadVorbisHeader( demux_t *p_demux, logical_stream_t *p_stream,
1963 ogg_packet *p_oggpacket )
1967 p_stream->fmt.i_cat = AUDIO_ES;
1968 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
1970 /* Signal that we want to keep a backup of the vorbis
1971 * stream headers. They will be used when switching between
1973 p_stream->b_force_backup = 1;
1975 /* Cheat and get additionnal info ;) */
1976 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
1977 oggpack_adv( &opb, 88 );
1978 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
1979 p_stream->f_rate = p_stream->fmt.audio.i_rate =
1980 oggpack_read( &opb, 32 );
1981 oggpack_adv( &opb, 32 );
1982 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
1984 if ( p_demux->p_sys->i_length < 0 )
1986 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
1987 if ( last_packet >= 0 )
1988 p_demux->p_sys->i_length = last_packet;
1992 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
1993 ogg_packet *p_oggpacket )
1997 p_stream->fmt.i_cat = AUDIO_ES;
1998 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2000 /* Signal that we want to keep a backup of the speex
2001 * stream headers. They will be used when switching between
2003 p_stream->b_force_backup = 1;
2005 /* Cheat and get additionnal info ;) */
2006 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2007 oggpack_adv( &opb, 224 );
2008 oggpack_adv( &opb, 32 ); /* speex_version_id */
2009 oggpack_adv( &opb, 32 ); /* header_size */
2010 p_stream->f_rate = p_stream->fmt.audio.i_rate = oggpack_read( &opb, 32 );
2011 oggpack_adv( &opb, 32 ); /* mode */
2012 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
2013 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
2014 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
2017 static void Ogg_ReadOpusHeader( demux_t *p_demux,
2018 logical_stream_t *p_stream,
2019 ogg_packet *p_oggpacket )
2023 p_stream->fmt.i_cat = AUDIO_ES;
2024 p_stream->fmt.i_codec = VLC_CODEC_OPUS;
2026 /* Signal that we want to keep a backup of the opus
2027 * stream headers. They will be used when switching between
2029 p_stream->b_force_backup = 1;
2031 /* All OggOpus streams are timestamped at 48kHz and
2032 * can be played at 48kHz. */
2033 p_stream->f_rate = p_stream->fmt.audio.i_rate = 48000;
2034 p_stream->fmt.i_bitrate = 0;
2036 /* Cheat and get additional info ;) */
2037 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2038 oggpack_adv( &opb, 64 );
2039 oggpack_adv( &opb, 8 ); /* version_id */
2040 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
2041 p_stream->i_pre_skip = oggpack_read( &opb, 16 );
2043 if ( p_demux->p_sys->i_length < 0 )
2045 int64_t last_packet = Ogg_GetLastPacket( p_demux, p_stream, p_oggpacket, p_stream->f_rate );
2046 if ( last_packet >= 0 )
2047 p_demux->p_sys->i_length = last_packet;
2051 static void Ogg_ReadFlacHeader( demux_t *p_demux, logical_stream_t *p_stream,
2052 ogg_packet *p_oggpacket )
2054 /* Parse the STREAMINFO metadata */
2057 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
2060 if( p_oggpacket->bytes > 0 && bs_read( &s, 7 ) == 0 )
2062 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
2065 p_stream->f_rate = p_stream->fmt.audio.i_rate = bs_read( &s, 20 );
2066 p_stream->fmt.audio.i_channels = bs_read( &s, 3 ) + 1;
2068 msg_Dbg( p_demux, "FLAC header, channels: %i, rate: %i",
2069 p_stream->fmt.audio.i_channels, (int)p_stream->f_rate );
2073 msg_Dbg( p_demux, "FLAC STREAMINFO metadata too short" );
2076 /* Fake this as the last metadata block */
2077 *((uint8_t*)p_oggpacket->packet) |= 0x80;
2081 /* This ain't a STREAMINFO metadata */
2082 msg_Dbg( p_demux, "Invalid FLAC STREAMINFO metadata" );
2086 static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
2087 ogg_packet *p_oggpacket )
2095 p_stream->fmt.i_cat = SPU_ES;
2096 p_stream->fmt.i_codec = VLC_CODEC_KATE;
2098 /* Signal that we want to keep a backup of the kate
2099 * stream headers. They will be used when switching between
2101 p_stream->b_force_backup = 1;
2103 /* Cheat and get additionnal info ;) */
2104 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2105 oggpack_adv( &opb, 11*8 ); /* packet type, kate magic, version */
2106 p_stream->i_kate_num_headers = oggpack_read( &opb, 8 );
2107 oggpack_adv( &opb, 3*8 );
2108 p_stream->i_granule_shift = oggpack_read( &opb, 8 );
2109 oggpack_adv( &opb, 8*8 ); /* reserved */
2110 gnum = oggpack_read( &opb, 32 );
2111 gden = oggpack_read( &opb, 32 );
2112 p_stream->f_rate = (double)gnum/gden;
2114 p_stream->fmt.psz_language = malloc(16);
2115 if( p_stream->fmt.psz_language )
2117 for( n = 0; n < 16; n++ )
2118 p_stream->fmt.psz_language[n] = oggpack_read(&opb,8);
2119 p_stream->fmt.psz_language[15] = 0; /* just in case */
2123 for( n = 0; n < 16; n++ )
2124 oggpack_read(&opb,8);
2126 p_stream->fmt.psz_description = malloc(16);
2127 if( p_stream->fmt.psz_description )
2129 for( n = 0; n < 16; n++ )
2130 p_stream->fmt.psz_description[n] = oggpack_read(&opb,8);
2131 p_stream->fmt.psz_description[15] = 0; /* just in case */
2133 /* Now find a localized user readable description for this category */
2134 psz_desc = strdup(FindKateCategoryName(p_stream->fmt.psz_description));
2137 free( p_stream->fmt.psz_description );
2138 p_stream->fmt.psz_description = psz_desc;
2143 for( n = 0; n < 16; n++ )
2144 oggpack_read(&opb,8);
2148 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,
2149 logical_stream_t *p_stream,
2150 ogg_packet *p_oggpacket )
2152 if( p_oggpacket->bytes >= 28 &&
2153 !memcmp( p_oggpacket->packet, "Annodex", 7 ) )
2157 uint16_t major_version;
2158 uint16_t minor_version;
2159 uint64_t timebase_numerator;
2160 uint64_t timebase_denominator;
2162 Ogg_ReadTheoraHeader( p_demux, p_stream, p_oggpacket );
2164 oggpack_readinit( &opb, p_oggpacket->packet, p_oggpacket->bytes);
2165 oggpack_adv( &opb, 8*8 ); /* "Annodex\0" header */
2166 major_version = oggpack_read( &opb, 2*8 ); /* major version */
2167 minor_version = oggpack_read( &opb, 2*8 ); /* minor version */
2168 timebase_numerator = GetQWLE( &p_oggpacket->packet[16] );
2169 timebase_denominator = GetQWLE( &p_oggpacket->packet[24] );
2171 else if( p_oggpacket->bytes >= 42 &&
2172 !memcmp( p_oggpacket->packet, "AnxData", 7 ) )
2174 uint64_t granule_rate_numerator;
2175 uint64_t granule_rate_denominator;
2176 char content_type_string[1024];
2178 /* Read in Annodex header fields */
2180 granule_rate_numerator = GetQWLE( &p_oggpacket->packet[8] );
2181 granule_rate_denominator = GetQWLE( &p_oggpacket->packet[16] );
2182 p_stream->i_secondary_header_packets =
2183 GetDWLE( &p_oggpacket->packet[24] );
2185 /* we are guaranteed that the first header field will be
2186 * the content-type (by the Annodex standard) */
2187 content_type_string[0] = '\0';
2188 if( !strncasecmp( (char*)(&p_oggpacket->packet[28]), "Content-Type: ", 14 ) )
2190 uint8_t *p = memchr( &p_oggpacket->packet[42], '\r',
2191 p_oggpacket->bytes - 1 );
2192 if( p && p[0] == '\r' && p[1] == '\n' )
2193 sscanf( (char*)(&p_oggpacket->packet[42]), "%1023s\r\n",
2194 content_type_string );
2197 msg_Dbg( p_demux, "AnxData packet info: %"PRId64" / %"PRId64", %d, ``%s''",
2198 granule_rate_numerator, granule_rate_denominator,
2199 p_stream->i_secondary_header_packets, content_type_string );
2201 p_stream->f_rate = (float) granule_rate_numerator /
2202 (float) granule_rate_denominator;
2204 /* What type of file do we have?
2205 * strcmp is safe to use here because we've extracted
2206 * content_type_string from the stream manually */
2207 if( !strncmp(content_type_string, "audio/x-wav", 11) )
2209 /* n.b. WAVs are unsupported right now */
2210 p_stream->fmt.i_cat = UNKNOWN_ES;
2212 else if( !strncmp(content_type_string, "audio/x-vorbis", 14) )
2214 p_stream->fmt.i_cat = AUDIO_ES;
2215 p_stream->fmt.i_codec = VLC_CODEC_VORBIS;
2217 p_stream->b_force_backup = 1;
2219 else if( !strncmp(content_type_string, "audio/x-speex", 13) )
2221 p_stream->fmt.i_cat = AUDIO_ES;
2222 p_stream->fmt.i_codec = VLC_CODEC_SPEEX;
2224 p_stream->b_force_backup = 1;
2226 else if( !strncmp(content_type_string, "video/x-theora", 14) )
2228 p_stream->fmt.i_cat = VIDEO_ES;
2229 p_stream->fmt.i_codec = VLC_CODEC_THEORA;
2231 p_stream->b_force_backup = 1;
2233 else if( !strncmp(content_type_string, "video/x-xvid", 12) )
2235 p_stream->fmt.i_cat = VIDEO_ES;
2236 p_stream->fmt.i_codec = VLC_FOURCC( 'x','v','i','d' );
2238 p_stream->b_force_backup = 1;
2240 else if( !strncmp(content_type_string, "video/mpeg", 10) )
2242 /* n.b. MPEG streams are unsupported right now */
2243 p_stream->fmt.i_cat = VIDEO_ES;
2244 p_stream->fmt.i_codec = VLC_CODEC_MPGV;
2246 else if( !strncmp(content_type_string, "text/x-cmml", 11) )
2248 ogg_stream_packetout( &p_stream->os, p_oggpacket );
2249 p_stream->fmt.i_cat = SPU_ES;
2250 p_stream->fmt.i_codec = VLC_CODEC_CMML;
2255 static uint32_t dirac_uint( bs_t *p_bs )
2257 uint32_t u_count = 0, u_value = 0;
2259 while( !bs_eof( p_bs ) && !bs_read( p_bs, 1 ) )
2263 u_value |= bs_read( p_bs, 1 );
2266 return (1<<u_count) - 1 + u_value;
2269 static int dirac_bool( bs_t *p_bs )
2271 return bs_read( p_bs, 1 );
2274 static bool Ogg_ReadDiracHeader( logical_stream_t *p_stream,
2275 ogg_packet *p_oggpacket )
2277 static const struct {
2278 uint32_t u_n /* numerator */, u_d /* denominator */;
2279 } p_dirac_frate_tbl[] = { /* table 10.3 */
2280 {1,1}, /* this first value is never used */
2281 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
2282 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
2284 static const size_t u_dirac_frate_tbl = sizeof(p_dirac_frate_tbl)/sizeof(*p_dirac_frate_tbl);
2286 static const uint32_t pu_dirac_vidfmt_frate[] = { /* table C.1 */
2287 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
2289 static const size_t u_dirac_vidfmt_frate = sizeof(pu_dirac_vidfmt_frate)/sizeof(*pu_dirac_vidfmt_frate);
2293 p_stream->i_granule_shift = 22; /* not 32 */
2295 /* Backing up stream headers is not required -- seqhdrs are repeated
2296 * thoughout the stream at suitable decoding start points */
2297 p_stream->b_force_backup = 0;
2299 /* read in useful bits from sequence header */
2300 bs_init( &bs, p_oggpacket->packet, p_oggpacket->bytes );
2301 bs_skip( &bs, 13*8); /* parse_info_header */
2302 dirac_uint( &bs ); /* major_version */
2303 dirac_uint( &bs ); /* minor_version */
2304 dirac_uint( &bs ); /* profile */
2305 dirac_uint( &bs ); /* level */
2307 uint32_t u_video_format = dirac_uint( &bs ); /* index */
2308 if( u_video_format >= u_dirac_vidfmt_frate )
2310 /* don't know how to parse this ogg dirac stream */
2314 if( dirac_bool( &bs ) )
2316 dirac_uint( &bs ); /* frame_width */
2317 dirac_uint( &bs ); /* frame_height */
2320 if( dirac_bool( &bs ) )
2322 dirac_uint( &bs ); /* chroma_format */
2325 if( dirac_bool( &bs ) )
2327 dirac_uint( &bs ); /* scan_format */
2330 uint32_t u_n = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_n;
2331 uint32_t u_d = p_dirac_frate_tbl[pu_dirac_vidfmt_frate[u_video_format]].u_d;
2332 if( dirac_bool( &bs ) )
2334 uint32_t u_frame_rate_index = dirac_uint( &bs );
2335 if( u_frame_rate_index >= u_dirac_frate_tbl )
2337 /* something is wrong with this stream */
2340 u_n = p_dirac_frate_tbl[u_frame_rate_index].u_n;
2341 u_d = p_dirac_frate_tbl[u_frame_rate_index].u_d;
2342 if( u_frame_rate_index == 0 )
2344 u_n = dirac_uint( &bs ); /* frame_rate_numerator */
2345 u_d = dirac_uint( &bs ); /* frame_rate_denominator */
2348 p_stream->f_rate = (float) u_n / u_d;
2350 /* probably is an ogg dirac es */
2351 p_stream->fmt.i_cat = VIDEO_ES;
2352 p_stream->fmt.i_codec = VLC_CODEC_DIRAC;