1 /*****************************************************************************
2 * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
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 /*****************************************************************************
26 *****************************************************************************/
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"
38 #include <speex/speex.h>
39 #include <speex/speex_header.h>
40 #include <speex/speex_stereo.h>
41 #include <speex/speex_callbacks.h>
45 /*****************************************************************************
47 *****************************************************************************/
48 static int OpenDecoder ( vlc_object_t * );
49 static int OpenPacketizer( vlc_object_t * );
50 static void CloseDecoder ( vlc_object_t * );
53 static int OpenEncoder ( vlc_object_t * );
54 static void CloseEncoder ( vlc_object_t * );
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
95 set_category( CAT_INPUT )
96 set_subcategory( SUBCAT_INPUT_ACODEC )
98 set_description( N_("Speex audio decoder") )
99 set_capability( "decoder", 100 )
100 set_shortname( N_("Speex") )
101 set_callbacks( OpenDecoder, CloseDecoder )
104 set_description( N_("Speex audio packetizer") )
105 set_capability( "packetizer", 100 )
106 set_callbacks( OpenPacketizer, CloseDecoder )
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, */
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 *****************************************************************************/
159 int i_frame_in_packet;
165 SpeexHeader *p_header;
166 SpeexStereoState stereo;
168 unsigned int rtp_rate;
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 /****************************************************************************
190 ****************************************************************************/
192 static block_t *DecodeBlock ( decoder_t *, block_t ** );
193 static block_t *DecodeRtpSpeexPacket( decoder_t *, block_t **);
194 static int ProcessHeaders( decoder_t * );
195 static int ProcessInitialHeader ( decoder_t *, ogg_packet * );
196 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
198 static block_t *DecodePacket( decoder_t *, ogg_packet * );
199 static block_t *SendPacket( decoder_t *, block_t * );
201 static void ParseSpeexComments( decoder_t *, ogg_packet * );
203 /*****************************************************************************
204 * OpenDecoder: probe the decoder and return score
205 *****************************************************************************/
206 static int OpenDecoder( vlc_object_t *p_this )
208 decoder_t *p_dec = (decoder_t*)p_this;
209 decoder_sys_t *p_sys;
211 if( p_dec->fmt_in.i_codec != VLC_CODEC_SPEEX )
214 /* Allocate the memory needed to store the decoder's structure */
215 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
217 p_dec->p_sys->bits.buf_size = 0;
218 p_dec->p_sys->b_packetizer = false;
219 p_dec->p_sys->rtp_rate = p_dec->fmt_in.audio.i_rate;
220 p_dec->p_sys->b_has_headers = false;
222 date_Set( &p_sys->end_date, 0 );
224 /* Set output properties */
225 p_dec->fmt_out.i_cat = AUDIO_ES;
226 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
230 If the codec is spxr then this decoder is
231 being invoked on a Speex stream arriving via RTP.
232 A special decoder callback is used.
234 if (p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('s', 'p', 'x', 'r'))
236 msg_Dbg( p_dec, "Using RTP version of Speex decoder @ rate %d.",
237 p_dec->fmt_in.audio.i_rate );
238 p_dec->pf_decode_audio = DecodeRtpSpeexPacket;
242 p_dec->pf_decode_audio = DecodeBlock;
244 p_dec->pf_packetize = DecodeBlock;
246 p_sys->p_state = NULL;
247 p_sys->p_header = NULL;
248 p_sys->i_frame_in_packet = 0;
253 static int OpenPacketizer( vlc_object_t *p_this )
255 decoder_t *p_dec = (decoder_t*)p_this;
257 int i_ret = OpenDecoder( p_this );
259 if( i_ret == VLC_SUCCESS )
261 p_dec->p_sys->b_packetizer = true;
262 p_dec->fmt_out.i_codec = VLC_CODEC_SPEEX;
268 static int CreateDefaultHeader( decoder_t *p_dec )
270 ogg_packet oggpacket;
271 SpeexHeader *p_header = malloc( sizeof(SpeexHeader) );
275 const int rate = p_dec->fmt_in.audio.i_rate;
276 const unsigned i_mode = (rate / 8000) >> 1;
278 const SpeexMode *mode;
279 int ret = VLC_SUCCESS;
280 oggpacket.packet = NULL;
287 mode = speex_lib_get_mode( i_mode );
290 msg_Err( p_dec, "Unexpected rate %d", rate );
295 speex_init_header( p_header, rate, p_dec->fmt_in.audio.i_channels, mode );
296 p_header->frames_per_packet = 160 << i_mode;
298 oggpacket.packet = (unsigned char *) speex_header_to_packet( p_header,
299 (int *) &oggpacket.bytes );
300 if( !oggpacket.packet )
308 oggpacket.granulepos = -1;
309 oggpacket.packetno = 0;
311 ret = ProcessInitialHeader( p_dec, &oggpacket );
313 if( ret != VLC_SUCCESS )
315 msg_Err( p_dec, "default Speex header is corrupted" );
319 free( oggpacket.packet );
326 /****************************************************************************
327 * DecodeBlock: the whole thing
328 ****************************************************************************
329 * This function must be fed with ogg packets.
330 ****************************************************************************/
331 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
333 decoder_sys_t *p_sys = p_dec->p_sys;
334 ogg_packet oggpacket;
336 if( !pp_block ) return NULL;
338 block_t *block = *pp_block;
342 /* Block to Ogg packet */
343 oggpacket.packet = block->p_buffer;
344 oggpacket.bytes = block->i_buffer;
348 if( p_sys->b_packetizer ) return NULL;
350 /* Block to Ogg packet */
351 oggpacket.packet = NULL;
355 oggpacket.granulepos = -1;
358 oggpacket.packetno = 0;
360 /* Check for headers */
361 if( !p_sys->b_has_headers )
363 if( !p_dec->fmt_in.p_extra )
365 msg_Warn( p_dec, "Header missing, using default settings" );
367 if( CreateDefaultHeader( p_dec ) )
370 block_Release( block );
374 else if( ProcessHeaders( p_dec ) )
377 block_Release( block );
380 p_sys->b_has_headers = true;
383 return ProcessPacket( p_dec, &oggpacket, pp_block );
386 /*****************************************************************************
387 * ProcessHeaders: process Speex headers.
388 *****************************************************************************/
389 static int ProcessHeaders( decoder_t *p_dec )
391 decoder_sys_t *p_sys = p_dec->p_sys;
392 ogg_packet oggpacket;
394 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
395 void *pp_data[XIPH_MAX_HEADER_COUNT];
397 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
398 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
401 return VLC_EGENERIC;;
403 oggpacket.granulepos = -1;
405 oggpacket.packetno = 0;
407 /* Take care of the initial Vorbis header */
408 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
409 oggpacket.bytes = pi_size[0];
410 oggpacket.packet = pp_data[0];
411 if( ProcessInitialHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
413 msg_Err( p_dec, "initial Speex header is corrupted" );
414 return VLC_EGENERIC;;
417 /* The next packet in order is the comments header */
419 oggpacket.bytes = pi_size[1];
420 oggpacket.packet = pp_data[1];
421 ParseSpeexComments( p_dec, &oggpacket );
423 if( p_sys->b_packetizer )
425 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
426 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
427 p_dec->fmt_out.i_extra );
428 memcpy( p_dec->fmt_out.p_extra,
429 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
435 /*****************************************************************************
436 * ProcessInitialHeader: processes the inital Speex header packet.
437 *****************************************************************************/
438 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
440 decoder_sys_t *p_sys = p_dec->p_sys;
443 SpeexHeader *p_header;
444 const SpeexMode *p_mode;
445 SpeexCallback callback;
447 p_sys->p_header = p_header =
448 speex_packet_to_header( (char *)p_oggpacket->packet,
449 p_oggpacket->bytes );
452 msg_Err( p_dec, "cannot read Speex header" );
455 if( p_header->mode >= SPEEX_NB_MODES || p_header->mode < 0 )
457 msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
458 "this version of libspeex.", p_header->mode );
462 p_mode = speex_mode_list[p_header->mode];
466 if( p_header->speex_version_id > 1 )
468 msg_Err( p_dec, "this file was encoded with Speex bit-stream "
469 "version %d which is not supported by this decoder.",
470 p_header->speex_version_id );
474 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
476 msg_Err( p_dec, "file encoded with a newer version of Speex." );
479 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
481 msg_Err( p_dec, "file encoded with an older version of Speex." );
485 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
486 p_header->rate, p_mode->modeName,
487 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
488 p_header->vbr ? ", VBR)" : ")" );
490 /* Take care of speex decoder init */
491 speex_bits_init( &p_sys->bits );
492 p_sys->p_state = p_state = speex_decoder_init( p_mode );
495 msg_Err( p_dec, "decoder initialization failed" );
499 if( p_header->nb_channels == 2 )
501 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
502 p_sys->stereo = stereo;
503 callback.callback_id = SPEEX_INBAND_STEREO;
504 callback.func = speex_std_stereo_request_handler;
505 callback.data = &p_sys->stereo;
506 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
508 if( p_header->nb_channels <= 0 ||
509 p_header->nb_channels > 5 )
511 msg_Err( p_dec, "invalid number of channels (not between 1 and 5): %i",
512 p_header->nb_channels );
516 /* Setup the format */
517 p_dec->fmt_out.audio.i_physical_channels =
518 p_dec->fmt_out.audio.i_original_channels =
519 pi_channels_maps[p_header->nb_channels];
520 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
521 p_dec->fmt_out.audio.i_rate = p_header->rate;
523 date_Init( &p_sys->end_date, p_header->rate, 1 );
528 /*****************************************************************************
529 * ProcessPacket: processes a Speex packet.
530 *****************************************************************************/
531 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
534 decoder_sys_t *p_sys = p_dec->p_sys;
535 block_t *p_block = *pp_block;
537 /* Date management */
538 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
539 p_block->i_pts != date_Get( &p_sys->end_date ) )
541 date_Set( &p_sys->end_date, p_block->i_pts );
544 if( !date_Get( &p_sys->end_date ) )
546 /* We've just started the stream, wait for the first PTS. */
547 if( p_block ) block_Release( p_block );
551 *pp_block = NULL; /* To avoid being fed the same packet again */
553 if( p_sys->b_packetizer )
555 if ( p_sys->p_header->frames_per_packet > 1 )
557 short *p_frame_holder = NULL;
558 int i_bits_before = 0, i_bits_after = 0, i_bytes_in_speex_frame = 0,
559 i_pcm_output_size = 0, i_bits_in_speex_frame = 0;
560 block_t *p_new_block = NULL;
562 i_pcm_output_size = p_sys->p_header->frame_size;
563 p_frame_holder = (short*)xmalloc( sizeof(short)*i_pcm_output_size );
565 speex_bits_read_from( &p_sys->bits, (char*)p_oggpacket->packet,
567 i_bits_before = speex_bits_remaining( &p_sys->bits );
568 speex_decode_int(p_sys->p_state, &p_sys->bits, p_frame_holder);
569 i_bits_after = speex_bits_remaining( &p_sys->bits );
571 i_bits_in_speex_frame = i_bits_before - i_bits_after;
572 i_bytes_in_speex_frame = ( i_bits_in_speex_frame +
573 (8 - (i_bits_in_speex_frame % 8)) )
576 p_new_block = block_Alloc( i_bytes_in_speex_frame );
577 memset( p_new_block->p_buffer, 0xff, i_bytes_in_speex_frame );
580 * Copy the first frame in this packet to a new packet.
582 speex_bits_rewind( &p_sys->bits );
583 speex_bits_write( &p_sys->bits,
584 (char*)p_new_block->p_buffer,
585 (int)i_bytes_in_speex_frame );
588 * Move the remaining part of the original packet (subsequent
589 * frames, if there are any) into the beginning
590 * of the original packet so
591 * they are preserved following the realloc.
592 * Note: Any bits that
593 * remain in the initial packet
594 * are "filler" if they do not constitute
597 if ( i_bits_after > 7 )
599 /* round-down since we rounded-up earlier (to include
600 * the speex terminator code.
602 i_bytes_in_speex_frame--;
603 speex_bits_write( &p_sys->bits,
604 (char*)p_block->p_buffer,
605 p_block->i_buffer - i_bytes_in_speex_frame );
606 p_block = block_Realloc( p_block,
608 p_block->i_buffer-i_bytes_in_speex_frame );
613 speex_bits_reset( &p_sys->bits );
616 free( p_frame_holder );
617 return SendPacket( p_dec, p_new_block);
621 return SendPacket( p_dec, p_block );
626 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
629 block_Release( p_block );
630 return p_aout_buffer;
634 static block_t *DecodeRtpSpeexPacket( decoder_t *p_dec, block_t **pp_block )
636 block_t *p_speex_bit_block = *pp_block;
637 decoder_sys_t *p_sys = p_dec->p_sys;
638 block_t *p_aout_buffer;
640 unsigned int i_speex_frame_size;
642 if ( !p_speex_bit_block || p_speex_bit_block->i_pts <= VLC_TS_INVALID )
646 If the SpeexBits buffer size is 0 (a default value),
647 we know that a proper initialization has not yet been done.
649 if ( p_sys->bits.buf_size==0 )
651 p_sys->p_header = malloc(sizeof(SpeexHeader));
652 if ( !p_sys->p_header )
654 msg_Err( p_dec, "Could not allocate a Speex header.");
658 const SpeexMode *mode = speex_lib_get_mode((p_sys->rtp_rate / 8000) >> 1);
660 speex_init_header( p_sys->p_header,p_sys->rtp_rate, 1, mode );
661 speex_bits_init( &p_sys->bits );
662 p_sys->p_state = speex_decoder_init( mode );
663 if ( !p_sys->p_state )
665 msg_Err( p_dec, "Could not allocate a Speex decoder." );
666 free( p_sys->p_header );
671 Assume that variable bit rate is enabled. Also assume
672 that there is only one frame per packet.
674 p_sys->p_header->vbr = 1;
675 p_sys->p_header->frames_per_packet = 1;
677 p_dec->fmt_out.audio.i_channels = p_sys->p_header->nb_channels;
678 p_dec->fmt_out.audio.i_physical_channels =
679 p_dec->fmt_out.audio.i_original_channels =
680 pi_channels_maps[p_sys->p_header->nb_channels];
681 p_dec->fmt_out.audio.i_rate = p_sys->p_header->rate;
683 if ( speex_mode_query( &speex_nb_mode,
684 SPEEX_MODE_FRAME_SIZE,
685 &i_speex_frame_size ) )
687 msg_Err( p_dec, "Could not determine the frame size." );
688 speex_decoder_destroy( p_sys->p_state );
689 free( p_sys->p_header );
692 p_dec->fmt_out.audio.i_bytes_per_frame = i_speex_frame_size;
694 date_Init(&p_sys->end_date, p_sys->p_header->rate, 1);
698 If the SpeexBits are initialized but there is
699 still no header, an error must be thrown.
701 if ( !p_sys->p_header )
703 msg_Err( p_dec, "There is no valid Speex header found." );
708 if ( !date_Get( &p_sys->end_date ) )
709 date_Set( &p_sys->end_date, p_speex_bit_block->i_dts );
712 Ask for a new audio output buffer and make sure
715 p_aout_buffer = decoder_NewAudioBuffer( p_dec,
716 p_sys->p_header->frame_size );
717 if ( !p_aout_buffer || p_aout_buffer->i_buffer == 0 )
719 msg_Err(p_dec, "Oops: No new buffer was returned!");
724 Read the Speex payload into the SpeexBits buffer.
726 speex_bits_read_from( &p_sys->bits,
727 (char*)p_speex_bit_block->p_buffer,
728 p_speex_bit_block->i_buffer );
731 Decode the input and ensure that no errors
734 i_decode_ret = speex_decode_int( p_sys->p_state, &p_sys->bits,
735 (int16_t*)p_aout_buffer->p_buffer );
736 if ( i_decode_ret < 0 )
738 msg_Err( p_dec, "Decoding failed. Perhaps we have a bad stream?" );
743 Handle date management on the audio output buffer.
745 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
746 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
747 p_sys->p_header->frame_size ) - p_aout_buffer->i_pts;
750 p_sys->i_frame_in_packet++;
751 block_Release( p_speex_bit_block );
753 return p_aout_buffer;
756 /*****************************************************************************
757 * DecodePacket: decodes a Speex packet.
758 *****************************************************************************/
759 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
761 decoder_sys_t *p_sys = p_dec->p_sys;
763 if( p_oggpacket->bytes )
765 /* Copy Ogg packet to Speex bitstream */
766 speex_bits_read_from( &p_sys->bits, (char *)p_oggpacket->packet,
767 p_oggpacket->bytes );
768 p_sys->i_frame_in_packet = 0;
771 /* Decode one frame at a time */
772 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
774 block_t *p_aout_buffer;
775 if( p_sys->p_header->frame_size == 0 )
779 decoder_NewAudioBuffer( p_dec, p_sys->p_header->frame_size );
785 switch( speex_decode_int( p_sys->p_state, &p_sys->bits,
786 (int16_t *)p_aout_buffer->p_buffer ) )
789 msg_Err( p_dec, "decoding error: corrupted stream?" );
790 case -1: /* End of stream */
794 if( speex_bits_remaining( &p_sys->bits ) < 0 )
796 msg_Err( p_dec, "decoding overflow: corrupted stream?" );
799 if( p_sys->p_header->nb_channels == 2 )
800 speex_decode_stereo_int( (int16_t *)p_aout_buffer->p_buffer,
801 p_sys->p_header->frame_size,
804 /* Date management */
805 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
806 p_aout_buffer->i_length =
807 date_Increment( &p_sys->end_date, p_sys->p_header->frame_size )
808 - p_aout_buffer->i_pts;
810 p_sys->i_frame_in_packet++;
812 return p_aout_buffer;
820 /*****************************************************************************
821 * SendPacket: send an ogg packet to the stream output.
822 *****************************************************************************/
823 static block_t *SendPacket( decoder_t *p_dec, block_t *p_block )
825 decoder_sys_t *p_sys = p_dec->p_sys;
827 /* Date management */
828 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
831 date_Increment( &p_sys->end_date,
832 p_sys->p_header->frame_size ) -
838 /*****************************************************************************
839 * ParseSpeexComments:
840 *****************************************************************************/
841 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
842 ((buf[base+2]<<16)&0xff0000)| \
843 ((buf[base+1]<<8)&0xff00)| \
846 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
848 decoder_sys_t *p_sys = p_dec->p_sys;
849 const SpeexMode *p_mode;
851 assert( p_sys->p_header->mode < SPEEX_NB_MODES );
853 p_mode = speex_mode_list[p_sys->p_header->mode];
854 assert( p_mode != NULL );
856 if( !p_dec->p_description )
858 p_dec->p_description = vlc_meta_New();
859 if( !p_dec->p_description )
865 if( asprintf( &psz_mode, "%s%s", p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" ) >= 0 )
867 vlc_meta_AddExtra( p_dec->p_description, _("Mode"), psz_mode );
871 /* TODO: finish comments parsing */
872 VLC_UNUSED( p_oggpacket );
875 /*****************************************************************************
876 * CloseDecoder: speex decoder destruction
877 *****************************************************************************/
878 static void CloseDecoder( vlc_object_t *p_this )
880 decoder_t * p_dec = (decoder_t *)p_this;
881 decoder_sys_t *p_sys = p_dec->p_sys;
885 speex_decoder_destroy( p_sys->p_state );
886 speex_bits_destroy( &p_sys->bits );
889 free( p_sys->p_header );
894 /*****************************************************************************
895 * encoder_sys_t: encoder descriptor
896 *****************************************************************************/
897 #define MAX_FRAME_SIZE 2000
898 #define MAX_FRAME_BYTES 2000
906 char p_buffer_out[MAX_FRAME_BYTES];
913 SpeexStereoState stereo;
916 int i_frames_per_packet;
917 int i_frames_in_packet;
924 static block_t *Encode ( encoder_t *, block_t * );
926 /*****************************************************************************
927 * OpenEncoder: probe the encoder and return score
928 *****************************************************************************/
929 static int OpenEncoder( vlc_object_t *p_this )
931 encoder_t *p_enc = (encoder_t *)p_this;
932 encoder_sys_t *p_sys;
933 const SpeexMode *p_speex_mode = &speex_nb_mode;
935 const char *pp_header[2];
939 if( p_enc->fmt_out.i_codec != VLC_CODEC_SPEEX &&
945 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
946 switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) )
949 msg_Dbg( p_enc, "Using wideband" );
950 p_speex_mode = &speex_wb_mode;
953 msg_Dbg( p_enc, "Using ultra-wideband" );
954 p_speex_mode = &speex_uwb_mode;
957 msg_Dbg( p_enc, "Using narrowband" );
958 p_speex_mode = &speex_nb_mode;
962 /* Allocate the memory needed to store the decoder's structure */
963 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
965 p_enc->p_sys = p_sys;
966 p_enc->pf_encode_audio = Encode;
967 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
968 p_enc->fmt_out.i_codec = VLC_CODEC_SPEEX;
970 speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
973 p_sys->header.frames_per_packet = 1;
974 p_sys->header.vbr = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
975 p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
977 /* Create a new encoder state in narrowband mode */
978 p_sys->p_state = speex_encoder_init( p_speex_mode );
981 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "complexity" );
982 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_COMPLEXITY, &i_tmp );
984 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
985 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR, &i_tmp );
987 if( i_tmp == 0 ) /* CBR */
989 i_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
990 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_tmp );
992 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "vad" ) ? 1 : 0;
993 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VAD, &i_tmp );
999 f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
1000 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_QUALITY, &f_tmp );
1002 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
1004 #ifdef SPEEX_SET_VBR_MAX_BITRATE
1005 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_MAX_BITRATE, &i_tmp );
1007 msg_Dbg( p_enc, "max-bitrate cannot be set in this version of libspeex");
1011 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "dtx" ) ? 1 : 0;
1012 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_DTX, &i_tmp );
1015 /*Initialization of the structure that holds the bits*/
1016 speex_bits_init( &p_sys->bits );
1018 p_sys->i_frames_in_packet = 0;
1019 p_sys->i_samples_delay = 0;
1021 speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
1022 &p_sys->i_frame_length );
1024 p_sys->i_frame_size = p_sys->i_frame_length *
1025 sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
1026 p_sys->p_buffer = xmalloc( p_sys->i_frame_size );
1028 /* Create and store headers */
1029 pp_header[0] = speex_header_to_packet( &p_sys->header, &pi_header[0] );
1030 pp_header[1] = "ENCODER=VLC media player";
1031 pi_header[1] = sizeof("ENCODER=VLC media player");
1033 p_enc->fmt_out.i_extra = 3 * 2 + pi_header[0] + pi_header[1];
1034 p_extra = p_enc->fmt_out.p_extra = xmalloc( p_enc->fmt_out.i_extra );
1035 for( i = 0; i < 2; i++ )
1037 *(p_extra++) = pi_header[i] >> 8;
1038 *(p_extra++) = pi_header[i] & 0xFF;
1039 memcpy( p_extra, pp_header[i], pi_header[i] );
1040 p_extra += pi_header[i];
1043 msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
1044 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
1045 p_enc->fmt_in.audio.i_rate );
1050 /****************************************************************************
1051 * Encode: the whole thing
1052 ****************************************************************************
1053 * This function spits out ogg packets.
1054 ****************************************************************************/
1055 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
1057 encoder_sys_t *p_sys = p_enc->p_sys;
1058 block_t *p_block, *p_chain = NULL;
1060 /* Encoder gets NULL when it's time to flush */
1061 if( unlikely( !p_aout_buf ) ) return NULL;
1063 unsigned char *p_buffer = p_aout_buf->p_buffer;
1064 int i_samples = p_aout_buf->i_nb_samples;
1065 int i_samples_delay = p_sys->i_samples_delay;
1067 mtime_t i_pts = p_aout_buf->i_pts -
1068 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1069 (mtime_t)p_enc->fmt_in.audio.i_rate;
1071 p_sys->i_samples_delay += i_samples;
1073 while( p_sys->i_samples_delay >= p_sys->i_frame_length )
1078 if( i_samples_delay )
1080 /* Take care of the left-over from last time */
1081 int i_delay_size = i_samples_delay * 2 *
1082 p_enc->fmt_in.audio.i_channels;
1083 int i_size = p_sys->i_frame_size - i_delay_size;
1085 p_samples = (int16_t *)p_sys->p_buffer;
1086 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
1087 p_buffer -= i_delay_size;
1088 i_samples += i_samples_delay;
1089 i_samples_delay = 0;
1093 p_samples = (int16_t *)p_buffer;
1096 /* Encode current frame */
1097 if( p_enc->fmt_in.audio.i_channels == 2 )
1098 speex_encode_stereo_int( p_samples, p_sys->i_frame_length,
1102 if( p_sys->preprocess )
1103 speex_preprocess( p_sys->preprocess, p_samples, NULL );
1106 speex_encode_int( p_sys->p_state, p_samples, &p_sys->bits );
1108 p_buffer += p_sys->i_frame_size;
1109 p_sys->i_samples_delay -= p_sys->i_frame_length;
1110 i_samples -= p_sys->i_frame_length;
1112 p_sys->i_frames_in_packet++;
1114 if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
1117 p_sys->i_frames_in_packet = 0;
1119 speex_bits_insert_terminator( &p_sys->bits );
1120 i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
1122 speex_bits_reset( &p_sys->bits );
1124 p_block = block_Alloc( i_out );
1125 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
1127 p_block->i_length = (mtime_t)1000000 *
1128 (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
1129 (mtime_t)p_enc->fmt_in.audio.i_rate;
1131 p_block->i_dts = p_block->i_pts = i_pts;
1134 i_pts += p_block->i_length;
1135 block_ChainAppend( &p_chain, p_block );
1139 /* Backup the remaining raw samples */
1142 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
1143 p_enc->fmt_in.audio.i_channels, p_buffer,
1144 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
1150 /*****************************************************************************
1151 * CloseEncoder: encoder destruction
1152 *****************************************************************************/
1153 static void CloseEncoder( vlc_object_t *p_this )
1155 encoder_t *p_enc = (encoder_t *)p_this;
1156 encoder_sys_t *p_sys = p_enc->p_sys;
1158 speex_encoder_destroy( p_sys->p_state );
1159 speex_bits_destroy( &p_sys->bits );
1161 free( p_sys->p_buffer );