1 /*
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 
26 namespace 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  */
40 status_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  */
52 MediaImage2 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  */
63 MediaImage2 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  */
75 status_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  */
86 status_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  */
91 bool IsYUV420(const C2GraphicView &view);
92 
93 /**
94  * Returns true iff a view has a NV12 layout.
95  */
96 bool IsNV12(const C2GraphicView &view);
97 
98 /**
99  * Returns true iff a view has a I420 layout.
100  */
101 bool IsI420(const C2GraphicView &view);
102 
103 /**
104  * Returns true iff a MediaImage2 has a YUV 420 888 layout.
105  */
106 bool IsYUV420(const MediaImage2 *img);
107 
108 /**
109  * Returns true iff a MediaImage2 has a NV12 layout.
110  */
111 bool IsNV12(const MediaImage2 *img);
112 
113 /**
114  * Returns true iff a MediaImage2 has a I420 layout.
115  */
116 bool IsI420(const MediaImage2 *img);
117 
118 /**
119  * A raw memory block to use for internal buffers.
120  *
121  * TODO: replace this with C2LinearBlocks from a private C2BlockPool
122  */
123 struct MemoryBlock : public C2MemoryBlock<uint8_t> {
124     virtual const uint8_t* data() const override;
125     virtual size_t size() const override;
126 
dataMemoryBlock127     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 
141 private:
142     std::shared_ptr<Impl> mImpl;
143 };
144 
145 /**
146  * A raw memory mini-pool.
147  */
148 struct 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 
159 private:
160     struct Impl;
161     std::shared_ptr<Impl> mImpl;
162 };
163 
164 } // namespace android
165 
166 #endif  // CODEC2_BUFFER_UTILS_H_
167