]> git.sesse.net Git - vlc/blob - modules/codec/speex.c
Options as infos were bad in several ways: it broke PLAYLIST_GO, used
[vlc] / modules / codec / speex.c
1 /*****************************************************************************
2  * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
3  *****************************************************************************
4  * Copyright (C) 2003 VideoLAN
5  * $Id: speex.c,v 1.11 2004/01/29 17:51:07 zorglub 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 #include "vlc_playlist.h"
30
31 #include <ogg/ogg.h>
32 #include <speex.h>
33 #include "speex_header.h"
34 #include "speex_stereo.h"
35 #include "speex_callbacks.h"
36
37 /*****************************************************************************
38  * decoder_sys_t : speex decoder descriptor
39  *****************************************************************************/
40 struct decoder_sys_t
41 {
42     /* Module mode */
43     vlc_bool_t b_packetizer;
44
45     /*
46      * Input properties
47      */
48     int i_headers;
49     int i_frame_in_packet;
50
51     /*
52      * Speex properties
53      */
54     SpeexBits bits;
55     SpeexHeader *p_header;
56     SpeexStereoState stereo;
57     void *p_state;
58
59     /*
60      * Common properties
61      */
62     audio_date_t end_date;
63
64 };
65
66 static int pi_channels_maps[6] =
67 {
68     0,
69     AOUT_CHAN_CENTER,   AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
70     AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
71     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
72      | AOUT_CHAN_REARRIGHT,
73     AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
74      | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
75 };
76
77 /****************************************************************************
78  * Local prototypes
79  ****************************************************************************/
80 static int  OpenDecoder   ( vlc_object_t * );
81 static int  OpenPacketizer( vlc_object_t * );
82 static void CloseDecoder  ( vlc_object_t * );
83
84 static void *DecodeBlock  ( decoder_t *, block_t ** );
85 static int  ProcessHeader ( decoder_t *, ogg_packet * );
86 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
87
88 static aout_buffer_t *DecodePacket( decoder_t *, ogg_packet * );
89 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
90
91 static void ParseSpeexComments( decoder_t *, ogg_packet * );
92
93 static int OpenEncoder   ( vlc_object_t * );
94 static void CloseEncoder ( vlc_object_t * );
95 static block_t *Headers  ( encoder_t * );
96 static block_t *Encode   ( encoder_t *, aout_buffer_t * );
97
98 /*****************************************************************************
99  * Module descriptor
100  *****************************************************************************/
101 vlc_module_begin();
102     set_description( _("Speex audio decoder") );
103     set_capability( "decoder", 100 );
104     set_callbacks( OpenDecoder, CloseDecoder );
105
106     add_submodule();
107     set_description( _("Speex audio packetizer") );
108     set_capability( "packetizer", 100 );
109     set_callbacks( OpenPacketizer, CloseDecoder );
110
111     add_submodule();
112     set_description( _("Speex audio encoder") );
113     set_capability( "encoder", 100 );
114     set_callbacks( OpenEncoder, CloseEncoder );
115 vlc_module_end();
116
117 /*****************************************************************************
118  * OpenDecoder: probe the decoder and return score
119  *****************************************************************************/
120 static int OpenDecoder( vlc_object_t *p_this )
121 {
122     decoder_t *p_dec = (decoder_t*)p_this;
123     decoder_sys_t *p_sys = p_dec->p_sys;
124
125     if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','p','x',' ') )
126     {
127         return VLC_EGENERIC;
128     }
129
130     /* Allocate the memory needed to store the decoder's structure */
131     if( ( p_dec->p_sys = p_sys =
132           (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
133     {
134         msg_Err( p_dec, "out of memory" );
135         return VLC_EGENERIC;
136     }
137     p_dec->p_sys->b_packetizer = VLC_FALSE;
138
139     aout_DateSet( &p_sys->end_date, 0 );
140
141     /* Set output properties */
142     p_dec->fmt_out.i_cat = AUDIO_ES;
143     p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
144
145     /* Set callbacks */
146     p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
147         DecodeBlock;
148     p_dec->pf_packetize    = (block_t *(*)(decoder_t *, block_t **))
149         DecodeBlock;
150
151     p_sys->i_headers = 0;
152     p_sys->p_state = NULL;
153     p_sys->p_header = NULL;
154     p_sys->i_frame_in_packet = 0;
155
156     return VLC_SUCCESS;
157 }
158
159 static int OpenPacketizer( vlc_object_t *p_this )
160 {
161     decoder_t *p_dec = (decoder_t*)p_this;
162
163     int i_ret = OpenDecoder( p_this );
164
165     if( i_ret == VLC_SUCCESS )
166     {
167         p_dec->p_sys->b_packetizer = VLC_TRUE;
168         p_dec->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
169     }
170
171     return i_ret;
172 }
173
174 /****************************************************************************
175  * DecodeBlock: the whole thing
176  ****************************************************************************
177  * This function must be fed with ogg packets.
178  ****************************************************************************/
179 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
180 {
181     decoder_sys_t *p_sys = p_dec->p_sys;
182     ogg_packet oggpacket;
183
184     if( !pp_block ) return NULL;
185
186     if( *pp_block )
187     {
188         /* Block to Ogg packet */
189         oggpacket.packet = (*pp_block)->p_buffer;
190         oggpacket.bytes = (*pp_block)->i_buffer;
191     }
192     else
193     {
194         if( p_sys->b_packetizer ) return NULL;
195
196         /* Block to Ogg packet */
197         oggpacket.packet = NULL;
198         oggpacket.bytes = 0;
199     }
200
201     oggpacket.granulepos = -1;
202     oggpacket.b_o_s = 0;
203     oggpacket.e_o_s = 0;
204     oggpacket.packetno = 0;
205
206     if( p_sys->i_headers == 0 )
207     {
208         /* Take care of the initial Speex header */
209         if( ProcessHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
210         {
211             msg_Err( p_dec, "initial Speex header is corrupted" );
212             block_Release( *pp_block );
213             return NULL;
214         }
215
216         p_sys->i_headers++;
217
218         return ProcessPacket( p_dec, &oggpacket, pp_block );
219     }
220
221     if( p_sys->i_headers == 1 )
222     {
223         /* The next packet in order is the comments header */
224         ParseSpeexComments( p_dec, &oggpacket );
225         p_sys->i_headers++;
226
227         return ProcessPacket( p_dec, &oggpacket, pp_block );
228     }
229
230     return ProcessPacket( p_dec, &oggpacket, pp_block );
231 }
232
233 /*****************************************************************************
234  * ProcessHeader: processes the inital Speex header packet.
235  *****************************************************************************/
236 static int ProcessHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
237 {
238     decoder_sys_t *p_sys = p_dec->p_sys;
239
240     void *p_state;
241     SpeexHeader *p_header;
242     SpeexMode *p_mode;
243     SpeexCallback callback;
244
245     p_sys->p_header = p_header =
246         speex_packet_to_header( p_oggpacket->packet, p_oggpacket->bytes );
247     if( !p_header )
248     {
249         msg_Err( p_dec, "cannot read Speex header" );
250         return VLC_EGENERIC;
251     }
252     if( p_header->mode >= SPEEX_NB_MODES )
253     {
254         msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
255                  "this version of libspeex.", p_header->mode );
256         return VLC_EGENERIC;
257     }
258
259     p_mode = speex_mode_list[p_header->mode];
260
261     if( p_header->speex_version_id > 1 )
262     {
263         msg_Err( p_dec, "this file was encoded with Speex bit-stream "
264                  "version %d, which I don't know how to decode.",
265                  p_header->speex_version_id );
266         return VLC_EGENERIC;
267     }
268
269     if( p_mode->bitstream_version < p_header->mode_bitstream_version )
270     {
271         msg_Err( p_dec, "file encoded with a newer version of Speex." );
272         return VLC_EGENERIC;
273     }
274     if( p_mode->bitstream_version > p_header->mode_bitstream_version )
275     {
276         msg_Err( p_dec, "file encoded with an older version of Speex." );
277         return VLC_EGENERIC;
278     }
279
280     msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
281              p_header->rate, p_mode->modeName,
282              ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
283              p_header->vbr ? ", VBR)" : ")" );
284
285     /* Take care of speex decoder init */
286     speex_bits_init( &p_sys->bits );
287     p_sys->p_state = p_state = speex_decoder_init( p_mode );
288     if( !p_state )
289     {
290         msg_Err( p_dec, "decoder initialization failed" );
291         return VLC_EGENERIC;
292     }
293
294     if( p_header->nb_channels == 2 )
295     {
296         SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
297         p_sys->stereo = stereo;
298         callback.callback_id = SPEEX_INBAND_STEREO;
299         callback.func = speex_std_stereo_request_handler;
300         callback.data = &p_sys->stereo;
301         speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
302     }
303
304     /* Setup the format */
305     p_dec->fmt_out.audio.i_physical_channels =
306         p_dec->fmt_out.audio.i_original_channels =
307             pi_channels_maps[p_header->nb_channels];
308     p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
309     p_dec->fmt_out.audio.i_rate = p_header->rate;
310
311     aout_DateInit( &p_sys->end_date, p_header->rate );
312
313     return VLC_SUCCESS;
314 }
315
316 /*****************************************************************************
317  * ProcessPacket: processes a Speex packet.
318  *****************************************************************************/
319 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
320                             block_t **pp_block )
321 {
322     decoder_sys_t *p_sys = p_dec->p_sys;
323     block_t *p_block = *pp_block;
324
325     /* Date management */
326     if( p_block && p_block->i_pts > 0 &&
327         p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
328     {
329         aout_DateSet( &p_sys->end_date, p_block->i_pts );
330     }
331
332     if( !aout_DateGet( &p_sys->end_date ) )
333     {
334         /* We've just started the stream, wait for the first PTS. */
335         if( p_block ) block_Release( p_block );
336         return NULL;
337     }
338
339     *pp_block = NULL; /* To avoid being fed the same packet again */
340
341     if( p_sys->b_packetizer )
342     {
343          return SendPacket( p_dec, p_oggpacket, p_block );
344     }
345     else
346     {
347         aout_buffer_t *p_aout_buffer;
348
349         if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
350             p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
351         else
352             p_aout_buffer = NULL; /* Skip headers */
353
354         if( p_block )
355         {
356             block_Release( p_block );
357         }
358         return p_aout_buffer;
359     }
360 }
361
362 /*****************************************************************************
363  * DecodePacket: decodes a Speex packet.
364  *****************************************************************************/
365 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
366 {
367     decoder_sys_t *p_sys = p_dec->p_sys;
368
369     if( p_oggpacket->bytes )
370     {
371         /* Copy Ogg packet to Speex bitstream */
372         speex_bits_read_from( &p_sys->bits, p_oggpacket->packet,
373                               p_oggpacket->bytes );
374         p_sys->i_frame_in_packet = 0;
375     }
376
377     /* Decode one frame at a time */
378     if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
379     {
380         aout_buffer_t *p_aout_buffer;
381         int i_ret;
382
383         p_aout_buffer =
384             p_dec->pf_aout_buffer_new( p_dec, p_sys->p_header->frame_size );
385         if( !p_aout_buffer )
386         {
387             return NULL;
388         }
389
390         i_ret = speex_decode( p_sys->p_state, &p_sys->bits,
391                               (int16_t *)p_aout_buffer->p_buffer );
392         if( i_ret == -1 )
393         {
394             /* End of stream */
395             return NULL;
396         }
397
398         if( i_ret== -2 )
399         {
400             msg_Warn( p_dec, "decoding error: corrupted stream?" );
401             return NULL;
402         }
403
404         if( speex_bits_remaining( &p_sys->bits ) < 0 )
405         {
406             msg_Warn( p_dec, "decoding overflow: corrupted stream?" );
407         }
408
409         if( p_sys->p_header->nb_channels == 2 )
410             speex_decode_stereo( (int16_t *)p_aout_buffer->p_buffer,
411                                  p_sys->p_header->frame_size, &p_sys->stereo );
412
413         /* Date management */
414         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
415         p_aout_buffer->end_date =
416             aout_DateIncrement( &p_sys->end_date, p_sys->p_header->frame_size);
417
418         p_sys->i_frame_in_packet++;
419
420         return p_aout_buffer;
421     }
422     else
423     {
424         return NULL;
425     }
426 }
427
428 /*****************************************************************************
429  * SendPacket: send an ogg packet to the stream output.
430  *****************************************************************************/
431 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
432                             block_t *p_block )
433 {
434     decoder_sys_t *p_sys = p_dec->p_sys;
435
436     /* Date management */
437     p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
438
439     if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
440         p_block->i_length =
441             aout_DateIncrement( &p_sys->end_date,
442                                 p_sys->p_header->frame_size ) -
443             p_block->i_pts;
444     else
445         p_block->i_length = 0;
446
447     return p_block;
448 }
449
450 /*****************************************************************************
451  * ParseSpeexComments: FIXME should be done in demuxer
452  *****************************************************************************/
453 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
454                            ((buf[base+2]<<16)&0xff0000)| \
455                            ((buf[base+1]<<8)&0xff00)| \
456                             (buf[base]&0xff))
457
458 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
459 {
460     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
461     decoder_sys_t *p_sys = p_dec->p_sys;
462
463     input_info_category_t *p_cat =
464         input_InfoCategory( p_input, _("Speex comment") );
465     playlist_t *p_playlist = vlc_object_find( p_dec, VLC_OBJECT_PLAYLIST,
466                                               FIND_ANYWHERE );
467
468     char *p_buf = (char *)p_oggpacket->packet;
469     SpeexMode *p_mode;
470     int i_len;
471
472     p_mode = speex_mode_list[p_sys->p_header->mode];
473     input_AddInfo( p_cat, _("Mode"), "%s%s",
474                   p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" );
475
476     vlc_mutex_lock( &p_playlist->object_lock );
477     p_item = playlist_ItemGetByPos( p_playlist, -1 );
478     vlc_mutex_unlock( &p_playlist->object_lock );
479     if( !p_item)
480     {
481         msg_Err(p_dec, "unable to find item" );
482         return;
483     }
484     vlc_mutex_lock( &p_item->lock );
485
486     playlist_ItemAddInfo( p_item, _("Speex comment") , _("Mode"),"%s%s",
487                     p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" );
488
489     if( p_oggpacket->bytes < 8 )
490     {
491         msg_Warn( p_dec, "invalid/corrupted comments" );
492         return;
493     }
494
495     i_len = readint( p_buf, 0 ); p_buf += 4;
496     if( i_len > p_oggpacket->bytes - 4 )
497     {
498         msg_Warn( p_dec, "invalid/corrupted comments" );
499         return;
500     }
501
502     input_AddInfo( p_cat, p_buf, "" );
503     playlist_ItemAddInfo( p_item , _("Speex comment") , p_buf , "" );
504
505     vlc_mutex_unlock( &p_item->lock );
506
507     if( p_playlist ) vlc_object_release( p_playlist );
508
509     /* TODO: finish comments parsing */
510 }
511
512 /*****************************************************************************
513  * CloseDecoder: speex decoder destruction
514  *****************************************************************************/
515 static void CloseDecoder( vlc_object_t *p_this )
516 {
517     decoder_t * p_dec = (decoder_t *)p_this;
518     decoder_sys_t *p_sys = p_dec->p_sys;
519
520     if( p_sys->p_state )
521     {
522         speex_decoder_destroy( p_sys->p_state );
523         speex_bits_destroy( &p_sys->bits );
524     }
525
526     if( p_sys->p_header ) free( p_sys->p_header );
527     free( p_sys );
528 }
529
530 /*****************************************************************************
531  * encoder_sys_t: encoder descriptor
532  *****************************************************************************/
533 #define MAX_FRAME_SIZE  2000
534 #define MAX_FRAME_BYTES 2000
535
536 struct encoder_sys_t
537 {
538     /*
539      * Input properties
540      */
541     int i_headers;
542
543     char *p_buffer;
544     char *p_buffer_out[MAX_FRAME_BYTES];
545
546     /*
547      * Speex properties
548      */
549     SpeexBits bits;
550     SpeexHeader header;
551     SpeexStereoState stereo;
552     void *p_state;
553
554     int i_frames_per_packet;
555     int i_frames_in_packet;
556
557     int i_frame_length;
558     int i_samples_delay;
559     int i_frame_size;
560
561     /*
562      * Common properties
563      */
564     mtime_t i_pts;
565 };
566
567 /*****************************************************************************
568  * OpenEncoder: probe the encoder and return score
569  *****************************************************************************/
570 static int OpenEncoder( vlc_object_t *p_this )
571 {
572     encoder_t *p_enc = (encoder_t *)p_this;
573     encoder_sys_t *p_sys;
574     SpeexMode *p_speex_mode = &speex_nb_mode;
575     int i_quality;
576
577     if( p_enc->fmt_out.i_codec != VLC_FOURCC('s','p','x',' ') )
578     {
579         return VLC_EGENERIC;
580     }
581
582     /* Allocate the memory needed to store the decoder's structure */
583     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
584     {
585         msg_Err( p_enc, "out of memory" );
586         return VLC_EGENERIC;
587     }
588     p_enc->p_sys = p_sys;
589     p_enc->pf_header = Headers;
590     p_enc->pf_encode_audio = Encode;
591     p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
592
593     speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
594                        1, p_speex_mode );
595
596     p_sys->header.frames_per_packet = 1;
597     p_sys->header.vbr = 1;
598     p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
599
600     /* Create a new encoder state in narrowband mode */
601     p_sys->p_state = speex_encoder_init( p_speex_mode );
602
603     /* Set the quality to 8 (15 kbps) */
604     i_quality = 8;
605     speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_quality );
606
607     /*Initialization of the structure that holds the bits*/
608     speex_bits_init( &p_sys->bits );
609
610     p_sys->i_frames_in_packet = 0;
611     p_sys->i_samples_delay = 0;
612     p_sys->i_headers = 0;
613     p_sys->i_pts = 0;
614
615     speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
616                        &p_sys->i_frame_length );
617
618     p_sys->i_frame_size = p_sys->i_frame_length *
619         sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
620     p_sys->p_buffer = malloc( p_sys->i_frame_size );
621
622     msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
623              p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
624              p_enc->fmt_in.audio.i_rate );
625
626     return VLC_SUCCESS;
627 }
628
629 /****************************************************************************
630  * Headers: spits out the headers
631  ****************************************************************************
632  * This function spits out ogg packets.
633  ****************************************************************************/
634 static block_t *Headers( encoder_t *p_enc )
635 {
636     encoder_sys_t *p_sys = p_enc->p_sys;
637     block_t *p_block, *p_chain = NULL;
638
639     /* Create speex headers */
640     if( !p_sys->i_headers )
641     {
642         char *p_buffer;
643         int i_buffer;
644
645         /* Main header */
646         p_buffer = speex_header_to_packet( &p_sys->header, &i_buffer );
647         p_block = block_New( p_enc, i_buffer );
648         memcpy( p_block->p_buffer, p_buffer, i_buffer );
649         p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
650         block_ChainAppend( &p_chain, p_block );
651
652         /* Comment */
653         p_block = block_New( p_enc, sizeof("ENCODER=VLC media player") );
654         memcpy( p_block->p_buffer, "ENCODER=VLC media player",
655                 p_block->i_buffer );
656         p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
657         block_ChainAppend( &p_chain, p_block );
658
659         p_sys->i_headers = 2;
660     }
661
662     return p_chain;
663 }
664
665 /****************************************************************************
666  * Encode: the whole thing
667  ****************************************************************************
668  * This function spits out ogg packets.
669  ****************************************************************************/
670 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
671 {
672     encoder_sys_t *p_sys = p_enc->p_sys;
673     block_t *p_block, *p_chain = NULL;
674
675     char *p_buffer = p_aout_buf->p_buffer;
676     int i_samples = p_aout_buf->i_nb_samples;
677     int i_samples_delay = p_sys->i_samples_delay;
678
679     p_sys->i_pts = p_aout_buf->start_date -
680                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
681                 (mtime_t)p_enc->fmt_in.audio.i_rate;
682
683     p_sys->i_samples_delay += i_samples;
684
685     while( p_sys->i_samples_delay >= p_sys->i_frame_length )
686     {
687         int16_t *p_samples;
688         int i_out;
689
690         if( i_samples_delay )
691         {
692             /* Take care of the left-over from last time */
693             int i_delay_size = i_samples_delay * 2 *
694                                  p_enc->fmt_in.audio.i_channels;
695             int i_size = p_sys->i_frame_size - i_delay_size;
696
697             p_samples = (int16_t *)p_sys->p_buffer;
698             memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
699             p_buffer -= i_delay_size;
700             i_samples += i_samples_delay;
701             i_samples_delay = 0;
702         }
703         else
704         {
705             p_samples = (int16_t *)p_buffer;
706         }
707
708         /* Encode current frame */
709         if( p_enc->fmt_in.audio.i_channels == 2 )
710             speex_encode_stereo( p_samples, p_sys->i_frame_length,
711                                  &p_sys->bits );
712
713 #if 0
714         if( p_sys->preprocess )
715             speex_preprocess( p_sys->preprocess, p_samples, NULL );
716 #endif
717
718         speex_encode( p_sys->p_state, p_samples, &p_sys->bits );
719
720         p_buffer += p_sys->i_frame_size;
721         p_sys->i_samples_delay -= p_sys->i_frame_length;
722         i_samples -= p_sys->i_frame_length;
723
724         p_sys->i_frames_in_packet++;
725
726         if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
727             continue;
728
729         p_sys->i_frames_in_packet = 0;
730
731         speex_bits_insert_terminator( &p_sys->bits );
732         i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
733                                   MAX_FRAME_BYTES );
734         speex_bits_reset( &p_sys->bits );
735
736         p_block = block_New( p_enc, i_out );
737         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
738
739         p_block->i_length = (mtime_t)1000000 *
740             (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
741             (mtime_t)p_enc->fmt_in.audio.i_rate;
742
743         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
744
745         /* Update pts */
746         p_sys->i_pts += p_block->i_length;
747         block_ChainAppend( &p_chain, p_block );
748
749     }
750
751     /* Backup the remaining raw samples */
752     if( i_samples )
753     {
754         memcpy( p_sys->p_buffer + i_samples_delay * 2 *
755                 p_enc->fmt_in.audio.i_channels, p_buffer,
756                 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
757     }
758
759     return p_chain;
760 }
761
762 /*****************************************************************************
763  * CloseEncoder: encoder destruction
764  *****************************************************************************/
765 static void CloseEncoder( vlc_object_t *p_this )
766 {
767     encoder_t *p_enc = (encoder_t *)p_this;
768     encoder_sys_t *p_sys = p_enc->p_sys;
769
770     speex_encoder_destroy( p_sys->p_state );
771     speex_bits_destroy( &p_sys->bits );
772
773     if( p_sys->p_buffer ) free( p_sys->p_buffer );
774     free( p_sys );
775 }