rsCpuIntrinsicYuvToRGB.cpp revision 709a0978ae141198018ca9769f8d96292a8928e6
1/* 2 * Copyright (C) 2012 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 18#include "rsCpuIntrinsic.h" 19#include "rsCpuIntrinsicInlines.h" 20 21using namespace android; 22using namespace android::renderscript; 23 24namespace android { 25namespace renderscript { 26 27 28class RsdCpuScriptIntrinsicYuvToRGB : public RsdCpuScriptIntrinsic { 29public: 30 virtual void populateScript(Script *); 31 virtual void invokeFreeChildren(); 32 33 virtual void setGlobalObj(uint32_t slot, ObjectBase *data); 34 35 virtual ~RsdCpuScriptIntrinsicYuvToRGB(); 36 RsdCpuScriptIntrinsicYuvToRGB(RsdCpuReferenceImpl *ctx, const Script *s); 37 38protected: 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 50void RsdCpuScriptIntrinsicYuvToRGB::setGlobalObj(uint32_t slot, ObjectBase *data) { 51 rsAssert(slot == 0); 52 alloc.set(static_cast<Allocation *>(data)); 53} 54 55 56 57 58static 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 91static 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 101extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t count, const short *param); 102 103void 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 } 111 const uchar *pin = (const uchar *)cp->alloc->mHal.drvState.lod[0].mallocPtr; 112 const size_t stride = cp->alloc->mHal.drvState.lod[0].stride; 113 114 const uchar *Y = pin + (p->y * p->dimX); 115 const uchar *uv = pin + (p->dimX * p->dimY); 116 uv += (p->y>>1) * p->dimX; 117 118 uchar4 *out = (uchar4 *)p->out; 119 uint32_t x1 = xstart; 120 uint32_t x2 = xend; 121 122 if(x2 > x1) { 123#if defined(ARCH_ARM_HAVE_NEON) 124 int32_t len = (x2 - x1 - 1) >> 3; 125 if(len > 0) { 126 rsdIntrinsicYuv_K(out, Y, uv, len, YuvCoeff); 127 x1 += len << 3; 128 out += len << 3; 129 } 130#endif 131 132 // ALOGE("y %i %i %i", p->y, x1, x2); 133 while(x1 < x2) { 134 uchar u = uv[(x1 & 0xffffe) + 1]; 135 uchar v = uv[(x1 & 0xffffe) + 0]; 136 *out = rsYuvToRGBA_uchar4(Y[x1], u, v); 137 out++; 138 x1++; 139 *out = rsYuvToRGBA_uchar4(Y[x1], u, v); 140 out++; 141 x1++; 142 } 143 } 144} 145 146RsdCpuScriptIntrinsicYuvToRGB::RsdCpuScriptIntrinsicYuvToRGB( 147 RsdCpuReferenceImpl *ctx, const Script *s) 148 : RsdCpuScriptIntrinsic(ctx, s, RS_SCRIPT_INTRINSIC_ID_YUV_TO_RGB) { 149 150 mRootPtr = &kernel; 151} 152 153RsdCpuScriptIntrinsicYuvToRGB::~RsdCpuScriptIntrinsicYuvToRGB() { 154} 155 156void RsdCpuScriptIntrinsicYuvToRGB::populateScript(Script *s) { 157 s->mHal.info.exportedVariableCount = 1; 158} 159 160void RsdCpuScriptIntrinsicYuvToRGB::invokeFreeChildren() { 161 alloc.clear(); 162} 163 164 165RsdCpuScriptImpl * rsdIntrinsic_YuvToRGB(RsdCpuReferenceImpl *ctx, const Script *s) { 166 return new RsdCpuScriptIntrinsicYuvToRGB(ctx, s); 167} 168 169 170