]> git.sesse.net Git - ffmpeg/blob - libavcodec/vp3dsp.c
0ce6b81d98af6c557de2191bed6610ef758700aa
[ffmpeg] / libavcodec / vp3dsp.c
1 /*
2  * Copyright (C) 2004 the ffmpeg project
3  *
4  * This file is part of Libav.
5  *
6  * Libav 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.
10  *
11  * Libav 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.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with Libav; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 /**
22  * @file
23  * Standard C DSP-oriented functions cribbed from the original VP3
24  * source code.
25  */
26
27 #include "libavutil/attributes.h"
28 #include "libavutil/common.h"
29 #include "avcodec.h"
30 #include "dsputil.h"
31 #include "vp3dsp.h"
32
33 #define IdctAdjustBeforeShift 8
34 #define xC1S7 64277
35 #define xC2S6 60547
36 #define xC3S5 54491
37 #define xC4S4 46341
38 #define xC5S3 36410
39 #define xC6S2 25080
40 #define xC7S1 12785
41
42 #define M(a,b) (((a) * (b))>>16)
43
44 static av_always_inline void idct(uint8_t *dst, int stride, int16_t *input, int type)
45 {
46     int16_t *ip = input;
47
48     int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
49     int Ed, Gd, Add, Bdd, Fd, Hd;
50
51     int i;
52
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]);
61
62             Ad = M(xC4S4, (A - C));
63             Bd = M(xC4S4, (B - D));
64
65             Cd = A + C;
66             Dd = B + D;
67
68             E = M(xC4S4, (ip[0] + ip[4]));
69             F = M(xC4S4, (ip[0] - ip[4]));
70
71             G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
72             H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
73
74             Ed = E - G;
75             Gd = E + G;
76
77             Add = F + Ad;
78             Bdd = Bd - H;
79
80             Fd = F - Ad;
81             Hd = Bd + H;
82
83             /*  Final sequence of operations over-write original inputs. */
84             ip[0] = Gd + Cd ;
85             ip[7] = Gd - Cd ;
86
87             ip[1] = Add + Hd;
88             ip[2] = Add - Hd;
89
90             ip[3] = Ed + Dd ;
91             ip[4] = Ed - Dd ;
92
93             ip[5] = Fd + Bdd;
94             ip[6] = Fd - Bdd;
95         }
96
97         ip += 8;            /* next row */
98     }
99
100     ip = input;
101
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] ) {
106
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]);
111
112             Ad = M(xC4S4, (A - C));
113             Bd = M(xC4S4, (B - D));
114
115             Cd = A + C;
116             Dd = B + D;
117
118             E = M(xC4S4, (ip[0*8] + ip[4*8])) + 8;
119             F = M(xC4S4, (ip[0*8] - ip[4*8])) + 8;
120
121             if(type==1){  //HACK
122                 E += 16*128;
123                 F += 16*128;
124             }
125
126             G = M(xC2S6, ip[2*8]) + M(xC6S2, ip[6*8]);
127             H = M(xC6S2, ip[2*8]) - M(xC2S6, ip[6*8]);
128
129             Ed = E - G;
130             Gd = E + G;
131
132             Add = F + Ad;
133             Bdd = Bd - H;
134
135             Fd = F - Ad;
136             Hd = Bd + H;
137
138             /* Final sequence of operations over-write original inputs. */
139             if(type==0){
140                 ip[0*8] = (Gd + Cd )  >> 4;
141                 ip[7*8] = (Gd - Cd )  >> 4;
142
143                 ip[1*8] = (Add + Hd ) >> 4;
144                 ip[2*8] = (Add - Hd ) >> 4;
145
146                 ip[3*8] = (Ed + Dd )  >> 4;
147                 ip[4*8] = (Ed - Dd )  >> 4;
148
149                 ip[5*8] = (Fd + Bdd ) >> 4;
150                 ip[6*8] = (Fd - Bdd ) >> 4;
151             }else if(type==1){
152                 dst[0*stride] = av_clip_uint8((Gd + Cd )  >> 4);
153                 dst[7*stride] = av_clip_uint8((Gd - Cd )  >> 4);
154
155                 dst[1*stride] = av_clip_uint8((Add + Hd ) >> 4);
156                 dst[2*stride] = av_clip_uint8((Add - Hd ) >> 4);
157
158                 dst[3*stride] = av_clip_uint8((Ed + Dd )  >> 4);
159                 dst[4*stride] = av_clip_uint8((Ed - Dd )  >> 4);
160
161                 dst[5*stride] = av_clip_uint8((Fd + Bdd ) >> 4);
162                 dst[6*stride] = av_clip_uint8((Fd - Bdd ) >> 4);
163             }else{
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));
166
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));
169
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));
172
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));
175             }
176
177         } else {
178             if(type==0){
179                 ip[0*8] =
180                 ip[1*8] =
181                 ip[2*8] =
182                 ip[3*8] =
183                 ip[4*8] =
184                 ip[5*8] =
185                 ip[6*8] =
186                 ip[7*8] = ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
187             }else if(type==1){
188                 dst[0*stride]=
189                 dst[1*stride]=
190                 dst[2*stride]=
191                 dst[3*stride]=
192                 dst[4*stride]=
193                 dst[5*stride]=
194                 dst[6*stride]=
195                 dst[7*stride]= av_clip_uint8(128 + ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20));
196             }else{
197                 if(ip[0*8]){
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);
207                 }
208             }
209         }
210
211         ip++;            /* next column */
212         dst++;
213     }
214 }
215
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);
218     memset(block, 0, sizeof(*block) * 64);
219 }
220
221 static void vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
222     idct(dest, line_size, block, 2);
223     memset(block, 0, sizeof(*block) * 64);
224 }
225
226 static void vp3_idct_dc_add_c(uint8_t *dest/*align 8*/, int line_size,
227                               DCTELEM *block/*align 16*/){
228     int i, dc = (block[0] + 15) >> 5;
229
230     for(i = 0; i < 8; i++){
231         dest[0] = av_clip_uint8(dest[0] + dc);
232         dest[1] = av_clip_uint8(dest[1] + dc);
233         dest[2] = av_clip_uint8(dest[2] + dc);
234         dest[3] = av_clip_uint8(dest[3] + dc);
235         dest[4] = av_clip_uint8(dest[4] + dc);
236         dest[5] = av_clip_uint8(dest[5] + dc);
237         dest[6] = av_clip_uint8(dest[6] + dc);
238         dest[7] = av_clip_uint8(dest[7] + dc);
239         dest += line_size;
240     }
241     block[0] = 0;
242 }
243
244 static void vp3_v_loop_filter_c(uint8_t *first_pixel, int stride,
245                                 int *bounding_values)
246 {
247     unsigned char *end;
248     int filter_value;
249     const int nstride= -stride;
250
251     for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
252         filter_value =
253             (first_pixel[2 * nstride] - first_pixel[ stride])
254          +3*(first_pixel[0          ] - first_pixel[nstride]);
255         filter_value = bounding_values[(filter_value + 4) >> 3];
256         first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
257         first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
258     }
259 }
260
261 static void vp3_h_loop_filter_c(uint8_t *first_pixel, int stride,
262                                 int *bounding_values)
263 {
264     unsigned char *end;
265     int filter_value;
266
267     for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
268         filter_value =
269             (first_pixel[-2] - first_pixel[ 1])
270          +3*(first_pixel[ 0] - first_pixel[-1]);
271         filter_value = bounding_values[(filter_value + 4) >> 3];
272         first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
273         first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
274     }
275 }
276
277 static void put_no_rnd_pixels_l2(uint8_t *dst, const uint8_t *src1,
278                                  const uint8_t *src2, ptrdiff_t stride, int h)
279 {
280     int i;
281
282     for (i = 0; i < h; i++) {
283         uint32_t a, b;
284
285         a = AV_RN32(&src1[i * stride]);
286         b = AV_RN32(&src2[i * stride]);
287         AV_WN32A(&dst[i * stride], no_rnd_avg32(a, b));
288         a = AV_RN32(&src1[i * stride + 4]);
289         b = AV_RN32(&src2[i * stride + 4]);
290         AV_WN32A(&dst[i * stride + 4], no_rnd_avg32(a, b));
291     }
292 }
293
294 av_cold void ff_vp3dsp_init(VP3DSPContext *c, int flags)
295 {
296     c->put_no_rnd_pixels_l2 = put_no_rnd_pixels_l2;
297
298     c->idct_put      = vp3_idct_put_c;
299     c->idct_add      = vp3_idct_add_c;
300     c->idct_dc_add   = vp3_idct_dc_add_c;
301     c->v_loop_filter = vp3_v_loop_filter_c;
302     c->h_loop_filter = vp3_h_loop_filter_c;
303
304     c->idct_perm = FF_NO_IDCT_PERM;
305
306     if (ARCH_ARM)
307         ff_vp3dsp_init_arm(c, flags);
308     if (ARCH_PPC)
309         ff_vp3dsp_init_ppc(c, flags);
310     if (ARCH_X86)
311         ff_vp3dsp_init_x86(c, flags);
312 }