Merge ../mlt
[melted] / src / modules / core / filter_luma.c
1 /*
2 * filter_luma.c -- luma filter
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <framework/mlt_filter.h>
22 #include <framework/mlt_factory.h>
23 #include <framework/mlt_frame.h>
24 #include <framework/mlt_producer.h>
25 #include <framework/mlt_transition.h>
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 /** Do it :-).
32 */
33
34 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
35 {
36 int error = 0;
37 mlt_filter filter = mlt_frame_pop_service( this );
38 mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
39 mlt_transition luma = mlt_properties_get_data( properties, "luma", NULL );
40 mlt_frame b_frame = mlt_properties_get_data( properties, "frame", NULL );
41 mlt_properties b_frame_props = b_frame ? MLT_FRAME_PROPERTIES( b_frame ) : NULL;
42 int out = mlt_properties_get_int( properties, "period" );
43
44 if ( out == 0 )
45 out = 24;
46
47 if ( b_frame == NULL || mlt_properties_get_int( b_frame_props, "width" ) != *width || mlt_properties_get_int( b_frame_props, "height" ) != *height )
48 {
49 b_frame = mlt_frame_init( MLT_FILTER_SERVICE( filter ) );
50 mlt_properties_set_data( properties, "frame", b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
51 }
52
53 if ( luma == NULL )
54 {
55 char *resource = mlt_properties_get( properties, "resource" );
56 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
57 luma = mlt_factory_transition( profile, "luma", resource );
58 if ( luma != NULL )
59 {
60 mlt_properties luma_properties = MLT_TRANSITION_PROPERTIES( luma );
61 mlt_properties_set_int( luma_properties, "in", 0 );
62 mlt_properties_set_int( luma_properties, "out", out );
63 mlt_properties_set_int( luma_properties, "reverse", 1 );
64 mlt_properties_set_data( properties, "luma", luma, 0, ( mlt_destructor )mlt_transition_close, NULL );
65 }
66
67 // Prime the filter with the first image to prevent a transition from the white
68 // of a test card.
69 error = mlt_frame_get_image( this, image, format, width, height, 1 );
70 if ( error == 0 )
71 {
72 mlt_properties a_props = MLT_FRAME_PROPERTIES( this );
73 int size = 0;
74 uint8_t *src = mlt_properties_get_data( a_props, "image", &size );
75 uint8_t *dst = mlt_pool_alloc( size );
76
77 if ( dst != NULL )
78 {
79 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
80 memcpy( dst, src, size );
81 mlt_properties_set_data( b_props, "image", dst, size, mlt_pool_release, NULL );
82 mlt_properties_set_int( b_props, "width", *width );
83 mlt_properties_set_int( b_props, "height", *height );
84 mlt_properties_set_int( b_props, "format", *format );
85 }
86 }
87 }
88
89 if ( luma != NULL &&
90 ( mlt_properties_get( properties, "blur" ) != NULL ||
91 (int)mlt_frame_get_position( this ) % ( out + 1 ) != out ) )
92 {
93 mlt_properties luma_properties = MLT_TRANSITION_PROPERTIES( luma );
94 mlt_properties_pass( luma_properties, properties, "luma." );
95 mlt_transition_process( luma, this, b_frame );
96 }
97
98 error = mlt_frame_get_image( this, image, format, width, height, 1 );
99
100 if ( error == 0 )
101 {
102 mlt_properties a_props = MLT_FRAME_PROPERTIES( this );
103 int size = 0;
104 uint8_t *src = mlt_properties_get_data( a_props, "image", &size );
105 uint8_t *dst = mlt_pool_alloc( size );
106
107 if ( dst != NULL )
108 {
109 mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
110 memcpy( dst, src, size );
111 mlt_properties_set_data( b_props, "image", dst, size, mlt_pool_release, NULL );
112 mlt_properties_set_int( b_props, "width", *width );
113 mlt_properties_set_int( b_props, "height", *height );
114 mlt_properties_set_int( b_props, "format", *format );
115 }
116 }
117
118 return error;
119 }
120
121 /** Filter processing.
122 */
123
124 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
125 {
126 // Push the filter on to the stack
127 mlt_frame_push_service( frame, this );
128
129 // Push the get_image on to the stack
130 mlt_frame_push_get_image( frame, filter_get_image );
131
132 return frame;
133 }
134
135 /** Constructor for the filter.
136 */
137
138 mlt_filter filter_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
139 {
140 mlt_filter this = mlt_filter_new( );
141 if ( this != NULL )
142 {
143 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
144 this->process = filter_process;
145 if ( arg != NULL )
146 mlt_properties_set( properties, "resource", arg );
147 }
148 return this;
149 }