1 /*****************************************************************************
2 * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
3 *****************************************************************************
4 * Copyright (C) 2003 VideoLAN
5 * $Id: speex.c,v 1.8 2003/12/22 02:24:51 sam Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 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 General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/decoder.h>
32 #include "speex_header.h"
33 #include "speex_stereo.h"
34 #include "speex_callbacks.h"
36 /*****************************************************************************
37 * decoder_sys_t : speex decoder descriptor
38 *****************************************************************************/
42 vlc_bool_t b_packetizer;
48 int i_frame_in_packet;
54 SpeexHeader *p_header;
55 SpeexStereoState stereo;
61 audio_date_t end_date;
65 static int pi_channels_maps[6] =
68 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
69 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
70 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
71 | AOUT_CHAN_REARRIGHT,
72 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
73 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
76 /****************************************************************************
78 ****************************************************************************/
79 static int OpenDecoder ( vlc_object_t * );
80 static int OpenPacketizer( vlc_object_t * );
81 static void CloseDecoder ( vlc_object_t * );
83 static void *DecodeBlock ( decoder_t *, block_t ** );
84 static int ProcessHeader ( decoder_t *, ogg_packet * );
85 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
87 static aout_buffer_t *DecodePacket( decoder_t *, ogg_packet * );
88 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
90 static void ParseSpeexComments( decoder_t *, ogg_packet * );
92 static int OpenEncoder ( vlc_object_t * );
93 static void CloseEncoder ( vlc_object_t * );
94 static block_t *Headers ( encoder_t * );
95 static block_t *Encode ( encoder_t *, aout_buffer_t * );
97 /*****************************************************************************
99 *****************************************************************************/
101 set_description( _("Speex audio decoder") );
102 set_capability( "decoder", 100 );
103 set_callbacks( OpenDecoder, CloseDecoder );
106 set_description( _("Speex audio packetizer") );
107 set_capability( "packetizer", 100 );
108 set_callbacks( OpenPacketizer, CloseDecoder );
111 set_description( _("Speex audio encoder") );
112 set_capability( "encoder", 100 );
113 set_callbacks( OpenEncoder, CloseEncoder );
116 /*****************************************************************************
117 * OpenDecoder: probe the decoder and return score
118 *****************************************************************************/
119 static int OpenDecoder( vlc_object_t *p_this )
121 decoder_t *p_dec = (decoder_t*)p_this;
122 decoder_sys_t *p_sys = p_dec->p_sys;
124 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','p','x',' ') )
129 /* Allocate the memory needed to store the decoder's structure */
130 if( ( p_dec->p_sys = p_sys =
131 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
133 msg_Err( p_dec, "out of memory" );
136 p_dec->p_sys->b_packetizer = VLC_FALSE;
138 aout_DateSet( &p_sys->end_date, 0 );
140 /* Set output properties */
141 p_dec->fmt_out.i_cat = AUDIO_ES;
142 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
145 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
147 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
150 p_sys->i_headers = 0;
151 p_sys->p_state = NULL;
152 p_sys->p_header = NULL;
153 p_sys->i_frame_in_packet = 0;
158 static int OpenPacketizer( vlc_object_t *p_this )
160 decoder_t *p_dec = (decoder_t*)p_this;
162 int i_ret = OpenDecoder( p_this );
164 if( i_ret == VLC_SUCCESS )
166 p_dec->p_sys->b_packetizer = VLC_TRUE;
167 p_dec->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
173 /****************************************************************************
174 * DecodeBlock: the whole thing
175 ****************************************************************************
176 * This function must be fed with ogg packets.
177 ****************************************************************************/
178 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
180 decoder_sys_t *p_sys = p_dec->p_sys;
181 ogg_packet oggpacket;
183 if( !pp_block ) return NULL;
187 /* Block to Ogg packet */
188 oggpacket.packet = (*pp_block)->p_buffer;
189 oggpacket.bytes = (*pp_block)->i_buffer;
193 if( p_sys->b_packetizer ) return NULL;
195 /* Block to Ogg packet */
196 oggpacket.packet = NULL;
200 oggpacket.granulepos = -1;
203 oggpacket.packetno = 0;
205 if( p_sys->i_headers == 0 )
207 /* Take care of the initial Speex header */
208 if( ProcessHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
210 msg_Err( p_dec, "Initial Speex header is corrupted" );
211 block_Release( *pp_block );
217 return ProcessPacket( p_dec, &oggpacket, pp_block );
220 if( p_sys->i_headers == 1 )
222 /* The next packet in order is the comments header */
223 ParseSpeexComments( p_dec, &oggpacket );
226 return ProcessPacket( p_dec, &oggpacket, pp_block );
229 return ProcessPacket( p_dec, &oggpacket, pp_block );
232 /*****************************************************************************
233 * ProcessHeader: processes the inital Speex header packet.
234 *****************************************************************************/
235 static int ProcessHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
237 decoder_sys_t *p_sys = p_dec->p_sys;
240 SpeexHeader *p_header;
242 SpeexCallback callback;
244 p_sys->p_header = p_header =
245 speex_packet_to_header( p_oggpacket->packet, p_oggpacket->bytes );
248 msg_Err( p_dec, "Cannot read Speex header" );
251 if( p_header->mode >= SPEEX_NB_MODES )
253 msg_Err( p_dec, "Mode number %d does not (yet/any longer) exist in "
254 "this version of libspeex", p_header->mode );
258 p_mode = speex_mode_list[p_header->mode];
260 if( p_header->speex_version_id > 1 )
262 msg_Err( p_dec, "This file was encoded with Speex bit-stream "
263 "version %d, which I don't know how to decode",
264 p_header->speex_version_id );
268 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
270 msg_Err( p_dec, "File encoded with a newer version of Speex" );
273 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
275 msg_Err( p_dec, "File encoded with an older version of Speex" );
279 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
280 p_header->rate, p_mode->modeName,
281 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
282 p_header->vbr ? ", VBR)" : ")" );
284 /* Take care of speex decoder init */
285 speex_bits_init( &p_sys->bits );
286 p_sys->p_state = p_state = speex_decoder_init( p_mode );
289 msg_Err( p_dec, "Decoder initialization failed" );
293 if( p_header->nb_channels == 2 )
295 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
296 p_sys->stereo = stereo;
297 callback.callback_id = SPEEX_INBAND_STEREO;
298 callback.func = speex_std_stereo_request_handler;
299 callback.data = &p_sys->stereo;
300 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
303 /* Setup the format */
304 p_dec->fmt_out.audio.i_physical_channels =
305 p_dec->fmt_out.audio.i_original_channels =
306 pi_channels_maps[p_header->nb_channels];
307 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
308 p_dec->fmt_out.audio.i_rate = p_header->rate;
310 aout_DateInit( &p_sys->end_date, p_header->rate );
315 /*****************************************************************************
316 * ProcessPacket: processes a Speex packet.
317 *****************************************************************************/
318 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
321 decoder_sys_t *p_sys = p_dec->p_sys;
322 block_t *p_block = *pp_block;
324 /* Date management */
325 if( p_block && p_block->i_pts > 0 &&
326 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
328 aout_DateSet( &p_sys->end_date, p_block->i_pts );
331 if( !aout_DateGet( &p_sys->end_date ) )
333 /* We've just started the stream, wait for the first PTS. */
334 if( p_block ) block_Release( p_block );
338 *pp_block = NULL; /* To avoid being fed the same packet again */
340 if( p_sys->b_packetizer )
342 return SendPacket( p_dec, p_oggpacket, p_block );
346 aout_buffer_t *p_aout_buffer;
348 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
349 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
351 p_aout_buffer = NULL; /* Skip headers */
355 block_Release( p_block );
357 return p_aout_buffer;
361 /*****************************************************************************
362 * DecodePacket: decodes a Speex packet.
363 *****************************************************************************/
364 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
366 decoder_sys_t *p_sys = p_dec->p_sys;
368 if( p_oggpacket->bytes )
370 /* Copy Ogg packet to Speex bitstream */
371 speex_bits_read_from( &p_sys->bits, p_oggpacket->packet,
372 p_oggpacket->bytes );
373 p_sys->i_frame_in_packet = 0;
376 /* Decode one frame at a time */
377 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
379 aout_buffer_t *p_aout_buffer;
383 p_dec->pf_aout_buffer_new( p_dec, p_sys->p_header->frame_size );
389 i_ret = speex_decode( p_sys->p_state, &p_sys->bits,
390 (int16_t *)p_aout_buffer->p_buffer );
399 msg_Warn( p_dec, "Decoding error: corrupted stream?" );
403 if( speex_bits_remaining( &p_sys->bits ) < 0 )
405 msg_Warn( p_dec, "Decoding overflow: corrupted stream?" );
408 if( p_sys->p_header->nb_channels == 2 )
409 speex_decode_stereo( (int16_t *)p_aout_buffer->p_buffer,
410 p_sys->p_header->frame_size, &p_sys->stereo );
412 /* Date management */
413 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
414 p_aout_buffer->end_date =
415 aout_DateIncrement( &p_sys->end_date, p_sys->p_header->frame_size);
417 p_sys->i_frame_in_packet++;
419 return p_aout_buffer;
427 /*****************************************************************************
428 * SendPacket: send an ogg packet to the stream output.
429 *****************************************************************************/
430 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
433 decoder_sys_t *p_sys = p_dec->p_sys;
435 /* Date management */
436 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
438 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
440 aout_DateIncrement( &p_sys->end_date,
441 p_sys->p_header->frame_size ) -
444 p_block->i_length = 0;
449 /*****************************************************************************
450 * ParseSpeexComments: FIXME should be done in demuxer
451 *****************************************************************************/
452 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
453 ((buf[base+2]<<16)&0xff0000)| \
454 ((buf[base+1]<<8)&0xff00)| \
457 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
459 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
460 decoder_sys_t *p_sys = p_dec->p_sys;
462 input_info_category_t *p_cat =
463 input_InfoCategory( p_input, _("Speex comment") );
465 char *p_buf = (char *)p_oggpacket->packet;
469 p_mode = speex_mode_list[p_sys->p_header->mode];
470 input_AddInfo( p_cat, _("Mode"), "%s%s",
471 p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" );
473 if( p_oggpacket->bytes < 8 )
475 msg_Warn( p_dec, "Invalid/corrupted comments" );
479 i_len = readint( p_buf, 0 ); p_buf += 4;
480 if( i_len > p_oggpacket->bytes - 4 )
482 msg_Warn( p_dec, "Invalid/corrupted comments" );
486 input_AddInfo( p_cat, p_buf, "" );
488 /* TODO: finish comments parsing */
491 /*****************************************************************************
492 * CloseDecoder: speex decoder destruction
493 *****************************************************************************/
494 static void CloseDecoder( vlc_object_t *p_this )
496 decoder_t * p_dec = (decoder_t *)p_this;
497 decoder_sys_t *p_sys = p_dec->p_sys;
501 speex_decoder_destroy( p_sys->p_state );
502 speex_bits_destroy( &p_sys->bits );
505 if( p_sys->p_header ) free( p_sys->p_header );
509 /*****************************************************************************
510 * encoder_sys_t: encoder descriptor
511 *****************************************************************************/
512 #define MAX_FRAME_SIZE 2000
513 #define MAX_FRAME_BYTES 2000
523 char *p_buffer_out[MAX_FRAME_BYTES];
530 SpeexStereoState stereo;
533 int i_frames_per_packet;
534 int i_frames_in_packet;
546 /*****************************************************************************
547 * OpenEncoder: probe the encoder and return score
548 *****************************************************************************/
549 static int OpenEncoder( vlc_object_t *p_this )
551 encoder_t *p_enc = (encoder_t *)p_this;
552 encoder_sys_t *p_sys;
553 SpeexMode *p_speex_mode = &speex_nb_mode;
556 if( p_enc->fmt_out.i_codec != VLC_FOURCC('s','p','x',' ') )
561 /* Allocate the memory needed to store the decoder's structure */
562 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
564 msg_Err( p_enc, "out of memory" );
567 p_enc->p_sys = p_sys;
568 p_enc->pf_header = Headers;
569 p_enc->pf_encode_audio = Encode;
570 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
572 speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
575 p_sys->header.frames_per_packet = 1;
576 p_sys->header.vbr = 1;
577 p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
579 /* Create a new encoder state in narrowband mode */
580 p_sys->p_state = speex_encoder_init( p_speex_mode );
582 /* Set the quality to 8 (15 kbps) */
584 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_quality );
586 /*Initialization of the structure that holds the bits*/
587 speex_bits_init( &p_sys->bits );
589 p_sys->i_frames_in_packet = 0;
590 p_sys->i_samples_delay = 0;
591 p_sys->i_headers = 0;
594 speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
595 &p_sys->i_frame_length );
597 p_sys->i_frame_size = p_sys->i_frame_length *
598 sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
599 p_sys->p_buffer = malloc( p_sys->i_frame_size );
601 msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
602 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
603 p_enc->fmt_in.audio.i_rate );
608 /****************************************************************************
609 * Headers: spits out the headers
610 ****************************************************************************
611 * This function spits out ogg packets.
612 ****************************************************************************/
613 static block_t *Headers( encoder_t *p_enc )
615 encoder_sys_t *p_sys = p_enc->p_sys;
616 block_t *p_block, *p_chain = NULL;
618 /* Create speex headers */
619 if( !p_sys->i_headers )
625 p_buffer = speex_header_to_packet( &p_sys->header, &i_buffer );
626 p_block = block_New( p_enc, i_buffer );
627 memcpy( p_block->p_buffer, p_buffer, i_buffer );
628 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
629 block_ChainAppend( &p_chain, p_block );
632 p_block = block_New( p_enc, sizeof("ENCODER=VLC media player") );
633 memcpy( p_block->p_buffer, "ENCODER=VLC media player",
635 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
636 block_ChainAppend( &p_chain, p_block );
638 p_sys->i_headers = 2;
644 /****************************************************************************
645 * Encode: the whole thing
646 ****************************************************************************
647 * This function spits out ogg packets.
648 ****************************************************************************/
649 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
651 encoder_sys_t *p_sys = p_enc->p_sys;
652 block_t *p_block, *p_chain = NULL;
654 char *p_buffer = p_aout_buf->p_buffer;
655 int i_samples = p_aout_buf->i_nb_samples;
656 int i_samples_delay = p_sys->i_samples_delay;
658 p_sys->i_pts = p_aout_buf->start_date -
659 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
660 (mtime_t)p_enc->fmt_in.audio.i_rate;
662 p_sys->i_samples_delay += i_samples;
664 while( p_sys->i_samples_delay >= p_sys->i_frame_length )
669 if( i_samples_delay )
671 /* Take care of the left-over from last time */
672 int i_delay_size = i_samples_delay * 2 *
673 p_enc->fmt_in.audio.i_channels;
674 int i_size = p_sys->i_frame_size - i_delay_size;
676 p_samples = (int16_t *)p_sys->p_buffer;
677 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
678 p_buffer -= i_delay_size;
679 i_samples += i_samples_delay;
684 p_samples = (int16_t *)p_buffer;
687 /* Encode current frame */
688 if( p_enc->fmt_in.audio.i_channels == 2 )
689 speex_encode_stereo( p_samples, p_sys->i_frame_length,
693 if( p_sys->preprocess )
694 speex_preprocess( p_sys->preprocess, p_samples, NULL );
697 speex_encode( p_sys->p_state, p_samples, &p_sys->bits );
699 p_buffer += p_sys->i_frame_size;
700 p_sys->i_samples_delay -= p_sys->i_frame_length;
701 i_samples -= p_sys->i_frame_length;
703 p_sys->i_frames_in_packet++;
705 if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
708 p_sys->i_frames_in_packet = 0;
710 speex_bits_insert_terminator( &p_sys->bits );
711 i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
713 speex_bits_reset( &p_sys->bits );
715 p_block = block_New( p_enc, i_out );
716 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
718 p_block->i_length = (mtime_t)1000000 *
719 (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
720 (mtime_t)p_enc->fmt_in.audio.i_rate;
722 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
725 p_sys->i_pts += p_block->i_length;
726 block_ChainAppend( &p_chain, p_block );
730 /* Backup the remaining raw samples */
733 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
734 p_enc->fmt_in.audio.i_channels, p_buffer,
735 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
741 /*****************************************************************************
742 * CloseEncoder: encoder destruction
743 *****************************************************************************/
744 static void CloseEncoder( vlc_object_t *p_this )
746 encoder_t *p_enc = (encoder_t *)p_this;
747 encoder_sys_t *p_sys = p_enc->p_sys;
749 speex_encoder_destroy( p_sys->p_state );
750 speex_bits_destroy( &p_sys->bits );
752 if( p_sys->p_buffer ) free( p_sys->p_buffer );