You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
12KB

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