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.

266 lines
8.8 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 "api/video/i010_buffer.h"
  11. #include <utility>
  12. #include "api/video/i420_buffer.h"
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/ref_counted_object.h"
  15. #include "third_party/libyuv/include/libyuv/convert.h"
  16. #include "third_party/libyuv/include/libyuv/scale.h"
  17. // Aligning pointer to 64 bytes for improved performance, e.g. use SIMD.
  18. static const int kBufferAlignment = 64;
  19. static const int kBytesPerPixel = 2;
  20. namespace webrtc {
  21. namespace {
  22. int I010DataSize(int height, int stride_y, int stride_u, int stride_v) {
  23. return kBytesPerPixel *
  24. (stride_y * height + (stride_u + stride_v) * ((height + 1) / 2));
  25. }
  26. } // namespace
  27. I010Buffer::I010Buffer(int width,
  28. int height,
  29. int stride_y,
  30. int stride_u,
  31. int stride_v)
  32. : width_(width),
  33. height_(height),
  34. stride_y_(stride_y),
  35. stride_u_(stride_u),
  36. stride_v_(stride_v),
  37. data_(static_cast<uint16_t*>(
  38. AlignedMalloc(I010DataSize(height, stride_y, stride_u, stride_v),
  39. kBufferAlignment))) {
  40. RTC_DCHECK_GT(width, 0);
  41. RTC_DCHECK_GT(height, 0);
  42. RTC_DCHECK_GE(stride_y, width);
  43. RTC_DCHECK_GE(stride_u, (width + 1) / 2);
  44. RTC_DCHECK_GE(stride_v, (width + 1) / 2);
  45. }
  46. I010Buffer::~I010Buffer() {}
  47. // static
  48. rtc::scoped_refptr<I010Buffer> I010Buffer::Create(int width, int height) {
  49. return new rtc::RefCountedObject<I010Buffer>(
  50. width, height, width, (width + 1) / 2, (width + 1) / 2);
  51. }
  52. // static
  53. rtc::scoped_refptr<I010Buffer> I010Buffer::Copy(
  54. const I010BufferInterface& source) {
  55. const int width = source.width();
  56. const int height = source.height();
  57. rtc::scoped_refptr<I010Buffer> buffer = Create(width, height);
  58. RTC_CHECK_EQ(
  59. 0, libyuv::I010Copy(
  60. source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
  61. source.DataV(), source.StrideV(), buffer->MutableDataY(),
  62. buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
  63. buffer->MutableDataV(), buffer->StrideV(), width, height));
  64. return buffer;
  65. }
  66. // static
  67. rtc::scoped_refptr<I010Buffer> I010Buffer::Copy(
  68. const I420BufferInterface& source) {
  69. const int width = source.width();
  70. const int height = source.height();
  71. rtc::scoped_refptr<I010Buffer> buffer = Create(width, height);
  72. RTC_CHECK_EQ(
  73. 0, libyuv::I420ToI010(
  74. source.DataY(), source.StrideY(), source.DataU(), source.StrideU(),
  75. source.DataV(), source.StrideV(), buffer->MutableDataY(),
  76. buffer->StrideY(), buffer->MutableDataU(), buffer->StrideU(),
  77. buffer->MutableDataV(), buffer->StrideV(), width, height));
  78. return buffer;
  79. }
  80. // static
  81. rtc::scoped_refptr<I010Buffer> I010Buffer::Rotate(
  82. const I010BufferInterface& src,
  83. VideoRotation rotation) {
  84. if (rotation == webrtc::kVideoRotation_0)
  85. return Copy(src);
  86. RTC_CHECK(src.DataY());
  87. RTC_CHECK(src.DataU());
  88. RTC_CHECK(src.DataV());
  89. int rotated_width = src.width();
  90. int rotated_height = src.height();
  91. if (rotation == webrtc::kVideoRotation_90 ||
  92. rotation == webrtc::kVideoRotation_270) {
  93. std::swap(rotated_width, rotated_height);
  94. }
  95. rtc::scoped_refptr<webrtc::I010Buffer> buffer =
  96. Create(rotated_width, rotated_height);
  97. // TODO(emircan): Remove this when there is libyuv::I010Rotate().
  98. for (int x = 0; x < src.width(); x++) {
  99. for (int y = 0; y < src.height(); y++) {
  100. int dest_x = x;
  101. int dest_y = y;
  102. switch (rotation) {
  103. // This case is covered by the early return.
  104. case webrtc::kVideoRotation_0:
  105. RTC_NOTREACHED();
  106. break;
  107. case webrtc::kVideoRotation_90:
  108. dest_x = src.height() - y - 1;
  109. dest_y = x;
  110. break;
  111. case webrtc::kVideoRotation_180:
  112. dest_x = src.width() - x - 1;
  113. dest_y = src.height() - y - 1;
  114. break;
  115. case webrtc::kVideoRotation_270:
  116. dest_x = y;
  117. dest_y = src.width() - x - 1;
  118. break;
  119. }
  120. buffer->MutableDataY()[dest_x + buffer->StrideY() * dest_y] =
  121. src.DataY()[x + src.StrideY() * y];
  122. dest_x /= 2;
  123. dest_y /= 2;
  124. int src_x = x / 2;
  125. int src_y = y / 2;
  126. buffer->MutableDataU()[dest_x + buffer->StrideU() * dest_y] =
  127. src.DataU()[src_x + src.StrideU() * src_y];
  128. buffer->MutableDataV()[dest_x + buffer->StrideV() * dest_y] =
  129. src.DataV()[src_x + src.StrideV() * src_y];
  130. }
  131. }
  132. return buffer;
  133. }
  134. rtc::scoped_refptr<I420BufferInterface> I010Buffer::ToI420() {
  135. rtc::scoped_refptr<I420Buffer> i420_buffer =
  136. I420Buffer::Create(width(), height());
  137. libyuv::I010ToI420(DataY(), StrideY(), DataU(), StrideU(), DataV(), StrideV(),
  138. i420_buffer->MutableDataY(), i420_buffer->StrideY(),
  139. i420_buffer->MutableDataU(), i420_buffer->StrideU(),
  140. i420_buffer->MutableDataV(), i420_buffer->StrideV(),
  141. width(), height());
  142. return i420_buffer;
  143. }
  144. int I010Buffer::width() const {
  145. return width_;
  146. }
  147. int I010Buffer::height() const {
  148. return height_;
  149. }
  150. const uint16_t* I010Buffer::DataY() const {
  151. return data_.get();
  152. }
  153. const uint16_t* I010Buffer::DataU() const {
  154. return data_.get() + stride_y_ * height_;
  155. }
  156. const uint16_t* I010Buffer::DataV() const {
  157. return data_.get() + stride_y_ * height_ + stride_u_ * ((height_ + 1) / 2);
  158. }
  159. int I010Buffer::StrideY() const {
  160. return stride_y_;
  161. }
  162. int I010Buffer::StrideU() const {
  163. return stride_u_;
  164. }
  165. int I010Buffer::StrideV() const {
  166. return stride_v_;
  167. }
  168. uint16_t* I010Buffer::MutableDataY() {
  169. return const_cast<uint16_t*>(DataY());
  170. }
  171. uint16_t* I010Buffer::MutableDataU() {
  172. return const_cast<uint16_t*>(DataU());
  173. }
  174. uint16_t* I010Buffer::MutableDataV() {
  175. return const_cast<uint16_t*>(DataV());
  176. }
  177. void I010Buffer::CropAndScaleFrom(const I010BufferInterface& src,
  178. int offset_x,
  179. int offset_y,
  180. int crop_width,
  181. int crop_height) {
  182. RTC_CHECK_LE(crop_width, src.width());
  183. RTC_CHECK_LE(crop_height, src.height());
  184. RTC_CHECK_LE(crop_width + offset_x, src.width());
  185. RTC_CHECK_LE(crop_height + offset_y, src.height());
  186. RTC_CHECK_GE(offset_x, 0);
  187. RTC_CHECK_GE(offset_y, 0);
  188. // Make sure offset is even so that u/v plane becomes aligned.
  189. const int uv_offset_x = offset_x / 2;
  190. const int uv_offset_y = offset_y / 2;
  191. offset_x = uv_offset_x * 2;
  192. offset_y = uv_offset_y * 2;
  193. const uint16_t* y_plane = src.DataY() + src.StrideY() * offset_y + offset_x;
  194. const uint16_t* u_plane =
  195. src.DataU() + src.StrideU() * uv_offset_y + uv_offset_x;
  196. const uint16_t* v_plane =
  197. src.DataV() + src.StrideV() * uv_offset_y + uv_offset_x;
  198. int res = libyuv::I420Scale_16(
  199. y_plane, src.StrideY(), u_plane, src.StrideU(), v_plane, src.StrideV(),
  200. crop_width, crop_height, MutableDataY(), StrideY(), MutableDataU(),
  201. StrideU(), MutableDataV(), StrideV(), width(), height(),
  202. libyuv::kFilterBox);
  203. RTC_DCHECK_EQ(res, 0);
  204. }
  205. void I010Buffer::ScaleFrom(const I010BufferInterface& src) {
  206. CropAndScaleFrom(src, 0, 0, src.width(), src.height());
  207. }
  208. void I010Buffer::PasteFrom(const I010BufferInterface& picture,
  209. int offset_col,
  210. int offset_row) {
  211. RTC_CHECK_LE(picture.width() + offset_col, width());
  212. RTC_CHECK_LE(picture.height() + offset_row, height());
  213. RTC_CHECK_GE(offset_col, 0);
  214. RTC_CHECK_GE(offset_row, 0);
  215. // Pasted picture has to be aligned so subsumpled UV plane isn't corrupted.
  216. RTC_CHECK(offset_col % 2 == 0);
  217. RTC_CHECK(offset_row % 2 == 0);
  218. RTC_CHECK(picture.width() % 2 == 0 ||
  219. picture.width() + offset_col == width());
  220. RTC_CHECK(picture.height() % 2 == 0 ||
  221. picture.height() + offset_row == height());
  222. libyuv::CopyPlane_16(picture.DataY(), picture.StrideY(),
  223. MutableDataY() + StrideY() * offset_row + offset_col,
  224. StrideY(), picture.width(), picture.height());
  225. libyuv::CopyPlane_16(
  226. picture.DataU(), picture.StrideU(),
  227. MutableDataU() + StrideU() * offset_row / 2 + offset_col / 2, StrideU(),
  228. picture.width() / 2, picture.height() / 2);
  229. libyuv::CopyPlane_16(
  230. picture.DataV(), picture.StrideV(),
  231. MutableDataV() + StrideV() * offset_row / 2 + offset_col / 2, StrideV(),
  232. picture.width() / 2, picture.height() / 2);
  233. }
  234. } // namespace webrtc