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