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