////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
// All rights reserved.
// Confidential and Proprietary - Qualcomm Technologies, Inc.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @file camxsensorproperty.h
/// @brief Define sensor properties per usecase and per frame
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#ifndef CAMXSENSORPROPERTY_H
#define CAMXSENSORPROPERTY_H

#include "camxdefs.h"
#include "camxpdafconfig.h"
#include "camxsensordriver.h"
#include "camxtypes.h"
#include "chi.h"
#include "chicommontypes.h"

CAMX_NAMESPACE_BEGIN

static const UINT   MaxSensorModes = 50; ///< Max number of sensor modes
static const UINT32 MinCFAPixelCoordinates = 4; ///< Defines the smallest number of pixels in a CFA pattern(1x1(BGGR), 2x2array)
static const UINT32 MaxCFAPixelCoordinates = 64; ///< Defines the max number of pixels in a CFA pattern(4x4 pattern, 8x8array)

/// @brief CSI Data Type (DT)
/// @note This is not the complete set, many more DT-s are sensor specific, to be added as needed
static const UINT8 CSIDataTypeEmbedData   = 0x12; ///< Data type for EmbedData format
static const UINT8 CSIDataTypeYUV422_8    = 0x1E; ///< Data type for YUV422_8 format
static const UINT8 CSIDataTypeYUV422_10   = 0x1F; ///< Data type for YUV422_10 format
static const UINT8 CSIDataTypeRaw8        = 0x2A; ///< Data type for Raw8 format
static const UINT8 CSIDataTypeRaw10       = 0x2B; ///< Data type for Raw10 format
static const UINT8 CSIDataTypeRaw12       = 0x2C; ///< Data type for Raw12 format
static const UINT8 CSIDataTypeRaw14       = 0x2D; ///< Data type for Raw14 format

/// @brief CSID config
static const UINT8  CSIDecode6Bit         = 0x0; ///< CSID config data
static const UINT8  CSIDecode8Bit         = 0x1; ///< CSID config data
static const UINT8  CSIDecode10Bit        = 0x2; ///< CSID config data
static const UINT8  CSIDecode12Bit        = 0x3; ///< CSID config data
static const UINT8  CSIDecode14Bit        = 0x4; ///< CSID config data
static const UINT8  CSIDecodeDPCM_10_8_10 = 0xF; ///< CSID config data
static const UINT8  CSIDecode16Bit        = 0x5; ///< CSID config data
static const UINT8  CSIDecode20Bit        = 0x6; ///< CSID config data
static const UINT8  CSIDecode24Bit        = 0x7; ///< CSID config data

/// @brief Aspect ratio tolerence beyond which aspect ratio adjustment required
static const FLOAT AspectRatioTolerance = 0.01f;

/// @brief Sensor pixel format including filter arrangement
enum class PixelFormat
{
    Invalid = -1,           ///< Invalid type
    BayerBGGR,              ///< Bayer format BGGR
    BayerGBRG,              ///< Bayer format GBRG
    BayerGRBG,              ///< Bayer format GRBG
    BayerRGGB,              ///< Bayer format RGGB
    YUVFormatY,             ///< YCbCr format Y
    YUVFormatUYVY,          ///< YCbCr format UYVY
    YUVFormatYUYV,          ///< YCbCr format YUYV
    YUVFormatVYUY,          ///< YCbCr format VYUY
    YUVFormatYVYU,          ///< YCbCr format YVYU
    BayerRGBIR,             ///< Bayer format RGBIR
    YUV422NV16,             ///< Y422 format
    MetaStatsEmbed,         ///< Meta format Embedded
    MetaStatsPDAF,          ///< Meta format PDAF
    MetaStatsHDR,           ///< Meta format HDR
    Monochrome,             ///< Mono format
    RGBIR,                  ///< RGBIR
    RGBWC,                  ///< RGBWC
    RGBWK,                  ///< RGBWK
    UnconventionalBayer,    ///< Unconventional Bayer Pattern
};

/// @brief Sensor operation mode for this usecase
enum class UsecaseMode
{
    Invalid = -1,    ///< Invalid type
    Default,         ///< Non HFR mode for normal camera, camcorder usecases
    HFR,             ///< HFR mode used to capture slow motion video
    HDR,             ///< HDR mode used to High Dynamic Range imaging
    RawHDR,          ///< RAW HDR mode used to stream raw HDR
};

/// @brief Sensor PDAF Buffer Format
enum class PDBufferFormat
{
    MIPI8,
    MIPI10,
    PACKED10,
    UNPACKED16,
    MIPI12
};

/// @brief Sensor PDAF Coordinates
struct PDPixelCoordinates
{
    INT32  PDXCoordinate;
    INT32  PDYCoordinate;
};

/// @brief Sensor output resolution
struct SensorResolution
{
    UINT    outputWidth;    ///< Output width
    UINT    outputHeight;   ///< Output height
};

/// @brief Sensor Aspect Ratio Info
struct SensorAspectRatioInfo
{
    FLOAT   sensorARValue;      ///< Sensor Aspect Ratio value
    UINT    sensorARMode;       ///< Current Sensor Aspect Ratio Mode (0, 1, 2, ...)
    UINT    sensorARModeNum;    ///< Number of Sesnor Aspect Ratio Modes
};

/// @brief Sensor output offset
struct SensorPixelOffset
{
    UINT    xStart;    ///< x Start
    UINT    yStart;    ///< y Start
};

/// @brief Sensor crop info
struct SensorCropInfo
{
    UINT    firstPixel;     ///< left crop
    UINT    firstLine;      ///< top crop
    UINT    lastPixel;      ///< outputWidth - right crop -1
    UINT    lastLine;       ///< outputHeight - bottom crop -1
};

/// @brief Maximum stream configuration structure per sensor resolution usecase
static const UINT MaxSensorModeStreamConfigCount = static_cast<UINT>(StreamType::MAX);


/// @brief Maximum sensor capabilities supported per sensor resolution (Normal, HFR, HDR, PDAF, ZZHDR, FASTAEC, DEPTH, SHDR, FS, Internal, InSensorZoom, XCFA, XHDR)
static const UINT MaxSensorCapabilityCount       = 12;

/// @brief Maximum Frequency Modulations supported per sensor resolution
static const UINT MaxFrequencyModulations        = 2;

/// @brief QCFA HDR Types
enum QHDRType
{
    PixelInterleaved     = 0, ///< Pixel Interleaved, QCFA format like native pixel arrangement
    RowInterleaved       = 1, ///< Row Interleaved, each exposure on different line via same VC-DT
    RowInlineInterleaved = 2, ///< Row Inline Interleaved, each exposure on different line via different VC-DT
};

/// @brief QCFA HDR Patterns
/// In direction of readout - First Exposure, Second Exposure, Third Exposure, Fourth Exposure
/// L - Long exposure, M - Middle exposure, S - Short Exposure
enum QHDRPattern
{
    MLSM = 0,
    SMML = 1,
    MSLM = 2,
    LMMS = 3,
    MLMS = 4,
    SMLM = 5,
    MSML = 6,
    LMSM = 7,
};

/// @brief contains QCFA HDR Information
struct QHDRInformation
{
    BOOL         isQHDRSupported; ///< True if QHDR information exists for this mode
    QHDRType     QHDRType;        ///< QCFA HDR Type
    QHDRPattern  QHDRPattern;     ///< QCFA HDR Pattern
};

/// @brief contains Context stream information

/// @brief Pixel Exposure Info
enum SensorHDRExposureType : UINT32
{
    Long,    ///< 0: Long   exposure Pixel
    Medium,  ///< 1: Medium exposure pixel
    Short,   ///< 2: Short  exposure Pixel
};


