• 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 #include <utils/Log.h>
20 
21 #include <libyuv.h>
22 
23 #include <list>
24 #include <mutex>
25 
26 #include <android/hardware_buffer.h>
27 #include <media/hardware/HardwareAPI.h>
28 #include <media/stagefright/foundation/AUtils.h>
29 
30 #include <C2Debug.h>
31 
32 #include "Codec2BufferUtils.h"
33 
34 namespace android {
35 
36 namespace {
37 
38 /**
39  * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) do not work as the results are
40  * always const.
41  */
42 template<bool ToA, size_t S>
43 struct MemCopier {
44     template<typename A, typename B>
copyandroid::__anonfee8894d0111::MemCopier45     inline static void copy(A *a, const B *b, size_t size) {
46         __builtin_memcpy(a, b, size);
47     }
48 };
49 
50 template<size_t S>
51 struct MemCopier<false, S> {
52     template<typename A, typename B>
copyandroid::__anonfee8894d0111::MemCopier53     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  */
66 template<bool ToMediaImage, typename View, typename ImagePixel>
_ImageCopy(View & view,const MediaImage2 * img,ImagePixel * imgBase)67 static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
68     // TODO: more efficient copying --- e.g. copy interleaved planes together, etc.
69     const C2PlanarLayout &layout = view.layout();
70     const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
71 
72     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;
90 
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;
101             }
102         } 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             }
114         }
115     }
116     return OK;
117 }
118 
119 }  // namespace
120 
ImageCopy(uint8_t * imgBase,const MediaImage2 * img,const C2GraphicView & view)121 status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
122     if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
123         return BAD_VALUE;
124     }
125     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 
140     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)) {
151             if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
152                                     dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
153                 return OK;
154             }
155         }
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)) {
174             if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
175                                     dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
176                 return OK;
177             }
178         } 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;
188         }
189     }
190     return _ImageCopy<true>(view, img, imgBase);
191 }
192 
ImageCopy(C2GraphicView & view,const uint8_t * imgBase,const MediaImage2 * img)193 status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
194     if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
195         return BAD_VALUE;
196     }
197     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;
211     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)) {
222             if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
223                                     dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
224                 return OK;
225             }
226         }
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)) {
245             if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
246                                     dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
247                 return OK;
248             }
249         } 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;
259         }
260     }
261     return _ImageCopy<false>(view, img, imgBase);
262 }
263 
IsYUV420(const C2GraphicView & view)264 bool 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 
IsNV12(const C2GraphicView & view)288 bool 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 
IsNV21(const C2GraphicView & view)302 bool 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 
IsI420(const C2GraphicView & view)316 bool 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 
IsYUV420(const MediaImage2 * img)330 bool 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 
IsNV12(const MediaImage2 * img)343 bool 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
349             && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1));
350 }
351 
IsNV21(const MediaImage2 * img)352 bool 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
358             && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1));
359 }
360 
IsI420(const MediaImage2 * img)361 bool 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 
GetYuv420FlexibleLayout()370 FlexLayout 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 
CreateYUV420PlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)440 MediaImage2 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 
CreateYUV420SemiPlanarMediaImage2(uint32_t width,uint32_t height,uint32_t stride,uint32_t vstride)475 MediaImage2 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 
510 // Matrix coefficient to convert RGB to Planar YUV data.
511 // Each sub-array represents the 3X3 coeff used with R, G and B
512 static const int16_t bt601Matrix[2][3][3] = {
513     { { 76, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */
514     { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } },  /* RANGE_LIMITED */
515 };
516 
517 static const int16_t bt709Matrix[2][3][3] = {
518     { { 54, 183, 18 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */
519     { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */
520 };
521 
ConvertRGBToPlanarYUV(uint8_t * dstY,size_t dstStride,size_t dstVStride,size_t bufferSize,const C2GraphicView & src,C2Color::matrix_t colorMatrix,C2Color::range_t colorRange)522 status_t ConvertRGBToPlanarYUV(
523         uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
524         const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) {
525     CHECK(dstY != nullptr);
526     CHECK((src.width() & 1) == 0);
527     CHECK((src.height() & 1) == 0);
528 
529     if (dstStride * dstVStride * 3 / 2 > bufferSize) {
530         ALOGD("conversion buffer is too small for converting from RGB to YUV");
531         return NO_MEMORY;
532     }
533 
534     uint8_t *dstU = dstY + dstStride * dstVStride;
535     uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
536 
537     const C2PlanarLayout &layout = src.layout();
538     const uint8_t *pRed   = src.data()[C2PlanarLayout::PLANE_R];
539     const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
540     const uint8_t *pBlue  = src.data()[C2PlanarLayout::PLANE_B];
541 
542     // set default range as limited
543     if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) {
544         colorRange = C2Color::RANGE_LIMITED;
545     }
546     const int16_t (*weights)[3] =
547         (colorMatrix == C2Color::MATRIX_BT709) ?
548             bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1];
549     uint8_t zeroLvl =  colorRange == C2Color::RANGE_FULL ? 0 : 16;
550     uint8_t maxLvlLuma =  colorRange == C2Color::RANGE_FULL ? 255 : 235;
551     uint8_t maxLvlChroma =  colorRange == C2Color::RANGE_FULL ? 255 : 240;
552 
553 #define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max)))
554     for (size_t y = 0; y < src.height(); ++y) {
555         for (size_t x = 0; x < src.width(); ++x) {
556             uint8_t r = *pRed;
557             uint8_t g = *pGreen;
558             uint8_t b = *pBlue;
559 
560             unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) +
561                              zeroLvl;
562 
563             dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma);
564 
565             if ((x & 1) == 0 && (y & 1) == 0) {
566                 unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) +
567                               128;
568 
569                 unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) +
570                               128;
571 
572                 dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma);
573                 dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma);
574             }
575             pRed   += layout.planes[C2PlanarLayout::PLANE_R].colInc;
576             pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
577             pBlue  += layout.planes[C2PlanarLayout::PLANE_B].colInc;
578         }
579 
580         if ((y & 1) == 0) {
581             dstU += dstStride >> 1;
582             dstV += dstStride >> 1;
583         }
584 
585         pRed   -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
586         pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
587         pBlue  -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
588         pRed   += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
589         pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
590         pBlue  += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
591 
592         dstY += dstStride;
593     }
594     return OK;
595 }
596 
597 namespace {
598 
599 /**
600  * A block of raw allocated memory.
601  */
602 struct MemoryBlockPoolBlock {
MemoryBlockPoolBlockandroid::__anonfee8894d0311::MemoryBlockPoolBlock603     MemoryBlockPoolBlock(size_t size)
604         : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
605 
~MemoryBlockPoolBlockandroid::__anonfee8894d0311::MemoryBlockPoolBlock606     ~MemoryBlockPoolBlock() {
607         delete[] mData;
608     }
609 
dataandroid::__anonfee8894d0311::MemoryBlockPoolBlock610     const uint8_t *data() const {
611         return mData;
612     }
613 
sizeandroid::__anonfee8894d0311::MemoryBlockPoolBlock614     size_t size() const {
615         return mSize;
616     }
617 
618     C2_DO_NOT_COPY(MemoryBlockPoolBlock);
619 
620 private:
621     uint8_t *mData;
622     size_t mSize;
623 };
624 
625 /**
626  * A simple raw memory block pool implementation.
627  */
628 struct MemoryBlockPoolImpl {
releaseandroid::__anonfee8894d0311::MemoryBlockPoolImpl629     void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
630         std::lock_guard<std::mutex> lock(mMutex);
631         // return block to free blocks if it is the current size; otherwise, discard
632         if (block->size() == mCurrentSize) {
633             mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
634         } else {
635             mBlocksInUse.erase(block);
636         }
637     }
638 
fetchandroid::__anonfee8894d0311::MemoryBlockPoolImpl639     std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
640         std::lock_guard<std::mutex> lock(mMutex);
641         mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
642             return block.size() != size;
643         });
644         mCurrentSize = size;
645         if (mFreeBlocks.empty()) {
646             mBlocksInUse.emplace_front(size);
647         } else {
648             mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
649         }
650         return mBlocksInUse.begin();
651     }
652 
653     MemoryBlockPoolImpl() = default;
654 
655     C2_DO_NOT_COPY(MemoryBlockPoolImpl);
656 
657 private:
658     std::mutex mMutex;
659     std::list<MemoryBlockPoolBlock> mFreeBlocks;
660     std::list<MemoryBlockPoolBlock> mBlocksInUse;
661     size_t mCurrentSize;
662 };
663 
664 } // namespace
665 
666 struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
667 };
668 
669 struct MemoryBlock::Impl {
Implandroid::MemoryBlock::Impl670     Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
671          std::shared_ptr<MemoryBlockPoolImpl> pool)
672         : mBlock(block), mPool(pool) {
673     }
674 
~Implandroid::MemoryBlock::Impl675     ~Impl() {
676         mPool->release(mBlock);
677     }
678 
dataandroid::MemoryBlock::Impl679     const uint8_t *data() const {
680         return mBlock->data();
681     }
682 
sizeandroid::MemoryBlock::Impl683     size_t size() const {
684         return mBlock->size();
685     }
686 
687 private:
688     std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
689     std::shared_ptr<MemoryBlockPoolImpl> mPool;
690 };
691 
fetch(size_t size)692 MemoryBlock MemoryBlockPool::fetch(size_t size) {
693     std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
694     return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
695             poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
696 }
697 
MemoryBlockPool()698 MemoryBlockPool::MemoryBlockPool()
699     : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
700 }
701 
MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)702 MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
703     : mImpl(impl) {
704 }
705 
706 MemoryBlock::MemoryBlock() = default;
707 
708 MemoryBlock::~MemoryBlock() = default;
709 
data() const710 const uint8_t* MemoryBlock::data() const {
711     return mImpl ? mImpl->data() : nullptr;
712 }
713 
size() const714 size_t MemoryBlock::size() const {
715     return mImpl ? mImpl->size() : 0;
716 }
717 
Allocate(size_t size)718 MemoryBlock MemoryBlock::Allocate(size_t size) {
719     return MemoryBlockPool().fetch(size);
720 }
721 
722 }  // namespace android
723