Handle multiple audio streams in a single piece of content
[dcpomatic.git] / src / lib / sndfile_decoder.cc
1 /*
2     Copyright (C) 2012-2015 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 <iostream>
21 #ifdef DCPOMATIC_WINDOWS
22 #include <windows.h>
23 #define ENABLE_SNDFILE_WINDOWS_PROTOTYPES 1
24 #endif
25 #include <sndfile.h>
26 #include "sndfile_content.h"
27 #include "sndfile_decoder.h"
28 #include "exceptions.h"
29 #include "audio_buffers.h"
30
31 #include "i18n.h"
32
33 using std::vector;
34 using std::string;
35 using std::min;
36 using std::cout;
37 using boost::shared_ptr;
38
39 SndfileDecoder::SndfileDecoder (shared_ptr<const SndfileContent> c)
40         : Sndfile (c)
41         , AudioDecoder (c)
42         , _done (0)
43         , _remaining (_info.frames)
44         , _deinterleave_buffer (0)
45 {
46         
47 }
48
49 SndfileDecoder::~SndfileDecoder ()
50 {
51         delete[] _deinterleave_buffer;
52 }
53
54 bool
55 SndfileDecoder::pass (PassReason)
56 {
57         if (_remaining == 0) {
58                 return true;
59         }
60         
61         /* Do things in half second blocks as I think there may be limits
62            to what FFmpeg (and in particular the resampler) can cope with.
63         */
64         sf_count_t const block = _sndfile_content->audio_stream()->frame_rate() / 2;
65         sf_count_t const this_time = min (block, _remaining);
66
67         int const channels = _sndfile_content->audio_stream()->channels ();
68         
69         shared_ptr<AudioBuffers> data (new AudioBuffers (channels, this_time));
70
71         if (_sndfile_content->audio_stream()->channels() == 1) {
72                 /* No de-interleaving required */
73                 sf_read_float (_sndfile, data->data(0), this_time);
74         } else {
75                 /* Deinterleave */
76                 if (!_deinterleave_buffer) {
77                         _deinterleave_buffer = new float[block * channels];
78                 }
79                 sf_readf_float (_sndfile, _deinterleave_buffer, this_time);
80                 vector<float*> out_ptr (channels);
81                 for (int i = 0; i < channels; ++i) {
82                         out_ptr[i] = data->data(i);
83                 }
84                 float* in_ptr = _deinterleave_buffer;
85                 for (int i = 0; i < this_time; ++i) {
86                         for (int j = 0; j < channels; ++j) {
87                                 *out_ptr[j]++ = *in_ptr++;
88                         }
89                 }
90         }
91                 
92         data->set_frames (this_time);
93         audio (_sndfile_content->audio_stream (), data, ContentTime::from_frames (_done, _info.samplerate));
94         _done += this_time;
95         _remaining -= this_time;
96
97         return _remaining == 0;
98 }
99
100 void
101 SndfileDecoder::seek (ContentTime t, bool accurate)
102 {
103         AudioDecoder::seek (t, accurate);
104
105         _done = t.frames (_info.samplerate);
106         _remaining = _info.frames - _done;
107 }