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"
20 #include "mainerror.h"
21 #include "mainprogress.h"
23 #include "preferences.h"
24 #include "videodevice.inc"
26 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
27 : FileBase(asset, file)
30 if(asset->format == FILE_UNKNOWN)
31 asset->format = FILE_FFMPEG;
34 FileFFMPEG::~FileFFMPEG()
40 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
41 int x, int y, char *title_text, int *output)
42 : BC_TumbleTextBox(window, (int64_t)*output,
43 (int64_t)-1, (int64_t)25000000, 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::handle_event()
63 *output = atol(get_text());
67 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
68 int x, int y, char *title_text, int *output)
69 : FFMpegConfigNum(window, x, y, title_text, output)
73 int FFMpegAudioBitrate::handle_event()
75 int ret = FFMpegAudioNum::handle_event();
79 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
80 int x, int y, char *title_text, int *output)
81 : FFMpegConfigNum(window, x, y, title_text, output)
85 int FFMpegVideoBitrate::handle_event()
87 int ret = FFMpegVideoNum::handle_event();
88 Asset *asset = window()->asset;
89 if( asset->ff_video_bitrate )
90 window()->quality->disable();
92 window()->quality->enable();
96 int FFMpegVideoQuality::handle_event()
98 int ret = FFMpegVideoNum::handle_event();
99 Asset *asset = window()->asset;
100 if( asset->ff_video_quality )
101 window()->bitrate->disable();
103 window()->bitrate->enable();
107 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
108 Asset *asset, BC_WindowBase *&format_window,
109 int audio_options, int video_options)
112 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
113 format_window = window;
114 window->create_objects();
115 window->run_window();
118 else if(video_options) {
119 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
120 format_window = window;
121 window->create_objects();
122 window->run_window();
127 int FileFFMPEG::check_sig(Asset *asset)
129 char *ptr = strstr(asset->path, ".pcm");
131 ptr = strstr(asset->path, ".raw");
135 int ret = !ffmpeg.init_decoder(asset->path) &&
136 !ffmpeg.open_decoder() ? 1 : 0;
140 void FileFFMPEG::get_info(char *path, char *text, int len)
144 cp += sprintf(cp, _("file path: %s\n"), path);
147 if( stat(path, &st) < 0 ) {
148 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
152 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
154 if( !ret ) ret = ffmpeg.init_decoder(path);
155 if( !ret ) ret = ffmpeg.open_decoder();
157 cp += sprintf(cp, _("info:\n"));
158 ffmpeg.info(cp, len-(cp-text));
161 sprintf(cp, _("== open failed\n"));
164 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
165 int &width, int &height, char *title)
168 pid = ff->ff_video_pid(track);
169 framerate = ff->ff_frame_rate(track);
170 width = ff->ff_video_width(track);
171 height = ff->ff_video_height(track);
172 if( title ) *title = 0;
176 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
179 return ff->ff_audio_for_video(vstream, astream, channel_mask);
182 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
184 if( !ff || !asset->video_data ) return 1;
185 asset->width = ff->ff_video_width(vstream);
186 asset->height = ff->ff_video_height(vstream);
187 asset->video_length = ff->ff_video_frames(vstream);
188 if( (asset->video_length = ff->ff_video_frames(vstream)) < 2 )
189 asset->video_length = asset->video_length < 0 ? 0 : -1;
190 asset->frame_rate = ff->ff_frame_rate(vstream);
194 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
196 if( !ff || !asset->audio_data ) return 1;
197 asset->sample_rate = ff->ff_sample_rate(astream);
198 asset->audio_length = ff->ff_audio_samples(astream);
202 int FileFFMPEG::open_file(int rd, int wr)
206 ff = new FFMPEG(this);
209 result = ff->init_decoder(asset->path);
210 if( !result ) result = ff->open_decoder();
212 int audio_channels = ff->ff_total_audio_channels();
213 if( audio_channels > 0 ) {
214 asset->audio_data = 1;
215 asset->channels = audio_channels;
216 asset->sample_rate = ff->ff_sample_rate(0);
217 asset->audio_length = ff->ff_audio_samples(0);
219 int video_layers = ff->ff_total_video_layers();
220 if( video_layers > 0 ) {
221 asset->video_data = 1;
222 if( !asset->layers ) asset->layers = video_layers;
223 asset->actual_width = ff->ff_video_width(0);
224 asset->actual_height = ff->ff_video_height(0);
225 if( !asset->width ) asset->width = asset->actual_width;
226 if( !asset->height ) asset->height = asset->actual_height;
227 if( !asset->video_length &&
228 (asset->video_length = ff->ff_video_frames(0)) < 2 )
229 asset->video_length = asset->video_length < 0 ? 0 : -1;
230 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
232 IndexState *index_state = asset->index_state;
233 index_state->read_markers(file->preferences->index_directory, asset->path);
237 result = ff->init_encoder(asset->path);
238 // must be in this order or dvdauthor will fail
239 if( !result && asset->video_data )
240 result = ff->open_encoder("video", asset->vcodec);
241 if( !result && asset->audio_data )
242 result = ff->open_encoder("audio", asset->acodec);
247 int FileFFMPEG::close_file()
255 int FileFFMPEG::write_samples(double **buffer, int64_t len)
257 if( !ff || len < 0 ) return -1;
259 int ret = ff->encode(stream, buffer, len);
263 int FileFFMPEG::write_frames(VFrame ***frames, int len)
266 int ret = 0, layer = 0;
267 for(int i = 0; i < 1; i++) {
268 for(int j = 0; j < len && !ret; j++) {
269 VFrame *frame = frames[i][j];
270 ret = ff->encode(layer, frame);
277 int FileFFMPEG::read_samples(double *buffer, int64_t len)
279 if( !ff || len < 0 ) return -1;
280 int ch = file->current_channel;
281 int64_t pos = file->current_sample;
282 int ret = ff->decode(ch, pos, buffer, len);
283 if( ret > 0 ) return 0;
284 memset(buffer,0,len*sizeof(*buffer));
288 int FileFFMPEG::read_frame(VFrame *frame)
291 int layer = file->current_layer;
292 int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
293 int ret = ff->decode(layer, pos, frame);
294 frame->set_status(ret);
295 if( ret >= 0 ) return 0;
296 frame->clear_frame();
301 int64_t FileFFMPEG::get_memory_usage()
306 int FileFFMPEG::colormodel_supported(int colormodel)
311 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
314 case PLAYBACK_X11: return BC_RGB888;
315 case PLAYBACK_X11_GL: return BC_YUV888;
322 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
323 : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
324 parent_window->get_abs_cursor_x(1),
325 parent_window->get_abs_cursor_y(1),
328 this->parent_window = parent_window;
334 ff_options_dialog = 0;
337 FFMPEGConfigAudio::~FFMPEGConfigAudio()
339 delete ff_options_dialog;
340 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
342 presets.remove_all_objects();
346 void FFMPEGConfigAudio::create_objects()
349 lock_window("FFMPEGConfigAudio::create_objects");
352 char option_path[BCTEXTLEN];
353 FFMPEG::set_option_path(option_path, "audio");
354 fs.update(option_path);
355 int total_files = fs.total_files();
356 for(int i = 0; i < total_files; i++) {
357 const char *name = fs.get_entry(i)->get_name();
358 if( asset->fformat[0] != 0 ) {
359 const char *ext = strrchr(name,'.');
361 if( strcmp(asset->fformat, ++ext) ) continue;
363 presets.append(new BC_ListBoxItem(name));
366 if( asset->acodec[0] ) {
367 int k = presets.size();
368 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
369 if( k < 0 ) asset->acodec[0] = 0;
372 if( !asset->acodec[0] && presets.size() > 0 )
373 strcpy(asset->acodec, presets[0]->get_text());
375 add_tool(new BC_Title(x, y, _("Preset:")));
377 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
378 preset_popup->create_objects();
381 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
382 bitrate->create_objects();
383 bitrate->set_increment(1000);
385 y += bitrate->get_h() + 10;
386 BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
387 add_subwindow(title);
389 ff_options_dialog = new FFOptionsAudioDialog(this);
390 int x1 = x + title->get_w() + 8;
391 add_subwindow(new FFOptionsViewAudio(this, x1, y, _("view")));
394 if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
395 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
396 FFMPEG::load_options(option_path, asset->ff_audio_options,
397 sizeof(asset->ff_audio_options));
400 audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
401 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
402 audio_options->create_objects();
403 add_subwindow(new BC_OKButton(this));
404 add_subwindow(new BC_CancelButton(this));
407 bitrate->handle_event();
411 int FFMPEGConfigAudio::close_event()
417 void FFMPEGConfigAudio::update_options()
419 audio_options->update(asset->ff_audio_options);
422 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
423 int x, int y, int w, int rows, int size, char *text)
424 : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
426 this->audio_popup = audio_popup;
429 int FFAudioOptions::handle_event()
431 strcpy(audio_popup->asset->ff_audio_options, get_text());
436 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
437 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
442 int FFMPEGConfigAudioPopup::handle_event()
444 strcpy(popup->asset->acodec, get_text());
445 Asset *asset = popup->asset;
446 char option_path[BCTEXTLEN];
447 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
448 FFMPEG::load_options(option_path, asset->ff_audio_options,
449 sizeof(asset->ff_audio_options));
450 popup->audio_options->update(asset->ff_audio_options);
455 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
456 char *title_text, int x, int y, int *output)
457 : BC_CheckBox(x, y, *output, title_text)
460 this->output = output;
462 int FFMPEGConfigAudioToggle::handle_event()
464 *output = get_value();
470 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
471 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
472 parent_window->get_abs_cursor_x(1),
473 parent_window->get_abs_cursor_y(1),
476 this->parent_window = parent_window;
485 FFMPEGConfigVideo::~FFMPEGConfigVideo()
487 delete ff_options_dialog;
488 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
489 if(preset_popup) delete preset_popup;
490 presets.remove_all_objects();
494 void FFMPEGConfigVideo::create_objects()
497 lock_window("FFMPEGConfigVideo::create_objects");
499 add_subwindow(new BC_Title(x, y, _("Compression:")));
503 char option_path[BCTEXTLEN];
504 FFMPEG::set_option_path(option_path, "video");
505 fs.update(option_path);
506 int total_files = fs.total_files();
507 for(int i = 0; i < total_files; i++) {
508 const char *name = fs.get_entry(i)->get_name();
509 if( asset->fformat[0] != 0 ) {
510 const char *ext = strrchr(name,'.');
512 if( strcmp(asset->fformat, ++ext) ) continue;
514 presets.append(new BC_ListBoxItem(name));
517 if( asset->vcodec[0] ) {
518 int k = presets.size();
519 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
520 if( k < 0 ) asset->vcodec[0] = 0;
523 if( !asset->vcodec[0] && presets.size() > 0 )
524 strcpy(asset->vcodec, presets[0]->get_text());
526 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
527 preset_popup->create_objects();
529 if( asset->ff_video_bitrate && asset->ff_video_quality ) {
530 asset->ff_video_bitrate = 0;
531 asset->ff_video_quality = 0;
535 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
536 bitrate->create_objects();
537 bitrate->set_increment(100000);
538 y += bitrate->get_h() + 5;
539 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
540 quality->create_objects();
541 quality->set_increment(1);
542 quality->set_boundaries((int64_t)0, (int64_t)31);
544 y += quality->get_h() + 10;
545 BC_Title *title = new BC_Title(x, y, _("Video Options:"));
546 add_subwindow(title);
548 ff_options_dialog = new FFOptionsVideoDialog(this);
549 int x1 = x + title->get_w() + 8;
550 add_subwindow(new FFOptionsViewVideo(this, x1, y, _("view")));
553 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
554 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
555 FFMPEG::load_options(option_path, asset->ff_video_options,
556 sizeof(asset->ff_video_options));
559 video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
560 sizeof(asset->ff_video_options)-1, asset->ff_video_options);
561 video_options->create_objects();
562 add_subwindow(new BC_OKButton(this));
563 add_subwindow(new BC_CancelButton(this));
566 if( asset->ff_video_bitrate )
568 if( asset->ff_video_quality )
573 int FFMPEGConfigVideo::close_event()
579 void FFMPEGConfigVideo::update_options()
581 video_options->update(asset->ff_video_options);
584 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
585 int x, int y, int w, int rows, int size, char *text)
586 : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
588 this->video_popup = video_popup;
591 int FFVideoOptions::handle_event()
593 strcpy(video_popup->asset->ff_video_options, get_text());
598 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
599 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
604 int FFMPEGConfigVideoPopup::handle_event()
606 strcpy(popup->asset->vcodec, get_text());
607 Asset *asset = popup->asset;
608 char option_path[BCTEXTLEN];
609 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
610 FFMPEG::load_options(option_path, asset->ff_video_options,
611 sizeof(asset->ff_video_options));
612 popup->video_options->update(asset->ff_video_options);
617 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
618 char *title_text, int x, int y, int *output)
619 : BC_CheckBox(x, y, *output, title_text)
622 this->output = output;
624 int FFMPEGConfigVideoToggle::handle_event()
626 *output = get_value();
630 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
631 const char *title, int64_t length, int64_t *position, int *canceled)
634 this->index_file = index_file;
635 this->progress_bar = progress_bar;
636 strcpy(this->progress_title, title);
637 this->length = length;
638 this->position = position;
639 this->canceled = canceled;
644 FFMPEGScanProgress::~FFMPEGScanProgress()
651 void FFMPEGScanProgress::run()
654 if( progress_bar->update(*position) ) {
655 *canceled = done = 1;
658 index_file->redraw_edits(0);
663 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
666 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
668 IndexState *index_state = index_file->get_state();
669 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
670 index_state->reset_index();
671 index_state->reset_markers();
672 index_state->index_status = INDEX_BUILDING;
674 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
675 FFAudioStream *aud = ff->ffaudio[aidx];
676 index_state->add_audio_stream(aud->channels, aud->length);
680 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
681 char *index_path = index_file->index_filename;
684 int64_t scan_position = 0;
685 FFMPEGScanProgress *scan_progress = 0;
687 char progress_title[BCTEXTLEN];
688 sprintf(progress_title, _("Creating %s\n"), index_path);
689 progress_bar->update_title(progress_title, 1);
690 progress_bar->update_length(file_bytes);
691 scan_progress = new FFMPEGScanProgress(index_file,
692 progress_bar, progress_title, file_bytes,
693 &scan_position, &canceled);
696 index_state->index_bytes = file_bytes;
697 index_state->init_scan(file->preferences->index_size);
699 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
700 index_state->reset_index();
701 index_state->reset_markers();
705 delete scan_progress;
706 if( canceled ) return 1;
708 index_state->marker_status = MARKERS_READY;
709 return index_state->create_index(index_path, asset);
714 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
715 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
718 update(); // init col/wid/columns
722 ~FFOptions_OptPanel()
726 void FFOptions_OptPanel::create_objects()
728 const char *cols[] = { "option", "value", };
729 const int col1_w = 150;
730 int wids[] = { col1_w, get_w()-col1_w };
731 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
734 int FFOptions_OptPanel::update()
738 FFOptions &options = fwin->options;
739 for( int i=0; i<options.size(); ++i ) {
740 FFOptions_Opt *opt = options[i];
741 opts.append(opt->item_name);
742 vals.append(opt->item_value);
748 int FFOptions_OptPanel::cursor_leave_event()
755 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
761 FFOptions_OptName::~FFOptions_OptName()
765 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
771 void FFOptions_OptValue::update()
774 char val[BCTEXTLEN]; val[0] = 0;
775 opt->get(val, sizeof(val));
779 void FFOptions_OptValue::update(const char *v)
784 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
786 this->options = options;
788 item_name = new FFOptions_OptName(this, nm);
789 item_value = new FFOptions_OptValue(this);
792 FFOptions_Opt::~FFOptions_Opt()
798 char *FFOptions_Opt::get(char *vp, int sz)
802 if( !opt ) return cp;
804 void *obj = (void *)options->obj;
806 if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
807 const char *val = (const char *)bp;
808 if( opt->unit && *val ) {
810 const char *uid = unit_name(id);
813 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
814 if( sz > 0 ) vp[sz-1] = 0;
821 void FFOptions_Opt::set(const char *val)
823 void *obj = (void *)options->obj;
824 if( !obj || !opt ) return;
825 av_opt_set(obj, opt->name, val, 0);
829 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
836 FFOptionsKindItem::~FFOptionsKindItem()
840 int FFOptionsKindItem::handle_event()
842 FFOptionsWindow *fwin = kind->fwin;
843 FFOptions &options = fwin->options;
844 options.initialize(fwin, idx);
849 const char *FFOptionsKind::kinds[] = {
850 N_("codec"), // FF_KIND_CODEC
851 N_("ffmpeg"), // FF_KIND_FFMPEG
855 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
856 : BC_PopupMenu(x, y, w-calculate_w(0), "")
866 void FFOptionsKind::create_objects()
868 for( int i=0; i<(int)(sizeof(kinds)/sizeof(kinds[0])); ++i )
869 add_item(new FFOptionsKindItem(this, _(kinds[i]), i));
872 int FFOptionsKind::handle_event()
877 void FFOptionsKind::set(int k)
884 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
885 : BC_TextBox(x, y, w, 1, (char *)"")
895 int FFOptionsText::handle_event()
901 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
902 : BC_PopupMenu(x, y, w, "")
912 int FFOptionsUnits::handle_event()
914 const char *text = get_text();
915 if( text && fwin->selected ) {
916 fwin->selected->set(text);
917 fwin->selected->item_value->update();
918 av_dict_set(&fwin->dialog->ff_opts,
919 fwin->selected->item_name->get_text(),
920 fwin->selected->item_value->get_text(), 0);
927 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
928 : BC_GenericButton(x, y, _("Apply"))
938 int FFOptionsApply::handle_event()
940 const char *text = fwin->text->get_text();
941 if( text && fwin->selected ) {
942 fwin->selected->set(text);
943 fwin->selected->item_value->update();
944 av_dict_set(&fwin->dialog->ff_opts,
945 fwin->selected->item_name->get_text(),
946 fwin->selected->item_value->get_text(), 0);
952 FFOptions::FFOptions()
958 FFOptions::~FFOptions()
960 remove_all_objects();
961 avcodec_free_context(&avctx);
964 int FFOptions::cmpr(const void *a, const void *b)
966 FFOptions_Opt *ap = *(FFOptions_Opt **)a;
967 FFOptions_Opt *bp = *(FFOptions_Opt **)b;
968 const char *vap = ap->item_name->get_text();
969 const char *vbp = bp->item_name->get_text();
970 return strcmp(vap, vbp);
973 void FFOptions::initialize(FFOptionsWindow *win, int kind)
975 remove_all_objects();
980 avctx = avcodec_alloc_context3(win->dialog->codec);
984 obj = (const void *)avctx->priv_data;
987 obj = (const void *)avctx;
992 FFOptions &conf = *this;
993 const AVOption *opt = 0;
994 while( (opt=av_opt_next(obj, opt)) != 0 ) {
995 if( opt->type == AV_OPT_TYPE_CONST ) continue;
997 for( int i=0; !dupl && i<size(); ++i ) {
998 FFOptions_Opt *fopt = conf[i];
999 const AVOption *op = fopt->opt;
1000 if( op->offset != opt->offset ) continue;
1001 if( op->type != opt->type ) continue;
1003 if( strlen(op->name) < strlen(opt->name) )
1006 if( dupl ) continue;
1007 FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1009 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1010 fopt->item_value->update(vp);
1014 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1015 win->kind->set(kind);
1016 win->panel->update();
1017 win->panel->set_yposition(0);
1020 int FFOptions::update()
1023 FFOptions &conf = *this;
1025 for( int i=0; i<size(); ++i ) {
1026 FFOptions_Opt *fopt = conf[i];
1027 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1028 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1029 fopt->item_value->update(val);
1035 void FFOptions::dump(FILE *fp)
1038 const AVOption *opt = 0;
1039 FFOptions &conf = *this;
1041 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1042 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1044 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1045 if( k < 0 ) continue;
1046 FFOptions_Opt *fopt = conf[k];
1047 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1048 fprintf(fp, " %s:=%s", opt->name, vp);
1050 char unt[BCTEXTLEN], *up = unt;
1052 fprintf(fp, "%s", unt);
1059 void FFOptionsWindow::update(FFOptions_Opt *opt)
1061 if( selected != opt ) {
1062 if( selected ) selected->item_name->set_selected(0);
1064 if( selected ) selected->item_name->set_selected(1);
1066 clear_box(0,0, 0,panel->get_y());
1067 char str[BCTEXTLEN], *sp;
1069 if( opt ) opt->types(sp);
1072 if( opt ) opt->ranges(sp);
1074 while( units->total_items() ) units->del_item(0);
1075 char unit[BCSTRLEN]; strcpy(unit, "()");
1076 if( opt && opt->opt ) {
1077 ArrayList<const char *> names;
1079 if( opt->opt->unit ) {
1080 n = opt->units(names);
1081 if( n > 0 ) strcpy(unit,opt->opt->unit);
1083 for( int i=0; i<n; ++i )
1084 units->add_item(new BC_MenuItem(names[i], 0));
1086 units->set_text(unit);
1087 char val[BCTEXTLEN]; val[0] = 0;
1088 if( opt ) opt->get(val, sizeof(val));
1094 void FFOptions_OptPanel::show_tip(const char *tip)
1097 int len = strlen(tip);
1098 if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1099 strncpy(tip_text,tip,len);
1101 int line_limit = 60;
1102 int limit2 = line_limit/2;
1103 int limit4 = line_limit/4-2;
1104 char *cp = tip_text, *dp = cp+len;
1105 int n; char *bp, *ep, *pp, *sp;
1107 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1108 // target about half remaining line, constrain line_limit
1109 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1111 // search for last punct, last space before line_limit
1112 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1113 if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1114 else if( isspace(*cp) ) sp = cp;
1118 // first, after punctuation
1119 if( pp && pp-bp >= limit4 )
1125 // last, on next space
1127 while( cp<dp && !isspace(*cp) ) ++cp;
1134 fwin->panel->set_tooltip(tip_text);
1135 fwin->panel->show_tooltip();
1138 int FFOptions_OptPanel::selection_changed()
1140 FFOptions_Opt *opt = 0;
1141 BC_ListBoxItem *item = get_selection(0, 0);
1143 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1144 opt = opt_name->opt;
1147 if( opt ) show_tip(opt->tip());
1152 int FFOptions_Opt::types(char *rp)
1154 const char *cp = "";
1155 if( opt ) switch (opt->type) {
1156 case AV_OPT_TYPE_FLAGS: cp = "<flags>"; break;
1157 case AV_OPT_TYPE_INT: cp = "<int>"; break;
1158 case AV_OPT_TYPE_INT64: cp = "<int64>"; break;
1159 case AV_OPT_TYPE_DOUBLE: cp = "<double>"; break;
1160 case AV_OPT_TYPE_FLOAT: cp = "<float>"; break;
1161 case AV_OPT_TYPE_STRING: cp = "<string>"; break;
1162 case AV_OPT_TYPE_RATIONAL: cp = "<rational>"; break;
1163 case AV_OPT_TYPE_BINARY: cp = "<binary>"; break;
1164 case AV_OPT_TYPE_IMAGE_SIZE: cp = "<image_size>"; break;
1165 case AV_OPT_TYPE_VIDEO_RATE: cp = "<video_rate>"; break;
1166 case AV_OPT_TYPE_PIXEL_FMT: cp = "<pix_fmt>"; break;
1167 case AV_OPT_TYPE_SAMPLE_FMT: cp = "<sample_fmt>"; break;
1168 case AV_OPT_TYPE_DURATION: cp = "<duration>"; break;
1169 case AV_OPT_TYPE_COLOR: cp = "<color>"; break;
1170 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = "<channel_layout>"; break;
1171 case AV_OPT_TYPE_BOOL: cp = "<bool>"; break;
1172 default: cp = "<undef>"; break;
1174 return sprintf(rp, "%s", cp);
1176 int FFOptions_Opt::scalar(double d, char *rp)
1179 if( d == INT_MAX ) cp = "INT_MAX";
1180 else if( d == INT_MIN ) cp = "INT_MIN";
1181 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1182 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1183 else if( d == INT64_MIN ) cp = "I64_MIN";
1184 else if( d == FLT_MAX ) cp = "FLT_MAX";
1185 else if( d == FLT_MIN ) cp = "FLT_MIN";
1186 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1187 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1188 else if( d == DBL_MAX ) cp = "DBL_MAX";
1189 else if( d == DBL_MIN ) cp = "DBL_MIN";
1190 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1191 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1192 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1193 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1194 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1195 else return sprintf(rp, "%g", d);
1196 return sprintf(rp, "%s", cp);
1199 int FFOptions_Opt::ranges(char *rp)
1201 if( !opt ) return 0;
1202 void *obj = (void *)options->obj;
1203 if( !obj ) return 0;
1205 switch (opt->type) {
1206 case AV_OPT_TYPE_INT:
1207 case AV_OPT_TYPE_INT64:
1208 case AV_OPT_TYPE_DOUBLE:
1209 case AV_OPT_TYPE_FLOAT: break;
1212 AVOptionRanges *r = 0;
1214 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1215 for( int i=0; i<r->nb_ranges; ++i ) {
1216 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
1217 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
1218 cp += sprintf(cp, ")");
1220 av_opt_freep_ranges(&r);
1224 int FFOptions_Opt::units(ArrayList<const char *> &names)
1226 if( !opt || !opt->unit ) return 0;
1227 const void *obj = options->obj;
1228 if( !obj ) return 0;
1230 ArrayList<const AVOption *> opts;
1231 const AVOption *opt = NULL;
1232 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1233 if( !opt->unit ) continue;
1234 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1235 if( strcmp(this->opt->unit, opt->unit) ) continue;
1236 int i = opts.size();
1238 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1239 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1242 if( i >= 0 ) continue;
1246 for( int i=0; i<opts.size(); ++i )
1247 names.append(opts[i]->name);
1249 return names.size();
1252 int FFOptions_Opt::units(char *rp)
1254 ArrayList<const char *> names;
1255 int n = units(names);
1258 cp += sprintf(cp, " [%s:", this->opt->unit);
1259 for( int i=0; i<n; ++i )
1260 cp += sprintf(cp, " %s", names[i]);
1261 cp += sprintf(cp, "]:");
1265 const char *FFOptions_Opt::unit_name(int id)
1267 if( !opt || !opt->unit ) return 0;
1268 const void *obj = options->obj;
1269 if( !obj ) return 0;
1271 const char *ret = 0;
1272 const AVOption *opt = NULL;
1273 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1274 if( !opt->unit ) continue;
1275 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1276 if( strcmp(this->opt->unit, opt->unit) ) continue;
1277 if( opt->default_val.i64 != id ) continue;
1278 if( !ret ) { ret = opt->name; continue; }
1279 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1285 const char *FFOptions_Opt::tip()
1287 return !opt ? 0 : opt->help;
1291 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog)
1292 : BC_Window(PROGRAM_NAME ": Options", 60, 30, 640, 400)
1294 this->dialog = dialog;
1298 FFOptionsWindow::~FFOptionsWindow()
1302 void FFOptionsWindow::create_objects()
1306 add_subwindow(title = new BC_Title(x, y, dialog->codec_name));
1307 y += title->get_h() + 10;
1309 add_subwindow(title = new BC_Title(x0, y0, _("Type: ")));
1310 x0 += title->get_w() + 8;
1311 add_subwindow(type = new BC_Title(x0, y0, (char *)""));
1313 add_subwindow(title = new BC_Title(x0, y0, _("Range: ")));
1314 x0 += title->get_w() + 8;
1315 add_subwindow(range = new BC_Title(x0, y0, (char *)""));
1317 y += title->get_h() + 10;
1318 add_subwindow(units = new FFOptionsUnits(this, x0, y, 120));
1319 x0 += units->get_w() + 8;
1320 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
1321 add_subwindow(text = new FFOptionsText(this, x0, y, x1-x0 - 8));
1322 add_subwindow(apply = new FFOptionsApply(this, x1, y));
1323 y += units->get_h() + 10;
1324 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1325 kind->create_objects();
1326 const char *kind_text = _("Kind:");
1327 x1 -= BC_Title::calculate_w(this, kind_text) + 8;
1328 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1330 panel_x = x; panel_y = y;
1331 panel_w = get_w()-10 - panel_x;
1332 panel_h = get_h()-10 - panel_y - BC_OKButton::calculate_h();
1333 panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1334 add_subwindow(panel);
1335 add_subwindow(new BC_OKButton(this));
1336 add_subwindow(new BC_CancelButton(this));
1337 panel->create_objects();
1338 options.initialize(this, FF_KIND_CODEC);
1343 void FFOptionsWindow::draw()
1348 int FFOptionsWindow::resize_event(int w, int h)
1350 int x1 = w - 8 - kind->get_w();
1351 int y = kind->get_y();
1352 kind->reposition_window(x1, y);
1353 x1 -= kind_title->get_w() + 8;
1354 kind_title->reposition_window(x1,y);
1355 x1 = get_w() - apply->get_w() - 8;
1356 int y1 = units->get_y();
1357 apply->reposition_window(x1, y1);
1358 int x0 = units->get_x() + units->get_w() + 8;
1359 int y0 = units->get_y();
1360 text->reposition_window(x0,y0, x1-x0-8);
1361 panel_w = get_w()-10 - panel_x;
1362 panel_h = get_h()-10 - panel_y;
1363 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1367 FFOptionsDialog::FFOptionsDialog()
1370 this->options_window = 0;
1371 this->codec_name = 0;
1373 this->ff_options = 0;
1378 FFOptionsDialog::~FFOptionsDialog()
1381 delete [] codec_name;
1384 void FFOptionsDialog::load_options()
1386 char line[BCTEXTLEN];
1387 char key[BCSTRLEN], val[BCTEXTLEN];
1388 char *bp = ff_options, *dp = bp + ff_len-1;
1390 while( bp < dp && *bp != 0 ) {
1392 char *cp = line, *ep = cp+sizeof(line)-1;
1393 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1395 if( line[0] == '#' ) {
1396 sprintf(key,"#%d", no);
1397 av_dict_set(&ff_opts, key, line, 0);
1400 if( line[0] == '\n' ) continue;
1401 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1402 av_dict_set(&ff_opts, key, val, 0);
1406 void FFOptionsDialog::store_options()
1408 char *cp = ff_options, *ep = cp + ff_len-1;
1409 AVDictionaryEntry *elem = 0;
1410 while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1411 if( elem->key[0] == '#' ) {
1412 cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1415 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1420 void FFOptionsDialog::start(const char *codec_name, AVCodec *codec, char *options, int len)
1422 if( options_window ) {
1423 options_window->lock_window("FFOptionsDialog::start");
1424 options_window->raise_window();
1425 options_window->unlock_window();
1429 this->codec_name = cstrdup(codec_name);
1430 this->codec = codec;
1432 this->ff_options = options;
1436 BC_DialogThread::start();
1439 BC_Window* FFOptionsDialog::new_gui()
1441 options_window = new FFOptionsWindow(this);
1442 options_window->create_objects();
1443 return options_window;
1446 void FFOptionsDialog::handle_done_event(int result)
1453 delete [] codec_name; codec_name = 0;
1454 av_dict_free(&ff_opts);
1457 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1459 this->aud_config = aud_config;
1462 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1467 void FFOptionsAudioDialog::update_options()
1469 aud_config->update_options();
1472 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1474 this->vid_config = vid_config;
1477 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1482 void FFOptionsVideoDialog::update_options()
1484 vid_config->update_options();
1488 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text)
1489 : BC_GenericButton(x, y, text)
1491 this->aud_config = aud_config;
1494 FFOptionsViewAudio::~FFOptionsViewAudio()
1498 int FFOptionsViewAudio::handle_event()
1501 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1502 Asset *asset = aud_config->asset;
1503 const char *name = asset->acodec;
1504 if( !FFMPEG::get_codec(audio_codec, "audio", name) )
1505 codec = avcodec_find_encoder_by_name(audio_codec);
1507 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1510 aud_config->ff_options_dialog->start(audio_codec, codec,
1511 asset->ff_audio_options, sizeof(asset->ff_audio_options));
1515 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text)
1516 : BC_GenericButton(x, y, text)
1518 this->vid_config = vid_config;
1521 FFOptionsViewVideo::~FFOptionsViewVideo()
1525 int FFOptionsViewVideo::handle_event()
1528 char video_codec[BCSTRLEN]; video_codec[0] = 0;
1529 Asset *asset = vid_config->asset;
1530 const char *name = asset->vcodec;
1531 if( !FFMPEG::get_codec(video_codec, "video", name) )
1532 codec = avcodec_find_encoder_by_name(video_codec);
1534 eprintf(_("no codec named: %s: %s"), name, video_codec);
1537 vid_config->ff_options_dialog->start(video_codec, codec,
1538 asset->ff_video_options, sizeof(asset->ff_video_options));