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 "rsdCore.h"
19 #include "rsdIntrinsics.h"
20 #include "rsdAllocation.h"
21
22 #include "rsdIntrinsicInlines.h"
23
24 using namespace android;
25 using namespace android::renderscript;
26
27 struct YuvParams {
28 ObjectBaseRef<Allocation> alloc;
29 };
30
YuvToRGB_Bind(const Context * dc,const Script * script,void * intrinsicData,uint32_t slot,Allocation * data)31 static void YuvToRGB_Bind(const Context *dc, const Script *script,
32 void * intrinsicData, uint32_t slot, Allocation *data) {
33 YuvParams *cp = (YuvParams *)intrinsicData;
34 rsAssert(slot == 0);
35 cp->alloc.set(data);
36 }
37
38
39
rsYuvToRGBA_uchar4(uchar y,uchar u,uchar v)40 static uchar4 rsYuvToRGBA_uchar4(uchar y, uchar u, uchar v) {
41 short Y = ((short)y) - 16;
42 short U = ((short)u) - 128;
43 short V = ((short)v) - 128;
44
45 short4 p;
46 p.r = (Y * 298 + V * 409 + 128) >> 8;
47 p.g = (Y * 298 - U * 100 - V * 208 + 128) >> 8;
48 p.b = (Y * 298 + U * 516 + 128) >> 8;
49 p.a = 255;
50 if(p.r < 0) {
51 p.r = 0;
52 }
53 if(p.r > 255) {
54 p.r = 255;
55 }
56 if(p.g < 0) {
57 p.g = 0;
58 }
59 if(p.g > 255) {
60 p.g = 255;
61 }
62 if(p.b < 0) {
63 p.b = 0;
64 }
65 if(p.b > 255) {
66 p.b = 255;
67 }
68
69 return (uchar4){p.r, p.g, p.b, p.a};
70 }
71
72
73 static short YuvCoeff[] = {
74 298, 409, -100, 516, -208, 255, 0, 0,
75 16, 16, 16, 16, 16, 16, 16, 16,
76 128, 128, 128, 128, 128, 128, 128, 128,
77 298, 298, 298, 298, 298, 298, 298, 298,
78 255, 255, 255, 255, 255, 255, 255, 255
79
80
81 };
82
83 extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t count, const short *param);
84
YuvToRGB_uchar4(const RsForEachStubParamStruct * p,uint32_t xstart,uint32_t xend,uint32_t instep,uint32_t outstep)85 static void YuvToRGB_uchar4(const RsForEachStubParamStruct *p,
86 uint32_t xstart, uint32_t xend,
87 uint32_t instep, uint32_t outstep) {
88 YuvParams *cp = (YuvParams *)p->usr;
89 if (!cp->alloc.get()) {
90 ALOGE("YuvToRGB executed without input, skipping");
91 return;
92 }
93 DrvAllocation *din = (DrvAllocation *)cp->alloc->mHal.drv;
94 const uchar *pin = (const uchar *)din->lod[0].mallocPtr;
95
96 const uchar *Y = pin + (p->y * p->dimX);
97 const uchar *uv = pin + (p->dimX * p->dimY);
98 uv += (p->y>>1) * p->dimX;
99
100 uchar4 *out = (uchar4 *)p->out;
101 uint32_t x1 = xstart;
102 uint32_t x2 = xend;
103
104 if(x2 > x1) {
105 #if defined(ARCH_ARM_HAVE_NEON)
106 int32_t len = (x2 - x1 - 1) >> 3;
107 if(len > 0) {
108 rsdIntrinsicYuv_K(out, Y, uv, len, YuvCoeff);
109 x1 += len << 3;
110 out += len << 3;
111 }
112 #endif
113
114 // ALOGE("y %i %i %i", p->y, x1, x2);
115 while(x1 < x2) {
116 uchar u = uv[(x1 & 0xffffe) + 1];
117 uchar v = uv[(x1 & 0xffffe) + 0];
118 *out = rsYuvToRGBA_uchar4(Y[x1], u, v);
119 out++;
120 x1++;
121 *out = rsYuvToRGBA_uchar4(Y[x1], u, v);
122 out++;
123 x1++;
124 }
125 }
126 }
127
rsdIntrinsic_InitYuvToRGB(const android::renderscript::Context * dc,android::renderscript::Script * script,RsdIntriniscFuncs_t * funcs)128 void * rsdIntrinsic_InitYuvToRGB(const android::renderscript::Context *dc,
129 android::renderscript::Script *script,
130 RsdIntriniscFuncs_t *funcs) {
131
132 script->mHal.info.exportedVariableCount = 1;
133 funcs->setVarObj = YuvToRGB_Bind;
134 funcs->root = YuvToRGB_uchar4;
135 YuvParams *cp = (YuvParams *)calloc(1, sizeof(YuvParams));
136 return cp;
137 }
138
139
140