Merge branch 'review-2' of git://github.com/rayl/mlt
[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 mlt_filter filter = mlt_frame_pop_service( this );
31 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
32
33 mlt_frame freeze_frame = NULL;;
34 int freeze_before = mlt_properties_get_int( properties, "freeze_before" );
35 int freeze_after = mlt_properties_get_int( properties, "freeze_after" );
36 mlt_position pos = mlt_properties_get_position( properties, "frame" );
37 mlt_position currentpos = mlt_properties_get_position( properties, "_seek_frame" );
38
39 int do_freeze = 0;
40 if (freeze_before == 0 && freeze_after == 0) {
41 do_freeze = 1;
42 } else if (freeze_before != 0 && pos > currentpos) {
43 do_freeze = 1;
44 } else if (freeze_after != 0 && pos < currentpos) {
45 do_freeze = 1;
46 }
47
48 if (do_freeze == 1) {
49 freeze_frame = mlt_properties_get_data( properties, "freeze_frame", NULL );
50
51 if( freeze_frame == NULL || mlt_properties_get_position( properties, "_frame" ) != pos )
52 {
53 // freeze_frame has not been fetched yet, so fetch it and cache it.
54 mlt_producer producer = mlt_frame_get_original_producer(this);
55 mlt_producer_seek( producer, pos );
56
57 // Get the frame
58 mlt_service_get_frame( mlt_producer_service(producer), &freeze_frame, 0 );
59
60 mlt_properties props = MLT_FRAME_PROPERTIES( this );
61 mlt_properties freeze_properties = MLT_FRAME_PROPERTIES( freeze_frame );
62 mlt_properties_set_double( freeze_properties, "consumer_aspect_ratio", mlt_properties_get_double( props, "consumer_aspect_ratio" ) );
63 mlt_properties_set( freeze_properties, "rescale.interp", mlt_properties_get( props, "rescale.interp" ) );
64 mlt_properties_set_double( freeze_properties, "aspect_ratio", mlt_frame_get_aspect_ratio( this ) );
65 mlt_properties_set_int( freeze_properties, "progressive", mlt_properties_get_int( props, "progressive" ) );
66
67 mlt_properties_set_data( properties, "freeze_frame", freeze_frame, 0, NULL, NULL );
68 mlt_properties_set_position( properties, "_frame", pos );
69 }
70 int error = mlt_frame_get_image( freeze_frame, image, format, width, height, 1 );
71 return error;
72 }
73
74 int error = mlt_frame_get_image( this, image, format, width, height, 1 );
75 return error;
76 }
77
78 /** Filter processing.
79 */
80
81 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
82 {
83
84 // Push the filter on to the stack
85 mlt_frame_push_service( frame, this );
86
87 // Determine the time position of this frame
88 mlt_properties_set_position( MLT_FILTER_PROPERTIES( this ), "_seek_frame", mlt_frame_get_position( frame ) - mlt_filter_get_in( this ) );
89
90 // Push the frame filter
91 mlt_frame_push_get_image( frame, filter_get_image );
92
93 return frame;
94 }
95
96 /** Constructor for the filter.
97 */
98
99 mlt_filter filter_freeze_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
100 {
101 mlt_filter this = mlt_filter_new( );
102 if ( this != NULL )
103 {
104 this->process = filter_process;
105 // Set the frame which will be chosen for freeze
106 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "frame", "0" );
107
108 // If freeze_after = 1, only frames after the "frame" value will be frozen
109 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "freeze_after", "0" );
110
111 // If freeze_before = 1, only frames after the "frame" value will be frozen
112 mlt_properties_set( MLT_FILTER_PROPERTIES( this ), "freeze_before", "0" );
113 }
114 return this;
115 }
116
117
118