26.06.14 09:31, Dan Dennedy написав(ла):
On Tue, Jun 24, 2014 at 11:36 PM, Maksym Veremeyenko<ve...@m1stereo.tv>  wrote:
Hi,

attached patch implement *matte* transition. it perform replacing alpha
channel of track A with luma of track B. Luma of track B will be scaled if
it required.

Hi Maksym, excellent contribution!
Have you seen the shape filter in the vmfx directory? It basically
does something similar when you set use_luminance=1, but it seems to
have been designed for some more odd and silly use cases whereas your
transition simply satisfies a more common need.

Some comments.
+/*
+ * transition_luma.c -- a generic dissolve/wipe processor

Fix the file name and description
done


+ * Copyright (C) 2003-2014 Ushodaya Enterprises Limited
+ * Author: Dan Dennedy<d...@dennedy.org>

Make yourself the author!
replaced only author string, copyright remain the same


+ * Adapted from Kino Plugin Timfx, which is
+ * Copyright (C) 2002 Timothy M. Shead<tsh...@k-3d.com>

Rremove those lines as they do not apply.

+#include "transition_composite.h"

Remove that include.

+// mlt_service_lock( MLT_TRANSITION_SERVICE( transition ) );
...
+// mlt_service_unlock( MLT_TRANSITION_SERVICE( transition ) );

Services need to be locked if they modify the state of the service
(transition, here) because in parallel processing there can be
multiple threads in the same get_image function. In your case, you are
not modifying state. So, remove those lines, which leads to:

+ // Get the properties of the transition
+ mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );

You never use this properties! You can remove those lines. And..

+ // Get the transition object
+ mlt_transition transition = mlt_frame_pop_service( a_frame );

You can remove those lines and the corresponding
mlt_frame_push_service() in your process function.

+// if ( b_frame == NULL )
+// {
+//
+// }
+ if ( b_frame != NULL )
+ {

You push the b_frame from your process function. In the process
function, you will always get a valid b_frame. Those lines can be
removed and code shifted to left when removing the block braces.

done

+++ b/src/modules/core/transition_matte.yml
@@ -0,0 +1,12 @@
+schema_version: 0.1
+type: transition
+identifier: matte
+title: Matte
+version: 1
+copyright: Ushodaya Enterprises Limited
+creator: Dan Dennedy

Again, make yourself the author.
replaced only author string, copyright remain the same


+license: LGPLv2.1
+language: en
+tags:
+  - Video
+description: Replace alphachannel with data from luma channel of other track.

Please document the full_luma property under a "parameters:" section.

i added more explanation and ffmpeg calls examples.



--
________________________________________
Maksym Veremeyenko
>From db129d6be3307584538574ed72039f29c0e0c485 Mon Sep 17 00:00:00 2001
From: Maksym Veremeyenko <ve...@m1.tv>
Date: Thu, 26 Jun 2014 13:35:08 +0300
Subject: [PATCH] implement matte transition

---
 src/modules/core/Makefile             |    1 +
 src/modules/core/factory.c            |    3 +
 src/modules/core/transition_matte.c   |  155 +++++++++++++++++++++++++++++++++
 src/modules/core/transition_matte.yml |   44 +++++++++
 4 files changed, 203 insertions(+), 0 deletions(-)
 create mode 100644 src/modules/core/transition_matte.c
 create mode 100644 src/modules/core/transition_matte.yml

diff --git a/src/modules/core/Makefile b/src/modules/core/Makefile
index f4b73f4..71bd848 100644
--- a/src/modules/core/Makefile
+++ b/src/modules/core/Makefile
@@ -39,6 +39,7 @@ OBJS = factory.o \
 	   transition_luma.o \
 	   transition_mix.o \
 	   transition_region.o \
+	   transition_matte.o \
 	   consumer_multi.o \
 	   consumer_null.o
 
diff --git a/src/modules/core/factory.c b/src/modules/core/factory.c
index 64b2fe2..86aaab3 100644
--- a/src/modules/core/factory.c
+++ b/src/modules/core/factory.c
@@ -56,6 +56,7 @@ extern mlt_producer producer_noise_init( mlt_profile profile, mlt_service_type t
 #include "transition_composite.h"
 extern mlt_transition transition_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
 extern mlt_transition transition_mix_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
+extern mlt_transition transition_matte_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg );
 #include "transition_region.h"
 
 static mlt_properties metadata( mlt_service_type type, const char *id, void *data )
@@ -105,6 +106,7 @@ MLT_REPOSITORY
 	MLT_REGISTER( transition_type, "composite", transition_composite_init );
 	MLT_REGISTER( transition_type, "luma", transition_luma_init );
 	MLT_REGISTER( transition_type, "mix", transition_mix_init );
+	MLT_REGISTER( transition_type, "matte", transition_matte_init );
 	MLT_REGISTER( transition_type, "region", transition_region_init );
 
 	MLT_REGISTER_METADATA( consumer_type, "multi", metadata, "consumer_multi.yml" );
@@ -139,5 +141,6 @@ MLT_REPOSITORY
 	MLT_REGISTER_METADATA( transition_type, "composite", metadata, "transition_composite.yml" );
 	MLT_REGISTER_METADATA( transition_type, "luma", metadata, "transition_luma.yml" );
 	MLT_REGISTER_METADATA( transition_type, "mix", metadata, "transition_mix.yml" );
+	MLT_REGISTER_METADATA( transition_type, "matte", metadata, "transition_matte.yml" );
 	MLT_REGISTER_METADATA( transition_type, "region", metadata, "transition_region.yml" );
 }
diff --git a/src/modules/core/transition_matte.c b/src/modules/core/transition_matte.c
new file mode 100644
index 0000000..c127761
--- /dev/null
+++ b/src/modules/core/transition_matte.c
@@ -0,0 +1,155 @@
+/*
+ * transition_matte.c -- replace alpha channel of track
+ *
+ * Copyright (C) 2003-2014 Ushodaya Enterprises Limited
+ * Author: Maksym Veremeyenko <ve...@m1stereo.tv>
+ *
+ * This library 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.
+ *
+ * This library 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 this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include <framework/mlt.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+#include <math.h>
+#include "transition_composite.h"
+
+typedef void ( *copy_luma_fn )(uint8_t* alpha_a, int stride_a, uint8_t* image_b, int stride_b, int width, int height);
+
+static void copy_Y_to_A_full_luma(uint8_t* alpha_a, int stride_a, uint8_t* image_b, int stride_b, int width, int height)
+{
+	int i, j;
+
+	for(j = 0; j < height; j++)
+	{
+		for(i = 0; i < width; i++)
+			alpha_a[i] = image_b[2*i];
+		alpha_a += stride_a;
+		image_b += stride_b;
+	};
+};
+
+static void copy_Y_to_A_scaled_luma(uint8_t* alpha_a, int stride_a, uint8_t* image_b, int stride_b, int width, int height)
+{
+	int i, j;
+
+	for(j = 0; j < height; j++)
+	{
+		for(i = 0; i < width; i++)
+		{
+			unsigned int p = image_b[2*i];
+
+			if(p < 16)
+				p = 16;
+			if(p > 235)
+				p = 235;
+			p = (p - 16) * 255 / 219;
+
+			alpha_a[i] = p;
+		};
+
+		alpha_a += stride_a;
+		image_b += stride_b;
+	};
+};
+
+/** Get the image.
+*/
+
+static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
+{
+	// Get the b frame from the stack
+	mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
+
+	mlt_frame_get_image( a_frame, image, format, width, height, 1 );
+
+	// Get the properties of the a frame
+	mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
+
+	// Get the properties of the b frame
+	mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
+
+	int
+		width_a = mlt_properties_get_int( a_props, "width" ),
+		width_b = mlt_properties_get_int( b_props, "width" ),
+		height_a = mlt_properties_get_int( a_props, "height" ),
+		height_b = mlt_properties_get_int( b_props, "height" );
+
+	uint8_t *alpha_a, *image_b;
+
+	copy_luma_fn copy_luma = mlt_properties_get_int(b_props, "full_luma")?
+		copy_Y_to_A_full_luma:copy_Y_to_A_scaled_luma;
+
+	// This compositer is yuv422 only
+	*format = mlt_image_yuv422;
+
+	// Get the image from the a frame
+	mlt_frame_get_image( b_frame, &image_b, format, &width_b, &height_b, 1 );
+	alpha_a = mlt_frame_get_alpha_mask( a_frame );
+
+	// copy data
+	copy_luma
+	(
+		alpha_a, width_a, image_b, width_b * 2,
+		(width_a > width_b)?width_b:width_a,
+		(height_a > height_b)?height_b:height_a
+	);
+
+	// Extract the a_frame image info
+	*width = mlt_properties_get_int( a_props, "width" );
+	*height = mlt_properties_get_int( a_props, "height" );
+	*image = mlt_properties_get_data( a_props, "image", NULL );
+
+	return 0;
+}
+
+
+/** Matte transition processing.
+*/
+
+static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
+{
+	// Push the b_frame on to the stack
+	mlt_frame_push_frame( a_frame, b_frame );
+
+	// Push the transition method
+	mlt_frame_push_get_image( a_frame, transition_get_image );
+	
+	return a_frame;
+}
+
+/** Constructor for the filter.
+*/
+
+mlt_transition transition_matte_init( mlt_profile profile, mlt_service_type type, const char *id, char *lumafile )
+{
+	mlt_transition transition = mlt_transition_new( );
+	if ( transition != NULL )
+	{
+		// Set the methods
+		transition->process = transition_process;
+		
+		// Default factory
+		mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "factory", mlt_environment( "MLT_PRODUCER" ) );
+
+		// Inform apps and framework that this is a video only transition
+		mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );
+
+		return transition;
+	}
+	return NULL;
+}
diff --git a/src/modules/core/transition_matte.yml b/src/modules/core/transition_matte.yml
new file mode 100644
index 0000000..c3580f7
--- /dev/null
+++ b/src/modules/core/transition_matte.yml
@@ -0,0 +1,44 @@
+schema_version: 0.1
+type: transition
+identifier: matte
+title: Matte
+version: 1
+copyright: Ushodaya Enterprises Limited
+creator: Maksym Veremeyenko
+license: LGPLv2.1
+language: en
+tags:
+  - Video
+description: >
+    Replace alphachannel with data from luma channel of other track.
+
+    track A is main track, luma values of pixel in track B used as
+    alpha channel for resulted transition. it does not alter track A
+    image but just replace/create alpha channel
+notes: >
+    please note that transition automatically detect if coming frame
+    from track B has scaled or not scaled luma value. frame propertie
+    "full_luma" should indicate it.
+
+    if you need to prepare FILL and KEY (MATTE) files, you can use ffmpeg
+    to extract alpha channel from existing video.
+
+    For example:
+        # extract alpha channel and video and create matte file:
+        ffmpeg -i sg_gm_2013_clip_title.avi -vf "alphaextract" \
+            -pix_fmt yuv422p -preset placebo -crf 10 \
+            -y sg_gm_2013_clip_title.matte_scaled.mp4
+
+    because of example above provides a scaled luma output, transition
+    performs scaling (not SSE optimized yet) it from [16,235] -> [0, 255],
+    it is possible to create unscaled (full) range:
+
+        ffmpeg -i sg_gm_2013_clip_title.avi -vf "alphaextract" \
+            -pix_fmt yuvj422p -preset placebo -crf 10 \
+            -y sg_gm_2013_clip_title.matte_full.mp4
+
+    fill track could be created similar:
+
+        ffmpeg -i sg_gm_2013_clip_title.avi -pix_fmt yuv422p \
+            -preset placebo -crf 10 -y sg_gm_2013_clip_title.fill.mp4
+
-- 
1.7.7.6

------------------------------------------------------------------------------
Open source business process management suite built on Java and Eclipse
Turn processes into business applications with Bonita BPM Community Edition
Quickly connect people, data, and systems into organized workflows
Winner of BOSSIE, CODIE, OW2 and Gartner awards
http://p.sf.net/sfu/Bonitasoft
_______________________________________________
Mlt-devel mailing list
Mlt-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlt-devel

Reply via email to