]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
* modules/*: sanitization of the modules description strings.
[vlc] / modules / codec / vorbis.c
1 /*****************************************************************************
2  * vorbis.c: vorbis decoder module making use of libvorbis.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: vorbis.c,v 1.16 2003/03/30 18:14:36 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>                                    /* memcpy(), memset() */
29
30 #include <vlc/vlc.h>
31 #include <vlc/aout.h>
32 #include <vlc/decoder.h>
33 #include <input_ext-dec.h>
34
35 #include <vlc/input.h>
36
37 #include <ogg/ogg.h>
38 #ifdef MODULE_NAME_IS_tremor
39 #include <tremor/ivorbiscodec.h>
40 #else
41 #include <vorbis/codec.h>
42 #endif
43
44 /*****************************************************************************
45  * dec_thread_t : vorbis decoder thread descriptor
46  *****************************************************************************/
47 typedef struct dec_thread_t
48 {
49     /*
50      * Thread properties
51      */
52     vlc_thread_t        thread_id;                /* id for thread functions */
53
54     /*
55      * Vorbis properties
56      */
57     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
58                             settings */
59     vorbis_comment   vc; /* struct that stores all the bitstream user
60                           * comments */
61     vorbis_dsp_state vd; /* central working state for the packet->PCM
62                           * decoder */
63     vorbis_block     vb; /* local working space for packet->PCM decode */
64
65     /*
66      * Input properties
67      */
68     decoder_fifo_t         *p_fifo;            /* stores the PES stream data */
69     pes_packet_t           *p_pes;            /* current PES we are decoding */
70
71     /*
72      * Output properties
73      */
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;
78
79 } dec_thread_t;
80
81 static int pi_channels_maps[6] =
82 {
83     0,
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
90 };
91
92 /*****************************************************************************
93  * Local prototypes
94  *****************************************************************************/
95 static int  OpenDecoder  ( vlc_object_t * );
96 static int  RunDecoder   ( decoder_fifo_t * );
97 static void CloseDecoder ( dec_thread_t * );
98
99 static void DecodePacket ( dec_thread_t * );
100 static int  GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
101
102 #ifdef MODULE_NAME_IS_tremor
103 static void Interleave   ( int32_t *, const int32_t **, int, int );
104 #else
105 static void Interleave   ( float *, const float **, int, int );
106 #endif
107
108 /*****************************************************************************
109  * Module descriptor
110  *****************************************************************************/
111 vlc_module_begin();
112     set_description( _("Vorbis audio decoder") );
113 #ifdef MODULE_NAME_IS_tremor
114     set_capability( "decoder", 90 );
115 #else
116     set_capability( "decoder", 100 );
117 #endif
118     set_callbacks( OpenDecoder, NULL );
119 vlc_module_end();
120
121 /*****************************************************************************
122  * OpenDecoder: probe the decoder and return score
123  *****************************************************************************/
124 static int OpenDecoder( vlc_object_t *p_this )
125 {
126     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
127
128     if( p_fifo->i_fourcc != VLC_FOURCC('v','o','r','b') )
129     {
130         return VLC_EGENERIC;
131     }
132
133     p_fifo->pf_run = RunDecoder;
134     return VLC_SUCCESS;
135 }
136
137 /*****************************************************************************
138  * RunDecoder: the vorbis decoder
139  *****************************************************************************/
140 static int RunDecoder( decoder_fifo_t * p_fifo )
141 {
142     dec_thread_t *p_dec;
143     ogg_packet oggpacket;
144     mtime_t i_pts;
145
146     /* Allocate the memory needed to store the thread's structure */
147     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
148             == NULL)
149     {
150         msg_Err( p_fifo, "out of memory" );
151         goto error;
152     }
153
154     /* Initialize the thread properties */
155     memset( p_dec, 0, sizeof(dec_thread_t) );
156     p_dec->p_fifo = p_fifo;
157     p_dec->p_pes  = NULL;
158
159     /* Take care of the initial Vorbis header */
160     vorbis_info_init( &p_dec->vi );
161     vorbis_comment_init( &p_dec->vc );
162
163     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
164         goto error;
165
166     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
167     if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
168     {
169         msg_Err( p_dec->p_fifo, "This bitstream does not contain Vorbis "
170                  "audio data");
171         goto error;
172     }
173
174     /* The next two packets in order are the comment and codebook headers.
175        We need to watch out that these packets are not missing as a
176        missing or corrupted header is fatal. */
177     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
178         goto error;
179
180     if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
181     {
182         msg_Err( p_dec->p_fifo, "2nd Vorbis header is corrupted" );
183         goto error;
184     }
185     /* parse the vorbis comment. FIXME should be done in demuxer*/
186     {
187         input_thread_t *p_input = (input_thread_t *)p_fifo->p_parent;
188         input_info_category_t *p_cat = input_InfoCategory( p_input,
189                                                            _("Vorbis Comment") );
190         int i = 0;
191         char *psz_name, *psz_value, *psz_comment;
192         while ( i < p_dec->vc.comments )
193         {
194             psz_comment = strdup( p_dec->vc.user_comments[i] );
195             if ( !psz_comment )
196             {
197                 msg_Warn( p_dec->p_fifo, "Out of memory" );
198                 break;
199             }
200             psz_name = psz_comment;
201             psz_value = strchr( psz_comment, '=' );
202             if( psz_value )
203             {
204                 *psz_value = '\0';
205                 psz_value++;
206                 input_AddInfo( p_cat, psz_name, psz_value );
207             }
208             free( psz_comment );
209             i++;
210         }
211     }
212     
213     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
214         goto error;
215
216     if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
217     {
218         msg_Err( p_dec->p_fifo, "3rd Vorbis header is corrupted" );
219         goto error;
220     }
221
222     /* Initialize the Vorbis packet->PCM decoder */
223     vorbis_synthesis_init( &p_dec->vd, &p_dec->vi );
224     vorbis_block_init( &p_dec->vd, &p_dec->vb );
225
226 #ifdef MODULE_NAME_IS_tremor
227     p_dec->output_format.i_format = VLC_FOURCC('f','i','3','2');
228 #else
229     p_dec->output_format.i_format = VLC_FOURCC('f','l','3','2');
230 #endif
231     p_dec->output_format.i_physical_channels =
232         p_dec->output_format.i_original_channels =
233             pi_channels_maps[p_dec->vi.channels];
234     p_dec->output_format.i_rate = p_dec->vi.rate;
235
236     aout_DateInit( &p_dec->end_date, p_dec->vi.rate );
237     p_dec->p_aout = NULL;
238     p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
239                                        &p_dec->p_aout,
240                                        &p_dec->output_format );
241
242     if( p_dec->p_aout_input == NULL )
243     {
244         msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
245         goto error;
246     }
247
248     /* vorbis decoder thread's main loop */
249     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
250     {
251         DecodePacket( p_dec );
252     }
253
254     /* If b_error is set, the vorbis decoder thread enters the error loop */
255     if( p_dec->p_fifo->b_error )
256     {
257         DecoderError( p_dec->p_fifo );
258     }
259
260     /* End of the vorbis decoder thread */
261     CloseDecoder( p_dec );
262
263     return 0;
264
265  error:
266     DecoderError( p_fifo );
267     if( p_dec )
268     {
269         if( p_dec->p_fifo )
270             p_dec->p_fifo->b_error = 1;
271
272         /* End of the vorbis decoder thread */
273         CloseDecoder( p_dec );
274     }
275
276     return -1;
277
278 }
279
280 /*****************************************************************************
281  * DecodePacket: decodes a Vorbis packet.
282  *****************************************************************************/
283 static void DecodePacket( dec_thread_t *p_dec )
284 {
285     aout_buffer_t *p_aout_buffer;
286     ogg_packet    oggpacket;
287 #ifdef MODULE_NAME_IS_tremor
288     int32_t       **pp_pcm;
289 #else
290     float         **pp_pcm;
291 #endif
292     int           i_samples;
293     mtime_t       i_pts;
294
295     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
296     {
297         /* This should mean an eos */
298         return;
299     }
300
301     /* Date management */
302     if( i_pts > 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
303     {
304         aout_DateSet( &p_dec->end_date, i_pts );
305     }
306
307     if( vorbis_synthesis( &p_dec->vb, &oggpacket ) == 0 )
308         vorbis_synthesis_blockin( &p_dec->vd, &p_dec->vb );
309
310     /* **pp_pcm is a multichannel float vector. In stereo, for
311      * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
312      * the size of each channel. Convert the float values
313      * (-1.<=range<=1.) to whatever PCM format and write it out */
314
315     while( ( i_samples = vorbis_synthesis_pcmout( &p_dec->vd, &pp_pcm ) ) > 0 )
316     {
317
318         p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
319                                            i_samples );
320         if( !p_aout_buffer )
321         {
322             msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
323             p_dec->p_fifo->b_error = 1;
324             return;
325         }
326
327         /* Interleave the samples */
328 #ifdef MODULE_NAME_IS_tremor
329         Interleave( (int32_t *)p_aout_buffer->p_buffer,
330                     (const int32_t **)pp_pcm, p_dec->vi.channels, i_samples );
331 #else
332         Interleave( (float *)p_aout_buffer->p_buffer,
333                     (const float **)pp_pcm, p_dec->vi.channels, i_samples );
334 #endif
335
336         /* Tell libvorbis how many samples we actually consumed */
337         vorbis_synthesis_read( &p_dec->vd, i_samples );
338
339         /* Date management */
340         p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
341         p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
342                                                       i_samples );
343
344         aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
345     }
346
347 }
348
349 /*****************************************************************************
350  * GetOggPacket: get the following vorbis packet from the stream and send back
351  *               the result in an ogg packet (for easy decoding by libvorbis).
352  *****************************************************************************
353  * Returns VLC_EGENERIC in case of eof.
354  *****************************************************************************/
355 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
356                          mtime_t *p_pts )
357 {
358     if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
359                                         p_dec->p_pes );
360
361     input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
362     if( !p_dec->p_pes ) return VLC_EGENERIC;
363
364     p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
365     p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
366     p_oggpacket->granulepos = p_dec->p_pes->i_dts;
367     p_oggpacket->b_o_s = 0;
368     p_oggpacket->e_o_s = 0;
369     p_oggpacket->packetno = 0;
370
371     *p_pts = p_dec->p_pes->i_pts;
372
373     return VLC_SUCCESS;
374 }
375
376 /*****************************************************************************
377  * Interleave: helper function to interleave channels
378  *****************************************************************************/
379 #ifdef MODULE_NAME_IS_tremor
380 static void Interleave( int32_t *p_out, const int32_t **pp_in,
381 #else
382 static void Interleave( float *p_out, const float **pp_in,
383 #endif
384                         int i_nb_channels, int i_samples )
385 {
386     int i, j;
387
388     for ( j = 0; j < i_samples; j++ )
389     {
390         for ( i = 0; i < i_nb_channels; i++ )
391         {
392             p_out[j * i_nb_channels + i] = pp_in[i][j];
393         }
394     }
395 }
396
397 /*****************************************************************************
398  * CloseDecoder: vorbis decoder destruction
399  *****************************************************************************/
400 static void CloseDecoder( dec_thread_t * p_dec )
401 {
402     if( p_dec->p_aout_input != NULL )
403     {
404         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
405     }
406
407     if( p_dec )
408     {
409         if( p_dec->p_pes )
410             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
411         vorbis_block_clear( &p_dec->vb );
412         vorbis_dsp_clear( &p_dec->vd );
413         vorbis_comment_clear( &p_dec->vc );
414         vorbis_info_clear( &p_dec->vi );  /* must be called last */
415         free( p_dec );
416     }
417 }