]> git.sesse.net Git - vlc/blob - modules/codec/speex.c
* src/input/control.c: added INPUT_ADD_INFO/INPUT_SET_NAME to input_Control().
[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$
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., 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 )
392         {
393             /* End of stream */
394             return NULL;
395         }
396
397         if( i_ret== -2 )
398         {
399             msg_Warn( p_dec, "decoding error: corrupted stream?" );
400             return NULL;
401         }
402
403         if( speex_bits_remaining( &p_sys->bits ) < 0 )
404         {
405             msg_Warn( p_dec, "decoding overflow: corrupted stream?" );
406         }
407
408         if( p_sys->p_header->nb_channels == 2 )
409             speex_decode_stereo( (int16_t *)p_aout_buffer->p_buffer,
410                                  p_sys->p_header->frame_size, &p_sys->stereo );
411
412         /* Date management */
413         p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
414         p_aout_buffer->end_date =
415             aout_DateIncrement( &p_sys->end_date, p_sys->p_header->frame_size);
416
417         p_sys->i_frame_in_packet++;
418
419         return p_aout_buffer;
420     }
421     else
422     {
423         return NULL;
424     }
425 }
426
427 /*****************************************************************************
428  * SendPacket: send an ogg packet to the stream output.
429  *****************************************************************************/
430 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
431                             block_t *p_block )
432 {
433     decoder_sys_t *p_sys = p_dec->p_sys;
434
435     /* Date management */
436     p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
437
438     if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
439         p_block->i_length =
440             aout_DateIncrement( &p_sys->end_date,
441                                 p_sys->p_header->frame_size ) -
442             p_block->i_pts;
443     else
444         p_block->i_length = 0;
445
446     return p_block;
447 }
448
449 /*****************************************************************************
450  * ParseSpeexComments: FIXME should be done in demuxer
451  *****************************************************************************/
452 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
453                            ((buf[base+2]<<16)&0xff0000)| \
454                            ((buf[base+1]<<8)&0xff00)| \
455                             (buf[base]&0xff))
456
457 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
458 {
459     input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
460     decoder_sys_t *p_sys = p_dec->p_sys;
461
462     char *p_buf = (char *)p_oggpacket->packet;
463     SpeexMode *p_mode;
464     int i_len;
465
466     p_mode = speex_mode_list[p_sys->p_header->mode];
467
468     input_Control( p_input, INPUT_ADD_INFO, _("Speex comment"), _("Mode"),
469                    "%s%s", p_mode->modeName,
470                    p_sys->p_header->vbr ? " VBR" : "" );
471
472     if( p_oggpacket->bytes < 8 )
473     {
474         msg_Warn( p_dec, "invalid/corrupted comments" );
475         return;
476     }
477
478     i_len = readint( p_buf, 0 ); p_buf += 4;
479     if( i_len > p_oggpacket->bytes - 4 )
480     {
481         msg_Warn( p_dec, "invalid/corrupted comments" );
482         return;
483     }
484
485     input_Control( p_input, INPUT_ADD_INFO, _("Speex comment"), p_buf, "" );
486
487     /* TODO: finish comments parsing */
488 }
489
490 /*****************************************************************************
491  * CloseDecoder: speex decoder destruction
492  *****************************************************************************/
493 static void CloseDecoder( vlc_object_t *p_this )
494 {
495     decoder_t * p_dec = (decoder_t *)p_this;
496     decoder_sys_t *p_sys = p_dec->p_sys;
497
498     if( p_sys->p_state )
499     {
500         speex_decoder_destroy( p_sys->p_state );
501         speex_bits_destroy( &p_sys->bits );
502     }
503
504     if( p_sys->p_header ) free( p_sys->p_header );
505     free( p_sys );
506 }
507
508 /*****************************************************************************
509  * encoder_sys_t: encoder descriptor
510  *****************************************************************************/
511 #define MAX_FRAME_SIZE  2000
512 #define MAX_FRAME_BYTES 2000
513
514 struct encoder_sys_t
515 {
516     /*
517      * Input properties
518      */
519     int i_headers;
520
521     char *p_buffer;
522     char *p_buffer_out[MAX_FRAME_BYTES];
523
524     /*
525      * Speex properties
526      */
527     SpeexBits bits;
528     SpeexHeader header;
529     SpeexStereoState stereo;
530     void *p_state;
531
532     int i_frames_per_packet;
533     int i_frames_in_packet;
534
535     int i_frame_length;
536     int i_samples_delay;
537     int i_frame_size;
538
539     /*
540      * Common properties
541      */
542     mtime_t i_pts;
543 };
544
545 /*****************************************************************************
546  * OpenEncoder: probe the encoder and return score
547  *****************************************************************************/
548 static int OpenEncoder( vlc_object_t *p_this )
549 {
550     encoder_t *p_enc = (encoder_t *)p_this;
551     encoder_sys_t *p_sys;
552     SpeexMode *p_speex_mode = &speex_nb_mode;
553     int i_quality;
554
555     if( p_enc->fmt_out.i_codec != VLC_FOURCC('s','p','x',' ') )
556     {
557         return VLC_EGENERIC;
558     }
559
560     /* Allocate the memory needed to store the decoder's structure */
561     if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
562     {
563         msg_Err( p_enc, "out of memory" );
564         return VLC_EGENERIC;
565     }
566     p_enc->p_sys = p_sys;
567     p_enc->pf_header = Headers;
568     p_enc->pf_encode_audio = Encode;
569     p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
570
571     speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
572                        1, p_speex_mode );
573
574     p_sys->header.frames_per_packet = 1;
575     p_sys->header.vbr = 1;
576     p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
577
578     /* Create a new encoder state in narrowband mode */
579     p_sys->p_state = speex_encoder_init( p_speex_mode );
580
581     /* Set the quality to 8 (15 kbps) */
582     i_quality = 8;
583     speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_quality );
584
585     /*Initialization of the structure that holds the bits*/
586     speex_bits_init( &p_sys->bits );
587
588     p_sys->i_frames_in_packet = 0;
589     p_sys->i_samples_delay = 0;
590     p_sys->i_headers = 0;
591     p_sys->i_pts = 0;
592
593     speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
594                        &p_sys->i_frame_length );
595
596     p_sys->i_frame_size = p_sys->i_frame_length *
597         sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
598     p_sys->p_buffer = malloc( p_sys->i_frame_size );
599
600     msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
601              p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
602              p_enc->fmt_in.audio.i_rate );
603
604     return VLC_SUCCESS;
605 }
606
607 /****************************************************************************
608  * Headers: spits out the headers
609  ****************************************************************************
610  * This function spits out ogg packets.
611  ****************************************************************************/
612 static block_t *Headers( encoder_t *p_enc )
613 {
614     encoder_sys_t *p_sys = p_enc->p_sys;
615     block_t *p_block, *p_chain = NULL;
616
617     /* Create speex headers */
618     if( !p_sys->i_headers )
619     {
620         char *p_buffer;
621         int i_buffer;
622
623         /* Main header */
624         p_buffer = speex_header_to_packet( &p_sys->header, &i_buffer );
625         p_block = block_New( p_enc, i_buffer );
626         memcpy( p_block->p_buffer, p_buffer, i_buffer );
627         p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
628         block_ChainAppend( &p_chain, p_block );
629
630         /* Comment */
631         p_block = block_New( p_enc, sizeof("ENCODER=VLC media player") );
632         memcpy( p_block->p_buffer, "ENCODER=VLC media player",
633                 p_block->i_buffer );
634         p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
635         block_ChainAppend( &p_chain, p_block );
636
637         p_sys->i_headers = 2;
638     }
639
640     return p_chain;
641 }
642
643 /****************************************************************************
644  * Encode: the whole thing
645  ****************************************************************************
646  * This function spits out ogg packets.
647  ****************************************************************************/
648 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
649 {
650     encoder_sys_t *p_sys = p_enc->p_sys;
651     block_t *p_block, *p_chain = NULL;
652
653     char *p_buffer = p_aout_buf->p_buffer;
654     int i_samples = p_aout_buf->i_nb_samples;
655     int i_samples_delay = p_sys->i_samples_delay;
656
657     p_sys->i_pts = p_aout_buf->start_date -
658                 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
659                 (mtime_t)p_enc->fmt_in.audio.i_rate;
660
661     p_sys->i_samples_delay += i_samples;
662
663     while( p_sys->i_samples_delay >= p_sys->i_frame_length )
664     {
665         int16_t *p_samples;
666         int i_out;
667
668         if( i_samples_delay )
669         {
670             /* Take care of the left-over from last time */
671             int i_delay_size = i_samples_delay * 2 *
672                                  p_enc->fmt_in.audio.i_channels;
673             int i_size = p_sys->i_frame_size - i_delay_size;
674
675             p_samples = (int16_t *)p_sys->p_buffer;
676             memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
677             p_buffer -= i_delay_size;
678             i_samples += i_samples_delay;
679             i_samples_delay = 0;
680         }
681         else
682         {
683             p_samples = (int16_t *)p_buffer;
684         }
685
686         /* Encode current frame */
687         if( p_enc->fmt_in.audio.i_channels == 2 )
688             speex_encode_stereo( p_samples, p_sys->i_frame_length,
689                                  &p_sys->bits );
690
691 #if 0
692         if( p_sys->preprocess )
693             speex_preprocess( p_sys->preprocess, p_samples, NULL );
694 #endif
695
696         speex_encode( p_sys->p_state, p_samples, &p_sys->bits );
697
698         p_buffer += p_sys->i_frame_size;
699         p_sys->i_samples_delay -= p_sys->i_frame_length;
700         i_samples -= p_sys->i_frame_length;
701
702         p_sys->i_frames_in_packet++;
703
704         if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
705             continue;
706
707         p_sys->i_frames_in_packet = 0;
708
709         speex_bits_insert_terminator( &p_sys->bits );
710         i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
711                                   MAX_FRAME_BYTES );
712         speex_bits_reset( &p_sys->bits );
713
714         p_block = block_New( p_enc, i_out );
715         memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
716
717         p_block->i_length = (mtime_t)1000000 *
718             (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
719             (mtime_t)p_enc->fmt_in.audio.i_rate;
720
721         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
722
723         /* Update pts */
724         p_sys->i_pts += p_block->i_length;
725         block_ChainAppend( &p_chain, p_block );
726
727     }
728
729     /* Backup the remaining raw samples */
730     if( i_samples )
731     {
732         memcpy( p_sys->p_buffer + i_samples_delay * 2 *
733                 p_enc->fmt_in.audio.i_channels, p_buffer,
734                 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
735     }
736
737     return p_chain;
738 }
739
740 /*****************************************************************************
741  * CloseEncoder: encoder destruction
742  *****************************************************************************/
743 static void CloseEncoder( vlc_object_t *p_this )
744 {
745     encoder_t *p_enc = (encoder_t *)p_this;
746     encoder_sys_t *p_sys = p_enc->p_sys;
747
748     speex_encoder_destroy( p_sys->p_state );
749     speex_bits_destroy( &p_sys->bits );
750
751     if( p_sys->p_buffer ) free( p_sys->p_buffer );
752     free( p_sys );
753 }