79 #define ALIVE_CELL 0xFF
80 #define OFFSET(x) offsetof(LifeContext, x)
81 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
93 {
"random_seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT, {.i64=-1}, -1, UINT32_MAX,
FLAGS },
94 {
"seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT, {.i64=-1}, -1, UINT32_MAX,
FLAGS },
105 static int parse_rule(uint16_t *born_rule, uint16_t *stay_rule,
106 const char *rule_str,
void *log_ctx)
109 const char *p = rule_str;
113 if (strchr(
"bBsS", *p)) {
117 uint16_t *rule = (*p ==
'b' || *p ==
'B') ? born_rule : stay_rule;
119 while (*p >=
'0' && *p <=
'8') {
120 *rule += 1<<(*p -
'0');
126 }
while (strchr(
"bBsS", *p));
133 long int rule = strtol(rule_str, &tail, 10);
136 *born_rule = ((1<<9)-1) & rule;
137 *stay_rule = rule >> 9;
156 for (i = 0; i < life->
h; i++) {
157 for (j = 0; j < life->
w; j++)
170 int ret, i, i0, j, h = 0, w, max_w = 0;
181 h++; max_w =
FFMAX(w, max_w); w = 0;
189 if (max_w > life->
w || h > life->
h) {
191 "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
192 life->
w, life->
h, max_w, h);
210 for (i0 = 0, i = (life->
h - h)/2; i0 < h; i0++, i++) {
211 for (j = (life->
w - max_w)/2;; j++) {
230 life->
class = &life_class;
248 #define PARSE_COLOR(name) do { \
249 if ((ret = av_parse_color(life->name ## _color, life->name ## _color_str, -1, ctx))) { \
250 av_log(ctx, AV_LOG_ERROR, "Invalid " #name " color '%s'\n", \
251 life->name ## _color_str); \
254 av_freep(&life->name ## _color_str); \
263 "Mold color is set while mold isn't, ignoring the color.\n");
283 for (i = 0; i < life->
w * life->
h; i++) {
285 if (r <= life->random_fill_ratio)
295 "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d seed:%u\n",
296 life->
w, life->
h, frame_rate.
num, frame_rate.
den,
316 outlink->
w = life->
w;
317 outlink->
h = life->
h;
330 enum { NW,
N, NE,
W,
E, SW,
S, SE };
333 for (i = 0; i < life->
h; i++) {
334 for (j = 0; j < life->
w; j++) {
335 int pos[8][2], n, alive,
cell;
337 pos[NW][0] = (i-1) < 0 ? life->
h-1 : i-1; pos[NW][1] = (j-1) < 0 ? life->
w-1 : j-1;
338 pos[
N ][0] = (i-1) < 0 ? life->
h-1 : i-1; pos[
N ][1] = j ;
339 pos[NE][0] = (i-1) < 0 ? life->
h-1 : i-1; pos[NE][1] = (j+1) == life->
w ? 0 : j+1;
340 pos[
W ][0] = i ; pos[
W ][1] = (j-1) < 0 ? life->
w-1 : j-1;
341 pos[
E ][0] = i ; pos[
E ][1] = (j+1) == life->
w ? 0 : j+1;
342 pos[SW][0] = (i+1) == life->
h ? 0 : i+1; pos[SW][1] = (j-1) < 0 ? life->
w-1 : j-1;
343 pos[
S ][0] = (i+1) == life->
h ? 0 : i+1; pos[
S ][1] = j ;
344 pos[SE][0] = (i+1) == life->
h ? 0 : i+1; pos[SE][1] = (j+1) == life->
w ? 0 : j+1;
346 pos[NW][0] = (i-1) < 0 ? -1 : i-1; pos[NW][1] = (j-1) < 0 ? -1 : j-1;
347 pos[
N ][0] = (i-1) < 0 ? -1 : i-1; pos[
N ][1] = j ;
348 pos[NE][0] = (i-1) < 0 ? -1 : i-1; pos[NE][1] = (j+1) == life->
w ? -1 : j+1;
349 pos[
W ][0] = i ; pos[
W ][1] = (j-1) < 0 ? -1 : j-1;
350 pos[
E ][0] = i ; pos[
E ][1] = (j+1) == life->
w ? -1 : j+1;
351 pos[SW][0] = (i+1) == life->
h ? -1 : i+1; pos[SW][1] = (j-1) < 0 ? -1 : j-1;
352 pos[
S ][0] = (i+1) == life->
h ? -1 : i+1; pos[
S ][1] = j ;
353 pos[SE][0] = (i+1) == life->
h ? -1 : i+1; pos[SE][1] = (j+1) == life->
w ? -1 : j+1;
357 n = (pos[NW][0] == -1 || pos[NW][1] == -1 ? 0 : oldbuf[pos[NW][0]*life->
w + pos[NW][1]] ==
ALIVE_CELL) +
358 (pos[
N ][0] == -1 || pos[
N ][1] == -1 ? 0 : oldbuf[pos[
N ][0]*life->
w + pos[
N ][1]] ==
ALIVE_CELL) +
359 (pos[NE][0] == -1 || pos[NE][1] == -1 ? 0 : oldbuf[pos[NE][0]*life->
w + pos[NE][1]] ==
ALIVE_CELL) +
360 (pos[
W ][0] == -1 || pos[
W ][1] == -1 ? 0 : oldbuf[pos[
W ][0]*life->
w + pos[
W ][1]] ==
ALIVE_CELL) +
361 (pos[
E ][0] == -1 || pos[
E ][1] == -1 ? 0 : oldbuf[pos[
E ][0]*life->
w + pos[
E ][1]] ==
ALIVE_CELL) +
362 (pos[SW][0] == -1 || pos[SW][1] == -1 ? 0 : oldbuf[pos[SW][0]*life->
w + pos[SW][1]] ==
ALIVE_CELL) +
363 (pos[
S ][0] == -1 || pos[
S ][1] == -1 ? 0 : oldbuf[pos[
S ][0]*life->
w + pos[
S ][1]] ==
ALIVE_CELL) +
364 (pos[SE][0] == -1 || pos[SE][1] == -1 ? 0 : oldbuf[pos[SE][0]*life->
w + pos[SE][1]] ==
ALIVE_CELL);
365 cell = oldbuf[i*life->
w + j];
368 else if (cell) *newbuf = cell - 1;
370 av_dlog(ctx,
"i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n", i, j, n, cell, *newbuf);
385 for (i = 0; i < life->
h; i++) {
388 for (k = 0, j = 0; j < life->
w; j++) {
390 if (k==8 || j == life->
w-1) {
401 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
410 for (i = 0; i < life->
h; i++) {
412 for (j = 0; j < life->
w; j++) {
417 int death_age =
FFMIN((0xff - v) * life->
mold, 0xff);
418 *p++ =
FAST_DIV255((c2[0] << 8) + ((
int)c1[0] - (
int)c2[0]) * death_age);
419 *p++ =
FAST_DIV255((c2[1] << 8) + ((
int)c1[1] - (
int)c2[1]) * death_age);
420 *p++ =
FAST_DIV255((c2[2] << 8) + ((
int)c1[2] - (
int)c2[2]) * death_age);
423 AV_WB24(p, c[0]<<16 | c[1]<<8 | c[2]);
435 picref->
pts = life->
pts++;
438 life->
draw(outlink->
src, picref);
441 show_life_grid(outlink->
src);
452 if (life->
mold || memcmp(life-> life_color,
"\xff\xff\xff", 3)
483 .priv_class = &life_class,