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.

217 lines
5.6KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <inttypes.h>
  19. #include <stdarg.h>
  20. #include "../config.h"
  21. #include "swscale.h"
  22. #include "../libvo/img_format.h"
  23. static int testFormat[]={
  24. IMGFMT_YVU9,
  25. IMGFMT_YV12,
  26. //IMGFMT_IYUV,
  27. IMGFMT_I420,
  28. IMGFMT_BGR15,
  29. IMGFMT_BGR16,
  30. IMGFMT_BGR24,
  31. IMGFMT_BGR32,
  32. IMGFMT_RGB24,
  33. IMGFMT_RGB32,
  34. //IMGFMT_Y8,
  35. IMGFMT_Y800,
  36. //IMGFMT_YUY2,
  37. 0
  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==IMGFMT_RGB24 || srcFormat==IMGFMT_BGR24)
  67. srcStride[i]= srcW*3;
  68. else
  69. srcStride[i]= srcW*4;
  70. if(dstFormat==IMGFMT_RGB24 || dstFormat==IMGFMT_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. }
  78. srcContext= sws_getContext(w, h, IMGFMT_YV12, srcW, srcH, srcFormat, flags, NULL, NULL);
  79. dstContext= sws_getContext(srcW, srcH, srcFormat, dstW, dstH, dstFormat, flags, NULL, NULL);
  80. outContext= sws_getContext(dstW, dstH, dstFormat, w, h, IMGFMT_YV12, flags, NULL, NULL);
  81. if(srcContext==NULL ||dstContext==NULL ||outContext==NULL){
  82. printf("Failed allocating swsContext\n");
  83. goto end;
  84. }
  85. // printf("test %X %X %X -> %X %X %X\n", (int)ref[0], (int)ref[1], (int)ref[2],
  86. // (int)src[0], (int)src[1], (int)src[2]);
  87. sws_scale(srcContext, ref, refStride, 0, h , src, srcStride);
  88. sws_scale(dstContext, src, srcStride, 0, srcH, dst, dstStride);
  89. sws_scale(outContext, dst, dstStride, 0, dstH, out, refStride);
  90. #ifdef ARCH_X86
  91. asm volatile ("emms\n\t");
  92. #endif
  93. ssdY= getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
  94. ssdU= getSSD(ref[1], out[1], refStride[1], refStride[1], (w+1)>>1, (h+1)>>1);
  95. ssdV= getSSD(ref[2], out[2], refStride[2], refStride[2], (w+1)>>1, (h+1)>>1);
  96. if(srcFormat == IMGFMT_Y800 || dstFormat==IMGFMT_Y800) ssdU=ssdV=0; //FIXME check that output is really gray
  97. ssdY/= w*h;
  98. ssdU/= w*h/4;
  99. ssdV/= w*h/4;
  100. if(ssdY>100 || ssdU>100 || ssdV>100){
  101. printf(" %s %dx%d -> %s %4dx%4d flags=%2d SSD=%5lld,%5lld,%5lld\n",
  102. vo_format_name(srcFormat), srcW, srcH,
  103. vo_format_name(dstFormat), dstW, dstH,
  104. flags,
  105. ssdY, ssdU, ssdV);
  106. }
  107. end:
  108. sws_freeContext(srcContext);
  109. sws_freeContext(dstContext);
  110. sws_freeContext(outContext);
  111. for(i=0; i<3; i++){
  112. free(src[i]);
  113. free(dst[i]);
  114. free(out[i]);
  115. }
  116. }
  117. void mp_msg_c( int x, const char *format, ... ){
  118. va_list va;
  119. va_start(va, format);
  120. vfprintf(stderr, format, va);
  121. va_end(va);
  122. }
  123. void fast_memcpy(void *a, void *b, int s){ //FIXME
  124. memcpy(a, b, s);
  125. }
  126. static void selfTest(uint8_t *src[3], int stride[3], int w, int h){
  127. int srcFormat, dstFormat, srcFormatIndex, dstFormatIndex;
  128. int srcW, srcH, dstW, dstH;
  129. int flags;
  130. for(srcFormatIndex=0; ;srcFormatIndex++){
  131. srcFormat= testFormat[srcFormatIndex];
  132. if(!srcFormat) break;
  133. for(dstFormatIndex=0; ;dstFormatIndex++){
  134. dstFormat= testFormat[dstFormatIndex];
  135. if(!dstFormat) break;
  136. // if(!isSupportedOut(dstFormat)) continue;
  137. printf("%s -> %s\n",
  138. vo_format_name(srcFormat),
  139. vo_format_name(dstFormat));
  140. srcW= w;
  141. srcH= h;
  142. for(dstW=w - w/3; dstW<= 4*w/3; dstW+= w/3){
  143. for(dstH=h - h/3; dstH<= 4*h/3; dstH+= h/3){
  144. for(flags=1; flags<33; flags*=2)
  145. doTest(src, stride, w, h, srcFormat, dstFormat,
  146. srcW, srcH, dstW, dstH, flags);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. #define W 96
  153. #define H 96
  154. int main(int argc, char **argv){
  155. uint8_t rgb_data[W*H*4];
  156. uint8_t *rgb_src[3]= {rgb_data, NULL, NULL};
  157. int rgb_stride[3]={4*W, 0, 0};
  158. uint8_t data[3][W*H];
  159. uint8_t *src[3]= {data[0], data[1], data[2]};
  160. int stride[3]={W, W, W};
  161. int x, y;
  162. struct SwsContext *sws;
  163. sws= sws_getContext(W/12, H/12, IMGFMT_BGR32, W, H, IMGFMT_YV12, 2, NULL, NULL);
  164. for(y=0; y<H; y++){
  165. for(x=0; x<W*4; x++){
  166. rgb_data[ x + y*4*W]= random();
  167. }
  168. }
  169. #ifdef ARCH_X86
  170. sws_rgb2rgb_init(SWS_CPU_CAPS_MMX*0);
  171. #else
  172. sws_rgb2rgb_init(0);
  173. #endif
  174. sws_scale(sws, rgb_src, rgb_stride, 0, H , src, stride);
  175. #ifdef ARCH_X86
  176. asm volatile ("emms\n\t");
  177. #endif
  178. selfTest(src, stride, W, H);
  179. return 123;
  180. }