]> git.sesse.net Git - mlt/blob - src/modules/core/transition_luma.c
633d8da96dcb7f72b3af69c0059f6ecdb66f0636
[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         double *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 double smoothstep( double edge1, double edge2, double 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 /** powerful stuff
63
64     \param field_order -1 = progressive, 0 = lower field first, 1 = top field first
65 */
66 static void luma_composite( mlt_frame this, mlt_frame b_frame, int luma_width, int luma_height,
67                                                         double *luma_bitmap, double pos, double frame_delta, double softness, int field_order )
68 {
69         int width_src, height_src;
70         int width_dest, height_dest;
71         mlt_image_format format_src, format_dest;
72         uint8_t *p_src, *p_dest;
73         int i, j;
74         int stride_src;
75         int stride_dest;
76         double weight = 0;
77         int field;
78
79         format_src = mlt_image_yuv422;
80         format_dest = mlt_image_yuv422;
81
82         mlt_frame_get_image( this, &p_dest, &format_dest, &width_dest, &height_dest, 1 /* writable */ );
83         mlt_frame_get_image( b_frame, &p_src, &format_src, &width_src, &height_src, 0 /* writable */ );
84
85         stride_src = width_src * 2;
86         stride_dest = width_dest * 2;
87
88         // composite using luma map
89         for ( field = 0; field < ( field_order < 0 ? 1 : 2 ); ++field )
90         {
91                 // Offset the position based on which field we're looking at ...
92                 double field_pos = pos + ( ( field_order == 0 ? 1 - field : field) * frame_delta * 0.5 );
93
94                 // adjust the position for the softness level
95                 field_pos *= ( 1.0 + softness );
96
97                 for ( i = field; i < height_src; i += ( field_order < 0 ? 1 : 2 ) )
98                 {
99                         uint8_t *p = &p_src[ i * stride_src ];
100                         uint8_t *q = &p_dest[ i * stride_dest ];
101                         uint8_t *o = &p_dest[ i * stride_dest ];
102                         double  *l = &luma_bitmap[ i * luma_width ];
103
104                         for ( j = 0; j < width_src; j ++ )
105                         {
106                                 uint8_t y = *p ++;
107                                 uint8_t uv = *p ++;
108                 weight = *l ++;
109                                 double value = smoothstep( weight, weight + softness, field_pos );
110
111                                 *o ++ = (uint8_t)( y * value + *q++ * ( 1 - value ) );
112                                 *o ++ = (uint8_t)( uv * value + *q++ * ( 1 - value ) );
113                         }
114                 }
115         }
116 }
117
118 /** Get the image.
119 */
120
121 static int transition_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
122 {
123         // Get the properties of the a frame
124         mlt_properties a_props = mlt_frame_properties( this );
125
126         // Get the b frame from the stack
127         mlt_frame b_frame = mlt_frame_pop_frame( this );
128
129         // Get the properties of the b frame
130         mlt_properties b_props = mlt_frame_properties( b_frame );
131
132         // Arbitrary composite defaults
133         static double previous_mix = 0;
134         double mix = 0;
135         int luma_width = 0;
136         int luma_height = 0;
137         double *luma_bitmap = NULL;
138         double luma_softness = 0;
139         int progressive = 0;
140         int top_field_first = 0;
141
142         // mix is the offset time value in the duration of the transition
143         // - also used as the mixing level for a dissolve
144         if ( mlt_properties_get( b_props, "image.mix" ) != NULL )
145                 mix = mlt_properties_get_double( b_props, "image.mix" );
146
147         // (mix - previous_mix) is the animation delta, if backwards reset previous
148         if ( mix < previous_mix )
149                 previous_mix = 0;
150
151         // Get the interlace and field properties of the frame
152         if ( mlt_properties_get( b_props, "progressive" ) != NULL )
153                 progressive = mlt_properties_get_int( b_props, "progressive" );
154         if ( mlt_properties_get( b_props, "top_field_first" ) != NULL )
155                 top_field_first = mlt_properties_get_int( b_props, "top_field_first" );
156
157         // Get the luma map parameters
158         if ( mlt_properties_get( b_props, "luma.width" ) != NULL )
159                 luma_width = mlt_properties_get_int( b_props, "luma.width" );
160         if ( mlt_properties_get( b_props, "luma.height" ) != NULL )
161                 luma_height = mlt_properties_get_int( b_props, "luma.height" );
162         if ( mlt_properties_get( b_props, "luma.softness" ) != NULL )
163                 luma_softness = mlt_properties_get_double( b_props, "luma.softness" );
164         luma_bitmap = (double*) mlt_properties_get_data( b_props, "luma.bitmap", NULL );
165
166         if ( luma_width > 0 && luma_height > 0 && luma_bitmap != NULL )
167                 // Composite the frames using a luma map
168                 luma_composite( this, b_frame, luma_width, luma_height, luma_bitmap, mix, mix - previous_mix,
169                         luma_softness, progressive > 0 ? -1 : top_field_first );
170         else
171                 // Dissolve the frames using the time offset for mix value
172                 mlt_frame_composite_yuv( this, b_frame, 0, 0, mix );
173
174         // Extract the a_frame image info
175         *width = mlt_properties_get_int( a_props, "width" );
176         *height = mlt_properties_get_int( a_props, "height" );
177         *image = mlt_properties_get_data( a_props, "image", NULL );
178
179         previous_mix = mix;
180
181         return 0;
182 }
183
184 /** Load the luma map from PGM stream.
185 */
186
187 static void luma_read_pgm( FILE *f, double **map, int *width, int *height )
188 {
189         uint8_t *data = NULL;
190         while (1)
191         {
192                 char line[128];
193                 int i = 2;
194                 int maxval;
195                 int bpp;
196                 double *p;
197                 
198                 line[127] = '\0';
199
200                 // get the magic code
201                 if ( fgets( line, 127, f ) == NULL )
202                         break;
203                 if ( line[0] != 'P' || line[1] != '5' )
204                         break;
205
206                 // skip white space and see if a new line must be fetched
207                 for ( i = 2; i < 127 && line[i] != '\0' && isspace( line[i] ); i++ );
208                 if ( line[i] == '\0' && fgets( line, 127, f ) == NULL )
209                         break;
210
211                 // get the dimensions
212                 if ( line[0] == 'P' )
213                         i = sscanf( line, "P5 %d %d %d", width, height, &maxval );
214                 else
215                         i = sscanf( line, "%d %d %d", width, height, &maxval );
216
217                 // get the height value, if not yet
218                 if ( i < 2 )
219                 {
220                         if ( fgets( line, 127, f ) == NULL )
221                                 break;
222                         i = sscanf( line, "%d", height );
223                         if ( i == 0 )
224                                 break;
225                         else
226                                 i = 2;
227                 }
228
229                 // get the maximum gray value, if not yet
230                 if ( i < 3 )
231                 {
232                         if ( fgets( line, 127, f ) == NULL )
233                                 break;
234                         i = sscanf( line, "%d", &maxval );
235                         if ( i == 0 )
236                                 break;
237                 }
238
239                 // determine if this is one or two bytes per pixel
240                 bpp = maxval > 255 ? 2 : 1;
241                         // allocate temporary storage for the raw data
242                 data = malloc( *width * *height * bpp );
243                 if ( data == NULL )
244                         break;
245
246                 // read the raw data
247                 if ( fread( data, *width * *height * bpp, 1, f ) != 1 )
248                         break;
249                 
250                 // allocate the luma bitmap
251                 *map =  p = (double*) malloc( *width * *height * sizeof( double ) );
252                 if ( *map == NULL )
253                         break;
254
255                 // proces the raw data into the luma bitmap
256                 for ( i = 0; i < *width * *height * bpp; i += bpp )
257                 {
258                         if ( bpp == 1 )
259                                 *p++ = (double) data[ i ] / (double) maxval;
260                         else
261                                 *p++ = (double) ( ( data[ i ] << 8 ) + data[ i+1 ] ) / (double) maxval;
262                 }
263
264                 break;
265         }
266                 
267         if ( data != NULL )
268                 free( data );
269 }
270
271
272 /** Luma transition processing.
273 */
274
275 static mlt_frame transition_process( mlt_transition transition, mlt_frame a_frame, mlt_frame b_frame )
276 {
277         transition_luma *this = (transition_luma*) transition->child;
278
279         // Get the properties of the transition
280         mlt_properties properties = mlt_transition_properties( transition );
281         
282         // Get the properties of the b frame
283         mlt_properties b_props = mlt_frame_properties( b_frame );
284
285         // If the filename property changed, reload the map
286         char *luma_file = mlt_properties_get( properties, "filename" );
287         if ( luma_file != NULL && ( this->filename == NULL || ( this->filename && strcmp( luma_file, this->filename ) ) ) )
288         {
289                 int width = mlt_properties_get_int( b_props, "width" );
290                 int height = mlt_properties_get_int( b_props, "height" );
291                 char command[ 512 ];
292                 FILE *pipe;
293                 
294                 command[ 511 ] = '\0';
295                 if ( this->filename )
296                         free( this->filename );
297                 this->filename = strdup( luma_file );
298                 snprintf( command, 511, "anytopnm %s | pnmscale -width %d -height %d", luma_file, width, height );
299                 //pipe = popen( command, "r" );
300                 pipe = fopen( luma_file, "r" );
301                 if ( pipe != NULL )
302                 {
303                         if ( this->bitmap )
304                                 free( this->bitmap );
305                         luma_read_pgm( pipe, &this->bitmap, &this->width, &this->height );
306                         //pclose( pipe );
307                         fclose( pipe );
308                 }
309                 
310         }
311
312         // Determine the time position of this frame in the transition duration
313         mlt_position in = mlt_transition_get_in( transition );
314         mlt_position out = mlt_transition_get_out( transition );
315         mlt_position time = mlt_frame_get_position( b_frame );
316         double pos = ( double )( time - in ) / ( double )( out - in + 1 );
317         
318         // Set the b frame properties
319         mlt_properties_set_double( b_props, "image.mix", pos );
320         mlt_properties_set_int( b_props, "luma.width", this->width );
321         mlt_properties_set_int( b_props, "luma.height", this->height );
322         mlt_properties_set_data( b_props, "luma.bitmap", this->bitmap, 0, NULL, NULL );
323         if ( mlt_properties_get( properties, "softness" ) != NULL )
324                 mlt_properties_set_double( b_props, "luma.softness", mlt_properties_get_double( properties, "softness" ) );
325
326         mlt_frame_push_get_image( a_frame, transition_get_image );
327         mlt_frame_push_frame( a_frame, b_frame );
328
329         return a_frame;
330 }
331
332 /** Constructor for the filter.
333 */
334
335 mlt_transition transition_luma_init( char *lumafile )
336 {
337         transition_luma *this = calloc( sizeof( transition_luma ), 1 );
338         if ( this != NULL )
339         {
340                 mlt_transition transition = &this->parent;
341                 mlt_transition_init( transition, this );
342                 transition->process = transition_process;
343                 transition->close = transition_close;
344
345                 if ( lumafile != NULL )
346                         mlt_properties_set( mlt_transition_properties( transition ), "filename", lumafile );
347                 
348                 return &this->parent;
349         }
350         return NULL;
351 }
352
353 /** Close the transition.
354 */
355
356 static void transition_close( mlt_transition parent )
357 {
358         transition_luma *this = (transition_luma*) parent->child;
359
360         if ( this->bitmap )
361                 free( this->bitmap );
362         
363         if ( this->filename )
364                 free( this->filename );
365
366         free( this );
367 }
368