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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "Codec2BufferUtils"
19 #define ATRACE_TAG ATRACE_TAG_VIDEO
20 #include <utils/Log.h>
21 #include <utils/Trace.h>
22
23 #include <libyuv.h>
24
25 #include <list>
26 #include <mutex>
27
28 #include <android/hardware_buffer.h>
29 #include <media/hardware/HardwareAPI.h>
30 #include <media/stagefright/foundation/AUtils.h>
31
32 #include <C2Debug.h>
33
34 #include "Codec2BufferUtils.h"
35
36 namespace android {
37
38 namespace {
39
40 /**
41 * A flippable, optimizable memcpy. Constructs such as (from ? src : dst)
42 * do not work as the results are always const.
43 */
44 template<bool ToA, size_t S>
45 struct MemCopier {
46 template<typename A, typename B>
copyandroid::__anon4b7fff5a0111::MemCopier47 inline static void copy(A *a, const B *b, size_t size) {
48 __builtin_memcpy(a, b, size);
49 }
50 };
51
52 template<size_t S>
53 struct MemCopier<false, S> {
54 template<typename A, typename B>
copyandroid::__anon4b7fff5a0111::MemCopier55 inline static void copy(const A *a, B *b, size_t size) {
56 MemCopier<true, S>::copy(b, a, size);
57 }
58 };
59
60 /**
61 * Copies between a MediaImage and a graphic view.
62 *
63 * \param ToMediaImage whether to copy to (or from) the MediaImage
64 * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction)
65 * \param img MediaImage data
66 * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction)
67 */
68 template<bool ToMediaImage, typename View, typename ImagePixel>
_ImageCopy(View & view,const MediaImage2 * img,ImagePixel * imgBase)69 static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
70 // TODO: more efficient copying --- e.g. copy interleaved planes together, etc.
71 const C2PlanarLayout &layout = view.layout();
72 const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
73
74 for (uint32_t i = 0; i < layout.numPlanes; ++i) {
75 typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow =
76 imgBase + img->mPlane[i].mOffset;
77 typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow =
78 viewRow = view.data()[i];
79 const C2PlaneInfo &plane = layout.planes[i];
80 if (plane.colSampling != img->mPlane[i].mHorizSubsampling
81 || plane.rowSampling != img->mPlane[i].mVertSubsampling
82 || plane.allocatedDepth != img->mBitDepthAllocated
83 || plane.allocatedDepth < plane.bitDepth
84 // MediaImage only supports MSB values
85 || plane.rightShift != plane.allocatedDepth - plane.bitDepth
86 || (bpp > 1 && plane.endianness != plane.NATIVE)) {
87 return BAD_VALUE;
88 }
89
90 uint32_t planeW = img->mWidth / plane.colSampling;
91 uint32_t planeH = img->mHeight / plane.rowSampling;
92
93 bool canCopyByRow = (plane.colInc == bpp) && (img->mPlane[i].mColInc == bpp);
94 bool canCopyByPlane = canCopyByRow && (plane.rowInc == img->mPlane[i].mRowInc);
95 if (canCopyByPlane) {
96 MemCopier<ToMediaImage, 0>::copy(imgRow, viewRow, plane.rowInc * planeH);
97 } else if (canCopyByRow) {
98 for (uint32_t row = 0; row < planeH; ++row) {
99 MemCopier<ToMediaImage, 0>::copy(
100 imgRow, viewRow, std::min(plane.rowInc, img->mPlane[i].mRowInc));
101 imgRow += img->mPlane[i].mRowInc;
102 viewRow += plane.rowInc;
103 }
104 } else {
105 for (uint32_t row = 0; row < planeH; ++row) {
106 decltype(imgRow) imgPtr = imgRow;
107 decltype(viewRow) viewPtr = viewRow;
108 for (uint32_t col = 0; col < planeW; ++col) {
109 MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp);
110 imgPtr += img->mPlane[i].mColInc;
111 viewPtr += plane.colInc;
112 }
113 imgRow += img->mPlane[i].mRowInc;
114 viewRow += plane.rowInc;
115 }
116 }
117 }
118 return OK;
119 }
120
121 } // namespace
122
ImageCopy(uint8_t * imgBase,const MediaImage2 * img,const C2GraphicView & view)123 status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
124 if (img == nullptr
125 || imgBase == nullptr
126 || view.crop().width != img->mWidth
127 || view.crop().height != img->mHeight) {
128 return BAD_VALUE;
129 }
130 const uint8_t* src_y = view.data()[0];
131 const uint8_t* src_u = view.data()[1];
132 const uint8_t* src_v = view.data()[2];
133 int32_t src_stride_y = view.layout().planes[0].rowInc;
134 int32_t src_stride_u = view.layout().planes[1].rowInc;
135 int32_t src_stride_v = view.layout().planes[2].rowInc;
136 uint8_t* dst_y = imgBase + img->mPlane[0].mOffset;
137 uint8_t* dst_u = imgBase + img->mPlane[1].mOffset;
138 uint8_t* dst_v = imgBase + img->mPlane[2].mOffset;
139 int32_t dst_stride_y = img->mPlane[0].mRowInc;
140 int32_t dst_stride_u = img->mPlane[1].mRowInc;
141 int32_t dst_stride_v = img->mPlane[2].mRowInc;
142 int width = view.crop().width;
143 int height = view.crop().height;
144
145 if (IsNV12(view)) {
146 if (IsNV12(img)) {
147 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12");
148 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
149 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
150 return OK;
151 } else if (IsNV21(img)) {
152 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21");
153 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
154 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
155 return OK;
156 }
157 } else if (IsI420(img)) {
158 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420");
159 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
160 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
161 return OK;
162 }
163 }
164 } else if (IsNV21(view)) {
165 if (IsNV12(img)) {
166 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12");
167 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
168 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
169 return OK;
170 }
171 } else if (IsNV21(img)) {
172 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21");
173 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
174 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
175 return OK;
176 } else if (IsI420(img)) {
177 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420");
178 if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
179 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
180 return OK;
181 }
182 }
183 } else if (IsI420(view)) {
184 if (IsNV12(img)) {
185 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12");
186 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
187 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
188 return OK;
189 }
190 } else if (IsNV21(img)) {
191 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21");
192 if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
193 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
194 return OK;
195 }
196 } else if (IsI420(img)) {
197 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420");
198 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
199 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
200 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
201 return OK;
202 }
203 }
204 ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic");
205 return _ImageCopy<true>(view, img, imgBase);
206 }
207
ImageCopy(C2GraphicView & view,const uint8_t * imgBase,const MediaImage2 * img)208 status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
209 if (img == nullptr
210 || imgBase == nullptr
211 || view.crop().width != img->mWidth
212 || view.crop().height != img->mHeight) {
213 return BAD_VALUE;
214 }
215 const uint8_t* src_y = imgBase + img->mPlane[0].mOffset;
216 const uint8_t* src_u = imgBase + img->mPlane[1].mOffset;
217 const uint8_t* src_v = imgBase + img->mPlane[2].mOffset;
218 int32_t src_stride_y = img->mPlane[0].mRowInc;
219 int32_t src_stride_u = img->mPlane[1].mRowInc;
220 int32_t src_stride_v = img->mPlane[2].mRowInc;
221 uint8_t* dst_y = view.data()[0];
222 uint8_t* dst_u = view.data()[1];
223 uint8_t* dst_v = view.data()[2];
224 int32_t dst_stride_y = view.layout().planes[0].rowInc;
225 int32_t dst_stride_u = view.layout().planes[1].rowInc;
226 int32_t dst_stride_v = view.layout().planes[2].rowInc;
227 int width = view.crop().width;
228 int height = view.crop().height;
229 if (IsNV12(img)) {
230 if (IsNV12(view)) {
231 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12");
232 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
233 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
234 return OK;
235 } else if (IsNV21(view)) {
236 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21");
237 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
238 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
239 return OK;
240 }
241 } else if (IsI420(view)) {
242 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420");
243 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
244 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
245 return OK;
246 }
247 }
248 } else if (IsNV21(img)) {
249 if (IsNV12(view)) {
250 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12");
251 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
252 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
253 return OK;
254 }
255 } else if (IsNV21(view)) {
256 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21");
257 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
258 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
259 return OK;
260 } else if (IsI420(view)) {
261 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420");
262 if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
263 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
264 return OK;
265 }
266 }
267 } else if (IsI420(img)) {
268 if (IsNV12(view)) {
269 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12");
270 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
271 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
272 return OK;
273 }
274 } else if (IsNV21(view)) {
275 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21");
276 if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
277 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
278 return OK;
279 }
280 } else if (IsI420(view)) {
281 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420");
282 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
283 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
284 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
285 return OK;
286 }
287 }
288 ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic");
289 return _ImageCopy<false>(view, img, imgBase);
290 }
291
IsYUV420(const C2GraphicView & view)292 bool IsYUV420(const C2GraphicView &view) {
293 const C2PlanarLayout &layout = view.layout();
294 return (layout.numPlanes == 3
295 && layout.type == C2PlanarLayout::TYPE_YUV
296 && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
297 && layout.planes[layout.PLANE_Y].allocatedDepth == 8
298 && layout.planes[layout.PLANE_Y].bitDepth == 8
299 && layout.planes[layout.PLANE_Y].rightShift == 0
300 && layout.planes[layout.PLANE_Y].colSampling == 1
301 && layout.planes[layout.PLANE_Y].rowSampling == 1
302 && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
303 && layout.planes[layout.PLANE_U].allocatedDepth == 8
304 && layout.planes[layout.PLANE_U].bitDepth == 8
305 && layout.planes[layout.PLANE_U].rightShift == 0
306 && layout.planes[layout.PLANE_U].colSampling == 2
307 && layout.planes[layout.PLANE_U].rowSampling == 2
308 && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
309 && layout.planes[layout.PLANE_V].allocatedDepth == 8
310 && layout.planes[layout.PLANE_V].bitDepth == 8
311 && layout.planes[layout.PLANE_V].rightShift == 0
312 && layout.planes[layout.PLANE_V].colSampling == 2
313 && layout.planes[layout.PLANE_V].rowSampling == 2);
314 }
315
IsYUV420_10bit(const C2GraphicView & view)316 bool IsYUV420_10bit(const C2GraphicView &view) {
317 const C2PlanarLayout &layout = view.layout();
318 return (layout.numPlanes == 3
319 && layout.type == C2PlanarLayout::TYPE_YUV
320 && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
321 && layout.planes[layout.PLANE_Y].allocatedDepth == 16
322 && layout.planes[layout.PLANE_Y].bitDepth == 10
323 && layout.planes[layout.PLANE_Y].colSampling == 1
324 && layout.planes[layout.PLANE_Y].rowSampling == 1
325 && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
326 && layout.planes[layout.PLANE_U].allocatedDepth == 16
327 && layout.planes[layout.PLANE_U].bitDepth == 10
328 && layout.planes[layout.PLANE_U].colSampling == 2
329 && layout.planes[layout.PLANE_U].rowSampling == 2
330 && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
331 && layout.planes[layout.PLANE_V].allocatedDepth == 16
332 && layout.planes[layout.PLANE_V].bitDepth == 10
333 && layout.planes[layout.PLANE_V].colSampling == 2
334 && layout.planes[layout.PLANE_V].rowSampling == 2);
335 }
336
337
IsNV12(const C2GraphicView & view)338 bool IsNV12(const C2GraphicView &view) {
339 if (!IsYUV420(view)) {
340 return false;
341 }
342 const C2PlanarLayout &layout = view.layout();
343 return (layout.rootPlanes == 2
344 && layout.planes[layout.PLANE_U].colInc == 2
345 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
346 && layout.planes[layout.PLANE_U].offset == 0
347 && layout.planes[layout.PLANE_V].colInc == 2
348 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
349 && layout.planes[layout.PLANE_V].offset == 1);
350 }
351
IsP010(const C2GraphicView & view)352 bool IsP010(const C2GraphicView &view) {
353 if (!IsYUV420_10bit(view)) {
354 return false;
355 }
356 const C2PlanarLayout &layout = view.layout();
357 return (layout.rootPlanes == 2
358 && layout.planes[layout.PLANE_U].colInc == 4
359 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
360 && layout.planes[layout.PLANE_U].offset == 0
361 && layout.planes[layout.PLANE_V].colInc == 4
362 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
363 && layout.planes[layout.PLANE_V].offset == 2
364 && layout.planes[layout.PLANE_Y].rightShift == 6
365 && layout.planes[layout.PLANE_U].rightShift == 6
366 && layout.planes[layout.PLANE_V].rightShift == 6);
367 }
368
369
IsNV21(const C2GraphicView & view)370 bool IsNV21(const C2GraphicView &view) {
371 if (!IsYUV420(view)) {
372 return false;
373 }
374 const C2PlanarLayout &layout = view.layout();
375 return (layout.rootPlanes == 2
376 && layout.planes[layout.PLANE_U].colInc == 2
377 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V
378 && layout.planes[layout.PLANE_U].offset == 1
379 && layout.planes[layout.PLANE_V].colInc == 2
380 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
381 && layout.planes[layout.PLANE_V].offset == 0);
382 }
383
IsI420(const C2GraphicView & view)384 bool IsI420(const C2GraphicView &view) {
385 if (!IsYUV420(view)) {
386 return false;
387 }
388 const C2PlanarLayout &layout = view.layout();
389 return (layout.rootPlanes == 3
390 && layout.planes[layout.PLANE_U].colInc == 1
391 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
392 && layout.planes[layout.PLANE_U].offset == 0
393 && layout.planes[layout.PLANE_V].colInc == 1
394 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
395 && layout.planes[layout.PLANE_V].offset == 0);
396 }
397
IsYUV420(const MediaImage2 * img)398 bool IsYUV420(const MediaImage2 *img) {
399 return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV
400 && img->mNumPlanes == 3
401 && img->mBitDepth == 8
402 && img->mBitDepthAllocated == 8
403 && img->mPlane[0].mHorizSubsampling == 1
404 && img->mPlane[0].mVertSubsampling == 1
405 && img->mPlane[1].mHorizSubsampling == 2
406 && img->mPlane[1].mVertSubsampling == 2
407 && img->mPlane[2].mHorizSubsampling == 2
408 && img->mPlane[2].mVertSubsampling == 2);
409 }
410
IsNV12(const MediaImage2 * img)411 bool IsNV12(const MediaImage2 *img) {
412 if (!IsYUV420(img)) {
413 return false;
414 }
415 return (img->mPlane[1].mColInc == 2
416 && img->mPlane[2].mColInc == 2
417 && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1));
418 }
419
IsNV21(const MediaImage2 * img)420 bool IsNV21(const MediaImage2 *img) {
421 if (!IsYUV420(img)) {
422 return false;
423 }
424 return (img->mPlane[1].mColInc == 2
425 && img->mPlane[2].mColInc == 2
426 && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1));
427 }
428
IsI420(const MediaImage2 * img)429 bool IsI420(const MediaImage2 *img) {
430 if (!IsYUV420(img)) {
431 return false;
432 }
433 return (img->mPlane[1].mColInc == 1
434 && img->mPlane[2].mColInc == 1
435 && img->mPlane[2].mOffset > img->mPlane[1].mOffset);
436 }
437
GetYuv420FlexibleLayout()438 FlexLayout GetYuv420FlexibleLayout() {
439 static FlexLayout sLayout = []{
440 AHardwareBuffer_Desc desc = {
441 16, // width
442 16, // height
443 1, // layers
444 AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420,
445 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
446 0, // stride
447 0, // rfu0
448 0, // rfu1
449 };
450 AHardwareBuffer *buffer = nullptr;
451 int ret = AHardwareBuffer_allocate(&desc, &buffer);
452 if (ret != 0) {
453 return FLEX_LAYOUT_UNKNOWN;
454 }
455 class AutoCloser {
456 public:
457 AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {}
458 ~AutoCloser() {
459 if (mLocked) {
460 AHardwareBuffer_unlock(mBuffer, nullptr);
461 }
462 AHardwareBuffer_release(mBuffer);
463 }
464
465 void setLocked() { mLocked = true; }
466
467 private:
468 AHardwareBuffer *mBuffer;
469 bool mLocked;
470 } autoCloser(buffer);
471 AHardwareBuffer_Planes planes;
472 ret = AHardwareBuffer_lockPlanes(
473 buffer,
474 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
475 -1, // fence
476 nullptr, // rect
477 &planes);
478 if (ret != 0) {
479 AHardwareBuffer_release(buffer);
480 return FLEX_LAYOUT_UNKNOWN;
481 }
482 autoCloser.setLocked();
483 if (planes.planeCount != 3) {
484 return FLEX_LAYOUT_UNKNOWN;
485 }
486 if (planes.planes[0].pixelStride != 1) {
487 return FLEX_LAYOUT_UNKNOWN;
488 }
489 if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) {
490 return FLEX_LAYOUT_PLANAR;
491 }
492 if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) {
493 ssize_t uvDist =
494 static_cast<uint8_t *>(planes.planes[2].data) -
495 static_cast<uint8_t *>(planes.planes[1].data);
496 if (uvDist == 1) {
497 return FLEX_LAYOUT_SEMIPLANAR_UV;
498 } else if (uvDist == -1) {
499 return FLEX_LAYOUT_SEMIPLANAR_VU;
500 }
501 return FLEX_LAYOUT_UNKNOWN;
502 }
503 return FLEX_LAYOUT_UNKNOWN;
504 }();
505 return sLayout;
506 }
507
CreateYUV420PlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)508 MediaImage2 CreateYUV420PlanarMediaImage2(
509 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
510 return MediaImage2 {
511 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
512 .mNumPlanes = 3,
513 .mWidth = width,
514 .mHeight = height,
515 .mBitDepth = 8,
516 .mBitDepthAllocated = 8,
517 .mPlane = {
518 {
519 .mOffset = 0,
520 .mColInc = 1,
521 .mRowInc = (int32_t)stride,
522 .mHorizSubsampling = 1,
523 .mVertSubsampling = 1,
524 },
525 {
526 .mOffset = stride * vstride,
527 .mColInc = 1,
528 .mRowInc = (int32_t)stride / 2,
529 .mHorizSubsampling = 2,
530 .mVertSubsampling = 2,
531 },
532 {
533 .mOffset = stride * vstride * 5 / 4,
534 .mColInc = 1,
535 .mRowInc = (int32_t)stride / 2,
536 .mHorizSubsampling = 2,
537 .mVertSubsampling = 2,
538 }
539 },
540 };
541 }
542
CreateYUV420SemiPlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)543 MediaImage2 CreateYUV420SemiPlanarMediaImage2(
544 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
545 return MediaImage2 {
546 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
547 .mNumPlanes = 3,
548 .mWidth = width,
549 .mHeight = height,
550 .mBitDepth = 8,
551 .mBitDepthAllocated = 8,
552 .mPlane = {
553 {
554 .mOffset = 0,
555 .mColInc = 1,
556 .mRowInc = (int32_t)stride,
557 .mHorizSubsampling = 1,
558 .mVertSubsampling = 1,
559 },
560 {
561 .mOffset = stride * vstride,
562 .mColInc = 2,
563 .mRowInc = (int32_t)stride,
564 .mHorizSubsampling = 2,
565 .mVertSubsampling = 2,
566 },
567 {
568 .mOffset = stride * vstride + 1,
569 .mColInc = 2,
570 .mRowInc = (int32_t)stride,
571 .mHorizSubsampling = 2,
572 .mVertSubsampling = 2,
573 }
574 },
575 };
576 }
577
578 // Matrix coefficient to convert RGB to Planar YUV data.
579 // Each sub-array represents the 3X3 coeff used with R, G and B
580 static const int16_t bt601Matrix[2][3][3] = {
581 { { 77, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */
582 { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } }, /* RANGE_LIMITED */
583 };
584
585 static const int16_t bt709Matrix[2][3][3] = {
586 // TRICKY: 18 is adjusted to 19 so that sum of row 1 is 256
587 { { 54, 183, 19 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */
588 // TRICKY: -87 is adjusted to -86 so that sum of row 2 is 0
589 { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */
590 };
591
ConvertRGBToPlanarYUV(uint8_t * dstY,size_t dstStride,size_t dstVStride,size_t bufferSize,const C2GraphicView & src,C2Color::matrix_t colorMatrix,C2Color::range_t colorRange)592 status_t ConvertRGBToPlanarYUV(
593 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
594 const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) {
595 CHECK(dstY != nullptr);
596
597 if (dstStride * dstVStride * 3 / 2 > bufferSize) {
598 ALOGD("conversion buffer is too small for converting from RGB to YUV");
599 return NO_MEMORY;
600 }
601
602 uint8_t *dstU = dstY + dstStride * dstVStride;
603 uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
604
605 const C2PlanarLayout &layout = src.layout();
606 const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R];
607 const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
608 const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B];
609
610 // set default range as limited
611 if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) {
612 colorRange = C2Color::RANGE_LIMITED;
613 }
614 const int16_t (*weights)[3] =
615 (colorMatrix == C2Color::MATRIX_BT709) ?
616 bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1];
617 uint8_t zeroLvl = colorRange == C2Color::RANGE_FULL ? 0 : 16;
618 uint8_t maxLvlLuma = colorRange == C2Color::RANGE_FULL ? 255 : 235;
619 uint8_t maxLvlChroma = colorRange == C2Color::RANGE_FULL ? 255 : 240;
620
621 #define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max)))
622 for (size_t y = 0; y < src.crop().height; ++y) {
623 for (size_t x = 0; x < src.crop().width; ++x) {
624 uint8_t r = *pRed;
625 uint8_t g = *pGreen;
626 uint8_t b = *pBlue;
627
628 unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) +
629 zeroLvl;
630
631 dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma);
632
633 if ((x & 1) == 0 && (y & 1) == 0) {
634 unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) +
635 128;
636
637 unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) +
638 128;
639
640 dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma);
641 dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma);
642 }
643 pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc;
644 pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
645 pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc;
646 }
647
648 if ((y & 1) == 0) {
649 dstU += dstStride >> 1;
650 dstV += dstStride >> 1;
651 }
652
653 pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
654 pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
655 pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
656 pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
657 pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
658 pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
659
660 dstY += dstStride;
661 }
662 return OK;
663 }
664
665 namespace {
666
667 /**
668 * A block of raw allocated memory.
669 */
670 struct MemoryBlockPoolBlock {
MemoryBlockPoolBlockandroid::__anon4b7fff5a0311::MemoryBlockPoolBlock671 MemoryBlockPoolBlock(size_t size)
672 : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
673
~MemoryBlockPoolBlockandroid::__anon4b7fff5a0311::MemoryBlockPoolBlock674 ~MemoryBlockPoolBlock() {
675 delete[] mData;
676 }
677
dataandroid::__anon4b7fff5a0311::MemoryBlockPoolBlock678 const uint8_t *data() const {
679 return mData;
680 }
681
sizeandroid::__anon4b7fff5a0311::MemoryBlockPoolBlock682 size_t size() const {
683 return mSize;
684 }
685
686 C2_DO_NOT_COPY(MemoryBlockPoolBlock);
687
688 private:
689 uint8_t *mData;
690 size_t mSize;
691 };
692
693 /**
694 * A simple raw memory block pool implementation.
695 */
696 struct MemoryBlockPoolImpl {
releaseandroid::__anon4b7fff5a0311::MemoryBlockPoolImpl697 void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
698 std::lock_guard<std::mutex> lock(mMutex);
699 // return block to free blocks if it is the current size; otherwise, discard
700 if (block->size() == mCurrentSize) {
701 mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
702 } else {
703 mBlocksInUse.erase(block);
704 }
705 }
706
fetchandroid::__anon4b7fff5a0311::MemoryBlockPoolImpl707 std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
708 std::lock_guard<std::mutex> lock(mMutex);
709 mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
710 return block.size() != size;
711 });
712 mCurrentSize = size;
713 if (mFreeBlocks.empty()) {
714 mBlocksInUse.emplace_front(size);
715 } else {
716 mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
717 }
718 return mBlocksInUse.begin();
719 }
720
721 MemoryBlockPoolImpl() = default;
722
723 C2_DO_NOT_COPY(MemoryBlockPoolImpl);
724
725 private:
726 std::mutex mMutex;
727 std::list<MemoryBlockPoolBlock> mFreeBlocks;
728 std::list<MemoryBlockPoolBlock> mBlocksInUse;
729 size_t mCurrentSize;
730 };
731
732 } // namespace
733
734 struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
735 };
736
737 struct MemoryBlock::Impl {
Implandroid::MemoryBlock::Impl738 Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
739 std::shared_ptr<MemoryBlockPoolImpl> pool)
740 : mBlock(block), mPool(pool) {
741 }
742
~Implandroid::MemoryBlock::Impl743 ~Impl() {
744 mPool->release(mBlock);
745 }
746
dataandroid::MemoryBlock::Impl747 const uint8_t *data() const {
748 return mBlock->data();
749 }
750
sizeandroid::MemoryBlock::Impl751 size_t size() const {
752 return mBlock->size();
753 }
754
755 private:
756 std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
757 std::shared_ptr<MemoryBlockPoolImpl> mPool;
758 };
759
fetch(size_t size)760 MemoryBlock MemoryBlockPool::fetch(size_t size) {
761 std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
762 return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
763 poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
764 }
765
MemoryBlockPool()766 MemoryBlockPool::MemoryBlockPool()
767 : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
768 }
769
MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)770 MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
771 : mImpl(impl) {
772 }
773
774 MemoryBlock::MemoryBlock() = default;
775
776 MemoryBlock::~MemoryBlock() = default;
777
data() const778 const uint8_t* MemoryBlock::data() const {
779 return mImpl ? mImpl->data() : nullptr;
780 }
781
size() const782 size_t MemoryBlock::size() const {
783 return mImpl ? mImpl->size() : 0;
784 }
785
Allocate(size_t size)786 MemoryBlock MemoryBlock::Allocate(size_t size) {
787 return MemoryBlockPool().fetch(size);
788 }
789
790 } // namespace android
791