1 /*
2  * opalplugins.h
3  *
4  * OPAL codec plugins handler
5  *
6  * Open Phone Abstraction Library (OPAL)
7  * Formally known as the Open H323 project.
8  *
9  * Copyright (C) 2004-2011 Post Increment
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  *   notice, this list of conditions and the following disclaimer.
17 
18  * - Redistributions in binary form must reproduce the above copyright
19  *   notice, this list of conditions and the following disclaimer in the
20  *   documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
26  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * $Revision: 28048 $
35  * $Author: rjongbloed $
36  * $Date: 2012-07-17 22:41:57 -0500 (Tue, 17 Jul 2012) $
37  */
38 
39 #ifndef OPAL_CODEC_OPALPLUGIN_H
40 #define OPAL_CODEC_OPALPLUGIN_H
41 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif
45 
46 #ifndef _CRT_NONSTDC_NO_DEPRECATE
47 #define _CRT_NONSTDC_NO_DEPRECATE 1
48 #endif
49 
50 #ifndef _CRT_SECURE_NO_WARNINGS
51 #define _CRT_SECURE_NO_WARNINGS 1
52 #endif
53 
54 #include <time.h>
55 
56 #ifdef _MSC_VER
57 #pragma warning(disable:4201)
58 #endif
59 
60 #ifdef _WIN32
61 #  ifdef PLUGIN_CODEC_DLL_EXPORTS
62 #    define PLUGIN_CODEC_DLL_API __declspec(dllexport)
63 #  else
64 #    define PLUGIN_CODEC_DLL_API __declspec(dllimport)
65 #  endif
66 
67 #if !defined(strcasecmp) && !defined(_WIN32_WCE)
68 #define strcasecmp stricmp
69 #endif
70 
71 #else
72 
73 #define PLUGIN_CODEC_DLL_API
74 
75 #endif
76 
77 #ifdef PWLIB_PLUGIN_API_VERSION
78 #undef PWLIB_PLUGIN_API_VERSION
79 #endif
80 #define PWLIB_PLUGIN_API_VERSION 1
81 
82 #define  PLUGIN_CODEC_VERSION_FIRST     1    // initial version
83 #define  PLUGIN_CODEC_VERSION_WIDEBAND  2    // added wideband
84 #define  PLUGIN_CODEC_VERSION_VIDEO     3    // added video
85 #define  PLUGIN_CODEC_VERSION_FAX       4    // added fax
86 #define  PLUGIN_CODEC_VERSION_OPTIONS   5    // added options handling
87 #define  PLUGIN_CODEC_VERSION_INTERSECT 6    // added media option intersection merge functionality
88 #define  PLUGIN_CODEC_VERSION_H245_DEF_GEN_PARAM 7 // added suppression of H.245 generic parameters via default
89 
90 #define  PLUGIN_CODEC_VERSION PLUGIN_CODEC_VERSION_INTERSECT // Always latest version
91 
92 #define PLUGIN_CODEC_API_VER_FN       PWLibPlugin_GetAPIVersion
93 #define PLUGIN_CODEC_API_VER_FN_STR   "PWLibPlugin_GetAPIVersion"
94 
95 #define PLUGIN_CODEC_GET_CODEC_FN     OpalCodecPlugin_GetCodecs
96 #define PLUGIN_CODEC_GET_CODEC_FN_STR "OpalCodecPlugin_GetCodecs"
97 
98 #define PLUGIN_CODEC_API_VER_FN_DECLARE \
99 PLUGIN_CODEC_DLL_API unsigned int PLUGIN_CODEC_API_VER_FN() \
100 { return PWLIB_PLUGIN_API_VERSION; }
101 
102 enum {
103   PluginCodec_License_None                           = 0,
104   PluginCodec_Licence_None = PluginCodec_License_None,        // allow for old code with misspelled constant
105   PluginCodec_License_GPL                            = 1,
106   PluginCodec_License_MPL                            = 2,
107   PluginCodec_License_Freeware                       = 3,
108   PluginCodec_License_ResearchAndDevelopmentUseOnly  = 4,
109   PluginCodec_License_BSD                            = 5,
110   PluginCodec_License_LGPL                           = 6,
111 
112   PluginCodec_License_NoRoyalties                    = 0x7f,
113 
114   // any license codes above here require royalty payments
115   PluginCodec_License_RoyaltiesRequired              = 0x80
116 };
117 
118 struct PluginCodec_information {
119   // start of version 1 fields
120   time_t timestamp;                     // codec creation time and date - obtain with command: date -u "+%c = %s"
121 
122   const char * sourceAuthor;            // source code author
123   const char * sourceVersion;           // source code version
124   const char * sourceEmail;             // source code email contact information
125   const char * sourceURL;               // source code web site
126   const char * sourceCopyright;         // source code copyright
127   const char * sourceLicense;           // source code license
128   unsigned char sourceLicenseCode;      // source code license
129 
130   const char * codecDescription;        // codec description
131   const char * codecAuthor;             // codec author
132   const char * codecVersion;            // codec version
133   const char * codecEmail;              // codec email contact information
134   const char * codecURL;                // codec web site
135   const char * codecCopyright;          // codec copyright information
136   const char * codecLicense;            // codec license
137   unsigned short codecLicenseCode;      // codec license code
138   // end of version 1 fields
139 
140 };
141 
142 enum PluginCodec_Flags {
143   PluginCodec_MediaTypeMask          = 0x000f,
144   PluginCodec_MediaTypeAudio         = 0x0000,
145   PluginCodec_MediaTypeVideo         = 0x0001,
146   PluginCodec_MediaTypeAudioStreamed = 0x0002,
147   PluginCodec_MediaTypeFax           = 0x0003,
148 
149   PluginCodec_InputTypeMask          = 0x0010,
150   PluginCodec_InputTypeRaw           = 0x0000, // Note video is always RTP
151   PluginCodec_InputTypeRTP           = 0x0010,
152 
153   PluginCodec_OutputTypeMask         = 0x0020,
154   PluginCodec_OutputTypeRaw          = 0x0000, // Note video is always RTP
155   PluginCodec_OutputTypeRTP          = 0x0020,
156 
157   PluginCodec_RTPTypeMask            = 0x0040,
158   PluginCodec_RTPTypeDynamic         = 0x0000,
159   PluginCodec_RTPTypeExplicit        = 0x0040,
160 
161   PluginCodec_RTPSharedMask          = 0x0080,
162   PluginCodec_RTPTypeNotShared       = 0x0000,
163   PluginCodec_RTPTypeShared          = 0x0080,
164 
165   PluginCodec_DecodeSilenceMask      = 0x0100,
166   PluginCodec_NoDecodeSilence        = 0x0000,
167   PluginCodec_DecodeSilence          = 0x0100,
168 
169   PluginCodec_EncodeSilenceMask      = 0x0200,
170   PluginCodec_NoEncodeSilence        = 0x0000,
171   PluginCodec_EncodeSilence          = 0x0200,
172 
173   PluginCodec_MediaExtensionMask     = 0x0400,
174   PluginCodec_MediaTypeExtVideo      = 0x0400,
175 
176   PluginCodec_ComfortNoiseMask       = 0x0800,
177   PluginCodec_ComfortNoise           = 0x0800,
178 
179   PluginCodec_EmptyPayloadMask       = 0x1000,
180   PluginCodec_EmptyPayload           = 0x1000,
181 
182   PluginCodec_OtherPayloadMask       = 0x2000,
183   PluginCodec_OtherPayload           = 0x2000,
184 
185   PluginCodec_BitsPerSamplePos       = 12,
186   PluginCodec_BitsPerSampleMask      = 0xf000,
187 
188   PluginCodec_ChannelsPos            = 16,
189   PluginCodec_ChannelsMask           = 0x003f0000
190 };
191 
192 #define PluginCodec_SetChannels(n) (((n-1)<<PluginCodec_ChannelsPos)&PluginCodec_ChannelsMask)
193 
194 
195 enum PluginCodec_CoderFlags {
196   PluginCodec_CoderSilenceFrame      = 1,    // request audio codec to create silence frame
197   PluginCodec_CoderForceIFrame       = 2     // request video codec to force I frame
198 };
199 
200 enum PluginCodec_ReturnCoderFlags {
201   PluginCodec_ReturnCoderLastFrame      = 1,    // indicates when video codec returns last data for frame
202   PluginCodec_ReturnCoderIFrame         = 2,    // indicates when video returns I frame
203   PluginCodec_ReturnCoderRequestIFrame  = 4,    // indicates when video decoder request I frame for resync
204   PluginCodec_ReturnCoderBufferTooSmall = 8     // indicates when output buffer is not large enough to receive
205                                                 // the data, another call to get_output_data_size is required
206 };
207 
208 struct PluginCodec_Definition;
209 
210 // Control function names
211 
212 #define PLUGINCODEC_CONTROL_VALID_FOR_PROTOCOL    "valid_for_protocol"
213 #define PLUGINCODEC_CONTROL_GET_CODEC_OPTIONS     "get_codec_options"
214 #define PLUGINCODEC_CONTROL_FREE_CODEC_OPTIONS    "free_codec_options"
215 #define PLUGINCODEC_CONTROL_GET_OUTPUT_DATA_SIZE  "get_output_data_size"
216 #define PLUGINCODEC_CONTROL_SET_CODEC_OPTIONS     "set_codec_options"
217 #define PLUGINCODEC_CONTROL_TO_NORMALISED_OPTIONS "to_normalised_options"
218 #define PLUGINCODEC_CONTROL_TO_CUSTOMISED_OPTIONS "to_customised_options"
219 #define PLUGINCODEC_CONTROL_SET_INSTANCE_ID       "set_instance_id"
220 #define PLUGINCODEC_CONTROL_SET_LOG_FUNCTION      "set_log_function"
221 #define PLUGINCODEC_CONTROL_GET_STATISTICS        "get_statistics"
222 #define PLUGINCODEC_CONTROL_TERMINATE_CODEC       "terminate_codec"
223 
224 
225 /* Log function, plug in gets a pointer to this function which allows
226    it to use the standard OPAL logging system. The function returns 0 if
227    no logging was performed due to the log level. Note if log == NULL
228    then this return state is all that happens, so this may be executed
229    first to prevent lengthy logs that would not result in any output. */
230 typedef int (*PluginCodec_LogFunction)(unsigned level,
231                                        const char * file,
232                                        unsigned line,
233                                        const char * section,
234                                        const char * log);
235 
236 
237 struct PluginCodec_ControlDefn {
238   const char * name;
239   int (*control)(const struct PluginCodec_Definition * codec, void * context,
240                  const char * name, void * parm, unsigned * parmLen);
241 
242 };
243 
244 enum PluginCodec_OptionTypes {
245   PluginCodec_StringOption,
246   PluginCodec_BoolOption,
247   PluginCodec_IntegerOption,
248   PluginCodec_RealOption,
249   PluginCodec_EnumOption,
250   PluginCodec_OctetsOption,
251   PluginCodec_NumOptionTypes,
252 };
253 
254 enum PluginCodec_OptionMerge {
255   PluginCodec_NoMerge,
256   PluginCodec_MinMerge,
257   PluginCodec_MaxMerge,
258   PluginCodec_EqualMerge,
259   PluginCodec_NotEqualMerge,
260   PluginCodec_AlwaysMerge,
261   PluginCodec_CustomMerge,
262   PluginCodec_IntersectionMerge,
263 
264   PluginCodec_AndMerge = PluginCodec_MinMerge,
265   PluginCodec_OrMerge  = PluginCodec_MaxMerge
266 };
267 
268 #define PluginCodec_H245_Collapsing    0x40000000
269 #define PluginCodec_H245_NonCollapsing 0x20000000
270 #define PluginCodec_H245_Unsigned32    0x10000000
271 #define PluginCodec_H245_BooleanArray  0x08000000
272 #define PluginCodec_H245_TCS           0x04000000
273 #define PluginCodec_H245_OLC           0x02000000
274 #define PluginCodec_H245_ReqMode       0x01000000
275 #define PluginCodec_H245_OrdinalMask   0x0000ffff
276 #define PluginCodec_H245_PositionMask  0x00ff0000
277 #define PluginCodec_H245_PositionShift 16
278 
279 typedef int (*PluginCodec_MergeFunction)(char ** result, const char * dest, const char * src);
280 typedef void (*PluginCodec_FreeFunction)(char * string);
281 
282 struct PluginCodec_Option {
283   // start of version 4 fields
284   enum PluginCodec_OptionTypes m_type;
285   const char *                 m_name;
286   unsigned                     m_readOnly;
287   enum PluginCodec_OptionMerge m_merge;
288   const char *                 m_value;
289   const char *                 m_FMTPName;
290   const char *                 m_FMTPDefault;
291   int                          m_H245Generic;
292   const char *                 m_minimum;
293   const char *                 m_maximum;
294   PluginCodec_MergeFunction    m_mergeFunction; // Used if m_merge==PluginCodec_CustomMerge
295   PluginCodec_FreeFunction     m_freeFunction;
296   const char *                 m_H245Default;
297 };
298 
299 
300 // Normalised option names
301 #define PLUGINCODEC_OPTION_NEEDS_JITTER               "Needs Jitter"
302 #define PLUGINCODEC_OPTION_CLOCK_RATE                 "Clock Rate"
303 #define PLUGINCODEC_OPTION_CHANNELS                   "Channels"
304 #define PLUGINCODEC_OPTION_FRAME_TIME                 "Frame Time"
305 #define PLUGINCODEC_OPTION_MAX_FRAME_SIZE             "Max Frame Size"
306 #define PLUGINCODEC_OPTION_MAX_TX_PACKET_SIZE         "Max Tx Packet Size"   /* Really max PAYLOAD size */
307 #define PLUGINCODEC_OPTION_MAX_BIT_RATE               "Max Bit Rate"
308 #define PLUGINCODEC_OPTION_TARGET_BIT_RATE            "Target Bit Rate"
309 #define PLUGINCODEC_OPTION_RATE_CONTROL_PERIOD        "Rate Control Period"
310 #define PLUGINCODEC_OPTION_RX_FRAMES_PER_PACKET       "Rx Frames Per Packet"
311 #define PLUGINCODEC_OPTION_TX_FRAMES_PER_PACKET       "Tx Frames Per Packet"
312 #define PLUGINCODEC_OPTION_FRAME_WIDTH                "Frame Width"
313 #define PLUGINCODEC_OPTION_FRAME_HEIGHT               "Frame Height"
314 #define PLUGINCODEC_OPTION_MIN_RX_FRAME_WIDTH         "Min Rx Frame Width"
315 #define PLUGINCODEC_OPTION_MIN_RX_FRAME_HEIGHT        "Min Rx Frame Height"
316 #define PLUGINCODEC_OPTION_MAX_RX_FRAME_WIDTH         "Max Rx Frame Width"
317 #define PLUGINCODEC_OPTION_MAX_RX_FRAME_HEIGHT        "Max Rx Frame Height"
318 #define PLUGINCODEC_OPTION_TEMPORAL_SPATIAL_TRADE_OFF "Temporal Spatial Trade Off"
319 #define PLUGINCODEC_OPTION_TX_KEY_FRAME_PERIOD        "Tx Key Frame Period"
320 
321 #define PLUGINCODEC_OPTION_PROTOCOL      "Protocol"
322 #define PLUGINCODEC_OPTION_PROTOCOL_H323 "H.323"
323 #define PLUGINCODEC_OPTION_PROTOCOL_SIP  "SIP"
324 
325 
326 // Full definition of the codec
327 
328 struct PluginCodec_Definition {
329   unsigned int version;                     // codec structure version
330 
331   // start of version 1 fields
332   struct PluginCodec_information * info;   // license information
333 
334   unsigned int flags;                      // b0-3: 0 = audio,        1 = video
335                                            // b4:   0 = raw input,    1 = RTP input
336                                            // b5:   0 = raw output,   1 = RTP output
337                                            // b6:   0 = dynamic RTP,  1 = explicit RTP
338                                            // b7:   0 = no share RTP, 1 = share RTP
339 
340   const char * descr;                       // text decription
341 
342   const char * sourceFormat;               // source format
343   const char * destFormat;                 // destination format
344 
345   const void * userData;                   // user data value
346 
347   unsigned int sampleRate;                 // samples per second
348   unsigned int bitsPerSec;                // raw bits per second
349   unsigned int usPerFrame;                 // microseconds per frame
350 
351   union _parm {
352     struct _audio {
353       unsigned int samplesPerFrame;            // audio: samples per frame
354       unsigned int bytesPerFrame;              // audio: max bytes per frame
355       unsigned int recommendedFramesPerPacket; // audio: recommended number of frames per packet
356       unsigned int maxFramesPerPacket;         // audio: maximum number of frames per packet
357     } audio;
358     struct _video {
359       unsigned int maxFrameWidth;              // video: frame width
360       unsigned int maxFrameHeight;             // video: frame height
361       unsigned int recommendedFrameRate;       // video: recommended frame rate
362       unsigned int maxFrameRate;               // video: max frame rate
363     } video;
364   } parm;
365 
366   unsigned char rtpPayload;                 // IANA RTP payload code (if defined)
367   const char * sdpFormat;                  // SDP format string (or NULL, if no SDP format)
368 
369   void * (*createCodec)(const struct PluginCodec_Definition * codec);                    // create codec
370   void (*destroyCodec) (const struct PluginCodec_Definition * codec,  void * context);   // destroy codec
371   int (*codecFunction) (const struct PluginCodec_Definition * codec,  void * context,   // do codec function
372                                   const void * from, unsigned * fromLen,
373                                         void * to,   unsigned * toLen,
374                                         unsigned int * flag);
375   struct PluginCodec_ControlDefn * codecControls;
376 
377   // H323 specific fields
378   unsigned char h323CapabilityType;
379   const void  * h323CapabilityData;
380 
381   // end of version 1 fields
382 
383   // NOTE!!!!! Due to an error in judgement, you cannot add ANY more fields
384   // to this structure without an API version change!!!!
385 };
386 
387 typedef struct PluginCodec_Definition * (* PluginCodec_GetCodecFunction)(unsigned int *, unsigned int);
388 typedef unsigned (* PluginCodec_GetAPIVersionFunction)();
389 
390 
391 ///////////////////////////////////////////////////////////////////
392 
393 #define PLUGINCODEC_RAW_AUDIO "L16"
394 #define PLUGINCODEC_RAW_VIDEO "YUV420P"
395 
396 /// Declare a pair of plug in definition entries for a codec
397 #define PLUGINCODEC_CODEC_PAIR(MediaFormat,    /**< Media Format */ \
398                                PayloadName,    /**< IANA RTP payload code */ \
399                                Description,    /**< Description text */ \
400                                SampleRate,     /**< Sample rate */ \
401                                BitsPerSecond,  /**< Maximum bits per second */ \
402                                FrameTime,      /**< Microseconds per frame */ \
403                                p1,p2,p3,p4, \
404                                PayloadType,    /**< IANA RTP payload type code */ \
405                                H323type,       /**< h323CapabilityType enumeration */ \
406                                H323data,       /**< Data to go with h323CapabilityType */ \
407                                CreateEncoder,  /**< Create encoder function */ \
408                                DestroyEncoder, /**< Destroy encoder function */ \
409                                EncodeMedia,    /**< Encode media function */ \
410                                CreateDecoder,  /**< Create decoder function */ \
411                                DestroyDecoder, /**< Destroy decoder function */ \
412                                DecodeMedia,    /**< Decode media function */ \
413                                ControlsTable,  /**< Codec controls tables */ \
414                                Flags,          /**< Flags */ \
415                                RawFormat       /**< Raw format */ \
416                                ) \
417   { \
418     PLUGIN_CODEC_VERSION, &MyLicenseInfo, Flags, Description, RawFormat, MediaFormat, NULL, \
419     SampleRate, BitsPerSecond, FrameTime, {{ p1,p2,p3,p4 }}, PayloadType, PayloadName, \
420     CreateEncoder, DestroyEncoder, EncodeMedia, ControlsTable, H323type, H323data \
421   }, \
422   { \
423     PLUGIN_CODEC_VERSION, &MyLicenseInfo, Flags, Description, MediaFormat, RawFormat, NULL, \
424     SampleRate, BitsPerSecond, FrameTime, {{ p1,p2,p3,p4 }}, PayloadType, PayloadName, \
425     CreateDecoder, DestroyDecoder, DecodeMedia, ControlsTable, H323type, H323data \
426   }
427 
428 #define PLUGINCODEC_AUDIO_CODEC(MediaFormat,     /**< Media Format */ \
429                                 PayloadName,     /**< IANA RTP payload code */ \
430                                 Description,     /**< Description text */ \
431                                 SampleRate,      /**< Sample rate */ \
432                                 BitsPerSecond,   /**< Maximum bits per second */ \
433                                 FrameTime,       /**< Microseconds per audio frame */ \
434                                 SamplesPerFrame, /**< Samples per audio frame */ \
435                                 BytesPerFrame,   /**< Samples per audio frame */ \
436                                 RecFramesPerPacket, /**< Recommended frames per packet */ \
437                                 MaxFramesPerPacket, /**< Maximum frames per packet */ \
438                                 RtpFlags,         /**< Extra flags typically if RTP payload type is fixed */ \
439                                 PayloadType,      /**< IANA RTP payload type code */ \
440                                 H323type,         /**< h323CapabilityType enumeration */ \
441                                 H323data,         /**< Data to go with h323CapabilityType */ \
442                                 CreateEncoder,    /**< Create encoder function */ \
443                                 DestroyEncoder,   /**< Destroy encoder function */ \
444                                 EncodeAudio,      /**< Encode media function */ \
445                                 CreateDecoder,    /**< Create decoder function */ \
446                                 DestroyDecoder,   /**< Destroy decoder function */ \
447                                 DecodeAudio,      /**< Decode media function */ \
448                                 ControlsTable     /**< Codec controls tables */ \
449                                 ) \
450          PLUGINCODEC_CODEC_PAIR(MediaFormat, \
451                                 PayloadName, \
452                                 Description, \
453                                 SampleRate, \
454                                 BitsPerSecond, \
455                                 FrameTime, \
456                                 SamplesPerFrame, \
457                                 BytesPerFrame, \
458                                 RecFramesPerPacket, \
459                                 MaxFramesPerPacket, \
460                                 PayloadType, \
461                                 H323type, \
462                                 H323data, \
463                                 CreateEncoder, \
464                                 DestroyEncoder, \
465                                 EncodeAudio, \
466                                 CreateDecoder, \
467                                 DestroyDecoder, \
468                                 DecodeAudio, \
469                                 ControlsTable, \
470                                 PluginCodec_MediaTypeAudio | /* audio codec */ \
471                                 PluginCodec_InputTypeRaw |   /* raw input data */ \
472                                 PluginCodec_OutputTypeRaw |  /* raw output data */ \
473                                 (RtpFlags), \
474                                 PLUGINCODEC_RAW_AUDIO)
475 
476 #define PLUGINCODEC_ONE_AUDIO_CODEC(MediaFormat,     /**< Media Format */ \
477                                     PayloadName,     /**< IANA RTP payload code */ \
478                                     Description,     /**< Description text */ \
479                                     SampleRate,      /**< Sample rate */ \
480                                     BitsPerSecond,   /**< Maximum bits per second */ \
481                                     FrameTime,       /**< Microseconds per audio frame */ \
482                                     SamplesPerFrame, /**< Samples per audio frame */ \
483                                     BytesPerFrame,   /**< Samples per audio frame */ \
484                                     RecFramesPerPacket, /**< Recommended frames per packet */ \
485                                     MaxFramesPerPacket, /**< Maximum frames per packet */ \
486                                     RtpFlags,         /**< Extra flags typically if RTP payload type is fixed */ \
487                                     PayloadType,      /**< IANA RTP payload type code */ \
488                                     H323type,         /**< h323CapabilityType enumeration */ \
489                                     H323data          /**< Data to go with h323CapabilityType */ \
490                                 ) \
491     static struct PluginCodec_Definition CodecDefinitionTable[] = { \
492             PLUGINCODEC_AUDIO_CODEC(MediaFormat, \
493                                     PayloadName, \
494                                     Description, \
495                                     SampleRate, \
496                                     BitsPerSecond, \
497                                     FrameTime, \
498                                     SamplesPerFrame, \
499                                     BytesPerFrame, \
500                                     RecFramesPerPacket, \
501                                     MaxFramesPerPacket, \
502                                     RtpFlags, \
503                                     PayloadType, \
504                                     H323type, \
505                                     H323data, \
506                                     MyCreateEncoder, \
507                                     MyDestroyEncoder, \
508                                     MyEncodeAudio, \
509                                     MyCreateDecoder, \
510                                     MyDestroyDecoder, \
511                                     MyDecodeAudio, \
512                                     MyControlsTable \
513                                     ) \
514     }
515 
516 #define PLUGINCODEC_VIDEO_CODEC(MediaFormat,     /**< Media Format */ \
517                                 PayloadName,     /**< IANA RTP payload code */ \
518                                 Description,     /**< Description text */ \
519                                 BitsPerSecond,   /**< Maximum bits per second */ \
520                                 MaxWidth,        /**< Max resolution (width) */ \
521                                 MaxHeight,       /**< Max resolution (height) */ \
522                                 RtpFlags,        /**< Extra flags typically if RTP payload type is fixed */ \
523                                 PayloadType,     /**< IANA RTP payload type code */ \
524                                 H323type,        /**< h323CapabilityType enumeration */ \
525                                 H323data,        /**< Data to go with h323CapabilityType */ \
526                                 CreateEncoder,   /**< Create encoder function */ \
527                                 DestroyEncoder,  /**< Destroy encoder function */ \
528                                 EncodeVideo,     /**< Encode media function */ \
529                                 CreateDecoder,   /**< Create decoder function */ \
530                                 DestroyDecoder,  /**< Destroy decoder function */ \
531                                 DecodeVideo,     /**< Decode media function */ \
532                                 ControlsTable    /**< Codec controls tables */ \
533                                 ) \
534          PLUGINCODEC_CODEC_PAIR(MediaFormat, \
535                                 PayloadName, \
536                                 Description, \
537                                 SampleRate, \
538                                 BitsPerSecond, \
539                                 90000, \
540                                 BitsPerSecond, \
541                                 100000, \
542                                 MaxWidth, \
543                                 MaxHeight, \
544                                 0,30, \
545                                 PayloadType, \
546                                 H323type, \
547                                 H323data, \
548                                 CreateEncoder, \
549                                 DestroyEncoder, \
550                                 EncodeVideo, \
551                                 CreateDecoder, \
552                                 DestroyDecoder, \
553                                 DecodeVideo, \
554                                 ControlsTable, \
555                                 PluginCodec_MediaTypeVideo | /* video codec */ \
556                                 PluginCodec_InputTypeRRP |   /* RTP input data */ \
557                                 PluginCodec_OutputTypeRRP |  /* RTP output data */ \
558                                 (RtpFlags), \
559                                 PLUGINCODEC_RAW_VIDEO)
560 
561 #define PLUGINCODEC_ONE_VIDEO_CODEC(MediaFormat,     /**< Media Format */ \
562                                     PayloadName,     /**< IANA RTP payload code */ \
563                                     Description,     /**< Description text */ \
564                                     BitsPerSecond,   /**< Maximum bits per second */ \
565                                     MaxWidth,        /**< Max resolution (width) */ \
566                                     MaxHeight,       /**< Max resolution (height) */ \
567                                     RtpFlags,        /**< Extra flags typically if RTP payload type is fixed */ \
568                                     PayloadType,     /**< IANA RTP payload type code */ \
569                                     H323type,        /**< h323CapabilityType enumeration */ \
570                                     H323data         /**< Data to go with h323CapabilityType */ \
571                                 ) \
572     static struct PluginCodec_Definition CodecDefinitionTable[] = { \
573             PLUGINCODEC_VIDEO_CODEC(MediaFormat, \
574                                     PayloadName, \
575                                     Description, \
576                                     BitsPerSecond, \
577                                     MaxWidth, \
578                                     MaxHeight, \
579                                     RtpFlags, \
580                                     PayloadType, \
581                                     H323type, \
582                                     H323data, \
583                                     CreateEncoder, \
584                                     DestroyEncoder, \
585                                     EncodeAudio, \
586                                     CreateDecoder, \
587                                     DestroyDecoder, \
588                                     DecodeAudio, \
589                                     ControlsTable \
590                                     ) \
591     }
592 
593 
594 //////////////////////////////////////////////////////////////////
595 //
596 //  H.323 specific values
597 //
598 
599 
600 struct PluginCodec_H323CapabilityExtension {
601   unsigned int index;
602   void * data;
603   unsigned dataLength;
604 };
605 
606 struct PluginCodec_H323NonStandardCodecData {
607   const char * objectId;
608   unsigned char  t35CountryCode;
609   unsigned char  t35Extension;
610   unsigned short manufacturerCode;
611   const unsigned char * data;
612   unsigned int dataLength;
613   int (*capabilityMatchFunction)(struct PluginCodec_H323NonStandardCodecData *);
614 };
615 
616 
617 struct PluginCodec_H323GenericParameterDefinition
618 {
619   /* The following used to be a simple integer for the collapsing flag in
620      version 3 and earlier. We hope that all those implementations just used
621      zero and one (a good bet) and thus the below bit fields will be backward
622      compatible, putting the parameter in all three PDU types.
623    */
624 #ifndef SOLARIS
625   struct {
626 #endif
627     int collapsing:1; /* boolean */
628     int excludeTCS:1;
629     int excludeOLC:1;
630     int excludeReqMode:1;
631     int readOnly:1;
632 #ifndef SOLARIS
633   };
634 #endif
635 
636   unsigned int id;
637 
638   enum PluginCodec_H323GenericParameterType {
639     /* these need to be in the same order as the choices in
640       H245_ParameterValue::Choices, as the value is just cast to that type
641     */
642     PluginCodec_GenericParameter_Logical = 0,
643     PluginCodec_GenericParameter_BooleanArray,
644     PluginCodec_GenericParameter_UnsignedMin,
645     PluginCodec_GenericParameter_UnsignedMax,
646     PluginCodec_GenericParameter_Unsigned32Min,
647     PluginCodec_GenericParameter_Unsigned32Max,
648     PluginCodec_GenericParameter_OctetString,
649     PluginCodec_GenericParameter_GenericParameter,
650 
651     PluginCodec_GenericParameter_logical = 0,
652     PluginCodec_GenericParameter_booleanArray,
653     PluginCodec_GenericParameter_unsignedMin,
654     PluginCodec_GenericParameter_unsignedMax,
655     PluginCodec_GenericParameter_unsigned32Min,
656     PluginCodec_GenericParameter_unsigned32Max,
657     PluginCodec_GenericParameter_octetString,
658     PluginCodec_GenericParameter_genericParameter
659   } type;
660 
661   union {
662     unsigned long integer;
663     const char * octetstring;
664     struct PluginCodec_H323GenericParameterDefinition *genericparameter;
665   } value;
666 };
667 
668 struct PluginCodec_H323GenericCodecData
669 {
670   // some cunning structures & lists, and associated logic in
671   // H323CodecPluginGenericAudioCapability::H323CodecPluginGenericAudioCapability()
672   const char * standardIdentifier;
673   unsigned int maxBitRate; // Zero means use value from OpalMediaFormat
674 
675   /* parameters; these are the parameters which are set in the
676      'TerminalCapabilitySet' and 'OpenLogicalChannel' requests */
677   unsigned int nParameters;
678   /* an array of nParameters parameter definitions */
679   const struct PluginCodec_H323GenericParameterDefinition *params;
680 };
681 
682 
683 struct PluginCodec_H323AudioGSMData {
684   int comfortNoise:1;
685   int scrambled:1;
686 };
687 
688 struct  PluginCodec_H323AudioG7231AnnexC {
689   unsigned char maxAl_sduAudioFrames;
690   int silenceSuppression:1;
691   int highRateMode0:6;          // INTEGER (27..78),  -- units octets
692   int  highRateMode1:6;          // INTEGER (27..78),  -- units octets
693   int  lowRateMode0:6;            // INTEGER (23..66),  -- units octets
694   int  lowRateMode1:6;            // INTEGER (23..66),  -- units octets
695   int  sidMode0:4;                // INTEGER (6..17),  -- units octets
696   int  sidMode1:4;                // INTEGER (6..17),  -- units octets
697 };
698 
699 
700 enum {
701   PluginCodec_H323Codec_undefined,      // must be zero, so empty struct is undefined
702   PluginCodec_H323Codec_programmed,      // H323ProgrammedCapability
703   PluginCodec_H323Codec_nonStandard,    // H323NonStandardData
704   PluginCodec_H323Codec_generic,            // H323GenericCodecData
705 
706   // audio codecs
707   PluginCodec_H323AudioCodec_g711Alaw_64k,        // int
708   PluginCodec_H323AudioCodec_g711Alaw_56k,        // int
709   PluginCodec_H323AudioCodec_g711Ulaw_64k,        // int
710   PluginCodec_H323AudioCodec_g711Ulaw_56k,        // int
711   PluginCodec_H323AudioCodec_g722_64k,            // int
712   PluginCodec_H323AudioCodec_g722_56k,            // int
713   PluginCodec_H323AudioCodec_g722_48k,            // int
714   PluginCodec_H323AudioCodec_g7231,                // H323AudioG7231Data
715   PluginCodec_H323AudioCodec_g728,                // int
716   PluginCodec_H323AudioCodec_g729,                // int
717   PluginCodec_H323AudioCodec_g729AnnexA,          // int
718   PluginCodec_H323AudioCodec_is11172,             // not yet implemented
719   PluginCodec_H323AudioCodec_is13818Audio,        // not yet implemented
720   PluginCodec_H323AudioCodec_g729wAnnexB,          // int
721   PluginCodec_H323AudioCodec_g729AnnexAwAnnexB,    // int
722   PluginCodec_H323AudioCodec_g7231AnnexC,         // H323AudioG7231AnnexC
723   PluginCodec_H323AudioCodec_gsmFullRate,          // H323AudioGSMData
724   PluginCodec_H323AudioCodec_gsmHalfRate,          // H323AudioGSMData
725   PluginCodec_H323AudioCodec_gsmEnhancedFullRate,  // H323AudioGSMData
726   PluginCodec_H323AudioCodec_g729Extensions,      // not yet implemented
727 
728   // video codecs
729   PluginCodec_H323VideoCodec_h261,                // implemented
730   PluginCodec_H323VideoCodec_h262,                // not yet implemented
731   PluginCodec_H323VideoCodec_h263,                // implemented
732   PluginCodec_H323VideoCodec_is11172,             // not yet implemented
733 
734   // other capabilities
735   PluginCodec_H323VideoCodec_Extended,            // implemented (for use with H.239)
736   PluginCodec_H323T38Codec,                       // not yet implemented
737 
738   // special codes
739   PluginCodec_H323Codec_NoH323 = 0xff,            // used for SIP-only codecs
740 };
741 
742 /////////////////
743 //
744 // Generic Codec Standard Identifiers
745 //
746 
747 // Audio Capabilities
748 // AMR (as defined in H.245v13 Annex I)
749 #define OpalPluginCodec_Identifer_AMR             "0.0.8.245.1.1.1"
750 
751 // AMR-NB\WB  (as defined in H.245v13 Annex R)
752 #define OpalPluginCodec_Identifer_AMR_NB          "0.0.8.245.1.1.9"
753 #define OpalPluginCodec_Identifer_AMR_WB          "0.0.8.245.1.1.10"
754 
755 // G.722.1
756 #define OpalPluginCodec_Identifer_G7221           "0.0.7.7221.1.0"
757 #define OpalPluginCodec_Identifer_G7221ext        "0.0.7.7221.1.1.0"
758 
759 // G.722.2 (aka AMR-WB)
760 #define OpalPluginCodec_Identifer_G7222           "0.0.7.7222.1.0"
761 
762 // iLBC (as defined in H.245v13 Annex S)
763 #define OpalPluginCodec_Identifer_iLBC            "0.0.8.245.1.1.11"
764 
765 
766 // Video Capabilities
767 
768 // H264 (as defined in H.241)
769 #define OpalPluginCodec_Identifer_H264_Aligned        "0.0.8.241.0.0.0.0"
770 #define OpalPluginCodec_Identifer_H264_NonInterleaved "0.0.8.241.0.0.0.1"
771 #define OpalPluginCodec_Identifer_H264_Interleaved    "0.0.8.241.0.0.0.2"
772 #define OpalPluginCodec_Identifer_H264_Generic        "0.0.8.241.0.0.1"
773 
774 // ISO/IEC 14496-2 MPEG4 part 2 (as defined in H.245v13 Annex E)
775 #define OpalPluginCodec_Identifer_MPEG4           "0.0.8.245.1.0.0"
776 
777 
778 /////////////////
779 //
780 // Predefined options for H.323 codecs
781 //
782 
783 #define PLUGINCODEC_SQCIF_MPI   "SQCIF MPI"
784 #define PLUGINCODEC_QCIF_MPI     "QCIF MPI"
785 #define PLUGINCODEC_CIF_MPI       "CIF MPI"
786 #define PLUGINCODEC_CIF4_MPI     "CIF4 MPI"
787 #define PLUGINCODEC_CIF16_MPI   "CIF16 MPI"
788 #define PLUGINCODEC_CUSTOM_MPI "Custom MPI"
789 
790 #define PLUGINCODEC_MPI_DISABLED 33
791 
792 #define PLUGINCODEC_MEDIA_PACKETIZATION  "Media Packetization"
793 #define PLUGINCODEC_MEDIA_PACKETIZATIONS "Media Packetizations"
794 
795 #define H261_ANNEX_D "Annex D - Still Image Transmit"
796 #define H263_ANNEX_D "Annex D - Unrestricted Motion Vector"
797 #define H263_ANNEX_F "Annex F - Advanced Prediction"
798 #define H263_ANNEX_I "Annex I - Advanced INTRA Coding"
799 #define H263_ANNEX_J "Annex J - Deblocking Filter"
800 #define H263_ANNEX_K "Annex K - Slice Structure"
801 #define H263_ANNEX_N "Annex N - Reference Picture Selection"
802 #define H263_ANNEX_S "Annex S - Alternative INTER VLC"
803 #define H263_ANNEX_T "Annex T - Modified Quantization"
804 
805 #ifndef STRINGIZE
806 #define __INTERNAL_STRINGIZE__(v) #v
807 #define STRINGIZE(v) __INTERNAL_STRINGIZE__(v)
808 #endif
809 
810 
811 /////////////////
812 //
813 // RTP specific definitions
814 //
815 
816 #define PluginCodec_RTP_MaxPacketSize  (1518-14-4-8-20-16)  // Max Ethernet packet (1518 bytes) minus 802.3/CRC, 802.3, IP, UDP headers
817 #define PluginCodec_RTP_MinHeaderSize  (12)
818 #define PluginCodec_RTP_MaxPayloadSize (PluginCodec_RTP_MaxPacketSize - PluginCodec_RTP_MinHeaderSize)
819 
820 #define PluginCodec_RTP_GetWORD(ptr, off)       ((((unsigned char*)(ptr))[off] << 8) | ((unsigned char*)(ptr))[off+1])
821 
822 #define PluginCodec_RTP_GetDWORD(ptr, off)      ((((unsigned char*)(ptr))[off  ] << 24)|\
823                                                  (((unsigned char*)(ptr))[off+1] << 16)|\
824                                                  (((unsigned char*)(ptr))[off+2] << 8 )|\
825                                                   ((unsigned char*)(ptr))[off+3])
826 
827 #define PluginCodec_RTP_SetWORD(ptr, off, val)  ((((unsigned char*)(ptr))[off  ] = (unsigned char)((val) >> 8 )),\
828                                                  (((unsigned char*)(ptr))[off+1] = (unsigned char) (val)      ))
829 
830 #define PluginCodec_RTP_SetDWORD(ptr, off, val) ((((unsigned char*)(ptr))[off  ] = (unsigned char)((val) >> 24)),\
831                                                  (((unsigned char*)(ptr))[off+1] = (unsigned char)((val) >> 16)),\
832                                                  (((unsigned char*)(ptr))[off+2] = (unsigned char)((val) >> 8 )),\
833                                                  (((unsigned char*)(ptr))[off+3] = (unsigned char) (val)      ))
834 
835 #define PluginCodec_RTP_GetCSRCHdrLength(ptr)      ((((unsigned char*)(ptr))[0] & 0x0f)*4 + PluginCodec_RTP_MinHeaderSize)
836 #define PluginCodec_RTP_GetExtHdrLength(ptr)       ((((unsigned char*)(ptr))[0] & 0x10) ? (PluginCodec_RTP_GetWORD(ptr, PluginCodec_RTP_GetCSRCHdrLength(ptr)+2)*4+4) : 0)
837 
838 #define PluginCodec_RTP_GetHeaderLength(ptr)       (PluginCodec_RTP_GetCSRCHdrLength(ptr) + PluginCodec_RTP_GetExtHdrLength(ptr))
839 #define PluginCodec_RTP_GetPayloadPtr(ptr)          ((unsigned char*)(ptr) + PluginCodec_RTP_GetHeaderLength(ptr))
840 #define PluginCodec_RTP_GetPayloadType(ptr)        (((unsigned char*)(ptr))[1] & 0x7f)
841 #define PluginCodec_RTP_SetPayloadType(ptr, type)   (((unsigned char*)(ptr))[1] = (unsigned char)((((unsigned char*)(ptr))[1] & 0x80) | (type & 0x7f)))
842 #define PluginCodec_RTP_GetMarker(ptr)            ((((unsigned char*)(ptr))[1] & 0x80) != 0)
843 #define PluginCodec_RTP_SetMarker(ptr, mark)        (((unsigned char*)(ptr))[1] = (unsigned char)((((unsigned char*)(ptr))[1] & 0x7f) | (mark != 0 ? 0x80 : 0)))
844 #define PluginCodec_RTP_GetTimestamp(ptr)          PluginCodec_RTP_GetDWORD(ptr, 4)
845 #define PluginCodec_RTP_SetTimestamp(ptr, ts)      PluginCodec_RTP_SetDWORD(ptr, 4, ts)
846 #define PluginCodec_RTP_GetSequenceNumber(ptr)     PluginCodec_RTP_GetWORD(ptr, 2)
847 #define PluginCodec_RTP_SetSequenceNumber(ptr, sn) PluginCodec_RTP_SetWORD(ptr, 2, sn)
848 #define PluginCodec_RTP_GetSSRC(ptr)               PluginCodec_RTP_GetDWORD(ptr, 8)
849 #define PluginCodec_RTP_SetSSRC(ptr, ssrc)         PluginCodec_RTP_SetDWORD(ptr, 8, ssrc)
850 
851 #define PluginCodec_RTP_SetExtended(ptr, type, sz) ((((unsigned char*)(ptr))[0] |= 0x10), \
852                                                     PluginCodec_RTP_SetWORD(ptr, PluginCodec_RTP_GetCSRCHdrLength(ptr), type), \
853                                                     PluginCodec_RTP_SetWORD(ptr, PluginCodec_RTP_GetCSRCHdrLength(ptr)+2, sz))
854 
855 
856 /////////////////
857 //
858 // video specific definitions
859 //
860 
861 struct PluginCodec_Video_FrameHeader {
862   unsigned int  x;
863   unsigned int  y;
864   unsigned int  width;
865   unsigned int  height;
866 };
867 
868 #ifdef __cplusplus
869 };
870 
OPAL_VIDEO_FRAME_DATA_PTR(struct PluginCodec_Video_FrameHeader * base)871 inline unsigned char * OPAL_VIDEO_FRAME_DATA_PTR(struct PluginCodec_Video_FrameHeader * base)
872 { return (((unsigned char *)base) + sizeof(PluginCodec_Video_FrameHeader)); }
873 
OPAL_VIDEO_FRAME_DATA_PTR(const PluginCodec_Video_FrameHeader * base)874 inline unsigned char * OPAL_VIDEO_FRAME_DATA_PTR(const PluginCodec_Video_FrameHeader * base)
875 { return (((unsigned char *)base) + sizeof(PluginCodec_Video_FrameHeader)); }
876 
877 extern "C" {
878 #endif
879 
880 #define PLUGIN_CODEC_VIDEO_SET_FRAME_SIZE_FN    "set_frame_size"    // argument is struct PluginCodec_VideoSetFrameInfo
881 struct PluginCodec_Video_SetFrameInfo {
882   int width;
883   int height;
884 };
885 
886 
887 /////////////////
888 //
889 // experimental definitions for statically linking codecs
890 //
891 
892 #ifdef OPAL_STATIC_CODEC
893 
894 #  undef PLUGIN_CODEC_DLL_API
895 #  define PLUGIN_CODEC_DLL_API static
896 #  define PLUGIN_CODEC_IMPLEMENT(name) \
897 unsigned int Opal_StaticCodec_##name##_GetAPIVersion() \
898 { return PWLIB_PLUGIN_API_VERSION; } \
899 static struct PluginCodec_Definition * PLUGIN_CODEC_GET_CODEC_FN(unsigned * count, unsigned /*version*/); \
900 struct PluginCodec_Definition * Opal_StaticCodec_##name##_GetCodecs(unsigned * p1, unsigned p2) \
901 { return PLUGIN_CODEC_GET_CODEC_FN(p1,p2); } \
902 
903 #  define PLUGIN_CODEC_IMPLEMENT_ALL(name, table, ver) \
904 unsigned int Opal_StaticCodec_##name##_GetAPIVersion() \
905 { return PWLIB_PLUGIN_API_VERSION; } \
906 PLUGIN_CODEC_DLL_API struct PluginCodec_Definition * Opal_StaticCodec_##name##_GetCodecs(unsigned * count, unsigned version) \
907 { *count = sizeof(table)/sizeof(struct PluginCodec_Definition); return version < ver ? NULL : table; }
908 
909 
910 #else
911 
912 #  define PLUGIN_CODEC_IMPLEMENT(name) \
913 PLUGIN_CODEC_DLL_API unsigned int PLUGIN_CODEC_API_VER_FN() \
914 { return PWLIB_PLUGIN_API_VERSION; } \
915 
916 #  define PLUGIN_CODEC_IMPLEMENT_ALL(name, table, ver) \
917 PLUGIN_CODEC_IMPLEMENT(name) \
918 PLUGIN_CODEC_DLL_API struct PluginCodec_Definition * PLUGIN_CODEC_GET_CODEC_FN(unsigned * count, unsigned version) \
919 { *count = sizeof(table)/sizeof(struct PluginCodec_Definition); return version < ver ? NULL : table; }
920 
921 
922 #endif
923 
924 #ifdef __cplusplus
925 };
926 #endif
927 
928 
929 #ifdef _MSC_VER
930 #pragma warning(default:4201)
931 #endif
932 
933 #endif // OPAL_CODEC_OPALPLUGIN_H
934