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
38
54 int64_t
generation;
///< the generation number, starting from 0
58
59 #define OFFSET(x) offsetof(CellAutoContext, x)
60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61
74 {
"random_seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX,
FLAGS },
75 {
"seed",
"set the seed for filling the initial grid randomly",
OFFSET(random_seed),
AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX,
FLAGS },
80 { NULL },
81 };
82
84
85 #ifdef DEBUG
87 {
89 int i;
92 if (!line)
93 return;
94
95 for (i = 0; i < cellauto->
w; i++)
96 line[i] = row[i] ? '@' : ' ';
97 line[i] = 0;
100 }
101 #endif
102
104 {
106 char *p;
107 int i, w = 0;
108
111
113 if (w > cellauto->
w) {
115 "The specified width is %d which cannot contain the provided string width of %d\n",
118 }
119 } else {
120 /* width was not specified, set it to width of the provided row */
122 cellauto->
h = (double)cellauto->
w *
M_PHI;
123 }
124
128
129 /* fill buf */
131 for (i = (cellauto->
w - w)/2;; i++) {
133 if (*p == '\n' || !*p)
134 break;
135 else
137 }
138
139 return 0;
140 }
141
143 {
146
149 if (ret < 0)
151
152 /* create a string based on the read file */
158
160 }
161
163 {
166
169
171 av_log(ctx,
AV_LOG_ERROR,
"Only one of the filename or pattern options can be used\n");
173 }
174
178 }
else if (cellauto->
pattern) {
181 } else {
182 /* fill the first row randomly */
183 int i;
184
190
192
193 for (i = 0; i < cellauto->
w; i++) {
195 if (r <= cellauto->random_fill_ratio)
196 cellauto->
buf[i] = 1;
197 }
198 }
199
201 "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
205 return 0;
206 }
207
209 {
211
215 }
216
218 {
220
221 outlink->
w = cellauto->
w;
222 outlink->
h = cellauto->
h;
224
225 return 0;
226 }
227
229 {
234
238
239 for (i = 0; i < cellauto->
w; i++) {
241 pos[NW] = i-1 < 0 ? cellauto->
w-1 : i-1;
243 pos[NE] = i+1 == cellauto->
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 < cellauto->
w ? prev_row[i+1] : 0;
250 }
251 row[i] = !!(cellauto->
rule & (1<<
v));
252 av_dlog(ctx,
"i:%d context:%c%c%c -> cell:%d\n", i,
253 v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
254 }
255
257 }
258
260 {
262 int i, j, k, row_idx = 0;
264
266 /* show on top the oldest row */
268
269 /* fill the output picture with the whole buffer */
270 for (i = 0; i < cellauto->
h; i++) {
272 uint8_t *row = cellauto->
buf + row_idx*cellauto->
w;
274 for (k = 0, j = 0; j < cellauto->
w; j++) {
275 byte |= row[j]<<(7-k++);
276 if (k==8 || j == cellauto->
w-1) {
277 k = 0;
279 byte = 0;
280 }
281 }
282 row_idx = (row_idx + 1) % cellauto->
h;
284 }
285 }
286
288 {
291 if (!picref)
295 int i;
296 for (i = 0; i < cellauto->
h-1; i++)
298 }
301
302 picref->
pts = cellauto->
pts++;
303
304 #ifdef DEBUG
305 show_cellauto_row(outlink->
src);
306 #endif
308 }
309
311 {
314 return 0;
315 }
316
318 {
323 },
324 { NULL }
325 };
326
329 .description =
NULL_IF_CONFIG_SMALL(
"Create pattern generated by an elementary cellular automaton."),
336 .priv_class = &cellauto_class,
337 };