Update y4m header parser.

Bug: none
Change-Id: Ice21cbb3532c608ac829c898c656170ea45f35bb
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/346260
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Philip Eliasson <philipel@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#42024}
This commit is contained in:
philipel 2024-04-09 11:21:49 +02:00 committed by WebRTC LUCI CQ
parent 8d079bea2a
commit 25468d2405

View File

@ -10,12 +10,14 @@
#include <stdio.h>
#include <charconv>
#include <string>
#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<absl::string_view> 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";