1 /*
2  * transition_frei0r.c -- frei0r transition
3  * Copyright (c) 2008 Marco Gittler <g.marco@freenet.de>
4  * Copyright (C) 2009-2020 Meltytech, LLC
5  *
6  * This library 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  * This library 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 this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 #include <framework/mlt.h>
22 #include "frei0r_helper.h"
23 #include <string.h>
24 
is_opaque(uint8_t * image,int width,int height)25 static int is_opaque( uint8_t *image, int width, int height )
26 {
27 	int pixels = width * height + 1;
28 	while ( --pixels ) {
29 		if ( image[3] != 0xff ) return 0;
30 		image += 4;
31 	}
32 	return 1;
33 }
34 
transition_get_image(mlt_frame a_frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)35 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable ){
36 
37 	mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
38 	mlt_transition transition = mlt_frame_pop_service( a_frame );
39 	mlt_properties properties = MLT_TRANSITION_PROPERTIES( transition );
40 	mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
41 	mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
42 	int invert = mlt_properties_get_int( properties, "invert" );
43 	uint8_t *images[] = {NULL, NULL, NULL};
44 	int request_width = *width;
45 	int request_height = *height;
46 	int error = 0;
47 
48 	// Get the B-frame.
49 	*format = mlt_image_rgba;
50 	error = mlt_frame_get_image( b_frame, &images[1], format, width, height, 0 );
51 	if ( error ) return error;
52 
53 	if (b_frame->convert_image && (*width != request_width || *height != request_height)) {
54 		mlt_properties_set_int(b_props, "convert_image_width", request_width);
55 		mlt_properties_set_int(b_props, "convert_image_height", request_height);
56 		b_frame->convert_image(b_frame, &images[1], format, *format);
57 		*width = request_width;
58 		*height = request_height;
59 	}
60 
61 	const char *service_name = mlt_properties_get(properties, "mlt_service");
62 	int is_cairoblend = service_name && !strcmp("frei0r.cairoblend", service_name);
63 	const char *blend_mode = mlt_properties_get(b_props, CAIROBLEND_MODE_PROPERTY);
64 
65 	// An optimization for cairoblend in normal (over) mode and opaque B frame.
66 	if (is_cairoblend
67 	    && ( !mlt_properties_get( properties, "0" ) || mlt_properties_get_double( properties, "0" ) == 1.0 )
68 	    && ( !mlt_properties_get( properties, "1" ) || !strcmp( "normal", mlt_properties_get( properties, "1" ) ) )
69 	    && ( !blend_mode || !strcmp("normal", blend_mode) )
70 	    // Check if the alpha channel is entirely opaque.
71 	    && is_opaque( images[1], *width, *height ) )
72 	{
73 		if (invert)
74 			error = mlt_frame_get_image( a_frame, image, format, width, height, 0 );
75 		else
76 			*image = images[1];
77 	}
78 	else
79 	{
80 		error = mlt_frame_get_image( a_frame, &images[0], format, width, height, 0 );
81 		if ( error ) return error;
82 
83 		if (a_frame->convert_image && (*width != request_width || *height != request_height)) {
84 			mlt_properties_set_int(a_props, "convert_image_width", request_width);
85 			mlt_properties_set_int(a_props, "convert_image_height", request_height);
86 			a_frame->convert_image(a_frame, &images[0], format, *format);
87 			*width = request_width;
88 			*height = request_height;
89 		}
90 
91 		mlt_position position = mlt_transition_get_position( transition, a_frame );
92 		mlt_profile profile = mlt_service_profile( MLT_TRANSITION_SERVICE( transition ) );
93 		double time = (double) position / mlt_profile_fps( profile );
94 		int length = mlt_transition_get_length( transition );
95 
96 		// Special cairoblend handling for an override from the cairoblend_mode filter.
97 		if (is_cairoblend) {
98 			mlt_properties_set(a_props, CAIROBLEND_MODE_PROPERTY, blend_mode);
99 		}
100 
101 		process_frei0r_item( MLT_TRANSITION_SERVICE(transition), position, time, length, !invert ? a_frame : b_frame, images, width, height );
102 
103 		*width = mlt_properties_get_int( !invert ? a_props : b_props, "width" );
104 		*height = mlt_properties_get_int( !invert ? a_props : b_props, "height" );
105 		*image = mlt_properties_get_data( !invert ? a_props : b_props , "image", NULL );
106 	}
107 	return error;
108 }
109 
transition_process(mlt_transition transition,mlt_frame a_frame,mlt_frame b_frame)110 mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
111 {
112 	mlt_frame_push_service( a_frame, transition );
113 	mlt_frame_push_frame( a_frame, b_frame );
114 	mlt_frame_push_get_image( a_frame, transition_get_image );
115 	return a_frame;
116 }
117 
transition_close(mlt_transition transition)118 void transition_close( mlt_transition transition )
119 {
120 	destruct ( MLT_TRANSITION_PROPERTIES ( transition ) );
121 	transition->close = NULL;
122 	mlt_transition_close( transition );
123 }
124