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