1 /*
2  * Copyright (c) 2017, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 
12 #include <tmmintrin.h>
13 
14 #include "config/av1_rtcd.h"
15 
16 #include "av1/common/cfl.h"
17 
18 #include "av1/common/x86/cfl_simd.h"
19 
20 // Load 32-bit integer from memory into the first element of dst.
_mm_loadh_epi32(__m128i const * mem_addr)21 static INLINE __m128i _mm_loadh_epi32(__m128i const *mem_addr) {
22   return _mm_cvtsi32_si128(*((int *)mem_addr));
23 }
24 
25 // Store 32-bit integer from the first element of a into memory.
_mm_storeh_epi32(__m128i const * mem_addr,__m128i a)26 static INLINE void _mm_storeh_epi32(__m128i const *mem_addr, __m128i a) {
27   *((int *)mem_addr) = _mm_cvtsi128_si32(a);
28 }
29 
30 /**
31  * Adds 4 pixels (in a 2x2 grid) and multiplies them by 2. Resulting in a more
32  * precise version of a box filter 4:2:0 pixel subsampling in Q3.
33  *
34  * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
35  * active area is specified using width and height.
36  *
37  * Note: We don't need to worry about going over the active area, as long as we
38  * stay inside the CfL prediction buffer.
39  */
cfl_luma_subsampling_420_lbd_ssse3(const uint8_t * input,int input_stride,uint16_t * pred_buf_q3,int width,int height)40 static INLINE void cfl_luma_subsampling_420_lbd_ssse3(const uint8_t *input,
41                                                       int input_stride,
42                                                       uint16_t *pred_buf_q3,
43                                                       int width, int height) {
44   const __m128i twos = _mm_set1_epi8(2);
45   __m128i *pred_buf_m128i = (__m128i *)pred_buf_q3;
46   const __m128i *end = pred_buf_m128i + (height >> 1) * CFL_BUF_LINE_I128;
47   const int luma_stride = input_stride << 1;
48   do {
49     if (width == 4) {
50       __m128i top = _mm_loadh_epi32((__m128i *)input);
51       top = _mm_maddubs_epi16(top, twos);
52       __m128i bot = _mm_loadh_epi32((__m128i *)(input + input_stride));
53       bot = _mm_maddubs_epi16(bot, twos);
54       const __m128i sum = _mm_add_epi16(top, bot);
55       _mm_storeh_epi32(pred_buf_m128i, sum);
56     } else if (width == 8) {
57       __m128i top = _mm_loadl_epi64((__m128i *)input);
58       top = _mm_maddubs_epi16(top, twos);
59       __m128i bot = _mm_loadl_epi64((__m128i *)(input + input_stride));
60       bot = _mm_maddubs_epi16(bot, twos);
61       const __m128i sum = _mm_add_epi16(top, bot);
62       _mm_storel_epi64(pred_buf_m128i, sum);
63     } else {
64       __m128i top = _mm_loadu_si128((__m128i *)input);
65       top = _mm_maddubs_epi16(top, twos);
66       __m128i bot = _mm_loadu_si128((__m128i *)(input + input_stride));
67       bot = _mm_maddubs_epi16(bot, twos);
68       const __m128i sum = _mm_add_epi16(top, bot);
69       _mm_storeu_si128(pred_buf_m128i, sum);
70       if (width == 32) {
71         __m128i top_1 = _mm_loadu_si128(((__m128i *)input) + 1);
72         __m128i bot_1 =
73             _mm_loadu_si128(((__m128i *)(input + input_stride)) + 1);
74         top_1 = _mm_maddubs_epi16(top_1, twos);
75         bot_1 = _mm_maddubs_epi16(bot_1, twos);
76         __m128i sum_1 = _mm_add_epi16(top_1, bot_1);
77         _mm_storeu_si128(pred_buf_m128i + 1, sum_1);
78       }
79     }
80     input += luma_stride;
81     pred_buf_m128i += CFL_BUF_LINE_I128;
82   } while (pred_buf_m128i < end);
83 }
84 
85 /**
86  * Adds 2 pixels (in a 2x1 grid) and multiplies them by 4. Resulting in a more
87  * precise version of a box filter 4:2:2 pixel subsampling in Q3.
88  *
89  * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
90  * active area is specified using width and height.
91  *
92  * Note: We don't need to worry about going over the active area, as long as we
93  * stay inside the CfL prediction buffer.
94  */
cfl_luma_subsampling_422_lbd_ssse3(const uint8_t * input,int input_stride,uint16_t * pred_buf_q3,int width,int height)95 static INLINE void cfl_luma_subsampling_422_lbd_ssse3(const uint8_t *input,
96                                                       int input_stride,
97                                                       uint16_t *pred_buf_q3,
98                                                       int width, int height) {
99   const __m128i fours = _mm_set1_epi8(4);
100   __m128i *pred_buf_m128i = (__m128i *)pred_buf_q3;
101   const __m128i *end = pred_buf_m128i + height * CFL_BUF_LINE_I128;
102   do {
103     if (width == 4) {
104       __m128i top = _mm_loadh_epi32((__m128i *)input);
105       top = _mm_maddubs_epi16(top, fours);
106       _mm_storeh_epi32(pred_buf_m128i, top);
107     } else if (width == 8) {
108       __m128i top = _mm_loadl_epi64((__m128i *)input);
109       top = _mm_maddubs_epi16(top, fours);
110       _mm_storel_epi64(pred_buf_m128i, top);
111     } else {
112       __m128i top = _mm_loadu_si128((__m128i *)input);
113       top = _mm_maddubs_epi16(top, fours);
114       _mm_storeu_si128(pred_buf_m128i, top);
115       if (width == 32) {
116         __m128i top_1 = _mm_loadu_si128(((__m128i *)input) + 1);
117         top_1 = _mm_maddubs_epi16(top_1, fours);
118         _mm_storeu_si128(pred_buf_m128i + 1, top_1);
119       }
120     }
121     input += input_stride;
122     pred_buf_m128i += CFL_BUF_LINE_I128;
123   } while (pred_buf_m128i < end);
124 }
125 
126 /**
127  * Multiplies the pixels by 8 (scaling in Q3).
128  *
129  * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
130  * active area is specified using width and height.
131  *
132  * Note: We don't need to worry about going over the active area, as long as we
133  * stay inside the CfL prediction buffer.
134  */
cfl_luma_subsampling_444_lbd_ssse3(const uint8_t * input,int input_stride,uint16_t * pred_buf_q3,int width,int height)135 static INLINE void cfl_luma_subsampling_444_lbd_ssse3(const uint8_t *input,
136                                                       int input_stride,
137                                                       uint16_t *pred_buf_q3,
138                                                       int width, int height) {
139   const __m128i zeros = _mm_setzero_si128();
140   const int luma_stride = input_stride;
141   __m128i *pred_buf_m128i = (__m128i *)pred_buf_q3;
142   const __m128i *end = pred_buf_m128i + height * CFL_BUF_LINE_I128;
143   do {
144     if (width == 4) {
145       __m128i row = _mm_loadh_epi32((__m128i *)input);
146       row = _mm_unpacklo_epi8(row, zeros);
147       _mm_storel_epi64(pred_buf_m128i, _mm_slli_epi16(row, 3));
148     } else if (width == 8) {
149       __m128i row = _mm_loadl_epi64((__m128i *)input);
150       row = _mm_unpacklo_epi8(row, zeros);
151       _mm_storeu_si128(pred_buf_m128i, _mm_slli_epi16(row, 3));
152     } else {
153       __m128i row = _mm_loadu_si128((__m128i *)input);
154       const __m128i row_lo = _mm_unpacklo_epi8(row, zeros);
155       const __m128i row_hi = _mm_unpackhi_epi8(row, zeros);
156       _mm_storeu_si128(pred_buf_m128i, _mm_slli_epi16(row_lo, 3));
157       _mm_storeu_si128(pred_buf_m128i + 1, _mm_slli_epi16(row_hi, 3));
158       if (width == 32) {
159         __m128i row_1 = _mm_loadu_si128(((__m128i *)input) + 1);
160         const __m128i row_1_lo = _mm_unpacklo_epi8(row_1, zeros);
161         const __m128i row_1_hi = _mm_unpackhi_epi8(row_1, zeros);
162         _mm_storeu_si128(pred_buf_m128i + 2, _mm_slli_epi16(row_1_lo, 3));
163         _mm_storeu_si128(pred_buf_m128i + 3, _mm_slli_epi16(row_1_hi, 3));
164       }
165     }
166     input += luma_stride;
167     pred_buf_m128i += CFL_BUF_LINE_I128;
168   } while (pred_buf_m128i < end);
169 }
170 
171 /**
172  * Adds 4 pixels (in a 2x2 grid) and multiplies them by 2. Resulting in a more
173  * precise version of a box filter 4:2:0 pixel subsampling in Q3.
174  *
175  * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
176  * active area is specified using width and height.
177  *
178  * Note: We don't need to worry about going over the active area, as long as we
179  * stay inside the CfL prediction buffer.
180  */
cfl_luma_subsampling_420_hbd_ssse3(const uint16_t * input,int input_stride,uint16_t * pred_buf_q3,int width,int height)181 static INLINE void cfl_luma_subsampling_420_hbd_ssse3(const uint16_t *input,
182                                                       int input_stride,
183                                                       uint16_t *pred_buf_q3,
184                                                       int width, int height) {
185   const uint16_t *end = pred_buf_q3 + (height >> 1) * CFL_BUF_LINE;
186   const int luma_stride = input_stride << 1;
187   do {
188     if (width == 4) {
189       const __m128i top = _mm_loadl_epi64((__m128i *)input);
190       const __m128i bot = _mm_loadl_epi64((__m128i *)(input + input_stride));
191       __m128i sum = _mm_add_epi16(top, bot);
192       sum = _mm_hadd_epi16(sum, sum);
193       *((int *)pred_buf_q3) = _mm_cvtsi128_si32(_mm_add_epi16(sum, sum));
194     } else {
195       const __m128i top = _mm_loadu_si128((__m128i *)input);
196       const __m128i bot = _mm_loadu_si128((__m128i *)(input + input_stride));
197       __m128i sum = _mm_add_epi16(top, bot);
198       if (width == 8) {
199         sum = _mm_hadd_epi16(sum, sum);
200         _mm_storel_epi64((__m128i *)pred_buf_q3, _mm_add_epi16(sum, sum));
201       } else {
202         const __m128i top_1 = _mm_loadu_si128(((__m128i *)input) + 1);
203         const __m128i bot_1 =
204             _mm_loadu_si128(((__m128i *)(input + input_stride)) + 1);
205         sum = _mm_hadd_epi16(sum, _mm_add_epi16(top_1, bot_1));
206         _mm_storeu_si128((__m128i *)pred_buf_q3, _mm_add_epi16(sum, sum));
207         if (width == 32) {
208           const __m128i top_2 = _mm_loadu_si128(((__m128i *)input) + 2);
209           const __m128i bot_2 =
210               _mm_loadu_si128(((__m128i *)(input + input_stride)) + 2);
211           const __m128i top_3 = _mm_loadu_si128(((__m128i *)input) + 3);
212           const __m128i bot_3 =
213               _mm_loadu_si128(((__m128i *)(input + input_stride)) + 3);
214           const __m128i sum_2 = _mm_add_epi16(top_2, bot_2);
215           const __m128i sum_3 = _mm_add_epi16(top_3, bot_3);
216           __m128i next_sum = _mm_hadd_epi16(sum_2, sum_3);
217           _mm_storeu_si128(((__m128i *)pred_buf_q3) + 1,
218                            _mm_add_epi16(next_sum, next_sum));
219         }
220       }
221     }
222     input += luma_stride;
223   } while ((pred_buf_q3 += CFL_BUF_LINE) < end);
224 }
225 
226 /**
227  * Adds 2 pixels (in a 2x1 grid) and multiplies them by 4. Resulting in a more
228  * precise version of a box filter 4:2:2 pixel subsampling in Q3.
229  *
230  * The CfL prediction buffer is always of size CFL_BUF_SQUARE. However, the
231  * active area is specified using width and height.
232  *
233  * Note: We don't need to worry about going over the active area, as long as we
234  * stay inside the CfL prediction buffer.
235  */
cfl_luma_subsampling_422_hbd_ssse3(const uint16_t * input,int input_stride,uint16_t * pred_buf_q3,int width,int height)236 static INLINE void cfl_luma_subsampling_422_hbd_ssse3(const uint16_t *input,
237                                                       int input_stride,
238                                                       uint16_t *pred_buf_q3,
239                                                       int width, int height) {
240   __m128i *pred_buf_m128i = (__m128i *)pred_buf_q3;
241   const __m128i *end = pred_buf_m128i + height * CFL_BUF_LINE_I128;
242   do {
243     if (width == 4) {
244       const __m128i top = _mm_loadl_epi64((__m128i *)input);
245       const __m128i sum = _mm_slli_epi16(_mm_hadd_epi16(top, top), 2);
246       _mm_storeh_epi32(pred_buf_m128i, sum);
247     } else {
248       const __m128i top = _mm_loadu_si128((__m128i *)input);
249       if (width == 8) {
250         const __m128i sum = _mm_slli_epi16(_mm_hadd_epi16(top, top), 2);
251         _mm_storel_epi64(pred_buf_m128i, sum);
252       } else {
253         const __m128i top_1 = _mm_loadu_si128(((__m128i *)input) + 1);
254         const __m128i sum = _mm_slli_epi16(_mm_hadd_epi16(top, top_1), 2);
255         _mm_storeu_si128(pred_buf_m128i, sum);
256         if (width == 32) {
257           const __m128i top_2 = _mm_loadu_si128(((__m128i *)input) + 2);
258           const __m128i top_3 = _mm_loadu_si128(((__m128i *)input) + 3);
259           const __m128i sum_1 = _mm_slli_epi16(_mm_hadd_epi16(top_2, top_3), 2);
260           _mm_storeu_si128(pred_buf_m128i + 1, sum_1);
261         }
262       }
263     }
264     pred_buf_m128i += CFL_BUF_LINE_I128;
265     input += input_stride;
266   } while (pred_buf_m128i < end);
267 }
268 
cfl_luma_subsampling_444_hbd_ssse3(const uint16_t * input,int input_stride,uint16_t * pred_buf_q3,int width,int height)269 static INLINE void cfl_luma_subsampling_444_hbd_ssse3(const uint16_t *input,
270                                                       int input_stride,
271                                                       uint16_t *pred_buf_q3,
272                                                       int width, int height) {
273   const uint16_t *end = pred_buf_q3 + height * CFL_BUF_LINE;
274   do {
275     if (width == 4) {
276       const __m128i row = _mm_slli_epi16(_mm_loadl_epi64((__m128i *)input), 3);
277       _mm_storel_epi64((__m128i *)pred_buf_q3, row);
278     } else {
279       const __m128i row = _mm_slli_epi16(_mm_loadu_si128((__m128i *)input), 3);
280       _mm_storeu_si128((__m128i *)pred_buf_q3, row);
281       if (width >= 16) {
282         __m128i row_1 = _mm_loadu_si128(((__m128i *)input) + 1);
283         row_1 = _mm_slli_epi16(row_1, 3);
284         _mm_storeu_si128(((__m128i *)pred_buf_q3) + 1, row_1);
285         if (width == 32) {
286           __m128i row_2 = _mm_loadu_si128(((__m128i *)input) + 2);
287           row_2 = _mm_slli_epi16(row_2, 3);
288           _mm_storeu_si128(((__m128i *)pred_buf_q3) + 2, row_2);
289           __m128i row_3 = _mm_loadu_si128(((__m128i *)input) + 3);
290           row_3 = _mm_slli_epi16(row_3, 3);
291           _mm_storeu_si128(((__m128i *)pred_buf_q3) + 3, row_3);
292         }
293       }
294     }
295     input += input_stride;
296     pred_buf_q3 += CFL_BUF_LINE;
297   } while (pred_buf_q3 < end);
298 }
299 
CFL_GET_SUBSAMPLE_FUNCTION(ssse3)300 CFL_GET_SUBSAMPLE_FUNCTION(ssse3)
301 
302 static INLINE __m128i predict_unclipped(const __m128i *input, __m128i alpha_q12,
303                                         __m128i alpha_sign, __m128i dc_q0) {
304   __m128i ac_q3 = _mm_loadu_si128(input);
305   __m128i ac_sign = _mm_sign_epi16(alpha_sign, ac_q3);
306   __m128i scaled_luma_q0 = _mm_mulhrs_epi16(_mm_abs_epi16(ac_q3), alpha_q12);
307   scaled_luma_q0 = _mm_sign_epi16(scaled_luma_q0, ac_sign);
308   return _mm_add_epi16(scaled_luma_q0, dc_q0);
309 }
310 
cfl_predict_lbd_ssse3(const int16_t * pred_buf_q3,uint8_t * dst,int dst_stride,int alpha_q3,int width,int height)311 static INLINE void cfl_predict_lbd_ssse3(const int16_t *pred_buf_q3,
312                                          uint8_t *dst, int dst_stride,
313                                          int alpha_q3, int width, int height) {
314   const __m128i alpha_sign = _mm_set1_epi16(alpha_q3);
315   const __m128i alpha_q12 = _mm_slli_epi16(_mm_abs_epi16(alpha_sign), 9);
316   const __m128i dc_q0 = _mm_set1_epi16(*dst);
317   __m128i *row = (__m128i *)pred_buf_q3;
318   const __m128i *row_end = row + height * CFL_BUF_LINE_I128;
319   do {
320     __m128i res = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
321     if (width < 16) {
322       res = _mm_packus_epi16(res, res);
323       if (width == 4)
324         _mm_storeh_epi32((__m128i *)dst, res);
325       else
326         _mm_storel_epi64((__m128i *)dst, res);
327     } else {
328       __m128i next = predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
329       res = _mm_packus_epi16(res, next);
330       _mm_storeu_si128((__m128i *)dst, res);
331       if (width == 32) {
332         res = predict_unclipped(row + 2, alpha_q12, alpha_sign, dc_q0);
333         next = predict_unclipped(row + 3, alpha_q12, alpha_sign, dc_q0);
334         res = _mm_packus_epi16(res, next);
335         _mm_storeu_si128((__m128i *)(dst + 16), res);
336       }
337     }
338     dst += dst_stride;
339   } while ((row += CFL_BUF_LINE_I128) < row_end);
340 }
341 
CFL_PREDICT_FN(ssse3,lbd)342 CFL_PREDICT_FN(ssse3, lbd)
343 
344 static INLINE __m128i highbd_max_epi16(int bd) {
345   const __m128i neg_one = _mm_set1_epi16(-1);
346   // (1 << bd) - 1 => -(-1 << bd) -1 => -1 - (-1 << bd) => -1 ^ (-1 << bd)
347   return _mm_xor_si128(_mm_slli_epi16(neg_one, bd), neg_one);
348 }
349 
highbd_clamp_epi16(__m128i u,__m128i zero,__m128i max)350 static INLINE __m128i highbd_clamp_epi16(__m128i u, __m128i zero, __m128i max) {
351   return _mm_max_epi16(_mm_min_epi16(u, max), zero);
352 }
353 
cfl_predict_hbd_ssse3(const int16_t * pred_buf_q3,uint16_t * dst,int dst_stride,int alpha_q3,int bd,int width,int height)354 static INLINE void cfl_predict_hbd_ssse3(const int16_t *pred_buf_q3,
355                                          uint16_t *dst, int dst_stride,
356                                          int alpha_q3, int bd, int width,
357                                          int height) {
358   const __m128i alpha_sign = _mm_set1_epi16(alpha_q3);
359   const __m128i alpha_q12 = _mm_slli_epi16(_mm_abs_epi16(alpha_sign), 9);
360   const __m128i dc_q0 = _mm_set1_epi16(*dst);
361   const __m128i max = highbd_max_epi16(bd);
362   const __m128i zeros = _mm_setzero_si128();
363   __m128i *row = (__m128i *)pred_buf_q3;
364   const __m128i *row_end = row + height * CFL_BUF_LINE_I128;
365   do {
366     __m128i res = predict_unclipped(row, alpha_q12, alpha_sign, dc_q0);
367     res = highbd_clamp_epi16(res, zeros, max);
368     if (width == 4) {
369       _mm_storel_epi64((__m128i *)dst, res);
370     } else {
371       _mm_storeu_si128((__m128i *)dst, res);
372     }
373     if (width >= 16) {
374       const __m128i res_1 =
375           predict_unclipped(row + 1, alpha_q12, alpha_sign, dc_q0);
376       _mm_storeu_si128(((__m128i *)dst) + 1,
377                        highbd_clamp_epi16(res_1, zeros, max));
378     }
379     if (width == 32) {
380       const __m128i res_2 =
381           predict_unclipped(row + 2, alpha_q12, alpha_sign, dc_q0);
382       _mm_storeu_si128((__m128i *)(dst + 16),
383                        highbd_clamp_epi16(res_2, zeros, max));
384       const __m128i res_3 =
385           predict_unclipped(row + 3, alpha_q12, alpha_sign, dc_q0);
386       _mm_storeu_si128((__m128i *)(dst + 24),
387                        highbd_clamp_epi16(res_3, zeros, max));
388     }
389     dst += dst_stride;
390   } while ((row += CFL_BUF_LINE_I128) < row_end);
391 }
392 
393 CFL_PREDICT_FN(ssse3, hbd)
394