Skip to content
Snippets Groups Projects
Commit 8877ce27 authored by Kateryna Kostiuk's avatar Kateryna Kostiuk
Browse files

video: improve CPU usage on decoding

When frame decoding is fast, av_read_frame called before new data
available. It will cause high CPU usage.
This patch adds wait time when no data is available.

Change-Id: I675092e2497f969976f797cc1bbb6b466b9dbcb5
parent 8997bb00
Branches
No related tags found
No related merge requests found
......@@ -325,6 +325,19 @@ MediaDemuxer::decode()
int ret = av_read_frame(inputCtx_, packet.get());
if (ret == AVERROR(EAGAIN)) {
/*no data available. Calculate time until next frame.
We do not use the emulated frame mechanism from the decoder because it will affect all platforms.
With the current implementation, the demuxer will be waiting just in case when av_read_frame
returns EAGAIN. For some platforms, av_read_frame is blocking and it will never happen.
*/
if (inputParams_.framerate.numerator() == 0)
return Status::Success;
rational<double> frameTime = 1e6/inputParams_.framerate;
int64_t timeToSleep = lastReadPacketTime_ - av_gettime_relative() + frameTime.real<int64_t>();
if (timeToSleep <= 0) {
return Status::Success;
}
std::this_thread::sleep_for(std::chrono::microseconds(timeToSleep));
return Status::Success;
} else if (ret == AVERROR_EOF) {
return Status::EndOfFile;
......@@ -338,6 +351,8 @@ MediaDemuxer::decode()
return Status::Success;
}
lastReadPacketTime_ = av_gettime_relative();
auto& cb = streams_[streamIndex];
if (cb) {
DecodeStatus ret = cb(*packet.get());
......
......@@ -131,6 +131,7 @@ private:
AVFormatContext* inputCtx_ = nullptr;
std::vector<StreamCallback> streams_;
int64_t startTime_;
int64_t lastReadPacketTime_ {};
DeviceParams inputParams_;
AVDictionary* options_ = nullptr;
MediaDemuxer::CurrentState currentState_;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment