1 /*****************************************************************************
2 * speex.c: speex decoder/packetizer module making use of libspeex.
3 *****************************************************************************
4 * Copyright (C) 1999-2001 VideoLAN
5 * $Id: speex.c,v 1.4 2003/11/22 23:39:14 fenrir 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 /*****************************************************************************
94 *****************************************************************************/
96 set_description( _("Speex audio decoder") );
97 set_capability( "decoder", 100 );
98 set_callbacks( OpenDecoder, CloseDecoder );
101 set_description( _("Speex audio packetizer") );
102 set_capability( "packetizer", 100 );
103 set_callbacks( OpenPacketizer, CloseDecoder );
106 /*****************************************************************************
107 * OpenDecoder: probe the decoder and return score
108 *****************************************************************************/
109 static int OpenDecoder( vlc_object_t *p_this )
111 decoder_t *p_dec = (decoder_t*)p_this;
112 decoder_sys_t *p_sys = p_dec->p_sys;
114 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','p','x',' ') )
119 /* Allocate the memory needed to store the decoder's structure */
120 if( ( p_dec->p_sys = p_sys =
121 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
123 msg_Err( p_dec, "out of memory" );
126 p_dec->p_sys->b_packetizer = VLC_FALSE;
128 aout_DateSet( &p_sys->end_date, 0 );
130 /* Set output properties */
131 p_dec->fmt_out.i_cat = AUDIO_ES;
132 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
135 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
137 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
140 p_sys->i_headers = 0;
141 p_sys->p_state = NULL;
142 p_sys->p_header = NULL;
143 p_sys->i_frame_in_packet = 0;
148 static int OpenPacketizer( vlc_object_t *p_this )
150 decoder_t *p_dec = (decoder_t*)p_this;
152 int i_ret = OpenDecoder( p_this );
154 if( i_ret == VLC_SUCCESS )
156 p_dec->p_sys->b_packetizer = VLC_TRUE;
157 p_dec->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
163 /****************************************************************************
164 * DecodeBlock: the whole thing
165 ****************************************************************************
166 * This function must be fed with ogg packets.
167 ****************************************************************************/
168 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
170 decoder_sys_t *p_sys = p_dec->p_sys;
171 ogg_packet oggpacket;
173 if( !pp_block ) return NULL;
177 /* Block to Ogg packet */
178 oggpacket.packet = (*pp_block)->p_buffer;
179 oggpacket.bytes = (*pp_block)->i_buffer;
183 if( p_sys->b_packetizer ) return NULL;
185 /* Block to Ogg packet */
186 oggpacket.packet = NULL;
190 oggpacket.granulepos = -1;
193 oggpacket.packetno = 0;
195 if( p_sys->i_headers == 0 )
197 /* Take care of the initial Speex header */
198 if( ProcessHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
200 msg_Err( p_dec, "Initial Speex header is corrupted" );
201 block_Release( *pp_block );
207 return ProcessPacket( p_dec, &oggpacket, pp_block );
210 if( p_sys->i_headers == 1 )
212 /* The next packet in order is the comments header */
213 ParseSpeexComments( p_dec, &oggpacket );
216 return ProcessPacket( p_dec, &oggpacket, pp_block );
219 return ProcessPacket( p_dec, &oggpacket, pp_block );
222 /*****************************************************************************
223 * ProcessHeader: processes the inital Speex header packet.
224 *****************************************************************************/
225 static int ProcessHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
227 decoder_sys_t *p_sys = p_dec->p_sys;
230 SpeexHeader *p_header;
232 SpeexCallback callback;
234 p_sys->p_header = p_header =
235 speex_packet_to_header( p_oggpacket->packet, p_oggpacket->bytes );
238 msg_Err( p_dec, "Cannot read Speex header" );
241 if( p_header->mode >= SPEEX_NB_MODES )
243 msg_Err( p_dec, "Mode number %d does not (yet/any longer) exist in "
244 "this version of libspeex", p_header->mode );
248 p_mode = speex_mode_list[p_header->mode];
250 if( p_header->speex_version_id > 1 )
252 msg_Err( p_dec, "This file was encoded with Speex bit-stream "
253 "version %d, which I don't know how to decode",
254 p_header->speex_version_id );
258 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
260 msg_Err( p_dec, "File encoded with a newer version of Speex" );
263 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
265 msg_Err( p_dec, "File encoded with an older version of Speex" );
269 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
270 p_header->rate, p_mode->modeName,
271 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
272 p_header->vbr ? ", VBR)" : ")" );
274 /* Take care of speex decoder init */
275 speex_bits_init( &p_sys->bits );
276 p_sys->p_state = p_state = speex_decoder_init( p_mode );
279 msg_Err( p_dec, "Decoder initialization failed" );
283 if( p_header->nb_channels == 2 )
285 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
286 p_sys->stereo = stereo;
287 callback.callback_id = SPEEX_INBAND_STEREO;
288 callback.func = speex_std_stereo_request_handler;
289 callback.data = &p_sys->stereo;
290 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
293 /* Setup the format */
294 p_dec->fmt_out.audio.i_physical_channels =
295 p_dec->fmt_out.audio.i_original_channels =
296 pi_channels_maps[p_header->nb_channels];
297 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
298 p_dec->fmt_out.audio.i_rate = p_header->rate;
300 aout_DateInit( &p_sys->end_date, p_header->rate );
305 /*****************************************************************************
306 * ProcessPacket: processes a Speex packet.
307 *****************************************************************************/
308 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
311 decoder_sys_t *p_sys = p_dec->p_sys;
312 block_t *p_block = *pp_block;
314 /* Date management */
315 if( p_block && p_block->i_pts > 0 &&
316 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
318 aout_DateSet( &p_sys->end_date, p_block->i_pts );
321 if( !aout_DateGet( &p_sys->end_date ) )
323 /* We've just started the stream, wait for the first PTS. */
324 if( p_block ) block_Release( p_block );
328 *pp_block = NULL; /* To avoid being fed the same packet again */
330 if( p_sys->b_packetizer )
332 return SendPacket( p_dec, p_oggpacket, p_block );
336 aout_buffer_t *p_aout_buffer;
338 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
339 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
341 p_aout_buffer = NULL; /* Skip headers */
345 block_Release( p_block );
347 return p_aout_buffer;
351 /*****************************************************************************
352 * DecodePacket: decodes a Speex packet.
353 *****************************************************************************/
354 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
356 decoder_sys_t *p_sys = p_dec->p_sys;
358 if( p_oggpacket->bytes )
360 /* Copy Ogg packet to Speex bitstream */
361 speex_bits_read_from( &p_sys->bits, p_oggpacket->packet,
362 p_oggpacket->bytes );
363 p_sys->i_frame_in_packet = 0;
366 /* Decode one frame at a time */
367 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
369 aout_buffer_t *p_aout_buffer;
373 p_dec->pf_aout_buffer_new( p_dec, p_sys->p_header->frame_size );
379 i_ret = speex_decode( p_sys->p_state, &p_sys->bits,
380 (int16_t *)p_aout_buffer->p_buffer );
381 if( i_ret == -1 ) return NULL; /* End of stream */
384 msg_Warn( p_dec, "Decoding error: corrupted stream?" );
388 if( speex_bits_remaining( &p_sys->bits ) < 0 )
390 msg_Warn( p_dec, "Decoding overflow: corrupted stream?" );
393 if( p_sys->p_header->nb_channels == 2 )
394 speex_decode_stereo( (int16_t *)p_aout_buffer->p_buffer,
395 p_sys->p_header->frame_size, &p_sys->stereo );
397 /* Date management */
398 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
399 p_aout_buffer->end_date =
400 aout_DateIncrement( &p_sys->end_date, p_sys->p_header->frame_size);
402 p_sys->i_frame_in_packet++;
404 return p_aout_buffer;
412 /*****************************************************************************
413 * SendPacket: send an ogg packet to the stream output.
414 *****************************************************************************/
415 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
418 decoder_sys_t *p_sys = p_dec->p_sys;
420 /* Date management */
421 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
423 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
425 aout_DateIncrement( &p_sys->end_date,
426 p_sys->p_header->frame_size ) -
429 p_block->i_length = 0;
434 /*****************************************************************************
435 * ParseSpeexComments: FIXME should be done in demuxer
436 *****************************************************************************/
437 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
438 ((buf[base+2]<<16)&0xff0000)| \
439 ((buf[base+1]<<8)&0xff00)| \
442 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
444 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
445 decoder_sys_t *p_sys = p_dec->p_sys;
447 input_info_category_t *p_cat =
448 input_InfoCategory( p_input, _("Speex Comment") );
450 char *p_buf = (char *)p_oggpacket->packet;
454 p_mode = speex_mode_list[p_sys->p_header->mode];
455 input_AddInfo( p_cat, _("Mode"), "%s%s",
456 p_mode->modeName, p_sys->p_header->vbr ? " VBR" : "" );
458 if( p_oggpacket->bytes < 8 )
460 msg_Warn( p_dec, "Invalid/corrupted comments" );
464 i_len = readint( p_buf, 0 ); p_buf += 4;
465 if( i_len > p_oggpacket->bytes - 4 )
467 msg_Warn( p_dec, "Invalid/corrupted comments" );
471 input_AddInfo( p_cat, p_buf, "" );
473 /* TODO: finish comments parsing */
476 /*****************************************************************************
477 * CloseDecoder: speex decoder destruction
478 *****************************************************************************/
479 static void CloseDecoder( vlc_object_t *p_this )
481 decoder_t * p_dec = (decoder_t *)p_this;
482 decoder_sys_t *p_sys = p_dec->p_sys;
486 speex_decoder_destroy( p_sys->p_state );
487 speex_bits_destroy( &p_sys->bits );
490 if( p_sys->p_header ) free( p_sys->p_header );