/* Copyright Sensoray Company Inc. 2013-2022
 * 2263 SDK functions
 * Version 1.1.27
 * Author: P.E.
 *
 */

#ifndef SRAY2263MID_H_
#define SRAY2263MID_H_

#include <sys/time.h> // for struct timeval

#ifdef __cplusplus
extern "C"
{
#endif

#define MID2263_API
#define MID2263_API_VERSION (0x10100 + 37)

/* 
ModelNumber values relating to board configurations:

model     video inputs  outputs     audio inputs  outputs 
--------  ------------  ---------   ------------  --------
2263      DVI           DVI         Digital       Line-out
          Composite     Composite   Line-in
          Component
          VGA

3364-DVI  DVI           DVI         Digital       Line-out
          Component                 Line-in
          VGA                       Mic

3364-SDI  SDI           SDI         Digital       Line-out
                                    Line-in
                                    Mic

2267      SDI           n/a         Digital       n/a
                                    *Line-in
                                    *Mic

2231      AHD 1         Composite   Line-in       n/a
          AHD 2                     Mic

2271/2273 DVI           n/a         Digital       n/a
          Component                 
          VGA                       
          SDI


(* indicates optional installation)

*/

/* maximum number of boards supported by SDK
 * Note: (The practical maximum number
 *        of boards may be less due to USB bandwidth
 *        constraints)
 */

#define MAX_DEVICES 32

#define MIN_WIDTH 160
#define MIN_HEIGHT 120
#define MAX_WIDTH 768
#define MAX_HD_WIDTH 1920
#define MAX_HEIGHT 576
#define MAX_HD_HEIGHT 1200
#define MAX_GAIN 119

#if 0 // not supported for now
typedef enum {
	MID2263_NTSC = 1,
	MID2263_PAL = 2,
} MID2263_VIDSYS;
#endif

typedef enum {
	MID2263_CAPTURE,
	MID2263_PREVIEW,
	MID2263_DISPLAY,
	MID2263_AUDIO,
} MID2263_STRMIDX;

typedef enum {
	MID2263_RECMODE_TS = 0,	 // Transport Stream with Audio
	MID2263_RECMODE_TS_NOAUDIO = 1, // Transport Stream without Audio
	MID2263_RECMODE_H264 = 2, // H264 Video Elementary Stream
	MID2263_RECMODE_MJPEG = 3, // MJPEG Stream
	MID2263_RECMODE_MP4 = 4, // MP4 file
	MID2263_RECMODE_MP4_NOAUDIO = 5, // MP4 file without Audio
	MID2263_RECMODE_AVI = 6, // AVI with audio
	MID2263_RECMODE_AVI_NOAUDIO = 7,
	MID2263_RECMODE_H265 = 8, // H265 (HEVC) Video Elementary Stream (model 2273 only)
	MID2263_RECMODE_UYVY = 10, // uncompressed YUV
} MID2263_RECMODE;

typedef enum {
	MID2263_FORMAT_UYVY = 0,
	MID2263_FORMAT_NV12 = 1,
} MID2263_FORMAT;

typedef enum {
	MID2263_BRIGHTNESS,
	MID2263_SATURATION,
	MID2263_HUE,
	MID2263_CONTRAST,
	MID2263_COLOR_KILLER, // note: cannot be read with S2263_GetLevel
} MID2263_PARAM;

typedef enum {
	MID2263_RATEMODE_CBR,
	MID2263_RATEMODE_VBR,
	MID2263_RATEMODE_FIXEDQP,
} MID2263_RATEMODE;

typedef enum {
	MID2263_VIDEO_DVI,
	MID2263_VIDEO_COMPOSITE,
	MID2263_VIDEO_SVIDEO,
	MID2263_VIDEO_COMPONENT,
	MID2263_VIDEO_VGA,
	MID2263_VIDEO_SDI2, // model 2271,2273 only
	MID2263_VIDEO_SDI = MID2263_VIDEO_DVI,
} MID2263_VIDINPUT;

typedef enum {
	MID2263_AUDIO_MIC = 0,
	MID2263_AUDIO_LINE_IN = 1,
	MID2263_AUDIO_ANALOG = 1,
	MID2263_AUDIO_DIGITAL = 2,
} MID2263_AUDINPUT;

typedef enum {
	MID2263_H264_BASELINE = 66,	// baseline profile
	MID2263_H264_MAIN = 77,		// main profile
	MID2263_H264_HIGH = 100,	// high profile (default)
} MID2263_H264_PROFILE;

typedef enum {
	MID2263_H264_1_0 = 10,	// 1.0
	MID2263_H264_1B = 9,	// 1B
	MID2263_H264_1_1 = 11,	// 1.1
	MID2263_H264_1_2 = 12,  // 1.2
	MID2263_H264_1_3 = 13,	// 1.3
	MID2263_H264_2_0 = 20,	// 2.0
	MID2263_H264_2_1 = 21,	// 2.1
	MID2263_H264_2_2 = 22,	// 2.2
	MID2263_H264_3_0 = 30,	// 3.0
	MID2263_H264_3_1 = 31,	// 3.1
	MID2263_H264_3_2 = 32,	// 3.2
	MID2263_H264_4_0 = 40,	// 4.0 (default)
	MID2263_H264_4_1 = 41,	// 4.1
	MID2263_H264_4_2 = 42,	// 4.2
	MID2263_H264_5_0 = 50,	// 5.0
} MID2263_H264_LEVEL;

/* NOTE: model 2273 supports the following profiles and levels:
   H.264 profiles: Baseline, Main, High (Constrained Baseline not in MID2263 API)
   H.264 levels: 1.0, 1B, 1.1, 1.2, 1.3, 2.0, 2.1, 2.2, 3.0, 3.1, 3.2, 4.0, 4.1, 4.2, 5.0
   H.265 profiles: Main; tiers: Main/High
   H.265 levels: 1.0, 2.0, 2.1, 3.0, 3.1, 4.0, 4.1, 5.0
*/

typedef enum {
	MID2263_NONE,
	MID2263_AAC,
	MID2263_MP3,
	MID2263_PCM,
} MID2263_AUDMODE;

typedef enum {
	MID2263_INTERPOLATE,
	MID2263_INTERLACE,

	MID2263_FIELD_TOP,
	MID2263_FIELD_BOTTOM,
	MID2263_FIELD_INTERLACED,
	MID2263_FIELD_SEQ_TB,
	MID2263_FIELD_SEQ_BT,
	MID2263_FIELD_ALTERNATE,
	MID2263_FIELD_INTERLACED_TB,
	MID2263_FIELD_INTERLACED_BT,
} MID2263_INTERLACE_MODE;

typedef enum {
	MID2263_ASPECT_NONE,	// square pixel
	MID2263_ASPECT_4_3,	// 4:3
	MID2263_ASPECT_16_9,	// 16:9
} MID2263_ASPECT;

typedef enum {
	MID2263_AUDBR_96	= 96,
	MID2263_AUDBR_128	= 128,
	MID2263_AUDBR_192	= 192,
	MID2263_AUDBR_224	= 224,
	MID2263_AUDBR_256	= 256,
} MID2263_AUDIO_BITRATE;

typedef enum {
	MID2263_FIXED_QP_MIN = 0,
	MID2263_FIXED_QP_MAX = 51,
} MID2263_FIXED_QP;

typedef enum {
	MID2263_OUTPUT_CVIDEO = 0,
	MID2263_OUTPUT_DVI = 2,
} MID2263_OUTPUT;

typedef enum {
	MID2263_OUTPUT_AUTO = 0,
	MID2263_OUTPUT_VGA = 4,  // 640x480p60
	MID2263_OUTPUT_NTSC = 5, // 720x480i60
	MID2263_OUTPUT_PAL = 6,  // 720x576i50
	MID2263_OUTPUT_480P60 = 7, // 720x480p60
	MID2263_OUTPUT_576P50 = 8, // 720x576p50
	MID2263_OUTPUT_720P60 = 9, // 1280x720p60
	MID2263_OUTPUT_720P50 = 10, // 1280x720p50
	MID2263_OUTPUT_720P30 = 11, // 1280x720p30
	MID2263_OUTPUT_1080I60 = 12, // 1920x1080i60
	MID2263_OUTPUT_1080I50 = 13, // 1920x1080i50
	MID2263_OUTPUT_1080P30 = 14, // 1920x1080p30
	MID2263_OUTPUT_1080P25 = 15, // 1920x1080p25
	MID2263_OUTPUT_1080P24 = 16, // 1920x1080p24
} MID2263_OUTPUT_STD;

typedef enum {
	MID2263_OUTPUT_IDLE = 0,
	MID2263_OUTPUT_PASSTHRU = 1,  // HD
	MID2263_OUTPUT_PASSTHRU_SD = 6, // SD
	MID2263_OUTPUT_COLORBARS = 2,
} MID2263_OUTPUT_MODE;

typedef enum {
	MID2263_SLICE_OFF = 0,
	MID2263_SLICE_ON = 1,
} MID2263_SLICE_MODE;

typedef enum {
	MID2263_FLIP_V = 1,
	MID2263_FLIP_H = 2,
	MID2263_ROTATE = 4,
	MID2263_FLIP_MAX = 7,
} MID2263_FLIP;

typedef enum {
	MID2263_CVBS = 0,
	MID2263_AHD = 1,
	MID2263_TVI = 2,
	MID2263_CVI = 3,
} MID2263_AHD_STANDARD;

typedef enum {
	MID2263_FLAG_MP3 = 0x1,
	MID2263_FLAG_SDI = 0x4,
} MID2263_FEATURE_FLAGS;


extern MID2263_API int S2263_Open(int brd_idx);
extern MID2263_API int S2263_BusOpen(const char *bus);
extern MID2263_API int S2263_Close(int devid);
extern MID2263_API int S2263_GetNumDevices(int *num_devices);
extern MID2263_API int S2263_GetAPIVersion();

#if 0 // not supported for now
extern MID2263_API int S2263_SetVidSys(MID2263_VIDSYS vidsys, int devid);
extern MID2263_API int S2263_GetVidSys(MID2263_VIDSYS *vidsys, int devid);
#endif
extern MID2263_API int S2263_SetInput(MID2263_VIDINPUT input, int devid);
extern MID2263_API int S2263_GetInput(MID2263_VIDINPUT *input, int devid);
extern MID2263_API int S2263_GetNumInputs(int *inputs, int devid);

extern MID2263_API int S2263_SetLevel(MID2263_PARAM param, int value, int devid);
extern MID2263_API int S2263_GetLevel(MID2263_PARAM param, int *value, int devid);

extern MID2263_API int S2263_SetImageSize(int width, int height, int devid, int strmidx);
extern MID2263_API int S2263_GetImageSize(int *width, int *height, int devid, int strmidx);
extern MID2263_API int S2263_SetImageFormat(MID2263_FORMAT fmt, int width, int height, int devid, int strmidx);
extern MID2263_API int S2263_GetImageFormat(MID2263_FORMAT *fmt, int *width, int *height, int devid, int strmidx);

extern MID2263_API int S2263_SetRecordMode(MID2263_RECMODE recmode, int devid, int strmidx);
extern MID2263_API int S2263_GetRecordMode(MID2263_RECMODE *recmode, int devid, int strmidx);
extern MID2263_API int S2263_SetMP4FastStart(int max_minutes, int interval_seconds, int devid, int strmidx);

extern MID2263_API int S2263_SetH264Profile(MID2263_H264_PROFILE profile, int devid, int strmidx);
extern MID2263_API int S2263_GetH264Profile(MID2263_H264_PROFILE *profile, int devid, int strmidx);
extern MID2263_API int S2263_SetH264Level(MID2263_H264_LEVEL level, int devid, int strmidx);
extern MID2263_API int S2263_GetH264Level(MID2263_H264_LEVEL *level, int devid, int strmidx);

extern MID2263_API int S2263_SetBitrate(int bitrate_kbps, int devid, int strmidx);
extern MID2263_API int S2263_GetBitrate(int *bitrate_kbps, int devid, int strmidx);
extern MID2263_API int S2263_SetGOPSize(int gopsize, int devid, int strmidx);
extern MID2263_API int S2263_GetGOPSize(int *gopsize, int devid, int strmidx);
extern MID2263_API int S2263_SetRateMode(MID2263_RATEMODE ratemode, int devid, int strmidx);
extern MID2263_API int S2263_GetRateMode(MID2263_RATEMODE *ratemode, int devid, int strmidx);
extern MID2263_API int S2263_SetJPEGQuality(int quality, int devid, int strmidx);
extern MID2263_API int S2263_GetJPEGQuality(int *quality, int devid, int strmidx);
extern MID2263_API int S2263_SetInterlaceMode(MID2263_INTERLACE_MODE mode, int devid, int strmidx);
extern MID2263_API int S2263_GetInterlaceMode(MID2263_INTERLACE_MODE *mode, int devid, int strmidx);
extern MID2263_API int S2263_SetFrameSkip(int skip, int devid, int strmidx);
extern MID2263_API int S2263_GetFrameSkip(int *skip, int devid, int strmidx);
extern MID2263_API int S2263_SetFrameCount(int count, int devid, int strmidx);
extern MID2263_API int S2263_GetFrameCount(int *count, int devid, int strmidx);
extern MID2263_API int S2263_SetFixedQP(int intraframeqp, int interpframeqp, int devid, int strmidx);
extern MID2263_API int S2263_GetFixedQP(int *intraframeqp, int *interpframeqp, int devid, int strmidx);
extern MID2263_API int S2263_SetAspectRatio(MID2263_ASPECT aspect, int devid, int strmidx);
extern MID2263_API int S2263_GetAspectRatio(MID2263_ASPECT *aspect, int devid, int strmidx);
extern MID2263_API int S2263_SetSliceMode(int slicemode, int devid, int strmidx);
extern MID2263_API int S2263_GetSliceMode(int *slicemode, int devid, int strmidx);
extern MID2263_API int S2263_SetFlip(int flip, int devid, int strmidx);
extern MID2263_API int S2263_GetFlip(int *flip, int devid, int strmidx);
extern MID2263_API int S2263_SetCrop(int left, int top, int width, int height, int devid, int strmidx);
extern MID2263_API int S2263_GetCrop(int *left, int *top, int *width, int *height, int devid, int strmidx);
extern MID2263_API int S2263_SetPrivacyMask(int left, int top, int width, int height, int devid);
extern MID2263_API int S2263_GetPrivacyMask(int *left, int *top, int *width, int *height, int devid);
extern MID2263_API int S2263_SetDeintMode(int enable, int channel, int threshold_low, int threshold_high,
					int exclude_start, int exclude_count, int devid);
extern MID2263_API int S2263_GetDeintMode(int *enable, int *channel, int *threshold_low, int *threshold_high,
					int *exclude_start, int *exclude_count, int devid);

extern MID2263_API int S2263_GetSerialNumber(int *serial_number, int devid);
extern MID2263_API int S2263_GetFirmwareVersion(int *firmware_version, int devid);
extern MID2263_API int S2263_GetModelNumber(int *model, int devid);
extern MID2263_API int S2263_GetFeatureFlags(int *flags, int devid);
extern MID2263_API int S2263_FirmwareUpdate(int devid);

extern MID2263_API int S2263_SetStreamWindow(int xid, int devid);
extern MID2263_API int S2263_SetStreamWindowPosition(int x, int y, int devid);

extern MID2263_API int S2263_SetAudioInput(MID2263_AUDINPUT input, int devid);
extern MID2263_API int S2263_GetAudioInput(MID2263_AUDINPUT *input, int devid);
extern MID2263_API int S2263_SetAudioBitrate(MID2263_AUDIO_BITRATE bitrate_kbps, int devid);
extern MID2263_API int S2263_GetAudioBitrate(MID2263_AUDIO_BITRATE *bitrate_kbps, int devid);
extern MID2263_API int S2263_SetAudioMode(MID2263_AUDMODE mode, int devid);
extern MID2263_API int S2263_GetAudioMode(MID2263_AUDMODE *mode, int devid);
extern MID2263_API int S2263_SetAudioGain(int agc, int gain, int devid);
extern MID2263_API int S2263_GetAudioGain(int *agc, int *gain, int devid);

extern MID2263_API int S2263_SetDebug(int param, int value, int devid);
extern MID2263_API int S2263_GetDebug(int param, int *value, int devid);

extern MID2263_API int S2263_GetVideoInfo(int *width, int *height, int *framerate, int *interlaced, int devid);
extern MID2263_API int S2263_GetVideoLock(int *locked, int devid);
extern MID2263_API int S2263_GetAnalogHDStandard(MID2263_AHD_STANDARD *standard, int devid);
extern MID2263_API int S2263_SetAnalogHDStandard(MID2263_AHD_STANDARD standard,
		int width, int height, int framerate, int devid);

extern MID2263_API int S2263_SetGPO(int value, int devid);
extern MID2263_API int S2263_GetGPI(int *value, int devid);

extern MID2263_API int S2263_StartRecord(const char *filename, int devid, int strmidx);
extern MID2263_API int S2263_StopRecord(int devid, int strmidx);

extern MID2263_API int S2263_StartPreview(int devid, int strmidx);
extern MID2263_API int S2263_StopPreview(int devid, int strmidx);

extern MID2263_API int S2263_Snapshot(const char *filename, int devid, int strmidx);

extern MID2263_API int S2263_StartAudioPreview(int devid);
extern MID2263_API int S2263_StopAudioPreview(int devid);

extern MID2263_API int S2263_StartPlayback(const char *filename, MID2263_RECMODE mode, int devid);
extern MID2263_API int S2263_StopPlayback(int devid);


extern MID2263_API int S2263_StartCallback(int devid, int strmidx);
typedef int (* S2263_cb_func_t)(unsigned char *data, unsigned int size, int devid, int strmidx);
extern MID2263_API int S2263_RegisterCallback(S2263_cb_func_t callback, int devid, int strmidx);
extern MID2263_API int S2263_StopStream(int devid, int strmidx);
extern MID2263_API int S2263_PauseStream(int devid, int strmidx);
extern MID2263_API int S2263_ResumeStream(int devid, int strmidx);

extern MID2263_API int S2263_SetOutput(MID2263_OUTPUT output, int devid);
extern MID2263_API int S2263_GetOutput(MID2263_OUTPUT *output, int devid);
extern MID2263_API int S2263_SetOutputStd(MID2263_OUTPUT_STD std, int devid);
extern MID2263_API int S2263_GetOutputStd(MID2263_OUTPUT_STD *std, int devid);
extern MID2263_API int S2263_SetOutputMode(MID2263_OUTPUT_MODE mode, int devid);
extern MID2263_API int S2263_GetOutputMode(MID2263_OUTPUT_MODE *mode, int devid);

/* Note: only for select boards equipped with temperature sensor: 2271,2273 */
extern MID2263_API int S2263_ReadTempSensor(float *temp_C, int devid);

/* Note: this enables workaround to avoid USB errors when undergoing very large 
 * temperature swings.  It will perform USB PHY reset when USB idle for 10 minutes,
 * which may cause the USB host to re-enumerate the device. */
extern MID2263_API int S2263_SetTemperatureRobustness(int devid);

/* sets global printf flag in the SDK. Loud output. for debug only */
extern MID2263_API int S2263_Debug(int bOnOff);

#define OVERLAY_TEXT_DISABLED   0
#define OVERLAY_TEXT_FONT_8x14  1
#define OVERLAY_TEXT_FONT_16x16 2
#define OVERLAY_TEXT_BG_SOLID (0 << 2)
#define OVERLAY_TEXT_BG_TRANSPARENT (1 << 2)
#define OVERLAY_TEXT_BG_SHADED (2 << 2)
#define OVERLAY_TEXT_BG_DARKSHADED (3 << 2)
#define OVERLAY_TEXT_DATE_MM_DD_YYYY (0 << 4)
#define OVERLAY_TEXT_DATE_DD_MM_YYYY (1 << 4)
#define OVERLAY_TEXT_DATE_MM_DD_YY (2 << 4)
#define OVERLAY_TEXT_DATE_DD_MM_YY (3 << 4)
#define OVERLAY_TEXT_DATE_MMM_DD_YYYY (4 << 4)
#define OVERLAY_TEXT_DATE_DD_MMM_YYYY (5 << 4)
#define OVERLAY_TEXT_DATE_MMM_DD_YY (6 << 4)
#define OVERLAY_TEXT_DATE_DD_MMM_YY (7 << 4)
#define OVERLAY_TEXT_DATE_MMM_DDc_YYYY (8 << 4)
#define OVERLAY_TEXT_DATE_DDMMMYYYY (9 << 4)
#define OVERLAY_TEXT_DATE_MMM_DDc_YY (10 << 4)
#define OVERLAY_TEXT_DATE_DDMMMYY (11 << 4)
#define OVERLAY_TEXT_DATE_YYYYMMDD (12 << 4)
#define OVERLAY_TEXT_DATE_YYYYMMMDD (13 << 4)
#define OVERLAY_TEXT_DATE_YYMMDD (14 << 4)
#define OVERLAY_TEXT_DATE_YYMMMDD (15 << 4)
#define OVERLAY_TEXT_DATE_YYYY_MM_DD (16 << 4)
#define OVERLAY_TEXT_DATE_YYYY_MMM_DD (17 << 4)
#define OVERLAY_TEXT_DATE_YY_MM_DD (18 << 4)
#define OVERLAY_TEXT_DATE_YY_MMM_DD (19 << 4)

#ifndef OVERLAY_TEXT_SECONDS_NONE
#define OVERLAY_TEXT_SECONDS_NONE (0 << 10)
#define OVERLAY_TEXT_SECONDS_TENTHS (1 << 10)
#define OVERLAY_TEXT_SECONDS_HUNDREDTHS (2 << 10)
#define OVERLAY_TEXT_SECONDS_THOUSANDTHS (3 << 10)
#define OVERLAY_TEXT_HOURS_MINUTES (4 << 10)
#define OVERLAY_TEXT_HOURS_MINUTES_AM_PM (5 << 10)
#define OVERLAY_TEXT_HOURS_MINUTES_A_P (6 << 10)
#endif

#define OVERLAY_STYLE_BOLD (1 << 0)
#define OVERLAY_STYLE_ITALIC (1 << 1)
#define OVERLAY_STYLE_UNDERLINE (1 << 2)
#define OVERLAY_STYLE_OUTLINE (1 << 3)
#define OVERLAY_STYLE_SHADOW (1 << 4)

extern MID2263_API int S2263_OverlayText(
	const char *text, unsigned int mode, int x, int y,
	int devid, int strmidx);
extern MID2263_API int S2263_OverlayStyledText(
	const char *text, const char *font_name, int font_size, unsigned int style, 
	int x, int y, int alpha,
	int devid, int strmidx, int channel);
extern MID2263_API int S2263_OverlayFile(
	const char *filename, int x, int y, int alpha, 
	int devid, int strmidx, int channel);
extern MID2263_API int S2263_OverlayData(
	const unsigned char *data, unsigned int length, int x, int y, int alpha, 
	int devid, int strmidx, int channel);
extern MID2263_API int S2263_OverlayMove(
	int x, int y, int alpha, 
	int devid, int strmidx, int channel);
extern MID2263_API int S2263_OverlayShow(
	int visible,
	int devid, int strmidx, int channel);

extern MID2263_API int S2263_SetClock(const struct timeval *tv, int devid);




// 3364 functions (may be moved later to s3364mid.h
typedef enum {
	MID3364_INTERLACE = 0,
	MID3364_TOP_FIELD,
	MID3364_BOTTOM_FIELD,
	MID3364_FIELD_SEQ, //
	MID3364_INTERLACE_MODE_LAST = 3,
} MID3364_INTERLACE_MODE;

#ifdef __cplusplus
}
#endif

#endif


