blob: 72f3a6919e4157ce9af8cba621b3d3c2a74b32b3 [file] [log] [blame]
Paulina Hensmanb671d462018-09-14 11:32:00 +02001/*
2 * Copyright (c) 2018 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 "rtc_tools/video_file_writer.h"
12
13#include <cmath>
14#include <string>
15
16#include "api/video/i420_buffer.h"
17#include "rtc_base/logging.h"
18#include "rtc_base/refcountedobject.h"
19
20namespace webrtc {
21namespace test {
22
23void WriteVideoToFile(const rtc::scoped_refptr<Video>& video,
24 const std::string& file_name,
25 int fps) {
26 FILE* output_file = fopen(file_name.c_str(), "wb");
27 if (output_file == nullptr) {
28 RTC_LOG(LS_ERROR) << "Could not open file for writing: " << file_name;
29 return;
30 }
31
32 bool isY4m = rtc::ends_with(file_name.c_str(), ".y4m");
33 if (isY4m) {
34 fprintf(output_file, "YUV4MPEG2 W%d H%d F%d:1 C420\n", video->width(),
35 video->height(), fps);
36 }
37 for (size_t i = 0; i < video->number_of_frames(); ++i) {
38 if (isY4m) {
39 std::string frame = "FRAME\n";
40 fwrite(frame.c_str(), 1, 6, output_file);
41 }
42 rtc::scoped_refptr<I420BufferInterface> buffer = video->GetFrame(i);
43 const uint8_t* data_y = buffer->DataY();
44 int stride = buffer->StrideY();
45 for (int i = 0; i < video->height(); ++i) {
46 fwrite(data_y + i * stride, /*size=*/1, stride, output_file);
47 }
48 const uint8_t* data_u = buffer->DataU();
49 stride = buffer->StrideU();
50 for (int i = 0; i < buffer->ChromaHeight(); ++i) {
51 fwrite(data_u + i * stride, /*size=*/1, stride, output_file);
52 }
53 const uint8_t* data_v = buffer->DataV();
54 stride = buffer->StrideV();
55 for (int i = 0; i < buffer->ChromaHeight(); ++i) {
56 fwrite(data_v + i * stride, /*size=*/1, stride, output_file);
57 }
58 }
59 if (ferror(output_file) != 0) {
60 RTC_LOG(LS_ERROR) << "Error writing to file " << file_name;
61 }
62 fclose(output_file);
63}
64
65} // namespace test
66} // namespace webrtc