Move SRC (badly) to AudioDecoder.
[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 "exceptions.h"
22 #include "log.h"
23
24 #include "i18n.h"
25
26 using std::stringstream;
27 using boost::optional;
28 using boost::shared_ptr;
29
30 AudioDecoder::AudioDecoder (shared_ptr<const Film> f, shared_ptr<const AudioContent> c)
31         : Decoder (f)
32         , _audio_content (c)
33 {
34         if (_audio_content->audio_frame_rate() != _film->target_audio_sample_rate()) {
35
36                 stringstream s;
37                 s << String::compose ("Will resample audio from %1 to %2", _audio_content->audio_frame_rate(), _film->target_audio_sample_rate());
38                 _film->log()->log (s.str ());
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 (MAX_AUDIO_CHANNELS),
51                         AV_SAMPLE_FMT_FLTP,
52                         _film->target_audio_sample_rate(),
53                         av_get_default_channel_layout (MAX_AUDIO_CHANNELS),
54                         AV_SAMPLE_FMT_FLTP,
55                         _audio_content->audio_frame_rate(),
56                         0, 0
57                         );
58                 
59                 swr_init (_swr_context);
60         } else {
61                 _swr_context = 0;
62         }
63 }
64
65 AudioDecoder::~AudioDecoder ()
66 {
67         if (_swr_context) {
68                 swr_free (&_swr_context);
69         }
70 }
71         
72
73 #if 0
74 void
75 AudioDecoder::process_end ()
76 {
77         if (_film->has_audio() && _swr_context) {
78
79                 shared_ptr<AudioBuffers> out (new AudioBuffers (_film->audio_mapping().dcp_channels(), 256));
80                         
81                 while (1) {
82                         int const frames = swr_convert (_swr_context, (uint8_t **) out->data(), 256, 0, 0);
83
84                         if (frames < 0) {
85                                 throw EncodeError (_("could not run sample-rate converter"));
86                         }
87
88                         if (frames == 0) {
89                                 break;
90                         }
91
92                         out->set_frames (frames);
93                         _writer->write (out);
94                 }
95
96         }
97 }
98 #endif
99
100 void
101 AudioDecoder::emit_audio (shared_ptr<const AudioBuffers> data, Time time)
102 {
103         /* XXX: map audio to 5.1 */
104         
105         /* Maybe sample-rate convert */
106         if (_swr_context) {
107
108                 /* Compute the resampled frames count and add 32 for luck */
109                 int const max_resampled_frames = ceil ((int64_t) data->frames() * _film->target_audio_sample_rate() / _audio_content->audio_frame_rate()) + 32;
110
111                 shared_ptr<AudioBuffers> resampled (new AudioBuffers (MAX_AUDIO_CHANNELS, max_resampled_frames));
112
113                 /* Resample audio */
114                 int const resampled_frames = swr_convert (
115                         _swr_context, (uint8_t **) resampled->data(), max_resampled_frames, (uint8_t const **) data->data(), data->frames()
116                         );
117                 
118                 if (resampled_frames < 0) {
119                         throw EncodeError (_("could not run sample-rate converter"));
120                 }
121
122                 resampled->set_frames (resampled_frames);
123                 
124                 /* And point our variables at the resampled audio */
125                 data = resampled;
126         }
127
128         Audio (data, time);
129 }
130
131