~akirad/cinecutie/trunk

« back to all changes in this revision

Viewing changes to plugins/shiftinterlace/shiftinterlace.C

  • Committer: Paolo Rampino
  • Date: 2010-02-17 19:46:21 UTC
  • Revision ID: git-v1:c39ff77ffa6ae08441c12e7d7f54e3897ddde7f1
Initial Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "bcdisplayinfo.h"
 
23
#include "clip.h"
 
24
#include "bchash.h"
 
25
#include "filexml.h"
 
26
#include "guicast.h"
 
27
#include "language.h"
 
28
#include "picon_png.h"
 
29
#include "pluginvclient.h"
 
30
#include "vframe.h"
 
31
 
 
32
 
 
33
 
 
34
#include <stdint.h>
 
35
#include <string.h>
 
36
 
 
37
 
 
38
 
 
39
 
 
40
 
 
41
 
 
42
 
 
43
class ShiftInterlaceWindow;
 
44
class ShiftInterlaceMain;
 
45
 
 
46
class ShiftInterlaceConfig
 
47
{
 
48
public:
 
49
        ShiftInterlaceConfig();
 
50
 
 
51
        int equivalent(ShiftInterlaceConfig &that);
 
52
        void copy_from(ShiftInterlaceConfig &that);
 
53
        void interpolate(ShiftInterlaceConfig &prev, 
 
54
                ShiftInterlaceConfig &next, 
 
55
                long prev_frame, 
 
56
                long next_frame, 
 
57
                long current_frame);
 
58
 
 
59
 
 
60
        int odd_offset;
 
61
        int even_offset;
 
62
};
 
63
 
 
64
 
 
65
class ShiftInterlaceOdd : public BC_ISlider
 
66
{
 
67
public:
 
68
        ShiftInterlaceOdd(ShiftInterlaceMain *plugin, int x, int y);
 
69
        int handle_event();
 
70
        ShiftInterlaceMain *plugin;
 
71
};
 
72
 
 
73
class ShiftInterlaceEven : public BC_ISlider
 
74
{
 
75
public:
 
76
        ShiftInterlaceEven(ShiftInterlaceMain *plugin, int x, int y);
 
77
        int handle_event();
 
78
        ShiftInterlaceMain *plugin;
 
79
};
 
80
 
 
81
class ShiftInterlaceWindow : public BC_Window
 
82
{
 
83
public:
 
84
        ShiftInterlaceWindow(ShiftInterlaceMain *plugin, int x, int y);
 
85
 
 
86
        void create_objects();
 
87
        int close_event();
 
88
 
 
89
        ShiftInterlaceOdd *odd_offset;
 
90
        ShiftInterlaceEven *even_offset;
 
91
        ShiftInterlaceMain *plugin;
 
92
};
 
93
 
 
94
 
 
95
PLUGIN_THREAD_HEADER(ShiftInterlaceMain, ShiftInterlaceThread, ShiftInterlaceWindow)
 
96
 
 
97
 
 
98
 
 
99
 
 
100
class ShiftInterlaceMain : public PluginVClient
 
101
{
 
102
public:
 
103
        ShiftInterlaceMain(PluginServer *server);
 
104
        ~ShiftInterlaceMain();
 
105
 
 
106
// required for all realtime plugins
 
107
        int process_realtime(VFrame *input_ptr, VFrame *output_ptr);
 
108
        int is_realtime();
 
109
        char* plugin_title();
 
110
        VFrame* new_picon();
 
111
        int show_gui();
 
112
        void raise_window();
 
113
        void update_gui();
 
114
        int set_string();
 
115
        void save_data(KeyFrame *keyframe);
 
116
        void read_data(KeyFrame *keyframe);
 
117
        int load_configuration();
 
118
        int load_defaults();
 
119
        int save_defaults();
 
120
 
 
121
 
 
122
        void shift_row(VFrame *input_frame, 
 
123
                VFrame *output_frame,
 
124
                int offset,
 
125
                int row);
 
126
 
 
127
 
 
128
 
 
129
 
 
130
        ShiftInterlaceConfig config;
 
131
        ShiftInterlaceThread *thread;
 
132
        BC_Hash *defaults;
 
133
};
 
134
 
 
135
 
 
136
 
 
137
 
 
138
PluginClient* new_plugin(PluginServer *server)
 
139
{
 
140
        return new ShiftInterlaceMain(server);
 
141
}
 
142
 
 
143
 
 
144
 
 
145
 
 
146
ShiftInterlaceConfig::ShiftInterlaceConfig()
 
