1 /*****************************************************************************
2 * vorbis.c: vorbis decoder module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: vorbis.c,v 1.20 2003/10/27 01:04:38 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
28 #include <string.h> /* memcpy(), memset() */
32 #include <vlc/decoder.h>
33 #include <vlc/input.h>
35 #include <input_ext-dec.h>
37 #include <vlc/input.h>
41 #ifdef MODULE_NAME_IS_tremor
42 #include <tremor/ivorbiscodec.h>
45 #include <vorbis/codec.h>
48 #ifdef HAVE_VORBIS_VORBISENC_H
49 # include <vorbis/vorbisenc.h>
50 # ifndef OV_ECTL_RATEMANAGE_AVG
51 # define OV_ECTL_RATEMANAGE_AVG 0x0
57 /*****************************************************************************
58 * decoder_sys_t : vorbis decoder descriptor
59 *****************************************************************************/
63 vlc_bool_t b_packetizer;
73 vorbis_info vi; /* struct that stores all the static vorbis bitstream
75 vorbis_comment vc; /* struct that stores all the bitstream user
77 vorbis_dsp_state vd; /* central working state for the packet->PCM
79 vorbis_block vb; /* local working space for packet->PCM decode */
84 aout_instance_t *p_aout;
85 aout_input_t *p_aout_input;
86 audio_sample_format_t aout_format;
89 * Packetizer output properties
91 sout_packetizer_input_t *p_sout_input;
92 sout_format_t sout_format;
97 audio_date_t end_date;
98 int i_last_block_size;
102 static int pi_channels_maps[6] =
105 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
106 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
107 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
108 | AOUT_CHAN_REARRIGHT,
109 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
110 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
113 /****************************************************************************
115 ****************************************************************************/
116 static int OpenDecoder ( vlc_object_t * );
117 static int OpenPacketizer( vlc_object_t * );
119 static int InitDecoder ( decoder_t * );
120 static int RunDecoder ( decoder_t *, block_t * );
121 static int EndDecoder ( decoder_t * );
123 static int ProcessPacket ( decoder_t *, ogg_packet *, mtime_t );
124 static int DecodePacket ( decoder_t *, ogg_packet * );
125 static int SendPacket ( decoder_t *, ogg_packet * );
127 static void ParseVorbisComments( decoder_t * );
129 #ifdef MODULE_NAME_IS_tremor
130 static void Interleave ( int32_t *, const int32_t **, int, int );
132 static void Interleave ( float *, const float **, int, int );
135 #ifndef MODULE_NAME_IS_tremor
136 static int OpenEncoder ( vlc_object_t * );
137 static void CloseEncoder ( vlc_object_t * );
138 static block_t *Headers ( encoder_t * );
139 static block_t *Encode ( encoder_t *, aout_buffer_t * );
142 /*****************************************************************************
144 *****************************************************************************/
147 set_description( _("Vorbis audio decoder") );
148 #ifdef MODULE_NAME_IS_tremor
149 set_capability( "decoder", 90 );
151 set_capability( "decoder", 100 );
153 set_callbacks( OpenDecoder, NULL );
156 set_description( _("Vorbis audio packetizer") );
157 set_capability( "packetizer", 100 );
158 set_callbacks( OpenPacketizer, NULL );
160 #ifndef MODULE_NAME_IS_tremor
162 set_description( _("Vorbis audio encoder") );
163 set_capability( "audio encoder", 100 );
164 set_callbacks( OpenEncoder, CloseEncoder );
169 /*****************************************************************************
170 * OpenDecoder: probe the decoder and return score
171 *****************************************************************************/
172 static int OpenDecoder( vlc_object_t *p_this )
174 decoder_t *p_dec = (decoder_t*)p_this;
176 if( p_dec->p_fifo->i_fourcc != VLC_FOURCC('v','o','r','b') )
181 p_dec->pf_init = InitDecoder;
182 p_dec->pf_decode = RunDecoder;
183 p_dec->pf_end = EndDecoder;
185 /* Allocate the memory needed to store the decoder's structure */
187 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
189 msg_Err( p_dec, "out of memory" );
192 p_dec->p_sys->b_packetizer = VLC_FALSE;
197 static int OpenPacketizer( vlc_object_t *p_this )
199 decoder_t *p_dec = (decoder_t*)p_this;
201 int i_ret = OpenDecoder( p_this );
203 if( i_ret == VLC_SUCCESS ) p_dec->p_sys->b_packetizer = VLC_TRUE;
208 /*****************************************************************************
209 * InitDecoder: Initalize the decoder
210 *****************************************************************************/
211 static int InitDecoder( decoder_t *p_dec )
213 decoder_sys_t *p_sys = p_dec->p_sys;
215 aout_DateSet( &p_sys->end_date, 0 );
217 p_sys->p_aout = NULL;
218 p_sys->p_aout_input = NULL;
219 p_sys->aout_format.i_format = VLC_FOURCC('v','o','r','b');
221 p_sys->p_sout_input = NULL;
222 p_sys->sout_format.i_cat = AUDIO_ES;
223 p_sys->sout_format.i_fourcc = VLC_FOURCC( 'v', 'o', 'r', 'b' );
224 p_sys->sout_format.i_block_align = 0;
225 p_sys->sout_format.i_bitrate = 0;
226 p_sys->sout_format.i_extra_data = 0;
227 p_sys->sout_format.p_extra_data = NULL;
229 /* Take care of vorbis init */
230 vorbis_info_init( &p_sys->vi );
231 vorbis_comment_init( &p_sys->vc );
233 p_sys->i_headers = 0;
238 /****************************************************************************
239 * RunDecoder: the whole thing
240 ****************************************************************************
241 * This function must be fed with ogg packets.
242 ****************************************************************************/
243 static int RunDecoder( decoder_t *p_dec, block_t *p_block )
245 decoder_sys_t *p_sys = p_dec->p_sys;
246 ogg_packet oggpacket;
249 /* Block to Ogg packet */
250 oggpacket.packet = p_block->p_buffer;
251 oggpacket.bytes = p_block->i_buffer;
252 oggpacket.granulepos = -1;
255 oggpacket.packetno = 0;
257 if( p_sys->i_headers == 0 )
259 /* Take care of the initial Vorbis header */
261 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
262 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc,
265 msg_Err( p_dec->p_fifo, "This bitstream does not contain Vorbis "
267 block_Release( p_block );
273 if( p_sys->b_packetizer )
275 /* add a input for the stream ouput */
276 p_sys->sout_format.i_sample_rate = p_sys->vi.rate;
277 p_sys->sout_format.i_channels = p_sys->vi.channels;
278 p_sys->sout_format.i_block_align = 1;
279 p_sys->sout_format.i_bitrate = p_sys->vi.bitrate_nominal;
281 p_sys->p_sout_input =
282 sout_InputNew( p_dec, &p_sys->sout_format );
284 if( !p_sys->p_sout_input )
286 msg_Err( p_dec, "cannot add a new stream" );
287 block_Release( p_block );
293 #ifdef MODULE_NAME_IS_tremor
294 p_sys->aout_format.i_format = VLC_FOURCC('f','i','3','2');
296 p_sys->aout_format.i_format = VLC_FOURCC('f','l','3','2');
298 p_sys->aout_format.i_physical_channels =
299 p_sys->aout_format.i_original_channels =
300 pi_channels_maps[p_sys->vi.channels];
301 p_sys->aout_format.i_rate = p_sys->vi.rate;
303 p_sys->p_aout = NULL;
304 p_sys->p_aout_input = aout_DecNew( p_dec, &p_sys->p_aout,
305 &p_sys->aout_format );
307 if( p_sys->p_aout_input == NULL )
309 msg_Err( p_dec, "failed to create aout fifo" );
310 block_Release( p_block );
315 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
317 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
318 p_sys->vi.channels, p_sys->vi.rate,
319 p_sys->vi.bitrate_nominal );
321 if( p_sys->b_packetizer )
323 i_ret = ProcessPacket( p_dec, &oggpacket, p_block->i_pts );
324 block_Release( p_block );
329 block_Release( p_block );
334 if( p_sys->i_headers == 1 )
336 /* The next packet in order is the comments header */
337 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
340 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
345 ParseVorbisComments( p_dec );
347 if( p_sys->b_packetizer )
349 i_ret = ProcessPacket( p_dec, &oggpacket, p_block->i_pts );
350 block_Release( p_block );
355 block_Release( p_block );
360 if( p_sys->i_headers == 2 )
362 /* The next packet in order is the codebooks header
363 We need to watch out that this packet is not missing as a
364 missing or corrupted header is fatal. */
365 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
368 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
373 if( !p_sys->b_packetizer )
375 /* Initialize the Vorbis packet->PCM decoder */
376 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
377 vorbis_block_init( &p_sys->vd, &p_sys->vb );
380 if( p_sys->b_packetizer )
382 i_ret = ProcessPacket( p_dec, &oggpacket, p_block->i_pts );
383 block_Release( p_block );
388 block_Release( p_block );
393 i_ret = ProcessPacket( p_dec, &oggpacket, p_block->i_pts );
394 block_Release( p_block );
398 /*****************************************************************************
399 * ProcessPacket: processes a Vorbis packet.
400 *****************************************************************************/
401 static int ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
404 decoder_sys_t *p_sys = p_dec->p_sys;
406 /* Date management */
407 if( i_pts > 0 && i_pts != aout_DateGet( &p_sys->end_date ) )
409 aout_DateSet( &p_sys->end_date, i_pts );
412 if( !aout_DateGet( &p_sys->end_date ) )
414 /* We've just started the stream, wait for the first PTS. */
418 if( p_sys->b_packetizer )
420 return SendPacket( p_dec, p_oggpacket );
424 return DecodePacket( p_dec, p_oggpacket );
428 /*****************************************************************************
429 * DecodePacket: decodes a Vorbis packet.
430 *****************************************************************************/
431 static int DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
433 decoder_sys_t *p_sys = p_dec->p_sys;
436 #ifdef MODULE_NAME_IS_tremor
442 if( vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
443 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
445 /* **pp_pcm is a multichannel float vector. In stereo, for
446 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
447 * the size of each channel. Convert the float values
448 * (-1.<=range<=1.) to whatever PCM format and write it out */
450 while( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
453 aout_buffer_t *p_aout_buffer;
454 p_aout_buffer = aout_DecNewBuffer( p_sys->p_aout, p_sys->p_aout_input,
458 msg_Err( p_dec, "cannot get aout buffer" );
462 /* Interleave the samples */
463 #ifdef MODULE_NAME_IS_tremor
464 Interleave( (int32_t *)p_aout_buffer->p_buffer,
465 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
467 Interleave( (float *)p_aout_buffer->p_buffer,
468 (const float **)pp_pcm, p_sys->vi.channels, i_samples );
471 /* Tell libvorbis how many samples we actually consumed */
472 vorbis_synthesis_read( &p_sys->vd, i_samples );
474 /* Date management */
475 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
476 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
479 aout_DecPlay( p_sys->p_aout, p_sys->p_aout_input, p_aout_buffer );
484 /*****************************************************************************
485 * SendPacket: send an ogg packet to the stream output.
486 *****************************************************************************/
487 static int SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
489 decoder_sys_t *p_sys = p_dec->p_sys;
490 int i_block_size, i_samples;
492 sout_buffer_t *p_sout_buffer =
493 sout_BufferNew( p_sys->p_sout_input->p_sout, p_oggpacket->bytes );
495 if( !p_sout_buffer ) return VLC_EGENERIC;
497 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
498 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
499 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
500 p_sys->i_last_block_size = i_block_size;
502 p_dec->p_vlc->pf_memcpy( p_sout_buffer->p_buffer,
504 p_oggpacket->bytes );
506 p_sout_buffer->i_bitrate = p_sys->vi.bitrate_nominal;
508 /* Date management */
509 p_sout_buffer->i_dts = p_sout_buffer->i_pts =
510 aout_DateGet( &p_sys->end_date );
512 if( p_sys->i_headers >= 3 )
513 p_sout_buffer->i_length =
514 aout_DateIncrement( &p_sys->end_date, i_samples ) -
515 p_sout_buffer->i_pts;
517 p_sout_buffer->i_length = 0;
519 sout_InputSendBuffer( p_sys->p_sout_input, p_sout_buffer );
524 /*****************************************************************************
525 * ParseVorbisComments: FIXME should be done in demuxer
526 *****************************************************************************/
527 static void ParseVorbisComments( decoder_t *p_dec )
529 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
530 input_info_category_t *p_cat =
531 input_InfoCategory( p_input, _("Vorbis Comment") );
533 char *psz_name, *psz_value, *psz_comment;
534 while ( i < p_dec->p_sys->vc.comments )
536 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
539 msg_Warn( p_dec, "Out of memory" );
542 psz_name = psz_comment;
543 psz_value = strchr( psz_comment, '=' );
548 input_AddInfo( p_cat, psz_name, psz_value );
555 /*****************************************************************************
556 * Interleave: helper function to interleave channels
557 *****************************************************************************/
558 #ifdef MODULE_NAME_IS_tremor
559 static void Interleave( int32_t *p_out, const int32_t **pp_in,
561 static void Interleave( float *p_out, const float **pp_in,
563 int i_nb_channels, int i_samples )
567 for ( j = 0; j < i_samples; j++ )
569 for ( i = 0; i < i_nb_channels; i++ )
571 p_out[j * i_nb_channels + i] = pp_in[i][j];
576 /*****************************************************************************
577 * EndDecoder: vorbis decoder destruction
578 *****************************************************************************/
579 static int EndDecoder( decoder_t * p_dec )
581 decoder_sys_t *p_sys = p_dec->p_sys;
583 if( p_sys->p_aout_input != NULL )
585 aout_DecDelete( p_sys->p_aout, p_sys->p_aout_input );
588 if( p_sys->p_sout_input != NULL )
590 sout_InputDelete( p_sys->p_sout_input );
593 if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
595 vorbis_block_clear( &p_sys->vb );
596 vorbis_dsp_clear( &p_sys->vd );
599 vorbis_comment_clear( &p_sys->vc );
600 vorbis_info_clear( &p_sys->vi ); /* must be called last */
608 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
610 /*****************************************************************************
611 * encoder_sys_t : theora encoder descriptor
612 *****************************************************************************/
623 vorbis_info vi; /* struct that stores all the static vorbis bitstream
625 vorbis_comment vc; /* struct that stores all the bitstream user
627 vorbis_dsp_state vd; /* central working state for the packet->PCM
629 vorbis_block vb; /* local working space for packet->PCM decode */
631 int i_last_block_size;
635 * Packetizer output properties
637 sout_packetizer_input_t *p_sout_input;
638 sout_format_t sout_format;
646 /*****************************************************************************
647 * OpenEncoder: probe the encoder and return score
648 *****************************************************************************/
649 static int OpenEncoder( vlc_object_t *p_this )
651 encoder_t *p_enc = (encoder_t *)p_this;
652 encoder_sys_t *p_sys = p_enc->p_sys;
654 if( p_enc->i_fourcc != VLC_FOURCC('v','o','r','b') )
659 /* Allocate the memory needed to store the decoder's structure */
660 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
662 msg_Err( p_enc, "out of memory" );
665 p_enc->p_sys = p_sys;
667 p_enc->pf_header = Headers;
668 p_enc->pf_encode_audio = Encode;
669 p_enc->format.audio.i_format = VLC_FOURCC('f','l','3','2');
671 /* Initialize vorbis encoder */
672 vorbis_info_init( &p_sys->vi );
674 if( vorbis_encode_setup_managed( &p_sys->vi,
675 aout_FormatNbChannels( &p_enc->format.audio ),
676 p_enc->format.audio.i_rate, -1, p_enc->i_bitrate, -1 ) ||
677 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
678 vorbis_encode_setup_init( &p_sys->vi ) ){}
681 vorbis_comment_init( &p_sys->vc);
682 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
684 /* set up the analysis state and auxiliary encoding storage */
685 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
686 vorbis_block_init( &p_sys->vd, &p_sys->vb );
688 p_sys->i_last_block_size = 0;
689 p_sys->i_samples_delay = 0;
690 p_sys->i_headers = 0;
696 /****************************************************************************
697 * Encode: the whole thing
698 ****************************************************************************
699 * This function spits out ogg packets.
700 ****************************************************************************/
701 static block_t *Headers( encoder_t *p_enc )
703 encoder_sys_t *p_sys = p_enc->p_sys;
704 block_t *p_block, **pp_block = NULL;
706 /* Create theora headers */
707 if( !p_sys->i_headers )
709 ogg_packet header[3];
712 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
713 &header[0], &header[1], &header[2]);
714 for( i = 0; i < 3; i++ )
716 p_block = block_New( p_enc, header[i].bytes );
717 memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
719 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
721 block_ChainAppend( pp_block, p_block );
723 p_sys->i_headers = 3;
731 /****************************************************************************
732 * Encode: the whole thing
733 ****************************************************************************
734 * This function spits out ogg packets.
735 ****************************************************************************/
736 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
738 encoder_sys_t *p_sys = p_enc->p_sys;
739 ogg_packet oggpacket;
740 block_t *p_block, *p_chain = NULL;
744 p_sys->i_pts = p_aout_buf->start_date -
745 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
746 (mtime_t)p_enc->format.audio.i_rate;
748 i_samples = p_aout_buf->i_nb_samples;
749 p_sys->i_samples_delay += i_samples;
751 buffer = vorbis_analysis_buffer( &p_sys->vd, i_samples );
754 if( id->ff_dec_c->channels != id->ff_enc_c->channels )
758 /* dumb downmixing */
759 for( i = 0; i < id->ff_enc_c->frame_size; i++ )
761 for( j = 0 ; j < id->f_dst.i_channels; j++ )
763 p_buffer[i*id->f_dst.i_channels+j] =
764 p_buffer[i*id->f_src.i_channels+j];
769 /* convert samples to float and uninterleave */
770 for( i = 0; i < id->f_dst.i_channels; i++ )
772 for( j = 0 ; j < i_samples ; j++ )
774 buffer[i][j]= ((float)( ((int16_t *)id->p_buffer)
775 [j*id->f_src.i_channels + i ] ))/ 32768.f;
780 vorbis_analysis_wrote( &p_sys->vd, i_samples );
782 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
784 vorbis_analysis( &p_sys->vb, NULL );
785 vorbis_bitrate_addblock( &p_sys->vb );
787 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
790 p_block = block_New( p_enc, oggpacket.bytes );
791 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
793 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
795 if( i_block_size < 0 ) i_block_size = 0;
796 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
797 p_sys->i_last_block_size = i_block_size;
799 p_block->i_length = (mtime_t)1000000 *
800 (mtime_t)i_samples / (mtime_t)p_enc->format.audio.i_rate;
802 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
804 p_sys->i_samples_delay -= i_samples;
807 p_sys->i_pts += p_block->i_length;
808 block_ChainAppend( &p_chain, p_block );
815 /*****************************************************************************
816 * CloseEncoder: theora encoder destruction
817 *****************************************************************************/
818 static void CloseEncoder( vlc_object_t *p_this )
820 encoder_t *p_enc = (encoder_t *)p_this;
821 encoder_sys_t *p_sys = p_enc->p_sys;
823 vorbis_block_clear( &p_sys->vb );
824 vorbis_dsp_clear( &p_sys->vd );
825 vorbis_comment_clear( &p_sys->vc );
826 vorbis_info_clear( &p_sys->vi ); /* must be called last */
831 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */