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.

250 lines
7.8KB

  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 "libavutil/imgutils.h"
  29. #include "libavutil/parseutils.h"
  30. enum Outer{
  31. ITERATION_COUNT,
  32. NORMALIZED_ITERATION_COUNT,
  33. };
  34. typedef struct Point {
  35. double p[2];
  36. uint32_t val;
  37. } Point;
  38. typedef struct {
  39. int w, h;
  40. AVRational time_base;
  41. uint64_t pts;
  42. int maxiter;
  43. double start_x;
  44. double start_y;
  45. double start_scale;
  46. double end_scale;
  47. double end_pts;
  48. double bailout;
  49. enum Outer outer;
  50. int cache_allocated;
  51. int cache_used;
  52. Point *point_cache;
  53. Point *next_cache;
  54. double (*zyklus)[2];
  55. } MBContext;
  56. static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
  57. {
  58. MBContext *mb = ctx->priv;
  59. char frame_size [128] = "320x240";
  60. char frame_rate [128] = "25";
  61. AVRational frame_rate_q;
  62. int ret;
  63. mb->maxiter=1024;
  64. mb->start_x=-0.743643887037158704752191506114774;
  65. mb->start_y=-0.131825904205311970493132056385139;
  66. mb->start_scale=3.0;
  67. mb->end_scale=0.3;
  68. mb->end_pts=200;
  69. mb->bailout=100;
  70. mb->outer= NORMALIZED_ITERATION_COUNT;
  71. if (args)
  72. sscanf(args, "%127[^:]:%127[^:]:%d,%lf:%lf:%lf", frame_size, frame_rate, &mb->maxiter, &mb->start_x, &mb->start_y, &mb->start_scale);
  73. if (av_parse_video_size(&mb->w, &mb->h, frame_size) < 0) {
  74. av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
  75. return AVERROR(EINVAL);
  76. }
  77. mb->start_scale /=mb->h;
  78. mb->end_scale /=mb->h;
  79. if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
  80. frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
  81. av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
  82. return AVERROR(EINVAL);
  83. }
  84. mb->time_base.num = frame_rate_q.den;
  85. mb->time_base.den = frame_rate_q.num;
  86. mb->cache_allocated = mb->w * mb->h*2;
  87. mb->cache_used = 0;
  88. mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
  89. mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
  90. mb-> zyklus = av_malloc(sizeof(*mb->zyklus) * mb->maxiter);
  91. return 0;
  92. }
  93. static av_cold void uninit(AVFilterContext *ctx)
  94. {
  95. MBContext *mb = ctx->priv;
  96. int i;
  97. av_freep(&mb->point_cache);
  98. av_freep(&mb-> next_cache);
  99. av_freep(&mb->zyklus);
  100. }
  101. static int query_formats(AVFilterContext *ctx)
  102. {
  103. static const enum PixelFormat pix_fmts[] = {
  104. PIX_FMT_BGR32,
  105. PIX_FMT_NONE
  106. };
  107. avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
  108. return 0;
  109. }
  110. static int config_props(AVFilterLink *inlink)
  111. {
  112. AVFilterContext *ctx = inlink->src;
  113. MBContext *mb = ctx->priv;
  114. if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
  115. return AVERROR(EINVAL);
  116. inlink->w = mb->w;
  117. inlink->h = mb->h;
  118. inlink->time_base = mb->time_base;
  119. return 0;
  120. }
  121. static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
  122. MBContext *mb = ctx->priv;
  123. for(; *in_cidx < mb->cache_used; (*in_cidx)++){
  124. Point *p= &mb->point_cache[*in_cidx];
  125. int x;
  126. if(*in_cidx >= mb->cache_used || p->p[1] > py)
  127. break;
  128. x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
  129. if(x<0 || x >= mb->w)
  130. continue;
  131. if(color) color[x] = p->val;
  132. if(out_cidx && *out_cidx < mb->cache_allocated)
  133. mb->next_cache[(*out_cidx)++]= *p;
  134. }
  135. }
  136. static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
  137. {
  138. MBContext *mb = ctx->priv;
  139. int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
  140. double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
  141. int use_zyklus=0;
  142. fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
  143. for(y=0; y<mb->h; y++){
  144. const double ci=mb->start_y+scale*(y-mb->h/2);
  145. memset(color+linesize*y, 0, sizeof(*color)*mb->w);
  146. fill_from_cache(ctx, color+linesize*y, &in_cidx, &next_cidx, ci, scale);
  147. tmp_cidx= in_cidx;
  148. fill_from_cache(ctx, color+linesize*y, &tmp_cidx, NULL, ci + scale/2, scale);
  149. for(x=0; x<mb->w; x++){
  150. const double cr=mb->start_x+scale*(x-mb->w/2);
  151. double zr=cr;
  152. double zi=ci;
  153. uint32_t c=0;
  154. if(color[x + y*linesize] & 0xFF000000)
  155. continue;
  156. for(i=0; i<mb->maxiter; i++){
  157. double t;
  158. if(zr*zr + zi*zi > mb->bailout){
  159. switch(mb->outer){
  160. case ITERATION_COUNT: zr= i; break;
  161. case NORMALIZED_ITERATION_COUNT: zr= i + (log(log(mb->bailout)) - log(log(sqrt(zr*zr + zi*zi))))/log(2); break;
  162. }
  163. c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
  164. break;
  165. }
  166. t= zr*zr - zi*zi;
  167. zi= 2*zr*zi + ci;
  168. zr= t + cr;
  169. if(use_zyklus){
  170. if(i && mb->zyklus[i>>1][0]==zr && mb->zyklus[i>>1][1]==zi)
  171. break;
  172. mb->zyklus[i][0]= zr;
  173. mb->zyklus[i][1]= zi;
  174. }
  175. }
  176. use_zyklus = !c;
  177. c |= 0xFF000000;
  178. color[x + y*linesize]= c;
  179. if(next_cidx < mb->cache_allocated){
  180. mb->next_cache[next_cidx ].p[0]= cr;
  181. mb->next_cache[next_cidx ].p[1]= ci;
  182. mb->next_cache[next_cidx++].val = c;
  183. }
  184. }
  185. fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
  186. }
  187. FFSWAP(void*, mb->next_cache, mb->point_cache);
  188. mb->cache_used = next_cidx;
  189. }
  190. static int request_frame(AVFilterLink *link)
  191. {
  192. MBContext *mb = link->src->priv;
  193. AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
  194. picref->video->sample_aspect_ratio = (AVRational) {1, 1};
  195. picref->pts = mb->pts++;
  196. picref->pos = -1;
  197. avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
  198. draw_mandelbrot(link->src, picref->data[0], picref->linesize[0]/4, picref->pts);
  199. avfilter_draw_slice(link, 0, mb->h, 1);
  200. avfilter_end_frame(link);
  201. avfilter_unref_buffer(picref);
  202. return 0;
  203. }
  204. AVFilter avfilter_vsrc_mandelbrot = {
  205. .name = "mandelbrot",
  206. .description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
  207. .priv_size = sizeof(MBContext),
  208. .init = init,
  209. .uninit = uninit,
  210. .query_formats = query_formats,
  211. .inputs = (const AVFilterPad[]) {{ .name = NULL}},
  212. .outputs = (const AVFilterPad[]) {{ .name = "default",
  213. .type = AVMEDIA_TYPE_VIDEO,
  214. .request_frame = request_frame,
  215. .config_props = config_props },
  216. { .name = NULL}},
  217. };