Various fixes to make audio analysis sort-of work.
[dcpomatic.git] / src / lib / audio_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 "audio_decoder.h"
21 #include "audio_buffers.h"
22 #include "exceptions.h"
23 #include "log.h"
24
25 #include "i18n.h"
26
27 using std::stringstream;
28 using std::list;
29 using std::pair;
30 using std::cout;
31 using boost::optional;
32 using boost::shared_ptr;
33
34 AudioDecoder::AudioDecoder (shared_ptr<const Film> f, shared_ptr<const AudioContent> c)
35         : Decoder (f)
36         , _next_audio (0)
37         , _audio_content (c)
38 {
39         if (_audio_content->content_audio_frame_rate() != _audio_content->output_audio_frame_rate()) {
40
41                 shared_ptr<const Film> film = _film.lock ();
42                 assert (film);
43
44                 stringstream s;
45                 s << String::compose (
46                         "Will resample audio from %1 to %2",
47                         _audio_content->content_audio_frame_rate(), _audio_content->output_audio_frame_rate()
48                         );
49                 
50                 film->log()->log (s.str ());
51
52                 /* We will be using planar float data when we call the
53                    resampler.  As far as I can see, the audio channel
54                    layout is not necessary for our purposes; it seems
55                    only to be used get the number of channels and
56                    decide if rematrixing is needed.  It won't be, since
57                    input and output layouts are the same.
58                 */
59
60                 _swr_context = swr_alloc_set_opts (
61                         0,
62                         av_get_default_channel_layout (_audio_content->audio_channels ()),
63                         AV_SAMPLE_FMT_FLTP,
64                         _audio_content->output_audio_frame_rate(),
65                         av_get_default_channel_layout (_audio_content->audio_channels ()),
66                         AV_SAMPLE_FMT_FLTP,
67                         _audio_content->content_audio_frame_rate(),
68                         0, 0
69                         );
70                 
71                 swr_init (_swr_context);
72         } else {
73                 _swr_context = 0;
74         }
75 }
76
77 AudioDecoder::~AudioDecoder ()
78 {
79         if (_swr_context) {
80                 swr_free (&_swr_context);
81         }
82 }
83         
84
85 #if 0
86 void
87 AudioDecoder::process_end ()
88 {
89         if (_swr_context) {
90
91                 shared_ptr<const Film> film = _film.lock ();
92                 assert (film);
93                 
94                 shared_ptr<AudioBuffers> out (new AudioBuffers (film->audio_mapping().dcp_channels(), 256));
95                         
96                 while (1) {
97                         int const frames = swr_convert (_swr_context, (uint8_t **) out->data(), 256, 0, 0);
98
99                         if (frames < 0) {
100                                 throw EncodeError (_("could not run sample-rate converter"));
101                         }
102
103                         if (frames == 0) {
104                                 break;
105                         }
106
107                         out->set_frames (frames);
108                         _writer->write (out);
109                 }
110
111         }
112 }
113 #endif
114
115 void
116 AudioDecoder::audio (shared_ptr<const AudioBuffers> data, Time time)
117 {
118         /* Maybe resample */
119         if (_swr_context) {
120
121                 /* Compute the resampled frames count and add 32 for luck */
122                 int const max_resampled_frames = ceil (
123                         (int64_t) data->frames() * _audio_content->output_audio_frame_rate() / _audio_content->content_audio_frame_rate()
124                         ) + 32;
125
126                 shared_ptr<AudioBuffers> resampled (new AudioBuffers (data->channels(), max_resampled_frames));
127
128                 /* Resample audio */
129                 int const resampled_frames = swr_convert (
130                         _swr_context, (uint8_t **) resampled->data(), max_resampled_frames, (uint8_t const **) data->data(), data->frames()
131                         );
132                 
133                 if (resampled_frames < 0) {
134                         throw EncodeError (_("could not run sample-rate converter"));
135                 }
136
137                 resampled->set_frames (resampled_frames);
138                 
139                 /* And point our variables at the resampled audio */
140                 data = resampled;
141         }
142
143         shared_ptr<const Film> film = _film.lock ();
144         assert (film);
145         
146         /* Remap channels */
147         shared_ptr<AudioBuffers> dcp_mapped (new AudioBuffers (film->dcp_audio_channels(), data->frames()));
148         dcp_mapped->make_silent ();
149         list<pair<int, libdcp::Channel> > map = _audio_content->audio_mapping().content_to_dcp ();
150         for (list<pair<int, libdcp::Channel> >::iterator i = map.begin(); i != map.end(); ++i) {
151                 dcp_mapped->accumulate_channel (data.get(), i->first, i->second);
152         }
153
154         Audio (dcp_mapped, time);
155         _next_audio = time + film->audio_frames_to_time (data->frames());
156 }
157
158 bool
159 AudioDecoder::audio_done () const
160 {
161         shared_ptr<const Film> film = _film.lock ();
162         assert (film);
163         
164         return (_audio_content->length() - _next_audio) < film->audio_frames_to_time (1);
165 }
166