vp3dsp.c
Go to the documentation of this file.
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 
27 #include "avcodec.h"
28 #include "dsputil.h"
29 
30 #define IdctAdjustBeforeShift 8
31 #define xC1S7 64277
32 #define xC2S6 60547
33 #define xC3S5 54491
34 #define xC4S4 46341
35 #define xC5S3 36410
36 #define xC6S2 25080
37 #define xC7S1 12785
38 
39 #define M(a,b) (((a) * (b))>>16)
40 
41 static av_always_inline void idct(uint8_t *dst, int stride, int16_t *input, int type)
42 {
43  int16_t *ip = input;
44 
45  int A, B, C, D, Ad, Bd, Cd, Dd, E, F, G, H;
46  int Ed, Gd, Add, Bdd, Fd, Hd;
47 
48  int i;
49 
50  /* Inverse DCT on the rows now */
51  for (i = 0; i < 8; i++) {
52  /* Check for non-zero values */
53  if ( ip[0] | ip[1] | ip[2] | ip[3] | ip[4] | ip[5] | ip[6] | ip[7] ) {
54  A = M(xC1S7, ip[1]) + M(xC7S1, ip[7]);
55  B = M(xC7S1, ip[1]) - M(xC1S7, ip[7]);
56  C = M(xC3S5, ip[3]) + M(xC5S3, ip[5]);
57  D = M(xC3S5, ip[5]) - M(xC5S3, ip[3]);
58 
59  Ad = M(xC4S4, (A - C));
60  Bd = M(xC4S4, (B - D));
61 
62  Cd = A + C;
63  Dd = B + D;
64 
65  E = M(xC4S4, (ip[0] + ip[4]));
66  F = M(xC4S4, (ip[0] - ip[4]));
67 
68  G = M(xC2S6, ip[2]) + M(xC6S2, ip[6]);
69  H = M(xC6S2, ip[2]) - M(xC2S6, ip[6]);
70 
71  Ed = E - G;
72  Gd = E + G;
73 
74  Add = F + Ad;
75  Bdd = Bd - H;
76 
77  Fd = F - Ad;
78  Hd = Bd + H;
79 
80  /* Final sequence of operations over-write original inputs. */
81  ip[0] = Gd + Cd ;
82  ip[7] = Gd - Cd ;
83 
84  ip[1] = Add + Hd;
85  ip[2] = Add - Hd;
86 
87  ip[3] = Ed + Dd ;
88  ip[4] = Ed - Dd ;
89 
90  ip[5] = Fd + Bdd;
91  ip[6] = Fd - Bdd;
92  }
93 
94  ip += 8; /* next row */
95  }
96 
97  ip = input;
98 
99  for ( i = 0; i < 8; i++) {
100  /* Check for non-zero values (bitwise or faster than ||) */
101  if ( ip[1 * 8] | ip[2 * 8] | ip[3 * 8] |
102  ip[4 * 8] | ip[5 * 8] | ip[6 * 8] | ip[7 * 8] ) {
103 
104  A = M(xC1S7, ip[1*8]) + M(xC7S1, ip[7*8]);
105  B = M(xC7S1, ip[1*8]) - M(xC1S7, ip[7*8]);
106  C = M(xC3S5, ip[3*8]) + M(xC5S3, ip[5*8]);
107  D = M(xC3S5, ip[5*8]) - M(xC5S3, ip[3*8]);
108 
109  Ad = M(xC4S4, (A - C));
110  Bd = M(xC4S4, (B - D));
111 
112  Cd = A + C;
113  Dd = B + D;
114 
115  E = M(xC4S4, (ip[0*8] + ip[4*8])) + 8;
116  F = M(xC4S4, (ip[0*8] - ip[4*8])) + 8;
117 
118  if(type==1){ //HACK
119  E += 16*128;
120  F += 16*128;
121  }
122 
123  G = M(xC2S6, ip[2*8]) + M(xC6S2, ip[6*8]);
124  H = M(xC6S2, ip[2*8]) - M(xC2S6, ip[6*8]);
125 
126  Ed = E - G;
127  Gd = E + G;
128 
129  Add = F + Ad;
130  Bdd = Bd - H;
131 
132  Fd = F - Ad;
133  Hd = Bd + H;
134 
135  /* Final sequence of operations over-write original inputs. */
136  if(type==0){
137  ip[0*8] = (Gd + Cd ) >> 4;
138  ip[7*8] = (Gd - Cd ) >> 4;
139 
140  ip[1*8] = (Add + Hd ) >> 4;
141  ip[2*8] = (Add - Hd ) >> 4;
142 
143  ip[3*8] = (Ed + Dd ) >> 4;
144  ip[4*8] = (Ed - Dd ) >> 4;
145 
146  ip[5*8] = (Fd + Bdd ) >> 4;
147  ip[6*8] = (Fd - Bdd ) >> 4;
148  }else if(type==1){
149  dst[0*stride] = av_clip_uint8((Gd + Cd ) >> 4);
150  dst[7*stride] = av_clip_uint8((Gd - Cd ) >> 4);
151 
152  dst[1*stride] = av_clip_uint8((Add + Hd ) >> 4);
153  dst[2*stride] = av_clip_uint8((Add - Hd ) >> 4);
154 
155  dst[3*stride] = av_clip_uint8((Ed + Dd ) >> 4);
156  dst[4*stride] = av_clip_uint8((Ed - Dd ) >> 4);
157 
158  dst[5*stride] = av_clip_uint8((Fd + Bdd ) >> 4);
159  dst[6*stride] = av_clip_uint8((Fd - Bdd ) >> 4);
160  }else{
161  dst[0*stride] = av_clip_uint8(dst[0*stride] + ((Gd + Cd ) >> 4));
162  dst[7*stride] = av_clip_uint8(dst[7*stride] + ((Gd - Cd ) >> 4));
163 
164  dst[1*stride] = av_clip_uint8(dst[1*stride] + ((Add + Hd ) >> 4));
165  dst[2*stride] = av_clip_uint8(dst[2*stride] + ((Add - Hd ) >> 4));
166 
167  dst[3*stride] = av_clip_uint8(dst[3*stride] + ((Ed + Dd ) >> 4));
168  dst[4*stride] = av_clip_uint8(dst[4*stride] + ((Ed - Dd ) >> 4));
169 
170  dst[5*stride] = av_clip_uint8(dst[5*stride] + ((Fd + Bdd ) >> 4));
171  dst[6*stride] = av_clip_uint8(dst[6*stride] + ((Fd - Bdd ) >> 4));
172  }
173 
174  } else {
175  if(type==0){
176  ip[0*8] =
177  ip[1*8] =
178  ip[2*8] =
179  ip[3*8] =
180  ip[4*8] =
181  ip[5*8] =
182  ip[6*8] =
183  ip[7*8] = ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
184  }else if(type==1){
185  dst[0*stride]=
186  dst[1*stride]=
187  dst[2*stride]=
188  dst[3*stride]=
189  dst[4*stride]=
190  dst[5*stride]=
191  dst[6*stride]=
192  dst[7*stride]= av_clip_uint8(128 + ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20));
193  }else{
194  if(ip[0*8]){
195  int v= ((xC4S4 * ip[0*8] + (IdctAdjustBeforeShift<<16))>>20);
196  dst[0*stride] = av_clip_uint8(dst[0*stride] + v);
197  dst[1*stride] = av_clip_uint8(dst[1*stride] + v);
198  dst[2*stride] = av_clip_uint8(dst[2*stride] + v);
199  dst[3*stride] = av_clip_uint8(dst[3*stride] + v);
200  dst[4*stride] = av_clip_uint8(dst[4*stride] + v);
201  dst[5*stride] = av_clip_uint8(dst[5*stride] + v);
202  dst[6*stride] = av_clip_uint8(dst[6*stride] + v);
203  dst[7*stride] = av_clip_uint8(dst[7*stride] + v);
204  }
205  }
206  }
207 
208  ip++; /* next column */
209  dst++;
210  }
211 }
212 
213 void ff_vp3_idct_c(DCTELEM *block/* align 16*/){
214  idct(NULL, 0, block, 0);
215 }
216 
217 void ff_vp3_idct_put_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
218  idct(dest, line_size, block, 1);
219 }
220 
221 void ff_vp3_idct_add_c(uint8_t *dest/*align 8*/, int line_size, DCTELEM *block/*align 16*/){
222  idct(dest, line_size, block, 2);
223 }
224 
225 void ff_vp3_idct_dc_add_c(uint8_t *dest/*align 8*/, int line_size, const DCTELEM *block/*align 16*/){
226  int i, dc = (block[0] + 15) >> 5;
227 
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);
237  dest += line_size;
238  }
239 }
240 
241 void ff_vp3_v_loop_filter_c(uint8_t *first_pixel, int stride, int *bounding_values)
242 {
243  unsigned char *end;
244  int filter_value;
245  const int nstride= -stride;
246 
247  for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
248  filter_value =
249  (first_pixel[2 * nstride] - first_pixel[ stride])
250  +3*(first_pixel[0 ] - first_pixel[nstride]);
251  filter_value = bounding_values[(filter_value + 4) >> 3];
252  first_pixel[nstride] = av_clip_uint8(first_pixel[nstride] + filter_value);
253  first_pixel[0] = av_clip_uint8(first_pixel[0] - filter_value);
254  }
255 }
256 
257 void ff_vp3_h_loop_filter_c(uint8_t *first_pixel, int stride, int *bounding_values)
258 {
259  unsigned char *end;
260  int filter_value;
261 
262  for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
263  filter_value =
264  (first_pixel[-2] - first_pixel[ 1])
265  +3*(first_pixel[ 0] - first_pixel[-1]);
266  filter_value = bounding_values[(filter_value + 4) >> 3];
267  first_pixel[-1] = av_clip_uint8(first_pixel[-1] + filter_value);
268  first_pixel[ 0] = av_clip_uint8(first_pixel[ 0] - filter_value);
269  }
270 }