ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / elinux / v4l_stage.c
1 #include <VP_Api/vp_api.h>
2 #include <VP_Api/vp_api_error.h>
3 #include <VP_Stages/vp_stages_configs.h>
4 #include <VP_Stages/vp_stages_V4L2_i_camif.h>
5 #include <VP_Stages/vp_stages_io_com.h>
6 #include <VP_Os/vp_os_print.h>
7 #include <VP_Os/vp_os_malloc.h>
8 #include <VP_Os/vp_os_delay.h>
9 #include <VP_Api/vp_api_thread_helper.h>
10 #include <VP_Stages/vp_stages_io_file.h>
11 #include <VLIB/Stages/vlib_stage_encode.h>
12 #include <VP_Stages/blockline_to_buffer_stage.h>
13
14
15 #include "Video/ov7720.h"
16 #include "Video/cresyn.h"
17
18 #include <sys/time.h>
19 #include <unistd.h>
20
21  
22 //#define ACQ_WIDTH 176
23 //#define ACQ_HEIGHT 144
24
25 #define ACQ_WIDTH 640
26 #define ACQ_HEIGHT 480
27
28 #define CRESYN_CAM
29 //#define OV_CAM
30 //#define BLOCK_MODE
31 //#define BUFFER
32 //#define ENCODE 
33 //#define FILE_OUTPUT
34 //#define ETHERNET_OUTPUT
35
36
37
38 #define NB_STAGES_MAX 10
39
40
41 static vp_stages_input_camif_config_t  ivc;
42 static vlib_stage_encoding_config_t  vec;
43 static vp_stages_output_file_config_t  ofc;
44 static vp_stages_output_com_config_t   occ;
45 static blockline_to_buffer_config_t bbc;
46
47 static PIPELINE_HANDLE pipeline_handle;
48
49
50 C_RESULT
51 buffer_to_picture_close(int *cfg)
52 {
53   return (SUCCESS);
54 }
55
56 const vp_api_stage_funcs_t V4L2_funcs =
57 {
58   (vp_api_stage_handle_msg_t)vp_stages_input_camif_stage_handle_msg,
59   (vp_api_stage_open_t)vp_stages_input_camif_stage_open,
60   (vp_api_stage_transform_t)vp_stages_input_camif_stage_transform,
61   (vp_api_stage_close_t)vp_stages_input_camif_stage_close
62 };
63
64 //// COM
65 vp_com_t* wired_com(void)
66 {
67   static vp_com_t com = {
68     VP_COM_WIRED,
69     FALSE,
70     0,
71     { { 0 } },
72     NULL,
73     NULL,
74     0,
75     NULL,
76     NULL,
77     NULL,
78     NULL,
79     NULL,
80     NULL,
81     NULL,
82     NULL,
83     NULL,
84     NULL,
85     NULL
86   };
87
88   return &com;
89 }
90
91 vp_com_config_t* wired_config(void)
92 {
93   static vp_com_wired_config_t config = {
94     "eth0",
95     "172.20.2.190",
96     "255.255.255.0",
97     "172.20.2.255"
98   };
99   return (vp_com_config_t*) &config;
100 }
101
102 vp_com_connection_t* wired_connection(void)
103 {
104   static vp_com_wired_connection_t connection = {
105     0
106   };
107
108   return (vp_com_connection_t*) (void*) &connection;
109 }
110
111
112
113
114 int main(int argc, char **argv)
115 {
116   vp_api_picture_t  video_picture;
117   vp_api_picture_t  buffer_blockline;
118 #ifdef BLOCK_MODE
119   vp_api_picture_t  video_blockline;
120 #endif
121   vp_api_io_pipeline_t pipeline;
122   vp_api_io_data_t out;
123   vp_api_io_stage_t stages[NB_STAGES_MAX];
124
125   vp_os_memset(&ivc,0,sizeof(ivc));
126   vp_os_memset(&ofc,0,sizeof(ofc));
127   vp_os_memset(&occ,0,sizeof(occ));
128
129
130   // CAMIF config
131   ivc.camera = "/dev/video1";
132   ivc.i2c = "/dev/i2c-0";
133 #ifdef OV_CAM
134   ivc.camera_configure = &camera_configure_ov77xx;
135 #else
136 #ifdef CRESYN_CAM
137   ivc.camera_configure = &camera_configure_cresyn;
138 #else  
139 #error no cam selected
140 #endif
141 #endif
142
143   ivc.resolution = VGA;
144   ivc.nb_buffers = 8;
145   ivc.format                              = V4L2_PIX_FMT_YUV420;
146   ivc.y_pad                               = 0;
147   ivc.c_pad                               = 0;
148   ivc.offset_delta                        = 0;
149 #ifdef BLOCK_MODE
150   ivc.vp_api_blockline                    = &video_blockline;
151 #endif
152   ivc.vp_api_picture                      = &video_picture;
153   ivc.use_chrominances                    = 1;
154   ivc.framerate                           = 15;
155
156   // BUFFER
157 #ifdef BLOCK_MODE
158   bbc.picture                    = &video_blockline;
159 #endif
160
161   // ENCODER
162   vec.width                               = 320;
163   vec.height                              = 240;
164 #ifdef BLOCK_MODE
165   vec.block_mode_enable                   = TRUE;
166 #else
167   vec.block_mode_enable                   = FALSE;
168 #endif
169   vec.picture                             = &video_picture;
170  
171   // OUTPUT FILE config
172   ofc.name = "/tmp/out.yuv";
173   
174   // COM CONFIG
175   occ.com                            = wired_com();
176   occ.config                         = wired_config();
177   occ.connection                     = wired_connection();
178   occ.socket.type                    = VP_COM_SERVER;
179   occ.socket.protocol                = VP_COM_TCP;
180   occ.socket.port                    = 5555;
181   occ.buffer_size                    = 640*480*3/2;
182
183   // build pipeline
184   pipeline.nb_stages = 0;
185
186   stages[pipeline.nb_stages].type    = VP_API_INPUT_CAMIF;
187   stages[pipeline.nb_stages].cfg     = (void *)&ivc;
188   stages[pipeline.nb_stages++].funcs = V4L2_funcs;
189
190 #ifdef BLOCK_MODE
191 #ifdef BUFFER
192   stages[pipeline.nb_stages].type    = VP_API_INPUT_CAMIF;
193   stages[pipeline.nb_stages].cfg     = (void *)&bbc;
194   stages[pipeline.nb_stages++].funcs =  blockline_to_buffer_funcs;
195 #endif
196 #endif
197
198 #ifdef ENCODE 
199   stages[pipeline.nb_stages].type    = VP_API_FILTER_ENCODER;
200   stages[pipeline.nb_stages].cfg     = (void*)&vec;
201   stages[pipeline.nb_stages++].funcs = vlib_encoding_funcs;
202 #endif
203
204 #ifdef FILE_OUTPUT
205   stages[pipeline.nb_stages].type    = VP_API_OUTPUT_FILE;
206   stages[pipeline.nb_stages].cfg     = (void *)&ofc;
207   stages[pipeline.nb_stages++].funcs = vp_stages_output_file_funcs;
208 #endif
209
210 #ifdef ETHERNET_OUTPUT
211   stages[pipeline.nb_stages].type    = VP_API_OUTPUT_SOCKET;
212   stages[pipeline.nb_stages].cfg     = (void *)&occ;
213   stages[pipeline.nb_stages++].funcs = vp_stages_output_com_funcs;
214 #endif
215
216   pipeline.stages = &stages[0];
217
218   // launch pipelines
219   vp_api_open(&pipeline, &pipeline_handle);
220   
221   out.status = VP_API_STATUS_PROCESSING;
222
223   /////////////////////////////////////////////////////////////////////////////////////////
224   
225   uint16_t i = 0;
226
227   struct timeval time1,time2;
228
229   printf("Pipeline launched....\n");
230   gettimeofday(&time1,NULL);
231   while(SUCCEED(vp_api_run(&pipeline, &out)) && (out.status == VP_API_STATUS_PROCESSING))
232     {
233       i++;
234       //printf("image %d \n",i);
235       if (i>50)
236         break;
237       //vp_os_delay(20);
238     }   
239   /////////////////////////////////////////////////////////////////////////////////////////
240  
241   gettimeofday(&time2,NULL);
242   int seconde = time2.tv_sec-time1.tv_sec;
243   int ms = time2.tv_usec-time1.tv_usec;
244   if (ms<0)
245   {
246      seconde--;
247      ms += 1000000;
248   }
249   ms = ms/1000;
250   float fps = ((float)i*1000)/(float)(seconde*1000+ms);
251   printf("temps ecoule : %d.%d / nombre image : %d average fps : %f\n",seconde,ms,i,fps);
252   vp_api_close(&pipeline, &pipeline_handle);
253
254   return EXIT_SUCCESS;
255 }
256