blob: e7a649fe8bf8a0f77d902b7209a26d654e7cb98e [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
Yves Gerey3e707812018-11-28 16:47:49 +010013#include <stdint.h>
14#include <cstdio>
Paulina Hensmanb671d462018-09-14 11:32:00 +020015#include <string>
16
Steve Anton68586e82018-12-13 17:41:25 -080017#include "absl/strings/match.h"
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "api/video/video_frame_buffer.h"
Paulina Hensmanb671d462018-09-14 11:32:00 +020019#include "rtc_base/logging.h"
Paulina Hensmanb671d462018-09-14 11:32:00 +020020
21namespace webrtc {
22namespace test {
23
24void WriteVideoToFile(const rtc::scoped_refptr<Video>& video,
25 const std::string& file_name,
26 int fps) {
27 FILE* output_file = fopen(file_name.c_str(), "wb");
28 if (output_file == nullptr) {
29 RTC_LOG(LS_ERROR) << "Could not open file for writing: " << file_name;
30 return;
31 }
32
Steve Anton68586e82018-12-13 17:41:25 -080033 bool isY4m = absl::EndsWith(file_name, ".y4m");
Paulina Hensmanb671d462018-09-14 11:32:00 +020034 if (isY4m) {
35 fprintf(output_file, "YUV4MPEG2 W%d H%d F%d:1 C420\n", video->width(),
36 video->height(), fps);
37 }
38 for (size_t i = 0; i < video->number_of_frames(); ++i) {
39 if (isY4m) {
40 std::string frame = "FRAME\n";
41 fwrite(frame.c_str(), 1, 6, output_file);
42 }
43 rtc::scoped_refptr<I420BufferInterface> buffer = video->GetFrame(i);
44 const uint8_t* data_y = buffer->DataY();
45 int stride = buffer->StrideY();
46 for (int i = 0; i < video->height(); ++i) {
47 fwrite(data_y + i * stride, /*size=*/1, stride, output_file);
48 }
49 const uint8_t* data_u = buffer->DataU();
50 stride = buffer->StrideU();
51 for (int i = 0; i < buffer->ChromaHeight(); ++i) {
52 fwrite(data_u + i * stride, /*size=*/1, stride, output_file);
53 }
54 const uint8_t* data_v = buffer->DataV();
55 stride = buffer->StrideV();
56 for (int i = 0; i < buffer->ChromaHeight(); ++i) {
57 fwrite(data_v + i * stride, /*size=*/1, stride, output_file);
58 }
59 }
60 if (ferror(output_file) != 0) {
61 RTC_LOG(LS_ERROR) << "Error writing to file " << file_name;
62 }
63 fclose(output_file);
64}
65
66} // namespace test
67} // namespace webrtc