hold modifications and test card env var
[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
32 /** Constructor for the frame holding producer. Basically, all this producer does is
33 provide a producer wrapper for the requested producer, allows the specifcation of
34 the frame required and will then repeatedly obtain that frame for each get_frame
35 and get_image requested.
36 */
37
38 mlt_producer producer_hold_init( char *arg )
39 {
40 // Construct a new holding producer
41 mlt_producer this = mlt_producer_new( );
42
43 // Construct the requested producer via fezzik
44 mlt_producer producer = mlt_factory_producer( "fezzik", arg );
45
46 // Initialise the frame holding capabilities
47 if ( this != NULL && producer != NULL )
48 {
49 // Get the properties of this producer
50 mlt_properties properties = mlt_producer_properties( this );
51
52 // Store the producer
53 mlt_properties_set_data( properties, "producer", producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
54
55 // Set frame, in, out and length for this producer
56 mlt_properties_set_position( properties, "frame", 0 );
57 mlt_properties_set_position( properties, "in", 0 );
58 mlt_properties_set_position( properties, "out", 25 );
59 mlt_properties_set_position( properties, "length", 15000 );
60 mlt_properties_set( properties, "resource", arg );
61
62 // Override the get_frame method
63 this->get_frame = producer_get_frame;
64 }
65 else
66 {
67 // Clean up (not sure which one failed, can't be bothered to find out, so close both)
68 if ( this )
69 mlt_producer_close( this );
70 if ( producer )
71 mlt_producer_close( producer );
72
73 // Make sure we return NULL
74 this = NULL;
75 }
76
77 // Return this producer
78 return this;
79 }
80
81 static int producer_get_image( mlt_frame frame, uint8_t **buffer, mlt_image_format *format, int *width, int *height, int writable )
82 {
83 // Get the properties of the frame
84 mlt_properties properties = mlt_frame_properties( frame );
85
86 // Obtain the real frame
87 mlt_frame real_frame = mlt_frame_pop_service( frame );
88
89 // Get the image from the real frame
90 int size = 0;
91 *buffer = mlt_properties_get_data( mlt_frame_properties( real_frame ), "image", &size );
92 *width = mlt_properties_get_int( mlt_frame_properties( real_frame ), "width" );
93 *height = mlt_properties_get_int( mlt_frame_properties( real_frame ), "height" );
94
95 // If this is the first time, get it from the producer
96 if ( *buffer == NULL )
97 {
98 mlt_properties_pass( mlt_frame_properties( real_frame ), properties, "" );
99
100 // We'll deinterlace on the downstream deinterlacer
101 mlt_properties_set_int( mlt_frame_properties( real_frame ), "consumer_deinterlace", 1 );
102
103 // We want distorted to ensure we don't hit the resize filter twice
104 mlt_properties_set_int( mlt_frame_properties( real_frame ), "distort", 1 );
105
106 // Get the image
107 mlt_frame_get_image( real_frame, buffer, format, width, height, writable );
108
109 // Make sure we get the size
110 *buffer = mlt_properties_get_data( mlt_frame_properties( real_frame ), "image", &size );
111 }
112
113 mlt_properties_pass( properties, mlt_frame_properties( real_frame ), "" );
114
115 // Set the values obtained on the frame
116 if ( *buffer != NULL )
117 {
118 uint8_t *image = mlt_pool_alloc( size );
119 memcpy( image, *buffer, size );
120 *buffer = image;
121 mlt_properties_set_data( properties, "image", *buffer, size, mlt_pool_release, NULL );
122 }
123 else
124 {
125 // Pass the current image as is
126 mlt_properties_set_data( properties, "image", *buffer, size, NULL, NULL );
127 }
128
129 // Make sure that no further scaling is done
130 mlt_properties_set( properties, "rescale.interps", "none" );
131 mlt_properties_set( properties, "scale", "off" );
132
133 // All done
134 return 0;
135 }
136
137 static int producer_get_frame( mlt_producer this, mlt_frame_ptr frame, int index )
138 {
139 // Get the properties of this producer
140 mlt_properties properties = mlt_producer_properties( this );
141
142 // Construct a new frame
143 *frame = mlt_frame_init( );
144
145 // If we have a frame, then stack the producer itself and the get_image method
146 if ( *frame != NULL )
147 {
148 // Define the real frame
149 mlt_frame real_frame = mlt_properties_get_data( properties, "real_frame", NULL );
150
151 // Obtain real frame if we don't have it
152 if ( real_frame == NULL )
153 {
154 // Get the producer
155 mlt_producer producer = mlt_properties_get_data( properties, "producer", NULL );
156
157 // Get the frame position requested
158 mlt_position position = mlt_properties_get_position( properties, "frame" );
159
160 // Seek the producer to the correct place
161 mlt_producer_seek( producer, position );
162
163 // Get the real frame
164 mlt_service_get_frame( mlt_producer_service( producer ), &real_frame, index );
165
166 // Ensure that the real frame gets wiped eventually
167 mlt_properties_set_data( properties, "real_frame", real_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
168 }
169 else
170 {
171 // Temporary fix - ensure that we aren't seen as a test frame
172 int8_t *image = mlt_properties_get_data( mlt_frame_properties( real_frame ), "image", NULL );
173 mlt_properties_set_data( mlt_frame_properties( *frame ), "image", image, 0, NULL, NULL );
174 mlt_properties_set_int( mlt_frame_properties( *frame ), "test_image", 0 );
175 }
176
177 // Stack the real frame and method
178 mlt_frame_push_service( *frame, real_frame );
179 mlt_frame_push_service( *frame, producer_get_image );
180
181 // Ensure that the consumer sees what the real frame has
182 mlt_properties_pass( mlt_frame_properties( *frame ), mlt_frame_properties( real_frame ), "" );
183 }
184
185 // Move to the next position
186 mlt_producer_prepare_next( this );
187
188 return 0;
189 }