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.

316 lines
11KB

  1. /*
  2. * Copyright (c) 2011 Michael Niedermayer
  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. * The vsrc_color filter from Stefano Sabatini was used as template to create
  21. * this
  22. */
  23. /**
  24. * @file
  25. * Mandelbrot fraktal renderer
  26. */
  27. #include "avfilter.h"
  28. #include "libavutil/imgutils.h"
  29. #include "libavutil/opt.h"
  30. #include "libavutil/parseutils.h"
  31. #include <float.h>
  32. #include <math.h>
  33. #define SQR(a) ((a)*(a))
  34. enum Outer{
  35. ITERATION_COUNT,
  36. NORMALIZED_ITERATION_COUNT,
  37. };
  38. enum Inner{
  39. BLACK,
  40. PERIOD,
  41. };
  42. typedef struct Point {
  43. double p[2];
  44. uint32_t val;
  45. } Point;
  46. typedef struct {
  47. const AVClass *class;
  48. int w, h;
  49. AVRational time_base;
  50. uint64_t pts;
  51. char *size, *rate;
  52. int maxiter;
  53. double start_x;
  54. double start_y;
  55. double start_scale;
  56. double end_scale;
  57. double end_pts;
  58. double bailout;
  59. enum Outer outer;
  60. enum Inner inner;
  61. int cache_allocated;
  62. int cache_used;
  63. Point *point_cache;
  64. Point *next_cache;
  65. double (*zyklus)[2];
  66. } MBContext;
  67. #define OFFSET(x) offsetof(MBContext, x)
  68. static const AVOption mandelbrot_options[] = {
  69. {"size", "set frame size", OFFSET(size), AV_OPT_TYPE_STRING, {.str="640x480"}, CHAR_MIN, CHAR_MAX },
  70. {"s", "set frame size", OFFSET(size), AV_OPT_TYPE_STRING, {.str="640x480"}, CHAR_MIN, CHAR_MAX },
  71. {"rate", "set frame rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str="25"}, CHAR_MIN, CHAR_MAX },
  72. {"r", "set frame rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str="25"}, CHAR_MIN, CHAR_MAX },
  73. {"maxiter", "set max iterations number", OFFSET(maxiter), AV_OPT_TYPE_INT, {.dbl=4096}, 1, INT_MAX },
  74. {"start_x", "set the initial x position", OFFSET(start_x), AV_OPT_TYPE_DOUBLE, {.dbl=-0.743643887037158704752191506114774}, -100, 100 },
  75. {"start_y", "set the initial y position", OFFSET(start_y), AV_OPT_TYPE_DOUBLE, {.dbl=-0.131825904205311970493132056385139}, -100, 100 },
  76. {"start_scale", "set the initial scale value", OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, FLT_MAX },
  77. {"end_scale", "set the terminal scale value", OFFSET(end_scale), AV_OPT_TYPE_DOUBLE, {.dbl=0.3}, 0, FLT_MAX },
  78. {"end_pts", "set the terminal pts value", OFFSET(end_pts), AV_OPT_TYPE_DOUBLE, {.dbl=800}, 0, INT64_MAX },
  79. {"bailout", "set the bailout value", OFFSET(bailout), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 0, FLT_MAX },
  80. {"outer", "set outer coloring mode", OFFSET(outer), AV_OPT_TYPE_INT, {.dbl=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, 0, "outer"},
  81. {"iteration_count", "set iteration count mode", 0, AV_OPT_TYPE_CONST, {.dbl=ITERATION_COUNT}, INT_MIN, INT_MAX, 0, "outer" },
  82. {"normalized_iteration_count", "set normalized iteration count mode", 0, AV_OPT_TYPE_CONST, {.dbl=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, 0, "outer" },
  83. {"inner", "set inner coloring mode", OFFSET(inner), AV_OPT_TYPE_INT, {.dbl=BLACK}, 0, INT_MAX, 0, "inner"},
  84. {"black", "set black mode", 0, AV_OPT_TYPE_CONST, {.dbl=BLACK}, INT_MIN, INT_MAX, 0, "inner" },
  85. {"period", "set period mode", 0, AV_OPT_TYPE_CONST, {.dbl=PERIOD}, INT_MIN, INT_MAX, 0, "inner" },
  86. {NULL},
  87. };
  88. static const char *mandelbrot_get_name(void *ctx)
  89. {
  90. return "mandelbrot";
  91. }
  92. static const AVClass mandelbrot_class = {
  93. "MBContext",
  94. mandelbrot_get_name,
  95. mandelbrot_options
  96. };
  97. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  98. {
  99. MBContext *mb = ctx->priv;
  100. AVRational rate_q;
  101. int err;
  102. mb->class = &mandelbrot_class;
  103. av_opt_set_defaults(mb);
  104. if ((err = (av_set_options_string(mb, args, "=", ":"))) < 0) {
  105. av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
  106. return err;
  107. }
  108. mb->bailout *= mb->bailout;
  109. if (av_parse_video_size(&mb->w, &mb->h, mb->size) < 0) {
  110. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", mb->size);
  111. return AVERROR(EINVAL);
  112. }
  113. mb->start_scale /=mb->h;
  114. mb->end_scale /=mb->h;
  115. if (av_parse_video_rate(&rate_q, mb->rate) < 0 ||
  116. rate_q.den <= 0 || rate_q.num <= 0) {
  117. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", mb->rate);
  118. return AVERROR(EINVAL);
  119. }
  120. mb->time_base.num = rate_q.den;
  121. mb->time_base.den = rate_q.num;
  122. mb->cache_allocated = mb->w * mb->h * 3;
  123. mb->cache_used = 0;
  124. mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
  125. mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
  126. mb-> zyklus = av_malloc(sizeof(*mb->zyklus) * mb->maxiter);
  127. return 0;
  128. }
  129. static av_cold void uninit(AVFilterContext *ctx)
  130. {
  131. MBContext *mb = ctx->priv;
  132. av_freep(&mb->size);
  133. av_freep(&mb->rate);
  134. av_freep(&mb->point_cache);
  135. av_freep(&mb-> next_cache);
  136. av_freep(&mb->zyklus);
  137. }
  138. static int query_formats(AVFilterContext *ctx)
  139. {
  140. static const enum PixelFormat pix_fmts[] = {
  141. PIX_FMT_BGR32,
  142. PIX_FMT_NONE
  143. };
  144. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  145. return 0;
  146. }
  147. static int config_props(AVFilterLink *inlink)
  148. {
  149. AVFilterContext *ctx = inlink->src;
  150. MBContext *mb = ctx->priv;
  151. if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
  152. return AVERROR(EINVAL);
  153. inlink->w = mb->w;
  154. inlink->h = mb->h;
  155. inlink->time_base = mb->time_base;
  156. return 0;
  157. }
  158. static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
  159. MBContext *mb = ctx->priv;
  160. for(; *in_cidx < mb->cache_used; (*in_cidx)++){
  161. Point *p= &mb->point_cache[*in_cidx];
  162. int x;
  163. if(p->p[1] > py)
  164. break;
  165. x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
  166. if(x<0 || x >= mb->w)
  167. continue;
  168. if(color) color[x] = p->val;
  169. if(out_cidx && *out_cidx < mb->cache_allocated)
  170. mb->next_cache[(*out_cidx)++]= *p;
  171. }
  172. }
  173. static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
  174. {
  175. MBContext *mb = ctx->priv;
  176. int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
  177. double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
  178. int use_zyklus=0;
  179. fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
  180. for(y=0; y<mb->h; y++){
  181. const double ci=mb->start_y+scale*(y-mb->h/2);
  182. memset(color+linesize*y, 0, sizeof(*color)*mb->w);
  183. fill_from_cache(ctx, color+linesize*y, &in_cidx, &next_cidx, ci, scale);
  184. tmp_cidx= in_cidx;
  185. fill_from_cache(ctx, color+linesize*y, &tmp_cidx, NULL, ci + scale/2, scale);
  186. for(x=0; x<mb->w; x++){
  187. const double cr=mb->start_x+scale*(x-mb->w/2);
  188. double zr=cr;
  189. double zi=ci;
  190. uint32_t c=0;
  191. if(color[x + y*linesize] & 0xFF000000)
  192. continue;
  193. use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
  194. for(i=0; i<mb->maxiter; i++){
  195. double t;
  196. if(zr*zr + zi*zi > mb->bailout){
  197. switch(mb->outer){
  198. case ITERATION_COUNT: zr = i - (SQR(zr-cr)+SQR(zi-ci) > SQR(mb->bailout)); break;
  199. case NORMALIZED_ITERATION_COUNT: zr= i + log2(log(mb->bailout) / log(zr*zr + zi*zi)); break;
  200. }
  201. c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
  202. break;
  203. }
  204. t= zr*zr - zi*zi + cr;
  205. zi= 2*zr*zi + ci;
  206. if(use_zyklus){
  207. mb->zyklus[i][0]= t;
  208. mb->zyklus[i][1]= zi;
  209. }
  210. i++;
  211. zr= t*t - zi*zi+cr;
  212. zi= 2*t*zi + ci;
  213. if(use_zyklus){
  214. if(mb->zyklus[i>>1][0]==zr && mb->zyklus[i>>1][1]==zi)
  215. break;
  216. mb->zyklus[i][0]= zr;
  217. mb->zyklus[i][1]= zi;
  218. }
  219. }
  220. if(!c && mb->inner==PERIOD){
  221. int j;
  222. for(j=i-1; j; j--)
  223. if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < 0.0000000000000001)
  224. break;
  225. if(j){
  226. c= i-j;
  227. c= ((c<<5)&0xE0) + ((c<<16)&0xE000) + ((c<<27)&0xE00000);
  228. }
  229. }
  230. c |= 0xFF000000;
  231. color[x + y*linesize]= c;
  232. if(next_cidx < mb->cache_allocated){
  233. mb->next_cache[next_cidx ].p[0]= cr;
  234. mb->next_cache[next_cidx ].p[1]= ci;
  235. mb->next_cache[next_cidx++].val = c;
  236. }
  237. }
  238. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
  239. }
  240. FFSWAP(void*, mb->next_cache, mb->point_cache);
  241. mb->cache_used = next_cidx;
  242. if(mb->cache_used == mb->cache_allocated)
  243. av_log(0, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
  244. }
  245. static int request_frame(AVFilterLink *link)
  246. {
  247. MBContext *mb = link->src->priv;
  248. AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
  249. picref->video->sample_aspect_ratio = (AVRational) {1, 1};
  250. picref->pts = mb->pts++;
  251. picref->pos = -1;
  252. avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
  253. draw_mandelbrot(link->src, picref->data[0], picref->linesize[0]/4, picref->pts);
  254. avfilter_draw_slice(link, 0, mb->h, 1);
  255. avfilter_end_frame(link);
  256. avfilter_unref_buffer(picref);
  257. return 0;
  258. }
  259. AVFilter avfilter_vsrc_mandelbrot = {
  260. .name = "mandelbrot",
  261. .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
  262. .priv_size = sizeof(MBContext),
  263. .init = init,
  264. .uninit = uninit,
  265. .query_formats = query_formats,
  266. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  267. .outputs = (const AVFilterPad[]) {{ .name = "default",
  268. .type = AVMEDIA_TYPE_VIDEO,
  269. .request_frame = request_frame,
  270. .config_props = config_props },
  271. { .name = NULL}},
  272. };