Merge ../mlt
[melted] / src / modules / fezzik / producer_hold.c
1 /*
2 * producer_hold.c -- frame holding producer
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <framework/mlt.h>
26
27 // Forward references
28 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index );
29 static void producer_close( mlt_producer this );
30
31 /** Constructor for the frame holding producer. Basically, all this producer does is
32 provide a producer wrapper for the requested producer, allows the specifcation of
33 the frame required and will then repeatedly obtain that frame for each get_frame
34 and get_image requested.
35 */
36
37 mlt_producer producer_hold_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
38 {
39 // Construct a new holding producer
40 mlt_producer this = mlt_producer_new( );
41
42 // Construct the requested producer via fezzik
43 mlt_producer producer = mlt_factory_producer( profile, "fezzik", arg );
44
45 // Initialise the frame holding capabilities
46 if ( this != NULL && producer != NULL )
47 {
48 // Get the properties of this producer
49 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
50
51 // Store the producer
52 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
53
54 // Set frame, in, out and length for this producer
55 mlt_properties_set_position( properties, "frame", 0 );
56 mlt_properties_set_position( properties, "in", 0 );
57 mlt_properties_set_position( properties, "out", 25 );
58 mlt_properties_set_position( properties, "length", 15000 );
59 mlt_properties_set( properties, "resource", arg );
60 mlt_properties_set( properties, "method", "onefield" );
61
62 // Override the get_frame method
63 this->get_frame = producer_get_frame;
64 this->close = ( mlt_destructor )producer_close;
65 }
66 else
67 {
68 // Clean up (not sure which one failed, can't be bothered to find out, so close both)
69 if ( this )
70 mlt_producer_close( this );
71 if ( producer )
72 mlt_producer_close( producer );
73
74 // Make sure we return NULL
75 this = NULL;
76 }
77
78 // Return this producer
79 return this;
80 }
81
82 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
83 {
84 // Get the properties of the frame
85 mlt_properties properties = MLT_FRAME_PROPERTIES( frame );
86
87 // Obtain the real frame
88 mlt_frame real_frame = mlt_frame_pop_service( frame );
89
90 // Get the image from the real frame
91 int size = 0;
92 *buffer = mlt_properties_get_data( MLT_FRAME_PROPERTIES( real_frame ), "image", &size );
93 *width = mlt_properties_get_int( MLT_FRAME_PROPERTIES( real_frame ), "width" );
94 *height = mlt_properties_get_int( MLT_FRAME_PROPERTIES( real_frame ), "height" );
95
96 // If this is the first time, get it from the producer
97 if ( *buffer == NULL )
98 {
99 mlt_properties_pass( MLT_FRAME_PROPERTIES( real_frame ), properties, "" );
100
101 // We'll deinterlace on the downstream deinterlacer
102 mlt_properties_set_int( MLT_FRAME_PROPERTIES( real_frame ), "consumer_deinterlace", 1 );
103
104 // We want distorted to ensure we don't hit the resize filter twice
105 mlt_properties_set_int( MLT_FRAME_PROPERTIES( real_frame ), "distort", 1 );
106
107 // Get the image
108 mlt_frame_get_image( real_frame, buffer, format, width, height, writable );
109
110 // Make sure we get the size
111 *buffer = mlt_properties_get_data( MLT_FRAME_PROPERTIES( real_frame ), "image", &size );
112 }
113
114 mlt_properties_pass( properties, MLT_FRAME_PROPERTIES( real_frame ), "" );
115
116 // Set the values obtained on the frame
117 if ( *buffer != NULL )
118 {
119 uint8_t *image = mlt_pool_alloc( size );
120 memcpy( image, *buffer, size );
121 *buffer = image;
122 mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
123 }
124 else
125 {
126 // Pass the current image as is
127 mlt_properties_set_data( properties, "image", *buffer, size, NULL, NULL );
128 }
129
130 // Make sure that no further scaling is done
131 mlt_properties_set( properties, "rescale.interps", "none" );
132 mlt_properties_set( properties, "scale", "off" );
133
134 // All done
135 return 0;
136 }
137
138 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
139 {
140 // Get the properties of this producer
141 mlt_properties properties = MLT_PRODUCER_PROPERTIES( this );
142
143 // Construct a new frame
144 *frame = mlt_frame_init( MLT_PRODUCER_SERVICE( this ) );
145
146 // If we have a frame, then stack the producer itself and the get_image method
147 if ( *frame != NULL )
148 {
149 // Define the real frame
150 mlt_frame real_frame = mlt_properties_get_data( properties, "real_frame", NULL );
151
152 // Obtain real frame if we don't have it
153 if ( real_frame == NULL )
154 {
155 // Get the producer
156 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
157
158 // Get the frame position requested
159 mlt_position position = mlt_properties_get_position( properties, "frame" );
160
161 // Seek the producer to the correct place
162 mlt_producer_seek( producer, position );
163
164 // Get the real frame
165 mlt_service_get_frame( MLT_PRODUCER_SERVICE( producer ), &real_frame, index );
166
167 // Ensure that the real frame gets wiped eventually
168 mlt_properties_set_data( properties, "real_frame", real_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
169 }
170 else
171 {
172 // Temporary fix - ensure that we aren't seen as a test frame
173 int8_t *image = mlt_properties_get_data( MLT_FRAME_PROPERTIES( real_frame ), "image", NULL );
174 mlt_properties_set_data( MLT_FRAME_PROPERTIES( *frame ), "image", image, 0, NULL, NULL );
175 mlt_properties_set_int( MLT_FRAME_PROPERTIES( *frame ), "test_image", 0 );
176 }
177
178 // Stack the real frame and method
179 mlt_frame_push_service( *frame, real_frame );
180 mlt_frame_push_service( *frame, producer_get_image );
181
182 // Ensure that the consumer sees what the real frame has
183 mlt_properties_pass( MLT_FRAME_PROPERTIES( *frame ), MLT_FRAME_PROPERTIES( real_frame ), "" );
184
185 mlt_properties_set( MLT_FRAME_PROPERTIES( real_frame ), "deinterlace_method",
186 mlt_properties_get( properties, "method" ) );
187 }
188
189 // Move to the next position
190 mlt_producer_prepare_next( this );
191
192 return 0;
193 }
194
195 static void producer_close( mlt_producer this )
196 {
197 this->close = NULL;
198 mlt_producer_close( this );
199 free( this );
200 }
201