1 /*****************************************************************************
2 * ogg.c : ogg stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ogg.c,v 1.11 2002/11/21 09:39:39 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
40 #define PAGES_READ_ONCE 1
42 /*****************************************************************************
43 * Definitions of structures and functions used by this plugins
44 *****************************************************************************/
45 typedef struct logical_stream_s
47 ogg_stream_state os; /* logical stream of packets */
50 int i_cat; /* AUDIO_ES, VIDEO_ES */
52 vlc_fourcc_t i_fourcc;
55 es_descriptor_t *p_es;
56 int b_selected; /* newly selected */
58 /* the header of some logical streams (eg vorbis) contain essential
59 * data for the decoder. We back them up here in case we need to re-feed
60 * them to the decoder. */
63 ogg_packet *p_packets_backup;
65 /* program clock reference (in units of 90kHz) derived from the previous
68 mtime_t i_interpolated_pcr;
70 /* info from logical streams */
75 /* codec specific stuff */
76 BITMAPINFOHEADER *p_bih;
78 int i_theora_keyframe_granule_shift;
84 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
86 int i_streams; /* number of logical bitstreams */
87 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
89 /* current audio and video es */
90 logical_stream_t *p_stream_video;
91 logical_stream_t *p_stream_audio;
92 logical_stream_t *p_stream_spu;
94 /* program clock reference (in units of 90kHz) derived from the pcr of
103 /* OggDS headers for the new header format (used in ogm files) */
104 typedef struct stream_header_video
108 } stream_header_video;
110 typedef struct stream_header_audio
112 ogg_int16_t channels;
113 ogg_int16_t blockalign;
114 ogg_int32_t avgbytespersec;
115 } stream_header_audio;
117 typedef struct stream_header
122 ogg_int32_t size; /* size of the structure */
124 ogg_int64_t time_unit; /* in reference time */
125 ogg_int64_t samples_per_unit;
126 ogg_int32_t default_len; /* in media time */
128 ogg_int32_t buffersize;
129 ogg_int16_t bits_per_sample;
134 stream_header_video video;
136 stream_header_audio audio;
140 /* Some defines from OggDS */
141 #define PACKET_TYPE_HEADER 0x01
142 #define PACKET_TYPE_BITS 0x07
143 #define PACKET_LEN_BITS01 0xc0
144 #define PACKET_LEN_BITS2 0x02
145 #define PACKET_IS_SYNCPOINT 0x08
147 /* Some functions to manipulate memory */
148 static uint16_t GetWLE( uint8_t *p_buff )
150 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
153 static uint32_t GetDWLE( uint8_t *p_buff )
155 return( p_buff[0] + ( p_buff[1] <<8 ) +
156 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
159 static uint64_t GetQWLE( uint8_t *p_buff )
161 return( GetDWLE( p_buff ) + ( ((uint64_t)GetDWLE( p_buff + 4 )) << 32 ) );
163 /*****************************************************************************
165 *****************************************************************************/
166 static int Activate ( vlc_object_t * );
167 static void Deactivate( vlc_object_t * );
168 static int Demux ( input_thread_t * );
170 /* Stream managment */
171 static int Ogg_StreamStart ( input_thread_t *, demux_sys_t *, int );
172 static void Ogg_StreamStop ( input_thread_t *, demux_sys_t *, int );
174 /* Bitstream manipulation */
175 static int Ogg_Check ( input_thread_t *p_input );
176 static int Ogg_ReadPage ( input_thread_t *, demux_sys_t *, ogg_page * );
177 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
178 static void Ogg_DecodePacket ( input_thread_t *p_input,
179 logical_stream_t *p_stream, ogg_packet * );
180 static int Ogg_FindLogicalStreams( input_thread_t *p_input,
181 demux_sys_t *p_ogg );
183 /*****************************************************************************
185 *****************************************************************************/
187 set_description( _("ogg stream demux" ) );
188 set_capability( "demux", 50 );
189 set_callbacks( Activate, Deactivate );
190 add_shortcut( "ogg" );
193 /*****************************************************************************
195 *****************************************************************************/
196 static int Ogg_StreamStart( input_thread_t *p_input,
197 demux_sys_t *p_ogg, int i_stream )
199 #define p_stream p_ogg->pp_stream[i_stream]
200 if( !p_stream->p_es )
202 msg_Warn( p_input, "stream[%d] unselectable", i_stream );
205 if( p_stream->i_activated )
207 msg_Warn( p_input, "stream[%d] already selected", i_stream );
211 if( !p_stream->p_es->p_decoder_fifo )
213 vlc_mutex_lock( &p_input->stream.stream_lock );
214 input_SelectES( p_input, p_stream->p_es );
215 vlc_mutex_unlock( &p_input->stream.stream_lock );
217 p_stream->i_activated = p_stream->p_es->p_decoder_fifo ? 1 : 0;
219 /* Feed the backup header to the decoder */
220 if( !p_stream->b_force_backup )
223 for( i = 0; i < p_stream->i_packets_backup; i++ )
225 Ogg_DecodePacket( p_input, p_stream,
226 &p_stream->p_packets_backup[i] );
230 return( p_stream->i_activated );
234 static void Ogg_StreamStop( input_thread_t *p_input,
235 demux_sys_t *p_ogg, int i_stream )
237 #define p_stream p_ogg->pp_stream[i_stream]
239 if( !p_stream->i_activated )
241 msg_Warn( p_input, "stream[%d] already unselected", i_stream );
245 if( p_stream->p_es->p_decoder_fifo )
247 vlc_mutex_lock( &p_input->stream.stream_lock );
248 input_UnselectES( p_input, p_stream->p_es );
249 vlc_mutex_unlock( &p_input->stream.stream_lock );
252 p_stream->i_activated = 0;
257 /****************************************************************************
258 * Ogg_Check: Check we are dealing with an ogg stream.
259 ****************************************************************************/
260 static int Ogg_Check( input_thread_t *p_input )
263 int i_size = input_Peek( p_input, &p_peek, 4 );
265 /* Check for the Ogg capture pattern */
266 if( !(i_size>3) || !(p_peek[0] == 'O') || !(p_peek[1] == 'g') ||
267 !(p_peek[2] == 'g') || !(p_peek[3] == 'S') )
270 /* FIXME: Capture pattern might not be enough so we can also check for the
271 * the first complete page */
276 /****************************************************************************
277 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
278 ****************************************************************************
279 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
280 * are at the end of stream.
281 ****************************************************************************/
282 static int Ogg_ReadPage( input_thread_t *p_input, demux_sys_t *p_ogg,
283 ogg_page *p_oggpage )
286 data_packet_t *p_data;
289 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
291 i_read = input_SplitBuffer( p_input, &p_data, OGG_BLOCK_SIZE );
295 p_buffer = ogg_sync_buffer( &p_ogg->oy, i_read );
296 p_input->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, i_read );
297 ogg_sync_wrote( &p_ogg->oy, i_read );
298 input_DeletePacket( p_input->p_method_data, p_data );
304 /****************************************************************************
305 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
307 ****************************************************************************/
308 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
309 ogg_packet *p_oggpacket )
312 /* Convert the next granulepos into a pcr */
313 if( p_oggpacket->granulepos >= 0 )
315 if( p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
317 p_stream->i_pcr = p_oggpacket->granulepos * 90000
322 ogg_int64_t iframe = p_oggpacket->granulepos >>
323 p_stream->i_theora_keyframe_granule_shift;
324 ogg_int64_t pframe = p_oggpacket->granulepos -
325 ( iframe << p_stream->i_theora_keyframe_granule_shift );
327 p_stream->i_pcr = ( iframe + pframe ) * 90000
331 p_stream->i_interpolated_pcr = p_stream->i_pcr;
335 /* FIXME: ffmpeg doesn't like null pts */
336 if( p_stream->i_cat == VIDEO_ES )
337 /* 1 frame per packet */
338 p_stream->i_pcr += (90000 / p_stream->f_rate);
340 p_stream->i_pcr = -1;
342 /* no granulepos available, try to interpolate the pcr */
343 if( p_stream->i_bitrate )
344 p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
345 / p_stream->i_bitrate / 8 );
347 p_stream->i_interpolated_pcr = -1;
351 /****************************************************************************
352 * Ogg_DecodePacket: Decode an Ogg packet.
353 ****************************************************************************/
354 static void Ogg_DecodePacket( input_thread_t *p_input,
355 logical_stream_t *p_stream,
356 ogg_packet *p_oggpacket )
359 data_packet_t *p_data;
360 //demux_sys_t *p_ogg = p_input->p_demux_data;
361 int i_header_len = 0;
363 if( p_stream->b_force_backup )
365 /* Backup the ogg packet (likely an header packet) */
366 ogg_packet *p_packet_backup;
367 p_stream->i_packets_backup++;
368 p_stream->p_packets_backup =
369 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
370 sizeof(ogg_packet) );
373 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
375 p_packet_backup->bytes = p_oggpacket->bytes;
376 p_packet_backup->granulepos = p_oggpacket->granulepos;
377 p_packet_backup->packet = malloc( p_oggpacket->bytes );
378 if( !p_packet_backup->packet ) return;
379 memcpy( p_packet_backup->packet, p_oggpacket->packet,
380 p_oggpacket->bytes );
382 switch( p_stream->i_fourcc )
384 case VLC_FOURCC( 'v','o','r','b' ):
385 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
389 p_stream->b_force_backup = 0;
394 if( !p_stream->p_es->p_decoder_fifo )
396 /* This stream isn't currently selected so we don't need to decode it,
397 * but we do need to store its pcr as it might be selected later on. */
398 Ogg_UpdatePCR( p_stream, p_oggpacket );
403 if( !( p_pes = input_NewPES( p_input->p_method_data ) ) )
407 if( !( p_data = input_NewPacket( p_input->p_method_data,
408 p_oggpacket->bytes ) ) )
410 input_DeletePES( p_input->p_method_data, p_pes );
414 /* Convert the pcr into a pts */
415 if( p_stream->i_cat != SPU_ES )
417 p_pes->i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
418 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
423 /* Of course subtitles had to be different! */
424 p_pes->i_pts = ( p_oggpacket->granulepos < 0 ) ? 0 :
425 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
426 p_oggpacket->granulepos * 90000 /
430 /* Convert the next granulepos into a pcr */
431 Ogg_UpdatePCR( p_stream, p_oggpacket );
433 p_pes->i_nb_data = 1;
434 p_pes->i_dts = p_oggpacket->granulepos;
435 p_pes->p_first = p_pes->p_last = p_data;
436 p_pes->i_pes_size = p_oggpacket->bytes;
438 if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) &&
439 p_stream->i_fourcc != VLC_FOURCC( 't','a','r','k' ) &&
440 p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
442 /* Remove the header from the packet */
443 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
444 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
447 p_pes->i_pes_size -= i_header_len;
451 if( p_stream->i_fourcc == VLC_FOURCC( 't','a','r','k' ) )
453 /* FIXME: the biggest hack I've ever done */
454 msg_Warn( p_input, "tark pts: %lli, granule: %i",
455 p_pes->i_pts, p_pes->i_dts );
459 memcpy( p_data->p_payload_start,
460 p_oggpacket->packet + i_header_len,
461 p_oggpacket->bytes - i_header_len );
463 p_data->p_payload_end = p_data->p_payload_start + p_pes->i_pes_size;
464 p_data->b_discard_payload = 0;
466 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
469 /****************************************************************************
470 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
471 * stream and fill p_ogg.
472 *****************************************************************************
473 * The initial page of a logical stream is marked as a 'bos' page.
474 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
475 * together and all of the initial pages must appear before any data pages.
477 * On success this function returns VLC_SUCCESS.
478 ****************************************************************************/
479 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
481 ogg_packet oggpacket;
485 while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
487 if( ogg_page_bos( &oggpage ) )
490 /* All is wonderful in our fine fine little world.
491 * We found the beginning of our first logical stream. */
492 while( ogg_page_bos( &oggpage ) )
496 realloc( p_ogg->pp_stream, p_ogg->i_streams *
497 sizeof(logical_stream_t *) );
499 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
501 p_stream = malloc( sizeof(logical_stream_t) );
502 memset( p_stream, 0, sizeof(logical_stream_t) );
504 /* Setup the logical stream */
505 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
506 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
508 /* Extract the initial header from the first page and verify
509 * the codec type of tis Ogg bitstream */
510 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
512 /* error. stream version mismatch perhaps */
513 msg_Err( p_input, "Error reading first page of "
514 "Ogg bitstream data" );
518 /* FIXME: check return value */
519 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
521 /* Check for Vorbis header */
522 if( oggpacket.bytes >= 7 &&
523 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
527 msg_Dbg( p_input, "found vorbis header" );
528 p_stream->i_cat = AUDIO_ES;
529 p_stream->i_fourcc = VLC_FOURCC( 'v','o','r','b' );
531 /* Signal that we want to keep a backup of the vorbis
532 * stream headers. They will be used when switching between
534 p_stream->b_force_backup = 1;
536 /* Cheat and get additionnal info ;) */
537 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
538 oggpack_adv( &opb, 96 );
539 p_stream->f_rate = oggpack_read( &opb, 32 );
540 oggpack_adv( &opb, 32 );
541 p_stream->i_bitrate = oggpack_read( &opb, 32 );
543 /* Check for Theora header */
544 else if( oggpacket.bytes >= 7 &&
545 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
550 int i_fps_denominator;
551 int i_keyframe_frequency_force;
554 msg_Dbg( p_input, "found theora header" );
556 p_stream->i_cat = VIDEO_ES;
557 p_stream->i_fourcc = VLC_FOURCC( 't','h','e','o' );
559 /* Cheat and get additionnal info ;) */
560 oggpackB_readinit(&opb, oggpacket.packet, oggpacket.bytes);
561 oggpackB_adv( &opb, 56 );
562 oggpackB_read( &opb, 8 ); /* major version num */
563 oggpackB_read( &opb, 8 ); /* minor version num */
564 oggpackB_read( &opb, 8 ); /* subminor version num */
565 oggpackB_read( &opb, 16 ) /*<< 4*/; /* width */
566 oggpackB_read( &opb, 16 ) /*<< 4*/; /* height */
567 i_fps_numerator = oggpackB_read( &opb, 32 );
568 i_fps_denominator = oggpackB_read( &opb, 32 );
569 oggpackB_read( &opb, 24 ); /* aspect_numerator */
570 oggpackB_read( &opb, 24 ); /* aspect_denominator */
571 i_keyframe_frequency_force = 1 << oggpackB_read( &opb, 5 );
572 p_stream->i_bitrate = oggpackB_read( &opb, 24 );
573 oggpackB_read(&opb,6); /* quality */
575 /* granule_shift = i_log( frequency_force -1 ) */
576 p_stream->i_theora_keyframe_granule_shift = 0;
577 i_keyframe_frequency_force--;
578 while( i_keyframe_frequency_force )
580 p_stream->i_theora_keyframe_granule_shift++;
581 i_keyframe_frequency_force >>= 1;
584 p_stream->f_rate = (float)i_fps_numerator /
587 "found theora header, bitrate: %i, rate: %f",
588 p_stream->i_bitrate, p_stream->f_rate );
589 #else /* HAVE_OGGPACKB */
590 msg_Dbg( p_input, "the ogg demuxer has been compiled "
591 "without support for the oggpackB extension."
592 "The theora stream won't be decoded." );
596 #endif /* HAVE_OGGPACKB */
598 /* Check for Tarkin header */
599 else if( oggpacket.bytes >= 7 &&
600 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
604 msg_Dbg( p_input, "found tarkin header" );
605 p_stream->i_cat = VIDEO_ES;
606 p_stream->i_fourcc = VLC_FOURCC( 't','a','r','k' );
608 /* Cheat and get additionnal info ;) */
609 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
610 oggpack_adv( &opb, 88 );
611 oggpack_adv( &opb, 104 );
612 p_stream->i_bitrate = oggpack_read( &opb, 32 );
613 p_stream->f_rate = 2; /* FIXME */
615 "found tarkin header, bitrate: %i, rate: %f",
616 p_stream->i_bitrate, p_stream->f_rate );
618 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
619 == PACKET_TYPE_HEADER &&
620 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
622 stream_header *st = (stream_header *)(oggpacket.packet+1);
624 /* Check for video header (new format) */
625 if( !strncmp( st->streamtype, "video", 5 ) )
627 p_stream->i_cat = VIDEO_ES;
629 /* We need to get rid of the header packet */
630 ogg_stream_packetout( &p_stream->os, &oggpacket );
632 p_stream->p_bih = (BITMAPINFOHEADER *)
633 malloc( sizeof(BITMAPINFOHEADER) );
634 if( !p_stream->p_bih )
636 /* Mem allocation error, just ignore the stream */
641 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
642 p_stream->p_bih->biCompression=
643 p_stream->i_fourcc = VLC_FOURCC( st->subtype[0],
647 msg_Dbg( p_input, "found video header of type: %.4s",
648 (char *)&p_stream->i_fourcc );
650 p_stream->f_rate = 10000000.0 /
651 GetQWLE((uint8_t *)&st->time_unit);
652 p_stream->p_bih->biBitCount =
653 GetWLE((uint8_t *)&st->bits_per_sample);
654 p_stream->p_bih->biWidth =
655 GetDWLE((uint8_t *)&st->sh.video.width);
656 p_stream->p_bih->biHeight =
657 GetDWLE((uint8_t *)&st->sh.video.height);
658 p_stream->p_bih->biPlanes= 1 ;
659 p_stream->p_bih->biSizeImage =
660 (p_stream->p_bih->biBitCount >> 3) *
661 p_stream->p_bih->biWidth *
662 p_stream->p_bih->biHeight;
665 "fps: %f, width:%i; height:%i, bitcount:%i",
666 p_stream->f_rate, p_stream->p_bih->biWidth,
667 p_stream->p_bih->biHeight,
668 p_stream->p_bih->biBitCount);
670 p_stream->i_bitrate = 0;
672 /* Check for audio header (new format) */
673 else if( !strncmp( st->streamtype, "audio", 5 ) )
677 p_stream->i_cat = AUDIO_ES;
679 /* We need to get rid of the header packet */
680 ogg_stream_packetout( &p_stream->os, &oggpacket );
682 p_stream->p_wf = (WAVEFORMATEX *)
683 malloc( sizeof(WAVEFORMATEX) );
684 if( !p_stream->p_wf )
686 /* Mem allocation error, just ignore the stream */
692 memcpy( p_buffer, st->subtype, 4 );
694 p_stream->p_wf->wFormatTag = strtol(p_buffer,NULL,16);
695 p_stream->p_wf->nChannels =
696 GetWLE((uint8_t *)&st->sh.audio.channels);
697 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
698 GetQWLE((uint8_t *)&st->samples_per_unit);
699 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
700 GetDWLE((uint8_t *)&st->sh.audio.avgbytespersec);
701 p_stream->i_bitrate *= 8;
702 p_stream->p_wf->nBlockAlign =
703 GetWLE((uint8_t *)&st->sh.audio.blockalign);
704 p_stream->p_wf->wBitsPerSample =
705 GetWLE((uint8_t *)&st->bits_per_sample);
706 p_stream->p_wf->cbSize = 0;
708 switch( p_stream->p_wf->wFormatTag )
710 case WAVE_FORMAT_PCM:
712 VLC_FOURCC( 'a', 'r', 'a', 'w' );
714 case WAVE_FORMAT_MPEG:
715 case WAVE_FORMAT_MPEGLAYER3:
717 VLC_FOURCC( 'm', 'p', 'g', 'a' );
719 case WAVE_FORMAT_A52:
721 VLC_FOURCC( 'a', '5', '2', ' ' );
723 case WAVE_FORMAT_WMA1:
725 VLC_FOURCC( 'w', 'm', 'a', '1' );
727 case WAVE_FORMAT_WMA2:
729 VLC_FOURCC( 'w', 'm', 'a', '2' );
732 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
733 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
734 p_stream->p_wf->wFormatTag & 0xff );
737 msg_Dbg( p_input, "found audio header of type: %.4s",
738 (char *)&p_stream->i_fourcc );
739 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
740 "%dbits/sample %dkb/s",
741 p_stream->p_wf->wFormatTag,
742 p_stream->p_wf->nChannels,
743 p_stream->p_wf->nSamplesPerSec,
744 p_stream->p_wf->wBitsPerSample,
745 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
747 /* Check for text (subtitles) header */
748 else if( !strncmp(st->streamtype, "text", 4) )
750 /* We need to get rid of the header packet */
751 ogg_stream_packetout( &p_stream->os, &oggpacket );
753 msg_Dbg( p_input, "found text subtitles header" );
754 p_stream->i_cat = SPU_ES;
756 VLC_FOURCC( 's', 'u', 'b', 't' );
757 p_stream->f_rate = 1000; /* granulepos is in milisec */
761 msg_Dbg( p_input, "stream %d has a header marker "
762 "but is of an unknown type", p_ogg->i_streams-1 );
769 msg_Dbg( p_input, "stream %d is of unknown type",
770 p_ogg->i_streams-1 );
777 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
781 /* This is the first data page, which means we are now finished
782 * with the initial pages. We just need to store it in the relevant
784 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
786 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
798 /*****************************************************************************
799 * Activate: initializes ogg demux structures
800 *****************************************************************************/
801 static int Activate( vlc_object_t * p_this )
803 int i_stream, b_forced;
805 input_thread_t *p_input = (input_thread_t *)p_this;
807 p_input->p_demux_data = NULL;
808 b_forced = ( ( *p_input->psz_demux )&&
809 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
811 /* Check if we are dealing with an ogg stream */
812 if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
816 if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
818 msg_Err( p_input, "out of memory" );
821 memset( p_ogg, 0, sizeof( demux_sys_t ) );
822 p_input->p_demux_data = p_ogg;
825 p_ogg->b_seekable = ( ( p_input->stream.b_seekable )
826 &&( p_input->stream.i_method == INPUT_METHOD_FILE ) );
828 /* Initialize the Ogg physical bitstream parser */
829 ogg_sync_init( &p_ogg->oy );
831 /* Find the logical streams embedded in the physical stream and
832 * initialize our p_ogg structure. */
833 if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
835 msg_Err( p_input, "couldn't find an ogg logical stream" );
839 /* Set the demux function */
840 p_input->pf_demux = Demux;
842 /* Initialize access plug-in structures. */
843 if( p_input->i_mtu == 0 )
846 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
849 /* Create one program */
850 vlc_mutex_lock( &p_input->stream.stream_lock );
851 if( input_InitStream( p_input, 0 ) == -1)
853 vlc_mutex_unlock( &p_input->stream.stream_lock );
854 msg_Err( p_input, "cannot init stream" );
857 if( input_AddProgram( p_input, 0, 0) == NULL )
859 vlc_mutex_unlock( &p_input->stream.stream_lock );
860 msg_Err( p_input, "cannot add program" );
863 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
864 p_input->stream.i_mux_rate = 0;
865 vlc_mutex_unlock( &p_input->stream.stream_lock );
867 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
869 #define p_stream p_ogg->pp_stream[i_stream]
870 vlc_mutex_lock( &p_input->stream.stream_lock );
871 p_stream->p_es = input_AddES( p_input,
872 p_input->stream.p_selected_program,
873 p_ogg->i_streams + 1, 0 );
874 p_input->stream.i_mux_rate += (p_stream->i_bitrate / ( 8 * 50 ));
875 vlc_mutex_unlock( &p_input->stream.stream_lock );
876 p_stream->p_es->i_stream_id = p_stream->p_es->i_id = i_stream;
877 p_stream->p_es->i_fourcc = p_stream->i_fourcc;
878 p_stream->p_es->i_cat = p_stream->i_cat;
879 p_stream->p_es->p_demux_data = p_stream->p_bih ?
880 (void *)p_stream->p_bih : (void *)p_stream->p_wf;
884 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
886 #define p_stream p_ogg->pp_stream[i_stream]
887 switch( p_stream->p_es->i_cat )
890 if( (p_ogg->p_stream_video == NULL) )
892 p_ogg->p_stream_video = p_stream;
893 /* TODO add test to see if a decoder has been found */
894 Ogg_StreamStart( p_input, p_ogg, i_stream );
899 if( (p_ogg->p_stream_audio == NULL) )
901 int i_audio = config_GetInt( p_input, "audio-channel" );
902 if( i_audio == i_stream || i_audio <= 0 ||
903 i_audio >= p_ogg->i_streams ||
904 p_ogg->pp_stream[i_audio]->p_es->i_cat != AUDIO_ES )
906 p_ogg->p_stream_audio = p_stream;
907 Ogg_StreamStart( p_input, p_ogg, i_stream );
913 if( (p_ogg->p_stream_spu == NULL) )
915 /* for spu, default is none */
916 int i_spu = config_GetInt( p_input, "spu-channel" );
917 if( i_spu < 0 || i_spu >= p_ogg->i_streams ||
918 p_ogg->pp_stream[i_spu]->p_es->i_cat != SPU_ES )
922 else if( i_spu == i_stream )
924 p_ogg->p_stream_spu = p_stream;
925 Ogg_StreamStart( p_input, p_ogg, i_stream );
936 /* we select the first audio and video ES */
937 vlc_mutex_lock( &p_input->stream.stream_lock );
938 if( !p_ogg->p_stream_video )
940 msg_Warn( p_input, "no video stream found" );
942 if( !p_ogg->p_stream_audio )
944 msg_Warn( p_input, "no audio stream found!" );
946 p_input->stream.p_selected_program->b_is_ok = 1;
947 vlc_mutex_unlock( &p_input->stream.stream_lock );
949 /* Call the pace control */
950 input_ClockManageRef( p_input,
951 p_input->stream.p_selected_program,
957 Deactivate( (vlc_object_t *)p_input );
962 /*****************************************************************************
963 * Deactivate: frees unused data
964 *****************************************************************************/
965 static void Deactivate( vlc_object_t *p_this )
967 input_thread_t *p_input = (input_thread_t *)p_this;
968 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
973 /* Cleanup the bitstream parser */
974 ogg_sync_clear( &p_ogg->oy );
976 for( i = 0; i < p_ogg->i_streams; i++ )
978 ogg_stream_clear( &p_ogg->pp_stream[i]->os );
979 for( j = 0; j < p_ogg->pp_stream[i]->i_packets_backup; j++ )
981 free( p_ogg->pp_stream[i]->p_packets_backup[j].packet );
983 if( p_ogg->pp_stream[i]->p_packets_backup)
984 free( p_ogg->pp_stream[i]->p_packets_backup );
985 #if 0 /* hmmm, it's already freed in input_DelES() */
986 if( p_ogg->pp_stream[i]->p_bih )
987 free( p_ogg->pp_stream[i]->p_bih );
988 if( p_ogg->pp_stream[i]->p_wf )
989 free( p_ogg->pp_stream[i]->p_wf );
991 free( p_ogg->pp_stream[i] );
993 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
999 /*****************************************************************************
1000 * Demux: reads and demuxes data packets
1001 *****************************************************************************
1002 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1003 *****************************************************************************/
1004 static int Demux( input_thread_t * p_input )
1006 int i, i_stream, b_eos = 0;
1008 ogg_packet oggpacket;
1009 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1011 #define p_stream p_ogg->pp_stream[i_stream]
1012 /* detect new selected/unselected streams */
1013 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1015 if( p_stream->p_es )
1017 if( p_stream->p_es->p_decoder_fifo &&
1018 !p_stream->i_activated )
1020 Ogg_StreamStart( p_input, p_ogg, i_stream );
1023 if( !p_stream->p_es->p_decoder_fifo &&
1024 p_stream->i_activated )
1026 Ogg_StreamStop( p_input, p_ogg, i_stream );
1031 /* search for new video and audio stream to select
1032 * if current have been unselected */
1033 if( ( !p_ogg->p_stream_video )
1034 || ( !p_ogg->p_stream_video->p_es->p_decoder_fifo ) )
1036 p_ogg->p_stream_video = NULL;
1037 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1039 if( ( p_stream->i_cat == VIDEO_ES )
1040 &&( p_stream->p_es->p_decoder_fifo ) )
1042 p_ogg->p_stream_video = p_stream;
1047 if( ( !p_ogg->p_stream_audio )
1048 ||( !p_ogg->p_stream_audio->p_es->p_decoder_fifo ) )
1050 p_ogg->p_stream_audio = NULL;
1051 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1053 if( ( p_stream->i_cat == AUDIO_ES )
1054 &&( p_stream->p_es->p_decoder_fifo ) )
1056 p_ogg->p_stream_audio = p_stream;
1062 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1064 msg_Warn( p_input, "synchro reinit" );
1066 /* An ogg packet does only contain the starting date of the next
1067 * packet, not its own starting date.
1068 * As a quick work around, we just skip an oggpage */
1070 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1072 /* we'll trash all the data until we find the next pcr */
1073 p_stream->b_reinit = 1;
1074 p_stream->i_pcr = -1;
1075 p_stream->i_interpolated_pcr = -1;
1077 p_ogg->b_reinit = 1;
1082 * Demux ogg pages from the stream
1084 for( i = 0; i < PAGES_READ_ONCE || p_ogg->b_reinit; i++ )
1086 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1092 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1094 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1097 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1099 if( !p_stream->p_es )
1104 if( p_stream->b_reinit )
1106 if( oggpacket.granulepos >= 0 )
1108 p_stream->b_reinit = 0;
1110 /* Convert the next granulepos into a pcr */
1111 Ogg_UpdatePCR( p_stream, &oggpacket );
1113 /* Call the pace control to reinitialize
1114 * the system clock */
1115 input_ClockManageRef( p_input,
1116 p_input->stream.p_selected_program,
1119 if( (!p_ogg->p_stream_video ||
1120 !p_ogg->p_stream_video->b_reinit) &&
1121 (!p_ogg->p_stream_audio ||
1122 !p_ogg->p_stream_audio->b_reinit) )
1124 p_ogg->b_reinit = 0;
1130 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1136 p_ogg->i_pcr = INT_MAX;
1137 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1139 if( p_stream->i_cat == SPU_ES )
1142 if( p_stream->i_interpolated_pcr >= 0 &&
1143 p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1144 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1149 /* Call the pace control */
1150 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1153 /* Did we reach the end of stream ? */
1154 return( b_eos ? 0 : 1 );