Commit | Line | Data |
---|---|---|
51133a7d | 1 | /* |
58c42af7 DB |
2 | * Generate a synthetic YUV video sequence suitable for codec testing. |
3 | * NOTE: No floats are used to guarantee bitexact output. | |
eebfba2e DB |
4 | * |
5 | * Copyright (c) 2002 Fabrice Bellard | |
6 | * | |
2912e87a | 7 | * This file is part of Libav. |
eebfba2e | 8 | * |
2912e87a | 9 | * Libav is free software; you can redistribute it and/or |
eebfba2e DB |
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 | * | |
2912e87a | 14 | * Libav is distributed in the hope that it will be useful, |
eebfba2e DB |
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 | |
2912e87a | 20 | * License along with Libav; if not, write to the Free Software |
eebfba2e | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
51133a7d | 22 | */ |
eebfba2e | 23 | |
51133a7d | 24 | #include <stdlib.h> |
e680989d | 25 | #include <stdint.h> |
51133a7d FB |
26 | #include <stdio.h> |
27 | ||
f0ccd53a | 28 | #include "utils.c" |
51133a7d FB |
29 | |
30 | static unsigned int myrnd(unsigned int *seed_ptr, int n) | |
31 | { | |
32 | unsigned int seed, val; | |
33 | ||
34 | seed = *seed_ptr; | |
35 | seed = (seed * 314159) + 1; | |
36 | if (n == 256) { | |
37 | val = seed >> 24; | |
38 | } else { | |
39 | val = seed % n; | |
40 | } | |
41 | *seed_ptr = seed; | |
42 | return val; | |
43 | } | |
44 | ||
45 | #define NOISE_X 10 | |
46 | #define NOISE_Y 30 | |
47 | #define NOISE_W 26 | |
48 | ||
49 | #define FRAC_BITS 8 | |
50 | #define FRAC_ONE (1 << FRAC_BITS) | |
51 | ||
52 | /* cosine approximate with 1-x^2 */ | |
6f2162b4 | 53 | static int int_cos(int a) |
51133a7d FB |
54 | { |
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; | |
b481bbc3 | 62 | a = (FRAC_ONE / 2) - a; |
51133a7d FB |
63 | } |
64 | v = FRAC_ONE - ((a * a) >> 4); | |
65 | v = (v ^ neg) - neg; | |
66 | return v; | |
67 | } | |
68 | ||
69 | #define NB_OBJS 10 | |
70 | ||
71 | typedef struct VObj { | |
72 | int x, y, w, h; | |
73 | int r, g, b; | |
74 | } VObj; | |
75 | ||
814208a7 | 76 | static VObj objs[NB_OBJS]; |
51133a7d | 77 | |
814208a7 | 78 | static unsigned int seed = 1; |
51133a7d | 79 | |
6f2162b4 | 80 | static void gen_image(int num, int w, int h) |
51133a7d FB |
81 | { |
82 | int r, g, b, x, y, i, dx, dy, x1, y1; | |
83 | unsigned int seed1; | |
84 | ||
85 | if (num == 0) { | |
b481bbc3 | 86 | for (i = 0; i < NB_OBJS; i++) { |
51133a7d FB |
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); | |
94 | } | |
95 | } | |
96 | ||
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; | |
b481bbc3 GN |
101 | for (y = 0; y < h; y++) { |
102 | for (x = 0; x < w; x++) { | |
51133a7d | 103 | x1 = (x << FRAC_BITS) + dx; |
07874f22 | 104 | y1 = (y << FRAC_BITS) + dy; |
b481bbc3 GN |
105 | r = ((y1 * 7) >> FRAC_BITS) & 0xff; |
106 | g = (((x1 + y1) * 9) >> FRAC_BITS) & 0xff; | |
107 | b = ((x1 * 5) >> FRAC_BITS) & 0xff; | |
51133a7d FB |
108 | put_pixel(x, y, r, g, b); |
109 | } | |
110 | } | |
111 | ||
112 | /* then some noise with very high intensity to test saturation */ | |
113 | seed1 = num; | |
b481bbc3 GN |
114 | for (y = 0; y < NOISE_W; y++) { |
115 | for (x = 0; x < NOISE_W; x++) { | |
51133a7d FB |
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); | |
120 | } | |
121 | } | |
115329f1 | 122 | |
51133a7d | 123 | /* then moving objects */ |
b481bbc3 | 124 | for (i = 0; i < NB_OBJS; i++) { |
51133a7d FB |
125 | VObj *p = &objs[i]; |
126 | seed1 = i; | |
b481bbc3 GN |
127 | for (y = 0; y < p->h; y++) { |
128 | for (x = 0; x < p->w; x++) { | |
51133a7d FB |
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); | |
137 | } | |
138 | } | |
139 | p->x += myrnd(&seed, 21) - 10; | |
140 | p->y += myrnd(&seed, 21) - 10; | |
141 | } | |
142 | } | |
143 | ||
144 | int main(int argc, char **argv) | |
145 | { | |
146 | int w, h, i; | |
147 | char buf[1024]; | |
148 | ||
149 | if (argc != 2) { | |
150 | printf("usage: %s file\n" | |
151 | "generate a test video stream\n", argv[0]); | |
152 | exit(1); | |
153 | } | |
154 | ||
51133a7d FB |
155 | w = DEFAULT_WIDTH; |
156 | h = DEFAULT_HEIGHT; | |
157 | ||
158 | rgb_tab = malloc(w * h * 3); | |
b481bbc3 GN |
159 | wrap = w * 3; |
160 | width = w; | |
161 | height = h; | |
51133a7d | 162 | |
b481bbc3 | 163 | for (i = 0; i < DEFAULT_NB_PICT; i++) { |
528bbdde | 164 | snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i); |
51133a7d FB |
165 | gen_image(i, w, h); |
166 | pgmyuv_save(buf, w, h, rgb_tab); | |
167 | } | |
115329f1 | 168 | |
51133a7d FB |
169 | free(rgb_tab); |
170 | return 0; | |
171 | } |