6d62e8737765e5f9acc9c52876122ec0bbf6cd5f
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / 720to480 / 720to480.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include "720to480.h"
23 #include "clip.h"
24 #include "bchash.h"
25 #include "file.h"
26 #include "filexml.h"
27 #include "bcdisplayinfo.h"
28 #include "keyframe.h"
29 #include "language.h"
30 #include "mainprogress.h"
31 #include "vframe.h"
32
33
34 #include <stdint.h>
35 #include <string.h>
36
37
38 REGISTER_PLUGIN(_720to480Main)
39
40
41 #define FORWARD 0
42 #define REVERSE 1
43
44 _720to480Config::_720to480Config()
45 {
46         first_field = 0;
47         direction = FORWARD;
48 }
49
50
51
52
53
54 _720to480Window::_720to480Window(_720to480Main *client, int x, int y)
55  : BC_Window(client->gui_string, x, y, xS(230), yS(150), xS(230), yS(150), 0, 0, 1)
56 {
57         this->client = client;
58 }
59
60
61 _720to480Window::~_720to480Window()
62 {
63 }
64
65 void _720to480Window::create_objects()
66 {
67         lock_window("720to480Window::create_objects");
68         int x = xS(10), y = yS(10);
69
70         add_tool(odd_first = new _720to480Order(client, this, 1, x, y, _("Odd field first")));
71         y += yS(25);
72         add_tool(even_first = new _720to480Order(client, this, 0, x, y, _("Even field first")));
73
74 //      y += yS(25);
75 //      add_tool(forward = new _720to480Direction(client, this, FORWARD, x, y, _("Downsample")));
76 //      y += yS(25);
77 //      add_tool(reverse = new _720to480Direction(client, this, REVERSE, x, y, _("Upsample")));
78 //
79         add_subwindow(new BC_OKButton(this));
80         add_subwindow(new BC_CancelButton(this));
81
82         show_window();
83         flush();
84         unlock_window();
85 }
86
87 int _720to480Window::close_event()
88 {
89         set_done(0);
90         return 1;
91 }
92
93 void _720to480Window::set_first_field(int first_field)
94 {
95         odd_first->update(first_field == 1);
96         even_first->update(first_field == 0);
97
98         client->config.first_field = first_field;
99 }
100
101 void _720to480Window::set_direction(int direction)
102 {
103         forward->update(direction == FORWARD);
104         reverse->update(direction == REVERSE);
105
106
107         client->config.direction = direction;
108 }
109
110
111
112 _720to480Order::_720to480Order(_720to480Main *client,
113                 _720to480Window *window,
114                 int output,
115                 int x,
116                 int y,
117                 char *text)
118  : BC_Radial(x,
119         y,
120         client->config.first_field == output,
121         text)
122 {
123         this->client = client;
124         this->window = window;
125         this->output = output;
126 }
127
128 int _720to480Order::handle_event()
129 {
130         window->set_first_field(output);
131         return 1;
132 }
133
134
135
136
137
138 _720to480Direction::_720to480Direction(_720to480Main *client,
139                 _720to480Window *window,
140                 int output,
141                 int x,
142                 int y,
143                 char *text)
144  : BC_Radial(x,
145         y,
146         client->config.direction == output,
147         text)
148 {
149         this->client = client;
150         this->window = window;
151         this->output = output;
152 }
153
154 int _720to480Direction::handle_event()
155 {
156         window->set_direction(output);
157         return 1;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173 _720to480Main::_720to480Main(PluginServer *server)
174  : PluginVClient(server)
175 {
176         temp = 0;
177 }
178
179 _720to480Main::~_720to480Main()
180 {
181
182         if(temp) delete temp;
183 }
184
185 const char* _720to480Main::plugin_title() { return N_("720 to 480"); }
186 int _720to480Main::is_realtime() { return 0; }
187
188 double _720to480Main::get_framerate()
189 {
190         return project_frame_rate / 2;
191 }
192
193
194
195
196
197 int _720to480Main::load_defaults()
198 {
199         char directory[BCTEXTLEN];
200         sprintf(directory, "%s/720to480.rc", File::get_config_path());
201
202         defaults = new BC_Hash(directory);
203         defaults->load();
204         config.first_field = defaults->get("FIRST_FIELD", config.first_field);
205         config.direction = defaults->get("DIRECTION", config.direction);
206         return 0;
207 }
208
209
210 int _720to480Main::save_defaults()
211 {
212         defaults->update("FIRST_FIELD", config.first_field);
213         defaults->update("DIRECTION", config.direction);
214         defaults->save();
215         return 0;
216 }
217
218 int _720to480Main::get_parameters()
219 {
220         BC_DisplayInfo info;
221         _720to480Window window(this,
222                 info.get_abs_cursor_x(),
223                 info.get_abs_cursor_y());
224         window.create_objects();
225         int result = window.run_window();
226         return result;
227 }
228
229 int _720to480Main::start_loop()
230 {
231         if(PluginClient::interactive)
232         {
233                 char string[BCTEXTLEN];
234                 sprintf(string, "%s...", plugin_title());
235                 progress = start_progress(string,
236                         PluginClient::end - PluginClient::start);
237         }
238
239         input_position = PluginClient::start;
240         return 0;
241 }
242
243
244 int _720to480Main::stop_loop()
245 {
246         if(PluginClient::interactive)
247         {
248                 progress->stop_progress();
249                 delete progress;
250         }
251         return 0;
252 }
253
254
255 #define DST_W 854
256 #define DST_H 240
257
258
259 void _720to480Main::reduce_field(VFrame *output, VFrame *input, int dest_row)
260 {
261         int in_w = input->get_w();
262         int in_h = input->get_h();
263         int out_w = output->get_w();
264         int out_h = output->get_h();
265
266 #define REDUCE_MACRO(type, temp, components) \
267 for(int i = 0; i < DST_H; i++) \
268 { \
269         int output_number = dest_row + i * 2; \
270         if(output_number >= out_h) break; \
271  \
272         int in1 = i * 3 + dest_row * 2; \
273         int in2 = i * 3 + 1 + dest_row * 2; \
274         int in3 = i * 3 + 2 + dest_row * 2; \
275  \
276         if(in1 >= in_h) in1 = in_h - 1; \
277         if(in2 >= in_h) in2 = in_h - 1; \
278         if(in3 >= in_h) in3 = in_h - 1; \
279  \
280         type *out_row = (type*)output->get_rows()[output_number]; \
281         type *in_row1 = (type*)input->get_rows()[in1]; \
282         type *in_row2 = (type*)input->get_rows()[in2]; \
283         type *in_row3 = (type*)input->get_rows()[in3]; \
284  \
285         int w = MIN(out_w, in_w) * components; \
286         for(int j = 0; j < w; j++) \
287         { \
288                 *out_row++ = ((temp)*in_row1++ + \
289                         (temp)*in_row2++ + \
290                         (temp)*in_row3++) / 3; \
291         } \
292 }
293
294         switch(input->get_color_model())
295         {
296                 case BC_RGB888:
297                 case BC_YUV888:
298                         REDUCE_MACRO(unsigned char, int64_t, 3);
299                         break;
300                 case BC_RGB_FLOAT:
301                         REDUCE_MACRO(float, float, 3);
302                         break;
303                 case BC_RGBA8888:
304                 case BC_YUVA8888:
305                         REDUCE_MACRO(unsigned char, int64_t, 4);
306                         break;
307                 case BC_RGBA_FLOAT:
308                         REDUCE_MACRO(float, float, 4);
309                         break;
310                 case BC_RGB161616:
311                 case BC_YUV161616:
312                         REDUCE_MACRO(uint16_t, int64_t, 3);
313                         break;
314                 case BC_RGBA16161616:
315                 case BC_YUVA16161616:
316                         REDUCE_MACRO(uint16_t, int64_t, 4);
317                         break;
318         }
319 }
320
321 int _720to480Main::process_loop(VFrame *output)
322 {
323         int result = 0;
324
325         if(!temp)
326                 temp = new VFrame(output->get_w(), output->get_h(),
327                                 output->get_color_model(), 0);
328
329         if(config.direction == FORWARD)
330         {
331 // Step 1: Reduce vertically and put in desired fields of output
332                 read_frame(temp, input_position);
333                 reduce_field(output, temp, config.first_field == 0 ? 0 : 1);
334                 input_position++;
335
336                 read_frame(temp, input_position);
337                 reduce_field(output, temp, config.first_field == 0 ? 1 : 0);
338                 input_position++;
339         }
340
341         if(PluginClient::interactive)
342                 result = progress->update(input_position - PluginClient::start);
343
344         if(input_position >= PluginClient::end) result = 1;
345
346         return result;
347 }
348
349
350
351
352
353
354 void _720to480Main::save_data(KeyFrame *keyframe)
355 {
356         FileXML output;
357         output.set_shared_output(keyframe->xbuf);
358         output.tag.set_title("720TO480");
359         output.tag.set_property("FIRST_FIELD", config.first_field);
360         output.tag.set_property("DIRECTION", config.direction);
361         output.append_tag();
362         output.tag.set_title("/720TO480");
363         output.append_tag();
364         output.append_newline();
365         output.terminate_string();
366 }
367
368 void _720to480Main::read_data(KeyFrame *keyframe)
369 {
370         FileXML input;
371         input.set_shared_input(keyframe->xbuf);
372
373         while(!input.read_tag())
374         {
375                 if(input.tag.title_is("720TO480"))
376                 {
377                         config.first_field = input.tag.get_property("FIRST_FIELD", config.first_field);
378                         config.direction = input.tag.get_property("DIRECTION", config.direction);
379                 }
380         }
381 }
382