FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/vsrc_cellauto.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 0 122 0.0%
Functions: 0 8 0.0%
Branches: 0 76 0.0%

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