Chromium Code Reviews| OLD | NEW |
|---|---|
| 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/base/checks.h" | |
| 11 #include "webrtc/test/testsupport/frame_writer.h" | 12 #include "webrtc/test/testsupport/frame_writer.h" |
| 12 | 13 |
| 13 #include <assert.h> | |
| 14 | |
| 15 namespace webrtc { | 14 namespace webrtc { |
| 16 namespace test { | 15 namespace test { |
| 17 | 16 |
| 18 FrameWriterImpl::FrameWriterImpl(std::string output_filename, | 17 YuvFrameWriterImpl::YuvFrameWriterImpl(std::string output_filename, |
| 19 size_t frame_length_in_bytes) | 18 int width, |
| 19 int height) | |
| 20 : output_filename_(output_filename), | 20 : output_filename_(output_filename), |
| 21 frame_length_in_bytes_(frame_length_in_bytes), | 21 frame_length_in_bytes_(0), |
| 22 output_file_(NULL) { | 22 width_(width), |
| 23 } | 23 height_(height), |
| 24 output_file_(nullptr) {} | |
| 24 | 25 |
| 25 FrameWriterImpl::~FrameWriterImpl() { | 26 YuvFrameWriterImpl::~YuvFrameWriterImpl() { |
| 26 Close(); | 27 Close(); |
| 27 } | 28 } |
| 28 | 29 |
| 29 bool FrameWriterImpl::Init() { | 30 bool YuvFrameWriterImpl::Init() { |
| 30 if (frame_length_in_bytes_ <= 0) { | 31 if (width_ <= 0 || height_ <= 0) { |
| 31 fprintf(stderr, "Frame length must be >0, was %zu\n", | 32 fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", width_, |
| 32 frame_length_in_bytes_); | 33 height_); |
| 33 return false; | 34 return false; |
| 34 } | 35 } |
| 36 frame_length_in_bytes_ = | |
| 37 width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2); | |
| 38 | |
| 35 output_file_ = fopen(output_filename_.c_str(), "wb"); | 39 output_file_ = fopen(output_filename_.c_str(), "wb"); |
| 36 if (output_file_ == NULL) { | 40 if (output_file_ == nullptr) { |
| 37 fprintf(stderr, "Couldn't open output file for writing: %s\n", | 41 fprintf(stderr, "Couldn't open output file for writing: %s\n", |
| 38 output_filename_.c_str()); | 42 output_filename_.c_str()); |
| 39 return false; | 43 return false; |
| 40 } | 44 } |
| 41 return true; | 45 return true; |
| 42 } | 46 } |
| 43 | 47 |
| 44 void FrameWriterImpl::Close() { | 48 bool YuvFrameWriterImpl::WriteFrame(uint8_t* frame_buffer) { |
| 45 if (output_file_ != NULL) { | 49 RTC_DCHECK(frame_buffer); |
| 46 fclose(output_file_); | 50 if (output_file_ == nullptr) { |
| 47 output_file_ = NULL; | |
| 48 } | |
| 49 } | |
| 50 | |
| 51 size_t FrameWriterImpl::FrameLength() { return frame_length_in_bytes_; } | |
| 52 | |
| 53 bool FrameWriterImpl::WriteFrame(uint8_t* frame_buffer) { | |
| 54 assert(frame_buffer); | |
| 55 if (output_file_ == NULL) { | |
| 56 fprintf(stderr, "FrameWriter is not initialized (output file is NULL)\n"); | 51 fprintf(stderr, "FrameWriter is not initialized (output file is NULL)\n"); |
| 57 return false; | 52 return false; |
| 58 } | 53 } |
| 59 size_t bytes_written = fwrite(frame_buffer, 1, frame_length_in_bytes_, | 54 size_t bytes_written = fwrite(frame_buffer, 1, frame_length_in_bytes_, |
| 60 output_file_); | 55 output_file_); |
| 61 if (bytes_written != frame_length_in_bytes_) { | 56 if (bytes_written != frame_length_in_bytes_) { |
| 62 fprintf(stderr, "Failed to write %zu bytes to file %s\n", | 57 fprintf(stderr, "Failed to write %zu bytes to file %s\n", |
| 63 frame_length_in_bytes_, output_filename_.c_str()); | 58 frame_length_in_bytes_, output_filename_.c_str()); |
| 64 return false; | 59 return false; |
| 65 } | 60 } |
| 66 return true; | 61 return true; |
| 67 } | 62 } |
| 68 | 63 |
| 64 void YuvFrameWriterImpl::Close() { | |
| 65 if (output_file_ != nullptr) { | |
| 66 fclose(output_file_); | |
| 67 output_file_ = nullptr; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 size_t YuvFrameWriterImpl::FrameLength() { | |
| 72 return frame_length_in_bytes_; | |
| 73 } | |
| 74 | |
| 75 Y4mFrameWriterImpl::Y4mFrameWriterImpl(std::string output_filename, | |
| 76 int width, | |
| 77 int height, | |
| 78 int frame_rate) | |
| 79 : YuvFrameWriterImpl(output_filename, width, height), | |
| 80 frame_rate_(frame_rate) {} | |
| 81 | |
| 82 Y4mFrameWriterImpl::~Y4mFrameWriterImpl() = default; | |
| 83 | |
| 84 bool Y4mFrameWriterImpl::Init() { | |
| 85 if (!YuvFrameWriterImpl::Init()) { | |
| 86 return false; | |
| 87 } | |
| 88 int bytes_written = fprintf(output_file_, "YUV4MPEG2 W%d H%d F%d:1 C420\n", | |
| 89 width_, height_, frame_rate_); | |
| 90 if (bytes_written < 0) { | |
| 91 fprintf(stderr, "Failed to write Y4M file header to file %s\n", | |
| 92 output_filename_.c_str()); | |
| 93 return false; | |
| 94 } | |
| 95 return true; | |
| 96 } | |
| 97 | |
| 98 bool Y4mFrameWriterImpl::WriteFrame(uint8_t* frame_buffer) { | |
| 99 int bytes_written = fprintf(output_file_, "FRAME\n"); | |
| 100 if (bytes_written < 0) { | |
| 101 fprintf(stderr, "Failed to write Y4M frame header to file %s\n", | |
| 102 output_filename_.c_str()); | |
|
åsapersson
2017/02/17 15:47:35
return false?
| |
| 103 } | |
| 104 return YuvFrameWriterImpl::WriteFrame(frame_buffer); | |
| 105 } | |
| 106 | |
| 69 } // namespace test | 107 } // namespace test |
| 70 } // namespace webrtc | 108 } // namespace webrtc |
| OLD | NEW |