video_frame_buffer.h 7.1 KB

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