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.

354 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 "libavutil/avstring.h"
  31. #include "avfilter.h"
  32. #include "internal.h"
  33. #include "formats.h"
  34. #include "video.h"
  35. typedef struct {
  36. const AVClass *class;
  37. int w, h;
  38. char *filename;
  39. char *rule_str;
  40. uint8_t *file_buf;
  41. size_t file_bufsize;
  42. uint8_t *buf;
  43. int buf_prev_row_idx, buf_row_idx;
  44. uint8_t rule;
  45. uint64_t pts;
  46. AVRational time_base;
  47. char *rate; ///< video 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(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, FLAGS },
  63. { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0, 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_INT, {.i64 = 1}, 0, 1, FLAGS },
  72. { "start_full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, FLAGS },
  73. { "full", "start filling the whole video", OFFSET(start_full), AV_OPT_TYPE_INT, {.i64 = 1}, 0, 1, FLAGS },
  74. { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.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 *cellauto = ctx->priv;
  82. int i;
  83. uint8_t *row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
  84. char *line = av_malloc(cellauto->w + 1);
  85. if (!line)
  86. return;
  87. for (i = 0; i < cellauto->w; i++)
  88. line[i] = row[i] ? '@' : ' ';
  89. line[i] = 0;
  90. av_log(ctx, AV_LOG_DEBUG, "generation:%"PRId64" row:%s|\n", cellauto->generation, line);
  91. av_free(line);
  92. }
  93. #endif
  94. static int init_pattern_from_string(AVFilterContext *ctx)
  95. {
  96. CellAutoContext *cellauto = ctx->priv;
  97. char *p;
  98. int i, w = 0;
  99. w = strlen(cellauto->pattern);
  100. av_log(ctx, AV_LOG_DEBUG, "w:%d\n", w);
  101. if (cellauto->w) {
  102. if (w > cellauto->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. cellauto->w, w);
  106. return AVERROR(EINVAL);
  107. }
  108. } else {
  109. /* width was not specified, set it to width of the provided row */
  110. cellauto->w = w;
  111. cellauto->h = (double)cellauto->w * M_PHI;
  112. }
  113. cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
  114. if (!cellauto->buf)
  115. return AVERROR(ENOMEM);
  116. /* fill buf */
  117. p = cellauto->pattern;
  118. for (i = (cellauto->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. cellauto->buf[i] = !!av_isgraph(*(p++));
  124. }
  125. return 0;
  126. }
  127. static int init_pattern_from_file(AVFilterContext *ctx)
  128. {
  129. CellAutoContext *cellauto = ctx->priv;
  130. int ret;
  131. ret = av_file_map(cellauto->filename,
  132. &cellauto->file_buf, &cellauto->file_bufsize, 0, ctx);
  133. if (ret < 0)
  134. return ret;
  135. /* create a string based on the read file */
  136. cellauto->pattern = av_malloc(cellauto->file_bufsize + 1);
  137. if (!cellauto->pattern)
  138. return AVERROR(ENOMEM);
  139. memcpy(cellauto->pattern, cellauto->file_buf, cellauto->file_bufsize);
  140. cellauto->pattern[cellauto->file_bufsize] = 0;
  141. return init_pattern_from_string(ctx);
  142. }
  143. static int init(AVFilterContext *ctx, const char *args)
  144. {
  145. CellAutoContext *cellauto = ctx->priv;
  146. AVRational frame_rate;
  147. int ret;
  148. cellauto->class = &cellauto_class;
  149. av_opt_set_defaults(cellauto);
  150. if ((ret = av_set_options_string(cellauto, args, "=", ":")) < 0)
  151. return ret;
  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, AVFrame *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. AVFrame *picref = ff_get_video_buffer(outlink, cellauto->w, cellauto->h);
  263. picref->sample_aspect_ratio = (AVRational) {1, 1};
  264. if (cellauto->generation == 0 && cellauto->start_full) {
  265. int i;
  266. for (i = 0; i < cellauto->h-1; i++)
  267. evolve(outlink->src);
  268. }
  269. fill_picture(outlink->src, picref);
  270. evolve(outlink->src);
  271. picref->pts = cellauto->pts++;
  272. #ifdef DEBUG
  273. show_cellauto_row(outlink->src);
  274. #endif
  275. ff_filter_frame(outlink, picref);
  276. return 0;
  277. }
  278. static int query_formats(AVFilterContext *ctx)
  279. {
  280. static const enum AVPixelFormat pix_fmts[] = { AV_PIX_FMT_MONOBLACK, AV_PIX_FMT_NONE };
  281. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  282. return 0;
  283. }
  284. static const AVFilterPad cellauto_outputs[] = {
  285. {
  286. .name = "default",
  287. .type = AVMEDIA_TYPE_VIDEO,
  288. .request_frame = request_frame,
  289. .config_props = config_props,
  290. },
  291. { NULL }
  292. };
  293. AVFilter avfilter_vsrc_cellauto = {
  294. .name = "cellauto",
  295. .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
  296. .priv_size = sizeof(CellAutoContext),
  297. .init = init,
  298. .uninit = uninit,
  299. .query_formats = query_formats,
  300. .inputs = NULL,
  301. .outputs = cellauto_outputs,
  302. .priv_class = &cellauto_class,
  303. };