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