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.

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