2
* Generates a synthetic YUV video sequence suitable for codec testing.
4
* rotozoom.c -> s.bechet@av7.net
11
#define MY_PI 205887 //(M_PI*FIX)
13
static int64_t int_pow(int64_t a, int p){
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
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
31
return a - int_pow(a, 3)/6 + int_pow(a, 5)/120 - int_pow(a, 7)/5040;
35
#define ONE_HALF (1 << (SCALEBITS - 1))
36
#define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
37
typedef unsigned char UINT8;
39
static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
40
UINT8 *src, int width, int height)
42
int wrap, wrap3, x, y;
43
int r, g, b, r1, g1, b1;
49
for(y=0;y<height;y+=2) {
50
for(x=0;x<width;x+=2) {
57
lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
58
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
65
lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
66
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
76
lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
77
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
84
lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
85
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
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;
103
#define DEFAULT_WIDTH 352
104
#define DEFAULT_HEIGHT 288
105
#define DEFAULT_NB_PICT 50
107
void pgmyuv_save(const char *filename, int w, int h,
108
unsigned char *rgb_tab)
112
unsigned char *cb, *cr;
113
unsigned char *lum_tab, *cb_tab, *cr_tab;
115
lum_tab = malloc(w * h);
116
cb_tab = malloc((w * h) / 4);
117
cr_tab = malloc((w * h) / 4);
119
rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
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);
129
fwrite(cb, 1, w2, f);
130
fwrite(cr, 1, w2, f);
141
unsigned char *rgb_tab;
142
int width, height, wrap;
144
void put_pixel(int x, int y, int r, int g, int b)
148
if (x < 0 || x >= width ||
149
y < 0 || y >= height)
152
p = rgb_tab + y * wrap + x * 3;
158
unsigned char tab_r[256*256];
159
unsigned char tab_g[256*256];
160
unsigned char tab_b[256*256];
166
static int ipol(uint8_t *src, int x, int y){
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;
178
return (((1<<16) - frac_y)*s0 + frac_y*s1)>>24;
181
void gen_image(int num, int w, int h)
183
const int c = h_cos [teta];
184
const int s = h_sin [teta];
186
const int xi = -(w/2) * c;
187
const int yi = (w/2) * s;
189
const int xj = -(h/2) * s;
190
const int yj = -(h/2) * c;
200
x = xprime + xi + FIXP*w/2;
203
y = yprime + yi + FIXP*h/2;
206
for ( i=0 ; i<w ; i++ ) {
210
put_pixel(i, j, ipol(tab_r, x, y), ipol(tab_g, x, y), ipol(tab_b, x, y));
214
dep = ((x>>16)&255) + (((y>>16)&255)<<8);
215
put_pixel(i, j, tab_r[dep], tab_g[dep], tab_b[dep]);
220
teta = (teta+1) % 360;
226
void init_demo(const char *filename) {
234
fichier = fopen(filename,"r");
240
fread(line, 1, 15, fichier);
242
fread(line,1,3*W,fichier);
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];
252
for (i=0;i<360;i++) {
253
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;
260
int main(int argc, char **argv)
266
printf("usage: %s directory/ image.pnm\n"
267
"generate a test video stream\n", argv[0]);
274
rgb_tab = malloc(w * h * 3);
281
for(i=0;i<DEFAULT_NB_PICT;i++) {
282
snprintf(buf, sizeof(buf), "%s%d.pgm", argv[1], i);
284
pgmyuv_save(buf, w, h, rgb_tab);