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.

239 lines
8.9KB

  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 4
  65. static const int input_sizes[INPUT_SIZES] = {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. randomize_buffers((uint8_t*)src_pixels, LARGEST_FILTER * LARGEST_INPUT_SIZE * sizeof(int16_t));
  81. randomize_buffers((uint8_t*)filter_coeff, LARGEST_FILTER * sizeof(int16_t));
  82. ctx = sws_alloc_context();
  83. if (sws_init_context(ctx, NULL, NULL) < 0)
  84. fail();
  85. ff_getSwsFunc(ctx);
  86. for(i = 0; i < LARGEST_INPUT_SIZE; ++i){
  87. dither[i] = d_val;
  88. }
  89. for(isi = 0; isi < INPUT_SIZES; ++isi){
  90. dstW = input_sizes[isi];
  91. for(osi = 0; osi < 64; osi += 16){
  92. for(fsi = 0; fsi < FILTER_SIZES; ++fsi){
  93. src = av_malloc(sizeof(int16_t*) * filter_sizes[fsi]);
  94. vFilterData = av_malloc((filter_sizes[fsi] + 2) * sizeof(union VFilterData));
  95. memset(vFilterData, 0, (filter_sizes[fsi] + 2) * sizeof(union VFilterData));
  96. for(i = 0; i < filter_sizes[fsi]; ++i){
  97. src[i] = &src_pixels[i * LARGEST_INPUT_SIZE];
  98. vFilterData[i].src = src[i];
  99. for(j = 0; j < 4; ++j)
  100. vFilterData[i].coeff[j + 4] = filter_coeff[i];
  101. }
  102. if (check_func(ctx->yuv2planeX, "yuv2yuvX_%d_%d", filter_sizes[fsi], osi)){
  103. memset(dst0, 0, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  104. memset(dst1, 0, LARGEST_INPUT_SIZE * sizeof(dst1[0]));
  105. // The reference function is not the scalar function selected when mmx
  106. // is deactivated as the SIMD functions do not give the same result as
  107. // the scalar ones due to rounding. The SIMD functions are activated by
  108. // the flag SWS_ACCURATE_RND
  109. ref_function(&filter_coeff[0], filter_sizes[fsi], src, dst0, dstW - osi, dither, osi);
  110. // There's no point in calling new for the reference function
  111. if(ctx->use_mmx_vfilter){
  112. call_new((const int16_t*)vFilterData, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi);
  113. if (memcmp(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0])))
  114. fail();
  115. if(dstW == LARGEST_INPUT_SIZE)
  116. bench_new((const int16_t*)vFilterData, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi);
  117. }
  118. }
  119. av_freep(&src);
  120. av_freep(&vFilterData);
  121. }
  122. }
  123. }
  124. sws_freeContext(ctx);
  125. #undef FILTER_SIZES
  126. }
  127. #undef SRC_PIXELS
  128. #define SRC_PIXELS 128
  129. static void check_hscale(void)
  130. {
  131. #define MAX_FILTER_WIDTH 40
  132. #define FILTER_SIZES 5
  133. static const int filter_sizes[FILTER_SIZES] = { 4, 8, 16, 32, 40 };
  134. #define HSCALE_PAIRS 2
  135. static const int hscale_pairs[HSCALE_PAIRS][2] = {
  136. { 8, 14 },
  137. { 8, 18 },
  138. };
  139. int i, j, fsi, hpi, width;
  140. struct SwsContext *ctx;
  141. // padded
  142. LOCAL_ALIGNED_32(uint8_t, src, [FFALIGN(SRC_PIXELS + MAX_FILTER_WIDTH - 1, 4)]);
  143. LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]);
  144. LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]);
  145. // padded
  146. LOCAL_ALIGNED_32(int16_t, filter, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]);
  147. LOCAL_ALIGNED_32(int32_t, filterPos, [SRC_PIXELS]);
  148. // The dst parameter here is either int16_t or int32_t but we use void* to
  149. // just cover both cases.
  150. declare_func_emms(AV_CPU_FLAG_MMX, void, void *c, void *dst, int dstW,
  151. const uint8_t *src, const int16_t *filter,
  152. const int32_t *filterPos, int filterSize);
  153. ctx = sws_alloc_context();
  154. if (sws_init_context(ctx, NULL, NULL) < 0)
  155. fail();
  156. randomize_buffers(src, SRC_PIXELS + MAX_FILTER_WIDTH - 1);
  157. for (hpi = 0; hpi < HSCALE_PAIRS; hpi++) {
  158. for (fsi = 0; fsi < FILTER_SIZES; fsi++) {
  159. width = filter_sizes[fsi];
  160. ctx->srcBpc = hscale_pairs[hpi][0];
  161. ctx->dstBpc = hscale_pairs[hpi][1];
  162. ctx->hLumFilterSize = ctx->hChrFilterSize = width;
  163. for (i = 0; i < SRC_PIXELS; i++) {
  164. filterPos[i] = i;
  165. // These filter cofficients are chosen to try break two corner
  166. // cases, namely:
  167. //
  168. // - Negative filter coefficients. The filters output signed
  169. // values, and it should be possible to end up with negative
  170. // output values.
  171. //
  172. // - Positive clipping. The hscale filter function has clipping
  173. // at (1<<15) - 1
  174. //
  175. // The coefficients sum to the 1.0 point for the hscale
  176. // functions (1 << 14).
  177. for (j = 0; j < width; j++) {
  178. filter[i * width + j] = -((1 << 14) / (width - 1));
  179. }
  180. filter[i * width + (rnd() % width)] = ((1 << 15) - 1);
  181. }
  182. for (i = 0; i < MAX_FILTER_WIDTH; i++) {
  183. // These values should be unused in SIMD implementations but
  184. // may still be read, random coefficients here should help show
  185. // issues where they are used in error.
  186. filter[SRC_PIXELS * width + i] = rnd();
  187. }
  188. ff_getSwsFunc(ctx);
  189. if (check_func(ctx->hcScale, "hscale_%d_to_%d_width%d", ctx->srcBpc, ctx->dstBpc + 1, width)) {
  190. memset(dst0, 0, SRC_PIXELS * sizeof(dst0[0]));
  191. memset(dst1, 0, SRC_PIXELS * sizeof(dst1[0]));
  192. call_ref(NULL, dst0, SRC_PIXELS, src, filter, filterPos, width);
  193. call_new(NULL, dst1, SRC_PIXELS, src, filter, filterPos, width);
  194. if (memcmp(dst0, dst1, SRC_PIXELS * sizeof(dst0[0])))
  195. fail();
  196. bench_new(NULL, dst0, SRC_PIXELS, src, filter, filterPos, width);
  197. }
  198. }
  199. }
  200. sws_freeContext(ctx);
  201. }
  202. void checkasm_check_sw_scale(void)
  203. {
  204. check_hscale();
  205. report("hscale");
  206. check_yuv2yuvX();
  207. report("yuv2yuvX");
  208. }