+static
+pcm_reader_t *open_input(aacenc_param_ex_t *params)
+{
+ wav_io_context_t wav_io = {
+ read_callback, seek_callback, tell_callback
+ };
+ 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;
+ }
+ if (fstat(fileno(params->input_fp), &stb) == 0
+ && (stb.st_mode & S_IFMT) != S_IFREG) {
+ wav_io.seek = 0;
+ wav_io.tell = 0;
+ }
+ 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(&wav_io, params->input_fp, &desc)) == 0) {
+ fprintf(stderr, "ERROR: failed to open raw input\n");
+ goto END;
+ }
+ } else {
+ if ((reader = wav_open(&wav_io, params->input_fp,
+ params->ignore_length)) == 0) {
+ fprintf(stderr, "ERROR: broken / unsupported input file\n");
+ goto END;
+ }
+ }
+END:
+ return reader;
+}
+