r863: Merge 2.1:
[cinelerra_cv/ct.git] / plugins / 1080to480 / 1080to480.C
blob3659b3a6a3af6c53dc7a6f6096ecd7a737ba1351
1 #include "1080to480.h"
2 #include "clip.h"
3 #include "bchash.h"
4 #include "filexml.h"
5 #include "bcdisplayinfo.h"
6 #include "keyframe.h"
7 #include "language.h"
8 #include "overlayframe.h"
9 #include "picon_png.h"
10 #include "vframe.h"
17 #include <stdint.h>
18 #include <string.h>
21 REGISTER_PLUGIN(_1080to480Main)
26 _1080to480Config::_1080to480Config()
28         first_field = 0;
31 int _1080to480Config::equivalent(_1080to480Config &that)
33         return first_field == that.first_field;
36 void _1080to480Config::copy_from(_1080to480Config &that)
38         first_field = that.first_field;
41 void _1080to480Config::interpolate(_1080to480Config &prev, 
42         _1080to480Config &next, 
43         long prev_frame, 
44         long next_frame, 
45         long current_frame)
47         copy_from(prev);
54 _1080to480Window::_1080to480Window(_1080to480Main *client, int x, int y)
55  : BC_Window(client->gui_string, 
56         x, 
57         y, 
58         200, 
59         100, 
60         200, 
61         100, 
62         0, 
63         0,
64         1)
65
66         this->client = client; 
70 _1080to480Window::~_1080to480Window()
74 int _1080to480Window::create_objects()
76         int x = 10, y = 10;
78         add_tool(odd_first = new _1080to480Option(client, this, 1, x, y, _("Odd field first")));
79         y += 25;
80         add_tool(even_first = new _1080to480Option(client, this, 0, x, y, _("Even field first")));
82         show_window();
83         flush();
84         return 0;
87 WINDOW_CLOSE_EVENT(_1080to480Window)
89 int _1080to480Window::set_first_field(int first_field, int send_event)
91         odd_first->update(first_field == 1);
92         even_first->update(first_field == 0);
95         if(send_event)
96         {
97                 client->config.first_field = first_field;
98                 client->send_configure_change();
99         }
100         return 0;
106 _1080to480Option::_1080to480Option(_1080to480Main *client, 
107                 _1080to480Window *window, 
108                 int output, 
109                 int x, 
110                 int y, 
111                 char *text)
112  : BC_Radial(x, 
113         y, 
114         client->config.first_field == output, 
115         text)
117         this->client = client;
118         this->window = window;
119         this->output = output;
122 int _1080to480Option::handle_event()
124         window->set_first_field(output, 1);
125         return 1;
130 PLUGIN_THREAD_OBJECT(_1080to480Main, _1080to480Thread, _1080to480Window)
138 _1080to480Main::_1080to480Main(PluginServer *server)
139  : PluginVClient(server)
141         PLUGIN_CONSTRUCTOR_MACRO
142         temp = 0;
145 _1080to480Main::~_1080to480Main()
147         PLUGIN_DESTRUCTOR_MACRO
148         if(temp) delete temp;
151 char* _1080to480Main::plugin_title() { return N_("1080 to 480"); }
152 int _1080to480Main::is_realtime() { return 1; }
154 SHOW_GUI_MACRO(_1080to480Main, _1080to480Thread)
155 RAISE_WINDOW_MACRO(_1080to480Main)
156 SET_STRING_MACRO(_1080to480Main)
157 NEW_PICON_MACRO(_1080to480Main)
158 LOAD_CONFIGURATION_MACRO(_1080to480Main, _1080to480Config)
161 #define TEMP_W 854
162 #define TEMP_H 480
163 #define OUT_ROWS 240
165 void _1080to480Main::reduce_field(VFrame *output, VFrame *input, int src_field, int dst_field)
167         int w = input->get_w();
168         int h = input->get_h();
170         if(h > output->get_h()) h = output->get_h();
171         if(w > output->get_w()) h = output->get_w();
173 #define REDUCE_MACRO(type, temp, components) \
174 for(int i = 0; i < OUT_ROWS; i++) \
175 { \
176         int in_number1 = dst_field * 2 + src_field + (int)(i * 9 / 4) * 2; \
177         int in_number2 = in_number1 + 2; \
178         int in_number3 = in_number2 + 2; \
179         int in_number4 = in_number3 + 2; \
180         int out_number = dst_field + i * 2; \
182         if(in_number1 >= h) in_number1 = h - 1; \
183         if(in_number2 >= h) in_number2 = h - 1; \
184         if(in_number3 >= h) in_number3 = h - 1; \
185         if(in_number4 >= h) in_number4 = h - 1; \
186         if(out_number >= h) out_number = h - 1; \
188         type *in_row1 = (type*)input->get_rows()[in_number1]; \
189         type *in_row2 = (type*)input->get_rows()[in_number2]; \
190         type *in_row3 = (type*)input->get_rows()[in_number3]; \
191         type *in_row4 = (type*)input->get_rows()[in_number4]; \
192         type *out_row = (type*)output->get_rows()[out_number]; \
194         for(int j = 0; j < w * components; j++) \
195         { \
196                 *out_row++ = ((temp)*in_row1++ +  \
197                         (temp)*in_row2++ +  \
198                         (temp)*in_row3++ +  \
199                         (temp)*in_row4++) / 4; \
200         } \
203         switch(input->get_color_model())
204         {
205                 case BC_RGB888:
206                 case BC_YUV888:
207                         REDUCE_MACRO(unsigned char, int64_t, 3);
208                         break;
209                 case BC_RGB_FLOAT:
210                         REDUCE_MACRO(float, float, 3);
211                         break;
212                 case BC_RGBA8888:
213                 case BC_YUVA8888:
214                         REDUCE_MACRO(unsigned char, int64_t, 4);
215                         break;
216                 case BC_RGBA_FLOAT:
217                         REDUCE_MACRO(float, float, 4);
218                         break;
219                 case BC_RGB161616:
220                 case BC_YUV161616:
221                         REDUCE_MACRO(uint16_t, int64_t, 3);
222                         break;
223                 case BC_RGBA16161616:
224                 case BC_YUVA16161616:
225                         REDUCE_MACRO(uint16_t, int64_t, 4);
226                         break;
227         }
231 int _1080to480Main::process_realtime(VFrame *input, VFrame *output)
233         load_configuration();
234         if(!temp)
235         {
236                 temp = new VFrame(0,
237                         input->get_w(),
238                         input->get_h(),
239                         input->get_color_model());
240                 temp->clear_frame();
241         }
243         reduce_field(temp, input, config.first_field == 0 ? 0 : 1, 0);
244         reduce_field(temp, input, config.first_field == 0 ? 1 : 0, 1);
245         
246         output->copy_from(temp);
248         return 0;
252 int _1080to480Main::load_defaults()
254         char directory[BCTEXTLEN], string[BCTEXTLEN];
255         sprintf(directory, "%s1080to480.rc", BCASTDIR);
256         
257         defaults = new BC_Hash(directory);
258         defaults->load();
259         config.first_field = defaults->get("FIRST_FIELD", config.first_field);
260         return 0;
264 int _1080to480Main::save_defaults()
266         defaults->update("FIRST_FIELD", config.first_field);
267         defaults->save();
268         return 0;
271 void _1080to480Main::save_data(KeyFrame *keyframe)
273         FileXML output;
274         output.set_shared_string(keyframe->data, MESSAGESIZE);
275         output.tag.set_title("1080TO480");
276         output.tag.set_property("FIRST_FIELD", config.first_field);
277         output.append_tag();
278         output.terminate_string();
281 void _1080to480Main::read_data(KeyFrame *keyframe)
283         FileXML input;
284         input.set_shared_string(keyframe->data, strlen(keyframe->data));
286         while(!input.read_tag())
287         {
288                 if(input.tag.title_is("1080TO480"))
289                 {
290                         config.first_field = input.tag.get_property("FIRST_FIELD", config.first_field);
291                 }
292         }
295 void _1080to480Main::update_gui()
297         if(thread) 
298         {
299                 load_configuration();
300                 thread->window->lock_window();
301                 thread->window->set_first_field(config.first_field, 0);
302                 thread->window->unlock_window();
303         }