r870: Merge 2.1:
[cinelerra_cv.git] / cinelerra / formattools.C
blob848d5e4a505a5640b3782367618a7fe50d7c94dd
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                                                 char *locked_compressor,
47                                                 int recording,
48                                                 int *strategy,
49                                                 int brender)
51         int x = init_x;
52         int y = init_y;
54         this->locked_compressor = locked_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(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);
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_DVB:
220 // Just give the user information about how the stream is going to be
221 // stored but don't change the asset.
222 // Want to be able to revert to user settings.
223                         if(asset->format != FILE_MPEG)
224                         {
225                                 format_text->update(_("MPEG transport stream"));
226                                 asset->format = FILE_MPEG;
227                         }
228                         locked_compressor = 0;
229                         audio_switch->update(1);
230                         video_switch->update(1);
231                         break;
233                 case CAPTURE_IEC61883:
234                 case CAPTURE_FIREWIRE:
235                         if(asset->format != FILE_AVI &&
236                                 asset->format != FILE_MOV)
237                         {
238                                 format_text->update(MOV_NAME);
239                                 asset->format = FILE_MOV;
240                         }
241                         else
242                                 format_text->update(File::formattostr(asset->format));
243                         locked_compressor = QUICKTIME_DVSD;
244                         strcpy(asset->vcodec, QUICKTIME_DVSD);
245                         audio_switch->update(asset->audio_data);
246                         video_switch->update(asset->video_data);
247                         break;
249                 case CAPTURE_BUZ:
250                 case VIDEO4LINUX2JPEG:
251                         if(asset->format != FILE_AVI &&
252                                 asset->format != FILE_MOV)
253                         {
254                                 format_text->update(MOV_NAME);
255                                 asset->format = FILE_MOV;
256                         }
257                         else
258                                 format_text->update(File::formattostr(asset->format));
259                         locked_compressor = QUICKTIME_MJPA;
260                         audio_switch->update(asset->audio_data);
261                         video_switch->update(asset->video_data);
262                         break;
264                 default:
265                         format_text->update(File::formattostr(asset->format));
266                         locked_compressor = 0;
267                         audio_switch->update(asset->audio_data);
268                         video_switch->update(asset->video_data);
269                         break;
270         }
271         close_format_windows();
276 int FormatTools::handle_event()
278         return 0;
281 Asset* FormatTools::get_asset()
283         return asset;
286 void FormatTools::update_extension()
288         char *extension = File::get_tag(asset->format);
289         if(extension)
290         {
291                 char *ptr = strrchr(asset->path, '.');
292                 if(!ptr)
293                 {
294                         ptr = asset->path + strlen(asset->path);
295                         *ptr = '.';
296                 }
297                 ptr++;
298                 sprintf(ptr, extension);
300                 int character1 = ptr - asset->path;
301                 int character2 = ptr - asset->path + strlen(extension);
302                 *(asset->path + character2) = 0;
303                 if(path_textbox) 
304                 {
305                         path_textbox->update(asset->path);
306                         path_textbox->set_selection(character1, character2, character2);
307                 }
308         }
311 void FormatTools::update(Asset *asset, int *strategy)
313         this->asset = asset;
314         this->strategy = strategy;
316         if(path_textbox) 
317                 path_textbox->update(asset->path);
318         format_text->update(File::formattostr(plugindb, asset->format));
319         if(do_audio && audio_switch) audio_switch->update(asset->audio_data);
320         if(do_video && video_switch) video_switch->update(asset->video_data);
321         if(strategy)
322         {
323                 multiple_files->update(strategy);
324         }
325         close_format_windows();
328 void FormatTools::close_format_windows()
330         if(aparams_thread) aparams_thread->file->close_window();
331         if(vparams_thread) vparams_thread->file->close_window();
334 int FormatTools::get_w()
336         return w;
339 void FormatTools::reposition_window(int &init_x, int &init_y)
341         int x = init_x;
342         int y = init_y;
344         if(path_textbox) 
345         {
346                 path_textbox->reposition_window(x, y);
347                 x += 305;
348                 path_button->reposition_window(x, y);
349                 x -= 305;
350                 y += 35;
351         }
353         format_title->reposition_window(x, y);
354         x += 90;
355         format_text->reposition_window(x, y);
356         x += format_text->get_w();
357         format_button->reposition_window(x, y);
359         x = init_x;
360         y += format_button->get_h() + 10;
362         if(do_audio)
363         {
364                 audio_title->reposition_window(x, y);
365                 x += 80;
366                 aparams_button->reposition_window(x, y);
367                 x += aparams_button->get_w() + 10;
368                 if(prompt_audio) audio_switch->reposition_window(x, y);
370                 x = init_x;
371                 y += aparams_button->get_h() + 20;
372                 if(prompt_audio_channels)
373                 {
374                         channels_title->reposition_window(x, y);
375                         x += 260;
376                         channels_button->reposition_window(x, y);
377                         x += channels_button->get_w() + 5;
378                         channels_tumbler->reposition_window(x, y);
379                         y += channels_button->get_h() + 20;
380                         x = init_x;
381                 }
382         }
385         if(do_video)
386         {
387                 video_title->reposition_window(x, y);
388                 x += 80;
389                 if(prompt_video_compression)
390                 {
391                         vparams_button->reposition_window(x, y);
392                         x += vparams_button->get_w() + 10;
393                 }
395                 if(prompt_video)
396                 {
397                         video_switch->reposition_window(x, y);
398                         y += video_switch->get_h();
399                 }
400                 else
401                 {
402                         y += vparams_button->get_h();
403                 }
405                 y += 10;
406                 x = init_x;
407         }
409         if(strategy)
410         {
411                 multiple_files->reposition_window(x, y);
412                 y += multiple_files->get_h() + 10;
413         }
415         init_y = y;
419 int FormatTools::set_audio_options()
421 //      if(video_driver == CAPTURE_DVB)
422 //      {
423 //              return 0;
424 //      }
426         if(!aparams_thread->running())
427         {
428                 aparams_thread->start();
429         }
430         else
431         {
432                 aparams_thread->file->raise_window();
433         }
434         return 0;
437 int FormatTools::set_video_options()
439 //      if(video_driver == CAPTURE_DVB)
440 //      {
441 //              return 0;
442 //      }
444         if(!vparams_thread->running())
445         {
446                 vparams_thread->start();
447         }
448         else
449         {
450                 vparams_thread->file->raise_window();
451         }
453         return 0;
460 FormatAParams::FormatAParams(MWindow *mwindow, FormatTools *format, int x, int y)
461  : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
463         this->format = format;
464         set_tooltip(_("Configure audio compression"));
466 FormatAParams::~FormatAParams() 
469 int FormatAParams::handle_event() 
471         format->set_audio_options(); 
474 FormatVParams::FormatVParams(MWindow *mwindow, FormatTools *format, int x, int y)
475  : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
477         this->format = format; 
478         set_tooltip(_("Configure video compression"));
480 FormatVParams::~FormatVParams() 
483 int FormatVParams::handle_event() 
485         format->set_video_options(); 
489 FormatAThread::FormatAThread(FormatTools *format)
490  : Thread()
492         this->format = format; 
493         file = new File;
496 FormatAThread::~FormatAThread() 
498         delete file;
501 void FormatAThread::run()
503         file->get_options(format, 1, 0);
509 FormatVThread::FormatVThread(FormatTools *format)
510  : Thread()
512         this->format = format;
513         file = new File;
516 FormatVThread::~FormatVThread() 
518         delete file;
521 void FormatVThread::run()
523         file->get_options(format, 0, 1);
526 FormatPathText::FormatPathText(int x, int y, FormatTools *format)
527  : BC_TextBox(x, y, 300, 1, format->asset->path) 
529         this->format = format; 
531 FormatPathText::~FormatPathText() 
534 int FormatPathText::handle_event() 
536         strcpy(format->asset->path, get_text());
537         format->handle_event();
543 FormatAudio::FormatAudio(int x, int y, FormatTools *format, int default_)
544  : BC_CheckBox(x, 
545         y, 
546         default_, 
547         (char*)(format->recording ? _("Record audio tracks") : _("Render audio tracks")))
549         this->format = format; 
551 FormatAudio::~FormatAudio() {}
552 int FormatAudio::handle_event()
554         format->asset->audio_data = get_value();
558 FormatVideo::FormatVideo(int x, int y, FormatTools *format, int default_)
559  : BC_CheckBox(x, 
560         y, 
561         default_, 
562         (char*)(format->recording ? _("Record video tracks") : _("Render video tracks")))
564 this->format = format; 
566 FormatVideo::~FormatVideo() {}
567 int FormatVideo::handle_event()
569         format->asset->video_data = get_value();
575 FormatFormat::FormatFormat(int x, 
576         int y, 
577         FormatTools *format)
578  : FormatPopup(format->plugindb, 
579         x, 
580         y,
581         format->use_brender)
583         this->format = format; 
585 FormatFormat::~FormatFormat() 
588 int FormatFormat::handle_event()
590         if(get_selection(0, 0) >= 0)
591         {
592                 int new_format = File::strtoformat(format->plugindb, get_selection(0, 0)->get_text());
593                 if(new_format != format->asset->format)
594                 {
595                         // save the state of the old format
596                         format->asset->save_format_defaults
597                                 (format->mwindow->defaults);
599                         // close the configure window if open
600                         format->close_format_windows();
602                         // change to the new format
603                         format->asset->format = new_format;
605                         // load the state for the new format
606                         format->asset->load_format_defaults
607                                 (format->mwindow->defaults);
609                         // update the render window to match
610                         format->format_text->update(get_selection(0, 0)->get_text());
611                         format->update_extension();
612                         format->path_textbox->update(format->asset->path);
613                         format->pipe_status->set_status(format->asset);
614                         format->path_recent->load_items
615                                 (FILE_FORMAT_PREFIX(format->asset->format));
616                 }
617         }
618         return 1;
623 FormatChannels::FormatChannels(int x, int y, FormatTools *format)
624  : BC_TextBox(x, y, 100, 1, format->asset->channels) 
626         this->format = format; 
628 FormatChannels::~FormatChannels() 
631 int FormatChannels::handle_event() 
633         format->asset->channels = atol(get_text());
634         return 1;
637 FormatToTracks::FormatToTracks(int x, int y, int *output)
638  : BC_CheckBox(x, y, *output, _("Overwrite project with output"))
640         this->output = output; 
642 FormatToTracks::~FormatToTracks() 
645 int FormatToTracks::handle_event()
647         *output = get_value();
648         return 1;
652 FormatMultiple::FormatMultiple(MWindow *mwindow, int x, int y, int *output)
653  : BC_CheckBox(x, 
654         y, 
655         (*output == FILE_PER_LABEL) || (*output == FILE_PER_LABEL_FARM), 
656         _("Create new file at each label"))
658         this->output = output;
659         this->mwindow = mwindow;
661 FormatMultiple::~FormatMultiple() 
664 int FormatMultiple::handle_event()
666         if(get_value())
667         {
668                 if(mwindow->preferences->use_renderfarm)
669                         *output = FILE_PER_LABEL_FARM;
670                 else
671                         *output = FILE_PER_LABEL;
672         }
673         else
674         {
675                 if(mwindow->preferences->use_renderfarm)
676                         *output = SINGLE_PASS_FARM;
677                 else
678                         *output = SINGLE_PASS;
679         }
680         return 1;
683 void FormatMultiple::update(int *output)
685         this->output = output;
686         if(*output == FILE_PER_LABEL_FARM ||
687                 *output ==FILE_PER_LABEL)
688                 set_value(1);
689         else
690                 set_value(0);