Speculative fix for crash when destroying AudioFilterGraph.
[dcpomatic.git] / src / lib / audio_filter_graph.cc
1 /*
2     Copyright (C) 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 "audio_filter_graph.h"
21 #include "audio_buffers.h"
22 #include "compose.hpp"
23 extern "C" {
24 #include <libavfilter/buffersink.h>
25 #include <libavfilter/buffersrc.h>
26 }
27
28 #include "i18n.h"
29
30 using std::string;
31 using std::cout;
32 using boost::shared_ptr;
33
34 AudioFilterGraph::AudioFilterGraph (int sample_rate, int64_t channel_layout)
35         : _sample_rate (sample_rate)
36         , _channel_layout (channel_layout)
37 {
38         _in_frame = av_frame_alloc ();
39 }
40
41 AudioFilterGraph::~AudioFilterGraph()
42 {
43         av_frame_free (&_in_frame);
44 }
45
46 string
47 AudioFilterGraph::src_parameters () const
48 {
49         SafeStringStream a;
50
51         char buffer[64];
52         av_get_channel_layout_string (buffer, sizeof(buffer), 0, _channel_layout);
53
54         a << "time_base=1/1:sample_rate=" << _sample_rate << ":"
55           << "sample_fmt=" << av_get_sample_fmt_name(AV_SAMPLE_FMT_FLTP) << ":"
56           << "channel_layout=" << buffer;
57
58         return a.str ();
59 }
60
61 void *
62 AudioFilterGraph::sink_parameters () const
63 {
64         AVABufferSinkParams* sink_params = av_abuffersink_params_alloc ();
65
66         AVSampleFormat* sample_fmts = new AVSampleFormat[2];
67         sample_fmts[0] = AV_SAMPLE_FMT_FLTP;
68         sample_fmts[1] = AV_SAMPLE_FMT_NONE;
69         sink_params->sample_fmts = sample_fmts;
70
71         int64_t* channel_layouts = new int64_t[2];
72         channel_layouts[0] = _channel_layout;
73         channel_layouts[1] = -1;
74         sink_params->channel_layouts = channel_layouts;
75
76         sink_params->sample_rates = new int[2];
77         sink_params->sample_rates[0] = _sample_rate;
78         sink_params->sample_rates[1] = -1;
79
80         return sink_params;
81 }
82
83 string
84 AudioFilterGraph::src_name () const
85 {
86         return "abuffer";
87 }
88
89 string
90 AudioFilterGraph::sink_name () const
91 {
92         return "abuffersink";
93 }
94
95 void
96 AudioFilterGraph::process (shared_ptr<const AudioBuffers> buffers)
97 {
98         _in_frame->extended_data = new uint8_t*[buffers->channels()];
99         for (int i = 0; i < buffers->channels(); ++i) {
100                 if (i < AV_NUM_DATA_POINTERS) {
101                         _in_frame->data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
102                 }
103                 _in_frame->extended_data[i] = reinterpret_cast<uint8_t*> (buffers->data(i));
104         }
105
106         _in_frame->nb_samples = buffers->frames ();
107         _in_frame->format = AV_SAMPLE_FMT_FLTP;
108         _in_frame->sample_rate = _sample_rate;
109         _in_frame->channel_layout = _channel_layout;
110         _in_frame->channels = av_get_channel_layout_nb_channels (_channel_layout);
111
112         int r = av_buffersrc_write_frame (_buffer_src_context, _in_frame);
113
114         delete[] _in_frame->extended_data;
115         /* Reset extended_data to its original value so that av_frame_free
116            does not try to free it.
117         */
118         _in_frame->extended_data = _in_frame->data;
119
120         if (r < 0) {
121                 char buffer[256];
122                 av_strerror (r, buffer, sizeof(buffer));
123                 throw DecodeError (String::compose (N_("could not push buffer into filter chain (%1)"), buffer));
124         }
125
126         while (true) {
127                 if (av_buffersink_get_frame (_buffer_sink_context, _frame) < 0) {
128                         break;
129                 }
130
131                 /* We don't extract audio data here, since the only use of this class
132                    is for ebur128.
133                 */
134
135                 av_frame_unref (_frame);
136         }
137 }