/*********************************************************************** * local routines **********************************************************************/ FLAC__bool safe_decoder_init_(const char *filename, FLAC__FileDecoder *decoder) { if(decoder == 0) { MessageBox(mod_.hMainWindow, "Decoder instance is NULL", "ERROR initializing decoder", 0); return false; } safe_decoder_finish_(decoder); FLAC__file_decoder_set_md5_checking(decoder, false); FLAC__file_decoder_set_filename(decoder, filename); FLAC__file_decoder_set_write_callback(decoder, write_callback_); FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_); FLAC__file_decoder_set_error_callback(decoder, error_callback_); FLAC__file_decoder_set_client_data(decoder, &file_info_); if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) { MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR initializing decoder", 0); return false; } file_info_.abort_flag = false; if(!FLAC__file_decoder_process_until_end_of_metadata(decoder)) { MessageBox(mod_.hMainWindow, FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)], "ERROR processing metadata", 0); return false; } if(file_info_.abort_flag) { /* metadata callback already popped up the error dialog */ return false; } return true; }
FLAC__bool FLAC_plugin__decoder_init(FLAC__FileDecoder *decoder, const char *filename, FLAC__int64 filesize, file_info_struct *file_info, output_config_t *config) { FLAC__ASSERT(decoder); FLAC_plugin__decoder_finish(decoder); /* init decoder */ FLAC__file_decoder_set_md5_checking(decoder, false); FLAC__file_decoder_set_filename(decoder, filename); FLAC__file_decoder_set_metadata_ignore_all(decoder); FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO); FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_VORBIS_COMMENT); FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback); FLAC__file_decoder_set_write_callback(decoder, write_callback); FLAC__file_decoder_set_error_callback(decoder, error_callback); FLAC__file_decoder_set_client_data(decoder, file_info); if (FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) { FLAC_plugin__show_error("Error while initializing decoder (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]); return false; } /* process */ cfg = *config; wide_samples_in_reservoir_ = 0; file_info->is_playing = false; file_info->abort_flag = false; file_info->has_replaygain = false; if (!FLAC__file_decoder_process_until_end_of_metadata(decoder)) { FLAC_plugin__show_error("Error while processing metadata (%s).", FLAC__FileDecoderStateString[FLAC__file_decoder_get_state(decoder)]); return false; } /* check results */ if (file_info->abort_flag) return false; /* metadata callback already popped up the error dialog */ /* init replaygain */ file_info->output_bits_per_sample = file_info->has_replaygain && cfg.replaygain.enable ? cfg.resolution.replaygain.bps_out : cfg.resolution.normal.dither_24_to_16 ? min(file_info->bits_per_sample, 16) : file_info->bits_per_sample; if (file_info->has_replaygain && cfg.replaygain.enable && cfg.resolution.replaygain.dither) FLAC__replaygain_synthesis__init_dither_context(&file_info->dither_context, file_info->bits_per_sample, cfg.resolution.replaygain.noise_shaping); /* more inits */ file_info->eof = false; file_info->seek_to = -1; file_info->is_playing = true; file_info->average_bps = (unsigned)(filesize / (125.*file_info->total_samples/file_info->sample_rate)); bh_index_last_w = 0; bh_index_last_o = BITRATE_HIST_SIZE; decode_position = 0; decode_position_last = 0; written_time_last = 0; return true; }
const char *grabbag__replaygain_analyze_file(const char *filename, float *title_gain, float *title_peak) { DecoderInstance instance; FLAC__FileDecoder *decoder = FLAC__file_decoder_new(); if(0 == decoder) return "memory allocation error"; instance.error = false; /* It does these three by default but lets be explicit: */ FLAC__file_decoder_set_md5_checking(decoder, false); FLAC__file_decoder_set_metadata_ignore_all(decoder); FLAC__file_decoder_set_metadata_respond(decoder, FLAC__METADATA_TYPE_STREAMINFO); FLAC__file_decoder_set_filename(decoder, filename); FLAC__file_decoder_set_write_callback(decoder, write_callback_); FLAC__file_decoder_set_metadata_callback(decoder, metadata_callback_); FLAC__file_decoder_set_error_callback(decoder, error_callback_); FLAC__file_decoder_set_client_data(decoder, &instance); if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) { FLAC__file_decoder_delete(decoder); return "initializing decoder"; } if(!FLAC__file_decoder_process_until_end_of_file(decoder) || instance.error) { FLAC__file_decoder_delete(decoder); return "decoding file"; } FLAC__file_decoder_delete(decoder); grabbag__replaygain_get_title(title_gain, title_peak); return 0; }
static FLAC__bool seek_barrage_native_flac(const char *filename, off_t filesize, unsigned count) { FLAC__FileDecoder *decoder; decoder_client_data_struct decoder_client_data; unsigned i; long int n; decoder_client_data.got_data = false; decoder_client_data.total_samples = 0; decoder_client_data.ignore_errors = false; decoder_client_data.error_occurred = false; printf("\n+++ seek test: FLAC__FileDecoder\n\n"); decoder = FLAC__file_decoder_new(); if(0 == decoder) return die_("FLAC__file_decoder_new() FAILED, returned NULL\n"); if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_)) return die_f_("FLAC__file_decoder_set_write_callback() FAILED", decoder); if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_)) return die_f_("FLAC__file_decoder_set_metadata_callback() FAILED", decoder); if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_)) return die_f_("FLAC__file_decoder_set_error_callback() FAILED", decoder); if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data)) return die_f_("FLAC__file_decoder_set_client_data() FAILED", decoder); if(!FLAC__file_decoder_set_filename(decoder, filename)) return die_f_("FLAC__file_decoder_set_filename() FAILED", decoder); if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) return die_f_("FLAC__file_decoder_init() FAILED", decoder); if(!FLAC__file_decoder_process_until_end_of_metadata(decoder)) return die_f_("FLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder); printf("file's total_samples is %llu\n", decoder_client_data.total_samples); #if !defined _MSC_VER && !defined __MINGW32__ && !defined __EMX__ if (decoder_client_data.total_samples > (FLAC__uint64)RAND_MAX) { printf("ERROR: must be total_samples < %u\n", (unsigned)RAND_MAX); return false; } #endif n = (long int)decoder_client_data.total_samples; /* if we don't have a total samples count, just guess based on the file size */ /* @@@ should get it from last page's granulepos */ if(n == 0) { /* 8 would imply no compression, 9 guarantees that we will get some samples off the end of the stream to test that case */ n = 9 * filesize / (decoder_client_data.channels * decoder_client_data.bits_per_sample); #if !defined _MSC_VER && !defined __MINGW32__ if(n > RAND_MAX) n = RAND_MAX; #endif } printf("Begin seek barrage, count=%u\n", count); for (i = 0; !stop_signal_ && (count == 0 || i < count); i++) { FLAC__uint64 pos; /* for the first 10, seek to the first 10 samples */ if (n >= 10 && i < 10) { pos = i; } /* for the second 10, seek to the last 10 samples */ else if (n >= 10 && i < 20) { pos = n - 1 - (i-10); } /* for the third 10, seek past the end and make sure we fail properly as expected */ else if (i < 30) { pos = n + (i-20); } else { #if !defined _MSC_VER && !defined __MINGW32__ pos = (FLAC__uint64)(random() % n); #else /* RAND_MAX is only 32767 in my MSVC */ pos = (FLAC__uint64)((rand()<<15|rand()) % n); #endif } printf("seek(%llu)... ", pos); fflush(stdout); if(!FLAC__file_decoder_seek_absolute(decoder, pos)) { if(pos < (FLAC__uint64)n && decoder_client_data.total_samples != 0) return die_f_("FLAC__file_decoder_seek_absolute() FAILED", decoder); else if(decoder_client_data.total_samples == 0) printf("seek failed, assuming it was past EOF... "); else printf("seek past end failed as expected... "); /* hack to work around a deficiency in the seek API's behavior */ /* seeking past EOF sets the file decoder state to non-OK and there's no ..._flush() or ..._reset() call to reset it */ if(!FLAC__file_decoder_finish(decoder)) return die_f_("FLAC__file_decoder_finish() FAILED", decoder); if(!FLAC__file_decoder_set_write_callback(decoder, file_decoder_write_callback_)) return die_f_("FLAC__file_decoder_set_write_callback() FAILED", decoder); if(!FLAC__file_decoder_set_metadata_callback(decoder, file_decoder_metadata_callback_)) return die_f_("FLAC__file_decoder_set_metadata_callback() FAILED", decoder); if(!FLAC__file_decoder_set_error_callback(decoder, file_decoder_error_callback_)) return die_f_("FLAC__file_decoder_set_error_callback() FAILED", decoder); if(!FLAC__file_decoder_set_client_data(decoder, &decoder_client_data)) return die_f_("FLAC__file_decoder_set_client_data() FAILED", decoder); if(!FLAC__file_decoder_set_filename(decoder, filename)) return die_f_("FLAC__file_decoder_set_filename() FAILED", decoder); if(FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) return die_f_("FLAC__file_decoder_init() FAILED", decoder); if(!FLAC__file_decoder_process_until_end_of_metadata(decoder)) return die_f_("FLAC__file_decoder_process_until_end_of_metadata() FAILED", decoder); } else { printf("decode_frame... "); fflush(stdout); if(!FLAC__file_decoder_process_single(decoder)) return die_f_("FLAC__file_decoder_process_single() FAILED", decoder); printf("decode_frame... "); fflush(stdout); if(!FLAC__file_decoder_process_single(decoder)) return die_f_("FLAC__file_decoder_process_single() FAILED", decoder); } printf("OK\n"); fflush(stdout); } if(FLAC__file_decoder_get_state(decoder) != FLAC__FILE_DECODER_UNINITIALIZED) { if(!FLAC__file_decoder_finish(decoder)) return die_f_("FLAC__file_decoder_finish() FAILED", decoder); } printf("\nPASSED!\n"); return true; }
static int start_read(sox_format_t * const ft) { priv_t * p = (priv_t *)ft->priv; lsx_debug("API version %u", FLAC_API_VERSION_CURRENT); p->decoder = FLAC__stream_decoder_new(); if (p->decoder == NULL) { lsx_fail_errno(ft, SOX_ENOMEM, "FLAC ERROR creating the decoder instance"); return SOX_EOF; } FLAC__stream_decoder_set_md5_checking(p->decoder, sox_true); FLAC__stream_decoder_set_metadata_respond_all(p->decoder); #if FLAC_API_VERSION_CURRENT <= 7 /* This is wrong: not using SoX IO, and there will be 2 FILEs open; * however, it's an old FLAC API, so not worth fixing now. */ FLAC__file_decoder_set_filename(p->decoder, ft->filename); FLAC__file_decoder_set_write_callback(p->decoder, FLAC__frame_decode_callback); FLAC__file_decoder_set_metadata_callback(p->decoder, FLAC__decoder_metadata_callback); FLAC__file_decoder_set_error_callback(p->decoder, FLAC__decoder_error_callback); FLAC__file_decoder_set_client_data(p->decoder, ft); if (FLAC__file_decoder_init(p->decoder) != FLAC__FILE_DECODER_OK) { lsx_fail_errno(ft, SOX_EHDR, "FLAC ERROR initialising decoder"); return SOX_EOF; } #else if (FLAC__stream_decoder_init_FILE(p->decoder, ft->fp, /* Not using SoX IO */ FLAC__frame_decode_callback, FLAC__decoder_metadata_callback, FLAC__decoder_error_callback, ft) != FLAC__STREAM_DECODER_INIT_STATUS_OK){ lsx_fail_errno(ft, SOX_EHDR, "FLAC ERROR initialising decoder"); return SOX_EOF; } ft->fp = NULL; /* Transfer ownership of fp to FLAC */ #endif if (!FLAC__stream_decoder_process_until_end_of_metadata(p->decoder)) { lsx_fail_errno(ft, SOX_EHDR, "FLAC ERROR whilst decoding metadata"); return SOX_EOF; } #if FLAC_API_VERSION_CURRENT <= 7 if (FLAC__file_decoder_get_state(p->decoder) != FLAC__FILE_DECODER_OK && FLAC__file_decoder_get_state(p->decoder) != FLAC__FILE_DECODER_END_OF_FILE) { #else if (FLAC__stream_decoder_get_state(p->decoder) > FLAC__STREAM_DECODER_END_OF_STREAM) { #endif lsx_fail_errno(ft, SOX_EHDR, "FLAC ERROR during metadata decoding"); return SOX_EOF; } ft->encoding.encoding = SOX_ENCODING_FLAC; ft->signal.rate = p->sample_rate; ft->encoding.bits_per_sample = p->bits_per_sample; ft->signal.channels = p->channels; ft->signal.length = p->total_samples * p->channels; return SOX_SUCCESS; } static size_t read_samples(sox_format_t * const ft, sox_sample_t * sampleBuffer, size_t const requested) { priv_t * p = (priv_t *)ft->priv; size_t actual = 0; while (!p->eof && actual < requested) { if (p->wide_sample_number >= p->number_of_wide_samples) FLAC__stream_decoder_process_single(p->decoder); if (p->wide_sample_number >= p->number_of_wide_samples) p->eof = sox_true; else { unsigned channel; for (channel = 0; channel < p->channels; channel++, actual++) { FLAC__int32 d = p->decoded_wide_samples[channel][p->wide_sample_number]; switch (p->bits_per_sample) { case 8: *sampleBuffer++ = SOX_SIGNED_8BIT_TO_SAMPLE(d,); break; case 16: *sampleBuffer++ = SOX_SIGNED_16BIT_TO_SAMPLE(d,); break; case 24: *sampleBuffer++ = SOX_SIGNED_24BIT_TO_SAMPLE(d,); break; case 32: *sampleBuffer++ = SOX_SIGNED_32BIT_TO_SAMPLE(d,); break; } } ++p->wide_sample_number; } } return actual; }
static libaudio_prop_t * loader_flc_open_prop(const char *filename, libaudio_prop_t *prop) { flc_data_t *data; FLAC__FileDecoder *decoder; data = (flc_data_t *)malloc(sizeof(flc_data_t)); if (data == NULL) return NULL; decoder = FLAC__file_decoder_new(); if (decoder == NULL) return NULL; if (!flac_init(decoder, data, filename)) { FLAC__file_decoder_delete(decoder); free(data); return NULL; } if (FLAC__file_decoder_init(decoder) != FLAC__FILE_DECODER_OK) { FLAC__file_decoder_delete(decoder); free(data); return NULL; } /* * The metadata callback should fill the sample_rate, channels * and bits_per_sample values in our data structure. */ data->sample_rate = 0; if (!FLAC__file_decoder_process_metadata(decoder)) { FLAC__file_decoder_delete(decoder); free(data); return NULL; } if (data->sample_rate == 0) { /* * We got no stream info while processing the metadata, odd. */ FLAC__file_decoder_delete(decoder); free(data); return NULL; } data->eof = 0; data->idx = 0; data->size = 0; data->decoder = decoder; prop->type = LIBAUDIO_TYPE_FLC; prop->loader = self; prop->loader_data = data; prop->sample_freq = data->sample_rate; prop->sample_size = data->bits_per_sample; prop->channels = data->channels; return prop; }