2 * Copyright (c) Stefano Sabatini 2010
4 * This file is part of FFmpeg.
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.
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.
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
23 * life video source, based on John Conways' Life Game
28 #include "libavutil/file.h"
29 #include "libavutil/lfg.h"
30 #include "libavutil/opt.h"
31 #include "libavutil/parseutils.h"
32 #include "libavutil/random_seed.h"
44 uint16_t stay_rule; ///< encode the behavior for filled cells
45 uint16_t born_rule; ///< encode the behavior for empty cells
48 char *size; ///< video frame size
49 char *rate; ///< video frame rate
50 double random_fill_ratio;
56 #define OFFSET(x) offsetof(LifeContext, x)
58 static const AVOption life_options[] = {
59 { "filename", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
60 { "f", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
61 { "size", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
62 { "s", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
63 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
64 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
65 { "rule", "set rule", OFFSET(rule_str), AV_OPT_TYPE_STRING, {.str = "B3/S23"}, CHAR_MIN, CHAR_MAX },
66 { "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 },
67 { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1/M_PHI}, 0, 1 },
68 { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.dbl=-1}, -1, UINT32_MAX },
69 { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.dbl=-1}, -1, UINT32_MAX },
70 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
74 static const char *life_get_name(void *ctx)
79 static const AVClass life_class = {
85 static int parse_rule(uint16_t *born_rule, uint16_t *stay_rule,
86 const char *rule_str, void *log_ctx)
89 const char *p = rule_str;
93 if (strchr("bBsS", *p)) {
94 /* parse rule as a Born / Stay Alive code, see
95 * http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life */
97 uint16_t *rule = (*p == 'b' || *p == 'B') ? born_rule : stay_rule;
99 while (*p >= '0' && *p <= '8') {
100 *rule += 1<<(*p - '0');
106 } while (strchr("bBsS", *p));
111 /* parse rule as a number, expressed in the form STAY|(BORN<<9),
112 * where STAY and BORN encode the corresponding 9-bits rule */
113 long int rule = strtol(rule_str, &tail, 10);
116 *born_rule = ((1<<9)-1) & rule;
117 *stay_rule = rule >> 9;
123 av_log(log_ctx, AV_LOG_ERROR, "Invalid rule code '%s' provided\n", rule_str);
124 return AVERROR(EINVAL);
128 static void show_life_grid(AVFilterContext *ctx)
130 LifeContext *life = ctx->priv;
133 char *line = av_malloc(life->w + 1);
136 for (i = 0; i < life->h; i++) {
137 for (j = 0; j < life->w; j++)
138 line[j] = life->buf[life->buf_idx][i*life->w + j] ? '@' : ' ';
140 av_log(ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
146 static int init_pattern_from_file(AVFilterContext *ctx)
148 LifeContext *life = ctx->priv;
150 int ret, i, i0, j, h = 0, w, max_w = 0;
152 if ((ret = av_file_map(life->filename, &life->file_buf, &life->file_bufsize,
156 /* prescan file to get the number of lines and the maximum width */
158 for (i = 0; i < life->file_bufsize; i++) {
159 if (life->file_buf[i] == '\n') {
160 h++; max_w = FFMAX(w, max_w); w = 0;
165 av_log(ctx, AV_LOG_DEBUG, "h:%d max_w:%d\n", h, max_w);
168 if (max_w > life->w || h > life->h) {
169 av_log(ctx, AV_LOG_ERROR,
170 "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
171 life->w, life->h, max_w, h);
172 return AVERROR(EINVAL);
175 /* size was not specified, set it to size of the grid */
180 if (!(life->buf[0] = av_mallocz(sizeof(char) * life->h * life->w)) ||
181 !(life->buf[1] = av_mallocz(sizeof(char) * life->h * life->w))) {
182 av_free(life->buf[0]);
183 av_free(life->buf[1]);
184 return AVERROR(ENOMEM);
189 for (i0 = 0, i = (life->h - h)/2; i0 < h; i0++, i++) {
190 for (j = (life->w - max_w)/2;; j++) {
191 av_log(ctx, AV_LOG_DEBUG, "%d:%d %c\n", i, j, *p == '\n' ? 'N' : *p);
195 life->buf[0][i*life->w + j] = !!isgraph(*(p++));
203 static int init(AVFilterContext *ctx, const char *args, void *opaque)
205 LifeContext *life = ctx->priv;
206 AVRational frame_rate;
209 life->class = &life_class;
210 av_opt_set_defaults(life);
212 if ((ret = av_set_options_string(life, args, "=", ":")) < 0) {
213 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
217 if ((ret = av_parse_video_rate(&frame_rate, life->rate)) < 0) {
218 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", life->rate);
219 return AVERROR(EINVAL);
222 if (!life->size && !life->filename)
223 av_opt_set(life, "size", "320x240", 0);
226 (ret = av_parse_video_size(&life->w, &life->h, life->size)) < 0) {
227 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", life->size);
231 if ((ret = parse_rule(&life->born_rule, &life->stay_rule, life->rule_str, ctx)) < 0)
234 life->time_base.num = frame_rate.den;
235 life->time_base.den = frame_rate.num;
237 if (!life->filename) {
238 /* fill the grid randomly */
241 if (!(life->buf[0] = av_mallocz(sizeof(char) * life->h * life->w)) ||
242 !(life->buf[1] = av_mallocz(sizeof(char) * life->h * life->w))) {
243 av_free(life->buf[0]);
244 av_free(life->buf[1]);
245 return AVERROR(ENOMEM);
247 if (life->random_seed == -1)
248 life->random_seed = av_get_random_seed();
250 av_lfg_init(&life->lfg, life->random_seed);
252 for (i = 0; i < life->w * life->h; i++) {
253 double r = (double)av_lfg_get(&life->lfg) / UINT32_MAX;
254 if (r <= life->random_fill_ratio)
259 if ((ret = init_pattern_from_file(ctx)) < 0)
263 av_log(ctx, AV_LOG_INFO,
264 "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d\n",
265 life->w, life->h, frame_rate.num, frame_rate.den,
266 life->rule_str, life->stay_rule, life->born_rule, life->stitch);
270 static av_cold void uninit(AVFilterContext *ctx)
272 LifeContext *life = ctx->priv;
274 av_file_unmap(life->file_buf, life->file_bufsize);
275 av_freep(&life->rule_str);
276 av_freep(&life->buf[0]);
277 av_freep(&life->buf[1]);
280 static int config_props(AVFilterLink *outlink)
282 LifeContext *life = outlink->src->priv;
284 outlink->w = life->w;
285 outlink->h = life->h;
286 outlink->time_base = life->time_base;
291 static void evolve(AVFilterContext *ctx)
293 LifeContext *life = ctx->priv;
295 uint8_t *oldbuf = life->buf[ life->buf_idx];
296 uint8_t *newbuf = life->buf[!life->buf_idx];
298 enum { NW, N, NE, W, E, SW, S, SE };
300 /* evolve the grid */
301 for (i = 0; i < life->h; i++) {
302 for (j = 0; j < life->w; j++) {
305 pos[NW][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NW][1] = (j-1) < 0 ? life->w-1 : j-1;
306 pos[N ][0] = (i-1) < 0 ? life->h-1 : i-1; pos[N ][1] = j ;
307 pos[NE][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NE][1] = (j+1) == life->w ? 0 : j+1;
308 pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? life->w-1 : j-1;
309 pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? 0 : j+1;
310 pos[SW][0] = (i+1) == life->h ? 0 : i+1; pos[SW][1] = (j-1) < 0 ? life->w-1 : j-1;
311 pos[S ][0] = (i+1) == life->h ? 0 : i+1; pos[S ][1] = j ;
312 pos[SE][0] = (i+1) == life->h ? 0 : i+1; pos[SE][1] = (j+1) == life->w ? 0 : j+1;
314 pos[NW][0] = (i-1) < 0 ? -1 : i-1; pos[NW][1] = (j-1) < 0 ? -1 : j-1;
315 pos[N ][0] = (i-1) < 0 ? -1 : i-1; pos[N ][1] = j ;
316 pos[NE][0] = (i-1) < 0 ? -1 : i-1; pos[NE][1] = (j+1) == life->w ? -1 : j+1;
317 pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? -1 : j-1;
318 pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? -1 : j+1;
319 pos[SW][0] = (i+1) == life->h ? -1 : i+1; pos[SW][1] = (j-1) < 0 ? -1 : j-1;
320 pos[S ][0] = (i+1) == life->h ? -1 : i+1; pos[S ][1] = j ;
321 pos[SE][0] = (i+1) == life->h ? -1 : i+1; pos[SE][1] = (j+1) == life->w ? -1 : j+1;
324 /* compute the number of live neighbor cells */
325 n = (pos[NW][0] == -1 || pos[NW][1] == -1 ? 0 : oldbuf[pos[NW][0]*life->w + pos[NW][1]]) +
326 (pos[N ][0] == -1 || pos[N ][1] == -1 ? 0 : oldbuf[pos[N ][0]*life->w + pos[N ][1]]) +
327 (pos[NE][0] == -1 || pos[NE][1] == -1 ? 0 : oldbuf[pos[NE][0]*life->w + pos[NE][1]]) +
328 (pos[W ][0] == -1 || pos[W ][1] == -1 ? 0 : oldbuf[pos[W ][0]*life->w + pos[W ][1]]) +
329 (pos[E ][0] == -1 || pos[E ][1] == -1 ? 0 : oldbuf[pos[E ][0]*life->w + pos[E ][1]]) +
330 (pos[SW][0] == -1 || pos[SW][1] == -1 ? 0 : oldbuf[pos[SW][0]*life->w + pos[SW][1]]) +
331 (pos[S ][0] == -1 || pos[S ][1] == -1 ? 0 : oldbuf[pos[S ][0]*life->w + pos[S ][1]]) +
332 (pos[SE][0] == -1 || pos[SE][1] == -1 ? 0 : oldbuf[pos[SE][0]*life->w + pos[SE][1]]);
333 v = !!(1<<n & (oldbuf[i*life->w + j] ? life->stay_rule : life->born_rule));
334 av_dlog(ctx, "i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n", i, j, n, oldbuf[i*life->w + j], v);
335 newbuf[i*life->w+j] = v;
339 life->buf_idx = !life->buf_idx;
342 static void fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
344 LifeContext *life = ctx->priv;
345 uint8_t *buf = life->buf[life->buf_idx];
348 /* fill the output picture with the old grid buffer */
349 for (i = 0; i < life->h; i++) {
351 uint8_t *p = picref->data[0] + i * picref->linesize[0];
352 for (k = 0, j = 0; j < life->w; j++) {
353 byte |= buf[i*life->w+j]<<(7-k++);
354 if (k==8 || j == life->w-1) {
363 static int request_frame(AVFilterLink *outlink)
365 LifeContext *life = outlink->src->priv;
366 AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, life->w, life->h);
367 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
368 picref->pts = life->pts++;
371 fill_picture(outlink->src, picref);
372 evolve(outlink->src);
374 show_life_grid(outlink->src);
377 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
378 avfilter_draw_slice(outlink, 0, life->h, 1);
379 avfilter_end_frame(outlink);
380 avfilter_unref_buffer(picref);
385 static int query_formats(AVFilterContext *ctx)
387 static const enum PixelFormat pix_fmts[] = { PIX_FMT_MONOBLACK, PIX_FMT_NONE };
388 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
392 AVFilter avfilter_vsrc_life = {
394 .description = NULL_IF_CONFIG_SMALL("Create life."),
395 .priv_size = sizeof(LifeContext),
398 .query_formats = query_formats,
400 .inputs = (const AVFilterPad[]) {
403 .outputs = (const AVFilterPad[]) {
405 .type = AVMEDIA_TYPE_VIDEO,
406 .request_frame = request_frame,
407 .config_props = config_props },