FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vp3dsp.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2004 the ffmpeg project
3  *
4  * This file is part of FFmpeg.
5  *
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.
10  *
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.
15  *
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
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 }
219 
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);
222 }
223 
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;
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 static void vp3_v_loop_filter_c(uint8_t *first_pixel, int stride,
242  int *bounding_values)
243 {
244  unsigned char *end;
245  int filter_value;
246  const int nstride= -stride;
247 
248  for (end= first_pixel + 8; first_pixel < end; first_pixel++) {
249  filter_value =
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);
255  }
256 }
257 
258 static void vp3_h_loop_filter_c(uint8_t *first_pixel, int stride,
259  int *bounding_values)
260 {
261  unsigned char *end;
262  int filter_value;
263 
264  for (end= first_pixel + 8*stride; first_pixel != end; first_pixel += stride) {
265  filter_value =
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);
271  }
272 }
273 
275 {
281 
283 
284  if (ARCH_ARM)
285  ff_vp3dsp_init_arm(c, flags);
286  if (ARCH_PPC)
287  ff_vp3dsp_init_ppc(c, flags);
288  if (ARCH_X86)
289  ff_vp3dsp_init_x86(c, flags);
290 }