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