/// @brief CFA Pixel Types
enum ColorFilterArrangementPixelType
{
    INVALID   = 0, ///< Invalid/Unsupported Pixel Type
    RED       = 1, ///< Red Pixel Type
    GREEN     = 2, ///< Green Pixel Type
    BLUE      = 3, ///< Blue Pixel Type
    INFRARED  = 4, ///< IR Pixel Type
    MONO      = 5, ///< Mono/W Pixel Type
};

/// @brief contains CFA Pixel Coordinate
struct ColorFilterArrangementPixelCoordinate
{
    UINT32                          xCoordinate;    ///< X coordinate for the pixel in the pattern
    UINT32                          yCoordinate;    ///< Y coordinate for the pixel in the pattern
    SensorHDRExposureType           exposureType;   ///< Exposure Type: Long, Medium, Short
    ColorFilterArrangementPixelType pixelType;      ///< Pixel type: Red, Green, Blue, IR, Mono
};

/// @brief contains CFA Information
struct ColorFilterArrangementInformation
{
    UINT32                                numberOfPixelsPerColor;   ///< Number of pixels per color in filer arrangement
    UINT32                                patternWidth;             ///< Number of pixels in horizontal direction for a pattern
    UINT32                                patternHeight;            ///< Number of pixels in vertical direction for a pattern
    UINT32                                pixelCoordinatesCount;    ///< Pixel cordinate count in a pattern
    ColorFilterArrangementPixelCoordinate pixelCoordinates[MaxCFAPixelCoordinates];
                                                                    ///< Array of pixel coordinates defined in the pattern
};

/// @brief contains Depth Information
struct SensorDepthInfo
{
    BOOL                    isAvailable;                                ///< Indicates if the sensor driver has depth info
    DepthPhaseType          phaseType;                                  ///< Number of phases supported by the mode
    BOOL                    isPhaseShuffle;                             ///< Indicates if mode supports Phase shuffle
    BOOL                    isInterleavingMode;                         ///< Indicates if the frames will be batched in
                                                                        ///< interleaving fashion
    UINT32                  freqModulation[MaxFrequencyModulations];    ///< List of light frequencies generated
    UINT32                  frequencyGCD;                               ///< Greatest Common Divisor of the light frequencies
};

/// @brief Structure describing current sensor mode in a usecase
struct SensorMode
{
    UINT8                     decodeFormat;                                 ///< CSI Decode Format
    UINT64                    outPixelClock;                                ///< Output pixel clock
    UINT64                    vtPixelClock;                                 ///< Vt pixel clock
    PixelFormat               format;                                       ///< Pixel format
    DOUBLE                    maxFPS;                                       ///< Max fps for the mode
    UINT32                    capabilityCount;                              ///< sensor capabilities count
    SensorCapability          capability[MaxSensorCapabilityCount];         ///< Normal, HFR, HDR, PDAF, QUADCFA, ZZHDR etc
    UINT                      numPixelsPerLine;                             ///< Number of pixels per line (lineLengthPclk)
    UINT                      numLinesPerFrame;                             ///< Number of lines per frame (frameLengthLines)
    UINT                      maxLineCount;                                 ///< Max line count
    UINT64                    minExposureTime;                              ///< Min exposure time
    UINT64                    maxExposureTime;                              ///< Min exposure time
    UINT64                    minMiddleExposureTime;                        ///< Min middle exposure time
    UINT64                    maxMiddleExposureTime;                        ///< Max middle exposure time
    UINT64                    minShortExposureTime;                         ///< Min short exposure time
    UINT64                    maxShortExposureTime;                         ///< Max short exposure time
    DOUBLE                    lineReadoutTimeNS;                            ///< Line Readout Time in nano seconds
    UINT32                    minHorizontalBlanking;                        ///< Minimum horizontal blanking interval in pixels
    UINT32                    minVerticalBlanking;                          ///< Minimum horizontal blanking interval in lines
    DOUBLE                    maxGain;                                      ///< Max gain
    SensorResolution          resolution;                                   ///< Sensor output width and height for this mode
    SensorAspectRatioInfo     aspectRatioInfo;                              ///< Aspect Ratio Infos for this mode
                                                                            ///  and the number of all AR modes

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Crop/Binning/scaling information
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    SensorPixelOffset                 offset;                                       ///< Sensor offsets w.r.to active array
    UINT                              binningTypeH;                                 ///< Binning factor in horizontal direction
    UINT                              binningTypeV;                                 ///< Binning factor in vertical direction
    FLOAT                             downScaleFactor;                              ///< downscale factor(in H&V directions)
    SensorCropInfo                    cropInfo;                                     ///< CSID crop requested
    SensorCropInfo                    SFEcropInfo;                                  ///< SFE crop requested
    BOOL                              isSFEcropEnable;                              ///< SFE crop enable
    CHIRectangle                      fullSizeArrayCropWindow;                      ///< Crop co-ordinates with respect to
                                                                                    ///  sensor full size pixel array.
    CHIRectangle                      activeArrayCropWindow;                        ///< Crop co-ordinates with respect to
                                                                                    ///  active pixel array.

    BOOL                              is3Phase;                                     ///< FALSE for DPHY, TRUE for CPHY
    UINT32                            CSIPHYId;                                     ///< PHY slot id
    UINT32                            laneCount;                                    ///< lane Count
    UINT64                            streamConfigCount;                            ///< Stream configurations count
    StreamConfiguration               streamConfig[MaxSensorModeStreamConfigCount]; ///< Stream Configurations
    UINT                              ZZHDRColorPattern;                            ///< ZZHDR Color Pattern Information
    ZZHDRFirstExposurePattern         ZZHDRFirstExposure;                           ///< ZZHDR First Exposure Pattern
    UINT                              HDR3ExposureTypeInfo;                         ///< 3-Exposure HDR type
    CHIREMOSAICTYPE                   RemosaicType;                                 ///< Remosaic type
    UINT                              sensorFrameSkip;                              ///< Initial frames that need to be skipped
                                                                                    ///  from sensor
    UINT16                            mipiFlags;                                    ///< mipi Receiver settings
    UINT64                            rollingShutterSkew;                           ///< Rolling shutter skew
    QHDRInformation                   QCFAHDRInformation;                           ///< QHDR Information for this sensor mode
    CHIHDREXPOSURETYPE                HDRExposureTypeInformation;                   ///< Sensor HDR Exposure Type
    UINT                              numberOfPixelsPerColor;                       ///< Number of pixels per color
                                                                                    ///< To be deprecated: part of the CFA info
    BOOL                              isOverlappedSHDRSupported;                    ///< Indication if sensor mode supports
                                                                                    ///< overlapped SHDR capability
    UINT64                            activeTransmissionTimeInUs;                   ///< Active transmission time in Us
    ColorFilterArrangementInformation CFAInfo;                                      ///< Information about CFA pattern
    SensorDepthInfo                   depthInfo;                                    ///< Sensor Depth Information
    UINT64                            contextInfoCount;                             ///< Context count
};

/// @brief Structure describing sensor modes for the current usecase
struct UsecaseSensorModes
{
    SensorMode allModes[MaxSensorModes];    ///< Supported sensor modes for this camera session
};

/// @brief Structure describing Lens info
struct LensInfo
{
    FLOAT  focalLength;            ///< Focal length of the sensor
    FLOAT  fNumber;                ///< f-number or aperture of the lens
    FLOAT  pixelSize;              ///< Pixel size of the sensor
    FLOAT  totalFDistance;         ///< Total f-distance
    FLOAT  actuatorSensitivity;    ///< Sensitivity of the actuator
};

