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