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