2
* Generates a synthetic YUV video sequence suitable for codec testing.
3
* NOTE: no floats are used to guaranty a bit exact output.
9
#define ONE_HALF (1 << (SCALEBITS - 1))
10
#define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
11
typedef unsigned char uint8_t;
13
static void rgb24_to_yuv420p(uint8_t *lum, uint8_t *cb, uint8_t *cr,
14
uint8_t *src, int width, int height)
16
int wrap, wrap3, x, y;
17
int r, g, b, r1, g1, b1;
23
for(y=0;y<height;y+=2) {
24
for(x=0;x<width;x+=2) {
31
lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
32
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
39
lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
40
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
50
lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
51
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
58
lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
59
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
61
cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
62
FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
63
cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
64
FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
77
#define DEFAULT_WIDTH 352
78
#define DEFAULT_HEIGHT 288
79
#define DEFAULT_NB_PICT 50 /* 2 seconds */
81
void pgmyuv_save(const char *filename, int w, int h,
82
unsigned char *rgb_tab)
86
unsigned char *cb, *cr;
87
unsigned char *lum_tab, *cb_tab, *cr_tab;
89
lum_tab = malloc(w * h);
90
cb_tab = malloc((w * h) / 4);
91
cr_tab = malloc((w * h) / 4);
93
rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
95
f = fopen(filename,"wb");
96
fprintf(f, "P5\n%d %d\n%d\n", w, (h * 3) / 2, 255);
97
fwrite(lum_tab, 1, w * h, f);
103
fwrite(cb, 1, w2, f);
104
fwrite(cr, 1, w2, f);
115
unsigned char *rgb_tab;
116
int width, height, wrap;
118
void put_pixel(int x, int y, int r, int g, int b)
122
if (x < 0 || x >= width ||
123
y < 0 || y >= height)
126
p = rgb_tab + y * wrap + x * 3;
132
static unsigned int myrnd(unsigned int *seed_ptr, int n)
134
unsigned int seed, val;
137
seed = (seed * 314159) + 1;
152
#define FRAC_ONE (1 << FRAC_BITS)
154
/* cosine approximate with 1-x^2 */
158
a = a & (FRAC_ONE - 1);
159
if (a >= (FRAC_ONE / 2))
162
if (a > (FRAC_ONE / 4)) {
164
a = (FRAC_ONE / 2) - a;
166
v = FRAC_ONE - ((a * a) >> 4);
173
typedef struct VObj {
180
unsigned int seed = 1;
182
void gen_image(int num, int w, int h)
184
int r, g, b, x, y, i, dx, dy, x1, y1;
188
for(i=0;i<NB_OBJS;i++) {
189
objs[i].x = myrnd(&seed, w);
190
objs[i].y = myrnd(&seed, h);
191
objs[i].w = myrnd(&seed, w / 4) + 10;
192
objs[i].h = myrnd(&seed, h / 4) + 10;
193
objs[i].r = myrnd(&seed, 256);
194
objs[i].g = myrnd(&seed, 256);
195
objs[i].b = myrnd(&seed, 256);
199
/* first a moving background with gradients */
200
/* test motion estimation */
201
dx = int_cos(num * FRAC_ONE / 50) * 35;
202
dy = int_cos(num * FRAC_ONE / 50 + FRAC_ONE / 10) * 30;
205
x1 = (x << FRAC_BITS) + dx;
206
y1 = (y << FRAC_BITS) + dx;
207
r = ((y1 * 7) >> FRAC_BITS) & 0xff;
208
g = (((x1 + y1) * 9) >> FRAC_BITS) & 0xff;
209
b = ((x1 * 5) >> FRAC_BITS) & 0xff;
210
put_pixel(x, y, r, g, b);
214
/* then some noise with very high intensity to test saturation */
216
for(y=0;y<NOISE_W;y++) {
217
for(x=0;x<NOISE_W;x++) {
218
r = myrnd(&seed1, 256);
219
g = myrnd(&seed1, 256);
220
b = myrnd(&seed1, 256);
221
put_pixel(x + NOISE_X, y + NOISE_Y, r, g, b);
225
/* then moving objects */
226
for(i=0;i<NB_OBJS;i++) {
229
for(y=0;y<p->h;y++) {
230
for(x=0;x<p->w;x++) {
234
/* add a per object noise */
235
r += myrnd(&seed1, 50);
236
g += myrnd(&seed1, 50);
237
b += myrnd(&seed1, 50);
238
put_pixel(x + p->x, y + p->y, r, g, b);
241
p->x += myrnd(&seed, 21) - 10;
242
p->y += myrnd(&seed, 21) - 10;
246
int main(int argc, char **argv)
252
printf("usage: %s file\n"
253
"generate a test video stream\n", argv[0]);
259
printf("cos(%d)=%d\n", i, int_cos(i));
265
rgb_tab = malloc(w * h * 3);
270
for(i=0;i<DEFAULT_NB_PICT;i++) {
271
snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
273
pgmyuv_save(buf, w, h, rgb_tab);