]> git.sesse.net Git - mlt/blob - src/modules/qimage/producer_kdenlivetitle.c
6a90abde67730964e9d57492e3ee5fdc49848e2c
[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  * Copyright (c) 2009 Jean-Baptiste Mardelle <jb@kdenlive.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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "kdenlivetitle_wrapper.h"
22
23 #include <framework/mlt.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27
28 void read_xml(mlt_properties properties)
29 {
30         FILE *f = fopen( mlt_properties_get( properties, "resource" ), "r");
31         if ( f != NULL )
32         {
33                 int size = 0;
34                 long lSize;
35  
36                 fseek (f , 0 , SEEK_END);
37                 lSize = ftell (f);
38                 rewind (f);
39
40                 char *infile = (char*) mlt_pool_alloc(lSize);
41                 size=fread(infile,1,lSize,f);
42                 infile[size] = '\0';
43                 fclose(f);
44                 mlt_properties_set(properties, "_xmldata", infile);
45                 mlt_pool_release( infile );
46         }
47 }
48
49 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
50 {
51         /* Obtain properties of frame */
52         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
53
54         /* Obtain the producer for this frame */
55         producer_ktitle this = mlt_properties_get_data( properties, "producer_kdenlivetitle", NULL );
56         
57         /* Obtain properties of producer */
58         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( &this->parent );
59         
60         *width = mlt_properties_get_int( properties, "rescale_width" );
61         *height = mlt_properties_get_int( properties, "rescale_height" );
62         
63         /* Allocate the image */
64         int size = *width * ( *height ) * 4;
65
66         mlt_service_lock( MLT_PRODUCER_SERVICE( &this->parent ) );
67
68         /* Allocate the image */
69         *format = mlt_image_rgb24a;
70         mlt_position time = mlt_producer_position( &this->parent ) + mlt_producer_get_in( &this->parent );
71         if ( mlt_properties_get_int( producer_props, "force_reload" ) ) {
72                 if (mlt_properties_get_int( producer_props, "force_reload" ) > 1) read_xml(producer_props);
73                 mlt_properties_set_int( producer_props, "force_reload", 0 );
74                 drawKdenliveTitle( this, frame, *width, *height, time, 1);
75         }
76         else drawKdenliveTitle( this, frame, *width, *height, time, 0);
77
78         // Get width and height (may have changed during the refresh)
79         *width = mlt_properties_get_int( properties, "width" );
80         *height = mlt_properties_get_int( properties, "height" );
81                 
82         if ( this->current_image )
83         {
84                 // Clone the image and the alpha
85                 int image_size = this->current_width * ( this->current_height ) * 4;
86                 uint8_t *image_copy = mlt_pool_alloc( image_size );
87                 memcpy( image_copy, this->current_image, image_size );
88                 // Now update properties so we free the copy after
89                 mlt_properties_set_data( properties, "image", image_copy, image_size, mlt_pool_release, NULL );
90                 // We're going to pass the copy on
91                 *buffer = image_copy;           
92                 
93                 /* Update the frame */
94                 mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
95
96                 mlt_log_debug( MLT_PRODUCER_SERVICE( &this->parent ), "width:%d height:%d %s\n", *width, *height, mlt_image_format_name( *format ) );
97         }
98
99         mlt_service_unlock( MLT_PRODUCER_SERVICE( &this->parent ) );
100
101         return 0;
102 }
103
104 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
105
106 {
107         // Get the real structure for this producer
108         producer_ktitle this = producer->child;
109
110         /* Generate a frame */
111         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
112
113         if ( *frame != NULL )
114         {
115                 /* Obtain properties of frame and producer */
116                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
117
118                 /* Obtain properties of producer */
119                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
120
121                 /* Set the producer on the frame properties */
122                 mlt_properties_set_data( properties, "producer_kdenlivetitle", this, 0, NULL, NULL );
123
124                 /* Update timecode on the frame we're creating */
125                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
126
127                 /* Set producer-specific frame properties */
128                 mlt_profile profile = mlt_service_profile ( MLT_PRODUCER_SERVICE( producer ) ) ;
129                 mlt_properties_set_int( properties, "progressive", ( profile ) ? profile->progressive : 1 );
130                 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
131
132                 /* Push the get_image method */
133                 mlt_frame_push_get_image( *frame, producer_get_image );
134         }
135
136         /* Calculate the next timecode */
137         mlt_producer_prepare_next( producer );
138
139         return 0;
140 }
141
142 static void producer_close( mlt_producer producer )
143 {
144         /* fprintf(stderr, "::::::::::::::  CLOSING TITLE\n"); */
145         producer->close = NULL;
146         mlt_producer_close( producer );
147         free( producer );
148 }
149
150
151 mlt_producer producer_kdenlivetitle_init( mlt_profile profile, mlt_service_type type, const char *id, char *filename )
152 {
153         /* fprintf(stderr, ":::::::::::: CREATE TITLE\n"); */
154         /* Create a new producer object */
155         
156         producer_ktitle this = calloc( sizeof( struct producer_ktitle_s ), 1 );
157         if ( this != NULL && mlt_producer_init( &this->parent, this ) == 0 )
158         {
159                 mlt_producer producer = &this->parent;
160                 
161                 /* Get the properties interface */
162                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
163                 /* Callback registration */
164                 producer->get_frame = producer_get_frame;
165                 producer->close = ( mlt_destructor )producer_close;
166                 mlt_properties_set( properties, "resource", filename );
167                 //mlt_properties_set_int( properties, "aspect_ratio", 1 );
168                 read_xml(properties);
169                 return producer;
170         }
171         free( this );
172         return NULL;
173 }
174