• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Stefano Sabatini 2011
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  * cellular automaton video source, based on Stephen Wolfram "experimentus crucis"
24  */
25 
26 /* #define DEBUG */
27 
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"
35 #include "avfilter.h"
36 #include "internal.h"
37 #include "formats.h"
38 #include "video.h"
39 
40 typedef struct CellAutoContext {
41     const AVClass *class;
42     int w, h;
43     char *filename;
44     char *rule_str;
45     uint8_t *file_buf;
46     size_t file_bufsize;
47     uint8_t *buf;
48     int buf_prev_row_idx, buf_row_idx;
49     uint8_t rule;
50     uint64_t pts;
51     AVRational frame_rate;
52     double   random_fill_ratio;
53     int64_t random_seed;
54     int stitch, scroll, start_full;
55     int64_t generation;         ///< the generation number, starting from 0
56     AVLFG lfg;
57     char *pattern;
58 } CellAutoContext;
59 
60 #define OFFSET(x) offsetof(CellAutoContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62 
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, INT_MAX, FLAGS },
69     { "r",        "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, 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_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
76     { "seed",        "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
77     { "scroll",      "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
78     { "start_full",  "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
79     { "full",        "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
80     { "stitch",      "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_BOOL,    {.i64 = 1},   0, 1, FLAGS },
81     { NULL }
82 };
83 
84 AVFILTER_DEFINE_CLASS(cellauto);
85 
86 #ifdef DEBUG
show_cellauto_row(AVFilterContext * ctx)87 static void show_cellauto_row(AVFilterContext *ctx)
88 {
89     CellAutoContext *s = ctx->priv;
90     int i;
91     uint8_t *row = s->buf + s->w * s->buf_row_idx;
92     char *line = av_malloc(s->w + 1);
93     if (!line)
94         return;
95 
96     for (i = 0; i < s->w; i++)
97         line[i] = row[i] ? '@' : ' ';
98     line[i] = 0;
99     av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", s->generation, line);
100     av_free(line);
101 }
102 #endif
103 
init_pattern_from_string(AVFilterContext * ctx)104 static int init_pattern_from_string(AVFilterContext *ctx)
105 {
106     CellAutoContext *s = ctx->priv;
107     char *p;
108     int i, w = 0;
109 
110     w = strlen(s->pattern);
111     av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
112 
113     if (s->w) {
114         if (w > s->w) {
115             av_log(ctx, AV_LOG_ERROR,
116                    "The specified width is %d which cannot contain the provided string width of %d\n",
117                    s->w, w);
118             return AVERROR(EINVAL);
119         }
120     } else {
121         /* width was not specified, set it to width of the provided row */
122         s->w = w;
123         s->h = (double)s->w * M_PHI;
124     }
125 
126     s->buf = av_calloc(s->w, s->h * sizeof(*s->buf));
127     if (!s->buf)
128         return AVERROR(ENOMEM);
129 
130     /* fill buf */
131     p = s->pattern;
132     for (i = (s->w - w)/2;; i++) {
133         av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p);
134         if (*p == '\n' || !*p)
135             break;
136         else
137             s->buf[i] = !!av_isgraph(*(p++));
138     }
139 
140     return 0;
141 }
142 
init_pattern_from_file(AVFilterContext * ctx)143 static int init_pattern_from_file(AVFilterContext *ctx)
144 {
145     CellAutoContext *s = ctx->priv;
146     int ret;
147 
148     ret = av_file_map(s->filename,
149                       &s->file_buf, &s->file_bufsize, 0, ctx);
150     if (ret < 0)
151         return ret;
152 
153     /* create a string based on the read file */
154     s->pattern = av_malloc(s->file_bufsize + 1);
155     if (!s->pattern)
156         return AVERROR(ENOMEM);
157     memcpy(s->pattern, s->file_buf, s->file_bufsize);
158     s->pattern[s->file_bufsize] = 0;
159 
160     return init_pattern_from_string(ctx);
161 }
162 
init(AVFilterContext * ctx)163 static av_cold int init(AVFilterContext *ctx)
164 {
165     CellAutoContext *s = ctx->priv;
166     int ret;
167 
168     if (!s->w && !s->filename && !s->pattern)
169         av_opt_set(s, "size", "320x518", 0);
170 
171     if (s->filename && s->pattern) {
172         av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
173         return AVERROR(EINVAL);
174     }
175 
176     if (s->filename) {
177         if ((ret = init_pattern_from_file(ctx)) < 0)
178             return ret;
179     } else if (s->pattern) {
180         if ((ret = init_pattern_from_string(ctx)) < 0)
181             return ret;
182     } else {
183         /* fill the first row randomly */
184         int i;
185 
186         s->buf = av_calloc(s->w, s->h * sizeof(*s->buf));
187         if (!s->buf)
188             return AVERROR(ENOMEM);
189         if (s->random_seed == -1)
190             s->random_seed = av_get_random_seed();
191 
192         av_lfg_init(&s->lfg, s->random_seed);
193 
194         for (i = 0; i < s->w; i++) {
195             double r = (double)av_lfg_get(&s->lfg) / UINT32_MAX;
196             if (r <= s->random_fill_ratio)
197                 s->buf[i] = 1;
198         }
199     }
200 
201     av_log(ctx, AV_LOG_VERBOSE,
202            "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%"PRId64"\n",
203            s->w, s->h, s->frame_rate.num, s->frame_rate.den,
204            s->rule, s->stitch, s->scroll, s->start_full,
205            s->random_seed);
206     return 0;
207 }
208 
uninit(AVFilterContext * ctx)209 static av_cold void uninit(AVFilterContext *ctx)
210 {
211     CellAutoContext *s = ctx->priv;
212 
213     av_file_unmap(s->file_buf, s->file_bufsize);
214     av_freep(&s->buf);
215     av_freep(&s->pattern);
216 }
217 
config_props(AVFilterLink * outlink)218 static int config_props(AVFilterLink *outlink)
219 {
220     CellAutoContext *s = outlink->src->priv;
221 
222     outlink->w = s->w;
223     outlink->h = s->h;
224     outlink->time_base = av_inv_q(s->frame_rate);
225     outlink->frame_rate = s->frame_rate;
226 
227     return 0;
228 }
229 
evolve(AVFilterContext * ctx)230 static void evolve(AVFilterContext *ctx)
231 {
232     CellAutoContext *s = ctx->priv;
233     int i, v, pos[3];
234     uint8_t *row, *prev_row = s->buf + s->buf_row_idx * s->w;
235     enum { NW, N, NE };
236 
237     s->buf_prev_row_idx = s->buf_row_idx;
238     s->buf_row_idx      = s->buf_row_idx == s->h-1 ? 0 : s->buf_row_idx+1;
239     row = s->buf + s->w * s->buf_row_idx;
240 
241     for (i = 0; i < s->w; i++) {
242         if (s->stitch) {
243             pos[NW] = i-1 < 0 ? s->w-1 : i-1;
244             pos[N]  = i;
245             pos[NE] = i+1 == s->w ? 0  : i+1;
246             v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
247         } else {
248             v = 0;
249             v|= i-1 >= 0          ? prev_row[i-1]<<2 : 0;
250             v|=                     prev_row[i  ]<<1    ;
251             v|= i+1 < s->w ? prev_row[i+1]    : 0;
252         }
253         row[i] = !!(s->rule & (1<<v));
254         ff_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
255                 v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
256     }
257 
258     s->generation++;
259 }
260 
fill_picture(AVFilterContext * ctx,AVFrame * picref)261 static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
262 {
263     CellAutoContext *s = ctx->priv;
264     int i, j, k, row_idx = 0;
265     uint8_t *p0 = picref->data[0];
266 
267     if (s->scroll && s->generation >= s->h)
268         /* show on top the oldest row */
269         row_idx = (s->buf_row_idx + 1) % s->h;
270 
271     /* fill the output picture with the whole buffer */
272     for (i = 0; i < s->h; i++) {
273         uint8_t byte = 0;
274         uint8_t *row = s->buf + row_idx*s->w;
275         uint8_t *p = p0;
276         for (k = 0, j = 0; j < s->w; j++) {
277             byte |= row[j]<<(7-k++);
278             if (k==8 || j == s->w-1) {
279                 k = 0;
280                 *p++ = byte;
281                 byte = 0;
282             }
283         }
284         row_idx = (row_idx + 1) % s->h;
285         p0 += picref->linesize[0];
286     }
287 }
288 
request_frame(AVFilterLink * outlink)289 static int request_frame(AVFilterLink *outlink)
290 {
291     CellAutoContext *s = outlink->src->priv;
292     AVFrame *picref = ff_get_video_buffer(outlink, s->w, s->h);
293     if (!picref)
294         return AVERROR(ENOMEM);
295     picref->sample_aspect_ratio = (AVRational) {1, 1};
296     if (s->generation == 0 && s->start_full) {
297         int i;
298         for (i = 0; i < s->h-1; i++)
299             evolve(outlink->src);
300     }
301     fill_picture(outlink->src, picref);
302     evolve(outlink->src);
303 
304     picref->pts = s->pts++;
305 
306 #ifdef DEBUG
307     show_cellauto_row(outlink->src);
308 #endif
309     return ff_filter_frame(outlink, picref);
310 }
311 
312 static const AVFilterPad cellauto_outputs[] = {
313     {
314         .name          = "default",
315         .type          = AVMEDIA_TYPE_VIDEO,
316         .request_frame = request_frame,
317         .config_props  = config_props,
318     },
319 };
320 
321 const AVFilter ff_vsrc_cellauto = {
322     .name          = "cellauto",
323     .description   = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
324     .priv_size     = sizeof(CellAutoContext),
325     .priv_class    = &cellauto_class,
326     .init          = init,
327     .uninit        = uninit,
328     .inputs        = NULL,
329     FILTER_OUTPUTS(cellauto_outputs),
330     FILTER_SINGLE_PIXFMT(AV_PIX_FMT_MONOBLACK),
331 };
332