static pcm_reader_t *open_input(aacenc_param_ex_t *params) { pcm_io_context_t io = { 0 }; pcm_reader_t *reader = 0; struct stat stb = { 0 }; if ((params->input_fp = aacenc_fopen(params->input_filename, "rb")) == 0) { aacenc_fprintf(stderr, "ERROR: %s: %s\n", params->input_filename, strerror(errno)); goto END; } io.cookie = params->input_fp; if (fstat(fileno(params->input_fp), &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFREG) io.vtbl = &pcm_io_vtbl; else io.vtbl = &pcm_io_vtbl_noseek; if (params->is_raw) { int bytes_per_channel; pcm_sample_description_t desc = { 0 }; if (parse_raw_spec(params->raw_format, &desc) < 0) { fprintf(stderr, "ERROR: invalid raw-format spec\n"); goto END; } desc.sample_rate = params->raw_rate; desc.channels_per_frame = params->raw_channels; bytes_per_channel = (desc.bits_per_channel + 7) / 8; desc.bytes_per_frame = params->raw_channels * bytes_per_channel; if ((reader = raw_open(&io, &desc)) == 0) { fprintf(stderr, "ERROR: failed to open raw input\n"); goto END; } } else { int c; ungetc(c = getc(params->input_fp), params->input_fp); switch (c) { case 'R': if ((reader = wav_open(&io, params->ignore_length)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } break; case 'c': params->source_tag_ctx.add = aacenc_add_tag_entry_to_store; params->source_tag_ctx.add_ctx = ¶ms->source_tags; if ((reader = caf_open(&io, aacenc_translate_generic_text_tag, ¶ms->source_tag_ctx)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } break; default: fprintf(stderr, "ERROR: unsupported input file\n"); goto END; } } if ((reader = pcm_open_sint16_converter(reader)) != 0) reader = extrapolater_open(reader); return reader; END: return 0; }
int main(int argc, char **argv) { wav_io_context_t wav_io = { read_callback, seek_callback, tell_callback }; m4af_io_callbacks_t m4af_io = { read_callback, write_callback, seek_callback, tell_callback }; aacenc_param_ex_t params = { 0 }; int result = 2; FILE *ifp = 0; FILE *ofp = 0; char *output_filename = 0; #ifdef USE_LIBSNDFILE SNDFILE* snd = NULL; SF_INFO snd_info; pcm_sample_description_t snd_desc = { 0 }; #else wav_reader_t *wavf = 0; #endif HANDLE_AACENCODER encoder = 0; AACENC_InfoStruct aacinfo = { 0 }; m4af_ctx_t *m4af = 0; const pcm_sample_description_t *sample_format; int downsampled_timescale = 0; int frame_count = 0; struct stat stb = { 0 }; setlocale(LC_CTYPE, ""); setbuf(stderr, 0); if (parse_options(argc, argv, ¶ms) < 0) return 1; #ifdef USE_LIBSNDFILE if ((snd = sf_open (params.input_filename, SFM_READ, &snd_info)) == NULL) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } #ifdef USE_LIBSAMPLERATE if(params.resample) { snd_desc.sample_rate = params.resample; printf("resampling to %dhz\n", snd_desc.sample_rate); } else { snd_desc.sample_rate = snd_info.samplerate; } snd_desc.sample_type = PCM_TYPE_FLOAT; // always -- libsndfile does the conversion for us snd_desc.bits_per_channel = sizeof(float)*8; #else snd_desc.sample_rate = snd_info.samplerate; snd_desc.sample_type = PCM_TYPE_SINT; // always -- libsndfile does the conversion for us snd_desc.bits_per_channel = sizeof(short)*8; #endif snd_desc.channels_per_frame = snd_info.channels; snd_desc.bytes_per_frame = snd_info.channels * (snd_desc.bits_per_channel / 8); snd_desc.channel_mask = 0; sample_format = &snd_desc; #else if ((ifp = aacenc_fopen(params.input_filename, "rb")) == 0) { aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.input_filename, strerror(errno)); goto END; } if (fstat(fileno(ifp), &stb) == 0 && (stb.st_mode & S_IFMT) != S_IFREG) { wav_io.seek = 0; wav_io.tell = 0; } if (!params.is_raw) { if ((wavf = wav_open(&wav_io, ifp, params.ignore_length)) == 0) { fprintf(stderr, "ERROR: broken / unsupported input file\n"); goto END; } } else { int bytes_per_channel; pcm_sample_description_t desc = { 0 }; if (parse_raw_spec(params.raw_format, &desc) < 0) { fprintf(stderr, "ERROR: invalid raw-format spec\n"); goto END; } desc.sample_rate = params.raw_rate; desc.channels_per_frame = params.raw_channels; bytes_per_channel = (desc.bits_per_channel + 7) / 8; desc.bytes_per_frame = params.raw_channels * bytes_per_channel; if ((wavf = raw_open(&wav_io, ifp, &desc)) == 0) { fprintf(stderr, "ERROR: failed to open raw input\n"); goto END; } } sample_format = wav_get_format(wavf); #endif if (aacenc_init(&encoder, (aacenc_param_t*)¶ms, sample_format, &aacinfo) < 0) goto END; if (!params.output_filename) { const char *ext = params.transport_format ? ".aac" : ".m4a"; output_filename = generate_output_filename(params.input_filename, ext); params.output_filename = output_filename; } if ((ofp = aacenc_fopen(params.output_filename, "wb+")) == 0) { aacenc_fprintf(stderr, "ERROR: %s: %s\n", params.output_filename, strerror(errno)); goto END; } handle_signals(); if (!params.transport_format) { uint32_t scale; unsigned framelen = aacinfo.frameLength; int sbr_mode = aacenc_is_sbr_active((aacenc_param_t*)¶ms); int sig_mode = aacEncoder_GetParam(encoder, AACENC_SIGNALING_MODE); if (sbr_mode && !sig_mode) downsampled_timescale = 1; scale = sample_format->sample_rate >> downsampled_timescale; if ((m4af = m4af_create(M4AF_CODEC_MP4A, scale, &m4af_io, ofp)) < 0) goto END; m4af_set_decoder_specific_info(m4af, 0, aacinfo.confBuf, aacinfo.confSize); m4af_set_fixed_frame_duration(m4af, 0, framelen >> downsampled_timescale); m4af_set_priming_mode(m4af, params.gapless_mode + 1); m4af_begin_write(m4af); }