sw_rgb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_internal.h"
  23. #include "libavutil/pixdesc.h"
  24. #include "libswscale/rgb2rgb.h"
  25. #include "libswscale/swscale.h"
  26. #include "libswscale/swscale_internal.h"
  27. #include "checkasm.h"
  28. #define randomize_buffers(buf, size) \
  29. do { \
  30. int j; \
  31. for (j = 0; j < size; j+=4) \
  32. AV_WN32(buf + j, rnd()); \
  33. } while (0)
  34. static const uint8_t width[] = {12, 16, 20, 32, 36, 128};
  35. static const struct {uint8_t w, h, s;} planes[] = {
  36. {12,16,12}, {16,16,16}, {20,23,25}, {32,18,48}, {8,128,16}, {128,128,128}
  37. };
  38. #define MAX_STRIDE 128
  39. #define MAX_HEIGHT 128
  40. static void check_shuffle_bytes(void * func, const char * report)
  41. {
  42. int i;
  43. LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE]);
  44. LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE]);
  45. LOCAL_ALIGNED_32(uint8_t, dst0, [MAX_STRIDE]);
  46. LOCAL_ALIGNED_32(uint8_t, dst1, [MAX_STRIDE]);
  47. declare_func(void, const uint8_t *src, uint8_t *dst, int src_size);
  48. memset(dst0, 0, MAX_STRIDE);
  49. memset(dst1, 0, MAX_STRIDE);
  50. randomize_buffers(src0, MAX_STRIDE);
  51. memcpy(src1, src0, MAX_STRIDE);
  52. if (check_func(func, "%s", report)) {
  53. for (i = 0; i < 6; i ++) {
  54. call_ref(src0, dst0, width[i]);
  55. call_new(src1, dst1, width[i]);
  56. if (memcmp(dst0, dst1, MAX_STRIDE))
  57. fail();
  58. }
  59. bench_new(src0, dst0, width[5]);
  60. }
  61. }
  62. static void check_uyvy_to_422p(void)
  63. {
  64. int i;
  65. LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE * MAX_HEIGHT * 2]);
  66. LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE * MAX_HEIGHT * 2]);
  67. LOCAL_ALIGNED_32(uint8_t, dst_y_0, [MAX_STRIDE * MAX_HEIGHT]);
  68. LOCAL_ALIGNED_32(uint8_t, dst_y_1, [MAX_STRIDE * MAX_HEIGHT]);
  69. LOCAL_ALIGNED_32(uint8_t, dst_u_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  70. LOCAL_ALIGNED_32(uint8_t, dst_u_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  71. LOCAL_ALIGNED_32(uint8_t, dst_v_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  72. LOCAL_ALIGNED_32(uint8_t, dst_v_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  73. declare_func(void, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  74. const uint8_t *src, int width, int height,
  75. int lumStride, int chromStride, int srcStride);
  76. randomize_buffers(src0, MAX_STRIDE * MAX_HEIGHT * 2);
  77. memcpy(src1, src0, MAX_STRIDE * MAX_HEIGHT * 2);
  78. if (check_func(uyvytoyuv422, "uyvytoyuv422")) {
  79. for (i = 0; i < 6; i ++) {
  80. memset(dst_y_0, 0, MAX_STRIDE * MAX_HEIGHT);
  81. memset(dst_y_1, 0, MAX_STRIDE * MAX_HEIGHT);
  82. memset(dst_u_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  83. memset(dst_u_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  84. memset(dst_v_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  85. memset(dst_v_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  86. call_ref(dst_y_0, dst_u_0, dst_v_0, src0, planes[i].w, planes[i].h,
  87. MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
  88. call_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[i].w, planes[i].h,
  89. MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
  90. if (memcmp(dst_y_0, dst_y_1, MAX_STRIDE * MAX_HEIGHT) ||
  91. memcmp(dst_u_0, dst_u_1, (MAX_STRIDE/2) * MAX_HEIGHT) ||
  92. memcmp(dst_v_0, dst_v_1, (MAX_STRIDE/2) * MAX_HEIGHT))
  93. fail();
  94. }
  95. bench_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[5].w, planes[5].h,
  96. MAX_STRIDE, MAX_STRIDE / 2, planes[5].s);
  97. }
  98. }
  99. static void check_interleave_bytes(void)
  100. {
  101. LOCAL_ALIGNED_16(uint8_t, src0_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
  102. LOCAL_ALIGNED_16(uint8_t, src1_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
  103. LOCAL_ALIGNED_16(uint8_t, dst0_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
  104. LOCAL_ALIGNED_16(uint8_t, dst1_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
  105. // Intentionally using unaligned buffers, as this function doesn't have
  106. // any alignment requirements.
  107. uint8_t *src0 = src0_buf + 1;
  108. uint8_t *src1 = src1_buf + 1;
  109. uint8_t *dst0 = dst0_buf + 2;
  110. uint8_t *dst1 = dst1_buf + 2;
  111. declare_func(void, const uint8_t *, const uint8_t *,
  112. uint8_t *, int, int, int, int, int);
  113. randomize_buffers(src0, MAX_STRIDE * MAX_HEIGHT);
  114. randomize_buffers(src1, MAX_STRIDE * MAX_HEIGHT);
  115. if (check_func(interleaveBytes, "interleave_bytes")) {
  116. for (int i = 0; i <= 16; i++) {
  117. // Try all widths [1,16], and try one random width.
  118. int w = i > 0 ? i : (1 + (rnd() % (MAX_STRIDE-2)));
  119. int h = 1 + (rnd() % (MAX_HEIGHT-2));
  120. int src0_offset = 0, src0_stride = MAX_STRIDE;
  121. int src1_offset = 0, src1_stride = MAX_STRIDE;
  122. int dst_offset = 0, dst_stride = 2 * MAX_STRIDE;
  123. memset(dst0, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
  124. memset(dst1, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
  125. // Try different combinations of negative strides
  126. if (i & 1) {
  127. src0_offset = (h-1)*src0_stride;
  128. src0_stride = -src0_stride;
  129. }
  130. if (i & 2) {
  131. src1_offset = (h-1)*src1_stride;
  132. src1_stride = -src1_stride;
  133. }
  134. if (i & 4) {
  135. dst_offset = (h-1)*dst_stride;
  136. dst_stride = -dst_stride;
  137. }
  138. call_ref(src0 + src0_offset, src1 + src1_offset, dst0 + dst_offset,
  139. w, h, src0_stride, src1_stride, dst_stride);
  140. call_new(src0 + src0_offset, src1 + src1_offset, dst1 + dst_offset,
  141. w, h, src0_stride, src1_stride, dst_stride);
  142. // Check a one pixel-pair edge around the destination area,
  143. // to catch overwrites past the end.
  144. checkasm_check(uint8_t, dst0, 2*MAX_STRIDE, dst1, 2*MAX_STRIDE,
  145. 2 * w + 2, h + 1, "dst");
  146. }
  147. bench_new(src0, src1, dst1, 127, MAX_HEIGHT,
  148. MAX_STRIDE, MAX_STRIDE, 2*MAX_STRIDE);
  149. }
  150. if (check_func(interleaveBytes, "interleave_bytes_aligned")) {
  151. // Bench the function in a more typical case, with aligned
  152. // buffers and widths.
  153. bench_new(src0_buf, src1_buf, dst1_buf, 128, MAX_HEIGHT,
  154. MAX_STRIDE, MAX_STRIDE, 2*MAX_STRIDE);
  155. }
  156. }
  157. #define MAX_LINE_SIZE 1920
  158. static const int input_sizes[] = {8, 128, 1080, MAX_LINE_SIZE};
  159. static const enum AVPixelFormat rgb_formats[] = {
  160. AV_PIX_FMT_RGB24,
  161. AV_PIX_FMT_BGR24,
  162. AV_PIX_FMT_RGBA,
  163. AV_PIX_FMT_BGRA,
  164. AV_PIX_FMT_ABGR,
  165. AV_PIX_FMT_ARGB,
  166. };
  167. static void check_rgb_to_y(struct SwsContext *ctx)
  168. {
  169. LOCAL_ALIGNED_16(uint8_t, src24, [MAX_LINE_SIZE * 3]);
  170. LOCAL_ALIGNED_16(uint8_t, src32, [MAX_LINE_SIZE * 4]);
  171. LOCAL_ALIGNED_32(uint8_t, dst0_y, [MAX_LINE_SIZE * 2]);
  172. LOCAL_ALIGNED_32(uint8_t, dst1_y, [MAX_LINE_SIZE * 2]);
  173. declare_func(void, uint8_t *dst, const uint8_t *src,
  174. const uint8_t *unused1, const uint8_t *unused2, int width,
  175. uint32_t *rgb2yuv, void *opq);
  176. randomize_buffers(src24, MAX_LINE_SIZE * 3);
  177. randomize_buffers(src32, MAX_LINE_SIZE * 4);
  178. for (int i = 0; i < FF_ARRAY_ELEMS(rgb_formats); i++) {
  179. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(rgb_formats[i]);
  180. ctx->srcFormat = rgb_formats[i];
  181. ff_sws_init_scale(ctx);
  182. for (int j = 0; j < FF_ARRAY_ELEMS(input_sizes); j++) {
  183. int w = input_sizes[j];
  184. if (check_func(ctx->lumToYV12, "%s_to_y_%d", desc->name, w)) {
  185. const uint8_t *src = desc->nb_components == 3 ? src24 : src32;
  186. memset(dst0_y, 0xFA, MAX_LINE_SIZE * 2);
  187. memset(dst1_y, 0xFA, MAX_LINE_SIZE * 2);
  188. call_ref(dst0_y, src, NULL, NULL, w, ctx->input_rgb2yuv_table, NULL);
  189. call_new(dst1_y, src, NULL, NULL, w, ctx->input_rgb2yuv_table, NULL);
  190. if (memcmp(dst0_y, dst1_y, w * 2))
  191. fail();
  192. if (desc->nb_components == 3 ||
  193. // only bench native endian formats
  194. (ctx->srcFormat == AV_PIX_FMT_RGB32 || ctx->srcFormat == AV_PIX_FMT_RGB32_1))
  195. bench_new(dst1_y, src, NULL, NULL, w, ctx->input_rgb2yuv_table, NULL);
  196. }
  197. }
  198. }
  199. }
  200. static void check_rgb_to_uv(struct SwsContext *ctx)
  201. {
  202. LOCAL_ALIGNED_16(uint8_t, src24, [MAX_LINE_SIZE * 3]);
  203. LOCAL_ALIGNED_16(uint8_t, src32, [MAX_LINE_SIZE * 4]);
  204. LOCAL_ALIGNED_16(uint8_t, dst0_u, [MAX_LINE_SIZE * 2]);
  205. LOCAL_ALIGNED_16(uint8_t, dst0_v, [MAX_LINE_SIZE * 2]);
  206. LOCAL_ALIGNED_16(uint8_t, dst1_u, [MAX_LINE_SIZE * 2]);
  207. LOCAL_ALIGNED_16(uint8_t, dst1_v, [MAX_LINE_SIZE * 2]);
  208. declare_func(void, uint8_t *dstU, uint8_t *dstV,
  209. const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  210. int width, uint32_t *pal, void *opq);
  211. randomize_buffers(src24, MAX_LINE_SIZE * 3);
  212. randomize_buffers(src32, MAX_LINE_SIZE * 4);
  213. for (int i = 0; i < 2 * FF_ARRAY_ELEMS(rgb_formats); i++) {
  214. enum AVPixelFormat src_fmt = rgb_formats[i / 2];
  215. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(src_fmt);
  216. ctx->chrSrcHSubSample = (i % 2) ? 0 : 1;
  217. ctx->srcFormat = src_fmt;
  218. ctx->dstFormat = ctx->chrSrcHSubSample ? AV_PIX_FMT_YUV420P : AV_PIX_FMT_YUV444P;
  219. ff_sws_init_scale(ctx);
  220. for (int j = 0; j < FF_ARRAY_ELEMS(input_sizes); j++) {
  221. int w = input_sizes[j] >> ctx->chrSrcHSubSample;
  222. if (check_func(ctx->chrToYV12, "%s_to_uv%s_%d", desc->name,
  223. ctx->chrSrcHSubSample ? "_half" : "",
  224. input_sizes[j])) {
  225. const uint8_t *src = desc->nb_components == 3 ? src24 : src32;
  226. memset(dst0_u, 0xFF, MAX_LINE_SIZE * 2);
  227. memset(dst0_v, 0xFF, MAX_LINE_SIZE * 2);
  228. memset(dst1_u, 0xFF, MAX_LINE_SIZE * 2);
  229. memset(dst1_v, 0xFF, MAX_LINE_SIZE * 2);
  230. call_ref(dst0_u, dst0_v, NULL, src, src, w, ctx->input_rgb2yuv_table, NULL);
  231. call_new(dst1_u, dst1_v, NULL, src, src, w, ctx->input_rgb2yuv_table, NULL);
  232. if (memcmp(dst0_u, dst1_u, w * 2) || memcmp(dst0_v, dst1_v, w * 2))
  233. fail();
  234. if (desc->nb_components == 3 ||
  235. // only bench native endian formats
  236. (ctx->srcFormat == AV_PIX_FMT_RGB32 || ctx->srcFormat == AV_PIX_FMT_RGB32_1))
  237. bench_new(dst1_u, dst1_v, NULL, src, src, w, ctx->input_rgb2yuv_table, NULL);
  238. }
  239. }
  240. }
  241. }
  242. void checkasm_check_sw_rgb(void)
  243. {
  244. struct SwsContext *ctx;
  245. ff_sws_rgb2rgb_init();
  246. check_shuffle_bytes(shuffle_bytes_2103, "shuffle_bytes_2103");
  247. report("shuffle_bytes_2103");
  248. check_shuffle_bytes(shuffle_bytes_0321, "shuffle_bytes_0321");
  249. report("shuffle_bytes_0321");
  250. check_shuffle_bytes(shuffle_bytes_1230, "shuffle_bytes_1230");
  251. report("shuffle_bytes_1230");
  252. check_shuffle_bytes(shuffle_bytes_3012, "shuffle_bytes_3012");
  253. report("shuffle_bytes_3012");
  254. check_shuffle_bytes(shuffle_bytes_3210, "shuffle_bytes_3210");
  255. report("shuffle_bytes_3210");
  256. check_uyvy_to_422p();
  257. report("uyvytoyuv422");
  258. check_interleave_bytes();
  259. report("interleave_bytes");
  260. ctx = sws_getContext(MAX_LINE_SIZE, MAX_LINE_SIZE, AV_PIX_FMT_RGB24,
  261. MAX_LINE_SIZE, MAX_LINE_SIZE, AV_PIX_FMT_YUV420P,
  262. SWS_ACCURATE_RND | SWS_BITEXACT, NULL, NULL, NULL);
  263. if (!ctx)
  264. fail();
  265. check_rgb_to_y(ctx);
  266. report("rgb_to_y");
  267. check_rgb_to_uv(ctx);
  268. report("rgb_to_uv");
  269. sws_freeContext(ctx);
  270. }