1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
5 * $Id: vorbis.c,v 1.25 2003/11/23 15:50:07 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/decoder.h>
32 #ifdef MODULE_NAME_IS_tremor
33 #include <tremor/ivorbiscodec.h>
36 #include <vorbis/codec.h>
39 #ifdef HAVE_VORBIS_VORBISENC_H
40 # include <vorbis/vorbisenc.h>
41 # ifndef OV_ECTL_RATEMANAGE_AVG
42 # define OV_ECTL_RATEMANAGE_AVG 0x0
48 /*****************************************************************************
49 * decoder_sys_t : vorbis decoder descriptor
50 *****************************************************************************/
54 vlc_bool_t b_packetizer;
64 vorbis_info vi; /* struct that stores all the static vorbis bitstream
66 vorbis_comment vc; /* struct that stores all the bitstream user
68 vorbis_dsp_state vd; /* central working state for the packet->PCM
70 vorbis_block vb; /* local working space for packet->PCM decode */
75 audio_date_t end_date;
76 int i_last_block_size;
80 static int pi_channels_maps[6] =
83 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
84 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
85 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
86 | AOUT_CHAN_REARRIGHT,
87 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
88 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
91 /****************************************************************************
93 ****************************************************************************/
94 static int OpenDecoder ( vlc_object_t * );
95 static int OpenPacketizer( vlc_object_t * );
96 static void CloseDecoder ( vlc_object_t * );
97 static void *DecodeBlock ( decoder_t *, block_t ** );
99 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
101 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
102 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
104 static void ParseVorbisComments( decoder_t * );
106 #ifdef MODULE_NAME_IS_tremor
107 static void Interleave ( int32_t *, const int32_t **, int, int );
109 static void Interleave ( float *, const float **, int, int );
112 #ifndef MODULE_NAME_IS_tremor
113 static int OpenEncoder ( vlc_object_t * );
114 static void CloseEncoder ( vlc_object_t * );
115 static block_t *Headers ( encoder_t * );
116 static block_t *Encode ( encoder_t *, aout_buffer_t * );
119 /*****************************************************************************
121 *****************************************************************************/
124 set_description( _("Vorbis audio decoder") );
125 #ifdef MODULE_NAME_IS_tremor
126 set_capability( "decoder", 90 );
128 set_capability( "decoder", 100 );
130 set_callbacks( OpenDecoder, CloseDecoder );
133 set_description( _("Vorbis audio packetizer") );
134 set_capability( "packetizer", 100 );
135 set_callbacks( OpenPacketizer, CloseDecoder );
137 #ifndef MODULE_NAME_IS_tremor
139 set_description( _("Vorbis audio encoder") );
140 set_capability( "encoder", 100 );
141 set_callbacks( OpenEncoder, CloseEncoder );
146 /*****************************************************************************
147 * OpenDecoder: probe the decoder and return score
148 *****************************************************************************/
149 static int OpenDecoder( vlc_object_t *p_this )
151 decoder_t *p_dec = (decoder_t*)p_this;
152 decoder_sys_t *p_sys;
154 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
159 /* Allocate the memory needed to store the decoder's structure */
160 if( ( p_dec->p_sys = p_sys =
161 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
163 msg_Err( p_dec, "out of memory" );
168 aout_DateSet( &p_sys->end_date, 0 );
169 p_sys->b_packetizer = VLC_FALSE;
170 p_sys->i_headers = 0;
172 /* Take care of vorbis init */
173 vorbis_info_init( &p_sys->vi );
174 vorbis_comment_init( &p_sys->vc );
176 /* Set output properties */
177 p_dec->fmt_out.i_cat = AUDIO_ES;
178 #ifdef MODULE_NAME_IS_tremor
179 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
181 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
185 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
187 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
193 static int OpenPacketizer( vlc_object_t *p_this )
195 decoder_t *p_dec = (decoder_t*)p_this;
197 int i_ret = OpenDecoder( p_this );
199 if( i_ret == VLC_SUCCESS )
201 p_dec->p_sys->b_packetizer = VLC_TRUE;
202 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
208 /****************************************************************************
209 * DecodeBlock: the whole thing
210 ****************************************************************************
211 * This function must be fed with ogg packets.
212 ****************************************************************************/
213 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
215 decoder_sys_t *p_sys = p_dec->p_sys;
216 ogg_packet oggpacket;
218 if( !pp_block ) return NULL;
222 /* Block to Ogg packet */
223 oggpacket.packet = (*pp_block)->p_buffer;
224 oggpacket.bytes = (*pp_block)->i_buffer;
228 if( p_sys->b_packetizer ) return NULL;
230 /* Block to Ogg packet */
231 oggpacket.packet = NULL;
235 oggpacket.granulepos = -1;
238 oggpacket.packetno = 0;
240 if( p_sys->i_headers == 0 )
242 /* Take care of the initial Vorbis header */
244 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
245 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc,
248 msg_Err( p_dec, "This bitstream does not contain Vorbis "
250 block_Release( *pp_block );
255 /* Setup the format */
256 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
257 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
258 p_dec->fmt_out.audio.i_physical_channels =
259 p_dec->fmt_out.audio.i_original_channels =
260 pi_channels_maps[p_sys->vi.channels];
261 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
263 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
265 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
266 p_sys->vi.channels, p_sys->vi.rate,
267 p_sys->vi.bitrate_nominal );
269 return ProcessPacket( p_dec, &oggpacket, pp_block );
272 if( p_sys->i_headers == 1 )
274 /* The next packet in order is the comments header */
275 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
278 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
279 block_Release( *pp_block );
284 ParseVorbisComments( p_dec );
286 return ProcessPacket( p_dec, &oggpacket, pp_block );
289 if( p_sys->i_headers == 2 )
291 /* The next packet in order is the codebooks header
292 We need to watch out that this packet is not missing as a
293 missing or corrupted header is fatal. */
294 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
297 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
298 block_Release( *pp_block );
303 if( !p_sys->b_packetizer )
305 /* Initialize the Vorbis packet->PCM decoder */
306 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
307 vorbis_block_init( &p_sys->vd, &p_sys->vb );
310 return ProcessPacket( p_dec, &oggpacket, pp_block );
313 return ProcessPacket( p_dec, &oggpacket, pp_block );
316 /*****************************************************************************
317 * ProcessPacket: processes a Vorbis packet.
318 *****************************************************************************/
319 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
322 decoder_sys_t *p_sys = p_dec->p_sys;
323 block_t *p_block = *pp_block;
325 /* Date management */
326 if( p_block && p_block->i_pts > 0 &&
327 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
329 aout_DateSet( &p_sys->end_date, p_block->i_pts );
332 if( !aout_DateGet( &p_sys->end_date ) )
334 /* We've just started the stream, wait for the first PTS. */
335 if( p_block ) block_Release( p_block );
339 *pp_block = NULL; /* To avoid being fed the same packet again */
341 if( p_sys->b_packetizer )
343 return SendPacket( p_dec, p_oggpacket, p_block );
347 aout_buffer_t *p_aout_buffer;
349 if( p_sys->i_headers >= 3 )
350 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
352 p_aout_buffer = NULL;
356 block_Release( p_block );
358 return p_aout_buffer;
362 /*****************************************************************************
363 * DecodePacket: decodes a Vorbis packet.
364 *****************************************************************************/
365 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
367 decoder_sys_t *p_sys = p_dec->p_sys;
370 #ifdef MODULE_NAME_IS_tremor
376 if( p_oggpacket->bytes &&
377 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
378 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
380 /* **pp_pcm is a multichannel float vector. In stereo, for
381 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
382 * the size of each channel. Convert the float values
383 * (-1.<=range<=1.) to whatever PCM format and write it out */
385 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
388 aout_buffer_t *p_aout_buffer;
391 p_dec->pf_aout_buffer_new( p_dec, i_samples );
393 if( p_aout_buffer == NULL ) return NULL;
395 /* Interleave the samples */
396 #ifdef MODULE_NAME_IS_tremor
397 Interleave( (int32_t *)p_aout_buffer->p_buffer,
398 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
400 Interleave( (float *)p_aout_buffer->p_buffer,
401 (const float **)pp_pcm, p_sys->vi.channels, i_samples );
404 /* Tell libvorbis how many samples we actually consumed */
405 vorbis_synthesis_read( &p_sys->vd, i_samples );
407 /* Date management */
408 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
409 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
411 return p_aout_buffer;
419 /*****************************************************************************
420 * SendPacket: send an ogg dated packet to the stream output.
421 *****************************************************************************/
422 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
425 decoder_sys_t *p_sys = p_dec->p_sys;
426 int i_block_size, i_samples;
428 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
429 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
430 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
431 p_sys->i_last_block_size = i_block_size;
433 /* Date management */
434 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
436 if( p_sys->i_headers >= 3 )
437 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
440 p_block->i_length = 0;
445 /*****************************************************************************
446 * ParseVorbisComments: FIXME should be done in demuxer
447 *****************************************************************************/
448 static void ParseVorbisComments( decoder_t *p_dec )
450 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
451 input_info_category_t *p_cat =
452 input_InfoCategory( p_input, _("Vorbis Comment") );
454 char *psz_name, *psz_value, *psz_comment;
455 while ( i < p_dec->p_sys->vc.comments )
457 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
460 msg_Warn( p_dec, "Out of memory" );
463 psz_name = psz_comment;
464 psz_value = strchr( psz_comment, '=' );
469 input_AddInfo( p_cat, psz_name, psz_value );
476 /*****************************************************************************
477 * Interleave: helper function to interleave channels
478 *****************************************************************************/
479 #ifdef MODULE_NAME_IS_tremor
480 static void Interleave( int32_t *p_out, const int32_t **pp_in,
482 static void Interleave( float *p_out, const float **pp_in,
484 int i_nb_channels, int i_samples )
488 for ( j = 0; j < i_samples; j++ )
490 for ( i = 0; i < i_nb_channels; i++ )
492 p_out[j * i_nb_channels + i] = pp_in[i][j];
497 /*****************************************************************************
498 * CloseDecoder: vorbis decoder destruction
499 *****************************************************************************/
500 static void CloseDecoder( vlc_object_t *p_this )
502 decoder_t *p_dec = (decoder_t *)p_this;
503 decoder_sys_t *p_sys = p_dec->p_sys;
505 if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
507 vorbis_block_clear( &p_sys->vb );
508 vorbis_dsp_clear( &p_sys->vd );
511 vorbis_comment_clear( &p_sys->vc );
512 vorbis_info_clear( &p_sys->vi ); /* must be called last */
517 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
519 /*****************************************************************************
520 * encoder_sys_t : theora encoder descriptor
521 *****************************************************************************/
532 vorbis_info vi; /* struct that stores all the static vorbis bitstream
534 vorbis_comment vc; /* struct that stores all the bitstream user
536 vorbis_dsp_state vd; /* central working state for the packet->PCM
538 vorbis_block vb; /* local working space for packet->PCM decode */
540 int i_last_block_size;
550 /*****************************************************************************
551 * OpenEncoder: probe the encoder and return score
552 *****************************************************************************/
553 static int OpenEncoder( vlc_object_t *p_this )
555 encoder_t *p_enc = (encoder_t *)p_this;
556 encoder_sys_t *p_sys;
558 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') )
563 /* Allocate the memory needed to store the decoder's structure */
564 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
566 msg_Err( p_enc, "out of memory" );
569 p_enc->p_sys = p_sys;
571 p_enc->pf_header = Headers;
572 p_enc->pf_encode_audio = Encode;
573 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
575 /* Initialize vorbis encoder */
576 vorbis_info_init( &p_sys->vi );
578 if( vorbis_encode_setup_managed( &p_sys->vi,
579 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
580 -1, p_enc->fmt_out.i_bitrate, -1 ) ||
581 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
582 vorbis_encode_setup_init( &p_sys->vi ) ){}
585 vorbis_comment_init( &p_sys->vc);
586 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
588 /* set up the analysis state and auxiliary encoding storage */
589 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
590 vorbis_block_init( &p_sys->vd, &p_sys->vb );
592 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
593 p_sys->i_last_block_size = 0;
594 p_sys->i_samples_delay = 0;
595 p_sys->i_headers = 0;
601 /****************************************************************************
602 * Encode: the whole thing
603 ****************************************************************************
604 * This function spits out ogg packets.
605 ****************************************************************************/
606 static block_t *Headers( encoder_t *p_enc )
608 encoder_sys_t *p_sys = p_enc->p_sys;
609 block_t *p_block, *p_chain = NULL;
611 /* Create theora headers */
612 if( !p_sys->i_headers )
614 ogg_packet header[3];
617 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
618 &header[0], &header[1], &header[2]);
619 for( i = 0; i < 3; i++ )
621 p_block = block_New( p_enc, header[i].bytes );
622 memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
624 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
626 block_ChainAppend( &p_chain, p_block );
628 p_sys->i_headers = 3;
634 /****************************************************************************
635 * Encode: the whole thing
636 ****************************************************************************
637 * This function spits out ogg packets.
638 ****************************************************************************/
639 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
641 encoder_sys_t *p_sys = p_enc->p_sys;
642 ogg_packet oggpacket;
643 block_t *p_block, *p_chain = NULL;
647 p_sys->i_pts = p_aout_buf->start_date -
648 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
649 (mtime_t)p_enc->fmt_in.audio.i_rate;
651 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
653 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
655 /* convert samples to float and uninterleave */
656 for( i = 0; i < p_sys->i_channels; i++ )
658 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
660 buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
661 [j * p_sys->i_channels + i ] )) / 32768.f;
665 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
667 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
671 vorbis_analysis( &p_sys->vb, NULL );
672 vorbis_bitrate_addblock( &p_sys->vb );
674 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
677 p_block = block_New( p_enc, oggpacket.bytes );
678 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
680 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
682 if( i_block_size < 0 ) i_block_size = 0;
683 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
684 p_sys->i_last_block_size = i_block_size;
686 p_block->i_length = (mtime_t)1000000 *
687 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
689 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
691 p_sys->i_samples_delay -= i_samples;
694 p_sys->i_pts += p_block->i_length;
695 block_ChainAppend( &p_chain, p_block );
702 /*****************************************************************************
703 * CloseEncoder: theora encoder destruction
704 *****************************************************************************/
705 static void CloseEncoder( vlc_object_t *p_this )
707 encoder_t *p_enc = (encoder_t *)p_this;
708 encoder_sys_t *p_sys = p_enc->p_sys;
710 vorbis_block_clear( &p_sys->vb );
711 vorbis_dsp_clear( &p_sys->vd );
712 vorbis_comment_clear( &p_sys->vc );
713 vorbis_info_clear( &p_sys->vi ); /* must be called last */
718 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */