Fix IvfFileReader to support different time scales

Bug: b/237998511
Change-Id: I8e7766b38cfe19b2fb58853c9614e4d32ea34715
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/285087
Reviewed-by: Rasmus Brandt <brandtr@webrtc.org>
Commit-Queue: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38737}
diff --git a/modules/video_coding/utility/ivf_file_reader.cc b/modules/video_coding/utility/ivf_file_reader.cc
index 85d1fa0..13092b5 100644
--- a/modules/video_coding/utility/ivf_file_reader.cc
+++ b/modules/video_coding/utility/ivf_file_reader.cc
@@ -30,6 +30,9 @@
 constexpr uint8_t kAv1Header[kCodecTypeBytesCount] = {'A', 'V', '0', '1'};
 constexpr uint8_t kH264Header[kCodecTypeBytesCount] = {'H', '2', '6', '4'};
 
+// RTP standard required 90kHz clock rate.
+constexpr int32_t kRtpClockRateHz = 90000;
+
 }  // namespace
 
 std::unique_ptr<IvfFileReader> IvfFileReader::Create(FileWrapper file) {
@@ -77,13 +80,9 @@
     return false;
   }
 
-  uint32_t time_scale = ByteReader<uint32_t>::ReadLittleEndian(&ivf_header[16]);
-  if (time_scale == 1000) {
-    using_capture_timestamps_ = true;
-  } else if (time_scale == 90000) {
-    using_capture_timestamps_ = false;
-  } else {
-    RTC_LOG(LS_ERROR) << "Invalid IVF header: Unknown time scale";
+  time_scale_ = ByteReader<uint32_t>::ReadLittleEndian(&ivf_header[16]);
+  if (time_scale_ == 0) {
+    RTC_LOG(LS_ERROR) << "Invalid IVF header: time scale can't be 0";
     return false;
   }
 
@@ -106,8 +105,7 @@
   const char* codec_name = CodecTypeToPayloadString(codec_type_);
   RTC_LOG(LS_INFO) << "Opened IVF file with codec data of type " << codec_name
                    << " at resolution " << width_ << " x " << height_
-                   << ", using " << (using_capture_timestamps_ ? "1" : "90")
-                   << "kHz clock resolution.";
+                   << ", using " << time_scale_ << "Hz clock resolution.";
 
   return true;
 }
@@ -157,12 +155,9 @@
   }
 
   EncodedImage image;
-  if (using_capture_timestamps_) {
-    image.capture_time_ms_ = current_timestamp;
-    image.SetTimestamp(static_cast<uint32_t>(90 * current_timestamp));
-  } else {
-    image.SetTimestamp(static_cast<uint32_t>(current_timestamp));
-  }
+  image.capture_time_ms_ = current_timestamp;
+  image.SetTimestamp(
+      static_cast<uint32_t>(current_timestamp * kRtpClockRateHz / time_scale_));
   image.SetEncodedData(payload);
   image.SetSpatialIndex(static_cast<int>(layer_sizes.size()) - 1);
   for (size_t i = 0; i < layer_sizes.size(); ++i) {
diff --git a/modules/video_coding/utility/ivf_file_reader.h b/modules/video_coding/utility/ivf_file_reader.h
index 75f2e3a..db4fc25 100644
--- a/modules/video_coding/utility/ivf_file_reader.h
+++ b/modules/video_coding/utility/ivf_file_reader.h
@@ -70,7 +70,7 @@
   size_t num_read_frames_;
   uint16_t width_;
   uint16_t height_;
-  bool using_capture_timestamps_;
+  uint32_t time_scale_;
   FileWrapper file_;
 
   absl::optional<FrameHeader> next_frame_header_;