Fix memory leak.
[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 {
47         int error;
48         _src = src_new (SRC_SINC_BEST_QUALITY, _channels, &error);
49         if (!_src) {
50                 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
51         }
52 }
53
54 Resampler::~Resampler ()
55 {
56         src_delete (_src);
57 }
58
59 void
60 Resampler::set_fast ()
61 {
62         src_delete (_src);
63         int error;
64         _src = src_new (SRC_LINEAR, _channels, &error);
65         if (!_src) {
66                 throw runtime_error (String::compose (N_("could not create sample-rate converter (%1)"), error));
67         }
68 }
69
70 pair<shared_ptr<const AudioBuffers>, Frame>
71 Resampler::run (shared_ptr<const AudioBuffers> in, Frame frame)
72 {
73         if (!_next_in || !_next_out || _next_in.get() != frame) {
74                 /* Either there was a discontinuity in the input or this is the first input;
75                    reset _next_out.
76                 */
77                 _next_out = lrintf (frame * _out_rate / _in_rate);
78         }
79
80         /* Expected next input frame */
81         _next_in = frame + in->frames ();
82
83         int in_frames = in->frames ();
84         int in_offset = 0;
85         int out_offset = 0;
86         shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, 0));
87
88         while (in_frames > 0) {
89
90                 /* Compute the resampled frames count and add 32 for luck */
91                 int const max_resampled_frames = ceil ((double) in_frames * _out_rate / _in_rate) + 32;
92
93                 SRC_DATA data;
94                 float* in_buffer = new float[in_frames * _channels];
95
96                 {
97                         float** p = in->data ();
98                         float* q = in_buffer;
99                         for (int i = 0; i < in_frames; ++i) {
100                                 for (int j = 0; j < _channels; ++j) {
101                                         *q++ = p[j][in_offset + i];
102                                 }
103                         }
104                 }
105
106                 data.data_in = in_buffer;
107                 data.input_frames = in_frames;
108
109                 data.data_out = new float[max_resampled_frames * _channels];
110                 data.output_frames = max_resampled_frames;
111
112                 data.end_of_input = 0;
113                 data.src_ratio = double (_out_rate) / _in_rate;
114
115                 int const r = src_process (_src, &data);
116                 if (r) {
117                         delete[] data.data_in;
118                         delete[] data.data_out;
119                         throw EncodeError (
120                                 String::compose (
121                                         N_("could not run sample-rate converter (%1) [processing %2 to %3, %4 channels]"),
122                                         src_strerror (r),
123                                         in_frames,
124                                         max_resampled_frames,
125                                         _channels
126                                         )
127                                 );
128                 }
129
130                 if (data.output_frames_gen == 0) {
131                         delete[] data.data_in;
132                         delete[] data.data_out;
133                         break;
134                 }
135
136                 resampled->ensure_size (out_offset + data.output_frames_gen);
137                 resampled->set_frames (out_offset + data.output_frames_gen);
138
139                 {
140                         float* p = data.data_out;
141                         float** q = resampled->data ();
142                         for (int i = 0; i < data.output_frames_gen; ++i) {
143                                 for (int j = 0; j < _channels; ++j) {
144                                         q[j][out_offset + i] = *p++;
145                                 }
146                         }
147                 }
148
149                 in_frames -= data.input_frames_used;
150                 in_offset += data.input_frames_used;
151                 out_offset += data.output_frames_gen;
152
153                 delete[] data.data_in;
154                 delete[] data.data_out;
155         }
156
157         Frame out_frame = _next_out.get ();
158
159         /* Expected next output frame */
160         _next_out = _next_out.get() + resampled->frames();
161
162         return make_pair (resampled, out_frame);
163 }
164
165 pair<shared_ptr<const AudioBuffers>, Frame>
166 Resampler::flush ()
167 {
168         shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
169         int out_offset = 0;
170         int64_t const output_size = 65536;
171
172         float dummy[1];
173         float* buffer = new float[output_size];
174
175         SRC_DATA data;
176         data.data_in = dummy;
177         data.input_frames = 0;
178         data.data_out = buffer;
179         data.output_frames = output_size;
180         data.end_of_input = 1;
181         data.src_ratio = double (_out_rate) / _in_rate;
182
183         int const r = src_process (_src, &data);
184         if (r) {
185                 delete[] buffer;
186                 throw EncodeError (String::compose (N_("could not run sample-rate converter (%1)"), src_strerror (r)));
187         }
188
189         out->ensure_size (out_offset + data.output_frames_gen);
190
191         float* p = data.data_out;
192         float** q = out->data ();
193         for (int i = 0; i < data.output_frames_gen; ++i) {
194                 for (int j = 0; j < _channels; ++j) {
195                         q[j][out_offset + i] = *p++;
196                 }
197         }
198
199         out_offset += data.output_frames_gen;
200         out->set_frames (out_offset);
201
202         delete[] buffer;
203         return make_pair (out, _next_out.get ());
204 }
205
206 void
207 Resampler::reset ()
208 {
209         src_reset (_src);
210 }