dd7db7513469f794a924ab180fb4d981b2082c58
[melted] / 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
25 static void parse_color( char *color, f0r_param_color_t *fcolor )
26 {
27 unsigned int temp = strtoul( color, &color, 0 );
28 if ( strlen( color ) > 6 )
29 temp >>= 8;
30 fcolor->r = ( temp >> 16 ) & 0xff;
31 fcolor->r /= 255;
32 fcolor->g = ( temp >> 8 ) & 0xff;
33 fcolor->g /= 255;
34 fcolor->b = ( temp >> 0 ) & 0xff;
35 fcolor->b /= 255;
36 }
37
38
39 int process_frei0r_item( mlt_service_type type, double position , mlt_properties prop , mlt_frame this, uint8_t **image, mlt_image_format *format, int *width, int *height, int writable ){
40
41 int i=0;
42 f0r_instance_t ( *f0r_construct ) ( unsigned int , unsigned int ) = mlt_properties_get_data( prop , "f0r_construct" ,NULL);
43 void (*f0r_update)(f0r_instance_t instance, double time, const uint32_t* inframe, uint32_t* outframe)=mlt_properties_get_data( prop , "f0r_update" ,NULL);
44 void (*f0r_destruct)(f0r_instance_t instance)=mlt_properties_get_data( prop , "f0r_destruct" ,NULL);
45
46 void (*f0r_get_plugin_info)(f0r_plugin_info_t*)=mlt_properties_get_data( prop, "f0r_get_plugin_info" ,NULL);
47 void (*f0r_get_param_info)(f0r_param_info_t* info, int param_index)=mlt_properties_get_data( prop , "f0r_get_param_info" ,NULL);
48 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);
49 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);
50 void (*f0r_update2) (f0r_instance_t instance, double time,
51 const uint32_t* inframe1,const uint32_t* inframe2,const uint32_t* inframe3,
52 uint32_t* outframe)=mlt_properties_get_data( prop , "f0r_update2" ,NULL);
53
54
55 //use as name the width and height
56 f0r_instance_t inst;
57 char ctorname[1024]="";
58 sprintf(ctorname,"ctor-%dx%d",*width,*height);
59
60 void* neu=mlt_properties_get_data( prop , ctorname ,NULL );
61 if (!f0r_construct){
62 //printf("no ctor\n");
63 return -1;
64 }
65 if ( neu == 0 ){
66 inst= f0r_construct(*width,*height);
67 mlt_properties_set_data( prop , ctorname , inst, sizeof(void*) , f0r_destruct , NULL );;
68 }else{
69 inst=mlt_properties_get_data( prop , ctorname , NULL );
70 }
71 if (f0r_get_plugin_info){
72 f0r_plugin_info_t info;
73 f0r_get_plugin_info(&info);
74 for (i=0;i<info.num_params;i++){
75 f0r_param_info_t pinfo;
76 f0r_get_param_info(&pinfo,i);
77 mlt_geometry geom=mlt_geometry_init();
78 struct mlt_geometry_item_s item;
79 //set param if found
80
81 double t=0.0;
82 f0r_get_param_value(inst,&t,i);
83 char *val;
84 if (mlt_properties_get( prop , pinfo.name ) !=NULL ){
85 switch (pinfo.type) {
86 case F0R_PARAM_DOUBLE:
87 case F0R_PARAM_BOOL:
88 val=mlt_properties_get(prop, pinfo.name );
89 mlt_geometry_parse(geom,val,-1,-1,-1);
90 mlt_geometry_fetch(geom,&item,position);
91 t=item.x;
92 f0r_set_param_value(inst,&t,i);
93 break;
94 case F0R_PARAM_COLOR:
95 {
96 f0r_param_color_t color;
97 parse_color(mlt_properties_get(prop , pinfo.name), &color);
98 f0r_set_param_value(inst, &color, i);
99 break;
100 }
101
102 }
103 }
104
105 mlt_geometry_close(geom);
106 }
107 }
108
109 int video_area = *width * *height;
110 uint32_t *img_a = mlt_pool_alloc( video_area * sizeof(uint32_t) );
111 uint32_t *img_b = mlt_pool_alloc( video_area * sizeof(uint32_t) );
112
113 if (type==filter_type){
114 mlt_convert_yuv422_to_rgb24a(*image, (uint8_t *)img_a, video_area);
115 f0r_update ( inst , position , img_a , img_b );
116 mlt_convert_rgb24a_to_yuv422((uint8_t *)img_b , *width, *height, *width * sizeof(uint32_t),
117 *image, NULL );
118 }else if (type==transition_type && f0r_update2 ){
119 uint32_t *result = mlt_pool_alloc( video_area * sizeof(uint32_t) );
120
121 mlt_convert_yuv422_to_rgb24a ( image[0] , (uint8_t *)img_a , video_area );
122 mlt_convert_yuv422_to_rgb24a ( image[1] , (uint8_t *)img_b , video_area );
123 f0r_update2 ( inst , position , img_a , img_b , NULL , result );
124
125 uint8_t * image_ptr=mlt_properties_get_data(MLT_FRAME_PROPERTIES(this), "image", NULL );
126 if (image_ptr)
127 mlt_convert_rgb24a_to_yuv422((uint8_t *)result, *width, *height, *width * sizeof(uint32_t), image_ptr , NULL );
128
129 mlt_pool_release(result);
130 }
131 mlt_pool_release(img_a);
132 mlt_pool_release(img_b);
133
134 return 0;
135 }
136
137 void destruct (mlt_properties prop ) {
138
139 void (*f0r_destruct)(f0r_instance_t instance)=mlt_properties_get_data( prop , "f0r_destruct" , NULL );
140 void (*f0r_deinit)(void)=mlt_properties_get_data ( prop , "f0r_deinit" , NULL);
141 int i=0;
142
143 if ( f0r_deinit != NULL )
144 f0r_deinit();
145
146 for ( i=0 ; i < mlt_properties_count ( prop ) ; i++ ){
147 if ( strstr ( mlt_properties_get_name ( prop , i ) , "ctor-" ) != NULL ){
148 void * inst=mlt_properties_get_data( prop , mlt_properties_get_name ( prop , i ) , NULL );
149 if ( inst != NULL ){
150 f0r_destruct( (f0r_instance_t) inst);
151 }
152 }
153 }
154 void (*dlclose)(void*)=mlt_properties_get_data ( prop , "_dlclose" , NULL);
155 void *handle=mlt_properties_get_data ( prop , "_dlclose_handle" , NULL);
156
157 if (handle && dlclose ){
158 dlclose ( handle );
159 }
160
161 }