Memory pooling
[melted] / src / modules / gtk2 / producer_pango.c
1 /*
2 * producer_pango.c -- a pango-based titler
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 "producer_pango.h"
22 #include <framework/mlt_frame.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <gdk-pixbuf/gdk-pixbuf.h>
26 #include <pango/pangoft2.h>
27 #include <freetype/freetype.h>
28
29 struct producer_pango_s
30 {
31 struct mlt_producer_s parent;
32 int width;
33 int height;
34 void *image_release;
35 uint8_t *image;
36 void *alpha_release;
37 uint8_t *alpha;
38 char *fgcolor;
39 char *bgcolor;
40 int align;
41 int pad;
42 char *markup;
43 char *text;
44 char *font;
45 };
46
47 // special color type used by internal pango routines
48 typedef struct
49 {
50 uint8_t r, g, b, a;
51 } rgba_color;
52
53 // Forward declarations
54 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
55 static void producer_close( mlt_producer parent );
56 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg );
57 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font,
58 rgba_color fg, rgba_color bg, int pad, int align );
59
60 /** Parse the alignment property.
61 */
62
63 static int alignment_parse( char* align )
64 {
65 int ret = pango_align_left;
66
67 if ( align == NULL );
68 else if ( isdigit( align[ 0 ] ) )
69 ret = atoi( align );
70 else if ( align[ 0 ] == 'c' || align[ 0 ] == 'm' )
71 ret = pango_align_center;
72 else if ( align[ 0 ] == 'r' )
73 ret = pango_align_right;
74
75 return ret;
76 }
77
78 mlt_producer producer_pango_init( const char *filename )
79 {
80 producer_pango this = calloc( sizeof( struct producer_pango_s ), 1 );
81 if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
82 {
83 mlt_producer producer = &this->parent;
84
85 producer->get_frame = producer_get_frame;
86 producer->close = producer_close;
87
88 // This is required to initialise gdk-pixbuf
89 g_type_init();
90
91 // Get the properties interface
92 mlt_properties properties = mlt_producer_properties( &this->parent );
93
94 // Set the default properties
95 mlt_properties_set( properties, "fgcolour", "0xffffffff" );
96 mlt_properties_set( properties, "bgcolour", "0x00000000" );
97 mlt_properties_set_int( properties, "align", pango_align_left );
98 mlt_properties_set_int( properties, "pad", 0 );
99 mlt_properties_set( properties, "text", "" );
100 mlt_properties_set( properties, "font", "Sans 48" );
101
102 if ( filename == NULL )
103 {
104 mlt_properties_set( properties, "resource", "pango" );
105 mlt_properties_set( properties, "markup", "" );
106 }
107 else if ( filename[ 0 ] == '+' || strstr( filename, "/+" ) )
108 {
109 char *copy = strdup( filename + 1 );
110 char *markup = copy;
111 if ( strstr( markup, "/+" ) )
112 markup = strstr( markup, "/+" ) + 2;
113 ( *strrchr( markup, '.' ) ) = '\0';
114 while ( strchr( markup, '~' ) )
115 ( *strchr( markup, '~' ) ) = '\n';
116 mlt_properties_set( properties, "resource", ( char * )filename );
117 mlt_properties_set( properties, "markup", markup );
118 free( copy );
119 }
120 else
121 {
122 FILE *f = fopen( filename, "r" );
123 if ( f != NULL )
124 {
125 char line[81];
126 char *markup = NULL;
127 size_t size = 0;
128 line[80] = '\0';
129
130 while ( fgets( line, 80, f ) )
131 {
132 size += strlen( line ) + 1;
133 if ( markup )
134 {
135 markup = realloc( markup, size );
136 strcat( markup, line );
137 }
138 else
139 {
140 markup = strdup( line );
141 }
142 }
143 fclose( f );
144
145 if ( markup[ strlen( markup ) - 1 ] == '\n' )
146 markup[ strlen( markup ) - 1 ] = '\0';
147
148 mlt_properties_set( properties, "resource", ( char * ) filename );
149 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
150 free( markup );
151 }
152 else
153 {
154 mlt_properties_set( properties, "resource", "pango" );
155 mlt_properties_set( properties, "markup", "" );
156 }
157 }
158
159 return producer;
160 }
161 free( this );
162 return NULL;
163 }
164
165 static void set_string( char **string, char *value, char *fallback )
166 {
167 if ( value != NULL )
168 {
169 free( *string );
170 *string = strdup( value );
171 }
172 else if ( *string == NULL && fallback != NULL )
173 {
174 *string = strdup( fallback );
175 }
176 else if ( *string != NULL && fallback == NULL )
177 {
178 free( *string );
179 *string = NULL;
180 }
181 }
182
183 rgba_color parse_color( char *color )
184 {
185 rgba_color result = { 0xff, 0xff, 0xff, 0xff };
186
187 if ( !strncmp( color, "0x", 2 ) )
188 {
189 unsigned int temp = 0;
190 sscanf( color + 2, "%x", &temp );
191 result.r = ( temp >> 24 ) & 0xff;
192 result.g = ( temp >> 16 ) & 0xff;
193 result.b = ( temp >> 8 ) & 0xff;
194 result.a = ( temp ) & 0xff;
195 }
196 else if ( !strcmp( color, "red" ) )
197 {
198 result.r = 0xff;
199 result.g = 0x00;
200 result.b = 0x00;
201 }
202 else if ( !strcmp( color, "green" ) )
203 {
204 result.r = 0x00;
205 result.g = 0xff;
206 result.b = 0x00;
207 }
208 else if ( !strcmp( color, "blue" ) )
209 {
210 result.r = 0x00;
211 result.g = 0x00;
212 result.b = 0xff;
213 }
214 else
215 {
216 unsigned int temp = 0;
217 sscanf( color, "%d", &temp );
218 result.r = ( temp >> 24 ) & 0xff;
219 result.g = ( temp >> 16 ) & 0xff;
220 result.b = ( temp >> 8 ) & 0xff;
221 result.a = ( temp ) & 0xff;
222 }
223
224 return result;
225 }
226
227 static void refresh_image( mlt_frame frame, int width, int height )
228 {
229 // Pixbuf
230 GdkPixbuf *pixbuf = NULL;
231
232 // Obtain properties of frame
233 mlt_properties properties = mlt_frame_properties( frame );
234
235 // Obtain the producer pango for this frame
236 producer_pango this = mlt_properties_get_data( properties, "producer_pango", NULL );
237
238 // Obtain the producer
239 mlt_producer producer = &this->parent;
240
241 // Obtain the producer properties
242 mlt_properties producer_props = mlt_producer_properties( producer );
243
244 // Get producer properties
245 char *fg = mlt_properties_get( producer_props, "fgcolour" );
246 char *bg = mlt_properties_get( producer_props, "bgcolour" );
247 int align = alignment_parse( mlt_properties_get( producer_props, "align" ) );
248 int pad = mlt_properties_get_int( producer_props, "pad" );
249 char *markup = mlt_properties_get( producer_props, "markup" );
250 char *text = mlt_properties_get( producer_props, "text" );
251 char *font = mlt_properties_get( producer_props, "font" );
252
253 // See if any properties changed
254 int property_changed = ( align != this->align );
255 property_changed = property_changed || ( this->fgcolor == NULL || ( fg && strcmp( fg, this->fgcolor ) ) );
256 property_changed = property_changed || ( this->bgcolor == NULL || ( bg && strcmp( bg, this->bgcolor ) ) );
257 property_changed = property_changed || ( pad != this->pad );
258 property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
259 property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
260 property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
261
262 // Save the properties for next comparison
263 this->align = align;
264 this->pad = pad;
265 set_string( &this->fgcolor, fg, "0xffffffff" );
266 set_string( &this->bgcolor, bg, "0x00000000" );
267 set_string( &this->markup, markup, NULL );
268 set_string( &this->text, text, NULL );
269 set_string( &this->font, font, "Sans 48" );
270
271 if ( property_changed )
272 {
273 rgba_color fgcolor = parse_color( this->fgcolor );
274 rgba_color bgcolor = parse_color( this->bgcolor );
275
276 mlt_pool_release( this->image_release );
277 mlt_pool_release( this->alpha_release );
278 this->image_release = NULL;
279 this->alpha_release = NULL;
280 this->image = NULL;
281 this->alpha = NULL;
282
283 // Render the title
284 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align );
285
286 if ( pixbuf != NULL )
287 {
288 // Register this pixbuf for destruction and reuse
289 mlt_properties_set_data( producer_props, "pixbuf", pixbuf, 0, ( mlt_destructor )g_object_unref, NULL );
290
291 mlt_properties_set_int( producer_props, "real_width", gdk_pixbuf_get_width( pixbuf ) );
292 mlt_properties_set_int( producer_props, "real_height", gdk_pixbuf_get_height( pixbuf ) );
293
294 // Store the width/height of the pixbuf temporarily
295 this->width = gdk_pixbuf_get_width( pixbuf );
296 this->height = gdk_pixbuf_get_height( pixbuf );
297 }
298 }
299 else if ( width > 0 && ( this->image == NULL || width != this->width || height != this->height ) )
300 {
301 mlt_pool_release( this->image_release );
302 mlt_pool_release( this->alpha_release );
303 this->image_release = NULL;
304 this->alpha_release = NULL;
305 this->image = NULL;
306 this->alpha = NULL;
307
308 pixbuf = mlt_properties_get_data( producer_props, "pixbuf", NULL );
309 }
310
311 // If we have a pixbuf and a valid width
312 if ( pixbuf && width > 0 )
313 {
314 char *interps = mlt_properties_get( properties, "rescale.interp" );
315 int interp = GDK_INTERP_BILINEAR;
316
317 if ( strcmp( interps, "nearest" ) == 0 )
318 interp = GDK_INTERP_NEAREST;
319 else if ( strcmp( interps, "tiles" ) == 0 )
320 interp = GDK_INTERP_TILES;
321 else if ( strcmp( interps, "hyper" ) == 0 )
322 interp = GDK_INTERP_HYPER;
323
324 // fprintf( stderr, "SCALING PANGO from %dx%d to %dx%d was %dx%d\n", gdk_pixbuf_get_width( pixbuf ), gdk_pixbuf_get_height( pixbuf ), width, height, this->width, this->height );
325
326 // Note - the original pixbuf is already safe and ready for destruction
327 pixbuf = gdk_pixbuf_scale_simple( pixbuf, width, height, interp );
328
329 // Store width and height
330 this->width = width;
331 this->height = height;
332
333 // Allocate/define image
334 this->image = mlt_pool_allocate( width * ( height + 1 ) * 2, &this->image_release );
335 this->alpha = mlt_pool_allocate( this->width * this->height, &this->alpha_release );
336
337 // Convert the image
338 mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
339 this->width, this->height,
340 gdk_pixbuf_get_rowstride( pixbuf ),
341 this->image, this->alpha );
342
343 // Finished with pixbuf now
344 g_object_unref( pixbuf );
345 }
346
347 // Set width/height
348 mlt_properties_set_int( properties, "width", this->width );
349 mlt_properties_set_int( properties, "height", this->height );
350 mlt_properties_set_int( properties, "real_width", mlt_properties_get_int( producer_props, "real_width" ) );
351 mlt_properties_set_int( properties, "real_height", mlt_properties_get_int( producer_props, "real_height" ) );
352
353 // pass the image data without destructor
354 mlt_properties_set_data( properties, "image", this->image, this->width * ( this->height + 1 ) * 2, NULL, NULL );
355 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
356 }
357
358 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
359 {
360 // Obtain properties of frame
361 mlt_properties properties = mlt_frame_properties( frame );
362
363 *width = mlt_properties_get_int( properties, "rescale_width" );
364 *height = mlt_properties_get_int( properties, "rescale_height" );
365
366 // Refresh the image
367 refresh_image( frame, *width, *height );
368
369 // Determine format
370 //mlt_producer this = mlt_properties_get_data( properties, "producer_pango", NULL );
371 //*format = ( mlt_properties_get_int( mlt_producer_properties( this ), "bpp" ) == 4 ) ? mlt_image_rgb24a : mlt_image_rgb24;
372
373 // May need to know the size of the image to clone it
374 int size = 0;
375
376 // Get the image
377 uint8_t *image = mlt_properties_get_data( properties, "image", &size );
378
379 // Get width and height
380 *width = mlt_properties_get_int( properties, "width" );
381 *height = mlt_properties_get_int( properties, "height" );
382
383 // Clone if necessary
384 if ( writable )
385 {
386 // Clone our image
387 void *release = NULL;
388 uint8_t *copy = mlt_pool_allocate( size, &release );
389 memcpy( copy, image, size );
390
391 // We're going to pass the copy on
392 image = copy;
393
394 // Now update properties so we free the copy after
395 mlt_properties_set_data( properties, "image_release", release, 0, ( mlt_destructor )mlt_pool_release, NULL );
396 mlt_properties_set_data( properties, "image", copy, size, NULL, NULL );
397 }
398
399 // Pass on the image
400 *buffer = image;
401
402 return 0;
403 }
404
405 static uint8_t *producer_get_alpha_mask( mlt_frame this )
406 {
407 // Obtain properties of frame
408 mlt_properties properties = mlt_frame_properties( this );
409
410 // Return the alpha mask
411 return mlt_properties_get_data( properties, "alpha", NULL );
412 }
413
414 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
415 {
416 producer_pango this = producer->child;
417
418 // Generate a frame
419 *frame = mlt_frame_init( );
420
421 // Obtain properties of frame and producer
422 mlt_properties properties = mlt_frame_properties( *frame );
423
424 // Set the producer on the frame properties
425 mlt_properties_set_data( properties, "producer_pango", this, 0, NULL, NULL );
426
427 // Refresh the pango image
428 refresh_image( *frame, 0, 0 );
429
430 // Set producer-specific frame properties
431 mlt_properties_set_int( properties, "progressive", 1 );
432 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( properties, "real_width" ) / mlt_properties_get_double( properties, "real_height" ) );
433
434 // Set alpha call back
435 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
436
437 // Stack the get image callback
438 mlt_frame_push_get_image( *frame, producer_get_image );
439
440 // Update timecode on the frame we're creating
441 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
442
443 // Calculate the next timecode
444 mlt_producer_prepare_next( producer );
445
446 return 0;
447 }
448
449 static void producer_close( mlt_producer parent )
450 {
451 producer_pango this = parent->child;
452 mlt_pool_release( this->image_release );
453 mlt_pool_release( this->alpha_release );
454 free( this->fgcolor );
455 free( this->bgcolor );
456 free( this->markup );
457 free( this->text );
458 free( this->font );
459 parent->close = NULL;
460 mlt_producer_close( parent );
461 free( this );
462 }
463
464 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
465 {
466 int ww = gdk_pixbuf_get_width( pixbuf );
467 int hh = gdk_pixbuf_get_height( pixbuf );
468 uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
469 int i, j;
470
471 for ( j = 0; j < hh; j++ )
472 {
473 for ( i = 0; i < ww; i++ )
474 {
475 *p++ = bg.r;
476 *p++ = bg.g;
477 *p++ = bg.b;
478 *p++ = bg.a;
479 }
480 }
481 }
482
483 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, int pad, int align )
484 {
485 PangoFT2FontMap *fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
486 PangoContext *context = pango_ft2_font_map_create_context( fontmap );
487 PangoLayout *layout = pango_layout_new( context );
488 int w, h, x;
489 int i, j;
490 GdkPixbuf *pixbuf = NULL;
491 FT_Bitmap bitmap;
492 uint8_t *src = NULL;
493 uint8_t* dest = NULL;
494 int stride;
495 void *release = NULL;
496
497 pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
498 pango_layout_set_width( layout, -1 ); // set wrapping constraints
499 pango_layout_set_font_description( layout, pango_font_description_from_string( font ) );
500 // pango_layout_set_spacing( layout, space );
501 pango_layout_set_alignment( layout, ( PangoAlignment ) align );
502 if ( markup != NULL && strcmp( markup, "" ) != 0 )
503 pango_layout_set_markup( layout, markup, strlen( markup ) );
504 else if ( text != NULL && strcmp( text, "" ) != 0 )
505 pango_layout_set_text( layout, text, strlen( text ) );
506 else
507 return NULL;
508 pango_layout_get_pixel_size( layout, &w, &h );
509
510 pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
511 pango_draw_background( pixbuf, bg );
512
513 stride = gdk_pixbuf_get_rowstride( pixbuf );
514
515 bitmap.width = w;
516 bitmap.pitch = 32 * ( ( w + 31 ) / 31 );
517 bitmap.rows = h;
518 bitmap.buffer = mlt_pool_allocate( h * bitmap.pitch, &release );
519 bitmap.num_grays = 256;
520 bitmap.pixel_mode = ft_pixel_mode_grays;
521
522 memset( bitmap.buffer, 0, h * bitmap.pitch );
523
524 pango_ft2_render_layout( &bitmap, layout, 0, 0 );
525
526 src = bitmap.buffer;
527 x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
528 dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
529 for ( j = 0; j < h; j++ )
530 {
531 uint8_t *d = dest;
532 for ( i = 0; i < w; i++ )
533 {
534 float a = ( float ) bitmap.buffer[ j * bitmap.pitch + i ] / 255.0;
535 *d++ = ( int ) ( a * fg.r + ( 1 - a ) * bg.r );
536 *d++ = ( int ) ( a * fg.g + ( 1 - a ) * bg.g );
537 *d++ = ( int ) ( a * fg.b + ( 1 - a ) * bg.b );
538 *d++ = ( int ) ( a * fg.a + ( 1 - a ) * bg.a );
539 }
540 dest += stride;
541 }
542 mlt_pool_release( release );
543 g_object_unref( layout );
544 g_object_unref( context );
545 g_object_unref( fontmap );
546
547 return pixbuf;
548 }
549