/// @brief Structure describing sensor metadata for a frame
struct SensorMetaData
{
    UINT32 width;                       ///< Width of the frame
    UINT32 height;                      ///< Height of this frame
    FLOAT  sensorGain;                  ///< Sensor real gain programed in this frame
    UINT32 frameLengthLines;            ///< Frame length lines of this frame
    UINT32 preShutterLengthLines;       ///< Pre shutter length lines
    UINT64 exposureTime;                ///< Exposure time of this frame
    FLOAT  middleSensorGain;            ///< Middle frame sensor real gain programed in this frame
    UINT64 middleExposureTime;          ///< Middle frame sensor exposure time for this frame
    FLOAT  shortSensorGain;             ///< Short frame sensor real gain programed in this frame
    UINT64 shortExposureTime;           ///< Short frame sensor exposure time for this frame
    INT32  sensitivity;                 ///< Sensor sensitivity
    INT32  middleSensitivity;           ///< Middle frame Sensor sensitivity
    INT32  shortSensitivity;            ///< Short frame Sensor sensitivity
    UINT32 testPatternMode;             ///< Test pattern mode
    UINT32 filterArrangement;           ///< Colour filter pattern
    UINT64 frameDuration;               ///< Total frame duration
    UINT64 rollingShutterSkew;          ///< Rolling shutter skew
    UINT64 longVerticalOffsetDuration;  ///< Long Vertcal Offset Duration
    UINT64 shortVerticalOffsetDuration; ///< Short Vertical Offset Duration
    UINT64 middleVertcalOffsetDuration; ///< Middle Vertical Offset Duration
};

/// @brief Structure describing sensor current mode resolution info for a frame
struct SensorResolutionInfo
{
    UINT64 vtPixelClk;          ///< Video domain timing clock value
    UINT16 lineLengthPixelClk;  ///< Line length pixel clock of frame
    FLOAT  frameRate;           ///< Frame rate of this frame
    UINT32 frameLengthLine;     ///< Frame length line of this frame
    UINT64 lineReadoutTime;     ///< one line read out time
};

/// @brief Structure describing camera module configuration as read from the driver
struct CameraConfigurationInformation
{
    UINT32 mountAngle;        ///< Camera mount angle
    UINT32 imageOrientation;  ///< Camera image Orientation
    UINT   position;          ///< Camera facing position
};

/// @brief Sensor PDAF Information needed for module configuration
struct SensorPDAFInfo
{
    UINT32               PDAFBlockWidth;          ///< PDAF block width
    UINT32               PDAFBlockHeight;         ///< PDAF block height
    UINT32               PDAFGlobaloffsetX;       ///< PDAF offset X
    UINT32               PDAFGlobaloffsetY;       ///< PDAF offset Y
    UINT32               PDAFPixelCount;          ///< PDAF Pixel count
    PDPixelCoordinates   PDAFPixelCoords[256];    ///< PDAF Pixel Coordinates
    PDBufferFormat       PDAFBufferFormat;        ///< PDAF Buffer Format
    PDBufferFormat       PDAFNativeBufferFormat;  ///< PDAF Native Buffer Format
    PDAFType             PDAFSensorType;          ///< PDAF Sensor type
};


static const UINT ExposureMaskMaxWidth  = 16; ///< Exposure Mask Pattern Max Width
static const UINT ExposureMaskMaxHeight = 16; ///< Exposure mask Pattern Max Height

/// @brief Sensor HDR Information needed for SFE
struct SensorHDRInfo
{
    UINT32                exposureBlockWidth;                                        ///< Exposure block width
    UINT32                exposureBlockHeight;                                       ///< Exposure block height
    SensorHDRExposureType exposureMask[ExposureMaskMaxWidth][ExposureMaskMaxHeight]; ///< Exposure Mask Msx 16 x 16 block
                                                                                     ///  Suppprted Block sizes are 2 x 2, 4 x 4
                                                                                     ///  8 x 8 and 16 x 16..
    UINT32                numberOfExposures;                                         ///< NUmber of exposures
    SensorHDRExposureType firstRowExposure;                                          ///< First  row exposure information
    SensorHDRExposureType secondRowExposure;                                         ///< Second row exposure information
    SensorHDRExposureType thirdRowExposure;                                          ///< Third  row  exposure informration
};

/// @brief Sensor Properties
struct SensorProperties
{
    INT32 sensingMethod;
    FLOAT focalLengthIn35mm;
};

CAMX_NAMESPACE_END

#endif // CAMXSENSORPROPERTY_H
