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.11 2004/01/29 17:51:07 zorglub 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>
29 #include "vlc_playlist.h"
33 #include "speex_header.h"
34 #include "speex_stereo.h"
35 #include "speex_callbacks.h"
37 /*****************************************************************************
38 * decoder_sys_t : speex decoder descriptor
39 *****************************************************************************/
43 vlc_bool_t b_packetizer;
49 int i_frame_in_packet;
55 SpeexHeader *p_header;
56 SpeexStereoState stereo;
62 audio_date_t end_date;
66 static int pi_channels_maps[6] =
69 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
70 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
71 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
72 | AOUT_CHAN_REARRIGHT,
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
74 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
77 /****************************************************************************
79 ****************************************************************************/
80 static int OpenDecoder ( vlc_object_t * );
81 static int OpenPacketizer( vlc_object_t * );
82 static void CloseDecoder ( vlc_object_t * );
84 static void *DecodeBlock ( decoder_t *, block_t ** );
85 static int ProcessHeader ( decoder_t *, ogg_packet * );
86 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
88 static aout_buffer_t *DecodePacket( decoder_t *, ogg_packet * );
89 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
91 static void ParseSpeexComments( decoder_t *, ogg_packet * );
93 static int OpenEncoder ( vlc_object_t * );
94 static void CloseEncoder ( vlc_object_t * );
95 static block_t *Headers ( encoder_t * );
96 static block_t *Encode ( encoder_t *, aout_buffer_t * );
98 /*****************************************************************************
100 *****************************************************************************/
102 set_description( _("Speex audio decoder") );
103 set_capability( "decoder", 100 );
104 set_callbacks( OpenDecoder, CloseDecoder );
107 set_description( _("Speex audio packetizer") );
108 set_capability( "packetizer", 100 );
109 set_callbacks( OpenPacketizer, CloseDecoder );
112 set_description( _("Speex audio encoder") );
113 set_capability( "encoder", 100 );
114 set_callbacks( OpenEncoder, CloseEncoder );
117 /*****************************************************************************
118 * OpenDecoder: probe the decoder and return score
119 *****************************************************************************/
120 static int OpenDecoder( vlc_object_t *p_this )
122 decoder_t *p_dec = (decoder_t*)p_this;
123 decoder_sys_t *p_sys = p_dec->p_sys;
125 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','p','x',' ') )
130 /* Allocate the memory needed to store the decoder's structure */
131 if( ( p_dec->p_sys = p_sys =
132 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
134 msg_Err( p_dec, "out of memory" );
137 p_dec->p_sys->b_packetizer = VLC_FALSE;
139 aout_DateSet( &p_sys->end_date, 0 );
141 /* Set output properties */
142 p_dec->fmt_out.i_cat = AUDIO_ES;
143 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
146 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
148 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
151 p_sys->i_headers = 0;
152 p_sys->p_state = NULL;
153 p_sys->p_header = NULL;
154 p_sys->i_frame_in_packet = 0;
159 static int OpenPacketizer( vlc_object_t *p_this )
161 decoder_t *p_dec = (decoder_t*)p_this;
163 int i_ret = OpenDecoder( p_this );
165 if( i_ret == VLC_SUCCESS )
167 p_dec->p_sys->b_packetizer = VLC_TRUE;
168 p_dec->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
174 /****************************************************************************
175 * DecodeBlock: the whole thing
176 ****************************************************************************
177 * This function must be fed with ogg packets.
178 ****************************************************************************/
179 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
181 decoder_sys_t *p_sys = p_dec->p_sys;
182 ogg_packet oggpacket;
184 if( !pp_block ) return NULL;
188 /* Block to Ogg packet */
189 oggpacket.packet = (*pp_block)->p_buffer;
190 oggpacket.bytes = (*pp_block)->i_buffer;
194 if( p_sys->b_packetizer ) return NULL;
196 /* Block to Ogg packet */
197 oggpacket.packet = NULL;
201 oggpacket.granulepos = -1;
204 oggpacket.packetno = 0;
206 if( p_sys->i_headers == 0 )
208 /* Take care of the initial Speex header */
209 if( ProcessHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
211 msg_Err( p_dec, "initial Speex header is corrupted" );
212 block_Release( *pp_block );
218 return ProcessPacket( p_dec, &oggpacket, pp_block );
221 if( p_sys->i_headers == 1 )
223 /* The next packet in order is the comments header */
224 ParseSpeexComments( p_dec, &oggpacket );
227 return ProcessPacket( p_dec, &oggpacket, pp_block );
230 return ProcessPacket( p_dec, &oggpacket, pp_block );
233 /*****************************************************************************
234 * ProcessHeader: processes the inital Speex header packet.
235 *****************************************************************************/
236 static int ProcessHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
238 decoder_sys_t *p_sys = p_dec->p_sys;
241 SpeexHeader *p_header;
243 SpeexCallback callback;
245 p_sys->p_header = p_header =
246 speex_packet_to_header( p_oggpacket->packet, p_oggpacket->bytes );
249 msg_Err( p_dec, "cannot read Speex header" );
252 if( p_header->mode >= SPEEX_NB_MODES )
254 msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
255 "this version of libspeex.", p_header->mode );
259 p_mode = speex_mode_list[p_header->mode];
261 if( p_header->speex_version_id > 1 )
263 msg_Err( p_dec, "this file was encoded with Speex bit-stream "
264 "version %d, which I don't know how to decode.",
265 p_header->speex_version_id );
269 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
271 msg_Err( p_dec, "file encoded with a newer version of Speex." );
274 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
276 msg_Err( p_dec, "file encoded with an older version of Speex." );
280 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
281 p_header->rate, p_mode->modeName,
282 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
283 p_header->vbr ? ", VBR)" : ")" );
285 /* Take care of speex decoder init */
286 speex_bits_init( &p_sys->bits );
287 p_sys->p_state = p_state = speex_decoder_init( p_mode );
290 msg_Err( p_dec, "decoder initialization failed" );
294 if( p_header->nb_channels == 2 )
296 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
297 p_sys->stereo = stereo;
298 callback.callback_id = SPEEX_INBAND_STEREO;
299 callback.func = speex_std_stereo_request_handler;
300 callback.data = &p_sys->stereo;
301 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
304 /* Setup the format */
305 p_dec->fmt_out.audio.i_physical_channels =
306 p_dec->fmt_out.audio.i_original_channels =
307 pi_channels_maps[p_header->nb_channels];
308 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
309 p_dec->fmt_out.audio.i_rate = p_header->rate;
311 aout_DateInit( &p_sys->end_date, p_header->rate );
316 /*****************************************************************************
317 * ProcessPacket: processes a Speex packet.
318 *****************************************************************************/
319 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
322 decoder_sys_t *p_sys = p_dec->p_sys;
323 block_t *p_block = *pp_block;
325 /* Date management */
326 if( p_block && p_block->i_pts > 0 &&
327 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
329 aout_DateSet( &p_sys->end_date, p_block->i_pts );
332 if( !aout_DateGet( &p_sys->end_date ) )
334 /* We've just started the stream, wait for the first PTS. */
335 if( p_block ) block_Release( p_block );
339 *pp_block = NULL; /* To avoid being fed the same packet again */
341 if( p_sys->b_packetizer )
343 return SendPacket( p_dec, p_oggpacket, p_block );
347 aout_buffer_t *p_aout_buffer;
349 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
350 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
352 p_aout_buffer = NULL; /* Skip headers */
356 block_Release( p_block );
358 return p_aout_buffer;
362 /*****************************************************************************
363 * DecodePacket: decodes a Speex packet.
364 *****************************************************************************/
365 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
367 decoder_sys_t *p_sys = p_dec->p_sys;
369 if( p_oggpacket->bytes )
371 /* Copy Ogg packet to Speex bitstream */
372 speex_bits_read_from( &p_sys->bits, p_oggpacket->packet,
373 p_oggpacket->bytes );
374 p_sys->i_frame_in_packet = 0;
377 /* Decode one frame at a time */
378 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
380 aout_buffer_t *p_aout_buffer;
384 p_dec->pf_aout_buffer_new( p_dec, p_sys->p_header->frame_size );
390 i_ret = speex_decode( p_sys->p_state, &p_sys->bits,
391 (int16_t *)p_aout_buffer->p_buffer );
400 msg_Warn( p_dec, "decoding error: corrupted stream?" );
404 if( speex_bits_remaining( &p_sys->bits ) < 0 )
406 msg_Warn( p_dec, "decoding overflow: corrupted stream?" );
409 if( p_sys->p_header->nb_channels == 2 )
410 speex_decode_stereo( (int16_t *)p_aout_buffer->p_buffer,
411 p_sys->p_header->frame_size, &p_sys->stereo );
413 /* Date management */
414 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
415 p_aout_buffer->end_date =
416 aout_DateIncrement( &p_sys->end_date, p_sys->p_header->frame_size);
418 p_sys->i_frame_in_packet++;
420 return p_aout_buffer;
428 /*****************************************************************************
429 * SendPacket: send an ogg packet to the stream output.
430 *****************************************************************************/
431 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
434 decoder_sys_t *p_sys = p_dec->p_sys;
436 /* Date management */
437 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
439 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
441 aout_DateIncrement( &p_sys->end_date,
442 p_sys->p_header->frame_size ) -
445 p_block->i_length = 0;
450 /*****************************************************************************
451 * ParseSpeexComments: FIXME should be done in demuxer
452 *****************************************************************************/
453 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
454 ((buf[base+2]<<16)&0xff0000)| \
455 ((buf[base+1]<<8)&0xff00)| \
458 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
460 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
461 decoder_sys_t *p_sys = p_dec->p_sys;
463 input_info_category_t *p_cat =
464 input_InfoCategory( p_input, _("Speex comment") );
465 playlist_t *p_playlist = vlc_object_find( p_dec, VLC_OBJECT_PLAYLIST,
468 char *p_buf = (char *)p_oggpacket->packet;
472 p_mode = speex_mode_list[p_sys->p_header->mode];
473 input_AddInfo( p_cat, _("Mode"), "%s%s",
474 p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" );
476 vlc_mutex_lock( &p_playlist->object_lock );
477 p_item = playlist_ItemGetByPos( p_playlist, -1 );
478 vlc_mutex_unlock( &p_playlist->object_lock );
481 msg_Err(p_dec, "unable to find item" );
484 vlc_mutex_lock( &p_item->lock );
486 playlist_ItemAddInfo( p_item, _("Speex comment") , _("Mode"),"%s%s",
487 p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" );
489 if( p_oggpacket->bytes < 8 )
491 msg_Warn( p_dec, "invalid/corrupted comments" );
495 i_len = readint( p_buf, 0 ); p_buf += 4;
496 if( i_len > p_oggpacket->bytes - 4 )
498 msg_Warn( p_dec, "invalid/corrupted comments" );
502 input_AddInfo( p_cat, p_buf, "" );
503 playlist_ItemAddInfo( p_item , _("Speex comment") , p_buf , "" );
505 vlc_mutex_unlock( &p_item->lock );
507 if( p_playlist ) vlc_object_release( p_playlist );
509 /* TODO: finish comments parsing */
512 /*****************************************************************************
513 * CloseDecoder: speex decoder destruction
514 *****************************************************************************/
515 static void CloseDecoder( vlc_object_t *p_this )
517 decoder_t * p_dec = (decoder_t *)p_this;
518 decoder_sys_t *p_sys = p_dec->p_sys;
522 speex_decoder_destroy( p_sys->p_state );
523 speex_bits_destroy( &p_sys->bits );
526 if( p_sys->p_header ) free( p_sys->p_header );
530 /*****************************************************************************
531 * encoder_sys_t: encoder descriptor
532 *****************************************************************************/
533 #define MAX_FRAME_SIZE 2000
534 #define MAX_FRAME_BYTES 2000
544 char *p_buffer_out[MAX_FRAME_BYTES];
551 SpeexStereoState stereo;
554 int i_frames_per_packet;
555 int i_frames_in_packet;
567 /*****************************************************************************
568 * OpenEncoder: probe the encoder and return score
569 *****************************************************************************/
570 static int OpenEncoder( vlc_object_t *p_this )
572 encoder_t *p_enc = (encoder_t *)p_this;
573 encoder_sys_t *p_sys;
574 SpeexMode *p_speex_mode = &speex_nb_mode;
577 if( p_enc->fmt_out.i_codec != VLC_FOURCC('s','p','x',' ') )
582 /* Allocate the memory needed to store the decoder's structure */
583 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
585 msg_Err( p_enc, "out of memory" );
588 p_enc->p_sys = p_sys;
589 p_enc->pf_header = Headers;
590 p_enc->pf_encode_audio = Encode;
591 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
593 speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
596 p_sys->header.frames_per_packet = 1;
597 p_sys->header.vbr = 1;
598 p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
600 /* Create a new encoder state in narrowband mode */
601 p_sys->p_state = speex_encoder_init( p_speex_mode );
603 /* Set the quality to 8 (15 kbps) */
605 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_quality );
607 /*Initialization of the structure that holds the bits*/
608 speex_bits_init( &p_sys->bits );
610 p_sys->i_frames_in_packet = 0;
611 p_sys->i_samples_delay = 0;
612 p_sys->i_headers = 0;
615 speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
616 &p_sys->i_frame_length );
618 p_sys->i_frame_size = p_sys->i_frame_length *
619 sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
620 p_sys->p_buffer = malloc( p_sys->i_frame_size );
622 msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
623 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
624 p_enc->fmt_in.audio.i_rate );
629 /****************************************************************************
630 * Headers: spits out the headers
631 ****************************************************************************
632 * This function spits out ogg packets.
633 ****************************************************************************/
634 static block_t *Headers( encoder_t *p_enc )
636 encoder_sys_t *p_sys = p_enc->p_sys;
637 block_t *p_block, *p_chain = NULL;
639 /* Create speex headers */
640 if( !p_sys->i_headers )
646 p_buffer = speex_header_to_packet( &p_sys->header, &i_buffer );
647 p_block = block_New( p_enc, i_buffer );
648 memcpy( p_block->p_buffer, p_buffer, i_buffer );
649 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
650 block_ChainAppend( &p_chain, p_block );
653 p_block = block_New( p_enc, sizeof("ENCODER=VLC media player") );
654 memcpy( p_block->p_buffer, "ENCODER=VLC media player",
656 p_block->i_dts = p_block->i_pts = p_block->i_length = 0;
657 block_ChainAppend( &p_chain, p_block );
659 p_sys->i_headers = 2;
665 /****************************************************************************
666 * Encode: the whole thing
667 ****************************************************************************
668 * This function spits out ogg packets.
669 ****************************************************************************/
670 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
672 encoder_sys_t *p_sys = p_enc->p_sys;
673 block_t *p_block, *p_chain = NULL;
675 char *p_buffer = p_aout_buf->p_buffer;
676 int i_samples = p_aout_buf->i_nb_samples;
677 int i_samples_delay = p_sys->i_samples_delay;
679 p_sys->i_pts = p_aout_buf->start_date -
680 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
681 (mtime_t)p_enc->fmt_in.audio.i_rate;
683 p_sys->i_samples_delay += i_samples;
685 while( p_sys->i_samples_delay >= p_sys->i_frame_length )
690 if( i_samples_delay )
692 /* Take care of the left-over from last time */
693 int i_delay_size = i_samples_delay * 2 *
694 p_enc->fmt_in.audio.i_channels;
695 int i_size = p_sys->i_frame_size - i_delay_size;
697 p_samples = (int16_t *)p_sys->p_buffer;
698 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
699 p_buffer -= i_delay_size;
700 i_samples += i_samples_delay;
705 p_samples = (int16_t *)p_buffer;
708 /* Encode current frame */
709 if( p_enc->fmt_in.audio.i_channels == 2 )
710 speex_encode_stereo( p_samples, p_sys->i_frame_length,
714 if( p_sys->preprocess )
715 speex_preprocess( p_sys->preprocess, p_samples, NULL );
718 speex_encode( p_sys->p_state, p_samples, &p_sys->bits );
720 p_buffer += p_sys->i_frame_size;
721 p_sys->i_samples_delay -= p_sys->i_frame_length;
722 i_samples -= p_sys->i_frame_length;
724 p_sys->i_frames_in_packet++;
726 if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
729 p_sys->i_frames_in_packet = 0;
731 speex_bits_insert_terminator( &p_sys->bits );
732 i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
734 speex_bits_reset( &p_sys->bits );
736 p_block = block_New( p_enc, i_out );
737 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
739 p_block->i_length = (mtime_t)1000000 *
740 (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
741 (mtime_t)p_enc->fmt_in.audio.i_rate;
743 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
746 p_sys->i_pts += p_block->i_length;
747 block_ChainAppend( &p_chain, p_block );
751 /* Backup the remaining raw samples */
754 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
755 p_enc->fmt_in.audio.i_channels, p_buffer,
756 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
762 /*****************************************************************************
763 * CloseEncoder: encoder destruction
764 *****************************************************************************/
765 static void CloseEncoder( vlc_object_t *p_this )
767 encoder_t *p_enc = (encoder_t *)p_this;
768 encoder_sys_t *p_sys = p_enc->p_sys;
770 speex_encoder_destroy( p_sys->p_state );
771 speex_bits_destroy( &p_sys->bits );
773 if( p_sys->p_buffer ) free( p_sys->p_buffer );