1 /*
2  * producer_framebuffer.c -- create subspeed frames
3  * Copyright (C) 2007 Jean-Baptiste Mardelle <jb@ader.ch>
4  * Author: Jean-Baptiste Mardelle, based on the code of motion_est by Zachary Drew
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 <framework/mlt.h>
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27 #include <sys/time.h>
28 #include <assert.h>
29 
30 // Forward references.
31 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index );
32 
33 /** Image stack(able) method
34 */
35 
framebuffer_get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)36 static int framebuffer_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
37 {
38 
39 	// Get the filter object and properties
40 	mlt_producer producer = mlt_frame_pop_service( frame );
41 	int index = mlt_frame_pop_service_int( frame );
42 	mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
43 
44 	mlt_service_lock( MLT_PRODUCER_SERVICE( producer ) );
45 
46 	// Frame properties objects
47 	mlt_properties frame_properties = MLT_FRAME_PROPERTIES( frame );
48 	mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
49 
50 	// Get producer parameters
51 	int strobe = mlt_properties_get_int( properties, "strobe" );
52 	int freeze = mlt_properties_get_int( properties, "freeze" );
53 	int freeze_after = mlt_properties_get_int( properties, "freeze_after" );
54 	int freeze_before = mlt_properties_get_int( properties, "freeze_before" );
55 	int in = mlt_properties_get_position( properties, "in" );
56 
57 	// Determine the position
58 	mlt_position first_position = (first_frame != NULL) ? mlt_frame_get_position( first_frame ) : -1;
59 	mlt_position need_first = freeze;
60 
61 	if ( !freeze || freeze_after || freeze_before )
62 	{
63 		double prod_speed = mlt_properties_get_double( properties, "_speed" );
64                 double actual_position = prod_speed * ( in + mlt_producer_position( producer ) );
65 
66 		if ( mlt_properties_get_int( properties, "reverse" ) )
67 			actual_position = mlt_producer_get_playtime( producer ) - actual_position;
68 
69 		if ( strobe < 2 )
70 		{
71 			need_first = floor( actual_position );
72 		}
73 		else
74 		{
75 			// Strobe effect wanted, calculate frame position
76 			need_first = floor( actual_position );
77 			need_first -= MLT_POSITION_MOD(need_first, strobe);
78 		}
79 		if ( freeze )
80 		{
81 			if ( freeze_after && need_first > freeze ) need_first = freeze;
82 			else if ( freeze_before && need_first < freeze ) need_first = freeze;
83 		}
84 	}
85 
86 	if ( *format == mlt_image_none )
87 	{
88 		// set format to the original's producer format
89 		*format = (mlt_image_format) mlt_properties_get_int( properties, "_original_format" );
90 	}
91 	// Determine output buffer size
92 	*width = mlt_properties_get_int( frame_properties, "width" );
93 	*height = mlt_properties_get_int( frame_properties, "height" );
94 	int size = mlt_image_format_size( *format, *width, *height, NULL );
95 
96 	// Get output buffer
97 	int buffersize = 0;
98         int alphasize = *width * *height;
99 	uint8_t *output = mlt_properties_get_data( properties, "output_buffer", &buffersize );
100         uint8_t *output_alpha = mlt_properties_get_data( properties, "output_alpha", NULL );
101 	if( buffersize == 0 || buffersize != size )
102 	{
103 		// invalidate cached frame
104 		first_position = -1;
105 	}
106 
107 	if ( need_first != first_position )
108 	{
109 		// invalidate cached frame
110 		first_position = -1;
111 
112 		// Bust the cached frame
113 		mlt_properties_set_data( properties, "first_frame", NULL, 0, NULL, NULL );
114 		first_frame = NULL;
115 	}
116 
117 	if ( output && first_position != -1 ) {
118 		// Using the cached frame
119 	  	uint8_t *image_copy = mlt_pool_alloc( size );
120 		memcpy( image_copy, output, size );
121                 uint8_t *alpha_copy = mlt_pool_alloc( alphasize );
122                 memcpy( alpha_copy, output_alpha, alphasize );
123 
124 		// Set the output image
125 		*image = image_copy;
126 		mlt_frame_set_image( frame, image_copy, size, mlt_pool_release );
127                 mlt_frame_set_alpha( frame, alpha_copy, alphasize, mlt_pool_release );
128 
129 		*width = mlt_properties_get_int( properties, "_output_width" );
130 		*height = mlt_properties_get_int( properties, "_output_height" );
131 		*format = mlt_properties_get_int( properties, "_output_format" );
132 
133 		mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
134 		return 0;
135 	}
136 
137 	// Get the cached frame
138 	if ( first_frame == NULL )
139 	{
140 		// Get the frame to cache from the real producer
141 		mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
142 
143 		// Seek the producer to the correct place
144 		mlt_producer_seek( real_producer, need_first );
145 
146 		// Get the frame
147 		mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
148 
149 		// Cache the frame
150 		mlt_properties_set_data( properties, "first_frame", first_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
151 	}
152 	mlt_properties first_frame_properties = MLT_FRAME_PROPERTIES( first_frame );
153 
154 
155 	// Which frames are buffered?
156 	uint8_t *first_image = mlt_properties_get_data( first_frame_properties, "image", NULL );
157         uint8_t *first_alpha = mlt_properties_get_data( first_frame_properties, "alpha", NULL );
158 	if ( !first_image )
159 	{
160 		mlt_properties_set( first_frame_properties, "rescale.interp", mlt_properties_get( frame_properties, "rescale.interp" ) );
161 
162 		int error = mlt_frame_get_image( first_frame, &first_image, format, width, height, writable );
163 
164 		if ( error != 0 ) {
165 			mlt_log_warning( MLT_PRODUCER_SERVICE( producer ), "first_image == NULL get image died\n" );
166 			mlt_properties_set_data( properties, "first_frame", NULL, 0, NULL, NULL );
167 			mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
168 			return error;
169 		}
170 		output = mlt_pool_alloc( size );
171 		memcpy( output, first_image, size );
172 		// Let someone else clean up
173 		mlt_properties_set_data( properties, "output_buffer", output, size, mlt_pool_release, NULL );
174 		mlt_properties_set_int( properties, "_output_width", *width );
175 		mlt_properties_set_int( properties, "_output_height", *height );
176 		mlt_properties_set_int( properties, "_output_format", *format );
177 	}
178 
179 	if ( !first_alpha )
180         {
181                 alphasize = *width * *height;
182                 first_alpha = mlt_frame_get_alpha_mask( first_frame );
183                 output_alpha = mlt_pool_alloc( alphasize );
184                 memcpy( output_alpha, first_alpha, alphasize );
185                 mlt_properties_set_data( properties, "output_alpha", output_alpha, alphasize, mlt_pool_release, NULL );
186         }
187 
188 	mlt_service_unlock( MLT_PRODUCER_SERVICE( producer ) );
189 
190 	// Create a copy
191 	uint8_t *image_copy = mlt_pool_alloc( size );
192 	memcpy( image_copy, first_image, size );
193         uint8_t *alpha_copy = mlt_pool_alloc( alphasize );
194         memcpy( alpha_copy, first_alpha, alphasize );
195 
196 	// Set the output image
197 	*image = image_copy;
198 	mlt_frame_set_image( frame, image_copy, size, mlt_pool_release );
199 	mlt_frame_set_alpha( frame, alpha_copy, alphasize, mlt_pool_release );
200 
201 	return 0;
202 }
203 
producer_get_frame(mlt_producer producer,mlt_frame_ptr frame,int index)204 static int producer_get_frame( mlt_producer producer, mlt_frame_ptr frame, int index )
205 {
206 	if ( frame )
207 	{
208 		// Construct a new frame
209 		*frame = mlt_frame_init( MLT_PRODUCER_SERVICE( producer ) );
210 
211 		// Stack the producer and producer's get image
212 		mlt_frame_push_service_int( *frame, index );
213 		mlt_frame_push_service( *frame, producer );
214 		mlt_frame_push_service( *frame, framebuffer_get_image );
215 
216 		mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
217 		mlt_properties frame_properties = MLT_FRAME_PROPERTIES(*frame);
218 
219 		// Get frame from the real producer
220 		mlt_frame first_frame = mlt_properties_get_data( properties, "first_frame", NULL );
221 
222 		if ( first_frame == NULL )
223 		{
224 		    // Get the frame to cache from the real producer
225 		    mlt_producer real_producer = mlt_properties_get_data( properties, "producer", NULL );
226 
227                     // Get the producer speed
228                     double prod_speed = mlt_properties_get_double( properties, "_speed" );
229 
230 		    // Seek the producer to the correct place
231 		    mlt_producer_seek( real_producer, mlt_producer_position( producer ) * prod_speed );
232 
233 		    // Get the frame
234 		    mlt_service_get_frame( MLT_PRODUCER_SERVICE( real_producer ), &first_frame, index );
235 
236 		    // Cache the frame
237 		    mlt_properties_set_data( properties, "first_frame", first_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
238 
239 		    // Find the original producer's format
240 		    int width = 0;
241 		    int height = 0;
242 		    mlt_image_format format = mlt_image_none;
243 		    uint8_t *image = NULL;
244 		    int error = mlt_frame_get_image( first_frame, &image, &format, &width, &height, 0 );
245 		    if ( !error )
246 		    {
247 			// cache the original producer's pixel format
248 			mlt_properties_set_int( properties, "_original_format", (int) format );
249 		    }
250 		}
251 
252 		mlt_properties_inherit( frame_properties, MLT_FRAME_PROPERTIES(first_frame) );
253 
254 		double force_aspect_ratio = mlt_properties_get_double( properties, "force_aspect_ratio" );
255 		if ( force_aspect_ratio <= 0.0 ) force_aspect_ratio = mlt_properties_get_double( properties, "aspect_ratio" );
256 		mlt_properties_set_double( frame_properties, "aspect_ratio", force_aspect_ratio );
257 
258 		// Give the returned frame temporal identity
259 		mlt_frame_set_position( *frame, mlt_producer_position( producer ) );
260 
261 		mlt_properties_set_int( frame_properties, "meta.media.width", mlt_properties_get_int( properties, "width" ) );
262 		mlt_properties_set_int( frame_properties, "meta.media.height", mlt_properties_get_int( properties, "height" ) );
263 		mlt_properties_pass_list( frame_properties, properties, "width, height" );
264 	}
265 
266 	return 0;
267 }
268 
269 
producer_framebuffer_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)270 mlt_producer producer_framebuffer_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
271 {
272 	if ( !arg ) return NULL;
273 	mlt_producer producer = NULL;
274 	producer = calloc( 1, sizeof( struct mlt_producer_s ) );
275 	if ( !producer )
276 		return NULL;
277 
278 	if ( mlt_producer_init( producer, NULL ) )
279 	{
280 		free( producer );
281 		return NULL;
282 	}
283 
284 	// Wrap loader
285 	mlt_producer real_producer;
286 
287 	// Check if a speed was specified.
288 	/**
289 
290 	* Speed must be appended to the filename with '?'. To play your video at 50%:
291 	 melt framebuffer:my_video.mpg?0.5
292 
293 	* Stroboscope effect can be obtained by adding a stobe=x parameter, where
294 	 x is the number of frames that will be ignored.
295 
296 	* You can play the movie backwards by adding reverse=1
297 
298 	* You can freeze the clip at a determined position by adding freeze=frame_pos
299 	  add freeze_after=1 to freeze only paste position or freeze_before to freeze before it
300 
301 	**/
302 
303 	double speed = 0.0;
304 	char *props = strdup( arg );
305 	char *ptr = strrchr( props, '?' );
306 
307 	if ( ptr )
308 	{
309 		speed = atof( ptr + 1 );
310 		if ( speed != 0.0 )
311 			// If speed was valid, then strip it and the delimiter.
312 			// Otherwise, an invalid speed probably means this '?' was not a delimiter.
313 			*ptr = '\0';
314 	}
315 
316 	real_producer = mlt_factory_producer( profile, "abnormal", props );
317 	free( props );
318 
319 	if (speed == 0.0) speed = 1.0;
320 
321 	if ( producer != NULL && real_producer != NULL)
322 	{
323 		// Get the properties of this producer
324 		mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
325 
326 		mlt_properties_set( properties, "resource", arg);
327 
328 		// Store the producer and filter
329 		mlt_properties_set_data( properties, "producer", real_producer, 0, ( mlt_destructor )mlt_producer_close, NULL );
330 
331 		// Grab some stuff from the real_producer
332 		mlt_properties_pass_list( properties, MLT_PRODUCER_PROPERTIES( real_producer ), "progressive, length, width, height, aspect_ratio" );
333 
334 		if ( speed < 0 )
335 		{
336 			speed = -speed;
337 			mlt_properties_set_int( properties, "reverse", 1 );
338 		}
339 
340 		if ( speed != 1.0 )
341 		{
342 			double real_length = ( (double)  mlt_producer_get_length( real_producer ) ) / speed;
343 			mlt_properties_set_position( properties, "length", real_length );
344 			mlt_properties real_properties = MLT_PRODUCER_PROPERTIES( real_producer );
345 			const char* service = mlt_properties_get( real_properties, "mlt_service" );
346 			if ( service && !strcmp( service, "avformat" ) )
347 			{
348 				int n = mlt_properties_count( real_properties );
349 				int i;
350 				for ( i = 0; i < n; i++ )
351 				{
352 					if ( strstr( mlt_properties_get_name( real_properties, i ), "stream.frame_rate" ) )
353 					{
354 						double source_fps = mlt_properties_get_double( real_properties, mlt_properties_get_name( real_properties, i ) );
355 						if ( source_fps > mlt_profile_fps( profile ) )
356 						{
357 							mlt_properties_set_double( real_properties, "force_fps", source_fps * speed );
358 							mlt_properties_set_position( real_properties, "length", real_length );
359 							mlt_properties_set_position( real_properties, "out", real_length - 1 );
360 							speed = 1.0;
361 						}
362 						break;
363 					}
364 				}
365 			}
366 		}
367 		mlt_properties_set_position( properties, "out", mlt_producer_get_length( producer ) - 1 );
368 
369 		// Since we control the seeking, prevent it from seeking on its own
370 		mlt_producer_set_speed( real_producer, 0 );
371 		mlt_producer_set_speed( producer, speed );
372 
373 		// Override the get_frame method
374 		producer->get_frame = producer_get_frame;
375 	}
376 	else
377 	{
378 		if ( producer )
379 			mlt_producer_close( producer );
380 		if ( real_producer )
381 			mlt_producer_close( real_producer );
382 
383 		producer = NULL;
384 	}
385 	return producer;
386 }
387