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