blob: 631e394e534f0b27479c1c6f6df61a051cc55f5b [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>
nisseaf916892017-01-10 07:44:26 -080015#include <memory>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "api/video/video_frame_buffer.h"
Karl Wiberg29e7bee2018-03-22 14:11:52 +010018#include "api/video/video_rotation.h"
19#include "rtc_base/memory/aligned_malloc.h"
Yves Gerey988cc082018-10-23 12:03:01 +020020#include "rtc_base/scoped_ref_ptr.h"
Mirko Bonadei977b46a2018-10-24 16:22:04 +020021#include "rtc_base/system/rtc_export.h"
nisseaf916892017-01-10 07:44:26 -080022
23namespace webrtc {
24
25// Plain I420 buffer in standard memory.
Mirko Bonadei977b46a2018-10-24 16:22:04 +020026class RTC_EXPORT I420Buffer : public I420BufferInterface {
nisseaf916892017-01-10 07:44:26 -080027 public:
28 static rtc::scoped_refptr<I420Buffer> Create(int width, int height);
29 static rtc::scoped_refptr<I420Buffer> Create(int width,
30 int height,
31 int stride_y,
32 int stride_u,
33 int stride_v);
34
35 // Create a new buffer and copy the pixel data.
magjed3f075492017-06-01 10:02:26 -070036 static rtc::scoped_refptr<I420Buffer> Copy(const I420BufferInterface& buffer);
37 // Deprecated.
38 static rtc::scoped_refptr<I420Buffer> Copy(const VideoFrameBuffer& buffer) {
39 return Copy(*buffer.GetI420());
40 }
nisseaf916892017-01-10 07:44:26 -080041
Yves Gerey665174f2018-06-19 15:03:05 +020042 static rtc::scoped_refptr<I420Buffer> Copy(int width,
43 int height,
44 const uint8_t* data_y,
45 int stride_y,
46 const uint8_t* data_u,
47 int stride_u,
48 const uint8_t* data_v,
49 int stride_v);
nisseaf916892017-01-10 07:44:26 -080050
51 // Returns a rotated copy of |src|.
magjed3f075492017-06-01 10:02:26 -070052 static rtc::scoped_refptr<I420Buffer> Rotate(const I420BufferInterface& src,
nisseaf916892017-01-10 07:44:26 -080053 VideoRotation rotation);
magjed3f075492017-06-01 10:02:26 -070054 // Deprecated.
55 static rtc::scoped_refptr<I420Buffer> Rotate(const VideoFrameBuffer& src,
56 VideoRotation rotation) {
57 return Rotate(*src.GetI420(), rotation);
58 }
nisseaf916892017-01-10 07:44:26 -080059
60 // Sets the buffer to all black.
61 static void SetBlack(I420Buffer* buffer);
62
63 // Sets all three planes to all zeros. Used to work around for
64 // quirks in memory checkers
65 // (https://bugs.chromium.org/p/libyuv/issues/detail?id=377) and
66 // ffmpeg (http://crbug.com/390941).
67 // TODO(nisse): Deprecated. Should be deleted if/when those issues
68 // are resolved in a better way. Or in the mean time, use SetBlack.
69 void InitializeData();
70
nisseaf916892017-01-10 07:44:26 -080071 int width() const override;
72 int height() const override;
73 const uint8_t* DataY() const override;
74 const uint8_t* DataU() const override;
75 const uint8_t* DataV() const override;
76
77 int StrideY() const override;
78 int StrideU() const override;
79 int StrideV() const override;
80
nisseaf916892017-01-10 07:44:26 -080081 uint8_t* MutableDataY();
82 uint8_t* MutableDataU();
83 uint8_t* MutableDataV();
84
85 // Scale the cropped area of |src| to the size of |this| buffer, and
86 // write the result into |this|.
magjed3f075492017-06-01 10:02:26 -070087 void CropAndScaleFrom(const I420BufferInterface& src,
nisseaf916892017-01-10 07:44:26 -080088 int offset_x,
89 int offset_y,
90 int crop_width,
91 int crop_height);
92
93 // The common case of a center crop, when needed to adjust the
94 // aspect ratio without distorting the image.
magjed3f075492017-06-01 10:02:26 -070095 void CropAndScaleFrom(const I420BufferInterface& src);
nisseaf916892017-01-10 07:44:26 -080096
97 // Scale all of |src| to the size of |this| buffer, with no cropping.
magjed3f075492017-06-01 10:02:26 -070098 void ScaleFrom(const I420BufferInterface& src);
nisseaf916892017-01-10 07:44:26 -080099
nisseaf916892017-01-10 07:44:26 -0800100 protected:
101 I420Buffer(int width, int height);
102 I420Buffer(int width, int height, int stride_y, int stride_u, int stride_v);
103
104 ~I420Buffer() override;
105
106 private:
107 const int width_;
108 const int height_;
109 const int stride_y_;
110 const int stride_u_;
111 const int stride_v_;
112 const std::unique_ptr<uint8_t, AlignedFreeDeleter> data_;
113};
114
115} // namespace webrtc
116
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200117#endif // API_VIDEO_I420_BUFFER_H_