1 /*
2 * Copyright (c) 2017 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 "test/frame_utils.h"
18 #include "test/testsupport/file_utils.h"
19 #include "test/testsupport/frame_reader.h"
20
21 namespace webrtc {
22 namespace test {
23
YuvFrameReaderImpl(std::string input_filename,int width,int height)24 YuvFrameReaderImpl::YuvFrameReaderImpl(std::string input_filename,
25 int width,
26 int height)
27 : input_filename_(input_filename),
28 frame_length_in_bytes_(width * height +
29 2 * ((width + 1) / 2) * ((height + 1) / 2)),
30 width_(width),
31 height_(height),
32 number_of_frames_(-1),
33 input_file_(nullptr) {}
34
~YuvFrameReaderImpl()35 YuvFrameReaderImpl::~YuvFrameReaderImpl() {
36 Close();
37 }
38
Init()39 bool YuvFrameReaderImpl::Init() {
40 if (width_ <= 0 || height_ <= 0) {
41 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", width_,
42 height_);
43 return false;
44 }
45 input_file_ = fopen(input_filename_.c_str(), "rb");
46 if (input_file_ == nullptr) {
47 fprintf(stderr, "Couldn't open input file for reading: %s\n",
48 input_filename_.c_str());
49 return false;
50 }
51 // Calculate total number of frames.
52 size_t source_file_size = GetFileSize(input_filename_);
53 if (source_file_size <= 0u) {
54 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str());
55 return false;
56 }
57 number_of_frames_ =
58 static_cast<int>(source_file_size / frame_length_in_bytes_);
59 return true;
60 }
61
ReadFrame()62 rtc::scoped_refptr<I420Buffer> YuvFrameReaderImpl::ReadFrame() {
63 if (input_file_ == nullptr) {
64 fprintf(stderr,
65 "YuvFrameReaderImpl is not initialized (input file is NULL)\n");
66 return nullptr;
67 }
68 rtc::scoped_refptr<I420Buffer> buffer(
69 ReadI420Buffer(width_, height_, input_file_));
70 if (!buffer && ferror(input_file_)) {
71 fprintf(stderr, "Error reading from input file: %s\n",
72 input_filename_.c_str());
73 }
74 return buffer;
75 }
76
Close()77 void YuvFrameReaderImpl::Close() {
78 if (input_file_ != nullptr) {
79 fclose(input_file_);
80 input_file_ = nullptr;
81 }
82 }
83
FrameLength()84 size_t YuvFrameReaderImpl::FrameLength() {
85 return frame_length_in_bytes_;
86 }
87
NumberOfFrames()88 int YuvFrameReaderImpl::NumberOfFrames() {
89 return number_of_frames_;
90 }
91
92 } // namespace test
93 } // namespace webrtc
94