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