1 /*****************************************************************************
2 * vorbis.c: vorbis decoder module making use of libvorbis.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: vorbis.c,v 1.2 2002/10/27 16:58:14 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 #include <vorbis/codec.h>
40 /*****************************************************************************
41 * dec_thread_t : vorbis decoder thread descriptor
42 *****************************************************************************/
43 typedef struct dec_thread_t
48 vlc_thread_t thread_id; /* id for thread functions */
53 vorbis_info vi; /* struct that stores all the static vorbis bitstream
55 vorbis_comment vc; /* struct that stores all the bitstream user
57 vorbis_dsp_state vd; /* central working state for the packet->PCM
59 vorbis_block vb; /* local working space for packet->PCM decode */
64 decoder_fifo_t *p_fifo; /* stores the PES stream data */
65 pes_packet_t *p_pes; /* current PES we are decoding */
70 aout_instance_t *p_aout;
71 aout_input_t *p_aout_input;
72 audio_sample_format_t output_format;
73 audio_date_t end_date;
77 /*****************************************************************************
79 *****************************************************************************/
80 static int OpenDecoder ( vlc_object_t * );
81 static int RunDecoder ( decoder_fifo_t * );
82 static void CloseDecoder ( dec_thread_t * );
84 static void DecodePacket ( dec_thread_t * );
85 static int GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
87 static void Interleave ( float *, const float **, int, int );
89 /*****************************************************************************
91 *****************************************************************************/
93 set_description( _("Vorbis decoder module") );
94 set_capability( "decoder", 100 );
95 set_callbacks( OpenDecoder, NULL );
98 /*****************************************************************************
99 * OpenDecoder: probe the decoder and return score
100 *****************************************************************************/
101 static int OpenDecoder( vlc_object_t *p_this )
103 decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
105 if( p_fifo->i_fourcc != VLC_FOURCC('v','o','r','b') )
110 p_fifo->pf_run = RunDecoder;
114 /*****************************************************************************
115 * RunDecoder: the vorbis decoder
116 *****************************************************************************/
117 static int RunDecoder( decoder_fifo_t * p_fifo )
120 ogg_packet oggpacket;
123 /* Allocate the memory needed to store the thread's structure */
124 if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
127 msg_Err( p_fifo, "out of memory" );
131 /* Initialize the thread properties */
132 p_dec->p_fifo = p_fifo;
135 /* Take care of the initial Vorbis header */
136 vorbis_info_init( &p_dec->vi );
137 vorbis_comment_init( &p_dec->vc );
139 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
142 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
143 if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
145 msg_Err( p_dec->p_fifo, "This bitstream does not contain Vorbis "
150 /* The next two packets in order are the comment and codebook headers.
151 We need to watch out that these packets are not missing as a
152 missing or corrupted header is fatal. */
153 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
156 if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
158 msg_Err( p_dec->p_fifo, "2nd Vorbis header is corrupted" );
162 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
165 if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
167 msg_Err( p_dec->p_fifo, "3rd Vorbis header is corrupted" );
171 /* Initialize the Vorbis packet->PCM decoder */
172 vorbis_synthesis_init( &p_dec->vd, &p_dec->vi );
173 vorbis_block_init( &p_dec->vd, &p_dec->vb );
175 p_dec->output_format.i_format = VLC_FOURCC('f','l','3','2');
176 p_dec->output_format.i_channels = p_dec->vi.channels;
177 p_dec->output_format.i_rate = p_dec->vi.rate;
179 aout_DateInit( &p_dec->end_date, p_dec->vi.rate );
180 p_dec->p_aout = NULL;
181 p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
183 &p_dec->output_format );
185 if( p_dec->p_aout_input == NULL )
187 msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
191 /* Take care of the first pts we receive. We need to be careful as a pts
192 * in vorbis language does in fact correspond to the presentation time of
193 * the _next_ packet to receive */
194 if( i_pts > 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
196 aout_DateSet( &p_dec->end_date, i_pts );
199 /* vorbis decoder thread's main loop */
200 while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
202 DecodePacket( p_dec );
205 /* If b_error is set, the vorbis decoder thread enters the error loop */
206 if( p_dec->p_fifo->b_error )
208 DecoderError( p_dec->p_fifo );
211 /* End of the vorbis decoder thread */
212 CloseDecoder( p_dec );
220 p_dec->p_fifo->b_error = 1;
224 DecoderError( p_fifo );
229 /*****************************************************************************
230 * DecodePacket: decodes a Vorbis packet.
231 *****************************************************************************/
232 static void DecodePacket( dec_thread_t *p_dec )
234 aout_buffer_t *p_aout_buffer;
235 ogg_packet oggpacket;
240 if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
242 /* This should mean an eos */
246 if( vorbis_synthesis( &p_dec->vb, &oggpacket ) == 0 )
247 vorbis_synthesis_blockin( &p_dec->vd, &p_dec->vb );
249 msg_Err( p_dec->p_fifo, "vorbis_synthesis error" );
251 /* **pp_pcm is a multichannel float vector. In stereo, for
252 * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
253 * the size of each channel. Convert the float values
254 * (-1.<=range<=1.) to whatever PCM format and write it out */
256 while( ( i_samples = vorbis_synthesis_pcmout( &p_dec->vd, &pp_pcm ) ) > 0 )
259 p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
263 msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
264 p_dec->p_fifo->b_error = 1;
268 /* Interleave the samples */
269 Interleave( (float *)p_aout_buffer->p_buffer, (const float **)pp_pcm,
270 p_dec->vi.channels, i_samples );
272 /* Tell libvorbis how many samples we actually consumed */
273 vorbis_synthesis_read( &p_dec->vd, i_samples );
275 /* Date management */
276 p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
277 p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
280 if( i_pts > 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
282 aout_DateSet( &p_dec->end_date, i_pts );
283 p_aout_buffer->end_date = aout_DateGet( &p_dec->end_date );
287 aout_DateSet( &p_dec->end_date, p_aout_buffer->end_date );
290 aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
295 /*****************************************************************************
296 * GetOggPacket: get the following vorbis packet from the stream and send back
297 * the result in an ogg packet (for easy decoding by libvorbis).
298 *****************************************************************************
299 * Returns VLC_EGENERIC in case of eof.
300 *****************************************************************************/
301 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
304 if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
307 input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
308 if( !p_dec->p_pes ) return VLC_EGENERIC;
310 p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
311 p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
312 p_oggpacket->granulepos = p_dec->p_pes->i_dts;
313 p_oggpacket->b_o_s = 0;
314 p_oggpacket->e_o_s = 0;
315 p_oggpacket->packetno = 0;
317 *p_pts = p_dec->p_pes->i_pts;
322 /*****************************************************************************
323 * Interleave: helper function to interleave channels
324 *****************************************************************************/
325 static void Interleave( float *p_out, const float **pp_in, int i_channels,
330 for ( j = 0; j < i_samples; j++ )
332 for ( i = 0; i < i_channels; i++ )
334 p_out[j * i_channels + i] = pp_in[i][j];
339 /*****************************************************************************
340 * CloseDecoder: vorbis decoder destruction
341 *****************************************************************************/
342 static void CloseDecoder( dec_thread_t * p_dec )
344 if( p_dec->p_aout_input != NULL )
346 aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
352 input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
353 vorbis_block_clear( &p_dec->vb );
354 vorbis_dsp_clear( &p_dec->vd );
355 vorbis_comment_clear( &p_dec->vc );
356 vorbis_info_clear( &p_dec->vi ); /* must be called last */