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