Don't overlap simultaneous video content in the timeline. Fix keep-aligned for separ...
[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 #include "compose.hpp"
27
28 #include "i18n.h"
29
30 using std::cout;
31 using std::pair;
32 using std::make_pair;
33 using boost::shared_ptr;
34
35 Resampler::Resampler (int in, int out, int channels)
36         : _in_rate (in)
37         , _out_rate (out)
38         , _channels (channels)
39 {
40         /* We will be using planar float data when we call the
41            resampler.  As far as I can see, the audio channel
42            layout is not necessary for our purposes; it seems
43            only to be used get the number of channels and
44            decide if rematrixing is needed.  It won't be, since
45            input and output layouts are the same.
46         */
47
48         _swr_context = swr_alloc_set_opts (
49                 0,
50                 av_get_default_channel_layout (_channels),
51                 AV_SAMPLE_FMT_FLTP,
52                 _out_rate,
53                 av_get_default_channel_layout (_channels),
54                 AV_SAMPLE_FMT_FLTP,
55                 _in_rate,
56                 0, 0
57                 );
58         
59         swr_init (_swr_context);
60 }
61
62 Resampler::~Resampler ()
63 {
64         swr_free (&_swr_context);
65 }
66
67 pair<shared_ptr<const AudioBuffers>, AudioContent::Frame>
68 Resampler::run (shared_ptr<const AudioBuffers> in, AudioContent::Frame frame)
69 {
70         AudioContent::Frame const resamp_time = swr_next_pts (_swr_context, frame * _out_rate) / _in_rate;
71                 
72         /* Compute the resampled frames count and add 32 for luck */
73         int const max_resampled_frames = ceil ((double) in->frames() * _out_rate / _in_rate) + 32;
74         shared_ptr<AudioBuffers> resampled (new AudioBuffers (_channels, max_resampled_frames));
75
76         int const resampled_frames = swr_convert (
77                 _swr_context, (uint8_t **) resampled->data(), max_resampled_frames, (uint8_t const **) in->data(), in->frames()
78                 );
79         
80         if (resampled_frames < 0) {
81                 char buf[256];
82                 av_strerror (resampled_frames, buf, sizeof(buf));
83                 throw EncodeError (String::compose (_("could not run sample-rate converter for %1 samples (%2) (%3)"), in->frames(), resampled_frames, buf));
84         }
85         
86         resampled->set_frames (resampled_frames);
87         return make_pair (resampled, resamp_time);
88 }       
89
90 shared_ptr<const AudioBuffers>
91 Resampler::flush ()
92 {
93         shared_ptr<AudioBuffers> out (new AudioBuffers (_channels, 0));
94         int out_offset = 0;
95         int64_t const pass_size = 256;
96         shared_ptr<AudioBuffers> pass (new AudioBuffers (_channels, 256));
97
98         while (1) {
99                 int const frames = swr_convert (_swr_context, (uint8_t **) pass->data(), pass_size, 0, 0);
100                 
101                 if (frames < 0) {
102                         throw EncodeError (_("could not run sample-rate converter"));
103                 }
104                 
105                 if (frames == 0) {
106                         break;
107                 }
108
109                 out->ensure_size (out_offset + frames);
110                 out->copy_from (pass.get(), frames, 0, out_offset);
111                 out_offset += frames;
112                 out->set_frames (out_offset);
113         }
114
115         return out;
116 }