i420_buffer.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_I420_BUFFER_H_
  11. #define API_VIDEO_I420_BUFFER_H_
  12. #include <stdint.h>
  13. #include <memory>
  14. #include "api/scoped_refptr.h"
  15. #include "api/video/video_frame_buffer.h"
  16. #include "api/video/video_rotation.h"
  17. #include "rtc_base/memory/aligned_malloc.h"
  18. #include "rtc_base/system/rtc_export.h"
  19. namespace webrtc {
  20. // Plain I420 buffer in standard memory.
  21. class RTC_EXPORT I420Buffer : public I420BufferInterface {
  22. public:
  23. static rtc::scoped_refptr<I420Buffer> Create(int width, int height);
  24. static rtc::scoped_refptr<I420Buffer> Create(int width,
  25. int height,
  26. int stride_y,
  27. int stride_u,
  28. int stride_v);
  29. // Create a new buffer and copy the pixel data.
  30. static rtc::scoped_refptr<I420Buffer> Copy(const I420BufferInterface& buffer);
  31. // Deprecated.
  32. static rtc::scoped_refptr<I420Buffer> Copy(const VideoFrameBuffer& buffer) {
  33. return Copy(*buffer.GetI420());
  34. }
  35. static rtc::scoped_refptr<I420Buffer> Copy(int width,
  36. int height,
  37. const uint8_t* data_y,
  38. int stride_y,
  39. const uint8_t* data_u,
  40. int stride_u,
  41. const uint8_t* data_v,
  42. int stride_v);
  43. // Returns a rotated copy of |src|.
  44. static rtc::scoped_refptr<I420Buffer> Rotate(const I420BufferInterface& src,
  45. VideoRotation rotation);
  46. // Deprecated.
  47. static rtc::scoped_refptr<I420Buffer> Rotate(const VideoFrameBuffer& src,
  48. VideoRotation rotation) {
  49. return Rotate(*src.GetI420(), rotation);
  50. }
  51. // Sets the buffer to all black.
  52. static void SetBlack(I420Buffer* buffer);
  53. // Sets all three planes to all zeros. Used to work around for
  54. // quirks in memory checkers
  55. // (https://bugs.chromium.org/p/libyuv/issues/detail?id=377) and
  56. // ffmpeg (http://crbug.com/390941).
  57. // TODO(nisse): Deprecated. Should be deleted if/when those issues
  58. // are resolved in a better way. Or in the mean time, use SetBlack.
  59. void InitializeData();
  60. int width() const override;
  61. int height() const override;
  62. const uint8_t* DataY() const override;
  63. const uint8_t* DataU() const override;
  64. const uint8_t* DataV() const override;
  65. int StrideY() const override;
  66. int StrideU() const override;
  67. int StrideV() const override;
  68. uint8_t* MutableDataY();
  69. uint8_t* MutableDataU();
  70. uint8_t* MutableDataV();
  71. // Scale the cropped area of |src| to the size of |this| buffer, and
  72. // write the result into |this|.
  73. void CropAndScaleFrom(const I420BufferInterface& src,
  74. int offset_x,
  75. int offset_y,
  76. int crop_width,
  77. int crop_height);
  78. // The common case of a center crop, when needed to adjust the
  79. // aspect ratio without distorting the image.
  80. void CropAndScaleFrom(const I420BufferInterface& src);
  81. // Scale all of |src| to the size of |this| buffer, with no cropping.
  82. void ScaleFrom(const I420BufferInterface& src);
  83. // Pastes whole picture to canvas at (offset_row, offset_col).
  84. // Offsets and picture dimensions must be even.
  85. void PasteFrom(const I420BufferInterface& picture,
  86. int offset_col,
  87. int offset_row);
  88. protected:
  89. I420Buffer(int width, int height);
  90. I420Buffer(int width, int height, int stride_y, int stride_u, int stride_v);
  91. ~I420Buffer() override;
  92. private:
  93. const int width_;
  94. const int height_;
  95. const int stride_y_;
  96. const int stride_u_;
  97. const int stride_v_;
  98. const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_;
  99. };
  100. } // namespace webrtc
  101. #endif // API_VIDEO_I420_BUFFER_H_