1 /*****************************************************************************
2 * ogg.c : ogg stream demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2007 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
8 * Andre Pang <Andre.Pang@csiro.au> (Annodex support)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
36 #include <vlc_input.h>
40 #include <vlc_codecs.h>
43 /*****************************************************************************
45 *****************************************************************************/
46 static int Open ( vlc_object_t
* );
47 static void Close( vlc_object_t
* );
50 set_shortname ( "OGG" );
51 set_description( N_("OGG demuxer" ) );
52 set_category( CAT_INPUT
);
53 set_subcategory( SUBCAT_INPUT_DEMUX
);
54 set_capability( "demux", 50 );
55 set_callbacks( Open
, Close
);
56 add_shortcut( "ogg" );
60 /*****************************************************************************
61 * Definitions of structures and functions used by this plugins
62 *****************************************************************************/
63 typedef struct logical_stream_s
65 ogg_stream_state os
; /* logical stream of packets */
73 /* the header of some logical streams (eg vorbis) contain essential
74 * data for the decoder. We back them up here in case we need to re-feed
75 * them to the decoder. */
81 /* program clock reference (in units of 90kHz) derived from the previous
84 mtime_t i_interpolated_pcr
;
85 mtime_t i_previous_pcr
;
91 /* kate streams have the number of headers in the ID header */
92 int i_kate_num_headers
;
94 /* for Annodex logical bitstreams */
95 int secondary_header_packets
;
101 ogg_sync_state oy
; /* sync and verify incoming physical bitstream */
103 int i_streams
; /* number of logical bitstreams */
104 logical_stream_t
**pp_stream
; /* pointer to an array of logical streams */
106 /* program clock reference (in units of 90kHz) derived from the pcr of
117 /* OggDS headers for the new header format (used in ogm files) */
118 typedef struct stream_header_video
122 } stream_header_video
;
124 typedef struct stream_header_audio
126 ogg_int16_t channels
;
127 ogg_int16_t blockalign
;
128 ogg_int32_t avgbytespersec
;
129 } stream_header_audio
;
131 typedef struct stream_header
136 ogg_int32_t size
; /* size of the structure */
138 ogg_int64_t time_unit
; /* in reference time */
139 ogg_int64_t samples_per_unit
;
140 ogg_int32_t default_len
; /* in media time */
142 ogg_int32_t buffersize
;
143 ogg_int16_t bits_per_sample
;
148 stream_header_video video
;
150 stream_header_audio audio
;
154 #define OGG_BLOCK_SIZE 4096
156 /* Some defines from OggDS */
157 #define PACKET_TYPE_HEADER 0x01
158 #define PACKET_TYPE_BITS 0x07
159 #define PACKET_LEN_BITS01 0xc0
160 #define PACKET_LEN_BITS2 0x02
161 #define PACKET_IS_SYNCPOINT 0x08
163 /*****************************************************************************
165 *****************************************************************************/
166 static int Demux ( demux_t
* );
167 static int Control( demux_t
*, int, va_list );
169 /* Bitstream manipulation */
170 static int Ogg_ReadPage ( demux_t
*, ogg_page
* );
171 static void Ogg_UpdatePCR ( logical_stream_t
*, ogg_packet
* );
172 static void Ogg_DecodePacket ( demux_t
*, logical_stream_t
*, ogg_packet
* );
174 static int Ogg_BeginningOfStream( demux_t
*p_demux
);
175 static int Ogg_FindLogicalStreams( demux_t
*p_demux
);
176 static void Ogg_EndOfStream( demux_t
*p_demux
);
178 /* Logical bitstream headers */
179 static void Ogg_ReadTheoraHeader( logical_stream_t
*, ogg_packet
* );
180 static void Ogg_ReadVorbisHeader( logical_stream_t
*, ogg_packet
* );
181 static void Ogg_ReadSpeexHeader( logical_stream_t
*, ogg_packet
* );
182 static void Ogg_ReadKateHeader( logical_stream_t
*, ogg_packet
* );
183 static void Ogg_ReadFlacHeader( demux_t
*, logical_stream_t
*, ogg_packet
* );
184 static void Ogg_ReadAnnodexHeader( vlc_object_t
*, logical_stream_t
*, ogg_packet
* );
185 static void Ogg_ReadDiracHeader( logical_stream_t
*, ogg_packet
* );
186 static uint32_t Ogg_ReadDiracPictureNumber( ogg_packet
*p_oggpacket
);
188 /*****************************************************************************
189 * Open: initializes ogg demux structures
190 *****************************************************************************/
191 static int Open( vlc_object_t
* p_this
)
193 demux_t
*p_demux
= (demux_t
*)p_this
;
195 const uint8_t *p_peek
;
198 /* Check if we are dealing with an ogg stream */
199 if( stream_Peek( p_demux
->s
, &p_peek
, 4 ) < 4 ) return VLC_EGENERIC
;
200 if( !p_demux
->b_force
&& memcmp( p_peek
, "OggS", 4 ) )
205 /* Set exported functions */
206 p_demux
->pf_demux
= Demux
;
207 p_demux
->pf_control
= Control
;
208 p_demux
->p_sys
= p_sys
= malloc( sizeof( demux_sys_t
) );
210 memset( p_sys
, 0, sizeof( demux_sys_t
) );
211 p_sys
->i_bitrate
= 0;
212 p_sys
->pp_stream
= NULL
;
214 /* Begnning of stream, tell the demux to look for elementary streams. */
217 /* Initialize the Ogg physical bitstream parser */
218 ogg_sync_init( &p_sys
->oy
);
223 /*****************************************************************************
224 * Close: frees unused data
225 *****************************************************************************/
226 static void Close( vlc_object_t
*p_this
)
228 demux_t
*p_demux
= (demux_t
*)p_this
;
229 demux_sys_t
*p_sys
= p_demux
->p_sys
;
231 /* Cleanup the bitstream parser */
232 ogg_sync_clear( &p_sys
->oy
);
234 Ogg_EndOfStream( p_demux
);
239 /*****************************************************************************
240 * Demux: reads and demuxes data packets
241 *****************************************************************************
242 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
243 *****************************************************************************/
244 static int Demux( demux_t
* p_demux
)
246 demux_sys_t
*p_sys
= p_demux
->p_sys
;
248 ogg_packet oggpacket
;
252 if( p_sys
->i_eos
== p_sys
->i_streams
)
256 msg_Dbg( p_demux
, "end of a group of logical streams" );
257 Ogg_EndOfStream( p_demux
);
261 if( Ogg_BeginningOfStream( p_demux
) != VLC_SUCCESS
) return 0;
263 msg_Dbg( p_demux
, "beginning of a group of logical streams" );
264 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
268 * Demux an ogg page from the stream
270 if( Ogg_ReadPage( p_demux
, &oggpage
) != VLC_SUCCESS
)
275 /* Test for End of Stream */
276 if( ogg_page_eos( &oggpage
) ) p_sys
->i_eos
++;
279 for( i_stream
= 0; i_stream
< p_sys
->i_streams
; i_stream
++ )
281 logical_stream_t
*p_stream
= p_sys
->pp_stream
[i_stream
];
283 if( ogg_stream_pagein( &p_stream
->os
, &oggpage
) != 0 )
286 while( ogg_stream_packetout( &p_stream
->os
, &oggpacket
) > 0 )
288 /* Read info from any secondary header packets, if there are any */
289 if( p_stream
->secondary_header_packets
> 0 )
291 if( p_stream
->fmt
.i_codec
== VLC_FOURCC('t','h','e','o') &&
292 oggpacket
.bytes
>= 7 &&
293 ! memcmp( oggpacket
.packet
, "\x80theora", 7 ) )
295 Ogg_ReadTheoraHeader( p_stream
, &oggpacket
);
296 p_stream
->secondary_header_packets
= 0;
298 else if( p_stream
->fmt
.i_codec
== VLC_FOURCC('v','o','r','b') &&
299 oggpacket
.bytes
>= 7 &&
300 ! memcmp( oggpacket
.packet
, "\x01vorbis", 7 ) )
302 Ogg_ReadVorbisHeader( p_stream
, &oggpacket
);
303 p_stream
->secondary_header_packets
= 0;
305 else if ( p_stream
->fmt
.i_codec
== VLC_FOURCC('c','m','m','l') )
307 p_stream
->secondary_header_packets
= 0;
311 if( p_stream
->b_reinit
)
313 /* If synchro is re-initialized we need to drop all the packets
314 * until we find a new dated one. */
315 Ogg_UpdatePCR( p_stream
, &oggpacket
);
317 if( p_stream
->i_pcr
>= 0 )
319 p_stream
->b_reinit
= 0;
323 p_stream
->i_interpolated_pcr
= -1;
327 /* An Ogg/vorbis packet contains an end date granulepos */
328 if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 'v','o','r','b' ) ||
329 p_stream
->fmt
.i_codec
== VLC_FOURCC( 's','p','x',' ' ) ||
330 p_stream
->fmt
.i_codec
== VLC_FOURCC( 'f','l','a','c' ) )
332 if( ogg_stream_packetout( &p_stream
->os
, &oggpacket
) > 0 )
334 Ogg_DecodePacket( p_demux
, p_stream
, &oggpacket
);
338 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
,
345 Ogg_DecodePacket( p_demux
, p_stream
, &oggpacket
);
350 i_stream
= 0; p_sys
->i_pcr
= -1;
351 for( ; i_stream
< p_sys
->i_streams
; i_stream
++ )
353 logical_stream_t
*p_stream
= p_sys
->pp_stream
[i_stream
];
355 if( p_stream
->fmt
.i_cat
== SPU_ES
)
357 if( p_stream
->i_interpolated_pcr
< 0 )
360 if( p_sys
->i_pcr
< 0 || p_stream
->i_interpolated_pcr
< p_sys
->i_pcr
)
361 p_sys
->i_pcr
= p_stream
->i_interpolated_pcr
;
364 if( p_sys
->i_pcr
>= 0 )
366 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_sys
->i_pcr
);
372 /*****************************************************************************
374 *****************************************************************************/
375 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
377 demux_sys_t
*p_sys
= p_demux
->p_sys
;
384 case DEMUX_HAS_UNSUPPORTED_META
:
385 pb_bool
= (bool*)va_arg( args
, bool* );
390 pi64
= (int64_t*)va_arg( args
, int64_t * );
391 *pi64
= p_sys
->i_pcr
;
397 case DEMUX_SET_POSITION
:
398 for( i
= 0; i
< p_sys
->i_streams
; i
++ )
400 logical_stream_t
*p_stream
= p_sys
->pp_stream
[i
];
402 /* we'll trash all the data until we find the next pcr */
403 p_stream
->b_reinit
= 1;
404 p_stream
->i_pcr
= -1;
405 p_stream
->i_interpolated_pcr
= -1;
406 ogg_stream_reset( &p_stream
->os
);
408 ogg_sync_reset( &p_sys
->oy
);
411 return demux_vaControlHelper( p_demux
->s
, 0, -1, p_sys
->i_bitrate
,
416 /****************************************************************************
417 * Ogg_ReadPage: Read a full Ogg page from the physical bitstream.
418 ****************************************************************************
419 * Returns VLC_SUCCESS if a page has been read. An error might happen if we
420 * are at the end of stream.
421 ****************************************************************************/
422 static int Ogg_ReadPage( demux_t
*p_demux
, ogg_page
*p_oggpage
)
424 demux_sys_t
*p_ogg
= p_demux
->p_sys
;
428 while( ogg_sync_pageout( &p_ogg
->oy
, p_oggpage
) != 1 )
430 p_buffer
= ogg_sync_buffer( &p_ogg
->oy
, OGG_BLOCK_SIZE
);
432 i_read
= stream_Read( p_demux
->s
, p_buffer
, OGG_BLOCK_SIZE
);
436 ogg_sync_wrote( &p_ogg
->oy
, i_read
);
442 /****************************************************************************
443 * Ogg_UpdatePCR: update the PCR (90kHz program clock reference) for the
445 ****************************************************************************/
446 static void Ogg_UpdatePCR( logical_stream_t
*p_stream
,
447 ogg_packet
*p_oggpacket
)
449 /* Convert the granulepos into a pcr */
450 if( p_oggpacket
->granulepos
>= 0 )
452 if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 't','h','e','o' ) ||
453 p_stream
->fmt
.i_codec
== VLC_FOURCC( 'd','r','a','c' ) ||
454 p_stream
->fmt
.i_codec
== VLC_FOURCC( 'k','a','t','e' ) )
456 ogg_int64_t iframe
= p_oggpacket
->granulepos
>>
457 p_stream
->i_granule_shift
;
458 ogg_int64_t pframe
= p_oggpacket
->granulepos
-
459 ( iframe
<< p_stream
->i_granule_shift
);
461 p_stream
->i_pcr
= ( iframe
+ pframe
) * INT64_C(1000000)
466 p_stream
->i_pcr
= p_oggpacket
->granulepos
* INT64_C(1000000)
470 p_stream
->i_interpolated_pcr
= p_stream
->i_pcr
;
474 p_stream
->i_pcr
= -1;
476 /* no granulepos available, try to interpolate the pcr.
477 * If we can't then don't touch the old value. */
478 if( p_stream
->fmt
.i_cat
== VIDEO_ES
)
479 /* 1 frame per packet */
480 p_stream
->i_interpolated_pcr
+= (INT64_C(1000000) / p_stream
->f_rate
);
481 else if( p_stream
->fmt
.i_bitrate
)
482 p_stream
->i_interpolated_pcr
+=
483 ( p_oggpacket
->bytes
* INT64_C(1000000) /
484 p_stream
->fmt
.i_bitrate
/ 8 );
488 /****************************************************************************
489 * Ogg_DecodePacket: Decode an Ogg packet.
490 ****************************************************************************/
491 static void Ogg_DecodePacket( demux_t
*p_demux
,
492 logical_stream_t
*p_stream
,
493 ogg_packet
*p_oggpacket
)
497 int i_header_len
= 0;
498 mtime_t i_pts
= -1, i_interpolated_pts
;
501 if( !p_oggpacket
->bytes
)
503 msg_Dbg( p_demux
, "discarding 0 sized packet" );
507 if( p_oggpacket
->bytes
>= 7 &&
508 ! memcmp ( p_oggpacket
->packet
, "Annodex", 7 ) )
510 /* it's an Annodex packet -- skip it (do nothing) */
513 else if( p_oggpacket
->bytes
>= 7 &&
514 ! memcmp ( p_oggpacket
->packet
, "AnxData", 7 ) )
516 /* it's an AnxData packet -- skip it (do nothing) */
520 if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 's','u','b','t' ) &&
521 p_oggpacket
->packet
[0] & PACKET_TYPE_BITS
) return;
523 /* Check the ES is selected */
524 es_out_Control( p_demux
->out
, ES_OUT_GET_ES_STATE
,
525 p_stream
->p_es
, &b_selected
);
527 if( p_stream
->b_force_backup
)
530 bool b_store_size
= true;
531 bool b_store_num_headers
= false;
533 p_stream
->i_packets_backup
++;
534 switch( p_stream
->fmt
.i_codec
)
536 case VLC_FOURCC( 'v','o','r','b' ):
537 case VLC_FOURCC( 's','p','x',' ' ):
538 case VLC_FOURCC( 't','h','e','o' ):
539 if( p_stream
->i_packets_backup
== 3 ) p_stream
->b_force_backup
= 0;
542 case VLC_FOURCC( 'f','l','a','c' ):
543 if( !p_stream
->fmt
.audio
.i_rate
&& p_stream
->i_packets_backup
== 2 )
545 Ogg_ReadFlacHeader( p_demux
, p_stream
, p_oggpacket
);
546 p_stream
->b_force_backup
= 0;
548 else if( p_stream
->fmt
.audio
.i_rate
)
550 p_stream
->b_force_backup
= 0;
551 if( p_oggpacket
->bytes
>= 9 )
553 p_oggpacket
->packet
+= 9;
554 p_oggpacket
->bytes
-= 9;
557 b_store_size
= false;
560 case VLC_FOURCC( 'k','a','t','e' ):
561 if( p_stream
->i_packets_backup
== 1)
562 b_store_num_headers
= true;
563 if( p_stream
->i_packets_backup
== p_stream
->i_kate_num_headers
) p_stream
->b_force_backup
= 0;
567 p_stream
->b_force_backup
= 0;
571 /* Backup the ogg packet (likely an header packet) */
572 p_stream
->p_headers
=
573 realloc( p_stream
->p_headers
, p_stream
->i_headers
+
574 p_oggpacket
->bytes
+ (b_store_size
? 2 : 0) + (b_store_num_headers
? 1 : 0) );
575 p_extra
= p_stream
->p_headers
+ p_stream
->i_headers
;
576 if( b_store_num_headers
)
578 /* Kate streams store the number of headers in the first header,
579 so we can't just test for 3 as Vorbis/Theora */
580 *(p_extra
++) = p_stream
->i_kate_num_headers
;
584 *(p_extra
++) = p_oggpacket
->bytes
>> 8;
585 *(p_extra
++) = p_oggpacket
->bytes
& 0xFF;
587 memcpy( p_extra
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
588 p_stream
->i_headers
+= p_oggpacket
->bytes
+ (b_store_size
? 2 : 0) + (b_store_num_headers
? 1 : 0);
590 if( !p_stream
->b_force_backup
)
592 /* Last header received, commit changes */
593 p_stream
->fmt
.i_extra
= p_stream
->i_headers
;
594 p_stream
->fmt
.p_extra
=
595 realloc( p_stream
->fmt
.p_extra
, p_stream
->i_headers
);
596 memcpy( p_stream
->fmt
.p_extra
, p_stream
->p_headers
,
597 p_stream
->i_headers
);
598 es_out_Control( p_demux
->out
, ES_OUT_SET_FMT
,
599 p_stream
->p_es
, &p_stream
->fmt
);
602 b_selected
= false; /* Discard the header packet */
605 /* Convert the pcr into a pts */
606 if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 'v','o','r','b' ) ||
607 p_stream
->fmt
.i_codec
== VLC_FOURCC( 's','p','x',' ' ) ||
608 p_stream
->fmt
.i_codec
== VLC_FOURCC( 'f','l','a','c' ) )
610 if( p_stream
->i_pcr
>= 0 )
612 /* This is for streams where the granulepos of the header packets
613 * doesn't match these of the data packets (eg. ogg web radios). */
614 if( p_stream
->i_previous_pcr
== 0 &&
615 p_stream
->i_pcr
> 3 * DEFAULT_PTS_DELAY
)
617 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
619 /* Call the pace control */
620 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
,
624 p_stream
->i_previous_pcr
= p_stream
->i_pcr
;
626 /* The granulepos is the end date of the sample */
627 i_pts
= p_stream
->i_pcr
;
631 /* Convert the granulepos into the next pcr */
632 i_interpolated_pts
= p_stream
->i_interpolated_pcr
;
633 Ogg_UpdatePCR( p_stream
, p_oggpacket
);
635 if( p_stream
->i_pcr
>= 0 )
637 /* This is for streams where the granulepos of the header packets
638 * doesn't match these of the data packets (eg. ogg web radios). */
639 if( p_stream
->i_previous_pcr
== 0 &&
640 p_stream
->i_pcr
> 3 * DEFAULT_PTS_DELAY
)
642 es_out_Control( p_demux
->out
, ES_OUT_RESET_PCR
);
644 /* Call the pace control */
645 es_out_Control( p_demux
->out
, ES_OUT_SET_PCR
, p_stream
->i_pcr
);
649 if( p_stream
->fmt
.i_codec
!= VLC_FOURCC( 'v','o','r','b' ) &&
650 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 's','p','x',' ' ) &&
651 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 'f','l','a','c' ) &&
652 p_stream
->i_pcr
>= 0 )
654 p_stream
->i_previous_pcr
= p_stream
->i_pcr
;
656 /* The granulepos is the start date of the sample */
657 i_pts
= p_stream
->i_pcr
;
662 /* This stream isn't currently selected so we don't need to decode it,
663 * but we did need to store its pcr as it might be selected later on */
667 if( p_oggpacket
->bytes
<= 0 )
670 if( !( p_block
= block_New( p_demux
, p_oggpacket
->bytes
) ) ) return;
673 if( i_pts
== 0 ) i_pts
= 1;
674 else if( i_pts
== -1 && i_interpolated_pts
== 0 ) i_pts
= 1;
675 else if( i_pts
== -1 ) i_pts
= 0;
677 if( p_stream
->fmt
.i_cat
== AUDIO_ES
)
678 p_block
->i_dts
= p_block
->i_pts
= i_pts
;
679 else if( p_stream
->fmt
.i_cat
== SPU_ES
)
681 p_block
->i_dts
= p_block
->i_pts
= i_pts
;
682 p_block
->i_length
= 0;
684 else if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 't','h','e','o' ) )
685 p_block
->i_dts
= p_block
->i_pts
= i_pts
;
686 else if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 'd','r','a','c' ) )
688 /* every packet[1] in the stream contains a picture, there may
689 * be header cruft infront of it though
690 * [1] EXCEPT the BOS page/packet */
691 uint32_t u_pnum
= Ogg_ReadDiracPictureNumber( p_oggpacket
);
693 if ( u_pnum
!= 0xffffffff ) {
695 p_block
->i_pts
= (u_pnum
* INT64_C(1000000) / p_stream
->f_rate
);
700 p_block
->i_dts
= i_pts
;
704 if( p_stream
->fmt
.i_codec
!= VLC_FOURCC( 'v','o','r','b' ) &&
705 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 's','p','x',' ' ) &&
706 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 'f','l','a','c' ) &&
707 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 't','a','r','k' ) &&
708 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 't','h','e','o' ) &&
709 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 'c','m','m','l' ) &&
710 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 'd','r','a','c' ) &&
711 p_stream
->fmt
.i_codec
!= VLC_FOURCC( 'k','a','t','e' ) )
713 /* We remove the header from the packet */
714 i_header_len
= (*p_oggpacket
->packet
& PACKET_LEN_BITS01
) >> 6;
715 i_header_len
|= (*p_oggpacket
->packet
& PACKET_LEN_BITS2
) << 1;
717 if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 's','u','b','t' ))
719 /* But with subtitles we need to retrieve the duration first */
722 if( i_header_len
> 0 && p_oggpacket
->bytes
>= i_header_len
+ 1 )
724 for( i
= 0, lenbytes
= 0; i
< i_header_len
; i
++ )
726 lenbytes
= lenbytes
<< 8;
727 lenbytes
+= *(p_oggpacket
->packet
+ i_header_len
- i
);
730 if( p_oggpacket
->bytes
- 1 - i_header_len
> 2 ||
731 ( p_oggpacket
->packet
[i_header_len
+ 1] != ' ' &&
732 p_oggpacket
->packet
[i_header_len
+ 1] != 0 &&
733 p_oggpacket
->packet
[i_header_len
+ 1] != '\n' &&
734 p_oggpacket
->packet
[i_header_len
+ 1] != '\r' ) )
736 p_block
->i_length
= (mtime_t
)lenbytes
* 1000;
741 if( p_block
->i_buffer
>= (unsigned int)i_header_len
)
742 p_block
->i_buffer
-= i_header_len
;
744 p_block
->i_buffer
= 0;
747 if( p_stream
->fmt
.i_codec
== VLC_FOURCC( 't','a','r','k' ) )
749 /* FIXME: the biggest hack I've ever done */
750 msg_Warn( p_demux
, "tarkin pts: %"PRId64
", granule: %"PRId64
,
751 p_block
->i_pts
, p_block
->i_dts
);
755 memcpy( p_block
->p_buffer
, p_oggpacket
->packet
+ i_header_len
,
756 p_oggpacket
->bytes
- i_header_len
);
758 es_out_Send( p_demux
->out
, p_stream
->p_es
, p_block
);
761 /****************************************************************************
762 * Ogg_FindLogicalStreams: Find the logical streams embedded in the physical
763 * stream and fill p_ogg.
764 *****************************************************************************
765 * The initial page of a logical stream is marked as a 'bos' page.
766 * Furthermore, the Ogg specification mandates that grouped bitstreams begin
767 * together and all of the initial pages must appear before any data pages.
769 * On success this function returns VLC_SUCCESS.
770 ****************************************************************************/
771 static int Ogg_FindLogicalStreams( demux_t
*p_demux
)
773 demux_sys_t
*p_ogg
= p_demux
->p_sys
;
774 ogg_packet oggpacket
;
778 #define p_stream p_ogg->pp_stream[p_ogg->i_streams - 1]
780 while( Ogg_ReadPage( p_demux
, &oggpage
) == VLC_SUCCESS
)
782 if( ogg_page_bos( &oggpage
) )
785 /* All is wonderful in our fine fine little world.
786 * We found the beginning of our first logical stream. */
787 while( ogg_page_bos( &oggpage
) )
791 realloc( p_ogg
->pp_stream
, p_ogg
->i_streams
*
792 sizeof(logical_stream_t
*) );
794 p_stream
= malloc( sizeof(logical_stream_t
) );
795 memset( p_stream
, 0, sizeof(logical_stream_t
) );
796 p_stream
->p_headers
= 0;
797 p_stream
->secondary_header_packets
= 0;
799 es_format_Init( &p_stream
->fmt
, 0, 0 );
801 /* Setup the logical stream */
802 p_stream
->i_serial_no
= ogg_page_serialno( &oggpage
);
803 ogg_stream_init( &p_stream
->os
, p_stream
->i_serial_no
);
805 /* Extract the initial header from the first page and verify
806 * the codec type of tis Ogg bitstream */
807 if( ogg_stream_pagein( &p_stream
->os
, &oggpage
) < 0 )
809 /* error. stream version mismatch perhaps */
810 msg_Err( p_demux
, "error reading first page of "
811 "Ogg bitstream data" );
815 /* FIXME: check return value */
816 ogg_stream_packetpeek( &p_stream
->os
, &oggpacket
);
818 /* Check for Vorbis header */
819 if( oggpacket
.bytes
>= 7 &&
820 ! memcmp( oggpacket
.packet
, "\x01vorbis", 7 ) )
822 Ogg_ReadVorbisHeader( p_stream
, &oggpacket
);
823 msg_Dbg( p_demux
, "found vorbis header" );
825 /* Check for Speex header */
826 else if( oggpacket
.bytes
>= 5 &&
827 ! memcmp( oggpacket
.packet
, "Speex", 5 ) )
829 Ogg_ReadSpeexHeader( p_stream
, &oggpacket
);
830 msg_Dbg( p_demux
, "found speex header, channels: %i, "
831 "rate: %i, bitrate: %i",
832 p_stream
->fmt
.audio
.i_channels
,
833 (int)p_stream
->f_rate
, p_stream
->fmt
.i_bitrate
);
835 /* Check for Flac header (< version 1.1.1) */
836 else if( oggpacket
.bytes
>= 4 &&
837 ! memcmp( oggpacket
.packet
, "fLaC", 4 ) )
839 msg_Dbg( p_demux
, "found FLAC header" );
841 /* Grrrr!!!! Did they really have to put all the
842 * important info in the second header packet!!!
843 * (STREAMINFO metadata is in the following packet) */
844 p_stream
->b_force_backup
= 1;
846 p_stream
->fmt
.i_cat
= AUDIO_ES
;
847 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'f','l','a','c' );
849 /* Check for Flac header (>= version 1.1.1) */
850 else if( oggpacket
.bytes
>= 13 && oggpacket
.packet
[0] ==0x7F &&
851 ! memcmp( &oggpacket
.packet
[1], "FLAC", 4 ) &&
852 ! memcmp( &oggpacket
.packet
[9], "fLaC", 4 ) )
854 int i_packets
= ((int)oggpacket
.packet
[7]) << 8 |
856 msg_Dbg( p_demux
, "found FLAC header version %i.%i "
857 "(%i header packets)",
858 oggpacket
.packet
[5], oggpacket
.packet
[6],
861 p_stream
->b_force_backup
= 1;
863 p_stream
->fmt
.i_cat
= AUDIO_ES
;
864 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'f','l','a','c' );
865 oggpacket
.packet
+= 13; oggpacket
.bytes
-= 13;
866 Ogg_ReadFlacHeader( p_demux
, p_stream
, &oggpacket
);
868 /* Check for Theora header */
869 else if( oggpacket
.bytes
>= 7 &&
870 ! memcmp( oggpacket
.packet
, "\x80theora", 7 ) )
872 Ogg_ReadTheoraHeader( p_stream
, &oggpacket
);
875 "found theora header, bitrate: %i, rate: %f",
876 p_stream
->fmt
.i_bitrate
, p_stream
->f_rate
);
878 /* Check for Dirac header */
879 else if( oggpacket
.bytes
>= 5 &&
880 ! memcmp( oggpacket
.packet
, "BBCD\x00", 5 ) )
882 Ogg_ReadDiracHeader( p_stream
, &oggpacket
);
883 msg_Dbg( p_demux
, "found dirac header" );
885 /* Check for Tarkin header */
886 else if( oggpacket
.bytes
>= 7 &&
887 ! memcmp( &oggpacket
.packet
[1], "tarkin", 6 ) )
891 msg_Dbg( p_demux
, "found tarkin header" );
892 p_stream
->fmt
.i_cat
= VIDEO_ES
;
893 p_stream
->fmt
.i_codec
= VLC_FOURCC( 't','a','r','k' );
895 /* Cheat and get additionnal info ;) */
896 oggpack_readinit( &opb
, oggpacket
.packet
, oggpacket
.bytes
);
897 oggpack_adv( &opb
, 88 );
898 oggpack_adv( &opb
, 104 );
899 p_stream
->fmt
.i_bitrate
= oggpack_read( &opb
, 32 );
900 p_stream
->f_rate
= 2; /* FIXME */
902 "found tarkin header, bitrate: %i, rate: %f",
903 p_stream
->fmt
.i_bitrate
, p_stream
->f_rate
);
905 /* Check for Annodex header */
906 else if( oggpacket
.bytes
>= 7 &&
907 ! memcmp( oggpacket
.packet
, "Annodex", 7 ) )
909 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux
), p_stream
,
911 /* kill annodex track */
915 /* Check for Annodex header */
916 else if( oggpacket
.bytes
>= 7 &&
917 ! memcmp( oggpacket
.packet
, "AnxData", 7 ) )
919 Ogg_ReadAnnodexHeader( VLC_OBJECT(p_demux
), p_stream
,
922 /* Check for Kate header */
923 else if( oggpacket
.bytes
>= 8 &&
924 ! memcmp( &oggpacket
.packet
[1], "kate\0\0\0", 7 ) )
926 Ogg_ReadKateHeader( p_stream
, &oggpacket
);
927 msg_Dbg( p_demux
, "found kate header" );
929 else if( oggpacket
.bytes
>= 142 &&
930 !memcmp( &oggpacket
.packet
[1],
931 "Direct Show Samples embedded in Ogg", 35 ))
933 /* Old header type */
935 /* Check for video header (old format) */
936 if( GetDWLE((oggpacket
.packet
+96)) == 0x05589f80 &&
937 oggpacket
.bytes
>= 184 )
939 p_stream
->fmt
.i_cat
= VIDEO_ES
;
940 p_stream
->fmt
.i_codec
=
941 VLC_FOURCC( oggpacket
.packet
[68],
942 oggpacket
.packet
[69],
943 oggpacket
.packet
[70],
944 oggpacket
.packet
[71] );
945 msg_Dbg( p_demux
, "found video header of type: %.4s",
946 (char *)&p_stream
->fmt
.i_codec
);
948 p_stream
->fmt
.video
.i_frame_rate
= 10000000;
949 p_stream
->fmt
.video
.i_frame_rate_base
=
950 GetQWLE((oggpacket
.packet
+164));
951 p_stream
->f_rate
= 10000000.0 /
952 GetQWLE((oggpacket
.packet
+164));
953 p_stream
->fmt
.video
.i_bits_per_pixel
=
954 GetWLE((oggpacket
.packet
+182));
955 if( !p_stream
->fmt
.video
.i_bits_per_pixel
)
957 p_stream
->fmt
.video
.i_bits_per_pixel
= 24;
958 p_stream
->fmt
.video
.i_width
=
959 GetDWLE((oggpacket
.packet
+176));
960 p_stream
->fmt
.video
.i_height
=
961 GetDWLE((oggpacket
.packet
+180));
964 "fps: %f, width:%i; height:%i, bitcount:%i",
966 p_stream
->fmt
.video
.i_width
,
967 p_stream
->fmt
.video
.i_height
,
968 p_stream
->fmt
.video
.i_bits_per_pixel
);
971 /* Check for audio header (old format) */
972 else if( GetDWLE((oggpacket
.packet
+96)) == 0x05589F81 )
974 unsigned int i_extra_size
;
975 unsigned int i_format_tag
;
977 p_stream
->fmt
.i_cat
= AUDIO_ES
;
979 i_extra_size
= GetWLE((oggpacket
.packet
+140));
982 p_stream
->fmt
.i_extra
= i_extra_size
;
983 p_stream
->fmt
.p_extra
= malloc( i_extra_size
);
984 memcpy( p_stream
->fmt
.p_extra
,
985 oggpacket
.packet
+ 142, i_extra_size
);
988 i_format_tag
= GetWLE((oggpacket
.packet
+124));
989 p_stream
->fmt
.audio
.i_channels
=
990 GetWLE((oggpacket
.packet
+126));
991 p_stream
->f_rate
= p_stream
->fmt
.audio
.i_rate
=
992 GetDWLE((oggpacket
.packet
+128));
993 p_stream
->fmt
.i_bitrate
=
994 GetDWLE((oggpacket
.packet
+132)) * 8;
995 p_stream
->fmt
.audio
.i_blockalign
=
996 GetWLE((oggpacket
.packet
+136));
997 p_stream
->fmt
.audio
.i_bitspersample
=
998 GetWLE((oggpacket
.packet
+138));
1000 wf_tag_to_fourcc( i_format_tag
,
1001 &p_stream
->fmt
.i_codec
, 0 );
1003 if( p_stream
->fmt
.i_codec
==
1004 VLC_FOURCC('u','n','d','f') )
1006 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'm', 's',
1007 ( i_format_tag
>> 8 ) & 0xff,
1008 i_format_tag
& 0xff );
1011 msg_Dbg( p_demux
, "found audio header of type: %.4s",
1012 (char *)&p_stream
->fmt
.i_codec
);
1013 msg_Dbg( p_demux
, "audio:0x%4.4x channels:%d %dHz "
1014 "%dbits/sample %dkb/s",
1016 p_stream
->fmt
.audio
.i_channels
,
1017 p_stream
->fmt
.audio
.i_rate
,
1018 p_stream
->fmt
.audio
.i_bitspersample
,
1019 p_stream
->fmt
.i_bitrate
/ 1024 );
1024 msg_Dbg( p_demux
, "stream %d has an old header "
1025 "but is of an unknown type", p_ogg
->i_streams
-1 );
1030 else if( (*oggpacket
.packet
& PACKET_TYPE_BITS
)
1031 == PACKET_TYPE_HEADER
&&
1032 oggpacket
.bytes
>= (int)sizeof(stream_header
)+1 )
1034 stream_header
*st
= (stream_header
*)(oggpacket
.packet
+1);
1036 /* Check for video header (new format) */
1037 if( !strncmp( st
->streamtype
, "video", 5 ) )
1039 p_stream
->fmt
.i_cat
= VIDEO_ES
;
1041 /* We need to get rid of the header packet */
1042 ogg_stream_packetout( &p_stream
->os
, &oggpacket
);
1044 p_stream
->fmt
.i_codec
=
1045 VLC_FOURCC( st
->subtype
[0], st
->subtype
[1],
1046 st
->subtype
[2], st
->subtype
[3] );
1047 msg_Dbg( p_demux
, "found video header of type: %.4s",
1048 (char *)&p_stream
->fmt
.i_codec
);
1050 p_stream
->fmt
.video
.i_frame_rate
= 10000000;
1051 p_stream
->fmt
.video
.i_frame_rate_base
=
1052 GetQWLE(&st
->time_unit
);
1053 p_stream
->f_rate
= 10000000.0 /
1054 GetQWLE(&st
->time_unit
);
1055 p_stream
->fmt
.video
.i_bits_per_pixel
=
1056 GetWLE(&st
->bits_per_sample
);
1057 p_stream
->fmt
.video
.i_width
=
1058 GetDWLE(&st
->sh
.video
.width
);
1059 p_stream
->fmt
.video
.i_height
=
1060 GetDWLE(&st
->sh
.video
.height
);
1063 "fps: %f, width:%i; height:%i, bitcount:%i",
1065 p_stream
->fmt
.video
.i_width
,
1066 p_stream
->fmt
.video
.i_height
,
1067 p_stream
->fmt
.video
.i_bits_per_pixel
);
1069 /* Check for audio header (new format) */
1070 else if( !strncmp( st
->streamtype
, "audio", 5 ) )
1075 p_stream
->fmt
.i_cat
= AUDIO_ES
;
1077 /* We need to get rid of the header packet */
1078 ogg_stream_packetout( &p_stream
->os
, &oggpacket
);
1080 p_stream
->fmt
.i_extra
= GetQWLE(&st
->size
) -
1081 sizeof(stream_header
);
1082 if( p_stream
->fmt
.i_extra
)
1084 p_stream
->fmt
.p_extra
=
1085 malloc( p_stream
->fmt
.i_extra
);
1086 memcpy( p_stream
->fmt
.p_extra
, st
+ 1,
1087 p_stream
->fmt
.i_extra
);
1090 memcpy( p_buffer
, st
->subtype
, 4 );
1092 i_format_tag
= strtol(p_buffer
,NULL
,16);
1093 p_stream
->fmt
.audio
.i_channels
=
1094 GetWLE(&st
->sh
.audio
.channels
);
1095 p_stream
->f_rate
= p_stream
->fmt
.audio
.i_rate
=
1096 GetQWLE(&st
->samples_per_unit
);
1097 p_stream
->fmt
.i_bitrate
=
1098 GetDWLE(&st
->sh
.audio
.avgbytespersec
) * 8;
1099 p_stream
->fmt
.audio
.i_blockalign
=
1100 GetWLE(&st
->sh
.audio
.blockalign
);
1101 p_stream
->fmt
.audio
.i_bitspersample
=
1102 GetWLE(&st
->bits_per_sample
);
1104 wf_tag_to_fourcc( i_format_tag
,
1105 &p_stream
->fmt
.i_codec
, 0 );
1107 if( p_stream
->fmt
.i_codec
==
1108 VLC_FOURCC('u','n','d','f') )
1110 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'm', 's',
1111 ( i_format_tag
>> 8 ) & 0xff,
1112 i_format_tag
& 0xff );
1115 msg_Dbg( p_demux
, "found audio header of type: %.4s",
1116 (char *)&p_stream
->fmt
.i_codec
);
1117 msg_Dbg( p_demux
, "audio:0x%4.4x channels:%d %dHz "
1118 "%dbits/sample %dkb/s",
1120 p_stream
->fmt
.audio
.i_channels
,
1121 p_stream
->fmt
.audio
.i_rate
,
1122 p_stream
->fmt
.audio
.i_bitspersample
,
1123 p_stream
->fmt
.i_bitrate
/ 1024 );
1125 /* Check for text (subtitles) header */
1126 else if( !strncmp(st
->streamtype
, "text", 4) )
1128 /* We need to get rid of the header packet */
1129 ogg_stream_packetout( &p_stream
->os
, &oggpacket
);
1131 msg_Dbg( p_demux
, "found text subtitles header" );
1132 p_stream
->fmt
.i_cat
= SPU_ES
;
1133 p_stream
->fmt
.i_codec
= VLC_FOURCC('s','u','b','t');
1134 p_stream
->f_rate
= 1000; /* granulepos is in milisec */
1138 msg_Dbg( p_demux
, "stream %d has a header marker "
1139 "but is of an unknown type", p_ogg
->i_streams
-1 );
1144 else if( oggpacket
.bytes
>= 7 &&
1145 ! memcmp( oggpacket
.packet
, "fishead", 7 ) )
1149 msg_Dbg( p_demux
, "stream %d is a skeleton",
1150 p_ogg
->i_streams
-1 );
1151 /* FIXME: https://trac.videolan.org/vlc/ticket/1412 */
1155 msg_Dbg( p_demux
, "stream %d is of unknown type",
1156 p_ogg
->i_streams
-1 );
1161 if( Ogg_ReadPage( p_demux
, &oggpage
) != VLC_SUCCESS
)
1162 return VLC_EGENERIC
;
1165 /* This is the first data page, which means we are now finished
1166 * with the initial pages. We just need to store it in the relevant
1168 for( i_stream
= 0; i_stream
< p_ogg
->i_streams
; i_stream
++ )
1170 if( ogg_stream_pagein( &p_ogg
->pp_stream
[i_stream
]->os
,
1182 return VLC_EGENERIC
;
1185 /****************************************************************************
1186 * Ogg_BeginningOfStream: Look for Beginning of Stream ogg pages and add
1187 * Elementary streams.
1188 ****************************************************************************/
1189 static int Ogg_BeginningOfStream( demux_t
*p_demux
)
1191 demux_sys_t
*p_ogg
= p_demux
->p_sys
;
1194 /* Find the logical streams embedded in the physical stream and
1195 * initialize our p_ogg structure. */
1196 if( Ogg_FindLogicalStreams( p_demux
) != VLC_SUCCESS
)
1198 msg_Warn( p_demux
, "couldn't find any ogg logical stream" );
1199 return VLC_EGENERIC
;
1202 p_ogg
->i_bitrate
= 0;
1204 for( i_stream
= 0 ; i_stream
< p_ogg
->i_streams
; i_stream
++ )
1206 #define p_stream p_ogg->pp_stream[i_stream]
1207 p_stream
->p_es
= es_out_Add( p_demux
->out
, &p_stream
->fmt
);
1209 // TODO: something to do here ?
1210 if( p_stream
->fmt
.i_codec
== VLC_FOURCC('c','m','m','l') )
1212 /* Set the CMML stream active */
1213 es_out_Control( p_demux
->out
, ES_OUT_SET_ES
, p_stream
->p_es
);
1216 p_ogg
->i_bitrate
+= p_stream
->fmt
.i_bitrate
;
1218 p_stream
->i_pcr
= p_stream
->i_previous_pcr
=
1219 p_stream
->i_interpolated_pcr
= -1;
1220 p_stream
->b_reinit
= 0;
1227 /****************************************************************************
1228 * Ogg_EndOfStream: clean up the ES when an End of Stream is detected.
1229 ****************************************************************************/
1230 static void Ogg_EndOfStream( demux_t
*p_demux
)
1232 demux_sys_t
*p_ogg
= p_demux
->p_sys
;
1235 #define p_stream p_ogg->pp_stream[i_stream]
1236 for( i_stream
= 0 ; i_stream
< p_ogg
->i_streams
; i_stream
++ )
1238 if( p_stream
->p_es
)
1239 es_out_Del( p_demux
->out
, p_stream
->p_es
);
1241 p_ogg
->i_bitrate
-= p_stream
->fmt
.i_bitrate
;
1243 ogg_stream_clear( &p_ogg
->pp_stream
[i_stream
]->os
);
1244 free( p_ogg
->pp_stream
[i_stream
]->p_headers
);
1246 es_format_Clean( &p_stream
->fmt
);
1248 free( p_ogg
->pp_stream
[i_stream
] );
1253 free( p_ogg
->pp_stream
);
1254 p_ogg
->pp_stream
= NULL
;
1255 p_ogg
->i_streams
= 0;
1258 static void Ogg_ReadTheoraHeader( logical_stream_t
*p_stream
,
1259 ogg_packet
*p_oggpacket
)
1262 int i_fps_numerator
;
1263 int i_fps_denominator
;
1264 int i_keyframe_frequency_force
;
1266 p_stream
->fmt
.i_cat
= VIDEO_ES
;
1267 p_stream
->fmt
.i_codec
= VLC_FOURCC( 't','h','e','o' );
1269 /* Signal that we want to keep a backup of the theora
1270 * stream headers. They will be used when switching between
1272 p_stream
->b_force_backup
= 1;
1274 /* Cheat and get additionnal info ;) */
1275 bs_init( &bitstream
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
1276 bs_skip( &bitstream
, 56 );
1277 bs_read( &bitstream
, 8 ); /* major version num */
1278 bs_read( &bitstream
, 8 ); /* minor version num */
1279 bs_read( &bitstream
, 8 ); /* subminor version num */
1280 bs_read( &bitstream
, 16 ) /*<< 4*/; /* width */
1281 bs_read( &bitstream
, 16 ) /*<< 4*/; /* height */
1282 bs_read( &bitstream
, 24 ); /* frame width */
1283 bs_read( &bitstream
, 24 ); /* frame height */
1284 bs_read( &bitstream
, 8 ); /* x offset */
1285 bs_read( &bitstream
, 8 ); /* y offset */
1287 i_fps_numerator
= bs_read( &bitstream
, 32 );
1288 i_fps_denominator
= bs_read( &bitstream
, 32 );
1289 bs_read( &bitstream
, 24 ); /* aspect_numerator */
1290 bs_read( &bitstream
, 24 ); /* aspect_denominator */
1292 p_stream
->fmt
.video
.i_frame_rate
= i_fps_numerator
;
1293 p_stream
->fmt
.video
.i_frame_rate_base
= i_fps_denominator
;
1295 bs_read( &bitstream
, 8 ); /* colorspace */
1296 p_stream
->fmt
.i_bitrate
= bs_read( &bitstream
, 24 );
1297 bs_read( &bitstream
, 6 ); /* quality */
1299 i_keyframe_frequency_force
= 1 << bs_read( &bitstream
, 5 );
1301 /* granule_shift = i_log( frequency_force -1 ) */
1302 p_stream
->i_granule_shift
= 0;
1303 i_keyframe_frequency_force
--;
1304 while( i_keyframe_frequency_force
)
1306 p_stream
->i_granule_shift
++;
1307 i_keyframe_frequency_force
>>= 1;
1310 p_stream
->f_rate
= ((float)i_fps_numerator
) / i_fps_denominator
;
1313 static void Ogg_ReadVorbisHeader( logical_stream_t
*p_stream
,
1314 ogg_packet
*p_oggpacket
)
1318 p_stream
->fmt
.i_cat
= AUDIO_ES
;
1319 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'v','o','r','b' );
1321 /* Signal that we want to keep a backup of the vorbis
1322 * stream headers. They will be used when switching between
1324 p_stream
->b_force_backup
= 1;
1326 /* Cheat and get additionnal info ;) */
1327 oggpack_readinit( &opb
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
1328 oggpack_adv( &opb
, 88 );
1329 p_stream
->fmt
.audio
.i_channels
= oggpack_read( &opb
, 8 );
1330 p_stream
->f_rate
= p_stream
->fmt
.audio
.i_rate
=
1331 oggpack_read( &opb
, 32 );
1332 oggpack_adv( &opb
, 32 );
1333 p_stream
->fmt
.i_bitrate
= oggpack_read( &opb
, 32 );
1336 static void Ogg_ReadSpeexHeader( logical_stream_t
*p_stream
,
1337 ogg_packet
*p_oggpacket
)
1341 p_stream
->fmt
.i_cat
= AUDIO_ES
;
1342 p_stream
->fmt
.i_codec
= VLC_FOURCC( 's','p','x',' ' );
1344 /* Signal that we want to keep a backup of the speex
1345 * stream headers. They will be used when switching between
1347 p_stream
->b_force_backup
= 1;
1349 /* Cheat and get additionnal info ;) */
1350 oggpack_readinit( &opb
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
1351 oggpack_adv( &opb
, 224 );
1352 oggpack_adv( &opb
, 32 ); /* speex_version_id */
1353 oggpack_adv( &opb
, 32 ); /* header_size */
1354 p_stream
->f_rate
= p_stream
->fmt
.audio
.i_rate
= oggpack_read( &opb
, 32 );
1355 oggpack_adv( &opb
, 32 ); /* mode */
1356 oggpack_adv( &opb
, 32 ); /* mode_bitstream_version */
1357 p_stream
->fmt
.audio
.i_channels
= oggpack_read( &opb
, 32 );
1358 p_stream
->fmt
.i_bitrate
= oggpack_read( &opb
, 32 );
1361 static void Ogg_ReadFlacHeader( demux_t
*p_demux
, logical_stream_t
*p_stream
,
1362 ogg_packet
*p_oggpacket
)
1364 /* Parse the STREAMINFO metadata */
1367 bs_init( &s
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
1370 if( bs_read( &s
, 7 ) == 0 )
1372 if( bs_read( &s
, 24 ) >= 34 /*size STREAMINFO*/ )
1375 p_stream
->f_rate
= p_stream
->fmt
.audio
.i_rate
= bs_read( &s
, 20 );
1376 p_stream
->fmt
.audio
.i_channels
= bs_read( &s
, 3 ) + 1;
1378 msg_Dbg( p_demux
, "FLAC header, channels: %i, rate: %i",
1379 p_stream
->fmt
.audio
.i_channels
, (int)p_stream
->f_rate
);
1381 else msg_Dbg( p_demux
, "FLAC STREAMINFO metadata too short" );
1383 /* Fake this as the last metadata block */
1384 *((uint8_t*)p_oggpacket
->packet
) |= 0x80;
1388 /* This ain't a STREAMINFO metadata */
1389 msg_Dbg( p_demux
, "Invalid FLAC STREAMINFO metadata" );
1393 static void Ogg_ReadKateHeader( logical_stream_t
*p_stream
,
1394 ogg_packet
*p_oggpacket
)
1401 p_stream
->fmt
.i_cat
= SPU_ES
;
1402 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'k','a','t','e' );
1404 /* Signal that we want to keep a backup of the kate
1405 * stream headers. They will be used when switching between
1407 p_stream
->b_force_backup
= 1;
1409 /* Cheat and get additionnal info ;) */
1410 oggpack_readinit( &opb
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
1411 oggpack_adv( &opb
, 11*8 ); /* packet type, kate magic, version */
1412 p_stream
->i_kate_num_headers
= oggpack_read( &opb
, 8 );
1413 oggpack_adv( &opb
, 3*8 );
1414 p_stream
->i_granule_shift
= oggpack_read( &opb
, 8 );
1415 oggpack_adv( &opb
, 8*8 ); /* reserved */
1416 gnum
= oggpack_read( &opb
, 32 );
1417 gden
= oggpack_read( &opb
, 32 );
1418 p_stream
->f_rate
= (double)gnum
/gden
;
1420 p_stream
->fmt
.psz_language
= malloc(16);
1421 if (p_stream
->fmt
.psz_language
)
1424 p_stream
->fmt
.psz_language
[n
] = oggpack_read(&opb
,8);
1425 p_stream
->fmt
.psz_language
[15] = 0; /* just in case */
1430 oggpack_read(&opb
,8);
1432 p_stream
->fmt
.psz_description
= malloc(16);
1433 if (p_stream
->fmt
.psz_description
)
1436 p_stream
->fmt
.psz_description
[n
] = oggpack_read(&opb
,8);
1437 p_stream
->fmt
.psz_description
[15] = 0; /* just in case */
1442 oggpack_read(&opb
,8);
1446 static void Ogg_ReadAnnodexHeader( vlc_object_t
*p_this
,
1447 logical_stream_t
*p_stream
,
1448 ogg_packet
*p_oggpacket
)
1450 if( p_oggpacket
->bytes
>= 28 &&
1451 !memcmp( p_oggpacket
->packet
, "Annodex", 7 ) )
1455 uint16_t major_version
;
1456 uint16_t minor_version
;
1457 uint64_t timebase_numerator
;
1458 uint64_t timebase_denominator
;
1460 Ogg_ReadTheoraHeader( p_stream
, p_oggpacket
);
1462 oggpack_readinit( &opb
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
1463 oggpack_adv( &opb
, 8*8 ); /* "Annodex\0" header */
1464 major_version
= oggpack_read( &opb
, 2*8 ); /* major version */
1465 minor_version
= oggpack_read( &opb
, 2*8 ); /* minor version */
1466 timebase_numerator
= GetQWLE( &p_oggpacket
->packet
[16] );
1467 timebase_denominator
= GetQWLE( &p_oggpacket
->packet
[24] );
1469 else if( p_oggpacket
->bytes
>= 42 &&
1470 !memcmp( p_oggpacket
->packet
, "AnxData", 7 ) )
1472 uint64_t granule_rate_numerator
;
1473 uint64_t granule_rate_denominator
;
1474 char content_type_string
[1024];
1476 /* Read in Annodex header fields */
1478 granule_rate_numerator
= GetQWLE( &p_oggpacket
->packet
[8] );
1479 granule_rate_denominator
= GetQWLE( &p_oggpacket
->packet
[16] );
1480 p_stream
->secondary_header_packets
=
1481 GetDWLE( &p_oggpacket
->packet
[24] );
1483 /* we are guaranteed that the first header field will be
1484 * the content-type (by the Annodex standard) */
1485 content_type_string
[0] = '\0';
1486 if( !strncasecmp( (char*)(&p_oggpacket
->packet
[28]), "Content-Type: ", 14 ) )
1488 uint8_t *p
= memchr( &p_oggpacket
->packet
[42], '\r',
1489 p_oggpacket
->bytes
- 1 );
1490 if( p
&& p
[0] == '\r' && p
[1] == '\n' )
1491 sscanf( (char*)(&p_oggpacket
->packet
[42]), "%1024s\r\n",
1492 content_type_string
);
1495 msg_Dbg( p_this
, "AnxData packet info: %"PRId64
" / %"PRId64
", %d, ``%s''",
1496 granule_rate_numerator
, granule_rate_denominator
,
1497 p_stream
->secondary_header_packets
, content_type_string
);
1499 p_stream
->f_rate
= (float) granule_rate_numerator
/
1500 (float) granule_rate_denominator
;
1502 /* What type of file do we have?
1503 * strcmp is safe to use here because we've extracted
1504 * content_type_string from the stream manually */
1505 if( !strncmp(content_type_string
, "audio/x-wav", 11) )
1507 /* n.b. WAVs are unsupported right now */
1508 p_stream
->fmt
.i_cat
= UNKNOWN_ES
;
1510 else if( !strncmp(content_type_string
, "audio/x-vorbis", 14) )
1512 p_stream
->fmt
.i_cat
= AUDIO_ES
;
1513 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'v','o','r','b' );
1515 p_stream
->b_force_backup
= 1;
1517 else if( !strncmp(content_type_string
, "audio/x-speex", 14) )
1519 p_stream
->fmt
.i_cat
= AUDIO_ES
;
1520 p_stream
->fmt
.i_codec
= VLC_FOURCC( 's','p','x',' ' );
1522 p_stream
->b_force_backup
= 1;
1524 else if( !strncmp(content_type_string
, "video/x-theora", 14) )
1526 p_stream
->fmt
.i_cat
= VIDEO_ES
;
1527 p_stream
->fmt
.i_codec
= VLC_FOURCC( 't','h','e','o' );
1529 p_stream
->b_force_backup
= 1;
1531 else if( !strncmp(content_type_string
, "video/x-xvid", 14) )
1533 p_stream
->fmt
.i_cat
= VIDEO_ES
;
1534 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'x','v','i','d' );
1536 p_stream
->b_force_backup
= 1;
1538 else if( !strncmp(content_type_string
, "video/mpeg", 14) )
1540 /* n.b. MPEG streams are unsupported right now */
1541 p_stream
->fmt
.i_cat
= VIDEO_ES
;
1542 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'm','p','g','v' );
1544 else if( !strncmp(content_type_string
, "text/x-cmml", 11) )
1546 ogg_stream_packetout( &p_stream
->os
, p_oggpacket
);
1547 p_stream
->fmt
.i_cat
= SPU_ES
;
1548 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'c','m','m','l' );
1553 /* every packet[1] in the stream contains a picture, there may
1554 * be header cruft infront of it though
1555 * [1] EXCEPT the BOS page/packet */
1556 static uint32_t Ogg_ReadDiracPictureNumber( ogg_packet
*p_oggpacket
)
1559 /* protect against falling off the edge */
1560 while ( u_pos
+ 13 < p_oggpacket
->bytes
) {
1561 /* find the picture startcode */
1562 if ( p_oggpacket
->packet
[u_pos
] & 0x08 ) {
1563 return GetDWBE( p_oggpacket
->packet
+ u_pos
+ 9 );
1565 /* skip to the next dirac parse unit */
1566 uint32_t u_npo
= GetDWBE( p_oggpacket
->packet
+ u_pos
+ 1 );
1574 static uint32_t dirac_uint( bs_t
*p_bs
)
1576 uint32_t count
= 0, value
= 0;
1577 while( !bs_read ( p_bs
, 1 ) ) {
1580 value
|= bs_read ( p_bs
, 1 );
1583 return (1<<count
) - 1 + value
;
1586 static int dirac_bool( bs_t
*p_bs
)
1588 return bs_read ( p_bs
, 1 );
1591 static void Ogg_ReadDiracHeader( logical_stream_t
*p_stream
,
1592 ogg_packet
*p_oggpacket
)
1596 p_stream
->fmt
.i_cat
= VIDEO_ES
;
1597 p_stream
->fmt
.i_codec
= VLC_FOURCC( 'd','r','a','c' );
1598 p_stream
->i_granule_shift
= 32;
1600 /* Backing up stream headers is not required -- seqhdrs are repeated
1601 * thoughout the stream at suitable decoding start points */
1602 p_stream
->b_force_backup
= 0;
1604 /* read in useful bits from sequence header */
1605 bs_init( &bs
, p_oggpacket
->packet
, p_oggpacket
->bytes
);
1606 bs_skip( &bs
, 13*8); /* parse_info_header */
1607 dirac_uint( &bs
); /* major_version */
1608 dirac_uint( &bs
); /* minor_version */
1609 dirac_uint( &bs
); /* profile */
1610 dirac_uint( &bs
); /* level */
1612 uint32_t u_video_format
= dirac_uint( &bs
); /* index */
1614 if (dirac_bool( &bs
)) {
1615 dirac_uint( &bs
); /* frame_width */
1616 dirac_uint( &bs
); /* frame_height */
1619 if (dirac_bool( &bs
)) {
1620 dirac_uint( &bs
); /* chroma_format */
1622 if (dirac_bool( &bs
)) {
1623 if (dirac_bool( &bs
)) { /* interlaced */
1624 dirac_bool( &bs
); /* top_field_first */
1628 static const struct {
1629 uint32_t u_n
/* numerator */, u_d
/* denominator */;
1630 } dirac_frate_tbl
[] = { /* table 10.3 */
1631 {1,1}, /* this first value is never used */
1632 {24000,1001}, {24,1}, {25,1}, {30000,1001}, {30,1},
1633 {50,1}, {60000,1001}, {60,1}, {15000,1001}, {25,2},
1636 static const uint32_t dirac_vidfmt_frate
[] = { /* table C.1 */
1637 1, 9, 10, 9, 10, 9, 10, 4, 3, 7, 6, 4, 3, 7, 6, 2, 2, 7, 6, 7, 6,
1640 uint32_t u_n
= dirac_frate_tbl
[dirac_vidfmt_frate
[u_video_format
]].u_n
;
1641 uint32_t u_d
= dirac_frate_tbl
[dirac_vidfmt_frate
[u_video_format
]].u_d
;
1642 if (dirac_bool( &bs
)) {
1643 uint32_t frame_rate_index
= dirac_uint( &bs
);
1644 u_n
= dirac_frate_tbl
[frame_rate_index
].u_n
;
1645 u_d
= dirac_frate_tbl
[frame_rate_index
].u_d
;
1646 if (frame_rate_index
== 0) {
1647 u_n
= dirac_uint( &bs
); /* frame_rate_numerator */
1648 u_d
= dirac_uint( &bs
); /* frame_rate_denominator */
1651 p_stream
->f_rate
= (float) u_n
/ u_d
;