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