78 #define ALIVE_CELL 0xFF
79 #define OFFSET(x) offsetof(LifeContext, x)
80 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
92 {
"random_seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT, {.i64=-1}, -1, UINT32_MAX,
FLAGS },
93 {
"seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT, {.i64=-1}, -1, UINT32_MAX,
FLAGS },
104 static int parse_rule(uint16_t *born_rule, uint16_t *stay_rule,
105 const char *rule_str,
void *log_ctx)
108 const char *p = rule_str;
112 if (strchr(
"bBsS", *p)) {
116 uint16_t *rule = (*p ==
'b' || *p ==
'B') ? born_rule : stay_rule;
118 while (*p >=
'0' && *p <=
'8') {
119 *rule += 1<<(*p -
'0');
125 }
while (strchr(
"bBsS", *p));
132 long int rule = strtol(rule_str, &tail, 10);
135 *born_rule = ((1<<9)-1) & rule;
136 *stay_rule = rule >> 9;
155 for (i = 0; i < life->
h; i++) {
156 for (j = 0; j < life->
w; j++)
169 int ret, i, i0, j, h = 0, w, max_w = 0;
180 h++; max_w =
FFMAX(w, max_w); w = 0;
188 if (max_w > life->
w || h > life->
h) {
190 "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
191 life->
w, life->
h, max_w, h);
209 for (i0 = 0, i = (life->
h - h)/2; i0 < h; i0++, i++) {
210 for (j = (life->
w - max_w)/2;; j++) {
229 life->
class = &life_class;
247 #define PARSE_COLOR(name) do { \
248 if ((ret = av_parse_color(life->name ## _color, life->name ## _color_str, -1, ctx))) { \
249 av_log(ctx, AV_LOG_ERROR, "Invalid " #name " color '%s'\n", \
250 life->name ## _color_str); \
253 av_freep(&life->name ## _color_str); \
262 "Mold color is set while mold isn't, ignoring the color.\n");
282 for (i = 0; i < life->
w * life->
h; i++) {
284 if (r <= life->random_fill_ratio)
294 "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d seed:%u\n",
295 life->
w, life->
h, frame_rate.
num, frame_rate.
den,
315 outlink->
w = life->
w;
316 outlink->
h = life->
h;
329 enum { NW,
N, NE,
W,
E, SW,
S, SE };
332 for (i = 0; i < life->
h; i++) {
333 for (j = 0; j < life->
w; j++) {
334 int pos[8][2], n, alive,
cell;
336 pos[NW][0] = (i-1) < 0 ? life->
h-1 : i-1; pos[NW][1] = (j-1) < 0 ? life->
w-1 : j-1;
337 pos[
N ][0] = (i-1) < 0 ? life->
h-1 : i-1; pos[
N ][1] = j ;
338 pos[NE][0] = (i-1) < 0 ? life->
h-1 : i-1; pos[NE][1] = (j+1) == life->
w ? 0 : j+1;
339 pos[
W ][0] = i ; pos[
W ][1] = (j-1) < 0 ? life->
w-1 : j-1;
340 pos[
E ][0] = i ; pos[
E ][1] = (j+1) == life->
w ? 0 : j+1;
341 pos[SW][0] = (i+1) == life->
h ? 0 : i+1; pos[SW][1] = (j-1) < 0 ? life->
w-1 : j-1;
342 pos[
S ][0] = (i+1) == life->
h ? 0 : i+1; pos[
S ][1] = j ;
343 pos[SE][0] = (i+1) == life->
h ? 0 : i+1; pos[SE][1] = (j+1) == life->
w ? 0 : j+1;
345 pos[NW][0] = (i-1) < 0 ? -1 : i-1; pos[NW][1] = (j-1) < 0 ? -1 : j-1;
346 pos[
N ][0] = (i-1) < 0 ? -1 : i-1; pos[
N ][1] = j ;
347 pos[NE][0] = (i-1) < 0 ? -1 : i-1; pos[NE][1] = (j+1) == life->
w ? -1 : j+1;
348 pos[
W ][0] = i ; pos[
W ][1] = (j-1) < 0 ? -1 : j-1;
349 pos[
E ][0] = i ; pos[
E ][1] = (j+1) == life->
w ? -1 : j+1;
350 pos[SW][0] = (i+1) == life->
h ? -1 : i+1; pos[SW][1] = (j-1) < 0 ? -1 : j-1;
351 pos[
S ][0] = (i+1) == life->
h ? -1 : i+1; pos[
S ][1] = j ;
352 pos[SE][0] = (i+1) == life->
h ? -1 : i+1; pos[SE][1] = (j+1) == life->
w ? -1 : j+1;
356 n = (pos[NW][0] == -1 || pos[NW][1] == -1 ? 0 : oldbuf[pos[NW][0]*life->
w + pos[NW][1]] ==
ALIVE_CELL) +
357 (pos[
N ][0] == -1 || pos[
N ][1] == -1 ? 0 : oldbuf[pos[
N ][0]*life->
w + pos[
N ][1]] ==
ALIVE_CELL) +
358 (pos[NE][0] == -1 || pos[NE][1] == -1 ? 0 : oldbuf[pos[NE][0]*life->
w + pos[NE][1]] ==
ALIVE_CELL) +
359 (pos[
W ][0] == -1 || pos[
W ][1] == -1 ? 0 : oldbuf[pos[
W ][0]*life->
w + pos[
W ][1]] ==
ALIVE_CELL) +
360 (pos[
E ][0] == -1 || pos[
E ][1] == -1 ? 0 : oldbuf[pos[
E ][0]*life->
w + pos[
E ][1]] ==
ALIVE_CELL) +
361 (pos[SW][0] == -1 || pos[SW][1] == -1 ? 0 : oldbuf[pos[SW][0]*life->
w + pos[SW][1]] ==
ALIVE_CELL) +
362 (pos[
S ][0] == -1 || pos[
S ][1] == -1 ? 0 : oldbuf[pos[
S ][0]*life->
w + pos[
S ][1]] ==
ALIVE_CELL) +
363 (pos[SE][0] == -1 || pos[SE][1] == -1 ? 0 : oldbuf[pos[SE][0]*life->
w + pos[SE][1]] ==
ALIVE_CELL);
364 cell = oldbuf[i*life->
w + j];
367 else if (cell) *newbuf = cell - 1;
369 av_dlog(ctx,
"i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n", i, j, n, cell, *newbuf);
384 for (i = 0; i < life->
h; i++) {
387 for (k = 0, j = 0; j < life->
w; j++) {
389 if (k==8 || j == life->
w-1) {
400 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
409 for (i = 0; i < life->
h; i++) {
411 for (j = 0; j < life->
w; j++) {
416 int death_age =
FFMIN((0xff - v) * life->
mold, 0xff);
417 *p++ =
FAST_DIV255((c2[0] << 8) + ((
int)c1[0] - (
int)c2[0]) * death_age);
418 *p++ =
FAST_DIV255((c2[1] << 8) + ((
int)c1[1] - (
int)c2[1]) * death_age);
419 *p++ =
FAST_DIV255((c2[2] << 8) + ((
int)c1[2] - (
int)c2[2]) * death_age);
422 AV_WB24(p, c[0]<<16 | c[1]<<8 | c[2]);
434 picref->
pts = life->
pts++;
437 life->
draw(outlink->
src, picref);
440 show_life_grid(outlink->
src);
451 if (life->
mold || memcmp(life-> life_color,
"\xff\xff\xff", 3)
482 .priv_class = &life_class,