Try to clean up resampler output correctly.
[dcpomatic.git] / src / lib / resampler.cc
1 /*
2     Copyright (C) 2013 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 extern "C" {
21 #include "libavutil/channel_layout.h"
22 }       
23 #include "resampler.h"
24 #include "audio_buffers.h"
25 #include "exceptions.h"
26
27 #include "i18n.h"
28
29 using std::cout;
30 using boost::shared_ptr;
31
32 Resampler::Resampler (int in, int out, int channels)
33         : _in_rate (in)
34         , _out_rate (out)
35         , _channels (channels)
36 {
37         /* We will be using planar float data when we call the
38            resampler.  As far as I can see, the audio channel
39            layout is not necessary for our purposes; it seems
40            only to be used get the number of channels and
41            decide if rematrixing is needed.  It won't be, since
42            input and output layouts are the same.
43         */
44
45         cout << "resamp for " << _channels << " " << _in_rate << " " << _out_rate << "\n";
46
47         _swr_context = swr_alloc_set_opts (
48                 0,
49                 av_get_default_channel_layout (_channels),
50                 AV_SAMPLE_FMT_FLTP,
51                 _out_rate,
52                 av_get_default_channel_layout (_channels),
53                 AV_SAMPLE_FMT_FLTP,
54                 _in_rate,
55                 0, 0
56                 );
57         
58         swr_init (_swr_context);
59 }
60
61 Resampler::~Resampler ()
62 {
63         swr_free (&_swr_context);
64 }
65
66 shared_ptr<const AudioBuffers>
67 Resampler::run (shared_ptr<const AudioBuffers> in)
68 {
69         /* Compute the resampled frames count and add 32 for luck */
70         int const max_resampled_frames = ceil ((double) in->frames() * _out_rate / _in_rate) + 32;
71         shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, max_resampled_frames));
72
73         int const resampled_frames = swr_convert (
74                 _swr_context, (uint8_t **) resampled->data(), max_resampled_frames, (uint8_t const **) in->data(), in->frames()
75                 );
76         
77         if (resampled_frames < 0) {
78                 throw EncodeError (_("could not run sample-rate converter"));
79         }
80         
81         resampled->set_frames (resampled_frames);
82         return resampled;
83 }       
84
85 shared_ptr<const AudioBuffers>
86 Resampler::flush ()
87 {
88         shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
89         int out_offset = 0;
90         int64_t const pass_size = 256;
91         shared_ptr<AudioBuffers> pass (new AudioBuffers (_channels, 256));
92
93         while (1) {
94                 int const frames = swr_convert (_swr_context, (uint8_t **) pass->data(), pass_size, 0, 0);
95                 
96                 if (frames < 0) {
97                         throw EncodeError (_("could not run sample-rate converter"));
98                 }
99                 
100                 if (frames == 0) {
101                         break;
102                 }
103
104                 out->ensure_size (out_offset + frames);
105                 out->copy_from (pass.get(), frames, 0, out_offset);
106                 out_offset += frames;
107                 out->set_frames (out_offset);
108         }
109
110         return out;
111 }