vsrc_mandelbrot.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "formats.h"
  29. #include "video.h"
  30. #include "internal.h"
  31. #include "libavutil/imgutils.h"
  32. #include "libavutil/opt.h"
  33. #include "libavutil/parseutils.h"
  34. #include <float.h>
  35. #include <math.h>
  36. #define SQR(a) ((a)*(a))
  37. enum Outer{
  38. ITERATION_COUNT,
  39. NORMALIZED_ITERATION_COUNT,
  40. };
  41. enum Inner{
  42. BLACK,
  43. PERIOD,
  44. CONVTIME,
  45. MINCOL,
  46. };
  47. typedef struct Point {
  48. double p[2];
  49. uint32_t val;
  50. } Point;
  51. typedef struct {
  52. const AVClass *class;
  53. int w, h;
  54. AVRational time_base;
  55. uint64_t pts;
  56. char *rate;
  57. int maxiter;
  58. double start_x;
  59. double start_y;
  60. double start_scale;
  61. double end_scale;
  62. double end_pts;
  63. double bailout;
  64. enum Outer outer;
  65. enum Inner inner;
  66. int cache_allocated;
  67. int cache_used;
  68. Point *point_cache;
  69. Point *next_cache;
  70. double (*zyklus)[2];
  71. uint32_t dither;
  72. } MBContext;
  73. #define OFFSET(x) offsetof(MBContext, x)
  74. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  75. static const AVOption mandelbrot_options[] = {
  76. {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX, FLAGS },
  77. {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX, FLAGS },
  78. {"rate", "set frame rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str="25"}, CHAR_MIN, CHAR_MAX, FLAGS },
  79. {"r", "set frame rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str="25"}, CHAR_MIN, CHAR_MAX, FLAGS },
  80. {"maxiter", "set max iterations number", OFFSET(maxiter), AV_OPT_TYPE_INT, {.i64=7189}, 1, INT_MAX, FLAGS },
  81. {"start_x", "set the initial x position", OFFSET(start_x), AV_OPT_TYPE_DOUBLE, {.dbl=-0.743643887037158704752191506114774}, -100, 100, FLAGS },
  82. {"start_y", "set the initial y position", OFFSET(start_y), AV_OPT_TYPE_DOUBLE, {.dbl=-0.131825904205311970493132056385139}, -100, 100, FLAGS },
  83. {"start_scale", "set the initial scale value", OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, FLT_MAX, FLAGS },
  84. {"end_scale", "set the terminal scale value", OFFSET(end_scale), AV_OPT_TYPE_DOUBLE, {.dbl=0.3}, 0, FLT_MAX, FLAGS },
  85. {"end_pts", "set the terminal pts value", OFFSET(end_pts), AV_OPT_TYPE_DOUBLE, {.dbl=400}, 0, INT64_MAX, FLAGS },
  86. {"bailout", "set the bailout value", OFFSET(bailout), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 0, FLT_MAX, FLAGS },
  87. {"outer", "set outer coloring mode", OFFSET(outer), AV_OPT_TYPE_INT, {.i64=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, FLAGS, "outer" },
  88. {"iteration_count", "set iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
  89. {"normalized_iteration_count", "set normalized iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
  90. {"inner", "set inner coloring mode", OFFSET(inner), AV_OPT_TYPE_INT, {.i64=MINCOL}, 0, INT_MAX, FLAGS, "inner" },
  91. {"black", "set black mode", 0, AV_OPT_TYPE_CONST, {.i64=BLACK}, INT_MIN, INT_MAX, FLAGS, "inner"},
  92. {"period", "set period mode", 0, AV_OPT_TYPE_CONST, {.i64=PERIOD}, INT_MIN, INT_MAX, FLAGS, "inner"},
  93. {"convergence", "show time until convergence", 0, AV_OPT_TYPE_CONST, {.i64=CONVTIME}, INT_MIN, INT_MAX, FLAGS, "inner"},
  94. {"mincol", "color based on point closest to the origin of the iterations", 0, AV_OPT_TYPE_CONST, {.i64=MINCOL}, INT_MIN, INT_MAX, FLAGS, "inner"},
  95. {NULL},
  96. };
  97. AVFILTER_DEFINE_CLASS(mandelbrot);
  98. static av_cold int init(AVFilterContext *ctx, const char *args)
  99. {
  100. MBContext *mb = ctx->priv;
  101. AVRational rate_q;
  102. int err;
  103. mb->class = &mandelbrot_class;
  104. av_opt_set_defaults(mb);
  105. if ((err = (av_set_options_string(mb, args, "=", ":"))) < 0)
  106. return err;
  107. mb->bailout *= mb->bailout;
  108. mb->start_scale /=mb->h;
  109. mb->end_scale /=mb->h;
  110. if (av_parse_video_rate(&rate_q, mb->rate) < 0) {
  111. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", mb->rate);
  112. return AVERROR(EINVAL);
  113. }
  114. mb->time_base.num = rate_q.den;
  115. mb->time_base.den = rate_q.num;
  116. mb->cache_allocated = mb->w * mb->h * 3;
  117. mb->cache_used = 0;
  118. mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
  119. mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
  120. mb-> zyklus = av_malloc(sizeof(*mb->zyklus) * (mb->maxiter+16));
  121. return 0;
  122. }
  123. static av_cold void uninit(AVFilterContext *ctx)
  124. {
  125. MBContext *mb = ctx->priv;
  126. av_freep(&mb->rate);
  127. av_freep(&mb->point_cache);
  128. av_freep(&mb-> next_cache);
  129. av_freep(&mb->zyklus);
  130. }
  131. static int query_formats(AVFilterContext *ctx)
  132. {
  133. static const enum PixelFormat pix_fmts[] = {
  134. PIX_FMT_BGR32,
  135. PIX_FMT_NONE
  136. };
  137. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  138. return 0;
  139. }
  140. static int config_props(AVFilterLink *inlink)
  141. {
  142. AVFilterContext *ctx = inlink->src;
  143. MBContext *mb = ctx->priv;
  144. if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
  145. return AVERROR(EINVAL);
  146. inlink->w = mb->w;
  147. inlink->h = mb->h;
  148. inlink->time_base = mb->time_base;
  149. return 0;
  150. }
  151. static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
  152. MBContext *mb = ctx->priv;
  153. for(; *in_cidx < mb->cache_used; (*in_cidx)++){
  154. Point *p= &mb->point_cache[*in_cidx];
  155. int x;
  156. if(p->p[1] > py)
  157. break;
  158. x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
  159. if(x<0 || x >= mb->w)
  160. continue;
  161. if(color) color[x] = p->val;
  162. if(out_cidx && *out_cidx < mb->cache_allocated)
  163. mb->next_cache[(*out_cidx)++]= *p;
  164. }
  165. }
  166. static int interpol(MBContext *mb, uint32_t *color, int x, int y, int linesize)
  167. {
  168. uint32_t a,b,c,d, i;
  169. uint32_t ipol=0xFF000000;
  170. int dist;
  171. if(!x || !y || x+1==mb->w || y+1==mb->h)
  172. return 0;
  173. dist= FFMAX(FFABS(x-(mb->w>>1))*mb->h, FFABS(y-(mb->h>>1))*mb->w);
  174. if(dist<(mb->w*mb->h>>3))
  175. return 0;
  176. a=color[(x+1) + (y+0)*linesize];
  177. b=color[(x-1) + (y+1)*linesize];
  178. c=color[(x+0) + (y+1)*linesize];
  179. d=color[(x+1) + (y+1)*linesize];
  180. if(a&&c){
  181. b= color[(x-1) + (y+0)*linesize];
  182. d= color[(x+0) + (y-1)*linesize];
  183. }else if(b&&d){
  184. a= color[(x+1) + (y-1)*linesize];
  185. c= color[(x-1) + (y-1)*linesize];
  186. }else if(c){
  187. d= color[(x+0) + (y-1)*linesize];
  188. a= color[(x-1) + (y+0)*linesize];
  189. b= color[(x+1) + (y-1)*linesize];
  190. }else if(d){
  191. c= color[(x-1) + (y-1)*linesize];
  192. a= color[(x-1) + (y+0)*linesize];
  193. b= color[(x+1) + (y-1)*linesize];
  194. }else
  195. return 0;
  196. for(i=0; i<3; i++){
  197. int s= 8*i;
  198. uint8_t ac= a>>s;
  199. uint8_t bc= b>>s;
  200. uint8_t cc= c>>s;
  201. uint8_t dc= d>>s;
  202. int ipolab= (ac + bc);
  203. int ipolcd= (cc + dc);
  204. if(FFABS(ipolab - ipolcd) > 5)
  205. return 0;
  206. if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
  207. return 0;
  208. ipol |= ((ipolab + ipolcd + 2)/4)<<s;
  209. }
  210. color[x + y*linesize]= ipol;
  211. return 1;
  212. }
  213. static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
  214. {
  215. MBContext *mb = ctx->priv;
  216. int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
  217. double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
  218. int use_zyklus=0;
  219. fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
  220. tmp_cidx= in_cidx;
  221. memset(color, 0, sizeof(*color)*mb->w);
  222. for(y=0; y<mb->h; y++){
  223. int y1= y+1;
  224. const double ci=mb->start_y+scale*(y-mb->h/2);
  225. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
  226. if(y1<mb->h){
  227. memset(color+linesize*y1, 0, sizeof(*color)*mb->w);
  228. fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
  229. }
  230. for(x=0; x<mb->w; x++){
  231. float av_uninit(epsilon);
  232. const double cr=mb->start_x+scale*(x-mb->w/2);
  233. double zr=cr;
  234. double zi=ci;
  235. uint32_t c=0;
  236. double dv= mb->dither / (double)(1LL<<32);
  237. mb->dither= mb->dither*1664525+1013904223;
  238. if(color[x + y*linesize] & 0xFF000000)
  239. continue;
  240. if(interpol(mb, color, x, y, linesize)){
  241. if(next_cidx < mb->cache_allocated){
  242. mb->next_cache[next_cidx ].p[0]= cr;
  243. mb->next_cache[next_cidx ].p[1]= ci;
  244. mb->next_cache[next_cidx++].val = color[x + y*linesize];
  245. }
  246. continue;
  247. }
  248. use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
  249. if(use_zyklus)
  250. epsilon= scale*1*sqrt(SQR(x-mb->w/2) + SQR(y-mb->h/2))/mb->w;
  251. #define Z_Z2_C(outr,outi,inr,ini)\
  252. outr= inr*inr - ini*ini + cr;\
  253. outi= 2*inr*ini + ci;
  254. #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
  255. Z_Z2_C(outr,outi,inr,ini)\
  256. if(use_zyklus){\
  257. if(Z && fabs(mb->zyklus[i>>1][0]-outr)+fabs(mb->zyklus[i>>1][1]-outi) <= epsilon)\
  258. break;\
  259. }\
  260. mb->zyklus[i][0]= outr;\
  261. mb->zyklus[i][1]= outi;\
  262. for(i=0; i<mb->maxiter-8; i++){
  263. double t;
  264. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  265. i++;
  266. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  267. i++;
  268. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  269. i++;
  270. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  271. i++;
  272. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  273. i++;
  274. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  275. i++;
  276. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  277. i++;
  278. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  279. if(zr*zr + zi*zi > mb->bailout){
  280. i-= FFMIN(7, i);
  281. for(; i<mb->maxiter; i++){
  282. zr= mb->zyklus[i][0];
  283. zi= mb->zyklus[i][1];
  284. if(zr*zr + zi*zi > mb->bailout){
  285. switch(mb->outer){
  286. case ITERATION_COUNT: zr = i; break;
  287. case NORMALIZED_ITERATION_COUNT: zr= i + log2(log(mb->bailout) / log(zr*zr + zi*zi)); break;
  288. }
  289. c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
  290. break;
  291. }
  292. }
  293. break;
  294. }
  295. }
  296. if(!c){
  297. if(mb->inner==PERIOD){
  298. int j;
  299. for(j=i-1; j; j--)
  300. if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < epsilon*epsilon*10)
  301. break;
  302. if(j){
  303. c= i-j;
  304. c= ((c<<5)&0xE0) + ((c<<16)&0xE000) + ((c<<27)&0xE00000);
  305. }
  306. }else if(mb->inner==CONVTIME){
  307. c= floor(i*255.0/mb->maxiter+dv)*0x010101;
  308. } else if(mb->inner==MINCOL){
  309. int j;
  310. double closest=9999;
  311. int closest_index=0;
  312. for(j=i-1; j>=0; j--)
  313. if(SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]) < closest){
  314. closest= SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]);
  315. closest_index= j;
  316. }
  317. closest = sqrt(closest);
  318. c= lrintf((mb->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((mb->zyklus[closest_index][1]/closest+1)*127+dv)*256;
  319. }
  320. }
  321. c |= 0xFF000000;
  322. color[x + y*linesize]= c;
  323. if(next_cidx < mb->cache_allocated){
  324. mb->next_cache[next_cidx ].p[0]= cr;
  325. mb->next_cache[next_cidx ].p[1]= ci;
  326. mb->next_cache[next_cidx++].val = c;
  327. }
  328. }
  329. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
  330. }
  331. FFSWAP(void*, mb->next_cache, mb->point_cache);
  332. mb->cache_used = next_cidx;
  333. if(mb->cache_used == mb->cache_allocated)
  334. av_log(0, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
  335. }
  336. static int request_frame(AVFilterLink *link)
  337. {
  338. MBContext *mb = link->src->priv;
  339. AVFilterBufferRef *picref = ff_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
  340. picref->video->sample_aspect_ratio = (AVRational) {1, 1};
  341. picref->pts = mb->pts++;
  342. picref->pos = -1;
  343. ff_start_frame(link, avfilter_ref_buffer(picref, ~0));
  344. draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
  345. ff_draw_slice(link, 0, mb->h, 1);
  346. ff_end_frame(link);
  347. avfilter_unref_buffer(picref);
  348. return 0;
  349. }
  350. AVFilter avfilter_vsrc_mandelbrot = {
  351. .name = "mandelbrot",
  352. .description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
  353. .priv_size = sizeof(MBContext),
  354. .init = init,
  355. .uninit = uninit,
  356. .query_formats = query_formats,
  357. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  358. .outputs = (const AVFilterPad[]) {{ .name = "default",
  359. .type = AVMEDIA_TYPE_VIDEO,
  360. .request_frame = request_frame,
  361. .config_props = config_props },
  362. { .name = NULL}},
  363. };