-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathv4l_camera.c
1892 lines (1599 loc) · 50.5 KB
/
v4l_camera.c
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 (C) 2012 Renesas Electronics Corporation
*
* 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.
*/
/**
* @file v4l_camera/v4l_camera.c
* @author Guennadi Liakhovetski <[email protected]>
* @brief generic V4L2 camera HAL
*/
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#define LOG_TAG "V4L2_Camera"
#include <utils/Errors.h>
#include <utils/Log.h>
#include <utils/Timers.h>
#include <camera.h>
#include "camera_param.h"
#include "camera_preview.h"
//#define CAM_LOG LOGE
#define CAM_LOG LOGI
/* These are random numbers so far */
#define NUM_BUF_MAX 4
#define NUM_BUF_PICTURE 3
#define NUM_BUF_PREVIEW (NUM_BUF_MAX - 1)
#define NUM_BUF_RECORD NUM_BUF_MAX
#define min(a, b) ({typeof(a) __a = (a); typeof(b) __b = (b); __a > __b ? __b : __a;})
struct buf_desc {
/* TODO: add a union to support multi-plane */
struct camera_memory *cam_mem;
uint32_t offset;
};
/**
* @index - starting index of buffers in this set
* @num_buffers - number of buffers in this set. NOTE: with MMAP this is
* the number of elements in buffers[], with USERPTR we
* use only one camera_memory object with @num_buffers
* elements in it.
* @buffers - camera_memory objects (see above)
*/
struct buf_set {
struct v4l2_format fmt;
enum v4l2_memory memory;
unsigned int num_buffers;
unsigned int index;
struct buf_desc buffers[NUM_BUF_MAX];
};
typedef _Bool bool;
enum {
false = 0,
true = 1
};
struct v4l_cam {
struct camera_device acam;
struct camera_callback cb;
preview_stream_ops_t *preview_ops;
unsigned int preview_width;
unsigned int preview_height;
int fb_fmt;
struct camera_param acp;
int id;
int fd;
int32_t msg;
int32_t msg_mask;
uint32_t cap;
bool exit : 1;
bool configured : 1;
bool picture : 1;
bool preview : 1;
bool record : 1;
bool record_start : 1;
bool record_stop : 1;
bool preview_lock : 1;
struct buf_set buf_preview;
struct buf_set buf_picture;
struct buf_set buf_record;
struct buf_set *buf_record_active;
int preview_pipe[2];
pthread_t thread_preview;
pthread_t picture_prepare;
pthread_mutex_t thread_mutex;
pthread_cond_t preview_cond;
pthread_cond_t stop_cond;
uint32_t picture_compressed_4cc;
camera_frame_converter converter;
};
static struct camera_info cam_info_set[] = {
{
.facing = CAMERA_FACING_FRONT,
.orientation = 0,
}, {
.facing = CAMERA_FACING_BACK,
.orientation = 0,
},
};
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
/* *** V4L2 API *** */
static void v4l_cam_streaming_stop(struct v4l_cam *cam, struct buf_set *set)
{
enum v4l2_buf_type type = set->fmt.type;
ioctl(cam->fd, VIDIOC_STREAMOFF, &type);
}
static int v4l_cam_streaming_start(struct v4l_cam *cam, struct buf_set *set)
{
enum v4l2_buf_type type = set->fmt.type;
return ioctl(cam->fd, VIDIOC_STREAMON, &type) < 0 ? -errno : 0;
}
static int v4l_cam_format_set(struct v4l_cam *cam, struct v4l2_format *fmt)
{
/* We also want to support multiplane formats */
return ioctl(cam->fd, VIDIOC_S_FMT, fmt) < 0 ? -errno : 0;
}
static void v4l_cam_ubuf_free(struct buf_set *set)
{
struct camera_memory *cam_mem = set->buffers[0].cam_mem;
cam_mem->release(cam_mem);
}
static void v4l_cam_mbuf_free(struct buf_set *set)
{
unsigned int i;
for (i = 0; i < set->num_buffers; i++) {
struct camera_memory *cam_mem = set->buffers[i].cam_mem;
if (!cam_mem)
continue;
cam_mem->release(cam_mem);
}
}
static void v4l_cam_free_buffers(struct buf_set *set)
{
switch (set->memory) {
case V4L2_MEMORY_USERPTR:
v4l_cam_ubuf_free(set);
break;
case V4L2_MEMORY_MMAP:
v4l_cam_mbuf_free(set);
break;
default:
break;
}
}
/*
* So far buffers of different .memory types cannot be mixed, also, even if
* mixing different .type works, a REQBUFS(count = 0) would, probably, free all
* buffers anyway. So, for now a single REQBUFS should actually suffice.
* Besides, an initialisation failure will lead to close(fd), upon which kernel
* video buffers will be freed, so, in principle we only have to munmap() MMAP
* and free() USERPTR buffers. Note, that this function is also called on the
* error path, therefore it must handle incompletely initialised buffers too.
*/
static void v4l_cam_destroy_buffers(struct v4l_cam *cam)
{
struct v4l2_requestbuffers req = {
.count = 0, /* release all buffers */
.type = cam->buf_preview.fmt.type,
.memory = cam->buf_preview.memory,
};
v4l_cam_free_buffers(&cam->buf_preview);
v4l_cam_free_buffers(&cam->buf_record);
v4l_cam_free_buffers(&cam->buf_picture);
ioctl(cam->fd, VIDIOC_REQBUFS, &req);
#if 0
req.type = cam->buf_picture.type;
req.memory = cam->buf_picture.memory;
ioctl(cam->fd, VIDIOC_REQBUFS, &req);
req.type = cam->buf_record.type;
req.memory = cam->buf_record.memory;
ioctl(cam->fd, VIDIOC_REQBUFS, &req);
#endif
}
#define v4l_format_pixelformat(f) ((f)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? \
(f)->fmt.pix.pixelformat : (f)->fmt.pix_mp.pixelformat)
#define v4l_format_field(f) ((f)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? \
(f)->fmt.pix.field : (f)->fmt.pix_mp.field)
#define v4l_format_width(f) ((f)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? \
(f)->fmt.pix.width : (f)->fmt.pix_mp.width)
#define v4l_format_height(f) ((f)->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ? \
(f)->fmt.pix.height : (f)->fmt.pix_mp.height)
static int v4l_cam_buf_create(struct v4l_cam *cam, struct buf_set *set, int n)
{
struct v4l2_create_buffers create = {
.memory = set->memory,
.count = n,
.format = set->fmt,
};
struct v4l2_buffer buf = {
.type = set->fmt.type,
.memory = set->memory,
};
/*
* If we ever have to support V4L2 devices, not implementing the
* VIDIOC_CREATE_BUFS ioctl(), we'll have to use REQBUFS for all our
* video buffers
*/
unsigned int i;
int ret = ioctl(cam->fd, VIDIOC_CREATE_BUFS, &create);
CAM_LOG("CREATE_BUFS for %x @ %ux%u: %d type %d",
v4l_format_pixelformat(&set->fmt),
v4l_format_width(&set->fmt), v4l_format_height(&set->fmt),
ret, set->fmt.type);
if (ret < 0)
return -errno;
buf.field = v4l_format_field(&set->fmt);
for (i = create.index; i < create.index + create.count; i++) {
buf.index = i;
ret = ioctl(cam->fd, VIDIOC_PREPARE_BUF, &buf);
if (ret < 0)
break;
}
CAM_LOG("PREPARE_BUF successful for %d buffers of %d: %d",
i - create.index, create.count, errno);
if (i == create.index)
/* already the first PREPARE_BUF above failed */
return -errno;
set->index = create.index;
set->num_buffers = create.count;
return 0;
}
/**
* v4l_cam_frame_data - return memory address
* @set: buffer set
* @idx: index within the set
*/
static uint8_t *v4l_cam_frame_data(const struct buf_set *set, int idx)
{
struct camera_memory *cmem;
const struct buf_desc *desc;
switch (set->memory) {
case V4L2_MEMORY_USERPTR:
cmem = set->buffers[0].cam_mem;
return (uint8_t *)cmem->data + cmem->size * idx;
case V4L2_MEMORY_MMAP:
desc = set->buffers + idx;
return (uint8_t *)desc->cam_mem->data;
default:
return NULL;
}
}
static int v4l_cam_mmem_by_addr(const struct buf_set *set, const uint8_t *ptr)
{
const struct buf_desc *desc;
unsigned int i;
for (i = 0, desc = set->buffers; i < set->num_buffers; i++, desc++)
if (ptr == (uint8_t *)desc->cam_mem->data)
return i;
LOGE("MMAP: Buffer %p not found!\n", ptr);
return -ENOENT;
}
static int v4l_cam_umem_by_addr(const struct buf_set *set, const uint8_t *ptr)
{
struct camera_memory *cmem = set->buffers[0].cam_mem;
unsigned int i = (ptr - (uint8_t *)cmem->data) / cmem->size;
if (i < set->num_buffers && i * cmem->size + (uint8_t *)cmem->data == ptr)
return i;
LOGE("USERPTR: Buffer %p not found!\n", ptr);
return -ENOENT;
}
static int v4l_cam_mem_by_addr(const struct v4l_cam *cam, const uint8_t *ptr)
{
const struct buf_set *set = cam->buf_record_active;
switch (set->memory) {
case V4L2_MEMORY_USERPTR:
return v4l_cam_umem_by_addr(set, ptr);
case V4L2_MEMORY_MMAP:
return v4l_cam_mmem_by_addr(set, ptr);
default:
return -EINVAL;
}
}
static int v4l_cam_ubuf_init(struct v4l_cam *cam, struct buf_set *set)
{
size_t sizeimage;
int i;
switch (set->fmt.type) {
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
sizeimage = set->fmt.fmt.pix.sizeimage;
break;
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
for (i = 0, sizeimage = 0;
i < min(VIDEO_MAX_PLANES, set->fmt.fmt.pix_mp.num_planes);
i++)
sizeimage += set->fmt.fmt.pix_mp.plane_fmt[i].sizeimage;
break;
default:
return -EINVAL;
}
set->buffers[0].cam_mem = cam->cb.get_mem(-1, sizeimage, set->num_buffers,
0, cam->cb.arg);
set->buffers[0].offset = 0;
return set->buffers[0].cam_mem ? 0 : -ENOMEM;
}
static int v4l_cam_mbuf_init(struct v4l_cam *cam, struct buf_set *set)
{
struct v4l2_buffer buf = {
.type = set->fmt.type,
.memory = set->memory,
};
unsigned int i;
int ret;
for (i = 0; i < set->num_buffers; i++) {
buf.index = set->index + i;
ret = ioctl(cam->fd, VIDIOC_QUERYBUF, &buf);
if (ret < 0)
return -errno;
set->buffers[i].cam_mem = cam->cb.get_mem(cam->fd, buf.length,
1, buf.m.offset, cam->cb.arg);
set->buffers[i].offset = buf.m.offset;
}
return 0;
}
static int v4l_cam_buf_init(struct v4l_cam *cam, struct buf_set *set)
{
switch (set->memory) {
case V4L2_MEMORY_USERPTR:
return v4l_cam_ubuf_init(cam, set);
case V4L2_MEMORY_MMAP:
return v4l_cam_mbuf_init(cam, set);
default:
return -EINVAL;
}
}
static int v4l_cam_configure(struct v4l_cam *cam)
{
struct v4l2_requestbuffers req = {
.count = NUM_BUF_PREVIEW,
.type = cam->buf_preview.fmt.type,
.memory = cam->buf_preview.memory,
};
int ret = v4l_cam_format_set(cam, &cam->buf_preview.fmt);
CAM_LOG("%s(): S_FMT() = %d", __func__, ret);
if (ret < 0 || cam->configured)
return ret;
ret = ioctl(cam->fd, VIDIOC_REQBUFS, &req);
if (ret < 0)
return -errno;
cam->buf_preview.num_buffers = req.count;
cam->buf_preview.index = 0;
ret = v4l_cam_buf_create(cam, &cam->buf_picture, NUM_BUF_PICTURE);
if (ret < 0)
goto err;
ret = v4l_cam_buf_create(cam, &cam->buf_record, NUM_BUF_RECORD);
if (!ret)
ret = v4l_cam_buf_init(cam, &cam->buf_preview);
if (!ret)
ret = v4l_cam_buf_init(cam, &cam->buf_picture);
if (!ret)
ret = v4l_cam_buf_init(cam, &cam->buf_record);
if (!ret)
return 0;
err:
v4l_cam_destroy_buffers(cam);
return ret;
}
/*
* Is it enough to support V4L2_MEMORY_MMAP or do we also have to support
* V4L2_MEMORY_USERPTR? Verify preview_stream_ops_t to see, where preview
* buffers are allocated.
*/
static int v4l_cam_buf_queue(struct v4l_cam *cam, struct buf_set *set, unsigned int n)
{
struct v4l2_buffer buf = {
.type = set->fmt.type,
.memory = set->memory,
.index = set->index + n,
};
if (n >= set->num_buffers)
return -ENOMEM;
/* TODO: support V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE */
switch (set->memory) {
case V4L2_MEMORY_USERPTR:
/* TODO: test USERPTR */
buf.m.userptr = (unsigned long)set->buffers[n].cam_mem->data;
buf.length = set->buffers[n].cam_mem->size;
break;
default:
return -ENOSYS;
case V4L2_MEMORY_MMAP:
break;
}
return ioctl(cam->fd, VIDIOC_QBUF, &buf) < 0 ? -errno : 0;
}
static int v4l_cam_buf_dequeue(struct v4l_cam *cam, struct buf_set *set)
{
struct v4l2_buffer buf = {
.type = set->fmt.type,
.memory = set->memory,
};
return ioctl(cam->fd, VIDIOC_DQBUF, &buf) < 0 ? -errno : (int)buf.index;
}
/* *** Android camera API *** */
/**
* locking: call only with thread_mutex held!
*/
static int v4l_cam_update_preview_geometry(struct v4l_cam *cam)
{
struct preview_stream_ops *ops = cam->preview_ops;
if (ops && (cam->preview_width != cam->acp.preview_fmt.width ||
cam->preview_height != cam->acp.preview_fmt.height)) {
int ret = ops->set_buffers_geometry(ops,
cam->acp.preview_fmt.width,
cam->acp.preview_fmt.height,
cam->fb_fmt);
if (ret != NO_ERROR)
return -EREMOTEIO;
cam->preview_width = cam->acp.preview_fmt.width;
cam->preview_height = cam->acp.preview_fmt.height;
}
return 0;
}
/* Set the preview_stream_ops to which preview frames are sent */
static int v4l_cam_set_preview_window(struct camera_device *dev,
struct preview_stream_ops *ops)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
int ret;
pthread_mutex_lock(&cam->thread_mutex);
cam->preview_width = cam->preview_height = 0;
cam->preview_ops = ops;
ret = v4l_cam_update_preview_geometry(cam);
pthread_mutex_unlock(&cam->thread_mutex);
CAM_LOG("%s(%p)", __func__, ops);
return ret;
}
/* Set the notification and data callbacks */
static void v4l_cam_set_callbacks(struct camera_device *dev,
camera_notify_callback notify_cb,
camera_data_callback data_cb,
camera_data_timestamp_callback data_cb_timestamp,
camera_request_memory get_memory,
void *arg)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
CAM_LOG("%s()", __func__);
cam->cb.arg = arg;
cam->cb.notify = notify_cb;
cam->cb.data = data_cb;
cam->cb.data_ts = data_cb_timestamp;
cam->cb.get_mem = get_memory;
}
/*
* The following three functions all take a msg_type, which is a bitmask of
* the messages defined in include/ui/Camera.h
*/
#define MSG_MASK (CAMERA_MSG_ERROR | CAMERA_MSG_SHUTTER | \
CAMERA_MSG_PREVIEW_FRAME | CAMERA_MSG_VIDEO_FRAME | \
CAMERA_MSG_RAW_IMAGE | CAMERA_MSG_RAW_IMAGE_NOTIFY)
/* Enable a message, or set of messages. */
static void v4l_cam_enable_msg_type(struct camera_device *dev, int32_t msg_type)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
CAM_LOG("%s(0x%x)", __func__, msg_type);
cam->msg |= msg_type & cam->msg_mask;
}
/* Disable a message, or a set of messages. */
static void v4l_cam_disable_msg_type(struct camera_device *dev, int32_t msg_type)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
CAM_LOG("%s(0x%x)", __func__, msg_type);
cam->msg &= ~msg_type;
}
/* Return true, if _all_ requested messages are enabled */
static int v4l_cam_msg_type_enabled(struct camera_device *dev, int32_t msg_type)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
CAM_LOG("%s()", __func__);
return (cam->msg & msg_type) == msg_type;
}
static int v4l_cam_cfmt2v4l2(struct v4l_cam *cam, struct camera_format *cfmt,
struct v4l2_format *vfmt, uint32_t cap)
{
int ret;
if (cap & V4L2_CAP_VIDEO_CAPTURE) {
/* Prefer the simple case for now */
vfmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
vfmt->fmt.pix.width = cfmt->width;
vfmt->fmt.pix.height = cfmt->height;
vfmt->fmt.pix.pixelformat = cfmt->fourcc;
vfmt->fmt.pix.colorspace = cfmt->cspace;
vfmt->fmt.pix.field = V4L2_FIELD_ANY;
} else if (cap & V4L2_CAP_VIDEO_CAPTURE_MPLANE) {
vfmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
vfmt->fmt.pix_mp.width = cfmt->width;
vfmt->fmt.pix_mp.height = cfmt->height;
vfmt->fmt.pix_mp.pixelformat = cfmt->fourcc;
vfmt->fmt.pix_mp.colorspace = cfmt->cspace;
vfmt->fmt.pix_mp.field = V4L2_FIELD_ANY;
} else {
/* BUG: impossible, we checked this in .open()! */
return -EINVAL;
}
ret = ioctl(cam->fd, VIDIOC_TRY_FMT, vfmt);
if (ret < 0 && errno == EINVAL && ((cap & (V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_VIDEO_CAPTURE_MPLANE)) ==
(V4L2_CAP_VIDEO_CAPTURE |
V4L2_CAP_VIDEO_CAPTURE_MPLANE)))
/*
* One more chance: if the driver supports both buffer types,
* and CAPTURE failed, try CAPTURE_MPLANE
*/
return v4l_cam_cfmt2v4l2(cam, cfmt, vfmt,
V4L2_CAP_VIDEO_CAPTURE_MPLANE);
CAM_LOG("%s() type %d", __func__, vfmt->type);
return ret < 0 ? -errno : 0;
}
#include <linux/fb.h>
#define FBDEV "/dev/graphics/fb0"
#define v4l_cam_try_buftype(cam, cmd, arg) \
({ \
int __ret; \
/* Try single plane first */ \
if (cam->cap & V4L2_CAP_VIDEO_CAPTURE) { \
(arg)->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; \
__ret = ioctl(cam->fd, cmd, (arg)); \
__ret = __ret < 0 ? -errno : 0; \
} else { \
__ret = -EINVAL; \
} \
if (__ret == -EINVAL && \
(cam->cap & V4L2_CAP_VIDEO_CAPTURE_MPLANE)) { \
(arg)->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; \
ioctl(cam->fd, cmd, (arg)); \
__ret = -errno; \
} \
__ret; \
})
/* I think the compiler will optimize constant false if's away */
#define v4l_cam_make_fmt(f, p, c, i, w, h, bug) do { \
switch ((f)->type) { \
case V4L2_BUF_TYPE_VIDEO_CAPTURE: \
if (p) \
(f)->fmt.pix.pixelformat = p; \
if (c) \
(f)->fmt.pix.colorspace = c; \
if (i > 0) \
(f)->fmt.pix.field = i; \
if (w) \
(f)->fmt.pix.width = w; \
if (h) \
(f)->fmt.pix.height = h; \
break; \
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: \
if (p) \
(f)->fmt.pix_mp.pixelformat = p; \
if (c) \
(f)->fmt.pix_mp.colorspace = c; \
if (i > 0) \
(f)->fmt.pix_mp.field = i; \
if (w) \
(f)->fmt.pix_mp.width = w; \
if (h) \
(f)->fmt.pix_mp.height = h; \
break; \
default: \
bug; \
} \
} while (0)
#define v4l_cam_try_or_bug(fd, fmt) do { \
if (ioctl((fd), VIDIOC_TRY_FMT, (fmt)) < 0) { \
LOGE("%s(): buggy camera driver! VIDIOC_TRY_FMT should never fail!", \
__func__); \
return -errno; \
} \
} while (0)
/* This is all just guessing, really */
static int v4l_cam_default_fmts(struct v4l_cam *cam)
{
struct v4l2_format fmt = {.fmt.raw_data = {0}};
/* Take the first format from enumeration */
struct v4l2_fmtdesc fmtdesc = {.index = 0,};
struct camera_format *pvu = &cam->acp.preview_fmt;
unsigned int width, height, fourcc;
int fbdev;
int bpp;
int ret;
fbdev = open(FBDEV, O_RDONLY);
if (fbdev >= 0) {
struct fb_var_screeninfo vinfo;
ret = ioctl(fbdev, FBIOGET_VSCREENINFO, &vinfo);
close(fbdev);
if (!ret) {
width = vinfo.xres;
height = vinfo.yres;
bpp = vinfo.bits_per_pixel;
CAM_LOG("%s() fbdev %ux%u:%d", __func__, width, height, bpp);
} else {
fbdev = -1;
}
}
if (fbdev < 0) {
width = 640;
height = 480;
bpp = 16;
}
/*
* It is assumed, that the preview display uses one of the below
* formats or the closest to it. For the camera we try to find a
* matching format among supported ones. Otherwise we pick up the first
* one, supported by Android.
*/
switch (bpp) {
case 15:
fourcc = V4L2_PIX_FMT_RGB555;
cam->fb_fmt = HAL_PIXEL_FORMAT_RGBA_5551;
break;
case 16:
fourcc = V4L2_PIX_FMT_RGB565;
cam->fb_fmt = HAL_PIXEL_FORMAT_RGB_565;
break;
case 24:
fourcc = V4L2_PIX_FMT_BGR24;
cam->fb_fmt = HAL_PIXEL_FORMAT_RGB_888;
break;
case 32:
fourcc = V4L2_PIX_FMT_BGR32;
cam->fb_fmt = HAL_PIXEL_FORMAT_RGBA_8888;
break;
default:
return -EINVAL;
}
while (!v4l_cam_try_buftype(cam, VIDIOC_ENUM_FMT, &fmtdesc)) {
if (!fmt.type)
fmt.type = fmtdesc.type;
if (fmtdesc.pixelformat == fourcc ||
(!pvu->fourcc && camera_param_fourcc2str(fmtdesc.pixelformat)))
pvu->fourcc = fmtdesc.pixelformat;
camera_param_add_supported_format(&cam->acp, fmtdesc.pixelformat);
if (fmtdesc.pixelformat == V4L2_PIX_FMT_JPEG)
cam->picture_compressed_4cc = V4L2_PIX_FMT_JPEG;
else if (fmtdesc.pixelformat == V4L2_PIX_FMT_NV21 &&
!cam->picture_compressed_4cc)
cam->picture_compressed_4cc = V4L2_PIX_FMT_NV21;
fmtdesc.index++;
}
if (!fmt.type || !pvu->fourcc)
return -ENODEV;
CAM_LOG("%s(): type %x, fourcc %x", __func__, fmt.type, pvu->fourcc);
/* TODO: determine colorspace, if needed */
v4l_cam_make_fmt(&fmt, pvu->fourcc, 0, V4L2_FIELD_ANY, width, height, return -EINVAL);
/*
* We assume this TRY_FMT doesn't change the pixel format, since we're
* using a definitely supported one
*/
v4l_cam_try_or_bug(cam->fd, &fmt);
cam->converter = camera_frame_converter_select(pvu->fourcc, cam->fb_fmt);
if (!cam->converter)
return -EINVAL;
switch (fmt.type) {
case V4L2_BUF_TYPE_VIDEO_CAPTURE:
pvu->width = fmt.fmt.pix.width;
pvu->height = fmt.fmt.pix.height;
break;
case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
pvu->width = fmt.fmt.pix_mp.width;
pvu->height = fmt.fmt.pix_mp.height;
break;
default:
/* BUG */
return -EINVAL;
}
cam->acp.record_fmt = *pvu;
cam->acp.picture_fmt = *pvu;
if (cam->picture_compressed_4cc) {
cam->msg_mask |= CAMERA_MSG_COMPRESSED_IMAGE;
if (cam->picture_compressed_4cc == V4L2_PIX_FMT_JPEG)
v4l_cam_make_fmt(&fmt, V4L2_PIX_FMT_JPEG, V4L2_COLORSPACE_JPEG,
-1, 0, 0, return -EINVAL);
else {
camera_param_add_picture_format(&cam->acp, V4L2_PIX_FMT_JPEG);
v4l_cam_make_fmt(&fmt, V4L2_PIX_FMT_NV21, V4L2_COLORSPACE_JPEG,
-1, 0, 0, return -EINVAL);
}
v4l_cam_try_or_bug(cam->fd, &fmt);
cam->acp.picture_fmt.fourcc = v4l_format_pixelformat(&fmt);
}
return 0;
}
/*
* Set the camera parameters. This returns BAD_VALUE if any parameter is
* invalid or not supported. Samsung doesn't perform any hardware configuration
* here, only upon .start_preview(), only verifies and records parameters here.
*/
static int v4l_cam_set_parameters(struct camera_device *dev,
const char *parms)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
int ret;
CAM_LOG("%s(%s)", __func__, parms);
if (parms) {
camera_param_parse_fmts(&cam->acp, parms);
/*
* TODO: implement independent recording, for now we only
* support fmt(record) == fmt(preview)
*/
cam->acp.record_fmt = cam->acp.preview_fmt;
} else {
ret = v4l_cam_default_fmts(cam);
if (ret < 0)
return ret;
}
ret = v4l_cam_cfmt2v4l2(cam, &cam->acp.preview_fmt,
&cam->buf_preview.fmt, cam->cap);
if (ret < 0)
return ret;
ret = v4l_cam_cfmt2v4l2(cam, &cam->acp.record_fmt,
&cam->buf_record.fmt, cam->cap);
if (ret < 0)
return ret;
ret = v4l_cam_cfmt2v4l2(cam, &cam->acp.picture_fmt,
&cam->buf_picture.fmt, cam->cap);
/* Do not reconfigure if preview is running */
if (ret < 0 || cam->preview)
return ret;
/* set preview format and allocate buffer sets */
ret = v4l_cam_configure(cam);
CAM_LOG("%s() configure() = %d", __func__, ret);
if (ret < 0)
return ret;
cam->configured = true;
return 0;
}
static int v4l_cam_buf_queue_set(struct v4l_cam *cam, struct buf_set *set)
{
unsigned int n;
int ret = -ENOBUFS;
for (n = 0; n < set->num_buffers; n++) {
ret = v4l_cam_buf_queue(cam, set, n);
if (ret < 0)
break;
}
/* Any buffers queued successfully? */
return n ? (int)n : ret;
}
/* Start preview mode. */
static int v4l_cam_start_preview(struct camera_device *dev)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
int ret;
CAM_LOG("%s()", __func__);
if (cam->preview)
return 0;
if (!cam->configured) {
ret = v4l_cam_set_parameters(dev, NULL);
if (ret < 0)
return ret;
}
pthread_mutex_lock(&cam->thread_mutex);
ret = v4l_cam_update_preview_geometry(cam);
pthread_mutex_unlock(&cam->thread_mutex);
if (ret < 0)
return ret;
ret = v4l_cam_buf_queue_set(cam, &cam->buf_preview);
if (ret < 0)
return ret;
pthread_mutex_lock(&cam->thread_mutex);
cam->preview = true;
pthread_cond_signal(&cam->preview_cond);
pthread_mutex_unlock(&cam->thread_mutex);
return 0;
}
/* Stop a previously started preview. */
static void v4l_cam_stop_preview(struct camera_device *dev)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
uint8_t z = 0;
if (!cam->preview)
return;
cam->preview = false;
/*
* Android calls various camera HAL methods asynchronously from
* different execution threads. We have to take care to prevent races
* and lock-ups ourselves. One of the requirements in this case is, that
* we shall not return from our .stop_preview() method until we really
* make sure, that the preview thread is safely parked and is ready to
* take a .start_preview() call.
*/
write(cam->preview_pipe[0], &z, 1);
pthread_mutex_lock(&cam->thread_mutex);
pthread_cond_wait(&cam->stop_cond,
&cam->thread_mutex);
pthread_mutex_unlock(&cam->thread_mutex);
v4l_cam_streaming_stop(cam, &cam->buf_preview);
CAM_LOG("%s(): streaming stopped", __func__);
}
/* Returns true if preview is enabled. */
static int v4l_cam_preview_enabled(struct camera_device *dev)
{
struct v4l_cam *cam = container_of(dev, struct v4l_cam, acam);
CAM_LOG("%s()", __func__);
return cam->preview;
}
/* See comments in hardware/libhardware/include/hardware/camera.h */
static int v4l_cam_store_meta_data_in_buffers(struct camera_device *dev, int meta)
{
CAM_LOG("%s(%d)", __func__, meta);
if (meta)
return INVALID_OPERATION;
return OK;
}
static void v4l_cam_picture_deliver(struct v4l_cam *cam, int idx)
{
struct buf_set *set = &cam->buf_picture;
if (cam->msg & CAMERA_MSG_SHUTTER)
cam->cb.notify(CAMERA_MSG_SHUTTER, 0, 0, cam->cb.arg);
if (cam->msg & CAMERA_MSG_RAW_IMAGE_NOTIFY)
cam->cb.notify(CAMERA_MSG_RAW_IMAGE_NOTIFY, 0, 0, cam->cb.arg);
if (cam->msg & CAMERA_MSG_RAW_IMAGE) {
nsecs_t timestamp = systemTime(SYSTEM_TIME_MONOTONIC);
switch (set->memory) {
case V4L2_MEMORY_USERPTR:
cam->cb.data_ts(timestamp, CAMERA_MSG_RAW_IMAGE,
set->buffers[0].cam_mem, idx,
cam->cb.arg);
break;
case V4L2_MEMORY_MMAP:
cam->cb.data_ts(timestamp, CAMERA_MSG_RAW_IMAGE,
set->buffers[idx].cam_mem, 0,
cam->cb.arg);
break;