]> git.sesse.net Git - mlt/blob - src/modules/core/transition_luma.c
a518e0666d7474e42ee6fd9b41cc2b6e35002567
[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         int width;
36         int height;
37         uint16_t *bitmap;
38 }
39 transition_luma;
40
41
42 // forward declarations
43 static void transition_close( mlt_transition parent );
44
45 /** Calculate the position for this frame.
46 */
47
48 static float position_calculate( mlt_transition this, mlt_frame frame )
49 {
50         // Get the in and out position
51         mlt_position in = mlt_transition_get_in( this );
52         mlt_position out = mlt_transition_get_out( this );
53
54         // Get the position of the frame
55         mlt_position position = mlt_frame_get_position( frame );
56
57         // Now do the calcs
58         return ( float )( position - in ) / ( float )( out - in + 1 );
59 }
60
61 /** Calculate the field delta for this frame - position between two frames.
62 */
63
64 static float delta_calculate( mlt_transition this, mlt_frame frame )
65 {
66         // Get the in and out position
67         mlt_position in = mlt_transition_get_in( this );
68         mlt_position out = mlt_transition_get_out( this );
69
70         // Get the position of the frame
71         mlt_position position = mlt_frame_get_position( frame );
72
73         // Now do the calcs
74         float x = ( float )( position - in ) / ( float )( out - in + 1 );
75         float y = ( float )( position + 1 - in ) / ( float )( out - in + 1 );
76
77         return ( y - x ) / 2.0;
78 }
79
80 static inline int dissolve_yuv( mlt_frame this, mlt_frame that, float weight, int width, int height )
81 {
82         int ret = 0;
83         int width_src = width, height_src = height;
84         mlt_image_format format = mlt_image_yuv422;
85         uint8_t *p_src, *p_dest;
86         uint8_t *p;
87         uint8_t *limit;
88
89         int32_t weigh = weight * ( 1 << 16 );
90         int32_t weigh_complement = ( 1 - weight ) * ( 1 << 16 );
91
92         mlt_frame_get_image( this, &p_dest, &format, &width, &height, 1 );
93         mlt_frame_get_image( that, &p_src, &format, &width_src, &height_src, 0 );
94
95         p = p_dest;
96         limit = p_dest + height_src * width_src * 2;
97
98         while ( p < limit )
99         {
100                 *p_dest++ = ( *p_src++ * weigh + *p++ * weigh_complement ) >> 16;
101                 *p_dest++ = ( *p_src++ * weigh + *p++ * weigh_complement ) >> 16;
102         }
103
104         return ret;
105 }
106
107 // image processing functions
108
109 static uint32_t smoothstep( int32_t edge1, int32_t edge2, uint32_t a )
110 {
111         if ( a < edge1 )
112                 return 0;
113
114         if ( a >= edge2 )
115                 return 0x10000;
116
117         a = ( ( a - edge1 ) << 16 ) / ( edge2 - edge1 );
118
119         return ( ( ( a * a ) >> 16 )  * ( ( 3 << 16 ) - ( 2 * a ) ) ) >> 16;
120 }
121
122 /** powerful stuff
123
124     \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
125 */
126 static void luma_composite( mlt_frame a_frame, mlt_frame b_frame, int luma_width, int luma_height,
127                                                         uint16_t *luma_bitmap, float pos, float frame_delta, float softness, int field_order,
128                                                         int *width, int *height )
129 {
130         int width_src = *width, height_src = *height;
131         int width_dest = *width, height_dest = *height;
132         mlt_image_format format_src = mlt_image_yuv422, format_dest = mlt_image_yuv422;
133         uint8_t *p_src, *p_dest;
134         int i, j;
135         int stride_src;
136         int stride_dest;
137         uint16_t weight = 0;
138
139         format_src = mlt_image_yuv422;
140         format_dest = mlt_image_yuv422;
141
142         mlt_frame_get_image( a_frame, &p_dest, &format_dest, &width_dest, &height_dest, 1 );
143         mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 );
144
145         stride_src = width_src * 2;
146         stride_dest = width_dest * 2;
147
148         // Offset the position based on which field we're looking at ...
149         int32_t field_pos[ 2 ];
150         field_pos[ 0 ] = ( pos + ( ( field_order == 0 ? 1 : 0 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
151         field_pos[ 1 ] = ( pos + ( ( field_order == 0 ? 0 : 1 ) * frame_delta * 0.5 ) ) * ( 1 << 16 ) * ( 1.0 + softness );
152
153         register uint8_t *p;
154         register uint8_t *q;
155         register uint8_t *o;
156         uint16_t  *l;
157
158         uint32_t value;
159
160         int32_t x_diff = ( luma_width << 16 ) / *width;
161         int32_t y_diff = ( luma_height << 16 ) / *height;
162         int32_t x_offset = 0;
163         int32_t y_offset = 0;
164         uint8_t *p_row;
165         uint8_t *q_row;
166
167         int32_t i_softness = softness * ( 1 << 16 );
168
169         int field_count = field_order <= 0 ? 1 : 2;
170         int field_stride_src = field_count * stride_src;
171         int field_stride_dest = field_count * stride_dest;
172
173         int field = 0;
174
175         // composite using luma map
176         while ( field < field_count )
177         {
178                 p_row = p_src + field * stride_src;
179                 q_row = p_dest + field * stride_dest;
180                 y_offset = ( field * luma_width ) << 16;
181                 i = field;
182
183                 while ( i < height_src )
184                 {
185                         p = p_row;
186                         q = q_row;
187                         o = q;
188                         l = luma_bitmap + ( y_offset >> 16 ) * ( luma_width * field_count );
189                         x_offset = 0;
190                         j = width_src;
191
192                         while( j -- )
193                         {
194                 weight = l[ x_offset >> 16 ];
195                                 value = smoothstep( weight, i_softness + weight, field_pos[ field ] );
196                                 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
197                                 *o ++ = ( *p ++ * value + *q++ * ( ( 1 << 16 ) - value ) ) >> 16;
198                                 x_offset += x_diff;
199                         }
200
201                         y_offset += y_diff;
202                         i += field_count;
203                         p_row += field_stride_src;
204                         q_row += field_stride_dest;
205                 }
206
207                 field ++;
208         }
209 }
210
211 /** Get the image.
212 */
213
214 static int transition_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
215 {
216         // Get the properties of the a frame
217         mlt_properties a_props = mlt_frame_properties( this );
218
219         // Get the b frame from the stack
220         mlt_frame b_frame = mlt_frame_pop_frame( this );
221
222         // Get the properties of the b frame
223         mlt_properties b_props = mlt_frame_properties( b_frame );
224
225         // Arbitrary composite defaults
226         float mix = mlt_properties_get_double( b_props, "image.mix" );
227         float frame_delta = mlt_properties_get_double( b_props, "luma.delta" );
228         int luma_width = mlt_properties_get_int( b_props, "luma.width" );
229         int luma_height = mlt_properties_get_int( b_props, "luma.height" );
230         uint16_t *luma_bitmap = mlt_properties_get_data( b_props, "luma.bitmap", NULL );
231         float luma_softness = mlt_properties_get_double( b_props, "luma.softness" );
232         int progressive = mlt_properties_get_int( b_props, "progressive" ) ||
233                         mlt_properties_get_int( a_props, "consumer_progressive" ) ||
234                         mlt_properties_get_int( b_props, "luma.progressive" );
235
236         int top_field_first =  mlt_properties_get_int( b_props, "top_field_first" );
237         int reverse = mlt_properties_get_int( b_props, "luma.reverse" );
238
239         // Since we are the consumer of the b_frame, we must pass along this
240         // consumer property from the a_frame
241         mlt_properties_set_double( b_props, "consumer_aspect_ratio", mlt_properties_get_double( a_props, "consumer_aspect_ratio" ) );
242         mlt_properties_set_double( b_props, "consumer_scale", mlt_properties_get_double( a_props, "consumer_scale" ) );
243                 
244         // Honour the reverse here
245         mix = reverse ? 1 - mix : mix;
246         frame_delta *= reverse ? -1.0 : 1.0;
247
248         // Ensure we get scaling on the b_frame
249         mlt_properties_set( b_props, "rescale.interp", "nearest" );
250
251         if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
252                 // Composite the frames using a luma map
253                 luma_composite( this, b_frame, luma_width, luma_height, luma_bitmap, mix, frame_delta,
254                         luma_softness, progressive ? -1 : top_field_first, width, height );
255         else
256                 // Dissolve the frames using the time offset for mix value
257                 dissolve_yuv( this, b_frame, mix, *width, *height );
258
259         // Extract the a_frame image info
260         *width = mlt_properties_get_int( a_props, "width" );
261         *height = mlt_properties_get_int( a_props, "height" );
262         *image = mlt_properties_get_data( a_props, "image", NULL );
263
264         return 0;
265 }
266
267 /** Load the luma map from PGM stream.
268 */
269
270 static void luma_read_pgm( FILE *f, uint16_t **map, int *width, int *height )
271 {
272         uint8_t *data = NULL;
273         while (1)
274         {
275                 char line[128];
276                 int i = 2;
277                 int maxval;
278                 int bpp;
279                 uint16_t *p;
280                 
281                 line[127] = '\0';
282
283                 // get the magic code
284                 if ( fgets( line, 127, f ) == NULL )
285                         break;
286                 if ( line[0] != 'P' || line[1] != '5' )
287                         break;
288
289                 // skip white space and see if a new line must be fetched
290                 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
291                 if ( line[i] == '\0' && fgets( line, 127, f ) == NULL )
292                         break;
293
294                 // get the dimensions
295                 if ( line[0] == 'P' )
296                         i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
297                 else
298                         i = sscanf( line, "%d %d %d", width, height, &maxval );
299
300                 // get the height value, if not yet
301                 if ( i < 2 )
302                 {
303                         if ( fgets( line, 127, f ) == NULL )
304                                 break;
305                         i = sscanf( line, "%d", height );
306                         if ( i == 0 )
307                                 break;
308                         else
309                                 i = 2;
310                 }
311
312                 // get the maximum gray value, if not yet
313                 if ( i < 3 )
314                 {
315                         if ( fgets( line, 127, f ) == NULL )
316                                 break;
317                         i = sscanf( line, "%d", &maxval );
318                         if ( i == 0 )
319                                 break;
320                 }
321
322                 // determine if this is one or two bytes per pixel
323                 bpp = maxval > 255 ? 2 : 1;
324                 
325                 // allocate temporary storage for the raw data
326                 data = mlt_pool_alloc( *width * *height * bpp );
327                 if ( data == NULL )
328                         break;
329
330                 // read the raw data
331                 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
332                         break;
333                 
334                 // allocate the luma bitmap
335                 *map = p = (uint16_t*)mlt_pool_alloc( *width * *height * sizeof( uint16_t ) );
336                 if ( *map == NULL )
337                         break;
338
339                 // proces the raw data into the luma bitmap
340                 for ( i = 0; i < *width * *height * bpp; i += bpp )
341                 {
342                         if ( bpp == 1 )
343                                 *p++ = data[ i ] << 8;
344                         else
345                                 *p++ = ( data[ i ] << 8 ) + data[ i+1 ];
346                 }
347
348                 break;
349         }
350                 
351         if ( data != NULL )
352                 mlt_pool_release( data );
353 }
354
355
356 /** Luma transition processing.
357 */
358
359 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
360 {
361         transition_luma *this = (transition_luma*) transition->child;
362
363         // Get the properties of the transition
364         mlt_properties properties = mlt_transition_properties( transition );
365         
366         // Get the properties of the b frame
367         mlt_properties b_props = mlt_frame_properties( b_frame );
368
369         // If the filename property changed, reload the map
370         char *lumafile = mlt_properties_get( properties, "resource" );
371         if ( this->bitmap == NULL && lumafile != NULL )
372         {
373                 FILE *pipe = fopen( lumafile, "r" );
374                 if ( pipe != NULL )
375                 {
376                         luma_read_pgm( pipe, &this->bitmap, &this->width, &this->height );
377                         fclose( pipe );
378                 }
379         }
380
381         // Set the b frame properties
382         mlt_properties_set_double( b_props, "image.mix", position_calculate( transition, b_frame ) );
383         mlt_properties_set_double( b_props, "luma.delta", delta_calculate( transition, b_frame ) );
384         mlt_properties_set_int( b_props, "luma.width", this->width );
385         mlt_properties_set_int( b_props, "luma.height", this->height );
386         mlt_properties_set_data( b_props, "luma.bitmap", this->bitmap, 0, NULL, NULL );
387         mlt_properties_set_int( b_props, "luma.reverse", mlt_properties_get_int( properties, "reverse" ) );
388         mlt_properties_set_double( b_props, "luma.softness", mlt_properties_get_double( properties, "softness" ) );
389
390         mlt_frame_push_get_image( a_frame, transition_get_image );
391         mlt_frame_push_frame( a_frame, b_frame );
392
393         return a_frame;
394 }
395
396 /** Constructor for the filter.
397 */
398
399 mlt_transition transition_luma_init( char *lumafile )
400 {
401         transition_luma *this = calloc( sizeof( transition_luma ), 1 );
402         if ( this != NULL )
403         {
404                 mlt_transition transition = &this->parent;
405                 mlt_transition_init( transition, this );
406                 transition->process = transition_process;
407                 transition->close = transition_close;
408                 mlt_properties_set( mlt_transition_properties( transition ), "resource", lumafile );
409                 return &this->parent;
410         }
411         return NULL;
412 }
413
414 /** Close the transition.
415 */
416
417 static void transition_close( mlt_transition parent )
418 {
419         transition_luma *this = (transition_luma*) parent->child;
420         mlt_pool_release( this->bitmap );
421         free( this );
422 }
423