7 // work around for __STDC_CONSTANT_MACROS
11 #include "bcwindowbase.h"
12 #include "bitspopup.h"
17 #include "fileffmpeg.h"
18 #include "filesystem.h"
19 #include "indexfile.h"
21 #include "mainerror.h"
22 #include "mainprogress.h"
24 #include "preferences.h"
25 #include "videodevice.inc"
27 FileFFMPEG::FileFFMPEG(Asset *asset, File *file)
28 : FileBase(asset, file)
31 if(asset->format == FILE_UNKNOWN)
32 asset->format = FILE_FFMPEG;
35 FileFFMPEG::~FileFFMPEG()
41 FFMpegConfigNum::FFMpegConfigNum(BC_Window *window,
42 int x, int y, char *title_text, int *output)
43 : BC_TumbleTextBox(window, *output, -1, INT_MAX, 100, y, 100)
45 this->window = window;
46 this->x = x; this->y = y;
47 this->title_text = title_text;
48 this->output = output;
51 FFMpegConfigNum::~FFMpegConfigNum()
55 void FFMpegConfigNum::create_objects()
57 window->add_subwindow(title = new BC_Title(x, y, title_text));
58 BC_TumbleTextBox::create_objects();
61 int FFMpegConfigNum::update_param(const char *param, const char *opts)
63 char value[BCTEXTLEN];
64 if( !FileFFMPEG::get_ff_option(param, opts, value) ) {
65 if( (*output = atoi(value)) < 0 ) {
70 BC_TumbleTextBox::update((int64_t)*output);
75 int FFMpegConfigNum::handle_event()
77 *output = atoi(get_text());
81 FFMpegAudioNum::FFMpegAudioNum(BC_Window *window,
82 int x, int y, char *title_text, int *output)
83 : FFMpegConfigNum(window, x, y, title_text, output)
87 int FFMpegAudioBitrate::handle_event()
89 int ret = FFMpegAudioNum::handle_event();
93 FFMpegVideoNum::FFMpegVideoNum(BC_Window *window,
94 int x, int y, char *title_text, int *output)
95 : FFMpegConfigNum(window, x, y, title_text, output)
99 int FFMpegVideoBitrate::handle_event()
101 int ret = FFMpegVideoNum::handle_event();
102 Asset *asset = window()->asset;
103 if( asset->ff_video_bitrate > 0 )
104 window()->quality->disable();
105 else if( !window()->quality->get_textbox()->is_hidden() )
106 window()->quality->enable();
110 int FFMpegVideoQuality::handle_event()
112 int ret = FFMpegVideoNum::handle_event();
113 Asset *asset = window()->asset;
114 if( asset->ff_video_quality >= 0 )
115 window()->bitrate->disable();
116 else if( !window()->bitrate->get_textbox()->is_hidden() )
117 window()->bitrate->enable();
121 void FileFFMPEG::set_parameters(char *cp, int len, const char *bp)
123 char *ep = cp + len-2, ch = 0;
124 while( cp < ep && *bp != 0 ) { ch = *bp++; *cp++ = ch; }
125 if( ch != '\n' ) *cp++ = '\n';
129 void FileFFMPEG::get_parameters(BC_WindowBase *parent_window,
130 Asset *asset, BC_WindowBase *&format_window,
131 int audio_options, int video_options)
134 FFMPEGConfigAudio *window = new FFMPEGConfigAudio(parent_window, asset);
135 format_window = window;
136 window->create_objects();
137 if( !window->run_window() )
138 set_parameters(asset->ff_audio_options, sizeof(asset->ff_audio_options),
139 window->audio_options->get_text());
142 else if(video_options) {
143 FFMPEGConfigVideo *window = new FFMPEGConfigVideo(parent_window, asset);
144 format_window = window;
145 window->create_objects();
146 if( !window->run_window() )
147 set_parameters(asset->ff_video_options, sizeof(asset->ff_video_options),
148 window->video_options->get_text());
153 int FileFFMPEG::check_sig(Asset *asset)
155 char *ptr = strstr(asset->path, ".pcm");
157 ptr = strstr(asset->path, ".raw");
161 int ret = !ffmpeg.init_decoder(asset->path) &&
162 !ffmpeg.open_decoder() ? 1 : 0;
166 void FileFFMPEG::get_info(char *path, char *text, int len)
170 cp += sprintf(cp, _("file path: %s\n"), path);
173 if( stat(path, &st) < 0 ) {
174 cp += sprintf(cp, _(" err: %s\n"), strerror(errno));
178 cp += sprintf(cp, _(" %jd bytes\n"), st.st_size);
180 if( !ret ) ret = ffmpeg.init_decoder(path);
181 if( !ret ) ret = ffmpeg.open_decoder();
183 cp += sprintf(cp, _("info:\n"));
184 ffmpeg.info(cp, len-(cp-text));
187 sprintf(cp, _("== open failed\n"));
190 int FileFFMPEG::get_video_info(int track, int &pid, double &framerate,
191 int &width, int &height, char *title)
194 pid = ff->ff_video_pid(track);
195 framerate = ff->ff_frame_rate(track);
196 width = ff->ff_video_width(track);
197 height = ff->ff_video_height(track);
198 if( title ) *title = 0;
202 int FileFFMPEG::get_audio_for_video(int vstream, int astream, int64_t &channel_mask)
205 return ff->ff_audio_for_video(vstream, astream, channel_mask);
208 int FileFFMPEG::select_video_stream(Asset *asset, int vstream)
210 if( !ff || !asset->video_data ) return 1;
211 asset->width = ff->ff_video_width(vstream);
212 asset->height = ff->ff_video_height(vstream);
213 if( (asset->video_length = ff->ff_video_frames(vstream)) < 2 )
214 asset->video_length = asset->video_length < 0 ? 0 : -1;
215 asset->frame_rate = ff->ff_frame_rate(vstream);
219 int FileFFMPEG::select_audio_stream(Asset *asset, int astream)
221 if( !ff || !asset->audio_data ) return 1;
222 asset->sample_rate = ff->ff_sample_rate(astream);
223 asset->audio_length = ff->ff_audio_samples(astream);
227 int FileFFMPEG::open_file(int rd, int wr)
231 ff = new FFMPEG(this);
234 result = ff->init_decoder(asset->path);
235 if( !result ) result = ff->open_decoder();
237 int audio_channels = ff->ff_total_audio_channels();
238 if( audio_channels > 0 ) {
239 asset->audio_data = 1;
240 asset->channels = audio_channels;
241 asset->sample_rate = ff->ff_sample_rate(0);
242 asset->audio_length = ff->ff_audio_samples(0);
243 strcpy(asset->acodec, ff->ff_audio_format(0));
245 int video_layers = ff->ff_total_video_layers();
246 if( video_layers > 0 ) {
247 asset->video_data = 1;
248 if( !asset->layers ) asset->layers = video_layers;
249 asset->actual_width = ff->ff_video_width(0);
250 asset->actual_height = ff->ff_video_height(0);
251 if( !asset->width ) asset->width = asset->actual_width;
252 if( !asset->height ) asset->height = asset->actual_height;
253 if( !asset->video_length &&
254 (asset->video_length = ff->ff_video_frames(0)) < 2 )
255 asset->video_length = asset->video_length < 0 ? 0 : -1;
256 if( !asset->frame_rate ) asset->frame_rate = ff->ff_frame_rate(0);
257 strcpy(asset->vcodec, ff->ff_video_format(0));
259 IndexState *index_state = asset->index_state;
260 index_state->read_markers(file->preferences->index_directory, asset->path);
264 result = ff->init_encoder(asset->path);
265 // must be in this order or dvdauthor will fail
266 if( !result && asset->video_data )
267 result = ff->open_encoder("video", asset->vcodec);
268 if( !result && asset->audio_data )
269 result = ff->open_encoder("audio", asset->acodec);
274 int FileFFMPEG::close_file()
282 int FileFFMPEG::write_samples(double **buffer, int64_t len)
284 if( !ff || len < 0 ) return -1;
286 int ret = ff->encode(stream, buffer, len);
290 int FileFFMPEG::write_frames(VFrame ***frames, int len)
293 int ret = 0, layer = 0;
294 for(int i = 0; i < 1; i++) {
295 for(int j = 0; j < len && !ret; j++) {
296 VFrame *frame = frames[i][j];
297 ret = ff->encode(layer, frame);
304 int FileFFMPEG::read_samples(double *buffer, int64_t len)
306 if( !ff || len < 0 ) return -1;
307 int ch = file->current_channel;
308 int64_t pos = file->current_sample;
309 int ret = ff->decode(ch, pos, buffer, len);
310 if( ret > 0 ) return 0;
311 memset(buffer,0,len*sizeof(*buffer));
315 int FileFFMPEG::read_frame(VFrame *frame)
318 int layer = file->current_layer;
319 int64_t pos = asset->video_length >= 0 ? file->current_frame : 0;
320 int ret = ff->decode(layer, pos, frame);
321 frame->set_status(ret);
322 if( ret >= 0 ) return 0;
323 frame->clear_frame();
328 int64_t FileFFMPEG::get_memory_usage()
333 int FileFFMPEG::colormodel_supported(int colormodel)
339 int FileFFMPEG::get_best_colormodel(int driver, int vstream)
341 if( vstream < 0 ) vstream = 0;
342 int is_mpeg = !ff ? 0 : ff->ff_video_mpeg_color_range(vstream);
346 case PLAYBACK_X11_GL: return is_mpeg ? BC_YUV888 : BC_RGB888;
347 case PLAYBACK_X11_XV: return BC_YUV420P;
353 int FileFFMPEG::get_best_colormodel(Asset *asset, int driver)
356 // the direct X11 color model requires scaling in the codec
359 case PLAYBACK_X11_GL: return BC_RGB888;
360 case PLAYBACK_X11_XV: return BC_YUV420P;
366 int FileFFMPEG::can_render(const char *fformat, const char *type)
369 char option_path[BCTEXTLEN];
370 FFMPEG::set_option_path(option_path, type);
371 fs.update(option_path);
372 int total_files = fs.total_files();
373 for( int i=0; i<total_files; ++i ) {
374 const char *name = fs.get_entry(i)->get_name();
375 const char *ext = strrchr(name,'.');
377 if( !strcmp(fformat, ++ext) ) return 1;
382 int FileFFMPEG::get_ff_option(const char *nm, const char *options, char *value)
384 for( const char *cp=options; *cp!=0; ) {
385 char line[BCTEXTLEN], *bp = line, *ep = bp+sizeof(line)-1;
386 while( bp < ep && *cp && *cp!='\n' ) *bp++ = *cp++;
389 if( !line[0] || line[0] == '#' || line[0] == ';' ) continue;
390 char key[BCSTRLEN], val[BCTEXTLEN];
391 if( FFMPEG::scan_option_line(line, key, val) ) continue;
392 if( !strcmp(key, nm) ) {
402 FFMPEGConfigAudio::FFMPEGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
403 : BC_Window(_(PROGRAM_NAME ": Audio Preset"),
404 parent_window->get_abs_cursor_x(1),
405 parent_window->get_abs_cursor_y(1),
408 this->parent_window = parent_window;
414 ff_options_dialog = 0;
417 FFMPEGConfigAudio::~FFMPEGConfigAudio()
419 delete ff_options_dialog;
420 lock_window("FFMPEGConfigAudio::~FFMPEGConfigAudio");
422 presets.remove_all_objects();
426 void FFMPEGConfigAudio::create_objects()
429 lock_window("FFMPEGConfigAudio::create_objects");
432 char option_path[BCTEXTLEN];
433 FFMPEG::set_option_path(option_path, "audio");
434 fs.update(option_path);
435 int total_files = fs.total_files();
436 for(int i = 0; i < total_files; i++) {
437 const char *name = fs.get_entry(i)->get_name();
438 if( asset->fformat[0] != 0 ) {
439 const char *ext = strrchr(name,'.');
441 if( strcmp(asset->fformat, ++ext) ) continue;
443 presets.append(new BC_ListBoxItem(name));
446 if( asset->acodec[0] ) {
447 int k = presets.size();
448 while( --k >= 0 && strcmp(asset->acodec, presets[k]->get_text()) );
449 if( k < 0 ) asset->acodec[0] = 0;
452 if( !asset->acodec[0] && presets.size() > 0 )
453 strcpy(asset->acodec, presets[0]->get_text());
455 add_tool(new BC_Title(x, y, _("Preset:")));
457 preset_popup = new FFMPEGConfigAudioPopup(this, x, y);
458 preset_popup->create_objects();
461 bitrate = new FFMpegAudioBitrate(this, x, y, _("Bitrate:"), &asset->ff_audio_bitrate);
462 bitrate->create_objects();
463 bitrate->set_increment(1000);
464 bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
466 y += bitrate->get_h() + 10;
467 BC_Title *title = new BC_Title(x, y, _("Audio Options:"));
468 add_subwindow(title);
470 ff_options_dialog = new FFOptionsAudioDialog(this);
471 int x1 = x + title->get_w() + 8;
472 add_subwindow(new FFOptionsViewAudio(this, x1, y, _("view")));
475 if( !asset->ff_audio_options[0] && asset->acodec[0] ) {
476 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
477 FFMPEG::load_options(option_path, asset->ff_audio_options,
478 sizeof(asset->ff_audio_options));
481 audio_options = new FFAudioOptions(this, x, y, get_w()-x-20, 10,
482 sizeof(asset->ff_audio_options)-1, asset->ff_audio_options);
483 audio_options->create_objects();
484 add_subwindow(new BC_OKButton(this));
485 add_subwindow(new BC_CancelButton(this));
488 bitrate->update_param("cin_bitrate", asset->ff_audio_options);
493 int FFMPEGConfigAudio::close_event()
499 FFAudioOptions::FFAudioOptions(FFMPEGConfigAudio *audio_popup,
500 int x, int y, int w, int rows, int size, char *text)
501 : BC_ScrollTextBox(audio_popup, x, y, w, rows, text, size)
503 this->audio_popup = audio_popup;
507 FFMPEGConfigAudioPopup::FFMPEGConfigAudioPopup(FFMPEGConfigAudio *popup, int x, int y)
508 : BC_PopupTextBox(popup, &popup->presets, popup->asset->acodec, x, y, 300, 300)
513 int FFMPEGConfigAudioPopup::handle_event()
515 strcpy(popup->asset->acodec, get_text());
516 Asset *asset = popup->asset;
517 asset->ff_audio_bitrate = 0;
518 char option_path[BCTEXTLEN];
519 FFMPEG::set_option_path(option_path, "audio/%s", asset->acodec);
520 FFMPEG::load_options(option_path, asset->ff_audio_options,
521 sizeof(asset->ff_audio_options));
522 popup->audio_options->update(asset->ff_audio_options);
523 popup->audio_options->set_text_row(0);
525 popup->bitrate->update_param("cin_bitrate", asset->ff_audio_options);
530 FFMPEGConfigAudioToggle::FFMPEGConfigAudioToggle(FFMPEGConfigAudio *popup,
531 char *title_text, int x, int y, int *output)
532 : BC_CheckBox(x, y, *output, title_text)
535 this->output = output;
537 int FFMPEGConfigAudioToggle::handle_event()
539 *output = get_value();
545 FFMPEGConfigVideo::FFMPEGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
546 : BC_Window(_(PROGRAM_NAME ": Video Preset"),
547 parent_window->get_abs_cursor_x(1),
548 parent_window->get_abs_cursor_y(1),
551 this->parent_window = parent_window;
560 FFMPEGConfigVideo::~FFMPEGConfigVideo()
562 delete ff_options_dialog;
563 lock_window("FFMPEGConfigVideo::~FFMPEGConfigVideo");
564 if(preset_popup) delete preset_popup;
565 presets.remove_all_objects();
569 void FFMPEGConfigVideo::create_objects()
572 lock_window("FFMPEGConfigVideo::create_objects");
574 add_subwindow(new BC_Title(x, y, _("Compression:")));
578 char option_path[BCTEXTLEN];
579 FFMPEG::set_option_path(option_path, "video");
580 fs.update(option_path);
581 int total_files = fs.total_files();
582 for(int i = 0; i < total_files; i++) {
583 const char *name = fs.get_entry(i)->get_name();
584 if( asset->fformat[0] != 0 ) {
585 const char *ext = strrchr(name,'.');
587 if( strcmp(asset->fformat, ++ext) ) continue;
589 presets.append(new BC_ListBoxItem(name));
592 if( asset->vcodec[0] ) {
593 int k = presets.size();
594 while( --k >= 0 && strcmp(asset->vcodec, presets[k]->get_text()) );
595 if( k < 0 ) asset->vcodec[0] = 0;
598 if( !asset->vcodec[0] && presets.size() > 0 )
599 strcpy(asset->vcodec, presets[0]->get_text());
601 preset_popup = new FFMPEGConfigVideoPopup(this, x, y);
602 preset_popup->create_objects();
604 if( asset->ff_video_bitrate > 0 && asset->ff_video_quality >= 0 ) {
605 asset->ff_video_bitrate = 0; asset->ff_video_quality = -1;
609 bitrate = new FFMpegVideoBitrate(this, x, y, _("Bitrate:"), &asset->ff_video_bitrate);
610 bitrate->create_objects();
611 bitrate->set_increment(100000);
612 bitrate->set_boundaries((int64_t)0, (int64_t)INT_MAX);
613 y += bitrate->get_h() + 5;
614 quality = new FFMpegVideoQuality(this, x, y, _("Quality:"), &asset->ff_video_quality);
615 quality->create_objects();
616 quality->set_increment(1);
617 quality->set_boundaries((int64_t)-1, (int64_t)51);
619 y += quality->get_h() + 10;
620 BC_Title *title = new BC_Title(x, y, _("Video Options:"));
621 add_subwindow(title);
623 ff_options_dialog = new FFOptionsVideoDialog(this);
624 int x1 = x + title->get_w() + 8;
625 add_subwindow(new FFOptionsViewVideo(this, x1, y, _("view")));
628 if( !asset->ff_video_options[0] && asset->vcodec[0] ) {
629 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
630 FFMPEG::load_options(option_path, asset->ff_video_options,
631 sizeof(asset->ff_video_options));
634 video_options = new FFVideoOptions(this, x, y, get_w()-x-20, 10,
635 sizeof(asset->ff_video_options)-1, asset->ff_video_options);
636 video_options->create_objects();
637 add_subwindow(new BC_OKButton(this));
638 add_subwindow(new BC_CancelButton(this));
641 bitrate->update_param("cin_bitrate", asset->ff_video_options);
642 quality->update_param("cin_quality", asset->ff_video_options);
644 if( asset->ff_video_bitrate > 0 ) quality->disable();
645 else if( asset->ff_video_quality >= 0 ) bitrate->disable();
649 int FFMPEGConfigVideo::close_event()
655 FFVideoOptions::FFVideoOptions(FFMPEGConfigVideo *video_popup,
656 int x, int y, int w, int rows, int size, char *text)
657 : BC_ScrollTextBox(video_popup, x, y, w, rows, text, size)
659 this->video_popup = video_popup;
663 FFMPEGConfigVideoPopup::FFMPEGConfigVideoPopup(FFMPEGConfigVideo *popup, int x, int y)
664 : BC_PopupTextBox(popup, &popup->presets, popup->asset->vcodec, x, y, 300, 300)
669 int FFMPEGConfigVideoPopup::handle_event()
671 strcpy(popup->asset->vcodec, get_text());
672 Asset *asset = popup->asset;
673 char option_path[BCTEXTLEN];
674 asset->ff_video_bitrate = 0; asset->ff_video_quality = -1;
675 FFMPEG::set_option_path(option_path, "video/%s", asset->vcodec);
676 FFMPEG::load_options(option_path, asset->ff_video_options,
677 sizeof(asset->ff_video_options));
678 popup->video_options->update(asset->ff_video_options);
679 popup->video_options->set_text_row(0);
681 popup->bitrate->update_param("cin_bitrate", asset->ff_video_options);
682 popup->quality->update_param("cin_quality", asset->ff_video_options);
687 FFMPEGConfigVideoToggle::FFMPEGConfigVideoToggle(FFMPEGConfigVideo *popup,
688 char *title_text, int x, int y, int *output)
689 : BC_CheckBox(x, y, *output, title_text)
692 this->output = output;
694 int FFMPEGConfigVideoToggle::handle_event()
696 *output = get_value();
700 FFMPEGScanProgress::FFMPEGScanProgress(IndexFile *index_file, MainProgressBar *progress_bar,
701 const char *title, int64_t length, int64_t *position, int *canceled)
704 this->index_file = index_file;
705 this->progress_bar = progress_bar;
706 strcpy(this->progress_title, title);
707 this->length = length;
708 this->position = position;
709 this->canceled = canceled;
714 FFMPEGScanProgress::~FFMPEGScanProgress()
721 void FFMPEGScanProgress::run()
724 if( progress_bar->update(*position) ) {
725 *canceled = done = 1;
728 index_file->redraw_edits(0);
733 int FileFFMPEG::get_index(IndexFile *index_file, MainProgressBar *progress_bar)
736 if( !file->preferences->ffmpeg_marker_indexes ) return 1;
738 IndexState *index_state = index_file->get_state();
739 if( index_state->index_status != INDEX_NOTTESTED ) return 0;
740 index_state->reset_index();
741 index_state->reset_markers();
742 index_state->index_status = INDEX_BUILDING;
744 for( int aidx=0; aidx<ff->ffaudio.size(); ++aidx ) {
745 FFAudioStream *aud = ff->ffaudio[aidx];
746 index_state->add_audio_stream(aud->channels, aud->length);
750 int64_t file_bytes = fs.get_size(ff->fmt_ctx->filename);
751 char *index_path = index_file->index_filename;
754 int64_t scan_position = 0;
755 FFMPEGScanProgress *scan_progress = 0;
757 char progress_title[BCTEXTLEN];
758 sprintf(progress_title, _("Creating %s\n"), index_path);
759 progress_bar->update_title(progress_title, 1);
760 progress_bar->update_length(file_bytes);
761 scan_progress = new FFMPEGScanProgress(index_file,
762 progress_bar, progress_title, file_bytes,
763 &scan_position, &canceled);
766 index_state->index_bytes = file_bytes;
767 index_state->init_scan(file->preferences->index_size);
769 if( ff->scan(index_state, &scan_position, &canceled) || canceled ) {
770 index_state->reset_index();
771 index_state->reset_markers();
775 delete scan_progress;
776 if( canceled ) return 1;
778 index_state->marker_status = MARKERS_READY;
779 return index_state->create_index(index_path, asset);
784 FFOptions_OptPanel(FFOptionsWindow *fwin, int x, int y, int w, int h)
785 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
788 update(); // init col/wid/columns
792 ~FFOptions_OptPanel()
796 void FFOptions_OptPanel::create_objects()
798 const char *cols[] = { _("option"), _("value"), };
799 const int col1_w = 150;
800 int wids[] = { col1_w, get_w()-col1_w };
801 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
804 int FFOptions_OptPanel::update()
808 FFOptions &options = fwin->options;
809 for( int i=0; i<options.size(); ++i ) {
810 FFOptions_Opt *opt = options[i];
811 opts.append(opt->item_name);
812 vals.append(opt->item_value);
818 int FFOptions_OptPanel::cursor_leave_event()
825 FFOptions_OptName::FFOptions_OptName(FFOptions_Opt *opt, const char *nm)
831 FFOptions_OptName::~FFOptions_OptName()
835 FFOptions_OptValue::FFOptions_OptValue(FFOptions_Opt *opt)
841 void FFOptions_OptValue::update()
844 char val[BCTEXTLEN]; val[0] = 0;
845 opt->get(val, sizeof(val));
849 void FFOptions_OptValue::update(const char *v)
854 FFOptions_Opt::FFOptions_Opt(FFOptions *options, const AVOption *opt, const char *nm)
856 this->options = options;
858 item_name = new FFOptions_OptName(this, nm);
859 item_value = new FFOptions_OptValue(this);
862 FFOptions_Opt::~FFOptions_Opt()
868 char *FFOptions_Opt::get(char *vp, int sz)
872 if( !opt ) return cp;
874 void *obj = (void *)options->obj;
876 if( av_opt_get(obj, opt->name, 0, &bp) >= 0 && bp != 0 ) {
877 const char *val = (const char *)bp;
878 if( opt->unit && *val ) {
880 const char *uid = unit_name(id);
883 cp = sz >= 0 ? strncpy(vp,val,sz) : strcpy(vp, val);
884 if( sz > 0 ) vp[sz-1] = 0;
891 void FFOptions_Opt::set(const char *val)
893 void *obj = (void *)options->obj;
894 if( !obj || !opt ) return;
895 av_opt_set(obj, opt->name, val, 0);
899 FFOptionsKindItem::FFOptionsKindItem(FFOptionsKind *kind, const char *text, int idx)
906 FFOptionsKindItem::~FFOptionsKindItem()
910 int FFOptionsKindItem::handle_event()
912 FFOptionsWindow *fwin = kind->fwin;
913 FFOptions &options = fwin->options;
914 options.initialize(fwin, idx);
919 const char *FFOptionsKind::kinds[] = {
920 N_("codec"), // FF_KIND_CODEC
921 N_("ffmpeg"), // FF_KIND_FFMPEG
925 FFOptionsKind(FFOptionsWindow *fwin, int x, int y, int w)
926 : BC_PopupMenu(x, y, w-calculate_w(0), "")
936 void FFOptionsKind::create_objects()
938 for( int i=0; i<(int)(sizeof(kinds)/sizeof(kinds[0])); ++i )
939 add_item(new FFOptionsKindItem(this, _(kinds[i]), i));
942 int FFOptionsKind::handle_event()
947 void FFOptionsKind::set(int k)
950 set_text(_(kinds[k]));
954 FFOptionsText(FFOptionsWindow *fwin, int x, int y, int w)
955 : BC_TextBox(x, y, w, 1, (char *)"")
965 int FFOptionsText::handle_event()
971 FFOptionsUnits(FFOptionsWindow *fwin, int x, int y, int w)
972 : BC_PopupMenu(x, y, w, "")
982 int FFOptionsUnits::handle_event()
984 const char *text = get_text();
985 if( text && fwin->selected ) {
986 fwin->selected->set(text);
987 fwin->selected->item_value->update();
988 av_dict_set(&fwin->dialog->ff_opts,
989 fwin->selected->item_name->get_text(),
990 fwin->selected->item_value->get_text(), 0);
997 FFOptionsApply(FFOptionsWindow *fwin, int x, int y)
998 : BC_GenericButton(x, y, _("Apply"))
1008 int FFOptionsApply::handle_event()
1010 const char *text = fwin->text->get_text();
1011 if( text && fwin->selected ) {
1012 fwin->selected->set(text);
1013 fwin->selected->item_value->update();
1014 av_dict_set(&fwin->dialog->ff_opts,
1015 fwin->selected->item_name->get_text(),
1016 fwin->selected->item_value->get_text(), 0);
1022 FFOptions::FFOptions()
1028 FFOptions::~FFOptions()
1030 remove_all_objects();
1031 avcodec_free_context(&avctx);
1034 int FFOptions::cmpr(const void *a, const void *b)
1036 FFOptions_Opt *ap = *(FFOptions_Opt **)a;
1037 FFOptions_Opt *bp = *(FFOptions_Opt **)b;
1038 const char *vap = ap->item_name->get_text();
1039 const char *vbp = bp->item_name->get_text();
1040 return strcmp(vap, vbp);
1043 void FFOptions::initialize(FFOptionsWindow *win, int kind)
1045 remove_all_objects();
1050 avctx = avcodec_alloc_context3(win->dialog->codec);
1054 obj = (const void *)avctx->priv_data;
1056 case FF_KIND_FFMPEG:
1057 obj = (const void *)avctx;
1062 FFOptions &conf = *this;
1063 const AVOption *opt = 0;
1064 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1065 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1067 for( int i=0; !dupl && i<size(); ++i ) {
1068 FFOptions_Opt *fopt = conf[i];
1069 const AVOption *op = fopt->opt;
1070 if( op->offset != opt->offset ) continue;
1071 if( op->type != opt->type ) continue;
1073 if( strlen(op->name) < strlen(opt->name) )
1076 if( dupl ) continue;
1077 FFOptions_Opt *fopt = new FFOptions_Opt(this, opt, opt->name);
1079 AVDictionaryEntry *elem = av_dict_get(win->dialog->ff_opts,
1080 opt->name, 0, AV_DICT_IGNORE_SUFFIX);
1081 if( elem && elem->value ) fopt->set(elem->value);
1082 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1083 fopt->item_value->update(vp);
1087 qsort(&values[0],size(),sizeof(values[0]),cmpr);
1088 win->kind->set(kind);
1089 win->panel->update();
1090 win->panel->set_yposition(0);
1093 int FFOptions::update()
1096 FFOptions &conf = *this;
1098 for( int i=0; i<size(); ++i ) {
1099 FFOptions_Opt *fopt = conf[i];
1100 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
1101 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
1102 fopt->item_value->update(val);
1108 void FFOptions::dump(FILE *fp)
1111 const AVOption *opt = 0;
1112 FFOptions &conf = *this;
1114 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1115 if( opt->type == AV_OPT_TYPE_CONST ) continue;
1117 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
1118 if( k < 0 ) continue;
1119 FFOptions_Opt *fopt = conf[k];
1120 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
1121 fprintf(fp, " %s:=%s", opt->name, vp);
1123 char unt[BCTEXTLEN], *up = unt;
1125 fprintf(fp, "%s", unt);
1132 void FFOptionsWindow::update(FFOptions_Opt *opt)
1134 if( selected != opt ) {
1135 if( selected ) selected->item_name->set_selected(0);
1137 if( selected ) selected->item_name->set_selected(1);
1139 clear_box(0,0, 0,panel->get_y());
1140 char str[BCTEXTLEN], *sp;
1142 if( opt ) opt->types(sp);
1145 if( opt ) opt->ranges(sp);
1147 while( units->total_items() ) units->del_item(0);
1148 char unit[BCSTRLEN]; strcpy(unit, "()");
1149 if( opt && opt->opt ) {
1150 ArrayList<const char *> names;
1152 if( opt->opt->unit ) {
1153 n = opt->units(names);
1154 if( n > 0 ) strcpy(unit,opt->opt->unit);
1156 for( int i=0; i<n; ++i )
1157 units->add_item(new BC_MenuItem(names[i], 0));
1159 units->set_text(unit);
1160 char val[BCTEXTLEN]; val[0] = 0;
1161 if( opt ) opt->get(val, sizeof(val));
1167 void FFOptions_OptPanel::show_tip(const char *tip)
1170 int len = strlen(tip);
1171 if( len > (int)sizeof(tip_text)-1 ) len = sizeof(tip_text)-1;
1172 strncpy(tip_text,tip,len);
1174 int line_limit = 60;
1175 int limit2 = line_limit/2;
1176 int limit4 = line_limit/4-2;
1177 char *cp = tip_text, *dp = cp+len;
1178 int n; char *bp, *ep, *pp, *sp;
1180 for( ep=cp; ep<dp && *ep!='\n'; ++ep );
1181 // target about half remaining line, constrain line_limit
1182 if( (n=(ep-1-cp)/2) < limit2 || n > line_limit )
1184 // search for last punct, last space before line_limit
1185 for( bp=cp, pp=sp=0; --n>=0 && cp<ep; ++cp ) {
1186 if( ispunct(*cp) && isspace(*(cp+1)) ) pp = cp;
1187 else if( isspace(*cp) ) sp = cp;
1191 // first, after punctuation
1192 if( pp && pp-bp >= limit4 )
1198 // last, on next space
1200 while( cp<dp && !isspace(*cp) ) ++cp;
1207 fwin->panel->set_tooltip(tip_text);
1208 fwin->panel->show_tooltip();
1211 int FFOptions_OptPanel::selection_changed()
1213 FFOptions_Opt *opt = 0;
1214 BC_ListBoxItem *item = get_selection(0, 0);
1216 FFOptions_OptName *opt_name = (FFOptions_OptName *)item;
1217 opt = opt_name->opt;
1220 if( opt ) show_tip(opt->tip());
1225 int FFOptions_Opt::types(char *rp)
1227 const char *cp = "";
1228 if( opt ) switch (opt->type) {
1229 case AV_OPT_TYPE_FLAGS: cp = N_("<flags>"); break;
1230 case AV_OPT_TYPE_INT: cp = N_("<int>"); break;
1231 case AV_OPT_TYPE_INT64: cp = N_("<int64>"); break;
1232 case AV_OPT_TYPE_DOUBLE: cp = N_("<double>"); break;
1233 case AV_OPT_TYPE_FLOAT: cp = N_("<float>"); break;
1234 case AV_OPT_TYPE_STRING: cp = N_("<string>"); break;
1235 case AV_OPT_TYPE_RATIONAL: cp = N_("<rational>"); break;
1236 case AV_OPT_TYPE_BINARY: cp = N_("<binary>"); break;
1237 case AV_OPT_TYPE_IMAGE_SIZE: cp = N_("<image_size>"); break;
1238 case AV_OPT_TYPE_VIDEO_RATE: cp = N_("<video_rate>"); break;
1239 case AV_OPT_TYPE_PIXEL_FMT: cp = N_("<pix_fmt>"); break;
1240 case AV_OPT_TYPE_SAMPLE_FMT: cp = N_("<sample_fmt>"); break;
1241 case AV_OPT_TYPE_DURATION: cp = N_("<duration>"); break;
1242 case AV_OPT_TYPE_COLOR: cp = N_("<color>"); break;
1243 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = N_("<channel_layout>"); break;
1244 case AV_OPT_TYPE_BOOL: cp = N_("<bool>"); break;
1245 default: cp = N_("<undef>"); break;
1247 return sprintf(rp, "%s", _(cp));
1249 int FFOptions_Opt::scalar(double d, char *rp)
1252 if( d == INT_MAX ) cp = "INT_MAX";
1253 else if( d == INT_MIN ) cp = "INT_MIN";
1254 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
1255 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
1256 else if( d == INT64_MIN ) cp = "I64_MIN";
1257 else if( d == FLT_MAX ) cp = "FLT_MAX";
1258 else if( d == FLT_MIN ) cp = "FLT_MIN";
1259 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
1260 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
1261 else if( d == DBL_MAX ) cp = "DBL_MAX";
1262 else if( d == DBL_MIN ) cp = "DBL_MIN";
1263 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
1264 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
1265 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
1266 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
1267 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
1268 else return sprintf(rp, "%g", d);
1269 return sprintf(rp, "%s", cp);
1272 int FFOptions_Opt::ranges(char *rp)
1274 if( !opt ) return 0;
1275 void *obj = (void *)options->obj;
1276 if( !obj ) return 0;
1278 switch (opt->type) {
1279 case AV_OPT_TYPE_INT:
1280 case AV_OPT_TYPE_INT64:
1281 case AV_OPT_TYPE_DOUBLE:
1282 case AV_OPT_TYPE_FLOAT: break;
1285 AVOptionRanges *r = 0;
1287 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
1288 for( int i=0; i<r->nb_ranges; ++i ) {
1289 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
1290 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
1291 cp += sprintf(cp, ")");
1293 av_opt_freep_ranges(&r);
1297 int FFOptions_Opt::units(ArrayList<const char *> &names)
1299 if( !opt || !opt->unit ) return 0;
1300 const void *obj = options->obj;
1301 if( !obj ) return 0;
1303 ArrayList<const AVOption *> opts;
1304 const AVOption *opt = NULL;
1305 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1306 if( !opt->unit ) continue;
1307 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1308 if( strcmp(this->opt->unit, opt->unit) ) continue;
1309 int i = opts.size();
1311 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
1312 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
1315 if( i >= 0 ) continue;
1319 for( int i=0; i<opts.size(); ++i )
1320 names.append(opts[i]->name);
1322 return names.size();
1325 int FFOptions_Opt::units(char *rp)
1327 ArrayList<const char *> names;
1328 int n = units(names);
1331 cp += sprintf(cp, " [%s:", this->opt->unit);
1332 for( int i=0; i<n; ++i )
1333 cp += sprintf(cp, " %s", names[i]);
1334 cp += sprintf(cp, "]:");
1338 const char *FFOptions_Opt::unit_name(int id)
1340 if( !opt || !opt->unit ) return 0;
1341 const void *obj = options->obj;
1342 if( !obj ) return 0;
1344 const char *ret = 0;
1345 const AVOption *opt = NULL;
1346 while( (opt=av_opt_next(obj, opt)) != 0 ) {
1347 if( !opt->unit ) continue;
1348 if( opt->type != AV_OPT_TYPE_CONST ) continue;
1349 if( strcmp(this->opt->unit, opt->unit) ) continue;
1350 if( opt->default_val.i64 != id ) continue;
1351 if( !ret ) { ret = opt->name; continue; }
1352 if( strlen(ret) < strlen(opt->name) ) ret = opt->name;
1358 const char *FFOptions_Opt::tip()
1360 return !opt ? 0 : opt->help;
1364 FFOptionsWindow::FFOptionsWindow(FFOptionsDialog *dialog)
1365 : BC_Window(_(PROGRAM_NAME ": Options"), 60, 30, 640, 400)
1367 this->dialog = dialog;
1371 FFOptionsWindow::~FFOptionsWindow()
1375 void FFOptionsWindow::create_objects()
1378 int x0 = 10, y0 = 10;
1380 add_subwindow(title = new BC_Title(x, y, _("Format: ")));
1381 x += title->get_w();
1382 add_subwindow(new BC_Title(x, y, dialog->format_name));
1384 add_subwindow(title = new BC_Title(x, y, _("Codec: ")));
1385 x += title->get_w();
1386 add_subwindow(new BC_Title(x, y, dialog->codec_name));
1388 x = x0; y += title->get_h() + 10; y0 = y;
1389 add_subwindow(title = new BC_Title(x, y, _("Type: ")));
1390 x += title->get_w() + 8;
1391 add_subwindow(type = new BC_Title(x, y, (char *)""));
1393 add_subwindow(title = new BC_Title(x, y, _("Range: ")));
1394 x += title->get_w() + 8;
1395 add_subwindow(range = new BC_Title(x, y, (char *)""));
1397 x = x0; y += title->get_h() + 10;
1398 add_subwindow(units = new FFOptionsUnits(this, x, y, 120));
1399 x += units->get_w() + 8;
1400 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
1401 add_subwindow(text = new FFOptionsText(this, x, y, x1-x - 8));
1402 add_subwindow(apply = new FFOptionsApply(this, x1, y));
1403 y += units->get_h() + 10;
1404 add_subwindow(kind = new FFOptionsKind(this, x1, y0, apply->get_w()));
1405 kind->create_objects();
1406 const char *kind_text = _("Kind:");
1407 x1 -= BC_Title::calculate_w(this, kind_text) + 8;
1408 add_subwindow(kind_title = new BC_Title(x1, y0, kind_text));
1411 panel_x = x0; panel_y = y0;
1412 panel_w = get_w()-10 - panel_x;
1413 panel_h = get_h()-10 - panel_y - BC_OKButton::calculate_h();
1414 panel = new FFOptions_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
1415 add_subwindow(panel);
1416 add_subwindow(new BC_OKButton(this));
1417 add_subwindow(new BC_CancelButton(this));
1418 panel->create_objects();
1419 options.initialize(this, FF_KIND_CODEC);
1424 void FFOptionsWindow::draw()
1429 int FFOptionsWindow::resize_event(int w, int h)
1431 int x1 = w - 8 - kind->get_w();
1432 int y = kind->get_y();
1433 kind->reposition_window(x1, y);
1434 x1 -= kind_title->get_w() + 8;
1435 kind_title->reposition_window(x1,y);
1436 x1 = get_w() - apply->get_w() - 8;
1437 int y1 = units->get_y();
1438 apply->reposition_window(x1, y1);
1439 int x0 = units->get_x() + units->get_w() + 8;
1440 int y0 = units->get_y();
1441 text->reposition_window(x0,y0, x1-x0-8);
1442 panel_w = get_w()-10 - panel_x;
1443 panel_h = get_h()-10 - panel_y;
1444 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
1448 FFOptionsDialog::FFOptionsDialog()
1451 this->options_window = 0;
1452 this->codec_name = 0;
1458 FFOptionsDialog::~FFOptionsDialog()
1461 delete [] codec_name;
1464 void FFOptionsDialog::load_options(const char *bp, int len)
1466 char line[BCTEXTLEN];
1467 char key[BCSTRLEN], val[BCTEXTLEN];
1468 const char *dp = bp + len-1;
1470 while( bp < dp && *bp != 0 ) {
1472 char *cp = line, *ep = cp+sizeof(line)-1;
1473 while( *bp && cp<ep && (*cp=*bp++)!='\n' ) ++cp;
1475 if( line[0] == '#' ) {
1476 sprintf(key,"#%d", no);
1477 av_dict_set(&ff_opts, key, line, 0);
1480 if( line[0] == '\n' ) continue;
1481 if( FFMPEG::scan_option_line(line, key, val) ) continue;
1482 av_dict_set(&ff_opts, key, val, 0);
1486 void FFOptionsDialog::store_options(char *cp, int len)
1488 char *ep = cp + len-1;
1489 AVDictionaryEntry *elem = 0;
1490 while( (elem=av_dict_get(ff_opts, "", elem, AV_DICT_IGNORE_SUFFIX)) != 0 ) {
1491 if( elem->key[0] == '#' ) {
1492 cp += snprintf(cp,ep-cp, "%s\n", elem->value);
1495 cp += snprintf(cp,ep-cp, "%s=%s\n", elem->key, elem->value);
1500 void FFOptionsDialog::start(const char *format_name, const char *codec_name,
1501 AVCodec *codec, const char *options, int len)
1503 if( options_window ) {
1504 options_window->lock_window("FFOptionsDialog::start");
1505 options_window->raise_window();
1506 options_window->unlock_window();
1510 this->format_name = cstrdup(format_name);
1511 this->codec_name = cstrdup(codec_name);
1512 this->codec = codec;
1515 load_options(options, len);
1517 BC_DialogThread::start();
1520 BC_Window* FFOptionsDialog::new_gui()
1522 options_window = new FFOptionsWindow(this);
1523 options_window->create_objects();
1524 return options_window;
1527 void FFOptionsDialog::handle_done_event(int result)
1530 char options[ff_len];
1531 store_options(options, ff_len);
1532 update_options(options);
1535 delete [] format_name; format_name = 0;
1536 delete [] codec_name; codec_name = 0;
1537 av_dict_free(&ff_opts);
1540 FFOptionsAudioDialog::FFOptionsAudioDialog(FFMPEGConfigAudio *aud_config)
1542 this->aud_config = aud_config;
1545 FFOptionsAudioDialog::~FFOptionsAudioDialog()
1550 void FFOptionsAudioDialog::update_options(const char *options)
1552 aud_config->audio_options->update(options);
1555 FFOptionsVideoDialog::FFOptionsVideoDialog(FFMPEGConfigVideo *vid_config)
1557 this->vid_config = vid_config;
1560 FFOptionsVideoDialog::~FFOptionsVideoDialog()
1565 void FFOptionsVideoDialog::update_options(const char *options)
1567 vid_config->video_options->update(options);
1571 FFOptionsViewAudio::FFOptionsViewAudio(FFMPEGConfigAudio *aud_config, int x, int y, const char *text)
1572 : BC_GenericButton(x, y, text)
1574 this->aud_config = aud_config;
1577 FFOptionsViewAudio::~FFOptionsViewAudio()
1581 int FFOptionsViewAudio::handle_event()
1583 char audio_format[BCSTRLEN]; audio_format[0] = 0;
1584 char audio_codec[BCSTRLEN]; audio_codec[0] = 0;
1586 Asset *asset = aud_config->asset;
1587 const char *name = asset->acodec;
1588 if( !FFMPEG::get_format(audio_format, "audio", name) &&
1589 !FFMPEG::get_codec(audio_codec, "audio", name) )
1590 codec = avcodec_find_encoder_by_name(audio_codec);
1592 eprintf(_("no codec named: %s: %s"), name, audio_codec);
1595 aud_config->ff_options_dialog->start(audio_format, audio_codec, codec,
1596 asset->ff_audio_options, sizeof(asset->ff_audio_options));
1600 FFOptionsViewVideo::FFOptionsViewVideo(FFMPEGConfigVideo *vid_config, int x, int y, const char *text)
1601 : BC_GenericButton(x, y, text)
1603 this->vid_config = vid_config;
1606 FFOptionsViewVideo::~FFOptionsViewVideo()
1610 int FFOptionsViewVideo::handle_event()
1612 char video_format[BCSTRLEN]; video_format[0] = 0;
1613 char video_codec[BCSTRLEN]; video_codec[0] = 0;
1615 Asset *asset = vid_config->asset;
1616 const char *name = asset->vcodec;
1617 if( !FFMPEG::get_format(video_format, "video", name) &&
1618 !FFMPEG::get_codec(video_codec, "video", name) )
1619 codec = avcodec_find_encoder_by_name(video_codec);
1621 eprintf(_("no codec named: %s: %s"), name, video_codec);
1624 vid_config->ff_options_dialog->start(video_format, video_codec, codec,
1625 asset->ff_video_options, sizeof(asset->ff_video_options));