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