]> git.sesse.net Git - mlt/blob - src/modules/core/filter_luma.c
Refactor to use mlt_frame_set_image/_alpha.
[mlt] / 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 #include <framework/mlt_log.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 /** Do it :-).
33 */
34
35 static int filter_get_image( mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable )
36 {
37         int error = 0;
38         mlt_filter filter = mlt_frame_pop_service( this );
39         mlt_properties properties = MLT_FILTER_PROPERTIES( filter );
40
41         mlt_service_lock( MLT_FILTER_SERVICE( filter ) );
42
43         mlt_transition luma = mlt_properties_get_data( properties, "luma", NULL );
44         mlt_frame b_frame = mlt_properties_get_data( properties, "frame", NULL );
45         mlt_properties b_frame_props = b_frame ? MLT_FRAME_PROPERTIES( b_frame ) : NULL;
46         int out = mlt_properties_get_int( properties, "period" );
47         int cycle = mlt_properties_get_int( properties, "cycle" );
48         int duration = mlt_properties_get_int( properties, "duration" );
49         char *name = mlt_properties_get( properties, "_unique_id" );
50         mlt_position position = mlt_properties_get_position( MLT_FRAME_PROPERTIES(this), name );
51
52         out = out? out + 1 : 25;
53         if ( cycle )
54                 out = cycle;
55         if ( duration < 1 || duration > out )
56                 duration = out;
57         *format = mlt_image_yuv422;
58
59         if ( b_frame == NULL || mlt_properties_get_int( b_frame_props, "width" ) != *width || mlt_properties_get_int( b_frame_props, "height" ) != *height )
60         {
61                 b_frame = mlt_frame_init( MLT_FILTER_SERVICE( filter ) );
62                 mlt_properties_set_data( properties, "frame", b_frame, 0, ( mlt_destructor )mlt_frame_close, NULL );
63         }
64
65         if ( luma == NULL )
66         {
67                 char *resource = mlt_properties_get( properties, "resource" );
68                 mlt_profile profile = mlt_service_profile( MLT_FILTER_SERVICE( filter ) );
69                 luma = mlt_factory_transition( profile, "luma", resource );
70                 if ( luma != NULL )
71                 {
72                         mlt_properties luma_properties = MLT_TRANSITION_PROPERTIES( luma );
73                         mlt_properties_set_int( luma_properties, "in", 0 );
74                         mlt_properties_set_int( luma_properties, "out", duration - 1 );
75                         mlt_properties_set_int( luma_properties, "reverse", 1 );
76                         mlt_properties_set_data( properties, "luma", luma, 0, ( mlt_destructor )mlt_transition_close, NULL );
77                 }
78         }
79
80         mlt_position modulo_pos = position % out;
81         mlt_log_debug( MLT_FILTER_SERVICE(filter), "pos %d mod period %d\n", position, modulo_pos );
82         if ( luma != NULL &&
83              ( mlt_properties_get( properties, "blur" ) != NULL ||
84                    ( position >= duration && modulo_pos < duration - 1 ) ) )
85         {
86                 mlt_properties luma_properties = MLT_TRANSITION_PROPERTIES( luma );
87                 mlt_properties_pass( luma_properties, properties, "luma." );
88                 int in = position / out * out + mlt_frame_get_position( this ) - position;
89                 mlt_properties_set_int( luma_properties, "in", in );
90                 mlt_properties_set_int( luma_properties, "out", in + duration - 1 );
91                 mlt_transition_process( luma, this, b_frame );
92         }
93
94         error = mlt_frame_get_image( this, image, format, width, height, 1 );
95
96         // We only need a copy of the last frame in the cycle, but we could miss it
97         // with realtime frame-dropping, so we copy the last several frames of the cycle.
98         if ( error == 0 && modulo_pos > out - duration )
99         {
100                 mlt_properties a_props = MLT_FRAME_PROPERTIES( this );
101                 int size = 0;
102                 uint8_t *src = mlt_properties_get_data( a_props, "image", &size );
103                 uint8_t *dst = mlt_pool_alloc( size );
104
105                 if ( dst != NULL )
106                 {
107                         mlt_log_debug( MLT_FILTER_SERVICE(filter), "copying frame %d\n", modulo_pos );
108                         mlt_properties b_props = MLT_FRAME_PROPERTIES( b_frame );
109                         memcpy( dst, src, size );
110                         mlt_frame_set_image( b_frame, dst, size, mlt_pool_release );
111                         mlt_properties_set_int( b_props, "width", *width );
112                         mlt_properties_set_int( b_props, "height", *height );
113                         mlt_properties_set_int( b_props, "format", *format );
114                 }
115         }
116
117         mlt_service_unlock( MLT_FILTER_SERVICE( filter ) );
118
119         return error;
120 }
121
122 /** Filter processing.
123 */
124
125 static mlt_frame filter_process( mlt_filter this, mlt_frame frame )
126 {
127         // Get a unique name to store the frame position
128         char *name = mlt_properties_get( MLT_FILTER_PROPERTIES( this ), "_unique_id" );
129
130         // Assign the current position to the name
131         mlt_properties_set_position( MLT_FRAME_PROPERTIES( frame ), name, mlt_frame_get_position( frame ) );
132
133         // Push the filter on to the stack
134         mlt_frame_push_service( frame, this );
135
136         // Push the get_image on to the stack
137         mlt_frame_push_get_image( frame, filter_get_image );
138
139         return frame;
140 }
141
142 /** Constructor for the filter.
143 */
144
145 mlt_filter filter_luma_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
146 {
147         mlt_filter this = mlt_filter_new( );
148         if ( this != NULL )
149         {
150                 mlt_properties properties = MLT_FILTER_PROPERTIES( this );
151                 this->process = filter_process;
152                 if ( arg != NULL )
153                         mlt_properties_set( properties, "resource", arg );
154         }
155         return this;
156 }