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, (int64_t)*output,
44 (int64_t)-1, (int64_t)25000000, 100, y, 100)
46 this->window = window;
47 this->x = x; this->y = y;
48 this->title_text = title_text;
49 this->output = output;
52 FFMpegConfigNum::~FFMpegConfigNum()
56 void FFMpegConfigNum::create_objects()
58 window->add_subwindow(title = new BC_Title(x, y, title_text));
59 BC_TumbleTextBox::create_objects();
62 int FFMpegConfigNum::handle_event()
64 *output = atol(get_text());
68 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
69 int x, int y, char *title_text, int *output)
70 : FFMpegConfigNum(window, x, y, title_text, output)
74 int FFMpegAudioBitrate::handle_event()
76 int ret = FFMpegAudioNum::handle_event();
80 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
81 int x, int y, char *title_text, int *output)
82 : FFMpegConfigNum(window, x, y, title_text, output)
86 int FFMpegVideoBitrate::handle_event()
88 int ret = FFMpegVideoNum::handle_event();
89 Asset *asset = window()->asset;
90 if( asset->ff_video_bitrate )
91 window()->quality->disable();
93 window()->quality->enable();
97 int FFMpegVideoQuality::handle_event()
99 int ret = FFMpegVideoNum::handle_event();
100 Asset *asset = window()->asset;
101 if( asset->ff_video_quality )
102 window()->bitrate->disable();
104 window()->bitrate->enable();
108 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
109 Asset *asset, BC_WindowBase *&format_window,
110 int audio_options, int video_options)
113 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
114 format_window = window;
115 window->create_objects();
116 if( !window->run_window() )
117 strcpy(asset->ff_audio_options, window->audio_options->get_text());
120 else if(video_options) {
121 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
122 format_window = window;
123 window->create_objects();
124 if( !window->run_window() )
125 strcpy(asset->ff_video_options, window->video_options->get_text());
130 int FileFFMPEG::check_sig(Asset *asset)
132 char *ptr = strstr(asset->path, ".pcm");
134 ptr = strstr(asset->path, ".raw");
138 int ret = !ffmpeg.init_decoder(asset->path) &&
139 !ffmpeg.open_decoder() ? 1 : 0;
143 void FileFFMPEG::get_info(char *path, char *text, int len)
147 cp += sprintf(cp, _("file path: %s\n"), path);
150 if( stat(path, &st) < 0 ) {
151 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
155 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
157 if( !ret ) ret = ffmpeg.init_decoder(path);
158 if( !ret ) ret = ffmpeg.open_decoder();
160 cp += sprintf(cp, _("info:\n"));
161 ffmpeg.info(cp, len-(cp-text));
164 sprintf(cp, _("== open failed\n"));
167 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
168 int &width, int &height, char *title)
171 pid = ff->ff_video_pid(track);
172 framerate = ff->ff_frame_rate(track);
173 width = ff->ff_video_width(track);
174 height = ff->ff_video_height(track);
175 if( title ) *title = 0;
179 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
182 return ff->ff_audio_for_video(vstream, astream, channel_mask);
185 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
187 if( !ff || !asset->video_data ) return 1;
188 asset->width = ff->ff_video_width(vstream);
189 asset->height = ff->ff_video_height(vstream);
190 asset->video_length = ff->ff_video_frames(vstream);
191 if( (asset->video_length = ff->ff_video_frames(vstream)) < 2 )
192 asset->video_length = asset->video_length < 0 ? 0 : -1;
193 asset->frame_rate = ff->ff_frame_rate(vstream);
197 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
199 if( !ff || !asset->audio_data ) return 1;
200 asset->sample_rate = ff->ff_sample_rate(astream);
201 asset->audio_length = ff->ff_audio_samples(astream);
205 int FileFFMPEG::open_file(int rd, int wr)
209 ff = new FFMPEG(this);
212 result = ff->init_decoder(asset->path);
213 if( !result ) result = ff->open_decoder();
215 int audio_channels = ff->ff_total_audio_channels();
216 if( audio_channels > 0 ) {
217 asset->audio_data = 1;
218 asset->channels = audio_channels;
219 asset->sample_rate = ff->ff_sample_rate(0);
220 asset->audio_length = ff->ff_audio_samples(0);
222 int video_layers = ff->ff_total_video_layers();
223 if( video_layers > 0 ) {
224 asset->video_data = 1;
225 if( !asset->layers ) asset->layers = video_layers;
226 asset->actual_width = ff->ff_video_width(0);
227 asset->actual_height = ff->ff_video_height(0);
228 if( !asset->width ) asset->width = asset->actual_width;
229 if( !asset->height ) asset->height = asset->actual_height;
230 if( !asset->video_length &&
231 (asset->video_length = ff->ff_video_frames(0)) < 2 )
232 asset->video_length = asset->video_length < 0 ? 0 : -1;
233 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
235 IndexState *index_state = asset->index_state;
236 index_state->read_markers(file->preferences->index_directory, asset->path);
240 result = ff->init_encoder(asset->path);
241 // must be in this order or dvdauthor will fail
242 if( !result && asset->video_data )
243 result = ff->open_encoder("video", asset->vcodec);
244 if( !result && asset->audio_data )
245 result = ff->open_encoder("audio", asset->acodec);
250 int FileFFMPEG::close_file()
258 int FileFFMPEG::write_samples(double **buffer, int64_t len)
260 if( !ff || len < 0 ) return -1;
262 int ret = ff->encode(stream, buffer, len);
266 int FileFFMPEG::write_frames(VFrame ***frames, int len)
269 int ret = 0, layer = 0;
270 for(int i = 0; i < 1; i++) {
271 for(int j = 0; j < len && !ret; j++) {
272 VFrame *frame = frames[i][j];
273 ret = ff->encode(layer, frame);
280 int FileFFMPEG::read_samples(double *buffer, int64_t len)
282 if( !ff || len < 0 ) return -1;
283 int ch = file->current_channel;
284 int64_t pos = file->current_sample;
285 int ret = ff->decode(ch, pos, buffer, len);
286 if( ret > 0 ) return 0;
287 memset(buffer,0,len*sizeof(*buffer));
291 int FileFFMPEG::read_frame(VFrame *frame)
294 int layer = file->current_layer;
295 int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
296 int ret = ff->decode(layer, pos, frame);
297 frame->set_status(ret);
298 if( ret >= 0 ) return 0;
299 frame->clear_frame();
304 int64_t FileFFMPEG::get_memory_usage()
309 int FileFFMPEG::colormodel_supported(int colormodel)
314 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
319 case PLAYBACK_X11_XV:
320 case PLAYBACK_ASYNCHRONOUS:
322 case PLAYBACK_X11_GL:
324 case PLAYBACK_DV1394:
325 case PLAYBACK_FIREWIRE:
329 case VIDEO4LINUX2JPEG:
330 return BC_COMPRESSED;
332 case VIDEO4LINUX2MPEG:
334 case CAPTURE_JPEG_WEBCAM:
335 return BC_COMPRESSED;
336 case CAPTURE_YUYV_WEBCAM:
338 case CAPTURE_FIREWIRE:
339 case CAPTURE_IEC61883:
348 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
349 : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
350 parent_window->get_abs_cursor_x(1),
351 parent_window->get_abs_cursor_y(1),
354 this->parent_window = parent_window;
360 ff_options_dialog = 0;
363 FFMPEGConfigAudio::~FFMPEGConfigAudio()
365 delete ff_options_dialog;
366 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
368 presets.remove_all_objects();
372 void FFMPEGConfigAudio::create_objects()
375 lock_window("FFMPEGConfigAudio::create_objects");
378 char option_path[BCTEXTLEN];
379 FFMPEG::set_option_path(option_path, "audio");
380 fs.update(option_path);
381 int total_files = fs.total_files();
382 for(int i = 0; i < total_files; i++) {
383 const char *name = fs.get_entry(i)->get_name();
384 if( asset->fformat[0] != 0 ) {
385 const char *ext = strrchr(name,'.');
387 if( strcmp(asset->fformat, ++ext) ) continue;
389 presets.append(new BC_ListBoxItem(name));
392 if( asset->acodec[0] ) {
393 int k = presets.size();
394 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
395 if( k < 0 ) asset->acodec[0] = 0;
398 if( !asset->acodec[0] && presets.size() > 0 )
399 strcpy(asset->acodec, presets[0]->get_text());
401 add_tool(new BC_Title(x, y, _("Preset:")));
403 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
404 preset_popup->create_objects();
407 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
408 bitrate->create_objects();
409 bitrate->set_increment(1000);
411 y += bitrate->get_h() + 10;
412 BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
413 add_subwindow(title);
415 ff_options_dialog = new FFOptionsAudioDialog(this);
416 int x1 = x + title->get_w() + 8;
417 add_subwindow(new FFOptionsViewAudio(this, x1, y, _("view")));
420 if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
421 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
422 FFMPEG::load_options(option_path, asset->ff_audio_options,
423 sizeof(asset->ff_audio_options));
426 audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
427 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
428 audio_options->create_objects();
429 add_subwindow(new BC_OKButton(this));
430 add_subwindow(new BC_CancelButton(this));
433 bitrate->handle_event();
437 int FFMPEGConfigAudio::close_event()
443 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
444 int x, int y, int w, int rows, int size, char *text)
445 : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
447 this->audio_popup = audio_popup;
450 int FFAudioOptions::handle_event()
452 strcpy(audio_popup->asset->ff_audio_options, get_text());
457 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
458 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
463 int FFMPEGConfigAudioPopup::handle_event()
465 strcpy(popup->asset->acodec, get_text());
466 Asset *asset = popup->asset;
467 char option_path[BCTEXTLEN];
468 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
469 FFMPEG::load_options(option_path, asset->ff_audio_options,
470 sizeof(asset->ff_audio_options));
471 popup->audio_options->update(asset->ff_audio_options);
476 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
477 char *title_text, int x, int y, int *output)
478 : BC_CheckBox(x, y, *output, title_text)
481 this->output = output;
483 int FFMPEGConfigAudioToggle::handle_event()
485 *output = get_value();
491 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
492 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
493 parent_window->get_abs_cursor_x(1),
494 parent_window->get_abs_cursor_y(1),
497 this->parent_window = parent_window;
506 FFMPEGConfigVideo::~FFMPEGConfigVideo()
508 delete ff_options_dialog;
509 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
510 if(preset_popup) delete preset_popup;
511 presets.remove_all_objects();
515 void FFMPEGConfigVideo::create_objects()
518 lock_window("FFMPEGConfigVideo::create_objects");
520 add_subwindow(new BC_Title(x, y, _("Compression:")));
524 char option_path[BCTEXTLEN];
525 FFMPEG::set_option_path(option_path, "video");
526 fs.update(option_path);
527 int total_files = fs.total_files();
528 for(int i = 0; i < total_files; i++) {
529 const char *name = fs.get_entry(i)->get_name();
530 if( asset->fformat[0] != 0 ) {
531 const char *ext = strrchr(name,'.');
533 if( strcmp(asset->fformat, ++ext) ) continue;
535 presets.append(new BC_ListBoxItem(name));
538 if( asset->vcodec[0] ) {
539 int k = presets.size();
540 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
541 if( k < 0 ) asset->vcodec[0] = 0;
544 if( !asset->vcodec[0] && presets.size() > 0 )
545 strcpy(asset->vcodec, presets[0]->get_text());
547 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
548 preset_popup->create_objects();
550 if( asset->ff_video_bitrate && asset->ff_video_quality ) {
551 asset->ff_video_bitrate = 0;
552 asset->ff_video_quality = 0;
556 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
557 bitrate->create_objects();
558 bitrate->set_increment(100000);
559 y += bitrate->get_h() + 5;
560 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
561 quality->create_objects();
562 quality->set_increment(1);
563 quality->set_boundaries((int64_t)0, (int64_t)31);
565 y += quality->get_h() + 10;
566 BC_Title *title = new BC_Title(x, y, _("Video Options:"));
567 add_subwindow(title);
569 ff_options_dialog = new FFOptionsVideoDialog(this);
570 int x1 = x + title->get_w() + 8;
571 add_subwindow(new FFOptionsViewVideo(this, x1, y, _("view")));
574 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
575 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
576 FFMPEG::load_options(option_path, asset->ff_video_options,
577 sizeof(asset->ff_video_options));
580 video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
581 sizeof(asset->ff_video_options)-1, asset->ff_video_options);
582 video_options->create_objects();
583 add_subwindow(new BC_OKButton(this));
584 add_subwindow(new BC_CancelButton(this));
587 if( asset->ff_video_bitrate )
589 if( asset->ff_video_quality )
594 int FFMPEGConfigVideo::close_event()
600 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
601 int x, int y, int w, int rows, int size, char *text)
602 : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
604 this->video_popup = video_popup;
607 int FFVideoOptions::handle_event()
609 strcpy(video_popup->asset->ff_video_options, get_text());
614 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
615 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
620 int FFMPEGConfigVideoPopup::handle_event()
622 strcpy(popup->asset->vcodec, get_text());
623 Asset *asset = popup->asset;
624 char option_path[BCTEXTLEN];
625 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
626 FFMPEG::load_options(option_path, asset->ff_video_options,
627 sizeof(asset->ff_video_options));
628 popup->video_options->update(asset->ff_video_options);
633 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
634 char *title_text, int x, int y, int *output)
635 : BC_CheckBox(x, y, *output, title_text)
638 this->output = output;
640 int FFMPEGConfigVideoToggle::handle_event()
642 *output = get_value();
646 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
647 const char *title, int64_t length, int64_t *position, int *canceled)
650 this->index_file = index_file;
651 this->progress_bar = progress_bar;
652 strcpy(this->progress_title, title);
653 this->length = length;
654 this->position = position;
655 this->canceled = canceled;
660 FFMPEGScanProgress::~FFMPEGScanProgress()
667 void FFMPEGScanProgress::run()
670 if( progress_bar->update(*position) ) {
671 *canceled = done = 1;
674 index_file->redraw_edits(0);
679 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
682 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
684 IndexState *index_state = index_file->get_state();
685 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
686 index_state->reset_index();
687 index_state->reset_markers();
688 index_state->index_status = INDEX_BUILDING;
690 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
691 FFAudioStream *aud = ff->ffaudio[aidx];
692 index_state->add_audio_stream(aud->channels, aud->length);
696 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
697 char *index_path = index_file->index_filename;
700 int64_t scan_position = 0;
701 FFMPEGScanProgress *scan_progress = 0;
703 char progress_title[BCTEXTLEN];
704 sprintf(progress_title, _("Creating %s\n"), index_path);
705 progress_bar->update_title(progress_title, 1);
706 progress_bar->update_length(file_bytes);
707 scan_progress = new FFMPEGScanProgress(index_file,
708 progress_bar, progress_title, file_bytes,
709 &scan_position, &canceled);
712 index_state->index_bytes = file_bytes;
713 index_state->init_scan(file->preferences->index_size);
715 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
716 index_state->reset_index();
717 index_state->reset_markers();
721 delete scan_progress;
722 if( canceled ) return 1;
724 index_state->marker_status = MARKERS_READY;
725 return index_state->create_index(index_path, asset);
730 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
731 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
734 update(); // init col/wid/columns
738 ~FFOptions_OptPanel()
742 void FFOptions_OptPanel::create_objects()
744 const char *cols[] = { _("option"), _("value"), };
745 const int col1_w = 150;
746 int wids[] = { col1_w, get_w()-col1_w };
747 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
750 int FFOptions_OptPanel::update()
754 FFOptions &options = fwin->options;
755 for( int i=0; i<options.size(); ++i ) {
756 FFOptions_Opt *opt = options[i];
757 opts.append(opt->item_name);
758 vals.append(opt->item_value);
764 int FFOptions_OptPanel::cursor_leave_event()
771 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
777 FFOptions_OptName::~FFOptions_OptName()
781 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
787 void FFOptions_OptValue::update()
790 char val[BCTEXTLEN]; val[0] = 0;
791 opt->get(val, sizeof(val));
795 void FFOptions_OptValue::update(const char *v)
800 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
802 this->options = options;
804 item_name = new FFOptions_OptName(this, nm);
805 item_value = new FFOptions_OptValue(this);
808 FFOptions_Opt::~FFOptions_Opt()
814 char *FFOptions_Opt::get(char *vp, int sz)
818 if( !opt ) return cp;
820 void *obj = (void *)options->obj;
822 if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
823 const char *val = (const char *)bp;
824 if( opt->unit && *val ) {
826 const char *uid = unit_name(id);
829 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
830 if( sz > 0 ) vp[sz-1] = 0;
837 void FFOptions_Opt::set(const char *val)
839 void *obj = (void *)options->obj;
840 if( !obj || !opt ) return;
841 av_opt_set(obj, opt->name, val, 0);
845 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
852 FFOptionsKindItem::~FFOptionsKindItem()
856 int FFOptionsKindItem::handle_event()
858 FFOptionsWindow *fwin = kind->fwin;
859 FFOptions &options = fwin->options;
860 options.initialize(fwin, idx);
865 const char *FFOptionsKind::kinds[] = {
866 N_("codec"), // FF_KIND_CODEC
867 N_("ffmpeg"), // FF_KIND_FFMPEG
871 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
872 : BC_PopupMenu(x, y, w-calculate_w(0), "")
882 void FFOptionsKind::create_objects()
884 for( int i=0; i<(int)(sizeof(kinds)/sizeof(kinds[0])); ++i )
885 add_item(new FFOptionsKindItem(this, _(kinds[i]), i));
888 int FFOptionsKind::handle_event()
893 void FFOptionsKind::set(int k)
896 set_text(_(kinds[k]));
900 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
901 : BC_TextBox(x, y, w, 1, (char *)"")
911 int FFOptionsText::handle_event()
917 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
918 : BC_PopupMenu(x, y, w, "")
928 int FFOptionsUnits::handle_event()
930 const char *text = get_text();
931 if( text && fwin->selected ) {
932 fwin->selected->set(text);
933 fwin->selected->item_value->update();
934 av_dict_set(&fwin->dialog->ff_opts,
935 fwin->selected->item_name->get_text(),
936 fwin->selected->item_value->get_text(), 0);
943 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
944 : BC_GenericButton(x, y, _("Apply"))
954 int FFOptionsApply::handle_event()
956 const char *text = fwin->text->get_text();
957 if( text && fwin->selected ) {
958 fwin->selected->set(text);
959 fwin->selected->item_value->update();
960 av_dict_set(&fwin->dialog->ff_opts,
961 fwin->selected->item_name->get_text(),
962 fwin->selected->item_value->get_text(), 0);
968 FFOptions::FFOptions()
974 FFOptions::~FFOptions()
976 remove_all_objects();
977 avcodec_free_context(&avctx);
980 int FFOptions::cmpr(const void *a, const void *b)
982 FFOptions_Opt *ap = *(FFOptions_Opt **)a;
983 FFOptions_Opt *bp = *(FFOptions_Opt **)b;
984 const char *vap = ap->item_name->get_text();
985 const char *vbp = bp->item_name->get_text();
986 return strcmp(vap, vbp);
989 void FFOptions::initialize(FFOptionsWindow *win, int kind)
991 remove_all_objects();
996 avctx = avcodec_alloc_context3(win->dialog->codec);
1000 obj = (const void *)avctx->priv_data;
1002 case FF_KIND_FFMPEG:
1003 obj = (const void *)avctx;
1008 FFOptions &conf = *this;
1009 const AVOption *opt = 0;
1010 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1011 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1013 for( int i=0; !dupl && i<size(); ++i ) {
1014 FFOptions_Opt *fopt = conf[i];
1015 const AVOption *op = fopt->opt;
1016 if( op->offset != opt->offset ) continue;
1017 if( op->type != opt->type ) continue;
1019 if( strlen(op->name) < strlen(opt->name) )
1022 if( dupl ) continue;
1023 FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1025 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1026 fopt->item_value->update(vp);
1030 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1031 win->kind->set(kind);
1032 win->panel->update();
1033 win->panel->set_yposition(0);
1036 int FFOptions::update()
1039 FFOptions &conf = *this;
1041 for( int i=0; i<size(); ++i ) {
1042 FFOptions_Opt *fopt = conf[i];
1043 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1044 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1045 fopt->item_value->update(val);
1051 void FFOptions::dump(FILE *fp)
1054 const AVOption *opt = 0;
1055 FFOptions &conf = *this;
1057 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1058 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1060 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1061 if( k < 0 ) continue;
1062 FFOptions_Opt *fopt = conf[k];
1063 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1064 fprintf(fp, " %s:=%s", opt->name, vp);
1066 char unt[BCTEXTLEN], *up = unt;
1068 fprintf(fp, "%s", unt);
1075 void FFOptionsWindow::update(FFOptions_Opt *opt)
1077 if( selected != opt ) {
1078 if( selected ) selected->item_name->set_selected(0);
1080 if( selected ) selected->item_name->set_selected(1);
1082 clear_box(0,0, 0,panel->get_y());
1083 char str[BCTEXTLEN], *sp;
1085 if( opt ) opt->types(sp);
1088 if( opt ) opt->ranges(sp);
1090 while( units->total_items() ) units->del_item(0);
1091 char unit[BCSTRLEN]; strcpy(unit, "()");
1092 if( opt && opt->opt ) {
1093 ArrayList<const char *> names;
1095 if( opt->opt->unit ) {
1096 n = opt->units(names);
1097 if( n > 0 ) strcpy(unit,opt->opt->unit);
1099 for( int i=0; i<n; ++i )
1100 units->add_item(new BC_MenuItem(names[i], 0));
1102 units->set_text(unit);
1103 char val[BCTEXTLEN]; val[0] = 0;
1104 if( opt ) opt->get(val, sizeof(val));
1110 void FFOptions_OptPanel::show_tip(const char *tip)
1113 int len = strlen(tip);
1114 if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1115 strncpy(tip_text,tip,len);
1117 int line_limit = 60;
1118 int limit2 = line_limit/2;
1119 int limit4 = line_limit/4-2;
1120 char *cp = tip_text, *dp = cp+len;
1121 int n; char *bp, *ep, *pp, *sp;
1123 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1124 // target about half remaining line, constrain line_limit
1125 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1127 // search for last punct, last space before line_limit
1128 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1129 if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1130 else if( isspace(*cp) ) sp = cp;
1134 // first, after punctuation
1135 if( pp && pp-bp >= limit4 )
1141 // last, on next space
1143 while( cp<dp && !isspace(*cp) ) ++cp;
1150 fwin->panel->set_tooltip(tip_text);
1151 fwin->panel->show_tooltip();
1154 int FFOptions_OptPanel::selection_changed()
1156 FFOptions_Opt *opt = 0;
1157 BC_ListBoxItem *item = get_selection(0, 0);
1159 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1160 opt = opt_name->opt;
1163 if( opt ) show_tip(opt->tip());
1168 int FFOptions_Opt::types(char *rp)
1170 const char *cp = "";
1171 if( opt ) switch (opt->type) {
1172 case AV_OPT_TYPE_FLAGS: cp = N_("<flags>"); break;
1173 case AV_OPT_TYPE_INT: cp = N_("<int>"); break;
1174 case AV_OPT_TYPE_INT64: cp = N_("<int64>"); break;
1175 case AV_OPT_TYPE_DOUBLE: cp = N_("<double>"); break;
1176 case AV_OPT_TYPE_FLOAT: cp = N_("<float>"); break;
1177 case AV_OPT_TYPE_STRING: cp = N_("<string>"); break;
1178 case AV_OPT_TYPE_RATIONAL: cp = N_("<rational>"); break;
1179 case AV_OPT_TYPE_BINARY: cp = N_("<binary>"); break;
1180 case AV_OPT_TYPE_IMAGE_SIZE: cp = N_("<image_size>"); break;
1181 case AV_OPT_TYPE_VIDEO_RATE: cp = N_("<video_rate>"); break;
1182 case AV_OPT_TYPE_PIXEL_FMT: cp = N_("<pix_fmt>"); break;
1183 case AV_OPT_TYPE_SAMPLE_FMT: cp = N_("<sample_fmt>"); break;
1184 case AV_OPT_TYPE_DURATION: cp = N_("<duration>"); break;
1185 case AV_OPT_TYPE_COLOR: cp = N_("<color>"); break;
1186 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = N_("<channel_layout>"); break;
1187 case AV_OPT_TYPE_BOOL: cp = N_("<bool>"); break;
1188 default: cp = N_("<undef>"); break;
1190 return sprintf(rp, "%s", _(cp));
1192 int FFOptions_Opt::scalar(double d, char *rp)
1195 if( d == INT_MAX ) cp = "INT_MAX";
1196 else if( d == INT_MIN ) cp = "INT_MIN";
1197 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1198 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1199 else if( d == INT64_MIN ) cp = "I64_MIN";
1200 else if( d == FLT_MAX ) cp = "FLT_MAX";
1201 else if( d == FLT_MIN ) cp = "FLT_MIN";
1202 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1203 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1204 else if( d == DBL_MAX ) cp = "DBL_MAX";
1205 else if( d == DBL_MIN ) cp = "DBL_MIN";
1206 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1207 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1208 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1209 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1210 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1211 else return sprintf(rp, "%g", d);
1212 return sprintf(rp, "%s", cp);
1215 int FFOptions_Opt::ranges(char *rp)
1217 if( !opt ) return 0;
1218 void *obj = (void *)options->obj;
1219 if( !obj ) return 0;
1221 switch (opt->type) {
1222 case AV_OPT_TYPE_INT:
1223 case AV_OPT_TYPE_INT64:
1224 case AV_OPT_TYPE_DOUBLE:
1225 case AV_OPT_TYPE_FLOAT: break;
1228 AVOptionRanges *r = 0;
1230 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1231 for( int i=0; i<r->nb_ranges; ++i ) {
1232 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
1233 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
1234 cp += sprintf(cp, ")");
1236 av_opt_freep_ranges(&r);
1240 int FFOptions_Opt::units(ArrayList<const char *> &names)
1242 if( !opt || !opt->unit ) return 0;
1243 const void *obj = options->obj;
1244 if( !obj ) return 0;
1246 ArrayList<const AVOption *> opts;
1247 const AVOption *opt = NULL;
1248 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1249 if( !opt->unit ) continue;
1250 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1251 if( strcmp(this->opt->unit, opt->unit) ) continue;
1252 int i = opts.size();
1254 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1255 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1258 if( i >= 0 ) continue;
1262 for( int i=0; i<opts.size(); ++i )
1263 names.append(opts[i]->name);
1265 return names.size();
1268 int FFOptions_Opt::units(char *rp)
1270 ArrayList<const char *> names;
1271 int n = units(names);
1274 cp += sprintf(cp, " [%s:", this->opt->unit);
1275 for( int i=0; i<n; ++i )
1276 cp += sprintf(cp, " %s", names[i]);
1277 cp += sprintf(cp, "]:");
1281 const char *FFOptions_Opt::unit_name(int id)
1283 if( !opt || !opt->unit ) return 0;
1284 const void *obj = options->obj;
1285 if( !obj ) return 0;
1287 const char *ret = 0;
1288 const AVOption *opt = NULL;
1289 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1290 if( !opt->unit ) continue;
1291 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1292 if( strcmp(this->opt->unit, opt->unit) ) continue;
1293 if( opt->default_val.i64 != id ) continue;
1294 if( !ret ) { ret = opt->name; continue; }
1295 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1301 const char *FFOptions_Opt::tip()
1303 return !opt ? 0 : opt->help;
1307 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog)
1308 : BC_Window(_(PROGRAM_NAME ": Options"), 60, 30, 640, 400)
1310 this->dialog = dialog;
1314 FFOptionsWindow::~FFOptionsWindow()
1318 void FFOptionsWindow::create_objects()
1321 int x0 = 10, y0 = 10;
1323 add_subwindow(title = new BC_Title(x, y, _("Format: ")));
1324 x += title->get_w();
1325 add_subwindow(new BC_Title(x, y, dialog->format_name));
1327 add_subwindow(title = new BC_Title(x, y, _("Codec: ")));
1328 x += title->get_w();
1329 add_subwindow(new BC_Title(x, y, dialog->codec_name));
1331 x = x0; y += title->get_h() + 10; y0 = y;
1332 add_subwindow(title = new BC_Title(x, y, _("Type: ")));
1333 x += title->get_w() + 8;
1334 add_subwindow(type = new BC_Title(x, y, (char *)""));
1336 add_subwindow(title = new BC_Title(x, y, _("Range: ")));
1337 x += title->get_w() + 8;
1338 add_subwindow(range = new BC_Title(x, y, (char *)""));
1340 x = x0; y += title->get_h() + 10;
1341 add_subwindow(units = new FFOptionsUnits(this, x, y, 120));
1342 x += units->get_w() + 8;
1343 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
1344 add_subwindow(text = new FFOptionsText(this, x, y, x1-x - 8));
1345 add_subwindow(apply = new FFOptionsApply(this, x1, y));
1346 y += units->get_h() + 10;
1347 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1348 kind->create_objects();
1349 const char *kind_text = _("Kind:");
1350 x1 -= BC_Title::calculate_w(this, kind_text) + 8;
1351 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1354 panel_x = x0; panel_y = y0;
1355 panel_w = get_w()-10 - panel_x;
1356 panel_h = get_h()-10 - panel_y - BC_OKButton::calculate_h();
1357 panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1358 add_subwindow(panel);
1359 add_subwindow(new BC_OKButton(this));
1360 add_subwindow(new BC_CancelButton(this));
1361 panel->create_objects();
1362 options.initialize(this, FF_KIND_CODEC);
1367 void FFOptionsWindow::draw()
1372 int FFOptionsWindow::resize_event(int w, int h)
1374 int x1 = w - 8 - kind->get_w();
1375 int y = kind->get_y();
1376 kind->reposition_window(x1, y);
1377 x1 -= kind_title->get_w() + 8;
1378 kind_title->reposition_window(x1,y);
1379 x1 = get_w() - apply->get_w() - 8;
1380 int y1 = units->get_y();
1381 apply->reposition_window(x1, y1);
1382 int x0 = units->get_x() + units->get_w() + 8;
1383 int y0 = units->get_y();
1384 text->reposition_window(x0,y0, x1-x0-8);
1385 panel_w = get_w()-10 - panel_x;
1386 panel_h = get_h()-10 - panel_y;
1387 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1391 FFOptionsDialog::FFOptionsDialog()
1394 this->options_window = 0;
1395 this->codec_name = 0;
1401 FFOptionsDialog::~FFOptionsDialog()
1404 delete [] codec_name;
1407 void FFOptionsDialog::load_options(const char *bp, int len)
1409 char line[BCTEXTLEN];
1410 char key[BCSTRLEN], val[BCTEXTLEN];
1411 const char *dp = bp + len-1;
1413 while( bp < dp && *bp != 0 ) {
1415 char *cp = line, *ep = cp+sizeof(line)-1;
1416 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1418 if( line[0] == '#' ) {
1419 sprintf(key,"#%d", no);
1420 av_dict_set(&ff_opts, key, line, 0);
1423 if( line[0] == '\n' ) continue;
1424 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1425 av_dict_set(&ff_opts, key, val, 0);
1429 void FFOptionsDialog::store_options(char *cp, int len)
1431 char *ep = cp + len-1;
1432 AVDictionaryEntry *elem = 0;
1433 while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1434 if( elem->key[0] == '#' ) {
1435 cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1438 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1443 void FFOptionsDialog::start(const char *format_name, const char *codec_name,
1444 AVCodec *codec, const char *options, int len)
1446 if( options_window ) {
1447 options_window->lock_window("FFOptionsDialog::start");
1448 options_window->raise_window();
1449 options_window->unlock_window();
1453 this->format_name = cstrdup(format_name);
1454 this->codec_name = cstrdup(codec_name);
1455 this->codec = codec;
1458 load_options(options, len);
1460 BC_DialogThread::start();
1463 BC_Window* FFOptionsDialog::new_gui()
1465 options_window = new FFOptionsWindow(this);
1466 options_window->create_objects();
1467 return options_window;
1470 void FFOptionsDialog::handle_done_event(int result)
1473 char options[ff_len];
1474 store_options(options, ff_len);
1475 update_options(options);
1478 delete [] format_name; format_name = 0;
1479 delete [] codec_name; codec_name = 0;
1480 av_dict_free(&ff_opts);
1483 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1485 this->aud_config = aud_config;
1488 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1493 void FFOptionsAudioDialog::update_options(const char *options)
1495 aud_config->audio_options->update(options);
1498 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1500 this->vid_config = vid_config;
1503 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1508 void FFOptionsVideoDialog::update_options(const char *options)
1510 vid_config->video_options->update(options);
1514 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text)
1515 : BC_GenericButton(x, y, text)
1517 this->aud_config = aud_config;
1520 FFOptionsViewAudio::~FFOptionsViewAudio()
1524 int FFOptionsViewAudio::handle_event()
1526 char audio_format[BCSTRLEN]; audio_format[0] = 0;
1527 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1529 Asset *asset = aud_config->asset;
1530 const char *name = asset->acodec;
1531 if( !FFMPEG::get_format(audio_format, "audio", name) &&
1532 !FFMPEG::get_codec(audio_codec, "audio", name) )
1533 codec = avcodec_find_encoder_by_name(audio_codec);
1535 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1538 aud_config->ff_options_dialog->start(audio_format, audio_codec, codec,
1539 asset->ff_audio_options, sizeof(asset->ff_audio_options));
1543 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text)
1544 : BC_GenericButton(x, y, text)
1546 this->vid_config = vid_config;
1549 FFOptionsViewVideo::~FFOptionsViewVideo()
1553 int FFOptionsViewVideo::handle_event()
1555 char video_format[BCSTRLEN]; video_format[0] = 0;
1556 char video_codec[BCSTRLEN]; video_codec[0] = 0;
1558 Asset *asset = vid_config->asset;
1559 const char *name = asset->vcodec;
1560 if( !FFMPEG::get_format(video_format, "video", name) &&
1561 !FFMPEG::get_codec(video_codec, "video", name) )
1562 codec = avcodec_find_encoder_by_name(video_codec);
1564 eprintf(_("no codec named: %s: %s"), name, video_codec);
1567 vid_config->ff_options_dialog->start(video_format, video_codec, codec,
1568 asset->ff_video_options, sizeof(asset->ff_video_options));