1 /*****************************************************************************
2 * vorbis.c: vorbis decoder/encoder/packetizer module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: vorbis.c,v 1.23 2003/11/16 22:54:12 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() */
31 #include <vlc/decoder.h>
32 #include <vlc/input.h>
36 #ifdef MODULE_NAME_IS_tremor
37 #include <tremor/ivorbiscodec.h>
40 #include <vorbis/codec.h>
43 #ifdef HAVE_VORBIS_VORBISENC_H
44 # include <vorbis/vorbisenc.h>
45 # ifndef OV_ECTL_RATEMANAGE_AVG
46 # define OV_ECTL_RATEMANAGE_AVG 0x0
52 /*****************************************************************************
53 * decoder_sys_t : vorbis decoder descriptor
54 *****************************************************************************/
58 vlc_bool_t b_packetizer;
68 vorbis_info vi; /* struct that stores all the static vorbis bitstream
70 vorbis_comment vc; /* struct that stores all the bitstream user
72 vorbis_dsp_state vd; /* central working state for the packet->PCM
74 vorbis_block vb; /* local working space for packet->PCM decode */
79 audio_date_t end_date;
80 int i_last_block_size;
84 static int pi_channels_maps[6] =
87 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
88 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
89 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
90 | AOUT_CHAN_REARRIGHT,
91 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
92 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
95 /****************************************************************************
97 ****************************************************************************/
98 static int OpenDecoder ( vlc_object_t * );
99 static int OpenPacketizer( vlc_object_t * );
100 static void CloseDecoder ( vlc_object_t * );
101 static void *DecodeBlock ( decoder_t *, block_t ** );
103 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
105 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
106 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
108 static void ParseVorbisComments( decoder_t * );
110 #ifdef MODULE_NAME_IS_tremor
111 static void Interleave ( int32_t *, const int32_t **, int, int );
113 static void Interleave ( float *, const float **, int, int );
116 #ifndef MODULE_NAME_IS_tremor
117 static int OpenEncoder ( vlc_object_t * );
118 static void CloseEncoder ( vlc_object_t * );
119 static block_t *Headers ( encoder_t * );
120 static block_t *Encode ( encoder_t *, aout_buffer_t * );
123 /*****************************************************************************
125 *****************************************************************************/
128 set_description( _("Vorbis audio decoder") );
129 #ifdef MODULE_NAME_IS_tremor
130 set_capability( "decoder", 90 );
132 set_capability( "decoder", 100 );
134 set_callbacks( OpenDecoder, CloseDecoder );
137 set_description( _("Vorbis audio packetizer") );
138 set_capability( "packetizer", 100 );
139 set_callbacks( OpenPacketizer, CloseDecoder );
141 #ifndef MODULE_NAME_IS_tremor
143 set_description( _("Vorbis audio encoder") );
144 set_capability( "encoder", 100 );
145 set_callbacks( OpenEncoder, CloseEncoder );
150 /*****************************************************************************
151 * OpenDecoder: probe the decoder and return score
152 *****************************************************************************/
153 static int OpenDecoder( vlc_object_t *p_this )
155 decoder_t *p_dec = (decoder_t*)p_this;
156 decoder_sys_t *p_sys;
158 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
163 /* Allocate the memory needed to store the decoder's structure */
164 if( ( p_dec->p_sys = p_sys =
165 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
167 msg_Err( p_dec, "out of memory" );
172 aout_DateSet( &p_sys->end_date, 0 );
173 p_sys->b_packetizer = VLC_FALSE;
174 p_sys->i_headers = 0;
176 /* Take care of vorbis init */
177 vorbis_info_init( &p_sys->vi );
178 vorbis_comment_init( &p_sys->vc );
180 /* Set output properties */
181 p_dec->fmt_out.i_cat = AUDIO_ES;
182 #ifdef MODULE_NAME_IS_tremor
183 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
185 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
189 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
191 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
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 )
205 p_dec->p_sys->b_packetizer = VLC_TRUE;
206 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
212 /****************************************************************************
213 * DecodeBlock: the whole thing
214 ****************************************************************************
215 * This function must be fed with ogg packets.
216 ****************************************************************************/
217 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
219 decoder_sys_t *p_sys = p_dec->p_sys;
220 ogg_packet oggpacket;
222 if( !pp_block ) return NULL;
226 /* Block to Ogg packet */
227 oggpacket.packet = (*pp_block)->p_buffer;
228 oggpacket.bytes = (*pp_block)->i_buffer;
232 if( p_sys->b_packetizer ) return NULL;
234 /* Block to Ogg packet */
235 oggpacket.packet = NULL;
239 oggpacket.granulepos = -1;
242 oggpacket.packetno = 0;
244 if( p_sys->i_headers == 0 )
246 /* Take care of the initial Vorbis header */
248 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
249 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc,
252 msg_Err( p_dec, "This bitstream does not contain Vorbis "
254 block_Release( *pp_block );
259 /* Setup the format */
260 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
261 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
262 p_dec->fmt_out.audio.i_physical_channels =
263 p_dec->fmt_out.audio.i_original_channels =
264 pi_channels_maps[p_sys->vi.channels];
265 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
267 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
269 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
270 p_sys->vi.channels, p_sys->vi.rate,
271 p_sys->vi.bitrate_nominal );
273 return ProcessPacket( p_dec, &oggpacket, pp_block );
276 if( p_sys->i_headers == 1 )
278 /* The next packet in order is the comments header */
279 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
282 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
283 block_Release( *pp_block );
288 ParseVorbisComments( p_dec );
290 return ProcessPacket( p_dec, &oggpacket, pp_block );
293 if( p_sys->i_headers == 2 )
295 /* The next packet in order is the codebooks header
296 We need to watch out that this packet is not missing as a
297 missing or corrupted header is fatal. */
298 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
301 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
302 block_Release( *pp_block );
307 if( !p_sys->b_packetizer )
309 /* Initialize the Vorbis packet->PCM decoder */
310 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
311 vorbis_block_init( &p_sys->vd, &p_sys->vb );
314 return ProcessPacket( p_dec, &oggpacket, pp_block );
317 return ProcessPacket( p_dec, &oggpacket, pp_block );
320 /*****************************************************************************
321 * ProcessPacket: processes a Vorbis packet.
322 *****************************************************************************/
323 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
326 decoder_sys_t *p_sys = p_dec->p_sys;
327 block_t *p_block = *pp_block;
329 /* Date management */
330 if( p_block && p_block->i_pts > 0 &&
331 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
333 aout_DateSet( &p_sys->end_date, p_block->i_pts );
336 if( !aout_DateGet( &p_sys->end_date ) )
338 /* We've just started the stream, wait for the first PTS. */
339 if( p_block ) block_Release( p_block );
343 *pp_block = NULL; /* To avoid being fed the same packet again */
345 if( p_sys->b_packetizer )
347 return SendPacket( p_dec, p_oggpacket, p_block );
351 aout_buffer_t *p_aout_buffer;
353 if( p_sys->i_headers >= 3 )
354 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
356 p_aout_buffer = NULL;
360 block_Release( p_block );
362 return p_aout_buffer;
366 /*****************************************************************************
367 * DecodePacket: decodes a Vorbis packet.
368 *****************************************************************************/
369 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
371 decoder_sys_t *p_sys = p_dec->p_sys;
374 #ifdef MODULE_NAME_IS_tremor
380 if( p_oggpacket->bytes &&
381 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
382 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
384 /* **pp_pcm is a multichannel float vector. In stereo, for
385 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
386 * the size of each channel. Convert the float values
387 * (-1.<=range<=1.) to whatever PCM format and write it out */
389 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
392 aout_buffer_t *p_aout_buffer;
395 p_dec->pf_aout_buffer_new( p_dec, i_samples );
397 if( p_aout_buffer == NULL ) return NULL;
399 /* Interleave the samples */
400 #ifdef MODULE_NAME_IS_tremor
401 Interleave( (int32_t *)p_aout_buffer->p_buffer,
402 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
404 Interleave( (float *)p_aout_buffer->p_buffer,
405 (const float **)pp_pcm, p_sys->vi.channels, i_samples );
408 /* Tell libvorbis how many samples we actually consumed */
409 vorbis_synthesis_read( &p_sys->vd, i_samples );
411 /* Date management */
412 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
413 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
415 return p_aout_buffer;
423 /*****************************************************************************
424 * SendPacket: send an ogg dated packet to the stream output.
425 *****************************************************************************/
426 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
429 decoder_sys_t *p_sys = p_dec->p_sys;
430 int i_block_size, i_samples;
432 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
433 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
434 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
435 p_sys->i_last_block_size = i_block_size;
437 /* Date management */
438 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
440 if( p_sys->i_headers >= 3 )
441 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
444 p_block->i_length = 0;
449 /*****************************************************************************
450 * ParseVorbisComments: FIXME should be done in demuxer
451 *****************************************************************************/
452 static void ParseVorbisComments( decoder_t *p_dec )
454 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
455 input_info_category_t *p_cat =
456 input_InfoCategory( p_input, _("Vorbis Comment") );
458 char *psz_name, *psz_value, *psz_comment;
459 while ( i < p_dec->p_sys->vc.comments )
461 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
464 msg_Warn( p_dec, "Out of memory" );
467 psz_name = psz_comment;
468 psz_value = strchr( psz_comment, '=' );
473 input_AddInfo( p_cat, psz_name, psz_value );
480 /*****************************************************************************
481 * Interleave: helper function to interleave channels
482 *****************************************************************************/
483 #ifdef MODULE_NAME_IS_tremor
484 static void Interleave( int32_t *p_out, const int32_t **pp_in,
486 static void Interleave( float *p_out, const float **pp_in,
488 int i_nb_channels, int i_samples )
492 for ( j = 0; j < i_samples; j++ )
494 for ( i = 0; i < i_nb_channels; i++ )
496 p_out[j * i_nb_channels + i] = pp_in[i][j];
501 /*****************************************************************************
502 * CloseDecoder: vorbis decoder destruction
503 *****************************************************************************/
504 static void CloseDecoder( vlc_object_t *p_this )
506 decoder_t *p_dec = (decoder_t *)p_this;
507 decoder_sys_t *p_sys = p_dec->p_sys;
509 if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
511 vorbis_block_clear( &p_sys->vb );
512 vorbis_dsp_clear( &p_sys->vd );
515 vorbis_comment_clear( &p_sys->vc );
516 vorbis_info_clear( &p_sys->vi ); /* must be called last */
521 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
523 /*****************************************************************************
524 * encoder_sys_t : theora encoder descriptor
525 *****************************************************************************/
536 vorbis_info vi; /* struct that stores all the static vorbis bitstream
538 vorbis_comment vc; /* struct that stores all the bitstream user
540 vorbis_dsp_state vd; /* central working state for the packet->PCM
542 vorbis_block vb; /* local working space for packet->PCM decode */
544 int i_last_block_size;
554 /*****************************************************************************
555 * OpenEncoder: probe the encoder and return score
556 *****************************************************************************/
557 static int OpenEncoder( vlc_object_t *p_this )
559 encoder_t *p_enc = (encoder_t *)p_this;
560 encoder_sys_t *p_sys;
562 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') )
567 /* Allocate the memory needed to store the decoder's structure */
568 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
570 msg_Err( p_enc, "out of memory" );
573 p_enc->p_sys = p_sys;
575 p_enc->pf_header = Headers;
576 p_enc->pf_encode_audio = Encode;
577 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
579 /* Initialize vorbis encoder */
580 vorbis_info_init( &p_sys->vi );
582 if( vorbis_encode_setup_managed( &p_sys->vi,
583 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
584 -1, p_enc->fmt_out.i_bitrate, -1 ) ||
585 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
586 vorbis_encode_setup_init( &p_sys->vi ) ){}
589 vorbis_comment_init( &p_sys->vc);
590 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
592 /* set up the analysis state and auxiliary encoding storage */
593 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
594 vorbis_block_init( &p_sys->vd, &p_sys->vb );
596 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
597 p_sys->i_last_block_size = 0;
598 p_sys->i_samples_delay = 0;
599 p_sys->i_headers = 0;
605 /****************************************************************************
606 * Encode: the whole thing
607 ****************************************************************************
608 * This function spits out ogg packets.
609 ****************************************************************************/
610 static block_t *Headers( encoder_t *p_enc )
612 encoder_sys_t *p_sys = p_enc->p_sys;
613 block_t *p_block, *p_chain = NULL;
615 /* Create theora headers */
616 if( !p_sys->i_headers )
618 ogg_packet header[3];
621 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
622 &header[0], &header[1], &header[2]);
623 for( i = 0; i < 3; i++ )
625 p_block = block_New( p_enc, header[i].bytes );
626 memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
628 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
630 block_ChainAppend( &p_chain, p_block );
632 p_sys->i_headers = 3;
638 /****************************************************************************
639 * Encode: the whole thing
640 ****************************************************************************
641 * This function spits out ogg packets.
642 ****************************************************************************/
643 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
645 encoder_sys_t *p_sys = p_enc->p_sys;
646 ogg_packet oggpacket;
647 block_t *p_block, *p_chain = NULL;
651 p_sys->i_pts = p_aout_buf->start_date -
652 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
653 (mtime_t)p_enc->fmt_in.audio.i_rate;
655 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
657 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
660 if( id->ff_dec_c->channels != id->ff_enc_c->channels )
664 /* dumb downmixing */
665 for( i = 0; i < id->ff_enc_c->frame_size; i++ )
667 for( j = 0 ; j < id->f_dst.i_channels; j++ )
669 p_buffer[i*id->f_dst.i_channels+j] =
670 p_buffer[i*id->f_src.i_channels+j];
676 /* convert samples to float and uninterleave */
677 for( i = 0; i < p_sys->i_channels; i++ )
679 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
681 buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
682 [j * p_sys->i_channels + i ] )) / 32768.f;
686 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
688 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
692 vorbis_analysis( &p_sys->vb, NULL );
693 vorbis_bitrate_addblock( &p_sys->vb );
695 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
698 p_block = block_New( p_enc, oggpacket.bytes );
699 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
701 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
703 if( i_block_size < 0 ) i_block_size = 0;
704 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
705 p_sys->i_last_block_size = i_block_size;
707 p_block->i_length = (mtime_t)1000000 *
708 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
710 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
712 p_sys->i_samples_delay -= i_samples;
715 p_sys->i_pts += p_block->i_length;
716 block_ChainAppend( &p_chain, p_block );
723 /*****************************************************************************
724 * CloseEncoder: theora encoder destruction
725 *****************************************************************************/
726 static void CloseEncoder( vlc_object_t *p_this )
728 encoder_t *p_enc = (encoder_t *)p_this;
729 encoder_sys_t *p_sys = p_enc->p_sys;
731 vorbis_block_clear( &p_sys->vb );
732 vorbis_dsp_clear( &p_sys->vd );
733 vorbis_comment_clear( &p_sys->vc );
734 vorbis_info_clear( &p_sys->vi ); /* must be called last */
739 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */