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