1 /*****************************************************************************
2 * ogg.c : ogg stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: ogg.c,v 1.21 2003/03/21 02:05:20 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 we can't then don't touch the old value. */
344 if( p_stream->i_bitrate )
345 p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
346 / p_stream->i_bitrate / 8 );
350 /****************************************************************************
351 * Ogg_DecodePacket: Decode an Ogg packet.
352 ****************************************************************************/
353 static void Ogg_DecodePacket( input_thread_t *p_input,
354 logical_stream_t *p_stream,
355 ogg_packet *p_oggpacket )
358 data_packet_t *p_data;
359 vlc_bool_t b_trash = VLC_FALSE;
360 int i_header_len = 0;
362 if( p_stream->b_force_backup )
364 /* Backup the ogg packet (likely an header packet) */
365 ogg_packet *p_packet_backup;
366 p_stream->i_packets_backup++;
367 p_stream->p_packets_backup =
368 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
369 sizeof(ogg_packet) );
372 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
374 p_packet_backup->bytes = p_oggpacket->bytes;
375 p_packet_backup->granulepos = p_oggpacket->granulepos;
376 p_packet_backup->packet = malloc( p_oggpacket->bytes );
377 if( !p_packet_backup->packet ) return;
378 memcpy( p_packet_backup->packet, p_oggpacket->packet,
379 p_oggpacket->bytes );
381 switch( p_stream->i_fourcc )
383 case VLC_FOURCC( 'v','o','r','b' ):
384 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
388 p_stream->b_force_backup = 0;
393 vlc_mutex_lock( &p_input->stream.control.control_lock );
394 if( p_stream->i_cat == AUDIO_ES && p_input->stream.control.b_mute )
398 vlc_mutex_unlock( &p_input->stream.control.control_lock );
400 if( !p_stream->p_es->p_decoder_fifo || b_trash )
402 /* This stream isn't currently selected so we don't need to decode it,
403 * but we do need to store its pcr as it might be selected later on. */
404 Ogg_UpdatePCR( p_stream, p_oggpacket );
409 if( !( p_pes = input_NewPES( p_input->p_method_data ) ) )
413 if( !( p_data = input_NewPacket( p_input->p_method_data,
414 p_oggpacket->bytes ) ) )
416 input_DeletePES( p_input->p_method_data, p_pes );
420 /* Convert the pcr into a pts */
421 if( p_stream->i_cat != SPU_ES )
423 p_pes->i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
424 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
429 /* Of course subtitles had to be different! */
430 p_pes->i_pts = ( p_oggpacket->granulepos < 0 ) ? 0 :
431 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
432 p_oggpacket->granulepos * 90000 /
436 /* Convert the next granulepos into a pcr */
437 Ogg_UpdatePCR( p_stream, p_oggpacket );
439 p_pes->i_nb_data = 1;
440 p_pes->i_dts = p_oggpacket->granulepos;
441 p_pes->p_first = p_pes->p_last = p_data;
442 p_pes->i_pes_size = p_oggpacket->bytes;
444 if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) &&
445 p_stream->i_fourcc != VLC_FOURCC( 't','a','r','k' ) &&
446 p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
448 /* Remove the header from the packet */
449 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
450 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
453 p_pes->i_pes_size -= i_header_len;
457 if( p_stream->i_fourcc == VLC_FOURCC( 't','a','r','k' ) )
459 /* FIXME: the biggest hack I've ever done */
460 msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
461 p_pes->i_pts, p_pes->i_dts );
465 memcpy( p_data->p_payload_start,
466 p_oggpacket->packet + i_header_len,
467 p_oggpacket->bytes - i_header_len );
469 p_data->p_payload_end = p_data->p_payload_start + p_pes->i_pes_size;
470 p_data->b_discard_payload = 0;
472 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
475 /****************************************************************************
476 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
477 * stream and fill p_ogg.
478 *****************************************************************************
479 * The initial page of a logical stream is marked as a 'bos' page.
480 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
481 * together and all of the initial pages must appear before any data pages.
483 * On success this function returns VLC_SUCCESS.
484 ****************************************************************************/
485 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
487 ogg_packet oggpacket;
491 while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
493 if( ogg_page_bos( &oggpage ) )
496 /* All is wonderful in our fine fine little world.
497 * We found the beginning of our first logical stream. */
498 while( ogg_page_bos( &oggpage ) )
502 realloc( p_ogg->pp_stream, p_ogg->i_streams *
503 sizeof(logical_stream_t *) );
505 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
507 p_stream = malloc( sizeof(logical_stream_t) );
508 memset( p_stream, 0, sizeof(logical_stream_t) );
510 /* Setup the logical stream */
511 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
512 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
514 /* Extract the initial header from the first page and verify
515 * the codec type of tis Ogg bitstream */
516 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
518 /* error. stream version mismatch perhaps */
519 msg_Err( p_input, "Error reading first page of "
520 "Ogg bitstream data" );
524 /* FIXME: check return value */
525 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
527 /* Check for Vorbis header */
528 if( oggpacket.bytes >= 7 &&
529 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
533 msg_Dbg( p_input, "found vorbis header" );
534 p_stream->i_cat = AUDIO_ES;
535 p_stream->i_fourcc = VLC_FOURCC( 'v','o','r','b' );
537 /* Signal that we want to keep a backup of the vorbis
538 * stream headers. They will be used when switching between
540 p_stream->b_force_backup = 1;
542 /* Cheat and get additionnal info ;) */
543 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
544 oggpack_adv( &opb, 96 );
545 p_stream->f_rate = oggpack_read( &opb, 32 );
546 oggpack_adv( &opb, 32 );
547 p_stream->i_bitrate = oggpack_read( &opb, 32 );
549 char title[sizeof("Stream") + 10];
550 input_info_category_t *p_cat;
551 sprintf( title, "Stream %d", p_ogg->i_streams );
552 p_cat = input_InfoCategory( p_input, title );
553 input_AddInfo( p_cat, _("Type"), _("Audio") );
554 input_AddInfo( p_cat, _("Codec"), _("Vorbis") );
555 input_AddInfo( p_cat, _("Sample Rate"), "%f",
557 input_AddInfo( p_cat, _("Bit Rate"), "%d",
558 p_stream->i_bitrate );
561 /* Check for Theora header */
562 else if( oggpacket.bytes >= 7 &&
563 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
568 int i_fps_denominator;
569 int i_keyframe_frequency_force;
572 msg_Dbg( p_input, "found theora header" );
574 p_stream->i_cat = VIDEO_ES;
575 p_stream->i_fourcc = VLC_FOURCC( 't','h','e','o' );
577 /* Cheat and get additionnal info ;) */
578 oggpackB_readinit(&opb, oggpacket.packet, oggpacket.bytes);
579 oggpackB_adv( &opb, 56 );
580 oggpackB_read( &opb, 8 ); /* major version num */
581 oggpackB_read( &opb, 8 ); /* minor version num */
582 oggpackB_read( &opb, 8 ); /* subminor version num */
583 oggpackB_read( &opb, 16 ) /*<< 4*/; /* width */
584 oggpackB_read( &opb, 16 ) /*<< 4*/; /* height */
585 i_fps_numerator = oggpackB_read( &opb, 32 );
586 i_fps_denominator = oggpackB_read( &opb, 32 );
587 oggpackB_read( &opb, 24 ); /* aspect_numerator */
588 oggpackB_read( &opb, 24 ); /* aspect_denominator */
589 i_keyframe_frequency_force = 1 << oggpackB_read( &opb, 5 );
590 p_stream->i_bitrate = oggpackB_read( &opb, 24 );
591 oggpackB_read(&opb,6); /* quality */
593 /* granule_shift = i_log( frequency_force -1 ) */
594 p_stream->i_theora_keyframe_granule_shift = 0;
595 i_keyframe_frequency_force--;
596 while( i_keyframe_frequency_force )
598 p_stream->i_theora_keyframe_granule_shift++;
599 i_keyframe_frequency_force >>= 1;
602 p_stream->f_rate = (float)i_fps_numerator /
605 "found theora header, bitrate: %i, rate: %f",
606 p_stream->i_bitrate, p_stream->f_rate );
608 char title[sizeof("Stream") + 10];
609 input_info_category_t *p_cat;
610 sprintf( title, "Stream %d", p_ogg->i_streams );
611 p_cat = input_InfoCategory( p_input, title );
612 input_AddInfo( p_cat, _("Type"), _("Video") );
613 input_AddInfo( p_cat, _("Codec"), _("Theora") );
614 input_AddInfo( p_cat, _("Frame Rate"), "%f",
616 input_AddInfo( p_cat, _("Bit Rate"), "%d",
617 p_stream->i_bitrate );
619 #else /* HAVE_OGGPACKB */
620 msg_Dbg( p_input, "the ogg demuxer has been compiled "
621 "without support for the oggpackB extension."
622 "The theora stream won't be decoded." );
626 #endif /* HAVE_OGGPACKB */
628 /* Check for Tarkin header */
629 else if( oggpacket.bytes >= 7 &&
630 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
634 msg_Dbg( p_input, "found tarkin header" );
635 p_stream->i_cat = VIDEO_ES;
636 p_stream->i_fourcc = VLC_FOURCC( 't','a','r','k' );
638 /* Cheat and get additionnal info ;) */
639 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
640 oggpack_adv( &opb, 88 );
641 oggpack_adv( &opb, 104 );
642 p_stream->i_bitrate = oggpack_read( &opb, 32 );
643 p_stream->f_rate = 2; /* FIXME */
645 "found tarkin header, bitrate: %i, rate: %f",
646 p_stream->i_bitrate, p_stream->f_rate );
648 char title[sizeof("Stream") + 10];
649 input_info_category_t *p_cat;
650 sprintf( title, "Stream %d", p_ogg->i_streams );
651 p_cat = input_InfoCategory( p_input, title );
652 input_AddInfo( p_cat, _("Type"), _("Video") );
653 input_AddInfo( p_cat, _("Codec"), _("tarkin") );
654 input_AddInfo( p_cat, _("Sample Rate"), "%f",
656 input_AddInfo( p_cat, _("Bit Rate"), "%d",
657 p_stream->i_bitrate );
661 else if( oggpacket.bytes >= 142 &&
662 !strncmp( &oggpacket.packet[1],
663 "Direct Show Samples embedded in Ogg", 35 ))
665 /* Old header type */
667 /* Check for video header (old format) */
668 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
669 oggpacket.bytes >= 184 )
671 p_stream->i_cat = VIDEO_ES;
673 p_stream->p_bih = (BITMAPINFOHEADER *)
674 malloc( sizeof(BITMAPINFOHEADER) );
675 if( !p_stream->p_bih )
677 /* Mem allocation error, just ignore the stream */
682 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
683 p_stream->p_bih->biCompression= p_stream->i_fourcc =
684 VLC_FOURCC( oggpacket.packet[68],
685 oggpacket.packet[69],
686 oggpacket.packet[70],
687 oggpacket.packet[71] );
688 msg_Dbg( p_input, "found video header of type: %.4s",
689 (char *)&p_stream->i_fourcc );
691 p_stream->f_rate = 10000000.0 /
692 GetQWLE((oggpacket.packet+164));
693 p_stream->p_bih->biBitCount =
694 GetWLE((oggpacket.packet+182));
695 if( !p_stream->p_bih->biBitCount )
696 p_stream->p_bih->biBitCount=24; // hack, FIXME
697 p_stream->p_bih->biWidth =
698 GetDWLE((oggpacket.packet+176));
699 p_stream->p_bih->biHeight =
700 GetDWLE((oggpacket.packet+180));
701 p_stream->p_bih->biPlanes= 1 ;
702 p_stream->p_bih->biSizeImage =
703 (p_stream->p_bih->biBitCount >> 3) *
704 p_stream->p_bih->biWidth *
705 p_stream->p_bih->biHeight;
708 "fps: %f, width:%i; height:%i, bitcount:%i",
709 p_stream->f_rate, p_stream->p_bih->biWidth,
710 p_stream->p_bih->biHeight,
711 p_stream->p_bih->biBitCount);
713 char title[sizeof("Stream") + 10];
714 input_info_category_t *p_cat;
715 sprintf( title, "Stream %d", p_ogg->i_streams );
716 p_cat = input_InfoCategory( p_input, title );
717 input_AddInfo( p_cat, _("Type"), _("Video") );
718 input_AddInfo( p_cat, _("Codec"), "%.4s",
719 (char *)&p_stream->i_fourcc );
720 input_AddInfo( p_cat, _("Frame Rate"), "%f",
722 input_AddInfo( p_cat, _("Bit Count"), "%d",
723 p_stream->p_bih->biBitCount );
724 input_AddInfo( p_cat, _("Width"), "%d",
725 p_stream->p_bih->biWidth );
726 input_AddInfo( p_cat, _("Height"), "%d",
727 p_stream->p_bih->biHeight );
729 p_stream->i_bitrate = 0;
731 /* Check for audio header (old format) */
732 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
734 unsigned int i_extra_size;
736 p_stream->i_cat = AUDIO_ES;
738 i_extra_size = GetWLE((oggpacket.packet+140));
740 p_stream->p_wf = (WAVEFORMATEX *)
741 malloc( sizeof(WAVEFORMATEX) + i_extra_size );
742 if( !p_stream->p_wf )
744 /* Mem allocation error, just ignore the stream */
750 p_stream->p_wf->wFormatTag =
751 GetWLE((oggpacket.packet+124));
752 p_stream->p_wf->nChannels =
753 GetWLE((oggpacket.packet+126));
754 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
755 GetDWLE((oggpacket.packet+128));
756 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
757 GetDWLE((oggpacket.packet+132));
758 p_stream->i_bitrate *= 8;
759 p_stream->p_wf->nBlockAlign =
760 GetWLE((oggpacket.packet+136));
761 p_stream->p_wf->wBitsPerSample =
762 GetWLE((oggpacket.packet+138));
763 p_stream->p_wf->cbSize = i_extra_size;
765 if( i_extra_size > 0 )
766 memcpy( p_stream->p_wf+sizeof(WAVEFORMATEX),
767 oggpacket.packet+142, i_extra_size );
769 switch( p_stream->p_wf->wFormatTag )
771 case WAVE_FORMAT_PCM:
773 VLC_FOURCC( 'a', 'r', 'a', 'w' );
775 case WAVE_FORMAT_MPEG:
776 case WAVE_FORMAT_MPEGLAYER3:
778 VLC_FOURCC( 'm', 'p', 'g', 'a' );
780 case WAVE_FORMAT_A52:
782 VLC_FOURCC( 'a', '5', '2', ' ' );
784 case WAVE_FORMAT_WMA1:
786 VLC_FOURCC( 'w', 'm', 'a', '1' );
788 case WAVE_FORMAT_WMA2:
790 VLC_FOURCC( 'w', 'm', 'a', '2' );
793 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
794 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
795 p_stream->p_wf->wFormatTag & 0xff );
798 msg_Dbg( p_input, "found audio header of type: %.4s",
799 (char *)&p_stream->i_fourcc );
800 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
801 "%dbits/sample %dkb/s",
802 p_stream->p_wf->wFormatTag,
803 p_stream->p_wf->nChannels,
804 p_stream->p_wf->nSamplesPerSec,
805 p_stream->p_wf->wBitsPerSample,
806 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
808 char title[sizeof("Stream") + 10];
809 input_info_category_t *p_cat;
810 sprintf( title, "Stream %d", p_ogg->i_streams );
811 p_cat = input_InfoCategory( p_input, title );
812 input_AddInfo( p_cat, _("Type"), _("Audio") );
813 input_AddInfo( p_cat, _("Codec"), _("%.4s"),
814 (char *)&p_stream->i_fourcc );
815 input_AddInfo( p_cat, _("Sample Rate"), "%d",
816 p_stream->p_wf->nSamplesPerSec );
817 input_AddInfo( p_cat, _("Bit Rate"), "%d",
818 p_stream->p_wf->nAvgBytesPerSec * 8
820 input_AddInfo( p_cat, _("Channels"), "%d",
821 p_stream->p_wf->nChannels );
822 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
823 p_stream->p_wf->wBitsPerSample );
829 msg_Dbg( p_input, "stream %d has an old header "
830 "but is of an unknown type", p_ogg->i_streams-1 );
835 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
836 == PACKET_TYPE_HEADER &&
837 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
839 stream_header *st = (stream_header *)(oggpacket.packet+1);
841 /* Check for video header (new format) */
842 if( !strncmp( st->streamtype, "video", 5 ) )
844 p_stream->i_cat = VIDEO_ES;
846 /* We need to get rid of the header packet */
847 ogg_stream_packetout( &p_stream->os, &oggpacket );
849 p_stream->p_bih = (BITMAPINFOHEADER *)
850 malloc( sizeof(BITMAPINFOHEADER) );
851 if( !p_stream->p_bih )
853 /* Mem allocation error, just ignore the stream */
858 p_stream->p_bih->biSize = sizeof(BITMAPINFOHEADER);
859 p_stream->p_bih->biCompression=
860 p_stream->i_fourcc = VLC_FOURCC( st->subtype[0],
864 msg_Dbg( p_input, "found video header of type: %.4s",
865 (char *)&p_stream->i_fourcc );
867 p_stream->f_rate = 10000000.0 /
868 GetQWLE((uint8_t *)&st->time_unit);
869 p_stream->p_bih->biBitCount =
870 GetWLE((uint8_t *)&st->bits_per_sample);
871 p_stream->p_bih->biWidth =
872 GetDWLE((uint8_t *)&st->sh.video.width);
873 p_stream->p_bih->biHeight =
874 GetDWLE((uint8_t *)&st->sh.video.height);
875 p_stream->p_bih->biPlanes= 1 ;
876 p_stream->p_bih->biSizeImage =
877 (p_stream->p_bih->biBitCount >> 3) *
878 p_stream->p_bih->biWidth *
879 p_stream->p_bih->biHeight;
882 "fps: %f, width:%i; height:%i, bitcount:%i",
883 p_stream->f_rate, p_stream->p_bih->biWidth,
884 p_stream->p_bih->biHeight,
885 p_stream->p_bih->biBitCount);
888 char title[sizeof("Stream") + 10];
889 input_info_category_t *p_cat;
890 sprintf( title, "Stream %d", p_ogg->i_streams );
891 p_cat = input_InfoCategory( p_input, title );
892 input_AddInfo( p_cat, _("Type"), _("Video") );
893 input_AddInfo( p_cat, _("Codec"), "%.4s",
894 (char *)&p_stream->i_fourcc );
895 input_AddInfo( p_cat, _("Frame Rate"), "%f",
897 input_AddInfo( p_cat, _("Bit Count"), "%d",
898 p_stream->p_bih->biBitCount );
899 input_AddInfo( p_cat, _("Width"), "%d",
900 p_stream->p_bih->biWidth );
901 input_AddInfo( p_cat, _("Height"), "%d",
902 p_stream->p_bih->biHeight );
904 p_stream->i_bitrate = 0;
906 /* Check for audio header (new format) */
907 else if( !strncmp( st->streamtype, "audio", 5 ) )
911 p_stream->i_cat = AUDIO_ES;
913 /* We need to get rid of the header packet */
914 ogg_stream_packetout( &p_stream->os, &oggpacket );
916 p_stream->p_wf = (WAVEFORMATEX *)
917 malloc( sizeof(WAVEFORMATEX) );
918 if( !p_stream->p_wf )
920 /* Mem allocation error, just ignore the stream */
926 memcpy( p_buffer, st->subtype, 4 );
928 p_stream->p_wf->wFormatTag = strtol(p_buffer,NULL,16);
929 p_stream->p_wf->nChannels =
930 GetWLE((uint8_t *)&st->sh.audio.channels);
931 p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
932 GetQWLE((uint8_t *)&st->samples_per_unit);
933 p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
934 GetDWLE((uint8_t *)&st->sh.audio.avgbytespersec);
935 p_stream->i_bitrate *= 8;
936 p_stream->p_wf->nBlockAlign =
937 GetWLE((uint8_t *)&st->sh.audio.blockalign);
938 p_stream->p_wf->wBitsPerSample =
939 GetWLE((uint8_t *)&st->bits_per_sample);
940 p_stream->p_wf->cbSize = 0;
942 switch( p_stream->p_wf->wFormatTag )
944 case WAVE_FORMAT_PCM:
946 VLC_FOURCC( 'a', 'r', 'a', 'w' );
948 case WAVE_FORMAT_MPEG:
949 case WAVE_FORMAT_MPEGLAYER3:
951 VLC_FOURCC( 'm', 'p', 'g', 'a' );
953 case WAVE_FORMAT_A52:
955 VLC_FOURCC( 'a', '5', '2', ' ' );
957 case WAVE_FORMAT_WMA1:
959 VLC_FOURCC( 'w', 'm', 'a', '1' );
961 case WAVE_FORMAT_WMA2:
963 VLC_FOURCC( 'w', 'm', 'a', '2' );
966 p_stream->i_fourcc = VLC_FOURCC( 'm', 's',
967 ( p_stream->p_wf->wFormatTag >> 8 ) & 0xff,
968 p_stream->p_wf->wFormatTag & 0xff );
971 msg_Dbg( p_input, "found audio header of type: %.4s",
972 (char *)&p_stream->i_fourcc );
973 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
974 "%dbits/sample %dkb/s",
975 p_stream->p_wf->wFormatTag,
976 p_stream->p_wf->nChannels,
977 p_stream->p_wf->nSamplesPerSec,
978 p_stream->p_wf->wBitsPerSample,
979 p_stream->p_wf->nAvgBytesPerSec * 8 / 1024 );
981 char title[sizeof("Stream") + 10];
982 input_info_category_t *p_cat;
983 sprintf( title, "Stream %d", p_ogg->i_streams );
984 p_cat = input_InfoCategory( p_input, title );
985 input_AddInfo( p_cat, _("Type"), _("Audio") );
986 input_AddInfo( p_cat, _("Codec"), "%.4s",
987 (char *)&p_stream->i_fourcc );
988 input_AddInfo( p_cat, _("Sample Rate"), "%d",
989 p_stream->p_wf->nSamplesPerSec );
990 input_AddInfo( p_cat, _("Bit Rate"), "%d",
991 p_stream->p_wf->nAvgBytesPerSec * 8
993 input_AddInfo( p_cat, _("Channels"), "%d",
994 p_stream->p_wf->nChannels );
995 input_AddInfo( p_cat, _("Bits per Sample"), "%d",
996 p_stream->p_wf->wBitsPerSample );
999 /* Check for text (subtitles) header */
1000 else if( !strncmp(st->streamtype, "text", 4) )
1002 /* We need to get rid of the header packet */
1003 ogg_stream_packetout( &p_stream->os, &oggpacket );
1005 msg_Dbg( p_input, "found text subtitles header" );
1006 p_stream->i_cat = SPU_ES;
1007 p_stream->i_fourcc =
1008 VLC_FOURCC( 's', 'u', 'b', 't' );
1009 p_stream->f_rate = 1000; /* granulepos is in milisec */
1013 msg_Dbg( p_input, "stream %d has a header marker "
1014 "but is of an unknown type", p_ogg->i_streams-1 );
1021 msg_Dbg( p_input, "stream %d is of unknown type",
1022 p_ogg->i_streams-1 );
1029 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1030 return VLC_EGENERIC;
1033 /* This is the first data page, which means we are now finished
1034 * with the initial pages. We just need to store it in the relevant
1036 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1038 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
1047 return VLC_EGENERIC;
1050 /*****************************************************************************
1051 * Activate: initializes ogg demux structures
1052 *****************************************************************************/
1053 static int Activate( vlc_object_t * p_this )
1055 int i_stream, b_forced;
1057 input_thread_t *p_input = (input_thread_t *)p_this;
1059 p_input->p_demux_data = NULL;
1060 b_forced = ( ( *p_input->psz_demux )&&
1061 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
1063 /* Check if we are dealing with an ogg stream */
1064 if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
1067 /* Allocate p_ogg */
1068 if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
1070 msg_Err( p_input, "out of memory" );
1073 memset( p_ogg, 0, sizeof( demux_sys_t ) );
1074 p_input->p_demux_data = p_ogg;
1077 p_ogg->b_seekable = ( ( p_input->stream.b_seekable )
1078 &&( p_input->stream.i_method == INPUT_METHOD_FILE ) );
1080 /* Initialize the Ogg physical bitstream parser */
1081 ogg_sync_init( &p_ogg->oy );
1083 /* Find the logical streams embedded in the physical stream and
1084 * initialize our p_ogg structure. */
1085 if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
1087 msg_Err( p_input, "couldn't find an ogg logical stream" );
1091 /* Set the demux function */
1092 p_input->pf_demux = Demux;
1094 /* Initialize access plug-in structures. */
1095 if( p_input->i_mtu == 0 )
1097 /* Improve speed. */
1098 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
1101 /* Create one program */
1102 vlc_mutex_lock( &p_input->stream.stream_lock );
1103 if( input_InitStream( p_input, 0 ) == -1)
1105 vlc_mutex_unlock( &p_input->stream.stream_lock );
1106 msg_Err( p_input, "cannot init stream" );
1109 if( input_AddProgram( p_input, 0, 0) == NULL )
1111 vlc_mutex_unlock( &p_input->stream.stream_lock );
1112 msg_Err( p_input, "cannot add program" );
1115 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1116 p_input->stream.i_mux_rate = 0;
1117 vlc_mutex_unlock( &p_input->stream.stream_lock );
1119 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1121 #define p_stream p_ogg->pp_stream[i_stream]
1122 vlc_mutex_lock( &p_input->stream.stream_lock );
1123 p_stream->p_es = input_AddES( p_input,
1124 p_input->stream.p_selected_program,
1125 p_ogg->i_streams + 1, 0 );
1126 p_input->stream.i_mux_rate += (p_stream->i_bitrate / ( 8 * 50 ));
1127 vlc_mutex_unlock( &p_input->stream.stream_lock );
1128 p_stream->p_es->i_stream_id = p_stream->p_es->i_id = i_stream;
1129 p_stream->p_es->i_fourcc = p_stream->i_fourcc;
1130 p_stream->p_es->i_cat = p_stream->i_cat;
1131 p_stream->p_es->p_waveformatex = (void*)p_stream->p_wf;
1132 p_stream->p_es->p_bitmapinfoheader = (void*)p_stream->p_bih;
1136 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1138 #define p_stream p_ogg->pp_stream[i_stream]
1139 switch( p_stream->p_es->i_cat )
1142 if( (p_ogg->p_stream_video == NULL) )
1144 p_ogg->p_stream_video = p_stream;
1145 /* TODO add test to see if a decoder has been found */
1146 Ogg_StreamStart( p_input, p_ogg, i_stream );
1151 if( (p_ogg->p_stream_audio == NULL) )
1153 int i_audio = config_GetInt( p_input, "audio-channel" );
1154 if( i_audio == i_stream || i_audio <= 0 ||
1155 i_audio >= p_ogg->i_streams ||
1156 p_ogg->pp_stream[i_audio]->p_es->i_cat != AUDIO_ES )
1158 p_ogg->p_stream_audio = p_stream;
1159 Ogg_StreamStart( p_input, p_ogg, i_stream );
1165 if( (p_ogg->p_stream_spu == NULL) )
1167 /* for spu, default is none */
1168 int i_spu = config_GetInt( p_input, "spu-channel" );
1169 if( i_spu < 0 || i_spu >= p_ogg->i_streams ||
1170 p_ogg->pp_stream[i_spu]->p_es->i_cat != SPU_ES )
1174 else if( i_spu == i_stream )
1176 p_ogg->p_stream_spu = p_stream;
1177 Ogg_StreamStart( p_input, p_ogg, i_stream );
1188 /* we select the first audio and video ES */
1189 vlc_mutex_lock( &p_input->stream.stream_lock );
1190 if( !p_ogg->p_stream_video )
1192 msg_Warn( p_input, "no video stream found" );
1194 if( !p_ogg->p_stream_audio )
1196 msg_Warn( p_input, "no audio stream found!" );
1198 p_input->stream.p_selected_program->b_is_ok = 1;
1199 vlc_mutex_unlock( &p_input->stream.stream_lock );
1201 /* Call the pace control */
1202 input_ClockManageRef( p_input,
1203 p_input->stream.p_selected_program,
1209 Deactivate( (vlc_object_t *)p_input );
1214 /*****************************************************************************
1215 * Deactivate: frees unused data
1216 *****************************************************************************/
1217 static void Deactivate( vlc_object_t *p_this )
1219 input_thread_t *p_input = (input_thread_t *)p_this;
1220 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
1225 /* Cleanup the bitstream parser */
1226 ogg_sync_clear( &p_ogg->oy );
1228 for( i = 0; i < p_ogg->i_streams; i++ )
1230 ogg_stream_clear( &p_ogg->pp_stream[i]->os );
1231 for( j = 0; j < p_ogg->pp_stream[i]->i_packets_backup; j++ )
1233 free( p_ogg->pp_stream[i]->p_packets_backup[j].packet );
1235 if( p_ogg->pp_stream[i]->p_packets_backup)
1236 free( p_ogg->pp_stream[i]->p_packets_backup );
1237 #if 0 /* hmmm, it's already freed in input_DelES() */
1238 if( p_ogg->pp_stream[i]->p_bih )
1239 free( p_ogg->pp_stream[i]->p_bih );
1240 if( p_ogg->pp_stream[i]->p_wf )
1241 free( p_ogg->pp_stream[i]->p_wf );
1243 free( p_ogg->pp_stream[i] );
1245 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1251 /*****************************************************************************
1252 * Demux: reads and demuxes data packets
1253 *****************************************************************************
1254 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1255 *****************************************************************************/
1256 static int Demux( input_thread_t * p_input )
1258 int i, i_stream, b_eos = 0;
1260 ogg_packet oggpacket;
1261 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1263 #define p_stream p_ogg->pp_stream[i_stream]
1264 /* detect new selected/unselected streams */
1265 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1267 if( p_stream->p_es )
1269 if( p_stream->p_es->p_decoder_fifo &&
1270 !p_stream->i_activated )
1272 Ogg_StreamStart( p_input, p_ogg, i_stream );
1275 if( !p_stream->p_es->p_decoder_fifo &&
1276 p_stream->i_activated )
1278 Ogg_StreamStop( p_input, p_ogg, i_stream );
1283 /* search for new video and audio stream to select
1284 * if current have been unselected */
1285 if( ( !p_ogg->p_stream_video )
1286 || ( !p_ogg->p_stream_video->p_es->p_decoder_fifo ) )
1288 p_ogg->p_stream_video = NULL;
1289 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1291 if( ( p_stream->i_cat == VIDEO_ES )
1292 &&( p_stream->p_es->p_decoder_fifo ) )
1294 p_ogg->p_stream_video = p_stream;
1299 if( ( !p_ogg->p_stream_audio )
1300 ||( !p_ogg->p_stream_audio->p_es->p_decoder_fifo ) )
1302 p_ogg->p_stream_audio = NULL;
1303 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1305 if( ( p_stream->i_cat == AUDIO_ES )
1306 &&( p_stream->p_es->p_decoder_fifo ) )
1308 p_ogg->p_stream_audio = p_stream;
1314 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1316 msg_Warn( p_input, "synchro reinit" );
1318 /* An ogg packet does only contain the starting date of the next
1319 * packet, not its own starting date.
1320 * As a quick work around, we just skip an oggpage */
1322 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1324 /* we'll trash all the data until we find the next pcr */
1325 p_stream->b_reinit = 1;
1326 p_stream->i_pcr = -1;
1327 p_stream->i_interpolated_pcr = -1;
1329 p_ogg->b_reinit = 1;
1334 * Demux ogg pages from the stream
1336 for( i = 0; i < PAGES_READ_ONCE || p_ogg->b_reinit; i++ )
1338 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1344 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1346 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1349 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1351 if( !p_stream->p_es )
1356 if( p_stream->b_reinit )
1358 if( oggpacket.granulepos >= 0 )
1360 p_stream->b_reinit = 0;
1362 /* Convert the next granulepos into a pcr */
1363 Ogg_UpdatePCR( p_stream, &oggpacket );
1365 /* Call the pace control to reinitialize
1366 * the system clock */
1367 input_ClockManageRef( p_input,
1368 p_input->stream.p_selected_program,
1371 if( (!p_ogg->p_stream_video ||
1372 !p_ogg->p_stream_video->b_reinit) &&
1373 (!p_ogg->p_stream_audio ||
1374 !p_ogg->p_stream_audio->b_reinit) )
1376 p_ogg->b_reinit = 0;
1382 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1389 p_ogg->i_old_pcr = p_ogg->i_pcr;
1390 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1391 for( ; i_stream < p_ogg->i_streams; i_stream++ )
1393 if( p_stream->i_cat == SPU_ES )
1396 if( p_stream->i_interpolated_pcr > 0
1397 && p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1398 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1402 /* Sanity check for streams where the granulepos of the header packets
1403 * don't match these of the data packets (eg. ogg web radios). */
1404 if( p_ogg->i_old_pcr == 0 && p_ogg->i_pcr > 1000000 )
1405 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
1407 /* Call the pace control */
1408 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1411 /* Did we reach the end of stream ? */
1412 return( b_eos ? 0 : 1 );