From 25468d240517727e7a7dab86431dc14e45789c93 Mon Sep 17 00:00:00 2001 From: philipel Date: Tue, 9 Apr 2024 11:21:49 +0200 Subject: [PATCH] Update y4m header parser. Bug: none Change-Id: Ice21cbb3532c608ac829c898c656170ea45f35bb Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/346260 Reviewed-by: Mirko Bonadei Commit-Queue: Philip Eliasson Cr-Commit-Position: refs/heads/main@{#42024} --- test/testsupport/y4m_frame_reader.cc | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/test/testsupport/y4m_frame_reader.cc b/test/testsupport/y4m_frame_reader.cc index 72fb9b5188..00d1fb8e26 100644 --- a/test/testsupport/y4m_frame_reader.cc +++ b/test/testsupport/y4m_frame_reader.cc @@ -10,12 +10,14 @@ #include +#include #include #include "api/scoped_refptr.h" #include "api/video/i420_buffer.h" #include "common_video/libyuv/include/webrtc_libyuv.h" #include "rtc_base/logging.h" +#include "rtc_base/string_encode.h" #include "rtc_base/strings/string_builder.h" #include "test/testsupport/file_utils.h" #include "test/testsupport/frame_reader.h" @@ -38,10 +40,29 @@ void ParseY4mHeader(std::string filepath, << "File " << filepath << " is too small"; fclose(file); - RTC_CHECK(sscanf(h, "YUV4MPEG2 W%d H%d", &resolution->width, - &resolution->height) == 2) + std::vector header = rtc::split(h, ' '); + RTC_CHECK(!header.empty() && header[0] == "YUV4MPEG2") << filepath << " is not a valid Y4M file"; + for (size_t i = 1; i < header.size(); ++i) { + RTC_CHECK(!header[i].empty()); + switch (header[i][0]) { + case 'W': { + auto n = header[i].substr(1); + std::from_chars(n.data(), n.data() + n.size(), resolution->width); + continue; + } + case 'H': { + auto n = header[i].substr(1); + std::from_chars(n.data(), n.data() + n.size(), resolution->height); + continue; + } + default: { + continue; + } + } + } + RTC_CHECK_GT(resolution->width, 0) << "Width must be positive"; RTC_CHECK_GT(resolution->height, 0) << "Height must be positive";