147
{
 
148
        odd_offset = 0;
 
149
        even_offset = 0;
 
150
}
 
151
 
 
152
 
 
153
int ShiftInterlaceConfig::equivalent(ShiftInterlaceConfig &that)
 
154
{
 
155
        return (odd_offset == that.odd_offset &&
 
156
                even_offset == that.even_offset);
 
157
}
 
158
 
 
159
void ShiftInterlaceConfig::copy_from(ShiftInterlaceConfig &that)
 
160
{
 
161
        odd_offset = that.odd_offset;
 
162
        even_offset = that.even_offset;
 
163
}
 
164
 
 
165
void ShiftInterlaceConfig::interpolate(ShiftInterlaceConfig &prev, 
 
166
                ShiftInterlaceConfig &next, 
 
167
                long prev_frame, 
 
168
                long next_frame, 
 
169
                long current_frame)
 
170
{
 
171
        double next_scale = (double)(current_frame - prev_frame) / (next_frame - prev_frame);
 
172
        double prev_scale = (double)(next_frame - current_frame) / (next_frame - prev_frame);
 
173
 
 
174
        this->odd_offset = (int)(prev.odd_offset * prev_scale + next.odd_offset * next_scale);
 
175
        this->even_offset = (int)(prev.even_offset * prev_scale + next.even_offset * next_scale);
 
176
}
 
177
 
 
178
 
 
179
 
 
180
 
 
181
 
 
182
 
 
183
ShiftInterlaceWindow::ShiftInterlaceWindow(ShiftInterlaceMain *plugin, 
 
184
        int x, 
 
185
        int y)
 
186
 : BC_Window(plugin->gui_string, 
 
187
        x,
 
188
        y,
 
189
        310, 
 
190
        100, 
 
191
        310, 
 
192
        100, 
 
193
        0, 
 
194
        0,
 
195
        1)
 
196
{
 
197
        this->plugin = plugin;
 
198
}
 
199
 
 
200
        
 
201
void ShiftInterlaceWindow::create_objects()
 
202
{
 
203
        int x = 10, y = 10;
 
204
        int margin = 30;
 
205
 
 
206
        add_subwindow(new BC_Title(x, y, _("Odd offset:")));
 
207
        add_subwindow(odd_offset = new ShiftInterlaceOdd(plugin, x + 90, y));
 
208
        y += margin;
 
209
        add_subwindow(new BC_Title(x, y, _("Even offset:")));
 
210
        add_subwindow(even_offset = new ShiftInterlaceEven(plugin, x + 90, y));
 
211
 
 
212
        show_window();
 
213
        flush();
 
214
}
 
215
 
 
216
WINDOW_CLOSE_EVENT(ShiftInterlaceWindow)
 
217
 
 
218
ShiftInterlaceOdd::ShiftInterlaceOdd(ShiftInterlaceMain *plugin, int x, int y)
 
219
 : BC_ISlider(x,
 
220
        y,
 
221
        0,
 
222
        200,
 
223
        200,
 
224
        -100,
 
225
        100,
 
226
        plugin->config.odd_offset)
 
227
{
 
228
        this->plugin = plugin;
 
229
}
 
230
int ShiftInterlaceOdd::handle_event()
 
231
{
 
232
        plugin->config.odd_offset = get_value();
 
233
        plugin->send_configure_change();
 
234
        return 1;
 
235
}
 
236
 
 
237
 
 
238
 
 
239
 
 
240
ShiftInterlaceEven::ShiftInterlaceEven(ShiftInterlaceMain *plugin, int x, int y)
 
241
 : BC_ISlider(x,
 
242
        y,
 
243
        0,
 
244
        200,
 
245
        200,
 
246
        -100,
 
247
        100,
 
248
        plugin->config.even_offset)
 
249
{
 
250
        this->plugin = plugin;
 
251
}
 
252
 
 
253
 
 
254
int ShiftInterlaceEven::handle_event()
 
255
{
 
256
        plugin->config.even_offset = get_value();
 
257
        plugin->send_configure_change();
 
258
        return 1;
 
259
}
 
260
 
 
261
 
 
262
 
 
263
 
 
264
 
 
265
 
 
266
 
 
267
 
 
268
PLUGIN_THREAD_OBJECT(ShiftInterlaceMain, ShiftInterlaceThread, ShiftInterlaceWindow)
 
