-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathofxAndroidVideoGrabber.cpp
More file actions
775 lines (644 loc) · 22.5 KB
/
ofxAndroidVideoGrabber.cpp
File metadata and controls
775 lines (644 loc) · 22.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
/*
* ofxAndroidVideoGrabber.cpp
*
* Created on: 09/07/2010
* Author: arturo
*/
#include "ofxAndroidVideoGrabber.h"
#include "ofxAndroidUtils.h"
#include <map>
#include "ofAppRunner.h"
#include "ofUtils.h"
#include "ofVideoGrabber.h"
#include "ofGLUtils.h"
#include "ofMatrix4x4.h"
using namespace std;
struct ofxAndroidVideoGrabber::Data{
bool bIsFrameNew;
bool bGrabberInited;
bool bUsePixels;
int width;
int height;
ofPixelFormat internalPixelFormat;
bool bNewBackFrame;
ofPixels frontBuffer, backBuffer, resizeBuffer;
ofTexture texture;
jfloatArray matrixJava;
int cameraId;
bool appPaused;
bool newPixels;
int attemptFramerate;
jobject javaVideoGrabber;
Data();
~Data();
void onAppPause();
void onAppResume();
void loadTexture();
void update();
};
map<int,weak_ptr<ofxAndroidVideoGrabber::Data>> & instances(){
static auto * instances = new map<int,weak_ptr<ofxAndroidVideoGrabber::Data>>;
return *instances;
}
static jclass getJavaClass(){
JNIEnv *env = ofGetJNIEnv();
jclass javaClass = env->FindClass("cc/openframeworks/OFAndroidVideoGrabber");
if(javaClass==NULL){
ofLogError("ofxAndroidVideoGrabber") << "couldn't find OFAndroidVideoGrabber java class";
}
return javaClass;
}
static void InitConvertTable();
static void ConvertYUV2RGB(unsigned char *src0,unsigned char *src1,unsigned char *dst_ori,
int width,int height);
static void ConvertYUV2toRGB565(unsigned char* yuvs, unsigned char* rgbs, int width, int height);
ofxAndroidVideoGrabber::Data::Data()
:bIsFrameNew(false)
,bGrabberInited(false)
,bUsePixels(true)
,cameraId(-1)
,appPaused(false)
,newPixels(false)
,attemptFramerate(-1)
,javaVideoGrabber(nullptr){
JNIEnv *env = ofGetJNIEnv();
jclass javaClass = getJavaClass();
if(!javaClass) return;
jmethodID videoGrabberConstructor = env->GetMethodID(javaClass,"<init>","()V");
if(!videoGrabberConstructor){
ofLogError("ofxAndroidVideoGrabber") << "initGrabber(): couldn't find OFAndroidVideoGrabber constructor";
return;
}
javaVideoGrabber = env->NewObject(javaClass,videoGrabberConstructor);
javaVideoGrabber = (jobject)env->NewGlobalRef(javaVideoGrabber);
jmethodID javaCameraId = env->GetMethodID(javaClass,"getId","()I");
if(javaVideoGrabber && javaCameraId){
cameraId = env->CallIntMethod(javaVideoGrabber,javaCameraId);
}else{
ofLogError("ofxAndroidVideoGrabber") << "initGrabber(): couldn't get OFAndroidVideoGrabber camera id";
}
InitConvertTable();
internalPixelFormat = OF_PIXELS_RGB;
jfloatArray localMatrixJava = ofGetJNIEnv()->NewFloatArray(16);
matrixJava = (jfloatArray) ofGetJNIEnv()->NewGlobalRef(localMatrixJava);
ofAddListener(ofxAndroidEvents().unloadGL,this,&ofxAndroidVideoGrabber::Data::onAppPause);
ofAddListener(ofxAndroidEvents().reloadGL,this,&ofxAndroidVideoGrabber::Data::onAppResume);
}
void ofxAndroidVideoGrabber::Data::update(){
JNIEnv *env = ofGetJNIEnv();
jmethodID getTextureMatrix = env->GetMethodID(getJavaClass(), "getTextureMatrix", "([F)V");
env->CallVoidMethod(javaVideoGrabber, getTextureMatrix, matrixJava);
jfloat* cfloats = env->GetFloatArrayElements(matrixJava, 0);
ofMatrix4x4 mat(cfloats);
if(mat(0,0) == -1){
mat.scale(-1,1,1);
mat.translate(1,0,0);
}
if(mat(1,1) == -1){
mat.scale(1,-1,1);
mat.translate(0,1,0);
}
texture.setTextureMatrix(mat);
}
ofxAndroidVideoGrabber::Data::~Data(){
JNIEnv *env = ofGetJNIEnv();
jclass javaClass = getJavaClass();
if(!javaClass) return;
jmethodID javaOFDestructor = env->GetMethodID(javaClass,"release","()V");
if(javaVideoGrabber && javaOFDestructor){
env->CallVoidMethod(javaVideoGrabber,javaOFDestructor);
env->DeleteGlobalRef(javaVideoGrabber);
}
if(matrixJava) ofGetJNIEnv()->DeleteGlobalRef(matrixJava);
ofRemoveListener(ofxAndroidEvents().unloadGL,this,&ofxAndroidVideoGrabber::Data::onAppPause);
ofRemoveListener(ofxAndroidEvents().reloadGL,this,&ofxAndroidVideoGrabber::Data::onAppResume);
}
void ofxAndroidVideoGrabber::Data::loadTexture(){
ofTextureData td;
GLuint texId[1];
glGenTextures(1, texId);
glEnable(GL_TEXTURE_EXTERNAL_OES);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, texId[0]);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#ifndef TARGET_PROGRAMMABLE_GL
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif
glDisable(GL_TEXTURE_EXTERNAL_OES);
// Set the externally created texture reference
texture.setUseExternalTextureID(texId[0]);
texture.texData.width = width;
texture.texData.height = height;
texture.texData.tex_w = width;
texture.texData.tex_h = height;
texture.texData.tex_t = 1; // Hack!
texture.texData.tex_u = 1;
texture.texData.textureTarget = GL_TEXTURE_EXTERNAL_OES;
texture.texData.glInternalFormat = GL_RGBA;
}
ofxAndroidVideoGrabber::ofxAndroidVideoGrabber()
:data(new Data){
if(data->cameraId!=-1){
instances().insert(std::pair<int,weak_ptr<ofxAndroidVideoGrabber::Data>>(data->cameraId,data));
}
}
ofxAndroidVideoGrabber::~ofxAndroidVideoGrabber(){
}
void ofxAndroidVideoGrabber::Data::onAppPause(){
appPaused = true;
glDeleteTextures(1, &texture.texData.textureID);
texture.texData.textureID = 0;
ofLogVerbose("ofxAndroidVideoGrabber") << "ofPauseVideoGrabbers(): releasing textures";
}
void ofxAndroidVideoGrabber::Data::onAppResume(){
if(!ofxAndroidCheckPermission(OFX_ANDROID_PERMISSION_CAMERA)) return;
ofLogVerbose("ofxAndroidVideoGrabber") << "ofResumeVideoGrabbers(): trying to allocate textures";
JNIEnv *env = ofGetJNIEnv();
if(!env){
ofLogError("ofxAndroidVideoGrabber") << "init grabber failed : couldn't get environment using GetEnv()";
return;
}
jclass javaClass = getJavaClass();
jmethodID javaInitGrabber = env->GetMethodID(javaClass,"initGrabber","(IIII)V");
loadTexture();
int texID= texture.texData.textureID;
int w=width;
int h=height;
env->CallVoidMethod(javaVideoGrabber,javaInitGrabber,w,h,attemptFramerate,texID);
ofLogVerbose("ofxAndroidVideoGrabber") << "ofResumeVideoGrabbers(): textures allocated";
bGrabberInited = true;
appPaused = false;
}
vector<ofVideoDevice> ofxAndroidVideoGrabber::listDevices() const{
vector<ofVideoDevice> devices;
int numDevices = getNumCameras();
for(int i = 0; i < numDevices; i++){
int facing = getFacingOfCamera(i);
ofVideoDevice vd;
vd.deviceName = facing == 0? "Back" : "Front";
vd.id = i;
vd.bAvailable = true;
devices.push_back(vd);
}
return devices;
}
bool ofxAndroidVideoGrabber::isFrameNew() const{
return data->bIsFrameNew;
}
void ofxAndroidVideoGrabber::update(){
if(data->appPaused){
//ofLogWarning("ofxAndroidVideoGrabber") << "update(): couldn't grab frame, movie is paused";
return;
}
//ofLogVerbose("ofxAndroidVideoGrabber") << "update(): updating camera";
if(data->bGrabberInited && data->newPixels){
//ofLogVerbose("ofxAndroidVideoGrabber") << "update(): new pixels";
data->newPixels = false;
data->bIsFrameNew = true;
if (data->bNewBackFrame && data->bUsePixels) {
//std::unique_lock <std::mutex> lck(data->mtx);
std::swap(data->backBuffer, data->frontBuffer);
data->bNewBackFrame = false;
}
// Call update in the java code
// This will tell the camera api that we are ready for a new frame
jmethodID update = ofGetJNIEnv()->GetMethodID(getJavaClass(), "update", "()V");
ofGetJNIEnv()->CallVoidMethod(data->javaVideoGrabber, update);
data->update();
} else {
data->bIsFrameNew = false;
}
}
void ofxAndroidVideoGrabber::close(){
// Release texture
glDeleteTextures(1, &data->texture.texData.textureID);
JNIEnv *env = ofGetJNIEnv();
jclass javaClass = getJavaClass();
jmethodID javaCloseGrabber = env->GetMethodID(javaClass,"close","()V");
if(data->javaVideoGrabber && javaCloseGrabber){
env->CallVoidMethod(data->javaVideoGrabber,javaCloseGrabber);
} else {
ofLogError("ofxAndroidVideoGrabber") << "close(): couldn't get OFAndroidVideoGrabber close grabber method";
}
data->bGrabberInited = false;
}
ofTexture * ofxAndroidVideoGrabber::getTexturePtr(){
if(supportsTextureRendering()) return &data->texture;
else return nullptr;
}
bool ofxAndroidVideoGrabber::supportsTextureRendering(){
static bool supportsTexture = false;
static bool supportChecked = false;
if(!supportChecked){
JNIEnv *env = ofGetJNIEnv();
if(!env){
ofLogError("ofxAndroidVideoGrabber") << "init grabber failed : couldn't get environment using GetEnv()";
return false;
}
jmethodID supportsTextureMethod = env->GetStaticMethodID(getJavaClass(),"supportsTextureRendering","()Z");
supportsTexture = env->CallStaticBooleanMethod(getJavaClass(),supportsTextureMethod);
supportChecked = true;
}
return supportsTexture;
}
bool ofxAndroidVideoGrabber::setup(int w, int h){
if(data->bGrabberInited){
ofLogError("ofxAndroidVideoGrabber") << "initGrabber(): camera already initialized";
return false;
}
ofxAndroidRequestPermission(OFX_ANDROID_PERMISSION_CAMERA);
if(!ofxAndroidCheckPermission(OFX_ANDROID_PERMISSION_CAMERA)) return false;
data->width = w;
data->height = h;
data->frontBuffer.allocate(w, h, data->internalPixelFormat);
data->backBuffer.allocate(w, h, data->internalPixelFormat);
ofLogNotice() << "initializing camera with external texture";
data->loadTexture();
bool bInit = initCamera();
if(!bInit) return false;
ofLogVerbose("ofxAndroidVideoGrabber") << "initGrabber(): camera initialized correctly";
data->appPaused = false;
return true;
}
bool ofxAndroidVideoGrabber::initCamera(){
if(!ofxAndroidCheckPermission(OFX_ANDROID_PERMISSION_CAMERA)) return false;
JNIEnv *env = ofGetJNIEnv();
if(!env) return false;
jclass javaClass = getJavaClass();
jmethodID javaInitGrabber = env->GetMethodID(javaClass,"initGrabber","(IIII)V");
if(data->javaVideoGrabber && javaInitGrabber){
env->CallVoidMethod(data->javaVideoGrabber,javaInitGrabber,data->width,data->height,data->attemptFramerate,data->texture.texData.textureID);
} else {
ofLogError("ofxAndroidVideoGrabber") << "initGrabber(): couldn't get OFAndroidVideoGrabber init grabber method";
return false;
}
//ofLogVerbose("ofxAndroidVideoGrabber") << "initGrabber(): new frame callback size: " << (int) width << "x" << (int) height;
data->bGrabberInited = true;
return true;
};
bool ofxAndroidVideoGrabber::isInitialized() const{
return data->bGrabberInited;
}
void ofxAndroidVideoGrabber::videoSettings(){
}
void ofxAndroidVideoGrabber::setUsePixels(bool usePixels){
data->bUsePixels = usePixels;
}
ofPixels& ofxAndroidVideoGrabber::getPixels(){
if(!data->bUsePixels){
ofLogNotice()<<"Calling getPixels will not return frame data when setUsePixels(false) has been set";
}
return data->frontBuffer;
}
const ofPixels& ofxAndroidVideoGrabber::getPixels() const {
return const_cast<ofxAndroidVideoGrabber*>(this)->getPixels();
}
void ofxAndroidVideoGrabber::setVerbose(bool bTalkToMe){
}
int ofxAndroidVideoGrabber::getCameraFacing(int facing)const{
JNIEnv *env = ofGetJNIEnv();
if(!env) return -1;
jclass javaClass = getJavaClass();
jmethodID javagetCameraFacing = env->GetMethodID(javaClass,"getCameraFacing","(I)I");
if(data->javaVideoGrabber && javagetCameraFacing){
return env->CallIntMethod(data->javaVideoGrabber,javagetCameraFacing,facing);
} else {
ofLogError("ofxAndroidVideoGrabber") << "getCameraFacing(): couldn't get OFAndroidVideoGrabber getCameraFacing method";
return -1;
}
}
int ofxAndroidVideoGrabber::getBackCamera()const{
return getCameraFacing(0);
}
int ofxAndroidVideoGrabber::getFrontCamera()const{
return getCameraFacing(1);
}
int ofxAndroidVideoGrabber::getNumCameras() const{
JNIEnv *env = ofGetJNIEnv();
if(!env) return 0;
jclass javaClass = getJavaClass();
jmethodID javagetNumCameras= env->GetMethodID(javaClass,"getNumCameras","()I");
if(data->javaVideoGrabber && javagetNumCameras){
return env->CallIntMethod(data->javaVideoGrabber,javagetNumCameras);
} else {
ofLogError("ofxAndroidVideoGrabber") << "getNumCameras(): couldn't get OFAndroidVideoGrabber getNumCameras method";
return 0;
}
}
int ofxAndroidVideoGrabber::getCameraOrientation(int device)const{
JNIEnv *env = ofGetJNIEnv();
if(!env) return 0;
jclass javaClass = getJavaClass();
jmethodID javagetCameraOrientation= env->GetMethodID(javaClass,"getCameraOrientation","(I)I");
if(data->javaVideoGrabber && javagetCameraOrientation){
return env->CallIntMethod(data->javaVideoGrabber,javagetCameraOrientation, device);
} else {
ofLogError("ofxAndroidVideoGrabber") << "getCameraOrientation(): couldn't get OFAndroidVideoGrabber getCameraOrientation method";
return 0;
}
}
int ofxAndroidVideoGrabber::getFacingOfCamera(int device)const{
JNIEnv *env = ofGetJNIEnv();
if(!env) return 0;
jclass javaClass = getJavaClass();
jmethodID javagetFacingOfCamera= env->GetMethodID(javaClass,"getFacingOfCamera","(I)I");
if(data->javaVideoGrabber && javagetFacingOfCamera){
return env->CallIntMethod(data->javaVideoGrabber,javagetFacingOfCamera, device);
} else {
ofLogError("ofxAndroidVideoGrabber") << "getFacingOfCamera(): couldn't get OFAndroidVideoGrabber getFacingOfCamera method";
return 0;
}
}
void ofxAndroidVideoGrabber::setDeviceID(int _deviceID){
bool wasInited = data->bGrabberInited;
int w = this->getWidth();
int h = this->getHeight();
if(data->bGrabberInited){
close();
}
JNIEnv *env = ofGetJNIEnv();
if(!env) return;
jclass javaClass = getJavaClass();
jmethodID javasetDeviceID = env->GetMethodID(javaClass,"setDeviceID","(I)V");
if(data->javaVideoGrabber && javasetDeviceID){
env->CallVoidMethod(data->javaVideoGrabber,javasetDeviceID,_deviceID);
} else {
ofLogError("ofxAndroidVideoGrabber") << "setDeviceID(): couldn't get OFAndroidVideoGrabber setDeviceID method";
return;
}
if(wasInited){
setup(w, h);
}
}
bool ofxAndroidVideoGrabber::setAutoFocus(bool autofocus){
JNIEnv *env = ofGetJNIEnv();
if(!env) return false;
jclass javaClass = getJavaClass();
jmethodID javasetAutoFocus = env->GetMethodID(javaClass,"setAutoFocus","(Z)Z");
if(data->javaVideoGrabber && javasetAutoFocus){
return env->CallBooleanMethod(data->javaVideoGrabber,javasetAutoFocus,autofocus);
}else{
ofLogError("ofxAndroidVideoGrabber") << "setAutoFocus(): couldn't get OFAndroidVideoGrabber setAutoFocus method";
return false;
}
}
void ofxAndroidVideoGrabber::setDesiredFrameRate(int framerate){
data->attemptFramerate = framerate;
}
float ofxAndroidVideoGrabber::getHeight() const{
return data->height;
}
float ofxAndroidVideoGrabber::getWidth() const{
return data->width;
}
bool ofxAndroidVideoGrabber::setPixelFormat(ofPixelFormat pixelFormat){
data->internalPixelFormat = pixelFormat;
return true;
}
ofPixelFormat ofxAndroidVideoGrabber::getPixelFormat() const{
return data->internalPixelFormat;
}
// Conversion from yuv nv21 to rgb24 adapted from
// videonet conversion from YUV420 to RGB24
// http://www.codeguru.com/cpp/g-m/multimedia/video/article.php/c7621
static long int crv_tab[256];
static long int cbu_tab[256];
static long int cgu_tab[256];
static long int cgv_tab[256];
static long int tab_76309[256];
static unsigned char clp[1024]; //for clip in CCIR601
//
//Initialize conversion table for YUV420 to RGB
//
void InitConvertTable()
{
static bool inited = false;
if(inited) return;
long int crv,cbu,cgu,cgv;
int i,ind;
crv = 104597; cbu = 132201; /* fra matrise i global.h */
cgu = 25675; cgv = 53279;
for (i = 0; i < 256; i++) {
crv_tab[i] = (i-128) * crv;
cbu_tab[i] = (i-128) * cbu;
cgu_tab[i] = (i-128) * cgu;
cgv_tab[i] = (i-128) * cgv;
tab_76309[i] = 76309*(i-16);
}
for (i=0; i<384; i++)
clp[i] =0;
ind=384;
for (i=0;i<256; i++)
clp[ind++]=i;
ind=640;
for (i=0;i<384;i++)
clp[ind++]=255;
inited = true;
}
void ConvertYUV2RGB(unsigned char *src0,unsigned char *src1,unsigned char *dst_ori,
int width,int height)
{
int y1,y2,u,v;
unsigned char *py1,*py2;
int i,j, c1, c2, c3, c4;
unsigned char *d1, *d2;
int width3 = 3*width;
py1=src0;
py2=py1+width;
d1=dst_ori;
d2=d1+width3;
for (j = 0; j < height; j += 2) {
for (i = 0; i < width; i += 2) {
v = *src1++;
u = *src1++;
c1 = crv_tab[v];
c2 = cgu_tab[u];
c3 = cgv_tab[v];
c4 = cbu_tab[u];
//up-left
y1 = tab_76309[*py1++];
*d1++ = clp[384+((y1 + c1)>>16)];
*d1++ = clp[384+((y1 - c2 - c3)>>16)];
*d1++ = clp[384+((y1 + c4)>>16)];
//down-left
y2 = tab_76309[*py2++];
*d2++ = clp[384+((y2 + c1)>>16)];
*d2++ = clp[384+((y2 - c2 - c3)>>16)];
*d2++ = clp[384+((y2 + c4)>>16)];
//up-right
y1 = tab_76309[*py1++];
*d1++ = clp[384+((y1 + c1)>>16)];
*d1++ = clp[384+((y1 - c2 - c3)>>16)];
*d1++ = clp[384+((y1 + c4)>>16)];
//down-right
y2 = tab_76309[*py2++];
*d2++ = clp[384+((y2 + c1)>>16)];
*d2++ = clp[384+((y2 - c2 - c3)>>16)];
*d2++ = clp[384+((y2 + c4)>>16)];
}
d1 += width3;
d2 += width3;
py1+= width;
py2+= width;
}
}
/**
* Converts semi-planar YUV420 as generated for camera preview into RGB565
* format for use as an OpenGL ES texture. It assumes that both the input
* and output data are contiguous and start at zero.
*
* @param yuvs the array of YUV420 semi-planar data
* @param rgbs an array into which the RGB565 data will be written
* @param width the number of pixels horizontally
* @param height the number of pixels vertically
*/
//we tackle the conversion two pixels at a time for greater speed
void ConvertYUV2toRGB565(unsigned char* yuvs, unsigned char* rgbs, int width, int height) {
//the end of the luminance data
int lumEnd = width * height;
//points to the next luminance value pair
int lumPtr = 0;
//points to the next chromiance value pair
int chrPtr = lumEnd;
//points to the next byte output pair of RGB565 value
int outPtr = 0;
//the end of the current luminance scanline
int lineEnd = width;
int R, G, B;
int Y1;
int Y2;
int Cr;
int Cb;
while (true) {
//skip back to the start of the chromiance values when necessary
if (lumPtr == lineEnd) {
if (lumPtr == lumEnd) break; //we've reached the end
//division here is a bit expensive, but's only done once per scanline
chrPtr = lumEnd + ((lumPtr >> 1) / width) * width;
lineEnd += width;
}
//read the luminance and chromiance values
Y1 = yuvs[lumPtr++] & 0xff;
Y2 = yuvs[lumPtr++] & 0xff;
Cr = (yuvs[chrPtr++] & 0xff) - 128;
Cb = (yuvs[chrPtr++] & 0xff) - 128;
//generate first RGB components
B = Y1 + ((454 * Cb) >> 8);
if(B < 0) B = 0; else if(B > 255) B = 255;
G = Y1 - ((88 * Cb + 183 * Cr) >> 8);
if(G < 0) G = 0; else if(G > 255) G = 255;
R = Y1 + ((359 * Cr) >> 8);
if(R < 0) R = 0; else if(R > 255) R = 255;
//NOTE: this assume little-endian encoding
rgbs[outPtr++] = (unsigned char) (((G & 0x3c) << 3) | (B >> 3));
rgbs[outPtr++] = (unsigned char) ((R & 0xf8) | (G >> 5));
//generate second RGB components
B = Y2 + ((454 * Cb) >> 8);
if(B < 0) B = 0; else if(B > 255) B = 255;
G = Y2 - ((88 * Cb + 183 * Cr) >> 8);
if(G < 0) G = 0; else if(G > 255) G = 255;
R = Y2 + ((359 * Cr) >> 8);
if(R < 0) R = 0; else if(R > 255) R = 255;
//NOTE: this assume little-endian encoding
rgbs[outPtr++] = (unsigned char) (((G & 0x3c) << 3) | (B >> 3));
rgbs[outPtr++] = (unsigned char) ((R & 0xf8) | (G >> 5));
}
}
void ConvertYUV2toRGB565_2(unsigned char *src0,unsigned char *src1,unsigned char *dst_ori,
int width,int height)
{
int y1,y2,u,v;
unsigned char *py1,*py2;
int i,j, c1, c2, c3, c4;
unsigned char *d1, *d2;
int R,G,B;
int width2 = 2*width;
py1=src0;
py2=py1+width;
d1=dst_ori;
d2=d1+width2;
for (j = 0; j < height; j += 2) {
for (i = 0; i < width; i += 2) {
v = *src1++;
u = *src1++;
c1 = crv_tab[v];
c2 = cgu_tab[u];
c3 = cgv_tab[v];
c4 = cbu_tab[u];
//up-left
y1 = tab_76309[*py1++];
R = clp[384+((y1 + c1)>>16)];
G = clp[384+((y1 - c2 - c3)>>16)];
B = clp[384+((y1 + c4)>>16)];
*d1++ = (unsigned char) (((G & 0x3c) << 3) | (B >> 3));
*d1++ = (unsigned char) ((R & 0xf8) | (G >> 5));
//down-left
y2 = tab_76309[*py2++];
B = clp[384+((y2 + c1)>>16)];
G = clp[384+((y2 - c2 - c3)>>16)];
R = clp[384+((y2 + c4)>>16)];
*d2++ = (unsigned char) (((G & 0x3c) << 3) | (B >> 3));
*d2++ = (unsigned char) ((R & 0xf8) | (G >> 5));
//up-right
y1 = tab_76309[*py1++];
R = clp[384+((y1 + c1)>>16)];
G = clp[384+((y1 - c2 - c3)>>16)];
B = clp[384+((y1 + c4)>>16)];
*d1++ = (unsigned char) (((G & 0x3c) << 3) | (B >> 3));
*d1++ = (unsigned char) ((R & 0xf8) | (G >> 5));
//down-right
y2 = tab_76309[*py2++];
B = clp[384+((y2 + c1)>>16)];
G = clp[384+((y2 - c2 - c3)>>16)];
R = clp[384+((y2 + c4)>>16)];
*d2++ = (unsigned char) (((G & 0x3c) << 3) | (B >> 3));
*d2++ = (unsigned char) ((R & 0xf8) | (G >> 5));
}
d1 += width2;
d2 += width2;
py1+= width;
py2+= width;
}
}
extern "C"{
jint
Java_cc_openframeworks_OFAndroidVideoGrabber_newFrame(JNIEnv* env, jobject thiz, jbyteArray array, jint width, jint height, jint cameraId){
auto data = instances()[cameraId].lock();
if(!data) return 1;
if(data->bUsePixels) {
jboolean isCopy;
auto currentFrame = (unsigned char *) env->GetByteArrayElements(array, &isCopy);
//ofLog()<<"Is copy: "<<(isCopy?true:false);
if (!currentFrame) return 1;
ofPixels &pixels = data->backBuffer;
bool needsResize = false;
if (pixels.getWidth() != width || pixels.getHeight() != height) {
needsResize = true;
pixels = data->resizeBuffer;
if(!pixels.isAllocated() || pixels.getWidth() != width || pixels.getHeight() != height) {
pixels.allocate(width, height, data->internalPixelFormat);
}
}
if (data->internalPixelFormat == OF_PIXELS_RGB) {
ConvertYUV2RGB(currentFrame, // y component
currentFrame + (width * height), // uv components
pixels.getData(), width, height);
} else if (data->internalPixelFormat == OF_PIXELS_RGB565) {
ConvertYUV2toRGB565(currentFrame, pixels.getData(), width, height);
} else if (data->internalPixelFormat == OF_PIXELS_GRAY) {
pixels.setFromPixels(currentFrame, width, height, OF_IMAGE_GRAYSCALE);
} else if (data->internalPixelFormat == OF_PIXELS_NV21) {
pixels.setFromPixels(currentFrame, width, height, OF_PIXELS_NV21);
}
env->ReleaseByteArrayElements(array, (jbyte*)currentFrame, 0);
if (needsResize) {
pixels.resizeTo(data->backBuffer, OF_INTERPOLATE_NEAREST_NEIGHBOR);
}
data->bNewBackFrame=true;
}
data->newPixels = true;
return 0;
}
}