blob: 84d22a377a17973dc4adb320ec40b1c337783e73 [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;
Pawin Vongmasa8be93112018-12-11 14:01:42 -080091
92 bool canCopyByRow = (plane.colInc == 1) && (img->mPlane[i].mColInc == 1);
93 bool canCopyByPlane = canCopyByRow && (plane.rowInc == img->mPlane[i].mRowInc);
94 if (canCopyByPlane) {
95 MemCopier<ToMediaImage, 0>::copy(imgRow, viewRow, plane.rowInc * planeH);
96 } else if (canCopyByRow) {
97 for (uint32_t row = 0; row < planeH; ++row) {
98 MemCopier<ToMediaImage, 0>::copy(
99 imgRow, viewRow, std::min(plane.rowInc, img->mPlane[i].mRowInc));
100 imgRow += img->mPlane[i].mRowInc;
101 viewRow += plane.rowInc;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800102 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800103 } else {
104 for (uint32_t row = 0; row < planeH; ++row) {
105 decltype(imgRow) imgPtr = imgRow;
106 decltype(viewRow) viewPtr = viewRow;
107 for (uint32_t col = 0; col < planeW; ++col) {
108 MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp);
109 imgPtr += img->mPlane[i].mColInc;
110 viewPtr += plane.colInc;
111 }
112 imgRow += img->mPlane[i].mRowInc;
113 viewRow += plane.rowInc;
114 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800115 }
116 }
117 return OK;
118}
119
120} // namespace
121
122status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
123 return _ImageCopy<true>(view, img, imgBase);
124}
125
126status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
127 return _ImageCopy<false>(view, img, imgBase);
128}
129
130bool IsYUV420(const C2GraphicView &view) {
131 const C2PlanarLayout &layout = view.layout();
132 return (layout.numPlanes == 3
133 && layout.type == C2PlanarLayout::TYPE_YUV
134 && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
135 && layout.planes[layout.PLANE_Y].allocatedDepth == 8
136 && layout.planes[layout.PLANE_Y].bitDepth == 8
137 && layout.planes[layout.PLANE_Y].rightShift == 0
138 && layout.planes[layout.PLANE_Y].colSampling == 1
139 && layout.planes[layout.PLANE_Y].rowSampling == 1
140 && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
141 && layout.planes[layout.PLANE_U].allocatedDepth == 8
142 && layout.planes[layout.PLANE_U].bitDepth == 8
143 && layout.planes[layout.PLANE_U].rightShift == 0
144 && layout.planes[layout.PLANE_U].colSampling == 2
145 && layout.planes[layout.PLANE_U].rowSampling == 2
146 && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
147 && layout.planes[layout.PLANE_V].allocatedDepth == 8
148 && layout.planes[layout.PLANE_V].bitDepth == 8
149 && layout.planes[layout.PLANE_V].rightShift == 0
150 && layout.planes[layout.PLANE_V].colSampling == 2
151 && layout.planes[layout.PLANE_V].rowSampling == 2);
152}
153
154MediaImage2 CreateYUV420PlanarMediaImage2(
155 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
156 return MediaImage2 {
157 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
158 .mNumPlanes = 3,
159 .mWidth = width,
160 .mHeight = height,
161 .mBitDepth = 8,
162 .mBitDepthAllocated = 8,
163 .mPlane = {
164 {
165 .mOffset = 0,
166 .mColInc = 1,
167 .mRowInc = (int32_t)stride,
168 .mHorizSubsampling = 1,
169 .mVertSubsampling = 1,
170 },
171 {
172 .mOffset = stride * vstride,
173 .mColInc = 1,
174 .mRowInc = (int32_t)stride / 2,
175 .mHorizSubsampling = 2,
176 .mVertSubsampling = 2,
177 },
178 {
179 .mOffset = stride * vstride * 5 / 4,
180 .mColInc = 1,
181 .mRowInc = (int32_t)stride / 2,
182 .mHorizSubsampling = 2,
183 .mVertSubsampling = 2,
184 }
185 },
186 };
187}
188
189MediaImage2 CreateYUV420SemiPlanarMediaImage2(
190 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
191 return MediaImage2 {
192 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
193 .mNumPlanes = 3,
194 .mWidth = width,
195 .mHeight = height,
196 .mBitDepth = 8,
197 .mBitDepthAllocated = 8,
198 .mPlane = {
199 {
200 .mOffset = 0,
201 .mColInc = 1,
202 .mRowInc = (int32_t)stride,
203 .mHorizSubsampling = 1,
204 .mVertSubsampling = 1,
205 },
206 {
207 .mOffset = stride * vstride,
208 .mColInc = 2,
209 .mRowInc = (int32_t)stride,
210 .mHorizSubsampling = 2,
211 .mVertSubsampling = 2,
212 },
213 {
214 .mOffset = stride * vstride + 1,
215 .mColInc = 2,
216 .mRowInc = (int32_t)stride,
217 .mHorizSubsampling = 2,
218 .mVertSubsampling = 2,
219 }
220 },
221 };
222}
223
224status_t ConvertRGBToPlanarYUV(
225 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
226 const C2GraphicView &src) {
227 CHECK(dstY != nullptr);
228 CHECK((src.width() & 1) == 0);
229 CHECK((src.height() & 1) == 0);
230
231 if (dstStride * dstVStride * 3 / 2 > bufferSize) {
232 ALOGD("conversion buffer is too small for converting from RGB to YUV");
233 return NO_MEMORY;
234 }
235
236 uint8_t *dstU = dstY + dstStride * dstVStride;
237 uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
238
239 const C2PlanarLayout &layout = src.layout();
240 const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R];
241 const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
242 const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B];
243
244#define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
245 for (size_t y = 0; y < src.height(); ++y) {
246 for (size_t x = 0; x < src.width(); ++x) {
247 uint8_t red = *pRed;
248 uint8_t green = *pGreen;
249 uint8_t blue = *pBlue;
250
251 // using ITU-R BT.601 conversion matrix
252 unsigned luma =
253 CLIP3(0, (((red * 66 + green * 129 + blue * 25) >> 8) + 16), 255);
254
255 dstY[x] = luma;
256
257 if ((x & 1) == 0 && (y & 1) == 0) {
258 unsigned U =
259 CLIP3(0, (((-red * 38 - green * 74 + blue * 112) >> 8) + 128), 255);
260
261 unsigned V =
262 CLIP3(0, (((red * 112 - green * 94 - blue * 18) >> 8) + 128), 255);
263
264 dstU[x >> 1] = U;
265 dstV[x >> 1] = V;
266 }
267 pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc;
268 pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
269 pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc;
270 }
271
272 if ((y & 1) == 0) {
273 dstU += dstStride >> 1;
274 dstV += dstStride >> 1;
275 }
276
277 pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
278 pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
279 pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
280 pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
281 pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
282 pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
283
284 dstY += dstStride;
285 }
286 return OK;
287}
288
289namespace {
290
291/**
292 * A block of raw allocated memory.
293 */
294struct MemoryBlockPoolBlock {
295 MemoryBlockPoolBlock(size_t size)
296 : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
297
298 ~MemoryBlockPoolBlock() {
299 delete[] mData;
300 }
301
302 const uint8_t *data() const {
303 return mData;
304 }
305
306 size_t size() const {
307 return mSize;
308 }
309
310 C2_DO_NOT_COPY(MemoryBlockPoolBlock);
311
312private:
313 uint8_t *mData;
314 size_t mSize;
315};
316
317/**
318 * A simple raw memory block pool implementation.
319 */
320struct MemoryBlockPoolImpl {
321 void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
322 std::lock_guard<std::mutex> lock(mMutex);
323 // return block to free blocks if it is the current size; otherwise, discard
324 if (block->size() == mCurrentSize) {
325 mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
326 } else {
327 mBlocksInUse.erase(block);
328 }
329 }
330
331 std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
332 std::lock_guard<std::mutex> lock(mMutex);
333 mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
334 return block.size() != size;
335 });
336 mCurrentSize = size;
337 if (mFreeBlocks.empty()) {
338 mBlocksInUse.emplace_front(size);
339 } else {
340 mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
341 }
342 return mBlocksInUse.begin();
343 }
344
345 MemoryBlockPoolImpl() = default;
346
347 C2_DO_NOT_COPY(MemoryBlockPoolImpl);
348
349private:
350 std::mutex mMutex;
351 std::list<MemoryBlockPoolBlock> mFreeBlocks;
352 std::list<MemoryBlockPoolBlock> mBlocksInUse;
353 size_t mCurrentSize;
354};
355
356} // namespace
357
358struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
359};
360
361struct MemoryBlock::Impl {
362 Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
363 std::shared_ptr<MemoryBlockPoolImpl> pool)
364 : mBlock(block), mPool(pool) {
365 }
366
367 ~Impl() {
368 mPool->release(mBlock);
369 }
370
371 const uint8_t *data() const {
372 return mBlock->data();
373 }
374
375 size_t size() const {
376 return mBlock->size();
377 }
378
379private:
380 std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
381 std::shared_ptr<MemoryBlockPoolImpl> mPool;
382};
383
384MemoryBlock MemoryBlockPool::fetch(size_t size) {
385 std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
386 return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
387 poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
388}
389
390MemoryBlockPool::MemoryBlockPool()
391 : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
392}
393
394MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
395 : mImpl(impl) {
396}
397
398MemoryBlock::MemoryBlock() = default;
399
400MemoryBlock::~MemoryBlock() = default;
401
402const uint8_t* MemoryBlock::data() const {
403 return mImpl ? mImpl->data() : nullptr;
404}
405
406size_t MemoryBlock::size() const {
407 return mImpl ? mImpl->size() : 0;
408}
409
410MemoryBlock MemoryBlock::Allocate(size_t size) {
411 return MemoryBlockPool().fetch(size);
412}
413
414} // namespace android