sw_scale.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. static void yuv2planeX_8_ref(const int16_t *filter, int filterSize,
  34. const int16_t **src, uint8_t *dest, int dstW,
  35. const uint8_t *dither, int offset)
  36. {
  37. // This corresponds to the yuv2planeX_8_c function
  38. int i;
  39. for (i = 0; i < dstW; i++) {
  40. int val = dither[(i + offset) & 7] << 12;
  41. int j;
  42. for (j = 0; j < filterSize; j++)
  43. val += src[j][i] * filter[j];
  44. dest[i]= av_clip_uint8(val >> 19);
  45. }
  46. }
  47. static int cmp_off_by_n(const uint8_t *ref, const uint8_t *test, size_t n, int accuracy)
  48. {
  49. for (size_t i = 0; i < n; i++) {
  50. if (abs(ref[i] - test[i]) > accuracy)
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. static void print_data(uint8_t *p, size_t len, size_t offset)
  56. {
  57. size_t i = 0;
  58. for (; i < len; i++) {
  59. if (i % 8 == 0) {
  60. printf("0x%04zx: ", i+offset);
  61. }
  62. printf("0x%02x ", (uint32_t) p[i]);
  63. if (i % 8 == 7) {
  64. printf("\n");
  65. }
  66. }
  67. if (i % 8 != 0) {
  68. printf("\n");
  69. }
  70. }
  71. static size_t show_differences(uint8_t *a, uint8_t *b, size_t len)
  72. {
  73. for (size_t i = 0; i < len; i++) {
  74. if (a[i] != b[i]) {
  75. size_t offset_of_mismatch = i;
  76. size_t offset;
  77. if (i >= 8) i-=8;
  78. offset = i & (~7);
  79. printf("test a:\n");
  80. print_data(&a[offset], 32, offset);
  81. printf("\ntest b:\n");
  82. print_data(&b[offset], 32, offset);
  83. printf("\n");
  84. return offset_of_mismatch;
  85. }
  86. }
  87. return len;
  88. }
  89. static void check_yuv2yuv1(int accurate)
  90. {
  91. struct SwsContext *ctx;
  92. int osi, isi;
  93. int dstW, offset;
  94. size_t fail_offset;
  95. const int input_sizes[] = {8, 24, 128, 144, 256, 512};
  96. const int INPUT_SIZES = sizeof(input_sizes)/sizeof(input_sizes[0]);
  97. #define LARGEST_INPUT_SIZE 512
  98. const int offsets[] = {0, 3, 8, 11, 16, 19};
  99. const int OFFSET_SIZES = sizeof(offsets)/sizeof(offsets[0]);
  100. const char *accurate_str = (accurate) ? "accurate" : "approximate";
  101. declare_func(void,
  102. const int16_t *src, uint8_t *dest,
  103. int dstW, const uint8_t *dither, int offset);
  104. LOCAL_ALIGNED_16(int16_t, src_pixels, [LARGEST_INPUT_SIZE]);
  105. LOCAL_ALIGNED_16(uint8_t, dst0, [LARGEST_INPUT_SIZE]);
  106. LOCAL_ALIGNED_16(uint8_t, dst1, [LARGEST_INPUT_SIZE]);
  107. LOCAL_ALIGNED_8(uint8_t, dither, [8]);
  108. randomize_buffers((uint8_t*)dither, 8);
  109. randomize_buffers((uint8_t*)src_pixels, LARGEST_INPUT_SIZE * sizeof(int16_t));
  110. ctx = sws_alloc_context();
  111. if (accurate)
  112. ctx->flags |= SWS_ACCURATE_RND;
  113. if (sws_init_context(ctx, NULL, NULL) < 0)
  114. fail();
  115. ff_sws_init_scale(ctx);
  116. for (isi = 0; isi < INPUT_SIZES; ++isi) {
  117. dstW = input_sizes[isi];
  118. for (osi = 0; osi < OFFSET_SIZES; osi++) {
  119. offset = offsets[osi];
  120. if (check_func(ctx->yuv2plane1, "yuv2yuv1_%d_%d_%s", offset, dstW, accurate_str)){
  121. memset(dst0, 0, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  122. memset(dst1, 0, LARGEST_INPUT_SIZE * sizeof(dst1[0]));
  123. call_ref(src_pixels, dst0, dstW, dither, offset);
  124. call_new(src_pixels, dst1, dstW, dither, offset);
  125. if (cmp_off_by_n(dst0, dst1, dstW * sizeof(dst0[0]), accurate ? 0 : 2)) {
  126. fail();
  127. printf("failed: yuv2yuv1_%d_%di_%s\n", offset, dstW, accurate_str);
  128. fail_offset = show_differences(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  129. printf("failing values: src: 0x%04x dither: 0x%02x dst-c: %02x dst-asm: %02x\n",
  130. (int) src_pixels[fail_offset],
  131. (int) dither[(fail_offset + fail_offset) & 7],
  132. (int) dst0[fail_offset],
  133. (int) dst1[fail_offset]);
  134. }
  135. if(dstW == LARGEST_INPUT_SIZE)
  136. bench_new(src_pixels, dst1, dstW, dither, offset);
  137. }
  138. }
  139. }
  140. sws_freeContext(ctx);
  141. }
  142. static void check_yuv2yuvX(int accurate)
  143. {
  144. struct SwsContext *ctx;
  145. int fsi, osi, isi, i, j;
  146. int dstW;
  147. #define LARGEST_FILTER 16
  148. // ff_yuv2planeX_8_sse2 can't handle odd filter sizes
  149. const int filter_sizes[] = {2, 4, 8, 16};
  150. const int FILTER_SIZES = sizeof(filter_sizes)/sizeof(filter_sizes[0]);
  151. #define LARGEST_INPUT_SIZE 512
  152. static const int input_sizes[] = {8, 24, 128, 144, 256, 512};
  153. const int INPUT_SIZES = sizeof(input_sizes)/sizeof(input_sizes[0]);
  154. const char *accurate_str = (accurate) ? "accurate" : "approximate";
  155. declare_func_emms(AV_CPU_FLAG_MMX, void, const int16_t *filter,
  156. int filterSize, const int16_t **src, uint8_t *dest,
  157. int dstW, const uint8_t *dither, int offset);
  158. const int16_t **src;
  159. LOCAL_ALIGNED_16(int16_t, src_pixels, [LARGEST_FILTER * LARGEST_INPUT_SIZE]);
  160. LOCAL_ALIGNED_16(int16_t, filter_coeff, [LARGEST_FILTER]);
  161. LOCAL_ALIGNED_16(uint8_t, dst0, [LARGEST_INPUT_SIZE]);
  162. LOCAL_ALIGNED_16(uint8_t, dst1, [LARGEST_INPUT_SIZE]);
  163. LOCAL_ALIGNED_16(uint8_t, dither, [LARGEST_INPUT_SIZE]);
  164. union VFilterData{
  165. const int16_t *src;
  166. uint16_t coeff[8];
  167. } *vFilterData;
  168. uint8_t d_val = rnd();
  169. memset(dither, d_val, LARGEST_INPUT_SIZE);
  170. randomize_buffers((uint8_t*)src_pixels, LARGEST_FILTER * LARGEST_INPUT_SIZE * sizeof(int16_t));
  171. ctx = sws_alloc_context();
  172. if (accurate)
  173. ctx->flags |= SWS_ACCURATE_RND;
  174. if (sws_init_context(ctx, NULL, NULL) < 0)
  175. fail();
  176. ff_sws_init_scale(ctx);
  177. for(isi = 0; isi < INPUT_SIZES; ++isi){
  178. dstW = input_sizes[isi];
  179. for(osi = 0; osi < 64; osi += 16){
  180. if (dstW <= osi)
  181. continue;
  182. for (fsi = 0; fsi < FILTER_SIZES; ++fsi) {
  183. // Generate filter coefficients for the given filter size,
  184. // with some properties:
  185. // - The coefficients add up to the intended sum (4096, 1<<12)
  186. // - The coefficients contain negative values
  187. // - The filter intermediates don't overflow for worst case
  188. // inputs (all positive coefficients are coupled with
  189. // input_max and all negative coefficients with input_min,
  190. // or vice versa).
  191. // Produce a filter with all coefficients set to
  192. // -((1<<12)/(filter_size-1)) except for one (randomly chosen)
  193. // which is set to ((1<<13)-1).
  194. for (i = 0; i < filter_sizes[fsi]; ++i)
  195. filter_coeff[i] = -((1 << 12) / (filter_sizes[fsi] - 1));
  196. filter_coeff[rnd() % filter_sizes[fsi]] = (1 << 13) - 1;
  197. src = av_malloc(sizeof(int16_t*) * filter_sizes[fsi]);
  198. vFilterData = av_malloc((filter_sizes[fsi] + 2) * sizeof(union VFilterData));
  199. memset(vFilterData, 0, (filter_sizes[fsi] + 2) * sizeof(union VFilterData));
  200. for (i = 0; i < filter_sizes[fsi]; ++i) {
  201. src[i] = &src_pixels[i * LARGEST_INPUT_SIZE];
  202. vFilterData[i].src = src[i] - osi;
  203. for(j = 0; j < 4; ++j)
  204. vFilterData[i].coeff[j + 4] = filter_coeff[i];
  205. }
  206. if (check_func(ctx->yuv2planeX, "yuv2yuvX_%d_%d_%d_%s", filter_sizes[fsi], osi, dstW, accurate_str)){
  207. // use vFilterData for the mmx function
  208. const int16_t *filter = ctx->use_mmx_vfilter ? (const int16_t*)vFilterData : &filter_coeff[0];
  209. memset(dst0, 0, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  210. memset(dst1, 0, LARGEST_INPUT_SIZE * sizeof(dst1[0]));
  211. // We can't use call_ref here, because we don't know if use_mmx_vfilter was set for that
  212. // function or not, so we can't pass it the parameters correctly.
  213. yuv2planeX_8_ref(&filter_coeff[0], filter_sizes[fsi], src, dst0, dstW - osi, dither, osi);
  214. call_new(filter, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi);
  215. if (cmp_off_by_n(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0]), accurate ? 0 : 2)) {
  216. fail();
  217. printf("failed: yuv2yuvX_%d_%d_%d_%s\n", filter_sizes[fsi], osi, dstW, accurate_str);
  218. show_differences(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  219. }
  220. if(dstW == LARGEST_INPUT_SIZE)
  221. bench_new((const int16_t*)vFilterData, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi);
  222. }
  223. av_freep(&src);
  224. av_freep(&vFilterData);
  225. }
  226. }
  227. }
  228. sws_freeContext(ctx);
  229. #undef FILTER_SIZES
  230. }
  231. #undef SRC_PIXELS
  232. #define SRC_PIXELS 512
  233. static void check_hscale(void)
  234. {
  235. #define MAX_FILTER_WIDTH 40
  236. #define FILTER_SIZES 6
  237. static const int filter_sizes[FILTER_SIZES] = { 4, 8, 12, 16, 32, 40 };
  238. #define HSCALE_PAIRS 2
  239. static const int hscale_pairs[HSCALE_PAIRS][2] = {
  240. { 8, 14 },
  241. { 8, 18 },
  242. };
  243. #define LARGEST_INPUT_SIZE 512
  244. #define INPUT_SIZES 6
  245. static const int input_sizes[INPUT_SIZES] = {8, 24, 128, 144, 256, 512};
  246. int i, j, fsi, hpi, width, dstWi;
  247. struct SwsContext *ctx;
  248. // padded
  249. LOCAL_ALIGNED_32(uint8_t, src, [FFALIGN(SRC_PIXELS + MAX_FILTER_WIDTH - 1, 4)]);
  250. LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]);
  251. LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]);
  252. // padded
  253. LOCAL_ALIGNED_32(int16_t, filter, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]);
  254. LOCAL_ALIGNED_32(int32_t, filterPos, [SRC_PIXELS]);
  255. LOCAL_ALIGNED_32(int16_t, filterAvx2, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]);
  256. LOCAL_ALIGNED_32(int32_t, filterPosAvx, [SRC_PIXELS]);
  257. // The dst parameter here is either int16_t or int32_t but we use void* to
  258. // just cover both cases.
  259. declare_func(void, void *c, void *dst, int dstW,
  260. const uint8_t *src, const int16_t *filter,
  261. const int32_t *filterPos, int filterSize);
  262. ctx = sws_alloc_context();
  263. if (sws_init_context(ctx, NULL, NULL) < 0)
  264. fail();
  265. randomize_buffers(src, SRC_PIXELS + MAX_FILTER_WIDTH - 1);
  266. for (hpi = 0; hpi < HSCALE_PAIRS; hpi++) {
  267. for (fsi = 0; fsi < FILTER_SIZES; fsi++) {
  268. for (dstWi = 0; dstWi < INPUT_SIZES; dstWi++) {
  269. width = filter_sizes[fsi];
  270. ctx->srcBpc = hscale_pairs[hpi][0];
  271. ctx->dstBpc = hscale_pairs[hpi][1];
  272. ctx->hLumFilterSize = ctx->hChrFilterSize = width;
  273. for (i = 0; i < SRC_PIXELS; i++) {
  274. filterPos[i] = i;
  275. filterPosAvx[i] = i;
  276. // These filter cofficients are chosen to try break two corner
  277. // cases, namely:
  278. //
  279. // - Negative filter coefficients. The filters output signed
  280. // values, and it should be possible to end up with negative
  281. // output values.
  282. //
  283. // - Positive clipping. The hscale filter function has clipping
  284. // at (1<<15) - 1
  285. //
  286. // The coefficients sum to the 1.0 point for the hscale
  287. // functions (1 << 14).
  288. for (j = 0; j < width; j++) {
  289. filter[i * width + j] = -((1 << 14) / (width - 1));
  290. }
  291. filter[i * width + (rnd() % width)] = ((1 << 15) - 1);
  292. }
  293. for (i = 0; i < MAX_FILTER_WIDTH; i++) {
  294. // These values should be unused in SIMD implementations but
  295. // may still be read, random coefficients here should help show
  296. // issues where they are used in error.
  297. filter[SRC_PIXELS * width + i] = rnd();
  298. }
  299. ctx->dstW = ctx->chrDstW = input_sizes[dstWi];
  300. ff_sws_init_scale(ctx);
  301. memcpy(filterAvx2, filter, sizeof(uint16_t) * (SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH));
  302. ff_shuffle_filter_coefficients(ctx, filterPosAvx, width, filterAvx2, ctx->dstW);
  303. if (check_func(ctx->hcScale, "hscale_%d_to_%d__fs_%d_dstW_%d", ctx->srcBpc, ctx->dstBpc + 1, width, ctx->dstW)) {
  304. memset(dst0, 0, SRC_PIXELS * sizeof(dst0[0]));
  305. memset(dst1, 0, SRC_PIXELS * sizeof(dst1[0]));
  306. call_ref(NULL, dst0, ctx->dstW, src, filter, filterPos, width);
  307. call_new(NULL, dst1, ctx->dstW, src, filterAvx2, filterPosAvx, width);
  308. if (memcmp(dst0, dst1, ctx->dstW * sizeof(dst0[0])))
  309. fail();
  310. bench_new(NULL, dst0, ctx->dstW, src, filter, filterPosAvx, width);
  311. }
  312. }
  313. }
  314. }
  315. sws_freeContext(ctx);
  316. }
  317. void checkasm_check_sw_scale(void)
  318. {
  319. check_hscale();
  320. report("hscale");
  321. check_yuv2yuv1(0);
  322. check_yuv2yuv1(1);
  323. report("yuv2yuv1");
  324. check_yuv2yuvX(0);
  325. check_yuv2yuvX(1);
  326. report("yuv2yuvX");
  327. }