video_frame_buffer.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2015 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 API_VIDEO_VIDEO_FRAME_BUFFER_H_
  11. #define API_VIDEO_VIDEO_FRAME_BUFFER_H_
  12. #include <stdint.h>
  13. #include "api/scoped_refptr.h"
  14. #include "rtc_base/ref_count.h"
  15. #include "rtc_base/system/rtc_export.h"
  16. namespace webrtc {
  17. class I420BufferInterface;
  18. class I420ABufferInterface;
  19. class I444BufferInterface;
  20. class I010BufferInterface;
  21. // Base class for frame buffers of different types of pixel format and storage.
  22. // The tag in type() indicates how the data is represented, and each type is
  23. // implemented as a subclass. To access the pixel data, call the appropriate
  24. // GetXXX() function, where XXX represents the type. There is also a function
  25. // ToI420() that returns a frame buffer in I420 format, converting from the
  26. // underlying representation if necessary. I420 is the most widely accepted
  27. // format and serves as a fallback for video sinks that can only handle I420,
  28. // e.g. the internal WebRTC software encoders. A special enum value 'kNative' is
  29. // provided for external clients to implement their own frame buffer
  30. // representations, e.g. as textures. The external client can produce such
  31. // native frame buffers from custom video sources, and then cast it back to the
  32. // correct subclass in custom video sinks. The purpose of this is to improve
  33. // performance by providing an optimized path without intermediate conversions.
  34. // Frame metadata such as rotation and timestamp are stored in
  35. // webrtc::VideoFrame, and not here.
  36. class RTC_EXPORT VideoFrameBuffer : public rtc::RefCountInterface {
  37. public:
  38. // New frame buffer types will be added conservatively when there is an
  39. // opportunity to optimize the path between some pair of video source and
  40. // video sink.
  41. enum class Type {
  42. kNative,
  43. kI420,
  44. kI420A,
  45. kI444,
  46. kI010,
  47. };
  48. // This function specifies in what pixel format the data is stored in.
  49. virtual Type type() const = 0;
  50. // The resolution of the frame in pixels. For formats where some planes are
  51. // subsampled, this is the highest-resolution plane.
  52. virtual int width() const = 0;
  53. virtual int height() const = 0;
  54. // Returns a memory-backed frame buffer in I420 format. If the pixel data is
  55. // in another format, a conversion will take place. All implementations must
  56. // provide a fallback to I420 for compatibility with e.g. the internal WebRTC
  57. // software encoders.
  58. virtual rtc::scoped_refptr<I420BufferInterface> ToI420() = 0;
  59. // GetI420() methods should return I420 buffer if conversion is trivial, i.e
  60. // no change for binary data is needed. Otherwise these methods should return
  61. // nullptr. One example of buffer with that property is
  62. // WebrtcVideoFrameAdapter in Chrome - it's I420 buffer backed by a shared
  63. // memory buffer. Therefore it must have type kNative. Yet, ToI420()
  64. // doesn't affect binary data at all. Another example is any I420A buffer.
  65. virtual const I420BufferInterface* GetI420() const;
  66. // These functions should only be called if type() is of the correct type.
  67. // Calling with a different type will result in a crash.
  68. const I420ABufferInterface* GetI420A() const;
  69. const I444BufferInterface* GetI444() const;
  70. const I010BufferInterface* GetI010() const;
  71. protected:
  72. ~VideoFrameBuffer() override {}
  73. };
  74. // This interface represents planar formats.
  75. class PlanarYuvBuffer : public VideoFrameBuffer {
  76. public:
  77. virtual int ChromaWidth() const = 0;
  78. virtual int ChromaHeight() const = 0;
  79. // Returns the number of steps(in terms of Data*() return type) between
  80. // successive rows for a given plane.
  81. virtual int StrideY() const = 0;
  82. virtual int StrideU() const = 0;
  83. virtual int StrideV() const = 0;
  84. protected:
  85. ~PlanarYuvBuffer() override {}
  86. };
  87. // This interface represents 8-bit color depth formats: Type::kI420,
  88. // Type::kI420A and Type::kI444.
  89. class PlanarYuv8Buffer : public PlanarYuvBuffer {
  90. public:
  91. // Returns pointer to the pixel data for a given plane. The memory is owned by
  92. // the VideoFrameBuffer object and must not be freed by the caller.
  93. virtual const uint8_t* DataY() const = 0;
  94. virtual const uint8_t* DataU() const = 0;
  95. virtual const uint8_t* DataV() const = 0;
  96. protected:
  97. ~PlanarYuv8Buffer() override {}
  98. };
  99. class RTC_EXPORT I420BufferInterface : public PlanarYuv8Buffer {
  100. public:
  101. Type type() const override;
  102. int ChromaWidth() const final;
  103. int ChromaHeight() const final;
  104. rtc::scoped_refptr<I420BufferInterface> ToI420() final;
  105. const I420BufferInterface* GetI420() const final;
  106. protected:
  107. ~I420BufferInterface() override {}
  108. };
  109. class RTC_EXPORT I420ABufferInterface : public I420BufferInterface {
  110. public:
  111. Type type() const final;
  112. virtual const uint8_t* DataA() const = 0;
  113. virtual int StrideA() const = 0;
  114. protected:
  115. ~I420ABufferInterface() override {}
  116. };
  117. class I444BufferInterface : public PlanarYuv8Buffer {
  118. public:
  119. Type type() const final;
  120. int ChromaWidth() const final;
  121. int ChromaHeight() const final;
  122. protected:
  123. ~I444BufferInterface() override {}
  124. };
  125. // This interface represents 8-bit to 16-bit color depth formats: Type::kI010.
  126. class PlanarYuv16BBuffer : public PlanarYuvBuffer {
  127. public:
  128. // Returns pointer to the pixel data for a given plane. The memory is owned by
  129. // the VideoFrameBuffer object and must not be freed by the caller.
  130. virtual const uint16_t* DataY() const = 0;
  131. virtual const uint16_t* DataU() const = 0;
  132. virtual const uint16_t* DataV() const = 0;
  133. protected:
  134. ~PlanarYuv16BBuffer() override {}
  135. };
  136. // Represents Type::kI010, allocates 16 bits per pixel and fills 10 least
  137. // significant bits with color information.
  138. class I010BufferInterface : public PlanarYuv16BBuffer {
  139. public:
  140. Type type() const override;
  141. int ChromaWidth() const final;
  142. int ChromaHeight() const final;
  143. protected:
  144. ~I010BufferInterface() override {}
  145. };
  146. } // namespace webrtc
  147. #endif // API_VIDEO_VIDEO_FRAME_BUFFER_H_