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.

422 lines
15KB

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