Skip to content

Commit 3c865b2

Browse files
Dan-FloresDaniel Flores
andauthored
Remove av_q2d, remove test rounding (#797)
Co-authored-by: Daniel Flores <[email protected]>
1 parent f93d222 commit 3c865b2

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ namespace facebook::torchcodec {
1818
namespace {
1919

2020
double ptsToSeconds(int64_t pts, const AVRational& timeBase) {
21-
return static_cast<double>(pts) * av_q2d(timeBase);
21+
// To perform the multiplication before the division, av_q2d is not used
22+
return static_cast<double>(pts) * timeBase.num / timeBase.den;
2223
}
2324

2425
int64_t secondsToClosestPts(double seconds, const AVRational& timeBase) {

test/VideoDecoderTest.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,9 @@ TEST_P(SingleStreamDecoderTest, SeeksCloseToEof) {
267267
ourDecoder->addVideoStream(-1);
268268
ourDecoder->setCursorPtsInSeconds(388388. / 30'000);
269269
auto output = ourDecoder->getNextFrame();
270-
EXPECT_DOUBLE_EQ(output.ptsSeconds, 388'388. / 30'000);
270+
EXPECT_EQ(output.ptsSeconds, 388'388. / 30'000);
271271
output = ourDecoder->getNextFrame();
272-
EXPECT_DOUBLE_EQ(output.ptsSeconds, 389'389. / 30'000);
272+
EXPECT_EQ(output.ptsSeconds, 389'389. / 30'000);
273273
EXPECT_THROW(ourDecoder->getNextFrame(), std::exception);
274274
}
275275

@@ -300,7 +300,7 @@ TEST_P(SingleStreamDecoderTest, GetsFramePlayedAtTimestamp) {
300300
// Sanity check: make sure duration is strictly positive.
301301
EXPECT_GT(kPtsPlusDurationOfLastFrame, kPtsOfLastFrameInVideoStream);
302302
output = ourDecoder->getFramePlayedAt(kPtsPlusDurationOfLastFrame - 1e-6);
303-
EXPECT_DOUBLE_EQ(output.ptsSeconds, kPtsOfLastFrameInVideoStream);
303+
EXPECT_EQ(output.ptsSeconds, kPtsOfLastFrameInVideoStream);
304304
}
305305

306306
TEST_P(SingleStreamDecoderTest, SeeksToFrameWithSpecificPts) {
@@ -311,7 +311,7 @@ TEST_P(SingleStreamDecoderTest, SeeksToFrameWithSpecificPts) {
311311
ourDecoder->setCursorPtsInSeconds(6.0);
312312
auto output = ourDecoder->getNextFrame();
313313
torch::Tensor tensor6FromOurDecoder = output.data;
314-
EXPECT_DOUBLE_EQ(output.ptsSeconds, 180'180. / 30'000);
314+
EXPECT_EQ(output.ptsSeconds, 180'180. / 30'000);
315315
torch::Tensor tensor6FromFFMPEG =
316316
readTensorFromDisk("nasa_13013.mp4.time6.000000.pt");
317317
EXPECT_TRUE(torch::equal(tensor6FromOurDecoder, tensor6FromFFMPEG));
@@ -327,7 +327,7 @@ TEST_P(SingleStreamDecoderTest, SeeksToFrameWithSpecificPts) {
327327
ourDecoder->setCursorPtsInSeconds(6.1);
328328
output = ourDecoder->getNextFrame();
329329
torch::Tensor tensor61FromOurDecoder = output.data;
330-
EXPECT_DOUBLE_EQ(output.ptsSeconds, 183'183. / 30'000);
330+
EXPECT_EQ(output.ptsSeconds, 183'183. / 30'000);
331331
torch::Tensor tensor61FromFFMPEG =
332332
readTensorFromDisk("nasa_13013.mp4.time6.100000.pt");
333333
EXPECT_TRUE(torch::equal(tensor61FromOurDecoder, tensor61FromFFMPEG));
@@ -347,7 +347,7 @@ TEST_P(SingleStreamDecoderTest, SeeksToFrameWithSpecificPts) {
347347
ourDecoder->setCursorPtsInSeconds(10.0);
348348
output = ourDecoder->getNextFrame();
349349
torch::Tensor tensor10FromOurDecoder = output.data;
350-
EXPECT_DOUBLE_EQ(output.ptsSeconds, 300'300. / 30'000);
350+
EXPECT_EQ(output.ptsSeconds, 300'300. / 30'000);
351351
torch::Tensor tensor10FromFFMPEG =
352352
readTensorFromDisk("nasa_13013.mp4.time10.000000.pt");
353353
EXPECT_TRUE(torch::equal(tensor10FromOurDecoder, tensor10FromFFMPEG));
@@ -364,7 +364,7 @@ TEST_P(SingleStreamDecoderTest, SeeksToFrameWithSpecificPts) {
364364
ourDecoder->setCursorPtsInSeconds(6.0);
365365
output = ourDecoder->getNextFrame();
366366
tensor6FromOurDecoder = output.data;
367-
EXPECT_DOUBLE_EQ(output.ptsSeconds, 180'180. / 30'000);
367+
EXPECT_EQ(output.ptsSeconds, 180'180. / 30'000);
368368
EXPECT_TRUE(torch::equal(tensor6FromOurDecoder, tensor6FromFFMPEG));
369369
EXPECT_EQ(ourDecoder->getDecodeStats().numSeeksAttempted, 1);
370370
// We cannot skip a seek here because timestamp=6 has a different keyframe
@@ -379,7 +379,7 @@ TEST_P(SingleStreamDecoderTest, SeeksToFrameWithSpecificPts) {
379379
ourDecoder->setCursorPtsInSeconds(kPtsOfLastFrameInVideoStream);
380380
output = ourDecoder->getNextFrame();
381381
torch::Tensor tensor7FromOurDecoder = output.data;
382-
EXPECT_DOUBLE_EQ(output.ptsSeconds, 389'389. / 30'000);
382+
EXPECT_EQ(output.ptsSeconds, 389'389. / 30'000);
383383
torch::Tensor tensor7FromFFMPEG =
384384
readTensorFromDisk("nasa_13013.mp4.time12.979633.pt");
385385
EXPECT_TRUE(torch::equal(tensor7FromOurDecoder, tensor7FromFFMPEG));

test/test_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ def test_next(self, asset):
781781
frame, asset.get_frame_data_by_index(frame_index)
782782
)
783783
frame_info = asset.get_frame_info(frame_index)
784-
assert pts_seconds == pytest.approx(frame_info.pts_seconds)
784+
assert pts_seconds == frame_info.pts_seconds
785785
assert duration_seconds == frame_info.duration_seconds
786786
frame_index += 1
787787

@@ -985,7 +985,7 @@ def test_pts(self, asset):
985985
frames, asset.get_frame_data_by_index(frame_index)
986986
)
987987

988-
assert pts_seconds == pytest.approx(start_seconds)
988+
assert pts_seconds == start_seconds
989989

990990
def test_sample_rate_conversion(self):
991991
def get_all_frames(asset, sample_rate=None, stop_seconds=None):

0 commit comments

Comments
 (0)