vf_colorspace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (c) 2016 Ronald S. Bultje <rsbultje@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <string.h>
  21. #include "checkasm.h"
  22. #include "libavfilter/colorspacedsp.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/internal.h"
  25. #include "libavutil/intreadwrite.h"
  26. #define W 64
  27. #define H 64
  28. #define randomize_buffers() \
  29. do { \
  30. unsigned mask = bpp_mask[idepth]; \
  31. int n, m; \
  32. int bpp = 1 + (!!idepth); \
  33. int buf_size = W * H * bpp; \
  34. for (m = 0; m < 3; m++) { \
  35. int ss = m ? ss_w + ss_h : 0; \
  36. int plane_sz = buf_size >> ss; \
  37. for (n = 0; n < plane_sz; n += 4) { \
  38. unsigned r = rnd() & mask; \
  39. AV_WN32A(&src[m][n], r); \
  40. } \
  41. } \
  42. } while (0)
  43. static const char *format_string[] = {
  44. "444", "422", "420"
  45. };
  46. static const unsigned bpp_mask[] = { 0xffffffff, 0x03ff03ff, 0x0fff0fff };
  47. static void check_yuv2yuv(void)
  48. {
  49. declare_func(void, uint8_t *dst[3], ptrdiff_t dst_stride[3],
  50. uint8_t *src[3], ptrdiff_t src_stride[3],
  51. int w, int h, const int16_t coeff[3][3][8],
  52. const int16_t off[2][8]);
  53. ColorSpaceDSPContext dsp;
  54. int idepth, odepth, fmt, n;
  55. LOCAL_ALIGNED_32(uint8_t, src_y, [W * H * 2]);
  56. LOCAL_ALIGNED_32(uint8_t, src_u, [W * H * 2]);
  57. LOCAL_ALIGNED_32(uint8_t, src_v, [W * H * 2]);
  58. uint8_t *src[3] = { src_y, src_u, src_v };
  59. LOCAL_ALIGNED_32(uint8_t, dst0_y, [W * H * 2]);
  60. LOCAL_ALIGNED_32(uint8_t, dst0_u, [W * H * 2]);
  61. LOCAL_ALIGNED_32(uint8_t, dst0_v, [W * H * 2]);
  62. LOCAL_ALIGNED_32(uint8_t, dst1_y, [W * H * 2]);
  63. LOCAL_ALIGNED_32(uint8_t, dst1_u, [W * H * 2]);
  64. LOCAL_ALIGNED_32(uint8_t, dst1_v, [W * H * 2]);
  65. uint8_t *dst0[3] = { dst0_y, dst0_u, dst0_v }, *dst1[3] = { dst1_y, dst1_u, dst1_v };
  66. LOCAL_ALIGNED_32(int16_t, offset_buf, [16]);
  67. LOCAL_ALIGNED_32(int16_t, coeff_buf, [3 * 3 * 8]);
  68. int16_t (*offset)[8] = (int16_t(*)[8]) offset_buf;
  69. int16_t (*coeff)[3][8] = (int16_t(*)[3][8]) coeff_buf;
  70. ff_colorspacedsp_init(&dsp);
  71. for (n = 0; n < 8; n++) {
  72. offset[0][n] = offset[1][n] = 16;
  73. coeff[0][0][n] = (1 << 14) + (1 << 7) + 1;
  74. coeff[0][1][n] = (1 << 7) - 1;
  75. coeff[0][2][n] = -(1 << 8);
  76. coeff[1][0][n] = coeff[2][0][n] = 0;
  77. coeff[1][1][n] = (1 << 14) + (1 << 7);
  78. coeff[1][2][n] = -(1 << 7);
  79. coeff[2][2][n] = (1 << 14) - (1 << 6);
  80. coeff[2][1][n] = 1 << 6;
  81. }
  82. for (idepth = 0; idepth < 3; idepth++) {
  83. for (odepth = 0; odepth < 3; odepth++) {
  84. for (fmt = 0; fmt < 3; fmt++) {
  85. if (check_func(dsp.yuv2yuv[idepth][odepth][fmt],
  86. "ff_colorspacedsp_yuv2yuv_%sp%dto%d",
  87. format_string[fmt],
  88. idepth * 2 + 8, odepth * 2 + 8)) {
  89. int ss_w = !!fmt, ss_h = fmt == 2;
  90. int y_src_stride = W << !!idepth, y_dst_stride = W << !!odepth;
  91. int uv_src_stride = y_src_stride >> ss_w, uv_dst_stride = y_dst_stride >> ss_w;
  92. randomize_buffers();
  93. call_ref(dst0, (ptrdiff_t[3]) { y_dst_stride, uv_dst_stride, uv_dst_stride },
  94. src, (ptrdiff_t[3]) { y_src_stride, uv_src_stride, uv_src_stride },
  95. W, H, coeff, offset);
  96. call_new(dst1, (ptrdiff_t[3]) { y_dst_stride, uv_dst_stride, uv_dst_stride },
  97. src, (ptrdiff_t[3]) { y_src_stride, uv_src_stride, uv_src_stride },
  98. W, H, coeff, offset);
  99. if (memcmp(dst0[0], dst1[0], y_dst_stride * H) ||
  100. memcmp(dst0[1], dst1[1], uv_dst_stride * H >> ss_h) ||
  101. memcmp(dst0[2], dst1[2], uv_dst_stride * H >> ss_h)) {
  102. fail();
  103. }
  104. }
  105. }
  106. }
  107. }
  108. report("yuv2yuv");
  109. }
  110. static void check_yuv2rgb(void)
  111. {
  112. declare_func(void, int16_t *dst[3], ptrdiff_t dst_stride,
  113. uint8_t *src[3], ptrdiff_t src_stride[3],
  114. int w, int h, const int16_t coeff[3][3][8],
  115. const int16_t off[8]);
  116. ColorSpaceDSPContext dsp;
  117. int idepth, fmt, n;
  118. LOCAL_ALIGNED_32(uint8_t, src_y, [W * H * 2]);
  119. LOCAL_ALIGNED_32(uint8_t, src_u, [W * H * 2]);
  120. LOCAL_ALIGNED_32(uint8_t, src_v, [W * H * 2]);
  121. uint8_t *src[3] = { src_y, src_u, src_v };
  122. LOCAL_ALIGNED_32(int16_t, dst0_y, [W * H]);
  123. LOCAL_ALIGNED_32(int16_t, dst0_u, [W * H]);
  124. LOCAL_ALIGNED_32(int16_t, dst0_v, [W * H]);
  125. LOCAL_ALIGNED_32(int16_t, dst1_y, [W * H]);
  126. LOCAL_ALIGNED_32(int16_t, dst1_u, [W * H]);
  127. LOCAL_ALIGNED_32(int16_t, dst1_v, [W * H]);
  128. int16_t *dst0[3] = { dst0_y, dst0_u, dst0_v }, *dst1[3] = { dst1_y, dst1_u, dst1_v };
  129. LOCAL_ALIGNED_32(int16_t, offset, [8]);
  130. LOCAL_ALIGNED_32(int16_t, coeff_buf, [3 * 3 * 8]);
  131. int16_t (*coeff)[3][8] = (int16_t(*)[3][8]) coeff_buf;
  132. ff_colorspacedsp_init(&dsp);
  133. for (n = 0; n < 8; n++) {
  134. offset[n] = 16;
  135. coeff[0][0][n] = coeff[1][0][n] = coeff[2][0][n] = (1 << 14) | 1;
  136. coeff[0][1][n] = coeff[2][2][n] = 0;
  137. coeff[0][2][n] = 1 << 13;
  138. coeff[1][1][n] = -(1 << 12);
  139. coeff[1][2][n] = 1 << 12;
  140. coeff[2][1][n] = 1 << 11;
  141. }
  142. for (idepth = 0; idepth < 3; idepth++) {
  143. for (fmt = 0; fmt < 3; fmt++) {
  144. if (check_func(dsp.yuv2rgb[idepth][fmt],
  145. "ff_colorspacedsp_yuv2rgb_%sp%d",
  146. format_string[fmt], idepth * 2 + 8)) {
  147. int ss_w = !!fmt, ss_h = fmt == 2;
  148. int y_src_stride = W << !!idepth;
  149. int uv_src_stride = y_src_stride >> ss_w;
  150. randomize_buffers();
  151. call_ref(dst0, W, src,
  152. (ptrdiff_t[3]) { y_src_stride, uv_src_stride, uv_src_stride },
  153. W, H, coeff, offset);
  154. call_new(dst1, W, src,
  155. (ptrdiff_t[3]) { y_src_stride, uv_src_stride, uv_src_stride },
  156. W, H, coeff, offset);
  157. if (memcmp(dst0[0], dst1[0], W * H * sizeof(int16_t)) ||
  158. memcmp(dst0[1], dst1[1], W * H * sizeof(int16_t)) ||
  159. memcmp(dst0[2], dst1[2], W * H * sizeof(int16_t))) {
  160. fail();
  161. }
  162. }
  163. }
  164. }
  165. report("yuv2rgb");
  166. }
  167. #undef randomize_buffers
  168. #define randomize_buffers() \
  169. do { \
  170. int y, x, p; \
  171. for (p = 0; p < 3; p++) { \
  172. for (y = 0; y < H; y++) { \
  173. for (x = 0; x < W; x++) { \
  174. int r = rnd() & 0x7fff; \
  175. r -= (32768 - 28672) >> 1; \
  176. src[p][y * W + x] = r; \
  177. } \
  178. } \
  179. } \
  180. } while (0)
  181. static void check_rgb2yuv(void)
  182. {
  183. declare_func(void, uint8_t *dst[3], ptrdiff_t dst_stride[3],
  184. int16_t *src[3], ptrdiff_t src_stride,
  185. int w, int h, const int16_t coeff[3][3][8],
  186. const int16_t off[8]);
  187. ColorSpaceDSPContext dsp;
  188. int odepth, fmt, n;
  189. LOCAL_ALIGNED_32(int16_t, src_y, [W * H * 2]);
  190. LOCAL_ALIGNED_32(int16_t, src_u, [W * H * 2]);
  191. LOCAL_ALIGNED_32(int16_t, src_v, [W * H * 2]);
  192. int16_t *src[3] = { src_y, src_u, src_v };
  193. LOCAL_ALIGNED_32(uint8_t, dst0_y, [W * H * 2]);
  194. LOCAL_ALIGNED_32(uint8_t, dst0_u, [W * H * 2]);
  195. LOCAL_ALIGNED_32(uint8_t, dst0_v, [W * H * 2]);
  196. LOCAL_ALIGNED_32(uint8_t, dst1_y, [W * H * 2]);
  197. LOCAL_ALIGNED_32(uint8_t, dst1_u, [W * H * 2]);
  198. LOCAL_ALIGNED_32(uint8_t, dst1_v, [W * H * 2]);
  199. uint8_t *dst0[3] = { dst0_y, dst0_u, dst0_v }, *dst1[3] = { dst1_y, dst1_u, dst1_v };
  200. LOCAL_ALIGNED_32(int16_t, offset, [8]);
  201. LOCAL_ALIGNED_32(int16_t, coeff_buf, [3 * 3 * 8]);
  202. int16_t (*coeff)[3][8] = (int16_t(*)[3][8]) coeff_buf;
  203. ff_colorspacedsp_init(&dsp);
  204. for (n = 0; n < 8; n++) {
  205. offset[n] = 16;
  206. // these somewhat resemble bt601/smpte170m coefficients
  207. coeff[0][0][n] = lrint(0.3 * (1 << 14));
  208. coeff[0][1][n] = lrint(0.6 * (1 << 14));
  209. coeff[0][2][n] = lrint(0.1 * (1 << 14));
  210. coeff[1][0][n] = lrint(-0.15 * (1 << 14));
  211. coeff[1][1][n] = lrint(-0.35 * (1 << 14));
  212. coeff[1][2][n] = lrint(0.5 * (1 << 14));
  213. coeff[2][0][n] = lrint(0.5 * (1 << 14));
  214. coeff[2][1][n] = lrint(-0.42 * (1 << 14));
  215. coeff[2][2][n] = lrint(-0.08 * (1 << 14));
  216. }
  217. for (odepth = 0; odepth < 3; odepth++) {
  218. for (fmt = 0; fmt < 3; fmt++) {
  219. if (check_func(dsp.rgb2yuv[odepth][fmt],
  220. "ff_colorspacedsp_rgb2yuv_%sp%d",
  221. format_string[fmt], odepth * 2 + 8)) {
  222. int ss_w = !!fmt, ss_h = fmt == 2;
  223. int y_dst_stride = W << !!odepth;
  224. int uv_dst_stride = y_dst_stride >> ss_w;
  225. randomize_buffers();
  226. call_ref(dst0, (ptrdiff_t[3]) { y_dst_stride, uv_dst_stride, uv_dst_stride },
  227. src, W, W, H, coeff, offset);
  228. call_new(dst1, (ptrdiff_t[3]) { y_dst_stride, uv_dst_stride, uv_dst_stride },
  229. src, W, W, H, coeff, offset);
  230. if (memcmp(dst0[0], dst1[0], H * y_dst_stride) ||
  231. memcmp(dst0[1], dst1[1], H * uv_dst_stride >> ss_h) ||
  232. memcmp(dst0[2], dst1[2], H * uv_dst_stride >> ss_h)) {
  233. fail();
  234. }
  235. }
  236. }
  237. }
  238. report("rgb2yuv");
  239. }
  240. static void check_multiply3x3(void)
  241. {
  242. declare_func(void, int16_t *data[3], ptrdiff_t stride,
  243. int w, int h, const int16_t coeff[3][3][8]);
  244. ColorSpaceDSPContext dsp;
  245. LOCAL_ALIGNED_32(int16_t, dst0_y, [W * H]);
  246. LOCAL_ALIGNED_32(int16_t, dst0_u, [W * H]);
  247. LOCAL_ALIGNED_32(int16_t, dst0_v, [W * H]);
  248. LOCAL_ALIGNED_32(int16_t, dst1_y, [W * H]);
  249. LOCAL_ALIGNED_32(int16_t, dst1_u, [W * H]);
  250. LOCAL_ALIGNED_32(int16_t, dst1_v, [W * H]);
  251. int16_t *dst0[3] = { dst0_y, dst0_u, dst0_v }, *dst1[3] = { dst1_y, dst1_u, dst1_v };
  252. int16_t **src = dst0;
  253. LOCAL_ALIGNED_32(int16_t, coeff_buf, [3 * 3 * 8]);
  254. int16_t (*coeff)[3][8] = (int16_t(*)[3][8]) coeff_buf;
  255. int n;
  256. ff_colorspacedsp_init(&dsp);
  257. for (n = 0; n < 8; n++) {
  258. coeff[0][0][n] = lrint(0.85 * (1 << 14));
  259. coeff[0][1][n] = lrint(0.10 * (1 << 14));
  260. coeff[0][2][n] = lrint(0.05 * (1 << 14));
  261. coeff[1][0][n] = lrint(-0.1 * (1 << 14));
  262. coeff[1][1][n] = lrint(0.95 * (1 << 14));
  263. coeff[1][2][n] = lrint(0.15 * (1 << 14));
  264. coeff[2][0][n] = lrint(-0.2 * (1 << 14));
  265. coeff[2][1][n] = lrint(0.30 * (1 << 14));
  266. coeff[2][2][n] = lrint(0.90 * (1 << 14));
  267. }
  268. if (check_func(dsp.multiply3x3, "ff_colorspacedsp_multiply3x3")) {
  269. randomize_buffers();
  270. memcpy(dst1_y, dst0_y, W * H * sizeof(*dst1_y));
  271. memcpy(dst1_u, dst0_u, W * H * sizeof(*dst1_u));
  272. memcpy(dst1_v, dst0_v, W * H * sizeof(*dst1_v));
  273. call_ref(dst0, W, W, H, coeff);
  274. call_new(dst1, W, W, H, coeff);
  275. if (memcmp(dst0[0], dst1[0], H * W * sizeof(*dst0_y)) ||
  276. memcmp(dst0[1], dst1[1], H * W * sizeof(*dst0_u)) ||
  277. memcmp(dst0[2], dst1[2], H * W * sizeof(*dst0_v))) {
  278. fail();
  279. }
  280. }
  281. report("multiply3x3");
  282. }
  283. void checkasm_check_colorspace(void)
  284. {
  285. check_yuv2yuv();
  286. check_yuv2rgb();
  287. check_rgb2yuv();
  288. check_multiply3x3();
  289. }