vsrc_cellauto.c 12 KB

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