rotozoom.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * Generates a synthetic YUV video sequence suitable for codec testing.
  3. *
  4. * copyright (c) Sebastien Bechet <s.bechet@av7.net>
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <inttypes.h>
  25. #define FIXP (1<<16)
  26. #define MY_PI 205887 //(M_PI*FIX)
  27. static int64_t int_pow(int64_t a, int p){
  28. int64_t v= FIXP;
  29. for(; p; p--){
  30. v*= a;
  31. v/= FIXP;
  32. }
  33. return v;
  34. }
  35. static int64_t int_sin(int64_t a){
  36. if(a<0) a= MY_PI-a; // 0..inf
  37. a %= 2*MY_PI; // 0..2PI
  38. if(a>=MY_PI*3/2) a -= 2*MY_PI; // -PI/2 .. 3PI/2
  39. if(a>=MY_PI/2 ) a = MY_PI - a; // -PI/2 .. PI/2
  40. return a - int_pow(a, 3)/6 + int_pow(a, 5)/120 - int_pow(a, 7)/5040;
  41. }
  42. #define SCALEBITS 8
  43. #define ONE_HALF (1 << (SCALEBITS - 1))
  44. #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
  45. typedef unsigned char UINT8;
  46. static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
  47. UINT8 *src, int width, int height)
  48. {
  49. int wrap, wrap3, x, y;
  50. int r, g, b, r1, g1, b1;
  51. UINT8 *p;
  52. wrap = width;
  53. wrap3 = width * 3;
  54. p = src;
  55. for(y=0;y<height;y+=2) {
  56. for(x=0;x<width;x+=2) {
  57. r = p[0];
  58. g = p[1];
  59. b = p[2];
  60. r1 = r;
  61. g1 = g;
  62. b1 = b;
  63. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  64. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  65. r = p[3];
  66. g = p[4];
  67. b = p[5];
  68. r1 += r;
  69. g1 += g;
  70. b1 += b;
  71. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  72. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  73. p += wrap3;
  74. lum += wrap;
  75. r = p[0];
  76. g = p[1];
  77. b = p[2];
  78. r1 += r;
  79. g1 += g;
  80. b1 += b;
  81. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  82. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  83. r = p[3];
  84. g = p[4];
  85. b = p[5];
  86. r1 += r;
  87. g1 += g;
  88. b1 += b;
  89. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  90. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  91. cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
  92. FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  93. cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
  94. FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  95. cb++;
  96. cr++;
  97. p += -wrap3 + 2 * 3;
  98. lum += -wrap + 2;
  99. }
  100. p += wrap3;
  101. lum += wrap;
  102. }
  103. }
  104. /* cif format */
  105. #define DEFAULT_WIDTH 352
  106. #define DEFAULT_HEIGHT 288
  107. #define DEFAULT_NB_PICT 50
  108. static void pgmyuv_save(const char *filename, int w, int h,
  109. unsigned char *rgb_tab)
  110. {
  111. FILE *f;
  112. int i, h2, w2;
  113. unsigned char *cb, *cr;
  114. unsigned char *lum_tab, *cb_tab, *cr_tab;
  115. lum_tab = malloc(w * h);
  116. cb_tab = malloc((w * h) / 4);
  117. cr_tab = malloc((w * h) / 4);
  118. rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
  119. f = fopen(filename,"wb");
  120. fprintf(f, "P5\n%d %d\n%d\n", w, (h * 3) / 2, 255);
  121. fwrite(lum_tab, 1, w * h, f);
  122. h2 = h / 2;
  123. w2 = w / 2;
  124. cb = cb_tab;
  125. cr = cr_tab;
  126. for(i=0;i<h2;i++) {
  127. fwrite(cb, 1, w2, f);
  128. fwrite(cr, 1, w2, f);
  129. cb += w2;
  130. cr += w2;
  131. }
  132. fclose(f);
  133. free(lum_tab);
  134. free(cb_tab);
  135. free(cr_tab);
  136. }
  137. unsigned char *rgb_tab;
  138. int width, height, wrap;
  139. static void put_pixel(int x, int y, int r, int g, int b)
  140. {
  141. unsigned char *p;
  142. if (x < 0 || x >= width ||
  143. y < 0 || y >= height)
  144. return;
  145. p = rgb_tab + y * wrap + x * 3;
  146. p[0] = r;
  147. p[1] = g;
  148. p[2] = b;
  149. }
  150. unsigned char tab_r[256*256];
  151. unsigned char tab_g[256*256];
  152. unsigned char tab_b[256*256];
  153. int teta = 0;
  154. int h_cos [360];
  155. int h_sin [360];
  156. static int ipol(uint8_t *src, int x, int y){
  157. int int_x= x>>16;
  158. int int_y= y>>16;
  159. int frac_x= x&0xFFFF;
  160. int frac_y= y&0xFFFF;
  161. int s00= src[ ( int_x &255) + 256*( int_y &255) ];
  162. int s01= src[ ((int_x+1)&255) + 256*( int_y &255) ];
  163. int s10= src[ ( int_x &255) + 256*((int_y+1)&255) ];
  164. int s11= src[ ((int_x+1)&255) + 256*((int_y+1)&255) ];
  165. int s0= (((1<<16) - frac_x)*s00 + frac_x*s01)>>8;
  166. int s1= (((1<<16) - frac_x)*s10 + frac_x*s11)>>8;
  167. return (((1<<16) - frac_y)*s0 + frac_y*s1)>>24;
  168. }
  169. static void gen_image(int num, int w, int h)
  170. {
  171. const int c = h_cos [teta];
  172. const int s = h_sin [teta];
  173. const int xi = -(w/2) * c;
  174. const int yi = (w/2) * s;
  175. const int xj = -(h/2) * s;
  176. const int yj = -(h/2) * c;
  177. int i,j;
  178. int x,y;
  179. int xprime = xj;
  180. int yprime = yj;
  181. for (j=0;j<h;j++) {
  182. x = xprime + xi + FIXP*w/2;
  183. xprime += s;
  184. y = yprime + yi + FIXP*h/2;
  185. yprime += c;
  186. for ( i=0 ; i<w ; i++ ) {
  187. x += c;
  188. y -= s;
  189. #if 1
  190. put_pixel(i, j, ipol(tab_r, x, y), ipol(tab_g, x, y), ipol(tab_b, x, y));
  191. #else
  192. {
  193. unsigned dep;
  194. dep = ((x>>16)&255) + (((y>>16)&255)<<8);
  195. put_pixel(i, j, tab_r[dep], tab_g[dep], tab_b[dep]);
  196. }
  197. #endif
  198. }
  199. }
  200. teta = (teta+1) % 360;
  201. }
  202. #define W 256
  203. #define H 256
  204. static void init_demo(const char *filename) {
  205. int i,j;
  206. int h;
  207. int radian;
  208. char line[3 * W];
  209. FILE *fichier;
  210. fichier = fopen(filename,"rb");
  211. if (!fichier) {
  212. perror(filename);
  213. exit(1);
  214. }
  215. fread(line, 1, 15, fichier);
  216. for (i=0;i<H;i++) {
  217. fread(line,1,3*W,fichier);
  218. for (j=0;j<W;j++) {
  219. tab_r[W*i+j] = line[3*j ];
  220. tab_g[W*i+j] = line[3*j + 1];
  221. tab_b[W*i+j] = line[3*j + 2];
  222. }
  223. }
  224. fclose(fichier);
  225. /* tables sin/cos */
  226. for (i=0;i<360;i++) {
  227. radian = 2*i*MY_PI/360;
  228. h = 2*FIXP + int_sin (radian);
  229. h_cos[i] = ( h * int_sin (radian + MY_PI/2) )/2/FIXP;
  230. h_sin[i] = ( h * int_sin (radian ) )/2/FIXP;
  231. }
  232. }
  233. int main(int argc, char **argv)
  234. {
  235. int w, h, i;
  236. char buf[1024];
  237. if (argc != 3) {
  238. printf("usage: %s directory/ image.pnm\n"
  239. "generate a test video stream\n", argv[0]);
  240. exit(1);
  241. }
  242. w = DEFAULT_WIDTH;
  243. h = DEFAULT_HEIGHT;
  244. rgb_tab = malloc(w * h * 3);
  245. wrap = w * 3;
  246. width = w;
  247. height = h;
  248. init_demo(argv[2]);
  249. for(i=0;i<DEFAULT_NB_PICT;i++) {
  250. snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
  251. gen_image(i, w, h);
  252. pgmyuv_save(buf, w, h, rgb_tab);
  253. }
  254. free(rgb_tab);
  255. return 0;
  256. }