Compiles; strange hang on adding content to a 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         , _audio_content (c)
34         , _output_audio_frame_rate (_audio_content->output_audio_frame_rate (f))
35 {
36         if (_audio_content->content_audio_frame_rate() != _output_audio_frame_rate) {
37
38                 stringstream s;
39                 s << String::compose ("Will resample audio from %1 to %2", _audio_content->content_audio_frame_rate(), _output_audio_frame_rate);
40                 _film->log()->log (s.str ());
41
42                 /* We will be using planar float data when we call the
43                    resampler.  As far as I can see, the audio channel
44                    layout is not necessary for our purposes; it seems
45                    only to be used get the number of channels and
46                    decide if rematrixing is needed.  It won't be, since
47                    input and output layouts are the same.
48                 */
49
50                 _swr_context = swr_alloc_set_opts (
51                         0,
52                         av_get_default_channel_layout (MAX_AUDIO_CHANNELS),
53                         AV_SAMPLE_FMT_FLTP,
54                         _output_audio_frame_rate,
55                         av_get_default_channel_layout (MAX_AUDIO_CHANNELS),
56                         AV_SAMPLE_FMT_FLTP,
57                         _audio_content->content_audio_frame_rate(),
58                         0, 0
59                         );
60                 
61                 swr_init (_swr_context);
62         } else {
63                 _swr_context = 0;
64         }
65 }
66
67 AudioDecoder::~AudioDecoder ()
68 {
69         if (_swr_context) {
70                 swr_free (&_swr_context);
71         }
72 }
73         
74
75 #if 0
76 void
77 AudioDecoder::process_end ()
78 {
79         if (_swr_context) {
80
81                 shared_ptr<AudioBuffers> out (new AudioBuffers (_film->audio_mapping().dcp_channels(), 256));
82                         
83                 while (1) {
84                         int const frames = swr_convert (_swr_context, (uint8_t **) out->data(), 256, 0, 0);
85
86                         if (frames < 0) {
87                                 throw EncodeError (_("could not run sample-rate converter"));
88                         }
89
90                         if (frames == 0) {
91                                 break;
92                         }
93
94                         out->set_frames (frames);
95                         _writer->write (out);
96                 }
97
98         }
99 }
100 #endif
101
102 void
103 AudioDecoder::emit_audio (shared_ptr<const AudioBuffers> data, Time time)
104 {
105         /* XXX: map audio to 5.1 */
106         
107         /* Maybe sample-rate convert */
108         if (_swr_context) {
109
110                 /* Compute the resampled frames count and add 32 for luck */
111                 int const max_resampled_frames = ceil ((int64_t) data->frames() * _output_audio_frame_rate / _audio_content->content_audio_frame_rate()) + 32;
112
113                 shared_ptr<AudioBuffers> resampled (new AudioBuffers (MAX_AUDIO_CHANNELS, max_resampled_frames));
114
115                 /* Resample audio */
116                 int const resampled_frames = swr_convert (
117                         _swr_context, (uint8_t **) resampled->data(), max_resampled_frames, (uint8_t const **) data->data(), data->frames()
118                         );
119                 
120                 if (resampled_frames < 0) {
121                         throw EncodeError (_("could not run sample-rate converter"));
122                 }
123
124                 resampled->set_frames (resampled_frames);
125                 
126                 /* And point our variables at the resampled audio */
127                 data = resampled;
128         }
129
130         Audio (data, time);
131 }
132
133