5 * Created by Frédéric D'HAEYER on 22/02/10.
6 * Copyright 2010 Parrot SA. All rights reserved.
9 #include <ardrone_tool/Video/video_stage.h>
13 PIPELINE_HANDLE pipeline_handle;
14 static bool_t video_stage_in_pause = TRUE;
15 static vp_os_cond_t video_stage_condition;
16 static vp_os_mutex_t video_stage_mutex;
17 static video_com_config_t icc;
18 static video_stage_config_t video_stage_config;
20 const vp_api_stage_funcs_t video_stage_funcs =
22 (vp_api_stage_handle_msg_t) NULL,
23 (vp_api_stage_open_t) video_stage_open,
24 (vp_api_stage_transform_t) video_stage_transform,
25 (vp_api_stage_close_t) video_stage_close
28 C_RESULT video_stage_open(vlib_stage_decoding_config_t *cfg)
30 vp_os_mutex_init( &video_stage_config.mutex );
32 vp_os_mutex_lock( &video_stage_config.mutex );
33 video_stage_config.data = vp_os_malloc(512 * 512 * 4);
34 vp_os_memset(video_stage_config.data, 0x0, 512 * 512 * 4);
35 vp_os_mutex_unlock( &video_stage_config.mutex );
40 C_RESULT video_stage_transform(vlib_stage_decoding_config_t *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
42 vp_os_mutex_lock( &out->lock );
44 if(out->status == VP_API_STATUS_INIT)
46 out->status = VP_API_STATUS_PROCESSING;
49 if( in->status == VP_API_STATUS_ENDED )
51 out->status = in->status;
54 if(out->status == VP_API_STATUS_PROCESSING )
56 vp_os_mutex_lock( &video_stage_config.mutex );
58 if(cfg->num_picture_decoded > video_stage_config.num_picture_decoded)
60 video_stage_config.num_picture_decoded = cfg->num_picture_decoded;
61 video_stage_config.num_frame = cfg->controller.num_frames;
62 video_stage_config.bytesPerPixel = 2;
63 video_stage_config.widthImage = cfg->controller.width;
64 video_stage_config.heightImage = cfg->controller.height;
66 if (video_stage_config.data != NULL)
68 vp_os_memcpy(video_stage_config.data, cfg->picture->y_buf, cfg->picture->width * cfg->picture->height );
71 out->numBuffers = in->numBuffers;
72 out->indexBuffer = in->indexBuffer;
73 out->buffers = in->buffers;
76 vp_os_mutex_unlock( &video_stage_config.mutex );
79 vp_os_mutex_unlock( &out->lock );
84 C_RESULT video_stage_close(vlib_stage_decoding_config_t *cfg)
86 vp_os_free(video_stage_config.data);
91 video_stage_config_t* video_stage_get(void)
93 return &video_stage_config;
96 void video_stage_init(void)
98 vp_os_mutex_init(&video_stage_mutex);
99 vp_os_cond_init(&video_stage_condition, &video_stage_mutex);
102 void video_stage_suspend_thread(void)
104 vp_os_mutex_lock(&video_stage_mutex);
105 video_stage_in_pause = TRUE;
106 vp_os_mutex_unlock(&video_stage_mutex);
109 void video_stage_resume_thread(void)
111 vp_os_mutex_lock(&video_stage_mutex);
112 vp_os_cond_signal(&video_stage_condition);
113 video_stage_in_pause = FALSE;
114 vp_os_mutex_unlock(&video_stage_mutex);
117 DEFINE_THREAD_ROUTINE(video_stage, data)
121 vp_api_io_pipeline_t pipeline;
122 vp_api_io_data_t out;
123 vp_api_io_stage_t stages[NB_STAGES];
125 vp_api_picture_t picture;
127 vlib_stage_decoding_config_t vec;
129 vp_os_memset(&icc, 0, sizeof( icc ));
130 vp_os_memset(&vec, 0, sizeof( vec ));
131 vp_os_memset(&picture, 0, sizeof( picture ));
133 //#ifdef RECORD_VIDEO
134 // video_stage_recorder_config_t vrc;
137 /// Picture configuration
138 picture.format = PIX_FMT_RGB565;
141 picture.height = 512;
142 picture.framerate = 15;
144 picture.y_buf = vp_os_malloc( picture.width * picture.height * 2);
145 picture.cr_buf = NULL;
146 picture.cb_buf = NULL;
148 picture.y_line_size = picture.width * 2;
149 picture.cb_line_size = 0;
150 picture.cr_line_size = 0;
152 icc.com = COM_VIDEO();
153 icc.buffer_size = 100000;
154 icc.protocol = VP_COM_UDP;
155 COM_CONFIG_SOCKET_VIDEO(&icc.socket, VP_COM_CLIENT, VIDEO_PORT, wifi_ardrone_ip);
159 vec.picture = &picture;
160 vec.luma_only = FALSE;
161 vec.block_mode_enable = TRUE;
163 pipeline.nb_stages = 0;
165 stages[pipeline.nb_stages].type = VP_API_INPUT_SOCKET;
166 stages[pipeline.nb_stages].cfg = (void *)&icc;
167 stages[pipeline.nb_stages].funcs = video_com_funcs;
168 pipeline.nb_stages++;
170 stages[pipeline.nb_stages].type = VP_API_FILTER_DECODER;
171 stages[pipeline.nb_stages].cfg = (void*)&vec;
172 stages[pipeline.nb_stages].funcs = vlib_decoding_funcs;
173 pipeline.nb_stages++;
177 stages[pipeline.nb_stages].type = VP_API_FILTER_DECODER;
178 stages[pipeline.nb_stages].cfg = (void*)&vrc;
179 stages[pipeline.nb_stages].funcs = video_recorder_funcs;
180 pipeline.nb_stages++;
183 stages[pipeline.nb_stages].type = VP_API_OUTPUT_LCD;
184 stages[pipeline.nb_stages].cfg = (void*)&vec;
185 stages[pipeline.nb_stages].funcs = video_stage_funcs;
186 pipeline.nb_stages++;
188 pipeline.stages = &stages[0];
190 if( !ardrone_tool_exit() )
192 PRINT("\nvideo stage thread initialisation\n\n");
194 res = vp_api_open(&pipeline, &pipeline_handle);
199 out.status = VP_API_STATUS_PROCESSING;
204 dest.pipeline = pipeline_handle;
205 vp_api_post_message( dest, PIPELINE_MSG_START, NULL, (void*)NULL);
209 while( !ardrone_tool_exit() && (loop == SUCCESS) )
211 if(video_stage_in_pause)
213 vp_os_mutex_lock(&video_stage_mutex);
214 icc.num_retries = VIDEO_MAX_RETRIES;
215 vp_os_cond_wait(&video_stage_condition);
216 vp_os_mutex_unlock(&video_stage_mutex);
219 if( SUCCEED(vp_api_run(&pipeline, &out)) ) {
220 if( (out.status == VP_API_STATUS_PROCESSING || out.status == VP_API_STATUS_STILL_RUNNING) ) {
224 else loop = -1; // Finish this thread
227 vp_api_close(&pipeline, &pipeline_handle);
231 PRINT(" video stage thread ended\n\n");
233 return (THREAD_RET)0;
236 uint32_t video_stage_get_num_retries(void)
238 return icc.num_retries;