]> git.sesse.net Git - vlc/blob - modules/codec/speex.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / codec / speex.c
1 /*****************************************************************************
2  * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
3  *****************************************************************************
4  * Copyright (C) 2003-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_input.h>
34 #include <vlc_codec.h>
35 #include <vlc_aout.h>
36 #include "../demux/xiph.h"
37
38 #include <ogg/ogg.h>
39 #include <speex/speex.h>
40 #include <speex/speex_header.h>
41 #include <speex/speex_stereo.h>
42 #include <speex/speex_callbacks.h>
43
44 #include <assert.h>
45
46 /*****************************************************************************
47  * Module descriptor
48  *****************************************************************************/
49 static int  OpenDecoder   ( vlc_object_t * );
50 static int  OpenPacketizer( vlc_object_t * );
51 static void CloseDecoder  ( vlc_object_t * );
52 static int OpenEncoder   ( vlc_object_t * );
53 static void CloseEncoder ( vlc_object_t * );
54
55 #define ENC_CFG_PREFIX "sout-speex-"
56
57 #define ENC_MODE_TEXT N_("Mode" )
58 #define ENC_MODE_LONGTEXT N_( \
59     "Enforce the mode of the encoder." )
60
61 #define ENC_QUALITY_TEXT N_("Encoding quality")
62 #define ENC_QUALITY_LONGTEXT N_( \
63     "Enforce a quality between 0 (low) and 10 (high)." )
64
65 #define ENC_COMPLEXITY_TEXT N_("Encoding complexity" )
66 #define ENC_COMPLEXITY_LONGTEXT N_( \
67     "Enforce the complexity of the encoder." )
68
69 #define ENC_MAXBITRATE_TEXT N_( "Maximal bitrate" )
70 #define ENC_MAXBITRATE_LONGTEXT N_( \
71     "Enforce the maximal VBR bitrate" )
72
73 #define ENC_CBR_TEXT N_( "CBR encoding" )
74 #define ENC_CBR_LONGTEXT N_( \
75     "Enforce a constant bitrate encoding (CBR) instead of default " \
76     "variable bitrate encoding (VBR)." )
77
78 #define ENC_VAD_TEXT N_( "Voice activity detection" )
79 #define ENC_VAD_LONGTEXT N_( \
80     "Enable voice activity detection (VAD). It is automatically " \
81     "activated in VBR mode." )
82
83 #define ENC_DTX_TEXT N_( "Discontinuous Transmission" )
84 #define ENC_DTX_LONGTEXT N_( \
85     "Enable discontinuous transmission (DTX)." )
86
87 static const int pi_enc_mode_values[] = { 0, 1, 2 };
88 static const char * const ppsz_enc_mode_descriptions[] = {
89     N_("Narrow-band (8kHz)"), N_("Wide-band (16kHz)"), N_("Ultra-wideband (32kHz)"), NULL
90 };
91
92 vlc_module_begin ()
93     set_category( CAT_INPUT )
94     set_subcategory( SUBCAT_INPUT_ACODEC )
95
96     set_description( N_("Speex audio decoder") )
97     set_capability( "decoder", 100 )
98     set_shortname( N_("Speex") )
99     set_callbacks( OpenDecoder, CloseDecoder )
100
101     add_submodule ()
102     set_description( N_("Speex audio packetizer") )
103     set_capability( "packetizer", 100 )
104     set_callbacks( OpenPacketizer, CloseDecoder )
105
106     add_submodule ()
107     set_description( N_("Speex audio encoder") )
108     set_capability( "encoder", 100 )
109     set_callbacks( OpenEncoder, CloseEncoder )
110
111     add_integer( ENC_CFG_PREFIX "mode", 0, NULL, ENC_MODE_TEXT,
112                  ENC_MODE_LONGTEXT, false )
113         change_integer_list( pi_enc_mode_values, ppsz_enc_mode_descriptions, NULL )
114
115     add_integer( ENC_CFG_PREFIX "complexity", 3, NULL, ENC_COMPLEXITY_TEXT,
116                  ENC_COMPLEXITY_LONGTEXT, false )
117         change_integer_range( 1, 10 )
118
119     add_bool( ENC_CFG_PREFIX "cbr", false, NULL, ENC_CBR_TEXT,
120                  ENC_CBR_LONGTEXT, false )
121
122     add_float( ENC_CFG_PREFIX "quality", 8.0, NULL, ENC_QUALITY_TEXT,
123                ENC_QUALITY_LONGTEXT, false )
124         change_float_range( 0.0, 10.0 )
125
126     add_integer( ENC_CFG_PREFIX "max-bitrate", 0, NULL, ENC_MAXBITRATE_TEXT,
127                  ENC_MAXBITRATE_LONGTEXT, false )
128
129     add_bool( ENC_CFG_PREFIX "vad", true, NULL, ENC_VAD_TEXT,
130                  ENC_VAD_LONGTEXT, false )
131
132     add_bool( ENC_CFG_PREFIX "dtx", false, NULL, ENC_DTX_TEXT,
133                  ENC_DTX_LONGTEXT, false )
134
135     /* TODO agc, noise suppression, */
136
137 vlc_module_end ()
138
139 static const char *const ppsz_enc_options[] = {
140     "mode", "complexity", "cbr", "quality", "max-bitrate", "vad", "dtx", NULL
141 };
142
143 /*****************************************************************************
144  * decoder_sys_t : speex decoder descriptor
145  *****************************************************************************/
146 struct decoder_sys_t
147 {
148     /* Module mode */
149     bool b_packetizer;
150
151     /*
152      * Input properties
153      */
154     bool b_has_headers;
155     int i_frame_in_packet;
156
157     /*
158      * Speex properties
159      */
160     SpeexBits bits;
161     SpeexHeader *p_header;
162     SpeexStereoState stereo;
163     void *p_state;
164     unsigned int rtp_rate;
165
166     /*
167      * Common properties
168      */
169     date_t end_date;
170
171 };
172
173 static const int pi_channels_maps[6] =
174 {
175     0,
176     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
177     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
178     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
179      | AOUT_CHAN_REARRIGHT,
180     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
181      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
182 };
183
184 /****************************************************************************
185  * Local prototypes
186  ****************************************************************************/
187
188 static void *DecodeBlock  ( decoder_t *, block_t ** );
189 static aout_buffer_t *DecodeRtpSpeexPacket( decoder_t *, block_t **);
190 static int  ProcessHeaders( decoder_t * );
191 static int  ProcessInitialHeader ( decoder_t *, ogg_packet * );
192 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
193
194 static aout_buffer_t *DecodePacket( decoder_t *, ogg_packet * );
195 static block_t *SendPacket( decoder_t *, block_t * );
196
197 static void ParseSpeexComments( decoder_t *, ogg_packet * );
198
199 static block_t *Encode   ( encoder_t *, aout_buffer_t * );
200
201 /*****************************************************************************
202  * OpenDecoder: probe the decoder and return score
203  *****************************************************************************/
204 static int OpenDecoder( vlc_object_t *p_this )
205 {
206     decoder_t *p_dec = (decoder_t*)p_this;
207     decoder_sys_t *p_sys;
208
209     if( p_dec->fmt_in.i_codec != VLC_CODEC_SPEEX )
210         return VLC_EGENERIC;
211
212     /* Allocate the memory needed to store the decoder's structure */
213     if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
214         return VLC_ENOMEM;
215     p_dec->p_sys->bits.buf_size = 0;
216     p_dec->p_sys->b_packetizer = false;
217     p_dec->p_sys->rtp_rate = p_dec->fmt_in.audio.i_rate;
218     p_dec->p_sys->b_has_headers = false;
219
220     date_Set( &p_sys->end_date, 0 );
221
222     /* Set output properties */
223     p_dec->fmt_out.i_cat = AUDIO_ES;
224     p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
225
226     /*
227       Set callbacks
228       If the codec is spxr then this decoder is 
229       being invoked on a Speex stream arriving via RTP. 
230       A special decoder callback is used.
231     */
232     if (p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('s', 'p', 'x', 'r'))
233     {
234         msg_Dbg( p_dec, "Using RTP version of Speex decoder @ rate %d.", 
235             p_dec->fmt_in.audio.i_rate );
236         p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
237             DecodeRtpSpeexPacket;
238     }
239     else
240     {
241         p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
242             DecodeBlock;
243     }
244     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
245         DecodeBlock;
246
247     p_sys->p_state = NULL;
248     p_sys->p_header = NULL;
249     p_sys->i_frame_in_packet = 0;
250
251     return VLC_SUCCESS;
252 }
253
254 static int OpenPacketizer( vlc_object_t *p_this )
255 {
256     decoder_t *p_dec = (decoder_t*)p_this;
257
258     int i_ret = OpenDecoder( p_this );
259
260     if( i_ret == VLC_SUCCESS )
261     {
262         p_dec->p_sys->b_packetizer = true;
263         p_dec->fmt_out.i_codec = VLC_CODEC_SPEEX;
264     }
265
266     return i_ret;
267 }
268
269 /****************************************************************************
270  * DecodeBlock: the whole thing
271  ****************************************************************************
272  * This function must be fed with ogg packets.
273  ****************************************************************************/
274 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
275 {
276     decoder_sys_t *p_sys = p_dec->p_sys;
277     ogg_packet oggpacket;
278
279     if( !pp_block ) return NULL;
280
281     if( *pp_block )
282     {
283         /* Block to Ogg packet */
284         oggpacket.packet = (*pp_block)->p_buffer;
285         oggpacket.bytes = (*pp_block)->i_buffer;
286     }
287     else
288     {
289         if( p_sys->b_packetizer ) return NULL;
290
291         /* Block to Ogg packet */
292         oggpacket.packet = NULL;
293         oggpacket.bytes = 0;
294     }
295
296     oggpacket.granulepos = -1;
297     oggpacket.b_o_s = 0;
298     oggpacket.e_o_s = 0;
299     oggpacket.packetno = 0;
300
301     /* Check for headers */
302     if( !p_sys->b_has_headers )
303     {
304         if( ProcessHeaders( p_dec ) )
305         {
306             block_Release( *pp_block );
307             return NULL;
308         }
309         p_sys->b_has_headers = true;
310     }
311
312     return ProcessPacket( p_dec, &oggpacket, pp_block );
313 }
314
315 /*****************************************************************************
316  * ProcessHeaders: process Speex headers.
317  *****************************************************************************/
318 static int ProcessHeaders( decoder_t *p_dec )
319 {
320     decoder_sys_t *p_sys = p_dec->p_sys;
321     ogg_packet oggpacket;
322
323     unsigned pi_size[XIPH_MAX_HEADER_COUNT];
324     void     *pp_data[XIPH_MAX_HEADER_COUNT];
325     unsigned i_count;
326     if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
327                            p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
328         return VLC_EGENERIC;
329     if( i_count < 2 )
330         goto error;
331
332     oggpacket.granulepos = -1;
333     oggpacket.e_o_s = 0;
334     oggpacket.packetno = 0;
335
336     /* Take care of the initial Vorbis header */
337     oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
338     oggpacket.bytes  = pi_size[0];
339     oggpacket.packet = pp_data[0];
340     if( ProcessInitialHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
341     {
342         msg_Err( p_dec, "initial Speex header is corrupted" );
343         goto error;
344     }
345
346     /* The next packet in order is the comments header */
347     oggpacket.b_o_s = 0;
348     oggpacket.bytes  = pi_size[1];
349     oggpacket.packet = pp_data[1];
350     ParseSpeexComments( p_dec, &oggpacket );
351
352     if( p_sys->b_packetizer )
353     {
354         p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
355         p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
356                                                   p_dec->fmt_out.i_extra );
357         memcpy( p_dec->fmt_out.p_extra,
358                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
359     }
360
361     for( unsigned i = 0; i < i_count; i++ )
362         free( pp_data[i] );
363     return VLC_SUCCESS;
364
365 error:
366     for( unsigned i = 0; i < i_count; i++ )
367         free( pp_data[i] );
368     return VLC_EGENERIC;
369 }
370
371 /*****************************************************************************
372  * ProcessInitialHeader: processes the inital Speex header packet.
373  *****************************************************************************/
374 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
375 {
376     decoder_sys_t *p_sys = p_dec->p_sys;
377
378     void *p_state;
379     SpeexHeader *p_header;
380     const SpeexMode *p_mode;
381     SpeexCallback callback;
382
383     p_sys->p_header = p_header =
384         speex_packet_to_header( (char *)p_oggpacket->packet,
385                                 p_oggpacket->bytes );
386     if( !p_header )
387     {
388         msg_Err( p_dec, "cannot read Speex header" );
389         return VLC_EGENERIC;
390     }
391     if( p_header->mode >= SPEEX_NB_MODES || p_header->mode < 0 )
392     {
393         msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
394                  "this version of libspeex.", p_header->mode );
395         return VLC_EGENERIC;
396     }
397
398     p_mode = speex_mode_list[p_header->mode];
399     if( p_mode == NULL )
400         return VLC_EGENERIC;
401
402     if( p_header->speex_version_id > 1 )
403     {
404         msg_Err( p_dec, "this file was encoded with Speex bit-stream "
405                  "version %d which is not supported by this decoder.",
406                  p_header->speex_version_id );
407         return VLC_EGENERIC;
408     }
409
410     if( p_mode->bitstream_version < p_header->mode_bitstream_version )
411     {
412         msg_Err( p_dec, "file encoded with a newer version of Speex." );
413         return VLC_EGENERIC;
414     }
415     if( p_mode->bitstream_version > p_header->mode_bitstream_version )
416     {
417         msg_Err( p_dec, "file encoded with an older version of Speex." );
418         return VLC_EGENERIC;
419     }
420
421     msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
422              p_header->rate, p_mode->modeName,
423              ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
424              p_header->vbr ? ", VBR)" : ")" );
425
426     /* Take care of speex decoder init */
427     speex_bits_init( &p_sys->bits );
428     p_sys->p_state = p_state = speex_decoder_init( p_mode );
429     if( !p_state )
430     {
431         msg_Err( p_dec, "decoder initialization failed" );
432         return VLC_EGENERIC;
433     }
434
435     if( p_header->nb_channels == 2 )
436     {
437         SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
438         p_sys->stereo = stereo;
439         callback.callback_id = SPEEX_INBAND_STEREO;
440         callback.func = speex_std_stereo_request_handler;
441         callback.data = &p_sys->stereo;
442         speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
443     }
444     if( p_header->nb_channels <= 0 ||
445         p_header->nb_channels > 5 )
446     {
447         msg_Err( p_dec, "invalid number of channels (not between 1 and 5): %i",
448                  p_header->nb_channels );
449         return VLC_EGENERIC;
450     }
451
452     /* Setup the format */
453     p_dec->fmt_out.audio.i_physical_channels =
454         p_dec->fmt_out.audio.i_original_channels =
455             pi_channels_maps[p_header->nb_channels];
456     p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
457     p_dec->fmt_out.audio.i_rate = p_header->rate;
458
459     date_Init( &p_sys->end_date, p_header->rate, 1 );
460
461     return VLC_SUCCESS;
462 }
463
464 /*****************************************************************************
465  * ProcessPacket: processes a Speex packet.
466  *****************************************************************************/
467 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
468                             block_t **pp_block )
469 {
470     decoder_sys_t *p_sys = p_dec->p_sys;
471     block_t *p_block = *pp_block;
472
473     /* Date management */
474     if( p_block && p_block->i_pts > VLC_TS_INVALID &&
475         p_block->i_pts != date_Get( &p_sys->end_date ) )
476     {
477         date_Set( &p_sys->end_date, p_block->i_pts );
478     }
479
480     if( !date_Get( &p_sys->end_date ) )
481     {
482         /* We've just started the stream, wait for the first PTS. */
483         if( p_block ) block_Release( p_block );
484         return NULL;
485     }
486
487     *pp_block = NULL; /* To avoid being fed the same packet again */
488
489     if( p_sys->b_packetizer )
490     {
491         if ( p_sys->p_header->frames_per_packet > 1 )
492         {
493             short *p_frame_holder = NULL;
494             int i_bits_before = 0, i_bits_after = 0, i_bytes_in_speex_frame = 0,
495                 i_pcm_output_size = 0, i_bits_in_speex_frame = 0;
496             block_t *p_new_block = NULL;
497
498             i_pcm_output_size = p_sys->p_header->frame_size;
499             p_frame_holder = (short*)xmalloc( sizeof(short)*i_pcm_output_size );
500
501             speex_bits_read_from( &p_sys->bits, (char*)p_oggpacket->packet,
502                 p_oggpacket->bytes);
503             i_bits_before = speex_bits_remaining( &p_sys->bits );
504             speex_decode_int(p_sys->p_state, &p_sys->bits, p_frame_holder);
505             i_bits_after = speex_bits_remaining( &p_sys->bits );
506
507             i_bits_in_speex_frame = i_bits_before - i_bits_after;
508             i_bytes_in_speex_frame = ( i_bits_in_speex_frame + 
509                 (8 - (i_bits_in_speex_frame % 8)) )
510                 / 8;
511
512             p_new_block = block_New( p_dec, i_bytes_in_speex_frame );
513             memset( p_new_block->p_buffer, 0xff, i_bytes_in_speex_frame );
514
515             /*
516              * Copy the first frame in this packet to a new packet.
517              */
518             speex_bits_rewind( &p_sys->bits );
519             speex_bits_write( &p_sys->bits, 
520                 (char*)p_new_block->p_buffer, 
521                     (int)i_bytes_in_speex_frame );
522
523             /*
524              * Move the remaining part of the original packet (subsequent
525              * frames, if there are any) into the beginning 
526              * of the original packet so
527              * they are preserved following the realloc. 
528              * Note: Any bits that
529              * remain in the initial packet
530              * are "filler" if they do not constitute
531              * an entire byte. 
532              */
533             if ( i_bits_after > 7 )
534             {
535                 /* round-down since we rounded-up earlier (to include
536                  * the speex terminator code. 
537                  */
538                 i_bytes_in_speex_frame--;
539                 speex_bits_write( &p_sys->bits, 
540                         (char*)p_block->p_buffer, 
541                         p_block->i_buffer - i_bytes_in_speex_frame );
542             p_block = block_Realloc( p_block, 
543                     0, 
544                         p_block->i_buffer-i_bytes_in_speex_frame );
545                 *pp_block = p_block;
546             }
547             else
548             {
549                 speex_bits_reset( &p_sys->bits );
550             }
551
552             free( p_frame_holder );
553             return SendPacket( p_dec, p_new_block);
554         }
555         else
556         {
557             return SendPacket( p_dec, p_block );
558         }
559     }
560     else
561     {
562         aout_buffer_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
563
564         if( p_block )
565             block_Release( p_block );
566         return p_aout_buffer;
567     }
568 }
569
570 static aout_buffer_t *DecodeRtpSpeexPacket( decoder_t *p_dec, block_t **pp_block )
571 {
572     block_t *p_speex_bit_block = *pp_block;
573     decoder_sys_t *p_sys = p_dec->p_sys;
574     aout_buffer_t *p_aout_buffer;
575     int i_decode_ret;
576     unsigned int i_speex_frame_size;
577
578     if ( !p_speex_bit_block || p_speex_bit_block->i_pts <= VLC_TS_INVALID )
579         return NULL;
580
581     /* 
582       If the SpeexBits buffer size is 0 (a default value),
583       we know that a proper initialization has not yet been done.
584     */
585     if ( p_sys->bits.buf_size==0 )
586     {
587         p_sys->p_header = (SpeexHeader *)malloc(sizeof(SpeexHeader));
588         if ( !p_sys->p_header )
589         {
590             msg_Err( p_dec, "Could not allocate a Speex header.");
591             return NULL;
592         }
593         speex_init_header( p_sys->p_header,p_sys->rtp_rate,1,&speex_nb_mode );
594         speex_bits_init( &p_sys->bits );
595         p_sys->p_state = speex_decoder_init( &speex_nb_mode );
596         if ( !p_sys->p_state )
597         {
598             msg_Err( p_dec, "Could not allocate a Speex decoder." );
599             free( p_sys->p_header );
600             return NULL;
601         }
602
603         /*
604           Assume that variable bit rate is enabled. Also assume
605           that there is only one frame per packet. 
606         */
607         p_sys->p_header->vbr = 1;
608         p_sys->p_header->frames_per_packet = 1;
609
610         p_dec->fmt_out.audio.i_channels = p_sys->p_header->nb_channels;
611         p_dec->fmt_out.audio.i_physical_channels = 
612         p_dec->fmt_out.audio.i_original_channels = 
613             pi_channels_maps[p_sys->p_header->nb_channels];
614         p_dec->fmt_out.audio.i_rate = p_sys->p_header->rate;
615
616         if ( speex_mode_query( &speex_nb_mode, 
617             SPEEX_MODE_FRAME_SIZE, 
618             &i_speex_frame_size ) )
619         {
620             msg_Err( p_dec, "Could not determine the frame size." );
621             speex_decoder_destroy( p_sys->p_state );
622             free( p_sys->p_header );
623             return NULL;
624         }
625         p_dec->fmt_out.audio.i_bytes_per_frame = i_speex_frame_size;
626
627         date_Init(&p_sys->end_date, p_sys->p_header->rate, 1);
628     }
629
630     /* 
631       If the SpeexBits are initialized but there is 
632       still no header, an error must be thrown.
633     */
634     if ( !p_sys->p_header )
635     {
636         msg_Err( p_dec, "There is no valid Speex header found." );
637         return NULL;
638     }
639     *pp_block = NULL;
640
641     if ( !date_Get( &p_sys->end_date ) )
642         date_Set( &p_sys->end_date, p_speex_bit_block->i_dts );
643
644     /*
645       Ask for a new audio output buffer and make sure
646       we get one. 
647     */
648     p_aout_buffer = decoder_NewAudioBuffer( p_dec, 
649         p_sys->p_header->frame_size );
650     if ( !p_aout_buffer || p_aout_buffer->i_buffer == 0 )
651     {
652         msg_Err(p_dec, "Oops: No new buffer was returned!");
653         return NULL;
654     }
655
656     /*
657       Read the Speex payload into the SpeexBits buffer.
658     */
659     speex_bits_read_from( &p_sys->bits, 
660         (char*)p_speex_bit_block->p_buffer, 
661         p_speex_bit_block->i_buffer );
662     
663     /* 
664       Decode the input and ensure that no errors 
665       were encountered.
666     */
667     i_decode_ret = speex_decode_int( p_sys->p_state, &p_sys->bits, 
668             (int16_t*)p_aout_buffer->p_buffer );
669     if ( i_decode_ret < 0 )
670     {
671         msg_Err( p_dec, "Decoding failed. Perhaps we have a bad stream?" );
672         return NULL;
673     }
674
675     /* 
676       Handle date management on the audio output buffer. 
677     */
678     p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
679     p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
680         p_sys->p_header->frame_size ) - p_aout_buffer->i_pts;
681     
682     
683     p_sys->i_frame_in_packet++;
684     block_Release( p_speex_bit_block );
685
686     return p_aout_buffer;
687 }
688
689 /*****************************************************************************
690  * DecodePacket: decodes a Speex packet.
691  *****************************************************************************/
692 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
693 {
694     decoder_sys_t *p_sys = p_dec->p_sys;
695
696     if( p_oggpacket->bytes )
697     {
698         /* Copy Ogg packet to Speex bitstream */
699         speex_bits_read_from( &p_sys->bits, (char *)p_oggpacket->packet,
700                               p_oggpacket->bytes );
701         p_sys->i_frame_in_packet = 0;
702     }
703
704     /* Decode one frame at a time */
705     if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
706     {
707         aout_buffer_t *p_aout_buffer;
708         if( p_sys->p_header->frame_size == 0 )
709             return NULL;
710
711         p_aout_buffer =
712             decoder_NewAudioBuffer( p_dec, p_sys->p_header->frame_size );
713         if( !p_aout_buffer )
714         {
715             return NULL;
716         }
717
718         switch( speex_decode_int( p_sys->p_state, &p_sys->bits,
719                                   (int16_t *)p_aout_buffer->p_buffer ) )
720         {
721             case -2:
722                 msg_Err( p_dec, "decoding error: corrupted stream?" );
723             case -1: /* End of stream */
724                 return NULL;
725         }
726
727         if( speex_bits_remaining( &p_sys->bits ) < 0 )
728         {
729             msg_Err( p_dec, "decoding overflow: corrupted stream?" );
730         }
731
732         if( p_sys->p_header->nb_channels == 2 )
733             speex_decode_stereo_int( (int16_t *)p_aout_buffer->p_buffer,
734                                      p_sys->p_header->frame_size,
735                                      &p_sys->stereo );
736
737         /* Date management */
738         p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
739         p_aout_buffer->i_length =
740             date_Increment( &p_sys->end_date, p_sys->p_header->frame_size )
741             - p_aout_buffer->i_pts;
742
743         p_sys->i_frame_in_packet++;
744
745         return p_aout_buffer;
746     }
747     else
748     {
749         return NULL;
750     }
751 }
752
753 /*****************************************************************************
754  * SendPacket: send an ogg packet to the stream output.
755  *****************************************************************************/
756 static block_t *SendPacket( decoder_t *p_dec, block_t *p_block )
757 {
758     decoder_sys_t *p_sys = p_dec->p_sys;
759
760     /* Date management */
761     p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
762
763     p_block->i_length =
764         date_Increment( &p_sys->end_date,
765                             p_sys->p_header->frame_size ) -
766         p_block->i_pts;
767
768     return p_block;
769 }
770
771 /*****************************************************************************
772  * ParseSpeexComments:
773  *****************************************************************************/
774 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
775                            ((buf[base+2]<<16)&0xff0000)| \
776                            ((buf[base+1]<<8)&0xff00)| \
777                             (buf[base]&0xff))
778
779 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
780 {
781     decoder_sys_t *p_sys = p_dec->p_sys;
782     const SpeexMode *p_mode;
783
784     assert( p_sys->p_header->mode < SPEEX_NB_MODES );
785
786     p_mode = speex_mode_list[p_sys->p_header->mode];
787     assert( p_mode != NULL );
788
789     if( !p_dec->p_description )
790     {
791         p_dec->p_description = vlc_meta_New();
792         if( !p_dec->p_description )
793             return;
794     }
795
796     /* */
797     char *psz_mode;
798     if( asprintf( &psz_mode, "%s%s", p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" ) >= 0 )
799     {
800         vlc_meta_AddExtra( p_dec->p_description, _("Mode"), psz_mode );
801         free( psz_mode );
802     }
803
804     /* TODO: finish comments parsing */
805     VLC_UNUSED( p_oggpacket );
806 }
807
808 /*****************************************************************************
809  * CloseDecoder: speex decoder destruction
810  *****************************************************************************/
811 static void CloseDecoder( vlc_object_t *p_this )
812 {
813     decoder_t * p_dec = (decoder_t *)p_this;
814     decoder_sys_t *p_sys = p_dec->p_sys;
815
816     if( p_sys->p_state )
817     {
818         speex_decoder_destroy( p_sys->p_state );
819         speex_bits_destroy( &p_sys->bits );
820     }
821
822     free( p_sys->p_header );
823     free( p_sys );
824 }
825
826 /*****************************************************************************
827  * encoder_sys_t: encoder descriptor
828  *****************************************************************************/
829 #define MAX_FRAME_SIZE  2000
830 #define MAX_FRAME_BYTES 2000
831
832 struct encoder_sys_t
833 {
834     /*
835      * Input properties
836      */
837     char *p_buffer;
838     char p_buffer_out[MAX_FRAME_BYTES];
839
840     /*
841      * Speex properties
842      */
843     SpeexBits bits;
844     SpeexHeader header;
845     SpeexStereoState stereo;
846     void *p_state;
847
848     int i_frames_per_packet;
849     int i_frames_in_packet;
850
851     int i_frame_length;
852     int i_samples_delay;
853     int i_frame_size;
854 };
855
856 /*****************************************************************************
857  * OpenEncoder: probe the encoder and return score
858  *****************************************************************************/
859 static int OpenEncoder( vlc_object_t *p_this )
860 {
861     encoder_t *p_enc = (encoder_t *)p_this;
862     encoder_sys_t *p_sys;
863     const SpeexMode *p_speex_mode = &speex_nb_mode;
864     int i_tmp, i;
865     const char *pp_header[2];
866     int pi_header[2];
867     uint8_t *p_extra;
868
869     if( p_enc->fmt_out.i_codec != VLC_CODEC_SPEEX &&
870         !p_enc->b_force )
871     {
872         return VLC_EGENERIC;
873     }
874
875     config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
876     switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) )
877     {
878     case 1:
879         msg_Dbg( p_enc, "Using wideband" );
880         p_speex_mode = &speex_wb_mode;
881         break;
882     case 2:
883         msg_Dbg( p_enc, "Using ultra-wideband" );
884         p_speex_mode = &speex_uwb_mode;
885         break;
886     default:
887         msg_Dbg( p_enc, "Using narrowband" );
888         p_speex_mode = &speex_nb_mode;
889         break;
890     }
891
892     /* Allocate the memory needed to store the decoder's structure */
893     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
894         return VLC_ENOMEM;
895     p_enc->p_sys = p_sys;
896     p_enc->pf_encode_audio = Encode;
897     p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
898     p_enc->fmt_out.i_codec = VLC_CODEC_SPEEX;
899
900     speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
901                        1, p_speex_mode );
902
903     p_sys->header.frames_per_packet = 1;
904     p_sys->header.vbr = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
905     p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
906
907     /* Create a new encoder state in narrowband mode */
908     p_sys->p_state = speex_encoder_init( p_speex_mode );
909
910     /* Parameters */
911     i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "complexity" );
912     speex_encoder_ctl( p_sys->p_state, SPEEX_SET_COMPLEXITY, &i_tmp );
913
914     i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
915     speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR, &i_tmp );
916
917     if( i_tmp == 0 ) /* CBR */
918     {
919         i_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
920         speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_tmp );
921
922         i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "vad" ) ? 1 : 0;
923         speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VAD, &i_tmp );
924     }
925     else
926     {
927         float f_tmp;
928
929         f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
930         speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_QUALITY, &f_tmp );
931
932         i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
933         if( i_tmp > 0 )
934 #ifdef SPEEX_SET_VBR_MAX_BITRATE
935             speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_MAX_BITRATE, &i_tmp );
936 #else
937             msg_Dbg( p_enc, "max-bitrate cannot be set in this version of libspeex");
938 #endif
939     }
940
941     i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "dtx" ) ? 1 : 0;
942     speex_encoder_ctl( p_sys->p_state, SPEEX_SET_DTX, &i_tmp );
943
944
945     /*Initialization of the structure that holds the bits*/
946     speex_bits_init( &p_sys->bits );
947
948     p_sys->i_frames_in_packet = 0;
949     p_sys->i_samples_delay = 0;
950
951     speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
952                        &p_sys->i_frame_length );
953
954     p_sys->i_frame_size = p_sys->i_frame_length *
955         sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
956     p_sys->p_buffer = xmalloc( p_sys->i_frame_size );
957
958     /* Create and store headers */
959     pp_header[0] = speex_header_to_packet( &p_sys->header, &pi_header[0] );
960     pp_header[1] = "ENCODER=VLC media player";
961     pi_header[1] = sizeof("ENCODER=VLC media player");
962
963     p_enc->fmt_out.i_extra = 3 * 2 + pi_header[0] + pi_header[1];
964     p_extra = p_enc->fmt_out.p_extra = xmalloc( p_enc->fmt_out.i_extra );
965     for( i = 0; i < 2; i++ )
966     {
967         *(p_extra++) = pi_header[i] >> 8;
968         *(p_extra++) = pi_header[i] & 0xFF;
969         memcpy( p_extra, pp_header[i], pi_header[i] );
970         p_extra += pi_header[i];
971     }
972
973     msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
974              p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
975              p_enc->fmt_in.audio.i_rate );
976
977     return VLC_SUCCESS;
978 }
979
980 /****************************************************************************
981  * Encode: the whole thing
982  ****************************************************************************
983  * This function spits out ogg packets.
984  ****************************************************************************/
985 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
986 {
987     encoder_sys_t *p_sys = p_enc->p_sys;
988     block_t *p_block, *p_chain = NULL;
989
990     unsigned char *p_buffer = p_aout_buf->p_buffer;
991     int i_samples = p_aout_buf->i_nb_samples;
992     int i_samples_delay = p_sys->i_samples_delay;
993
994     mtime_t i_pts = p_aout_buf->i_pts -
995                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
996                 (mtime_t)p_enc->fmt_in.audio.i_rate;
997
998     p_sys->i_samples_delay += i_samples;
999
1000     while( p_sys->i_samples_delay >= p_sys->i_frame_length )
1001     {
1002         int16_t *p_samples;
1003         int i_out;
1004
1005         if( i_samples_delay )
1006         {
1007             /* Take care of the left-over from last time */
1008             int i_delay_size = i_samples_delay * 2 *
1009                                  p_enc->fmt_in.audio.i_channels;
1010             int i_size = p_sys->i_frame_size - i_delay_size;
1011
1012             p_samples = (int16_t *)p_sys->p_buffer;
1013             memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
1014             p_buffer -= i_delay_size;
1015             i_samples += i_samples_delay;
1016             i_samples_delay = 0;
1017         }
1018         else
1019         {
1020             p_samples = (int16_t *)p_buffer;
1021         }
1022
1023         /* Encode current frame */
1024         if( p_enc->fmt_in.audio.i_channels == 2 )
1025             speex_encode_stereo_int( p_samples, p_sys->i_frame_length,
1026                                      &p_sys->bits );
1027
1028 #if 0
1029         if( p_sys->preprocess )
1030             speex_preprocess( p_sys->preprocess, p_samples, NULL );
1031 #endif
1032
1033         speex_encode_int( p_sys->p_state, p_samples, &p_sys->bits );
1034
1035         p_buffer += p_sys->i_frame_size;
1036         p_sys->i_samples_delay -= p_sys->i_frame_length;
1037         i_samples -= p_sys->i_frame_length;
1038
1039         p_sys->i_frames_in_packet++;
1040
1041         if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
1042             continue;
1043
1044         p_sys->i_frames_in_packet = 0;
1045
1046         speex_bits_insert_terminator( &p_sys->bits );
1047         i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
1048                                   MAX_FRAME_BYTES );
1049         speex_bits_reset( &p_sys->bits );
1050
1051         p_block = block_New( p_enc, i_out );
1052         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
1053
1054         p_block->i_length = (mtime_t)1000000 *
1055             (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
1056             (mtime_t)p_enc->fmt_in.audio.i_rate;
1057
1058         p_block->i_dts = p_block->i_pts = i_pts;
1059
1060         /* Update pts */
1061         i_pts += p_block->i_length;
1062         block_ChainAppend( &p_chain, p_block );
1063
1064     }
1065
1066     /* Backup the remaining raw samples */
1067     if( i_samples )
1068     {
1069         memcpy( p_sys->p_buffer + i_samples_delay * 2 *
1070                 p_enc->fmt_in.audio.i_channels, p_buffer,
1071                 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
1072     }
1073
1074     return p_chain;
1075 }
1076
1077 /*****************************************************************************
1078  * CloseEncoder: encoder destruction
1079  *****************************************************************************/
1080 static void CloseEncoder( vlc_object_t *p_this )
1081 {
1082     encoder_t *p_enc = (encoder_t *)p_this;
1083     encoder_sys_t *p_sys = p_enc->p_sys;
1084
1085     speex_encoder_destroy( p_sys->p_state );
1086     speex_bits_destroy( &p_sys->bits );
1087
1088     free( p_sys->p_buffer );
1089     free( p_sys );
1090 }