00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00026
00027
00028 #include "libavutil/file.h"
00029 #include "libavutil/lfg.h"
00030 #include "libavutil/opt.h"
00031 #include "libavutil/parseutils.h"
00032 #include "libavutil/random_seed.h"
00033 #include "avfilter.h"
00034 #include "internal.h"
00035 #include "formats.h"
00036 #include "video.h"
00037
00038 typedef struct {
00039 const AVClass *class;
00040 int w, h;
00041 char *filename;
00042 char *rule_str;
00043 uint8_t *file_buf;
00044 size_t file_bufsize;
00045 uint8_t *buf;
00046 int buf_prev_row_idx, buf_row_idx;
00047 uint8_t rule;
00048 uint64_t pts;
00049 AVRational time_base;
00050 char *rate;
00051 double random_fill_ratio;
00052 uint32_t random_seed;
00053 int stitch, scroll, start_full;
00054 int64_t generation;
00055 AVLFG lfg;
00056 char *pattern;
00057 } CellAutoContext;
00058
00059 #define OFFSET(x) offsetof(CellAutoContext, x)
00060 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
00061
00062 static const AVOption cellauto_options[] = {
00063 { "filename", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00064 { "f", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00065 { "pattern", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00066 { "p", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00067 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
00068 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
00069 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
00070 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
00071 { "rule", "set rule", OFFSET(rule), AV_OPT_TYPE_INT, {.i64 = 110}, 0, 255, FLAGS },
00072 { "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 },
00073 { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl = 1/M_PHI}, 0, 1, FLAGS },
00074 { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
00075 { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
00076 { "scroll", "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
00077 { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
00078 { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
00079 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
00080 { NULL },
00081 };
00082
00083 AVFILTER_DEFINE_CLASS(cellauto);
00084
00085 #ifdef DEBUG
00086 static void show_cellauto_row(AVFilterContext *ctx)
00087 {
00088 CellAutoContext *cellauto = ctx->priv;
00089 int i;
00090 uint8_t *row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
00091 char *line = av_malloc(cellauto->w + 1);
00092 if (!line)
00093 return;
00094
00095 for (i = 0; i < cellauto->w; i++)
00096 line[i] = row[i] ? '@' : ' ';
00097 line[i] = 0;
00098 av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", cellauto->generation, line);
00099 av_free(line);
00100 }
00101 #endif
00102
00103 static int init_pattern_from_string(AVFilterContext *ctx)
00104 {
00105 CellAutoContext *cellauto = ctx->priv;
00106 char *p;
00107 int i, w = 0;
00108
00109 w = strlen(cellauto->pattern);
00110 av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
00111
00112 if (cellauto->w) {
00113 if (w > cellauto->w) {
00114 av_log(ctx, AV_LOG_ERROR,
00115 "The specified width is %d which cannot contain the provided string width of %d\n",
00116 cellauto->w, w);
00117 return AVERROR(EINVAL);
00118 }
00119 } else {
00120
00121 cellauto->w = w;
00122 cellauto->h = (double)cellauto->w * M_PHI;
00123 }
00124
00125 cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
00126 if (!cellauto->buf)
00127 return AVERROR(ENOMEM);
00128
00129
00130 p = cellauto->pattern;
00131 for (i = (cellauto->w - w)/2;; i++) {
00132 av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p);
00133 if (*p == '\n' || !*p)
00134 break;
00135 else
00136 cellauto->buf[i] = !!isgraph(*(p++));
00137 }
00138
00139 return 0;
00140 }
00141
00142 static int init_pattern_from_file(AVFilterContext *ctx)
00143 {
00144 CellAutoContext *cellauto = ctx->priv;
00145 int ret;
00146
00147 ret = av_file_map(cellauto->filename,
00148 &cellauto->file_buf, &cellauto->file_bufsize, 0, ctx);
00149 if (ret < 0)
00150 return ret;
00151
00152
00153 cellauto->pattern = av_malloc(cellauto->file_bufsize + 1);
00154 if (!cellauto->pattern)
00155 return AVERROR(ENOMEM);
00156 memcpy(cellauto->pattern, cellauto->file_buf, cellauto->file_bufsize);
00157 cellauto->pattern[cellauto->file_bufsize] = 0;
00158
00159 return init_pattern_from_string(ctx);
00160 }
00161
00162 static int init(AVFilterContext *ctx, const char *args)
00163 {
00164 CellAutoContext *cellauto = ctx->priv;
00165 AVRational frame_rate;
00166 int ret;
00167
00168 cellauto->class = &cellauto_class;
00169 av_opt_set_defaults(cellauto);
00170
00171 if ((ret = av_set_options_string(cellauto, args, "=", ":")) < 0)
00172 return ret;
00173
00174 if ((ret = av_parse_video_rate(&frame_rate, cellauto->rate)) < 0) {
00175 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", cellauto->rate);
00176 return AVERROR(EINVAL);
00177 }
00178
00179 if (!cellauto->w && !cellauto->filename && !cellauto->pattern)
00180 av_opt_set(cellauto, "size", "320x518", 0);
00181
00182 cellauto->time_base.num = frame_rate.den;
00183 cellauto->time_base.den = frame_rate.num;
00184
00185 if (cellauto->filename && cellauto->pattern) {
00186 av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
00187 return AVERROR(EINVAL);
00188 }
00189
00190 if (cellauto->filename) {
00191 if ((ret = init_pattern_from_file(ctx)) < 0)
00192 return ret;
00193 } else if (cellauto->pattern) {
00194 if ((ret = init_pattern_from_string(ctx)) < 0)
00195 return ret;
00196 } else {
00197
00198 int i;
00199
00200 cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
00201 if (!cellauto->buf)
00202 return AVERROR(ENOMEM);
00203 if (cellauto->random_seed == -1)
00204 cellauto->random_seed = av_get_random_seed();
00205
00206 av_lfg_init(&cellauto->lfg, cellauto->random_seed);
00207
00208 for (i = 0; i < cellauto->w; i++) {
00209 double r = (double)av_lfg_get(&cellauto->lfg) / UINT32_MAX;
00210 if (r <= cellauto->random_fill_ratio)
00211 cellauto->buf[i] = 1;
00212 }
00213 }
00214
00215 av_log(ctx, AV_LOG_VERBOSE,
00216 "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
00217 cellauto->w, cellauto->h, frame_rate.num, frame_rate.den,
00218 cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full,
00219 cellauto->random_seed);
00220 return 0;
00221 }
00222
00223 static av_cold void uninit(AVFilterContext *ctx)
00224 {
00225 CellAutoContext *cellauto = ctx->priv;
00226
00227 av_file_unmap(cellauto->file_buf, cellauto->file_bufsize);
00228 av_freep(&cellauto->buf);
00229 av_freep(&cellauto->pattern);
00230 }
00231
00232 static int config_props(AVFilterLink *outlink)
00233 {
00234 CellAutoContext *cellauto = outlink->src->priv;
00235
00236 outlink->w = cellauto->w;
00237 outlink->h = cellauto->h;
00238 outlink->time_base = cellauto->time_base;
00239
00240 return 0;
00241 }
00242
00243 static void evolve(AVFilterContext *ctx)
00244 {
00245 CellAutoContext *cellauto = ctx->priv;
00246 int i, v, pos[3];
00247 uint8_t *row, *prev_row = cellauto->buf + cellauto->buf_row_idx * cellauto->w;
00248 enum { NW, N, NE };
00249
00250 cellauto->buf_prev_row_idx = cellauto->buf_row_idx;
00251 cellauto->buf_row_idx = cellauto->buf_row_idx == cellauto->h-1 ? 0 : cellauto->buf_row_idx+1;
00252 row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
00253
00254 for (i = 0; i < cellauto->w; i++) {
00255 if (cellauto->stitch) {
00256 pos[NW] = i-1 < 0 ? cellauto->w-1 : i-1;
00257 pos[N] = i;
00258 pos[NE] = i+1 == cellauto->w ? 0 : i+1;
00259 v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
00260 } else {
00261 v = 0;
00262 v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
00263 v|= prev_row[i ]<<1 ;
00264 v|= i+1 < cellauto->w ? prev_row[i+1] : 0;
00265 }
00266 row[i] = !!(cellauto->rule & (1<<v));
00267 av_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
00268 v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
00269 }
00270
00271 cellauto->generation++;
00272 }
00273
00274 static void fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
00275 {
00276 CellAutoContext *cellauto = ctx->priv;
00277 int i, j, k, row_idx = 0;
00278 uint8_t *p0 = picref->data[0];
00279
00280 if (cellauto->scroll && cellauto->generation >= cellauto->h)
00281
00282 row_idx = (cellauto->buf_row_idx + 1) % cellauto->h;
00283
00284
00285 for (i = 0; i < cellauto->h; i++) {
00286 uint8_t byte = 0;
00287 uint8_t *row = cellauto->buf + row_idx*cellauto->w;
00288 uint8_t *p = p0;
00289 for (k = 0, j = 0; j < cellauto->w; j++) {
00290 byte |= row[j]<<(7-k++);
00291 if (k==8 || j == cellauto->w-1) {
00292 k = 0;
00293 *p++ = byte;
00294 byte = 0;
00295 }
00296 }
00297 row_idx = (row_idx + 1) % cellauto->h;
00298 p0 += picref->linesize[0];
00299 }
00300 }
00301
00302 static int request_frame(AVFilterLink *outlink)
00303 {
00304 CellAutoContext *cellauto = outlink->src->priv;
00305 AVFilterBufferRef *picref =
00306 ff_get_video_buffer(outlink, AV_PERM_WRITE, cellauto->w, cellauto->h);
00307 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
00308 if (cellauto->generation == 0 && cellauto->start_full) {
00309 int i;
00310 for (i = 0; i < cellauto->h-1; i++)
00311 evolve(outlink->src);
00312 }
00313 fill_picture(outlink->src, picref);
00314 evolve(outlink->src);
00315
00316 picref->pts = cellauto->pts++;
00317 picref->pos = -1;
00318
00319 #ifdef DEBUG
00320 show_cellauto_row(outlink->src);
00321 #endif
00322 ff_filter_frame(outlink, picref);
00323
00324 return 0;
00325 }
00326
00327 static int query_formats(AVFilterContext *ctx)
00328 {
00329 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE };
00330 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00331 return 0;
00332 }
00333
00334 static const AVFilterPad cellauto_outputs[] = {
00335 {
00336 .name = "default",
00337 .type = AVMEDIA_TYPE_VIDEO,
00338 .request_frame = request_frame,
00339 .config_props = config_props,
00340 },
00341 { NULL }
00342 };
00343
00344 AVFilter avfilter_vsrc_cellauto = {
00345 .name = "cellauto",
00346 .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
00347 .priv_size = sizeof(CellAutoContext),
00348 .init = init,
00349 .uninit = uninit,
00350 .query_formats = query_formats,
00351 .inputs = NULL,
00352 .outputs = cellauto_outputs,
00353 .priv_class = &cellauto_class,
00354 };