// SPDX-FileCopyrightText: © 2025 Allegro DVT <github-ip@allegrodvt.com>
// SPDX-License-Identifier: MIT

#pragma once
#include "EncEPBuffer.h"
#include "lib_common/SliceConsts.h"
#include "lib_common/BufCommonInternal.h"
#include "lib_common_enc/EncBuffers.h"
#include "lib_common_enc/Lambdas.h"
#include "lib_rtos/types.h"
#define AL_MAX_LAWINDOWSIZE 0
#define ENC_MAX_CMD (AL_MAX_NUM_B_PICT + 3 + AL_MAX_LAWINDOWSIZE)

static const AL_TBufInfo EP1_BUF_SCL_LST =
{
  16, 25600, 256
};

/* see ChooseLda.h for EP1_BUF_LAMBDAS */
// Encoder Parameter Buf 1 Flag,  Size, Offset

static const AL_TBufInfo EP3_BUF_RC_TABLE1 =
{
  1, 512, 0
};
static const AL_TBufInfo EP3_BUF_RC_TABLE2 =
{
  2, 512, 512
};
static const AL_TBufInfo EP3_BUF_RC_CTX =
{
  4, 4096, 1024
}; // no fixed size with max = 4096
static const AL_TBufInfo EP3_BUF_RC_LVL =
{
  8, 32, 5120
};

/*****************************************************************************
   \brief  Retrieves the size of a QP parameters buffer 1 (Lda + SclMtx)
   \return maximum size (in bytes) needed to store
*****************************************************************************/
uint32_t AL_GetAllocSizeEP1(AL_ECodec eCodec);

/*****************************************************************************
   \brief Retrieves the size of a Encoder parameters buffer 2 (QP Ctrl)
   \param[in] tDim Frame size in pixels
   \param[in] eCodec Codec
   \param[in] uLog2MaxCuSize Max size of a coding unit (log2)
   \param[in] uQpLCUGranularity QP block granularity in LCU unit
   \return maximum size (in bytes) needed to store
*****************************************************************************/
uint32_t AL_GetAllocSizeFlexibleEP2(AL_TDimension tDim, AL_ECodec eCodec, uint8_t uLog2MaxCuSize, uint8_t uQpLCUGranularity);

/*****************************************************************************
   \brief  Retrieves the size of a Encoder parameters buffer 3 (HW RateCtrl) for one core
   \return maximum size (in bytes) needed to store
*****************************************************************************/
uint32_t AL_GetAllocSizeEP3PerCore(void);

/*****************************************************************************
   \brief  Retrieves the size of a Encoder parameters buffer 3 (HW RateCtrl)
   \return maximum size (in bytes) needed to store
*****************************************************************************/
uint32_t AL_GetAllocSizeEP3(void);

static const size_t MVBUFF_PL_OFFSET[2] =
{
  0, 64
}; // POC List
static const size_t MVBUFF_LONG_TERM_OFFSET = 128; // Long term flag List
static const size_t MVBUFF_USED_POC = 132; // Used POCs
static const size_t MVBUFF_MV_OFFSET = 256; // Motion Vectors

/*****************************************************************************
   \brief Retrieves the size of a Reference YUV frame buffer
   \param[in] tDim Frame dimensions
   \param[in] uBitDepth YUV bit-depth
   \param[in] eStorageMode YUV storage mode
   \param[in] uLCUSize Max size of a coding unit
   \param[in] eChromaMode Chroma Mode
   \param[in] eOptions Encoding option flags
   \param[in] uMVVRange extra buffer lines used for reconstructed buffering
   \return maximum size (in bytes) needed for the YUV frame buffer
*****************************************************************************/
uint32_t AL_GetAllocSize_EncReference(AL_TDimension tDim, uint8_t uBitDepth, AL_EFbStorageMode eStorageMode, uint8_t uLCUSize, AL_EChromaMode eChromaMode, AL_EChEncOption eOptions, uint16_t uMVVRange);

