8 #include "bcwindowbase.h"
13 #include "pluginfclient.h"
14 #include "pluginserver.h"
20 #define av_filter_iterate(p) ((*(const AVFilter**)(p))=avfilter_next(*(const AVFilter **)(p)))
23 static void ff_err(int ret, const char *fmt, ...)
28 vsnprintf(msg, sizeof(msg), fmt, ap);
30 char errmsg[BCSTRLEN];
31 av_strerror(ret, errmsg, sizeof(errmsg));
32 fprintf(stderr,_("%s err: %s\n"),msg, errmsg);
35 PluginFClientConfig::PluginFClientConfig()
40 PluginFClientConfig::~PluginFClientConfig()
46 int PluginFClientConfig::equivalent(PluginFClientConfig &that)
48 PluginFClientConfig &conf = *this;
49 for( int i=0; i<that.size(); ++i ) {
50 PluginFClient_Opt *topt = conf[i], *vopt = that[i];
51 char tval[BCTEXTLEN], *tp = topt->get(tval, sizeof(tval));
52 char vval[BCTEXTLEN], *vp = vopt->get(vval, sizeof(vval));
53 int ret = tp && vp ? strcmp(tp, vp) : tp || vp ? 1 : 0;
59 void PluginFClientConfig::copy_from(PluginFClientConfig &that)
61 PluginFClientConfig &conf = *this;
62 for( int i=0; i<that.size(); ++i ) {
63 PluginFClient_Opt *vp = that[i];
64 const char *nm = vp->opt->name;
66 while( --k >= 0 && strcmp(nm, conf[k]->opt->name) );
68 PluginFClient_Opt *fopt = conf[k];
69 char str[BCTEXTLEN], *sp = vp->get(str, sizeof(str));
70 if( sp ) fopt->set(sp);
74 void PluginFClientConfig::interpolate(PluginFClientConfig &prev, PluginFClientConfig &next,
75 int64_t prev_frame, int64_t next_frame, int64_t current_frame)
80 void PluginFClientConfig::initialize(const char *name)
83 ffilt = PluginFFilter::new_ffilter(name);
84 const AVOption *opt = 0;
85 void *obj = ffilt->filter_config();
87 const AVClass *filt_class = ffilt->filter_class();
88 if( filt_class && filt_class->option ) {
89 PluginFClientConfig &conf = *this;
90 while( (opt=av_opt_next(obj, opt)) != 0 ) {
91 if( opt->type == AV_OPT_TYPE_CONST ) continue;
93 for( int i=0; !dupl && i<size(); ++i ) {
94 PluginFClient_Opt *fp = conf[i];
95 const AVOption *op = fp->opt;
96 if( op->offset != opt->offset ) continue;
97 if( op->type != opt->type ) continue;
99 if( strlen(op->name) < strlen(opt->name) )
103 PluginFClient_Opt *fopt = new PluginFClient_Opt(this, opt);
107 if( (ffilt->filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
108 opt = av_opt_find(ffilt->fctx, "threads", NULL, 0, 0);
110 PluginFClient_Opt *fopt = new PluginFClient_Opt(this, opt);
116 int PluginFClientConfig::update()
119 PluginFClientConfig &conf = *this;
121 for( int i=0; i<size(); ++i ) {
122 PluginFClient_Opt *fopt = conf[i];
123 char val[BCTEXTLEN], *vp = fopt->get(val, sizeof(val));
124 if( !vp || !strcmp(val, fopt->item_value->get_text()) ) continue;
125 fopt->item_value->update();
131 void PluginFClientConfig::dump(FILE *fp)
133 const AVOption *opt = 0;
134 const AVClass *obj = filter_class();
135 PluginFClientConfig &conf = *this;
137 while( (opt=av_opt_next(&obj, opt)) != 0 ) {
138 if( opt->type == AV_OPT_TYPE_CONST ) continue;
140 while( --k >= 0 && strcmp(opt->name, conf[k]->opt->name) );
141 if( k < 0 ) continue;
142 PluginFClient_Opt *fopt = conf[k];
143 char val[BCTEXTLEN], *vp = fopt->get(val,sizeof(val));
144 fprintf(fp, " %s:=%s", opt->name, vp);
146 char unt[BCTEXTLEN], *up = unt;
148 fprintf(fp, "%s", unt);
155 PluginFClientReset(PluginFClientWindow *fwin, int x, int y)
156 : BC_GenericButton(x, y, _("Reset"))
162 ~PluginFClientReset()
166 int PluginFClientReset::handle_event()
168 fwin->ffmpeg->config.initialize(fwin->ffmpeg->name);
169 if( fwin->ffmpeg->config.update() > 0 )
171 fwin->ffmpeg->plugin->send_configure_change();
176 PluginFClientText(PluginFClientWindow *fwin, int x, int y, int w)
177 : BC_TextBox(x, y, w, 1, (char *)"")
187 int PluginFClientText::handle_event()
193 PluginFClientUnits(PluginFClientWindow *fwin, int x, int y, int w)
194 : BC_PopupMenu(x, y, w, "")
200 ~PluginFClientUnits()
204 int PluginFClientUnits::handle_event()
206 const char *text = get_text();
207 if( text && fwin->selected ) {
208 if( text ) fwin->selected->set(text);
209 fwin->selected->item_value->update();
211 fwin->ffmpeg->plugin->send_configure_change();
217 PluginFClientApply(PluginFClientWindow *fwin, int x, int y)
218 : BC_GenericButton(x, y, _("Apply"))
224 ~PluginFClientApply()
228 int PluginFClientApply::handle_event()
230 return fwin->update();
233 PluginFClientPot::PluginFClientPot(PluginFClientWindow *fwin, int x, int y)
234 : BC_FPot(x, y, 0.f, 0.f, 0.f)
239 int PluginFClient_Opt::get_range(float &fmn, float &fmx)
241 switch( opt->type ) {
242 case AV_OPT_TYPE_INT:
243 case AV_OPT_TYPE_INT64:
244 case AV_OPT_TYPE_DOUBLE:
245 case AV_OPT_TYPE_FLOAT: break;
248 const AVClass *filt_class = filter_class();
249 if( !filt_class || !filt_class->option ) return 1;
250 AVOptionRanges *r = 0;
251 void *obj = &filt_class;
252 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 1;
254 if( r->nb_ranges == 1 ) {
255 fmn = r->range[0]->value_min;
256 fmx = r->range[0]->value_max;
259 av_opt_freep_ranges(&r);
263 float PluginFClient_Opt::get_float()
265 char val[BCTEXTLEN]; val[0] = 0;
266 get(val, sizeof(val));
270 void PluginFClient_Opt::set_float(float v)
272 char val[BCTEXTLEN]; val[0] = 0;
273 sprintf(val, "%f", v);
277 int PluginFClientPot::handle_event()
279 if( fwin->selected ) {
280 fwin->selected->set_float(get_value());
281 fwin->update_selected();
282 return fwin->update();
287 PluginFClientSlider::PluginFClientSlider(PluginFClientWindow *fwin, int x, int y)
288 : BC_FSlider(x, y, 0, fwin->get_w()-x-20, fwin->get_w()-x-20, 0.f, 0.f, 0.f)
293 int PluginFClientSlider::handle_event()
295 if( fwin->selected ) {
296 fwin->selected->set_float(get_value());
297 fwin->update_selected();
298 return fwin->update();
304 PluginFClient_OptPanel::
305 PluginFClient_OptPanel(PluginFClientWindow *fwin, int x, int y, int w, int h)
306 : BC_ListBox(x, y, w, h, LISTBOX_TEXT), opts(items[0]), vals(items[1])
309 update(); // init col/wid/columns
312 PluginFClient_OptPanel::
313 ~PluginFClient_OptPanel()
317 void PluginFClient_OptPanel::create_objects()
319 PluginFClientConfig &fconfig = fwin->ffmpeg->config;
320 for( int i=0; i<fconfig.size(); ++i ) {
321 PluginFClient_Opt *opt = fconfig[i];
322 opts.append(opt->item_name);
323 vals.append(opt->item_value);
327 int PluginFClient_OptPanel::cursor_leave_event()
333 void PluginFClientWindow::update(PluginFClient_Opt *opt)
335 if( selected != opt ) {
336 if( selected ) selected->item_name->set_selected(0);
338 if( selected ) selected->item_name->set_selected(1);
340 clear_box(0,0, 0,panel->get_y());
341 char str[BCTEXTLEN], *sp;
343 if( opt ) opt->types(sp);
346 if( opt ) opt->ranges(sp);
348 while( units->total_items() ) units->del_item(0);
349 ArrayList<const AVOption *> opts;
350 int n = !opt ? 0 : opt->units(opts);
351 for( int i=0; i<n; ++i )
352 units->add_item(new BC_MenuItem(opts[i]->name, 0));
353 char unit[BCSTRLEN]; strcpy(unit, "()");
354 if( units->total_items() && opt && opt->opt->unit )
355 strcpy(unit, opt->opt->unit);
356 units->set_text(unit);
357 char val[BCTEXTLEN]; val[0] = 0;
358 if( opt ) opt->get(val, sizeof(val));
361 float v = 0, fmn = 0, fmx = 0;
362 if( opt && !opt->get_range(fmn, fmx) ) v = atof(val);
363 float p = (fmx-fmn) / slider->get_w();
364 slider->set_precision(p);
365 slider->update(slider->get_w(), v, fmn, fmx);
366 pot->set_precision(p);
367 pot->update(v, fmn, fmx);
371 int PluginFClientWindow::update()
373 const char *txt = text->get_text();
374 if( txt && selected ) {
376 selected->item_value->update();
378 ffmpeg->plugin->send_configure_change();
383 void PluginFClientWindow::update_selected()
388 int PluginFClient_OptPanel::selection_changed()
390 PluginFClient_Opt *opt = 0;
391 BC_ListBoxItem *item = get_selection(0, 0);
393 PluginFClient_OptName *opt_name = (PluginFClient_OptName *)item;
397 fwin->panel->set_tooltip(!opt ? 0 : opt->tip());
398 fwin->panel->show_tooltip();
402 void *PluginFClient_Opt::filter_config()
404 return conf->filter_config();
406 const AVClass *PluginFClient_Opt::filter_class()
408 return conf->filter_class();
411 int PluginFClient_Opt::types(char *rp)
415 case AV_OPT_TYPE_FLAGS: cp = "<flags>"; break;
416 case AV_OPT_TYPE_INT: cp = "<int>"; break;
417 case AV_OPT_TYPE_INT64: cp = "<int64>"; break;
418 case AV_OPT_TYPE_DOUBLE: cp = "<double>"; break;
419 case AV_OPT_TYPE_FLOAT: cp = "<float>"; break;
420 case AV_OPT_TYPE_STRING: cp = "<string>"; break;
421 case AV_OPT_TYPE_RATIONAL: cp = "<rational>"; break;
422 case AV_OPT_TYPE_BINARY: cp = "<binary>"; break;
423 case AV_OPT_TYPE_IMAGE_SIZE: cp = "<image_size>"; break;
424 case AV_OPT_TYPE_VIDEO_RATE: cp = "<video_rate>"; break;
425 case AV_OPT_TYPE_PIXEL_FMT: cp = "<pix_fmt>"; break;
426 case AV_OPT_TYPE_SAMPLE_FMT: cp = "<sample_fmt>"; break;
427 case AV_OPT_TYPE_DURATION: cp = "<duration>"; break;
428 case AV_OPT_TYPE_COLOR: cp = "<color>"; break;
429 case AV_OPT_TYPE_CHANNEL_LAYOUT: cp = "<channel_layout>"; break;
430 default: cp = "<undef>"; break;
432 return sprintf(rp, "%s", cp);
434 int PluginFClient_Opt::scalar(double d, char *rp)
437 if( d == INT_MAX ) cp = "INT_MAX";
438 else if( d == INT_MIN ) cp = "INT_MIN";
439 else if( d == UINT32_MAX ) cp = "UINT32_MAX";
440 else if( d == (double)INT64_MAX ) cp = "I64_MAX";
441 else if( d == INT64_MIN ) cp = "I64_MIN";
442 else if( d == FLT_MAX ) cp = "FLT_MAX";
443 else if( d == FLT_MIN ) cp = "FLT_MIN";
444 else if( d == -FLT_MAX ) cp = "-FLT_MAX";
445 else if( d == -FLT_MIN ) cp = "-FLT_MIN";
446 else if( d == DBL_MAX ) cp = "DBL_MAX";
447 else if( d == DBL_MIN ) cp = "DBL_MIN";
448 else if( d == -DBL_MAX ) cp = "-DBL_MAX";
449 else if( d == -DBL_MIN ) cp = "-DBL_MIN";
450 else if( d == 0 ) cp = signbit(d) ? "-0" : "0";
451 else if( isnan(d) ) cp = signbit(d) ? "-NAN" : "NAN";
452 else if( isinf(d) ) cp = signbit(d) ? "-INF" : "INF";
453 else return sprintf(rp, "%g", d);
454 return sprintf(rp, "%s", cp);
457 int PluginFClient_Opt::ranges(char *rp)
459 const AVClass *filt_class = filter_class();
460 if( !filt_class || !filt_class->option ) return 0;
462 case AV_OPT_TYPE_INT:
463 case AV_OPT_TYPE_INT64:
464 case AV_OPT_TYPE_DOUBLE:
465 case AV_OPT_TYPE_FLOAT: break;
468 AVOptionRanges *r = 0;
469 void *obj = &filt_class;
471 if( av_opt_query_ranges(&r, obj, opt->name, AV_OPT_SEARCH_FAKE_OBJ) < 0 ) return 0;
472 for( int i=0; i<r->nb_ranges; ++i ) {
473 cp += sprintf(cp, " ("); cp += scalar(r->range[i]->value_min, cp);
474 cp += sprintf(cp, ".."); cp += scalar(r->range[i]->value_max, cp);
475 cp += sprintf(cp, ")");
477 av_opt_freep_ranges(&r);
481 int PluginFClient_Opt::units(ArrayList<const AVOption *> &opts)
483 if( !this->opt->unit ) return 0;
484 const AVClass *filt_class = filter_class();
485 if( !filt_class || !filt_class->option ) return 0;
486 void *obj = &filt_class;
487 const AVOption *opt = NULL;
488 while( (opt=av_opt_next(obj, opt)) != 0 ) {
489 if( !opt->unit ) continue;
490 if( opt->type != AV_OPT_TYPE_CONST ) continue;
491 if( strcmp(this->opt->unit, opt->unit) ) continue;
494 if( opts[i]->default_val.i64 != opt->default_val.i64 ) continue;
495 if( strlen(opts[i]->name) < strlen(opt->name) ) opts[i] = opt;
504 int PluginFClient_Opt::units(char *rp)
506 ArrayList<const AVOption *> opts;
510 cp += sprintf(cp, " [%s:", this->opt->unit);
511 for( int i=0; i<n; ++i )
512 cp += sprintf(cp, " %s", opts[i]->name);
513 cp += sprintf(cp, "]:");
517 const char *PluginFClient_Opt::tip()
522 int PluginFClient_OptPanel::update()
524 const char *cols[] = { "option", "value", };
525 const int col1_w = 150;
526 int wids[] = { col1_w, get_w()-col1_w };
527 BC_ListBox::update(&items[0], &cols[0], &wids[0], sizeof(items)/sizeof(items[0]));
532 PluginFClientWindow::PluginFClientWindow(PluginFClient *ffmpeg)
533 : PluginClientWindow(ffmpeg->plugin, 600, 300, 600, 300, 1)
535 this->ffmpeg = ffmpeg;
539 PluginFClientWindow::~PluginFClientWindow()
543 void PluginFClientWindow::create_objects()
545 char string[BCTEXTLEN];
548 const char *descr = ffmpeg->config.ffilt->description();
549 if( !descr ) descr = ffmpeg->config.ffilt->filter_name();
550 add_subwindow(title = new BC_Title(x, y, descr));
551 y += title->get_h() + 10;
553 sprintf(string, _("Type: "));
554 add_subwindow(title = new BC_Title(x0, y, string));
555 x0 += title->get_w() + 8;
556 add_subwindow(type = new BC_Title(x0, y, (char *)""));
558 sprintf(string, _("Range: "));
559 add_subwindow(title = new BC_Title(x0, y, string));
560 x0 += title->get_w() + 8;
561 add_subwindow(range = new BC_Title(x0, y, (char *)""));
562 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Reset")) - 8;
563 add_subwindow(reset = new PluginFClientReset(this, x1, y));
564 y += title->get_h() + 10;
566 add_subwindow(units = new PluginFClientUnits(this, x0, y, 120));
567 x0 += units->get_w() + 8;
568 x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
569 add_subwindow(apply = new PluginFClientApply(this, x1, y));
570 add_subwindow(text = new PluginFClientText(this, x0, y, x1-x0 - 8));
571 y += title->get_h() + 10;
572 add_subwindow(pot = new PluginFClientPot(this, x, y));
573 x1 = x + pot->get_w() + 10;
574 add_subwindow(slider = new PluginFClientSlider(this, x1, y+10));
575 y += pot->get_h() + 10;
577 panel_x = x; panel_y = y;
578 panel_w = get_w()-10 - panel_x;
579 panel_h = get_h()-10 - panel_y;
580 panel = new PluginFClient_OptPanel(this, panel_x, panel_y, panel_w, panel_h);
581 add_subwindow(panel);
582 panel->create_objects();
583 ffmpeg->config.update();
588 void PluginFClientWindow::draw()
593 int PluginFClientWindow::resize_event(int w, int h)
595 int x = get_w() - BC_GenericButton::calculate_w(this, _("Reset")) - 8;
596 int y = reset->get_y();
597 reset->reposition_window(x, y);
598 int x1 = get_w() - BC_GenericButton::calculate_w(this, _("Apply")) - 8;
599 int y1 = units->get_y();
600 apply->reposition_window(x1, y1);
601 int x0 = units->get_x() + units->get_w() + 8;
602 int y0 = units->get_y();
603 text->reposition_window(x0,y0, x1-x0-8);
604 x1 = pot->get_x() + pot->get_w() + 10;
605 int w1 = w - slider->get_x() - 20;
606 slider->set_pointer_motion_range(w1);
607 slider->reposition_window(x1, slider->get_y(), w1, slider->get_h());
608 panel_w = get_w()-10 - panel_x;
609 panel_h = get_h()-10 - panel_y;
610 panel->reposition_window(panel_x,panel_y, panel_w, panel_h);
615 PluginFClient::PluginFClient(PluginClient *plugin, const char *name)
617 this->plugin = plugin;
621 plugin_position = -1;
622 filter_position = -1;
624 sprintf(title, "F_%s", name);
625 config.initialize(name);
626 curr_config.initialize(name);
629 PluginFClient::~PluginFClient()
634 bool PluginFClient::is_audio(const AVFilter *fp)
636 if( !fp->outputs ) return 0;
637 if( avfilter_pad_count(fp->outputs) > 1 ) return 0;
638 if( !avfilter_pad_get_name(fp->outputs, 0) ) return 0;
639 if( avfilter_pad_get_type(fp->outputs, 0) != AVMEDIA_TYPE_AUDIO ) return 0;
640 if( !fp->inputs ) return 1;
641 if( avfilter_pad_count(fp->inputs) > 1 ) return 0;
642 if( !avfilter_pad_get_name(fp->inputs, 0) ) return 0;
643 if( avfilter_pad_get_type(fp->inputs, 0) != AVMEDIA_TYPE_AUDIO ) return 0;
646 bool PluginFClient::is_video(const AVFilter *fp)
648 if( !fp->outputs ) return 0;
649 if( avfilter_pad_count(fp->outputs) > 1 ) return 0;
650 if( !avfilter_pad_get_name(fp->outputs, 0) ) return 0;
651 if( avfilter_pad_get_type(fp->outputs, 0) != AVMEDIA_TYPE_VIDEO ) return 0;
652 if( !fp->inputs ) return 1;
653 if( avfilter_pad_count(fp->inputs) > 1 ) return 0;
654 if( !avfilter_pad_get_name(fp->inputs, 0) ) return 0;
655 if( avfilter_pad_get_type(fp->inputs, 0) != AVMEDIA_TYPE_VIDEO ) return 0;
659 NEW_WINDOW_MACRO(PluginFClient, PluginFClientWindow)
661 int PluginFClient::load_configuration()
663 int64_t src_position = get_source_position();
664 KeyFrame *prev_keyframe = get_prev_keyframe(src_position);
665 config.copy_from(curr_config);
666 read_data(prev_keyframe);
667 int ret = !config.equivalent(curr_config) ? 1 : 0;
672 void PluginFClient::render_gui(void *data, int size)
674 PluginFClientConfig *conf = (PluginFClientConfig *)data;
675 config.copy_from(*conf);
676 KeyFrame *keyframe = plugin->server->get_keyframe();
681 void PluginFClient::update_gui()
683 PluginClientThread *thread = plugin->get_thread();
684 if( !thread ) return;
685 PluginFClientWindow *window = (PluginFClientWindow*)thread->get_window();
686 window->lock_window("PluginFClient::update_gui");
687 load_configuration();
688 if( config.update() > 0 )
690 window->unlock_window();
693 const char *PluginFClient::plugin_title()
698 char *PluginFClient::to_upper(char *cp, const char *sp)
701 while( *sp != 0 ) *bp++ = toupper(*sp++);
706 void PluginFClient::save_data(KeyFrame *keyframe)
709 char string[BCTEXTLEN];
711 // cause data to be stored directly in text
712 output.set_shared_output(keyframe->get_data(), MESSAGESIZE);
713 output.tag.set_title(to_upper(string, plugin_title()));
714 const AVClass *filt_class = config.filter_class();
715 if( filt_class && filt_class->option ) {
716 void *obj = config.filter_config();
717 const AVOption *opt = NULL;
718 while( (opt=av_opt_next(obj, opt)) != 0 ) {
720 if( av_opt_get(obj, opt->name, 0, &buf) < 0 ) continue;
721 output.tag.set_property(opt->name, (const char *)buf);
725 if( (config.ffilt->filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
727 if( av_opt_get(config.ffilt->fctx, "threads", 0, &buf) >= 0 && buf ) {
728 output.tag.set_property("threads", (const char *)buf);
734 output.terminate_string();
737 void PluginFClient::read_data(KeyFrame *keyframe)
740 char string[BCTEXTLEN];
741 input.set_shared_input(keyframe->get_data(), strlen(keyframe->get_data()));
743 while( !input.read_tag() ) {
744 to_upper(string, plugin_title());
745 if( !input.tag.title_is(string) ) continue;
746 const AVClass *filt_class = config.filter_class();
747 if( filt_class && filt_class->option ) {
748 void *obj = config.filter_config();
749 const AVOption *opt = NULL;
750 while( (opt=av_opt_next(obj, opt)) != 0 ) {
751 const char *v = input.tag.get_property(opt->name);
752 if( v ) av_opt_set(obj, opt->name, v, 0);
754 if( (config.ffilt->filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
755 const char *v = input.tag.get_property("threads");
756 if( v ) av_opt_set(config.ffilt->fctx, "threads", v, 0);
762 int PluginFClient::activate()
765 avfilter_link(fsrc, 0, ffilt->fctx, 0);
766 avfilter_link(ffilt->fctx, 0, fsink, 0);
767 int ret = avfilter_graph_config(ffilt->graph, NULL);
769 curr_config.copy_from(config);
775 void PluginFClient::reactivate()
777 delete ffilt; ffilt = 0;
781 PluginFAClient::PluginFAClient(PluginServer *server, const char *name)
782 : PluginAClient(server), PluginFClient(this, name)
786 PluginFAClient::~PluginFAClient()
790 int PluginFAClient::activate()
792 if( activated ) return activated;
793 ffilt = PluginFFilter::new_ffilter(name, &config);
795 config.copy_from(curr_config);
796 send_configure_change();
797 send_render_gui(&config, sizeof(config));
798 ffilt = PluginFFilter::new_ffilter(name, &config);
800 AVSampleFormat sample_fmt = AV_SAMPLE_FMT_FLTP;
801 int channels = PluginClient::total_in_buffers;
802 uint64_t layout = (((uint64_t)1)<<channels) - 1;
803 AVFilterGraph *graph = !ffilt ? 0 : ffilt->graph;
804 int ret = !graph ? -1 : 0;
805 if( ret >= 0 && ffilt->filter->inputs ) {
806 char args[BCTEXTLEN];
807 snprintf(args, sizeof(args),
808 "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%jx",
809 1, sample_rate, sample_rate, av_get_sample_fmt_name(sample_fmt), layout);
810 ret = avfilter_graph_create_filter(&fsrc, avfilter_get_by_name("abuffer"),
811 "in", args, NULL, graph);
814 ret = avfilter_graph_create_filter(&fsink, avfilter_get_by_name("abuffersink"),
815 "out", NULL, NULL, graph);
817 ret = av_opt_set_bin(fsink, "sample_fmts",
818 (uint8_t*)&sample_fmt, sizeof(sample_fmt), AV_OPT_SEARCH_CHILDREN);
820 ret = av_opt_set_bin(fsink, "channel_layouts",
821 (uint8_t*)&layout, sizeof(layout), AV_OPT_SEARCH_CHILDREN);
823 ret = av_opt_set_bin(fsink, "sample_rates",
824 (uint8_t*)&sample_rate, sizeof(sample_rate), AV_OPT_SEARCH_CHILDREN);
826 ret = PluginFClient::activate();
827 if( ret < 0 && activated >= 0 ) {
828 ff_err(ret, "PluginFAClient::activate: %s failed\n", plugin_title());
835 static AVRational best_frame_rate(double frame_rate)
837 static const int m1 = 1001*12, m2 = 1000*12;
838 static const int freqs[] = {
839 40*m1, 48*m1, 50*m1, 60*m1, 80*m1,120*m1, 240*m1,
840 24*m2, 30*m2, 60*m2, 12*m2, 15*m2, 48*m2, 0,
843 int freq, best_freq = 0;
844 for( int i=0; (freq = i<30*12 ? (i+1)*1001 : freqs[i-30*12]); ++i ) {
845 double framerate = (double)freq / m1;
846 double err = fabs(frame_rate/framerate - 1.);
847 if( err >= max_err ) continue;
851 return max_err < 0.0001 ?
852 (AVRational) { best_freq, m1 } :
853 (AVRational) { 0, 0 };
856 int PluginFVClient::activate(int width, int height, int color_model)
858 if( activated ) return activated;
859 ffilt = PluginFFilter::new_ffilter(name, &config);
861 config.copy_from(curr_config);
862 send_configure_change();
863 send_render_gui(&config, sizeof(config));
864 ffilt = PluginFFilter::new_ffilter(name, &config);
866 AVPixelFormat pix_fmt = color_model_to_pix_fmt(color_model);
867 AVFilterGraph *graph = !ffilt ? 0 : ffilt->graph;
868 int ret = !graph ? -1 : 0;
869 if( ret >= 0 && ffilt->filter->inputs ) {
870 curr_config.copy_from(config);
871 if( pix_fmt == AV_PIX_FMT_NB ) {
872 int bpp = BC_CModels::calculate_pixelsize(color_model) * 8;
873 int bits_per_comp = bpp / BC_CModels::components(color_model);
874 int alpha = BC_CModels::has_alpha(color_model);
875 pix_fmt = bits_per_comp > 8 ?
876 !alpha ? AV_PIX_FMT_RGB48LE : AV_PIX_FMT_RGBA64LE :
877 !alpha ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_RGBA ;
879 int aspect_w = 1, aspect_h = 1; // XXX
880 AVRational best_rate = best_frame_rate(frame_rate);
881 char args[BCTEXTLEN];
882 snprintf(args, sizeof(args),
883 "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
884 width, height, pix_fmt, best_rate.num, best_rate.den, aspect_w, aspect_h);
885 ret = avfilter_graph_create_filter(&fsrc, avfilter_get_by_name("buffer"),
886 "in", args, NULL, graph);
889 ret = avfilter_graph_create_filter(&fsink, avfilter_get_by_name("buffersink"),
890 "out", NULL, NULL, graph);
892 ret = av_opt_set_bin(fsink, "pix_fmts",
893 (uint8_t*)&pix_fmt, sizeof(pix_fmt), AV_OPT_SEARCH_CHILDREN);
895 ret = PluginFClient::activate();
896 if( ret < 0 && activated >= 0 ) {
897 ff_err(ret, "PluginFVClient::activate() %s\n", plugin_title());
903 int PluginFAClient::get_inchannels()
905 AVFilterContext *fctx = ffilt->fctx;
906 AVFilterLink **links = !fctx->nb_inputs ? 0 : fctx->inputs;
907 return !links ? 0 : links[0]->channels;
910 int PluginFAClient::get_outchannels()
912 AVFilterContext *fctx = ffilt->fctx;
913 AVFilterLink **links = !fctx->nb_outputs ? 0 : fctx->outputs;
914 return !links ? 0 : links[0]->channels;
917 int PluginFAClient::process_buffer(int64_t size, Samples **buffer, int64_t start_position, int sample_rate)
919 int total_in = PluginClient::total_in_buffers;
920 int total_out = PluginClient::total_out_buffers;
921 int in_channels = 0, out_channels = 0;
923 if( load_configuration() )
926 if( plugin_position != start_position )
927 filter_position = plugin_position = start_position;
930 int ret = activate();
931 if( ret >= 0 && !(frame = av_frame_alloc()) ) {
932 fprintf(stderr, "PluginFAClient::process_buffer: av_frame_alloc failed\n");
933 ret = AVERROR(ENOMEM);
936 in_channels = get_inchannels();
937 out_channels = get_outchannels();
938 frame->nb_samples = size;
939 frame->format = AV_SAMPLE_FMT_FLTP;
940 frame->channel_layout = (1<<in_channels)-1;
941 frame->sample_rate = sample_rate;
942 frame->pts = local_to_edl(filter_position);
946 while( ret >= 0 && --retry >= 0 ) {
947 ret = av_buffersink_get_frame(fsink, frame);
948 if( ret >= 0 || ret != AVERROR(EAGAIN) ) break;
949 if( !fsrc ) { ret = AVERROR(EIO); break; }
950 for( int i=0; i<total_in; ++i )
951 read_samples(buffer[i], i, sample_rate, filter_position, size);
952 filter_position += size;
953 ret = av_frame_get_buffer(frame, 0);
955 float **in_buffers = (float **)&frame->extended_data[0];
956 for(int i = 0; i < in_channels; i++) {
957 float *in_buffer = in_buffers[i];
958 int k = i < total_in ? i : 0;
959 double *in_ptr = buffer[k]->get_data();
960 for(int j = 0; j < size; j++) in_buffer[j] = in_ptr[j];
962 ret = av_buffersrc_add_frame_flags(fsrc, frame, 0);
964 if( ret >= 0 && retry < 0 )
965 ret = AVERROR(EAGAIN);
968 int nbfrs = total_out;
969 if( out_channels < nbfrs ) nbfrs = out_channels;
970 float **out_buffers = (float **)&frame->extended_data[0];
973 float *out_buffer = out_buffers[i];
974 double *out_ptr = buffer[i++]->get_data();
975 for(int j = 0; j < size; j++) out_ptr[j] = out_buffer[j];
977 while( i < total_out ) {
978 double *out_ptr = buffer[i++]->get_data();
979 bzero(out_ptr, sizeof(double) * size);
983 int64_t position = PluginFClient::get_source_position();
984 double t0 = (double)position/sample_rate, dt = 1./sample_rate;
985 for( int i=0; i<total_out; ++i ) {
986 double t = t0, *out = buffer[i]->get_data();
987 for( int k=size; --k>=0; t+=dt ) {
988 double w = int(2*t) & 1 ? 2*M_PI*440 : 2*M_PI*697;
992 ff_err(ret, "PluginFAClient::process_buffer() %s\n", plugin_title());
995 av_frame_free(&frame);
996 plugin_position += size;
1001 PluginFVClient::PluginFVClient(PluginServer *server, const char *name)
1002 : PluginVClient(server), PluginFClient(this, name)
1006 PluginFVClient::~PluginFVClient()
1010 int PluginFVClient::process_buffer(VFrame **frames, int64_t position, double frame_rate)
1012 VFrame *vframe = *frames;
1013 int width = vframe->get_w();
1014 int height = vframe->get_h();
1016 if( load_configuration() )
1019 if( plugin_position != position )
1020 filter_position = plugin_position = position;
1022 int colormodel = vframe->get_color_model();
1023 int ret = activate(width, height, colormodel);
1024 AVPixelFormat pix_fmt = fsrc ?
1025 (AVPixelFormat) fsrc->outputs[0]->format :
1026 color_model_to_pix_fmt(colormodel);
1027 if( pix_fmt <= AV_PIX_FMT_NONE || pix_fmt >= AV_PIX_FMT_NB )
1028 pix_fmt = AV_PIX_FMT_RGBA;
1031 if( ret >= 0 && !(frame = av_frame_alloc()) ) {
1032 fprintf(stderr, "PluginFVClient::process_buffer: av_frame_alloc failed\n");
1033 ret = AVERROR(ENOMEM);
1037 while( ret >= 0 && --retry >= 0 ) {
1038 ret = av_buffersink_get_frame(fsink, frame);
1039 if( ret >= 0 || ret != AVERROR(EAGAIN) ) break;
1040 if( !fsrc ) { ret = AVERROR(EIO); break; }
1041 read_frame(vframe, 0, filter_position++, frame_rate, 0);
1042 frame->format = pix_fmt;
1043 frame->width = width;
1044 frame->height = height;
1045 frame->pts = local_to_edl(position);
1046 ret = av_frame_get_buffer(frame, 32);
1047 if( ret < 0 ) break;
1048 ret = transfer_pixfmt(vframe, frame);
1049 if( ret < 0 ) break;
1050 ret = av_buffersrc_add_frame_flags(fsrc, frame, 0);
1052 if( ret >= 0 && retry < 0 )
1053 ret = AVERROR(EAGAIN);
1056 pix_fmt = (AVPixelFormat) frame->format;
1057 ret = transfer_cmodel(vframe, frame);
1060 ff_err(ret, "PluginFVClient::process_buffer() %s\n", plugin_title());
1061 if( position & 1 ) vframe->clear_frame();
1065 av_frame_free(&frame);
1066 return ret >= 0 ? 0 : 1;
1070 PluginFClient_OptName:: PluginFClient_OptName(PluginFClient_Opt *opt)
1073 set_text(opt->opt->name);
1076 PluginFClient_OptValue::PluginFClient_OptValue(PluginFClient_Opt *opt)
1082 void PluginFClient_OptValue::update()
1084 char val[BCTEXTLEN]; val[0] = 0;
1085 opt->get(val, sizeof(val));
1090 PluginFClient_Opt::PluginFClient_Opt(PluginFClientConfig *conf, const AVOption *opt)
1094 item_name = new PluginFClient_OptName(this);
1095 item_value = new PluginFClient_OptValue(this);
1098 PluginFClient_Opt::~PluginFClient_Opt()
1104 const char *PluginFClientConfig::get(const char *name)
1107 if( av_opt_get(filter_config(), name, 0, &bp) >= 0 ||
1108 av_opt_get(ffilt->fctx, name, 0, &bp) >= 0 )
1109 return (const char *)bp;
1112 char *PluginFClient_Opt::get(char *vp, int sz)
1114 const char *val = conf->get(opt->name);
1116 strncpy(vp, val, sz);
1125 void PluginFClientConfig::set(const char *name, const char *val)
1127 if( av_opt_set(filter_config(), name, val, 0) < 0 )
1128 av_opt_set(ffilt->fctx, name, val, 0);
1130 void PluginFClient_Opt::set(const char *val)
1132 conf->set(opt->name, val);
1135 void PluginFFilter::uninit()
1139 static int get_defaults(const char *name, char *args)
1142 char defaults_path[BCTEXTLEN];
1143 FFMPEG::set_option_path(defaults_path, "plugin.opts");
1144 FILE *fp = fopen(defaults_path,"r");
1146 char ff_plugin[BCSTRLEN], ff_args[BCTEXTLEN], *ap = 0;
1147 while( fgets(ff_args, sizeof(ff_args), fp) ) {
1149 if( *cp == ';' ) continue;
1150 if( *cp == '#' ) ++cp;
1151 char *bp = ff_plugin, *ep = bp + sizeof(ff_plugin)-1;
1152 while( bp < ep && *cp && *cp != '\n' && *cp != ' ' ) *bp++ = *cp++;
1154 if( !strcmp(ff_plugin, name) ) { ap = cp; break; }
1158 if( ff_args[0] == '#' ) return -1;
1159 while( *ap == ' ' ) ++ap;
1160 while( *ap && *ap != '\n' ) *args++ = *ap++;
1165 bool PluginFFilter::is_audio()
1167 return PluginFClient::is_audio(filter);
1170 bool PluginFFilter::is_video()
1172 return PluginFClient::is_video(filter);
1175 int PluginFFilter::init(const char *name, PluginFClientConfig *conf)
1177 char args[BCTEXTLEN];
1178 int ret = get_defaults(name, args);
1179 if( ret < 0 ) return 0;
1180 PluginFLogLevel errs(AV_LOG_ERROR);
1181 this->filter = avfilter_get_by_name(name);
1182 if( !this->filter ) return AVERROR(ENOENT);
1183 int flag_mask = AVFILTER_FLAG_DYNAMIC_INPUTS | AVFILTER_FLAG_DYNAMIC_OUTPUTS;
1184 if( filter->flags & flag_mask ) return AVERROR(EPERM);
1185 if( !this->is_audio() && !this->is_video() ) return AVERROR(EIO);
1186 this->graph = avfilter_graph_alloc();
1187 if( !this->graph ) return AVERROR(ENOMEM);
1188 static int inst = 0;
1189 char inst_name[BCSTRLEN];
1190 sprintf(inst_name,"%s_%d", name, ++inst);
1191 if( conf && (filter->flags & AVFILTER_FLAG_SLICE_THREADS) != 0 ) {
1192 graph->thread_type = AVFILTER_THREAD_SLICE;
1193 graph->nb_threads = atoi(conf->get("threads"));
1196 graph->thread_type = 0;
1197 graph->nb_threads = 0;
1199 fctx = avfilter_graph_alloc_filter(graph, filter, inst_name);
1200 if( !fctx ) return AVERROR(ENOMEM);
1201 fctx->thread_type = graph->thread_type; // bug in avfilter
1203 AVDictionary *opts = 0;
1204 for( int i=0; i<conf->size(); ++i ) {
1205 PluginFClient_Opt *op = conf->get(i);
1206 const char *name = op->opt->name;
1207 char val[BCTEXTLEN], *vp = op->get(val, sizeof(val));
1210 // unspecified opts cause a special behavior in some filters (lut3d)
1211 // so... if opt value is the default, skip it or no special behavior
1212 if( av_opt_get(filter_config(), name, 0, &bp) >= 0 ) {
1213 int result = strcmp((const char *)bp, vp);
1215 if( !result ) continue;
1217 av_dict_set(&opts, name, vp, 0);
1219 ret = avfilter_init_dict(fctx, &opts);
1220 av_dict_free(&opts);
1223 ret = avfilter_init_str(fctx, args);
1224 return ret < 0 ? ret : 1;
1228 PluginFFilter::PluginFFilter()
1235 PluginFFilter::~PluginFFilter()
1237 PluginFLogLevel errs(AV_LOG_ERROR);
1238 avfilter_graph_free(&graph);
1239 filter = 0; fctx = 0;
1242 PluginFFilter *PluginFFilter::new_ffilter(const char *name, PluginFClientConfig *conf)
1244 PluginFFilter *ffilt = new PluginFFilter;
1245 int ret = ffilt->init(name, conf);
1246 if( ret < 0 ) ff_err(ret, "PluginFFilter::new_ffilter(%s)\n", name);
1247 if( ret <= 0 ) { delete ffilt; ffilt = 0; }
1251 PluginClient *PluginServer::new_ffmpeg_plugin()
1253 const AVFilter *filter = avfilter_get_by_name(ff_name);
1254 if( !filter ) return 0;
1255 PluginFClient *ffmpeg =
1256 PluginFClient::is_audio(filter) ?
1257 (PluginFClient *)new PluginFAClient(this, ff_name) :
1258 PluginFClient::is_video(filter) ?
1259 (PluginFClient *)new PluginFVClient(this, ff_name) :
1261 if( !ffmpeg ) return 0;
1262 return ffmpeg->plugin;
1266 PluginServer* MWindow::new_ffmpeg_server(MWindow *mwindow, const char *name)
1268 PluginFFilter *ffilt = PluginFFilter::new_ffilter(name);
1269 if( !ffilt ) return 0;
1271 return new PluginServer(mwindow, name, PLUGIN_TYPE_FFMPEG);
1274 void MWindow::init_ffmpeg_index(MWindow *mwindow, Preferences *preferences, FILE *fp)
1276 PluginFLogLevel errs(AV_LOG_ERROR);
1277 const AVFilter *filter = 0; void *idx = 0;
1278 while( (filter=av_filter_iterate(&idx)) != 0 ) {
1279 //printf("%s\n",filter->name);
1280 PluginServer *server = new_ffmpeg_server(mwindow, filter->name);
1282 int result = server->open_plugin(1, preferences, 0, 0);
1284 server->write_table(fp, filter->name, PLUGIN_FFMPEG_ID, 0);
1285 server->close_plugin();
1288 if( result ) fprintf(fp, "#%s\n", filter->name);
1293 void MWindow::init_ffmpeg()