a389e01dfb9708d49284faa5dd8655f8d837e2e8
[melted] / 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 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
28 {
29 // Get the image
30
31
32 mlt_position currentpos = mlt_frame_get_position( this );
33
34 mlt_filter filter = mlt_frame_pop_service( this );
35 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
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
42 int do_freeze = 0;
43 if (freeze_before == 0 && freeze_after == 0) {
44 do_freeze = 1;
45 } else if (freeze_before != 0 && pos > currentpos) {
46 do_freeze = 1;
47 } else if (freeze_after != 0 && pos < currentpos) {
48 do_freeze = 1;
49 }
50
51 if (do_freeze == 1) {
52 freeze_frame = mlt_properties_get_data( properties, "freeze_frame", NULL );
53
54 if( freeze_frame == NULL)
55 {
56 // freeze_frame has not been fetched yet, so fetch it and cache it.
57 mlt_producer producer = mlt_frame_get_original_producer(this);
58 mlt_producer_seek( producer, pos );
59
60 // Get the frame
61 mlt_service_get_frame( mlt_producer_service(producer), &freeze_frame, 0 );
62
63 mlt_properties props = MLT_FRAME_PROPERTIES( this );
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
70 mlt_properties_set_data( properties, "freeze_frame", freeze_frame, 0, NULL, NULL );
71 }
72 int error = mlt_frame_get_image( freeze_frame, image, format, width, height, 1 );
73 return error;
74 }
75
76 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
77 return error;
78 }
79
80 /** Filter processing.
81 */
82
83 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
84 {
85
86 // Push the filter on to the stack
87 mlt_frame_push_service( frame, this );
88
89 // Push the frame filter
90 mlt_frame_push_get_image( frame, filter_get_image );
91
92 return frame;
93 }
94
95 /** Constructor for the filter.
96 */
97
98 mlt_filter filter_freeze_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
99 {
100 mlt_filter this = mlt_filter_new( );
101 if ( this != NULL )
102 {
103 this->process = filter_process;
104 // Set the frame which will be chosen for freeze
105 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "frame", "0" );
106
107 // If freeze_after = 1, only frames after the "frame" value will be frozen
108 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "freeze_after", "0" );
109
110 // If freeze_before = 1, only frames after the "frame" value will be frozen
111 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "freeze_before", "0" );
112 }
113 return this;
114 }
115
116
117