rotozoom.c 7.6 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. {
  29. int64_t v = FIXP;
  30. for (; p; p--) {
  31. v *= a;
  32. v /= FIXP;
  33. }
  34. return v;
  35. }
  36. static int64_t int_sin(int64_t a)
  37. {
  38. if (a < 0)
  39. a = MY_PI - a; // 0..inf
  40. a %= 2 * MY_PI; // 0..2PI
  41. if (a >= MY_PI * 3 / 2)
  42. a -= 2 * MY_PI; // -PI / 2 .. 3PI / 2
  43. if (a >= MY_PI /2)
  44. a = MY_PI - a; // -PI / 2 .. PI / 2
  45. return a - int_pow(a, 3) / 6 + int_pow(a, 5) / 120 - int_pow(a, 7) / 5040;
  46. }
  47. #define SCALEBITS 8
  48. #define ONE_HALF (1 << (SCALEBITS - 1))
  49. #define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
  50. static void rgb24_to_yuv420p(unsigned char *lum, unsigned char *cb,
  51. unsigned char *cr, unsigned char *src,
  52. int width, int height)
  53. {
  54. int wrap, wrap3, x, y;
  55. int r, g, b, r1, g1, b1;
  56. unsigned char *p;
  57. wrap = width;
  58. wrap3 = width * 3;
  59. p = src;
  60. for (y = 0; y < height; y += 2) {
  61. for (x = 0; x < width; x += 2) {
  62. r = p[0];
  63. g = p[1];
  64. b = p[2];
  65. r1 = r;
  66. g1 = g;
  67. b1 = b;
  68. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  69. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  70. r = p[3];
  71. g = p[4];
  72. b = p[5];
  73. r1 += r;
  74. g1 += g;
  75. b1 += b;
  76. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  77. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  78. p += wrap3;
  79. lum += wrap;
  80. r = p[0];
  81. g = p[1];
  82. b = p[2];
  83. r1 += r;
  84. g1 += g;
  85. b1 += b;
  86. lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
  87. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  88. r = p[3];
  89. g = p[4];
  90. b = p[5];
  91. r1 += r;
  92. g1 += g;
  93. b1 += b;
  94. lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
  95. FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
  96. cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
  97. FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  98. cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
  99. FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
  100. cb++;
  101. cr++;
  102. p += -wrap3 + 2 * 3;
  103. lum += -wrap + 2;
  104. }
  105. p += wrap3;
  106. lum += wrap;
  107. }
  108. }
  109. /* cif format */
  110. #define DEFAULT_WIDTH 352
  111. #define DEFAULT_HEIGHT 288
  112. #define DEFAULT_NB_PICT 50
  113. static void pgmyuv_save(const char *filename, int w, int h,
  114. unsigned char *rgb_tab)
  115. {
  116. FILE *f;
  117. int i, h2, w2;
  118. unsigned char *cb, *cr;
  119. unsigned char *lum_tab, *cb_tab, *cr_tab;
  120. lum_tab = malloc(w * h);
  121. cb_tab = malloc(w * h / 4);
  122. cr_tab = malloc(w * h / 4);
  123. rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
  124. f = fopen(filename, "wb");
  125. fprintf(f, "P5\n%d %d\n%d\n", w, h * 3 / 2, 255);
  126. fwrite(lum_tab, 1, w * h, f);
  127. h2 = h / 2;
  128. w2 = w / 2;
  129. cb = cb_tab;
  130. cr = cr_tab;
  131. for (i = 0; i < h2; i++) {
  132. fwrite(cb, 1, w2, f);
  133. fwrite(cr, 1, w2, f);
  134. cb += w2;
  135. cr += w2;
  136. }
  137. fclose(f);
  138. free(lum_tab);
  139. free(cb_tab);
  140. free(cr_tab);
  141. }
  142. unsigned char *rgb_tab;
  143. int width, height, wrap;
  144. static void put_pixel(int x, int y, int r, int g, int b)
  145. {
  146. unsigned char *p;
  147. if (x < 0 || x >= width ||
  148. y < 0 || y >= height)
  149. return;
  150. p = rgb_tab + y * wrap + x * 3;
  151. p[0] = r;
  152. p[1] = g;
  153. p[2] = b;
  154. }
  155. unsigned char tab_r[256 * 256];
  156. unsigned char tab_g[256 * 256];
  157. unsigned char tab_b[256 * 256];
  158. int h_cos [360];
  159. int h_sin [360];
  160. static int ipol(uint8_t *src, int x, int y)
  161. {
  162. int int_x = x >> 16;
  163. int int_y = y >> 16;
  164. int frac_x = x & 0xFFFF;
  165. int frac_y = y & 0xFFFF;
  166. int s00 = src[( int_x & 255) + 256 * ( int_y & 255)];
  167. int s01 = src[((int_x + 1) & 255) + 256 * ( int_y & 255)];
  168. int s10 = src[( int_x & 255) + 256 * ((int_y + 1) & 255)];
  169. int s11 = src[((int_x + 1) & 255) + 256 * ((int_y + 1) & 255)];
  170. int s0 = (((1 << 16) - frac_x) * s00 + frac_x * s01) >> 8;
  171. int s1 = (((1 << 16) - frac_x) * s10 + frac_x * s11) >> 8;
  172. return (((1 << 16) - frac_y) * s0 + frac_y * s1) >> 24;
  173. }
  174. static void gen_image(int num, int w, int h)
  175. {
  176. const int c = h_cos [num % 360];
  177. const int s = h_sin [num % 360];
  178. const int xi = -(w / 2) * c;
  179. const int yi = (w / 2) * s;
  180. const int xj = -(h / 2) * s;
  181. const int yj = -(h / 2) * c;
  182. int i, j;
  183. int x, y;
  184. int xprime = xj;
  185. int yprime = yj;
  186. for (j = 0; j < h; j++) {
  187. x = xprime + xi + FIXP * w / 2;
  188. xprime += s;
  189. y = yprime + yi + FIXP * h / 2;
  190. yprime += c;
  191. for (i = 0; i < w; i++ ) {
  192. x += c;
  193. y -= s;
  194. put_pixel(i, j, ipol(tab_r, x, y), ipol(tab_g, x, y), ipol(tab_b, x, y));
  195. }
  196. }
  197. }
  198. #define W 256
  199. #define H 256
  200. static int init_demo(const char *filename)
  201. {
  202. int i, j;
  203. int h;
  204. int radian;
  205. char line[3 * W];
  206. FILE *input_file;
  207. input_file = fopen(filename, "rb");
  208. if (!input_file) {
  209. perror(filename);
  210. return 1;
  211. }
  212. if (fread(line, 1, 15, input_file) != 15)
  213. return 1;
  214. for (i = 0; i < H; i++) {
  215. if (fread(line, 1, 3 * W, input_file) != 3 * W)
  216. return 1;
  217. for (j = 0; j < W; j++) {
  218. tab_r[W * i + j] = line[3 * j ];
  219. tab_g[W * i + j] = line[3 * j + 1];
  220. tab_b[W * i + j] = line[3 * j + 2];
  221. }
  222. }
  223. fclose(input_file);
  224. /* tables sin/cos */
  225. for (i = 0; i < 360; i++) {
  226. radian = 2 * i * MY_PI / 360;
  227. h = 2 * FIXP + int_sin (radian);
  228. h_cos[i] = h * int_sin(radian + MY_PI / 2) / 2 / FIXP;
  229. h_sin[i] = h * int_sin(radian) / 2 / FIXP;
  230. }
  231. return 0;
  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. return 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. if (init_demo(argv[2]))
  249. return 1;
  250. for (i = 0; i < DEFAULT_NB_PICT; i++) {
  251. snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
  252. gen_image(i, w, h);
  253. pgmyuv_save(buf, w, h, rgb_tab);
  254. }
  255. free(rgb_tab);
  256. return 0;
  257. }