Merge remote-tracking branch 'libav/master'
[FFMpeg-mirror/mplayer-patches.git] / tests / videogen.c
blob0b7f67eb18925252e22bc0839416740dd153a0e1
1 /*
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 Libav.
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.
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.
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
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
28 #include "utils.c"
30 static unsigned int myrnd(unsigned int *seed_ptr, int n)
32 unsigned int seed, val;
34 seed = *seed_ptr;
35 seed = (seed * 314159) + 1;
36 if (n == 256) {
37 val = seed >> 24;
38 } else {
39 val = seed % n;
41 *seed_ptr = seed;
42 return val;
45 #define NOISE_X 10
46 #define NOISE_Y 30
47 #define NOISE_W 26
49 #define FRAC_BITS 8
50 #define FRAC_ONE (1 << FRAC_BITS)
52 /* cosine approximate with 1-x^2 */
53 static int int_cos(int a)
55 int v, neg;
56 a = a & (FRAC_ONE - 1);
57 if (a >= (FRAC_ONE / 2))
58 a = FRAC_ONE - a;
59 neg = 0;
60 if (a > (FRAC_ONE / 4)) {
61 neg = -1;
62 a = (FRAC_ONE / 2) - a;
64 v = FRAC_ONE - ((a * a) >> 4);
65 v = (v ^ neg) - neg;
66 return v;
69 #define NB_OBJS 10
71 typedef struct VObj {
72 int x, y, w, h;
73 int r, g, b;
74 } VObj;
76 static VObj objs[NB_OBJS];
78 static unsigned int seed = 1;
80 static void gen_image(int num, int w, int h)
82 int r, g, b, x, y, i, dx, dy, x1, y1;
83 unsigned int seed1;
85 if (num == 0) {
86 for (i = 0; i < NB_OBJS; i++) {
87 objs[i].x = myrnd(&seed, w);
88 objs[i].y = myrnd(&seed, h);
89 objs[i].w = myrnd(&seed, w / 4) + 10;
90 objs[i].h = myrnd(&seed, h / 4) + 10;
91 objs[i].r = myrnd(&seed, 256);
92 objs[i].g = myrnd(&seed, 256);
93 objs[i].b = myrnd(&seed, 256);
97 /* first a moving background with gradients */
98 /* test motion estimation */
99 dx = int_cos(num * FRAC_ONE / 50) * 35;
100 dy = int_cos(num * FRAC_ONE / 50 + FRAC_ONE / 10) * 30;
101 for (y = 0; y < h; y++) {
102 for (x = 0; x < w; x++) {
103 x1 = (x << FRAC_BITS) + dx;
104 y1 = (y << FRAC_BITS) + dy;
105 r = ((y1 * 7) >> FRAC_BITS) & 0xff;
106 g = (((x1 + y1) * 9) >> FRAC_BITS) & 0xff;
107 b = ((x1 * 5) >> FRAC_BITS) & 0xff;
108 put_pixel(x, y, r, g, b);
112 /* then some noise with very high intensity to test saturation */
113 seed1 = num;
114 for (y = 0; y < NOISE_W; y++) {
115 for (x = 0; x < NOISE_W; x++) {
116 r = myrnd(&seed1, 256);
117 g = myrnd(&seed1, 256);
118 b = myrnd(&seed1, 256);
119 put_pixel(x + NOISE_X, y + NOISE_Y, r, g, b);
123 /* then moving objects */
124 for (i = 0; i < NB_OBJS; i++) {
125 VObj *p = &objs[i];
126 seed1 = i;
127 for (y = 0; y < p->h; y++) {
128 for (x = 0; x < p->w; x++) {
129 r = p->r;
130 g = p->g;
131 b = p->b;
132 /* add a per object noise */
133 r += myrnd(&seed1, 50);
134 g += myrnd(&seed1, 50);
135 b += myrnd(&seed1, 50);
136 put_pixel(x + p->x, y + p->y, r, g, b);
139 p->x += myrnd(&seed, 21) - 10;
140 p->y += myrnd(&seed, 21) - 10;
144 int main(int argc, char **argv)
146 int w, h, i;
147 char buf[1024];
148 int isdir = 0;
150 if (argc != 2) {
151 printf("usage: %s file|dir\n"
152 "generate a test video stream\n", argv[0]);
153 exit(1);
156 if (!freopen(argv[1], "wb", stdout))
157 isdir = 1;
159 w = DEFAULT_WIDTH;
160 h = DEFAULT_HEIGHT;
162 rgb_tab = malloc(w * h * 3);
163 wrap = w * 3;
164 width = w;
165 height = h;
167 for (i = 0; i < DEFAULT_NB_PICT; i++) {
168 gen_image(i, w, h);
169 if (isdir) {
170 snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
171 pgmyuv_save(buf, w, h, rgb_tab);
172 } else {
173 pgmyuv_save(NULL, w, h, rgb_tab);
177 free(rgb_tab);
178 return 0;