videogen.c
Go to the documentation of this file.
1 /*
2  * Generate a synthetic YUV video sequence suitable for codec testing.
3  * NOTE: No floats are used to guarantee bitexact output.
4  *
5  * Copyright (c) 2002 Fabrice Bellard
6  *
7  * This file is part of Libav.
8  *
9  * Libav is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * Libav is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with Libav; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
27 
28 #define SCALEBITS 8
29 #define ONE_HALF (1 << (SCALEBITS - 1))
30 #define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
31 
32 static void rgb24_to_yuv420p(uint8_t *lum, uint8_t *cb, uint8_t *cr,
33  uint8_t *src, int width, int height)
34 {
35  int wrap, wrap3, x, y;
36  int r, g, b, r1, g1, b1;
37  uint8_t *p;
38 
39  wrap = width;
40  wrap3 = width * 3;
41  p = src;
42  for (y = 0; y < height; y += 2) {
43  for (x = 0; x < width; x += 2) {
44  r = p[0];
45  g = p[1];
46  b = p[2];
47  r1 = r;
48  g1 = g;
49  b1 = b;
50  lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
51  FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
52  r = p[3];
53  g = p[4];
54  b = p[5];
55  r1 += r;
56  g1 += g;
57  b1 += b;
58  lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
59  FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
60  p += wrap3;
61  lum += wrap;
62 
63  r = p[0];
64  g = p[1];
65  b = p[2];
66  r1 += r;
67  g1 += g;
68  b1 += b;
69  lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
70  FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
71  r = p[3];
72  g = p[4];
73  b = p[5];
74  r1 += r;
75  g1 += g;
76  b1 += b;
77  lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
78  FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
79 
80  cb[0] = 128 + ((- FIX(0.16874) * r1 -
81  FIX(0.33126) * g1 +
82  FIX(0.50000) * b1 +
83  4 * ONE_HALF - 1)
84  >> (SCALEBITS + 2));
85  cr[0] = 128 + ((FIX(0.50000) * r1 -
86  FIX(0.41869) * g1 -
87  FIX(0.08131) * b1 +
88  4 * ONE_HALF - 1)
89  >> (SCALEBITS + 2));
90 
91  cb++;
92  cr++;
93  p += -wrap3 + 2 * 3;
94  lum += -wrap + 2;
95  }
96  p += wrap3;
97  lum += wrap;
98  }
99 }
100 
101 /* cif format */
102 #define DEFAULT_WIDTH 352
103 #define DEFAULT_HEIGHT 288
104 #define DEFAULT_NB_PICT 50 /* 2 seconds */
105 
106 static void pgmyuv_save(const char *filename, int w, int h,
107  unsigned char *rgb_tab)
108 {
109  FILE *f;
110  int i, h2, w2;
111  unsigned char *cb, *cr;
112  unsigned char *lum_tab, *cb_tab, *cr_tab;
113 
114  lum_tab = malloc(w * h);
115  cb_tab = malloc((w * h) / 4);
116  cr_tab = malloc((w * h) / 4);
117 
118  rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
119 
120  f = fopen(filename, "wb");
121  fprintf(f, "P5\n%d %d\n%d\n", w, (h * 3) / 2, 255);
122  fwrite(lum_tab, 1, w * h, f);
123  h2 = h / 2;
124  w2 = w / 2;
125  cb = cb_tab;
126  cr = cr_tab;
127  for (i = 0; i < h2; i++) {
128  fwrite(cb, 1, w2, f);
129  fwrite(cr, 1, w2, f);
130  cb += w2;
131  cr += w2;
132  }
133  fclose(f);
134 
135  free(lum_tab);
136  free(cb_tab);
137  free(cr_tab);
138 }
139 
140 unsigned char *rgb_tab;
142 
143 static void put_pixel(int x, int y, int r, int g, int b)
144 {
145  unsigned char *p;
146 
147  if (x < 0 || x >= width ||
148  y < 0 || y >= height)
149  return;
150 
151  p = rgb_tab + y * wrap + x * 3;
152  p[0] = r;
153  p[1] = g;
154  p[2] = b;
155 }
156 
157 static unsigned int myrnd(unsigned int *seed_ptr, int n)
158 {
159  unsigned int seed, val;
160 
161  seed = *seed_ptr;
162  seed = (seed * 314159) + 1;
163  if (n == 256) {
164  val = seed >> 24;
165  } else {
166  val = seed % n;
167  }
168  *seed_ptr = seed;
169  return val;
170 }
171 
172 #define NOISE_X 10
173 #define NOISE_Y 30
174 #define NOISE_W 26
175 
176 #define FRAC_BITS 8
177 #define FRAC_ONE (1 << FRAC_BITS)
178 
179 /* cosine approximate with 1-x^2 */
180 static int int_cos(int a)
181 {
182  int v, neg;
183  a = a & (FRAC_ONE - 1);
184  if (a >= (FRAC_ONE / 2))
185  a = FRAC_ONE - a;
186  neg = 0;
187  if (a > (FRAC_ONE / 4)) {
188  neg = -1;
189  a = (FRAC_ONE / 2) - a;
190  }
191  v = FRAC_ONE - ((a * a) >> 4);
192  v = (v ^ neg) - neg;
193  return v;
194 }
195 
196 #define NB_OBJS 10
197 
198 typedef struct VObj {
199  int x, y, w, h;
200  int r, g, b;
201 } VObj;
202 
204 
205 unsigned int seed = 1;
206 
207 static void gen_image(int num, int w, int h)
208 {
209  int r, g, b, x, y, i, dx, dy, x1, y1;
210  unsigned int seed1;
211 
212  if (num == 0) {
213  for (i = 0; i < NB_OBJS; i++) {
214  objs[i].x = myrnd(&seed, w);
215  objs[i].y = myrnd(&seed, h);
216  objs[i].w = myrnd(&seed, w / 4) + 10;
217  objs[i].h = myrnd(&seed, h / 4) + 10;
218  objs[i].r = myrnd(&seed, 256);
219  objs[i].g = myrnd(&seed, 256);
220  objs[i].b = myrnd(&seed, 256);
221  }
222  }
223 
224  /* first a moving background with gradients */
225  /* test motion estimation */
226  dx = int_cos(num * FRAC_ONE / 50) * 35;
227  dy = int_cos(num * FRAC_ONE / 50 + FRAC_ONE / 10) * 30;
228  for (y = 0; y < h; y++) {
229  for (x = 0; x < w; x++) {
230  x1 = (x << FRAC_BITS) + dx;
231  y1 = (y << FRAC_BITS) + dy;
232  r = ((y1 * 7) >> FRAC_BITS) & 0xff;
233  g = (((x1 + y1) * 9) >> FRAC_BITS) & 0xff;
234  b = ((x1 * 5) >> FRAC_BITS) & 0xff;
235  put_pixel(x, y, r, g, b);
236  }
237  }
238 
239  /* then some noise with very high intensity to test saturation */
240  seed1 = num;
241  for (y = 0; y < NOISE_W; y++) {
242  for (x = 0; x < NOISE_W; x++) {
243  r = myrnd(&seed1, 256);
244  g = myrnd(&seed1, 256);
245  b = myrnd(&seed1, 256);
246  put_pixel(x + NOISE_X, y + NOISE_Y, r, g, b);
247  }
248  }
249 
250  /* then moving objects */
251  for (i = 0; i < NB_OBJS; i++) {
252  VObj *p = &objs[i];
253  seed1 = i;
254  for (y = 0; y < p->h; y++) {
255  for (x = 0; x < p->w; x++) {
256  r = p->r;
257  g = p->g;
258  b = p->b;
259  /* add a per object noise */
260  r += myrnd(&seed1, 50);
261  g += myrnd(&seed1, 50);
262  b += myrnd(&seed1, 50);
263  put_pixel(x + p->x, y + p->y, r, g, b);
264  }
265  }
266  p->x += myrnd(&seed, 21) - 10;
267  p->y += myrnd(&seed, 21) - 10;
268  }
269 }
270 
271 int main(int argc, char **argv)
272 {
273  int w, h, i;
274  char buf[1024];
275 
276  if (argc != 2) {
277  printf("usage: %s file\n"
278  "generate a test video stream\n", argv[0]);
279  exit(1);
280  }
281 
282  w = DEFAULT_WIDTH;
283  h = DEFAULT_HEIGHT;
284 
285  rgb_tab = malloc(w * h * 3);
286  wrap = w * 3;
287  width = w;
288  height = h;
289 
290  for (i = 0; i < DEFAULT_NB_PICT; i++) {
291  snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
292  gen_image(i, w, h);
293  pgmyuv_save(buf, w, h, rgb_tab);
294  }
295 
296  free(rgb_tab);
297  return 0;
298 }