blob: b337489657fcc5fd23086835aa8543a26f6c108c [file] [log] [blame]
nisseaf916892017-01-10 07:44:26 -08001/*
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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef API_VIDEO_I420_BUFFER_H_
12#define API_VIDEO_I420_BUFFER_H_
nisseaf916892017-01-10 07:44:26 -080013
Yves Gerey988cc082018-10-23 12:03:01 +020014#include <stdint.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020015
nisseaf916892017-01-10 07:44:26 -080016#include <memory>
17
Mirko Bonadeid9708072019-01-25 20:26:48 +010018#include "api/scoped_refptr.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/video/video_frame_buffer.h"
Karl Wiberg29e7bee2018-03-22 14:11:52 +010020#include "api/video/video_rotation.h"
21#include "rtc_base/memory/aligned_malloc.h"
Mirko Bonadei977b46a2018-10-24 16:22:04 +020022#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080023
24namespace webrtc {
25
26// Plain I420 buffer in standard memory.
Mirko Bonadei977b46a2018-10-24 16:22:04 +020027class RTC_EXPORT I420Buffer : public I420BufferInterface {
nisseaf916892017-01-10 07:44:26 -080028 public:
29 static rtc::scoped_refptr<I420Buffer> Create(int width, int height);
30 static rtc::scoped_refptr<I420Buffer> 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.
magjed3f075492017-06-01 10:02:26 -070037 static rtc::scoped_refptr<I420Buffer> Copy(const I420BufferInterface& buffer);
38 // Deprecated.
39 static rtc::scoped_refptr<I420Buffer> Copy(const VideoFrameBuffer& buffer) {
40 return Copy(*buffer.GetI420());
41 }
nisseaf916892017-01-10 07:44:26 -080042
Yves Gerey665174f2018-06-19 15:03:05 +020043 static rtc::scoped_refptr<I420Buffer> Copy(int width,
44 int height,
45 const uint8_t* data_y,
46 int stride_y,
47 const uint8_t* data_u,
48 int stride_u,
49 const uint8_t* data_v,
50 int stride_v);
nisseaf916892017-01-10 07:44:26 -080051
Artem Titov0e61fdd2021-07-25 21:50:14 +020052 // Returns a rotated copy of `src`.
magjed3f075492017-06-01 10:02:26 -070053 static rtc::scoped_refptr<I420Buffer> Rotate(const I420BufferInterface& src,
nisseaf916892017-01-10 07:44:26 -080054 VideoRotation rotation);
magjed3f075492017-06-01 10:02:26 -070055 // Deprecated.
56 static rtc::scoped_refptr<I420Buffer> Rotate(const VideoFrameBuffer& src,
57 VideoRotation rotation) {
58 return Rotate(*src.GetI420(), rotation);
59 }
nisseaf916892017-01-10 07:44:26 -080060
61 // Sets the buffer to all black.
62 static void SetBlack(I420Buffer* buffer);
63
64 // Sets all three planes to all zeros. Used to work around for
65 // quirks in memory checkers
66 // (https://bugs.chromium.org/p/libyuv/issues/detail?id=377) and
67 // ffmpeg (http://crbug.com/390941).
Niels Möller6939f632022-07-05 08:55:19 +020068 // TODO(https://crbug.com/390941): Deprecated. Should be deleted if/when those
69 // issues are resolved in a better way. Or in the mean time, use SetBlack.
nisseaf916892017-01-10 07:44:26 -080070 void InitializeData();
71
nisseaf916892017-01-10 07:44:26 -080072 int width() const override;
73 int height() const override;
74 const uint8_t* DataY() const override;
75 const uint8_t* DataU() const override;
76 const uint8_t* DataV() const override;
77
78 int StrideY() const override;
79 int StrideU() const override;
80 int StrideV() const override;
81
nisseaf916892017-01-10 07:44:26 -080082 uint8_t* MutableDataY();
83 uint8_t* MutableDataU();
84 uint8_t* MutableDataV();
85
Artem Titov0e61fdd2021-07-25 21:50:14 +020086 // Scale the cropped area of `src` to the size of `this` buffer, and
87 // write the result into `this`.
magjed3f075492017-06-01 10:02:26 -070088 void CropAndScaleFrom(const I420BufferInterface& src,
nisseaf916892017-01-10 07:44:26 -080089 int offset_x,
90 int offset_y,
91 int crop_width,
92 int crop_height);
93
94 // The common case of a center crop, when needed to adjust the
95 // aspect ratio without distorting the image.
magjed3f075492017-06-01 10:02:26 -070096 void CropAndScaleFrom(const I420BufferInterface& src);
nisseaf916892017-01-10 07:44:26 -080097
Artem Titov0e61fdd2021-07-25 21:50:14 +020098 // Scale all of `src` to the size of `this` buffer, with no cropping.
magjed3f075492017-06-01 10:02:26 -070099 void ScaleFrom(const I420BufferInterface& src);
nisseaf916892017-01-10 07:44:26 -0800100
nisseaf916892017-01-10 07:44:26 -0800101 protected:
102 I420Buffer(int width, int height);
103 I420Buffer(int width, int height, int stride_y, int stride_u, int stride_v);
104
105 ~I420Buffer() override;
106
107 private:
108 const int width_;
109 const int height_;
110 const int stride_y_;
111 const int stride_u_;
112 const int stride_v_;
113 const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_;
114};
115
116} // namespace webrtc
117
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200118#endif // API_VIDEO_I420_BUFFER_H_