]> git.sesse.net Git - mlt/blob - src/modules/gtk2/producer_pango.c
new volume, mix, and resample filters and transitions
[mlt] / 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         int   fgcolor;
37         int   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_int( properties, "video_standard", mlt_video_standard_pal );
76                 mlt_properties_set_int( properties, "fgcolor", 0xffffffff );
77                 mlt_properties_set_int( properties, "bgcolor", 0x00000000 );
78                 mlt_properties_set_int( properties, "align", pango_align_left );
79                 mlt_properties_set_int( properties, "pad", 0 );
80                 mlt_properties_set( properties, "text", "" );
81                 mlt_properties_set( properties, "font", "Sans 48" );
82                 mlt_properties_set_int( properties, "x", 0 );
83                 mlt_properties_set_int( properties, "y", 0 );
84                 mlt_properties_set_double( properties, "mix", 1.0 );
85
86                 if ( filename == NULL )
87                 {
88                         mlt_properties_set( properties, "resource", "pango" );
89                         mlt_properties_set( properties, "markup", "" );
90                 }
91                 else
92                 {
93                         FILE *f = fopen( filename, "r" );
94                         if ( f != NULL )
95                         {
96                                 char line[81];
97                                 char *markup = NULL;
98                                 size_t size = 0;
99                                 line[80] = '\0';
100                                 
101                                 while ( fgets( line, 80, f ) )
102                                 {
103                                         size += strlen( line ) + 1;
104                                         if ( markup )
105                                         {
106                                                 realloc( markup, size );
107                                                 strcat( markup, line );
108                                         }
109                                         else
110                                         {
111                                                 markup = strdup( line );
112                                         }
113                                 }
114                                 fclose( f );
115                                 mlt_properties_set( properties, "resource", ( char * ) filename );
116                                 mlt_properties_set( properties, "markup", ( char * ) ( markup == NULL ? "" : markup ) );
117                                 if ( markup )
118                                         free( markup );
119                         }
120                         else
121                         {
122                                 mlt_properties_set( properties, "resource", "pango" );
123                                 mlt_properties_set( properties, "markup", "" );
124                         }
125                 }
126
127                 return producer;
128         }
129         free( this );
130         return NULL;
131 }
132
133 static int producer_get_image( mlt_frame this, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
134 {
135         // Obtain properties of frame
136         mlt_properties properties = mlt_frame_properties( this );
137
138         // May need to know the size of the image to clone it
139         int size = 0;
140
141         // Get the image
142         uint8_t *image = mlt_properties_get_data( properties, "image", &size );
143
144         // Get width and height
145         *width = mlt_properties_get_int( properties, "width" );
146         *height = mlt_properties_get_int( properties, "height" );
147
148         // Clone if necessary
149         if ( writable )
150         {
151                 // Clone our image
152                 uint8_t *copy = malloc( size );
153                 memcpy( copy, image, size );
154
155                 // We're going to pass the copy on
156                 image = copy;
157
158                 // Now update properties so we free the copy after
159                 mlt_properties_set_data( properties, "image", copy, size, free, NULL );
160         }
161
162         // Pass on the image
163         *buffer = image;
164
165         return 0;
166 }
167
168 static uint8_t *producer_get_alpha_mask( mlt_frame this )
169 {
170         // Obtain properties of frame
171         mlt_properties properties = mlt_frame_properties( this );
172
173         // Return the alpha mask
174         return mlt_properties_get_data( properties, "alpha", NULL );
175 }
176
177 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
178 {
179         producer_pango this = producer->child;
180         GdkPixbuf *pixbuf = NULL;
181
182         // Generate a frame
183         *frame = mlt_frame_init( );
184
185         // Obtain properties of frame and producer
186         mlt_properties properties = mlt_frame_properties( *frame );
187         mlt_properties producer_props = mlt_producer_properties( producer );
188         
189         // Get producer properties
190         int fg = mlt_properties_get_int( producer_props, "fgcolor" );
191         int bg = mlt_properties_get_int( producer_props, "bgcolor" );
192         int align = mlt_properties_get_int( producer_props, "align" );
193         int pad = mlt_properties_get_int( producer_props, "pad" );
194         char *markup = mlt_properties_get( producer_props, "markup" );
195         char *text = mlt_properties_get( producer_props, "text" );
196         char *font = mlt_properties_get( producer_props, "font" );
197
198         // See if any properties changed
199         int property_changed = ( fg != this->fgcolor );
200         property_changed = property_changed || ( bg != this->bgcolor );
201         property_changed = property_changed || ( align != this->align );
202         property_changed = property_changed || ( pad != this->pad );
203         property_changed = property_changed || ( markup && this->markup && strcmp( markup, this->markup ) );
204         property_changed = property_changed || ( text && this->text && strcmp( text, this->text ) );
205         property_changed = property_changed || ( font && this->font && strcmp( font, this->font ) );
206
207         // Save the properties for next comparison
208         this->fgcolor = fg;
209         this->bgcolor = bg;
210         this->align = align;
211         this->pad = pad;
212         if ( markup != NULL )
213         {
214                 if ( this->markup != NULL )
215                         free( this->markup );
216                 this->markup = strdup( markup );
217         }
218         if ( text != NULL )
219         {
220                 if ( this->text != NULL )
221                         free( this->text );
222                 this->text = strdup( text );
223         }
224         if ( font != NULL )
225         {
226                 if ( this->font != NULL )
227                         free( this->font );
228                 this->font = strdup( font );
229         }
230
231         if ( property_changed )
232         {
233                 rgba_color fgcolor =
234                 {
235                         ( fg & 0xff000000 ) >> 24,
236                         ( fg & 0x00ff0000 ) >> 16,
237                         ( fg & 0x0000ff00 ) >> 8,
238                         ( fg & 0x000000ff )
239                 };
240                 rgba_color bgcolor =
241                 {
242                         ( bg & 0xff000000 ) >> 24,
243                         ( bg & 0x00ff0000 ) >> 16,
244                         ( bg & 0x0000ff00 ) >> 8,
245                         ( bg & 0x000000ff )
246                 };
247
248                 // Render the title
249                 pixbuf = pango_get_pixbuf( markup, text, font, fgcolor, bgcolor, pad, align );
250         }
251
252         // If we have a pixbuf
253         if ( pixbuf )
254         {
255                 // Scale to adjust for sample aspect ratio
256                 if ( mlt_properties_get_int( properties, "video_standard" ) == mlt_video_standard_pal )
257                 {
258                         GdkPixbuf *temp = pixbuf;
259                         GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
260                                 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 54.0/59.0),
261                                 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
262                         pixbuf = scaled;
263                         g_object_unref( temp );
264                 }
265                 else
266                 {
267                         GdkPixbuf *temp = pixbuf;
268                         GdkPixbuf *scaled = gdk_pixbuf_scale_simple( pixbuf,
269                                 (gint) ( (float) gdk_pixbuf_get_width( pixbuf ) * 11.0/10.0 ),
270                                 gdk_pixbuf_get_height( pixbuf ), GDK_INTERP_HYPER );
271                         pixbuf = scaled;
272                         g_object_unref( temp );
273                 }
274
275                 // Store width and height
276                 this->width = gdk_pixbuf_get_width( pixbuf );
277                 this->height = gdk_pixbuf_get_height( pixbuf );
278
279                 // Allocate/define image and alpha
280                 uint8_t *image = malloc( this->width * this->height * 2 );
281                 uint8_t *alpha = NULL;
282
283                 // Extract YUV422 and alpha
284                 if ( gdk_pixbuf_get_has_alpha( pixbuf ) )
285                 {
286                         // Allocate the alpha mask
287                         alpha = malloc( this->width * this->height );
288
289                         // Convert the image
290                         mlt_convert_rgb24a_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
291                                                                                   this->width, this->height,
292                                                                                   gdk_pixbuf_get_rowstride( pixbuf ),
293                                                                                   image, alpha );
294                 }
295                 else
296                 { 
297                         // No alpha to extract
298                         mlt_convert_rgb24_to_yuv422( gdk_pixbuf_get_pixels( pixbuf ),
299                                                                                  this->width, this->height,
300                                                                                  gdk_pixbuf_get_rowstride( pixbuf ),
301                                                                                  image );
302                 }
303
304                 // Finished with pixbuf now
305                 g_object_unref( pixbuf );
306                 
307                 // if single picture, reference the image and alpha in the producer
308                 if ( this->image != NULL )
309                         free( this->image );
310                 this->image = image;
311                 if ( this->alpha != NULL )
312                         free( this->alpha );
313                 this->alpha = alpha;
314
315         }
316
317         if ( this->image != NULL )
318         {
319                 // Set width/height
320                 mlt_properties_set_int( properties, "width", this->width );
321                 mlt_properties_set_int( properties, "height", this->height );
322
323                 // Set the compositing properties
324                 if ( mlt_properties_get( producer_props, "x" ) != NULL )
325                         mlt_properties_set_int( properties, "x", mlt_properties_get_int( producer_props, "x" ) );
326                 if ( mlt_properties_get( producer_props, "y" ) != NULL )
327                         mlt_properties_set_int( properties, "y", mlt_properties_get_int( producer_props, "y" ) );
328                 if ( mlt_properties_get( producer_props, "mix" ) != NULL )
329                         mlt_properties_set_double( properties, "image.mix",  mlt_properties_get_double( producer_props, "mix" ) );
330
331                 // if picture sequence pass the image and alpha data without destructor
332                 mlt_properties_set_data( properties, "image", this->image, this->width * this->height * 2, NULL, NULL );
333                 mlt_properties_set_data( properties, "alpha", this->alpha, this->width * this->height, NULL, NULL );
334
335                 // Set alpha mask call back
336                 ( *frame )->get_alpha_mask = producer_get_alpha_mask;
337
338                 // Stack the get image callback
339                 mlt_frame_push_get_image( *frame, producer_get_image );
340         }
341
342         // Update timecode on the frame we're creating
343         mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
344
345         // Calculate the next timecode
346         mlt_producer_prepare_next( producer );
347
348         return 0;
349 }
350
351 static void producer_close( mlt_producer parent )
352 {
353         producer_pango this = parent->child;
354         if ( this->image != NULL )
355                 free( this->image );
356         if ( this->alpha != NULL )
357                 free( this->alpha );
358         if ( this->markup != NULL )
359                 free( this->markup );
360         if ( this->text != NULL )
361                 free( this->text );
362         if ( this->font != NULL )
363                 free( this->font );
364         parent->close = NULL;
365         mlt_producer_close( parent );
366         free( this );
367 }
368
369 static void pango_draw_background( GdkPixbuf *pixbuf, rgba_color bg )
370 {
371         int ww = gdk_pixbuf_get_width( pixbuf );
372         int hh = gdk_pixbuf_get_height( pixbuf );
373         uint8_t *p = gdk_pixbuf_get_pixels( pixbuf );
374         int i, j;
375
376         for ( j = 0; j < hh; j++ )
377         {
378                 for ( i = 0; i < ww; i++ )
379                 {
380                         *p++ = bg.r;
381                         *p++ = bg.g;
382                         *p++ = bg.b;
383                         *p++ = bg.a;
384                 }
385         }
386 }
387
388 static GdkPixbuf *pango_get_pixbuf( const char *markup, const char *text, const char *font, rgba_color fg, rgba_color bg, int pad, int align )
389 {
390         PangoFT2FontMap *fontmap = (PangoFT2FontMap*) pango_ft2_font_map_new();
391         PangoContext *context = pango_ft2_font_map_create_context( fontmap );
392         PangoLayout *layout = pango_layout_new( context );
393         int w, h, x;
394         int i, j;
395         GdkPixbuf *pixbuf = NULL;
396         FT_Bitmap bitmap;
397         uint8_t *src = NULL;
398         uint8_t* dest = NULL;
399         int stride;
400
401         pango_ft2_font_map_set_resolution( fontmap, 72, 72 );
402         pango_layout_set_width( layout, -1 ); // set wrapping constraints
403         pango_layout_set_font_description( layout, pango_font_description_from_string( font ) );
404 //      pango_layout_set_spacing( layout, space );
405         pango_layout_set_alignment( layout, ( PangoAlignment ) align  );
406         if ( markup != NULL && strcmp( markup, "" ) != 0 )
407                 pango_layout_set_markup( layout, markup, strlen( markup ) );
408         else if ( text != NULL && strcmp( text, "" ) != 0 )
409                 pango_layout_set_text( layout, text, strlen( text ) );
410         else
411                 return NULL;
412         pango_layout_get_pixel_size( layout, &w, &h );
413
414         pixbuf = gdk_pixbuf_new( GDK_COLORSPACE_RGB, TRUE /* has alpha */, 8, w + 2 * pad, h + 2 * pad );
415         pango_draw_background( pixbuf, bg );
416
417         stride = gdk_pixbuf_get_rowstride( pixbuf );
418
419         bitmap.width     = w;
420         bitmap.pitch     = 32 * ( ( w + 31 ) / 31 );
421         bitmap.rows      = h;
422         bitmap.buffer    = ( unsigned char * ) calloc( 1, h * bitmap.pitch );
423         bitmap.num_grays = 256;
424         bitmap.pixel_mode = ft_pixel_mode_grays;
425
426         pango_ft2_render_layout( &bitmap, layout, 0, 0 );
427
428         src = bitmap.buffer;
429         x = ( gdk_pixbuf_get_width( pixbuf ) - w - 2 * pad ) * align / 2 + pad;
430         dest = gdk_pixbuf_get_pixels( pixbuf ) + 4 * x + pad * stride;
431         for ( j = 0; j < h; j++ )
432         {
433                 uint8_t *d = dest;
434                 for ( i = 0; i < w; i++ )
435                 {
436                         float a = ( float ) bitmap.buffer[ j * bitmap.pitch + i ] / 255.0;
437                         *d++ = ( int ) ( a * fg.r + ( 1 - a ) * bg.r );
438                         *d++ = ( int ) ( a * fg.g + ( 1 - a ) * bg.g );
439                         *d++ = ( int ) ( a * fg.b + ( 1 - a ) * bg.b );
440                         *d++ = ( int ) ( a * fg.a + ( 1 - a ) * bg.a );
441                 }
442                 dest += stride;
443         }
444         free( bitmap.buffer );
445
446         g_object_unref( layout );
447         g_object_unref( context );
448         g_object_unref( fontmap );
449
450         return pixbuf;
451 }
452