1cabdff1aSopenharmony_ci/* 2cabdff1aSopenharmony_ci * 3cabdff1aSopenharmony_ci * This file is part of FFmpeg. 4cabdff1aSopenharmony_ci * 5cabdff1aSopenharmony_ci * FFmpeg is free software; you can redistribute it and/or modify 6cabdff1aSopenharmony_ci * it under the terms of the GNU General Public License as published by 7cabdff1aSopenharmony_ci * the Free Software Foundation; either version 2 of the License, or 8cabdff1aSopenharmony_ci * (at your option) any later version. 9cabdff1aSopenharmony_ci * 10cabdff1aSopenharmony_ci * FFmpeg is distributed in the hope that it will be useful, 11cabdff1aSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 12cabdff1aSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13cabdff1aSopenharmony_ci * GNU General Public License for more details. 14cabdff1aSopenharmony_ci * 15cabdff1aSopenharmony_ci * You should have received a copy of the GNU General Public License along 16cabdff1aSopenharmony_ci * with FFmpeg; if not, write to the Free Software Foundation, Inc., 17cabdff1aSopenharmony_ci * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18cabdff1aSopenharmony_ci */ 19cabdff1aSopenharmony_ci 20cabdff1aSopenharmony_ci#include <string.h> 21cabdff1aSopenharmony_ci 22cabdff1aSopenharmony_ci#include "libavutil/common.h" 23cabdff1aSopenharmony_ci#include "libavutil/intreadwrite.h" 24cabdff1aSopenharmony_ci#include "libavutil/mem_internal.h" 25cabdff1aSopenharmony_ci 26cabdff1aSopenharmony_ci#include "libswscale/swscale.h" 27cabdff1aSopenharmony_ci#include "libswscale/swscale_internal.h" 28cabdff1aSopenharmony_ci 29cabdff1aSopenharmony_ci#include "checkasm.h" 30cabdff1aSopenharmony_ci 31cabdff1aSopenharmony_ci#define randomize_buffers(buf, size) \ 32cabdff1aSopenharmony_ci do { \ 33cabdff1aSopenharmony_ci int j; \ 34cabdff1aSopenharmony_ci for (j = 0; j < size; j+=4) \ 35cabdff1aSopenharmony_ci AV_WN32(buf + j, rnd()); \ 36cabdff1aSopenharmony_ci } while (0) 37cabdff1aSopenharmony_ci 38cabdff1aSopenharmony_ci// This reference function is the same approximate algorithm employed by the 39cabdff1aSopenharmony_ci// SIMD functions 40cabdff1aSopenharmony_cistatic void ref_function(const int16_t *filter, int filterSize, 41cabdff1aSopenharmony_ci const int16_t **src, uint8_t *dest, int dstW, 42cabdff1aSopenharmony_ci const uint8_t *dither, int offset) 43cabdff1aSopenharmony_ci{ 44cabdff1aSopenharmony_ci int i, d; 45cabdff1aSopenharmony_ci d = ((filterSize - 1) * 8 + dither[0]) >> 4; 46cabdff1aSopenharmony_ci for ( i = 0; i < dstW; i++) { 47cabdff1aSopenharmony_ci int16_t val = d; 48cabdff1aSopenharmony_ci int j; 49cabdff1aSopenharmony_ci union { 50cabdff1aSopenharmony_ci int val; 51cabdff1aSopenharmony_ci int16_t v[2]; 52cabdff1aSopenharmony_ci } t; 53cabdff1aSopenharmony_ci for (j = 0; j < filterSize; j++){ 54cabdff1aSopenharmony_ci t.val = (int)src[j][i + offset] * (int)filter[j]; 55cabdff1aSopenharmony_ci val += t.v[1]; 56cabdff1aSopenharmony_ci } 57cabdff1aSopenharmony_ci dest[i]= av_clip_uint8(val>>3); 58cabdff1aSopenharmony_ci } 59cabdff1aSopenharmony_ci} 60cabdff1aSopenharmony_ci 61cabdff1aSopenharmony_cistatic void check_yuv2yuvX(void) 62cabdff1aSopenharmony_ci{ 63cabdff1aSopenharmony_ci struct SwsContext *ctx; 64cabdff1aSopenharmony_ci int fsi, osi, isi, i, j; 65cabdff1aSopenharmony_ci int dstW; 66cabdff1aSopenharmony_ci#define LARGEST_FILTER 16 67cabdff1aSopenharmony_ci#define FILTER_SIZES 4 68cabdff1aSopenharmony_ci static const int filter_sizes[FILTER_SIZES] = {1, 4, 8, 16}; 69cabdff1aSopenharmony_ci#define LARGEST_INPUT_SIZE 512 70cabdff1aSopenharmony_ci#define INPUT_SIZES 6 71cabdff1aSopenharmony_ci static const int input_sizes[INPUT_SIZES] = {8, 24, 128, 144, 256, 512}; 72cabdff1aSopenharmony_ci 73cabdff1aSopenharmony_ci declare_func_emms(AV_CPU_FLAG_MMX, void, const int16_t *filter, 74cabdff1aSopenharmony_ci int filterSize, const int16_t **src, uint8_t *dest, 75cabdff1aSopenharmony_ci int dstW, const uint8_t *dither, int offset); 76cabdff1aSopenharmony_ci 77cabdff1aSopenharmony_ci const int16_t **src; 78cabdff1aSopenharmony_ci LOCAL_ALIGNED_16(int16_t, src_pixels, [LARGEST_FILTER * LARGEST_INPUT_SIZE]); 79cabdff1aSopenharmony_ci LOCAL_ALIGNED_16(int16_t, filter_coeff, [LARGEST_FILTER]); 80cabdff1aSopenharmony_ci LOCAL_ALIGNED_16(uint8_t, dst0, [LARGEST_INPUT_SIZE]); 81cabdff1aSopenharmony_ci LOCAL_ALIGNED_16(uint8_t, dst1, [LARGEST_INPUT_SIZE]); 82cabdff1aSopenharmony_ci LOCAL_ALIGNED_16(uint8_t, dither, [LARGEST_INPUT_SIZE]); 83cabdff1aSopenharmony_ci union VFilterData{ 84cabdff1aSopenharmony_ci const int16_t *src; 85cabdff1aSopenharmony_ci uint16_t coeff[8]; 86cabdff1aSopenharmony_ci } *vFilterData; 87cabdff1aSopenharmony_ci uint8_t d_val = rnd(); 88cabdff1aSopenharmony_ci memset(dither, d_val, LARGEST_INPUT_SIZE); 89cabdff1aSopenharmony_ci randomize_buffers((uint8_t*)src_pixels, LARGEST_FILTER * LARGEST_INPUT_SIZE * sizeof(int16_t)); 90cabdff1aSopenharmony_ci randomize_buffers((uint8_t*)filter_coeff, LARGEST_FILTER * sizeof(int16_t)); 91cabdff1aSopenharmony_ci ctx = sws_alloc_context(); 92cabdff1aSopenharmony_ci if (sws_init_context(ctx, NULL, NULL) < 0) 93cabdff1aSopenharmony_ci fail(); 94cabdff1aSopenharmony_ci 95cabdff1aSopenharmony_ci ff_sws_init_scale(ctx); 96cabdff1aSopenharmony_ci for(isi = 0; isi < INPUT_SIZES; ++isi){ 97cabdff1aSopenharmony_ci dstW = input_sizes[isi]; 98cabdff1aSopenharmony_ci for(osi = 0; osi < 64; osi += 16){ 99cabdff1aSopenharmony_ci for(fsi = 0; fsi < FILTER_SIZES; ++fsi){ 100cabdff1aSopenharmony_ci src = av_malloc(sizeof(int16_t*) * filter_sizes[fsi]); 101cabdff1aSopenharmony_ci vFilterData = av_malloc((filter_sizes[fsi] + 2) * sizeof(union VFilterData)); 102cabdff1aSopenharmony_ci memset(vFilterData, 0, (filter_sizes[fsi] + 2) * sizeof(union VFilterData)); 103cabdff1aSopenharmony_ci for(i = 0; i < filter_sizes[fsi]; ++i){ 104cabdff1aSopenharmony_ci src[i] = &src_pixels[i * LARGEST_INPUT_SIZE]; 105cabdff1aSopenharmony_ci vFilterData[i].src = src[i]; 106cabdff1aSopenharmony_ci for(j = 0; j < 4; ++j) 107cabdff1aSopenharmony_ci vFilterData[i].coeff[j + 4] = filter_coeff[i]; 108cabdff1aSopenharmony_ci } 109cabdff1aSopenharmony_ci if (check_func(ctx->yuv2planeX, "yuv2yuvX_%d_%d_%d", filter_sizes[fsi], osi, dstW)){ 110cabdff1aSopenharmony_ci memset(dst0, 0, LARGEST_INPUT_SIZE * sizeof(dst0[0])); 111cabdff1aSopenharmony_ci memset(dst1, 0, LARGEST_INPUT_SIZE * sizeof(dst1[0])); 112cabdff1aSopenharmony_ci 113cabdff1aSopenharmony_ci // The reference function is not the scalar function selected when mmx 114cabdff1aSopenharmony_ci // is deactivated as the SIMD functions do not give the same result as 115cabdff1aSopenharmony_ci // the scalar ones due to rounding. The SIMD functions are activated by 116cabdff1aSopenharmony_ci // the flag SWS_ACCURATE_RND 117cabdff1aSopenharmony_ci ref_function(&filter_coeff[0], filter_sizes[fsi], src, dst0, dstW - osi, dither, osi); 118cabdff1aSopenharmony_ci // There's no point in calling new for the reference function 119cabdff1aSopenharmony_ci if(ctx->use_mmx_vfilter){ 120cabdff1aSopenharmony_ci call_new((const int16_t*)vFilterData, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi); 121cabdff1aSopenharmony_ci if (memcmp(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0]))) 122cabdff1aSopenharmony_ci fail(); 123cabdff1aSopenharmony_ci if(dstW == LARGEST_INPUT_SIZE) 124cabdff1aSopenharmony_ci bench_new((const int16_t*)vFilterData, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi); 125cabdff1aSopenharmony_ci } 126cabdff1aSopenharmony_ci } 127cabdff1aSopenharmony_ci av_freep(&src); 128cabdff1aSopenharmony_ci av_freep(&vFilterData); 129cabdff1aSopenharmony_ci } 130cabdff1aSopenharmony_ci } 131cabdff1aSopenharmony_ci } 132cabdff1aSopenharmony_ci sws_freeContext(ctx); 133cabdff1aSopenharmony_ci#undef FILTER_SIZES 134cabdff1aSopenharmony_ci} 135cabdff1aSopenharmony_ci 136cabdff1aSopenharmony_ci#undef SRC_PIXELS 137cabdff1aSopenharmony_ci#define SRC_PIXELS 512 138cabdff1aSopenharmony_ci 139cabdff1aSopenharmony_cistatic void check_hscale(void) 140cabdff1aSopenharmony_ci{ 141cabdff1aSopenharmony_ci#define MAX_FILTER_WIDTH 40 142cabdff1aSopenharmony_ci#define FILTER_SIZES 6 143cabdff1aSopenharmony_ci static const int filter_sizes[FILTER_SIZES] = { 4, 8, 12, 16, 32, 40 }; 144cabdff1aSopenharmony_ci 145cabdff1aSopenharmony_ci#define HSCALE_PAIRS 2 146cabdff1aSopenharmony_ci static const int hscale_pairs[HSCALE_PAIRS][2] = { 147cabdff1aSopenharmony_ci { 8, 14 }, 148cabdff1aSopenharmony_ci { 8, 18 }, 149cabdff1aSopenharmony_ci }; 150cabdff1aSopenharmony_ci 151cabdff1aSopenharmony_ci#define LARGEST_INPUT_SIZE 512 152cabdff1aSopenharmony_ci#define INPUT_SIZES 6 153cabdff1aSopenharmony_ci static const int input_sizes[INPUT_SIZES] = {8, 24, 128, 144, 256, 512}; 154cabdff1aSopenharmony_ci 155cabdff1aSopenharmony_ci int i, j, fsi, hpi, width, dstWi; 156cabdff1aSopenharmony_ci struct SwsContext *ctx; 157cabdff1aSopenharmony_ci 158cabdff1aSopenharmony_ci // padded 159cabdff1aSopenharmony_ci LOCAL_ALIGNED_32(uint8_t, src, [FFALIGN(SRC_PIXELS + MAX_FILTER_WIDTH - 1, 4)]); 160cabdff1aSopenharmony_ci LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]); 161cabdff1aSopenharmony_ci LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]); 162cabdff1aSopenharmony_ci 163cabdff1aSopenharmony_ci // padded 164cabdff1aSopenharmony_ci LOCAL_ALIGNED_32(int16_t, filter, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]); 165cabdff1aSopenharmony_ci LOCAL_ALIGNED_32(int32_t, filterPos, [SRC_PIXELS]); 166cabdff1aSopenharmony_ci LOCAL_ALIGNED_32(int16_t, filterAvx2, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]); 167cabdff1aSopenharmony_ci LOCAL_ALIGNED_32(int32_t, filterPosAvx, [SRC_PIXELS]); 168cabdff1aSopenharmony_ci 169cabdff1aSopenharmony_ci // The dst parameter here is either int16_t or int32_t but we use void* to 170cabdff1aSopenharmony_ci // just cover both cases. 171cabdff1aSopenharmony_ci declare_func_emms(AV_CPU_FLAG_MMX, void, void *c, void *dst, int dstW, 172cabdff1aSopenharmony_ci const uint8_t *src, const int16_t *filter, 173cabdff1aSopenharmony_ci const int32_t *filterPos, int filterSize); 174cabdff1aSopenharmony_ci 175cabdff1aSopenharmony_ci int cpu_flags = av_get_cpu_flags(); 176cabdff1aSopenharmony_ci 177cabdff1aSopenharmony_ci ctx = sws_alloc_context(); 178cabdff1aSopenharmony_ci if (sws_init_context(ctx, NULL, NULL) < 0) 179cabdff1aSopenharmony_ci fail(); 180cabdff1aSopenharmony_ci 181cabdff1aSopenharmony_ci randomize_buffers(src, SRC_PIXELS + MAX_FILTER_WIDTH - 1); 182cabdff1aSopenharmony_ci 183cabdff1aSopenharmony_ci for (hpi = 0; hpi < HSCALE_PAIRS; hpi++) { 184cabdff1aSopenharmony_ci for (fsi = 0; fsi < FILTER_SIZES; fsi++) { 185cabdff1aSopenharmony_ci for (dstWi = 0; dstWi < INPUT_SIZES; dstWi++) { 186cabdff1aSopenharmony_ci width = filter_sizes[fsi]; 187cabdff1aSopenharmony_ci 188cabdff1aSopenharmony_ci ctx->srcBpc = hscale_pairs[hpi][0]; 189cabdff1aSopenharmony_ci ctx->dstBpc = hscale_pairs[hpi][1]; 190cabdff1aSopenharmony_ci ctx->hLumFilterSize = ctx->hChrFilterSize = width; 191cabdff1aSopenharmony_ci 192cabdff1aSopenharmony_ci for (i = 0; i < SRC_PIXELS; i++) { 193cabdff1aSopenharmony_ci filterPos[i] = i; 194cabdff1aSopenharmony_ci filterPosAvx[i] = i; 195cabdff1aSopenharmony_ci 196cabdff1aSopenharmony_ci // These filter cofficients are chosen to try break two corner 197cabdff1aSopenharmony_ci // cases, namely: 198cabdff1aSopenharmony_ci // 199cabdff1aSopenharmony_ci // - Negative filter coefficients. The filters output signed 200cabdff1aSopenharmony_ci // values, and it should be possible to end up with negative 201cabdff1aSopenharmony_ci // output values. 202cabdff1aSopenharmony_ci // 203cabdff1aSopenharmony_ci // - Positive clipping. The hscale filter function has clipping 204cabdff1aSopenharmony_ci // at (1<<15) - 1 205cabdff1aSopenharmony_ci // 206cabdff1aSopenharmony_ci // The coefficients sum to the 1.0 point for the hscale 207cabdff1aSopenharmony_ci // functions (1 << 14). 208cabdff1aSopenharmony_ci 209cabdff1aSopenharmony_ci for (j = 0; j < width; j++) { 210cabdff1aSopenharmony_ci filter[i * width + j] = -((1 << 14) / (width - 1)); 211cabdff1aSopenharmony_ci } 212cabdff1aSopenharmony_ci filter[i * width + (rnd() % width)] = ((1 << 15) - 1); 213cabdff1aSopenharmony_ci } 214cabdff1aSopenharmony_ci 215cabdff1aSopenharmony_ci for (i = 0; i < MAX_FILTER_WIDTH; i++) { 216cabdff1aSopenharmony_ci // These values should be unused in SIMD implementations but 217cabdff1aSopenharmony_ci // may still be read, random coefficients here should help show 218cabdff1aSopenharmony_ci // issues where they are used in error. 219cabdff1aSopenharmony_ci 220cabdff1aSopenharmony_ci filter[SRC_PIXELS * width + i] = rnd(); 221cabdff1aSopenharmony_ci } 222cabdff1aSopenharmony_ci ctx->dstW = ctx->chrDstW = input_sizes[dstWi]; 223cabdff1aSopenharmony_ci ff_sws_init_scale(ctx); 224cabdff1aSopenharmony_ci memcpy(filterAvx2, filter, sizeof(uint16_t) * (SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH)); 225cabdff1aSopenharmony_ci if ((cpu_flags & AV_CPU_FLAG_AVX2) && !(cpu_flags & AV_CPU_FLAG_SLOW_GATHER)) 226cabdff1aSopenharmony_ci ff_shuffle_filter_coefficients(ctx, filterPosAvx, width, filterAvx2, SRC_PIXELS); 227cabdff1aSopenharmony_ci 228cabdff1aSopenharmony_ci if (check_func(ctx->hcScale, "hscale_%d_to_%d__fs_%d_dstW_%d", ctx->srcBpc, ctx->dstBpc + 1, width, ctx->dstW)) { 229cabdff1aSopenharmony_ci memset(dst0, 0, SRC_PIXELS * sizeof(dst0[0])); 230cabdff1aSopenharmony_ci memset(dst1, 0, SRC_PIXELS * sizeof(dst1[0])); 231cabdff1aSopenharmony_ci 232cabdff1aSopenharmony_ci call_ref(NULL, dst0, ctx->dstW, src, filter, filterPos, width); 233cabdff1aSopenharmony_ci call_new(NULL, dst1, ctx->dstW, src, filterAvx2, filterPosAvx, width); 234cabdff1aSopenharmony_ci if (memcmp(dst0, dst1, ctx->dstW * sizeof(dst0[0]))) 235cabdff1aSopenharmony_ci fail(); 236cabdff1aSopenharmony_ci bench_new(NULL, dst0, ctx->dstW, src, filter, filterPosAvx, width); 237cabdff1aSopenharmony_ci } 238cabdff1aSopenharmony_ci } 239cabdff1aSopenharmony_ci } 240cabdff1aSopenharmony_ci } 241cabdff1aSopenharmony_ci sws_freeContext(ctx); 242cabdff1aSopenharmony_ci} 243cabdff1aSopenharmony_ci 244cabdff1aSopenharmony_civoid checkasm_check_sw_scale(void) 245cabdff1aSopenharmony_ci{ 246cabdff1aSopenharmony_ci check_hscale(); 247cabdff1aSopenharmony_ci report("hscale"); 248cabdff1aSopenharmony_ci check_yuv2yuvX(); 249cabdff1aSopenharmony_ci report("yuv2yuvX"); 250cabdff1aSopenharmony_ci} 251