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.

439 lines
16KB

  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. 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 {
  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. enum Outer outer;
  66. enum Inner 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, const char *args)
  108. {
  109. MBContext *mb = ctx->priv;
  110. int err;
  111. mb->class = &mandelbrot_class;
  112. av_opt_set_defaults(mb);
  113. if ((err = (av_set_options_string(mb, args, "=", ":"))) < 0)
  114. return err;
  115. mb->bailout *= mb->bailout;
  116. mb->start_scale /=mb->h;
  117. mb->end_scale /=mb->h;
  118. mb->cache_allocated = mb->w * mb->h * 3;
  119. mb->cache_used = 0;
  120. mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
  121. mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
  122. mb-> zyklus = av_malloc(sizeof(*mb->zyklus) * (mb->maxiter+16));
  123. return 0;
  124. }
  125. static av_cold void uninit(AVFilterContext *ctx)
  126. {
  127. MBContext *mb = ctx->priv;
  128. av_freep(&mb->point_cache);
  129. av_freep(&mb-> next_cache);
  130. av_freep(&mb->zyklus);
  131. }
  132. static int query_formats(AVFilterContext *ctx)
  133. {
  134. static const enum AVPixelFormat pix_fmts[] = {
  135. AV_PIX_FMT_BGR32,
  136. AV_PIX_FMT_NONE
  137. };
  138. ff_set_common_formats(ctx, ff_make_format_list(pix_fmts));
  139. return 0;
  140. }
  141. static int config_props(AVFilterLink *inlink)
  142. {
  143. AVFilterContext *ctx = inlink->src;
  144. MBContext *mb = ctx->priv;
  145. if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
  146. return AVERROR(EINVAL);
  147. inlink->w = mb->w;
  148. inlink->h = mb->h;
  149. inlink->time_base = av_inv_q(mb->frame_rate);
  150. return 0;
  151. }
  152. static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
  153. MBContext *mb = ctx->priv;
  154. if(mb->morphamp)
  155. return;
  156. for(; *in_cidx < mb->cache_used; (*in_cidx)++){
  157. Point *p= &mb->point_cache[*in_cidx];
  158. int x;
  159. if(p->p[1] > py)
  160. break;
  161. x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
  162. if(x<0 || x >= mb->w)
  163. continue;
  164. if(color) color[x] = p->val;
  165. if(out_cidx && *out_cidx < mb->cache_allocated)
  166. mb->next_cache[(*out_cidx)++]= *p;
  167. }
  168. }
  169. static int interpol(MBContext *mb, uint32_t *color, int x, int y, int linesize)
  170. {
  171. uint32_t a,b,c,d, i;
  172. uint32_t ipol=0xFF000000;
  173. int dist;
  174. if(!x || !y || x+1==mb->w || y+1==mb->h)
  175. return 0;
  176. dist= FFMAX(FFABS(x-(mb->w>>1))*mb->h, FFABS(y-(mb->h>>1))*mb->w);
  177. if(dist<(mb->w*mb->h>>3))
  178. return 0;
  179. a=color[(x+1) + (y+0)*linesize];
  180. b=color[(x-1) + (y+1)*linesize];
  181. c=color[(x+0) + (y+1)*linesize];
  182. d=color[(x+1) + (y+1)*linesize];
  183. if(a&&c){
  184. b= color[(x-1) + (y+0)*linesize];
  185. d= color[(x+0) + (y-1)*linesize];
  186. }else if(b&&d){
  187. a= color[(x+1) + (y-1)*linesize];
  188. c= color[(x-1) + (y-1)*linesize];
  189. }else if(c){
  190. d= color[(x+0) + (y-1)*linesize];
  191. a= color[(x-1) + (y+0)*linesize];
  192. b= color[(x+1) + (y-1)*linesize];
  193. }else if(d){
  194. c= color[(x-1) + (y-1)*linesize];
  195. a= color[(x-1) + (y+0)*linesize];
  196. b= color[(x+1) + (y-1)*linesize];
  197. }else
  198. return 0;
  199. for(i=0; i<3; i++){
  200. int s= 8*i;
  201. uint8_t ac= a>>s;
  202. uint8_t bc= b>>s;
  203. uint8_t cc= c>>s;
  204. uint8_t dc= d>>s;
  205. int ipolab= (ac + bc);
  206. int ipolcd= (cc + dc);
  207. if(FFABS(ipolab - ipolcd) > 5)
  208. return 0;
  209. if(FFABS(ac-bc)+FFABS(cc-dc) > 20)
  210. return 0;
  211. ipol |= ((ipolab + ipolcd + 2)/4)<<s;
  212. }
  213. color[x + y*linesize]= ipol;
  214. return 1;
  215. }
  216. static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
  217. {
  218. MBContext *mb = ctx->priv;
  219. int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
  220. double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
  221. int use_zyklus=0;
  222. fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
  223. tmp_cidx= in_cidx;
  224. memset(color, 0, sizeof(*color)*mb->w);
  225. for(y=0; y<mb->h; y++){
  226. int y1= y+1;
  227. const double ci=mb->start_y+scale*(y-mb->h/2);
  228. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci, scale);
  229. if(y1<mb->h){
  230. memset(color+linesize*y1, 0, sizeof(*color)*mb->w);
  231. fill_from_cache(ctx, color+linesize*y1, &tmp_cidx, NULL, ci + 3*scale/2, scale);
  232. }
  233. for(x=0; x<mb->w; x++){
  234. float av_uninit(epsilon);
  235. const double cr=mb->start_x+scale*(x-mb->w/2);
  236. double zr=cr;
  237. double zi=ci;
  238. uint32_t c=0;
  239. double dv= mb->dither / (double)(1LL<<32);
  240. mb->dither= mb->dither*1664525+1013904223;
  241. if(color[x + y*linesize] & 0xFF000000)
  242. continue;
  243. if(!mb->morphamp){
  244. if(interpol(mb, color, x, y, linesize)){
  245. if(next_cidx < mb->cache_allocated){
  246. mb->next_cache[next_cidx ].p[0]= cr;
  247. mb->next_cache[next_cidx ].p[1]= ci;
  248. mb->next_cache[next_cidx++].val = color[x + y*linesize];
  249. }
  250. continue;
  251. }
  252. }else{
  253. zr += cos(pts * mb->morphxf) * mb->morphamp;
  254. zi += sin(pts * mb->morphyf) * mb->morphamp;
  255. }
  256. use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
  257. if(use_zyklus)
  258. epsilon= scale*1*sqrt(SQR(x-mb->w/2) + SQR(y-mb->h/2))/mb->w;
  259. #define Z_Z2_C(outr,outi,inr,ini)\
  260. outr= inr*inr - ini*ini + cr;\
  261. outi= 2*inr*ini + ci;
  262. #define Z_Z2_C_ZYKLUS(outr,outi,inr,ini, Z)\
  263. Z_Z2_C(outr,outi,inr,ini)\
  264. if(use_zyklus){\
  265. if(Z && fabs(mb->zyklus[i>>1][0]-outr)+fabs(mb->zyklus[i>>1][1]-outi) <= epsilon)\
  266. break;\
  267. }\
  268. mb->zyklus[i][0]= outr;\
  269. mb->zyklus[i][1]= outi;\
  270. for(i=0; i<mb->maxiter-8; i++){
  271. double t;
  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. i++;
  280. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  281. i++;
  282. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  283. i++;
  284. Z_Z2_C_ZYKLUS(t, zi, zr, zi, 0)
  285. i++;
  286. Z_Z2_C_ZYKLUS(zr, zi, t, zi, 1)
  287. if(zr*zr + zi*zi > mb->bailout){
  288. i-= FFMIN(7, i);
  289. for(; i<mb->maxiter; i++){
  290. zr= mb->zyklus[i][0];
  291. zi= mb->zyklus[i][1];
  292. if(zr*zr + zi*zi > mb->bailout){
  293. switch(mb->outer){
  294. case ITERATION_COUNT:
  295. zr = i;
  296. c = lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
  297. break;
  298. case NORMALIZED_ITERATION_COUNT:
  299. zr = i + log2(log(mb->bailout) / log(zr*zr + zi*zi));
  300. c = lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
  301. break;
  302. case WHITE:
  303. c = 0xFFFFFF;
  304. break;
  305. case OUTZ:
  306. zr /= mb->bailout;
  307. zi /= mb->bailout;
  308. c = (((int)(zr*128+128))&0xFF)*256 + (((int)(zi*128+128))&0xFF);
  309. }
  310. break;
  311. }
  312. }
  313. break;
  314. }
  315. }
  316. if(!c){
  317. if(mb->inner==PERIOD){
  318. int j;
  319. for(j=i-1; j; j--)
  320. if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < epsilon*epsilon*10)
  321. break;
  322. if(j){
  323. c= i-j;
  324. c= ((c<<5)&0xE0) + ((c<<10)&0xE000) + ((c<<15)&0xE00000);
  325. }
  326. }else if(mb->inner==CONVTIME){
  327. c= floor(i*255.0/mb->maxiter+dv)*0x010101;
  328. } else if(mb->inner==MINCOL){
  329. int j;
  330. double closest=9999;
  331. int closest_index=0;
  332. for(j=i-1; j>=0; j--)
  333. if(SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]) < closest){
  334. closest= SQR(mb->zyklus[j][0]) + SQR(mb->zyklus[j][1]);
  335. closest_index= j;
  336. }
  337. closest = sqrt(closest);
  338. c= lrintf((mb->zyklus[closest_index][0]/closest+1)*127+dv) + lrintf((mb->zyklus[closest_index][1]/closest+1)*127+dv)*256;
  339. }
  340. }
  341. c |= 0xFF000000;
  342. color[x + y*linesize]= c;
  343. if(next_cidx < mb->cache_allocated){
  344. mb->next_cache[next_cidx ].p[0]= cr;
  345. mb->next_cache[next_cidx ].p[1]= ci;
  346. mb->next_cache[next_cidx++].val = c;
  347. }
  348. }
  349. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
  350. }
  351. FFSWAP(void*, mb->next_cache, mb->point_cache);
  352. mb->cache_used = next_cidx;
  353. if(mb->cache_used == mb->cache_allocated)
  354. av_log(ctx, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
  355. }
  356. static int request_frame(AVFilterLink *link)
  357. {
  358. MBContext *mb = link->src->priv;
  359. AVFrame *picref = ff_get_video_buffer(link, mb->w, mb->h);
  360. if (!picref)
  361. return AVERROR(ENOMEM);
  362. picref->sample_aspect_ratio = (AVRational) {1, 1};
  363. picref->pts = mb->pts++;
  364. draw_mandelbrot(link->src, (uint32_t*)picref->data[0], picref->linesize[0]/4, picref->pts);
  365. return ff_filter_frame(link, picref);
  366. }
  367. static const AVFilterPad mandelbrot_outputs[] = {
  368. {
  369. .name = "default",
  370. .type = AVMEDIA_TYPE_VIDEO,
  371. .request_frame = request_frame,
  372. .config_props = config_props,
  373. },
  374. { NULL },
  375. };
  376. AVFilter avfilter_vsrc_mandelbrot = {
  377. .name = "mandelbrot",
  378. .description = NULL_IF_CONFIG_SMALL("Render a Mandelbrot fractal."),
  379. .priv_size = sizeof(MBContext),
  380. .init = init,
  381. .uninit = uninit,
  382. .query_formats = query_formats,
  383. .inputs = NULL,
  384. .outputs = mandelbrot_outputs,
  385. .priv_class = &mandelbrot_class,
  386. };