From: lilo_booter Date: Sun, 28 Aug 2005 12:43:18 +0000 (+0000) Subject: + Added rudimentary chroma to alpha filter (optimised on green by default) X-Git-Url: https://git.sesse.net/?a=commitdiff_plain;h=052a4094e84db1317d143849bb307a8f3c306bab;p=mlt + Added rudimentary chroma to alpha filter (optimised on green by default) git-svn-id: https://mlt.svn.sourceforge.net/svnroot/mlt/trunk/mlt@812 d19143bc-622f-0410-bfdd-b5b2a6649095 --- diff --git a/src/modules/vmfx/Makefile b/src/modules/vmfx/Makefile index 83e76651..98eb5474 100644 --- a/src/modules/vmfx/Makefile +++ b/src/modules/vmfx/Makefile @@ -3,6 +3,7 @@ include ../../../config.mak TARGET = ../libmltvmfx$(LIBSUF) OBJS = factory.o \ + filter_chroma.o \ filter_shape.o \ producer_pgm.o diff --git a/src/modules/vmfx/configure b/src/modules/vmfx/configure index f80432ca..37230f95 100755 --- a/src/modules/vmfx/configure +++ b/src/modules/vmfx/configure @@ -8,6 +8,7 @@ pgm libmltvmfx$LIBSUF EOF cat << EOF >> ../filters.dat +chroma libmltvmfx$LIBSUF shape libmltvmfx$LIBSUF EOF diff --git a/src/modules/vmfx/factory.c b/src/modules/vmfx/factory.c index b1b809b1..cd67d5ce 100644 --- a/src/modules/vmfx/factory.c +++ b/src/modules/vmfx/factory.c @@ -20,6 +20,7 @@ #include +#include "filter_chroma.h" #include "filter_shape.h" #include "producer_pgm.h" @@ -32,6 +33,8 @@ void *mlt_create_producer( char *id, void *arg ) void *mlt_create_filter( char *id, void *arg ) { + if ( !strcmp( id, "chroma" ) ) + return filter_chroma_init( arg ); if ( !strcmp( id, "shape" ) ) return filter_shape_init( arg ); return NULL; diff --git a/src/modules/vmfx/filter_chroma.c b/src/modules/vmfx/filter_chroma.c new file mode 100644 index 00000000..565ac029 --- /dev/null +++ b/src/modules/vmfx/filter_chroma.c @@ -0,0 +1,96 @@ +/* + * filter_chroma.c -- Maps a chroma key to the alpha channel + * Copyright (C) 2005 Visual Media Fx Inc. + * Author: Charles Yates + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "filter_chroma.h" +#include +#include +#include +#include +#include + +static inline int in_range( uint8_t v, uint8_t c, int var ) +{ + return ( v >= c - var ) && ( v <= c + var ); +} + +static inline uint8_t alpha_value( uint8_t a, uint8_t *p, uint8_t u, uint8_t v, int var ) +{ + return ( in_range( *( p + 1 ), u, var ) && in_range( *( p + 3 ), v, var ) ) ? 0 : a; +} + +/** Get the images and map the chroma to the alpha of the frame. +*/ + +static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable ) +{ + mlt_filter this = mlt_frame_pop_service( frame ); + char *key = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "key" ); + int variance = 255 * mlt_properties_get_double( MLT_FILTER_PROPERTIES( this ), "variance" ) + 0.5; + int32_t key_val = strtol( key, &key, 0 ); + uint8_t b = key_val & 0xff; + uint8_t g = ( key_val >> 8 ) & 0xff; + uint8_t r = ( key_val >> 16 ) & 0xff; + uint8_t y, u, v; + + RGB2YUV( r, g, b, y, u, v ); + + if ( mlt_frame_get_image( frame, image, format, width, height, writable ) == 0 ) + { + uint8_t *alpha = mlt_frame_get_alpha_mask( frame ); + uint8_t *p = *image; + int size = *width * *height; + int odd = 0; + while ( size -- ) + { + *alpha = alpha_value( *alpha, p, u, v, variance ); + if ( odd ) p += 4; + odd = !odd; + alpha ++; + } + } + + return 0; +} + +/** Filter processing. +*/ + +static mlt_frame filter_process( mlt_filter this, mlt_frame frame ) +{ + mlt_frame_push_service( frame, this ); + mlt_frame_push_service( frame, filter_get_image ); + return frame; +} + +/** Constructor for the filter. +*/ + +mlt_filter filter_chroma_init( char *arg ) +{ + mlt_filter this = mlt_filter_new( ); + if ( this != NULL ) + { + mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "key", arg == NULL ? "0x00ff00" : arg ); + mlt_properties_set_double( MLT_FILTER_PROPERTIES( this ), "variance", 0.3 ); + this->process = filter_process; + } + return this; +} + diff --git a/src/modules/vmfx/filter_chroma.h b/src/modules/vmfx/filter_chroma.h new file mode 100644 index 00000000..13c7a13c --- /dev/null +++ b/src/modules/vmfx/filter_chroma.h @@ -0,0 +1,28 @@ +/* + * filter_chroma.h -- Maps a chroma key to the alpha channel + * Copyright (C) 2005 Visual Media Fx Inc. + * Author: Charles Yates + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef _FILTER_CHROMA_H_ +#define _FILTER_CHROMA_H_ + +#include + +extern mlt_filter filter_chroma_init( char *arg ); + +#endif