swscale-example.c 5.9 KB

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