1 /*****************************************************************************
2 * vorbis.c: vorbis decoder module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: vorbis.c,v 1.7 2002/11/21 21:37:46 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 <input_ext-dec.h>
35 #include <vlc/input.h>
38 #ifdef MODULE_NAME_IS_tremor
39 #include <tremor/ivorbiscodec.h>
41 #include <vorbis/codec.h>
44 /*****************************************************************************
45 * dec_thread_t : vorbis decoder thread descriptor
46 *****************************************************************************/
47 typedef struct dec_thread_t
52 vlc_thread_t thread_id; /* id for thread functions */
57 vorbis_info vi; /* struct that stores all the static vorbis bitstream
59 vorbis_comment vc; /* struct that stores all the bitstream user
61 vorbis_dsp_state vd; /* central working state for the packet->PCM
63 vorbis_block vb; /* local working space for packet->PCM decode */
68 decoder_fifo_t *p_fifo; /* stores the PES stream data */
69 pes_packet_t *p_pes; /* current PES we are decoding */
74 aout_instance_t *p_aout;
75 aout_input_t *p_aout_input;
76 audio_sample_format_t output_format;
77 audio_date_t end_date;
81 static int pi_channels_maps[6] =
84 AOUT_CHAN_CENTER, 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
92 /*****************************************************************************
94 *****************************************************************************/
95 static int OpenDecoder ( vlc_object_t * );
96 static int RunDecoder ( decoder_fifo_t * );
97 static void CloseDecoder ( dec_thread_t * );
99 static void DecodePacket ( dec_thread_t * );
100 static int GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
102 #ifdef MODULE_NAME_IS_tremor
103 static void Interleave ( int32_t *, const int32_t **, int, int );
105 static void Interleave ( float *, const float **, int, int );
108 /*****************************************************************************
110 *****************************************************************************/
112 set_description( _("Vorbis decoder module") );
113 set_capability( "decoder", 100 );
114 set_callbacks( OpenDecoder, NULL );
117 /*****************************************************************************
118 * OpenDecoder: probe the decoder and return score
119 *****************************************************************************/
120 static int OpenDecoder( vlc_object_t *p_this )
122 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
124 if( p_fifo->i_fourcc != VLC_FOURCC('v','o','r','b') )
129 p_fifo->pf_run = RunDecoder;
133 /*****************************************************************************
134 * RunDecoder: the vorbis decoder
135 *****************************************************************************/
136 static int RunDecoder( decoder_fifo_t * p_fifo )
139 ogg_packet oggpacket;
142 /* Allocate the memory needed to store the thread's structure */
143 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
146 msg_Err( p_fifo, "out of memory" );
150 /* Initialize the thread properties */
151 memset( p_dec, 0, sizeof(dec_thread_t) );
152 p_dec->p_fifo = p_fifo;
155 /* Take care of the initial Vorbis header */
156 vorbis_info_init( &p_dec->vi );
157 vorbis_comment_init( &p_dec->vc );
159 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
162 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
163 if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
165 msg_Err( p_dec->p_fifo, "This bitstream does not contain Vorbis "
170 /* The next two packets in order are the comment and codebook headers.
171 We need to watch out that these packets are not missing as a
172 missing or corrupted header is fatal. */
173 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
176 if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
178 msg_Err( p_dec->p_fifo, "2nd Vorbis header is corrupted" );
182 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
185 if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
187 msg_Err( p_dec->p_fifo, "3rd Vorbis header is corrupted" );
191 /* Initialize the Vorbis packet->PCM decoder */
192 vorbis_synthesis_init( &p_dec->vd, &p_dec->vi );
193 vorbis_block_init( &p_dec->vd, &p_dec->vb );
195 #ifdef MODULE_NAME_IS_tremor
196 p_dec->output_format.i_format = VLC_FOURCC('f','i','3','2');
198 p_dec->output_format.i_format = VLC_FOURCC('f','l','3','2');
200 p_dec->output_format.i_physical_channels =
201 p_dec->output_format.i_original_channels =
202 pi_channels_maps[p_dec->vi.channels];
203 p_dec->output_format.i_rate = p_dec->vi.rate;
205 aout_DateInit( &p_dec->end_date, p_dec->vi.rate );
206 p_dec->p_aout = NULL;
207 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
209 &p_dec->output_format );
211 if( p_dec->p_aout_input == NULL )
213 msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
217 /* vorbis decoder thread's main loop */
218 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
220 DecodePacket( p_dec );
223 /* If b_error is set, the vorbis decoder thread enters the error loop */
224 if( p_dec->p_fifo->b_error )
226 DecoderError( p_dec->p_fifo );
229 /* End of the vorbis decoder thread */
230 CloseDecoder( p_dec );
235 DecoderError( p_fifo );
239 p_dec->p_fifo->b_error = 1;
241 /* End of the vorbis decoder thread */
242 CloseDecoder( p_dec );
249 /*****************************************************************************
250 * DecodePacket: decodes a Vorbis packet.
251 *****************************************************************************/
252 static void DecodePacket( dec_thread_t *p_dec )
254 aout_buffer_t *p_aout_buffer;
255 ogg_packet oggpacket;
256 #ifdef MODULE_NAME_IS_tremor
264 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
266 /* This should mean an eos */
270 /* Date management */
271 if( i_pts > 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
273 aout_DateSet( &p_dec->end_date, i_pts );
276 if( vorbis_synthesis( &p_dec->vb, &oggpacket ) == 0 )
277 vorbis_synthesis_blockin( &p_dec->vd, &p_dec->vb );
279 /* **pp_pcm is a multichannel float vector. In stereo, for
280 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
281 * the size of each channel. Convert the float values
282 * (-1.<=range<=1.) to whatever PCM format and write it out */
284 while( ( i_samples = vorbis_synthesis_pcmout( &p_dec->vd, &pp_pcm ) ) > 0 )
287 p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
291 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
292 p_dec->p_fifo->b_error = 1;
296 /* Interleave the samples */
297 #ifdef MODULE_NAME_IS_tremor
298 Interleave( (int32_t *)p_aout_buffer->p_buffer,
299 (const int32_t **)pp_pcm, p_dec->vi.channels, i_samples );
301 Interleave( (float *)p_aout_buffer->p_buffer,
302 (const float **)pp_pcm, p_dec->vi.channels, i_samples );
305 /* Tell libvorbis how many samples we actually consumed */
306 vorbis_synthesis_read( &p_dec->vd, i_samples );
308 /* Date management */
309 p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
310 p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
313 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
318 /*****************************************************************************
319 * GetOggPacket: get the following vorbis packet from the stream and send back
320 * the result in an ogg packet (for easy decoding by libvorbis).
321 *****************************************************************************
322 * Returns VLC_EGENERIC in case of eof.
323 *****************************************************************************/
324 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
327 if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
330 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
331 if( !p_dec->p_pes ) return VLC_EGENERIC;
333 p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
334 p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
335 p_oggpacket->granulepos = p_dec->p_pes->i_dts;
336 p_oggpacket->b_o_s = 0;
337 p_oggpacket->e_o_s = 0;
338 p_oggpacket->packetno = 0;
340 *p_pts = p_dec->p_pes->i_pts;
345 /*****************************************************************************
346 * Interleave: helper function to interleave channels
347 *****************************************************************************/
348 #ifdef MODULE_NAME_IS_tremor
349 static void Interleave( int32_t *p_out, const int32_t **pp_in,
351 static void Interleave( float *p_out, const float **pp_in,
353 int i_nb_channels, int i_samples )
357 for ( j = 0; j < i_samples; j++ )
359 for ( i = 0; i < i_nb_channels; i++ )
361 p_out[j * i_nb_channels + i] = pp_in[i][j];
366 /*****************************************************************************
367 * CloseDecoder: vorbis decoder destruction
368 *****************************************************************************/
369 static void CloseDecoder( dec_thread_t * p_dec )
371 if( p_dec->p_aout_input != NULL )
373 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
379 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
380 vorbis_block_clear( &p_dec->vb );
381 vorbis_dsp_clear( &p_dec->vd );
382 vorbis_comment_clear( &p_dec->vc );
383 vorbis_info_clear( &p_dec->vi ); /* must be called last */