#include <math.h>
#include <string.h>
-#define MAX( x, y ) x > y ? x : y
-#define MIN( x, y ) x < y ? x : y
+#define MAX( x, y ) ( ( x ) > ( y ) ? ( x ) : ( y ) )
+#define MIN( x, y ) ( ( x ) < ( y ) ? ( x ) : ( y ) )
#define SQR( x ) ( x ) * ( x )
/** x, y tuple with double precision */
struct PointF h2;
} BPointF;
-enum MODES { MODE_RGB, MODE_ALPHA, MODE_MATTE };
-const char *MODESTR[3] = { "rgb", "alpha", "matte" };
+enum MODES { MODE_RGB, MODE_ALPHA, MODE_LUMA };
+const char *MODESTR[3] = { "rgb", "alpha", "luma" };
enum ALPHAOPERATIONS { ALPHA_CLEAR, ALPHA_MAX, ALPHA_MIN, ALPHA_ADD, ALPHA_SUB };
const char *ALPHAOPERATIONSTR[5] = { "clear", "max", "min", "add", "sub" };
/** Returns the index of \param string in \param stringList.
* Useful for assigning string parameters to enums. */
-int stringValue( const char *string, const char **stringList, int max )
+static int stringValue( const char *string, const char **stringList, int max )
{
int i;
for ( i = 0; i < max; i++ )
return 0;
}
+/** Sets "spline_is_dirty" to 1 if property "spline" was changed.
+ * We then know when to parse the json stored in "spline" */
+static void rotoPropertyChanged( mlt_service owner, mlt_filter this, char *name )
+{
+ if ( !strcmp( name, "spline" ) )
+ mlt_properties_set_int( MLT_FILTER_PROPERTIES( this ), "_spline_is_dirty", 1 );
+}
+
/** Linear interp */
-inline void lerp( const PointF *a, const PointF *b, PointF *result, double t )
+static inline void lerp( const PointF *a, const PointF *b, PointF *result, double t )
{
result->x = a->x + ( b->x - a->x ) * t;
result->y = a->y + ( b->y - a->y ) * t;
/** Linear interp. with t = 0.5
* Speed gain? */
-inline void lerpHalf( const PointF *a, const PointF *b, PointF *result )
+static inline void lerpHalf( const PointF *a, const PointF *b, PointF *result )
{
result->x = ( a->x + b->x ) * .5;
result->y = ( a->y + b->y ) * .5;
}
/** Turns a json array with two children into a point (x, y tuple). */
-void jsonGetPoint( cJSON *json, PointF *point )
+static void jsonGetPoint( cJSON *json, PointF *point )
{
if ( cJSON_GetArraySize( json ) == 2 )
{
* \param points pointer to array of points. Will be allocated and filled with the points in \param array
* \return number of points
*/
-int json2BCurves( cJSON *array, BPointF **points )
+static int json2BCurves( cJSON *array, BPointF **points )
{
int count = cJSON_GetArraySize( array );
cJSON *child = array->child;
int i = 0;
do
{
- if ( cJSON_GetArraySize( child ) == 3 )
+ if ( child && cJSON_GetArraySize( child ) == 3 )
{
jsonGetPoint( child->child , &(*points)[i].h1 );
jsonGetPoint( child->child->next, &(*points)[i].p );
jsonGetPoint( child->child->next->next, &(*points)[i].h2 );
i++;
}
- } while ( ( child = child->next ) );
+ } while ( child && ( child = child->next ) );
if ( i < count )
*points = mlt_pool_realloc( *points, i * sizeof( BPointF ) );
return i;
}
+/** Blurs \param src horizontally. \See funtion blur. */
+static void blurHorizontal( uint8_t *src, uint8_t *dst, int width, int height, int radius)
+{
+ int x, y, kx, yOff, total, amount, amountInit;
+ amountInit = radius * 2 + 1;
+ for (y = 0; y < height; ++y)
+ {
+ total = 0;
+ yOff = y * width;
+ // Process entire window for first pixel
+ int size = MIN(radius + 1, width);
+ for ( kx = 0; kx < size; ++kx )
+ total += src[yOff + kx];
+ dst[yOff] = total / ( radius + 1 );
+ // Subsequent pixels just update window total
+ for ( x = 1; x < width; ++x )
+ {
+ amount = amountInit;
+ // Subtract pixel leaving window
+ if ( x - radius - 1 >= 0 )
+ total -= src[yOff + x - radius - 1];
+ else
+ amount -= radius - x;
+ // Add pixel entering window
+ if ( x + radius < width )
+ total += src[yOff + x + radius];
+ else
+ amount -= radius - width + x;
+ dst[yOff + x] = total / amount;
+ }
+ }
+}
+
+/** Blurs \param src vertically. \See funtion blur. */
+static void blurVertical( uint8_t *src, uint8_t *dst, int width, int height, int radius)
+{
+ int x, y, ky, total, amount, amountInit;
+ amountInit = radius * 2 + 1;
+ for (x = 0; x < width; ++x)
+ {
+ total = 0;
+ int size = MIN(radius + 1, height);
+ for ( ky = 0; ky < size; ++ky )
+ total += src[x + ky * width];
+ dst[x] = total / ( radius + 1 );
+ for ( y = 1; y < height; ++y )
+ {
+ amount = amountInit;
+ if ( y - radius - 1 >= 0 )
+ total -= src[( y - radius - 1 ) * width + x];
+ else
+ amount -= radius - y;
+ if ( y + radius < height )
+ total += src[( y + radius ) * width + x];
+ else
+ amount -= radius - height + y;
+ dst[y * width + x] = total / amount;
+ }
+ }
+}
+
+/**
+ * Blurs the \param map using a simple "average" blur.
+ * \param map Will be blured; 1bpp
+ * \param width x dimension of channel stored in \param map
+ * \param height y dimension of channel stored in \param map
+ * \param radius blur radius
+ * \param passes blur passes
+ */
+static void blur( uint8_t *map, int width, int height, int radius, int passes )
+{
+ uint8_t *src = mlt_pool_alloc( width * height );
+ uint8_t *tmp = mlt_pool_alloc( width * height );
+
+ int i;
+ for ( i = 0; i < passes; ++i )
+ {
+ memcpy( src, map, width * height );
+ blurHorizontal( src, tmp, width, height, radius );
+ blurVertical( tmp, map, width, height, radius );
+ }
+
+ mlt_pool_release(src);
+ mlt_pool_release(tmp);
+}
+
/**
* Determines which points are located in the polygon and sets their value in \param map to \param value
* \param vertices points defining the polygon
* \param map array of integers of the dimension width * height.
* The map entries belonging to the points in the polygon will be set to \param set * 255 the others to !set * 255.
*/
-void fillMap( PointF *vertices, int count, int width, int height, int invert, uint8_t *map )
+static void fillMap( PointF *vertices, int count, int width, int height, int invert, uint8_t *map )
{
int nodes, nodeX[1024], pixelY, i, j, value;
{
nodeX[i] = MAX( 0, nodeX[i] );
nodeX[i+1] = MIN( nodeX[i+1], width );
- memset( map + width * pixelY + nodeX[i], value, nodeX[i+1] - nodeX[i] + 1 );
+ memset( map + width * pixelY + nodeX[i], value, nodeX[i+1] - nodeX[i] );
}
}
}
/** Determines the point in the middle of the Bézier curve (t = 0.5) defined by \param p1 and \param p2
* using De Casteljau's algorithm.
*/
-void deCasteljau( BPointF *p1, BPointF *p2, BPointF *mid )
+static void deCasteljau( BPointF *p1, BPointF *p2, BPointF *mid )
{
struct PointF ab, bc, cd;
/**
* Calculates points for the cubic Bézier curve defined by \param p1 and \param p2.
- * Points are calculated until the squared distanced between neighbour points is smaller than \param errorSquared.
+ * Points are calculated until the squared distanced between neighbour points is smaller than 2.
* \param points Pointer to list of points. Will be allocted and filled with calculated points.
* \param count Number of calculated points in \param points
* \param size Allocated size of \param points (in elements not in bytes)
*/
-void curvePoints( BPointF p1, BPointF p2, PointF **points, int *count, int *size, const double *errorSquared )
+static void curvePoints( BPointF p1, BPointF p2, PointF **points, int *count, int *size )
{
double errorSqr = SQR( p1.p.x - p2.p.x ) + SQR( p1.p.y - p2.p.y );
if ( *size + 1 >= *count )
{
- *size += (int)sqrt( errorSqr / *errorSquared );
+ *size += (int)sqrt( errorSqr / 2 );
*points = mlt_pool_realloc( *points, *size * sizeof ( struct PointF ) );
}
(*points)[(*count)++] = p1.p;
- if ( errorSqr <= *errorSquared )
+ if ( errorSqr <= 2 )
return;
BPointF mid;
deCasteljau( &p1, &p2, &mid );
- curvePoints( p1, mid, points, count, size, errorSquared );
+ curvePoints( p1, mid, points, count, size );
- curvePoints( mid, p2, points, count, size, errorSquared );
+ curvePoints( mid, p2, points, count, size );
(*points)[*(count)++] = p2.p;
}
/** Do it :-).
*/
-static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
+static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
{
- mlt_properties properties = MLT_FRAME_PROPERTIES( this );
+ mlt_properties unique = mlt_frame_pop_service( frame );
- int mode = mlt_properties_get_int( properties, "mode" );
+ int mode = mlt_properties_get_int( unique, "mode" );
// Get the image
if ( mode == MODE_RGB )
*format = mlt_image_rgb24;
- int error = mlt_frame_get_image( this, image, format, width, height, 1 );
+ int error = mlt_frame_get_image( frame, image, format, width, height, writable );
// Only process if we have no error and a valid colour space
if ( !error )
BPointF *bpoints;
struct PointF *points;
int bcount, length, count, size, i, j;
- bpoints = mlt_properties_get_data( properties, "points", &length );
+ bpoints = mlt_properties_get_data( unique, "points", &length );
bcount = length / sizeof( BPointF );
for ( i = 0; i < bcount; i++ )
bpoints[i].h2.y *= *height;
}
- double errorSqr = (double)SQR( mlt_properties_get_int( properties, "precision" ) );
count = 0;
size = 1;
points = mlt_pool_alloc( size * sizeof( struct PointF ) );
for ( i = 0; i < bcount; i++ )
{
j = (i + 1) % bcount;
- curvePoints( bpoints[i], bpoints[j], &points, &count, &size, &errorSqr );
+ curvePoints( bpoints[i], bpoints[j], &points, &count, &size );
}
if ( count )
{
- uint8_t *map = mlt_pool_alloc( *width * *height );
- int invert = mlt_properties_get_int( properties, "invert" );
+ length = *width * *height;
+ uint8_t *map = mlt_pool_alloc( length );
+ int invert = mlt_properties_get_int( unique, "invert" );
fillMap( points, count, *width, *height, invert, map );
- double bpp = 4;
- if ( mode != MODE_ALPHA )
- {
- if ( *format == mlt_image_rgb24 )
- bpp = 3;
- else if ( *format == mlt_image_yuv422 )
- bpp = 2;
- else if ( *format == mlt_image_yuv420p )
- bpp = 3 / 2.;
- }
-
- i = 0;
- length = *width * *height;
+ int feather = mlt_properties_get_int( unique, "feather" );
+ if ( feather && mode != MODE_RGB )
+ blur( map, *width, *height, feather, mlt_properties_get_int( unique, "feather_passes" ) );
+ int bpp;
+ size = mlt_image_format_size( *format, *width, *height, &bpp );
uint8_t *p = *image;
- uint8_t *q = *image + (int)( length * bpp );
+ uint8_t *q = *image + size;
+ i = 0;
uint8_t *alpha;
switch ( mode )
p += 3;
}
break;
- case MODE_MATTE:
+ case MODE_LUMA:
switch ( *format )
{
case mlt_image_rgb24:
while ( p != q )
{
p[0] = p[1] = p[2] = map[i++];
- p += (int)bpp;
+ p += bpp;
}
break;
case mlt_image_yuv422:
}
break;
case MODE_ALPHA:
- alpha = mlt_frame_get_alpha_mask( this );
- switch ( mlt_properties_get_int( properties, "alpha_operation" ) )
+ switch ( *format )
{
- case ALPHA_CLEAR:
- memcpy( alpha, map, length );
- break;
- case ALPHA_MAX:
- for ( ; i < length; i++, alpha++ )
- *alpha = MAX( map[i], *alpha );
- break;
- case ALPHA_MIN:
- for ( ; i < length; i++, alpha++ )
- *alpha = MIN( map[i], *alpha );
- break;
- case ALPHA_ADD:
- for ( ; i < length; i++, alpha++ )
- *alpha = MIN( *alpha + map[i], 255 );
+ case mlt_image_rgb24a:
+ case mlt_image_opengl:
+ switch ( mlt_properties_get_int( unique, "alpha_operation" ) )
+ {
+ case ALPHA_CLEAR:
+ while ( p != q )
+ {
+ p[3] = map[i++];
+ p += 4;
+ }
+ break;
+ case ALPHA_MAX:
+ while ( p != q )
+ {
+ p[3] = MAX( p[3], map[i] );
+ p += 4;
+ i++;
+ }
+ break;
+ case ALPHA_MIN:
+ while ( p != q )
+ {
+ p[3] = MIN( p[3], map[i] );
+ p += 4;
+ i++;
+ }
+ break;
+ case ALPHA_ADD:
+ while ( p != q )
+ {
+ p[3] = MIN( p[3] + map[i], 255 );
+ p += 4;
+ i++;
+ }
+ break;
+ case ALPHA_SUB:
+ while ( p != q )
+ {
+ p[3] = MAX( p[3] - map[i], 0 );
+ p += 4;
+ i++;
+ }
+ break;
+ }
break;
- case ALPHA_SUB:
- for ( ; i < length; i++, alpha++ )
- *alpha = MAX( *alpha - map[i], 0 );
+ default:
+ alpha = mlt_frame_get_alpha_mask( frame );
+ switch ( mlt_properties_get_int( unique, "alpha_operation" ) )
+ {
+ case ALPHA_CLEAR:
+ memcpy( alpha, map, length );
+ break;
+ case ALPHA_MAX:
+ for ( ; i < length; i++, alpha++ )
+ *alpha = MAX( map[i], *alpha );
+ break;
+ case ALPHA_MIN:
+ for ( ; i < length; i++, alpha++ )
+ *alpha = MIN( map[i], *alpha );
+ break;
+ case ALPHA_ADD:
+ for ( ; i < length; i++, alpha++ )
+ *alpha = MIN( *alpha + map[i], 255 );
+ break;
+ case ALPHA_SUB:
+ for ( ; i < length; i++, alpha++ )
+ *alpha = MAX( *alpha - map[i], 0 );
+ break;
+ }
break;
}
break;
/** Filter processing.
*/
-static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
+static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
{
- mlt_properties properties = MLT_FILTER_PROPERTIES( this );
- mlt_properties frameProperties = MLT_FRAME_PROPERTIES( frame );
- char *spline = mlt_properties_get( properties, "spline" );
- char *splineOld = mlt_properties_get( properties, "spline_old" );
+ mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
+ int splineIsDirty = mlt_properties_get_int( properties, "_spline_is_dirty" );
char *modeStr = mlt_properties_get( properties, "mode" );
+ cJSON *root = mlt_properties_get_data( properties, "_spline_parsed", NULL );
- cJSON *root;
- int newSpline = 1;
- if ( splineOld != NULL && strlen( spline ) && strcmp( spline, splineOld ) == 0 ) {
- // the very same parameter was already parsed by json, use the saved json struct
- newSpline = 0;
- root = mlt_properties_get_data( properties, "spline_json", NULL );
- }
- else
+ if ( splineIsDirty || root == NULL )
{
+ // we need to (re-)parse
+ char *spline = mlt_properties_get( properties, "spline" );
root = cJSON_Parse( spline );
+ mlt_properties_set_data( properties, "_spline_parsed", root, 0, (mlt_destructor)cJSON_Delete, NULL );
+ mlt_properties_set_int( properties, "_spline_is_dirty", 0 );
}
if ( root == NULL )
if ( root->type == cJSON_Array )
{
/*
- * constant (over time)
+ * constant
*/
count = json2BCurves( root, &points );
}
time = mlt_frame_get_position( frame );
cJSON *keyframe = root->child;
- cJSON *keyframeOld = NULL;
+ cJSON *keyframeOld = keyframe;
+
+ if ( !keyframe )
+ return frame;
+
while ( atoi( keyframe->string ) < time && keyframe->next )
{
keyframeOld = keyframe;
keyframe = keyframe->next;
}
- if ( keyframeOld == NULL ) {
- // parameter has only 1 keyframe or we are before the 1. keyframe
- keyframeOld = keyframe;
- }
-
- if ( !keyframe )
- {
- if ( keyframeOld )
- {
- // parameter malformed
- keyframe = keyframeOld;
- }
- else if ( root->child )
- {
- // parameter malformed
- keyframe = root->child;
- keyframeOld = keyframe;
- }
- else
- {
- // json object has no children
- cJSON_Delete( root );
- return frame;
- }
- }
-
- pos2 = atoi( keyframe->string );
pos1 = atoi( keyframeOld->string );
+ pos2 = atoi( keyframe->string );
if ( pos1 >= pos2 || time >= pos2 )
{
- // keyframes in wrong order or after last keyframe
+ // keyframes in wrong order or before first / after last keyframe
count = json2BCurves( keyframe, &points );
}
else
*/
BPointF *p1, *p2;
- int c1, c2;
- c1 = json2BCurves( keyframeOld, &p1 );
- c2 = json2BCurves( keyframe, &p2 );
+ int c1 = json2BCurves( keyframeOld, &p1 );
+ int c2 = json2BCurves( keyframe, &p2 );
- if ( c1 > c2 )
+ // range 0-1
+ double position = ( time - pos1 ) / (double)( pos2 - pos1 + 1 );
+
+ count = MIN( c1, c2 ); // additional points are ignored
+ points = mlt_pool_alloc( count * sizeof( BPointF ) );
+ for ( i = 0; i < count; i++ )
{
- // number of points decreasing from p1 to p2; we can't handle this yet
- count = c2;
- points = mlt_pool_alloc( count * sizeof( BPointF ) );
- memcpy( points, p2, count * sizeof( BPointF ) );
- mlt_pool_release( p1 );
- mlt_pool_release( p2 );
+ lerp( &(p1[i].h1), &(p2[i].h1), &(points[i].h1), position );
+ lerp( &(p1[i].p), &(p2[i].p), &(points[i].p), position );
+ lerp( &(p1[i].h2), &(p2[i].h2), &(points[i].h2), position );
}
- else
- {
- // range 0-1
- double position = ( time - pos1 ) / (double)( pos2 - pos1 + 1 );
-
- count = c1; // additional points in p2 are ignored
- points = mlt_pool_alloc( count * sizeof( BPointF ) );
- for ( i = 0; i < count; i++ )
- {
- lerp( &(p1[i].h1), &(p2[i].h1), &(points[i].h1), position );
- lerp( &(p1[i].p), &(p2[i].p), &(points[i].p), position );
- lerp( &(p1[i].h2), &(p2[i].h2), &(points[i].h2), position );
- }
- mlt_pool_release( p1 );
- mlt_pool_release( p2 );
- }
+ mlt_pool_release( p1 );
+ mlt_pool_release( p2 );
}
}
else
{
- cJSON_Delete( root );
return frame;
}
- int length = count * sizeof( BPointF );
-
- if ( newSpline )
- {
- mlt_properties_set_data( properties, "spline_json", root, 0, (mlt_destructor)cJSON_Delete, NULL );
- mlt_properties_set( properties, "spline_old", strdup( spline ) );
- }
-
- mlt_properties_set_data( frameProperties, "points", points, length, (mlt_destructor)mlt_pool_release, NULL );
- mlt_properties_set_int( frameProperties, "mode", stringValue( modeStr, MODESTR, 3 ) );
- mlt_properties_set_int( frameProperties, "alpha_operation", stringValue( mlt_properties_get( properties, "alpha_operation" ), ALPHAOPERATIONSTR, 5 ) );
- mlt_properties_set_int( frameProperties, "invert", mlt_properties_get_int( properties, "invert" ) );
- mlt_properties_set_int( frameProperties, "precision", mlt_properties_get_int( properties, "precision" ) );
+ mlt_properties unique = mlt_frame_unique_properties( frame, MLT_FILTER_SERVICE( filter ) );
+ mlt_properties_set_data( unique, "points", points, count * sizeof( BPointF ), (mlt_destructor)mlt_pool_release, NULL );
+ mlt_properties_set_int( unique, "mode", stringValue( modeStr, MODESTR, 3 ) );
+ mlt_properties_set_int( unique, "alpha_operation", stringValue( mlt_properties_get( properties, "alpha_operation" ), ALPHAOPERATIONSTR, 5 ) );
+ mlt_properties_set_int( unique, "invert", mlt_properties_get_int( properties, "invert" ) );
+ mlt_properties_set_int( unique, "feather", mlt_properties_get_int( properties, "feather" ) );
+ mlt_properties_set_int( unique, "feather_passes", mlt_properties_get_int( properties, "feather_passes" ) );
+ mlt_frame_push_service( frame, unique );
mlt_frame_push_get_image( frame, filter_get_image );
return frame;
*/
mlt_filter filter_rotoscoping_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
{
- mlt_filter this = mlt_filter_new( );
- if ( this != NULL )
+ mlt_filter filter = mlt_filter_new( );
+ if ( filter )
{
- this->process = filter_process;
- mlt_properties properties = MLT_FILTER_PROPERTIES( this );
+ filter->process = filter_process;
+ mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
mlt_properties_set( properties, "mode", "alpha" );
mlt_properties_set( properties, "alpha_operation", "clear" );
mlt_properties_set_int( properties, "invert", 0 );
- mlt_properties_set_int( properties, "precision", 1 );
- if ( arg != NULL )
+ mlt_properties_set_int( properties, "feather", 0 );
+ mlt_properties_set_int( properties, "feather_passes", 1 );
+ if ( arg )
mlt_properties_set( properties, "spline", arg );
+
+ mlt_events_listen( properties, filter, "property-changed", (mlt_listener)rotoPropertyChanged );
}
- return this;
+ return filter;
}