8 #include "bcwindowbase.h"
14 #include "pluginfclient.h"
15 #include "pluginserver.h"
21 #define av_filter_iterate(p) ((*(const AVFilter**)(p))=avfilter_next(*(const AVFilter **)(p)))
24 static void ff_err(int ret, const char *fmt, ...)
29 vsnprintf(msg, sizeof(msg), fmt, ap);
31 char errmsg[BCSTRLEN];
32 av_strerror(ret, errmsg, sizeof(errmsg));
33 fprintf(stderr,_("%s err: %s\n"),msg, errmsg);
36 PluginFClientConfig::PluginFClientConfig()
41 PluginFClientConfig::~PluginFClientConfig()
47 int PluginFClientConfig::equivalent(PluginFClientConfig &that)
49 PluginFClientConfig &conf = *this;
50 for( int i=0; i<that.size(); ++i ) {
51 PluginFClient_Opt *topt = conf[i], *vopt = that[i];
52 char tval[BCTEXTLEN], *tp = topt->get(tval, sizeof(tval));
53 char vval[BCTEXTLEN], *vp = vopt->get(vval, sizeof(vval));
54 int ret = tp && vp ? strcmp(tp, vp) : tp || vp ? 1 : 0;
60 void PluginFClientConfig::copy_from(PluginFClientConfig &that)
62 PluginFClientConfig &conf = *this;
63 for( int i=0; i<that.size(); ++i ) {
64 PluginFClient_Opt *vp = that[i];
65 const char *nm = vp->opt->name;
67 while( --k >= 0 && strcmp(nm, conf[k]->opt->name) );
69 PluginFClient_Opt *fopt = conf[k];
70 char str[BCTEXTLEN], *sp = vp->get(str, sizeof(str));
71 if( sp ) fopt->set(sp);
75 void PluginFClientConfig::interpolate(PluginFClientConfig &prev, PluginFClientConfig &next,
76 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
81 void PluginFClientConfig::initialize(const char *name)
84 ffilt = PluginFFilter::new_ffilter(name);
85 const AVOption *opt = 0;
86 void *obj = ffilt->filter_config();
88 const AVClass *filt_class = ffilt->filter_class();
89 if( filt_class && filt_class->option ) {
90 PluginFClientConfig &conf = *this;
91 while( (opt=av_opt_next(obj, opt)) != 0 ) {
92 if( opt->type == AV_OPT_TYPE_CONST ) continue;
94 for( int i=0; !dupl && i<size(); ++i ) {
95 PluginFClient_Opt *fp = conf[i];
96 const AVOption *op = fp->opt;
97 if( op->offset != opt->offset ) continue;
98 if( op->type != opt->type ) continue;
100 if( strlen(op->name) < strlen(opt->name) )
104 PluginFClient_Opt *fopt = new PluginFClient_Opt(this, opt);
108 if( (ffilt->filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
109 opt = av_opt_find(ffilt->fctx, "threads", NULL, 0, 0);
111 PluginFClient_Opt *fopt = new PluginFClient_Opt(this, opt);
117 int PluginFClientConfig::update()
120 PluginFClientConfig &conf = *this;
122 for( int i=0; i<size(); ++i ) {
123 PluginFClient_Opt *fopt = conf[i];
124 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
125 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
126 fopt->item_value->update();
132 void PluginFClientConfig::dump(FILE *fp)
134 const AVOption *opt = 0;
135 const AVClass *obj = filter_class();
136 PluginFClientConfig &conf = *this;
138 while( (opt=av_opt_next(&obj, opt)) != 0 ) {
139 if( opt->type == AV_OPT_TYPE_CONST ) continue;
141 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
142 if( k < 0 ) continue;
143 PluginFClient_Opt *fopt = conf[k];
144 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
145 fprintf(fp, " %s:=%s", opt->name, vp);
147 char unt[BCTEXTLEN], *up = unt;
149 fprintf(fp, "%s", unt);
156 PluginFClientReset(PluginFClientWindow *fwin, int x, int y)
157 : BC_GenericButton(x, y, _("Reset"))
163 ~PluginFClientReset()
167 int PluginFClientReset::handle_event()
169 fwin->ffmpeg->config.initialize(fwin->ffmpeg->name);
170 if( fwin->ffmpeg->config.update() > 0 )
172 fwin->ffmpeg->plugin->send_configure_change();
177 PluginFClientText(PluginFClientWindow *fwin, int x, int y, int w)
178 : BC_TextBox(x, y, w, 1, (char *)"")
188 int PluginFClientText::handle_event()
190 if( get_keypress() == RETURN ) {
198 PluginFClientUnits(PluginFClientWindow *fwin, int x, int y, int w)
199 : BC_PopupMenu(x, y, w, "")
205 ~PluginFClientUnits()
209 int PluginFClientUnits::handle_event()
211 const char *text = get_text();
212 if( text && fwin->selected ) {
213 if( text ) fwin->selected->set(text);
214 fwin->selected->item_value->update();
216 fwin->ffmpeg->plugin->send_configure_change();
222 PluginFClientApply(PluginFClientWindow *fwin, int x, int y)
223 : BC_GenericButton(x, y, _("Apply"))
229 ~PluginFClientApply()
233 int PluginFClientApply::handle_event()
235 return fwin->update();
238 PluginFClientPot::PluginFClientPot(PluginFClientWindow *fwin, int x, int y)
239 : BC_FPot(x, y, 0.f, 0.f, 0.f)
244 int PluginFClient_Opt::get_range(float &fmn, float &fmx)
246 switch( opt->type ) {
247 case AV_OPT_TYPE_INT:
248 case AV_OPT_TYPE_INT64:
249 case AV_OPT_TYPE_DOUBLE:
250 case AV_OPT_TYPE_FLOAT: break;
253 const AVClass *filt_class = filter_class();
254 if( !filt_class || !filt_class->option ) return 1;
255 AVOptionRanges *r = 0;
256 void *obj = &filt_class;
257 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 1;
259 if( r->nb_ranges == 1 ) {
260 fmn = r->range[0]->value_min;
261 fmx = r->range[0]->value_max;
264 av_opt_freep_ranges(&r);
268 float PluginFClient_Opt::get_float()
270 char val[BCTEXTLEN]; val[0] = 0;
271 get(val, sizeof(val));
275 void PluginFClient_Opt::set_float(float v)
277 char val[BCTEXTLEN]; val[0] = 0;
278 sprintf(val, "%f", v);
282 int PluginFClientPot::handle_event()
284 if( fwin->selected ) {
285 fwin->selected->set_float(get_value());
286 fwin->update_selected();
287 return fwin->update();
292 PluginFClientSlider::PluginFClientSlider(PluginFClientWindow *fwin, int x, int y)
293 : BC_FSlider(x, y, 0, fwin->get_w()-x-xS(20), fwin->get_w()-x-xS(20), 0.f, 0.f, 0.f)
298 int PluginFClientSlider::handle_event()
300 if( fwin->selected ) {
301 fwin->selected->set_float(get_value());
302 fwin->update_selected();
303 return fwin->update();
309 PluginFClient_OptPanel::
310 PluginFClient_OptPanel(PluginFClientWindow *fwin, int x, int y, int w, int h)
311 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
314 update(); // init col/wid/columns
317 PluginFClient_OptPanel::
318 ~PluginFClient_OptPanel()
322 void PluginFClient_OptPanel::create_objects()
324 PluginFClientConfig &fconfig = fwin->ffmpeg->config;
325 for( int i=0; i<fconfig.size(); ++i ) {
326 PluginFClient_Opt *opt = fconfig[i];
327 opts.append(opt->item_name);
328 vals.append(opt->item_value);
332 int PluginFClient_OptPanel::cursor_leave_event()
338 void PluginFClientWindow::update(PluginFClient_Opt *opt)
340 if( selected != opt ) {
341 if( selected ) selected->item_name->set_selected(0);
343 if( selected ) selected->item_name->set_selected(1);
345 clear_box(0,0, 0,panel->get_y());
346 char str[BCTEXTLEN], *sp;
348 if( opt ) opt->types(sp);
351 if( opt ) opt->ranges(sp);
353 while( units->total_items() ) units->del_item(0);
354 ArrayList<const AVOption *> opts;
355 int n = !opt ? 0 : opt->units(opts);
356 for( int i=0; i<n; ++i )
357 units->add_item(new BC_MenuItem(opts[i]->name, 0));
358 char unit[BCSTRLEN]; strcpy(unit, "()");
359 if( units->total_items() && opt && opt->opt->unit )
360 strcpy(unit, opt->opt->unit);
361 units->set_text(unit);
362 char val[BCTEXTLEN]; val[0] = 0;
363 if( opt ) opt->get(val, sizeof(val));
366 float v = 0, fmn = 0, fmx = 0;
367 if( opt && !opt->get_range(fmn, fmx) ) v = atof(val);
368 float p = (fmx-fmn) / slider->get_w();
369 slider->set_precision(p);
370 slider->update(slider->get_w(), v, fmn, fmx);
371 pot->set_precision(p);
372 pot->update(v, fmn, fmx);
376 int PluginFClientWindow::update()
378 const char *txt = text->get_text();
379 if( txt && selected ) {
381 selected->item_value->update();
383 ffmpeg->plugin->send_configure_change();
388 void PluginFClientWindow::update_selected()
393 int PluginFClient_OptPanel::selection_changed()
395 PluginFClient_Opt *opt = 0;
396 BC_ListBoxItem *item = get_selection(0, 0);
398 PluginFClient_OptName *opt_name = (PluginFClient_OptName *)item;
402 fwin->panel->set_tooltip(!opt ? 0 : opt->tip());
403 fwin->panel->show_tooltip();
407 void *PluginFClient_Opt::filter_config()
409 return conf->filter_config();
411 const AVClass *PluginFClient_Opt::filter_class()
413 return conf->filter_class();
416 int PluginFClient_Opt::types(char *rp)
420 case AV_OPT_TYPE_FLAGS: cp = "<flags>"; break;
421 case AV_OPT_TYPE_INT: cp = "<int>"; break;
422 case AV_OPT_TYPE_INT64: cp = "<int64>"; break;
423 case AV_OPT_TYPE_DOUBLE: cp = "<double>"; break;
424 case AV_OPT_TYPE_FLOAT: cp = "<float>"; break;
425 case AV_OPT_TYPE_STRING: cp = "<string>"; break;
426 case AV_OPT_TYPE_RATIONAL: cp = "<rational>"; break;
427 case AV_OPT_TYPE_BINARY: cp = "<binary>"; break;
428 case AV_OPT_TYPE_IMAGE_SIZE: cp = "<image_size>"; break;
429 case AV_OPT_TYPE_VIDEO_RATE: cp = "<video_rate>"; break;
430 case AV_OPT_TYPE_PIXEL_FMT: cp = "<pix_fmt>"; break;
431 case AV_OPT_TYPE_SAMPLE_FMT: cp = "<sample_fmt>"; break;
432 case AV_OPT_TYPE_DURATION: cp = "<duration>"; break;
433 case AV_OPT_TYPE_COLOR: cp = "<color>"; break;
434 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = "<channel_layout>"; break;
435 default: cp = "<undef>"; break;
437 return sprintf(rp, "%s", cp);
439 int PluginFClient_Opt::scalar(double d, char *rp)
442 if( d == INT_MAX ) cp = "INT_MAX";
443 else if( d == INT_MIN ) cp = "INT_MIN";
444 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
445 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
446 else if( d == INT64_MIN ) cp = "I64_MIN";
447 else if( d == FLT_MAX ) cp = "FLT_MAX";
448 else if( d == FLT_MIN ) cp = "FLT_MIN";
449 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
450 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
451 else if( d == DBL_MAX ) cp = "DBL_MAX";
452 else if( d == DBL_MIN ) cp = "DBL_MIN";
453 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
454 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
455 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
456 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
457 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
458 else return sprintf(rp, "%g", d);
459 return sprintf(rp, "%s", cp);
462 int PluginFClient_Opt::ranges(char *rp)
464 const AVClass *filt_class = filter_class();
465 if( !filt_class || !filt_class->option ) return 0;
467 case AV_OPT_TYPE_INT:
468 case AV_OPT_TYPE_INT64:
469 case AV_OPT_TYPE_DOUBLE:
470 case AV_OPT_TYPE_FLOAT: break;
473 AVOptionRanges *r = 0;
474 void *obj = &filt_class;
476 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
477 for( int i=0; i<r->nb_ranges; ++i ) {
478 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
479 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
480 cp += sprintf(cp, ")");
482 av_opt_freep_ranges(&r);
486 int PluginFClient_Opt::units(ArrayList<const AVOption *> &opts)
488 if( !this->opt->unit ) return 0;
489 const AVClass *filt_class = filter_class();
490 if( !filt_class || !filt_class->option ) return 0;
491 void *obj = &filt_class;
492 const AVOption *opt = NULL;
493 while( (opt=av_opt_next(obj, opt)) != 0 ) {
494 if( !opt->unit ) continue;
495 if( opt->type != AV_OPT_TYPE_CONST ) continue;
496 if( strcmp(this->opt->unit, opt->unit) ) continue;
499 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
500 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
509 int PluginFClient_Opt::units(char *rp)
511 ArrayList<const AVOption *> opts;
515 cp += sprintf(cp, " [%s:", this->opt->unit);
516 for( int i=0; i<n; ++i )
517 cp += sprintf(cp, " %s", opts[i]->name);
518 cp += sprintf(cp, "]:");
522 const char *PluginFClient_Opt::tip()
527 int PluginFClient_OptPanel::update()
529 const char *cols[] = { "option", "value", };
530 const int col1_w = xS(150);
531 int wids[] = { col1_w, get_w()-col1_w };
532 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]),
533 get_xposition(), get_yposition(), get_highlighted_item());
538 PluginFClientWindow::PluginFClientWindow(PluginFClient *ffmpeg)
539 : PluginClientWindow(ffmpeg->plugin, xS(600), yS(300), xS(600), yS(300), 1)
541 this->ffmpeg = ffmpeg;
545 PluginFClientWindow::~PluginFClientWindow()
549 void PluginFClientWindow::create_objects()
551 int xs8 = xS(8), xs10 = xS(10), ys10 = yS(10);
552 char string[BCTEXTLEN];
554 int x = xs10, y = ys10;
555 const char *descr = ffmpeg->config.ffilt->description();
556 if( !descr ) descr = ffmpeg->config.ffilt->filter_name();
557 add_subwindow(title = new BC_Title(x, y, descr));
558 y += title->get_h() + ys10;
560 sprintf(string, _("Type: "));
561 add_subwindow(title = new BC_Title(x0, y, string));
562 x0 += title->get_w() + xs8;
563 add_subwindow(type = new BC_Title(x0, y, (char *)""));
565 sprintf(string, _("Range: "));
566 add_subwindow(title = new BC_Title(x0, y, string));
567 x0 += title->get_w() + xs8;
568 add_subwindow(range = new BC_Title(x0, y, (char *)""));
569 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Reset")) - xs8;
570 add_subwindow(reset = new PluginFClientReset(this, x1, y));
571 y += title->get_h() + ys10;
573 add_subwindow(units = new PluginFClientUnits(this, x0, y, xS(120)));
574 x0 += units->get_w() + xs8;
575 x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - xs8;
576 add_subwindow(apply = new PluginFClientApply(this, x1, y));
577 add_subwindow(text = new PluginFClientText(this, x0, y, x1-x0 - xs8));
578 y += title->get_h() + ys10;
579 add_subwindow(pot = new PluginFClientPot(this, x, y));
580 x1 = x + pot->get_w() + xs10;
581 add_subwindow(slider = new PluginFClientSlider(this, x1, y+ys10));
582 y += pot->get_h() + ys10;
584 panel_x = x; panel_y = y;
585 panel_w = get_w()-xs10 - panel_x;
586 panel_h = get_h()-ys10 - panel_y;
587 panel = new PluginFClient_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
588 add_subwindow(panel);
589 panel->create_objects();
590 ffmpeg->config.update();
595 void PluginFClientWindow::draw()
600 int PluginFClientWindow::resize_event(int w, int h)
602 int xs8 = xS(8), xs10 = xS(10), ys10 = yS(10);
603 int x = get_w() - BC_GenericButton::calculate_w(this, _("Reset")) - xs8;
604 int y = reset->get_y();
605 reset->reposition_window(x, y);
606 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - xs8;
607 int y1 = units->get_y();
608 apply->reposition_window(x1, y1);
609 int x0 = units->get_x() + units->get_w() + xs8;
610 int y0 = units->get_y();
611 text->reposition_window(x0,y0, x1-x0-xs8);
612 x1 = pot->get_x() + pot->get_w() + xs10;
613 int w1 = w - slider->get_x() - xS(20);
614 slider->set_pointer_motion_range(w1);
615 slider->reposition_window(x1, slider->get_y(), w1, slider->get_h());
616 panel_w = get_w()-xs10 - panel_x;
617 panel_h = get_h()-ys10 - panel_y;
618 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
623 PluginFClient::PluginFClient(PluginClient *plugin, const char *name)
625 this->plugin = plugin;
629 plugin_position = -1;
630 filter_position = -1;
632 sprintf(title, "F_%s", name);
633 config.initialize(name);
634 curr_config.initialize(name);
637 PluginFClient::~PluginFClient()
642 bool PluginFClient::is_audio(const AVFilter *fp)
644 if( !fp->outputs ) return 0;
645 if( avfilter_pad_count(fp->outputs) > 1 ) return 0;
646 if( !avfilter_pad_get_name(fp->outputs, 0) ) return 0;
647 if( avfilter_pad_get_type(fp->outputs, 0) != AVMEDIA_TYPE_AUDIO ) return 0;
648 if( !fp->inputs ) return 1;
649 if( avfilter_pad_count(fp->inputs) > 1 ) return 0;
650 if( !avfilter_pad_get_name(fp->inputs, 0) ) return 0;
651 if( avfilter_pad_get_type(fp->inputs, 0) != AVMEDIA_TYPE_AUDIO ) return 0;
654 bool PluginFClient::is_video(const AVFilter *fp)
656 if( !fp->outputs ) return 0;
657 if( avfilter_pad_count(fp->outputs) > 1 ) return 0;
658 if( !avfilter_pad_get_name(fp->outputs, 0) ) return 0;
659 if( avfilter_pad_get_type(fp->outputs, 0) != AVMEDIA_TYPE_VIDEO ) return 0;
660 if( !fp->inputs ) return 1;
661 if( avfilter_pad_count(fp->inputs) > 1 ) return 0;
662 if( !avfilter_pad_get_name(fp->inputs, 0) ) return 0;
663 if( avfilter_pad_get_type(fp->inputs, 0) != AVMEDIA_TYPE_VIDEO ) return 0;
667 NEW_WINDOW_MACRO(PluginFClient, PluginFClientWindow)
669 int PluginFClient::load_configuration()
671 int64_t src_position = get_source_position();
672 KeyFrame *prev_keyframe = get_prev_keyframe(src_position);
673 config.copy_from(curr_config);
674 read_data(prev_keyframe);
675 int ret = !config.equivalent(curr_config) ? 1 : 0;
680 void PluginFClient::render_gui(void *data, int size)
682 PluginFClientConfig *conf = (PluginFClientConfig *)data;
683 config.copy_from(*conf);
684 KeyFrame *keyframe = plugin->server->get_keyframe();
689 void PluginFClient::update_gui()
691 PluginClientThread *thread = plugin->get_thread();
692 if( !thread ) return;
693 PluginFClientWindow *window = (PluginFClientWindow*)thread->get_window();
694 window->lock_window("PluginFClient::update_gui");
695 load_configuration();
696 if( config.update() > 0 )
698 window->unlock_window();
701 const char *PluginFClient::plugin_title()
706 char *PluginFClient::to_upper(char *cp, const char *sp)
709 while( *sp != 0 ) *bp++ = toupper(*sp++);
714 void PluginFClient::save_data(KeyFrame *keyframe)
717 char string[BCTEXTLEN];
719 // cause data to be stored directly in text
720 output.set_shared_output(keyframe->xbuf);
721 output.tag.set_title(to_upper(string, plugin_title()));
722 const AVClass *filt_class = config.filter_class();
723 if( filt_class && filt_class->option ) {
724 void *obj = config.filter_config();
725 const AVOption *opt = NULL;
726 while( (opt=av_opt_next(obj, opt)) != 0 ) {
728 if( av_opt_get(obj, opt->name, 0, &buf) < 0 ) continue;
729 output.tag.set_property(opt->name, (const char *)buf);
733 if( (config.ffilt->filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
735 if( av_opt_get(config.ffilt->fctx, "threads", 0, &buf) >= 0 && buf ) {
736 output.tag.set_property("threads", (const char *)buf);
742 output.terminate_string();
745 void PluginFClient::read_data(KeyFrame *keyframe)
748 char string[BCTEXTLEN];
749 input.set_shared_input(keyframe->xbuf);
751 while( !input.read_tag() ) {
752 to_upper(string, plugin_title());
753 if( !input.tag.title_is(string) ) continue;
754 const AVClass *filt_class = config.filter_class();
755 if( filt_class && filt_class->option ) {
756 void *obj = config.filter_config();
757 const AVOption *opt = NULL;
758 while( (opt=av_opt_next(obj, opt)) != 0 ) {
759 const char *v = input.tag.get_property(opt->name);
760 if( v ) av_opt_set(obj, opt->name, v, 0);
762 if( (config.ffilt->filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
763 const char *v = input.tag.get_property("threads");
764 if( v ) av_opt_set(config.ffilt->fctx, "threads", v, 0);
770 int PluginFClient::activate()
773 avfilter_link(fsrc, 0, ffilt->fctx, 0);
774 avfilter_link(ffilt->fctx, 0, fsink, 0);
775 int ret = avfilter_graph_config(ffilt->graph, NULL);
777 curr_config.copy_from(config);
783 void PluginFClient::reactivate()
785 delete ffilt; ffilt = 0;
789 PluginFAClient::PluginFAClient(PluginServer *server, const char *name)
790 : PluginAClient(server), PluginFClient(this, name)
794 PluginFAClient::~PluginFAClient()
798 int PluginFAClient::activate()
800 if( activated ) return activated;
801 ffilt = PluginFFilter::new_ffilter(name, &config);
803 config.copy_from(curr_config);
804 send_configure_change();
805 send_render_gui(&config, sizeof(config));
806 ffilt = PluginFFilter::new_ffilter(name, &config);
808 AVSampleFormat sample_fmt = AV_SAMPLE_FMT_FLTP;
809 int channels = PluginClient::total_in_buffers;
810 uint64_t layout = (((uint64_t)1)<<channels) - 1;
811 AVFilterGraph *graph = !ffilt ? 0 : ffilt->graph;
812 int ret = !graph ? -1 : 0;
813 if( ret >= 0 && ffilt->filter->inputs ) {
814 char args[BCTEXTLEN];
815 snprintf(args, sizeof(args),
816 "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx",
817 1, sample_rate, sample_rate, av_get_sample_fmt_name(sample_fmt), layout);
818 ret = avfilter_graph_create_filter(&fsrc, avfilter_get_by_name("abuffer"),
819 "in", args, NULL, graph);
822 ret = avfilter_graph_create_filter(&fsink, avfilter_get_by_name("abuffersink"),
823 "out", NULL, NULL, graph);
825 ret = av_opt_set_bin(fsink, "sample_fmts",
826 (uint8_t*)&sample_fmt, sizeof(sample_fmt), AV_OPT_SEARCH_CHILDREN);
828 ret = av_opt_set_bin(fsink, "channel_layouts",
829 (uint8_t*)&layout, sizeof(layout), AV_OPT_SEARCH_CHILDREN);
831 ret = av_opt_set_bin(fsink, "sample_rates",
832 (uint8_t*)&sample_rate, sizeof(sample_rate), AV_OPT_SEARCH_CHILDREN);
834 ret = PluginFClient::activate();
835 if( ret < 0 && activated >= 0 ) {
836 ff_err(ret, "PluginFAClient::activate: %s failed\n", plugin_title());
843 static AVRational best_frame_rate(double frame_rate)
845 static const int m1 = 1001*12, m2 = 1000*12;
846 static const int freqs[] = {
847 40*m1, 48*m1, 50*m1, 60*m1, 80*m1,120*m1, 240*m1,
848 24*m2, 30*m2, 60*m2, 12*m2, 15*m2, 48*m2, 0,
851 int freq, best_freq = 0;
852 for( int i=0; (freq = i<30*12 ? (i+1)*1001 : freqs[i-30*12]); ++i ) {
853 double framerate = (double)freq / m1;
854 double err = fabs(frame_rate/framerate - 1.);
855 if( err >= max_err ) continue;
859 return max_err < 0.0001 ?
860 (AVRational) { best_freq, m1 } :
861 (AVRational) { 0, 0 };
864 int PluginFVClient::activate(int width, int height, int color_model)
866 if( activated ) return activated;
867 ffilt = PluginFFilter::new_ffilter(name, &config);
869 config.copy_from(curr_config);
870 send_configure_change();
871 send_render_gui(&config, sizeof(config));
872 ffilt = PluginFFilter::new_ffilter(name, &config);
874 AVPixelFormat pix_fmt = color_model_to_pix_fmt(color_model);
875 AVFilterGraph *graph = !ffilt ? 0 : ffilt->graph;
876 int ret = !graph ? -1 : 0;
877 if( ret >= 0 && ffilt->filter->inputs ) {
878 curr_config.copy_from(config);
879 if( pix_fmt == AV_PIX_FMT_NB ) {
880 int bpp = BC_CModels::calculate_pixelsize(color_model) * 8;
881 int bits_per_comp = bpp / BC_CModels::components(color_model);
882 int alpha = BC_CModels::has_alpha(color_model);
883 pix_fmt = bits_per_comp > 8 ?
884 !alpha ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGBA64LE :
885 !alpha ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_RGBA ;
887 int aspect_w = 1, aspect_h = 1; // XXX
888 AVRational best_rate = best_frame_rate(frame_rate);
889 char args[BCTEXTLEN];
890 snprintf(args, sizeof(args),
891 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
892 width, height, pix_fmt, best_rate.den, best_rate.num, aspect_w, aspect_h);
893 ret = avfilter_graph_create_filter(&fsrc, avfilter_get_by_name("buffer"),
894 "in", args, NULL, graph);
897 ret = avfilter_graph_create_filter(&fsink, avfilter_get_by_name("buffersink"),
898 "out", NULL, NULL, graph);
900 ret = av_opt_set_bin(fsink, "pix_fmts",
901 (uint8_t*)&pix_fmt, sizeof(pix_fmt), AV_OPT_SEARCH_CHILDREN);
903 ret = PluginFClient::activate();
904 if( ret < 0 && activated >= 0 ) {
905 ff_err(ret, "PluginFVClient::activate() %s\n", plugin_title());
911 int PluginFAClient::get_inchannels()
913 AVFilterContext *fctx = ffilt->fctx;
914 AVFilterLink **links = !fctx->nb_inputs ? 0 : fctx->inputs;
915 return !links ? 0 : links[0]->channels;
918 int PluginFAClient::get_outchannels()
920 AVFilterContext *fctx = ffilt->fctx;
921 AVFilterLink **links = !fctx->nb_outputs ? 0 : fctx->outputs;
922 return !links ? 0 : links[0]->channels;
925 int PluginFAClient::process_buffer(int64_t size, Samples **buffer, int64_t start_position, int sample_rate)
927 int total_in = PluginClient::total_in_buffers;
928 int total_out = PluginClient::total_out_buffers;
929 int in_channels = 0, out_channels = 0;
931 if( load_configuration() )
934 if( plugin_position != start_position )
935 filter_position = plugin_position = start_position;
938 int ret = activate();
939 if( ret >= 0 && !(frame = av_frame_alloc()) ) {
940 fprintf(stderr, "PluginFAClient::process_buffer: av_frame_alloc failed\n");
941 ret = AVERROR(ENOMEM);
944 in_channels = get_inchannels();
945 out_channels = get_outchannels();
946 frame->nb_samples = size;
947 frame->format = AV_SAMPLE_FMT_FLTP;
948 frame->channel_layout = (1<<in_channels)-1;
949 frame->sample_rate = sample_rate;
950 frame->pts = filter_position;
954 while( ret >= 0 && --retry >= 0 ) {
955 ret = av_buffersink_get_frame(fsink, frame);
956 if( ret >= 0 || ret != AVERROR(EAGAIN) ) break;
957 if( !fsrc ) { ret = AVERROR(EIO); break; }
958 for( int i=0; i<total_in; ++i )
959 read_samples(buffer[i], i, sample_rate, filter_position, size);
960 filter_position += size;
961 ret = av_frame_get_buffer(frame, 0);
963 float **in_buffers = (float **)&frame->extended_data[0];
964 for(int i = 0; i < in_channels; i++) {
965 float *in_buffer = in_buffers[i];
966 int k = i < total_in ? i : 0;
967 double *in_ptr = buffer[k]->get_data();
968 for(int j = 0; j < size; j++) in_buffer[j] = in_ptr[j];
970 ret = av_buffersrc_add_frame_flags(fsrc, frame, 0);
972 if( ret >= 0 && retry < 0 )
973 ret = AVERROR(EAGAIN);
976 int nbfrs = total_out;
977 if( out_channels < nbfrs ) nbfrs = out_channels;
978 float **out_buffers = (float **)&frame->extended_data[0];
981 float *out_buffer = out_buffers[i];
982 double *out_ptr = buffer[i++]->get_data();
983 for(int j = 0; j < size; j++) out_ptr[j] = out_buffer[j];
985 while( i < total_out ) {
986 double *out_ptr = buffer[i++]->get_data();
987 bzero(out_ptr, sizeof(double) * size);
991 int64_t position = PluginFClient::get_source_position();
992 double t0 = (double)position/sample_rate, dt = 1./sample_rate;
993 for( int i=0; i<total_out; ++i ) {
994 double t = t0, *out = buffer[i]->get_data();
995 for( int k=size; --k>=0; t+=dt ) {
996 double w = int(2*t) & 1 ? 2*M_PI*440 : 2*M_PI*697;
1000 ff_err(ret, "PluginFAClient::process_buffer() %s\n", plugin_title());
1003 av_frame_free(&frame);
1004 plugin_position += size;
1009 PluginFVClient::PluginFVClient(PluginServer *server, const char *name)
1010 : PluginVClient(server),
1011 PluginFClient(this, name),
1012 FFVideoConvert(server->preferences)
1016 PluginFVClient::~PluginFVClient()
1020 int PluginFVClient::process_buffer(VFrame **frames, int64_t position, double frame_rate)
1022 VFrame *vframe = *frames;
1023 int width = vframe->get_w();
1024 int height = vframe->get_h();
1026 if( load_configuration() )
1029 if( plugin_position != position )
1030 filter_position = plugin_position = position;
1032 int colormodel = vframe->get_color_model();
1033 int ret = activate(width, height, colormodel);
1034 AVPixelFormat pix_fmt = fsrc ?
1035 (AVPixelFormat) fsrc->outputs[0]->format :
1036 color_model_to_pix_fmt(colormodel);
1037 if( pix_fmt <= AV_PIX_FMT_NONE || pix_fmt >= AV_PIX_FMT_NB )
1038 pix_fmt = AV_PIX_FMT_RGBA;
1041 if( ret >= 0 && !(frame = av_frame_alloc()) ) {
1042 fprintf(stderr, "PluginFVClient::process_buffer: av_frame_alloc failed\n");
1043 ret = AVERROR(ENOMEM);
1047 while( ret >= 0 && --retry >= 0 ) {
1048 ret = av_buffersink_get_frame(fsink, frame);
1049 if( ret >= 0 || ret != AVERROR(EAGAIN) ) break;
1050 if( !fsrc ) { ret = AVERROR(EIO); break; }
1051 read_frame(vframe, 0, filter_position, frame_rate, 0);
1052 frame->format = pix_fmt;
1053 frame->width = width;
1054 frame->height = height;
1055 frame->pts = filter_position++;
1056 ret = av_frame_get_buffer(frame, 32);
1057 if( ret < 0 ) break;
1058 ret = transfer_pixfmt(vframe, frame);
1059 if( ret < 0 ) break;
1060 ret = av_buffersrc_add_frame_flags(fsrc, frame, 0);
1062 if( ret >= 0 && retry < 0 )
1063 ret = AVERROR(EAGAIN);
1066 pix_fmt = (AVPixelFormat) frame->format;
1067 ret = transfer_cmodel(vframe, frame);
1070 ff_err(ret, "PluginFVClient::process_buffer() %s\n", plugin_title());
1071 if( position & 1 ) vframe->clear_frame();
1075 av_frame_free(&frame);
1076 return ret >= 0 ? 0 : 1;
1080 PluginFClient_OptName:: PluginFClient_OptName(PluginFClient_Opt *opt)
1083 set_text(opt->opt->name);
1086 PluginFClient_OptValue::PluginFClient_OptValue(PluginFClient_Opt *opt)
1092 void PluginFClient_OptValue::update()
1094 char val[BCTEXTLEN]; val[0] = 0;
1095 opt->get(val, sizeof(val));
1100 PluginFClient_Opt::PluginFClient_Opt(PluginFClientConfig *conf, const AVOption *opt)
1104 item_name = new PluginFClient_OptName(this);
1105 item_value = new PluginFClient_OptValue(this);
1108 PluginFClient_Opt::~PluginFClient_Opt()
1114 const char *PluginFClientConfig::get(const char *name)
1117 if( av_opt_get(filter_config(), name, 0, &bp) >= 0 ||
1118 av_opt_get(ffilt->fctx, name, 0, &bp) >= 0 )
1119 return (const char *)bp;
1122 char *PluginFClient_Opt::get(char *vp, int sz)
1124 const char *val = conf->get(opt->name);
1126 strncpy(vp, val, sz);
1135 void PluginFClientConfig::set(const char *name, const char *val)
1137 if( av_opt_set(filter_config(), name, val, 0) < 0 )
1138 av_opt_set(ffilt->fctx, name, val, 0);
1140 void PluginFClient_Opt::set(const char *val)
1142 conf->set(opt->name, val);
1145 void PluginFFilter::uninit()
1149 static int get_defaults(const char *name, char *args)
1152 char defaults_path[BCTEXTLEN];
1153 FFMPEG::set_option_path(defaults_path, "plugin.opts");
1154 FILE *fp = fopen(defaults_path,"r");
1156 char ff_plugin[BCSTRLEN], ff_args[BCTEXTLEN], *ap = 0;
1157 while( fgets(ff_args, sizeof(ff_args), fp) ) {
1159 if( *cp == ';' ) continue;
1160 if( *cp == '#' ) ++cp;
1161 char *bp = ff_plugin, *ep = bp + sizeof(ff_plugin)-1;
1162 while( bp < ep && *cp && *cp != '\n' && *cp != ' ' ) *bp++ = *cp++;
1164 if( !strcmp(ff_plugin, name) ) { ap = cp; break; }
1168 if( ff_args[0] == '#' ) return -1;
1169 while( *ap == ' ' ) ++ap;
1170 while( *ap && *ap != '\n' ) *args++ = *ap++;
1175 bool PluginFFilter::is_audio()
1177 return PluginFClient::is_audio(filter);
1180 bool PluginFFilter::is_video()
1182 return PluginFClient::is_video(filter);
1185 int PluginFFilter::init(const char *name, PluginFClientConfig *conf)
1187 char args[BCTEXTLEN];
1188 int ret = get_defaults(name, args);
1189 if( ret < 0 ) return 0;
1190 PluginFLogLevel errs(AV_LOG_ERROR);
1191 this->filter = avfilter_get_by_name(name);
1192 if( !this->filter ) return AVERROR(ENOENT);
1193 int flag_mask = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS;
1194 if( filter->flags & flag_mask ) return AVERROR(EPERM);
1195 if( !this->is_audio() && !this->is_video() ) return AVERROR(EIO);
1196 this->graph = avfilter_graph_alloc();
1197 if( !this->graph ) return AVERROR(ENOMEM);
1198 static int inst = 0;
1199 char inst_name[BCSTRLEN];
1200 sprintf(inst_name,"%s_%d", name, ++inst);
1201 if( conf && (filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
1202 graph->thread_type = AVFILTER_THREAD_SLICE;
1203 graph->nb_threads = atoi(conf->get("threads"));
1206 graph->thread_type = 0;
1207 graph->nb_threads = 0;
1209 fctx = avfilter_graph_alloc_filter(graph, filter, inst_name);
1210 if( !fctx ) return AVERROR(ENOMEM);
1211 fctx->thread_type = graph->thread_type; // bug in avfilter
1213 AVDictionary *opts = 0;
1214 for( int i=0; i<conf->size(); ++i ) {
1215 PluginFClient_Opt *op = conf->get(i);
1216 const char *name = op->opt->name;
1217 char val[BCTEXTLEN], *vp = op->get(val, sizeof(val));
1220 // unspecified opts cause a special behavior in some filters (lut3d)
1221 // so... if opt value is the default, skip it or no special behavior
1222 if( av_opt_get(filter_config(), name, 0, &bp) >= 0 ) {
1223 int result = strcmp((const char *)bp, vp);
1225 if( !result ) continue;
1227 av_dict_set(&opts, name, vp, 0);
1229 ret = avfilter_init_dict(fctx, &opts);
1230 av_dict_free(&opts);
1233 ret = avfilter_init_str(fctx, args);
1234 return ret < 0 ? ret : 1;
1238 PluginFFilter::PluginFFilter()
1245 PluginFFilter::~PluginFFilter()
1247 PluginFLogLevel errs(AV_LOG_ERROR);
1248 avfilter_graph_free(&graph);
1249 filter = 0; fctx = 0;
1252 PluginFFilter *PluginFFilter::new_ffilter(const char *name, PluginFClientConfig *conf)
1254 PluginFFilter *ffilt = new PluginFFilter;
1255 int ret = ffilt->init(name, conf);
1256 if( ret < 0 ) ff_err(ret, "PluginFFilter::new_ffilter(%s)\n", name);
1257 if( ret <= 0 ) { delete ffilt; ffilt = 0; }
1261 PluginClient *PluginServer::new_ffmpeg_plugin()
1263 const AVFilter *filter = avfilter_get_by_name(ff_name);
1264 if( !filter ) return 0;
1265 PluginFClient *ffmpeg =
1266 PluginFClient::is_audio(filter) ?
1267 (PluginFClient *)new PluginFAClient(this, ff_name) :
1268 PluginFClient::is_video(filter) ?
1269 (PluginFClient *)new PluginFVClient(this, ff_name) :
1271 if( !ffmpeg ) return 0;
1272 return ffmpeg->plugin;
1276 PluginServer* MWindow::new_ffmpeg_server(MWindow *mwindow, const char *name)
1278 PluginFFilter *ffilt = PluginFFilter::new_ffilter(name);
1279 if( !ffilt ) return 0;
1281 return new PluginServer(mwindow, name, PLUGIN_TYPE_FFMPEG);
1284 void MWindow::init_ffmpeg_index(MWindow *mwindow, Preferences *preferences, FILE *fp)
1286 PluginFLogLevel errs(AV_LOG_ERROR);
1287 const AVFilter *filter = 0; void *idx = 0;
1288 while( (filter=av_filter_iterate(&idx)) != 0 ) {
1289 //printf("%s\n",filter->name);
1290 PluginServer *server = new_ffmpeg_server(mwindow, filter->name);
1292 int result = server->open_plugin(1, preferences, 0, 0);
1294 server->write_table(fp, filter->name, PLUGIN_FFMPEG_ID, 0);
1295 server->close_plugin();
1298 if( result ) fprintf(fp, "#%s\n", filter->name);
1303 void MWindow::init_ffmpeg()