blob: afadf006c8a433ab636552ff7ef37de3aa01e66a [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>
21#include <C2ParamDef.h>
22
23#include <media/hardware/VideoAPI.h>
24#include <utils/Errors.h>
25
26namespace android {
27
28/**
29 * Converts an RGB view to planar YUV 420 media image.
30 *
31 * \param dstY pointer to media image buffer
32 * \param dstStride stride in bytes
33 * \param dstVStride vertical stride in pixels
34 * \param bufferSize media image buffer size
35 * \param src source image
36 *
37 * \retval NO_MEMORY media image is too small
38 * \retval OK on success
39 */
40status_t ConvertRGBToPlanarYUV(
41 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
42 const C2GraphicView &src);
43
44/**
45 * Returns a planar YUV 420 8-bit media image descriptor.
46 *
47 * \param width width of image in pixels
48 * \param height height of image in pixels
49 * \param stride stride of image in pixels
50 * \param vstride vertical stride of image in pixels
51 */
52MediaImage2 CreateYUV420PlanarMediaImage2(
53 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride);
54
55/**
56 * Returns a semiplanar YUV 420 8-bit media image descriptor.
57 *
58 * \param width width of image in pixels
59 * \param height height of image in pixels
60 * \param stride stride of image in pixels
61 * \param vstride vertical stride of image in pixels
62 */
63MediaImage2 CreateYUV420SemiPlanarMediaImage2(
64 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride);
65
66/**
67 * Copies a graphic view into a media image.
68 *
69 * \param imgBase base of MediaImage
70 * \param img MediaImage data
71 * \param view graphic view
72 *
73 * \return OK on success
74 */
75status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view);
76
77/**
78 * Copies a media image into a graphic view.
79 *
80 * \param view graphic view
81 * \param imgBase base of MediaImage
82 * \param img MediaImage data
83 *
84 * \return OK on success
85 */
86status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img);
87
88/**
89 * Returns true iff a view has a YUV 420 888 layout.
90 */
91bool IsYUV420(const C2GraphicView &view);
92
93/**
Pawin Vongmasa1f213362019-01-24 06:59:16 -080094 * Returns true iff a view has a NV12 layout.
95 */
96bool IsNV12(const C2GraphicView &view);
97
98/**
99 * Returns true iff a view has a I420 layout.
100 */
101bool IsI420(const C2GraphicView &view);
102
103/**
104 * Returns true iff a MediaImage2 has a YUV 420 888 layout.
105 */
106bool IsYUV420(const MediaImage2 *img);
107
108/**
109 * Returns true iff a MediaImage2 has a NV12 layout.
110 */
111bool IsNV12(const MediaImage2 *img);
112
113/**
114 * Returns true iff a MediaImage2 has a I420 layout.
115 */
116bool IsI420(const MediaImage2 *img);
117
118/**
Pawin Vongmasa36653902018-11-15 00:10:25 -0800119 * A raw memory block to use for internal buffers.
120 *
121 * TODO: replace this with C2LinearBlocks from a private C2BlockPool
122 */
123struct MemoryBlock : public C2MemoryBlock<uint8_t> {
124 virtual const uint8_t* data() const override;
125 virtual size_t size() const override;
126
127 inline uint8_t *data() {
128 return const_cast<uint8_t*>(const_cast<const MemoryBlock*>(this)->data());
129 }
130
131 // allocates an unmanaged block (not in a pool)
132 static MemoryBlock Allocate(size_t);
133
134 // memory block with no actual memory (size is 0, data is null)
135 MemoryBlock();
136
137 struct Impl;
138 MemoryBlock(std::shared_ptr<Impl> impl);
139 virtual ~MemoryBlock();
140
141private:
142 std::shared_ptr<Impl> mImpl;
143};
144
145/**
146 * A raw memory mini-pool.
147 */
148struct MemoryBlockPool {
149 /**
150 * Fetches a block with a given size.
151 *
152 * \param size size in bytes
153 */
154 MemoryBlock fetch(size_t size);
155
156 MemoryBlockPool();
157 ~MemoryBlockPool() = default;
158
159private:
160 struct Impl;
161 std::shared_ptr<Impl> mImpl;
162};
163
164} // namespace android
165
166#endif // CODEC2_BUFFER_UTILS_H_