]> git.sesse.net Git - mlt/blob - src/modules/core/producer_colour.c
Minor tweaks
[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 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_colour.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 typedef struct
31 {
32         uint8_t r, g, b, a;
33 } rgba_color;
34
35 static int producer_get_frame( mlt_producer parent, mlt_frame_ptr frame, int index );
36 static void producer_close( mlt_producer parent );
37
38 mlt_producer producer_colour_init( char *colour )
39 {
40         mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
41         if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
42         {
43                 // Get the properties interface
44                 mlt_properties properties = mlt_producer_properties( producer );
45         
46                 // Callback registration
47                 producer->get_frame = producer_get_frame;
48                 producer->close = producer_close;
49
50                 // Set the default properties
51                 mlt_properties_set( properties, "resource", colour == NULL ? "0x000000ff" : colour );
52                 
53                 return producer;
54         }
55         free( producer );
56         return NULL;
57 }
58
59 rgba_color parse_color( char *color )
60 {
61         rgba_color result = { 0xff, 0xff, 0xff, 0xff };
62
63         if ( !strncmp( color, "0x", 2 ) )
64         {
65                 unsigned int temp = 0;
66                 sscanf( color + 2, "%x", &temp );
67                 result.r = ( temp >> 24 ) & 0xff;
68                 result.g = ( temp >> 16 ) & 0xff;
69                 result.b = ( temp >> 8 ) & 0xff;
70                 result.a = ( temp ) & 0xff;
71         }
72         else if ( !strcmp( color, "red" ) )
73         {
74                 result.r = 0xff;
75                 result.g = 0x00;
76                 result.b = 0x00;
77         }
78         else if ( !strcmp( color, "green" ) )
79         {
80                 result.r = 0x00;
81                 result.g = 0xff;
82                 result.b = 0x00;
83         }
84         else if ( !strcmp( color, "blue" ) )
85         {
86                 result.r = 0x00;
87                 result.g = 0x00;
88                 result.b = 0xff;
89         }
90         else if ( strcmp( color, "white" ) )
91         {
92                 unsigned int temp = 0;
93                 sscanf( color, "%d", &temp );
94                 result.r = ( temp >> 24 ) & 0xff;
95                 result.g = ( temp >> 16 ) & 0xff;
96                 result.b = ( temp >> 8 ) & 0xff;
97                 result.a = ( temp ) & 0xff;
98         }
99
100         return result;
101 }
102
103 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
104 {
105         // May need to know the size of the image to clone it
106         int size = 0;
107         
108         // Obtain properties of frame
109         mlt_properties properties = mlt_frame_properties( frame );
110
111         // Obtain the producer for this frame
112         mlt_producer producer = mlt_properties_get_data( properties, "producer_colour", NULL );
113
114         // Obtain properties of producer
115         mlt_properties producer_props = mlt_producer_properties( producer );
116
117         // Get the current image and dimensions cached in the producer
118         uint8_t *image = mlt_properties_get_data( producer_props, "image", &size );
119         int current_width = mlt_properties_get_int( producer_props, "width" );
120         int current_height = mlt_properties_get_int( producer_props, "height" );
121
122         // See if we need to regenerate
123         if ( *width != current_width || *height != current_height )
124         {
125                 // Allocate the image
126                 size = *width * *height * 2;
127                 image = mlt_pool_alloc( size );
128
129                 // Update the producer
130                 mlt_properties_set_data( producer_props, "image", image, size, mlt_pool_release, NULL );
131                 mlt_properties_set_int( producer_props, "width", *width );
132                 mlt_properties_set_int( producer_props, "height", *height );
133
134                 // Color the image
135                 rgba_color color = parse_color( mlt_properties_get( producer_props, "resource" ) );
136                 uint8_t y, u, v;
137                 int i = 0;
138                 RGB2YUV( color.r, color.g, color.b, y, u, v );
139
140                 while ( i < size )
141                 {
142                         image[ i ++ ] = y;
143                         image[ i ++ ] = u;
144                         image[ i ++ ] = y;
145                         image[ i ++ ] = v;
146                 }
147         }
148
149         // Update the frame
150         mlt_properties_set_data( properties, "image", image, size, NULL, NULL );
151         mlt_properties_set_int( properties, "width", *width );
152         mlt_properties_set_int( properties, "height", *height );
153         
154         // Clone if necessary
155         if ( writable )
156         {
157                 // Clone our image
158                 uint8_t *copy = mlt_pool_alloc( size );
159                 memcpy( copy, image, size );
160
161                 // We're going to pass the copy on
162                 image = copy;
163
164                 // Now update properties so we free the copy after
165                 mlt_properties_set_data( properties, "image", copy, size, mlt_pool_release, NULL );
166         }
167
168         // Pass on the image
169         *buffer = image;
170         *format = mlt_image_yuv422;
171
172         return 0;
173 }
174
175 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
176 {
177         // Generate a frame
178         *frame = mlt_frame_init( );
179
180         if ( *frame != NULL )
181         {
182                 // Obtain properties of frame and producer
183                 mlt_properties properties = mlt_frame_properties( *frame );
184
185                 // Obtain properties of producer
186                 mlt_properties producer_props = mlt_producer_properties( producer );
187
188                 // Determine if we're producing PAL or NTSC
189                 int is_pal = mlt_properties_get_double( producer_props, "fps" ) == 25.0;
190
191                 // Set the producer on the frame properties
192                 mlt_properties_set_data( properties, "producer_colour", producer, 0, NULL, NULL );
193
194                 // Update timecode on the frame we're creating
195                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
196
197                 // Set producer-specific frame properties
198                 mlt_properties_set_int( properties, "progressive", 1 );
199                 mlt_properties_set_double( properties, "aspect_ratio", is_pal ? 128.0/117.0 : 72.0/79.0 );
200
201                 // colour is an alias for resource
202                 if ( mlt_properties_get( producer_props, "colour" ) != NULL )
203                         mlt_properties_set( producer_props, "resource", mlt_properties_get( producer_props, "colour" ) );
204                 
205                 // Push the get_image method
206                 mlt_frame_push_get_image( *frame, producer_get_image );
207         }
208
209         // Calculate the next timecode
210         mlt_producer_prepare_next( producer );
211
212         return 0;
213 }
214
215 static void producer_close( mlt_producer producer )
216 {
217         producer->close = NULL;
218         mlt_producer_close( producer );
219         free( producer );
220 }