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