2011-11-12 00:47:06 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011 Michael Niedermayer
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* The vsrc_color filter from Stefano Sabatini was used as template to create
|
|
|
|
* this
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Mandelbrot fraktal renderer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "libavutil/imgutils.h"
|
|
|
|
#include "libavutil/parseutils.h"
|
|
|
|
|
2011-11-13 20:14:10 +03:00
|
|
|
#define SQR(a) ((a)*(a))
|
|
|
|
|
2011-11-12 01:22:04 +03:00
|
|
|
enum Outer{
|
|
|
|
ITERATION_COUNT,
|
|
|
|
NORMALIZED_ITERATION_COUNT,
|
|
|
|
};
|
|
|
|
|
2011-11-13 21:26:22 +03:00
|
|
|
enum Inner{
|
|
|
|
BLACK,
|
2011-11-13 21:28:24 +03:00
|
|
|
PERIOD,
|
2011-11-13 21:26:22 +03:00
|
|
|
};
|
|
|
|
|
2011-11-12 17:44:13 +03:00
|
|
|
typedef struct Point {
|
|
|
|
double p[2];
|
|
|
|
uint32_t val;
|
|
|
|
} Point;
|
|
|
|
|
2011-11-12 00:47:06 +03:00
|
|
|
typedef struct {
|
|
|
|
int w, h;
|
|
|
|
AVRational time_base;
|
|
|
|
uint64_t pts;
|
|
|
|
int maxiter;
|
|
|
|
double start_x;
|
|
|
|
double start_y;
|
|
|
|
double start_scale;
|
2011-11-12 01:58:08 +03:00
|
|
|
double end_scale;
|
|
|
|
double end_pts;
|
2011-11-12 01:21:42 +03:00
|
|
|
double bailout;
|
2011-11-12 01:22:04 +03:00
|
|
|
enum Outer outer;
|
2011-11-13 21:26:22 +03:00
|
|
|
enum Inner inner;
|
2011-11-12 17:44:13 +03:00
|
|
|
int cache_allocated;
|
|
|
|
int cache_used;
|
|
|
|
Point *point_cache;
|
|
|
|
Point *next_cache;
|
2011-11-12 18:33:12 +03:00
|
|
|
double (*zyklus)[2];
|
2011-11-12 00:47:06 +03:00
|
|
|
} MBContext;
|
|
|
|
|
|
|
|
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
|
|
|
|
{
|
|
|
|
MBContext *mb = ctx->priv;
|
2011-11-12 19:04:08 +03:00
|
|
|
char frame_size [128] = "640x480";
|
2011-11-12 00:47:06 +03:00
|
|
|
char frame_rate [128] = "25";
|
|
|
|
AVRational frame_rate_q;
|
|
|
|
|
2011-11-12 18:34:40 +03:00
|
|
|
mb->maxiter=4096;
|
2011-11-12 17:42:55 +03:00
|
|
|
mb->start_x=-0.743643887037158704752191506114774;
|
|
|
|
mb->start_y=-0.131825904205311970493132056385139;
|
2011-11-12 00:47:06 +03:00
|
|
|
mb->start_scale=3.0;
|
2011-11-12 01:58:08 +03:00
|
|
|
mb->end_scale=0.3;
|
2011-11-12 19:04:08 +03:00
|
|
|
mb->end_pts=800;
|
2011-11-13 02:20:34 +03:00
|
|
|
mb->bailout=10;
|
2011-11-12 01:22:04 +03:00
|
|
|
mb->outer= NORMALIZED_ITERATION_COUNT;
|
2011-11-12 00:47:06 +03:00
|
|
|
if (args)
|
2011-11-13 02:20:34 +03:00
|
|
|
sscanf(args, "%127[^:]:%127[^:]:%d:%lf:%lf:%lf:%lf:%lf:%lf:%d", frame_size, frame_rate,
|
|
|
|
&mb->maxiter, &mb->start_x, &mb->start_y, &mb->start_scale, &mb->end_scale,
|
|
|
|
&mb->end_pts, &mb->bailout, &mb->outer);
|
|
|
|
mb->bailout *= mb->bailout;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
|
|
|
if (av_parse_video_size(&mb->w, &mb->h, frame_size) < 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
mb->start_scale /=mb->h;
|
2011-11-12 01:58:08 +03:00
|
|
|
mb->end_scale /=mb->h;
|
2011-11-12 00:47:06 +03:00
|
|
|
|
|
|
|
if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
|
|
|
|
frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
|
|
|
|
av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
}
|
|
|
|
mb->time_base.num = frame_rate_q.den;
|
|
|
|
mb->time_base.den = frame_rate_q.num;
|
|
|
|
|
2011-11-12 19:04:50 +03:00
|
|
|
mb->cache_allocated = mb->w * mb->h * 3;
|
2011-11-12 17:44:13 +03:00
|
|
|
mb->cache_used = 0;
|
|
|
|
mb->point_cache= av_malloc(sizeof(*mb->point_cache)*mb->cache_allocated);
|
|
|
|
mb-> next_cache= av_malloc(sizeof(*mb-> next_cache)*mb->cache_allocated);
|
2011-11-12 18:33:12 +03:00
|
|
|
mb-> zyklus = av_malloc(sizeof(*mb->zyklus) * mb->maxiter);
|
2011-11-12 17:44:13 +03:00
|
|
|
|
2011-11-12 00:47:06 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static av_cold void uninit(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
MBContext *mb = ctx->priv;
|
|
|
|
|
2011-11-12 17:44:13 +03:00
|
|
|
av_freep(&mb->point_cache);
|
|
|
|
av_freep(&mb-> next_cache);
|
2011-11-12 18:33:12 +03:00
|
|
|
av_freep(&mb->zyklus);
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int query_formats(AVFilterContext *ctx)
|
|
|
|
{
|
|
|
|
static const enum PixelFormat pix_fmts[] = {
|
|
|
|
PIX_FMT_BGR32,
|
|
|
|
PIX_FMT_NONE
|
|
|
|
};
|
|
|
|
|
|
|
|
avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int config_props(AVFilterLink *inlink)
|
|
|
|
{
|
|
|
|
AVFilterContext *ctx = inlink->src;
|
|
|
|
MBContext *mb = ctx->priv;
|
|
|
|
|
|
|
|
if (av_image_check_size(mb->w, mb->h, 0, ctx) < 0)
|
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
|
|
|
inlink->w = mb->w;
|
|
|
|
inlink->h = mb->h;
|
|
|
|
inlink->time_base = mb->time_base;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-11-12 17:44:13 +03:00
|
|
|
static void fill_from_cache(AVFilterContext *ctx, uint32_t *color, int *in_cidx, int *out_cidx, double py, double scale){
|
|
|
|
MBContext *mb = ctx->priv;
|
|
|
|
for(; *in_cidx < mb->cache_used; (*in_cidx)++){
|
|
|
|
Point *p= &mb->point_cache[*in_cidx];
|
|
|
|
int x;
|
2011-11-13 15:51:09 +03:00
|
|
|
if(p->p[1] > py)
|
2011-11-12 17:44:13 +03:00
|
|
|
break;
|
|
|
|
x= round((p->p[0] - mb->start_x) / scale + mb->w/2);
|
|
|
|
if(x<0 || x >= mb->w)
|
|
|
|
continue;
|
|
|
|
if(color) color[x] = p->val;
|
|
|
|
if(out_cidx && *out_cidx < mb->cache_allocated)
|
|
|
|
mb->next_cache[(*out_cidx)++]= *p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-12 00:47:06 +03:00
|
|
|
static void draw_mandelbrot(AVFilterContext *ctx, uint32_t *color, int linesize, int64_t pts)
|
|
|
|
{
|
|
|
|
MBContext *mb = ctx->priv;
|
2011-11-12 17:44:13 +03:00
|
|
|
int x,y,i, in_cidx=0, next_cidx=0, tmp_cidx;
|
2011-11-12 01:58:08 +03:00
|
|
|
double scale= mb->start_scale*pow(mb->end_scale/mb->start_scale, pts/mb->end_pts);
|
2011-11-12 18:33:56 +03:00
|
|
|
int use_zyklus=0;
|
2011-11-12 17:44:13 +03:00
|
|
|
fill_from_cache(ctx, NULL, &in_cidx, NULL, mb->start_y+scale*(-mb->h/2-0.5), scale);
|
2011-11-12 00:47:06 +03:00
|
|
|
for(y=0; y<mb->h; y++){
|
2011-11-12 17:44:13 +03:00
|
|
|
const double ci=mb->start_y+scale*(y-mb->h/2);
|
|
|
|
memset(color+linesize*y, 0, sizeof(*color)*mb->w);
|
|
|
|
fill_from_cache(ctx, color+linesize*y, &in_cidx, &next_cidx, ci, scale);
|
|
|
|
tmp_cidx= in_cidx;
|
|
|
|
fill_from_cache(ctx, color+linesize*y, &tmp_cidx, NULL, ci + scale/2, scale);
|
|
|
|
|
2011-11-12 00:47:06 +03:00
|
|
|
for(x=0; x<mb->w; x++){
|
2011-11-12 01:58:08 +03:00
|
|
|
const double cr=mb->start_x+scale*(x-mb->w/2);
|
2011-11-12 00:47:06 +03:00
|
|
|
double zr=cr;
|
|
|
|
double zi=ci;
|
|
|
|
uint32_t c=0;
|
|
|
|
|
2011-11-12 17:44:13 +03:00
|
|
|
if(color[x + y*linesize] & 0xFF000000)
|
|
|
|
continue;
|
|
|
|
|
2011-11-13 21:28:24 +03:00
|
|
|
use_zyklus= (x==0 || mb->inner!=BLACK ||color[x-1 + y*linesize] == 0xFF000000);
|
2011-11-13 17:14:25 +03:00
|
|
|
|
2011-11-12 01:38:27 +03:00
|
|
|
for(i=0; i<mb->maxiter; i++){
|
2011-11-12 00:47:06 +03:00
|
|
|
double t;
|
2011-11-12 01:21:42 +03:00
|
|
|
if(zr*zr + zi*zi > mb->bailout){
|
2011-11-12 01:22:04 +03:00
|
|
|
switch(mb->outer){
|
2011-11-13 20:14:40 +03:00
|
|
|
case ITERATION_COUNT: zr = i - (SQR(zr-cr)+SQR(zi-ci) > SQR(mb->bailout)); break;
|
2011-11-13 18:09:58 +03:00
|
|
|
case NORMALIZED_ITERATION_COUNT: zr= i + log2(log(mb->bailout) / log(zr*zr + zi*zi)); break;
|
2011-11-12 01:22:04 +03:00
|
|
|
}
|
2011-11-12 00:47:06 +03:00
|
|
|
c= lrintf((sin(zr)+1)*127) + lrintf((sin(zr/1.234)+1)*127)*256*256 + lrintf((sin(zr/100)+1)*127)*256;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-13 18:55:28 +03:00
|
|
|
t= zr*zr - zi*zi + cr;
|
2011-11-12 00:47:06 +03:00
|
|
|
zi= 2*zr*zi + ci;
|
2011-11-12 18:33:56 +03:00
|
|
|
if(use_zyklus){
|
2011-11-13 18:55:28 +03:00
|
|
|
mb->zyklus[i][0]= t;
|
|
|
|
mb->zyklus[i][1]= zi;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
zr= t*t - zi*zi+cr;
|
|
|
|
zi= 2*t*zi + ci;
|
|
|
|
if(use_zyklus){
|
|
|
|
if(mb->zyklus[i>>1][0]==zr && mb->zyklus[i>>1][1]==zi)
|
2011-11-12 18:33:56 +03:00
|
|
|
break;
|
|
|
|
mb->zyklus[i][0]= zr;
|
|
|
|
mb->zyklus[i][1]= zi;
|
|
|
|
}
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
2011-11-13 21:28:24 +03:00
|
|
|
if(!c && mb->inner==PERIOD){
|
|
|
|
int j;
|
|
|
|
for(j=i-1; j; j--)
|
|
|
|
if(SQR(mb->zyklus[j][0]-zr) + SQR(mb->zyklus[j][1]-zi) < 0.0000000000000001)
|
|
|
|
break;
|
|
|
|
if(j){
|
|
|
|
c= i-j;
|
|
|
|
c= ((c<<5)&0xE0) + ((c<<16)&0xE000) + ((c<<27)&0xE00000);
|
|
|
|
}
|
|
|
|
}
|
2011-11-12 17:44:13 +03:00
|
|
|
c |= 0xFF000000;
|
2011-11-12 00:47:06 +03:00
|
|
|
color[x + y*linesize]= c;
|
2011-11-12 17:44:13 +03:00
|
|
|
if(next_cidx < mb->cache_allocated){
|
|
|
|
mb->next_cache[next_cidx ].p[0]= cr;
|
|
|
|
mb->next_cache[next_cidx ].p[1]= ci;
|
|
|
|
mb->next_cache[next_cidx++].val = c;
|
|
|
|
}
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
2011-11-12 17:44:13 +03:00
|
|
|
fill_from_cache(ctx, NULL, &in_cidx, &next_cidx, ci + scale/2, scale);
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
2011-11-12 17:44:13 +03:00
|
|
|
FFSWAP(void*, mb->next_cache, mb->point_cache);
|
|
|
|
mb->cache_used = next_cidx;
|
2011-11-12 19:05:11 +03:00
|
|
|
if(mb->cache_used == mb->cache_allocated)
|
|
|
|
av_log(0, AV_LOG_INFO, "Mandelbrot cache is too small!\n");
|
2011-11-12 00:47:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static int request_frame(AVFilterLink *link)
|
|
|
|
{
|
|
|
|
MBContext *mb = link->src->priv;
|
|
|
|
AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, mb->w, mb->h);
|
|
|
|
picref->video->sample_aspect_ratio = (AVRational) {1, 1};
|
|
|
|
picref->pts = mb->pts++;
|
|
|
|
picref->pos = -1;
|
|
|
|
|
|
|
|
avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
|
|
|
|
draw_mandelbrot(link->src, picref->data[0], picref->linesize[0]/4, picref->pts);
|
|
|
|
avfilter_draw_slice(link, 0, mb->h, 1);
|
|
|
|
avfilter_end_frame(link);
|
|
|
|
avfilter_unref_buffer(picref);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
AVFilter avfilter_vsrc_mandelbrot = {
|
|
|
|
.name = "mandelbrot",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Mandelbrot renderer"),
|
|
|
|
|
|
|
|
.priv_size = sizeof(MBContext),
|
|
|
|
.init = init,
|
|
|
|
.uninit = uninit,
|
|
|
|
|
|
|
|
.query_formats = query_formats,
|
|
|
|
|
|
|
|
.inputs = (const AVFilterPad[]) {{ .name = NULL}},
|
|
|
|
|
|
|
|
.outputs = (const AVFilterPad[]) {{ .name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_VIDEO,
|
|
|
|
.request_frame = request_frame,
|
|
|
|
.config_props = config_props },
|
|
|
|
{ .name = NULL}},
|
|
|
|
};
|