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> 22#include "checkasm.h" 23#include "libavcodec/avcodec.h" 24#include "libavcodec/h264dsp.h" 25#include "libavcodec/h264data.h" 26#include "libavcodec/h264_parse.h" 27#include "libavutil/common.h" 28#include "libavutil/internal.h" 29#include "libavutil/intreadwrite.h" 30#include "libavutil/mem_internal.h" 31 32static const uint32_t pixel_mask[3] = { 0xffffffff, 0x01ff01ff, 0x03ff03ff }; 33static const uint32_t pixel_mask_lf[3] = { 0xff0fff0f, 0x01ff000f, 0x03ff000f }; 34 35#define SIZEOF_PIXEL ((bit_depth + 7) / 8) 36#define SIZEOF_COEF (2 * ((bit_depth + 7) / 8)) 37#define PIXEL_STRIDE 16 38 39#define randomize_buffers() \ 40 do { \ 41 int x, y; \ 42 uint32_t mask = pixel_mask[bit_depth - 8]; \ 43 for (y = 0; y < sz; y++) { \ 44 for (x = 0; x < PIXEL_STRIDE; x += 4) { \ 45 AV_WN32A(src + y * PIXEL_STRIDE + x, rnd() & mask); \ 46 AV_WN32A(dst + y * PIXEL_STRIDE + x, rnd() & mask); \ 47 } \ 48 for (x = 0; x < sz; x++) { \ 49 if (bit_depth == 8) { \ 50 coef[y * sz + x] = src[y * PIXEL_STRIDE + x] - \ 51 dst[y * PIXEL_STRIDE + x]; \ 52 } else { \ 53 ((int32_t *)coef)[y * sz + x] = \ 54 ((uint16_t *)src)[y * (PIXEL_STRIDE/2) + x] - \ 55 ((uint16_t *)dst)[y * (PIXEL_STRIDE/2) + x]; \ 56 } \ 57 } \ 58 } \ 59 } while (0) 60 61#define dct4x4_impl(size, dctcoef) \ 62static void dct4x4_##size(dctcoef *coef) \ 63{ \ 64 int i, y, x; \ 65 dctcoef tmp[16]; \ 66 for (i = 0; i < 4; i++) { \ 67 const int z0 = coef[i*4 + 0] + coef[i*4 + 3]; \ 68 const int z1 = coef[i*4 + 1] + coef[i*4 + 2]; \ 69 const int z2 = coef[i*4 + 0] - coef[i*4 + 3]; \ 70 const int z3 = coef[i*4 + 1] - coef[i*4 + 2]; \ 71 tmp[i + 4*0] = z0 + z1; \ 72 tmp[i + 4*1] = 2*z2 + z3; \ 73 tmp[i + 4*2] = z0 - z1; \ 74 tmp[i + 4*3] = z2 - 2*z3; \ 75 } \ 76 for (i = 0; i < 4; i++) { \ 77 const int z0 = tmp[i*4 + 0] + tmp[i*4 + 3]; \ 78 const int z1 = tmp[i*4 + 1] + tmp[i*4 + 2]; \ 79 const int z2 = tmp[i*4 + 0] - tmp[i*4 + 3]; \ 80 const int z3 = tmp[i*4 + 1] - tmp[i*4 + 2]; \ 81 coef[i*4 + 0] = z0 + z1; \ 82 coef[i*4 + 1] = 2*z2 + z3; \ 83 coef[i*4 + 2] = z0 - z1; \ 84 coef[i*4 + 3] = z2 - 2*z3; \ 85 } \ 86 for (y = 0; y < 4; y++) { \ 87 for (x = 0; x < 4; x++) { \ 88 static const int scale[] = { 13107 * 10, 8066 * 13, 5243 * 16 }; \ 89 const int idx = (y & 1) + (x & 1); \ 90 coef[y*4 + x] = (coef[y*4 + x] * scale[idx] + (1 << 14)) >> 15; \ 91 } \ 92 } \ 93} 94 95#define DCT8_1D(src, srcstride, dst, dststride) do { \ 96 const int a0 = (src)[srcstride * 0] + (src)[srcstride * 7]; \ 97 const int a1 = (src)[srcstride * 0] - (src)[srcstride * 7]; \ 98 const int a2 = (src)[srcstride * 1] + (src)[srcstride * 6]; \ 99 const int a3 = (src)[srcstride * 1] - (src)[srcstride * 6]; \ 100 const int a4 = (src)[srcstride * 2] + (src)[srcstride * 5]; \ 101 const int a5 = (src)[srcstride * 2] - (src)[srcstride * 5]; \ 102 const int a6 = (src)[srcstride * 3] + (src)[srcstride * 4]; \ 103 const int a7 = (src)[srcstride * 3] - (src)[srcstride * 4]; \ 104 const int b0 = a0 + a6; \ 105 const int b1 = a2 + a4; \ 106 const int b2 = a0 - a6; \ 107 const int b3 = a2 - a4; \ 108 const int b4 = a3 + a5 + (a1 + (a1 >> 1)); \ 109 const int b5 = a1 - a7 - (a5 + (a5 >> 1)); \ 110 const int b6 = a1 + a7 - (a3 + (a3 >> 1)); \ 111 const int b7 = a3 - a5 + (a7 + (a7 >> 1)); \ 112 (dst)[dststride * 0] = b0 + b1; \ 113 (dst)[dststride * 1] = b4 + (b7 >> 2); \ 114 (dst)[dststride * 2] = b2 + (b3 >> 1); \ 115 (dst)[dststride * 3] = b5 + (b6 >> 2); \ 116 (dst)[dststride * 4] = b0 - b1; \ 117 (dst)[dststride * 5] = b6 - (b5 >> 2); \ 118 (dst)[dststride * 6] = (b2 >> 1) - b3; \ 119 (dst)[dststride * 7] = (b4 >> 2) - b7; \ 120} while (0) 121 122#define dct8x8_impl(size, dctcoef) \ 123static void dct8x8_##size(dctcoef *coef) \ 124{ \ 125 int i, x, y; \ 126 dctcoef tmp[64]; \ 127 for (i = 0; i < 8; i++) \ 128 DCT8_1D(coef + i, 8, tmp + i, 8); \ 129 \ 130 for (i = 0; i < 8; i++) \ 131 DCT8_1D(tmp + 8*i, 1, coef + i, 8); \ 132 \ 133 for (y = 0; y < 8; y++) { \ 134 for (x = 0; x < 8; x++) { \ 135 static const int scale[] = { \ 136 13107 * 20, 11428 * 18, 20972 * 32, \ 137 12222 * 19, 16777 * 25, 15481 * 24, \ 138 }; \ 139 static const int idxmap[] = { \ 140 0, 3, 4, 3, \ 141 3, 1, 5, 1, \ 142 4, 5, 2, 5, \ 143 3, 1, 5, 1, \ 144 }; \ 145 const int idx = idxmap[(y & 3) * 4 + (x & 3)]; \ 146 coef[y*8 + x] = ((int64_t)coef[y*8 + x] * \ 147 scale[idx] + (1 << 17)) >> 18; \ 148 } \ 149 } \ 150} 151 152dct4x4_impl(16, int16_t) 153dct4x4_impl(32, int32_t) 154 155dct8x8_impl(16, int16_t) 156dct8x8_impl(32, int32_t) 157 158static void dct4x4(int16_t *coef, int bit_depth) 159{ 160 if (bit_depth == 8) 161 dct4x4_16(coef); 162 else 163 dct4x4_32((int32_t *) coef); 164} 165 166static void dct8x8(int16_t *coef, int bit_depth) 167{ 168 if (bit_depth == 8) { 169 dct8x8_16(coef); 170 } else { 171 dct8x8_32((int32_t *) coef); 172 } 173} 174 175 176static void check_idct(void) 177{ 178 LOCAL_ALIGNED_16(uint8_t, src, [8 * 8 * 2]); 179 LOCAL_ALIGNED_16(uint8_t, dst, [8 * 8 * 2]); 180 LOCAL_ALIGNED_16(uint8_t, dst0, [8 * 8 * 2]); 181 LOCAL_ALIGNED_16(uint8_t, dst1_base, [8 * 8 * 2 + 32]); 182 LOCAL_ALIGNED_16(int16_t, coef, [8 * 8 * 2]); 183 LOCAL_ALIGNED_16(int16_t, subcoef0, [8 * 8 * 2]); 184 LOCAL_ALIGNED_16(int16_t, subcoef1, [8 * 8 * 2]); 185 H264DSPContext h; 186 int bit_depth, sz, align, dc; 187 declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, int16_t *block, int stride); 188 189 for (bit_depth = 8; bit_depth <= 10; bit_depth++) { 190 ff_h264dsp_init(&h, bit_depth, 1); 191 for (sz = 4; sz <= 8; sz += 4) { 192 randomize_buffers(); 193 194 if (sz == 4) 195 dct4x4(coef, bit_depth); 196 else 197 dct8x8(coef, bit_depth); 198 199 for (dc = 0; dc <= 1; dc++) { 200 void (*idct)(uint8_t *, int16_t *, int) = NULL; 201 switch ((sz << 1) | dc) { 202 case (4 << 1) | 0: idct = h.h264_idct_add; break; 203 case (4 << 1) | 1: idct = h.h264_idct_dc_add; break; 204 case (8 << 1) | 0: idct = h.h264_idct8_add; break; 205 case (8 << 1) | 1: idct = h.h264_idct8_dc_add; break; 206 } 207 if (check_func(idct, "h264_idct%d_add%s_%dbpp", sz, dc ? "_dc" : "", bit_depth)) { 208 for (align = 0; align < 16; align += sz * SIZEOF_PIXEL) { 209 uint8_t *dst1 = dst1_base + align; 210 if (dc) { 211 memset(subcoef0, 0, sz * sz * SIZEOF_COEF); 212 memcpy(subcoef0, coef, SIZEOF_COEF); 213 } else { 214 memcpy(subcoef0, coef, sz * sz * SIZEOF_COEF); 215 } 216 memcpy(dst0, dst, sz * PIXEL_STRIDE); 217 memcpy(dst1, dst, sz * PIXEL_STRIDE); 218 memcpy(subcoef1, subcoef0, sz * sz * SIZEOF_COEF); 219 call_ref(dst0, subcoef0, PIXEL_STRIDE); 220 call_new(dst1, subcoef1, PIXEL_STRIDE); 221 if (memcmp(dst0, dst1, sz * PIXEL_STRIDE) || 222 memcmp(subcoef0, subcoef1, sz * sz * SIZEOF_COEF)) 223 fail(); 224 bench_new(dst1, subcoef1, sz * SIZEOF_PIXEL); 225 } 226 } 227 } 228 } 229 } 230} 231 232static void check_idct_multiple(void) 233{ 234 LOCAL_ALIGNED_16(uint8_t, dst_full, [16 * 16 * 2]); 235 LOCAL_ALIGNED_16(int16_t, coef_full, [16 * 16 * 2]); 236 LOCAL_ALIGNED_16(uint8_t, dst0, [16 * 16 * 2]); 237 LOCAL_ALIGNED_16(uint8_t, dst1, [16 * 16 * 2]); 238 LOCAL_ALIGNED_16(int16_t, coef0, [16 * 16 * 2]); 239 LOCAL_ALIGNED_16(int16_t, coef1, [16 * 16 * 2]); 240 LOCAL_ALIGNED_16(uint8_t, nnzc, [15 * 8]); 241 H264DSPContext h; 242 int bit_depth, i, y, func; 243 declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *dst, const int *block_offset, int16_t *block, int stride, const uint8_t nnzc[15*8]); 244 245 for (bit_depth = 8; bit_depth <= 10; bit_depth++) { 246 ff_h264dsp_init(&h, bit_depth, 1); 247 for (func = 0; func < 3; func++) { 248 void (*idct)(uint8_t *, const int *, int16_t *, int, const uint8_t[]) = NULL; 249 const char *name; 250 int sz = 4, intra = 0; 251 int block_offset[16] = { 0 }; 252 switch (func) { 253 case 0: 254 idct = h.h264_idct_add16; 255 name = "h264_idct_add16"; 256 break; 257 case 1: 258 idct = h.h264_idct_add16intra; 259 name = "h264_idct_add16intra"; 260 intra = 1; 261 break; 262 case 2: 263 idct = h.h264_idct8_add4; 264 name = "h264_idct8_add4"; 265 sz = 8; 266 break; 267 } 268 memset(nnzc, 0, 15 * 8); 269 memset(coef_full, 0, 16 * 16 * SIZEOF_COEF); 270 for (i = 0; i < 16 * 16; i += sz * sz) { 271 uint8_t src[8 * 8 * 2]; 272 uint8_t dst[8 * 8 * 2]; 273 int16_t coef[8 * 8 * 2]; 274 int index = i / sz; 275 int block_y = (index / 16) * sz; 276 int block_x = index % 16; 277 int offset = (block_y * 16 + block_x) * SIZEOF_PIXEL; 278 int nnz = rnd() % 3; 279 280 randomize_buffers(); 281 if (sz == 4) 282 dct4x4(coef, bit_depth); 283 else 284 dct8x8(coef, bit_depth); 285 286 for (y = 0; y < sz; y++) 287 memcpy(&dst_full[offset + y * 16 * SIZEOF_PIXEL], 288 &dst[PIXEL_STRIDE * y], sz * SIZEOF_PIXEL); 289 290 if (nnz > 1) 291 nnz = sz * sz; 292 memcpy(&coef_full[i * SIZEOF_COEF/sizeof(coef[0])], 293 coef, nnz * SIZEOF_COEF); 294 295 if (intra && nnz == 1) 296 nnz = 0; 297 298 nnzc[scan8[i / 16]] = nnz; 299 block_offset[i / 16] = offset; 300 } 301 302 if (check_func(idct, "%s_%dbpp", name, bit_depth)) { 303 memcpy(coef0, coef_full, 16 * 16 * SIZEOF_COEF); 304 memcpy(coef1, coef_full, 16 * 16 * SIZEOF_COEF); 305 memcpy(dst0, dst_full, 16 * 16 * SIZEOF_PIXEL); 306 memcpy(dst1, dst_full, 16 * 16 * SIZEOF_PIXEL); 307 call_ref(dst0, block_offset, coef0, 16 * SIZEOF_PIXEL, nnzc); 308 call_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc); 309 if (memcmp(dst0, dst1, 16 * 16 * SIZEOF_PIXEL) || 310 memcmp(coef0, coef1, 16 * 16 * SIZEOF_COEF)) 311 fail(); 312 bench_new(dst1, block_offset, coef1, 16 * SIZEOF_PIXEL, nnzc); 313 } 314 } 315 } 316} 317 318 319static void check_loop_filter(void) 320{ 321 LOCAL_ALIGNED_16(uint8_t, dst, [32 * 16 * 2]); 322 LOCAL_ALIGNED_16(uint8_t, dst0, [32 * 16 * 2]); 323 LOCAL_ALIGNED_16(uint8_t, dst1, [32 * 16 * 2]); 324 H264DSPContext h; 325 int bit_depth; 326 int alphas[36], betas[36]; 327 int8_t tc0[36][4]; 328 329 declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *pix, ptrdiff_t stride, 330 int alpha, int beta, int8_t *tc0); 331 332 for (bit_depth = 8; bit_depth <= 10; bit_depth++) { 333 int i, j, a, c; 334 uint32_t mask = pixel_mask_lf[bit_depth - 8]; 335 ff_h264dsp_init(&h, bit_depth, 1); 336 for (i = 35, a = 255, c = 250; i >= 0; i--) { 337 alphas[i] = a << (bit_depth - 8); 338 betas[i] = (i + 1) / 2 << (bit_depth - 8); 339 tc0[i][0] = tc0[i][3] = (c + 6) / 10; 340 tc0[i][1] = (c + 7) / 15; 341 tc0[i][2] = (c + 9) / 20; 342 a = a*9/10; 343 c = c*9/10; 344 } 345 346#define CHECK_LOOP_FILTER(name, align, idc) \ 347 do { \ 348 if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \ 349 for (j = 0; j < 36; j++) { \ 350 intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \ 351 for (i = 0; i < 1024; i+=4) { \ 352 AV_WN32A(dst + i, rnd() & mask); \ 353 } \ 354 memcpy(dst0, dst, 32 * 16 * 2); \ 355 memcpy(dst1, dst, 32 * 16 * 2); \ 356 \ 357 call_ref(dst0 + off, 32, alphas[j], betas[j], tc0[j]); \ 358 call_new(dst1 + off, 32, alphas[j], betas[j], tc0[j]); \ 359 if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \ 360 fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d " \ 361 "tc0:{%d,%d,%d,%d}\n", j, alphas[j], betas[j], \ 362 tc0[j][0], tc0[j][1], tc0[j][2], tc0[j][3]); \ 363 fail(); \ 364 } \ 365 bench_new(dst1, 32, alphas[j], betas[j], tc0[j]); \ 366 } \ 367 } \ 368 } while (0) 369 370 CHECK_LOOP_FILTER(h264_v_loop_filter_luma, 1,); 371 CHECK_LOOP_FILTER(h264_h_loop_filter_luma, 0,); 372 CHECK_LOOP_FILTER(h264_h_loop_filter_luma_mbaff, 0,); 373 CHECK_LOOP_FILTER(h264_v_loop_filter_chroma, 1,); 374 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma, 0,); 375 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff, 0,); 376 377 ff_h264dsp_init(&h, bit_depth, 2); 378 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma, 0, 422); 379 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff, 0, 422); 380#undef CHECK_LOOP_FILTER 381 } 382} 383 384static void check_loop_filter_intra(void) 385{ 386 LOCAL_ALIGNED_16(uint8_t, dst, [32 * 16 * 2]); 387 LOCAL_ALIGNED_16(uint8_t, dst0, [32 * 16 * 2]); 388 LOCAL_ALIGNED_16(uint8_t, dst1, [32 * 16 * 2]); 389 H264DSPContext h; 390 int bit_depth; 391 int alphas[36], betas[36]; 392 393 declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *pix, ptrdiff_t stride, 394 int alpha, int beta); 395 396 for (bit_depth = 8; bit_depth <= 10; bit_depth++) { 397 int i, j, a; 398 uint32_t mask = pixel_mask_lf[bit_depth - 8]; 399 ff_h264dsp_init(&h, bit_depth, 1); 400 for (i = 35, a = 255; i >= 0; i--) { 401 alphas[i] = a << (bit_depth - 8); 402 betas[i] = (i + 1) / 2 << (bit_depth - 8); 403 a = a*9/10; 404 } 405 406#define CHECK_LOOP_FILTER(name, align, idc) \ 407 do { \ 408 if (check_func(h.name, #name #idc "_%dbpp", bit_depth)) { \ 409 for (j = 0; j < 36; j++) { \ 410 intptr_t off = 8 * 32 + (j & 15) * 4 * !align; \ 411 for (i = 0; i < 1024; i+=4) { \ 412 AV_WN32A(dst + i, rnd() & mask); \ 413 } \ 414 memcpy(dst0, dst, 32 * 16 * 2); \ 415 memcpy(dst1, dst, 32 * 16 * 2); \ 416 \ 417 call_ref(dst0 + off, 32, alphas[j], betas[j]); \ 418 call_new(dst1 + off, 32, alphas[j], betas[j]); \ 419 if (memcmp(dst0, dst1, 32 * 16 * SIZEOF_PIXEL)) { \ 420 fprintf(stderr, #name #idc ": j:%d, alpha:%d beta:%d\n", \ 421 j, alphas[j], betas[j]); \ 422 fail(); \ 423 } \ 424 bench_new(dst1, 32, alphas[j], betas[j]); \ 425 } \ 426 } \ 427 } while (0) 428 429 CHECK_LOOP_FILTER(h264_v_loop_filter_luma_intra, 1,); 430 CHECK_LOOP_FILTER(h264_h_loop_filter_luma_intra, 0,); 431 CHECK_LOOP_FILTER(h264_h_loop_filter_luma_mbaff_intra, 0,); 432 CHECK_LOOP_FILTER(h264_v_loop_filter_chroma_intra, 1,); 433 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_intra, 0,); 434 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff_intra, 0,); 435 436 ff_h264dsp_init(&h, bit_depth, 2); 437 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_intra, 0, 422); 438 CHECK_LOOP_FILTER(h264_h_loop_filter_chroma_mbaff_intra, 0, 422); 439#undef CHECK_LOOP_FILTER 440 } 441} 442 443void checkasm_check_h264dsp(void) 444{ 445 check_idct(); 446 check_idct_multiple(); 447 report("idct"); 448 449 check_loop_filter(); 450 report("loop_filter"); 451 452 check_loop_filter_intra(); 453 report("loop_filter_intra"); 454} 455