vsrc_cellauto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/internal.h"
  27. #include "libavutil/lfg.h"
  28. #include "libavutil/opt.h"
  29. #include "libavutil/parseutils.h"
  30. #include "libavutil/random_seed.h"
  31. #include "libavutil/avstring.h"
  32. #include "avfilter.h"
  33. #include "internal.h"
  34. #include "formats.h"
  35. #include "video.h"
  36. typedef struct CellAutoContext {
  37. const AVClass *class;
  38. int w, h;
  39. char *filename;
  40. char *rule_str;
  41. uint8_t *file_buf;
  42. size_t file_bufsize;
  43. uint8_t *buf;
  44. int buf_prev_row_idx, buf_row_idx;
  45. uint8_t rule;
  46. uint64_t pts;
  47. AVRational frame_rate;
  48. double random_fill_ratio;
  49. uint32_t random_seed;
  50. int stitch, scroll, start_full;
  51. int64_t generation; ///< the generation number, starting from 0
  52. AVLFG lfg;
  53. char *pattern;
  54. } CellAutoContext;
  55. #define OFFSET(x) offsetof(CellAutoContext, x)
  56. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  57. static const AVOption cellauto_options[] = {
  58. { "filename", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  59. { "f", "read initial pattern from file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  60. { "pattern", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  61. { "p", "set initial pattern", OFFSET(pattern), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0, FLAGS },
  62. { "rate", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
  63. { "r", "set video rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str = "25"}, 0, INT_MAX, FLAGS },
  64. { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
  65. { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = NULL}, 0, 0, FLAGS },
  66. { "rule", "set rule", OFFSET(rule), AV_OPT_TYPE_INT, {.i64 = 110}, 0, 255, FLAGS },
  67. { "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 },
  68. { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl = 1/M_PHI}, 0, 1, FLAGS },
  69. { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
  70. { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.i64 = -1}, -1, UINT32_MAX, FLAGS },
  71. { "scroll", "scroll pattern downward", OFFSET(scroll), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
  72. { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, FLAGS },
  73. { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
  74. { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, FLAGS },
  75. { NULL }
  76. };
  77. AVFILTER_DEFINE_CLASS(cellauto);
  78. #ifdef DEBUG
  79. static void show_cellauto_row(AVFilterContext *ctx)
  80. {
  81. CellAutoContext *s = ctx->priv;
  82. int i;
  83. uint8_t *row = s->buf + s->w * s->buf_row_idx;
  84. char *line = av_malloc(s->w + 1);
  85. if (!line)
  86. return;
  87. for (i = 0; i < s->w; i++)
  88. line[i] = row[i] ? '@' : ' ';
  89. line[i] = 0;
  90. av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", s->generation, line);
  91. av_free(line);
  92. }
  93. #endif
  94. static int init_pattern_from_string(AVFilterContext *ctx)
  95. {
  96. CellAutoContext *s = ctx->priv;
  97. char *p;
  98. int i, w = 0;
  99. w = strlen(s->pattern);
  100. av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
  101. if (s->w) {
  102. if (w > s->w) {
  103. av_log(ctx, AV_LOG_ERROR,
  104. "The specified width is %d which cannot contain the provided string width of %d\n",
  105. s->w, w);
  106. return AVERROR(EINVAL);
  107. }
  108. } else {
  109. /* width was not specified, set it to width of the provided row */
  110. s->w = w;
  111. s->h = (double)s->w * M_PHI;
  112. }
  113. s->buf = av_mallocz_array(sizeof(uint8_t) * s->w, s->h);
  114. if (!s->buf)
  115. return AVERROR(ENOMEM);
  116. /* fill buf */
  117. p = s->pattern;
  118. for (i = (s->w - w)/2;; i++) {
  119. av_log(ctx, AV_LOG_DEBUG, "%d %c\n", i, *p == '\n' ? 'N' : *p);
  120. if (*p == '\n' || !*p)
  121. break;
  122. else
  123. s->buf[i] = !!av_isgraph(*(p++));
  124. }
  125. return 0;
  126. }
  127. static int init_pattern_from_file(AVFilterContext *ctx)
  128. {
  129. CellAutoContext *s = ctx->priv;
  130. int ret;
  131. ret = av_file_map(s->filename,
  132. &s->file_buf, &s->file_bufsize, 0, ctx);
  133. if (ret < 0)
  134. return ret;
  135. /* create a string based on the read file */
  136. s->pattern = av_malloc(s->file_bufsize + 1);
  137. if (!s->pattern)
  138. return AVERROR(ENOMEM);
  139. memcpy(s->pattern, s->file_buf, s->file_bufsize);
  140. s->pattern[s->file_bufsize] = 0;
  141. return init_pattern_from_string(ctx);
  142. }
  143. static av_cold int init(AVFilterContext *ctx)
  144. {
  145. CellAutoContext *s = ctx->priv;
  146. int ret;
  147. if (!s->w && !s->filename && !s->pattern)
  148. av_opt_set(s, "size", "320x518", 0);
  149. if (s->filename && s->pattern) {
  150. av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
  151. return AVERROR(EINVAL);
  152. }
  153. if (s->filename) {
  154. if ((ret = init_pattern_from_file(ctx)) < 0)
  155. return ret;
  156. } else if (s->pattern) {
  157. if ((ret = init_pattern_from_string(ctx)) < 0)
  158. return ret;
  159. } else {
  160. /* fill the first row randomly */
  161. int i;
  162. s->buf = av_mallocz_array(sizeof(uint8_t) * s->w, s->h);
  163. if (!s->buf)
  164. return AVERROR(ENOMEM);
  165. if (s->random_seed == -1)
  166. s->random_seed = av_get_random_seed();
  167. av_lfg_init(&s->lfg, s->random_seed);
  168. for (i = 0; i < s->w; i++) {
  169. double r = (double)av_lfg_get(&s->lfg) / UINT32_MAX;
  170. if (r <= s->random_fill_ratio)
  171. s->buf[i] = 1;
  172. }
  173. }
  174. av_log(ctx, AV_LOG_VERBOSE,
  175. "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%"PRIu32"\n",
  176. s->w, s->h, s->frame_rate.num, s->frame_rate.den,
  177. s->rule, s->stitch, s->scroll, s->start_full,
  178. s->random_seed);
  179. return 0;
  180. }
  181. static av_cold void uninit(AVFilterContext *ctx)
  182. {
  183. CellAutoContext *s = ctx->priv;
  184. av_file_unmap(s->file_buf, s->file_bufsize);
  185. av_freep(&s->buf);
  186. av_freep(&s->pattern);
  187. }
  188. static int config_props(AVFilterLink *outlink)
  189. {
  190. CellAutoContext *s = outlink->src->priv;
  191. outlink->w = s->w;
  192. outlink->h = s->h;
  193. outlink->time_base = av_inv_q(s->frame_rate);
  194. return 0;
  195. }
  196. static void evolve(AVFilterContext *ctx)
  197. {
  198. CellAutoContext *s = ctx->priv;
  199. int i, v, pos[3];
  200. uint8_t *row, *prev_row = s->buf + s->buf_row_idx * s->w;
  201. enum { NW, N, NE };
  202. s->buf_prev_row_idx = s->buf_row_idx;
  203. s->buf_row_idx = s->buf_row_idx == s->h-1 ? 0 : s->buf_row_idx+1;
  204. row = s->buf + s->w * s->buf_row_idx;
  205. for (i = 0; i < s->w; i++) {
  206. if (s->stitch) {
  207. pos[NW] = i-1 < 0 ? s->w-1 : i-1;
  208. pos[N] = i;
  209. pos[NE] = i+1 == s->w ? 0 : i+1;
  210. v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
  211. } else {
  212. v = 0;
  213. v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
  214. v|= prev_row[i ]<<1 ;
  215. v|= i+1 < s->w ? prev_row[i+1] : 0;
  216. }
  217. row[i] = !!(s->rule & (1<<v));
  218. ff_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
  219. v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
  220. }
  221. s->generation++;
  222. }
  223. static void fill_picture(AVFilterContext *ctx, AVFrame *picref)
  224. {
  225. CellAutoContext *s = ctx->priv;
  226. int i, j, k, row_idx = 0;
  227. uint8_t *p0 = picref->data[0];
  228. if (s->scroll && s->generation >= s->h)
  229. /* show on top the oldest row */
  230. row_idx = (s->buf_row_idx + 1) % s->h;
  231. /* fill the output picture with the whole buffer */
  232. for (i = 0; i < s->h; i++) {
  233. uint8_t byte = 0;
  234. uint8_t *row = s->buf + row_idx*s->w;
  235. uint8_t *p = p0;
  236. for (k = 0, j = 0; j < s->w; j++) {
  237. byte |= row[j]<<(7-k++);
  238. if (k==8 || j == s->w-1) {
  239. k = 0;
  240. *p++ = byte;
  241. byte = 0;
  242. }
  243. }
  244. row_idx = (row_idx + 1) % s->h;
  245. p0 += picref->linesize[0];
  246. }
  247. }
  248. static int request_frame(AVFilterLink *outlink)
  249. {
  250. CellAutoContext *s = outlink->src->priv;
  251. AVFrame *picref = ff_get_video_buffer(outlink, s->w, s->h);
  252. if (!picref)
  253. return AVERROR(ENOMEM);
  254. picref->sample_aspect_ratio = (AVRational) {1, 1};
  255. if (s->generation == 0 && s->start_full) {
  256. int i;
  257. for (i = 0; i < s->h-1; i++)
  258. evolve(outlink->src);
  259. }
  260. fill_picture(outlink->src, picref);
  261. evolve(outlink->src);
  262. picref->pts = s->pts++;
  263. #ifdef DEBUG
  264. show_cellauto_row(outlink->src);
  265. #endif
  266. return ff_filter_frame(outlink, picref);
  267. }
  268. static int query_formats(AVFilterContext *ctx)
  269. {
  270. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE };
  271. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  272. if (!fmts_list)
  273. return AVERROR(ENOMEM);
  274. return ff_set_common_formats(ctx, fmts_list);
  275. }
  276. static const AVFilterPad cellauto_outputs[] = {
  277. {
  278. .name = "default",
  279. .type = AVMEDIA_TYPE_VIDEO,
  280. .request_frame = request_frame,
  281. .config_props = config_props,
  282. },
  283. { NULL }
  284. };
  285. AVFilter ff_vsrc_cellauto = {
  286. .name = "cellauto",
  287. .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
  288. .priv_size = sizeof(CellAutoContext),
  289. .priv_class = &cellauto_class,
  290. .init = init,
  291. .uninit = uninit,
  292. .query_formats = query_formats,
  293. .inputs = NULL,
  294. .outputs = cellauto_outputs,
  295. };