]> git.sesse.net Git - mlt/blob - src/modules/qimage/producer_kdenlivetitle.c
Fix crash + position in time
[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 extern void init_qt();
25
26 extern void refresh_kdenlivetitle( uint8_t*, int, int, double, char*, char*, int );
27
28 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
29 {
30
31         // Obtain properties of frame
32         mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
33
34         // Obtain the producer for this frame
35         mlt_producer producer = mlt_properties_get_data( properties, "producer_kdenlivetitle", NULL );
36
37         // Obtain properties of producer
38         mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
39         
40         // Allocate the image
41         int size = *width * ( *height ) * 4;
42
43         *buffer = mlt_pool_alloc( size );
44         // Update the frame
45         mlt_properties_set_int( properties, "width", *width );
46         mlt_properties_set_int( properties, "height", *height );
47
48         // Update the frame
49         mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
50         //cache later ??
51
52         if ( 1 )
53         {
54                 // Allocate the image
55                 *format = mlt_image_rgb24a;
56                 mlt_position in = mlt_producer_get_in( producer );
57                 mlt_position out = mlt_producer_get_out( producer );
58                 mlt_position time = mlt_producer_position( producer );
59                 double position = ( double )( time - in ) / ( double )( out - in + 1 );
60                 fprintf(stderr, "TITLE POS: %dx%d = %d\n", (int)in, (int)out, (int) time);
61                 if ( mlt_properties_get_int( producer_props, "force_reload" ) ) {
62                         refresh_kdenlivetitle( *buffer, *width, *height, position, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ),  1);
63                         mlt_properties_set_int( producer_props, "force_reload", 0 );
64                 }
65                 else refresh_kdenlivetitle( *buffer, *width, *height, position, mlt_properties_get( producer_props, "xmldata" ), mlt_properties_get( producer_props, "templatetext" ),  0);
66                 mlt_log_debug( MLT_PRODUCER_SERVICE( producer ), "width:%d height:%d %s\n", *width, *height, mlt_image_format_name( *format ) );
67         }
68
69         return 0;
70 }
71
72 int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
73
74 {
75
76         // Generate a frame
77         *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
78
79         if ( *frame != NULL )
80         {
81                 // Obtain properties of frame and producer
82                 mlt_properties properties = MLT_FRAME_PROPERTIES( *frame );
83
84                 // Obtain properties of producer
85                 mlt_properties producer_props = MLT_PRODUCER_PROPERTIES( producer );
86
87                 // Set the producer on the frame properties
88                 mlt_properties_set_data( properties, "producer_kdenlivetitle", producer, 0, NULL, NULL );
89
90                 // Update timecode on the frame we're creating
91                 mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
92
93                 // Set producer-specific frame properties
94                 mlt_properties_set_int( properties, "progressive", 1 );
95                 mlt_properties_set_double( properties, "aspect_ratio", mlt_properties_get_double( producer_props, "aspect_ratio" ) );
96
97                 // Push the get_image method
98                 mlt_frame_push_get_image( *frame, producer_get_image );
99         }
100
101         // Calculate the next timecode
102         mlt_producer_prepare_next( producer );
103
104         return 0;
105 }
106
107 void producer_close( mlt_producer producer )
108 {
109         producer->close = NULL;
110         mlt_producer_close( producer );
111
112         free( producer );
113 }
114
115 mlt_producer producer_kdenlivetitle_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
116 {
117         // Create a new producer object
118         mlt_producer this = mlt_producer_new( );
119
120         // Initialise the producer
121
122         if ( this != NULL )
123         {
124                 init_qt( arg );
125                 // Callback registration
126                 this->get_frame = producer_get_frame;
127                 this->close = ( mlt_destructor ) producer_close;
128                 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
129                 mlt_properties_set( properties, "resource", arg );
130         }
131
132         return this;
133 }
134