blob: a3a023a3d22f5c1765c36ced5a1d28886eebbd7e [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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 Name6bd9a7d2022-03-25 12:37:58 -070019#define ATRACE_TAG ATRACE_TAG_VIDEO
Pawin Vongmasa36653902018-11-15 00:10:25 -080020#include <utils/Log.h>
My Name6bd9a7d2022-03-25 12:37:58 -070021#include <utils/Trace.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080022
Pawin Vongmasa1f213362019-01-24 06:59:16 -080023#include <libyuv.h>
24
Pawin Vongmasa36653902018-11-15 00:10:25 -080025#include <list>
26#include <mutex>
27
Wonsik Kima38fdf22021-04-05 14:50:29 -070028#include <android/hardware_buffer.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080029#include <media/hardware/HardwareAPI.h>
30#include <media/stagefright/foundation/AUtils.h>
31
32#include <C2Debug.h>
33
34#include "Codec2BufferUtils.h"
35
36namespace android {
37
38namespace {
39
40/**
My Name6bd9a7d2022-03-25 12:37:58 -070041 * A flippable, optimizable memcpy. Constructs such as (from ? src : dst)
42 * do not work as the results are always const.
Pawin Vongmasa36653902018-11-15 00:10:25 -080043 */
44template<bool ToA, size_t S>
45struct 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
52template<size_t S>
53struct 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 */
68template<bool ToMediaImage, typename View, typename ImagePixel>
69static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -080070 // TODO: more efficient copying --- e.g. copy interleaved planes together, etc.
Pawin Vongmasa36653902018-11-15 00:10:25 -080071 const C2PlanarLayout &layout = view.layout();
72 const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
Pawin Vongmasa1f213362019-01-24 06:59:16 -080073
Pawin Vongmasa36653902018-11-15 00:10:25 -080074 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 Vongmasa8be93112018-12-11 14:01:42 -080092
Praveen Chavanbc2b7132022-01-19 23:02:56 -080093 bool canCopyByRow = (plane.colInc == bpp) && (img->mPlane[i].mColInc == bpp);
Pawin Vongmasa8be93112018-12-11 14:01:42 -080094 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 Vongmasa36653902018-11-15 00:10:25 -0800103 }
Pawin Vongmasa8be93112018-12-11 14:01:42 -0800104 } 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 Vongmasa36653902018-11-15 00:10:25 -0800116 }
117 }
118 return OK;
119}
120
121} // namespace
122
123status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
Harish Mahendrakarf7c49e22019-05-24 14:19:16 -0700124 if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800125 return BAD_VALUE;
126 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800127 const uint8_t* src_y = view.data()[0];
128 const uint8_t* src_u = view.data()[1];
129 const uint8_t* src_v = view.data()[2];
130 int32_t src_stride_y = view.layout().planes[0].rowInc;
131 int32_t src_stride_u = view.layout().planes[1].rowInc;
132 int32_t src_stride_v = view.layout().planes[2].rowInc;
133 uint8_t* dst_y = imgBase + img->mPlane[0].mOffset;
134 uint8_t* dst_u = imgBase + img->mPlane[1].mOffset;
135 uint8_t* dst_v = imgBase + img->mPlane[2].mOffset;
136 int32_t dst_stride_y = img->mPlane[0].mRowInc;
137 int32_t dst_stride_u = img->mPlane[1].mRowInc;
138 int32_t dst_stride_v = img->mPlane[2].mRowInc;
139 int width = view.crop().width;
140 int height = view.crop().height;
141
Wonsik Kima38fdf22021-04-05 14:50:29 -0700142 if (IsNV12(view)) {
143 if (IsNV12(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700144 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700145 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
146 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
147 return OK;
148 } else if (IsNV21(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700149 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700150 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
151 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
152 return OK;
153 }
154 } else if (IsI420(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700155 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420");
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800156 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800157 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800158 return OK;
159 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700160 }
161 } else if (IsNV21(view)) {
162 if (IsNV12(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700163 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700164 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
165 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
166 return OK;
167 }
168 } else if (IsNV21(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700169 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700170 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
171 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
172 return OK;
173 } else if (IsI420(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700174 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700175 if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
176 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
177 return OK;
178 }
179 }
180 } else if (IsI420(view)) {
181 if (IsNV12(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700182 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12");
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800183 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800184 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800185 return OK;
186 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700187 } else if (IsNV21(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700188 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700189 if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
190 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
191 return OK;
192 }
193 } else if (IsI420(img)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700194 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700195 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
196 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
197 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
198 return OK;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800199 }
200 }
My Name6bd9a7d2022-03-25 12:37:58 -0700201 ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800202 return _ImageCopy<true>(view, img, imgBase);
203}
204
205status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
Harish Mahendrakarf7c49e22019-05-24 14:19:16 -0700206 if (view.crop().width != img->mWidth || view.crop().height != img->mHeight) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800207 return BAD_VALUE;
208 }
Wonsik Kimf37f2422021-03-04 15:38:59 -0800209 const uint8_t* src_y = imgBase + img->mPlane[0].mOffset;
210 const uint8_t* src_u = imgBase + img->mPlane[1].mOffset;
211 const uint8_t* src_v = imgBase + img->mPlane[2].mOffset;
212 int32_t src_stride_y = img->mPlane[0].mRowInc;
213 int32_t src_stride_u = img->mPlane[1].mRowInc;
214 int32_t src_stride_v = img->mPlane[2].mRowInc;
215 uint8_t* dst_y = view.data()[0];
216 uint8_t* dst_u = view.data()[1];
217 uint8_t* dst_v = view.data()[2];
218 int32_t dst_stride_y = view.layout().planes[0].rowInc;
219 int32_t dst_stride_u = view.layout().planes[1].rowInc;
220 int32_t dst_stride_v = view.layout().planes[2].rowInc;
221 int width = view.crop().width;
222 int height = view.crop().height;
Wonsik Kima38fdf22021-04-05 14:50:29 -0700223 if (IsNV12(img)) {
224 if (IsNV12(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700225 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV12");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700226 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
227 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width, height / 2);
228 return OK;
229 } else if (IsNV21(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700230 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->NV21");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700231 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_u, src_stride_u,
232 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
233 return OK;
234 }
235 } else if (IsI420(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700236 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV12->I420");
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800237 if (!libyuv::NV12ToI420(src_y, src_stride_y, src_u, src_stride_u, dst_y, dst_stride_y,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800238 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800239 return OK;
240 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700241 }
242 } else if (IsNV21(img)) {
243 if (IsNV12(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700244 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV12");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700245 if (!libyuv::NV21ToNV12(src_y, src_stride_y, src_v, src_stride_v,
246 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
247 return OK;
248 }
249 } else if (IsNV21(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700250 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->NV21");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700251 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
252 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width, height / 2);
253 return OK;
254 } else if (IsI420(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700255 ScopedTrace trace(ATRACE_TAG, "ImageCopy: NV21->I420");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700256 if (!libyuv::NV21ToI420(src_y, src_stride_y, src_v, src_stride_v, dst_y, dst_stride_y,
257 dst_u, dst_stride_u, dst_v, dst_stride_v, width, height)) {
258 return OK;
259 }
260 }
261 } else if (IsI420(img)) {
262 if (IsNV12(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700263 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV12");
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800264 if (!libyuv::I420ToNV12(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
Wonsik Kimf37f2422021-03-04 15:38:59 -0800265 dst_y, dst_stride_y, dst_u, dst_stride_u, width, height)) {
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800266 return OK;
267 }
Wonsik Kima38fdf22021-04-05 14:50:29 -0700268 } else if (IsNV21(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700269 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->NV21");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700270 if (!libyuv::I420ToNV21(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v,
271 dst_y, dst_stride_y, dst_v, dst_stride_v, width, height)) {
272 return OK;
273 }
274 } else if (IsI420(view)) {
My Name6bd9a7d2022-03-25 12:37:58 -0700275 ScopedTrace trace(ATRACE_TAG, "ImageCopy: I420->I420");
Wonsik Kima38fdf22021-04-05 14:50:29 -0700276 libyuv::CopyPlane(src_y, src_stride_y, dst_y, dst_stride_y, width, height);
277 libyuv::CopyPlane(src_u, src_stride_u, dst_u, dst_stride_u, width / 2, height / 2);
278 libyuv::CopyPlane(src_v, src_stride_v, dst_v, dst_stride_v, width / 2, height / 2);
279 return OK;
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800280 }
281 }
My Name6bd9a7d2022-03-25 12:37:58 -0700282 ScopedTrace trace(ATRACE_TAG, "ImageCopy: generic");
Pawin Vongmasa36653902018-11-15 00:10:25 -0800283 return _ImageCopy<false>(view, img, imgBase);
284}
285
286bool IsYUV420(const C2GraphicView &view) {
287 const C2PlanarLayout &layout = view.layout();
288 return (layout.numPlanes == 3
289 && layout.type == C2PlanarLayout::TYPE_YUV
290 && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
291 && layout.planes[layout.PLANE_Y].allocatedDepth == 8
292 && layout.planes[layout.PLANE_Y].bitDepth == 8
293 && layout.planes[layout.PLANE_Y].rightShift == 0
294 && layout.planes[layout.PLANE_Y].colSampling == 1
295 && layout.planes[layout.PLANE_Y].rowSampling == 1
296 && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
297 && layout.planes[layout.PLANE_U].allocatedDepth == 8
298 && layout.planes[layout.PLANE_U].bitDepth == 8
299 && layout.planes[layout.PLANE_U].rightShift == 0
300 && layout.planes[layout.PLANE_U].colSampling == 2
301 && layout.planes[layout.PLANE_U].rowSampling == 2
302 && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
303 && layout.planes[layout.PLANE_V].allocatedDepth == 8
304 && layout.planes[layout.PLANE_V].bitDepth == 8
305 && layout.planes[layout.PLANE_V].rightShift == 0
306 && layout.planes[layout.PLANE_V].colSampling == 2
307 && layout.planes[layout.PLANE_V].rowSampling == 2);
308}
309
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800310bool IsNV12(const C2GraphicView &view) {
311 if (!IsYUV420(view)) {
312 return false;
313 }
314 const C2PlanarLayout &layout = view.layout();
315 return (layout.rootPlanes == 2
316 && layout.planes[layout.PLANE_U].colInc == 2
317 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_U
318 && layout.planes[layout.PLANE_U].offset == 0
319 && layout.planes[layout.PLANE_V].colInc == 2
320 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_U
321 && layout.planes[layout.PLANE_V].offset == 1);
322}
323
Wonsik Kima38fdf22021-04-05 14:50:29 -0700324bool IsNV21(const C2GraphicView &view) {
325 if (!IsYUV420(view)) {
326 return false;
327 }
328 const C2PlanarLayout &layout = view.layout();
329 return (layout.rootPlanes == 2
330 && layout.planes[layout.PLANE_U].colInc == 2
331 && layout.planes[layout.PLANE_U].rootIx == layout.PLANE_V
332 && layout.planes[layout.PLANE_U].offset == 1
333 && layout.planes[layout.PLANE_V].colInc == 2
334 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
335 && layout.planes[layout.PLANE_V].offset == 0);
336}
337
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800338bool IsI420(const C2GraphicView &view) {
339 if (!IsYUV420(view)) {
340 return false;
341 }
342 const C2PlanarLayout &layout = view.layout();
343 return (layout.rootPlanes == 3
344 && layout.planes[layout.PLANE_U].colInc == 1
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 == 1
348 && layout.planes[layout.PLANE_V].rootIx == layout.PLANE_V
349 && layout.planes[layout.PLANE_V].offset == 0);
350}
351
352bool IsYUV420(const MediaImage2 *img) {
353 return (img->mType == MediaImage2::MEDIA_IMAGE_TYPE_YUV
354 && img->mNumPlanes == 3
355 && img->mBitDepth == 8
356 && img->mBitDepthAllocated == 8
357 && img->mPlane[0].mHorizSubsampling == 1
358 && img->mPlane[0].mVertSubsampling == 1
359 && img->mPlane[1].mHorizSubsampling == 2
360 && img->mPlane[1].mVertSubsampling == 2
361 && img->mPlane[2].mHorizSubsampling == 2
362 && img->mPlane[2].mVertSubsampling == 2);
363}
364
365bool IsNV12(const MediaImage2 *img) {
366 if (!IsYUV420(img)) {
367 return false;
368 }
369 return (img->mPlane[1].mColInc == 2
370 && img->mPlane[2].mColInc == 2
Harish Mahendrakarcd59f032021-04-16 18:55:41 -0700371 && (img->mPlane[2].mOffset == img->mPlane[1].mOffset + 1));
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800372}
373
Wonsik Kima38fdf22021-04-05 14:50:29 -0700374bool IsNV21(const MediaImage2 *img) {
375 if (!IsYUV420(img)) {
376 return false;
377 }
378 return (img->mPlane[1].mColInc == 2
379 && img->mPlane[2].mColInc == 2
Harish Mahendrakarcd59f032021-04-16 18:55:41 -0700380 && (img->mPlane[1].mOffset == img->mPlane[2].mOffset + 1));
Wonsik Kima38fdf22021-04-05 14:50:29 -0700381}
382
Pawin Vongmasa1f213362019-01-24 06:59:16 -0800383bool IsI420(const MediaImage2 *img) {
384 if (!IsYUV420(img)) {
385 return false;
386 }
387 return (img->mPlane[1].mColInc == 1
388 && img->mPlane[2].mColInc == 1
389 && img->mPlane[2].mOffset > img->mPlane[1].mOffset);
390}
391
Wonsik Kima38fdf22021-04-05 14:50:29 -0700392FlexLayout GetYuv420FlexibleLayout() {
393 static FlexLayout sLayout = []{
394 AHardwareBuffer_Desc desc = {
395 16, // width
396 16, // height
397 1, // layers
398 AHARDWAREBUFFER_FORMAT_Y8Cb8Cr8_420,
399 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
400 0, // stride
401 0, // rfu0
402 0, // rfu1
403 };
404 AHardwareBuffer *buffer = nullptr;
405 int ret = AHardwareBuffer_allocate(&desc, &buffer);
406 if (ret != 0) {
407 return FLEX_LAYOUT_UNKNOWN;
408 }
409 class AutoCloser {
410 public:
411 AutoCloser(AHardwareBuffer *buffer) : mBuffer(buffer), mLocked(false) {}
412 ~AutoCloser() {
413 if (mLocked) {
414 AHardwareBuffer_unlock(mBuffer, nullptr);
415 }
416 AHardwareBuffer_release(mBuffer);
417 }
418
419 void setLocked() { mLocked = true; }
420
421 private:
422 AHardwareBuffer *mBuffer;
423 bool mLocked;
424 } autoCloser(buffer);
425 AHardwareBuffer_Planes planes;
426 ret = AHardwareBuffer_lockPlanes(
427 buffer,
428 AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN | AHARDWAREBUFFER_USAGE_CPU_WRITE_OFTEN,
429 -1, // fence
430 nullptr, // rect
431 &planes);
432 if (ret != 0) {
433 AHardwareBuffer_release(buffer);
434 return FLEX_LAYOUT_UNKNOWN;
435 }
436 autoCloser.setLocked();
437 if (planes.planeCount != 3) {
438 return FLEX_LAYOUT_UNKNOWN;
439 }
440 if (planes.planes[0].pixelStride != 1) {
441 return FLEX_LAYOUT_UNKNOWN;
442 }
443 if (planes.planes[1].pixelStride == 1 && planes.planes[2].pixelStride == 1) {
444 return FLEX_LAYOUT_PLANAR;
445 }
446 if (planes.planes[1].pixelStride == 2 && planes.planes[2].pixelStride == 2) {
447 ssize_t uvDist =
448 static_cast<uint8_t *>(planes.planes[2].data) -
449 static_cast<uint8_t *>(planes.planes[1].data);
450 if (uvDist == 1) {
451 return FLEX_LAYOUT_SEMIPLANAR_UV;
452 } else if (uvDist == -1) {
453 return FLEX_LAYOUT_SEMIPLANAR_VU;
454 }
455 return FLEX_LAYOUT_UNKNOWN;
456 }
457 return FLEX_LAYOUT_UNKNOWN;
458 }();
459 return sLayout;
460}
461
Pawin Vongmasa36653902018-11-15 00:10:25 -0800462MediaImage2 CreateYUV420PlanarMediaImage2(
463 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
464 return MediaImage2 {
465 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
466 .mNumPlanes = 3,
467 .mWidth = width,
468 .mHeight = height,
469 .mBitDepth = 8,
470 .mBitDepthAllocated = 8,
471 .mPlane = {
472 {
473 .mOffset = 0,
474 .mColInc = 1,
475 .mRowInc = (int32_t)stride,
476 .mHorizSubsampling = 1,
477 .mVertSubsampling = 1,
478 },
479 {
480 .mOffset = stride * vstride,
481 .mColInc = 1,
482 .mRowInc = (int32_t)stride / 2,
483 .mHorizSubsampling = 2,
484 .mVertSubsampling = 2,
485 },
486 {
487 .mOffset = stride * vstride * 5 / 4,
488 .mColInc = 1,
489 .mRowInc = (int32_t)stride / 2,
490 .mHorizSubsampling = 2,
491 .mVertSubsampling = 2,
492 }
493 },
494 };
495}
496
497MediaImage2 CreateYUV420SemiPlanarMediaImage2(
498 uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
499 return MediaImage2 {
500 .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
501 .mNumPlanes = 3,
502 .mWidth = width,
503 .mHeight = height,
504 .mBitDepth = 8,
505 .mBitDepthAllocated = 8,
506 .mPlane = {
507 {
508 .mOffset = 0,
509 .mColInc = 1,
510 .mRowInc = (int32_t)stride,
511 .mHorizSubsampling = 1,
512 .mVertSubsampling = 1,
513 },
514 {
515 .mOffset = stride * vstride,
516 .mColInc = 2,
517 .mRowInc = (int32_t)stride,
518 .mHorizSubsampling = 2,
519 .mVertSubsampling = 2,
520 },
521 {
522 .mOffset = stride * vstride + 1,
523 .mColInc = 2,
524 .mRowInc = (int32_t)stride,
525 .mHorizSubsampling = 2,
526 .mVertSubsampling = 2,
527 }
528 },
529 };
530}
531
Manisha Jajoo478423c2021-05-20 21:28:14 +0530532// Matrix coefficient to convert RGB to Planar YUV data.
533// Each sub-array represents the 3X3 coeff used with R, G and B
534static const int16_t bt601Matrix[2][3][3] = {
Lajos Molnard842c0d2022-06-01 14:37:55 -0700535 { { 77, 150, 29 }, { -43, -85, 128 }, { 128, -107, -21 } }, /* RANGE_FULL */
Manisha Jajoo478423c2021-05-20 21:28:14 +0530536 { { 66, 129, 25 }, { -38, -74, 112 }, { 112, -94, -18 } }, /* RANGE_LIMITED */
537};
538
539static const int16_t bt709Matrix[2][3][3] = {
Lajos Molnard842c0d2022-06-01 14:37:55 -0700540 // TRICKY: 18 is adjusted to 19 so that sum of row 1 is 256
541 { { 54, 183, 19 }, { -29, -99, 128 }, { 128, -116, -12 } }, /* RANGE_FULL */
542 // TRICKY: -87 is adjusted to -86 so that sum of row 2 is 0
Manisha Jajoo478423c2021-05-20 21:28:14 +0530543 { { 47, 157, 16 }, { -26, -86, 112 }, { 112, -102, -10 } }, /* RANGE_LIMITED */
544};
545
Pawin Vongmasa36653902018-11-15 00:10:25 -0800546status_t ConvertRGBToPlanarYUV(
547 uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
Manisha Jajoo478423c2021-05-20 21:28:14 +0530548 const C2GraphicView &src, C2Color::matrix_t colorMatrix, C2Color::range_t colorRange) {
Pawin Vongmasa36653902018-11-15 00:10:25 -0800549 CHECK(dstY != nullptr);
550 CHECK((src.width() & 1) == 0);
551 CHECK((src.height() & 1) == 0);
552
553 if (dstStride * dstVStride * 3 / 2 > bufferSize) {
554 ALOGD("conversion buffer is too small for converting from RGB to YUV");
555 return NO_MEMORY;
556 }
557
558 uint8_t *dstU = dstY + dstStride * dstVStride;
559 uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
560
561 const C2PlanarLayout &layout = src.layout();
562 const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R];
563 const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
564 const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B];
565
Manisha Jajoo478423c2021-05-20 21:28:14 +0530566 // set default range as limited
567 if (colorRange != C2Color::RANGE_FULL && colorRange != C2Color::RANGE_LIMITED) {
568 colorRange = C2Color::RANGE_LIMITED;
569 }
570 const int16_t (*weights)[3] =
571 (colorMatrix == C2Color::MATRIX_BT709) ?
572 bt709Matrix[colorRange - 1] : bt601Matrix[colorRange - 1];
573 uint8_t zeroLvl = colorRange == C2Color::RANGE_FULL ? 0 : 16;
574 uint8_t maxLvlLuma = colorRange == C2Color::RANGE_FULL ? 255 : 235;
575 uint8_t maxLvlChroma = colorRange == C2Color::RANGE_FULL ? 255 : 240;
576
577#define CLIP3(min,v,max) (((v) < (min)) ? (min) : (((max) > (v)) ? (v) : (max)))
Pawin Vongmasa36653902018-11-15 00:10:25 -0800578 for (size_t y = 0; y < src.height(); ++y) {
579 for (size_t x = 0; x < src.width(); ++x) {
Manisha Jajoo478423c2021-05-20 21:28:14 +0530580 uint8_t r = *pRed;
581 uint8_t g = *pGreen;
582 uint8_t b = *pBlue;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800583
Manisha Jajoo478423c2021-05-20 21:28:14 +0530584 unsigned luma = ((r * weights[0][0] + g * weights[0][1] + b * weights[0][2]) >> 8) +
585 zeroLvl;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800586
Manisha Jajoo478423c2021-05-20 21:28:14 +0530587 dstY[x] = CLIP3(zeroLvl, luma, maxLvlLuma);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800588
589 if ((x & 1) == 0 && (y & 1) == 0) {
Manisha Jajoo478423c2021-05-20 21:28:14 +0530590 unsigned U = ((r * weights[1][0] + g * weights[1][1] + b * weights[1][2]) >> 8) +
591 128;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800592
Manisha Jajoo478423c2021-05-20 21:28:14 +0530593 unsigned V = ((r * weights[2][0] + g * weights[2][1] + b * weights[2][2]) >> 8) +
594 128;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800595
Manisha Jajoo478423c2021-05-20 21:28:14 +0530596 dstU[x >> 1] = CLIP3(zeroLvl, U, maxLvlChroma);
597 dstV[x >> 1] = CLIP3(zeroLvl, V, maxLvlChroma);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800598 }
599 pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc;
600 pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
601 pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc;
602 }
603
604 if ((y & 1) == 0) {
605 dstU += dstStride >> 1;
606 dstV += dstStride >> 1;
607 }
608
609 pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
610 pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
611 pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
612 pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
613 pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
614 pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
615
616 dstY += dstStride;
617 }
618 return OK;
619}
620
621namespace {
622
623/**
624 * A block of raw allocated memory.
625 */
626struct MemoryBlockPoolBlock {
627 MemoryBlockPoolBlock(size_t size)
628 : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
629
630 ~MemoryBlockPoolBlock() {
631 delete[] mData;
632 }
633
634 const uint8_t *data() const {
635 return mData;
636 }
637
638 size_t size() const {
639 return mSize;
640 }
641
642 C2_DO_NOT_COPY(MemoryBlockPoolBlock);
643
644private:
645 uint8_t *mData;
646 size_t mSize;
647};
648
649/**
650 * A simple raw memory block pool implementation.
651 */
652struct MemoryBlockPoolImpl {
653 void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
654 std::lock_guard<std::mutex> lock(mMutex);
655 // return block to free blocks if it is the current size; otherwise, discard
656 if (block->size() == mCurrentSize) {
657 mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
658 } else {
659 mBlocksInUse.erase(block);
660 }
661 }
662
663 std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
664 std::lock_guard<std::mutex> lock(mMutex);
665 mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
666 return block.size() != size;
667 });
668 mCurrentSize = size;
669 if (mFreeBlocks.empty()) {
670 mBlocksInUse.emplace_front(size);
671 } else {
672 mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
673 }
674 return mBlocksInUse.begin();
675 }
676
677 MemoryBlockPoolImpl() = default;
678
679 C2_DO_NOT_COPY(MemoryBlockPoolImpl);
680
681private:
682 std::mutex mMutex;
683 std::list<MemoryBlockPoolBlock> mFreeBlocks;
684 std::list<MemoryBlockPoolBlock> mBlocksInUse;
685 size_t mCurrentSize;
686};
687
688} // namespace
689
690struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
691};
692
693struct MemoryBlock::Impl {
694 Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
695 std::shared_ptr<MemoryBlockPoolImpl> pool)
696 : mBlock(block), mPool(pool) {
697 }
698
699 ~Impl() {
700 mPool->release(mBlock);
701 }
702
703 const uint8_t *data() const {
704 return mBlock->data();
705 }
706
707 size_t size() const {
708 return mBlock->size();
709 }
710
711private:
712 std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
713 std::shared_ptr<MemoryBlockPoolImpl> mPool;
714};
715
716MemoryBlock MemoryBlockPool::fetch(size_t size) {
717 std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
718 return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
719 poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
720}
721
722MemoryBlockPool::MemoryBlockPool()
723 : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
724}
725
726MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
727 : mImpl(impl) {
728}
729
730MemoryBlock::MemoryBlock() = default;
731
732MemoryBlock::~MemoryBlock() = default;
733
734const uint8_t* MemoryBlock::data() const {
735 return mImpl ? mImpl->data() : nullptr;
736}
737
738size_t MemoryBlock::size() const {
739 return mImpl ? mImpl->size() : 0;
740}
741
742MemoryBlock MemoryBlock::Allocate(size_t size) {
743 return MemoryBlockPool().fetch(size);
744}
745
746} // namespace android