xref: /aosp_15_r20/external/webrtc/api/video/i422_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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_I422_BUFFER_H_
12 #define API_VIDEO_I422_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 I422 buffer in standard memory.
27 class RTC_EXPORT I422Buffer : public I422BufferInterface {
28  public:
29   static rtc::scoped_refptr<I422Buffer> Create(int width, int height);
30   static rtc::scoped_refptr<I422Buffer> 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<I422Buffer> Copy(const I422BufferInterface& buffer);
38   /// Convert and put I420 buffer into a new buffer.
39   static rtc::scoped_refptr<I422Buffer> Copy(const I420BufferInterface& buffer);
40 
41   static rtc::scoped_refptr<I422Buffer> Copy(int width,
42                                              int height,
43                                              const uint8_t* data_y,
44                                              int stride_y,
45                                              const uint8_t* data_u,
46                                              int stride_u,
47                                              const uint8_t* data_v,
48                                              int stride_v);
49 
50   // Returns a rotated copy of `src`.
51   static rtc::scoped_refptr<I422Buffer> Rotate(const I422BufferInterface& src,
52                                                VideoRotation rotation);
53 
54   rtc::scoped_refptr<I420BufferInterface> ToI420() final;
GetI420()55   const I420BufferInterface* GetI420() const final { return nullptr; }
56 
57   // Sets the buffer to all black.
58   static void SetBlack(I422Buffer* 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(https://crbug.com/390941): Deprecated. Should be deleted if/when those
65   // issues are resolved in a better way. Or in the mean time, use SetBlack.
66   void InitializeData();
67 
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 
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`.
84   void CropAndScaleFrom(const I422BufferInterface& src,
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.
92   void CropAndScaleFrom(const I422BufferInterface& src);
93 
94   // Scale all of `src` to the size of `this` buffer, with no cropping.
95   void ScaleFrom(const I422BufferInterface& src);
96 
97  protected:
98   I422Buffer(int width, int height);
99   I422Buffer(int width, int height, int stride_y, int stride_u, int stride_v);
100 
101   ~I422Buffer() 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 
114 #endif  // API_VIDEO_I422_BUFFER_H_
115