1 /*
2 * filter_freeze.c -- simple frame freezing filter
3 * Copyright (C) 2007 Jean-Baptiste Mardelle <jb@kdenlive.org>
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_filter.h>
21 #include <framework/mlt_frame.h>
22 #include <framework/mlt_producer.h>
23 #include <framework/mlt_service.h>
24 #include <framework/mlt_factory.h>
25 #include <framework/mlt_property.h>
26
27 #include <stdio.h>
28 #include <string.h>
29
filter_get_image(mlt_frame frame,uint8_t ** image,mlt_image_format * format,int * width,int * height,int writable)30 static int filter_get_image( mlt_frame frame, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
31 {
32 // Get the image
33 mlt_filter filter = mlt_frame_pop_service( frame );
34 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
35 mlt_properties props = MLT_FRAME_PROPERTIES( frame );
36
37 mlt_frame freeze_frame = NULL;;
38 int freeze_before = mlt_properties_get_int( properties, "freeze_before" );
39 int freeze_after = mlt_properties_get_int( properties, "freeze_after" );
40 mlt_position pos = mlt_properties_get_position( properties, "frame" ) + mlt_producer_get_in( mlt_frame_get_original_producer( frame ) );
41 mlt_position currentpos = mlt_filter_get_position( filter, frame );
42
43 int do_freeze = 0;
44 if (freeze_before == 0 && freeze_after == 0) {
45 do_freeze = 1;
46 } else if (freeze_before != 0 && pos > currentpos) {
47 do_freeze = 1;
48 } else if (freeze_after != 0 && pos < currentpos) {
49 do_freeze = 1;
50 }
51
52 if (do_freeze == 1) {
53 mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
54 freeze_frame = mlt_properties_get_data( properties, "freeze_frame", NULL );
55 if ( !freeze_frame || mlt_properties_get_position( properties, "_frame" ) != pos )
56 {
57 // freeze_frame has not been fetched yet or is not useful, so fetch it and cache it.
58 // get parent producer
59 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
60 mlt_producer_seek( producer, pos );
61
62 // Get the frame
63 mlt_service_get_frame( mlt_producer_service(producer), &freeze_frame, 0 );
64
65 mlt_properties freeze_properties = MLT_FRAME_PROPERTIES( freeze_frame );
66 mlt_properties_set( freeze_properties, "rescale.interp", mlt_properties_get( props, "rescale.interp" ) );
67 mlt_properties_set_double( freeze_properties, "aspect_ratio", mlt_frame_get_aspect_ratio( frame ) );
68 mlt_properties_set_int( freeze_properties, "progressive", mlt_properties_get_int( props, "progressive" ) );
69 mlt_properties_set_int( freeze_properties, "consumer_deinterlace", mlt_properties_get_int( props, "consumer_deinterlace" ) || mlt_properties_get_int( properties, "deinterlace" ) );
70 mlt_properties_set_data( properties, "freeze_frame", freeze_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
71 mlt_properties_set_position( properties, "_frame", pos );
72 }
73
74 // Get frozen image
75 uint8_t *buffer = NULL;
76 int error = mlt_frame_get_image( freeze_frame, &buffer, format, width, height, 1 );
77 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
78
79 // Copy it to current frame
80 int size = mlt_image_format_size( *format, *width, *height, NULL );
81 uint8_t *image_copy = mlt_pool_alloc( size );
82 memcpy( image_copy, buffer, size );
83 *image = image_copy;
84 mlt_frame_set_image( frame, *image, size, mlt_pool_release );
85
86 uint8_t *alpha_buffer = mlt_frame_get_alpha( freeze_frame );
87 if ( alpha_buffer )
88 {
89 int alphasize = *width * *height;
90 uint8_t *alpha_copy = mlt_pool_alloc( alphasize );
91 memcpy( alpha_copy, alpha_buffer, alphasize );
92 mlt_frame_set_alpha( frame, alpha_copy, alphasize, mlt_pool_release );
93 }
94 return error;
95 }
96
97 int error = mlt_frame_get_image( frame, image, format, width, height, 1 );
98 return error;
99 }
100
101 /** Filter processing.
102 */
103
filter_process(mlt_filter filter,mlt_frame frame)104 static mlt_frame filter_process( mlt_filter filter, mlt_frame frame )
105 {
106
107 // Push the filter on to the stack
108 mlt_frame_push_service( frame, filter );
109
110 // Push the frame filter
111 mlt_frame_push_get_image( frame, filter_get_image );
112
113 return frame;
114 }
115
116 /** Constructor for the filter.
117 */
118
filter_freeze_init(mlt_profile profile,mlt_service_type type,const char * id,char * arg)119 mlt_filter filter_freeze_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
120 {
121 mlt_filter filter = mlt_filter_new( );
122 if ( filter != NULL )
123 {
124 filter->process = filter_process;
125 // Set the frame which will be chosen for freeze
126 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "frame", "0" );
127
128 // If freeze_after = 1, only frames after the "frame" value will be frozen
129 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "freeze_after", "0" );
130
131 // If freeze_before = 1, only frames before the "frame" value will be frozen
132 mlt_properties_set( MLT_FILTER_PROPERTIES( filter ), "freeze_before", "0" );
133 }
134 return filter;
135 }
136
137
138
139