]> git.sesse.net Git - mlt/blob - src/modules/plus/transition_affine.c
Add always_active property to affine transition.
[mlt] / src / modules / plus / transition_affine.c
1 /*
2  * transition_affine.c -- affine transformations
3  * Copyright (C) 2003-2010 Ushodaya Enterprises Limited
4  * Author: Charles Yates <charles.yates@pandora.be>
5  * Author: Dan Dennedy <dan@dennedy.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <framework/mlt_transition.h>
23 #include <framework/mlt.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include <string.h>
29 #include <math.h>
30
31 #include "interp.h"
32
33 /** Calculate real geometry.
34 */
35
36 static void geometry_calculate( mlt_transition this, const char *store, struct mlt_geometry_item_s *output, float position )
37 {
38         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
39         mlt_geometry geometry = mlt_properties_get_data( properties, store, NULL );
40         int mirror_off = mlt_properties_get_int( properties, "mirror_off" );
41         int repeat_off = mlt_properties_get_int( properties, "repeat_off" );
42         int length = mlt_geometry_get_length( geometry );
43
44         // Allow wrapping
45         if ( !repeat_off && position >= length && length != 0 )
46         {
47                 int section = position / length;
48                 position -= section * length;
49                 if ( !mirror_off && section % 2 == 1 )
50                         position = length - position;
51         }
52
53         // Fetch the key for the position
54         mlt_geometry_fetch( geometry, output, position );
55 }
56
57
58 static mlt_geometry transition_parse_keys( mlt_transition this, const char *name, const char *store, int normalised_width, int normalised_height )
59 {
60         // Get the properties of the transition
61         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
62
63         // Try to fetch it first
64         mlt_geometry geometry = mlt_properties_get_data( properties, store, NULL );
65
66         // Get the in and out position
67         int always_active = mlt_properties_get_int( properties, "always_active" );
68         mlt_position in = mlt_transition_get_in( this );
69         mlt_position out = !always_active ? mlt_transition_get_out( this ) : -1;
70
71         // Determine length and obtain cycle
72         int length = out - in + 1;
73         double cycle = mlt_properties_get_double( properties, "cycle" );
74
75         // Allow a geometry repeat cycle
76         if ( cycle >= 1 )
77                 length = cycle;
78         else if ( cycle > 0 )
79                 length *= cycle;
80
81         if ( geometry == NULL )
82         {
83                 // Get the new style geometry string
84                 char *property = mlt_properties_get( properties, name );
85
86                 // Create an empty geometries object
87                 geometry = mlt_geometry_init( );
88
89                 // Parse the geometry if we have one
90                 mlt_geometry_parse( geometry, property, length, normalised_width, normalised_height );
91
92                 // Store it
93                 mlt_properties_set_data( properties, store, geometry, 0, ( mlt_destructor )mlt_geometry_close, NULL );
94         }
95         else
96         {
97                 // Check for updates and refresh if necessary
98                 mlt_geometry_refresh( geometry, mlt_properties_get( properties, name ), length, normalised_width, normalised_height );
99         }
100
101         return geometry;
102 }
103
104 static mlt_geometry composite_calculate( mlt_transition this, struct mlt_geometry_item_s *result, int nw, int nh, float position )
105 {
106         // Structures for geometry
107         mlt_geometry start = transition_parse_keys( this, "geometry", "geometries", nw, nh );
108
109         // Do the calculation
110         geometry_calculate( this, "geometries", result, position );
111
112         return start;
113 }
114
115 static inline float composite_calculate_key( mlt_transition this, const char *name, const char *store, int norm, float position )
116 {
117         // Struct for the result
118         struct mlt_geometry_item_s result;
119
120         // Structures for geometry
121         transition_parse_keys( this, name, store, norm, 0 );
122
123         // Do the calculation
124         geometry_calculate( this, store, &result, position );
125
126         return result.x;
127 }
128
129 typedef struct
130 {
131         float matrix[3][3];
132 }
133 affine_t;
134
135 static void affine_init( float this[3][3] )
136 {
137         this[0][0] = 1;
138         this[0][1] = 0;
139         this[0][2] = 0;
140         this[1][0] = 0;
141         this[1][1] = 1;
142         this[1][2] = 0;
143         this[2][0] = 0;
144         this[2][1] = 0;
145         this[2][2] = 1;
146 }
147
148 // Multiply two this affine transform with that
149 static void affine_multiply( float this[3][3], float that[3][3] )
150 {
151         float output[3][3];
152         int i;
153         int j;
154
155         for ( i = 0; i < 3; i ++ )
156                 for ( j = 0; j < 3; j ++ )
157                         output[i][j] = this[i][0] * that[j][0] + this[i][1] * that[j][1] + this[i][2] * that[j][2];
158
159         this[0][0] = output[0][0];
160         this[0][1] = output[0][1];
161         this[0][2] = output[0][2];
162         this[1][0] = output[1][0];
163         this[1][1] = output[1][1];
164         this[1][2] = output[1][2];
165         this[2][0] = output[2][0];
166         this[2][1] = output[2][1];
167         this[2][2] = output[2][2];
168 }
169
170 // Rotate by a given angle
171 static void affine_rotate_x( float this[3][3], float angle )
172 {
173         float affine[3][3];
174         affine[0][0] = cos( angle * M_PI / 180 );
175         affine[0][1] = 0 - sin( angle * M_PI / 180 );
176         affine[0][2] = 0;
177         affine[1][0] = sin( angle * M_PI / 180 );
178         affine[1][1] = cos( angle * M_PI / 180 );
179         affine[1][2] = 0;
180         affine[2][0] = 0;
181         affine[2][1] = 0;
182         affine[2][2] = 1;
183         affine_multiply( this, affine );
184 }
185
186 static void affine_rotate_y( float this[3][3], float angle )
187 {
188         float affine[3][3];
189         affine[0][0] = cos( angle * M_PI / 180 );
190         affine[0][1] = 0;
191         affine[0][2] = 0 - sin( angle * M_PI / 180 );
192         affine[1][0] = 0;
193         affine[1][1] = 1;
194         affine[1][2] = 0;
195         affine[2][0] = sin( angle * M_PI / 180 );
196         affine[2][1] = 0;
197         affine[2][2] = cos( angle * M_PI / 180 );
198         affine_multiply( this, affine );
199 }
200
201 static void affine_rotate_z( float this[3][3], float angle )
202 {
203         float affine[3][3];
204         affine[0][0] = 1;
205         affine[0][1] = 0;
206         affine[0][2] = 0;
207         affine[1][0] = 0;
208         affine[1][1] = cos( angle * M_PI / 180 );
209         affine[1][2] = sin( angle * M_PI / 180 );
210         affine[2][0] = 0;
211         affine[2][1] = - sin( angle * M_PI / 180 );
212         affine[2][2] = cos( angle * M_PI / 180 );
213         affine_multiply( this, affine );
214 }
215
216 static void affine_scale( float this[3][3], float sx, float sy )
217 {
218         float affine[3][3];
219         affine[0][0] = sx;
220         affine[0][1] = 0;
221         affine[0][2] = 0;
222         affine[1][0] = 0;
223         affine[1][1] = sy;
224         affine[1][2] = 0;
225         affine[2][0] = 0;
226         affine[2][1] = 0;
227         affine[2][2] = 1;
228         affine_multiply( this, affine );
229 }
230
231 // Shear by a given value
232 static void affine_shear( float this[3][3], float shear_x, float shear_y, float shear_z )
233 {
234         float affine[3][3];
235         affine[0][0] = 1;
236         affine[0][1] = tan( shear_x * M_PI / 180 );
237         affine[0][2] = 0;
238         affine[1][0] = tan( shear_y * M_PI / 180 );
239         affine[1][1] = 1;
240         affine[1][2] = tan( shear_z * M_PI / 180 );
241         affine[2][0] = 0;
242         affine[2][1] = 0;
243         affine[2][2] = 1;
244         affine_multiply( this, affine );
245 }
246
247 static void affine_offset( float this[3][3], float x, float y )
248 {
249         this[0][2] += x;
250         this[1][2] += y;
251 }
252
253 // Obtain the mapped x coordinate of the input
254 static inline double MapX( float this[3][3], float x, float y )
255 {
256         return this[0][0] * x + this[0][1] * y + this[0][2];
257 }
258
259 // Obtain the mapped y coordinate of the input
260 static inline double MapY( float this[3][3], float x, float y )
261 {
262         return this[1][0] * x + this[1][1] * y + this[1][2];
263 }
264
265 static inline double MapZ( float this[3][3], float x, float y )
266 {
267         return this[2][0] * x + this[2][1] * y + this[2][2];
268 }
269
270 #define MAX( x, y ) x > y ? x : y
271 #define MIN( x, y ) x < y ? x : y
272
273 static void affine_max_output( float this[3][3], float *w, float *h, float dz, float max_width, float max_height )
274 {
275         int tlx = MapX( this, -max_width,  max_height ) / dz;
276         int tly = MapY( this, -max_width,  max_height ) / dz;
277         int trx = MapX( this,  max_width,  max_height ) / dz;
278         int try = MapY( this,  max_width,  max_height ) / dz;
279         int blx = MapX( this, -max_width, -max_height ) / dz;
280         int bly = MapY( this, -max_width, -max_height ) / dz;
281         int brx = MapX( this,  max_width, -max_height ) / dz;
282         int bry = MapY( this,  max_width, -max_height ) / dz;
283
284         int max_x;
285         int max_y;
286         int min_x;
287         int min_y;
288
289         max_x = MAX( tlx, trx );
290         max_x = MAX( max_x, blx );
291         max_x = MAX( max_x, brx );
292
293         min_x = MIN( tlx, trx );
294         min_x = MIN( min_x, blx );
295         min_x = MIN( min_x, brx );
296
297         max_y = MAX( tly, try );
298         max_y = MAX( max_y, bly );
299         max_y = MAX( max_y, bry );
300
301         min_y = MIN( tly, try );
302         min_y = MIN( min_y, bly );
303         min_y = MIN( min_y, bry );
304
305         *w = ( float )( max_x - min_x + 1 ) / max_width / 2.0;
306         *h = ( float )( max_y - min_y + 1 ) / max_height / 2.0;
307 }
308
309 #define IN_RANGE( v, r )        ( v >= - r / 2 && v < r / 2 )
310
311 static inline void get_affine( affine_t *affine, mlt_transition this, float position )
312 {
313         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
314         int keyed = mlt_properties_get_int( properties, "keyed" );
315
316         if ( keyed == 0 )
317         {
318                 float fix_rotate_x = mlt_properties_get_double( properties, "fix_rotate_x" );
319                 float fix_rotate_y = mlt_properties_get_double( properties, "fix_rotate_y" );
320                 float fix_rotate_z = mlt_properties_get_double( properties, "fix_rotate_z" );
321                 float rotate_x = mlt_properties_get_double( properties, "rotate_x" );
322                 float rotate_y = mlt_properties_get_double( properties, "rotate_y" );
323                 float rotate_z = mlt_properties_get_double( properties, "rotate_z" );
324                 float fix_shear_x = mlt_properties_get_double( properties, "fix_shear_x" );
325                 float fix_shear_y = mlt_properties_get_double( properties, "fix_shear_y" );
326                 float fix_shear_z = mlt_properties_get_double( properties, "fix_shear_z" );
327                 float shear_x = mlt_properties_get_double( properties, "shear_x" );
328                 float shear_y = mlt_properties_get_double( properties, "shear_y" );
329                 float shear_z = mlt_properties_get_double( properties, "shear_z" );
330                 float ox = mlt_properties_get_double( properties, "ox" );
331                 float oy = mlt_properties_get_double( properties, "oy" );
332
333                 affine_rotate_x( affine->matrix, fix_rotate_x + rotate_x * position );
334                 affine_rotate_y( affine->matrix, fix_rotate_y + rotate_y * position );
335                 affine_rotate_z( affine->matrix, fix_rotate_z + rotate_z * position );
336                 affine_shear( affine->matrix,
337                                           fix_shear_x + shear_x * position,
338                                           fix_shear_y + shear_y * position,
339                                           fix_shear_z + shear_z * position );
340                 affine_offset( affine->matrix, ox, oy );
341         }
342         else
343         {
344                 float rotate_x = composite_calculate_key( this, "rotate_x", "rotate_x_info", 360, position );
345                 float rotate_y = composite_calculate_key( this, "rotate_y", "rotate_y_info", 360, position );
346                 float rotate_z = composite_calculate_key( this, "rotate_z", "rotate_z_info", 360, position );
347                 float shear_x = composite_calculate_key( this, "shear_x", "shear_x_info", 360, position );
348                 float shear_y = composite_calculate_key( this, "shear_y", "shear_y_info", 360, position );
349                 float shear_z = composite_calculate_key( this, "shear_z", "shear_z_info", 360, position );
350
351                 affine_rotate_x( affine->matrix, rotate_x );
352                 affine_rotate_y( affine->matrix, rotate_y );
353                 affine_rotate_z( affine->matrix, rotate_z );
354                 affine_shear( affine->matrix, shear_x, shear_y, shear_z );
355         }
356 }
357
358 /** Get the image.
359 */
360
361 static int transition_get_image( mlt_frame a_frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
362 {
363         // Get the b frame from the stack
364         mlt_frame b_frame = mlt_frame_pop_frame( a_frame );
365
366         // Get the transition object
367         mlt_transition this = mlt_frame_pop_service( a_frame );
368
369         // Get the properties of the transition
370         mlt_properties properties = MLT_TRANSITION_PROPERTIES( this );
371
372         // Get the properties of the a frame
373         mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
374
375         // Get the properties of the b frame
376         mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
377
378         // Image, format, width, height and image for the b frame
379         uint8_t *b_image = NULL;
380         mlt_image_format b_format = mlt_image_rgb24a;
381         int b_width;
382         int b_height;
383
384         // Get the unique name to retrieve the frame position
385         char *name = mlt_properties_get( properties, "_unique_id" );
386
387         // Assign the current position to the name
388         mlt_position position =  mlt_properties_get_position( a_props, name );
389         
390         mlt_properties props = mlt_properties_get_data( b_props, "_producer", NULL );
391         int always_active = mlt_properties_get_int( properties, "always_active" );
392
393         mlt_position in = !always_active ? mlt_properties_get_position( properties, "in" ) : mlt_properties_get_int( props, "in" );
394         mlt_position out = !always_active ? mlt_properties_get_position( properties, "out" ) : mlt_properties_get_int( props, "out" );
395         int mirror = mlt_properties_get_position( properties, "mirror" );
396         int length = out - in + 1;
397
398         // Obtain the normalised width and height from the a_frame
399         int normalised_width = mlt_properties_get_int( a_props, "normalised_width" );
400         int normalised_height = mlt_properties_get_int( a_props, "normalised_height" );
401
402         double consumer_ar = mlt_properties_get_double( a_props, "consumer_aspect_ratio" );
403
404         // Structures for geometry
405         struct mlt_geometry_item_s result;
406
407         if ( mirror && position > length / 2 )
408                 position = abs( position - length );
409
410         // Fetch the a frame image
411         *format = mlt_image_rgb24a;
412         mlt_frame_get_image( a_frame, image, format, width, height, 1 );
413
414         // Calculate the region now
415         composite_calculate( this, &result, normalised_width, normalised_height, ( float )position );
416
417         // Fetch the b frame image
418         result.w = ( result.w * *width / normalised_width );
419         result.h = ( result.h * *height / normalised_height );
420         result.x = ( result.x * *width / normalised_width );
421         result.y = ( result.y * *height / normalised_height );
422
423         // Request full resolution of b frame image.
424         b_width = mlt_properties_get_int( b_props, "real_width" );
425         b_height = mlt_properties_get_int( b_props, "real_height" );
426         mlt_properties_set_int( b_props, "rescale_width", b_width );
427         mlt_properties_set_int( b_props, "rescale_height", b_height );
428
429         // Suppress padding and aspect normalization.
430         char *interps = mlt_properties_get( b_props, "rescale.interp" );
431         if ( interps )
432                 interps = strdup( interps );
433         mlt_properties_set( b_props, "rescale.interp", "none" );
434         if ( mlt_properties_get_double( b_props, "aspect_ratio" ) == 0.0 )
435                 mlt_properties_set_double( b_props, "aspect_ratio", consumer_ar );
436
437         // This is not a field-aware transform.
438         mlt_properties_set_int( b_props, "consumer_deinterlace", 1 );
439
440         mlt_frame_get_image( b_frame, &b_image, &b_format, &b_width, &b_height, 0 );
441
442         // Check that both images are of the correct format and process
443         if ( *format == mlt_image_rgb24a && b_format == mlt_image_rgb24a )
444         {
445                 float x, y;
446                 float dx, dy;
447                 float dz;
448                 float sw, sh;
449                 uint8_t *p = *image;
450
451                 // Get values from the transition
452                 float scale_x = mlt_properties_get_double( properties, "scale_x" );
453                 float scale_y = mlt_properties_get_double( properties, "scale_y" );
454                 int scale = mlt_properties_get_int( properties, "scale" );
455                 float geom_scale_x = (float) b_width / result.w;
456                 float geom_scale_y = (float) b_height / result.h;
457                 float cx = result.x + result.w / 2.0;
458                 float cy = result.y + result.h / 2.0;
459                 float lower_x = - cx;
460                 float lower_y = - cy;
461                 float x_offset = (float) b_width / 2.0;
462                 float y_offset = (float) b_height / 2.0;
463                 affine_t affine;
464                 interpp interp = interpBL_b32;
465                 int i, j; // loop counters
466
467                 affine_init( affine.matrix );
468
469                 // Compute the affine transform
470                 get_affine( &affine, this, ( float )position );
471                 dz = MapZ( affine.matrix, 0, 0 );
472                 if ( ( int )abs( dz * 1000 ) < 25 )
473                 {
474                         if ( interps )
475                                 free( interps );
476                         return 0;
477                 }
478
479                 // Factor scaling into the transformation based on output resolution.
480                 if ( mlt_properties_get_int( properties, "distort" ) )
481                 {
482                         scale_x = geom_scale_x * ( scale_x == 0 ? 1 : scale_x );
483                         scale_y = geom_scale_y * ( scale_y == 0 ? 1 : scale_y );
484                 }
485                 else
486                 {
487                         // Determine scale with respect to aspect ratio.
488                         double consumer_dar = consumer_ar * normalised_width / normalised_height;
489                         double b_ar = mlt_properties_get_double( b_props, "aspect_ratio" );
490                         double b_dar = b_ar * b_width / b_height;
491                         
492                         if ( b_dar > consumer_dar )
493                         {
494                                 scale_x = geom_scale_x * ( scale_x == 0 ? 1 : scale_x );
495                                 scale_y = geom_scale_x * ( scale_y == 0 ? 1 : scale_y );
496                         }
497                         else
498                         {
499                                 scale_x = geom_scale_y * ( scale_x == 0 ? 1 : scale_x );
500                                 scale_y = geom_scale_y * ( scale_y == 0 ? 1 : scale_y );
501                         }
502                         scale_x *= consumer_ar / b_ar;
503                 }
504                 if ( scale )
505                 {
506                         affine_max_output( affine.matrix, &sw, &sh, dz, *width, *height );
507                         affine_scale( affine.matrix, sw * MIN( geom_scale_x, geom_scale_y ), sh * MIN( geom_scale_x, geom_scale_y ) );
508                 }
509                 else if ( scale_x != 0 && scale_y != 0 )
510                 {
511                         affine_scale( affine.matrix, scale_x, scale_y );
512                 }
513
514                 // Set the interpolation function
515                 if ( interps == NULL || strcmp( interps, "nearest" ) == 0 || strcmp( interps, "neighbor" ) == 0 )
516                         interp = interpNN_b32;
517                 else if ( strcmp( interps, "tiles" ) == 0 || strcmp( interps, "fast_bilinear" ) == 0 )
518                         interp = interpNN_b32;
519                 else if ( strcmp( interps, "bilinear" ) == 0 )
520                         interp = interpBL_b32;
521                 else if ( strcmp( interps, "bicubic" ) == 0 )
522                         interp = interpBC_b32;
523                  // TODO: lanczos 8x8
524                 else if ( strcmp( interps, "hyper" ) == 0 || strcmp( interps, "sinc" ) == 0 || strcmp( interps, "lanczos" ) == 0 )
525                         interp = interpBC_b32;
526                 else if ( strcmp( interps, "spline" ) == 0 ) // TODO: spline 4x4 or 6x6
527                         interp = interpBC_b32;
528
529                 // Do the transform with interpolation
530                 for ( i = 0, y = lower_y; i < *height; i++, y++ )
531                 {
532                         for ( j = 0, x = lower_x; j < *width; j++, x++ )
533                         {
534                                 dx = MapX( affine.matrix, x, y ) / dz + x_offset;
535                                 dy = MapY( affine.matrix, x, y ) / dz + y_offset;
536                                 if ( dx >= 0 && dx < (b_width - 1) && dy >=0 && dy < (b_height - 1) )
537                                         interp( b_image, b_width, b_height, dx, dy, result.mix/100.0, p );
538                                 p += 4;
539                         }
540                 }
541         }
542         if ( interps )
543                 free( interps );
544
545         return 0;
546 }
547
548 /** Affine transition processing.
549 */
550
551 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
552 {
553         // Get a unique name to store the frame position
554         char *name = mlt_properties_get( MLT_TRANSITION_PROPERTIES( transition ), "_unique_id" );
555
556         // Assign the current position to the name
557         mlt_properties a_props = MLT_FRAME_PROPERTIES( a_frame );
558         mlt_properties_set_position( a_props, name, mlt_frame_get_position( a_frame ) - mlt_transition_get_in( transition ) );
559
560         // Push the transition on to the frame
561         mlt_frame_push_service( a_frame, transition );
562
563         // Push the b_frame on to the stack
564         mlt_frame_push_frame( a_frame, b_frame );
565
566         // Push the transition method
567         mlt_frame_push_get_image( a_frame, transition_get_image );
568
569         return a_frame;
570 }
571
572 /** Constructor for the filter.
573 */
574
575 mlt_transition transition_affine_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
576 {
577         mlt_transition transition = mlt_transition_new( );
578         if ( transition != NULL )
579         {
580                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "distort", 0 );
581                 mlt_properties_set( MLT_TRANSITION_PROPERTIES( transition ), "geometry", "0,0:100%x100%" );
582                 // Inform apps and framework that this is a video only transition
583                 mlt_properties_set_int( MLT_TRANSITION_PROPERTIES( transition ), "_transition_type", 1 );
584                 transition->process = transition_process;
585         }
586         return transition;
587 }