andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +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 | 20a5c46 | 2013-05-27 08:02:22 +0000 | [diff] [blame] | 11 | #include "webrtc/test/testsupport/frame_reader.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 12 | |
pbos@webrtc.org | 3f45c2e | 2013-08-05 16:22:53 +0000 | [diff] [blame] | 13 | #include <assert.h> |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 14 | |
kjellander@webrtc.org | 6d4d28e | 2013-02-13 09:35:12 +0000 | [diff] [blame] | 15 | #include "webrtc/test/testsupport/fileutils.h" |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | namespace test { |
| 19 | |
| 20 | FrameReaderImpl::FrameReaderImpl(std::string input_filename, |
kjellander@webrtc.org | 6d4d28e | 2013-02-13 09:35:12 +0000 | [diff] [blame] | 21 | size_t frame_length_in_bytes) |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 22 | : input_filename_(input_filename), |
| 23 | frame_length_in_bytes_(frame_length_in_bytes), |
| 24 | input_file_(NULL) { |
| 25 | } |
| 26 | |
| 27 | FrameReaderImpl::~FrameReaderImpl() { |
| 28 | Close(); |
| 29 | } |
| 30 | |
| 31 | bool FrameReaderImpl::Init() { |
| 32 | if (frame_length_in_bytes_ <= 0) { |
kjellander@webrtc.org | 1e7f77a | 2013-02-04 10:07:17 +0000 | [diff] [blame] | 33 | fprintf(stderr, "Frame length must be >0, was %zu\n", |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 34 | frame_length_in_bytes_); |
| 35 | return false; |
| 36 | } |
| 37 | input_file_ = fopen(input_filename_.c_str(), "rb"); |
| 38 | if (input_file_ == NULL) { |
| 39 | fprintf(stderr, "Couldn't open input file for reading: %s\n", |
| 40 | input_filename_.c_str()); |
| 41 | return false; |
| 42 | } |
| 43 | // Calculate total number of frames. |
| 44 | size_t source_file_size = GetFileSize(input_filename_); |
| 45 | if (source_file_size <= 0u) { |
| 46 | fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str()); |
| 47 | return false; |
| 48 | } |
kjellander@webrtc.org | 1e7f77a | 2013-02-04 10:07:17 +0000 | [diff] [blame] | 49 | number_of_frames_ = static_cast<int>(source_file_size / |
| 50 | frame_length_in_bytes_); |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | |
| 54 | void FrameReaderImpl::Close() { |
| 55 | if (input_file_ != NULL) { |
| 56 | fclose(input_file_); |
| 57 | input_file_ = NULL; |
| 58 | } |
| 59 | } |
| 60 | |
pbos@webrtc.org | 91cab71 | 2013-04-09 11:10:21 +0000 | [diff] [blame] | 61 | bool FrameReaderImpl::ReadFrame(uint8_t* source_buffer) { |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 62 | assert(source_buffer); |
| 63 | if (input_file_ == NULL) { |
| 64 | fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n"); |
| 65 | return false; |
| 66 | } |
| 67 | size_t nbr_read = fread(source_buffer, 1, frame_length_in_bytes_, |
| 68 | input_file_); |
| 69 | if (nbr_read != static_cast<unsigned int>(frame_length_in_bytes_) && |
| 70 | ferror(input_file_)) { |
| 71 | fprintf(stderr, "Error reading from input file: %s\n", |
| 72 | input_filename_.c_str()); |
| 73 | return false; |
| 74 | } |
| 75 | if (feof(input_file_) != 0) { |
| 76 | return false; // No more frames to process. |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 | |
pbos@webrtc.org | 77c6d71 | 2013-07-30 13:08:38 +0000 | [diff] [blame] | 81 | size_t FrameReaderImpl::FrameLength() { return frame_length_in_bytes_; } |
| 82 | int FrameReaderImpl::NumberOfFrames() { return number_of_frames_; } |
| 83 | |
andrew@webrtc.org | b015cbe | 2012-10-22 18:19:23 +0000 | [diff] [blame] | 84 | } // namespace test |
| 85 | } // namespace webrtc |