blob: dc0d822e9be0e893e9499fce85cf5e371872a142 [file] [log] [blame]
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +00001/*
2 * Copyright (c) 2011 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
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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000013#include <assert.h>
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000014
nisse115bd152016-09-30 04:14:07 -070015#include "webrtc/test/frame_utils.h"
kjellander@webrtc.org9c4e6622013-02-13 09:35:12 +000016#include "webrtc/test/testsupport/fileutils.h"
nisse115bd152016-09-30 04:14:07 -070017#include "webrtc/common_video/include/video_frame_buffer.h"
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000018
19namespace webrtc {
20namespace test {
21
22FrameReaderImpl::FrameReaderImpl(std::string input_filename,
nisse115bd152016-09-30 04:14:07 -070023 int width, int height)
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000024 : input_filename_(input_filename),
nisse115bd152016-09-30 04:14:07 -070025 width_(width), height_(height),
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000026 input_file_(NULL) {
27}
28
29FrameReaderImpl::~FrameReaderImpl() {
30 Close();
31}
32
33bool FrameReaderImpl::Init() {
nisse115bd152016-09-30 04:14:07 -070034 if (width_ <= 0 || height_ <= 0) {
35 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n",
36 width_, height_);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000037 return false;
38 }
nisse115bd152016-09-30 04:14:07 -070039 frame_length_in_bytes_ =
40 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2);
41
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000042 input_file_ = fopen(input_filename_.c_str(), "rb");
43 if (input_file_ == NULL) {
44 fprintf(stderr, "Couldn't open input file for reading: %s\n",
45 input_filename_.c_str());
46 return false;
47 }
48 // Calculate total number of frames.
49 size_t source_file_size = GetFileSize(input_filename_);
50 if (source_file_size <= 0u) {
51 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str());
52 return false;
53 }
kjellander@webrtc.orgfa53d872013-02-04 10:07:17 +000054 number_of_frames_ = static_cast<int>(source_file_size /
55 frame_length_in_bytes_);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000056 return true;
57}
58
59void FrameReaderImpl::Close() {
60 if (input_file_ != NULL) {
61 fclose(input_file_);
62 input_file_ = NULL;
63 }
64}
65
nisse115bd152016-09-30 04:14:07 -070066rtc::scoped_refptr<I420Buffer> FrameReaderImpl::ReadFrame() {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000067 if (input_file_ == NULL) {
kjellander@webrtc.org173b7bb2011-12-21 16:11:25 +000068 fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n");
nisse115bd152016-09-30 04:14:07 -070069 return nullptr;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000070 }
nisse115bd152016-09-30 04:14:07 -070071 rtc::scoped_refptr<I420Buffer> buffer(
72 ReadI420Buffer(width_, height_, input_file_));
73 if (!buffer && ferror(input_file_)) {
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000074 fprintf(stderr, "Error reading from input file: %s\n",
75 input_filename_.c_str());
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000076 }
nisse115bd152016-09-30 04:14:07 -070077 return buffer;
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000078}
79
pbos@webrtc.orge6c39662013-07-30 13:08:38 +000080size_t FrameReaderImpl::FrameLength() { return frame_length_in_bytes_; }
81int FrameReaderImpl::NumberOfFrames() { return number_of_frames_; }
82
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000083} // namespace test
84} // namespace webrtc