2 * Copyright (c) Stefano Sabatini 2011
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 * cellular automaton video source, based on Stephen Wolfram "experimentus crucis"
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"
43 int buf_prev_row_idx, buf_row_idx;
47 char *rate; ///< video frame rate
48 double random_fill_ratio;
50 int stitch, scroll, start_full;
51 int64_t generation; ///< the generation number, starting from 0
56 #define OFFSET(x) offsetof(CellAutoContext, x)
58 static const AVOption cellauto_options[] = {
59 { "filename", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
60 { "f", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
61 { "pattern", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
62 { "p", "set initial pattern", OFFSET(pattern), 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 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0 },
66 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0 },
67 { "rule", "set rule", OFFSET(rule), AV_OPT_TYPE_INT, {.dbl = 110}, 0, 255 },
68 { "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 },
69 { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl = 1/M_PHI}, 0, 1 },
70 { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.dbl = -1}, -1, UINT32_MAX },
71 { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.dbl = -1}, -1, UINT32_MAX },
72 { "scroll", "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1 },
73 { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.dbl = 0}, 0, 1 },
74 { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1 },
75 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.dbl = 1}, 0, 1 },
79 static const char *cellauto_get_name(void *ctx)
84 static const AVClass cellauto_class = {
91 static void show_cellauto_row(AVFilterContext *ctx)
93 CellAutoContext *cellauto = ctx->priv;
95 uint8_t *row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
96 char *line = av_malloc(cellauto->w + 1);
100 for (i = 0; i < cellauto->w; i++)
101 line[i] = row[i] ? '@' : ' ';
103 av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", cellauto->generation, line);
108 static int init_pattern_from_string(AVFilterContext *ctx)
110 CellAutoContext *cellauto = ctx->priv;
114 w = strlen(cellauto->pattern);
115 av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
118 if (w > cellauto->w) {
119 av_log(ctx, AV_LOG_ERROR,
120 "The specified width is %d which cannot contain the provided string width of %d\n",
122 return AVERROR(EINVAL);
125 /* width was not specified, set it to width of the provided row */
127 cellauto->h = (double)cellauto->w * M_PHI;
130 cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
132 return AVERROR(ENOMEM);
135 p = cellauto->pattern;
136 for (i = (cellauto->w - w)/2;; i++) {
137 av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p);
138 if (*p == '\n' || !*p)
141 cellauto->buf[i] = !!isgraph(*(p++));
147 static int init_pattern_from_file(AVFilterContext *ctx)
149 CellAutoContext *cellauto = ctx->priv;
152 ret = av_file_map(cellauto->filename,
153 &cellauto->file_buf, &cellauto->file_bufsize, 0, ctx);
157 /* create a string based on the read file */
158 cellauto->pattern = av_malloc(cellauto->file_bufsize + 1);
159 if (!cellauto->pattern)
160 return AVERROR(ENOMEM);
161 memcpy(cellauto->pattern, cellauto->file_buf, cellauto->file_bufsize);
162 cellauto->pattern[cellauto->file_bufsize] = 0;
164 return init_pattern_from_string(ctx);
167 static int init(AVFilterContext *ctx, const char *args, void *opaque)
169 CellAutoContext *cellauto = ctx->priv;
170 AVRational frame_rate;
173 cellauto->class = &cellauto_class;
174 av_opt_set_defaults(cellauto);
176 if ((ret = av_set_options_string(cellauto, args, "=", ":")) < 0) {
177 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
181 if ((ret = av_parse_video_rate(&frame_rate, cellauto->rate)) < 0) {
182 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", cellauto->rate);
183 return AVERROR(EINVAL);
186 if (!cellauto->w && !cellauto->filename && !cellauto->pattern)
187 av_opt_set(cellauto, "size", "320x518", 0);
189 cellauto->time_base.num = frame_rate.den;
190 cellauto->time_base.den = frame_rate.num;
192 if (cellauto->filename && cellauto->pattern) {
193 av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
194 return AVERROR(EINVAL);
197 if (cellauto->filename) {
198 if ((ret = init_pattern_from_file(ctx)) < 0)
200 } else if (cellauto->pattern) {
201 if ((ret = init_pattern_from_string(ctx)) < 0)
204 /* fill the first row randomly */
207 cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
209 return AVERROR(ENOMEM);
210 if (cellauto->random_seed == -1)
211 cellauto->random_seed = av_get_random_seed();
213 av_lfg_init(&cellauto->lfg, cellauto->random_seed);
215 for (i = 0; i < cellauto->w; i++) {
216 double r = (double)av_lfg_get(&cellauto->lfg) / UINT32_MAX;
217 if (r <= cellauto->random_fill_ratio)
218 cellauto->buf[i] = 1;
222 av_log(ctx, AV_LOG_INFO,
223 "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
224 cellauto->w, cellauto->h, frame_rate.num, frame_rate.den,
225 cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full,
226 cellauto->random_seed);
230 static av_cold void uninit(AVFilterContext *ctx)
232 CellAutoContext *cellauto = ctx->priv;
234 av_file_unmap(cellauto->file_buf, cellauto->file_bufsize);
235 av_freep(&cellauto->buf);
236 av_freep(&cellauto->pattern);
239 static int config_props(AVFilterLink *outlink)
241 CellAutoContext *cellauto = outlink->src->priv;
243 outlink->w = cellauto->w;
244 outlink->h = cellauto->h;
245 outlink->time_base = cellauto->time_base;
250 static void evolve(AVFilterContext *ctx)
252 CellAutoContext *cellauto = ctx->priv;
254 uint8_t *row, *prev_row = cellauto->buf + cellauto->buf_row_idx * cellauto->w;
257 cellauto->buf_prev_row_idx = cellauto->buf_row_idx;
258 cellauto->buf_row_idx = cellauto->buf_row_idx == cellauto->h-1 ? 0 : cellauto->buf_row_idx+1;
259 row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
261 for (i = 0; i < cellauto->w; i++) {
262 if (cellauto->stitch) {
263 pos[NW] = i-1 < 0 ? cellauto->w-1 : i-1;
265 pos[NE] = i+1 == cellauto->w ? 0 : i+1;
266 v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
269 v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
270 v|= prev_row[i ]<<1 ;
271 v|= i+1 < cellauto->w ? prev_row[i+1] : 0;
273 row[i] = !!(cellauto->rule & (1<<v));
274 av_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
275 v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
278 cellauto->generation++;
281 static void fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
283 CellAutoContext *cellauto = ctx->priv;
284 int i, j, k, row_idx = 0;
285 uint8_t *p0 = picref->data[0];
287 if (cellauto->scroll && cellauto->generation >= cellauto->h)
288 /* show on top the oldest row */
289 row_idx = (cellauto->buf_row_idx + 1) % cellauto->h;
291 /* fill the output picture with the whole buffer */
292 for (i = 0; i < cellauto->h; i++) {
294 uint8_t *row = cellauto->buf + row_idx*cellauto->w;
296 for (k = 0, j = 0; j < cellauto->w; j++) {
297 byte |= row[j]<<(7-k++);
298 if (k==8 || j == cellauto->w-1) {
304 row_idx = (row_idx + 1) % cellauto->h;
305 p0 += picref->linesize[0];
309 static int request_frame(AVFilterLink *outlink)
311 CellAutoContext *cellauto = outlink->src->priv;
312 AVFilterBufferRef *picref =
313 avfilter_get_video_buffer(outlink, AV_PERM_WRITE, cellauto->w, cellauto->h);
314 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
315 if (cellauto->generation == 0 && cellauto->start_full) {
317 for (i = 0; i < cellauto->h-1; i++)
318 evolve(outlink->src);
320 fill_picture(outlink->src, picref);
321 evolve(outlink->src);
323 picref->pts = cellauto->pts++;
327 show_cellauto_row(outlink->src);
330 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
331 avfilter_draw_slice(outlink, 0, cellauto->h, 1);
332 avfilter_end_frame(outlink);
333 avfilter_unref_buffer(picref);
338 static int query_formats(AVFilterContext *ctx)
340 static const enum PixelFormat pix_fmts[] = { PIX_FMT_MONOBLACK, PIX_FMT_NONE };
341 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
345 AVFilter avfilter_vsrc_cellauto = {
347 .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
348 .priv_size = sizeof(CellAutoContext),
351 .query_formats = query_formats,
353 .inputs = (const AVFilterPad[]) {
356 .outputs = (const AVFilterPad[]) {
358 .type = AVMEDIA_TYPE_VIDEO,
359 .request_frame = request_frame,
360 .config_props = config_props },