1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 2001-2003 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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[7] =
84 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
85 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
86 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
87 | AOUT_CHAN_REARRIGHT,
88 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
89 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
90 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
91 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
94 /****************************************************************************
96 ****************************************************************************/
97 static int OpenDecoder ( vlc_object_t * );
98 static int OpenPacketizer( vlc_object_t * );
99 static void CloseDecoder ( vlc_object_t * );
100 static void *DecodeBlock ( decoder_t *, block_t ** );
102 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
104 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
105 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
107 static void ParseVorbisComments( decoder_t * );
109 #ifdef MODULE_NAME_IS_tremor
110 static void Interleave ( int32_t *, const int32_t **, int, int );
112 static void Interleave ( float *, const float **, int, int );
115 #ifndef MODULE_NAME_IS_tremor
116 static int OpenEncoder ( vlc_object_t * );
117 static void CloseEncoder ( vlc_object_t * );
118 static block_t *Headers ( encoder_t * );
119 static block_t *Encode ( encoder_t *, aout_buffer_t * );
122 /*****************************************************************************
124 *****************************************************************************/
127 set_description( _("Vorbis audio decoder") );
128 #ifdef MODULE_NAME_IS_tremor
129 set_capability( "decoder", 90 );
131 set_capability( "decoder", 100 );
133 set_callbacks( OpenDecoder, CloseDecoder );
136 set_description( _("Vorbis audio packetizer") );
137 set_capability( "packetizer", 100 );
138 set_callbacks( OpenPacketizer, CloseDecoder );
140 #ifndef MODULE_NAME_IS_tremor
142 set_description( _("Vorbis audio encoder") );
143 set_capability( "encoder", 100 );
144 set_callbacks( OpenEncoder, CloseEncoder );
149 /*****************************************************************************
150 * OpenDecoder: probe the decoder and return score
151 *****************************************************************************/
152 static int OpenDecoder( vlc_object_t *p_this )
154 decoder_t *p_dec = (decoder_t*)p_this;
155 decoder_sys_t *p_sys;
157 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
162 /* Allocate the memory needed to store the decoder's structure */
163 if( ( p_dec->p_sys = p_sys =
164 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
166 msg_Err( p_dec, "out of memory" );
171 aout_DateSet( &p_sys->end_date, 0 );
172 p_sys->b_packetizer = VLC_FALSE;
173 p_sys->i_headers = 0;
175 /* Take care of vorbis init */
176 vorbis_info_init( &p_sys->vi );
177 vorbis_comment_init( &p_sys->vc );
179 /* Set output properties */
180 p_dec->fmt_out.i_cat = AUDIO_ES;
181 #ifdef MODULE_NAME_IS_tremor
182 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
184 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
188 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
190 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
196 static int OpenPacketizer( vlc_object_t *p_this )
198 decoder_t *p_dec = (decoder_t*)p_this;
200 int i_ret = OpenDecoder( p_this );
202 if( i_ret == VLC_SUCCESS )
204 p_dec->p_sys->b_packetizer = VLC_TRUE;
205 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
211 /****************************************************************************
212 * DecodeBlock: the whole thing
213 ****************************************************************************
214 * This function must be fed with ogg packets.
215 ****************************************************************************/
216 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
218 decoder_sys_t *p_sys = p_dec->p_sys;
219 ogg_packet oggpacket;
221 if( !pp_block ) return NULL;
225 /* Block to Ogg packet */
226 oggpacket.packet = (*pp_block)->p_buffer;
227 oggpacket.bytes = (*pp_block)->i_buffer;
231 if( p_sys->b_packetizer ) return NULL;
233 /* Block to Ogg packet */
234 oggpacket.packet = NULL;
238 oggpacket.granulepos = -1;
241 oggpacket.packetno = 0;
243 if( p_sys->i_headers == 0 )
245 /* Take care of the initial Vorbis header */
247 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
248 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc,
251 msg_Err( p_dec, "this bitstream does not contain Vorbis "
253 block_Release( *pp_block );
258 /* Setup the format */
259 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
260 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
261 p_dec->fmt_out.audio.i_physical_channels =
262 p_dec->fmt_out.audio.i_original_channels =
263 pi_channels_maps[p_sys->vi.channels];
264 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
266 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
268 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
269 p_sys->vi.channels, p_sys->vi.rate,
270 p_sys->vi.bitrate_nominal );
272 return ProcessPacket( p_dec, &oggpacket, pp_block );
275 if( p_sys->i_headers == 1 )
277 /* The next packet in order is the comments header */
278 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
281 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
282 block_Release( *pp_block );
287 ParseVorbisComments( p_dec );
289 return ProcessPacket( p_dec, &oggpacket, pp_block );
292 if( p_sys->i_headers == 2 )
294 /* The next packet in order is the codebooks header
295 We need to watch out that this packet is not missing as a
296 missing or corrupted header is fatal. */
297 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
300 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
301 block_Release( *pp_block );
306 if( !p_sys->b_packetizer )
308 /* Initialize the Vorbis packet->PCM decoder */
309 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
310 vorbis_block_init( &p_sys->vd, &p_sys->vb );
313 return ProcessPacket( p_dec, &oggpacket, pp_block );
316 return ProcessPacket( p_dec, &oggpacket, pp_block );
319 /*****************************************************************************
320 * ProcessPacket: processes a Vorbis packet.
321 *****************************************************************************/
322 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
325 decoder_sys_t *p_sys = p_dec->p_sys;
326 block_t *p_block = *pp_block;
328 /* Date management */
329 if( p_block && p_block->i_pts > 0 &&
330 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
332 aout_DateSet( &p_sys->end_date, p_block->i_pts );
335 if( !aout_DateGet( &p_sys->end_date ) )
337 /* We've just started the stream, wait for the first PTS. */
338 if( p_block ) block_Release( p_block );
342 *pp_block = NULL; /* To avoid being fed the same packet again */
344 if( p_sys->b_packetizer )
346 return SendPacket( p_dec, p_oggpacket, p_block );
350 aout_buffer_t *p_aout_buffer;
352 if( p_sys->i_headers >= 3 )
353 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
355 p_aout_buffer = NULL;
359 block_Release( p_block );
361 return p_aout_buffer;
365 /*****************************************************************************
366 * DecodePacket: decodes a Vorbis packet.
367 *****************************************************************************/
368 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
370 decoder_sys_t *p_sys = p_dec->p_sys;
373 #ifdef MODULE_NAME_IS_tremor
379 if( p_oggpacket->bytes &&
380 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
381 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
383 /* **pp_pcm is a multichannel float vector. In stereo, for
384 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
385 * the size of each channel. Convert the float values
386 * (-1.<=range<=1.) to whatever PCM format and write it out */
388 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
391 aout_buffer_t *p_aout_buffer;
394 p_dec->pf_aout_buffer_new( p_dec, i_samples );
396 if( p_aout_buffer == NULL ) return NULL;
398 /* Interleave the samples */
399 #ifdef MODULE_NAME_IS_tremor
400 Interleave( (int32_t *)p_aout_buffer->p_buffer,
401 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
403 Interleave( (float *)p_aout_buffer->p_buffer,
404 (const float **)pp_pcm, p_sys->vi.channels, i_samples );
407 /* Tell libvorbis how many samples we actually consumed */
408 vorbis_synthesis_read( &p_sys->vd, i_samples );
410 /* Date management */
411 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
412 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
414 return p_aout_buffer;
422 /*****************************************************************************
423 * SendPacket: send an ogg dated packet to the stream output.
424 *****************************************************************************/
425 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
428 decoder_sys_t *p_sys = p_dec->p_sys;
429 int i_block_size, i_samples;
431 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
432 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
433 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
434 p_sys->i_last_block_size = i_block_size;
436 /* Date management */
437 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
439 if( p_sys->i_headers >= 3 )
440 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
443 p_block->i_length = 0;
448 /*****************************************************************************
449 * ParseVorbisComments: FIXME should be done in demuxer
450 *****************************************************************************/
451 static void ParseVorbisComments( decoder_t *p_dec )
453 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
455 char *psz_name, *psz_value, *psz_comment;
456 while ( i < p_dec->p_sys->vc.comments )
458 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
461 msg_Warn( p_dec, "out of memory" );
464 psz_name = psz_comment;
465 psz_value = strchr( psz_comment, '=' );
470 input_Control( p_input, INPUT_ADD_INFO, _("Vorbis comment"),
471 psz_name, psz_value );
478 /*****************************************************************************
479 * Interleave: helper function to interleave channels
480 *****************************************************************************/
481 static void Interleave(
482 #ifdef MODULE_NAME_IS_tremor
483 int32_t *p_out, const int32_t **pp_in,
485 float *p_out, const float **pp_in,
487 int i_nb_channels, int i_samples )
491 for ( j = 0; j < i_samples; j++ )
493 for ( i = 0; i < i_nb_channels; i++ )
495 p_out[j * i_nb_channels + i] = pp_in[i][j];
500 /*****************************************************************************
501 * CloseDecoder: vorbis decoder destruction
502 *****************************************************************************/
503 static void CloseDecoder( vlc_object_t *p_this )
505 decoder_t *p_dec = (decoder_t *)p_this;
506 decoder_sys_t *p_sys = p_dec->p_sys;
508 if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
510 vorbis_block_clear( &p_sys->vb );
511 vorbis_dsp_clear( &p_sys->vd );
514 vorbis_comment_clear( &p_sys->vc );
515 vorbis_info_clear( &p_sys->vi ); /* must be called last */
520 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
522 /*****************************************************************************
523 * encoder_sys_t : theora encoder descriptor
524 *****************************************************************************/
535 vorbis_info vi; /* struct that stores all the static vorbis bitstream
537 vorbis_comment vc; /* struct that stores all the bitstream user
539 vorbis_dsp_state vd; /* central working state for the packet->PCM
541 vorbis_block vb; /* local working space for packet->PCM decode */
543 int i_last_block_size;
553 /*****************************************************************************
554 * OpenEncoder: probe the encoder and return score
555 *****************************************************************************/
556 static int OpenEncoder( vlc_object_t *p_this )
558 encoder_t *p_enc = (encoder_t *)p_this;
559 encoder_sys_t *p_sys;
561 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') )
566 /* Allocate the memory needed to store the decoder's structure */
567 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
569 msg_Err( p_enc, "out of memory" );
572 p_enc->p_sys = p_sys;
574 p_enc->pf_header = Headers;
575 p_enc->pf_encode_audio = Encode;
576 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
578 /* Initialize vorbis encoder */
579 vorbis_info_init( &p_sys->vi );
581 if( vorbis_encode_setup_managed( &p_sys->vi,
582 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
583 -1, p_enc->fmt_out.i_bitrate, -1 ) ||
584 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
585 vorbis_encode_setup_init( &p_sys->vi ) )
591 vorbis_comment_init( &p_sys->vc);
592 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
594 /* set up the analysis state and auxiliary encoding storage */
595 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
596 vorbis_block_init( &p_sys->vd, &p_sys->vb );
598 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
599 p_sys->i_last_block_size = 0;
600 p_sys->i_samples_delay = 0;
601 p_sys->i_headers = 0;
607 /****************************************************************************
608 * Encode: the whole thing
609 ****************************************************************************
610 * This function spits out ogg packets.
611 ****************************************************************************/
612 static block_t *Headers( encoder_t *p_enc )
614 encoder_sys_t *p_sys = p_enc->p_sys;
615 block_t *p_block, *p_chain = NULL;
617 /* Create theora headers */
618 if( !p_sys->i_headers )
620 ogg_packet header[3];
623 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
624 &header[0], &header[1], &header[2]);
625 for( i = 0; i < 3; i++ )
627 p_block = block_New( p_enc, header[i].bytes );
628 memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
630 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
632 block_ChainAppend( &p_chain, p_block );
634 p_sys->i_headers = 3;
640 /****************************************************************************
641 * Encode: the whole thing
642 ****************************************************************************
643 * This function spits out ogg packets.
644 ****************************************************************************/
645 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
647 encoder_sys_t *p_sys = p_enc->p_sys;
648 ogg_packet oggpacket;
649 block_t *p_block, *p_chain = NULL;
654 p_sys->i_pts = p_aout_buf->start_date -
655 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
656 (mtime_t)p_enc->fmt_in.audio.i_rate;
658 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
660 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
662 /* convert samples to float and uninterleave */
663 for( i = 0; i < p_sys->i_channels; i++ )
665 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
667 buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
668 [j * p_sys->i_channels + i ] )) / 32768.f;
672 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
674 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
678 vorbis_analysis( &p_sys->vb, NULL );
679 vorbis_bitrate_addblock( &p_sys->vb );
681 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
684 p_block = block_New( p_enc, oggpacket.bytes );
685 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
687 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
689 if( i_block_size < 0 ) i_block_size = 0;
690 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
691 p_sys->i_last_block_size = i_block_size;
693 p_block->i_length = (mtime_t)1000000 *
694 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
696 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
698 p_sys->i_samples_delay -= i_samples;
701 p_sys->i_pts += p_block->i_length;
702 block_ChainAppend( &p_chain, p_block );
709 /*****************************************************************************
710 * CloseEncoder: theora encoder destruction
711 *****************************************************************************/
712 static void CloseEncoder( vlc_object_t *p_this )
714 encoder_t *p_enc = (encoder_t *)p_this;
715 encoder_sys_t *p_sys = p_enc->p_sys;
717 vorbis_block_clear( &p_sys->vb );
718 vorbis_dsp_clear( &p_sys->vd );
719 vorbis_comment_clear( &p_sys->vc );
720 vorbis_info_clear( &p_sys->vi ); /* must be called last */
725 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */