• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2014 The WebM project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 #include <stdlib.h>
11 
12 #include "./vpx_dsp_rtcd.h"
13 #include "vpx_ports/mem.h"
14 
vpx_avg_8x8_c(const uint8_t * s,int p)15 unsigned int vpx_avg_8x8_c(const uint8_t *s, int p) {
16   int i, j;
17   int sum = 0;
18   for (i = 0; i < 8; ++i, s += p)
19     for (j = 0; j < 8; sum += s[j], ++j) {
20     }
21 
22   return (sum + 32) >> 6;
23 }
24 
vpx_avg_4x4_c(const uint8_t * s,int p)25 unsigned int vpx_avg_4x4_c(const uint8_t *s, int p) {
26   int i, j;
27   int sum = 0;
28   for (i = 0; i < 4; ++i, s += p)
29     for (j = 0; j < 4; sum += s[j], ++j) {
30     }
31 
32   return (sum + 8) >> 4;
33 }
34 
35 #if CONFIG_VP9_HIGHBITDEPTH
36 // src_diff: 13 bit, dynamic range [-4095, 4095]
37 // coeff: 16 bit
hadamard_highbd_col8_first_pass(const int16_t * src_diff,ptrdiff_t src_stride,int16_t * coeff)38 static void hadamard_highbd_col8_first_pass(const int16_t *src_diff,
39                                             ptrdiff_t src_stride,
40                                             int16_t *coeff) {
41   int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
42   int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
43   int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
44   int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
45   int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
46   int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
47   int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
48   int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
49 
50   int16_t c0 = b0 + b2;
51   int16_t c1 = b1 + b3;
52   int16_t c2 = b0 - b2;
53   int16_t c3 = b1 - b3;
54   int16_t c4 = b4 + b6;
55   int16_t c5 = b5 + b7;
56   int16_t c6 = b4 - b6;
57   int16_t c7 = b5 - b7;
58 
59   coeff[0] = c0 + c4;
60   coeff[7] = c1 + c5;
61   coeff[3] = c2 + c6;
62   coeff[4] = c3 + c7;
63   coeff[2] = c0 - c4;
64   coeff[6] = c1 - c5;
65   coeff[1] = c2 - c6;
66   coeff[5] = c3 - c7;
67 }
68 
69 // src_diff: 16 bit, dynamic range [-32760, 32760]
70 // coeff: 19 bit
hadamard_highbd_col8_second_pass(const int16_t * src_diff,ptrdiff_t src_stride,int32_t * coeff)71 static void hadamard_highbd_col8_second_pass(const int16_t *src_diff,
72                                              ptrdiff_t src_stride,
73                                              int32_t *coeff) {
74   int32_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
75   int32_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
76   int32_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
77   int32_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
78   int32_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
79   int32_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
80   int32_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
81   int32_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
82 
83   int32_t c0 = b0 + b2;
84   int32_t c1 = b1 + b3;
85   int32_t c2 = b0 - b2;
86   int32_t c3 = b1 - b3;
87   int32_t c4 = b4 + b6;
88   int32_t c5 = b5 + b7;
89   int32_t c6 = b4 - b6;
90   int32_t c7 = b5 - b7;
91 
92   coeff[0] = c0 + c4;
93   coeff[7] = c1 + c5;
94   coeff[3] = c2 + c6;
95   coeff[4] = c3 + c7;
96   coeff[2] = c0 - c4;
97   coeff[6] = c1 - c5;
98   coeff[1] = c2 - c6;
99   coeff[5] = c3 - c7;
100 }
101 
102 // The order of the output coeff of the hadamard is not important. For
103 // optimization purposes the final transpose may be skipped.
vpx_highbd_hadamard_8x8_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)104 void vpx_highbd_hadamard_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
105                                tran_low_t *coeff) {
106   int idx;
107   int16_t buffer[64];
108   int32_t buffer2[64];
109   int16_t *tmp_buf = &buffer[0];
110   for (idx = 0; idx < 8; ++idx) {
111     // src_diff: 13 bit
112     // buffer: 16 bit, dynamic range [-32760, 32760]
113     hadamard_highbd_col8_first_pass(src_diff, src_stride, tmp_buf);
114     tmp_buf += 8;
115     ++src_diff;
116   }
117 
118   tmp_buf = &buffer[0];
119   for (idx = 0; idx < 8; ++idx) {
120     // buffer: 16 bit
121     // buffer2: 19 bit, dynamic range [-262080, 262080]
122     hadamard_highbd_col8_second_pass(tmp_buf, 8, buffer2 + 8 * idx);
123     ++tmp_buf;
124   }
125 
126   for (idx = 0; idx < 64; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
127 }
128 
129 // In place 16x16 2D Hadamard transform
vpx_highbd_hadamard_16x16_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)130 void vpx_highbd_hadamard_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
131                                  tran_low_t *coeff) {
132   int idx;
133   for (idx = 0; idx < 4; ++idx) {
134     // src_diff: 13 bit, dynamic range [-4095, 4095]
135     const int16_t *src_ptr =
136         src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
137     vpx_highbd_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
138   }
139 
140   // coeff: 19 bit, dynamic range [-262080, 262080]
141   for (idx = 0; idx < 64; ++idx) {
142     tran_low_t a0 = coeff[0];
143     tran_low_t a1 = coeff[64];
144     tran_low_t a2 = coeff[128];
145     tran_low_t a3 = coeff[192];
146 
147     tran_low_t b0 = (a0 + a1) >> 1;
148     tran_low_t b1 = (a0 - a1) >> 1;
149     tran_low_t b2 = (a2 + a3) >> 1;
150     tran_low_t b3 = (a2 - a3) >> 1;
151 
152     // new coeff dynamic range: 20 bit
153     coeff[0] = b0 + b2;
154     coeff[64] = b1 + b3;
155     coeff[128] = b0 - b2;
156     coeff[192] = b1 - b3;
157 
158     ++coeff;
159   }
160 }
161 
vpx_highbd_hadamard_32x32_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)162 void vpx_highbd_hadamard_32x32_c(const int16_t *src_diff, ptrdiff_t src_stride,
163                                  tran_low_t *coeff) {
164   int idx;
165   for (idx = 0; idx < 4; ++idx) {
166     // src_diff: 13 bit, dynamic range [-4095, 4095]
167     const int16_t *src_ptr =
168         src_diff + (idx >> 1) * 16 * src_stride + (idx & 0x01) * 16;
169     vpx_highbd_hadamard_16x16_c(src_ptr, src_stride, coeff + idx * 256);
170   }
171 
172   // coeff: 20 bit
173   for (idx = 0; idx < 256; ++idx) {
174     tran_low_t a0 = coeff[0];
175     tran_low_t a1 = coeff[256];
176     tran_low_t a2 = coeff[512];
177     tran_low_t a3 = coeff[768];
178 
179     tran_low_t b0 = (a0 + a1) >> 2;
180     tran_low_t b1 = (a0 - a1) >> 2;
181     tran_low_t b2 = (a2 + a3) >> 2;
182     tran_low_t b3 = (a2 - a3) >> 2;
183 
184     // new coeff dynamic range: 20 bit
185     coeff[0] = b0 + b2;
186     coeff[256] = b1 + b3;
187     coeff[512] = b0 - b2;
188     coeff[768] = b1 - b3;
189 
190     ++coeff;
191   }
192 }
193 #endif  // CONFIG_VP9_HIGHBITDEPTH
194 
195 // src_diff: first pass, 9 bit, dynamic range [-255, 255]
196 //           second pass, 12 bit, dynamic range [-2040, 2040]
hadamard_col8(const int16_t * src_diff,ptrdiff_t src_stride,int16_t * coeff)197 static void hadamard_col8(const int16_t *src_diff, ptrdiff_t src_stride,
198                           int16_t *coeff) {
199   int16_t b0 = src_diff[0 * src_stride] + src_diff[1 * src_stride];
200   int16_t b1 = src_diff[0 * src_stride] - src_diff[1 * src_stride];
201   int16_t b2 = src_diff[2 * src_stride] + src_diff[3 * src_stride];
202   int16_t b3 = src_diff[2 * src_stride] - src_diff[3 * src_stride];
203   int16_t b4 = src_diff[4 * src_stride] + src_diff[5 * src_stride];
204   int16_t b5 = src_diff[4 * src_stride] - src_diff[5 * src_stride];
205   int16_t b6 = src_diff[6 * src_stride] + src_diff[7 * src_stride];
206   int16_t b7 = src_diff[6 * src_stride] - src_diff[7 * src_stride];
207 
208   int16_t c0 = b0 + b2;
209   int16_t c1 = b1 + b3;
210   int16_t c2 = b0 - b2;
211   int16_t c3 = b1 - b3;
212   int16_t c4 = b4 + b6;
213   int16_t c5 = b5 + b7;
214   int16_t c6 = b4 - b6;
215   int16_t c7 = b5 - b7;
216 
217   coeff[0] = c0 + c4;
218   coeff[7] = c1 + c5;
219   coeff[3] = c2 + c6;
220   coeff[4] = c3 + c7;
221   coeff[2] = c0 - c4;
222   coeff[6] = c1 - c5;
223   coeff[1] = c2 - c6;
224   coeff[5] = c3 - c7;
225 }
226 
227 // The order of the output coeff of the hadamard is not important. For
228 // optimization purposes the final transpose may be skipped.
vpx_hadamard_8x8_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)229 void vpx_hadamard_8x8_c(const int16_t *src_diff, ptrdiff_t src_stride,
230                         tran_low_t *coeff) {
231   int idx;
232   int16_t buffer[64];
233   int16_t buffer2[64];
234   int16_t *tmp_buf = &buffer[0];
235   for (idx = 0; idx < 8; ++idx) {
236     hadamard_col8(src_diff, src_stride, tmp_buf);  // src_diff: 9 bit
237                                                    // dynamic range [-255, 255]
238     tmp_buf += 8;
239     ++src_diff;
240   }
241 
242   tmp_buf = &buffer[0];
243   for (idx = 0; idx < 8; ++idx) {
244     hadamard_col8(tmp_buf, 8, buffer2 + 8 * idx);  // tmp_buf: 12 bit
245     // dynamic range [-2040, 2040]
246     // buffer2: 15 bit
247     // dynamic range [-16320, 16320]
248     ++tmp_buf;
249   }
250 
251   for (idx = 0; idx < 64; ++idx) coeff[idx] = (tran_low_t)buffer2[idx];
252 }
253 
254 // In place 16x16 2D Hadamard transform
vpx_hadamard_16x16_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)255 void vpx_hadamard_16x16_c(const int16_t *src_diff, ptrdiff_t src_stride,
256                           tran_low_t *coeff) {
257   int idx;
258   for (idx = 0; idx < 4; ++idx) {
259     // src_diff: 9 bit, dynamic range [-255, 255]
260     const int16_t *src_ptr =
261         src_diff + (idx >> 1) * 8 * src_stride + (idx & 0x01) * 8;
262     vpx_hadamard_8x8_c(src_ptr, src_stride, coeff + idx * 64);
263   }
264 
265   // coeff: 15 bit, dynamic range [-16320, 16320]
266   for (idx = 0; idx < 64; ++idx) {
267     tran_low_t a0 = coeff[0];
268     tran_low_t a1 = coeff[64];
269     tran_low_t a2 = coeff[128];
270     tran_low_t a3 = coeff[192];
271 
272     tran_low_t b0 = (a0 + a1) >> 1;  // (a0 + a1): 16 bit, [-32640, 32640]
273     tran_low_t b1 = (a0 - a1) >> 1;  // b0-b3: 15 bit, dynamic range
274     tran_low_t b2 = (a2 + a3) >> 1;  // [-16320, 16320]
275     tran_low_t b3 = (a2 - a3) >> 1;
276 
277     coeff[0] = b0 + b2;  // 16 bit, [-32640, 32640]
278     coeff[64] = b1 + b3;
279     coeff[128] = b0 - b2;
280     coeff[192] = b1 - b3;
281 
282     ++coeff;
283   }
284 }
285 
vpx_hadamard_32x32_c(const int16_t * src_diff,ptrdiff_t src_stride,tran_low_t * coeff)286 void vpx_hadamard_32x32_c(const int16_t *src_diff, ptrdiff_t src_stride,
287                           tran_low_t *coeff) {
288   int idx;
289   for (idx = 0; idx < 4; ++idx) {
290     // src_diff: 9 bit, dynamic range [-255, 255]
291     const int16_t *src_ptr =
292         src_diff + (idx >> 1) * 16 * src_stride + (idx & 0x01) * 16;
293     vpx_hadamard_16x16_c(src_ptr, src_stride, coeff + idx * 256);
294   }
295 
296   // coeff: 15 bit, dynamic range [-16320, 16320]
297   for (idx = 0; idx < 256; ++idx) {
298     tran_low_t a0 = coeff[0];
299     tran_low_t a1 = coeff[256];
300     tran_low_t a2 = coeff[512];
301     tran_low_t a3 = coeff[768];
302 
303     tran_low_t b0 = (a0 + a1) >> 2;  // (a0 + a1): 16 bit, [-32640, 32640]
304     tran_low_t b1 = (a0 - a1) >> 2;  // b0-b3: 15 bit, dynamic range
305     tran_low_t b2 = (a2 + a3) >> 2;  // [-16320, 16320]
306     tran_low_t b3 = (a2 - a3) >> 2;
307 
308     coeff[0] = b0 + b2;  // 16 bit, [-32640, 32640]
309     coeff[256] = b1 + b3;
310     coeff[512] = b0 - b2;
311     coeff[768] = b1 - b3;
312 
313     ++coeff;
314   }
315 }
316 
317 #if CONFIG_VP9_HIGHBITDEPTH
318 // coeff: dynamic range 20 bit.
319 // length: value range {16, 64, 256, 1024}.
vpx_highbd_satd_c(const tran_low_t * coeff,int length)320 int vpx_highbd_satd_c(const tran_low_t *coeff, int length) {
321   int i;
322   int satd = 0;
323   for (i = 0; i < length; ++i) satd += abs(coeff[i]);
324 
325   // satd: 30 bits
326   return satd;
327 }
328 #endif  // CONFIG_VP9_HIGHBITDEPTH
329 
330 // coeff: 16 bits, dynamic range [-32640, 32640].
331 // length: value range {16, 64, 256, 1024}.
vpx_satd_c(const tran_low_t * coeff,int length)332 int vpx_satd_c(const tran_low_t *coeff, int length) {
333   int i;
334   int satd = 0;
335   for (i = 0; i < length; ++i) satd += abs(coeff[i]);
336 
337   // satd: 26 bits, dynamic range [-32640 * 1024, 32640 * 1024]
338   return satd;
339 }
340 
341 // Integer projection onto row vectors.
342 // height: value range {16, 32, 64}.
vpx_int_pro_row_c(int16_t hbuf[16],const uint8_t * ref,const int ref_stride,const int height)343 void vpx_int_pro_row_c(int16_t hbuf[16], const uint8_t *ref,
344                        const int ref_stride, const int height) {
345   int idx;
346   const int norm_factor = height >> 1;
347   for (idx = 0; idx < 16; ++idx) {
348     int i;
349     hbuf[idx] = 0;
350     // hbuf[idx]: 14 bit, dynamic range [0, 16320].
351     for (i = 0; i < height; ++i) hbuf[idx] += ref[i * ref_stride];
352     // hbuf[idx]: 9 bit, dynamic range [0, 510].
353     hbuf[idx] /= norm_factor;
354     ++ref;
355   }
356 }
357 
358 // width: value range {16, 32, 64}.
vpx_int_pro_col_c(const uint8_t * ref,const int width)359 int16_t vpx_int_pro_col_c(const uint8_t *ref, const int width) {
360   int idx;
361   int16_t sum = 0;
362   // sum: 14 bit, dynamic range [0, 16320]
363   for (idx = 0; idx < width; ++idx) sum += ref[idx];
364   return sum;
365 }
366 
367 // ref: [0 - 510]
368 // src: [0 - 510]
369 // bwl: {2, 3, 4}
vpx_vector_var_c(const int16_t * ref,const int16_t * src,const int bwl)370 int vpx_vector_var_c(const int16_t *ref, const int16_t *src, const int bwl) {
371   int i;
372   int width = 4 << bwl;
373   int sse = 0, mean = 0, var;
374 
375   for (i = 0; i < width; ++i) {
376     int diff = ref[i] - src[i];  // diff: dynamic range [-510, 510], 10 bits.
377     mean += diff;                // mean: dynamic range 16 bits.
378     sse += diff * diff;          // sse:  dynamic range 26 bits.
379   }
380 
381   // (mean * mean): dynamic range 31 bits.
382   var = sse - ((mean * mean) >> (bwl + 2));
383   return var;
384 }
385 
vpx_minmax_8x8_c(const uint8_t * s,int p,const uint8_t * d,int dp,int * min,int * max)386 void vpx_minmax_8x8_c(const uint8_t *s, int p, const uint8_t *d, int dp,
387                       int *min, int *max) {
388   int i, j;
389   *min = 255;
390   *max = 0;
391   for (i = 0; i < 8; ++i, s += p, d += dp) {
392     for (j = 0; j < 8; ++j) {
393       int diff = abs(s[j] - d[j]);
394       *min = diff < *min ? diff : *min;
395       *max = diff > *max ? diff : *max;
396     }
397   }
398 }
399 
400 #if CONFIG_VP9_HIGHBITDEPTH
vpx_highbd_avg_8x8_c(const uint8_t * s8,int p)401 unsigned int vpx_highbd_avg_8x8_c(const uint8_t *s8, int p) {
402   int i, j;
403   int sum = 0;
404   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
405   for (i = 0; i < 8; ++i, s += p)
406     for (j = 0; j < 8; sum += s[j], ++j) {
407     }
408 
409   return (sum + 32) >> 6;
410 }
411 
vpx_highbd_avg_4x4_c(const uint8_t * s8,int p)412 unsigned int vpx_highbd_avg_4x4_c(const uint8_t *s8, int p) {
413   int i, j;
414   int sum = 0;
415   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
416   for (i = 0; i < 4; ++i, s += p)
417     for (j = 0; j < 4; sum += s[j], ++j) {
418     }
419 
420   return (sum + 8) >> 4;
421 }
422 
vpx_highbd_minmax_8x8_c(const uint8_t * s8,int p,const uint8_t * d8,int dp,int * min,int * max)423 void vpx_highbd_minmax_8x8_c(const uint8_t *s8, int p, const uint8_t *d8,
424                              int dp, int *min, int *max) {
425   int i, j;
426   const uint16_t *s = CONVERT_TO_SHORTPTR(s8);
427   const uint16_t *d = CONVERT_TO_SHORTPTR(d8);
428   *min = 255;
429   *max = 0;
430   for (i = 0; i < 8; ++i, s += p, d += dp) {
431     for (j = 0; j < 8; ++j) {
432       int diff = abs(s[j] - d[j]);
433       *min = diff < *min ? diff : *min;
434       *max = diff > *max ? diff : *max;
435     }
436   }
437 }
438 #endif  // CONFIG_VP9_HIGHBITDEPTH
439