From bc73ede578b3a838875ac2ee69c259c154a246d3 Mon Sep 17 00:00:00 2001 From: kxxt Date: Tue, 30 May 2023 11:45:07 +0800 Subject: [PATCH] Fix Frame::GetSamplesPerFrame when channels = 0 This function is relying on the implementation defined behavior of x86_64 to work properly. On x86_64, `fmod` returns -NaN when `channels = 0`, it is handled by `if (samples_per_frame < 0)`. But on other architectures(like riscv64), `fmod` may return a positive NaN which will cause this function to return a bad value. It is causing several tests to spin forever on riscv64. This PR fixes it by directly return 0 when `channels == 0` so that we do not need to deal with NaNs. --- src/Frame.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Frame.cpp b/src/Frame.cpp index e6b9af47c..85d7fd20f 100644 --- a/src/Frame.cpp +++ b/src/Frame.cpp @@ -455,6 +455,10 @@ void Frame::SetFrameNumber(int64_t new_number) // Calculate the # of samples per video frame (for a specific frame number and frame rate) int Frame::GetSamplesPerFrame(int64_t number, Fraction fps, int sample_rate, int channels) { + // Directly return 0 if there are no channels + // so that we do not need to deal with NaNs later + if (channels == 0) return 0; + // Get the total # of samples for the previous frame, and the current frame (rounded) double fps_rate = fps.Reciprocal().ToDouble();