demux: mp4: avoid audio cuts on seek
[vlc.git] / modules / codec / speex.c
blob9f6884a40cf70b3421d941e6b97667180eed94a9
1 /*****************************************************************************
2 * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
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 Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_input.h>
34 #include <vlc_codec.h>
35 #include "../demux/xiph.h"
37 #include <ogg/ogg.h>
38 #include <speex/speex.h>
39 #include <speex/speex_header.h>
40 #include <speex/speex_stereo.h>
41 #include <speex/speex_callbacks.h>
43 #include <assert.h>
45 /*****************************************************************************
46 * Module descriptor
47 *****************************************************************************/
48 static int OpenDecoder ( vlc_object_t * );
49 static int OpenPacketizer( vlc_object_t * );
50 static void CloseDecoder ( vlc_object_t * );
52 #ifdef ENABLE_SOUT
53 static int OpenEncoder ( vlc_object_t * );
54 static void CloseEncoder ( vlc_object_t * );
55 #endif
57 #define ENC_CFG_PREFIX "sout-speex-"
59 #define ENC_MODE_TEXT N_("Mode" )
60 #define ENC_MODE_LONGTEXT N_( \
61 "Enforce the mode of the encoder." )
63 #define ENC_QUALITY_TEXT N_("Encoding quality")
64 #define ENC_QUALITY_LONGTEXT N_( \
65 "Enforce a quality between 0 (low) and 10 (high)." )
67 #define ENC_COMPLEXITY_TEXT N_("Encoding complexity" )
68 #define ENC_COMPLEXITY_LONGTEXT N_( \
69 "Enforce the complexity of the encoder." )
71 #define ENC_MAXBITRATE_TEXT N_( "Maximal bitrate" )
72 #define ENC_MAXBITRATE_LONGTEXT N_( \
73 "Enforce the maximal VBR bitrate" )
75 #define ENC_CBR_TEXT N_( "CBR encoding" )
76 #define ENC_CBR_LONGTEXT N_( \
77 "Enforce a constant bitrate encoding (CBR) instead of default " \
78 "variable bitrate encoding (VBR)." )
80 #define ENC_VAD_TEXT N_( "Voice activity detection" )
81 #define ENC_VAD_LONGTEXT N_( \
82 "Enable voice activity detection (VAD). It is automatically " \
83 "activated in VBR mode." )
85 #define ENC_DTX_TEXT N_( "Discontinuous Transmission" )
86 #define ENC_DTX_LONGTEXT N_( \
87 "Enable discontinuous transmission (DTX)." )
89 static const int pi_enc_mode_values[] = { 0, 1, 2 };
90 static const char * const ppsz_enc_mode_descriptions[] = {
91 N_("Narrow-band (8kHz)"), N_("Wide-band (16kHz)"), N_("Ultra-wideband (32kHz)"), NULL
94 vlc_module_begin ()
95 set_category( CAT_INPUT )
96 set_subcategory( SUBCAT_INPUT_ACODEC )
98 set_description( N_("Speex audio decoder") )
99 set_capability( "audio decoder", 100 )
100 set_shortname( N_("Speex") )
101 set_callbacks( OpenDecoder, CloseDecoder )
103 add_submodule ()
104 set_description( N_("Speex audio packetizer") )
105 set_capability( "packetizer", 100 )
106 set_callbacks( OpenPacketizer, CloseDecoder )
108 #ifdef ENABLE_SOUT
109 add_submodule ()
110 set_description( N_("Speex audio encoder") )
111 set_capability( "encoder", 100 )
112 set_callbacks( OpenEncoder, CloseEncoder )
114 add_integer( ENC_CFG_PREFIX "mode", 0, ENC_MODE_TEXT,
115 ENC_MODE_LONGTEXT, false )
116 change_integer_list( pi_enc_mode_values, ppsz_enc_mode_descriptions )
118 add_integer( ENC_CFG_PREFIX "complexity", 3, ENC_COMPLEXITY_TEXT,
119 ENC_COMPLEXITY_LONGTEXT, false )
120 change_integer_range( 1, 10 )
122 add_bool( ENC_CFG_PREFIX "cbr", false, ENC_CBR_TEXT,
123 ENC_CBR_LONGTEXT, false )
125 add_float( ENC_CFG_PREFIX "quality", 8.0, ENC_QUALITY_TEXT,
126 ENC_QUALITY_LONGTEXT, false )
127 change_float_range( 0.0, 10.0 )
129 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, ENC_MAXBITRATE_TEXT,
130 ENC_MAXBITRATE_LONGTEXT, false )
132 add_bool( ENC_CFG_PREFIX "vad", true, ENC_VAD_TEXT,
133 ENC_VAD_LONGTEXT, false )
135 add_bool( ENC_CFG_PREFIX "dtx", false, ENC_DTX_TEXT,
136 ENC_DTX_LONGTEXT, false )
138 /* TODO agc, noise suppression, */
139 #endif
141 vlc_module_end ()
143 static const char *const ppsz_enc_options[] = {
144 "mode", "complexity", "cbr", "quality", "max-bitrate", "vad", "dtx", NULL
147 /*****************************************************************************
148 * decoder_sys_t : speex decoder descriptor
149 *****************************************************************************/
150 struct decoder_sys_t
152 /* Module mode */
153 bool b_packetizer;
156 * Input properties
158 bool b_has_headers;
159 int i_frame_in_packet;
162 * Speex properties
164 SpeexBits bits;
165 SpeexHeader *p_header;
166 SpeexStereoState stereo;
167 void *p_state;
168 unsigned int rtp_rate;
171 * Common properties
173 date_t end_date;
177 static const int pi_channels_maps[6] =
180 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
181 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
182 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
183 | AOUT_CHAN_REARRIGHT,
184 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
185 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
188 /****************************************************************************
189 * Local prototypes
190 ****************************************************************************/
192 static block_t *Packetize ( decoder_t *, block_t ** );
193 static int DecodeAudio ( decoder_t *, block_t * );
194 static int DecodeRtpSpeexPacket( decoder_t *, block_t *);
195 static int ProcessHeaders( decoder_t * );
196 static int ProcessInitialHeader ( decoder_t *, ogg_packet * );
197 static block_t *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
198 static void Flush( decoder_t * );
200 static block_t *DecodePacket( decoder_t *, ogg_packet * );
201 static block_t *SendPacket( decoder_t *, block_t * );
203 static void ParseSpeexComments( decoder_t *, ogg_packet * );
205 /*****************************************************************************
206 * OpenDecoder: probe the decoder and return score
207 *****************************************************************************/
208 static int OpenDecoder( vlc_object_t *p_this )
210 decoder_t *p_dec = (decoder_t*)p_this;
211 decoder_sys_t *p_sys;
213 if( p_dec->fmt_in.i_codec != VLC_CODEC_SPEEX )
214 return VLC_EGENERIC;
216 /* Allocate the memory needed to store the decoder's structure */
217 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
218 return VLC_ENOMEM;
219 p_dec->p_sys->bits.buf_size = 0;
220 p_dec->p_sys->b_packetizer = false;
221 p_dec->p_sys->rtp_rate = p_dec->fmt_in.audio.i_rate;
222 p_dec->p_sys->b_has_headers = false;
224 date_Set( &p_sys->end_date, 0 );
226 /* Set output properties */
227 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
230 Set callbacks
231 If the codec is spxr then this decoder is
232 being invoked on a Speex stream arriving via RTP.
233 A special decoder callback is used.
235 if (p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('s', 'p', 'x', 'r'))
237 msg_Dbg( p_dec, "Using RTP version of Speex decoder @ rate %d.",
238 p_dec->fmt_in.audio.i_rate );
239 p_dec->pf_decode = DecodeRtpSpeexPacket;
241 else
243 p_dec->pf_decode = DecodeAudio;
245 p_dec->pf_packetize = Packetize;
246 p_dec->pf_flush = Flush;
248 p_sys->p_state = NULL;
249 p_sys->p_header = NULL;
250 p_sys->i_frame_in_packet = 0;
252 return VLC_SUCCESS;
255 static int OpenPacketizer( vlc_object_t *p_this )
257 decoder_t *p_dec = (decoder_t*)p_this;
259 int i_ret = OpenDecoder( p_this );
261 if( i_ret == VLC_SUCCESS )
263 p_dec->p_sys->b_packetizer = true;
264 p_dec->fmt_out.i_codec = VLC_CODEC_SPEEX;
267 return i_ret;
270 static int CreateDefaultHeader( decoder_t *p_dec )
272 ogg_packet oggpacket;
273 SpeexHeader *p_header = malloc( sizeof(SpeexHeader) );
274 if( !p_header )
275 return VLC_ENOMEM;
277 const int rate = p_dec->fmt_in.audio.i_rate;
278 const unsigned i_mode = (rate / 8000) >> 1;
280 const SpeexMode *mode;
281 int ret = VLC_SUCCESS;
282 oggpacket.packet = NULL;
284 switch( rate )
286 case 8000:
287 case 16000:
288 case 32000:
289 mode = speex_lib_get_mode( i_mode );
290 break;
291 default:
292 msg_Err( p_dec, "Unexpected rate %d", rate );
293 ret = VLC_EGENERIC;
294 goto cleanup;
297 speex_init_header( p_header, rate, p_dec->fmt_in.audio.i_channels, mode );
298 p_header->frames_per_packet = 160 << i_mode;
300 oggpacket.packet = (unsigned char *) speex_header_to_packet( p_header,
301 (int *) &oggpacket.bytes );
302 if( !oggpacket.packet )
304 ret = VLC_ENOMEM;
305 goto cleanup;
308 oggpacket.b_o_s = 1;
309 oggpacket.e_o_s = 0;
310 oggpacket.granulepos = -1;
311 oggpacket.packetno = 0;
313 ret = ProcessInitialHeader( p_dec, &oggpacket );
315 if( ret != VLC_SUCCESS )
317 msg_Err( p_dec, "default Speex header is corrupted" );
320 cleanup:
321 free( oggpacket.packet );
322 free( p_header );
324 return ret;
328 /****************************************************************************
329 * DecodeBlock: the whole thing
330 ****************************************************************************
331 * This function must be fed with ogg packets.
332 ****************************************************************************/
333 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
335 decoder_sys_t *p_sys = p_dec->p_sys;
336 ogg_packet oggpacket;
338 block_t *block = *pp_block;
340 if( block != NULL )
342 if( block->i_flags & (BLOCK_FLAG_CORRUPTED|BLOCK_FLAG_DISCONTINUITY) )
344 Flush( p_dec );
345 if( block->i_flags & BLOCK_FLAG_CORRUPTED )
347 block_Release( block );
348 *pp_block = NULL;
349 return NULL;
352 /* Block to Ogg packet */
353 oggpacket.packet = block->p_buffer;
354 oggpacket.bytes = block->i_buffer;
356 else
358 if( p_sys->b_packetizer ) return NULL;
360 /* Block to Ogg packet */
361 oggpacket.packet = NULL;
362 oggpacket.bytes = 0;
365 oggpacket.granulepos = -1;
366 oggpacket.b_o_s = 0;
367 oggpacket.e_o_s = 0;
368 oggpacket.packetno = 0;
370 /* Check for headers */
371 if( !p_sys->b_has_headers )
373 if( !p_dec->fmt_in.p_extra )
375 msg_Warn( p_dec, "Header missing, using default settings" );
377 if( CreateDefaultHeader( p_dec ) )
379 if( block != NULL )
380 block_Release( block );
381 return NULL;
384 else if( ProcessHeaders( p_dec ) )
386 if( block != NULL )
387 block_Release( block );
388 return NULL;
390 p_sys->b_has_headers = true;
393 return ProcessPacket( p_dec, &oggpacket, pp_block );
396 static int DecodeAudio( decoder_t *p_dec, block_t *p_block )
398 if( p_block == NULL ) /* No Drain */
399 return VLCDEC_SUCCESS;
401 block_t **pp_block = &p_block, *p_out;
402 while( ( p_out = DecodeBlock( p_dec, pp_block ) ) != NULL )
403 decoder_QueueAudio( p_dec, p_out );
404 return VLCDEC_SUCCESS;
407 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
409 if( pp_block == NULL ) /* No Drain */
410 return NULL;
411 return DecodeBlock( p_dec, pp_block );
414 /*****************************************************************************
415 * ProcessHeaders: process Speex headers.
416 *****************************************************************************/
417 static int ProcessHeaders( decoder_t *p_dec )
419 decoder_sys_t *p_sys = p_dec->p_sys;
420 ogg_packet oggpacket;
422 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
423 void *pp_data[XIPH_MAX_HEADER_COUNT];
424 unsigned i_count;
425 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
426 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
427 return VLC_EGENERIC;
428 if( i_count < 2 )
429 return VLC_EGENERIC;;
431 oggpacket.granulepos = -1;
432 oggpacket.e_o_s = 0;
433 oggpacket.packetno = 0;
435 /* Take care of the initial Vorbis header */
436 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
437 oggpacket.bytes = pi_size[0];
438 oggpacket.packet = pp_data[0];
439 if( ProcessInitialHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
441 msg_Err( p_dec, "initial Speex header is corrupted" );
442 return VLC_EGENERIC;;
445 /* The next packet in order is the comments header */
446 oggpacket.b_o_s = 0;
447 oggpacket.bytes = pi_size[1];
448 oggpacket.packet = pp_data[1];
449 ParseSpeexComments( p_dec, &oggpacket );
451 if( p_sys->b_packetizer )
453 void* p_extra = realloc( p_dec->fmt_out.p_extra,
454 p_dec->fmt_in.i_extra );
455 if( unlikely( p_extra == NULL ) )
457 return VLC_ENOMEM;
459 p_dec->fmt_out.p_extra = p_extra;
460 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
461 memcpy( p_dec->fmt_out.p_extra,
462 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
465 return VLC_SUCCESS;
468 /*****************************************************************************
469 * ProcessInitialHeader: processes the inital Speex header packet.
470 *****************************************************************************/
471 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
473 decoder_sys_t *p_sys = p_dec->p_sys;
475 void *p_state;
476 SpeexHeader *p_header;
477 const SpeexMode *p_mode;
478 SpeexCallback callback;
480 p_sys->p_header = p_header =
481 speex_packet_to_header( (char *)p_oggpacket->packet,
482 p_oggpacket->bytes );
483 if( !p_header )
485 msg_Err( p_dec, "cannot read Speex header" );
486 return VLC_EGENERIC;
488 if( p_header->mode >= SPEEX_NB_MODES || p_header->mode < 0 )
490 msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
491 "this version of libspeex.", p_header->mode );
492 return VLC_EGENERIC;
495 p_mode = speex_mode_list[p_header->mode];
496 if( p_mode == NULL )
497 return VLC_EGENERIC;
499 if( p_header->speex_version_id > 1 )
501 msg_Err( p_dec, "this file was encoded with Speex bit-stream "
502 "version %d which is not supported by this decoder.",
503 p_header->speex_version_id );
504 return VLC_EGENERIC;
507 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
509 msg_Err( p_dec, "file encoded with a newer version of Speex." );
510 return VLC_EGENERIC;
512 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
514 msg_Err( p_dec, "file encoded with an older version of Speex." );
515 return VLC_EGENERIC;
518 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
519 p_header->rate, p_mode->modeName,
520 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
521 p_header->vbr ? ", VBR)" : ")" );
523 /* Take care of speex decoder init */
524 speex_bits_init( &p_sys->bits );
525 p_sys->p_state = p_state = speex_decoder_init( p_mode );
526 if( !p_state )
528 msg_Err( p_dec, "decoder initialization failed" );
529 return VLC_EGENERIC;
532 if( p_header->nb_channels == 2 )
534 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
535 p_sys->stereo = stereo;
536 callback.callback_id = SPEEX_INBAND_STEREO;
537 callback.func = speex_std_stereo_request_handler;
538 callback.data = &p_sys->stereo;
539 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
541 if( p_header->nb_channels <= 0 ||
542 p_header->nb_channels > 5 )
544 msg_Err( p_dec, "invalid number of channels (not between 1 and 5): %i",
545 p_header->nb_channels );
546 return VLC_EGENERIC;
549 /* Setup the format */
550 p_dec->fmt_out.audio.i_physical_channels =
551 pi_channels_maps[p_header->nb_channels];
552 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
553 p_dec->fmt_out.audio.i_rate = p_header->rate;
555 date_Init( &p_sys->end_date, p_header->rate, 1 );
557 return VLC_SUCCESS;
560 /*****************************************************************************
561 * Flush:
562 *****************************************************************************/
563 static void Flush( decoder_t *p_dec )
565 decoder_sys_t *p_sys = p_dec->p_sys;
567 date_Set( &p_sys->end_date, 0 );
570 /*****************************************************************************
571 * ProcessPacket: processes a Speex packet.
572 *****************************************************************************/
573 static block_t *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
574 block_t **pp_block )
576 decoder_sys_t *p_sys = p_dec->p_sys;
577 block_t *p_block = *pp_block;
579 /* Date management */
580 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
581 p_block->i_pts != date_Get( &p_sys->end_date ) )
583 date_Set( &p_sys->end_date, p_block->i_pts );
586 if( !date_Get( &p_sys->end_date ) )
588 /* We've just started the stream, wait for the first PTS. */
589 if( p_block ) block_Release( p_block );
590 return NULL;
593 *pp_block = NULL; /* To avoid being fed the same packet again */
595 if( p_sys->b_packetizer )
597 if ( p_sys->p_header->frames_per_packet > 1 )
599 short *p_frame_holder = NULL;
600 int i_bits_before = 0, i_bits_after = 0, i_bytes_in_speex_frame = 0,
601 i_pcm_output_size = 0, i_bits_in_speex_frame = 0;
602 block_t *p_new_block = NULL;
604 i_pcm_output_size = p_sys->p_header->frame_size;
605 p_frame_holder = (short*)xmalloc( sizeof(short)*i_pcm_output_size );
607 speex_bits_read_from( &p_sys->bits, (char*)p_oggpacket->packet,
608 p_oggpacket->bytes);
609 i_bits_before = speex_bits_remaining( &p_sys->bits );
610 speex_decode_int(p_sys->p_state, &p_sys->bits, p_frame_holder);
611 i_bits_after = speex_bits_remaining( &p_sys->bits );
613 i_bits_in_speex_frame = i_bits_before - i_bits_after;
614 i_bytes_in_speex_frame = ( i_bits_in_speex_frame +
615 (8 - (i_bits_in_speex_frame % 8)) )
616 / 8;
618 p_new_block = block_Alloc( i_bytes_in_speex_frame );
619 memset( p_new_block->p_buffer, 0xff, i_bytes_in_speex_frame );
622 * Copy the first frame in this packet to a new packet.
624 speex_bits_rewind( &p_sys->bits );
625 speex_bits_write( &p_sys->bits,
626 (char*)p_new_block->p_buffer,
627 (int)i_bytes_in_speex_frame );
630 * Move the remaining part of the original packet (subsequent
631 * frames, if there are any) into the beginning
632 * of the original packet so
633 * they are preserved following the realloc.
634 * Note: Any bits that
635 * remain in the initial packet
636 * are "filler" if they do not constitute
637 * an entire byte.
639 if ( i_bits_after > 7 )
641 /* round-down since we rounded-up earlier (to include
642 * the speex terminator code.
644 i_bytes_in_speex_frame--;
645 speex_bits_write( &p_sys->bits,
646 (char*)p_block->p_buffer,
647 p_block->i_buffer - i_bytes_in_speex_frame );
648 p_block = block_Realloc( p_block,
650 p_block->i_buffer-i_bytes_in_speex_frame );
651 *pp_block = p_block;
653 else
655 speex_bits_reset( &p_sys->bits );
658 free( p_frame_holder );
659 return SendPacket( p_dec, p_new_block);
661 else
663 return SendPacket( p_dec, p_block );
666 else
668 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
670 if( p_block )
671 block_Release( p_block );
672 return p_aout_buffer;
676 static int DecodeRtpSpeexPacket( decoder_t *p_dec, block_t *p_speex_bit_block )
678 decoder_sys_t *p_sys = p_dec->p_sys;
679 block_t *p_aout_buffer;
680 int i_decode_ret;
681 unsigned int i_speex_frame_size;
683 if ( !p_speex_bit_block || p_speex_bit_block->i_pts <= VLC_TS_INVALID )
684 return VLCDEC_SUCCESS;
687 If the SpeexBits buffer size is 0 (a default value),
688 we know that a proper initialization has not yet been done.
690 if ( p_sys->bits.buf_size==0 )
692 p_sys->p_header = malloc(sizeof(SpeexHeader));
693 if ( !p_sys->p_header )
695 msg_Err( p_dec, "Could not allocate a Speex header.");
696 return VLCDEC_SUCCESS;
699 const SpeexMode *mode = speex_lib_get_mode((p_sys->rtp_rate / 8000) >> 1);
701 speex_init_header( p_sys->p_header,p_sys->rtp_rate, 1, mode );
702 speex_bits_init( &p_sys->bits );
703 p_sys->p_state = speex_decoder_init( mode );
704 if ( !p_sys->p_state )
706 msg_Err( p_dec, "Could not allocate a Speex decoder." );
707 free( p_sys->p_header );
708 return VLCDEC_SUCCESS;
712 Assume that variable bit rate is enabled. Also assume
713 that there is only one frame per packet.
715 p_sys->p_header->vbr = 1;
716 p_sys->p_header->frames_per_packet = 1;
718 p_dec->fmt_out.audio.i_channels = p_sys->p_header->nb_channels;
719 p_dec->fmt_out.audio.i_physical_channels =
720 pi_channels_maps[p_sys->p_header->nb_channels];
721 p_dec->fmt_out.audio.i_rate = p_sys->p_header->rate;
723 if ( speex_mode_query( &speex_nb_mode,
724 SPEEX_MODE_FRAME_SIZE,
725 &i_speex_frame_size ) )
727 msg_Err( p_dec, "Could not determine the frame size." );
728 speex_decoder_destroy( p_sys->p_state );
729 free( p_sys->p_header );
730 return VLCDEC_SUCCESS;
732 p_dec->fmt_out.audio.i_bytes_per_frame = i_speex_frame_size;
734 date_Init(&p_sys->end_date, p_sys->p_header->rate, 1);
738 If the SpeexBits are initialized but there is
739 still no header, an error must be thrown.
741 if ( !p_sys->p_header )
743 msg_Err( p_dec, "There is no valid Speex header found." );
744 return VLCDEC_SUCCESS;
747 if ( !date_Get( &p_sys->end_date ) )
748 date_Set( &p_sys->end_date, p_speex_bit_block->i_dts );
751 Ask for a new audio output buffer and make sure
752 we get one.
754 if( decoder_UpdateAudioFormat( p_dec ) )
755 p_aout_buffer = NULL;
756 else
757 p_aout_buffer = decoder_NewAudioBuffer( p_dec,
758 p_sys->p_header->frame_size );
759 if ( !p_aout_buffer || p_aout_buffer->i_buffer == 0 )
761 msg_Err(p_dec, "Oops: No new buffer was returned!");
762 return VLCDEC_SUCCESS;
766 Read the Speex payload into the SpeexBits buffer.
768 speex_bits_read_from( &p_sys->bits,
769 (char*)p_speex_bit_block->p_buffer,
770 p_speex_bit_block->i_buffer );
773 Decode the input and ensure that no errors
774 were encountered.
776 i_decode_ret = speex_decode_int( p_sys->p_state, &p_sys->bits,
777 (int16_t*)p_aout_buffer->p_buffer );
778 if ( i_decode_ret < 0 )
780 msg_Err( p_dec, "Decoding failed. Perhaps we have a bad stream?" );
781 return VLCDEC_SUCCESS;
785 Handle date management on the audio output buffer.
787 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
788 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
789 p_sys->p_header->frame_size ) - p_aout_buffer->i_pts;
792 p_sys->i_frame_in_packet++;
793 block_Release( p_speex_bit_block );
794 decoder_QueueAudio( p_dec, p_aout_buffer );
795 return VLCDEC_SUCCESS;
798 /*****************************************************************************
799 * DecodePacket: decodes a Speex packet.
800 *****************************************************************************/
801 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
803 decoder_sys_t *p_sys = p_dec->p_sys;
805 if( p_oggpacket->bytes )
807 /* Copy Ogg packet to Speex bitstream */
808 speex_bits_read_from( &p_sys->bits, (char *)p_oggpacket->packet,
809 p_oggpacket->bytes );
810 p_sys->i_frame_in_packet = 0;
813 /* Decode one frame at a time */
814 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
816 block_t *p_aout_buffer;
817 if( p_sys->p_header->frame_size == 0 )
818 return NULL;
820 if( decoder_UpdateAudioFormat( p_dec ) )
821 return NULL;
822 p_aout_buffer =
823 decoder_NewAudioBuffer( p_dec, p_sys->p_header->frame_size );
824 if( !p_aout_buffer )
826 return NULL;
829 switch( speex_decode_int( p_sys->p_state, &p_sys->bits,
830 (int16_t *)p_aout_buffer->p_buffer ) )
832 case -2:
833 msg_Err( p_dec, "decoding error: corrupted stream?" );
834 case -1: /* End of stream */
835 return NULL;
838 if( speex_bits_remaining( &p_sys->bits ) < 0 )
840 msg_Err( p_dec, "decoding overflow: corrupted stream?" );
843 if( p_sys->p_header->nb_channels == 2 )
844 speex_decode_stereo_int( (int16_t *)p_aout_buffer->p_buffer,
845 p_sys->p_header->frame_size,
846 &p_sys->stereo );
848 /* Date management */
849 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
850 p_aout_buffer->i_length =
851 date_Increment( &p_sys->end_date, p_sys->p_header->frame_size )
852 - p_aout_buffer->i_pts;
854 p_sys->i_frame_in_packet++;
856 return p_aout_buffer;
858 else
860 return NULL;
864 /*****************************************************************************
865 * SendPacket: send an ogg packet to the stream output.
866 *****************************************************************************/
867 static block_t *SendPacket( decoder_t *p_dec, block_t *p_block )
869 decoder_sys_t *p_sys = p_dec->p_sys;
871 /* Date management */
872 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
874 p_block->i_length =
875 date_Increment( &p_sys->end_date,
876 p_sys->p_header->frame_size ) -
877 p_block->i_pts;
879 return p_block;
882 /*****************************************************************************
883 * ParseSpeexComments:
884 *****************************************************************************/
886 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
888 decoder_sys_t *p_sys = p_dec->p_sys;
889 const SpeexMode *p_mode;
891 assert( p_sys->p_header->mode < SPEEX_NB_MODES );
893 p_mode = speex_mode_list[p_sys->p_header->mode];
894 assert( p_mode != NULL );
896 if( !p_dec->p_description )
898 p_dec->p_description = vlc_meta_New();
899 if( !p_dec->p_description )
900 return;
903 /* */
904 char *psz_mode;
905 if( asprintf( &psz_mode, "%s%s", p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" ) >= 0 )
907 vlc_meta_AddExtra( p_dec->p_description, _("Mode"), psz_mode );
908 free( psz_mode );
911 /* TODO: finish comments parsing */
912 VLC_UNUSED( p_oggpacket );
915 /*****************************************************************************
916 * CloseDecoder: speex decoder destruction
917 *****************************************************************************/
918 static void CloseDecoder( vlc_object_t *p_this )
920 decoder_t * p_dec = (decoder_t *)p_this;
921 decoder_sys_t *p_sys = p_dec->p_sys;
923 if( p_sys->p_state )
925 speex_decoder_destroy( p_sys->p_state );
926 speex_bits_destroy( &p_sys->bits );
929 free( p_sys->p_header );
930 free( p_sys );
933 #ifdef ENABLE_SOUT
934 /*****************************************************************************
935 * encoder_sys_t: encoder descriptor
936 *****************************************************************************/
937 #define MAX_FRAME_BYTES 2000
939 struct encoder_sys_t
942 * Input properties
944 char *p_buffer;
945 char p_buffer_out[MAX_FRAME_BYTES];
948 * Speex properties
950 SpeexBits bits;
951 SpeexHeader header;
952 SpeexStereoState stereo;
953 void *p_state;
955 int i_frames_per_packet;
956 int i_frames_in_packet;
958 int i_frame_length;
959 int i_samples_delay;
960 int i_frame_size;
963 static block_t *Encode ( encoder_t *, block_t * );
965 /*****************************************************************************
966 * OpenEncoder: probe the encoder and return score
967 *****************************************************************************/
968 static int OpenEncoder( vlc_object_t *p_this )
970 encoder_t *p_enc = (encoder_t *)p_this;
971 encoder_sys_t *p_sys;
972 const SpeexMode *p_speex_mode = &speex_nb_mode;
973 int i_tmp, i;
974 const char *pp_header[2];
975 int pi_header[2];
976 uint8_t *p_extra;
978 if( p_enc->fmt_out.i_codec != VLC_CODEC_SPEEX &&
979 !p_enc->obj.force )
981 return VLC_EGENERIC;
984 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
985 switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) )
987 case 1:
988 msg_Dbg( p_enc, "Using wideband" );
989 p_speex_mode = &speex_wb_mode;
990 break;
991 case 2:
992 msg_Dbg( p_enc, "Using ultra-wideband" );
993 p_speex_mode = &speex_uwb_mode;
994 break;
995 default:
996 msg_Dbg( p_enc, "Using narrowband" );
997 p_speex_mode = &speex_nb_mode;
998 break;
1001 /* Allocate the memory needed to store the decoder's structure */
1002 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
1003 return VLC_ENOMEM;
1004 p_enc->p_sys = p_sys;
1005 p_enc->pf_encode_audio = Encode;
1006 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
1007 p_enc->fmt_out.i_codec = VLC_CODEC_SPEEX;
1009 speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
1010 1, p_speex_mode );
1012 p_sys->header.frames_per_packet = 1;
1013 p_sys->header.vbr = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
1014 p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
1016 /* Create a new encoder state in narrowband mode */
1017 p_sys->p_state = speex_encoder_init( p_speex_mode );
1019 /* Parameters */
1020 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "complexity" );
1021 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_COMPLEXITY, &i_tmp );
1023 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
1024 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR, &i_tmp );
1026 if( i_tmp == 0 ) /* CBR */
1028 i_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
1029 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_tmp );
1031 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "vad" ) ? 1 : 0;
1032 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VAD, &i_tmp );
1034 else
1036 float f_tmp;
1038 f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
1039 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_QUALITY, &f_tmp );
1041 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
1042 if( i_tmp > 0 )
1043 #ifdef SPEEX_SET_VBR_MAX_BITRATE
1044 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_MAX_BITRATE, &i_tmp );
1045 #else
1046 msg_Dbg( p_enc, "max-bitrate cannot be set in this version of libspeex");
1047 #endif
1050 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "dtx" ) ? 1 : 0;
1051 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_DTX, &i_tmp );
1054 /*Initialization of the structure that holds the bits*/
1055 speex_bits_init( &p_sys->bits );
1057 p_sys->i_frames_in_packet = 0;
1058 p_sys->i_samples_delay = 0;
1060 speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
1061 &p_sys->i_frame_length );
1063 p_sys->i_frame_size = p_sys->i_frame_length *
1064 sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
1065 p_sys->p_buffer = xmalloc( p_sys->i_frame_size );
1067 /* Create and store headers */
1068 pp_header[0] = speex_header_to_packet( &p_sys->header, &pi_header[0] );
1069 pp_header[1] = "ENCODER=VLC media player";
1070 pi_header[1] = sizeof("ENCODER=VLC media player");
1072 p_enc->fmt_out.i_extra = 3 * 2 + pi_header[0] + pi_header[1];
1073 p_extra = p_enc->fmt_out.p_extra = xmalloc( p_enc->fmt_out.i_extra );
1074 for( i = 0; i < 2; i++ )
1076 *(p_extra++) = pi_header[i] >> 8;
1077 *(p_extra++) = pi_header[i] & 0xFF;
1078 memcpy( p_extra, pp_header[i], pi_header[i] );
1079 p_extra += pi_header[i];
1082 msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
1083 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
1084 p_enc->fmt_in.audio.i_rate );
1086 return VLC_SUCCESS;
1089 /****************************************************************************
1090 * Encode: the whole thing
1091 ****************************************************************************
1092 * This function spits out ogg packets.
1093 ****************************************************************************/
1094 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
1096 encoder_sys_t *p_sys = p_enc->p_sys;
1097 block_t *p_block, *p_chain = NULL;
1099 /* Encoder gets NULL when it's time to flush */
1100 if( unlikely( !p_aout_buf ) ) return NULL;
1102 unsigned char *p_buffer = p_aout_buf->p_buffer;
1103 unsigned i_samples = p_aout_buf->i_nb_samples;
1104 int i_samples_delay = p_sys->i_samples_delay;
1106 mtime_t i_pts = p_aout_buf->i_pts -
1107 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1108 (mtime_t)p_enc->fmt_in.audio.i_rate;
1110 p_sys->i_samples_delay += i_samples;
1112 while( p_sys->i_samples_delay >= p_sys->i_frame_length )
1114 int16_t *p_samples;
1115 int i_out;
1117 if( i_samples_delay )
1119 /* Take care of the left-over from last time */
1120 int i_delay_size = i_samples_delay * 2 *
1121 p_enc->fmt_in.audio.i_channels;
1122 int i_size = p_sys->i_frame_size - i_delay_size;
1124 p_samples = (int16_t *)p_sys->p_buffer;
1125 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
1126 p_buffer -= i_delay_size;
1127 i_samples += i_samples_delay;
1128 i_samples_delay = 0;
1130 else
1132 p_samples = (int16_t *)p_buffer;
1135 /* Encode current frame */
1136 if( p_enc->fmt_in.audio.i_channels == 2 )
1137 speex_encode_stereo_int( p_samples, p_sys->i_frame_length,
1138 &p_sys->bits );
1140 #if 0
1141 if( p_sys->preprocess )
1142 speex_preprocess( p_sys->preprocess, p_samples, NULL );
1143 #endif
1145 speex_encode_int( p_sys->p_state, p_samples, &p_sys->bits );
1147 p_buffer += p_sys->i_frame_size;
1148 p_sys->i_samples_delay -= p_sys->i_frame_length;
1149 i_samples -= p_sys->i_frame_length;
1151 p_sys->i_frames_in_packet++;
1153 if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
1154 continue;
1156 p_sys->i_frames_in_packet = 0;
1158 speex_bits_insert_terminator( &p_sys->bits );
1159 i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
1160 MAX_FRAME_BYTES );
1161 speex_bits_reset( &p_sys->bits );
1163 p_block = block_Alloc( i_out );
1164 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
1166 p_block->i_length = (mtime_t)1000000 *
1167 (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
1168 (mtime_t)p_enc->fmt_in.audio.i_rate;
1170 p_block->i_dts = p_block->i_pts = i_pts;
1172 /* Update pts */
1173 i_pts += p_block->i_length;
1174 block_ChainAppend( &p_chain, p_block );
1178 /* Backup the remaining raw samples */
1179 if( i_samples )
1181 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
1182 p_enc->fmt_in.audio.i_channels, p_buffer,
1183 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
1186 return p_chain;
1189 /*****************************************************************************
1190 * CloseEncoder: encoder destruction
1191 *****************************************************************************/
1192 static void CloseEncoder( vlc_object_t *p_this )
1194 encoder_t *p_enc = (encoder_t *)p_this;
1195 encoder_sys_t *p_sys = p_enc->p_sys;
1197 speex_encoder_destroy( p_sys->p_state );
1198 speex_bits_destroy( &p_sys->bits );
1200 free( p_sys->p_buffer );
1201 free( p_sys );
1203 #endif