r125: This commit was manufactured by cvs2svn to create tag 'r1_1_7-last'.
[cinelerra_cv/mob.git] / hvirtual / plugins / 720to480 / 720to480.C
blobf96d1b641a942d4e27f52bb448a1618eef095960
1 #include "720to480.h"
2 #include "clip.h"
3 #include "defaults.h"
4 #include "filexml.h"
5 #include "bcdisplayinfo.h"
6 #include "keyframe.h"
7 #include "mainprogress.h"
8 #include "vframe.h"
10 #include <libintl.h>
11 #define _(String) gettext(String)
12 #define gettext_noop(String) String
13 #define N_(String) gettext_noop (String)
16 #include <stdint.h>
17 #include <string.h>
20 REGISTER_PLUGIN(_720to480Main)
23 #define FORWARD 0
24 #define REVERSE 1
26 _720to480Config::_720to480Config()
28         first_field = 0;
29         direction = FORWARD;
36 _720to480Window::_720to480Window(_720to480Main *client, int x, int y)
37  : BC_Window(client->gui_string, 
38         x, 
39         y, 
40         230, 
41         150, 
42         230, 
43         150, 
44         0, 
45         0,
46         1)
47
48         this->client = client; 
52 _720to480Window::~_720to480Window()
56 int _720to480Window::create_objects()
58         int x = 10, y = 10;
60         add_tool(odd_first = new _720to480Order(client, this, 1, x, y, _("Odd field first")));
61         y += 25;
62         add_tool(even_first = new _720to480Order(client, this, 0, x, y, _("Even field first")));
64 //      y += 25;
65 //      add_tool(forward = new _720to480Direction(client, this, FORWARD, x, y, _("Downsample")));
66 //      y += 25;
67 //      add_tool(reverse = new _720to480Direction(client, this, REVERSE, x, y, _("Upsample")));
68 // 
69         add_subwindow(new BC_OKButton(this));
70         add_subwindow(new BC_CancelButton(this));
72         show_window();
73         flush();
74         return 0;
77 WINDOW_CLOSE_EVENT(_720to480Window)
79 int _720to480Window::set_first_field(int first_field)
81         odd_first->update(first_field == 1);
82         even_first->update(first_field == 0);
84         client->config.first_field = first_field;
85         return 0;
88 int _720to480Window::set_direction(int direction)
90         forward->update(direction == FORWARD);
91         reverse->update(direction == REVERSE);
94         client->config.direction = direction;
95         return 0;
100 _720to480Order::_720to480Order(_720to480Main *client, 
101                 _720to480Window *window, 
102                 int output, 
103                 int x, 
104                 int y, 
105                 char *text)
106  : BC_Radial(x, 
107         y, 
108         client->config.first_field == output, 
109         text)
111         this->client = client;
112         this->window = window;
113         this->output = output;
116 int _720to480Order::handle_event()
118         window->set_first_field(output);
119         return 1;
126 _720to480Direction::_720to480Direction(_720to480Main *client, 
127                 _720to480Window *window, 
128                 int output, 
129                 int x, 
130                 int y, 
131                 char *text)
132  : BC_Radial(x, 
133         y, 
134         client->config.direction == output, 
135         text)
137         this->client = client;
138         this->window = window;
139         this->output = output;
142 int _720to480Direction::handle_event()
144         window->set_direction(output);
145         return 1;
163 _720to480Main::_720to480Main(PluginServer *server)
164  : PluginVClient(server)
166         temp = 0;
167         load_defaults();
170 _720to480Main::~_720to480Main()
172         save_defaults();
173         delete defaults;
175         if(temp) delete temp;
178 char* _720to480Main::plugin_title() { return _("720 to 480"); }
179 int _720to480Main::is_realtime() { return 0; }
181 double _720to480Main::get_framerate()
183         return project_frame_rate / 2;
190 int _720to480Main::load_defaults()
192         char directory[BCTEXTLEN], string[BCTEXTLEN];
193         sprintf(directory, "%s720to480.rc", BCASTDIR);
194         
195         defaults = new Defaults(directory);
196         defaults->load();
197         config.first_field = defaults->get("FIRST_FIELD", config.first_field);
198         config.direction = defaults->get("DIRECTION", config.direction);
199         return 0;
203 int _720to480Main::save_defaults()
205         defaults->update("FIRST_FIELD", config.first_field);
206         defaults->update("DIRECTION", config.direction);
207         defaults->save();
208         return 0;
211 int _720to480Main::get_parameters()
213         BC_DisplayInfo info;
214         _720to480Window window(this, 
215                 info.get_abs_cursor_x(), 
216                 info.get_abs_cursor_y());
217         window.create_objects();
218         int result = window.run_window();
219         return result;
222 int _720to480Main::start_loop()
224         if(PluginClient::interactive)
225         {
226                 char string[BCTEXTLEN];
227                 sprintf(string, "%s...", plugin_title());
228                 progress = start_progress(string, 
229                         PluginClient::end - PluginClient::start);
230         }
232         input_position = PluginClient::start;
233         return 0;
237 int _720to480Main::stop_loop()
239         if(PluginClient::interactive)
240         {
241                 progress->stop_progress();
242                 delete progress;
243         }
244         return 0;
248 #define DST_W 854
249 #define DST_H 240
252 void _720to480Main::reduce_field(VFrame *output, VFrame *input, int dest_row)
254         int in_w = input->get_w();
255         int in_h = input->get_h();
256         int out_w = output->get_w();
257         int out_h = output->get_h();
259 #define REDUCE_MACRO(type, components) \
260 for(int i = 0; i < DST_H; i++) \
261 { \
262         int output_number = dest_row + i * 2; \
263         if(output_number >= out_h) break; \
265         int in1 = i * 3 + dest_row * 2; \
266         int in2 = i * 3 + 1 + dest_row * 2; \
267         int in3 = i * 3 + 2 + dest_row * 2; \
269         if(in1 >= in_h) in1 = in_h - 1; \
270         if(in2 >= in_h) in2 = in_h - 1; \
271         if(in3 >= in_h) in3 = in_h - 1; \
273         type *out_row = (type*)output->get_rows()[output_number]; \
274         type *in_row1 = (type*)input->get_rows()[in1]; \
275         type *in_row2 = (type*)input->get_rows()[in2]; \
276         type *in_row3 = (type*)input->get_rows()[in3]; \
278         int w = MIN(out_w, in_w) * components; \
279         for(int j = 0; j < w; j++) \
280         { \
281                 *out_row++ = ((int64_t)*in_row1++ + (int64_t)*in_row2++ + (int64_t)*in_row3++) / 3; \
282         } \
285         switch(input->get_color_model())
286         {
287                 case BC_RGB888:
288                 case BC_YUV888:
289                         REDUCE_MACRO(unsigned char, 3);
290                         break;
291                 case BC_RGBA8888:
292                 case BC_YUVA8888:
293                         REDUCE_MACRO(unsigned char, 4);
294                         break;
295                 case BC_RGB161616:
296                 case BC_YUV161616:
297                         REDUCE_MACRO(uint16_t, 3);
298                         break;
299                 case BC_RGBA16161616:
300                 case BC_YUVA16161616:
301                         REDUCE_MACRO(uint16_t, 4);
302                         break;
303         }
306 int _720to480Main::process_loop(VFrame *output)
308         int result = 0;
310         if(!temp)
311                 temp = new VFrame(0,
312                         output->get_w(),
313                         output->get_h(),
314                         output->get_color_model());
316         if(config.direction == FORWARD)
317         {
318 // Step 1: Reduce vertically and put in desired fields of output
319                 read_frame(temp, input_position);
320                 reduce_field(output, temp, config.first_field == 0 ? 0 : 1);
321                 input_position++;
323                 read_frame(temp, input_position);
324                 reduce_field(output, temp, config.first_field == 0 ? 1 : 0);
325                 input_position++;
326         }
328         if(PluginClient::interactive) 
329                 result = progress->update(input_position - PluginClient::start);
331         if(input_position >= PluginClient::end) result = 1;
333         return result;
341 void _720to480Main::save_data(KeyFrame *keyframe)
343         FileXML output;
344         output.set_shared_string(keyframe->data, MESSAGESIZE);
345         output.tag.set_title("720TO480");
346         output.tag.set_property("FIRST_FIELD", config.first_field);
347         output.tag.set_property("DIRECTION", config.direction);
348         output.append_tag();
349         output.terminate_string();
352 void _720to480Main::read_data(KeyFrame *keyframe)
354         FileXML input;
355         input.set_shared_string(keyframe->data, strlen(keyframe->data));
357         while(!input.read_tag())
358         {
359                 if(input.tag.title_is("720TO480"))
360                 {
361                         config.first_field = input.tag.get_property("FIRST_FIELD", config.first_field);
362                         config.direction = input.tag.get_property("DIRECTION", config.direction);
363                 }
364         }