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 | |
| 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
| 24 | namespace android { |
| 25 | namespace renderscript { |
| 26 | |
| 27 | |
| 28 | class RsdCpuScriptIntrinsicYuvToRGB : public RsdCpuScriptIntrinsic { |
| 29 | public: |
| 30 | virtual void populateScript(Script *); |
| 31 | virtual void invokeFreeChildren(); |
| 32 | |
| 33 | virtual void setGlobalObj(uint32_t slot, ObjectBase *data); |
| 34 | |
| 35 | virtual ~RsdCpuScriptIntrinsicYuvToRGB(); |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 36 | RsdCpuScriptIntrinsicYuvToRGB(RsdCpuReferenceImpl *ctx, const Script *s, const Element *e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 37 | |
| 38 | protected: |
| 39 | ObjectBaseRef<Allocation> alloc; |
| 40 | |
| 41 | static void kernel(const RsForEachStubParamStruct *p, |
| 42 | uint32_t xstart, uint32_t xend, |
| 43 | uint32_t instep, uint32_t outstep); |
| 44 | }; |
| 45 | |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void RsdCpuScriptIntrinsicYuvToRGB::setGlobalObj(uint32_t slot, ObjectBase *data) { |
| 51 | rsAssert(slot == 0); |
| 52 | alloc.set(static_cast<Allocation *>(data)); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | static uchar4 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) { |
| 59 | short Y = ((short)y) - 16; |
| 60 | short U = ((short)u) - 128; |
| 61 | short V = ((short)v) - 128; |
| 62 | |
| 63 | short4 p; |
| 64 | p.r = (Y * 298 + V * 409 + 128) >> 8; |
| 65 | p.g = (Y * 298 - U * 100 - V * 208 + 128) >> 8; |
| 66 | p.b = (Y * 298 + U * 516 + 128) >> 8; |
| 67 | p.a = 255; |
| 68 | if(p.r < 0) { |
| 69 | p.r = 0; |
| 70 | } |
| 71 | if(p.r > 255) { |
| 72 | p.r = 255; |
| 73 | } |
| 74 | if(p.g < 0) { |
| 75 | p.g = 0; |
| 76 | } |
| 77 | if(p.g > 255) { |
| 78 | p.g = 255; |
| 79 | } |
| 80 | if(p.b < 0) { |
| 81 | p.b = 0; |
| 82 | } |
| 83 | if(p.b > 255) { |
| 84 | p.b = 255; |
| 85 | } |
| 86 | |
| 87 | return (uchar4){p.r, p.g, p.b, p.a}; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static short YuvCoeff[] = { |
| 92 | 298, 409, -100, 516, -208, 255, 0, 0, |
| 93 | 16, 16, 16, 16, 16, 16, 16, 16, |
| 94 | 128, 128, 128, 128, 128, 128, 128, 128, |
| 95 | 298, 298, 298, 298, 298, 298, 298, 298, |
| 96 | 255, 255, 255, 255, 255, 255, 255, 255 |
| 97 | |
| 98 | |
| 99 | }; |
| 100 | |
| 101 | extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t count, const short *param); |
| 102 | |
| 103 | void RsdCpuScriptIntrinsicYuvToRGB::kernel(const RsForEachStubParamStruct *p, |
| 104 | uint32_t xstart, uint32_t xend, |
| 105 | uint32_t instep, uint32_t outstep) { |
| 106 | RsdCpuScriptIntrinsicYuvToRGB *cp = (RsdCpuScriptIntrinsicYuvToRGB *)p->usr; |
| 107 | if (!cp->alloc.get()) { |
| 108 | ALOGE("YuvToRGB executed without input, skipping"); |
| 109 | return; |
| 110 | } |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame^] | 111 | const uchar *pinY = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr; |
| 112 | const uchar *pinUV = (const uchar *)cp->alloc->mHal.drvState.lod[1].mallocPtr; |
| 113 | const size_t strideY = cp->alloc->mHal.drvState.lod[0].stride; |
| 114 | const size_t strideUV = cp->alloc->mHal.drvState.lod[1].stride; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 115 | |
Jason Sams | bc0ca6b | 2013-02-15 18:13:43 -0800 | [diff] [blame^] | 116 | const uchar *Y = pinY + (p->y * strideY); |
| 117 | const uchar *uv = pinUV + ((p->y >> 1) * strideUV); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 118 | |
| 119 | uchar4 *out = (uchar4 *)p->out; |
| 120 | uint32_t x1 = xstart; |
| 121 | uint32_t x2 = xend; |
| 122 | |
| 123 | if(x2 > x1) { |
| 124 | #if defined(ARCH_ARM_HAVE_NEON) |
| 125 | int32_t len = (x2 - x1 - 1) >> 3; |
| 126 | if(len > 0) { |
| 127 | rsdIntrinsicYuv_K(out, Y, uv, len, YuvCoeff); |
| 128 | x1 += len << 3; |
| 129 | out += len << 3; |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | // ALOGE("y %i %i %i", p->y, x1, x2); |
| 134 | while(x1 < x2) { |
| 135 | uchar u = uv[(x1 & 0xffffe) + 1]; |
| 136 | uchar v = uv[(x1 & 0xffffe) + 0]; |
| 137 | *out = rsYuvToRGBA_uchar4(Y[x1], u, v); |
| 138 | out++; |
| 139 | x1++; |
| 140 | *out = rsYuvToRGBA_uchar4(Y[x1], u, v); |
| 141 | out++; |
| 142 | x1++; |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | RsdCpuScriptIntrinsicYuvToRGB::RsdCpuScriptIntrinsicYuvToRGB( |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 148 | RsdCpuReferenceImpl *ctx, const Script *s, const Element *e) |
| 149 | : RsdCpuScriptIntrinsic(ctx, s, e, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 150 | |
| 151 | mRootPtr = &kernel; |
| 152 | } |
| 153 | |
| 154 | RsdCpuScriptIntrinsicYuvToRGB::~RsdCpuScriptIntrinsicYuvToRGB() { |
| 155 | } |
| 156 | |
| 157 | void RsdCpuScriptIntrinsicYuvToRGB::populateScript(Script *s) { |
| 158 | s->mHal.info.exportedVariableCount = 1; |
| 159 | } |
| 160 | |
| 161 | void RsdCpuScriptIntrinsicYuvToRGB::invokeFreeChildren() { |
| 162 | alloc.clear(); |
| 163 | } |
| 164 | |
| 165 | |
Jason Sams | c905efd | 2012-11-26 15:20:18 -0800 | [diff] [blame] | 166 | RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx, |
| 167 | const Script *s, const Element *e) { |
| 168 | return new RsdCpuScriptIntrinsicYuvToRGB(ctx, s, e); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |