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 <C2Config.h> 22 #include <C2ParamDef.h> 23 24 #include <media/hardware/VideoAPI.h> 25 #include <utils/Errors.h> 26 27 namespace 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 */ 41 status_t ConvertRGBToPlanarYUV( 42 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize, 43 const C2GraphicView &src, C2Color::matrix_t colorMatrix = C2Color::MATRIX_BT601, 44 C2Color::range_t colorRange = C2Color::RANGE_LIMITED); 45 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 */ 54 MediaImage2 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 */ 65 MediaImage2 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 */ 77 status_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 */ 88 status_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 */ 93 bool IsYUV420(const C2GraphicView &view); 94 95 /** 96 * Returns true iff a view has a YUV 420 10-10-10 layout. 97 */ 98 bool IsYUV420_10bit(const C2GraphicView &view); 99 100 /** 101 * Returns true iff a view has a NV12 layout. 102 */ 103 bool IsNV12(const C2GraphicView &view); 104 105 /** 106 * Returns true iff a view has a P010 layout. 107 */ 108 bool IsP010(const C2GraphicView &view); 109 110 /** 111 * Returns true iff a view has a NV21 layout. 112 */ 113 bool IsNV21(const C2GraphicView &view); 114 115 /** 116 * Returns true iff a view has a I420 layout. 117 */ 118 bool IsI420(const C2GraphicView &view); 119 120 /** 121 * Returns true iff a MediaImage2 has a YUV 420 888 layout. 122 */ 123 bool IsYUV420(const MediaImage2 *img); 124 125 /** 126 * Returns true iff a MediaImage2 has a NV12 layout. 127 */ 128 bool IsNV12(const MediaImage2 *img); 129 130 /** 131 * Returns true iff a MediaImage2 has a NV21 layout. 132 */ 133 bool IsNV21(const MediaImage2 *img); 134 135 /** 136 * Returns true iff a MediaImage2 has a I420 layout. 137 */ 138 bool IsI420(const MediaImage2 *img); 139 140 enum 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 */ 149 FlexLayout GetYuv420FlexibleLayout(); 150 151 /** 152 * A raw memory block to use for internal buffers. 153 * 154 * TODO: replace this with C2LinearBlocks from a private C2BlockPool 155 */ 156 struct MemoryBlock : public C2MemoryBlock<uint8_t> { 157 virtual const uint8_t* data() const override; 158 virtual size_t size() const override; 159 dataMemoryBlock160 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 174 private: 175 std::shared_ptr<Impl> mImpl; 176 }; 177 178 /** 179 * A raw memory mini-pool. 180 */ 181 struct 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 192 private: 193 struct Impl; 194 std::shared_ptr<Impl> mImpl; 195 }; 196 197 } // namespace android 198 199 #endif // CODEC2_BUFFER_UTILS_H_ 200