1 /*****************************************************************************
2 * flac.c: flac decoder/encoder module making use of libflac
3 *****************************************************************************
4 * Copyright (C) 1999-2001 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
8 * Sigmund Augdal Helberg <dnumgis@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_codec.h>
38 #include <FLAC/stream_decoder.h>
39 #include <FLAC/stream_encoder.h>
41 #include <vlc_block_helper.h>
44 #if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT >= 8
45 # define USE_NEW_FLAC_API
48 /*****************************************************************************
49 * decoder_sys_t : FLAC decoder descriptor
50 *****************************************************************************/
54 * Input/Output properties
57 aout_buffer_t *p_aout_buffer;
62 FLAC__StreamDecoder *p_flac;
63 FLAC__StreamMetadata_StreamInfo stream_info;
67 static const int pi_channels_maps[9] =
71 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
72 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
73 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
74 | AOUT_CHAN_REARRIGHT,
75 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
76 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT,
77 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
78 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_LFE,
79 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
80 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT
81 | AOUT_CHAN_MIDDLERIGHT,
82 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER | AOUT_CHAN_REARLEFT
83 | AOUT_CHAN_REARRIGHT | AOUT_CHAN_MIDDLELEFT | AOUT_CHAN_MIDDLERIGHT
87 /*****************************************************************************
89 *****************************************************************************/
90 static int OpenDecoder ( vlc_object_t * );
91 static void CloseDecoder ( vlc_object_t * );
93 static int OpenEncoder ( vlc_object_t * );
94 static void CloseEncoder ( vlc_object_t * );
96 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
98 static FLAC__StreamDecoderReadStatus
99 DecoderReadCallback( const FLAC__StreamDecoder *decoder,
100 FLAC__byte buffer[], unsigned *bytes, void *client_data );
102 static FLAC__StreamDecoderWriteStatus
103 DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
104 const FLAC__Frame *frame,
105 const FLAC__int32 *const buffer[], void *client_data );
107 static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
108 const FLAC__StreamMetadata *metadata,
110 static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
111 FLAC__StreamDecoderErrorStatus status,
114 static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
115 const int *pi_order, int i_nb_channels, int i_samples );
116 static void Interleave24( int8_t *p_out, const int32_t * const *pp_in,
117 const int *pi_order, int i_nb_channels, int i_samples );
118 static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
119 const int *pi_order, int i_nb_channels, int i_samples );
121 static void decoder_state_error( decoder_t *p_dec,
122 FLAC__StreamDecoderState state );
124 /*****************************************************************************
126 *****************************************************************************/
129 set_category( CAT_INPUT )
130 set_subcategory( SUBCAT_INPUT_ACODEC )
131 add_shortcut( "flac" )
133 set_description( N_("Flac audio decoder") )
134 set_capability( "decoder", 100 )
135 set_callbacks( OpenDecoder, CloseDecoder )
138 add_shortcut( "flac" )
139 set_description( N_("Flac audio encoder") )
140 set_capability( "encoder", 100 )
141 set_callbacks( OpenEncoder, CloseEncoder )
145 /*****************************************************************************
146 * OpenDecoder: probe the decoder and return score
147 *****************************************************************************/
148 static int OpenDecoder( vlc_object_t *p_this )
150 decoder_t *p_dec = (decoder_t*)p_this;
151 decoder_sys_t *p_sys;
153 if( p_dec->fmt_in.i_codec != VLC_CODEC_FLAC )
158 /* Allocate the memory needed to store the decoder's structure */
159 if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
163 p_sys->b_stream_info = false;
164 p_sys->p_block = NULL;
166 /* Take care of flac init */
167 if( !(p_sys->p_flac = FLAC__stream_decoder_new()) )
169 msg_Err( p_dec, "FLAC__stream_decoder_new() failed" );
174 #ifdef USE_NEW_FLAC_API
175 if( FLAC__stream_decoder_init_stream( p_sys->p_flac,
181 DecoderWriteCallback,
182 DecoderMetadataCallback,
183 DecoderErrorCallback,
185 != FLAC__STREAM_DECODER_INIT_STATUS_OK )
187 msg_Err( p_dec, "FLAC__stream_decoder_init_stream() failed" );
188 FLAC__stream_decoder_delete( p_sys->p_flac );
193 FLAC__stream_decoder_set_read_callback( p_sys->p_flac,
194 DecoderReadCallback );
195 FLAC__stream_decoder_set_write_callback( p_sys->p_flac,
196 DecoderWriteCallback );
197 FLAC__stream_decoder_set_metadata_callback( p_sys->p_flac,
198 DecoderMetadataCallback );
199 FLAC__stream_decoder_set_error_callback( p_sys->p_flac,
200 DecoderErrorCallback );
201 FLAC__stream_decoder_set_client_data( p_sys->p_flac, p_dec );
203 FLAC__stream_decoder_init( p_sys->p_flac );
206 /* Set output properties */
207 p_dec->fmt_out.i_cat = AUDIO_ES;
208 p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
211 p_dec->pf_decode_audio = DecodeBlock;
214 p_dec->b_need_packetized = true;
219 /*****************************************************************************
220 * CloseDecoder: flac decoder destruction
221 *****************************************************************************/
222 static void CloseDecoder( vlc_object_t *p_this )
224 decoder_t *p_dec = (decoder_t *)p_this;
225 decoder_sys_t *p_sys = p_dec->p_sys;
227 FLAC__stream_decoder_finish( p_sys->p_flac );
228 FLAC__stream_decoder_delete( p_sys->p_flac );
231 block_Release( p_sys->p_block );
235 /*****************************************************************************
236 * ProcessHeader: process Flac header.
237 *****************************************************************************/
238 static void ProcessHeader( decoder_t *p_dec )
240 decoder_sys_t *p_sys = p_dec->p_sys;
242 if( !p_dec->fmt_in.i_extra )
245 /* Decode STREAMINFO */
246 msg_Dbg( p_dec, "decode STREAMINFO" );
247 p_sys->p_block = block_New( p_dec, p_dec->fmt_in.i_extra );
248 memcpy( p_sys->p_block->p_buffer, p_dec->fmt_in.p_extra,
249 p_dec->fmt_in.i_extra );
250 FLAC__stream_decoder_process_until_end_of_metadata( p_sys->p_flac );
251 msg_Dbg( p_dec, "STREAMINFO decoded" );
254 /****************************************************************************
255 * DecodeBlock: the whole thing
256 ****************************************************************************/
257 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
259 decoder_sys_t *p_sys = p_dec->p_sys;
261 if( !pp_block || !*pp_block )
263 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
265 block_Release( *pp_block );
269 if( !p_sys->b_stream_info )
270 ProcessHeader( p_dec );
272 p_sys->p_block = *pp_block;
275 p_sys->p_aout_buffer = 0;
277 if( !FLAC__stream_decoder_process_single( p_sys->p_flac ) )
279 decoder_state_error( p_dec,
280 FLAC__stream_decoder_get_state( p_sys->p_flac ) );
281 FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
284 /* If the decoder is in the "aborted" state,
285 * FLAC__stream_decoder_process_single() won't return an error. */
286 if( FLAC__stream_decoder_get_state(p_dec->p_sys->p_flac)
287 == FLAC__STREAM_DECODER_ABORTED )
289 FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
292 block_Release( p_sys->p_block );
293 p_sys->p_block = NULL;
295 return p_sys->p_aout_buffer;
298 /*****************************************************************************
299 * DecoderReadCallback: called by libflac when it needs more data
300 *****************************************************************************/
301 static FLAC__StreamDecoderReadStatus
302 DecoderReadCallback( const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
303 unsigned *bytes, void *client_data )
306 decoder_t *p_dec = (decoder_t *)client_data;
307 decoder_sys_t *p_sys = p_dec->p_sys;
309 if( p_sys->p_block && p_sys->p_block->i_buffer )
311 *bytes = __MIN(*bytes, (unsigned)p_sys->p_block->i_buffer);
312 memcpy( buffer, p_sys->p_block->p_buffer, *bytes );
313 p_sys->p_block->i_buffer -= *bytes;
314 p_sys->p_block->p_buffer += *bytes;
319 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
322 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
325 /*****************************************************************************
326 * DecoderWriteCallback: called by libflac to output decoded samples
327 *****************************************************************************/
328 static FLAC__StreamDecoderWriteStatus
329 DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
330 const FLAC__Frame *frame,
331 const FLAC__int32 *const buffer[], void *client_data )
333 /* XXX it supposes our internal format is WG4 */
334 static const int ppi_reorder[1+8][8] = {
341 { 0, 1, 4, 5, 2, 3 },
343 { 0, 1, 6, 4, 5, 2, 3 }, /* 7.0 Unspecified by flac, but following SMPTE */
344 { 0, 1, 6, 7, 4, 5, 2, 3 }, /* 7.1 Unspecified by flac, but following SMPTE */
348 decoder_t *p_dec = (decoder_t *)client_data;
349 decoder_sys_t *p_sys = p_dec->p_sys;
351 if( p_dec->fmt_out.audio.i_channels <= 0 ||
352 p_dec->fmt_out.audio.i_channels > 8 )
353 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
354 const int * const pi_reorder = ppi_reorder[p_dec->fmt_out.audio.i_channels];
356 p_sys->p_aout_buffer =
357 decoder_NewAudioBuffer( p_dec, frame->header.blocksize );
359 if( p_sys->p_aout_buffer == NULL )
360 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
362 switch( frame->header.bits_per_sample )
365 Interleave16( (int16_t *)p_sys->p_aout_buffer->p_buffer, buffer, pi_reorder,
366 frame->header.channels, frame->header.blocksize );
369 Interleave24( (int8_t *)p_sys->p_aout_buffer->p_buffer, buffer, pi_reorder,
370 frame->header.channels, frame->header.blocksize );
373 Interleave32( (int32_t *)p_sys->p_aout_buffer->p_buffer, buffer, pi_reorder,
374 frame->header.channels, frame->header.blocksize );
377 /* Date management (already done by packetizer) */
378 p_sys->p_aout_buffer->i_pts = p_sys->p_block->i_pts;
379 p_sys->p_aout_buffer->i_length = p_sys->p_block->i_length;
381 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
384 /*****************************************************************************
385 * DecoderMetadataCallback: called by libflac to when it encounters metadata
386 *****************************************************************************/
387 static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
388 const FLAC__StreamMetadata *metadata,
392 decoder_t *p_dec = (decoder_t *)client_data;
393 decoder_sys_t *p_sys = p_dec->p_sys;
395 if( p_dec->pf_decode_audio )
397 switch( metadata->data.stream_info.bits_per_sample )
400 p_dec->fmt_out.i_codec = VLC_CODEC_S8;
403 p_dec->fmt_out.i_codec = VLC_CODEC_S16N;
406 p_dec->fmt_out.i_codec = VLC_CODEC_S24N;
409 msg_Dbg( p_dec, "strange bit/sample value: %d",
410 metadata->data.stream_info.bits_per_sample );
411 p_dec->fmt_out.i_codec = VLC_CODEC_FI32;
416 /* Setup the format */
417 p_dec->fmt_out.audio.i_rate = metadata->data.stream_info.sample_rate;
418 p_dec->fmt_out.audio.i_channels = metadata->data.stream_info.channels;
419 p_dec->fmt_out.audio.i_physical_channels =
420 p_dec->fmt_out.audio.i_original_channels =
421 pi_channels_maps[metadata->data.stream_info.channels];
422 p_dec->fmt_out.audio.i_bitspersample =
423 metadata->data.stream_info.bits_per_sample;
425 msg_Dbg( p_dec, "channels:%d samplerate:%d bitspersamples:%d",
426 p_dec->fmt_out.audio.i_channels, p_dec->fmt_out.audio.i_rate,
427 p_dec->fmt_out.audio.i_bitspersample );
429 p_sys->b_stream_info = true;
430 p_sys->stream_info = metadata->data.stream_info;
435 /*****************************************************************************
436 * DecoderErrorCallback: called when the libflac decoder encounters an error
437 *****************************************************************************/
438 static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
439 FLAC__StreamDecoderErrorStatus status,
443 decoder_t *p_dec = (decoder_t *)client_data;
447 case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
448 msg_Warn( p_dec, "an error in the stream caused the decoder to "
449 "lose synchronization." );
451 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
452 msg_Err( p_dec, "the decoder encountered a corrupted frame header." );
454 case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
455 msg_Err( p_dec, "frame's data did not match the CRC in the "
459 msg_Err( p_dec, "got decoder error: %d", status );
462 FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
466 /*****************************************************************************
467 * Interleave: helper function to interleave channels
468 *****************************************************************************/
469 static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
470 const int pi_index[],
471 int i_nb_channels, int i_samples )
474 for ( j = 0; j < i_samples; j++ )
476 for ( i = 0; i < i_nb_channels; i++ )
478 p_out[j * i_nb_channels + i] = pp_in[pi_index[i]][j];
483 static void Interleave24( int8_t *p_out, const int32_t * const *pp_in,
484 const int pi_index[],
485 int i_nb_channels, int i_samples )
488 for ( j = 0; j < i_samples; j++ )
490 for ( i = 0; i < i_nb_channels; i++ )
492 const int i_index = pi_index[i];
493 #ifdef WORDS_BIGENDIAN
494 p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 16) & 0xff;
495 p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
496 p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 0 ) & 0xff;
498 p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 16) & 0xff;
499 p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
500 p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 0 ) & 0xff;
506 static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
507 const int pi_index[],
508 int i_nb_channels, int i_samples )
511 for ( j = 0; j < i_samples; j++ )
513 for ( i = 0; i < i_nb_channels; i++ )
515 p_out[j * i_nb_channels + i] = (int32_t)(pp_in[pi_index[i]][j]);
520 /*****************************************************************************
521 * decoder_state_error: print meaningful error messages
522 *****************************************************************************/
523 static void decoder_state_error( decoder_t *p_dec,
524 FLAC__StreamDecoderState state )
528 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
529 msg_Dbg( p_dec, "the decoder is ready to search for metadata." );
531 case FLAC__STREAM_DECODER_READ_METADATA:
532 msg_Dbg( p_dec, "the decoder is ready to or is in the process of "
533 "reading metadata." );
535 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
536 msg_Dbg( p_dec, "the decoder is ready to or is in the process of "
537 "searching for the frame sync code." );
539 case FLAC__STREAM_DECODER_READ_FRAME:
540 msg_Dbg( p_dec, "the decoder is ready to or is in the process of "
541 "reading a frame." );
543 case FLAC__STREAM_DECODER_END_OF_STREAM:
544 msg_Dbg( p_dec, "the decoder has reached the end of the stream." );
546 #ifdef USE_NEW_FLAC_API
547 case FLAC__STREAM_DECODER_OGG_ERROR:
548 msg_Err( p_dec, "error occurred in the Ogg layer." );
550 case FLAC__STREAM_DECODER_SEEK_ERROR:
551 msg_Err( p_dec, "error occurred while seeking." );
554 case FLAC__STREAM_DECODER_ABORTED:
555 msg_Warn( p_dec, "the decoder was aborted by the read callback." );
557 #ifndef USE_NEW_FLAC_API
558 case FLAC__STREAM_DECODER_UNPARSEABLE_STREAM:
559 msg_Warn( p_dec, "the decoder encountered reserved fields in use "
563 case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
564 msg_Err( p_dec, "error when allocating memory." );
566 #ifndef USE_NEW_FLAC_API
567 case FLAC__STREAM_DECODER_ALREADY_INITIALIZED:
568 msg_Err( p_dec, "FLAC__stream_decoder_init() was called when the "
569 "decoder was already initialized, usually because "
570 "FLAC__stream_decoder_finish() was not called." );
572 case FLAC__STREAM_DECODER_INVALID_CALLBACK:
573 msg_Err( p_dec, "FLAC__stream_decoder_init() was called without "
574 "all callbacks being set." );
577 case FLAC__STREAM_DECODER_UNINITIALIZED:
578 msg_Err( p_dec, "decoder in uninitialized state." );
581 msg_Warn(p_dec, "unknown error" );
585 /*****************************************************************************
586 * encoder_sys_t : flac encoder descriptor
587 *****************************************************************************/
598 FLAC__int32 *p_buffer;
599 unsigned int i_buffer;
606 FLAC__StreamEncoder *p_flac;
607 FLAC__StreamMetadata_StreamInfo stream_info;
615 #define STREAMINFO_SIZE 38
617 static block_t *Encode( encoder_t *, aout_buffer_t * );
619 static FLAC__StreamEncoderWriteStatus
620 EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
621 const FLAC__byte buffer[],
622 unsigned bytes, unsigned samples,
623 unsigned current_frame, void *client_data );
625 static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
626 const FLAC__StreamMetadata *metadata,
629 /*****************************************************************************
630 * OpenEncoder: probe the encoder and return score
631 *****************************************************************************/
632 static int OpenEncoder( vlc_object_t *p_this )
634 encoder_t *p_enc = (encoder_t *)p_this;
635 encoder_sys_t *p_sys;
637 if( p_enc->fmt_out.i_codec != VLC_CODEC_FLAC &&
643 /* Allocate the memory needed to store the decoder's structure */
644 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
646 p_enc->p_sys = p_sys;
647 p_enc->pf_encode_audio = Encode;
648 p_enc->fmt_out.i_codec = VLC_CODEC_FLAC;
650 p_sys->i_headers = 0;
653 p_sys->i_samples_delay = 0;
655 /* Create flac encoder */
656 if( !(p_sys->p_flac = FLAC__stream_encoder_new()) )
658 msg_Err( p_enc, "FLAC__stream_encoder_new() failed" );
663 FLAC__stream_encoder_set_streamable_subset( p_sys->p_flac, 1 );
664 FLAC__stream_encoder_set_channels( p_sys->p_flac,
665 p_enc->fmt_in.audio.i_channels );
666 FLAC__stream_encoder_set_sample_rate( p_sys->p_flac,
667 p_enc->fmt_in.audio.i_rate );
668 FLAC__stream_encoder_set_bits_per_sample( p_sys->p_flac, 16 );
669 p_enc->fmt_in.i_codec = VLC_CODEC_S16N;
671 /* Get and store the STREAMINFO metadata block as a p_extra */
674 #ifdef USE_NEW_FLAC_API
675 if( FLAC__stream_encoder_init_stream( p_sys->p_flac,
676 EncoderWriteCallback,
679 EncoderMetadataCallback,
681 != FLAC__STREAM_ENCODER_INIT_STATUS_OK )
683 msg_Err( p_enc, "FLAC__stream_encoder_init_stream() failed" );
684 FLAC__stream_encoder_delete( p_sys->p_flac );
689 FLAC__stream_encoder_set_write_callback( p_sys->p_flac,
690 EncoderWriteCallback );
691 FLAC__stream_encoder_set_metadata_callback( p_sys->p_flac,
692 EncoderMetadataCallback );
693 FLAC__stream_encoder_set_client_data( p_sys->p_flac, p_enc );
695 FLAC__stream_encoder_init( p_sys->p_flac );
701 /****************************************************************************
702 * Encode: the whole thing
703 ****************************************************************************
704 * This function spits out ogg packets.
705 ****************************************************************************/
706 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
708 encoder_sys_t *p_sys = p_enc->p_sys;
712 p_sys->i_pts = p_aout_buf->i_pts -
713 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
714 (mtime_t)p_enc->fmt_in.audio.i_rate;
716 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
718 /* Convert samples to FLAC__int32 */
719 if( p_sys->i_buffer < p_aout_buf->i_buffer * 2 )
722 xrealloc( p_sys->p_buffer, p_aout_buf->i_buffer * 2 );
723 p_sys->i_buffer = p_aout_buf->i_buffer * 2;
726 for( i = 0 ; i < p_aout_buf->i_buffer / 2 ; i++ )
728 p_sys->p_buffer[i]= ((int16_t *)p_aout_buf->p_buffer)[i];
731 FLAC__stream_encoder_process_interleaved( p_sys->p_flac, p_sys->p_buffer,
732 p_aout_buf->i_nb_samples );
734 p_chain = p_sys->p_chain;
740 /*****************************************************************************
741 * CloseEncoder: encoder destruction
742 *****************************************************************************/
743 static void CloseEncoder( vlc_object_t *p_this )
745 encoder_t *p_enc = (encoder_t *)p_this;
746 encoder_sys_t *p_sys = p_enc->p_sys;
748 FLAC__stream_encoder_delete( p_sys->p_flac );
750 free( p_sys->p_buffer );
754 /*****************************************************************************
755 * EncoderMetadataCallback: called by libflac to output metadata
756 *****************************************************************************/
757 static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
758 const FLAC__StreamMetadata *metadata,
762 encoder_t *p_enc = (encoder_t *)client_data;
764 msg_Err( p_enc, "MetadataCallback: %i", metadata->type );
768 /*****************************************************************************
769 * EncoderWriteCallback: called by libflac to output encoded samples
770 *****************************************************************************/
771 static FLAC__StreamEncoderWriteStatus
772 EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
773 const FLAC__byte buffer[],
774 unsigned bytes, unsigned samples,
775 unsigned current_frame, void *client_data )
777 VLC_UNUSED(encoder); VLC_UNUSED(current_frame);
778 encoder_t *p_enc = (encoder_t *)client_data;
779 encoder_sys_t *p_sys = p_enc->p_sys;
784 if( p_sys->i_headers == 1 )
786 msg_Dbg( p_enc, "Writing STREAMINFO: %i", bytes );
788 /* Backup the STREAMINFO metadata block */
789 p_enc->fmt_out.i_extra = STREAMINFO_SIZE + 4;
790 p_enc->fmt_out.p_extra = xmalloc( STREAMINFO_SIZE + 4 );
791 memcpy( p_enc->fmt_out.p_extra, "fLaC", 4 );
792 memcpy( ((uint8_t *)p_enc->fmt_out.p_extra) + 4, buffer,
795 /* Fake this as the last metadata block */
796 ((uint8_t*)p_enc->fmt_out.p_extra)[4] |= 0x80;
799 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
802 p_block = block_New( p_enc, bytes );
803 memcpy( p_block->p_buffer, buffer, bytes );
805 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
807 p_sys->i_samples_delay -= samples;
809 p_block->i_length = (mtime_t)1000000 *
810 (mtime_t)samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
813 p_sys->i_pts += p_block->i_length;
815 block_ChainAppend( &p_sys->p_chain, p_block );
817 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;