Applied Roumen Petrov patch to fix crash/memory leak
[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
166void gen_image(int num, int w, int h)
167{
168 const int c = h_cos [teta];
169 const int s = h_sin [teta];
170
171 const int xi = -(w/2) * c;
172 const int yi = (w/2) * s;
173
174 const int xj = -(h/2) * s;
175 const int yj = -(h/2) * c;
176
177 unsigned dep;
178 int i,j;
179
180 int x,y;
181 int xprime = xj;
182 int yprime = yj;
183
184
185 for (j=0;j<h;j++) {
186
6afd3b92 187 x = xprime + xi + FIXP*w/2;
66961636
MN
188 xprime += s;
189
6afd3b92 190 y = yprime + yi + FIXP*h/2;
66961636
MN
191 yprime += c;
192
193 for ( i=0 ; i<w ; i++ ) {
194 x += c;
195 y -= s;
6afd3b92 196 dep = ((x>>16)&255) + (((y>>16)&255)<<8);
66961636
MN
197 put_pixel(i, j, tab_r[dep], tab_g[dep], tab_b[dep]);
198 }
199 }
200 teta = (teta+1) % 360;
201}
202
6afd3b92
MN
203#define W 256
204#define H 256
205
66961636
MN
206void init_demo() {
207 int i,j;
6afd3b92
MN
208 int h;
209 int radian;
210 char line[3 * W];
66961636
MN
211
212 FILE *fichier;
213
6afd3b92 214 fichier = fopen("lena.pnm","r");
66961636 215 fread(line, 1, 15, fichier);
6afd3b92
MN
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];
66961636 222 }
66961636
MN
223 }
224 fclose(fichier);
225
66961636
MN
226 /* tables sin/cos */
227 for (i=0;i<360;i++) {
6afd3b92
MN
228 radian = 2*i*MY_PI/360;
229 h = 2*FIXP + int_sin (radian);
230 h_cos[i] = ( h * int_sin (radian + MY_PI/2) )/2/FIXP;
231 h_sin[i] = ( h * int_sin (radian ) )/2/FIXP;
66961636
MN
232 }
233}
234
235int main(int argc, char **argv)
236{
237 int w, h, i;
238 char buf[1024];
239
240 if (argc != 2) {
241 printf("usage: %s directory/\n"
242 "generate a test video stream\n", argv[0]);
243 exit(1);
244 }
245
246 w = DEFAULT_WIDTH;
247 h = DEFAULT_HEIGHT;
248
249 rgb_tab = malloc(w * h * 3);
250 wrap = w * 3;
251 width = w;
252 height = h;
253
254 init_demo();
255
256 for(i=0;i<DEFAULT_NB_PICT;i++) {
6afd3b92 257 snprintf(buf, sizeof(buf), "%s%d.pgm", argv[1], i);
66961636
MN
258 gen_image(i, w, h);
259 pgmyuv_save(buf, w, h, rgb_tab);
260 }
261
262 free(rgb_tab);
263 return 0;
264}