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.30 2004/01/25 18:20:12 bigben 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_playlist.h"
33 #ifdef MODULE_NAME_IS_tremor
34 #include <tremor/ivorbiscodec.h>
37 #include <vorbis/codec.h>
40 #ifdef HAVE_VORBIS_VORBISENC_H
41 # include <vorbis/vorbisenc.h>
42 # ifndef OV_ECTL_RATEMANAGE_AVG
43 # define OV_ECTL_RATEMANAGE_AVG 0x0
49 /*****************************************************************************
50 * decoder_sys_t : vorbis decoder descriptor
51 *****************************************************************************/
55 vlc_bool_t b_packetizer;
65 vorbis_info vi; /* struct that stores all the static vorbis bitstream
67 vorbis_comment vc; /* struct that stores all the bitstream user
69 vorbis_dsp_state vd; /* central working state for the packet->PCM
71 vorbis_block vb; /* local working space for packet->PCM decode */
76 audio_date_t end_date;
77 int i_last_block_size;
81 static int pi_channels_maps[7] =
85 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
86 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
87 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
88 | AOUT_CHAN_REARRIGHT,
89 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
90 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
91 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
92 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE
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") );
457 playlist_t *p_playlist = vlc_object_find( p_dec, VLC_OBJECT_PLAYLIST,
460 char *psz_name, *psz_value, *psz_comment;
461 while ( i < p_dec->p_sys->vc.comments )
463 psz_comment = strdup( p_dec->p_sys->vc.user_comments[i] );
466 msg_Warn( p_dec, "out of memory" );
469 psz_name = psz_comment;
470 psz_value = strchr( psz_comment, '=' );
475 input_AddInfo( p_cat, psz_name, psz_value );
476 playlist_AddInfo( p_playlist, -1, _("Vorbis comment") ,
477 psz_name, psz_value );
482 if( p_playlist ) vlc_object_release( p_playlist );
485 /*****************************************************************************
486 * Interleave: helper function to interleave channels
487 *****************************************************************************/
488 static void Interleave(
489 #ifdef MODULE_NAME_IS_tremor
490 int32_t *p_out, const int32_t **pp_in,
492 float *p_out, const float **pp_in,
494 int i_nb_channels, int i_samples )
498 for ( j = 0; j < i_samples; j++ )
500 for ( i = 0; i < i_nb_channels; i++ )
502 p_out[j * i_nb_channels + i] = pp_in[i][j];
507 /*****************************************************************************
508 * CloseDecoder: vorbis decoder destruction
509 *****************************************************************************/
510 static void CloseDecoder( vlc_object_t *p_this )
512 decoder_t *p_dec = (decoder_t *)p_this;
513 decoder_sys_t *p_sys = p_dec->p_sys;
515 if( !p_sys->b_packetizer && p_sys->i_headers >= 3 )
517 vorbis_block_clear( &p_sys->vb );
518 vorbis_dsp_clear( &p_sys->vd );
521 vorbis_comment_clear( &p_sys->vc );
522 vorbis_info_clear( &p_sys->vi ); /* must be called last */
527 #if defined(HAVE_VORBIS_VORBISENC_H) && !defined(MODULE_NAME_IS_tremor)
529 /*****************************************************************************
530 * encoder_sys_t : theora encoder descriptor
531 *****************************************************************************/
542 vorbis_info vi; /* struct that stores all the static vorbis bitstream
544 vorbis_comment vc; /* struct that stores all the bitstream user
546 vorbis_dsp_state vd; /* central working state for the packet->PCM
548 vorbis_block vb; /* local working space for packet->PCM decode */
550 int i_last_block_size;
560 /*****************************************************************************
561 * OpenEncoder: probe the encoder and return score
562 *****************************************************************************/
563 static int OpenEncoder( vlc_object_t *p_this )
565 encoder_t *p_enc = (encoder_t *)p_this;
566 encoder_sys_t *p_sys;
568 if( p_enc->fmt_out.i_codec != VLC_FOURCC('v','o','r','b') )
573 /* Allocate the memory needed to store the decoder's structure */
574 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
576 msg_Err( p_enc, "out of memory" );
579 p_enc->p_sys = p_sys;
581 p_enc->pf_header = Headers;
582 p_enc->pf_encode_audio = Encode;
583 p_enc->fmt_in.i_codec = VLC_FOURCC('f','l','3','2');
585 /* Initialize vorbis encoder */
586 vorbis_info_init( &p_sys->vi );
588 if( vorbis_encode_setup_managed( &p_sys->vi,
589 p_enc->fmt_in.audio.i_channels, p_enc->fmt_in.audio.i_rate,
590 -1, p_enc->fmt_out.i_bitrate, -1 ) ||
591 vorbis_encode_ctl( &p_sys->vi, OV_ECTL_RATEMANAGE_AVG, NULL ) ||
592 vorbis_encode_setup_init( &p_sys->vi ) )
598 vorbis_comment_init( &p_sys->vc);
599 vorbis_comment_add_tag( &p_sys->vc, "ENCODER", "VLC media player");
601 /* set up the analysis state and auxiliary encoding storage */
602 vorbis_analysis_init( &p_sys->vd, &p_sys->vi );
603 vorbis_block_init( &p_sys->vd, &p_sys->vb );
605 p_sys->i_channels = p_enc->fmt_in.audio.i_channels;
606 p_sys->i_last_block_size = 0;
607 p_sys->i_samples_delay = 0;
608 p_sys->i_headers = 0;
614 /****************************************************************************
615 * Encode: the whole thing
616 ****************************************************************************
617 * This function spits out ogg packets.
618 ****************************************************************************/
619 static block_t *Headers( encoder_t *p_enc )
621 encoder_sys_t *p_sys = p_enc->p_sys;
622 block_t *p_block, *p_chain = NULL;
624 /* Create theora headers */
625 if( !p_sys->i_headers )
627 ogg_packet header[3];
630 vorbis_analysis_headerout( &p_sys->vd, &p_sys->vc,
631 &header[0], &header[1], &header[2]);
632 for( i = 0; i < 3; i++ )
634 p_block = block_New( p_enc, header[i].bytes );
635 memcpy( p_block->p_buffer, header[i].packet, header[i].bytes );
637 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
639 block_ChainAppend( &p_chain, p_block );
641 p_sys->i_headers = 3;
647 /****************************************************************************
648 * Encode: the whole thing
649 ****************************************************************************
650 * This function spits out ogg packets.
651 ****************************************************************************/
652 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
654 encoder_sys_t *p_sys = p_enc->p_sys;
655 ogg_packet oggpacket;
656 block_t *p_block, *p_chain = NULL;
661 p_sys->i_pts = p_aout_buf->start_date -
662 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
663 (mtime_t)p_enc->fmt_in.audio.i_rate;
665 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
667 buffer = vorbis_analysis_buffer( &p_sys->vd, p_aout_buf->i_nb_samples );
669 /* convert samples to float and uninterleave */
670 for( i = 0; i < p_sys->i_channels; i++ )
672 for( j = 0 ; j < p_aout_buf->i_nb_samples ; j++ )
674 buffer[i][j]= ((float)( ((int16_t *)p_aout_buf->p_buffer )
675 [j * p_sys->i_channels + i ] )) / 32768.f;
679 vorbis_analysis_wrote( &p_sys->vd, p_aout_buf->i_nb_samples );
681 while( vorbis_analysis_blockout( &p_sys->vd, &p_sys->vb ) == 1 )
685 vorbis_analysis( &p_sys->vb, NULL );
686 vorbis_bitrate_addblock( &p_sys->vb );
688 while( vorbis_bitrate_flushpacket( &p_sys->vd, &oggpacket ) )
691 p_block = block_New( p_enc, oggpacket.bytes );
692 memcpy( p_block->p_buffer, oggpacket.packet, oggpacket.bytes );
694 i_block_size = vorbis_packet_blocksize( &p_sys->vi, &oggpacket );
696 if( i_block_size < 0 ) i_block_size = 0;
697 i_samples = ( p_sys->i_last_block_size + i_block_size ) >> 2;
698 p_sys->i_last_block_size = i_block_size;
700 p_block->i_length = (mtime_t)1000000 *
701 (mtime_t)i_samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
703 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
705 p_sys->i_samples_delay -= i_samples;
708 p_sys->i_pts += p_block->i_length;
709 block_ChainAppend( &p_chain, p_block );
716 /*****************************************************************************
717 * CloseEncoder: theora encoder destruction
718 *****************************************************************************/
719 static void CloseEncoder( vlc_object_t *p_this )
721 encoder_t *p_enc = (encoder_t *)p_this;
722 encoder_sys_t *p_sys = p_enc->p_sys;
724 vorbis_block_clear( &p_sys->vb );
725 vorbis_dsp_clear( &p_sys->vd );
726 vorbis_comment_clear( &p_sys->vc );
727 vorbis_info_clear( &p_sys->vi ); /* must be called last */
732 #endif /* HAVE_VORBIS_VORBISENC_H && !MODULE_NAME_IS_tremor */