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.

221 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 int testFormat[]={
  24. PIX_FMT_YUV410P,
  25. PIX_FMT_YUV420P,
  26. //IMGFMT_IYUV,
  27. //IMGFMT_I420,
  28. PIX_FMT_BGR555,
  29. PIX_FMT_BGR565,
  30. PIX_FMT_BGR24,
  31. PIX_FMT_RGB32,
  32. PIX_FMT_RGB24,
  33. PIX_FMT_BGR32,
  34. //IMGFMT_Y8,
  35. PIX_FMT_GRAY8,
  36. //IMGFMT_YUY2,
  37. PIX_FMT_NONE
  38. };
  39. static uint64_t getSSD(uint8_t *src1, uint8_t *src2, int stride1, int stride2, int w, int h){
  40. int x,y;
  41. uint64_t ssd=0;
  42. //printf("%d %d\n", w, h);
  43. for(y=0; y<h; y++){
  44. for(x=0; x<w; x++){
  45. int d= src1[x + y*stride1] - src2[x + y*stride2];
  46. ssd+= d*d;
  47. //printf("%d", abs(src1[x + y*stride1] - src2[x + y*stride2])/26 );
  48. }
  49. //printf("\n");
  50. }
  51. return ssd;
  52. }
  53. // test by ref -> src -> dst -> out & compare out against ref
  54. // ref & out are YV12
  55. static void doTest(uint8_t *ref[3], int refStride[3], int w, int h, int srcFormat, int dstFormat,
  56. int srcW, int srcH, int dstW, int dstH, int flags){
  57. uint8_t *src[3];
  58. uint8_t *dst[3];
  59. uint8_t *out[3];
  60. int srcStride[3], dstStride[3];
  61. int i;
  62. uint64_t ssdY, ssdU, ssdV;
  63. struct SwsContext *srcContext, *dstContext, *outContext;
  64. for(i=0; i<3; i++){
  65. // avoid stride % bpp != 0
  66. if(srcFormat==PIX_FMT_RGB24 || srcFormat==PIX_FMT_BGR24)
  67. srcStride[i]= srcW*3;
  68. else
  69. srcStride[i]= srcW*4;
  70. if(dstFormat==PIX_FMT_RGB24 || dstFormat==PIX_FMT_BGR24)
  71. dstStride[i]= dstW*3;
  72. else
  73. dstStride[i]= dstW*4;
  74. src[i]= (uint8_t*) malloc(srcStride[i]*srcH);
  75. dst[i]= (uint8_t*) malloc(dstStride[i]*dstH);
  76. out[i]= (uint8_t*) malloc(refStride[i]*h);
  77. if ((src[i] == NULL) || (dst[i] == NULL) || (out[i] == NULL)) {
  78. perror("Malloc");
  79. goto end;
  80. }
  81. }
  82. srcContext= sws_getContext(w, h, PIX_FMT_YUV420P, srcW, srcH, srcFormat, flags, NULL, NULL, NULL);
  83. dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL, NULL);
  84. outContext= sws_getContext(dstW, dstH, dstFormat, w, h, PIX_FMT_YUV420P, flags, NULL, NULL, NULL);
  85. if(srcContext==NULL ||dstContext==NULL ||outContext==NULL){
  86. printf("Failed allocating swsContext\n");
  87. goto end;
  88. }
  89. // printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
  90. // (int)src[0], (int)src[1], (int)src[2]);
  91. sws_scale(srcContext, ref, refStride, 0, h , src, srcStride);
  92. sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
  93. sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
  94. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  95. asm volatile ("emms\n\t");
  96. #endif
  97. ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  98. ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
  99. ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
  100. if(srcFormat == PIX_FMT_GRAY8 || dstFormat==PIX_FMT_GRAY8) ssdU=ssdV=0; //FIXME check that output is really gray
  101. ssdY/= w*h;
  102. ssdU/= w*h/4;
  103. ssdV/= w*h/4;
  104. if(ssdY>100 || ssdU>100 || ssdV>100){
  105. printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5lld,%5lld,%5lld\n",
  106. sws_format_name(srcFormat), srcW, srcH,
  107. sws_format_name(dstFormat), dstW, dstH,
  108. flags,
  109. ssdY, ssdU, ssdV);
  110. }
  111. end:
  112. sws_freeContext(srcContext);
  113. sws_freeContext(dstContext);
  114. sws_freeContext(outContext);
  115. for(i=0; i<3; i++){
  116. free(src[i]);
  117. free(dst[i]);
  118. free(out[i]);
  119. }
  120. }
  121. void mp_msg( int x, int y, const char *format, ... ){
  122. va_list va;
  123. va_start(va, format);
  124. vfprintf(stderr, format, va);
  125. va_end(va);
  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. int srcFormat, dstFormat, srcFormatIndex, dstFormatIndex;
  132. int srcW, srcH, dstW, dstH;
  133. int flags;
  134. for(srcFormatIndex=0; ;srcFormatIndex++){
  135. srcFormat= testFormat[srcFormatIndex];
  136. if(srcFormat == PIX_FMT_NONE) break;
  137. for(dstFormatIndex=0; ;dstFormatIndex++){
  138. dstFormat= testFormat[dstFormatIndex];
  139. if(dstFormat == PIX_FMT_NONE) break;
  140. // if(!isSupportedOut(dstFormat)) continue;
  141. printf("%s -> %s\n",
  142. sws_format_name(srcFormat),
  143. sws_format_name(dstFormat));
  144. srcW= w;
  145. srcH= h;
  146. for(dstW=w - w/3; dstW<= 4*w/3; dstW+= w/3){
  147. for(dstH=h - h/3; dstH<= 4*h/3; dstH+= h/3){
  148. for(flags=1; flags<33; flags*=2)
  149. doTest(src, stride, w, h, srcFormat, dstFormat,
  150. srcW, srcH, dstW, dstH, flags);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. #define W 96
  157. #define H 96
  158. int main(int argc, char **argv){
  159. uint8_t rgb_data[W*H*4];
  160. uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
  161. int rgb_stride[3]={4*W, 0, 0};
  162. uint8_t data[3][W*H];
  163. uint8_t *src[3]= {data[0], data[1], data[2]};
  164. int stride[3]={W, W, W};
  165. int x, y;
  166. struct SwsContext *sws;
  167. sws= sws_getContext(W/12, H/12, PIX_FMT_RGB32, W, H, PIX_FMT_YUV420P, 2, NULL, NULL, NULL);
  168. for(y=0; y<H; y++){
  169. for(x=0; x<W*4; x++){
  170. rgb_data[ x + y*4*W]= random();
  171. }
  172. }
  173. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  174. sws_rgb2rgb_init(SWS_CPU_CAPS_MMX*0);
  175. #else
  176. sws_rgb2rgb_init(0);
  177. #endif
  178. sws_scale(sws, rgb_src, rgb_stride, 0, H , src, stride);
  179. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  180. asm volatile ("emms\n\t");
  181. #endif
  182. selfTest(src, stride, W, H);
  183. return 123;
  184. }