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.

406 lines
14KB

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