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