demux: heif: send extradata with avif
[vlc.git] / modules / codec / speex.c
blobe7310e777fbb05074670e070349bb29c8695eb3e
1 /*****************************************************************************
2 * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
3 *****************************************************************************
4 * Copyright (C) 2003-2009 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
25 * Preamble
26 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_input.h>
34 #include <vlc_codec.h>
35 #include "../demux/xiph.h"
37 #include <ogg/ogg.h>
38 #include <speex/speex.h>
39 #include <speex/speex_header.h>
40 #include <speex/speex_stereo.h>
41 #include <speex/speex_callbacks.h>
43 #include <assert.h>
45 /*****************************************************************************
46 * Module descriptor
47 *****************************************************************************/
48 static int OpenDecoder ( vlc_object_t * );
49 static int OpenPacketizer( vlc_object_t * );
50 static void CloseDecoder ( vlc_object_t * );
52 #ifdef ENABLE_SOUT
53 static int OpenEncoder ( vlc_object_t * );
54 static void CloseEncoder ( vlc_object_t * );
55 #endif
57 #define ENC_CFG_PREFIX "sout-speex-"
59 #define ENC_MODE_TEXT N_("Mode" )
60 #define ENC_MODE_LONGTEXT N_( \
61 "Enforce the mode of the encoder." )
63 #define ENC_QUALITY_TEXT N_("Encoding quality")
64 #define ENC_QUALITY_LONGTEXT N_( \
65 "Enforce a quality between 0 (low) and 10 (high)." )
67 #define ENC_COMPLEXITY_TEXT N_("Encoding complexity" )
68 #define ENC_COMPLEXITY_LONGTEXT N_( \
69 "Enforce the complexity of the encoder." )
71 #define ENC_MAXBITRATE_TEXT N_( "Maximal bitrate" )
72 #define ENC_MAXBITRATE_LONGTEXT N_( \
73 "Enforce the maximal VBR bitrate" )
75 #define ENC_CBR_TEXT N_( "CBR encoding" )
76 #define ENC_CBR_LONGTEXT N_( \
77 "Enforce a constant bitrate encoding (CBR) instead of default " \
78 "variable bitrate encoding (VBR)." )
80 #define ENC_VAD_TEXT N_( "Voice activity detection" )
81 #define ENC_VAD_LONGTEXT N_( \
82 "Enable voice activity detection (VAD). It is automatically " \
83 "activated in VBR mode." )
85 #define ENC_DTX_TEXT N_( "Discontinuous Transmission" )
86 #define ENC_DTX_LONGTEXT N_( \
87 "Enable discontinuous transmission (DTX)." )
89 static const int pi_enc_mode_values[] = { 0, 1, 2 };
90 static const char * const ppsz_enc_mode_descriptions[] = {
91 N_("Narrow-band (8kHz)"), N_("Wide-band (16kHz)"), N_("Ultra-wideband (32kHz)"), NULL
94 vlc_module_begin ()
95 set_category( CAT_INPUT )
96 set_subcategory( SUBCAT_INPUT_ACODEC )
98 set_description( N_("Speex audio decoder") )
99 set_capability( "audio decoder", 100 )
100 set_shortname( N_("Speex") )
101 set_callbacks( OpenDecoder, CloseDecoder )
103 add_submodule ()
104 set_description( N_("Speex audio packetizer") )
105 set_capability( "packetizer", 100 )
106 set_callbacks( OpenPacketizer, CloseDecoder )
108 #ifdef ENABLE_SOUT
109 add_submodule ()
110 set_description( N_("Speex audio encoder") )
111 set_capability( "encoder", 100 )
112 set_callbacks( OpenEncoder, CloseEncoder )
114 add_integer( ENC_CFG_PREFIX "mode", 0, ENC_MODE_TEXT,
115 ENC_MODE_LONGTEXT, false )
116 change_integer_list( pi_enc_mode_values, ppsz_enc_mode_descriptions )
118 add_integer( ENC_CFG_PREFIX "complexity", 3, ENC_COMPLEXITY_TEXT,
119 ENC_COMPLEXITY_LONGTEXT, false )
120 change_integer_range( 1, 10 )
122 add_bool( ENC_CFG_PREFIX "cbr", false, ENC_CBR_TEXT,
123 ENC_CBR_LONGTEXT, false )
125 add_float( ENC_CFG_PREFIX "quality", 8.0, ENC_QUALITY_TEXT,
126 ENC_QUALITY_LONGTEXT, false )
127 change_float_range( 0.0, 10.0 )
129 add_integer( ENC_CFG_PREFIX "max-bitrate", 0, ENC_MAXBITRATE_TEXT,
130 ENC_MAXBITRATE_LONGTEXT, false )
132 add_bool( ENC_CFG_PREFIX "vad", true, ENC_VAD_TEXT,
133 ENC_VAD_LONGTEXT, false )
135 add_bool( ENC_CFG_PREFIX "dtx", false, ENC_DTX_TEXT,
136 ENC_DTX_LONGTEXT, false )
138 /* TODO agc, noise suppression, */
139 #endif
141 vlc_module_end ()
143 static const char *const ppsz_enc_options[] = {
144 "mode", "complexity", "cbr", "quality", "max-bitrate", "vad", "dtx", NULL
147 /*****************************************************************************
148 * decoder_sys_t : speex decoder descriptor
149 *****************************************************************************/
150 typedef struct
152 /* Module mode */
153 bool b_packetizer;
156 * Input properties
158 bool b_has_headers;
159 int i_frame_in_packet;
162 * Speex properties
164 SpeexBits bits;
165 SpeexHeader *p_header;
166 SpeexStereoState stereo;
167 void *p_state;
168 unsigned int rtp_rate;
171 * Common properties
173 date_t end_date;
175 } decoder_sys_t;
177 /****************************************************************************
178 * Local prototypes
179 ****************************************************************************/
181 static block_t *Packetize ( decoder_t *, block_t ** );
182 static int DecodeAudio ( decoder_t *, block_t * );
183 static int DecodeRtpSpeexPacket( decoder_t *, block_t *);
184 static int ProcessHeaders( decoder_t * );
185 static int ProcessInitialHeader ( decoder_t *, ogg_packet * );
186 static block_t *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
187 static void Flush( decoder_t * );
189 static block_t *DecodePacket( decoder_t *, ogg_packet * );
190 static block_t *SendPacket( decoder_t *, block_t * );
192 static void ParseSpeexComments( decoder_t *, ogg_packet * );
194 static int OpenCommon( vlc_object_t *p_this, bool b_packetizer )
196 decoder_t *p_dec = (decoder_t*)p_this;
197 decoder_sys_t *p_sys;
199 if( p_dec->fmt_in.i_codec != VLC_CODEC_SPEEX )
200 return VLC_EGENERIC;
202 /* Allocate the memory needed to store the decoder's structure */
203 if( ( p_dec->p_sys = p_sys = malloc(sizeof(decoder_sys_t)) ) == NULL )
204 return VLC_ENOMEM;
205 p_sys->bits.buf_size = 0;
206 p_sys->b_packetizer = b_packetizer;
207 p_sys->rtp_rate = p_dec->fmt_in.audio.i_rate;
208 p_sys->b_has_headers = false;
210 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
212 if( b_packetizer )
214 p_dec->fmt_out.i_codec = VLC_CODEC_SPEEX;
215 p_dec->pf_packetize = Packetize;
217 else
219 /* Set output properties */
220 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
223 Set callbacks
224 If the codec is spxr then this decoder is
225 being invoked on a Speex stream arriving via RTP.
226 A special decoder callback is used.
228 if (p_dec->fmt_in.i_original_fourcc == VLC_FOURCC('s', 'p', 'x', 'r'))
230 msg_Dbg( p_dec, "Using RTP version of Speex decoder @ rate %d.",
231 p_dec->fmt_in.audio.i_rate );
232 p_dec->pf_decode = DecodeRtpSpeexPacket;
234 else
236 p_dec->pf_decode = DecodeAudio;
239 p_dec->pf_flush = Flush;
241 p_sys->p_state = NULL;
242 p_sys->p_header = NULL;
243 p_sys->i_frame_in_packet = 0;
245 return VLC_SUCCESS;
248 /*****************************************************************************
249 * OpenDecoder: probe the decoder and return score
250 *****************************************************************************/
251 static int OpenDecoder( vlc_object_t *p_this )
253 return OpenCommon( p_this, false );
256 static int OpenPacketizer( vlc_object_t *p_this )
258 return OpenCommon( p_this, true );
261 static int CreateDefaultHeader( decoder_t *p_dec )
263 ogg_packet oggpacket;
264 SpeexHeader *p_header = malloc( sizeof(SpeexHeader) );
265 if( !p_header )
266 return VLC_ENOMEM;
268 const int rate = p_dec->fmt_in.audio.i_rate;
269 const unsigned i_mode = (rate / 8000) >> 1;
271 const SpeexMode *mode;
272 int ret = VLC_SUCCESS;
273 oggpacket.packet = NULL;
275 switch( rate )
277 case 8000:
278 case 16000:
279 case 32000:
280 mode = speex_lib_get_mode( i_mode );
281 break;
282 default:
283 msg_Err( p_dec, "Unexpected rate %d", rate );
284 ret = VLC_EGENERIC;
285 goto cleanup;
288 speex_init_header( p_header, rate, p_dec->fmt_in.audio.i_channels, mode );
289 p_header->frames_per_packet = 160 << i_mode;
291 oggpacket.packet = (unsigned char *) speex_header_to_packet( p_header,
292 (int *) &oggpacket.bytes );
293 if( !oggpacket.packet )
295 ret = VLC_ENOMEM;
296 goto cleanup;
299 oggpacket.b_o_s = 1;
300 oggpacket.e_o_s = 0;
301 oggpacket.granulepos = -1;
302 oggpacket.packetno = 0;
304 ret = ProcessInitialHeader( p_dec, &oggpacket );
306 if( ret != VLC_SUCCESS )
308 msg_Err( p_dec, "default Speex header is corrupted" );
311 cleanup:
312 free( oggpacket.packet );
313 free( p_header );
315 return ret;
319 /****************************************************************************
320 * DecodeBlock: the whole thing
321 ****************************************************************************
322 * This function must be fed with ogg packets.
323 ****************************************************************************/
324 static block_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
326 decoder_sys_t *p_sys = p_dec->p_sys;
327 ogg_packet oggpacket;
329 block_t *block = *pp_block;
331 if( block != NULL )
333 if( block->i_flags & (BLOCK_FLAG_CORRUPTED|BLOCK_FLAG_DISCONTINUITY) )
335 Flush( p_dec );
336 if( block->i_flags & BLOCK_FLAG_CORRUPTED )
338 block_Release( block );
339 *pp_block = NULL;
340 return NULL;
343 /* Block to Ogg packet */
344 oggpacket.packet = block->p_buffer;
345 oggpacket.bytes = block->i_buffer;
347 else
349 if( p_sys->b_packetizer ) return NULL;
351 /* Block to Ogg packet */
352 oggpacket.packet = NULL;
353 oggpacket.bytes = 0;
356 oggpacket.granulepos = -1;
357 oggpacket.b_o_s = 0;
358 oggpacket.e_o_s = 0;
359 oggpacket.packetno = 0;
361 /* Check for headers */
362 if( !p_sys->b_has_headers )
364 if( !p_dec->fmt_in.p_extra )
366 msg_Warn( p_dec, "Header missing, using default settings" );
368 if( CreateDefaultHeader( p_dec ) )
370 if( block != NULL )
371 block_Release( block );
372 return NULL;
375 else if( ProcessHeaders( p_dec ) )
377 if( block != NULL )
378 block_Release( block );
379 return NULL;
381 p_sys->b_has_headers = true;
384 return ProcessPacket( p_dec, &oggpacket, pp_block );
387 static int DecodeAudio( decoder_t *p_dec, block_t *p_block )
389 if( p_block == NULL ) /* No Drain */
390 return VLCDEC_SUCCESS;
392 block_t **pp_block = &p_block, *p_out;
393 while( ( p_out = DecodeBlock( p_dec, pp_block ) ) != NULL )
394 decoder_QueueAudio( p_dec, p_out );
395 return VLCDEC_SUCCESS;
398 static block_t *Packetize( decoder_t *p_dec, block_t **pp_block )
400 if( pp_block == NULL ) /* No Drain */
401 return NULL;
402 return DecodeBlock( p_dec, pp_block );
405 /*****************************************************************************
406 * ProcessHeaders: process Speex headers.
407 *****************************************************************************/
408 static int ProcessHeaders( decoder_t *p_dec )
410 decoder_sys_t *p_sys = p_dec->p_sys;
411 ogg_packet oggpacket;
413 unsigned pi_size[XIPH_MAX_HEADER_COUNT];
414 void *pp_data[XIPH_MAX_HEADER_COUNT];
415 unsigned i_count;
416 if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
417 p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
418 return VLC_EGENERIC;
419 if( i_count < 2 )
420 return VLC_EGENERIC;;
422 oggpacket.granulepos = -1;
423 oggpacket.e_o_s = 0;
424 oggpacket.packetno = 0;
426 /* Take care of the initial Vorbis header */
427 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
428 oggpacket.bytes = pi_size[0];
429 oggpacket.packet = pp_data[0];
430 if( ProcessInitialHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
432 msg_Err( p_dec, "initial Speex header is corrupted" );
433 return VLC_EGENERIC;;
436 /* The next packet in order is the comments header */
437 oggpacket.b_o_s = 0;
438 oggpacket.bytes = pi_size[1];
439 oggpacket.packet = pp_data[1];
440 ParseSpeexComments( p_dec, &oggpacket );
442 if( p_sys->b_packetizer )
444 void* p_extra = realloc( p_dec->fmt_out.p_extra,
445 p_dec->fmt_in.i_extra );
446 if( unlikely( p_extra == NULL ) )
448 return VLC_ENOMEM;
450 p_dec->fmt_out.p_extra = p_extra;
451 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
452 memcpy( p_dec->fmt_out.p_extra,
453 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
456 return VLC_SUCCESS;
459 /*****************************************************************************
460 * ProcessInitialHeader: processes the inital Speex header packet.
461 *****************************************************************************/
462 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
464 decoder_sys_t *p_sys = p_dec->p_sys;
466 void *p_state;
467 SpeexHeader *p_header;
468 const SpeexMode *p_mode;
469 SpeexCallback callback;
471 p_sys->p_header = p_header =
472 speex_packet_to_header( (char *)p_oggpacket->packet,
473 p_oggpacket->bytes );
474 if( !p_header )
476 msg_Err( p_dec, "cannot read Speex header" );
477 return VLC_EGENERIC;
479 if( p_header->mode >= SPEEX_NB_MODES || p_header->mode < 0 )
481 msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
482 "this version of libspeex.", p_header->mode );
483 return VLC_EGENERIC;
486 p_mode = speex_mode_list[p_header->mode];
487 if( p_mode == NULL )
488 return VLC_EGENERIC;
490 if( p_header->speex_version_id > 1 )
492 msg_Err( p_dec, "this file was encoded with Speex bit-stream "
493 "version %d which is not supported by this decoder.",
494 p_header->speex_version_id );
495 return VLC_EGENERIC;
498 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
500 msg_Err( p_dec, "file encoded with a newer version of Speex." );
501 return VLC_EGENERIC;
503 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
505 msg_Err( p_dec, "file encoded with an older version of Speex." );
506 return VLC_EGENERIC;
509 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
510 p_header->rate, p_mode->modeName,
511 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
512 p_header->vbr ? ", VBR)" : ")" );
514 /* Take care of speex decoder init */
515 speex_bits_init( &p_sys->bits );
516 p_sys->p_state = p_state = speex_decoder_init( p_mode );
517 if( !p_state )
519 msg_Err( p_dec, "decoder initialization failed" );
520 return VLC_EGENERIC;
523 if( p_header->nb_channels == 2 )
525 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
526 p_sys->stereo = stereo;
527 callback.callback_id = SPEEX_INBAND_STEREO;
528 callback.func = speex_std_stereo_request_handler;
529 callback.data = &p_sys->stereo;
530 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
532 if( p_header->nb_channels <= 0 ||
533 p_header->nb_channels > 5 )
535 msg_Err( p_dec, "invalid number of channels (not between 1 and 5): %i",
536 p_header->nb_channels );
537 return VLC_EGENERIC;
540 /* Setup the format */
541 p_dec->fmt_out.audio.i_physical_channels =
542 vlc_chan_maps[p_header->nb_channels];
543 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
544 p_dec->fmt_out.audio.i_rate = p_header->rate;
546 date_Init( &p_sys->end_date, p_header->rate, 1 );
548 return VLC_SUCCESS;
551 /*****************************************************************************
552 * Flush:
553 *****************************************************************************/
554 static void Flush( decoder_t *p_dec )
556 decoder_sys_t *p_sys = p_dec->p_sys;
558 date_Set( &p_sys->end_date, VLC_TICK_INVALID );
561 /*****************************************************************************
562 * ProcessPacket: processes a Speex packet.
563 *****************************************************************************/
564 static block_t *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
565 block_t **pp_block )
567 decoder_sys_t *p_sys = p_dec->p_sys;
568 block_t *p_block = *pp_block;
570 /* Date management */
571 if( p_block && p_block->i_pts != VLC_TICK_INVALID &&
572 p_block->i_pts != date_Get( &p_sys->end_date ) )
574 date_Set( &p_sys->end_date, p_block->i_pts );
577 if( date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
579 /* We've just started the stream, wait for the first PTS. */
580 if( p_block ) block_Release( p_block );
581 return NULL;
584 *pp_block = NULL; /* To avoid being fed the same packet again */
586 if( p_sys->b_packetizer )
588 if ( p_sys->p_header->frames_per_packet > 1 )
590 short *p_frame_holder = NULL;
591 int i_bits_before = 0, i_bits_after = 0, i_bytes_in_speex_frame = 0,
592 i_pcm_output_size = 0, i_bits_in_speex_frame = 0;
593 block_t *p_new_block = NULL;
595 i_pcm_output_size = p_sys->p_header->frame_size;
596 p_frame_holder = (short*)xmalloc( sizeof(short)*i_pcm_output_size );
598 speex_bits_read_from( &p_sys->bits, (char*)p_oggpacket->packet,
599 p_oggpacket->bytes);
600 i_bits_before = speex_bits_remaining( &p_sys->bits );
601 speex_decode_int(p_sys->p_state, &p_sys->bits, p_frame_holder);
602 i_bits_after = speex_bits_remaining( &p_sys->bits );
604 i_bits_in_speex_frame = i_bits_before - i_bits_after;
605 i_bytes_in_speex_frame = ( i_bits_in_speex_frame +
606 (8 - (i_bits_in_speex_frame % 8)) )
607 / 8;
609 p_new_block = block_Alloc( i_bytes_in_speex_frame );
610 memset( p_new_block->p_buffer, 0xff, i_bytes_in_speex_frame );
613 * Copy the first frame in this packet to a new packet.
615 speex_bits_rewind( &p_sys->bits );
616 speex_bits_write( &p_sys->bits,
617 (char*)p_new_block->p_buffer,
618 (int)i_bytes_in_speex_frame );
621 * Move the remaining part of the original packet (subsequent
622 * frames, if there are any) into the beginning
623 * of the original packet so
624 * they are preserved following the realloc.
625 * Note: Any bits that
626 * remain in the initial packet
627 * are "filler" if they do not constitute
628 * an entire byte.
630 if ( i_bits_after > 7 )
632 /* round-down since we rounded-up earlier (to include
633 * the speex terminator code.
635 i_bytes_in_speex_frame--;
636 speex_bits_write( &p_sys->bits,
637 (char*)p_block->p_buffer,
638 p_block->i_buffer - i_bytes_in_speex_frame );
639 p_block = block_Realloc( p_block,
641 p_block->i_buffer-i_bytes_in_speex_frame );
642 *pp_block = p_block;
644 else
646 speex_bits_reset( &p_sys->bits );
649 free( p_frame_holder );
650 return SendPacket( p_dec, p_new_block);
652 else
654 return SendPacket( p_dec, p_block );
657 else
659 block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
661 if( p_block )
662 block_Release( p_block );
663 return p_aout_buffer;
667 static int DecodeRtpSpeexPacket( decoder_t *p_dec, block_t *p_speex_bit_block )
669 decoder_sys_t *p_sys = p_dec->p_sys;
670 block_t *p_aout_buffer;
671 int i_decode_ret;
672 unsigned int i_speex_frame_size;
674 if ( !p_speex_bit_block || p_speex_bit_block->i_pts == VLC_TICK_INVALID )
675 return VLCDEC_SUCCESS;
678 If the SpeexBits buffer size is 0 (a default value),
679 we know that a proper initialization has not yet been done.
681 if ( p_sys->bits.buf_size==0 )
683 p_sys->p_header = malloc(sizeof(SpeexHeader));
684 if ( !p_sys->p_header )
686 msg_Err( p_dec, "Could not allocate a Speex header.");
687 return VLCDEC_SUCCESS;
690 const SpeexMode *mode = speex_lib_get_mode((p_sys->rtp_rate / 8000) >> 1);
692 speex_init_header( p_sys->p_header,p_sys->rtp_rate, 1, mode );
693 speex_bits_init( &p_sys->bits );
694 p_sys->p_state = speex_decoder_init( mode );
695 if ( !p_sys->p_state )
697 msg_Err( p_dec, "Could not allocate a Speex decoder." );
698 free( p_sys->p_header );
699 return VLCDEC_SUCCESS;
703 Assume that variable bit rate is enabled. Also assume
704 that there is only one frame per packet.
706 p_sys->p_header->vbr = 1;
707 p_sys->p_header->frames_per_packet = 1;
709 p_dec->fmt_out.audio.i_channels = p_sys->p_header->nb_channels;
710 p_dec->fmt_out.audio.i_physical_channels =
711 vlc_chan_maps[p_sys->p_header->nb_channels];
712 p_dec->fmt_out.audio.i_rate = p_sys->p_header->rate;
714 if ( speex_mode_query( &speex_nb_mode,
715 SPEEX_MODE_FRAME_SIZE,
716 &i_speex_frame_size ) )
718 msg_Err( p_dec, "Could not determine the frame size." );
719 speex_decoder_destroy( p_sys->p_state );
720 free( p_sys->p_header );
721 return VLCDEC_SUCCESS;
723 p_dec->fmt_out.audio.i_bytes_per_frame = i_speex_frame_size;
725 date_Init(&p_sys->end_date, p_sys->p_header->rate, 1);
729 If the SpeexBits are initialized but there is
730 still no header, an error must be thrown.
732 if ( !p_sys->p_header )
734 msg_Err( p_dec, "There is no valid Speex header found." );
735 return VLCDEC_SUCCESS;
738 if ( date_Get( &p_sys->end_date ) == VLC_TICK_INVALID )
739 date_Set( &p_sys->end_date, p_speex_bit_block->i_dts );
742 Ask for a new audio output buffer and make sure
743 we get one.
745 if( decoder_UpdateAudioFormat( p_dec ) )
746 p_aout_buffer = NULL;
747 else
748 p_aout_buffer = decoder_NewAudioBuffer( p_dec,
749 p_sys->p_header->frame_size );
750 if ( !p_aout_buffer || p_aout_buffer->i_buffer == 0 )
752 msg_Err(p_dec, "Oops: No new buffer was returned!");
753 return VLCDEC_SUCCESS;
757 Read the Speex payload into the SpeexBits buffer.
759 speex_bits_read_from( &p_sys->bits,
760 (char*)p_speex_bit_block->p_buffer,
761 p_speex_bit_block->i_buffer );
764 Decode the input and ensure that no errors
765 were encountered.
767 i_decode_ret = speex_decode_int( p_sys->p_state, &p_sys->bits,
768 (int16_t*)p_aout_buffer->p_buffer );
769 if ( i_decode_ret < 0 )
771 msg_Err( p_dec, "Decoding failed. Perhaps we have a bad stream?" );
772 return VLCDEC_SUCCESS;
776 Handle date management on the audio output buffer.
778 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
779 p_aout_buffer->i_length = date_Increment( &p_sys->end_date,
780 p_sys->p_header->frame_size ) - p_aout_buffer->i_pts;
783 p_sys->i_frame_in_packet++;
784 block_Release( p_speex_bit_block );
785 decoder_QueueAudio( p_dec, p_aout_buffer );
786 return VLCDEC_SUCCESS;
789 /*****************************************************************************
790 * DecodePacket: decodes a Speex packet.
791 *****************************************************************************/
792 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
794 decoder_sys_t *p_sys = p_dec->p_sys;
796 if( p_oggpacket->bytes )
798 /* Copy Ogg packet to Speex bitstream */
799 speex_bits_read_from( &p_sys->bits, (char *)p_oggpacket->packet,
800 p_oggpacket->bytes );
801 p_sys->i_frame_in_packet = 0;
804 /* Decode one frame at a time */
805 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
807 block_t *p_aout_buffer;
808 if( p_sys->p_header->frame_size == 0 )
809 return NULL;
811 if( decoder_UpdateAudioFormat( p_dec ) )
812 return NULL;
813 p_aout_buffer =
814 decoder_NewAudioBuffer( p_dec, p_sys->p_header->frame_size );
815 if( !p_aout_buffer )
817 return NULL;
820 switch( speex_decode_int( p_sys->p_state, &p_sys->bits,
821 (int16_t *)p_aout_buffer->p_buffer ) )
823 case -2:
824 msg_Err( p_dec, "decoding error: corrupted stream?" );
825 case -1: /* End of stream */
826 return NULL;
829 if( speex_bits_remaining( &p_sys->bits ) < 0 )
831 msg_Err( p_dec, "decoding overflow: corrupted stream?" );
834 if( p_sys->p_header->nb_channels == 2 )
835 speex_decode_stereo_int( (int16_t *)p_aout_buffer->p_buffer,
836 p_sys->p_header->frame_size,
837 &p_sys->stereo );
839 /* Date management */
840 p_aout_buffer->i_pts = date_Get( &p_sys->end_date );
841 p_aout_buffer->i_length =
842 date_Increment( &p_sys->end_date, p_sys->p_header->frame_size )
843 - p_aout_buffer->i_pts;
845 p_sys->i_frame_in_packet++;
847 return p_aout_buffer;
849 else
851 return NULL;
855 /*****************************************************************************
856 * SendPacket: send an ogg packet to the stream output.
857 *****************************************************************************/
858 static block_t *SendPacket( decoder_t *p_dec, block_t *p_block )
860 decoder_sys_t *p_sys = p_dec->p_sys;
862 /* Date management */
863 p_block->i_dts = p_block->i_pts = date_Get( &p_sys->end_date );
865 p_block->i_length =
866 date_Increment( &p_sys->end_date,
867 p_sys->p_header->frame_size ) -
868 p_block->i_pts;
870 return p_block;
873 /*****************************************************************************
874 * ParseSpeexComments:
875 *****************************************************************************/
877 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
879 decoder_sys_t *p_sys = p_dec->p_sys;
880 const SpeexMode *p_mode;
882 assert( p_sys->p_header->mode < SPEEX_NB_MODES );
884 p_mode = speex_mode_list[p_sys->p_header->mode];
885 assert( p_mode != NULL );
887 if( !p_dec->p_description )
889 p_dec->p_description = vlc_meta_New();
890 if( !p_dec->p_description )
891 return;
894 /* */
895 char *psz_mode;
896 if( asprintf( &psz_mode, "%s%s", p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" ) >= 0 )
898 vlc_meta_AddExtra( p_dec->p_description, _("Mode"), psz_mode );
899 free( psz_mode );
902 /* TODO: finish comments parsing */
903 VLC_UNUSED( p_oggpacket );
906 /*****************************************************************************
907 * CloseDecoder: speex decoder destruction
908 *****************************************************************************/
909 static void CloseDecoder( vlc_object_t *p_this )
911 decoder_t * p_dec = (decoder_t *)p_this;
912 decoder_sys_t *p_sys = p_dec->p_sys;
914 if( p_sys->p_state )
916 speex_decoder_destroy( p_sys->p_state );
917 speex_bits_destroy( &p_sys->bits );
920 free( p_sys->p_header );
921 free( p_sys );
924 #ifdef ENABLE_SOUT
925 /*****************************************************************************
926 * encoder_sys_t: encoder descriptor
927 *****************************************************************************/
928 #define MAX_FRAME_BYTES 2000
930 typedef struct
933 * Input properties
935 char *p_buffer;
936 char p_buffer_out[MAX_FRAME_BYTES];
939 * Speex properties
941 SpeexBits bits;
942 SpeexHeader header;
943 SpeexStereoState stereo;
944 void *p_state;
946 int i_frames_per_packet;
947 int i_frames_in_packet;
949 int i_frame_length;
950 int i_samples_delay;
951 int i_frame_size;
952 } encoder_sys_t;
954 static block_t *Encode ( encoder_t *, block_t * );
956 /*****************************************************************************
957 * OpenEncoder: probe the encoder and return score
958 *****************************************************************************/
959 static int OpenEncoder( vlc_object_t *p_this )
961 encoder_t *p_enc = (encoder_t *)p_this;
962 encoder_sys_t *p_sys;
963 const SpeexMode *p_speex_mode = &speex_nb_mode;
964 int i_tmp, i;
965 const char *pp_header[2];
966 int pi_header[2];
967 uint8_t *p_extra;
969 if( p_enc->fmt_out.i_codec != VLC_CODEC_SPEEX &&
970 !p_enc->obj.force )
972 return VLC_EGENERIC;
975 config_ChainParse( p_enc, ENC_CFG_PREFIX, ppsz_enc_options, p_enc->p_cfg );
976 switch( var_GetInteger( p_enc, ENC_CFG_PREFIX "mode" ) )
978 case 1:
979 msg_Dbg( p_enc, "Using wideband" );
980 p_speex_mode = &speex_wb_mode;
981 break;
982 case 2:
983 msg_Dbg( p_enc, "Using ultra-wideband" );
984 p_speex_mode = &speex_uwb_mode;
985 break;
986 default:
987 msg_Dbg( p_enc, "Using narrowband" );
988 p_speex_mode = &speex_nb_mode;
989 break;
992 /* Allocate the memory needed to store the decoder's structure */
993 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
994 return VLC_ENOMEM;
995 p_enc->p_sys = p_sys;
996 p_enc->pf_encode_audio = Encode;
997 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
998 p_enc->fmt_out.i_codec = VLC_CODEC_SPEEX;
1000 speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
1001 1, p_speex_mode );
1003 p_sys->header.frames_per_packet = 1;
1004 p_sys->header.vbr = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
1005 p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
1007 /* Create a new encoder state in narrowband mode */
1008 p_sys->p_state = speex_encoder_init( p_speex_mode );
1010 /* Parameters */
1011 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "complexity" );
1012 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_COMPLEXITY, &i_tmp );
1014 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "cbr" ) ? 0 : 1;
1015 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR, &i_tmp );
1017 if( i_tmp == 0 ) /* CBR */
1019 i_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
1020 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_tmp );
1022 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "vad" ) ? 1 : 0;
1023 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VAD, &i_tmp );
1025 else
1027 float f_tmp;
1029 f_tmp = var_GetFloat( p_enc, ENC_CFG_PREFIX "quality" );
1030 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_QUALITY, &f_tmp );
1032 i_tmp = var_GetInteger( p_enc, ENC_CFG_PREFIX "max-bitrate" );
1033 if( i_tmp > 0 )
1034 #ifdef SPEEX_SET_VBR_MAX_BITRATE
1035 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_VBR_MAX_BITRATE, &i_tmp );
1036 #else
1037 msg_Dbg( p_enc, "max-bitrate cannot be set in this version of libspeex");
1038 #endif
1041 i_tmp = var_GetBool( p_enc, ENC_CFG_PREFIX "dtx" ) ? 1 : 0;
1042 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_DTX, &i_tmp );
1045 /*Initialization of the structure that holds the bits*/
1046 speex_bits_init( &p_sys->bits );
1048 p_sys->i_frames_in_packet = 0;
1049 p_sys->i_samples_delay = 0;
1051 speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
1052 &p_sys->i_frame_length );
1054 p_sys->i_frame_size = p_sys->i_frame_length *
1055 sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
1056 p_sys->p_buffer = xmalloc( p_sys->i_frame_size );
1058 /* Create and store headers */
1059 pp_header[0] = speex_header_to_packet( &p_sys->header, &pi_header[0] );
1060 pp_header[1] = "ENCODER=VLC media player";
1061 pi_header[1] = sizeof("ENCODER=VLC media player");
1063 p_enc->fmt_out.i_extra = 3 * 2 + pi_header[0] + pi_header[1];
1064 p_extra = p_enc->fmt_out.p_extra = xmalloc( p_enc->fmt_out.i_extra );
1065 for( i = 0; i < 2; i++ )
1067 *(p_extra++) = pi_header[i] >> 8;
1068 *(p_extra++) = pi_header[i] & 0xFF;
1069 memcpy( p_extra, pp_header[i], pi_header[i] );
1070 p_extra += pi_header[i];
1073 msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
1074 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
1075 p_enc->fmt_in.audio.i_rate );
1077 return VLC_SUCCESS;
1080 /****************************************************************************
1081 * Encode: the whole thing
1082 ****************************************************************************
1083 * This function spits out ogg packets.
1084 ****************************************************************************/
1085 static block_t *Encode( encoder_t *p_enc, block_t *p_aout_buf )
1087 encoder_sys_t *p_sys = p_enc->p_sys;
1088 block_t *p_block, *p_chain = NULL;
1090 /* Encoder gets NULL when it's time to flush */
1091 if( unlikely( !p_aout_buf ) ) return NULL;
1093 unsigned char *p_buffer = p_aout_buf->p_buffer;
1094 unsigned i_samples = p_aout_buf->i_nb_samples;
1095 int i_samples_delay = p_sys->i_samples_delay;
1097 vlc_tick_t i_pts = p_aout_buf->i_pts -
1098 vlc_tick_from_samples( p_sys->i_samples_delay,
1099 p_enc->fmt_in.audio.i_rate );
1101 p_sys->i_samples_delay += i_samples;
1103 while( p_sys->i_samples_delay >= p_sys->i_frame_length )
1105 int16_t *p_samples;
1106 int i_out;
1108 if( i_samples_delay )
1110 /* Take care of the left-over from last time */
1111 int i_delay_size = i_samples_delay * 2 *
1112 p_enc->fmt_in.audio.i_channels;
1113 int i_size = p_sys->i_frame_size - i_delay_size;
1115 p_samples = (int16_t *)p_sys->p_buffer;
1116 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
1117 p_buffer -= i_delay_size;
1118 i_samples += i_samples_delay;
1119 i_samples_delay = 0;
1121 else
1123 p_samples = (int16_t *)p_buffer;
1126 /* Encode current frame */
1127 if( p_enc->fmt_in.audio.i_channels == 2 )
1128 speex_encode_stereo_int( p_samples, p_sys->i_frame_length,
1129 &p_sys->bits );
1131 #if 0
1132 if( p_sys->preprocess )
1133 speex_preprocess( p_sys->preprocess, p_samples, NULL );
1134 #endif
1136 speex_encode_int( p_sys->p_state, p_samples, &p_sys->bits );
1138 p_buffer += p_sys->i_frame_size;
1139 p_sys->i_samples_delay -= p_sys->i_frame_length;
1140 i_samples -= p_sys->i_frame_length;
1142 p_sys->i_frames_in_packet++;
1144 if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
1145 continue;
1147 p_sys->i_frames_in_packet = 0;
1149 speex_bits_insert_terminator( &p_sys->bits );
1150 i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
1151 MAX_FRAME_BYTES );
1152 speex_bits_reset( &p_sys->bits );
1154 p_block = block_Alloc( i_out );
1155 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
1157 p_block->i_length = vlc_tick_from_samples(
1158 p_sys->i_frame_length * p_sys->header.frames_per_packet,
1159 p_enc->fmt_in.audio.i_rate );
1161 p_block->i_dts = p_block->i_pts = i_pts;
1163 /* Update pts */
1164 i_pts += p_block->i_length;
1165 block_ChainAppend( &p_chain, p_block );
1169 /* Backup the remaining raw samples */
1170 if( i_samples )
1172 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
1173 p_enc->fmt_in.audio.i_channels, p_buffer,
1174 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
1177 return p_chain;
1180 /*****************************************************************************
1181 * CloseEncoder: encoder destruction
1182 *****************************************************************************/
1183 static void CloseEncoder( vlc_object_t *p_this )
1185 encoder_t *p_enc = (encoder_t *)p_this;
1186 encoder_sys_t *p_sys = p_enc->p_sys;
1188 speex_encoder_destroy( p_sys->p_state );
1189 speex_bits_destroy( &p_sys->bits );
1191 free( p_sys->p_buffer );
1192 free( p_sys );
1194 #endif