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