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
39
55 int64_t
generation;
///< the generation number, starting from 0
59
60 #define OFFSET(x) offsetof(CellAutoContext, x)
61 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
62
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 },
82 };
83
85
86 #ifdef DEBUG
88 {
90 int i;
93 if (!line)
94 return;
95
96 for (i = 0; i < s->
w; i++)
97 line[i] = row[i] ? '@' : ' ';
98 line[i] = 0;
101 }
102 #endif
103
105 {
107 char *p;
108 int i, w = 0;
109
112
116 "The specified width is %d which cannot contain the provided string width of %d\n",
119 }
120 } else {
121 /* width was not specified, set it to width of the provided row */
124 }
125
129
130 /* fill buf */
132 for (i = (s->
w - w)/2;; i++) {
134 if (*p == '\n' || !*p)
135 break;
136 else
138 }
139
140 return 0;
141 }
142
144 {
146 int ret;
147
150 if (ret < 0)
151 return ret;
152
153 /* create a string based on the read file */
159
161 }
162
164 {
166 int ret;
167
170
172 av_log(ctx,
AV_LOG_ERROR,
"Only one of the filename or pattern options can be used\n");
174 }
175
178 return ret;
181 return ret;
182 } else {
183 /* fill the first row randomly */
184 int i;
185
191
193
194 for (i = 0; i < s->
w; i++) {
196 if (r <= s->random_fill_ratio)
198 }
199 }
200
202 "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
206 return 0;
207 }
208
210 {
212
216 }
217
219 {
221
225
226 return 0;
227 }
228
230 {
232 int i, v, pos[3];
235
239
240 for (i = 0; i < s->
w; i++) {
242 pos[NW] = i-1 < 0 ? s->
w-1 : i-1;
244 pos[NE] = i+1 == s->
w ? 0 : i+1;
245 v = prev_row[pos[NW]]<<2 | prev_row[pos[
N]]<<1 | prev_row[pos[NE]];
246 } else {
247 v = 0;
248 v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
249 v|= prev_row[i ]<<1 ;
250 v|= i+1 < s->
w ? prev_row[i+1] : 0;
251 }
252 row[i] = !!(s->
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]);
255 }
256
258 }
259
261 {
263 int i, j, k, row_idx = 0;
265
267 /* show on top the oldest row */
269
270 /* fill the output picture with the whole buffer */
271 for (i = 0; i < s->
h; i++) {
275 for (k = 0, j = 0; j < s->
w; j++) {
276 byte |= row[j]<<(7-k++);
277 if (k==8 || j == s->
w-1) {
278 k = 0;
280 byte = 0;
281 }
282 }
283 row_idx = (row_idx + 1) % s->
h;
285 }
286 }
287
289 {
292 if (!picref)
296 int i;
297 for (i = 0; i < s->
h-1; i++)
299 }
302
304
305 #ifdef DEBUG
306 show_cellauto_row(outlink->
src);
307 #endif
309 }
310
312 {
315 if (!fmts_list)
318 }
319
321 {
326 },
328 };
329
332 .description =
NULL_IF_CONFIG_SMALL(
"Create pattern generated by an elementary cellular automaton."),
334 .priv_class = &cellauto_class,
340 };
static int request_frame(AVFilterLink *outlink)
This structure describes decoded (raw) audio or video data.
Main libavfilter public API header.
int h
agreed upon image height
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
static av_cold int init(AVFilterContext *ctx)
const char * name
Pad name.
static const AVFilterPad cellauto_outputs[]
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
static int config_props(AVFilterLink *outlink)
#define AV_LOG_VERBOSE
Detailed information.
A filter pad used for either input or output.
A link between two filters.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
static int init_pattern_from_file(AVFilterContext *ctx)
int w
agreed upon image width
static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
common internal API header
static void evolve(AVFilterContext *ctx)
AVFilter ff_vsrc_cellauto
AVFilterContext * src
source filter
static const AVFilterPad outputs[]
static const AVFilterPad inputs[]
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL byte
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Describe the class of an AVClass context structure.
rational number numerator/denominator
static int query_formats(AVFilterContext *ctx)
offset must point to AVRational
const char * name
Filter name.
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
offset must point to two consecutive integers
static enum AVPixelFormat pix_fmts[]
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static av_const int av_isgraph(int c)
Locale-independent conversion of ASCII isgraph.
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb...
static int init_pattern_from_string(AVFilterContext *ctx)
static const AVOption cellauto_options[]
int64_t generation
the generation number, starting from 0
static av_cold void uninit(AVFilterContext *ctx)
AVFILTER_DEFINE_CLASS(cellauto)
static void * av_mallocz_array(size_t nmemb, size_t size)
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
AVPixelFormat
Pixel format.
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)