1 /*****************************************************************************
2 * ogg.c : ogg stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ogg.c,v 1.44 2003/11/18 00:57:04 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
99 int i_prev_sync_state;
105 /* OggDS headers for the new header format (used in ogm files) */
106 typedef struct stream_header_video
110 } stream_header_video;
112 typedef struct stream_header_audio
114 ogg_int16_t channels;
115 ogg_int16_t blockalign;
116 ogg_int32_t avgbytespersec;
117 } stream_header_audio;
119 typedef struct stream_header
124 ogg_int32_t size; /* size of the structure */
126 ogg_int64_t time_unit; /* in reference time */
127 ogg_int64_t samples_per_unit;
128 ogg_int32_t default_len; /* in media time */
130 ogg_int32_t buffersize;
131 ogg_int16_t bits_per_sample;
136 stream_header_video video;
138 stream_header_audio audio;
142 /* Some defines from OggDS */
143 #define PACKET_TYPE_HEADER 0x01
144 #define PACKET_TYPE_BITS 0x07
145 #define PACKET_LEN_BITS01 0xc0
146 #define PACKET_LEN_BITS2 0x02
147 #define PACKET_IS_SYNCPOINT 0x08
149 /*****************************************************************************
151 *****************************************************************************/
152 static int Activate ( vlc_object_t * );
153 static void Deactivate( vlc_object_t * );
154 static int Demux ( input_thread_t * );
155 static int Control ( input_thread_t *, int, va_list );
157 /* Stream managment */
158 static int Ogg_ElemStreamStart ( input_thread_t *, demux_sys_t *, int );
159 static void Ogg_ElemStreamStop ( input_thread_t *, demux_sys_t *, int );
161 /* Bitstream manipulation */
162 static int Ogg_Check ( input_thread_t *p_input );
163 static int Ogg_ReadPage ( input_thread_t *, demux_sys_t *, ogg_page * );
164 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
165 static void Ogg_DecodePacket ( input_thread_t *p_input,
166 logical_stream_t *p_stream, ogg_packet * );
168 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg);
169 static int Ogg_FindLogicalStreams( input_thread_t *p_input,demux_sys_t *p_ogg);
170 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg );
172 /*****************************************************************************
174 *****************************************************************************/
176 set_description( _("ogg stream demuxer" ) );
177 set_capability( "demux", 50 );
178 set_callbacks( Activate, Deactivate );
179 add_shortcut( "ogg" );
182 /*****************************************************************************
184 *****************************************************************************/
185 static int Ogg_ElemStreamStart( input_thread_t *p_input,
186 demux_sys_t *p_ogg, int i_stream )
188 #define p_stream p_ogg->pp_stream[i_stream]
189 if( !p_stream->p_es )
191 msg_Warn( p_input, "stream[%d] unselectable", i_stream );
194 if( p_stream->i_activated )
196 msg_Warn( p_input, "stream[%d] already selected", i_stream );
200 if( !p_stream->p_es->p_decoder_fifo )
202 vlc_mutex_lock( &p_input->stream.stream_lock );
203 input_SelectES( p_input, p_stream->p_es );
204 vlc_mutex_unlock( &p_input->stream.stream_lock );
206 p_stream->i_activated = p_stream->p_es->p_decoder_fifo ? 1 : 0;
208 /* Feed the backup header to the decoder */
209 if( !p_stream->b_force_backup )
212 for( i = 0; i < p_stream->i_packets_backup; i++ )
214 /* Set correct starting date in header packets */
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( 's','p','x',' ' ):
361 case VLC_FOURCC( 't','h','e','o' ):
362 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
365 case VLC_FOURCC( 'f','l','a','c' ):
366 if( p_stream->i_packets_backup == 2 )
370 char title[sizeof("Stream") + 10];
371 input_info_category_t *p_cat;
373 sprintf( title, "Stream %d", p_stream->i_serial_no );
374 p_cat = input_InfoCategory( p_input, title );
375 input_AddInfo( p_cat, _("Type"), _("Audio") );
376 input_AddInfo( p_cat, _("Codec"), _("Flac") );
378 /* Parse the STREAMINFO metadata */
379 oggpackB_readinit(&opb, p_oggpacket->packet, p_oggpacket->bytes);
380 oggpackB_adv( &opb, 1 );
381 if( oggpackB_read( &opb, 7 ) == 0 )
383 if( oggpackB_read( &opb, 24 ) >= 34 /*size STREAMINFO*/ )
385 oggpackB_adv( &opb, 80 );
386 p_stream->f_rate = oggpackB_read( &opb, 20 );
387 p_stream->i_channels = oggpackB_read( &opb, 3 ) + 1;
389 input_AddInfo( p_cat, _("Sample Rate"), "%d",
390 (int)p_stream->f_rate );
391 input_AddInfo( p_cat, _("Channels"), "%d",
392 p_stream->i_channels );
393 msg_Dbg( p_input, "Flac header, channels: %i, rate: %i",
394 p_stream->i_channels, (int)p_stream->f_rate );
398 msg_Dbg( p_input, "FLAC STREAMINFO metadata too short" );
403 /* This ain't a STREAMINFO metadata */
404 msg_Dbg( p_input, "Invalid FLAC STREAMINFO metadata" );
407 p_stream->b_force_backup = 0;
412 p_stream->b_force_backup = 0;
416 /* Backup the ogg packet (likely an header packet) */
417 p_stream->p_packets_backup =
418 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
419 sizeof(ogg_packet) );
422 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
424 p_packet_backup->bytes = p_oggpacket->bytes;
425 p_packet_backup->granulepos = p_oggpacket->granulepos;
427 if( p_oggpacket->granulepos >= 0 )
429 /* Because of vorbis granulepos scheme we must set the pcr for the
430 * 1st header packet so it doesn't get discarded in the
432 Ogg_UpdatePCR( p_stream, p_oggpacket );
435 p_packet_backup->packet = malloc( p_oggpacket->bytes );
436 if( !p_packet_backup->packet ) return;
437 memcpy( p_packet_backup->packet, p_oggpacket->packet,
438 p_oggpacket->bytes );
441 vlc_mutex_lock( &p_input->stream.control.control_lock );
442 if( p_stream->i_cat == AUDIO_ES && p_input->stream.control.b_mute )
446 vlc_mutex_unlock( &p_input->stream.control.control_lock );
448 /* Convert the pcr into a pts */
449 if( p_stream->i_fourcc == VLC_FOURCC( 'v','o','r','b' ) ||
450 p_stream->i_fourcc == VLC_FOURCC( 's','p','x',' ' ) ||
451 p_stream->i_fourcc == VLC_FOURCC( 'f','l','a','c' ) )
453 if( p_stream->i_pcr >= 0 )
455 /* This is for streams where the granulepos of the header packets
456 * doesn't match these of the data packets (eg. ogg web radios). */
457 if( p_stream->i_previous_pcr == 0 &&
458 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
459 p_input->stream.p_selected_program->i_synchro_state =
462 p_stream->i_previous_pcr = p_stream->i_pcr;
464 /* Call the pace control */
465 if( p_input->stream.p_selected_program->i_synchro_state ==
467 input_ClockManageRef( p_input,
468 p_input->stream.p_selected_program,
472 /* The granulepos is the end date of the sample */
473 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
474 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
477 /* Convert the granulepos into the next pcr */
478 Ogg_UpdatePCR( p_stream, p_oggpacket );
482 /* Convert the granulepos into the current pcr */
483 Ogg_UpdatePCR( p_stream, p_oggpacket );
485 if( p_stream->i_pcr >= 0 )
487 /* This is for streams where the granulepos of the header packets
488 * doesn't match these of the data packets (eg. ogg web radios). */
489 if( p_stream->i_previous_pcr == 0 &&
490 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
491 p_input->stream.p_selected_program->i_synchro_state =
494 p_stream->i_previous_pcr = p_stream->i_pcr;
496 /* Call the pace control */
497 if( p_input->stream.p_selected_program->i_synchro_state ==
499 input_ClockManageRef( p_input,
500 p_input->stream.p_selected_program,
504 /* The granulepos is the start date of the sample */
505 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
506 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
510 if( !p_stream->p_es->p_decoder_fifo || b_trash )
512 /* This stream isn't currently selected so we don't need to decode it,
513 * but we did need to store its pcr as it might be selected later on */
517 if( !( p_pes = input_NewPES( p_input->p_method_data ) ) )
521 if( !( p_data = input_NewPacket( p_input->p_method_data,
522 p_oggpacket->bytes ) ) )
524 input_DeletePES( p_input->p_method_data, p_pes );
527 p_data->p_payload_end = p_data->p_payload_start + p_oggpacket->bytes;
529 p_pes->i_nb_data = 1;
530 p_pes->i_dts = p_pes->i_pts = i_pts;
531 p_pes->p_first = p_pes->p_last = p_data;
532 p_pes->i_pes_size = p_oggpacket->bytes;
534 if( p_stream->i_cat == SPU_ES ) p_pes->i_dts = 0;
536 if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) &&
537 p_stream->i_fourcc != VLC_FOURCC( 's','p','x',' ' ) &&
538 p_stream->i_fourcc != VLC_FOURCC( 'f','l','a','c' ) &&
539 p_stream->i_fourcc != VLC_FOURCC( 't','a','r','k' ) &&
540 p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
542 /* Remove the header from the packet */
543 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
544 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
547 p_pes->i_pes_size -= i_header_len;
550 if( p_stream->i_fourcc == VLC_FOURCC( 't','a','r','k' ) )
552 /* FIXME: the biggest hack I've ever done */
553 msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
554 p_pes->i_pts, p_pes->i_dts );
558 memcpy( p_data->p_payload_start,
559 p_oggpacket->packet + i_header_len,
560 p_oggpacket->bytes - i_header_len );
562 p_data->p_payload_end = p_data->p_payload_start + p_pes->i_pes_size;
563 p_data->b_discard_payload = 0;
565 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
568 /****************************************************************************
569 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
570 * stream and fill p_ogg.
571 *****************************************************************************
572 * The initial page of a logical stream is marked as a 'bos' page.
573 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
574 * together and all of the initial pages must appear before any data pages.
576 * On success this function returns VLC_SUCCESS.
577 ****************************************************************************/
578 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
580 ogg_packet oggpacket;
584 while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
586 if( ogg_page_bos( &oggpage ) )
589 /* All is wonderful in our fine fine little world.
590 * We found the beginning of our first logical stream. */
591 while( ogg_page_bos( &oggpage ) )
595 realloc( p_ogg->pp_stream, p_ogg->i_streams *
596 sizeof(logical_stream_t *) );
598 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
600 p_stream = malloc( sizeof(logical_stream_t) );
601 memset( p_stream, 0, sizeof(logical_stream_t) );
603 /* Setup the logical stream */
604 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
605 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
607 /* Extract the initial header from the first page and verify
608 * the codec type of tis Ogg bitstream */
609 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
611 /* error. stream version mismatch perhaps */
612 msg_Err( p_input, "Error reading first page of "
613 "Ogg bitstream data" );
617 /* FIXME: check return value */
618 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
620 /* Check for Vorbis header */
621 if( oggpacket.bytes >= 7 &&
622 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
626 msg_Dbg( p_input, "found vorbis header" );
627 p_stream->i_cat = AUDIO_ES;
628 p_stream->i_fourcc = VLC_FOURCC( 'v','o','r','b' );
630 /* Signal that we want to keep a backup of the vorbis
631 * stream headers. They will be used when switching between
633 p_stream->b_force_backup = 1;
635 /* Cheat and get additionnal info ;) */
636 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
637 oggpack_adv( &opb, 88 );
638 p_stream->i_channels = oggpack_read( &opb, 8 );
639 p_stream->f_rate = oggpack_read( &opb, 32 );
640 oggpack_adv( &opb, 32 );
641 p_stream->i_bitrate = oggpack_read( &opb, 32 );
643 char title[sizeof("Stream") + 10];
644 input_info_category_t *p_cat;
645 sprintf( title, "Stream %d", p_ogg->i_streams );
646 p_cat = input_InfoCategory( p_input, title );
647 input_AddInfo( p_cat, _("Type"), _("Audio") );
648 input_AddInfo( p_cat, _("Codec"), _("Vorbis") );
649 input_AddInfo( p_cat, _("Sample Rate"), "%d",
650 (int)p_stream->f_rate );
651 input_AddInfo( p_cat, _("Channels"), "%d",
652 p_stream->i_channels );
653 input_AddInfo( p_cat, _("Bit Rate"), "%d",
654 p_stream->i_bitrate );
657 /* Check for Speex header */
658 else if( oggpacket.bytes >= 7 &&
659 ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
663 p_stream->i_cat = AUDIO_ES;
664 p_stream->i_fourcc = VLC_FOURCC( 's','p','x',' ' );
666 /* Signal that we want to keep a backup of the vorbis
667 * stream headers. They will be used when switching between
669 p_stream->b_force_backup = 1;
671 /* Cheat and get additionnal info ;) */
672 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
673 oggpack_adv( &opb, 224 );
674 oggpack_adv( &opb, 32 ); /* speex_version_id */
675 oggpack_adv( &opb, 32 ); /* header_size */
676 p_stream->f_rate = oggpack_read( &opb, 32 );
677 oggpack_adv( &opb, 32 ); /* mode */
678 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
679 p_stream->i_channels = oggpack_read( &opb, 32 );
680 p_stream->i_bitrate = oggpack_read( &opb, 32 );
682 char title[sizeof("Stream") + 10];
683 input_info_category_t *p_cat;
684 sprintf( title, "Stream %d", p_ogg->i_streams );
685 p_cat = input_InfoCategory( p_input, title );
686 input_AddInfo( p_cat, _("Type"), _("Audio") );
687 input_AddInfo( p_cat, _("Codec"), _("Speex") );
688 input_AddInfo( p_cat, _("Sample Rate"), "%d",
689 (int)p_stream->f_rate );
690 input_AddInfo( p_cat, _("Channels"), "%d",
691 p_stream->i_channels );
692 input_AddInfo( p_cat, _("Bit Rate"), "%d",
693 p_stream->i_bitrate );
694 msg_Dbg( p_input, "found speex header, channels: %i, "
695 "rate: %i, bitrate: %i",
696 p_stream->i_channels,
697 (int)p_stream->f_rate, p_stream->i_bitrate );
700 /* Check for Flac header */
701 else if( oggpacket.bytes >= 4 &&
702 ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
704 msg_Dbg( p_input, "found Flac header" );
706 /* Grrrr!!!! Did they really have to put all the
707 * important info in the second header packet!!!
708 * (STREAMINFO metadata is in the following packet) */
709 p_stream->b_force_backup = 1;
711 p_stream->i_cat = AUDIO_ES;
712 p_stream->i_fourcc = VLC_FOURCC( 'f','l','a','c' );
713 p_stream->i_bitrate = 0;
715 /* Check for Theora header */
716 else if( oggpacket.bytes >= 7 &&
717 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
722 int i_fps_denominator;
723 int i_keyframe_frequency_force;
726 msg_Dbg( p_input, "found theora header" );
728 p_stream->i_cat = VIDEO_ES;
729 p_stream->i_fourcc = VLC_FOURCC( 't','h','e','o' );
731 /* Signal that we want to keep a backup of the vorbis
732 * stream headers. They will be used when switching between
734 p_stream->b_force_backup = 1;
736 /* Cheat and get additionnal info ;) */
737 oggpackB_readinit(&opb, oggpacket.packet, oggpacket.bytes);
738 oggpackB_adv( &opb, 56 );
739 oggpackB_read( &opb, 8 ); /* major version num */
740 oggpackB_read( &opb, 8 ); /* minor version num */
741 oggpackB_read( &opb, 8 ); /* subminor version num */
742 oggpackB_read( &opb, 16 ) /*<< 4*/; /* width */
743 oggpackB_read( &opb, 16 ) /*<< 4*/; /* height */
744 oggpackB_read( &opb, 24 ); /* frame width */
745 oggpackB_read( &opb, 24 ); /* frame height */
746 oggpackB_read( &opb, 8 ); /* x offset */
747 oggpackB_read( &opb, 8 ); /* y offset */
749 i_fps_numerator = oggpackB_read( &opb, 32 );
750 i_fps_denominator = oggpackB_read( &opb, 32 );
751 oggpackB_read( &opb, 24 ); /* aspect_numerator */
752 oggpackB_read( &opb, 24 ); /* aspect_denominator */
753 i_keyframe_frequency_force = 1 << oggpackB_read( &opb, 5 );
754 oggpackB_read( &opb, 8 ); /* colorspace */
755 p_stream->i_bitrate = oggpackB_read( &opb, 24 );
756 oggpackB_read( &opb, 6 ); /* quality */
758 /* granule_shift = i_log( frequency_force -1 ) */
759 p_stream->i_theora_keyframe_granule_shift = 0;
760 i_keyframe_frequency_force--;
761 while( i_keyframe_frequency_force )
763 p_stream->i_theora_keyframe_granule_shift++;
764 i_keyframe_frequency_force >>= 1;
767 p_stream->f_rate = ((float)i_fps_numerator) /
770 "found theora header, bitrate: %i, rate: %f",
771 p_stream->i_bitrate, p_stream->f_rate );
773 char title[sizeof("Stream") + 10];
774 input_info_category_t *p_cat;
775 sprintf( title, "Stream %d", p_ogg->i_streams );
776 p_cat = input_InfoCategory( p_input, title );
777 input_AddInfo( p_cat, _("Type"), _("Video") );
778 input_AddInfo( p_cat, _("Codec"), _("Theora") );
779 input_AddInfo( p_cat, _("Frame Rate"), "%.2f",
781 input_AddInfo( p_cat, _("Bit Rate"), "%d",
782 p_stream->i_bitrate );
784 #else /* HAVE_OGGPACKB */
785 msg_Dbg( p_input, "the ogg demuxer has been compiled "
786 "without support for the oggpackB extension."
787 "The theora stream won't be decoded." );
790 #endif /* HAVE_OGGPACKB */
792 /* Check for Tarkin header */
793 else if( oggpacket.bytes >= 7 &&
794 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
798 msg_Dbg( p_input, "found tarkin header" );
799 p_stream->i_cat = VIDEO_ES;
800 p_stream->i_fourcc = VLC_FOURCC( 't','a','r','k' );
802 /* Cheat and get additionnal info ;) */
803 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
804 oggpack_adv( &opb, 88 );
805 oggpack_adv( &opb, 104 );
806 p_stream->i_bitrate = oggpack_read( &opb, 32 );
807 p_stream->f_rate = 2; /* FIXME */
809 "found tarkin header, bitrate: %i, rate: %f",
810 p_stream->i_bitrate, p_stream->f_rate );
812 char title[sizeof("Stream") + 10];
813 input_info_category_t *p_cat;
814 sprintf( title, "Stream %d", p_ogg->i_streams );
815 p_cat = input_InfoCategory( p_input, title );
816 input_AddInfo( p_cat, _("Type"), _("Video") );
817 input_AddInfo( p_cat, _("Codec"), _("tarkin") );
818 input_AddInfo( p_cat, _("Sample Rate"), "%d",
819 (int)p_stream->f_rate );
820 input_AddInfo( p_cat, _("Bit Rate"), "%d",
821 p_stream->i_bitrate );
825 else if( oggpacket.bytes >= 142 &&
826 !strncmp( &oggpacket.packet[1],
827 "Direct Show Samples embedded in Ogg", 35 ))
829 /* Old header type */
831 /* Check for video header (old format) */
832 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
833 oggpacket.bytes >= 184 )
835 p_stream->i_cat = VIDEO_ES;
837 p_stream->p_bih = (BITMAPINFOHEADER *)
838 malloc( sizeof(BITMAPINFOHEADER) );
839 if( !p_stream->p_bih )
841 /* Mem allocation error, just ignore the stream */
846 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
847 p_stream->p_bih->biCompression= p_stream->i_fourcc =
848 VLC_FOURCC( oggpacket.packet[68],
849 oggpacket.packet[69],
850 oggpacket.packet[70],
851 oggpacket.packet[71] );
852 msg_Dbg( p_input, "found video header of type: %.4s",
853 (char *)&p_stream->i_fourcc );
855 p_stream->f_rate = 10000000.0 /
856 GetQWLE((oggpacket.packet+164));
857 p_stream->p_bih->biBitCount =
858 GetWLE((oggpacket.packet+182));
859 if( !p_stream->p_bih->biBitCount )
860 p_stream->p_bih->biBitCount=24; // hack, FIXME
861 p_stream->p_bih->biWidth =
862 GetDWLE((oggpacket.packet+176));
863 p_stream->p_bih->biHeight =
864 GetDWLE((oggpacket.packet+180));
865 p_stream->p_bih->biPlanes= 1 ;
866 p_stream->p_bih->biSizeImage =
867 (p_stream->p_bih->biBitCount >> 3) *
868 p_stream->p_bih->biWidth *
869 p_stream->p_bih->biHeight;
872 "fps: %f, width:%i; height:%i, bitcount:%i",
873 p_stream->f_rate, p_stream->p_bih->biWidth,
874 p_stream->p_bih->biHeight,
875 p_stream->p_bih->biBitCount);
877 char title[sizeof("Stream") + 10];
878 input_info_category_t *p_cat;
879 sprintf( title, "Stream %d", p_ogg->i_streams );
880 p_cat = input_InfoCategory( p_input, title );
881 input_AddInfo( p_cat, _("Type"), _("Video") );
882 input_AddInfo( p_cat, _("Codec"), "%.4s",
883 (char *)&p_stream->i_fourcc );
884 input_AddInfo( p_cat, _("Frame Rate"), "%.2f",
886 input_AddInfo( p_cat, _("Bit Count"), "%d",
887 p_stream->p_bih->biBitCount );
888 input_AddInfo( p_cat, _("Width"), "%d",
889 p_stream->p_bih->biWidth );
890 input_AddInfo( p_cat, _("Height"), "%d",
891 p_stream->p_bih->biHeight );
893 p_stream->i_bitrate = 0;
895 /* Check for audio header (old format) */
896 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
898 unsigned int i_extra_size;
900 p_stream->i_cat = AUDIO_ES;
902 i_extra_size = GetWLE((oggpacket.packet+140));
904 p_stream->p_wf = (WAVEFORMATEX *)
905 malloc( sizeof(WAVEFORMATEX) + i_extra_size );
906 if( !p_stream->p_wf )
908 /* Mem allocation error, just ignore the stream */
914 p_stream->p_wf->wFormatTag =
915 GetWLE((oggpacket.packet+124));
916 p_stream->p_wf->nChannels =
917 GetWLE((oggpacket.packet+126));
918 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
919 GetDWLE((oggpacket.packet+128));
920 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
921 GetDWLE((oggpacket.packet+132));
922 p_stream->i_bitrate *= 8;
923 p_stream->p_wf->nBlockAlign =
924 GetWLE((oggpacket.packet+136));
925 p_stream->p_wf->wBitsPerSample =
926 GetWLE((oggpacket.packet+138));
927 p_stream->p_wf->cbSize = i_extra_size;
929 if( i_extra_size > 0 )
930 memcpy( p_stream->p_wf+sizeof(WAVEFORMATEX),
931 oggpacket.packet+142, i_extra_size );
933 switch( p_stream->p_wf->wFormatTag )
935 case WAVE_FORMAT_PCM:
937 VLC_FOURCC( 'a', 'r', 'a', 'w' );
939 case WAVE_FORMAT_MPEG:
940 case WAVE_FORMAT_MPEGLAYER3:
942 VLC_FOURCC( 'm', 'p', 'g', 'a' );
944 case WAVE_FORMAT_A52:
946 VLC_FOURCC( 'a', '5', '2', ' ' );
948 case WAVE_FORMAT_WMA1:
950 VLC_FOURCC( 'w', 'm', 'a', '1' );
952 case WAVE_FORMAT_WMA2:
954 VLC_FOURCC( 'w', 'm', 'a', '2' );
957 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
958 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
959 p_stream->p_wf->wFormatTag & 0xff );
962 msg_Dbg( p_input, "found audio header of type: %.4s",
963 (char *)&p_stream->i_fourcc );
964 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
965 "%dbits/sample %dkb/s",
966 p_stream->p_wf->wFormatTag,
967 p_stream->p_wf->nChannels,
968 p_stream->p_wf->nSamplesPerSec,
969 p_stream->p_wf->wBitsPerSample,
970 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
972 char title[sizeof("Stream") + 10];
973 input_info_category_t *p_cat;
974 sprintf( title, "Stream %d", p_ogg->i_streams );
975 p_cat = input_InfoCategory( p_input, title );
976 input_AddInfo( p_cat, _("Type"), _("Audio") );
977 input_AddInfo( p_cat, _("Codec"), "%.4s",
978 (char *)&p_stream->i_fourcc );
979 input_AddInfo( p_cat, _("Sample Rate"), "%d",
980 p_stream->p_wf->nSamplesPerSec );
981 input_AddInfo( p_cat, _("Bit Rate"), "%d",
982 p_stream->p_wf->nAvgBytesPerSec * 8
984 input_AddInfo( p_cat, _("Channels"), "%d",
985 p_stream->p_wf->nChannels );
986 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
987 p_stream->p_wf->wBitsPerSample );
993 msg_Dbg( p_input, "stream %d has an old header "
994 "but is of an unknown type", p_ogg->i_streams-1 );
999 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
1000 == PACKET_TYPE_HEADER &&
1001 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
1003 stream_header *st = (stream_header *)(oggpacket.packet+1);
1005 /* Check for video header (new format) */
1006 if( !strncmp( st->streamtype, "video", 5 ) )
1008 p_stream->i_cat = VIDEO_ES;
1010 /* We need to get rid of the header packet */
1011 ogg_stream_packetout( &p_stream->os, &oggpacket );
1013 p_stream->p_bih = (BITMAPINFOHEADER *)
1014 malloc( sizeof(BITMAPINFOHEADER) );
1015 if( !p_stream->p_bih )
1017 /* Mem allocation error, just ignore the stream */
1022 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
1023 p_stream->p_bih->biCompression=
1024 p_stream->i_fourcc = VLC_FOURCC( st->subtype[0],
1028 msg_Dbg( p_input, "found video header of type: %.4s",
1029 (char *)&p_stream->i_fourcc );
1031 p_stream->f_rate = 10000000.0 /
1032 GetQWLE(&st->time_unit);
1033 p_stream->p_bih->biBitCount =
1034 GetWLE(&st->bits_per_sample);
1035 p_stream->p_bih->biWidth =
1036 GetDWLE(&st->sh.video.width);
1037 p_stream->p_bih->biHeight =
1038 GetDWLE(&st->sh.video.height);
1039 p_stream->p_bih->biPlanes= 1 ;
1040 p_stream->p_bih->biSizeImage =
1041 (p_stream->p_bih->biBitCount >> 3) *
1042 p_stream->p_bih->biWidth *
1043 p_stream->p_bih->biHeight;
1046 "fps: %f, width:%i; height:%i, bitcount:%i",
1047 p_stream->f_rate, p_stream->p_bih->biWidth,
1048 p_stream->p_bih->biHeight,
1049 p_stream->p_bih->biBitCount);
1052 char title[sizeof("Stream") + 10];
1053 input_info_category_t *p_cat;
1054 sprintf( title, "Stream %d", p_ogg->i_streams );
1055 p_cat = input_InfoCategory( p_input, title );
1056 input_AddInfo( p_cat, _("Type"), _("Video") );
1057 input_AddInfo( p_cat, _("Codec"), "%.4s",
1058 (char *)&p_stream->i_fourcc );
1059 input_AddInfo( p_cat, _("Frame Rate"), "%.2f",
1061 input_AddInfo( p_cat, _("Bit Count"), "%d",
1062 p_stream->p_bih->biBitCount );
1063 input_AddInfo( p_cat, _("Width"), "%d",
1064 p_stream->p_bih->biWidth );
1065 input_AddInfo( p_cat, _("Height"), "%d",
1066 p_stream->p_bih->biHeight );
1068 p_stream->i_bitrate = 0;
1070 /* Check for audio header (new format) */
1071 else if( !strncmp( st->streamtype, "audio", 5 ) )
1075 p_stream->i_cat = AUDIO_ES;
1077 /* We need to get rid of the header packet */
1078 ogg_stream_packetout( &p_stream->os, &oggpacket );
1080 p_stream->p_wf = (WAVEFORMATEX *)
1081 malloc( sizeof(WAVEFORMATEX) );
1082 if( !p_stream->p_wf )
1084 /* Mem allocation error, just ignore the stream */
1090 memcpy( p_buffer, st->subtype, 4 );
1092 p_stream->p_wf->wFormatTag = strtol(p_buffer,NULL,16);
1093 p_stream->p_wf->nChannels =
1094 GetWLE(&st->sh.audio.channels);
1095 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
1096 GetQWLE(&st->samples_per_unit);
1097 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
1098 GetDWLE(&st->sh.audio.avgbytespersec);
1099 p_stream->i_bitrate *= 8;
1100 p_stream->p_wf->nBlockAlign =
1101 GetWLE(&st->sh.audio.blockalign);
1102 p_stream->p_wf->wBitsPerSample =
1103 GetWLE(&st->bits_per_sample);
1104 p_stream->p_wf->cbSize = 0;
1106 switch( p_stream->p_wf->wFormatTag )
1108 case WAVE_FORMAT_PCM:
1109 p_stream->i_fourcc =
1110 VLC_FOURCC( 'a', 'r', 'a', 'w' );
1112 case WAVE_FORMAT_MPEG:
1113 case WAVE_FORMAT_MPEGLAYER3:
1114 p_stream->i_fourcc =
1115 VLC_FOURCC( 'm', 'p', 'g', 'a' );
1117 case WAVE_FORMAT_A52:
1118 p_stream->i_fourcc =
1119 VLC_FOURCC( 'a', '5', '2', ' ' );
1121 case WAVE_FORMAT_WMA1:
1122 p_stream->i_fourcc =
1123 VLC_FOURCC( 'w', 'm', 'a', '1' );
1125 case WAVE_FORMAT_WMA2:
1126 p_stream->i_fourcc =
1127 VLC_FOURCC( 'w', 'm', 'a', '2' );
1130 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
1131 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
1132 p_stream->p_wf->wFormatTag & 0xff );
1135 msg_Dbg( p_input, "found audio header of type: %.4s",
1136 (char *)&p_stream->i_fourcc );
1137 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
1138 "%dbits/sample %dkb/s",
1139 p_stream->p_wf->wFormatTag,
1140 p_stream->p_wf->nChannels,
1141 p_stream->p_wf->nSamplesPerSec,
1142 p_stream->p_wf->wBitsPerSample,
1143 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
1145 char title[sizeof("Stream") + 10];
1146 input_info_category_t *p_cat;
1147 sprintf( title, "Stream %d", p_ogg->i_streams );
1148 p_cat = input_InfoCategory( p_input, title );
1149 input_AddInfo( p_cat, _("Type"), _("Audio") );
1150 input_AddInfo( p_cat, _("Codec"), "%.4s",
1151 (char *)&p_stream->i_fourcc );
1152 input_AddInfo( p_cat, _("Sample Rate"), "%d",
1153 p_stream->p_wf->nSamplesPerSec );
1154 input_AddInfo( p_cat, _("Bit Rate"), "%d",
1155 p_stream->p_wf->nAvgBytesPerSec * 8
1157 input_AddInfo( p_cat, _("Channels"), "%d",
1158 p_stream->p_wf->nChannels );
1159 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
1160 p_stream->p_wf->wBitsPerSample );
1163 /* Check for text (subtitles) header */
1164 else if( !strncmp(st->streamtype, "text", 4) )
1166 /* We need to get rid of the header packet */
1167 ogg_stream_packetout( &p_stream->os, &oggpacket );
1169 msg_Dbg( p_input, "found text subtitles header" );
1170 p_stream->i_cat = SPU_ES;
1171 p_stream->i_fourcc =
1172 VLC_FOURCC( 's', 'u', 'b', 't' );
1173 p_stream->f_rate = 1000; /* granulepos is in milisec */
1177 msg_Dbg( p_input, "stream %d has a header marker "
1178 "but is of an unknown type", p_ogg->i_streams-1 );
1185 msg_Dbg( p_input, "stream %d is of unknown type",
1186 p_ogg->i_streams-1 );
1193 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1194 return VLC_EGENERIC;
1197 /* This is the first data page, which means we are now finished
1198 * with the initial pages. We just need to store it in the relevant
1200 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1202 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1211 return VLC_EGENERIC;
1214 /*****************************************************************************
1215 * Activate: initializes ogg demux structures
1216 *****************************************************************************/
1217 static int Activate( vlc_object_t * p_this )
1219 input_thread_t *p_input = (input_thread_t *)p_this;
1223 p_input->p_demux_data = NULL;
1224 b_forced = ( ( *p_input->psz_demux )&&
1225 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
1227 /* Check if we are dealing with an ogg stream */
1228 if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
1231 /* Allocate p_ogg */
1232 if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
1234 msg_Err( p_input, "out of memory" );
1237 memset( p_ogg, 0, sizeof( demux_sys_t ) );
1238 p_input->p_demux_data = p_ogg;
1239 p_ogg->pp_stream = NULL;
1240 p_ogg->p_stream_video = NULL;
1241 p_ogg->p_stream_audio = NULL;
1242 p_ogg->p_stream_spu = NULL;
1244 /* Initialize the Ogg physical bitstream parser */
1245 ogg_sync_init( &p_ogg->oy );
1247 /*Set exported functions */
1248 p_input->pf_demux = Demux;
1249 p_input->pf_demux_control = Control;
1251 /* Initialize access plug-in structures. */
1252 if( p_input->i_mtu == 0 )
1254 /* Improve speed. */
1255 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
1258 /* Create one program */
1259 vlc_mutex_lock( &p_input->stream.stream_lock );
1260 if( input_InitStream( p_input, 0 ) == -1)
1262 vlc_mutex_unlock( &p_input->stream.stream_lock );
1263 msg_Err( p_input, "cannot init stream" );
1266 if( input_AddProgram( p_input, 0, 0) == NULL )
1268 vlc_mutex_unlock( &p_input->stream.stream_lock );
1269 msg_Err( p_input, "cannot add program" );
1272 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1273 vlc_mutex_unlock( &p_input->stream.stream_lock );
1275 /* Begnning of stream, tell the demux to look for elementary streams. */
1278 p_ogg->i_prev_sync_state = SYNCHRO_REINIT;
1283 Deactivate( (vlc_object_t *)p_input );
1288 /****************************************************************************
1289 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1290 * Elementary streams.
1291 ****************************************************************************/
1292 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg)
1296 /* Find the logical streams embedded in the physical stream and
1297 * initialize our p_ogg structure. */
1298 if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
1300 msg_Warn( p_input, "couldn't find any ogg logical stream" );
1301 return VLC_EGENERIC;
1304 vlc_mutex_lock( &p_input->stream.stream_lock );
1305 p_input->stream.i_mux_rate = 0;
1306 vlc_mutex_unlock( &p_input->stream.stream_lock );
1308 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1310 #define p_stream p_ogg->pp_stream[i_stream]
1311 vlc_mutex_lock( &p_input->stream.stream_lock );
1312 p_stream->p_es = input_AddES( p_input,
1313 p_input->stream.p_selected_program,
1315 p_stream->i_cat, NULL, 0 );
1316 p_input->stream.i_mux_rate += (p_stream->i_bitrate / ( 8 * 50 ));
1317 vlc_mutex_unlock( &p_input->stream.stream_lock );
1318 p_stream->p_es->i_stream_id = i_stream;
1319 p_stream->p_es->i_fourcc = p_stream->i_fourcc;
1320 p_stream->p_es->p_waveformatex = (void*)p_stream->p_wf;
1321 p_stream->p_es->p_bitmapinfoheader = (void*)p_stream->p_bih;
1323 p_stream->i_pcr = p_stream->i_previous_pcr = -1;
1324 p_stream->b_reinit = 0;
1328 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1330 #define p_stream p_ogg->pp_stream[i_stream]
1331 switch( p_stream->p_es->i_cat )
1334 if( (p_ogg->p_stream_video == NULL) )
1336 p_ogg->p_stream_video = p_stream;
1337 /* TODO add test to see if a decoder has been found */
1338 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1343 if( (p_ogg->p_stream_audio == NULL) )
1345 int i_audio = config_GetInt( p_input, "audio-channel" );
1346 if( i_audio == i_stream || i_audio <= 0 ||
1347 i_audio >= p_ogg->i_streams ||
1348 p_ogg->pp_stream[i_audio]->p_es->i_cat != AUDIO_ES )
1350 p_ogg->p_stream_audio = p_stream;
1351 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1357 if( (p_ogg->p_stream_spu == NULL) )
1359 /* for spu, default is none */
1360 int i_spu = config_GetInt( p_input, "spu-channel" );
1361 if( i_spu < 0 || i_spu >= p_ogg->i_streams ||
1362 p_ogg->pp_stream[i_spu]->p_es->i_cat != SPU_ES )
1366 else if( i_spu == i_stream )
1368 p_ogg->p_stream_spu = p_stream;
1369 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1380 /* we select the first audio and video ES */
1381 vlc_mutex_lock( &p_input->stream.stream_lock );
1382 if( !p_ogg->p_stream_video )
1384 msg_Warn( p_input, "no video stream found" );
1386 if( !p_ogg->p_stream_audio )
1388 msg_Warn( p_input, "no audio stream found!" );
1390 p_input->stream.p_selected_program->b_is_ok = 1;
1391 vlc_mutex_unlock( &p_input->stream.stream_lock );
1396 /****************************************************************************
1397 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1398 ****************************************************************************/
1399 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg )
1403 #define p_stream p_ogg->pp_stream[i_stream]
1404 vlc_mutex_lock( &p_input->stream.stream_lock );
1405 if( p_input->stream.i_pgrm_number )
1406 while( p_input->stream.p_selected_program->i_es_number )
1408 input_DelES( p_input,
1409 p_input->stream.p_selected_program->pp_es[0] );
1411 vlc_mutex_unlock( &p_input->stream.stream_lock );
1414 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1416 #define p_stream p_ogg->pp_stream[i_stream]
1417 vlc_mutex_lock( &p_input->stream.stream_lock );
1418 p_input->stream.i_mux_rate -= (p_stream->i_bitrate / ( 8 * 50 ));
1419 vlc_mutex_unlock( &p_input->stream.stream_lock );
1422 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1423 for( j = 0; j < p_ogg->pp_stream[i_stream]->i_packets_backup; j++ )
1425 free( p_ogg->pp_stream[i_stream]->p_packets_backup[j].packet );
1427 if( p_ogg->pp_stream[i_stream]->p_packets_backup)
1428 free( p_ogg->pp_stream[i_stream]->p_packets_backup );
1430 #if 0 /* hmmm, it's already freed in input_DelES() */
1431 if( p_ogg->pp_stream[i]->p_bih )
1432 free( p_ogg->pp_stream[i]->p_bih );
1433 if( p_ogg->pp_stream[i]->p_wf )
1434 free( p_ogg->pp_stream[i]->p_wf );
1437 free( p_ogg->pp_stream[i_stream] );
1441 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1442 p_ogg->pp_stream = NULL;
1443 p_ogg->i_streams = 0;
1444 p_ogg->p_stream_video = NULL;
1445 p_ogg->p_stream_audio = NULL;
1446 p_ogg->p_stream_spu = NULL;
1449 /*****************************************************************************
1450 * Deactivate: frees unused data
1451 *****************************************************************************/
1452 static void Deactivate( vlc_object_t *p_this )
1454 input_thread_t *p_input = (input_thread_t *)p_this;
1455 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
1459 /* Cleanup the bitstream parser */
1460 ogg_sync_clear( &p_ogg->oy );
1462 Ogg_EndOfStream( p_input, p_ogg );
1468 /*****************************************************************************
1469 * Demux: reads and demuxes data packets
1470 *****************************************************************************
1471 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1472 *****************************************************************************/
1473 static int Demux( input_thread_t * p_input )
1475 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1477 ogg_packet oggpacket;
1480 if( p_ogg->i_eos == p_ogg->i_streams )
1484 msg_Dbg( p_input, "end of a group of logical streams" );
1485 Ogg_EndOfStream( p_input, p_ogg );
1488 if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
1491 msg_Dbg( p_input, "beginning of a group of logical streams" );
1493 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
1494 input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
1497 #define p_stream p_ogg->pp_stream[i_stream]
1499 /* detect new selected/unselected streams */
1500 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1502 if( p_stream->p_es )
1504 if( p_stream->p_es->p_decoder_fifo &&
1505 !p_stream->i_activated )
1507 Ogg_ElemStreamStart( p_input, p_ogg, i_stream );
1510 if( !p_stream->p_es->p_decoder_fifo &&
1511 p_stream->i_activated )
1513 Ogg_ElemStreamStop( p_input, p_ogg, i_stream );
1518 /* search for new video and audio stream to select
1519 * if current have been unselected */
1520 if( ( !p_ogg->p_stream_video )
1521 || ( !p_ogg->p_stream_video->p_es->p_decoder_fifo ) )
1523 p_ogg->p_stream_video = NULL;
1524 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1526 if( ( p_stream->i_cat == VIDEO_ES )
1527 &&( p_stream->p_es->p_decoder_fifo ) )
1529 p_ogg->p_stream_video = p_stream;
1534 if( ( !p_ogg->p_stream_audio )
1535 ||( !p_ogg->p_stream_audio->p_es->p_decoder_fifo ) )
1537 p_ogg->p_stream_audio = NULL;
1538 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1540 if( ( p_stream->i_cat == AUDIO_ES )
1541 &&( p_stream->p_es->p_decoder_fifo ) )
1543 p_ogg->p_stream_audio = p_stream;
1549 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1551 msg_Warn( p_input, "synchro reinit" );
1553 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1555 /* we'll trash all the data until we find the next pcr */
1556 p_stream->b_reinit = 1;
1557 p_stream->i_pcr = -1;
1558 p_stream->i_interpolated_pcr = -1;
1560 if( p_ogg->i_prev_sync_state != SYNCHRO_REINIT )
1561 ogg_sync_reset( &p_ogg->oy );
1564 p_ogg->i_prev_sync_state =
1565 p_input->stream.p_selected_program->i_synchro_state;
1568 * Demux an ogg page from the stream
1570 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1575 /* Test for End of Stream */
1576 if( ogg_page_eos( &oggpage ) ) p_ogg->i_eos++;
1579 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1581 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1584 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1586 if( !p_stream->p_es )
1591 if( p_stream->b_reinit )
1593 /* If synchro is re-initialized we need to drop all the packets
1594 * until we find a new dated one. */
1595 Ogg_UpdatePCR( p_stream, &oggpacket );
1597 if( p_stream->i_pcr >= 0 )
1599 p_stream->b_reinit = 0;
1603 p_stream->i_interpolated_pcr = -1;
1607 /* An Ogg/vorbis packet contains an end date granulepos */
1608 if( p_stream->i_fourcc == VLC_FOURCC( 'v','o','r','b' ) ||
1609 p_stream->i_fourcc == VLC_FOURCC( 's','p','x',' ' ) ||
1610 p_stream->i_fourcc == VLC_FOURCC( 'f','l','a','c' ) )
1612 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1614 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1618 input_ClockManageRef( p_input,
1619 p_input->stream.p_selected_program, p_ogg->i_pcr );
1625 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1630 i_stream = 0; p_ogg->i_pcr = -1;
1631 for( ; i_stream < p_ogg->i_streams; i_stream++ )
1633 if( p_stream->i_cat == SPU_ES )
1635 if( p_stream->i_interpolated_pcr < 0 )
1638 if( p_ogg->i_pcr < 0 || p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1639 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1642 if( p_input->stream.p_selected_program->i_synchro_state != SYNCHRO_REINIT )
1644 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1653 /*****************************************************************************
1655 *****************************************************************************/
1656 static int Control( input_thread_t *p_input, int i_query, va_list args )
1658 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1663 case DEMUX_GET_TIME:
1664 pi64 = (int64_t*)va_arg( args, int64_t * );
1665 *pi64 = p_ogg->i_pcr * 100 / 9;
1669 return demux_vaControlDefault( p_input, i_query, args );