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 *****************************************************************************/
29 /* workaround libflac overriding assert.h system header */
30 #define assert(x) do {} while(0)
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_codec.h>
41 #include <stream_decoder.h>
42 #include <stream_encoder.h>
44 #include <vlc_block_helper.h>
47 #if defined(FLAC_API_VERSION_CURRENT) && FLAC_API_VERSION_CURRENT >= 8
48 # define USE_NEW_FLAC_API
51 /*****************************************************************************
52 * decoder_sys_t : FLAC decoder descriptor
53 *****************************************************************************/
57 * Input/Output properties
60 aout_buffer_t
*p_aout_buffer
;
66 FLAC__StreamDecoder
*p_flac
;
67 FLAC__StreamMetadata_StreamInfo stream_info
;
71 static const int pi_channels_maps
[9] =
75 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
,
76 AOUT_CHAN_CENTER
| AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
,
77 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_REARLEFT
78 | AOUT_CHAN_REARRIGHT
,
79 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
80 | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
,
81 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
82 | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_LFE
,
83 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
84 | AOUT_CHAN_REARLEFT
| AOUT_CHAN_REARRIGHT
| AOUT_CHAN_MIDDLELEFT
85 | AOUT_CHAN_MIDDLERIGHT
,
86 AOUT_CHAN_LEFT
| AOUT_CHAN_RIGHT
| AOUT_CHAN_CENTER
| AOUT_CHAN_REARLEFT
87 | AOUT_CHAN_REARRIGHT
| AOUT_CHAN_MIDDLELEFT
| AOUT_CHAN_MIDDLERIGHT
91 /*****************************************************************************
93 *****************************************************************************/
94 static int OpenDecoder ( vlc_object_t
* );
95 static void CloseDecoder ( vlc_object_t
* );
97 static int OpenEncoder ( vlc_object_t
* );
98 static void CloseEncoder ( vlc_object_t
* );
100 static aout_buffer_t
*DecodeBlock( decoder_t
*, block_t
** );
102 static FLAC__StreamDecoderReadStatus
103 DecoderReadCallback( const FLAC__StreamDecoder
*decoder
,
104 FLAC__byte buffer
[], unsigned *bytes
, void *client_data
);
106 static FLAC__StreamDecoderWriteStatus
107 DecoderWriteCallback( const FLAC__StreamDecoder
*decoder
,
108 const FLAC__Frame
*frame
,
109 const FLAC__int32
*const buffer
[], void *client_data
);
111 static void DecoderMetadataCallback( const FLAC__StreamDecoder
*decoder
,
112 const FLAC__StreamMetadata
*metadata
,
114 static void DecoderErrorCallback( const FLAC__StreamDecoder
*decoder
,
115 FLAC__StreamDecoderErrorStatus status
,
118 static void Interleave32( int32_t *p_out
, const int32_t * const *pp_in
,
119 const int *pi_order
, int i_nb_channels
, int i_samples
);
120 static void Interleave24( int8_t *p_out
, const int32_t * const *pp_in
,
121 const int *pi_order
, int i_nb_channels
, int i_samples
);
122 static void Interleave16( int16_t *p_out
, const int32_t * const *pp_in
,
123 const int *pi_order
, int i_nb_channels
, int i_samples
);
125 static void decoder_state_error( decoder_t
*p_dec
,
126 FLAC__StreamDecoderState state
);
128 /*****************************************************************************
130 *****************************************************************************/
133 set_category( CAT_INPUT
)
134 set_subcategory( SUBCAT_INPUT_ACODEC
)
135 add_shortcut( "flac" )
137 set_description( N_("Flac audio decoder") )
138 set_capability( "decoder", 100 )
139 set_callbacks( OpenDecoder
, CloseDecoder
)
142 add_shortcut( "flac" )
143 set_description( N_("Flac audio encoder") )
144 set_capability( "encoder", 100 )
145 set_callbacks( OpenEncoder
, CloseEncoder
)
149 /*****************************************************************************
150 * OpenDecoder: probe the decoder and return score
151 *****************************************************************************/
152 static int OpenDecoder( vlc_object_t
*p_this
)
154 decoder_t
*p_dec
= (decoder_t
*)p_this
;
155 decoder_sys_t
*p_sys
;
157 if( p_dec
->fmt_in
.i_codec
!= VLC_CODEC_FLAC
)
162 /* Allocate the memory needed to store the decoder's structure */
163 if( ( p_dec
->p_sys
= p_sys
= malloc(sizeof(*p_sys
)) ) == NULL
)
167 p_sys
->b_stream_info
= false;
168 p_sys
->p_block
= NULL
;
170 /* Take care of flac init */
171 if( !(p_sys
->p_flac
= FLAC__stream_decoder_new()) )
173 msg_Err( p_dec
, "FLAC__stream_decoder_new() failed" );
178 #ifdef USE_NEW_FLAC_API
179 if( FLAC__stream_decoder_init_stream( p_sys
->p_flac
,
185 DecoderWriteCallback
,
186 DecoderMetadataCallback
,
187 DecoderErrorCallback
,
189 != FLAC__STREAM_DECODER_INIT_STATUS_OK
)
191 msg_Err( p_dec
, "FLAC__stream_decoder_init_stream() failed" );
192 FLAC__stream_decoder_delete( p_sys
->p_flac
);
197 FLAC__stream_decoder_set_read_callback( p_sys
->p_flac
,
198 DecoderReadCallback
);
199 FLAC__stream_decoder_set_write_callback( p_sys
->p_flac
,
200 DecoderWriteCallback
);
201 FLAC__stream_decoder_set_metadata_callback( p_sys
->p_flac
,
202 DecoderMetadataCallback
);
203 FLAC__stream_decoder_set_error_callback( p_sys
->p_flac
,
204 DecoderErrorCallback
);
205 FLAC__stream_decoder_set_client_data( p_sys
->p_flac
, p_dec
);
207 FLAC__stream_decoder_init( p_sys
->p_flac
);
210 /* Set output properties */
211 p_dec
->fmt_out
.i_cat
= AUDIO_ES
;
212 p_dec
->fmt_out
.i_codec
= VLC_CODEC_FL32
;
215 p_dec
->pf_decode_audio
= DecodeBlock
;
218 p_dec
->b_need_packetized
= true;
223 /*****************************************************************************
224 * CloseDecoder: flac decoder destruction
225 *****************************************************************************/
226 static void CloseDecoder( vlc_object_t
*p_this
)
228 decoder_t
*p_dec
= (decoder_t
*)p_this
;
229 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
231 FLAC__stream_decoder_finish( p_sys
->p_flac
);
232 FLAC__stream_decoder_delete( p_sys
->p_flac
);
235 block_Release( p_sys
->p_block
);
239 /*****************************************************************************
240 * ProcessHeader: process Flac header.
241 *****************************************************************************/
242 static void ProcessHeader( decoder_t
*p_dec
)
244 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
246 if( !p_dec
->fmt_in
.i_extra
)
249 /* Decode STREAMINFO */
250 msg_Dbg( p_dec
, "decode STREAMINFO" );
251 p_sys
->p_block
= block_New( p_dec
, p_dec
->fmt_in
.i_extra
);
252 memcpy( p_sys
->p_block
->p_buffer
, p_dec
->fmt_in
.p_extra
,
253 p_dec
->fmt_in
.i_extra
);
254 FLAC__stream_decoder_process_until_end_of_metadata( p_sys
->p_flac
);
255 msg_Dbg( p_dec
, "STREAMINFO decoded" );
258 /****************************************************************************
259 * DecodeBlock: the whole thing
260 ****************************************************************************/
261 static aout_buffer_t
*DecodeBlock( decoder_t
*p_dec
, block_t
**pp_block
)
263 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
265 if( !pp_block
|| !*pp_block
)
267 if( (*pp_block
)->i_flags
&(BLOCK_FLAG_DISCONTINUITY
|BLOCK_FLAG_CORRUPTED
) )
269 block_Release( *pp_block
);
273 if( !p_sys
->b_stream_info
)
274 ProcessHeader( p_dec
);
276 p_sys
->p_block
= *pp_block
;
279 if( p_sys
->p_block
->i_pts
> VLC_TS_INVALID
&&
280 p_sys
->p_block
->i_pts
!= date_Get( &p_sys
->end_date
) )
281 date_Set( &p_sys
->end_date
, p_sys
->p_block
->i_pts
);
283 p_sys
->p_aout_buffer
= 0;
285 if( !FLAC__stream_decoder_process_single( p_sys
->p_flac
) )
287 decoder_state_error( p_dec
,
288 FLAC__stream_decoder_get_state( p_sys
->p_flac
) );
289 FLAC__stream_decoder_flush( p_dec
->p_sys
->p_flac
);
292 /* If the decoder is in the "aborted" state,
293 * FLAC__stream_decoder_process_single() won't return an error. */
294 if( FLAC__stream_decoder_get_state(p_dec
->p_sys
->p_flac
)
295 == FLAC__STREAM_DECODER_ABORTED
)
297 FLAC__stream_decoder_flush( p_dec
->p_sys
->p_flac
);
300 block_Release( p_sys
->p_block
);
301 p_sys
->p_block
= NULL
;
303 return p_sys
->p_aout_buffer
;
306 /*****************************************************************************
307 * DecoderReadCallback: called by libflac when it needs more data
308 *****************************************************************************/
309 static FLAC__StreamDecoderReadStatus
310 DecoderReadCallback( const FLAC__StreamDecoder
*decoder
, FLAC__byte buffer
[],
311 unsigned *bytes
, void *client_data
)
314 decoder_t
*p_dec
= (decoder_t
*)client_data
;
315 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
317 if( p_sys
->p_block
&& p_sys
->p_block
->i_buffer
)
319 *bytes
= __MIN(*bytes
, (unsigned)p_sys
->p_block
->i_buffer
);
320 memcpy( buffer
, p_sys
->p_block
->p_buffer
, *bytes
);
321 p_sys
->p_block
->i_buffer
-= *bytes
;
322 p_sys
->p_block
->p_buffer
+= *bytes
;
327 return FLAC__STREAM_DECODER_READ_STATUS_ABORT
;
330 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE
;
333 /*****************************************************************************
334 * DecoderWriteCallback: called by libflac to output decoded samples
335 *****************************************************************************/
336 static FLAC__StreamDecoderWriteStatus
337 DecoderWriteCallback( const FLAC__StreamDecoder
*decoder
,
338 const FLAC__Frame
*frame
,
339 const FLAC__int32
*const buffer
[], void *client_data
)
341 /* XXX it supposes our internal format is WG4 */
342 static const int ppi_reorder
[1+8][8] = {
349 { 0, 1, 4, 5, 2, 3 },
351 { 0, 1, 6, 4, 5, 2, 3 }, /* 7.0 Unspecified by flac, but following SMPTE */
352 { 0, 1, 6, 7, 4, 5, 2, 3 }, /* 7.1 Unspecified by flac, but following SMPTE */
356 decoder_t
*p_dec
= (decoder_t
*)client_data
;
357 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
359 if( p_dec
->fmt_out
.audio
.i_channels
<= 0 ||
360 p_dec
->fmt_out
.audio
.i_channels
> 8 )
361 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
362 if( date_Get( &p_sys
->end_date
) <= VLC_TS_INVALID
)
363 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
365 const int * const pi_reorder
= ppi_reorder
[p_dec
->fmt_out
.audio
.i_channels
];
367 p_sys
->p_aout_buffer
=
368 decoder_NewAudioBuffer( p_dec
, frame
->header
.blocksize
);
370 if( p_sys
->p_aout_buffer
== NULL
)
371 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
373 switch( frame
->header
.bits_per_sample
)
376 Interleave16( (int16_t *)p_sys
->p_aout_buffer
->p_buffer
, buffer
, pi_reorder
,
377 frame
->header
.channels
, frame
->header
.blocksize
);
380 Interleave24( (int8_t *)p_sys
->p_aout_buffer
->p_buffer
, buffer
, pi_reorder
,
381 frame
->header
.channels
, frame
->header
.blocksize
);
384 Interleave32( (int32_t *)p_sys
->p_aout_buffer
->p_buffer
, buffer
, pi_reorder
,
385 frame
->header
.channels
, frame
->header
.blocksize
);
388 /* Date management (already done by packetizer) */
389 p_sys
->p_aout_buffer
->i_pts
= date_Get( &p_sys
->end_date
);
390 p_sys
->p_aout_buffer
->i_length
=
391 date_Increment( &p_sys
->end_date
, frame
->header
.blocksize
) -
392 p_sys
->p_aout_buffer
->i_pts
;
394 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
397 /*****************************************************************************
398 * DecoderMetadataCallback: called by libflac to when it encounters metadata
399 *****************************************************************************/
400 static void DecoderMetadataCallback( const FLAC__StreamDecoder
*decoder
,
401 const FLAC__StreamMetadata
*metadata
,
405 decoder_t
*p_dec
= (decoder_t
*)client_data
;
406 decoder_sys_t
*p_sys
= p_dec
->p_sys
;
408 if( p_dec
->pf_decode_audio
)
410 switch( metadata
->data
.stream_info
.bits_per_sample
)
413 p_dec
->fmt_out
.i_codec
= VLC_CODEC_S8
;
416 p_dec
->fmt_out
.i_codec
= VLC_CODEC_S16N
;
419 p_dec
->fmt_out
.i_codec
= VLC_CODEC_S24N
;
422 msg_Dbg( p_dec
, "strange bit/sample value: %d",
423 metadata
->data
.stream_info
.bits_per_sample
);
424 p_dec
->fmt_out
.i_codec
= VLC_CODEC_FI32
;
429 /* Setup the format */
430 p_dec
->fmt_out
.audio
.i_rate
= metadata
->data
.stream_info
.sample_rate
;
431 p_dec
->fmt_out
.audio
.i_channels
= metadata
->data
.stream_info
.channels
;
432 p_dec
->fmt_out
.audio
.i_physical_channels
=
433 p_dec
->fmt_out
.audio
.i_original_channels
=
434 pi_channels_maps
[metadata
->data
.stream_info
.channels
];
435 p_dec
->fmt_out
.audio
.i_bitspersample
=
436 metadata
->data
.stream_info
.bits_per_sample
;
438 msg_Dbg( p_dec
, "channels:%d samplerate:%d bitspersamples:%d",
439 p_dec
->fmt_out
.audio
.i_channels
, p_dec
->fmt_out
.audio
.i_rate
,
440 p_dec
->fmt_out
.audio
.i_bitspersample
);
442 p_sys
->b_stream_info
= true;
443 p_sys
->stream_info
= metadata
->data
.stream_info
;
445 date_Init( &p_sys
->end_date
, p_dec
->fmt_out
.audio
.i_rate
, 1 );
446 date_Set( &p_sys
->end_date
, VLC_TS_INVALID
);
449 /*****************************************************************************
450 * DecoderErrorCallback: called when the libflac decoder encounters an error
451 *****************************************************************************/
452 static void DecoderErrorCallback( const FLAC__StreamDecoder
*decoder
,
453 FLAC__StreamDecoderErrorStatus status
,
457 decoder_t
*p_dec
= (decoder_t
*)client_data
;
461 case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC
:
462 msg_Warn( p_dec
, "an error in the stream caused the decoder to "
463 "lose synchronization." );
465 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER
:
466 msg_Err( p_dec
, "the decoder encountered a corrupted frame header." );
468 case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH
:
469 msg_Err( p_dec
, "frame's data did not match the CRC in the "
473 msg_Err( p_dec
, "got decoder error: %d", status
);
476 FLAC__stream_decoder_flush( p_dec
->p_sys
->p_flac
);
480 /*****************************************************************************
481 * Interleave: helper function to interleave channels
482 *****************************************************************************/
483 static void Interleave32( int32_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 p_out
[j
* i_nb_channels
+ i
] = pp_in
[pi_index
[i
]][j
];
497 static void Interleave24( int8_t *p_out
, const int32_t * const *pp_in
,
498 const int pi_index
[],
499 int i_nb_channels
, int i_samples
)
502 for ( j
= 0; j
< i_samples
; j
++ )
504 for ( i
= 0; i
< i_nb_channels
; i
++ )
506 const int i_index
= pi_index
[i
];
507 #ifdef WORDS_BIGENDIAN
508 p_out
[3*(j
* i_nb_channels
+ i
)+0] = (pp_in
[i_index
][j
] >> 16) & 0xff;
509 p_out
[3*(j
* i_nb_channels
+ i
)+1] = (pp_in
[i_index
][j
] >> 8 ) & 0xff;
510 p_out
[3*(j
* i_nb_channels
+ i
)+2] = (pp_in
[i_index
][j
] >> 0 ) & 0xff;
512 p_out
[3*(j
* i_nb_channels
+ i
)+2] = (pp_in
[i_index
][j
] >> 16) & 0xff;
513 p_out
[3*(j
* i_nb_channels
+ i
)+1] = (pp_in
[i_index
][j
] >> 8 ) & 0xff;
514 p_out
[3*(j
* i_nb_channels
+ i
)+0] = (pp_in
[i_index
][j
] >> 0 ) & 0xff;
520 static void Interleave16( int16_t *p_out
, const int32_t * const *pp_in
,
521 const int pi_index
[],
522 int i_nb_channels
, int i_samples
)
525 for ( j
= 0; j
< i_samples
; j
++ )
527 for ( i
= 0; i
< i_nb_channels
; i
++ )
529 p_out
[j
* i_nb_channels
+ i
] = (int32_t)(pp_in
[pi_index
[i
]][j
]);
534 /*****************************************************************************
535 * decoder_state_error: print meaningful error messages
536 *****************************************************************************/
537 static void decoder_state_error( decoder_t
*p_dec
,
538 FLAC__StreamDecoderState state
)
542 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA
:
543 msg_Dbg( p_dec
, "the decoder is ready to search for metadata." );
545 case FLAC__STREAM_DECODER_READ_METADATA
:
546 msg_Dbg( p_dec
, "the decoder is ready to or is in the process of "
547 "reading metadata." );
549 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC
:
550 msg_Dbg( p_dec
, "the decoder is ready to or is in the process of "
551 "searching for the frame sync code." );
553 case FLAC__STREAM_DECODER_READ_FRAME
:
554 msg_Dbg( p_dec
, "the decoder is ready to or is in the process of "
555 "reading a frame." );
557 case FLAC__STREAM_DECODER_END_OF_STREAM
:
558 msg_Dbg( p_dec
, "the decoder has reached the end of the stream." );
560 #ifdef USE_NEW_FLAC_API
561 case FLAC__STREAM_DECODER_OGG_ERROR
:
562 msg_Err( p_dec
, "error occurred in the Ogg layer." );
564 case FLAC__STREAM_DECODER_SEEK_ERROR
:
565 msg_Err( p_dec
, "error occurred while seeking." );
568 case FLAC__STREAM_DECODER_ABORTED
:
569 msg_Warn( p_dec
, "the decoder was aborted by the read callback." );
571 #ifndef USE_NEW_FLAC_API
572 case FLAC__STREAM_DECODER_UNPARSEABLE_STREAM
:
573 msg_Warn( p_dec
, "the decoder encountered reserved fields in use "
577 case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR
:
578 msg_Err( p_dec
, "error when allocating memory." );
580 #ifndef USE_NEW_FLAC_API
581 case FLAC__STREAM_DECODER_ALREADY_INITIALIZED
:
582 msg_Err( p_dec
, "FLAC__stream_decoder_init() was called when the "
583 "decoder was already initialized, usually because "
584 "FLAC__stream_decoder_finish() was not called." );
586 case FLAC__STREAM_DECODER_INVALID_CALLBACK
:
587 msg_Err( p_dec
, "FLAC__stream_decoder_init() was called without "
588 "all callbacks being set." );
591 case FLAC__STREAM_DECODER_UNINITIALIZED
:
592 msg_Err( p_dec
, "decoder in uninitialized state." );
595 msg_Warn(p_dec
, "unknown error" );
599 /*****************************************************************************
600 * encoder_sys_t : flac encoder descriptor
601 *****************************************************************************/
612 FLAC__int32
*p_buffer
;
613 unsigned int i_buffer
;
620 FLAC__StreamEncoder
*p_flac
;
621 FLAC__StreamMetadata_StreamInfo stream_info
;
629 #define STREAMINFO_SIZE 38
631 static block_t
*Encode( encoder_t
*, aout_buffer_t
* );
633 static FLAC__StreamEncoderWriteStatus
634 EncoderWriteCallback( const FLAC__StreamEncoder
*encoder
,
635 const FLAC__byte buffer
[],
636 unsigned bytes
, unsigned samples
,
637 unsigned current_frame
, void *client_data
);
639 static void EncoderMetadataCallback( const FLAC__StreamEncoder
*encoder
,
640 const FLAC__StreamMetadata
*metadata
,
643 /*****************************************************************************
644 * OpenEncoder: probe the encoder and return score
645 *****************************************************************************/
646 static int OpenEncoder( vlc_object_t
*p_this
)
648 encoder_t
*p_enc
= (encoder_t
*)p_this
;
649 encoder_sys_t
*p_sys
;
651 if( p_enc
->fmt_out
.i_codec
!= VLC_CODEC_FLAC
&&
657 /* Allocate the memory needed to store the decoder's structure */
658 if( ( p_sys
= (encoder_sys_t
*)malloc(sizeof(encoder_sys_t
)) ) == NULL
)
660 p_enc
->p_sys
= p_sys
;
661 p_enc
->pf_encode_audio
= Encode
;
662 p_enc
->fmt_out
.i_codec
= VLC_CODEC_FLAC
;
664 p_sys
->i_headers
= 0;
667 p_sys
->i_samples_delay
= 0;
669 /* Create flac encoder */
670 if( !(p_sys
->p_flac
= FLAC__stream_encoder_new()) )
672 msg_Err( p_enc
, "FLAC__stream_encoder_new() failed" );
677 FLAC__stream_encoder_set_streamable_subset( p_sys
->p_flac
, 1 );
678 FLAC__stream_encoder_set_channels( p_sys
->p_flac
,
679 p_enc
->fmt_in
.audio
.i_channels
);
680 FLAC__stream_encoder_set_sample_rate( p_sys
->p_flac
,
681 p_enc
->fmt_in
.audio
.i_rate
);
682 FLAC__stream_encoder_set_bits_per_sample( p_sys
->p_flac
, 16 );
683 p_enc
->fmt_in
.i_codec
= VLC_CODEC_S16N
;
685 /* Get and store the STREAMINFO metadata block as a p_extra */
688 #ifdef USE_NEW_FLAC_API
689 if( FLAC__stream_encoder_init_stream( p_sys
->p_flac
,
690 EncoderWriteCallback
,
693 EncoderMetadataCallback
,
695 != FLAC__STREAM_ENCODER_INIT_STATUS_OK
)
697 msg_Err( p_enc
, "FLAC__stream_encoder_init_stream() failed" );
698 FLAC__stream_encoder_delete( p_sys
->p_flac
);
703 FLAC__stream_encoder_set_write_callback( p_sys
->p_flac
,
704 EncoderWriteCallback
);
705 FLAC__stream_encoder_set_metadata_callback( p_sys
->p_flac
,
706 EncoderMetadataCallback
);
707 FLAC__stream_encoder_set_client_data( p_sys
->p_flac
, p_enc
);
709 FLAC__stream_encoder_init( p_sys
->p_flac
);
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
;
726 p_sys
->i_pts
= p_aout_buf
->i_pts
-
727 (mtime_t
)1000000 * (mtime_t
)p_sys
->i_samples_delay
/
728 (mtime_t
)p_enc
->fmt_in
.audio
.i_rate
;
730 p_sys
->i_samples_delay
+= p_aout_buf
->i_nb_samples
;
732 /* Convert samples to FLAC__int32 */
733 if( p_sys
->i_buffer
< p_aout_buf
->i_buffer
* 2 )
736 xrealloc( p_sys
->p_buffer
, p_aout_buf
->i_buffer
* 2 );
737 p_sys
->i_buffer
= p_aout_buf
->i_buffer
* 2;
740 for( i
= 0 ; i
< p_aout_buf
->i_buffer
/ 2 ; i
++ )
742 p_sys
->p_buffer
[i
]= ((int16_t *)p_aout_buf
->p_buffer
)[i
];
745 FLAC__stream_encoder_process_interleaved( p_sys
->p_flac
, p_sys
->p_buffer
,
746 p_aout_buf
->i_nb_samples
);
748 p_chain
= p_sys
->p_chain
;
754 /*****************************************************************************
755 * CloseEncoder: encoder destruction
756 *****************************************************************************/
757 static void CloseEncoder( vlc_object_t
*p_this
)
759 encoder_t
*p_enc
= (encoder_t
*)p_this
;
760 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
762 FLAC__stream_encoder_delete( p_sys
->p_flac
);
764 free( p_sys
->p_buffer
);
768 /*****************************************************************************
769 * EncoderMetadataCallback: called by libflac to output metadata
770 *****************************************************************************/
771 static void EncoderMetadataCallback( const FLAC__StreamEncoder
*encoder
,
772 const FLAC__StreamMetadata
*metadata
,
776 encoder_t
*p_enc
= (encoder_t
*)client_data
;
778 msg_Err( p_enc
, "MetadataCallback: %i", metadata
->type
);
782 /*****************************************************************************
783 * EncoderWriteCallback: called by libflac to output encoded samples
784 *****************************************************************************/
785 static FLAC__StreamEncoderWriteStatus
786 EncoderWriteCallback( const FLAC__StreamEncoder
*encoder
,
787 const FLAC__byte buffer
[],
788 unsigned bytes
, unsigned samples
,
789 unsigned current_frame
, void *client_data
)
791 VLC_UNUSED(encoder
); VLC_UNUSED(current_frame
);
792 encoder_t
*p_enc
= (encoder_t
*)client_data
;
793 encoder_sys_t
*p_sys
= p_enc
->p_sys
;
798 if( p_sys
->i_headers
== 1 )
800 msg_Dbg( p_enc
, "Writing STREAMINFO: %i", bytes
);
802 /* Backup the STREAMINFO metadata block */
803 p_enc
->fmt_out
.i_extra
= STREAMINFO_SIZE
+ 4;
804 p_enc
->fmt_out
.p_extra
= xmalloc( STREAMINFO_SIZE
+ 4 );
805 memcpy( p_enc
->fmt_out
.p_extra
, "fLaC", 4 );
806 memcpy( ((uint8_t *)p_enc
->fmt_out
.p_extra
) + 4, buffer
,
809 /* Fake this as the last metadata block */
810 ((uint8_t*)p_enc
->fmt_out
.p_extra
)[4] |= 0x80;
813 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;
816 p_block
= block_New( p_enc
, bytes
);
817 memcpy( p_block
->p_buffer
, buffer
, bytes
);
819 p_block
->i_dts
= p_block
->i_pts
= p_sys
->i_pts
;
821 p_sys
->i_samples_delay
-= samples
;
823 p_block
->i_length
= (mtime_t
)1000000 *
824 (mtime_t
)samples
/ (mtime_t
)p_enc
->fmt_in
.audio
.i_rate
;
827 p_sys
->i_pts
+= p_block
->i_length
;
829 block_ChainAppend( &p_sys
->p_chain
, p_block
);
831 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE
;