]> git.sesse.net Git - mlt/blob - src/modules/core/producer_colour.c
Fix crashing when using opengl services with wrapper producers.
[mlt] / src / modules / core / producer_colour.c
1 /*
2  * producer_colour.c -- raster image loader based upon gdk-pixbuf
3  * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4  * Author: Dan Dennedy <dan@dennedy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library 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 GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <framework/mlt_producer.h>
22 #include <framework/mlt_frame.h>
23 #include <framework/mlt_pool.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29
30 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
31 static void producer_close( mlt_producer parent );
32
33 mlt_producer producer_colour_init( mlt_profile profile, mlt_service_type type, const char *id, char *colour )
34 {
35         mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
36         if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
37         {
38                 // Get the properties interface
39                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
40         
41                 // Callback registration
42                 producer->get_frame = producer_get_frame;
43                 producer->close = ( mlt_destructor )producer_close;
44
45                 // Set the default properties
46                 mlt_properties_set( properties, "resource", ( !colour || !strcmp( colour, "" ) ) ? "0x000000ff" : colour );
47                 mlt_properties_set( properties, "_resource", "" );
48                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
49                 
50                 return producer;
51         }
52         free( producer );
53         return NULL;
54 }
55
56 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
57 {
58         // Obtain properties of frame
59         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
60
61         // Obtain the producer for this frame
62         mlt_producer producer = mlt_properties_get_data( properties, "producer_colour", NULL );
63
64         mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
65
66         // Obtain properties of producer
67         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
68
69         // Get the current and previous colour strings
70         char *now = mlt_properties_get( producer_props, "resource" );
71         char *then = mlt_properties_get( producer_props, "_resource" );
72
73         // Get the current image and dimensions cached in the producer
74         int size = 0;
75         uint8_t *image = mlt_properties_get_data( producer_props, "image", &size );
76         int current_width = mlt_properties_get_int( producer_props, "_width" );
77         int current_height = mlt_properties_get_int( producer_props, "_height" );
78         mlt_image_format current_format = mlt_properties_get_int( producer_props, "_format" );
79
80         // Parse the colour
81         if ( now && strchr( now, '/' ) )
82         {
83                 now = strdup( strrchr( now, '/' ) + 1 );
84                 mlt_properties_set( producer_props, "resource", now );
85                 free( now );
86                 now = mlt_properties_get( producer_props, "resource" );
87         }
88         mlt_color color = mlt_properties_get_color( producer_props, "resource" );
89
90         // Choose suitable out values if nothing specific requested
91         if ( *format == mlt_image_none || *format == mlt_image_glsl )
92                 *format = mlt_image_rgb24a;
93         if ( *width <= 0 )
94                 *width = mlt_service_profile( MLT_PRODUCER_SERVICE(producer) )->width;
95         if ( *height <= 0 )
96                 *height = mlt_service_profile( MLT_PRODUCER_SERVICE(producer) )->height;
97
98         // See if we need to regenerate
99         if ( strcmp( now, then ) || *width != current_width || *height != current_height || *format != current_format )
100         {
101                 // Color the image
102                 int i = *width * *height + 1;
103                 int bpp;
104
105                 // Allocate the image
106                 size = mlt_image_format_size( *format, *width, *height, &bpp );
107                 uint8_t *p = image = mlt_pool_alloc( size );
108
109                 // Update the producer
110                 mlt_properties_set_data( producer_props, "image", image, size, mlt_pool_release, NULL );
111                 mlt_properties_set_int( producer_props, "_width", *width );
112                 mlt_properties_set_int( producer_props, "_height", *height );
113                 mlt_properties_set_int( producer_props, "_format", *format );
114                 mlt_properties_set( producer_props, "_resource", now );
115
116                 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
117
118                 switch ( *format )
119                 {
120                 case mlt_image_yuv422:
121                 {
122                         int uneven = *width % 2;
123                         int count = ( *width - uneven ) / 2 + 1;
124                         uint8_t y, u, v;
125
126                         RGB2YUV_601_SCALED( color.r, color.g, color.b, y, u, v );
127                         i = *height + 1;
128                         while ( --i )
129                         {
130                                 int j = count;
131                                 while ( --j )
132                                 {
133                                         *p ++ = y;
134                                         *p ++ = u;
135                                         *p ++ = y;
136                                         *p ++ = v;
137                                 }
138                                 if ( uneven )
139                                 {
140                                         *p ++ = y;
141                                         *p ++ = u;
142                                 }
143                         }
144                         break;
145                 }
146                 case mlt_image_rgb24:
147                         while ( --i )
148                         {
149                                 *p ++ = color.r;
150                                 *p ++ = color.g;
151                                 *p ++ = color.b;
152                         }
153                         break;
154                 case mlt_image_glsl:
155                 case mlt_image_glsl_texture:
156                         memset(p, 0, size);
157                         break;
158                 default:
159                         *format = mlt_image_rgb24a;
160                         while ( --i )
161                         {
162                                 *p ++ = color.r;
163                                 *p ++ = color.g;
164                                 *p ++ = color.b;
165                                 *p ++ = color.a;
166                         }
167                         break;
168                 }
169         }
170         else
171         {
172                 mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
173         }
174
175         // Create the alpha channel
176         int alpha_size = *width * *height;
177         uint8_t *alpha = mlt_pool_alloc( alpha_size );
178
179         // Initialise the alpha
180         if ( alpha )
181                 memset( alpha, color.a, alpha_size );
182
183         // Clone our image
184         *buffer = mlt_pool_alloc( size );
185         memcpy( *buffer, image, size );
186
187         // Now update properties so we free the copy after
188         mlt_frame_set_image( frame, *buffer, size, mlt_pool_release );
189         mlt_frame_set_alpha( frame, alpha, alpha_size, mlt_pool_release );
190         mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
191         mlt_properties_set_int( properties, "meta.media.width", *width );
192         mlt_properties_set_int( properties, "meta.media.height", *height );
193
194
195         return 0;
196 }
197
198 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
199 {
200         // Generate a frame
201         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
202
203         if ( *frame != NULL )
204         {
205                 // Obtain properties of frame and producer
206                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
207
208                 // Obtain properties of producer
209                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
210
211                 // Set the producer on the frame properties
212                 mlt_properties_set_data( properties, "producer_colour", producer, 0, NULL, NULL );
213
214                 // Update timecode on the frame we're creating
215                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
216
217                 // Set producer-specific frame properties
218                 mlt_properties_set_int( properties, "progressive", 1 );
219                 mlt_profile profile = mlt_service_profile( MLT_PRODUCER_SERVICE( producer ) );
220                 mlt_properties_set_double( properties, "aspect_ratio", mlt_profile_sar( profile ) );
221
222                 // colour is an alias for resource
223                 if ( mlt_properties_get( producer_props, "colour" ) != NULL )
224                         mlt_properties_set( producer_props, "resource", mlt_properties_get( producer_props, "colour" ) );
225                 
226                 // Push the get_image method
227                 mlt_frame_push_get_image( *frame, producer_get_image );
228         }
229
230         // Calculate the next timecode
231         mlt_producer_prepare_next( producer );
232
233         return 0;
234 }
235
236 static void producer_close( mlt_producer producer )
237 {
238         producer->close = NULL;
239         mlt_producer_close( producer );
240         free( producer );
241 }