]> git.sesse.net Git - vlc/blob - modules/codec/vorbis.c
Major change of the channels management. p_format->i_channels disappeares
[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.5 2002/11/14 22:38:47 massiot 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 #include <vorbis/codec.h>
39
40 /*****************************************************************************
41  * dec_thread_t : vorbis decoder thread descriptor
42  *****************************************************************************/
43 typedef struct dec_thread_t
44 {
45     /*
46      * Thread properties
47      */
48     vlc_thread_t        thread_id;                /* id for thread functions */
49
50     /*
51      * Vorbis properties
52      */
53     vorbis_info      vi; /* struct that stores all the static vorbis bitstream
54                             settings */
55     vorbis_comment   vc; /* struct that stores all the bitstream user
56                           * comments */
57     vorbis_dsp_state vd; /* central working state for the packet->PCM
58                           * decoder */
59     vorbis_block     vb; /* local working space for packet->PCM decode */
60
61     /*
62      * Input properties
63      */
64     decoder_fifo_t         *p_fifo;            /* stores the PES stream data */
65     pes_packet_t           *p_pes;            /* current PES we are decoding */
66
67     /*
68      * Output properties
69      */
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;
74
75 } dec_thread_t;
76
77 static int pi_channels_maps[6] =
78 {
79     0,
80     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
81     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
82     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
83     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
84      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
85 };
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int  OpenDecoder  ( vlc_object_t * );
91 static int  RunDecoder   ( decoder_fifo_t * );
92 static void CloseDecoder ( dec_thread_t * );
93
94 static void DecodePacket ( dec_thread_t * );
95 static int  GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
96
97 static void Interleave   ( float *, const float **, int, int );
98
99 /*****************************************************************************
100  * Module descriptor
101  *****************************************************************************/
102 vlc_module_begin();
103     set_description( _("Vorbis decoder module") );
104     set_capability( "decoder", 100 );
105     set_callbacks( OpenDecoder, NULL );
106 vlc_module_end();
107
108 /*****************************************************************************
109  * OpenDecoder: probe the decoder and return score
110  *****************************************************************************/
111 static int OpenDecoder( vlc_object_t *p_this )
112 {
113     decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
114
115     if( p_fifo->i_fourcc != VLC_FOURCC('v','o','r','b') )
116     {
117         return VLC_EGENERIC;
118     }
119
120     p_fifo->pf_run = RunDecoder;
121     return VLC_SUCCESS;
122 }
123
124 /*****************************************************************************
125  * RunDecoder: the vorbis decoder
126  *****************************************************************************/
127 static int RunDecoder( decoder_fifo_t * p_fifo )
128 {
129     dec_thread_t *p_dec;
130     ogg_packet oggpacket;
131     mtime_t i_pts;
132
133     /* Allocate the memory needed to store the thread's structure */
134     if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
135             == NULL)
136     {
137         msg_Err( p_fifo, "out of memory" );
138         goto error;
139     }
140
141     /* Initialize the thread properties */
142     memset( p_dec, 0, sizeof(dec_thread_t) );
143     p_dec->p_fifo = p_fifo;
144     p_dec->p_pes  = NULL;
145
146     /* Take care of the initial Vorbis header */
147     vorbis_info_init( &p_dec->vi );
148     vorbis_comment_init( &p_dec->vc );
149
150     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
151         goto error;
152
153     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
154     if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
155     {
156         msg_Err( p_dec->p_fifo, "This bitstream does not contain Vorbis "
157                  "audio data");
158         goto error;
159     }
160
161     /* The next two packets in order are the comment and codebook headers.
162        We need to watch out that these packets are not missing as a
163        missing or corrupted header is fatal. */
164     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
165         goto error;
166
167     if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
168     {
169         msg_Err( p_dec->p_fifo, "2nd Vorbis header is corrupted" );
170         goto error;
171     }
172
173     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
174         goto error;
175
176     if( vorbis_synthesis_headerin( &p_dec->vi, &p_dec->vc, &oggpacket ) < 0 )
177     {
178         msg_Err( p_dec->p_fifo, "3rd Vorbis header is corrupted" );
179         goto error;
180     }
181
182     /* Initialize the Vorbis packet->PCM decoder */
183     vorbis_synthesis_init( &p_dec->vd, &p_dec->vi );
184     vorbis_block_init( &p_dec->vd, &p_dec->vb );
185
186     p_dec->output_format.i_format = VLC_FOURCC('f','l','3','2');
187     p_dec->output_format.i_channels = pi_channels_maps[p_dec->vi.channels];
188     p_dec->output_format.i_rate = p_dec->vi.rate;
189
190     aout_DateInit( &p_dec->end_date, p_dec->vi.rate );
191     p_dec->p_aout = NULL;
192     p_dec->p_aout_input = aout_DecNew( p_dec->p_fifo,
193                                        &p_dec->p_aout,
194                                        &p_dec->output_format );
195
196     if( p_dec->p_aout_input == NULL )
197     {
198         msg_Err( p_dec->p_fifo, "failed to create aout fifo" );
199         goto error;
200     }
201
202     /* vorbis decoder thread's main loop */
203     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
204     {
205         DecodePacket( p_dec );
206     }
207
208     /* If b_error is set, the vorbis decoder thread enters the error loop */
209     if( p_dec->p_fifo->b_error )
210     {
211         DecoderError( p_dec->p_fifo );
212     }
213
214     /* End of the vorbis decoder thread */
215     CloseDecoder( p_dec );
216
217     return 0;
218
219  error:
220     DecoderError( p_fifo );
221     if( p_dec )
222     {
223         if( p_dec->p_fifo )
224             p_dec->p_fifo->b_error = 1;
225
226         /* End of the vorbis decoder thread */
227         CloseDecoder( p_dec );
228     }
229
230     return -1;
231
232 }
233
234 /*****************************************************************************
235  * DecodePacket: decodes a Vorbis packet.
236  *****************************************************************************/
237 static void DecodePacket( dec_thread_t *p_dec )
238 {
239     aout_buffer_t *p_aout_buffer;
240     ogg_packet    oggpacket;
241     float         **pp_pcm;
242     int           i_samples;
243     mtime_t       i_pts;
244
245     if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
246     {
247         /* This should mean an eos */
248         return;
249     }
250
251     /* Date management */
252     if( i_pts > 0 && i_pts != aout_DateGet( &p_dec->end_date ) )
253     {
254         aout_DateSet( &p_dec->end_date, i_pts );
255     }
256
257     if( vorbis_synthesis( &p_dec->vb, &oggpacket ) == 0 )
258         vorbis_synthesis_blockin( &p_dec->vd, &p_dec->vb );
259
260     /* **pp_pcm is a multichannel float vector. In stereo, for
261      * example, pp_pcm[0] is left, and pp_pcm[1] is right. i_samples is
262      * the size of each channel. Convert the float values
263      * (-1.<=range<=1.) to whatever PCM format and write it out */
264
265     while( ( i_samples = vorbis_synthesis_pcmout( &p_dec->vd, &pp_pcm ) ) > 0 )
266     {
267
268         p_aout_buffer = aout_DecNewBuffer( p_dec->p_aout, p_dec->p_aout_input,
269                                            i_samples );
270         if( !p_aout_buffer )
271         {
272             msg_Err( p_dec->p_fifo, "cannot get aout buffer" );
273             p_dec->p_fifo->b_error = 1;
274             return;
275         }
276
277         /* Interleave the samples */
278         Interleave( (float *)p_aout_buffer->p_buffer, (const float **)pp_pcm,
279                     p_dec->vi.channels, i_samples );
280
281         /* Tell libvorbis how many samples we actually consumed */
282         vorbis_synthesis_read( &p_dec->vd, i_samples );
283
284         /* Date management */
285         p_aout_buffer->start_date = aout_DateGet( &p_dec->end_date );
286         p_aout_buffer->end_date = aout_DateIncrement( &p_dec->end_date,
287                                                       i_samples );
288
289         aout_DecPlay( p_dec->p_aout, p_dec->p_aout_input, p_aout_buffer );
290     }
291
292 }
293
294 /*****************************************************************************
295  * GetOggPacket: get the following vorbis packet from the stream and send back
296  *               the result in an ogg packet (for easy decoding by libvorbis).
297  *****************************************************************************
298  * Returns VLC_EGENERIC in case of eof.
299  *****************************************************************************/
300 static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
301                          mtime_t *p_pts )
302 {
303     if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
304                                         p_dec->p_pes );
305
306     input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
307     if( !p_dec->p_pes ) return VLC_EGENERIC;
308
309     p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
310     p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
311     p_oggpacket->granulepos = p_dec->p_pes->i_dts;
312     p_oggpacket->b_o_s = 0;
313     p_oggpacket->e_o_s = 0;
314     p_oggpacket->packetno = 0;
315
316     *p_pts = p_dec->p_pes->i_pts;
317
318     return VLC_SUCCESS;
319 }
320
321 /*****************************************************************************
322  * Interleave: helper function to interleave channels
323  *****************************************************************************/
324 static void Interleave( float *p_out, const float **pp_in, int i_nb_channels,
325                         int i_samples )
326 {
327     int i, j;
328
329     for ( j = 0; j < i_samples; j++ )
330     {
331         for ( i = 0; i < i_nb_channels; i++ )
332         {
333             p_out[j * i_nb_channels + i] = pp_in[i][j];
334         }
335     }
336 }
337
338 /*****************************************************************************
339  * CloseDecoder: vorbis decoder destruction
340  *****************************************************************************/
341 static void CloseDecoder( dec_thread_t * p_dec )
342 {
343     if( p_dec->p_aout_input != NULL )
344     {
345         aout_DecDelete( p_dec->p_aout, p_dec->p_aout_input );
346     }
347
348     if( p_dec )
349     {
350         if( p_dec->p_pes )
351             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
352         vorbis_block_clear( &p_dec->vb );
353         vorbis_dsp_clear( &p_dec->vd );
354         vorbis_comment_clear( &p_dec->vc );
355         vorbis_info_clear( &p_dec->vi );  /* must be called last */
356         free( p_dec );
357     }
358 }