/*
 * Copyright 2010 Trident Microsystems (Far East) Ltd. All rights reserved.
 * All prior copyright rights in this work and the accompanying software
 * products transferred to Trident Microsystems (Far East) Ltd. by written
 * agreement.
 */
/*****************************************************************************/
/* Copyright (c) 2010 NXP B.V.  All rights are reserved.                     */
/*                                                                           */
/* Redistribution and use in source and binary forms, with or without        */
/* modification, are permitted provided that the following conditions        */
/* are met:                                                                  */
/*                                                                           */
/* Redistributions of source code must retain the above copyright            */
/* notice, this list of conditions and the following disclaimer.             */
/*                                                                           */
/* Redistributions in binary form must reproduce the above copyright         */
/* notice, this list of conditions and the following disclaimer in the       */
/* documentation and/or other materials provided with the distribution.      */
/*                                                                           */
/* Neither the name of NXP nor the names of its                              */
/* contributors may be used to endorse or promote products derived from      */
/* this software without specific prior written permission.                  */
/*                                                                           */
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR     */
/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT      */
/* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,     */
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT          */
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     */
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY     */
/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT       */
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE         */
/* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH          */
/* DAMAGE.                                                                   */
/*                                                                           */
/*****************************************************************************/
/**
    @file   phStbCommon.h
    @brief  Common stuff.

@b  Component: IphStbCommon

*//*

Rev Date        Author      Comments
--------------------------------------------------------------------------------
  1 15042005    laird       First Revision
  2 18042005    laird       More common types added.
  3 20042005    batelaan    Make it compile and work!
  4 05052005    batelaan    Prevent including <tmNxTypes.h> under Linux/MIPS.
  5 13062005    laird       Adding colour LUT stuff so it is available for both processors.
  6 09082005    batelaan    Only define Null if not yet defined.
                            Don't redefine types if tmtypes.h has been included.
  7 17082005    batelaan    Build for WinCE
  8 28092005    batelaan    Build for WinNT.
  9 11112005    batelaan    Added various defs used by tmman port.
 10 15082006    awest       Added (TMFL_OS_IS_LINUX==1) to include tmNxTypes.h
 11 15012007    laird       Making Sure TM_OK is available.
 12 20070124    neill       Remove TM_OK definition
 13 20070709    batelaan    Added (TMFL_OS_IS_CE==1) to include tmNxTypes.h
 14 20070716    myers       #2758: Add support for PTS extraction through dfb - map tmDbClock_t
 15 20070926    batelaan    Add MSG_ macros.
 16 20071012    batelaan    Add MSG_PRINT macro for CE.
 17 20071017    iwillis     Improve QA C score
 18 20071205    batelaan    Corrected MSG_LEVEL_ENABLED
 19 20080110    hutchinson  Merge in EB's changes for WindowsCE 080104 Release.
 20 20080118    hutchinson  Merge in EB's changes for 20080117 release.
 21 20080208    batelaan    Update file copyright/license text
 22 20080310    barnish     Make Bool an unsigned char
 24 20080701    batelaan    Use linux/kernel.h instead of kernel.h
 25 20081008    laird       Add a FrameType here.
 26 20081021    batelaan    Void is now a #define for void to solve C++ issue.
 27 20081110    laird       Update description details.
27.1.1 20090120 laird       Fix my bad coding in phStbFrameInfo utility macros
 28 20090120    batelaan    Update file copyright/license text
 30,31 20090316 nxp13212    Add phStbUserDataInfo_t.
 32 20090407    laird       Add description of number of frame stores in use.
--------------------------------------------------------------------------------
    For consistency and standardisation retain the Section Separators.
*/

#ifndef PHSTBCOMMON_H
#define PHSTBCOMMON_H

/*******************
* INCLUDE FILES    *
********************/

#include "tmFlags.h"

/* For MSG_* macros */
#if TMFL_OS_IS_LINUX
  #if TMFL_MODE_KERNEL
    #include <linux/kernel.h>
  #else
    #include <stdio.h>
  #endif
#endif

/* On TriMedia, Linux, and (Win)CE we use the standard tmNxTypes.h file,
   otherwise we have our own cutdown version. */
#if defined(__TCS__) || (TMFL_CPU_IS_TM==1) || (TMFL_OS_IS_LINUX==1) || (TMFL_OS_IS_CE==1)
#include <tmNxTypes.h>
#else

#define TMNXTYPES_H

/**************************************************************
* EXPORTED MACROS         PH[<LAYER>]_<MODULE><_WORD>+        *
***************************************************************/

/**************************************************************
* EXPORTED TYPEDEFS       [p]ph[<Layer>]<Module>_<Word>+_t  *
***************************************************************/
#if !defined(_TMtypes_h)
/** Standard Type mappings. */
#define Void void
typedef signed   char   Int8;       /*  8-bit   signed integer */
typedef signed   short  Int16;      /* 16-bit   signed integer */
typedef signed   long   Int32;      /* 32-bit   signed integer */
typedef unsigned char   UInt8;      /*  8-bit unsigned integer */
typedef unsigned short  UInt16;     /* 16-bit unsigned integer */
typedef unsigned int    UInt32;     /* 32-bit unsigned integer */
typedef float           Float;      /* 32-bit floating point */
typedef float           Float32;    /* 32-bit floating point */
typedef unsigned char   Bool;       /* Boolean (True/False) */
typedef char            Char;       /* character, character array ptr */
typedef int             Int;        /* machine-natural integer */
typedef unsigned int    UInt;       /* machine-natural unsigned integer */
typedef char           *String;     /* Null-terminated 8-bit char str */

#if (defined(_WIN32) || defined(_WIN64)) && !defined(UNDER_CE)
typedef INT64  Int64;   /* 64-bit signed   integer */
typedef UINT32 UInt64;  /* 64-bit unsigned integer */
#else
typedef signed   long long int Int64;   /* 64-bit signed   integer */
typedef unsigned long long int UInt64;  /* 64-bit unsigned integer */
#endif

#endif //!defined(_TMtypes_h)

#ifndef TM_OK
/** Global success return status */
#define TM_OK 0U
#endif

/** Standard Type mappings
    NOTE: IBits32/UBits32 types are defined for use with 32-bit bitfields.
    This is done because ANSI/ISO compliant compilers require bitfields
    to be of type "int" else a large number of compiler warnings will
    result.  To avoid the risks associated with redefining Int32/UInt32
    to type "int" instead of type "long" (which are the same size on 32-
    bit CPUs) separate 32-bit signed/unsigned bitfield types are defined.
*/
typedef signed   int    IBits32;    /* 32-bit   signed integer bitfields */
typedef unsigned int    UBits32;    /* 32-bit unsigned integer bitfields */

typedef Int8    *pInt8;             /*  8-bit   signed integer ptr */
typedef Int16   *pInt16;            /* 16-bit   signed integer ptr */
typedef Int32   *pInt32;            /* 32-bit   signed integer ptr */
typedef Int64   *pInt64;            /* 64-bit   signed integer ptr */
typedef IBits32 *pIBits32;          /* 32-bit   signed integer bitfield ptr */
typedef UBits32 *pUBits32;          /* 32-bit unsigned integer bitfield ptr */
typedef UInt8   *pUInt8;            /*  8-bit unsigned integer ptr */
typedef UInt16  *pUInt16;           /* 16-bit unsigned integer ptr */
typedef UInt32  *pUInt32;           /* 32-bit unsigned integer ptr */
typedef UInt64  *pUInt64;           /* 64-bit unsigned integer ptr */
typedef Void    *pVoid;             /* Void (typeless) ptr */
typedef Float   *pFloat;            /* 32-bit floating point, float ptr */
typedef double  Double, *pDouble;   /* 32/64-bit floating point, double ptr */
typedef Bool    *pBool;             /* Boolean (True/False) ptr */
typedef Char    *pChar;             /* character, character array ptr */
typedef Int     *pInt;              /* machine-natural integer ptr */
typedef UInt    *pUInt;             /* machine-natural unsigned integer ptr */
typedef String  *pString;           /* Null-terminated 8-bit char str ptr */

/** Map an error Code to UInt32*/
typedef UInt32 tmErrorCode_t;

/** Map Null to linux NULL */
#ifndef Null
#define Null   NULL
#endif

/*-----------------------------------------------------------------------------
** Hardware device power states
*/
typedef enum tmPowerState
{
    tmPowerOn,                          /* Device powered on      (D0 state) */
    tmPowerStandby,                     /* Device power standby   (D1 state) */
    tmPowerSuspend,                     /* Device power suspended (D2 state) */
    tmPowerOff                          /* Device powered off     (D3 state) */

}   tmPowerState_t, *ptmPowerState_t;

/*-----------------------------------------------------------------------------
** Software Version Structure
*/
typedef struct tmSWVersion
{
    UInt32      compatibilityNr;        /* Interface compatibility number */
    UInt32      majorVersionNr;         /* Interface major version number */
    UInt32      minorVersionNr;         /* Interface minor version number */

}   tmSWVersion_t, *ptmSWVersion_t;

/*-----------------------------------------------------------------------------
** NOTE: This macro converts four characters into a UInt32 value.  It is used
**       instead of multi-character constants (i.e. 'ABCD') to prevent some
**       compiler warnings, especially with GNU-based compilers.
*/
#define TM_CHARS_TO_UINT32(a,b,c,d) ((UInt32) ((UInt8) (a) << 24 | (UInt8) (b) << 16 | (UInt8) (c) << 8 | (UInt8) (d)))

#endif /* #if defined(__TCS__) || (TMFL_CPU_IS_TM==1) || (TMFL_OS_IS_LINUX==1) || (TMFL_OS_IS_CE==1) */

/** The following are masks that can be used to fill in the flags
 *  part of phStbFrameInfo_t.*/
#define PHSTB_COMMON_FRAME_INFO_FRAME_TYPE_MASK  (0x00000003)
#define PHSTB_COMMON_FRAME_INFO_PTS_VALID_MASK   (0x00000004)
#define PHSTB_COMMON_FRAME_INFO_PTS_DERIVED_MASK (0x00000008)

typedef struct _phStbFrameInfo_t
{
    /** The high bit of pts (only uses 1 bit) but often
     *  a int64 is used so waste a whole uint32 here.*/
    UInt32 ptsHigh;
    /** The low PTS value.*/
    UInt32 ptsLo;
    /** Frame Width max 4096.
        Lower 16 bits = width.
        Top 16 bits   = height*/
    UInt32 frameDimensions;
    /* Extra flags (can be extended. later)
       Bits 0-1 -> FrameType (I/B/P)
       Bit  2 -> ptsValid
       Bit  3 -> ptsDerived.
       Bits 8 -> 15 Number of frame stores available to be decoded into.
       Bits 16 -> 23 Number of decoded frames awaiting presentation (not including the one being displayed).
       Bits 24 -> 31 Frame Index. */
    UInt32 flags;
} phStbFrameInfo_t;

/** The following are masks that can be used to fill in the flags
 *  part of phStbUserDataInfo_t.*/
#define PHSTB_COMMON_USERDATA_INFO_FRAME_TYPE_MASK      (0x00000003)
#define PHSTB_COMMON_USERDATA_INFO_TOP_FIELD_FIRST_MASK (0x00000004)

typedef struct _phStbUserDataInfo_t
{
    /** Size of User Data */
    UInt32 size;
    /** Extra flags
        Bits 0-1 -> FrameType (I/B/P)
        Bit  2 -> TopFieldFirst */
    UInt32 flags;
} phStbUserDataInfo_t;

/******************************************************************************/
/** Print out a message.
    For example:
    @code
    MSG_PRINT(("hi %s", name));
    @endcode

    @param _expr        @b IN  A parenthesised printf-style expression.
    @retval void
    @note Not affected by #MSG_DBG_ENABLE.
*/
#if TMFL_OS_IS_LINUX
  #if TMFL_MODE_KERNEL
    #define MSG_PRINT(_expr) do { (void) printk _expr; } while (0)
  #else
    #define MSG_PRINT(_expr) do { (void) printf _expr; } while (0)
  #endif
#endif

#if TMFL_OS_IS_CE
  #include "userlib/log.h"
  /* TODO: don't include nxp_kernel.h in usermode!!!
     At the moment it's needed since printk is used
     in both! Doh!!! */
  #include "compat_nxp/nxp_kernel.h"
  #if !TMFL_MODE_KERNEL
    #include "compat_nxp/nxp_userspace.h"
  #endif
  #define MSG_PRINT(_expr) do { printk _expr; } while (0)
#endif

/******************************************************************************/
/** Disables debug message related preprocessor macros if set to 1.
    The default value of MSG_DBG_ENABLE is 1.

    @see MSG_DBG_PRINT
    @see MSG_DPRINT
    @see MSG_LEVEL_ENABLED
    @see MSG_LEVEL_PRINT
    @see MSG_LPRINT
*/
#ifndef MSG_DBG_ENABLE
  #if (TMFL_REL == TMFL_REL_RETAIL) || (TMFL_REL == TMFL_REL_ASSERT)
    #define MSG_DBG_ENABLE 0
  #else
    #define MSG_DBG_ENABLE 1
  #endif
#endif

/******************************************************************************/
// TODO: remove in a later stage; currently, for dev. under CE currently we
// really NEEED THIS FUNCTIONALITY!!!!!!!!!!!!!!!!!!!!!!!!!!
/******************************************************************************/
#if TMFL_OS_IS_CE
#  undef  MSG_DBG_ENABLE
#  define MSG_DBG_ENABLE 1
/* to overcome compiler error:
   C:/WINCE600/PLATFORM/STB225/src/NXP/stb225/src/comps/phStbNetDev/src/phStbNetDev_Common.c(85) : error C2065: 'gphStbNetDev_Debug' : undeclared identifier
   enable it here, not via makefile
 */
#  define PH_STBNETDEV_DEBUG 1
#endif


/******************************************************************************/
/** Print out a message if debug is enabled.
    For example:
    @code
    MSG_DBG_PRINT(gMyCompName_DebugFlag, ("hi %s", name));
    @endcode

    @param _enabled     @b IN  Boolean, if true then the message is printed.
    @param _expr        @b IN  A parenthesised printf-style expression.
    @retval void
    @note Expands to an empty statement if #MSG_DBG_ENABLE is 0.
    @note See #MSG_DPRINT for a shorter version of this macro.
*/
#if MSG_DBG_ENABLE
#define MSG_DBG_PRINT(_enabled, _expr)                  \
  do { if (_enabled) { MSG_PRINT(_expr); } } while (0)
#else
#define MSG_DBG_PRINT(_enabled, _expr) do { } while (0)
#endif

/******************************************************************************/
/** Print out a message if #MSG_DPRINT_FLAG is true.
    For example:
    @code
    #define MSG_DPRINT_FLAG gMyCompName_DebugFlag
    ...
    MSG_DPRINT(("hi %s", name));
    @endcode

    @param _expr        @b IN  A parenthesised printf-style expression.
    @retval void
    @note The use of this macro requires the user to define #MSG_DPRINT_FLAG.
    @note Expands to an empty statement if #MSG_DBG_ENABLE is 0.
    @note See #MSG_DBG_PRINT for a longer version of this macro.
*/
#if MSG_DBG_ENABLE
#define MSG_DPRINT(_requiredLevel, _expr) \
    MSG_DBG_PRINT((MSG_DPRINT_FLAG), _expr)
#else
#define MSG_DPRINT(_requiredLevel, _expr) do { } while (0)
#endif

/******************************************************************************/
/** @macro MSG_DPRINT_FLAG
    The current debug flag used by #MSG_DPRINT.
    @note The user of the #MSG_DPRINT macro must define this macro.
*/

/******************************************************************************/
/** True if debug level >= specified.
    For example:
    @code
    if (MSG_LEVEL_ENABLED(2, gMyCompName_DebugLevel)) ...
    @endcode
    @param _requiredLevel @b IN If not less than _actualLevel, true is returned.
    @param _actualLevel   @b IN If greater or equal than _requiredLevel, true is returned.
    @retval C greater-or-equal-than comparison return value.
    @note Expands to 0 if #MSG_DBG_ENABLE is 0.
*/
#if MSG_DBG_ENABLE
#define MSG_LEVEL_ENABLED(_requiredLevel, _actualLevel) \
      (((UInt32)(_actualLevel)) >= ((UInt32)(_requiredLevel)))
#else
#define MSG_LEVEL_ENABLED(_requiredLevel, _actualLevel) 0
#endif

/******************************************************************************/
/** Print out a message if the debug level >= specified.
    For example:
    @code
    MSG_LEVEL_PRINT(2, gMyCompName_DebugLevel, ("hi %s", name));
    @endcode

    @param _requiredLevel @b IN If not less than _actualLevel, then the message is printed.
    @param _actualLevel   @b IN If greater or equal than _requiredLevel,
    then the message is printed.
    @param _expr        @b IN  A parenthesised printf-style expression.
    @retval void
    @note Expands to an empty statement if #MSG_DBG_ENABLE is 0.
    @note See #MSG_LPRINT for a shorter version of this macro.
*/
#if MSG_DBG_ENABLE
#define MSG_LEVEL_PRINT(_requiredLevel, _actualLevel, _expr) \
    MSG_DBG_PRINT(MSG_LEVEL_ENABLED((_requiredLevel), (_actualLevel)), _expr)
#else
#define MSG_LEVEL_PRINT(_requiredLevel, _actualLevel, _expr) do { } while (0)
#endif

