OLD | NEW |
(Empty) | |
| 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 |
| 11 #include "webrtc/test/testsupport/frame_reader.h" |
| 12 |
| 13 #include <assert.h> |
| 14 |
| 15 #include "webrtc/api/video/i420_buffer.h" |
| 16 #include "webrtc/test/frame_utils.h" |
| 17 #include "webrtc/test/testsupport/fileutils.h" |
| 18 |
| 19 namespace webrtc { |
| 20 namespace test { |
| 21 |
| 22 FrameReaderImpl::FrameReaderImpl(std::string input_filename, |
| 23 int width, int height) |
| 24 : input_filename_(input_filename), |
| 25 width_(width), height_(height), |
| 26 input_file_(NULL) { |
| 27 } |
| 28 |
| 29 FrameReaderImpl::~FrameReaderImpl() { |
| 30 Close(); |
| 31 } |
| 32 |
| 33 bool FrameReaderImpl::Init() { |
| 34 if (width_ <= 0 || height_ <= 0) { |
| 35 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", |
| 36 width_, height_); |
| 37 return false; |
| 38 } |
| 39 frame_length_in_bytes_ = |
| 40 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2); |
| 41 |
| 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 } |
| 54 number_of_frames_ = static_cast<int>(source_file_size / |
| 55 frame_length_in_bytes_); |
| 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 |
| 66 rtc::scoped_refptr<I420Buffer> FrameReaderImpl::ReadFrame() { |
| 67 if (input_file_ == NULL) { |
| 68 fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n"); |
| 69 return nullptr; |
| 70 } |
| 71 rtc::scoped_refptr<I420Buffer> buffer( |
| 72 ReadI420Buffer(width_, height_, input_file_)); |
| 73 if (!buffer && ferror(input_file_)) { |
| 74 fprintf(stderr, "Error reading from input file: %s\n", |
| 75 input_filename_.c_str()); |
| 76 } |
| 77 return buffer; |
| 78 } |
| 79 |
| 80 size_t FrameReaderImpl::FrameLength() { return frame_length_in_bytes_; } |
| 81 int FrameReaderImpl::NumberOfFrames() { return number_of_frames_; } |
| 82 |
| 83 } // namespace test |
| 84 } // namespace webrtc |
OLD | NEW |