clib/libmpeg2/idct.c

filter: Statementstmt 0.0% 0 / 148 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 180 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« clib/libmpeg2/header.c ↑ Summary clib/libmpeg2/idct_mmx.c »
1 /*
2 * idct.c
3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5 *
6 * This file is part of mpeg2dec, a free MPEG-2 video stream decoder.
7 * See http://libmpeg2.sourceforge.net/ for updates.
8 *
9 * mpeg2dec is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * mpeg2dec is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23  
24 #include "config.h"
25  
26 #include <stdlib.h>
27 #include <inttypes.h>
28  
29 #include "mpeg2.h"
30 #include "attributes.h"
31 #include "mpeg2_internal.h"
32  
33 #define W1 2841 /* 2048 * sqrt (2) * cos (1 * pi / 16) */
34 #define W2 2676 /* 2048 * sqrt (2) * cos (2 * pi / 16) */
35 #define W3 2408 /* 2048 * sqrt (2) * cos (3 * pi / 16) */
36 #define W5 1609 /* 2048 * sqrt (2) * cos (5 * pi / 16) */
37 #define W6 1108 /* 2048 * sqrt (2) * cos (6 * pi / 16) */
38 #define W7 565 /* 2048 * sqrt (2) * cos (7 * pi / 16) */
39  
40 /* idct main entry point */
41 void (* mpeg2_idct_copy) (int16_t * block, uint8_t * dest, int stride);
42 void (* mpeg2_idct_add) (int last, int16_t * block,
43 uint8_t * dest, int stride);
44  
45 /*
46 * In legal streams, the IDCT output should be between -384 and +384.
47 * In corrupted streams, it is possible to force the IDCT output to go
48 * to +-3826 - this is the worst case for a column IDCT where the
49 * column inputs are 16-bit values.
50 */
51 uint8_t mpeg2_clip[3840 * 2 + 256];
52 #define CLIP(i) ((mpeg2_clip + 3840)[i])
53  
54 #if 0
55 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
56 do { \
57 t0 = W0 * d0 + W1 * d1; \
58 t1 = W0 * d1 - W1 * d0; \
59 } while (0)
60 #else
61 #define BUTTERFLY(t0,t1,W0,W1,d0,d1) \
62 do { \
63 int tmp = W0 * (d0 + d1); \
64 t0 = tmp + (W1 - W0) * d1; \
65 t1 = tmp - (W1 + W0) * d0; \
66 } while (0)
67 #endif
68  
69 0 static inline void idct_row (int16_t * const block)
70 {
71 int d0, d1, d2, d3;
72 int a0, a1, a2, a3, b0, b1, b2, b3;
73 int t0, t1, t2, t3;
74  
75 /* shortcut */
76 0 ▶ if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] |
Branch: 1 branch
BranchTrueFalseTotal
if (likely (!(block[1] | ((int32_t *)block)[1] | ((int32_t *)block)[2] | 0 0 0
77 ((int32_t *)block)[3]))) {
78 0 uint32_t tmp = (uint16_t) (block[0] >> 1);
79 0 tmp |= tmp << 16;
80 0 ((int32_t *)block)[0] = tmp;
81 0 ((int32_t *)block)[1] = tmp;
82 0 ((int32_t *)block)[2] = tmp;
83 0 ((int32_t *)block)[3] = tmp;
84 0 return;
85 }
86  
87 0 d0 = (block[0] << 11) + 2048;
88 0 d1 = block[1];
89 0 d2 = block[2] << 11;
90 0 d3 = block[3];
91 0 t0 = d0 + d2;
92 0 t1 = d0 - d2;
93 0 BUTTERFLY (t2, t3, W6, W2, d3, d1);
94 0 a0 = t0 + t2;
95 0 a1 = t1 + t3;
96 0 a2 = t1 - t3;
97 0 a3 = t0 - t2;
98  
99 0 d0 = block[4];
100 0 d1 = block[5];
101 0 d2 = block[6];
102 0 d3 = block[7];
103 0 BUTTERFLY (t0, t1, W7, W1, d3, d0);
104 0 BUTTERFLY (t2, t3, W3, W5, d1, d2);
105 0 b0 = t0 + t2;
106 0 b3 = t1 + t3;
107 0 t0 -= t2;
108 0 t1 -= t3;
109 0 b1 = ((t0 + t1) >> 8) * 181;
110 0 b2 = ((t0 - t1) >> 8) * 181;
111  
112 0 block[0] = (a0 + b0) >> 12;
113 0 block[1] = (a1 + b1) >> 12;
114 0 block[2] = (a2 + b2) >> 12;
115 0 block[3] = (a3 + b3) >> 12;
116 0 block[4] = (a3 - b3) >> 12;
117 0 block[5] = (a2 - b2) >> 12;
118 0 block[6] = (a1 - b1) >> 12;
119 0 block[7] = (a0 - b0) >> 12;
120 }
121  
122 0 static inline void idct_col (int16_t * const block)
123 {
124 int d0, d1, d2, d3;
125 int a0, a1, a2, a3, b0, b1, b2, b3;
126 int t0, t1, t2, t3;
127  
128 0 d0 = (block[8*0] << 11) + 65536;
129 0 d1 = block[8*1];
130 0 d2 = block[8*2] << 11;
131 0 d3 = block[8*3];
132 0 t0 = d0 + d2;
133 0 t1 = d0 - d2;
134 0 BUTTERFLY (t2, t3, W6, W2, d3, d1);
135 0 a0 = t0 + t2;
136 0 a1 = t1 + t3;
137 0 a2 = t1 - t3;
138 0 a3 = t0 - t2;
139  
140 0 d0 = block[8*4];
141 0 d1 = block[8*5];
142 0 d2 = block[8*6];
143 0 d3 = block[8*7];
144 0 BUTTERFLY (t0, t1, W7, W1, d3, d0);
145 0 BUTTERFLY (t2, t3, W3, W5, d1, d2);
146 0 b0 = t0 + t2;
147 0 b3 = t1 + t3;
148 0 t0 -= t2;
149 0 t1 -= t3;
150 0 b1 = ((t0 + t1) >> 8) * 181;
151 0 b2 = ((t0 - t1) >> 8) * 181;
152  
153 0 block[8*0] = (a0 + b0) >> 17;
154 0 block[8*1] = (a1 + b1) >> 17;
155 0 block[8*2] = (a2 + b2) >> 17;
156 0 block[8*3] = (a3 + b3) >> 17;
157 0 block[8*4] = (a3 - b3) >> 17;
158 0 block[8*5] = (a2 - b2) >> 17;
159 0 block[8*6] = (a1 - b1) >> 17;
160 0 block[8*7] = (a0 - b0) >> 17;
161 0 }
162  
163 0 static void mpeg2_idct_copy_c (int16_t * block, uint8_t * dest,
164 const int stride)
165 {
166 int i;
167  
168 0 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 0 0 0
169 0 idct_row (block + 8 * i);
170 0 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 0 0 0
171 0 idct_col (block + i);
172 do {
173 0 dest[0] = CLIP (block[0]);
174 0 dest[1] = CLIP (block[1]);
175 0 dest[2] = CLIP (block[2]);
176 0 dest[3] = CLIP (block[3]);
177 0 dest[4] = CLIP (block[4]);
178 0 dest[5] = CLIP (block[5]);
179 0 dest[6] = CLIP (block[6]);
180 0 dest[7] = CLIP (block[7]);
181  
182 0 ((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0;
183 0 ((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0;
184  
185 0 dest += stride;
186 0 block += 8;
187 0 ▶ } while (--i);
Branch: 1 branch
BranchTrueFalseTotal
} while (--i); 0 0 0
188 0 }
189  
190 0 static void mpeg2_idct_add_c (const int last, int16_t * block,
191 uint8_t * dest, const int stride)
192 {
193 int i;
194  
195 0 ▶ if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) {
Branch: 2 branches
BranchTrueFalseTotal
if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) { 0 0 0
if (last != 129 || (block[0] & (7 << 4)) == (4 << 4)) { 0 0 0
196 0 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 0 0 0
197 0 idct_row (block + 8 * i);
198 0 ▶ for (i = 0; i < 8; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 8; i++) 0 0 0
199 0 idct_col (block + i);
200 do {
201 0 dest[0] = CLIP (block[0] + dest[0]);
202 0 dest[1] = CLIP (block[1] + dest[1]);
203 0 dest[2] = CLIP (block[2] + dest[2]);
204 0 dest[3] = CLIP (block[3] + dest[3]);
205 0 dest[4] = CLIP (block[4] + dest[4]);
206 0 dest[5] = CLIP (block[5] + dest[5]);
207 0 dest[6] = CLIP (block[6] + dest[6]);
208 0 dest[7] = CLIP (block[7] + dest[7]);
209  
210 0 ((int32_t *)block)[0] = 0; ((int32_t *)block)[1] = 0;
211 0 ((int32_t *)block)[2] = 0; ((int32_t *)block)[3] = 0;
212  
213 0 dest += stride;
214 0 block += 8;
215 0 ▶ } while (--i);
Branch: 1 branch
BranchTrueFalseTotal
} while (--i); 0 0 0
216 } else {
217 int DC;
218  
219 0 DC = (block[0] + 64) >> 7;
220 0 block[0] = block[63] = 0;
221 0 i = 8;
222 do {
223 0 dest[0] = CLIP (DC + dest[0]);
224 0 dest[1] = CLIP (DC + dest[1]);
225 0 dest[2] = CLIP (DC + dest[2]);
226 0 dest[3] = CLIP (DC + dest[3]);
227 0 dest[4] = CLIP (DC + dest[4]);
228 0 dest[5] = CLIP (DC + dest[5]);
229 0 dest[6] = CLIP (DC + dest[6]);
230 0 dest[7] = CLIP (DC + dest[7]);
231 0 dest += stride;
232 0 ▶ } while (--i);
Branch: 1 branch
BranchTrueFalseTotal
} while (--i); 0 0 0
233 }
234 0 }
235  
236 0 void mpeg2_idct_init (uint32_t accel)
237 {
238 #ifdef ARCH_X86
239 0 ▶ if (accel & MPEG2_ACCEL_X86_SSE2) {
Branch: 1 branch
BranchTrueFalseTotal
if (accel & MPEG2_ACCEL_X86_SSE2) { 0 0 0
240 0 mpeg2_idct_copy = mpeg2_idct_copy_sse2;
241 0 mpeg2_idct_add = mpeg2_idct_add_sse2;
242 0 mpeg2_idct_mmx_init ();
243 0 ▶ } else if (accel & MPEG2_ACCEL_X86_MMXEXT) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (accel & MPEG2_ACCEL_X86_MMXEXT) { 0 0 0
244 0 mpeg2_idct_copy = mpeg2_idct_copy_mmxext;
245 0 mpeg2_idct_add = mpeg2_idct_add_mmxext;
246 0 mpeg2_idct_mmx_init ();
247 0 ▶ } else if (accel & MPEG2_ACCEL_X86_MMX) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (accel & MPEG2_ACCEL_X86_MMX) { 0 0 0
248 0 mpeg2_idct_copy = mpeg2_idct_copy_mmx;
249 0 mpeg2_idct_add = mpeg2_idct_add_mmx;
250 0 mpeg2_idct_mmx_init ();
251 } else
252 #endif
253 #ifdef ARCH_PPC
254 if (accel & MPEG2_ACCEL_PPC_ALTIVEC) {
255 mpeg2_idct_copy = mpeg2_idct_copy_altivec;
256 mpeg2_idct_add = mpeg2_idct_add_altivec;
257 mpeg2_idct_altivec_init ();
258 } else
259 #endif
260 #ifdef ARCH_ALPHA
261 if (accel & MPEG2_ACCEL_ALPHA_MVI) {
262 mpeg2_idct_copy = mpeg2_idct_copy_mvi;
263 mpeg2_idct_add = mpeg2_idct_add_mvi;
264 mpeg2_idct_alpha_init ();
265 } else if (accel & MPEG2_ACCEL_ALPHA) {
266 int i;
267  
268 mpeg2_idct_copy = mpeg2_idct_copy_alpha;
269 mpeg2_idct_add = mpeg2_idct_add_alpha;
270 mpeg2_idct_alpha_init ();
271 for (i = -3840; i < 3840 + 256; i++)
272 CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
273 } else
274 #endif
275 {
276 int i, j;
277  
278 0 mpeg2_idct_copy = mpeg2_idct_copy_c;
279 0 mpeg2_idct_add = mpeg2_idct_add_c;
280 0 ▶ for (i = -3840; i < 3840 + 256; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = -3840; i < 3840 + 256; i++) 0 0 0
281 0 ▶ CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i);
Branch: 1 branch
BranchTrueFalseTotal
CLIP(i) = (i < 0) ? 0 : ((i > 255) ? 255 : i); 0 0 0
282 0 ▶ for (i = 0; i < 64; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 64; i++) { 0 0 0
283 0 j = mpeg2_scan_norm[i];
284 0 mpeg2_scan_norm[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
285 0 j = mpeg2_scan_alt[i];
286 0 mpeg2_scan_alt[i] = ((j & 0x36) >> 1) | ((j & 0x09) << 2);
287 }
288 }
289 0 }
« clib/libmpeg2/header.c ↑ Summary clib/libmpeg2/idct_mmx.c »