You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
8.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  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. #include "rtc_tools/video_file_reader.h"
  11. #include <cstdio>
  12. #include <string>
  13. #include <vector>
  14. #include "absl/strings/match.h"
  15. #include "absl/types/optional.h"
  16. #include "api/video/i420_buffer.h"
  17. #include "rtc_base/checks.h"
  18. #include "rtc_base/logging.h"
  19. #include "rtc_base/ref_counted_object.h"
  20. #include "rtc_base/string_encode.h"
  21. #include "rtc_base/string_to_number.h"
  22. namespace webrtc {
  23. namespace test {
  24. namespace {
  25. bool ReadBytes(uint8_t* dst, size_t n, FILE* file) {
  26. return fread(reinterpret_cast<char*>(dst), /* size= */ 1, n, file) == n;
  27. }
  28. // Common base class for .yuv and .y4m files.
  29. class VideoFile : public Video {
  30. public:
  31. VideoFile(int width,
  32. int height,
  33. const std::vector<fpos_t>& frame_positions,
  34. FILE* file)
  35. : width_(width),
  36. height_(height),
  37. frame_positions_(frame_positions),
  38. file_(file) {}
  39. ~VideoFile() override { fclose(file_); }
  40. size_t number_of_frames() const override { return frame_positions_.size(); }
  41. int width() const override { return width_; }
  42. int height() const override { return height_; }
  43. rtc::scoped_refptr<I420BufferInterface> GetFrame(
  44. size_t frame_index) const override {
  45. RTC_CHECK_LT(frame_index, frame_positions_.size());
  46. fsetpos(file_, &frame_positions_[frame_index]);
  47. rtc::scoped_refptr<I420Buffer> buffer = I420Buffer::Create(width_, height_);
  48. if (!ReadBytes(buffer->MutableDataY(), width_ * height_, file_) ||
  49. !ReadBytes(buffer->MutableDataU(),
  50. buffer->ChromaWidth() * buffer->ChromaHeight(), file_) ||
  51. !ReadBytes(buffer->MutableDataV(),
  52. buffer->ChromaWidth() * buffer->ChromaHeight(), file_)) {
  53. RTC_LOG(LS_ERROR) << "Could not read YUV data for frame " << frame_index;
  54. return nullptr;
  55. }
  56. return buffer;
  57. }
  58. private:
  59. const int width_;
  60. const int height_;
  61. const std::vector<fpos_t> frame_positions_;
  62. FILE* const file_;
  63. };
  64. } // namespace
  65. Video::Iterator::Iterator(const rtc::scoped_refptr<const Video>& video,
  66. size_t index)
  67. : video_(video), index_(index) {}
  68. Video::Iterator::Iterator(const Video::Iterator& other) = default;
  69. Video::Iterator::Iterator(Video::Iterator&& other) = default;
  70. Video::Iterator& Video::Iterator::operator=(Video::Iterator&&) = default;
  71. Video::Iterator& Video::Iterator::operator=(const Video::Iterator&) = default;
  72. Video::Iterator::~Iterator() = default;
  73. rtc::scoped_refptr<I420BufferInterface> Video::Iterator::operator*() const {
  74. return video_->GetFrame(index_);
  75. }
  76. bool Video::Iterator::operator==(const Video::Iterator& other) const {
  77. return index_ == other.index_;
  78. }
  79. bool Video::Iterator::operator!=(const Video::Iterator& other) const {
  80. return !(*this == other);
  81. }
  82. Video::Iterator Video::Iterator::operator++(int) {
  83. const Iterator copy = *this;
  84. ++*this;
  85. return copy;
  86. }
  87. Video::Iterator& Video::Iterator::operator++() {
  88. ++index_;
  89. return *this;
  90. }
  91. Video::Iterator Video::begin() const {
  92. return Iterator(this, 0);
  93. }
  94. Video::Iterator Video::end() const {
  95. return Iterator(this, number_of_frames());
  96. }
  97. rtc::scoped_refptr<Video> OpenY4mFile(const std::string& file_name) {
  98. FILE* file = fopen(file_name.c_str(), "rb");
  99. if (file == nullptr) {
  100. RTC_LOG(LS_ERROR) << "Could not open input file for reading: " << file_name;
  101. return nullptr;
  102. }
  103. int parse_file_header_result = -1;
  104. if (fscanf(file, "YUV4MPEG2 %n", &parse_file_header_result) != 0 ||
  105. parse_file_header_result == -1) {
  106. RTC_LOG(LS_ERROR) << "File " << file_name
  107. << " does not start with YUV4MPEG2 header";
  108. return nullptr;
  109. }
  110. std::string header_line;
  111. while (true) {
  112. const int c = fgetc(file);
  113. if (c == EOF) {
  114. RTC_LOG(LS_ERROR) << "Could not read header line";
  115. return nullptr;
  116. }
  117. if (c == '\n')
  118. break;
  119. header_line.push_back(static_cast<char>(c));
  120. }
  121. absl::optional<int> width;
  122. absl::optional<int> height;
  123. absl::optional<float> fps;
  124. std::vector<std::string> fields;
  125. rtc::tokenize(header_line, ' ', &fields);
  126. for (const std::string& field : fields) {
  127. const char prefix = field.front();
  128. const std::string suffix = field.substr(1);
  129. switch (prefix) {
  130. case 'W':
  131. width = rtc::StringToNumber<int>(suffix);
  132. break;
  133. case 'H':
  134. height = rtc::StringToNumber<int>(suffix);
  135. break;
  136. case 'C':
  137. if (suffix != "420" && suffix != "420mpeg2") {
  138. RTC_LOG(LS_ERROR)
  139. << "Does not support any other color space than I420 or "
  140. "420mpeg2, but was: "
  141. << suffix;
  142. return nullptr;
  143. }
  144. break;
  145. case 'F': {
  146. std::vector<std::string> fraction;
  147. rtc::tokenize(suffix, ':', &fraction);
  148. if (fraction.size() == 2) {
  149. const absl::optional<int> numerator =
  150. rtc::StringToNumber<int>(fraction[0]);
  151. const absl::optional<int> denominator =
  152. rtc::StringToNumber<int>(fraction[1]);
  153. if (numerator && denominator && *denominator != 0)
  154. fps = *numerator / static_cast<float>(*denominator);
  155. break;
  156. }
  157. }
  158. }
  159. }
  160. if (!width || !height) {
  161. RTC_LOG(LS_ERROR) << "Could not find width and height in file header";
  162. return nullptr;
  163. }
  164. if (!fps) {
  165. RTC_LOG(LS_ERROR) << "Could not find fps in file header";
  166. return nullptr;
  167. }
  168. RTC_LOG(LS_INFO) << "Video has resolution: " << *width << "x" << *height
  169. << " " << *fps << " fps";
  170. if (*width % 2 != 0 || *height % 2 != 0) {
  171. RTC_LOG(LS_ERROR)
  172. << "Only supports even width/height so that chroma size is a "
  173. "whole number.";
  174. return nullptr;
  175. }
  176. const int i420_frame_size = 3 * *width * *height / 2;
  177. std::vector<fpos_t> frame_positions;
  178. while (true) {
  179. int parse_frame_header_result = -1;
  180. if (fscanf(file, "FRAME\n%n", &parse_frame_header_result) != 0 ||
  181. parse_frame_header_result == -1) {
  182. if (!feof(file)) {
  183. RTC_LOG(LS_ERROR) << "Did not find FRAME header, ignoring rest of file";
  184. }
  185. break;
  186. }
  187. fpos_t pos;
  188. fgetpos(file, &pos);
  189. frame_positions.push_back(pos);
  190. // Skip over YUV pixel data.
  191. fseek(file, i420_frame_size, SEEK_CUR);
  192. }
  193. if (frame_positions.empty()) {
  194. RTC_LOG(LS_ERROR) << "Could not find any frames in the file";
  195. return nullptr;
  196. }
  197. RTC_LOG(LS_INFO) << "Video has " << frame_positions.size() << " frames";
  198. return new rtc::RefCountedObject<VideoFile>(*width, *height, frame_positions,
  199. file);
  200. }
  201. rtc::scoped_refptr<Video> OpenYuvFile(const std::string& file_name,
  202. int width,
  203. int height) {
  204. FILE* file = fopen(file_name.c_str(), "rb");
  205. if (file == nullptr) {
  206. RTC_LOG(LS_ERROR) << "Could not open input file for reading: " << file_name;
  207. return nullptr;
  208. }
  209. if (width % 2 != 0 || height % 2 != 0) {
  210. RTC_LOG(LS_ERROR)
  211. << "Only supports even width/height so that chroma size is a "
  212. "whole number.";
  213. return nullptr;
  214. }
  215. // Seek to end of file.
  216. fseek(file, 0, SEEK_END);
  217. const size_t file_size = ftell(file);
  218. // Seek back to beginning of file.
  219. fseek(file, 0, SEEK_SET);
  220. const int i420_frame_size = 3 * width * height / 2;
  221. const size_t number_of_frames = file_size / i420_frame_size;
  222. std::vector<fpos_t> frame_positions;
  223. for (size_t i = 0; i < number_of_frames; ++i) {
  224. fpos_t pos;
  225. fgetpos(file, &pos);
  226. frame_positions.push_back(pos);
  227. fseek(file, i420_frame_size, SEEK_CUR);
  228. }
  229. if (frame_positions.empty()) {
  230. RTC_LOG(LS_ERROR) << "Could not find any frames in the file";
  231. return nullptr;
  232. }
  233. RTC_LOG(LS_INFO) << "Video has " << frame_positions.size() << " frames";
  234. return new rtc::RefCountedObject<VideoFile>(width, height, frame_positions,
  235. file);
  236. }
  237. rtc::scoped_refptr<Video> OpenYuvOrY4mFile(const std::string& file_name,
  238. int width,
  239. int height) {
  240. if (absl::EndsWith(file_name, ".yuv"))
  241. return OpenYuvFile(file_name, width, height);
  242. if (absl::EndsWith(file_name, ".y4m"))
  243. return OpenY4mFile(file_name);
  244. RTC_LOG(LS_ERROR) << "Video file does not end in either .yuv or .y4m: "
  245. << file_name;
  246. return nullptr;
  247. }
  248. } // namespace test
  249. } // namespace webrtc