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.

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