FFmpeg
Loading...
Searching...
No Matches
simple_idct_template.c
Go to the documentation of this file.
1/*
2 * Simple IDCT
3 *
4 * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23/**
24 * @file
25 * simpleidct in C.
26 */
27
28/* Based upon some commented-out C code from mpeg2dec (idct_mmx.c
29 * written by Aaron Holtzman <aholtzma@ess.engr.uvic.ca>). */
30
31#include "bit_depth_template.c"
32
33#undef W1
34#undef W2
35#undef W3
36#undef W4
37#undef W5
38#undef W6
39#undef W7
40#undef ROW_SHIFT
41#undef COL_SHIFT
42#undef DC_SHIFT
43#undef MUL
44#undef MAC
45
46#if BIT_DEPTH == 8
47
48#define W1 22725 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
49#define W2 21407 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
50#define W3 19266 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
51#define W4 16383 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
52#define W5 12873 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
53#define W6 8867 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
54#define W7 4520 //cos(i*M_PI/16)*sqrt(2)*(1<<14) + 0.5
55
56#define ROW_SHIFT 11
57#define COL_SHIFT 20
58#define DC_SHIFT 3
59
60#define MUL(a, b) MUL16(a, b)
61#define MAC(a, b, c) MAC16(a, b, c)
62
63#elif BIT_DEPTH == 10 || BIT_DEPTH == 12 || BIT_DEPTH == 16
64
65# if BIT_DEPTH == 10 || BIT_DEPTH == 16
66#define W1 22725 // 90901
67#define W2 21407 // 85627
68#define W3 19265 // 77062
69#define W4 16384 // 65535
70#define W5 12873 // 51491
71#define W6 8867 // 35468
72#define W7 4520 // 18081
73
74# if BIT_DEPTH == 16
75/* 16-bit output from 16-bit coefficients. The rows keep two more
76 * fractional bits than 16-bit intermediates could hold, and the columns
77 * are shifted by as little as the 32-bit sums of a 16-bit result allow */
78# if IN_IDCT_DEPTH != 32
79#error "The 16-bit iDCT needs 32-bit intermediates"
80# endif
81#define ROW_SHIFT 13
82#define COL_SHIFT 15
83#define DC_SHIFT 1
84# elif defined(EXTRA_SHIFT)
85#define ROW_SHIFT 13
86#define COL_SHIFT 18
87#define DC_SHIFT 1
88# elif IN_IDCT_DEPTH == 32
89#define ROW_SHIFT 13
90#define COL_SHIFT 21
91#define DC_SHIFT 2
92# else
93#define ROW_SHIFT 12
94#define COL_SHIFT 19
95#define DC_SHIFT 2
96# endif
97
98# else
99#define W1 45451
100#define W2 42813
101#define W3 38531
102#define W4 32767
103#define W5 25746
104#define W6 17734
105#define W7 9041
106
107#define ROW_SHIFT 16
108#define COL_SHIFT 17
109#define DC_SHIFT -1
110# endif
111
112#define MUL(a, b) ((int)((SUINT)(a) * (b)))
113#define MAC(a, b, c) ((a) += (SUINT)(b) * (c))
114
115#else
116
117#error "Unsupported bitdepth"
118
119#endif
120
121#ifdef EXTRA_SHIFT
122static inline void FUNC(idctRowCondDC_extrashift)(int16_t *row, int extra_shift)
123#else
124static inline void FUNC6(idctRowCondDC)(idctin *row, int extra_shift)
125#endif
126{
127 SUINT a0, a1, a2, a3, b0, b1, b2, b3;
128
129// TODO: Add DC-only support for int32_t input
130#if IN_IDCT_DEPTH == 16
131#if HAVE_FAST_64BIT
132#define ROW0_MASK (0xffffULL << 48 * HAVE_BIGENDIAN)
133 if (((AV_RN64A(row) & ~ROW0_MASK) | AV_RN64A(row+4)) == 0) {
134 uint64_t temp;
135 if (DC_SHIFT - extra_shift >= 0) {
136 temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
137 } else {
138 temp = ((row[0] + (1<<(extra_shift - DC_SHIFT-1))) >> (extra_shift - DC_SHIFT)) & 0xffff;
139 }
140 temp += temp * (1 << 16);
141 temp += temp * ((uint64_t) 1 << 32);
142 AV_WN64A(row, temp);
143 AV_WN64A(row + 4, temp);
144 return;
145 }
146#else
147 if (!(AV_RN32A(row+2) |
148 AV_RN32A(row+4) |
149 AV_RN32A(row+6) |
150 row[1])) {
151 uint32_t temp;
152 if (DC_SHIFT - extra_shift >= 0) {
153 temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
154 } else {
155 temp = ((row[0] + (1<<(extra_shift - DC_SHIFT-1))) >> (extra_shift - DC_SHIFT)) & 0xffff;
156 }
157 temp += temp * (1 << 16);
158 AV_WN32A(row, temp);
159 AV_WN32A(row+2, temp);
160 AV_WN32A(row+4, temp);
161 AV_WN32A(row+6, temp);
162 return;
163 }
164#endif
165#endif
166
167 a0 = ((SUINT)W4 * row[0]) + (1 << (ROW_SHIFT + extra_shift - 1));
168 a1 = a0;
169 a2 = a0;
170 a3 = a0;
171
172 a0 += (SUINT)W2 * row[2];
173 a1 += (SUINT)W6 * row[2];
174 a2 -= (SUINT)W6 * row[2];
175 a3 -= (SUINT)W2 * row[2];
176
177 b0 = MUL(W1, row[1]);
178 MAC(b0, W3, row[3]);
179 b1 = MUL(W3, row[1]);
180 MAC(b1, -W7, row[3]);
181 b2 = MUL(W5, row[1]);
182 MAC(b2, -W1, row[3]);
183 b3 = MUL(W7, row[1]);
184 MAC(b3, -W5, row[3]);
185
186#if IN_IDCT_DEPTH == 32
187 if (AV_RN64A(row + 4) | AV_RN64A(row + 6)) {
188#else
189 if (AV_RN64A(row + 4)) {
190#endif
191 a0 += (SUINT) W4*row[4] + (SUINT)W6*row[6];
192 a1 += (SUINT)- W4*row[4] - (SUINT)W2*row[6];
193 a2 += (SUINT)- W4*row[4] + (SUINT)W2*row[6];
194 a3 += (SUINT) W4*row[4] - (SUINT)W6*row[6];
195
196 MAC(b0, W5, row[5]);
197 MAC(b0, W7, row[7]);
198
199 MAC(b1, -W1, row[5]);
200 MAC(b1, -W5, row[7]);
201
202 MAC(b2, W7, row[5]);
203 MAC(b2, W3, row[7]);
204
205 MAC(b3, W3, row[5]);
206 MAC(b3, -W1, row[7]);
207 }
208
209 row[0] = (int)(a0 + b0) >> (ROW_SHIFT + extra_shift);
210 row[7] = (int)(a0 - b0) >> (ROW_SHIFT + extra_shift);
211 row[1] = (int)(a1 + b1) >> (ROW_SHIFT + extra_shift);
212 row[6] = (int)(a1 - b1) >> (ROW_SHIFT + extra_shift);
213 row[2] = (int)(a2 + b2) >> (ROW_SHIFT + extra_shift);
214 row[5] = (int)(a2 - b2) >> (ROW_SHIFT + extra_shift);
215 row[3] = (int)(a3 + b3) >> (ROW_SHIFT + extra_shift);
216 row[4] = (int)(a3 - b3) >> (ROW_SHIFT + extra_shift);
217}
218
219#define IDCT_COLS do { \
220 a0 = (SUINT)W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
221 a1 = a0; \
222 a2 = a0; \
223 a3 = a0; \
224 \
225 a0 += (SUINT) W2*col[8*2]; \
226 a1 += (SUINT) W6*col[8*2]; \
227 a2 += (SUINT)-W6*col[8*2]; \
228 a3 += (SUINT)-W2*col[8*2]; \
229 \
230 b0 = MUL(W1, col[8*1]); \
231 b1 = MUL(W3, col[8*1]); \
232 b2 = MUL(W5, col[8*1]); \
233 b3 = MUL(W7, col[8*1]); \
234 \
235 MAC(b0, W3, col[8*3]); \
236 MAC(b1, -W7, col[8*3]); \
237 MAC(b2, -W1, col[8*3]); \
238 MAC(b3, -W5, col[8*3]); \
239 \
240 if (col[8*4]) { \
241 a0 += (SUINT) W4*col[8*4]; \
242 a1 += (SUINT)-W4*col[8*4]; \
243 a2 += (SUINT)-W4*col[8*4]; \
244 a3 += (SUINT) W4*col[8*4]; \
245 } \
246 \
247 if (col[8*5]) { \
248 MAC(b0, W5, col[8*5]); \
249 MAC(b1, -W1, col[8*5]); \
250 MAC(b2, W7, col[8*5]); \
251 MAC(b3, W3, col[8*5]); \
252 } \
253 \
254 if (col[8*6]) { \
255 a0 += (SUINT) W6*col[8*6]; \
256 a1 += (SUINT)-W2*col[8*6]; \
257 a2 += (SUINT) W2*col[8*6]; \
258 a3 += (SUINT)-W6*col[8*6]; \
259 } \
260 \
261 if (col[8*7]) { \
262 MAC(b0, W7, col[8*7]); \
263 MAC(b1, -W5, col[8*7]); \
264 MAC(b2, W3, col[8*7]); \
265 MAC(b3, -W1, col[8*7]); \
266 } \
267 } while (0)
268
269#ifdef EXTRA_SHIFT
270static inline void FUNC(idctSparseCol_extrashift)(int16_t *col)
271#else
272static inline void FUNC6(idctSparseCol)(idctin *col)
273#endif
274{
275 unsigned a0, a1, a2, a3, b0, b1, b2, b3;
276
277 IDCT_COLS;
278
279 col[0 ] = ((int)(a0 + b0) >> COL_SHIFT);
280 col[8 ] = ((int)(a1 + b1) >> COL_SHIFT);
281 col[16] = ((int)(a2 + b2) >> COL_SHIFT);
282 col[24] = ((int)(a3 + b3) >> COL_SHIFT);
283 col[32] = ((int)(a3 - b3) >> COL_SHIFT);
284 col[40] = ((int)(a2 - b2) >> COL_SHIFT);
285 col[48] = ((int)(a1 - b1) >> COL_SHIFT);
286 col[56] = ((int)(a0 - b0) >> COL_SHIFT);
287}
288
289#ifndef PRORES_ONLY
290#ifndef EXTRA_SHIFT
291static inline void FUNC6(idctSparseColPut)(pixel *dest, ptrdiff_t line_size,
292 idctin *col)
293{
294 SUINT a0, a1, a2, a3, b0, b1, b2, b3;
295
296 IDCT_COLS;
297
298 dest[0] = av_clip_pixel((int)(a0 + b0) >> COL_SHIFT);
299 dest += line_size;
300 dest[0] = av_clip_pixel((int)(a1 + b1) >> COL_SHIFT);
301 dest += line_size;
302 dest[0] = av_clip_pixel((int)(a2 + b2) >> COL_SHIFT);
303 dest += line_size;
304 dest[0] = av_clip_pixel((int)(a3 + b3) >> COL_SHIFT);
305 dest += line_size;
306 dest[0] = av_clip_pixel((int)(a3 - b3) >> COL_SHIFT);
307 dest += line_size;
308 dest[0] = av_clip_pixel((int)(a2 - b2) >> COL_SHIFT);
309 dest += line_size;
310 dest[0] = av_clip_pixel((int)(a1 - b1) >> COL_SHIFT);
311 dest += line_size;
312 dest[0] = av_clip_pixel((int)(a0 - b0) >> COL_SHIFT);
313}
314
315static inline void FUNC6(idctSparseColAdd)(pixel *dest, ptrdiff_t line_size,
316 idctin *col)
317{
318 unsigned a0, a1, a2, a3, b0, b1, b2, b3;
319
320 IDCT_COLS;
321
322 dest[0] = av_clip_pixel(dest[0] + ((int)(a0 + b0) >> COL_SHIFT));
323 dest += line_size;
324 dest[0] = av_clip_pixel(dest[0] + ((int)(a1 + b1) >> COL_SHIFT));
325 dest += line_size;
326 dest[0] = av_clip_pixel(dest[0] + ((int)(a2 + b2) >> COL_SHIFT));
327 dest += line_size;
328 dest[0] = av_clip_pixel(dest[0] + ((int)(a3 + b3) >> COL_SHIFT));
329 dest += line_size;
330 dest[0] = av_clip_pixel(dest[0] + ((int)(a3 - b3) >> COL_SHIFT));
331 dest += line_size;
332 dest[0] = av_clip_pixel(dest[0] + ((int)(a2 - b2) >> COL_SHIFT));
333 dest += line_size;
334 dest[0] = av_clip_pixel(dest[0] + ((int)(a1 - b1) >> COL_SHIFT));
335 dest += line_size;
336 dest[0] = av_clip_pixel(dest[0] + ((int)(a0 - b0) >> COL_SHIFT));
337}
338
339void FUNC6(ff_simple_idct_put)(uint8_t *dest_, ptrdiff_t line_size, int16_t *block_)
340{
341 idctin *block = (idctin *)block_;
342 pixel *dest = (pixel *)dest_;
343 int i;
344
345 line_size /= sizeof(pixel);
346
347 for (i = 0; i < 8; i++)
348 FUNC6(idctRowCondDC)(block + i*8, 0);
349
350 for (i = 0; i < 8; i++)
351 FUNC6(idctSparseColPut)(dest + i, line_size, block + i);
352}
353
354#if IN_IDCT_DEPTH == 16
355void FUNC6(ff_simple_idct_add)(uint8_t *dest_, ptrdiff_t line_size, int16_t *block)
356{
357 pixel *dest = (pixel *)dest_;
358 int i;
359
360 line_size /= sizeof(pixel);
361
362 for (i = 0; i < 8; i++)
363 FUNC6(idctRowCondDC)(block + i*8, 0);
364
365 for (i = 0; i < 8; i++)
366 FUNC6(idctSparseColAdd)(dest + i, line_size, block + i);
367}
368
369void FUNC6(ff_simple_idct)(int16_t *block)
370{
371 int i;
372
373 for (i = 0; i < 8; i++)
374 FUNC6(idctRowCondDC)(block + i*8, 0);
375
376 for (i = 0; i < 8; i++)
378}
379#endif
380#endif
381#endif /* PRORES_ONLY */
#define MUL(X, Y)
Definition binkdsp.c:35
#define idctin
#define av_clip_pixel(a)
#define FUNC(a)
#define FUNC6(a)
#define pixel
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define SUINT
static int16_t block[64]
Definition dct.c:125
#define AV_WN32A(p, v)
#define AV_RN32A(p)
#define AV_WN64A(p, v)
#define AV_RN64A(p)
#define COL_SHIFT
#define W3
#define W6
#define DC_SHIFT
#define ROW_SHIFT
#define W5
#define W7
#define W1
#define W2
#define W4
void FUNC6 ff_simple_idct_put(uint8_t *dest_, ptrdiff_t line_size, int16_t *block_)
static void FUNC6 idctRowCondDC(idctin *row, int extra_shift)
#define IDCT_COLS
static void FUNC6 idctSparseColAdd(pixel *dest, ptrdiff_t line_size, idctin *col)
static void FUNC6 idctSparseColPut(pixel *dest, ptrdiff_t line_size, idctin *col)
static void FUNC6 idctSparseCol(idctin *col)
else temp
Definition vf_mcdeint.c:275
static double b1(void *priv, double x, double y)
Definition vf_xfade.c:2034
static double a0(void *priv, double x, double y)
Definition vf_xfade.c:2028
static double b2(void *priv, double x, double y)
Definition vf_xfade.c:2035
static double b3(void *priv, double x, double y)
Definition vf_xfade.c:2036
static double a3(void *priv, double x, double y)
Definition vf_xfade.c:2031
static double b0(void *priv, double x, double y)
Definition vf_xfade.c:2033
static double a2(void *priv, double x, double y)
Definition vf_xfade.c:2030
static double a1(void *priv, double x, double y)
Definition vf_xfade.c:2029