1/*
2**
3** Copyright 2008, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#define LOG_TAG "FakeCamera"
19#include <utils/Log.h>
20
21#include <string.h>
22#include <stdlib.h>
23#include <utils/String8.h>
24
25#include "FakeCamera.h"
26
27
28namespace android {
29
30// TODO: All this rgb to yuv should probably be in a util class.
31
32// TODO: I think something is wrong in this class because the shadow is kBlue
33// and the square color should alternate between kRed and kGreen. However on the
34// emulator screen these are all shades of gray. Y seems ok but the U and V are
35// probably not.
36
37static int tables_initialized = 0;
38uint8_t *gYTable, *gCbTable, *gCrTable;
39
40static int
41clamp(int  x)
42{
43    if (x > 255) return 255;
44    if (x < 0)   return 0;
45    return x;
46}
47
48/* the equation used by the video code to translate YUV to RGB looks like this
49 *
50 *    Y  = (Y0 - 16)*k0
51 *    Cb = Cb0 - 128
52 *    Cr = Cr0 - 128
53 *
54 *    G = ( Y - k1*Cr - k2*Cb )
55 *    R = ( Y + k3*Cr )
56 *    B = ( Y + k4*Cb )
57 *
58 */
59
60static const double  k0 = 1.164;
61static const double  k1 = 0.813;
62static const double  k2 = 0.391;
63static const double  k3 = 1.596;
64static const double  k4 = 2.018;
65
66/* let's try to extract the value of Y
67 *
68 *   G + k1/k3*R + k2/k4*B = Y*( 1 + k1/k3 + k2/k4 )
69 *
70 *   Y  = ( G + k1/k3*R + k2/k4*B ) / (1 + k1/k3 + k2/k4)
71 *   Y0 = ( G0 + k1/k3*R0 + k2/k4*B0 ) / ((1 + k1/k3 + k2/k4)*k0) + 16
72 *
73 * let define:
74 *   kYr = k1/k3
75 *   kYb = k2/k4
76 *   kYy = k0 * ( 1 + kYr + kYb )
77 *
78 * we have:
79 *    Y  = ( G + kYr*R + kYb*B )
80 *    Y0 = clamp[ Y/kYy + 16 ]
81 */
82
83static const double kYr = k1/k3;
84static const double kYb = k2/k4;
85static const double kYy = k0*( 1. + kYr + kYb );
86
87static void
88initYtab( void )
89{
90    const  int imax = (int)( (kYr + kYb)*(31 << 2) + (61 << 3) + 0.1 );
91    int    i;
92
93    gYTable = (uint8_t *)malloc(imax);
94
95    for(i=0; i<imax; i++) {
96        int  x = (int)(i/kYy + 16.5);
97        if (x < 16) x = 16;
98        else if (x > 235) x = 235;
99        gYTable[i] = (uint8_t) x;
100    }
101}
102
103/*
104 *   the source is RGB565, so adjust for 8-bit range of input values:
105 *
106 *   G = (pixels >> 3) & 0xFC;
107 *   R = (pixels >> 8) & 0xF8;
108 *   B = (pixels & 0x1f) << 3;
109 *
110 *   R2 = (pixels >> 11)      R = R2*8
111 *   B2 = (pixels & 0x1f)     B = B2*8
112 *
113 *   kYr*R = kYr2*R2 =>  kYr2 = kYr*8
114 *   kYb*B = kYb2*B2 =>  kYb2 = kYb*8
115 *
116 *   we want to use integer multiplications:
117 *
118 *   SHIFT1 = 9
119 *
120 *   (ALPHA*R2) >> SHIFT1 == R*kYr  =>  ALPHA = kYr*8*(1 << SHIFT1)
121 *
122 *   ALPHA = kYr*(1 << (SHIFT1+3))
123 *   BETA  = kYb*(1 << (SHIFT1+3))
124 */
125
126static const int  SHIFT1  = 9;
127static const int  ALPHA   = (int)( kYr*(1 << (SHIFT1+3)) + 0.5 );
128static const int  BETA    = (int)( kYb*(1 << (SHIFT1+3)) + 0.5 );
129
130/*
131 *  now let's try to get the values of Cb and Cr
132 *
133 *  R-B = (k3*Cr - k4*Cb)
134 *
135 *    k3*Cr = k4*Cb + (R-B)
136 *    k4*Cb = k3*Cr - (R-B)
137 *
138 *  R-G = (k1+k3)*Cr + k2*Cb
139 *      = (k1+k3)*Cr + k2/k4*(k3*Cr - (R-B)/k0)
140 *      = (k1 + k3 + k2*k3/k4)*Cr - k2/k4*(R-B)
141 *
142 *  kRr*Cr = (R-G) + kYb*(R-B)
143 *
144 *  Cr  = ((R-G) + kYb*(R-B))/kRr
145 *  Cr0 = clamp(Cr + 128)
146 */
147
148static const double  kRr = (k1 + k3 + k2*k3/k4);
149
150static void
151initCrtab( void )
152{
153    uint8_t *pTable;
154    int i;
155
156    gCrTable = (uint8_t *)malloc(768*2);
157
158    pTable = gCrTable + 384;
159    for(i=-384; i<384; i++)
160        pTable[i] = (uint8_t) clamp( i/kRr + 128.5 );
161}
162
163/*
164 *  B-G = (k2 + k4)*Cb + k1*Cr
165 *      = (k2 + k4)*Cb + k1/k3*(k4*Cb + (R-B))
166 *      = (k2 + k4 + k1*k4/k3)*Cb + k1/k3*(R-B)
167 *
168 *  kBb*Cb = (B-G) - kYr*(R-B)
169 *
170 *  Cb   = ((B-G) - kYr*(R-B))/kBb
171 *  Cb0  = clamp(Cb + 128)
172 *
173 */
174
175static const double  kBb = (k2 + k4 + k1*k4/k3);
176
177static void
178initCbtab( void )
179{
180    uint8_t *pTable;
181    int i;
182
183    gCbTable = (uint8_t *)malloc(768*2);
184
185    pTable = gCbTable + 384;
186    for(i=-384; i<384; i++)
187        pTable[i] = (uint8_t) clamp( i/kBb + 128.5 );
188}
189
190/*
191 *   SHIFT2 = 16
192 *
193 *   DELTA = kYb*(1 << SHIFT2)
194 *   GAMMA = kYr*(1 << SHIFT2)
195 */
196
197static const int  SHIFT2 = 16;
198static const int  DELTA  = kYb*(1 << SHIFT2);
199static const int  GAMMA  = kYr*(1 << SHIFT2);
200
201int32_t ccrgb16toyuv_wo_colorkey(uint8_t *rgb16, uint8_t *yuv420,
202        uint32_t *param, uint8_t *table[])
203{
204    uint16_t *inputRGB = (uint16_t*)rgb16;
205    uint8_t *outYUV = yuv420;
206    int32_t width_dst = param[0];
207    int32_t height_dst = param[1];
208    int32_t pitch_dst = param[2];
209    int32_t mheight_dst = param[3];
210    int32_t pitch_src = param[4];
211    uint8_t *y_tab = table[0];
212    uint8_t *cb_tab = table[1];
213    uint8_t *cr_tab = table[2];
214
215    int32_t size16 = pitch_dst*mheight_dst;
216    int32_t i,j,count;
217    int32_t ilimit,jlimit;
218    uint8_t *tempY,*tempU,*tempV;
219    uint16_t pixels;
220    int   tmp;
221uint32_t temp;
222
223    tempY = outYUV;
224    tempU = outYUV + (height_dst * pitch_dst);
225    tempV = tempU + 1;
226
227    jlimit = height_dst;
228    ilimit = width_dst;
229
230    for(j=0; j<jlimit; j+=1)
231    {
232        for (i=0; i<ilimit; i+=2)
233        {
234            int32_t   G_ds = 0, B_ds = 0, R_ds = 0;
235            uint8_t   y0, y1, u, v;
236
237            pixels =  inputRGB[i];
238            temp = (BETA*(pixels & 0x001F) + ALPHA*(pixels>>11) );
239            y0   = y_tab[(temp>>SHIFT1) + ((pixels>>3) & 0x00FC)];
240
241            G_ds    += (pixels>>1) & 0x03E0;
242            B_ds    += (pixels<<5) & 0x03E0;
243            R_ds    += (pixels>>6) & 0x03E0;
244
245            pixels =  inputRGB[i+1];
246            temp = (BETA*(pixels & 0x001F) + ALPHA*(pixels>>11) );
247            y1   = y_tab[(temp>>SHIFT1) + ((pixels>>3) & 0x00FC)];
248
249            G_ds    += (pixels>>1) & 0x03E0;
250            B_ds    += (pixels<<5) & 0x03E0;
251            R_ds    += (pixels>>6) & 0x03E0;
252
253            R_ds >>= 1;
254            B_ds >>= 1;
255            G_ds >>= 1;
256
257            tmp = R_ds - B_ds;
258
259            u = cb_tab[(((B_ds-G_ds)<<SHIFT2) - GAMMA*tmp)>>(SHIFT2+2)];
260            v = cr_tab[(((R_ds-G_ds)<<SHIFT2) + DELTA*tmp)>>(SHIFT2+2)];
261
262            tempY[0] = y0;
263            tempY[1] = y1;
264            tempY += 2;
265
266            if ((j&1) == 0) {
267                tempU[0] = u;
268                tempV[0] = v;
269                tempU += 2;
270                tempV += 2;
271            }
272        }
273
274        inputRGB += pitch_src;
275    }
276
277    return 1;
278}
279
280#define min(a,b) ((a)<(b)?(a):(b))
281#define max(a,b) ((a)>(b)?(a):(b))
282
283static void convert_rgb16_to_yuv420(uint8_t *rgb, uint8_t *yuv, int width, int height)
284{
285    if (!tables_initialized) {
286        initYtab();
287        initCrtab();
288        initCbtab();
289        tables_initialized = 1;
290    }
291
292    uint32_t param[6];
293    param[0] = (uint32_t) width;
294    param[1] = (uint32_t) height;
295    param[2] = (uint32_t) width;
296    param[3] = (uint32_t) height;
297    param[4] = (uint32_t) width;
298    param[5] = (uint32_t) 0;
299
300    uint8_t *table[3];
301    table[0] = gYTable;
302    table[1] = gCbTable + 384;
303    table[2] = gCrTable + 384;
304
305    ccrgb16toyuv_wo_colorkey(rgb, yuv, param, table);
306}
307
308const int FakeCamera::kRed;
309const int FakeCamera::kGreen;
310const int FakeCamera::kBlue;
311
312FakeCamera::FakeCamera(int width, int height)
313          : mTmpRgb16Buffer(0)
314{
315    setSize(width, height);
316}
317
318FakeCamera::~FakeCamera()
319{
320    delete[] mTmpRgb16Buffer;
321}
322
323void FakeCamera::setSize(int width, int height)
324{
325    mWidth = width;
326    mHeight = height;
327    mCounter = 0;
328    mCheckX = 0;
329    mCheckY = 0;
330
331    // This will cause it to be reallocated on the next call
332    // to getNextFrameAsYuv420().
333    delete[] mTmpRgb16Buffer;
334    mTmpRgb16Buffer = 0;
335}
336
337void FakeCamera::getNextFrameAsRgb565(uint16_t *buffer)
338{
339    int size = mWidth / 10;
340
341    drawCheckerboard(buffer, size);
342
343    int x = ((mCounter*3)&255);
344    if(x>128) x = 255 - x;
345    int y = ((mCounter*5)&255);
346    if(y>128) y = 255 - y;
347
348    drawSquare(buffer, x*size/32, y*size/32, (size*5)>>1, (mCounter&0x100)?kRed:kGreen, kBlue);
349
350    mCounter++;
351}
352
353void FakeCamera::getNextFrameAsYuv420(uint8_t *buffer)
354{
355    if (mTmpRgb16Buffer == 0)
356        mTmpRgb16Buffer = new uint16_t[mWidth * mHeight];
357
358    getNextFrameAsRgb565(mTmpRgb16Buffer);
359    convert_rgb16_to_yuv420((uint8_t*)mTmpRgb16Buffer, buffer, mWidth, mHeight);
360}
361
362void FakeCamera::drawSquare(uint16_t *dst, int x, int y, int size, int color, int shadow)
363{
364    int square_xstop, square_ystop, shadow_xstop, shadow_ystop;
365
366    square_xstop = min(mWidth, x+size);
367    square_ystop = min(mHeight, y+size);
368    shadow_xstop = min(mWidth, x+size+(size/4));
369    shadow_ystop = min(mHeight, y+size+(size/4));
370
371    // Do the shadow.
372    uint16_t *sh = &dst[(y+(size/4))*mWidth];
373    for (int j = y + (size/4); j < shadow_ystop; j++) {
374        for (int i = x + (size/4); i < shadow_xstop; i++) {
375            sh[i] &= shadow;
376        }
377        sh += mWidth;
378    }
379
380    // Draw the square.
381    uint16_t *sq = &dst[y*mWidth];
382    for (int j = y; j < square_ystop; j++) {
383        for (int i = x; i < square_xstop; i++) {
384            sq[i] = color;
385        }
386        sq += mWidth;
387    }
388}
389
390void FakeCamera::drawCheckerboard(uint16_t *dst, int size)
391{
392    bool black = true;
393
394    if((mCheckX/size)&1)
395        black = false;
396    if((mCheckY/size)&1)
397        black = !black;
398
399    int county = mCheckY%size;
400    int checkxremainder = mCheckX%size;
401
402    for(int y=0;y<mHeight;y++) {
403        int countx = checkxremainder;
404        bool current = black;
405        for(int x=0;x<mWidth;x++) {
406            dst[y*mWidth+x] = current?0:0xffff;
407            if(countx++ >= size) {
408                countx=0;
409                current = !current;
410            }
411        }
412        if(county++ >= size) {
413            county=0;
414            black = !black;
415        }
416    }
417    mCheckX += 3;
418    mCheckY++;
419}
420
421
422void FakeCamera::dump(int fd) const
423{
424    const size_t SIZE = 256;
425    char buffer[SIZE];
426    String8 result;
427    snprintf(buffer, 255, " width x height (%d x %d), counter (%d), check x-y coordinate(%d, %d)\n", mWidth, mHeight, mCounter, mCheckX, mCheckY);
428    result.append(buffer);
429    ::write(fd, result.string(), result.size());
430}
431
432
433}; // namespace android
434