blob: 3fe481ee1ebd95b6a1e2dd13909972566515ab16 [file] [log] [blame]
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00001/*
brandtrb78bc752017-02-22 01:26:59 -08002 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00003 *
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
pbos@webrtc.org34741c82013-05-27 08:02:22 +000011#include "webrtc/test/testsupport/frame_reader.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
nisseaf916892017-01-10 07:44:26 -080013#include "webrtc/api/video/i420_buffer.h"
nisse115bd152016-09-30 04:14:07 -070014#include "webrtc/test/frame_utils.h"
kjellander@webrtc.org9c4e6622013-02-13 09:35:12 +000015#include "webrtc/test/testsupport/fileutils.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000016
17namespace webrtc {
18namespace test {
19
brandtrb78bc752017-02-22 01:26:59 -080020YuvFrameReaderImpl::YuvFrameReaderImpl(std::string input_filename,
21 int width,
22 int height)
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000023 : input_filename_(input_filename),
brandtrb78bc752017-02-22 01:26:59 -080024 frame_length_in_bytes_(0),
25 width_(width),
26 height_(height),
27 number_of_frames_(-1),
28 input_file_(nullptr) {}
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000029
brandtrb78bc752017-02-22 01:26:59 -080030YuvFrameReaderImpl::~YuvFrameReaderImpl() {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000031 Close();
32}
33
brandtrb78bc752017-02-22 01:26:59 -080034bool YuvFrameReaderImpl::Init() {
nisse115bd152016-09-30 04:14:07 -070035 if (width_ <= 0 || height_ <= 0) {
brandtrb78bc752017-02-22 01:26:59 -080036 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", width_,
37 height_);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000038 return false;
39 }
nisse115bd152016-09-30 04:14:07 -070040 frame_length_in_bytes_ =
41 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2);
42
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000043 input_file_ = fopen(input_filename_.c_str(), "rb");
brandtrb78bc752017-02-22 01:26:59 -080044 if (input_file_ == nullptr) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000045 fprintf(stderr, "Couldn't open input file for reading: %s\n",
46 input_filename_.c_str());
47 return false;
48 }
49 // Calculate total number of frames.
50 size_t source_file_size = GetFileSize(input_filename_);
51 if (source_file_size <= 0u) {
52 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str());
53 return false;
54 }
brandtrb78bc752017-02-22 01:26:59 -080055 number_of_frames_ =
56 static_cast<int>(source_file_size / frame_length_in_bytes_);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000057 return true;
58}
59
brandtrb78bc752017-02-22 01:26:59 -080060rtc::scoped_refptr<I420Buffer> YuvFrameReaderImpl::ReadFrame() {
61 if (input_file_ == nullptr) {
62 fprintf(stderr,
63 "YuvFrameReaderImpl is not initialized (input file is NULL)\n");
nisse115bd152016-09-30 04:14:07 -070064 return nullptr;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000065 }
nisse115bd152016-09-30 04:14:07 -070066 rtc::scoped_refptr<I420Buffer> buffer(
67 ReadI420Buffer(width_, height_, input_file_));
68 if (!buffer && ferror(input_file_)) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000069 fprintf(stderr, "Error reading from input file: %s\n",
70 input_filename_.c_str());
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000071 }
nisse115bd152016-09-30 04:14:07 -070072 return buffer;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000073}
74
brandtrb78bc752017-02-22 01:26:59 -080075void YuvFrameReaderImpl::Close() {
76 if (input_file_ != nullptr) {
77 fclose(input_file_);
78 input_file_ = nullptr;
79 }
80}
81
82size_t YuvFrameReaderImpl::FrameLength() {
83 return frame_length_in_bytes_;
84}
85
86int YuvFrameReaderImpl::NumberOfFrames() {
87 return number_of_frames_;
88}
pbos@webrtc.orge6c39662013-07-30 13:08:38 +000089
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000090} // namespace test
91} // namespace webrtc