ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / linux / api_ifile_raw_sdl.c
1 #include <stdlib.h>
2 #include <ctype.h>
3
4 #include <VP_Api/vp_api.h>
5 #include <VP_Api/vp_api_thread_helper.h>
6 #include <VP_Api/vp_api_error.h>
7 #include <VP_Api/vp_api_picture.h>
8 #include <VP_Stages/vp_stages_configs.h>
9 #include <VP_Stages/vp_stages_io_console.h>
10 #include <VP_Stages/vp_stages_o_sdl.h>
11 #include <VP_Stages/vp_stages_io_com.h>
12 #include <VP_Stages/vp_stages_io_file.h>
13 #include <VP_Os/vp_os_print.h>
14 #include <VP_Os/vp_os_malloc.h>
15 #include <VP_Os/vp_os_delay.h>
16
17 #include <MJPEG/mjpeg.h>
18 #include <MJPEG/dct.h>
19
20 #define ACQ_WIDTH  (320)
21 #define ACQ_HEIGHT (240)
22
23 #define NB_STAGES 3
24
25
26 PIPELINE_HANDLE pipeline_handle;
27
28 PROTO_THREAD_ROUTINE(escaper, nomParams);
29 PROTO_THREAD_ROUTINE(app, nomParams);
30
31 BEGIN_THREAD_TABLE
32   THREAD_TABLE_ENTRY(escaper, 20)
33   THREAD_TABLE_ENTRY(app, 20)
34 END_THREAD_TABLE
35
36
37 ///*******************************************************************************************************************///
38
39 typedef struct _buffer_to_picture_config_t
40 {
41   vp_api_picture_t* picture;
42
43 } buffer_to_picture_config_t;
44
45 C_RESULT
46 buffer_to_picture_open(buffer_to_picture_config_t *cfg)
47 {
48   cfg->picture->format        = PIX_FMT_YUV420P;
49
50   cfg->picture->width         = ACQ_WIDTH;
51   cfg->picture->height        = ACQ_HEIGHT;
52   cfg->picture->framerate     = 15;
53
54   cfg->picture->y_line_size   = ACQ_WIDTH;
55   cfg->picture->cb_line_size  = ACQ_WIDTH / 2;
56   cfg->picture->cr_line_size  = ACQ_WIDTH / 2;
57
58   cfg->picture->y_pad         = 0;
59   cfg->picture->c_pad         = 0;
60
61   return (SUCCESS);
62 }
63
64 C_RESULT
65 buffer_to_picture_transform(buffer_to_picture_config_t *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*3)/2;
74     out->buffers      = (int8_t **) cfg->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     cfg->picture->y_buf   = in->buffers[0];
86     cfg->picture->cb_buf  = in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT;
87     cfg->picture->cr_buf  = in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT + ACQ_WIDTH*ACQ_HEIGHT/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(buffer_to_picture_config_t *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 int
113 main(int argc, char **argv)
114 {
115   if(argc != 2)
116   {
117     PRINT("You must specify a filename.\n");
118     return EXIT_FAILURE;
119   }
120
121   START_THREAD(escaper, NO_PARAM);
122   START_THREAD(app, argv);
123
124   JOIN_THREAD(escaper);
125   JOIN_THREAD(app);
126
127   return EXIT_SUCCESS;
128 }
129
130
131 PROTO_THREAD_ROUTINE(app,argv)
132 {
133   vp_api_picture_t picture;
134
135   vp_api_io_pipeline_t    pipeline;
136   vp_api_io_data_t        out;
137   vp_api_io_stage_t       stages[NB_STAGES];
138
139   vp_stages_input_file_config_t     ifc;
140   vp_stages_output_sdl_config_t     osc;
141   buffer_to_picture_config_t        bpc;
142
143   vp_os_memset(&ifc,0,sizeof(vp_stages_input_file_config_t));
144
145   ifc.name            = ((char**)argv)[1];
146   ifc.buffer_size     = (ACQ_WIDTH*ACQ_HEIGHT*3)/2;
147
148   osc.width           = ACQ_WIDTH;
149   osc.height          = ACQ_HEIGHT;
150   osc.bpp             = 16;
151   osc.window_width    = ACQ_WIDTH;
152   osc.window_height   = ACQ_HEIGHT;
153   osc.pic_width       = ACQ_WIDTH;
154   osc.pic_height      = ACQ_HEIGHT;
155   osc.y_size          = ACQ_WIDTH*ACQ_HEIGHT;
156   osc.c_size          = (ACQ_WIDTH*ACQ_HEIGHT) >> 2;
157
158   bpc.picture         = &picture;
159
160   stages[0].type      = VP_API_INPUT_FILE;
161   stages[0].cfg       = (void *)&ifc;
162   stages[0].funcs     = vp_stages_input_file_funcs;
163
164   stages[1].type      = VP_API_FILTER_DECODER;
165   stages[1].cfg       = (void *)&bpc;
166   stages[1].funcs     = buffer_to_picture_funcs;
167
168   stages[2].type      = VP_API_OUTPUT_SDL;
169   stages[2].cfg       = (void *)&osc;
170   stages[2].funcs     = vp_stages_output_sdl_funcs;
171
172   pipeline.nb_stages  = NB_STAGES;
173   pipeline.stages     = &stages[0];
174
175   vp_api_open(&pipeline, &pipeline_handle);
176   out.status = VP_API_STATUS_PROCESSING;
177   while(SUCCEED(vp_api_run(&pipeline, &out)) && (out.status == VP_API_STATUS_PROCESSING || out.status == VP_API_STATUS_STILL_RUNNING))
178   {
179     vp_os_delay( 500 );
180   }
181
182   vp_api_close(&pipeline, &pipeline_handle);
183
184   return EXIT_SUCCESS;
185 }