]> git.sesse.net Git - ffmpeg/blob - tests/rotozoom.c
Add long-term prediction to the ALS decoder.
[ffmpeg] / tests / rotozoom.c
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
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <inttypes.h>
26
27 #define FIXP (1<<16)
28 #define MY_PI 205887 //(M_PI*FIX)
29
30 static 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
41 static 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
47
48     return a - int_pow(a, 3)/6 + int_pow(a, 5)/120 - int_pow(a, 7)/5040;
49 }
50
51 #define SCALEBITS 8
52 #define ONE_HALF  (1 << (SCALEBITS - 1))
53 #define FIX(x)    ((int) ((x) * (1L<<SCALEBITS) + 0.5))
54 typedef unsigned char UINT8;
55
56 static 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;
74             lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
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;
82             lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
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;
93             lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
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;
101             lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
102                       FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
103
104             cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
105                       FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
106             cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
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
122 #define DEFAULT_NB_PICT 50
123
124 static void pgmyuv_save(const char *filename, int w, int h,
125                         unsigned char *rgb_tab)
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
138     f = fopen(filename,"wb");
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
158 unsigned char *rgb_tab;
159 int width, height, wrap;
160
161 static void put_pixel(int x, int y, int r, int g, int b)
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
175 unsigned char tab_r[256*256];
176 unsigned char tab_g[256*256];
177 unsigned char tab_b[256*256];
178
179 int h_cos [360];
180 int h_sin [360];
181
182 static 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;
193
194     return (((1<<16) - frac_y)*s0 + frac_y*s1)>>24;
195 }
196
197 static void gen_image(int num, int w, int h)
198 {
199   const int c = h_cos [num % 360];
200   const int s = h_sin [num % 360];
201
202   const int xi = -(w/2) * c;
203   const int yi =  (w/2) * s;
204
205   const int xj = -(h/2) * s;
206   const int yj = -(h/2) * c;
207   int i,j;
208
209   int x,y;
210   int xprime = xj;
211   int yprime = yj;
212
213
214   for (j=0;j<h;j++) {
215
216     x = xprime + xi + FIXP*w/2;
217     xprime += s;
218
219     y = yprime + yi + FIXP*h/2;
220     yprime += c;
221
222     for ( i=0 ; i<w ; i++ ) {
223       x += c;
224       y -= s;
225 #if 1
226       put_pixel(i, j, ipol(tab_r, x, y), ipol(tab_g, x, y), ipol(tab_b, x, y));
227 #else
228       {
229           unsigned dep;
230           dep = ((x>>16)&255) + (((y>>16)&255)<<8);
231           put_pixel(i, j, tab_r[dep], tab_g[dep], tab_b[dep]);
232       }
233 #endif
234     }
235   }
236 }
237
238 #define W 256
239 #define H 256
240
241 static void init_demo(const char *filename) {
242   int i,j;
243   int h;
244   int radian;
245   char line[3 * W];
246
247   FILE *fichier;
248
249   fichier = fopen(filename,"rb");
250   if (!fichier) {
251       perror(filename);
252       exit(1);
253   }
254
255   fread(line, 1, 15, fichier);
256   for (i=0;i<H;i++) {
257     fread(line,1,3*W,fichier);
258     for (j=0;j<W;j++) {
259           tab_r[W*i+j] = line[3*j    ];
260           tab_g[W*i+j] = line[3*j + 1];
261           tab_b[W*i+j] = line[3*j + 2];
262     }
263   }
264   fclose(fichier);
265
266   /* tables sin/cos */
267   for (i=0;i<360;i++) {
268     radian = 2*i*MY_PI/360;
269     h = 2*FIXP + int_sin (radian);
270     h_cos[i] = ( h * int_sin (radian + MY_PI/2) )/2/FIXP;
271     h_sin[i] = ( h * int_sin (radian          ) )/2/FIXP;
272   }
273 }
274
275 int main(int argc, char **argv)
276 {
277     int w, h, i;
278     char buf[1024];
279
280     if (argc != 3) {
281         printf("usage: %s directory/ image.pnm\n"
282                "generate a test video stream\n", argv[0]);
283         exit(1);
284     }
285
286     w = DEFAULT_WIDTH;
287     h = DEFAULT_HEIGHT;
288
289     rgb_tab = malloc(w * h * 3);
290     wrap = w * 3;
291     width = w;
292     height = h;
293
294     init_demo(argv[2]);
295
296     for(i=0;i<DEFAULT_NB_PICT;i++) {
297         snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
298         gen_image(i, w, h);
299         pgmyuv_save(buf, w, h, rgb_tab);
300     }
301
302     free(rgb_tab);
303     return 0;
304 }