Skip to content

Commit

Permalink
Add VFR detection (#3921)
Browse files Browse the repository at this point in the history
Add VFR check to FramesDecoder.

Signed-off-by: Albert Wolant <[email protected]>
  • Loading branch information
awolant authored May 26, 2022
1 parent 0954b18 commit 77f4882
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
18 changes: 18 additions & 0 deletions dali/operators/reader/loader/video/frames_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ FramesDecoder::FramesDecoder(const std::string &filename)
" Supported codecs: h264, HEVC."));
InitAvState();
BuildIndex();
DetectVfr();
}

void FramesDecoder::BuildIndex() {
Expand Down Expand Up @@ -134,6 +135,23 @@ void FramesDecoder::BuildIndex() {
Reset();
}

void FramesDecoder::DetectVfr() {
if (NumFrames() < 3) {
is_vfr_ = false;
return;
}

int pts_step = index_[1].pts - index_[0].pts;
for (int frame_id = 2; frame_id < NumFrames(); ++frame_id) {
if ((index_[frame_id].pts - index_[frame_id - 1].pts) != pts_step) {
is_vfr_ = true;
return;
}
}

is_vfr_ = false;
}

void FramesDecoder::CopyToOutput(uint8_t *data) {
LazyInitSwContext();

Expand Down
11 changes: 11 additions & 0 deletions dali/operators/reader/loader/video/frames_decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ class DLL_PUBLIC FramesDecoder {
return Channels() * Width() * Height();
}

/**
* @brief Is video variable frame rate
*
*/
bool IsVfr() const {
return is_vfr_;
}

/**
* @brief Reads next frame of the video and copies it to the provided buffer, if copy_to_output is True.
*
Expand Down Expand Up @@ -203,9 +211,12 @@ class DLL_PUBLIC FramesDecoder {

bool CheckCodecSupport();

void DetectVfr();

int channels_ = 3;
bool flush_state_ = false;
std::string filename_;
bool is_vfr_ = false;
};
} // namespace dali

Expand Down
1 change: 1 addition & 0 deletions dali/operators/reader/loader/video/frames_decoder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class FramesDecoderTestBase : public VideoTestBase {
ASSERT_EQ(decoder.Width(), ground_truth.Width());
ASSERT_EQ(decoder.Channels(), ground_truth.NumChannels());
ASSERT_EQ(decoder.NumFrames(), ground_truth.NumFrames());
ASSERT_EQ(decoder.IsVfr(), ground_truth.IsVfr());

// Iterate through the whole video in order
for (int i = 0; i < decoder.NumFrames(); ++i) {
Expand Down
2 changes: 2 additions & 0 deletions dali/operators/reader/loader/video/video_test_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class TestVideo {

void CompareFrameAvgError(int frame_id, const uint8_t *frame, double eps = 1.0);

bool IsVfr() { return is_vfr_; }

std::vector<cv::Mat> frames_;
bool is_vfr_ = false;
};
Expand Down

0 comments on commit 77f4882

Please sign in to comment.