Statistics
| Branch: | Revision:

ffmpeg / tests / rotozoom.c @ 47805d21

History | View | Annotate | Download (6.41 KB)

1 66961636 Michael Niedermayer
/*
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>
8 6afd3b92 Michael Niedermayer
#include <inttypes.h>
9
10
#define FIXP (1<<16)
11
#define MY_PI 205887 //(M_PI*FIX)
12
13
static 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
24
static 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
}
33 66961636 Michael Niedermayer
34
#define SCALEBITS 8
35
#define ONE_HALF  (1 << (SCALEBITS - 1))
36
#define FIX(x)                ((int) ((x) * (1L<<SCALEBITS) + 0.5))
37
typedef unsigned char UINT8;
38
39
static 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
105 6afd3b92 Michael Niedermayer
#define DEFAULT_NB_PICT 50
106 66961636 Michael Niedermayer
107
void 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 6feb1de5 Michael Niedermayer
    f = fopen(filename,"wb");
122 66961636 Michael Niedermayer
    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
141
unsigned char *rgb_tab;
142
int width, height, wrap;
143
144
void 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
158
unsigned char tab_r[256*256];
159
unsigned char tab_g[256*256];
160
unsigned char tab_b[256*256];
161
162
int teta = 0;
163
int h_cos [360];
164
int h_sin [360];
165
166 2add6b5d Michael Niedermayer
static 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
181 66961636 Michael Niedermayer
void 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
  int i,j;
192
  
193
  int x,y;
194
  int xprime = xj;
195
  int yprime = yj;
196
197
198
  for (j=0;j<h;j++) {
199
200 6afd3b92 Michael Niedermayer
    x = xprime + xi + FIXP*w/2;
201 66961636 Michael Niedermayer
    xprime += s;
202
203 6afd3b92 Michael Niedermayer
    y = yprime + yi + FIXP*h/2;
204 66961636 Michael Niedermayer
    yprime += c;
205
      
206
    for ( i=0 ; i<w ; i++ ) {
207
      x += c;
208
      y -= s;
209 2add6b5d Michael Niedermayer
#if 1
210
      put_pixel(i, j, ipol(tab_r, x, y), ipol(tab_g, x, y), ipol(tab_b, x, y));
211
#else
212 67f7f316 Fabrice Bellard
      {
213
          unsigned dep;
214
          dep = ((x>>16)&255) + (((y>>16)&255)<<8);
215
          put_pixel(i, j, tab_r[dep], tab_g[dep], tab_b[dep]);
216
      }
217 2add6b5d Michael Niedermayer
#endif
218 66961636 Michael Niedermayer
    }
219
  }
220
  teta = (teta+1) % 360;
221
}
222
223 6afd3b92 Michael Niedermayer
#define W 256
224
#define H 256
225
226 67f7f316 Fabrice Bellard
void init_demo(const char *filename) {
227 66961636 Michael Niedermayer
  int i,j;
228 6afd3b92 Michael Niedermayer
  int h;
229
  int radian;
230
  char line[3 * W];
231 66961636 Michael Niedermayer
232
  FILE *fichier;
233
234 6feb1de5 Michael Niedermayer
  fichier = fopen(filename,"rb");
235 67f7f316 Fabrice Bellard
  if (!fichier) {
236
      perror(filename);
237
      exit(1);
238
  }
239
      
240 66961636 Michael Niedermayer
  fread(line, 1, 15, fichier);
241 6afd3b92 Michael Niedermayer
  for (i=0;i<H;i++) {
242
    fread(line,1,3*W,fichier);
243
    for (j=0;j<W;j++) {
244
          tab_r[W*i+j] = line[3*j    ];
245
          tab_g[W*i+j] = line[3*j + 1];
246
          tab_b[W*i+j] = line[3*j + 2];
247 66961636 Michael Niedermayer
    }
248
  }
249
  fclose(fichier);
250
251
  /* tables sin/cos */
252
  for (i=0;i<360;i++) {
253 6afd3b92 Michael Niedermayer
    radian = 2*i*MY_PI/360;
254
    h = 2*FIXP + int_sin (radian);
255
    h_cos[i] = ( h * int_sin (radian + MY_PI/2) )/2/FIXP;
256
    h_sin[i] = ( h * int_sin (radian          ) )/2/FIXP;
257 66961636 Michael Niedermayer
  }
258
}
259
260
int main(int argc, char **argv)
261
{
262
    int w, h, i;
263
    char buf[1024];
264
265 67f7f316 Fabrice Bellard
    if (argc != 3) {
266
        printf("usage: %s directory/ image.pnm\n"
267 66961636 Michael Niedermayer
               "generate a test video stream\n", argv[0]);
268
        exit(1);
269
    }
270
271
    w = DEFAULT_WIDTH;
272
    h = DEFAULT_HEIGHT;
273
274
    rgb_tab = malloc(w * h * 3);
275
    wrap = w * 3;
276
    width = w;
277
    height = h;
278
279 67f7f316 Fabrice Bellard
    init_demo(argv[2]);
280 66961636 Michael Niedermayer
281
    for(i=0;i<DEFAULT_NB_PICT;i++) {
282 6afd3b92 Michael Niedermayer
        snprintf(buf, sizeof(buf), "%s%d.pgm", argv[1], i);
283 66961636 Michael Niedermayer
        gen_image(i, w, h);
284
        pgmyuv_save(buf, w, h, rgb_tab);
285
    }
286
    
287
    free(rgb_tab);
288
    return 0;
289
}