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.32 2004/02/22 15:57:41 fenrir 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>
29 #include <vlc/input.h>
30 #include "vlc_playlist.h"
34 #ifdef MODULE_NAME_IS_tremor
35 #include <tremor/ivorbiscodec.h>
38 #include <vorbis/codec.h>
41 #ifdef HAVE_VORBIS_VORBISENC_H
42 # include <vorbis/vorbisenc.h>
43 # ifndef OV_ECTL_RATEMANAGE_AVG
44 # define OV_ECTL_RATEMANAGE_AVG 0x0
50 /*****************************************************************************
51 * decoder_sys_t : vorbis decoder descriptor
52 *****************************************************************************/
56 vlc_bool_t b_packetizer;
66 vorbis_info vi; /* struct that stores all the static vorbis bitstream
68 vorbis_comment vc; /* struct that stores all the bitstream user
70 vorbis_dsp_state vd; /* central working state for the packet->PCM
72 vorbis_block vb; /* local working space for packet->PCM decode */
77 audio_date_t end_date;
78 int i_last_block_size;
82 static int pi_channels_maps[7] =
86 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
87 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
88 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
89 | AOUT_CHAN_REARRIGHT,
90 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
91 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
92 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
93 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
96 /****************************************************************************
98 ****************************************************************************/
99 static int OpenDecoder ( vlc_object_t * );
100 static int OpenPacketizer( vlc_object_t * );
101 static void CloseDecoder ( vlc_object_t * );
102 static void *DecodeBlock ( decoder_t *, block_t ** );
104 static void *ProcessPacket ( decoder_t *, ogg_packet *, block_t ** );
106 static aout_buffer_t *DecodePacket ( decoder_t *, ogg_packet * );
107 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
109 static void ParseVorbisComments( decoder_t * );
111 #ifdef MODULE_NAME_IS_tremor
112 static void Interleave ( int32_t *, const int32_t **, int, int );
114 static void Interleave ( float *, const float **, int, int );
117 #ifndef MODULE_NAME_IS_tremor
118 static int OpenEncoder ( vlc_object_t * );
119 static void CloseEncoder ( vlc_object_t * );
120 static block_t *Headers ( encoder_t * );
121 static block_t *Encode ( encoder_t *, aout_buffer_t * );
124 /*****************************************************************************
126 *****************************************************************************/
129 set_description( _("Vorbis audio decoder") );
130 #ifdef MODULE_NAME_IS_tremor
131 set_capability( "decoder", 90 );
133 set_capability( "decoder", 100 );
135 set_callbacks( OpenDecoder, CloseDecoder );
138 set_description( _("Vorbis audio packetizer") );
139 set_capability( "packetizer", 100 );
140 set_callbacks( OpenPacketizer, CloseDecoder );
142 #ifndef MODULE_NAME_IS_tremor
144 set_description( _("Vorbis audio encoder") );
145 set_capability( "encoder", 100 );
146 set_callbacks( OpenEncoder, CloseEncoder );
151 /*****************************************************************************
152 * OpenDecoder: probe the decoder and return score
153 *****************************************************************************/
154 static int OpenDecoder( vlc_object_t *p_this )
156 decoder_t *p_dec = (decoder_t*)p_this;
157 decoder_sys_t *p_sys;
159 if( p_dec->fmt_in.i_codec != VLC_FOURCC('v','o','r','b') )
164 /* Allocate the memory needed to store the decoder's structure */
165 if( ( p_dec->p_sys = p_sys =
166 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
168 msg_Err( p_dec, "out of memory" );
173 aout_DateSet( &p_sys->end_date, 0 );
174 p_sys->b_packetizer = VLC_FALSE;
175 p_sys->i_headers = 0;
177 /* Take care of vorbis init */
178 vorbis_info_init( &p_sys->vi );
179 vorbis_comment_init( &p_sys->vc );
181 /* Set output properties */
182 p_dec->fmt_out.i_cat = AUDIO_ES;
183 #ifdef MODULE_NAME_IS_tremor
184 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
186 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
190 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
192 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
198 static int OpenPacketizer( vlc_object_t *p_this )
200 decoder_t *p_dec = (decoder_t*)p_this;
202 int i_ret = OpenDecoder( p_this );
204 if( i_ret == VLC_SUCCESS )
206 p_dec->p_sys->b_packetizer = VLC_TRUE;
207 p_dec->fmt_out.i_codec = VLC_FOURCC('v','o','r','b');
213 /****************************************************************************
214 * DecodeBlock: the whole thing
215 ****************************************************************************
216 * This function must be fed with ogg packets.
217 ****************************************************************************/
218 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
220 decoder_sys_t *p_sys = p_dec->p_sys;
221 ogg_packet oggpacket;
223 if( !pp_block ) return NULL;
227 /* Block to Ogg packet */
228 oggpacket.packet = (*pp_block)->p_buffer;
229 oggpacket.bytes = (*pp_block)->i_buffer;
233 if( p_sys->b_packetizer ) return NULL;
235 /* Block to Ogg packet */
236 oggpacket.packet = NULL;
240 oggpacket.granulepos = -1;
243 oggpacket.packetno = 0;
245 if( p_sys->i_headers == 0 )
247 /* Take care of the initial Vorbis header */
249 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
250 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc,
253 msg_Err( p_dec, "this bitstream does not contain Vorbis "
255 block_Release( *pp_block );
260 /* Setup the format */
261 p_dec->fmt_out.audio.i_rate = p_sys->vi.rate;
262 p_dec->fmt_out.audio.i_channels = p_sys->vi.channels;
263 p_dec->fmt_out.audio.i_physical_channels =
264 p_dec->fmt_out.audio.i_original_channels =
265 pi_channels_maps[p_sys->vi.channels];
266 p_dec->fmt_out.i_bitrate = p_sys->vi.bitrate_nominal;
268 aout_DateInit( &p_sys->end_date, p_sys->vi.rate );
270 msg_Dbg( p_dec, "channels:%d samplerate:%ld bitrate:%ld",
271 p_sys->vi.channels, p_sys->vi.rate,
272 p_sys->vi.bitrate_nominal );
274 return ProcessPacket( p_dec, &oggpacket, pp_block );
277 if( p_sys->i_headers == 1 )
279 /* The next packet in order is the comments header */
280 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
283 msg_Err( p_dec, "2nd Vorbis header is corrupted" );
284 block_Release( *pp_block );
289 ParseVorbisComments( p_dec );
291 return ProcessPacket( p_dec, &oggpacket, pp_block );
294 if( p_sys->i_headers == 2 )
296 /* The next packet in order is the codebooks header
297 We need to watch out that this packet is not missing as a
298 missing or corrupted header is fatal. */
299 if( vorbis_synthesis_headerin( &p_sys->vi, &p_sys->vc, &oggpacket )
302 msg_Err( p_dec, "3rd Vorbis header is corrupted" );
303 block_Release( *pp_block );
308 if( !p_sys->b_packetizer )
310 /* Initialize the Vorbis packet->PCM decoder */
311 vorbis_synthesis_init( &p_sys->vd, &p_sys->vi );
312 vorbis_block_init( &p_sys->vd, &p_sys->vb );
315 return ProcessPacket( p_dec, &oggpacket, pp_block );
318 return ProcessPacket( p_dec, &oggpacket, pp_block );
321 /*****************************************************************************
322 * ProcessPacket: processes a Vorbis packet.
323 *****************************************************************************/
324 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
327 decoder_sys_t *p_sys = p_dec->p_sys;
328 block_t *p_block = *pp_block;
330 /* Date management */
331 if( p_block && p_block->i_pts > 0 &&
332 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
334 aout_DateSet( &p_sys->end_date, p_block->i_pts );
337 if( !aout_DateGet( &p_sys->end_date ) )
339 /* We've just started the stream, wait for the first PTS. */
340 if( p_block ) block_Release( p_block );
344 *pp_block = NULL; /* To avoid being fed the same packet again */
346 if( p_sys->b_packetizer )
348 return SendPacket( p_dec, p_oggpacket, p_block );
352 aout_buffer_t *p_aout_buffer;
354 if( p_sys->i_headers >= 3 )
355 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
357 p_aout_buffer = NULL;
361 block_Release( p_block );
363 return p_aout_buffer;
367 /*****************************************************************************
368 * DecodePacket: decodes a Vorbis packet.
369 *****************************************************************************/
370 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
372 decoder_sys_t *p_sys = p_dec->p_sys;
375 #ifdef MODULE_NAME_IS_tremor
381 if( p_oggpacket->bytes &&
382 vorbis_synthesis( &p_sys->vb, p_oggpacket ) == 0 )
383 vorbis_synthesis_blockin( &p_sys->vd, &p_sys->vb );
385 /* **pp_pcm is a multichannel float vector. In stereo, for
386 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
387 * the size of each channel. Convert the float values
388 * (-1.<=range<=1.) to whatever PCM format and write it out */
390 if( ( i_samples = vorbis_synthesis_pcmout( &p_sys->vd, &pp_pcm ) ) > 0 )
393 aout_buffer_t *p_aout_buffer;
396 p_dec->pf_aout_buffer_new( p_dec, i_samples );
398 if( p_aout_buffer == NULL ) return NULL;
400 /* Interleave the samples */
401 #ifdef MODULE_NAME_IS_tremor
402 Interleave( (int32_t *)p_aout_buffer->p_buffer,
403 (const int32_t **)pp_pcm, p_sys->vi.channels, i_samples );
405 Interleave( (float *)p_aout_buffer->p_buffer,
406 (const float **)pp_pcm, p_sys->vi.channels, i_samples );
409 /* Tell libvorbis how many samples we actually consumed */
410 vorbis_synthesis_read( &p_sys->vd, i_samples );
412 /* Date management */
413 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
414 p_aout_buffer->end_date = aout_DateIncrement( &p_sys->end_date,
416 return p_aout_buffer;
424 /*****************************************************************************
425 * SendPacket: send an ogg dated packet to the stream output.
426 *****************************************************************************/
427 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
430 decoder_sys_t *p_sys = p_dec->p_sys;
431 int i_block_size, i_samples;
433 i_block_size = vorbis_packet_blocksize( &p_sys->vi, p_oggpacket );
434 if( i_block_size < 0 ) i_block_size = 0; /* non audio packet */
435 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
436 p_sys->i_last_block_size = i_block_size;
438 /* Date management */
439 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
441 if( p_sys->i_headers >= 3 )
442 p_block->i_length = aout_DateIncrement( &p_sys->end_date, i_samples ) -
445 p_block->i_length = 0;
450 /*****************************************************************************
451 * ParseVorbisComments: FIXME should be done in demuxer
452 *****************************************************************************/
453 static void ParseVorbisComments( decoder_t *p_dec )
455 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
456 input_info_category_t *p_cat =
457 input_InfoCategory( p_input, _("Vorbis comment") );
458 playlist_t *p_playlist = vlc_object_find( p_dec, VLC_OBJECT_PLAYLIST,
460 playlist_item_t *p_item;
462 char *psz_name, *psz_value, *psz_comment;
463 while ( i < p_dec->p_sys->vc.comments )
465 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
468 msg_Warn( p_dec, "out of memory" );
471 psz_name = psz_comment;
472 psz_value = strchr( psz_comment, '=' );
477 input_AddInfo( p_cat, psz_name, psz_value );
478 vlc_mutex_lock( &p_playlist->object_lock );
479 p_item = playlist_ItemGetByPos( p_playlist, -1 );
480 vlc_mutex_unlock( &p_playlist->object_lock );
483 msg_Err(p_dec, "unable to find item" );
486 vlc_mutex_lock( &p_item->lock );
487 playlist_ItemAddInfo( p_item, _("Vorbis comment") ,
488 psz_name, psz_value );
489 vlc_mutex_unlock( &p_item->lock );
494 if( p_playlist ) vlc_object_release( p_playlist );
497 /*****************************************************************************
498 * Interleave: helper function to interleave channels
499 *****************************************************************************/
500 static void Interleave(
501 #ifdef MODULE_NAME_IS_tremor
502 int32_t *p_out, const int32_t **pp_in,
504 float *p_out, const float **pp_in,
506 int i_nb_channels, int i_samples )
510 for ( j = 0; j < i_samples; j++ )
512 for ( i = 0; i < i_nb_channels; i++ )
514 p_out[j * i_nb_channels + i] = pp_in[i][j];
519 /*****************************************************************************
520 * CloseDecoder: vorbis decoder destruction
521 *****************************************************************************/
522 static void CloseDecoder( vlc_object_t *p_this )
524 decoder_t *p_dec = (decoder_t *)p_this;
525 decoder_sys_t *p_sys = p_dec->p_sys;
527 if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
529 vorbis_block_clear( &p_sys->vb );
530 vorbis_dsp_clear( &p_sys->vd );
533 vorbis_comment_clear( &p_sys->vc );
534 vorbis_info_clear( &p_sys->vi ); /* must be called last */
539 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
541 /*****************************************************************************
542 * encoder_sys_t : theora encoder descriptor
543 *****************************************************************************/
554 vorbis_info vi; /* struct that stores all the static vorbis bitstream
556 vorbis_comment vc; /* struct that stores all the bitstream user
558 vorbis_dsp_state vd; /* central working state for the packet->PCM
560 vorbis_block vb; /* local working space for packet->PCM decode */
562 int i_last_block_size;
572 /*****************************************************************************
573 * OpenEncoder: probe the encoder and return score
574 *****************************************************************************/
575 static int OpenEncoder( vlc_object_t *p_this )
577 encoder_t *p_enc = (encoder_t *)p_this;
578 encoder_sys_t *p_sys;
580 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') )
585 /* Allocate the memory needed to store the decoder's structure */
586 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
588 msg_Err( p_enc, "out of memory" );
591 p_enc->p_sys = p_sys;
593 p_enc->pf_header = Headers;
594 p_enc->pf_encode_audio = Encode;
595 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
597 /* Initialize vorbis encoder */
598 vorbis_info_init( &p_sys->vi );
600 if( vorbis_encode_setup_managed( &p_sys->vi,
601 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
602 -1, p_enc->fmt_out.i_bitrate, -1 ) ||
603 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
604 vorbis_encode_setup_init( &p_sys->vi ) )
610 vorbis_comment_init( &p_sys->vc);
611 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
613 /* set up the analysis state and auxiliary encoding storage */
614 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
615 vorbis_block_init( &p_sys->vd, &p_sys->vb );
617 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
618 p_sys->i_last_block_size = 0;
619 p_sys->i_samples_delay = 0;
620 p_sys->i_headers = 0;
626 /****************************************************************************
627 * Encode: the whole thing
628 ****************************************************************************
629 * This function spits out ogg packets.
630 ****************************************************************************/
631 static block_t *Headers( encoder_t *p_enc )
633 encoder_sys_t *p_sys = p_enc->p_sys;
634 block_t *p_block, *p_chain = NULL;
636 /* Create theora headers */
637 if( !p_sys->i_headers )
639 ogg_packet header[3];
642 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
643 &header[0], &header[1], &header[2]);
644 for( i = 0; i < 3; i++ )
646 p_block = block_New( p_enc, header[i].bytes );
647 memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
649 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
651 block_ChainAppend( &p_chain, p_block );
653 p_sys->i_headers = 3;
659 /****************************************************************************
660 * Encode: the whole thing
661 ****************************************************************************
662 * This function spits out ogg packets.
663 ****************************************************************************/
664 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
666 encoder_sys_t *p_sys = p_enc->p_sys;
667 ogg_packet oggpacket;
668 block_t *p_block, *p_chain = NULL;
673 p_sys->i_pts = p_aout_buf->start_date -
674 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
675 (mtime_t)p_enc->fmt_in.audio.i_rate;
677 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
679 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
681 /* convert samples to float and uninterleave */
682 for( i = 0; i < p_sys->i_channels; i++ )
684 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
686 buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
687 [j * p_sys->i_channels + i ] )) / 32768.f;
691 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
693 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
697 vorbis_analysis( &p_sys->vb, NULL );
698 vorbis_bitrate_addblock( &p_sys->vb );
700 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
703 p_block = block_New( p_enc, oggpacket.bytes );
704 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
706 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
708 if( i_block_size < 0 ) i_block_size = 0;
709 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
710 p_sys->i_last_block_size = i_block_size;
712 p_block->i_length = (mtime_t)1000000 *
713 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
715 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
717 p_sys->i_samples_delay -= i_samples;
720 p_sys->i_pts += p_block->i_length;
721 block_ChainAppend( &p_chain, p_block );
728 /*****************************************************************************
729 * CloseEncoder: theora encoder destruction
730 *****************************************************************************/
731 static void CloseEncoder( vlc_object_t *p_this )
733 encoder_t *p_enc = (encoder_t *)p_this;
734 encoder_sys_t *p_sys = p_enc->p_sys;
736 vorbis_block_clear( &p_sys->vb );
737 vorbis_dsp_clear( &p_sys->vd );
738 vorbis_comment_clear( &p_sys->vc );
739 vorbis_info_clear( &p_sys->vi ); /* must be called last */
744 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */