Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 1 | /* |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 2 | * Copyright (C) 2013 The Android Open Source Project |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 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 | |
| 18 | #include "rsCpuIntrinsic.h" |
| 19 | #include "rsCpuIntrinsicInlines.h" |
| 20 | |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 21 | #ifndef RS_COMPATIBILITY_LIB |
| 22 | #include "hardware/gralloc.h" |
| 23 | #endif |
| 24 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 25 | using namespace android; |
| 26 | using namespace android::renderscript; |
| 27 | |
| 28 | namespace android { |
| 29 | namespace renderscript { |
| 30 | |
| 31 | |
| 32 | class RsdCpuScriptIntrinsicYuvToRGB : public RsdCpuScriptIntrinsic { |
| 33 | public: |
| 34 | virtual void populateScript(Script *); |
| 35 | virtual void invokeFreeChildren(); |
| 36 | |
| 37 | virtual void setGlobalObj(uint32_t slot, ObjectBase *data); |
| 38 | |
| 39 | virtual ~RsdCpuScriptIntrinsicYuvToRGB(); |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 40 | RsdCpuScriptIntrinsicYuvToRGB(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | ObjectBaseRef<Allocation> alloc; |
| 44 | |
| 45 | static void kernel(const RsForEachStubParamStruct *p, |
| 46 | uint32_t xstart, uint32_t xend, |
| 47 | uint32_t instep, uint32_t outstep); |
| 48 | }; |
| 49 | |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | void RsdCpuScriptIntrinsicYuvToRGB::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| 55 | rsAssert(slot == 0); |
| 56 | alloc.set(static_cast<Allocation *>(data)); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | static uchar4 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) { |
| 63 | short Y = ((short)y) - 16; |
| 64 | short U = ((short)u) - 128; |
| 65 | short V = ((short)v) - 128; |
| 66 | |
| 67 | short4 p; |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 68 | p.x = (Y * 298 + V * 409 + 128) >> 8; |
| 69 | p.y = (Y * 298 - U * 100 - V * 208 + 128) >> 8; |
| 70 | p.z = (Y * 298 + U * 516 + 128) >> 8; |
| 71 | p.w = 255; |
| 72 | if(p.x < 0) { |
| 73 | p.x = 0; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 74 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 75 | if(p.x > 255) { |
| 76 | p.x = 255; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 77 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 78 | if(p.y < 0) { |
| 79 | p.y = 0; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 80 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 81 | if(p.y > 255) { |
| 82 | p.y = 255; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 83 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 84 | if(p.z < 0) { |
| 85 | p.z = 0; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 86 | } |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 87 | if(p.z > 255) { |
| 88 | p.z = 255; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Tim Murray | 0b575de | 2013-03-15 15:56:43 -0700 | [diff] [blame] | 91 | return (uchar4){p.x, p.y, p.z, p.w}; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | |
| 95 | static short YuvCoeff[] = { |
| 96 | 298, 409, -100, 516, -208, 255, 0, 0, |
| 97 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 98 | 128, 128, 128, 128, 128, 128, 128, 128, |
| 99 | 298, 298, 298, 298, 298, 298, 298, 298, |
| 100 | 255, 255, 255, 255, 255, 255, 255, 255 |
| 101 | |
| 102 | |
| 103 | }; |
| 104 | |
| 105 | extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t count, const short *param); |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 106 | extern "C" void rsdIntrinsicYuv2_K(void *dst, const uchar *Y, const uchar *u, const uchar *v, uint32_t count, const short *param); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 107 | |
| 108 | void RsdCpuScriptIntrinsicYuvToRGB::kernel(const RsForEachStubParamStruct *p, |
| 109 | uint32_t xstart, uint32_t xend, |
| 110 | uint32_t instep, uint32_t outstep) { |
| 111 | RsdCpuScriptIntrinsicYuvToRGB *cp = (RsdCpuScriptIntrinsicYuvToRGB *)p->usr; |
| 112 | if (!cp->alloc.get()) { |
| 113 | ALOGE("YuvToRGB executed without input, skipping"); |
| 114 | return; |
| 115 | } |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 116 | const uchar *pinY = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 117 | const size_t strideY = cp->alloc->mHal.drvState.lod[0].stride; |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame] | 118 | const uchar *Y = pinY + (p->y * strideY); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 119 | |
| 120 | uchar4 *out = (uchar4 *)p->out; |
| 121 | uint32_t x1 = xstart; |
| 122 | uint32_t x2 = xend; |
| 123 | |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 124 | switch (cp->alloc->mHal.state.yuv) { |
| 125 | // In API 17 there was no yuv format and the intrinsic treated everything as NV21 |
| 126 | case 0: |
| 127 | #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) |
| 128 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: // NV21 |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 129 | #endif |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 130 | { |
| 131 | const uchar *pinUV = (const uchar *)cp->alloc->mHal.drvState.lod[1].mallocPtr; |
| 132 | const size_t strideUV = cp->alloc->mHal.drvState.lod[1].stride; |
| 133 | const uchar *uv = pinUV + ((p->y >> 1) * strideUV); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 134 | |
Jason Sams | 06bd91e | 2013-06-11 18:38:11 -0700 | [diff] [blame^] | 135 | if (pinUV == NULL) { |
| 136 | // Legacy yuv support didn't fill in uv |
| 137 | uv = ((uint8_t *)cp->alloc->mHal.drvState.lod[0].mallocPtr) + |
| 138 | (cp->alloc->mHal.drvState.lod[0].stride * |
| 139 | cp->alloc->mHal.drvState.lod[0].dimY); |
| 140 | } |
| 141 | |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 142 | if(x2 > x1) { |
| 143 | #if defined(ARCH_ARM_HAVE_NEON) |
| 144 | int32_t len = (x2 - x1 - 1) >> 3; |
| 145 | if(len > 0) { |
| 146 | rsdIntrinsicYuv_K(out, Y, uv, len, YuvCoeff); |
| 147 | x1 += len << 3; |
| 148 | out += len << 3; |
| 149 | } |
| 150 | #endif |
| 151 | |
| 152 | // ALOGE("y %i %i %i", p->y, x1, x2); |
| 153 | while(x1 < x2) { |
| 154 | uchar u = uv[(x1 & 0xffffe) + 1]; |
| 155 | uchar v = uv[(x1 & 0xffffe) + 0]; |
| 156 | *out = rsYuvToRGBA_uchar4(Y[x1], u, v); |
| 157 | out++; |
| 158 | x1++; |
| 159 | *out = rsYuvToRGBA_uchar4(Y[x1], u, v); |
| 160 | out++; |
| 161 | x1++; |
| 162 | } |
| 163 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 164 | } |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 165 | break; |
| 166 | |
| 167 | #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB) |
| 168 | case HAL_PIXEL_FORMAT_YV12: |
| 169 | { |
| 170 | const uchar *pinU = (const uchar *)cp->alloc->mHal.drvState.lod[1].mallocPtr; |
| 171 | const size_t strideU = cp->alloc->mHal.drvState.lod[1].stride; |
| 172 | const uchar *u = pinU + ((p->y >> 1) * strideU); |
| 173 | |
| 174 | const uchar *pinV = (const uchar *)cp->alloc->mHal.drvState.lod[2].mallocPtr; |
| 175 | const size_t strideV = cp->alloc->mHal.drvState.lod[2].stride; |
| 176 | const uchar *v = pinV + ((p->y >> 1) * strideV); |
| 177 | |
| 178 | if(x2 > x1) { |
| 179 | #if defined(ARCH_ARM_HAVE_NEON) |
| 180 | int32_t len = (x2 - x1 - 1) >> 3; |
| 181 | if(len > 0) { |
| 182 | rsdIntrinsicYuv2_K(out, Y, u, v, len, YuvCoeff); |
| 183 | x1 += len << 3; |
| 184 | out += len << 3; |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | // ALOGE("y %i %i %i", p->y, x1, x2); |
| 189 | while(x1 < x2) { |
| 190 | uchar ut = u[x1]; |
| 191 | uchar vt = v[x1]; |
| 192 | *out = rsYuvToRGBA_uchar4(Y[x1], ut, vt); |
| 193 | out++; |
| 194 | x1++; |
| 195 | *out = rsYuvToRGBA_uchar4(Y[x1], ut, vt); |
| 196 | out++; |
| 197 | x1++; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | break; |
| 202 | #endif |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 203 | } |
Jason Sams | 6b58909 | 2013-04-19 14:32:31 -0700 | [diff] [blame] | 204 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | RsdCpuScriptIntrinsicYuvToRGB::RsdCpuScriptIntrinsicYuvToRGB( |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 208 | RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) |
| 209 | : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 210 | |
| 211 | mRootPtr = &kernel; |
| 212 | } |
| 213 | |
| 214 | RsdCpuScriptIntrinsicYuvToRGB::~RsdCpuScriptIntrinsicYuvToRGB() { |
| 215 | } |
| 216 | |
| 217 | void RsdCpuScriptIntrinsicYuvToRGB::populateScript(Script *s) { |
| 218 | s->mHal.info.exportedVariableCount = 1; |
| 219 | } |
| 220 | |
| 221 | void RsdCpuScriptIntrinsicYuvToRGB::invokeFreeChildren() { |
| 222 | alloc.clear(); |
| 223 | } |
| 224 | |
| 225 | |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 226 | RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx, |
| 227 | const Script *s, const Element *e) { |
| 228 | return new RsdCpuScriptIntrinsicYuvToRGB(ctx, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | |