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;
340 /* Block to Ogg packet */
341 oggpacket.packet = (*pp_block)->p_buffer;
342 oggpacket.bytes = (*pp_block)->i_buffer;
346 if( p_sys->b_packetizer ) return NULL;
348 /* Block to Ogg packet */
349 oggpacket.packet = NULL;
353 oggpacket.granulepos = -1;
356 oggpacket.packetno = 0;
358 /* Check for headers */
359 if( !p_sys->b_has_headers )
361 if( !p_dec->fmt_in.p_extra )
363 msg_Warn( p_dec, "Header missing, using default settings" );
365 if( CreateDefaultHeader( p_dec ) )
367 block_Release( *pp_block );
371 else if( ProcessHeaders( p_dec ) )
373 block_Release( *pp_block );
376 p_sys->b_has_headers = true;
379 return ProcessPacket( p_dec, &oggpacket, pp_block );
382 /*****************************************************************************
383 * ProcessHeaders: process Speex headers.
384 *****************************************************************************/
385 static int ProcessHeaders( decoder_t *p_dec )
387 decoder_sys_t *p_sys = p_dec->p_sys;
388 ogg_packet oggpacket;
390 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
391 void *pp_data[XIPH_MAX_HEADER_COUNT];
393 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
394 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
397 return VLC_EGENERIC;;
399 oggpacket.granulepos = -1;
401 oggpacket.packetno = 0;
403 /* Take care of the initial Vorbis header */
404 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
405 oggpacket.bytes = pi_size[0];
406 oggpacket.packet = pp_data[0];
407 if( ProcessInitialHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
409 msg_Err( p_dec, "initial Speex header is corrupted" );
410 return VLC_EGENERIC;;
413 /* The next packet in order is the comments header */
415 oggpacket.bytes = pi_size[1];
416 oggpacket.packet = pp_data[1];
417 ParseSpeexComments( p_dec, &oggpacket );
419 if( p_sys->b_packetizer )
421 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
422 p_dec->fmt_out.p_extra = xrealloc( p_dec->fmt_out.p_extra,
423 p_dec->fmt_out.i_extra );
424 memcpy( p_dec->fmt_out.p_extra,
425 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
431 /*****************************************************************************
432 * ProcessInitialHeader: processes the inital Speex header packet.
433 *****************************************************************************/
434 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
436 decoder_sys_t *p_sys = p_dec->p_sys;
439 SpeexHeader *p_header;
440 const SpeexMode *p_mode;
441 SpeexCallback callback;
443 p_sys->p_header = p_header =
444 speex_packet_to_header( (char *)p_oggpacket->packet,
445 p_oggpacket->bytes );
448 msg_Err( p_dec, "cannot read Speex header" );
451 if( p_header->mode >= SPEEX_NB_MODES || p_header->mode < 0 )
453 msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
454 "this version of libspeex.", p_header->mode );
458 p_mode = speex_mode_list[p_header->mode];
462 if( p_header->speex_version_id > 1 )
464 msg_Err( p_dec, "this file was encoded with Speex bit-stream "
465 "version %d which is not supported by this decoder.",
466 p_header->speex_version_id );
470 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
472 msg_Err( p_dec, "file encoded with a newer version of Speex." );
475 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
477 msg_Err( p_dec, "file encoded with an older version of Speex." );
481 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
482 p_header->rate, p_mode->modeName,
483 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
484 p_header->vbr ? ", VBR)" : ")" );
486 /* Take care of speex decoder init */
487 speex_bits_init( &p_sys->bits );
488 p_sys->p_state = p_state = speex_decoder_init( p_mode );
491 msg_Err( p_dec, "decoder initialization failed" );
495 if( p_header->nb_channels == 2 )
497 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
498 p_sys->stereo = stereo;
499 callback.callback_id = SPEEX_INBAND_STEREO;
500 callback.func = speex_std_stereo_request_handler;
501 callback.data = &p_sys->stereo;
502 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
504 if( p_header->nb_channels <= 0 ||
505 p_header->nb_channels > 5 )
507 msg_Err( p_dec, "invalid number of channels (not between 1 and 5): %i",
508 p_header->nb_channels );
512 /* Setup the format */
513 p_dec->fmt_out.audio.i_physical_channels =
514 p_dec->fmt_out.audio.i_original_channels =
515 pi_channels_maps[p_header->nb_channels];
516 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
517 p_dec->fmt_out.audio.i_rate = p_header->rate;
519 date_Init( &p_sys->end_date, p_header->rate, 1 );
524 /*****************************************************************************
525 * ProcessPacket: processes a Speex packet.
526 *****************************************************************************/
527 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
530 decoder_sys_t *p_sys = p_dec->p_sys;
531 block_t *p_block = *pp_block;
533 /* Date management */
534 if( p_block && p_block->i_pts > VLC_TS_INVALID &&
535 p_block->i_pts != date_Get( &p_sys->end_date ) )
537 date_Set( &p_sys->end_date, p_block->i_pts );
540 if( !date_Get( &p_sys->end_date ) )
542 /* We've just started the stream, wait for the first PTS. */
543 if( p_block ) block_Release( p_block );
547 *pp_block = NULL; /* To avoid being fed the same packet again */
549 if( p_sys->b_packetizer )
551 if ( p_sys->p_header->frames_per_packet > 1 )
553 short *p_frame_holder = NULL;
554 int i_bits_before = 0, i_bits_after = 0, i_bytes_in_speex_frame = 0,
555 i_pcm_output_size = 0, i_bits_in_speex_frame = 0;
556 block_t *p_new_block = NULL;
558 i_pcm_output_size = p_sys->p_header->frame_size;
559 p_frame_holder = (short*)xmalloc( sizeof(short)*i_pcm_output_size );
561 speex_bits_read_from( &p_sys->bits, (char*)p_oggpacket->packet,
563 i_bits_before = speex_bits_remaining( &p_sys->bits );
564 speex_decode_int(p_sys->p_state, &p_sys->bits, p_frame_holder);
565 i_bits_after = speex_bits_remaining( &p_sys->bits );
567 i_bits_in_speex_frame = i_bits_before - i_bits_after;
568 i_bytes_in_speex_frame = ( i_bits_in_speex_frame +
569 (8 - (i_bits_in_speex_frame % 8)) )
572 p_new_block = block_Alloc( i_bytes_in_speex_frame );
573 memset( p_new_block->p_buffer, 0xff, i_bytes_in_speex_frame );
576 * Copy the first frame in this packet to a new packet.
578 speex_bits_rewind( &p_sys->bits );
579 speex_bits_write( &p_sys->bits,
580 (char*)p_new_block->p_buffer,
581 (int)i_bytes_in_speex_frame );
584 * Move the remaining part of the original packet (subsequent
585 * frames, if there are any) into the beginning
586 * of the original packet so
587 * they are preserved following the realloc.
588 * Note: Any bits that
589 * remain in the initial packet
590 * are "filler" if they do not constitute
593 if ( i_bits_after > 7 )
595 /* round-down since we rounded-up earlier (to include
596 * the speex terminator code.
598 i_bytes_in_speex_frame--;
599 speex_bits_write( &p_sys->bits,
600 (char*)p_block->p_buffer,
601 p_block->i_buffer - i_bytes_in_speex_frame );
602 p_block = block_Realloc( p_block,
604 p_block->i_buffer-i_bytes_in_speex_frame );
609 speex_bits_reset( &p_sys->bits );
612 free( p_frame_holder );
613 return SendPacket( p_dec, p_new_block);
617 return SendPacket( p_dec, p_block );
622 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
625 block_Release( p_block );
626 return p_aout_buffer;
630 static block_t *DecodeRtpSpeexPacket( decoder_t *p_dec, block_t **pp_block )
632 block_t *p_speex_bit_block = *pp_block;
633 decoder_sys_t *p_sys = p_dec->p_sys;
634 block_t *p_aout_buffer;
636 unsigned int i_speex_frame_size;
638 if ( !p_speex_bit_block || p_speex_bit_block->i_pts <= VLC_TS_INVALID )
642 If the SpeexBits buffer size is 0 (a default value),
643 we know that a proper initialization has not yet been done.
645 if ( p_sys->bits.buf_size==0 )
647 p_sys->p_header = malloc(sizeof(SpeexHeader));
648 if ( !p_sys->p_header )
650 msg_Err( p_dec, "Could not allocate a Speex header.");
654 const SpeexMode *mode = speex_lib_get_mode((p_sys->rtp_rate / 8000) >> 1);
656 speex_init_header( p_sys->p_header,p_sys->rtp_rate, 1, mode );
657 speex_bits_init( &p_sys->bits );
658 p_sys->p_state = speex_decoder_init( mode );
659 if ( !p_sys->p_state )
661 msg_Err( p_dec, "Could not allocate a Speex decoder." );
662 free( p_sys->p_header );
667 Assume that variable bit rate is enabled. Also assume
668 that there is only one frame per packet.
670 p_sys->p_header->vbr = 1;
671 p_sys->p_header->frames_per_packet = 1;
673 p_dec->fmt_out.audio.i_channels = p_sys->p_header->nb_channels;
674 p_dec->fmt_out.audio.i_physical_channels =
675 p_dec->fmt_out.audio.i_original_channels =
676 pi_channels_maps[p_sys->p_header->nb_channels];
677 p_dec->fmt_out.audio.i_rate = p_sys->p_header->rate;
679 if ( speex_mode_query( &speex_nb_mode,
680 SPEEX_MODE_FRAME_SIZE,
681 &i_speex_frame_size ) )
683 msg_Err( p_dec, "Could not determine the frame size." );
684 speex_decoder_destroy( p_sys->p_state );
685 free( p_sys->p_header );
688 p_dec->fmt_out.audio.i_bytes_per_frame = i_speex_frame_size;
690 date_Init(&p_sys->end_date, p_sys->p_header->rate, 1);
694 If the SpeexBits are initialized but there is
695 still no header, an error must be thrown.
697 if ( !p_sys->p_header )
699 msg_Err( p_dec, "There is no valid Speex header found." );
704 if ( !date_Get( &p_sys->end_date ) )
705 date_Set( &p_sys->end_date, p_speex_bit_block->i_dts );
708 Ask for a new audio output buffer and make sure
711 p_aout_buffer = decoder_NewAudioBuffer( p_dec,
712 p_sys->p_header->frame_size );
713 if ( !p_aout_buffer || p_aout_buffer->i_buffer == 0 )
715 msg_Err(p_dec, "Oops: No new buffer was returned!");
720 Read the Speex payload into the SpeexBits buffer.
722 speex_bits_read_from( &p_sys->bits,
723 (char*)p_speex_bit_block->p_buffer,
724 p_speex_bit_block->i_buffer );
727 Decode the input and ensure that no errors
730 i_decode_ret = speex_decode_int( p_sys->p_state, &p_sys->bits,
731 (int16_t*)p_aout_buffer->p_buffer );
732 if ( i_decode_ret < 0 )
734 msg_Err( p_dec, "Decoding failed. Perhaps we have a bad stream?" );
739 Handle date management on the audio output buffer.
741 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
742 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
743 p_sys->p_header->frame_size ) - p_aout_buffer->i_pts;
746 p_sys->i_frame_in_packet++;
747 block_Release( p_speex_bit_block );
749 return p_aout_buffer;
752 /*****************************************************************************
753 * DecodePacket: decodes a Speex packet.
754 *****************************************************************************/
755 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
757 decoder_sys_t *p_sys = p_dec->p_sys;
759 if( p_oggpacket->bytes )
761 /* Copy Ogg packet to Speex bitstream */
762 speex_bits_read_from( &p_sys->bits, (char *)p_oggpacket->packet,
763 p_oggpacket->bytes );
764 p_sys->i_frame_in_packet = 0;
767 /* Decode one frame at a time */
768 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
770 block_t *p_aout_buffer;
771 if( p_sys->p_header->frame_size == 0 )
775 decoder_NewAudioBuffer( p_dec, p_sys->p_header->frame_size );
781 switch( speex_decode_int( p_sys->p_state, &p_sys->bits,
782 (int16_t *)p_aout_buffer->p_buffer ) )
785 msg_Err( p_dec, "decoding error: corrupted stream?" );
786 case -1: /* End of stream */
790 if( speex_bits_remaining( &p_sys->bits ) < 0 )
792 msg_Err( p_dec, "decoding overflow: corrupted stream?" );
795 if( p_sys->p_header->nb_channels == 2 )
796 speex_decode_stereo_int( (int16_t *)p_aout_buffer->p_buffer,
797 p_sys->p_header->frame_size,
800 /* Date management */
801 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
802 p_aout_buffer->i_length =
803 date_Increment( &p_sys->end_date, p_sys->p_header->frame_size )
804 - p_aout_buffer->i_pts;
806 p_sys->i_frame_in_packet++;
808 return p_aout_buffer;
816 /*****************************************************************************
817 * SendPacket: send an ogg packet to the stream output.
818 *****************************************************************************/
819 static block_t *SendPacket( decoder_t *p_dec, block_t *p_block )
821 decoder_sys_t *p_sys = p_dec->p_sys;
823 /* Date management */
824 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
827 date_Increment( &p_sys->end_date,
828 p_sys->p_header->frame_size ) -
834 /*****************************************************************************
835 * ParseSpeexComments:
836 *****************************************************************************/
837 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
838 ((buf[base+2]<<16)&0xff0000)| \
839 ((buf[base+1]<<8)&0xff00)| \
842 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
844 decoder_sys_t *p_sys = p_dec->p_sys;
845 const SpeexMode *p_mode;
847 assert( p_sys->p_header->mode < SPEEX_NB_MODES );
849 p_mode = speex_mode_list[p_sys->p_header->mode];
850 assert( p_mode != NULL );
852 if( !p_dec->p_description )
854 p_dec->p_description = vlc_meta_New();
855 if( !p_dec->p_description )
861 if( asprintf( &psz_mode, "%s%s", p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" ) >= 0 )
863 vlc_meta_AddExtra( p_dec->p_description, _("Mode"), psz_mode );
867 /* TODO: finish comments parsing */
868 VLC_UNUSED( p_oggpacket );
871 /*****************************************************************************
872 * CloseDecoder: speex decoder destruction
873 *****************************************************************************/
874 static void CloseDecoder( vlc_object_t *p_this )
876 decoder_t * p_dec = (decoder_t *)p_this;
877 decoder_sys_t *p_sys = p_dec->p_sys;
881 speex_decoder_destroy( p_sys->p_state );
882 speex_bits_destroy( &p_sys->bits );
885 free( p_sys->p_header );
890 /*****************************************************************************
891 * encoder_sys_t: encoder descriptor
892 *****************************************************************************/
893 #define MAX_FRAME_SIZE 2000
894 #define MAX_FRAME_BYTES 2000
902 char p_buffer_out[MAX_FRAME_BYTES];
909 SpeexStereoState stereo;
912 int i_frames_per_packet;
913 int i_frames_in_packet;
920 static block_t *Encode ( encoder_t *, block_t * );
922 /*****************************************************************************
923 * OpenEncoder: probe the encoder and return score
924 *****************************************************************************/
925 static int OpenEncoder( vlc_object_t *p_this )
927 encoder_t *p_enc = (encoder_t *)p_this;
928 encoder_sys_t *p_sys;
929 const SpeexMode *p_speex_mode = &speex_nb_mode;
931 const char *pp_header[2];
935 if( p_enc->fmt_out.i_codec != VLC_CODEC_SPEEX &&
941 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
942 switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) )
945 msg_Dbg( p_enc, "Using wideband" );
946 p_speex_mode = &speex_wb_mode;
949 msg_Dbg( p_enc, "Using ultra-wideband" );
950 p_speex_mode = &speex_uwb_mode;
953 msg_Dbg( p_enc, "Using narrowband" );
954 p_speex_mode = &speex_nb_mode;
958 /* Allocate the memory needed to store the decoder's structure */
959 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
961 p_enc->p_sys = p_sys;
962 p_enc->pf_encode_audio = Encode;
963 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
964 p_enc->fmt_out.i_codec = VLC_CODEC_SPEEX;
966 speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
969 p_sys->header.frames_per_packet = 1;
970 p_sys->header.vbr = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
971 p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
973 /* Create a new encoder state in narrowband mode */
974 p_sys->p_state = speex_encoder_init( p_speex_mode );
977 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "complexity" );
978 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_COMPLEXITY, &i_tmp );
980 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
981 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR, &i_tmp );
983 if( i_tmp == 0 ) /* CBR */
985 i_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
986 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_tmp );
988 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "vad" ) ? 1 : 0;
989 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VAD, &i_tmp );
995 f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
996 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_QUALITY, &f_tmp );
998 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
1000 #ifdef SPEEX_SET_VBR_MAX_BITRATE
1001 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_MAX_BITRATE, &i_tmp );
1003 msg_Dbg( p_enc, "max-bitrate cannot be set in this version of libspeex");
1007 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "dtx" ) ? 1 : 0;
1008 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_DTX, &i_tmp );
1011 /*Initialization of the structure that holds the bits*/
1012 speex_bits_init( &p_sys->bits );
1014 p_sys->i_frames_in_packet = 0;
1015 p_sys->i_samples_delay = 0;
1017 speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
1018 &p_sys->i_frame_length );
1020 p_sys->i_frame_size = p_sys->i_frame_length *
1021 sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
1022 p_sys->p_buffer = xmalloc( p_sys->i_frame_size );
1024 /* Create and store headers */
1025 pp_header[0] = speex_header_to_packet( &p_sys->header, &pi_header[0] );
1026 pp_header[1] = "ENCODER=VLC media player";
1027 pi_header[1] = sizeof("ENCODER=VLC media player");
1029 p_enc->fmt_out.i_extra = 3 * 2 + pi_header[0] + pi_header[1];
1030 p_extra = p_enc->fmt_out.p_extra = xmalloc( p_enc->fmt_out.i_extra );
1031 for( i = 0; i < 2; i++ )
1033 *(p_extra++) = pi_header[i] >> 8;
1034 *(p_extra++) = pi_header[i] & 0xFF;
1035 memcpy( p_extra, pp_header[i], pi_header[i] );
1036 p_extra += pi_header[i];
1039 msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
1040 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
1041 p_enc->fmt_in.audio.i_rate );
1046 /****************************************************************************
1047 * Encode: the whole thing
1048 ****************************************************************************
1049 * This function spits out ogg packets.
1050 ****************************************************************************/
1051 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
1053 encoder_sys_t *p_sys = p_enc->p_sys;
1054 block_t *p_block, *p_chain = NULL;
1056 /* Encoder gets NULL when it's time to flush */
1057 if( unlikely( !p_aout_buf ) ) return NULL;
1059 unsigned char *p_buffer = p_aout_buf->p_buffer;
1060 int i_samples = p_aout_buf->i_nb_samples;
1061 int i_samples_delay = p_sys->i_samples_delay;
1063 mtime_t i_pts = p_aout_buf->i_pts -
1064 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
1065 (mtime_t)p_enc->fmt_in.audio.i_rate;
1067 p_sys->i_samples_delay += i_samples;
1069 while( p_sys->i_samples_delay >= p_sys->i_frame_length )
1074 if( i_samples_delay )
1076 /* Take care of the left-over from last time */
1077 int i_delay_size = i_samples_delay * 2 *
1078 p_enc->fmt_in.audio.i_channels;
1079 int i_size = p_sys->i_frame_size - i_delay_size;
1081 p_samples = (int16_t *)p_sys->p_buffer;
1082 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
1083 p_buffer -= i_delay_size;
1084 i_samples += i_samples_delay;
1085 i_samples_delay = 0;
1089 p_samples = (int16_t *)p_buffer;
1092 /* Encode current frame */
1093 if( p_enc->fmt_in.audio.i_channels == 2 )
1094 speex_encode_stereo_int( p_samples, p_sys->i_frame_length,
1098 if( p_sys->preprocess )
1099 speex_preprocess( p_sys->preprocess, p_samples, NULL );
1102 speex_encode_int( p_sys->p_state, p_samples, &p_sys->bits );
1104 p_buffer += p_sys->i_frame_size;
1105 p_sys->i_samples_delay -= p_sys->i_frame_length;
1106 i_samples -= p_sys->i_frame_length;
1108 p_sys->i_frames_in_packet++;
1110 if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
1113 p_sys->i_frames_in_packet = 0;
1115 speex_bits_insert_terminator( &p_sys->bits );
1116 i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
1118 speex_bits_reset( &p_sys->bits );
1120 p_block = block_Alloc( i_out );
1121 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
1123 p_block->i_length = (mtime_t)1000000 *
1124 (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
1125 (mtime_t)p_enc->fmt_in.audio.i_rate;
1127 p_block->i_dts = p_block->i_pts = i_pts;
1130 i_pts += p_block->i_length;
1131 block_ChainAppend( &p_chain, p_block );
1135 /* Backup the remaining raw samples */
1138 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
1139 p_enc->fmt_in.audio.i_channels, p_buffer,
1140 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
1146 /*****************************************************************************
1147 * CloseEncoder: encoder destruction
1148 *****************************************************************************/
1149 static void CloseEncoder( vlc_object_t *p_this )
1151 encoder_t *p_enc = (encoder_t *)p_this;
1152 encoder_sys_t *p_sys = p_enc->p_sys;
1154 speex_encoder_destroy( p_sys->p_state );
1155 speex_bits_destroy( &p_sys->bits );
1157 free( p_sys->p_buffer );