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));
399 audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
400 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
401 audio_options->create_objects();
402 add_subwindow(new BC_OKButton(this));
405 bitrate->handle_event();
409 int FFMPEGConfigAudio::close_event()
415 void FFMPEGConfigAudio::update_options()
417 audio_options->update(asset->ff_audio_options);
420 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
421 int x, int y, int w, int rows, int size, char *text)
422 : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
424 this->audio_popup = audio_popup;
427 int FFAudioOptions::handle_event()
429 strcpy(audio_popup->asset->ff_audio_options, get_text());
434 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
435 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
440 int FFMPEGConfigAudioPopup::handle_event()
442 strcpy(popup->asset->acodec, get_text());
443 Asset *asset = popup->asset;
444 char option_path[BCTEXTLEN];
445 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
446 FFMPEG::load_options(option_path, asset->ff_audio_options,
447 sizeof(asset->ff_audio_options));
448 popup->audio_options->update(asset->ff_audio_options);
453 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
454 char *title_text, int x, int y, int *output)
455 : BC_CheckBox(x, y, *output, title_text)
458 this->output = output;
460 int FFMPEGConfigAudioToggle::handle_event()
462 *output = get_value();
468 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
469 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
470 parent_window->get_abs_cursor_x(1),
471 parent_window->get_abs_cursor_y(1),
474 this->parent_window = parent_window;
483 FFMPEGConfigVideo::~FFMPEGConfigVideo()
485 delete ff_options_dialog;
486 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
487 if(preset_popup) delete preset_popup;
488 presets.remove_all_objects();
492 void FFMPEGConfigVideo::create_objects()
495 lock_window("FFMPEGConfigVideo::create_objects");
497 add_subwindow(new BC_Title(x, y, _("Compression:")));
501 char option_path[BCTEXTLEN];
502 FFMPEG::set_option_path(option_path, "video");
503 fs.update(option_path);
504 int total_files = fs.total_files();
505 for(int i = 0; i < total_files; i++) {
506 const char *name = fs.get_entry(i)->get_name();
507 if( asset->fformat[0] != 0 ) {
508 const char *ext = strrchr(name,'.');
510 if( strcmp(asset->fformat, ++ext) ) continue;
512 presets.append(new BC_ListBoxItem(name));
515 if( asset->vcodec[0] ) {
516 int k = presets.size();
517 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
518 if( k < 0 ) asset->vcodec[0] = 0;
521 if( !asset->vcodec[0] && presets.size() > 0 )
522 strcpy(asset->vcodec, presets[0]->get_text());
524 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
525 preset_popup->create_objects();
527 if( asset->ff_video_bitrate && asset->ff_video_quality ) {
528 asset->ff_video_bitrate = 0;
529 asset->ff_video_quality = 0;
533 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
534 bitrate->create_objects();
535 bitrate->set_increment(100000);
536 y += bitrate->get_h() + 5;
537 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
538 quality->create_objects();
539 quality->set_increment(1);
540 quality->set_boundaries((int64_t)0, (int64_t)31);
542 y += quality->get_h() + 10;
543 BC_Title *title = new BC_Title(x, y, _("Video Options:"));
544 add_subwindow(title);
546 ff_options_dialog = new FFOptionsVideoDialog(this);
547 int x1 = x + title->get_w() + 8;
548 add_subwindow(new FFOptionsViewVideo(this, x1, y, _("view")));
551 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
552 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
553 FFMPEG::load_options(option_path, asset->ff_video_options,
554 sizeof(asset->ff_video_options));
557 video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
558 sizeof(asset->ff_video_options)-1, asset->ff_video_options);
559 video_options->create_objects();
561 add_subwindow(new BC_OKButton(this));
563 if( asset->ff_video_bitrate )
565 if( asset->ff_video_quality )
570 int FFMPEGConfigVideo::close_event()
576 void FFMPEGConfigVideo::update_options()
578 video_options->update(asset->ff_video_options);
581 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
582 int x, int y, int w, int rows, int size, char *text)
583 : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
585 this->video_popup = video_popup;
588 int FFVideoOptions::handle_event()
590 strcpy(video_popup->asset->ff_video_options, get_text());
595 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
596 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
601 int FFMPEGConfigVideoPopup::handle_event()
603 strcpy(popup->asset->vcodec, get_text());
604 Asset *asset = popup->asset;
605 char option_path[BCTEXTLEN];
606 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
607 FFMPEG::load_options(option_path, asset->ff_video_options,
608 sizeof(asset->ff_video_options));
609 popup->video_options->update(asset->ff_video_options);
614 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
615 char *title_text, int x, int y, int *output)
616 : BC_CheckBox(x, y, *output, title_text)
619 this->output = output;
621 int FFMPEGConfigVideoToggle::handle_event()
623 *output = get_value();
627 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
628 const char *title, int64_t length, int64_t *position, int *canceled)
631 this->index_file = index_file;
632 this->progress_bar = progress_bar;
633 strcpy(this->progress_title, title);
634 this->length = length;
635 this->position = position;
636 this->canceled = canceled;
641 FFMPEGScanProgress::~FFMPEGScanProgress()
648 void FFMPEGScanProgress::run()
651 if( progress_bar->update(*position) ) {
652 *canceled = done = 1;
655 index_file->redraw_edits(0);
660 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
663 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
665 IndexState *index_state = index_file->get_state();
666 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
667 index_state->reset_index();
668 index_state->reset_markers();
669 index_state->index_status = INDEX_BUILDING;
671 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
672 FFAudioStream *aud = ff->ffaudio[aidx];
673 index_state->add_audio_stream(aud->channels, aud->length);
677 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
678 char *index_path = index_file->index_filename;
681 int64_t scan_position = 0;
682 FFMPEGScanProgress *scan_progress = 0;
684 char progress_title[BCTEXTLEN];
685 sprintf(progress_title, _("Creating %s\n"), index_path);
686 progress_bar->update_title(progress_title, 1);
687 progress_bar->update_length(file_bytes);
688 scan_progress = new FFMPEGScanProgress(index_file,
689 progress_bar, progress_title, file_bytes,
690 &scan_position, &canceled);
693 index_state->index_bytes = file_bytes;
694 index_state->init_scan(file->preferences->index_size);
696 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
697 index_state->reset_index();
698 index_state->reset_markers();
702 delete scan_progress;
703 if( canceled ) return 1;
705 index_state->marker_status = MARKERS_READY;
706 return index_state->create_index(index_path, asset);
711 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
712 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
715 update(); // init col/wid/columns
719 ~FFOptions_OptPanel()
723 void FFOptions_OptPanel::create_objects()
725 const char *cols[] = { "option", "value", };
726 const int col1_w = 150;
727 int wids[] = { col1_w, get_w()-col1_w };
728 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
731 int FFOptions_OptPanel::update()
735 FFOptions &options = fwin->options;
736 for( int i=0; i<options.size(); ++i ) {
737 FFOptions_Opt *opt = options[i];
738 opts.append(opt->item_name);
739 vals.append(opt->item_value);
745 int FFOptions_OptPanel::cursor_leave_event()
752 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
758 FFOptions_OptName::~FFOptions_OptName()
762 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
768 void FFOptions_OptValue::update()
771 char val[BCTEXTLEN]; val[0] = 0;
772 opt->get(val, sizeof(val));
776 void FFOptions_OptValue::update(const char *v)
781 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
783 this->options = options;
785 item_name = new FFOptions_OptName(this, nm);
786 item_value = new FFOptions_OptValue(this);
789 FFOptions_Opt::~FFOptions_Opt()
795 char *FFOptions_Opt::get(char *vp, int sz)
799 if( !opt ) return cp;
801 void *obj = (void *)options->obj;
803 if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
804 const char *val = (const char *)bp;
805 if( opt->unit && *val ) {
807 const char *uid = unit_name(id);
810 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
811 if( sz > 0 ) vp[sz-1] = 0;
818 void FFOptions_Opt::set(const char *val)
820 void *obj = (void *)options->obj;
821 if( !obj || !opt ) return;
822 av_opt_set(obj, opt->name, val, 0);
826 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
833 FFOptionsKindItem::~FFOptionsKindItem()
837 int FFOptionsKindItem::handle_event()
839 FFOptionsWindow *fwin = kind->fwin;
840 FFOptions &options = fwin->options;
841 options.initialize(fwin, idx);
846 const char *FFOptionsKind::kinds[] = {
847 N_("codec"), // FF_KIND_CODEC
848 N_("ffmpeg"), // FF_KIND_FFMPEG
852 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
853 : BC_PopupMenu(x, y, w-calculate_w(0), "")
863 void FFOptionsKind::create_objects()
865 for( int i=0; i<(int)(sizeof(kinds)/sizeof(kinds[0])); ++i )
866 add_item(new FFOptionsKindItem(this, _(kinds[i]), i));
869 int FFOptionsKind::handle_event()
874 void FFOptionsKind::set(int k)
881 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
882 : BC_TextBox(x, y, w, 1, (char *)"")
892 int FFOptionsText::handle_event()
898 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
899 : BC_PopupMenu(x, y, w, "")
909 int FFOptionsUnits::handle_event()
911 const char *text = get_text();
912 if( text && fwin->selected ) {
913 fwin->selected->set(text);
914 fwin->selected->item_value->update();
915 av_dict_set(&fwin->dialog->ff_opts,
916 fwin->selected->item_name->get_text(),
917 fwin->selected->item_value->get_text(), 0);
924 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
925 : BC_GenericButton(x, y, _("Apply"))
935 int FFOptionsApply::handle_event()
937 const char *text = fwin->text->get_text();
938 if( text && fwin->selected ) {
939 fwin->selected->set(text);
940 fwin->selected->item_value->update();
941 av_dict_set(&fwin->dialog->ff_opts,
942 fwin->selected->item_name->get_text(),
943 fwin->selected->item_value->get_text(), 0);
949 FFOptions::FFOptions()
955 FFOptions::~FFOptions()
957 remove_all_objects();
958 avcodec_free_context(&avctx);
961 int FFOptions::cmpr(const void *a, const void *b)
963 FFOptions_Opt *ap = *(FFOptions_Opt **)a;
964 FFOptions_Opt *bp = *(FFOptions_Opt **)b;
965 const char *vap = ap->item_name->get_text();
966 const char *vbp = bp->item_name->get_text();
967 return strcmp(vap, vbp);
970 void FFOptions::initialize(FFOptionsWindow *win, int kind)
972 remove_all_objects();
977 avctx = avcodec_alloc_context3(win->dialog->codec);
981 obj = (const void *)avctx->priv_data;
984 obj = (const void *)avctx;
989 FFOptions &conf = *this;
990 const AVOption *opt = 0;
991 while( (opt=av_opt_next(obj, opt)) != 0 ) {
992 if( opt->type == AV_OPT_TYPE_CONST ) continue;
994 for( int i=0; !dupl && i<size(); ++i ) {
995 FFOptions_Opt *fopt = conf[i];
996 const AVOption *op = fopt->opt;
997 if( op->offset != opt->offset ) continue;
998 if( op->type != opt->type ) continue;
1000 if( strlen(op->name) < strlen(opt->name) )
1003 if( dupl ) continue;
1004 FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1006 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1007 fopt->item_value->update(vp);
1011 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1012 win->kind->set(kind);
1013 win->panel->update();
1014 win->panel->set_yposition(0);
1017 int FFOptions::update()
1020 FFOptions &conf = *this;
1022 for( int i=0; i<size(); ++i ) {
1023 FFOptions_Opt *fopt = conf[i];
1024 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1025 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1026 fopt->item_value->update(val);
1032 void FFOptions::dump(FILE *fp)
1035 const AVOption *opt = 0;
1036 FFOptions &conf = *this;
1038 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1039 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1041 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1042 if( k < 0 ) continue;
1043 FFOptions_Opt *fopt = conf[k];
1044 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1045 fprintf(fp, " %s:=%s", opt->name, vp);
1047 char unt[BCTEXTLEN], *up = unt;
1049 fprintf(fp, "%s", unt);
1056 void FFOptionsWindow::update(FFOptions_Opt *opt)
1058 if( selected != opt ) {
1059 if( selected ) selected->item_name->set_selected(0);
1061 if( selected ) selected->item_name->set_selected(1);
1063 clear_box(0,0, 0,panel->get_y());
1064 char str[BCTEXTLEN], *sp;
1066 if( opt ) opt->types(sp);
1069 if( opt ) opt->ranges(sp);
1071 while( units->total_items() ) units->del_item(0);
1072 char unit[BCSTRLEN]; strcpy(unit, "()");
1073 if( opt && opt->opt ) {
1074 ArrayList<const char *> names;
1076 if( opt->opt->unit ) {
1077 n = opt->units(names);
1078 if( n > 0 ) strcpy(unit,opt->opt->unit);
1080 for( int i=0; i<n; ++i )
1081 units->add_item(new BC_MenuItem(names[i], 0));
1083 units->set_text(unit);
1084 char val[BCTEXTLEN]; val[0] = 0;
1085 if( opt ) opt->get(val, sizeof(val));
1091 void FFOptions_OptPanel::show_tip(const char *tip)
1094 int len = strlen(tip);
1095 if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1096 strncpy(tip_text,tip,len);
1098 int line_limit = 60;
1099 int limit2 = line_limit/2;
1100 int limit4 = line_limit/4-2;
1101 char *cp = tip_text, *dp = cp+len;
1102 int n; char *bp, *ep, *pp, *sp;
1104 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1105 // target about half remaining line, constrain line_limit
1106 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1108 // search for last punct, last space before line_limit
1109 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1110 if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1111 else if( isspace(*cp) ) sp = cp;
1115 // first, after punctuation
1116 if( pp && pp-bp >= limit4 )
1122 // last, on next space
1124 while( cp<dp && !isspace(*cp) ) ++cp;
1131 fwin->panel->set_tooltip(tip_text);
1132 fwin->panel->show_tooltip();
1135 int FFOptions_OptPanel::selection_changed()
1137 FFOptions_Opt *opt = 0;
1138 BC_ListBoxItem *item = get_selection(0, 0);
1140 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1141 opt = opt_name->opt;
1144 if( opt ) show_tip(opt->tip());
1149 int FFOptions_Opt::types(char *rp)
1151 const char *cp = "";
1152 if( opt ) switch (opt->type) {
1153 case AV_OPT_TYPE_FLAGS: cp = "<flags>"; break;
1154 case AV_OPT_TYPE_INT: cp = "<int>"; break;
1155 case AV_OPT_TYPE_INT64: cp = "<int64>"; break;
1156 case AV_OPT_TYPE_DOUBLE: cp = "<double>"; break;
1157 case AV_OPT_TYPE_FLOAT: cp = "<float>"; break;
1158 case AV_OPT_TYPE_STRING: cp = "<string>"; break;
1159 case AV_OPT_TYPE_RATIONAL: cp = "<rational>"; break;
1160 case AV_OPT_TYPE_BINARY: cp = "<binary>"; break;
1161 case AV_OPT_TYPE_IMAGE_SIZE: cp = "<image_size>"; break;
1162 case AV_OPT_TYPE_VIDEO_RATE: cp = "<video_rate>"; break;
1163 case AV_OPT_TYPE_PIXEL_FMT: cp = "<pix_fmt>"; break;
1164 case AV_OPT_TYPE_SAMPLE_FMT: cp = "<sample_fmt>"; break;
1165 case AV_OPT_TYPE_DURATION: cp = "<duration>"; break;
1166 case AV_OPT_TYPE_COLOR: cp = "<color>"; break;
1167 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = "<channel_layout>"; break;
1168 case AV_OPT_TYPE_BOOL: cp = "<bool>"; break;
1169 default: cp = "<undef>"; break;
1171 return sprintf(rp, "%s", cp);
1173 int FFOptions_Opt::scalar(double d, char *rp)
1176 if( d == INT_MAX ) cp = "INT_MAX";
1177 else if( d == INT_MIN ) cp = "INT_MIN";
1178 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1179 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1180 else if( d == INT64_MIN ) cp = "I64_MIN";
1181 else if( d == FLT_MAX ) cp = "FLT_MAX";
1182 else if( d == FLT_MIN ) cp = "FLT_MIN";
1183 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1184 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1185 else if( d == DBL_MAX ) cp = "DBL_MAX";
1186 else if( d == DBL_MIN ) cp = "DBL_MIN";
1187 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1188 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1189 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1190 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1191 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1192 else return sprintf(rp, "%g", d);
1193 return sprintf(rp, "%s", cp);
1196 int FFOptions_Opt::ranges(char *rp)
1198 if( !opt ) return 0;
1199 void *obj = (void *)options->obj;
1200 if( !obj ) return 0;
1202 switch (opt->type) {
1203 case AV_OPT_TYPE_INT:
1204 case AV_OPT_TYPE_INT64:
1205 case AV_OPT_TYPE_DOUBLE:
1206 case AV_OPT_TYPE_FLOAT: break;
1209 AVOptionRanges *r = 0;
1211 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1212 for( int i=0; i<r->nb_ranges; ++i ) {
1213 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
1214 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
1215 cp += sprintf(cp, ")");
1217 av_opt_freep_ranges(&r);
1221 int FFOptions_Opt::units(ArrayList<const char *> &names)
1223 if( !opt || !opt->unit ) return 0;
1224 const void *obj = options->obj;
1225 if( !obj ) return 0;
1227 ArrayList<const AVOption *> opts;
1228 const AVOption *opt = NULL;
1229 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1230 if( !opt->unit ) continue;
1231 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1232 if( strcmp(this->opt->unit, opt->unit) ) continue;
1233 int i = opts.size();
1235 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1236 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1239 if( i >= 0 ) continue;
1243 for( int i=0; i<opts.size(); ++i )
1244 names.append(opts[i]->name);
1246 return names.size();
1249 int FFOptions_Opt::units(char *rp)
1251 ArrayList<const char *> names;
1252 int n = units(names);
1255 cp += sprintf(cp, " [%s:", this->opt->unit);
1256 for( int i=0; i<n; ++i )
1257 cp += sprintf(cp, " %s", names[i]);
1258 cp += sprintf(cp, "]:");
1262 const char *FFOptions_Opt::unit_name(int id)
1264 if( !opt || !opt->unit ) return 0;
1265 const void *obj = options->obj;
1266 if( !obj ) return 0;
1268 const char *ret = 0;
1269 const AVOption *opt = NULL;
1270 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1271 if( !opt->unit ) continue;
1272 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1273 if( strcmp(this->opt->unit, opt->unit) ) continue;
1274 if( opt->default_val.i64 != id ) continue;
1275 if( !ret ) { ret = opt->name; continue; }
1276 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1282 const char *FFOptions_Opt::tip()
1284 return !opt ? 0 : opt->help;
1288 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog)
1289 : BC_Window(PROGRAM_NAME ": Options", 60, 30, 640, 400)
1291 this->dialog = dialog;
1295 FFOptionsWindow::~FFOptionsWindow()
1299 void FFOptionsWindow::create_objects()
1303 add_subwindow(title = new BC_Title(x, y, dialog->codec_name));
1304 y += title->get_h() + 10;
1306 add_subwindow(title = new BC_Title(x0, y0, _("Type: ")));
1307 x0 += title->get_w() + 8;
1308 add_subwindow(type = new BC_Title(x0, y0, (char *)""));
1310 add_subwindow(title = new BC_Title(x0, y0, _("Range: ")));
1311 x0 += title->get_w() + 8;
1312 add_subwindow(range = new BC_Title(x0, y0, (char *)""));
1314 y += title->get_h() + 10;
1315 add_subwindow(units = new FFOptionsUnits(this, x0, y, 120));
1316 x0 += units->get_w() + 8;
1317 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
1318 add_subwindow(text = new FFOptionsText(this, x0, y, x1-x0 - 8));
1319 add_subwindow(apply = new FFOptionsApply(this, x1, y));
1320 y += units->get_h() + 10;
1321 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1322 kind->create_objects();
1323 const char *kind_text = _("Kind:");
1324 x1 -= BC_Title::calculate_w(this, kind_text) + 8;
1325 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1327 panel_x = x; panel_y = y;
1328 panel_w = get_w()-10 - panel_x;
1329 panel_h = get_h()-10 - panel_y - BC_OKButton::calculate_h();
1330 panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1331 add_subwindow(panel);
1332 add_subwindow(new BC_OKButton(this));
1333 add_subwindow(new BC_CancelButton(this));
1334 panel->create_objects();
1335 options.initialize(this, FF_KIND_CODEC);
1340 void FFOptionsWindow::draw()
1345 int FFOptionsWindow::resize_event(int w, int h)
1347 int x1 = w - 8 - kind->get_w();
1348 int y = kind->get_y();
1349 kind->reposition_window(x1, y);
1350 x1 -= kind_title->get_w() + 8;
1351 kind_title->reposition_window(x1,y);
1352 x1 = get_w() - apply->get_w() - 8;
1353 int y1 = units->get_y();
1354 apply->reposition_window(x1, y1);
1355 int x0 = units->get_x() + units->get_w() + 8;
1356 int y0 = units->get_y();
1357 text->reposition_window(x0,y0, x1-x0-8);
1358 panel_w = get_w()-10 - panel_x;
1359 panel_h = get_h()-10 - panel_y;
1360 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1364 FFOptionsDialog::FFOptionsDialog()
1367 this->options_window = 0;
1368 this->codec_name = 0;
1370 this->ff_options = 0;
1375 FFOptionsDialog::~FFOptionsDialog()
1378 delete [] codec_name;
1381 void FFOptionsDialog::load_options()
1383 char line[BCTEXTLEN];
1384 char key[BCSTRLEN], val[BCTEXTLEN];
1385 char *bp = ff_options, *dp = bp + ff_len-1;
1387 while( bp < dp && *bp != 0 ) {
1389 char *cp = line, *ep = cp+sizeof(line)-1;
1390 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1392 if( line[0] == '#' ) {
1393 sprintf(key,"#%d", no);
1394 av_dict_set(&ff_opts, key, line, 0);
1397 if( line[0] == '\n' ) continue;
1398 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1399 av_dict_set(&ff_opts, key, val, 0);
1403 void FFOptionsDialog::store_options()
1405 char *cp = ff_options, *ep = cp + ff_len-1;
1406 AVDictionaryEntry *elem = 0;
1407 while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1408 if( elem->key[0] == '#' ) {
1409 cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1412 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1417 void FFOptionsDialog::start(const char *codec_name, AVCodec *codec, char *options, int len)
1419 if( options_window ) {
1420 options_window->lock_window("FFOptionsDialog::start");
1421 options_window->raise_window();
1422 options_window->unlock_window();
1426 this->codec_name = cstrdup(codec_name);
1427 this->codec = codec;
1429 this->ff_options = options;
1433 BC_DialogThread::start();
1436 BC_Window* FFOptionsDialog::new_gui()
1438 options_window = new FFOptionsWindow(this);
1439 options_window->create_objects();
1440 return options_window;
1443 void FFOptionsDialog::handle_done_event(int result)
1450 delete [] codec_name; codec_name = 0;
1451 av_dict_free(&ff_opts);
1454 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1456 this->aud_config = aud_config;
1459 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1464 void FFOptionsAudioDialog::update_options()
1466 aud_config->update_options();
1469 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1471 this->vid_config = vid_config;
1474 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1479 void FFOptionsVideoDialog::update_options()
1481 vid_config->update_options();
1485 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text)
1486 : BC_GenericButton(x, y, text)
1488 this->aud_config = aud_config;
1491 FFOptionsViewAudio::~FFOptionsViewAudio()
1495 int FFOptionsViewAudio::handle_event()
1498 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1499 Asset *asset = aud_config->asset;
1500 const char *name = asset->acodec;
1501 if( !FFMPEG::get_codec(audio_codec, "audio", name) )
1502 codec = avcodec_find_encoder_by_name(audio_codec);
1504 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1507 aud_config->ff_options_dialog->start(audio_codec, codec,
1508 asset->ff_audio_options, sizeof(asset->ff_audio_options));
1512 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text)
1513 : BC_GenericButton(x, y, text)
1515 this->vid_config = vid_config;
1518 FFOptionsViewVideo::~FFOptionsViewVideo()
1522 int FFOptionsViewVideo::handle_event()
1525 char video_codec[BCSTRLEN]; video_codec[0] = 0;
1526 Asset *asset = vid_config->asset;
1527 const char *name = asset->vcodec;
1528 if( !FFMPEG::get_codec(video_codec, "video", name) )
1529 codec = avcodec_find_encoder_by_name(video_codec);
1531 eprintf(_("no codec named: %s: %s"), name, video_codec);
1534 vid_config->ff_options_dialog->start(video_codec, codec,
1535 asset->ff_video_options, sizeof(asset->ff_video_options));