vsrc_cellauto.c 12 KB

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