]> git.sesse.net Git - mlt/blob - src/modules/frei0r/frei0r_helper.c
Massive refactoring of image conversion.
[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 int process_frei0r_item( mlt_service_type type, double position, mlt_properties prop, mlt_frame this, uint8_t **image, int *width, int *height )
35 {
36         int i=0;
37         f0r_instance_t ( *f0r_construct ) ( unsigned int , unsigned int ) =  mlt_properties_get_data(  prop , "f0r_construct" ,NULL);
38         void (*f0r_update)(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)=mlt_properties_get_data(  prop , "f0r_update" ,NULL);
39         void (*f0r_destruct)(f0r_instance_t instance)=mlt_properties_get_data(  prop , "f0r_destruct" ,NULL);
40
41         void (*f0r_get_plugin_info)(f0r_plugin_info_t*)=mlt_properties_get_data( prop, "f0r_get_plugin_info" ,NULL);
42         void (*f0r_get_param_info)(f0r_param_info_t* info, int param_index)=mlt_properties_get_data( prop ,  "f0r_get_param_info" ,NULL);
43         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);
44         void (*f0r_get_param_value)(f0r_instance_t instance, f0r_param_t param, int param_index)=mlt_properties_get_data(  prop , "f0r_get_param_value" ,NULL);
45         void (*f0r_update2) (f0r_instance_t instance, double time,
46                          const uint32_t* inframe1,const uint32_t* inframe2,const uint32_t* inframe3,
47          uint32_t* outframe)=mlt_properties_get_data(  prop , "f0r_update2" ,NULL);
48
49
50         //use as name the width and height
51         f0r_instance_t inst;
52         char ctorname[1024]="";
53         sprintf(ctorname,"ctor-%dx%d",*width,*height);
54
55         void* neu=mlt_properties_get_data( prop , ctorname ,NULL );
56         if (!f0r_construct){
57                 //printf("no ctor\n");
58                 return -1;
59         }
60         if ( neu == 0 ){
61                 inst= f0r_construct(*width,*height);
62                 mlt_properties_set_data(  prop  ,  ctorname , inst, sizeof(void*) , f0r_destruct , NULL );;
63         }else{
64                 inst=mlt_properties_get_data( prop ,  ctorname , NULL );
65         }
66         if (f0r_get_plugin_info){
67                 f0r_plugin_info_t info;
68                 f0r_get_plugin_info(&info);
69                 for (i=0;i<info.num_params;i++){
70                         f0r_param_info_t pinfo;
71                         f0r_get_param_info(&pinfo,i);
72                         mlt_geometry geom=mlt_geometry_init();
73                         struct mlt_geometry_item_s item;
74                         //set param if found
75
76                         double t=0.0;
77                         f0r_get_param_value(inst,&t,i);
78                         char *val;
79                         if (mlt_properties_get( prop , pinfo.name ) !=NULL ){
80                                 switch (pinfo.type) {
81                                         case F0R_PARAM_DOUBLE:
82                                         case F0R_PARAM_BOOL:
83                                                 val=mlt_properties_get(prop, pinfo.name );
84                                                 mlt_geometry_parse(geom,val,-1,-1,-1);
85                                                 mlt_geometry_fetch(geom,&item,position);
86                                                 t=item.x;
87                                                 f0r_set_param_value(inst,&t,i);
88                                                 break;
89                                         case F0R_PARAM_COLOR:
90                                         {
91                                                 f0r_param_color_t color;
92                                                 parse_color(mlt_properties_get_int(prop , pinfo.name), &color);
93                                                 f0r_set_param_value(inst, &color, i);
94                                                 break;
95                                         }
96                                 }
97                         }
98
99                         mlt_geometry_close(geom);
100                 }
101         }
102
103         int video_area = *width * *height;
104         uint32_t *result = mlt_pool_alloc( video_area * sizeof(uint32_t) );
105
106         if (type==producer_type) {
107                 f0r_update (inst, position, NULL, result );
108         } else if (type==filter_type) {
109                 f0r_update ( inst, position, (uint32_t *)image[0], result );
110         } else if (type==transition_type && f0r_update2 ){
111                 f0r_update2 ( inst, position, (uint32_t *)image[0], (uint32_t *)image[1], NULL, result );
112         }
113         *image = (uint8_t*) result;
114         mlt_properties_set_data(MLT_FRAME_PROPERTIES(this), "image", result, video_area * sizeof(uint32_t), mlt_pool_release, NULL);
115
116         return 0;
117 }
118
119 void destruct (mlt_properties prop ) {
120
121         void (*f0r_destruct)(f0r_instance_t instance)=mlt_properties_get_data(  prop , "f0r_destruct" , NULL );
122         void (*f0r_deinit)(void)=mlt_properties_get_data ( prop , "f0r_deinit" , NULL);
123         int i=0;
124
125         if ( f0r_deinit != NULL )
126                 f0r_deinit();
127
128         for ( i=0 ; i < mlt_properties_count ( prop ) ; i++ ){
129                 if ( strstr ( mlt_properties_get_name ( prop , i ) , "ctor-" ) != NULL ){
130                         void * inst=mlt_properties_get_data( prop , mlt_properties_get_name ( prop , i ) , NULL );
131                         if ( inst != NULL ){
132                                 f0r_destruct( (f0r_instance_t) inst);
133                         }
134                 }
135         }
136         void (*dlclose)(void*)=mlt_properties_get_data ( prop , "_dlclose" , NULL);
137         void *handle=mlt_properties_get_data ( prop , "_dlclose_handle" , NULL);
138
139         if (handle && dlclose ){
140                 dlclose ( handle );
141         }
142
143 }