annotate ffmpeg/libavfilter/vsrc_cellauto.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents f445c3017523
children
rev   line source
yading@11 1 /*
yading@11 2 * Copyright (c) Stefano Sabatini 2011
yading@11 3 *
yading@11 4 * This file is part of FFmpeg.
yading@11 5 *
yading@11 6 * FFmpeg is free software; you can redistribute it and/or
yading@11 7 * modify it under the terms of the GNU Lesser General Public
yading@11 8 * License as published by the Free Software Foundation; either
yading@11 9 * version 2.1 of the License, or (at your option) any later version.
yading@11 10 *
yading@11 11 * FFmpeg is distributed in the hope that it will be useful,
yading@11 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@11 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@11 14 * Lesser General Public License for more details.
yading@11 15 *
yading@11 16 * You should have received a copy of the GNU Lesser General Public
yading@11 17 * License along with FFmpeg; if not, write to the Free Software
yading@11 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@11 19 */
yading@11 20
yading@11 21 /**
yading@11 22 * @file
yading@11 23 * cellular automaton video source, based on Stephen Wolfram "experimentus crucis"
yading@11 24 */
yading@11 25
yading@11 26 /* #define DEBUG */
yading@11 27
yading@11 28 #include "libavutil/file.h"
yading@11 29 #include "libavutil/lfg.h"
yading@11 30 #include "libavutil/opt.h"
yading@11 31 #include "libavutil/parseutils.h"
yading@11 32 #include "libavutil/random_seed.h"
yading@11 33 #include "libavutil/avstring.h"
yading@11 34 #include "avfilter.h"
yading@11 35 #include "internal.h"
yading@11 36 #include "formats.h"
yading@11 37 #include "video.h"
yading@11 38
yading@11 39 typedef struct {
yading@11 40 const AVClass *class;
yading@11 41 int w, h;
yading@11 42 char *filename;
yading@11 43 char *rule_str;
yading@11 44 uint8_t *file_buf;
yading@11 45 size_t file_bufsize;
yading@11 46 uint8_t *buf;
yading@11 47 int buf_prev_row_idx, buf_row_idx;
yading@11 48 uint8_t rule;
yading@11 49 uint64_t pts;
yading@11 50 AVRational frame_rate;
yading@11 51 double random_fill_ratio;
yading@11 52 uint32_t random_seed;
yading@11 53 int stitch, scroll, start_full;
yading@11 54 int64_t generation; ///< the generation number, starting from 0
yading@11 55 AVLFG lfg;
yading@11 56 char *pattern;
yading@11 57 } CellAutoContext;
yading@11 58
yading@11 59 #define OFFSET(x) offsetof(CellAutoContext, x)
yading@11 60 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
yading@11 61
yading@11 62 static const AVOption cellauto_options[] = {
yading@11 63 { "filename", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
yading@11 64 { "f", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
yading@11 65 { "pattern", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
yading@11 66 { "p", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
yading@11 67 { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
yading@11 68 { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
yading@11 69 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
yading@11 70 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
yading@11 71 { "rule", "set rule", OFFSET(rule), AV_OPT_TYPE_INT, {.i64 = 110}, 0, 255, FLAGS },
yading@11 72 { "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 73 { "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 74 { "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 75 { "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 76 { "scroll", "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
yading@11 77 { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
yading@11 78 { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
yading@11 79 { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
yading@11 80 { NULL },
yading@11 81 };
yading@11 82
yading@11 83 AVFILTER_DEFINE_CLASS(cellauto);
yading@11 84
yading@11 85 #ifdef DEBUG
yading@11 86 static void show_cellauto_row(AVFilterContext *ctx)
yading@11 87 {
yading@11 88 CellAutoContext *cellauto = ctx->priv;
yading@11 89 int i;
yading@11 90 uint8_t *row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
yading@11 91 char *line = av_malloc(cellauto->w + 1);
yading@11 92 if (!line)
yading@11 93 return;
yading@11 94
yading@11 95 for (i = 0; i < cellauto->w; i++)
yading@11 96 line[i] = row[i] ? '@' : ' ';
yading@11 97 line[i] = 0;
yading@11 98 av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", cellauto->generation, line);
yading@11 99 av_free(line);
yading@11 100 }
yading@11 101 #endif
yading@11 102
yading@11 103 static int init_pattern_from_string(AVFilterContext *ctx)
yading@11 104 {
yading@11 105 CellAutoContext *cellauto = ctx->priv;
yading@11 106 char *p;
yading@11 107 int i, w = 0;
yading@11 108
yading@11 109 w = strlen(cellauto->pattern);
yading@11 110 av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
yading@11 111
yading@11 112 if (cellauto->w) {
yading@11 113 if (w > cellauto->w) {
yading@11 114 av_log(ctx, AV_LOG_ERROR,
yading@11 115 "The specified width is %d which cannot contain the provided string width of %d\n",
yading@11 116 cellauto->w, w);
yading@11 117 return AVERROR(EINVAL);
yading@11 118 }
yading@11 119 } else {
yading@11 120 /* width was not specified, set it to width of the provided row */
yading@11 121 cellauto->w = w;
yading@11 122 cellauto->h = (double)cellauto->w * M_PHI;
yading@11 123 }
yading@11 124
yading@11 125 cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
yading@11 126 if (!cellauto->buf)
yading@11 127 return AVERROR(ENOMEM);
yading@11 128
yading@11 129 /* fill buf */
yading@11 130 p = cellauto->pattern;
yading@11 131 for (i = (cellauto->w - w)/2;; i++) {
yading@11 132 av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p);
yading@11 133 if (*p == '\n' || !*p)
yading@11 134 break;
yading@11 135 else
yading@11 136 cellauto->buf[i] = !!av_isgraph(*(p++));
yading@11 137 }
yading@11 138
yading@11 139 return 0;
yading@11 140 }
yading@11 141
yading@11 142 static int init_pattern_from_file(AVFilterContext *ctx)
yading@11 143 {
yading@11 144 CellAutoContext *cellauto = ctx->priv;
yading@11 145 int ret;
yading@11 146
yading@11 147 ret = av_file_map(cellauto->filename,
yading@11 148 &cellauto->file_buf, &cellauto->file_bufsize, 0, ctx);
yading@11 149 if (ret < 0)
yading@11 150 return ret;
yading@11 151
yading@11 152 /* create a string based on the read file */
yading@11 153 cellauto->pattern = av_malloc(cellauto->file_bufsize + 1);
yading@11 154 if (!cellauto->pattern)
yading@11 155 return AVERROR(ENOMEM);
yading@11 156 memcpy(cellauto->pattern, cellauto->file_buf, cellauto->file_bufsize);
yading@11 157 cellauto->pattern[cellauto->file_bufsize] = 0;
yading@11 158
yading@11 159 return init_pattern_from_string(ctx);
yading@11 160 }
yading@11 161
yading@11 162 static int init(AVFilterContext *ctx)
yading@11 163 {
yading@11 164 CellAutoContext *cellauto = ctx->priv;
yading@11 165 int ret;
yading@11 166
yading@11 167 if (!cellauto->w && !cellauto->filename && !cellauto->pattern)
yading@11 168 av_opt_set(cellauto, "size", "320x518", 0);
yading@11 169
yading@11 170 if (cellauto->filename && cellauto->pattern) {
yading@11 171 av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
yading@11 172 return AVERROR(EINVAL);
yading@11 173 }
yading@11 174
yading@11 175 if (cellauto->filename) {
yading@11 176 if ((ret = init_pattern_from_file(ctx)) < 0)
yading@11 177 return ret;
yading@11 178 } else if (cellauto->pattern) {
yading@11 179 if ((ret = init_pattern_from_string(ctx)) < 0)
yading@11 180 return ret;
yading@11 181 } else {
yading@11 182 /* fill the first row randomly */
yading@11 183 int i;
yading@11 184
yading@11 185 cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
yading@11 186 if (!cellauto->buf)
yading@11 187 return AVERROR(ENOMEM);
yading@11 188 if (cellauto->random_seed == -1)
yading@11 189 cellauto->random_seed = av_get_random_seed();
yading@11 190
yading@11 191 av_lfg_init(&cellauto->lfg, cellauto->random_seed);
yading@11 192
yading@11 193 for (i = 0; i < cellauto->w; i++) {
yading@11 194 double r = (double)av_lfg_get(&cellauto->lfg) / UINT32_MAX;
yading@11 195 if (r <= cellauto->random_fill_ratio)
yading@11 196 cellauto->buf[i] = 1;
yading@11 197 }
yading@11 198 }
yading@11 199
yading@11 200 av_log(ctx, AV_LOG_VERBOSE,
yading@11 201 "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
yading@11 202 cellauto->w, cellauto->h, cellauto->frame_rate.num, cellauto->frame_rate.den,
yading@11 203 cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full,
yading@11 204 cellauto->random_seed);
yading@11 205 return 0;
yading@11 206 }
yading@11 207
yading@11 208 static av_cold void uninit(AVFilterContext *ctx)
yading@11 209 {
yading@11 210 CellAutoContext *cellauto = ctx->priv;
yading@11 211
yading@11 212 av_file_unmap(cellauto->file_buf, cellauto->file_bufsize);
yading@11 213 av_freep(&cellauto->buf);
yading@11 214 av_freep(&cellauto->pattern);
yading@11 215 }
yading@11 216
yading@11 217 static int config_props(AVFilterLink *outlink)
yading@11 218 {
yading@11 219 CellAutoContext *cellauto = outlink->src->priv;
yading@11 220
yading@11 221 outlink->w = cellauto->w;
yading@11 222 outlink->h = cellauto->h;
yading@11 223 outlink->time_base = av_inv_q(cellauto->frame_rate);
yading@11 224
yading@11 225 return 0;
yading@11 226 }
yading@11 227
yading@11 228 static void evolve(AVFilterContext *ctx)
yading@11 229 {
yading@11 230 CellAutoContext *cellauto = ctx->priv;
yading@11 231 int i, v, pos[3];
yading@11 232 uint8_t *row, *prev_row = cellauto->buf + cellauto->buf_row_idx * cellauto->w;
yading@11 233 enum { NW, N, NE };
yading@11 234
yading@11 235 cellauto->buf_prev_row_idx = cellauto->buf_row_idx;
yading@11 236 cellauto->buf_row_idx = cellauto->buf_row_idx == cellauto->h-1 ? 0 : cellauto->buf_row_idx+1;
yading@11 237 row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
yading@11 238
yading@11 239 for (i = 0; i < cellauto->w; i++) {
yading@11 240 if (cellauto->stitch) {
yading@11 241 pos[NW] = i-1 < 0 ? cellauto->w-1 : i-1;
yading@11 242 pos[N] = i;
yading@11 243 pos[NE] = i+1 == cellauto->w ? 0 : i+1;
yading@11 244 v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
yading@11 245 } else {
yading@11 246 v = 0;
yading@11 247 v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
yading@11 248 v|= prev_row[i ]<<1 ;
yading@11 249 v|= i+1 < cellauto->w ? prev_row[i+1] : 0;
yading@11 250 }
yading@11 251 row[i] = !!(cellauto->rule & (1<<v));
yading@11 252 av_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
yading@11 253 v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
yading@11 254 }
yading@11 255
yading@11 256 cellauto->generation++;
yading@11 257 }
yading@11 258
yading@11 259 static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
yading@11 260 {
yading@11 261 CellAutoContext *cellauto = ctx->priv;
yading@11 262 int i, j, k, row_idx = 0;
yading@11 263 uint8_t *p0 = picref->data[0];
yading@11 264
yading@11 265 if (cellauto->scroll && cellauto->generation >= cellauto->h)
yading@11 266 /* show on top the oldest row */
yading@11 267 row_idx = (cellauto->buf_row_idx + 1) % cellauto->h;
yading@11 268
yading@11 269 /* fill the output picture with the whole buffer */
yading@11 270 for (i = 0; i < cellauto->h; i++) {
yading@11 271 uint8_t byte = 0;
yading@11 272 uint8_t *row = cellauto->buf + row_idx*cellauto->w;
yading@11 273 uint8_t *p = p0;
yading@11 274 for (k = 0, j = 0; j < cellauto->w; j++) {
yading@11 275 byte |= row[j]<<(7-k++);
yading@11 276 if (k==8 || j == cellauto->w-1) {
yading@11 277 k = 0;
yading@11 278 *p++ = byte;
yading@11 279 byte = 0;
yading@11 280 }
yading@11 281 }
yading@11 282 row_idx = (row_idx + 1) % cellauto->h;
yading@11 283 p0 += picref->linesize[0];
yading@11 284 }
yading@11 285 }
yading@11 286
yading@11 287 static int request_frame(AVFilterLink *outlink)
yading@11 288 {
yading@11 289 CellAutoContext *cellauto = outlink->src->priv;
yading@11 290 AVFrame *picref = ff_get_video_buffer(outlink, cellauto->w, cellauto->h);
yading@11 291 if (!picref)
yading@11 292 return AVERROR(ENOMEM);
yading@11 293 picref->sample_aspect_ratio = (AVRational) {1, 1};
yading@11 294 if (cellauto->generation == 0 && cellauto->start_full) {
yading@11 295 int i;
yading@11 296 for (i = 0; i < cellauto->h-1; i++)
yading@11 297 evolve(outlink->src);
yading@11 298 }
yading@11 299 fill_picture(outlink->src, picref);
yading@11 300 evolve(outlink->src);
yading@11 301
yading@11 302 picref->pts = cellauto->pts++;
yading@11 303
yading@11 304 #ifdef DEBUG
yading@11 305 show_cellauto_row(outlink->src);
yading@11 306 #endif
yading@11 307 return ff_filter_frame(outlink, picref);
yading@11 308 }
yading@11 309
yading@11 310 static int query_formats(AVFilterContext *ctx)
yading@11 311 {
yading@11 312 static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE };
yading@11 313 ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
yading@11 314 return 0;
yading@11 315 }
yading@11 316
yading@11 317 static const AVFilterPad cellauto_outputs[] = {
yading@11 318 {
yading@11 319 .name = "default",
yading@11 320 .type = AVMEDIA_TYPE_VIDEO,
yading@11 321 .request_frame = request_frame,
yading@11 322 .config_props = config_props,
yading@11 323 },
yading@11 324 { NULL }
yading@11 325 };
yading@11 326
yading@11 327 AVFilter avfilter_vsrc_cellauto = {
yading@11 328 .name = "cellauto",
yading@11 329 .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
yading@11 330 .priv_size = sizeof(CellAutoContext),
yading@11 331 .init = init,
yading@11 332 .uninit = uninit,
yading@11 333 .query_formats = query_formats,
yading@11 334 .inputs = NULL,
yading@11 335 .outputs = cellauto_outputs,
yading@11 336 .priv_class = &cellauto_class,
yading@11 337 };