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
24using namespace android;
25using namespace android::renderscript;
26
27struct YuvParams {
28    ObjectBaseRef<Allocation> alloc;
29};
30
31static 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
40static 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
73static 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
83extern "C" void rsdIntrinsicYuv_K(void *dst, const uchar *Y, const uchar *uv, uint32_t count, const short *param);
84
85static 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    DrvAllocation *din = (DrvAllocation *)cp->alloc->mHal.drv;
90    const uchar *pin = (const uchar *)din->lod[0].mallocPtr;
91
92    const uchar *Y = pin + (p->y * p->dimX);
93    const uchar *uv = pin + (p->dimX * p->dimY);
94    uv += (p->y>>1) * p->dimX;
95
96    uchar4 *out = (uchar4 *)p->out;
97    uint32_t x1 = xstart;
98    uint32_t x2 = xend;
99
100    if(x2 > x1) {
101#if defined(ARCH_ARM_HAVE_NEON)
102        int32_t len = (x2 - x1 - 1) >> 3;
103        if(len > 0) {
104            rsdIntrinsicYuv_K(out, Y, uv, len, YuvCoeff);
105            x1 += len << 3;
106            out += len << 3;
107        }
108#endif
109
110       // ALOGE("y %i  %i  %i", p->y, x1, x2);
111        while(x1 < x2) {
112            uchar u = uv[(x1 & 0xffffe) + 1];
113            uchar v = uv[(x1 & 0xffffe) + 0];
114            *out = rsYuvToRGBA_uchar4(Y[x1], u, v);
115            out++;
116            x1++;
117            *out = rsYuvToRGBA_uchar4(Y[x1], u, v);
118            out++;
119            x1++;
120        }
121    }
122}
123
124void * rsdIntrinsic_InitYuvToRGB(const android::renderscript::Context *dc,
125                                 android::renderscript::Script *script,
126                                 RsdIntriniscFuncs_t *funcs) {
127
128    script->mHal.info.exportedVariableCount = 1;
129    funcs->bind = YuvToRGB_Bind;
130    funcs->root = YuvToRGB_uchar4;
131    YuvParams *cp = (YuvParams *)calloc(1, sizeof(YuvParams));
132    return cp;
133}
134
135
136