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.

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