0d11bf35251ca3bd12fbca0b5b4f1992611f83c5
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VLIB / Stages / vlib_stage_decode.c
1 #include <VP_Os/vp_os_malloc.h>
2 #include <VP_Os/vp_os_print.h>
3 #include <VLIB/Stages/vlib_stage_decode.h>
4
5
6 #define FRAME_MODE_BUFFER_SIZE 256
7
8 static video_stream_t stream;
9
10 const vp_api_stage_funcs_t vlib_decoding_funcs = {
11   (vp_api_stage_handle_msg_t) NULL,
12   (vp_api_stage_open_t) vlib_stage_decoding_open,
13   (vp_api_stage_transform_t) vlib_stage_decoding_transform,
14   (vp_api_stage_close_t) vlib_stage_decoding_close
15 };
16
17 C_RESULT vlib_stage_decoding_open(vlib_stage_decoding_config_t *cfg)
18 {
19   // init video decoder with NULL_CODEC
20   video_codec_open( &cfg->controller, NULL_CODEC );
21
22   if(cfg->block_mode_enable)
23   {
24     vp_os_free( cfg->controller.in_stream.bytes );
25   }
26   else
27   {
28     stream.bytes  = (uint32_t*)vp_os_malloc(FRAME_MODE_BUFFER_SIZE*sizeof(uint32_t));
29     stream.index  = 0;
30     stream.used   = 0;
31     stream.size   = FRAME_MODE_BUFFER_SIZE*sizeof(uint32_t);
32   }
33
34   cfg->num_picture_decoded = 0;
35
36   return C_OK;
37 }
38
39 C_RESULT vlib_stage_decoding_transform(vlib_stage_decoding_config_t *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
40 {
41   bool_t got_image;
42
43   vp_os_mutex_lock( &out->lock );
44
45   if(out->status == VP_API_STATUS_INIT)
46   {
47     out->numBuffers   = 1;
48     out->buffers      = (int8_t**)(int8_t*)cfg->picture;
49     out->indexBuffer  = 0;
50     out->lineSize     = 0;
51
52     out->status = VP_API_STATUS_PROCESSING;
53   }
54
55   if( in->status == VP_API_STATUS_ENDED ) {
56     out->status = in->status;
57   }
58
59   if(out->status == VP_API_STATUS_PROCESSING )
60   {
61     // If out->size == 1 it means picture is ready
62     out->size = 0;
63
64     cfg->picture->vision_complete = 0;
65     cfg->picture->complete        = 0;
66     cfg->picture->blockline       = 0;
67
68     got_image = FALSE;
69
70     if(cfg->block_mode_enable)
71     {
72       cfg->controller.in_stream.bytes   = (uint32_t*)in->buffers[0];
73       cfg->controller.in_stream.used    = in->size;
74       cfg->controller.in_stream.size    = in->size;
75       cfg->controller.in_stream.index   = 0;
76       cfg->controller.in_stream.length  = 32;
77       cfg->controller.in_stream.code    = 0;
78
79               video_decode_blockline( &cfg->controller, cfg->picture, &got_image );
80     }
81     else
82     {
83       stream.index  = 0;
84       stream.used   = in->size;
85       vp_os_memcpy(&stream.bytes[0], (uint32_t*)in->buffers[0], in->size);
86
87       video_decode_picture( &cfg->controller, cfg->picture, &stream, &got_image );
88     }
89
90           if( got_image )
91     {
92       // we got one picture
93       out->size = 1;
94       cfg->picture->complete        = 1;
95       cfg->num_picture_decoded++;
96
97       if( cfg->luma_only )
98       {
99         int32_t i;
100         for(i = 0; i < (int32_t)cfg->picture->width * (int32_t)cfg->picture->height / 4; i++ )
101         {
102           cfg->picture->cr_buf[i] = 0x80;
103           cfg->picture->cb_buf[i] = 0x80;
104         }
105       }
106     }
107   }
108
109   vp_os_mutex_unlock( &out->lock );
110
111   return C_OK;
112 }
113
114 C_RESULT vlib_stage_decoding_close(vlib_stage_decoding_config_t *cfg)
115 {
116   if(!cfg->block_mode_enable)
117     vp_os_free(stream.bytes);
118   return video_codec_close( &cfg->controller );
119 }