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