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/intreadwrite.h"
00030 #include "libavutil/lfg.h"
00031 #include "libavutil/opt.h"
00032 #include "libavutil/parseutils.h"
00033 #include "libavutil/random_seed.h"
00034 #include "avfilter.h"
00035 #include "internal.h"
00036 #include "formats.h"
00037 #include "video.h"
00038
00039 typedef struct {
00040 const AVClass *class;
00041 int w, h;
00042 char *filename;
00043 char *rule_str;
00044 uint8_t *file_buf;
00045 size_t file_bufsize;
00046
00056 uint8_t *buf[2];
00057
00058 uint8_t buf_idx;
00059 uint16_t stay_rule;
00060 uint16_t born_rule;
00061 uint64_t pts;
00062 AVRational time_base;
00063 char *rate;
00064 double random_fill_ratio;
00065 uint32_t random_seed;
00066 int stitch;
00067 int mold;
00068 char *life_color_str;
00069 char *death_color_str;
00070 char *mold_color_str;
00071 uint8_t life_color[4];
00072 uint8_t death_color[4];
00073 uint8_t mold_color[4];
00074 AVLFG lfg;
00075 void (*draw)(AVFilterContext*, AVFilterBufferRef*);
00076 } LifeContext;
00077
00078 #define ALIVE_CELL 0xFF
00079 #define OFFSET(x) offsetof(LifeContext, x)
00080 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
00081
00082 static const AVOption life_options[] = {
00083 { "filename", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00084 { "f", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
00085 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
00086 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
00087 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
00088 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
00089 { "rule", "set rule", OFFSET(rule_str), AV_OPT_TYPE_STRING, {.str = "B3/S23"}, CHAR_MIN, CHAR_MAX, FLAGS },
00090 { "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 },
00091 { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1/M_PHI}, 0, 1, FLAGS },
00092 { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, UINT32_MAX, FLAGS },
00093 { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64=-1}, -1, UINT32_MAX, FLAGS },
00094 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
00095 { "mold", "set mold speed for dead cells", OFFSET(mold), AV_OPT_TYPE_INT, {.i64=0}, 0, 0xFF, FLAGS },
00096 { "life_color", "set life color", OFFSET( life_color_str), AV_OPT_TYPE_STRING, {.str="white"}, CHAR_MIN, CHAR_MAX, FLAGS },
00097 { "death_color", "set death color", OFFSET(death_color_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS },
00098 { "mold_color", "set mold color", OFFSET( mold_color_str), AV_OPT_TYPE_STRING, {.str="black"}, CHAR_MIN, CHAR_MAX, FLAGS },
00099 { NULL },
00100 };
00101
00102 AVFILTER_DEFINE_CLASS(life);
00103
00104 static int parse_rule(uint16_t *born_rule, uint16_t *stay_rule,
00105 const char *rule_str, void *log_ctx)
00106 {
00107 char *tail;
00108 const char *p = rule_str;
00109 *born_rule = 0;
00110 *stay_rule = 0;
00111
00112 if (strchr("bBsS", *p)) {
00113
00114
00115 do {
00116 uint16_t *rule = (*p == 'b' || *p == 'B') ? born_rule : stay_rule;
00117 p++;
00118 while (*p >= '0' && *p <= '8') {
00119 *rule += 1<<(*p - '0');
00120 p++;
00121 }
00122 if (*p != '/')
00123 break;
00124 p++;
00125 } while (strchr("bBsS", *p));
00126
00127 if (*p)
00128 goto error;
00129 } else {
00130
00131
00132 long int rule = strtol(rule_str, &tail, 10);
00133 if (*tail)
00134 goto error;
00135 *born_rule = ((1<<9)-1) & rule;
00136 *stay_rule = rule >> 9;
00137 }
00138
00139 return 0;
00140
00141 error:
00142 av_log(log_ctx, AV_LOG_ERROR, "Invalid rule code '%s' provided\n", rule_str);
00143 return AVERROR(EINVAL);
00144 }
00145
00146 #ifdef DEBUG
00147 static void show_life_grid(AVFilterContext *ctx)
00148 {
00149 LifeContext *life = ctx->priv;
00150 int i, j;
00151
00152 char *line = av_malloc(life->w + 1);
00153 if (!line)
00154 return;
00155 for (i = 0; i < life->h; i++) {
00156 for (j = 0; j < life->w; j++)
00157 line[j] = life->buf[life->buf_idx][i*life->w + j] == ALIVE_CELL ? '@' : ' ';
00158 line[j] = 0;
00159 av_log(ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
00160 }
00161 av_free(line);
00162 }
00163 #endif
00164
00165 static int init_pattern_from_file(AVFilterContext *ctx)
00166 {
00167 LifeContext *life = ctx->priv;
00168 char *p;
00169 int ret, i, i0, j, h = 0, w, max_w = 0;
00170
00171 if ((ret = av_file_map(life->filename, &life->file_buf, &life->file_bufsize,
00172 0, ctx)) < 0)
00173 return ret;
00174 av_freep(&life->filename);
00175
00176
00177 w = 0;
00178 for (i = 0; i < life->file_bufsize; i++) {
00179 if (life->file_buf[i] == '\n') {
00180 h++; max_w = FFMAX(w, max_w); w = 0;
00181 } else {
00182 w++;
00183 }
00184 }
00185 av_log(ctx, AV_LOG_DEBUG, "h:%d max_w:%d\n", h, max_w);
00186
00187 if (life->w) {
00188 if (max_w > life->w || h > life->h) {
00189 av_log(ctx, AV_LOG_ERROR,
00190 "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
00191 life->w, life->h, max_w, h);
00192 return AVERROR(EINVAL);
00193 }
00194 } else {
00195
00196 life->w = max_w;
00197 life->h = h;
00198 }
00199
00200 if (!(life->buf[0] = av_mallocz(sizeof(char) * life->h * life->w)) ||
00201 !(life->buf[1] = av_mallocz(sizeof(char) * life->h * life->w))) {
00202 av_free(life->buf[0]);
00203 av_free(life->buf[1]);
00204 return AVERROR(ENOMEM);
00205 }
00206
00207
00208 p = life->file_buf;
00209 for (i0 = 0, i = (life->h - h)/2; i0 < h; i0++, i++) {
00210 for (j = (life->w - max_w)/2;; j++) {
00211 av_log(ctx, AV_LOG_DEBUG, "%d:%d %c\n", i, j, *p == '\n' ? 'N' : *p);
00212 if (*p == '\n') {
00213 p++; break;
00214 } else
00215 life->buf[0][i*life->w + j] = isgraph(*(p++)) ? ALIVE_CELL : 0;
00216 }
00217 }
00218 life->buf_idx = 0;
00219
00220 return 0;
00221 }
00222
00223 static int init(AVFilterContext *ctx, const char *args)
00224 {
00225 LifeContext *life = ctx->priv;
00226 AVRational frame_rate;
00227 int ret;
00228
00229 life->class = &life_class;
00230 av_opt_set_defaults(life);
00231
00232 if ((ret = av_set_options_string(life, args, "=", ":")) < 0)
00233 return ret;
00234
00235 if ((ret = av_parse_video_rate(&frame_rate, life->rate)) < 0) {
00236 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", life->rate);
00237 return AVERROR(EINVAL);
00238 }
00239 av_freep(&life->rate);
00240
00241 if (!life->w && !life->filename)
00242 av_opt_set(life, "size", "320x240", 0);
00243
00244 if ((ret = parse_rule(&life->born_rule, &life->stay_rule, life->rule_str, ctx)) < 0)
00245 return ret;
00246
00247 #define PARSE_COLOR(name) do { \
00248 if ((ret = av_parse_color(life->name ## _color, life->name ## _color_str, -1, ctx))) { \
00249 av_log(ctx, AV_LOG_ERROR, "Invalid " #name " color '%s'\n", \
00250 life->name ## _color_str); \
00251 return ret; \
00252 } \
00253 av_freep(&life->name ## _color_str); \
00254 } while (0)
00255
00256 PARSE_COLOR(life);
00257 PARSE_COLOR(death);
00258 PARSE_COLOR(mold);
00259
00260 if (!life->mold && memcmp(life->mold_color, "\x00\x00\x00", 3))
00261 av_log(ctx, AV_LOG_WARNING,
00262 "Mold color is set while mold isn't, ignoring the color.\n");
00263
00264 life->time_base.num = frame_rate.den;
00265 life->time_base.den = frame_rate.num;
00266
00267 if (!life->filename) {
00268
00269 int i;
00270
00271 if (!(life->buf[0] = av_mallocz(sizeof(char) * life->h * life->w)) ||
00272 !(life->buf[1] = av_mallocz(sizeof(char) * life->h * life->w))) {
00273 av_free(life->buf[0]);
00274 av_free(life->buf[1]);
00275 return AVERROR(ENOMEM);
00276 }
00277 if (life->random_seed == -1)
00278 life->random_seed = av_get_random_seed();
00279
00280 av_lfg_init(&life->lfg, life->random_seed);
00281
00282 for (i = 0; i < life->w * life->h; i++) {
00283 double r = (double)av_lfg_get(&life->lfg) / UINT32_MAX;
00284 if (r <= life->random_fill_ratio)
00285 life->buf[0][i] = ALIVE_CELL;
00286 }
00287 life->buf_idx = 0;
00288 } else {
00289 if ((ret = init_pattern_from_file(ctx)) < 0)
00290 return ret;
00291 }
00292
00293 av_log(ctx, AV_LOG_VERBOSE,
00294 "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d seed:%u\n",
00295 life->w, life->h, frame_rate.num, frame_rate.den,
00296 life->rule_str, life->stay_rule, life->born_rule, life->stitch,
00297 life->random_seed);
00298 return 0;
00299 }
00300
00301 static av_cold void uninit(AVFilterContext *ctx)
00302 {
00303 LifeContext *life = ctx->priv;
00304
00305 av_file_unmap(life->file_buf, life->file_bufsize);
00306 av_freep(&life->rule_str);
00307 av_freep(&life->buf[0]);
00308 av_freep(&life->buf[1]);
00309 }
00310
00311 static int config_props(AVFilterLink *outlink)
00312 {
00313 LifeContext *life = outlink->src->priv;
00314
00315 outlink->w = life->w;
00316 outlink->h = life->h;
00317 outlink->time_base = life->time_base;
00318
00319 return 0;
00320 }
00321
00322 static void evolve(AVFilterContext *ctx)
00323 {
00324 LifeContext *life = ctx->priv;
00325 int i, j;
00326 uint8_t *oldbuf = life->buf[ life->buf_idx];
00327 uint8_t *newbuf = life->buf[!life->buf_idx];
00328
00329 enum { NW, N, NE, W, E, SW, S, SE };
00330
00331
00332 for (i = 0; i < life->h; i++) {
00333 for (j = 0; j < life->w; j++) {
00334 int pos[8][2], n, alive, cell;
00335 if (life->stitch) {
00336 pos[NW][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NW][1] = (j-1) < 0 ? life->w-1 : j-1;
00337 pos[N ][0] = (i-1) < 0 ? life->h-1 : i-1; pos[N ][1] = j ;
00338 pos[NE][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NE][1] = (j+1) == life->w ? 0 : j+1;
00339 pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? life->w-1 : j-1;
00340 pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? 0 : j+1;
00341 pos[SW][0] = (i+1) == life->h ? 0 : i+1; pos[SW][1] = (j-1) < 0 ? life->w-1 : j-1;
00342 pos[S ][0] = (i+1) == life->h ? 0 : i+1; pos[S ][1] = j ;
00343 pos[SE][0] = (i+1) == life->h ? 0 : i+1; pos[SE][1] = (j+1) == life->w ? 0 : j+1;
00344 } else {
00345 pos[NW][0] = (i-1) < 0 ? -1 : i-1; pos[NW][1] = (j-1) < 0 ? -1 : j-1;
00346 pos[N ][0] = (i-1) < 0 ? -1 : i-1; pos[N ][1] = j ;
00347 pos[NE][0] = (i-1) < 0 ? -1 : i-1; pos[NE][1] = (j+1) == life->w ? -1 : j+1;
00348 pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? -1 : j-1;
00349 pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? -1 : j+1;
00350 pos[SW][0] = (i+1) == life->h ? -1 : i+1; pos[SW][1] = (j-1) < 0 ? -1 : j-1;
00351 pos[S ][0] = (i+1) == life->h ? -1 : i+1; pos[S ][1] = j ;
00352 pos[SE][0] = (i+1) == life->h ? -1 : i+1; pos[SE][1] = (j+1) == life->w ? -1 : j+1;
00353 }
00354
00355
00356 n = (pos[NW][0] == -1 || pos[NW][1] == -1 ? 0 : oldbuf[pos[NW][0]*life->w + pos[NW][1]] == ALIVE_CELL) +
00357 (pos[N ][0] == -1 || pos[N ][1] == -1 ? 0 : oldbuf[pos[N ][0]*life->w + pos[N ][1]] == ALIVE_CELL) +
00358 (pos[NE][0] == -1 || pos[NE][1] == -1 ? 0 : oldbuf[pos[NE][0]*life->w + pos[NE][1]] == ALIVE_CELL) +
00359 (pos[W ][0] == -1 || pos[W ][1] == -1 ? 0 : oldbuf[pos[W ][0]*life->w + pos[W ][1]] == ALIVE_CELL) +
00360 (pos[E ][0] == -1 || pos[E ][1] == -1 ? 0 : oldbuf[pos[E ][0]*life->w + pos[E ][1]] == ALIVE_CELL) +
00361 (pos[SW][0] == -1 || pos[SW][1] == -1 ? 0 : oldbuf[pos[SW][0]*life->w + pos[SW][1]] == ALIVE_CELL) +
00362 (pos[S ][0] == -1 || pos[S ][1] == -1 ? 0 : oldbuf[pos[S ][0]*life->w + pos[S ][1]] == ALIVE_CELL) +
00363 (pos[SE][0] == -1 || pos[SE][1] == -1 ? 0 : oldbuf[pos[SE][0]*life->w + pos[SE][1]] == ALIVE_CELL);
00364 cell = oldbuf[i*life->w + j];
00365 alive = 1<<n & (cell == ALIVE_CELL ? life->stay_rule : life->born_rule);
00366 if (alive) *newbuf = ALIVE_CELL;
00367 else if (cell) *newbuf = cell - 1;
00368 else *newbuf = 0;
00369 av_dlog(ctx, "i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n", i, j, n, cell, *newbuf);
00370 newbuf++;
00371 }
00372 }
00373
00374 life->buf_idx = !life->buf_idx;
00375 }
00376
00377 static void fill_picture_monoblack(AVFilterContext *ctx, AVFilterBufferRef *picref)
00378 {
00379 LifeContext *life = ctx->priv;
00380 uint8_t *buf = life->buf[life->buf_idx];
00381 int i, j, k;
00382
00383
00384 for (i = 0; i < life->h; i++) {
00385 uint8_t byte = 0;
00386 uint8_t *p = picref->data[0] + i * picref->linesize[0];
00387 for (k = 0, j = 0; j < life->w; j++) {
00388 byte |= (buf[i*life->w+j] == ALIVE_CELL)<<(7-k++);
00389 if (k==8 || j == life->w-1) {
00390 k = 0;
00391 *p++ = byte;
00392 byte = 0;
00393 }
00394 }
00395 }
00396 }
00397
00398
00399
00400 #define FAST_DIV255(x) ((((x) + 128) * 257) >> 16)
00401
00402 static void fill_picture_rgb(AVFilterContext *ctx, AVFilterBufferRef *picref)
00403 {
00404 LifeContext *life = ctx->priv;
00405 uint8_t *buf = life->buf[life->buf_idx];
00406 int i, j;
00407
00408
00409 for (i = 0; i < life->h; i++) {
00410 uint8_t *p = picref->data[0] + i * picref->linesize[0];
00411 for (j = 0; j < life->w; j++) {
00412 uint8_t v = buf[i*life->w + j];
00413 if (life->mold && v != ALIVE_CELL) {
00414 const uint8_t *c1 = life-> mold_color;
00415 const uint8_t *c2 = life->death_color;
00416 int death_age = FFMIN((0xff - v) * life->mold, 0xff);
00417 *p++ = FAST_DIV255((c2[0] << 8) + ((int)c1[0] - (int)c2[0]) * death_age);
00418 *p++ = FAST_DIV255((c2[1] << 8) + ((int)c1[1] - (int)c2[1]) * death_age);
00419 *p++ = FAST_DIV255((c2[2] << 8) + ((int)c1[2] - (int)c2[2]) * death_age);
00420 } else {
00421 const uint8_t *c = v == ALIVE_CELL ? life->life_color : life->death_color;
00422 AV_WB24(p, c[0]<<16 | c[1]<<8 | c[2]);
00423 p += 3;
00424 }
00425 }
00426 }
00427 }
00428
00429 static int request_frame(AVFilterLink *outlink)
00430 {
00431 LifeContext *life = outlink->src->priv;
00432 AVFilterBufferRef *picref = ff_get_video_buffer(outlink, AV_PERM_WRITE, life->w, life->h);
00433 picref->video->sample_aspect_ratio = (AVRational) {1, 1};
00434 picref->pts = life->pts++;
00435 picref->pos = -1;
00436
00437 life->draw(outlink->src, picref);
00438 evolve(outlink->src);
00439 #ifdef DEBUG
00440 show_life_grid(outlink->src);
00441 #endif
00442
00443 ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
00444 ff_draw_slice(outlink, 0, life->h, 1);
00445 ff_end_frame(outlink);
00446 avfilter_unref_buffer(picref);
00447
00448 return 0;
00449 }
00450
00451 static int query_formats(AVFilterContext *ctx)
00452 {
00453 LifeContext *life = ctx->priv;
00454 enum PixelFormat pix_fmts[] = { PIX_FMT_NONE, PIX_FMT_NONE };
00455 if (life->mold || memcmp(life-> life_color, "\xff\xff\xff", 3)
00456 || memcmp(life->death_color, "\x00\x00\x00", 3)) {
00457 pix_fmts[0] = PIX_FMT_RGB24;
00458 life->draw = fill_picture_rgb;
00459 } else {
00460 pix_fmts[0] = PIX_FMT_MONOBLACK;
00461 life->draw = fill_picture_monoblack;
00462 }
00463 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
00464 return 0;
00465 }
00466
00467 AVFilter avfilter_vsrc_life = {
00468 .name = "life",
00469 .description = NULL_IF_CONFIG_SMALL("Create life."),
00470 .priv_size = sizeof(LifeContext),
00471 .init = init,
00472 .uninit = uninit,
00473 .query_formats = query_formats,
00474
00475 .inputs = (const AVFilterPad[]) {
00476 { .name = NULL}
00477 },
00478 .outputs = (const AVFilterPad[]) {
00479 { .name = "default",
00480 .type = AVMEDIA_TYPE_VIDEO,
00481 .request_frame = request_frame,
00482 .config_props = config_props },
00483 { .name = NULL}
00484 },
00485 .priv_class = &life_class,
00486 };