2 * This file is part of the Independent JPEG Group's software.
4 * The authors make NO WARRANTY or representation, either express or implied,
5 * with respect to this software, its quality, accuracy, merchantability, or
6 * fitness for a particular purpose. This software is provided "AS IS", and
7 * you, its user, assume the entire risk as to its quality and accuracy.
9 * This software is copyright (C) 1991, 1992, Thomas G. Lane.
10 * All Rights Reserved except as specified below.
12 * Permission is hereby granted to use, copy, modify, and distribute this
13 * software (or portions thereof) for any purpose, without fee, subject to
15 * (1) If any part of the source code for this software is distributed, then
16 * this README file must be included, with this copyright and no-warranty
17 * notice unaltered; and any additions, deletions, or changes to the original
18 * files must be clearly indicated in accompanying documentation.
19 * (2) If only executable code is distributed, then the accompanying
20 * documentation must state that "this software is based in part on the work
21 * of the Independent JPEG Group".
22 * (3) Permission for use of this software is granted only if the user accepts
23 * full responsibility for any undesirable consequences; the authors accept
24 * NO LIABILITY for damages of any kind.
26 * These conditions apply to any software derived from or based on the IJG
27 * code, not just to the unmodified library. If you use our work, you ought
30 * Permission is NOT granted for the use of any IJG author's name or company
31 * name in advertising or publicity relating to this software or products
32 * derived from it. This software may be referred to only as "the Independent
33 * JPEG Group's software".
35 * We specifically permit and encourage the use of this software as the basis
36 * of commercial products, provided that all warranty or liability claims are
37 * assumed by the product vendor.
39 * This file contains the basic inverse-DCT transformation subroutine.
41 * This implementation is based on an algorithm described in
42 * C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
43 * Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
44 * Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
45 * The primary algorithm described there uses 11 multiplies and 29 adds.
46 * We use their alternate method with 12 multiplies and 32 adds.
47 * The advantage of this method is that no data path contains more than one
48 * multiplication; this allows a very simple and accurate implementation in
49 * scaled fixed-point arithmetic, with a minimal number of shifts.
51 * I've made lots of modifications to attempt to take advantage of the
52 * sparse nature of the DCT matrices we're getting. Although the logic
53 * is cumbersome, it's straightforward and the resulting code is much
56 * A better way to do this would be to pass in the DCT block as a sparse
57 * matrix, perhaps with the difference cases encoded.
62 * Independent JPEG Group's LLM idct.
65 #include "libavutil/common.h"
66 #include "libavutil/intreadwrite.h"
71 #define EIGHT_BIT_SAMPLES
78 #define RIGHT_SHIFT(x, n) ((x) >> (n))
80 typedef int16_t DCTBLOCK[DCTSIZE2];
85 * This routine is specialized to the case DCTSIZE = 8.
89 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
94 * A 2-D IDCT can be done by 1-D IDCT on each row followed by 1-D IDCT
95 * on each column. Direct algorithms are also available, but they are
96 * much more complex and seem not to be any faster when reduced to code.
98 * The poop on this scaling stuff is as follows:
100 * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
101 * larger than the true IDCT outputs. The final outputs are therefore
102 * a factor of N larger than desired; since N=8 this can be cured by
103 * a simple right shift at the end of the algorithm. The advantage of
104 * this arrangement is that we save two multiplications per 1-D IDCT,
105 * because the y0 and y4 inputs need not be divided by sqrt(N).
107 * We have to do addition and subtraction of the integer inputs, which
108 * is no problem, and multiplication by fractional constants, which is
109 * a problem to do in integer arithmetic. We multiply all the constants
110 * by CONST_SCALE and convert them to integer constants (thus retaining
111 * CONST_BITS bits of precision in the constants). After doing a
112 * multiplication we have to divide the product by CONST_SCALE, with proper
113 * rounding, to produce the correct output. This division can be done
114 * cheaply as a right shift of CONST_BITS bits. We postpone shifting
115 * as long as possible so that partial sums can be added together with
116 * full fractional precision.
118 * The outputs of the first pass are scaled up by PASS1_BITS bits so that
119 * they are represented to better-than-integral precision. These outputs
120 * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
121 * with the recommended scaling. (To scale up 12-bit sample data further, an
122 * intermediate int32 array would be needed.)
124 * To avoid overflow of the 32-bit intermediate results in pass 2, we must
125 * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
126 * shows that the values given below are the most effective.
129 #ifdef EIGHT_BIT_SAMPLES
132 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
135 #define ONE ((int32_t) 1)
137 #define CONST_SCALE (ONE << CONST_BITS)
139 /* Convert a positive real constant to an integer scaled by CONST_SCALE.
140 * IMPORTANT: if your compiler doesn't do this arithmetic at compile time,
141 * you will pay a significant penalty in run time. In that case, figure
142 * the correct integer constant values and insert them by hand.
145 /* Actually FIX is no longer used, we precomputed them all */
146 #define FIX(x) ((int32_t) ((x) * CONST_SCALE + 0.5))
148 /* Descale and correctly round an int32_t value that's scaled by N bits.
149 * We assume RIGHT_SHIFT rounds towards minus infinity, so adding
150 * the fudge factor is correct for either sign of X.
153 #define DESCALE(x,n) RIGHT_SHIFT((x) + (ONE << ((n)-1)), n)
155 /* Multiply an int32_t variable by an int32_t constant to yield an int32_t result.
156 * For 8-bit samples with the recommended scaling, all the variable
157 * and constant values involved are no more than 16 bits wide, so a
158 * 16x16->32 bit multiply can be used instead of a full 32x32 multiply;
159 * this provides a useful speedup on many machines.
160 * There is no way to specify a 16x16->32 multiply in portable C, but
161 * some C compilers will do the right thing if you provide the correct
162 * combination of casts.
163 * NB: for 12-bit samples, a full 32-bit multiplication will be needed.
166 #ifdef EIGHT_BIT_SAMPLES
167 #ifdef SHORTxSHORT_32 /* may work if 'int' is 32 bits */
168 #define MULTIPLY(var,const) (((int16_t) (var)) * ((int16_t) (const)))
170 #ifdef SHORTxLCONST_32 /* known to work with Microsoft C 6.0 */
171 #define MULTIPLY(var,const) (((int16_t) (var)) * ((int32_t) (const)))
175 #ifndef MULTIPLY /* default definition */
176 #define MULTIPLY(var,const) ((var) * (const))
181 Unlike our decoder where we approximate the FIXes, we need to use exact
182 ones here or successive P-frames will drift too much with Reference frame coding
184 #define FIX_0_211164243 1730
185 #define FIX_0_275899380 2260
186 #define FIX_0_298631336 2446
187 #define FIX_0_390180644 3196
188 #define FIX_0_509795579 4176
189 #define FIX_0_541196100 4433
190 #define FIX_0_601344887 4926
191 #define FIX_0_765366865 6270
192 #define FIX_0_785694958 6436
193 #define FIX_0_899976223 7373
194 #define FIX_1_061594337 8697
195 #define FIX_1_111140466 9102
196 #define FIX_1_175875602 9633
197 #define FIX_1_306562965 10703
198 #define FIX_1_387039845 11363
199 #define FIX_1_451774981 11893
200 #define FIX_1_501321110 12299
201 #define FIX_1_662939225 13623
202 #define FIX_1_847759065 15137
203 #define FIX_1_961570560 16069
204 #define FIX_2_053119869 16819
205 #define FIX_2_172734803 17799
206 #define FIX_2_562915447 20995
207 #define FIX_3_072711026 25172
210 * Perform the inverse DCT on one block of coefficients.
213 void ff_j_rev_dct(DCTBLOCK data)
215 int32_t tmp0, tmp1, tmp2, tmp3;
216 int32_t tmp10, tmp11, tmp12, tmp13;
217 int32_t z1, z2, z3, z4, z5;
218 int32_t d0, d1, d2, d3, d4, d5, d6, d7;
219 register int16_t *dataptr;
222 /* Pass 1: process rows. */
223 /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
224 /* furthermore, we scale the results by 2**PASS1_BITS. */
228 for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
229 /* Due to quantization, we will usually find that many of the input
230 * coefficients are zero, especially the AC terms. We can exploit this
231 * by short-circuiting the IDCT calculation for any row in which all
232 * the AC terms are zero. In that case each output is equal to the
233 * DC coefficient (with scale factor as needed).
234 * With typical images and quantization tables, half or more of the
235 * row DCT calculations can be simplified this way.
238 register uint8_t *idataptr = (uint8_t*)dataptr;
240 /* WARNING: we do the same permutation as MMX idct to simplify the
251 if ((d1 | d2 | d3 | d4 | d5 | d6 | d7) == 0) {
252 /* AC terms all zero */
254 /* Compute a 32 bit value to assign. */
255 int16_t dcval = (int16_t) (d0 * (1 << PASS1_BITS));
256 register int v = (dcval & 0xffff) | ((dcval * (1 << 16)) & 0xffff0000);
258 AV_WN32A(&idataptr[ 0], v);
259 AV_WN32A(&idataptr[ 4], v);
260 AV_WN32A(&idataptr[ 8], v);
261 AV_WN32A(&idataptr[12], v);
264 dataptr += DCTSIZE; /* advance pointer to next row */
268 /* Even part: reverse the even part of the forward DCT. */
269 /* The rotator is sqrt(2)*c(-6). */
273 /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
274 z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
275 tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
276 tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
278 tmp0 = (d0 + d4) * CONST_SCALE;
279 tmp1 = (d0 - d4) * CONST_SCALE;
286 /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
287 tmp2 = MULTIPLY(-d6, FIX_1_306562965);
288 tmp3 = MULTIPLY(d6, FIX_0_541196100);
290 tmp0 = (d0 + d4) * CONST_SCALE;
291 tmp1 = (d0 - d4) * CONST_SCALE;
300 /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
301 tmp2 = MULTIPLY(d2, FIX_0_541196100);
302 tmp3 = MULTIPLY(d2, FIX_1_306562965);
304 tmp0 = (d0 + d4) * CONST_SCALE;
305 tmp1 = (d0 - d4) * CONST_SCALE;
312 /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
313 tmp10 = tmp13 = (d0 + d4) * CONST_SCALE;
314 tmp11 = tmp12 = (d0 - d4) * CONST_SCALE;
318 /* Odd part per figure 8; the matrix is unitary and hence its
319 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
326 /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */
331 z5 = MULTIPLY(z3 + z4, FIX_1_175875602);
333 tmp0 = MULTIPLY(d7, FIX_0_298631336);
334 tmp1 = MULTIPLY(d5, FIX_2_053119869);
335 tmp2 = MULTIPLY(d3, FIX_3_072711026);
336 tmp3 = MULTIPLY(d1, FIX_1_501321110);
337 z1 = MULTIPLY(-z1, FIX_0_899976223);
338 z2 = MULTIPLY(-z2, FIX_2_562915447);
339 z3 = MULTIPLY(-z3, FIX_1_961570560);
340 z4 = MULTIPLY(-z4, FIX_0_390180644);
350 /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */
353 z5 = MULTIPLY(z3 + d5, FIX_1_175875602);
355 tmp0 = MULTIPLY(d7, FIX_0_298631336);
356 tmp1 = MULTIPLY(d5, FIX_2_053119869);
357 tmp2 = MULTIPLY(d3, FIX_3_072711026);
358 z1 = MULTIPLY(-d7, FIX_0_899976223);
359 z2 = MULTIPLY(-z2, FIX_2_562915447);
360 z3 = MULTIPLY(-z3, FIX_1_961570560);
361 z4 = MULTIPLY(-d5, FIX_0_390180644);
373 /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */
376 z5 = MULTIPLY(d7 + z4, FIX_1_175875602);
378 tmp0 = MULTIPLY(d7, FIX_0_298631336);
379 tmp1 = MULTIPLY(d5, FIX_2_053119869);
380 tmp3 = MULTIPLY(d1, FIX_1_501321110);
381 z1 = MULTIPLY(-z1, FIX_0_899976223);
382 z2 = MULTIPLY(-d5, FIX_2_562915447);
383 z3 = MULTIPLY(-d7, FIX_1_961570560);
384 z4 = MULTIPLY(-z4, FIX_0_390180644);
394 /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */
395 tmp0 = MULTIPLY(-d7, FIX_0_601344887);
396 z1 = MULTIPLY(-d7, FIX_0_899976223);
397 z3 = MULTIPLY(-d7, FIX_1_961570560);
398 tmp1 = MULTIPLY(-d5, FIX_0_509795579);
399 z2 = MULTIPLY(-d5, FIX_2_562915447);
400 z4 = MULTIPLY(-d5, FIX_0_390180644);
401 z5 = MULTIPLY(d5 + d7, FIX_1_175875602);
415 /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */
418 z5 = MULTIPLY(z3 + d1, FIX_1_175875602);
420 tmp0 = MULTIPLY(d7, FIX_0_298631336);
421 tmp2 = MULTIPLY(d3, FIX_3_072711026);
422 tmp3 = MULTIPLY(d1, FIX_1_501321110);
423 z1 = MULTIPLY(-z1, FIX_0_899976223);
424 z2 = MULTIPLY(-d3, FIX_2_562915447);
425 z3 = MULTIPLY(-z3, FIX_1_961570560);
426 z4 = MULTIPLY(-d1, FIX_0_390180644);
436 /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */
439 tmp0 = MULTIPLY(-d7, FIX_0_601344887);
440 z1 = MULTIPLY(-d7, FIX_0_899976223);
441 tmp2 = MULTIPLY(d3, FIX_0_509795579);
442 z2 = MULTIPLY(-d3, FIX_2_562915447);
443 z5 = MULTIPLY(z3, FIX_1_175875602);
444 z3 = MULTIPLY(-z3, FIX_0_785694958);
453 /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */
455 z5 = MULTIPLY(z1, FIX_1_175875602);
457 z1 = MULTIPLY(z1, FIX_0_275899380);
458 z3 = MULTIPLY(-d7, FIX_1_961570560);
459 tmp0 = MULTIPLY(-d7, FIX_1_662939225);
460 z4 = MULTIPLY(-d1, FIX_0_390180644);
461 tmp3 = MULTIPLY(d1, FIX_1_111140466);
468 /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */
469 tmp0 = MULTIPLY(-d7, FIX_1_387039845);
470 tmp1 = MULTIPLY(d7, FIX_1_175875602);
471 tmp2 = MULTIPLY(-d7, FIX_0_785694958);
472 tmp3 = MULTIPLY(d7, FIX_0_275899380);
480 /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */
483 z5 = MULTIPLY(d3 + z4, FIX_1_175875602);
485 tmp1 = MULTIPLY(d5, FIX_2_053119869);
486 tmp2 = MULTIPLY(d3, FIX_3_072711026);
487 tmp3 = MULTIPLY(d1, FIX_1_501321110);
488 z1 = MULTIPLY(-d1, FIX_0_899976223);
489 z2 = MULTIPLY(-z2, FIX_2_562915447);
490 z3 = MULTIPLY(-d3, FIX_1_961570560);
491 z4 = MULTIPLY(-z4, FIX_0_390180644);
501 /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */
504 z5 = MULTIPLY(z2, FIX_1_175875602);
505 tmp1 = MULTIPLY(d5, FIX_1_662939225);
506 z4 = MULTIPLY(-d5, FIX_0_390180644);
507 z2 = MULTIPLY(-z2, FIX_1_387039845);
508 tmp2 = MULTIPLY(d3, FIX_1_111140466);
509 z3 = MULTIPLY(-d3, FIX_1_961570560);
518 /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */
521 z5 = MULTIPLY(z4, FIX_1_175875602);
522 z1 = MULTIPLY(-d1, FIX_0_899976223);
523 tmp3 = MULTIPLY(d1, FIX_0_601344887);
524 tmp1 = MULTIPLY(-d5, FIX_0_509795579);
525 z2 = MULTIPLY(-d5, FIX_2_562915447);
526 z4 = MULTIPLY(z4, FIX_0_785694958);
533 /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */
534 tmp0 = MULTIPLY(d5, FIX_1_175875602);
535 tmp1 = MULTIPLY(d5, FIX_0_275899380);
536 tmp2 = MULTIPLY(-d5, FIX_1_387039845);
537 tmp3 = MULTIPLY(d5, FIX_0_785694958);
543 /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */
545 tmp3 = MULTIPLY(d1, FIX_0_211164243);
546 tmp2 = MULTIPLY(-d3, FIX_1_451774981);
547 z1 = MULTIPLY(d1, FIX_1_061594337);
548 z2 = MULTIPLY(-d3, FIX_2_172734803);
549 z4 = MULTIPLY(z5, FIX_0_785694958);
550 z5 = MULTIPLY(z5, FIX_1_175875602);
557 /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */
558 tmp0 = MULTIPLY(-d3, FIX_0_785694958);
559 tmp1 = MULTIPLY(-d3, FIX_1_387039845);
560 tmp2 = MULTIPLY(-d3, FIX_0_275899380);
561 tmp3 = MULTIPLY(d3, FIX_1_175875602);
565 /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */
566 tmp0 = MULTIPLY(d1, FIX_0_275899380);
567 tmp1 = MULTIPLY(d1, FIX_0_785694958);
568 tmp2 = MULTIPLY(d1, FIX_1_175875602);
569 tmp3 = MULTIPLY(d1, FIX_1_387039845);
571 /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */
572 tmp0 = tmp1 = tmp2 = tmp3 = 0;
578 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
580 dataptr[0] = (int16_t) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
581 dataptr[7] = (int16_t) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
582 dataptr[1] = (int16_t) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
583 dataptr[6] = (int16_t) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
584 dataptr[2] = (int16_t) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
585 dataptr[5] = (int16_t) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
586 dataptr[3] = (int16_t) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
587 dataptr[4] = (int16_t) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
589 dataptr += DCTSIZE; /* advance pointer to next row */
592 /* Pass 2: process columns. */
593 /* Note that we must descale the results by a factor of 8 == 2**3, */
594 /* and also undo the PASS1_BITS scaling. */
597 for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
598 /* Columns of zeroes can be exploited in the same way as we did with rows.
599 * However, the row calculation has created many nonzero AC terms, so the
600 * simplification applies less often (typically 5% to 10% of the time).
601 * On machines with very fast multiplication, it's possible that the
602 * test takes more time than it's worth. In that case this section
603 * may be commented out.
606 d0 = dataptr[DCTSIZE*0];
607 d1 = dataptr[DCTSIZE*1];
608 d2 = dataptr[DCTSIZE*2];
609 d3 = dataptr[DCTSIZE*3];
610 d4 = dataptr[DCTSIZE*4];
611 d5 = dataptr[DCTSIZE*5];
612 d6 = dataptr[DCTSIZE*6];
613 d7 = dataptr[DCTSIZE*7];
615 /* Even part: reverse the even part of the forward DCT. */
616 /* The rotator is sqrt(2)*c(-6). */
619 /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
620 z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
621 tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
622 tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
624 tmp0 = (d0 + d4) * CONST_SCALE;
625 tmp1 = (d0 - d4) * CONST_SCALE;
632 /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
633 tmp2 = MULTIPLY(-d6, FIX_1_306562965);
634 tmp3 = MULTIPLY(d6, FIX_0_541196100);
636 tmp0 = (d0 + d4) * CONST_SCALE;
637 tmp1 = (d0 - d4) * CONST_SCALE;
646 /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
647 tmp2 = MULTIPLY(d2, FIX_0_541196100);
648 tmp3 = MULTIPLY(d2, FIX_1_306562965);
650 tmp0 = (d0 + d4) * CONST_SCALE;
651 tmp1 = (d0 - d4) * CONST_SCALE;
658 /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
659 tmp10 = tmp13 = (d0 + d4) * CONST_SCALE;
660 tmp11 = tmp12 = (d0 - d4) * CONST_SCALE;
664 /* Odd part per figure 8; the matrix is unitary and hence its
665 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
671 /* d1 != 0, d3 != 0, d5 != 0, d7 != 0 */
676 z5 = MULTIPLY(z3 + z4, FIX_1_175875602);
678 tmp0 = MULTIPLY(d7, FIX_0_298631336);
679 tmp1 = MULTIPLY(d5, FIX_2_053119869);
680 tmp2 = MULTIPLY(d3, FIX_3_072711026);
681 tmp3 = MULTIPLY(d1, FIX_1_501321110);
682 z1 = MULTIPLY(-z1, FIX_0_899976223);
683 z2 = MULTIPLY(-z2, FIX_2_562915447);
684 z3 = MULTIPLY(-z3, FIX_1_961570560);
685 z4 = MULTIPLY(-z4, FIX_0_390180644);
695 /* d1 == 0, d3 != 0, d5 != 0, d7 != 0 */
698 z5 = MULTIPLY(z3 + d5, FIX_1_175875602);
700 tmp0 = MULTIPLY(d7, FIX_0_298631336);
701 tmp1 = MULTIPLY(d5, FIX_2_053119869);
702 tmp2 = MULTIPLY(d3, FIX_3_072711026);
703 z1 = MULTIPLY(-d7, FIX_0_899976223);
704 z2 = MULTIPLY(-z2, FIX_2_562915447);
705 z3 = MULTIPLY(-z3, FIX_1_961570560);
706 z4 = MULTIPLY(-d5, FIX_0_390180644);
718 /* d1 != 0, d3 == 0, d5 != 0, d7 != 0 */
722 z5 = MULTIPLY(z3 + z4, FIX_1_175875602);
724 tmp0 = MULTIPLY(d7, FIX_0_298631336);
725 tmp1 = MULTIPLY(d5, FIX_2_053119869);
726 tmp3 = MULTIPLY(d1, FIX_1_501321110);
727 z1 = MULTIPLY(-z1, FIX_0_899976223);
728 z2 = MULTIPLY(-d5, FIX_2_562915447);
729 z3 = MULTIPLY(-d7, FIX_1_961570560);
730 z4 = MULTIPLY(-z4, FIX_0_390180644);
740 /* d1 == 0, d3 == 0, d5 != 0, d7 != 0 */
741 tmp0 = MULTIPLY(-d7, FIX_0_601344887);
742 z1 = MULTIPLY(-d7, FIX_0_899976223);
743 z3 = MULTIPLY(-d7, FIX_1_961570560);
744 tmp1 = MULTIPLY(-d5, FIX_0_509795579);
745 z2 = MULTIPLY(-d5, FIX_2_562915447);
746 z4 = MULTIPLY(-d5, FIX_0_390180644);
747 z5 = MULTIPLY(d5 + d7, FIX_1_175875602);
761 /* d1 != 0, d3 != 0, d5 == 0, d7 != 0 */
764 z5 = MULTIPLY(z3 + d1, FIX_1_175875602);
766 tmp0 = MULTIPLY(d7, FIX_0_298631336);
767 tmp2 = MULTIPLY(d3, FIX_3_072711026);
768 tmp3 = MULTIPLY(d1, FIX_1_501321110);
769 z1 = MULTIPLY(-z1, FIX_0_899976223);
770 z2 = MULTIPLY(-d3, FIX_2_562915447);
771 z3 = MULTIPLY(-z3, FIX_1_961570560);
772 z4 = MULTIPLY(-d1, FIX_0_390180644);
782 /* d1 == 0, d3 != 0, d5 == 0, d7 != 0 */
785 tmp0 = MULTIPLY(-d7, FIX_0_601344887);
786 z1 = MULTIPLY(-d7, FIX_0_899976223);
787 tmp2 = MULTIPLY(d3, FIX_0_509795579);
788 z2 = MULTIPLY(-d3, FIX_2_562915447);
789 z5 = MULTIPLY(z3, FIX_1_175875602);
790 z3 = MULTIPLY(-z3, FIX_0_785694958);
799 /* d1 != 0, d3 == 0, d5 == 0, d7 != 0 */
801 z5 = MULTIPLY(z1, FIX_1_175875602);
803 z1 = MULTIPLY(z1, FIX_0_275899380);
804 z3 = MULTIPLY(-d7, FIX_1_961570560);
805 tmp0 = MULTIPLY(-d7, FIX_1_662939225);
806 z4 = MULTIPLY(-d1, FIX_0_390180644);
807 tmp3 = MULTIPLY(d1, FIX_1_111140466);
814 /* d1 == 0, d3 == 0, d5 == 0, d7 != 0 */
815 tmp0 = MULTIPLY(-d7, FIX_1_387039845);
816 tmp1 = MULTIPLY(d7, FIX_1_175875602);
817 tmp2 = MULTIPLY(-d7, FIX_0_785694958);
818 tmp3 = MULTIPLY(d7, FIX_0_275899380);
826 /* d1 != 0, d3 != 0, d5 != 0, d7 == 0 */
829 z5 = MULTIPLY(d3 + z4, FIX_1_175875602);
831 tmp1 = MULTIPLY(d5, FIX_2_053119869);
832 tmp2 = MULTIPLY(d3, FIX_3_072711026);
833 tmp3 = MULTIPLY(d1, FIX_1_501321110);
834 z1 = MULTIPLY(-d1, FIX_0_899976223);
835 z2 = MULTIPLY(-z2, FIX_2_562915447);
836 z3 = MULTIPLY(-d3, FIX_1_961570560);
837 z4 = MULTIPLY(-z4, FIX_0_390180644);
847 /* d1 == 0, d3 != 0, d5 != 0, d7 == 0 */
850 z5 = MULTIPLY(z2, FIX_1_175875602);
851 tmp1 = MULTIPLY(d5, FIX_1_662939225);
852 z4 = MULTIPLY(-d5, FIX_0_390180644);
853 z2 = MULTIPLY(-z2, FIX_1_387039845);
854 tmp2 = MULTIPLY(d3, FIX_1_111140466);
855 z3 = MULTIPLY(-d3, FIX_1_961570560);
864 /* d1 != 0, d3 == 0, d5 != 0, d7 == 0 */
867 z5 = MULTIPLY(z4, FIX_1_175875602);
868 z1 = MULTIPLY(-d1, FIX_0_899976223);
869 tmp3 = MULTIPLY(d1, FIX_0_601344887);
870 tmp1 = MULTIPLY(-d5, FIX_0_509795579);
871 z2 = MULTIPLY(-d5, FIX_2_562915447);
872 z4 = MULTIPLY(z4, FIX_0_785694958);
879 /* d1 == 0, d3 == 0, d5 != 0, d7 == 0 */
880 tmp0 = MULTIPLY(d5, FIX_1_175875602);
881 tmp1 = MULTIPLY(d5, FIX_0_275899380);
882 tmp2 = MULTIPLY(-d5, FIX_1_387039845);
883 tmp3 = MULTIPLY(d5, FIX_0_785694958);
889 /* d1 != 0, d3 != 0, d5 == 0, d7 == 0 */
891 tmp3 = MULTIPLY(d1, FIX_0_211164243);
892 tmp2 = MULTIPLY(-d3, FIX_1_451774981);
893 z1 = MULTIPLY(d1, FIX_1_061594337);
894 z2 = MULTIPLY(-d3, FIX_2_172734803);
895 z4 = MULTIPLY(z5, FIX_0_785694958);
896 z5 = MULTIPLY(z5, FIX_1_175875602);
903 /* d1 == 0, d3 != 0, d5 == 0, d7 == 0 */
904 tmp0 = MULTIPLY(-d3, FIX_0_785694958);
905 tmp1 = MULTIPLY(-d3, FIX_1_387039845);
906 tmp2 = MULTIPLY(-d3, FIX_0_275899380);
907 tmp3 = MULTIPLY(d3, FIX_1_175875602);
911 /* d1 != 0, d3 == 0, d5 == 0, d7 == 0 */
912 tmp0 = MULTIPLY(d1, FIX_0_275899380);
913 tmp1 = MULTIPLY(d1, FIX_0_785694958);
914 tmp2 = MULTIPLY(d1, FIX_1_175875602);
915 tmp3 = MULTIPLY(d1, FIX_1_387039845);
917 /* d1 == 0, d3 == 0, d5 == 0, d7 == 0 */
918 tmp0 = tmp1 = tmp2 = tmp3 = 0;
924 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
926 dataptr[DCTSIZE*0] = (int16_t) DESCALE(tmp10 + tmp3,
927 CONST_BITS+PASS1_BITS+3);
928 dataptr[DCTSIZE*7] = (int16_t) DESCALE(tmp10 - tmp3,
929 CONST_BITS+PASS1_BITS+3);
930 dataptr[DCTSIZE*1] = (int16_t) DESCALE(tmp11 + tmp2,
931 CONST_BITS+PASS1_BITS+3);
932 dataptr[DCTSIZE*6] = (int16_t) DESCALE(tmp11 - tmp2,
933 CONST_BITS+PASS1_BITS+3);
934 dataptr[DCTSIZE*2] = (int16_t) DESCALE(tmp12 + tmp1,
935 CONST_BITS+PASS1_BITS+3);
936 dataptr[DCTSIZE*5] = (int16_t) DESCALE(tmp12 - tmp1,
937 CONST_BITS+PASS1_BITS+3);
938 dataptr[DCTSIZE*3] = (int16_t) DESCALE(tmp13 + tmp0,
939 CONST_BITS+PASS1_BITS+3);
940 dataptr[DCTSIZE*4] = (int16_t) DESCALE(tmp13 - tmp0,
941 CONST_BITS+PASS1_BITS+3);
943 dataptr++; /* advance pointer to next column */
951 void ff_j_rev_dct4(DCTBLOCK data)
953 int32_t tmp0, tmp1, tmp2, tmp3;
954 int32_t tmp10, tmp11, tmp12, tmp13;
956 int32_t d0, d2, d4, d6;
957 register int16_t *dataptr;
960 /* Pass 1: process rows. */
961 /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
962 /* furthermore, we scale the results by 2**PASS1_BITS. */
968 for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
969 /* Due to quantization, we will usually find that many of the input
970 * coefficients are zero, especially the AC terms. We can exploit this
971 * by short-circuiting the IDCT calculation for any row in which all
972 * the AC terms are zero. In that case each output is equal to the
973 * DC coefficient (with scale factor as needed).
974 * With typical images and quantization tables, half or more of the
975 * row DCT calculations can be simplified this way.
978 register uint8_t *idataptr = (uint8_t*)dataptr;
985 if ((d2 | d4 | d6) == 0) {
986 /* AC terms all zero */
988 /* Compute a 32 bit value to assign. */
989 int16_t dcval = (int16_t) (d0 << PASS1_BITS);
990 register int v = (dcval & 0xffff) | ((dcval << 16) & 0xffff0000);
992 AV_WN32A(&idataptr[0], v);
993 AV_WN32A(&idataptr[4], v);
996 dataptr += DCTSTRIDE; /* advance pointer to next row */
1000 /* Even part: reverse the even part of the forward DCT. */
1001 /* The rotator is sqrt(2)*c(-6). */
1004 /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
1005 z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
1006 tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
1007 tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
1009 tmp0 = (d0 + d4) << CONST_BITS;
1010 tmp1 = (d0 - d4) << CONST_BITS;
1012 tmp10 = tmp0 + tmp3;
1013 tmp13 = tmp0 - tmp3;
1014 tmp11 = tmp1 + tmp2;
1015 tmp12 = tmp1 - tmp2;
1017 /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
1018 tmp2 = MULTIPLY(-d6, FIX_1_306562965);
1019 tmp3 = MULTIPLY(d6, FIX_0_541196100);
1021 tmp0 = (d0 + d4) << CONST_BITS;
1022 tmp1 = (d0 - d4) << CONST_BITS;
1024 tmp10 = tmp0 + tmp3;
1025 tmp13 = tmp0 - tmp3;
1026 tmp11 = tmp1 + tmp2;
1027 tmp12 = tmp1 - tmp2;
1031 /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
1032 tmp2 = MULTIPLY(d2, FIX_0_541196100);
1033 tmp3 = MULTIPLY(d2, FIX_1_306562965);
1035 tmp0 = (d0 + d4) << CONST_BITS;
1036 tmp1 = (d0 - d4) << CONST_BITS;
1038 tmp10 = tmp0 + tmp3;
1039 tmp13 = tmp0 - tmp3;
1040 tmp11 = tmp1 + tmp2;
1041 tmp12 = tmp1 - tmp2;
1043 /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
1044 tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
1045 tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
1049 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
1051 dataptr[0] = (int16_t) DESCALE(tmp10, CONST_BITS-PASS1_BITS);
1052 dataptr[1] = (int16_t) DESCALE(tmp11, CONST_BITS-PASS1_BITS);
1053 dataptr[2] = (int16_t) DESCALE(tmp12, CONST_BITS-PASS1_BITS);
1054 dataptr[3] = (int16_t) DESCALE(tmp13, CONST_BITS-PASS1_BITS);
1056 dataptr += DCTSTRIDE; /* advance pointer to next row */
1059 /* Pass 2: process columns. */
1060 /* Note that we must descale the results by a factor of 8 == 2**3, */
1061 /* and also undo the PASS1_BITS scaling. */
1064 for (rowctr = DCTSIZE-1; rowctr >= 0; rowctr--) {
1065 /* Columns of zeroes can be exploited in the same way as we did with rows.
1066 * However, the row calculation has created many nonzero AC terms, so the
1067 * simplification applies less often (typically 5% to 10% of the time).
1068 * On machines with very fast multiplication, it's possible that the
1069 * test takes more time than it's worth. In that case this section
1070 * may be commented out.
1073 d0 = dataptr[DCTSTRIDE*0];
1074 d2 = dataptr[DCTSTRIDE*1];
1075 d4 = dataptr[DCTSTRIDE*2];
1076 d6 = dataptr[DCTSTRIDE*3];
1078 /* Even part: reverse the even part of the forward DCT. */
1079 /* The rotator is sqrt(2)*c(-6). */
1082 /* d0 != 0, d2 != 0, d4 != 0, d6 != 0 */
1083 z1 = MULTIPLY(d2 + d6, FIX_0_541196100);
1084 tmp2 = z1 + MULTIPLY(-d6, FIX_1_847759065);
1085 tmp3 = z1 + MULTIPLY(d2, FIX_0_765366865);
1087 tmp0 = (d0 + d4) << CONST_BITS;
1088 tmp1 = (d0 - d4) << CONST_BITS;
1090 tmp10 = tmp0 + tmp3;
1091 tmp13 = tmp0 - tmp3;
1092 tmp11 = tmp1 + tmp2;
1093 tmp12 = tmp1 - tmp2;
1095 /* d0 != 0, d2 == 0, d4 != 0, d6 != 0 */
1096 tmp2 = MULTIPLY(-d6, FIX_1_306562965);
1097 tmp3 = MULTIPLY(d6, FIX_0_541196100);
1099 tmp0 = (d0 + d4) << CONST_BITS;
1100 tmp1 = (d0 - d4) << CONST_BITS;
1102 tmp10 = tmp0 + tmp3;
1103 tmp13 = tmp0 - tmp3;
1104 tmp11 = tmp1 + tmp2;
1105 tmp12 = tmp1 - tmp2;
1109 /* d0 != 0, d2 != 0, d4 != 0, d6 == 0 */
1110 tmp2 = MULTIPLY(d2, FIX_0_541196100);
1111 tmp3 = MULTIPLY(d2, FIX_1_306562965);
1113 tmp0 = (d0 + d4) << CONST_BITS;
1114 tmp1 = (d0 - d4) << CONST_BITS;
1116 tmp10 = tmp0 + tmp3;
1117 tmp13 = tmp0 - tmp3;
1118 tmp11 = tmp1 + tmp2;
1119 tmp12 = tmp1 - tmp2;
1121 /* d0 != 0, d2 == 0, d4 != 0, d6 == 0 */
1122 tmp10 = tmp13 = (d0 + d4) << CONST_BITS;
1123 tmp11 = tmp12 = (d0 - d4) << CONST_BITS;
1127 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
1129 dataptr[DCTSTRIDE*0] = tmp10 >> (CONST_BITS+PASS1_BITS+3);
1130 dataptr[DCTSTRIDE*1] = tmp11 >> (CONST_BITS+PASS1_BITS+3);
1131 dataptr[DCTSTRIDE*2] = tmp12 >> (CONST_BITS+PASS1_BITS+3);
1132 dataptr[DCTSTRIDE*3] = tmp13 >> (CONST_BITS+PASS1_BITS+3);
1134 dataptr++; /* advance pointer to next column */
1138 void ff_j_rev_dct2(DCTBLOCK data){
1139 int d00, d01, d10, d11;
1142 d00 = data[0+0*DCTSTRIDE] + data[1+0*DCTSTRIDE];
1143 d01 = data[0+0*DCTSTRIDE] - data[1+0*DCTSTRIDE];
1144 d10 = data[0+1*DCTSTRIDE] + data[1+1*DCTSTRIDE];
1145 d11 = data[0+1*DCTSTRIDE] - data[1+1*DCTSTRIDE];
1147 data[0+0*DCTSTRIDE]= (d00 + d10)>>3;
1148 data[1+0*DCTSTRIDE]= (d01 + d11)>>3;
1149 data[0+1*DCTSTRIDE]= (d00 - d10)>>3;
1150 data[1+1*DCTSTRIDE]= (d01 - d11)>>3;
1153 void ff_j_rev_dct1(DCTBLOCK data){
1154 data[0] = (data[0] + 4)>>3;
1160 void ff_jref_idct_put(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
1162 ff_j_rev_dct(block);
1163 ff_put_pixels_clamped_c(block, dest, line_size);
1166 void ff_jref_idct_add(uint8_t *dest, ptrdiff_t line_size, int16_t *block)
1168 ff_j_rev_dct(block);
1169 ff_add_pixels_clamped_c(block, dest, line_size);