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