sw_scale.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. SwsContext *sws;
  92. SwsInternal *c;
  93. int osi, isi;
  94. int dstW, offset;
  95. size_t fail_offset;
  96. const int input_sizes[] = {8, 24, 128, 144, 256, 512};
  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. sws = sws_alloc_context();
  111. if (accurate)
  112. sws->flags |= SWS_ACCURATE_RND;
  113. if (sws_init_context(sws, NULL, NULL) < 0)
  114. fail();
  115. c = sws_internal(sws);
  116. ff_sws_init_scale(c);
  117. for (isi = 0; isi < FF_ARRAY_ELEMS(input_sizes); ++isi) {
  118. dstW = input_sizes[isi];
  119. for (osi = 0; osi < OFFSET_SIZES; osi++) {
  120. offset = offsets[osi];
  121. if (check_func(c->yuv2plane1, "yuv2yuv1_%d_%d_%s", offset, dstW, accurate_str)){
  122. memset(dst0, 0, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  123. memset(dst1, 0, LARGEST_INPUT_SIZE * sizeof(dst1[0]));
  124. call_ref(src_pixels, dst0, dstW, dither, offset);
  125. call_new(src_pixels, dst1, dstW, dither, offset);
  126. if (cmp_off_by_n(dst0, dst1, dstW * sizeof(dst0[0]), accurate ? 0 : 2)) {
  127. fail();
  128. printf("failed: yuv2yuv1_%d_%di_%s\n", offset, dstW, accurate_str);
  129. fail_offset = show_differences(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  130. printf("failing values: src: 0x%04x dither: 0x%02x dst-c: %02x dst-asm: %02x\n",
  131. (int) src_pixels[fail_offset],
  132. (int) dither[(fail_offset + fail_offset) & 7],
  133. (int) dst0[fail_offset],
  134. (int) dst1[fail_offset]);
  135. }
  136. if(dstW == LARGEST_INPUT_SIZE)
  137. bench_new(src_pixels, dst1, dstW, dither, offset);
  138. }
  139. }
  140. }
  141. sws_freeContext(sws);
  142. }
  143. static void check_yuv2yuvX(int accurate)
  144. {
  145. SwsContext *sws;
  146. SwsInternal *c;
  147. int fsi, osi, isi, i, j;
  148. int dstW;
  149. #define LARGEST_FILTER 16
  150. // ff_yuv2planeX_8_sse2 can't handle odd filter sizes
  151. const int filter_sizes[] = {2, 4, 8, 16};
  152. const int FILTER_SIZES = sizeof(filter_sizes)/sizeof(filter_sizes[0]);
  153. #define LARGEST_INPUT_SIZE 512
  154. static const int input_sizes[] = {8, 24, 128, 144, 256, 512};
  155. const char *accurate_str = (accurate) ? "accurate" : "approximate";
  156. declare_func_emms(AV_CPU_FLAG_MMX, void, const int16_t *filter,
  157. int filterSize, const int16_t **src, uint8_t *dest,
  158. int dstW, const uint8_t *dither, int offset);
  159. const int16_t **src;
  160. LOCAL_ALIGNED_16(int16_t, src_pixels, [LARGEST_FILTER * LARGEST_INPUT_SIZE]);
  161. LOCAL_ALIGNED_16(int16_t, filter_coeff, [LARGEST_FILTER]);
  162. LOCAL_ALIGNED_16(uint8_t, dst0, [LARGEST_INPUT_SIZE]);
  163. LOCAL_ALIGNED_16(uint8_t, dst1, [LARGEST_INPUT_SIZE]);
  164. LOCAL_ALIGNED_16(uint8_t, dither, [LARGEST_INPUT_SIZE]);
  165. union VFilterData{
  166. const int16_t *src;
  167. uint16_t coeff[8];
  168. } *vFilterData;
  169. uint8_t d_val = rnd();
  170. memset(dither, d_val, LARGEST_INPUT_SIZE);
  171. randomize_buffers((uint8_t*)src_pixels, LARGEST_FILTER * LARGEST_INPUT_SIZE * sizeof(int16_t));
  172. sws = sws_alloc_context();
  173. if (accurate)
  174. sws->flags |= SWS_ACCURATE_RND;
  175. if (sws_init_context(sws, NULL, NULL) < 0)
  176. fail();
  177. c = sws_internal(sws);
  178. ff_sws_init_scale(c);
  179. for(isi = 0; isi < FF_ARRAY_ELEMS(input_sizes); ++isi){
  180. dstW = input_sizes[isi];
  181. for(osi = 0; osi < 64; osi += 16){
  182. if (dstW <= osi)
  183. continue;
  184. for (fsi = 0; fsi < FILTER_SIZES; ++fsi) {
  185. // Generate filter coefficients for the given filter size,
  186. // with some properties:
  187. // - The coefficients add up to the intended sum (4096, 1<<12)
  188. // - The coefficients contain negative values
  189. // - The filter intermediates don't overflow for worst case
  190. // inputs (all positive coefficients are coupled with
  191. // input_max and all negative coefficients with input_min,
  192. // or vice versa).
  193. // Produce a filter with all coefficients set to
  194. // -((1<<12)/(filter_size-1)) except for one (randomly chosen)
  195. // which is set to ((1<<13)-1).
  196. for (i = 0; i < filter_sizes[fsi]; ++i)
  197. filter_coeff[i] = -((1 << 12) / (filter_sizes[fsi] - 1));
  198. filter_coeff[rnd() % filter_sizes[fsi]] = (1 << 13) - 1;
  199. src = av_malloc(sizeof(int16_t*) * filter_sizes[fsi]);
  200. vFilterData = av_malloc((filter_sizes[fsi] + 2) * sizeof(union VFilterData));
  201. memset(vFilterData, 0, (filter_sizes[fsi] + 2) * sizeof(union VFilterData));
  202. for (i = 0; i < filter_sizes[fsi]; ++i) {
  203. src[i] = &src_pixels[i * LARGEST_INPUT_SIZE];
  204. vFilterData[i].src = src[i] - osi;
  205. for(j = 0; j < 4; ++j)
  206. vFilterData[i].coeff[j + 4] = filter_coeff[i];
  207. }
  208. if (check_func(c->yuv2planeX, "yuv2yuvX_%d_%d_%d_%s", filter_sizes[fsi], osi, dstW, accurate_str)){
  209. // use vFilterData for the mmx function
  210. const int16_t *filter = c->use_mmx_vfilter ? (const int16_t*)vFilterData : &filter_coeff[0];
  211. memset(dst0, 0, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  212. memset(dst1, 0, LARGEST_INPUT_SIZE * sizeof(dst1[0]));
  213. // We can't use call_ref here, because we don't know if use_mmx_vfilter was set for that
  214. // function or not, so we can't pass it the parameters correctly.
  215. yuv2planeX_8_ref(&filter_coeff[0], filter_sizes[fsi], src, dst0, dstW - osi, dither, osi);
  216. call_new(filter, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi);
  217. if (cmp_off_by_n(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0]), accurate ? 0 : 2)) {
  218. fail();
  219. printf("failed: yuv2yuvX_%d_%d_%d_%s\n", filter_sizes[fsi], osi, dstW, accurate_str);
  220. show_differences(dst0, dst1, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  221. }
  222. if(dstW == LARGEST_INPUT_SIZE)
  223. bench_new((const int16_t*)vFilterData, filter_sizes[fsi], src, dst1, dstW - osi, dither, osi);
  224. }
  225. av_freep(&src);
  226. av_freep(&vFilterData);
  227. }
  228. }
  229. }
  230. sws_freeContext(sws);
  231. #undef FILTER_SIZES
  232. }
  233. static void check_yuv2nv12cX(int accurate)
  234. {
  235. SwsContext *sws;
  236. SwsInternal *c;
  237. #define LARGEST_FILTER 16
  238. const int filter_sizes[] = {2, 4, 8, 16};
  239. #define LARGEST_INPUT_SIZE 512
  240. static const int input_sizes[] = {8, 24, 128, 144, 256, 512};
  241. const char *accurate_str = (accurate) ? "accurate" : "approximate";
  242. declare_func_emms(AV_CPU_FLAG_MMX, void, enum AVPixelFormat dstFormat,
  243. const uint8_t *chrDither, const int16_t *chrFilter,
  244. int chrFilterSize, const int16_t **chrUSrc,
  245. const int16_t **chrVSrc, uint8_t *dest, int dstW);
  246. const int16_t *srcU[LARGEST_FILTER], *srcV[LARGEST_FILTER];
  247. LOCAL_ALIGNED_16(int16_t, srcU_pixels, [LARGEST_FILTER * LARGEST_INPUT_SIZE]);
  248. LOCAL_ALIGNED_16(int16_t, srcV_pixels, [LARGEST_FILTER * LARGEST_INPUT_SIZE]);
  249. LOCAL_ALIGNED_16(int16_t, filter_coeff, [LARGEST_FILTER]);
  250. LOCAL_ALIGNED_16(uint8_t, dst0, [LARGEST_INPUT_SIZE * 2]);
  251. LOCAL_ALIGNED_16(uint8_t, dst1, [LARGEST_INPUT_SIZE * 2]);
  252. LOCAL_ALIGNED_16(uint8_t, dither, [LARGEST_INPUT_SIZE]);
  253. uint8_t d_val = rnd();
  254. memset(dither, d_val, LARGEST_INPUT_SIZE);
  255. randomize_buffers((uint8_t*)srcU_pixels, LARGEST_FILTER * LARGEST_INPUT_SIZE * sizeof(int16_t));
  256. randomize_buffers((uint8_t*)srcV_pixels, LARGEST_FILTER * LARGEST_INPUT_SIZE * sizeof(int16_t));
  257. for (int i = 0; i < LARGEST_FILTER; i++) {
  258. srcU[i] = &srcU_pixels[i * LARGEST_INPUT_SIZE];
  259. srcV[i] = &srcV_pixels[i * LARGEST_INPUT_SIZE];
  260. }
  261. sws = sws_alloc_context();
  262. sws->dst_format = AV_PIX_FMT_NV12;
  263. if (accurate)
  264. sws->flags |= SWS_ACCURATE_RND;
  265. if (sws_init_context(sws, NULL, NULL) < 0)
  266. fail();
  267. c = sws_internal(sws);
  268. ff_sws_init_scale(c);
  269. for (int isi = 0; isi < FF_ARRAY_ELEMS(input_sizes); isi++){
  270. const int dstW = input_sizes[isi];
  271. for (int fsi = 0; fsi < FF_ARRAY_ELEMS(filter_sizes); fsi++) {
  272. const int filter_size = filter_sizes[fsi];
  273. for (int i = 0; i < filter_size; i++)
  274. filter_coeff[i] = -((1 << 12) / (filter_size - 1));
  275. filter_coeff[rnd() % filter_size] = (1 << 13) - 1;
  276. if (check_func(c->yuv2nv12cX, "yuv2nv12cX_%d_%d_%s", filter_size, dstW, accurate_str)){
  277. memset(dst0, 0, LARGEST_INPUT_SIZE * sizeof(dst0[0]));
  278. memset(dst1, 0, LARGEST_INPUT_SIZE * sizeof(dst1[0]));
  279. call_ref(sws->dst_format, dither, &filter_coeff[0], filter_size, srcU, srcV, dst0, dstW);
  280. call_new(sws->dst_format, dither, &filter_coeff[0], filter_size, srcU, srcV, dst1, dstW);
  281. if (cmp_off_by_n(dst0, dst1, dstW * 2 * sizeof(dst0[0]), accurate ? 0 : 2)) {
  282. fail();
  283. printf("failed: yuv2nv12wX_%d_%d_%s\n", filter_size, dstW, accurate_str);
  284. show_differences(dst0, dst1, dstW * 2 * sizeof(dst0[0]));
  285. }
  286. if (dstW == LARGEST_INPUT_SIZE)
  287. bench_new(sws->dst_format, dither, &filter_coeff[0], filter_size, srcU, srcV, dst1, dstW);
  288. }
  289. }
  290. }
  291. sws_freeContext(sws);
  292. }
  293. #undef LARGEST_FILTER
  294. #undef LARGEST_INPUT_SIZE
  295. #undef SRC_PIXELS
  296. #define SRC_PIXELS 512
  297. static void check_hscale(void)
  298. {
  299. #define MAX_FILTER_WIDTH 40
  300. #define FILTER_SIZES 6
  301. static const int filter_sizes[FILTER_SIZES] = { 4, 8, 12, 16, 32, 40 };
  302. #define HSCALE_PAIRS 2
  303. static const int hscale_pairs[HSCALE_PAIRS][2] = {
  304. { 8, 14 },
  305. { 8, 18 },
  306. };
  307. #define LARGEST_INPUT_SIZE 512
  308. static const int input_sizes[] = {8, 24, 128, 144, 256, 512};
  309. int i, j, fsi, hpi, width, dstWi;
  310. SwsContext *sws;
  311. SwsInternal *c;
  312. // padded
  313. LOCAL_ALIGNED_32(uint8_t, src, [FFALIGN(SRC_PIXELS + MAX_FILTER_WIDTH - 1, 4)]);
  314. LOCAL_ALIGNED_32(uint32_t, dst0, [SRC_PIXELS]);
  315. LOCAL_ALIGNED_32(uint32_t, dst1, [SRC_PIXELS]);
  316. // padded
  317. LOCAL_ALIGNED_32(int16_t, filter, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]);
  318. LOCAL_ALIGNED_32(int32_t, filterPos, [SRC_PIXELS]);
  319. LOCAL_ALIGNED_32(int16_t, filterAvx2, [SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH]);
  320. LOCAL_ALIGNED_32(int32_t, filterPosAvx, [SRC_PIXELS]);
  321. // The dst parameter here is either int16_t or int32_t but we use void* to
  322. // just cover both cases.
  323. declare_func(void, void *c, void *dst, int dstW,
  324. const uint8_t *src, const int16_t *filter,
  325. const int32_t *filterPos, int filterSize);
  326. sws = sws_alloc_context();
  327. if (sws_init_context(sws, NULL, NULL) < 0)
  328. fail();
  329. c = sws_internal(sws);
  330. randomize_buffers(src, SRC_PIXELS + MAX_FILTER_WIDTH - 1);
  331. for (hpi = 0; hpi < HSCALE_PAIRS; hpi++) {
  332. for (fsi = 0; fsi < FILTER_SIZES; fsi++) {
  333. for (dstWi = 0; dstWi < FF_ARRAY_ELEMS(input_sizes); dstWi++) {
  334. width = filter_sizes[fsi];
  335. c->srcBpc = hscale_pairs[hpi][0];
  336. c->dstBpc = hscale_pairs[hpi][1];
  337. c->hLumFilterSize = c->hChrFilterSize = width;
  338. for (i = 0; i < SRC_PIXELS; i++) {
  339. filterPos[i] = i;
  340. filterPosAvx[i] = i;
  341. // These filter cofficients are chosen to try break two corner
  342. // cases, namely:
  343. //
  344. // - Negative filter coefficients. The filters output signed
  345. // values, and it should be possible to end up with negative
  346. // output values.
  347. //
  348. // - Positive clipping. The hscale filter function has clipping
  349. // at (1<<15) - 1
  350. //
  351. // The coefficients sum to the 1.0 point for the hscale
  352. // functions (1 << 14).
  353. for (j = 0; j < width; j++) {
  354. filter[i * width + j] = -((1 << 14) / (width - 1));
  355. }
  356. filter[i * width + (rnd() % width)] = ((1 << 15) - 1);
  357. }
  358. for (i = 0; i < MAX_FILTER_WIDTH; i++) {
  359. // These values should be unused in SIMD implementations but
  360. // may still be read, random coefficients here should help show
  361. // issues where they are used in error.
  362. filter[SRC_PIXELS * width + i] = rnd();
  363. }
  364. sws->dst_w = c->chrDstW = input_sizes[dstWi];
  365. ff_sws_init_scale(c);
  366. memcpy(filterAvx2, filter, sizeof(uint16_t) * (SRC_PIXELS * MAX_FILTER_WIDTH + MAX_FILTER_WIDTH));
  367. ff_shuffle_filter_coefficients(c, filterPosAvx, width, filterAvx2, sws->dst_w);
  368. av_assert0(c->hyScale == c->hcScale);
  369. if (check_func(c->hcScale, "hscale_%d_to_%d__fs_%d_dstW_%d", c->srcBpc, c->dstBpc + 1, width, sws->dst_w)) {
  370. memset(dst0, 0, SRC_PIXELS * sizeof(dst0[0]));
  371. memset(dst1, 0, SRC_PIXELS * sizeof(dst1[0]));
  372. call_ref(NULL, dst0, sws->dst_w, src, filter, filterPos, width);
  373. call_new(NULL, dst1, sws->dst_w, src, filterAvx2, filterPosAvx, width);
  374. if (memcmp(dst0, dst1, sws->dst_w * sizeof(dst0[0])))
  375. fail();
  376. bench_new(NULL, dst0, sws->dst_w, src, filter, filterPosAvx, width);
  377. }
  378. }
  379. }
  380. }
  381. sws_freeContext(sws);
  382. }
  383. void checkasm_check_sw_scale(void)
  384. {
  385. check_hscale();
  386. report("hscale");
  387. check_yuv2yuv1(0);
  388. check_yuv2yuv1(1);
  389. report("yuv2yuv1");
  390. check_yuv2yuvX(0);
  391. check_yuv2yuvX(1);
  392. report("yuv2yuvX");
  393. check_yuv2nv12cX(0);
  394. check_yuv2nv12cX(1);
  395. report("yuv2nv12cX");
  396. }