ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / Examples / win32 / 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_file.h>
12 #include <VP_Os/vp_os_print.h>
13 #include <VP_Os/vp_os_malloc.h>
14 #include <VP_Os/vp_os_delay.h>
15
16 #include <MJPEG/mjpeg.h>
17 #include <MJPEG/dct.h>
18
19 #define ACQ_WIDTH  (176*2)
20 #define ACQ_HEIGHT (144*2)
21
22 #define NB_STAGES 3
23
24
25 PIPELINE_HANDLE pipeline_handle;
26
27 PROTO_THREAD_ROUTINE(escaper, nomParams);
28 PROTO_THREAD_ROUTINE(app, nomParams);
29
30 BEGIN_THREAD_TABLE
31   THREAD_TABLE_ENTRY(escaper, 20)
32   THREAD_TABLE_ENTRY(app, 20)
33 END_THREAD_TABLE
34
35
36 ///*******************************************************************************************************************///
37
38 typedef struct _buffer_to_picture_config_t
39 {
40   vp_api_picture_t* picture;
41
42 } buffer_to_picture_config_t;
43
44 C_RESULT
45 buffer_to_picture_open(buffer_to_picture_config_t *cfg)
46 {
47   cfg->picture->format        = PIX_FMT_YUV420P;
48
49   cfg->picture->width         = ACQ_WIDTH;
50   cfg->picture->height        = ACQ_HEIGHT;
51   cfg->picture->framerate     = 15;
52
53   cfg->picture->y_line_size   = ACQ_WIDTH;
54   cfg->picture->cb_line_size  = ACQ_WIDTH / 2;
55   cfg->picture->cr_line_size  = ACQ_WIDTH / 2;
56
57   cfg->picture->y_pad         = 0;
58   cfg->picture->c_pad         = 0;
59
60   return (SUCCESS);
61 }
62
63 C_RESULT
64 buffer_to_picture_transform(buffer_to_picture_config_t *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
65 {
66   vp_os_mutex_lock(&out->lock);
67
68
69   if(out->status == VP_API_STATUS_INIT)
70   {
71     out->numBuffers   = 1;
72     out->size         = (ACQ_WIDTH*ACQ_HEIGHT*3)/2;
73     out->buffers      = (int8_t **) cfg->picture;
74     out->indexBuffer  = 0;
75     out->status       = VP_API_STATUS_PROCESSING;
76   }
77
78   if(out->status == VP_API_STATUS_ENDED)
79   {
80   }
81
82   if(out->status == VP_API_STATUS_PROCESSING)
83   {
84     cfg->picture->y_buf   = in->buffers[0];
85     cfg->picture->cb_buf  = in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT;
86     cfg->picture->cr_buf  = in->buffers[0] + ACQ_WIDTH*ACQ_HEIGHT + ACQ_WIDTH*ACQ_HEIGHT/4;
87   }
88
89   out->status = in->status;
90
91   vp_os_mutex_unlock(&out->lock);
92
93   return (SUCCESS);
94 }
95
96 C_RESULT
97 buffer_to_picture_close(buffer_to_picture_config_t *cfg)
98 {
99   return (SUCCESS);
100 }
101
102 const vp_api_stage_funcs_t buffer_to_picture_funcs =
103 {
104   NULL,
105   (vp_api_stage_open_t)buffer_to_picture_open,
106   (vp_api_stage_transform_t)buffer_to_picture_transform,
107   (vp_api_stage_close_t)buffer_to_picture_close
108 };
109
110
111 int
112 main(int argc, char **argv)
113 {
114   if(argc != 2)
115   {
116     PRINT("You must specify a filename.\n");
117     return EXIT_FAILURE;
118   }
119
120   START_THREAD(escaper, NO_PARAM);
121   START_THREAD(app, argv);
122
123   JOIN_THREAD(escaper);
124   JOIN_THREAD(app);
125
126   return EXIT_SUCCESS;
127 }
128
129
130 PROTO_THREAD_ROUTINE(app,argv)
131 {
132   vp_api_picture_t picture;
133
134   vp_api_io_pipeline_t    pipeline;
135   vp_api_io_data_t        out;
136   vp_api_io_stage_t       stages[NB_STAGES];
137
138   vp_stages_input_file_config_t     ifc;
139   vp_stages_output_sdl_config_t     osc;
140   buffer_to_picture_config_t        bpc;
141
142   vp_os_memset(&ifc,0,sizeof(vp_stages_input_file_config_t));
143
144   ifc.name            = ((char**)argv)[1];
145   ifc.buffer_size     = (ACQ_WIDTH*ACQ_HEIGHT*3)/2;
146
147   osc.width           = ACQ_WIDTH;
148   osc.height          = ACQ_HEIGHT;
149   osc.bpp             = 16;
150   osc.window_width    = ACQ_WIDTH;
151   osc.window_height   = ACQ_HEIGHT;
152   osc.pic_width       = ACQ_WIDTH;
153   osc.pic_height      = ACQ_HEIGHT;
154   osc.y_size          = ACQ_WIDTH*ACQ_HEIGHT;
155   osc.c_size          = (ACQ_WIDTH*ACQ_HEIGHT) >> 2;
156
157   bpc.picture         = &picture;
158
159   stages[0].type      = VP_API_INPUT_FILE;
160   stages[0].cfg       = (void *)&ifc;
161   stages[0].funcs     = vp_stages_input_file_funcs;
162
163   stages[1].type      = VP_API_FILTER_DECODER;
164   stages[1].cfg       = (void *)&bpc;
165   stages[1].funcs     = buffer_to_picture_funcs;
166
167   stages[2].type      = VP_API_OUTPUT_SDL;
168   stages[2].cfg       = (void *)&osc;
169   stages[2].funcs     = vp_stages_output_sdl_funcs;
170
171   pipeline.nb_stages  = NB_STAGES;
172   pipeline.stages     = &stages[0];
173
174   vp_api_open(&pipeline, &pipeline_handle);
175   out.status = VP_API_STATUS_PROCESSING;
176   while(SUCCEED(vp_api_run(&pipeline, &out)) && (out.status == VP_API_STATUS_PROCESSING || out.status == VP_API_STATUS_STILL_RUNNING))
177   {
178     vp_os_delay( 100 );
179   }
180
181   vp_api_close(&pipeline, &pipeline_handle);
182
183   return EXIT_SUCCESS;
184 }