Thanks to visit codestin.com
Credit goes to webrtc.googlesource.com

blob: 2a1bc67cc6d5118b03a0505913a93f2465bd4c31 [file] [log] [blame]
Paulina Hensmanb671d462018-09-14 09:32:001/*
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
Philipp Hancke83b748d2025-07-02 17:22:2113#include <cstdint>
Yves Gerey3e707812018-11-28 15:47:4914#include <cstdio>
Paulina Hensmanb671d462018-09-14 09:32:0015#include <string>
16
Steve Anton68586e82018-12-14 01:41:2517#include "absl/strings/match.h"
Philipp Hancke83b748d2025-07-02 17:22:2118#include "api/scoped_refptr.h"
Yves Gerey3e707812018-11-28 15:47:4919#include "api/video/video_frame_buffer.h"
Philipp Hancke83b748d2025-07-02 17:22:2120#include "rtc_base/checks.h"
Paulina Hensmanb671d462018-09-14 09:32:0021#include "rtc_base/logging.h"
Philipp Hancke83b748d2025-07-02 17:22:2122#include "rtc_tools/video_file_reader.h"
Paulina Hensmanb671d462018-09-14 09:32:0023
24namespace webrtc {
25namespace test {
Yves Gerey3cb5e5b2019-03-21 10:22:4226namespace {
Paulina Hensmanb671d462018-09-14 09:32:0027
Evan Shrubsolea4fd9d92025-04-15 14:51:5728void WriteVideoToFile(const scoped_refptr<Video>& video,
Paulina Hensmanb671d462018-09-14 09:32:0029 const std::string& file_name,
Yves Gerey3cb5e5b2019-03-21 10:22:4230 int fps,
31 bool isY4m) {
32 RTC_CHECK(video);
Paulina Hensmanb671d462018-09-14 09:32:0033 FILE* output_file = fopen(file_name.c_str(), "wb");
34 if (output_file == nullptr) {
35 RTC_LOG(LS_ERROR) << "Could not open file for writing: " << file_name;
36 return;
37 }
38
Paulina Hensmanb671d462018-09-14 09:32:0039 if (isY4m) {
40 fprintf(output_file, "YUV4MPEG2 W%d H%d F%d:1 C420\n", video->width(),
41 video->height(), fps);
42 }
43 for (size_t i = 0; i < video->number_of_frames(); ++i) {
44 if (isY4m) {
45 std::string frame = "FRAME\n";
46 fwrite(frame.c_str(), 1, 6, output_file);
47 }
Evan Shrubsolea4fd9d92025-04-15 14:51:5748 scoped_refptr<I420BufferInterface> buffer = video->GetFrame(i);
Yves Gerey3cb5e5b2019-03-21 10:22:4249 RTC_CHECK(buffer) << "Frame: " << i
50 << "\nWhile trying to create: " << file_name;
Paulina Hensmanb671d462018-09-14 09:32:0051 const uint8_t* data_y = buffer->DataY();
52 int stride = buffer->StrideY();
Philipp Hancke05eb7742025-03-19 14:54:1453 for (int j = 0; j < video->height(); ++j) {
54 fwrite(data_y + j * stride, /*size=*/1, stride, output_file);
Paulina Hensmanb671d462018-09-14 09:32:0055 }
56 const uint8_t* data_u = buffer->DataU();
57 stride = buffer->StrideU();
Philipp Hancke05eb7742025-03-19 14:54:1458 for (int j = 0; j < buffer->ChromaHeight(); ++j) {
59 fwrite(data_u + j * stride, /*size=*/1, stride, output_file);
Paulina Hensmanb671d462018-09-14 09:32:0060 }
61 const uint8_t* data_v = buffer->DataV();
62 stride = buffer->StrideV();
Philipp Hancke05eb7742025-03-19 14:54:1463 for (int j = 0; j < buffer->ChromaHeight(); ++j) {
64 fwrite(data_v + j * stride, /*size=*/1, stride, output_file);
Paulina Hensmanb671d462018-09-14 09:32:0065 }
66 }
67 if (ferror(output_file) != 0) {
68 RTC_LOG(LS_ERROR) << "Error writing to file " << file_name;
69 }
70 fclose(output_file);
71}
72
Yves Gerey3cb5e5b2019-03-21 10:22:4273} // Anonymous namespace
74
Evan Shrubsolea4fd9d92025-04-15 14:51:5775void WriteVideoToFile(const scoped_refptr<Video>& video,
Yves Gerey3cb5e5b2019-03-21 10:22:4276 const std::string& file_name,
77 int fps) {
78 WriteVideoToFile(video, file_name, fps,
79 /*isY4m=*/absl::EndsWith(file_name, ".y4m"));
80}
81
Evan Shrubsolea4fd9d92025-04-15 14:51:5782void WriteY4mVideoToFile(const scoped_refptr<Video>& video,
Yves Gerey3cb5e5b2019-03-21 10:22:4283 const std::string& file_name,
84 int fps) {
85 WriteVideoToFile(video, file_name, fps, /*isY4m=*/true);
86}
87
Evan Shrubsolea4fd9d92025-04-15 14:51:5788void WriteYuvVideoToFile(const scoped_refptr<Video>& video,
Yves Gerey3cb5e5b2019-03-21 10:22:4289 const std::string& file_name,
90 int fps) {
91 WriteVideoToFile(video, file_name, fps, /*isY4m=*/false);
92}
93
Paulina Hensmanb671d462018-09-14 09:32:0094} // namespace test
95} // namespace webrtc