Commit f6e06590 authored by Rafaël Carré's avatar Rafaël Carré

flac: reorder, remove some forward declarations

parent 7c39a792
......@@ -99,32 +99,6 @@ static void CloseEncoder ( vlc_object_t * );
static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
static FLAC__StreamDecoderReadStatus
DecoderReadCallback( const FLAC__StreamDecoder *decoder,
FLAC__byte buffer[], size_t *bytes, void *client_data );
static FLAC__StreamDecoderWriteStatus
DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
const FLAC__Frame *frame,
const FLAC__int32 *const buffer[], void *client_data );
static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
const FLAC__StreamMetadata *metadata,
void *client_data );
static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
FLAC__StreamDecoderErrorStatus status,
void *client_data);
static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
const int *pi_order, int i_nb_channels, int i_samples );
static void Interleave24( int8_t *p_out, const int32_t * const *pp_in,
const int *pi_order, int i_nb_channels, int i_samples );
static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
const int *pi_order, int i_nb_channels, int i_samples );
static void decoder_state_error( decoder_t *p_dec,
FLAC__StreamDecoderState state );
/*****************************************************************************
* Module descriptor
*****************************************************************************/
......@@ -147,187 +121,57 @@ vlc_module_begin ()
vlc_module_end ()
/*****************************************************************************
* OpenDecoder: probe the decoder and return score
* Interleave: helper function to interleave channels
*****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
const int pi_index[],
int i_nb_channels, int i_samples )
{
decoder_t *p_dec = (decoder_t*)p_this;
decoder_sys_t *p_sys;
if( p_dec->fmt_in.i_codec != VLC_CODEC_FLAC )
{
return VLC_EGENERIC;
}
/* Allocate the memory needed to store the decoder's structure */
if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
return VLC_ENOMEM;
/* Misc init */
p_sys->b_stream_info = false;
p_sys->p_block = NULL;
/* Take care of flac init */
if( !(p_sys->p_flac = FLAC__stream_decoder_new()) )
{
msg_Err( p_dec, "FLAC__stream_decoder_new() failed" );
free( p_sys );
return VLC_EGENERIC;
}
#ifdef USE_NEW_FLAC_API
if( FLAC__stream_decoder_init_stream( p_sys->p_flac,
DecoderReadCallback,
NULL,
NULL,
NULL,
NULL,
DecoderWriteCallback,
DecoderMetadataCallback,
DecoderErrorCallback,
p_dec )
!= FLAC__STREAM_DECODER_INIT_STATUS_OK )
int i, j;
for ( j = 0; j < i_samples; j++ )
{
msg_Err( p_dec, "FLAC__stream_decoder_init_stream() failed" );
FLAC__stream_decoder_delete( p_sys->p_flac );
free( p_sys );
return VLC_EGENERIC;
for ( i = 0; i < i_nb_channels; i++ )
{
p_out[j * i_nb_channels + i] = pp_in[pi_index[i]][j];
}
}
#else
FLAC__stream_decoder_set_read_callback( p_sys->p_flac,
DecoderReadCallback );
FLAC__stream_decoder_set_write_callback( p_sys->p_flac,
DecoderWriteCallback );
FLAC__stream_decoder_set_metadata_callback( p_sys->p_flac,
DecoderMetadataCallback );
FLAC__stream_decoder_set_error_callback( p_sys->p_flac,
DecoderErrorCallback );
FLAC__stream_decoder_set_client_data( p_sys->p_flac, p_dec );
FLAC__stream_decoder_init( p_sys->p_flac );
#endif
/* Set output properties */
p_dec->fmt_out.i_cat = AUDIO_ES;
p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
/* Set callbacks */
p_dec->pf_decode_audio = DecodeBlock;
/* */
p_dec->b_need_packetized = true;
return VLC_SUCCESS;
}
/*****************************************************************************
* CloseDecoder: flac decoder destruction
*****************************************************************************/
static void CloseDecoder( vlc_object_t *p_this )
{
decoder_t *p_dec = (decoder_t *)p_this;
decoder_sys_t *p_sys = p_dec->p_sys;
FLAC__stream_decoder_finish( p_sys->p_flac );
FLAC__stream_decoder_delete( p_sys->p_flac );
if( p_sys->p_block )
block_Release( p_sys->p_block );
free( p_sys );
}
/*****************************************************************************
* ProcessHeader: process Flac header.
*****************************************************************************/
static void ProcessHeader( decoder_t *p_dec )
{
decoder_sys_t *p_sys = p_dec->p_sys;
if( !p_dec->fmt_in.i_extra )
return;
/* Decode STREAMINFO */
msg_Dbg( p_dec, "decode STREAMINFO" );
p_sys->p_block = block_New( p_dec, p_dec->fmt_in.i_extra );
memcpy( p_sys->p_block->p_buffer, p_dec->fmt_in.p_extra,
p_dec->fmt_in.i_extra );
FLAC__stream_decoder_process_until_end_of_metadata( p_sys->p_flac );
msg_Dbg( p_dec, "STREAMINFO decoded" );
}
/****************************************************************************
* DecodeBlock: the whole thing
****************************************************************************/
static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
static void Interleave24( int8_t *p_out, const int32_t * const *pp_in,
const int pi_index[],
int i_nb_channels, int i_samples )
{
decoder_sys_t *p_sys = p_dec->p_sys;
if( !pp_block || !*pp_block )
return NULL;
if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{
block_Release( *pp_block );
return NULL;
}
if( !p_sys->b_stream_info )
ProcessHeader( p_dec );
p_sys->p_block = *pp_block;
*pp_block = NULL;
if( p_sys->p_block->i_pts > VLC_TS_INVALID &&
p_sys->p_block->i_pts != date_Get( &p_sys->end_date ) )
date_Set( &p_sys->end_date, p_sys->p_block->i_pts );
p_sys->p_aout_buffer = 0;
if( !FLAC__stream_decoder_process_single( p_sys->p_flac ) )
{
decoder_state_error( p_dec,
FLAC__stream_decoder_get_state( p_sys->p_flac ) );
FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
}
/* If the decoder is in the "aborted" state,
* FLAC__stream_decoder_process_single() won't return an error. */
if( FLAC__stream_decoder_get_state(p_dec->p_sys->p_flac)
== FLAC__STREAM_DECODER_ABORTED )
int i, j;
for ( j = 0; j < i_samples; j++ )
{
FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
for ( i = 0; i < i_nb_channels; i++ )
{
const int i_index = pi_index[i];
#ifdef WORDS_BIGENDIAN
p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 16) & 0xff;
p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 0 ) & 0xff;
#else
p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 16) & 0xff;
p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 0 ) & 0xff;
#endif
}
}
block_Release( p_sys->p_block );
p_sys->p_block = NULL;
return p_sys->p_aout_buffer;
}
/*****************************************************************************
* DecoderReadCallback: called by libflac when it needs more data
*****************************************************************************/
static FLAC__StreamDecoderReadStatus
DecoderReadCallback( const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
size_t *bytes, void *client_data )
static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
const int pi_index[],
int i_nb_channels, int i_samples )
{
VLC_UNUSED(decoder);
decoder_t *p_dec = (decoder_t *)client_data;
decoder_sys_t *p_sys = p_dec->p_sys;
if( p_sys->p_block && p_sys->p_block->i_buffer )
{
*bytes = __MIN(*bytes, p_sys->p_block->i_buffer);
memcpy( buffer, p_sys->p_block->p_buffer, *bytes );
p_sys->p_block->i_buffer -= *bytes;
p_sys->p_block->p_buffer += *bytes;
}
else
int i, j;
for ( j = 0; j < i_samples; j++ )
{
*bytes = 0;
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
for ( i = 0; i < i_nb_channels; i++ )
{
p_out[j * i_nb_channels + i] = (int32_t)(pp_in[pi_index[i]][j]);
}
}
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
/*****************************************************************************
......@@ -394,6 +238,33 @@ DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
/*****************************************************************************
* DecoderReadCallback: called by libflac when it needs more data
*****************************************************************************/
static FLAC__StreamDecoderReadStatus
DecoderReadCallback( const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
size_t *bytes, void *client_data )
{
VLC_UNUSED(decoder);
decoder_t *p_dec = (decoder_t *)client_data;
decoder_sys_t *p_sys = p_dec->p_sys;
if( p_sys->p_block && p_sys->p_block->i_buffer )
{
*bytes = __MIN(*bytes, p_sys->p_block->i_buffer);
memcpy( buffer, p_sys->p_block->p_buffer, *bytes );
p_sys->p_block->i_buffer -= *bytes;
p_sys->p_block->p_buffer += *bytes;
}
else
{
*bytes = 0;
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
/*****************************************************************************
* DecoderMetadataCallback: called by libflac to when it encounters metadata
*****************************************************************************/
......@@ -473,62 +344,116 @@ static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
msg_Err( p_dec, "got decoder error: %d", status );
}
FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
return;
FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
return;
}
/*****************************************************************************
* OpenDecoder: probe the decoder and return score
*****************************************************************************/
static int OpenDecoder( vlc_object_t *p_this )
{
decoder_t *p_dec = (decoder_t*)p_this;
decoder_sys_t *p_sys;
if( p_dec->fmt_in.i_codec != VLC_CODEC_FLAC )
{
return VLC_EGENERIC;
}
/* Allocate the memory needed to store the decoder's structure */
if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
return VLC_ENOMEM;
/* Misc init */
p_sys->b_stream_info = false;
p_sys->p_block = NULL;
/* Take care of flac init */
if( !(p_sys->p_flac = FLAC__stream_decoder_new()) )
{
msg_Err( p_dec, "FLAC__stream_decoder_new() failed" );
free( p_sys );
return VLC_EGENERIC;
}
#ifdef USE_NEW_FLAC_API
if( FLAC__stream_decoder_init_stream( p_sys->p_flac,
DecoderReadCallback,
NULL,
NULL,
NULL,
NULL,
DecoderWriteCallback,
DecoderMetadataCallback,
DecoderErrorCallback,
p_dec )
!= FLAC__STREAM_DECODER_INIT_STATUS_OK )
{
msg_Err( p_dec, "FLAC__stream_decoder_init_stream() failed" );
FLAC__stream_decoder_delete( p_sys->p_flac );
free( p_sys );
return VLC_EGENERIC;
}
#else
FLAC__stream_decoder_set_read_callback( p_sys->p_flac,
DecoderReadCallback );
FLAC__stream_decoder_set_write_callback( p_sys->p_flac,
DecoderWriteCallback );
FLAC__stream_decoder_set_metadata_callback( p_sys->p_flac,
DecoderMetadataCallback );
FLAC__stream_decoder_set_error_callback( p_sys->p_flac,
DecoderErrorCallback );
FLAC__stream_decoder_set_client_data( p_sys->p_flac, p_dec );
FLAC__stream_decoder_init( p_sys->p_flac );
#endif
/* Set output properties */
p_dec->fmt_out.i_cat = AUDIO_ES;
p_dec->fmt_out.i_codec = VLC_CODEC_FL32;
/* Set callbacks */
p_dec->pf_decode_audio = DecodeBlock;
/* */
p_dec->b_need_packetized = true;
return VLC_SUCCESS;
}
/*****************************************************************************
* Interleave: helper function to interleave channels
* CloseDecoder: flac decoder destruction
*****************************************************************************/
static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
const int pi_index[],
int i_nb_channels, int i_samples )
static void CloseDecoder( vlc_object_t *p_this )
{
int i, j;
for ( j = 0; j < i_samples; j++ )
{
for ( i = 0; i < i_nb_channels; i++ )
{
p_out[j * i_nb_channels + i] = pp_in[pi_index[i]][j];
}
}
}
decoder_t *p_dec = (decoder_t *)p_this;
decoder_sys_t *p_sys = p_dec->p_sys;
static void Interleave24( int8_t *p_out, const int32_t * const *pp_in,
const int pi_index[],
int i_nb_channels, int i_samples )
{
int i, j;
for ( j = 0; j < i_samples; j++ )
{
for ( i = 0; i < i_nb_channels; i++ )
{
const int i_index = pi_index[i];
#ifdef WORDS_BIGENDIAN
p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 16) & 0xff;
p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 0 ) & 0xff;
#else
p_out[3*(j * i_nb_channels + i)+2] = (pp_in[i_index][j] >> 16) & 0xff;
p_out[3*(j * i_nb_channels + i)+1] = (pp_in[i_index][j] >> 8 ) & 0xff;
p_out[3*(j * i_nb_channels + i)+0] = (pp_in[i_index][j] >> 0 ) & 0xff;
#endif
}
}
FLAC__stream_decoder_finish( p_sys->p_flac );
FLAC__stream_decoder_delete( p_sys->p_flac );
if( p_sys->p_block )
block_Release( p_sys->p_block );
free( p_sys );
}
static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
const int pi_index[],
int i_nb_channels, int i_samples )
/*****************************************************************************
* ProcessHeader: process Flac header.
*****************************************************************************/
static void ProcessHeader( decoder_t *p_dec )
{
int i, j;
for ( j = 0; j < i_samples; j++ )
{
for ( i = 0; i < i_nb_channels; i++ )
{
p_out[j * i_nb_channels + i] = (int32_t)(pp_in[pi_index[i]][j]);
}
}
decoder_sys_t *p_sys = p_dec->p_sys;
if( !p_dec->fmt_in.i_extra )
return;
/* Decode STREAMINFO */
msg_Dbg( p_dec, "decode STREAMINFO" );
p_sys->p_block = block_New( p_dec, p_dec->fmt_in.i_extra );
memcpy( p_sys->p_block->p_buffer, p_dec->fmt_in.p_extra,
p_dec->fmt_in.i_extra );
FLAC__stream_decoder_process_until_end_of_metadata( p_sys->p_flac );
msg_Dbg( p_dec, "STREAMINFO decoded" );
}
/*****************************************************************************
......@@ -596,6 +521,54 @@ static void decoder_state_error( decoder_t *p_dec,
}
}
/****************************************************************************
* DecodeBlock: the whole thing
****************************************************************************/
static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
{
decoder_sys_t *p_sys = p_dec->p_sys;
if( !pp_block || !*pp_block )
return NULL;
if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
{
block_Release( *pp_block );
return NULL;
}
if( !p_sys->b_stream_info )
ProcessHeader( p_dec );
p_sys->p_block = *pp_block;
*pp_block = NULL;
if( p_sys->p_block->i_pts > VLC_TS_INVALID &&
p_sys->p_block->i_pts != date_Get( &p_sys->end_date ) )
date_Set( &p_sys->end_date, p_sys->p_block->i_pts );
p_sys->p_aout_buffer = 0;
if( !FLAC__stream_decoder_process_single( p_sys->p_flac ) )
{
decoder_state_error( p_dec,
FLAC__stream_decoder_get_state( p_sys->p_flac ) );
FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
}
/* If the decoder is in the "aborted" state,
* FLAC__stream_decoder_process_single() won't return an error. */
if( FLAC__stream_decoder_get_state(p_dec->p_sys->p_flac)
== FLAC__STREAM_DECODER_ABORTED )
{
FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
}
block_Release( p_sys->p_block );
p_sys->p_block = NULL;
return p_sys->p_aout_buffer;
}
/*****************************************************************************
* encoder_sys_t : flac encoder descriptor
*****************************************************************************/
......@@ -630,15 +603,70 @@ struct encoder_sys_t
static block_t *Encode( encoder_t *, aout_buffer_t * );
/*****************************************************************************
* EncoderWriteCallback: called by libflac to output encoded samples
*****************************************************************************/
static FLAC__StreamEncoderWriteStatus
EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
const FLAC__byte buffer[],
size_t bytes, unsigned samples,
unsigned current_frame, void *client_data );
unsigned current_frame, void *client_data )
{
VLC_UNUSED(encoder); VLC_UNUSED(current_frame);
encoder_t *p_enc = (encoder_t *)client_data;
encoder_sys_t *p_sys = p_enc->p_sys;
block_t *p_block;
if( samples == 0 )
{
if( p_sys->i_headers == 1 )
{
msg_Dbg( p_enc, "Writing STREAMINFO: %zu", bytes );
/* Backup the STREAMINFO metadata block */
p_enc->fmt_out.i_extra = STREAMINFO_SIZE + 4;
p_enc->fmt_out.p_extra = xmalloc( STREAMINFO_SIZE + 4 );
memcpy( p_enc->fmt_out.p_extra, "fLaC", 4 );
memcpy( ((uint8_t *)p_enc->fmt_out.p_extra) + 4, buffer,
STREAMINFO_SIZE );
/* Fake this as the last metadata block */
((uint8_t*)p_enc->fmt_out.p_extra)[4] |= 0x80;
}
p_sys->i_headers++;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
p_block = block_New( p_enc, bytes );
memcpy( p_block->p_buffer, buffer, bytes );
p_block->i_dts = p_block->i_pts = p_sys->i_pts;
p_sys->i_samples_delay -= samples;
p_block->i_length = (mtime_t)1000000 *
(mtime_t)samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
/* Update pts */
p_sys->i_pts += p_block->i_length;
block_ChainAppend( &p_sys->p_chain, p_block );
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
/*****************************************************************************
* EncoderMetadataCallback: called by libflac to output metadata
*****************************************************************************/
static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
const FLAC__StreamMetadata *metadata,
void *client_data );
void *client_data )
{
VLC_UNUSED(encoder);
encoder_t *p_enc = (encoder_t *)client_data;
msg_Err( p_enc, "MetadataCallback: %i", metadata->type );
return;
}
/*****************************************************************************
* OpenEncoder: probe the encoder and return score
......@@ -764,70 +792,3 @@ static void CloseEncoder( vlc_object_t *p_this )
free( p_sys->p_buffer );
free( p_sys );
}
/*****************************************************************************
* EncoderMetadataCallback: called by libflac to output metadata
*****************************************************************************/
static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
const FLAC__StreamMetadata *metadata,
void *client_data )
{
VLC_UNUSED(encoder);
encoder_t *p_enc = (encoder_t *)client_data;
msg_Err( p_enc, "MetadataCallback: %i", metadata->type );
return;
}
/*****************************************************************************
* EncoderWriteCallback: called by libflac to output encoded samples
*****************************************************************************/
static FLAC__StreamEncoderWriteStatus
EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
const FLAC__byte buffer[],
size_t bytes, unsigned samples,
unsigned current_frame, void *client_data )
{
VLC_UNUSED(encoder); VLC_UNUSED(current_frame);
encoder_t *p_enc = (encoder_t *)client_data;
encoder_sys_t *p_sys = p_enc->p_sys;
block_t *p_block;
if( samples == 0 )
{
if( p_sys->i_headers == 1 )
{
msg_Dbg( p_enc, "Writing STREAMINFO: %zu", bytes );
/* Backup the STREAMINFO metadata block */
p_enc->fmt_out.i_extra = STREAMINFO_SIZE + 4;
p_enc->fmt_out.p_extra = xmalloc( STREAMINFO_SIZE + 4 );
memcpy( p_enc->fmt_out.p_extra, "fLaC", 4 );
memcpy( ((uint8_t *)p_enc->fmt_out.p_extra) + 4, buffer,
STREAMINFO_SIZE );
/* Fake this as the last metadata block */
((uint8_t*)p_enc->fmt_out.p_extra)[4] |= 0x80;
}
p_sys->i_headers++;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
p_block = block_New( p_enc, bytes );
memcpy( p_block->p_buffer, buffer, bytes );
p_block->i_dts = p_block->i_pts = p_sys->i_pts;
p_sys->i_samples_delay -= samples;