You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

237 lines
8.8KB

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