]> git.sesse.net Git - mlt/blob - src/modules/core/transition_luma.c
0cd3741471f31a1a860ae9f1edd88e6e251e5e14
[mlt] / src / modules / core / transition_luma.c
1 /*
2  * transition_luma.c -- a generic dissolve/wipe processor
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include "transition_luma.h"
22 #include <framework/mlt_frame.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <string.h>
28
29 /** Luma class.
30 */
31
32 typedef struct 
33 {
34         struct mlt_transition_s parent;
35         char *filename;
36         int width;
37         int height;                                                     
38         float *bitmap;
39 }
40 transition_luma;
41
42
43 // forward declarations
44 static void transition_close( mlt_transition parent );
45
46
47 // image processing functions
48
49 static inline float smoothstep( float edge1, float edge2, float a )
50 {
51         if ( a < edge1 )
52                 return 0.0;
53
54         if ( a >= edge2 )
55                 return 1.0;
56
57         a = ( a - edge1 ) / ( edge2 - edge1 );
58
59         return ( a * a * ( 3 - 2 * a ) );
60 }
61
62 static int frame_composite_yuv( mlt_frame this, mlt_frame that, int x, int y, float weight, int *width, int *height )
63 {
64         int ret = 0;
65         int width_src = *width, height_src = *height;
66         int width_dest = *width, height_dest = *height;
67         mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
68         uint8_t *p_src, *p_dest;
69         int i, j;
70         int stride_src;
71         int stride_dest;
72         int x_src = 0, y_src = 0;
73
74         // optimization point - no work to do
75         if ( ( x < 0 && -x >= width_src ) || ( y < 0 && -y >= height_src ) )
76                 return ret;
77
78         format_src = mlt_image_yuv422;
79         format_dest = mlt_image_yuv422;
80
81         mlt_frame_get_image( this, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
82         mlt_frame_get_image( that, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
83
84         stride_src = width_src * 2;
85         stride_dest = width_dest * 2;
86         
87         // crop overlay off the left edge of frame
88         if ( x < 0 )
89         {
90                 x_src = -x;
91                 width_src -= x_src;
92                 x = 0;
93         }
94         
95         // crop overlay beyond right edge of frame
96         else if ( x + width_src > width_dest )
97                 width_src = width_dest - x;
98
99         // crop overlay off the top edge of the frame
100         if ( y < 0 )
101         {
102                 y_src = -y;
103                 height_src -= y_src;
104         }
105         // crop overlay below bottom edge of frame
106         else if ( y + height_src > height_dest )
107                 height_src = height_dest - y;
108
109         // offset pointer into overlay buffer based on cropping
110         p_src += x_src * 2 + y_src * stride_src;
111
112         // offset pointer into frame buffer based upon positive, even coordinates only!
113         p_dest += ( x < 0 ? 0 : x ) * 2 + ( y < 0 ? 0 : y ) * stride_dest;
114
115         // Get the alpha channel of the overlay
116         uint8_t *p_alpha = mlt_frame_get_alpha_mask( that );
117
118         // offset pointer into alpha channel based upon cropping
119         if ( p_alpha )
120                 p_alpha += x_src + y_src * stride_src / 2;
121
122         uint8_t *p = p_src;
123         uint8_t *q = p_dest;
124         uint8_t *o = p_dest;
125         uint8_t *z = p_alpha;
126
127         uint8_t Y;
128         uint8_t UV;
129         uint8_t a;
130         float value;
131
132         // now do the compositing only to cropped extents
133         for ( i = 0; i < height_src; i++ )
134         {
135                 p = p_src;
136                 q = p_dest;
137                 o = p_dest;
138                 z = p_alpha;
139
140                 for ( j = 0; j < width_src; j ++ )
141                 {
142                         Y = *p ++;
143                         UV = *p ++;
144                         a = ( z == NULL ) ? 255 : *z ++;
145                         value = ( weight * ( float ) a / 255.0 );
146                         *o ++ = (uint8_t)( Y * value + *q++ * ( 1 - value ) );
147                         *o ++ = (uint8_t)( UV * value + *q++ * ( 1 - value ) );
148                 }
149
150                 p_src += stride_src;
151                 p_dest += stride_dest;
152                 if ( p_alpha )
153                         p_alpha += stride_src / 2;
154         }
155
156         return ret;
157 }
158
159 /** powerful stuff
160
161     \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
162 */
163 static void luma_composite( mlt_frame a_frame, mlt_frame b_frame, int luma_width, int luma_height,
164                                                         float *luma_bitmap, float pos, float frame_delta, float softness, int field_order,
165                                                         int *width, int *height )
166 {
167         int width_src = *width, height_src = *height;
168         int width_dest = *width, height_dest = *height;
169         mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
170         uint8_t *p_src, *p_dest;
171         int i, j;
172         int stride_src;
173         int stride_dest;
174         float weight = 0;
175         int field;
176
177         format_src = mlt_image_yuv422;
178         format_dest = mlt_image_yuv422;
179
180         mlt_frame_get_image( a_frame, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
181         mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
182
183         stride_src = width_src * 2;
184         stride_dest = width_dest * 2;
185
186         // Offset the position based on which field we're looking at ...
187         float field_pos[ 2 ];
188         field_pos[ 0 ] = pos + ( ( field_order == 0 ? 1 : 0 ) * frame_delta * 0.5 );
189         field_pos[ 1 ] = pos + ( ( field_order == 0 ? 0 : 1 ) * frame_delta * 0.5 );
190
191         // adjust the position for the softness level
192         field_pos[ 0 ] *= ( 1.0 + softness );
193         field_pos[ 1 ] *= ( 1.0 + softness );
194
195         uint8_t *p;
196         uint8_t *q;
197         uint8_t *o;
198         float  *l;
199
200         uint8_t y;
201         uint8_t uv;
202         float value;
203
204         float x_diff = ( float )luma_width / ( float )*width;
205         float y_diff = ( float )luma_height / ( float )*height;
206
207         // composite using luma map
208         for ( field = 0; field < ( field_order < 0 ? 1 : 2 ); ++field )
209         {
210                 for ( i = field; i < height_src; i += ( field_order < 0 ? 1 : 2 ) )
211                 {
212                         p = &p_src[ i * stride_src ];
213                         q = &p_dest[ i * stride_dest ];
214                         o = &p_dest[ i * stride_dest ];
215                         l = &luma_bitmap[ ( int )( ( float )i * y_diff ) * luma_width ];
216
217                         for ( j = 0; j < width_src; j ++ )
218                         {
219                                 y = *p ++;
220                                 uv = *p ++;
221                 weight = l[ ( int )( ( float )j * x_diff ) ];
222                                 value = smoothstep( weight, weight + softness, field_pos[ field ] );
223
224                                 *o ++ = (uint8_t)( y * value + *q++ * ( 1 - value ) );
225                                 *o ++ = (uint8_t)( uv * value + *q++ * ( 1 - value ) );
226                         }
227                 }
228         }
229 }
230
231 /** Get the image.
232 */
233
234 static int transition_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
235 {
236         // Get the properties of the a frame
237         mlt_properties a_props = mlt_frame_properties( this );
238
239         // Get the b frame from the stack
240         mlt_frame b_frame = mlt_frame_pop_frame( this );
241
242         // Get the properties of the b frame
243         mlt_properties b_props = mlt_frame_properties( b_frame );
244
245         // Arbitrary composite defaults
246         float frame_delta = 1 / mlt_properties_get_double( b_props, "fps" );
247         float mix = mlt_properties_get_double( b_props, "image.mix" );
248         int luma_width = mlt_properties_get_int( b_props, "luma.width" );
249         int luma_height = mlt_properties_get_int( b_props, "luma.height" );
250         float *luma_bitmap = mlt_properties_get_data( b_props, "luma.bitmap", NULL );
251         float luma_softness = mlt_properties_get_double( b_props, "luma.softness" );
252         int progressive = mlt_properties_get_int( b_props, "progressive" );
253         int top_field_first =  mlt_properties_get_int( b_props, "top_field_first" );
254         int reverse = mlt_properties_get_int( b_props, "luma.reverse" );
255
256         // Honour the reverse here
257         mix = reverse ? 1 - mix : mix;
258
259         // Ensure we get scaling on the b_frame
260         mlt_properties_set( b_props, "rescale.interp", "nearest" );
261
262         if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
263                 // Composite the frames using a luma map
264                 luma_composite( this, b_frame, luma_width, luma_height, luma_bitmap, mix, frame_delta,
265                         luma_softness, progressive > 0 ? -1 : top_field_first, width, height );
266         else
267                 // Dissolve the frames using the time offset for mix value
268                 frame_composite_yuv( this, b_frame, 0, 0, mix, width, height );
269
270         // Extract the a_frame image info
271         *width = mlt_properties_get_int( a_props, "width" );
272         *height = mlt_properties_get_int( a_props, "height" );
273         *image = mlt_properties_get_data( a_props, "image", NULL );
274
275         return 0;
276 }
277
278 /** Load the luma map from PGM stream.
279 */
280
281 static void luma_read_pgm( FILE *f, float **map, int *width, int *height )
282 {
283         uint8_t *data = NULL;
284         while (1)
285         {
286                 char line[128];
287                 int i = 2;
288                 int maxval;
289                 int bpp;
290                 float *p;
291                 
292                 line[127] = '\0';
293
294                 // get the magic code
295                 if ( fgets( line, 127, f ) == NULL )
296                         break;
297                 if ( line[0] != 'P' || line[1] != '5' )
298                         break;
299
300                 // skip white space and see if a new line must be fetched
301                 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
302                 if ( line[i] == '\0' && fgets( line, 127, f ) == NULL )
303                         break;
304
305                 // get the dimensions
306                 if ( line[0] == 'P' )
307                         i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
308                 else
309                         i = sscanf( line, "%d %d %d", width, height, &maxval );
310
311                 // get the height value, if not yet
312                 if ( i < 2 )
313                 {
314                         if ( fgets( line, 127, f ) == NULL )
315                                 break;
316                         i = sscanf( line, "%d", height );
317                         if ( i == 0 )
318                                 break;
319                         else
320                                 i = 2;
321                 }
322
323                 // get the maximum gray value, if not yet
324                 if ( i < 3 )
325                 {
326                         if ( fgets( line, 127, f ) == NULL )
327                                 break;
328                         i = sscanf( line, "%d", &maxval );
329                         if ( i == 0 )
330                                 break;
331                 }
332
333                 // determine if this is one or two bytes per pixel
334                 bpp = maxval > 255 ? 2 : 1;
335                         // allocate temporary storage for the raw data
336                 data = malloc( *width * *height * bpp );
337                 if ( data == NULL )
338                         break;
339
340                 // read the raw data
341                 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
342                         break;
343                 
344                 // allocate the luma bitmap
345                 *map =  p = (float*) malloc( *width * *height * sizeof( float ) );
346                 if ( *map == NULL )
347                         break;
348
349                 // proces the raw data into the luma bitmap
350                 for ( i = 0; i < *width * *height * bpp; i += bpp )
351                 {
352                         if ( bpp == 1 )
353                                 *p++ = (float) data[ i ] / (float) maxval;
354                         else
355                                 *p++ = (float) ( ( data[ i ] << 8 ) + data[ i+1 ] ) / (float) maxval;
356                 }
357
358                 break;
359         }
360                 
361         if ( data != NULL )
362                 free( data );
363 }
364
365
366 /** Luma transition processing.
367 */
368
369 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
370 {
371         transition_luma *this = (transition_luma*) transition->child;
372
373         // Get the properties of the transition
374         mlt_properties properties = mlt_transition_properties( transition );
375         
376         // Get the properties of the b frame
377         mlt_properties b_props = mlt_frame_properties( b_frame );
378
379         // If the filename property changed, reload the map
380         char *luma_file = mlt_properties_get( properties, "resource" );
381         if ( luma_file != NULL && ( this->filename == NULL || ( this->filename && strcmp( luma_file, this->filename ) ) ) )
382         {
383                 FILE *pipe;
384                 
385                 free( this->filename );
386                 this->filename = strdup( luma_file );
387                 pipe = fopen( luma_file, "r" );
388                 if ( pipe != NULL )
389                 {
390                         free( this->bitmap );
391                         luma_read_pgm( pipe, &this->bitmap, &this->width, &this->height );
392                         fclose( pipe );
393                 }
394         }
395
396         // Determine the time position of this frame in the transition duration
397         mlt_position in = mlt_transition_get_in( transition );
398         mlt_position out = mlt_transition_get_out( transition );
399         mlt_position time = mlt_frame_get_position( b_frame );
400         float pos = ( float )( time - in ) / ( float )( out - in + 1 );
401         
402         // Set the b frame properties
403         mlt_properties_set_double( b_props, "image.mix", pos );
404         mlt_properties_set_int( b_props, "luma.width", this->width );
405         mlt_properties_set_int( b_props, "luma.height", this->height );
406         mlt_properties_set_data( b_props, "luma.bitmap", this->bitmap, 0, NULL, NULL );
407         mlt_properties_set_int( b_props, "luma.reverse", mlt_properties_get_int( properties, "reverse" ) );
408         mlt_properties_set_double( b_props, "luma.softness", mlt_properties_get_double( properties, "softness" ) );
409
410         mlt_frame_push_get_image( a_frame, transition_get_image );
411         mlt_frame_push_frame( a_frame, b_frame );
412
413         return a_frame;
414 }
415
416 /** Constructor for the filter.
417 */
418
419 mlt_transition transition_luma_init( char *lumafile )
420 {
421         transition_luma *this = calloc( sizeof( transition_luma ), 1 );
422         if ( this != NULL )
423         {
424                 mlt_transition transition = &this->parent;
425                 mlt_transition_init( transition, this );
426                 transition->process = transition_process;
427                 transition->close = transition_close;
428                 mlt_properties_set( mlt_transition_properties( transition ), "resource", lumafile );
429                 return &this->parent;
430         }
431         return NULL;
432 }
433
434 /** Close the transition.
435 */
436
437 static void transition_close( mlt_transition parent )
438 {
439         transition_luma *this = (transition_luma*) parent->child;
440         free( this->bitmap );
441         free( this->filename );
442         free( this );
443 }
444