fixed SRC_PATH usage
[libav.git] / tests / rotozoom.c
CommitLineData
66961636
MN
1/*
2 * Generates a synthetic YUV video sequence suitable for codec testing.
3 * GPLv2
4 * rotozoom.c -> s.bechet@av7.net
5 */
6#include <stdlib.h>
7#include <stdio.h>
6afd3b92
MN
8#include <inttypes.h>
9
10#define FIXP (1<<16)
11#define MY_PI 205887 //(M_PI*FIX)
12
13static int64_t int_pow(int64_t a, int p){
14 int64_t v= FIXP;
15
16 for(; p; p--){
17 v*= a;
18 v/= FIXP;
19 }
20
21 return v;
22}
23
24static int64_t int_sin(int64_t a){
25 if(a<0) a= MY_PI-a; // 0..inf
26 a %= 2*MY_PI; // 0..2PI
27
28 if(a>=MY_PI*3/2) a -= 2*MY_PI; // -PI/2 .. 3PI/2
29 if(a>=MY_PI/2 ) a = MY_PI - a; // -PI/2 .. PI/2
30
31 return a - int_pow(a, 3)/6 + int_pow(a, 5)/120 - int_pow(a, 7)/5040;
32}
66961636
MN
33
34#define SCALEBITS 8
35#define ONE_HALF (1 << (SCALEBITS - 1))
36#define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
37typedef unsigned char UINT8;
38
39static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
40 UINT8 *src, int width, int height)
41{
42 int wrap, wrap3, x, y;
43 int r, g, b, r1, g1, b1;
44 UINT8 *p;
45
46 wrap = width;
47 wrap3 = width * 3;
48 p = src;
49 for(y=0;y<height;y+=2) {
50 for(x=0;x<width;x+=2) {
51 r = p[0];
52 g = p[1];
53 b = p[2];
54 r1 = r;
55 g1 = g;
56 b1 = b;
57 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
58 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
59 r = p[3];
60 g = p[4];
61 b = p[5];
62 r1 += r;
63 g1 += g;
64 b1 += b;
65 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
66 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
67 p += wrap3;
68 lum += wrap;
69
70 r = p[0];
71 g = p[1];
72 b = p[2];
73 r1 += r;
74 g1 += g;
75 b1 += b;
76 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
77 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
78 r = p[3];
79 g = p[4];
80 b = p[5];
81 r1 += r;
82 g1 += g;
83 b1 += b;
84 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
85 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
86
87 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
88 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
89 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
90 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
91
92 cb++;
93 cr++;
94 p += -wrap3 + 2 * 3;
95 lum += -wrap + 2;
96 }
97 p += wrap3;
98 lum += wrap;
99 }
100}
101
102/* cif format */
103#define DEFAULT_WIDTH 352
104#define DEFAULT_HEIGHT 288
6afd3b92 105#define DEFAULT_NB_PICT 50
66961636
MN
106
107void pgmyuv_save(const char *filename, int w, int h,
108 unsigned char *rgb_tab)
109{
110 FILE *f;
111 int i, h2, w2;
112 unsigned char *cb, *cr;
113 unsigned char *lum_tab, *cb_tab, *cr_tab;
114
115 lum_tab = malloc(w * h);
116 cb_tab = malloc((w * h) / 4);
117 cr_tab = malloc((w * h) / 4);
118
119 rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
120
121 f = fopen(filename,"w");
122 fprintf(f, "P5\n%d %d\n%d\n", w, (h * 3) / 2, 255);
123 fwrite(lum_tab, 1, w * h, f);
124 h2 = h / 2;
125 w2 = w / 2;
126 cb = cb_tab;
127 cr = cr_tab;
128 for(i=0;i<h2;i++) {
129 fwrite(cb, 1, w2, f);
130 fwrite(cr, 1, w2, f);
131 cb += w2;
132 cr += w2;
133 }
134 fclose(f);
135
136 free(lum_tab);
137 free(cb_tab);
138 free(cr_tab);
139}
140
141unsigned char *rgb_tab;
142int width, height, wrap;
143
144void put_pixel(int x, int y, int r, int g, int b)
145{
146 unsigned char *p;
147
148 if (x < 0 || x >= width ||
149 y < 0 || y >= height)
150 return;
151
152 p = rgb_tab + y * wrap + x * 3;
153 p[0] = r;
154 p[1] = g;
155 p[2] = b;
156}
157
158unsigned char tab_r[256*256];
159unsigned char tab_g[256*256];
160unsigned char tab_b[256*256];
161
162int teta = 0;
163int h_cos [360];
164int h_sin [360];
165
2add6b5d
MN
166static int ipol(uint8_t *src, int x, int y){
167 int int_x= x>>16;
168 int int_y= y>>16;
169 int frac_x= x&0xFFFF;
170 int frac_y= y&0xFFFF;
171 int s00= src[ ( int_x &255) + 256*( int_y &255) ];
172 int s01= src[ ((int_x+1)&255) + 256*( int_y &255) ];
173 int s10= src[ ( int_x &255) + 256*((int_y+1)&255) ];
174 int s11= src[ ((int_x+1)&255) + 256*((int_y+1)&255) ];
175 int s0= (((1<<16) - frac_x)*s00 + frac_x*s01)>>8;
176 int s1= (((1<<16) - frac_x)*s10 + frac_x*s11)>>8;
177
178 return (((1<<16) - frac_y)*s0 + frac_y*s1)>>24;
179}
180
66961636
MN
181void gen_image(int num, int w, int h)
182{
183 const int c = h_cos [teta];
184 const int s = h_sin [teta];
185
186 const int xi = -(w/2) * c;
187 const int yi = (w/2) * s;
188
189 const int xj = -(h/2) * s;
190 const int yj = -(h/2) * c;
191
192 unsigned dep;
193 int i,j;
194
195 int x,y;
196 int xprime = xj;
197 int yprime = yj;
198
199
200 for (j=0;j<h;j++) {
201
6afd3b92 202 x = xprime + xi + FIXP*w/2;
66961636
MN
203 xprime += s;
204
6afd3b92 205 y = yprime + yi + FIXP*h/2;
66961636
MN
206 yprime += c;
207
208 for ( i=0 ; i<w ; i++ ) {
209 x += c;
210 y -= s;
2add6b5d
MN
211#if 1
212 put_pixel(i, j, ipol(tab_r, x, y), ipol(tab_g, x, y), ipol(tab_b, x, y));
213#else
6afd3b92 214 dep = ((x>>16)&255) + (((y>>16)&255)<<8);
66961636 215 put_pixel(i, j, tab_r[dep], tab_g[dep], tab_b[dep]);
2add6b5d 216#endif
66961636
MN
217 }
218 }
219 teta = (teta+1) % 360;
220}
221
6afd3b92
MN
222#define W 256
223#define H 256
224
66961636
MN
225void init_demo() {
226 int i,j;
6afd3b92
MN
227 int h;
228 int radian;
229 char line[3 * W];
66961636
MN
230
231 FILE *fichier;
232
6afd3b92 233 fichier = fopen("lena.pnm","r");
66961636 234 fread(line, 1, 15, fichier);
6afd3b92
MN
235 for (i=0;i<H;i++) {
236 fread(line,1,3*W,fichier);
237 for (j=0;j<W;j++) {
238 tab_r[W*i+j] = line[3*j ];
239 tab_g[W*i+j] = line[3*j + 1];
240 tab_b[W*i+j] = line[3*j + 2];
66961636 241 }
66961636
MN
242 }
243 fclose(fichier);
244
66961636
MN
245 /* tables sin/cos */
246 for (i=0;i<360;i++) {
6afd3b92
MN
247 radian = 2*i*MY_PI/360;
248 h = 2*FIXP + int_sin (radian);
249 h_cos[i] = ( h * int_sin (radian + MY_PI/2) )/2/FIXP;
250 h_sin[i] = ( h * int_sin (radian ) )/2/FIXP;
66961636
MN
251 }
252}
253
254int main(int argc, char **argv)
255{
256 int w, h, i;
257 char buf[1024];
258
259 if (argc != 2) {
260 printf("usage: %s directory/\n"
261 "generate a test video stream\n", argv[0]);
262 exit(1);
263 }
264
265 w = DEFAULT_WIDTH;
266 h = DEFAULT_HEIGHT;
267
268 rgb_tab = malloc(w * h * 3);
269 wrap = w * 3;
270 width = w;
271 height = h;
272
273 init_demo();
274
275 for(i=0;i<DEFAULT_NB_PICT;i++) {
6afd3b92 276 snprintf(buf, sizeof(buf), "%s%d.pgm", argv[1], i);
66961636
MN
277 gen_image(i, w, h);
278 pgmyuv_save(buf, w, h, rgb_tab);
279 }
280
281 free(rgb_tab);
282 return 0;
283}