• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::__anona09fa2f20111::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::__anona09fa2f20111::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 
IsNV12(const C2GraphicView & view)316 bool IsNV12(const C2GraphicView &view) {
317     if (!IsYUV420(view)) {
318         return false;
319     }
320     const C2PlanarLayout &layout = view.layout();
321     return (layout.rootPlanes == 2
322             && layout.planes[layout.PLANE_U].colInc == 2
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 == 2
326             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
327             && layout.planes[layout.PLANE_V].offset == 1);
328 }
329 
IsNV21(const C2GraphicView & view)330 bool IsNV21(const C2GraphicView &view) {
331     if (!IsYUV420(view)) {
332         return false;
333     }
334     const C2PlanarLayout &layout = view.layout();
335     return (layout.rootPlanes == 2
336             && layout.planes[layout.PLANE_U].colInc == 2
337             && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V
338             && layout.planes[layout.PLANE_U].offset == 1
339             && layout.planes[layout.PLANE_V].colInc == 2
340             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
341             && layout.planes[layout.PLANE_V].offset == 0);
342 }
343 
IsI420(const C2GraphicView & view)344 bool IsI420(const C2GraphicView &view) {
345     if (!IsYUV420(view)) {
346         return false;
347     }
348     const C2PlanarLayout &layout = view.layout();
349     return (layout.rootPlanes == 3
350             && layout.planes[layout.PLANE_U].colInc == 1
351             && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
352             && layout.planes[layout.PLANE_U].offset == 0
353             && layout.planes[layout.PLANE_V].colInc == 1
354             && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
355             && layout.planes[layout.PLANE_V].offset == 0);
356 }
357 
IsYUV420(const MediaImage2 * img)358 bool IsYUV420(const MediaImage2 *img) {
359     return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV
360             && img->mNumPlanes == 3
361             && img->mBitDepth == 8
362             && img->mBitDepthAllocated == 8
363             && img->mPlane[0].mHorizSubsampling == 1
364             && img->mPlane[0].mVertSubsampling == 1
365             && img->mPlane[1].mHorizSubsampling == 2
366             && img->mPlane[1].mVertSubsampling == 2
367             && img->mPlane[2].mHorizSubsampling == 2
368             && img->mPlane[2].mVertSubsampling == 2);
369 }
370 
IsNV12(const MediaImage2 * img)371 bool IsNV12(const MediaImage2 *img) {
372     if (!IsYUV420(img)) {
373         return false;
374     }
375     return (img->mPlane[1].mColInc == 2
376             && img->mPlane[2].mColInc == 2
377             && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1));
378 }
379 
IsNV21(const MediaImage2 * img)380 bool IsNV21(const MediaImage2 *img) {
381     if (!IsYUV420(img)) {
382         return false;
383     }
384     return (img->mPlane[1].mColInc == 2
385             && img->mPlane[2].mColInc == 2
386             && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1));
387 }
388 
IsI420(const MediaImage2 * img)389 bool IsI420(const MediaImage2 *img) {
390     if (!IsYUV420(img)) {
391         return false;
392     }
393     return (img->mPlane[1].mColInc == 1
394             && img->mPlane[2].mColInc == 1
395             && img->mPlane[2].mOffset > img->mPlane[1].mOffset);
396 }
397 
GetYuv420FlexibleLayout()398 FlexLayout GetYuv420FlexibleLayout() {
399     static FlexLayout sLayout = []{
400         AHardwareBuffer_Desc desc = {
401             16,  // width
402             16,  // height
403             1,   // layers
404             AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420,
405             AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
406             0,   // stride
407             0,   // rfu0
408             0,   // rfu1
409         };
410         AHardwareBuffer *buffer = nullptr;
411         int ret = AHardwareBuffer_allocate(&desc, &buffer);
412         if (ret != 0) {
413             return FLEX_LAYOUT_UNKNOWN;
414         }
415         class AutoCloser {
416         public:
417             AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {}
418             ~AutoCloser() {
419                 if (mLocked) {
420                     AHardwareBuffer_unlock(mBuffer, nullptr);
421                 }
422                 AHardwareBuffer_release(mBuffer);
423             }
424 
425             void setLocked() { mLocked = true; }
426 
427         private:
428             AHardwareBuffer *mBuffer;
429             bool mLocked;
430         } autoCloser(buffer);
431         AHardwareBuffer_Planes planes;
432         ret = AHardwareBuffer_lockPlanes(
433                 buffer,
434                 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
435                 -1,       // fence
436                 nullptr,  // rect
437                 &planes);
438         if (ret != 0) {
439             AHardwareBuffer_release(buffer);
440             return FLEX_LAYOUT_UNKNOWN;
441         }
442         autoCloser.setLocked();
443         if (planes.planeCount != 3) {
444             return FLEX_LAYOUT_UNKNOWN;
445         }
446         if (planes.planes[0].pixelStride != 1) {
447             return FLEX_LAYOUT_UNKNOWN;
448         }
449         if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) {
450             return FLEX_LAYOUT_PLANAR;
451         }
452         if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) {
453             ssize_t uvDist =
454                 static_cast<uint8_t *>(planes.planes[2].data) -
455                 static_cast<uint8_t *>(planes.planes[1].data);
456             if (uvDist == 1) {
457                 return FLEX_LAYOUT_SEMIPLANAR_UV;
458             } else if (uvDist == -1) {
459                 return FLEX_LAYOUT_SEMIPLANAR_VU;
460             }
461             return FLEX_LAYOUT_UNKNOWN;
462         }
463         return FLEX_LAYOUT_UNKNOWN;
464     }();
465     return sLayout;
466 }
467 
CreateYUV420PlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)468 MediaImage2 CreateYUV420PlanarMediaImage2(
469         uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
470     return MediaImage2 {
471         .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
472         .mNumPlanes = 3,
473         .mWidth = width,
474         .mHeight = height,
475         .mBitDepth = 8,
476         .mBitDepthAllocated = 8,
477         .mPlane = {
478             {
479                 .mOffset = 0,
480                 .mColInc = 1,
481                 .mRowInc = (int32_t)stride,
482                 .mHorizSubsampling = 1,
483                 .mVertSubsampling = 1,
484             },
485             {
486                 .mOffset = stride * vstride,
487                 .mColInc = 1,
488                 .mRowInc = (int32_t)stride / 2,
489                 .mHorizSubsampling = 2,
490                 .mVertSubsampling = 2,
491             },
492             {
493                 .mOffset = stride * vstride * 5 / 4,
494                 .mColInc = 1,
495                 .mRowInc = (int32_t)stride / 2,
496                 .mHorizSubsampling = 2,
497                 .mVertSubsampling = 2,
498             }
499         },
500     };
501 }
502 
CreateYUV420SemiPlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)503 MediaImage2 CreateYUV420SemiPlanarMediaImage2(
504         uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
505     return MediaImage2 {
506         .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
507         .mNumPlanes = 3,
508         .mWidth = width,
509         .mHeight = height,
510         .mBitDepth = 8,
511         .mBitDepthAllocated = 8,
512         .mPlane = {
513             {
514                 .mOffset = 0,
515                 .mColInc = 1,
516                 .mRowInc = (int32_t)stride,
517                 .mHorizSubsampling = 1,
518                 .mVertSubsampling = 1,
519             },
520             {
521                 .mOffset = stride * vstride,
522                 .mColInc = 2,
523                 .mRowInc = (int32_t)stride,
524                 .mHorizSubsampling = 2,
525                 .mVertSubsampling = 2,
526             },
527             {
528                 .mOffset = stride * vstride + 1,
529                 .mColInc = 2,
530                 .mRowInc = (int32_t)stride,
531                 .mHorizSubsampling = 2,
532                 .mVertSubsampling = 2,
533             }
534         },
535     };
536 }
537 
538 // Matrix coefficient to convert RGB to Planar YUV data.
539 // Each sub-array represents the 3X3 coeff used with R, G and B
540 static const int16_t bt601Matrix[2][3][3] = {
541     { { 77, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */
542     { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } },  /* RANGE_LIMITED */
543 };
544 
545 static const int16_t bt709Matrix[2][3][3] = {
546     // TRICKY: 18 is adjusted to 19 so that sum of row 1 is 256
547     { { 54, 183, 19 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */
548     // TRICKY: -87 is adjusted to -86 so that sum of row 2 is 0
549     { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */
550 };
551 
ConvertRGBToPlanarYUV(uint8_t * dstY,size_t dstStride,size_t dstVStride,size_t bufferSize,const C2GraphicView & src,C2Color::matrix_t colorMatrix,C2Color::range_t colorRange)552 status_t ConvertRGBToPlanarYUV(
553         uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
554         const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) {
555     CHECK(dstY != nullptr);
556     CHECK((src.width() & 1) == 0);
557     CHECK((src.height() & 1) == 0);
558 
559     if (dstStride * dstVStride * 3 / 2 > bufferSize) {
560         ALOGD("conversion buffer is too small for converting from RGB to YUV");
561         return NO_MEMORY;
562     }
563 
564     uint8_t *dstU = dstY + dstStride * dstVStride;
565     uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
566 
567     const C2PlanarLayout &layout = src.layout();
568     const uint8_t *pRed   = src.data()[C2PlanarLayout::PLANE_R];
569     const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
570     const uint8_t *pBlue  = src.data()[C2PlanarLayout::PLANE_B];
571 
572     // set default range as limited
573     if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) {
574         colorRange = C2Color::RANGE_LIMITED;
575     }
576     const int16_t (*weights)[3] =
577         (colorMatrix == C2Color::MATRIX_BT709) ?
578             bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1];
579     uint8_t zeroLvl =  colorRange == C2Color::RANGE_FULL ? 0 : 16;
580     uint8_t maxLvlLuma =  colorRange == C2Color::RANGE_FULL ? 255 : 235;
581     uint8_t maxLvlChroma =  colorRange == C2Color::RANGE_FULL ? 255 : 240;
582 
583 #define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max)))
584     for (size_t y = 0; y < src.height(); ++y) {
585         for (size_t x = 0; x < src.width(); ++x) {
586             uint8_t r = *pRed;
587             uint8_t g = *pGreen;
588             uint8_t b = *pBlue;
589 
590             unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) +
591                              zeroLvl;
592 
593             dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma);
594 
595             if ((x & 1) == 0 && (y & 1) == 0) {
596                 unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) +
597                               128;
598 
599                 unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) +
600                               128;
601 
602                 dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma);
603                 dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma);
604             }
605             pRed   += layout.planes[C2PlanarLayout::PLANE_R].colInc;
606             pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
607             pBlue  += layout.planes[C2PlanarLayout::PLANE_B].colInc;
608         }
609 
610         if ((y & 1) == 0) {
611             dstU += dstStride >> 1;
612             dstV += dstStride >> 1;
613         }
614 
615         pRed   -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
616         pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
617         pBlue  -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
618         pRed   += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
619         pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
620         pBlue  += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
621 
622         dstY += dstStride;
623     }
624     return OK;
625 }
626 
627 namespace {
628 
629 /**
630  * A block of raw allocated memory.
631  */
632 struct MemoryBlockPoolBlock {
MemoryBlockPoolBlockandroid::__anona09fa2f20311::MemoryBlockPoolBlock633     MemoryBlockPoolBlock(size_t size)
634         : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
635 
~MemoryBlockPoolBlockandroid::__anona09fa2f20311::MemoryBlockPoolBlock636     ~MemoryBlockPoolBlock() {
637         delete[] mData;
638     }
639 
dataandroid::__anona09fa2f20311::MemoryBlockPoolBlock640     const uint8_t *data() const {
641         return mData;
642     }
643 
sizeandroid::__anona09fa2f20311::MemoryBlockPoolBlock644     size_t size() const {
645         return mSize;
646     }
647 
648     C2_DO_NOT_COPY(MemoryBlockPoolBlock);
649 
650 private:
651     uint8_t *mData;
652     size_t mSize;
653 };
654 
655 /**
656  * A simple raw memory block pool implementation.
657  */
658 struct MemoryBlockPoolImpl {
releaseandroid::__anona09fa2f20311::MemoryBlockPoolImpl659     void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
660         std::lock_guard<std::mutex> lock(mMutex);
661         // return block to free blocks if it is the current size; otherwise, discard
662         if (block->size() == mCurrentSize) {
663             mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
664         } else {
665             mBlocksInUse.erase(block);
666         }
667     }
668 
fetchandroid::__anona09fa2f20311::MemoryBlockPoolImpl669     std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
670         std::lock_guard<std::mutex> lock(mMutex);
671         mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
672             return block.size() != size;
673         });
674         mCurrentSize = size;
675         if (mFreeBlocks.empty()) {
676             mBlocksInUse.emplace_front(size);
677         } else {
678             mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
679         }
680         return mBlocksInUse.begin();
681     }
682 
683     MemoryBlockPoolImpl() = default;
684 
685     C2_DO_NOT_COPY(MemoryBlockPoolImpl);
686 
687 private:
688     std::mutex mMutex;
689     std::list<MemoryBlockPoolBlock> mFreeBlocks;
690     std::list<MemoryBlockPoolBlock> mBlocksInUse;
691     size_t mCurrentSize;
692 };
693 
694 } // namespace
695 
696 struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
697 };
698 
699 struct MemoryBlock::Impl {
Implandroid::MemoryBlock::Impl700     Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
701          std::shared_ptr<MemoryBlockPoolImpl> pool)
702         : mBlock(block), mPool(pool) {
703     }
704 
~Implandroid::MemoryBlock::Impl705     ~Impl() {
706         mPool->release(mBlock);
707     }
708 
dataandroid::MemoryBlock::Impl709     const uint8_t *data() const {
710         return mBlock->data();
711     }
712 
sizeandroid::MemoryBlock::Impl713     size_t size() const {
714         return mBlock->size();
715     }
716 
717 private:
718     std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
719     std::shared_ptr<MemoryBlockPoolImpl> mPool;
720 };
721 
fetch(size_t size)722 MemoryBlock MemoryBlockPool::fetch(size_t size) {
723     std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
724     return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
725             poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
726 }
727 
MemoryBlockPool()728 MemoryBlockPool::MemoryBlockPool()
729     : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
730 }
731 
MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)732 MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
733     : mImpl(impl) {
734 }
735 
736 MemoryBlock::MemoryBlock() = default;
737 
738 MemoryBlock::~MemoryBlock() = default;
739 
data() const740 const uint8_t* MemoryBlock::data() const {
741     return mImpl ? mImpl->data() : nullptr;
742 }
743 
size() const744 size_t MemoryBlock::size() const {
745     return mImpl ? mImpl->size() : 0;
746 }
747 
Allocate(size_t size)748 MemoryBlock MemoryBlock::Allocate(size_t size) {
749     return MemoryBlockPool().fetch(size);
750 }
751 
752 }  // namespace android
753