FFmpeg
Loading...
Searching...
No Matches
vsrc_cellauto.c
Go to the documentation of this file.
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/mem.h"
32#include "libavutil/opt.h"
34#include "libavutil/avstring.h"
35#include "avfilter.h"
36#include "filters.h"
37#include "video.h"
38
39typedef struct CellAutoContext {
40 const AVClass *class;
41 int w, h;
42 char *filename;
43 char *rule_str;
44 uint8_t *file_buf;
46 uint8_t *buf;
48 uint8_t rule;
49 uint64_t pts;
54 int64_t generation; ///< the generation number, starting from 0
56 char *pattern;
58
59#define OFFSET(x) offsetof(CellAutoContext, x)
60#define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
61
62static const AVOption cellauto_options[] = {
63 { "filename", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
64 { "f", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
65 { "pattern", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
66 { "p", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
67 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
68 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
69 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
70 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
71 { "rule", "set rule", OFFSET(rule), AV_OPT_TYPE_INT, {.i64 = 110}, 0, 255, FLAGS },
72 { "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 },
73 { "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 { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
75 { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT64, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
76 { "scroll", "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
77 { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
78 { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
79 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
80 { NULL }
81};
82
84
85#ifdef DEBUG
86static void show_cellauto_row(AVFilterContext *ctx)
87{
88 CellAutoContext *s = ctx->priv;
89 int i;
90 uint8_t *row = s->buf + s->w * s->buf_row_idx;
91 char *line = av_malloc(s->w + 1);
92 if (!line)
93 return;
94
95 for (i = 0; i < s->w; i++)
96 line[i] = row[i] ? '@' : ' ';
97 line[i] = 0;
98 av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", s->generation, line);
100}
101#endif
102
104{
105 CellAutoContext *s = ctx->priv;
106 char *p;
107 int i, w = 0;
108
109 w = strlen(s->pattern);
110 av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
111
112 if (s->w) {
113 if (w > s->w) {
115 "The specified width is %d which cannot contain the provided string width of %d\n",
116 s->w, w);
117 return AVERROR(EINVAL);
118 }
119 } else {
120 /* width was not specified, set it to width of the provided row */
121 s->w = w;
122 s->h = (double)s->w * M_PHI;
123 }
124
125 s->buf = av_calloc(s->w, s->h * sizeof(*s->buf));
126 if (!s->buf)
127 return AVERROR(ENOMEM);
128
129 /* fill buf */
130 p = s->pattern;
131 for (i = (s->w - w)/2;; i++) {
132 av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p);
133 if (*p == '\n' || !*p)
134 break;
135 else
136 s->buf[i] = !!av_isgraph(*(p++));
137 }
138
139 return 0;
140}
141
143{
144 CellAutoContext *s = ctx->priv;
145 int ret;
146
147 ret = av_file_map(s->filename,
148 &s->file_buf, &s->file_bufsize, 0, ctx);
149 if (ret < 0)
150 return ret;
151
152 /* create a string based on the read file */
153 s->pattern = av_malloc(s->file_bufsize + 1);
154 if (!s->pattern)
155 return AVERROR(ENOMEM);
156 memcpy(s->pattern, s->file_buf, s->file_bufsize);
157 s->pattern[s->file_bufsize] = 0;
158
160}
161
163{
164 CellAutoContext *s = ctx->priv;
165 int ret;
166
167 if (!s->w && !s->filename && !s->pattern)
168 av_opt_set(s, "size", "320x518", 0);
169
170 if (s->filename && s->pattern) {
171 av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
172 return AVERROR(EINVAL);
173 }
174
175 if (s->filename) {
176 if ((ret = init_pattern_from_file(ctx)) < 0)
177 return ret;
178 } else if (s->pattern) {
179 if ((ret = init_pattern_from_string(ctx)) < 0)
180 return ret;
181 } else {
182 /* fill the first row randomly */
183 int i;
184
185 s->buf = av_calloc(s->w, s->h * sizeof(*s->buf));
186 if (!s->buf)
187 return AVERROR(ENOMEM);
188 if (s->random_seed == -1)
189 s->random_seed = av_get_random_seed();
190
191 av_lfg_init(&s->lfg, s->random_seed);
192
193 for (i = 0; i < s->w; i++) {
194 double r = (double)av_lfg_get(&s->lfg) / UINT32_MAX;
195 if (r <= s->random_fill_ratio)
196 s->buf[i] = 1;
197 }
198 }
199
201 "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%"PRId64"\n",
202 s->w, s->h, s->frame_rate.num, s->frame_rate.den,
203 s->rule, s->stitch, s->scroll, s->start_full,
204 s->random_seed);
205 return 0;
206}
207
209{
210 CellAutoContext *s = ctx->priv;
211
212 av_file_unmap(s->file_buf, s->file_bufsize);
213 av_freep(&s->buf);
214 av_freep(&s->pattern);
215}
216
217static int config_props(AVFilterLink *outlink)
218{
219 CellAutoContext *s = outlink->src->priv;
220 FilterLink *l = ff_filter_link(outlink);
221
222 outlink->w = s->w;
223 outlink->h = s->h;
224 outlink->time_base = av_inv_q(s->frame_rate);
225 l->frame_rate = s->frame_rate;
226
227 return 0;
228}
229
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
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
289static 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 picref->duration = 1;
306
307#ifdef DEBUG
308 show_cellauto_row(outlink->src);
309#endif
310 return ff_filter_frame(outlink, picref);
311}
312
314 {
315 .name = "default",
316 .type = AVMEDIA_TYPE_VIDEO,
317 .request_frame = request_frame,
318 .config_props = config_props,
319 },
320};
321
323 .p.name = "cellauto",
324 .p.description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
325 .p.priv_class = &cellauto_class,
326 .p.inputs = NULL,
327 .priv_size = sizeof(CellAutoContext),
328 .init = init,
329 .uninit = uninit,
332};
static int request_frame(AVFilterLink *outlink)
Definition af_aecho.c:272
#define N
Definition af_mcompand.c:54
const FFFilter ff_vsrc_cellauto
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1068
Main libavfilter public API header.
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
Definition bytestream.h:99
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define FLAGS
Definition cmdutils.c:598
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int(* init)(AVBSFContext *ctx)
Definition dts2pts.c:608
Misc file utilities.
@ AV_OPT_TYPE_IMAGE_SIZE
Underlying C type is two consecutive integers.
Definition opt.h:302
@ AV_OPT_TYPE_VIDEO_RATE
Underlying C type is AVRational.
Definition opt.h:314
@ AV_OPT_TYPE_INT64
Underlying C type is int64_t.
Definition opt.h:262
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_DOUBLE
Underlying C type is double.
Definition opt.h:266
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
@ AV_OPT_TYPE_STRING
Underlying C type is a uint8_t* that is either NULL or points to a C string allocated with the av_mal...
Definition opt.h:275
uint32_t av_get_random_seed(void)
Get a seed to use in conjunction with random functions.
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition rational.h:159
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
static av_const int av_isgraph(int c)
Locale-independent conversion of ASCII isgraph.
Definition avstring.h:210
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition opt.c:887
#define r
Definition input.c:42
static av_cold void uninit(AVBitStreamFilterContext *ctx)
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
Definition lfg.c:32
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
Definition lfg.h:53
#define FILTER_OUTPUTS(array)
Definition filters.h:265
static FilterLink * ff_filter_link(AVFilterLink *link)
Definition filters.h:199
#define FILTER_SINGLE_PIXFMT(pix_fmt_)
Definition filters.h:254
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:478
#define av_cold
Definition attributes.h:117
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
Definition file.c:142
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...
Definition file.c:55
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
uint8_t w
Definition llvidencdsp.c:39
#define M_PHI
Definition mathematics.h:61
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define av_malloc(s)
Definition ops_static.c:52
AVOptions.
@ AV_PIX_FMT_MONOBLACK
Y , 1bpp, 0 is black, 1 is white, in each byte pixels are ordered from the msb to the lsb.
Definition pixfmt.h:83
static int config_props(AVBitStreamFilterLink *link)
Definition source.c:181
unsigned int pos
Definition spdifenc.c:431
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:273
void * priv
private data for use by the filter
Definition avfilter.h:288
A filter pad used for either input or output.
Definition filters.h:40
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition frame.h:574
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition frame.h:493
AVRational sample_aspect_ratio
Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
Definition frame.h:569
int linesize[AV_NUM_DATA_POINTERS]
For video, a positive or negative value, which is typically indicating the size in bytes of each pict...
Definition frame.h:517
int64_t duration
Duration of the frame, in the same units as pts.
Definition frame.h:820
Context structure for the Lagged Fibonacci PRNG.
Definition lfg.h:33
AVOption.
Definition opt.h:428
Rational number (pair of numerator and denominator).
Definition rational.h:58
int64_t generation
the generation number, starting from 0
AVRational frame_rate
double random_fill_ratio
uint8_t * file_buf
#define av_free(p)
#define ff_dlog(a,...)
#define av_freep(p)
#define av_log(a,...)
static AVFormatContext * ctx
Definition movenc.c:49
static void scroll(AVFilterContext *ctx, AVFrame *in, AVFrame *out)
Definition vf_scroll.c:103
AVFrame * ff_get_video_buffer(AVFilterLink *link, int w, int h)
Request a picture buffer with a specific set of permissions.
Definition video.c:89
static int init_pattern_from_file(AVFilterContext *ctx)
static int config_props(AVFilterLink *outlink)
static void evolve(AVFilterContext *ctx)
static const AVOption cellauto_options[]
static int request_frame(AVFilterLink *outlink)
static int init_pattern_from_string(AVFilterContext *ctx)
static const AVFilterPad cellauto_outputs[]
static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
static av_cold void uninit(AVFilterContext *ctx)
#define OFFSET(x)