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 NV12 layout. 97 */ 98 bool IsNV12(const C2GraphicView &view); 99 100 /** 101 * Returns true iff a view has a NV21 layout. 102 */ 103 bool IsNV21(const C2GraphicView &view); 104 105 /** 106 * Returns true iff a view has a I420 layout. 107 */ 108 bool IsI420(const C2GraphicView &view); 109 110 /** 111 * Returns true iff a MediaImage2 has a YUV 420 888 layout. 112 */ 113 bool IsYUV420(const MediaImage2 *img); 114 115 /** 116 * Returns true iff a MediaImage2 has a NV12 layout. 117 */ 118 bool IsNV12(const MediaImage2 *img); 119 120 /** 121 * Returns true iff a MediaImage2 has a NV21 layout. 122 */ 123 bool IsNV21(const MediaImage2 *img); 124 125 /** 126 * Returns true iff a MediaImage2 has a I420 layout. 127 */ 128 bool IsI420(const MediaImage2 *img); 129 130 enum FlexLayout { 131 FLEX_LAYOUT_UNKNOWN, 132 FLEX_LAYOUT_PLANAR, 133 FLEX_LAYOUT_SEMIPLANAR_UV, 134 FLEX_LAYOUT_SEMIPLANAR_VU, 135 }; 136 /** 137 * Returns layout of YCBCR_420_888 pixel format. 138 */ 139 FlexLayout GetYuv420FlexibleLayout(); 140 141 /** 142 * A raw memory block to use for internal buffers. 143 * 144 * TODO: replace this with C2LinearBlocks from a private C2BlockPool 145 */ 146 struct MemoryBlock : public C2MemoryBlock<uint8_t> { 147 virtual const uint8_t* data() const override; 148 virtual size_t size() const override; 149 dataMemoryBlock150 inline uint8_t *data() { 151 return const_cast<uint8_t*>(const_cast<const MemoryBlock*>(this)->data()); 152 } 153 154 // allocates an unmanaged block (not in a pool) 155 static MemoryBlock Allocate(size_t); 156 157 // memory block with no actual memory (size is 0, data is null) 158 MemoryBlock(); 159 160 struct Impl; 161 MemoryBlock(std::shared_ptr<Impl> impl); 162 virtual ~MemoryBlock(); 163 164 private: 165 std::shared_ptr<Impl> mImpl; 166 }; 167 168 /** 169 * A raw memory mini-pool. 170 */ 171 struct MemoryBlockPool { 172 /** 173 * Fetches a block with a given size. 174 * 175 * \param size size in bytes 176 */ 177 MemoryBlock fetch(size_t size); 178 179 MemoryBlockPool(); 180 ~MemoryBlockPool() = default; 181 182 private: 183 struct Impl; 184 std::shared_ptr<Impl> mImpl; 185 }; 186 187 } // namespace android 188 189 #endif // CODEC2_BUFFER_UTILS_H_ 190