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/internal.h"
30 #include "libavutil/lfg.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/random_seed.h"
34 #include "libavutil/avstring.h"
48 int buf_prev_row_idx, buf_row_idx;
51 AVRational frame_rate;
52 double random_fill_ratio;
54 int stitch, scroll, start_full;
55 int64_t generation; ///< the generation number, starting from 0
60 #define OFFSET(x) offsetof(CellAutoContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
63 static const AVOption cellauto_options[] = {
64 { "filename", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
65 { "f", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
66 { "pattern", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
67 { "p", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
68 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
69 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
70 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
71 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
72 { "rule", "set rule", OFFSET(rule), AV_OPT_TYPE_INT, {.i64 = 110}, 0, 255, FLAGS },
73 { "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 },
74 { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl = 1/M_PHI}, 0, 1, FLAGS },
75 { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
76 { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
77 { "scroll", "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
78 { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
79 { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
80 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
84 AVFILTER_DEFINE_CLASS(cellauto);
87 static void show_cellauto_row(AVFilterContext *ctx)
89 CellAutoContext *cellauto = ctx->priv;
91 uint8_t *row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
92 char *line = av_malloc(cellauto->w + 1);
96 for (i = 0; i < cellauto->w; i++)
97 line[i] = row[i] ? '@' : ' ';
99 av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", cellauto->generation, line);
104 static int init_pattern_from_string(AVFilterContext *ctx)
106 CellAutoContext *cellauto = ctx->priv;
110 w = strlen(cellauto->pattern);
111 av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
114 if (w > cellauto->w) {
115 av_log(ctx, AV_LOG_ERROR,
116 "The specified width is %d which cannot contain the provided string width of %d\n",
118 return AVERROR(EINVAL);
121 /* width was not specified, set it to width of the provided row */
123 cellauto->h = (double)cellauto->w * M_PHI;
126 cellauto->buf = av_mallocz_array(sizeof(uint8_t) * cellauto->w, cellauto->h);
128 return AVERROR(ENOMEM);
131 p = cellauto->pattern;
132 for (i = (cellauto->w - w)/2;; i++) {
133 av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p);
134 if (*p == '\n' || !*p)
137 cellauto->buf[i] = !!av_isgraph(*(p++));
143 static int init_pattern_from_file(AVFilterContext *ctx)
145 CellAutoContext *cellauto = ctx->priv;
148 ret = av_file_map(cellauto->filename,
149 &cellauto->file_buf, &cellauto->file_bufsize, 0, ctx);
153 /* create a string based on the read file */
154 cellauto->pattern = av_malloc(cellauto->file_bufsize + 1);
155 if (!cellauto->pattern)
156 return AVERROR(ENOMEM);
157 memcpy(cellauto->pattern, cellauto->file_buf, cellauto->file_bufsize);
158 cellauto->pattern[cellauto->file_bufsize] = 0;
160 return init_pattern_from_string(ctx);
163 static av_cold int init(AVFilterContext *ctx)
165 CellAutoContext *cellauto = ctx->priv;
168 if (!cellauto->w && !cellauto->filename && !cellauto->pattern)
169 av_opt_set(cellauto, "size", "320x518", 0);
171 if (cellauto->filename && cellauto->pattern) {
172 av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
173 return AVERROR(EINVAL);
176 if (cellauto->filename) {
177 if ((ret = init_pattern_from_file(ctx)) < 0)
179 } else if (cellauto->pattern) {
180 if ((ret = init_pattern_from_string(ctx)) < 0)
183 /* fill the first row randomly */
186 cellauto->buf = av_mallocz_array(sizeof(uint8_t) * cellauto->w, cellauto->h);
188 return AVERROR(ENOMEM);
189 if (cellauto->random_seed == -1)
190 cellauto->random_seed = av_get_random_seed();
192 av_lfg_init(&cellauto->lfg, cellauto->random_seed);
194 for (i = 0; i < cellauto->w; i++) {
195 double r = (double)av_lfg_get(&cellauto->lfg) / UINT32_MAX;
196 if (r <= cellauto->random_fill_ratio)
197 cellauto->buf[i] = 1;
201 av_log(ctx, AV_LOG_VERBOSE,
202 "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
203 cellauto->w, cellauto->h, cellauto->frame_rate.num, cellauto->frame_rate.den,
204 cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full,
205 cellauto->random_seed);
209 static av_cold void uninit(AVFilterContext *ctx)
211 CellAutoContext *cellauto = ctx->priv;
213 av_file_unmap(cellauto->file_buf, cellauto->file_bufsize);
214 av_freep(&cellauto->buf);
215 av_freep(&cellauto->pattern);
218 static int config_props(AVFilterLink *outlink)
220 CellAutoContext *cellauto = outlink->src->priv;
222 outlink->w = cellauto->w;
223 outlink->h = cellauto->h;
224 outlink->time_base = av_inv_q(cellauto->frame_rate);
229 static void evolve(AVFilterContext *ctx)
231 CellAutoContext *cellauto = ctx->priv;
233 uint8_t *row, *prev_row = cellauto->buf + cellauto->buf_row_idx * cellauto->w;
236 cellauto->buf_prev_row_idx = cellauto->buf_row_idx;
237 cellauto->buf_row_idx = cellauto->buf_row_idx == cellauto->h-1 ? 0 : cellauto->buf_row_idx+1;
238 row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
240 for (i = 0; i < cellauto->w; i++) {
241 if (cellauto->stitch) {
242 pos[NW] = i-1 < 0 ? cellauto->w-1 : i-1;
244 pos[NE] = i+1 == cellauto->w ? 0 : i+1;
245 v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
248 v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
249 v|= prev_row[i ]<<1 ;
250 v|= i+1 < cellauto->w ? prev_row[i+1] : 0;
252 row[i] = !!(cellauto->rule & (1<<v));
253 ff_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
254 v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
257 cellauto->generation++;
260 static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
262 CellAutoContext *cellauto = ctx->priv;
263 int i, j, k, row_idx = 0;
264 uint8_t *p0 = picref->data[0];
266 if (cellauto->scroll && cellauto->generation >= cellauto->h)
267 /* show on top the oldest row */
268 row_idx = (cellauto->buf_row_idx + 1) % cellauto->h;
270 /* fill the output picture with the whole buffer */
271 for (i = 0; i < cellauto->h; i++) {
273 uint8_t *row = cellauto->buf + row_idx*cellauto->w;
275 for (k = 0, j = 0; j < cellauto->w; j++) {
276 byte |= row[j]<<(7-k++);
277 if (k==8 || j == cellauto->w-1) {
283 row_idx = (row_idx + 1) % cellauto->h;
284 p0 += picref->linesize[0];
288 static int request_frame(AVFilterLink *outlink)
290 CellAutoContext *cellauto = outlink->src->priv;
291 AVFrame *picref = ff_get_video_buffer(outlink, cellauto->w, cellauto->h);
293 return AVERROR(ENOMEM);
294 picref->sample_aspect_ratio = (AVRational) {1, 1};
295 if (cellauto->generation == 0 && cellauto->start_full) {
297 for (i = 0; i < cellauto->h-1; i++)
298 evolve(outlink->src);
300 fill_picture(outlink->src, picref);
301 evolve(outlink->src);
303 picref->pts = cellauto->pts++;
306 show_cellauto_row(outlink->src);
308 return ff_filter_frame(outlink, picref);
311 static int query_formats(AVFilterContext *ctx)
313 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE };
314 AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
316 return AVERROR(ENOMEM);
317 return ff_set_common_formats(ctx, fmts_list);
320 static const AVFilterPad cellauto_outputs[] = {
323 .type = AVMEDIA_TYPE_VIDEO,
324 .request_frame = request_frame,
325 .config_props = config_props,
330 AVFilter ff_vsrc_cellauto = {
332 .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
333 .priv_size = sizeof(CellAutoContext),
334 .priv_class = &cellauto_class,
337 .query_formats = query_formats,
339 .outputs = cellauto_outputs,