4 * Copyright (C) 2010-2013 Adam Williams <broadcast at earthling dot net>
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.
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.
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
23 #include "bcsignals.h"
27 #include "filesystem.h"
28 #include "formattools.h"
32 #include "maxchannels.h"
34 #include "preferences.h"
36 #include "videodevice.inc"
42 FormatTools::FormatTools(MWindow *mwindow,
43 BC_WindowBase *window,
46 this->mwindow = mwindow;
47 this->window = window;
49 this->plugindb = mwindow->plugindb;
78 prompt_audio_channels = 0;
80 prompt_video_compression = 0;
82 locked_compressor = 0;
86 FormatTools::~FormatTools()
96 if(aparams_button) delete aparams_button;
98 if(vparams_button) delete vparams_button;
100 if(aparams_thread) delete aparams_thread;
102 if(vparams_thread) delete vparams_thread;
104 if(channels_tumbler) delete channels_tumbler;
108 file_entries->remove_all_objects();
113 void FormatTools::create_objects(int &init_x,
115 int do_audio, // Include support for audio
116 int do_video, // Include support for video
117 int prompt_audio, // Include checkbox for audio
119 int prompt_audio_channels,
120 int prompt_video_compression,
121 char *locked_compressor,
129 this->locked_compressor = locked_compressor;
130 this->recording = recording;
131 this->use_brender = brender;
132 this->do_audio = do_audio;
133 this->do_video = do_video;
134 this->prompt_audio = prompt_audio;
135 this->prompt_audio_channels = prompt_audio_channels;
136 this->prompt_video = prompt_video;
137 this->prompt_video_compression = prompt_video_compression;
138 this->strategy = strategy;
141 file_entries = new ArrayList<BC_ListBoxItem*>;
143 char string[BCTEXTLEN];
144 // Load current directory
145 fs.update(getcwd(string, BCTEXTLEN));
146 for(int i = 0; i < fs.total_files(); i++)
148 file_entries->append(
150 fs.get_entry(i)->get_name()));
153 //printf("FormatTools::create_objects 1\n");
155 // Modify strategy depending on render farm
158 if(mwindow->preferences->use_renderfarm)
160 if(*strategy == FILE_PER_LABEL)
161 *strategy = FILE_PER_LABEL_FARM;
163 if(*strategy == SINGLE_PASS)
164 *strategy = SINGLE_PASS_FARM;
168 if(*strategy == FILE_PER_LABEL_FARM)
169 *strategy = FILE_PER_LABEL;
171 if(*strategy == SINGLE_PASS_FARM)
172 *strategy = SINGLE_PASS;
178 window->add_subwindow(path_textbox = new FormatPathText(x, y, this));
179 x += path_textbox->get_w() + 5;
180 window->add_subwindow(path_button = new BrowseButton(
188 _("Select a file to write to:"),
193 // w = x + path_button->get_w() + 5;
194 x -= path_textbox->get_w() + 5;
203 window->add_subwindow(format_title = new BC_Title(x, y, _("File Format:")));
205 window->add_subwindow(format_text = new BC_TextBox(x, y, 180, 1,
206 File::formattostr(asset->format)));
207 x += format_text->get_w();
208 //printf("FormatTools::create_objects %d %p\n", __LINE__, window);
209 window->add_subwindow(format_button = new FormatFormat(x, y, this));
210 format_button->create_objects();
211 x += format_button->get_w() + 5;
212 window->add_subwindow(ffmpeg_type = new FFMpegType(x, y, 50, 1, asset->fformat));
213 x += ffmpeg_type->get_w();
214 window->add_subwindow(format_ffmpeg = new FormatFFMPEG(x, y, this));
215 format_ffmpeg->create_objects();
218 y += format_button->get_h() + 10;
221 window->add_subwindow(audio_title = new BC_Title(x, y, _("Audio:"), LARGEFONT, RED));
223 window->add_subwindow(aparams_button = new FormatAParams(mwindow, this, x, y));
224 x += aparams_button->get_w() + 10;
227 window->add_subwindow(audio_switch = new FormatAudio(x, y, this, asset->audio_data));
230 y += aparams_button->get_h() + 10;
232 // Audio channels only used for recording.
233 // if(prompt_audio_channels)
235 // window->add_subwindow(channels_title = new BC_Title(x, y, _("Number of audio channels to record:")));
237 // window->add_subwindow(channels_button = new FormatChannels(x, y, this));
238 // x += channels_button->get_w() + 5;
239 // window->add_subwindow(channels_tumbler = new BC_ITumbler(channels_button, 1, MAXCHANNELS, x, y));
240 // y += channels_button->get_h() + 20;
244 //printf("FormatTools::create_objects 6\n");
245 aparams_thread = new FormatAThread(this);
248 //printf("FormatTools::create_objects 7\n");
252 //printf("FormatTools::create_objects 8\n");
253 window->add_subwindow(video_title = new BC_Title(x, y, _("Video:"), LARGEFONT, RED));
255 if(prompt_video_compression)
257 window->add_subwindow(vparams_button = new FormatVParams(mwindow, this, x, y));
258 x += vparams_button->get_w() + 10;
261 //printf("FormatTools::create_objects 9\n");
264 window->add_subwindow(video_switch = new FormatVideo(x, y, this, asset->video_data));
265 y += video_switch->get_h();
269 y += vparams_button->get_h();
272 //printf("FormatTools::create_objects 10\n");
274 vparams_thread = new FormatVThread(this);
277 //printf("FormatTools::create_objects 11\n");
282 window->add_subwindow(multiple_files = new FormatMultiple(mwindow, x, y, strategy));
283 y += multiple_files->get_h() + 10;
286 //printf("FormatTools::create_objects 12\n");
292 void FormatTools::update_driver(int driver)
294 this->video_driver = driver;
296 locked_compressor = 0;
300 case VIDEO4LINUX2MPEG:
301 // Just give the user information about how the stream is going to be
302 // stored but don't change the asset.
303 // Want to be able to revert to user settings.
304 if(asset->format != FILE_MPEG)
306 format_text->update(_("MPEG stream"));
307 asset->format = FILE_MPEG;
309 audio_switch->update(1);
310 video_switch->update(1);
313 case CAPTURE_IEC61883:
314 case CAPTURE_FIREWIRE:
317 case VIDEO4LINUX2JPEG:
318 case CAPTURE_JPEG_WEBCAM:
319 asset->format = FILE_FFMPEG;
320 format_text->update(File::formattostr(asset->format));
323 case CAPTURE_IEC61883:
324 case CAPTURE_FIREWIRE:
325 locked_compressor = (char*)CODEC_TAG_DVSD;
330 case VIDEO4LINUX2JPEG:
331 locked_compressor = (char*)CODEC_TAG_MJPEG;
334 case CAPTURE_JPEG_WEBCAM:
335 locked_compressor = (char*)CODEC_TAG_JPEG;
338 if( locked_compressor )
339 strcpy(asset->vcodec, locked_compressor);
341 audio_switch->update(asset->audio_data);
342 video_switch->update(asset->video_data);
346 format_text->update(File::formattostr(asset->format));
347 audio_switch->update(asset->audio_data);
348 video_switch->update(asset->video_data);
351 close_format_windows();
354 void FormatTools::update_format()
356 if( do_audio && prompt_audio && audio_switch ) {
357 audio_switch->update(asset->audio_data);
358 if( !asset->audio_data )
359 audio_switch->disable();
361 audio_switch->enable();
363 if( do_video && prompt_video && video_switch ) {
364 video_switch->update(asset->video_data);
365 if( !asset->video_data )
366 video_switch->disable();
368 video_switch->enable();
370 if( asset->format == FILE_FFMPEG ) {
372 format_ffmpeg->show();
376 format_ffmpeg->hide();
380 int FormatTools::handle_event()
385 Asset* FormatTools::get_asset()
390 void FormatTools::update_extension()
392 const char *extension = File::get_tag(asset->format);
393 // split multiple extensions
394 ArrayList<const char*> extensions;
395 int len = strlen(extension);
396 const char *extension_ptr = extension;
397 for(int i = 0; i <= len; i++)
399 if(extension[i] == '/' || extension[i] == 0)
401 extensions.append(extension_ptr);
402 extension_ptr = extension + i + 1;
406 if(extensions.size())
408 char *ptr = strrchr(asset->path, '.');
411 ptr = asset->path + strlen(asset->path);
417 // test for equivalent extension
418 int need_extension = 1;
419 //int extension_len = 0;
420 for(int i = 0; i < extensions.size() && need_extension; i++)
423 extension_ptr = extensions.get(i);
426 while(*ptr1 != 0 && *extension_ptr != 0 && *extension_ptr != '/')
428 if(tolower(*ptr1) != tolower(*extension_ptr))
438 *extension_ptr != 0 &&
439 *extension_ptr != '/')
443 //printf("FormatTools::update_extension %d %d\n", __LINE__, need_extension);
448 extension_ptr = asset->format != FILE_FFMPEG ?
449 extensions.get(0) : asset->fformat;
450 while(*extension_ptr != 0 && *extension_ptr != '/')
451 *ptr1++ = *extension_ptr++;
455 int character1 = ptr - asset->path;
456 int character2 = strlen(asset->path);
457 // *(asset->path + character2) = 0;
460 path_textbox->update(asset->path);
461 path_textbox->set_selection(character1, character2, character2);
466 void FormatTools::update(Asset *asset, int *strategy)
469 this->strategy = strategy;
472 path_textbox->update(asset->path);
473 format_text->update(File::formattostr(plugindb, asset->format));
474 if(do_audio && prompt_audio && audio_switch)
475 audio_switch->update(asset->audio_data);
476 if(do_video && prompt_video && video_switch)
477 video_switch->update(asset->video_data);
480 multiple_files->update(strategy);
482 close_format_windows();
485 void FormatTools::close_format_windows()
487 // This is done in ~file
488 if(aparams_thread && aparams_thread->running())
490 aparams_thread->file->close_window();
491 aparams_thread->join();
493 if(vparams_thread && vparams_thread->running())
495 vparams_thread->file->close_window();
496 vparams_thread->join();
500 int FormatTools::get_w()
505 void FormatTools::set_w(int w)
510 void FormatTools::reposition_window(int &init_x, int &init_y)
517 path_textbox->reposition_window(x, y);
518 x += path_textbox->get_w() + 5;
519 path_button->reposition_window(x, y);
520 x -= path_textbox->get_w() + 5;
524 format_title->reposition_window(x, y);
526 format_text->reposition_window(x, y);
527 x += format_text->get_w();
528 format_button->reposition_window(x, y);
531 y += format_button->get_h() + 10;
535 audio_title->reposition_window(x, y);
537 aparams_button->reposition_window(x, y);
538 x += aparams_button->get_w() + 10;
539 if(prompt_audio) audio_switch->reposition_window(x, y);
542 y += aparams_button->get_h() + 20;
543 if(prompt_audio_channels)
545 channels_title->reposition_window(x, y);
547 channels_button->reposition_window(x, y);
548 x += channels_button->get_w() + 5;
549 channels_tumbler->reposition_window(x, y);
550 y += channels_button->get_h() + 20;
558 video_title->reposition_window(x, y);
560 if(prompt_video_compression)
562 vparams_button->reposition_window(x, y);
563 x += vparams_button->get_w() + 10;
568 video_switch->reposition_window(x, y);
569 y += video_switch->get_h();
573 y += vparams_button->get_h();
582 multiple_files->reposition_window(x, y);
583 y += multiple_files->get_h() + 10;
590 int FormatTools::set_audio_options()
592 // if(video_driver == CAPTURE_DVB)
597 if(!aparams_thread->running())
599 aparams_thread->start();
603 aparams_thread->file->raise_window();
608 int FormatTools::set_video_options()
610 // if(video_driver == CAPTURE_DVB)
615 if(!vparams_thread->running())
617 vparams_thread->start();
621 vparams_thread->file->raise_window();
631 FormatAParams::FormatAParams(MWindow *mwindow, FormatTools *format, int x, int y)
632 : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
634 this->format = format;
635 set_tooltip(_("Configure audio compression"));
638 FormatAParams::~FormatAParams()
642 int FormatAParams::handle_event()
644 format->set_audio_options();
652 FormatVParams::FormatVParams(MWindow *mwindow, FormatTools *format, int x, int y)
653 : BC_Button(x, y, mwindow->theme->get_image_set("wrench"))
655 this->format = format;
656 set_tooltip(_("Configure video compression"));
659 FormatVParams::~FormatVParams()
663 int FormatVParams::handle_event()
665 format->set_video_options();
673 FormatAThread::FormatAThread(FormatTools *format)
676 this->format = format;
681 FormatAThread::~FormatAThread()
688 void FormatAThread::start()
696 void FormatAThread::run()
698 file->get_options(format, 1, 0);
704 FormatVThread::FormatVThread(FormatTools *format)
707 this->format = format;
712 FormatVThread::~FormatVThread()
719 void FormatVThread::start()
726 void FormatVThread::run()
728 file->get_options(format, 0, 1);
735 FormatPathText::FormatPathText(int x, int y, FormatTools *format)
739 format->mwindow->theme->get_image_set("wrench")[0]->get_w() -
744 this->format = format;
747 FormatPathText::~FormatPathText()
750 int FormatPathText::handle_event()
753 calculate_suggestions(format->file_entries);
757 strcpy(format->asset->path, get_text());
758 format->handle_event();
766 FormatAudio::FormatAudio(int x, int y, FormatTools *format, int default_)
770 (char*)(format->recording ? _("Record audio tracks") : _("Render audio tracks")))
772 this->format = format;
775 FormatAudio::~FormatAudio() {}
776 int FormatAudio::handle_event()
778 format->asset->audio_data = get_value();
783 FormatVideo::FormatVideo(int x, int y, FormatTools *format, int default_)
787 (char*)(format->recording ? _("Record video tracks") : _("Render video tracks")))
789 this->format = format;
792 FormatVideo::~FormatVideo() {}
793 int FormatVideo::handle_event()
795 format->asset->video_data = get_value();
802 FormatFormat::FormatFormat(int x, int y, FormatTools *format)
803 : FormatPopup(format->plugindb, x, y, format->use_brender)
805 this->format = format;
808 FormatFormat::~FormatFormat()
812 int FormatFormat::handle_event()
814 BC_ListBoxItem *selection = get_selection(0, 0);
816 int new_format = File::strtoformat(format->plugindb, get_selection(0, 0)->get_text());
817 // if(new_format != format->asset->format)
819 Asset *asset = format->asset;
820 asset->format = new_format;
821 asset->audio_data = File::supports_audio(asset->format);
822 asset->video_data = File::supports_video(asset->format);
823 format->format_text->update(selection->get_text());
824 format->update_extension();
825 format->close_format_windows();
826 format->update_format();
833 FormatFFMPEG::FormatFFMPEG(int x, int y, FormatTools *format)
834 : FFMPEGPopup(format->plugindb, x, y)
836 this->format = format;
839 FormatFFMPEG::~FormatFFMPEG()
843 int FormatFFMPEG::load_defaults(const char *path, const char *type,
844 char *codec, char *codec_options, int len)
846 char default_file[BCTEXTLEN];
847 FFMPEG::set_option_path(default_file, "%s/%s.dfl", path, type);
848 FILE *fp = fopen(default_file,"r");
850 fgets(codec, BCSTRLEN, fp);
852 while( *cp && *cp!='\n' ) ++cp;
854 while( len > 0 && fgets(codec_options, len, fp) ) {
855 int n = strlen(codec_options);
856 codec_options += n; len -= n;
859 FFMPEG::set_option_path(default_file, "%s/%s", path, codec);
860 return FFMPEG::load_options(default_file, codec_options, len);
863 int FormatFFMPEG::handle_event()
865 BC_ListBoxItem *selection = get_selection(0, 0);
867 char *text = get_selection(0, 0)->get_text();
868 format->ffmpeg_type->update(text);
869 Asset *asset = format->asset;
870 strcpy(asset->fformat, text);
871 strcpy(asset->ff_audio_options, "");
872 strcpy(asset->ff_video_options, "");
873 asset->audio_data = !load_defaults("audio", text, asset->acodec,
874 asset->ff_audio_options, sizeof(asset->ff_audio_options));
875 asset->video_data = !load_defaults("video", text, asset->vcodec,
876 asset->ff_video_options, sizeof(asset->ff_video_options));
877 format->update_extension();
878 format->close_format_windows();
879 format->update_format();
887 FormatChannels::FormatChannels(int x, int y, FormatTools *format)
888 : BC_TextBox(x, y, 100, 1, format->asset->channels)
890 this->format = format;
893 FormatChannels::~FormatChannels()
897 int FormatChannels::handle_event()
899 format->asset->channels = atol(get_text());
904 FormatToTracks::FormatToTracks(int x, int y, int *output)
905 : BC_CheckBox(x, y, *output, _("Overwrite project with output"))
907 this->output = output;
910 FormatToTracks::~FormatToTracks()
914 int FormatToTracks::handle_event()
916 *output = get_value();
921 FormatMultiple::FormatMultiple(MWindow *mwindow, int x, int y, int *output)
924 (*output == FILE_PER_LABEL) || (*output == FILE_PER_LABEL_FARM),
925 _("Create new file at each label"))
927 this->output = output;
928 this->mwindow = mwindow;
931 FormatMultiple::~FormatMultiple()
935 int FormatMultiple::handle_event()
939 if(mwindow->preferences->use_renderfarm)
940 *output = FILE_PER_LABEL_FARM;
942 *output = FILE_PER_LABEL;
946 if(mwindow->preferences->use_renderfarm)
947 *output = SINGLE_PASS_FARM;
949 *output = SINGLE_PASS;
954 void FormatMultiple::update(int *output)
956 this->output = output;
957 if(*output == FILE_PER_LABEL_FARM ||
958 *output ==FILE_PER_LABEL)