Allow content parts to not be preset in XML.
[dcpomatic.git] / src / lib / audio_filter_graph.cc
index 0035fb21357a7d3dec5fd63f21e369d2f7fe2bdc..a43f1881ea515755ed34176bc43784d82807867e 100644 (file)
@@ -31,18 +31,25 @@ using std::string;
 using std::cout;
 using boost::shared_ptr;
 
-AudioFilterGraph::AudioFilterGraph (int sample_rate, int64_t channel_layout)
+AudioFilterGraph::AudioFilterGraph (int sample_rate, int channels)
        : _sample_rate (sample_rate)
-       , _channel_layout (channel_layout)
+       , _channels (channels)
 {
+       /* FFmpeg doesn't know any channel layouts for any counts between 8 and 16,
+          so we need to tell it we're using 16 channels if we are using more than 8.
+       */
+       if (_channels > 8) {
+               _channel_layout = av_get_default_channel_layout (16);
+       } else {
+               _channel_layout = av_get_default_channel_layout (_channels);
+       }
+
        _in_frame = av_frame_alloc ();
 }
 
 AudioFilterGraph::~AudioFilterGraph()
 {
-       if (_in_frame) {
-               av_frame_free (&_in_frame);
-       }
+       av_frame_free (&_in_frame);
 }
 
 string
@@ -97,7 +104,26 @@ AudioFilterGraph::sink_name () const
 void
 AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
 {
-       _in_frame->extended_data = new uint8_t*[buffers->channels()];
+       int const process_channels = av_get_channel_layout_nb_channels (_channel_layout);
+       DCPOMATIC_ASSERT (process_channels >= buffers->channels());
+
+       if (buffers->channels() < process_channels) {
+               /* We are processing more data than we actually have (see the hack in
+                  the constructor) so we need to create new buffers with some extra
+                  silent channels.
+               */
+               shared_ptr<AudioBuffers> extended_buffers (new AudioBuffers (process_channels, buffers->frames()));
+               for (int i = 0; i < buffers->channels(); ++i) {
+                       extended_buffers->copy_channel_from (buffers.get(), i, i);
+               }
+               for (int i = buffers->channels(); i < process_channels; ++i) {
+                       extended_buffers->make_silent (i);
+               }
+
+               buffers = extended_buffers;
+       }
+
+       _in_frame->extended_data = new uint8_t*[process_channels];
        for (int i = 0; i < buffers->channels(); ++i) {
                if (i < AV_NUM_DATA_POINTERS) {
                        _in_frame->data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
@@ -109,11 +135,15 @@ AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
        _in_frame->format = AV_SAMPLE_FMT_FLTP;
        _in_frame->sample_rate = _sample_rate;
        _in_frame->channel_layout = _channel_layout;
-       _in_frame->channels = av_get_channel_layout_nb_channels (_channel_layout);
+       _in_frame->channels = process_channels;
 
        int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
 
        delete[] _in_frame->extended_data;
+       /* Reset extended_data to its original value so that av_frame_free
+          does not try to free it.
+       */
+       _in_frame->extended_data = _in_frame->data;
 
        if (r < 0) {
                char buffer[256];