1 /*****************************************************************************
2 * speex.c: speex decoder/packetizer/encoder module making use of libspeex.
3 *****************************************************************************
4 * Copyright (C) 2003 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc_input.h>
29 #include <vlc_codec.h>
33 #include <speex/speex.h>
34 #include <speex/speex_header.h>
35 #include <speex/speex_stereo.h>
36 #include <speex/speex_callbacks.h>
38 /*****************************************************************************
39 * decoder_sys_t : speex decoder descriptor
40 *****************************************************************************/
44 vlc_bool_t b_packetizer;
50 int i_frame_in_packet;
56 SpeexHeader *p_header;
57 SpeexStereoState stereo;
63 audio_date_t end_date;
67 static int pi_channels_maps[6] =
70 AOUT_CHAN_CENTER, AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
71 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
72 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
73 | AOUT_CHAN_REARRIGHT,
74 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
75 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
78 /****************************************************************************
80 ****************************************************************************/
81 static int OpenDecoder ( vlc_object_t * );
82 static int OpenPacketizer( vlc_object_t * );
83 static void CloseDecoder ( vlc_object_t * );
85 static void *DecodeBlock ( decoder_t *, block_t ** );
86 static int ProcessHeaders( decoder_t * );
87 static int ProcessInitialHeader ( decoder_t *, ogg_packet * );
88 static void *ProcessPacket( decoder_t *, ogg_packet *, block_t ** );
90 static aout_buffer_t *DecodePacket( decoder_t *, ogg_packet * );
91 static block_t *SendPacket( decoder_t *, ogg_packet *, block_t * );
93 static void ParseSpeexComments( decoder_t *, ogg_packet * );
95 static int OpenEncoder ( vlc_object_t * );
96 static void CloseEncoder ( vlc_object_t * );
97 static block_t *Encode ( encoder_t *, aout_buffer_t * );
99 /*****************************************************************************
101 *****************************************************************************/
103 set_category( CAT_INPUT );
104 set_subcategory( SUBCAT_INPUT_ACODEC );
106 set_description( _("Speex audio decoder") );
107 set_capability( "decoder", 100 );
108 set_callbacks( OpenDecoder, CloseDecoder );
111 set_description( _("Speex audio packetizer") );
112 set_capability( "packetizer", 100 );
113 set_callbacks( OpenPacketizer, CloseDecoder );
116 set_description( _("Speex audio encoder") );
117 set_capability( "encoder", 100 );
118 set_callbacks( OpenEncoder, CloseEncoder );
121 /*****************************************************************************
122 * OpenDecoder: probe the decoder and return score
123 *****************************************************************************/
124 static int OpenDecoder( vlc_object_t *p_this )
126 decoder_t *p_dec = (decoder_t*)p_this;
127 decoder_sys_t *p_sys = p_dec->p_sys;
129 if( p_dec->fmt_in.i_codec != VLC_FOURCC('s','p','x',' ') )
134 /* Allocate the memory needed to store the decoder's structure */
135 if( ( p_dec->p_sys = p_sys =
136 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
138 msg_Err( p_dec, "out of memory" );
141 p_dec->p_sys->b_packetizer = VLC_FALSE;
143 aout_DateSet( &p_sys->end_date, 0 );
145 /* Set output properties */
146 p_dec->fmt_out.i_cat = AUDIO_ES;
147 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
150 p_dec->pf_decode_audio = (aout_buffer_t *(*)(decoder_t *, block_t **))
152 p_dec->pf_packetize = (block_t *(*)(decoder_t *, block_t **))
155 p_sys->i_headers = 0;
156 p_sys->p_state = NULL;
157 p_sys->p_header = NULL;
158 p_sys->i_frame_in_packet = 0;
163 static int OpenPacketizer( vlc_object_t *p_this )
165 decoder_t *p_dec = (decoder_t*)p_this;
167 int i_ret = OpenDecoder( p_this );
169 if( i_ret == VLC_SUCCESS )
171 p_dec->p_sys->b_packetizer = VLC_TRUE;
172 p_dec->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
178 /****************************************************************************
179 * DecodeBlock: the whole thing
180 ****************************************************************************
181 * This function must be fed with ogg packets.
182 ****************************************************************************/
183 static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
185 decoder_sys_t *p_sys = p_dec->p_sys;
186 ogg_packet oggpacket;
188 if( !pp_block ) return NULL;
192 /* Block to Ogg packet */
193 oggpacket.packet = (*pp_block)->p_buffer;
194 oggpacket.bytes = (*pp_block)->i_buffer;
198 if( p_sys->b_packetizer ) return NULL;
200 /* Block to Ogg packet */
201 oggpacket.packet = NULL;
205 oggpacket.granulepos = -1;
208 oggpacket.packetno = 0;
210 /* Check for headers */
211 if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
213 /* Headers already available as extra data */
214 p_sys->i_headers = 2;
216 else if( oggpacket.bytes && p_sys->i_headers < 2 )
218 /* Backup headers as extra data */
221 p_dec->fmt_in.p_extra =
222 realloc( p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra +
223 oggpacket.bytes + 2 );
224 p_extra = ((uint8_t *)p_dec->fmt_in.p_extra) + p_dec->fmt_in.i_extra;
225 *(p_extra++) = oggpacket.bytes >> 8;
226 *(p_extra++) = oggpacket.bytes & 0xFF;
228 memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
229 p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
231 block_Release( *pp_block );
236 if( p_sys->i_headers == 2 )
238 if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
240 p_sys->i_headers = 0;
241 p_dec->fmt_in.i_extra = 0;
242 block_Release( *pp_block );
245 else p_sys->i_headers++;
248 return ProcessPacket( p_dec, &oggpacket, pp_block );
251 /*****************************************************************************
252 * ProcessHeaders: process Speex headers.
253 *****************************************************************************/
254 static int ProcessHeaders( decoder_t *p_dec )
256 decoder_sys_t *p_sys = p_dec->p_sys;
257 ogg_packet oggpacket;
261 if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
263 oggpacket.granulepos = -1;
264 oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
266 oggpacket.packetno = 0;
267 p_extra = p_dec->fmt_in.p_extra;
268 i_extra = p_dec->fmt_in.i_extra;
270 /* Take care of the initial Vorbis header */
271 oggpacket.bytes = *(p_extra++) << 8;
272 oggpacket.bytes |= (*(p_extra++) & 0xFF);
273 oggpacket.packet = p_extra;
274 p_extra += oggpacket.bytes;
275 i_extra -= (oggpacket.bytes + 2);
278 msg_Err( p_dec, "header data corrupted");
282 /* Take care of the initial Speex header */
283 if( ProcessInitialHeader( p_dec, &oggpacket ) != VLC_SUCCESS )
285 msg_Err( p_dec, "initial Speex header is corrupted" );
289 /* The next packet in order is the comments header */
291 oggpacket.bytes = *(p_extra++) << 8;
292 oggpacket.bytes |= (*(p_extra++) & 0xFF);
293 oggpacket.packet = p_extra;
294 p_extra += oggpacket.bytes;
295 i_extra -= (oggpacket.bytes + 2);
298 msg_Err( p_dec, "header data corrupted");
302 ParseSpeexComments( p_dec, &oggpacket );
304 if( p_sys->b_packetizer )
306 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
307 p_dec->fmt_out.p_extra =
308 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
309 memcpy( p_dec->fmt_out.p_extra,
310 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
316 /*****************************************************************************
317 * ProcessInitialHeader: processes the inital Speex header packet.
318 *****************************************************************************/
319 static int ProcessInitialHeader( decoder_t *p_dec, ogg_packet *p_oggpacket )
321 decoder_sys_t *p_sys = p_dec->p_sys;
324 SpeexHeader *p_header;
325 const SpeexMode *p_mode;
326 SpeexCallback callback;
328 p_sys->p_header = p_header =
329 speex_packet_to_header( (char *)p_oggpacket->packet,
330 p_oggpacket->bytes );
333 msg_Err( p_dec, "cannot read Speex header" );
336 if( p_header->mode >= SPEEX_NB_MODES )
338 msg_Err( p_dec, "mode number %d does not (yet/any longer) exist in "
339 "this version of libspeex.", p_header->mode );
343 p_mode = speex_mode_list[p_header->mode];
345 if( p_header->speex_version_id > 1 )
347 msg_Err( p_dec, "this file was encoded with Speex bit-stream "
348 "version %d which is not supported by this decoder.",
349 p_header->speex_version_id );
353 if( p_mode->bitstream_version < p_header->mode_bitstream_version )
355 msg_Err( p_dec, "file encoded with a newer version of Speex." );
358 if( p_mode->bitstream_version > p_header->mode_bitstream_version )
360 msg_Err( p_dec, "file encoded with an older version of Speex." );
364 msg_Dbg( p_dec, "Speex %d Hz audio using %s mode %s%s",
365 p_header->rate, p_mode->modeName,
366 ( p_header->nb_channels == 1 ) ? " (mono" : " (stereo",
367 p_header->vbr ? ", VBR)" : ")" );
369 /* Take care of speex decoder init */
370 speex_bits_init( &p_sys->bits );
371 p_sys->p_state = p_state = speex_decoder_init( p_mode );
374 msg_Err( p_dec, "decoder initialization failed" );
378 if( p_header->nb_channels == 2 )
380 SpeexStereoState stereo = SPEEX_STEREO_STATE_INIT;
381 p_sys->stereo = stereo;
382 callback.callback_id = SPEEX_INBAND_STEREO;
383 callback.func = speex_std_stereo_request_handler;
384 callback.data = &p_sys->stereo;
385 speex_decoder_ctl( p_state, SPEEX_SET_HANDLER, &callback );
387 if( p_header->nb_channels <= 0 ||
388 p_header->nb_channels > 5 )
390 msg_Err( p_dec, "invalid number of channels (not between 1 and 5): %i",
391 p_header->nb_channels );
395 /* Setup the format */
396 p_dec->fmt_out.audio.i_physical_channels =
397 p_dec->fmt_out.audio.i_original_channels =
398 pi_channels_maps[p_header->nb_channels];
399 p_dec->fmt_out.audio.i_channels = p_header->nb_channels;
400 p_dec->fmt_out.audio.i_rate = p_header->rate;
402 aout_DateInit( &p_sys->end_date, p_header->rate );
407 /*****************************************************************************
408 * ProcessPacket: processes a Speex packet.
409 *****************************************************************************/
410 static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
413 decoder_sys_t *p_sys = p_dec->p_sys;
414 block_t *p_block = *pp_block;
416 /* Date management */
417 if( p_block && p_block->i_pts > 0 &&
418 p_block->i_pts != aout_DateGet( &p_sys->end_date ) )
420 aout_DateSet( &p_sys->end_date, p_block->i_pts );
423 if( !aout_DateGet( &p_sys->end_date ) )
425 /* We've just started the stream, wait for the first PTS. */
426 if( p_block ) block_Release( p_block );
430 *pp_block = NULL; /* To avoid being fed the same packet again */
432 if( p_sys->b_packetizer )
434 return SendPacket( p_dec, p_oggpacket, p_block );
438 aout_buffer_t *p_aout_buffer;
440 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
441 p_aout_buffer = DecodePacket( p_dec, p_oggpacket );
443 p_aout_buffer = NULL; /* Skip headers */
445 if( p_block ) block_Release( p_block );
446 return p_aout_buffer;
450 /*****************************************************************************
451 * DecodePacket: decodes a Speex packet.
452 *****************************************************************************/
453 static aout_buffer_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket )
455 decoder_sys_t *p_sys = p_dec->p_sys;
457 if( p_oggpacket->bytes )
459 /* Copy Ogg packet to Speex bitstream */
460 speex_bits_read_from( &p_sys->bits, (char *)p_oggpacket->packet,
461 p_oggpacket->bytes );
462 p_sys->i_frame_in_packet = 0;
465 /* Decode one frame at a time */
466 if( p_sys->i_frame_in_packet < p_sys->p_header->frames_per_packet )
468 aout_buffer_t *p_aout_buffer;
472 p_dec->pf_aout_buffer_new( p_dec, p_sys->p_header->frame_size );
478 i_ret = speex_decode_int( p_sys->p_state, &p_sys->bits,
479 (int16_t *)p_aout_buffer->p_buffer );
488 msg_Warn( p_dec, "decoding error: corrupted stream?" );
492 if( speex_bits_remaining( &p_sys->bits ) < 0 )
494 msg_Warn( p_dec, "decoding overflow: corrupted stream?" );
497 if( p_sys->p_header->nb_channels == 2 )
498 speex_decode_stereo_int( (int16_t *)p_aout_buffer->p_buffer,
499 p_sys->p_header->frame_size,
502 /* Date management */
503 p_aout_buffer->start_date = aout_DateGet( &p_sys->end_date );
504 p_aout_buffer->end_date =
505 aout_DateIncrement( &p_sys->end_date, p_sys->p_header->frame_size);
507 p_sys->i_frame_in_packet++;
509 return p_aout_buffer;
517 /*****************************************************************************
518 * SendPacket: send an ogg packet to the stream output.
519 *****************************************************************************/
520 static block_t *SendPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
523 decoder_sys_t *p_sys = p_dec->p_sys;
525 /* Date management */
526 p_block->i_dts = p_block->i_pts = aout_DateGet( &p_sys->end_date );
528 if( p_sys->i_headers >= p_sys->p_header->extra_headers + 2 )
530 aout_DateIncrement( &p_sys->end_date,
531 p_sys->p_header->frame_size ) -
534 p_block->i_length = 0;
539 /*****************************************************************************
540 * ParseSpeexComments: FIXME should be done in demuxer
541 *****************************************************************************/
542 #define readint(buf, base) (((buf[base+3]<<24)&0xff000000)| \
543 ((buf[base+2]<<16)&0xff0000)| \
544 ((buf[base+1]<<8)&0xff00)| \
547 static void ParseSpeexComments( decoder_t *p_dec, ogg_packet *p_oggpacket )
549 input_thread_t *p_input = (input_thread_t *)p_dec->p_parent;
550 decoder_sys_t *p_sys = p_dec->p_sys;
552 char *p_buf = (char *)p_oggpacket->packet;
553 const SpeexMode *p_mode;
556 if( p_input->i_object_type != VLC_OBJECT_INPUT ) return;
558 p_mode = speex_mode_list[p_sys->p_header->mode];
560 input_Control( p_input, INPUT_ADD_INFO, _("Speex comment"), _("Mode"),
561 "%s%s", p_mode->modeName,
562 p_sys->p_header->vbr ? " VBR" : "" );
564 if( p_oggpacket->bytes < 8 )
566 msg_Warn( p_dec, "invalid/corrupted comments" );
570 i_len = readint( p_buf, 0 ); p_buf += 4;
571 if( i_len > p_oggpacket->bytes - 4 )
573 msg_Warn( p_dec, "invalid/corrupted comments" );
577 input_Control( p_input, INPUT_ADD_INFO, _("Speex comment"), p_buf, "" );
579 /* TODO: finish comments parsing */
582 /*****************************************************************************
583 * CloseDecoder: speex decoder destruction
584 *****************************************************************************/
585 static void CloseDecoder( vlc_object_t *p_this )
587 decoder_t * p_dec = (decoder_t *)p_this;
588 decoder_sys_t *p_sys = p_dec->p_sys;
592 speex_decoder_destroy( p_sys->p_state );
593 speex_bits_destroy( &p_sys->bits );
596 if( p_sys->p_header ) free( p_sys->p_header );
600 /*****************************************************************************
601 * encoder_sys_t: encoder descriptor
602 *****************************************************************************/
603 #define MAX_FRAME_SIZE 2000
604 #define MAX_FRAME_BYTES 2000
612 char p_buffer_out[MAX_FRAME_BYTES];
619 SpeexStereoState stereo;
622 int i_frames_per_packet;
623 int i_frames_in_packet;
635 /*****************************************************************************
636 * OpenEncoder: probe the encoder and return score
637 *****************************************************************************/
638 static int OpenEncoder( vlc_object_t *p_this )
640 encoder_t *p_enc = (encoder_t *)p_this;
641 encoder_sys_t *p_sys;
642 const SpeexMode *p_speex_mode = &speex_nb_mode;
648 if( p_enc->fmt_out.i_codec != VLC_FOURCC('s','p','x',' ') &&
654 /* Allocate the memory needed to store the decoder's structure */
655 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
657 msg_Err( p_enc, "out of memory" );
660 p_enc->p_sys = p_sys;
661 p_enc->pf_encode_audio = Encode;
662 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
663 p_enc->fmt_out.i_codec = VLC_FOURCC('s','p','x',' ');
665 speex_init_header( &p_sys->header, p_enc->fmt_in.audio.i_rate,
668 p_sys->header.frames_per_packet = 1;
669 p_sys->header.vbr = 1;
670 p_sys->header.nb_channels = p_enc->fmt_in.audio.i_channels;
672 /* Create a new encoder state in narrowband mode */
673 p_sys->p_state = speex_encoder_init( p_speex_mode );
675 /* Set the quality to 8 (15 kbps) */
677 speex_encoder_ctl( p_sys->p_state, SPEEX_SET_QUALITY, &i_quality );
679 /*Initialization of the structure that holds the bits*/
680 speex_bits_init( &p_sys->bits );
682 p_sys->i_frames_in_packet = 0;
683 p_sys->i_samples_delay = 0;
686 speex_encoder_ctl( p_sys->p_state, SPEEX_GET_FRAME_SIZE,
687 &p_sys->i_frame_length );
689 p_sys->i_frame_size = p_sys->i_frame_length *
690 sizeof(int16_t) * p_enc->fmt_in.audio.i_channels;
691 p_sys->p_buffer = malloc( p_sys->i_frame_size );
693 /* Create and store headers */
694 pp_header[0] = speex_header_to_packet( &p_sys->header, &pi_header[0] );
695 pp_header[1] = "ENCODER=VLC media player";
696 pi_header[1] = sizeof("ENCODER=VLC media player");
698 p_enc->fmt_out.i_extra = 3 * 2 + pi_header[0] + pi_header[1];
699 p_extra = p_enc->fmt_out.p_extra = malloc( p_enc->fmt_out.i_extra );
700 for( i = 0; i < 2; i++ )
702 *(p_extra++) = pi_header[i] >> 8;
703 *(p_extra++) = pi_header[i] & 0xFF;
704 memcpy( p_extra, pp_header[i], pi_header[i] );
705 p_extra += pi_header[i];
708 msg_Dbg( p_enc, "encoding: frame size:%d, channels:%d, samplerate:%d",
709 p_sys->i_frame_size, p_enc->fmt_in.audio.i_channels,
710 p_enc->fmt_in.audio.i_rate );
715 /****************************************************************************
716 * Encode: the whole thing
717 ****************************************************************************
718 * This function spits out ogg packets.
719 ****************************************************************************/
720 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
722 encoder_sys_t *p_sys = p_enc->p_sys;
723 block_t *p_block, *p_chain = NULL;
725 unsigned char *p_buffer = p_aout_buf->p_buffer;
726 int i_samples = p_aout_buf->i_nb_samples;
727 int i_samples_delay = p_sys->i_samples_delay;
729 p_sys->i_pts = p_aout_buf->start_date -
730 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
731 (mtime_t)p_enc->fmt_in.audio.i_rate;
733 p_sys->i_samples_delay += i_samples;
735 while( p_sys->i_samples_delay >= p_sys->i_frame_length )
740 if( i_samples_delay )
742 /* Take care of the left-over from last time */
743 int i_delay_size = i_samples_delay * 2 *
744 p_enc->fmt_in.audio.i_channels;
745 int i_size = p_sys->i_frame_size - i_delay_size;
747 p_samples = (int16_t *)p_sys->p_buffer;
748 memcpy( p_sys->p_buffer + i_delay_size, p_buffer, i_size );
749 p_buffer -= i_delay_size;
750 i_samples += i_samples_delay;
755 p_samples = (int16_t *)p_buffer;
758 /* Encode current frame */
759 if( p_enc->fmt_in.audio.i_channels == 2 )
760 speex_encode_stereo_int( p_samples, p_sys->i_frame_length,
764 if( p_sys->preprocess )
765 speex_preprocess( p_sys->preprocess, p_samples, NULL );
768 speex_encode_int( p_sys->p_state, p_samples, &p_sys->bits );
770 p_buffer += p_sys->i_frame_size;
771 p_sys->i_samples_delay -= p_sys->i_frame_length;
772 i_samples -= p_sys->i_frame_length;
774 p_sys->i_frames_in_packet++;
776 if( p_sys->i_frames_in_packet < p_sys->header.frames_per_packet )
779 p_sys->i_frames_in_packet = 0;
781 speex_bits_insert_terminator( &p_sys->bits );
782 i_out = speex_bits_write( &p_sys->bits, p_sys->p_buffer_out,
784 speex_bits_reset( &p_sys->bits );
786 p_block = block_New( p_enc, i_out );
787 memcpy( p_block->p_buffer, p_sys->p_buffer_out, i_out );
789 p_block->i_length = (mtime_t)1000000 *
790 (mtime_t)p_sys->i_frame_length * p_sys->header.frames_per_packet /
791 (mtime_t)p_enc->fmt_in.audio.i_rate;
793 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
796 p_sys->i_pts += p_block->i_length;
797 block_ChainAppend( &p_chain, p_block );
801 /* Backup the remaining raw samples */
804 memcpy( p_sys->p_buffer + i_samples_delay * 2 *
805 p_enc->fmt_in.audio.i_channels, p_buffer,
806 i_samples * 2 * p_enc->fmt_in.audio.i_channels );
812 /*****************************************************************************
813 * CloseEncoder: encoder destruction
814 *****************************************************************************/
815 static void CloseEncoder( vlc_object_t *p_this )
817 encoder_t *p_enc = (encoder_t *)p_this;
818 encoder_sys_t *p_sys = p_enc->p_sys;
820 speex_encoder_destroy( p_sys->p_state );
821 speex_bits_destroy( &p_sys->bits );
823 if( p_sys->p_buffer ) free( p_sys->p_buffer );