kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 34741c8 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 11 | #include "webrtc/test/testsupport/frame_reader.h" |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 12dc1a3 | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 14 | |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 15 | #include "webrtc/test/frame_utils.h" |
kjellander@webrtc.org | 9c4e662 | 2013-02-13 09:35:12 +0000 | [diff] [blame] | 16 | #include "webrtc/test/testsupport/fileutils.h" |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 17 | #include "webrtc/common_video/include/video_frame_buffer.h" |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 18 | |
| 19 | namespace webrtc { |
| 20 | namespace test { |
| 21 | |
| 22 | FrameReaderImpl::FrameReaderImpl(std::string input_filename, |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 23 | int width, int height) |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 24 | : input_filename_(input_filename), |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 25 | width_(width), height_(height), |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 26 | input_file_(NULL) { |
| 27 | } |
| 28 | |
| 29 | FrameReaderImpl::~FrameReaderImpl() { |
| 30 | Close(); |
| 31 | } |
| 32 | |
| 33 | bool FrameReaderImpl::Init() { |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 34 | 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.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 37 | return false; |
| 38 | } |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 39 | frame_length_in_bytes_ = |
| 40 | width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2); |
| 41 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 42 | 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.org | fa53d87 | 2013-02-04 10:07:17 +0000 | [diff] [blame] | 54 | number_of_frames_ = static_cast<int>(source_file_size / |
| 55 | frame_length_in_bytes_); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | |
| 59 | void FrameReaderImpl::Close() { |
| 60 | if (input_file_ != NULL) { |
| 61 | fclose(input_file_); |
| 62 | input_file_ = NULL; |
| 63 | } |
| 64 | } |
| 65 | |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 66 | rtc::scoped_refptr<I420Buffer> FrameReaderImpl::ReadFrame() { |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 67 | if (input_file_ == NULL) { |
kjellander@webrtc.org | 173b7bb | 2011-12-21 16:11:25 +0000 | [diff] [blame] | 68 | fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n"); |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 69 | return nullptr; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 70 | } |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 71 | rtc::scoped_refptr<I420Buffer> buffer( |
| 72 | ReadI420Buffer(width_, height_, input_file_)); |
| 73 | if (!buffer && ferror(input_file_)) { |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 74 | fprintf(stderr, "Error reading from input file: %s\n", |
| 75 | input_filename_.c_str()); |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 76 | } |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 77 | return buffer; |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 78 | } |
| 79 | |
pbos@webrtc.org | e6c3966 | 2013-07-30 13:08:38 +0000 | [diff] [blame] | 80 | size_t FrameReaderImpl::FrameLength() { return frame_length_in_bytes_; } |
| 81 | int FrameReaderImpl::NumberOfFrames() { return number_of_frames_; } |
| 82 | |
kjellander@webrtc.org | 5b97b12 | 2011-12-08 07:42:18 +0000 | [diff] [blame] | 83 | } // namespace test |
| 84 | } // namespace webrtc |