ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / elinux / api_v4l_raw_ethernet.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_i_v4l.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 <VLIB/Stages/vlib_stage_encode.h>
11
12
13 #undef BLOCK_MODE
14 #undef ACQ_WIDTH
15 #undef ACQ_HEIGHT
16
17 #define ACQ_WIDTH 320
18 #define ACQ_HEIGHT 240
19
20 #define COM_VIDEO()             wired_com()
21 #define COM_CONFIG_VIDEO()      wired_config()
22 #define COM_CONNECTION_VIDEO()  wired_connection()
23
24
25 PROTO_THREAD_ROUTINE(app,nomParams);
26
27 BEGIN_THREAD_TABLE
28   THREAD_TABLE_ENTRY(app,20)
29 END_THREAD_TABLE
30
31
32 #define NB_STAGES_MAX 10
33
34
35 static vp_stages_input_v4l_config_t  ivc;
36 static vp_stages_output_com_config_t occ;
37
38 static PIPELINE_HANDLE pipeline_handle;
39
40 static vp_api_picture_t picture;
41
42
43 C_RESULT
44 buffer_to_picture_open(int *cfg)
45 {
46   picture.format = PIX_FMT_YUV420P;
47
48   picture.width = ACQ_WIDTH;
49   picture.height = ACQ_HEIGHT;
50   picture.framerate = 15;
51
52   picture.y_line_size = ACQ_WIDTH;
53   picture.cb_line_size = ACQ_WIDTH/2;
54   picture.cr_line_size = ACQ_WIDTH/2;
55
56   return (SUCCESS);
57 }
58
59 C_RESULT
60 buffer_to_picture_transform(int *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
61 {
62   static int32_t blockline;
63
64   vp_os_mutex_lock(&out->lock);
65
66   if(out->status == VP_API_STATUS_INIT)
67     {
68       blockline = 0;
69       out->numBuffers = 1;
70       out->size = ACQ_WIDTH*ACQ_HEIGHT*3/2;
71       out->buffers = (int8_t **)(int8_t *)&picture;
72       out->indexBuffer = 0;
73       out->status = VP_API_STATUS_PROCESSING;
74     }
75
76   out->status = in->status;
77
78   if(out->status == VP_API_STATUS_ENDED)
79     {
80     }
81
82   if(out->status == VP_API_STATUS_PROCESSING)
83     {
84       picture.y_buf = (uint8_t*)in->buffers[0];
85       picture.cb_buf = (uint8_t*)in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT; 
86       picture.cr_buf = (uint8_t*)in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT*5/4; 
87       picture.complete = 1;
88     }
89   
90   out->status = in->status;
91   
92   vp_os_mutex_unlock(&out->lock);
93   
94   return (SUCCESS);
95 }
96
97 C_RESULT
98 buffer_to_picture_close(int *cfg)
99 {
100   return (SUCCESS);
101 }
102
103 const vp_api_stage_funcs_t buffer_to_picture_funcs =
104 {
105   NULL,
106   (vp_api_stage_open_t)buffer_to_picture_open,
107   (vp_api_stage_transform_t)buffer_to_picture_transform,
108   (vp_api_stage_close_t)buffer_to_picture_close
109 };
110
111
112 vp_com_t* wired_com(void)
113 {
114   static vp_com_t com = {
115     VP_COM_WIRED,
116     FALSE,
117     0,
118     { { 0 } },
119     NULL,
120     NULL,
121     0,
122     NULL,
123     NULL,
124     NULL,
125     NULL,
126     NULL,
127     NULL,
128     NULL,
129     NULL,
130     NULL,
131     NULL,
132     NULL
133   };
134
135   return &com;
136 }
137
138 vp_com_config_t* wired_config(void)
139 {
140   static vp_com_wired_config_t config = {
141     "eth0",
142     "172.20.22.253",
143     "255.255.255.0",
144     "172.20.22.255"
145   };
146
147   return (vp_com_config_t*) &config;
148 }
149
150 vp_com_connection_t* wired_connection(void)
151 {
152   static vp_com_wired_connection_t connection = {
153     0
154   };
155
156   return (vp_com_connection_t*) (void*) &connection;
157 }
158
159
160 int main(int argc, char **argv)
161 {
162   START_THREAD(app, argv);
163   JOIN_THREAD(app);
164
165   return EXIT_SUCCESS;
166 }
167
168
169 PROTO_THREAD_ROUTINE(app,argv)
170 {
171   vp_api_io_pipeline_t pipeline;
172   vp_api_io_data_t out;
173   vp_api_io_stage_t stages[NB_STAGES_MAX];
174
175   uint16_t time1,time2;
176   uint16_t i;
177
178   time1 = clock();
179
180   vp_os_memset(&ivc,0,sizeof(ivc));
181   //vp_os_memset(&vec,0,sizeof(vec));
182   vp_os_memset(&occ,0,sizeof(occ));
183
184   ivc.device = "/tmp/video0";
185   ivc.width = ACQ_WIDTH;
186   ivc.height = ACQ_HEIGHT;
187   ivc.vp_api_picture = &picture;
188
189 /*   vec.width                               = ACQ_WIDTH; */
190 /*   vec.height                              = ACQ_HEIGHT; */
191 /*   vec.block_mode_enable                   = FALSE; */
192 /*   vec.picture                             = &picture; */
193
194   occ.com                           = COM_VIDEO();
195   occ.config                        = COM_CONFIG_VIDEO();
196   occ.connection                    = COM_CONNECTION_VIDEO();
197   occ.socket.type                   = VP_COM_SERVER;
198   occ.socket.protocol               = VP_COM_TCP;
199   occ.socket.port                   = 5555;
200   occ.buffer_size                   = 320*240*3/2;
201
202   pipeline.nb_stages = 0;
203
204   stages[pipeline.nb_stages].type    = VP_API_INPUT_FILE;
205   stages[pipeline.nb_stages].cfg     = (void *)&ivc;
206   stages[pipeline.nb_stages++].funcs = vp_stages_input_v4l_funcs;
207   
208 /*   stages[pipeline.nb_stages].type    = VP_API_FILTER_ENCODER; */
209 /*   stages[pipeline.nb_stages].cfg     = (void *)NULL; */
210 /*   stages[pipeline.nb_stages++].funcs = buffer_to_picture_funcs; */
211   
212 /*   stages[pipeline.nb_stages].type    = VP_API_FILTER_ENCODER; */
213 /*   stages[pipeline.nb_stages].cfg     = (void*)&vec; */
214 /*   stages[pipeline.nb_stages++].funcs = vlib_encoding_funcs; */
215
216   stages[pipeline.nb_stages].type    = VP_API_OUTPUT_SOCKET;
217   stages[pipeline.nb_stages].cfg     = (void *)&occ;
218   stages[pipeline.nb_stages++].funcs = vp_stages_output_com_funcs;
219
220   pipeline.stages = &stages[0];
221
222   vp_api_open(&pipeline, &pipeline_handle);
223  
224   out.status = VP_API_STATUS_PROCESSING;
225
226   /////////////////////////////////////////////////////////////////////////////////////////
227   i = 0;
228   while(SUCCEED(vp_api_run(&pipeline, &out)) && (out.status == VP_API_STATUS_PROCESSING))
229     {
230       i++;
231       PRINT("image %d \n",i);
232       //vp_os_delay(50);
233     }
234   /////////////////////////////////////////////////////////////////////////////////////////
235
236   time2 = clock();
237   printf("temps ecoule : %d \n",time2-time1);
238     
239   vp_api_close(&pipeline, &pipeline_handle);
240
241   return EXIT_SUCCESS;
242 }
243