-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathofxProgrammingGuide.xml
More file actions
1183 lines (947 loc) · 46.6 KB
/
ofxProgrammingGuide.xml
File metadata and controls
1183 lines (947 loc) · 46.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<book>
<bookinfo>
<title>OpenFX Plug-in API Programming Guide</title>
<author>
<firstname>Bruno</firstname>
<surname>Nicoletti</surname>
</author>
<copyright>
<year>2005-2015</year>
<holder>The Open Effects Association</holder>
</copyright>
<releaseinfo>Document version 1.4</releaseinfo>
</bookinfo>
<preface>
<title>Foreword</title>
<para>OFX is an open API for writing visual effects plug-ins for a wide
variety of applications, such as video editing systems and compositing
systems. It seems to have two interchangable names, "OpenFX" and "OFX". I
prefer OFX for some reason. This book is actually very broad in scope, as
it has to not only define the API, it will need to go into some depth
describing concepts behind imaging applications that use it.</para>
<para>UNFINISHED</para>
<section>
<title>Intended Audience</title>
<para>Do you write visual effects or image processing software? Do you
have an application which deals with moving images and hosts plug-ins or
would like to host plug-ins? Then OFX is for you.</para>
<para>This book assumes you can program in the "C" language and are
familiar with the concepts involved in writing visual effects software.
You need to understand concepts like pixels, clips, pixel aspect ratios
and more. If you don't, I suggest you read further or attempt to soldier
on bravely and see how far you go before you get lost.</para>
</section>
<section>
<title>What is OFX</title>
<para>OFX is actually several things. At the lowest level OFX is a
generic 'C' based plug-in architecture that can be used to define any
kind of plug-in API. You could use this low level architecture to
implement any API, however it was originally designed to host our visual
effects image processing API. The basic architecture could be re-used to
create other higher level APIs such as a sound effects API, a 3D API and
more.</para>
<para>This book describes the basic OFX plug-in architecture and the
visual effects plug-in API built on top of it. The visual effects API is
very broad and intended to allow visual effects plug-ins to work on a
wide range of host applications, including compositing hosts,
rotoscopers, encoding applications, colour grading hosts, editing hosts
and more I haven't thought of yet. While all these type of applications
process images, they often have very different work flows and present
effects to a user in incompatible ways. OFX is an attempt to deal with
all of these in a clear and consistent manner.</para>
</section>
</preface>
<chapter>
<title>OFX Overview</title>
<section>
<para> This chapter provides a brief overview of the basic plug-in architecture and some of the reasoning behind it. It will touch lightly .</para>
</section>
<section>
<title>Language</title>
<para>The OFX API is specified using the 'C' programming language. The traditional languages used to implement visual effects applications and plug-ins have been C and C++, so we stuck with that school. By making the API C, rather than C++, you remove the whole set of problems around C++ symbol mangling between the host an application.
</para>
</section>
<section>
<title>API Definition</title>
<para>The APIs defined by OFX are defined purely by a set of C header files and the associated documentation. There are no binary libraries to link against.
</para>
</section>
<section>
<title>Symbolic Dependancies</title>
<para>The host relies on two symbols within a plug-in, all other communication is boot strapped from those two symbols. The plug-in has no symbolic dependancies from the host. This minimal symbolic dependancy allows for run-time determination of what features to provide over the API, making implementation much more flexible and less prone to backwards compatibility problems.
</para>
</section>
<section>
<title>Host to Plugin Communication</title>
<para>A host communicates with a plug-in via a
</para>
</section>
<section>
<title>Host to Plugin Communication</title>
<para>A host communicates with a plug-in via a
</para>
</section>
</chapter>
<chapter>
<title>Introduction To OFX, A Simple Plug-in</title>
<section>
<para>This chapter will describe in detail a fully functioning, but
simple OFX image effect plug-in that inverts 8 bit RGBA images. It gives
a flavour for how the API operates and some of the components in it. The
source will be broken down into sections and each one explained as we go
along. The code is actually C++, but it uses the raw OFX API. The code
is not meant to be perfectly optimised example of how to write an image
processing effect, but rather as an illustrative example of the
API.</para>
</section>
<section>
<title>Headers</title>
<informalexample>
<programlisting>
#include <string.h>
#include "ofxCore.h"
#include "ofxProperty.h"
#include "ofxImageEffect.h"</programlisting>
</informalexample>
<para>This section of code shows the headers used by the invert plug-in.
It includes three OFX header files (you can tell because they start with
"ofx") which are... <itemizedlist>
<listitem> <filename>ofxCore.h</filename> which provides definitions for the underlying plug-in loading mechanisms </listitem>
<listitem> <filename>ofxProperty.h</filename> which defines the property getting/setting suite </listitem>
<listitem> <filename>ofxImageEffect.h</filename> which provides access to the image effect suite </listitem>
</itemizedlist> The system include file <filename>string.h</filename>
is for use of strcmp as the API passes strings about rather than
integers or enums to specify actions and properties.</para>
</section>
<section>
<title>The Plug-in Struct and The Two Exported Functions</title>
<informalexample>
<programlisting>
// forward declaration of two functions needed by the plug-in struct
static OfxStatus pluginMain(const char *action, const void *handle, OfxPropertySetHandle inArgs, OfxPropertySetHandle outArgs);
static void setHostFunc(OfxHost *hostStruct);
////////////////////////////////////////////////////////////////////////////////
// the plugin struct
static OfxPlugin pluginStruct =
{
kOfxImageEffectPluginApi,
1,
"net.sf.openfx.OfxInvertExample",
1,
0,
setHostFunc,
pluginMain
};
// the mandated function to return the nth plug-in
OfxExport OfxPlugin *
OfxGetPlugin(int nth)
{
if(nth == 0)
return &pluginStruct;
return 0;
}
// the mandated function to return the number of plug-ins in this binary
OfxExport int
OfxGetNumberOfPlugins(void)
{
return 1;
}</programlisting>
</informalexample>
<para>The first thing to note in this section are the two functions
<function>OfxGetPlugin</function> and
<function>OfxGetNumberOfPlugins</function>. These two functions are the
only symbols that a plug-in must export and are there to boot strap the
whole plug-in identification and definition process. Note the macro
<structname>OfxExport</structname>, which should be inserted before any
symbol that needs to be exported from the plug-in.</para>
<para><function>OfxGetNumberOfPlugins</function> is the first function
by a host after a binary is loaded. It returns the number of separate
plug-ins contained inside the binary. In this case we have only one
plug-in.</para>
<para><function>OfxGetPlugin</function> is called once for each plug-in
inside the binary, and returns an <structname>OfxPlugin</structname>
struct. This struct is used to tell the host what kind of plug-in it is,
it's name, versioning info and to give the host two functions to use for
communications. Our function returns a pointer to the static
<varname>pluginStruct</varname>. All such structs should be static to
avoid memory allocation issues, otherwise they will not be deleted by
the host.</para>
<para>In order the members of <varname>pluginStruct</varname> tell the
host... <orderedlist>
<listitem>
the kind of plug-in it is, in this case a
<literal>kOfxImageEffectPluginApi</literal>
. Which happens to be an image effect plug-in,
</listitem>
<listitem>
the version of the API the plug-in was written to. In this case version
<literal>1</literal>
,
</listitem>
<listitem>
the unique name of the plug-in. Used only to disambiguate the plug-in from all other plug-ins, not necessarily for human eyes,
</listitem>
<listitem>
the major and minor version of the plug-in, in this case
<literal>1.0</literal>
, which allows for simplified upgrades to the plug-in,
</listitem>
<listitem>
a function used to set an important data structure in the plug-in,
</listitem>
<listitem>
the function which the host uses to send messages to the plug-in
</listitem>
</orderedlist></para>
</section>
<section>
<title>The Host Struct</title>
<informalexample>
<programlisting>
// pointer to the host structure passed to us by the host application
OfxHost *gHost;
// function called by the host application to set the host structure
static void
setHostFunc(OfxHost *hostStruct)
{
gHost = hostStruct;
}
</programlisting>
</informalexample>
<para>This section has the structure that holds information about the
host application and allows us to fetch <structname>suites</structname>
from the host. The <function>setHostFunc</function> is called by the
host application to set this pointer and is passed to the host inside
the <structname>OfxPlugin</structname> struct.</para>
</section>
<section>
<title>The Main Entry Function</title>
<informalexample>
<programlisting>
// forward declarations
static OfxStatus render(OfxImageEffectHandle instance, OfxPropertySetHandle inArgs, OfxPropertySetHandle outArgs);
static OfxStatus describe(OfxImageEffectHandle effect);
static OfxStatus onLoad(void);
static OfxStatus describeInContext( OfxImageEffectHandle effect, OfxPropertySetHandle inArgs)
// The main entry point function
static OfxStatus
pluginMain(const char *action, const void *handle, OfxPropertySetHandle inArgs, OfxPropertySetHandle outArgs)
{
// cast to appropriate type
OfxImageEffectHandle effect = (OfxImageEffectHandle) handle;
// called as the very first action before any other
if(strcmp(action, kOfxActionLoad) == 0) {
return onLoad();
}
// called to describe the plug-in
else if(strcmp(action, kOfxActionDescribe) == 0) {
return describe(effect);
}
// called to describe the plug-in in a given context
else if(strcmp(action, kOfxImageEffectActionDescribeInContext) == 0) {
return describeInContext(effect, inArgs);
}
// called to render a frame
else if(strcmp(action, kOfxImageEffectActionRender) == 0) {
return render(effect, inArgs, outArgs);
}
// all other actions return the default value
return kOfxStatReplyDefault;
}
</programlisting>
</informalexample>
<para>The <function>pluginMain</function> was passed to the host int the
<structname>OfxPlugin</structname> struct returned by
<function>OfxGetPlugin</function>. This function is where the host tells
the plug-in what it needs to do. You will note that the
<emphasis>actions</emphasis> passed to the plug-in are encoded as
strings. This makes actions easily extensible whilst minimising the
chances of name clashing.</para>
<para>The function also takes a "handle" argument. This handle is the
thing the action must be performed on. In this case, as we are an image
effect, the handle must be cast to an
<structname>OfxImageEffectHandle</structname>, which is simply a blind
pointer to some host data representing an image effect.</para>
<para>The last two parameters <varname>inArgs</varname> and
<varname>outArgs</varname> are used to pass in arguments for the
specific action. These are encapsulated via blind property sets. The
<varname>inArgs</varname> are extra arguments the action will need to do
its job, whilst <varname>outArgs</varname> are for things the plug-in
will need to set to do its action.</para>
<para>Our main entry only traps four of the many possible actions. The
actual actions will be described below.</para>
</section>
<section>
<title>The Load Action</title>
<informalexample>
<programlisting>
OfxImageEffectSuiteV1 *gEffectSuite = 0;
OfxPropertySuiteV1 *gPropertySuite = 0;
bool supportsNullSuiteFunctionPointers = false;
////////////////////////////////////////////////////////////////////////////////
// Called at load
static OfxStatus
onLoad(void)
{
// fetch the host suites out of the global host pointer
if(!gHost) return kOfxStatErrMissingHostFeature;
gEffectSuite = (OfxImageEffectSuiteV1 *) gHost->fetchSuite(gHost->host, kOfxImageEffectSuite, 1, supportsNullSuiteFunctionPointers);
gPropertySuite = (OfxPropertySuiteV1 *) gHost->fetchSuite(gHost->host, kOfxPropertySuite, 1, supportsNullSuiteFunctionPointers);
if(!gEffectSuite || !gPropertySuite)
return kOfxStatErrMissingHostFeature;
return kOfxStatOK;
}
</programlisting>
</informalexample>
<para>The load action is always the first action called after the
initial boot-strap phase has happened. By this time the plug-in's
<function>setHost</function> function will have been called, so we have
access to the host pointer and so the ability to fetch suites.</para>
<para>What this action does is first to check that the host application
has in fact set the <varname>gHost</varname> pointer, it then goes ahead
and fetches two <structname>suites</structname> from the host. These
suites are simply structs full of function pointers from the host which
the plug-in uses to communicate with the host. We are only fetching two
suites from the host, the <structname>OfxImageEffectSuiteV1</structname>
to give us access to images and more, and the
<structname>OfxPropertySuiteV1</structname> which allows us to access
values in property sets.</para>
</section>
<section>
<title>The Describe Action</title>
<informalexample>
<programlisting>
// the plug-in's description routine
static OfxStatus
describe(OfxImageEffectHandle effect)
{
// get the property handle for the plugin
OfxPropertySetHandle effectProps;
gEffectSuite->getPropertySet(effect, &effectProps);
// say we cannot support multiple pixel depths and let the clip preferences action deal with it all.
gPropertySuite->propSetInt(effectProps, kOfxImageEffectPropSupportsMultipleClipDepths, 0, 0);
// set the bit depths the plug-in can handle
gPropertySuite->propSetString(effectProps, kOfxImageEffectPropSupportedPixelDepths, 0, kOfxBitDepthByte);
// set plug-in label and the group it belongs to
gPropertySuite->propSetString(effectProps, kOfxPropLabel, 0, "OFX Invert Example");
gPropertySuite->propSetString(effectProps, kOfxImageEffectPluginPropGrouping, 0, "OFX Example");
// define the contexts we can be used in
gPropertySuite->propSetString(effectProps, kOfxImageEffectPropSupportedContexts, 0, kOfxImageEffectContextFilter);
return kOfxStatOK;
}
</programlisting>
</informalexample>
<para>The purpose of the describe action is to tell the host the overall
behaviour of the plug-in. The
<structname>OfxImageEffectHandle</structname> passed to the describe
action is a <emphasis>descriptor</emphasis> as opposed to an
<emphasis>instance</emphasis>. A descriptor is a handle that is used to
tell the host how a plug-in should look, whilst an instance is an actual
running copy of a plug-in. Descriptors are passed to only two actions,
the describe action, and the describe in context action. Most other
actions that are passed an instance handle.</para>
<para>The first thing this function does is to fetch the handle holding
the properties used to describe the plug-in. Note how it uses the
<varname>gEffectSuite</varname> pointer to do this.</para>
<para>The next thing the plug-in does is set the integer property
<literal>kOfxImageEffectPropSupportsMultipleClipDepths</literal> to be
false using the property suite. This tells the host application that the
plug-in can only support a single pixel depth at a time, otherwise the
host might attempt to have an 8 bit input and a 16 bit output.</para>
<para>Note how the property setting functions take four arguments. The
first is the set of properties to be modified, the second is the name
(again a string) of the property in that set they wish to change, the
third is the index of the property (as properties can be
multi-dimensional) and the last is the value they wish to set.</para>
<para>The plug-in goes on to set properties that describe it as
supporting only images of 8 bits per component, the user visible name of
the effect, the grouping the effect should be placed in on any user
interface and finally the context it can be used under.</para>
<para>An image effect context describes the semantics of how a plug-in
should be used. In this case we are saying the plug-in is a filter,
which means it has to have one input clip called "Source" and one output
clip called "Output". Other contexts include transitions, where an image
effect has two inputs and a single output and is require to go from one
to the other, a general context for tree based compositing systems and
several more. A plug-in can say that it works in more than one context,
which brings us on to the next function.</para>
</section>
<section>
<title>The Describe In Context Action</title>
<informalexample>
<programlisting>
// describe the plug-in in context
static OfxStatus
describeInContext( OfxImageEffectHandle effect, OfxPropertySetHandle inArgs)
{
OfxPropertySetHandle props;
// define the single output clip in both contexts
gEffectSuite->clipDefine(effect, "Output", &props);
// set the component types we can handle on out output
gPropertySuite->propSetString(props, kOfxImageEffectPropSupportedComponents, 0, kOfxImageComponentRGBA);
// define the single source clip in both contexts
gEffectSuite->clipDefine(effect, "Source", &props);
// set the component types we can handle on our main input
gPropertySuite->propSetString(props, kOfxImageEffectPropSupportedComponents, 0, kOfxImageComponentRGBA);
return kOfxStatOK;
}
</programlisting>
</informalexample>
<para>The describe in context action is called once for each context
that a plug-in says it can work in. Here the plug-in must define the
clips it will use and any parameters it may need for that context. The
set of clips and parameters need not be the same for each context,
though typically a core of parameters and some clips will be the same.
Note that, as with the describe action, the describe in context action
is passed a descriptor rather than an instance.</para>
<para>The first thing our function does is to define the clip called
"Output" which is mandatory for the filter context. This function
returns a property set which is used to describe how the effect wants to
deal with that clip. In this case the effect says that it will only
accept RGBA images on the output. Next it does the same for the mandated
"Source" clip. Having defined the two clips needed for this context it
returns.</para>
</section>
<section>
<title>The Render Action</title>
<informalexample>
<programlisting>
// look up a pixel in the image, does bounds checking to see if it is in the image rectangle
inline OfxRGBAColourB *
pixelAddress(OfxRGBAColourB *img, OfxRectI rect, int x, int y, int bytesPerLine)
{
if(x < rect.x1 || x >= rect.x2 || y < rect.y1 || y > rect.y2)
return 0;
OfxRGBAColourB *pix = (OfxRGBAColourB *) (((char *) img) + (y - rect.y1) * bytesPerLine);
pix += x - rect.x1;
return pix;
}
// the process code that the host sees
static OfxStatus render(OfxImageEffectHandle instance,
OfxPropertySetHandle inArgs,
OfxPropertySetHandle outArgs)
{
// get the render window and the time from the inArgs
OfxTime time;
OfxRectI renderWindow;
gPropertySuite->propGetDouble(inArgs, kOfxPropTime, 0, &time);
gPropertySuite->propGetIntN(inArgs, kOfxImageEffectPropRenderWindow, 4, &renderWindow.x1);
// fetch output clip
OfxImageClipHandle outputClip;
gEffectSuite->clipGetHandle(instance, "Output", &outputClip, 0);
// fetch image to render into from that clip
OfxPropertySetHandle outputImg;
gEffectSuite->clipGetImage(outputClip, time, NULL, &outputImg);
// fetch output image info from that handle
int dstRowBytes, dstBitDepth;
OfxRectI dstRect;
void *dstPtr;
gPropertySuite->propGetInt(outputImg, kOfxImagePropRowBytes, 0, &dstRowBytes);
gPropertySuite->propGetIntN(outputImg, kOfxImagePropBounds, 4, &dstRect.x1);
gPropertySuite->propGetPointer(outputImg, kOfxImagePropData, 0, &dstPtr);
// fetch main input clip
OfxImageClipHandle sourceClip;
gEffectSuite->clipGetHandle(instance, "Source", &sourceClip, 0);
// fetch image at render time from that clip
OfxPropertySetHandle sourceImg;
gEffectSuite->clipGetImage(sourceClip, time, NULL, &sourceImg);
// fetch image info out of that handle
int srcRowBytes, srcBitDepth;
OfxRectI srcRect;
void *srcPtr;
gPropertySuite->propGetInt(sourceImg, kOfxImagePropRowBytes, 0, &srcRowBytes);
gPropertySuite->propGetIntN(sourceImg, kOfxImagePropBounds, 4, &srcRect.x1);
gPropertySuite->propGetPointer(sourceImg, kOfxImagePropData, 0, &srcPtr);
// cast data pointers to 8 bit RGBA
OfxRGBAColourB *src = (OfxRGBAColourB *) srcPtr;
OfxRGBAColourB *dst = (OfxRGBAColourB *) dstPtr;
// and do some inverting
for(int y = renderWindow.y1; y < renderWindow.y2; y++) {
if(gEffectSuite->abort(instance)) break;
OfxRGBAColourB *dstPix = pixelAddress(dst, dstRect, renderWindow.x1, y, dstRowBytes);
for(int x = renderWindow.x1; x < renderWindow.x2; x++) {
OfxRGBAColourB *srcPix = pixelAddress(src, srcRect, x, y, srcRowBytes);
if(srcPix) {
dstPix->r = 255 - srcPix->r;
dstPix->g = 255 - srcPix->g;
dstPix->b = 255 - srcPix->b;
dstPix->a = 255 - srcPix->a;
}
else {
dstPix->r = 0;
dstPix->g = 0;
dstPix->b = 0;
dstPix->a = 0;
}
dstPix++;
}
}
// we are finished with the source images so release them
gEffectSuite->clipReleaseImage(sourceImg);
gEffectSuite->clipReleaseImage(outputImg);
// if we aborted, then we have failed to produce and image, so say so
if(gEffectSuite->abort(instance)) return kOfxStatFailed;
// otherwise all was well
return kOfxStatOK;
}
</programlisting>
</informalexample>
<para>The render action is where a plug-in turns its input images into
output images. The first thing to note is that the effect is no longer a
descriptor by an. For our simple example, this makes not much
difference, however if the plug-in is maintaining private data, it is
very important.</para>
<para>The first thing the render function does is to extract two
properties from the <varname>inArgs</varname> property set. These are
the time to render at, and the window to render over. Note that the
render window is a 4 dimensional integer property.</para>
<para>Next the function fetches the output clip. A clip is a handle that
is accessible by name and represents a sequences of images. The returned
handle is valid for the lifetime of the instance, and so does not need
releasing or deleting. Next an image is extracted from the clip. An
image, unlike a clip, does need to be released when the plug-in is done
with it.</para>
<para>Images are encapsulated as a property set, and so the ordinary
property mechanism is used to fetch out information from the image. In
this case three properties are needed, the <varname>rowBytes</varname>,
or the number of bytes in a scan line (as there may be padding at the
end), the image bounds, being the region where there is image data and a
pointer to the image data. The source image is fetched in a similar way
as the destination image.</para>
<para>Next the <structname>void *</structname> data pointers are cast to
<structname>OfxRGBAColourB</structname> pointers, and we start iterating
over the render window filling in output pixels. The render window must
always be equal to or less than the bounds on the destination image. The
inline function <function>pixelAddress</function> shows how pixel
arithmetic is performed. Note the <function>abort</function> function
from the image effect suite being called at every scan line to see if
the rendering should be halted.</para>
<para>Finally, once we are done, the images are released and we
return.</para>
</section>
<section>
<title>In Summary</title>
<para>Our small working example shows the core mechanisms behind the OFX
Image Effect API, but leaves out much of the messy detail. The important
things to note are the use of suites to call functions on the host, the
use of properties to get and set values in objects and the use of
actions to call things on the plug-in.</para>
<para>There are many more suites which have not been used, as well as
actions that have not been trapped, as well as gory bits to do with
pixel aspect ratios, interlacing and more. These extra suites and
actions add a rich functionality to OFX that give it a great flexibility
as well as dealing with the evil little details such as video
standards.</para>
</section>
</chapter>
<chapter>
<title>The Generic OFX Plug-in Architecture</title>
<section>
<title>Overview</title>
<para>As explained in the introduction, OFX is actually several things,
<itemizedlist>
<listitem>
an underlying generic plug-in architecture,
</listitem>
<listitem>
specific plug-in APIs defined using that architecture.
</listitem>
</itemizedlist> This chapter concentrates on defining the underlying
architecture, including how plug-ins should be identified, defined,
versioned, installed and the initial bootstrapping of communication
occurs.</para>
<para>The two OFX include files relevant for this section are
<itemizedlist>
<listitem>
<filename>ofxCore.h</filename>
which defines basic data structures used in loading and identifying plug-ins,
</listitem>
<listitem>
<filename>ofxProperty.h</filename>
which defines the suite used for fetching and setting properties on objects.
</listitem>
</itemizedlist></para>
<para>OFX APIs are all implemented via prototyped "C" header files.
There are no OFX specific libraries to link against as the APIs consists
solely of sets of header files and their supporting documentation. The
API is also designed to minimise symbolic dependencies between the host
and the plug-in.</para>
<para>Certain concepts are at the core of the OFX plug-in architecture,
these are <emphasis>APIs</emphasis>, <emphasis>actions</emphasis>,
<emphasis>suites</emphasis>, <emphasis>properties</emphasis> and
<emphasis>handles</emphasis>. No matter what kind of API is being built
on top of the core architecture, it will use these objects.</para>
<variablelist>
<varlistentry>
<term>Actions</term>
<listitem>
<para>An action is how a host communicates with a plug-in, e.g.:
"render frame 10". Actions are labelled by strings passed to a
plug-in's 'mainEntry' function.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Suites</term>
<listitem>
<para>A suite is how a plug-in communicates to a host. Suites are
'C' structs filled with pointers to functions, the plug-in calls
these functions to perform certain tasks (e.g.: fetch an
image).</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Handles</term>
<listitem>
<para>Handles are blind data pointers that are passed back to a
plug-in from a host. They represent various types of data (e.g.: a
set of properties, an instance of an image effect and so on) and
are how objects on the host side are referenced by a plug-in. The
data pointers themselves are typed by declaring them as pointers
to defined but undeclared C structs.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Properties</term>
<listitem>
<para>Properties are simple data types used to communicate
information between a host and a plug-in. They avoid having to
pass around C structs and so makes versioning and backwards
compatibility easier. They are name/value pairs labelled with a
C-string. Properties are accessed using the property suite found
in <filename>ofxProperty.h</filename>.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>APIs</term>
<listitem>
<para>An 'API', in the OFX sense, is a named set of suites,
actions and supporting objects used to implement a type of
plug-in. Currently there is only one API built on the core
architecture, this is the OFX Image Effect API, which is labelled
by the string <literal>"OfxImageEffectPluginAPI"</literal></para>
</listitem>
</varlistentry>
</variablelist>
</section>
<section>
<title>Plug-in Binaries and Required Symbols</title>
<para>OFX plug-ins are distributed as dynamically loadable shared
libraries, the exact format is dependant upon the host operating system
and is defined below. Each such binary can contain one or more OFX
plug-ins .The whole set of OFX APIs bootstrap from two symbols inside a
plug-in, these are <function>OfxGetNumberOfPlugins</function> and
<function>OfxGetPlugin.</function></para>
<section>
<title>OfxGetNumberOfPlugins</title>
<programlisting>int OfxGetNumberOfPlugins(void)</programlisting>
This function returns the number of plug-ins inside the given binary. This will be the first function called by a host after initially loading the binary.
</section>
<section>
<title>OfxGetPlugin</title>
<programlisting>OfxPlugin * OfxGetPlugin(int nth)</programlisting>
This function returns an
<structname>OfxPlugin</structname>
struct which defines the
<varname>nth</varname>
plug-in to the host. The data structure returned should point to a static chunk of memory in the plug-in, as the host will make no attempt to free the returned data structure when the plug-in is unloaded. Typically this will be the called multiple times by the host, once for each plug-in that the plug-in says it contains.
</section>
</section>
<section>
<title>Identifying, Versioning and Defining an OFX Plug-in</title>
<para>A plug-in's description is bootstrapped by using the required
<function>OfxGetPlugin</function> function. This function returns an
<structname>OfxPlugin</structname> data structure which gives a basic
description of the plug-in.</para>
&ofxPluginStruct;
</section>
<section>
<title>Suites, Hosts and APIs</title>
<section>
<title>Function Suites</title>
<para>A function suite is simply a struct filled with function
pointers. Below is some code from the memory allocation suite found in
<filename>ofxMemory.h</filename>.</para>
<programlisting>
#define kOfxMemorySuite "OfxMemorySuite"
typedef struct OfxMemorySuiteV1 {
OfxStatus (*memoryAlloc)(void *handle, size_t nBytes, void **allocatedData);
OfxStatus (*memoryFree)(void *allocatedData);
} OfxMemorySuiteV1;
</programlisting>
<para>The use is fairly obvious, you call functions through the
pointers in the struct. This has the effect of avoiding any symbolic
dependencies from the plug-in to the host. Note two other things about
this code listing. <itemizedlist>
<listitem>
the suite has a cstring name associated with it,
</listitem>
<listitem>
the suite is versioned, via the "V1" suffix appended to the struct tag and typedef.
</listitem>
</itemizedlist> The explicit naming and versioning of suites is how
a plug-in fetches one, by calling the <function>fetchSuite</function>
function in the <structname>OfxHost</structname> struct which is
passed to it during the boot strapping stage.</para>
</section>
&ofxHostStruct;
</section>
&ofxLoadingSequence;
<section>
<title>Errors, Status Codes and Exceptions</title>
<para>OFX has a standard set of unsigned integer status codes used to
indicate errors and general status. These are returned by most functions
in the host suites and by the plug ins' entry points. The error codes
returned by functions and actions are documented along side them.</para>
<para>It is an error to throw a C++ exception across the API, all
exceptions should be trapped on either side of the API and an
appropriate error code passed instead.</para>
</section>
<section>
<title>Packaging OFX Plug-ins</title>
<para>Where a host application chooses to search for OFX plug ins, what
binary format they are in and any directory hierarchy is entirely up to
it. However, it is strongly recommended that the following scheme be
followed.</para>
<section>
<title>Binary Types</title>
<para>Plug-ins should be distributed in the following formats,
depending on the host operating system.... <itemizedlist>
<listitem>
Microsoft Windows, as ".dll" dynamically linked libraries,
</listitem>
<listitem>
Apple OSX, as Mach-O binary bundles,
</listitem>
<listitem>
IRIX and LINUX, as ELF dynamic shared objects.
</listitem>
</itemizedlist></para>
</section>
<section>
<title>Installation Directory Hierarchy</title>
<para>Each plug-in binary is distributed as a Mac OS X package style
directory hierarchy. Note that the there are two distinct meanings of
'bundle', one referring to a binary file format, the other to a
directory hierarchy used to distribute software. We are distributing
binaries in a bundle package, and in the case of OSX, the binary is a
binary bundle. All the binaries must end with
<literal>".ofx"</literal>, regardless of the host operating
system.</para>
<para>The directory hierarchy is as follows..... <itemizedlist>
<listitem>
NAME.ofx.bundle
<itemizedlist>
<listitem>Contents <itemizedlist>
<listitem>Info.plist</listitem>
<listitem>Resources <itemizedlist>
<listitem>NAME.xml</listitem>
<listitem>PLUGIN_A.png</listitem>
<listitem>PLUGIN_B.png</listitem>
<listitem>...</listitem>
<listitem>PLUGIN_N.png</listitem>
</itemizedlist></listitem>
<itemizedlist>
<listitem>ARCHITECTURE_A <itemizedlist>
<listitem>NAME.ofx</listitem>
</itemizedlist></listitem>
</itemizedlist>
<itemizedlist>
<listitem>ARCHITECTURE_B <itemizedlist>
<listitem>NAME.ofx</listitem>
</itemizedlist></listitem>
</itemizedlist>
<listitem>....</listitem>
<listitem>ARCHITECTURE_N <itemizedlist>
<listitem>NAME.ofx</listitem>
</itemizedlist></listitem>
</itemizedlist></listitem>
</itemizedlist>
</listitem>
</itemizedlist> Where... <itemizedlist>
<listitem>
Info.plist is relevant for OSX only and needs to be filled in appropriately,
</listitem>
<listitem>
NAME is the file name you want the installed plug-in to be identified by,
</listitem>
<listitem>
PLUGIN.png - is the image to use as an icon for the plug-in in the binary which has a matching
<structfield>pluginIdentifier</structfield>
field in the
<structname>OfxPlugin</structname>
struct,
</listitem>
<listitem>
ARCHITECTURE is the specific operating system architecture the plug-in was built for, these are currently...
<itemizedlist>
<listitem>MacOS - for Apple Macintosh OS X (compiled 32
bit)</listitem>
<listitem>Win32 - for Microsoft Windows (compiled 32
bit)</listitem>
<listitem>IRIX - for SGI IRIX plug-ins (compiled 32
bit)</listitem>
<listitem>IRIX64 - for SGI IRIX plug-ins (compiled 64
bit)</listitem>
<listitem>Linux-x86 - for Linux on intel x86 CPUs (compiled 32
bit)</listitem>
<listitem>Linux-x86-64 - for Linux on intel x86 CPUs running
AMD's 64 bit extensions</listitem>
</itemizedlist>
</listitem>
</itemizedlist> Note that not all the above architectures need be
supported, only at least one.</para>
<para>This structure is necessary on OS X, but it also gives a nice
skeleton to hang all other operating systems from in a single install,
as well as a clean place to put resources.</para>
<para>The <filename>Info.plist</filename> is specific to Apple and you
should consult the Apple developer's website for more details. It
should contain the following keys... <itemizedlist>
<listitem>
CFBundleExecutable - the name of the binary bundle in the MacOS directory
</listitem>
<listitem>
CFBundlePackageType - to be 'BNDL'
</listitem>