yading@11: /* yading@11: * Copyright (c) Stefano Sabatini 2011 yading@11: * yading@11: * This file is part of FFmpeg. yading@11: * yading@11: * FFmpeg is free software; you can redistribute it and/or yading@11: * modify it under the terms of the GNU Lesser General Public yading@11: * License as published by the Free Software Foundation; either yading@11: * version 2.1 of the License, or (at your option) any later version. yading@11: * yading@11: * FFmpeg is distributed in the hope that it will be useful, yading@11: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@11: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@11: * Lesser General Public License for more details. yading@11: * yading@11: * You should have received a copy of the GNU Lesser General Public yading@11: * License along with FFmpeg; if not, write to the Free Software yading@11: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@11: */ yading@11: yading@11: /** yading@11: * @file yading@11: * cellular automaton video source, based on Stephen Wolfram "experimentus crucis" yading@11: */ yading@11: yading@11: /* #define DEBUG */ yading@11: yading@11: #include "libavutil/file.h" yading@11: #include "libavutil/lfg.h" yading@11: #include "libavutil/opt.h" yading@11: #include "libavutil/parseutils.h" yading@11: #include "libavutil/random_seed.h" yading@11: #include "libavutil/avstring.h" yading@11: #include "avfilter.h" yading@11: #include "internal.h" yading@11: #include "formats.h" yading@11: #include "video.h" yading@11: yading@11: typedef struct { yading@11: const AVClass *class; yading@11: int w, h; yading@11: char *filename; yading@11: char *rule_str; yading@11: uint8_t *file_buf; yading@11: size_t file_bufsize; yading@11: uint8_t *buf; yading@11: int buf_prev_row_idx, buf_row_idx; yading@11: uint8_t rule; yading@11: uint64_t pts; yading@11: AVRational frame_rate; yading@11: double random_fill_ratio; yading@11: uint32_t random_seed; yading@11: int stitch, scroll, start_full; yading@11: int64_t generation; ///< the generation number, starting from 0 yading@11: AVLFG lfg; yading@11: char *pattern; yading@11: } CellAutoContext; yading@11: yading@11: #define OFFSET(x) offsetof(CellAutoContext, x) yading@11: #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM yading@11: yading@11: static const AVOption cellauto_options[] = { yading@11: { "filename", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, yading@11: { "f", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, yading@11: { "pattern", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, yading@11: { "p", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS }, yading@11: { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS }, yading@11: { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS }, yading@11: { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, yading@11: { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS }, yading@11: { "rule", "set rule", OFFSET(rule), AV_OPT_TYPE_INT, {.i64 = 110}, 0, 255, FLAGS }, yading@11: { "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 }, yading@11: { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl = 1/M_PHI}, 0, 1, FLAGS }, yading@11: { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS }, yading@11: { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS }, yading@11: { "scroll", "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS }, yading@11: { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS }, yading@11: { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS }, yading@11: { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS }, yading@11: { NULL }, yading@11: }; yading@11: yading@11: AVFILTER_DEFINE_CLASS(cellauto); yading@11: yading@11: #ifdef DEBUG yading@11: static void show_cellauto_row(AVFilterContext *ctx) yading@11: { yading@11: CellAutoContext *cellauto = ctx->priv; yading@11: int i; yading@11: uint8_t *row = cellauto->buf + cellauto->w * cellauto->buf_row_idx; yading@11: char *line = av_malloc(cellauto->w + 1); yading@11: if (!line) yading@11: return; yading@11: yading@11: for (i = 0; i < cellauto->w; i++) yading@11: line[i] = row[i] ? '@' : ' '; yading@11: line[i] = 0; yading@11: av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", cellauto->generation, line); yading@11: av_free(line); yading@11: } yading@11: #endif yading@11: yading@11: static int init_pattern_from_string(AVFilterContext *ctx) yading@11: { yading@11: CellAutoContext *cellauto = ctx->priv; yading@11: char *p; yading@11: int i, w = 0; yading@11: yading@11: w = strlen(cellauto->pattern); yading@11: av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w); yading@11: yading@11: if (cellauto->w) { yading@11: if (w > cellauto->w) { yading@11: av_log(ctx, AV_LOG_ERROR, yading@11: "The specified width is %d which cannot contain the provided string width of %d\n", yading@11: cellauto->w, w); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: } else { yading@11: /* width was not specified, set it to width of the provided row */ yading@11: cellauto->w = w; yading@11: cellauto->h = (double)cellauto->w * M_PHI; yading@11: } yading@11: yading@11: cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h); yading@11: if (!cellauto->buf) yading@11: return AVERROR(ENOMEM); yading@11: yading@11: /* fill buf */ yading@11: p = cellauto->pattern; yading@11: for (i = (cellauto->w - w)/2;; i++) { yading@11: av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p); yading@11: if (*p == '\n' || !*p) yading@11: break; yading@11: else yading@11: cellauto->buf[i] = !!av_isgraph(*(p++)); yading@11: } yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static int init_pattern_from_file(AVFilterContext *ctx) yading@11: { yading@11: CellAutoContext *cellauto = ctx->priv; yading@11: int ret; yading@11: yading@11: ret = av_file_map(cellauto->filename, yading@11: &cellauto->file_buf, &cellauto->file_bufsize, 0, ctx); yading@11: if (ret < 0) yading@11: return ret; yading@11: yading@11: /* create a string based on the read file */ yading@11: cellauto->pattern = av_malloc(cellauto->file_bufsize + 1); yading@11: if (!cellauto->pattern) yading@11: return AVERROR(ENOMEM); yading@11: memcpy(cellauto->pattern, cellauto->file_buf, cellauto->file_bufsize); yading@11: cellauto->pattern[cellauto->file_bufsize] = 0; yading@11: yading@11: return init_pattern_from_string(ctx); yading@11: } yading@11: yading@11: static int init(AVFilterContext *ctx) yading@11: { yading@11: CellAutoContext *cellauto = ctx->priv; yading@11: int ret; yading@11: yading@11: if (!cellauto->w && !cellauto->filename && !cellauto->pattern) yading@11: av_opt_set(cellauto, "size", "320x518", 0); yading@11: yading@11: if (cellauto->filename && cellauto->pattern) { yading@11: av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n"); yading@11: return AVERROR(EINVAL); yading@11: } yading@11: yading@11: if (cellauto->filename) { yading@11: if ((ret = init_pattern_from_file(ctx)) < 0) yading@11: return ret; yading@11: } else if (cellauto->pattern) { yading@11: if ((ret = init_pattern_from_string(ctx)) < 0) yading@11: return ret; yading@11: } else { yading@11: /* fill the first row randomly */ yading@11: int i; yading@11: yading@11: cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h); yading@11: if (!cellauto->buf) yading@11: return AVERROR(ENOMEM); yading@11: if (cellauto->random_seed == -1) yading@11: cellauto->random_seed = av_get_random_seed(); yading@11: yading@11: av_lfg_init(&cellauto->lfg, cellauto->random_seed); yading@11: yading@11: for (i = 0; i < cellauto->w; i++) { yading@11: double r = (double)av_lfg_get(&cellauto->lfg) / UINT32_MAX; yading@11: if (r <= cellauto->random_fill_ratio) yading@11: cellauto->buf[i] = 1; yading@11: } yading@11: } yading@11: yading@11: av_log(ctx, AV_LOG_VERBOSE, yading@11: "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n", yading@11: cellauto->w, cellauto->h, cellauto->frame_rate.num, cellauto->frame_rate.den, yading@11: cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full, yading@11: cellauto->random_seed); yading@11: return 0; yading@11: } yading@11: yading@11: static av_cold void uninit(AVFilterContext *ctx) yading@11: { yading@11: CellAutoContext *cellauto = ctx->priv; yading@11: yading@11: av_file_unmap(cellauto->file_buf, cellauto->file_bufsize); yading@11: av_freep(&cellauto->buf); yading@11: av_freep(&cellauto->pattern); yading@11: } yading@11: yading@11: static int config_props(AVFilterLink *outlink) yading@11: { yading@11: CellAutoContext *cellauto = outlink->src->priv; yading@11: yading@11: outlink->w = cellauto->w; yading@11: outlink->h = cellauto->h; yading@11: outlink->time_base = av_inv_q(cellauto->frame_rate); yading@11: yading@11: return 0; yading@11: } yading@11: yading@11: static void evolve(AVFilterContext *ctx) yading@11: { yading@11: CellAutoContext *cellauto = ctx->priv; yading@11: int i, v, pos[3]; yading@11: uint8_t *row, *prev_row = cellauto->buf + cellauto->buf_row_idx * cellauto->w; yading@11: enum { NW, N, NE }; yading@11: yading@11: cellauto->buf_prev_row_idx = cellauto->buf_row_idx; yading@11: cellauto->buf_row_idx = cellauto->buf_row_idx == cellauto->h-1 ? 0 : cellauto->buf_row_idx+1; yading@11: row = cellauto->buf + cellauto->w * cellauto->buf_row_idx; yading@11: yading@11: for (i = 0; i < cellauto->w; i++) { yading@11: if (cellauto->stitch) { yading@11: pos[NW] = i-1 < 0 ? cellauto->w-1 : i-1; yading@11: pos[N] = i; yading@11: pos[NE] = i+1 == cellauto->w ? 0 : i+1; yading@11: v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]]; yading@11: } else { yading@11: v = 0; yading@11: v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0; yading@11: v|= prev_row[i ]<<1 ; yading@11: v|= i+1 < cellauto->w ? prev_row[i+1] : 0; yading@11: } yading@11: row[i] = !!(cellauto->rule & (1< cell:%d\n", i, yading@11: v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]); yading@11: } yading@11: yading@11: cellauto->generation++; yading@11: } yading@11: yading@11: static void fill_picture(AVFilterContext *ctx, AVFrame *picref) yading@11: { yading@11: CellAutoContext *cellauto = ctx->priv; yading@11: int i, j, k, row_idx = 0; yading@11: uint8_t *p0 = picref->data[0]; yading@11: yading@11: if (cellauto->scroll && cellauto->generation >= cellauto->h) yading@11: /* show on top the oldest row */ yading@11: row_idx = (cellauto->buf_row_idx + 1) % cellauto->h; yading@11: yading@11: /* fill the output picture with the whole buffer */ yading@11: for (i = 0; i < cellauto->h; i++) { yading@11: uint8_t byte = 0; yading@11: uint8_t *row = cellauto->buf + row_idx*cellauto->w; yading@11: uint8_t *p = p0; yading@11: for (k = 0, j = 0; j < cellauto->w; j++) { yading@11: byte |= row[j]<<(7-k++); yading@11: if (k==8 || j == cellauto->w-1) { yading@11: k = 0; yading@11: *p++ = byte; yading@11: byte = 0; yading@11: } yading@11: } yading@11: row_idx = (row_idx + 1) % cellauto->h; yading@11: p0 += picref->linesize[0]; yading@11: } yading@11: } yading@11: yading@11: static int request_frame(AVFilterLink *outlink) yading@11: { yading@11: CellAutoContext *cellauto = outlink->src->priv; yading@11: AVFrame *picref = ff_get_video_buffer(outlink, cellauto->w, cellauto->h); yading@11: if (!picref) yading@11: return AVERROR(ENOMEM); yading@11: picref->sample_aspect_ratio = (AVRational) {1, 1}; yading@11: if (cellauto->generation == 0 && cellauto->start_full) { yading@11: int i; yading@11: for (i = 0; i < cellauto->h-1; i++) yading@11: evolve(outlink->src); yading@11: } yading@11: fill_picture(outlink->src, picref); yading@11: evolve(outlink->src); yading@11: yading@11: picref->pts = cellauto->pts++; yading@11: yading@11: #ifdef DEBUG yading@11: show_cellauto_row(outlink->src); yading@11: #endif yading@11: return ff_filter_frame(outlink, picref); yading@11: } yading@11: yading@11: static int query_formats(AVFilterContext *ctx) yading@11: { yading@11: static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE }; yading@11: ff_set_common_formats(ctx, ff_make_format_list(pix_fmts)); yading@11: return 0; yading@11: } yading@11: yading@11: static const AVFilterPad cellauto_outputs[] = { yading@11: { yading@11: .name = "default", yading@11: .type = AVMEDIA_TYPE_VIDEO, yading@11: .request_frame = request_frame, yading@11: .config_props = config_props, yading@11: }, yading@11: { NULL } yading@11: }; yading@11: yading@11: AVFilter avfilter_vsrc_cellauto = { yading@11: .name = "cellauto", yading@11: .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."), yading@11: .priv_size = sizeof(CellAutoContext), yading@11: .init = init, yading@11: .uninit = uninit, yading@11: .query_formats = query_formats, yading@11: .inputs = NULL, yading@11: .outputs = cellauto_outputs, yading@11: .priv_class = &cellauto_class, yading@11: };