blob: 6b0ba7f7eea0700fcc72894bc1861b58de3f276e [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright 2018, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef CODEC2_BUFFER_UTILS_H_
18#define CODEC2_BUFFER_UTILS_H_
19
20#include <C2Buffer.h>
Manisha Jajoo478423c2021-05-20 21:28:14 +053021#include <C2Config.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080022#include <C2ParamDef.h>
23
24#include <media/hardware/VideoAPI.h>
25#include <utils/Errors.h>
26
27namespace android {
28
29/**
30 * Converts an RGB view to planar YUV 420 media image.
31 *
32 * \param dstY pointer to media image buffer
33 * \param dstStride stride in bytes
34 * \param dstVStride vertical stride in pixels
35 * \param bufferSize media image buffer size
36 * \param src source image
37 *
38 * \retval NO_MEMORY media image is too small
39 * \retval OK on success
40 */
41status_t ConvertRGBToPlanarYUV(
42 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
Manisha Jajoo478423c2021-05-20 21:28:14 +053043 const C2GraphicView &src, C2Color::matrix_t colorMatrix = C2Color::MATRIX_BT601,
44 C2Color::range_t colorRange = C2Color::RANGE_LIMITED);
Pawin Vongmasa36653902018-11-15 00:10:25 -080045
46/**
47 * Returns a planar YUV 420 8-bit media image descriptor.
48 *
49 * \param width width of image in pixels
50 * \param height height of image in pixels
51 * \param stride stride of image in pixels
52 * \param vstride vertical stride of image in pixels
53 */
54MediaImage2 CreateYUV420PlanarMediaImage2(
55 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride);
56
57/**
58 * Returns a semiplanar YUV 420 8-bit media image descriptor.
59 *
60 * \param width width of image in pixels
61 * \param height height of image in pixels
62 * \param stride stride of image in pixels
63 * \param vstride vertical stride of image in pixels
64 */
65MediaImage2 CreateYUV420SemiPlanarMediaImage2(
66 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride);
67
68/**
69 * Copies a graphic view into a media image.
70 *
71 * \param imgBase base of MediaImage
72 * \param img MediaImage data
73 * \param view graphic view
74 *
75 * \return OK on success
76 */
77status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view);
78
79/**
80 * Copies a media image into a graphic view.
81 *
82 * \param view graphic view
83 * \param imgBase base of MediaImage
84 * \param img MediaImage data
85 *
86 * \return OK on success
87 */
88status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img);
89
90/**
91 * Returns true iff a view has a YUV 420 888 layout.
92 */
93bool IsYUV420(const C2GraphicView &view);
94
95/**
Fyodor Kyslovd97e9912022-11-07 19:23:21 +000096 * Returns true iff a view has a YUV 420 10-10-10 layout.
97 */
98bool IsYUV420_10bit(const C2GraphicView &view);
99
100/**
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800101 * Returns true iff a view has a NV12 layout.
102 */
103bool IsNV12(const C2GraphicView &view);
104
105/**
Fyodor Kyslovd97e9912022-11-07 19:23:21 +0000106 * Returns true iff a view has a P010 layout.
107 */
108bool IsP010(const C2GraphicView &view);
109
110/**
Wonsik Kima38fdf22021-04-05 14:50:29 -0700111 * Returns true iff a view has a NV21 layout.
112 */
113bool IsNV21(const C2GraphicView &view);
114
115/**
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800116 * Returns true iff a view has a I420 layout.
117 */
118bool IsI420(const C2GraphicView &view);
119
120/**
121 * Returns true iff a MediaImage2 has a YUV 420 888 layout.
122 */
123bool IsYUV420(const MediaImage2 *img);
124
125/**
126 * Returns true iff a MediaImage2 has a NV12 layout.
127 */
128bool IsNV12(const MediaImage2 *img);
129
130/**
Wonsik Kima38fdf22021-04-05 14:50:29 -0700131 * Returns true iff a MediaImage2 has a NV21 layout.
132 */
133bool IsNV21(const MediaImage2 *img);
134
135/**
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800136 * Returns true iff a MediaImage2 has a I420 layout.
137 */
138bool IsI420(const MediaImage2 *img);
139
Wonsik Kima38fdf22021-04-05 14:50:29 -0700140enum FlexLayout {
141 FLEX_LAYOUT_UNKNOWN,
142 FLEX_LAYOUT_PLANAR,
143 FLEX_LAYOUT_SEMIPLANAR_UV,
144 FLEX_LAYOUT_SEMIPLANAR_VU,
145};
146/**
147 * Returns layout of YCBCR_420_888 pixel format.
148 */
149FlexLayout GetYuv420FlexibleLayout();
150
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800151/**
Pawin Vongmasa36653902018-11-15 00:10:25 -0800152 * A raw memory block to use for internal buffers.
153 *
154 * TODO: replace this with C2LinearBlocks from a private C2BlockPool
155 */
156struct MemoryBlock : public C2MemoryBlock<uint8_t> {
157 virtual const uint8_t* data() const override;
158 virtual size_t size() const override;
159
160 inline uint8_t *data() {
161 return const_cast<uint8_t*>(const_cast<const MemoryBlock*>(this)->data());
162 }
163
164 // allocates an unmanaged block (not in a pool)
165 static MemoryBlock Allocate(size_t);
166
167 // memory block with no actual memory (size is 0, data is null)
168 MemoryBlock();
169
170 struct Impl;
171 MemoryBlock(std::shared_ptr<Impl> impl);
172 virtual ~MemoryBlock();
173
174private:
175 std::shared_ptr<Impl> mImpl;
176};
177
178/**
179 * A raw memory mini-pool.
180 */
181struct MemoryBlockPool {
182 /**
183 * Fetches a block with a given size.
184 *
185 * \param size size in bytes
186 */
187 MemoryBlock fetch(size_t size);
188
189 MemoryBlockPool();
190 ~MemoryBlockPool() = default;
191
192private:
193 struct Impl;
194 std::shared_ptr<Impl> mImpl;
195};
196
197} // namespace android
198
199#endif // CODEC2_BUFFER_UTILS_H_