r860: Merge 2.1:
[cinelerra_cv.git] / cinelerra / formattools.C
blob0aef3a6701829f457a49c2ed111cfb55771f74e9
1 #include "asset.h"
2 #include "guicast.h"
3 #include "file.h"
4 #include "formattools.h"
5 #include "language.h"
6 #include "maxchannels.h"
7 #include "mwindow.h"
8 #include "preferences.h"
9 #include "quicktime.h"
10 #include "theme.h"
11 #include "videodevice.inc"
12 #include <string.h>
13 #include "pipe.h"
16 FormatTools::FormatTools(MWindow *mwindow,
17                                 BC_WindowBase *window, 
18                                 Asset *asset)
20         this->mwindow = mwindow;
21         this->window = window;
22         this->asset = asset;
23         this->plugindb = mwindow->plugindb;
25         aparams_thread = 0;
26         vparams_thread = 0;
27         path_textbox = 0;
28         path_button = 0;
29         w = 0;
32 FormatTools::~FormatTools()
34         delete aparams_thread;
35         delete vparams_thread;
38 int FormatTools::create_objects(int &init_x, 
39                                                 int &init_y, 
40                                                 int do_audio,    // Include support for audio
41                                                 int do_video,   // Include support for video
42                                                 int prompt_audio,  // Include checkbox for audio
43                                                 int prompt_video,
44                                                 int prompt_audio_channels,
45                                                 int prompt_video_compression,
46                                                 int lock_compressor,
47                                                 int recording,
48                                                 int *strategy,
49                                                 int brender)
51         int x = init_x;
52         int y = init_y;
54         this->lock_compressor = lock_compressor;
55         this->recording = recording;
56         this->use_brender = brender;
57         this->do_audio = do_audio;
58         this->do_video = do_video;
59         this->prompt_audio = prompt_audio;
60         this->prompt_audio_channels = prompt_audio_channels;
61         this->prompt_video = prompt_video;
62         this->prompt_video_compression = prompt_video_compression;
63         this->strategy = strategy;
65 //printf("FormatTools::create_objects 1\n");
67 // Modify strategy depending on render farm
68         if(strategy)
69         {
70                 if(mwindow->preferences->use_renderfarm)
71                 {
72                         if(*strategy == FILE_PER_LABEL)
73                                 *strategy = FILE_PER_LABEL_FARM;
74                         else
75                         if(*strategy == SINGLE_PASS)
76                                 *strategy = SINGLE_PASS_FARM;
77                 }
78                 else
79                 {
80                         if(*strategy == FILE_PER_LABEL_FARM)
81                                 *strategy = FILE_PER_LABEL;
82                         else
83                         if(*strategy == SINGLE_PASS_FARM)
84                                 *strategy = SINGLE_PASS;
85                 }
86         }
88 //printf("FormatTools::create_objects 1\n");
89         if(!recording)
90         {
91                 window->add_subwindow(path_textbox = new FormatPathText(x, y, this));
92                 x += 305;
93                 path_recent = new BC_RecentList("PATH", mwindow->defaults,
94                                         path_textbox, 10, x, y, 300, 100);
95                 window->add_subwindow(path_recent);
96                 path_recent->load_items(FILE_FORMAT_PREFIX(asset->format));
98                 x += 18;
99                 window->add_subwindow(path_button = new BrowseButton(
100                         mwindow,
101                         window,
102                         path_textbox, 
103                         x, 
104                         y, 
105                         asset->path,
106                         _("Output to file"),
107                         _("Select a file to write to:"),
108                         0));
110 // Set w for user.
111                 w = x + path_button->get_w() + 5;
112                 x -= 305;
114                 y += 25;
116                 pipe_status = new PipeStatus(x, y, "");
117                 window->add_subwindow(pipe_status);
118                 pipe_status->set_status(asset);
120                 y += 35;
121         }
122         else
123                 w = x + 305;
125         window->add_subwindow(format_title = new BC_Title(x, y, _("File Format:")));
126         x += 90;
127         window->add_subwindow(format_text = new BC_TextBox(x, 
128                 y, 
129                 200, 
130                 1, 
131                 File::formattostr(plugindb, asset->format)));
132         x += format_text->get_w();
133         window->add_subwindow(format_button = new FormatFormat(x, 
134                 y, 
135                 this));
136         format_button->create_objects();
138         x = init_x;
139         y += format_button->get_h() + 10;
140         if(do_audio)
141         {
142                 window->add_subwindow(audio_title = new BC_Title(x, y, _("Audio:"), LARGEFONT,  BC_WindowBase::get_resources()->audiovideo_color));
143                 x += 80;
144                 window->add_subwindow(aparams_button = new FormatAParams(mwindow, this, x, y));
145                 x += aparams_button->get_w() + 10;
146                 if(prompt_audio) 
147                 {
148                         window->add_subwindow(audio_switch = new FormatAudio(x, y, this, asset->audio_data));
149                 }
150                 x = init_x;
151                 y += aparams_button->get_h() + 20;
153 // Audio channels only used for recording.
154 //              if(prompt_audio_channels)
155 //              {
156 //                      window->add_subwindow(channels_title = new BC_Title(x, y, _("Number of audio channels to record:")));
157 //                      x += 260;
158 //                      window->add_subwindow(channels_button = new FormatChannels(x, y, this));
159 //                      x += channels_button->get_w() + 5;
160 //                      window->add_subwindow(channels_tumbler = new BC_ITumbler(channels_button, 1, MAXCHANNELS, x, y));
161 //                      y += channels_button->get_h() + 20;
162 //                      x = init_x;
163 //              }
165 //printf("FormatTools::create_objects 6\n");
166                 aparams_thread = new FormatAThread(this);
167         }
169 //printf("FormatTools::create_objects 7\n");
170         if(do_video)
171         {
173 //printf("FormatTools::create_objects 8\n");
174                 window->add_subwindow(video_title = new BC_Title(x, y, _("Video:"), LARGEFONT,  BC_WindowBase::get_resources()->audiovideo_color));
175                 x += 80;
176                 if(prompt_video_compression)
177                 {
178                         window->add_subwindow(vparams_button = new FormatVParams(mwindow, this, x, y));
179                         x += vparams_button->get_w() + 10;
180                 }
182 //printf("FormatTools::create_objects 9\n");
183                 if(prompt_video)
184                 {
185                         window->add_subwindow(video_switch = new FormatVideo(x, y, this, asset->video_data));
186                         y += video_switch->get_h();
187                 }
188                 else
189                 {
190                         y += vparams_button->get_h();
191                 }
193 //printf("FormatTools::create_objects 10\n");
194                 y += 10;
195                 vparams_thread = new FormatVThread(this, lock_compressor);
196         }
198 //printf("FormatTools::create_objects 11\n");
200         x = init_x;
201         if(strategy)
202         {
203                 window->add_subwindow(multiple_files = new FormatMultiple(mwindow, x, y, strategy));
204                 y += multiple_files->get_h() + 10;
205         }
207 //printf("FormatTools::create_objects 12\n");
209         init_y = y;
210         return 0;
213 void FormatTools::update_driver(int driver)
215         this->video_driver = driver;
217         switch(driver)
218         {
219                 case CAPTURE_IEC61883:
220                 case CAPTURE_FIREWIRE:
221                         if(asset->format != FILE_AVI &&
222                                 asset->format != FILE_MOV)
223                         {
224                                 format_text->update(MOV_NAME);
225                                 asset->format = FILE_MOV;
226                         }
227                         else
228                                 format_text->update(File::formattostr(asset->format));
229                         strcpy(asset->vcodec, QUICKTIME_DVSD);
230                         audio_switch->update(asset->audio_data);
231                         video_switch->update(asset->video_data);
232                         break;
234                 case CAPTURE_BUZ:
235                 case VIDEO4LINUX2JPEG:
236                         if(asset->format != FILE_AVI &&
237                                 asset->format != FILE_MOV)
238                         {
239                                 format_text->update(MOV_NAME);
240                                 asset->format = FILE_MOV;
241                         }
242                         else
243                                 format_text->update(File::formattostr(asset->format));
244                         audio_switch->update(asset->audio_data);
245                         video_switch->update(asset->video_data);
246                         break;
248                 default:
249                         format_text->update(File::formattostr(asset->format));
250                         audio_switch->update(asset->audio_data);
251                         video_switch->update(asset->video_data);
252                         break;
253         }
254         close_format_windows();
259 int FormatTools::handle_event()
261         return 0;
264 Asset* FormatTools::get_asset()
266         return asset;
269 void FormatTools::update_extension()
271         char *extension = File::get_tag(asset->format);
272         if(extension)
273         {
274                 char *ptr = strrchr(asset->path, '.');
275                 if(!ptr)
276                 {
277                         ptr = asset->path + strlen(asset->path);
278                         *ptr = '.';
279                 }
280                 ptr++;
281                 sprintf(ptr, extension);
283                 int character1 = ptr - asset->path;
284                 int character2 = ptr - asset->path + strlen(extension);
285                 *(asset->path + character2) = 0;
286                 if(path_textbox) 
287                 {
288                         path_textbox->update(asset->path);
289                         path_textbox->set_selection(character1, character2, character2);
290                 }
291         }
294 void FormatTools::update(Asset *asset, int *strategy)
296         this->asset = asset;
297         this->strategy = strategy;
299         if(path_textbox) 
300                 path_textbox->update(asset->path);
301         format_text->update(File::formattostr(plugindb, asset->format));
302         if(do_audio && audio_switch) audio_switch->update(asset->audio_data);
303         if(do_video && video_switch) video_switch->update(asset->video_data);
304         if(strategy)
305         {
306                 multiple_files->update(strategy);
307         }
308         close_format_windows();
311 void FormatTools::close_format_windows()
313         if(aparams_thread) aparams_thread->file->close_window();
314         if(vparams_thread) vparams_thread->file->close_window();
317 int FormatTools::get_w()
319         return w;
322 void FormatTools::reposition_window(int &init_x, int &init_y)
324         int x = init_x;
325         int y = init_y;
327         if(path_textbox) 
328         {
329                 path_textbox->reposition_window(x, y);
330                 x += 305;
331                 path_button->reposition_window(x, y);
332                 x -= 305;
333                 y += 35;
334         }
336         format_title->reposition_window(x, y);
337         x += 90;
338         format_text->reposition_window(x, y);
339         x += format_text->get_w();
340         format_button->reposition_window(x, y);
342         x = init_x;
343         y += format_button->get_h() + 10;
345         if(do_audio)
346         {
347                 audio_title->reposition_window(x, y);
348                 x += 80;
349                 aparams_button->reposition_window(x, y);
350                 x += aparams_button->get_w() + 10;
351                 if(prompt_audio) audio_switch->reposition_window(x, y);
353                 x = init_x;
354                 y += aparams_button->get_h() + 20;
355                 if(prompt_audio_channels)
356                 {
357                         channels_title->reposition_window(x, y);
358                         x += 260;
359                         channels_button->reposition_window(x, y);
360                         x += channels_button->get_w() + 5;
361                         channels_tumbler->reposition_window(x, y);
362                         y += channels_button->get_h() + 20;
363                         x = init_x;
364                 }
365         }
368         if(do_video)
369         {
370                 video_title->reposition_window(x, y);
371                 x += 80;
372                 if(prompt_video_compression)
373                 {
374                         vparams_button->reposition_window(x, y);
375                         x += vparams_button->get_w() + 10;
376                 }
378                 if(prompt_video)
379                 {
380                         video_switch->reposition_window(x, y);
381                         y += video_switch->get_h();
382                 }
383                 else
384                 {
385                         y += vparams_button->get_h();
386                 }
388                 y += 10;
389                 x = init_x;
390         }
392         if(strategy)
393         {
394                 multiple_files->reposition_window(x, y);
395                 y += multiple_files->get_h() + 10;
396         }
398         init_y = y;
402 int FormatTools::set_audio_options()
404         if(!aparams_thread->running())
405         {
406                 aparams_thread->start();
407         }
408         else
409         {
410                 aparams_thread->file->raise_window();
411         }
412         return 0;
415 int FormatTools::set_video_options()
417         if(!vparams_thread->running())
418         {
419                 vparams_thread->start();
420         }
421         else
422         {
423                 vparams_thread->file->raise_window();
424         }
426         return 0;
433 FormatAParams::FormatAParams(MWindow *mwindow, FormatTools *format, int x, int y)
434  : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
436         this->format = format;
437         set_tooltip(_("Configure audio compression"));
439 FormatAParams::~FormatAParams() 
442 int FormatAParams::handle_event() 
444         format->set_audio_options(); 
447 FormatVParams::FormatVParams(MWindow *mwindow, FormatTools *format, int x, int y)
448  : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
450         this->format = format; 
451         set_tooltip(_("Configure video compression"));
453 FormatVParams::~FormatVParams() 
456 int FormatVParams::handle_event() 
458         format->set_video_options(); 
462 FormatAThread::FormatAThread(FormatTools *format)
463  : Thread()
465         this->format = format; 
466         file = new File;
469 FormatAThread::~FormatAThread() 
471         delete file;
474 void FormatAThread::run()
476         file->get_options(format, 1, 0, 0);
482 FormatVThread::FormatVThread(FormatTools *format, 
483         int lock_compressor)
484  : Thread()
486         this->lock_compressor = lock_compressor;
487         this->format = format;
488         file = new File;
491 FormatVThread::~FormatVThread() 
493         delete file;
496 void FormatVThread::run()
498         file->get_options(format, 0, 1, lock_compressor);
501 FormatPathText::FormatPathText(int x, int y, FormatTools *format)
502  : BC_TextBox(x, y, 300, 1, format->asset->path) 
504         this->format = format; 
506 FormatPathText::~FormatPathText() 
509 int FormatPathText::handle_event() 
511         strcpy(format->asset->path, get_text());
512         format->handle_event();
518 FormatAudio::FormatAudio(int x, int y, FormatTools *format, int default_)
519  : BC_CheckBox(x, 
520         y, 
521         default_, 
522         (char*)(format->recording ? _("Record audio tracks") : _("Render audio tracks")))
524         this->format = format; 
526 FormatAudio::~FormatAudio() {}
527 int FormatAudio::handle_event()
529         format->asset->audio_data = get_value();
533 FormatVideo::FormatVideo(int x, int y, FormatTools *format, int default_)
534  : BC_CheckBox(x, 
535         y, 
536         default_, 
537         (char*)(format->recording ? _("Record video tracks") : _("Render video tracks")))
539 this->format = format; 
541 FormatVideo::~FormatVideo() {}
542 int FormatVideo::handle_event()
544         format->asset->video_data = get_value();
550 FormatFormat::FormatFormat(int x, 
551         int y, 
552         FormatTools *format)
553  : FormatPopup(format->plugindb, 
554         x, 
555         y,
556         format->use_brender)
558         this->format = format; 
560 FormatFormat::~FormatFormat() 
563 int FormatFormat::handle_event()
565         if(get_selection(0, 0) >= 0)
566         {
567                 int new_format = File::strtoformat(format->plugindb, get_selection(0, 0)->get_text());
568                 if(new_format != format->asset->format)
569                 {
570                         // save the state of the old format
571                         format->asset->save_format_defaults
572                                 (format->mwindow->defaults);
574                         // close the configure window if open
575                         format->close_format_windows();
577                         // change to the new format
578                         format->asset->format = new_format;
580                         // load the state for the new format
581                         format->asset->load_format_defaults
582                                 (format->mwindow->defaults);
584                         // update the render window to match
585                         format->format_text->
586                                 update(get_selection(0, 0)->get_text());
587                         format->update_extension();
588                         format->path_textbox->update(format->asset->path);
589                         format->pipe_status->set_status(format->asset);
590                         format->path_recent->load_items
591                                 (FILE_FORMAT_PREFIX(format->asset->format));
592                 }
593         }
594         return 1;
599 FormatChannels::FormatChannels(int x, int y, FormatTools *format)
600  : BC_TextBox(x, y, 100, 1, format->asset->channels) 
602         this->format = format; 
604 FormatChannels::~FormatChannels() 
607 int FormatChannels::handle_event() 
609         format->asset->channels = atol(get_text());
610         return 1;
613 FormatToTracks::FormatToTracks(int x, int y, int *output)
614  : BC_CheckBox(x, y, *output, _("Overwrite project with output"))
616         this->output = output; 
618 FormatToTracks::~FormatToTracks() 
621 int FormatToTracks::handle_event()
623         *output = get_value();
624         return 1;
628 FormatMultiple::FormatMultiple(MWindow *mwindow, int x, int y, int *output)
629  : BC_CheckBox(x, 
630         y, 
631         (*output == FILE_PER_LABEL) || (*output == FILE_PER_LABEL_FARM), 
632         _("Create new file at each label"))
634         this->output = output;
635         this->mwindow = mwindow;
637 FormatMultiple::~FormatMultiple() 
640 int FormatMultiple::handle_event()
642         if(get_value())
643         {
644                 if(mwindow->preferences->use_renderfarm)
645                         *output = FILE_PER_LABEL_FARM;
646                 else
647                         *output = FILE_PER_LABEL;
648         }
649         else
650         {
651                 if(mwindow->preferences->use_renderfarm)
652                         *output = SINGLE_PASS_FARM;
653                 else
654                         *output = SINGLE_PASS;
655         }
656         return 1;
659 void FormatMultiple::update(int *output)
661         this->output = output;
662         if(*output == FILE_PER_LABEL_FARM ||
663                 *output ==FILE_PER_LABEL)
664                 set_value(1);
665         else
666                 set_value(0);