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