f1bad8eef70266704c5a717daa1d2dd37d8256b1
[mardrone] / mardrone / ARDrone_SDK_Version_1_8_20110726 / ARDroneLib / VLIB / Utils / video_bin_dct.c
1 #include <VLIB/Utils/video_bin_dct.h>
2
3 static void bin_fdct(const uint16_t* data_in, int16_t* data_out)
4 {
5 }
6
7 static void bin_idct(const int16_t* data_in, uint16_t* data_out)
8 {
9   int i;
10   int16_t x0, x1, x2, x3, x4, x5, x6, x7;
11   int16_t tx0, tx3, tx4, tx5;
12   const int16_t* in  = data_in;
13   int16_t* out = (int16_t*) data_out;
14
15   // Compute lines
16   for( i = 8; i > 0; i-- )
17   {
18     x0 = in[0];  x1 = in[1];  x2 = in[2];  x3 = in[3];
19     x4 = in[4];  x5 = in[5];  x6 = in[6];  x7 = in[7];
20
21     // First step
22     x4 = x0/2 - x4;
23     x2 = x2 - 3*x6/8;
24     x3 = x3 + x5/2;
25
26     // Second step
27     x0 = x0 - x4;
28     x6 = x6 + 3*x2/8;
29     x7 = x7 + x1/8;
30     x5 = x5 - 7*x3/8;
31
32     // Third step
33     x0 = x0 + x2;
34     x2 = x0 - x2*2;
35     x4 = x4 + x6;
36     x6 = x4 - x6*2;
37     x7 = x7 + x5;
38     x5 = x7 - x5*2;
39     x1 = x1 + x3;
40     x3 = x1 - x3*2;
41
42     // Fourth step
43     x5 = 5*x3/8 - x5;
44
45     // Fifth step
46     x3 = x3 - 3*x5/8;
47
48     // Final step
49     out[0] = (x0 + x1) >> 1;
50     out[1] = (x4 + x3) >> 1;
51     out[2] = (x6 + x5) >> 1;
52     out[3] = (x2 + x7) >> 1;
53     out[4] = (x2 - x7) >> 1;
54     out[5] = (x6 - x5) >> 1;
55     out[6] = (x4 - x3) >> 1;
56     out[7] = (x0 - x1) >> 1;
57
58     in  += 8;
59     out += 8;
60   }
61
62   in  = (const int16_t*) data_out;
63   out = (int16_t*) data_out;
64
65   // Compute columns
66   for( i = 8; i > 0; i-- )
67   {
68     tx0 = in[0];  x1 = in[8];  x2 = in[16];  tx3 = in[24];
69     tx4 = in[32]; tx5 = in[40];  x6 = in[48];  x7 = in[56];
70
71     // First step
72     x4 = tx0/2 - tx4;
73     x0 = tx0/2 + tx4;
74     x3 = tx5/2 + tx3;
75     x5 = tx5/2 - tx3;
76
77     x2 =   x2   - 3*x6/8;
78     x6 = 3*x2/8 +   x6;
79
80     x7 += x1/8;
81     x5 += x3/8;
82
83     // Second step
84     x0 = x0 + x2;
85     x2 = x0 - x2*2;
86     x4 = x4 + x6;
87     x6 = x4 - x6*2;
88     x7 = x7 + x5;
89     x5 = x7 - x5*2;
90     x1 = x1 + x3;
91     x3 = x1 - x3*2;
92
93     // Third step
94     x5 = 5*x3/8 -   x5;
95     x3 =   x3   - 3*x5/8;
96
97     // Final step - TODO Check if we can replace out by data_out
98     out[0]  = x0 + x1;
99     out[8]  = x4 + x3;
100     out[16] = x6 + x5;
101     out[24] = x2 + x7;
102     out[32] = x2 - x7;
103     out[40] = x6 - x5;
104     out[48] = x4 - x3;
105     out[56] = x0 - x1;
106
107     in  += 1;
108     out += 1;
109   }
110 }
111
112 int16_t* video_fdct_compute(int16_t* in, int16_t* out, int32_t num_macro_blocks)
113 {
114   while( num_macro_blocks > 0 )
115   {
116     bin_fdct((uint16_t*)in, out);
117
118     in  += MCU_BLOCK_SIZE;
119     out += MCU_BLOCK_SIZE;
120
121     bin_fdct((uint16_t*)in, out);
122
123     in  += MCU_BLOCK_SIZE;
124     out += MCU_BLOCK_SIZE;
125
126     bin_fdct((uint16_t*)in, out);
127
128     in  += MCU_BLOCK_SIZE;
129     out += MCU_BLOCK_SIZE;
130
131     bin_fdct((uint16_t*)in, out);
132
133     in  += MCU_BLOCK_SIZE;
134     out += MCU_BLOCK_SIZE;
135
136     bin_fdct((uint16_t*)in, out);
137
138     in  += MCU_BLOCK_SIZE;
139     out += MCU_BLOCK_SIZE;
140
141     bin_fdct((uint16_t*)in, out);
142
143     in  += MCU_BLOCK_SIZE;
144     out += MCU_BLOCK_SIZE;
145
146     num_macro_blocks--;
147   }
148
149   return out;
150 }
151
152 int16_t* video_idct_compute(int16_t* in, int16_t* out, int32_t num_macro_blocks)
153 {
154   while( num_macro_blocks > 0 )
155   {
156     bin_idct(in, (uint16_t*)out);
157
158     in  += MCU_BLOCK_SIZE;
159     out += MCU_BLOCK_SIZE;
160
161     bin_idct(in, (uint16_t*)out);
162
163     in  += MCU_BLOCK_SIZE;
164     out += MCU_BLOCK_SIZE;
165
166     bin_idct(in, (uint16_t*)out);
167
168     in  += MCU_BLOCK_SIZE;
169     out += MCU_BLOCK_SIZE;
170
171     bin_idct(in, (uint16_t*)out);
172
173     in  += MCU_BLOCK_SIZE;
174     out += MCU_BLOCK_SIZE;
175
176     bin_idct(in, (uint16_t*)out);
177
178     in  += MCU_BLOCK_SIZE;
179     out += MCU_BLOCK_SIZE;
180
181     bin_idct(in, (uint16_t*)out);
182
183     in  += MCU_BLOCK_SIZE;
184     out += MCU_BLOCK_SIZE;
185
186     num_macro_blocks--;
187   }
188
189   return out;
190 }