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