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.

215 lines
5.7KB

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