]> git.sesse.net Git - ffmpeg/blob - libavcodec/simple_idct.c
hevc: change the stride of the MC buffer to be in bytes instead of elements
[ffmpeg] / libavcodec / simple_idct.c
1 /*
2  * Simple IDCT
3  *
4  * Copyright (c) 2001 Michael Niedermayer <michaelni@gmx.at>
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 #include "libavutil/intreadwrite.h"
29 #include "avcodec.h"
30 #include "mathops.h"
31 #include "simple_idct.h"
32
33 #define BIT_DEPTH 8
34 #include "simple_idct_template.c"
35 #undef BIT_DEPTH
36
37 #define BIT_DEPTH 10
38 #include "simple_idct_template.c"
39 #undef BIT_DEPTH
40
41 /* 2x4x8 idct */
42
43 #define CN_SHIFT 12
44 #define C_FIX(x) ((int)((x) * (1 << CN_SHIFT) + 0.5))
45 #define C1 C_FIX(0.6532814824)
46 #define C2 C_FIX(0.2705980501)
47
48 /* row idct is multiple by 16 * sqrt(2.0), col idct4 is normalized,
49    and the butterfly must be multiplied by 0.5 * sqrt(2.0) */
50 #define C_SHIFT (4+1+12)
51
52 static inline void idct4col_put(uint8_t *dest, int line_size, const int16_t *col)
53 {
54     int c0, c1, c2, c3, a0, a1, a2, a3;
55
56     a0 = col[8*0];
57     a1 = col[8*2];
58     a2 = col[8*4];
59     a3 = col[8*6];
60     c0 = ((a0 + a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
61     c2 = ((a0 - a2) << (CN_SHIFT - 1)) + (1 << (C_SHIFT - 1));
62     c1 = a1 * C1 + a3 * C2;
63     c3 = a1 * C2 - a3 * C1;
64     dest[0] = av_clip_uint8((c0 + c1) >> C_SHIFT);
65     dest += line_size;
66     dest[0] = av_clip_uint8((c2 + c3) >> C_SHIFT);
67     dest += line_size;
68     dest[0] = av_clip_uint8((c2 - c3) >> C_SHIFT);
69     dest += line_size;
70     dest[0] = av_clip_uint8((c0 - c1) >> C_SHIFT);
71 }
72
73 #define BF(k) \
74 {\
75     int a0, a1;\
76     a0 = ptr[k];\
77     a1 = ptr[8 + k];\
78     ptr[k] = a0 + a1;\
79     ptr[8 + k] = a0 - a1;\
80 }
81
82 /* only used by DV codec. The input must be interlaced. 128 is added
83    to the pixels before clamping to avoid systematic error
84    (1024*sqrt(2)) offset would be needed otherwise. */
85 /* XXX: I think a 1.0/sqrt(2) normalization should be needed to
86    compensate the extra butterfly stage - I don't have the full DV
87    specification */
88 void ff_simple_idct248_put(uint8_t *dest, int line_size, int16_t *block)
89 {
90     int i;
91     int16_t *ptr;
92
93     /* butterfly */
94     ptr = block;
95     for(i=0;i<4;i++) {
96         BF(0);
97         BF(1);
98         BF(2);
99         BF(3);
100         BF(4);
101         BF(5);
102         BF(6);
103         BF(7);
104         ptr += 2 * 8;
105     }
106
107     /* IDCT8 on each line */
108     for(i=0; i<8; i++) {
109         idctRowCondDC_8(block + i*8, 0);
110     }
111
112     /* IDCT4 and store */
113     for(i=0;i<8;i++) {
114         idct4col_put(dest + i, 2 * line_size, block + i);
115         idct4col_put(dest + line_size + i, 2 * line_size, block + 8 + i);
116     }
117 }
118
119 /* 8x4 & 4x8 WMV2 IDCT */
120 #undef CN_SHIFT
121 #undef C_SHIFT
122 #undef C_FIX
123 #undef C1
124 #undef C2
125 #define CN_SHIFT 12
126 #define C_FIX(x) ((int)((x) * 1.414213562 * (1 << CN_SHIFT) + 0.5))
127 #define C1 C_FIX(0.6532814824)
128 #define C2 C_FIX(0.2705980501)
129 #define C3 C_FIX(0.5)
130 #define C_SHIFT (4+1+12)
131 static inline void idct4col_add(uint8_t *dest, int line_size, const int16_t *col)
132 {
133     int c0, c1, c2, c3, a0, a1, a2, a3;
134
135     a0 = col[8*0];
136     a1 = col[8*1];
137     a2 = col[8*2];
138     a3 = col[8*3];
139     c0 = (a0 + a2)*C3 + (1 << (C_SHIFT - 1));
140     c2 = (a0 - a2)*C3 + (1 << (C_SHIFT - 1));
141     c1 = a1 * C1 + a3 * C2;
142     c3 = a1 * C2 - a3 * C1;
143     dest[0] = av_clip_uint8(dest[0] + ((c0 + c1) >> C_SHIFT));
144     dest += line_size;
145     dest[0] = av_clip_uint8(dest[0] + ((c2 + c3) >> C_SHIFT));
146     dest += line_size;
147     dest[0] = av_clip_uint8(dest[0] + ((c2 - c3) >> C_SHIFT));
148     dest += line_size;
149     dest[0] = av_clip_uint8(dest[0] + ((c0 - c1) >> C_SHIFT));
150 }
151
152 #define RN_SHIFT 15
153 #define R_FIX(x) ((int)((x) * 1.414213562 * (1 << RN_SHIFT) + 0.5))
154 #define R1 R_FIX(0.6532814824)
155 #define R2 R_FIX(0.2705980501)
156 #define R3 R_FIX(0.5)
157 #define R_SHIFT 11
158 static inline void idct4row(int16_t *row)
159 {
160     int c0, c1, c2, c3, a0, a1, a2, a3;
161
162     a0 = row[0];
163     a1 = row[1];
164     a2 = row[2];
165     a3 = row[3];
166     c0 = (a0 + a2)*R3 + (1 << (R_SHIFT - 1));
167     c2 = (a0 - a2)*R3 + (1 << (R_SHIFT - 1));
168     c1 = a1 * R1 + a3 * R2;
169     c3 = a1 * R2 - a3 * R1;
170     row[0]= (c0 + c1) >> R_SHIFT;
171     row[1]= (c2 + c3) >> R_SHIFT;
172     row[2]= (c2 - c3) >> R_SHIFT;
173     row[3]= (c0 - c1) >> R_SHIFT;
174 }
175
176 void ff_simple_idct84_add(uint8_t *dest, int line_size, int16_t *block)
177 {
178     int i;
179
180     /* IDCT8 on each line */
181     for(i=0; i<4; i++) {
182         idctRowCondDC_8(block + i*8, 0);
183     }
184
185     /* IDCT4 and store */
186     for(i=0;i<8;i++) {
187         idct4col_add(dest + i, line_size, block + i);
188     }
189 }
190
191 void ff_simple_idct48_add(uint8_t *dest, int line_size, int16_t *block)
192 {
193     int i;
194
195     /* IDCT4 on each line */
196     for(i=0; i<8; i++) {
197         idct4row(block + i*8);
198     }
199
200     /* IDCT8 and store */
201     for(i=0; i<4; i++){
202         idctSparseColAdd_8(dest + i, line_size, block + i);
203     }
204 }
205
206 void ff_simple_idct44_add(uint8_t *dest, int line_size, int16_t *block)
207 {
208     int i;
209
210     /* IDCT4 on each line */
211     for(i=0; i<4; i++) {
212         idct4row(block + i*8);
213     }
214
215     /* IDCT4 and store */
216     for(i=0; i<4; i++){
217         idct4col_add(dest + i, line_size, block + i);
218     }
219 }
220
221 void ff_prores_idct(int16_t *block, const int16_t *qmat)
222 {
223     int i;
224
225     for (i = 0; i < 64; i++)
226         block[i] *= qmat[i];
227
228     for (i = 0; i < 8; i++)
229         idctRowCondDC_10(block + i*8, 2);
230
231     for (i = 0; i < 8; i++)
232         idctSparseCol_10(block + i);
233 }