sw_rgb.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 "libswscale/rgb2rgb.h"
  24. #include "checkasm.h"
  25. #define randomize_buffers(buf, size) \
  26. do { \
  27. int j; \
  28. for (j = 0; j < size; j+=4) \
  29. AV_WN32(buf + j, rnd()); \
  30. } while (0)
  31. static const uint8_t width[] = {12, 16, 20, 32, 36, 128};
  32. static const struct {uint8_t w, h, s;} planes[] = {
  33. {12,16,12}, {16,16,16}, {20,23,25}, {32,18,48}, {8,128,16}, {128,128,128}
  34. };
  35. #define MAX_STRIDE 128
  36. #define MAX_HEIGHT 128
  37. static void check_shuffle_bytes(void * func, const char * report)
  38. {
  39. int i;
  40. LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE]);
  41. LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE]);
  42. LOCAL_ALIGNED_32(uint8_t, dst0, [MAX_STRIDE]);
  43. LOCAL_ALIGNED_32(uint8_t, dst1, [MAX_STRIDE]);
  44. declare_func_emms(AV_CPU_FLAG_MMX, void, const uint8_t *src, uint8_t *dst, int src_size);
  45. memset(dst0, 0, MAX_STRIDE);
  46. memset(dst1, 0, MAX_STRIDE);
  47. randomize_buffers(src0, MAX_STRIDE);
  48. memcpy(src1, src0, MAX_STRIDE);
  49. if (check_func(func, "%s", report)) {
  50. for (i = 0; i < 6; i ++) {
  51. call_ref(src0, dst0, width[i]);
  52. call_new(src1, dst1, width[i]);
  53. if (memcmp(dst0, dst1, MAX_STRIDE))
  54. fail();
  55. }
  56. bench_new(src0, dst0, width[5]);
  57. }
  58. }
  59. static void check_uyvy_to_422p(void)
  60. {
  61. int i;
  62. LOCAL_ALIGNED_32(uint8_t, src0, [MAX_STRIDE * MAX_HEIGHT * 2]);
  63. LOCAL_ALIGNED_32(uint8_t, src1, [MAX_STRIDE * MAX_HEIGHT * 2]);
  64. LOCAL_ALIGNED_32(uint8_t, dst_y_0, [MAX_STRIDE * MAX_HEIGHT]);
  65. LOCAL_ALIGNED_32(uint8_t, dst_y_1, [MAX_STRIDE * MAX_HEIGHT]);
  66. LOCAL_ALIGNED_32(uint8_t, dst_u_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  67. LOCAL_ALIGNED_32(uint8_t, dst_u_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  68. LOCAL_ALIGNED_32(uint8_t, dst_v_0, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  69. LOCAL_ALIGNED_32(uint8_t, dst_v_1, [(MAX_STRIDE/2) * MAX_HEIGHT]);
  70. declare_func_emms(AV_CPU_FLAG_MMX, void, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  71. const uint8_t *src, int width, int height,
  72. int lumStride, int chromStride, int srcStride);
  73. randomize_buffers(src0, MAX_STRIDE * MAX_HEIGHT * 2);
  74. memcpy(src1, src0, MAX_STRIDE * MAX_HEIGHT * 2);
  75. if (check_func(uyvytoyuv422, "uyvytoyuv422")) {
  76. for (i = 0; i < 6; i ++) {
  77. memset(dst_y_0, 0, MAX_STRIDE * MAX_HEIGHT);
  78. memset(dst_y_1, 0, MAX_STRIDE * MAX_HEIGHT);
  79. memset(dst_u_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  80. memset(dst_u_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  81. memset(dst_v_0, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  82. memset(dst_v_1, 0, (MAX_STRIDE/2) * MAX_HEIGHT);
  83. call_ref(dst_y_0, dst_u_0, dst_v_0, src0, planes[i].w, planes[i].h,
  84. MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
  85. call_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[i].w, planes[i].h,
  86. MAX_STRIDE, MAX_STRIDE / 2, planes[i].s);
  87. if (memcmp(dst_y_0, dst_y_1, MAX_STRIDE * MAX_HEIGHT) ||
  88. memcmp(dst_u_0, dst_u_1, (MAX_STRIDE/2) * MAX_HEIGHT) ||
  89. memcmp(dst_v_0, dst_v_1, (MAX_STRIDE/2) * MAX_HEIGHT))
  90. fail();
  91. }
  92. bench_new(dst_y_1, dst_u_1, dst_v_1, src1, planes[5].w, planes[5].h,
  93. MAX_STRIDE, MAX_STRIDE / 2, planes[5].s);
  94. }
  95. }
  96. static void check_interleave_bytes(void)
  97. {
  98. LOCAL_ALIGNED_16(uint8_t, src0_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
  99. LOCAL_ALIGNED_16(uint8_t, src1_buf, [MAX_STRIDE*MAX_HEIGHT+1]);
  100. LOCAL_ALIGNED_16(uint8_t, dst0_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
  101. LOCAL_ALIGNED_16(uint8_t, dst1_buf, [2*MAX_STRIDE*MAX_HEIGHT+2]);
  102. // Intentionally using unaligned buffers, as this function doesn't have
  103. // any alignment requirements.
  104. uint8_t *src0 = src0_buf + 1;
  105. uint8_t *src1 = src1_buf + 1;
  106. uint8_t *dst0 = dst0_buf + 2;
  107. uint8_t *dst1 = dst1_buf + 2;
  108. declare_func_emms(AV_CPU_FLAG_MMX, void, const uint8_t *, const uint8_t *,
  109. uint8_t *, int, int, int, int, int);
  110. randomize_buffers(src0, MAX_STRIDE * MAX_HEIGHT);
  111. randomize_buffers(src1, MAX_STRIDE * MAX_HEIGHT);
  112. if (check_func(interleaveBytes, "interleave_bytes")) {
  113. for (int i = 0; i <= 16; i++) {
  114. // Try all widths [1,16], and try one random width.
  115. int w = i > 0 ? i : (1 + (rnd() % (MAX_STRIDE-2)));
  116. int h = 1 + (rnd() % (MAX_HEIGHT-2));
  117. int src0_offset = 0, src0_stride = MAX_STRIDE;
  118. int src1_offset = 0, src1_stride = MAX_STRIDE;
  119. int dst_offset = 0, dst_stride = 2 * MAX_STRIDE;
  120. memset(dst0, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
  121. memset(dst1, 0, 2 * MAX_STRIDE * MAX_HEIGHT);
  122. // Try different combinations of negative strides
  123. if (i & 1) {
  124. src0_offset = (h-1)*src0_stride;
  125. src0_stride = -src0_stride;
  126. }
  127. if (i & 2) {
  128. src1_offset = (h-1)*src1_stride;
  129. src1_stride = -src1_stride;
  130. }
  131. if (i & 4) {
  132. dst_offset = (h-1)*dst_stride;
  133. dst_stride = -dst_stride;
  134. }
  135. call_ref(src0 + src0_offset, src1 + src1_offset, dst0 + dst_offset,
  136. w, h, src0_stride, src1_stride, dst_stride);
  137. call_new(src0 + src0_offset, src1 + src1_offset, dst1 + dst_offset,
  138. w, h, src0_stride, src1_stride, dst_stride);
  139. // Check a one pixel-pair edge around the destination area,
  140. // to catch overwrites past the end.
  141. checkasm_check(uint8_t, dst0, 2*MAX_STRIDE, dst1, 2*MAX_STRIDE,
  142. 2 * w + 2, h + 1, "dst");
  143. }
  144. bench_new(src0, src1, dst1, 127, MAX_HEIGHT,
  145. MAX_STRIDE, MAX_STRIDE, 2*MAX_STRIDE);
  146. }
  147. if (check_func(interleaveBytes, "interleave_bytes_aligned")) {
  148. // Bench the function in a more typical case, with aligned
  149. // buffers and widths.
  150. bench_new(src0_buf, src1_buf, dst1_buf, 128, MAX_HEIGHT,
  151. MAX_STRIDE, MAX_STRIDE, 2*MAX_STRIDE);
  152. }
  153. }
  154. void checkasm_check_sw_rgb(void)
  155. {
  156. ff_sws_rgb2rgb_init();
  157. check_shuffle_bytes(shuffle_bytes_2103, "shuffle_bytes_2103");
  158. report("shuffle_bytes_2103");
  159. check_shuffle_bytes(shuffle_bytes_0321, "shuffle_bytes_0321");
  160. report("shuffle_bytes_0321");
  161. check_shuffle_bytes(shuffle_bytes_1230, "shuffle_bytes_1230");
  162. report("shuffle_bytes_1230");
  163. check_shuffle_bytes(shuffle_bytes_3012, "shuffle_bytes_3012");
  164. report("shuffle_bytes_3012");
  165. check_shuffle_bytes(shuffle_bytes_3210, "shuffle_bytes_3210");
  166. report("shuffle_bytes_3210");
  167. check_uyvy_to_422p();
  168. report("uyvytoyuv422");
  169. check_interleave_bytes();
  170. report("interleave_bytes");
  171. }