b05750ac8dc3aa7675fcd677b4da070a58a157ae
[dcpomatic.git] / src / lib / sndfile_decoder.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <iostream>
21 #include <sndfile.h>
22 #include "audio_content.h"
23 #include "sndfile_content.h"
24 #include "sndfile_decoder.h"
25 #include "exceptions.h"
26 #include "audio_buffers.h"
27
28 #include "i18n.h"
29
30 using std::vector;
31 using std::string;
32 using std::min;
33 using std::cout;
34 using boost::shared_ptr;
35
36 SndfileDecoder::SndfileDecoder (shared_ptr<const SndfileContent> c, bool fast, shared_ptr<Log> log)
37         : Sndfile (c)
38         , AudioDecoder (c->audio, fast, log)
39         , _done (0)
40         , _remaining (_info.frames)
41         , _deinterleave_buffer (0)
42 {
43
44 }
45
46 SndfileDecoder::~SndfileDecoder ()
47 {
48         delete[] _deinterleave_buffer;
49 }
50
51 bool
52 SndfileDecoder::pass (PassReason, bool)
53 {
54         if (_remaining == 0) {
55                 return true;
56         }
57
58         /* Do things in half second blocks as I think there may be limits
59            to what FFmpeg (and in particular the resampler) can cope with.
60         */
61         sf_count_t const block = _sndfile_content->audio->stream()->frame_rate() / 2;
62         sf_count_t const this_time = min (block, _remaining);
63
64         int const channels = _sndfile_content->audio->stream()->channels ();
65
66         shared_ptr<AudioBuffers> data (new AudioBuffers (channels, this_time));
67
68         if (_sndfile_content->audio->stream()->channels() == 1) {
69                 /* No de-interleaving required */
70                 sf_read_float (_sndfile, data->data(0), this_time);
71         } else {
72                 /* Deinterleave */
73                 if (!_deinterleave_buffer) {
74                         _deinterleave_buffer = new float[block * channels];
75                 }
76                 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
77                 vector<float*> out_ptr (channels);
78                 for (int i = 0; i < channels; ++i) {
79                         out_ptr[i] = data->data(i);
80                 }
81                 float* in_ptr = _deinterleave_buffer;
82                 for (int i = 0; i < this_time; ++i) {
83                         for (int j = 0; j < channels; ++j) {
84                                 *out_ptr[j]++ = *in_ptr++;
85                         }
86                 }
87         }
88
89         data->set_frames (this_time);
90         audio (_sndfile_content->audio->stream (), data, ContentTime::from_frames (_done, _info.samplerate));
91         _done += this_time;
92         _remaining -= this_time;
93
94         return _remaining == 0;
95 }
96
97 void
98 SndfileDecoder::seek (ContentTime t, bool accurate)
99 {
100         AudioDecoder::seek (t, accurate);
101
102         _done = t.frames_round (_info.samplerate);
103         _remaining = _info.frames - _done;
104 }