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.

411 lines
14KB

  1. /*
  2. * Copyright (c) Stefano Sabatini 2010
  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. * life video source, based on John Conways' Life Game
  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. char *buf[2];
  39. uint8_t buf_idx;
  40. uint16_t stay_rule; ///< encode the behavior for filled cells
  41. uint16_t born_rule; ///< encode the behavior for empty cells
  42. uint64_t pts;
  43. AVRational time_base;
  44. char *size; ///< video frame size
  45. char *rate; ///< video frame rate
  46. double random_fill_ratio;
  47. uint32_t random_seed;
  48. int stitch;
  49. AVLFG lfg;
  50. } LifeContext;
  51. #define OFFSET(x) offsetof(LifeContext, x)
  52. static const AVOption life_options[] = {
  53. { "filename", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  54. { "f", "set source file", OFFSET(filename), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  55. { "size", "set video size", OFFSET(size), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
  56. { "s", "set video size", OFFSET(size), 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. { "rule", "set rule", OFFSET(rule_str), AV_OPT_TYPE_STRING, {.str = "B3/S23"}, CHAR_MIN, CHAR_MAX },
  60. { "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 },
  61. { "ratio", "set fill ratio for filling initial grid randomly", OFFSET(random_fill_ratio), AV_OPT_TYPE_DOUBLE, {.dbl=1/M_PHI}, 0, 1 },
  62. { "random_seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.dbl=-1}, -1, UINT32_MAX },
  63. { "seed", "set the seed for filling the initial grid randomly", OFFSET(random_seed), AV_OPT_TYPE_INT, {.dbl=-1}, -1, UINT32_MAX },
  64. { "stitch", "stitch boundaries", OFFSET(stitch), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 },
  65. { NULL },
  66. };
  67. static const char *life_get_name(void *ctx)
  68. {
  69. return "life";
  70. }
  71. static const AVClass life_class = {
  72. "LifeContext",
  73. life_get_name,
  74. life_options
  75. };
  76. static int parse_rule(uint16_t *born_rule, uint16_t *stay_rule,
  77. const char *rule_str, void *log_ctx)
  78. {
  79. char *tail;
  80. const char *p = rule_str;
  81. *born_rule = 0;
  82. *stay_rule = 0;
  83. if (strchr("bBsS", *p)) {
  84. /* parse rule as a Born / Stay Alive code, see
  85. * http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life */
  86. do {
  87. uint16_t *rule = (*p == 'b' || *p == 'B') ? born_rule : stay_rule;
  88. p++;
  89. while (*p >= '0' && *p <= '8') {
  90. *rule += 1<<(*p - '0');
  91. p++;
  92. }
  93. if (*p != '/')
  94. break;
  95. p++;
  96. } while (strchr("bBsS", *p));
  97. if (*p)
  98. goto error;
  99. } else {
  100. /* parse rule as a number, expressed in the form STAY|(BORN<<9),
  101. * where STAY and BORN encode the corresponding 9-bits rule */
  102. long int rule = strtol(rule_str, &tail, 10);
  103. if (*tail)
  104. goto error;
  105. *born_rule = ((1<<9)-1) & rule;
  106. *stay_rule = rule >> 9;
  107. }
  108. return 0;
  109. error:
  110. av_log(log_ctx, AV_LOG_ERROR, "Invalid rule code '%s' provided\n", rule_str);
  111. return AVERROR(EINVAL);
  112. }
  113. #ifdef DEBUG
  114. static void show_life_grid(AVFilterContext *ctx)
  115. {
  116. LifeContext *life = ctx->priv;
  117. int i, j;
  118. char *line = av_malloc(life->w + 1);
  119. if (!line)
  120. return;
  121. for (i = 0; i < life->h; i++) {
  122. for (j = 0; j < life->w; j++)
  123. line[j] = life->buf[life->buf_idx][i*life->w + j] ? '@' : ' ';
  124. line[j] = 0;
  125. av_log(ctx, AV_LOG_DEBUG, "%3d: %s\n", i, line);
  126. }
  127. av_free(line);
  128. }
  129. #endif
  130. static int init_pattern_from_file(AVFilterContext *ctx)
  131. {
  132. LifeContext *life = ctx->priv;
  133. char *p;
  134. int ret, i, i0, j, h = 0, w, max_w = 0;
  135. if ((ret = av_file_map(life->filename, &life->file_buf, &life->file_bufsize,
  136. 0, ctx)) < 0)
  137. return ret;
  138. /* prescan file to get the number of lines and the maximum width */
  139. w = 0;
  140. for (i = 0; i < life->file_bufsize; i++) {
  141. if (life->file_buf[i] == '\n') {
  142. h++; max_w = FFMAX(w, max_w); w = 0;
  143. } else {
  144. w++;
  145. }
  146. }
  147. av_log(ctx, AV_LOG_DEBUG, "h:%d max_w:%d\n", h, max_w);
  148. if (life->size) {
  149. if (max_w > life->w || h > life->h) {
  150. av_log(ctx, AV_LOG_ERROR,
  151. "The specified size is %dx%d which cannot contain the provided file size of %dx%d\n",
  152. life->w, life->h, max_w, h);
  153. return AVERROR(EINVAL);
  154. }
  155. } else {
  156. /* size was not specified, set it to size of the grid */
  157. life->w = max_w;
  158. life->h = h;
  159. }
  160. if (!(life->buf[0] = av_mallocz(sizeof(char) * life->h * life->w)) ||
  161. !(life->buf[1] = av_mallocz(sizeof(char) * life->h * life->w))) {
  162. av_free(life->buf[0]);
  163. av_free(life->buf[1]);
  164. return AVERROR(ENOMEM);
  165. }
  166. /* fill buf[0] */
  167. p = life->file_buf;
  168. for (i0 = 0, i = (life->h - h)/2; i0 < h; i0++, i++) {
  169. for (j = (life->w - max_w)/2;; j++) {
  170. av_log(ctx, AV_LOG_DEBUG, "%d:%d %c\n", i, j, *p == '\n' ? 'N' : *p);
  171. if (*p == '\n') {
  172. p++; break;
  173. } else
  174. life->buf[0][i*life->w + j] = !!isgraph(*(p++));
  175. }
  176. }
  177. life->buf_idx = 0;
  178. return 0;
  179. }
  180. static int init(AVFilterContext *ctx, const char *args, void *opaque)
  181. {
  182. LifeContext *life = ctx->priv;
  183. AVRational frame_rate;
  184. int ret;
  185. life->class = &life_class;
  186. av_opt_set_defaults(life);
  187. if ((ret = av_set_options_string(life, args, "=", ":")) < 0) {
  188. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  189. return ret;
  190. }
  191. if ((ret = av_parse_video_rate(&frame_rate, life->rate)) < 0) {
  192. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", life->rate);
  193. return AVERROR(EINVAL);
  194. }
  195. if (!life->size && !life->filename)
  196. av_opt_set(life, "size", "320x240", 0);
  197. if (life->size &&
  198. (ret = av_parse_video_size(&life->w, &life->h, life->size)) < 0) {
  199. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", life->size);
  200. return ret;
  201. }
  202. if ((ret = parse_rule(&life->born_rule, &life->stay_rule, life->rule_str, ctx)) < 0)
  203. return ret;
  204. life->time_base.num = frame_rate.den;
  205. life->time_base.den = frame_rate.num;
  206. if (!life->filename) {
  207. /* fill the grid randomly */
  208. int i;
  209. if (!(life->buf[0] = av_mallocz(sizeof(char) * life->h * life->w)) ||
  210. !(life->buf[1] = av_mallocz(sizeof(char) * life->h * life->w))) {
  211. av_free(life->buf[0]);
  212. av_free(life->buf[1]);
  213. return AVERROR(ENOMEM);
  214. }
  215. if (life->random_seed == -1)
  216. life->random_seed = av_get_random_seed();
  217. av_lfg_init(&life->lfg, life->random_seed);
  218. for (i = 0; i < life->w * life->h; i++) {
  219. double r = (double)av_lfg_get(&life->lfg) / UINT32_MAX;
  220. if (r <= life->random_fill_ratio)
  221. life->buf[0][i] = 1;
  222. }
  223. life->buf_idx = 0;
  224. } else {
  225. if ((ret = init_pattern_from_file(ctx)) < 0)
  226. return ret;
  227. }
  228. av_log(ctx, AV_LOG_INFO,
  229. "s:%dx%d r:%d/%d rule:%s stay_rule:%d born_rule:%d stitch:%d\n",
  230. life->w, life->h, frame_rate.num, frame_rate.den,
  231. life->rule_str, life->stay_rule, life->born_rule, life->stitch);
  232. return 0;
  233. }
  234. static av_cold void uninit(AVFilterContext *ctx)
  235. {
  236. LifeContext *life = ctx->priv;
  237. av_file_unmap(life->file_buf, life->file_bufsize);
  238. av_freep(&life->rule_str);
  239. av_freep(&life->buf[0]);
  240. av_freep(&life->buf[1]);
  241. }
  242. static int config_props(AVFilterLink *outlink)
  243. {
  244. LifeContext *life = outlink->src->priv;
  245. outlink->w = life->w;
  246. outlink->h = life->h;
  247. outlink->time_base = life->time_base;
  248. return 0;
  249. }
  250. static void evolve(AVFilterContext *ctx)
  251. {
  252. LifeContext *life = ctx->priv;
  253. int i, j, v;
  254. uint8_t *oldbuf = life->buf[ life->buf_idx];
  255. uint8_t *newbuf = life->buf[!life->buf_idx];
  256. enum { NW, N, NE, W, E, SW, S, SE };
  257. /* evolve the grid */
  258. for (i = 0; i < life->h; i++) {
  259. for (j = 0; j < life->w; j++) {
  260. int pos[8][2], n;
  261. if (life->stitch) {
  262. pos[NW][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NW][1] = (j-1) < 0 ? life->w-1 : j-1;
  263. pos[N ][0] = (i-1) < 0 ? life->h-1 : i-1; pos[N ][1] = j ;
  264. pos[NE][0] = (i-1) < 0 ? life->h-1 : i-1; pos[NE][1] = (j+1) == life->w ? 0 : j+1;
  265. pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? life->w-1 : j-1;
  266. pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? 0 : j+1;
  267. pos[SW][0] = (i+1) == life->h ? 0 : i+1; pos[SW][1] = (j-1) < 0 ? life->w-1 : j-1;
  268. pos[S ][0] = (i+1) == life->h ? 0 : i+1; pos[S ][1] = j ;
  269. pos[SE][0] = (i+1) == life->h ? 0 : i+1; pos[SE][1] = (j+1) == life->w ? 0 : j+1;
  270. } else {
  271. pos[NW][0] = (i-1) < 0 ? -1 : i-1; pos[NW][1] = (j-1) < 0 ? -1 : j-1;
  272. pos[N ][0] = (i-1) < 0 ? -1 : i-1; pos[N ][1] = j ;
  273. pos[NE][0] = (i-1) < 0 ? -1 : i-1; pos[NE][1] = (j+1) == life->w ? -1 : j+1;
  274. pos[W ][0] = i ; pos[W ][1] = (j-1) < 0 ? -1 : j-1;
  275. pos[E ][0] = i ; pos[E ][1] = (j+1) == life->w ? -1 : j+1;
  276. pos[SW][0] = (i+1) == life->h ? -1 : i+1; pos[SW][1] = (j-1) < 0 ? -1 : j-1;
  277. pos[S ][0] = (i+1) == life->h ? -1 : i+1; pos[S ][1] = j ;
  278. pos[SE][0] = (i+1) == life->h ? -1 : i+1; pos[SE][1] = (j+1) == life->w ? -1 : j+1;
  279. }
  280. /* compute the number of live neighbor cells */
  281. n = (pos[NW][0] == -1 || pos[NW][1] == -1 ? 0 : oldbuf[pos[NW][0]*life->w + pos[NW][1]]) +
  282. (pos[N ][0] == -1 || pos[N ][1] == -1 ? 0 : oldbuf[pos[N ][0]*life->w + pos[N ][1]]) +
  283. (pos[NE][0] == -1 || pos[NE][1] == -1 ? 0 : oldbuf[pos[NE][0]*life->w + pos[NE][1]]) +
  284. (pos[W ][0] == -1 || pos[W ][1] == -1 ? 0 : oldbuf[pos[W ][0]*life->w + pos[W ][1]]) +
  285. (pos[E ][0] == -1 || pos[E ][1] == -1 ? 0 : oldbuf[pos[E ][0]*life->w + pos[E ][1]]) +
  286. (pos[SW][0] == -1 || pos[SW][1] == -1 ? 0 : oldbuf[pos[SW][0]*life->w + pos[SW][1]]) +
  287. (pos[S ][0] == -1 || pos[S ][1] == -1 ? 0 : oldbuf[pos[S ][0]*life->w + pos[S ][1]]) +
  288. (pos[SE][0] == -1 || pos[SE][1] == -1 ? 0 : oldbuf[pos[SE][0]*life->w + pos[SE][1]]);
  289. v = !!(1<<n & (oldbuf[i*life->w + j] ? life->stay_rule : life->born_rule));
  290. av_dlog(ctx, "i:%d j:%d live_neighbors:%d cell:%d -> cell:%d\n", i, j, n, oldbuf[i*life->w + j], v);
  291. newbuf[i*life->w+j] = v;
  292. }
  293. }
  294. life->buf_idx = !life->buf_idx;
  295. }
  296. static void fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
  297. {
  298. LifeContext *life = ctx->priv;
  299. uint8_t *buf = life->buf[life->buf_idx];
  300. int i, j, k;
  301. /* fill the output picture with the old grid buffer */
  302. for (i = 0; i < life->h; i++) {
  303. uint8_t byte = 0;
  304. uint8_t *p = picref->data[0] + i * picref->linesize[0];
  305. for (k = 0, j = 0; j < life->w; j++) {
  306. byte |= buf[i*life->w+j]<<(7-k++);
  307. if (k==8 || j == life->w-1) {
  308. k = 0;
  309. *p++ = byte;
  310. byte = 0;
  311. }
  312. }
  313. }
  314. }
  315. static int request_frame(AVFilterLink *outlink)
  316. {
  317. LifeContext *life = outlink->src->priv;
  318. AVFilterBufferRef *picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE, life->w, life->h);
  319. picref->video->sample_aspect_ratio = (AVRational) {1, 1};
  320. picref->pts = life->pts++;
  321. picref->pos = -1;
  322. fill_picture(outlink->src, picref);
  323. evolve(outlink->src);
  324. #ifdef DEBUG
  325. show_life_grid(outlink->src);
  326. #endif
  327. avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
  328. avfilter_draw_slice(outlink, 0, life->h, 1);
  329. avfilter_end_frame(outlink);
  330. avfilter_unref_buffer(picref);
  331. return 0;
  332. }
  333. static int query_formats(AVFilterContext *ctx)
  334. {
  335. static const enum PixelFormat pix_fmts[] = { PIX_FMT_MONOBLACK, PIX_FMT_NONE };
  336. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  337. return 0;
  338. }
  339. AVFilter avfilter_vsrc_life = {
  340. .name = "life",
  341. .description = NULL_IF_CONFIG_SMALL("Create life."),
  342. .priv_size = sizeof(LifeContext),
  343. .init = init,
  344. .uninit = uninit,
  345. .query_formats = query_formats,
  346. .inputs = (const AVFilterPad[]) {
  347. { .name = NULL}
  348. },
  349. .outputs = (const AVFilterPad[]) {
  350. { .name = "default",
  351. .type = AVMEDIA_TYPE_VIDEO,
  352. .request_frame = request_frame,
  353. .config_props = config_props },
  354. { .name = NULL}
  355. },
  356. };