1 #include <VP_Os/vp_os_malloc.h>
2 #include <VP_Os/vp_os_print.h>
5 #include <ardrone_tool/Video/buffer_to_picture_stage.h>
7 static int32_t copy_input_to_buffer( uint8_t* buffer, int32_t input_size, int32_t max_size, buffer_to_picture_config_t *cfg )
11 size_to_copy = input_size;
12 if( size_to_copy > max_size )
13 size_to_copy = max_size;
14 vp_os_memcpy( buffer, cfg->input_ptr, size_to_copy );
16 cfg->cumulated_size += size_to_copy;
17 cfg->input_ptr += size_to_copy;
22 C_RESULT buffer_to_picture_open(buffer_to_picture_config_t *cfg)
24 cfg->num_picture_decoded = 0;
29 C_RESULT buffer_to_picture_transform(buffer_to_picture_config_t *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
31 vp_os_mutex_lock(&out->lock);
33 if(out->status == VP_API_STATUS_INIT)
36 out->buffers = (int8_t**)(int8_t*) cfg->picture;
40 out->status = VP_API_STATUS_PROCESSING;
42 cfg->y_buf_ptr = cfg->picture->y_buf;
44 cfg->cr_buf_ptr = cfg->picture->cr_buf;
45 cfg->cb_buf_ptr = cfg->picture->cb_buf;
47 cfg->cumulated_size = 0;
48 cfg->input_ptr = NULL;
51 if( in->status == VP_API_STATUS_ENDED )
52 out->status = in->status;
54 if( out->status == VP_API_STATUS_PROCESSING )
55 cfg->input_ptr = (uint8_t*)in->buffers[in->indexBuffer];
57 if(out->status == VP_API_STATUS_PROCESSING || out->status == VP_API_STATUS_STILL_RUNNING)
59 int32_t copied_size, y_size, c_size = 0;
60 // If out->size == 1 it means picture is ready
62 out->status = VP_API_STATUS_PROCESSING;
64 y_size = cfg->y_blockline_size;
66 c_size = cfg->y_blockline_size / 4;
69 while(in->size > 0 && cfg->y_current_size != cfg->y_buffer_size)
71 if( in->size > 0 && cfg->cumulated_size < y_size )
73 copied_size = copy_input_to_buffer( cfg->y_buf_ptr, in->size, y_size - cfg->cumulated_size, cfg );
75 cfg->y_buf_ptr += copied_size;
76 in->size -= copied_size;
80 if( in->size > 0 && cfg->cumulated_size >= y_size && cfg->cumulated_size < y_size + c_size )
82 copied_size = copy_input_to_buffer( cfg->cb_buf_ptr, in->size, y_size + c_size - cfg->cumulated_size, cfg );
84 cfg->cb_buf_ptr += copied_size;
85 in->size -= copied_size;
88 if( in->size > 0 && cfg->cumulated_size >= y_size + c_size && cfg->cumulated_size < y_size + 2*c_size )
90 copied_size = copy_input_to_buffer( cfg->cr_buf_ptr, in->size, y_size + 2*c_size - cfg->cumulated_size, cfg );
92 cfg->cr_buf_ptr += copied_size;
93 in->size -= copied_size;
97 if( cfg->cumulated_size == y_size + 2*c_size )
99 cfg->cumulated_size = 0;
100 cfg->y_current_size += cfg->y_blockline_size;
104 // All buffers are full but there's still data
106 out->status = VP_API_STATUS_STILL_RUNNING;
108 if( cfg->y_current_size == cfg->y_buffer_size )
110 // we got one picture (handle case 1)
113 cfg->num_picture_decoded++;
115 DEBUG_PRINT_SDK( "%d picture received\n", (int)cfg->num_picture_decoded );
117 cfg->y_current_size = 0;
118 cfg->y_buf_ptr = cfg->picture->y_buf;
120 cfg->cr_buf_ptr = cfg->picture->cr_buf;
121 cfg->cb_buf_ptr = cfg->picture->cb_buf;
126 vp_os_mutex_unlock(&out->lock);
131 C_RESULT buffer_to_picture_close(buffer_to_picture_config_t *cfg)
136 const vp_api_stage_funcs_t buffer_to_picture_funcs =
139 (vp_api_stage_open_t)buffer_to_picture_open,
140 (vp_api_stage_transform_t)buffer_to_picture_transform,
141 (vp_api_stage_close_t)buffer_to_picture_close