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