VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/tests/videogen.c@ 7812

Last change on this file since 7812 was 5776, checked in by vboxsync, 17 years ago

ffmpeg: exported to OSE

File size: 6.6 KB
Line 
1/*
2 * Generates a synthetic YUV video sequence suitable for codec testing.
3 * NOTE: no floats are used to guaranty a bit exact output.
4 */
5#include <stdlib.h>
6#include <stdio.h>
7
8#define SCALEBITS 8
9#define ONE_HALF (1 << (SCALEBITS - 1))
10#define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
11typedef unsigned char uint8_t;
12
13static void rgb24_to_yuv420p(uint8_t *lum, uint8_t *cb, uint8_t *cr,
14 uint8_t *src, int width, int height)
15{
16 int wrap, wrap3, x, y;
17 int r, g, b, r1, g1, b1;
18 uint8_t *p;
19
20 wrap = width;
21 wrap3 = width * 3;
22 p = src;
23 for(y=0;y<height;y+=2) {
24 for(x=0;x<width;x+=2) {
25 r = p[0];
26 g = p[1];
27 b = p[2];
28 r1 = r;
29 g1 = g;
30 b1 = b;
31 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
32 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
33 r = p[3];
34 g = p[4];
35 b = p[5];
36 r1 += r;
37 g1 += g;
38 b1 += b;
39 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
40 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
41 p += wrap3;
42 lum += wrap;
43
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
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;
65
66 cb++;
67 cr++;
68 p += -wrap3 + 2 * 3;
69 lum += -wrap + 2;
70 }
71 p += wrap3;
72 lum += wrap;
73 }
74}
75
76/* cif format */
77#define DEFAULT_WIDTH 352
78#define DEFAULT_HEIGHT 288
79#define DEFAULT_NB_PICT 50 /* 2 seconds */
80
81void pgmyuv_save(const char *filename, int w, int h,
82 unsigned char *rgb_tab)
83{
84 FILE *f;
85 int i, h2, w2;
86 unsigned char *cb, *cr;
87 unsigned char *lum_tab, *cb_tab, *cr_tab;
88
89 lum_tab = malloc(w * h);
90 cb_tab = malloc((w * h) / 4);
91 cr_tab = malloc((w * h) / 4);
92
93 rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
94
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);
98 h2 = h / 2;
99 w2 = w / 2;
100 cb = cb_tab;
101 cr = cr_tab;
102 for(i=0;i<h2;i++) {
103 fwrite(cb, 1, w2, f);
104 fwrite(cr, 1, w2, f);
105 cb += w2;
106 cr += w2;
107 }
108 fclose(f);
109
110 free(lum_tab);
111 free(cb_tab);
112 free(cr_tab);
113}
114
115unsigned char *rgb_tab;
116int width, height, wrap;
117
118void put_pixel(int x, int y, int r, int g, int b)
119{
120 unsigned char *p;
121
122 if (x < 0 || x >= width ||
123 y < 0 || y >= height)
124 return;
125
126 p = rgb_tab + y * wrap + x * 3;
127 p[0] = r;
128 p[1] = g;
129 p[2] = b;
130}
131
132static unsigned int myrnd(unsigned int *seed_ptr, int n)
133{
134 unsigned int seed, val;
135
136 seed = *seed_ptr;
137 seed = (seed * 314159) + 1;
138 if (n == 256) {
139 val = seed >> 24;
140 } else {
141 val = seed % n;
142 }
143 *seed_ptr = seed;
144 return val;
145}
146
147#define NOISE_X 10
148#define NOISE_Y 30
149#define NOISE_W 26
150
151#define FRAC_BITS 8
152#define FRAC_ONE (1 << FRAC_BITS)
153
154/* cosine approximate with 1-x^2 */
155int int_cos(int a)
156{
157 int v, neg;
158 a = a & (FRAC_ONE - 1);
159 if (a >= (FRAC_ONE / 2))
160 a = FRAC_ONE - a;
161 neg = 0;
162 if (a > (FRAC_ONE / 4)) {
163 neg = -1;
164 a = (FRAC_ONE / 2) - a;
165 }
166 v = FRAC_ONE - ((a * a) >> 4);
167 v = (v ^ neg) - neg;
168 return v;
169}
170
171#define NB_OBJS 10
172
173typedef struct VObj {
174 int x, y, w, h;
175 int r, g, b;
176} VObj;
177
178VObj objs[NB_OBJS];
179
180unsigned int seed = 1;
181
182void gen_image(int num, int w, int h)
183{
184 int r, g, b, x, y, i, dx, dy, x1, y1;
185 unsigned int seed1;
186
187 if (num == 0) {
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);
196 }
197 }
198
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;
203 for(y=0;y<h;y++) {
204 for(x=0;x<w;x++) {
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);
211 }
212 }
213
214 /* then some noise with very high intensity to test saturation */
215 seed1 = num;
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);
222 }
223 }
224
225 /* then moving objects */
226 for(i=0;i<NB_OBJS;i++) {
227 VObj *p = &objs[i];
228 seed1 = i;
229 for(y=0;y<p->h;y++) {
230 for(x=0;x<p->w;x++) {
231 r = p->r;
232 g = p->g;
233 b = p->b;
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);
239 }
240 }
241 p->x += myrnd(&seed, 21) - 10;
242 p->y += myrnd(&seed, 21) - 10;
243 }
244}
245
246int main(int argc, char **argv)
247{
248 int w, h, i;
249 char buf[1024];
250
251 if (argc != 2) {
252 printf("usage: %s file\n"
253 "generate a test video stream\n", argv[0]);
254 exit(1);
255 }
256
257#if 0
258 for(i=0;i<256;i++)
259 printf("cos(%d)=%d\n", i, int_cos(i));
260#endif
261
262 w = DEFAULT_WIDTH;
263 h = DEFAULT_HEIGHT;
264
265 rgb_tab = malloc(w * h * 3);
266 wrap = w * 3;
267 width = w;
268 height = h;
269
270 for(i=0;i<DEFAULT_NB_PICT;i++) {
271 snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
272 gen_image(i, w, h);
273 pgmyuv_save(buf, w, h, rgb_tab);
274 }
275
276 free(rgb_tab);
277 return 0;
278}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette