You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
5.9KB

  1. /*
  2. Copyright (C) 2003 Michael Niedermayer <michaelni@gmx.at>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include <stdarg.h>
  20. #undef HAVE_AV_CONFIG_H
  21. #include "avutil.h"
  22. #include "swscale.h"
  23. static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h){
  24. int x,y;
  25. uint64_t ssd=0;
  26. //printf("%d %d\n", w, h);
  27. for(y=0; y<h; y++){
  28. for(x=0; x<w; x++){
  29. int d= src1[x + y*stride1] - src2[x + y*stride2];
  30. ssd+= d*d;
  31. //printf("%d", abs(src1[x + y*stride1] - src2[x + y*stride2])/26 );
  32. }
  33. //printf("\n");
  34. }
  35. return ssd;
  36. }
  37. // test by ref -> src -> dst -> out & compare out against ref
  38. // ref & out are YV12
  39. static int doTest(uint8_t *ref[3], int refStride[3], int w, int h, int srcFormat, int dstFormat,
  40. int srcW, int srcH, int dstW, int dstH, int flags){
  41. uint8_t *src[3];
  42. uint8_t *dst[3];
  43. uint8_t *out[3];
  44. int srcStride[3], dstStride[3];
  45. int i;
  46. uint64_t ssdY, ssdU, ssdV;
  47. struct SwsContext *srcContext, *dstContext, *outContext;
  48. int res;
  49. res = 0;
  50. for(i=0; i<3; i++){
  51. // avoid stride % bpp != 0
  52. if(srcFormat==PIX_FMT_RGB24 || srcFormat==PIX_FMT_BGR24)
  53. srcStride[i]= srcW*3;
  54. else
  55. srcStride[i]= srcW*4;
  56. if(dstFormat==PIX_FMT_RGB24 || dstFormat==PIX_FMT_BGR24)
  57. dstStride[i]= dstW*3;
  58. else
  59. dstStride[i]= dstW*4;
  60. src[i]= (uint8_t*) malloc(srcStride[i]*srcH);
  61. dst[i]= (uint8_t*) malloc(dstStride[i]*dstH);
  62. out[i]= (uint8_t*) malloc(refStride[i]*h);
  63. if ((src[i] == NULL) || (dst[i] == NULL) || (out[i] == NULL)) {
  64. perror("Malloc");
  65. res = -1;
  66. goto end;
  67. }
  68. }
  69. dstContext = outContext = NULL;
  70. srcContext= sws_getContext(w, h, PIX_FMT_YUV420P, srcW, srcH, srcFormat, flags, NULL, NULL, NULL);
  71. if (srcContext == NULL) {
  72. fprintf(stderr, "Failed to get %s ---> %s\n",
  73. sws_format_name(PIX_FMT_YUV420P),
  74. sws_format_name(srcFormat));
  75. res = -1;
  76. goto end;
  77. }
  78. dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
  79. if (dstContext == NULL) {
  80. fprintf(stderr, "Failed to get %s ---> %s\n",
  81. sws_format_name(srcFormat),
  82. sws_format_name(dstFormat));
  83. res = -1;
  84. goto end;
  85. }
  86. outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
  87. if (outContext == NULL) {
  88. fprintf(stderr, "Failed to get %s ---> %s\n",
  89. sws_format_name(dstFormat),
  90. sws_format_name(PIX_FMT_YUV420P));
  91. res = -1;
  92. goto end;
  93. }
  94. // printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
  95. // (int)src[0], (int)src[1], (int)src[2]);
  96. sws_scale(srcContext, ref, refStride, 0, h , src, srcStride);
  97. sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
  98. sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
  99. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  100. asm volatile ("emms\n\t");
  101. #endif
  102. ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  103. ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
  104. ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
  105. if(srcFormat == PIX_FMT_GRAY8 || dstFormat==PIX_FMT_GRAY8) ssdU=ssdV=0; //FIXME check that output is really gray
  106. ssdY/= w*h;
  107. ssdU/= w*h/4;
  108. ssdV/= w*h/4;
  109. if(ssdY>100 || ssdU>100 || ssdV>100){
  110. printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5lld,%5lld,%5lld\n",
  111. sws_format_name(srcFormat), srcW, srcH,
  112. sws_format_name(dstFormat), dstW, dstH,
  113. flags,
  114. ssdY, ssdU, ssdV);
  115. }
  116. end:
  117. sws_freeContext(srcContext);
  118. sws_freeContext(dstContext);
  119. sws_freeContext(outContext);
  120. for(i=0; i<3; i++){
  121. free(src[i]);
  122. free(dst[i]);
  123. free(out[i]);
  124. }
  125. return res;
  126. }
  127. void fast_memcpy(void *a, void *b, int s){ //FIXME
  128. memcpy(a, b, s);
  129. }
  130. static void selfTest(uint8_t *src[3], int stride[3], int w, int h){
  131. enum PixelFormat srcFormat, dstFormat;
  132. int srcW, srcH, dstW, dstH;
  133. int flags;
  134. for(srcFormat = 0; srcFormat < PIX_FMT_NB; srcFormat++) {
  135. for(dstFormat = 0; dstFormat < PIX_FMT_NB; dstFormat++) {
  136. printf("%s -> %s\n",
  137. sws_format_name(srcFormat),
  138. sws_format_name(dstFormat));
  139. srcW= w;
  140. srcH= h;
  141. for(dstW=w - w/3; dstW<= 4*w/3; dstW+= w/3){
  142. for(dstH=h - h/3; dstH<= 4*h/3; dstH+= h/3){
  143. for(flags=1; flags<33; flags*=2) {
  144. int res;
  145. res = doTest(src, stride, w, h, srcFormat, dstFormat,
  146. srcW, srcH, dstW, dstH, flags);
  147. if (res < 0) {
  148. dstW = 4 * w / 3;
  149. dstH = 4 * h / 3;
  150. flags = 33;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. }
  158. #define W 96
  159. #define H 96
  160. int main(int argc, char **argv){
  161. uint8_t rgb_data[W*H*4];
  162. uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
  163. int rgb_stride[3]={4*W, 0, 0};
  164. uint8_t data[3][W*H];
  165. uint8_t *src[3]= {data[0], data[1], data[2]};
  166. int stride[3]={W, W, W};
  167. int x, y;
  168. struct SwsContext *sws;
  169. sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUV420P, 2, NULL, NULL, NULL);
  170. for(y=0; y<H; y++){
  171. for(x=0; x<W*4; x++){
  172. rgb_data[ x + y*4*W]= random();
  173. }
  174. }
  175. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  176. sws_rgb2rgb_init(SWS_CPU_CAPS_MMX*0);
  177. #else
  178. sws_rgb2rgb_init(0);
  179. #endif
  180. sws_scale(sws, rgb_src, rgb_stride, 0, H , src, stride);
  181. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  182. asm volatile ("emms\n\t");
  183. #endif
  184. selfTest(src, stride, W, H);
  185. return 123;
  186. }