
/*!
 ***************************************************************************
 * \file
 *    rdopt.h
 *
 * \author
 *    Alexis Michael Tourapis
 *
 * \date
 *    21. February 2005
 *
 * \brief
 *    Headerfile for for rd-optimized mode decision
 **************************************************************************
 */

/*******************************************************************************************
* Modifications:
*
* October 7, 2005: Support for the new 4:4:4 coding method (Advanced 4:4:4 Profile in JVT-P017)
*
* Copyright 2005, Thomson Inc, all rights reserved
*
* Haoping.Yu@thomson.net
*
********************************************************************************************/


#ifndef _RD_OPT_H_
#define _RD_OPT_H_

#define KS_MV

extern CSptr cs_mb, cs_b8, cs_cm, cs_imb, cs_ib8, cs_ib4, cs_pc;
extern imgpel   mpr_8x8ts[16][16];
extern imgpel   rec_mbY[16][16], rec_mbU[16][16], rec_mbV[16][16];    // reconstruction values
extern RD_8x8DATA tr4x4, tr8x8;

extern const  int LEVELMVLIMIT[17][6];
extern int    ****cofAC_8x8ts;        // [8x8block][4x4block][level/run][scan_pos]
extern int    ****cofAC, ****cofAC8x8;        // [8x8block][4x4block][level/run][scan_pos]
extern int    QP2QUANT[40];
extern int    cbp_blk8x8;
extern int    cbp, cbp8x8, cnt_nonz_8x8;
extern int64  cbp_blk;
extern int64  cbp_blk8_8x8ts;
extern int    cbp8_8x8ts;
extern int    cnt_nonz8_8x8ts;
extern int    QP,QP2;
extern int    DELTA_QP,DELTA_QP2;
extern int    diffy[16][16];

// Residue Color Transform
extern int    cofAC4x4_chroma[2][2][18];
extern int    mprRGB_8x8[3][16][16], mprRGB_8x8ts[3][16][16];
extern char   b4_ipredmode[16], b4_intra_pred_modes[16];

extern short  bi_pred_me;
extern short  best_mode;
extern short  best8x8mode          [4]; // [block]
extern short  best8x8pdir [MAXMODE][4]; // [mode][block]
extern short  best8x8fwref[MAXMODE][4]; // [mode][block]
extern short  best8x8bwref[MAXMODE][4]; // [mode][block]
extern imgpel pred[16][16];

extern void   set_stored_macroblock_parameters ();
extern void   StoreMV8x8(int);
extern void   RestoreMV8x8(int);
extern void   store_macroblock_parameters (int);
extern void   SetModesAndRefframeForBlocks (int);
extern void   SetRefAndMotionVectors (int, int, int, int, int);
extern void   StoreNewMotionVectorsBlock8x8(int, int, int, int, int, int, int);
extern void   assign_enc_picture_params(int, int, int, int, int, int, int);
extern void   update_refresh_map(int intra, int intra1, Macroblock *currMB);
extern void   SetMotionVectorsMB (Macroblock*, int);
extern void   SetCoeffAndReconstruction8x8 (Macroblock*);

extern int    GetBestTransformP8x8();
extern int    I16Offset (int, int);
extern int    CheckReliabilityOfRef (int, int, int, int);
extern int    Mode_Decision_for_Intra4x4Macroblock (double, int*);
extern void   Mode_Decision_AddLuma_for_Intra4x4Macroblock (double, int*, int); /*lliu, 07/12*/
extern int    Mode_Decision_AddLuma_for_8x8IntraBlocks (int b8, double lambda,int *cost8x8, int uv); /*lliu, 07/12*/
extern int    Mode_Decision_AddLuma_for_4x4IntraBlocks (int  b8,  int  b4,  double  lambda, int* cost4x4, int uv); /*lliu, 07/12*/
extern double RDCost_AddLuma_for_4x4IntraBlocks (int uv, int* nonzero, int b8, int b4, int ipmode, double lambda, double min_rdcost, int mostProbableMode); /*lliu, 07/12*/
extern int    RDCost_for_macroblocks (double, int, double*, double*, int);
extern double RDCost_for_8x8blocks (int*, int64*, double, int, int, short, short, short);

extern const int  b8_mode_table[6];
extern const int  mb_mode_table[9];
  // Residue Color Transform
extern const int  mb_mode_table_RCT[11];

void rc_store_diff(int cpix_x, int cpix_y, imgpel prediction[16][16]);
void update_rc(Macroblock *currMB, short best_mode);
void submacroblock_mode_decision(RD_PARAMS, RD_8x8DATA *, Macroblock *,int ***, int *, short, int, int *, int *, int *, int);
void init_enc_mb_params(Macroblock* currMB, RD_PARAMS *enc_mb, int intra, int bslice);
void list_prediction_cost(int list, int block, int mode, RD_PARAMS enc_mb, int bmcost[5], char best_ref[2]);
void determine_prediction_list(int, int [5], char [2], short *, int *, short *);

#endif

