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> |
| 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 | /** |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 41 | * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) |
| 42 | * do not work as the results are always const. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 43 | */ |
| 44 | template<bool ToA, size_t S> |
| 45 | struct MemCopier { |
| 46 | template<typename A, typename B> |
| 47 | 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> |
| 55 | 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> |
| 69 | static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 70 | // TODO: more efficient copying --- e.g. copy interleaved planes together, etc. |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 71 | const C2PlanarLayout &layout = view.layout(); |
| 72 | const size_t bpp = divUp(img->mBitDepthAllocated, 8u); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 73 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 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; |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 92 | |
Praveen Chavan | bc2b713 | 2022-01-19 23:02:56 -0800 | [diff] [blame] | 93 | bool canCopyByRow = (plane.colInc == bpp) && (img->mPlane[i].mColInc == bpp); |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 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; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 103 | } |
Pawin Vongmasa | 8be9311 | 2018-12-11 14:01:42 -0800 | [diff] [blame] | 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 | } |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return OK; |
| 119 | } |
| 120 | |
| 121 | } // namespace |
| 122 | |
| 123 | status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) { |
Arun Johnson | 3ab32cd | 2022-06-10 18:58:01 +0000 | [diff] [blame] | 124 | if (img == nullptr |
| 125 | || imgBase == nullptr |
| 126 | || view.crop().width != img->mWidth |
| 127 | || view.crop().height != img->mHeight) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 128 | return BAD_VALUE; |
| 129 | } |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 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 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 145 | if (IsNV12(view)) { |
| 146 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 147 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 152 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 158 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 159 | 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] | 160 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 161 | return OK; |
| 162 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 163 | } |
| 164 | } else if (IsNV21(view)) { |
| 165 | if (IsNV12(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 166 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 172 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 177 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 185 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 186 | 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] | 187 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 188 | return OK; |
| 189 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 190 | } else if (IsNV21(img)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 191 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 197 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 202 | } |
| 203 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 204 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 205 | return _ImageCopy<true>(view, img, imgBase); |
| 206 | } |
| 207 | |
| 208 | status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) { |
Arun Johnson | 3ab32cd | 2022-06-10 18:58:01 +0000 | [diff] [blame] | 209 | if (img == nullptr |
| 210 | || imgBase == nullptr |
| 211 | || view.crop().width != img->mWidth |
| 212 | || view.crop().height != img->mHeight) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 213 | return BAD_VALUE; |
| 214 | } |
Wonsik Kim | f37f242 | 2021-03-04 15:38:59 -0800 | [diff] [blame] | 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; |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 229 | if (IsNV12(img)) { |
| 230 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 231 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 236 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 242 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 243 | 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] | 244 | dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 245 | return OK; |
| 246 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 247 | } |
| 248 | } else if (IsNV21(img)) { |
| 249 | if (IsNV12(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 250 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 256 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 261 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 269 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12"); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 270 | 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] | 271 | dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) { |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 272 | return OK; |
| 273 | } |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 274 | } else if (IsNV21(view)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 275 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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)) { |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 281 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420"); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 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; |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
My Name | 6bd9a7d | 2022-03-25 12:37:58 -0700 | [diff] [blame] | 288 | ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic"); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 289 | return _ImageCopy<false>(view, img, imgBase); |
| 290 | } |
| 291 | |
| 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 | |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame^] | 316 | bool IsYUV420_10bit(const C2GraphicView &view) { |
| 317 | const C2PlanarLayout &layout = view.layout(); |
| 318 | return (layout.numPlanes == 3 |
| 319 | && layout.type == C2PlanarLayout::TYPE_YUV |
| 320 | && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y |
| 321 | && layout.planes[layout.PLANE_Y].allocatedDepth == 16 |
| 322 | && layout.planes[layout.PLANE_Y].bitDepth == 10 |
| 323 | && layout.planes[layout.PLANE_Y].colSampling == 1 |
| 324 | && layout.planes[layout.PLANE_Y].rowSampling == 1 |
| 325 | && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB |
| 326 | && layout.planes[layout.PLANE_U].allocatedDepth == 16 |
| 327 | && layout.planes[layout.PLANE_U].bitDepth == 10 |
| 328 | && layout.planes[layout.PLANE_U].colSampling == 2 |
| 329 | && layout.planes[layout.PLANE_U].rowSampling == 2 |
| 330 | && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR |
| 331 | && layout.planes[layout.PLANE_V].allocatedDepth == 16 |
| 332 | && layout.planes[layout.PLANE_V].bitDepth == 10 |
| 333 | && layout.planes[layout.PLANE_V].colSampling == 2 |
| 334 | && layout.planes[layout.PLANE_V].rowSampling == 2); |
| 335 | } |
| 336 | |
| 337 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 338 | bool IsNV12(const C2GraphicView &view) { |
| 339 | if (!IsYUV420(view)) { |
| 340 | return false; |
| 341 | } |
| 342 | const C2PlanarLayout &layout = view.layout(); |
| 343 | return (layout.rootPlanes == 2 |
| 344 | && layout.planes[layout.PLANE_U].colInc == 2 |
| 345 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 346 | && layout.planes[layout.PLANE_U].offset == 0 |
| 347 | && layout.planes[layout.PLANE_V].colInc == 2 |
| 348 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U |
| 349 | && layout.planes[layout.PLANE_V].offset == 1); |
| 350 | } |
| 351 | |
Fyodor Kyslov | d97e991 | 2022-11-07 19:23:21 +0000 | [diff] [blame^] | 352 | bool IsP010(const C2GraphicView &view) { |
| 353 | if (!IsYUV420_10bit(view)) { |
| 354 | return false; |
| 355 | } |
| 356 | const C2PlanarLayout &layout = view.layout(); |
| 357 | return (layout.rootPlanes == 2 |
| 358 | && layout.planes[layout.PLANE_U].colInc == 4 |
| 359 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 360 | && layout.planes[layout.PLANE_U].offset == 0 |
| 361 | && layout.planes[layout.PLANE_V].colInc == 4 |
| 362 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U |
| 363 | && layout.planes[layout.PLANE_V].offset == 2 |
| 364 | && layout.planes[layout.PLANE_Y].rightShift == 6 |
| 365 | && layout.planes[layout.PLANE_U].rightShift == 6 |
| 366 | && layout.planes[layout.PLANE_V].rightShift == 6); |
| 367 | } |
| 368 | |
| 369 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 370 | bool IsNV21(const C2GraphicView &view) { |
| 371 | if (!IsYUV420(view)) { |
| 372 | return false; |
| 373 | } |
| 374 | const C2PlanarLayout &layout = view.layout(); |
| 375 | return (layout.rootPlanes == 2 |
| 376 | && layout.planes[layout.PLANE_U].colInc == 2 |
| 377 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V |
| 378 | && layout.planes[layout.PLANE_U].offset == 1 |
| 379 | && layout.planes[layout.PLANE_V].colInc == 2 |
| 380 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V |
| 381 | && layout.planes[layout.PLANE_V].offset == 0); |
| 382 | } |
| 383 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 384 | bool IsI420(const C2GraphicView &view) { |
| 385 | if (!IsYUV420(view)) { |
| 386 | return false; |
| 387 | } |
| 388 | const C2PlanarLayout &layout = view.layout(); |
| 389 | return (layout.rootPlanes == 3 |
| 390 | && layout.planes[layout.PLANE_U].colInc == 1 |
| 391 | && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U |
| 392 | && layout.planes[layout.PLANE_U].offset == 0 |
| 393 | && layout.planes[layout.PLANE_V].colInc == 1 |
| 394 | && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V |
| 395 | && layout.planes[layout.PLANE_V].offset == 0); |
| 396 | } |
| 397 | |
| 398 | bool IsYUV420(const MediaImage2 *img) { |
| 399 | return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV |
| 400 | && img->mNumPlanes == 3 |
| 401 | && img->mBitDepth == 8 |
| 402 | && img->mBitDepthAllocated == 8 |
| 403 | && img->mPlane[0].mHorizSubsampling == 1 |
| 404 | && img->mPlane[0].mVertSubsampling == 1 |
| 405 | && img->mPlane[1].mHorizSubsampling == 2 |
| 406 | && img->mPlane[1].mVertSubsampling == 2 |
| 407 | && img->mPlane[2].mHorizSubsampling == 2 |
| 408 | && img->mPlane[2].mVertSubsampling == 2); |
| 409 | } |
| 410 | |
| 411 | bool IsNV12(const MediaImage2 *img) { |
| 412 | if (!IsYUV420(img)) { |
| 413 | return false; |
| 414 | } |
| 415 | return (img->mPlane[1].mColInc == 2 |
| 416 | && img->mPlane[2].mColInc == 2 |
Harish Mahendrakar | cd59f03 | 2021-04-16 18:55:41 -0700 | [diff] [blame] | 417 | && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1)); |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 418 | } |
| 419 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 420 | bool IsNV21(const MediaImage2 *img) { |
| 421 | if (!IsYUV420(img)) { |
| 422 | return false; |
| 423 | } |
| 424 | return (img->mPlane[1].mColInc == 2 |
| 425 | && img->mPlane[2].mColInc == 2 |
Harish Mahendrakar | cd59f03 | 2021-04-16 18:55:41 -0700 | [diff] [blame] | 426 | && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1)); |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Pawin Vongmasa | 1f21336 | 2019-01-24 06:59:16 -0800 | [diff] [blame] | 429 | bool IsI420(const MediaImage2 *img) { |
| 430 | if (!IsYUV420(img)) { |
| 431 | return false; |
| 432 | } |
| 433 | return (img->mPlane[1].mColInc == 1 |
| 434 | && img->mPlane[2].mColInc == 1 |
| 435 | && img->mPlane[2].mOffset > img->mPlane[1].mOffset); |
| 436 | } |
| 437 | |
Wonsik Kim | a38fdf2 | 2021-04-05 14:50:29 -0700 | [diff] [blame] | 438 | FlexLayout GetYuv420FlexibleLayout() { |
| 439 | static FlexLayout sLayout = []{ |
| 440 | AHardwareBuffer_Desc desc = { |
| 441 | 16, // width |
| 442 | 16, // height |
| 443 | 1, // layers |
| 444 | AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420, |
| 445 | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, |
| 446 | 0, // stride |
| 447 | 0, // rfu0 |
| 448 | 0, // rfu1 |
| 449 | }; |
| 450 | AHardwareBuffer *buffer = nullptr; |
| 451 | int ret = AHardwareBuffer_allocate(&desc, &buffer); |
| 452 | if (ret != 0) { |
| 453 | return FLEX_LAYOUT_UNKNOWN; |
| 454 | } |
| 455 | class AutoCloser { |
| 456 | public: |
| 457 | AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {} |
| 458 | ~AutoCloser() { |
| 459 | if (mLocked) { |
| 460 | AHardwareBuffer_unlock(mBuffer, nullptr); |
| 461 | } |
| 462 | AHardwareBuffer_release(mBuffer); |
| 463 | } |
| 464 | |
| 465 | void setLocked() { mLocked = true; } |
| 466 | |
| 467 | private: |
| 468 | AHardwareBuffer *mBuffer; |
| 469 | bool mLocked; |
| 470 | } autoCloser(buffer); |
| 471 | AHardwareBuffer_Planes planes; |
| 472 | ret = AHardwareBuffer_lockPlanes( |
| 473 | buffer, |
| 474 | AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN, |
| 475 | -1, // fence |
| 476 | nullptr, // rect |
| 477 | &planes); |
| 478 | if (ret != 0) { |
| 479 | AHardwareBuffer_release(buffer); |
| 480 | return FLEX_LAYOUT_UNKNOWN; |
| 481 | } |
| 482 | autoCloser.setLocked(); |
| 483 | if (planes.planeCount != 3) { |
| 484 | return FLEX_LAYOUT_UNKNOWN; |
| 485 | } |
| 486 | if (planes.planes[0].pixelStride != 1) { |
| 487 | return FLEX_LAYOUT_UNKNOWN; |
| 488 | } |
| 489 | if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) { |
| 490 | return FLEX_LAYOUT_PLANAR; |
| 491 | } |
| 492 | if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) { |
| 493 | ssize_t uvDist = |
| 494 | static_cast<uint8_t *>(planes.planes[2].data) - |
| 495 | static_cast<uint8_t *>(planes.planes[1].data); |
| 496 | if (uvDist == 1) { |
| 497 | return FLEX_LAYOUT_SEMIPLANAR_UV; |
| 498 | } else if (uvDist == -1) { |
| 499 | return FLEX_LAYOUT_SEMIPLANAR_VU; |
| 500 | } |
| 501 | return FLEX_LAYOUT_UNKNOWN; |
| 502 | } |
| 503 | return FLEX_LAYOUT_UNKNOWN; |
| 504 | }(); |
| 505 | return sLayout; |
| 506 | } |
| 507 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 508 | MediaImage2 CreateYUV420PlanarMediaImage2( |
| 509 | uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) { |
| 510 | return MediaImage2 { |
| 511 | .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV, |
| 512 | .mNumPlanes = 3, |
| 513 | .mWidth = width, |
| 514 | .mHeight = height, |
| 515 | .mBitDepth = 8, |
| 516 | .mBitDepthAllocated = 8, |
| 517 | .mPlane = { |
| 518 | { |
| 519 | .mOffset = 0, |
| 520 | .mColInc = 1, |
| 521 | .mRowInc = (int32_t)stride, |
| 522 | .mHorizSubsampling = 1, |
| 523 | .mVertSubsampling = 1, |
| 524 | }, |
| 525 | { |
| 526 | .mOffset = stride * vstride, |
| 527 | .mColInc = 1, |
| 528 | .mRowInc = (int32_t)stride / 2, |
| 529 | .mHorizSubsampling = 2, |
| 530 | .mVertSubsampling = 2, |
| 531 | }, |
| 532 | { |
| 533 | .mOffset = stride * vstride * 5 / 4, |
| 534 | .mColInc = 1, |
| 535 | .mRowInc = (int32_t)stride / 2, |
| 536 | .mHorizSubsampling = 2, |
| 537 | .mVertSubsampling = 2, |
| 538 | } |
| 539 | }, |
| 540 | }; |
| 541 | } |
| 542 | |
| 543 | MediaImage2 CreateYUV420SemiPlanarMediaImage2( |
| 544 | uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) { |
| 545 | return MediaImage2 { |
| 546 | .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV, |
| 547 | .mNumPlanes = 3, |
| 548 | .mWidth = width, |
| 549 | .mHeight = height, |
| 550 | .mBitDepth = 8, |
| 551 | .mBitDepthAllocated = 8, |
| 552 | .mPlane = { |
| 553 | { |
| 554 | .mOffset = 0, |
| 555 | .mColInc = 1, |
| 556 | .mRowInc = (int32_t)stride, |
| 557 | .mHorizSubsampling = 1, |
| 558 | .mVertSubsampling = 1, |
| 559 | }, |
| 560 | { |
| 561 | .mOffset = stride * vstride, |
| 562 | .mColInc = 2, |
| 563 | .mRowInc = (int32_t)stride, |
| 564 | .mHorizSubsampling = 2, |
| 565 | .mVertSubsampling = 2, |
| 566 | }, |
| 567 | { |
| 568 | .mOffset = stride * vstride + 1, |
| 569 | .mColInc = 2, |
| 570 | .mRowInc = (int32_t)stride, |
| 571 | .mHorizSubsampling = 2, |
| 572 | .mVertSubsampling = 2, |
| 573 | } |
| 574 | }, |
| 575 | }; |
| 576 | } |
| 577 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 578 | // Matrix coefficient to convert RGB to Planar YUV data. |
| 579 | // Each sub-array represents the 3X3 coeff used with R, G and B |
| 580 | static const int16_t bt601Matrix[2][3][3] = { |
Lajos Molnar | d842c0d | 2022-06-01 14:37:55 -0700 | [diff] [blame] | 581 | { { 77, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */ |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 582 | { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } }, /* RANGE_LIMITED */ |
| 583 | }; |
| 584 | |
| 585 | static const int16_t bt709Matrix[2][3][3] = { |
Lajos Molnar | d842c0d | 2022-06-01 14:37:55 -0700 | [diff] [blame] | 586 | // TRICKY: 18 is adjusted to 19 so that sum of row 1 is 256 |
| 587 | { { 54, 183, 19 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */ |
| 588 | // TRICKY: -87 is adjusted to -86 so that sum of row 2 is 0 |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 589 | { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */ |
| 590 | }; |
| 591 | |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 592 | status_t ConvertRGBToPlanarYUV( |
| 593 | uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize, |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 594 | const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) { |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 595 | CHECK(dstY != nullptr); |
| 596 | CHECK((src.width() & 1) == 0); |
| 597 | CHECK((src.height() & 1) == 0); |
| 598 | |
| 599 | if (dstStride * dstVStride * 3 / 2 > bufferSize) { |
| 600 | ALOGD("conversion buffer is too small for converting from RGB to YUV"); |
| 601 | return NO_MEMORY; |
| 602 | } |
| 603 | |
| 604 | uint8_t *dstU = dstY + dstStride * dstVStride; |
| 605 | uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1); |
| 606 | |
| 607 | const C2PlanarLayout &layout = src.layout(); |
| 608 | const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R]; |
| 609 | const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G]; |
| 610 | const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B]; |
| 611 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 612 | // set default range as limited |
| 613 | if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) { |
| 614 | colorRange = C2Color::RANGE_LIMITED; |
| 615 | } |
| 616 | const int16_t (*weights)[3] = |
| 617 | (colorMatrix == C2Color::MATRIX_BT709) ? |
| 618 | bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1]; |
| 619 | uint8_t zeroLvl = colorRange == C2Color::RANGE_FULL ? 0 : 16; |
| 620 | uint8_t maxLvlLuma = colorRange == C2Color::RANGE_FULL ? 255 : 235; |
| 621 | uint8_t maxLvlChroma = colorRange == C2Color::RANGE_FULL ? 255 : 240; |
| 622 | |
| 623 | #define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max))) |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 624 | for (size_t y = 0; y < src.height(); ++y) { |
| 625 | for (size_t x = 0; x < src.width(); ++x) { |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 626 | uint8_t r = *pRed; |
| 627 | uint8_t g = *pGreen; |
| 628 | uint8_t b = *pBlue; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 629 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 630 | unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) + |
| 631 | zeroLvl; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 632 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 633 | dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 634 | |
| 635 | if ((x & 1) == 0 && (y & 1) == 0) { |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 636 | unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) + |
| 637 | 128; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 638 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 639 | unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) + |
| 640 | 128; |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 641 | |
Manisha Jajoo | 478423c | 2021-05-20 21:28:14 +0530 | [diff] [blame] | 642 | dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma); |
| 643 | dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma); |
Pawin Vongmasa | 3665390 | 2018-11-15 00:10:25 -0800 | [diff] [blame] | 644 | } |
| 645 | pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc; |
| 646 | pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc; |
| 647 | pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc; |
| 648 | } |
| 649 | |
| 650 | if ((y & 1) == 0) { |
| 651 | dstU += dstStride >> 1; |
| 652 | dstV += dstStride >> 1; |
| 653 | } |
| 654 | |
| 655 | pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width(); |
| 656 | pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width(); |
| 657 | pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width(); |
| 658 | pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc; |
| 659 | pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc; |
| 660 | pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc; |
| 661 | |
| 662 | dstY += dstStride; |
| 663 | } |
| 664 | return OK; |
| 665 | } |
| 666 | |
| 667 | namespace { |
| 668 | |
| 669 | /** |
| 670 | * A block of raw allocated memory. |
| 671 | */ |
| 672 | struct MemoryBlockPoolBlock { |
| 673 | MemoryBlockPoolBlock(size_t size) |
| 674 | : mData(new uint8_t[size]), mSize(mData ? size : 0) { } |
| 675 | |
| 676 | ~MemoryBlockPoolBlock() { |
| 677 | delete[] mData; |
| 678 | } |
| 679 | |
| 680 | const uint8_t *data() const { |
| 681 | return mData; |
| 682 | } |
| 683 | |
| 684 | size_t size() const { |
| 685 | return mSize; |
| 686 | } |
| 687 | |
| 688 | C2_DO_NOT_COPY(MemoryBlockPoolBlock); |
| 689 | |
| 690 | private: |
| 691 | uint8_t *mData; |
| 692 | size_t mSize; |
| 693 | }; |
| 694 | |
| 695 | /** |
| 696 | * A simple raw memory block pool implementation. |
| 697 | */ |
| 698 | struct MemoryBlockPoolImpl { |
| 699 | void release(std::list<MemoryBlockPoolBlock>::const_iterator block) { |
| 700 | std::lock_guard<std::mutex> lock(mMutex); |
| 701 | // return block to free blocks if it is the current size; otherwise, discard |
| 702 | if (block->size() == mCurrentSize) { |
| 703 | mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block); |
| 704 | } else { |
| 705 | mBlocksInUse.erase(block); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) { |
| 710 | std::lock_guard<std::mutex> lock(mMutex); |
| 711 | mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool { |
| 712 | return block.size() != size; |
| 713 | }); |
| 714 | mCurrentSize = size; |
| 715 | if (mFreeBlocks.empty()) { |
| 716 | mBlocksInUse.emplace_front(size); |
| 717 | } else { |
| 718 | mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin()); |
| 719 | } |
| 720 | return mBlocksInUse.begin(); |
| 721 | } |
| 722 | |
| 723 | MemoryBlockPoolImpl() = default; |
| 724 | |
| 725 | C2_DO_NOT_COPY(MemoryBlockPoolImpl); |
| 726 | |
| 727 | private: |
| 728 | std::mutex mMutex; |
| 729 | std::list<MemoryBlockPoolBlock> mFreeBlocks; |
| 730 | std::list<MemoryBlockPoolBlock> mBlocksInUse; |
| 731 | size_t mCurrentSize; |
| 732 | }; |
| 733 | |
| 734 | } // namespace |
| 735 | |
| 736 | struct MemoryBlockPool::Impl : MemoryBlockPoolImpl { |
| 737 | }; |
| 738 | |
| 739 | struct MemoryBlock::Impl { |
| 740 | Impl(std::list<MemoryBlockPoolBlock>::const_iterator block, |
| 741 | std::shared_ptr<MemoryBlockPoolImpl> pool) |
| 742 | : mBlock(block), mPool(pool) { |
| 743 | } |
| 744 | |
| 745 | ~Impl() { |
| 746 | mPool->release(mBlock); |
| 747 | } |
| 748 | |
| 749 | const uint8_t *data() const { |
| 750 | return mBlock->data(); |
| 751 | } |
| 752 | |
| 753 | size_t size() const { |
| 754 | return mBlock->size(); |
| 755 | } |
| 756 | |
| 757 | private: |
| 758 | std::list<MemoryBlockPoolBlock>::const_iterator mBlock; |
| 759 | std::shared_ptr<MemoryBlockPoolImpl> mPool; |
| 760 | }; |
| 761 | |
| 762 | MemoryBlock MemoryBlockPool::fetch(size_t size) { |
| 763 | std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size); |
| 764 | return MemoryBlock(std::make_shared<MemoryBlock::Impl>( |
| 765 | poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl))); |
| 766 | } |
| 767 | |
| 768 | MemoryBlockPool::MemoryBlockPool() |
| 769 | : mImpl(std::make_shared<MemoryBlockPool::Impl>()) { |
| 770 | } |
| 771 | |
| 772 | MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl) |
| 773 | : mImpl(impl) { |
| 774 | } |
| 775 | |
| 776 | MemoryBlock::MemoryBlock() = default; |
| 777 | |
| 778 | MemoryBlock::~MemoryBlock() = default; |
| 779 | |
| 780 | const uint8_t* MemoryBlock::data() const { |
| 781 | return mImpl ? mImpl->data() : nullptr; |
| 782 | } |
| 783 | |
| 784 | size_t MemoryBlock::size() const { |
| 785 | return mImpl ? mImpl->size() : 0; |
| 786 | } |
| 787 | |
| 788 | MemoryBlock MemoryBlock::Allocate(size_t size) { |
| 789 | return MemoryBlockPool().fetch(size); |
| 790 | } |
| 791 | |
| 792 | } // namespace android |