X-Git-Url: https://git.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fsndfile_decoder.cc;h=67bb25e0dc71e56fe5678c8bef2b25bd0f17c655;hb=e6f28e7cda23c1ba3c49cc1bf2dc1491c2f87160;hp=fdaf2eeaaf4397ad5d1cb1430ebc6d006607a5b7;hpb=a8d24d35dc999228f804ed425c0d9e90b9ddceaa;p=dcpomatic.git diff --git a/src/lib/sndfile_decoder.cc b/src/lib/sndfile_decoder.cc index fdaf2eeaa..67bb25e0d 100644 --- a/src/lib/sndfile_decoder.cc +++ b/src/lib/sndfile_decoder.cc @@ -18,154 +18,122 @@ */ #include +#ifdef DCPOMATIC_WINDOWS +#include +#define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1 +#endif #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) - , _done (0) - , _frames (0) +SndfileDecoder::SndfileDecoder (shared_ptr c) + : AudioDecoder (c) + , _sndfile_content (c) + , _deinterleave_buffer (0) { - _done = 0; - _frames = 0; + _info.format = 0; + + /* Here be monsters. See fopen_boost for similar shenanigans */ +#ifdef DCPOMATIC_WINDOWS + _sndfile = sf_wchar_open (_sndfile_content->path(0).c_str(), SFM_READ, &_info); +#else + _sndfile = sf_open (_sndfile_content->path(0).string().c_str(), SFM_READ, &_info); +#endif - 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; + if (!_sndfile) { + throw DecodeError (_("could not open audio file for reading")); } - bool first = true; - - 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")); - } + _done = 0; + _remaining = _info.frames; +} - 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")); - } - } - } - } +SndfileDecoder::~SndfileDecoder () +{ + sf_close (_sndfile); + delete[] _deinterleave_buffer; } bool SndfileDecoder::pass () { + 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 const this_time = min (block, _frames - _done); - 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), this_time); + sf_count_t const block = _sndfile_content->content_audio_frame_rate() / 2; + sf_count_t const this_time = min (block, _remaining); + + int const channels = _sndfile_content->audio_channels (); + + shared_ptr data (new AudioBuffers (channels, this_time)); + + if (_sndfile_content->audio_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++; + } } } - - audio->set_frames (this_time); - Audio (audio, double(_done) / _audio_stream->sample_rate()); + + data->set_frames (this_time); + audio (data, ContentTime::from_frames (_done, audio_frame_rate ())); _done += this_time; + _remaining -= this_time; - return (_done == _frames); -} - -SndfileDecoder::~SndfileDecoder () -{ - for (size_t i = 0; i < _sndfiles.size(); ++i) { - if (_sndfiles[i]) { - sf_close (_sndfiles[i]); - } - } + return _remaining == 0; } -shared_ptr -SndfileStream::create () +int +SndfileDecoder::audio_channels () const { - return shared_ptr (new SndfileStream); + return _info.channels; } -shared_ptr -SndfileStream::create (string t, optional v) +ContentTime +SndfileDecoder::audio_length () const { - 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 (); - } - - return shared_ptr (new SndfileStream (t, v)); + return ContentTime::from_frames (_info.frames, audio_frame_rate ()); } -SndfileStream::SndfileStream (string t, optional v) +int +SndfileDecoder::audio_frame_rate () const { - assert (v); - - stringstream s (t); - string type; - s >> type >> _sample_rate >> _channel_layout; + return _info.samplerate; } -SndfileStream::SndfileStream () +void +SndfileDecoder::seek (ContentTime t, bool accurate) { + Decoder::seek (t, accurate); + AudioDecoder::seek (t, accurate); -} - -string -SndfileStream::to_string () const -{ - return String::compose (N_("external %1 %2"), _sample_rate, _channel_layout); + _done = t.frames (audio_frame_rate ()); + _remaining = _info.frames - _done; }