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.

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