]> git.sesse.net Git - mlt/blob - src/modules/qimage/producer_kdenlivetitle.c
Rescale title when they are played with a different profile
[mlt] / src / modules / qimage / producer_kdenlivetitle.c
1 /*
2  * producer_kdenlivetitle.c -- kdenlive producer
3  * Copyright (c) 2009 Marco Gittler <g.marco@freenet.de>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  */
19
20 #include <framework/mlt.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24
25 extern void refresh_kdenlivetitle( mlt_producer producer, uint8_t*, int, int, double, int );
26
27 void read_xml(mlt_properties properties)
28 {
29         FILE *f = fopen( mlt_properties_get( properties, "resource" ), "r");
30         if ( f != NULL )
31         {
32                 int size = 0;
33                 long lSize;
34  
35                 fseek (f , 0 , SEEK_END);
36                 lSize = ftell (f);
37                 rewind (f);
38
39                 char *infile = (char*) mlt_pool_alloc(lSize);
40                 size=fread(infile,1,lSize,f);
41                 infile[size] = '\0';
42                 fclose(f);
43                 mlt_properties_set(properties, "xmldata", infile);
44                 mlt_pool_release( infile );
45         }
46 }
47
48 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
49 {
50         /* Obtain properties of frame */
51         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
52
53         /* Obtain the producer for this frame */
54         mlt_producer producer = mlt_properties_get_data( properties, "producer_kdenlivetitle", NULL );
55
56         /* Obtain properties of producer */
57         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
58         
59         *width = mlt_properties_get_int( properties, "width" );
60         *height = mlt_properties_get_int( properties, "height" );
61         
62         /* Allocate the image */
63         int size = *width * ( *height ) * 4;
64
65         *buffer = mlt_pool_alloc( size );
66
67         mlt_properties_set_int( properties, "width", *width );
68         mlt_properties_set_int( properties, "height", *height );
69
70         /* cache later ?? */
71
72         if ( 1 )
73         {
74                 /* Allocate the image */
75                 *format = mlt_image_rgb24a;
76                 mlt_position in = mlt_producer_get_in( producer );
77                 mlt_position out = mlt_producer_get_out( producer );
78                 mlt_position time = mlt_producer_position( producer );
79                 double position = ( double )( time - in ) / ( double )( out - in + 1 );
80                 if ( mlt_properties_get_int( producer_props, "force_reload" ) ) {
81                         if (mlt_properties_get_int( producer_props, "force_reload" ) > 1) read_xml(producer_props);
82                         mlt_properties_set_int( producer_props, "force_reload", 0 );
83                         refresh_kdenlivetitle( producer, *buffer, *width, *height, position, 1);
84                 }
85                 else refresh_kdenlivetitle( producer, *buffer, *width, *height, position, 0);
86                 /* Update the frame */
87                 mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
88
89                 mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "width:%d height:%d %s\n", *width, *height, mlt_image_format_name( *format ) );
90         }
91
92         return 0;
93 }
94
95 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
96
97 {
98
99         /* Generate a frame */
100         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
101
102         if ( *frame != NULL )
103         {
104                 /* Obtain properties of frame and producer */
105                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
106
107                 /* Obtain properties of producer */
108                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
109
110                 /* Set the producer on the frame properties */
111                 mlt_properties_set_data( properties, "producer_kdenlivetitle", producer, 0, NULL, NULL );
112
113                 /* Update timecode on the frame we're creating */
114                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
115
116                 /* Set producer-specific frame properties */
117                 mlt_properties_set_int( properties, "progressive", 1 );
118                 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
119
120                 /* Push the get_image method */
121                 mlt_frame_push_get_image( *frame, producer_get_image );
122         }
123
124         /* Calculate the next timecode */
125         mlt_producer_prepare_next( producer );
126
127         return 0;
128 }
129
130 static void producer_close( mlt_producer producer )
131 {
132         /* fprintf(stderr, "::::::::::::::  CLOSING TITLE\n"); */
133         producer->close = NULL;
134         mlt_producer_close( producer );
135         free( producer );
136 }
137
138
139 mlt_producer producer_kdenlivetitle_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
140 {
141         /* fprintf(stderr, ":::::::::::: CREATE TITLE\n"); */
142         /* Create a new producer object */
143
144         mlt_producer producer = calloc( 1, sizeof( struct mlt_producer_s ) );
145         if ( producer != NULL && mlt_producer_init( producer, NULL ) == 0 )
146         {
147                 /* Get the properties interface */
148                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
149                 /* Callback registration */
150                 producer->get_frame = producer_get_frame;
151                 producer->close = ( mlt_destructor )producer_close;
152                 mlt_properties_set( properties, "resource", filename );
153                 //mlt_properties_set_int( properties, "aspect_ratio", 1 );
154                 read_xml(properties);
155                 return producer;
156         }
157         free( producer );
158         return NULL;
159 }
160