1 /*
2 * Copyright (c) 2016 Martin Storsjo
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (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
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <string.h>
30
31 static const uint32_t
pixel_mask[5] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff, 0x0fff0fff, 0x3fff3fff };
32 static const uint32_t
pixel_mask_lf[3] = { 0xff0fff0f, 0x01ff000f, 0x03ff000f };
33
34 #define SIZEOF_PIXEL ((bit_depth + 7) / 8)
35 #define SIZEOF_COEF (2 * ((bit_depth + 7) / 8))
36 #define PIXEL_STRIDE 16
37
38 #define randomize_buffers(idx) \
39 do { \
40 int x, y; \
41 uint32_t mask = pixel_mask[(idx)]; \
42 for (y = 0; y < sz; y++) { \
43 for (x = 0; x < PIXEL_STRIDE; x += 4) { \
44 AV_WN32A(src + y * PIXEL_STRIDE + x, rnd() & mask); \
45 AV_WN32A(dst + y * PIXEL_STRIDE + x, rnd() & mask); \
46 } \
47 for (x = 0; x < sz; x++) { \
48 if (bit_depth == 8) { \
49 coef[y * sz + x] = src[y * PIXEL_STRIDE + x] - \
50 dst[y * PIXEL_STRIDE + x]; \
51 } else { \
52 ((int32_t *)coef)[y * sz + x] = \
53 ((uint16_t *)src)[y * (PIXEL_STRIDE/2) + x] - \
54 ((uint16_t *)dst)[y * (PIXEL_STRIDE/2) + x]; \
55 } \
56 } \
57 } \
58 } while (0)
59
60 #define dct4x4_impl(size, dctcoef) \
61 static void dct4x4_##size(dctcoef *coef) \
62 { \
63 int i, y, x; \
64 dctcoef tmp[16]; \
65 for (i = 0; i < 4; i++) { \
66 const int z0 = coef[i*4 + 0] + coef[i*4 + 3]; \
67 const int z1 = coef[i*4 + 1] + coef[i*4 + 2]; \
68 const int z2 = coef[i*4 + 0] - coef[i*4 + 3]; \
69 const int z3 = coef[i*4 + 1] - coef[i*4 + 2]; \
70 tmp[i + 4*0] = z0 + z1; \
71 tmp[i + 4*1] = 2*z2 + z3; \
72 tmp[i + 4*2] = z0 - z1; \
73 tmp[i + 4*3] = z2 - 2*z3; \
74 } \
75 for (i = 0; i < 4; i++) { \
76 const int z0 = tmp[i*4 + 0] + tmp[i*4 + 3]; \
77 const int z1 = tmp[i*4 + 1] + tmp[i*4 + 2]; \
78 const int z2 = tmp[i*4 + 0] - tmp[i*4 + 3]; \
79 const int z3 = tmp[i*4 + 1] - tmp[i*4 + 2]; \
80 coef[i*4 + 0] = z0 + z1; \
81 coef[i*4 + 1] = 2*z2 + z3; \
82 coef[i*4 + 2] = z0 - z1; \
83 coef[i*4 + 3] = z2 - 2*z3; \
84 } \
85 for (y = 0; y < 4; y++) { \
86 for (x = 0; x < 4; x++) { \
87 const int64_t scale[] = { 13107 * 10, 8066 * 13, 5243 * 16 }; \
88 const int idx = (y & 1) + (x & 1); \
89 coef[y*4 + x] = (coef[y*4 + x] * scale[idx] + (1 << 14)) >> 15; \
90 } \
91 } \
92 }
93
94 #define DCT8_1D(src, srcstride, dst, dststride) do { \
95 const int a0 = (src)[srcstride * 0] + (src)[srcstride * 7]; \
96 const int a1 = (src)[srcstride * 0] - (src)[srcstride * 7]; \
97 const int a2 = (src)[srcstride * 1] + (src)[srcstride * 6]; \
98 const int a3 = (src)[srcstride * 1] - (src)[srcstride * 6]; \
99 const int a4 = (src)[srcstride * 2] + (src)[srcstride * 5]; \
100 const int a5 = (src)[srcstride * 2] - (src)[srcstride * 5]; \
101 const int a6 = (src)[srcstride * 3] + (src)[srcstride * 4]; \
102 const int a7 = (src)[srcstride * 3] - (src)[srcstride * 4]; \
103 const int b0 = a0 + a6; \
104 const int b1 = a2 + a4; \
105 const int b2 = a0 - a6; \
106 const int b3 = a2 - a4; \
107 const int b4 = a3 + a5 + (a1 + (a1 >> 1)); \
108 const int b5 = a1 - a7 - (a5 + (a5 >> 1)); \
109 const int b6 = a1 + a7 - (a3 + (a3 >> 1)); \
110 const int b7 = a3 - a5 + (a7 + (a7 >> 1)); \
111 (dst)[dststride * 0] = b0 + b1; \
112 (dst)[dststride * 1] = b4 + (b7 >> 2); \
113 (dst)[dststride * 2] = b2 + (b3 >> 1); \
114 (dst)[dststride * 3] = b5 + (b6 >> 2); \
115 (dst)[dststride * 4] = b0 - b1; \
116 (dst)[dststride * 5] = b6 - (b5 >> 2); \
117 (dst)[dststride * 6] = (b2 >> 1) - b3; \
118 (dst)[dststride * 7] = (b4 >> 2) - b7; \
119 } while (0)
120
121 #define dct8x8_impl(size, dctcoef) \
122 static void dct8x8_##size(dctcoef *coef) \
123 { \
124 int i, x, y; \
125 dctcoef tmp[64]; \
126 for (i = 0; i < 8; i++) \
127 DCT8_1D(coef + i, 8, tmp + i, 8); \
128 \
129 for (i = 0; i < 8; i++) \
130 DCT8_1D(tmp + 8*i, 1, coef + i, 8); \
131 \
132 for (y = 0; y < 8; y++) { \
133 for (x = 0; x < 8; x++) { \
134 static const int scale[] = { \
135 13107 * 20, 11428 * 18, 20972 * 32, \
136 12222 * 19, 16777 * 25, 15481 * 24, \
137 }; \
138 static const int idxmap[] = { \
139 0, 3, 4, 3, \
140 3, 1, 5, 1, \
141 4, 5, 2, 5, \
142 3, 1, 5, 1, \
143 }; \
144 const int idx = idxmap[(y & 3) * 4 + (x & 3)]; \
145 coef[y*8 + x] = ((int64_t)coef[y*8 + x] * \
146 scale[idx] + (1 << 17)) >> 18; \
147 } \
148 } \
149 }
150
153
156
158 {
160 dct4x4_16(coef);
161 else
163 }
164
166 {
168 dct8x8_16(coef);
169 } else {
171 }
172 }
173
174
176 {
177 static const int depths[5] = { 8, 9, 10, 12, 14 };
188
192
193 for (
dc = 0;
dc <= 2;
dc++) {
194 for (sz = 4; sz <= 8; sz += 4) {
195 void (*
idct)(uint8_t *, int16_t *, int) =
NULL;
196 const char fmts[3][28] = {
197 "h264_idct%d_add_%dbpp", "h264_idct%d_dc_add_%dbpp",
198 "h264_add_pixels%d_%dbpp",
199 };
200
202
203 if (sz == 4)
205 else
207
208 switch ((sz << 2) |
dc) {
209 case (4 << 2) | 0:
idct =
h.h264_idct_add;
break;
210 case (4 << 2) | 1:
idct =
h.h264_idct_dc_add;
break;
211 case (4 << 2) | 2:
idct =
h.h264_add_pixels4_clear;
break;
212 case (8 << 2) | 0:
idct =
h.h264_idct8_add;
break;
213 case (8 << 2) | 1:
idct =
h.h264_idct8_dc_add;
break;
214 case (8 << 2) | 2:
idct =
h.h264_add_pixels8_clear;
break;
215 }
216
219 uint8_t *dst1 = dst1_base +
align;
223 } else {
225 }
235 }
236 }
237 }
238 }
239 }
240 }
241
243 {
254
258 void (*
idct)(uint8_t *,
const int *, int16_t *, int,
const uint8_t[]) =
NULL;
260 int sz = 4, intra = 0;
261 int block_offset[16] = { 0 };
263 case 0:
264 idct =
h.h264_idct_add16;
265 name =
"h264_idct_add16";
266 break;
267 case 1:
268 idct =
h.h264_idct_add16intra;
269 name =
"h264_idct_add16intra";
270 intra = 1;
271 break;
272 case 2:
273 idct =
h.h264_idct8_add4;
274 name =
"h264_idct8_add4";
275 sz = 8;
276 break;
277 }
278 memset(nnzc, 0, 15 * 8);
280 for (
i = 0;
i < 16 * 16;
i += sz * sz) {
281 uint8_t
src[8 * 8 * 2];
282 uint8_t
dst[8 * 8 * 2];
283 int16_t coef[8 * 8 * 2];
285 int block_y = (
index / 16) * sz;
286 int block_x =
index % 16;
289
291 if (sz == 4)
293 else
295
296 for (y = 0; y < sz; y++)
299
300 if (nnz > 1)
301 nnz = sz * sz;
304
305 if (intra && nnz == 1)
306 nnz = 0;
307
308 nnzc[
scan8[
i / 16]] = nnz;
310 }
311
323 }
324 }
325 }
326 }
327
329 {
330 static const int depths[5] = { 8, 9, 10, 12, 14 };
340
342
346
347 void *
src, *dst_ref, *dst_new;
350 dst_ref = dst0_16;
351 dst_new = dst1_16;
352 for (int j = 0; j < 16; j++)
353 src16[j] = (
rnd() % 512) - 256;
354 } else {
356 dst_ref = dst0_32;
357 dst_new = dst1_32;
358 for (int j = 0; j < 16; j++)
360 }
363
365
370 }
371 }
372 }
373
374
376 {
382 int alphas[36], betas[36];
383 int8_t tc0[36][4];
384
386 int alpha,
int beta, int8_t *tc0);
387
392 for (
i = 35,
a = 255,
c = 250;
i >= 0;
i--) {
395 tc0[
i][0] = tc0[
i][3] = (
c + 6) / 10;
396 tc0[
i][1] = (
c + 7) / 15;
397 tc0[
i][2] = (
c + 9) / 20;
400 }
401
402 #define CHECK_LOOP_FILTER(name, align, idc) \
403 do { \
404 if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \
405 for (j = 0; j < 36; j++) { \
406 intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \
407 for (i = 0; i < 1024; i+=4) { \
408 AV_WN32A(dst + i, rnd() & mask); \
409 } \
410 memcpy(dst0, dst, 32 * 16 * 2); \
411 memcpy(dst1, dst, 32 * 16 * 2); \
412 \
413 call_ref(dst0 + off, 32, alphas[j], betas[j], tc0[j]); \
414 call_new(dst1 + off, 32, alphas[j], betas[j], tc0[j]); \
415 if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \
416 fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d " \
417 "tc0:{%d,%d,%d,%d}\n", j, alphas[j], betas[j], \
418 tc0[j][0], tc0[j][1], tc0[j][2], tc0[j][3]); \
419 fail(); \
420 } \
421 bench_new(dst1 + off, 32, alphas[j], betas[j], tc0[j]);\
422 } \
423 } \
424 } while (0)
425
432
436 #undef CHECK_LOOP_FILTER
437 }
438 }
439
441 {
447 int alphas[36], betas[36];
448
450 int alpha,
int beta);
451
456 for (
i = 35,
a = 255;
i >= 0;
i--) {
460 }
461
462 #define CHECK_LOOP_FILTER(name, align, idc) \
463 do { \
464 if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \
465 for (j = 0; j < 36; j++) { \
466 intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \
467 for (i = 0; i < 1024; i+=4) { \
468 AV_WN32A(dst + i, rnd() & mask); \
469 } \
470 memcpy(dst0, dst, 32 * 16 * 2); \
471 memcpy(dst1, dst, 32 * 16 * 2); \
472 \
473 call_ref(dst0 + off, 32, alphas[j], betas[j]); \
474 call_new(dst1 + off, 32, alphas[j], betas[j]); \
475 if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \
476 fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d\n", \
477 j, alphas[j], betas[j]); \
478 fail(); \
479 } \
480 bench_new(dst1 + off, 32, alphas[j], betas[j]); \
481 } \
482 } \
483 } while (0)
484
491
495 #undef CHECK_LOOP_FILTER
496 }
497 }
498
500 {
505
508
510 report(
"loop_filter_intra");
511 }