Throw exception if av_frame_alloc() fails.
[dcpomatic.git] / src / lib / audio_filter_graph.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "audio_buffers.h"
23 #include "audio_filter_graph.h"
24 #include "compose.hpp"
25 extern "C" {
26 #include <libavfilter/buffersink.h>
27 #include <libavfilter/buffersrc.h>
28 #include <libavutil/channel_layout.h>
29 #include <libavutil/opt.h>
30 }
31 #include <iostream>
32
33 #include "i18n.h"
34
35
36 using std::cout;
37 using std::make_shared;
38 using std::shared_ptr;
39 using std::string;
40
41
42 AudioFilterGraph::AudioFilterGraph (int sample_rate, int channels)
43         : _sample_rate (sample_rate)
44         , _channels (channels)
45 {
46         /* FFmpeg doesn't know any channel layouts for any counts between 8 and 16,
47            so we need to tell it we're using 16 channels if we are using more than 8.
48         */
49         if (_channels > 8) {
50                 _channel_layout = av_get_default_channel_layout (16);
51         } else {
52                 _channel_layout = av_get_default_channel_layout (_channels);
53         }
54
55         _in_frame = av_frame_alloc ();
56         if (_in_frame == nullptr) {
57                 throw std::bad_alloc();
58         }
59 }
60
61 AudioFilterGraph::~AudioFilterGraph()
62 {
63         av_frame_free (&_in_frame);
64 }
65
66 string
67 AudioFilterGraph::src_parameters () const
68 {
69         char layout[64];
70         av_get_channel_layout_string (layout, sizeof(layout), 0, _channel_layout);
71
72         char buffer[256];
73         snprintf (
74                 buffer, sizeof(buffer), "time_base=1/1:sample_rate=%d:sample_fmt=%s:channel_layout=%s",
75                 _sample_rate, av_get_sample_fmt_name(AV_SAMPLE_FMT_FLTP), layout
76                 );
77
78         return buffer;
79 }
80
81
82 void
83 AudioFilterGraph::set_parameters (AVFilterContext* context) const
84 {
85         AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
86         int r = av_opt_set_int_list (context, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
87         DCPOMATIC_ASSERT (r >= 0);
88
89         int64_t channel_layouts[] = { _channel_layout, -1 };
90         r = av_opt_set_int_list (context, "channel_layouts", channel_layouts, -1, AV_OPT_SEARCH_CHILDREN);
91         DCPOMATIC_ASSERT (r >= 0);
92
93         int sample_rates[] = { _sample_rate, -1 };
94         r = av_opt_set_int_list (context, "sample_rates", sample_rates, -1, AV_OPT_SEARCH_CHILDREN);
95         DCPOMATIC_ASSERT (r >= 0);
96 }
97
98
99 string
100 AudioFilterGraph::src_name () const
101 {
102         return "abuffer";
103 }
104
105 string
106 AudioFilterGraph::sink_name () const
107 {
108         return "abuffersink";
109 }
110
111 void
112 AudioFilterGraph::process (shared_ptr<AudioBuffers> buffers)
113 {
114         DCPOMATIC_ASSERT (buffers->frames() > 0);
115         int const process_channels = av_get_channel_layout_nb_channels (_channel_layout);
116         DCPOMATIC_ASSERT (process_channels >= buffers->channels());
117
118         if (buffers->channels() < process_channels) {
119                 /* We are processing more data than we actually have (see the hack in
120                    the constructor) so we need to create new buffers with some extra
121                    silent channels.
122                 */
123                 auto extended_buffers = make_shared<AudioBuffers>(process_channels, buffers->frames());
124                 for (int i = 0; i < buffers->channels(); ++i) {
125                         extended_buffers->copy_channel_from (buffers.get(), i, i);
126                 }
127                 for (int i = buffers->channels(); i < process_channels; ++i) {
128                         extended_buffers->make_silent (i);
129                 }
130
131                 buffers = extended_buffers;
132         }
133
134         _in_frame->extended_data = new uint8_t*[process_channels];
135         for (int i = 0; i < buffers->channels(); ++i) {
136                 if (i < AV_NUM_DATA_POINTERS) {
137                         _in_frame->data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
138                 }
139                 _in_frame->extended_data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
140         }
141
142         _in_frame->nb_samples = buffers->frames ();
143         _in_frame->format = AV_SAMPLE_FMT_FLTP;
144         _in_frame->sample_rate = _sample_rate;
145         _in_frame->channel_layout = _channel_layout;
146         _in_frame->channels = process_channels;
147
148         int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
149
150         delete[] _in_frame->extended_data;
151         /* Reset extended_data to its original value so that av_frame_free
152            does not try to free it.
153         */
154         _in_frame->extended_data = _in_frame->data;
155
156         if (r < 0) {
157                 char buffer[256];
158                 av_strerror (r, buffer, sizeof(buffer));
159                 throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), &buffer[0]));
160         }
161
162         while (true) {
163                 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
164                         break;
165                 }
166
167                 /* We don't extract audio data here, since the only use of this class
168                    is for ebur128.
169                 */
170
171                 av_frame_unref (_frame);
172         }
173 }