/******************************************************************************/
/** Print out a message if the debug level >= #MSG_LPRINT_LEVEL.
    For example:
    @code
    #define MSG_LPRINT_LEVEL gMyCompName_DebugLevel
    ...
    MSG_LPRINT(2, ("hi %s", name));
    @endcode

    @param _requiredLevel @b IN If not less than #MSG_LPRINT_LEVEL, then the message is printed.
    then the message is printed.
    @param _expr        @b IN  A parenthesised printf-style expression.
    @retval void
    @note The use of this macro requires the user to define #MSG_LPRINT_LEVEL.
    @note Expands to an empty statement if #MSG_DBG_ENABLE is 0.
    @note See #MSG_LEVEL_PRINT for a longer version of this macro.
*/
#if MSG_DBG_ENABLE
#define MSG_LPRINT(_requiredLevel, _expr) \
    MSG_LEVEL_PRINT((_requiredLevel), (MSG_LPRINT_LEVEL), _expr)
#else
#define MSG_LPRINT(_requiredLevel, _expr) do { } while (0)
#endif

/******************************************************************************/
/** @macro MSG_LPRINT_LEVEL
    The current debug level used by #MSG_LPRINT.
    @note The user of the #MSG_LPRINT macro must define this macro.
    @note Never referenced if #MSG_DBG_ENABLE is 0.
*/

/*******************************************************************
* EXPORTED DATA      g[k|p|kp|pk|kpk]tm[<layer>]<Module>_<Word>+ *
********************************************************************/

/** 3-byte color structure */
typedef union phStbColour3
{
    UBits32 u32;
#if (TMFL_ENDIAN == TMFL_ENDIAN_BIG)
    struct {
        UBits32       : 8;
        UBits32 red   : 8;
        UBits32 green : 8;
        UBits32 blue  : 8;
    } rgb;
    struct {
        UBits32   : 8;
        UBits32 y : 8;
        UBits32 u : 8;
        UBits32 v : 8;
    } yuv;
    struct {
        UBits32   : 8;
        UBits32 u : 8;
        UBits32 m : 8;
        UBits32 l : 8;
    } uml;
#else /* (TMFL_ENDIAN == TMFL_ENDIAN_LITTLE) */
    struct {
        UBits32 blue  : 8;
        UBits32 green : 8;
        UBits32 red   : 8;
        UBits32       : 8;
    } rgb;
    struct {
        UBits32 v : 8;
        UBits32 u : 8;
        UBits32 y : 8;
        UBits32   : 8;
    } yuv;
    struct {
        UBits32 l : 8;
        UBits32 m : 8;
        UBits32 u : 8;
        UBits32   : 8;
    } uml;
#endif /* (TMFL_ENDIAN == TMFL_ENDIAN_BIG) */
}   phStbColour3_t;

/** 4-byte color structure */
typedef union phStbColour4
{
    UBits32 u32;
#if (TMFL_ENDIAN == TMFL_ENDIAN_BIG)
    struct {
        UBits32 alpha    : 8;
        UBits32 red      : 8;
        UBits32 green    : 8;
        UBits32 blue     : 8;
    } argb;
    struct {
        UBits32 alpha    : 8;
        UBits32 y        : 8;
        UBits32 u        : 8;
        UBits32 v        : 8;
    } ayuv;
    struct {
        UBits32 alpha    : 8;
        UBits32 u        : 8;
        UBits32 m        : 8;
        UBits32 l        : 8;
    } auml;
#else /* (TMFL_ENDIAN == TMFL_ENDIAN_LITTLE) */
    struct {
        UBits32 blue     : 8;
        UBits32 green    : 8;
        UBits32 red      : 8;
        UBits32 alpha    : 8;
    } argb;
    struct {
        UBits32 v        : 8;
        UBits32 u        : 8;
        UBits32 y        : 8;
        UBits32 alpha    : 8;
    } ayuv;
    struct {
        UBits32 l        : 8;
        UBits32 m        : 8;
        UBits32 u        : 8;
        UBits32 alpha    : 8;
    } auml;
#endif /* (TMFL_ENDIAN == TMFL_ENDIAN_BIG) */
}   phStbColour4_t;

/** A Colour LUT that has 255 possible values, each one
    has a type of ::phStbColour4_t.
    */
typedef struct phStb8BitColourLut
{
    phStbColour4_t   values[255];
} phStb8BitColourLut_t;

typedef struct phStbClock
{
    UInt32 clockHi;                  // Bit 33 of clock
    UInt32 clockLo;                  // Bits 0-32 of clock
    UInt32 clockExtn;                // 9 bits clock extn value
} phStbClock_t;

#endif /* PHSTBCOMMON_H      Do not add any thing below this line */
