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
11 #include <stdio.h>
12
13 #include <string>
14
15 #include "api/scoped_refptr.h"
16 #include "api/video/i420_buffer.h"
17 #include "rtc_base/logging.h"
18 #include "rtc_base/strings/string_builder.h"
19 #include "test/testsupport/file_utils.h"
20 #include "test/testsupport/frame_reader.h"
21
22 namespace webrtc {
23 namespace test {
24 namespace {
25
26 // Size of header: "YUV4MPEG2 WXXXXXX HXXXXXX FXXX:1 C420\n"
27 // We allocate up to 6 digits for width and height and up to 3 digits for FPS.
28 const size_t kFileHeaderMaxSize = 38;
29 // Size of header: "YUV4MPEG2 WX HX FX:1 C420\n"
30 const size_t kFileHeaderMinSize = 26;
31 // Size of header: "FRAME\n"
32 const size_t kFrameHeaderSize = 6;
33
GetExpectedHeaderPrefix(int width,int height)34 std::string GetExpectedHeaderPrefix(int width, int height) {
35 rtc::StringBuilder out;
36 out << "YUV4MPEG2 W" << width << " H" << height << " F";
37 return out.str();
38 }
39
40 } // namespace
41
Y4mFrameReaderImpl(std::string input_filename,int width,int height)42 Y4mFrameReaderImpl::Y4mFrameReaderImpl(std::string input_filename,
43 int width,
44 int height)
45 : YuvFrameReaderImpl(input_filename, width, height) {
46 frame_length_in_bytes_ += kFrameHeaderSize;
47 buffer_ = new char[kFileHeaderMaxSize];
48 }
~Y4mFrameReaderImpl()49 Y4mFrameReaderImpl::~Y4mFrameReaderImpl() {
50 delete[] buffer_;
51 }
52
Init()53 bool Y4mFrameReaderImpl::Init() {
54 if (input_width_ <= 0 || input_height_ <= 0) {
55 RTC_LOG(LS_ERROR) << "Frame width and height must be positive. Was: "
56 << input_width_ << "x" << input_height_;
57 return false;
58 }
59 input_file_ = fopen(input_filename_.c_str(), "rb");
60 if (input_file_ == nullptr) {
61 RTC_LOG(LS_ERROR) << "Couldn't open input file: " << input_filename_;
62 return false;
63 }
64 size_t source_file_size = GetFileSize(input_filename_);
65 if (source_file_size <= 0u) {
66 RTC_LOG(LS_ERROR) << "Input file " << input_filename_ << " is empty.";
67 return false;
68 }
69 char* c_file_header = fgets(buffer_, kFileHeaderMaxSize, input_file_);
70 std::string file_header(c_file_header);
71 if (file_header.size() < kFileHeaderMinSize) {
72 RTC_LOG(LS_ERROR) << "Couldn't read Y4M header from input file: "
73 << input_filename_;
74 return false;
75 }
76 if (file_header.find(GetExpectedHeaderPrefix(input_width_, input_height_)) !=
77 0) {
78 RTC_LOG(LS_ERROR) << "Couldn't read Y4M file: " << input_filename_
79 << ". Input file has different resolution, expected: "
80 << GetExpectedHeaderPrefix(input_width_, input_height_)
81 << "[0-9]?:1 C420; got: " << file_header;
82 return false;
83 }
84
85 number_of_frames_ = static_cast<int>((source_file_size - file_header.size()) /
86 frame_length_in_bytes_);
87
88 if (number_of_frames_ == 0) {
89 RTC_LOG(LS_ERROR) << "Input file " << input_filename_ << " is too small.";
90 }
91 return true;
92 }
93
ReadFrame()94 rtc::scoped_refptr<I420Buffer> Y4mFrameReaderImpl::ReadFrame() {
95 if (input_file_ == nullptr) {
96 RTC_LOG(LS_ERROR) << "Y4mFrameReaderImpl is not initialized.";
97 return nullptr;
98 }
99 if (fread(buffer_, 1, kFrameHeaderSize, input_file_) < kFrameHeaderSize &&
100 ferror(input_file_)) {
101 RTC_LOG(LS_ERROR) << "Couldn't read frame header from input file: "
102 << input_filename_;
103 return nullptr;
104 }
105 return YuvFrameReaderImpl::ReadFrame();
106 }
107
108 } // namespace test
109 } // namespace webrtc
110