1 /*****************************************************************************
2 * ogg.c : ogg stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ogg.c,v 1.36 2003/09/27 15:33:02 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 the last header packet */
214 if( i == p_stream->i_packets_backup -1 )
215 p_stream->p_packets_backup[i].granulepos =
216 p_stream->i_interpolated_pcr * p_stream->f_rate / 90000;
218 Ogg_DecodePacket( p_input, p_stream,
219 &p_stream->p_packets_backup[i] );
223 return( p_stream->i_activated );
227 static void Ogg_ElemStreamStop( input_thread_t *p_input,
228 demux_sys_t *p_ogg, int i_stream )
230 #define p_stream p_ogg->pp_stream[i_stream]
232 if( !p_stream->i_activated )
234 msg_Warn( p_input, "stream[%d] already unselected", i_stream );
238 if( p_stream->p_es->p_decoder_fifo )
240 vlc_mutex_lock( &p_input->stream.stream_lock );
241 input_UnselectES( p_input, p_stream->p_es );
242 vlc_mutex_unlock( &p_input->stream.stream_lock );
245 p_stream->i_activated = 0;
250 /****************************************************************************
251 * Ogg_Check: Check we are dealing with an ogg stream.
252 ****************************************************************************/
253 static int Ogg_Check( input_thread_t *p_input )
256 int i_size = input_Peek( p_input, &p_peek, 4 );
258 /* Check for the Ogg capture pattern */
259 if( !(i_size>3) || !(p_peek[0] == 'O') || !(p_peek[1] == 'g') ||
260 !(p_peek[2] == 'g') || !(p_peek[3] == 'S') )
263 /* FIXME: Capture pattern might not be enough so we can also check for the
264 * the first complete page */
269 /****************************************************************************
270 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
271 ****************************************************************************
272 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
273 * are at the end of stream.
274 ****************************************************************************/
275 static int Ogg_ReadPage( input_thread_t *p_input, demux_sys_t *p_ogg,
276 ogg_page *p_oggpage )
279 data_packet_t *p_data;
282 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
284 i_read = input_SplitBuffer( p_input, &p_data, OGG_BLOCK_SIZE );
288 p_buffer = ogg_sync_buffer( &p_ogg->oy, i_read );
289 p_input->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, i_read );
290 ogg_sync_wrote( &p_ogg->oy, i_read );
291 input_DeletePacket( p_input->p_method_data, p_data );
297 /****************************************************************************
298 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
300 ****************************************************************************/
301 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
302 ogg_packet *p_oggpacket )
304 /* Convert the granulepos into a pcr */
305 if( p_oggpacket->granulepos >= 0 )
307 if( p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
309 p_stream->i_pcr = p_oggpacket->granulepos * 90000
314 ogg_int64_t iframe = p_oggpacket->granulepos >>
315 p_stream->i_theora_keyframe_granule_shift;
316 ogg_int64_t pframe = p_oggpacket->granulepos -
317 ( iframe << p_stream->i_theora_keyframe_granule_shift );
319 p_stream->i_pcr = ( iframe + pframe ) * 90000
323 p_stream->i_interpolated_pcr = p_stream->i_pcr;
327 p_stream->i_pcr = -1;
329 /* no granulepos available, try to interpolate the pcr.
330 * If we can't then don't touch the old value. */
331 if( p_stream->i_cat == VIDEO_ES )
332 /* 1 frame per packet */
333 p_stream->i_interpolated_pcr += (90000 / p_stream->f_rate);
334 else if( p_stream->i_bitrate )
335 p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
336 / p_stream->i_bitrate / 8 );
340 /****************************************************************************
341 * Ogg_DecodePacket: Decode an Ogg packet.
342 ****************************************************************************/
343 static void Ogg_DecodePacket( input_thread_t *p_input,
344 logical_stream_t *p_stream,
345 ogg_packet *p_oggpacket )
348 data_packet_t *p_data;
349 vlc_bool_t b_trash = VLC_FALSE;
350 int i_header_len = 0;
353 if( p_stream->b_force_backup )
355 ogg_packet *p_packet_backup;
356 p_stream->i_packets_backup++;
357 switch( p_stream->i_fourcc )
359 case VLC_FOURCC( 'v','o','r','b' ):
360 case VLC_FOURCC( 't','h','e','o' ):
361 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
365 p_stream->b_force_backup = 0;
369 /* Backup the ogg packet (likely an header packet) */
370 p_stream->p_packets_backup =
371 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
372 sizeof(ogg_packet) );
375 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
377 p_packet_backup->bytes = p_oggpacket->bytes;
378 if( p_stream->b_force_backup ) p_oggpacket->granulepos = -1;
379 p_packet_backup->granulepos = p_oggpacket->granulepos;
380 p_packet_backup->packet = malloc( p_oggpacket->bytes );
381 if( !p_packet_backup->packet ) return;
382 memcpy( p_packet_backup->packet, p_oggpacket->packet,
383 p_oggpacket->bytes );
386 vlc_mutex_lock( &p_input->stream.control.control_lock );
387 if( p_stream->i_cat == AUDIO_ES && p_input->stream.control.b_mute )
391 vlc_mutex_unlock( &p_input->stream.control.control_lock );
393 /* Convert the pcr into a pts */
394 if( p_stream->i_fourcc == VLC_FOURCC( 'v','o','r','b' ) )
396 if( p_stream->i_pcr >= 0 )
398 /* This is for streams where the granulepos of the header packets
399 * doesn't match these of the data packets (eg. ogg web radios). */
400 if( p_stream->i_previous_pcr == 0 &&
401 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
402 p_input->stream.p_selected_program->i_synchro_state =
405 p_stream->i_previous_pcr = p_stream->i_pcr;
407 /* Call the pace control */
408 if( p_input->stream.p_selected_program->i_synchro_state ==
410 input_ClockManageRef( p_input,
411 p_input->stream.p_selected_program,
415 /* The granulepos is the end date of the sample */
416 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
417 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
420 /* Convert the granulepos into the next pcr */
421 Ogg_UpdatePCR( p_stream, p_oggpacket );
425 /* Convert the granulepos into the current pcr */
426 Ogg_UpdatePCR( p_stream, p_oggpacket );
428 if( p_stream->i_pcr >= 0 )
430 /* This is for streams where the granulepos of the header packets
431 * doesn't match these of the data packets (eg. ogg web radios). */
432 if( p_stream->i_previous_pcr == 0 &&
433 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
434 p_input->stream.p_selected_program->i_synchro_state =
437 p_stream->i_previous_pcr = p_stream->i_pcr;
439 /* Call the pace control */
440 if( p_input->stream.p_selected_program->i_synchro_state ==
442 input_ClockManageRef( p_input,
443 p_input->stream.p_selected_program,
447 /* The granulepos is the start date of the sample */
448 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
449 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
453 if( !p_stream->p_es->p_decoder_fifo || b_trash )
455 /* This stream isn't currently selected so we don't need to decode it,
456 * but we did need to store its pcr as it might be selected later on */
460 if( !( p_pes = input_NewPES( p_input->p_method_data ) ) )
464 if( !( p_data = input_NewPacket( p_input->p_method_data,
465 p_oggpacket->bytes ) ) )
467 input_DeletePES( p_input->p_method_data, p_pes );
470 p_data->p_payload_end = p_data->p_payload_start + p_oggpacket->bytes;
472 p_pes->i_nb_data = 1;
473 p_pes->i_dts = p_pes->i_pts = i_pts;
474 p_pes->p_first = p_pes->p_last = p_data;
475 p_pes->i_pes_size = p_oggpacket->bytes;
477 if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) &&
478 p_stream->i_fourcc != VLC_FOURCC( 't','a','r','k' ) &&
479 p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
481 /* Remove the header from the packet */
482 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
483 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
486 p_pes->i_pes_size -= i_header_len;
489 if( p_stream->i_fourcc == VLC_FOURCC( 't','a','r','k' ) )
491 /* FIXME: the biggest hack I've ever done */
492 msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
493 p_pes->i_pts, p_pes->i_dts );
497 memcpy( p_data->p_payload_start,
498 p_oggpacket->packet + i_header_len,
499 p_oggpacket->bytes - i_header_len );
501 p_data->p_payload_end = p_data->p_payload_start + p_pes->i_pes_size;
502 p_data->b_discard_payload = 0;
504 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
507 /****************************************************************************
508 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
509 * stream and fill p_ogg.
510 *****************************************************************************
511 * The initial page of a logical stream is marked as a 'bos' page.
512 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
513 * together and all of the initial pages must appear before any data pages.
515 * On success this function returns VLC_SUCCESS.
516 ****************************************************************************/
517 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
519 ogg_packet oggpacket;
523 while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
525 if( ogg_page_bos( &oggpage ) )
528 /* All is wonderful in our fine fine little world.
529 * We found the beginning of our first logical stream. */
530 while( ogg_page_bos( &oggpage ) )
534 realloc( p_ogg->pp_stream, p_ogg->i_streams *
535 sizeof(logical_stream_t *) );
537 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
539 p_stream = malloc( sizeof(logical_stream_t) );
540 memset( p_stream, 0, sizeof(logical_stream_t) );
542 /* Setup the logical stream */
543 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
544 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
546 /* Extract the initial header from the first page and verify
547 * the codec type of tis Ogg bitstream */
548 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
550 /* error. stream version mismatch perhaps */
551 msg_Err( p_input, "Error reading first page of "
552 "Ogg bitstream data" );
556 /* FIXME: check return value */
557 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
559 /* Check for Vorbis header */
560 if( oggpacket.bytes >= 7 &&
561 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
565 msg_Dbg( p_input, "found vorbis header" );
566 p_stream->i_cat = AUDIO_ES;
567 p_stream->i_fourcc = VLC_FOURCC( 'v','o','r','b' );
569 /* Signal that we want to keep a backup of the vorbis
570 * stream headers. They will be used when switching between
572 p_stream->b_force_backup = 1;
574 /* Cheat and get additionnal info ;) */
575 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
576 oggpack_adv( &opb, 88 );
577 p_stream->i_channels = oggpack_read( &opb, 8 );
578 p_stream->f_rate = oggpack_read( &opb, 32 );
579 oggpack_adv( &opb, 32 );
580 p_stream->i_bitrate = oggpack_read( &opb, 32 );
582 char title[sizeof("Stream") + 10];
583 input_info_category_t *p_cat;
584 sprintf( title, "Stream %d", p_ogg->i_streams );
585 p_cat = input_InfoCategory( p_input, title );
586 input_AddInfo( p_cat, _("Type"), _("Audio") );
587 input_AddInfo( p_cat, _("Codec"), _("Vorbis") );
588 input_AddInfo( p_cat, _("Sample Rate"), "%f",
590 input_AddInfo( p_cat, _("Channels"), "%d",
591 p_stream->i_channels );
592 input_AddInfo( p_cat, _("Bit Rate"), "%d",
593 p_stream->i_bitrate );
596 /* Check for Theora header */
597 else if( oggpacket.bytes >= 7 &&
598 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
603 int i_fps_denominator;
604 int i_keyframe_frequency_force;
607 msg_Dbg( p_input, "found theora header" );
609 p_stream->i_cat = VIDEO_ES;
610 p_stream->i_fourcc = VLC_FOURCC( 't','h','e','o' );
612 /* Signal that we want to keep a backup of the vorbis
613 * stream headers. They will be used when switching between
615 p_stream->b_force_backup = 1;
617 /* Cheat and get additionnal info ;) */
618 oggpackB_readinit(&opb, oggpacket.packet, oggpacket.bytes);
619 oggpackB_adv( &opb, 56 );
620 oggpackB_read( &opb, 8 ); /* major version num */
621 oggpackB_read( &opb, 8 ); /* minor version num */
622 oggpackB_read( &opb, 8 ); /* subminor version num */
623 oggpackB_read( &opb, 16 ) /*<< 4*/; /* width */
624 oggpackB_read( &opb, 16 ) /*<< 4*/; /* height */
625 oggpackB_read( &opb, 24 ); /* frame width */
626 oggpackB_read( &opb, 24 ); /* frame height */
627 oggpackB_read( &opb, 8 ); /* x offset */
628 oggpackB_read( &opb, 8 ); /* y offset */
630 i_fps_numerator = oggpackB_read( &opb, 32 );
631 i_fps_denominator = oggpackB_read( &opb, 32 );
632 oggpackB_read( &opb, 24 ); /* aspect_numerator */
633 oggpackB_read( &opb, 24 ); /* aspect_denominator */
634 i_keyframe_frequency_force = 1 << oggpackB_read( &opb, 5 );
635 oggpackB_read( &opb, 8 ); /* colorspace */
636 p_stream->i_bitrate = oggpackB_read( &opb, 24 );
637 oggpackB_read( &opb, 6 ); /* quality */
639 /* granule_shift = i_log( frequency_force -1 ) */
640 p_stream->i_theora_keyframe_granule_shift = 0;
641 i_keyframe_frequency_force--;
642 while( i_keyframe_frequency_force )
644 p_stream->i_theora_keyframe_granule_shift++;
645 i_keyframe_frequency_force >>= 1;
648 p_stream->f_rate = ((float)i_fps_numerator) /
651 "found theora header, bitrate: %i, rate: %f",
652 p_stream->i_bitrate, p_stream->f_rate );
654 char title[sizeof("Stream") + 10];
655 input_info_category_t *p_cat;
656 sprintf( title, "Stream %d", p_ogg->i_streams );
657 p_cat = input_InfoCategory( p_input, title );
658 input_AddInfo( p_cat, _("Type"), _("Video") );
659 input_AddInfo( p_cat, _("Codec"), _("Theora") );
660 input_AddInfo( p_cat, _("Frame Rate"), "%f",
662 input_AddInfo( p_cat, _("Bit Rate"), "%d",
663 p_stream->i_bitrate );
665 #else /* HAVE_OGGPACKB */
666 msg_Dbg( p_input, "the ogg demuxer has been compiled "
667 "without support for the oggpackB extension."
668 "The theora stream won't be decoded." );
672 #endif /* HAVE_OGGPACKB */
674 /* Check for Tarkin header */
675 else if( oggpacket.bytes >= 7 &&
676 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
680 msg_Dbg( p_input, "found tarkin header" );
681 p_stream->i_cat = VIDEO_ES;
682 p_stream->i_fourcc = VLC_FOURCC( 't','a','r','k' );
684 /* Cheat and get additionnal info ;) */
685 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
686 oggpack_adv( &opb, 88 );
687 oggpack_adv( &opb, 104 );
688 p_stream->i_bitrate = oggpack_read( &opb, 32 );
689 p_stream->f_rate = 2; /* FIXME */
691 "found tarkin header, bitrate: %i, rate: %f",
692 p_stream->i_bitrate, p_stream->f_rate );
694 char title[sizeof("Stream") + 10];
695 input_info_category_t *p_cat;
696 sprintf( title, "Stream %d", p_ogg->i_streams );
697 p_cat = input_InfoCategory( p_input, title );
698 input_AddInfo( p_cat, _("Type"), _("Video") );
699 input_AddInfo( p_cat, _("Codec"), _("tarkin") );
700 input_AddInfo( p_cat, _("Sample Rate"), "%f",
702 input_AddInfo( p_cat, _("Bit Rate"), "%d",
703 p_stream->i_bitrate );
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->i_cat = VIDEO_ES;
719 p_stream->p_bih = (BITMAPINFOHEADER *)
720 malloc( sizeof(BITMAPINFOHEADER) );
721 if( !p_stream->p_bih )
723 /* Mem allocation error, just ignore the stream */
728 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
729 p_stream->p_bih->biCompression= p_stream->i_fourcc =
730 VLC_FOURCC( oggpacket.packet[68],
731 oggpacket.packet[69],
732 oggpacket.packet[70],
733 oggpacket.packet[71] );
734 msg_Dbg( p_input, "found video header of type: %.4s",
735 (char *)&p_stream->i_fourcc );
737 p_stream->f_rate = 10000000.0 /
738 GetQWLE((oggpacket.packet+164));
739 p_stream->p_bih->biBitCount =
740 GetWLE((oggpacket.packet+182));
741 if( !p_stream->p_bih->biBitCount )
742 p_stream->p_bih->biBitCount=24; // hack, FIXME
743 p_stream->p_bih->biWidth =
744 GetDWLE((oggpacket.packet+176));
745 p_stream->p_bih->biHeight =
746 GetDWLE((oggpacket.packet+180));
747 p_stream->p_bih->biPlanes= 1 ;
748 p_stream->p_bih->biSizeImage =
749 (p_stream->p_bih->biBitCount >> 3) *
750 p_stream->p_bih->biWidth *
751 p_stream->p_bih->biHeight;
754 "fps: %f, width:%i; height:%i, bitcount:%i",
755 p_stream->f_rate, p_stream->p_bih->biWidth,
756 p_stream->p_bih->biHeight,
757 p_stream->p_bih->biBitCount);
759 char title[sizeof("Stream") + 10];
760 input_info_category_t *p_cat;
761 sprintf( title, "Stream %d", p_ogg->i_streams );
762 p_cat = input_InfoCategory( p_input, title );
763 input_AddInfo( p_cat, _("Type"), _("Video") );
764 input_AddInfo( p_cat, _("Codec"), "%.4s",
765 (char *)&p_stream->i_fourcc );
766 input_AddInfo( p_cat, _("Frame Rate"), "%f",
768 input_AddInfo( p_cat, _("Bit Count"), "%d",
769 p_stream->p_bih->biBitCount );
770 input_AddInfo( p_cat, _("Width"), "%d",
771 p_stream->p_bih->biWidth );
772 input_AddInfo( p_cat, _("Height"), "%d",
773 p_stream->p_bih->biHeight );
775 p_stream->i_bitrate = 0;
777 /* Check for audio header (old format) */
778 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
780 unsigned int i_extra_size;
782 p_stream->i_cat = AUDIO_ES;
784 i_extra_size = GetWLE((oggpacket.packet+140));
786 p_stream->p_wf = (WAVEFORMATEX *)
787 malloc( sizeof(WAVEFORMATEX) + i_extra_size );
788 if( !p_stream->p_wf )
790 /* Mem allocation error, just ignore the stream */
796 p_stream->p_wf->wFormatTag =
797 GetWLE((oggpacket.packet+124));
798 p_stream->p_wf->nChannels =
799 GetWLE((oggpacket.packet+126));
800 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
801 GetDWLE((oggpacket.packet+128));
802 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
803 GetDWLE((oggpacket.packet+132));
804 p_stream->i_bitrate *= 8;
805 p_stream->p_wf->nBlockAlign =
806 GetWLE((oggpacket.packet+136));
807 p_stream->p_wf->wBitsPerSample =
808 GetWLE((oggpacket.packet+138));
809 p_stream->p_wf->cbSize = i_extra_size;
811 if( i_extra_size > 0 )
812 memcpy( p_stream->p_wf+sizeof(WAVEFORMATEX),
813 oggpacket.packet+142, i_extra_size );
815 switch( p_stream->p_wf->wFormatTag )
817 case WAVE_FORMAT_PCM:
819 VLC_FOURCC( 'a', 'r', 'a', 'w' );
821 case WAVE_FORMAT_MPEG:
822 case WAVE_FORMAT_MPEGLAYER3:
824 VLC_FOURCC( 'm', 'p', 'g', 'a' );
826 case WAVE_FORMAT_A52:
828 VLC_FOURCC( 'a', '5', '2', ' ' );
830 case WAVE_FORMAT_WMA1:
832 VLC_FOURCC( 'w', 'm', 'a', '1' );
834 case WAVE_FORMAT_WMA2:
836 VLC_FOURCC( 'w', 'm', 'a', '2' );
839 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
840 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
841 p_stream->p_wf->wFormatTag & 0xff );
844 msg_Dbg( p_input, "found audio header of type: %.4s",
845 (char *)&p_stream->i_fourcc );
846 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
847 "%dbits/sample %dkb/s",
848 p_stream->p_wf->wFormatTag,
849 p_stream->p_wf->nChannels,
850 p_stream->p_wf->nSamplesPerSec,
851 p_stream->p_wf->wBitsPerSample,
852 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
854 char title[sizeof("Stream") + 10];
855 input_info_category_t *p_cat;
856 sprintf( title, "Stream %d", p_ogg->i_streams );
857 p_cat = input_InfoCategory( p_input, title );
858 input_AddInfo( p_cat, _("Type"), _("Audio") );
859 input_AddInfo( p_cat, _("Codec"), "%.4s",
860 (char *)&p_stream->i_fourcc );
861 input_AddInfo( p_cat, _("Sample Rate"), "%d",
862 p_stream->p_wf->nSamplesPerSec );
863 input_AddInfo( p_cat, _("Bit Rate"), "%d",
864 p_stream->p_wf->nAvgBytesPerSec * 8
866 input_AddInfo( p_cat, _("Channels"), "%d",
867 p_stream->p_wf->nChannels );
868 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
869 p_stream->p_wf->wBitsPerSample );
875 msg_Dbg( p_input, "stream %d has an old header "
876 "but is of an unknown type", p_ogg->i_streams-1 );
881 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
882 == PACKET_TYPE_HEADER &&
883 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
885 stream_header *st = (stream_header *)(oggpacket.packet+1);
887 /* Check for video header (new format) */
888 if( !strncmp( st->streamtype, "video", 5 ) )
890 p_stream->i_cat = VIDEO_ES;
892 /* We need to get rid of the header packet */
893 ogg_stream_packetout( &p_stream->os, &oggpacket );
895 p_stream->p_bih = (BITMAPINFOHEADER *)
896 malloc( sizeof(BITMAPINFOHEADER) );
897 if( !p_stream->p_bih )
899 /* Mem allocation error, just ignore the stream */
904 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
905 p_stream->p_bih->biCompression=
906 p_stream->i_fourcc = VLC_FOURCC( st->subtype[0],
910 msg_Dbg( p_input, "found video header of type: %.4s",
911 (char *)&p_stream->i_fourcc );
913 p_stream->f_rate = 10000000.0 /
914 GetQWLE(&st->time_unit);
915 p_stream->p_bih->biBitCount =
916 GetWLE(&st->bits_per_sample);
917 p_stream->p_bih->biWidth =
918 GetDWLE(&st->sh.video.width);
919 p_stream->p_bih->biHeight =
920 GetDWLE(&st->sh.video.height);
921 p_stream->p_bih->biPlanes= 1 ;
922 p_stream->p_bih->biSizeImage =
923 (p_stream->p_bih->biBitCount >> 3) *
924 p_stream->p_bih->biWidth *
925 p_stream->p_bih->biHeight;
928 "fps: %f, width:%i; height:%i, bitcount:%i",
929 p_stream->f_rate, p_stream->p_bih->biWidth,
930 p_stream->p_bih->biHeight,
931 p_stream->p_bih->biBitCount);
934 char title[sizeof("Stream") + 10];
935 input_info_category_t *p_cat;
936 sprintf( title, "Stream %d", p_ogg->i_streams );
937 p_cat = input_InfoCategory( p_input, title );
938 input_AddInfo( p_cat, _("Type"), _("Video") );
939 input_AddInfo( p_cat, _("Codec"), "%.4s",
940 (char *)&p_stream->i_fourcc );
941 input_AddInfo( p_cat, _("Frame Rate"), "%f",
943 input_AddInfo( p_cat, _("Bit Count"), "%d",
944 p_stream->p_bih->biBitCount );
945 input_AddInfo( p_cat, _("Width"), "%d",
946 p_stream->p_bih->biWidth );
947 input_AddInfo( p_cat, _("Height"), "%d",
948 p_stream->p_bih->biHeight );
950 p_stream->i_bitrate = 0;
952 /* Check for audio header (new format) */
953 else if( !strncmp( st->streamtype, "audio", 5 ) )
957 p_stream->i_cat = AUDIO_ES;
959 /* We need to get rid of the header packet */
960 ogg_stream_packetout( &p_stream->os, &oggpacket );
962 p_stream->p_wf = (WAVEFORMATEX *)
963 malloc( sizeof(WAVEFORMATEX) );
964 if( !p_stream->p_wf )
966 /* Mem allocation error, just ignore the stream */
972 memcpy( p_buffer, st->subtype, 4 );
974 p_stream->p_wf->wFormatTag = strtol(p_buffer,NULL,16);
975 p_stream->p_wf->nChannels =
976 GetWLE(&st->sh.audio.channels);
977 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
978 GetQWLE(&st->samples_per_unit);
979 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
980 GetDWLE(&st->sh.audio.avgbytespersec);
981 p_stream->i_bitrate *= 8;
982 p_stream->p_wf->nBlockAlign =
983 GetWLE(&st->sh.audio.blockalign);
984 p_stream->p_wf->wBitsPerSample =
985 GetWLE(&st->bits_per_sample);
986 p_stream->p_wf->cbSize = 0;
988 switch( p_stream->p_wf->wFormatTag )
990 case WAVE_FORMAT_PCM:
992 VLC_FOURCC( 'a', 'r', 'a', 'w' );
994 case WAVE_FORMAT_MPEG:
995 case WAVE_FORMAT_MPEGLAYER3:
997 VLC_FOURCC( 'm', 'p', 'g', 'a' );
999 case WAVE_FORMAT_A52:
1000 p_stream->i_fourcc =
1001 VLC_FOURCC( 'a', '5', '2', ' ' );
1003 case WAVE_FORMAT_WMA1:
1004 p_stream->i_fourcc =
1005 VLC_FOURCC( 'w', 'm', 'a', '1' );
1007 case WAVE_FORMAT_WMA2:
1008 p_stream->i_fourcc =
1009 VLC_FOURCC( 'w', 'm', 'a', '2' );
1012 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
1013 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
1014 p_stream->p_wf->wFormatTag & 0xff );
1017 msg_Dbg( p_input, "found audio header of type: %.4s",
1018 (char *)&p_stream->i_fourcc );
1019 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
1020 "%dbits/sample %dkb/s",
1021 p_stream->p_wf->wFormatTag,
1022 p_stream->p_wf->nChannels,
1023 p_stream->p_wf->nSamplesPerSec,
1024 p_stream->p_wf->wBitsPerSample,
1025 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
1027 char title[sizeof("Stream") + 10];
1028 input_info_category_t *p_cat;
1029 sprintf( title, "Stream %d", p_ogg->i_streams );
1030 p_cat = input_InfoCategory( p_input, title );
1031 input_AddInfo( p_cat, _("Type"), _("Audio") );
1032 input_AddInfo( p_cat, _("Codec"), "%.4s",
1033 (char *)&p_stream->i_fourcc );
1034 input_AddInfo( p_cat, _("Sample Rate"), "%d",
1035 p_stream->p_wf->nSamplesPerSec );
1036 input_AddInfo( p_cat, _("Bit Rate"), "%d",
1037 p_stream->p_wf->nAvgBytesPerSec * 8
1039 input_AddInfo( p_cat, _("Channels"), "%d",
1040 p_stream->p_wf->nChannels );
1041 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
1042 p_stream->p_wf->wBitsPerSample );
1045 /* Check for text (subtitles) header */
1046 else if( !strncmp(st->streamtype, "text", 4) )
1048 /* We need to get rid of the header packet */
1049 ogg_stream_packetout( &p_stream->os, &oggpacket );
1051 msg_Dbg( p_input, "found text subtitles header" );
1052 p_stream->i_cat = SPU_ES;
1053 p_stream->i_fourcc =
1054 VLC_FOURCC( 's', 'u', 'b', 't' );
1055 p_stream->f_rate = 1000; /* granulepos is in milisec */
1059 msg_Dbg( p_input, "stream %d has a header marker "
1060 "but is of an unknown type", p_ogg->i_streams-1 );
1067 msg_Dbg( p_input, "stream %d is of unknown type",
1068 p_ogg->i_streams-1 );
1075 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1076 return VLC_EGENERIC;
1079 /* This is the first data page, which means we are now finished
1080 * with the initial pages. We just need to store it in the relevant
1082 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1084 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1093 return VLC_EGENERIC;
1096 /*****************************************************************************
1097 * Activate: initializes ogg demux structures
1098 *****************************************************************************/
1099 static int Activate( vlc_object_t * p_this )
1101 input_thread_t *p_input = (input_thread_t *)p_this;
1105 p_input->p_demux_data = NULL;
1106 b_forced = ( ( *p_input->psz_demux )&&
1107 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
1109 /* Check if we are dealing with an ogg stream */
1110 if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
1113 /* Allocate p_ogg */
1114 if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
1116 msg_Err( p_input, "out of memory" );
1119 memset( p_ogg, 0, sizeof( demux_sys_t ) );
1120 p_input->p_demux_data = p_ogg;
1121 p_ogg->pp_stream = NULL;
1122 p_ogg->p_stream_video = NULL;
1123 p_ogg->p_stream_audio = NULL;
1124 p_ogg->p_stream_spu = NULL;
1126 /* Initialize the Ogg physical bitstream parser */
1127 ogg_sync_init( &p_ogg->oy );
1129 /*Set exported functions */
1130 p_input->pf_demux = Demux;
1131 p_input->pf_demux_control = Control;
1133 /* Initialize access plug-in structures. */
1134 if( p_input->i_mtu == 0 )
1136 /* Improve speed. */
1137 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
1140 /* Create one program */
1141 vlc_mutex_lock( &p_input->stream.stream_lock );
1142 if( input_InitStream( p_input, 0 ) == -1)
1144 vlc_mutex_unlock( &p_input->stream.stream_lock );
1145 msg_Err( p_input, "cannot init stream" );
1148 if( input_AddProgram( p_input, 0, 0) == NULL )
1150 vlc_mutex_unlock( &p_input->stream.stream_lock );
1151 msg_Err( p_input, "cannot add program" );
1154 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1155 vlc_mutex_unlock( &p_input->stream.stream_lock );
1157 /* Begnning of stream, tell the demux to look for elementary streams. */
1158 p_ogg->b_eos = VLC_TRUE;
1163 Deactivate( (vlc_object_t *)p_input );
1168 /****************************************************************************
1169 * Ogg_EndOfStream: Look for Beginning of Stream ogg pages and add Elementary
1171 ****************************************************************************/
1172 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg)
1176 /* Find the logical streams embedded in the physical stream and
1177 * initialize our p_ogg structure. */
1178 if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
1180 msg_Warn( p_input, "couldn't find any ogg logical stream" );
1181 return VLC_EGENERIC;
1184 vlc_mutex_lock( &p_input->stream.stream_lock );
1185 p_input->stream.i_mux_rate = 0;
1186 vlc_mutex_unlock( &p_input->stream.stream_lock );
1188 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1190 #define p_stream p_ogg->pp_stream[i_stream]
1191 vlc_mutex_lock( &p_input->stream.stream_lock );
1192 p_stream->p_es = input_AddES( p_input,
1193 p_input->stream.p_selected_program,
1195 p_stream->i_cat, NULL, 0 );
1196 p_input->stream.i_mux_rate += (p_stream->i_bitrate / ( 8 * 50 ));
1197 vlc_mutex_unlock( &p_input->stream.stream_lock );
1198 p_stream->p_es->i_stream_id = i_stream;
1199 p_stream->p_es->i_fourcc = p_stream->i_fourcc;
1200 p_stream->p_es->p_waveformatex = (void*)p_stream->p_wf;
1201 p_stream->p_es->p_bitmapinfoheader = (void*)p_stream->p_bih;
1203 p_stream->i_pcr = p_stream->i_previous_pcr = -1;
1207 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1209 #define p_stream p_ogg->pp_stream[i_stream]
1210 switch( p_stream->p_es->i_cat )
1213 if( (p_ogg->p_stream_video == NULL) )
1215 p_ogg->p_stream_video = p_stream;
1216 /* TODO add test to see if a decoder has been found */
1217 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1222 if( (p_ogg->p_stream_audio == NULL) )
1224 int i_audio = config_GetInt( p_input, "audio-channel" );
1225 if( i_audio == i_stream || i_audio <= 0 ||
1226 i_audio >= p_ogg->i_streams ||
1227 p_ogg->pp_stream[i_audio]->p_es->i_cat != AUDIO_ES )
1229 p_ogg->p_stream_audio = p_stream;
1230 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1236 if( (p_ogg->p_stream_spu == NULL) )
1238 /* for spu, default is none */
1239 int i_spu = config_GetInt( p_input, "spu-channel" );
1240 if( i_spu < 0 || i_spu >= p_ogg->i_streams ||
1241 p_ogg->pp_stream[i_spu]->p_es->i_cat != SPU_ES )
1245 else if( i_spu == i_stream )
1247 p_ogg->p_stream_spu = p_stream;
1248 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1259 /* we select the first audio and video ES */
1260 vlc_mutex_lock( &p_input->stream.stream_lock );
1261 if( !p_ogg->p_stream_video )
1263 msg_Warn( p_input, "no video stream found" );
1265 if( !p_ogg->p_stream_audio )
1267 msg_Warn( p_input, "no audio stream found!" );
1269 p_input->stream.p_selected_program->b_is_ok = 1;
1270 vlc_mutex_unlock( &p_input->stream.stream_lock );
1275 /****************************************************************************
1276 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1277 ****************************************************************************/
1278 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg )
1282 #define p_stream p_ogg->pp_stream[i_stream]
1283 vlc_mutex_lock( &p_input->stream.stream_lock );
1284 if( p_input->stream.i_pgrm_number )
1285 while( p_input->stream.p_selected_program->i_es_number )
1287 input_DelES( p_input,
1288 p_input->stream.p_selected_program->pp_es[0] );
1290 vlc_mutex_unlock( &p_input->stream.stream_lock );
1293 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1295 #define p_stream p_ogg->pp_stream[i_stream]
1296 vlc_mutex_lock( &p_input->stream.stream_lock );
1297 p_input->stream.i_mux_rate -= (p_stream->i_bitrate / ( 8 * 50 ));
1298 vlc_mutex_unlock( &p_input->stream.stream_lock );
1301 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1302 for( j = 0; j < p_ogg->pp_stream[i_stream]->i_packets_backup; j++ )
1304 free( p_ogg->pp_stream[i_stream]->p_packets_backup[j].packet );
1306 if( p_ogg->pp_stream[i_stream]->p_packets_backup)
1307 free( p_ogg->pp_stream[i_stream]->p_packets_backup );
1309 #if 0 /* hmmm, it's already freed in input_DelES() */
1310 if( p_ogg->pp_stream[i]->p_bih )
1311 free( p_ogg->pp_stream[i]->p_bih );
1312 if( p_ogg->pp_stream[i]->p_wf )
1313 free( p_ogg->pp_stream[i]->p_wf );
1316 free( p_ogg->pp_stream[i_stream] );
1320 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1321 p_ogg->pp_stream = NULL;
1322 p_ogg->i_streams = 0;
1323 p_ogg->p_stream_video = NULL;
1324 p_ogg->p_stream_audio = NULL;
1325 p_ogg->p_stream_spu = NULL;
1328 /*****************************************************************************
1329 * Deactivate: frees unused data
1330 *****************************************************************************/
1331 static void Deactivate( vlc_object_t *p_this )
1333 input_thread_t *p_input = (input_thread_t *)p_this;
1334 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
1338 /* Cleanup the bitstream parser */
1339 ogg_sync_clear( &p_ogg->oy );
1341 Ogg_EndOfStream( p_input, p_ogg );
1347 /*****************************************************************************
1348 * Demux: reads and demuxes data packets
1349 *****************************************************************************
1350 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1351 *****************************************************************************/
1352 static int Demux( input_thread_t * p_input )
1354 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1356 ogg_packet oggpacket;
1361 if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
1362 p_ogg->b_eos = VLC_FALSE;
1364 msg_Dbg( p_input, "beginning of a group of logical streams" );
1366 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
1367 input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
1370 #define p_stream p_ogg->pp_stream[i_stream]
1372 /* detect new selected/unselected streams */
1373 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1375 if( p_stream->p_es )
1377 if( p_stream->p_es->p_decoder_fifo &&
1378 !p_stream->i_activated )
1380 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1383 if( !p_stream->p_es->p_decoder_fifo &&
1384 p_stream->i_activated )
1386 Ogg_ElemStreamStop( p_input, p_ogg, i_stream );
1391 /* search for new video and audio stream to select
1392 * if current have been unselected */
1393 if( ( !p_ogg->p_stream_video )
1394 || ( !p_ogg->p_stream_video->p_es->p_decoder_fifo ) )
1396 p_ogg->p_stream_video = NULL;
1397 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1399 if( ( p_stream->i_cat == VIDEO_ES )
1400 &&( p_stream->p_es->p_decoder_fifo ) )
1402 p_ogg->p_stream_video = p_stream;
1407 if( ( !p_ogg->p_stream_audio )
1408 ||( !p_ogg->p_stream_audio->p_es->p_decoder_fifo ) )
1410 p_ogg->p_stream_audio = NULL;
1411 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1413 if( ( p_stream->i_cat == AUDIO_ES )
1414 &&( p_stream->p_es->p_decoder_fifo ) )
1416 p_ogg->p_stream_audio = p_stream;
1422 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1424 msg_Warn( p_input, "synchro reinit" );
1426 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1428 /* we'll trash all the data until we find the next pcr */
1429 p_stream->b_reinit = 1;
1430 p_stream->i_pcr = -1;
1431 p_stream->i_interpolated_pcr = -1;
1437 * Demux an ogg page from the stream
1439 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1444 /* Test for End of Stream */
1445 if( ogg_page_eos( &oggpage ) )
1447 msg_Dbg( p_input, "end of a group of logical streams" );
1449 Ogg_EndOfStream( p_input, p_ogg );
1450 p_ogg->b_eos = VLC_TRUE;
1454 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1456 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1459 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1461 if( !p_stream->p_es )
1466 if( p_stream->b_reinit )
1468 /* If synchro is re-initialized we need to drop all the packets
1469 * until we find a new dated one. */
1470 Ogg_UpdatePCR( p_stream, &oggpacket );
1472 if( p_stream->i_pcr >= 0 )
1474 p_stream->b_reinit = 0;
1478 p_stream->i_interpolated_pcr = -1;
1482 /* An Ogg/vorbis packet contains an end date granulepos */
1483 if( p_stream->i_fourcc == VLC_FOURCC( 'v','o','r','b' ) )
1485 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1487 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1493 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1498 i_stream = 0; p_ogg->i_pcr = -1;
1499 for( ; i_stream < p_ogg->i_streams; i_stream++ )
1501 if( p_stream->i_cat == SPU_ES )
1503 if( p_stream->i_interpolated_pcr < 0 )
1506 if( p_ogg->i_pcr < 0 || p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1507 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1510 if( p_input->stream.p_selected_program->i_synchro_state != SYNCHRO_REINIT )
1512 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1521 /*****************************************************************************
1523 *****************************************************************************/
1524 static int Control( input_thread_t *p_input, int i_query, va_list args )
1526 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1531 case DEMUX_GET_TIME:
1532 pi64 = (int64_t*)va_arg( args, int64_t * );
1533 *pi64 = p_ogg->i_pcr * 100 / 9;
1537 return demux_vaControlDefault( p_input, i_query, args );