1 /*****************************************************************************
2 * ogg.c : ogg stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ogg.c,v 1.39 2003/10/19 16:53:59 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 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
33 #include <sys/types.h>
37 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
39 #define OGG_BLOCK_SIZE 4096
41 /*****************************************************************************
42 * Definitions of structures and functions used by this plugins
43 *****************************************************************************/
44 typedef struct logical_stream_s
46 ogg_stream_state os; /* logical stream of packets */
49 int i_cat; /* AUDIO_ES, VIDEO_ES */
51 vlc_fourcc_t i_fourcc;
54 es_descriptor_t *p_es;
55 int b_selected; /* newly selected */
57 /* the header of some logical streams (eg vorbis) contain essential
58 * data for the decoder. We back them up here in case we need to re-feed
59 * them to the decoder. */
62 ogg_packet *p_packets_backup;
64 /* program clock reference (in units of 90kHz) derived from the previous
67 mtime_t i_interpolated_pcr;
68 mtime_t i_previous_pcr;
70 /* info from logical streams */
76 /* codec specific stuff */
77 BITMAPINFOHEADER *p_bih;
79 int i_theora_keyframe_granule_shift;
85 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
87 int i_streams; /* number of logical bitstreams */
88 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
90 /* current audio and video es */
91 logical_stream_t *p_stream_video;
92 logical_stream_t *p_stream_audio;
93 logical_stream_t *p_stream_spu;
95 /* program clock reference (in units of 90kHz) derived from the pcr of
104 /* OggDS headers for the new header format (used in ogm files) */
105 typedef struct stream_header_video
109 } stream_header_video;
111 typedef struct stream_header_audio
113 ogg_int16_t channels;
114 ogg_int16_t blockalign;
115 ogg_int32_t avgbytespersec;
116 } stream_header_audio;
118 typedef struct stream_header
123 ogg_int32_t size; /* size of the structure */
125 ogg_int64_t time_unit; /* in reference time */
126 ogg_int64_t samples_per_unit;
127 ogg_int32_t default_len; /* in media time */
129 ogg_int32_t buffersize;
130 ogg_int16_t bits_per_sample;
135 stream_header_video video;
137 stream_header_audio audio;
141 /* Some defines from OggDS */
142 #define PACKET_TYPE_HEADER 0x01
143 #define PACKET_TYPE_BITS 0x07
144 #define PACKET_LEN_BITS01 0xc0
145 #define PACKET_LEN_BITS2 0x02
146 #define PACKET_IS_SYNCPOINT 0x08
148 /*****************************************************************************
150 *****************************************************************************/
151 static int Activate ( vlc_object_t * );
152 static void Deactivate( vlc_object_t * );
153 static int Demux ( input_thread_t * );
154 static int Control ( input_thread_t *, int, va_list );
156 /* Stream managment */
157 static int Ogg_ElemStreamStart ( input_thread_t *, demux_sys_t *, int );
158 static void Ogg_ElemStreamStop ( input_thread_t *, demux_sys_t *, int );
160 /* Bitstream manipulation */
161 static int Ogg_Check ( input_thread_t *p_input );
162 static int Ogg_ReadPage ( input_thread_t *, demux_sys_t *, ogg_page * );
163 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
164 static void Ogg_DecodePacket ( input_thread_t *p_input,
165 logical_stream_t *p_stream, ogg_packet * );
167 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg);
168 static int Ogg_FindLogicalStreams( input_thread_t *p_input,demux_sys_t *p_ogg);
169 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg );
171 /*****************************************************************************
173 *****************************************************************************/
175 set_description( _("ogg stream demuxer" ) );
176 set_capability( "demux", 50 );
177 set_callbacks( Activate, Deactivate );
178 add_shortcut( "ogg" );
181 /*****************************************************************************
183 *****************************************************************************/
184 static int Ogg_ElemStreamStart( input_thread_t *p_input,
185 demux_sys_t *p_ogg, int i_stream )
187 #define p_stream p_ogg->pp_stream[i_stream]
188 if( !p_stream->p_es )
190 msg_Warn( p_input, "stream[%d] unselectable", i_stream );
193 if( p_stream->i_activated )
195 msg_Warn( p_input, "stream[%d] already selected", i_stream );
199 if( !p_stream->p_es->p_decoder_fifo )
201 vlc_mutex_lock( &p_input->stream.stream_lock );
202 input_SelectES( p_input, p_stream->p_es );
203 vlc_mutex_unlock( &p_input->stream.stream_lock );
205 p_stream->i_activated = p_stream->p_es->p_decoder_fifo ? 1 : 0;
207 /* Feed the backup header to the decoder */
208 if( !p_stream->b_force_backup )
211 for( i = 0; i < p_stream->i_packets_backup; i++ )
213 /* Set correct starting date in header packets */
214 p_stream->p_packets_backup[i].granulepos =
215 p_stream->i_interpolated_pcr * p_stream->f_rate / 90000;
217 Ogg_DecodePacket( p_input, p_stream,
218 &p_stream->p_packets_backup[i] );
222 return( p_stream->i_activated );
226 static void Ogg_ElemStreamStop( input_thread_t *p_input,
227 demux_sys_t *p_ogg, int i_stream )
229 #define p_stream p_ogg->pp_stream[i_stream]
231 if( !p_stream->i_activated )
233 msg_Warn( p_input, "stream[%d] already unselected", i_stream );
237 if( p_stream->p_es->p_decoder_fifo )
239 vlc_mutex_lock( &p_input->stream.stream_lock );
240 input_UnselectES( p_input, p_stream->p_es );
241 vlc_mutex_unlock( &p_input->stream.stream_lock );
244 p_stream->i_activated = 0;
249 /****************************************************************************
250 * Ogg_Check: Check we are dealing with an ogg stream.
251 ****************************************************************************/
252 static int Ogg_Check( input_thread_t *p_input )
255 int i_size = input_Peek( p_input, &p_peek, 4 );
257 /* Check for the Ogg capture pattern */
258 if( !(i_size>3) || !(p_peek[0] == 'O') || !(p_peek[1] == 'g') ||
259 !(p_peek[2] == 'g') || !(p_peek[3] == 'S') )
262 /* FIXME: Capture pattern might not be enough so we can also check for the
263 * the first complete page */
268 /****************************************************************************
269 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
270 ****************************************************************************
271 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
272 * are at the end of stream.
273 ****************************************************************************/
274 static int Ogg_ReadPage( input_thread_t *p_input, demux_sys_t *p_ogg,
275 ogg_page *p_oggpage )
278 data_packet_t *p_data;
281 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
283 i_read = input_SplitBuffer( p_input, &p_data, OGG_BLOCK_SIZE );
287 p_buffer = ogg_sync_buffer( &p_ogg->oy, i_read );
288 p_input->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, i_read );
289 ogg_sync_wrote( &p_ogg->oy, i_read );
290 input_DeletePacket( p_input->p_method_data, p_data );
296 /****************************************************************************
297 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
299 ****************************************************************************/
300 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
301 ogg_packet *p_oggpacket )
303 /* Convert the granulepos into a pcr */
304 if( p_oggpacket->granulepos >= 0 )
306 if( p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
308 p_stream->i_pcr = p_oggpacket->granulepos * 90000
313 ogg_int64_t iframe = p_oggpacket->granulepos >>
314 p_stream->i_theora_keyframe_granule_shift;
315 ogg_int64_t pframe = p_oggpacket->granulepos -
316 ( iframe << p_stream->i_theora_keyframe_granule_shift );
318 p_stream->i_pcr = ( iframe + pframe ) * 90000
322 p_stream->i_interpolated_pcr = p_stream->i_pcr;
326 p_stream->i_pcr = -1;
328 /* no granulepos available, try to interpolate the pcr.
329 * If we can't then don't touch the old value. */
330 if( p_stream->i_cat == VIDEO_ES )
331 /* 1 frame per packet */
332 p_stream->i_interpolated_pcr += (90000 / p_stream->f_rate);
333 else if( p_stream->i_bitrate )
334 p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
335 / p_stream->i_bitrate / 8 );
339 /****************************************************************************
340 * Ogg_DecodePacket: Decode an Ogg packet.
341 ****************************************************************************/
342 static void Ogg_DecodePacket( input_thread_t *p_input,
343 logical_stream_t *p_stream,
344 ogg_packet *p_oggpacket )
347 data_packet_t *p_data;
348 vlc_bool_t b_trash = VLC_FALSE;
349 int i_header_len = 0;
352 if( p_stream->b_force_backup )
354 ogg_packet *p_packet_backup;
355 p_stream->i_packets_backup++;
356 switch( p_stream->i_fourcc )
358 case VLC_FOURCC( 'v','o','r','b' ):
359 case VLC_FOURCC( 't','h','e','o' ):
360 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
364 p_stream->b_force_backup = 0;
368 /* Backup the ogg packet (likely an header packet) */
369 p_stream->p_packets_backup =
370 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
371 sizeof(ogg_packet) );
374 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
376 p_packet_backup->bytes = p_oggpacket->bytes;
377 p_packet_backup->granulepos = p_oggpacket->granulepos;
379 if( p_oggpacket->granulepos >= 0 )
381 /* Because of vorbis granulepos scheme we must set the pcr for the
382 * 1st header packet so it doesn't get discarded in the
384 Ogg_UpdatePCR( p_stream, p_oggpacket );
387 p_packet_backup->packet = malloc( p_oggpacket->bytes );
388 if( !p_packet_backup->packet ) return;
389 memcpy( p_packet_backup->packet, p_oggpacket->packet,
390 p_oggpacket->bytes );
393 vlc_mutex_lock( &p_input->stream.control.control_lock );
394 if( p_stream->i_cat == AUDIO_ES && p_input->stream.control.b_mute )
398 vlc_mutex_unlock( &p_input->stream.control.control_lock );
400 /* Convert the pcr into a pts */
401 if( p_stream->i_fourcc == VLC_FOURCC( 'v','o','r','b' ) )
403 if( p_stream->i_pcr >= 0 )
405 /* This is for streams where the granulepos of the header packets
406 * doesn't match these of the data packets (eg. ogg web radios). */
407 if( p_stream->i_previous_pcr == 0 &&
408 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
409 p_input->stream.p_selected_program->i_synchro_state =
412 p_stream->i_previous_pcr = p_stream->i_pcr;
414 /* Call the pace control */
415 if( p_input->stream.p_selected_program->i_synchro_state ==
417 input_ClockManageRef( p_input,
418 p_input->stream.p_selected_program,
422 /* The granulepos is the end date of the sample */
423 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
424 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
427 /* Convert the granulepos into the next pcr */
428 Ogg_UpdatePCR( p_stream, p_oggpacket );
432 /* Convert the granulepos into the current pcr */
433 Ogg_UpdatePCR( p_stream, p_oggpacket );
435 if( p_stream->i_pcr >= 0 )
437 /* This is for streams where the granulepos of the header packets
438 * doesn't match these of the data packets (eg. ogg web radios). */
439 if( p_stream->i_previous_pcr == 0 &&
440 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
441 p_input->stream.p_selected_program->i_synchro_state =
444 p_stream->i_previous_pcr = p_stream->i_pcr;
446 /* Call the pace control */
447 if( p_input->stream.p_selected_program->i_synchro_state ==
449 input_ClockManageRef( p_input,
450 p_input->stream.p_selected_program,
454 /* The granulepos is the start date of the sample */
455 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
456 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
460 if( !p_stream->p_es->p_decoder_fifo || b_trash )
462 /* This stream isn't currently selected so we don't need to decode it,
463 * but we did need to store its pcr as it might be selected later on */
467 if( !( p_pes = input_NewPES( p_input->p_method_data ) ) )
471 if( !( p_data = input_NewPacket( p_input->p_method_data,
472 p_oggpacket->bytes ) ) )
474 input_DeletePES( p_input->p_method_data, p_pes );
477 p_data->p_payload_end = p_data->p_payload_start + p_oggpacket->bytes;
479 p_pes->i_nb_data = 1;
480 p_pes->i_dts = p_pes->i_pts = i_pts;
481 p_pes->p_first = p_pes->p_last = p_data;
482 p_pes->i_pes_size = p_oggpacket->bytes;
484 if( p_stream->i_cat == SPU_ES ) p_pes->i_dts = 0;
486 if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) &&
487 p_stream->i_fourcc != VLC_FOURCC( 't','a','r','k' ) &&
488 p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
490 /* Remove the header from the packet */
491 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
492 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
495 p_pes->i_pes_size -= i_header_len;
498 if( p_stream->i_fourcc == VLC_FOURCC( 't','a','r','k' ) )
500 /* FIXME: the biggest hack I've ever done */
501 msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
502 p_pes->i_pts, p_pes->i_dts );
506 memcpy( p_data->p_payload_start,
507 p_oggpacket->packet + i_header_len,
508 p_oggpacket->bytes - i_header_len );
510 p_data->p_payload_end = p_data->p_payload_start + p_pes->i_pes_size;
511 p_data->b_discard_payload = 0;
513 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
516 /****************************************************************************
517 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
518 * stream and fill p_ogg.
519 *****************************************************************************
520 * The initial page of a logical stream is marked as a 'bos' page.
521 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
522 * together and all of the initial pages must appear before any data pages.
524 * On success this function returns VLC_SUCCESS.
525 ****************************************************************************/
526 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
528 ogg_packet oggpacket;
532 while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
534 if( ogg_page_bos( &oggpage ) )
537 /* All is wonderful in our fine fine little world.
538 * We found the beginning of our first logical stream. */
539 while( ogg_page_bos( &oggpage ) )
543 realloc( p_ogg->pp_stream, p_ogg->i_streams *
544 sizeof(logical_stream_t *) );
546 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
548 p_stream = malloc( sizeof(logical_stream_t) );
549 memset( p_stream, 0, sizeof(logical_stream_t) );
551 /* Setup the logical stream */
552 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
553 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
555 /* Extract the initial header from the first page and verify
556 * the codec type of tis Ogg bitstream */
557 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
559 /* error. stream version mismatch perhaps */
560 msg_Err( p_input, "Error reading first page of "
561 "Ogg bitstream data" );
565 /* FIXME: check return value */
566 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
568 /* Check for Vorbis header */
569 if( oggpacket.bytes >= 7 &&
570 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
574 msg_Dbg( p_input, "found vorbis header" );
575 p_stream->i_cat = AUDIO_ES;
576 p_stream->i_fourcc = VLC_FOURCC( 'v','o','r','b' );
578 /* Signal that we want to keep a backup of the vorbis
579 * stream headers. They will be used when switching between
581 p_stream->b_force_backup = 1;
583 /* Cheat and get additionnal info ;) */
584 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
585 oggpack_adv( &opb, 88 );
586 p_stream->i_channels = oggpack_read( &opb, 8 );
587 p_stream->f_rate = oggpack_read( &opb, 32 );
588 oggpack_adv( &opb, 32 );
589 p_stream->i_bitrate = oggpack_read( &opb, 32 );
591 char title[sizeof("Stream") + 10];
592 input_info_category_t *p_cat;
593 sprintf( title, "Stream %d", p_ogg->i_streams );
594 p_cat = input_InfoCategory( p_input, title );
595 input_AddInfo( p_cat, _("Type"), _("Audio") );
596 input_AddInfo( p_cat, _("Codec"), _("Vorbis") );
597 input_AddInfo( p_cat, _("Sample Rate"), "%f",
599 input_AddInfo( p_cat, _("Channels"), "%d",
600 p_stream->i_channels );
601 input_AddInfo( p_cat, _("Bit Rate"), "%d",
602 p_stream->i_bitrate );
605 /* Check for Theora header */
606 else if( oggpacket.bytes >= 7 &&
607 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
612 int i_fps_denominator;
613 int i_keyframe_frequency_force;
616 msg_Dbg( p_input, "found theora header" );
618 p_stream->i_cat = VIDEO_ES;
619 p_stream->i_fourcc = VLC_FOURCC( 't','h','e','o' );
621 /* Signal that we want to keep a backup of the vorbis
622 * stream headers. They will be used when switching between
624 p_stream->b_force_backup = 1;
626 /* Cheat and get additionnal info ;) */
627 oggpackB_readinit(&opb, oggpacket.packet, oggpacket.bytes);
628 oggpackB_adv( &opb, 56 );
629 oggpackB_read( &opb, 8 ); /* major version num */
630 oggpackB_read( &opb, 8 ); /* minor version num */
631 oggpackB_read( &opb, 8 ); /* subminor version num */
632 oggpackB_read( &opb, 16 ) /*<< 4*/; /* width */
633 oggpackB_read( &opb, 16 ) /*<< 4*/; /* height */
634 oggpackB_read( &opb, 24 ); /* frame width */
635 oggpackB_read( &opb, 24 ); /* frame height */
636 oggpackB_read( &opb, 8 ); /* x offset */
637 oggpackB_read( &opb, 8 ); /* y offset */
639 i_fps_numerator = oggpackB_read( &opb, 32 );
640 i_fps_denominator = oggpackB_read( &opb, 32 );
641 oggpackB_read( &opb, 24 ); /* aspect_numerator */
642 oggpackB_read( &opb, 24 ); /* aspect_denominator */
643 i_keyframe_frequency_force = 1 << oggpackB_read( &opb, 5 );
644 oggpackB_read( &opb, 8 ); /* colorspace */
645 p_stream->i_bitrate = oggpackB_read( &opb, 24 );
646 oggpackB_read( &opb, 6 ); /* quality */
648 /* granule_shift = i_log( frequency_force -1 ) */
649 p_stream->i_theora_keyframe_granule_shift = 0;
650 i_keyframe_frequency_force--;
651 while( i_keyframe_frequency_force )
653 p_stream->i_theora_keyframe_granule_shift++;
654 i_keyframe_frequency_force >>= 1;
657 p_stream->f_rate = ((float)i_fps_numerator) /
660 "found theora header, bitrate: %i, rate: %f",
661 p_stream->i_bitrate, p_stream->f_rate );
663 char title[sizeof("Stream") + 10];
664 input_info_category_t *p_cat;
665 sprintf( title, "Stream %d", p_ogg->i_streams );
666 p_cat = input_InfoCategory( p_input, title );
667 input_AddInfo( p_cat, _("Type"), _("Video") );
668 input_AddInfo( p_cat, _("Codec"), _("Theora") );
669 input_AddInfo( p_cat, _("Frame Rate"), "%f",
671 input_AddInfo( p_cat, _("Bit Rate"), "%d",
672 p_stream->i_bitrate );
674 #else /* HAVE_OGGPACKB */
675 msg_Dbg( p_input, "the ogg demuxer has been compiled "
676 "without support for the oggpackB extension."
677 "The theora stream won't be decoded." );
680 #endif /* HAVE_OGGPACKB */
682 /* Check for Tarkin header */
683 else if( oggpacket.bytes >= 7 &&
684 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
688 msg_Dbg( p_input, "found tarkin header" );
689 p_stream->i_cat = VIDEO_ES;
690 p_stream->i_fourcc = VLC_FOURCC( 't','a','r','k' );
692 /* Cheat and get additionnal info ;) */
693 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
694 oggpack_adv( &opb, 88 );
695 oggpack_adv( &opb, 104 );
696 p_stream->i_bitrate = oggpack_read( &opb, 32 );
697 p_stream->f_rate = 2; /* FIXME */
699 "found tarkin header, bitrate: %i, rate: %f",
700 p_stream->i_bitrate, p_stream->f_rate );
702 char title[sizeof("Stream") + 10];
703 input_info_category_t *p_cat;
704 sprintf( title, "Stream %d", p_ogg->i_streams );
705 p_cat = input_InfoCategory( p_input, title );
706 input_AddInfo( p_cat, _("Type"), _("Video") );
707 input_AddInfo( p_cat, _("Codec"), _("tarkin") );
708 input_AddInfo( p_cat, _("Sample Rate"), "%f",
710 input_AddInfo( p_cat, _("Bit Rate"), "%d",
711 p_stream->i_bitrate );
715 else if( oggpacket.bytes >= 142 &&
716 !strncmp( &oggpacket.packet[1],
717 "Direct Show Samples embedded in Ogg", 35 ))
719 /* Old header type */
721 /* Check for video header (old format) */
722 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
723 oggpacket.bytes >= 184 )
725 p_stream->i_cat = VIDEO_ES;
727 p_stream->p_bih = (BITMAPINFOHEADER *)
728 malloc( sizeof(BITMAPINFOHEADER) );
729 if( !p_stream->p_bih )
731 /* Mem allocation error, just ignore the stream */
736 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
737 p_stream->p_bih->biCompression= p_stream->i_fourcc =
738 VLC_FOURCC( oggpacket.packet[68],
739 oggpacket.packet[69],
740 oggpacket.packet[70],
741 oggpacket.packet[71] );
742 msg_Dbg( p_input, "found video header of type: %.4s",
743 (char *)&p_stream->i_fourcc );
745 p_stream->f_rate = 10000000.0 /
746 GetQWLE((oggpacket.packet+164));
747 p_stream->p_bih->biBitCount =
748 GetWLE((oggpacket.packet+182));
749 if( !p_stream->p_bih->biBitCount )
750 p_stream->p_bih->biBitCount=24; // hack, FIXME
751 p_stream->p_bih->biWidth =
752 GetDWLE((oggpacket.packet+176));
753 p_stream->p_bih->biHeight =
754 GetDWLE((oggpacket.packet+180));
755 p_stream->p_bih->biPlanes= 1 ;
756 p_stream->p_bih->biSizeImage =
757 (p_stream->p_bih->biBitCount >> 3) *
758 p_stream->p_bih->biWidth *
759 p_stream->p_bih->biHeight;
762 "fps: %f, width:%i; height:%i, bitcount:%i",
763 p_stream->f_rate, p_stream->p_bih->biWidth,
764 p_stream->p_bih->biHeight,
765 p_stream->p_bih->biBitCount);
767 char title[sizeof("Stream") + 10];
768 input_info_category_t *p_cat;
769 sprintf( title, "Stream %d", p_ogg->i_streams );
770 p_cat = input_InfoCategory( p_input, title );
771 input_AddInfo( p_cat, _("Type"), _("Video") );
772 input_AddInfo( p_cat, _("Codec"), "%.4s",
773 (char *)&p_stream->i_fourcc );
774 input_AddInfo( p_cat, _("Frame Rate"), "%f",
776 input_AddInfo( p_cat, _("Bit Count"), "%d",
777 p_stream->p_bih->biBitCount );
778 input_AddInfo( p_cat, _("Width"), "%d",
779 p_stream->p_bih->biWidth );
780 input_AddInfo( p_cat, _("Height"), "%d",
781 p_stream->p_bih->biHeight );
783 p_stream->i_bitrate = 0;
785 /* Check for audio header (old format) */
786 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
788 unsigned int i_extra_size;
790 p_stream->i_cat = AUDIO_ES;
792 i_extra_size = GetWLE((oggpacket.packet+140));
794 p_stream->p_wf = (WAVEFORMATEX *)
795 malloc( sizeof(WAVEFORMATEX) + i_extra_size );
796 if( !p_stream->p_wf )
798 /* Mem allocation error, just ignore the stream */
804 p_stream->p_wf->wFormatTag =
805 GetWLE((oggpacket.packet+124));
806 p_stream->p_wf->nChannels =
807 GetWLE((oggpacket.packet+126));
808 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
809 GetDWLE((oggpacket.packet+128));
810 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
811 GetDWLE((oggpacket.packet+132));
812 p_stream->i_bitrate *= 8;
813 p_stream->p_wf->nBlockAlign =
814 GetWLE((oggpacket.packet+136));
815 p_stream->p_wf->wBitsPerSample =
816 GetWLE((oggpacket.packet+138));
817 p_stream->p_wf->cbSize = i_extra_size;
819 if( i_extra_size > 0 )
820 memcpy( p_stream->p_wf+sizeof(WAVEFORMATEX),
821 oggpacket.packet+142, i_extra_size );
823 switch( p_stream->p_wf->wFormatTag )
825 case WAVE_FORMAT_PCM:
827 VLC_FOURCC( 'a', 'r', 'a', 'w' );
829 case WAVE_FORMAT_MPEG:
830 case WAVE_FORMAT_MPEGLAYER3:
832 VLC_FOURCC( 'm', 'p', 'g', 'a' );
834 case WAVE_FORMAT_A52:
836 VLC_FOURCC( 'a', '5', '2', ' ' );
838 case WAVE_FORMAT_WMA1:
840 VLC_FOURCC( 'w', 'm', 'a', '1' );
842 case WAVE_FORMAT_WMA2:
844 VLC_FOURCC( 'w', 'm', 'a', '2' );
847 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
848 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
849 p_stream->p_wf->wFormatTag & 0xff );
852 msg_Dbg( p_input, "found audio header of type: %.4s",
853 (char *)&p_stream->i_fourcc );
854 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
855 "%dbits/sample %dkb/s",
856 p_stream->p_wf->wFormatTag,
857 p_stream->p_wf->nChannels,
858 p_stream->p_wf->nSamplesPerSec,
859 p_stream->p_wf->wBitsPerSample,
860 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
862 char title[sizeof("Stream") + 10];
863 input_info_category_t *p_cat;
864 sprintf( title, "Stream %d", p_ogg->i_streams );
865 p_cat = input_InfoCategory( p_input, title );
866 input_AddInfo( p_cat, _("Type"), _("Audio") );
867 input_AddInfo( p_cat, _("Codec"), "%.4s",
868 (char *)&p_stream->i_fourcc );
869 input_AddInfo( p_cat, _("Sample Rate"), "%d",
870 p_stream->p_wf->nSamplesPerSec );
871 input_AddInfo( p_cat, _("Bit Rate"), "%d",
872 p_stream->p_wf->nAvgBytesPerSec * 8
874 input_AddInfo( p_cat, _("Channels"), "%d",
875 p_stream->p_wf->nChannels );
876 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
877 p_stream->p_wf->wBitsPerSample );
883 msg_Dbg( p_input, "stream %d has an old header "
884 "but is of an unknown type", p_ogg->i_streams-1 );
889 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
890 == PACKET_TYPE_HEADER &&
891 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
893 stream_header *st = (stream_header *)(oggpacket.packet+1);
895 /* Check for video header (new format) */
896 if( !strncmp( st->streamtype, "video", 5 ) )
898 p_stream->i_cat = VIDEO_ES;
900 /* We need to get rid of the header packet */
901 ogg_stream_packetout( &p_stream->os, &oggpacket );
903 p_stream->p_bih = (BITMAPINFOHEADER *)
904 malloc( sizeof(BITMAPINFOHEADER) );
905 if( !p_stream->p_bih )
907 /* Mem allocation error, just ignore the stream */
912 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
913 p_stream->p_bih->biCompression=
914 p_stream->i_fourcc = VLC_FOURCC( st->subtype[0],
918 msg_Dbg( p_input, "found video header of type: %.4s",
919 (char *)&p_stream->i_fourcc );
921 p_stream->f_rate = 10000000.0 /
922 GetQWLE(&st->time_unit);
923 p_stream->p_bih->biBitCount =
924 GetWLE(&st->bits_per_sample);
925 p_stream->p_bih->biWidth =
926 GetDWLE(&st->sh.video.width);
927 p_stream->p_bih->biHeight =
928 GetDWLE(&st->sh.video.height);
929 p_stream->p_bih->biPlanes= 1 ;
930 p_stream->p_bih->biSizeImage =
931 (p_stream->p_bih->biBitCount >> 3) *
932 p_stream->p_bih->biWidth *
933 p_stream->p_bih->biHeight;
936 "fps: %f, width:%i; height:%i, bitcount:%i",
937 p_stream->f_rate, p_stream->p_bih->biWidth,
938 p_stream->p_bih->biHeight,
939 p_stream->p_bih->biBitCount);
942 char title[sizeof("Stream") + 10];
943 input_info_category_t *p_cat;
944 sprintf( title, "Stream %d", p_ogg->i_streams );
945 p_cat = input_InfoCategory( p_input, title );
946 input_AddInfo( p_cat, _("Type"), _("Video") );
947 input_AddInfo( p_cat, _("Codec"), "%.4s",
948 (char *)&p_stream->i_fourcc );
949 input_AddInfo( p_cat, _("Frame Rate"), "%f",
951 input_AddInfo( p_cat, _("Bit Count"), "%d",
952 p_stream->p_bih->biBitCount );
953 input_AddInfo( p_cat, _("Width"), "%d",
954 p_stream->p_bih->biWidth );
955 input_AddInfo( p_cat, _("Height"), "%d",
956 p_stream->p_bih->biHeight );
958 p_stream->i_bitrate = 0;
960 /* Check for audio header (new format) */
961 else if( !strncmp( st->streamtype, "audio", 5 ) )
965 p_stream->i_cat = AUDIO_ES;
967 /* We need to get rid of the header packet */
968 ogg_stream_packetout( &p_stream->os, &oggpacket );
970 p_stream->p_wf = (WAVEFORMATEX *)
971 malloc( sizeof(WAVEFORMATEX) );
972 if( !p_stream->p_wf )
974 /* Mem allocation error, just ignore the stream */
980 memcpy( p_buffer, st->subtype, 4 );
982 p_stream->p_wf->wFormatTag = strtol(p_buffer,NULL,16);
983 p_stream->p_wf->nChannels =
984 GetWLE(&st->sh.audio.channels);
985 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
986 GetQWLE(&st->samples_per_unit);
987 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
988 GetDWLE(&st->sh.audio.avgbytespersec);
989 p_stream->i_bitrate *= 8;
990 p_stream->p_wf->nBlockAlign =
991 GetWLE(&st->sh.audio.blockalign);
992 p_stream->p_wf->wBitsPerSample =
993 GetWLE(&st->bits_per_sample);
994 p_stream->p_wf->cbSize = 0;
996 switch( p_stream->p_wf->wFormatTag )
998 case WAVE_FORMAT_PCM:
1000 VLC_FOURCC( 'a', 'r', 'a', 'w' );
1002 case WAVE_FORMAT_MPEG:
1003 case WAVE_FORMAT_MPEGLAYER3:
1004 p_stream->i_fourcc =
1005 VLC_FOURCC( 'm', 'p', 'g', 'a' );
1007 case WAVE_FORMAT_A52:
1008 p_stream->i_fourcc =
1009 VLC_FOURCC( 'a', '5', '2', ' ' );
1011 case WAVE_FORMAT_WMA1:
1012 p_stream->i_fourcc =
1013 VLC_FOURCC( 'w', 'm', 'a', '1' );
1015 case WAVE_FORMAT_WMA2:
1016 p_stream->i_fourcc =
1017 VLC_FOURCC( 'w', 'm', 'a', '2' );
1020 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
1021 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
1022 p_stream->p_wf->wFormatTag & 0xff );
1025 msg_Dbg( p_input, "found audio header of type: %.4s",
1026 (char *)&p_stream->i_fourcc );
1027 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
1028 "%dbits/sample %dkb/s",
1029 p_stream->p_wf->wFormatTag,
1030 p_stream->p_wf->nChannels,
1031 p_stream->p_wf->nSamplesPerSec,
1032 p_stream->p_wf->wBitsPerSample,
1033 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
1035 char title[sizeof("Stream") + 10];
1036 input_info_category_t *p_cat;
1037 sprintf( title, "Stream %d", p_ogg->i_streams );
1038 p_cat = input_InfoCategory( p_input, title );
1039 input_AddInfo( p_cat, _("Type"), _("Audio") );
1040 input_AddInfo( p_cat, _("Codec"), "%.4s",
1041 (char *)&p_stream->i_fourcc );
1042 input_AddInfo( p_cat, _("Sample Rate"), "%d",
1043 p_stream->p_wf->nSamplesPerSec );
1044 input_AddInfo( p_cat, _("Bit Rate"), "%d",
1045 p_stream->p_wf->nAvgBytesPerSec * 8
1047 input_AddInfo( p_cat, _("Channels"), "%d",
1048 p_stream->p_wf->nChannels );
1049 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
1050 p_stream->p_wf->wBitsPerSample );
1053 /* Check for text (subtitles) header */
1054 else if( !strncmp(st->streamtype, "text", 4) )
1056 /* We need to get rid of the header packet */
1057 ogg_stream_packetout( &p_stream->os, &oggpacket );
1059 msg_Dbg( p_input, "found text subtitles header" );
1060 p_stream->i_cat = SPU_ES;
1061 p_stream->i_fourcc =
1062 VLC_FOURCC( 's', 'u', 'b', 't' );
1063 p_stream->f_rate = 1000; /* granulepos is in milisec */
1067 msg_Dbg( p_input, "stream %d has a header marker "
1068 "but is of an unknown type", p_ogg->i_streams-1 );
1075 msg_Dbg( p_input, "stream %d is of unknown type",
1076 p_ogg->i_streams-1 );
1083 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1084 return VLC_EGENERIC;
1087 /* This is the first data page, which means we are now finished
1088 * with the initial pages. We just need to store it in the relevant
1090 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1092 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1101 return VLC_EGENERIC;
1104 /*****************************************************************************
1105 * Activate: initializes ogg demux structures
1106 *****************************************************************************/
1107 static int Activate( vlc_object_t * p_this )
1109 input_thread_t *p_input = (input_thread_t *)p_this;
1113 p_input->p_demux_data = NULL;
1114 b_forced = ( ( *p_input->psz_demux )&&
1115 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
1117 /* Check if we are dealing with an ogg stream */
1118 if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
1121 /* Allocate p_ogg */
1122 if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
1124 msg_Err( p_input, "out of memory" );
1127 memset( p_ogg, 0, sizeof( demux_sys_t ) );
1128 p_input->p_demux_data = p_ogg;
1129 p_ogg->pp_stream = NULL;
1130 p_ogg->p_stream_video = NULL;
1131 p_ogg->p_stream_audio = NULL;
1132 p_ogg->p_stream_spu = NULL;
1134 /* Initialize the Ogg physical bitstream parser */
1135 ogg_sync_init( &p_ogg->oy );
1137 /*Set exported functions */
1138 p_input->pf_demux = Demux;
1139 p_input->pf_demux_control = Control;
1141 /* Initialize access plug-in structures. */
1142 if( p_input->i_mtu == 0 )
1144 /* Improve speed. */
1145 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
1148 /* Create one program */
1149 vlc_mutex_lock( &p_input->stream.stream_lock );
1150 if( input_InitStream( p_input, 0 ) == -1)
1152 vlc_mutex_unlock( &p_input->stream.stream_lock );
1153 msg_Err( p_input, "cannot init stream" );
1156 if( input_AddProgram( p_input, 0, 0) == NULL )
1158 vlc_mutex_unlock( &p_input->stream.stream_lock );
1159 msg_Err( p_input, "cannot add program" );
1162 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1163 vlc_mutex_unlock( &p_input->stream.stream_lock );
1165 /* Begnning of stream, tell the demux to look for elementary streams. */
1166 p_ogg->b_eos = VLC_TRUE;
1171 Deactivate( (vlc_object_t *)p_input );
1176 /****************************************************************************
1177 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1178 * Elementary streams.
1179 ****************************************************************************/
1180 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg)
1184 /* Find the logical streams embedded in the physical stream and
1185 * initialize our p_ogg structure. */
1186 if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
1188 msg_Warn( p_input, "couldn't find any ogg logical stream" );
1189 return VLC_EGENERIC;
1192 vlc_mutex_lock( &p_input->stream.stream_lock );
1193 p_input->stream.i_mux_rate = 0;
1194 vlc_mutex_unlock( &p_input->stream.stream_lock );
1196 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1198 #define p_stream p_ogg->pp_stream[i_stream]
1199 vlc_mutex_lock( &p_input->stream.stream_lock );
1200 p_stream->p_es = input_AddES( p_input,
1201 p_input->stream.p_selected_program,
1203 p_stream->i_cat, NULL, 0 );
1204 p_input->stream.i_mux_rate += (p_stream->i_bitrate / ( 8 * 50 ));
1205 vlc_mutex_unlock( &p_input->stream.stream_lock );
1206 p_stream->p_es->i_stream_id = i_stream;
1207 p_stream->p_es->i_fourcc = p_stream->i_fourcc;
1208 p_stream->p_es->p_waveformatex = (void*)p_stream->p_wf;
1209 p_stream->p_es->p_bitmapinfoheader = (void*)p_stream->p_bih;
1211 p_stream->i_pcr = p_stream->i_previous_pcr = -1;
1212 p_stream->b_reinit = 0;
1216 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1218 #define p_stream p_ogg->pp_stream[i_stream]
1219 switch( p_stream->p_es->i_cat )
1222 if( (p_ogg->p_stream_video == NULL) )
1224 p_ogg->p_stream_video = p_stream;
1225 /* TODO add test to see if a decoder has been found */
1226 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1231 if( (p_ogg->p_stream_audio == NULL) )
1233 int i_audio = config_GetInt( p_input, "audio-channel" );
1234 if( i_audio == i_stream || i_audio <= 0 ||
1235 i_audio >= p_ogg->i_streams ||
1236 p_ogg->pp_stream[i_audio]->p_es->i_cat != AUDIO_ES )
1238 p_ogg->p_stream_audio = p_stream;
1239 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1245 if( (p_ogg->p_stream_spu == NULL) )
1247 /* for spu, default is none */
1248 int i_spu = config_GetInt( p_input, "spu-channel" );
1249 if( i_spu < 0 || i_spu >= p_ogg->i_streams ||
1250 p_ogg->pp_stream[i_spu]->p_es->i_cat != SPU_ES )
1254 else if( i_spu == i_stream )
1256 p_ogg->p_stream_spu = p_stream;
1257 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1268 /* we select the first audio and video ES */
1269 vlc_mutex_lock( &p_input->stream.stream_lock );
1270 if( !p_ogg->p_stream_video )
1272 msg_Warn( p_input, "no video stream found" );
1274 if( !p_ogg->p_stream_audio )
1276 msg_Warn( p_input, "no audio stream found!" );
1278 p_input->stream.p_selected_program->b_is_ok = 1;
1279 vlc_mutex_unlock( &p_input->stream.stream_lock );
1284 /****************************************************************************
1285 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1286 ****************************************************************************/
1287 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg )
1291 #define p_stream p_ogg->pp_stream[i_stream]
1292 vlc_mutex_lock( &p_input->stream.stream_lock );
1293 if( p_input->stream.i_pgrm_number )
1294 while( p_input->stream.p_selected_program->i_es_number )
1296 input_DelES( p_input,
1297 p_input->stream.p_selected_program->pp_es[0] );
1299 vlc_mutex_unlock( &p_input->stream.stream_lock );
1302 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1304 #define p_stream p_ogg->pp_stream[i_stream]
1305 vlc_mutex_lock( &p_input->stream.stream_lock );
1306 p_input->stream.i_mux_rate -= (p_stream->i_bitrate / ( 8 * 50 ));
1307 vlc_mutex_unlock( &p_input->stream.stream_lock );
1310 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1311 for( j = 0; j < p_ogg->pp_stream[i_stream]->i_packets_backup; j++ )
1313 free( p_ogg->pp_stream[i_stream]->p_packets_backup[j].packet );
1315 if( p_ogg->pp_stream[i_stream]->p_packets_backup)
1316 free( p_ogg->pp_stream[i_stream]->p_packets_backup );
1318 #if 0 /* hmmm, it's already freed in input_DelES() */
1319 if( p_ogg->pp_stream[i]->p_bih )
1320 free( p_ogg->pp_stream[i]->p_bih );
1321 if( p_ogg->pp_stream[i]->p_wf )
1322 free( p_ogg->pp_stream[i]->p_wf );
1325 free( p_ogg->pp_stream[i_stream] );
1329 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1330 p_ogg->pp_stream = NULL;
1331 p_ogg->i_streams = 0;
1332 p_ogg->p_stream_video = NULL;
1333 p_ogg->p_stream_audio = NULL;
1334 p_ogg->p_stream_spu = NULL;
1337 /*****************************************************************************
1338 * Deactivate: frees unused data
1339 *****************************************************************************/
1340 static void Deactivate( vlc_object_t *p_this )
1342 input_thread_t *p_input = (input_thread_t *)p_this;
1343 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
1347 /* Cleanup the bitstream parser */
1348 ogg_sync_clear( &p_ogg->oy );
1350 Ogg_EndOfStream( p_input, p_ogg );
1356 /*****************************************************************************
1357 * Demux: reads and demuxes data packets
1358 *****************************************************************************
1359 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1360 *****************************************************************************/
1361 static int Demux( input_thread_t * p_input )
1363 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1365 ogg_packet oggpacket;
1370 if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
1371 p_ogg->b_eos = VLC_FALSE;
1373 msg_Dbg( p_input, "beginning of a group of logical streams" );
1375 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
1376 input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
1379 #define p_stream p_ogg->pp_stream[i_stream]
1381 /* detect new selected/unselected streams */
1382 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1384 if( p_stream->p_es )
1386 if( p_stream->p_es->p_decoder_fifo &&
1387 !p_stream->i_activated )
1389 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1392 if( !p_stream->p_es->p_decoder_fifo &&
1393 p_stream->i_activated )
1395 Ogg_ElemStreamStop( p_input, p_ogg, i_stream );
1400 /* search for new video and audio stream to select
1401 * if current have been unselected */
1402 if( ( !p_ogg->p_stream_video )
1403 || ( !p_ogg->p_stream_video->p_es->p_decoder_fifo ) )
1405 p_ogg->p_stream_video = NULL;
1406 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1408 if( ( p_stream->i_cat == VIDEO_ES )
1409 &&( p_stream->p_es->p_decoder_fifo ) )
1411 p_ogg->p_stream_video = p_stream;
1416 if( ( !p_ogg->p_stream_audio )
1417 ||( !p_ogg->p_stream_audio->p_es->p_decoder_fifo ) )
1419 p_ogg->p_stream_audio = NULL;
1420 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1422 if( ( p_stream->i_cat == AUDIO_ES )
1423 &&( p_stream->p_es->p_decoder_fifo ) )
1425 p_ogg->p_stream_audio = p_stream;
1431 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1433 msg_Warn( p_input, "synchro reinit" );
1435 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1437 /* we'll trash all the data until we find the next pcr */
1438 p_stream->b_reinit = 1;
1439 p_stream->i_pcr = -1;
1440 p_stream->i_interpolated_pcr = -1;
1446 * Demux an ogg page from the stream
1448 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1453 /* Test for End of Stream */
1454 if( ogg_page_eos( &oggpage ) )
1456 msg_Dbg( p_input, "end of a group of logical streams" );
1458 Ogg_EndOfStream( p_input, p_ogg );
1459 p_ogg->b_eos = VLC_TRUE;
1463 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1465 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1468 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1470 if( !p_stream->p_es )
1475 if( p_stream->b_reinit )
1477 /* If synchro is re-initialized we need to drop all the packets
1478 * until we find a new dated one. */
1479 Ogg_UpdatePCR( p_stream, &oggpacket );
1481 if( p_stream->i_pcr >= 0 )
1483 p_stream->b_reinit = 0;
1487 p_stream->i_interpolated_pcr = -1;
1491 /* An Ogg/vorbis packet contains an end date granulepos */
1492 if( p_stream->i_fourcc == VLC_FOURCC( 'v','o','r','b' ) )
1494 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1496 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1502 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1507 i_stream = 0; p_ogg->i_pcr = -1;
1508 for( ; i_stream < p_ogg->i_streams; i_stream++ )
1510 if( p_stream->i_cat == SPU_ES )
1512 if( p_stream->i_interpolated_pcr < 0 )
1515 if( p_ogg->i_pcr < 0 || p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1516 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1519 if( p_input->stream.p_selected_program->i_synchro_state != SYNCHRO_REINIT )
1521 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1530 /*****************************************************************************
1532 *****************************************************************************/
1533 static int Control( input_thread_t *p_input, int i_query, va_list args )
1535 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1540 case DEMUX_GET_TIME:
1541 pi64 = (int64_t*)va_arg( args, int64_t * );
1542 *pi64 = p_ogg->i_pcr * 100 / 9;
1546 return demux_vaControlDefault( p_input, i_query, args );