-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcamera_test.c
More file actions
executable file
·1449 lines (1231 loc) · 32.8 KB
/
camera_test.c
File metadata and controls
executable file
·1449 lines (1231 loc) · 32.8 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <getopt.h> /* getopt_long() */
#include <fcntl.h> /* low-level i/o */
#include <unistd.h>
#include <errno.h>
#include <malloc.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/select.h>
#include <termios.h>
//#include <stropts.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <asm/types.h> /* for videodev2.h */
#include <linux/videodev2.h>
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
typedef struct camera_cfg{
int fd;
int x;
int y;
int w;
int h;
int pic_w;
int pic_h;
char mclk;
int videobuf_cnt;
} camera_cfg_t;
enum{
UC13X_EXTCMD_G_BUFADDR = 0x80001000,
UC13X_EXTCMD_ALLOC_BUF,
UC13X_EXTCMD_FREE_BUF,
UC13X_EXTCMD_IMG_ZOOM,
UC13X_EXTCMD_IMG_ROTATE,
};
enum{
UC13X_CAM_ROTATOR_0 = 0x80001000,
UC13X_CAM_ROTATOR_90,
UC13X_CAM_ROTATOR_180,
UC13X_CAM_ROTATOR_270,
UC13X_CAM_ROTATOR_FLIP,
UC13X_CAM_ROTATOR_MIEEOR,
};
struct uc13x_img_zoom{
unsigned int src_index;
unsigned int dst_index;
unsigned int imageformat;
unsigned int srcwidth;
unsigned int srcheight;
unsigned int dstwidth;
unsigned int dstheight;
};
struct uc13x_img_rot{
unsigned int src_id;
unsigned int dst_id;
unsigned int dst_offset;
unsigned int rotate_mode;
unsigned int width;
unsigned int height;
};
struct uc13x_mem_info{
unsigned int blk_id;
unsigned int phy_addr;
unsigned int vir_addr;
};
struct un13x_frame_info{
unsigned int size;
void * buf;
};
struct uc13x_extcmd{
unsigned int cmd;
union{
unsigned int id; /*UC13X_EXTCMD_G_BUFADDR*/
unsigned int phy_addr; /*output give addr for video buffer*/
struct uc13x_mem_info mem_info;
struct un13x_frame_info frame;
struct uc13x_img_zoom zoom;
struct uc13x_img_rot rot;
}u;
};
FILE* fyuv = NULL;
int camera_extioctl(int fd, struct uc13x_extcmd* p_cmd){
int ret = 0;
unsigned int input;
input = (unsigned int )p_cmd;
ret = ioctl (fd, VIDIOC_S_INPUT, &input);
if(ret !=0){
printf ("VIDIOC_S_INPUT error %d\n",ret);
return -1;
}
return 0;
}
int camera_zoom_img(int fd, int src,int dst ,camera_cfg_t *cfg){
struct uc13x_extcmd cmd;
cmd.cmd = UC13X_EXTCMD_IMG_ZOOM;
cmd.u.zoom.src_index = src;
cmd.u.zoom.dst_index = dst;
cmd.u.zoom.imageformat = 0;
cmd.u.zoom.srcwidth = cfg->w;
cmd.u.zoom.srcheight = cfg->h;
cmd.u.zoom.dstwidth = cfg->pic_w;
cmd.u.zoom.dstheight = cfg->pic_h;
return camera_extioctl(fd, &cmd);
}
#define VIDEO_OUT_DBG_EN
typedef enum{
PF_RGB565,
PF_RGB888,
PF_YUV420,
PF_YUV420I,
PF_YUV422
}PIXEL_FORMAT;
#define ULONG unsigned long
#ifdef VIDEO_OUT_DBG_EN
#define VIDEO_OUT_DBG printf
#else
#define VIDEO_OUT_DBG 0,
#endif
#define VIDEO_OUT_ERR printf
typedef enum{
tp_video,
tp_osd
}video_type;
typedef struct{
video_type type;
int idev; //v4l2 device handle
int buf_count; //display buffer number
int buf_size;
int index; //current display buffer id
unsigned char **buff; //display buffer buffer addr
int *offsett; //return by driver
int pixel_format;//input pic pixel format
int src_x; //input pic width
int src_y; //input pic height
int left;// Specifies the x-coordinate of the upper-left corner of the input pic on screen.
int top; //Specifies the y-coordinate of the upper-left corner of the input pic on screen.
int win_x;// Specifies the width of the input pic on screen.
int win_y;// Specifies the height of the input pic on screen.
int bpp; //bits per pixel
int pic_size;
}video, *p_video;
typedef struct{
PIXEL_FORMAT format;
int width; //src picture width
int height; //src picture height
int disp_top; //ldc pic display position
int disp_left; //ldc pic display position
int disp_width; //ldc pic display width
int disp_height; //ldc pic display height
p_video pvideo; // camera used video
}cv_param;
static int set_video_param(video *pvideo,cv_param *p_cv_param)
{
struct v4l2_format format;
struct v4l2_requestbuffers req_buffers;
struct v4l2_buffer qery_buffer;
int i, k;
memset((void *) &format, 0, sizeof(struct v4l2_format));
/* set video param */
format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
if (pvideo->type == tp_video){
format.fmt.pix.width = p_cv_param->width;
format.fmt.pix.height = p_cv_param->height;
}else{
format.fmt.pix.width = p_cv_param->width;
format.fmt.pix.height = p_cv_param->height;
}
if (PF_RGB565 == p_cv_param->format){
format.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB565;
format.fmt.pix.field = V4L2_FIELD_ANY;
format.fmt.pix.bytesperline = format.fmt.pix.width * 2;
format.fmt.pix.sizeimage = format.fmt.pix.bytesperline * format.fmt.pix.height;
format.fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
}else if (PF_YUV420 == p_cv_param->format){
format.fmt.pix.pixelformat = V4L2_PIX_FMT_NV12;
format.fmt.pix.field = V4L2_FIELD_ANY;
format.fmt.pix.bytesperline = format.fmt.pix.width * 3 / 2;
format.fmt.pix.sizeimage = format.fmt.pix.bytesperline * format.fmt.pix.height;
format.fmt.pix.colorspace = V4L2_COLORSPACE_JPEG;
}else{
VIDEO_OUT_ERR("[set_video_param]not supported pixel format\n");
return -1;
}
i = ioctl(pvideo->idev, VIDIOC_S_FMT, &format);
if (i == 0){
pvideo->pixel_format = p_cv_param->format;
pvideo->src_x = format.fmt.pix.width;
pvideo->src_y = format.fmt.pix.height;
pvideo->bpp = (PF_RGB565 == p_cv_param->format) ? 16 : 12 ;
pvideo->pic_size = (PF_RGB565 == p_cv_param->format) ?
(pvideo->src_x * pvideo->src_y * 2) :
(pvideo->src_x * pvideo->src_y * 3 / 2);
}else{
VIDEO_OUT_ERR("[set_video_param]set video param err\n");
return -1;
}
/* set video output param */
if (pvideo->type == tp_video){
format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
}else{
format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
}
i = ioctl(pvideo->idev, VIDIOC_G_FMT, &format);
if (i != 0){
VIDEO_OUT_ERR("[set_video_param]get video output param err\n");
return -1;
}
format.fmt.win.w.left = p_cv_param->disp_left;
format.fmt.win.w.top = p_cv_param->disp_top;
if (pvideo->type == tp_video){
format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
format.fmt.win.w.width = p_cv_param->disp_width;
format.fmt.win.w.height = p_cv_param->disp_height;
}else{
format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
format.fmt.win.w.width = p_cv_param->disp_width;
format.fmt.win.w.height = p_cv_param->disp_height;
}
i = ioctl(pvideo->idev, VIDIOC_S_FMT, &format);
if (i != 0){
VIDEO_OUT_ERR("[set_video_param]set video param failed\n");
return -1;
}
pvideo->win_x = format.fmt.win.w.width;
pvideo->win_y = format.fmt.win.w.height;
pvideo->left = p_cv_param->disp_left;
pvideo->top = p_cv_param->disp_top;
/* request buffers */
memset(&req_buffers, 0, sizeof(req_buffers));
req_buffers.count = pvideo->buf_count;
req_buffers.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
req_buffers.memory = V4L2_MEMORY_MMAP;
VIDEO_OUT_DBG("[set_video_param]req buffer :(%d-%d-%d)\n",
req_buffers.count,
req_buffers.type,
req_buffers.memory);
i = ioctl(pvideo->idev, VIDIOC_REQBUFS, &req_buffers);
if (i != 0){
VIDEO_OUT_ERR("[set_video_param]driver malloc buffers err\n");
return -1;
}
/* query buffer */
memset(&qery_buffer, 0, sizeof(qery_buffer));
for (k = 0;k < pvideo->buf_count ;k++){
qery_buffer.index = k;
qery_buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
qery_buffer.memory = V4L2_MEMORY_MMAP;
i = ioctl(pvideo->idev, VIDIOC_QUERYBUF, &qery_buffer);
if (i != 0){
VIDEO_OUT_ERR("[set_video_param]driver malloc buffers err\n");
return -1;
}
pvideo->offsett[k] = qery_buffer.m.offset;
pvideo->buf_size = qery_buffer.length;
VIDEO_OUT_ERR("[set_video_param] buf size = %x \toffset = %x\n",
pvideo->buf_size,
pvideo->offsett[k]);
pvideo->buff[k] = mmap(NULL,
pvideo->buf_size,
PROT_READ | PROT_WRITE,
MAP_SHARED,
pvideo->idev,
pvideo->offsett[k]);
if (pvideo->buff[k] == (unsigned char *) 0xffffffff){
VIDEO_OUT_ERR("[set_video_param]map buffers err = %d \n", errno);
return -1;
}
VIDEO_OUT_DBG("[set_video_param]qery buffer(%d:%x:%x)\n",
k,
pvideo->offsett[k],
pvideo->buff[k]);
}
pvideo->index = 0;
return i;
}
/*
*init video handle pic buffer,
* temp set 3 frame buffer.
*/
static int init_video(video_type type,p_video pvideo,int ibuf)
{
int i;
pvideo->type = type;
pvideo->buf_count = ibuf; // internal buffer num , for flip display
pvideo->buff = (unsigned char * *) malloc(sizeof(char *) * ibuf);
if (pvideo->buff == NULL){
VIDEO_OUT_ERR("[init_video]malloc memory err\n");
return -1;
}
pvideo->index = 0; //init index
for (i = 0;i < ibuf;i++){
pvideo->buff[i] = NULL;
}
pvideo->offsett = (int *) malloc(sizeof(int) * ibuf);
if (pvideo->offsett == NULL){
//not free memory
VIDEO_OUT_ERR("[init_video]malloc memory err\n");
return -1;
}
if (type == tp_video){
pvideo->idev = open("/dev/video0", O_RDWR);
}else{
pvideo->idev = open("/dev/video1", O_RDWR);
}
VIDEO_OUT_ERR("opend device pvideo->idev is %p\n",pvideo->idev);
if (pvideo->idev == 0){
free(pvideo->buff);
pvideo->buff = NULL;
VIDEO_OUT_ERR("[init_video]open video device err\n");
return -1;
}
return 0;
}
static void uninit_video(p_video pvideo)
{
int k = 0;
VIDEO_OUT_ERR("unmap video mem");
for (k = 0;k < pvideo->buf_count ;k++){
munmap(pvideo->buff[k],pvideo->buf_size);
}
if (pvideo->buff){
free(pvideo->buff);
}
if (pvideo->offsett){
free(pvideo->offsett);
}
if (pvideo->idev){
close(pvideo->idev);
VIDEO_OUT_ERR("closed device pvideo->idev is %p\n",pvideo->idev);
VIDEO_OUT_ERR("[uninit_video]close video device\n");
}
memset((void *) pvideo, 0, sizeof(video));
}
static void init_video_buffer(p_video pvideo)
{
int i, j;
unsigned short * pframe;
struct rgb565{
unsigned short r : 5;
unsigned short g : 6;
unsigned short b : 5;
};
union{
struct rgb565 rgb565;
unsigned short v;
}rgb[3];
if (PF_RGB565 == pvideo->pixel_format){
rgb[0].rgb565.r = 0x0;
rgb[0].rgb565.g = 0;
rgb[0].rgb565.b = 0;
rgb[1].rgb565.r = 0;
rgb[1].rgb565.g = 0x0;
rgb[1].rgb565.b = 0;
rgb[2].rgb565.r = 0;
rgb[2].rgb565.g = 0;
rgb[2].rgb565.b = 0x0;
for (i = 0;i < pvideo->buf_count;i++){
pframe = (unsigned short *) pvideo->buff[i];
VIDEO_OUT_ERR("[init_video_buffer ]PF_RGB565 pframe 0x%x pic_size %d\n",
pframe,
pvideo->pic_size);
for (j = 0;j < pvideo->pic_size / 2;j++){
pframe[j] = rgb[i % 4].v;
}
}
}else if (PF_YUV420 == pvideo->pixel_format){
int i, j;
char pff[3];
unsigned char * pframe;
char f0 = 0x0;
char f1 = 0x0;
char f2 = 0x0;
pff[0] = f0;
pff[1] = f1;
pff[2] = f2;
for (i = 0;i < pvideo->buf_count;i++){
pframe = (unsigned char *) pvideo->buff[i];
VIDEO_OUT_ERR("[init_video_buffer ]PF_YUV420 pframe 0x%x pic_size %d\n",
pframe,
pvideo->pic_size);
for (j = 0;j < pvideo->pic_size;j++){
pframe[j] = pff[i % 3];
}
}
}else{
VIDEO_OUT_ERR("[init_video_buffer ]not supported pixel_format \n");
}
}
static void enable_video(p_video pvideo)
{
ioctl(pvideo->idev, VIDIOC_STREAMON, 0);
}
static void disable_video(p_video pvideo)
{
ioctl(pvideo->idev, VIDIOC_STREAMOFF, 0);
}
static int set_position(p_video pvideo,int x,int y)
{
int i;
struct v4l2_format format;
memset((void *) &format, 0, sizeof(struct v4l2_format));
if (pvideo->type == tp_video){
format.type = V4L2_BUF_TYPE_VIDEO_OVERLAY;
}else//tp_osd
{
format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY;
}
i = ioctl(pvideo->idev, VIDIOC_G_FMT, &format);
if (i == 0){
format.fmt.win.w.left = x;
format.fmt.win.w.top = y;
ioctl(pvideo->idev, VIDIOC_S_FMT, &format);
}
return i;
}
static int qbuf_video(p_video pvideo)
{
struct v4l2_buffer vbuffer;
memset((void *) &vbuffer, 0, sizeof(struct v4l2_buffer));
vbuffer.index = pvideo->index;
vbuffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
vbuffer.memory = V4L2_MEMORY_MMAP;
vbuffer.m.offset = pvideo->offsett[pvideo->index];
return ioctl(pvideo->idev, VIDIOC_QBUF, &vbuffer);
}
//now only support yuv420
ULONG create_videoout(cv_param *p_cv_param)
{
p_video pvideo;
int irtn;
video_type type = tp_video;
if (!p_cv_param){
VIDEO_OUT_ERR("[create_videoout], in param p_cv_param error!!!\n");
return 0;
}
if (p_cv_param->disp_height > SCREEN_HEIGHT - p_cv_param->disp_top){
VIDEO_OUT_ERR("[create_videoout], in param p_cv_param->disp_height not fit screen !!!\n");
p_cv_param->disp_height = SCREEN_HEIGHT - p_cv_param->disp_top;
}
if (p_cv_param->disp_width > SCREEN_WIDTH - p_cv_param->disp_left){
VIDEO_OUT_ERR("[create_videoout], in param p_cv_param->disp_width not fit screen !!!\n");
p_cv_param->disp_width = SCREEN_WIDTH - p_cv_param->disp_left;
}
if ((!p_cv_param->width) || (!p_cv_param->width)){
VIDEO_OUT_ERR("[create_videoout], not support p_cv_param->format !!!\n");
return 0;
}
//now only support yuv420
if (PF_YUV420 != p_cv_param->format){
VIDEO_OUT_ERR("[create_videoout], not support p_cv_param->format !!!\n");
p_cv_param->format = PF_YUV420;
}
pvideo = (p_video) malloc(sizeof(video));
p_cv_param->pvideo = pvideo;
if (pvideo){
memset((void *) pvideo, 0, sizeof(video));
irtn = init_video(type, pvideo, 3);
if (irtn == 0){
irtn = set_video_param(pvideo, p_cv_param);
if (irtn != 0){
VIDEO_OUT_ERR("set video param err \n");
uninit_video(pvideo);
free(pvideo);
return 0;
}
init_video_buffer(pvideo);
VIDEO_OUT_DBG("init_video_buffer over\n");
if (p_cv_param->disp_left < 0 ||
p_cv_param->disp_top <0 ||
p_cv_param->disp_left >= SCREEN_WIDTH ||
p_cv_param->disp_top>SCREEN_HEIGHT){
VIDEO_OUT_DBG("p_cv_param not fit screen \n");
set_position(pvideo, 0, 0);
}else{
set_position(pvideo, p_cv_param->disp_left, p_cv_param->disp_top);
}
qbuf_video(pvideo);
enable_video(pvideo);
VIDEO_OUT_DBG("pvideo (%d/%d/%d/%d)\n",
pvideo->pixel_format,
pvideo->win_x,
pvideo->win_y,
pvideo->bpp);
return (ULONG) pvideo;
}else{
uninit_video(pvideo);
VIDEO_OUT_ERR("init video err\n");
free(pvideo);
}
}
return 0; //malloc fialed
}
void destroy_videoout(p_video pvideo)
{
if (!pvideo){
VIDEO_OUT_ERR("[destroy_videoout] in param video handle error\n");
return ;
}
VIDEO_OUT_ERR("disable video\n");
disable_video(pvideo);
VIDEO_OUT_ERR("uninit video\n");
uninit_video(pvideo);
free(pvideo);
}
static int flip_video(p_video pvideo,char *p_y,char *p_u,char *p_v)
{
int irtn;
unsigned char * pframe;
struct v4l2_buffer vbuffer;
memset((void *) &vbuffer, 0, sizeof(struct v4l2_buffer));
pvideo->index++;
pvideo->index %= pvideo->buf_count;
VIDEO_OUT_DBG("pvideo->index %d pframe 0x%d src_x:%d src_y:%d win_x:%d, win_y:%d\n ",
pvideo->index,
(int)
pframe,
pvideo->src_x,
pvideo->src_y,
pvideo->win_x,
pvideo->win_y);
pframe = pvideo->buff[pvideo->index];
memcpy(pframe, p_y, (pvideo->src_x * pvideo->src_y));
pframe += (pvideo->src_x * pvideo->src_y);
memcpy(pframe, p_u, (pvideo->src_x * pvideo->src_y) / 4);
pframe += (pvideo->src_x * pvideo->src_y) / 4;
memcpy(pframe, p_v, (pvideo->src_x * pvideo->src_y) / 4);
vbuffer.index = pvideo->index;
vbuffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
vbuffer.memory = V4L2_MEMORY_MMAP;
vbuffer.m.offset = pvideo->offsett[pvideo->index];
return ioctl(pvideo->idev, VIDIOC_QBUF, &vbuffer);
}
void display_video(ULONG h_videoout,char *p_y,char *p_u,char *p_v)
{
p_video pvideo = (p_video) h_videoout;
if (!h_videoout || (!p_y) || (!p_u) || (!p_v)){
VIDEO_OUT_ERR("h_videoout[%p],p_y[%p],p_u[%p],p_v [%p]\n",h_videoout,p_y,p_u,p_v);
VIDEO_OUT_ERR("[display_video] in param video handle error\n");
return ;
}
//set_position(pvideo, pvideo->left, pvideo->top); //mask, and v4L2 driver fixed this bug
flip_video(pvideo, p_y, p_u, p_v);
}
unsigned long buffer_count;
int src = 0;
unsigned long buffer_count;
unsigned int win_x,win_y,win_w,win_h;
unsigned long width, height;
int mclk = 24;
#define CLEAR(x) memset (&(x), 0, sizeof (x))
#define DL_CMIF_U_OFFSET(y_addr,w,h) (unsigned int)((y_addr)+((w)*(h)))
#define DL_CMIF_V_OFFSET(y_addr,w,h) (unsigned int)((y_addr)+((w)*(h)/4)*5)
#define container_of(ptr, type, member) ( \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) ); )
#define list_entry(ptr, type, member) container_of(ptr, type, member)
struct list_head {
struct list_head *next, *prev;
};
enum{
UC13X_CTRL_STREAM_PRIV = 0x1000,
UC13X_CTRL_STREAM_ZOOM,
} ;
typedef enum{
IO_METHOD_READ,
IO_METHOD_MMAP,
IO_METHOD_USERPTR,
} io_method;
struct buffer_t{
void *start;
size_t length;
};
static char dev_name[20] = "/dev/video100";
static io_method io = IO_METHOD_MMAP;
static int fd = -1;
struct buffer_t buffers[32];
static unsigned int n_buffers = 0;
static unsigned long video_handle;
static void errno_exit(const char *s)
{
fprintf(stderr, "%s error %d, %s\n", s, errno, strerror(errno));
exit(EXIT_FAILURE);
}
static int xioctl(int fd,int request,void *arg)
{
int r;
r = ioctl(fd, request, arg);
while (-1 == r && EINTR == errno);
return r;
}
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
static void stop_capturing(void);
static void process_image(const void *p, int id)
{
unsigned int len = width * height* 3 / 2 ;
unsigned int yuv_addr[3];
struct v4l2_control control;
struct uc13x_img_zoom zoom;
control.id = UC13X_CTRL_STREAM_ZOOM;
control.value = (int)(&zoom);
zoom.src_index = id;
zoom.dst_index = id;
zoom.imageformat = 0;
zoom.srcwidth = win_w;
zoom.srcheight = win_h;
zoom.dstwidth = width;
zoom.dstheight = height;
xioctl(fd, VIDIOC_S_CTRL, &control);
if(src ==0) {
yuv_addr[0] = (unsigned int) p;
yuv_addr[1] = DL_CMIF_U_OFFSET(yuv_addr[0], width, height);
yuv_addr[2] = DL_CMIF_V_OFFSET(yuv_addr[0], width, height);
//printf("YUV addr is Y %x ,U %x,V %x\n", yuv_addr[0], yuv_addr[1], yuv_addr[2]);
display_video(video_handle,
(char *) yuv_addr[0],
(char *) yuv_addr[1],
(char *) yuv_addr[2]);
}
else{
fputc('*', stdout);
fflush(stdout);
fwrite(p, sizeof(unsigned char), len, fyuv);
fflush(fyuv);
}
}
static int read_frame(void)
{
struct v4l2_buffer buf;
unsigned int i;
switch (io){
case IO_METHOD_MMAP:
///fprintf(stderr,"IO_METHOD_MMAP\n");
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)){
switch (errno){
case -EAGAIN:
return 0;
case -EIO:
/* Could ignore EIO, see spec. */
/* fall through */
default:
errno_exit("VIDIOC_DQBUF");
}
}
assert(buf.index < n_buffers);
process_image(buffers[buf.index].start,buf.index);
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
errno_exit("VIDIOC_QBUF");
break;
}
return 1;
}
static void mainloop(void)
{
enum v4l2_buf_type type;
unsigned int count;
char ch = 0;
struct timeval start, stop;
int diff;
struct v4l2_control control;
struct uc13x_img_zoom zoom;
unsigned int len = width * height* 3 / 2 ;
unsigned int yuv_addr[3];
int frame_count = 0;
camera_cfg_t cfg;
FILE* fout;
fd_set fds;
struct timeval tv;
int r;
while(1){
if(kbhit()){
ch = getchar();
if(ch == 'q'){
printf("Key press 'q',Exit\n");
break;
}else if(ch == 'p'){
printf("Set QVGA\n");
ch = getchar();
//....
}else if(ch == 'l'){
printf("Set VGA\n");
//...
}
}
for (;;){
/*TODO : comment: if the next paragram between #if 1 and #endif does not work, then
* the test pattern will always ended when calling VIDIOC_DQBUF iocontrol, although sometimes
* it can work for several hours. On the ohterwise, if the next paragram between #if 1
* and #endif works, and if the process_image function does not write image data to a
* file, then the test pattern can always being working rightly, but if writing data to a
* file, then it can cause an usr_irq fault, maybe it's caused by the nfs, which needs to
* verify latter. The next paragram before read_frame function is mainly used to call poll
* function*/
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 20;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r){
if (EINTR == errno) {
printf("main loop : EINTR == errno!\n");
continue;
}
errno_exit("select");
}
if (0 == r){
fprintf(stderr, "select timeout\n");
exit(EXIT_FAILURE);
}
if (read_frame()){
frame_count++;
break;
}
/* EAGAIN - continue select loop. */
}
}
cfg.fd = fd;
cfg.videobuf_cnt = 0;
cfg.x = 0;
cfg.y = 0;
cfg.w = SCREEN_WIDTH;
cfg.h = SCREEN_HEIGHT;
cfg.pic_w = 1024;
cfg.pic_h = 768;
camera_zoom_img(fd,1,2,&cfg);
fout = fopen("/mnt/udisk/vga.yuv","wb");
if(fout ==NULL){
printf("can't open log file\n");
return;
}
fwrite(buffers[1].start,1,SCREEN_WIDTH*SCREEN_HEIGHT*3/2, fout);
fclose(fout);
fout = fopen("/mnt/udisk/svga.yuv","wb");
if(fout ==NULL){
printf("can't open log file\n");
return;
}
fwrite(buffers[2].start,1,1024*768*3/2, fout);
fclose(fout);
}
static void dqbuf(void){
struct v4l2_buffer buf;
for (;;){
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 20;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if (-1 == r){
if (EINTR == errno)
continue;
errno_exit("select");
}
if (0 == r){
fprintf(stderr, "select timeout\n");
exit(EXIT_FAILURE);
}
CLEAR(buf);
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
if (-1 == xioctl(fd, VIDIOC_DQBUF, &buf)){
switch (errno){
case -EAGAIN:
continue;
case -EIO:
/* Could ignore EIO, see spec. */
/* fall through */
default:
errno_exit("VIDIOC_DQBUF");
}
}
break;
/* EAGAIN - continue select loop. */
}
}
static void stop_capturing(void)
{
enum v4l2_buf_type type;
fprintf(stderr, "stop_capturing\n");
switch (io){
case IO_METHOD_READ:
/* Nothing to do. */
break;
case IO_METHOD_MMAP: