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 if( !window->run_window() )
116 strcpy(asset->ff_audio_options, window->audio_options->get_text());
119 else if(video_options) {
120 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
121 format_window = window;
122 window->create_objects();
123 if( !window->run_window() )
124 strcpy(asset->ff_video_options, window->video_options->get_text());
129 int FileFFMPEG::check_sig(Asset *asset)
131 char *ptr = strstr(asset->path, ".pcm");
133 ptr = strstr(asset->path, ".raw");
137 int ret = !ffmpeg.init_decoder(asset->path) &&
138 !ffmpeg.open_decoder() ? 1 : 0;
142 void FileFFMPEG::get_info(char *path, char *text, int len)
146 cp += sprintf(cp, _("file path: %s\n"), path);
149 if( stat(path, &st) < 0 ) {
150 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
154 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
156 if( !ret ) ret = ffmpeg.init_decoder(path);
157 if( !ret ) ret = ffmpeg.open_decoder();
159 cp += sprintf(cp, _("info:\n"));
160 ffmpeg.info(cp, len-(cp-text));
163 sprintf(cp, _("== open failed\n"));
166 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
167 int &width, int &height, char *title)
170 pid = ff->ff_video_pid(track);
171 framerate = ff->ff_frame_rate(track);
172 width = ff->ff_video_width(track);
173 height = ff->ff_video_height(track);
174 if( title ) *title = 0;
178 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
181 return ff->ff_audio_for_video(vstream, astream, channel_mask);
184 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
186 if( !ff || !asset->video_data ) return 1;
187 asset->width = ff->ff_video_width(vstream);
188 asset->height = ff->ff_video_height(vstream);
189 asset->video_length = ff->ff_video_frames(vstream);
190 if( (asset->video_length = ff->ff_video_frames(vstream)) < 2 )
191 asset->video_length = asset->video_length < 0 ? 0 : -1;
192 asset->frame_rate = ff->ff_frame_rate(vstream);
196 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
198 if( !ff || !asset->audio_data ) return 1;
199 asset->sample_rate = ff->ff_sample_rate(astream);
200 asset->audio_length = ff->ff_audio_samples(astream);
204 int FileFFMPEG::open_file(int rd, int wr)
208 ff = new FFMPEG(this);
211 result = ff->init_decoder(asset->path);
212 if( !result ) result = ff->open_decoder();
214 int audio_channels = ff->ff_total_audio_channels();
215 if( audio_channels > 0 ) {
216 asset->audio_data = 1;
217 asset->channels = audio_channels;
218 asset->sample_rate = ff->ff_sample_rate(0);
219 asset->audio_length = ff->ff_audio_samples(0);
221 int video_layers = ff->ff_total_video_layers();
222 if( video_layers > 0 ) {
223 asset->video_data = 1;
224 if( !asset->layers ) asset->layers = video_layers;
225 asset->actual_width = ff->ff_video_width(0);
226 asset->actual_height = ff->ff_video_height(0);
227 if( !asset->width ) asset->width = asset->actual_width;
228 if( !asset->height ) asset->height = asset->actual_height;
229 if( !asset->video_length &&
230 (asset->video_length = ff->ff_video_frames(0)) < 2 )
231 asset->video_length = asset->video_length < 0 ? 0 : -1;
232 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
234 IndexState *index_state = asset->index_state;
235 index_state->read_markers(file->preferences->index_directory, asset->path);
239 result = ff->init_encoder(asset->path);
240 // must be in this order or dvdauthor will fail
241 if( !result && asset->video_data )
242 result = ff->open_encoder("video", asset->vcodec);
243 if( !result && asset->audio_data )
244 result = ff->open_encoder("audio", asset->acodec);
249 int FileFFMPEG::close_file()
257 int FileFFMPEG::write_samples(double **buffer, int64_t len)
259 if( !ff || len < 0 ) return -1;
261 int ret = ff->encode(stream, buffer, len);
265 int FileFFMPEG::write_frames(VFrame ***frames, int len)
268 int ret = 0, layer = 0;
269 for(int i = 0; i < 1; i++) {
270 for(int j = 0; j < len && !ret; j++) {
271 VFrame *frame = frames[i][j];
272 ret = ff->encode(layer, frame);
279 int FileFFMPEG::read_samples(double *buffer, int64_t len)
281 if( !ff || len < 0 ) return -1;
282 int ch = file->current_channel;
283 int64_t pos = file->current_sample;
284 int ret = ff->decode(ch, pos, buffer, len);
285 if( ret > 0 ) return 0;
286 memset(buffer,0,len*sizeof(*buffer));
290 int FileFFMPEG::read_frame(VFrame *frame)
293 int layer = file->current_layer;
294 int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
295 int ret = ff->decode(layer, pos, frame);
296 frame->set_status(ret);
297 if( ret >= 0 ) return 0;
298 frame->clear_frame();
303 int64_t FileFFMPEG::get_memory_usage()
308 int FileFFMPEG::colormodel_supported(int colormodel)
313 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
316 case PLAYBACK_X11: return BC_RGB888;
317 case PLAYBACK_X11_GL: return BC_YUV888;
324 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
325 : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
326 parent_window->get_abs_cursor_x(1),
327 parent_window->get_abs_cursor_y(1),
330 this->parent_window = parent_window;
336 ff_options_dialog = 0;
339 FFMPEGConfigAudio::~FFMPEGConfigAudio()
341 delete ff_options_dialog;
342 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
344 presets.remove_all_objects();
348 void FFMPEGConfigAudio::create_objects()
351 lock_window("FFMPEGConfigAudio::create_objects");
354 char option_path[BCTEXTLEN];
355 FFMPEG::set_option_path(option_path, "audio");
356 fs.update(option_path);
357 int total_files = fs.total_files();
358 for(int i = 0; i < total_files; i++) {
359 const char *name = fs.get_entry(i)->get_name();
360 if( asset->fformat[0] != 0 ) {
361 const char *ext = strrchr(name,'.');
363 if( strcmp(asset->fformat, ++ext) ) continue;
365 presets.append(new BC_ListBoxItem(name));
368 if( asset->acodec[0] ) {
369 int k = presets.size();
370 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
371 if( k < 0 ) asset->acodec[0] = 0;
374 if( !asset->acodec[0] && presets.size() > 0 )
375 strcpy(asset->acodec, presets[0]->get_text());
377 add_tool(new BC_Title(x, y, _("Preset:")));
379 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
380 preset_popup->create_objects();
383 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
384 bitrate->create_objects();
385 bitrate->set_increment(1000);
387 y += bitrate->get_h() + 10;
388 BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
389 add_subwindow(title);
391 ff_options_dialog = new FFOptionsAudioDialog(this);
392 int x1 = x + title->get_w() + 8;
393 add_subwindow(new FFOptionsViewAudio(this, x1, y, _("view")));
396 if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
397 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
398 FFMPEG::load_options(option_path, asset->ff_audio_options,
399 sizeof(asset->ff_audio_options));
402 audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
403 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
404 audio_options->create_objects();
405 add_subwindow(new BC_OKButton(this));
406 add_subwindow(new BC_CancelButton(this));
409 bitrate->handle_event();
413 int FFMPEGConfigAudio::close_event()
419 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
420 int x, int y, int w, int rows, int size, char *text)
421 : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
423 this->audio_popup = audio_popup;
426 int FFAudioOptions::handle_event()
428 strcpy(audio_popup->asset->ff_audio_options, get_text());
433 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
434 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
439 int FFMPEGConfigAudioPopup::handle_event()
441 strcpy(popup->asset->acodec, get_text());
442 Asset *asset = popup->asset;
443 char option_path[BCTEXTLEN];
444 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
445 FFMPEG::load_options(option_path, asset->ff_audio_options,
446 sizeof(asset->ff_audio_options));
447 popup->audio_options->update(asset->ff_audio_options);
452 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
453 char *title_text, int x, int y, int *output)
454 : BC_CheckBox(x, y, *output, title_text)
457 this->output = output;
459 int FFMPEGConfigAudioToggle::handle_event()
461 *output = get_value();
467 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
468 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
469 parent_window->get_abs_cursor_x(1),
470 parent_window->get_abs_cursor_y(1),
473 this->parent_window = parent_window;
482 FFMPEGConfigVideo::~FFMPEGConfigVideo()
484 delete ff_options_dialog;
485 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
486 if(preset_popup) delete preset_popup;
487 presets.remove_all_objects();
491 void FFMPEGConfigVideo::create_objects()
494 lock_window("FFMPEGConfigVideo::create_objects");
496 add_subwindow(new BC_Title(x, y, _("Compression:")));
500 char option_path[BCTEXTLEN];
501 FFMPEG::set_option_path(option_path, "video");
502 fs.update(option_path);
503 int total_files = fs.total_files();
504 for(int i = 0; i < total_files; i++) {
505 const char *name = fs.get_entry(i)->get_name();
506 if( asset->fformat[0] != 0 ) {
507 const char *ext = strrchr(name,'.');
509 if( strcmp(asset->fformat, ++ext) ) continue;
511 presets.append(new BC_ListBoxItem(name));
514 if( asset->vcodec[0] ) {
515 int k = presets.size();
516 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
517 if( k < 0 ) asset->vcodec[0] = 0;
520 if( !asset->vcodec[0] && presets.size() > 0 )
521 strcpy(asset->vcodec, presets[0]->get_text());
523 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
524 preset_popup->create_objects();
526 if( asset->ff_video_bitrate && asset->ff_video_quality ) {
527 asset->ff_video_bitrate = 0;
528 asset->ff_video_quality = 0;
532 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
533 bitrate->create_objects();
534 bitrate->set_increment(100000);
535 y += bitrate->get_h() + 5;
536 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
537 quality->create_objects();
538 quality->set_increment(1);
539 quality->set_boundaries((int64_t)0, (int64_t)31);
541 y += quality->get_h() + 10;
542 BC_Title *title = new BC_Title(x, y, _("Video Options:"));
543 add_subwindow(title);
545 ff_options_dialog = new FFOptionsVideoDialog(this);
546 int x1 = x + title->get_w() + 8;
547 add_subwindow(new FFOptionsViewVideo(this, x1, y, _("view")));
550 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
551 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
552 FFMPEG::load_options(option_path, asset->ff_video_options,
553 sizeof(asset->ff_video_options));
556 video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
557 sizeof(asset->ff_video_options)-1, asset->ff_video_options);
558 video_options->create_objects();
559 add_subwindow(new BC_OKButton(this));
560 add_subwindow(new BC_CancelButton(this));
563 if( asset->ff_video_bitrate )
565 if( asset->ff_video_quality )
570 int FFMPEGConfigVideo::close_event()
576 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
577 int x, int y, int w, int rows, int size, char *text)
578 : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
580 this->video_popup = video_popup;
583 int FFVideoOptions::handle_event()
585 strcpy(video_popup->asset->ff_video_options, get_text());
590 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
591 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
596 int FFMPEGConfigVideoPopup::handle_event()
598 strcpy(popup->asset->vcodec, get_text());
599 Asset *asset = popup->asset;
600 char option_path[BCTEXTLEN];
601 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
602 FFMPEG::load_options(option_path, asset->ff_video_options,
603 sizeof(asset->ff_video_options));
604 popup->video_options->update(asset->ff_video_options);
609 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
610 char *title_text, int x, int y, int *output)
611 : BC_CheckBox(x, y, *output, title_text)
614 this->output = output;
616 int FFMPEGConfigVideoToggle::handle_event()
618 *output = get_value();
622 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
623 const char *title, int64_t length, int64_t *position, int *canceled)
626 this->index_file = index_file;
627 this->progress_bar = progress_bar;
628 strcpy(this->progress_title, title);
629 this->length = length;
630 this->position = position;
631 this->canceled = canceled;
636 FFMPEGScanProgress::~FFMPEGScanProgress()
643 void FFMPEGScanProgress::run()
646 if( progress_bar->update(*position) ) {
647 *canceled = done = 1;
650 index_file->redraw_edits(0);
655 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
658 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
660 IndexState *index_state = index_file->get_state();
661 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
662 index_state->reset_index();
663 index_state->reset_markers();
664 index_state->index_status = INDEX_BUILDING;
666 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
667 FFAudioStream *aud = ff->ffaudio[aidx];
668 index_state->add_audio_stream(aud->channels, aud->length);
672 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
673 char *index_path = index_file->index_filename;
676 int64_t scan_position = 0;
677 FFMPEGScanProgress *scan_progress = 0;
679 char progress_title[BCTEXTLEN];
680 sprintf(progress_title, _("Creating %s\n"), index_path);
681 progress_bar->update_title(progress_title, 1);
682 progress_bar->update_length(file_bytes);
683 scan_progress = new FFMPEGScanProgress(index_file,
684 progress_bar, progress_title, file_bytes,
685 &scan_position, &canceled);
688 index_state->index_bytes = file_bytes;
689 index_state->init_scan(file->preferences->index_size);
691 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
692 index_state->reset_index();
693 index_state->reset_markers();
697 delete scan_progress;
698 if( canceled ) return 1;
700 index_state->marker_status = MARKERS_READY;
701 return index_state->create_index(index_path, asset);
706 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
707 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
710 update(); // init col/wid/columns
714 ~FFOptions_OptPanel()
718 void FFOptions_OptPanel::create_objects()
720 const char *cols[] = { "option", "value", };
721 const int col1_w = 150;
722 int wids[] = { col1_w, get_w()-col1_w };
723 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
726 int FFOptions_OptPanel::update()
730 FFOptions &options = fwin->options;
731 for( int i=0; i<options.size(); ++i ) {
732 FFOptions_Opt *opt = options[i];
733 opts.append(opt->item_name);
734 vals.append(opt->item_value);
740 int FFOptions_OptPanel::cursor_leave_event()
747 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
753 FFOptions_OptName::~FFOptions_OptName()
757 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
763 void FFOptions_OptValue::update()
766 char val[BCTEXTLEN]; val[0] = 0;
767 opt->get(val, sizeof(val));
771 void FFOptions_OptValue::update(const char *v)
776 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
778 this->options = options;
780 item_name = new FFOptions_OptName(this, nm);
781 item_value = new FFOptions_OptValue(this);
784 FFOptions_Opt::~FFOptions_Opt()
790 char *FFOptions_Opt::get(char *vp, int sz)
794 if( !opt ) return cp;
796 void *obj = (void *)options->obj;
798 if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
799 const char *val = (const char *)bp;
800 if( opt->unit && *val ) {
802 const char *uid = unit_name(id);
805 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
806 if( sz > 0 ) vp[sz-1] = 0;
813 void FFOptions_Opt::set(const char *val)
815 void *obj = (void *)options->obj;
816 if( !obj || !opt ) return;
817 av_opt_set(obj, opt->name, val, 0);
821 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
828 FFOptionsKindItem::~FFOptionsKindItem()
832 int FFOptionsKindItem::handle_event()
834 FFOptionsWindow *fwin = kind->fwin;
835 FFOptions &options = fwin->options;
836 options.initialize(fwin, idx);
841 const char *FFOptionsKind::kinds[] = {
842 N_("codec"), // FF_KIND_CODEC
843 N_("ffmpeg"), // FF_KIND_FFMPEG
847 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
848 : BC_PopupMenu(x, y, w-calculate_w(0), "")
858 void FFOptionsKind::create_objects()
860 for( int i=0; i<(int)(sizeof(kinds)/sizeof(kinds[0])); ++i )
861 add_item(new FFOptionsKindItem(this, _(kinds[i]), i));
864 int FFOptionsKind::handle_event()
869 void FFOptionsKind::set(int k)
876 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
877 : BC_TextBox(x, y, w, 1, (char *)"")
887 int FFOptionsText::handle_event()
893 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
894 : BC_PopupMenu(x, y, w, "")
904 int FFOptionsUnits::handle_event()
906 const char *text = get_text();
907 if( text && fwin->selected ) {
908 fwin->selected->set(text);
909 fwin->selected->item_value->update();
910 av_dict_set(&fwin->dialog->ff_opts,
911 fwin->selected->item_name->get_text(),
912 fwin->selected->item_value->get_text(), 0);
919 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
920 : BC_GenericButton(x, y, _("Apply"))
930 int FFOptionsApply::handle_event()
932 const char *text = fwin->text->get_text();
933 if( text && fwin->selected ) {
934 fwin->selected->set(text);
935 fwin->selected->item_value->update();
936 av_dict_set(&fwin->dialog->ff_opts,
937 fwin->selected->item_name->get_text(),
938 fwin->selected->item_value->get_text(), 0);
944 FFOptions::FFOptions()
950 FFOptions::~FFOptions()
952 remove_all_objects();
953 avcodec_free_context(&avctx);
956 int FFOptions::cmpr(const void *a, const void *b)
958 FFOptions_Opt *ap = *(FFOptions_Opt **)a;
959 FFOptions_Opt *bp = *(FFOptions_Opt **)b;
960 const char *vap = ap->item_name->get_text();
961 const char *vbp = bp->item_name->get_text();
962 return strcmp(vap, vbp);
965 void FFOptions::initialize(FFOptionsWindow *win, int kind)
967 remove_all_objects();
972 avctx = avcodec_alloc_context3(win->dialog->codec);
976 obj = (const void *)avctx->priv_data;
979 obj = (const void *)avctx;
984 FFOptions &conf = *this;
985 const AVOption *opt = 0;
986 while( (opt=av_opt_next(obj, opt)) != 0 ) {
987 if( opt->type == AV_OPT_TYPE_CONST ) continue;
989 for( int i=0; !dupl && i<size(); ++i ) {
990 FFOptions_Opt *fopt = conf[i];
991 const AVOption *op = fopt->opt;
992 if( op->offset != opt->offset ) continue;
993 if( op->type != opt->type ) continue;
995 if( strlen(op->name) < strlen(opt->name) )
999 FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1001 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1002 fopt->item_value->update(vp);
1006 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1007 win->kind->set(kind);
1008 win->panel->update();
1009 win->panel->set_yposition(0);
1012 int FFOptions::update()
1015 FFOptions &conf = *this;
1017 for( int i=0; i<size(); ++i ) {
1018 FFOptions_Opt *fopt = conf[i];
1019 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1020 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1021 fopt->item_value->update(val);
1027 void FFOptions::dump(FILE *fp)
1030 const AVOption *opt = 0;
1031 FFOptions &conf = *this;
1033 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1034 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1036 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1037 if( k < 0 ) continue;
1038 FFOptions_Opt *fopt = conf[k];
1039 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1040 fprintf(fp, " %s:=%s", opt->name, vp);
1042 char unt[BCTEXTLEN], *up = unt;
1044 fprintf(fp, "%s", unt);
1051 void FFOptionsWindow::update(FFOptions_Opt *opt)
1053 if( selected != opt ) {
1054 if( selected ) selected->item_name->set_selected(0);
1056 if( selected ) selected->item_name->set_selected(1);
1058 clear_box(0,0, 0,panel->get_y());
1059 char str[BCTEXTLEN], *sp;
1061 if( opt ) opt->types(sp);
1064 if( opt ) opt->ranges(sp);
1066 while( units->total_items() ) units->del_item(0);
1067 char unit[BCSTRLEN]; strcpy(unit, "()");
1068 if( opt && opt->opt ) {
1069 ArrayList<const char *> names;
1071 if( opt->opt->unit ) {
1072 n = opt->units(names);
1073 if( n > 0 ) strcpy(unit,opt->opt->unit);
1075 for( int i=0; i<n; ++i )
1076 units->add_item(new BC_MenuItem(names[i], 0));
1078 units->set_text(unit);
1079 char val[BCTEXTLEN]; val[0] = 0;
1080 if( opt ) opt->get(val, sizeof(val));
1086 void FFOptions_OptPanel::show_tip(const char *tip)
1089 int len = strlen(tip);
1090 if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1091 strncpy(tip_text,tip,len);
1093 int line_limit = 60;
1094 int limit2 = line_limit/2;
1095 int limit4 = line_limit/4-2;
1096 char *cp = tip_text, *dp = cp+len;
1097 int n; char *bp, *ep, *pp, *sp;
1099 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1100 // target about half remaining line, constrain line_limit
1101 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1103 // search for last punct, last space before line_limit
1104 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1105 if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1106 else if( isspace(*cp) ) sp = cp;
1110 // first, after punctuation
1111 if( pp && pp-bp >= limit4 )
1117 // last, on next space
1119 while( cp<dp && !isspace(*cp) ) ++cp;
1126 fwin->panel->set_tooltip(tip_text);
1127 fwin->panel->show_tooltip();
1130 int FFOptions_OptPanel::selection_changed()
1132 FFOptions_Opt *opt = 0;
1133 BC_ListBoxItem *item = get_selection(0, 0);
1135 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1136 opt = opt_name->opt;
1139 if( opt ) show_tip(opt->tip());
1144 int FFOptions_Opt::types(char *rp)
1146 const char *cp = "";
1147 if( opt ) switch (opt->type) {
1148 case AV_OPT_TYPE_FLAGS: cp = "<flags>"; break;
1149 case AV_OPT_TYPE_INT: cp = "<int>"; break;
1150 case AV_OPT_TYPE_INT64: cp = "<int64>"; break;
1151 case AV_OPT_TYPE_DOUBLE: cp = "<double>"; break;
1152 case AV_OPT_TYPE_FLOAT: cp = "<float>"; break;
1153 case AV_OPT_TYPE_STRING: cp = "<string>"; break;
1154 case AV_OPT_TYPE_RATIONAL: cp = "<rational>"; break;
1155 case AV_OPT_TYPE_BINARY: cp = "<binary>"; break;
1156 case AV_OPT_TYPE_IMAGE_SIZE: cp = "<image_size>"; break;
1157 case AV_OPT_TYPE_VIDEO_RATE: cp = "<video_rate>"; break;
1158 case AV_OPT_TYPE_PIXEL_FMT: cp = "<pix_fmt>"; break;
1159 case AV_OPT_TYPE_SAMPLE_FMT: cp = "<sample_fmt>"; break;
1160 case AV_OPT_TYPE_DURATION: cp = "<duration>"; break;
1161 case AV_OPT_TYPE_COLOR: cp = "<color>"; break;
1162 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = "<channel_layout>"; break;
1163 case AV_OPT_TYPE_BOOL: cp = "<bool>"; break;
1164 default: cp = "<undef>"; break;
1166 return sprintf(rp, "%s", cp);
1168 int FFOptions_Opt::scalar(double d, char *rp)
1171 if( d == INT_MAX ) cp = "INT_MAX";
1172 else if( d == INT_MIN ) cp = "INT_MIN";
1173 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1174 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1175 else if( d == INT64_MIN ) cp = "I64_MIN";
1176 else if( d == FLT_MAX ) cp = "FLT_MAX";
1177 else if( d == FLT_MIN ) cp = "FLT_MIN";
1178 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1179 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1180 else if( d == DBL_MAX ) cp = "DBL_MAX";
1181 else if( d == DBL_MIN ) cp = "DBL_MIN";
1182 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1183 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1184 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1185 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1186 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1187 else return sprintf(rp, "%g", d);
1188 return sprintf(rp, "%s", cp);
1191 int FFOptions_Opt::ranges(char *rp)
1193 if( !opt ) return 0;
1194 void *obj = (void *)options->obj;
1195 if( !obj ) return 0;
1197 switch (opt->type) {
1198 case AV_OPT_TYPE_INT:
1199 case AV_OPT_TYPE_INT64:
1200 case AV_OPT_TYPE_DOUBLE:
1201 case AV_OPT_TYPE_FLOAT: break;
1204 AVOptionRanges *r = 0;
1206 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1207 for( int i=0; i<r->nb_ranges; ++i ) {
1208 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
1209 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
1210 cp += sprintf(cp, ")");
1212 av_opt_freep_ranges(&r);
1216 int FFOptions_Opt::units(ArrayList<const char *> &names)
1218 if( !opt || !opt->unit ) return 0;
1219 const void *obj = options->obj;
1220 if( !obj ) return 0;
1222 ArrayList<const AVOption *> opts;
1223 const AVOption *opt = NULL;
1224 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1225 if( !opt->unit ) continue;
1226 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1227 if( strcmp(this->opt->unit, opt->unit) ) continue;
1228 int i = opts.size();
1230 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1231 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1234 if( i >= 0 ) continue;
1238 for( int i=0; i<opts.size(); ++i )
1239 names.append(opts[i]->name);
1241 return names.size();
1244 int FFOptions_Opt::units(char *rp)
1246 ArrayList<const char *> names;
1247 int n = units(names);
1250 cp += sprintf(cp, " [%s:", this->opt->unit);
1251 for( int i=0; i<n; ++i )
1252 cp += sprintf(cp, " %s", names[i]);
1253 cp += sprintf(cp, "]:");
1257 const char *FFOptions_Opt::unit_name(int id)
1259 if( !opt || !opt->unit ) return 0;
1260 const void *obj = options->obj;
1261 if( !obj ) return 0;
1263 const char *ret = 0;
1264 const AVOption *opt = NULL;
1265 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1266 if( !opt->unit ) continue;
1267 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1268 if( strcmp(this->opt->unit, opt->unit) ) continue;
1269 if( opt->default_val.i64 != id ) continue;
1270 if( !ret ) { ret = opt->name; continue; }
1271 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1277 const char *FFOptions_Opt::tip()
1279 return !opt ? 0 : opt->help;
1283 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog)
1284 : BC_Window(_(PROGRAM_NAME ": Options"), 60, 30, 640, 400)
1286 this->dialog = dialog;
1290 FFOptionsWindow::~FFOptionsWindow()
1294 void FFOptionsWindow::create_objects()
1297 int x0 = 10, y0 = 10;
1299 add_subwindow(title = new BC_Title(x, y, _("Format: ")));
1300 x += title->get_w();
1301 add_subwindow(new BC_Title(x, y, dialog->format_name));
1303 add_subwindow(title = new BC_Title(x, y, _("Codec: ")));
1304 x += title->get_w();
1305 add_subwindow(new BC_Title(x, y, dialog->codec_name));
1307 x = x0; y += title->get_h() + 10; y0 = y;
1308 add_subwindow(title = new BC_Title(x, y, _("Type: ")));
1309 x += title->get_w() + 8;
1310 add_subwindow(type = new BC_Title(x, y, (char *)""));
1312 add_subwindow(title = new BC_Title(x, y, _("Range: ")));
1313 x += title->get_w() + 8;
1314 add_subwindow(range = new BC_Title(x, y, (char *)""));
1316 x = x0; y += title->get_h() + 10;
1317 add_subwindow(units = new FFOptionsUnits(this, x, y, 120));
1318 x += units->get_w() + 8;
1319 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
1320 add_subwindow(text = new FFOptionsText(this, x, y, x1-x - 8));
1321 add_subwindow(apply = new FFOptionsApply(this, x1, y));
1322 y += units->get_h() + 10;
1323 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1324 kind->create_objects();
1325 const char *kind_text = _("Kind:");
1326 x1 -= BC_Title::calculate_w(this, kind_text) + 8;
1327 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1330 panel_x = x0; panel_y = y0;
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;
1377 FFOptionsDialog::~FFOptionsDialog()
1380 delete [] codec_name;
1383 void FFOptionsDialog::load_options(const char *bp, int len)
1385 char line[BCTEXTLEN];
1386 char key[BCSTRLEN], val[BCTEXTLEN];
1387 const char *dp = bp + len-1;
1389 while( bp < dp && *bp != 0 ) {
1391 char *cp = line, *ep = cp+sizeof(line)-1;
1392 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1394 if( line[0] == '#' ) {
1395 sprintf(key,"#%d", no);
1396 av_dict_set(&ff_opts, key, line, 0);
1399 if( line[0] == '\n' ) continue;
1400 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1401 av_dict_set(&ff_opts, key, val, 0);
1405 void FFOptionsDialog::store_options(char *cp, int len)
1407 char *ep = cp + len-1;
1408 AVDictionaryEntry *elem = 0;
1409 while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1410 if( elem->key[0] == '#' ) {
1411 cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1414 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1419 void FFOptionsDialog::start(const char *format_name, const char *codec_name,
1420 AVCodec *codec, const 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->format_name = cstrdup(format_name);
1430 this->codec_name = cstrdup(codec_name);
1431 this->codec = codec;
1434 load_options(options, len);
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)
1449 char options[ff_len];
1450 store_options(options, ff_len);
1451 update_options(options);
1454 delete [] format_name; format_name = 0;
1455 delete [] codec_name; codec_name = 0;
1456 av_dict_free(&ff_opts);
1459 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1461 this->aud_config = aud_config;
1464 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1469 void FFOptionsAudioDialog::update_options(const char *options)
1471 aud_config->audio_options->update(options);
1474 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1476 this->vid_config = vid_config;
1479 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1484 void FFOptionsVideoDialog::update_options(const char *options)
1486 vid_config->video_options->update(options);
1490 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text)
1491 : BC_GenericButton(x, y, text)
1493 this->aud_config = aud_config;
1496 FFOptionsViewAudio::~FFOptionsViewAudio()
1500 int FFOptionsViewAudio::handle_event()
1502 char audio_format[BCSTRLEN]; audio_format[0] = 0;
1503 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1505 Asset *asset = aud_config->asset;
1506 const char *name = asset->acodec;
1507 if( !FFMPEG::get_format(audio_format, "audio", name) &&
1508 !FFMPEG::get_codec(audio_codec, "audio", name) )
1509 codec = avcodec_find_encoder_by_name(audio_codec);
1511 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1514 aud_config->ff_options_dialog->start(audio_format, audio_codec, codec,
1515 asset->ff_audio_options, sizeof(asset->ff_audio_options));
1519 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text)
1520 : BC_GenericButton(x, y, text)
1522 this->vid_config = vid_config;
1525 FFOptionsViewVideo::~FFOptionsViewVideo()
1529 int FFOptionsViewVideo::handle_event()
1531 char video_format[BCSTRLEN]; video_format[0] = 0;
1532 char video_codec[BCSTRLEN]; video_codec[0] = 0;
1534 Asset *asset = vid_config->asset;
1535 const char *name = asset->vcodec;
1536 if( !FFMPEG::get_format(video_format, "video", name) &&
1537 !FFMPEG::get_codec(video_codec, "video", name) )
1538 codec = avcodec_find_encoder_by_name(video_codec);
1540 eprintf(_("no codec named: %s: %s"), name, video_codec);
1543 vid_config->ff_options_dialog->start(video_format, video_codec, codec,
1544 asset->ff_video_options, sizeof(asset->ff_video_options));