1 /*****************************************************************************
2 * ogg.c : ogg stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: ogg.c,v 1.50 2003/12/15 23:31:11 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
35 #define OGG_BLOCK_SIZE 4096
37 /*****************************************************************************
38 * Definitions of structures and functions used by this plugins
39 *****************************************************************************/
40 typedef struct logical_stream_s
42 ogg_stream_state os; /* logical stream of packets */
51 /* the header of some logical streams (eg vorbis) contain essential
52 * data for the decoder. We back them up here in case we need to re-feed
53 * them to the decoder. */
56 ogg_packet *p_packets_backup;
58 /* program clock reference (in units of 90kHz) derived from the previous
61 mtime_t i_interpolated_pcr;
62 mtime_t i_previous_pcr;
66 int i_theora_keyframe_granule_shift;
72 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
74 int i_streams; /* number of logical bitstreams */
75 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
77 /* program clock reference (in units of 90kHz) derived from the pcr of
80 int i_old_synchro_state;
86 /* OggDS headers for the new header format (used in ogm files) */
87 typedef struct stream_header_video
91 } stream_header_video;
93 typedef struct stream_header_audio
96 ogg_int16_t blockalign;
97 ogg_int32_t avgbytespersec;
98 } stream_header_audio;
100 typedef struct stream_header
105 ogg_int32_t size; /* size of the structure */
107 ogg_int64_t time_unit; /* in reference time */
108 ogg_int64_t samples_per_unit;
109 ogg_int32_t default_len; /* in media time */
111 ogg_int32_t buffersize;
112 ogg_int16_t bits_per_sample;
117 stream_header_video video;
119 stream_header_audio audio;
123 /* Some defines from OggDS */
124 #define PACKET_TYPE_HEADER 0x01
125 #define PACKET_TYPE_BITS 0x07
126 #define PACKET_LEN_BITS01 0xc0
127 #define PACKET_LEN_BITS2 0x02
128 #define PACKET_IS_SYNCPOINT 0x08
130 /*****************************************************************************
132 *****************************************************************************/
133 static int Activate ( vlc_object_t * );
134 static void Deactivate( vlc_object_t * );
135 static int Demux ( input_thread_t * );
136 static int Control ( input_thread_t *, int, va_list );
138 /* Bitstream manipulation */
139 static int Ogg_Check ( input_thread_t *p_input );
140 static int Ogg_ReadPage ( input_thread_t *, demux_sys_t *, ogg_page * );
141 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
142 static void Ogg_DecodePacket ( input_thread_t *p_input,
143 logical_stream_t *p_stream, ogg_packet * );
145 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg);
146 static int Ogg_FindLogicalStreams( input_thread_t *p_input,demux_sys_t *p_ogg);
147 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg );
149 /*****************************************************************************
151 *****************************************************************************/
153 set_description( _("ogg stream demuxer" ) );
154 set_capability( "demux", 50 );
155 set_callbacks( Activate, Deactivate );
156 add_shortcut( "ogg" );
159 /****************************************************************************
160 * Ogg_Check: Check we are dealing with an ogg stream.
161 ****************************************************************************/
162 static int Ogg_Check( input_thread_t *p_input )
165 int i_size = input_Peek( p_input, &p_peek, 4 );
167 /* Check for the Ogg capture pattern */
168 if( !(i_size>3) || !(p_peek[0] == 'O') || !(p_peek[1] == 'g') ||
169 !(p_peek[2] == 'g') || !(p_peek[3] == 'S') )
172 /* FIXME: Capture pattern might not be enough so we can also check for the
173 * the first complete page */
178 /****************************************************************************
179 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
180 ****************************************************************************
181 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
182 * are at the end of stream.
183 ****************************************************************************/
184 static int Ogg_ReadPage( input_thread_t *p_input, demux_sys_t *p_ogg,
185 ogg_page *p_oggpage )
188 data_packet_t *p_data;
191 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
193 i_read = input_SplitBuffer( p_input, &p_data, OGG_BLOCK_SIZE );
197 p_buffer = ogg_sync_buffer( &p_ogg->oy, i_read );
198 p_input->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, i_read );
199 ogg_sync_wrote( &p_ogg->oy, i_read );
200 input_DeletePacket( p_input->p_method_data, p_data );
206 /****************************************************************************
207 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
209 ****************************************************************************/
210 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
211 ogg_packet *p_oggpacket )
213 /* Convert the granulepos into a pcr */
214 if( p_oggpacket->granulepos >= 0 )
216 if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
218 p_stream->i_pcr = p_oggpacket->granulepos * 90000
223 ogg_int64_t iframe = p_oggpacket->granulepos >>
224 p_stream->i_theora_keyframe_granule_shift;
225 ogg_int64_t pframe = p_oggpacket->granulepos -
226 ( iframe << p_stream->i_theora_keyframe_granule_shift );
228 p_stream->i_pcr = ( iframe + pframe ) * 90000
232 p_stream->i_interpolated_pcr = p_stream->i_pcr;
236 p_stream->i_pcr = -1;
238 /* no granulepos available, try to interpolate the pcr.
239 * If we can't then don't touch the old value. */
240 if( p_stream->fmt.i_cat == VIDEO_ES )
241 /* 1 frame per packet */
242 p_stream->i_interpolated_pcr += (90000 / p_stream->f_rate);
243 else if( p_stream->fmt.i_bitrate )
244 p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
245 / p_stream->fmt.i_bitrate / 8 );
249 /****************************************************************************
250 * Ogg_DecodePacket: Decode an Ogg packet.
251 ****************************************************************************/
252 static void Ogg_DecodePacket( input_thread_t *p_input,
253 logical_stream_t *p_stream,
254 ogg_packet *p_oggpacket )
257 vlc_bool_t b_selected;
258 int i_header_len = 0;
262 if( !p_oggpacket->bytes )
264 msg_Dbg( p_input, "discarding 0 sized packet" );
268 if( p_stream->b_force_backup )
270 ogg_packet *p_packet_backup;
271 p_stream->i_packets_backup++;
272 switch( p_stream->fmt.i_codec )
274 case VLC_FOURCC( 'v','o','r','b' ):
275 case VLC_FOURCC( 's','p','x',' ' ):
276 case VLC_FOURCC( 't','h','e','o' ):
277 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
280 case VLC_FOURCC( 'f','l','a','c' ):
281 if( p_stream->i_packets_backup == 1 ) return;
282 else if( p_stream->i_packets_backup == 2 )
284 /* Parse the STREAMINFO metadata */
286 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
288 if( bs_read( &s, 7 ) == 0 )
290 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
293 p_stream->f_rate = p_stream->fmt.audio.i_rate =
295 p_stream->fmt.audio.i_channels =
296 bs_read( &s, 3 ) + 1;
298 msg_Dbg( p_input, "Flac header, channels: %i, rate: %i",
299 p_stream->fmt.audio.i_channels,
300 (int)p_stream->f_rate );
304 msg_Dbg( p_input, "FLAC STREAMINFO metadata too short" );
307 /* Store STREAMINFO for the decoder and packetizer */
308 p_stream->fmt.i_extra = p_oggpacket->bytes + 4;
309 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
310 memcpy( p_stream->fmt.p_extra, "fLaC", 4);
311 memcpy( ((uint8_t *)p_stream->fmt.p_extra) + 4,
312 p_oggpacket->packet, p_oggpacket->bytes );
314 /* Fake this as the last metadata block */
315 ((uint8_t*)p_stream->fmt.p_extra)[4] |= 0x80;
317 p_stream->p_es = es_out_Add( p_input->p_es_out,
322 /* This ain't a STREAMINFO metadata */
323 msg_Dbg( p_input, "Invalid FLAC STREAMINFO metadata" );
325 p_stream->b_force_backup = 0;
326 p_stream->i_packets_backup = 0;
328 if( p_oggpacket->granulepos >= 0 )
329 Ogg_UpdatePCR( p_stream, p_oggpacket );
331 p_stream->i_previous_pcr = 0;
337 p_stream->b_force_backup = 0;
341 /* Backup the ogg packet (likely an header packet) */
342 p_stream->p_packets_backup =
343 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
344 sizeof(ogg_packet) );
347 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
349 p_packet_backup->bytes = p_oggpacket->bytes;
350 p_packet_backup->granulepos = p_oggpacket->granulepos;
352 if( p_oggpacket->granulepos >= 0 )
354 /* Because of vorbis granulepos scheme we must set the pcr for the
355 * 1st header packet so it doesn't get discarded in the
357 Ogg_UpdatePCR( p_stream, p_oggpacket );
360 p_packet_backup->packet = malloc( p_oggpacket->bytes );
361 if( !p_packet_backup->packet ) return;
362 memcpy( p_packet_backup->packet, p_oggpacket->packet,
363 p_oggpacket->bytes );
366 /* Check the ES is selected */
367 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE,
368 p_stream->p_es, &b_selected );
370 if( b_selected && !p_stream->b_activated )
372 p_stream->b_activated = VLC_TRUE;
374 /* Newly activated stream, feed the backup headers to the decoder */
375 if( !p_stream->b_force_backup )
378 for( i = 0; i < p_stream->i_packets_backup; i++ )
380 /* Set correct starting date in header packets */
381 p_stream->p_packets_backup[i].granulepos =
382 p_stream->i_interpolated_pcr * p_stream->f_rate / 90000;
384 Ogg_DecodePacket( p_input, p_stream,
385 &p_stream->p_packets_backup[i] );
390 /* Convert the pcr into a pts */
391 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
392 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
393 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
395 if( p_stream->i_pcr >= 0 )
397 /* This is for streams where the granulepos of the header packets
398 * doesn't match these of the data packets (eg. ogg web radios). */
399 if( p_stream->i_previous_pcr == 0 &&
400 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
401 p_input->stream.p_selected_program->i_synchro_state =
404 p_stream->i_previous_pcr = p_stream->i_pcr;
406 /* Call the pace control */
407 if( p_input->stream.p_selected_program->i_synchro_state ==
409 input_ClockManageRef( p_input,
410 p_input->stream.p_selected_program,
414 /* The granulepos is the end date of the sample */
415 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
416 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
419 /* Convert the granulepos into the next pcr */
420 Ogg_UpdatePCR( p_stream, p_oggpacket );
424 /* Convert the granulepos into the current pcr */
425 Ogg_UpdatePCR( p_stream, p_oggpacket );
427 if( p_stream->i_pcr >= 0 )
429 /* This is for streams where the granulepos of the header packets
430 * doesn't match these of the data packets (eg. ogg web radios). */
431 if( p_stream->i_previous_pcr == 0 &&
432 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
433 p_input->stream.p_selected_program->i_synchro_state =
436 p_stream->i_previous_pcr = p_stream->i_pcr;
438 /* Call the pace control */
439 if( p_input->stream.p_selected_program->i_synchro_state ==
441 input_ClockManageRef( p_input,
442 p_input->stream.p_selected_program,
446 /* The granulepos is the start date of the sample */
447 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
448 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
454 /* This stream isn't currently selected so we don't need to decode it,
455 * but we did need to store its pcr as it might be selected later on */
456 p_stream->b_activated = VLC_FALSE;
460 if( !( p_block = block_New( p_input, p_oggpacket->bytes ) ) )
465 if( p_stream->fmt.i_cat == AUDIO_ES )
466 p_block->i_dts = p_block->i_pts = i_pts;
467 else if( p_stream->fmt.i_cat == SPU_ES )
469 p_block->i_pts = i_pts;
472 else if( p_stream->fmt.i_codec == VLC_FOURCC( 't','h','e','o' ) )
473 p_block->i_dts = p_block->i_pts = i_pts;
476 p_block->i_dts = i_pts;
480 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
481 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
482 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
483 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
484 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
486 /* Remove the header from the packet */
487 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
488 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
491 p_block->i_buffer -= i_header_len;
494 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
496 /* FIXME: the biggest hack I've ever done */
497 msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
498 p_block->i_pts, p_block->i_dts );
502 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
503 p_oggpacket->bytes - i_header_len );
505 es_out_Send( p_input->p_es_out, p_stream->p_es, p_block );
508 /****************************************************************************
509 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
510 * stream and fill p_ogg.
511 *****************************************************************************
512 * The initial page of a logical stream is marked as a 'bos' page.
513 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
514 * together and all of the initial pages must appear before any data pages.
516 * On success this function returns VLC_SUCCESS.
517 ****************************************************************************/
518 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
520 ogg_packet oggpacket;
524 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
526 while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
528 if( ogg_page_bos( &oggpage ) )
531 /* All is wonderful in our fine fine little world.
532 * We found the beginning of our first logical stream. */
533 while( ogg_page_bos( &oggpage ) )
537 realloc( p_ogg->pp_stream, p_ogg->i_streams *
538 sizeof(logical_stream_t *) );
540 p_stream = malloc( sizeof(logical_stream_t) );
541 memset( p_stream, 0, sizeof(logical_stream_t) );
543 es_format_Init( &p_stream->fmt, 0, 0 );
544 p_stream->b_activated = VLC_TRUE;
546 /* Setup the logical stream */
547 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
548 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
550 /* Extract the initial header from the first page and verify
551 * the codec type of tis Ogg bitstream */
552 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
554 /* error. stream version mismatch perhaps */
555 msg_Err( p_input, "Error reading first page of "
556 "Ogg bitstream data" );
560 /* FIXME: check return value */
561 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
563 /* Check for Vorbis header */
564 if( oggpacket.bytes >= 7 &&
565 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
569 msg_Dbg( p_input, "found vorbis header" );
570 p_stream->fmt.i_cat = AUDIO_ES;
571 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
573 /* Signal that we want to keep a backup of the vorbis
574 * stream headers. They will be used when switching between
576 p_stream->b_force_backup = 1;
578 /* Cheat and get additionnal info ;) */
579 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
580 oggpack_adv( &opb, 88 );
581 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
582 p_stream->f_rate = p_stream->fmt.audio.i_rate =
583 oggpack_read( &opb, 32 );
584 oggpack_adv( &opb, 32 );
585 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
587 /* Check for Speex header */
588 else if( oggpacket.bytes >= 7 &&
589 ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
593 p_stream->fmt.i_cat = AUDIO_ES;
594 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
596 /* Signal that we want to keep a backup of the vorbis
597 * stream headers. They will be used when switching between
599 p_stream->b_force_backup = 1;
601 /* Cheat and get additionnal info ;) */
602 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
603 oggpack_adv( &opb, 224 );
604 oggpack_adv( &opb, 32 ); /* speex_version_id */
605 oggpack_adv( &opb, 32 ); /* header_size */
606 p_stream->f_rate = p_stream->fmt.audio.i_rate =
607 oggpack_read( &opb, 32 );
608 oggpack_adv( &opb, 32 ); /* mode */
609 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
610 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
611 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
613 msg_Dbg( p_input, "found speex header, channels: %i, "
614 "rate: %i, bitrate: %i",
615 p_stream->fmt.audio.i_channels,
616 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
618 /* Check for Flac header */
619 else if( oggpacket.bytes >= 4 &&
620 ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
622 msg_Dbg( p_input, "found Flac header" );
624 /* Grrrr!!!! Did they really have to put all the
625 * important info in the second header packet!!!
626 * (STREAMINFO metadata is in the following packet) */
627 p_stream->b_force_backup = 1;
629 p_stream->fmt.i_cat = AUDIO_ES;
630 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
632 /* Check for Theora header */
633 else if( oggpacket.bytes >= 7 &&
634 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
638 int i_fps_denominator;
639 int i_keyframe_frequency_force;
641 msg_Dbg( p_input, "found theora header" );
642 p_stream->fmt.i_cat = VIDEO_ES;
643 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
645 /* Signal that we want to keep a backup of the vorbis
646 * stream headers. They will be used when switching between
648 p_stream->b_force_backup = 1;
650 /* Cheat and get additionnal info ;) */
651 bs_init( &bitstream, oggpacket.packet, oggpacket.bytes );
652 bs_skip( &bitstream, 56 );
653 bs_read( &bitstream, 8 ); /* major version num */
654 bs_read( &bitstream, 8 ); /* minor version num */
655 bs_read( &bitstream, 8 ); /* subminor version num */
656 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
657 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
658 bs_read( &bitstream, 24 ); /* frame width */
659 bs_read( &bitstream, 24 ); /* frame height */
660 bs_read( &bitstream, 8 ); /* x offset */
661 bs_read( &bitstream, 8 ); /* y offset */
663 i_fps_numerator = bs_read( &bitstream, 32 );
664 i_fps_denominator = bs_read( &bitstream, 32 );
665 bs_read( &bitstream, 24 ); /* aspect_numerator */
666 bs_read( &bitstream, 24 ); /* aspect_denominator */
667 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
668 bs_read( &bitstream, 8 ); /* colorspace */
669 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
670 bs_read( &bitstream, 6 ); /* quality */
672 /* granule_shift = i_log( frequency_force -1 ) */
673 p_stream->i_theora_keyframe_granule_shift = 0;
674 i_keyframe_frequency_force--;
675 while( i_keyframe_frequency_force )
677 p_stream->i_theora_keyframe_granule_shift++;
678 i_keyframe_frequency_force >>= 1;
681 p_stream->f_rate = ((float)i_fps_numerator) /
684 "found theora header, bitrate: %i, rate: %f",
685 p_stream->fmt.i_bitrate, p_stream->f_rate );
687 /* Check for Tarkin header */
688 else if( oggpacket.bytes >= 7 &&
689 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
693 msg_Dbg( p_input, "found tarkin header" );
694 p_stream->fmt.i_cat = VIDEO_ES;
695 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
697 /* Cheat and get additionnal info ;) */
698 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
699 oggpack_adv( &opb, 88 );
700 oggpack_adv( &opb, 104 );
701 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
702 p_stream->f_rate = 2; /* FIXME */
704 "found tarkin header, bitrate: %i, rate: %f",
705 p_stream->fmt.i_bitrate, p_stream->f_rate );
707 else if( oggpacket.bytes >= 142 &&
708 !strncmp( &oggpacket.packet[1],
709 "Direct Show Samples embedded in Ogg", 35 ))
711 /* Old header type */
713 /* Check for video header (old format) */
714 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
715 oggpacket.bytes >= 184 )
717 p_stream->fmt.i_cat = VIDEO_ES;
718 p_stream->fmt.i_codec =
719 VLC_FOURCC( oggpacket.packet[68],
720 oggpacket.packet[69],
721 oggpacket.packet[70],
722 oggpacket.packet[71] );
723 msg_Dbg( p_input, "found video header of type: %.4s",
724 (char *)&p_stream->fmt.i_codec );
726 p_stream->f_rate = 10000000.0 /
727 GetQWLE((oggpacket.packet+164));
728 p_stream->fmt.video.i_bits_per_pixel =
729 GetWLE((oggpacket.packet+182));
730 if( !p_stream->fmt.video.i_bits_per_pixel )
732 p_stream->fmt.video.i_bits_per_pixel = 24;
733 p_stream->fmt.video.i_width =
734 GetDWLE((oggpacket.packet+176));
735 p_stream->fmt.video.i_height =
736 GetDWLE((oggpacket.packet+180));
739 "fps: %f, width:%i; height:%i, bitcount:%i",
741 p_stream->fmt.video.i_width,
742 p_stream->fmt.video.i_height,
743 p_stream->fmt.video.i_bits_per_pixel);
746 /* Check for audio header (old format) */
747 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
749 unsigned int i_extra_size;
750 unsigned int i_format_tag;
752 p_stream->fmt.i_cat = AUDIO_ES;
754 i_extra_size = GetWLE((oggpacket.packet+140));
757 p_stream->fmt.i_extra = i_extra_size;
758 p_stream->fmt.p_extra = malloc( i_extra_size );
759 memcpy( p_stream->fmt.p_extra,
760 oggpacket.packet + 142, i_extra_size );
763 i_format_tag = GetWLE((oggpacket.packet+124));
764 p_stream->fmt.audio.i_channels =
765 GetWLE((oggpacket.packet+126));
766 p_stream->f_rate = p_stream->fmt.audio.i_rate =
767 GetDWLE((oggpacket.packet+128));
768 p_stream->fmt.i_bitrate =
769 GetDWLE((oggpacket.packet+132)) * 8;
770 p_stream->fmt.audio.i_blockalign =
771 GetWLE((oggpacket.packet+136));
772 p_stream->fmt.audio.i_bitspersample =
773 GetWLE((oggpacket.packet+138));
775 switch( i_format_tag )
777 case WAVE_FORMAT_PCM:
778 p_stream->fmt.i_codec =
779 VLC_FOURCC( 'a', 'r', 'a', 'w' );
781 case WAVE_FORMAT_MPEG:
782 case WAVE_FORMAT_MPEGLAYER3:
783 p_stream->fmt.i_codec =
784 VLC_FOURCC( 'm', 'p', 'g', 'a' );
786 case WAVE_FORMAT_A52:
787 p_stream->fmt.i_codec =
788 VLC_FOURCC( 'a', '5', '2', ' ' );
790 case WAVE_FORMAT_WMA1:
791 p_stream->fmt.i_codec =
792 VLC_FOURCC( 'w', 'm', 'a', '1' );
794 case WAVE_FORMAT_WMA2:
795 p_stream->fmt.i_codec =
796 VLC_FOURCC( 'w', 'm', 'a', '2' );
799 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
800 ( i_format_tag >> 8 ) & 0xff,
801 i_format_tag & 0xff );
804 msg_Dbg( p_input, "found audio header of type: %.4s",
805 (char *)&p_stream->fmt.i_codec );
806 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
807 "%dbits/sample %dkb/s",
809 p_stream->fmt.audio.i_channels,
810 p_stream->fmt.audio.i_rate,
811 p_stream->fmt.audio.i_bitspersample,
812 p_stream->fmt.i_bitrate / 1024 );
817 msg_Dbg( p_input, "stream %d has an old header "
818 "but is of an unknown type", p_ogg->i_streams-1 );
823 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
824 == PACKET_TYPE_HEADER &&
825 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
827 stream_header *st = (stream_header *)(oggpacket.packet+1);
829 /* Check for video header (new format) */
830 if( !strncmp( st->streamtype, "video", 5 ) )
832 p_stream->fmt.i_cat = VIDEO_ES;
834 /* We need to get rid of the header packet */
835 ogg_stream_packetout( &p_stream->os, &oggpacket );
837 p_stream->fmt.i_codec =
838 VLC_FOURCC( st->subtype[0], st->subtype[1],
839 st->subtype[2], st->subtype[3] );
840 msg_Dbg( p_input, "found video header of type: %.4s",
841 (char *)&p_stream->fmt.i_codec );
843 p_stream->f_rate = 10000000.0 /
844 GetQWLE(&st->time_unit);
845 p_stream->fmt.video.i_bits_per_pixel =
846 GetWLE(&st->bits_per_sample);
847 p_stream->fmt.video.i_width =
848 GetDWLE(&st->sh.video.width);
849 p_stream->fmt.video.i_height =
850 GetDWLE(&st->sh.video.height);
853 "fps: %f, width:%i; height:%i, bitcount:%i",
855 p_stream->fmt.video.i_width,
856 p_stream->fmt.video.i_height,
857 p_stream->fmt.video.i_bits_per_pixel );
859 /* Check for audio header (new format) */
860 else if( !strncmp( st->streamtype, "audio", 5 ) )
865 p_stream->fmt.i_cat = AUDIO_ES;
867 /* We need to get rid of the header packet */
868 ogg_stream_packetout( &p_stream->os, &oggpacket );
870 memcpy( p_buffer, st->subtype, 4 );
872 i_format_tag = strtol(p_buffer,NULL,16);
873 p_stream->fmt.audio.i_channels =
874 GetWLE(&st->sh.audio.channels);
875 p_stream->f_rate = p_stream->fmt.audio.i_rate =
876 GetQWLE(&st->samples_per_unit);
877 p_stream->fmt.i_bitrate =
878 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
879 p_stream->fmt.audio.i_blockalign =
880 GetWLE(&st->sh.audio.blockalign);
881 p_stream->fmt.audio.i_bitspersample =
882 GetWLE(&st->bits_per_sample);
884 switch( i_format_tag )
886 case WAVE_FORMAT_PCM:
887 p_stream->fmt.i_codec =
888 VLC_FOURCC( 'a', 'r', 'a', 'w' );
890 case WAVE_FORMAT_MPEG:
891 case WAVE_FORMAT_MPEGLAYER3:
892 p_stream->fmt.i_codec =
893 VLC_FOURCC( 'm', 'p', 'g', 'a' );
895 case WAVE_FORMAT_A52:
896 p_stream->fmt.i_codec =
897 VLC_FOURCC( 'a', '5', '2', ' ' );
899 case WAVE_FORMAT_WMA1:
900 p_stream->fmt.i_codec =
901 VLC_FOURCC( 'w', 'm', 'a', '1' );
903 case WAVE_FORMAT_WMA2:
904 p_stream->fmt.i_codec =
905 VLC_FOURCC( 'w', 'm', 'a', '2' );
908 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
909 ( i_format_tag >> 8 ) & 0xff,
910 i_format_tag & 0xff );
913 msg_Dbg( p_input, "found audio header of type: %.4s",
914 (char *)&p_stream->fmt.i_codec );
915 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
916 "%dbits/sample %dkb/s",
918 p_stream->fmt.audio.i_channels,
919 p_stream->fmt.audio.i_rate,
920 p_stream->fmt.audio.i_bitspersample,
921 p_stream->fmt.i_bitrate / 1024 );
923 /* Check for text (subtitles) header */
924 else if( !strncmp(st->streamtype, "text", 4) )
926 /* We need to get rid of the header packet */
927 ogg_stream_packetout( &p_stream->os, &oggpacket );
929 msg_Dbg( p_input, "found text subtitles header" );
930 p_stream->fmt.i_cat = SPU_ES;
931 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
932 p_stream->f_rate = 1000; /* granulepos is in milisec */
936 msg_Dbg( p_input, "stream %d has a header marker "
937 "but is of an unknown type", p_ogg->i_streams-1 );
944 msg_Dbg( p_input, "stream %d is of unknown type",
945 p_ogg->i_streams-1 );
950 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
954 /* This is the first data page, which means we are now finished
955 * with the initial pages. We just need to store it in the relevant
957 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
959 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
974 /*****************************************************************************
975 * Activate: initializes ogg demux structures
976 *****************************************************************************/
977 static int Activate( vlc_object_t * p_this )
979 input_thread_t *p_input = (input_thread_t *)p_this;
983 p_input->p_demux_data = NULL;
984 b_forced = ( ( *p_input->psz_demux )&&
985 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
987 /* Check if we are dealing with an ogg stream */
988 if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
992 if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
994 msg_Err( p_input, "out of memory" );
997 memset( p_ogg, 0, sizeof( demux_sys_t ) );
998 p_input->p_demux_data = p_ogg;
999 p_ogg->pp_stream = NULL;
1001 /* Initialize the Ogg physical bitstream parser */
1002 ogg_sync_init( &p_ogg->oy );
1004 /* Set exported functions */
1005 p_input->pf_demux = Demux;
1006 p_input->pf_demux_control = Control;
1008 /* Initialize access plug-in structures. */
1009 if( p_input->i_mtu == 0 )
1011 /* Improve speed. */
1012 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
1015 /* Create one program */
1016 vlc_mutex_lock( &p_input->stream.stream_lock );
1017 if( input_InitStream( p_input, 0 ) == -1)
1019 vlc_mutex_unlock( &p_input->stream.stream_lock );
1020 msg_Err( p_input, "cannot init stream" );
1023 if( input_AddProgram( p_input, 0, 0) == NULL )
1025 vlc_mutex_unlock( &p_input->stream.stream_lock );
1026 msg_Err( p_input, "cannot add program" );
1029 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1030 vlc_mutex_unlock( &p_input->stream.stream_lock );
1032 /* Begnning of stream, tell the demux to look for elementary streams. */
1035 p_ogg->i_old_synchro_state = !SYNCHRO_REINIT;
1040 Deactivate( (vlc_object_t *)p_input );
1045 /****************************************************************************
1046 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1047 * Elementary streams.
1048 ****************************************************************************/
1049 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg)
1053 /* Find the logical streams embedded in the physical stream and
1054 * initialize our p_ogg structure. */
1055 if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
1057 msg_Warn( p_input, "couldn't find any ogg logical stream" );
1058 return VLC_EGENERIC;
1061 vlc_mutex_lock( &p_input->stream.stream_lock );
1062 p_input->stream.i_mux_rate = 0;
1063 vlc_mutex_unlock( &p_input->stream.stream_lock );
1065 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1067 #define p_stream p_ogg->pp_stream[i_stream]
1068 if( p_stream->fmt.i_codec != VLC_FOURCC('f','l','a','c') )
1069 p_stream->p_es = es_out_Add( p_input->p_es_out, &p_stream->fmt );
1071 vlc_mutex_lock( &p_input->stream.stream_lock );
1072 p_input->stream.i_mux_rate += (p_stream->fmt.i_bitrate / ( 8 * 50 ));
1073 vlc_mutex_unlock( &p_input->stream.stream_lock );
1075 p_stream->i_pcr = p_stream->i_previous_pcr = -1;
1076 p_stream->b_reinit = 0;
1083 /****************************************************************************
1084 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1085 ****************************************************************************/
1086 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg )
1090 #define p_stream p_ogg->pp_stream[i_stream]
1091 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1093 if( p_stream->p_es )
1094 es_out_Del( p_input->p_es_out, p_stream->p_es );
1096 vlc_mutex_lock( &p_input->stream.stream_lock );
1097 p_input->stream.i_mux_rate -= (p_stream->fmt.i_bitrate / ( 8 * 50 ));
1098 vlc_mutex_unlock( &p_input->stream.stream_lock );
1100 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1101 for( j = 0; j < p_ogg->pp_stream[i_stream]->i_packets_backup; j++ )
1103 free( p_ogg->pp_stream[i_stream]->p_packets_backup[j].packet );
1105 if( p_ogg->pp_stream[i_stream]->p_packets_backup)
1106 free( p_ogg->pp_stream[i_stream]->p_packets_backup );
1108 free( p_ogg->pp_stream[i_stream] );
1113 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1114 p_ogg->pp_stream = NULL;
1115 p_ogg->i_streams = 0;
1118 /*****************************************************************************
1119 * Deactivate: frees unused data
1120 *****************************************************************************/
1121 static void Deactivate( vlc_object_t *p_this )
1123 input_thread_t *p_input = (input_thread_t *)p_this;
1124 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
1128 /* Cleanup the bitstream parser */
1129 ogg_sync_clear( &p_ogg->oy );
1131 Ogg_EndOfStream( p_input, p_ogg );
1137 /*****************************************************************************
1138 * Demux: reads and demuxes data packets
1139 *****************************************************************************
1140 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1141 *****************************************************************************/
1142 static int Demux( input_thread_t * p_input )
1144 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1146 ogg_packet oggpacket;
1149 #define p_stream p_ogg->pp_stream[i_stream]
1151 if( p_ogg->i_eos == p_ogg->i_streams )
1155 msg_Dbg( p_input, "end of a group of logical streams" );
1156 Ogg_EndOfStream( p_input, p_ogg );
1159 if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
1162 msg_Dbg( p_input, "beginning of a group of logical streams" );
1164 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
1165 input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
1168 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1170 msg_Warn( p_input, "synchro reinit" );
1172 if( p_ogg->i_old_synchro_state != SYNCHRO_REINIT )
1173 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1175 /* we'll trash all the data until we find the next pcr */
1176 p_stream->b_reinit = 1;
1177 p_stream->i_pcr = -1;
1178 p_stream->i_interpolated_pcr = -1;
1179 ogg_stream_reset( &p_stream->os );
1181 if( p_ogg->i_old_synchro_state != SYNCHRO_REINIT )
1182 ogg_sync_reset( &p_ogg->oy );
1186 * Demux an ogg page from the stream
1188 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1193 /* Test for End of Stream */
1194 if( ogg_page_eos( &oggpage ) ) p_ogg->i_eos++;
1197 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1199 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1202 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1204 if( p_stream->b_reinit )
1206 /* If synchro is re-initialized we need to drop all the packets
1207 * until we find a new dated one. */
1208 Ogg_UpdatePCR( p_stream, &oggpacket );
1210 if( p_stream->i_pcr >= 0 )
1212 p_stream->b_reinit = 0;
1216 p_stream->i_interpolated_pcr = -1;
1220 /* An Ogg/vorbis packet contains an end date granulepos */
1221 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
1222 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
1223 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
1225 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1227 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1231 input_ClockManageRef( p_input,
1232 p_input->stream.p_selected_program,
1239 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1244 i_stream = 0; p_ogg->i_pcr = -1;
1245 for( ; i_stream < p_ogg->i_streams; i_stream++ )
1247 if( p_stream->fmt.i_cat == SPU_ES )
1249 if( p_stream->i_interpolated_pcr < 0 )
1252 if( p_ogg->i_pcr < 0 || p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1253 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1256 if( p_ogg->i_pcr >= 0 )
1258 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1264 p_ogg->i_old_synchro_state =
1265 p_input->stream.p_selected_program->i_synchro_state;
1270 /*****************************************************************************
1272 *****************************************************************************/
1273 static int Control( input_thread_t *p_input, int i_query, va_list args )
1275 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1280 case DEMUX_GET_TIME:
1281 pi64 = (int64_t*)va_arg( args, int64_t * );
1282 *pi64 = p_ogg->i_pcr * 100 / 9;
1286 return demux_vaControlDefault( p_input, i_query, args );