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