Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: compensate codec latency in positive offset #320

Merged
merged 8 commits into from
Jun 29, 2020
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
*.la
*.a

build
build*
dist
install*
test/**/test*[.wav|.avi|.mov|.h264|.mxf]

# CMake
CMakeCache.txt
Expand Down
4 changes: 4 additions & 0 deletions src/AvTranscoder/decoder/AudioDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ bool AudioDecoder::decodeNextFrame(IFrame& frameBuffer)
return false;
}
}

if(decodeNextFrame)
incrementNbDecodedFrames(frameBuffer.getAVFrame().nb_samples);

return decodeNextFrame;
}

Expand Down
2 changes: 2 additions & 0 deletions src/AvTranscoder/decoder/AudioGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ bool AudioGenerator::decodeNextFrame(IFrame& frameBuffer)
LOG_DEBUG("Convert data of the audio specified when decode next frame")
_audioTransform.convert(*_inputFrame, frameBuffer);
}

incrementNbDecodedFrames(_silent->getNbSamplesPerChannel());
return true;
}

Expand Down
19 changes: 19 additions & 0 deletions src/AvTranscoder/decoder/IDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ namespace avtranscoder

class AvExport IDecoder
{
protected:
IDecoder()
: _decoded_frames_counter(0)
{
}

public:



virtual ~IDecoder(){};

/**
Expand Down Expand Up @@ -51,6 +60,16 @@ class AvExport IDecoder
* @note Not sense for generators.
*/
virtual void flushDecoder() {}

size_t getNbDecodedFrames() { return _decoded_frames_counter; }

protected:
void incrementNbDecodedFrames(const size_t& nb_frames = 1) {
_decoded_frames_counter += nb_frames;
}

private:
size_t _decoded_frames_counter;
};
}

Expand Down
4 changes: 4 additions & 0 deletions src/AvTranscoder/decoder/VideoDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ bool VideoDecoder::decodeNextFrame(IFrame& frameBuffer)
return false;
}
}

if(decodeNextFrame)
incrementNbDecodedFrames();

return decodeNextFrame;
}

Expand Down
2 changes: 2 additions & 0 deletions src/AvTranscoder/decoder/VideoGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ bool VideoGenerator::decodeNextFrame(IFrame& frameBuffer)
LOG_DEBUG("Convert data of the image specified when decode next frame")
_videoTransform.convert(*_inputFrame, frameBuffer);
}

incrementNbDecodedFrames();
return true;
}

Expand Down
32 changes: 29 additions & 3 deletions src/AvTranscoder/transcoder/StreamTranscoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,20 @@ bool StreamTranscoder::processFrame()
// Manage offset
if(_offset > 0)
{
const bool endOfOffset = _outputStream->getStreamDuration() >= _offset;
bool endOfOffset = false;
if(_currentDecoder == _generators.at(0))
{
const double fps = 1.0 * _outputEncoder->getCodec().getAVCodecContext().time_base.den /
(_outputEncoder->getCodec().getAVCodecContext().time_base.num * _outputEncoder->getCodec().getAVCodecContext().ticks_per_frame);
const double frame_duration = 1.0 / fps;
const double generated_duration = _currentDecoder->getNbDecodedFrames() * frame_duration;
endOfOffset = generated_duration >= _offset;
}
else
{
endOfOffset = _outputStream->getStreamDuration() >= _offset;
}

if(endOfOffset)
{
LOG_INFO("End of positive offset")
Expand Down Expand Up @@ -704,6 +717,7 @@ bool StreamTranscoder::processTranscode()
assert(_outputEncoder != NULL);
assert(! _decodedData.empty());
assert(_transform != NULL);
assert(_generators.size() == _inputDecoders.size());

LOG_DEBUG("StreamTranscoder::processTranscode")

Expand Down Expand Up @@ -750,8 +764,7 @@ bool StreamTranscoder::processTranscode()
}
}

// Transform
CodedData data;
// Check decoding status
bool continueProcess = true;
for(size_t index = 0; index < decodingStatus.size(); ++index)
{
Expand All @@ -760,6 +773,17 @@ bool StreamTranscoder::processTranscode()
if(!_filterGraph->hasFilters() || !_filterGraph->hasBufferedFrames(index))
{
continueProcess = false;
if(_needToSwitchToGenerator)
{
switchToGeneratorDecoder();
LOG_INFO("Force reallocation of the decoded data buffers since the decoders could have cleared them.")
for(std::vector<IFrame*>::iterator it = _decodedData.begin(); it != _decodedData.end(); ++it)
{
if(! (*it)->isDataAllocated())
(*it)->allocateData();
}
return processTranscode();
}
break;
}
LOG_DEBUG("Some frames remain into filter graph buffer " << index);
Expand All @@ -771,8 +795,10 @@ bool StreamTranscoder::processTranscode()
}
}

CodedData data;
if(continueProcess)
{
// Transform
IFrame* dataToTransform = NULL;
if(_filterGraph->hasFilters())
{
Expand Down