visageSDK
AndroidCameraCapture.cpp
1 #include "AndroidCameraCapture.h"
2 #include <android/log.h>
3 
4 #define LOG_TAG "AndroidCameraCapture"
5 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
6 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
7 
8 namespace VisageSDK
9 {
10 
12 {
13  pts = 0;
14 }
15 
16 AndroidCameraCapture::AndroidCameraCapture(int width, int height, int orientation, int flip)
17 {
18  buffer = vsCreateImage(vsSize(width, height),VS_DEPTH_8U,3);
19  bufferN = vsCreateImage(vsSize(width, height),VS_DEPTH_8U,3);
20  bufferT = vsCreateImage(vsSize(height, width),VS_DEPTH_8U,3);
21  pthread_mutex_init(&mutex, NULL);
22  pthread_cond_init(&cond, NULL);
23  pts = 0;
24  frameArrived = false;
25  this->orientation = orientation;
26  this->flip = flip;
27  this->width = width;
28  this->height = height;
29 }
30 
32 {
33  // cleaning up
34  vsReleaseImage(&buffer);
35  vsReleaseImage(&bufferN);
36  vsReleaseImage(&bufferT);
37  pthread_mutex_destroy(&mutex);
38  pthread_cond_destroy(&cond);
39 }
40 
41 void AndroidCameraCapture::WriteFrame(unsigned char* imageData)
42 {
43  pthread_mutex_lock(&mutex);
44  //data = imageData;
45  //YUV420toRGB(data, buffer, width, height);
46  for (int i=0;i<width*height;i++){
47  buffer->imageData[i*3] = imageData[i*4];
48  buffer->imageData[i*3+1] = imageData[i*4+1];
49  buffer->imageData[i*3+2] = imageData[i*4+2];
50  }
51  frameArrived = true;
52  pthread_cond_signal(&cond);
53  pthread_mutex_unlock(&mutex);
54 }
55 
56 void AndroidCameraCapture::WriteFrameYUV(unsigned char* imageData)
57 {
58  pthread_mutex_lock(&mutex);
59  //YUV420toRGB(imageData, buffer, width, height);
60  YUV_NV21_TO_RGB(imageData, buffer, width, height);
61  frameArrived = true;
62  pthread_cond_signal(&cond);
63  pthread_mutex_unlock(&mutex);
64 }
65 
67 {
68  struct timespec ts;
69  struct timeval tp;
70  VsImage* ret;
71 
72  int rc = gettimeofday(&tp, NULL);
73  ts.tv_sec = tp.tv_sec + 2;
74  ts.tv_nsec = 0;
75  pthread_mutex_lock(&mutex);
76  while (!frameArrived){
77  int ret_cond = pthread_cond_timedwait(&cond, &mutex, &ts);
78  if (ret_cond == ETIMEDOUT){
79  pthread_mutex_unlock(&mutex);
80  return 0;
81  }
82  }
83  frameArrived = false;
84 
85  switch(orientation){
86  case 0: case 360:
87  if (flip)
88  vsFlip(buffer, bufferN, 1);
89  else
90  vsCopy(buffer, bufferN);
91  ret = bufferN;
92  break;
93  case 90:
94  vsTranspose(buffer, bufferT);
95  if (!flip)
96  vsFlip(bufferT, bufferT, 1);
97  ret = bufferT;
98  break;
99  case 180:
100  if (flip)
101  vsFlip(buffer, bufferN, 0);
102  else
103  vsFlip(buffer, bufferN, -1);
104  ret = bufferN;
105  break;
106  case 270:
107  vsTranspose(buffer, bufferT);
108  if (flip)
109  vsFlip(bufferT, bufferT, -1);
110  else
111  vsFlip(bufferT, bufferT, 0);
112  ret = bufferT;
113  break;
114  }
115 
116  pthread_mutex_unlock(&mutex);
117  timeStamp = pts++;
118  return ret;
119 }
120 void AndroidCameraCapture::YUV420toRGB(unsigned char* data, VsImage* buff, int width, int height){
121  int size = width*height;
122  int offset = size;
123  int u, v, y1, y2, y3, y4;
124 
125 
126  // i along Y and the final pixels
127  // k along pixels U and V
128  for(int i=0, k=0; i < size; i+=2, k+=2) {
129  y1 = data[i ]&0xff;
130  y2 = data[i+1]&0xff;
131  y3 = data[width+i ]&0xff;
132  y4 = data[width+i+1]&0xff;
133 
134  u = data[offset+k ]&0xff;
135  v = data[offset+k+1]&0xff;
136  u = u-128;
137  v = v-128;
138 
139  convertYUVtoARGB(y1,u,v, &buff->imageData[i*3], &buff->imageData[i*3+1], &buff->imageData[i*3+2]);
140  convertYUVtoARGB(y2,u,v, &buff->imageData[(i+1)*3], &buff->imageData[(i+1)*3+1], &buff->imageData[(i+1)*3+2]);
141  convertYUVtoARGB(y3,u,v, &buff->imageData[(width+i)*3], &buff->imageData[(width+i)*3+1], &buff->imageData[(width+i)*3+2]);
142  convertYUVtoARGB(y4,u,v, &buff->imageData[(width+i+1)*3], &buff->imageData[(width+i+1)*3+1], &buff->imageData[(width+i+1)*3+2]);
143 
144  if (i!=0 && (i+2)%width==0)
145  i+=width;
146  }
147 }
148 
149 int AndroidCameraCapture::clamp(int x) {
150  unsigned y;
151  return !(y=x>>8) ? x : (0xff ^ (y>>24));
152 }
153 
154 void AndroidCameraCapture::YUV_NV21_TO_RGB(unsigned char* yuv, VsImage* buff, int width, int height)
155 {
156  const int frameSize = width * height;
157 
158  const int ii = 0;
159  const int ij = 0;
160  const int di = +1;
161  const int dj = +1;
162 
163  unsigned char* rgb = (unsigned char*)buff->imageData;
164 
165  int a = 0;
166  for (int i = 0, ci = ii; i < height; ++i, ci += di)
167  {
168  for (int j = 0, cj = ij; j < width; ++j, cj += dj)
169  {
170  int y = (0xff & ((int) yuv[ci * width + cj]));
171  int v = (0xff & ((int) yuv[frameSize + (ci >> 1) * width + (cj & ~1) + 0]));
172  int u = (0xff & ((int) yuv[frameSize + (ci >> 1) * width + (cj & ~1) + 1]));
173  y = y < 16 ? 16 : y;
174 
175  //int r = (int) (1.164f * (y - 16) + 1.596f * (v - 128));
176  //int g = (int) (1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));
177  //int b = (int) (1.164f * (y - 16) + 2.018f * (u - 128));
178 
179  int a0 = 1192 * (y - 16);
180  int a1 = 1634 * (v - 128);
181  int a2 = 832 * (v - 128);
182  int a3 = 400 * (u - 128);
183  int a4 = 2066 * (u - 128);
184 
185  int r = (a0 + a1) >> 10;
186  int g = (a0 - a2 - a3) >> 10;
187  int b = (a0 + a4) >> 10;
188 
189  *rgb++ = clamp(r);
190  *rgb++ = clamp(g);
191  *rgb++ = clamp(b);
192  }
193  }
194 }
195 
196 void AndroidCameraCapture::convertYUVtoARGB(int y, int u, int v, char *r, char *g, char *b) {
197  int rr, gg, bb;
198  rr = y + (int)1.402f*u;
199  gg = y - (int)(0.344f*v +0.714f*u);
200  bb = y + (int)1.772f*v;
201  *r = (rr)>255? 255 : (rr)<0 ? 0 : (rr);
202  *g = (gg)>255? 255 : (gg)<0 ? 0 : (gg);
203  *b = (bb)>255? 255 : (bb)<0 ? 0 : (bb);
204 }
205 }
void WriteFrame(unsigned char *imageData)
Method for writing imageData to buffer object.
char * imageData
Pointer to aligned image data.
Namespace.
Definition: android_header.h:1
VsImage * GrabFrame(long &timeStamp)
This function is called periodically to get the new video frame to process.
Image store container.