• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * A raw memory block to use for internal buffers.
95  *
96  * TODO: replace this with C2LinearBlocks from a private C2BlockPool
97  */
98 struct MemoryBlock : public C2MemoryBlock<uint8_t> {
99     virtual const uint8_t* data() const override;
100     virtual size_t size() const override;
101 
dataMemoryBlock102     inline uint8_t *data() {
103         return const_cast<uint8_t*>(const_cast<const MemoryBlock*>(this)->data());
104     }
105 
106     // allocates an unmanaged block (not in a pool)
107     static MemoryBlock Allocate(size_t);
108 
109     // memory block with no actual memory (size is 0, data is null)
110     MemoryBlock();
111 
112     struct Impl;
113     MemoryBlock(std::shared_ptr<Impl> impl);
114     virtual ~MemoryBlock();
115 
116 private:
117     std::shared_ptr<Impl> mImpl;
118 };
119 
120 /**
121  * A raw memory mini-pool.
122  */
123 struct MemoryBlockPool {
124     /**
125      * Fetches a block with a given size.
126      *
127      * \param size size in bytes
128      */
129     MemoryBlock fetch(size_t size);
130 
131     MemoryBlockPool();
132     ~MemoryBlockPool() = default;
133 
134 private:
135     struct Impl;
136     std::shared_ptr<Impl> mImpl;
137 };
138 
139 } // namespace android
140 
141 #endif  // CODEC2_BUFFER_UTILS_H_
142