]> git.sesse.net Git - mlt/blob - src/modules/kdenlive/filter_freeze.c
c4768e27e074d2df89059b40996a61979debfe38
[mlt] / src / modules / kdenlive / filter_freeze.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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
30 static int filter_get_image( mlt_frame this, 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( this );
34         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
35         mlt_properties props = MLT_FRAME_PROPERTIES( this );
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" );
41         mlt_position currentpos = mlt_properties_get_position( properties, "_seek_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 == NULL || 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                         mlt_producer producer = mlt_frame_get_original_producer(this);
59                         mlt_producer_seek( producer, pos );
60
61                         // Get the frame
62                         mlt_service_get_frame( mlt_producer_service(producer), &freeze_frame, 0 );
63
64                         mlt_properties freeze_properties = MLT_FRAME_PROPERTIES( freeze_frame );
65                         mlt_properties_set_double( freeze_properties, "consumer_aspect_ratio", mlt_properties_get_double( props, "consumer_aspect_ratio" ) );
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( this ) );
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_double( freeze_properties, "output_ratio", mlt_properties_get_double( props, "output_ratio" ) );
71                         mlt_properties_set_data( properties, "freeze_frame", freeze_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
72                         mlt_properties_set_position( properties, "_frame", pos );
73                 }
74
75                 // Get frozen image
76                 uint8_t *buffer = NULL;
77                 int error = mlt_frame_get_image( freeze_frame, &buffer, format, width, height, 1 );
78
79                 int size = 0;
80                 switch ( *format )
81                 {
82                         case mlt_image_yuv420p:
83                                 size = *width * 3 * ( *height + 1 ) / 2;
84                                 break;
85                         case mlt_image_rgb24:
86                                 size = *width * ( *height + 1 ) * 3;
87                                 break;
88                         case mlt_image_rgb24a:
89                         case mlt_image_opengl:
90                                 size = *width * ( *height + 1 ) * 4;
91                                 break;
92                         default:
93                                 *format = mlt_image_yuv422;
94                                 size = *width * ( *height + 1 ) * 2;
95                                 break;
96                 }
97
98                 // Copy it to current frame
99                 uint8_t *image_copy = mlt_pool_alloc( size );
100                 memcpy( image_copy, buffer, size );
101                 *image = image_copy;
102                 mlt_properties_set_data( props, "image", *image, size, ( mlt_destructor ) mlt_pool_release, NULL );
103                 mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
104
105                 return error;
106         }
107
108         int error = mlt_frame_get_image( this, image, format, width, height, 1 );
109         return error;
110 }
111
112 /** Filter processing.
113 */
114
115 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
116 {
117
118         // Push the filter on to the stack
119         mlt_frame_push_service( frame, this );
120
121         // Determine the time position of this frame
122         mlt_properties_set_position( MLT_FILTER_PROPERTIES( this ), "_seek_frame", mlt_frame_get_position( frame ) -  mlt_filter_get_in( this ) );
123
124         // Push the frame filter
125         mlt_frame_push_get_image( frame, filter_get_image );
126
127         return frame;
128 }
129
130 /** Constructor for the filter.
131 */
132
133 mlt_filter filter_freeze_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
134 {
135         mlt_filter this = mlt_filter_new( );
136         if ( this != NULL )
137         {
138                 this->process = filter_process;
139                 // Set the frame which will be chosen for freeze
140                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "frame", "0" );
141
142                 // If freeze_after = 1, only frames after the "frame" value will be frozen
143                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "freeze_after", "0" );
144
145                 // If freeze_before = 1, only frames after the "frame" value will be frozen
146                 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "freeze_before", "0" );
147         }
148         return this;
149 }
150
151
152