nisse | af91689 | 2017-01-10 07:44:26 -0800 | [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 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef API_VIDEO_I420_BUFFER_H_ |
| 12 | #define API_VIDEO_I420_BUFFER_H_ |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 13 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 14 | #include <stdint.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 15 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 19 | #include "api/video/video_frame_buffer.h" |
Karl Wiberg | 29e7bee | 2018-03-22 14:11:52 +0100 | [diff] [blame] | 20 | #include "api/video/video_rotation.h" |
| 21 | #include "rtc_base/memory/aligned_malloc.h" |
Mirko Bonadei | 977b46a | 2018-10-24 16:22:04 +0200 | [diff] [blame] | 22 | #include "rtc_base/system/rtc_export.h" |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | |
| 26 | // Plain I420 buffer in standard memory. |
Mirko Bonadei | 977b46a | 2018-10-24 16:22:04 +0200 | [diff] [blame] | 27 | class RTC_EXPORT I420Buffer : public I420BufferInterface { |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 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. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 37 | static rtc::scoped_refptr<I420Buffer> Copy(const I420BufferInterface& buffer); |
| 38 | // Deprecated. |
| 39 | static rtc::scoped_refptr<I420Buffer> Copy(const VideoFrameBuffer& buffer) { |
| 40 | return Copy(*buffer.GetI420()); |
| 41 | } |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 42 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 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); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 51 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame^] | 52 | // Returns a rotated copy of `src`. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 53 | static rtc::scoped_refptr<I420Buffer> Rotate(const I420BufferInterface& src, |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 54 | VideoRotation rotation); |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 55 | // Deprecated. |
| 56 | static rtc::scoped_refptr<I420Buffer> Rotate(const VideoFrameBuffer& src, |
| 57 | VideoRotation rotation) { |
| 58 | return Rotate(*src.GetI420(), rotation); |
| 59 | } |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 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(nisse): Deprecated. Should be deleted if/when those issues |
| 69 | // are resolved in a better way. Or in the mean time, use SetBlack. |
| 70 | void InitializeData(); |
| 71 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 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 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 82 | uint8_t* MutableDataY(); |
| 83 | uint8_t* MutableDataU(); |
| 84 | uint8_t* MutableDataV(); |
| 85 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame^] | 86 | // Scale the cropped area of `src` to the size of `this` buffer, and |
| 87 | // write the result into `this`. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 88 | void CropAndScaleFrom(const I420BufferInterface& src, |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 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. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 96 | void CropAndScaleFrom(const I420BufferInterface& src); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 97 | |
Artem Titov | 0e61fdd | 2021-07-25 21:50:14 +0200 | [diff] [blame^] | 98 | // Scale all of `src` to the size of `this` buffer, with no cropping. |
magjed | 3f07549 | 2017-06-01 10:02:26 -0700 | [diff] [blame] | 99 | void ScaleFrom(const I420BufferInterface& src); |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 100 | |
Ilya Nikolaevskiy | a921660 | 2018-12-21 14:21:08 +0100 | [diff] [blame] | 101 | // Pastes whole picture to canvas at (offset_row, offset_col). |
| 102 | // Offsets and picture dimensions must be even. |
| 103 | void PasteFrom(const I420BufferInterface& picture, |
| 104 | int offset_col, |
| 105 | int offset_row); |
| 106 | |
nisse | af91689 | 2017-01-10 07:44:26 -0800 | [diff] [blame] | 107 | protected: |
| 108 | I420Buffer(int width, int height); |
| 109 | I420Buffer(int width, int height, int stride_y, int stride_u, int stride_v); |
| 110 | |
| 111 | ~I420Buffer() override; |
| 112 | |
| 113 | private: |
| 114 | const int width_; |
| 115 | const int height_; |
| 116 | const int stride_y_; |
| 117 | const int stride_u_; |
| 118 | const int stride_v_; |
| 119 | const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_; |
| 120 | }; |
| 121 | |
| 122 | } // namespace webrtc |
| 123 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 124 | #endif // API_VIDEO_I420_BUFFER_H_ |