/*****************************************************************************
   \brief Retrieves the size of a compressed buffer(LCU header + MVDs + Residuals)
   \param[in] tDim Frame dimensions
   \param[in] uLog2MaxCuSize Max size of a coding unit (log2)
   \param[in] uBitDepth YUV bit-depth
   \param[in] eChromaMode Chroma Mode
   \param[in] bUseEnt Do we use entropy compression
   \return maximum size (in bytes) needed for the compressed buffer
*****************************************************************************/
uint32_t AL_GetAllocSize_CompData(AL_TDimension tDim, uint8_t uLog2MaxCuSize, uint8_t uBitDepth, AL_EChromaMode eChromaMode, bool bUseEnt);

/*****************************************************************************
   \brief Retrieves the offset of the current LCU Hdr_MVDs words
   \param[in] tDim Frame dimensions
   \param[in] uLog2MaxCuSize Max size of a coding unit (log2)
   \param[in] uNumCore number of core used by the channel
   \param[in] bUseEnt Do we use entropy compression
   \return maximum size (in bytes) needed for the LCU Info buffer
*****************************************************************************/
uint32_t AL_GetAllocSize_EncCompMap(AL_TDimension tDim, uint8_t uLog2MaxCuSize, uint8_t uNumCore, bool bUseEnt);

/*****************************************************************************
   \brief Retrieves the size of a colocated frame buffer
   \param[in] tDim Frame dimensions
   \param[in] uLog2MaxCuSize Max size of a coding unit (log2)
   \param[in] Codec Flag which specifies the codec used
   \return the size (in bytes) needed for the colocated frame buffer
*****************************************************************************/
uint32_t AL_GetAllocSize_MV(AL_TDimension tDim, uint8_t uLog2MaxCuSize, AL_ECodec Codec);

/*****************************************************************************
   \brief Retrieves the size of an entry_points size buffer
   \param[in] iLCUPicHeight Frame Height in pixel
   \param[in] iNumSlices Number of slices within the frame
   \param[in] uNumCore Number of used core
   \return the size (in bytes) needed for the entry_points size buffer
*****************************************************************************/
uint32_t AL_GetAllocSize_WPP(int32_t iLCUPicHeight, int32_t iNumSlices, uint8_t uNumCore);

uint32_t AL_GetAllocSize_SliceSize(uint32_t uWidth, uint32_t uHeight, uint32_t uNumSlices, uint32_t uLog2MaxCuSize);

/*****************************************************************************
   \brief Retrieves the size of a stream part size buffer
   \param[in] eProfile Encoding profile
   \param[in] iNumCores Number of cores used to encode the frame
   \param[in] iNumSlices Number of slices within the frame
   \param[in] bSliceSize True if frame is encode in slice size mode
   \param[in] iNumTilesPerCore Number of tiles on the frame that will be encode on each core
   \return the size (in bytes) needed for the entry_points size buffer
*****************************************************************************/
uint32_t GetAllocSize_StreamPart(AL_EProfile eProfile, int32_t iNumCores, int32_t iNumSlices, bool bSliceSize, int32_t iNumTilesPerCore);

/*****************************************************************************
   \brief Retrieves plane description of the reference buffer
   \param[in] pPlaneDesc Plane description to fill
   \param[in] tDim Frame dimensions
   \param[in] eChromaMode Chroma Mode
   \param[in] uBitDepth YUV bit-depth
   \param[in] eCodec Codec
   \param[in] uLCUSize Max size of a coding unit
   \param[in] uMVVRange extra buffer lines used for reconstructed buffering
   \param[in] eOptions Encoding option flags
*****************************************************************************/
void AL_FillPlaneDesc_EncReference(AL_TPlaneDescription* pPlaneDesc, AL_TDimension tDim, AL_TPicFormat tPicFormat, AL_ECodec eCodec, uint8_t uLCUSize, uint16_t uMVVRange, AL_EChEncOption eOptions);

/*****************************************************************************
   \brief Calculates the pitch of reconstructed
   \param[in] tTileDim Dimension of tiles
   \param[in] uBitDepth YUV bit-depth
   \param[in] eStorageMode YUV storage mode
   \param[in] uWidth Width of the Rec
   \param[in] bIsLuma Is the plane Luma or Choma
*****************************************************************************/
uint32_t AL_GetRecPitch(AL_TDimension tTileDim, uint32_t uBitDepth, AL_EFbStorageMode eStorageMode, uint32_t uWidth, bool bIsLuma);
