Merge branch 'master' of ssh://houllier/home/carl/git/dvdomatic
[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_decoder.h"
23 #include "film.h"
24 #include "exceptions.h"
25
26 #include "i18n.h"
27
28 using std::vector;
29 using std::string;
30 using std::stringstream;
31 using std::min;
32 using std::cout;
33 using boost::shared_ptr;
34 using boost::optional;
35
36 SndfileDecoder::SndfileDecoder (shared_ptr<Film> f, DecodeOptions o)
37         : Decoder (f, o)
38         , AudioDecoder (f, o)
39         , _done (0)
40         , _frames (0)
41 {
42         _done = 0;
43         _frames = 0;
44         
45         vector<string> const files = _film->external_audio ();
46
47         int N = 0;
48         for (size_t i = 0; i < files.size(); ++i) {
49                 if (!files[i].empty()) {
50                         N = i + 1;
51                 }
52         }
53
54         if (N == 0) {
55                 return;
56         }
57
58         bool first = true;
59         
60         for (size_t i = 0; i < (size_t) N; ++i) {
61                 if (files[i].empty ()) {
62                         _sndfiles.push_back (0);
63                 } else {
64                         SF_INFO info;
65                         SNDFILE* s = sf_open (files[i].c_str(), SFM_READ, &info);
66                         if (!s) {
67                                 throw DecodeError (_("could not open external audio file for reading"));
68                         }
69
70                         if (info.channels != 1) {
71                                 throw DecodeError (_("external audio files must be mono"));
72                         }
73                         
74                         _sndfiles.push_back (s);
75
76                         if (first) {
77                                 shared_ptr<SndfileStream> st (
78                                         new SndfileStream (
79                                                 info.samplerate, av_get_default_channel_layout (N)
80                                                 )
81                                         );
82                                 
83                                 _audio_streams.push_back (st);
84                                 _audio_stream = st;
85                                 _frames = info.frames;
86                                 first = false;
87                         } else {
88                                 if (info.frames != _frames) {
89                                         throw DecodeError (_("external audio files have differing lengths"));
90                                 }
91                         }
92                 }
93         }
94 }
95
96 bool
97 SndfileDecoder::pass ()
98 {
99         if (_audio_streams.empty ()) {
100                 return true;
101         }
102         
103         /* Do things in half second blocks as I think there may be limits
104            to what FFmpeg (and in particular the resampler) can cope with.
105         */
106         sf_count_t const block = _audio_stream->sample_rate() / 2;
107         shared_ptr<AudioBuffers> audio (new AudioBuffers (_audio_stream->channels(), block));
108         sf_count_t const this_time = min (block, _frames - _done);
109         for (size_t i = 0; i < _sndfiles.size(); ++i) {
110                 if (!_sndfiles[i]) {
111                         audio->make_silent (i);
112                 } else {
113                         sf_read_float (_sndfiles[i], audio->data(i), this_time);
114                 }
115         }
116
117         audio->set_frames (this_time);
118         Audio (audio, double(_done) / _audio_stream->sample_rate());
119         _done += this_time;
120
121         return (_done == _frames);
122 }
123
124 SndfileDecoder::~SndfileDecoder ()
125 {
126         for (size_t i = 0; i < _sndfiles.size(); ++i) {
127                 if (_sndfiles[i]) {
128                         sf_close (_sndfiles[i]);
129                 }
130         }
131 }
132
133 shared_ptr<SndfileStream>
134 SndfileStream::create ()
135 {
136         return shared_ptr<SndfileStream> (new SndfileStream);
137 }
138
139 shared_ptr<SndfileStream>
140 SndfileStream::create (string t, optional<int> v)
141 {
142         if (!v) {
143                 /* version < 1; no type in the string, and there's only FFmpeg streams anyway */
144                 return shared_ptr<SndfileStream> ();
145         }
146
147         stringstream s (t);
148         string type;
149         s >> type;
150         if (type != N_("external")) {
151                 return shared_ptr<SndfileStream> ();
152         }
153
154         return shared_ptr<SndfileStream> (new SndfileStream (t, v));
155 }
156
157 SndfileStream::SndfileStream (string t, optional<int> v)
158 {
159         assert (v);
160
161         stringstream s (t);
162         string type;
163         s >> type >> _sample_rate >> _channel_layout;
164 }
165
166 SndfileStream::SndfileStream ()
167 {
168
169 }
170
171 string
172 SndfileStream::to_string () const
173 {
174         return String::compose (N_("external %1 %2"), _sample_rate, _channel_layout);
175 }