Bump ffmpeg to 5.1.2 "Riemann"
[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 #include "dcpomatic_assert.h"
26 #include "exceptions.h"
27 extern "C" {
28 #include <libavfilter/buffersink.h>
29 #include <libavfilter/buffersrc.h>
30 #include <libavutil/channel_layout.h>
31 #include <libavutil/opt.h>
32 }
33 #include <iostream>
34
35 #include "i18n.h"
36
37
38 using std::cout;
39 using std::make_shared;
40 using std::shared_ptr;
41 using std::string;
42
43
44 AudioFilterGraph::AudioFilterGraph (int sample_rate, int channels)
45         : _sample_rate (sample_rate)
46         , _channels (channels)
47 {
48         /* FFmpeg doesn't know any channel layouts for any counts between 8 and 16,
49            so we need to tell it we're using 16 channels if we are using more than 8.
50         */
51         av_channel_layout_default(&_channel_layout, _channels > 8 ? 16 : _channels);
52
53         _in_frame = av_frame_alloc ();
54         if (_in_frame == nullptr) {
55                 throw std::bad_alloc();
56         }
57 }
58
59 AudioFilterGraph::~AudioFilterGraph()
60 {
61         av_frame_free (&_in_frame);
62 }
63
64 string
65 AudioFilterGraph::src_parameters () const
66 {
67         char layout[64];
68         av_channel_layout_describe(&_channel_layout, layout, sizeof(layout));
69
70         char buffer[256];
71         snprintf (
72                 buffer, sizeof(buffer), "time_base=1/1:sample_rate=%d:sample_fmt=%s:channel_layout=%s",
73                 _sample_rate, av_get_sample_fmt_name(AV_SAMPLE_FMT_FLTP), layout
74                 );
75
76         return buffer;
77 }
78
79
80 void
81 AudioFilterGraph::set_parameters (AVFilterContext* context) const
82 {
83         AVSampleFormat sample_fmts[] = { AV_SAMPLE_FMT_FLTP, AV_SAMPLE_FMT_NONE };
84         int r = av_opt_set_int_list (context, "sample_fmts", sample_fmts, AV_SAMPLE_FMT_NONE, AV_OPT_SEARCH_CHILDREN);
85         DCPOMATIC_ASSERT (r >= 0);
86
87         char ch_layout[64];
88         av_channel_layout_describe(&_channel_layout, ch_layout, sizeof(ch_layout));
89         r = av_opt_set(context, "ch_layouts", ch_layout, AV_OPT_SEARCH_CHILDREN);
90         DCPOMATIC_ASSERT (r >= 0);
91
92         int sample_rates[] = { _sample_rate, -1 };
93         r = av_opt_set_int_list (context, "sample_rates", sample_rates, -1, AV_OPT_SEARCH_CHILDREN);
94         DCPOMATIC_ASSERT (r >= 0);
95 }
96
97
98 string
99 AudioFilterGraph::src_name () const
100 {
101         return "abuffer";
102 }
103
104 string
105 AudioFilterGraph::sink_name () const
106 {
107         return "abuffersink";
108 }
109
110 void
111 AudioFilterGraph::process (shared_ptr<AudioBuffers> buffers)
112 {
113         DCPOMATIC_ASSERT (buffers->frames() > 0);
114         int const process_channels = _channel_layout.nb_channels;
115         DCPOMATIC_ASSERT (process_channels >= buffers->channels());
116
117         if (buffers->channels() < process_channels) {
118                 /* We are processing more data than we actually have (see the hack in
119                    the constructor) so we need to create new buffers with some extra
120                    silent channels.
121                 */
122                 auto extended_buffers = make_shared<AudioBuffers>(process_channels, buffers->frames());
123                 for (int i = 0; i < buffers->channels(); ++i) {
124                         extended_buffers->copy_channel_from (buffers.get(), i, i);
125                 }
126                 for (int i = buffers->channels(); i < process_channels; ++i) {
127                         extended_buffers->make_silent (i);
128                 }
129
130                 buffers = extended_buffers;
131         }
132
133         _in_frame->extended_data = new uint8_t*[process_channels];
134         for (int i = 0; i < buffers->channels(); ++i) {
135                 if (i < AV_NUM_DATA_POINTERS) {
136                         _in_frame->data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
137                 }
138                 _in_frame->extended_data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
139         }
140
141         _in_frame->nb_samples = buffers->frames ();
142         _in_frame->format = AV_SAMPLE_FMT_FLTP;
143         _in_frame->sample_rate = _sample_rate;
144         _in_frame->ch_layout = _channel_layout;
145 LIBDCP_DISABLE_WARNINGS
146         _in_frame->channels = process_channels;
147 LIBDCP_ENABLE_WARNINGS
148
149         int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
150
151         delete[] _in_frame->extended_data;
152         /* Reset extended_data to its original value so that av_frame_free
153            does not try to free it.
154         */
155         _in_frame->extended_data = _in_frame->data;
156
157         if (r < 0) {
158                 char buffer[256];
159                 av_strerror (r, buffer, sizeof(buffer));
160                 throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), &buffer[0]));
161         }
162
163         while (true) {
164                 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
165                         break;
166                 }
167
168                 /* We don't extract audio data here, since the only use of this class
169                    is for ebur128.
170                 */
171
172                 av_frame_unref (_frame);
173         }
174 }