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