vsrc_cellauto.c 12 KB

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