xref: /aosp_15_r20/external/webrtc/api/video/nv12_buffer.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright (c) 2020 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_NV12_BUFFER_H_
12 #define API_VIDEO_NV12_BUFFER_H_
13 
14 #include <memory>
15 #include <utility>
16 
17 #include "api/scoped_refptr.h"
18 #include "api/video/video_frame_buffer.h"
19 #include "rtc_base/memory/aligned_malloc.h"
20 #include "rtc_base/system/rtc_export.h"
21 
22 namespace webrtc {
23 
24 // NV12 is a biplanar encoding format, with full-resolution Y and
25 // half-resolution interleved UV. More information can be found at
26 // http://msdn.microsoft.com/library/windows/desktop/dd206750.aspx#nv12.
27 class RTC_EXPORT NV12Buffer : public NV12BufferInterface {
28  public:
29   static rtc::scoped_refptr<NV12Buffer> Create(int width, int height);
30   static rtc::scoped_refptr<NV12Buffer> Create(int width,
31                                                int height,
32                                                int stride_y,
33                                                int stride_uv);
34   static rtc::scoped_refptr<NV12Buffer> Copy(
35       const I420BufferInterface& i420_buffer);
36 
37   rtc::scoped_refptr<I420BufferInterface> ToI420() override;
38 
39   int width() const override;
40   int height() const override;
41 
42   int StrideY() const override;
43   int StrideUV() const override;
44 
45   const uint8_t* DataY() const override;
46   const uint8_t* DataUV() const override;
47 
48   uint8_t* MutableDataY();
49   uint8_t* MutableDataUV();
50 
51   // Sets all three planes to all zeros. Used to work around for
52   // quirks in memory checkers
53   // (https://bugs.chromium.org/p/libyuv/issues/detail?id=377) and
54   // ffmpeg (http://crbug.com/390941).
55   // TODO(https://crbug.com/390941): Deprecated. Should be deleted if/when those
56   // issues are resolved in a better way. Or in the mean time, use SetBlack.
57   void InitializeData();
58 
59   // Scale the cropped area of `src` to the size of `this` buffer, and
60   // write the result into `this`.
61   void CropAndScaleFrom(const NV12BufferInterface& src,
62                         int offset_x,
63                         int offset_y,
64                         int crop_width,
65                         int crop_height);
66 
67  protected:
68   NV12Buffer(int width, int height);
69   NV12Buffer(int width, int height, int stride_y, int stride_uv);
70 
71   ~NV12Buffer() override;
72 
73  private:
74   size_t UVOffset() const;
75 
76   const int width_;
77   const int height_;
78   const int stride_y_;
79   const int stride_uv_;
80   const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_;
81 };
82 
83 }  // namespace webrtc
84 
85 #endif  // API_VIDEO_NV12_BUFFER_H_
86