121f5cf16e778e0632df0821bdc7987178416a67
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / linux / api_serial_raw_sdl.c
1 #include <stdlib.h>
2 #include <ctype.h>
3 #include <stdio.h>
4 #include <termios.h>
5 #include <fcntl.h>
6 #include <errno.h>
7 #include <unistd.h>
8
9 #include <VP_Api/vp_api.h>
10 #include <VP_Api/vp_api_thread_helper.h>
11 #include <VP_Api/vp_api_error.h>
12 #include <VP_Api/vp_api_picture.h>
13 #include <VP_Stages/vp_stages_configs.h>
14 #include <VP_Stages/vp_stages_io_console.h>
15 #include <VP_Stages/vp_stages_o_sdl.h>
16 #include <VP_Stages/vp_stages_io_com.h>
17 #include <VP_Stages/vp_stages_io_file.h>
18 #include <VP_Os/vp_os_print.h>
19 #include <VP_Os/vp_os_malloc.h>
20 #include <VP_Os/vp_os_delay.h>
21
22
23 vp_stages_output_sdl_config_t     osc;
24
25 #define ACQ_WIDTH  (osc.width)
26 #define ACQ_HEIGHT (osc.height)
27
28 #define NB_STAGES 3
29
30
31 PIPELINE_HANDLE pipeline_handle;
32
33
34 // used by buffer_to_picture
35 static vp_api_picture_t picture;
36
37
38 PROTO_THREAD_ROUTINE(escaper,nomParams);
39 PROTO_THREAD_ROUTINE(app,nomParams);
40
41
42 BEGIN_THREAD_TABLE
43   THREAD_TABLE_ENTRY(escaper,20)
44   THREAD_TABLE_ENTRY(app,20)
45 END_THREAD_TABLE
46
47
48 C_RESULT
49 buffer_to_picture_open(int *cfg)
50 {
51   picture.format = PIX_FMT_YUV420P;
52
53   picture.width = ACQ_WIDTH;
54   picture.height = ACQ_HEIGHT;
55   picture.framerate = 15;
56
57   picture.y_line_size = ACQ_WIDTH;
58   picture.cb_line_size = 0; //ACQ_WIDTH/2;
59   picture.cr_line_size = 0; //ACQ_WIDTH/2;
60
61   return (SUCCESS);
62 }
63
64 C_RESULT
65 buffer_to_picture_transform(int *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
66 {
67   vp_os_mutex_lock(&out->lock);
68
69
70   if(out->status == VP_API_STATUS_INIT)
71     {
72       out->numBuffers = 1;
73       out->size = ACQ_WIDTH*ACQ_HEIGHT; //(ACQ_WIDTH*ACQ_HEIGHT*3)/2;
74       out->buffers = (int8_t **)(int8_t *)&picture;
75       out->indexBuffer = 0;
76       out->status = VP_API_STATUS_PROCESSING;
77     }
78
79   if(out->status == VP_API_STATUS_ENDED)
80     {
81     }
82
83   if(out->status == VP_API_STATUS_PROCESSING)
84     {
85       picture.y_buf = in->buffers[in->indexBuffer];
86       picture.cb_buf = in->buffers[in->indexBuffer]+ACQ_WIDTH*ACQ_HEIGHT;
87       picture.cr_buf = in->buffers[in->indexBuffer]+(ACQ_WIDTH*ACQ_HEIGHT*5)/4;
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
104 const vp_api_stage_funcs_t buffer_to_picture_funcs =
105 {
106   (vp_api_stage_handle_msg_t) NULL,
107   (vp_api_stage_open_t)buffer_to_picture_open,
108   (vp_api_stage_transform_t)buffer_to_picture_transform,
109   (vp_api_stage_close_t)buffer_to_picture_close
110 };
111
112
113 int
114 main(int argc, char **argv)
115 {
116   START_THREAD(escaper, NO_PARAM);
117   START_THREAD(app, argv);
118
119   JOIN_THREAD(escaper);
120   JOIN_THREAD(app);
121
122   return EXIT_SUCCESS;
123 }
124
125
126 PROTO_THREAD_ROUTINE(app,argv)
127 {
128   vp_api_io_pipeline_t    pipeline;
129   vp_api_io_data_t        out;
130   vp_api_io_stage_t       stages[NB_STAGES];
131
132   vp_com_t                          com;
133   vp_stages_input_com_config_t      icc;
134   vp_com_serial_config_t            config;
135
136
137   vp_os_memset(&icc,0,sizeof(vp_stages_input_com_config_t));
138   vp_os_memset(&com, 0, sizeof(vp_com_t));
139   vp_stages_fill_default_config(UART1_COM_CONFIG, &config, sizeof(config));
140   vp_stages_fill_default_config(SDL_RAW_QCIF_CONFIG, &osc, sizeof(osc));
141
142   com.type                = VP_COM_SERIAL;
143   icc.com                 = &com;
144   icc.socket.type         = VP_COM_CLIENT;
145   icc.config              = (vp_com_config_t *)&config;
146   icc.buffer_size         = 176*144; //ACQ_WIDTH*ACQ_HEIGHT;
147
148
149   stages[0].type    = VP_API_INPUT_SOCKET;
150   stages[0].cfg     = (void *)&icc;
151   stages[0].funcs   = vp_stages_input_com_funcs;
152
153   stages[1].type    = VP_API_FILTER_DECODER;
154   stages[1].cfg     = (void *)NULL;
155   stages[1].funcs   = buffer_to_picture_funcs;
156
157   stages[2].type    = VP_API_OUTPUT_SDL;
158   stages[2].cfg     = (void *)&osc;
159   stages[2].funcs   = vp_stages_output_sdl_funcs;
160
161   pipeline.nb_stages = NB_STAGES;
162   pipeline.stages = &stages[0];
163
164
165   vp_api_open(&pipeline, &pipeline_handle);
166   out.status = VP_API_STATUS_PROCESSING;
167   while(SUCCEED(vp_api_run(&pipeline, &out)) && (out.status == VP_API_STATUS_PROCESSING || out.status == VP_API_STATUS_STILL_RUNNING))
168     {
169       // no delay here
170     }
171
172   vp_api_close(&pipeline, &pipeline_handle);
173
174   return EXIT_SUCCESS;
175 }