vsrc_mandelbrot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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 fractal 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. WHITE,
  41. OUTZ,
  42. };
  43. enum Inner{
  44. BLACK,
  45. PERIOD,
  46. CONVTIME,
  47. MINCOL,
  48. };
  49. typedef struct Point {
  50. double p[2];
  51. uint32_t val;
  52. } Point;
  53. typedef struct MBContext {
  54. const AVClass *class;
  55. int w, h;
  56. AVRational frame_rate;
  57. uint64_t pts;
  58. int maxiter;
  59. double start_x;
  60. double start_y;
  61. double start_scale;
  62. double end_scale;
  63. double end_pts;
  64. double bailout;
  65. int outer;
  66. int inner;
  67. int cache_allocated;
  68. int cache_used;
  69. Point *point_cache;
  70. Point *next_cache;
  71. double (*zyklus)[2];
  72. uint32_t dither;
  73. double morphxf;
  74. double morphyf;
  75. double morphamp;
  76. } MBContext;
  77. #define OFFSET(x) offsetof(MBContext, x)
  78. #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
  79. static const AVOption mandelbrot_options[] = {
  80. {"size", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX, FLAGS },
  81. {"s", "set frame size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str="640x480"}, CHAR_MIN, CHAR_MAX, FLAGS },
  82. {"rate", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, CHAR_MIN, CHAR_MAX, FLAGS },
  83. {"r", "set frame rate", OFFSET(frame_rate), AV_OPT_TYPE_VIDEO_RATE, {.str="25"}, CHAR_MIN, CHAR_MAX, FLAGS },
  84. {"maxiter", "set max iterations number", OFFSET(maxiter), AV_OPT_TYPE_INT, {.i64=7189}, 1, INT_MAX, FLAGS },
  85. {"start_x", "set the initial x position", OFFSET(start_x), AV_OPT_TYPE_DOUBLE, {.dbl=-0.743643887037158704752191506114774}, -100, 100, FLAGS },
  86. {"start_y", "set the initial y position", OFFSET(start_y), AV_OPT_TYPE_DOUBLE, {.dbl=-0.131825904205311970493132056385139}, -100, 100, FLAGS },
  87. {"start_scale", "set the initial scale value", OFFSET(start_scale), AV_OPT_TYPE_DOUBLE, {.dbl=3.0}, 0, FLT_MAX, FLAGS },
  88. {"end_scale", "set the terminal scale value", OFFSET(end_scale), AV_OPT_TYPE_DOUBLE, {.dbl=0.3}, 0, FLT_MAX, FLAGS },
  89. {"end_pts", "set the terminal pts value", OFFSET(end_pts), AV_OPT_TYPE_DOUBLE, {.dbl=400}, 0, INT64_MAX, FLAGS },
  90. {"bailout", "set the bailout value", OFFSET(bailout), AV_OPT_TYPE_DOUBLE, {.dbl=10}, 0, FLT_MAX, FLAGS },
  91. {"morphxf", "set morph x frequency", OFFSET(morphxf), AV_OPT_TYPE_DOUBLE, {.dbl=0.01}, -FLT_MAX, FLT_MAX, FLAGS },
  92. {"morphyf", "set morph y frequency", OFFSET(morphyf), AV_OPT_TYPE_DOUBLE, {.dbl=0.0123}, -FLT_MAX, FLT_MAX, FLAGS },
  93. {"morphamp", "set morph amplitude", OFFSET(morphamp), AV_OPT_TYPE_DOUBLE, {.dbl=0}, -FLT_MAX, FLT_MAX, FLAGS },
  94. {"outer", "set outer coloring mode", OFFSET(outer), AV_OPT_TYPE_INT, {.i64=NORMALIZED_ITERATION_COUNT}, 0, INT_MAX, FLAGS, "outer" },
  95. {"iteration_count", "set iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
  96. {"normalized_iteration_count", "set normalized iteration count mode", 0, AV_OPT_TYPE_CONST, {.i64=NORMALIZED_ITERATION_COUNT}, INT_MIN, INT_MAX, FLAGS, "outer" },
  97. {"white", "set white mode", 0, AV_OPT_TYPE_CONST, {.i64=WHITE}, INT_MIN, INT_MAX, FLAGS, "outer" },
  98. {"outz", "set outz mode", 0, AV_OPT_TYPE_CONST, {.i64=OUTZ}, INT_MIN, INT_MAX, FLAGS, "outer" },
  99. {"inner", "set inner coloring mode", OFFSET(inner), AV_OPT_TYPE_INT, {.i64=MINCOL}, 0, INT_MAX, FLAGS, "inner" },
  100. {"black", "set black mode", 0, AV_OPT_TYPE_CONST, {.i64=BLACK}, INT_MIN, INT_MAX, FLAGS, "inner"},
  101. {"period", "set period mode", 0, AV_OPT_TYPE_CONST, {.i64=PERIOD}, INT_MIN, INT_MAX, FLAGS, "inner"},
  102. {"convergence", "show time until convergence", 0, AV_OPT_TYPE_CONST, {.i64=CONVTIME}, INT_MIN, INT_MAX, FLAGS, "inner"},
  103. {"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"},
  104. {NULL},
  105. };
  106. AVFILTER_DEFINE_CLASS(mandelbrot);
  107. static av_cold int init(AVFilterContext *ctx)
  108. {
  109. MBContext *s = ctx->priv;
  110. s->bailout *= s->bailout;
  111. s->start_scale /=s->h;
  112. s->end_scale /=s->h;
  113. s->cache_allocated = s->w * s->h * 3;
  114. s->cache_used = 0;
  115. s->point_cache= av_malloc_array(s->cache_allocated, sizeof(*s->point_cache));
  116. s-> next_cache= av_malloc_array(s->cache_allocated, sizeof(*s-> next_cache));
  117. s-> zyklus = av_malloc_array(s->maxiter + 16, sizeof(*s->zyklus));
  118. return 0;
  119. }
  120. static av_cold void uninit(AVFilterContext *ctx)
  121. {
  122. MBContext *s = ctx->priv;
  123. av_freep(&s->point_cache);
  124. av_freep(&s-> next_cache);
  125. av_freep(&s->zyklus);
  126. }
  127. static int query_formats(AVFilterContext *ctx)
  128. {
  129. static const enum AVPixelFormat pix_fmts[] = {
  130. AV_PIX_FMT_0BGR32,
  131. AV_PIX_FMT_NONE
  132. };
  133. AVFilterFormats *fmts_list = ff_make_format_list(pix_fmts);
  134. if (!fmts_list)
  135. return AVERROR(ENOMEM);
  136. return ff_set_common_formats(ctx, fmts_list);
  137. }
  138. static int config_props(AVFilterLink *inlink)
  139. {
  140. AVFilterContext *ctx = inlink->src;
  141. MBContext *s = ctx->priv;
  142. if (av_image_check_size(s->w, s->h, 0, ctx) < 0)
  143. return AVERROR(EINVAL);
  144. inlink->w = s->w;
  145. inlink->h = s->h;
  146. inlink->time_base = av_inv_q(s->frame_rate);
  147. return 0;
  148. }
  149. static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
  150. MBContext *s = ctx->priv;
  151. if(s->morphamp)
  152. return;
  153. for(; *in_cidx < s->cache_used; (*in_cidx)++){
  154. Point *p= &s->point_cache[*in_cidx];
  155. int x;
  156. if(p->p[1] > py)
  157. break;
  158. x= lrint((p->p[0] - s->start_x) / scale + s->w/2);
  159. if(x<0 || x >= s->w)
  160. continue;
  161. if(color) color[x] = p->val;
  162. if(out_cidx && *out_cidx < s->cache_allocated)
  163. s->next_cache[(*out_cidx)++]= *p;
  164. }
  165. }
  166. static int interpol(MBContext *s, 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==s->w || y+1==s->h)
  172. return 0;
  173. dist= FFMAX(FFABS(x-(s->w>>1))*s->h, FFABS(y-(s->h>>1))*s->w);
  174. if(dist<(s->w*s->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 *s = ctx->priv;
  216. int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
  217. double scale= s->start_scale*pow(s->end_scale/s->start_scale, pts/s->end_pts);
  218. int use_zyklus=0;
  219. fill_from_cache(ctx, NULL, &in_cidx, NULL, s->start_y+scale*(-s->h/2-0.5), scale);
  220. tmp_cidx= in_cidx;
  221. memset(color, 0, sizeof(*color)*s->w);
  222. for(y=0; y<s->h; y++){
  223. int y1= y+1;
  224. const double ci=s->start_y+scale*(y-s->h/2);
  225. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
  226. if(y1<s->h){
  227. memset(color+linesize*y1, 0, sizeof(*color)*s->w);
  228. fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
  229. }
  230. for(x=0; x<s->w; x++){
  231. float av_uninit(epsilon);
  232. const double cr=s->start_x+scale*(x-s->w/2);
  233. double zr=cr;
  234. double zi=ci;
  235. uint32_t c=0;
  236. double dv= s->dither / (double)(1LL<<32);
  237. s->dither= s->dither*1664525+1013904223;
  238. if(color[x + y*linesize] & 0xFF000000)
  239. continue;
  240. if(!s->morphamp){
  241. if(interpol(s, color, x, y, linesize)){
  242. if(next_cidx < s->cache_allocated){
  243. s->next_cache[next_cidx ].p[0]= cr;
  244. s->next_cache[next_cidx ].p[1]= ci;
  245. s->next_cache[next_cidx++].val = color[x + y*linesize];
  246. }
  247. continue;
  248. }
  249. }else{
  250. zr += cos(pts * s->morphxf) * s->morphamp;
  251. zi += sin(pts * s->morphyf) * s->morphamp;
  252. }
  253. use_zyklus= (x==0 || s->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
  254. if(use_zyklus)
  255. epsilon= scale*(abs(x-s->w/2) + abs(y-s->h/2))/s->w;
  256. #define Z_Z2_C(outr,outi,inr,ini)\
  257. outr= inr*inr - ini*ini + cr;\
  258. outi= 2*inr*ini + ci;
  259. #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
  260. Z_Z2_C(outr,outi,inr,ini)\
  261. if(use_zyklus){\
  262. if(Z && fabs(s->zyklus[i>>1][0]-outr)+fabs(s->zyklus[i>>1][1]-outi) <= epsilon)\
  263. break;\
  264. }\
  265. s->zyklus[i][0]= outr;\
  266. s->zyklus[i][1]= outi;\
  267. for(i=0; i<s->maxiter-8; i++){
  268. double t;
  269. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  270. i++;
  271. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  272. i++;
  273. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  274. i++;
  275. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  276. i++;
  277. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  278. i++;
  279. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  280. i++;
  281. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  282. i++;
  283. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  284. if(zr*zr + zi*zi > s->bailout){
  285. i-= FFMIN(7, i);
  286. for(; i<s->maxiter; i++){
  287. zr= s->zyklus[i][0];
  288. zi= s->zyklus[i][1];
  289. if(zr*zr + zi*zi > s->bailout){
  290. switch(s->outer){
  291. case ITERATION_COUNT:
  292. zr = i;
  293. c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
  294. break;
  295. case NORMALIZED_ITERATION_COUNT:
  296. zr = i + log2(log(s->bailout) / log(zr*zr + zi*zi));
  297. c = lrintf((sinf(zr)+1)*127) + lrintf((sinf(zr/1.234)+1)*127)*256*256 + lrintf((sinf(zr/100)+1)*127)*256;
  298. break;
  299. case WHITE:
  300. c = 0xFFFFFF;
  301. break;
  302. case OUTZ:
  303. zr /= s->bailout;
  304. zi /= s->bailout;
  305. c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
  306. }
  307. break;
  308. }
  309. }
  310. break;
  311. }
  312. }
  313. if(!c){
  314. if(s->inner==PERIOD){
  315. int j;
  316. for(j=i-1; j; j--)
  317. if(SQR(s->zyklus[j][0]-zr) + SQR(s->zyklus[j][1]-zi) < epsilon*epsilon*10)
  318. break;
  319. if(j){
  320. c= i-j;
  321. c= ((c<<5)&0xE0) + ((c<<10)&0xE000) + ((c<<15)&0xE00000);
  322. }
  323. }else if(s->inner==CONVTIME){
  324. c= floor(i*255.0/s->maxiter+dv)*0x010101;
  325. } else if(s->inner==MINCOL){
  326. int j;
  327. double closest=9999;
  328. int closest_index=0;
  329. for(j=i-1; j>=0; j--)
  330. if(SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]) < closest){
  331. closest= SQR(s->zyklus[j][0]) + SQR(s->zyklus[j][1]);
  332. closest_index= j;
  333. }
  334. closest = sqrt(closest);
  335. c= lrintf((s->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((s->zyklus[closest_index][1]/closest+1)*127+dv)*256;
  336. }
  337. }
  338. c |= 0xFF000000;
  339. color[x + y*linesize]= c;
  340. if(next_cidx < s->cache_allocated){
  341. s->next_cache[next_cidx ].p[0]= cr;
  342. s->next_cache[next_cidx ].p[1]= ci;
  343. s->next_cache[next_cidx++].val = c;
  344. }
  345. }
  346. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
  347. }
  348. FFSWAP(void*, s->next_cache, s->point_cache);
  349. s->cache_used = next_cidx;
  350. if(s->cache_used == s->cache_allocated)
  351. av_log(ctx, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
  352. }
  353. static int request_frame(AVFilterLink *link)
  354. {
  355. MBContext *s = link->src->priv;
  356. AVFrame *picref = ff_get_video_buffer(link, s->w, s->h);
  357. if (!picref)
  358. return AVERROR(ENOMEM);
  359. picref->sample_aspect_ratio = (AVRational) {1, 1};
  360. picref->pts = s->pts++;
  361. draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
  362. return ff_filter_frame(link, picref);
  363. }
  364. static const AVFilterPad mandelbrot_outputs[] = {
  365. {
  366. .name = "default",
  367. .type = AVMEDIA_TYPE_VIDEO,
  368. .request_frame = request_frame,
  369. .config_props = config_props,
  370. },
  371. { NULL }
  372. };
  373. AVFilter ff_vsrc_mandelbrot = {
  374. .name = "mandelbrot",
  375. .description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
  376. .priv_size = sizeof(MBContext),
  377. .priv_class = &mandelbrot_class,
  378. .init = init,
  379. .uninit = uninit,
  380. .query_formats = query_formats,
  381. .inputs = NULL,
  382. .outputs = mandelbrot_outputs,
  383. };