FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vsrc_life.c
Date: 2026-10-11 16:25:10
Exec Total Coverage
Lines: 120 197 60.9%
Functions: 9 11 81.8%
Branches: 104 198 52.5%

Line Branch Exec Source
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
28 #include "libavutil/file.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/lfg.h"
32 #include "libavutil/mem.h"
33 #include "libavutil/opt.h"
34 #include "libavutil/random_seed.h"
35 #include "libavutil/avstring.h"
36 #include "avfilter.h"
37 #include "filters.h"
38 #include "formats.h"
39 #include "video.h"
40
41 typedef struct LifeContext {
42 const AVClass *class;
43 int w, h;
44 char *filename;
45 char *rule_str;
46 uint8_t *file_buf;
47 size_t file_bufsize;
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 */
58 uint8_t *buf[2];
59
60 uint8_t buf_idx;
61 uint16_t stay_rule; ///< encode the behavior for filled cells
62 uint16_t born_rule; ///< encode the behavior for empty cells
63 uint64_t pts;
64 AVRational frame_rate;
65 double random_fill_ratio;
66 int64_t random_seed;
67 int stitch;
68 int mold;
69 uint8_t life_color[4];
70 uint8_t death_color[4];
71 uint8_t mold_color[4];
72 AVLFG lfg;
73 void (*draw)(AVFilterContext*, AVFrame*);
74 } LifeContext;
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
80 static const AVOption life_options[] = {
81 { "filename", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
82 { "f", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
83 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
84 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
85 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
86 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
87 { "rule", "set rule", OFFSET(rule_str), AV_OPT_TYPE_STRING, {.str = "B3/S23"}, 0, 0, FLAGS },
88 { "random_fill_ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1/M_PHI}, 0, 1, FLAGS },
89 { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1/M_PHI}, 0, 1, FLAGS },
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 },
92 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
93 { "mold", "set mold speed for dead cells", OFFSET(mold), AV_OPT_TYPE_INT, {.i64=0}, 0, 0xFF, FLAGS },
94 { "life_color", "set life color", OFFSET( life_color), AV_OPT_TYPE_COLOR, {.str="white"}, 0, 0, FLAGS },
95 { "death_color", "set death color", OFFSET(death_color), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, FLAGS },
96 { "mold_color", "set mold color", OFFSET( mold_color), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, FLAGS },
97 { NULL }
98 };
99
100 AVFILTER_DEFINE_CLASS(life);
101
102 1 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 1 const char *p = rule_str;
107 1 *born_rule = 0;
108 1 *stay_rule = 0;
109
110
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 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
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 uint16_t *rule = (*p == 'b' || *p == 'B') ? born_rule : stay_rule;
115 2 p++;
116
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
5 while (*p >= '0' && *p <= '8') {
117 3 *rule += 1<<(*p - '0');
118 3 p++;
119 }
120
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (*p != '/')
121 1 break;
122 1 p++;
123
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } while (strchr("bBsS", *p));
124
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*p)
126 ✗ goto error;
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)
132 ✗ goto error;
133 ✗ *born_rule = ((1<<9)-1) & rule;
134 ✗ *stay_rule = rule >> 9;
135 }
136
137 1 return 0;
138
139 ✗ error:
140 ✗ av_log(log_ctx, AV_LOG_ERROR, "Invalid rule code '%s' provided\n", rule_str);
141 ✗ return AVERROR(EINVAL);
142 }
143
144 #ifdef DEBUG
145 static void show_life_grid(AVFilterContext *ctx)
146 {
147 LifeContext *life = ctx->priv;
148 int i, j;
149
150 char *line = av_malloc(life->w + 1);
151 if (!line)
152 return;
153 for (i = 0; i < life->h; i++) {
154 for (j = 0; j < life->w; j++)
155 line[j] = life->buf[life->buf_idx][i*life->w + j] == ALIVE_CELL ? '@' : ' ';
156 line[j] = 0;
157 av_log(ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
158 }
159 av_free(line);
160 }
161 #endif
162
163 ✗ static void fill_picture_monoblack(AVFilterContext *ctx, AVFrame *picref)
164 {
165 ✗ LifeContext *life = ctx->priv;
166 ✗ uint8_t *buf = life->buf[life->buf_idx];
167 int i, j, k;
168
169 /* fill the output picture with the old grid buffer */
170 ✗ for (i = 0; i < life->h; i++) {
171 ✗ uint8_t byte = 0;
172 ✗ uint8_t *p = picref->data[0] + i * picref->linesize[0];
173 ✗ for (k = 0, j = 0; j < life->w; j++) {
174 ✗ byte |= (buf[i*life->w+j] == ALIVE_CELL)<<(7-k++);
175 ✗ if (k==8 || j == life->w-1) {
176 ✗ k = 0;
177 ✗ *p++ = byte;
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
188 17 static void fill_picture_rgb(AVFilterContext *ctx, AVFrame *picref)
189 {
190 17 LifeContext *life = ctx->priv;
191 17 uint8_t *buf = life->buf[life->buf_idx];
192 int i, j;
193
194 /* fill the output picture with the old grid buffer */
195
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 17 times.
697 for (i = 0; i < life->h; i++) {
196 680 uint8_t *p = picref->data[0] + i * picref->linesize[0];
197
2/2
✓ Branch 0 taken 27200 times.
✓ Branch 1 taken 680 times.
27880 for (j = 0; j < life->w; j++) {
198 27200 uint8_t v = buf[i*life->w + j];
199
3/4
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25272 times.
✓ Branch 3 taken 1928 times.
27200 if (life->mold && v != ALIVE_CELL) {
200 25272 const uint8_t *c1 = life-> mold_color;
201 25272 const uint8_t *c2 = life->death_color;
202 25272 int death_age = FFMIN((0xff - v) * life->mold, 0xff);
203 25272 *p++ = FAST_DIV255((c2[0] << 8) + ((int)c1[0] - (int)c2[0]) * death_age);
204 25272 *p++ = FAST_DIV255((c2[1] << 8) + ((int)c1[1] - (int)c2[1]) * death_age);
205 25272 *p++ = FAST_DIV255((c2[2] << 8) + ((int)c1[2] - (int)c2[2]) * death_age);
206 } else {
207
1/2
✓ Branch 0 taken 1928 times.
✗ Branch 1 not taken.
1928 const uint8_t *c = v == ALIVE_CELL ? life->life_color : life->death_color;
208 1928 AV_WB24(p, c[0]<<16 | c[1]<<8 | c[2]);
209 1928 p += 3;
210 }
211 }
212 }
213 17 }
214
215 1 static int alloc_grid(AVFilterContext *ctx)
216 {
217 1 LifeContext *life = ctx->priv;
218
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 if (life->w <= 0 || life->h <= 0 || life->w > INT_MAX / life->h) {
219 ✗ av_log(ctx, AV_LOG_ERROR, "Invalid grid size %dx%d\n", life->w, life->h);
220 ✗ return AVERROR(EINVAL);
221 }
222
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (!(life->buf[0] = av_calloc(life->h * life->w, sizeof(*life->buf[0]))) ||
223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 !(life->buf[1] = av_calloc(life->h * life->w, sizeof(*life->buf[1])))) {
224 ✗ av_freep(&life->buf[0]);
225 ✗ av_freep(&life->buf[1]);
226 ✗ return AVERROR(ENOMEM);
227 }
228 1 return 0;
229 }
230
231 ✗ static int init_pattern_from_file(AVFilterContext *ctx)
232 {
233 ✗ LifeContext *life = ctx->priv;
234 char *p;
235 ✗ int ret, i, i0, j, h = 0, w, max_w = 0;
236
237 ✗ if ((ret = av_file_map(life->filename, &life->file_buf, &life->file_bufsize,
238 0, ctx)) < 0)
239 ✗ return ret;
240 ✗ av_freep(&life->filename);
241 ✗ if (life->file_bufsize > INT_MAX) {
242 ✗ av_log(ctx, AV_LOG_ERROR, "Pattern file too large\n");
243 ✗ return AVERROR(EINVAL);
244 }
245
246 /* prescan file to get the number of lines and the maximum width */
247 ✗ w = 0;
248 ✗ for (i = 0; i < life->file_bufsize; i++) {
249 ✗ if (life->file_buf[i] == '\n') {
250 ✗ h++; max_w = FFMAX(w, max_w); w = 0;
251 } else {
252 ✗ w++;
253 }
254 }
255 ✗ av_log(ctx, AV_LOG_DEBUG, "h:%d max_w:%d\n", h, max_w);
256
257 ✗ if (life->w) {
258 ✗ if (max_w > life->w || h > life->h) {
259 ✗ av_log(ctx, AV_LOG_ERROR,
260 "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
261 life->w, life->h, max_w, h);
262 ✗ return AVERROR(EINVAL);
263 }
264 } else {
265 /* size was not specified, set it to size of the grid */
266 ✗ life->w = max_w;
267 ✗ life->h = h;
268 }
269
270 ✗ if ((ret = alloc_grid(ctx)) < 0)
271 ✗ return ret;
272
273 /* fill buf[0] */
274 ✗ p = life->file_buf;
275 ✗ for (i0 = 0, i = (life->h - h)/2; i0 < h; i0++, i++) {
276 ✗ for (j = (life->w - max_w)/2;; j++) {
277 ✗ av_log(ctx, AV_LOG_DEBUG, "%d:%d %c\n", i, j, *p == '\n' ? 'N' : *p);
278 ✗ if (*p == '\n') {
279 ✗ p++; break;
280 } else
281 ✗ life->buf[0][i*life->w + j] = av_isgraph(*(p++)) ? ALIVE_CELL : 0;
282 }
283 }
284 ✗ life->buf_idx = 0;
285
286 ✗ return 0;
287 }
288
289 1 static av_cold int init(AVFilterContext *ctx)
290 {
291 1 LifeContext *life = ctx->priv;
292 int ret;
293
294
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (!life->w && !life->filename)
295 ✗ av_opt_set(life, "size", "320x240", 0);
296
297
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = parse_rule(&life->born_rule, &life->stay_rule, life->rule_str, ctx)) < 0)
298 ✗ return ret;
299
300
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (!life->mold && memcmp(life->mold_color, "\x00\x00\x00", 3))
301 ✗ av_log(ctx, AV_LOG_WARNING,
302 "Mold color is set while mold isn't, ignoring the color.\n");
303
304
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!life->filename) {
305 /* fill the grid randomly */
306 int i;
307
308
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if ((ret = alloc_grid(ctx)) < 0)
309 ✗ return ret;
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (life->random_seed == -1)
311 ✗ life->random_seed = av_get_random_seed();
312
313 1 av_lfg_init(&life->lfg, life->random_seed);
314
315
2/2
✓ Branch 0 taken 1600 times.
✓ Branch 1 taken 1 times.
1601 for (i = 0; i < life->w * life->h; i++) {
316 1600 double r = (double)av_lfg_get(&life->lfg) / UINT32_MAX;
317
2/2
✓ Branch 0 taken 171 times.
✓ Branch 1 taken 1429 times.
1600 if (r <= life->random_fill_ratio)
318 171 life->buf[0][i] = ALIVE_CELL;
319 }
320 1 life->buf_idx = 0;
321 } else {
322 ✗ if ((ret = init_pattern_from_file(ctx)) < 0)
323 ✗ return ret;
324 }
325
326
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 if (life->mold || memcmp(life-> life_color, "\xff\xff\xff", 3)
327 ✗ || memcmp(life->death_color, "\x00\x00\x00", 3)) {
328 1 life->draw = fill_picture_rgb;
329 } else {
330 ✗ life->draw = fill_picture_monoblack;
331 }
332
333 1 av_log(ctx, AV_LOG_VERBOSE,
334 "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d seed:%"PRId64"\n",
335 life->w, life->h, life->frame_rate.num, life->frame_rate.den,
336 1 life->rule_str, life->stay_rule, life->born_rule, life->stitch,
337 life->random_seed);
338 1 return 0;
339 }
340
341 1 static av_cold void uninit(AVFilterContext *ctx)
342 {
343 1 LifeContext *life = ctx->priv;
344
345 1 av_file_unmap(life->file_buf, life->file_bufsize);
346 1 av_freep(&life->rule_str);
347 1 av_freep(&life->buf[0]);
348 1 av_freep(&life->buf[1]);
349 1 }
350
351 1 static int config_props(AVFilterLink *outlink)
352 {
353 1 LifeContext *life = outlink->src->priv;
354 1 FilterLink *l = ff_filter_link(outlink);
355
356 1 outlink->w = life->w;
357 1 outlink->h = life->h;
358 1 outlink->time_base = av_inv_q(life->frame_rate);
359 1 l->frame_rate = life->frame_rate;
360
361 1 return 0;
362 }
363
364 17 static void evolve(AVFilterContext *ctx)
365 {
366 17 LifeContext *life = ctx->priv;
367 int i, j;
368 17 uint8_t *oldbuf = life->buf[ life->buf_idx];
369 17 uint8_t *newbuf = life->buf[!life->buf_idx];
370
371 enum { NW, N, NE, W, E, SW, S, SE };
372
373 /* evolve the grid */
374
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 17 times.
697 for (i = 0; i < life->h; i++) {
375
2/2
✓ Branch 0 taken 27200 times.
✓ Branch 1 taken 680 times.
27880 for (j = 0; j < life->w; j++) {
376 int pos[8][2], n, alive, cell;
377
1/2
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
27200 if (life->stitch) {
378
4/4
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 26520 times.
✓ Branch 2 taken 680 times.
✓ Branch 3 taken 26520 times.
27200 pos[NW][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NW][1] = (j-1) < 0 ? life->w-1 : j-1;
379
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 26520 times.
27200 pos[N ][0] = (i-1) < 0 ? life->h-1 : i-1; pos[N ][1] = j ;
380
4/4
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 26520 times.
✓ Branch 2 taken 26520 times.
✓ Branch 3 taken 680 times.
27200 pos[NE][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NE][1] = (j+1) == life->w ? 0 : j+1;
381
2/2
✓ Branch 0 taken 680 times.
✓ Branch 1 taken 26520 times.
27200 pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? life->w-1 : j-1;
382
2/2
✓ Branch 0 taken 26520 times.
✓ Branch 1 taken 680 times.
27200 pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? 0 : j+1;
383
4/4
✓ Branch 0 taken 26520 times.
✓ Branch 1 taken 680 times.
✓ Branch 2 taken 680 times.
✓ Branch 3 taken 26520 times.
27200 pos[SW][0] = (i+1) == life->h ? 0 : i+1; pos[SW][1] = (j-1) < 0 ? life->w-1 : j-1;
384
2/2
✓ Branch 0 taken 26520 times.
✓ Branch 1 taken 680 times.
27200 pos[S ][0] = (i+1) == life->h ? 0 : i+1; pos[S ][1] = j ;
385
4/4
✓ Branch 0 taken 26520 times.
✓ Branch 1 taken 680 times.
✓ Branch 2 taken 26520 times.
✓ Branch 3 taken 680 times.
27200 pos[SE][0] = (i+1) == life->h ? 0 : i+1; pos[SE][1] = (j+1) == life->w ? 0 : j+1;
386 } else {
387 ✗ pos[NW][0] = (i-1) < 0 ? -1 : i-1; pos[NW][1] = (j-1) < 0 ? -1 : j-1;
388 ✗ pos[N ][0] = (i-1) < 0 ? -1 : i-1; pos[N ][1] = j ;
389 ✗ pos[NE][0] = (i-1) < 0 ? -1 : i-1; pos[NE][1] = (j+1) == life->w ? -1 : j+1;
390 ✗ pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? -1 : j-1;
391 ✗ pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? -1 : j+1;
392 ✗ pos[SW][0] = (i+1) == life->h ? -1 : i+1; pos[SW][1] = (j-1) < 0 ? -1 : j-1;
393 ✗ pos[S ][0] = (i+1) == life->h ? -1 : i+1; pos[S ][1] = j ;
394 ✗ pos[SE][0] = (i+1) == life->h ? -1 : i+1; pos[SE][1] = (j+1) == life->w ? -1 : j+1;
395 }
396
397 /* compute the number of live neighbor cells */
398
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 n = (pos[NW][0] == -1 || pos[NW][1] == -1 ? 0 : oldbuf[pos[NW][0]*life->w + pos[NW][1]] == ALIVE_CELL) +
399
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 (pos[N ][0] == -1 || pos[N ][1] == -1 ? 0 : oldbuf[pos[N ][0]*life->w + pos[N ][1]] == ALIVE_CELL) +
400
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 (pos[NE][0] == -1 || pos[NE][1] == -1 ? 0 : oldbuf[pos[NE][0]*life->w + pos[NE][1]] == ALIVE_CELL) +
401
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 (pos[W ][0] == -1 || pos[W ][1] == -1 ? 0 : oldbuf[pos[W ][0]*life->w + pos[W ][1]] == ALIVE_CELL) +
402
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 (pos[E ][0] == -1 || pos[E ][1] == -1 ? 0 : oldbuf[pos[E ][0]*life->w + pos[E ][1]] == ALIVE_CELL) +
403
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 (pos[SW][0] == -1 || pos[SW][1] == -1 ? 0 : oldbuf[pos[SW][0]*life->w + pos[SW][1]] == ALIVE_CELL) +
404
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 (pos[S ][0] == -1 || pos[S ][1] == -1 ? 0 : oldbuf[pos[S ][0]*life->w + pos[S ][1]] == ALIVE_CELL) +
405
4/6
✓ Branch 0 taken 27200 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 27200 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1928 times.
✓ Branch 5 taken 25272 times.
27200 (pos[SE][0] == -1 || pos[SE][1] == -1 ? 0 : oldbuf[pos[SE][0]*life->w + pos[SE][1]] == ALIVE_CELL);
406 27200 cell = oldbuf[i*life->w + j];
407
2/2
✓ Branch 0 taken 1928 times.
✓ Branch 1 taken 25272 times.
27200 alive = 1<<n & (cell == ALIVE_CELL ? life->stay_rule : life->born_rule);
408
2/2
✓ Branch 0 taken 1877 times.
✓ Branch 1 taken 25323 times.
27200 if (alive) *newbuf = ALIVE_CELL; // new cell is alive
409
2/2
✓ Branch 0 taken 4244 times.
✓ Branch 1 taken 21079 times.
25323 else if (cell) *newbuf = cell - 1; // new cell is dead and in the process of mold
410 21079 else *newbuf = 0; // new cell is definitely dead
411 ff_dlog(ctx, "i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n", i, j, n, cell, *newbuf);
412 27200 newbuf++;
413 }
414 }
415
416 17 life->buf_idx = !life->buf_idx;
417 17 }
418
419 17 static int request_frame(AVFilterLink *outlink)
420 {
421 17 LifeContext *life = outlink->src->priv;
422 17 AVFrame *picref = ff_get_video_buffer(outlink, life->w, life->h);
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!picref)
424 ✗ return AVERROR(ENOMEM);
425 17 picref->sample_aspect_ratio = (AVRational) {1, 1};
426 17 picref->pts = life->pts++;
427 17 picref->duration = 1;
428
429 17 life->draw(outlink->src, picref);
430 17 evolve(outlink->src);
431 #ifdef DEBUG
432 show_life_grid(outlink->src);
433 #endif
434 17 return ff_filter_frame(outlink, picref);
435 }
436
437 1 static int query_formats(const AVFilterContext *ctx,
438 AVFilterFormatsConfig **cfg_in,
439 AVFilterFormatsConfig **cfg_out)
440 {
441 1 const LifeContext *life = ctx->priv;
442 1 const enum AVPixelFormat pix_fmts[] = {
443
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 life->draw == fill_picture_rgb ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_MONOBLACK,
444 AV_PIX_FMT_NONE
445 };
446
447 1 return ff_set_pixel_formats_from_list2(ctx, cfg_in, cfg_out, pix_fmts);
448 }
449
450 static const AVFilterPad life_outputs[] = {
451 {
452 .name = "default",
453 .type = AVMEDIA_TYPE_VIDEO,
454 .request_frame = request_frame,
455 .config_props = config_props,
456 },
457 };
458
459 const FFFilter ff_vsrc_life = {
460 .p.name = "life",
461 .p.description = NULL_IF_CONFIG_SMALL("Create life."),
462 .p.priv_class = &life_class,
463 .p.inputs = NULL,
464 .priv_size = sizeof(LifeContext),
465 .init = init,
466 .uninit = uninit,
467 FILTER_OUTPUTS(life_outputs),
468 FILTER_QUERY_FUNC2(query_formats),
469 };
470