no longer need ffmpeg patch0 which was for Termux
[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 // *** CONTEXT_HELP ***
59         if(client) context_help_set_keyword(client->plugin_title());
60         else       context_help_set_keyword("Rendered Video Effects");
61 }
62
63
64 _720to480Window::~_720to480Window()
65 {
66 }
67
68 void _720to480Window::create_objects()
69 {
70         lock_window("720to480Window::create_objects");
71         int x = xS(10), y = yS(10);
72
73         add_tool(odd_first = new _720to480Order(client, this, 1, x, y, _("Odd field first")));
74         y += yS(25);
75         add_tool(even_first = new _720to480Order(client, this, 0, x, y, _("Even field first")));
76
77 //      y += yS(25);
78 //      add_tool(forward = new _720to480Direction(client, this, FORWARD, x, y, _("Downsample")));
79 //      y += yS(25);
80 //      add_tool(reverse = new _720to480Direction(client, this, REVERSE, x, y, _("Upsample")));
81 //
82         add_subwindow(new BC_OKButton(this));
83         add_subwindow(new BC_CancelButton(this));
84
85         show_window();
86         flush();
87         unlock_window();
88 }
89
90 int _720to480Window::close_event()
91 {
92         set_done(0);
93         return 1;
94 }
95
96 void _720to480Window::set_first_field(int first_field)
97 {
98         odd_first->update(first_field == 1);
99         even_first->update(first_field == 0);
100
101         client->config.first_field = first_field;
102 }
103
104 void _720to480Window::set_direction(int direction)
105 {
106         forward->update(direction == FORWARD);
107         reverse->update(direction == REVERSE);
108
109
110         client->config.direction = direction;
111 }
112
113
114
115 _720to480Order::_720to480Order(_720to480Main *client,
116                 _720to480Window *window,
117                 int output,
118                 int x,
119                 int y,
120                 char *text)
121  : BC_Radial(x,
122         y,
123         client->config.first_field == output,
124         text)
125 {
126         this->client = client;
127         this->window = window;
128         this->output = output;
129 }
130
131 int _720to480Order::handle_event()
132 {
133         window->set_first_field(output);
134         return 1;
135 }
136
137
138
139
140
141 _720to480Direction::_720to480Direction(_720to480Main *client,
142                 _720to480Window *window,
143                 int output,
144                 int x,
145                 int y,
146                 char *text)
147  : BC_Radial(x,
148         y,
149         client->config.direction == output,
150         text)
151 {
152         this->client = client;
153         this->window = window;
154         this->output = output;
155 }
156
157 int _720to480Direction::handle_event()
158 {
159         window->set_direction(output);
160         return 1;
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 _720to480Main::_720to480Main(PluginServer *server)
177  : PluginVClient(server)
178 {
179         temp = 0;
180 }
181
182 _720to480Main::~_720to480Main()
183 {
184
185         if(temp) delete temp;
186 }
187
188 const char* _720to480Main::plugin_title() { return N_("720 to 480"); }
189 int _720to480Main::is_realtime() { return 0; }
190
191 double _720to480Main::get_framerate()
192 {
193         return project_frame_rate / 2;
194 }
195
196
197
198
199
200 int _720to480Main::load_defaults()
201 {
202         char directory[BCTEXTLEN];
203         sprintf(directory, "%s/720to480.rc", File::get_config_path());
204
205         defaults = new BC_Hash(directory);
206         defaults->load();
207         config.first_field = defaults->get("FIRST_FIELD", config.first_field);
208         config.direction = defaults->get("DIRECTION", config.direction);
209         return 0;
210 }
211
212
213 int _720to480Main::save_defaults()
214 {
215         defaults->update("FIRST_FIELD", config.first_field);
216         defaults->update("DIRECTION", config.direction);
217         defaults->save();
218         return 0;
219 }
220
221 int _720to480Main::get_parameters()
222 {
223         BC_DisplayInfo info;
224         _720to480Window window(this,
225                 info.get_abs_cursor_x(),
226                 info.get_abs_cursor_y());
227         window.create_objects();
228         int result = window.run_window();
229         return result;
230 }
231
232 int _720to480Main::start_loop()
233 {
234         if(PluginClient::interactive)
235         {
236                 char string[BCTEXTLEN];
237                 sprintf(string, "%s...", plugin_title());
238                 progress = start_progress(string,
239                         PluginClient::end - PluginClient::start);
240         }
241
242         input_position = PluginClient::start;
243         return 0;
244 }
245
246
247 int _720to480Main::stop_loop()
248 {
249         if(PluginClient::interactive)
250         {
251                 progress->stop_progress();
252                 delete progress;
253         }
254         return 0;
255 }
256
257
258 #define DST_W 854
259 #define DST_H 240
260
261
262 void _720to480Main::reduce_field(VFrame *output, VFrame *input, int dest_row)
263 {
264         int in_w = input->get_w();
265         int in_h = input->get_h();
266         int out_w = output->get_w();
267         int out_h = output->get_h();
268
269 #define REDUCE_MACRO(type, temp, components) \
270 for(int i = 0; i < DST_H; i++) \
271 { \
272         int output_number = dest_row + i * 2; \
273         if(output_number >= out_h) break; \
274  \
275         int in1 = i * 3 + dest_row * 2; \
276         int in2 = i * 3 + 1 + dest_row * 2; \
277         int in3 = i * 3 + 2 + dest_row * 2; \
278  \
279         if(in1 >= in_h) in1 = in_h - 1; \
280         if(in2 >= in_h) in2 = in_h - 1; \
281         if(in3 >= in_h) in3 = in_h - 1; \
282  \
283         type *out_row = (type*)output->get_rows()[output_number]; \
284         type *in_row1 = (type*)input->get_rows()[in1]; \
285         type *in_row2 = (type*)input->get_rows()[in2]; \
286         type *in_row3 = (type*)input->get_rows()[in3]; \
287  \
288         int w = MIN(out_w, in_w) * components; \
289         for(int j = 0; j < w; j++) \
290         { \
291                 *out_row++ = ((temp)*in_row1++ + \
292                         (temp)*in_row2++ + \
293                         (temp)*in_row3++) / 3; \
294         } \
295 }
296
297         switch(input->get_color_model())
298         {
299                 case BC_RGB888:
300                 case BC_YUV888:
301                         REDUCE_MACRO(unsigned char, int64_t, 3);
302                         break;
303                 case BC_RGB_FLOAT:
304                         REDUCE_MACRO(float, float, 3);
305                         break;
306                 case BC_RGBA8888:
307                 case BC_YUVA8888:
308                         REDUCE_MACRO(unsigned char, int64_t, 4);
309                         break;
310                 case BC_RGBA_FLOAT:
311                         REDUCE_MACRO(float, float, 4);
312                         break;
313                 case BC_RGB161616:
314                 case BC_YUV161616:
315                         REDUCE_MACRO(uint16_t, int64_t, 3);
316                         break;
317                 case BC_RGBA16161616:
318                 case BC_YUVA16161616:
319                         REDUCE_MACRO(uint16_t, int64_t, 4);
320                         break;
321         }
322 }
323
324 int _720to480Main::process_loop(VFrame *output)
325 {
326         int result = 0;
327
328         if(!temp)
329                 temp = new VFrame(output->get_w(), output->get_h(),
330                                 output->get_color_model(), 0);
331
332         if(config.direction == FORWARD)
333         {
334 // Step 1: Reduce vertically and put in desired fields of output
335                 read_frame(temp, input_position);
336                 reduce_field(output, temp, config.first_field == 0 ? 0 : 1);
337                 input_position++;
338
339                 read_frame(temp, input_position);
340                 reduce_field(output, temp, config.first_field == 0 ? 1 : 0);
341                 input_position++;
342         }
343
344         if(PluginClient::interactive)
345                 result = progress->update(input_position - PluginClient::start);
346
347         if(input_position >= PluginClient::end) result = 1;
348
349         return result;
350 }
351
352
353
354
355
356
357 void _720to480Main::save_data(KeyFrame *keyframe)
358 {
359         FileXML output;
360         output.set_shared_output(keyframe->xbuf);
361         output.tag.set_title("720TO480");
362         output.tag.set_property("FIRST_FIELD", config.first_field);
363         output.tag.set_property("DIRECTION", config.direction);
364         output.append_tag();
365         output.tag.set_title("/720TO480");
366         output.append_tag();
367         output.append_newline();
368         output.terminate_string();
369 }
370
371 void _720to480Main::read_data(KeyFrame *keyframe)
372 {
373         FileXML input;
374         input.set_shared_input(keyframe->xbuf);
375
376         while(!input.read_tag())
377         {
378                 if(input.tag.title_is("720TO480"))
379                 {
380                         config.first_field = input.tag.get_property("FIRST_FIELD", config.first_field);
381                         config.direction = input.tag.get_property("DIRECTION", config.direction);
382                 }
383         }
384 }
385