2 * Generate a synthetic YUV video sequence suitable for codec testing.
3 * NOTE: No floats are used to guarantee bitexact output.
5 * Copyright (c) 2002 Fabrice Bellard
7 * This file is part of FFmpeg.
9 * FFmpeg 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.
14 * FFmpeg 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.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #define ONE_HALF (1 << (SCALEBITS - 1))
30 #define FIX(x) ((int) ((x) * (1L << SCALEBITS) + 0.5))
32 static void rgb24_to_yuv420p(uint8_t *lum, uint8_t *cb, uint8_t *cr,
33 uint8_t *src, int width, int height)
35 int wrap, wrap3, x, y;
36 int r, g, b, r1, g1, b1;
42 for (y = 0; y < height; y += 2) {
43 for (x = 0; x < width; x += 2) {
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;
69 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
70 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
77 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
78 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
80 cb[0] = 128 + ((- FIX(0.16874) * r1 -
85 cr[0] = 128 + ((FIX(0.50000) * r1 -
102 #define DEFAULT_WIDTH 352
103 #define DEFAULT_HEIGHT 288
104 #define DEFAULT_NB_PICT 50 /* 2 seconds */
106 static void pgmyuv_save(const char *filename, int w, int h,
107 unsigned char *rgb_tab)
111 unsigned char *cb, *cr;
112 unsigned char *lum_tab, *cb_tab, *cr_tab;
114 lum_tab = malloc(w * h);
115 cb_tab = malloc((w * h) / 4);
116 cr_tab = malloc((w * h) / 4);
118 rgb24_to_yuv420p(lum_tab, cb_tab, cr_tab, rgb_tab, w, h);
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);
127 for (i = 0; i < h2; i++) {
128 fwrite(cb, 1, w2, f);
129 fwrite(cr, 1, w2, f);
140 unsigned char *rgb_tab;
141 int width, height, wrap;
143 static void put_pixel(int x, int y, int r, int g, int b)
147 if (x < 0 || x >= width ||
148 y < 0 || y >= height)
151 p = rgb_tab + y * wrap + x * 3;
157 static unsigned int myrnd(unsigned int *seed_ptr, int n)
159 unsigned int seed, val;
162 seed = (seed * 314159) + 1;
177 #define FRAC_ONE (1 << FRAC_BITS)
179 /* cosine approximate with 1-x^2 */
180 static int int_cos(int a)
183 a = a & (FRAC_ONE - 1);
184 if (a >= (FRAC_ONE / 2))
187 if (a > (FRAC_ONE / 4)) {
189 a = (FRAC_ONE / 2) - a;
191 v = FRAC_ONE - ((a * a) >> 4);
198 typedef struct VObj {
205 unsigned int seed = 1;
207 static void gen_image(int num, int w, int h)
209 int r, g, b, x, y, i, dx, dy, x1, y1;
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);
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);
239 /* then some noise with very high intensity to test saturation */
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);
250 /* then moving objects */
251 for (i = 0; i < NB_OBJS; i++) {
254 for (y = 0; y < p->h; y++) {
255 for (x = 0; x < p->w; x++) {
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);
266 p->x += myrnd(&seed, 21) - 10;
267 p->y += myrnd(&seed, 21) - 10;
271 int main(int argc, char **argv)
277 printf("usage: %s file\n"
278 "generate a test video stream\n", argv[0]);
285 rgb_tab = malloc(w * h * 3);
290 for (i = 0; i < DEFAULT_NB_PICT; i++) {
291 snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
293 pgmyuv_save(buf, w, h, rgb_tab);