blob: 3daf47dbadeb9241e8cf599f03ca40d590227b1a [file] [log] [blame]
Niko Cataniab059eb32009-03-24 20:53:55 -07001/*
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018#define LOG_TAG "FakeCamera"
19#include <utils/Log.h>
20
21#include <string.h>
22#include <stdlib.h>
Niko Cataniab059eb32009-03-24 20:53:55 -070023#include <utils/String8.h>
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include "FakeCamera.h"
26
Niko Cataniab059eb32009-03-24 20:53:55 -070027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028namespace android {
29
Niko Cataniab059eb32009-03-24 20:53:55 -070030// 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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037static 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 *yuv422,uint32_t *param,uint8_t *table[])
202{
203 uint16_t *inputRGB = (uint16_t*)rgb16;
204 uint8_t *outYUV = yuv422;
205 int32_t width_dst = param[0];
206 int32_t height_dst = param[1];
207 int32_t pitch_dst = param[2];
208 int32_t mheight_dst = param[3];
209 int32_t pitch_src = param[4];
210 uint8_t *y_tab = table[0];
211 uint8_t *cb_tab = table[1];
212 uint8_t *cr_tab = table[2];
213
214 int32_t size16 = pitch_dst*mheight_dst;
215 int32_t i,j,count;
216 int32_t ilimit,jlimit;
217 uint8_t *tempY,*tempU,*tempV;
218 uint16_t pixels;
219 int tmp;
220uint32_t temp;
221
222 tempY = outYUV;
223 tempU = outYUV + (height_dst * pitch_dst);
224 tempV = tempU + 1;
225
226 jlimit = height_dst;
227 ilimit = width_dst;
228
229 for(j=0; j<jlimit; j+=1)
230 {
231 for (i=0; i<ilimit; i+=2)
232 {
233 int32_t G_ds = 0, B_ds = 0, R_ds = 0;
234 uint8_t y0, y1, u, v;
235
236 pixels = inputRGB[i];
237 temp = (ALPHA*(pixels & 0x001F) + BETA*(pixels>>11) );
238 y0 = y_tab[(temp>>SHIFT1) + ((pixels>>3) & 0x00FC)];
239
240 G_ds += (pixels>>1) & 0x03E0;
241 B_ds += (pixels<<5) & 0x03E0;
242 R_ds += (pixels>>6) & 0x03E0;
243
244 pixels = inputRGB[i+1];
245 temp = (ALPHA*(pixels & 0x001F) + BETA*(pixels>>11) );
246 y1 = y_tab[(temp>>SHIFT1) + ((pixels>>3) & 0x00FC)];
247
248 G_ds += (pixels>>1) & 0x03E0;
249 B_ds += (pixels<<5) & 0x03E0;
250 R_ds += (pixels>>6) & 0x03E0;
251
252 R_ds >>= 1;
253 B_ds >>= 1;
254 G_ds >>= 1;
255
256 tmp = R_ds - B_ds;
257
258 u = cb_tab[(((R_ds-G_ds)<<SHIFT2) + DELTA*tmp)>>(SHIFT2+2)];
259 v = cr_tab[(((B_ds-G_ds)<<SHIFT2) - GAMMA*tmp)>>(SHIFT2+2)];
260
261 tempY[0] = y0;
262 tempY[1] = y1;
263 tempU[0] = u;
264 tempV[0] = v;
265
266 tempY += 2;
267 tempU += 2;
268 tempV += 2;
269 }
270
271 inputRGB += pitch_src;
272 }
273
274 return 1;
275}
276
277#define min(a,b) ((a)<(b)?(a):(b))
278#define max(a,b) ((a)>(b)?(a):(b))
279
280static void convert_rgb16_to_yuv422(uint8_t *rgb, uint8_t *yuv, int width, int height)
281{
282 if (!tables_initialized) {
283 initYtab();
284 initCrtab();
285 initCbtab();
286 tables_initialized = 1;
287 }
288
289 uint32_t param[6];
290 param[0] = (uint32_t) width;
291 param[1] = (uint32_t) height;
292 param[2] = (uint32_t) width;
293 param[3] = (uint32_t) height;
294 param[4] = (uint32_t) width;
295 param[5] = (uint32_t) 0;
296
297 uint8_t *table[3];
298 table[0] = gYTable;
299 table[1] = gCbTable + 384;
300 table[2] = gCrTable + 384;
301
302 ccrgb16toyuv_wo_colorkey(rgb, yuv, param, table);
303}
304
305const int FakeCamera::kRed;
306const int FakeCamera::kGreen;
307const int FakeCamera::kBlue;
308
309FakeCamera::FakeCamera(int width, int height)
310 : mTmpRgb16Buffer(0)
311{
312 setSize(width, height);
313}
314
315FakeCamera::~FakeCamera()
316{
317 delete[] mTmpRgb16Buffer;
318}
319
320void FakeCamera::setSize(int width, int height)
321{
322 mWidth = width;
323 mHeight = height;
324 mCounter = 0;
325 mCheckX = 0;
326 mCheckY = 0;
327
328 // This will cause it to be reallocated on the next call
329 // to getNextFrameAsYuv422().
330 delete[] mTmpRgb16Buffer;
331 mTmpRgb16Buffer = 0;
332}
333
334void FakeCamera::getNextFrameAsRgb565(uint16_t *buffer)
335{
336 int size = mWidth / 10;
337
338 drawCheckerboard(buffer, size);
339
340 int x = ((mCounter*3)&255);
341 if(x>128) x = 255 - x;
342 int y = ((mCounter*5)&255);
343 if(y>128) y = 255 - y;
344
345 drawSquare(buffer, x*size/32, y*size/32, (size*5)>>1, (mCounter&0x100)?kRed:kGreen, kBlue);
346
347 mCounter++;
348}
349
350void FakeCamera::getNextFrameAsYuv422(uint8_t *buffer)
351{
352 if (mTmpRgb16Buffer == 0)
353 mTmpRgb16Buffer = new uint16_t[mWidth * mHeight];
354
355 getNextFrameAsRgb565(mTmpRgb16Buffer);
356 convert_rgb16_to_yuv422((uint8_t*)mTmpRgb16Buffer, buffer, mWidth, mHeight);
357}
358
359void FakeCamera::drawSquare(uint16_t *dst, int x, int y, int size, int color, int shadow)
360{
361 int square_xstop, square_ystop, shadow_xstop, shadow_ystop;
362
363 square_xstop = min(mWidth, x+size);
364 square_ystop = min(mHeight, y+size);
365 shadow_xstop = min(mWidth, x+size+(size/4));
366 shadow_ystop = min(mHeight, y+size+(size/4));
367
368 // Do the shadow.
369 uint16_t *sh = &dst[(y+(size/4))*mWidth];
370 for (int j = y + (size/4); j < shadow_ystop; j++) {
371 for (int i = x + (size/4); i < shadow_xstop; i++) {
372 sh[i] &= shadow;
373 }
374 sh += mWidth;
375 }
376
377 // Draw the square.
378 uint16_t *sq = &dst[y*mWidth];
379 for (int j = y; j < square_ystop; j++) {
380 for (int i = x; i < square_xstop; i++) {
381 sq[i] = color;
382 }
383 sq += mWidth;
384 }
385}
386
387void FakeCamera::drawCheckerboard(uint16_t *dst, int size)
388{
389 bool black = true;
390
391 if((mCheckX/size)&1)
392 black = false;
393 if((mCheckY/size)&1)
394 black = !black;
395
396 int county = mCheckY%size;
397 int checkxremainder = mCheckX%size;
398
399 for(int y=0;y<mHeight;y++) {
400 int countx = checkxremainder;
401 bool current = black;
402 for(int x=0;x<mWidth;x++) {
403 dst[y*mWidth+x] = current?0:0xffff;
404 if(countx++ >= size) {
405 countx=0;
406 current = !current;
407 }
408 }
409 if(county++ >= size) {
410 county=0;
411 black = !black;
412 }
413 }
414 mCheckX += 3;
415 mCheckY++;
416}
417
418
Niko Cataniab059eb32009-03-24 20:53:55 -0700419void FakeCamera::dump(int fd) const
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420{
421 const size_t SIZE = 256;
422 char buffer[SIZE];
423 String8 result;
424 snprintf(buffer, 255, " width x height (%d x %d), counter (%d), check x-y coordinate(%d, %d)\n", mWidth, mHeight, mCounter, mCheckX, mCheckY);
425 result.append(buffer);
426 ::write(fd, result.string(), result.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427}
428
429
430}; // namespace android