269
 
 
270
 
 
271
 
 
272
ShiftInterlaceMain::ShiftInterlaceMain(PluginServer *server)
 
273
 : PluginVClient(server)
 
274
{
 
275
        PLUGIN_CONSTRUCTOR_MACRO
 
276
}
 
277
 
 
278
ShiftInterlaceMain::~ShiftInterlaceMain()
 
279
{
 
280
        PLUGIN_DESTRUCTOR_MACRO
 
281
}
 
282
 
 
283
 
 
284
char* ShiftInterlaceMain::plugin_title()  { return N_("ShiftInterlace"); }
 
285
int ShiftInterlaceMain::is_realtime() { return 1; }
 
286
 
 
287
 
 
288
SHOW_GUI_MACRO(ShiftInterlaceMain, ShiftInterlaceThread)
 
289
 
 
290
NEW_PICON_MACRO(ShiftInterlaceMain)
 
291
 
 
292
SET_STRING_MACRO(ShiftInterlaceMain)
 
293
 
 
294
LOAD_CONFIGURATION_MACRO(ShiftInterlaceMain, ShiftInterlaceConfig)
 
295
 
 
296
RAISE_WINDOW_MACRO(ShiftInterlaceMain)
 
297
 
 
298
 
 
299
int ShiftInterlaceMain::load_defaults()
 
300
{
 
301
        char directory[1024], string[1024];
 
302
// set the default directory
 
303
        sprintf(directory, "%sshiftinterlace.rc", BCASTDIR);
 
304
 
 
305
// load the defaults
 
306
        defaults = new BC_Hash(directory);
 
307
        defaults->load();
 
308
 
 
309
        config.odd_offset = defaults->get("ODD_OFFSET", config.odd_offset);
 
310
        config.even_offset = defaults->get("EVEN_OFFSET", config.even_offset);
 
311
        return 0;
 
312
}
 
313
 
 
314
int ShiftInterlaceMain::save_defaults()
 
315
{
 
316
        defaults->update("ODD_OFFSET", config.odd_offset);
 
317
        defaults->update("EVEN_OFFSET", config.even_offset);
 
318
        defaults->save();
 
319
        return 0;
 
320
}
 
321
 
 
322
void ShiftInterlaceMain::save_data(KeyFrame *keyframe)
 
323
{
 
324
        FileXML output;
 
325
 
 
326
// cause data to be stored directly in text
 
327
        output.set_shared_string(keyframe->data, MESSAGESIZE);
 
328
        output.tag.set_title("SHIFTINTERLACE");
 
329
        output.tag.set_property("ODD_OFFSET", config.odd_offset);
 
330
        output.tag.set_property("EVEN_OFFSET", config.even_offset);
 
331
        output.append_tag();
 
332
        output.tag.set_title("/SHIFTINTERLACE");
 
333
        output.append_tag();
 
334
        output.append_newline();
 
335
        output.terminate_string();
 
336
// data is now in *text
 
337
}
 
338
 
 
339
void ShiftInterlaceMain::read_data(KeyFrame *keyframe)
 
340
{
 
341
        FileXML input;
 
342
 
 
343
        input.set_shared_string(keyframe->data, strlen(keyframe->data));
 
344
 
 
345
        int result = 0;
 
346
 
 
347
        while(!result)
 
348
        {
 
349
                result = input.read_tag();
 
350
 
 
351
                if(!result)
 
352
                {
 
353
                        if(input.tag.title_is("SHIFTINTERLACE"))
 
354
                        {
 
355
                                config.odd_offset = input.tag.get_property("ODD_OFFSET", config.odd_offset);
 
356
                                config.even_offset = input.tag.get_property("EVEN_OFFSET", config.even_offset);
 
357
                        }
 
358
                }
 
359
        }
 
360
}
 
361
 
 
362
void ShiftInterlaceMain::update_gui()
 
363
{
 
364
        if(thread) 
 
365
        {
 
366
                load_configuration();
 
367
                thread->window->lock_window();
 
368
                thread->window->odd_offset->update(config.odd_offset);
 
369
                thread->window->even_offset->update(config.even_offset);
 
370
                thread->window->unlock_window();
 
371
        }
 
372
}
 
373
 
 
374
 
 
375
#define SHIFT_ROW_MACRO(components, type, chroma_offset) \
 
