Setup fast state of decoder after creation.
[dcpomatic.git] / src / lib / resampler.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "resampler.h"
22 #include "audio_buffers.h"
23 #include "exceptions.h"
24 #include "compose.hpp"
25 #include "dcpomatic_assert.h"
26 #include <samplerate.h>
27 #include <iostream>
28 #include <cmath>
29
30 #include "i18n.h"
31
32 using std::cout;
33 using std::pair;
34 using std::make_pair;
35 using std::runtime_error;
36 using boost::shared_ptr;
37
38 /** @param in Input sampling rate (Hz)
39  *  @param out Output sampling rate (Hz)
40  *  @param channels Number of channels.
41  */
42 Resampler::Resampler (int in, int out, int channels)
43         : _in_rate (in)
44         , _out_rate (out)
45         , _channels (channels)
46         , _fast (false)
47 {
48         int error;
49         _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
50         if (!_src) {
51                 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
52         }
53 }
54
55 Resampler::~Resampler ()
56 {
57         if (_src) {
58                 src_delete (_src);
59         }
60 }
61
62 void
63 Resampler::set_fast (bool fast)
64 {
65         if (fast == _fast) {
66                 return;
67         }
68
69         _fast = fast;
70
71         src_delete (_src);
72         _src = 0;
73
74         int error;
75         _src = src_new (_fast ? SRC_LINEAR : SRC_SINC_BEST_QUALITY, _channels, &error);
76         if (!_src) {
77                 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
78         }
79 }
80
81 shared_ptr<const AudioBuffers>
82 Resampler::run (shared_ptr<const AudioBuffers> in)
83 {
84         int in_frames = in->frames ();
85         int in_offset = 0;
86         int out_offset = 0;
87         shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
88
89         while (in_frames > 0) {
90
91                 /* Compute the resampled frames count and add 32 for luck */
92                 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
93
94                 SRC_DATA data;
95                 float* in_buffer = new float[in_frames * _channels];
96
97                 {
98                         float** p = in->data ();
99                         float* q = in_buffer;
100                         for (int i = 0; i < in_frames; ++i) {
101                                 for (int j = 0; j < _channels; ++j) {
102                                         *q++ = p[j][in_offset + i];
103                                 }
104                         }
105                 }
106
107                 data.data_in = in_buffer;
108                 data.input_frames = in_frames;
109
110                 data.data_out = new float[max_resampled_frames * _channels];
111                 data.output_frames = max_resampled_frames;
112
113                 data.end_of_input = 0;
114                 data.src_ratio = double (_out_rate) / _in_rate;
115
116                 int const r = src_process (_src, &data);
117                 if (r) {
118                         delete[] data.data_in;
119                         delete[] data.data_out;
120                         throw EncodeError (
121                                 String::compose (
122                                         N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
123                                         src_strerror (r),
124                                         in_frames,
125                                         max_resampled_frames,
126                                         _channels
127                                         )
128                                 );
129                 }
130
131                 if (data.output_frames_gen == 0) {
132                         delete[] data.data_in;
133                         delete[] data.data_out;
134                         break;
135                 }
136
137                 resampled->ensure_size (out_offset + data.output_frames_gen);
138                 resampled->set_frames (out_offset + data.output_frames_gen);
139
140                 {
141                         float* p = data.data_out;
142                         float** q = resampled->data ();
143                         for (int i = 0; i < data.output_frames_gen; ++i) {
144                                 for (int j = 0; j < _channels; ++j) {
145                                         q[j][out_offset + i] = *p++;
146                                 }
147                         }
148                 }
149
150                 in_frames -= data.input_frames_used;
151                 in_offset += data.input_frames_used;
152                 out_offset += data.output_frames_gen;
153
154                 delete[] data.data_in;
155                 delete[] data.data_out;
156         }
157
158         return resampled;
159 }
160
161 shared_ptr<const AudioBuffers>
162 Resampler::flush ()
163 {
164         shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
165         int out_offset = 0;
166         int64_t const output_size = 65536;
167
168         float dummy[1];
169         float* buffer = new float[output_size];
170
171         SRC_DATA data;
172         data.data_in = dummy;
173         data.input_frames = 0;
174         data.data_out = buffer;
175         data.output_frames = output_size;
176         data.end_of_input = 1;
177         data.src_ratio = double (_out_rate) / _in_rate;
178
179         int const r = src_process (_src, &data);
180         if (r) {
181                 delete[] buffer;
182                 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
183         }
184
185         out->ensure_size (out_offset + data.output_frames_gen);
186
187         float* p = data.data_out;
188         float** q = out->data ();
189         for (int i = 0; i < data.output_frames_gen; ++i) {
190                 for (int j = 0; j < _channels; ++j) {
191                         q[j][out_offset + i] = *p++;
192                 }
193         }
194
195         out_offset += data.output_frames_gen;
196         out->set_frames (out_offset);
197
198         delete[] buffer;
199         return out;
200 }
201
202 void
203 Resampler::reset ()
204 {
205         src_reset (_src);
206 }