@@ -124,31 +124,59 @@ FilterGraph::FilterGraph(const ICodec& codec)
124
124
125
125
FilterGraph::~FilterGraph ()
126
126
{
127
+ _inputFramesBuffer.clear ();
127
128
for (std::vector<Filter*>::iterator it = _filters.begin (); it < _filters.end (); ++it)
128
129
{
129
130
delete (*it);
130
131
}
131
132
avfilter_graph_free (&_graph);
132
133
}
133
134
135
+ size_t FilterGraph::getMinInputFrameSize (const std::vector<IFrame*>& inputs)
136
+ {
137
+ if (!inputs.size ())
138
+ return 0 ;
139
+
140
+ int minFrameSize = inputs.at (0 )->getDataSize ();
141
+ for (size_t index = 1 ; index < inputs.size (); ++index)
142
+ {
143
+ if (minFrameSize > inputs.at (index)->getDataSize ())
144
+ minFrameSize = inputs.at (index)->getDataSize ();
145
+ }
146
+ return minFrameSize;
147
+ }
148
+
134
149
void FilterGraph::process (const std::vector<IFrame*>& inputs, IFrame& output)
135
150
{
136
151
// init filter graph
137
152
if (!_isInit)
138
153
init (inputs, output);
139
154
140
155
// setup input frames
156
+
157
+ // Fill the frame buffer with inputs
141
158
for (size_t index = 0 ; index < inputs.size (); ++index)
142
159
{
143
- const int ret = av_buffersrc_add_frame_flags (_filters.at (index)->getAVFilterContext (), &inputs.at (index)->getAVFrame (), AV_BUFFERSRC_FLAG_PUSH);
160
+ _inputFramesBuffer.at (index).addFrame (inputs.at (index));
161
+ }
162
+
163
+ // Get the minimum input frames size
164
+ const size_t minInputFrameSize = getMinInputFrameSize (inputs);
165
+
166
+ // Setup input frames into the filter graph
167
+ for (size_t index = 0 ; index < inputs.size (); ++index)
168
+ {
169
+ IFrame* inputBufferedFrame = _inputFramesBuffer.at (index).getFrame (minInputFrameSize);
170
+ const int ret = av_buffersrc_add_frame_flags (_filters.at (index)->getAVFilterContext (), &inputBufferedFrame->getAVFrame (), AV_BUFFERSRC_FLAG_PUSH);
171
+
144
172
if (ret < 0 )
145
173
{
146
174
throw std::runtime_error (" Error when adding a frame to the source buffer used to start to process filters: " +
147
175
getDescriptionFromErrorCode (ret));
148
176
}
149
177
}
150
178
151
- // pull filtered data from the filter graph
179
+ // Pull filtered data from the filter graph
152
180
for (;;)
153
181
{
154
182
const int ret = av_buffersink_get_frame (_filters.at (_filters.size () - 1 )->getAVFilterContext (), &output.getAVFrame ());
@@ -246,6 +274,11 @@ void FilterGraph::addInBuffer(const std::vector<IFrame*>& inputs)
246
274
filterOptions << " sample_rate=" << audioFrame->getSampleRate () << " :" ;
247
275
filterOptions << " sample_fmt=" << getSampleFormatName (audioFrame->getSampleFormat ()) << " :" ;
248
276
filterOptions << " channel_layout=0x" << std::hex << audioFrame->getChannelLayout ();
277
+
278
+ const AudioFrameDesc audioFrameDesc (audioFrame->getSampleRate (),
279
+ audioFrame->getNbChannels (),
280
+ getSampleFormatName (audioFrame->getSampleFormat ()));
281
+ _inputFramesBuffer.push_back (FrameBuffer (audioFrameDesc));
249
282
}
250
283
// video frame
251
284
else if ((*it)->isVideoFrame ())
0 commit comments