]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp3dsp.c
ec62d9456de9eea493536285dd2f3d49e91f8fd1
[ffmpeg] / libavcodec / vp3dsp.c
1 /*
2  * Copyright (C) 2004 the ffmpeg project
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 /**
20  * @file vp3dsp.c
21  * Standard C DSP-oriented functions cribbed from the original VP3 
22  * source code.
23  */
24
25 #include "common.h"
26 #include "avcodec.h"
27 #include "vp3data.h"
28
29 #define IdctAdjustBeforeShift 8
30 #define xC1S7 64277
31 #define xC2S6 60547
32 #define xC3S5 54491
33 #define xC4S4 46341
34 #define xC5S3 36410
35 #define xC6S2 25080
36 #define xC7S1 12785
37
38 void vp3_dsp_init_c(void)
39 {
40     /* nop */
41 }
42
43 static void vp3_idct_c(int32_t *dequantized_data, int16_t *output_data)
44 {
45     int32_t *ip = dequantized_data;
46     int16_t *op = output_data;
47
48     int32_t A_, B_, C_, D_, _Ad, _Bd, _Cd, _Dd, E_, F_, G_, H_;
49     int32_t _Ed, _Gd, _Add, _Bdd, _Fd, _Hd;
50     int32_t t1, t2;
51
52     int i;
53
54     /* Inverse DCT on the rows now */
55     for (i = 0; i < 8; i++) {
56         /* Check for non-zero values */
57         if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
58             t1 = (int32_t)(xC1S7 * ip[1]);
59             t2 = (int32_t)(xC7S1 * ip[7]);
60             t1 >>= 16;
61             t2 >>= 16;
62             A_ = t1 + t2;
63
64             t1 = (int32_t)(xC7S1 * ip[1]);
65             t2 = (int32_t)(xC1S7 * ip[7]);
66             t1 >>= 16;
67             t2 >>= 16;
68             B_ = t1 - t2;
69
70             t1 = (int32_t)(xC3S5 * ip[3]);
71             t2 = (int32_t)(xC5S3 * ip[5]);
72             t1 >>= 16;
73             t2 >>= 16;
74             C_ = t1 + t2;
75
76             t1 = (int32_t)(xC3S5 * ip[5]);
77             t2 = (int32_t)(xC5S3 * ip[3]);
78             t1 >>= 16;
79             t2 >>= 16;
80             D_ = t1 - t2;
81
82
83             t1 = (int32_t)(xC4S4 * (A_ - C_));
84             t1 >>= 16;
85             _Ad = t1;
86
87             t1 = (int32_t)(xC4S4 * (B_ - D_));
88             t1 >>= 16;
89             _Bd = t1;
90
91
92             _Cd = A_ + C_;
93             _Dd = B_ + D_;
94
95             t1 = (int32_t)(xC4S4 * (ip[0] + ip[4]));
96             t1 >>= 16;
97             E_ = t1;
98
99             t1 = (int32_t)(xC4S4 * (ip[0] - ip[4]));
100             t1 >>= 16;
101             F_ = t1;
102
103             t1 = (int32_t)(xC2S6 * ip[2]);
104             t2 = (int32_t)(xC6S2 * ip[6]);
105             t1 >>= 16;
106             t2 >>= 16;
107             G_ = t1 + t2;
108
109             t1 = (int32_t)(xC6S2 * ip[2]);
110             t2 = (int32_t)(xC2S6 * ip[6]);
111             t1 >>= 16;
112             t2 >>= 16;
113             H_ = t1 - t2;
114
115
116             _Ed = E_ - G_;
117             _Gd = E_ + G_;
118
119             _Add = F_ + _Ad;
120             _Bdd = _Bd - H_;
121
122             _Fd = F_ - _Ad;
123             _Hd = _Bd + H_;
124
125             /*  Final sequence of operations over-write original inputs. */
126             ip[0] = (int16_t)((_Gd + _Cd )   >> 0);
127             ip[7] = (int16_t)((_Gd - _Cd )   >> 0);
128
129             ip[1] = (int16_t)((_Add + _Hd )  >> 0);
130             ip[2] = (int16_t)((_Add - _Hd )  >> 0);
131
132             ip[3] = (int16_t)((_Ed + _Dd )   >> 0);
133             ip[4] = (int16_t)((_Ed - _Dd )   >> 0);
134
135             ip[5] = (int16_t)((_Fd + _Bdd )  >> 0);
136             ip[6] = (int16_t)((_Fd - _Bdd )  >> 0);
137
138         }
139
140         ip += 8;            /* next row */
141     }
142
143     ip = dequantized_data;
144
145     for ( i = 0; i < 8; i++) {
146         /* Check for non-zero values (bitwise or faster than ||) */
147         if ( ip[0 * 8] | ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
148              ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
149
150             t1 = (int32_t)(xC1S7 * ip[1*8]);
151             t2 = (int32_t)(xC7S1 * ip[7*8]);
152             t1 >>= 16;
153             t2 >>= 16;
154             A_ = t1 + t2;
155
156             t1 = (int32_t)(xC7S1 * ip[1*8]);
157             t2 = (int32_t)(xC1S7 * ip[7*8]);
158             t1 >>= 16;
159             t2 >>= 16;
160             B_ = t1 - t2;
161
162             t1 = (int32_t)(xC3S5 * ip[3*8]);
163             t2 = (int32_t)(xC5S3 * ip[5*8]);
164             t1 >>= 16;
165             t2 >>= 16;
166             C_ = t1 + t2;
167
168             t1 = (int32_t)(xC3S5 * ip[5*8]);
169             t2 = (int32_t)(xC5S3 * ip[3*8]);
170             t1 >>= 16;
171             t2 >>= 16;
172             D_ = t1 - t2;
173
174
175             t1 = (int32_t)(xC4S4 * (A_ - C_));
176             t1 >>= 16;
177             _Ad = t1;
178
179             t1 = (int32_t)(xC4S4 * (B_ - D_));
180             t1 >>= 16;
181             _Bd = t1;
182
183
184             _Cd = A_ + C_;
185             _Dd = B_ + D_;
186
187             t1 = (int32_t)(xC4S4 * (ip[0*8] + ip[4*8]));
188             t1 >>= 16;
189             E_ = t1;
190
191             t1 = (int32_t)(xC4S4 * (ip[0*8] - ip[4*8]));
192             t1 >>= 16;
193             F_ = t1;
194
195             t1 = (int32_t)(xC2S6 * ip[2*8]);
196             t2 = (int32_t)(xC6S2 * ip[6*8]);
197             t1 >>= 16;
198             t2 >>= 16;
199             G_ = t1 + t2;
200
201             t1 = (int32_t)(xC6S2 * ip[2*8]);
202             t2 = (int32_t)(xC2S6 * ip[6*8]);
203             t1 >>= 16;
204             t2 >>= 16;
205             H_ = t1 - t2;
206
207
208             _Ed = E_ - G_;
209             _Gd = E_ + G_;
210
211             _Add = F_ + _Ad;
212             _Bdd = _Bd - H_;
213
214             _Fd = F_ - _Ad;
215             _Hd = _Bd + H_;
216
217             _Gd += IdctAdjustBeforeShift;
218             _Add += IdctAdjustBeforeShift;
219             _Ed += IdctAdjustBeforeShift;
220             _Fd += IdctAdjustBeforeShift;
221
222             /* Final sequence of operations over-write original inputs. */
223             op[0*8] = (int16_t)((_Gd + _Cd )   >> 4);
224             op[7*8] = (int16_t)((_Gd - _Cd )   >> 4);
225
226             op[1*8] = (int16_t)((_Add + _Hd )  >> 4);
227             op[2*8] = (int16_t)((_Add - _Hd )  >> 4);
228
229             op[3*8] = (int16_t)((_Ed + _Dd )   >> 4);
230             op[4*8] = (int16_t)((_Ed - _Dd )   >> 4);
231
232             op[5*8] = (int16_t)((_Fd + _Bdd )  >> 4);
233             op[6*8] = (int16_t)((_Fd - _Bdd )  >> 4);
234
235         } else {
236
237             op[0*8] = 0;
238             op[7*8] = 0;
239             op[1*8] = 0;
240             op[2*8] = 0;
241             op[3*8] = 0;
242             op[4*8] = 0;
243             op[5*8] = 0;
244             op[6*8] = 0;
245         }
246
247         ip++;            /* next column */
248         op++;
249     }
250 }
251
252 void vp3_idct_put_c(int16_t *input_data, int16_t *dequant_matrix,
253     int coeff_count, uint8_t *dest, int stride)
254 {
255     int32_t dequantized_data[64];
256     int16_t transformed_data[64];
257     int16_t *op;
258     int i, j;
259
260     /* de-zigzag and dequantize */
261     for (i = 0; i < coeff_count; i++) {
262         j = dezigzag_index[i];
263         dequantized_data[j] = dequant_matrix[i] * input_data[i];
264     }
265
266     vp3_idct_c(dequantized_data, transformed_data);
267
268     /* place in final output */
269     op = transformed_data;
270     for (i = 0; i < 8; i++) {
271         for (j = 0; j < 8; j++) {
272             if (*op < -128)
273                 *dest = 0;
274             else if (*op > 127)
275                 *dest = 255;
276             else
277                 *dest = (uint8_t)(*op + 128);
278             op++;
279             dest++;
280         }
281         dest += (stride - 8);
282     }
283 }
284
285 void vp3_idct_add_c(int16_t *input_data, int16_t *dequant_matrix,
286     int coeff_count, uint8_t *dest, int stride)
287 {
288     int32_t dequantized_data[64];
289     int16_t transformed_data[64];
290     int16_t *op;
291     int i, j;
292     int16_t sample;
293
294     /* de-zigzag and dequantize */
295     for (i = 0; i < coeff_count; i++) {
296         j = dezigzag_index[i];
297         dequantized_data[j] = dequant_matrix[i] * input_data[i];
298     }
299
300     vp3_idct_c(dequantized_data, transformed_data);
301
302     /* place in final output */
303     op = transformed_data;
304     for (i = 0; i < 8; i++) {
305         for (j = 0; j < 8; j++) {
306             sample = *dest + *op;
307             if (sample < 0)
308                 *dest = 0;
309             else if (sample > 255)
310                 *dest = 255;
311             else
312                 *dest = (uint8_t)(sample & 0xFF);
313             op++;
314             dest++;
315         }
316         dest += (stride - 8);
317     }
318 }