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