Skip to content

Commit 053f9be

Browse files
author
Valentin NOEL
committed
FilterGraph: frame buffers can be bypassed if not necessary
If the input frames have the same size and the buffers are empty, bypass to avoid useless data copies.
1 parent 3864d7e commit 053f9be

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/AvTranscoder/filter/FilterGraph.cpp

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,28 +147,64 @@ size_t FilterGraph::getMinInputFrameSize(const std::vector<IFrame*>& inputs)
147147
return minFrameSize;
148148
}
149149

150+
bool FilterGraph::areInputFrameSizeEqual(const std::vector<IFrame*>& inputs)
151+
{
152+
if(!inputs.size() || inputs.size() == 1)
153+
return true;
154+
155+
const int frameSize = inputs.at(0)->getDataSize();
156+
for(size_t index = 1; index < inputs.size(); ++index)
157+
{
158+
if(frameSize != inputs.at(index)->getDataSize())
159+
return false;
160+
}
161+
return true;
162+
}
163+
164+
bool FilterGraph::areFrameBuffersEmpty()
165+
{
166+
if(!_inputFramesBuffer.size())
167+
return true;
168+
169+
for(std::vector<FrameBuffer>::iterator it = _inputFramesBuffer.begin(); it != _inputFramesBuffer.end(); ++it)
170+
{
171+
if(!it->isEmpty())
172+
return false;
173+
}
174+
return true;
175+
}
176+
150177
void FilterGraph::process(const std::vector<IFrame*>& inputs, IFrame& output)
151178
{
152-
// init filter graph
179+
// Init the filter graph
153180
if(!_isInit)
154181
init(inputs, output);
155182

156-
// setup input frames
183+
// Check whether we can bypass the input buffers
184+
const bool bypassBuffers = areInputFrameSizeEqual(inputs) && areFrameBuffersEmpty();
185+
size_t minInputFrameSize = 0;
157186

158-
// Fill the frame buffer with inputs
159-
for(size_t index = 0; index < inputs.size(); ++index)
187+
if(!bypassBuffers)
160188
{
161-
_inputFramesBuffer.at(index).addFrame(inputs.at(index));
189+
// Fill the frame buffer with inputs
190+
for(size_t index = 0; index < inputs.size(); ++index)
191+
_inputFramesBuffer.at(index).addFrame(inputs.at(index));
192+
193+
// Get the minimum input frames size
194+
minInputFrameSize = getMinInputFrameSize(inputs);
162195
}
163196

164-
// Get the minimum input frames size
165-
const size_t minInputFrameSize = getMinInputFrameSize(inputs);
166197

167198
// Setup input frames into the filter graph
168199
for(size_t index = 0; index < inputs.size(); ++index)
169200
{
170-
IFrame* inputBufferedFrame = _inputFramesBuffer.at(index).getFrame(minInputFrameSize);
171-
const int ret = av_buffersrc_add_frame_flags(_filters.at(index)->getAVFilterContext(), &inputBufferedFrame->getAVFrame(), AV_BUFFERSRC_FLAG_PUSH);
201+
IFrame* inputFrame = NULL;
202+
if(bypassBuffers)
203+
inputFrame = inputs.at(index);
204+
else
205+
inputFrame = _inputFramesBuffer.at(index).getFrame(minInputFrameSize);
206+
207+
const int ret = av_buffersrc_add_frame_flags(_filters.at(index)->getAVFilterContext(), &inputFrame->getAVFrame(), AV_BUFFERSRC_FLAG_PUSH);
172208

173209
if(ret < 0)
174210
{

src/AvTranscoder/filter/FilterGraph.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ class AvExport FilterGraph
128128
//@}
129129

130130
size_t getMinInputFrameSize(const std::vector<IFrame*>& inputs);
131+
bool areInputFrameSizeEqual(const std::vector<IFrame*>& inputs);
132+
bool areFrameBuffersEmpty();
131133

132134
private:
133135
AVFilterGraph* _graph; ///< The graph which holds the filters.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy