render effect segv, drag chkbox track coords, check mask active,
[goodguy/cinelerra.git] / cinelerra-5.1 / plugins / livevideo / livevideo.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 "asset.h"
23 #include "bcdisplayinfo.h"
24 #include "bcsignals.h"
25 #include "channel.h"
26 #include "channeldb.h"
27 #include "clip.h"
28 #include "bchash.h"
29 #include "edl.h"
30 #include "edlsession.h"
31 #include "filexml.h"
32 #include "guicast.h"
33 #include "language.h"
34 #include "libdv.h"
35 #include "libmjpeg.h"
36 #include "mwindow.h"
37 #include "picture.h"
38 #include "pluginvclient.h"
39 #include "pluginserver.h"
40 #include "recordconfig.h"
41 #include "transportque.inc"
42 #include "vframe.h"
43 #include "videodevice.h"
44 #include "videodevice.inc"
45
46 #include <string.h>
47 #include <stdint.h>
48
49 #define HISTORY_FRAMES 30
50 class LiveVideo;
51 class LiveVideoWindow;
52
53
54 class LiveVideoConfig
55 {
56 public:
57         LiveVideoConfig();
58         void copy_from(LiveVideoConfig &src);
59         int equivalent(LiveVideoConfig &src);
60         void interpolate(LiveVideoConfig &prev,
61                 LiveVideoConfig &next,
62                 int64_t prev_frame,
63                 int64_t next_frame,
64                 int64_t current_frame);
65         int channel;
66 };
67
68
69 // Without access to the video device, the ChannelPicker can't
70 // do any of the things it was designed to.  Instead, just provide
71 // a list of channels.
72 class LiveChannelList : public BC_ListBox
73 {
74 public:
75         LiveChannelList(LiveVideo *plugin,
76                 LiveVideoWindow *gui,
77                 int x,
78                 int y,
79                 int w,
80                 int h);
81         int handle_event();
82         LiveVideo *plugin;
83         LiveVideoWindow *gui;
84 };
85
86 class LiveChannelSelect : public BC_Button
87 {
88 public:
89         LiveChannelSelect(LiveVideo *plugin,
90                 LiveVideoWindow *gui,
91                 int x,
92                 int y);
93         int handle_event();
94         LiveVideo *plugin;
95         LiveVideoWindow *gui;
96 };
97
98
99 class LiveVideoWindow : public PluginClientWindow
100 {
101 public:
102         LiveVideoWindow(LiveVideo *plugin);
103         ~LiveVideoWindow();
104
105         void create_objects();
106
107         int resize_event(int w, int h);
108
109         ArrayList<BC_ListBoxItem*> channel_list;
110         BC_Title *title;
111         LiveChannelList *list;
112         LiveChannelSelect *select;
113         LiveVideo *plugin;
114 };
115
116
117
118
119
120
121 class LiveVideo : public PluginVClient
122 {
123 public:
124         LiveVideo(PluginServer *server);
125         ~LiveVideo();
126
127
128         PLUGIN_CLASS_MEMBERS(LiveVideoConfig);
129
130         int process_buffer(VFrame *frame,
131                 int64_t start_position,
132                 double frame_rate);
133         int is_realtime();
134         int is_multichannel();
135         int is_synthesis();
136         void save_data(KeyFrame *keyframe);
137         void read_data(KeyFrame *keyframe);
138         void update_gui();
139         void render_stop();
140
141         ChannelDB *channeldb;
142         VideoDevice *vdevice;
143 // Colormodel the device generates
144         int input_cmodel;
145 // Temporary for colormodel conversion
146         VFrame *temp;
147 // What configuration parameters the device supports
148         Channel master_channel;
149         PictureConfig *picture;
150         int prev_channel;
151         int w, h;
152 // Decompressors for different video drivers
153         dv_t *dv;
154         mjpeg_t *mjpeg;
155 };
156
157
158
159
160
161
162
163
164
165
166
167
168 LiveVideoConfig::LiveVideoConfig()
169 {
170         channel = 0;
171 }
172
173 void LiveVideoConfig::copy_from(LiveVideoConfig &src)
174 {
175         this->channel = src.channel;
176 }
177
178 int LiveVideoConfig::equivalent(LiveVideoConfig &src)
179 {
180         return (this->channel == src.channel);
181 }
182
183 void LiveVideoConfig::interpolate(LiveVideoConfig &prev,
184         LiveVideoConfig &next,
185         int64_t prev_frame,
186         int64_t next_frame,
187         int64_t current_frame)
188 {
189         this->channel = prev.channel;
190 }
191
192
193
194
195
196 LiveVideoWindow::LiveVideoWindow(LiveVideo *plugin)
197  : PluginClientWindow(plugin,
198         plugin->w,
199         plugin->h,
200         100,
201         100,
202         1)
203 {
204         this->plugin = plugin;
205 }
206
207 LiveVideoWindow::~LiveVideoWindow()
208 {
209         channel_list.remove_all_objects();
210 }
211
212 void LiveVideoWindow::create_objects()
213 {
214         int x = 10, y = 10;
215
216         EDLSession *session = plugin->get_edl()->session;
217         if(session)
218                 VideoDevice::load_channeldb(plugin->channeldb, session->vconfig_in);
219         for(int i = 0; i < plugin->channeldb->size(); i++)
220         {
221                 BC_ListBoxItem *current;
222                 channel_list.append(current =
223                         new BC_ListBoxItem(plugin->channeldb->get(i)->title));
224                 if(i == plugin->config.channel) current->set_selected(1);
225         }
226
227         add_subwindow(title = new BC_Title(x, y, _("Channels:")));
228         y += title->get_h() + 5;
229         add_subwindow(list = new LiveChannelList(plugin,
230                 this,
231                 x,
232                 y,
233                 get_w() - x - 10,
234                 get_h() - y - BC_OKButton::calculate_h() - 10 - 10));
235         y += list->get_h() + 10;
236         add_subwindow(select = new LiveChannelSelect(plugin,
237                 this,
238                 x,
239                 y));
240         show_window();
241 }
242
243
244
245 int LiveVideoWindow::resize_event(int w, int h)
246 {
247         int list_bottom = get_h() - list->get_y() - list->get_h();
248         int list_side = get_w() - list->get_x() - list->get_w();
249         int select_top = get_h() - select->get_y();
250
251         title->reposition_window(title->get_x(), title->get_y());
252
253         list->reposition_window(list->get_x(),
254                 list->get_y(),
255                 w - list->get_x() - list_side,
256                 h - list->get_y() - list_bottom);
257         select->reposition_window(select->get_x(),
258                 h - select_top);
259         plugin->w = w;
260         plugin->h = h;
261         return 1;
262 }
263
264
265
266
267 LiveChannelList::LiveChannelList(LiveVideo *plugin,
268         LiveVideoWindow *gui,
269         int x,
270         int y,
271         int w,
272         int h)
273  : BC_ListBox(x,
274         y,
275         w,
276         h,
277         LISTBOX_TEXT,                   // Display text list or icons
278         &gui->channel_list) // Each column has an ArrayList of BC_ListBoxItems.
279 {
280         this->plugin = plugin;
281         this->gui = gui;
282 }
283
284 int LiveChannelList::handle_event()
285 {
286         plugin->config.channel = get_selection_number(0, 0);
287         plugin->send_configure_change();
288         return 1;
289 }
290
291
292 LiveChannelSelect::LiveChannelSelect(LiveVideo *plugin,
293         LiveVideoWindow *gui,
294         int x,
295         int y)
296  :  BC_Button(x, y,
297         BC_WindowBase::get_resources()->ok_images)
298 {
299         this->plugin = plugin;
300         this->gui = gui;
301 }
302
303 int LiveChannelSelect::handle_event()
304 {
305         plugin->config.channel = gui->list->get_selection_number(0, 0);
306         plugin->send_configure_change();
307         return 1;
308 }
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 REGISTER_PLUGIN(LiveVideo)
336
337
338
339
340
341
342 LiveVideo::LiveVideo(PluginServer *server)
343  : PluginVClient(server)
344 {
345         vdevice = 0;
346         temp = 0;
347         channeldb = new ChannelDB;
348         w = 320;
349         h = 640;
350         prev_channel = 0;
351         dv = 0;
352         mjpeg = 0;
353         picture = 0;
354         this->server = server;
355
356 }
357
358
359 LiveVideo::~LiveVideo()
360 {
361
362         if(vdevice)
363         {
364                 vdevice->interrupt_crash();
365                 vdevice->close_all();
366                 delete vdevice;
367         }
368
369         delete channeldb;
370         delete temp;
371         if(dv) dv_delete(dv);
372         if(mjpeg) mjpeg_delete(mjpeg);
373         delete picture;
374 }
375
376
377
378 int LiveVideo::process_buffer(VFrame *frame,
379         int64_t start_position,
380         double frame_rate)
381 {
382         load_configuration();
383 //printf("LiveVideo::process_buffer 10 start_position=%lld buffer_size=%d size=%d\n",
384 //start_position, get_buffer_size(), size);
385
386         EDLSession *session = get_edl()->session;
387         if(!vdevice)
388         {
389                 if(session)
390                 {
391                         vdevice = new VideoDevice(server ? server->mwindow : 0);
392                         vdevice->open_input(session->vconfig_in,
393                                 0,
394                                 0,
395                                 1.0,
396                                 frame_rate);
397
398 // The color model depends on the asset configured by the user for recording.
399 // Unfortunately, get_best_colormodel returns the best colormodel for displaying
400 // on the record monitor, not the colormodel supported by the device.
401 // Some devices can read directly to the best colormodel and some can't.
402                         switch(session->vconfig_in->driver)
403                         {
404                                 case CAPTURE_FIREWIRE:
405                                 case CAPTURE_IEC61883:
406                                 case VIDEO4LINUX2JPEG:
407                                 case CAPTURE_JPEG_WEBCAM:
408                                         input_cmodel = BC_COMPRESSED;
409                                         break;
410                                 default:
411                                         input_cmodel = vdevice->get_best_colormodel(session->recording_format);
412                                         break;
413                         }
414
415
416 // Load the picture config from the main defaults file.
417
418 // Load channel table
419                         VideoDevice::load_channeldb(channeldb, session->vconfig_in);
420
421                         if(!picture)
422                         {
423                                 picture = new PictureConfig;
424                                 picture->load_defaults();
425                         }
426
427 // Picture must have usage from driver before it can load defaults.
428                         master_channel.copy_usage(vdevice->channel);
429                         picture->copy_usage(vdevice->picture);
430                         picture->load_defaults();
431
432 // Need to load picture defaults but this requires MWindow.
433                         vdevice->set_picture(picture);
434                         vdevice->set_channel(channeldb->get(config.channel));
435                 }
436                 prev_channel = config.channel;
437         }
438
439         if(session && vdevice)
440         {
441 // Update channel
442                 if(prev_channel != config.channel)
443                 {
444                         prev_channel = config.channel;
445                         vdevice->set_picture(picture);
446                         vdevice->set_channel(channeldb->get(config.channel));
447                 }
448
449
450                 (void)vdevice->config_updated();
451                 VFrame *input = frame;
452                 if(input_cmodel != frame->get_color_model() ||
453                         session->vconfig_in->w != frame->get_w() ||
454                         session->vconfig_in->h != frame->get_h())
455                 {
456                         if(!temp)
457                         {
458                                 temp = new VFrame(session->vconfig_in->w, session->vconfig_in->h,
459                                                 input_cmodel, 0);
460                         }
461                         input = temp;
462                 }
463
464                 vdevice->read_buffer(input);
465
466                 if(input != frame)
467                 {
468                         if(input->get_color_model() != BC_COMPRESSED)
469                         {
470                                 int w = MIN(session->vconfig_in->w, frame->get_w());
471                                 int h = MIN(session->vconfig_in->h, frame->get_h());
472                                 BC_CModels::transfer(frame->get_rows(), /* Leave NULL if non existent */
473                                         input->get_rows(),
474                                         frame->get_y(), /* Leave NULL if non existent */
475                                         frame->get_u(),
476                                         frame->get_v(),
477                                         input->get_y(), /* Leave NULL if non existent */
478                                         input->get_u(),
479                                         input->get_v(),
480                                         0,        /* Dimensions to capture from input frame */
481                                         0,
482                                         w,
483                                         h,
484                                         0,       /* Dimensions to project on output frame */
485                                         0,
486                                         w,
487                                         h,
488                                         input->get_color_model(),
489                                         frame->get_color_model(),
490                                         0,         /* When transfering BC_RGBA8888 to non-alpha this is the background color in 0xRRGGBB hex */
491                                         input->get_bytes_per_line(),       /* For planar use the luma rowspan */
492                                         frame->get_bytes_per_line());     /* For planar use the luma rowspan */
493                                 frame->set_opengl_state(VFrame::RAM);
494                         }
495                         else
496                         if(input->get_compressed_size())
497                         {
498                                 switch(session->vconfig_in->driver)
499                                 {
500                                         case CAPTURE_FIREWIRE:
501                                         case CAPTURE_IEC61883:
502 // Decompress a DV frame from the driver
503                                                 if(!dv)
504                                                         dv = dv_new();
505                                                 dv_read_video(((dv_t*)dv),
506                                                         frame->get_rows(),
507                                                         input->get_data(),
508                                                         input->get_compressed_size(),
509                                                         frame->get_color_model());
510                                                 frame->set_opengl_state(VFrame::RAM);
511                                                 break;
512
513                                         case VIDEO4LINUX2JPEG:
514                                                 if(!mjpeg)
515                                                         mjpeg = mjpeg_new(frame->get_w(),
516                                                                 frame->get_h(),
517                                                                 2);  // fields
518                                                 mjpeg_decompress(mjpeg,
519                                                         input->get_data(),
520                                                         input->get_compressed_size(),
521                                                         input->get_field2_offset(),
522                                                         frame->get_rows(),
523                                                         frame->get_y(),
524                                                         frame->get_u(),
525                                                         frame->get_v(),
526                                                         frame->get_color_model(),
527                                                         get_project_smp() + 1);
528                                                 break;
529
530                                         case CAPTURE_JPEG_WEBCAM:
531                                                 if(!mjpeg)
532                                                         mjpeg = mjpeg_new(frame->get_w(),
533                                                                 frame->get_h(),
534                                                                 1);  // fields
535 // printf("LiveVideo::process_buffer %d %p %d\n",
536 // __LINE__,
537 // input->get_data(),
538 // input->get_compressed_size());
539                                                 mjpeg_decompress(mjpeg,
540                                                         input->get_data(),
541                                                         input->get_compressed_size(),
542                                                         0,
543                                                         frame->get_rows(),
544                                                         frame->get_y(),
545                                                         frame->get_u(),
546                                                         frame->get_v(),
547                                                         frame->get_color_model(),
548                                                         get_project_smp() + 1);
549                                                 break;
550                                 }
551                         }
552                         else
553                         {
554                                 printf("LiveVideo::process_buffer %d zero size image\n", __LINE__);
555                         }
556                 }
557         }
558
559         return 0;
560 }
561
562 void LiveVideo::render_stop()
563 {
564         if(vdevice)
565         {
566                 vdevice->interrupt_crash();
567                 vdevice->close_all();
568                 delete vdevice;
569                 vdevice = 0;
570         }
571         delete picture;
572         picture = 0;
573 }
574
575
576 const char* LiveVideo::plugin_title() { return N_("Live Video"); }
577 int LiveVideo::is_realtime() { return 1; }
578 int LiveVideo::is_multichannel() { return 0; }
579 int LiveVideo::is_synthesis() { return 1; }
580
581
582
583 NEW_WINDOW_MACRO(LiveVideo, LiveVideoWindow)
584
585 LOAD_CONFIGURATION_MACRO(LiveVideo, LiveVideoConfig)
586
587
588
589 void LiveVideo::save_data(KeyFrame *keyframe)
590 {
591         FileXML output;
592         output.set_shared_output(keyframe->xbuf);
593         output.tag.set_title("LIVEVIDEO");
594         output.tag.set_property("CHANNEL", config.channel);
595         output.append_tag();
596         output.tag.set_title("/LIVEVIDEO");
597         output.append_tag();
598         output.append_newline();
599         output.terminate_string();
600 }
601
602 void LiveVideo::read_data(KeyFrame *keyframe)
603 {
604         FileXML input;
605
606         input.set_shared_input(keyframe->xbuf);
607
608         int result = 0;
609
610         while(!result)
611         {
612                 result = input.read_tag();
613
614                 if(!result)
615                 {
616                         if(input.tag.title_is("LIVEVIDEO"))
617                         {
618                                 config.channel = input.tag.get_property("CHANNEL", config.channel);
619                         }
620                 }
621         }
622 }
623
624 void LiveVideo::update_gui()
625 {
626         if(thread)
627         {
628                 if(load_configuration())
629                 {
630                         thread->window->lock_window("LiveVideo::update_gui");
631                         ((LiveVideoWindow*)thread->window)->list->set_selected(
632                                 &((LiveVideoWindow*)thread->window)->channel_list,
633                                 config.channel,
634                                 1);
635                         ((LiveVideoWindow*)thread->window)->list->draw_items(1);
636                         thread->window->unlock_window();
637                 }
638         }
639 }
640
641
642
643
644