1 /*****************************************************************************
2 * ogg.c : ogg stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: ogg.c,v 1.45 2003/11/22 12:41:32 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 *****************************************************************************/
28 #include <vlc/input.h>
35 #define OGG_BLOCK_SIZE 4096
37 /*****************************************************************************
38 * Definitions of structures and functions used by this plugins
39 *****************************************************************************/
40 typedef struct logical_stream_s
42 ogg_stream_state os; /* logical stream of packets */
51 /* the header of some logical streams (eg vorbis) contain essential
52 * data for the decoder. We back them up here in case we need to re-feed
53 * them to the decoder. */
56 ogg_packet *p_packets_backup;
58 /* program clock reference (in units of 90kHz) derived from the previous
61 mtime_t i_interpolated_pcr;
62 mtime_t i_previous_pcr;
66 int i_theora_keyframe_granule_shift;
72 ogg_sync_state oy; /* sync and verify incoming physical bitstream */
74 int i_streams; /* number of logical bitstreams */
75 logical_stream_t **pp_stream; /* pointer to an array of logical streams */
77 /* program clock reference (in units of 90kHz) derived from the pcr of
81 int i_prev_sync_state;
87 /* OggDS headers for the new header format (used in ogm files) */
88 typedef struct stream_header_video
92 } stream_header_video;
94 typedef struct stream_header_audio
97 ogg_int16_t blockalign;
98 ogg_int32_t avgbytespersec;
99 } stream_header_audio;
101 typedef struct stream_header
106 ogg_int32_t size; /* size of the structure */
108 ogg_int64_t time_unit; /* in reference time */
109 ogg_int64_t samples_per_unit;
110 ogg_int32_t default_len; /* in media time */
112 ogg_int32_t buffersize;
113 ogg_int16_t bits_per_sample;
118 stream_header_video video;
120 stream_header_audio audio;
124 /* Some defines from OggDS */
125 #define PACKET_TYPE_HEADER 0x01
126 #define PACKET_TYPE_BITS 0x07
127 #define PACKET_LEN_BITS01 0xc0
128 #define PACKET_LEN_BITS2 0x02
129 #define PACKET_IS_SYNCPOINT 0x08
131 /*****************************************************************************
133 *****************************************************************************/
134 static int Activate ( vlc_object_t * );
135 static void Deactivate( vlc_object_t * );
136 static int Demux ( input_thread_t * );
137 static int Control ( input_thread_t *, int, va_list );
139 /* Bitstream manipulation */
140 static int Ogg_Check ( input_thread_t *p_input );
141 static int Ogg_ReadPage ( input_thread_t *, demux_sys_t *, ogg_page * );
142 static void Ogg_UpdatePCR ( logical_stream_t *, ogg_packet * );
143 static void Ogg_DecodePacket ( input_thread_t *p_input,
144 logical_stream_t *p_stream, ogg_packet * );
146 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg);
147 static int Ogg_FindLogicalStreams( input_thread_t *p_input,demux_sys_t *p_ogg);
148 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg );
150 /*****************************************************************************
152 *****************************************************************************/
154 set_description( _("ogg stream demuxer" ) );
155 set_capability( "demux", 50 );
156 set_callbacks( Activate, Deactivate );
157 add_shortcut( "ogg" );
160 /****************************************************************************
161 * Ogg_Check: Check we are dealing with an ogg stream.
162 ****************************************************************************/
163 static int Ogg_Check( input_thread_t *p_input )
166 int i_size = input_Peek( p_input, &p_peek, 4 );
168 /* Check for the Ogg capture pattern */
169 if( !(i_size>3) || !(p_peek[0] == 'O') || !(p_peek[1] == 'g') ||
170 !(p_peek[2] == 'g') || !(p_peek[3] == 'S') )
173 /* FIXME: Capture pattern might not be enough so we can also check for the
174 * the first complete page */
179 /****************************************************************************
180 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
181 ****************************************************************************
182 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
183 * are at the end of stream.
184 ****************************************************************************/
185 static int Ogg_ReadPage( input_thread_t *p_input, demux_sys_t *p_ogg,
186 ogg_page *p_oggpage )
189 data_packet_t *p_data;
192 while( ogg_sync_pageout( &p_ogg->oy, p_oggpage ) != 1 )
194 i_read = input_SplitBuffer( p_input, &p_data, OGG_BLOCK_SIZE );
198 p_buffer = ogg_sync_buffer( &p_ogg->oy, i_read );
199 p_input->p_vlc->pf_memcpy( p_buffer, p_data->p_payload_start, i_read );
200 ogg_sync_wrote( &p_ogg->oy, i_read );
201 input_DeletePacket( p_input->p_method_data, p_data );
207 /****************************************************************************
208 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
210 ****************************************************************************/
211 static void Ogg_UpdatePCR( logical_stream_t *p_stream,
212 ogg_packet *p_oggpacket )
214 /* Convert the granulepos into a pcr */
215 if( p_oggpacket->granulepos >= 0 )
217 if( p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
219 p_stream->i_pcr = p_oggpacket->granulepos * 90000
224 ogg_int64_t iframe = p_oggpacket->granulepos >>
225 p_stream->i_theora_keyframe_granule_shift;
226 ogg_int64_t pframe = p_oggpacket->granulepos -
227 ( iframe << p_stream->i_theora_keyframe_granule_shift );
229 p_stream->i_pcr = ( iframe + pframe ) * 90000
233 p_stream->i_interpolated_pcr = p_stream->i_pcr;
237 p_stream->i_pcr = -1;
239 /* no granulepos available, try to interpolate the pcr.
240 * If we can't then don't touch the old value. */
241 if( p_stream->fmt.i_cat == VIDEO_ES )
242 /* 1 frame per packet */
243 p_stream->i_interpolated_pcr += (90000 / p_stream->f_rate);
244 else if( p_stream->fmt.i_bitrate )
245 p_stream->i_interpolated_pcr += ( p_oggpacket->bytes * 90000
246 / p_stream->fmt.i_bitrate / 8 );
250 /****************************************************************************
251 * Ogg_DecodePacket: Decode an Ogg packet.
252 ****************************************************************************/
253 static void Ogg_DecodePacket( input_thread_t *p_input,
254 logical_stream_t *p_stream,
255 ogg_packet *p_oggpacket )
258 vlc_bool_t b_selected;
259 int i_header_len = 0;
262 if( p_stream->b_force_backup )
264 ogg_packet *p_packet_backup;
265 p_stream->i_packets_backup++;
266 switch( p_stream->fmt.i_codec )
268 case VLC_FOURCC( 'v','o','r','b' ):
269 case VLC_FOURCC( 's','p','x',' ' ):
270 case VLC_FOURCC( 't','h','e','o' ):
271 if( p_stream->i_packets_backup == 3 ) p_stream->b_force_backup = 0;
274 case VLC_FOURCC( 'f','l','a','c' ):
275 if( p_stream->i_packets_backup == 1 ) return;
276 else if( p_stream->i_packets_backup == 2 )
278 /* Parse the STREAMINFO metadata */
280 bs_init( &s, p_oggpacket->packet, p_oggpacket->bytes );
282 if( bs_read( &s, 7 ) == 0 )
284 if( bs_read( &s, 24 ) >= 34 /*size STREAMINFO*/ )
287 p_stream->f_rate = p_stream->fmt.audio.i_rate =
289 p_stream->fmt.audio.i_channels =
290 bs_read( &s, 3 ) + 1;
292 msg_Dbg( p_input, "Flac header, channels: %i, rate: %i",
293 p_stream->fmt.audio.i_channels,
294 (int)p_stream->f_rate );
298 msg_Dbg( p_input, "FLAC STREAMINFO metadata too short" );
301 /* Store STREAMINFO for the decoder and packetizer */
302 p_stream->fmt.i_extra = p_oggpacket->bytes + 4;
303 p_stream->fmt.p_extra = malloc( p_stream->fmt.i_extra );
304 memcpy( p_stream->fmt.p_extra, "fLaC", 4);
305 memcpy( ((uint8_t *)p_stream->fmt.p_extra) + 4,
306 p_oggpacket->packet, p_oggpacket->bytes );
308 /* Fake this as the last metadata block */
309 ((uint8_t*)p_stream->fmt.p_extra)[4] |= 0x80;
311 p_stream->p_es = es_out_Add( p_input->p_es_out,
316 /* This ain't a STREAMINFO metadata */
317 msg_Dbg( p_input, "Invalid FLAC STREAMINFO metadata" );
319 p_stream->b_force_backup = 0;
320 p_stream->i_packets_backup = 0;
326 p_stream->b_force_backup = 0;
330 /* Backup the ogg packet (likely an header packet) */
331 p_stream->p_packets_backup =
332 realloc( p_stream->p_packets_backup, p_stream->i_packets_backup *
333 sizeof(ogg_packet) );
336 &p_stream->p_packets_backup[p_stream->i_packets_backup - 1];
338 p_packet_backup->bytes = p_oggpacket->bytes;
339 p_packet_backup->granulepos = p_oggpacket->granulepos;
341 if( p_oggpacket->granulepos >= 0 )
343 /* Because of vorbis granulepos scheme we must set the pcr for the
344 * 1st header packet so it doesn't get discarded in the
346 Ogg_UpdatePCR( p_stream, p_oggpacket );
349 p_packet_backup->packet = malloc( p_oggpacket->bytes );
350 if( !p_packet_backup->packet ) return;
351 memcpy( p_packet_backup->packet, p_oggpacket->packet,
352 p_oggpacket->bytes );
355 /* Check the ES is selected */
356 es_out_Control( p_input->p_es_out, ES_OUT_GET_SELECT,
357 p_stream->p_es, &b_selected );
359 if( b_selected && !p_stream->b_activated )
361 p_stream->b_activated = VLC_TRUE;
363 /* Newly activated stream, feed the backup headers to the decoder */
364 if( !p_stream->b_force_backup )
367 for( i = 0; i < p_stream->i_packets_backup; i++ )
369 /* Set correct starting date in header packets */
370 p_stream->p_packets_backup[i].granulepos =
371 p_stream->i_interpolated_pcr * p_stream->f_rate / 90000;
373 Ogg_DecodePacket( p_input, p_stream,
374 &p_stream->p_packets_backup[i] );
379 /* Convert the pcr into a pts */
380 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
381 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
382 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
384 if( p_stream->i_pcr >= 0 )
386 /* This is for streams where the granulepos of the header packets
387 * doesn't match these of the data packets (eg. ogg web radios). */
388 if( p_stream->i_previous_pcr == 0 &&
389 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
390 p_input->stream.p_selected_program->i_synchro_state =
393 p_stream->i_previous_pcr = p_stream->i_pcr;
395 /* Call the pace control */
396 if( p_input->stream.p_selected_program->i_synchro_state ==
398 input_ClockManageRef( p_input,
399 p_input->stream.p_selected_program,
403 /* The granulepos is the end date of the sample */
404 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
405 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
408 /* Convert the granulepos into the next pcr */
409 Ogg_UpdatePCR( p_stream, p_oggpacket );
413 /* Convert the granulepos into the current pcr */
414 Ogg_UpdatePCR( p_stream, p_oggpacket );
416 if( p_stream->i_pcr >= 0 )
418 /* This is for streams where the granulepos of the header packets
419 * doesn't match these of the data packets (eg. ogg web radios). */
420 if( p_stream->i_previous_pcr == 0 &&
421 p_stream->i_pcr > 3 * DEFAULT_PTS_DELAY * 9/100 )
422 p_input->stream.p_selected_program->i_synchro_state =
425 p_stream->i_previous_pcr = p_stream->i_pcr;
427 /* Call the pace control */
428 if( p_input->stream.p_selected_program->i_synchro_state ==
430 input_ClockManageRef( p_input,
431 p_input->stream.p_selected_program,
435 /* The granulepos is the start date of the sample */
436 i_pts = ( p_stream->i_pcr < 0 ) ? 0 :
437 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
443 /* This stream isn't currently selected so we don't need to decode it,
444 * but we did need to store its pcr as it might be selected later on */
445 p_stream->b_activated = VLC_FALSE;
449 if( !( p_block = block_New( p_input, p_oggpacket->bytes ) ) )
453 p_block->i_dts = p_block->i_pts = i_pts;
455 if( p_stream->fmt.i_cat == SPU_ES ) p_block->i_dts = 0;
457 if( p_stream->fmt.i_codec != VLC_FOURCC( 'v','o','r','b' ) &&
458 p_stream->fmt.i_codec != VLC_FOURCC( 's','p','x',' ' ) &&
459 p_stream->fmt.i_codec != VLC_FOURCC( 'f','l','a','c' ) &&
460 p_stream->fmt.i_codec != VLC_FOURCC( 't','a','r','k' ) &&
461 p_stream->fmt.i_codec != VLC_FOURCC( 't','h','e','o' ) )
463 /* Remove the header from the packet */
464 i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
465 i_header_len |= (*p_oggpacket->packet & PACKET_LEN_BITS2) << 1;
468 p_block->i_buffer -= i_header_len;
471 if( p_stream->fmt.i_codec == VLC_FOURCC( 't','a','r','k' ) )
473 /* FIXME: the biggest hack I've ever done */
474 msg_Warn( p_input, "tark pts: "I64Fd", granule: "I64Fd,
475 p_block->i_pts, p_block->i_dts );
479 memcpy( p_block->p_buffer, p_oggpacket->packet + i_header_len,
480 p_oggpacket->bytes - i_header_len );
482 es_out_Send( p_input->p_es_out, p_stream->p_es, p_block );
485 /****************************************************************************
486 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
487 * stream and fill p_ogg.
488 *****************************************************************************
489 * The initial page of a logical stream is marked as a 'bos' page.
490 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
491 * together and all of the initial pages must appear before any data pages.
493 * On success this function returns VLC_SUCCESS.
494 ****************************************************************************/
495 static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
497 ogg_packet oggpacket;
501 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
503 while( Ogg_ReadPage( p_input, p_ogg, &oggpage ) == VLC_SUCCESS )
505 if( ogg_page_bos( &oggpage ) )
508 /* All is wonderful in our fine fine little world.
509 * We found the beginning of our first logical stream. */
510 while( ogg_page_bos( &oggpage ) )
514 realloc( p_ogg->pp_stream, p_ogg->i_streams *
515 sizeof(logical_stream_t *) );
517 p_stream = malloc( sizeof(logical_stream_t) );
518 memset( p_stream, 0, sizeof(logical_stream_t) );
520 es_format_Init( &p_stream->fmt, 0, 0 );
521 p_stream->b_activated = VLC_TRUE;
523 /* Setup the logical stream */
524 p_stream->i_serial_no = ogg_page_serialno( &oggpage );
525 ogg_stream_init( &p_stream->os, p_stream->i_serial_no );
527 /* Extract the initial header from the first page and verify
528 * the codec type of tis Ogg bitstream */
529 if( ogg_stream_pagein( &p_stream->os, &oggpage ) < 0 )
531 /* error. stream version mismatch perhaps */
532 msg_Err( p_input, "Error reading first page of "
533 "Ogg bitstream data" );
537 /* FIXME: check return value */
538 ogg_stream_packetpeek( &p_stream->os, &oggpacket );
540 /* Check for Vorbis header */
541 if( oggpacket.bytes >= 7 &&
542 ! strncmp( &oggpacket.packet[1], "vorbis", 6 ) )
546 msg_Dbg( p_input, "found vorbis header" );
547 p_stream->fmt.i_cat = AUDIO_ES;
548 p_stream->fmt.i_codec = VLC_FOURCC( 'v','o','r','b' );
550 /* Signal that we want to keep a backup of the vorbis
551 * stream headers. They will be used when switching between
553 p_stream->b_force_backup = 1;
555 /* Cheat and get additionnal info ;) */
556 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
557 oggpack_adv( &opb, 88 );
558 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 8 );
559 p_stream->f_rate = p_stream->fmt.audio.i_rate =
560 oggpack_read( &opb, 32 );
561 oggpack_adv( &opb, 32 );
562 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
564 /* Check for Speex header */
565 else if( oggpacket.bytes >= 7 &&
566 ! strncmp( &oggpacket.packet[0], "Speex", 5 ) )
570 p_stream->fmt.i_cat = AUDIO_ES;
571 p_stream->fmt.i_codec = VLC_FOURCC( 's','p','x',' ' );
573 /* Signal that we want to keep a backup of the vorbis
574 * stream headers. They will be used when switching between
576 p_stream->b_force_backup = 1;
578 /* Cheat and get additionnal info ;) */
579 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
580 oggpack_adv( &opb, 224 );
581 oggpack_adv( &opb, 32 ); /* speex_version_id */
582 oggpack_adv( &opb, 32 ); /* header_size */
583 p_stream->f_rate = p_stream->fmt.audio.i_rate =
584 oggpack_read( &opb, 32 );
585 oggpack_adv( &opb, 32 ); /* mode */
586 oggpack_adv( &opb, 32 ); /* mode_bitstream_version */
587 p_stream->fmt.audio.i_channels = oggpack_read( &opb, 32 );
588 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
590 msg_Dbg( p_input, "found speex header, channels: %i, "
591 "rate: %i, bitrate: %i",
592 p_stream->fmt.audio.i_channels,
593 (int)p_stream->f_rate, p_stream->fmt.i_bitrate );
595 /* Check for Flac header */
596 else if( oggpacket.bytes >= 4 &&
597 ! strncmp( &oggpacket.packet[0], "fLaC", 4 ) )
599 msg_Dbg( p_input, "found Flac header" );
601 /* Grrrr!!!! Did they really have to put all the
602 * important info in the second header packet!!!
603 * (STREAMINFO metadata is in the following packet) */
604 p_stream->b_force_backup = 1;
606 p_stream->fmt.i_cat = AUDIO_ES;
607 p_stream->fmt.i_codec = VLC_FOURCC( 'f','l','a','c' );
609 /* Check for Theora header */
610 else if( oggpacket.bytes >= 7 &&
611 ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
615 int i_fps_denominator;
616 int i_keyframe_frequency_force;
618 msg_Dbg( p_input, "found theora header" );
619 p_stream->fmt.i_cat = VIDEO_ES;
620 p_stream->fmt.i_codec = VLC_FOURCC( 't','h','e','o' );
622 /* Signal that we want to keep a backup of the vorbis
623 * stream headers. They will be used when switching between
625 p_stream->b_force_backup = 1;
627 /* Cheat and get additionnal info ;) */
628 bs_init( &bitstream, oggpacket.packet, oggpacket.bytes );
629 bs_skip( &bitstream, 56 );
630 bs_read( &bitstream, 8 ); /* major version num */
631 bs_read( &bitstream, 8 ); /* minor version num */
632 bs_read( &bitstream, 8 ); /* subminor version num */
633 bs_read( &bitstream, 16 ) /*<< 4*/; /* width */
634 bs_read( &bitstream, 16 ) /*<< 4*/; /* height */
635 bs_read( &bitstream, 24 ); /* frame width */
636 bs_read( &bitstream, 24 ); /* frame height */
637 bs_read( &bitstream, 8 ); /* x offset */
638 bs_read( &bitstream, 8 ); /* y offset */
640 i_fps_numerator = bs_read( &bitstream, 32 );
641 i_fps_denominator = bs_read( &bitstream, 32 );
642 bs_read( &bitstream, 24 ); /* aspect_numerator */
643 bs_read( &bitstream, 24 ); /* aspect_denominator */
644 i_keyframe_frequency_force = 1 << bs_read( &bitstream, 5 );
645 bs_read( &bitstream, 8 ); /* colorspace */
646 p_stream->fmt.i_bitrate = bs_read( &bitstream, 24 );
647 bs_read( &bitstream, 6 ); /* quality */
649 /* granule_shift = i_log( frequency_force -1 ) */
650 p_stream->i_theora_keyframe_granule_shift = 0;
651 i_keyframe_frequency_force--;
652 while( i_keyframe_frequency_force )
654 p_stream->i_theora_keyframe_granule_shift++;
655 i_keyframe_frequency_force >>= 1;
658 p_stream->f_rate = ((float)i_fps_numerator) /
661 "found theora header, bitrate: %i, rate: %f",
662 p_stream->fmt.i_bitrate, p_stream->f_rate );
664 /* Check for Tarkin header */
665 else if( oggpacket.bytes >= 7 &&
666 ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
670 msg_Dbg( p_input, "found tarkin header" );
671 p_stream->fmt.i_cat = VIDEO_ES;
672 p_stream->fmt.i_codec = VLC_FOURCC( 't','a','r','k' );
674 /* Cheat and get additionnal info ;) */
675 oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
676 oggpack_adv( &opb, 88 );
677 oggpack_adv( &opb, 104 );
678 p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
679 p_stream->f_rate = 2; /* FIXME */
681 "found tarkin header, bitrate: %i, rate: %f",
682 p_stream->fmt.i_bitrate, p_stream->f_rate );
684 else if( oggpacket.bytes >= 142 &&
685 !strncmp( &oggpacket.packet[1],
686 "Direct Show Samples embedded in Ogg", 35 ))
688 /* Old header type */
690 /* Check for video header (old format) */
691 if( GetDWLE((oggpacket.packet+96)) == 0x05589f80 &&
692 oggpacket.bytes >= 184 )
694 p_stream->fmt.i_cat = VIDEO_ES;
695 p_stream->fmt.i_codec =
696 VLC_FOURCC( oggpacket.packet[68],
697 oggpacket.packet[69],
698 oggpacket.packet[70],
699 oggpacket.packet[71] );
700 msg_Dbg( p_input, "found video header of type: %.4s",
701 (char *)&p_stream->fmt.i_codec );
703 p_stream->f_rate = 10000000.0 /
704 GetQWLE((oggpacket.packet+164));
705 p_stream->fmt.video.i_bits_per_pixel =
706 GetWLE((oggpacket.packet+182));
707 if( !p_stream->fmt.video.i_bits_per_pixel )
709 p_stream->fmt.video.i_bits_per_pixel = 24;
710 p_stream->fmt.video.i_width =
711 GetDWLE((oggpacket.packet+176));
712 p_stream->fmt.video.i_height =
713 GetDWLE((oggpacket.packet+180));
716 "fps: %f, width:%i; height:%i, bitcount:%i",
718 p_stream->fmt.video.i_width,
719 p_stream->fmt.video.i_height,
720 p_stream->fmt.video.i_bits_per_pixel);
723 /* Check for audio header (old format) */
724 else if( GetDWLE((oggpacket.packet+96)) == 0x05589F81 )
726 unsigned int i_extra_size;
727 unsigned int i_format_tag;
729 p_stream->fmt.i_cat = AUDIO_ES;
731 i_extra_size = GetWLE((oggpacket.packet+140));
734 p_stream->fmt.i_extra = i_extra_size;
735 p_stream->fmt.p_extra = malloc( i_extra_size );
736 memcpy( p_stream->fmt.p_extra,
737 oggpacket.packet + 142, i_extra_size );
740 i_format_tag = GetWLE((oggpacket.packet+124));
741 p_stream->fmt.audio.i_channels =
742 GetWLE((oggpacket.packet+126));
743 p_stream->f_rate = p_stream->fmt.audio.i_rate =
744 GetDWLE((oggpacket.packet+128));
745 p_stream->fmt.i_bitrate =
746 GetDWLE((oggpacket.packet+132)) * 8;
747 p_stream->fmt.audio.i_blockalign =
748 GetWLE((oggpacket.packet+136));
749 p_stream->fmt.audio.i_bitspersample =
750 GetWLE((oggpacket.packet+138));
752 switch( i_format_tag )
754 case WAVE_FORMAT_PCM:
755 p_stream->fmt.i_codec =
756 VLC_FOURCC( 'a', 'r', 'a', 'w' );
758 case WAVE_FORMAT_MPEG:
759 case WAVE_FORMAT_MPEGLAYER3:
760 p_stream->fmt.i_codec =
761 VLC_FOURCC( 'm', 'p', 'g', 'a' );
763 case WAVE_FORMAT_A52:
764 p_stream->fmt.i_codec =
765 VLC_FOURCC( 'a', '5', '2', ' ' );
767 case WAVE_FORMAT_WMA1:
768 p_stream->fmt.i_codec =
769 VLC_FOURCC( 'w', 'm', 'a', '1' );
771 case WAVE_FORMAT_WMA2:
772 p_stream->fmt.i_codec =
773 VLC_FOURCC( 'w', 'm', 'a', '2' );
776 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
777 ( i_format_tag >> 8 ) & 0xff,
778 i_format_tag & 0xff );
781 msg_Dbg( p_input, "found audio header of type: %.4s",
782 (char *)&p_stream->fmt.i_codec );
783 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
784 "%dbits/sample %dkb/s",
786 p_stream->fmt.audio.i_channels,
787 p_stream->fmt.audio.i_rate,
788 p_stream->fmt.audio.i_bitspersample,
789 p_stream->fmt.i_bitrate / 1024 );
794 msg_Dbg( p_input, "stream %d has an old header "
795 "but is of an unknown type", p_ogg->i_streams-1 );
800 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
801 == PACKET_TYPE_HEADER &&
802 oggpacket.bytes >= (int)sizeof(stream_header)+1 )
804 stream_header *st = (stream_header *)(oggpacket.packet+1);
806 /* Check for video header (new format) */
807 if( !strncmp( st->streamtype, "video", 5 ) )
809 p_stream->fmt.i_cat = VIDEO_ES;
811 /* We need to get rid of the header packet */
812 ogg_stream_packetout( &p_stream->os, &oggpacket );
814 p_stream->fmt.i_codec =
815 VLC_FOURCC( st->subtype[0], st->subtype[1],
816 st->subtype[2], st->subtype[3] );
817 msg_Dbg( p_input, "found video header of type: %.4s",
818 (char *)&p_stream->fmt.i_codec );
820 p_stream->f_rate = 10000000.0 /
821 GetQWLE(&st->time_unit);
822 p_stream->fmt.video.i_bits_per_pixel =
823 GetWLE(&st->bits_per_sample);
824 p_stream->fmt.video.i_width =
825 GetDWLE(&st->sh.video.width);
826 p_stream->fmt.video.i_height =
827 GetDWLE(&st->sh.video.height);
830 "fps: %f, width:%i; height:%i, bitcount:%i",
832 p_stream->fmt.video.i_width,
833 p_stream->fmt.video.i_height,
834 p_stream->fmt.video.i_bits_per_pixel );
836 /* Check for audio header (new format) */
837 else if( !strncmp( st->streamtype, "audio", 5 ) )
842 p_stream->fmt.i_cat = AUDIO_ES;
844 /* We need to get rid of the header packet */
845 ogg_stream_packetout( &p_stream->os, &oggpacket );
847 memcpy( p_buffer, st->subtype, 4 );
849 i_format_tag = strtol(p_buffer,NULL,16);
850 p_stream->fmt.audio.i_channels =
851 GetWLE(&st->sh.audio.channels);
852 p_stream->f_rate = p_stream->fmt.audio.i_rate =
853 GetQWLE(&st->samples_per_unit);
854 p_stream->fmt.i_bitrate =
855 GetDWLE(&st->sh.audio.avgbytespersec) * 8;
856 p_stream->fmt.audio.i_blockalign =
857 GetWLE(&st->sh.audio.blockalign);
858 p_stream->fmt.audio.i_bitspersample =
859 GetWLE(&st->bits_per_sample);
861 switch( i_format_tag )
863 case WAVE_FORMAT_PCM:
864 p_stream->fmt.i_codec =
865 VLC_FOURCC( 'a', 'r', 'a', 'w' );
867 case WAVE_FORMAT_MPEG:
868 case WAVE_FORMAT_MPEGLAYER3:
869 p_stream->fmt.i_codec =
870 VLC_FOURCC( 'm', 'p', 'g', 'a' );
872 case WAVE_FORMAT_A52:
873 p_stream->fmt.i_codec =
874 VLC_FOURCC( 'a', '5', '2', ' ' );
876 case WAVE_FORMAT_WMA1:
877 p_stream->fmt.i_codec =
878 VLC_FOURCC( 'w', 'm', 'a', '1' );
880 case WAVE_FORMAT_WMA2:
881 p_stream->fmt.i_codec =
882 VLC_FOURCC( 'w', 'm', 'a', '2' );
885 p_stream->fmt.i_codec = VLC_FOURCC( 'm', 's',
886 ( i_format_tag >> 8 ) & 0xff,
887 i_format_tag & 0xff );
890 msg_Dbg( p_input, "found audio header of type: %.4s",
891 (char *)&p_stream->fmt.i_codec );
892 msg_Dbg( p_input, "audio:0x%4.4x channels:%d %dHz "
893 "%dbits/sample %dkb/s",
895 p_stream->fmt.audio.i_channels,
896 p_stream->fmt.audio.i_rate,
897 p_stream->fmt.audio.i_bitspersample,
898 p_stream->fmt.i_bitrate / 1024 );
900 /* Check for text (subtitles) header */
901 else if( !strncmp(st->streamtype, "text", 4) )
903 /* We need to get rid of the header packet */
904 ogg_stream_packetout( &p_stream->os, &oggpacket );
906 msg_Dbg( p_input, "found text subtitles header" );
907 p_stream->fmt.i_cat = SPU_ES;
908 p_stream->fmt.i_codec = VLC_FOURCC('s','u','b','t');
909 p_stream->f_rate = 1000; /* granulepos is in milisec */
913 msg_Dbg( p_input, "stream %d has a header marker "
914 "but is of an unknown type", p_ogg->i_streams-1 );
921 msg_Dbg( p_input, "stream %d is of unknown type",
922 p_ogg->i_streams-1 );
927 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
931 /* This is the first data page, which means we are now finished
932 * with the initial pages. We just need to store it in the relevant
934 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
936 if( ogg_stream_pagein( &p_ogg->pp_stream[i_stream]->os,
951 /*****************************************************************************
952 * Activate: initializes ogg demux structures
953 *****************************************************************************/
954 static int Activate( vlc_object_t * p_this )
956 input_thread_t *p_input = (input_thread_t *)p_this;
960 p_input->p_demux_data = NULL;
961 b_forced = ( ( *p_input->psz_demux )&&
962 ( !strncmp( p_input->psz_demux, "ogg", 10 ) ) ) ? 1 : 0;
964 /* Check if we are dealing with an ogg stream */
965 if( !b_forced && ( Ogg_Check( p_input ) != VLC_SUCCESS ) )
969 if( !( p_ogg = malloc( sizeof( demux_sys_t ) ) ) )
971 msg_Err( p_input, "out of memory" );
974 memset( p_ogg, 0, sizeof( demux_sys_t ) );
975 p_input->p_demux_data = p_ogg;
976 p_ogg->pp_stream = NULL;
978 /* Initialize the Ogg physical bitstream parser */
979 ogg_sync_init( &p_ogg->oy );
981 /* Set exported functions */
982 p_input->pf_demux = Demux;
983 p_input->pf_demux_control = Control;
985 /* Initialize access plug-in structures. */
986 if( p_input->i_mtu == 0 )
989 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
992 /* Create one program */
993 vlc_mutex_lock( &p_input->stream.stream_lock );
994 if( input_InitStream( p_input, 0 ) == -1)
996 vlc_mutex_unlock( &p_input->stream.stream_lock );
997 msg_Err( p_input, "cannot init stream" );
1000 if( input_AddProgram( p_input, 0, 0) == NULL )
1002 vlc_mutex_unlock( &p_input->stream.stream_lock );
1003 msg_Err( p_input, "cannot add program" );
1006 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1007 vlc_mutex_unlock( &p_input->stream.stream_lock );
1009 /* Begnning of stream, tell the demux to look for elementary streams. */
1012 p_ogg->i_prev_sync_state = SYNCHRO_REINIT;
1017 Deactivate( (vlc_object_t *)p_input );
1022 /****************************************************************************
1023 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1024 * Elementary streams.
1025 ****************************************************************************/
1026 static int Ogg_BeginningOfStream( input_thread_t *p_input, demux_sys_t *p_ogg)
1030 /* Find the logical streams embedded in the physical stream and
1031 * initialize our p_ogg structure. */
1032 if( Ogg_FindLogicalStreams( p_input, p_ogg ) != VLC_SUCCESS )
1034 msg_Warn( p_input, "couldn't find any ogg logical stream" );
1035 return VLC_EGENERIC;
1038 vlc_mutex_lock( &p_input->stream.stream_lock );
1039 p_input->stream.i_mux_rate = 0;
1040 vlc_mutex_unlock( &p_input->stream.stream_lock );
1042 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1044 #define p_stream p_ogg->pp_stream[i_stream]
1045 if( p_stream->fmt.i_codec != VLC_FOURCC('f','l','a','c') )
1046 p_stream->p_es = es_out_Add( p_input->p_es_out, &p_stream->fmt );
1048 vlc_mutex_lock( &p_input->stream.stream_lock );
1049 p_input->stream.i_mux_rate += (p_stream->fmt.i_bitrate / ( 8 * 50 ));
1050 vlc_mutex_unlock( &p_input->stream.stream_lock );
1052 p_stream->i_pcr = p_stream->i_previous_pcr = -1;
1053 p_stream->b_reinit = 0;
1060 /****************************************************************************
1061 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1062 ****************************************************************************/
1063 static void Ogg_EndOfStream( input_thread_t *p_input, demux_sys_t *p_ogg )
1067 #define p_stream p_ogg->pp_stream[i_stream]
1068 for( i_stream = 0 ; i_stream < p_ogg->i_streams; i_stream++ )
1070 es_out_Del( p_input->p_es_out, p_stream->p_es );
1072 vlc_mutex_lock( &p_input->stream.stream_lock );
1073 p_input->stream.i_mux_rate -= (p_stream->fmt.i_bitrate / ( 8 * 50 ));
1074 vlc_mutex_unlock( &p_input->stream.stream_lock );
1076 ogg_stream_clear( &p_ogg->pp_stream[i_stream]->os );
1077 for( j = 0; j < p_ogg->pp_stream[i_stream]->i_packets_backup; j++ )
1079 free( p_ogg->pp_stream[i_stream]->p_packets_backup[j].packet );
1081 if( p_ogg->pp_stream[i_stream]->p_packets_backup)
1082 free( p_ogg->pp_stream[i_stream]->p_packets_backup );
1084 free( p_ogg->pp_stream[i_stream] );
1089 if( p_ogg->pp_stream ) free( p_ogg->pp_stream );
1090 p_ogg->pp_stream = NULL;
1091 p_ogg->i_streams = 0;
1094 /*****************************************************************************
1095 * Deactivate: frees unused data
1096 *****************************************************************************/
1097 static void Deactivate( vlc_object_t *p_this )
1099 input_thread_t *p_input = (input_thread_t *)p_this;
1100 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data ;
1104 /* Cleanup the bitstream parser */
1105 ogg_sync_clear( &p_ogg->oy );
1107 Ogg_EndOfStream( p_input, p_ogg );
1113 /*****************************************************************************
1114 * Demux: reads and demuxes data packets
1115 *****************************************************************************
1116 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1117 *****************************************************************************/
1118 static int Demux( input_thread_t * p_input )
1120 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1122 ogg_packet oggpacket;
1125 #define p_stream p_ogg->pp_stream[i_stream]
1127 if( p_ogg->i_eos == p_ogg->i_streams )
1131 msg_Dbg( p_input, "end of a group of logical streams" );
1132 Ogg_EndOfStream( p_input, p_ogg );
1135 if( Ogg_BeginningOfStream( p_input, p_ogg ) != VLC_SUCCESS ) return 0;
1138 msg_Dbg( p_input, "beginning of a group of logical streams" );
1140 p_input->stream.p_selected_program->i_synchro_state = SYNCHRO_REINIT;
1141 input_ClockManageRef( p_input, p_input->stream.p_selected_program, 0 );
1144 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1146 msg_Warn( p_input, "synchro reinit" );
1148 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1150 /* we'll trash all the data until we find the next pcr */
1151 p_stream->b_reinit = 1;
1152 p_stream->i_pcr = -1;
1153 p_stream->i_interpolated_pcr = -1;
1155 if( p_ogg->i_prev_sync_state != SYNCHRO_REINIT )
1156 ogg_sync_reset( &p_ogg->oy );
1159 p_ogg->i_prev_sync_state =
1160 p_input->stream.p_selected_program->i_synchro_state;
1163 * Demux an ogg page from the stream
1165 if( Ogg_ReadPage( p_input, p_ogg, &oggpage ) != VLC_SUCCESS )
1170 /* Test for End of Stream */
1171 if( ogg_page_eos( &oggpage ) ) p_ogg->i_eos++;
1174 for( i_stream = 0; i_stream < p_ogg->i_streams; i_stream++ )
1176 if( ogg_stream_pagein( &p_stream->os, &oggpage ) != 0 )
1179 while( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1181 if( p_stream->b_reinit )
1183 /* If synchro is re-initialized we need to drop all the packets
1184 * until we find a new dated one. */
1185 Ogg_UpdatePCR( p_stream, &oggpacket );
1187 if( p_stream->i_pcr >= 0 )
1189 p_stream->b_reinit = 0;
1193 p_stream->i_interpolated_pcr = -1;
1197 /* An Ogg/vorbis packet contains an end date granulepos */
1198 if( p_stream->fmt.i_codec == VLC_FOURCC( 'v','o','r','b' ) ||
1199 p_stream->fmt.i_codec == VLC_FOURCC( 's','p','x',' ' ) ||
1200 p_stream->fmt.i_codec == VLC_FOURCC( 'f','l','a','c' ) )
1202 if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
1204 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1208 input_ClockManageRef( p_input,
1209 p_input->stream.p_selected_program, p_ogg->i_pcr );
1215 Ogg_DecodePacket( p_input, p_stream, &oggpacket );
1220 i_stream = 0; p_ogg->i_pcr = -1;
1221 for( ; i_stream < p_ogg->i_streams; i_stream++ )
1223 if( p_stream->fmt.i_cat == SPU_ES )
1225 if( p_stream->i_interpolated_pcr < 0 )
1228 if( p_ogg->i_pcr < 0 || p_stream->i_interpolated_pcr < p_ogg->i_pcr )
1229 p_ogg->i_pcr = p_stream->i_interpolated_pcr;
1232 if( p_input->stream.p_selected_program->i_synchro_state != SYNCHRO_REINIT )
1234 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1243 /*****************************************************************************
1245 *****************************************************************************/
1246 static int Control( input_thread_t *p_input, int i_query, va_list args )
1248 demux_sys_t *p_ogg = (demux_sys_t *)p_input->p_demux_data;
1253 case DEMUX_GET_TIME:
1254 pi64 = (int64_t*)va_arg( args, int64_t * );
1255 *pi64 = p_ogg->i_pcr * 100 / 9;
1259 return demux_vaControlDefault( p_input, i_query, args );