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