blob: b7519da389cd427ea95157df0c66baa30c169606 [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//#define LOG_NDEBUG 0
18#define LOG_TAG "Codec2BufferUtils"
19#include <utils/Log.h>
20
21#include <list>
22#include <mutex>
23
24#include <media/hardware/HardwareAPI.h>
25#include <media/stagefright/foundation/AUtils.h>
26
27#include <C2Debug.h>
28
29#include "Codec2BufferUtils.h"
30
31namespace android {
32
33namespace {
34
35/**
36 * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) do not work as the results are
37 * always const.
38 */
39template<bool ToA, size_t S>
40struct MemCopier {
41 template<typename A, typename B>
42 inline static void copy(A *a, const B *b, size_t size) {
43 __builtin_memcpy(a, b, size);
44 }
45};
46
47template<size_t S>
48struct MemCopier<false, S> {
49 template<typename A, typename B>
50 inline static void copy(const A *a, B *b, size_t size) {
51 MemCopier<true, S>::copy(b, a, size);
52 }
53};
54
55/**
56 * Copies between a MediaImage and a graphic view.
57 *
58 * \param ToMediaImage whether to copy to (or from) the MediaImage
59 * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction)
60 * \param img MediaImage data
61 * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction)
62 */
63template<bool ToMediaImage, typename View, typename ImagePixel>
64static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
65 // TODO: more efficient copying --- e.g. one row at a time, copying
66 // interleaved planes together, etc.
67 const C2PlanarLayout &layout = view.layout();
68 const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
69 if (view.width() != img->mWidth
70 || view.height() != img->mHeight) {
71 return BAD_VALUE;
72 }
73 for (uint32_t i = 0; i < layout.numPlanes; ++i) {
74 typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow =
75 imgBase + img->mPlane[i].mOffset;
76 typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow =
77 viewRow = view.data()[i];
78 const C2PlaneInfo &plane = layout.planes[i];
79 if (plane.colSampling != img->mPlane[i].mHorizSubsampling
80 || plane.rowSampling != img->mPlane[i].mVertSubsampling
81 || plane.allocatedDepth != img->mBitDepthAllocated
82 || plane.allocatedDepth < plane.bitDepth
83 // MediaImage only supports MSB values
84 || plane.rightShift != plane.allocatedDepth - plane.bitDepth
85 || (bpp > 1 && plane.endianness != plane.NATIVE)) {
86 return BAD_VALUE;
87 }
88
89 uint32_t planeW = img->mWidth / plane.colSampling;
90 uint32_t planeH = img->mHeight / plane.rowSampling;
91 for (uint32_t row = 0; row < planeH; ++row) {
92 decltype(imgRow) imgPtr = imgRow;
93 decltype(viewRow) viewPtr = viewRow;
94 for (uint32_t col = 0; col < planeW; ++col) {
95 MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp);
96 imgPtr += img->mPlane[i].mColInc;
97 viewPtr += plane.colInc;
98 }
99 imgRow += img->mPlane[i].mRowInc;
100 viewRow += plane.rowInc;
101 }
102 }
103 return OK;
104}
105
106} // namespace
107
108status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
109 return _ImageCopy<true>(view, img, imgBase);
110}
111
112status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
113 return _ImageCopy<false>(view, img, imgBase);
114}
115
116bool IsYUV420(const C2GraphicView &view) {
117 const C2PlanarLayout &layout = view.layout();
118 return (layout.numPlanes == 3
119 && layout.type == C2PlanarLayout::TYPE_YUV
120 && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
121 && layout.planes[layout.PLANE_Y].allocatedDepth == 8
122 && layout.planes[layout.PLANE_Y].bitDepth == 8
123 && layout.planes[layout.PLANE_Y].rightShift == 0
124 && layout.planes[layout.PLANE_Y].colSampling == 1
125 && layout.planes[layout.PLANE_Y].rowSampling == 1
126 && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
127 && layout.planes[layout.PLANE_U].allocatedDepth == 8
128 && layout.planes[layout.PLANE_U].bitDepth == 8
129 && layout.planes[layout.PLANE_U].rightShift == 0
130 && layout.planes[layout.PLANE_U].colSampling == 2
131 && layout.planes[layout.PLANE_U].rowSampling == 2
132 && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
133 && layout.planes[layout.PLANE_V].allocatedDepth == 8
134 && layout.planes[layout.PLANE_V].bitDepth == 8
135 && layout.planes[layout.PLANE_V].rightShift == 0
136 && layout.planes[layout.PLANE_V].colSampling == 2
137 && layout.planes[layout.PLANE_V].rowSampling == 2);
138}
139
140MediaImage2 CreateYUV420PlanarMediaImage2(
141 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
142 return MediaImage2 {
143 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
144 .mNumPlanes = 3,
145 .mWidth = width,
146 .mHeight = height,
147 .mBitDepth = 8,
148 .mBitDepthAllocated = 8,
149 .mPlane = {
150 {
151 .mOffset = 0,
152 .mColInc = 1,
153 .mRowInc = (int32_t)stride,
154 .mHorizSubsampling = 1,
155 .mVertSubsampling = 1,
156 },
157 {
158 .mOffset = stride * vstride,
159 .mColInc = 1,
160 .mRowInc = (int32_t)stride / 2,
161 .mHorizSubsampling = 2,
162 .mVertSubsampling = 2,
163 },
164 {
165 .mOffset = stride * vstride * 5 / 4,
166 .mColInc = 1,
167 .mRowInc = (int32_t)stride / 2,
168 .mHorizSubsampling = 2,
169 .mVertSubsampling = 2,
170 }
171 },
172 };
173}
174
175MediaImage2 CreateYUV420SemiPlanarMediaImage2(
176 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
177 return MediaImage2 {
178 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
179 .mNumPlanes = 3,
180 .mWidth = width,
181 .mHeight = height,
182 .mBitDepth = 8,
183 .mBitDepthAllocated = 8,
184 .mPlane = {
185 {
186 .mOffset = 0,
187 .mColInc = 1,
188 .mRowInc = (int32_t)stride,
189 .mHorizSubsampling = 1,
190 .mVertSubsampling = 1,
191 },
192 {
193 .mOffset = stride * vstride,
194 .mColInc = 2,
195 .mRowInc = (int32_t)stride,
196 .mHorizSubsampling = 2,
197 .mVertSubsampling = 2,
198 },
199 {
200 .mOffset = stride * vstride + 1,
201 .mColInc = 2,
202 .mRowInc = (int32_t)stride,
203 .mHorizSubsampling = 2,
204 .mVertSubsampling = 2,
205 }
206 },
207 };
208}
209
210status_t ConvertRGBToPlanarYUV(
211 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
212 const C2GraphicView &src) {
213 CHECK(dstY != nullptr);
214 CHECK((src.width() & 1) == 0);
215 CHECK((src.height() & 1) == 0);
216
217 if (dstStride * dstVStride * 3 / 2 > bufferSize) {
218 ALOGD("conversion buffer is too small for converting from RGB to YUV");
219 return NO_MEMORY;
220 }
221
222 uint8_t *dstU = dstY + dstStride * dstVStride;
223 uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
224
225 const C2PlanarLayout &layout = src.layout();
226 const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R];
227 const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
228 const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B];
229
230#define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
231 for (size_t y = 0; y < src.height(); ++y) {
232 for (size_t x = 0; x < src.width(); ++x) {
233 uint8_t red = *pRed;
234 uint8_t green = *pGreen;
235 uint8_t blue = *pBlue;
236
237 // using ITU-R BT.601 conversion matrix
238 unsigned luma =
239 CLIP3(0, (((red * 66 + green * 129 + blue * 25) >> 8) + 16), 255);
240
241 dstY[x] = luma;
242
243 if ((x & 1) == 0 && (y & 1) == 0) {
244 unsigned U =
245 CLIP3(0, (((-red * 38 - green * 74 + blue * 112) >> 8) + 128), 255);
246
247 unsigned V =
248 CLIP3(0, (((red * 112 - green * 94 - blue * 18) >> 8) + 128), 255);
249
250 dstU[x >> 1] = U;
251 dstV[x >> 1] = V;
252 }
253 pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc;
254 pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
255 pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc;
256 }
257
258 if ((y & 1) == 0) {
259 dstU += dstStride >> 1;
260 dstV += dstStride >> 1;
261 }
262
263 pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
264 pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
265 pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
266 pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
267 pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
268 pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
269
270 dstY += dstStride;
271 }
272 return OK;
273}
274
275namespace {
276
277/**
278 * A block of raw allocated memory.
279 */
280struct MemoryBlockPoolBlock {
281 MemoryBlockPoolBlock(size_t size)
282 : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
283
284 ~MemoryBlockPoolBlock() {
285 delete[] mData;
286 }
287
288 const uint8_t *data() const {
289 return mData;
290 }
291
292 size_t size() const {
293 return mSize;
294 }
295
296 C2_DO_NOT_COPY(MemoryBlockPoolBlock);
297
298private:
299 uint8_t *mData;
300 size_t mSize;
301};
302
303/**
304 * A simple raw memory block pool implementation.
305 */
306struct MemoryBlockPoolImpl {
307 void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
308 std::lock_guard<std::mutex> lock(mMutex);
309 // return block to free blocks if it is the current size; otherwise, discard
310 if (block->size() == mCurrentSize) {
311 mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
312 } else {
313 mBlocksInUse.erase(block);
314 }
315 }
316
317 std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
318 std::lock_guard<std::mutex> lock(mMutex);
319 mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
320 return block.size() != size;
321 });
322 mCurrentSize = size;
323 if (mFreeBlocks.empty()) {
324 mBlocksInUse.emplace_front(size);
325 } else {
326 mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
327 }
328 return mBlocksInUse.begin();
329 }
330
331 MemoryBlockPoolImpl() = default;
332
333 C2_DO_NOT_COPY(MemoryBlockPoolImpl);
334
335private:
336 std::mutex mMutex;
337 std::list<MemoryBlockPoolBlock> mFreeBlocks;
338 std::list<MemoryBlockPoolBlock> mBlocksInUse;
339 size_t mCurrentSize;
340};
341
342} // namespace
343
344struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
345};
346
347struct MemoryBlock::Impl {
348 Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
349 std::shared_ptr<MemoryBlockPoolImpl> pool)
350 : mBlock(block), mPool(pool) {
351 }
352
353 ~Impl() {
354 mPool->release(mBlock);
355 }
356
357 const uint8_t *data() const {
358 return mBlock->data();
359 }
360
361 size_t size() const {
362 return mBlock->size();
363 }
364
365private:
366 std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
367 std::shared_ptr<MemoryBlockPoolImpl> mPool;
368};
369
370MemoryBlock MemoryBlockPool::fetch(size_t size) {
371 std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
372 return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
373 poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
374}
375
376MemoryBlockPool::MemoryBlockPool()
377 : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
378}
379
380MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
381 : mImpl(impl) {
382}
383
384MemoryBlock::MemoryBlock() = default;
385
386MemoryBlock::~MemoryBlock() = default;
387
388const uint8_t* MemoryBlock::data() const {
389 return mImpl ? mImpl->data() : nullptr;
390}
391
392size_t MemoryBlock::size() const {
393 return mImpl ? mImpl->size() : 0;
394}
395
396MemoryBlock MemoryBlock::Allocate(size_t size) {
397 return MemoryBlockPool().fetch(size);
398}
399
400} // namespace android