1 /*
2 * Copyright (c) Stefano Sabatini 2010
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * @file
23 * life video source, based on John Conways' Life Game
24 */
25
26 /* #define DEBUG */
27
40
48
49 /**
50 * The two grid state buffers.
51 *
52 * A 0xFF (ALIVE_CELL) value means the cell is alive (or new born), while
53 * the decreasing values from 0xFE to 0 means the cell is dead; the range
54 * of values is used for the slow death effect, or mold (0xFE means dead,
55 * 0xFD means very dead, 0xFC means very very dead... and 0x00 means
56 * definitely dead/mold).
57 */
59
61 uint16_t
stay_rule;
///< encode the behavior for filled cells
62 uint16_t
born_rule;
///< encode the behavior for empty cells
75
76 #define ALIVE_CELL 0xFF
77 #define OFFSET(x) offsetof(LifeContext, x)
78 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
79
90 {
"random_seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX,
FLAGS },
91 {
"seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT64, {.i64=-1}, -1, UINT32_MAX,
FLAGS },
98 };
99
101
102 static int parse_rule(uint16_t *born_rule, uint16_t *stay_rule,
103 const char *rule_str, void *log_ctx)
104 {
105 char *tail;
106 const char *p = rule_str;
107 *born_rule = 0;
108 *stay_rule = 0;
109
110 if (strchr("bBsS", *p)) {
111 /* parse rule as a Born / Stay Alive code, see
112 * http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life */
113 do {
114 uint16_t *rule = (*p == 'b' || *p == 'B') ? born_rule : stay_rule;
115 p++;
116 while (*p >= '0' && *p <= '8') {
117 *rule += 1<<(*p - '0');
118 p++;
119 }
120 if (*p != '/')
121 break;
122 p++;
123 } while (strchr("bBsS", *p));
124
125 if (*p)
127 } else {
128 /* parse rule as a number, expressed in the form STAY|(BORN<<9),
129 * where STAY and BORN encode the corresponding 9-bits rule */
130 long int rule = strtol(rule_str, &tail, 10);
131 if (*tail)
133 *born_rule = ((1<<9)-1) & rule;
134 *stay_rule = rule >> 9;
135 }
136
137 return 0;
138
142 }
143
144 #ifdef DEBUG
146 {
149
152 return;
153 for (
i = 0;
i < life->
h;
i++) {
154 for (j = 0; j < life->
w; j++)
158 }
160 }
161 #endif
162
164 {
166 char *p;
167 int ret,
i, i0, j,
h = 0,
w, max_w = 0;
168
173
174 /* prescan file to get the number of lines and the maximum width */
178 h++; max_w =
FFMAX(
w, max_w);
w = 0;
179 } else {
181 }
182 }
184
186 if (max_w > life->
w ||
h > life->
h) {
188 "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
189 life->
w, life->
h, max_w,
h);
191 }
192 } else {
193 /* size was not specified, set it to size of the grid */
196 }
197
203 }
204
205 /* fill buf[0] */
207 for (i0 = 0,
i = (life->
h -
h)/2; i0 <
h; i0++,
i++) {
208 for (j = (life->
w - max_w)/2;; j++) {
210 if (*p == '\n') {
211 p++; break;
212 } else
214 }
215 }
217
218 return 0;
219 }
220
222 {
225
228
231
234 "Mold color is set while mold isn't, ignoring the color.\n");
235
237 /* fill the grid randomly */
239
245 }
248
250
251 for (
i = 0;
i < life->
w * life->
h;
i++) {
253 if (r <= life->random_fill_ratio)
255 }
257 } else {
260 }
261
263 "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d seed:%"PRId64"\n",
267 return 0;
268 }
269
271 {
273
278 }
279
281 {
284
285 outlink->
w = life->
w;
286 outlink->
h = life->
h;
289
290 return 0;
291 }
292
294 {
299
300 enum { NW,
N, NE,
W,
E,
SW,
S,
SE };
301
302 /* evolve the grid */
303 for (
i = 0;
i < life->
h;
i++) {
304 for (j = 0; j < life->
w; j++) {
305 int pos[8][2], n, alive, cell;
307 pos[NW][0] = (
i-1) < 0 ? life->
h-1 :
i-1;
pos[NW][1] = (j-1) < 0 ? life->
w-1 : j-1;
308 pos[
N ][0] = (
i-1) < 0 ? life->
h-1 :
i-1;
pos[
N ][1] = j ;
309 pos[NE][0] = (
i-1) < 0 ? life->
h-1 :
i-1;
pos[NE][1] = (j+1) == life->
w ? 0 : j+1;
310 pos[
W ][0] =
i ;
pos[
W ][1] = (j-1) < 0 ? life->
w-1 : j-1;
311 pos[
E ][0] =
i ;
pos[
E ][1] = (j+1) == life->
w ? 0 : j+1;
312 pos[
SW][0] = (
i+1) == life->
h ? 0 :
i+1;
pos[
SW][1] = (j-1) < 0 ? life->
w-1 : j-1;
313 pos[
S ][0] = (
i+1) == life->
h ? 0 :
i+1;
pos[
S ][1] = j ;
314 pos[
SE][0] = (
i+1) == life->
h ? 0 :
i+1;
pos[
SE][1] = (j+1) == life->
w ? 0 : j+1;
315 } else {
316 pos[NW][0] = (
i-1) < 0 ? -1 :
i-1;
pos[NW][1] = (j-1) < 0 ? -1 : j-1;
317 pos[
N ][0] = (
i-1) < 0 ? -1 :
i-1;
pos[
N ][1] = j ;
318 pos[NE][0] = (
i-1) < 0 ? -1 :
i-1;
pos[NE][1] = (j+1) == life->
w ? -1 : j+1;
319 pos[
W ][0] =
i ;
pos[
W ][1] = (j-1) < 0 ? -1 : j-1;
320 pos[
E ][0] =
i ;
pos[
E ][1] = (j+1) == life->
w ? -1 : j+1;
321 pos[
SW][0] = (
i+1) == life->
h ? -1 :
i+1;
pos[
SW][1] = (j-1) < 0 ? -1 : j-1;
322 pos[
S ][0] = (
i+1) == life->
h ? -1 :
i+1;
pos[
S ][1] = j ;
323 pos[
SE][0] = (
i+1) == life->
h ? -1 :
i+1;
pos[
SE][1] = (j+1) == life->
w ? -1 : j+1;
324 }
325
326 /* compute the number of live neighbor cells */
335 cell = oldbuf[
i*life->
w + j];
337 if (alive) *newbuf =
ALIVE_CELL;
// new cell is alive
338 else if (cell) *newbuf = cell - 1; // new cell is dead and in the process of mold
339 else *newbuf = 0; // new cell is definitely dead
340 ff_dlog(
ctx,
"i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n",
i, j, n, cell, *newbuf);
341 newbuf++;
342 }
343 }
344
346 }
347
349 {
353
354 /* fill the output picture with the old grid buffer */
355 for (
i = 0;
i < life->
h;
i++) {
356 uint8_t byte = 0;
358 for (k = 0, j = 0; j < life->
w; j++) {
360 if (k==8 || j == life->
w-1) {
361 k = 0;
363 byte = 0;
364 }
365 }
366 }
367 }
368
369 // divide by 255 and round to nearest
370 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
371 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
372
374 {
378
379 /* fill the output picture with the old grid buffer */
380 for (
i = 0;
i < life->
h;
i++) {
382 for (j = 0; j < life->
w; j++) {
383 uint8_t v = buf[
i*life->
w + j];
385 const uint8_t *
c1 = life-> mold_color;
387 int death_age =
FFMIN((0xff - v) * life->
mold, 0xff);
391 } else {
394 p += 3;
395 }
396 }
397 }
398 }
399
401 {
404 if (!picref)
407 picref->
pts = life->
pts++;
409
410 life->
draw(outlink->
src, picref);
412 #ifdef DEBUG
413 show_life_grid(outlink->
src);
414 #endif
416 }
417
419 {
422
423 if (life->
mold || memcmp(life-> life_color,
"\xff\xff\xff", 3)
427 } else {
430 }
431
433 }
434
436 {
441 },
442 };
443
448 .priv_class = &life_class,
454 };