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;
116 while (*
p >=
'0' && *
p <=
'8') {
117 *rule += 1<<(*
p -
'0');
119 }
121 break;
123 }
while (strchr(
"bBsS", *
p));
124
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 {
168
169 /* fill the output picture with the old grid buffer */
170 for (
i = 0;
i < life->
h;
i++) {
171 uint8_t byte = 0;
173 for (k = 0, j = 0; j < life->
w; j++) {
175 if (k==8 || j == life->
w-1) {
176 k = 0;
178 byte = 0;
179 }
180 }
181 }
182 }
183
184 // divide by 255 and round to nearest
185 // apply a fast variant: (X+127)/255 = ((X+127)*257+257)>>16 = ((X+128)*257)>>16
186 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
187
189 {
193
194 /* fill the output picture with the old grid buffer */
195 for (
i = 0;
i < life->
h;
i++) {
197 for (j = 0; j < life->
w; j++) {
198 uint8_t v = buf[
i*life->
w + j];
200 const uint8_t *
c1 = life-> mold_color;
202 int death_age =
FFMIN((0xff - v) * life->
mold, 0xff);
206 } else {
210 }
211 }
212 }
213 }
214
216 {
219 int ret,
i, i0, j,
h = 0,
w, max_w = 0;
220
225
226 /* prescan file to get the number of lines and the maximum width */
230 h++; max_w =
FFMAX(
w, max_w);
w = 0;
231 } else {
233 }
234 }
236
238 if (max_w > life->
w ||
h > life->
h) {
240 "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
241 life->
w, life->
h, max_w,
h);
243 }
244 } else {
245 /* size was not specified, set it to size of the grid */
248 }
249
255 }
256
257 /* fill buf[0] */
259 for (i0 = 0,
i = (life->
h -
h)/2; i0 <
h; i0++,
i++) {
260 for (j = (life->
w - max_w)/2;; j++) {
264 } else
266 }
267 }
269
270 return 0;
271 }
272
274 {
277
280
283
286 "Mold color is set while mold isn't, ignoring the color.\n");
287
289 /* fill the grid randomly */
291
297 }
300
302
303 for (
i = 0;
i < life->
w * life->
h;
i++) {
305 if (r <= life->random_fill_ratio)
307 }
309 } else {
312 }
313
314 if (life->
mold || memcmp(life-> life_color,
"\xff\xff\xff", 3)
317 } else {
319 }
320
322 "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d seed:%"PRId64"\n",
326 return 0;
327 }
328
330 {
332
337 }
338
340 {
343
344 outlink->
w = life->
w;
345 outlink->
h = life->
h;
348
349 return 0;
350 }
351
353 {
358
359 enum { NW,
N, NE,
W,
E,
SW,
S,
SE };
360
361 /* evolve the grid */
362 for (
i = 0;
i < life->
h;
i++) {
363 for (j = 0; j < life->
w; j++) {
364 int pos[8][2], n, alive, cell;
366 pos[NW][0] = (
i-1) < 0 ? life->
h-1 :
i-1;
pos[NW][1] = (j-1) < 0 ? life->
w-1 : j-1;
367 pos[
N ][0] = (
i-1) < 0 ? life->
h-1 :
i-1;
pos[
N ][1] = j ;
368 pos[NE][0] = (
i-1) < 0 ? life->
h-1 :
i-1;
pos[NE][1] = (j+1) == life->
w ? 0 : j+1;
369 pos[
W ][0] =
i ;
pos[
W ][1] = (j-1) < 0 ? life->
w-1 : j-1;
370 pos[
E ][0] =
i ;
pos[
E ][1] = (j+1) == life->
w ? 0 : j+1;
371 pos[
SW][0] = (
i+1) == life->
h ? 0 :
i+1;
pos[
SW][1] = (j-1) < 0 ? life->
w-1 : j-1;
372 pos[
S ][0] = (
i+1) == life->
h ? 0 :
i+1;
pos[
S ][1] = j ;
373 pos[
SE][0] = (
i+1) == life->
h ? 0 :
i+1;
pos[
SE][1] = (j+1) == life->
w ? 0 : j+1;
374 } else {
375 pos[NW][0] = (
i-1) < 0 ? -1 :
i-1;
pos[NW][1] = (j-1) < 0 ? -1 : j-1;
376 pos[
N ][0] = (
i-1) < 0 ? -1 :
i-1;
pos[
N ][1] = j ;
377 pos[NE][0] = (
i-1) < 0 ? -1 :
i-1;
pos[NE][1] = (j+1) == life->
w ? -1 : j+1;
378 pos[
W ][0] =
i ;
pos[
W ][1] = (j-1) < 0 ? -1 : j-1;
379 pos[
E ][0] =
i ;
pos[
E ][1] = (j+1) == life->
w ? -1 : j+1;
380 pos[
SW][0] = (
i+1) == life->
h ? -1 :
i+1;
pos[
SW][1] = (j-1) < 0 ? -1 : j-1;
381 pos[
S ][0] = (
i+1) == life->
h ? -1 :
i+1;
pos[
S ][1] = j ;
382 pos[
SE][0] = (
i+1) == life->
h ? -1 :
i+1;
pos[
SE][1] = (j+1) == life->
w ? -1 : j+1;
383 }
384
385 /* compute the number of live neighbor cells */
394 cell = oldbuf[
i*life->
w + j];
396 if (alive) *newbuf =
ALIVE_CELL;
// new cell is alive
397 else if (cell) *newbuf = cell - 1; // new cell is dead and in the process of mold
398 else *newbuf = 0; // new cell is definitely dead
399 ff_dlog(
ctx,
"i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n",
i, j, n, cell, *newbuf);
400 newbuf++;
401 }
402 }
403
405 }
406
408 {
411 if (!picref)
414 picref->
pts = life->
pts++;
416
417 life->
draw(outlink->
src, picref);
419 #ifdef DEBUG
420 show_life_grid(outlink->
src);
421 #endif
423 }
424
428 {
433 };
434
436 }
437
439 {
444 },
445 };
446
450 .p.priv_class = &life_class,
457 };