]> git.sesse.net Git - mlt/blob - src/modules/frei0r/frei0r_helper.c
Refactor to use mlt_frame_set_image/_alpha.
[mlt] / src / modules / frei0r / frei0r_helper.c
1 /*
2  * frei0r_helper.c -- frei0r helper
3  * Copyright (c) 2008 Marco Gittler <g.marco@freenet.de>
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 #include "frei0r_helper.h"
20 #include <frei0r.h>
21 #include <string.h>
22 #include <stdlib.h>
23
24 static void parse_color( int color, f0r_param_color_t *fcolor )
25 {
26         fcolor->r = ( color >> 24 ) & 0xff;
27         fcolor->r /= 255;
28         fcolor->g = ( color >> 16 ) & 0xff;
29         fcolor->g /= 255;
30         fcolor->b = ( color >>  8 ) & 0xff;
31         fcolor->b /= 255;
32 }
33
34 static void rgba_bgra( uint8_t *src, uint8_t* dst, int width, int height )
35 {
36         int n = width * height + 1;
37
38         while ( --n )
39         {
40                 *dst++ = src[2];
41                 *dst++ = src[1];
42                 *dst++ = src[0];
43                 *dst++ = src[3];
44                 src += 4;
45         }       
46 }
47
48 int process_frei0r_item( mlt_service service, double position, mlt_properties prop, mlt_frame this, uint8_t **image, int *width, int *height )
49 {
50         int i=0;
51         f0r_instance_t ( *f0r_construct ) ( unsigned int , unsigned int ) =  mlt_properties_get_data(  prop , "f0r_construct" ,NULL);
52         void (*f0r_update)(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)=mlt_properties_get_data(  prop , "f0r_update" ,NULL);
53         void (*f0r_destruct)(f0r_instance_t instance)=mlt_properties_get_data(  prop , "f0r_destruct" ,NULL);
54
55         void (*f0r_get_plugin_info)(f0r_plugin_info_t*)=mlt_properties_get_data( prop, "f0r_get_plugin_info" ,NULL);
56         void (*f0r_get_param_info)(f0r_param_info_t* info, int param_index)=mlt_properties_get_data( prop ,  "f0r_get_param_info" ,NULL);
57         void (*f0r_set_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index)=mlt_properties_get_data(  prop , "f0r_set_param_value" ,NULL);
58         void (*f0r_update2) (f0r_instance_t instance, double time,
59                          const uint32_t* inframe1,const uint32_t* inframe2,const uint32_t* inframe3,
60         uint32_t* outframe)=mlt_properties_get_data(  prop , "f0r_update2" ,NULL);
61         mlt_service_type type = mlt_service_identify( service );
62         int not_thread_safe = mlt_properties_get_int( prop, "_not_thread_safe" );
63
64         //use as name the width and height
65         f0r_instance_t inst;
66         f0r_plugin_info_t info;
67         char ctorname[1024]="";
68         sprintf(ctorname,"ctor-%dx%d",*width,*height);
69
70         mlt_service_lock( service );
71
72         void* neu=mlt_properties_get_data( prop , ctorname ,NULL );
73         if (!f0r_construct){
74                 //printf("no ctor\n");
75                 return -1;
76         }
77         if ( neu == 0 ){
78                 inst= f0r_construct(*width,*height);
79                 mlt_properties_set_data(  prop  ,  ctorname , inst, sizeof(void*) , f0r_destruct , NULL );;
80         }else{
81                 inst=mlt_properties_get_data( prop ,  ctorname , NULL );
82         }
83
84         if ( !not_thread_safe )
85                 mlt_service_unlock( service );
86         
87         if (f0r_get_plugin_info){
88                 f0r_get_plugin_info(&info);
89                 for (i=0;i<info.num_params;i++){
90                         f0r_param_info_t pinfo;
91                         f0r_get_param_info(&pinfo,i);
92                         if (mlt_properties_get( prop , pinfo.name ) !=NULL ){
93                                 switch (pinfo.type) {
94                                         case F0R_PARAM_DOUBLE:
95                                         case F0R_PARAM_BOOL:
96                                         {
97                                                 char *val=mlt_properties_get(prop, pinfo.name );
98                                                 mlt_geometry geom=mlt_geometry_init();
99                                                 struct mlt_geometry_item_s item;
100                                                 mlt_geometry_parse(geom,val,-1,-1,-1);
101                                                 mlt_geometry_fetch(geom,&item,position);
102                                                 double t=item.x;
103                                                 f0r_set_param_value(inst,&t,i);
104                                                 mlt_geometry_close(geom);
105                                                 break;
106                                         }
107                                         case F0R_PARAM_COLOR:
108                                         {
109                                                 f0r_param_color_t color;
110                                                 parse_color(mlt_properties_get_int(prop , pinfo.name), &color);
111                                                 f0r_set_param_value(inst, &color, i);
112                                                 break;
113                                         }
114                                         case F0R_PARAM_STRING:
115                                         {
116                                                 f0r_param_string val = mlt_properties_get(prop, pinfo.name);
117                                                 if (val) f0r_set_param_value(inst, &val, i);
118                                                 break;
119                                         }
120                                 }
121                         }
122                 }
123         }
124
125         int video_area = *width * *height;
126         uint32_t *result = mlt_pool_alloc( video_area * sizeof(uint32_t) );
127         uint32_t *extra = NULL;
128         uint32_t *source[2] = { (uint32_t*) image[0], (uint32_t*) image[1] };
129         uint32_t *dest = result;
130
131         if (info.color_model == F0R_COLOR_MODEL_BGRA8888) {
132                 rgba_bgra(image[0], (uint8_t*) result, *width, *height);
133                 source[0] = result;
134                 dest = (uint32_t*) image[0];
135                 if (type == producer_type) {
136                         extra = mlt_pool_alloc( video_area * sizeof(uint32_t) );
137                         dest = extra;
138                 }
139                 else if (type == transition_type && f0r_update2) {
140                         extra = mlt_pool_alloc( video_area * sizeof(uint32_t) );
141                         rgba_bgra(image[1], (uint8_t*) extra, *width, *height);
142                         source[1] = extra;
143                 }
144         }
145         if (type==producer_type) {
146                 f0r_update (inst, position, NULL, dest );
147         } else if (type==filter_type) {
148                 f0r_update ( inst, position, source[0], dest );
149         } else if (type==transition_type && f0r_update2 ){
150                 f0r_update2 ( inst, position, source[0], source[1], NULL, dest );
151         }
152         if ( not_thread_safe )
153                 mlt_service_unlock( service );
154         if (info.color_model == F0R_COLOR_MODEL_BGRA8888) {
155                 rgba_bgra((uint8_t*) dest, (uint8_t*) result, *width, *height);
156         }
157         *image = (uint8_t*) result;
158         mlt_frame_set_image(this, (uint8_t*) result, video_area * sizeof(uint32_t), mlt_pool_release);
159         if (extra)
160                 mlt_pool_release(extra);
161
162         return 0;
163 }
164
165 void destruct (mlt_properties prop ) {
166
167         void (*f0r_destruct)(f0r_instance_t instance)=mlt_properties_get_data(  prop , "f0r_destruct" , NULL );
168         void (*f0r_deinit)(void)=mlt_properties_get_data ( prop , "f0r_deinit" , NULL);
169         int i=0;
170
171         if ( f0r_deinit != NULL )
172                 f0r_deinit();
173
174         for ( i=0 ; i < mlt_properties_count ( prop ) ; i++ ){
175                 if ( strstr ( mlt_properties_get_name ( prop , i ) , "ctor-" ) != NULL ){
176                         void * inst=mlt_properties_get_data( prop , mlt_properties_get_name ( prop , i ) , NULL );
177                         if ( inst != NULL ){
178                                 f0r_destruct( (f0r_instance_t) inst);
179                         }
180                 }
181         }
182         void (*dlclose)(void*)=mlt_properties_get_data ( prop , "_dlclose" , NULL);
183         void *handle=mlt_properties_get_data ( prop , "_dlclose_handle" , NULL);
184
185         if (handle && dlclose ){
186                 dlclose ( handle );
187         }
188
189 }