2 * Copyright (C) 2004 the ffmpeg project
4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Standard C DSP-oriented functions cribbed from the original VP3
27 #include "libavutil/attributes.h"
28 #include "libavutil/common.h"
33 #define IdctAdjustBeforeShift 8
42 #define M(a,b) (((a) * (b))>>16)
44 static av_always_inline void idct(uint8_t *dst, int stride, int16_t *input, int type)
48 int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
49 int Ed, Gd, Add, Bdd, Fd, Hd;
53 /* Inverse DCT on the rows now */
54 for (i = 0; i < 8; i++) {
55 /* Check for non-zero values */
56 if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
57 A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
58 B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
59 C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
60 D = M(xC3S5, ip[5]) - M(xC5S3, ip[3]);
62 Ad = M(xC4S4, (A - C));
63 Bd = M(xC4S4, (B - D));
68 E = M(xC4S4, (ip[0] + ip[4]));
69 F = M(xC4S4, (ip[0] - ip[4]));
71 G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
72 H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
83 /* Final sequence of operations over-write original inputs. */
97 ip += 8; /* next row */
102 for ( i = 0; i < 8; i++) {
103 /* Check for non-zero values (bitwise or faster than ||) */
104 if ( ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
105 ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
107 A = M(xC1S7, ip[1*8]) + M(xC7S1, ip[7*8]);
108 B = M(xC7S1, ip[1*8]) - M(xC1S7, ip[7*8]);
109 C = M(xC3S5, ip[3*8]) + M(xC5S3, ip[5*8]);
110 D = M(xC3S5, ip[5*8]) - M(xC5S3, ip[3*8]);
112 Ad = M(xC4S4, (A - C));
113 Bd = M(xC4S4, (B - D));
118 E = M(xC4S4, (ip[0*8] + ip[4*8])) + 8;
119 F = M(xC4S4, (ip[0*8] - ip[4*8])) + 8;
126 G = M(xC2S6, ip[2*8]) + M(xC6S2, ip[6*8]);
127 H = M(xC6S2, ip[2*8]) - M(xC2S6, ip[6*8]);
138 /* Final sequence of operations over-write original inputs. */
140 ip[0*8] = (Gd + Cd ) >> 4;
141 ip[7*8] = (Gd - Cd ) >> 4;
143 ip[1*8] = (Add + Hd ) >> 4;
144 ip[2*8] = (Add - Hd ) >> 4;
146 ip[3*8] = (Ed + Dd ) >> 4;
147 ip[4*8] = (Ed - Dd ) >> 4;
149 ip[5*8] = (Fd + Bdd ) >> 4;
150 ip[6*8] = (Fd - Bdd ) >> 4;
152 dst[0*stride] = av_clip_uint8((Gd + Cd ) >> 4);
153 dst[7*stride] = av_clip_uint8((Gd - Cd ) >> 4);
155 dst[1*stride] = av_clip_uint8((Add + Hd ) >> 4);
156 dst[2*stride] = av_clip_uint8((Add - Hd ) >> 4);
158 dst[3*stride] = av_clip_uint8((Ed + Dd ) >> 4);
159 dst[4*stride] = av_clip_uint8((Ed - Dd ) >> 4);
161 dst[5*stride] = av_clip_uint8((Fd + Bdd ) >> 4);
162 dst[6*stride] = av_clip_uint8((Fd - Bdd ) >> 4);
164 dst[0*stride] = av_clip_uint8(dst[0*stride] + ((Gd + Cd ) >> 4));
165 dst[7*stride] = av_clip_uint8(dst[7*stride] + ((Gd - Cd ) >> 4));
167 dst[1*stride] = av_clip_uint8(dst[1*stride] + ((Add + Hd ) >> 4));
168 dst[2*stride] = av_clip_uint8(dst[2*stride] + ((Add - Hd ) >> 4));
170 dst[3*stride] = av_clip_uint8(dst[3*stride] + ((Ed + Dd ) >> 4));
171 dst[4*stride] = av_clip_uint8(dst[4*stride] + ((Ed - Dd ) >> 4));
173 dst[5*stride] = av_clip_uint8(dst[5*stride] + ((Fd + Bdd ) >> 4));
174 dst[6*stride] = av_clip_uint8(dst[6*stride] + ((Fd - Bdd ) >> 4));
186 ip[7*8] = ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
195 dst[7*stride]= av_clip_uint8(128 + ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20));
198 int v= ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
199 dst[0*stride] = av_clip_uint8(dst[0*stride] + v);
200 dst[1*stride] = av_clip_uint8(dst[1*stride] + v);
201 dst[2*stride] = av_clip_uint8(dst[2*stride] + v);
202 dst[3*stride] = av_clip_uint8(dst[3*stride] + v);
203 dst[4*stride] = av_clip_uint8(dst[4*stride] + v);
204 dst[5*stride] = av_clip_uint8(dst[5*stride] + v);
205 dst[6*stride] = av_clip_uint8(dst[6*stride] + v);
206 dst[7*stride] = av_clip_uint8(dst[7*stride] + v);
211 ip++; /* next column */
216 static void vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
217 idct(dest, line_size, block, 1);
220 static void vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
221 idct(dest, line_size, block, 2);
224 static void vp3_idct_dc_add_c(uint8_t *dest/*align 8*/, int line_size,
225 const DCTELEM *block/*align 16*/){
226 int i, dc = (block[0] + 15) >> 5;
228 for(i = 0; i < 8; i++){
229 dest[0] = av_clip_uint8(dest[0] + dc);
230 dest[1] = av_clip_uint8(dest[1] + dc);
231 dest[2] = av_clip_uint8(dest[2] + dc);
232 dest[3] = av_clip_uint8(dest[3] + dc);
233 dest[4] = av_clip_uint8(dest[4] + dc);
234 dest[5] = av_clip_uint8(dest[5] + dc);
235 dest[6] = av_clip_uint8(dest[6] + dc);
236 dest[7] = av_clip_uint8(dest[7] + dc);
241 static void vp3_v_loop_filter_c(uint8_t *first_pixel, int stride,
242 int *bounding_values)
246 const int nstride= -stride;
248 for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
250 (first_pixel[2 * nstride] - first_pixel[ stride])
251 +3*(first_pixel[0 ] - first_pixel[nstride]);
252 filter_value = bounding_values[(filter_value + 4) >> 3];
253 first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
254 first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
258 static void vp3_h_loop_filter_c(uint8_t *first_pixel, int stride,
259 int *bounding_values)
264 for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
266 (first_pixel[-2] - first_pixel[ 1])
267 +3*(first_pixel[ 0] - first_pixel[-1]);
268 filter_value = bounding_values[(filter_value + 4) >> 3];
269 first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
270 first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
274 av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
276 c->idct_put = vp3_idct_put_c;
277 c->idct_add = vp3_idct_add_c;
278 c->idct_dc_add = vp3_idct_dc_add_c;
279 c->v_loop_filter = vp3_v_loop_filter_c;
280 c->h_loop_filter = vp3_h_loop_filter_c;
282 c->idct_perm = FF_NO_IDCT_PERM;
285 ff_vp3dsp_init_arm(c, flags);
287 ff_vp3dsp_init_ppc(c, flags);
289 ff_vp3dsp_init_x86(c, flags);