| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 1 | /* |
| 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 Hancke | 83b748d | 2025-07-02 17:22:21 | [diff] [blame] | 13 | #include <cstdint> |
| Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 14 | #include <cstdio> |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 15 | #include <string> |
| 16 | |
| Steve Anton | 68586e8 | 2018-12-14 01:41:25 | [diff] [blame] | 17 | #include "absl/strings/match.h" |
| Philipp Hancke | 83b748d | 2025-07-02 17:22:21 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
| Yves Gerey | 3e70781 | 2018-11-28 15:47:49 | [diff] [blame] | 19 | #include "api/video/video_frame_buffer.h" |
| Philipp Hancke | 83b748d | 2025-07-02 17:22:21 | [diff] [blame] | 20 | #include "rtc_base/checks.h" |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 21 | #include "rtc_base/logging.h" |
| Philipp Hancke | 83b748d | 2025-07-02 17:22:21 | [diff] [blame] | 22 | #include "rtc_tools/video_file_reader.h" |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace test { |
| Yves Gerey | 3cb5e5b | 2019-03-21 10:22:42 | [diff] [blame] | 26 | namespace { |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 27 | |
| Evan Shrubsole | a4fd9d9 | 2025-04-15 14:51:57 | [diff] [blame] | 28 | void WriteVideoToFile(const scoped_refptr<Video>& video, |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 29 | const std::string& file_name, |
| Yves Gerey | 3cb5e5b | 2019-03-21 10:22:42 | [diff] [blame] | 30 | int fps, |
| 31 | bool isY4m) { |
| 32 | RTC_CHECK(video); |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 33 | 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 Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 39 | 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 Shrubsole | a4fd9d9 | 2025-04-15 14:51:57 | [diff] [blame] | 48 | scoped_refptr<I420BufferInterface> buffer = video->GetFrame(i); |
| Yves Gerey | 3cb5e5b | 2019-03-21 10:22:42 | [diff] [blame] | 49 | RTC_CHECK(buffer) << "Frame: " << i |
| 50 | << "\nWhile trying to create: " << file_name; |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 51 | const uint8_t* data_y = buffer->DataY(); |
| 52 | int stride = buffer->StrideY(); |
| Philipp Hancke | 05eb774 | 2025-03-19 14:54:14 | [diff] [blame] | 53 | for (int j = 0; j < video->height(); ++j) { |
| 54 | fwrite(data_y + j * stride, /*size=*/1, stride, output_file); |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 55 | } |
| 56 | const uint8_t* data_u = buffer->DataU(); |
| 57 | stride = buffer->StrideU(); |
| Philipp Hancke | 05eb774 | 2025-03-19 14:54:14 | [diff] [blame] | 58 | for (int j = 0; j < buffer->ChromaHeight(); ++j) { |
| 59 | fwrite(data_u + j * stride, /*size=*/1, stride, output_file); |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 60 | } |
| 61 | const uint8_t* data_v = buffer->DataV(); |
| 62 | stride = buffer->StrideV(); |
| Philipp Hancke | 05eb774 | 2025-03-19 14:54:14 | [diff] [blame] | 63 | for (int j = 0; j < buffer->ChromaHeight(); ++j) { |
| 64 | fwrite(data_v + j * stride, /*size=*/1, stride, output_file); |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 65 | } |
| 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 Gerey | 3cb5e5b | 2019-03-21 10:22:42 | [diff] [blame] | 73 | } // Anonymous namespace |
| 74 | |
| Evan Shrubsole | a4fd9d9 | 2025-04-15 14:51:57 | [diff] [blame] | 75 | void WriteVideoToFile(const scoped_refptr<Video>& video, |
| Yves Gerey | 3cb5e5b | 2019-03-21 10:22:42 | [diff] [blame] | 76 | const std::string& file_name, |
| 77 | int fps) { |
| 78 | WriteVideoToFile(video, file_name, fps, |
| 79 | /*isY4m=*/absl::EndsWith(file_name, ".y4m")); |
| 80 | } |
| 81 | |
| Evan Shrubsole | a4fd9d9 | 2025-04-15 14:51:57 | [diff] [blame] | 82 | void WriteY4mVideoToFile(const scoped_refptr<Video>& video, |
| Yves Gerey | 3cb5e5b | 2019-03-21 10:22:42 | [diff] [blame] | 83 | const std::string& file_name, |
| 84 | int fps) { |
| 85 | WriteVideoToFile(video, file_name, fps, /*isY4m=*/true); |
| 86 | } |
| 87 | |
| Evan Shrubsole | a4fd9d9 | 2025-04-15 14:51:57 | [diff] [blame] | 88 | void WriteYuvVideoToFile(const scoped_refptr<Video>& video, |
| Yves Gerey | 3cb5e5b | 2019-03-21 10:22:42 | [diff] [blame] | 89 | const std::string& file_name, |
| 90 | int fps) { |
| 91 | WriteVideoToFile(video, file_name, fps, /*isY4m=*/false); |
| 92 | } |
| 93 | |
| Paulina Hensman | b671d46 | 2018-09-14 09:32:00 | [diff] [blame] | 94 | } // namespace test |
| 95 | } // namespace webrtc |