Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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" |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 19 | #define ATRACE_TAG ATRACE_TAG_VIDEO |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 20 | #include <utils/Log.h> |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 21 | #include <utils/Trace.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 22 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 23 | #include <libyuv.h> |
| 24 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 25 | #include <list> |
| 26 | #include <mutex> |
| 27 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 28 | #include <android/hardware_buffer.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 29 | #include <media/hardware/HardwareAPI.h> |
Wonsik Kim | 3ce32b1 | 2024-08-14 00:17:48 +0000 | [diff] [blame] | 30 | #include <media/stagefright/foundation/ABuffer.h> |
| 31 | #include <media/stagefright/foundation/AMessage.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 32 | #include <media/stagefright/foundation/AUtils.h> |
Wonsik Kim | 3ce32b1 | 2024-08-14 00:17:48 +0000 | [diff] [blame] | 33 | #include <media/stagefright/MediaCodecConstants.h> |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 34 | |
| 35 | #include <C2Debug.h> |
| 36 | |
| 37 | #include "Codec2BufferUtils.h" |
| 38 | |
| 39 | namespace android { |
| 40 | |
| 41 | namespace { |
| 42 | |
| 43 | /** |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 44 | * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) |
| 45 | * do not work as the results are always const. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 46 | */ |
| 47 | template<bool ToA, size_t S> |
| 48 | struct MemCopier { |
| 49 | template<typename A, typename B> |
| 50 | inline static void copy(A *a, const B *b, size_t size) { |
| 51 | __builtin_memcpy(a, b, size); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | template<size_t S> |
| 56 | struct MemCopier<false, S> { |
| 57 | template<typename A, typename B> |
| 58 | inline static void copy(const A *a, B *b, size_t size) { |
| 59 | MemCopier<true, S>::copy(b, a, size); |
| 60 | } |
| 61 | }; |
| 62 | |
| 63 | /** |
| 64 | * Copies between a MediaImage and a graphic view. |
| 65 | * |
| 66 | * \param ToMediaImage whether to copy to (or from) the MediaImage |
| 67 | * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction) |
| 68 | * \param img MediaImage data |
| 69 | * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction) |
| 70 | */ |
| 71 | template<bool ToMediaImage, typename View, typename ImagePixel> |
| 72 | static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 73 | // TODO: more efficient copying --- e.g. copy interleaved planes together, etc. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 74 | const C2PlanarLayout &layout = view.layout(); |
| 75 | const size_t bpp = divUp(img->mBitDepthAllocated, 8u); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 76 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 77 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 78 | typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow = |
| 79 | imgBase + img->mPlane[i].mOffset; |
| 80 | typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow = |
| 81 | viewRow = view.data()[i]; |
| 82 | const C2PlaneInfo &plane = layout.planes[i]; |
| 83 | if (plane.colSampling != img->mPlane[i].mHorizSubsampling |
| 84 | || plane.rowSampling != img->mPlane[i].mVertSubsampling |
| 85 | || plane.allocatedDepth != img->mBitDepthAllocated |
| 86 | || plane.allocatedDepth < plane.bitDepth |
| 87 | // MediaImage only supports MSB values |
| 88 | || plane.rightShift != plane.allocatedDepth - plane.bitDepth |
| 89 | || (bpp > 1 && plane.endianness != plane.NATIVE)) { |
| 90 | return BAD_VALUE; |
| 91 | } |
| 92 | |
| 93 | uint32_t planeW = img->mWidth / plane.colSampling; |
| 94 | uint32_t planeH = img->mHeight / plane.rowSampling; |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 95 | |
Praveen Chavan | bc2b713 | 2022-01-19 23:02:56 -0800 | [diff] [blame] | 96 | bool canCopyByRow = (plane.colInc == bpp) && (img->mPlane[i].mColInc == bpp); |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 97 | bool canCopyByPlane = canCopyByRow && (plane.rowInc == img->mPlane[i].mRowInc); |
| 98 | if (canCopyByPlane) { |
| 99 | MemCopier<ToMediaImage, 0>::copy(imgRow, viewRow, plane.rowInc * planeH); |
| 100 | } else if (canCopyByRow) { |
| 101 | for (uint32_t row = 0; row < planeH; ++row) { |
| 102 | MemCopier<ToMediaImage, 0>::copy( |
| 103 | imgRow, viewRow, std::min(plane.rowInc, img->mPlane[i].mRowInc)); |
| 104 | imgRow += img->mPlane[i].mRowInc; |
| 105 | viewRow += plane.rowInc; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 106 | } |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 107 | } else { |
| 108 | for (uint32_t row = 0; row < planeH; ++row) { |
| 109 | decltype(imgRow) imgPtr = imgRow; |
| 110 | decltype(viewRow) viewPtr = viewRow; |
| 111 | for (uint32_t col = 0; col < planeW; ++col) { |
| 112 | MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp); |
| 113 | imgPtr += img->mPlane[i].mColInc; |
| 114 | viewPtr += plane.colInc; |
| 115 | } |
| 116 | imgRow += img->mPlane[i].mRowInc; |
| 117 | viewRow += plane.rowInc; |
| 118 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | return OK; |
| 122 | } |
| 123 | |
| 124 | } // namespace |
| 125 | |
| 126 | status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) { |
Arun Johnson | 3ab32cd | 2022-06-10 18:58:01 +0000 | [diff] [blame] | 127 | if (img == nullptr |
| 128 | || imgBase == nullptr |
| 129 | || view.crop().width != img->mWidth |
| 130 | || view.crop().height != img->mHeight) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 131 | return BAD_VALUE; |
| 132 | } |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 133 | const uint8_t* src_y = view.data()[0]; |
| 134 | const uint8_t* src_u = view.data()[1]; |
| 135 | const uint8_t* src_v = view.data()[2]; |
| 136 | int32_t src_stride_y = view.layout().planes[0].rowInc; |
| 137 | int32_t src_stride_u = view.layout().planes[1].rowInc; |
| 138 | int32_t src_stride_v = view.layout().planes[2].rowInc; |
| 139 | uint8_t* dst_y = imgBase + img->mPlane[0].mOffset; |
| 140 | uint8_t* dst_u = imgBase + img->mPlane[1].mOffset; |
| 141 | uint8_t* dst_v = imgBase + img->mPlane[2].mOffset; |
| 142 | int32_t dst_stride_y = img->mPlane[0].mRowInc; |
| 143 | int32_t dst_stride_u = img->mPlane[1].mRowInc; |
| 144 | int32_t dst_stride_v = img->mPlane[2].mRowInc; |
| 145 | int width = view.crop().width; |
| 146 | int height = view.crop().height; |
| 147 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 148 | if (IsNV12(view)) { |
| 149 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 150 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 151 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 152 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2); |
| 153 | return OK; |
| 154 | } else if (IsNV21(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 155 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 156 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u, |
| 157 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 158 | return OK; |
| 159 | } |
| 160 | } else if (IsI420(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 161 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 162 | if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 163 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 164 | return OK; |
| 165 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 166 | } |
| 167 | } else if (IsNV21(view)) { |
| 168 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 169 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 170 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v, |
| 171 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
| 172 | return OK; |
| 173 | } |
| 174 | } else if (IsNV21(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 175 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 176 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 177 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2); |
| 178 | return OK; |
| 179 | } else if (IsI420(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 180 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 181 | if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y, |
| 182 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
| 183 | return OK; |
| 184 | } |
| 185 | } |
| 186 | } else if (IsI420(view)) { |
| 187 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 188 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 189 | if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 190 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 191 | return OK; |
| 192 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 193 | } else if (IsNV21(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 194 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 195 | if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
| 196 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 197 | return OK; |
| 198 | } |
| 199 | } else if (IsI420(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 200 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 201 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 202 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2); |
| 203 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2); |
| 204 | return OK; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 207 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 208 | return _ImageCopy<true>(view, img, imgBase); |
| 209 | } |
| 210 | |
| 211 | status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) { |
Arun Johnson | 3ab32cd | 2022-06-10 18:58:01 +0000 | [diff] [blame] | 212 | if (img == nullptr |
| 213 | || imgBase == nullptr |
| 214 | || view.crop().width != img->mWidth |
| 215 | || view.crop().height != img->mHeight) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 216 | return BAD_VALUE; |
| 217 | } |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 218 | const uint8_t* src_y = imgBase + img->mPlane[0].mOffset; |
| 219 | const uint8_t* src_u = imgBase + img->mPlane[1].mOffset; |
| 220 | const uint8_t* src_v = imgBase + img->mPlane[2].mOffset; |
| 221 | int32_t src_stride_y = img->mPlane[0].mRowInc; |
| 222 | int32_t src_stride_u = img->mPlane[1].mRowInc; |
| 223 | int32_t src_stride_v = img->mPlane[2].mRowInc; |
| 224 | uint8_t* dst_y = view.data()[0]; |
| 225 | uint8_t* dst_u = view.data()[1]; |
| 226 | uint8_t* dst_v = view.data()[2]; |
| 227 | int32_t dst_stride_y = view.layout().planes[0].rowInc; |
| 228 | int32_t dst_stride_u = view.layout().planes[1].rowInc; |
| 229 | int32_t dst_stride_v = view.layout().planes[2].rowInc; |
| 230 | int width = view.crop().width; |
| 231 | int height = view.crop().height; |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 232 | if (IsNV12(img)) { |
| 233 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 234 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 235 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 236 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2); |
| 237 | return OK; |
| 238 | } else if (IsNV21(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 239 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 240 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u, |
| 241 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 242 | return OK; |
| 243 | } |
| 244 | } else if (IsI420(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 245 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 246 | if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 247 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 248 | return OK; |
| 249 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 250 | } |
| 251 | } else if (IsNV21(img)) { |
| 252 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 253 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 254 | if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v, |
| 255 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
| 256 | return OK; |
| 257 | } |
| 258 | } else if (IsNV21(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 259 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 260 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 261 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2); |
| 262 | return OK; |
| 263 | } else if (IsI420(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 264 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 265 | if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y, |
| 266 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
| 267 | return OK; |
| 268 | } |
| 269 | } |
| 270 | } else if (IsI420(img)) { |
| 271 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 272 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 273 | if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 274 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 275 | return OK; |
| 276 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 277 | } else if (IsNV21(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 278 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 279 | if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, |
| 280 | dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) { |
| 281 | return OK; |
| 282 | } |
| 283 | } else if (IsI420(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 284 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 285 | libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height); |
| 286 | libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2); |
| 287 | libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2); |
| 288 | return OK; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 289 | } |
| 290 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 291 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 292 | return _ImageCopy<false>(view, img, imgBase); |
| 293 | } |
| 294 | |
| 295 | bool IsYUV420(const C2GraphicView &view) { |
| 296 | const C2PlanarLayout &layout = view.layout(); |
| 297 | return (layout.numPlanes == 3 |
| 298 | && layout.type == C2PlanarLayout::TYPE_YUV |
| 299 | && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y |
| 300 | && layout.planes[layout.PLANE_Y].allocatedDepth == 8 |
| 301 | && layout.planes[layout.PLANE_Y].bitDepth == 8 |
| 302 | && layout.planes[layout.PLANE_Y].rightShift == 0 |
| 303 | && layout.planes[layout.PLANE_Y].colSampling == 1 |
| 304 | && layout.planes[layout.PLANE_Y].rowSampling == 1 |
| 305 | && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB |
| 306 | && layout.planes[layout.PLANE_U].allocatedDepth == 8 |
| 307 | && layout.planes[layout.PLANE_U].bitDepth == 8 |
| 308 | && layout.planes[layout.PLANE_U].rightShift == 0 |
| 309 | && layout.planes[layout.PLANE_U].colSampling == 2 |
| 310 | && layout.planes[layout.PLANE_U].rowSampling == 2 |
| 311 | && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR |
| 312 | && layout.planes[layout.PLANE_V].allocatedDepth == 8 |
| 313 | && layout.planes[layout.PLANE_V].bitDepth == 8 |
| 314 | && layout.planes[layout.PLANE_V].rightShift == 0 |
| 315 | && layout.planes[layout.PLANE_V].colSampling == 2 |
| 316 | && layout.planes[layout.PLANE_V].rowSampling == 2); |
| 317 | } |
| 318 | |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 319 | bool IsYUV420_10bit(const C2GraphicView &view) { |
| 320 | const C2PlanarLayout &layout = view.layout(); |
| 321 | return (layout.numPlanes == 3 |
| 322 | && layout.type == C2PlanarLayout::TYPE_YUV |
| 323 | && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y |
| 324 | && layout.planes[layout.PLANE_Y].allocatedDepth == 16 |
| 325 | && layout.planes[layout.PLANE_Y].bitDepth == 10 |
| 326 | && layout.planes[layout.PLANE_Y].colSampling == 1 |
| 327 | && layout.planes[layout.PLANE_Y].rowSampling == 1 |
| 328 | && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB |
| 329 | && layout.planes[layout.PLANE_U].allocatedDepth == 16 |
| 330 | && layout.planes[layout.PLANE_U].bitDepth == 10 |
| 331 | && layout.planes[layout.PLANE_U].colSampling == 2 |
| 332 | && layout.planes[layout.PLANE_U].rowSampling == 2 |
| 333 | && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR |
| 334 | && layout.planes[layout.PLANE_V].allocatedDepth == 16 |
| 335 | && layout.planes[layout.PLANE_V].bitDepth == 10 |
| 336 | && layout.planes[layout.PLANE_V].colSampling == 2 |
| 337 | && layout.planes[layout.PLANE_V].rowSampling == 2); |
| 338 | } |
| 339 | |
| 340 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 341 | bool IsNV12(const C2GraphicView &view) { |
| 342 | if (!IsYUV420(view)) { |
| 343 | return false; |
| 344 | } |
| 345 | const C2PlanarLayout &layout = view.layout(); |
| 346 | return (layout.rootPlanes == 2 |
| 347 | && layout.planes[layout.PLANE_U].colInc == 2 |
| 348 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 349 | && layout.planes[layout.PLANE_U].offset == 0 |
| 350 | && layout.planes[layout.PLANE_V].colInc == 2 |
| 351 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U |
| 352 | && layout.planes[layout.PLANE_V].offset == 1); |
| 353 | } |
| 354 | |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame] | 355 | bool IsP010(const C2GraphicView &view) { |
| 356 | if (!IsYUV420_10bit(view)) { |
| 357 | return false; |
| 358 | } |
| 359 | const C2PlanarLayout &layout = view.layout(); |
| 360 | return (layout.rootPlanes == 2 |
| 361 | && layout.planes[layout.PLANE_U].colInc == 4 |
| 362 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 363 | && layout.planes[layout.PLANE_U].offset == 0 |
| 364 | && layout.planes[layout.PLANE_V].colInc == 4 |
| 365 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U |
| 366 | && layout.planes[layout.PLANE_V].offset == 2 |
| 367 | && layout.planes[layout.PLANE_Y].rightShift == 6 |
| 368 | && layout.planes[layout.PLANE_U].rightShift == 6 |
| 369 | && layout.planes[layout.PLANE_V].rightShift == 6); |
| 370 | } |
| 371 | |
| 372 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 373 | bool IsNV21(const C2GraphicView &view) { |
| 374 | if (!IsYUV420(view)) { |
| 375 | return false; |
| 376 | } |
| 377 | const C2PlanarLayout &layout = view.layout(); |
| 378 | return (layout.rootPlanes == 2 |
| 379 | && layout.planes[layout.PLANE_U].colInc == 2 |
| 380 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V |
| 381 | && layout.planes[layout.PLANE_U].offset == 1 |
| 382 | && layout.planes[layout.PLANE_V].colInc == 2 |
| 383 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V |
| 384 | && layout.planes[layout.PLANE_V].offset == 0); |
| 385 | } |
| 386 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 387 | bool IsI420(const C2GraphicView &view) { |
| 388 | if (!IsYUV420(view)) { |
| 389 | return false; |
| 390 | } |
| 391 | const C2PlanarLayout &layout = view.layout(); |
| 392 | return (layout.rootPlanes == 3 |
| 393 | && layout.planes[layout.PLANE_U].colInc == 1 |
| 394 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 395 | && layout.planes[layout.PLANE_U].offset == 0 |
| 396 | && layout.planes[layout.PLANE_V].colInc == 1 |
| 397 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V |
| 398 | && layout.planes[layout.PLANE_V].offset == 0); |
| 399 | } |
| 400 | |
| 401 | bool IsYUV420(const MediaImage2 *img) { |
| 402 | return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV |
| 403 | && img->mNumPlanes == 3 |
| 404 | && img->mBitDepth == 8 |
| 405 | && img->mBitDepthAllocated == 8 |
| 406 | && img->mPlane[0].mHorizSubsampling == 1 |
| 407 | && img->mPlane[0].mVertSubsampling == 1 |
| 408 | && img->mPlane[1].mHorizSubsampling == 2 |
| 409 | && img->mPlane[1].mVertSubsampling == 2 |
| 410 | && img->mPlane[2].mHorizSubsampling == 2 |
| 411 | && img->mPlane[2].mVertSubsampling == 2); |
| 412 | } |
| 413 | |
| 414 | bool IsNV12(const MediaImage2 *img) { |
| 415 | if (!IsYUV420(img)) { |
| 416 | return false; |
| 417 | } |
| 418 | return (img->mPlane[1].mColInc == 2 |
| 419 | && img->mPlane[2].mColInc == 2 |
Harish Mahendrakar | cd59f03 | 2021-04-16 18:55:41 -0700 | [diff] [blame] | 420 | && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1)); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 423 | bool IsNV21(const MediaImage2 *img) { |
| 424 | if (!IsYUV420(img)) { |
| 425 | return false; |
| 426 | } |
| 427 | return (img->mPlane[1].mColInc == 2 |
| 428 | && img->mPlane[2].mColInc == 2 |
Harish Mahendrakar | cd59f03 | 2021-04-16 18:55:41 -0700 | [diff] [blame] | 429 | && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1)); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 432 | bool IsI420(const MediaImage2 *img) { |
| 433 | if (!IsYUV420(img)) { |
| 434 | return false; |
| 435 | } |
| 436 | return (img->mPlane[1].mColInc == 1 |
| 437 | && img->mPlane[2].mColInc == 1 |
| 438 | && img->mPlane[2].mOffset > img->mPlane[1].mOffset); |
| 439 | } |
| 440 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 441 | FlexLayout GetYuv420FlexibleLayout() { |
| 442 | static FlexLayout sLayout = []{ |
| 443 | AHardwareBuffer_Desc desc = { |
| 444 | 16, // width |
| 445 | 16, // height |
| 446 | 1, // layers |
| 447 | AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420, |
| 448 | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, |
| 449 | 0, // stride |
| 450 | 0, // rfu0 |
| 451 | 0, // rfu1 |
| 452 | }; |
| 453 | AHardwareBuffer *buffer = nullptr; |
| 454 | int ret = AHardwareBuffer_allocate(&desc, &buffer); |
| 455 | if (ret != 0) { |
| 456 | return FLEX_LAYOUT_UNKNOWN; |
| 457 | } |
| 458 | class AutoCloser { |
| 459 | public: |
| 460 | AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {} |
| 461 | ~AutoCloser() { |
| 462 | if (mLocked) { |
| 463 | AHardwareBuffer_unlock(mBuffer, nullptr); |
| 464 | } |
| 465 | AHardwareBuffer_release(mBuffer); |
| 466 | } |
| 467 | |
| 468 | void setLocked() { mLocked = true; } |
| 469 | |
| 470 | private: |
| 471 | AHardwareBuffer *mBuffer; |
| 472 | bool mLocked; |
| 473 | } autoCloser(buffer); |
| 474 | AHardwareBuffer_Planes planes; |
| 475 | ret = AHardwareBuffer_lockPlanes( |
| 476 | buffer, |
| 477 | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, |
| 478 | -1, // fence |
| 479 | nullptr, // rect |
| 480 | &planes); |
| 481 | if (ret != 0) { |
| 482 | AHardwareBuffer_release(buffer); |
| 483 | return FLEX_LAYOUT_UNKNOWN; |
| 484 | } |
| 485 | autoCloser.setLocked(); |
| 486 | if (planes.planeCount != 3) { |
| 487 | return FLEX_LAYOUT_UNKNOWN; |
| 488 | } |
| 489 | if (planes.planes[0].pixelStride != 1) { |
| 490 | return FLEX_LAYOUT_UNKNOWN; |
| 491 | } |
| 492 | if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) { |
| 493 | return FLEX_LAYOUT_PLANAR; |
| 494 | } |
| 495 | if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) { |
| 496 | ssize_t uvDist = |
| 497 | static_cast<uint8_t *>(planes.planes[2].data) - |
| 498 | static_cast<uint8_t *>(planes.planes[1].data); |
| 499 | if (uvDist == 1) { |
| 500 | return FLEX_LAYOUT_SEMIPLANAR_UV; |
| 501 | } else if (uvDist == -1) { |
| 502 | return FLEX_LAYOUT_SEMIPLANAR_VU; |
| 503 | } |
| 504 | return FLEX_LAYOUT_UNKNOWN; |
| 505 | } |
| 506 | return FLEX_LAYOUT_UNKNOWN; |
| 507 | }(); |
| 508 | return sLayout; |
| 509 | } |
| 510 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 511 | MediaImage2 CreateYUV420PlanarMediaImage2( |
| 512 | uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) { |
| 513 | return MediaImage2 { |
| 514 | .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV, |
| 515 | .mNumPlanes = 3, |
| 516 | .mWidth = width, |
| 517 | .mHeight = height, |
| 518 | .mBitDepth = 8, |
| 519 | .mBitDepthAllocated = 8, |
| 520 | .mPlane = { |
| 521 | { |
| 522 | .mOffset = 0, |
| 523 | .mColInc = 1, |
| 524 | .mRowInc = (int32_t)stride, |
| 525 | .mHorizSubsampling = 1, |
| 526 | .mVertSubsampling = 1, |
| 527 | }, |
| 528 | { |
| 529 | .mOffset = stride * vstride, |
| 530 | .mColInc = 1, |
| 531 | .mRowInc = (int32_t)stride / 2, |
| 532 | .mHorizSubsampling = 2, |
| 533 | .mVertSubsampling = 2, |
| 534 | }, |
| 535 | { |
| 536 | .mOffset = stride * vstride * 5 / 4, |
| 537 | .mColInc = 1, |
| 538 | .mRowInc = (int32_t)stride / 2, |
| 539 | .mHorizSubsampling = 2, |
| 540 | .mVertSubsampling = 2, |
| 541 | } |
| 542 | }, |
| 543 | }; |
| 544 | } |
| 545 | |
| 546 | MediaImage2 CreateYUV420SemiPlanarMediaImage2( |
| 547 | uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) { |
| 548 | return MediaImage2 { |
| 549 | .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV, |
| 550 | .mNumPlanes = 3, |
| 551 | .mWidth = width, |
| 552 | .mHeight = height, |
| 553 | .mBitDepth = 8, |
| 554 | .mBitDepthAllocated = 8, |
| 555 | .mPlane = { |
| 556 | { |
| 557 | .mOffset = 0, |
| 558 | .mColInc = 1, |
| 559 | .mRowInc = (int32_t)stride, |
| 560 | .mHorizSubsampling = 1, |
| 561 | .mVertSubsampling = 1, |
| 562 | }, |
| 563 | { |
| 564 | .mOffset = stride * vstride, |
| 565 | .mColInc = 2, |
| 566 | .mRowInc = (int32_t)stride, |
| 567 | .mHorizSubsampling = 2, |
| 568 | .mVertSubsampling = 2, |
| 569 | }, |
| 570 | { |
| 571 | .mOffset = stride * vstride + 1, |
| 572 | .mColInc = 2, |
| 573 | .mRowInc = (int32_t)stride, |
| 574 | .mHorizSubsampling = 2, |
| 575 | .mVertSubsampling = 2, |
| 576 | } |
| 577 | }, |
| 578 | }; |
| 579 | } |
| 580 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 581 | // Matrix coefficient to convert RGB to Planar YUV data. |
| 582 | // Each sub-array represents the 3X3 coeff used with R, G and B |
| 583 | static const int16_t bt601Matrix[2][3][3] = { |
Lajos Molnar | d842c0d | 2022-06-01 14:37:55 -0700 | [diff] [blame] | 584 | { { 77, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */ |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 585 | { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } }, /* RANGE_LIMITED */ |
| 586 | }; |
| 587 | |
| 588 | static const int16_t bt709Matrix[2][3][3] = { |
Lajos Molnar | d842c0d | 2022-06-01 14:37:55 -0700 | [diff] [blame] | 589 | // TRICKY: 18 is adjusted to 19 so that sum of row 1 is 256 |
| 590 | { { 54, 183, 19 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */ |
| 591 | // TRICKY: -87 is adjusted to -86 so that sum of row 2 is 0 |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 592 | { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */ |
| 593 | }; |
| 594 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 595 | status_t ConvertRGBToPlanarYUV( |
| 596 | uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize, |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 597 | const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 598 | CHECK(dstY != nullptr); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 599 | |
| 600 | if (dstStride * dstVStride * 3 / 2 > bufferSize) { |
| 601 | ALOGD("conversion buffer is too small for converting from RGB to YUV"); |
| 602 | return NO_MEMORY; |
| 603 | } |
| 604 | |
| 605 | uint8_t *dstU = dstY + dstStride * dstVStride; |
| 606 | uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1); |
| 607 | |
| 608 | const C2PlanarLayout &layout = src.layout(); |
| 609 | const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R]; |
| 610 | const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G]; |
| 611 | const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B]; |
| 612 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 613 | // set default range as limited |
| 614 | if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) { |
| 615 | colorRange = C2Color::RANGE_LIMITED; |
| 616 | } |
| 617 | const int16_t (*weights)[3] = |
| 618 | (colorMatrix == C2Color::MATRIX_BT709) ? |
| 619 | bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1]; |
| 620 | uint8_t zeroLvl = colorRange == C2Color::RANGE_FULL ? 0 : 16; |
| 621 | uint8_t maxLvlLuma = colorRange == C2Color::RANGE_FULL ? 255 : 235; |
| 622 | uint8_t maxLvlChroma = colorRange == C2Color::RANGE_FULL ? 255 : 240; |
| 623 | |
| 624 | #define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max))) |
Harish Mahendrakar | 4eba80f | 2023-08-28 17:35:56 +0000 | [diff] [blame] | 625 | for (size_t y = 0; y < src.crop().height; ++y) { |
| 626 | for (size_t x = 0; x < src.crop().width; ++x) { |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 627 | uint8_t r = *pRed; |
| 628 | uint8_t g = *pGreen; |
| 629 | uint8_t b = *pBlue; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 630 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 631 | unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) + |
| 632 | zeroLvl; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 633 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 634 | dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 635 | |
| 636 | if ((x & 1) == 0 && (y & 1) == 0) { |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 637 | unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) + |
| 638 | 128; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 639 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 640 | unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) + |
| 641 | 128; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 642 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 643 | dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma); |
| 644 | dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 645 | } |
| 646 | pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc; |
| 647 | pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc; |
| 648 | pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc; |
| 649 | } |
| 650 | |
| 651 | if ((y & 1) == 0) { |
| 652 | dstU += dstStride >> 1; |
| 653 | dstV += dstStride >> 1; |
| 654 | } |
| 655 | |
| 656 | pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width(); |
| 657 | pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width(); |
| 658 | pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width(); |
| 659 | pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc; |
| 660 | pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc; |
| 661 | pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc; |
| 662 | |
| 663 | dstY += dstStride; |
| 664 | } |
| 665 | return OK; |
| 666 | } |
| 667 | |
| 668 | namespace { |
| 669 | |
| 670 | /** |
| 671 | * A block of raw allocated memory. |
| 672 | */ |
| 673 | struct MemoryBlockPoolBlock { |
| 674 | MemoryBlockPoolBlock(size_t size) |
| 675 | : mData(new uint8_t[size]), mSize(mData ? size : 0) { } |
| 676 | |
| 677 | ~MemoryBlockPoolBlock() { |
| 678 | delete[] mData; |
| 679 | } |
| 680 | |
| 681 | const uint8_t *data() const { |
| 682 | return mData; |
| 683 | } |
| 684 | |
| 685 | size_t size() const { |
| 686 | return mSize; |
| 687 | } |
| 688 | |
| 689 | C2_DO_NOT_COPY(MemoryBlockPoolBlock); |
| 690 | |
| 691 | private: |
| 692 | uint8_t *mData; |
| 693 | size_t mSize; |
| 694 | }; |
| 695 | |
| 696 | /** |
| 697 | * A simple raw memory block pool implementation. |
| 698 | */ |
| 699 | struct MemoryBlockPoolImpl { |
| 700 | void release(std::list<MemoryBlockPoolBlock>::const_iterator block) { |
| 701 | std::lock_guard<std::mutex> lock(mMutex); |
| 702 | // return block to free blocks if it is the current size; otherwise, discard |
| 703 | if (block->size() == mCurrentSize) { |
| 704 | mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block); |
| 705 | } else { |
| 706 | mBlocksInUse.erase(block); |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) { |
| 711 | std::lock_guard<std::mutex> lock(mMutex); |
| 712 | mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool { |
| 713 | return block.size() != size; |
| 714 | }); |
| 715 | mCurrentSize = size; |
| 716 | if (mFreeBlocks.empty()) { |
| 717 | mBlocksInUse.emplace_front(size); |
| 718 | } else { |
| 719 | mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin()); |
| 720 | } |
| 721 | return mBlocksInUse.begin(); |
| 722 | } |
| 723 | |
| 724 | MemoryBlockPoolImpl() = default; |
| 725 | |
| 726 | C2_DO_NOT_COPY(MemoryBlockPoolImpl); |
| 727 | |
| 728 | private: |
| 729 | std::mutex mMutex; |
| 730 | std::list<MemoryBlockPoolBlock> mFreeBlocks; |
| 731 | std::list<MemoryBlockPoolBlock> mBlocksInUse; |
| 732 | size_t mCurrentSize; |
| 733 | }; |
| 734 | |
| 735 | } // namespace |
| 736 | |
| 737 | struct MemoryBlockPool::Impl : MemoryBlockPoolImpl { |
| 738 | }; |
| 739 | |
| 740 | struct MemoryBlock::Impl { |
| 741 | Impl(std::list<MemoryBlockPoolBlock>::const_iterator block, |
| 742 | std::shared_ptr<MemoryBlockPoolImpl> pool) |
| 743 | : mBlock(block), mPool(pool) { |
| 744 | } |
| 745 | |
| 746 | ~Impl() { |
| 747 | mPool->release(mBlock); |
| 748 | } |
| 749 | |
| 750 | const uint8_t *data() const { |
| 751 | return mBlock->data(); |
| 752 | } |
| 753 | |
| 754 | size_t size() const { |
| 755 | return mBlock->size(); |
| 756 | } |
| 757 | |
| 758 | private: |
| 759 | std::list<MemoryBlockPoolBlock>::const_iterator mBlock; |
| 760 | std::shared_ptr<MemoryBlockPoolImpl> mPool; |
| 761 | }; |
| 762 | |
| 763 | MemoryBlock MemoryBlockPool::fetch(size_t size) { |
| 764 | std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size); |
| 765 | return MemoryBlock(std::make_shared<MemoryBlock::Impl>( |
| 766 | poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl))); |
| 767 | } |
| 768 | |
| 769 | MemoryBlockPool::MemoryBlockPool() |
| 770 | : mImpl(std::make_shared<MemoryBlockPool::Impl>()) { |
| 771 | } |
| 772 | |
| 773 | MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl) |
| 774 | : mImpl(impl) { |
| 775 | } |
| 776 | |
| 777 | MemoryBlock::MemoryBlock() = default; |
| 778 | |
| 779 | MemoryBlock::~MemoryBlock() = default; |
| 780 | |
| 781 | const uint8_t* MemoryBlock::data() const { |
| 782 | return mImpl ? mImpl->data() : nullptr; |
| 783 | } |
| 784 | |
| 785 | size_t MemoryBlock::size() const { |
| 786 | return mImpl ? mImpl->size() : 0; |
| 787 | } |
| 788 | |
| 789 | MemoryBlock MemoryBlock::Allocate(size_t size) { |
| 790 | return MemoryBlockPool().fetch(size); |
| 791 | } |
| 792 | |
Wonsik Kim | 3ce32b1 | 2024-08-14 00:17:48 +0000 | [diff] [blame] | 793 | GraphicView2MediaImageConverter::GraphicView2MediaImageConverter( |
| 794 | const C2GraphicView &view, const sp<AMessage> &format, bool copy) |
| 795 | : mInitCheck(NO_INIT), |
| 796 | mView(view), |
| 797 | mWidth(view.width()), |
| 798 | mHeight(view.height()), |
| 799 | mAllocatedDepth(0), |
| 800 | mBackBufferSize(0), |
| 801 | mMediaImage(new ABuffer(sizeof(MediaImage2))) { |
| 802 | ATRACE_CALL(); |
| 803 | if (!format->findInt32(KEY_COLOR_FORMAT, &mClientColorFormat)) { |
| 804 | mClientColorFormat = COLOR_FormatYUV420Flexible; |
| 805 | } |
| 806 | if (!format->findInt32("android._color-format", &mComponentColorFormat)) { |
| 807 | mComponentColorFormat = COLOR_FormatYUV420Flexible; |
| 808 | } |
| 809 | if (view.error() != C2_OK) { |
| 810 | ALOGD("Converter: view.error() = %d", view.error()); |
| 811 | mInitCheck = BAD_VALUE; |
| 812 | return; |
| 813 | } |
| 814 | MediaImage2 *mediaImage = (MediaImage2 *)mMediaImage->base(); |
| 815 | const C2PlanarLayout &layout = view.layout(); |
| 816 | if (layout.numPlanes == 0) { |
| 817 | ALOGD("Converter: 0 planes"); |
| 818 | mInitCheck = BAD_VALUE; |
| 819 | return; |
| 820 | } |
| 821 | memset(mediaImage, 0, sizeof(*mediaImage)); |
| 822 | mAllocatedDepth = layout.planes[0].allocatedDepth; |
| 823 | uint32_t bitDepth = layout.planes[0].bitDepth; |
| 824 | |
| 825 | // align width and height to support subsampling cleanly |
| 826 | uint32_t stride = align(view.crop().width, 2) * divUp(layout.planes[0].allocatedDepth, 8u); |
| 827 | uint32_t vStride = align(view.crop().height, 2); |
| 828 | |
| 829 | bool tryWrapping = !copy; |
| 830 | |
| 831 | switch (layout.type) { |
| 832 | case C2PlanarLayout::TYPE_YUV: { |
| 833 | mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV; |
| 834 | if (layout.numPlanes != 3) { |
| 835 | ALOGD("Converter: %d planes for YUV layout", layout.numPlanes); |
| 836 | mInitCheck = BAD_VALUE; |
| 837 | return; |
| 838 | } |
| 839 | std::optional<int> clientBitDepth = {}; |
| 840 | switch (mClientColorFormat) { |
| 841 | case COLOR_FormatYUVP010: |
| 842 | clientBitDepth = 10; |
| 843 | break; |
| 844 | case COLOR_FormatYUV411PackedPlanar: |
| 845 | case COLOR_FormatYUV411Planar: |
| 846 | case COLOR_FormatYUV420Flexible: |
| 847 | case COLOR_FormatYUV420PackedPlanar: |
| 848 | case COLOR_FormatYUV420PackedSemiPlanar: |
| 849 | case COLOR_FormatYUV420Planar: |
| 850 | case COLOR_FormatYUV420SemiPlanar: |
| 851 | case COLOR_FormatYUV422Flexible: |
| 852 | case COLOR_FormatYUV422PackedPlanar: |
| 853 | case COLOR_FormatYUV422PackedSemiPlanar: |
| 854 | case COLOR_FormatYUV422Planar: |
| 855 | case COLOR_FormatYUV422SemiPlanar: |
| 856 | case COLOR_FormatYUV444Flexible: |
| 857 | case COLOR_FormatYUV444Interleaved: |
| 858 | clientBitDepth = 8; |
| 859 | break; |
| 860 | default: |
| 861 | // no-op; used with optional |
| 862 | break; |
| 863 | |
| 864 | } |
| 865 | // conversion fails if client bit-depth and the component bit-depth differs |
| 866 | if ((clientBitDepth) && (bitDepth != clientBitDepth.value())) { |
| 867 | ALOGD("Bit depth of client: %d and component: %d differs", |
| 868 | *clientBitDepth, bitDepth); |
| 869 | mInitCheck = BAD_VALUE; |
| 870 | return; |
| 871 | } |
| 872 | C2PlaneInfo yPlane = layout.planes[C2PlanarLayout::PLANE_Y]; |
| 873 | C2PlaneInfo uPlane = layout.planes[C2PlanarLayout::PLANE_U]; |
| 874 | C2PlaneInfo vPlane = layout.planes[C2PlanarLayout::PLANE_V]; |
| 875 | if (yPlane.channel != C2PlaneInfo::CHANNEL_Y |
| 876 | || uPlane.channel != C2PlaneInfo::CHANNEL_CB |
| 877 | || vPlane.channel != C2PlaneInfo::CHANNEL_CR) { |
| 878 | ALOGD("Converter: not YUV layout"); |
| 879 | mInitCheck = BAD_VALUE; |
| 880 | return; |
| 881 | } |
| 882 | bool yuv420888 = yPlane.rowSampling == 1 && yPlane.colSampling == 1 |
| 883 | && uPlane.rowSampling == 2 && uPlane.colSampling == 2 |
| 884 | && vPlane.rowSampling == 2 && vPlane.colSampling == 2; |
| 885 | if (yuv420888) { |
| 886 | for (uint32_t i = 0; i < 3; ++i) { |
| 887 | const C2PlaneInfo &plane = layout.planes[i]; |
| 888 | if (plane.allocatedDepth != 8 || plane.bitDepth != 8) { |
| 889 | yuv420888 = false; |
| 890 | break; |
| 891 | } |
| 892 | } |
| 893 | yuv420888 = yuv420888 && yPlane.colInc == 1 && uPlane.rowInc == vPlane.rowInc; |
| 894 | } |
| 895 | int32_t copyFormat = mClientColorFormat; |
| 896 | if (yuv420888 && mClientColorFormat == COLOR_FormatYUV420Flexible) { |
| 897 | if (uPlane.colInc == 2 && vPlane.colInc == 2 |
| 898 | && yPlane.rowInc == uPlane.rowInc) { |
| 899 | copyFormat = COLOR_FormatYUV420PackedSemiPlanar; |
| 900 | } else if (uPlane.colInc == 1 && vPlane.colInc == 1 |
| 901 | && yPlane.rowInc == uPlane.rowInc * 2) { |
| 902 | copyFormat = COLOR_FormatYUV420PackedPlanar; |
| 903 | } |
| 904 | } |
| 905 | ALOGV("client_fmt=0x%x y:{colInc=%d rowInc=%d} u:{colInc=%d rowInc=%d} " |
| 906 | "v:{colInc=%d rowInc=%d}", |
| 907 | mClientColorFormat, |
| 908 | yPlane.colInc, yPlane.rowInc, |
| 909 | uPlane.colInc, uPlane.rowInc, |
| 910 | vPlane.colInc, vPlane.rowInc); |
| 911 | switch (copyFormat) { |
| 912 | case COLOR_FormatYUV420Flexible: |
| 913 | case COLOR_FormatYUV420Planar: |
| 914 | case COLOR_FormatYUV420PackedPlanar: |
| 915 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 916 | mediaImage->mPlane[mediaImage->Y].mColInc = 1; |
| 917 | mediaImage->mPlane[mediaImage->Y].mRowInc = stride; |
| 918 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1; |
| 919 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1; |
| 920 | |
| 921 | mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride; |
| 922 | mediaImage->mPlane[mediaImage->U].mColInc = 1; |
| 923 | mediaImage->mPlane[mediaImage->U].mRowInc = stride / 2; |
| 924 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2; |
| 925 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2; |
| 926 | |
| 927 | mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride * 5 / 4; |
| 928 | mediaImage->mPlane[mediaImage->V].mColInc = 1; |
| 929 | mediaImage->mPlane[mediaImage->V].mRowInc = stride / 2; |
| 930 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2; |
| 931 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2; |
| 932 | |
| 933 | if (tryWrapping && mClientColorFormat != COLOR_FormatYUV420Flexible) { |
| 934 | tryWrapping = yuv420888 && uPlane.colInc == 1 && vPlane.colInc == 1 |
| 935 | && yPlane.rowInc == uPlane.rowInc * 2 |
| 936 | && view.data()[0] < view.data()[1] |
| 937 | && view.data()[1] < view.data()[2]; |
| 938 | } |
| 939 | break; |
| 940 | |
| 941 | case COLOR_FormatYUV420SemiPlanar: |
| 942 | case COLOR_FormatYUV420PackedSemiPlanar: |
| 943 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 944 | mediaImage->mPlane[mediaImage->Y].mColInc = 1; |
| 945 | mediaImage->mPlane[mediaImage->Y].mRowInc = stride; |
| 946 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1; |
| 947 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1; |
| 948 | |
| 949 | mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride; |
| 950 | mediaImage->mPlane[mediaImage->U].mColInc = 2; |
| 951 | mediaImage->mPlane[mediaImage->U].mRowInc = stride; |
| 952 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2; |
| 953 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2; |
| 954 | |
| 955 | mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride + 1; |
| 956 | mediaImage->mPlane[mediaImage->V].mColInc = 2; |
| 957 | mediaImage->mPlane[mediaImage->V].mRowInc = stride; |
| 958 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2; |
| 959 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2; |
| 960 | |
| 961 | if (tryWrapping && mClientColorFormat != COLOR_FormatYUV420Flexible) { |
| 962 | tryWrapping = yuv420888 && uPlane.colInc == 2 && vPlane.colInc == 2 |
| 963 | && yPlane.rowInc == uPlane.rowInc |
| 964 | && view.data()[0] < view.data()[1] |
| 965 | && view.data()[1] < view.data()[2]; |
| 966 | } |
| 967 | break; |
| 968 | |
| 969 | case COLOR_FormatYUVP010: |
| 970 | // stride is in bytes |
| 971 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 972 | mediaImage->mPlane[mediaImage->Y].mColInc = 2; |
| 973 | mediaImage->mPlane[mediaImage->Y].mRowInc = stride; |
| 974 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = 1; |
| 975 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = 1; |
| 976 | |
| 977 | mediaImage->mPlane[mediaImage->U].mOffset = stride * vStride; |
| 978 | mediaImage->mPlane[mediaImage->U].mColInc = 4; |
| 979 | mediaImage->mPlane[mediaImage->U].mRowInc = stride; |
| 980 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = 2; |
| 981 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = 2; |
| 982 | |
| 983 | mediaImage->mPlane[mediaImage->V].mOffset = stride * vStride + 2; |
| 984 | mediaImage->mPlane[mediaImage->V].mColInc = 4; |
| 985 | mediaImage->mPlane[mediaImage->V].mRowInc = stride; |
| 986 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = 2; |
| 987 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = 2; |
| 988 | if (tryWrapping) { |
| 989 | tryWrapping = yPlane.allocatedDepth == 16 |
| 990 | && uPlane.allocatedDepth == 16 |
| 991 | && vPlane.allocatedDepth == 16 |
| 992 | && yPlane.bitDepth == 10 |
| 993 | && uPlane.bitDepth == 10 |
| 994 | && vPlane.bitDepth == 10 |
| 995 | && yPlane.rightShift == 6 |
| 996 | && uPlane.rightShift == 6 |
| 997 | && vPlane.rightShift == 6 |
| 998 | && yPlane.rowSampling == 1 && yPlane.colSampling == 1 |
| 999 | && uPlane.rowSampling == 2 && uPlane.colSampling == 2 |
| 1000 | && vPlane.rowSampling == 2 && vPlane.colSampling == 2 |
| 1001 | && yPlane.colInc == 2 |
| 1002 | && uPlane.colInc == 4 |
| 1003 | && vPlane.colInc == 4 |
| 1004 | && yPlane.rowInc == uPlane.rowInc |
| 1005 | && yPlane.rowInc == vPlane.rowInc; |
| 1006 | } |
| 1007 | break; |
| 1008 | |
| 1009 | default: { |
| 1010 | // default to fully planar format --- this will be overridden if wrapping |
| 1011 | // TODO: keep interleaved format |
| 1012 | int32_t colInc = divUp(mAllocatedDepth, 8u); |
| 1013 | int32_t rowInc = stride * colInc / yPlane.colSampling; |
| 1014 | mediaImage->mPlane[mediaImage->Y].mOffset = 0; |
| 1015 | mediaImage->mPlane[mediaImage->Y].mColInc = colInc; |
| 1016 | mediaImage->mPlane[mediaImage->Y].mRowInc = rowInc; |
| 1017 | mediaImage->mPlane[mediaImage->Y].mHorizSubsampling = yPlane.colSampling; |
| 1018 | mediaImage->mPlane[mediaImage->Y].mVertSubsampling = yPlane.rowSampling; |
| 1019 | int32_t offset = rowInc * vStride / yPlane.rowSampling; |
| 1020 | |
| 1021 | rowInc = stride * colInc / uPlane.colSampling; |
| 1022 | mediaImage->mPlane[mediaImage->U].mOffset = offset; |
| 1023 | mediaImage->mPlane[mediaImage->U].mColInc = colInc; |
| 1024 | mediaImage->mPlane[mediaImage->U].mRowInc = rowInc; |
| 1025 | mediaImage->mPlane[mediaImage->U].mHorizSubsampling = uPlane.colSampling; |
| 1026 | mediaImage->mPlane[mediaImage->U].mVertSubsampling = uPlane.rowSampling; |
| 1027 | offset += rowInc * vStride / uPlane.rowSampling; |
| 1028 | |
| 1029 | rowInc = stride * colInc / vPlane.colSampling; |
| 1030 | mediaImage->mPlane[mediaImage->V].mOffset = offset; |
| 1031 | mediaImage->mPlane[mediaImage->V].mColInc = colInc; |
| 1032 | mediaImage->mPlane[mediaImage->V].mRowInc = rowInc; |
| 1033 | mediaImage->mPlane[mediaImage->V].mHorizSubsampling = vPlane.colSampling; |
| 1034 | mediaImage->mPlane[mediaImage->V].mVertSubsampling = vPlane.rowSampling; |
| 1035 | break; |
| 1036 | } |
| 1037 | } |
| 1038 | break; |
| 1039 | } |
| 1040 | |
| 1041 | case C2PlanarLayout::TYPE_YUVA: |
| 1042 | ALOGD("Converter: unrecognized color format " |
| 1043 | "(client %d component %d) for YUVA layout", |
| 1044 | mClientColorFormat, mComponentColorFormat); |
| 1045 | mInitCheck = NO_INIT; |
| 1046 | return; |
| 1047 | case C2PlanarLayout::TYPE_RGB: |
| 1048 | mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_RGB; |
| 1049 | // TODO: support MediaImage layout |
| 1050 | switch (mClientColorFormat) { |
| 1051 | case COLOR_FormatSurface: |
| 1052 | case COLOR_FormatRGBFlexible: |
| 1053 | case COLOR_Format24bitBGR888: |
| 1054 | case COLOR_Format24bitRGB888: |
| 1055 | ALOGD("Converter: accept color format " |
| 1056 | "(client %d component %d) for RGB layout", |
| 1057 | mClientColorFormat, mComponentColorFormat); |
| 1058 | break; |
| 1059 | default: |
| 1060 | ALOGD("Converter: unrecognized color format " |
| 1061 | "(client %d component %d) for RGB layout", |
| 1062 | mClientColorFormat, mComponentColorFormat); |
| 1063 | mInitCheck = BAD_VALUE; |
| 1064 | return; |
| 1065 | } |
| 1066 | if (layout.numPlanes != 3) { |
| 1067 | ALOGD("Converter: %d planes for RGB layout", layout.numPlanes); |
| 1068 | mInitCheck = BAD_VALUE; |
| 1069 | return; |
| 1070 | } |
| 1071 | break; |
| 1072 | case C2PlanarLayout::TYPE_RGBA: |
| 1073 | mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_RGBA; |
| 1074 | // TODO: support MediaImage layout |
| 1075 | switch (mClientColorFormat) { |
| 1076 | case COLOR_FormatSurface: |
| 1077 | case COLOR_FormatRGBAFlexible: |
| 1078 | case COLOR_Format32bitABGR8888: |
| 1079 | case COLOR_Format32bitARGB8888: |
| 1080 | case COLOR_Format32bitBGRA8888: |
| 1081 | ALOGD("Converter: accept color format " |
| 1082 | "(client %d component %d) for RGBA layout", |
| 1083 | mClientColorFormat, mComponentColorFormat); |
| 1084 | break; |
| 1085 | default: |
| 1086 | ALOGD("Converter: unrecognized color format " |
| 1087 | "(client %d component %d) for RGBA layout", |
| 1088 | mClientColorFormat, mComponentColorFormat); |
| 1089 | mInitCheck = BAD_VALUE; |
| 1090 | return; |
| 1091 | } |
| 1092 | if (layout.numPlanes != 4) { |
| 1093 | ALOGD("Converter: %d planes for RGBA layout", layout.numPlanes); |
| 1094 | mInitCheck = BAD_VALUE; |
| 1095 | return; |
| 1096 | } |
| 1097 | break; |
| 1098 | default: |
| 1099 | mediaImage->mType = MediaImage2::MEDIA_IMAGE_TYPE_UNKNOWN; |
| 1100 | if (layout.numPlanes == 1) { |
| 1101 | const C2PlaneInfo &plane = layout.planes[0]; |
| 1102 | if (plane.colInc < 0 || plane.rowInc < 0) { |
| 1103 | // Copy-only if we have negative colInc/rowInc |
| 1104 | tryWrapping = false; |
| 1105 | } |
| 1106 | mediaImage->mPlane[0].mOffset = 0; |
| 1107 | mediaImage->mPlane[0].mColInc = std::abs(plane.colInc); |
| 1108 | mediaImage->mPlane[0].mRowInc = std::abs(plane.rowInc); |
| 1109 | mediaImage->mPlane[0].mHorizSubsampling = plane.colSampling; |
| 1110 | mediaImage->mPlane[0].mVertSubsampling = plane.rowSampling; |
| 1111 | } else { |
| 1112 | ALOGD("Converter: unrecognized layout: color format (client %d component %d)", |
| 1113 | mClientColorFormat, mComponentColorFormat); |
| 1114 | mInitCheck = NO_INIT; |
| 1115 | return; |
| 1116 | } |
| 1117 | break; |
| 1118 | } |
| 1119 | if (tryWrapping) { |
| 1120 | // try to map directly. check if the planes are near one another |
| 1121 | const uint8_t *minPtr = mView.data()[0]; |
| 1122 | const uint8_t *maxPtr = mView.data()[0]; |
| 1123 | int32_t planeSize = 0; |
| 1124 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 1125 | const C2PlaneInfo &plane = layout.planes[i]; |
| 1126 | int64_t planeStride = std::abs(plane.rowInc / plane.colInc); |
| 1127 | ssize_t minOffset = plane.minOffset( |
| 1128 | mWidth / plane.colSampling, mHeight / plane.rowSampling); |
| 1129 | ssize_t maxOffset = plane.maxOffset( |
| 1130 | mWidth / plane.colSampling, mHeight / plane.rowSampling); |
| 1131 | if (minPtr > mView.data()[i] + minOffset) { |
| 1132 | minPtr = mView.data()[i] + minOffset; |
| 1133 | } |
| 1134 | if (maxPtr < mView.data()[i] + maxOffset) { |
| 1135 | maxPtr = mView.data()[i] + maxOffset; |
| 1136 | } |
| 1137 | planeSize += planeStride * divUp(mAllocatedDepth, 8u) |
| 1138 | * align(mHeight, 64) / plane.rowSampling; |
| 1139 | } |
| 1140 | |
| 1141 | if (minPtr == mView.data()[0] && (maxPtr - minPtr) <= planeSize) { |
| 1142 | // FIXME: this is risky as reading/writing data out of bound results |
| 1143 | // in an undefined behavior, but gralloc does assume a |
| 1144 | // contiguous mapping |
| 1145 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 1146 | const C2PlaneInfo &plane = layout.planes[i]; |
| 1147 | mediaImage->mPlane[i].mOffset = mView.data()[i] - minPtr; |
| 1148 | mediaImage->mPlane[i].mColInc = plane.colInc; |
| 1149 | mediaImage->mPlane[i].mRowInc = plane.rowInc; |
| 1150 | mediaImage->mPlane[i].mHorizSubsampling = plane.colSampling; |
| 1151 | mediaImage->mPlane[i].mVertSubsampling = plane.rowSampling; |
| 1152 | } |
| 1153 | mWrapped = new ABuffer(const_cast<uint8_t *>(minPtr), maxPtr - minPtr); |
| 1154 | ALOGV("Converter: wrapped (capacity=%zu)", mWrapped->capacity()); |
| 1155 | } |
| 1156 | } |
| 1157 | mediaImage->mNumPlanes = layout.numPlanes; |
| 1158 | mediaImage->mWidth = view.crop().width; |
| 1159 | mediaImage->mHeight = view.crop().height; |
| 1160 | mediaImage->mBitDepth = bitDepth; |
| 1161 | mediaImage->mBitDepthAllocated = mAllocatedDepth; |
| 1162 | |
| 1163 | uint32_t bufferSize = 0; |
| 1164 | for (uint32_t i = 0; i < layout.numPlanes; ++i) { |
| 1165 | const C2PlaneInfo &plane = layout.planes[i]; |
| 1166 | if (plane.allocatedDepth < plane.bitDepth |
| 1167 | || plane.rightShift != plane.allocatedDepth - plane.bitDepth) { |
| 1168 | ALOGD("rightShift value of %u unsupported", plane.rightShift); |
| 1169 | mInitCheck = BAD_VALUE; |
| 1170 | return; |
| 1171 | } |
| 1172 | if (plane.allocatedDepth > 8 && plane.endianness != C2PlaneInfo::NATIVE) { |
| 1173 | ALOGD("endianness value of %u unsupported", plane.endianness); |
| 1174 | mInitCheck = BAD_VALUE; |
| 1175 | return; |
| 1176 | } |
| 1177 | if (plane.allocatedDepth != mAllocatedDepth || plane.bitDepth != bitDepth) { |
| 1178 | ALOGD("different allocatedDepth/bitDepth per plane unsupported"); |
| 1179 | mInitCheck = BAD_VALUE; |
| 1180 | return; |
| 1181 | } |
| 1182 | // stride is in bytes |
| 1183 | bufferSize += stride * vStride / plane.rowSampling / plane.colSampling; |
| 1184 | } |
| 1185 | |
| 1186 | mBackBufferSize = bufferSize; |
| 1187 | mInitCheck = OK; |
| 1188 | } |
| 1189 | |
| 1190 | status_t GraphicView2MediaImageConverter::initCheck() const { return mInitCheck; } |
| 1191 | |
| 1192 | uint32_t GraphicView2MediaImageConverter::backBufferSize() const { return mBackBufferSize; } |
| 1193 | |
| 1194 | sp<ABuffer> GraphicView2MediaImageConverter::wrap() const { |
| 1195 | if (mBackBuffer == nullptr) { |
| 1196 | return mWrapped; |
| 1197 | } |
| 1198 | return nullptr; |
| 1199 | } |
| 1200 | |
| 1201 | bool GraphicView2MediaImageConverter::setBackBuffer(const sp<ABuffer> &backBuffer) { |
| 1202 | if (backBuffer == nullptr) { |
| 1203 | return false; |
| 1204 | } |
| 1205 | if (backBuffer->capacity() < mBackBufferSize) { |
| 1206 | return false; |
| 1207 | } |
| 1208 | backBuffer->setRange(0, mBackBufferSize); |
| 1209 | mBackBuffer = backBuffer; |
| 1210 | return true; |
| 1211 | } |
| 1212 | |
| 1213 | status_t GraphicView2MediaImageConverter::copyToMediaImage() { |
| 1214 | ATRACE_CALL(); |
| 1215 | if (mInitCheck != OK) { |
| 1216 | return mInitCheck; |
| 1217 | } |
| 1218 | return ImageCopy(mBackBuffer->base(), getMediaImage(), mView); |
| 1219 | } |
| 1220 | |
| 1221 | const sp<ABuffer> &GraphicView2MediaImageConverter::imageData() const { return mMediaImage; } |
| 1222 | |
| 1223 | MediaImage2 *GraphicView2MediaImageConverter::getMediaImage() { |
| 1224 | return (MediaImage2 *)mMediaImage->base(); |
| 1225 | } |
| 1226 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 1227 | } // namespace android |