7 // work around for __STDC_CONSTANT_MACROS
11 #include "bcwindowbase.h"
12 #include "bitspopup.h"
17 #include "fileffmpeg.h"
18 #include "filesystem.h"
19 #include "indexfile.h"
21 #include "mainerror.h"
22 #include "mainprogress.h"
24 #include "preferences.h"
25 #include "videodevice.inc"
27 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
28 : FileBase(asset, file)
31 if(asset->format == FILE_UNKNOWN)
32 asset->format = FILE_FFMPEG;
35 FileFFMPEG::~FileFFMPEG()
41 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
42 int x, int y, char *title_text, int *output)
43 : BC_TumbleTextBox(window, *output, -1, INT_MAX, 100, y, 100)
45 this->window = window;
46 this->x = x; this->y = y;
47 this->title_text = title_text;
48 this->output = output;
51 FFMpegConfigNum::~FFMpegConfigNum()
55 void FFMpegConfigNum::create_objects()
57 window->add_subwindow(title = new BC_Title(x, y, title_text));
58 BC_TumbleTextBox::create_objects();
61 int FFMpegConfigNum::update_param(const char *param, const char *opts)
63 char value[BCTEXTLEN];
64 if( !FileFFMPEG::get_ff_option(param, opts, value) ) {
65 if( (*output = atoi(value)) < 0 ) {
70 BC_TumbleTextBox::update((int64_t)*output);
75 int FFMpegConfigNum::handle_event()
77 *output = atoi(get_text());
81 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
82 int x, int y, char *title_text, int *output)
83 : FFMpegConfigNum(window, x, y, title_text, output)
87 int FFMpegAudioBitrate::handle_event()
89 int ret = FFMpegAudioNum::handle_event();
93 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
94 int x, int y, char *title_text, int *output)
95 : FFMpegConfigNum(window, x, y, title_text, output)
99 int FFMpegVideoBitrate::handle_event()
101 int ret = FFMpegVideoNum::handle_event();
102 Asset *asset = window()->asset;
103 if( asset->ff_video_bitrate > 0 )
104 window()->quality->disable();
105 else if( !window()->quality->get_textbox()->is_hidden() )
106 window()->quality->enable();
110 int FFMpegVideoQuality::handle_event()
112 int ret = FFMpegVideoNum::handle_event();
113 Asset *asset = window()->asset;
114 if( asset->ff_video_quality >= 0 )
115 window()->bitrate->disable();
116 else if( !window()->bitrate->get_textbox()->is_hidden() )
117 window()->bitrate->enable();
121 void FileFFMPEG::set_parameters(char *cp, int len, const char *bp)
123 char *ep = cp + len-2, ch = 0;
124 while( cp < ep && *bp != 0 ) { ch = *bp++; *cp++ = ch; }
125 if( ch != '\n' ) *cp++ = '\n';
129 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
130 Asset *asset, BC_WindowBase *&format_window,
131 int audio_options, int video_options)
134 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
135 format_window = window;
136 window->create_objects();
137 if( !window->run_window() )
138 set_parameters(asset->ff_audio_options, sizeof(asset->ff_audio_options),
139 window->audio_options->get_text());
142 else if(video_options) {
143 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
144 format_window = window;
145 window->create_objects();
146 if( !window->run_window() )
147 set_parameters(asset->ff_video_options, sizeof(asset->ff_video_options),
148 window->video_options->get_text());
153 int FileFFMPEG::check_sig(Asset *asset)
155 char *ptr = strstr(asset->path, ".pcm");
157 ptr = strstr(asset->path, ".raw");
161 int ret = !ffmpeg.init_decoder(asset->path) &&
162 !ffmpeg.open_decoder() ? 1 : 0;
166 void FileFFMPEG::get_info(char *path, char *text, int len)
170 cp += sprintf(cp, _("file path: %s\n"), path);
173 if( stat(path, &st) < 0 ) {
174 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
178 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
180 if( !ret ) ret = ffmpeg.init_decoder(path);
181 if( !ret ) ret = ffmpeg.open_decoder();
183 cp += sprintf(cp, _("info:\n"));
184 ffmpeg.info(cp, len-(cp-text));
187 sprintf(cp, _("== open failed\n"));
190 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
191 int &width, int &height, char *title)
194 pid = ff->ff_video_pid(track);
195 framerate = ff->ff_frame_rate(track);
196 width = ff->ff_video_width(track);
197 height = ff->ff_video_height(track);
198 if( title ) *title = 0;
202 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
205 return ff->ff_audio_for_video(vstream, astream, channel_mask);
208 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
210 if( !ff || !asset->video_data ) return 1;
211 asset->width = ff->ff_video_width(vstream);
212 asset->height = ff->ff_video_height(vstream);
213 asset->video_length = ff->ff_video_frames(vstream);
214 if( (asset->video_length = ff->ff_video_frames(vstream)) < 2 )
215 asset->video_length = asset->video_length < 0 ? 0 : -1;
216 asset->frame_rate = ff->ff_frame_rate(vstream);
220 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
222 if( !ff || !asset->audio_data ) return 1;
223 asset->sample_rate = ff->ff_sample_rate(astream);
224 asset->audio_length = ff->ff_audio_samples(astream);
228 int FileFFMPEG::open_file(int rd, int wr)
232 ff = new FFMPEG(this);
235 result = ff->init_decoder(asset->path);
236 if( !result ) result = ff->open_decoder();
238 int audio_channels = ff->ff_total_audio_channels();
239 if( audio_channels > 0 ) {
240 asset->audio_data = 1;
241 asset->channels = audio_channels;
242 asset->sample_rate = ff->ff_sample_rate(0);
243 asset->audio_length = ff->ff_audio_samples(0);
245 int video_layers = ff->ff_total_video_layers();
246 if( video_layers > 0 ) {
247 asset->video_data = 1;
248 if( !asset->layers ) asset->layers = video_layers;
249 asset->actual_width = ff->ff_video_width(0);
250 asset->actual_height = ff->ff_video_height(0);
251 if( !asset->width ) asset->width = asset->actual_width;
252 if( !asset->height ) asset->height = asset->actual_height;
253 if( !asset->video_length &&
254 (asset->video_length = ff->ff_video_frames(0)) < 2 )
255 asset->video_length = asset->video_length < 0 ? 0 : -1;
256 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
258 IndexState *index_state = asset->index_state;
259 index_state->read_markers(file->preferences->index_directory, asset->path);
263 result = ff->init_encoder(asset->path);
264 // must be in this order or dvdauthor will fail
265 if( !result && asset->video_data )
266 result = ff->open_encoder("video", asset->vcodec);
267 if( !result && asset->audio_data )
268 result = ff->open_encoder("audio", asset->acodec);
273 int FileFFMPEG::close_file()
281 int FileFFMPEG::write_samples(double **buffer, int64_t len)
283 if( !ff || len < 0 ) return -1;
285 int ret = ff->encode(stream, buffer, len);
289 int FileFFMPEG::write_frames(VFrame ***frames, int len)
292 int ret = 0, layer = 0;
293 for(int i = 0; i < 1; i++) {
294 for(int j = 0; j < len && !ret; j++) {
295 VFrame *frame = frames[i][j];
296 ret = ff->encode(layer, frame);
303 int FileFFMPEG::read_samples(double *buffer, int64_t len)
305 if( !ff || len < 0 ) return -1;
306 int ch = file->current_channel;
307 int64_t pos = file->current_sample;
308 int ret = ff->decode(ch, pos, buffer, len);
309 if( ret > 0 ) return 0;
310 memset(buffer,0,len*sizeof(*buffer));
314 int FileFFMPEG::read_frame(VFrame *frame)
317 int layer = file->current_layer;
318 int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
319 int ret = ff->decode(layer, pos, frame);
320 frame->set_status(ret);
321 if( ret >= 0 ) return 0;
322 frame->clear_frame();
327 int64_t FileFFMPEG::get_memory_usage()
332 int FileFFMPEG::colormodel_supported(int colormodel)
337 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
342 case PLAYBACK_X11_XV:
343 case PLAYBACK_ASYNCHRONOUS:
345 case PLAYBACK_X11_GL:
347 case PLAYBACK_DV1394:
348 case PLAYBACK_FIREWIRE:
352 case VIDEO4LINUX2JPEG:
353 return BC_COMPRESSED;
355 case VIDEO4LINUX2MPEG:
357 case CAPTURE_JPEG_WEBCAM:
358 return BC_COMPRESSED;
359 case CAPTURE_YUYV_WEBCAM:
361 case CAPTURE_FIREWIRE:
362 case CAPTURE_IEC61883:
370 int FileFFMPEG::get_ff_option(const char *nm, const char *options, char *value)
372 for( const char *cp=options; *cp!=0; ) {
373 char line[BCTEXTLEN], *bp = line, *ep = bp+sizeof(line)-1;
374 while( bp < ep && *cp && *cp!='\n' ) *bp++ = *cp++;
377 if( !line[0] || line[0] == '#' || line[0] == ';' ) continue;
378 char key[BCSTRLEN], val[BCTEXTLEN];
379 if( FFMPEG::scan_option_line(line, key, val) ) continue;
380 if( !strcmp(key, nm) ) {
390 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
391 : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
392 parent_window->get_abs_cursor_x(1),
393 parent_window->get_abs_cursor_y(1),
396 this->parent_window = parent_window;
402 ff_options_dialog = 0;
405 FFMPEGConfigAudio::~FFMPEGConfigAudio()
407 delete ff_options_dialog;
408 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
410 presets.remove_all_objects();
414 void FFMPEGConfigAudio::create_objects()
417 lock_window("FFMPEGConfigAudio::create_objects");
420 char option_path[BCTEXTLEN];
421 FFMPEG::set_option_path(option_path, "audio");
422 fs.update(option_path);
423 int total_files = fs.total_files();
424 for(int i = 0; i < total_files; i++) {
425 const char *name = fs.get_entry(i)->get_name();
426 if( asset->fformat[0] != 0 ) {
427 const char *ext = strrchr(name,'.');
429 if( strcmp(asset->fformat, ++ext) ) continue;
431 presets.append(new BC_ListBoxItem(name));
434 if( asset->acodec[0] ) {
435 int k = presets.size();
436 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
437 if( k < 0 ) asset->acodec[0] = 0;
440 if( !asset->acodec[0] && presets.size() > 0 )
441 strcpy(asset->acodec, presets[0]->get_text());
443 add_tool(new BC_Title(x, y, _("Preset:")));
445 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
446 preset_popup->create_objects();
449 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
450 bitrate->create_objects();
451 bitrate->set_increment(1000);
452 bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
454 y += bitrate->get_h() + 10;
455 BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
456 add_subwindow(title);
458 ff_options_dialog = new FFOptionsAudioDialog(this);
459 int x1 = x + title->get_w() + 8;
460 add_subwindow(new FFOptionsViewAudio(this, x1, y, _("view")));
463 if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
464 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
465 FFMPEG::load_options(option_path, asset->ff_audio_options,
466 sizeof(asset->ff_audio_options));
469 audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
470 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
471 audio_options->create_objects();
472 add_subwindow(new BC_OKButton(this));
473 add_subwindow(new BC_CancelButton(this));
476 bitrate->update_param("cin_bitrate", asset->ff_audio_options);
481 int FFMPEGConfigAudio::close_event()
487 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
488 int x, int y, int w, int rows, int size, char *text)
489 : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
491 this->audio_popup = audio_popup;
495 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
496 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
501 int FFMPEGConfigAudioPopup::handle_event()
503 strcpy(popup->asset->acodec, get_text());
504 Asset *asset = popup->asset;
505 asset->ff_audio_bitrate = 0;
506 char option_path[BCTEXTLEN];
507 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
508 FFMPEG::load_options(option_path, asset->ff_audio_options,
509 sizeof(asset->ff_audio_options));
510 popup->audio_options->update(asset->ff_audio_options);
511 popup->audio_options->set_text_row(0);
513 popup->bitrate->update_param("cin_bitrate", asset->ff_audio_options);
518 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
519 char *title_text, int x, int y, int *output)
520 : BC_CheckBox(x, y, *output, title_text)
523 this->output = output;
525 int FFMPEGConfigAudioToggle::handle_event()
527 *output = get_value();
533 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
534 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
535 parent_window->get_abs_cursor_x(1),
536 parent_window->get_abs_cursor_y(1),
539 this->parent_window = parent_window;
548 FFMPEGConfigVideo::~FFMPEGConfigVideo()
550 delete ff_options_dialog;
551 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
552 if(preset_popup) delete preset_popup;
553 presets.remove_all_objects();
557 void FFMPEGConfigVideo::create_objects()
560 lock_window("FFMPEGConfigVideo::create_objects");
562 add_subwindow(new BC_Title(x, y, _("Compression:")));
566 char option_path[BCTEXTLEN];
567 FFMPEG::set_option_path(option_path, "video");
568 fs.update(option_path);
569 int total_files = fs.total_files();
570 for(int i = 0; i < total_files; i++) {
571 const char *name = fs.get_entry(i)->get_name();
572 if( asset->fformat[0] != 0 ) {
573 const char *ext = strrchr(name,'.');
575 if( strcmp(asset->fformat, ++ext) ) continue;
577 presets.append(new BC_ListBoxItem(name));
580 if( asset->vcodec[0] ) {
581 int k = presets.size();
582 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
583 if( k < 0 ) asset->vcodec[0] = 0;
586 if( !asset->vcodec[0] && presets.size() > 0 )
587 strcpy(asset->vcodec, presets[0]->get_text());
589 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
590 preset_popup->create_objects();
592 if( asset->ff_video_bitrate > 0 && asset->ff_video_quality >= 0 ) {
593 asset->ff_video_bitrate = 0; asset->ff_video_quality = -1;
597 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
598 bitrate->create_objects();
599 bitrate->set_increment(100000);
600 bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
601 y += bitrate->get_h() + 5;
602 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
603 quality->create_objects();
604 quality->set_increment(1);
605 quality->set_boundaries((int64_t)-1, (int64_t)51);
607 y += quality->get_h() + 10;
608 BC_Title *title = new BC_Title(x, y, _("Video Options:"));
609 add_subwindow(title);
611 ff_options_dialog = new FFOptionsVideoDialog(this);
612 int x1 = x + title->get_w() + 8;
613 add_subwindow(new FFOptionsViewVideo(this, x1, y, _("view")));
616 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
617 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
618 FFMPEG::load_options(option_path, asset->ff_video_options,
619 sizeof(asset->ff_video_options));
622 video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
623 sizeof(asset->ff_video_options)-1, asset->ff_video_options);
624 video_options->create_objects();
625 add_subwindow(new BC_OKButton(this));
626 add_subwindow(new BC_CancelButton(this));
629 bitrate->update_param("cin_bitrate", asset->ff_video_options);
630 quality->update_param("cin_quality", asset->ff_video_options);
632 if( asset->ff_video_bitrate > 0 ) quality->disable();
633 else if( asset->ff_video_quality >= 0 ) bitrate->disable();
637 int FFMPEGConfigVideo::close_event()
643 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
644 int x, int y, int w, int rows, int size, char *text)
645 : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
647 this->video_popup = video_popup;
651 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
652 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
657 int FFMPEGConfigVideoPopup::handle_event()
659 strcpy(popup->asset->vcodec, get_text());
660 Asset *asset = popup->asset;
661 char option_path[BCTEXTLEN];
662 asset->ff_video_bitrate = 0; asset->ff_video_quality = -1;
663 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
664 FFMPEG::load_options(option_path, asset->ff_video_options,
665 sizeof(asset->ff_video_options));
666 popup->video_options->update(asset->ff_video_options);
667 popup->video_options->set_text_row(0);
669 popup->bitrate->update_param("cin_bitrate", asset->ff_video_options);
670 popup->quality->update_param("cin_quality", asset->ff_video_options);
675 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
676 char *title_text, int x, int y, int *output)
677 : BC_CheckBox(x, y, *output, title_text)
680 this->output = output;
682 int FFMPEGConfigVideoToggle::handle_event()
684 *output = get_value();
688 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
689 const char *title, int64_t length, int64_t *position, int *canceled)
692 this->index_file = index_file;
693 this->progress_bar = progress_bar;
694 strcpy(this->progress_title, title);
695 this->length = length;
696 this->position = position;
697 this->canceled = canceled;
702 FFMPEGScanProgress::~FFMPEGScanProgress()
709 void FFMPEGScanProgress::run()
712 if( progress_bar->update(*position) ) {
713 *canceled = done = 1;
716 index_file->redraw_edits(0);
721 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
724 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
726 IndexState *index_state = index_file->get_state();
727 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
728 index_state->reset_index();
729 index_state->reset_markers();
730 index_state->index_status = INDEX_BUILDING;
732 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
733 FFAudioStream *aud = ff->ffaudio[aidx];
734 index_state->add_audio_stream(aud->channels, aud->length);
738 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
739 char *index_path = index_file->index_filename;
742 int64_t scan_position = 0;
743 FFMPEGScanProgress *scan_progress = 0;
745 char progress_title[BCTEXTLEN];
746 sprintf(progress_title, _("Creating %s\n"), index_path);
747 progress_bar->update_title(progress_title, 1);
748 progress_bar->update_length(file_bytes);
749 scan_progress = new FFMPEGScanProgress(index_file,
750 progress_bar, progress_title, file_bytes,
751 &scan_position, &canceled);
754 index_state->index_bytes = file_bytes;
755 index_state->init_scan(file->preferences->index_size);
757 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
758 index_state->reset_index();
759 index_state->reset_markers();
763 delete scan_progress;
764 if( canceled ) return 1;
766 index_state->marker_status = MARKERS_READY;
767 return index_state->create_index(index_path, asset);
772 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
773 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
776 update(); // init col/wid/columns
780 ~FFOptions_OptPanel()
784 void FFOptions_OptPanel::create_objects()
786 const char *cols[] = { _("option"), _("value"), };
787 const int col1_w = 150;
788 int wids[] = { col1_w, get_w()-col1_w };
789 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
792 int FFOptions_OptPanel::update()
796 FFOptions &options = fwin->options;
797 for( int i=0; i<options.size(); ++i ) {
798 FFOptions_Opt *opt = options[i];
799 opts.append(opt->item_name);
800 vals.append(opt->item_value);
806 int FFOptions_OptPanel::cursor_leave_event()
813 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
819 FFOptions_OptName::~FFOptions_OptName()
823 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
829 void FFOptions_OptValue::update()
832 char val[BCTEXTLEN]; val[0] = 0;
833 opt->get(val, sizeof(val));
837 void FFOptions_OptValue::update(const char *v)
842 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
844 this->options = options;
846 item_name = new FFOptions_OptName(this, nm);
847 item_value = new FFOptions_OptValue(this);
850 FFOptions_Opt::~FFOptions_Opt()
856 char *FFOptions_Opt::get(char *vp, int sz)
860 if( !opt ) return cp;
862 void *obj = (void *)options->obj;
864 if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
865 const char *val = (const char *)bp;
866 if( opt->unit && *val ) {
868 const char *uid = unit_name(id);
871 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
872 if( sz > 0 ) vp[sz-1] = 0;
879 void FFOptions_Opt::set(const char *val)
881 void *obj = (void *)options->obj;
882 if( !obj || !opt ) return;
883 av_opt_set(obj, opt->name, val, 0);
887 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
894 FFOptionsKindItem::~FFOptionsKindItem()
898 int FFOptionsKindItem::handle_event()
900 FFOptionsWindow *fwin = kind->fwin;
901 FFOptions &options = fwin->options;
902 options.initialize(fwin, idx);
907 const char *FFOptionsKind::kinds[] = {
908 N_("codec"), // FF_KIND_CODEC
909 N_("ffmpeg"), // FF_KIND_FFMPEG
913 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
914 : BC_PopupMenu(x, y, w-calculate_w(0), "")
924 void FFOptionsKind::create_objects()
926 for( int i=0; i<(int)(sizeof(kinds)/sizeof(kinds[0])); ++i )
927 add_item(new FFOptionsKindItem(this, _(kinds[i]), i));
930 int FFOptionsKind::handle_event()
935 void FFOptionsKind::set(int k)
938 set_text(_(kinds[k]));
942 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
943 : BC_TextBox(x, y, w, 1, (char *)"")
953 int FFOptionsText::handle_event()
959 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
960 : BC_PopupMenu(x, y, w, "")
970 int FFOptionsUnits::handle_event()
972 const char *text = get_text();
973 if( text && fwin->selected ) {
974 fwin->selected->set(text);
975 fwin->selected->item_value->update();
976 av_dict_set(&fwin->dialog->ff_opts,
977 fwin->selected->item_name->get_text(),
978 fwin->selected->item_value->get_text(), 0);
985 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
986 : BC_GenericButton(x, y, _("Apply"))
996 int FFOptionsApply::handle_event()
998 const char *text = fwin->text->get_text();
999 if( text && fwin->selected ) {
1000 fwin->selected->set(text);
1001 fwin->selected->item_value->update();
1002 av_dict_set(&fwin->dialog->ff_opts,
1003 fwin->selected->item_name->get_text(),
1004 fwin->selected->item_value->get_text(), 0);
1010 FFOptions::FFOptions()
1016 FFOptions::~FFOptions()
1018 remove_all_objects();
1019 avcodec_free_context(&avctx);
1022 int FFOptions::cmpr(const void *a, const void *b)
1024 FFOptions_Opt *ap = *(FFOptions_Opt **)a;
1025 FFOptions_Opt *bp = *(FFOptions_Opt **)b;
1026 const char *vap = ap->item_name->get_text();
1027 const char *vbp = bp->item_name->get_text();
1028 return strcmp(vap, vbp);
1031 void FFOptions::initialize(FFOptionsWindow *win, int kind)
1033 remove_all_objects();
1038 avctx = avcodec_alloc_context3(win->dialog->codec);
1042 obj = (const void *)avctx->priv_data;
1044 case FF_KIND_FFMPEG:
1045 obj = (const void *)avctx;
1050 FFOptions &conf = *this;
1051 const AVOption *opt = 0;
1052 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1053 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1055 for( int i=0; !dupl && i<size(); ++i ) {
1056 FFOptions_Opt *fopt = conf[i];
1057 const AVOption *op = fopt->opt;
1058 if( op->offset != opt->offset ) continue;
1059 if( op->type != opt->type ) continue;
1061 if( strlen(op->name) < strlen(opt->name) )
1064 if( dupl ) continue;
1065 FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1067 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1068 fopt->item_value->update(vp);
1072 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1073 win->kind->set(kind);
1074 win->panel->update();
1075 win->panel->set_yposition(0);
1078 int FFOptions::update()
1081 FFOptions &conf = *this;
1083 for( int i=0; i<size(); ++i ) {
1084 FFOptions_Opt *fopt = conf[i];
1085 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1086 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1087 fopt->item_value->update(val);
1093 void FFOptions::dump(FILE *fp)
1096 const AVOption *opt = 0;
1097 FFOptions &conf = *this;
1099 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1100 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1102 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1103 if( k < 0 ) continue;
1104 FFOptions_Opt *fopt = conf[k];
1105 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1106 fprintf(fp, " %s:=%s", opt->name, vp);
1108 char unt[BCTEXTLEN], *up = unt;
1110 fprintf(fp, "%s", unt);
1117 void FFOptionsWindow::update(FFOptions_Opt *opt)
1119 if( selected != opt ) {
1120 if( selected ) selected->item_name->set_selected(0);
1122 if( selected ) selected->item_name->set_selected(1);
1124 clear_box(0,0, 0,panel->get_y());
1125 char str[BCTEXTLEN], *sp;
1127 if( opt ) opt->types(sp);
1130 if( opt ) opt->ranges(sp);
1132 while( units->total_items() ) units->del_item(0);
1133 char unit[BCSTRLEN]; strcpy(unit, "()");
1134 if( opt && opt->opt ) {
1135 ArrayList<const char *> names;
1137 if( opt->opt->unit ) {
1138 n = opt->units(names);
1139 if( n > 0 ) strcpy(unit,opt->opt->unit);
1141 for( int i=0; i<n; ++i )
1142 units->add_item(new BC_MenuItem(names[i], 0));
1144 units->set_text(unit);
1145 char val[BCTEXTLEN]; val[0] = 0;
1146 if( opt ) opt->get(val, sizeof(val));
1152 void FFOptions_OptPanel::show_tip(const char *tip)
1155 int len = strlen(tip);
1156 if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1157 strncpy(tip_text,tip,len);
1159 int line_limit = 60;
1160 int limit2 = line_limit/2;
1161 int limit4 = line_limit/4-2;
1162 char *cp = tip_text, *dp = cp+len;
1163 int n; char *bp, *ep, *pp, *sp;
1165 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1166 // target about half remaining line, constrain line_limit
1167 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1169 // search for last punct, last space before line_limit
1170 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1171 if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1172 else if( isspace(*cp) ) sp = cp;
1176 // first, after punctuation
1177 if( pp && pp-bp >= limit4 )
1183 // last, on next space
1185 while( cp<dp && !isspace(*cp) ) ++cp;
1192 fwin->panel->set_tooltip(tip_text);
1193 fwin->panel->show_tooltip();
1196 int FFOptions_OptPanel::selection_changed()
1198 FFOptions_Opt *opt = 0;
1199 BC_ListBoxItem *item = get_selection(0, 0);
1201 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1202 opt = opt_name->opt;
1205 if( opt ) show_tip(opt->tip());
1210 int FFOptions_Opt::types(char *rp)
1212 const char *cp = "";
1213 if( opt ) switch (opt->type) {
1214 case AV_OPT_TYPE_FLAGS: cp = N_("<flags>"); break;
1215 case AV_OPT_TYPE_INT: cp = N_("<int>"); break;
1216 case AV_OPT_TYPE_INT64: cp = N_("<int64>"); break;
1217 case AV_OPT_TYPE_DOUBLE: cp = N_("<double>"); break;
1218 case AV_OPT_TYPE_FLOAT: cp = N_("<float>"); break;
1219 case AV_OPT_TYPE_STRING: cp = N_("<string>"); break;
1220 case AV_OPT_TYPE_RATIONAL: cp = N_("<rational>"); break;
1221 case AV_OPT_TYPE_BINARY: cp = N_("<binary>"); break;
1222 case AV_OPT_TYPE_IMAGE_SIZE: cp = N_("<image_size>"); break;
1223 case AV_OPT_TYPE_VIDEO_RATE: cp = N_("<video_rate>"); break;
1224 case AV_OPT_TYPE_PIXEL_FMT: cp = N_("<pix_fmt>"); break;
1225 case AV_OPT_TYPE_SAMPLE_FMT: cp = N_("<sample_fmt>"); break;
1226 case AV_OPT_TYPE_DURATION: cp = N_("<duration>"); break;
1227 case AV_OPT_TYPE_COLOR: cp = N_("<color>"); break;
1228 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = N_("<channel_layout>"); break;
1229 case AV_OPT_TYPE_BOOL: cp = N_("<bool>"); break;
1230 default: cp = N_("<undef>"); break;
1232 return sprintf(rp, "%s", _(cp));
1234 int FFOptions_Opt::scalar(double d, char *rp)
1237 if( d == INT_MAX ) cp = "INT_MAX";
1238 else if( d == INT_MIN ) cp = "INT_MIN";
1239 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1240 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1241 else if( d == INT64_MIN ) cp = "I64_MIN";
1242 else if( d == FLT_MAX ) cp = "FLT_MAX";
1243 else if( d == FLT_MIN ) cp = "FLT_MIN";
1244 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1245 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1246 else if( d == DBL_MAX ) cp = "DBL_MAX";
1247 else if( d == DBL_MIN ) cp = "DBL_MIN";
1248 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1249 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1250 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1251 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1252 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1253 else return sprintf(rp, "%g", d);
1254 return sprintf(rp, "%s", cp);
1257 int FFOptions_Opt::ranges(char *rp)
1259 if( !opt ) return 0;
1260 void *obj = (void *)options->obj;
1261 if( !obj ) return 0;
1263 switch (opt->type) {
1264 case AV_OPT_TYPE_INT:
1265 case AV_OPT_TYPE_INT64:
1266 case AV_OPT_TYPE_DOUBLE:
1267 case AV_OPT_TYPE_FLOAT: break;
1270 AVOptionRanges *r = 0;
1272 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1273 for( int i=0; i<r->nb_ranges; ++i ) {
1274 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
1275 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
1276 cp += sprintf(cp, ")");
1278 av_opt_freep_ranges(&r);
1282 int FFOptions_Opt::units(ArrayList<const char *> &names)
1284 if( !opt || !opt->unit ) return 0;
1285 const void *obj = options->obj;
1286 if( !obj ) return 0;
1288 ArrayList<const AVOption *> opts;
1289 const AVOption *opt = NULL;
1290 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1291 if( !opt->unit ) continue;
1292 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1293 if( strcmp(this->opt->unit, opt->unit) ) continue;
1294 int i = opts.size();
1296 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1297 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1300 if( i >= 0 ) continue;
1304 for( int i=0; i<opts.size(); ++i )
1305 names.append(opts[i]->name);
1307 return names.size();
1310 int FFOptions_Opt::units(char *rp)
1312 ArrayList<const char *> names;
1313 int n = units(names);
1316 cp += sprintf(cp, " [%s:", this->opt->unit);
1317 for( int i=0; i<n; ++i )
1318 cp += sprintf(cp, " %s", names[i]);
1319 cp += sprintf(cp, "]:");
1323 const char *FFOptions_Opt::unit_name(int id)
1325 if( !opt || !opt->unit ) return 0;
1326 const void *obj = options->obj;
1327 if( !obj ) return 0;
1329 const char *ret = 0;
1330 const AVOption *opt = NULL;
1331 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1332 if( !opt->unit ) continue;
1333 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1334 if( strcmp(this->opt->unit, opt->unit) ) continue;
1335 if( opt->default_val.i64 != id ) continue;
1336 if( !ret ) { ret = opt->name; continue; }
1337 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1343 const char *FFOptions_Opt::tip()
1345 return !opt ? 0 : opt->help;
1349 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog)
1350 : BC_Window(_(PROGRAM_NAME ": Options"), 60, 30, 640, 400)
1352 this->dialog = dialog;
1356 FFOptionsWindow::~FFOptionsWindow()
1360 void FFOptionsWindow::create_objects()
1363 int x0 = 10, y0 = 10;
1365 add_subwindow(title = new BC_Title(x, y, _("Format: ")));
1366 x += title->get_w();
1367 add_subwindow(new BC_Title(x, y, dialog->format_name));
1369 add_subwindow(title = new BC_Title(x, y, _("Codec: ")));
1370 x += title->get_w();
1371 add_subwindow(new BC_Title(x, y, dialog->codec_name));
1373 x = x0; y += title->get_h() + 10; y0 = y;
1374 add_subwindow(title = new BC_Title(x, y, _("Type: ")));
1375 x += title->get_w() + 8;
1376 add_subwindow(type = new BC_Title(x, y, (char *)""));
1378 add_subwindow(title = new BC_Title(x, y, _("Range: ")));
1379 x += title->get_w() + 8;
1380 add_subwindow(range = new BC_Title(x, y, (char *)""));
1382 x = x0; y += title->get_h() + 10;
1383 add_subwindow(units = new FFOptionsUnits(this, x, y, 120));
1384 x += units->get_w() + 8;
1385 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
1386 add_subwindow(text = new FFOptionsText(this, x, y, x1-x - 8));
1387 add_subwindow(apply = new FFOptionsApply(this, x1, y));
1388 y += units->get_h() + 10;
1389 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1390 kind->create_objects();
1391 const char *kind_text = _("Kind:");
1392 x1 -= BC_Title::calculate_w(this, kind_text) + 8;
1393 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1396 panel_x = x0; panel_y = y0;
1397 panel_w = get_w()-10 - panel_x;
1398 panel_h = get_h()-10 - panel_y - BC_OKButton::calculate_h();
1399 panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1400 add_subwindow(panel);
1401 add_subwindow(new BC_OKButton(this));
1402 add_subwindow(new BC_CancelButton(this));
1403 panel->create_objects();
1404 options.initialize(this, FF_KIND_CODEC);
1409 void FFOptionsWindow::draw()
1414 int FFOptionsWindow::resize_event(int w, int h)
1416 int x1 = w - 8 - kind->get_w();
1417 int y = kind->get_y();
1418 kind->reposition_window(x1, y);
1419 x1 -= kind_title->get_w() + 8;
1420 kind_title->reposition_window(x1,y);
1421 x1 = get_w() - apply->get_w() - 8;
1422 int y1 = units->get_y();
1423 apply->reposition_window(x1, y1);
1424 int x0 = units->get_x() + units->get_w() + 8;
1425 int y0 = units->get_y();
1426 text->reposition_window(x0,y0, x1-x0-8);
1427 panel_w = get_w()-10 - panel_x;
1428 panel_h = get_h()-10 - panel_y;
1429 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1433 FFOptionsDialog::FFOptionsDialog()
1436 this->options_window = 0;
1437 this->codec_name = 0;
1443 FFOptionsDialog::~FFOptionsDialog()
1446 delete [] codec_name;
1449 void FFOptionsDialog::load_options(const char *bp, int len)
1451 char line[BCTEXTLEN];
1452 char key[BCSTRLEN], val[BCTEXTLEN];
1453 const char *dp = bp + len-1;
1455 while( bp < dp && *bp != 0 ) {
1457 char *cp = line, *ep = cp+sizeof(line)-1;
1458 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1460 if( line[0] == '#' ) {
1461 sprintf(key,"#%d", no);
1462 av_dict_set(&ff_opts, key, line, 0);
1465 if( line[0] == '\n' ) continue;
1466 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1467 av_dict_set(&ff_opts, key, val, 0);
1471 void FFOptionsDialog::store_options(char *cp, int len)
1473 char *ep = cp + len-1;
1474 AVDictionaryEntry *elem = 0;
1475 while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1476 if( elem->key[0] == '#' ) {
1477 cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1480 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1485 void FFOptionsDialog::start(const char *format_name, const char *codec_name,
1486 AVCodec *codec, const char *options, int len)
1488 if( options_window ) {
1489 options_window->lock_window("FFOptionsDialog::start");
1490 options_window->raise_window();
1491 options_window->unlock_window();
1495 this->format_name = cstrdup(format_name);
1496 this->codec_name = cstrdup(codec_name);
1497 this->codec = codec;
1500 load_options(options, len);
1502 BC_DialogThread::start();
1505 BC_Window* FFOptionsDialog::new_gui()
1507 options_window = new FFOptionsWindow(this);
1508 options_window->create_objects();
1509 return options_window;
1512 void FFOptionsDialog::handle_done_event(int result)
1515 char options[ff_len];
1516 store_options(options, ff_len);
1517 update_options(options);
1520 delete [] format_name; format_name = 0;
1521 delete [] codec_name; codec_name = 0;
1522 av_dict_free(&ff_opts);
1525 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1527 this->aud_config = aud_config;
1530 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1535 void FFOptionsAudioDialog::update_options(const char *options)
1537 aud_config->audio_options->update(options);
1540 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1542 this->vid_config = vid_config;
1545 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1550 void FFOptionsVideoDialog::update_options(const char *options)
1552 vid_config->video_options->update(options);
1556 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text)
1557 : BC_GenericButton(x, y, text)
1559 this->aud_config = aud_config;
1562 FFOptionsViewAudio::~FFOptionsViewAudio()
1566 int FFOptionsViewAudio::handle_event()
1568 char audio_format[BCSTRLEN]; audio_format[0] = 0;
1569 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1571 Asset *asset = aud_config->asset;
1572 const char *name = asset->acodec;
1573 if( !FFMPEG::get_format(audio_format, "audio", name) &&
1574 !FFMPEG::get_codec(audio_codec, "audio", name) )
1575 codec = avcodec_find_encoder_by_name(audio_codec);
1577 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1580 aud_config->ff_options_dialog->start(audio_format, audio_codec, codec,
1581 asset->ff_audio_options, sizeof(asset->ff_audio_options));
1585 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text)
1586 : BC_GenericButton(x, y, text)
1588 this->vid_config = vid_config;
1591 FFOptionsViewVideo::~FFOptionsViewVideo()
1595 int FFOptionsViewVideo::handle_event()
1597 char video_format[BCSTRLEN]; video_format[0] = 0;
1598 char video_codec[BCSTRLEN]; video_codec[0] = 0;
1600 Asset *asset = vid_config->asset;
1601 const char *name = asset->vcodec;
1602 if( !FFMPEG::get_format(video_format, "video", name) &&
1603 !FFMPEG::get_codec(video_codec, "video", name) )
1604 codec = avcodec_find_encoder_by_name(video_codec);
1606 eprintf(_("no codec named: %s: %s"), name, video_codec);
1609 vid_config->ff_options_dialog->start(video_format, video_codec, codec,
1610 asset->ff_video_options, sizeof(asset->ff_video_options));