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.

290 lines
9.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "common_video/include/video_frame_buffer.h"
  11. #include "api/video/i420_buffer.h"
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/ref_counted_object.h"
  14. #include "third_party/libyuv/include/libyuv/convert.h"
  15. namespace webrtc {
  16. namespace {
  17. // Template to implement a wrapped buffer for a I4??BufferInterface.
  18. template <typename Base>
  19. class WrappedYuvBuffer : public Base {
  20. public:
  21. WrappedYuvBuffer(int width,
  22. int height,
  23. const uint8_t* y_plane,
  24. int y_stride,
  25. const uint8_t* u_plane,
  26. int u_stride,
  27. const uint8_t* v_plane,
  28. int v_stride,
  29. const rtc::Callback0<void>& no_longer_used)
  30. : width_(width),
  31. height_(height),
  32. y_plane_(y_plane),
  33. u_plane_(u_plane),
  34. v_plane_(v_plane),
  35. y_stride_(y_stride),
  36. u_stride_(u_stride),
  37. v_stride_(v_stride),
  38. no_longer_used_cb_(no_longer_used) {}
  39. ~WrappedYuvBuffer() override { no_longer_used_cb_(); }
  40. int width() const override { return width_; }
  41. int height() const override { return height_; }
  42. const uint8_t* DataY() const override { return y_plane_; }
  43. const uint8_t* DataU() const override { return u_plane_; }
  44. const uint8_t* DataV() const override { return v_plane_; }
  45. int StrideY() const override { return y_stride_; }
  46. int StrideU() const override { return u_stride_; }
  47. int StrideV() const override { return v_stride_; }
  48. private:
  49. friend class rtc::RefCountedObject<WrappedYuvBuffer>;
  50. const int width_;
  51. const int height_;
  52. const uint8_t* const y_plane_;
  53. const uint8_t* const u_plane_;
  54. const uint8_t* const v_plane_;
  55. const int y_stride_;
  56. const int u_stride_;
  57. const int v_stride_;
  58. rtc::Callback0<void> no_longer_used_cb_;
  59. };
  60. // Template to implement a wrapped buffer for a I4??BufferInterface.
  61. template <typename BaseWithA>
  62. class WrappedYuvaBuffer : public WrappedYuvBuffer<BaseWithA> {
  63. public:
  64. WrappedYuvaBuffer(int width,
  65. int height,
  66. const uint8_t* y_plane,
  67. int y_stride,
  68. const uint8_t* u_plane,
  69. int u_stride,
  70. const uint8_t* v_plane,
  71. int v_stride,
  72. const uint8_t* a_plane,
  73. int a_stride,
  74. const rtc::Callback0<void>& no_longer_used)
  75. : WrappedYuvBuffer<BaseWithA>(width,
  76. height,
  77. y_plane,
  78. y_stride,
  79. u_plane,
  80. u_stride,
  81. v_plane,
  82. v_stride,
  83. no_longer_used),
  84. a_plane_(a_plane),
  85. a_stride_(a_stride) {}
  86. const uint8_t* DataA() const override { return a_plane_; }
  87. int StrideA() const override { return a_stride_; }
  88. private:
  89. const uint8_t* const a_plane_;
  90. const int a_stride_;
  91. };
  92. class I444BufferBase : public I444BufferInterface {
  93. public:
  94. rtc::scoped_refptr<I420BufferInterface> ToI420() final;
  95. };
  96. rtc::scoped_refptr<I420BufferInterface> I444BufferBase::ToI420() {
  97. rtc::scoped_refptr<I420Buffer> i420_buffer =
  98. I420Buffer::Create(width(), height());
  99. libyuv::I444ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
  100. i420_buffer->MutableDataY(), i420_buffer->StrideY(),
  101. i420_buffer->MutableDataU(), i420_buffer->StrideU(),
  102. i420_buffer->MutableDataV(), i420_buffer->StrideV(),
  103. width(), height());
  104. return i420_buffer;
  105. }
  106. // Template to implement a wrapped buffer for a PlanarYuv16BBuffer.
  107. template <typename Base>
  108. class WrappedYuv16BBuffer : public Base {
  109. public:
  110. WrappedYuv16BBuffer(int width,
  111. int height,
  112. const uint16_t* y_plane,
  113. int y_stride,
  114. const uint16_t* u_plane,
  115. int u_stride,
  116. const uint16_t* v_plane,
  117. int v_stride,
  118. const rtc::Callback0<void>& no_longer_used)
  119. : width_(width),
  120. height_(height),
  121. y_plane_(y_plane),
  122. u_plane_(u_plane),
  123. v_plane_(v_plane),
  124. y_stride_(y_stride),
  125. u_stride_(u_stride),
  126. v_stride_(v_stride),
  127. no_longer_used_cb_(no_longer_used) {}
  128. ~WrappedYuv16BBuffer() override { no_longer_used_cb_(); }
  129. int width() const override { return width_; }
  130. int height() const override { return height_; }
  131. const uint16_t* DataY() const override { return y_plane_; }
  132. const uint16_t* DataU() const override { return u_plane_; }
  133. const uint16_t* DataV() const override { return v_plane_; }
  134. int StrideY() const override { return y_stride_; }
  135. int StrideU() const override { return u_stride_; }
  136. int StrideV() const override { return v_stride_; }
  137. private:
  138. friend class rtc::RefCountedObject<WrappedYuv16BBuffer>;
  139. const int width_;
  140. const int height_;
  141. const uint16_t* const y_plane_;
  142. const uint16_t* const u_plane_;
  143. const uint16_t* const v_plane_;
  144. const int y_stride_;
  145. const int u_stride_;
  146. const int v_stride_;
  147. rtc::Callback0<void> no_longer_used_cb_;
  148. };
  149. class I010BufferBase : public I010BufferInterface {
  150. public:
  151. rtc::scoped_refptr<I420BufferInterface> ToI420() final;
  152. };
  153. rtc::scoped_refptr<I420BufferInterface> I010BufferBase::ToI420() {
  154. rtc::scoped_refptr<I420Buffer> i420_buffer =
  155. I420Buffer::Create(width(), height());
  156. libyuv::I010ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
  157. i420_buffer->MutableDataY(), i420_buffer->StrideY(),
  158. i420_buffer->MutableDataU(), i420_buffer->StrideU(),
  159. i420_buffer->MutableDataV(), i420_buffer->StrideV(),
  160. width(), height());
  161. return i420_buffer;
  162. }
  163. } // namespace
  164. rtc::scoped_refptr<I420BufferInterface> WrapI420Buffer(
  165. int width,
  166. int height,
  167. const uint8_t* y_plane,
  168. int y_stride,
  169. const uint8_t* u_plane,
  170. int u_stride,
  171. const uint8_t* v_plane,
  172. int v_stride,
  173. const rtc::Callback0<void>& no_longer_used) {
  174. return rtc::scoped_refptr<I420BufferInterface>(
  175. new rtc::RefCountedObject<WrappedYuvBuffer<I420BufferInterface>>(
  176. width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
  177. v_stride, no_longer_used));
  178. }
  179. rtc::scoped_refptr<I420ABufferInterface> WrapI420ABuffer(
  180. int width,
  181. int height,
  182. const uint8_t* y_plane,
  183. int y_stride,
  184. const uint8_t* u_plane,
  185. int u_stride,
  186. const uint8_t* v_plane,
  187. int v_stride,
  188. const uint8_t* a_plane,
  189. int a_stride,
  190. const rtc::Callback0<void>& no_longer_used) {
  191. return rtc::scoped_refptr<I420ABufferInterface>(
  192. new rtc::RefCountedObject<WrappedYuvaBuffer<I420ABufferInterface>>(
  193. width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
  194. v_stride, a_plane, a_stride, no_longer_used));
  195. }
  196. rtc::scoped_refptr<I444BufferInterface> WrapI444Buffer(
  197. int width,
  198. int height,
  199. const uint8_t* y_plane,
  200. int y_stride,
  201. const uint8_t* u_plane,
  202. int u_stride,
  203. const uint8_t* v_plane,
  204. int v_stride,
  205. const rtc::Callback0<void>& no_longer_used) {
  206. return rtc::scoped_refptr<I444BufferInterface>(
  207. new rtc::RefCountedObject<WrappedYuvBuffer<I444BufferBase>>(
  208. width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
  209. v_stride, no_longer_used));
  210. }
  211. rtc::scoped_refptr<PlanarYuvBuffer> WrapYuvBuffer(
  212. VideoFrameBuffer::Type type,
  213. int width,
  214. int height,
  215. const uint8_t* y_plane,
  216. int y_stride,
  217. const uint8_t* u_plane,
  218. int u_stride,
  219. const uint8_t* v_plane,
  220. int v_stride,
  221. const rtc::Callback0<void>& no_longer_used) {
  222. switch (type) {
  223. case VideoFrameBuffer::Type::kI420:
  224. return WrapI420Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
  225. v_plane, v_stride, no_longer_used);
  226. case VideoFrameBuffer::Type::kI444:
  227. return WrapI444Buffer(width, height, y_plane, y_stride, u_plane, u_stride,
  228. v_plane, v_stride, no_longer_used);
  229. default:
  230. FATAL() << "Unexpected frame buffer type.";
  231. return nullptr;
  232. }
  233. }
  234. rtc::scoped_refptr<I010BufferInterface> WrapI010Buffer(
  235. int width,
  236. int height,
  237. const uint16_t* y_plane,
  238. int y_stride,
  239. const uint16_t* u_plane,
  240. int u_stride,
  241. const uint16_t* v_plane,
  242. int v_stride,
  243. const rtc::Callback0<void>& no_longer_used) {
  244. return rtc::scoped_refptr<I010BufferInterface>(
  245. new rtc::RefCountedObject<WrappedYuv16BBuffer<I010BufferBase>>(
  246. width, height, y_plane, y_stride, u_plane, u_stride, v_plane,
  247. v_stride, no_longer_used));
  248. }
  249. } // namespace webrtc