FFmpeg
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
64 
65 # if BIT_DEPTH == 10
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 # ifdef EXTRA_SHIFT
75 #define ROW_SHIFT 13
76 #define COL_SHIFT 18
77 #define DC_SHIFT 1
78 # elif IN_IDCT_DEPTH == 32
79 #define ROW_SHIFT 13
80 #define COL_SHIFT 21
81 #define DC_SHIFT 2
82 # else
83 #define ROW_SHIFT 12
84 #define COL_SHIFT 19
85 #define DC_SHIFT 2
86 # endif
87 
88 # else
89 #define W1 45451
90 #define W2 42813
91 #define W3 38531
92 #define W4 32767
93 #define W5 25746
94 #define W6 17734
95 #define W7 9041
96 
97 #define ROW_SHIFT 16
98 #define COL_SHIFT 17
99 #define DC_SHIFT -1
100 # endif
101 
102 #define MUL(a, b) ((int)((SUINT)(a) * (b)))
103 #define MAC(a, b, c) ((a) += (SUINT)(b) * (c))
104 
105 #else
106 
107 #error "Unsupported bitdepth"
108 
109 #endif
110 
111 #ifdef EXTRA_SHIFT
112 static inline void FUNC(idctRowCondDC_extrashift)(int16_t *row, int extra_shift)
113 #else
114 static inline void FUNC6(idctRowCondDC)(idctin *row, int extra_shift)
115 #endif
116 {
117  SUINT a0, a1, a2, a3, b0, b1, b2, b3;
118 
119 // TODO: Add DC-only support for int32_t input
120 #if IN_IDCT_DEPTH == 16
121 #if HAVE_FAST_64BIT
122 #define ROW0_MASK (0xffffULL << 48 * HAVE_BIGENDIAN)
123  if (((AV_RN64A(row) & ~ROW0_MASK) | AV_RN64A(row+4)) == 0) {
124  uint64_t temp;
125  if (DC_SHIFT - extra_shift >= 0) {
126  temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
127  } else {
128  temp = ((row[0] + (1<<(extra_shift - DC_SHIFT-1))) >> (extra_shift - DC_SHIFT)) & 0xffff;
129  }
130  temp += temp * (1 << 16);
131  temp += temp * ((uint64_t) 1 << 32);
132  AV_WN64A(row, temp);
133  AV_WN64A(row + 4, temp);
134  return;
135  }
136 #else
137  if (!(AV_RN32A(row+2) |
138  AV_RN32A(row+4) |
139  AV_RN32A(row+6) |
140  row[1])) {
141  uint32_t temp;
142  if (DC_SHIFT - extra_shift >= 0) {
143  temp = (row[0] * (1 << (DC_SHIFT - extra_shift))) & 0xffff;
144  } else {
145  temp = ((row[0] + (1<<(extra_shift - DC_SHIFT-1))) >> (extra_shift - DC_SHIFT)) & 0xffff;
146  }
147  temp += temp * (1 << 16);
148  AV_WN32A(row, temp);
149  AV_WN32A(row+2, temp);
150  AV_WN32A(row+4, temp);
151  AV_WN32A(row+6, temp);
152  return;
153  }
154 #endif
155 #endif
156 
157  a0 = ((SUINT)W4 * row[0]) + (1 << (ROW_SHIFT + extra_shift - 1));
158  a1 = a0;
159  a2 = a0;
160  a3 = a0;
161 
162  a0 += (SUINT)W2 * row[2];
163  a1 += (SUINT)W6 * row[2];
164  a2 -= (SUINT)W6 * row[2];
165  a3 -= (SUINT)W2 * row[2];
166 
167  b0 = MUL(W1, row[1]);
168  MAC(b0, W3, row[3]);
169  b1 = MUL(W3, row[1]);
170  MAC(b1, -W7, row[3]);
171  b2 = MUL(W5, row[1]);
172  MAC(b2, -W1, row[3]);
173  b3 = MUL(W7, row[1]);
174  MAC(b3, -W5, row[3]);
175 
176 #if IN_IDCT_DEPTH == 32
177  if (AV_RN64A(row + 4) | AV_RN64A(row + 6)) {
178 #else
179  if (AV_RN64A(row + 4)) {
180 #endif
181  a0 += (SUINT) W4*row[4] + (SUINT)W6*row[6];
182  a1 += (SUINT)- W4*row[4] - (SUINT)W2*row[6];
183  a2 += (SUINT)- W4*row[4] + (SUINT)W2*row[6];
184  a3 += (SUINT) W4*row[4] - (SUINT)W6*row[6];
185 
186  MAC(b0, W5, row[5]);
187  MAC(b0, W7, row[7]);
188 
189  MAC(b1, -W1, row[5]);
190  MAC(b1, -W5, row[7]);
191 
192  MAC(b2, W7, row[5]);
193  MAC(b2, W3, row[7]);
194 
195  MAC(b3, W3, row[5]);
196  MAC(b3, -W1, row[7]);
197  }
198 
199  row[0] = (int)(a0 + b0) >> (ROW_SHIFT + extra_shift);
200  row[7] = (int)(a0 - b0) >> (ROW_SHIFT + extra_shift);
201  row[1] = (int)(a1 + b1) >> (ROW_SHIFT + extra_shift);
202  row[6] = (int)(a1 - b1) >> (ROW_SHIFT + extra_shift);
203  row[2] = (int)(a2 + b2) >> (ROW_SHIFT + extra_shift);
204  row[5] = (int)(a2 - b2) >> (ROW_SHIFT + extra_shift);
205  row[3] = (int)(a3 + b3) >> (ROW_SHIFT + extra_shift);
206  row[4] = (int)(a3 - b3) >> (ROW_SHIFT + extra_shift);
207 }
208 
209 #define IDCT_COLS do { \
210  a0 = (SUINT)W4 * (col[8*0] + ((1<<(COL_SHIFT-1))/W4)); \
211  a1 = a0; \
212  a2 = a0; \
213  a3 = a0; \
214  \
215  a0 += (SUINT) W2*col[8*2]; \
216  a1 += (SUINT) W6*col[8*2]; \
217  a2 += (SUINT)-W6*col[8*2]; \
218  a3 += (SUINT)-W2*col[8*2]; \
219  \
220  b0 = MUL(W1, col[8*1]); \
221  b1 = MUL(W3, col[8*1]); \
222  b2 = MUL(W5, col[8*1]); \
223  b3 = MUL(W7, col[8*1]); \
224  \
225  MAC(b0, W3, col[8*3]); \
226  MAC(b1, -W7, col[8*3]); \
227  MAC(b2, -W1, col[8*3]); \
228  MAC(b3, -W5, col[8*3]); \
229  \
230  if (col[8*4]) { \
231  a0 += (SUINT) W4*col[8*4]; \
232  a1 += (SUINT)-W4*col[8*4]; \
233  a2 += (SUINT)-W4*col[8*4]; \
234  a3 += (SUINT) W4*col[8*4]; \
235  } \
236  \
237  if (col[8*5]) { \
238  MAC(b0, W5, col[8*5]); \
239  MAC(b1, -W1, col[8*5]); \
240  MAC(b2, W7, col[8*5]); \
241  MAC(b3, W3, col[8*5]); \
242  } \
243  \
244  if (col[8*6]) { \
245  a0 += (SUINT) W6*col[8*6]; \
246  a1 += (SUINT)-W2*col[8*6]; \
247  a2 += (SUINT) W2*col[8*6]; \
248  a3 += (SUINT)-W6*col[8*6]; \
249  } \
250  \
251  if (col[8*7]) { \
252  MAC(b0, W7, col[8*7]); \
253  MAC(b1, -W5, col[8*7]); \
254  MAC(b2, W3, col[8*7]); \
255  MAC(b3, -W1, col[8*7]); \
256  } \
257  } while (0)
258 
259 #ifdef EXTRA_SHIFT
260 static inline void FUNC(idctSparseCol_extrashift)(int16_t *col)
261 #else
262 static inline void FUNC6(idctSparseCol)(idctin *col)
263 #endif
264 {
265  unsigned a0, a1, a2, a3, b0, b1, b2, b3;
266 
267  IDCT_COLS;
268 
269  col[0 ] = ((int)(a0 + b0) >> COL_SHIFT);
270  col[8 ] = ((int)(a1 + b1) >> COL_SHIFT);
271  col[16] = ((int)(a2 + b2) >> COL_SHIFT);
272  col[24] = ((int)(a3 + b3) >> COL_SHIFT);
273  col[32] = ((int)(a3 - b3) >> COL_SHIFT);
274  col[40] = ((int)(a2 - b2) >> COL_SHIFT);
275  col[48] = ((int)(a1 - b1) >> COL_SHIFT);
276  col[56] = ((int)(a0 - b0) >> COL_SHIFT);
277 }
278 
279 #ifndef PRORES_ONLY
280 #ifndef EXTRA_SHIFT
281 static inline void FUNC6(idctSparseColPut)(pixel *dest, ptrdiff_t line_size,
282  idctin *col)
283 {
284  SUINT a0, a1, a2, a3, b0, b1, b2, b3;
285 
286  IDCT_COLS;
287 
288  dest[0] = av_clip_pixel((int)(a0 + b0) >> COL_SHIFT);
289  dest += line_size;
290  dest[0] = av_clip_pixel((int)(a1 + b1) >> COL_SHIFT);
291  dest += line_size;
292  dest[0] = av_clip_pixel((int)(a2 + b2) >> COL_SHIFT);
293  dest += line_size;
294  dest[0] = av_clip_pixel((int)(a3 + b3) >> COL_SHIFT);
295  dest += line_size;
296  dest[0] = av_clip_pixel((int)(a3 - b3) >> COL_SHIFT);
297  dest += line_size;
298  dest[0] = av_clip_pixel((int)(a2 - b2) >> 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)(a0 - b0) >> COL_SHIFT);
303 }
304 
305 static inline void FUNC6(idctSparseColAdd)(pixel *dest, ptrdiff_t line_size,
306  idctin *col)
307 {
308  unsigned a0, a1, a2, a3, b0, b1, b2, b3;
309 
310  IDCT_COLS;
311 
312  dest[0] = av_clip_pixel(dest[0] + ((int)(a0 + b0) >> COL_SHIFT));
313  dest += line_size;
314  dest[0] = av_clip_pixel(dest[0] + ((int)(a1 + b1) >> COL_SHIFT));
315  dest += line_size;
316  dest[0] = av_clip_pixel(dest[0] + ((int)(a2 + b2) >> COL_SHIFT));
317  dest += line_size;
318  dest[0] = av_clip_pixel(dest[0] + ((int)(a3 + b3) >> COL_SHIFT));
319  dest += line_size;
320  dest[0] = av_clip_pixel(dest[0] + ((int)(a3 - b3) >> COL_SHIFT));
321  dest += line_size;
322  dest[0] = av_clip_pixel(dest[0] + ((int)(a2 - b2) >> 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)(a0 - b0) >> COL_SHIFT));
327 }
328 
329 void FUNC6(ff_simple_idct_put)(uint8_t *dest_, ptrdiff_t line_size, int16_t *block_)
330 {
331  idctin *block = (idctin *)block_;
332  pixel *dest = (pixel *)dest_;
333  int i;
334 
335  line_size /= sizeof(pixel);
336 
337  for (i = 0; i < 8; i++)
338  FUNC6(idctRowCondDC)(block + i*8, 0);
339 
340  for (i = 0; i < 8; i++)
341  FUNC6(idctSparseColPut)(dest + i, line_size, block + i);
342 }
343 
344 #if IN_IDCT_DEPTH == 16
345 void FUNC6(ff_simple_idct_add)(uint8_t *dest_, ptrdiff_t line_size, int16_t *block)
346 {
347  pixel *dest = (pixel *)dest_;
348  int i;
349 
350  line_size /= sizeof(pixel);
351 
352  for (i = 0; i < 8; i++)
353  FUNC6(idctRowCondDC)(block + i*8, 0);
354 
355  for (i = 0; i < 8; i++)
356  FUNC6(idctSparseColAdd)(dest + i, line_size, block + i);
357 }
358 
359 void FUNC6(ff_simple_idct)(int16_t *block)
360 {
361  int i;
362 
363  for (i = 0; i < 8; i++)
364  FUNC6(idctRowCondDC)(block + i*8, 0);
365 
366  for (i = 0; i < 8; i++)
368 }
369 #endif
370 #endif
371 #endif /* PRORES_ONLY */
idctSparseCol
static void FUNC6() idctSparseCol(idctin *col)
Definition: simple_idct_template.c:262
idctSparseColAdd
static void FUNC6() idctSparseColAdd(pixel *dest, ptrdiff_t line_size, idctin *col)
Definition: simple_idct_template.c:305
W4
#define W4
Definition: simple_idct_mmi.c:34
W1
#define W1
Definition: simple_idct_mmi.c:31
AV_WN32A
#define AV_WN32A(p, v)
Definition: intreadwrite.h:534
b1
static double b1(void *priv, double x, double y)
Definition: vf_xfade.c:2034
a2
static double a2(void *priv, double x, double y)
Definition: vf_xfade.c:2030
b3
static double b3(void *priv, double x, double y)
Definition: vf_xfade.c:2036
W2
#define W2
Definition: simple_idct_mmi.c:32
a3
static double a3(void *priv, double x, double y)
Definition: vf_xfade.c:2031
pixel
uint8_t pixel
Definition: tiny_ssim.c:41
W3
#define W3
Definition: simple_idct_mmi.c:33
bit_depth_template.c
MUL
#define MUL(X, Y)
Definition: binkdsp.c:35
W7
#define W7
Definition: simple_idct_mmi.c:37
AV_RN64A
#define AV_RN64A(p)
Definition: intreadwrite.h:526
b2
static double b2(void *priv, double x, double y)
Definition: vf_xfade.c:2035
a0
static double a0(void *priv, double x, double y)
Definition: vf_xfade.c:2028
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
idctSparseColPut
static void FUNC6() idctSparseColPut(pixel *dest, ptrdiff_t line_size, idctin *col)
Definition: simple_idct_template.c:281
SUINT
#define SUINT
Definition: dct32_template.c:30
AV_RN32A
#define AV_RN32A(p)
Definition: intreadwrite.h:522
FUNC6
#define FUNC6(a)
Definition: bit_depth_template.c:105
av_clip_pixel
#define av_clip_pixel(a)
Definition: bit_depth_template.c:95
AV_WN64A
#define AV_WN64A(p, v)
Definition: intreadwrite.h:538
ff_simple_idct_put
void FUNC6() ff_simple_idct_put(uint8_t *dest_, ptrdiff_t line_size, int16_t *block_)
Definition: simple_idct_template.c:329
FUNC
#define FUNC(a)
Definition: bit_depth_template.c:101
temp
else temp
Definition: vf_mcdeint.c:263
ROW_SHIFT
#define ROW_SHIFT
Definition: simple_idct_mmi.c:39
IDCT_COLS
#define IDCT_COLS
Definition: simple_idct_template.c:209
idctin
#define idctin
Definition: bit_depth_template.c:83
COL_SHIFT
#define COL_SHIFT
Definition: simple_idct_mmi.c:40
DC_SHIFT
#define DC_SHIFT
Definition: simple_idct_mmi.c:41
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
b0
static double b0(void *priv, double x, double y)
Definition: vf_xfade.c:2033
a1
static double a1(void *priv, double x, double y)
Definition: vf_xfade.c:2029
W6
#define W6
Definition: simple_idct_mmi.c:36
idctRowCondDC
static void FUNC6() idctRowCondDC(idctin *row, int extra_shift)
Definition: simple_idct_template.c:114
W5
#define W5
Definition: simple_idct_mmi.c:35