X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fsndfile_decoder.cc;h=ac01e0da87c99bf23fbc23da0b7ad4fc4a5eb4f9;hb=b01da3cb8088d5d5df6112ae9358ba3ed606b8b6;hp=af59c049c31dba1acc59d9dad4132a2de5b8dcaa;hpb=e6dfbc05158cf643d507e6f7adf6e6cb84572b69;p=dcpomatic.git diff --git a/src/lib/sndfile_decoder.cc b/src/lib/sndfile_decoder.cc index af59c049c..ac01e0da8 100644 --- a/src/lib/sndfile_decoder.cc +++ b/src/lib/sndfile_decoder.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2012 Carl Hetherington + Copyright (C) 2012-2015 Carl Hetherington This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -19,171 +19,85 @@ #include #include +#include "sndfile_content.h" #include "sndfile_decoder.h" -#include "film.h" #include "exceptions.h" +#include "audio_buffers.h" #include "i18n.h" using std::vector; using std::string; -using std::stringstream; using std::min; using std::cout; using boost::shared_ptr; -using boost::optional; -SndfileDecoder::SndfileDecoder (shared_ptr f, DecodeOptions o) - : Decoder (f, o) - , AudioDecoder (f, o) +SndfileDecoder::SndfileDecoder (shared_ptr c, bool fast) + : Sndfile (c) + , AudioDecoder (c, fast) + , _done (0) + , _remaining (_info.frames) + , _deinterleave_buffer (0) { - sf_count_t frames; - vector sf = open_files (frames); - close_files (sf); + } -vector -SndfileDecoder::open_files (sf_count_t & frames) +SndfileDecoder::~SndfileDecoder () { - vector const files = _film->external_audio (); - - int N = 0; - for (size_t i = 0; i < files.size(); ++i) { - if (!files[i].empty()) { - N = i + 1; - } - } - - if (N == 0) { - return vector (); - } - - bool first = true; - frames = 0; - - vector sndfiles; - for (size_t i = 0; i < (size_t) N; ++i) { - if (files[i].empty ()) { - sndfiles.push_back (0); - } else { - SF_INFO info; - SNDFILE* s = sf_open (files[i].c_str(), SFM_READ, &info); - if (!s) { - throw DecodeError (_("could not open external audio file for reading")); - } - - if (info.channels != 1) { - throw DecodeError (_("external audio files must be mono")); - } - - sndfiles.push_back (s); - - if (first) { - shared_ptr st ( - new SndfileStream ( - info.samplerate, av_get_default_channel_layout (N) - ) - ); - - _audio_streams.push_back (st); - _audio_stream = st; - frames = info.frames; - first = false; - } else { - if (info.frames != frames) { - throw DecodeError (_("external audio files have differing lengths")); - } - } - } - } - - return sndfiles; + delete[] _deinterleave_buffer; } bool -SndfileDecoder::pass () +SndfileDecoder::pass (PassReason, bool) { - sf_count_t frames; - vector sndfiles = open_files (frames); - if (sndfiles.empty()) { + if (_remaining == 0) { return true; } /* Do things in half second blocks as I think there may be limits to what FFmpeg (and in particular the resampler) can cope with. */ - sf_count_t const block = _audio_stream->sample_rate() / 2; - shared_ptr audio (new AudioBuffers (_audio_stream->channels(), block)); - sf_count_t done = 0; - while (frames > 0) { - sf_count_t const this_time = min (block, frames); - for (size_t i = 0; i < sndfiles.size(); ++i) { - if (!sndfiles[i]) { - audio->make_silent (i); - } else { - sf_read_float (sndfiles[i], audio->data(i), block); - } - } + sf_count_t const block = _sndfile_content->audio_stream()->frame_rate() / 2; + sf_count_t const this_time = min (block, _remaining); - audio->set_frames (this_time); - Audio (audio, double(done) / _audio_stream->sample_rate()); - done += this_time; - frames -= this_time; - } - - close_files (sndfiles); + int const channels = _sndfile_content->audio_stream()->channels (); - return true; -} + shared_ptr data (new AudioBuffers (channels, this_time)); -void -SndfileDecoder::close_files (vector const & sndfiles) -{ - for (size_t i = 0; i < sndfiles.size(); ++i) { - sf_close (sndfiles[i]); - } -} - -shared_ptr -SndfileStream::create () -{ - return shared_ptr (new SndfileStream); -} - -shared_ptr -SndfileStream::create (string t, optional v) -{ - if (!v) { - /* version < 1; no type in the string, and there's only FFmpeg streams anyway */ - return shared_ptr (); - } - - stringstream s (t); - string type; - s >> type; - if (type != N_("external")) { - return shared_ptr (); + if (_sndfile_content->audio_stream()->channels() == 1) { + /* No de-interleaving required */ + sf_read_float (_sndfile, data->data(0), this_time); + } else { + /* Deinterleave */ + if (!_deinterleave_buffer) { + _deinterleave_buffer = new float[block * channels]; + } + sf_readf_float (_sndfile, _deinterleave_buffer, this_time); + vector out_ptr (channels); + for (int i = 0; i < channels; ++i) { + out_ptr[i] = data->data(i); + } + float* in_ptr = _deinterleave_buffer; + for (int i = 0; i < this_time; ++i) { + for (int j = 0; j < channels; ++j) { + *out_ptr[j]++ = *in_ptr++; + } + } } - return shared_ptr (new SndfileStream (t, v)); -} - -SndfileStream::SndfileStream (string t, optional v) -{ - assert (v); + data->set_frames (this_time); + audio (_sndfile_content->audio_stream (), data, ContentTime::from_frames (_done, _info.samplerate)); + _done += this_time; + _remaining -= this_time; - stringstream s (t); - string type; - s >> type >> _sample_rate >> _channel_layout; + return _remaining == 0; } -SndfileStream::SndfileStream () +void +SndfileDecoder::seek (ContentTime t, bool accurate) { + AudioDecoder::seek (t, accurate); -} - -string -SndfileStream::to_string () const -{ - return String::compose (N_("external %1 %2"), _sample_rate, _channel_layout); + _done = t.frames_round (_info.samplerate); + _remaining = _info.frames - _done; }