blob: 039f767e106d25aca2372ed2342e614a2ce10aa1 [file] [log] [blame]
Evan Shrubsole84995432020-09-09 16:14:19 +02001/*
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
22namespace 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.
27class 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);
Evan Shrubsole55c17862020-09-28 10:16:00 +020034 static rtc::scoped_refptr<NV12Buffer> Copy(
35 const I420BufferInterface& i420_buffer);
Evan Shrubsole84995432020-09-09 16:14:19 +020036
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
Ilya Nikolaevskiy4c87d832020-09-18 15:18:54 +020051 // 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(nisse): Deprecated. Should be deleted if/when those issues
56 // are resolved in a better way. Or in the mean time, use SetBlack.
57 void InitializeData();
58
Evan Shrubsole84995432020-09-09 16:14:19 +020059 protected:
60 NV12Buffer(int width, int height);
61 NV12Buffer(int width, int height, int stride_y, int stride_uv);
62
63 ~NV12Buffer() override;
64
65 private:
66 size_t UVOffset() const;
67
68 const int width_;
69 const int height_;
70 const int stride_y_;
71 const int stride_uv_;
72 const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_;
73};
74
75} // namespace webrtc
76
77#endif // API_VIDEO_NV12_BUFFER_H_