]> git.sesse.net Git - ffmpeg/blob - tests/videogen.c
avformat/avio: Add Metacube support
[ffmpeg] / tests / videogen.c
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 FFmpeg.
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <stdlib.h>
25 #include <stdint.h>
26 #include <stdio.h>
27
28 #include "utils.c"
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 */
53 static int int_cos(int a)
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;
62         a   = (FRAC_ONE / 2) - a;
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
76 static VObj objs[NB_OBJS];
77
78 static unsigned int seed = 1;
79
80 static void gen_image(int num, int w, int h)
81 {
82     int r, g, b, x, y, i, dx, dy, x1, y1;
83     unsigned int seed1;
84
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);
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;
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);
109         }
110     }
111
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);
120         }
121     }
122
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);
137             }
138         }
139         p->x += myrnd(&seed, 21) - 10;
140         p->y += myrnd(&seed, 21) - 10;
141     }
142 }
143
144 void print_help(const char* name)
145 {
146     printf("usage: %s file|dir [w=%i] [h=%i]\n"
147             "generate a test video stream\n",
148             name, DEFAULT_WIDTH, DEFAULT_HEIGHT);
149     exit(1);
150 }
151
152 int main(int argc, char **argv)
153 {
154     int w, h, i;
155     char buf[1024];
156     int isdir = 0;
157
158     if (argc < 2 || argc > 4) {
159         print_help(argv[0]);
160     }
161
162     if (!freopen(argv[1], "wb", stdout))
163         isdir = 1;
164
165     w = DEFAULT_WIDTH;
166     if(argc > 2) {
167         w = atoi(argv[2]);
168         if (w < 1) print_help(argv[0]);
169     }
170     h = DEFAULT_HEIGHT;
171     if(argc > 3) {
172         h = atoi(argv[3]);
173         if (h < 1) print_help(argv[0]);
174     }
175
176     rgb_tab = malloc(w * h * 3);
177     wrap    = w * 3;
178     width   = w;
179     height  = h;
180
181     for (i = 0; i < DEFAULT_NB_PICT; i++) {
182         gen_image(i, w, h);
183         if (isdir) {
184             snprintf(buf, sizeof(buf), "%s%02d.pgm", argv[1], i);
185             pgmyuv_save(buf, w, h, rgb_tab);
186         } else {
187             pgmyuv_save(NULL, w, h, rgb_tab);
188         }
189     }
190
191     free(rgb_tab);
192     return 0;
193 }