frame_writer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2012 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. #ifndef TEST_TESTSUPPORT_FRAME_WRITER_H_
  11. #define TEST_TESTSUPPORT_FRAME_WRITER_H_
  12. #include <stdio.h>
  13. #include <string>
  14. #include "api/video/video_frame.h"
  15. namespace webrtc {
  16. namespace test {
  17. // Handles writing of video files.
  18. class FrameWriter {
  19. public:
  20. virtual ~FrameWriter() {}
  21. // Initializes the file handler, i.e. opens the input and output files etc.
  22. // This must be called before reading or writing frames has started.
  23. // Returns false if an error has occurred, in addition to printing to stderr.
  24. virtual bool Init() = 0;
  25. // Writes a frame of the configured frame length to the output file.
  26. // Returns true if the write was successful, false otherwise.
  27. virtual bool WriteFrame(uint8_t* frame_buffer) = 0;
  28. // Closes the output file if open. Essentially makes this class impossible
  29. // to use anymore. Will also be invoked by the destructor.
  30. virtual void Close() = 0;
  31. // Frame length in bytes of a single frame image.
  32. virtual size_t FrameLength() = 0;
  33. };
  34. // Writes raw I420 frames in sequence.
  35. class YuvFrameWriterImpl : public FrameWriter {
  36. public:
  37. // Creates a file handler. The input file is assumed to exist and be readable
  38. // and the output file must be writable.
  39. // Parameters:
  40. // output_filename The file to write. Will be overwritten if already
  41. // existing.
  42. // width, height Size of each frame to read.
  43. YuvFrameWriterImpl(std::string output_filename, int width, int height);
  44. ~YuvFrameWriterImpl() override;
  45. bool Init() override;
  46. bool WriteFrame(uint8_t* frame_buffer) override;
  47. void Close() override;
  48. size_t FrameLength() override;
  49. protected:
  50. const std::string output_filename_;
  51. size_t frame_length_in_bytes_;
  52. const int width_;
  53. const int height_;
  54. FILE* output_file_;
  55. };
  56. // Writes raw I420 frames in sequence, but with Y4M file and frame headers for
  57. // more convenient playback in external media players.
  58. class Y4mFrameWriterImpl : public YuvFrameWriterImpl {
  59. public:
  60. Y4mFrameWriterImpl(std::string output_filename,
  61. int width,
  62. int height,
  63. int frame_rate);
  64. ~Y4mFrameWriterImpl() override;
  65. bool Init() override;
  66. bool WriteFrame(uint8_t* frame_buffer) override;
  67. private:
  68. const int frame_rate_;
  69. };
  70. // LibJpeg is not available on iOS. This class will do nothing on iOS.
  71. class JpegFrameWriter {
  72. public:
  73. JpegFrameWriter(const std::string& output_filename);
  74. // Quality can be from 0 (worst) to 100 (best). Best quality is still lossy.
  75. // WriteFrame can be called only once. Subsequent calls will fail.
  76. bool WriteFrame(const VideoFrame& input_frame, int quality);
  77. #if !defined(WEBRTC_IOS)
  78. private:
  79. bool frame_written_;
  80. const std::string output_filename_;
  81. FILE* output_file_;
  82. #endif
  83. };
  84. } // namespace test
  85. } // namespace webrtc
  86. #endif // TEST_TESTSUPPORT_FRAME_WRITER_H_