brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 | |
Edward Lemur | c20978e | 2017-07-06 19:44:34 +0200 | [diff] [blame] | 11 | #include "webrtc/rtc_base/checks.h" |
brandtr | b78bc75 | 2017-02-22 01:26:59 -0800 | [diff] [blame] | 12 | #include "webrtc/test/testsupport/frame_writer.h" |
| 13 | |
| 14 | namespace webrtc { |
| 15 | namespace test { |
| 16 | |
| 17 | YuvFrameWriterImpl::YuvFrameWriterImpl(std::string output_filename, |
| 18 | int width, |
| 19 | int height) |
| 20 | : output_filename_(output_filename), |
| 21 | frame_length_in_bytes_(0), |
| 22 | width_(width), |
| 23 | height_(height), |
| 24 | output_file_(nullptr) {} |
| 25 | |
| 26 | YuvFrameWriterImpl::~YuvFrameWriterImpl() { |
| 27 | Close(); |
| 28 | } |
| 29 | |
| 30 | bool YuvFrameWriterImpl::Init() { |
| 31 | if (width_ <= 0 || height_ <= 0) { |
| 32 | fprintf(stderr, "Frame width and height must be >0, was %d x %d\n", width_, |
| 33 | height_); |
| 34 | return false; |
| 35 | } |
| 36 | frame_length_in_bytes_ = |
| 37 | width_ * height_ + 2 * ((width_ + 1) / 2) * ((height_ + 1) / 2); |
| 38 | |
| 39 | output_file_ = fopen(output_filename_.c_str(), "wb"); |
| 40 | if (output_file_ == nullptr) { |
| 41 | fprintf(stderr, "Couldn't open output file for writing: %s\n", |
| 42 | output_filename_.c_str()); |
| 43 | return false; |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | bool YuvFrameWriterImpl::WriteFrame(uint8_t* frame_buffer) { |
| 49 | RTC_DCHECK(frame_buffer); |
| 50 | if (output_file_ == nullptr) { |
| 51 | fprintf(stderr, |
| 52 | "YuvFrameWriterImpl is not initialized (output file is NULL)\n"); |
| 53 | return false; |
| 54 | } |
| 55 | size_t bytes_written = |
| 56 | fwrite(frame_buffer, 1, frame_length_in_bytes_, output_file_); |
| 57 | if (bytes_written != frame_length_in_bytes_) { |
| 58 | fprintf(stderr, "Failed to write %zu bytes to file %s\n", |
| 59 | frame_length_in_bytes_, output_filename_.c_str()); |
| 60 | return false; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | void YuvFrameWriterImpl::Close() { |
| 66 | if (output_file_ != nullptr) { |
| 67 | fclose(output_file_); |
| 68 | output_file_ = nullptr; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | size_t YuvFrameWriterImpl::FrameLength() { |
| 73 | return frame_length_in_bytes_; |
| 74 | } |
| 75 | |
| 76 | } // namespace test |
| 77 | } // namespace webrtc |