Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [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 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <cstdio> |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 15 | #include <string> |
| 16 | |
Steve Anton | 68586e8 | 2018-12-13 17:41:25 -0800 | [diff] [blame] | 17 | #include "absl/strings/match.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 18 | #include "api/video/video_frame_buffer.h" |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 19 | #include "rtc_base/logging.h" |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 20 | |
| 21 | namespace webrtc { |
| 22 | namespace test { |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 23 | namespace { |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 24 | |
| 25 | void WriteVideoToFile(const rtc::scoped_refptr<Video>& video, |
| 26 | const std::string& file_name, |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 27 | int fps, |
| 28 | bool isY4m) { |
| 29 | RTC_CHECK(video); |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 30 | FILE* output_file = fopen(file_name.c_str(), "wb"); |
| 31 | if (output_file == nullptr) { |
| 32 | RTC_LOG(LS_ERROR) << "Could not open file for writing: " << file_name; |
| 33 | return; |
| 34 | } |
| 35 | |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 36 | if (isY4m) { |
| 37 | fprintf(output_file, "YUV4MPEG2 W%d H%d F%d:1 C420\n", video->width(), |
| 38 | video->height(), fps); |
| 39 | } |
| 40 | for (size_t i = 0; i < video->number_of_frames(); ++i) { |
| 41 | if (isY4m) { |
| 42 | std::string frame = "FRAME\n"; |
| 43 | fwrite(frame.c_str(), 1, 6, output_file); |
| 44 | } |
| 45 | rtc::scoped_refptr<I420BufferInterface> buffer = video->GetFrame(i); |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 46 | RTC_CHECK(buffer) << "Frame: " << i |
| 47 | << "\nWhile trying to create: " << file_name; |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 48 | const uint8_t* data_y = buffer->DataY(); |
| 49 | int stride = buffer->StrideY(); |
| 50 | for (int i = 0; i < video->height(); ++i) { |
| 51 | fwrite(data_y + i * stride, /*size=*/1, stride, output_file); |
| 52 | } |
| 53 | const uint8_t* data_u = buffer->DataU(); |
| 54 | stride = buffer->StrideU(); |
| 55 | for (int i = 0; i < buffer->ChromaHeight(); ++i) { |
| 56 | fwrite(data_u + i * stride, /*size=*/1, stride, output_file); |
| 57 | } |
| 58 | const uint8_t* data_v = buffer->DataV(); |
| 59 | stride = buffer->StrideV(); |
| 60 | for (int i = 0; i < buffer->ChromaHeight(); ++i) { |
| 61 | fwrite(data_v + i * stride, /*size=*/1, stride, output_file); |
| 62 | } |
| 63 | } |
| 64 | if (ferror(output_file) != 0) { |
| 65 | RTC_LOG(LS_ERROR) << "Error writing to file " << file_name; |
| 66 | } |
| 67 | fclose(output_file); |
| 68 | } |
| 69 | |
Yves Gerey | 3cb5e5b | 2019-03-21 11:22:42 +0100 | [diff] [blame^] | 70 | } // Anonymous namespace |
| 71 | |
| 72 | void WriteVideoToFile(const rtc::scoped_refptr<Video>& video, |
| 73 | const std::string& file_name, |
| 74 | int fps) { |
| 75 | WriteVideoToFile(video, file_name, fps, |
| 76 | /*isY4m=*/absl::EndsWith(file_name, ".y4m")); |
| 77 | } |
| 78 | |
| 79 | void WriteY4mVideoToFile(const rtc::scoped_refptr<Video>& video, |
| 80 | const std::string& file_name, |
| 81 | int fps) { |
| 82 | WriteVideoToFile(video, file_name, fps, /*isY4m=*/true); |
| 83 | } |
| 84 | |
| 85 | void WriteYuvVideoToFile(const rtc::scoped_refptr<Video>& video, |
| 86 | const std::string& file_name, |
| 87 | int fps) { |
| 88 | WriteVideoToFile(video, file_name, fps, /*isY4m=*/false); |
| 89 | } |
| 90 | |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 91 | } // namespace test |
| 92 | } // namespace webrtc |