OMX_ERRORTYPE SoftFlacEncoder::configureEncoder() { ALOGV("SoftFlacEncoder::configureEncoder() numChannel=%d, sampleRate=%d", mNumChannels, mSampleRate); if (mSignalledError || (mFlacStreamEncoder == NULL)) { ALOGE("can't configure encoder: no encoder or invalid state"); return OMX_ErrorInvalidState; } FLAC__bool ok = true; ok = ok && FLAC__stream_encoder_set_channels(mFlacStreamEncoder, mNumChannels); ok = ok && FLAC__stream_encoder_set_sample_rate(mFlacStreamEncoder, mSampleRate); ok = ok && FLAC__stream_encoder_set_bits_per_sample(mFlacStreamEncoder, 16); ok = ok && FLAC__stream_encoder_set_compression_level(mFlacStreamEncoder, (unsigned)mCompressionLevel); ok = ok && FLAC__stream_encoder_set_verify(mFlacStreamEncoder, false); if (!ok) { goto return_result; } ok &= FLAC__STREAM_ENCODER_INIT_STATUS_OK == FLAC__stream_encoder_init_stream(mFlacStreamEncoder, flacEncoderWriteCallback /*write_callback*/, NULL /*seek_callback*/, NULL /*tell_callback*/, NULL /*metadata_callback*/, (void *) this /*client_data*/); return_result: if (ok) { ALOGV("encoder successfully configured"); return OMX_ErrorNone; } else { ALOGE("unknown error when configuring encoder"); return OMX_ErrorUndefined; } }
static gint flac_open(void) { flac_encoder = FLAC__stream_encoder_new(); FLAC__stream_encoder_set_channels(flac_encoder, input.channels); FLAC__stream_encoder_set_sample_rate(flac_encoder, input.frequency); FLAC__stream_encoder_init_stream(flac_encoder, flac_write_cb, flac_seek_cb, flac_tell_cb, NULL, output_file); if (tuple) { FLAC__StreamMetadata *meta; meta = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); insert_vorbis_comment (meta, "title", tuple, FIELD_TITLE); insert_vorbis_comment (meta, "artist", tuple, FIELD_ARTIST); insert_vorbis_comment (meta, "album", tuple, FIELD_ALBUM); insert_vorbis_comment (meta, "genre", tuple, FIELD_GENRE); insert_vorbis_comment (meta, "comment", tuple, FIELD_COMMENT); insert_vorbis_comment (meta, "date", tuple, FIELD_DATE); insert_vorbis_comment (meta, "year", tuple, FIELD_YEAR); insert_vorbis_comment (meta, "tracknumber", tuple, FIELD_TRACK_NUMBER); FLAC__stream_encoder_set_metadata(flac_encoder, &meta, 1); } return 1; }
void StartStream() { ASSERT(inited && !started); FLAC__stream_encoder_init_stream(encoder, FLACWriteCallback, FLACSeekCallback, FLACTellCallback, nullptr, this); started = true; ASSERT(inited && started); }
void FlacAudioEncoder::setup(AudioEncoderProfile::SharedPtr profile, AudioMetaData::SharedPtr metadata, uint32_t data_size) { _profile = profile; // allocate the encoder _encoder = FLAC__stream_encoder_new(); if (_encoder == nullptr) { THROW_EXCEPTION(AudioEncoderException, "ERROR: allocating encoder"); return; } if (not FLAC__stream_encoder_set_channels(_encoder, profile->channels())) { THROW_EXCEPTION(AudioEncoderException, "ERROR: could not set channel count"); return; } if (not FLAC__stream_encoder_set_sample_rate(_encoder, profile->sample_rate())) { THROW_EXCEPTION(AudioEncoderException, "ERROR: could not set sample rate"); return; } if (not FLAC__stream_encoder_set_bits_per_sample(_encoder, profile->bits_per_sample())) { THROW_EXCEPTION(AudioEncoderException, "ERROR: could not set sample width"); return; } if (not FLAC__stream_encoder_set_compression_level(_encoder, profile->quality_level())) { THROW_EXCEPTION(AudioEncoderException, "ERROR: could not set compression level"); return; } FLAC__stream_encoder_set_verify(_encoder, true); //FLAC__stream_encoder_set_total_samples_estimate(encoder, total_samples); set_tags(metadata); FLAC__StreamEncoderInitStatus init_status = FLAC__stream_encoder_init_stream(_encoder, write_callback, seek_callback, tell_callback, nullptr, this); if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { THROW_EXCEPTION(AudioEncoderException, "ERROR: could not initialize FLAC encoder"); } }
bool Start(void *ctx, int iInChannels, int iInRate, int iInBits, const char* title, const char* artist, const char* albumartist, const char* album, const char* year, const char* track, const char* genre, const char* comment, int iTrackLength) { flac_context *context = (flac_context *)ctx; if (!context || !context->encoder) return false; // we accept only 2 / 44100 / 16 atm if (iInChannels != 2 || iInRate != 44100 || iInBits != 16) return false; FLAC__bool ok = 1; ok &= FLAC__stream_encoder_set_verify(context->encoder, true); ok &= FLAC__stream_encoder_set_channels(context->encoder, iInChannels); ok &= FLAC__stream_encoder_set_bits_per_sample(context->encoder, iInBits); ok &= FLAC__stream_encoder_set_sample_rate(context->encoder, iInRate); ok &= FLAC__stream_encoder_set_total_samples_estimate(context->encoder, iTrackLength / 4); ok &= FLAC__stream_encoder_set_compression_level(context->encoder, level); // now add some metadata FLAC__StreamMetadata_VorbisComment_Entry entry; if (ok) { if ( (context->metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == NULL || (context->metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == NULL || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", artist) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ALBUM", album) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ALBUMARTIST", albumartist) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", title) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "GENRE", genre) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TRACKNUMBER", track) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "DATE", year) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "COMMENT", comment) || !FLAC__metadata_object_vorbiscomment_append_comment(context->metadata[0], entry, false) ) { ok = false; } else { context->metadata[1]->length = 4096; ok = FLAC__stream_encoder_set_metadata(context->encoder, context->metadata, 2); } } // initialize encoder in stream mode if (ok) { FLAC__StreamEncoderInitStatus init_status; init_status = FLAC__stream_encoder_init_stream(context->encoder, write_callback_flac, seek_callback_flac, tell_callback_flac, NULL, context); if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { ok = false; } } if (!ok) { return false; } return true; }
static int flac_output_open(const char *fname, const char *comment) { int fd; int nch; FLAC__StreamMetadata padding; FLAC__StreamMetadata *metadata[4]; int num_metadata = 0; #ifndef LEGACY_FLAC FLAC__StreamEncoderInitStatus init_status; #endif FLAC_ctx *ctx; if (flac_ctx == NULL) flac_session_close(); if (!(flac_ctx = (FLAC_ctx *)calloc(sizeof(FLAC_ctx), 1))) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s", strerror(errno)); return -1; } ctx = flac_ctx; ctx->in_bytes = ctx->out_bytes = 0; if(strcmp(fname, "-") == 0) { fd = 1; /* data to stdout */ if (comment == NULL) comment = "(stdout)"; } else { /* Open the audio file */ fd = open(fname, FILE_OUTPUT_MODE); if(fd < 0) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "%s: %s", fname, strerror(errno)); return -1; } if(comment == NULL) comment = fname; } dpm.fd = fd; nch = (dpm.encoding & PE_MONO) ? 1 : 2; if (0 < flac_options.padding) { padding.is_last = 0; padding.type = FLAC__METADATA_TYPE_PADDING; padding.length = flac_options.padding; metadata[num_metadata++] = &padding; } #ifdef LEGACY_FLAC #ifdef AU_OGGFLAC if (flac_options.isogg) { if ((ctx->encoder.ogg.stream = OggFLAC__stream_encoder_new()) == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create OggFLAC stream"); flac_session_close(); return -1; } OggFLAC__stream_encoder_set_channels(ctx->encoder.ogg.stream, nch); /* 16bps only */ OggFLAC__stream_encoder_set_bits_per_sample(ctx->encoder.ogg.stream, 16); /* set sequential number for serial */ serial_number++; if (serial_number == 1) { srand(time(NULL)); serial_number = rand(); } OggFLAC__stream_encoder_set_serial_number(ctx->encoder.ogg.stream, serial_number); OggFLAC__stream_encoder_set_verify(ctx->encoder.ogg.stream, flac_options.verify); if (!FLAC__format_sample_rate_is_valid(dpm.rate)) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "invalid sampling rate %d", dpm.rate); flac_session_close(); return -1; } OggFLAC__stream_encoder_set_sample_rate(ctx->encoder.ogg.stream, dpm.rate); OggFLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.ogg.stream, flac_options.qlp_coeff_precision); /* expensive! */ OggFLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.ogg.stream, flac_options.qlp_coeff_precision_search); if (nch == 2) { OggFLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.ogg.stream, flac_options.mid_side); OggFLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.ogg.stream, flac_options.adaptive_mid_side); } OggFLAC__stream_encoder_set_max_lpc_order(ctx->encoder.ogg.stream, flac_options.max_lpc_order); OggFLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.ogg.stream, flac_options.min_residual_partition_order); OggFLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.ogg.stream, flac_options.max_residual_partition_order); OggFLAC__stream_encoder_set_blocksize(ctx->encoder.ogg.stream, flac_options.blocksize); OggFLAC__stream_encoder_set_client_data(ctx->encoder.ogg.stream, ctx); if (0 < num_metadata) OggFLAC__stream_encoder_set_metadata(ctx->encoder.ogg.stream, metadata, num_metadata); /* set callback */ OggFLAC__stream_encoder_set_write_callback(ctx->encoder.ogg.stream, ogg_stream_encoder_write_callback); ctx->state.ogg = OggFLAC__stream_encoder_init(ctx->encoder.ogg.stream); if (ctx->state.ogg != OggFLAC__STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create OggFLAC state (%s)", OggFLAC__StreamEncoderStateString[ctx->state.ogg]); flac_session_close(); return -1; } } else #endif /* AU_OGGFLAC */ if (flac_options.seekable) { if ((ctx->encoder.flac.s_stream = FLAC__seekable_stream_encoder_new()) == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC stream"); flac_session_close(); return -1; } FLAC__seekable_stream_encoder_set_channels(ctx->encoder.flac.s_stream, nch); /* 16bps only */ FLAC__seekable_stream_encoder_set_bits_per_sample(ctx->encoder.flac.s_stream, 16); FLAC__seekable_stream_encoder_set_verify(ctx->encoder.flac.s_stream, flac_options.verify); if (!FLAC__format_sample_rate_is_valid(dpm.rate)) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "invalid sampling rate %d", dpm.rate); flac_session_close(); return -1; } FLAC__seekable_stream_encoder_set_sample_rate(ctx->encoder.flac.s_stream, dpm.rate); FLAC__seekable_stream_encoder_set_qlp_coeff_precision(ctx->encoder.flac.s_stream, flac_options.qlp_coeff_precision); /* expensive! */ FLAC__seekable_stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.flac.s_stream, flac_options.qlp_coeff_precision_search); if (nch == 2) { FLAC__seekable_stream_encoder_set_do_mid_side_stereo(ctx->encoder.flac.s_stream, flac_options.mid_side); FLAC__seekable_stream_encoder_set_loose_mid_side_stereo(ctx->encoder.flac.s_stream, flac_options.adaptive_mid_side); } FLAC__seekable_stream_encoder_set_max_lpc_order(ctx->encoder.flac.s_stream, flac_options.max_lpc_order); FLAC__seekable_stream_encoder_set_min_residual_partition_order(ctx->encoder.flac.s_stream, flac_options.min_residual_partition_order); FLAC__seekable_stream_encoder_set_max_residual_partition_order(ctx->encoder.flac.s_stream, flac_options.max_residual_partition_order); FLAC__seekable_stream_encoder_set_blocksize(ctx->encoder.flac.s_stream, flac_options.blocksize); FLAC__seekable_stream_encoder_set_client_data(ctx->encoder.flac.s_stream, ctx); if (0 < num_metadata) FLAC__seekable_stream_encoder_set_metadata(ctx->encoder.flac.s_stream, metadata, num_metadata); /* set callback */ /* FLAC__seekable_stream_encoder_set_metadata_callback(ctx->encoder.flac.s_stream, flac_seekable_stream_encoder_metadata_callback); /* */ #if (!defined(__BORLANDC__) && !defined(__POCC__)) FLAC__stream_encoder_set_metadata_callback(ctx->encoder.flac.s_stream, flac_seekable_stream_encoder_metadata_callback); /* */ #endif FLAC__seekable_stream_encoder_set_write_callback(ctx->encoder.flac.s_stream, flac_seekable_stream_encoder_write_callback); ctx->state.s_flac = FLAC__seekable_stream_encoder_init(ctx->encoder.flac.s_stream); if (ctx->state.s_flac != FLAC__SEEKABLE_STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC state (%s)", FLAC__SeekableStreamEncoderStateString[ctx->state.s_flac]); flac_session_close(); return -1; } } else { if ((ctx->encoder.flac.stream = FLAC__stream_encoder_new()) == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC stream"); flac_session_close(); return -1; } FLAC__stream_encoder_set_channels(ctx->encoder.flac.stream, nch); /* 16bps only */ FLAC__stream_encoder_set_bits_per_sample(ctx->encoder.flac.stream, 16); FLAC__stream_encoder_set_verify(ctx->encoder.flac.stream, flac_options.verify); if (!FLAC__format_sample_rate_is_valid(dpm.rate)) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "invalid sampling rate %d", dpm.rate); flac_session_close(); return -1; } FLAC__stream_encoder_set_sample_rate(ctx->encoder.flac.stream, dpm.rate); FLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision); /* expensive! */ FLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision_search); if (nch == 2) { FLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.flac.stream, flac_options.mid_side); FLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.flac.stream, flac_options.adaptive_mid_side); } FLAC__stream_encoder_set_max_lpc_order(ctx->encoder.flac.stream, flac_options.max_lpc_order); FLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.flac.stream, flac_options.min_residual_partition_order); FLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.flac.stream, flac_options.max_residual_partition_order); FLAC__stream_encoder_set_blocksize(ctx->encoder.flac.stream, flac_options.blocksize); FLAC__stream_encoder_set_client_data(ctx->encoder.flac.stream, ctx); if (0 < num_metadata) FLAC__stream_encoder_set_metadata(ctx->encoder.flac.stream, metadata, num_metadata); /* set callback */ FLAC__stream_encoder_set_metadata_callback(ctx->encoder.flac.stream, flac_stream_encoder_metadata_callback); FLAC__stream_encoder_set_write_callback(ctx->encoder.flac.stream, flac_stream_encoder_write_callback); ctx->state.flac = FLAC__stream_encoder_init(ctx->encoder.flac.stream); if (ctx->state.flac != FLAC__STREAM_ENCODER_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC state (%s)", FLAC__StreamEncoderStateString[ctx->state.flac]); flac_session_close(); return -1; } } #else /* !LEGACY_FLAC */ if ((ctx->encoder.flac.stream = FLAC__stream_encoder_new()) == NULL) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC stream"); flac_session_close(); return -1; } #ifdef AU_OGGFLAC if (flac_options.isogg) { /* set sequential number for serial */ serial_number++; if (serial_number == 1) { srand(time(NULL)); serial_number = rand(); } FLAC__stream_encoder_set_ogg_serial_number(ctx->encoder.flac.stream, serial_number); } #endif /* AU_OGGFLAC */ FLAC__stream_encoder_set_channels(ctx->encoder.flac.stream, nch); /* 16bps only */ FLAC__stream_encoder_set_bits_per_sample(ctx->encoder.flac.stream, 16); FLAC__stream_encoder_set_verify(ctx->encoder.flac.stream, flac_options.verify); if (!FLAC__format_sample_rate_is_valid(dpm.rate)) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "invalid sampling rate %d", dpm.rate); flac_session_close(); return -1; } FLAC__stream_encoder_set_sample_rate(ctx->encoder.flac.stream, dpm.rate); FLAC__stream_encoder_set_qlp_coeff_precision(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision); /* expensive! */ FLAC__stream_encoder_set_do_qlp_coeff_prec_search(ctx->encoder.flac.stream, flac_options.qlp_coeff_precision_search); if (nch == 2) { FLAC__stream_encoder_set_do_mid_side_stereo(ctx->encoder.flac.stream, flac_options.mid_side); FLAC__stream_encoder_set_loose_mid_side_stereo(ctx->encoder.flac.stream, flac_options.adaptive_mid_side); } FLAC__stream_encoder_set_max_lpc_order(ctx->encoder.flac.stream, flac_options.max_lpc_order); FLAC__stream_encoder_set_min_residual_partition_order(ctx->encoder.flac.stream, flac_options.min_residual_partition_order); FLAC__stream_encoder_set_max_residual_partition_order(ctx->encoder.flac.stream, flac_options.max_residual_partition_order); FLAC__stream_encoder_set_blocksize(ctx->encoder.flac.stream, flac_options.blocksize); if (0 < num_metadata) FLAC__stream_encoder_set_metadata(ctx->encoder.flac.stream, metadata, num_metadata); #ifdef AU_OGGFLAC if (flac_options.isogg) init_status = FLAC__stream_encoder_init_ogg_stream(ctx->encoder.flac.stream, flac_stream_encoder_write_callback, NULL, NULL, NULL, ctx); else #endif init_status = FLAC__stream_encoder_init_stream(ctx->encoder.flac.stream, flac_stream_encoder_write_callback, NULL, NULL, NULL, ctx); if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "cannot create FLAC encoder (init status: %s)", FLAC__StreamEncoderInitStatusString[init_status]); flac_session_close(); return -1; } #endif return 0; }
FLAC__bool file_utils__generate_flacfile(FLAC__bool is_ogg, const char *output_filename, off_t *output_filesize, unsigned length, const FLAC__StreamMetadata *streaminfo, FLAC__StreamMetadata **metadata, unsigned num_metadata) { FLAC__int32 samples[1024]; FLAC__StreamEncoder *encoder; FLAC__StreamEncoderInitStatus init_status; encoder_client_struct encoder_client_data; unsigned i, n; FLAC__ASSERT(0 != output_filename); FLAC__ASSERT(0 != streaminfo); FLAC__ASSERT(streaminfo->type == FLAC__METADATA_TYPE_STREAMINFO); FLAC__ASSERT((streaminfo->is_last && num_metadata == 0) || (!streaminfo->is_last && num_metadata > 0)); if(0 == (encoder_client_data.file = fopen(output_filename, "wb"))) return false; encoder = FLAC__stream_encoder_new(); if(0 == encoder) { fclose(encoder_client_data.file); return false; } FLAC__stream_encoder_set_ogg_serial_number(encoder, file_utils__ogg_serial_number); FLAC__stream_encoder_set_verify(encoder, true); FLAC__stream_encoder_set_streamable_subset(encoder, true); FLAC__stream_encoder_set_do_mid_side_stereo(encoder, false); FLAC__stream_encoder_set_loose_mid_side_stereo(encoder, false); FLAC__stream_encoder_set_channels(encoder, streaminfo->data.stream_info.channels); FLAC__stream_encoder_set_bits_per_sample(encoder, streaminfo->data.stream_info.bits_per_sample); FLAC__stream_encoder_set_sample_rate(encoder, streaminfo->data.stream_info.sample_rate); FLAC__stream_encoder_set_blocksize(encoder, streaminfo->data.stream_info.min_blocksize); FLAC__stream_encoder_set_max_lpc_order(encoder, 0); FLAC__stream_encoder_set_qlp_coeff_precision(encoder, 0); FLAC__stream_encoder_set_do_qlp_coeff_prec_search(encoder, false); FLAC__stream_encoder_set_do_escape_coding(encoder, false); FLAC__stream_encoder_set_do_exhaustive_model_search(encoder, false); FLAC__stream_encoder_set_min_residual_partition_order(encoder, 0); FLAC__stream_encoder_set_max_residual_partition_order(encoder, 0); FLAC__stream_encoder_set_rice_parameter_search_dist(encoder, 0); FLAC__stream_encoder_set_total_samples_estimate(encoder, streaminfo->data.stream_info.total_samples); FLAC__stream_encoder_set_metadata(encoder, metadata, num_metadata); if(is_ogg) init_status = FLAC__stream_encoder_init_ogg_stream(encoder, /*read_callback=*/0, encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, encoder_metadata_callback_, &encoder_client_data); else init_status = FLAC__stream_encoder_init_stream(encoder, encoder_write_callback_, /*seek_callback=*/0, /*tell_callback=*/0, encoder_metadata_callback_, &encoder_client_data); if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { fclose(encoder_client_data.file); return false; } /* init the dummy sample buffer */ for(i = 0; i < sizeof(samples) / sizeof(FLAC__int32); i++) samples[i] = i & 7; while(length > 0) { n = min(length, sizeof(samples) / sizeof(FLAC__int32)); if(!FLAC__stream_encoder_process_interleaved(encoder, samples, n)) { fclose(encoder_client_data.file); return false; } length -= n; } (void)FLAC__stream_encoder_finish(encoder); fclose(encoder_client_data.file); FLAC__stream_encoder_delete(encoder); if(0 != output_filesize) { struct stat filestats; if(stat(output_filename, &filestats) != 0) return false; else *output_filesize = filestats.st_size; } return true; }
static int start_write(sox_format_t * const ft) { priv_t * p = (priv_t *)ft->priv; FLAC__StreamEncoderState status; unsigned compression_level = MAX_COMPRESSION; /* Default to "best" */ if (ft->encoding.compression != HUGE_VAL) { compression_level = ft->encoding.compression; if (compression_level != ft->encoding.compression || compression_level > MAX_COMPRESSION) { lsx_fail_errno(ft, SOX_EINVAL, "FLAC compression level must be a whole number from 0 to %i", MAX_COMPRESSION); return SOX_EOF; } } p->encoder = FLAC__stream_encoder_new(); if (p->encoder == NULL) { lsx_fail_errno(ft, SOX_ENOMEM, "FLAC ERROR creating the encoder instance"); return SOX_EOF; } p->decoded_samples = lsx_malloc(sox_globals.bufsiz * sizeof(FLAC__int32)); p->bits_per_sample = ft->encoding.bits_per_sample; lsx_report("encoding at %i bits per sample", p->bits_per_sample); FLAC__stream_encoder_set_channels(p->encoder, ft->signal.channels); FLAC__stream_encoder_set_bits_per_sample(p->encoder, p->bits_per_sample); FLAC__stream_encoder_set_sample_rate(p->encoder, (unsigned)(ft->signal.rate + .5)); { /* Check if rate is streamable: */ static const unsigned streamable_rates[] = {8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000}; size_t i; sox_bool streamable = sox_false; for (i = 0; !streamable && i < array_length(streamable_rates); ++i) streamable = (streamable_rates[i] == ft->signal.rate); if (!streamable) { lsx_report("non-standard rate; output may not be streamable"); FLAC__stream_encoder_set_streamable_subset(p->encoder, sox_false); } } #if FLAC_API_VERSION_CURRENT >= 10 FLAC__stream_encoder_set_compression_level(p->encoder, compression_level); #else { static struct { unsigned blocksize; FLAC__bool do_exhaustive_model_search; FLAC__bool do_mid_side_stereo; FLAC__bool loose_mid_side_stereo; unsigned max_lpc_order; unsigned max_residual_partition_order; unsigned min_residual_partition_order; } const options[MAX_COMPRESSION + 1] = { {1152, sox_false, sox_false, sox_false, 0, 2, 2}, {1152, sox_false, sox_true, sox_true, 0, 2, 2}, {1152, sox_false, sox_true, sox_false, 0, 3, 0}, {4608, sox_false, sox_false, sox_false, 6, 3, 3}, {4608, sox_false, sox_true, sox_true, 8, 3, 3}, {4608, sox_false, sox_true, sox_false, 8, 3, 3}, {4608, sox_false, sox_true, sox_false, 8, 4, 0}, {4608, sox_true, sox_true, sox_false, 8, 6, 0}, {4608, sox_true, sox_true, sox_false, 12, 6, 0}, }; #define SET_OPTION(x) do {\ lsx_report(#x" = %i", options[compression_level].x); \ FLAC__stream_encoder_set_##x(p->encoder, options[compression_level].x);\ } while (0) SET_OPTION(blocksize); SET_OPTION(do_exhaustive_model_search); SET_OPTION(max_lpc_order); SET_OPTION(max_residual_partition_order); SET_OPTION(min_residual_partition_order); if (ft->signal.channels == 2) { SET_OPTION(do_mid_side_stereo); SET_OPTION(loose_mid_side_stereo); } #undef SET_OPTION } #endif if (ft->signal.length != 0) { FLAC__stream_encoder_set_total_samples_estimate(p->encoder, (FLAC__uint64)(ft->signal.length / ft->signal.channels)); p->metadata[p->num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_SEEKTABLE); if (p->metadata[p->num_metadata] == NULL) { lsx_fail_errno(ft, SOX_ENOMEM, "FLAC ERROR creating the encoder seek table template"); return SOX_EOF; } { #if FLAC_API_VERSION_CURRENT >= 8 if (!FLAC__metadata_object_seektable_template_append_spaced_points_by_samples(p->metadata[p->num_metadata], (unsigned)(10 * ft->signal.rate + .5), (FLAC__uint64)(ft->signal.length/ft->signal.channels))) { #else size_t samples = 10 * ft->signal.rate; size_t total_samples = ft->signal.length/ft->signal.channels; if (!FLAC__metadata_object_seektable_template_append_spaced_points(p->metadata[p->num_metadata], total_samples / samples + (total_samples % samples != 0), (FLAC__uint64)total_samples)) { #endif lsx_fail_errno(ft, SOX_ENOMEM, "FLAC ERROR creating the encoder seek table points"); return SOX_EOF; } } p->metadata[p->num_metadata]->is_last = sox_false; /* the encoder will set this for us */ ++p->num_metadata; } if (ft->oob.comments) { /* Make the comment structure */ FLAC__StreamMetadata_VorbisComment_Entry entry; int i; p->metadata[p->num_metadata] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT); for (i = 0; ft->oob.comments[i]; ++i) { static const char prepend[] = "Comment="; char * text = lsx_calloc(strlen(prepend) + strlen(ft->oob.comments[i]) + 1, sizeof(*text)); /* Prepend `Comment=' if no field-name already in the comment */ if (!strchr(ft->oob.comments[i], '=')) strcpy(text, prepend); entry.entry = (FLAC__byte *) strcat(text, ft->oob.comments[i]); entry.length = strlen(text); FLAC__metadata_object_vorbiscomment_append_comment(p->metadata[p->num_metadata], entry, /*copy= */ sox_true); free(text); } ++p->num_metadata; } if (p->num_metadata) FLAC__stream_encoder_set_metadata(p->encoder, p->metadata, p->num_metadata); #if FLAC_API_VERSION_CURRENT <= 7 FLAC__stream_encoder_set_write_callback(p->encoder, flac_stream_encoder_write_callback); FLAC__stream_encoder_set_metadata_callback(p->encoder, flac_stream_encoder_metadata_callback); FLAC__stream_encoder_set_client_data(p->encoder, ft); status = FLAC__stream_encoder_init(p->encoder); #else status = FLAC__stream_encoder_init_stream(p->encoder, flac_stream_encoder_write_callback, flac_stream_encoder_seek_callback, flac_stream_encoder_tell_callback, flac_stream_encoder_metadata_callback, ft); #endif if (status != FLAC__STREAM_ENCODER_OK) { lsx_fail_errno(ft, SOX_EINVAL, "%s", FLAC__StreamEncoderStateString[status]); return SOX_EOF; } return SOX_SUCCESS; } static size_t write_samples(sox_format_t * const ft, sox_sample_t const * const sampleBuffer, size_t const len) { priv_t * p = (priv_t *)ft->priv; unsigned i; for (i = 0; i < len; ++i) { long pcm = SOX_SAMPLE_TO_SIGNED_32BIT(sampleBuffer[i], ft->clips); p->decoded_samples[i] = pcm >> (32 - p->bits_per_sample); switch (p->bits_per_sample) { case 8: p->decoded_samples[i] = SOX_SAMPLE_TO_SIGNED_8BIT(sampleBuffer[i], ft->clips); break; case 16: p->decoded_samples[i] = SOX_SAMPLE_TO_SIGNED_16BIT(sampleBuffer[i], ft->clips); break; case 24: p->decoded_samples[i] = /* sign extension: */ SOX_SAMPLE_TO_SIGNED_24BIT(sampleBuffer[i],ft->clips) << 8; p->decoded_samples[i] >>= 8; break; case 32: p->decoded_samples[i] = SOX_SAMPLE_TO_SIGNED_32BIT(sampleBuffer[i],ft->clips); break; } } FLAC__stream_encoder_process_interleaved(p->encoder, p->decoded_samples, (unsigned) len / ft->signal.channels); return FLAC__stream_encoder_get_state(p->encoder) == FLAC__STREAM_ENCODER_OK ? len : 0; }
int WFLACEncoder::Initialize(unsigned int nSampleRate, unsigned int nNumberOfChannels, unsigned int nBitPerSample, unsigned int custom_value, TEncoderReadCallback read_callback, TEncoderWriteCallback write_callback, TEncoderSeekCallback seek_callback, TEncoderTellCallback tell_callback) { c_nSampleRate = nSampleRate; c_nNumberOfChannels = nNumberOfChannels; c_nBitBerSample = nBitPerSample; c_read_calllback = read_callback; c_write_callback = write_callback; c_seek_callback = seek_callback; c_tell_callback = tell_callback; c_user_data = (void*) custom_value; if((c_encoder = FLAC__stream_encoder_new()) == NULL) { err(ENCODER_INITIALIZATION_ERROR); return 0; } FLAC__bool ok = true; ok &= FLAC__stream_encoder_set_verify(c_encoder, false); ok &= FLAC__stream_encoder_set_compression_level(c_encoder, 5); ok &= FLAC__stream_encoder_set_channels(c_encoder, c_nNumberOfChannels); ok &= FLAC__stream_encoder_set_bits_per_sample(c_encoder, c_nBitBerSample); ok &= FLAC__stream_encoder_set_sample_rate(c_encoder, c_nSampleRate); ok &= FLAC__stream_encoder_set_total_samples_estimate(c_encoder, 0); if(!ok) { FLAC__stream_encoder_delete(c_encoder); c_encoder = NULL; err(ENCODER_INITIALIZATION_ERROR); return 0; } if(c_fOgg) { if(FLAC__stream_encoder_init_ogg_stream(c_encoder, f_read_callback, f_write_callback, f_seek_callback, f_tell_callback, NULL, this) != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { FLAC__stream_encoder_delete(c_encoder); c_encoder = NULL; err(ENCODER_FILEOPEN_ERROR); return 0; } } else { if(FLAC__stream_encoder_init_stream(c_encoder, f_write_callback, f_seek_callback, f_tell_callback, NULL, this) != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { FLAC__stream_encoder_delete(c_encoder); c_encoder = NULL; err(ENCODER_FILEOPEN_ERROR); return 0; } } c_fReady = 1; return 1; }
bool CEncoderFlac::Start(int inChannels, int inRate, int inBits, const std::string& title, const std::string& artist, const std::string& albumartist, const std::string& album, const std::string& year, const std::string& track, const std::string& genre, const std::string& comment, int trackLength) { if (!m_encoder) return false; // we accept only 2 / 44100 / 16 atm if (inChannels != 2 || inRate != 44100 || inBits != 16) { kodi::Log(ADDON_LOG_ERROR, "Invalid input format to encode"); return false; } FLAC__bool ok = 1; ok &= FLAC__stream_encoder_set_verify(m_encoder, true); ok &= FLAC__stream_encoder_set_channels(m_encoder, inChannels); ok &= FLAC__stream_encoder_set_bits_per_sample(m_encoder, inBits); ok &= FLAC__stream_encoder_set_sample_rate(m_encoder, inRate); ok &= FLAC__stream_encoder_set_total_samples_estimate(m_encoder, trackLength / 4); ok &= FLAC__stream_encoder_set_compression_level(m_encoder, kodi::GetSettingInt("level")); // now add some metadata FLAC__StreamMetadata_VorbisComment_Entry entry; if (ok) { if ( (m_metadata[0] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_VORBIS_COMMENT)) == nullptr || (m_metadata[1] = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)) == nullptr || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ARTIST", artist.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ALBUM", album.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "ALBUMARTIST", albumartist.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TITLE", title.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "GENRE", genre.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "TRACKNUMBER", track.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "DATE", year.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) || !FLAC__metadata_object_vorbiscomment_entry_from_name_value_pair(&entry, "COMMENT", comment.c_str()) || !FLAC__metadata_object_vorbiscomment_append_comment(m_metadata[0], entry, false) ) { ok = false; } else { m_metadata[1]->length = 4096; ok = FLAC__stream_encoder_set_metadata(m_encoder, m_metadata, 2); } } // initialize encoder in stream mode if (ok) { FLAC__StreamEncoderInitStatus init_status; init_status = FLAC__stream_encoder_init_stream(m_encoder, write_callback_flac, seek_callback_flac, tell_callback_flac, nullptr, this); if (init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { ok = false; } } if (!ok) { kodi::Log(ADDON_LOG_ERROR, "Failed to create flac stream encoder"); return false; } return true; }
static bool flac_encoder_open(struct encoder *_encoder, struct audio_format *audio_format, GError **error) { struct flac_encoder *encoder = (struct flac_encoder *)_encoder; unsigned bits_per_sample; encoder->audio_format = *audio_format; /* FIXME: flac should support 32bit as well */ switch (audio_format->format) { case SAMPLE_FORMAT_S8: bits_per_sample = 8; break; case SAMPLE_FORMAT_S16: bits_per_sample = 16; break; case SAMPLE_FORMAT_S24_P32: bits_per_sample = 24; break; default: bits_per_sample = 24; audio_format->format = SAMPLE_FORMAT_S24_P32; } /* allocate the encoder */ encoder->fse = FLAC__stream_encoder_new(); if (encoder->fse == NULL) { g_set_error(error, flac_encoder_quark(), 0, "flac_new() failed"); return false; } if (!flac_encoder_setup(encoder, bits_per_sample, error)) { FLAC__stream_encoder_delete(encoder->fse); return false; } encoder->buffer_length = 0; pcm_buffer_init(&encoder->buffer); pcm_buffer_init(&encoder->expand_buffer); /* this immediately outputs data through callback */ #if !defined(FLAC_API_VERSION_CURRENT) || FLAC_API_VERSION_CURRENT <= 7 { FLAC__StreamEncoderState init_status; FLAC__stream_encoder_set_write_callback(encoder->fse, flac_write_callback); init_status = FLAC__stream_encoder_init(encoder->fse); if (init_status != FLAC__STREAM_ENCODER_OK) { g_set_error(error, flac_encoder_quark(), 0, "failed to initialize encoder: %s\n", FLAC__StreamEncoderStateString[init_status]); flac_encoder_close(_encoder); return false; } } #else { FLAC__StreamEncoderInitStatus init_status; init_status = FLAC__stream_encoder_init_stream(encoder->fse, flac_write_callback, NULL, NULL, NULL, encoder); if(init_status != FLAC__STREAM_ENCODER_INIT_STATUS_OK) { g_set_error(error, flac_encoder_quark(), 0, "failed to initialize encoder: %s\n", FLAC__StreamEncoderInitStatusString[init_status]); flac_encoder_close(_encoder); return false; } } #endif return true; }