ArDrone SDK 1.8 added
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VP_SDK / VP_Stages / vp_stages_io_file.c
1 /**
2  *  \brief    VP Stages. File stage declaration
3  *  \author   Sylvain Gaeremynck <sylvain.gaeremynck@parrot.fr>
4  *  \author   Aurelien Morelle <aurelien.morelle@parrot.fr>
5  *  \author   Thomas Landais <thomas.landais@parrot.fr>
6  *  \version  2.0
7  *  \date     first release 16/03/2007
8  *  \date     modification  19/03/2007
9  */
10
11 ///////////////////////////////////////////////
12 // INCLUDES
13
14 #include <VP_Stages/vp_stages_io_file.h>
15 #include <VP_Api/vp_api_error.h>
16 #include <VP_Os/vp_os_assert.h>
17 #include <VP_Os/vp_os_print.h>
18 #include <VP_Os/vp_os_delay.h>
19 #include <VP_Os/vp_os_malloc.h>
20
21 C_RESULT
22 vp_stages_input_file_stage_open(vp_stages_input_file_config_t *cfg)
23 {
24   cfg->f = fopen(cfg->name, "rb");
25
26   if(cfg->f == NULL)
27   {
28     PRINT("Missing input file\n");
29     return (VP_FAILURE);
30   }
31   return (VP_SUCCESS);
32 }
33
34 C_RESULT
35 vp_stages_input_file_stage_transform(vp_stages_input_file_config_t *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
36 {
37   vp_os_mutex_lock(&out->lock);
38   uint32_t UI32_i=0;
39   char c;
40   uint32_t y_size, c_size;
41   if( out->status == VP_API_STATUS_INIT )
42   {
43     out->numBuffers =  1;
44     out->size = cfg->buffer_size;
45     out->buffers = (int8_t **) vp_os_malloc (sizeof(int8_t *)+out->size*sizeof(int8_t));
46     out->buffers[0] = (int8_t *)(out->buffers+1);
47     out->indexBuffer = 0;
48     // out->lineSize not used
49     out->status = VP_API_STATUS_PROCESSING;
50   }
51
52   // work and update status
53   if(out->size < (int32_t)cfg->buffer_size || feof(cfg->f))
54   {
55     if (cfg->loop)
56     {
57       rewind(cfg->f);
58     }
59     else
60     {
61       //vp_os_free(out->buffers);
62       out->status = VP_API_STATUS_ENDED;
63     }
64   }
65   else
66   {
67     if(out->status == VP_API_STATUS_PROCESSING)
68       out->size = fread(out->buffers[0], sizeof(int8_t), cfg->buffer_size*sizeof(int8_t), cfg->f);
69
70     if(out->size <= 0)
71     {
72       if (cfg->loop)
73       {
74         rewind(cfg->f);
75         out->size = fread(out->buffers[0], sizeof(int8_t), cfg->buffer_size*sizeof(int8_t), cfg->f);
76       }
77       else
78       {
79         vp_os_free(out->buffers);
80         out->status = VP_API_STATUS_ENDED;
81       }
82     }
83
84     if(ferror(cfg->f))
85     {
86       PRINT("ferror\n");
87       out->status = VP_API_STATUS_ERROR;
88     }
89   }
90
91   vp_os_mutex_unlock(&out->lock);
92   return (VP_SUCCESS);
93 }
94
95
96 C_RESULT
97 vp_stages_input_file_stage_close(vp_stages_input_file_config_t *cfg)
98 {
99   fclose(cfg->f);
100   return (VP_SUCCESS);
101 }
102
103 C_RESULT
104 vp_stages_output_file_stage_open(vp_stages_output_file_config_t *cfg)
105 {
106   VP_OS_ASSERT(cfg->flush_every_nb >= 0);
107   cfg->f = fopen(cfg->name, "wb");
108   return (VP_SUCCESS);
109 }
110
111 #define RATIO 1
112
113 C_RESULT
114 vp_stages_output_file_stage_transform(vp_stages_output_file_config_t *cfg, vp_api_io_data_t *in, vp_api_io_data_t *out)
115 {
116   static int total_size = 0;
117
118   vp_os_mutex_lock(&out->lock);
119
120   if(in->status == VP_API_STATUS_PROCESSING && in->size > 0)
121     fwrite(in->buffers[in->indexBuffer], sizeof(int8_t), in->size*sizeof(int8_t), cfg->f);
122
123   fflush(cfg->f);
124   total_size += in->size;
125   out->status = in->status;
126   vp_os_mutex_unlock(&out->lock);
127
128   return (VP_SUCCESS);
129 }
130
131 C_RESULT
132 vp_stages_output_file_stage_close(vp_stages_output_file_config_t *cfg)
133 {
134   fclose(cfg->f);
135   return (VP_SUCCESS);
136 }