vsrc_cellauto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 "libavutil/avstring.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "formats.h"
  34. #include "video.h"
  35. typedef struct {
  36. const AVClass *class;
  37. int w, h;
  38. char *filename;
  39. char *rule_str;
  40. uint8_t *file_buf;
  41. size_t file_bufsize;
  42. uint8_t *buf;
  43. int buf_prev_row_idx, buf_row_idx;
  44. uint8_t rule;
  45. uint64_t pts;
  46. AVRational 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(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, 0, FLAGS },
  62. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.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] = !!av_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 av_cold int init(AVFilterContext *ctx)
  143. {
  144. CellAutoContext *cellauto = ctx->priv;
  145. int ret;
  146. if (!cellauto->w && !cellauto->filename && !cellauto->pattern)
  147. av_opt_set(cellauto, "size", "320x518", 0);
  148. if (cellauto->filename && cellauto->pattern) {
  149. av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
  150. return AVERROR(EINVAL);
  151. }
  152. if (cellauto->filename) {
  153. if ((ret = init_pattern_from_file(ctx)) < 0)
  154. return ret;
  155. } else if (cellauto->pattern) {
  156. if ((ret = init_pattern_from_string(ctx)) < 0)
  157. return ret;
  158. } else {
  159. /* fill the first row randomly */
  160. int i;
  161. cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
  162. if (!cellauto->buf)
  163. return AVERROR(ENOMEM);
  164. if (cellauto->random_seed == -1)
  165. cellauto->random_seed = av_get_random_seed();
  166. av_lfg_init(&cellauto->lfg, cellauto->random_seed);
  167. for (i = 0; i < cellauto->w; i++) {
  168. double r = (double)av_lfg_get(&cellauto->lfg) / UINT32_MAX;
  169. if (r <= cellauto->random_fill_ratio)
  170. cellauto->buf[i] = 1;
  171. }
  172. }
  173. av_log(ctx, AV_LOG_VERBOSE,
  174. "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
  175. cellauto->w, cellauto->h, cellauto->frame_rate.num, cellauto->frame_rate.den,
  176. cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full,
  177. cellauto->random_seed);
  178. return 0;
  179. }
  180. static av_cold void uninit(AVFilterContext *ctx)
  181. {
  182. CellAutoContext *cellauto = ctx->priv;
  183. av_file_unmap(cellauto->file_buf, cellauto->file_bufsize);
  184. av_freep(&cellauto->buf);
  185. av_freep(&cellauto->pattern);
  186. }
  187. static int config_props(AVFilterLink *outlink)
  188. {
  189. CellAutoContext *cellauto = outlink->src->priv;
  190. outlink->w = cellauto->w;
  191. outlink->h = cellauto->h;
  192. outlink->time_base = av_inv_q(cellauto->frame_rate);
  193. return 0;
  194. }
  195. static void evolve(AVFilterContext *ctx)
  196. {
  197. CellAutoContext *cellauto = ctx->priv;
  198. int i, v, pos[3];
  199. uint8_t *row, *prev_row = cellauto->buf + cellauto->buf_row_idx * cellauto->w;
  200. enum { NW, N, NE };
  201. cellauto->buf_prev_row_idx = cellauto->buf_row_idx;
  202. cellauto->buf_row_idx = cellauto->buf_row_idx == cellauto->h-1 ? 0 : cellauto->buf_row_idx+1;
  203. row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
  204. for (i = 0; i < cellauto->w; i++) {
  205. if (cellauto->stitch) {
  206. pos[NW] = i-1 < 0 ? cellauto->w-1 : i-1;
  207. pos[N] = i;
  208. pos[NE] = i+1 == cellauto->w ? 0 : i+1;
  209. v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
  210. } else {
  211. v = 0;
  212. v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
  213. v|= prev_row[i ]<<1 ;
  214. v|= i+1 < cellauto->w ? prev_row[i+1] : 0;
  215. }
  216. row[i] = !!(cellauto->rule & (1<<v));
  217. av_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
  218. v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
  219. }
  220. cellauto->generation++;
  221. }
  222. static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
  223. {
  224. CellAutoContext *cellauto = ctx->priv;
  225. int i, j, k, row_idx = 0;
  226. uint8_t *p0 = picref->data[0];
  227. if (cellauto->scroll && cellauto->generation >= cellauto->h)
  228. /* show on top the oldest row */
  229. row_idx = (cellauto->buf_row_idx + 1) % cellauto->h;
  230. /* fill the output picture with the whole buffer */
  231. for (i = 0; i < cellauto->h; i++) {
  232. uint8_t byte = 0;
  233. uint8_t *row = cellauto->buf + row_idx*cellauto->w;
  234. uint8_t *p = p0;
  235. for (k = 0, j = 0; j < cellauto->w; j++) {
  236. byte |= row[j]<<(7-k++);
  237. if (k==8 || j == cellauto->w-1) {
  238. k = 0;
  239. *p++ = byte;
  240. byte = 0;
  241. }
  242. }
  243. row_idx = (row_idx + 1) % cellauto->h;
  244. p0 += picref->linesize[0];
  245. }
  246. }
  247. static int request_frame(AVFilterLink *outlink)
  248. {
  249. CellAutoContext *cellauto = outlink->src->priv;
  250. AVFrame *picref = ff_get_video_buffer(outlink, cellauto->w, cellauto->h);
  251. if (!picref)
  252. return AVERROR(ENOMEM);
  253. picref->sample_aspect_ratio = (AVRational) {1, 1};
  254. if (cellauto->generation == 0 && cellauto->start_full) {
  255. int i;
  256. for (i = 0; i < cellauto->h-1; i++)
  257. evolve(outlink->src);
  258. }
  259. fill_picture(outlink->src, picref);
  260. evolve(outlink->src);
  261. picref->pts = cellauto->pts++;
  262. #ifdef DEBUG
  263. show_cellauto_row(outlink->src);
  264. #endif
  265. return ff_filter_frame(outlink, picref);
  266. }
  267. static int query_formats(AVFilterContext *ctx)
  268. {
  269. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE };
  270. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  271. return 0;
  272. }
  273. static const AVFilterPad cellauto_outputs[] = {
  274. {
  275. .name = "default",
  276. .type = AVMEDIA_TYPE_VIDEO,
  277. .request_frame = request_frame,
  278. .config_props = config_props,
  279. },
  280. { NULL }
  281. };
  282. AVFilter ff_vsrc_cellauto = {
  283. .name = "cellauto",
  284. .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
  285. .priv_size = sizeof(CellAutoContext),
  286. .priv_class = &cellauto_class,
  287. .init = init,
  288. .uninit = uninit,
  289. .query_formats = query_formats,
  290. .inputs = NULL,
  291. .outputs = cellauto_outputs,
  292. };