Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: webrtc/test/testsupport/frame_reader.cc

Issue 2362683002: New helper function test::ReadI420Buffer, refactor FrameReader to use it. (Closed)
Patch Set: Another try to fix the 64-bit windows build. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/test/testsupport/frame_reader.h" 11 #include "webrtc/test/testsupport/frame_reader.h"
12 12
13 #include <assert.h> 13 #include <assert.h>
14 14
15 #include "webrtc/test/frame_utils.h"
15 #include "webrtc/test/testsupport/fileutils.h" 16 #include "webrtc/test/testsupport/fileutils.h"
17 #include "webrtc/common_video/include/video_frame_buffer.h"
16 18
17 namespace webrtc { 19 namespace webrtc {
18 namespace test { 20 namespace test {
19 21
20 FrameReaderImpl::FrameReaderImpl(std::string input_filename, 22 FrameReaderImpl::FrameReaderImpl(std::string input_filename,
21 size_t frame_length_in_bytes) 23 int width, int height)
22 : input_filename_(input_filename), 24 : input_filename_(input_filename),
23 frame_length_in_bytes_(frame_length_in_bytes), 25 width_(width), height_(height),
24 input_file_(NULL) { 26 input_file_(NULL) {
25 } 27 }
26 28
27 FrameReaderImpl::~FrameReaderImpl() { 29 FrameReaderImpl::~FrameReaderImpl() {
28 Close(); 30 Close();
29 } 31 }
30 32
31 bool FrameReaderImpl::Init() { 33 bool FrameReaderImpl::Init() {
32 if (frame_length_in_bytes_ <= 0) { 34 if (width_ <= 0 || height_ <= 0) {
33 fprintf(stderr, "Frame length must be >0, was %zu\n", 35 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n",
34 frame_length_in_bytes_); 36 width_, height_);
35 return false; 37 return false;
36 } 38 }
39 frame_length_in_bytes_ =
40 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2);
41
37 input_file_ = fopen(input_filename_.c_str(), "rb"); 42 input_file_ = fopen(input_filename_.c_str(), "rb");
38 if (input_file_ == NULL) { 43 if (input_file_ == NULL) {
39 fprintf(stderr, "Couldn't open input file for reading: %s\n", 44 fprintf(stderr, "Couldn't open input file for reading: %s\n",
40 input_filename_.c_str()); 45 input_filename_.c_str());
41 return false; 46 return false;
42 } 47 }
43 // Calculate total number of frames. 48 // Calculate total number of frames.
44 size_t source_file_size = GetFileSize(input_filename_); 49 size_t source_file_size = GetFileSize(input_filename_);
45 if (source_file_size <= 0u) { 50 if (source_file_size <= 0u) {
46 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str()); 51 fprintf(stderr, "Found empty file: %s\n", input_filename_.c_str());
47 return false; 52 return false;
48 } 53 }
49 number_of_frames_ = static_cast<int>(source_file_size / 54 number_of_frames_ = static_cast<int>(source_file_size /
50 frame_length_in_bytes_); 55 frame_length_in_bytes_);
51 return true; 56 return true;
52 } 57 }
53 58
54 void FrameReaderImpl::Close() { 59 void FrameReaderImpl::Close() {
55 if (input_file_ != NULL) { 60 if (input_file_ != NULL) {
56 fclose(input_file_); 61 fclose(input_file_);
57 input_file_ = NULL; 62 input_file_ = NULL;
58 } 63 }
59 } 64 }
60 65
61 bool FrameReaderImpl::ReadFrame(uint8_t* source_buffer) { 66 rtc::scoped_refptr<I420Buffer> FrameReaderImpl::ReadFrame() {
62 assert(source_buffer);
63 if (input_file_ == NULL) { 67 if (input_file_ == NULL) {
64 fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n"); 68 fprintf(stderr, "FrameReader is not initialized (input file is NULL)\n");
65 return false; 69 return nullptr;
66 } 70 }
67 size_t nbr_read = fread(source_buffer, 1, frame_length_in_bytes_, 71 rtc::scoped_refptr<I420Buffer> buffer(
68 input_file_); 72 ReadI420Buffer(width_, height_, input_file_));
69 if (nbr_read != static_cast<unsigned int>(frame_length_in_bytes_) && 73 if (!buffer && ferror(input_file_)) {
70 ferror(input_file_)) {
71 fprintf(stderr, "Error reading from input file: %s\n", 74 fprintf(stderr, "Error reading from input file: %s\n",
72 input_filename_.c_str()); 75 input_filename_.c_str());
73 return false;
74 } 76 }
75 if (feof(input_file_) != 0) { 77 return buffer;
76 return false; // No more frames to process.
77 }
78 return true;
79 } 78 }
80 79
81 size_t FrameReaderImpl::FrameLength() { return frame_length_in_bytes_; } 80 size_t FrameReaderImpl::FrameLength() { return frame_length_in_bytes_; }
82 int FrameReaderImpl::NumberOfFrames() { return number_of_frames_; } 81 int FrameReaderImpl::NumberOfFrames() { return number_of_frames_; }
83 82
84 } // namespace test 83 } // namespace test
85 } // namespace webrtc 84 } // namespace webrtc
OLDNEW
« no previous file with comments | « webrtc/test/testsupport/frame_reader.h ('k') | webrtc/test/testsupport/frame_reader_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698