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.

356 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. return ret;
  150. if ((ret = av_parse_video_rate(&frame_rate, cellauto->rate)) < 0) {
  151. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", cellauto->rate);
  152. return AVERROR(EINVAL);
  153. }
  154. if (!cellauto->w && !cellauto->filename && !cellauto->pattern)
  155. av_opt_set(cellauto, "size", "320x518", 0);
  156. cellauto->time_base.num = frame_rate.den;
  157. cellauto->time_base.den = frame_rate.num;
  158. if (cellauto->filename && cellauto->pattern) {
  159. av_log(ctx, AV_LOG_ERROR, "Only one of the filename or pattern options can be used\n");
  160. return AVERROR(EINVAL);
  161. }
  162. if (cellauto->filename) {
  163. if ((ret = init_pattern_from_file(ctx)) < 0)
  164. return ret;
  165. } else if (cellauto->pattern) {
  166. if ((ret = init_pattern_from_string(ctx)) < 0)
  167. return ret;
  168. } else {
  169. /* fill the first row randomly */
  170. int i;
  171. cellauto->buf = av_mallocz(sizeof(uint8_t) * cellauto->w * cellauto->h);
  172. if (!cellauto->buf)
  173. return AVERROR(ENOMEM);
  174. if (cellauto->random_seed == -1)
  175. cellauto->random_seed = av_get_random_seed();
  176. av_lfg_init(&cellauto->lfg, cellauto->random_seed);
  177. for (i = 0; i < cellauto->w; i++) {
  178. double r = (double)av_lfg_get(&cellauto->lfg) / UINT32_MAX;
  179. if (r <= cellauto->random_fill_ratio)
  180. cellauto->buf[i] = 1;
  181. }
  182. }
  183. av_log(ctx, AV_LOG_VERBOSE,
  184. "s:%dx%d r:%d/%d rule:%d stitch:%d scroll:%d full:%d seed:%u\n",
  185. cellauto->w, cellauto->h, frame_rate.num, frame_rate.den,
  186. cellauto->rule, cellauto->stitch, cellauto->scroll, cellauto->start_full,
  187. cellauto->random_seed);
  188. return 0;
  189. }
  190. static av_cold void uninit(AVFilterContext *ctx)
  191. {
  192. CellAutoContext *cellauto = ctx->priv;
  193. av_file_unmap(cellauto->file_buf, cellauto->file_bufsize);
  194. av_freep(&cellauto->buf);
  195. av_freep(&cellauto->pattern);
  196. }
  197. static int config_props(AVFilterLink *outlink)
  198. {
  199. CellAutoContext *cellauto = outlink->src->priv;
  200. outlink->w = cellauto->w;
  201. outlink->h = cellauto->h;
  202. outlink->time_base = cellauto->time_base;
  203. return 0;
  204. }
  205. static void evolve(AVFilterContext *ctx)
  206. {
  207. CellAutoContext *cellauto = ctx->priv;
  208. int i, v, pos[3];
  209. uint8_t *row, *prev_row = cellauto->buf + cellauto->buf_row_idx * cellauto->w;
  210. enum { NW, N, NE };
  211. cellauto->buf_prev_row_idx = cellauto->buf_row_idx;
  212. cellauto->buf_row_idx = cellauto->buf_row_idx == cellauto->h-1 ? 0 : cellauto->buf_row_idx+1;
  213. row = cellauto->buf + cellauto->w * cellauto->buf_row_idx;
  214. for (i = 0; i < cellauto->w; i++) {
  215. if (cellauto->stitch) {
  216. pos[NW] = i-1 < 0 ? cellauto->w-1 : i-1;
  217. pos[N] = i;
  218. pos[NE] = i+1 == cellauto->w ? 0 : i+1;
  219. v = prev_row[pos[NW]]<<2 | prev_row[pos[N]]<<1 | prev_row[pos[NE]];
  220. } else {
  221. v = 0;
  222. v|= i-1 >= 0 ? prev_row[i-1]<<2 : 0;
  223. v|= prev_row[i ]<<1 ;
  224. v|= i+1 < cellauto->w ? prev_row[i+1] : 0;
  225. }
  226. row[i] = !!(cellauto->rule & (1<<v));
  227. av_dlog(ctx, "i:%d context:%c%c%c -> cell:%d\n", i,
  228. v&4?'@':' ', v&2?'@':' ', v&1?'@':' ', row[i]);
  229. }
  230. cellauto->generation++;
  231. }
  232. static void fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  233. {
  234. CellAutoContext *cellauto = ctx->priv;
  235. int i, j, k, row_idx = 0;
  236. uint8_t *p0 = picref->data[0];
  237. if (cellauto->scroll && cellauto->generation >= cellauto->h)
  238. /* show on top the oldest row */
  239. row_idx = (cellauto->buf_row_idx + 1) % cellauto->h;
  240. /* fill the output picture with the whole buffer */
  241. for (i = 0; i < cellauto->h; i++) {
  242. uint8_t byte = 0;
  243. uint8_t *row = cellauto->buf + row_idx*cellauto->w;
  244. uint8_t *p = p0;
  245. for (k = 0, j = 0; j < cellauto->w; j++) {
  246. byte |= row[j]<<(7-k++);
  247. if (k==8 || j == cellauto->w-1) {
  248. k = 0;
  249. *p++ = byte;
  250. byte = 0;
  251. }
  252. }
  253. row_idx = (row_idx + 1) % cellauto->h;
  254. p0 += picref->linesize[0];
  255. }
  256. }
  257. static int request_frame(AVFilterLink *outlink)
  258. {
  259. CellAutoContext *cellauto = outlink->src->priv;
  260. AVFilterBufferRef *picref =
  261. ff_get_video_buffer(outlink, AV_PERM_WRITE, cellauto->w, cellauto->h);
  262. picref->video->sample_aspect_ratio = (AVRational) {1, 1};
  263. if (cellauto->generation == 0 && cellauto->start_full) {
  264. int i;
  265. for (i = 0; i < cellauto->h-1; i++)
  266. evolve(outlink->src);
  267. }
  268. fill_picture(outlink->src, picref);
  269. evolve(outlink->src);
  270. picref->pts = cellauto->pts++;
  271. picref->pos = -1;
  272. #ifdef DEBUG
  273. show_cellauto_row(outlink->src);
  274. #endif
  275. ff_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  276. ff_draw_slice(outlink, 0, cellauto->h, 1);
  277. ff_end_frame(outlink);
  278. avfilter_unref_buffer(picref);
  279. return 0;
  280. }
  281. static int query_formats(AVFilterContext *ctx)
  282. {
  283. static const enum PixelFormat pix_fmts[] = { PIX_FMT_MONOBLACK, PIX_FMT_NONE };
  284. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  285. return 0;
  286. }
  287. AVFilter avfilter_vsrc_cellauto = {
  288. .name = "cellauto",
  289. .description = NULL_IF_CONFIG_SMALL("Create pattern generated by an elementary cellular automaton."),
  290. .priv_size = sizeof(CellAutoContext),
  291. .init = init,
  292. .uninit = uninit,
  293. .query_formats = query_formats,
  294. .inputs = (const AVFilterPad[]) {
  295. { .name = NULL}
  296. },
  297. .outputs = (const AVFilterPad[]) {
  298. { .name = "default",
  299. .type = AVMEDIA_TYPE_VIDEO,
  300. .request_frame = request_frame,
  301. .config_props = config_props },
  302. { .name = NULL}
  303. },
  304. };