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
*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
)
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
)
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_cat
= AUDIO_ES
;
228 p_dec
->fmt_out
.i_codec
= VLC_CODEC_S16N
;
232 If the codec is spxr then this decoder is
233 being invoked on a Speex stream arriving via RTP.
234 A special decoder callback is used.
236 if (p_dec
->fmt_in
.i_original_fourcc
== VLC_FOURCC('s', 'p', 'x', 'r'))
238 msg_Dbg( p_dec
, "Using RTP version of Speex decoder @ rate %d.",
239 p_dec
->fmt_in
.audio
.i_rate
);
240 p_dec
->pf_decode
= DecodeRtpSpeexPacket
;
244 p_dec
->pf_decode
= DecodeAudio
;
246 p_dec
->pf_packetize
= Packetize
;
247 p_dec
->pf_flush
= Flush
;
249 p_sys
->p_state
= NULL
;
250 p_sys
->p_header
= NULL
;
251 p_sys
->i_frame_in_packet
= 0;
256 static int OpenPacketizer( vlc_object_t
*p_this
)
258 decoder_t
*p_dec
= (decoder_t
*)p_this
;
260 int i_ret
= OpenDecoder( p_this
);
262 if( i_ret
== VLC_SUCCESS
)
264 p_dec
->p_sys
->b_packetizer
= true;
265 p_dec
->fmt_out
.i_codec
= VLC_CODEC_SPEEX
;
271 static int CreateDefaultHeader( decoder_t
*p_dec
)
273 ogg_packet oggpacket
;
274 SpeexHeader
*p_header
= malloc( sizeof(SpeexHeader
) );
278 const int rate
= p_dec
->fmt_in
.audio
.i_rate
;
279 const unsigned i_mode
= (rate
/ 8000) >> 1;
281 const SpeexMode
*mode
;
282 int ret
= VLC_SUCCESS
;
283 oggpacket
.packet
= NULL
;
290 mode
= speex_lib_get_mode( i_mode
);
293 msg_Err( p_dec
, "Unexpected rate %d", rate
);
298 speex_init_header( p_header
, rate
, p_dec
->fmt_in
.audio
.i_channels
, mode
);
299 p_header
->frames_per_packet
= 160 << i_mode
;
301 oggpacket
.packet
= (unsigned char *) speex_header_to_packet( p_header
,
302 (int *) &oggpacket
.bytes
);
303 if( !oggpacket
.packet
)
311 oggpacket
.granulepos
= -1;
312 oggpacket
.packetno
= 0;
314 ret
= ProcessInitialHeader( p_dec
, &oggpacket
);
316 if( ret
!= VLC_SUCCESS
)
318 msg_Err( p_dec
, "default Speex header is corrupted" );
322 free( oggpacket
.packet
);
329 /****************************************************************************
330 * DecodeBlock: the whole thing
331 ****************************************************************************
332 * This function must be fed with ogg packets.
333 ****************************************************************************/
334 static block_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
336 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
337 ogg_packet oggpacket
;
339 block_t
*block
= *pp_block
;
343 if( block
->i_flags
& (BLOCK_FLAG_CORRUPTED
|BLOCK_FLAG_DISCONTINUITY
) )
346 if( block
->i_flags
& BLOCK_FLAG_CORRUPTED
)
348 block_Release( block
);
353 /* Block to Ogg packet */
354 oggpacket
.packet
= block
->p_buffer
;
355 oggpacket
.bytes
= block
->i_buffer
;
359 if( p_sys
->b_packetizer
) return NULL
;
361 /* Block to Ogg packet */
362 oggpacket
.packet
= NULL
;
366 oggpacket
.granulepos
= -1;
369 oggpacket
.packetno
= 0;
371 /* Check for headers */
372 if( !p_sys
->b_has_headers
)
374 if( !p_dec
->fmt_in
.p_extra
)
376 msg_Warn( p_dec
, "Header missing, using default settings" );
378 if( CreateDefaultHeader( p_dec
) )
381 block_Release( block
);
385 else if( ProcessHeaders( p_dec
) )
388 block_Release( block
);
391 p_sys
->b_has_headers
= true;
394 return ProcessPacket( p_dec
, &oggpacket
, pp_block
);
397 static int DecodeAudio( decoder_t
*p_dec
, block_t
*p_block
)
399 if( p_block
== NULL
) /* No Drain */
400 return VLCDEC_SUCCESS
;
402 block_t
**pp_block
= &p_block
, *p_out
;
403 while( ( p_out
= DecodeBlock( p_dec
, pp_block
) ) != NULL
)
404 decoder_QueueAudio( p_dec
, p_out
);
405 return VLCDEC_SUCCESS
;
408 static block_t
*Packetize( decoder_t
*p_dec
, block_t
**pp_block
)
410 if( pp_block
== NULL
) /* No Drain */
412 return DecodeBlock( p_dec
, pp_block
);
415 /*****************************************************************************
416 * ProcessHeaders: process Speex headers.
417 *****************************************************************************/
418 static int ProcessHeaders( decoder_t
*p_dec
)
420 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
421 ogg_packet oggpacket
;
423 unsigned pi_size
[XIPH_MAX_HEADER_COUNT
];
424 void *pp_data
[XIPH_MAX_HEADER_COUNT
];
426 if( xiph_SplitHeaders( pi_size
, pp_data
, &i_count
,
427 p_dec
->fmt_in
.i_extra
, p_dec
->fmt_in
.p_extra
) )
430 return VLC_EGENERIC
;;
432 oggpacket
.granulepos
= -1;
434 oggpacket
.packetno
= 0;
436 /* Take care of the initial Vorbis header */
437 oggpacket
.b_o_s
= 1; /* yes this actually is a b_o_s packet :) */
438 oggpacket
.bytes
= pi_size
[0];
439 oggpacket
.packet
= pp_data
[0];
440 if( ProcessInitialHeader( p_dec
, &oggpacket
) != VLC_SUCCESS
)
442 msg_Err( p_dec
, "initial Speex header is corrupted" );
443 return VLC_EGENERIC
;;
446 /* The next packet in order is the comments header */
448 oggpacket
.bytes
= pi_size
[1];
449 oggpacket
.packet
= pp_data
[1];
450 ParseSpeexComments( p_dec
, &oggpacket
);
452 if( p_sys
->b_packetizer
)
454 void* p_extra
= realloc( p_dec
->fmt_out
.p_extra
,
455 p_dec
->fmt_in
.i_extra
);
456 if( unlikely( p_extra
== NULL
) )
460 p_dec
->fmt_out
.p_extra
= p_extra
;
461 p_dec
->fmt_out
.i_extra
= p_dec
->fmt_in
.i_extra
;
462 memcpy( p_dec
->fmt_out
.p_extra
,
463 p_dec
->fmt_in
.p_extra
, p_dec
->fmt_out
.i_extra
);
469 /*****************************************************************************
470 * ProcessInitialHeader: processes the inital Speex header packet.
471 *****************************************************************************/
472 static int ProcessInitialHeader( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
)
474 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
477 SpeexHeader
*p_header
;
478 const SpeexMode
*p_mode
;
479 SpeexCallback callback
;
481 p_sys
->p_header
= p_header
=
482 speex_packet_to_header( (char *)p_oggpacket
->packet
,
483 p_oggpacket
->bytes
);
486 msg_Err( p_dec
, "cannot read Speex header" );
489 if( p_header
->mode
>= SPEEX_NB_MODES
|| p_header
->mode
< 0 )
491 msg_Err( p_dec
, "mode number %d does not (yet/any longer) exist in "
492 "this version of libspeex.", p_header
->mode
);
496 p_mode
= speex_mode_list
[p_header
->mode
];
500 if( p_header
->speex_version_id
> 1 )
502 msg_Err( p_dec
, "this file was encoded with Speex bit-stream "
503 "version %d which is not supported by this decoder.",
504 p_header
->speex_version_id
);
508 if( p_mode
->bitstream_version
< p_header
->mode_bitstream_version
)
510 msg_Err( p_dec
, "file encoded with a newer version of Speex." );
513 if( p_mode
->bitstream_version
> p_header
->mode_bitstream_version
)
515 msg_Err( p_dec
, "file encoded with an older version of Speex." );
519 msg_Dbg( p_dec
, "Speex %d Hz audio using %s mode %s%s",
520 p_header
->rate
, p_mode
->modeName
,
521 ( p_header
->nb_channels
== 1 ) ? " (mono" : " (stereo",
522 p_header
->vbr
? ", VBR)" : ")" );
524 /* Take care of speex decoder init */
525 speex_bits_init( &p_sys
->bits
);
526 p_sys
->p_state
= p_state
= speex_decoder_init( p_mode
);
529 msg_Err( p_dec
, "decoder initialization failed" );
533 if( p_header
->nb_channels
== 2 )
535 SpeexStereoState stereo
= SPEEX_STEREO_STATE_INIT
;
536 p_sys
->stereo
= stereo
;
537 callback
.callback_id
= SPEEX_INBAND_STEREO
;
538 callback
.func
= speex_std_stereo_request_handler
;
539 callback
.data
= &p_sys
->stereo
;
540 speex_decoder_ctl( p_state
, SPEEX_SET_HANDLER
, &callback
);
542 if( p_header
->nb_channels
<= 0 ||
543 p_header
->nb_channels
> 5 )
545 msg_Err( p_dec
, "invalid number of channels (not between 1 and 5): %i",
546 p_header
->nb_channels
);
550 /* Setup the format */
551 p_dec
->fmt_out
.audio
.i_physical_channels
=
552 p_dec
->fmt_out
.audio
.i_original_channels
=
553 pi_channels_maps
[p_header
->nb_channels
];
554 p_dec
->fmt_out
.audio
.i_channels
= p_header
->nb_channels
;
555 p_dec
->fmt_out
.audio
.i_rate
= p_header
->rate
;
557 date_Init( &p_sys
->end_date
, p_header
->rate
, 1 );
562 /*****************************************************************************
564 *****************************************************************************/
565 static void Flush( decoder_t
*p_dec
)
567 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
569 date_Set( &p_sys
->end_date
, 0 );
572 /*****************************************************************************
573 * ProcessPacket: processes a Speex packet.
574 *****************************************************************************/
575 static block_t
*ProcessPacket( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
,
578 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
579 block_t
*p_block
= *pp_block
;
581 /* Date management */
582 if( p_block
&& p_block
->i_pts
> VLC_TS_INVALID
&&
583 p_block
->i_pts
!= date_Get( &p_sys
->end_date
) )
585 date_Set( &p_sys
->end_date
, p_block
->i_pts
);
588 if( !date_Get( &p_sys
->end_date
) )
590 /* We've just started the stream, wait for the first PTS. */
591 if( p_block
) block_Release( p_block
);
595 *pp_block
= NULL
; /* To avoid being fed the same packet again */
597 if( p_sys
->b_packetizer
)
599 if ( p_sys
->p_header
->frames_per_packet
> 1 )
601 short *p_frame_holder
= NULL
;
602 int i_bits_before
= 0, i_bits_after
= 0, i_bytes_in_speex_frame
= 0,
603 i_pcm_output_size
= 0, i_bits_in_speex_frame
= 0;
604 block_t
*p_new_block
= NULL
;
606 i_pcm_output_size
= p_sys
->p_header
->frame_size
;
607 p_frame_holder
= (short*)xmalloc( sizeof(short)*i_pcm_output_size
);
609 speex_bits_read_from( &p_sys
->bits
, (char*)p_oggpacket
->packet
,
611 i_bits_before
= speex_bits_remaining( &p_sys
->bits
);
612 speex_decode_int(p_sys
->p_state
, &p_sys
->bits
, p_frame_holder
);
613 i_bits_after
= speex_bits_remaining( &p_sys
->bits
);
615 i_bits_in_speex_frame
= i_bits_before
- i_bits_after
;
616 i_bytes_in_speex_frame
= ( i_bits_in_speex_frame
+
617 (8 - (i_bits_in_speex_frame
% 8)) )
620 p_new_block
= block_Alloc( i_bytes_in_speex_frame
);
621 memset( p_new_block
->p_buffer
, 0xff, i_bytes_in_speex_frame
);
624 * Copy the first frame in this packet to a new packet.
626 speex_bits_rewind( &p_sys
->bits
);
627 speex_bits_write( &p_sys
->bits
,
628 (char*)p_new_block
->p_buffer
,
629 (int)i_bytes_in_speex_frame
);
632 * Move the remaining part of the original packet (subsequent
633 * frames, if there are any) into the beginning
634 * of the original packet so
635 * they are preserved following the realloc.
636 * Note: Any bits that
637 * remain in the initial packet
638 * are "filler" if they do not constitute
641 if ( i_bits_after
> 7 )
643 /* round-down since we rounded-up earlier (to include
644 * the speex terminator code.
646 i_bytes_in_speex_frame
--;
647 speex_bits_write( &p_sys
->bits
,
648 (char*)p_block
->p_buffer
,
649 p_block
->i_buffer
- i_bytes_in_speex_frame
);
650 p_block
= block_Realloc( p_block
,
652 p_block
->i_buffer
-i_bytes_in_speex_frame
);
657 speex_bits_reset( &p_sys
->bits
);
660 free( p_frame_holder
);
661 return SendPacket( p_dec
, p_new_block
);
665 return SendPacket( p_dec
, p_block
);
670 block_t
*p_aout_buffer
= DecodePacket( p_dec
, p_oggpacket
);
673 block_Release( p_block
);
674 return p_aout_buffer
;
678 static int DecodeRtpSpeexPacket( decoder_t
*p_dec
, block_t
*p_speex_bit_block
)
680 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
681 block_t
*p_aout_buffer
;
683 unsigned int i_speex_frame_size
;
685 if ( !p_speex_bit_block
|| p_speex_bit_block
->i_pts
<= VLC_TS_INVALID
)
686 return VLCDEC_SUCCESS
;
689 If the SpeexBits buffer size is 0 (a default value),
690 we know that a proper initialization has not yet been done.
692 if ( p_sys
->bits
.buf_size
==0 )
694 p_sys
->p_header
= malloc(sizeof(SpeexHeader
));
695 if ( !p_sys
->p_header
)
697 msg_Err( p_dec
, "Could not allocate a Speex header.");
698 return VLCDEC_SUCCESS
;
701 const SpeexMode
*mode
= speex_lib_get_mode((p_sys
->rtp_rate
/ 8000) >> 1);
703 speex_init_header( p_sys
->p_header
,p_sys
->rtp_rate
, 1, mode
);
704 speex_bits_init( &p_sys
->bits
);
705 p_sys
->p_state
= speex_decoder_init( mode
);
706 if ( !p_sys
->p_state
)
708 msg_Err( p_dec
, "Could not allocate a Speex decoder." );
709 free( p_sys
->p_header
);
710 return VLCDEC_SUCCESS
;
714 Assume that variable bit rate is enabled. Also assume
715 that there is only one frame per packet.
717 p_sys
->p_header
->vbr
= 1;
718 p_sys
->p_header
->frames_per_packet
= 1;
720 p_dec
->fmt_out
.audio
.i_channels
= p_sys
->p_header
->nb_channels
;
721 p_dec
->fmt_out
.audio
.i_physical_channels
=
722 p_dec
->fmt_out
.audio
.i_original_channels
=
723 pi_channels_maps
[p_sys
->p_header
->nb_channels
];
724 p_dec
->fmt_out
.audio
.i_rate
= p_sys
->p_header
->rate
;
726 if ( speex_mode_query( &speex_nb_mode
,
727 SPEEX_MODE_FRAME_SIZE
,
728 &i_speex_frame_size
) )
730 msg_Err( p_dec
, "Could not determine the frame size." );
731 speex_decoder_destroy( p_sys
->p_state
);
732 free( p_sys
->p_header
);
733 return VLCDEC_SUCCESS
;
735 p_dec
->fmt_out
.audio
.i_bytes_per_frame
= i_speex_frame_size
;
737 date_Init(&p_sys
->end_date
, p_sys
->p_header
->rate
, 1);
741 If the SpeexBits are initialized but there is
742 still no header, an error must be thrown.
744 if ( !p_sys
->p_header
)
746 msg_Err( p_dec
, "There is no valid Speex header found." );
747 return VLCDEC_SUCCESS
;
750 if ( !date_Get( &p_sys
->end_date
) )
751 date_Set( &p_sys
->end_date
, p_speex_bit_block
->i_dts
);
754 Ask for a new audio output buffer and make sure
757 if( decoder_UpdateAudioFormat( p_dec
) )
758 p_aout_buffer
= NULL
;
760 p_aout_buffer
= decoder_NewAudioBuffer( p_dec
,
761 p_sys
->p_header
->frame_size
);
762 if ( !p_aout_buffer
|| p_aout_buffer
->i_buffer
== 0 )
764 msg_Err(p_dec
, "Oops: No new buffer was returned!");
765 return VLCDEC_SUCCESS
;
769 Read the Speex payload into the SpeexBits buffer.
771 speex_bits_read_from( &p_sys
->bits
,
772 (char*)p_speex_bit_block
->p_buffer
,
773 p_speex_bit_block
->i_buffer
);
776 Decode the input and ensure that no errors
779 i_decode_ret
= speex_decode_int( p_sys
->p_state
, &p_sys
->bits
,
780 (int16_t*)p_aout_buffer
->p_buffer
);
781 if ( i_decode_ret
< 0 )
783 msg_Err( p_dec
, "Decoding failed. Perhaps we have a bad stream?" );
784 return VLCDEC_SUCCESS
;
788 Handle date management on the audio output buffer.
790 p_aout_buffer
->i_pts
= date_Get( &p_sys
->end_date
);
791 p_aout_buffer
->i_length
= date_Increment( &p_sys
->end_date
,
792 p_sys
->p_header
->frame_size
) - p_aout_buffer
->i_pts
;
795 p_sys
->i_frame_in_packet
++;
796 block_Release( p_speex_bit_block
);
797 decoder_QueueAudio( p_dec
, p_aout_buffer
);
798 return VLCDEC_SUCCESS
;
801 /*****************************************************************************
802 * DecodePacket: decodes a Speex packet.
803 *****************************************************************************/
804 static block_t
*DecodePacket( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
)
806 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
808 if( p_oggpacket
->bytes
)
810 /* Copy Ogg packet to Speex bitstream */
811 speex_bits_read_from( &p_sys
->bits
, (char *)p_oggpacket
->packet
,
812 p_oggpacket
->bytes
);
813 p_sys
->i_frame_in_packet
= 0;
816 /* Decode one frame at a time */
817 if( p_sys
->i_frame_in_packet
< p_sys
->p_header
->frames_per_packet
)
819 block_t
*p_aout_buffer
;
820 if( p_sys
->p_header
->frame_size
== 0 )
823 if( decoder_UpdateAudioFormat( p_dec
) )
826 decoder_NewAudioBuffer( p_dec
, p_sys
->p_header
->frame_size
);
832 switch( speex_decode_int( p_sys
->p_state
, &p_sys
->bits
,
833 (int16_t *)p_aout_buffer
->p_buffer
) )
836 msg_Err( p_dec
, "decoding error: corrupted stream?" );
837 case -1: /* End of stream */
841 if( speex_bits_remaining( &p_sys
->bits
) < 0 )
843 msg_Err( p_dec
, "decoding overflow: corrupted stream?" );
846 if( p_sys
->p_header
->nb_channels
== 2 )
847 speex_decode_stereo_int( (int16_t *)p_aout_buffer
->p_buffer
,
848 p_sys
->p_header
->frame_size
,
851 /* Date management */
852 p_aout_buffer
->i_pts
= date_Get( &p_sys
->end_date
);
853 p_aout_buffer
->i_length
=
854 date_Increment( &p_sys
->end_date
, p_sys
->p_header
->frame_size
)
855 - p_aout_buffer
->i_pts
;
857 p_sys
->i_frame_in_packet
++;
859 return p_aout_buffer
;
867 /*****************************************************************************
868 * SendPacket: send an ogg packet to the stream output.
869 *****************************************************************************/
870 static block_t
*SendPacket( decoder_t
*p_dec
, block_t
*p_block
)
872 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
874 /* Date management */
875 p_block
->i_dts
= p_block
->i_pts
= date_Get( &p_sys
->end_date
);
878 date_Increment( &p_sys
->end_date
,
879 p_sys
->p_header
->frame_size
) -
885 /*****************************************************************************
886 * ParseSpeexComments:
887 *****************************************************************************/
889 static void ParseSpeexComments( decoder_t
*p_dec
, ogg_packet
*p_oggpacket
)
891 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
892 const SpeexMode
*p_mode
;
894 assert( p_sys
->p_header
->mode
< SPEEX_NB_MODES
);
896 p_mode
= speex_mode_list
[p_sys
->p_header
->mode
];
897 assert( p_mode
!= NULL
);
899 if( !p_dec
->p_description
)
901 p_dec
->p_description
= vlc_meta_New();
902 if( !p_dec
->p_description
)
908 if( asprintf( &psz_mode
, "%s%s", p_mode
->modeName
, p_sys
->p_header
->vbr
? " VBR" : "" ) >= 0 )
910 vlc_meta_AddExtra( p_dec
->p_description
, _("Mode"), psz_mode
);
914 /* TODO: finish comments parsing */
915 VLC_UNUSED( p_oggpacket
);
918 /*****************************************************************************
919 * CloseDecoder: speex decoder destruction
920 *****************************************************************************/
921 static void CloseDecoder( vlc_object_t
*p_this
)
923 decoder_t
* p_dec
= (decoder_t
*)p_this
;
924 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
928 speex_decoder_destroy( p_sys
->p_state
);
929 speex_bits_destroy( &p_sys
->bits
);
932 free( p_sys
->p_header
);
937 /*****************************************************************************
938 * encoder_sys_t: encoder descriptor
939 *****************************************************************************/
940 #define MAX_FRAME_BYTES 2000
948 char p_buffer_out
[MAX_FRAME_BYTES
];
955 SpeexStereoState stereo
;
958 int i_frames_per_packet
;
959 int i_frames_in_packet
;
966 static block_t
*Encode ( encoder_t
*, block_t
* );
968 /*****************************************************************************
969 * OpenEncoder: probe the encoder and return score
970 *****************************************************************************/
971 static int OpenEncoder( vlc_object_t
*p_this
)
973 encoder_t
*p_enc
= (encoder_t
*)p_this
;
974 encoder_sys_t
*p_sys
;
975 const SpeexMode
*p_speex_mode
= &speex_nb_mode
;
977 const char *pp_header
[2];
981 if( p_enc
->fmt_out
.i_codec
!= VLC_CODEC_SPEEX
&&
987 config_ChainParse( p_enc
, ENC_CFG_PREFIX
, ppsz_enc_options
, p_enc
->p_cfg
);
988 switch( var_GetInteger( p_enc
, ENC_CFG_PREFIX
"mode" ) )
991 msg_Dbg( p_enc
, "Using wideband" );
992 p_speex_mode
= &speex_wb_mode
;
995 msg_Dbg( p_enc
, "Using ultra-wideband" );
996 p_speex_mode
= &speex_uwb_mode
;
999 msg_Dbg( p_enc
, "Using narrowband" );
1000 p_speex_mode
= &speex_nb_mode
;
1004 /* Allocate the memory needed to store the decoder's structure */
1005 if( ( p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
)) ) == NULL
)
1007 p_enc
->p_sys
= p_sys
;
1008 p_enc
->pf_encode_audio
= Encode
;
1009 p_enc
->fmt_in
.i_codec
= VLC_CODEC_S16N
;
1010 p_enc
->fmt_out
.i_codec
= VLC_CODEC_SPEEX
;
1012 speex_init_header( &p_sys
->header
, p_enc
->fmt_in
.audio
.i_rate
,
1015 p_sys
->header
.frames_per_packet
= 1;
1016 p_sys
->header
.vbr
= var_GetBool( p_enc
, ENC_CFG_PREFIX
"cbr" ) ? 0 : 1;
1017 p_sys
->header
.nb_channels
= p_enc
->fmt_in
.audio
.i_channels
;
1019 /* Create a new encoder state in narrowband mode */
1020 p_sys
->p_state
= speex_encoder_init( p_speex_mode
);
1023 i_tmp
= var_GetInteger( p_enc
, ENC_CFG_PREFIX
"complexity" );
1024 speex_encoder_ctl( p_sys
->p_state
, SPEEX_SET_COMPLEXITY
, &i_tmp
);
1026 i_tmp
= var_GetBool( p_enc
, ENC_CFG_PREFIX
"cbr" ) ? 0 : 1;
1027 speex_encoder_ctl( p_sys
->p_state
, SPEEX_SET_VBR
, &i_tmp
);
1029 if( i_tmp
== 0 ) /* CBR */
1031 i_tmp
= var_GetFloat( p_enc
, ENC_CFG_PREFIX
"quality" );
1032 speex_encoder_ctl( p_sys
->p_state
, SPEEX_SET_QUALITY
, &i_tmp
);
1034 i_tmp
= var_GetBool( p_enc
, ENC_CFG_PREFIX
"vad" ) ? 1 : 0;
1035 speex_encoder_ctl( p_sys
->p_state
, SPEEX_SET_VAD
, &i_tmp
);
1041 f_tmp
= var_GetFloat( p_enc
, ENC_CFG_PREFIX
"quality" );
1042 speex_encoder_ctl( p_sys
->p_state
, SPEEX_SET_VBR_QUALITY
, &f_tmp
);
1044 i_tmp
= var_GetInteger( p_enc
, ENC_CFG_PREFIX
"max-bitrate" );
1046 #ifdef SPEEX_SET_VBR_MAX_BITRATE
1047 speex_encoder_ctl( p_sys
->p_state
, SPEEX_SET_VBR_MAX_BITRATE
, &i_tmp
);
1049 msg_Dbg( p_enc
, "max-bitrate cannot be set in this version of libspeex");
1053 i_tmp
= var_GetBool( p_enc
, ENC_CFG_PREFIX
"dtx" ) ? 1 : 0;
1054 speex_encoder_ctl( p_sys
->p_state
, SPEEX_SET_DTX
, &i_tmp
);
1057 /*Initialization of the structure that holds the bits*/
1058 speex_bits_init( &p_sys
->bits
);
1060 p_sys
->i_frames_in_packet
= 0;
1061 p_sys
->i_samples_delay
= 0;
1063 speex_encoder_ctl( p_sys
->p_state
, SPEEX_GET_FRAME_SIZE
,
1064 &p_sys
->i_frame_length
);
1066 p_sys
->i_frame_size
= p_sys
->i_frame_length
*
1067 sizeof(int16_t) * p_enc
->fmt_in
.audio
.i_channels
;
1068 p_sys
->p_buffer
= xmalloc( p_sys
->i_frame_size
);
1070 /* Create and store headers */
1071 pp_header
[0] = speex_header_to_packet( &p_sys
->header
, &pi_header
[0] );
1072 pp_header
[1] = "ENCODER=VLC media player";
1073 pi_header
[1] = sizeof("ENCODER=VLC media player");
1075 p_enc
->fmt_out
.i_extra
= 3 * 2 + pi_header
[0] + pi_header
[1];
1076 p_extra
= p_enc
->fmt_out
.p_extra
= xmalloc( p_enc
->fmt_out
.i_extra
);
1077 for( i
= 0; i
< 2; i
++ )
1079 *(p_extra
++) = pi_header
[i
] >> 8;
1080 *(p_extra
++) = pi_header
[i
] & 0xFF;
1081 memcpy( p_extra
, pp_header
[i
], pi_header
[i
] );
1082 p_extra
+= pi_header
[i
];
1085 msg_Dbg( p_enc
, "encoding: frame size:%d, channels:%d, samplerate:%d",
1086 p_sys
->i_frame_size
, p_enc
->fmt_in
.audio
.i_channels
,
1087 p_enc
->fmt_in
.audio
.i_rate
);
1092 /****************************************************************************
1093 * Encode: the whole thing
1094 ****************************************************************************
1095 * This function spits out ogg packets.
1096 ****************************************************************************/
1097 static block_t
*Encode( encoder_t
*p_enc
, block_t
*p_aout_buf
)
1099 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
1100 block_t
*p_block
, *p_chain
= NULL
;
1102 /* Encoder gets NULL when it's time to flush */
1103 if( unlikely( !p_aout_buf
) ) return NULL
;
1105 unsigned char *p_buffer
= p_aout_buf
->p_buffer
;
1106 unsigned i_samples
= p_aout_buf
->i_nb_samples
;
1107 int i_samples_delay
= p_sys
->i_samples_delay
;
1109 mtime_t i_pts
= p_aout_buf
->i_pts
-
1110 (mtime_t
)1000000 * (mtime_t
)p_sys
->i_samples_delay
/
1111 (mtime_t
)p_enc
->fmt_in
.audio
.i_rate
;
1113 p_sys
->i_samples_delay
+= i_samples
;
1115 while( p_sys
->i_samples_delay
>= p_sys
->i_frame_length
)
1120 if( i_samples_delay
)
1122 /* Take care of the left-over from last time */
1123 int i_delay_size
= i_samples_delay
* 2 *
1124 p_enc
->fmt_in
.audio
.i_channels
;
1125 int i_size
= p_sys
->i_frame_size
- i_delay_size
;
1127 p_samples
= (int16_t *)p_sys
->p_buffer
;
1128 memcpy( p_sys
->p_buffer
+ i_delay_size
, p_buffer
, i_size
);
1129 p_buffer
-= i_delay_size
;
1130 i_samples
+= i_samples_delay
;
1131 i_samples_delay
= 0;
1135 p_samples
= (int16_t *)p_buffer
;
1138 /* Encode current frame */
1139 if( p_enc
->fmt_in
.audio
.i_channels
== 2 )
1140 speex_encode_stereo_int( p_samples
, p_sys
->i_frame_length
,
1144 if( p_sys
->preprocess
)
1145 speex_preprocess( p_sys
->preprocess
, p_samples
, NULL
);
1148 speex_encode_int( p_sys
->p_state
, p_samples
, &p_sys
->bits
);
1150 p_buffer
+= p_sys
->i_frame_size
;
1151 p_sys
->i_samples_delay
-= p_sys
->i_frame_length
;
1152 i_samples
-= p_sys
->i_frame_length
;
1154 p_sys
->i_frames_in_packet
++;
1156 if( p_sys
->i_frames_in_packet
< p_sys
->header
.frames_per_packet
)
1159 p_sys
->i_frames_in_packet
= 0;
1161 speex_bits_insert_terminator( &p_sys
->bits
);
1162 i_out
= speex_bits_write( &p_sys
->bits
, p_sys
->p_buffer_out
,
1164 speex_bits_reset( &p_sys
->bits
);
1166 p_block
= block_Alloc( i_out
);
1167 memcpy( p_block
->p_buffer
, p_sys
->p_buffer_out
, i_out
);
1169 p_block
->i_length
= (mtime_t
)1000000 *
1170 (mtime_t
)p_sys
->i_frame_length
* p_sys
->header
.frames_per_packet
/
1171 (mtime_t
)p_enc
->fmt_in
.audio
.i_rate
;
1173 p_block
->i_dts
= p_block
->i_pts
= i_pts
;
1176 i_pts
+= p_block
->i_length
;
1177 block_ChainAppend( &p_chain
, p_block
);
1181 /* Backup the remaining raw samples */
1184 memcpy( p_sys
->p_buffer
+ i_samples_delay
* 2 *
1185 p_enc
->fmt_in
.audio
.i_channels
, p_buffer
,
1186 i_samples
* 2 * p_enc
->fmt_in
.audio
.i_channels
);
1192 /*****************************************************************************
1193 * CloseEncoder: encoder destruction
1194 *****************************************************************************/
1195 static void CloseEncoder( vlc_object_t
*p_this
)
1197 encoder_t
*p_enc
= (encoder_t
*)p_this
;
1198 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
1200 speex_encoder_destroy( p_sys
->p_state
);
1201 speex_bits_destroy( &p_sys
->bits
);
1203 free( p_sys
->p_buffer
);