blob: b01fc0fcddb23ce43cfeab2fac1e6048c15032aa [file] [log] [blame]
Magnus Jedvert10e829a2018-09-05 10:46:18 +02001/*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "rtc_tools/video_file_reader.h"
12
Magnus Jedvert10e829a2018-09-05 10:46:18 +020013#include <cstdio>
14#include <string>
Yves Gerey3e707812018-11-28 16:47:49 +010015#include <vector>
Magnus Jedvert10e829a2018-09-05 10:46:18 +020016
Steve Anton68586e82018-12-13 17:41:25 -080017#include "absl/strings/match.h"
Magnus Jedvert10e829a2018-09-05 10:46:18 +020018#include "absl/types/optional.h"
19#include "api/video/i420_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "rtc_base/checks.h"
Magnus Jedvert10e829a2018-09-05 10:46:18 +020021#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080022#include "rtc_base/ref_counted_object.h"
23#include "rtc_base/string_encode.h"
Magnus Jedvert10e829a2018-09-05 10:46:18 +020024#include "rtc_base/string_to_number.h"
Magnus Jedvert10e829a2018-09-05 10:46:18 +020025
26namespace webrtc {
27namespace test {
28
29namespace {
30
Hans Wennborgb4f7ab12019-02-13 09:13:23 +010031bool ReadBytes(uint8_t* dst, size_t n, FILE* file) {
32 return fread(reinterpret_cast<char*>(dst), /* size= */ 1, n, file) == n;
33}
34
Magnus Jedvert10e829a2018-09-05 10:46:18 +020035// Common base class for .yuv and .y4m files.
36class VideoFile : public Video {
37 public:
38 VideoFile(int width,
39 int height,
40 const std::vector<fpos_t>& frame_positions,
41 FILE* file)
42 : width_(width),
43 height_(height),
44 frame_positions_(frame_positions),
45 file_(file) {}
46
47 ~VideoFile() override { fclose(file_); }
48
49 size_t number_of_frames() const override { return frame_positions_.size(); }
50 int width() const override { return width_; }
51 int height() const override { return height_; }
52
53 rtc::scoped_refptr<I420BufferInterface> GetFrame(
54 size_t frame_index) const override {
55 RTC_CHECK_LT(frame_index, frame_positions_.size());
56
57 fsetpos(file_, &frame_positions_[frame_index]);
58 rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(width_, height_);
Magnus Jedvert10e829a2018-09-05 10:46:18 +020059
Hans Wennborgb4f7ab12019-02-13 09:13:23 +010060 if (!ReadBytes(buffer->MutableDataY(), width_ * height_, file_) ||
61 !ReadBytes(buffer->MutableDataU(),
62 buffer->ChromaWidth() * buffer->ChromaHeight(), file_) ||
63 !ReadBytes(buffer->MutableDataV(),
64 buffer->ChromaWidth() * buffer->ChromaHeight(), file_)) {
Magnus Jedvert10e829a2018-09-05 10:46:18 +020065 RTC_LOG(LS_ERROR) << "Could not read YUV data for frame " << frame_index;
66 return nullptr;
67 }
Hans Wennborgb4f7ab12019-02-13 09:13:23 +010068
Magnus Jedvert10e829a2018-09-05 10:46:18 +020069 return buffer;
70 }
71
72 private:
73 const int width_;
74 const int height_;
75 const std::vector<fpos_t> frame_positions_;
76 FILE* const file_;
77};
78
79} // namespace
80
81Video::Iterator::Iterator(const rtc::scoped_refptr<const Video>& video,
82 size_t index)
83 : video_(video), index_(index) {}
84
85Video::Iterator::Iterator(const Video::Iterator& other) = default;
86Video::Iterator::Iterator(Video::Iterator&& other) = default;
87Video::Iterator& Video::Iterator::operator=(Video::Iterator&&) = default;
88Video::Iterator& Video::Iterator::operator=(const Video::Iterator&) = default;
89Video::Iterator::~Iterator() = default;
90
91rtc::scoped_refptr<I420BufferInterface> Video::Iterator::operator*() const {
92 return video_->GetFrame(index_);
93}
94bool Video::Iterator::operator==(const Video::Iterator& other) const {
95 return index_ == other.index_;
96}
97bool Video::Iterator::operator!=(const Video::Iterator& other) const {
98 return !(*this == other);
99}
100
101Video::Iterator Video::Iterator::operator++(int) {
102 const Iterator copy = *this;
103 ++*this;
104 return copy;
105}
106
107Video::Iterator& Video::Iterator::operator++() {
108 ++index_;
109 return *this;
110}
111
112Video::Iterator Video::begin() const {
113 return Iterator(this, 0);
114}
115
116Video::Iterator Video::end() const {
117 return Iterator(this, number_of_frames());
118}
119
120rtc::scoped_refptr<Video> OpenY4mFile(const std::string& file_name) {
121 FILE* file = fopen(file_name.c_str(), "rb");
122 if (file == nullptr) {
123 RTC_LOG(LS_ERROR) << "Could not open input file for reading: " << file_name;
124 return nullptr;
125 }
126
127 int parse_file_header_result = -1;
Hans Wennborgb4f7ab12019-02-13 09:13:23 +0100128 if (fscanf(file, "YUV4MPEG2 %n", &parse_file_header_result) != 0 ||
129 parse_file_header_result == -1) {
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200130 RTC_LOG(LS_ERROR) << "File " << file_name
131 << " does not start with YUV4MPEG2 header";
132 return nullptr;
133 }
134
135 std::string header_line;
136 while (true) {
137 const int c = fgetc(file);
138 if (c == EOF) {
139 RTC_LOG(LS_ERROR) << "Could not read header line";
140 return nullptr;
141 }
142 if (c == '\n')
143 break;
144 header_line.push_back(static_cast<char>(c));
145 }
146
147 absl::optional<int> width;
148 absl::optional<int> height;
149 absl::optional<float> fps;
150
151 std::vector<std::string> fields;
152 rtc::tokenize(header_line, ' ', &fields);
153 for (const std::string& field : fields) {
154 const char prefix = field.front();
155 const std::string suffix = field.substr(1);
156 switch (prefix) {
157 case 'W':
158 width = rtc::StringToNumber<int>(suffix);
159 break;
160 case 'H':
161 height = rtc::StringToNumber<int>(suffix);
162 break;
163 case 'C':
164 if (suffix != "420" && suffix != "420mpeg2") {
165 RTC_LOG(LS_ERROR)
166 << "Does not support any other color space than I420 or "
167 "420mpeg2, but was: "
168 << suffix;
169 return nullptr;
170 }
171 break;
172 case 'F': {
173 std::vector<std::string> fraction;
174 rtc::tokenize(suffix, ':', &fraction);
175 if (fraction.size() == 2) {
176 const absl::optional<int> numerator =
177 rtc::StringToNumber<int>(fraction[0]);
178 const absl::optional<int> denominator =
179 rtc::StringToNumber<int>(fraction[1]);
180 if (numerator && denominator && *denominator != 0)
181 fps = *numerator / static_cast<float>(*denominator);
182 break;
183 }
184 }
185 }
186 }
187 if (!width || !height) {
188 RTC_LOG(LS_ERROR) << "Could not find width and height in file header";
189 return nullptr;
190 }
191 if (!fps) {
192 RTC_LOG(LS_ERROR) << "Could not find fps in file header";
193 return nullptr;
194 }
195 RTC_LOG(LS_INFO) << "Video has resolution: " << *width << "x" << *height
196 << " " << *fps << " fps";
197 if (*width % 2 != 0 || *height % 2 != 0) {
198 RTC_LOG(LS_ERROR)
199 << "Only supports even width/height so that chroma size is a "
200 "whole number.";
201 return nullptr;
202 }
203
204 const int i420_frame_size = 3 * *width * *height / 2;
205 std::vector<fpos_t> frame_positions;
206 while (true) {
207 int parse_frame_header_result = -1;
Hans Wennborgb4f7ab12019-02-13 09:13:23 +0100208 if (fscanf(file, "FRAME\n%n", &parse_frame_header_result) != 0 ||
209 parse_frame_header_result == -1) {
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200210 if (!feof(file)) {
211 RTC_LOG(LS_ERROR) << "Did not find FRAME header, ignoring rest of file";
212 }
213 break;
214 }
215 fpos_t pos;
216 fgetpos(file, &pos);
217 frame_positions.push_back(pos);
218 // Skip over YUV pixel data.
219 fseek(file, i420_frame_size, SEEK_CUR);
220 }
221 if (frame_positions.empty()) {
222 RTC_LOG(LS_ERROR) << "Could not find any frames in the file";
223 return nullptr;
224 }
225 RTC_LOG(LS_INFO) << "Video has " << frame_positions.size() << " frames";
226
227 return new rtc::RefCountedObject<VideoFile>(*width, *height, frame_positions,
228 file);
229}
230
231rtc::scoped_refptr<Video> OpenYuvFile(const std::string& file_name,
232 int width,
233 int height) {
234 FILE* file = fopen(file_name.c_str(), "rb");
235 if (file == nullptr) {
236 RTC_LOG(LS_ERROR) << "Could not open input file for reading: " << file_name;
237 return nullptr;
238 }
239
240 if (width % 2 != 0 || height % 2 != 0) {
241 RTC_LOG(LS_ERROR)
242 << "Only supports even width/height so that chroma size is a "
243 "whole number.";
244 return nullptr;
245 }
246
247 // Seek to end of file.
248 fseek(file, 0, SEEK_END);
249 const size_t file_size = ftell(file);
250 // Seek back to beginning of file.
251 fseek(file, 0, SEEK_SET);
252
253 const int i420_frame_size = 3 * width * height / 2;
254 const size_t number_of_frames = file_size / i420_frame_size;
255
256 std::vector<fpos_t> frame_positions;
257 for (size_t i = 0; i < number_of_frames; ++i) {
258 fpos_t pos;
259 fgetpos(file, &pos);
260 frame_positions.push_back(pos);
261 fseek(file, i420_frame_size, SEEK_CUR);
262 }
263 if (frame_positions.empty()) {
264 RTC_LOG(LS_ERROR) << "Could not find any frames in the file";
265 return nullptr;
266 }
267 RTC_LOG(LS_INFO) << "Video has " << frame_positions.size() << " frames";
268
269 return new rtc::RefCountedObject<VideoFile>(width, height, frame_positions,
270 file);
271}
272
273rtc::scoped_refptr<Video> OpenYuvOrY4mFile(const std::string& file_name,
274 int width,
275 int height) {
Steve Anton68586e82018-12-13 17:41:25 -0800276 if (absl::EndsWith(file_name, ".yuv"))
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200277 return OpenYuvFile(file_name, width, height);
Steve Anton68586e82018-12-13 17:41:25 -0800278 if (absl::EndsWith(file_name, ".y4m"))
Magnus Jedvertb468ace2018-09-05 16:11:48 +0200279 return OpenY4mFile(file_name);
Magnus Jedvert10e829a2018-09-05 10:46:18 +0200280
281 RTC_LOG(LS_ERROR) << "Video file does not end in either .yuv or .y4m: "
282 << file_name;
283
284 return nullptr;
285}
286
287} // namespace test
288} // namespace webrtc