Merge branch 'master' into 2.0
[dcpomatic.git] / src / lib / sndfile_decoder.cc
1 /*
2     Copyright (C) 2012 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 "sndfile_content.h"
23 #include "sndfile_decoder.h"
24 #include "film.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 Film> f, shared_ptr<const SndfileContent> c)
37         : Decoder (f)
38         , AudioDecoder (f, c)
39         , _sndfile_content (c)
40         , _deinterleave_buffer (0)
41 {
42         _info.format = 0;
43         _sndfile = sf_open (_sndfile_content->path(0).string().c_str(), SFM_READ, &_info);
44         if (!_sndfile) {
45                 throw DecodeError (_("could not open audio file for reading"));
46         }
47
48         _done = 0;
49         _remaining = _info.frames;
50 }
51
52 SndfileDecoder::~SndfileDecoder ()
53 {
54         sf_close (_sndfile);
55         delete[] _deinterleave_buffer;
56 }
57
58 bool
59 SndfileDecoder::pass ()
60 {
61         if (_remaining == 0) {
62                 return true;
63         }
64         
65         /* Do things in half second blocks as I think there may be limits
66            to what FFmpeg (and in particular the resampler) can cope with.
67         */
68         sf_count_t const block = _sndfile_content->content_audio_frame_rate() / 2;
69         sf_count_t const this_time = min (block, _remaining);
70
71         int const channels = _sndfile_content->audio_channels ();
72         
73         shared_ptr<AudioBuffers> data (new AudioBuffers (channels, this_time));
74
75         if (_sndfile_content->audio_channels() == 1) {
76                 /* No de-interleaving required */
77                 sf_read_float (_sndfile, data->data(0), this_time);
78         } else {
79                 /* Deinterleave */
80                 if (!_deinterleave_buffer) {
81                         _deinterleave_buffer = new float[block * channels];
82                 }
83                 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
84                 vector<float*> out_ptr (channels);
85                 for (int i = 0; i < channels; ++i) {
86                         out_ptr[i] = data->data(i);
87                 }
88                 float* in_ptr = _deinterleave_buffer;
89                 for (int i = 0; i < this_time; ++i) {
90                         for (int j = 0; j < channels; ++j) {
91                                 *out_ptr[j]++ = *in_ptr++;
92                         }
93                 }
94         }
95                 
96         data->set_frames (this_time);
97         audio (data, _done * TIME_HZ / audio_frame_rate ());
98         _done += this_time;
99         _remaining -= this_time;
100
101         return _remaining == 0;
102 }
103
104 int
105 SndfileDecoder::audio_channels () const
106 {
107         return _info.channels;
108 }
109
110 AudioFrame
111 SndfileDecoder::audio_length () const
112 {
113         return _info.frames;
114 }
115
116 int
117 SndfileDecoder::audio_frame_rate () const
118 {
119         return _info.samplerate;
120 }
121
122 void
123 SndfileDecoder::seek (ContentTime t, bool accurate)
124 {
125         Decoder::seek (t, accurate);
126         AudioDecoder::seek (t, accurate);
127
128         _done = t * audio_frame_rate() / TIME_HZ;
129         _remaining = _info.frames - _done;
130 }