]> git.sesse.net Git - vlc/blob - modules/codec/speex.c
* all: only include header that are needed (and no more stdlib.h, string.h
[vlc] / modules / codec / speex.c
1 /*****************************************************************************
2  * speex.c: speex decoder/packetizer module making use of libspeex.
3  *****************************************************************************
4  * Copyright (C) 1999-2001 VideoLAN
5  * $Id: speex.c,v 1.4 2003/11/22 23:39:14 fenrir 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 <vlc/vlc.h>
28 #include <vlc/decoder.h>
29
30 #include <ogg/ogg.h>
31 #include <speex.h>
32 #include "speex_header.h"
33 #include "speex_stereo.h"
34 #include "speex_callbacks.h"
35
36 /*****************************************************************************
37  * decoder_sys_t : speex decoder descriptor
38  *****************************************************************************/
39 struct decoder_sys_t
40 {
41     /* Module mode */
42     vlc_bool_t b_packetizer;
43
44     /*
45      * Input properties
46      */
47     int i_headers;
48     int i_frame_in_packet;
49
50     /*
51      * Speex properties
52      */
53     SpeexBits bits;
54     SpeexHeader *p_header;
55     SpeexStereoState stereo;
56     void *p_state;
57
58     /*
59      * Common properties
60      */
61     audio_date_t end_date;
62
63 };
64
65 static int pi_channels_maps[6] =
66 {
67     0,
68     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
69     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
70     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
71      | AOUT_CHAN_REARRIGHT,
72     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
73      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
74 };
75
76 /****************************************************************************
77  * Local prototypes
78  ****************************************************************************/
79 static int  OpenDecoder   ( vlc_object_t * );
80 static int  OpenPacketizer( vlc_object_t * );
81 static void CloseDecoder  ( vlc_object_t * );
82
83 static void *DecodeBlock  ( decoder_t *, block_t ** );
84 static int  ProcessHeader ( decoder_t *, ogg_packet * );
85 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
86
87 static aout_buffer_t *DecodePacket( decoder_t *, ogg_packet * );
88 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
89
90 static void ParseSpeexComments( decoder_t *, ogg_packet * );
91
92 /*****************************************************************************
93  * Module descriptor
94  *****************************************************************************/
95 vlc_module_begin();
96     set_description( _("Speex audio decoder") );
97     set_capability( "decoder", 100 );
98     set_callbacks( OpenDecoder, CloseDecoder );
99
100     add_submodule();
101     set_description( _("Speex audio packetizer") );
102     set_capability( "packetizer", 100 );
103     set_callbacks( OpenPacketizer, CloseDecoder );
104 vlc_module_end();
105
106 /*****************************************************************************
107  * OpenDecoder: probe the decoder and return score
108  *****************************************************************************/
109 static int OpenDecoder( vlc_object_t *p_this )
110 {
111     decoder_t *p_dec = (decoder_t*)p_this;
112     decoder_sys_t *p_sys = p_dec->p_sys;
113
114     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','p','x',' ') )
115     {
116         return VLC_EGENERIC;
117     }
118
119     /* Allocate the memory needed to store the decoder's structure */
120     if( ( p_dec->p_sys = p_sys =
121           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
122     {
123         msg_Err( p_dec, "out of memory" );
124         return VLC_EGENERIC;
125     }
126     p_dec->p_sys->b_packetizer = VLC_FALSE;
127
128     aout_DateSet( &p_sys->end_date, 0 );
129
130     /* Set output properties */
131     p_dec->fmt_out.i_cat = AUDIO_ES;
132     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
133
134     /* Set callbacks */
135     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
136         DecodeBlock;
137     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
138         DecodeBlock;
139
140     p_sys->i_headers = 0;
141     p_sys->p_state = NULL;
142     p_sys->p_header = NULL;
143     p_sys->i_frame_in_packet = 0;
144
145     return VLC_SUCCESS;
146 }
147
148 static int OpenPacketizer( vlc_object_t *p_this )
149 {
150     decoder_t *p_dec = (decoder_t*)p_this;
151
152     int i_ret = OpenDecoder( p_this );
153
154     if( i_ret == VLC_SUCCESS )
155     {
156         p_dec->p_sys->b_packetizer = VLC_TRUE;
157         p_dec->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
158     }
159
160     return i_ret;
161 }
162
163 /****************************************************************************
164  * DecodeBlock: the whole thing
165  ****************************************************************************
166  * This function must be fed with ogg packets.
167  ****************************************************************************/
168 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
169 {
170     decoder_sys_t *p_sys = p_dec->p_sys;
171     ogg_packet oggpacket;
172
173     if( !pp_block ) return NULL;
174
175     if( *pp_block )
176     {
177         /* Block to Ogg packet */
178         oggpacket.packet = (*pp_block)->p_buffer;
179         oggpacket.bytes = (*pp_block)->i_buffer;
180     }
181     else
182     {
183         if( p_sys->b_packetizer ) return NULL;
184
185         /* Block to Ogg packet */
186         oggpacket.packet = NULL;
187         oggpacket.bytes = 0;
188     }
189
190     oggpacket.granulepos = -1;
191     oggpacket.b_o_s = 0;
192     oggpacket.e_o_s = 0;
193     oggpacket.packetno = 0;
194
195     if( p_sys->i_headers == 0 )
196     {
197         /* Take care of the initial Speex header */
198         if( ProcessHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
199         {
200             msg_Err( p_dec, "Initial Speex header is corrupted" );
201             block_Release( *pp_block );
202             return NULL;
203         }
204
205         p_sys->i_headers++;
206
207         return ProcessPacket( p_dec, &oggpacket, pp_block );
208     }
209
210     if( p_sys->i_headers == 1 )
211     {
212         /* The next packet in order is the comments header */
213         ParseSpeexComments( p_dec, &oggpacket );
214         p_sys->i_headers++;
215
216         return ProcessPacket( p_dec, &oggpacket, pp_block );
217     }
218
219     return ProcessPacket( p_dec, &oggpacket, pp_block );
220 }
221
222 /*****************************************************************************
223  * ProcessHeader: processes the inital Speex header packet.
224  *****************************************************************************/
225 static int ProcessHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
226 {
227     decoder_sys_t *p_sys = p_dec->p_sys;
228
229     void *p_state;
230     SpeexHeader *p_header;
231     SpeexMode *p_mode;
232     SpeexCallback callback;
233
234     p_sys->p_header = p_header =
235         speex_packet_to_header( p_oggpacket->packet, p_oggpacket->bytes );
236     if( !p_header )
237     {
238         msg_Err( p_dec, "Cannot read Speex header" );
239         return VLC_EGENERIC;
240     }
241     if( p_header->mode >= SPEEX_NB_MODES )
242     {
243         msg_Err( p_dec, "Mode number %d does not (yet/any longer) exist in "
244                  "this version of libspeex", p_header->mode );
245         return VLC_EGENERIC;
246     }
247
248     p_mode = speex_mode_list[p_header->mode];
249
250     if( p_header->speex_version_id > 1 )
251     {
252         msg_Err( p_dec, "This file was encoded with Speex bit-stream "
253                  "version %d, which I don't know how to decode",
254                  p_header->speex_version_id );
255         return VLC_EGENERIC;
256     }
257
258     if( p_mode->bitstream_version < p_header->mode_bitstream_version )
259     {
260         msg_Err( p_dec, "File encoded with a newer version of Speex" );
261         return VLC_EGENERIC;
262     }
263     if( p_mode->bitstream_version > p_header->mode_bitstream_version ) 
264     {
265         msg_Err( p_dec, "File encoded with an older version of Speex" );
266         return VLC_EGENERIC;
267     }
268    
269     msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s", 
270              p_header->rate, p_mode->modeName,
271              ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
272              p_header->vbr ? ", VBR)" : ")" );
273
274     /* Take care of speex decoder init */
275     speex_bits_init( &p_sys->bits );
276     p_sys->p_state = p_state = speex_decoder_init( p_mode );
277     if( !p_state )
278     {
279         msg_Err( p_dec, "Decoder initialization failed" );
280         return VLC_EGENERIC;
281     }
282
283     if( p_header->nb_channels == 2 )
284     {
285         SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
286         p_sys->stereo = stereo;
287         callback.callback_id = SPEEX_INBAND_STEREO;
288         callback.func = speex_std_stereo_request_handler;
289         callback.data = &p_sys->stereo;
290         speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
291     }
292
293     /* Setup the format */
294     p_dec->fmt_out.audio.i_physical_channels =
295         p_dec->fmt_out.audio.i_original_channels =
296             pi_channels_maps[p_header->nb_channels];
297     p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
298     p_dec->fmt_out.audio.i_rate = p_header->rate;
299
300     aout_DateInit( &p_sys->end_date, p_header->rate );
301
302     return VLC_SUCCESS;
303 }
304
305 /*****************************************************************************
306  * ProcessPacket: processes a Speex packet.
307  *****************************************************************************/
308 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
309                             block_t **pp_block )
310 {
311     decoder_sys_t *p_sys = p_dec->p_sys;
312     block_t *p_block = *pp_block;
313
314     /* Date management */
315     if( p_block && p_block->i_pts > 0 &&
316         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
317     {
318         aout_DateSet( &p_sys->end_date, p_block->i_pts );
319     }
320
321     if( !aout_DateGet( &p_sys->end_date ) )
322     {
323         /* We've just started the stream, wait for the first PTS. */
324         if( p_block ) block_Release( p_block );
325         return NULL;
326     }
327
328     *pp_block = NULL; /* To avoid being fed the same packet again */
329
330     if( p_sys->b_packetizer )
331     {
332          return SendPacket( p_dec, p_oggpacket, p_block );
333     }
334     else
335     {
336         aout_buffer_t *p_aout_buffer;
337
338         if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
339             p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
340         else
341             p_aout_buffer = NULL; /* Skip headers */
342
343         if( p_block )
344         {
345             block_Release( p_block );
346         }
347         return p_aout_buffer;
348     }
349 }
350
351 /*****************************************************************************
352  * DecodePacket: decodes a Speex packet.
353  *****************************************************************************/
354 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
355 {
356     decoder_sys_t *p_sys = p_dec->p_sys;
357
358     if( p_oggpacket->bytes )
359     {
360         /* Copy Ogg packet to Speex bitstream */
361         speex_bits_read_from( &p_sys->bits, p_oggpacket->packet,
362                               p_oggpacket->bytes );
363         p_sys->i_frame_in_packet = 0;
364     }
365
366     /* Decode one frame at a time */
367     if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
368     {
369         aout_buffer_t *p_aout_buffer;
370         int i_ret;
371
372         p_aout_buffer =
373             p_dec->pf_aout_buffer_new( p_dec, p_sys->p_header->frame_size );
374         if( !p_aout_buffer )
375         {
376             return NULL;
377         }
378
379         i_ret = speex_decode( p_sys->p_state, &p_sys->bits,
380                               (int16_t *)p_aout_buffer->p_buffer );
381         if( i_ret == -1 ) return NULL; /* End of stream */
382         if( i_ret== -2 )
383         {
384             msg_Warn( p_dec, "Decoding error: corrupted stream?" );
385             return NULL;
386         }
387
388         if( speex_bits_remaining( &p_sys->bits ) < 0 )
389         {
390             msg_Warn( p_dec, "Decoding overflow: corrupted stream?" );
391         }
392
393         if( p_sys->p_header->nb_channels == 2 )
394             speex_decode_stereo( (int16_t *)p_aout_buffer->p_buffer,
395                                  p_sys->p_header->frame_size, &p_sys->stereo );
396
397         /* Date management */
398         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
399         p_aout_buffer->end_date =
400             aout_DateIncrement( &p_sys->end_date, p_sys->p_header->frame_size);
401
402         p_sys->i_frame_in_packet++;
403
404         return p_aout_buffer;
405     }
406     else
407     {
408         return NULL;
409     }
410 }
411
412 /*****************************************************************************
413  * SendPacket: send an ogg packet to the stream output.
414  *****************************************************************************/
415 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
416                             block_t *p_block )
417 {
418     decoder_sys_t *p_sys = p_dec->p_sys;
419
420     /* Date management */
421     p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
422
423     if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
424         p_block->i_length =
425             aout_DateIncrement( &p_sys->end_date,
426                                 p_sys->p_header->frame_size ) -
427             p_block->i_pts;
428     else
429         p_block->i_length = 0;
430
431     return p_block;
432 }
433
434 /*****************************************************************************
435  * ParseSpeexComments: FIXME should be done in demuxer
436  *****************************************************************************/
437 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
438                            ((buf[base+2]<<16)&0xff0000)| \
439                            ((buf[base+1]<<8)&0xff00)| \
440                             (buf[base]&0xff))
441
442 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
443 {
444     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
445     decoder_sys_t *p_sys = p_dec->p_sys;
446
447     input_info_category_t *p_cat =
448         input_InfoCategory( p_input, _("Speex Comment") );
449
450     char *p_buf = (char *)p_oggpacket->packet;
451     SpeexMode *p_mode;
452     int i_len;
453
454     p_mode = speex_mode_list[p_sys->p_header->mode];
455     input_AddInfo( p_cat, _("Mode"), "%s%s",
456                    p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" );
457
458     if( p_oggpacket->bytes < 8 )
459     {
460         msg_Warn( p_dec, "Invalid/corrupted comments" );
461         return;
462     }
463
464     i_len = readint( p_buf, 0 ); p_buf += 4;
465     if( i_len > p_oggpacket->bytes - 4 )
466     {
467         msg_Warn( p_dec, "Invalid/corrupted comments" );
468         return;
469     }
470
471     input_AddInfo( p_cat, p_buf, "" );
472
473     /* TODO: finish comments parsing */
474 }
475
476 /*****************************************************************************
477  * CloseDecoder: speex decoder destruction
478  *****************************************************************************/
479 static void CloseDecoder( vlc_object_t *p_this )
480 {
481     decoder_t * p_dec = (decoder_t *)p_this;
482     decoder_sys_t *p_sys = p_dec->p_sys;
483
484     if( p_sys->p_state )
485     {
486         speex_decoder_destroy( p_sys->p_state );
487         speex_bits_destroy( &p_sys->bits );
488     }
489
490     if( p_sys->p_header ) free( p_sys->p_header );
491     free( p_sys );
492 }