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