00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include <vlc/vlc.h>
00030 #include <vlc/decoder.h>
00031
00032 #ifdef HAVE_FLAC_STREAM_DECODER_H
00033 # include <FLAC/stream_decoder.h>
00034 # include <FLAC/stream_encoder.h>
00035 # define USE_LIBFLAC
00036 #endif
00037
00038 #include "vlc_block_helper.h"
00039 #include "vlc_bits.h"
00040
00041 #define MAX_FLAC_HEADER_SIZE 16
00042
00043
00044
00045
00046 struct decoder_sys_t
00047 {
00048
00049
00050
00051 int i_state;
00052
00053 block_bytestream_t bytestream;
00054
00055
00056
00057
00058 block_t *p_block;
00059 aout_buffer_t *p_aout_buffer;
00060
00061
00062
00063
00064 #ifdef USE_LIBFLAC
00065 FLAC__StreamDecoder *p_flac;
00066 FLAC__StreamMetadata_StreamInfo stream_info;
00067 #else
00068 struct
00069 {
00070 unsigned min_blocksize, max_blocksize;
00071 unsigned min_framesize, max_framesize;
00072 unsigned sample_rate;
00073 unsigned channels;
00074 unsigned bits_per_sample;
00075
00076 } stream_info;
00077 #endif
00078 vlc_bool_t b_stream_info;
00079
00080
00081
00082
00083 audio_date_t end_date;
00084 mtime_t i_pts;
00085
00086 int i_frame_size, i_frame_length, i_bits_per_sample;
00087 unsigned int i_rate, i_channels, i_channels_conf;
00088 };
00089
00090 enum {
00091
00092 STATE_NOSYNC,
00093 STATE_SYNC,
00094 STATE_HEADER,
00095 STATE_NEXT_SYNC,
00096 STATE_GET_DATA,
00097 STATE_SEND_DATA
00098 };
00099
00100 static int pi_channels_maps[6] =
00101 {
00102 0,
00103 AOUT_CHAN_CENTER,
00104 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
00105 AOUT_CHAN_CENTER | AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT,
00106 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_REARLEFT
00107 | AOUT_CHAN_REARRIGHT,
00108 AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
00109 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
00110 };
00111
00112
00113
00114
00115 static int OpenDecoder ( vlc_object_t * );
00116 static int OpenPacketizer( vlc_object_t * );
00117 static void CloseDecoder ( vlc_object_t * );
00118
00119 #ifdef USE_LIBFLAC
00120 static int OpenEncoder ( vlc_object_t * );
00121 static void CloseEncoder ( vlc_object_t * );
00122 #endif
00123
00124 #ifdef USE_LIBFLAC
00125 static aout_buffer_t *DecodeBlock( decoder_t *, block_t ** );
00126 #endif
00127 static block_t *PacketizeBlock( decoder_t *, block_t ** );
00128
00129 static int SyncInfo( decoder_t *, uint8_t *, unsigned int *, unsigned int *,
00130 unsigned int *,int * );
00131
00132
00133 #ifdef USE_LIBFLAC
00134 static FLAC__StreamDecoderReadStatus
00135 DecoderReadCallback( const FLAC__StreamDecoder *decoder,
00136 FLAC__byte buffer[], unsigned *bytes, void *client_data );
00137
00138 static FLAC__StreamDecoderWriteStatus
00139 DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
00140 const FLAC__Frame *frame,
00141 const FLAC__int32 *const buffer[], void *client_data );
00142
00143 static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
00144 const FLAC__StreamMetadata *metadata,
00145 void *client_data );
00146 static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
00147 FLAC__StreamDecoderErrorStatus status,
00148 void *client_data);
00149
00150 static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
00151 int i_nb_channels, int i_samples );
00152 static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
00153 int i_nb_channels, int i_samples );
00154
00155 static void decoder_state_error( decoder_t *p_dec,
00156 FLAC__StreamDecoderState state );
00157 #endif
00158
00159 static uint64_t read_utf8( const uint8_t *p_buf, int *pi_read );
00160 static uint8_t flac_crc8( const uint8_t *data, unsigned len );
00161
00162
00163
00164
00165 vlc_module_begin();
00166
00167 set_category( CAT_INPUT );
00168 set_subcategory( SUBCAT_INPUT_ACODEC );
00169
00170 #ifdef USE_LIBFLAC
00171 set_description( _("Flac audio decoder") );
00172 set_capability( "decoder", 100 );
00173 set_callbacks( OpenDecoder, CloseDecoder );
00174
00175 add_submodule();
00176 set_description( _("Flac audio encoder") );
00177 set_capability( "encoder", 100 );
00178 set_callbacks( OpenEncoder, CloseEncoder );
00179
00180 add_submodule();
00181 #endif
00182 set_description( _("Flac audio packetizer") );
00183 set_capability( "packetizer", 100 );
00184 set_callbacks( OpenPacketizer, CloseDecoder );
00185
00186 add_shortcut( "flac" );
00187 vlc_module_end();
00188
00189
00190
00191
00192 static int OpenDecoder( vlc_object_t *p_this )
00193 {
00194 decoder_t *p_dec = (decoder_t*)p_this;
00195 decoder_sys_t *p_sys;
00196
00197 if( p_dec->fmt_in.i_codec != VLC_FOURCC('f','l','a','c') )
00198 {
00199 return VLC_EGENERIC;
00200 }
00201
00202
00203 if( ( p_dec->p_sys = p_sys =
00204 (decoder_sys_t *)malloc(sizeof(decoder_sys_t)) ) == NULL )
00205 {
00206 msg_Err( p_dec, "out of memory" );
00207 return VLC_EGENERIC;
00208 }
00209
00210
00211 aout_DateSet( &p_sys->end_date, 0 );
00212 p_sys->i_state = STATE_NOSYNC;
00213 p_sys->b_stream_info = VLC_FALSE;
00214
00215 p_sys->bytestream = block_BytestreamInit( p_dec );
00216
00217 #ifdef USE_LIBFLAC
00218
00219 if( !(p_sys->p_flac = FLAC__stream_decoder_new()) )
00220 {
00221 msg_Err( p_dec, "FLAC__stream_decoder_new() failed" );
00222 free( p_sys );
00223 return VLC_EGENERIC;
00224 }
00225
00226 FLAC__stream_decoder_set_read_callback( p_sys->p_flac,
00227 DecoderReadCallback );
00228 FLAC__stream_decoder_set_write_callback( p_sys->p_flac,
00229 DecoderWriteCallback );
00230 FLAC__stream_decoder_set_metadata_callback( p_sys->p_flac,
00231 DecoderMetadataCallback );
00232 FLAC__stream_decoder_set_error_callback( p_sys->p_flac,
00233 DecoderErrorCallback );
00234 FLAC__stream_decoder_set_client_data( p_sys->p_flac, p_dec );
00235
00236 FLAC__stream_decoder_init( p_sys->p_flac );
00237 #endif
00238
00239
00240 p_dec->fmt_out.i_cat = AUDIO_ES;
00241 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','3','2');
00242
00243
00244 #ifdef USE_LIBFLAC
00245 p_dec->pf_decode_audio = DecodeBlock;
00246 #endif
00247 p_dec->pf_packetize = PacketizeBlock;
00248
00249 return VLC_SUCCESS;
00250 }
00251
00252 static int OpenPacketizer( vlc_object_t *p_this )
00253 {
00254 decoder_t *p_dec = (decoder_t*)p_this;
00255 int i_ret;
00256
00257
00258 es_format_Copy( &p_dec->fmt_out, &p_dec->fmt_in );
00259
00260 i_ret = OpenDecoder( p_this );
00261
00262
00263 p_dec->fmt_out.i_codec = VLC_FOURCC('f','l','a','c');
00264
00265 if( i_ret != VLC_SUCCESS ) return i_ret;
00266
00267 return i_ret;
00268 }
00269
00270
00271
00272
00273 static void CloseDecoder( vlc_object_t *p_this )
00274 {
00275 decoder_t *p_dec = (decoder_t *)p_this;
00276 decoder_sys_t *p_sys = p_dec->p_sys;
00277
00278 #ifdef USE_LIBFLAC
00279 FLAC__stream_decoder_finish( p_sys->p_flac );
00280 FLAC__stream_decoder_delete( p_sys->p_flac );
00281 #endif
00282
00283 if( p_sys->p_block ) free( p_sys->p_block );
00284 free( p_sys );
00285 }
00286
00287
00288
00289
00290 static void ProcessHeader( decoder_t *p_dec )
00291 {
00292 decoder_sys_t *p_sys = p_dec->p_sys;
00293
00294 #ifdef USE_LIBFLAC
00295 if( !p_dec->fmt_in.i_extra ) return;
00296
00297
00298 msg_Dbg( p_dec, "decode STREAMINFO" );
00299 p_sys->p_block = block_New( p_dec, p_dec->fmt_in.i_extra );
00300 memcpy( p_sys->p_block->p_buffer, p_dec->fmt_in.p_extra,
00301 p_dec->fmt_in.i_extra );
00302 FLAC__stream_decoder_process_until_end_of_metadata( p_sys->p_flac );
00303 msg_Dbg( p_dec, "STREAMINFO decoded" );
00304
00305 #else
00306 bs_t bs;
00307
00308 if( !p_dec->fmt_in.i_extra ) return;
00309
00310 bs_init( &bs, p_dec->fmt_in.p_extra, p_dec->fmt_in.i_extra );
00311
00312 p_sys->stream_info.min_blocksize = bs_read( &bs, 16 );
00313 p_sys->stream_info.max_blocksize = bs_read( &bs, 16 );
00314
00315 p_sys->stream_info.min_framesize = bs_read( &bs, 24 );
00316 p_sys->stream_info.max_framesize = bs_read( &bs, 24 );
00317
00318 p_sys->stream_info.sample_rate = bs_read( &bs, 20 );
00319 p_sys->stream_info.channels = bs_read( &bs, 3 ) + 1;
00320 p_sys->stream_info.bits_per_sample = bs_read( &bs, 5 ) + 1;
00321 #endif
00322
00323 if( !p_sys->b_stream_info ) return;
00324
00325 if( p_dec->fmt_out.i_codec == VLC_FOURCC('f','l','a','c') )
00326 {
00327 p_dec->fmt_out.i_extra = p_dec->fmt_in.i_extra;
00328 p_dec->fmt_out.p_extra =
00329 realloc( p_dec->fmt_out.p_extra, p_dec->fmt_out.i_extra );
00330 memcpy( p_dec->fmt_out.p_extra,
00331 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
00332 }
00333 }
00334
00335
00336
00337
00338
00339
00340 static block_t *PacketizeBlock( decoder_t *p_dec, block_t **pp_block )
00341 {
00342 decoder_sys_t *p_sys = p_dec->p_sys;
00343 uint8_t p_header[MAX_FLAC_HEADER_SIZE];
00344 block_t *p_sout_block;
00345
00346 if( !pp_block || !*pp_block ) return NULL;
00347
00348 if( !p_sys->b_stream_info ) ProcessHeader( p_dec );
00349
00350 if( !aout_DateGet( &p_sys->end_date ) && !(*pp_block)->i_pts )
00351 {
00352
00353 block_Release( *pp_block );
00354 return NULL;
00355 }
00356 else if( !aout_DateGet( &p_sys->end_date ) )
00357 {
00358
00359 aout_DateSet( &p_sys->end_date, (*pp_block)->i_pts );
00360 }
00361
00362 if( (*pp_block)->i_flags&(BLOCK_FLAG_DISCONTINUITY|BLOCK_FLAG_CORRUPTED) )
00363 {
00364 p_sys->i_state = STATE_NOSYNC;
00365 }
00366
00367 block_BytestreamPush( &p_sys->bytestream, *pp_block );
00368
00369 while( 1 )
00370 {
00371 switch( p_sys->i_state )
00372 {
00373 case STATE_NOSYNC:
00374 while( block_PeekBytes( &p_sys->bytestream, p_header, 2 )
00375 == VLC_SUCCESS )
00376 {
00377 if( p_header[0] == 0xFF && p_header[1] == 0xF8 )
00378 {
00379 p_sys->i_state = STATE_SYNC;
00380 break;
00381 }
00382 block_SkipByte( &p_sys->bytestream );
00383 }
00384 if( p_sys->i_state != STATE_SYNC )
00385 {
00386 block_BytestreamFlush( &p_sys->bytestream );
00387
00388
00389 return NULL;
00390 }
00391
00392 case STATE_SYNC:
00393
00394 p_sys->i_pts = p_sys->bytestream.p_block->i_pts;
00395 if( p_sys->i_pts != 0 &&
00396 p_sys->i_pts != aout_DateGet( &p_sys->end_date ) )
00397 {
00398 aout_DateSet( &p_sys->end_date, p_sys->i_pts );
00399 }
00400 p_sys->i_state = STATE_HEADER;
00401
00402 case STATE_HEADER:
00403
00404 if( block_PeekBytes( &p_sys->bytestream, p_header,
00405 MAX_FLAC_HEADER_SIZE ) != VLC_SUCCESS )
00406 {
00407
00408 return NULL;
00409 }
00410
00411
00412 p_sys->i_frame_length = SyncInfo( p_dec, p_header,
00413 &p_sys->i_channels,
00414 &p_sys->i_channels_conf,
00415 &p_sys->i_rate,
00416 &p_sys->i_bits_per_sample );
00417 if( !p_sys->i_frame_length )
00418 {
00419 msg_Dbg( p_dec, "emulated sync word" );
00420 block_SkipByte( &p_sys->bytestream );
00421 p_sys->i_state = STATE_NOSYNC;
00422 break;
00423 }
00424 if( p_sys->i_rate != p_dec->fmt_out.audio.i_rate )
00425 {
00426 p_dec->fmt_out.audio.i_rate = p_sys->i_rate;
00427 aout_DateInit( &p_sys->end_date, p_sys->i_rate );
00428 }
00429 p_sys->i_state = STATE_NEXT_SYNC;
00430 p_sys->i_frame_size = 1;
00431
00432 case STATE_NEXT_SYNC:
00433
00434
00435
00436
00437 while( block_PeekOffsetBytes( &p_sys->bytestream,
00438 p_sys->i_frame_size, p_header,
00439 MAX_FLAC_HEADER_SIZE )
00440 == VLC_SUCCESS )
00441 {
00442 if( p_header[0] == 0xFF && p_header[1] == 0xF8 )
00443 {
00444
00445 int i_frame_length =
00446 SyncInfo( p_dec, p_header,
00447 &p_sys->i_channels,
00448 &p_sys->i_channels_conf,
00449 &p_sys->i_rate,
00450 &p_sys->i_bits_per_sample );
00451
00452 if( i_frame_length )
00453 {
00454 p_sys->i_state = STATE_SEND_DATA;
00455 break;
00456 }
00457 }
00458 p_sys->i_frame_size++;
00459 }
00460
00461 if( p_sys->i_state != STATE_SEND_DATA )
00462 {
00463
00464 return NULL;
00465 }
00466
00467 case STATE_SEND_DATA:
00468 p_sout_block = block_New( p_dec, p_sys->i_frame_size );
00469
00470
00471
00472 block_GetBytes( &p_sys->bytestream, p_sout_block->p_buffer,
00473 p_sys->i_frame_size );
00474
00475
00476 if( p_sys->i_pts == p_sys->bytestream.p_block->i_pts )
00477 p_sys->i_pts = p_sys->bytestream.p_block->i_pts = 0;
00478
00479
00480 *pp_block = block_BytestreamPop( &p_sys->bytestream );
00481
00482 p_sys->i_state = STATE_NOSYNC;
00483
00484
00485 p_sout_block->i_pts =
00486 p_sout_block->i_dts = aout_DateGet( &p_sys->end_date );
00487 aout_DateIncrement( &p_sys->end_date, p_sys->i_frame_length );
00488 p_sout_block->i_length =
00489 aout_DateGet( &p_sys->end_date ) - p_sout_block->i_pts;
00490
00491 return p_sout_block;
00492 }
00493 }
00494
00495 return NULL;
00496 }
00497
00498 #ifdef USE_LIBFLAC
00499
00500
00501
00502 static aout_buffer_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
00503 {
00504 decoder_sys_t *p_sys = p_dec->p_sys;
00505
00506 if( !pp_block || !*pp_block ) return NULL;
00507
00508 p_sys->p_aout_buffer = 0;
00509 if( ( p_sys->p_block = PacketizeBlock( p_dec, pp_block ) ) )
00510 {
00511 if( !FLAC__stream_decoder_process_single( p_sys->p_flac ) )
00512 {
00513 decoder_state_error( p_dec,
00514 FLAC__stream_decoder_get_state( p_sys->p_flac ) );
00515 FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
00516 }
00517
00518
00519
00520 if( FLAC__stream_decoder_get_state(p_dec->p_sys->p_flac)
00521 == FLAC__STREAM_DECODER_ABORTED )
00522 {
00523 FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
00524 }
00525
00526 block_Release( p_sys->p_block );
00527 p_sys->p_block = NULL;
00528 }
00529
00530 return p_sys->p_aout_buffer;
00531 }
00532
00533
00534
00535
00536 static FLAC__StreamDecoderReadStatus
00537 DecoderReadCallback( const FLAC__StreamDecoder *decoder, FLAC__byte buffer[],
00538 unsigned *bytes, void *client_data )
00539 {
00540 decoder_t *p_dec = (decoder_t *)client_data;
00541 decoder_sys_t *p_sys = p_dec->p_sys;
00542
00543 if( p_sys->p_block && p_sys->p_block->i_buffer )
00544 {
00545 *bytes = __MIN(*bytes, (unsigned)p_sys->p_block->i_buffer);
00546 memcpy( buffer, p_sys->p_block->p_buffer, *bytes );
00547 p_sys->p_block->i_buffer -= *bytes;
00548 p_sys->p_block->p_buffer += *bytes;
00549 }
00550 else
00551 {
00552 *bytes = 0;
00553 return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
00554 }
00555
00556 return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
00557 }
00558
00559
00560
00561
00562 static FLAC__StreamDecoderWriteStatus
00563 DecoderWriteCallback( const FLAC__StreamDecoder *decoder,
00564 const FLAC__Frame *frame,
00565 const FLAC__int32 *const buffer[], void *client_data )
00566 {
00567 decoder_t *p_dec = (decoder_t *)client_data;
00568 decoder_sys_t *p_sys = p_dec->p_sys;
00569
00570 p_sys->p_aout_buffer =
00571 p_dec->pf_aout_buffer_new( p_dec, frame->header.blocksize );
00572
00573 if( p_sys->p_aout_buffer == NULL )
00574 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
00575
00576 switch( frame->header.bits_per_sample )
00577 {
00578 case 16:
00579 Interleave16( (int16_t *)p_sys->p_aout_buffer->p_buffer, buffer,
00580 frame->header.channels, frame->header.blocksize );
00581 break;
00582 default:
00583 Interleave32( (int32_t *)p_sys->p_aout_buffer->p_buffer, buffer,
00584 frame->header.channels, frame->header.blocksize );
00585 }
00586
00587
00588 p_sys->p_aout_buffer->start_date = p_sys->p_block->i_pts;
00589 p_sys->p_aout_buffer->end_date =
00590 p_sys->p_block->i_pts + p_sys->p_block->i_length;
00591
00592 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
00593 }
00594
00595
00596
00597
00598 static void DecoderMetadataCallback( const FLAC__StreamDecoder *decoder,
00599 const FLAC__StreamMetadata *metadata,
00600 void *client_data )
00601 {
00602 decoder_t *p_dec = (decoder_t *)client_data;
00603 decoder_sys_t *p_sys = p_dec->p_sys;
00604
00605 switch( metadata->data.stream_info.bits_per_sample )
00606 {
00607 case 8:
00608 p_dec->fmt_out.i_codec = VLC_FOURCC('s','8',' ',' ');
00609 break;
00610 case 16:
00611 p_dec->fmt_out.i_codec = AOUT_FMT_S16_NE;
00612 break;
00613 default:
00614 msg_Dbg( p_dec, "strange bit/sample value: %d",
00615 metadata->data.stream_info.bits_per_sample );
00616 p_dec->fmt_out.i_codec = VLC_FOURCC('f','i','3','2');
00617 break;
00618 }
00619
00620
00621 p_dec->fmt_out.audio.i_rate = metadata->data.stream_info.sample_rate;
00622 p_dec->fmt_out.audio.i_channels = metadata->data.stream_info.channels;
00623 p_dec->fmt_out.audio.i_physical_channels =
00624 p_dec->fmt_out.audio.i_original_channels =
00625 pi_channels_maps[metadata->data.stream_info.channels];
00626 p_dec->fmt_out.audio.i_bitspersample =
00627 metadata->data.stream_info.bits_per_sample;
00628
00629 aout_DateInit( &p_sys->end_date, p_dec->fmt_out.audio.i_rate );
00630
00631 msg_Dbg( p_dec, "channels:%d samplerate:%d bitspersamples:%d",
00632 p_dec->fmt_out.audio.i_channels, p_dec->fmt_out.audio.i_rate,
00633 p_dec->fmt_out.audio.i_bitspersample );
00634
00635 p_sys->b_stream_info = VLC_TRUE;
00636 p_sys->stream_info = metadata->data.stream_info;
00637
00638 return;
00639 }
00640
00641
00642
00643
00644 static void DecoderErrorCallback( const FLAC__StreamDecoder *decoder,
00645 FLAC__StreamDecoderErrorStatus status,
00646 void *client_data )
00647 {
00648 decoder_t *p_dec = (decoder_t *)client_data;
00649
00650 switch( status )
00651 {
00652 case FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC:
00653 msg_Err( p_dec, "an error in the stream caused the decoder to "
00654 "lose synchronization." );
00655 break;
00656 case FLAC__STREAM_DECODER_ERROR_STATUS_BAD_HEADER:
00657 msg_Err( p_dec, "the decoder encountered a corrupted frame header." );
00658 break;
00659 case FLAC__STREAM_DECODER_ERROR_STATUS_FRAME_CRC_MISMATCH:
00660 msg_Err( p_dec, "frame's data did not match the CRC in the "
00661 "footer." );
00662 break;
00663 default:
00664 msg_Err( p_dec, "got decoder error: %d", status );
00665 }
00666
00667 FLAC__stream_decoder_flush( p_dec->p_sys->p_flac );
00668 return;
00669 }
00670
00671
00672
00673
00674 static void Interleave32( int32_t *p_out, const int32_t * const *pp_in,
00675 int i_nb_channels, int i_samples )
00676 {
00677 int i, j;
00678 for ( j = 0; j < i_samples; j++ )
00679 {
00680 for ( i = 0; i < i_nb_channels; i++ )
00681 {
00682 p_out[j * i_nb_channels + i] = pp_in[i][j];
00683 }
00684 }
00685 }
00686 static void Interleave16( int16_t *p_out, const int32_t * const *pp_in,
00687 int i_nb_channels, int i_samples )
00688 {
00689 int i, j;
00690 for ( j = 0; j < i_samples; j++ )
00691 {
00692 for ( i = 0; i < i_nb_channels; i++ )
00693 {
00694 p_out[j * i_nb_channels + i] = (int32_t)(pp_in[i][j]);
00695 }
00696 }
00697 }
00698
00699
00700
00701
00702 static void decoder_state_error( decoder_t *p_dec,
00703 FLAC__StreamDecoderState state )
00704 {
00705 switch ( state )
00706 {
00707 case FLAC__STREAM_DECODER_SEARCH_FOR_METADATA:
00708 msg_Err( p_dec, "the decoder is ready to search for metadata." );
00709 break;
00710 case FLAC__STREAM_DECODER_READ_METADATA:
00711 msg_Err( p_dec, "the decoder is ready to or is in the process of "
00712 "reading metadata." );
00713 break;
00714 case FLAC__STREAM_DECODER_SEARCH_FOR_FRAME_SYNC:
00715 msg_Err( p_dec, "the decoder is ready to or is in the process of "
00716 "searching for the frame sync code." );
00717 break;
00718 case FLAC__STREAM_DECODER_READ_FRAME:
00719 msg_Err( p_dec, "the decoder is ready to or is in the process of "
00720 "reading a frame." );
00721 break;
00722 case FLAC__STREAM_DECODER_END_OF_STREAM:
00723 msg_Err( p_dec, "the decoder has reached the end of the stream." );
00724 break;
00725 case FLAC__STREAM_DECODER_ABORTED:
00726 msg_Err( p_dec, "the decoder was aborted by the read callback." );
00727 break;
00728 case FLAC__STREAM_DECODER_UNPARSEABLE_STREAM:
00729 msg_Err( p_dec, "the decoder encountered reserved fields in use "
00730 "in the stream." );
00731 break;
00732 case FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR:
00733 msg_Err( p_dec, "error when allocating memory." );
00734 break;
00735 case FLAC__STREAM_DECODER_ALREADY_INITIALIZED:
00736 msg_Err( p_dec, "FLAC__stream_decoder_init() was called when the "
00737 "decoder was already initialized, usually because "
00738 "FLAC__stream_decoder_finish() was not called." );
00739 break;
00740 case FLAC__STREAM_DECODER_INVALID_CALLBACK:
00741 msg_Err( p_dec, "FLAC__stream_decoder_init() was called without "
00742 "all callbacks being set." );
00743 break;
00744 case FLAC__STREAM_DECODER_UNINITIALIZED:
00745 msg_Err( p_dec, "decoder in uninitialized state." );
00746 break;
00747 default:
00748 msg_Err(p_dec, "unknown error" );
00749 }
00750 }
00751 #endif
00752
00753
00754
00755
00756 static int SyncInfo( decoder_t *p_dec, uint8_t *p_buf,
00757 unsigned int * pi_channels,
00758 unsigned int * pi_channels_conf,
00759 unsigned int * pi_sample_rate,
00760 int * pi_bits_per_sample )
00761 {
00762 decoder_sys_t *p_sys = p_dec->p_sys;
00763 int i_header, i_temp, i_read;
00764 int i_blocksize = 0, i_blocksize_hint = 0, i_sample_rate_hint = 0;
00765 uint64_t i_sample_number = 0;
00766
00767 vlc_bool_t b_variable_blocksize = ( p_sys->b_stream_info &&
00768 p_sys->stream_info.min_blocksize != p_sys->stream_info.max_blocksize );
00769 vlc_bool_t b_fixed_blocksize = ( p_sys->b_stream_info &&
00770 p_sys->stream_info.min_blocksize == p_sys->stream_info.max_blocksize );
00771
00772
00773 if( p_buf[0] != 0xFF || p_buf[1] != 0xF8 ) return 0;
00774
00775
00776 if( p_buf[2] == 0xff || p_buf[3] == 0xFF ) return 0;
00777
00778
00779 switch( i_temp = p_buf[2] >> 4 )
00780 {
00781 case 0:
00782 if( b_fixed_blocksize )
00783 i_blocksize = p_sys->stream_info.min_blocksize;
00784 else return 0;
00785 break;
00786
00787 case 1:
00788 i_blocksize = 192;
00789 break;
00790
00791 case 2:
00792 case 3:
00793 case 4:
00794 case 5:
00795 i_blocksize = 576 << (i_temp - 2);
00796 break;
00797
00798 case 6:
00799 case 7:
00800 i_blocksize_hint = i_temp;
00801 break;
00802
00803 case 8:
00804 case 9:
00805 case 10:
00806 case 11:
00807 case 12:
00808 case 13:
00809 case 14:
00810 case 15:
00811 i_blocksize = 256 << (i_temp - 8);
00812 break;
00813 }
00814
00815
00816 switch( i_temp = p_buf[2] & 0x0f )
00817 {
00818 case 0:
00819 if( p_sys->b_stream_info )
00820 *pi_sample_rate = p_sys->stream_info.sample_rate;
00821 else return 0;
00822 break;
00823
00824 case 1:
00825 case 2:
00826 case 3:
00827 return 0;
00828 break;
00829
00830 case 4:
00831 *pi_sample_rate = 8000;
00832 break;
00833
00834 case 5:
00835 *pi_sample_rate = 16000;
00836 break;
00837
00838 case 6:
00839 *pi_sample_rate = 22050;
00840 break;
00841
00842 case 7:
00843 *pi_sample_rate = 24000;
00844 break;
00845
00846 case 8:
00847 *pi_sample_rate = 32000;
00848 break;
00849
00850 case 9:
00851 *pi_sample_rate = 44100;
00852 break;
00853
00854 case 10:
00855 *pi_sample_rate = 48000;
00856 break;
00857
00858 case 11:
00859 *pi_sample_rate = 96000;
00860 break;
00861
00862 case 12:
00863 case 13:
00864 case 14:
00865 i_sample_rate_hint = i_temp;
00866 break;
00867
00868 case 15:
00869 return 0;
00870 }
00871
00872
00873 i_temp = (unsigned)(p_buf[3] >> 4);
00874 if( i_temp & 8 )
00875 {
00876 #ifdef USE_LIBFLAC
00877 int i_channel_assignment;
00878
00879 switch( i_temp & 7 )
00880 {
00881 case 0:
00882 i_channel_assignment = FLAC__CHANNEL_ASSIGNMENT_LEFT_SIDE;
00883 break;
00884 case 1:
00885 i_channel_assignment = FLAC__CHANNEL_ASSIGNMENT_RIGHT_SIDE;
00886 break;
00887 case 2:
00888 i_channel_assignment = FLAC__CHANNEL_ASSIGNMENT_MID_SIDE;
00889 break;
00890 default:
00891 return 0;
00892 break;
00893 }
00894 #endif
00895
00896 *pi_channels = 2;
00897 }
00898 else
00899 {
00900 *pi_channels = i_temp + 1;
00901 *pi_channels_conf = pi_channels_maps[ *pi_channels ];
00902 }
00903
00904
00905 switch( i_temp = (unsigned)(p_buf[3] & 0x0e) >> 1 )
00906 {
00907 case 0:
00908 if( p_sys->b_stream_info )
00909 *pi_bits_per_sample = p_sys->stream_info.bits_per_sample;
00910 else
00911 return 0;
00912 break;
00913
00914 case 1:
00915 *pi_bits_per_sample = 8;
00916 break;
00917
00918 case 2:
00919 *pi_bits_per_sample = 12;
00920 break;
00921
00922 case 4:
00923 *pi_bits_per_sample = 16;
00924 break;
00925
00926 case 5:
00927 *pi_bits_per_sample = 20;
00928 break;
00929
00930 case 6:
00931 *pi_bits_per_sample = 24;
00932 break;
00933
00934 case 3:
00935 case 7:
00936 return 0;
00937 break;
00938 }
00939
00940
00941 if( p_buf[3] & 0x01 ) return 0;
00942
00943
00944 i_header = 4;
00945
00946
00947 if( i_blocksize_hint && b_variable_blocksize )
00948 {
00949 i_sample_number = read_utf8( &p_buf[i_header++], &i_read );
00950 if( i_sample_number == I64C(0xffffffffffffffff) ) return 0;
00951 }
00952 else
00953 {
00954 i_sample_number = read_utf8( &p_buf[i_header++], &i_read );
00955 if( i_sample_number == I64C(0xffffffffffffffff) ) return 0;
00956
00957 if( p_sys->b_stream_info )
00958 i_sample_number *= p_sys->stream_info.min_blocksize;
00959 }
00960
00961 i_header += i_read;
00962
00963
00964 if( i_blocksize_hint )
00965 {
00966 int i_val1 = p_buf[i_header++];
00967 if( i_blocksize_hint == 7 )
00968 {
00969 int i_val2 = p_buf[i_header++];
00970 i_val1 = (i_val1 << 8) | i_val2;
00971 }
00972 i_blocksize = i_val1 + 1;
00973 }
00974
00975
00976 if( i_sample_rate_hint )
00977 {
00978 int i_val1 = p_buf[i_header++];
00979 if( i_sample_rate_hint != 12 )
00980 {
00981 int i_val2 = p_buf[i_header++];
00982 i_val1 = (i_val1 << 8) | i_val2;
00983 }
00984 if( i_sample_rate_hint == 12 ) *pi_sample_rate = i_val1 * 1000;
00985 else if( i_sample_rate_hint == 13 ) *pi_sample_rate = i_val1;
00986 else *pi_sample_rate = i_val1 * 10;
00987 }
00988
00989
00990 if( flac_crc8( p_buf, i_header ) != p_buf[i_header] )
00991 {
00992 return 0;
00993 }
00994
00995 return i_blocksize;
00996 }
00997
00998
00999 static uint64_t read_utf8( const uint8_t *p_buf, int *pi_read )
01000 {
01001 uint64_t i_result = 0;
01002 unsigned i, j;
01003
01004 if( !(p_buf[0] & 0x80) )
01005 {
01006 i_result = p_buf[0];
01007 i = 0;
01008 }
01009 else if( p_buf[0] & 0xC0 && !(p_buf[0] & 0x20) )
01010 {
01011 i_result = p_buf[0] & 0x1F;
01012 i = 1;
01013 }
01014 else if( p_buf[0] & 0xE0 && !(p_buf[0] & 0x10) )
01015 {
01016 i_result = p_buf[0] & 0x0F;
01017 i = 2;
01018 }
01019 else if( p_buf[0] & 0xF0 && !(p_buf[0] & 0x08) )
01020 {
01021 i_result = p_buf[0] & 0x07;
01022 i = 3;
01023 }
01024 else if( p_buf[0] & 0xF8 && !(p_buf[0] & 0x04) )
01025 {
01026 i_result = p_buf[0] & 0x03;
01027 i = 4;
01028 }
01029 else if( p_buf[0] & 0xFC && !(p_buf[0] & 0x02) )
01030 {
01031 i_result = p_buf[0] & 0x01;
01032 i = 5;
01033 }
01034 else if( p_buf[0] & 0xFE && !(p_buf[0] & 0x01) )
01035 {
01036 i_result = 0;
01037 i = 6;
01038 }
01039 else {
01040 return I64C(0xffffffffffffffff);
01041 }
01042
01043 for( j = 1; j <= i; j++ )
01044 {
01045 if( !(p_buf[j] & 0x80) || (p_buf[j] & 0x40) )
01046 {
01047 return I64C(0xffffffffffffffff);
01048 }
01049 i_result <<= 6;
01050 i_result |= (p_buf[j] & 0x3F);
01051 }
01052
01053 *pi_read = i;
01054 return i_result;
01055 }
01056
01057
01058 static uint8_t const flac_crc8_table[256] = {
01059 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
01060 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
01061 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
01062 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
01063 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5,
01064 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
01065 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
01066 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
01067 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
01068 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
01069 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2,
01070 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
01071 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32,
01072 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
01073 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
01074 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
01075 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C,
01076 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
01077 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
01078 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
01079 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
01080 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
01081 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
01082 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
01083 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B,
01084 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
01085 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
01086 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
01087 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
01088 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
01089 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
01090 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
01091 };
01092
01093 static uint8_t flac_crc8( const uint8_t *data, unsigned len )
01094 {
01095 uint8_t crc = 0;
01096
01097 while(len--)
01098 crc = flac_crc8_table[crc ^ *data++];
01099
01100 return crc;
01101 }
01102
01103 #ifdef USE_LIBFLAC
01104
01105
01106
01107 struct encoder_sys_t
01108 {
01109
01110
01111
01112 int i_headers;
01113
01114 int i_samples_delay;
01115 int i_channels;
01116
01117 FLAC__int32 *p_buffer;
01118 int i_buffer;
01119
01120 block_t *p_chain;
01121
01122
01123
01124
01125 FLAC__StreamEncoder *p_flac;
01126 FLAC__StreamMetadata_StreamInfo stream_info;
01127
01128
01129
01130
01131 mtime_t i_pts;
01132 };
01133
01134 #define STREAMINFO_SIZE 38
01135
01136 static block_t *Encode( encoder_t *, aout_buffer_t * );
01137
01138 static FLAC__StreamEncoderWriteStatus
01139 EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
01140 const FLAC__byte buffer[],
01141 unsigned bytes, unsigned samples,
01142 unsigned current_frame, void *client_data );
01143
01144 static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
01145 const FLAC__StreamMetadata *metadata,
01146 void *client_data );
01147
01148
01149
01150
01151 static int OpenEncoder( vlc_object_t *p_this )
01152 {
01153 encoder_t *p_enc = (encoder_t *)p_this;
01154 encoder_sys_t *p_sys;
01155
01156 if( p_enc->fmt_out.i_codec != VLC_FOURCC('f','l','a','c') &&
01157 !p_enc->b_force )
01158 {
01159 return VLC_EGENERIC;
01160 }
01161
01162
01163 if( ( p_sys = (encoder_sys_t *)malloc(sizeof(encoder_sys_t)) ) == NULL )
01164 {
01165 msg_Err( p_enc, "out of memory" );
01166 return VLC_EGENERIC;
01167 }
01168 p_enc->p_sys = p_sys;
01169 p_enc->pf_encode_audio = Encode;
01170 p_enc->fmt_out.i_codec = VLC_FOURCC('f','l','a','c');
01171
01172 p_sys->i_headers = 0;
01173 p_sys->p_buffer = 0;
01174 p_sys->i_buffer = 0;
01175 p_sys->i_samples_delay = 0;
01176
01177
01178 p_sys->p_flac = FLAC__stream_encoder_new();
01179
01180 FLAC__stream_encoder_set_streamable_subset( p_sys->p_flac, 1 );
01181 FLAC__stream_encoder_set_channels( p_sys->p_flac,
01182 p_enc->fmt_in.audio.i_channels );
01183 FLAC__stream_encoder_set_sample_rate( p_sys->p_flac,
01184 p_enc->fmt_in.audio.i_rate );
01185 FLAC__stream_encoder_set_bits_per_sample( p_sys->p_flac, 16 );
01186 p_enc->fmt_in.i_codec = AOUT_FMT_S16_NE;
01187
01188 FLAC__stream_encoder_set_write_callback( p_sys->p_flac,
01189 EncoderWriteCallback );
01190 FLAC__stream_encoder_set_metadata_callback( p_sys->p_flac,
01191 EncoderMetadataCallback );
01192 FLAC__stream_encoder_set_client_data( p_sys->p_flac, p_enc );
01193
01194
01195 p_sys->p_chain = 0;
01196 FLAC__stream_encoder_init( p_sys->p_flac );
01197
01198 return VLC_SUCCESS;
01199 }
01200
01201
01202
01203
01204
01205
01206 static block_t *Encode( encoder_t *p_enc, aout_buffer_t *p_aout_buf )
01207 {
01208 encoder_sys_t *p_sys = p_enc->p_sys;
01209 block_t *p_chain;
01210 int i;
01211
01212 p_sys->i_pts = p_aout_buf->start_date -
01213 (mtime_t)1000000 * (mtime_t)p_sys->i_samples_delay /
01214 (mtime_t)p_enc->fmt_in.audio.i_rate;
01215
01216 p_sys->i_samples_delay += p_aout_buf->i_nb_samples;
01217
01218
01219 if( p_sys->i_buffer < p_aout_buf->i_nb_bytes * 2 )
01220 {
01221 p_sys->p_buffer =
01222 realloc( p_sys->p_buffer, p_aout_buf->i_nb_bytes * 2 );
01223 p_sys->i_buffer = p_aout_buf->i_nb_bytes * 2;
01224 }
01225
01226 for( i = 0 ; i < p_aout_buf->i_nb_bytes / 2 ; i++ )
01227 {
01228 p_sys->p_buffer[i]= ((int16_t *)p_aout_buf->p_buffer)[i];
01229 }
01230
01231 FLAC__stream_encoder_process_interleaved( p_sys->p_flac, p_sys->p_buffer,
01232 p_aout_buf->i_nb_samples );
01233
01234 p_chain = p_sys->p_chain;
01235 p_sys->p_chain = 0;
01236
01237 return p_chain;
01238 }
01239
01240
01241
01242
01243 static void CloseEncoder( vlc_object_t *p_this )
01244 {
01245 encoder_t *p_enc = (encoder_t *)p_this;
01246 encoder_sys_t *p_sys = p_enc->p_sys;
01247
01248 FLAC__stream_encoder_delete( p_sys->p_flac );
01249
01250 if( p_sys->p_buffer ) free( p_sys->p_buffer );
01251 free( p_sys );
01252 }
01253
01254
01255
01256
01257 static void EncoderMetadataCallback( const FLAC__StreamEncoder *encoder,
01258 const FLAC__StreamMetadata *metadata,
01259 void *client_data )
01260 {
01261 encoder_t *p_enc = (encoder_t *)client_data;
01262
01263 msg_Err( p_enc, "MetadataCallback: %i", metadata->type );
01264 return;
01265 }
01266
01267
01268
01269
01270 static FLAC__StreamEncoderWriteStatus
01271 EncoderWriteCallback( const FLAC__StreamEncoder *encoder,
01272 const FLAC__byte buffer[],
01273 unsigned bytes, unsigned samples,
01274 unsigned current_frame, void *client_data )
01275 {
01276 encoder_t *p_enc = (encoder_t *)client_data;
01277 encoder_sys_t *p_sys = p_enc->p_sys;
01278 block_t *p_block;
01279
01280 if( samples == 0 )
01281 {
01282 if( p_sys->i_headers == 1 )
01283 {
01284 msg_Dbg( p_enc, "Writing STREAMINFO: %i", bytes );
01285
01286
01287 p_enc->fmt_out.i_extra = STREAMINFO_SIZE + 4;
01288 p_enc->fmt_out.p_extra = malloc( STREAMINFO_SIZE + 4 );
01289 memcpy( p_enc->fmt_out.p_extra, "fLaC", 4 );
01290 memcpy( ((uint8_t *)p_enc->fmt_out.p_extra) + 4, buffer,
01291 STREAMINFO_SIZE );
01292
01293
01294 ((uint8_t*)p_enc->fmt_out.p_extra)[4] |= 0x80;
01295 }
01296 p_sys->i_headers++;
01297 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
01298 }
01299
01300 p_block = block_New( p_enc, bytes );
01301 memcpy( p_block->p_buffer, buffer, bytes );
01302
01303 p_block->i_dts = p_block->i_pts = p_sys->i_pts;
01304
01305 p_sys->i_samples_delay -= samples;
01306
01307 p_block->i_length = (mtime_t)1000000 *
01308 (mtime_t)samples / (mtime_t)p_enc->fmt_in.audio.i_rate;
01309
01310
01311 p_sys->i_pts += p_block->i_length;
01312
01313 block_ChainAppend( &p_sys->p_chain, p_block );
01314
01315 return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
01316 }
01317 #endif