You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

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