376
{ \
 
377
        type *input_row = (type*)input_frame->get_rows()[row]; \
 
378
        type *output_row = (type*)output_frame->get_rows()[row]; \
 
379
 \
 
380
        if(offset < 0) \
 
381
        { \
 
382
                int i, j; \
 
383
                for(i = 0, j = -offset;  \
 
384
                        j < w;  \
 
385
                        i++, j++) \
 
386
                { \
 
387
                        output_row[i * components + 0] = input_row[j * components + 0]; \
 
388
                        output_row[i * components + 1] = input_row[j * components + 1]; \
 
389
                        output_row[i * components + 2] = input_row[j * components + 2]; \
 
390
                        if(components == 4) output_row[i * components + 3] = input_row[j * components + 3]; \
 
391
                } \
 
392
 \
 
393
                for( ; i < w; i++) \
 
394
                { \
 
395
                        output_row[i * components + 0] = 0; \
 
396
                        output_row[i * components + 1] = chroma_offset; \
 
397
                        output_row[i * components + 2] = chroma_offset; \
 
398
                        if(components == 4) output_row[i * components + 3] = 0; \
 
399
                } \
 
400
        } \
 
401
        else \
 
402
        { \
 
403
                int i, j; \
 
404
                for(i = w - offset - 1, j = w - 1; \
 
405
                        j >= offset; \
 
406
                        i--, \
 
407
                        j--) \
 
408
                { \
 
409
                        output_row[j * components + 0] = input_row[i * components + 0]; \
 
410
                        output_row[j * components + 1] = input_row[i * components + 1]; \
 
411
                        output_row[j * components + 2] = input_row[i * components + 2]; \
 
412
                        if(components == 4) output_row[j * components + 3] = input_row[i * components + 3]; \
 
413
                } \
 
414
 \
 
415
                for( ; j >= 0; j--) \
 
416
                { \
 
417
                        output_row[j * components + 0] = 0; \
 
418
                        output_row[j * components + 1] = chroma_offset; \
 
419
                        output_row[j * components + 2] = chroma_offset; \
 
420
                        if(components == 4) output_row[j * components + 3] = 0; \
 
421
                } \
 
422
        } \
 
423
}
 
424
 
 
425
 
 
426
void ShiftInterlaceMain::shift_row(VFrame *input_frame, 
 
427
        VFrame *output_frame,
 
428
        int offset,
 
429
        int row)
 
430
{
 
431
        int w = input_frame->get_w();
 
432
        switch(input_frame->get_color_model())
 
433
        {
 
434
                case BC_RGB888:
 
435
                        SHIFT_ROW_MACRO(3, unsigned char, 0x0)
 
436
                        break;
 
437
                case BC_RGB_FLOAT:
 
438
                        SHIFT_ROW_MACRO(3, float, 0x0)
 
439
                        break;
 
440
                case BC_YUV888:
 
441
                        SHIFT_ROW_MACRO(3, unsigned char, 0x80)
 
442
                        break;
 
443
                case BC_RGBA_FLOAT:
 
444
                        SHIFT_ROW_MACRO(4, float, 0x0)
 
445
                        break;
 
446
                case BC_RGBA8888:
 
447
                        SHIFT_ROW_MACRO(4, unsigned char, 0x0)
 
448
                        break;
 
449
                case BC_YUVA8888:
 
450
                        SHIFT_ROW_MACRO(4, unsigned char, 0x80)
 
451
                        break;
 
452
                case BC_RGB161616:
 
453
                        SHIFT_ROW_MACRO(3, uint16_t, 0x0)
 
454
                        break;
 
455
                case BC_YUV161616:
 
456
                        SHIFT_ROW_MACRO(3, uint16_t, 0x8000)
 
457
                        break;
 
458
                case BC_RGBA16161616:
 
459
                        SHIFT_ROW_MACRO(4, uint16_t, 0x0)
 
460
                        break;
 
461
                case BC_YUVA16161616:
 
462
                        SHIFT_ROW_MACRO(4, uint16_t, 0x8000)
 
463
                        break;
 
464
        }
 
465
}
 
466
 
 
467
int ShiftInterlaceMain::process_realtime(VFrame *input_ptr, VFrame *output_ptr)
 
468
{
 
469
        load_configuration();
 
470
 
 
471
        int h = input_ptr->get_h();
 
472
        for(int i = 0; i < h; i++)
 
473
        {
 
474
                if(i % 2)
 
475
                        shift_row(input_ptr, output_ptr, config.even_offset, i);
 
476
                else
 
477
                        shift_row(input_ptr, output_ptr, config.odd_offset, i);
 
478
        }
 
479
 
 
480
        return 0;
 
481
}
 
482
 
 
483