blob: 096698836ac31b178a098942edcf82cce8133a28 [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
Pawin Vongmasa1f213362019-01-24 06:59:16 -080021#include <libyuv.h>
22
Pawin Vongmasa36653902018-11-15 00:10:25 -080023#include <list>
24#include <mutex>
25
Wonsik Kima38fdf22021-04-05 14:50:29 -070026#include <android/hardware_buffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080027#include <media/hardware/HardwareAPI.h>
28#include <media/stagefright/foundation/AUtils.h>
29
30#include <C2Debug.h>
31
32#include "Codec2BufferUtils.h"
33
34namespace android {
35
36namespace {
37
38/**
39 * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) do not work as the results are
40 * always const.
41 */
42template<bool ToA, size_t S>
43struct MemCopier {
44 template<typename A, typename B>
45 inline static void copy(A *a, const B *b, size_t size) {
46 __builtin_memcpy(a, b, size);
47 }
48};
49
50template<size_t S>
51struct MemCopier<false, S> {
52 template<typename A, typename B>
53 inline static void copy(const A *a, B *b, size_t size) {
54 MemCopier<true, S>::copy(b, a, size);
55 }
56};
57
58/**
59 * Copies between a MediaImage and a graphic view.
60 *
61 * \param ToMediaImage whether to copy to (or from) the MediaImage
62 * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction)
63 * \param img MediaImage data
64 * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction)
65 */
66template<bool ToMediaImage, typename View, typename ImagePixel>
67static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -080068 // TODO: more efficient copying --- e.g. copy interleaved planes together, etc.
Pawin Vongmasa36653902018-11-15 00:10:25 -080069 const C2PlanarLayout &layout = view.layout();
70 const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
Pawin Vongmasa1f213362019-01-24 06:59:16 -080071
Pawin Vongmasa36653902018-11-15 00:10:25 -080072 for (uint32_t i = 0; i < layout.numPlanes; ++i) {
73 typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow =
74 imgBase + img->mPlane[i].mOffset;
75 typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow =
76 viewRow = view.data()[i];
77 const C2PlaneInfo &plane = layout.planes[i];
78 if (plane.colSampling != img->mPlane[i].mHorizSubsampling
79 || plane.rowSampling != img->mPlane[i].mVertSubsampling
80 || plane.allocatedDepth != img->mBitDepthAllocated
81 || plane.allocatedDepth < plane.bitDepth
82 // MediaImage only supports MSB values
83 || plane.rightShift != plane.allocatedDepth - plane.bitDepth
84 || (bpp > 1 && plane.endianness != plane.NATIVE)) {
85 return BAD_VALUE;
86 }
87
88 uint32_t planeW = img->mWidth / plane.colSampling;
89 uint32_t planeH = img->mHeight / plane.rowSampling;
Pawin Vongmasa8be93112018-12-11 14:01:42 -080090
91 bool canCopyByRow = (plane.colInc == 1) && (img->mPlane[i].mColInc == 1);
92 bool canCopyByPlane = canCopyByRow && (plane.rowInc == img->mPlane[i].mRowInc);
93 if (canCopyByPlane) {
94 MemCopier<ToMediaImage, 0>::copy(imgRow, viewRow, plane.rowInc * planeH);
95 } else if (canCopyByRow) {
96 for (uint32_t row = 0; row < planeH; ++row) {
97 MemCopier<ToMediaImage, 0>::copy(
98 imgRow, viewRow, std::min(plane.rowInc, img->mPlane[i].mRowInc));
99 imgRow += img->mPlane[i].mRowInc;
100 viewRow += plane.rowInc;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800101 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800102 } else {
103 for (uint32_t row = 0; row < planeH; ++row) {
104 decltype(imgRow) imgPtr = imgRow;
105 decltype(viewRow) viewPtr = viewRow;
106 for (uint32_t col = 0; col < planeW; ++col) {
107 MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp);
108 imgPtr += img->mPlane[i].mColInc;
109 viewPtr += plane.colInc;
110 }
111 imgRow += img->mPlane[i].mRowInc;
112 viewRow += plane.rowInc;
113 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800114 }
115 }
116 return OK;
117}
118
119} // namespace
120
121status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
Harish Mahendrakarf7c49e22019-05-24 14:19:16 -0700122 if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800123 return BAD_VALUE;
124 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800125 const uint8_t* src_y = view.data()[0];
126 const uint8_t* src_u = view.data()[1];
127 const uint8_t* src_v = view.data()[2];
128 int32_t src_stride_y = view.layout().planes[0].rowInc;
129 int32_t src_stride_u = view.layout().planes[1].rowInc;
130 int32_t src_stride_v = view.layout().planes[2].rowInc;
131 uint8_t* dst_y = imgBase + img->mPlane[0].mOffset;
132 uint8_t* dst_u = imgBase + img->mPlane[1].mOffset;
133 uint8_t* dst_v = imgBase + img->mPlane[2].mOffset;
134 int32_t dst_stride_y = img->mPlane[0].mRowInc;
135 int32_t dst_stride_u = img->mPlane[1].mRowInc;
136 int32_t dst_stride_v = img->mPlane[2].mRowInc;
137 int width = view.crop().width;
138 int height = view.crop().height;
139
Wonsik Kima38fdf22021-04-05 14:50:29 -0700140 if (IsNV12(view)) {
141 if (IsNV12(img)) {
142 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
143 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
144 return OK;
145 } else if (IsNV21(img)) {
146 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
147 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
148 return OK;
149 }
150 } else if (IsI420(img)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800151 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800152 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800153 return OK;
154 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700155 }
156 } else if (IsNV21(view)) {
157 if (IsNV12(img)) {
158 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
159 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
160 return OK;
161 }
162 } else if (IsNV21(img)) {
163 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
164 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
165 return OK;
166 } else if (IsI420(img)) {
167 if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
168 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
169 return OK;
170 }
171 }
172 } else if (IsI420(view)) {
173 if (IsNV12(img)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800174 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800175 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800176 return OK;
177 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700178 } else if (IsNV21(img)) {
179 if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
180 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
181 return OK;
182 }
183 } else if (IsI420(img)) {
184 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
185 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
186 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
187 return OK;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800188 }
189 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800190 return _ImageCopy<true>(view, img, imgBase);
191}
192
193status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
Harish Mahendrakarf7c49e22019-05-24 14:19:16 -0700194 if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800195 return BAD_VALUE;
196 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800197 const uint8_t* src_y = imgBase + img->mPlane[0].mOffset;
198 const uint8_t* src_u = imgBase + img->mPlane[1].mOffset;
199 const uint8_t* src_v = imgBase + img->mPlane[2].mOffset;
200 int32_t src_stride_y = img->mPlane[0].mRowInc;
201 int32_t src_stride_u = img->mPlane[1].mRowInc;
202 int32_t src_stride_v = img->mPlane[2].mRowInc;
203 uint8_t* dst_y = view.data()[0];
204 uint8_t* dst_u = view.data()[1];
205 uint8_t* dst_v = view.data()[2];
206 int32_t dst_stride_y = view.layout().planes[0].rowInc;
207 int32_t dst_stride_u = view.layout().planes[1].rowInc;
208 int32_t dst_stride_v = view.layout().planes[2].rowInc;
209 int width = view.crop().width;
210 int height = view.crop().height;
Wonsik Kima38fdf22021-04-05 14:50:29 -0700211 if (IsNV12(img)) {
212 if (IsNV12(view)) {
213 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
214 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
215 return OK;
216 } else if (IsNV21(view)) {
217 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
218 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
219 return OK;
220 }
221 } else if (IsI420(view)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800222 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800223 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800224 return OK;
225 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700226 }
227 } else if (IsNV21(img)) {
228 if (IsNV12(view)) {
229 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
230 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
231 return OK;
232 }
233 } else if (IsNV21(view)) {
234 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
235 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
236 return OK;
237 } else if (IsI420(view)) {
238 if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
239 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
240 return OK;
241 }
242 }
243 } else if (IsI420(img)) {
244 if (IsNV12(view)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800245 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800246 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800247 return OK;
248 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700249 } else if (IsNV21(view)) {
250 if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
251 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
252 return OK;
253 }
254 } else if (IsI420(view)) {
255 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
256 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
257 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
258 return OK;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800259 }
260 }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800261 return _ImageCopy<false>(view, img, imgBase);
262}
263
264bool IsYUV420(const C2GraphicView &view) {
265 const C2PlanarLayout &layout = view.layout();
266 return (layout.numPlanes == 3
267 && layout.type == C2PlanarLayout::TYPE_YUV
268 && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
269 && layout.planes[layout.PLANE_Y].allocatedDepth == 8
270 && layout.planes[layout.PLANE_Y].bitDepth == 8
271 && layout.planes[layout.PLANE_Y].rightShift == 0
272 && layout.planes[layout.PLANE_Y].colSampling == 1
273 && layout.planes[layout.PLANE_Y].rowSampling == 1
274 && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
275 && layout.planes[layout.PLANE_U].allocatedDepth == 8
276 && layout.planes[layout.PLANE_U].bitDepth == 8
277 && layout.planes[layout.PLANE_U].rightShift == 0
278 && layout.planes[layout.PLANE_U].colSampling == 2
279 && layout.planes[layout.PLANE_U].rowSampling == 2
280 && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
281 && layout.planes[layout.PLANE_V].allocatedDepth == 8
282 && layout.planes[layout.PLANE_V].bitDepth == 8
283 && layout.planes[layout.PLANE_V].rightShift == 0
284 && layout.planes[layout.PLANE_V].colSampling == 2
285 && layout.planes[layout.PLANE_V].rowSampling == 2);
286}
287
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800288bool IsNV12(const C2GraphicView &view) {
289 if (!IsYUV420(view)) {
290 return false;
291 }
292 const C2PlanarLayout &layout = view.layout();
293 return (layout.rootPlanes == 2
294 && layout.planes[layout.PLANE_U].colInc == 2
295 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
296 && layout.planes[layout.PLANE_U].offset == 0
297 && layout.planes[layout.PLANE_V].colInc == 2
298 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
299 && layout.planes[layout.PLANE_V].offset == 1);
300}
301
Wonsik Kima38fdf22021-04-05 14:50:29 -0700302bool IsNV21(const C2GraphicView &view) {
303 if (!IsYUV420(view)) {
304 return false;
305 }
306 const C2PlanarLayout &layout = view.layout();
307 return (layout.rootPlanes == 2
308 && layout.planes[layout.PLANE_U].colInc == 2
309 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V
310 && layout.planes[layout.PLANE_U].offset == 1
311 && layout.planes[layout.PLANE_V].colInc == 2
312 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
313 && layout.planes[layout.PLANE_V].offset == 0);
314}
315
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800316bool IsI420(const C2GraphicView &view) {
317 if (!IsYUV420(view)) {
318 return false;
319 }
320 const C2PlanarLayout &layout = view.layout();
321 return (layout.rootPlanes == 3
322 && layout.planes[layout.PLANE_U].colInc == 1
323 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
324 && layout.planes[layout.PLANE_U].offset == 0
325 && layout.planes[layout.PLANE_V].colInc == 1
326 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
327 && layout.planes[layout.PLANE_V].offset == 0);
328}
329
330bool IsYUV420(const MediaImage2 *img) {
331 return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV
332 && img->mNumPlanes == 3
333 && img->mBitDepth == 8
334 && img->mBitDepthAllocated == 8
335 && img->mPlane[0].mHorizSubsampling == 1
336 && img->mPlane[0].mVertSubsampling == 1
337 && img->mPlane[1].mHorizSubsampling == 2
338 && img->mPlane[1].mVertSubsampling == 2
339 && img->mPlane[2].mHorizSubsampling == 2
340 && img->mPlane[2].mVertSubsampling == 2);
341}
342
343bool IsNV12(const MediaImage2 *img) {
344 if (!IsYUV420(img)) {
345 return false;
346 }
347 return (img->mPlane[1].mColInc == 2
348 && img->mPlane[2].mColInc == 2
Harish Mahendrakarcd59f032021-04-16 18:55:41 -0700349 && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1));
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800350}
351
Wonsik Kima38fdf22021-04-05 14:50:29 -0700352bool IsNV21(const MediaImage2 *img) {
353 if (!IsYUV420(img)) {
354 return false;
355 }
356 return (img->mPlane[1].mColInc == 2
357 && img->mPlane[2].mColInc == 2
Harish Mahendrakarcd59f032021-04-16 18:55:41 -0700358 && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1));
Wonsik Kima38fdf22021-04-05 14:50:29 -0700359}
360
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800361bool IsI420(const MediaImage2 *img) {
362 if (!IsYUV420(img)) {
363 return false;
364 }
365 return (img->mPlane[1].mColInc == 1
366 && img->mPlane[2].mColInc == 1
367 && img->mPlane[2].mOffset > img->mPlane[1].mOffset);
368}
369
Wonsik Kima38fdf22021-04-05 14:50:29 -0700370FlexLayout GetYuv420FlexibleLayout() {
371 static FlexLayout sLayout = []{
372 AHardwareBuffer_Desc desc = {
373 16, // width
374 16, // height
375 1, // layers
376 AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420,
377 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
378 0, // stride
379 0, // rfu0
380 0, // rfu1
381 };
382 AHardwareBuffer *buffer = nullptr;
383 int ret = AHardwareBuffer_allocate(&desc, &buffer);
384 if (ret != 0) {
385 return FLEX_LAYOUT_UNKNOWN;
386 }
387 class AutoCloser {
388 public:
389 AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {}
390 ~AutoCloser() {
391 if (mLocked) {
392 AHardwareBuffer_unlock(mBuffer, nullptr);
393 }
394 AHardwareBuffer_release(mBuffer);
395 }
396
397 void setLocked() { mLocked = true; }
398
399 private:
400 AHardwareBuffer *mBuffer;
401 bool mLocked;
402 } autoCloser(buffer);
403 AHardwareBuffer_Planes planes;
404 ret = AHardwareBuffer_lockPlanes(
405 buffer,
406 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
407 -1, // fence
408 nullptr, // rect
409 &planes);
410 if (ret != 0) {
411 AHardwareBuffer_release(buffer);
412 return FLEX_LAYOUT_UNKNOWN;
413 }
414 autoCloser.setLocked();
415 if (planes.planeCount != 3) {
416 return FLEX_LAYOUT_UNKNOWN;
417 }
418 if (planes.planes[0].pixelStride != 1) {
419 return FLEX_LAYOUT_UNKNOWN;
420 }
421 if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) {
422 return FLEX_LAYOUT_PLANAR;
423 }
424 if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) {
425 ssize_t uvDist =
426 static_cast<uint8_t *>(planes.planes[2].data) -
427 static_cast<uint8_t *>(planes.planes[1].data);
428 if (uvDist == 1) {
429 return FLEX_LAYOUT_SEMIPLANAR_UV;
430 } else if (uvDist == -1) {
431 return FLEX_LAYOUT_SEMIPLANAR_VU;
432 }
433 return FLEX_LAYOUT_UNKNOWN;
434 }
435 return FLEX_LAYOUT_UNKNOWN;
436 }();
437 return sLayout;
438}
439
Pawin Vongmasa36653902018-11-15 00:10:25 -0800440MediaImage2 CreateYUV420PlanarMediaImage2(
441 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
442 return MediaImage2 {
443 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
444 .mNumPlanes = 3,
445 .mWidth = width,
446 .mHeight = height,
447 .mBitDepth = 8,
448 .mBitDepthAllocated = 8,
449 .mPlane = {
450 {
451 .mOffset = 0,
452 .mColInc = 1,
453 .mRowInc = (int32_t)stride,
454 .mHorizSubsampling = 1,
455 .mVertSubsampling = 1,
456 },
457 {
458 .mOffset = stride * vstride,
459 .mColInc = 1,
460 .mRowInc = (int32_t)stride / 2,
461 .mHorizSubsampling = 2,
462 .mVertSubsampling = 2,
463 },
464 {
465 .mOffset = stride * vstride * 5 / 4,
466 .mColInc = 1,
467 .mRowInc = (int32_t)stride / 2,
468 .mHorizSubsampling = 2,
469 .mVertSubsampling = 2,
470 }
471 },
472 };
473}
474
475MediaImage2 CreateYUV420SemiPlanarMediaImage2(
476 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
477 return MediaImage2 {
478 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
479 .mNumPlanes = 3,
480 .mWidth = width,
481 .mHeight = height,
482 .mBitDepth = 8,
483 .mBitDepthAllocated = 8,
484 .mPlane = {
485 {
486 .mOffset = 0,
487 .mColInc = 1,
488 .mRowInc = (int32_t)stride,
489 .mHorizSubsampling = 1,
490 .mVertSubsampling = 1,
491 },
492 {
493 .mOffset = stride * vstride,
494 .mColInc = 2,
495 .mRowInc = (int32_t)stride,
496 .mHorizSubsampling = 2,
497 .mVertSubsampling = 2,
498 },
499 {
500 .mOffset = stride * vstride + 1,
501 .mColInc = 2,
502 .mRowInc = (int32_t)stride,
503 .mHorizSubsampling = 2,
504 .mVertSubsampling = 2,
505 }
506 },
507 };
508}
509
510status_t ConvertRGBToPlanarYUV(
511 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
512 const C2GraphicView &src) {
513 CHECK(dstY != nullptr);
514 CHECK((src.width() & 1) == 0);
515 CHECK((src.height() & 1) == 0);
516
517 if (dstStride * dstVStride * 3 / 2 > bufferSize) {
518 ALOGD("conversion buffer is too small for converting from RGB to YUV");
519 return NO_MEMORY;
520 }
521
522 uint8_t *dstU = dstY + dstStride * dstVStride;
523 uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
524
525 const C2PlanarLayout &layout = src.layout();
526 const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R];
527 const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
528 const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B];
529
530#define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
531 for (size_t y = 0; y < src.height(); ++y) {
532 for (size_t x = 0; x < src.width(); ++x) {
533 uint8_t red = *pRed;
534 uint8_t green = *pGreen;
535 uint8_t blue = *pBlue;
536
537 // using ITU-R BT.601 conversion matrix
538 unsigned luma =
539 CLIP3(0, (((red * 66 + green * 129 + blue * 25) >> 8) + 16), 255);
540
541 dstY[x] = luma;
542
543 if ((x & 1) == 0 && (y & 1) == 0) {
544 unsigned U =
545 CLIP3(0, (((-red * 38 - green * 74 + blue * 112) >> 8) + 128), 255);
546
547 unsigned V =
548 CLIP3(0, (((red * 112 - green * 94 - blue * 18) >> 8) + 128), 255);
549
550 dstU[x >> 1] = U;
551 dstV[x >> 1] = V;
552 }
553 pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc;
554 pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
555 pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc;
556 }
557
558 if ((y & 1) == 0) {
559 dstU += dstStride >> 1;
560 dstV += dstStride >> 1;
561 }
562
563 pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
564 pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
565 pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
566 pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
567 pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
568 pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
569
570 dstY += dstStride;
571 }
572 return OK;
573}
574
575namespace {
576
577/**
578 * A block of raw allocated memory.
579 */
580struct MemoryBlockPoolBlock {
581 MemoryBlockPoolBlock(size_t size)
582 : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
583
584 ~MemoryBlockPoolBlock() {
585 delete[] mData;
586 }
587
588 const uint8_t *data() const {
589 return mData;
590 }
591
592 size_t size() const {
593 return mSize;
594 }
595
596 C2_DO_NOT_COPY(MemoryBlockPoolBlock);
597
598private:
599 uint8_t *mData;
600 size_t mSize;
601};
602
603/**
604 * A simple raw memory block pool implementation.
605 */
606struct MemoryBlockPoolImpl {
607 void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
608 std::lock_guard<std::mutex> lock(mMutex);
609 // return block to free blocks if it is the current size; otherwise, discard
610 if (block->size() == mCurrentSize) {
611 mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
612 } else {
613 mBlocksInUse.erase(block);
614 }
615 }
616
617 std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
618 std::lock_guard<std::mutex> lock(mMutex);
619 mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
620 return block.size() != size;
621 });
622 mCurrentSize = size;
623 if (mFreeBlocks.empty()) {
624 mBlocksInUse.emplace_front(size);
625 } else {
626 mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
627 }
628 return mBlocksInUse.begin();
629 }
630
631 MemoryBlockPoolImpl() = default;
632
633 C2_DO_NOT_COPY(MemoryBlockPoolImpl);
634
635private:
636 std::mutex mMutex;
637 std::list<MemoryBlockPoolBlock> mFreeBlocks;
638 std::list<MemoryBlockPoolBlock> mBlocksInUse;
639 size_t mCurrentSize;
640};
641
642} // namespace
643
644struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
645};
646
647struct MemoryBlock::Impl {
648 Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
649 std::shared_ptr<MemoryBlockPoolImpl> pool)
650 : mBlock(block), mPool(pool) {
651 }
652
653 ~Impl() {
654 mPool->release(mBlock);
655 }
656
657 const uint8_t *data() const {
658 return mBlock->data();
659 }
660
661 size_t size() const {
662 return mBlock->size();
663 }
664
665private:
666 std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
667 std::shared_ptr<MemoryBlockPoolImpl> mPool;
668};
669
670MemoryBlock MemoryBlockPool::fetch(size_t size) {
671 std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
672 return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
673 poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
674}
675
676MemoryBlockPool::MemoryBlockPool()
677 : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
678}
679
680MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
681 : mImpl(impl) {
682}
683
684MemoryBlock::MemoryBlock() = default;
685
686MemoryBlock::~MemoryBlock() = default;
687
688const uint8_t* MemoryBlock::data() const {
689 return mImpl ? mImpl->data() : nullptr;
690}
691
692size_t MemoryBlock::size() const {
693 return mImpl ? mImpl->size() : 0;
694}
695
696MemoryBlock MemoryBlock::Allocate(size_t size) {
697 return MemoryBlockPool().fetch(size);
698}
699
700} // namespace android