forked from dkbrown/android_device_samsung_epic
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSecCamera.cpp
executable file
·3244 lines (2636 loc) · 89.6 KB
/
SecCamera.cpp
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2008, The Android Open Source Project
* Copyright 2010, Samsung Electronics Co. LTD
* Copyright 2011, The CyanogenMod Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
************************************
* Filename: SecCamera.cpp
* Author: Sachin P. Kamat
* Purpose: This file interacts with the Camera and JPEG drivers.
*************************************
*/
//#define LOG_NDEBUG 0
#define LOG_TAG "SecCamera"
#include <utils/Log.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <sys/poll.h>
#include "SecCamera.h"
#include "cutils/properties.h"
using namespace android;
#define CHECK(return_value) \
if (return_value < 0) { \
ALOGE("%s::%d fail. errno: %s, m_camera_id = %d\n", \
__func__, __LINE__, strerror(errno), m_camera_id); \
return -1; \
}
#define CHECK_PTR(return_value) \
if (return_value < 0) { \
ALOGE("%s::%d fail, errno: %s, m_camera_id = %d\n", \
__func__,__LINE__, strerror(errno), m_camera_id); \
return NULL; \
}
#define ALIGN_TO_32B(x) ((((x) + (1 << 5) - 1) >> 5) << 5)
#define ALIGN_TO_128B(x) ((((x) + (1 << 7) - 1) >> 7) << 7)
#define ALIGN_TO_8KB(x) ((((x) + (1 << 13) - 1) >> 13) << 13)
namespace android {
// ======================================================================
// Camera controls
static struct timeval time_start;
static struct timeval time_stop;
unsigned long measure_time(struct timeval *start, struct timeval *stop)
{
unsigned long sec, usec, time;
sec = stop->tv_sec - start->tv_sec;
if (stop->tv_usec >= start->tv_usec) {
usec = stop->tv_usec - start->tv_usec;
} else {
usec = stop->tv_usec + 1000000 - start->tv_usec;
sec--;
}
time = (sec * 1000000) + usec;
return time;
}
static int get_pixel_depth(unsigned int fmt)
{
int depth = 0;
switch (fmt) {
case V4L2_PIX_FMT_NV12:
depth = 12;
break;
case V4L2_PIX_FMT_NV12T:
depth = 12;
break;
case V4L2_PIX_FMT_NV21:
depth = 12;
break;
case V4L2_PIX_FMT_YUV420:
depth = 12;
break;
case V4L2_PIX_FMT_RGB565:
case V4L2_PIX_FMT_YUYV:
case V4L2_PIX_FMT_YVYU:
case V4L2_PIX_FMT_UYVY:
case V4L2_PIX_FMT_VYUY:
case V4L2_PIX_FMT_NV16:
case V4L2_PIX_FMT_NV61:
case V4L2_PIX_FMT_YUV422P:
depth = 16;
break;
case V4L2_PIX_FMT_RGB32:
depth = 32;
break;
}
return depth;
}
#define ALIGN_W(x) (((x) + 0x7F) & (~0x7F)) // Set as multiple of 128
#define ALIGN_H(x) (((x) + 0x1F) & (~0x1F)) // Set as multiple of 32
#define ALIGN_BUF(x) (((x) + 0x1FFF)& (~0x1FFF)) // Set as multiple of 8K
static int fimc_poll(struct pollfd *events)
{
int ret;
/* 10 second delay is because sensor can take a long time
* to do auto focus and capture in dark settings
*/
ret = poll(events, 1, 10000);
if (ret < 0) {
ALOGE("ERR(%s):poll error\n", __func__);
return ret;
}
if (ret == 0) {
ALOGE("ERR(%s):No data in 10 secs..\n", __func__);
return ret;
}
return ret;
}
int SecCamera::previewPoll(bool preview)
{
int ret;
if (preview) {
#ifdef ENABLE_ESD_PREVIEW_CHECK
int status = 0;
if (!(++m_esd_check_count % 60)) {
status = getCameraSensorESDStatus();
m_esd_check_count = 0;
if (status) {
ALOGE("ERR(%s) ESD status(%d)", __func__, status);
return status;
}
}
#endif
ret = poll(&m_events_c, 1, 1000);
} else {
ret = poll(&m_events_c2, 1, 1000);
}
if (ret < 0) {
ALOGE("ERR(%s):poll error\n", __func__);
return ret;
}
if (ret == 0) {
ALOGE("ERR(%s):No data in 1 secs.. Camera Device Reset \n", __func__);
return ret;
}
return ret;
}
static int fimc_v4l2_querycap(int fp)
{
struct v4l2_capability cap;
int ret = 0;
ret = ioctl(fp, VIDIOC_QUERYCAP, &cap);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_QUERYCAP failed\n", __func__);
return -1;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
ALOGE("ERR(%s):no capture devices\n", __func__);
return -1;
}
return ret;
}
static const __u8* fimc_v4l2_enuminput(int fp, int index)
{
static struct v4l2_input input;
input.index = index;
if (ioctl(fp, VIDIOC_ENUMINPUT, &input) != 0) {
ALOGE("ERR(%s):No matching index found\n", __func__);
return NULL;
}
ALOGI("Name of input channel[%d] is %s\n", input.index, input.name);
return input.name;
}
static int fimc_v4l2_s_input(int fp, int index)
{
struct v4l2_input input;
int ret;
input.index = index;
ret = ioctl(fp, VIDIOC_S_INPUT, &input);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_S_INPUT failed\n", __func__);
return ret;
}
return ret;
}
static int fimc_v4l2_s_fmt(int fp, int width, int height, unsigned int fmt, int flag_capture)
{
struct v4l2_format v4l2_fmt;
struct v4l2_pix_format pixfmt;
int ret;
v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
memset(&pixfmt, 0, sizeof(pixfmt));
pixfmt.width = width;
pixfmt.height = height;
pixfmt.pixelformat = fmt;
pixfmt.sizeimage = (width * height * get_pixel_depth(fmt)) / 8;
pixfmt.field = V4L2_FIELD_NONE;
v4l2_fmt.fmt.pix = pixfmt;
/* Set up for capture */
ret = ioctl(fp, VIDIOC_S_FMT, &v4l2_fmt);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_S_FMT failed\n", __func__);
return -1;
}
return 0;
}
static int fimc_v4l2_s_fmt_cap(int fp, int width, int height, unsigned int fmt)
{
struct v4l2_format v4l2_fmt;
struct v4l2_pix_format pixfmt;
int ret;
memset(&pixfmt, 0, sizeof(pixfmt));
v4l2_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
pixfmt.width = width;
pixfmt.height = height;
pixfmt.pixelformat = fmt;
if (fmt == V4L2_PIX_FMT_JPEG) {
pixfmt.colorspace = V4L2_COLORSPACE_JPEG;
}
pixfmt.sizeimage = (width * height * get_pixel_depth(fmt)) / 8;
v4l2_fmt.fmt.pix = pixfmt;
//ALOGE("ori_w %d, ori_h %d, w %d, h %d\n", width, height, v4l2_fmt.fmt.pix.width, v4l2_fmt.fmt.pix.height);
/* Set up for capture */
ret = ioctl(fp, VIDIOC_S_FMT, &v4l2_fmt);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_S_FMT failed\n", __func__);
return ret;
}
return ret;
}
static int fimc_v4l2_enum_fmt(int fp, unsigned int fmt)
{
struct v4l2_fmtdesc fmtdesc;
int found = 0;
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmtdesc.index = 0;
while (ioctl(fp, VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
if (fmtdesc.pixelformat == fmt) {
ALOGV("passed fmt = %#x found pixel format[%d]: %s\n", fmt, fmtdesc.index, fmtdesc.description);
found = 1;
break;
}
fmtdesc.index++;
}
if (!found) {
ALOGE("unsupported pixel format\n");
return -1;
}
return 0;
}
static int fimc_v4l2_reqbufs(int fp, enum v4l2_buf_type type, int nr_bufs)
{
struct v4l2_requestbuffers req;
int ret;
req.count = nr_bufs;
req.type = type;
req.memory = V4L2_MEMORY_MMAP;
ret = ioctl(fp, VIDIOC_REQBUFS, &req);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_REQBUFS failed\n", __func__);
return -1;
}
return req.count;
}
static int fimc_v4l2_querybuf(int fp, struct fimc_buffer *buffer, enum v4l2_buf_type type)
{
struct v4l2_buffer v4l2_buf;
int ret;
ALOGI("%s :", __func__);
v4l2_buf.type = type;
v4l2_buf.memory = V4L2_MEMORY_MMAP;
v4l2_buf.index = 0;
ret = ioctl(fp , VIDIOC_QUERYBUF, &v4l2_buf);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_QUERYBUF failed\n", __func__);
return -1;
}
buffer->length = v4l2_buf.length;
if ((buffer->start = (char *)mmap(0, v4l2_buf.length,
PROT_READ | PROT_WRITE, MAP_SHARED,
fp, v4l2_buf.m.offset)) < 0) {
ALOGE("%s %d] mmap() failed\n",__func__, __LINE__);
return -1;
}
ALOGI("%s: buffer->start = %p v4l2_buf.length = %d",
__func__, buffer->start, v4l2_buf.length);
return 0;
}
static int fimc_v4l2_streamon(int fp)
{
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
int ret;
ret = ioctl(fp, VIDIOC_STREAMON, &type);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_STREAMON failed\n", __func__);
return ret;
}
return ret;
}
static int fimc_v4l2_streamoff(int fp)
{
enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
int ret;
ALOGV("%s :", __func__);
ret = ioctl(fp, VIDIOC_STREAMOFF, &type);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_STREAMOFF failed\n", __func__);
return ret;
}
return ret;
}
static int fimc_v4l2_qbuf(int fp, int index)
{
struct v4l2_buffer v4l2_buf;
int ret;
v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v4l2_buf.memory = V4L2_MEMORY_MMAP;
v4l2_buf.index = index;
ret = ioctl(fp, VIDIOC_QBUF, &v4l2_buf);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_QBUF failed\n", __func__);
return ret;
}
return 0;
}
static int fimc_v4l2_dqbuf(int fp)
{
struct v4l2_buffer v4l2_buf;
int ret;
v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
v4l2_buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(fp, VIDIOC_DQBUF, &v4l2_buf);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_DQBUF failed, dropped frame\n", __func__);
return ret;
}
return v4l2_buf.index;
}
static int fimc_v4l2_g_ctrl(int fp, unsigned int id)
{
struct v4l2_control ctrl;
int ret;
ctrl.id = id;
ret = ioctl(fp, VIDIOC_G_CTRL, &ctrl);
if (ret < 0) {
ALOGE("ERR(%s): VIDIOC_G_CTRL(id = 0x%x (%d)) failed, ret = %d\n",
__func__, id, id-V4L2_CID_PRIVATE_BASE, ret);
return ret;
}
return ctrl.value;
}
static int fimc_v4l2_s_ctrl(int fp, unsigned int id, unsigned int value)
{
struct v4l2_control ctrl;
int ret;
ctrl.id = id;
ctrl.value = value;
ret = ioctl(fp, VIDIOC_S_CTRL, &ctrl);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_S_CTRL(id = %#x (%d), value = %d) failed ret = %d\n",
__func__, id, id-V4L2_CID_PRIVATE_BASE, value, ret);
return ret;
}
return ctrl.value;
}
static int fimc_v4l2_s_ext_ctrl(int fp, unsigned int id, void *value)
{
struct v4l2_ext_controls ctrls;
struct v4l2_ext_control ctrl;
int ret;
ctrl.id = id;
ctrl.string = (char *) value;
ctrls.ctrl_class = V4L2_CTRL_CLASS_CAMERA;
ctrls.count = 1;
ctrls.controls = &ctrl;
ret = ioctl(fp, VIDIOC_S_EXT_CTRLS, &ctrls);
if (ret < 0)
ALOGE("ERR(%s):VIDIOC_S_EXT_CTRLS failed\n", __func__);
return ret;
}
static int fimc_v4l2_g_parm(int fp, struct v4l2_streamparm *streamparm)
{
int ret;
streamparm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(fp, VIDIOC_G_PARM, streamparm);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_G_PARM failed\n", __func__);
return -1;
}
ALOGV("%s : timeperframe: numerator %d, denominator %d\n", __func__,
streamparm->parm.capture.timeperframe.numerator,
streamparm->parm.capture.timeperframe.denominator);
return 0;
}
static int fimc_v4l2_s_parm(int fp, struct v4l2_streamparm *streamparm)
{
int ret;
streamparm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(fp, VIDIOC_S_PARM, streamparm);
if (ret < 0) {
ALOGE("ERR(%s):VIDIOC_S_PARM failed\n", __func__);
return ret;
}
return 0;
}
// ======================================================================
// Constructor & Destructor
SecCamera::SecCamera() :
m_flag_init(0),
m_camera_id(CAMERA_ID_BACK),
m_cam_fd(-1),
m_cam_fd2(-1),
m_preview_v4lformat(V4L2_PIX_FMT_NV21),
m_preview_width (0),
m_preview_height (0),
m_preview_max_width (MAX_BACK_CAMERA_PREVIEW_WIDTH),
m_preview_max_height (MAX_BACK_CAMERA_PREVIEW_HEIGHT),
m_snapshot_v4lformat(-1),
m_snapshot_width (0),
m_snapshot_height (0),
m_snapshot_max_width (MAX_BACK_CAMERA_SNAPSHOT_WIDTH),
m_snapshot_max_height (MAX_BACK_CAMERA_SNAPSHOT_HEIGHT),
m_angle(-1),
m_anti_banding(-1),
m_wdr(-1),
m_anti_shake(-1),
m_zoom_level(-1),
m_object_tracking(-1),
m_smart_auto(-1),
m_beauty_shot(-1),
m_vintage_mode(-1),
m_face_detect(-1),
m_gps_enabled(false),
m_gps_latitude(-1),
m_gps_longitude(-1),
m_gps_altitude(-1),
m_gps_timestamp(-1),
m_vtmode(0),
m_sensor_mode(-1),
m_shot_mode(-1),
m_exif_orientation(-1),
m_blur_level(-1),
m_chk_dataline(-1),
m_video_gamma(-1),
m_slow_ae(-1),
m_camera_af_flag(-1),
m_flag_camera_start(0),
m_jpeg_thumbnail_width (0),
m_jpeg_thumbnail_height(0),
m_jpeg_quality(100)
#ifdef ENABLE_ESD_PREVIEW_CHECK
,
m_esd_check_count(0)
#endif // ENABLE_ESD_PREVIEW_CHECK
{
m_params = (struct sec_cam_parm*)&m_streamparm.parm.raw_data;
struct v4l2_captureparm capture;
m_params->capture.timeperframe.numerator = 1;
m_params->capture.timeperframe.denominator = 0;
m_params->contrast = -1;
m_params->effects = -1;
m_params->brightness = -1;
m_params->flash_mode = -1;
m_params->focus_mode = -1;
m_params->iso = -1;
m_params->metering = -1;
m_params->saturation = -1;
m_params->scene_mode = -1;
m_params->sharpness = -1;
m_params->white_balance = -1;
memset(&m_capture_buf, 0, sizeof(m_capture_buf));
ALOGV("%s :", __func__);
}
SecCamera::~SecCamera()
{
ALOGV("%s :", __func__);
}
int SecCamera::initCamera(int index)
{
ALOGV("%s :", __func__);
int ret = 0;
if (!m_flag_init) {
/* Arun C
* Reset the lense position only during camera starts; don't do
* reset between shot to shot
*/
m_camera_af_flag = -1;
m_cam_fd = open(CAMERA_DEV_NAME, O_RDWR);
if (m_cam_fd < 0) {
ALOGE("ERR(%s):Cannot open %s (error : %s)\n", __func__, CAMERA_DEV_NAME, strerror(errno));
return -1;
}
ALOGV("%s: open(%s) --> m_cam_fd %d", __FUNCTION__, CAMERA_DEV_NAME, m_cam_fd);
ALOGE("initCamera: m_cam_fd(%d), m_jpeg_fd(%d)", m_cam_fd, m_jpeg_fd);
ret = fimc_v4l2_querycap(m_cam_fd);
CHECK(ret);
if (!fimc_v4l2_enuminput(m_cam_fd, index))
return -1;
ret = fimc_v4l2_s_input(m_cam_fd, index);
CHECK(ret);
m_cam_fd2 = open(CAMERA_DEV_NAME2, O_RDWR);
ALOGV("%s: open(%s) --> m_cam_fd2 = %d", __FUNCTION__, CAMERA_DEV_NAME2, m_cam_fd2);
if (m_cam_fd2 < 0) {
ALOGE("ERR(%s):Cannot open %s (error : %s)\n", __func__, CAMERA_DEV_NAME2, strerror(errno));
return -1;
}
ALOGE("initCamera: m_cam_fd2(%d)", m_cam_fd2);
ret = fimc_v4l2_querycap(m_cam_fd2);
CHECK(ret);
if (!fimc_v4l2_enuminput(m_cam_fd2, index))
return -1;
ret = fimc_v4l2_s_input(m_cam_fd2, index);
CHECK(ret);
m_camera_id = index;
switch (m_camera_id) {
case CAMERA_ID_FRONT:
m_preview_max_width = MAX_FRONT_CAMERA_PREVIEW_WIDTH;
m_preview_max_height = MAX_FRONT_CAMERA_PREVIEW_HEIGHT;
m_snapshot_max_width = MAX_FRONT_CAMERA_SNAPSHOT_WIDTH;
m_snapshot_max_height = MAX_FRONT_CAMERA_SNAPSHOT_HEIGHT;
break;
case CAMERA_ID_BACK:
m_preview_max_width = MAX_BACK_CAMERA_PREVIEW_WIDTH;
m_preview_max_height = MAX_BACK_CAMERA_PREVIEW_HEIGHT;
m_snapshot_max_width = MAX_BACK_CAMERA_SNAPSHOT_WIDTH;
m_snapshot_max_height = MAX_BACK_CAMERA_SNAPSHOT_HEIGHT;
break;
}
setExifFixedAttribute();
m_flag_init = 1;
ALOGI("%s : initialized", __FUNCTION__);
}
return 0;
}
void SecCamera::resetCamera()
{
ALOGV("%s :", __func__);
DeinitCamera();
initCamera(m_camera_id);
}
void SecCamera::DeinitCamera()
{
ALOGV("%s :", __func__);
if (m_flag_init) {
stopRecord();
/* close m_cam_fd after stopRecord() because stopRecord()
* uses m_cam_fd to change frame rate
*/
ALOGI("DeinitCamera: m_cam_fd(%d)", m_cam_fd);
if (m_cam_fd > -1) {
close(m_cam_fd);
m_cam_fd = -1;
}
ALOGI("DeinitCamera: m_cam_fd2(%d)", m_cam_fd2);
if (m_cam_fd2 > -1) {
close(m_cam_fd2);
m_cam_fd2 = -1;
}
m_flag_init = 0;
}
else ALOGI("%s : already deinitialized", __FUNCTION__);
}
int SecCamera::getCameraFd(void)
{
return m_cam_fd;
}
// ======================================================================
// Preview
int SecCamera::startPreview(void)
{
v4l2_streamparm streamparm;
struct sec_cam_parm *parms;
parms = (struct sec_cam_parm*)&streamparm.parm.raw_data;
ALOGV("%s :", __func__);
// aleady started
if (m_flag_camera_start > 0) {
ALOGE("ERR(%s):Preview was already started\n", __func__);
return 0;
}
if (m_cam_fd <= 0) {
ALOGE("ERR(%s):Camera was closed\n", __func__);
return -1;
}
memset(&m_events_c, 0, sizeof(m_events_c));
m_events_c.fd = m_cam_fd;
m_events_c.events = POLLIN | POLLERR;
/* enum_fmt, s_fmt sample */
int ret = fimc_v4l2_enum_fmt(m_cam_fd,m_preview_v4lformat);
CHECK(ret);
if (m_camera_id == CAMERA_ID_BACK)
ret = fimc_v4l2_s_fmt(m_cam_fd, m_preview_width,m_preview_height,m_preview_v4lformat, 0);
else
ret = fimc_v4l2_s_fmt(m_cam_fd, m_preview_height,m_preview_width,m_preview_v4lformat, 0);
CHECK(ret);
ret = fimc_v4l2_reqbufs(m_cam_fd, V4L2_BUF_TYPE_VIDEO_CAPTURE, MAX_BUFFERS);
CHECK(ret);
ALOGV("%s : m_preview_width: %d m_preview_height: %d m_angle: %d\n",
__func__, m_preview_width, m_preview_height, m_angle);
ret = fimc_v4l2_s_ctrl(m_cam_fd,
V4L2_CID_CAMERA_CHECK_DATALINE, m_chk_dataline);
CHECK(ret);
if (m_camera_id == CAMERA_ID_FRONT) {
/* VT mode setting */
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_VT_MODE, m_vtmode);
CHECK(ret);
}
/* start with all buffers in queue */
for (int i = 0; i < MAX_BUFFERS; i++) {
ret = fimc_v4l2_qbuf(m_cam_fd, i);
CHECK(ret);
}
if (m_camera_id == CAMERA_ID_BACK) {
// Init some parameters required for CE147
// Force antibanding for back camera - only value supported
m_anti_banding = ANTI_BANDING_50HZ;
// It doesn't hurt to keep these on as the kernel camera driver only
// enables it when recording HD video. Turning it on before recording
// doesn't work because it needs to be set before the preview is started.
m_video_gamma = GAMMA_ON;
m_slow_ae = SLOW_AE_ON;
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_ANTI_BANDING, m_anti_banding);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_ISO, m_params->iso);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_BRIGHTNESS, m_params->brightness);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_FRAME_RATE, m_params->capture.timeperframe.denominator);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_METERING, m_params->metering);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_SET_GAMMA, m_video_gamma);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_SET_SLOW_AE, m_slow_ae);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_EFFECT, m_params->effects);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_WHITE_BALANCE, m_params->white_balance);
CHECK(ret);
}
ret = fimc_v4l2_streamon(m_cam_fd);
CHECK(ret);
if (m_camera_id == CAMERA_ID_BACK) {
// More parameters for CE147
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_FOCUS_MODE, m_params->focus_mode);
CHECK(ret);
m_face_detect = 0;
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_FACE_DETECTION, m_face_detect);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_SHARPNESS, m_params->sharpness);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_SATURATION, m_params->saturation);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_CONTRAST, m_params->contrast);
CHECK(ret);
// TODO
m_beauty_shot = 0;
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_BEAUTY_SHOT, m_beauty_shot);
CHECK(ret);
m_zoom_level = 0;
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_ZOOM, m_zoom_level);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_BATCH_REFLECTION, 1);
CHECK(ret);
}
m_flag_camera_start = 1;
ret = fimc_v4l2_s_parm(m_cam_fd, &m_streamparm);
CHECK(ret);
if (m_camera_id == CAMERA_ID_FRONT) {
/* Blur setting */
ALOGV("m_blur_level = %d", m_blur_level);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_VGA_BLUR,
m_blur_level);
CHECK(ret);
}
#ifdef HAVE_FLASH
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_FLASH_MODE, m_params->flash_mode);
CHECK(ret);
#endif
// It is a delay for a new frame, not to show the previous bigger ugly picture frame.
ret = fimc_poll(&m_events_c);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_RETURN_FOCUS, 0);
CHECK(ret);
ALOGV("%s: got the first frame of the preview\n", __func__);
return 0;
}
int SecCamera::stopPreview(void)
{
int ret;
ALOGV("%s :", __func__);
if (m_flag_camera_start == 0) {
ALOGW("%s: doing nothing because m_flag_camera_start is zero", __func__);
return 0;
}
if (m_params->flash_mode == FLASH_MODE_TORCH)
setFlashMode(FLASH_MODE_OFF);
if (m_cam_fd <= 0) {
ALOGE("ERR(%s):Camera was closed\n", __func__);
return -1;
}
ret = fimc_v4l2_streamoff(m_cam_fd);
CHECK(ret);
m_flag_camera_start = 0;
return ret;
}
//Recording
int SecCamera::startRecord(void)
{
int ret, i;
ALOGV("%s :", __func__);
// aleady started
if (m_flag_record_start > 0) {
ALOGE("ERR(%s):Preview was already started\n", __func__);
return 0;
}
if (m_cam_fd2 <= 0) {
ALOGE("ERR(%s):Camera was closed\n", __func__);
return -1;
}
/* enum_fmt, s_fmt sample */
ret = fimc_v4l2_enum_fmt(m_cam_fd2, V4L2_PIX_FMT_NV12T);
CHECK(ret);
ALOGI("%s: m_recording_width = %d, m_recording_height = %d\n",
__func__, m_recording_width, m_recording_height);
if (m_camera_id == CAMERA_ID_BACK) {
// Some properties for back camera video recording
setISO(ISO_MOVIE);
setMetering(METERING_MATRIX);
setBatchReflection();
ret = fimc_v4l2_s_fmt(m_cam_fd2, m_recording_width,
m_recording_height, V4L2_PIX_FMT_NV12T, 0);
}
else {
ret = fimc_v4l2_s_fmt(m_cam_fd2, m_recording_height,
m_recording_width, V4L2_PIX_FMT_NV12T, 0);
}
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_FRAME_RATE,
m_params->capture.timeperframe.denominator);
CHECK(ret);
ret = fimc_v4l2_reqbufs(m_cam_fd2, V4L2_BUF_TYPE_VIDEO_CAPTURE, MAX_BUFFERS);
CHECK(ret);
/* start with all buffers in queue */
for (i = 0; i < MAX_BUFFERS; i++) {
ret = fimc_v4l2_qbuf(m_cam_fd2, i);
CHECK(ret);
}
ret = fimc_v4l2_streamon(m_cam_fd2);
CHECK(ret);
// Get and throw away the first frame since it is often garbled.
memset(&m_events_c2, 0, sizeof(m_events_c2));
m_events_c2.fd = m_cam_fd2;
m_events_c2.events = POLLIN | POLLERR;
ret = fimc_poll(&m_events_c2);
CHECK(ret);
// Continuous autofocus for main camera
if (m_camera_id == CAMERA_ID_BACK) {
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_CAF_START_STOP, 1);
CHECK(ret);
}
m_flag_record_start = 1;
return 0;
}
int SecCamera::stopRecord(void)
{
int ret;
ALOGV("%s :", __func__);
if (m_flag_record_start == 0) {
ALOGW("%s: doing nothing because m_flag_record_start is zero", __func__);
return 0;
}
if (m_cam_fd2 <= 0) {
ALOGE("ERR(%s):Camera was closed\n", __func__);
return -1;
}
// Continuous autofocus for main camera
if (m_camera_id == CAMERA_ID_BACK) {
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_CAF_START_STOP, 0);
CHECK(ret);
// Need to switch focus mode so that the camera can focus properly
// after using caf.
// Note: This bug is not affected when the original mode is macro
// so we can safely use that as a mode to switch to.
int orig_mode = m_params->focus_mode;
setFocusMode(FOCUS_MODE_MACRO);
setFocusMode(orig_mode);
}
m_flag_record_start = 0;
ret = fimc_v4l2_streamoff(m_cam_fd2);
CHECK(ret);
ret = fimc_v4l2_s_ctrl(m_cam_fd, V4L2_CID_CAMERA_FRAME_RATE,
FRAME_RATE_AUTO);
CHECK(ret);
// Properties for back camera non-video recording
if (m_camera_id == CAMERA_ID_BACK) {