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