1/*
2 * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3 * Copyright (c) Imagination Technologies Limited, UK
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20 * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 * Authors:
26 *    Edward Lin <edward.lin@intel.com>
27 *
28 */
29
30#include <unistd.h>
31#include <stdio.h>
32#include <memory.h>
33#include "hwdefs/topazhp_core_regs.h"
34#include "hwdefs/topaz_vlc_regs.h"
35#include "hwdefs/topazhp_multicore_regs.h"
36#include "hwdefs/topazhp_multicore_regs_old.h"
37#include "psb_drv_video.h"
38#include "tng_cmdbuf.h"
39#include "tng_hostbias.h"
40#include "psb_drv_debug.h"
41#include <stdlib.h>
42
43#define UNINIT_PARAM 0xCDCDCDCD
44#define TOPAZHP_DEFAULT_bZeroDetectionDisable IMG_FALSE
45#define TH_SKIP_IPE                      6
46#define TH_INTER                           60
47#define TH_INTER_QP                    10
48#define TH_INTER_MAX_LEVEL      1500
49#define TH_SKIP_SPE                     6
50
51#define TOPAZHP_DEFAULT_uTHInter                           TH_INTER
52#define TOPAZHP_DEFAULT_uTHInterQP                      TH_INTER_QP
53#define TOPAZHP_DEFAULT_uTHInterMaxLevel		     TH_INTER_MAX_LEVEL
54#define TOPAZHP_DEFAULT_uTHSkipIPE                      TH_SKIP_IPE
55#define TOPAZHP_DEFAULT_uTHSkipSPE                      TH_SKIP_SPE
56
57#define MIN_32_REV 0x00030200
58#define MAX_32_REV 0x00030299
59// New MP4 Lambda table
60static IMG_UINT8 MPEG4_QPLAMBDA_MAP[31] = {
61    0,  0,  1,  2,  3,
62    3,  4,  4,  5,  5,
63    6,  6,  7,  7,  8,
64    8,  9,  9,  10, 10,
65    11, 11, 11, 11, 12,
66    12, 12, 12, 13, 13, 13
67};
68
69static IMG_UINT8 H263_QPLAMBDA_MAP[31] = {
70    0, 0, 1, 1, 2,
71    2, 3, 3, 4, 4,
72    4, 5, 5, 5, 6,
73    6, 6, 7, 7, 7,
74    7, 8, 8, 8, 8,
75    9, 9, 9, 9, 10,10
76};
77
78// new H.264 Lambda
79static IMG_INT8 H264_QPLAMBDA_MAP_SAD[40] = {
80    2, 2, 2, 2, 3, 3, 4, 4,
81    4, 5, 5, 5, 5, 5, 6, 6,
82    6, 7, 7, 7, 8, 8, 9, 11,
83    13, 14, 15, 17, 20, 23, 27, 31,
84    36, 41, 51, 62, 74, 79, 85, 91
85};
86
87static IMG_INT8 H264_QPLAMBDA_MAP_SATD_TABLES[][52] = {
88    //table 0
89    {
90        8,  8,  8,  8,  8,  8,  8,
91        8,  8,  8,  8,  8,  8,  8,
92        8,  8,  8,  9,  9,     10, 10,
93        11, 11, 12, 12, 13,     13, 14,
94        14, 15, 15, 16, 17,     18, 20,
95        21, 23, 25, 27, 30,     32, 38,
96        44, 50, 56, 63, 67,     72, 77,
97        82, 87, 92
98    },
99};
100
101static IMG_INT32 H264_DIRECT_BIAS[27] = {
102    24, 24, 24, 24, 24, 24, 24, 24, 36,
103    48, 48, 60, 60, 72, 72, 84, 96, 108,
104    200, 324, 384, 528, 672, 804, 924, 1044, 1104
105};
106
107static IMG_INT32 H264_INTRA8_SCALE[27] = {
108    (234 + 8) >> 4, (231 + 8) >> 4,
109    (226 + 8) >> 4, (221 + 8) >> 4,
110    (217 + 8) >> 4, (213 + 8) >> 4,
111    (210 + 8) >> 4, (207 + 8) >> 4,
112    (204 + 8) >> 4, (202 + 8) >> 4,
113    (200 + 8) >> 4, (199 + 8) >> 4,
114    (197 + 8) >> 4, (197 + 8) >> 4,
115    (196 + 8) >> 4, (196 + 8) >> 4,
116    (197 + 8) >> 4, (197 + 8) >> 4,
117    (198 + 8) >> 4, (200 + 8) >> 4,
118    (202 + 8) >> 4, (204 + 8) >> 4,
119    (207 + 8) >> 4, (210 + 8) >> 4,
120    (213 + 8) >> 4, (217 + 8) >> 4,
121    (217 + 8) >> 4
122};
123
124/***********************************************************************************
125* Function Name : H264InterBias
126* Inputs                                : ui8QP
127* Outputs                               :
128* Returns                               : IMG_INT16
129* Description           : return the Inter Bias Value to use for the given QP
130  ************************************************************************************/
131
132static IMG_INT16 H264_InterIntraBias[27] = {
133    20, 20, 20, 20, 20, 20, 50,
134    100, 210, 420, 420, 445, 470,
135    495, 520, 535, 550, 570, 715,
136    860, 900, 1000, 1200, 1400,
137    1600, 1800, 2000
138};
139
140static IMG_INT16 tng__H264ES_inter_bias(IMG_INT8 i8QP)
141{
142    if (i8QP < 1) {
143        i8QP = 1;
144    }
145    if (i8QP > 51) {
146        i8QP = 51;
147    }
148
149    //return aui16InterBiasValues[i8QP-36];
150    return H264_InterIntraBias[(i8QP+1)>>1];
151}
152
153/*****************************************************************************
154 *  Function Name      : CalculateDCScaler
155 *  Inputs             : iQP, bChroma
156 *  Outputs            : iDCScaler
157 *  Returns            : IMG_INT
158 *  Description        : Calculates either the Luma or Chroma DC scaler from the quantization scaler
159 *******************************************************************************/
160IMG_INT
161CalculateDCScaler(IMG_INT iQP, IMG_BOOL bChroma)
162{
163    IMG_INT     iDCScaler;
164    if(!bChroma)
165    {
166        if (iQP > 0 && iQP < 5)
167        {
168            iDCScaler = 8;
169        }
170        else if (iQP > 4 &&     iQP     < 9)
171        {
172            iDCScaler = 2 * iQP;
173        }
174        else if (iQP > 8 &&     iQP     < 25)
175        {
176            iDCScaler = iQP + 8;
177        }
178        else
179        {
180            iDCScaler = 2 * iQP -16;
181        }
182    }
183    else
184    {
185        if (iQP > 0 && iQP < 5)
186        {
187            iDCScaler = 8;
188        }
189        else if (iQP > 4 &&     iQP     < 25)
190        {
191            iDCScaler = (iQP + 13) / 2;
192        }
193        else
194        {
195            iDCScaler = iQP - 6;
196        }
197    }
198    return iDCScaler;
199}
200
201/**************************************************************************************************
202* Function:		MPEG4_GenerateBiasTables
203* Description:	Genereate the bias tables for MPEG4
204*
205***************************************************************************************************/
206void
207tng__MPEG4ES_generate_bias_tables(
208	context_ENC_p ctx)
209{
210    IMG_INT16 n;
211    IMG_INT16 iX;
212    IMG_UINT32 ui32RegVal;
213    IMG_UINT8 uiDCScaleL,uiDCScaleC,uiLambda;
214    IMG_UINT32 uDirectVecBias,iInterMBBias,iIntra16Bias;
215    IMG_BIAS_PARAMS *psBiasParams = &(ctx->sBiasParams);
216
217    ctx->sBiasTables.ui32LritcCacheChunkConfig = F_ENCODE(ctx->uChunksPerMb, INTEL_CH_PM) |
218						 F_ENCODE(ctx->uMaxChunks, INTEL_CH_MX) |
219						 F_ENCODE(ctx->uMaxChunks - ctx->uPriorityChunks, INTEL_CH_PY);
220
221
222    for(n=31;n>=1;n--)
223    {
224	iX = n-12;
225	if(iX < 0)
226	{
227	    iX = 0;
228	}
229	// Dont Write QP Values To ESB -- IPE will write these values
230	// Update the quantization parameter which includes doing Lamda and the Chroma QP
231
232	uiDCScaleL = CalculateDCScaler(n, IMG_FALSE);
233	uiDCScaleC = CalculateDCScaler(n, IMG_TRUE);
234	uiLambda = psBiasParams->uLambdaSAD ? psBiasParams->uLambdaSAD : MPEG4_QPLAMBDA_MAP[n - 1];
235
236	ui32RegVal = uiDCScaleL;
237	ui32RegVal |= (uiDCScaleC)<<8;
238	ui32RegVal |= (uiLambda)<<16;
239	ctx->sBiasTables.aui32LambdaBias[n] = ui32RegVal;
240    }
241
242    for(n=31;n>=1;n-=2) {
243        if(psBiasParams->bRCEnable || psBiasParams->bRCBiases) {
244            uDirectVecBias = psBiasParams->uTHSkipIPE * uiLambda;
245            iInterMBBias    = psBiasParams->uTHInter * (n - psBiasParams->uTHInterQP);
246            //if(iInterMBBias < 0)
247            //    iInterMBBias	= 0;
248            if(iInterMBBias > psBiasParams->uTHInterMaxLevel)
249                iInterMBBias	= psBiasParams->uTHInterMaxLevel;
250            iIntra16Bias = 0;
251        } else {
252            uDirectVecBias  = psBiasParams->uIPESkipVecBias;
253            iInterMBBias    = psBiasParams->iInterMBBias;
254            iIntra16Bias    = psBiasParams->iIntra16Bias;
255        }
256
257        ctx->sBiasTables.aui32IntraBias[n] = iIntra16Bias;
258        ctx->sBiasTables.aui32InterBias_P[n] = iInterMBBias;
259        ctx->sBiasTables.aui32DirectBias_P[n] = uDirectVecBias;
260    }
261
262    if(psBiasParams->bRCEnable || psBiasParams->bRCBiases)
263        ctx->sBiasTables.ui32sz1 = psBiasParams->uisz1;
264    else
265        ctx->sBiasTables.ui32sz1 = psBiasParams->uisz2;
266}
267
268/**************************************************************************************************
269* Function:		H263_GenerateBiasTables
270* Description:	Genereate the bias tables for H.263
271*
272***************************************************************************************************/
273void
274tng__H263ES_generate_bias_tables(
275	context_ENC_p ctx)
276{
277    IMG_INT16 n;
278    IMG_INT16 iX;
279    IMG_UINT32 ui32RegVal;
280    IMG_UINT8 uiDCScaleL,uiDCScaleC,uiLambda;
281    IMG_UINT32 uDirectVecBias,iInterMBBias,iIntra16Bias;
282    IMG_BIAS_PARAMS * psBiasParams = &(ctx->sBiasParams);
283
284    ctx->sBiasTables.ui32LritcCacheChunkConfig = F_ENCODE(ctx->uChunksPerMb, INTEL_CH_PM) |
285						 F_ENCODE(ctx->uMaxChunks, INTEL_CH_MX) |
286						 F_ENCODE(ctx->uMaxChunks - ctx->uPriorityChunks, INTEL_CH_PY);
287
288    for(n=31;n>=1;n--)
289    {
290	iX = n-12;
291	if(iX < 0)
292	{
293	    iX = 0;
294	}
295	// Dont Write QP Values To ESB -- IPE will write these values
296	// Update the quantization parameter which includes doing Lamda and the Chroma QP
297
298	uiDCScaleL	= CalculateDCScaler(n, IMG_FALSE);
299	uiDCScaleC	= CalculateDCScaler(n, IMG_TRUE);
300	uiLambda	= psBiasParams->uLambdaSAD ? psBiasParams->uLambdaSAD : H263_QPLAMBDA_MAP[n - 1];
301
302	ui32RegVal=uiDCScaleL;
303	ui32RegVal |= (uiDCScaleC)<<8;
304	ui32RegVal |= (uiLambda)<<16;
305
306   	ctx->sBiasTables.aui32LambdaBias[n] = ui32RegVal;
307    }
308
309    for(n=31;n>=1;n-=2) {
310        if(psBiasParams->bRCEnable || psBiasParams->bRCBiases) {
311            uDirectVecBias = psBiasParams->uTHSkipIPE * uiLambda;
312            iInterMBBias    = psBiasParams->uTHInter * (n - psBiasParams->uTHInterQP);
313            //if(iInterMBBias < 0)
314            //    iInterMBBias	= 0;
315            if(iInterMBBias > psBiasParams->uTHInterMaxLevel)
316                iInterMBBias	= psBiasParams->uTHInterMaxLevel;
317            iIntra16Bias = 0;
318        } else {
319            uDirectVecBias  = psBiasParams->uIPESkipVecBias;
320            iInterMBBias    = psBiasParams->iInterMBBias;
321            iIntra16Bias    = psBiasParams->iIntra16Bias;
322        }
323
324        ctx->sBiasTables.aui32IntraBias[n] = iIntra16Bias;
325        ctx->sBiasTables.aui32InterBias_P[n] = iInterMBBias;
326        ctx->sBiasTables.aui32DirectBias_P[n] = uDirectVecBias;
327    }
328
329    if(psBiasParams->bRCEnable || psBiasParams->bRCBiases)
330        ctx->sBiasTables.ui32sz1 = psBiasParams->uisz1;
331    else
332        ctx->sBiasTables.ui32sz1 = psBiasParams->uisz2;
333}
334
335/**************************************************************************************************
336* Function:             H264_GenerateBiasTables
337* Description:  Generate the bias tables for H.264
338*
339***************************************************************************************************/
340static void tng__H264ES_generate_bias_tables(context_ENC_p ctx)
341{
342    IMG_INT32 n;
343    IMG_UINT32 ui32RegVal;
344    IMG_UINT32 iIntra16Bias, uisz2, uIntra8Scale, uDirectVecBias_P, iInterMBBias_P, uDirectVecBias_B, iInterMBBias_B;
345    IMG_BIAS_PARAMS * psBiasParams = &(ctx->sBiasParams);
346
347    IMG_BYTE PVR_QP_SCALE_CR[52] = {
348        0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
349        12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,
350        28, 29, 29, 30, 31, 32, 32, 33, 34, 34, 35, 35, 36, 36, 37, 37,
351        37, 38, 38, 38, 39, 39, 39, 39
352    };
353
354    ctx->sBiasTables.ui32LritcCacheChunkConfig =
355        F_ENCODE(ctx->uChunksPerMb, INTEL_CH_PM) |
356        F_ENCODE(ctx->uMaxChunks, INTEL_CH_MX) |
357        F_ENCODE(ctx->uMaxChunks - ctx->uPriorityChunks, INTEL_CH_PY);
358
359    uIntra8Scale = 0;
360    for (n = 51; n >= 0; n--) {
361        IMG_INT32  iX;
362        IMG_UINT32 uiLambdaSAD, uiLambdaSATD;
363
364        iX = n - 12;
365        if (iX < 0) iX = 0;
366
367        uiLambdaSAD  = H264_QPLAMBDA_MAP_SAD[iX];
368        uiLambdaSATD = H264_QPLAMBDA_MAP_SATD_TABLES[psBiasParams->uLambdaSATDTable][n];
369
370        if (psBiasParams->uLambdaSAD  != 0) uiLambdaSAD  = psBiasParams->uLambdaSAD;
371        if (psBiasParams->uLambdaSATD != 0) uiLambdaSATD = psBiasParams->uLambdaSATD;
372
373        // Dont Write QP Values To ESB -- IPE will write these values
374        // Update the quantization parameter which includes doing Lamda and the Chroma QP
375        ui32RegVal = PVR_QP_SCALE_CR[n];
376        ui32RegVal |= (uiLambdaSATD) << 8; //SATD lambda
377        ui32RegVal |= (uiLambdaSAD) << 16; //SAD lambda
378
379        ctx->sBiasTables.aui32LambdaBias[n] = ui32RegVal;
380    }
381
382    for (n = 52; n >= 0; n -= 2) {
383        IMG_INT8 qp = n;
384        if (qp > 51) qp = 51;
385
386        if (psBiasParams->bRCEnable || psBiasParams->bRCBiases) {
387            iInterMBBias_P  = tng__H264ES_inter_bias(qp);
388            uDirectVecBias_P  = H264_DIRECT_BIAS[n/2];
389
390            iInterMBBias_B  = iInterMBBias_P;
391            uDirectVecBias_B  = uDirectVecBias_P  ;
392
393            iIntra16Bias    = 0;
394            uIntra8Scale    = H264_INTRA8_SCALE[n/2] - 8;
395
396            if (psBiasParams->uDirectVecBias != UNINIT_PARAM)
397                uDirectVecBias_B  = psBiasParams->uDirectVecBias;
398            if (psBiasParams->iInterMBBiasB != UNINIT_PARAM)
399                iInterMBBias_B    = psBiasParams->iInterMBBiasB;
400
401            if (psBiasParams->uIPESkipVecBias != UNINIT_PARAM)
402                uDirectVecBias_P  = psBiasParams->uIPESkipVecBias;
403            if (psBiasParams->iInterMBBias != UNINIT_PARAM)
404                iInterMBBias_P    = psBiasParams->iInterMBBias;
405
406            if (psBiasParams->iIntra16Bias != UNINIT_PARAM) iIntra16Bias    = psBiasParams->iIntra16Bias;
407        } else {
408            if (psBiasParams->uDirectVecBias == UNINIT_PARAM || psBiasParams->iInterMBBiasB == UNINIT_PARAM) {
409                drv_debug_msg(VIDEO_DEBUG_GENERAL, "ERROR: Bias for B pictures not set up ... uDirectVecBias = 0x%x, iInterMBBiasB = 0x%x\n", psBiasParams->uDirectVecBias, psBiasParams->iInterMBBiasB);
410                abort();
411            }
412            uDirectVecBias_B  = psBiasParams->uDirectVecBias;
413            iInterMBBias_B    = psBiasParams->iInterMBBiasB;
414
415            if (psBiasParams->uIPESkipVecBias == UNINIT_PARAM || psBiasParams->iInterMBBias == UNINIT_PARAM) {
416                drv_debug_msg(VIDEO_DEBUG_GENERAL, "ERROR: Bias for I/P pictures not set up ... uIPESkipVecBias = 0x%x, iInterMBBias = 0x%x\n", psBiasParams->uIPESkipVecBias, psBiasParams->iInterMBBias);
417                abort();
418            }
419            uDirectVecBias_P  = psBiasParams->uIPESkipVecBias;
420            iInterMBBias_P    = psBiasParams->iInterMBBias;
421
422            iIntra16Bias    = psBiasParams->iIntra16Bias;
423            uisz2   = psBiasParams->uisz2;
424        }
425
426#ifdef BRN_30029
427        //adjust the intra8x8 bias so that we don't do anything silly when 8x8 mode is not in use.
428        if (ctx->ui32PredCombControl & F_ENCODE(1, TOPAZHP_CR_INTRA8X8_DISABLE)) {
429            iIntra16Bias |= 0x7fff << 16;
430        }
431#endif
432//      drv_debug_msg(VIDEO_DEBUG_GENERAL, "qp %d, iIntra16Bias %d, iInterMBBias %d, uDirectVecBias %d\n", qp, iIntra16Bias, iInterMBBias, uDirectVecBias);
433        ctx->sBiasTables.aui32IntraBias[n] = iIntra16Bias;
434        ctx->sBiasTables.aui32InterBias_P[n] = iInterMBBias_P;
435        ctx->sBiasTables.aui32DirectBias_P[n] = uDirectVecBias_P;
436        ctx->sBiasTables.aui32InterBias_B[n] = iInterMBBias_B;
437        ctx->sBiasTables.aui32DirectBias_B[n] = uDirectVecBias_B;
438        ctx->sBiasTables.aui32IntraScale[n] = uIntra8Scale;
439    }
440
441    if (psBiasParams->bRCEnable || psBiasParams->bRCBiases)
442        ctx->sBiasTables.ui32sz1 = psBiasParams->uisz1;
443    else
444        ctx->sBiasTables.ui32sz1 = psBiasParams->uisz2;
445
446    if (psBiasParams->bZeroDetectionDisable) {
447        ctx->sBiasTables.ui32RejectThresholdH264 = F_ENCODE(0, INTEL_H264_ConfigReg1)
448                | F_ENCODE(0, INTEL_H264_ConfigReg2);
449    } else {
450        ctx->sBiasTables.ui32RejectThresholdH264 = F_ENCODE(psBiasParams->uzb4, INTEL_H264_ConfigReg1)
451                | F_ENCODE(psBiasParams->uzb8, INTEL_H264_ConfigReg2);
452    }
453}
454
455
456/**************************************************************************************************
457* Function:             VIDEO_GenerateBias
458* Description:  Generate the bias tables
459*
460***************************************************************************************************/
461VAStatus tng__generate_bias(context_ENC_p ctx)
462{
463    assert(ctx);
464
465    switch (ctx->eStandard) {
466        case IMG_STANDARD_H264:
467            tng__H264ES_generate_bias_tables(ctx);
468            break;
469        case IMG_STANDARD_H263:
470            tng__H263ES_generate_bias_tables(ctx);
471            break;
472        case IMG_STANDARD_MPEG4:
473	    tng__MPEG4ES_generate_bias_tables(ctx);
474	    break;
475/*
476        case IMG_STANDARD_MPEG2:
477                MPEG2_GenerateBiasTables(ctx, psBiasParams);
478                break;
479*/
480        default:
481            break;
482    }
483
484    return VA_STATUS_SUCCESS;
485}
486
487//load bias
488static IMG_INT H264_LAMBDA_COEFFS[4][3] = {
489	{175, -10166, 163244 },	 //SATD Lambda High
490	{ 16,   -236,   8693 },  //SATD Lambda Low
491	{198, -12240, 198865 },	 //SAD Lambda High
492	{ 12,   -176,   1402 },  //SAD Lambda Low
493};
494
495static IMG_INT MPEG4_LAMBDA_COEFFS[3] = {
496	0, 458, 1030
497};
498
499static IMG_INT H263_LAMBDA_COEFFS[3] = {
500	0, 333, 716
501};
502
503static void tng__H263ES_load_bias_tables(
504    context_ENC_p ctx,
505    IMG_FRAME_TYPE __maybe_unused eFrameType)
506{
507    IMG_INT32 n;
508    IMG_UINT32 ui32RegVal;
509    IMG_UINT32 count = 0, cmd_word = 0;
510    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
511    IMG_BIAS_TABLES* psBiasTables = &(ctx->sBiasTables);
512    IMG_UINT32 *pCount;
513    IMG_UINT32 ui8Pipe;
514
515    cmd_word = (MTX_CMDID_SW_WRITEREG & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT;
516    *cmdbuf->cmd_idx++ = cmd_word;
517    pCount = cmdbuf->cmd_idx;
518    cmdbuf->cmd_idx++;
519
520    ctx->ui32CoreRev = 0x00030401;
521
522    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++)
523        tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, TOPAZHP_CR_SEQUENCER_CONFIG, 0, psBiasTables->ui32SeqConfigInit);
524
525    if (ctx->ui32CoreRev <= MAX_32_REV)
526    {
527	for(n=31;n>=1;n--)
528	{
529	    //FIXME: Zhaohan, missing register TOPAZHP_TOP_CR_LAMBDA_DC_TABLE
530	    //tng_cmdbuf_insert_reg_write(TOPAZHP_TOP_CR_LAMBDA_DC_TABLE, 0, psBiasTables->aui32LambdaBias[n]);
531	}
532    } else {
533	ui32RegVal = (((H263_LAMBDA_COEFFS[0]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_00)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_00));
534	ui32RegVal |= (((H263_LAMBDA_COEFFS[1]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_BETA_COEFF_CORE_00)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_BETA_COEFF_CORE_00));
535	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_0, 0, ui32RegVal);
536
537	ui32RegVal = (((H263_LAMBDA_COEFFS[2]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_01)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_01));
538	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_0, 0, ui32RegVal);
539
540	ui32RegVal = 0x3f;
541	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_CUTOFF_CORE_0, 0, ui32RegVal);
542    }
543
544    for(n=31;n>=1;n-=2)
545    {
546	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTRA_BIAS_TABLE, 0, psBiasTables->aui32IntraBias[n]);
547	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTER_BIAS_TABLE, 0, psBiasTables->aui32InterBias_P[n]);
548	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_DIRECT_BIAS_TABLE, 0, psBiasTables->aui32DirectBias_P[n]);
549    }
550
551    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++) {
552	tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, INTEL_SZ, 0, psBiasTables->ui32sz1);
553    }
554
555    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++)
556        tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, INTEL_CHCF, 0, psBiasTables->ui32LritcCacheChunkConfig);
557
558    *pCount = count;
559}
560
561static void tng__MPEG4_load_bias_tables(context_ENC_p ctx)
562{
563    IMG_INT32 n;
564    IMG_UINT32 ui32RegVal;
565    IMG_UINT32 count = 0, cmd_word = 0;
566    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
567    IMG_BIAS_TABLES* psBiasTables = &(ctx->sBiasTables);
568    IMG_UINT32 *pCount;
569    IMG_UINT8 ui8Pipe;
570
571    cmd_word = (MTX_CMDID_SW_WRITEREG & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT;
572    *cmdbuf->cmd_idx++ = cmd_word;
573    pCount = cmdbuf->cmd_idx;
574    cmdbuf->cmd_idx++;
575
576    ctx->ui32CoreRev = 0x00030401;
577
578    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++)
579        tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, TOPAZHP_CR_SEQUENCER_CONFIG, 0, psBiasTables->ui32SeqConfigInit);
580
581    if (ctx->ui32CoreRev <= MAX_32_REV) {
582        for (n=31; n >= 1; n--) {
583	    //FIXME: Zhaohan, missing register TOPAZHP_TOP_CR_LAMBDA_DC_TABLE
584            //tng_cmdbuf_insert_reg_write(TOPAZHP_TOP_CR_LAMBDA_DC_TABLE, 0, psBiasTables->aui32LambdaBias[n]);
585        }
586    } else {
587	    //ui32RegVal = MPEG4_LAMBDA_COEFFS[0]| (MPEG4_LAMBDA_COEFFS[1]<<8);
588	    ui32RegVal = (((MPEG4_LAMBDA_COEFFS[0]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_00)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_00));
589	    ui32RegVal |= (((MPEG4_LAMBDA_COEFFS[1]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_BETA_COEFF_CORE_00)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_BETA_COEFF_CORE_00));
590
591	    tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_0, 0, ui32RegVal);
592	    //ui32RegVal = MPEG4_LAMBDA_COEFFS[2];
593	    ui32RegVal = (((MPEG4_LAMBDA_COEFFS[2]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_01)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_01));
594
595	    tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_0, 0, ui32RegVal);
596
597	    ui32RegVal = 0x3f;
598	    tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_CUTOFF_CORE_0, 0, ui32RegVal);
599    }
600
601    for(n=31;n>=1;n-=2)
602    {
603	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTRA_BIAS_TABLE, 0, psBiasTables->aui32IntraBias[n]);
604	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTER_BIAS_TABLE, 0, psBiasTables->aui32InterBias_P[n]);
605	tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_DIRECT_BIAS_TABLE, 0, psBiasTables->aui32DirectBias_P[n]);
606    }
607
608    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++) {
609	tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, INTEL_SZ, 0, psBiasTables->ui32sz1);
610
611	//VLC RSize is fcode - 1 and only done for mpeg4 AND mpeg2 not H263
612	tng_cmdbuf_insert_reg_write(TOPAZ_VLC_REG, TOPAZ_VLC_CR_VLC_MPEG4_CFG, 0, F_ENCODE(psBiasTables->ui32FCode - 1, TOPAZ_VLC_CR_RSIZE));
613    }
614
615    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++)
616        tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, INTEL_CHCF, 0, psBiasTables->ui32LritcCacheChunkConfig);
617
618    *pCount = count;
619}
620
621static void tng__H264ES_load_bias_tables(
622    context_ENC_p ctx,
623    IMG_FRAME_TYPE eFrameType)
624{
625    IMG_INT32 n;
626    IMG_UINT32 ui32RegVal;
627    IMG_BIAS_TABLES* psBiasTables = &(ctx->sBiasTables);
628    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
629    IMG_UINT32 count = 0, cmd_word = 0;
630    IMG_UINT32 *pCount;
631    IMG_UINT32 ui8Pipe;
632    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: start\n", __FUNCTION__);
633
634    cmd_word = (MTX_CMDID_SW_WRITEREG & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT;
635    *cmdbuf->cmd_idx++ = cmd_word;
636    pCount = cmdbuf->cmd_idx;
637    cmdbuf->cmd_idx++;
638
639    psBiasTables->ui32SeqConfigInit = 0x40038412;
640
641    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++)
642        tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, TOPAZHP_CR_SEQUENCER_CONFIG, 0, psBiasTables->ui32SeqConfigInit);
643
644    ctx->ui32CoreRev = 0x00030401;
645
646    if (ctx->ui32CoreRev <= MAX_32_REV) {
647        for (n=51; n >= 0; n--) {
648	    //FIXME: Zhaohan, missing register TOPAZHP_TOP_CR_LAMBDA_DC_TABLE
649            //tng_cmdbuf_insert_reg_write(TOPAZHP_TOP_CR_LAMBDA_DC_TABLE, 0, psBiasTables->aui32LambdaBias[n]);
650        }
651    } else {
652        //Load the lambda coeffs
653        for (n = 0; n < 4; n++) {
654            //ui32RegVal = H264_LAMBDA_COEFFS[n][0]| (H264_LAMBDA_COEFFS[n][1]<<8);
655            ui32RegVal = (((H264_LAMBDA_COEFFS[n][0]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_00)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_00));
656            ui32RegVal |= (((H264_LAMBDA_COEFFS[n][1]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_BETA_COEFF_CORE_00)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_BETA_COEFF_CORE_00));
657
658            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_0, 0, ui32RegVal);
659            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_1, 0, ui32RegVal);
660            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_ALPHA_COEFF_CORE_2, 0, ui32RegVal);
661
662            //ui32RegVal = H264_LAMBDA_COEFFS[n][2];
663            ui32RegVal = (((H264_LAMBDA_COEFFS[n][2]) << (SHIFT_TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_01)) & (MASK_TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_01));
664
665            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_0, 0, ui32RegVal);
666            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_1, 0, ui32RegVal);
667            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_GAMMA_COEFF_CORE_2, 0, ui32RegVal);
668        }
669        ui32RegVal = 29 |(29<<6);
670        tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_CUTOFF_CORE_0, 0, ui32RegVal);
671        tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_CUTOFF_CORE_1, 0, ui32RegVal);
672        tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_POLYNOM_CUTOFF_CORE_2, 0, ui32RegVal);
673    }
674
675    for (n=52;n>=0;n-=2) {
676        if (eFrameType == IMG_INTER_B) {
677            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTER_BIAS_TABLE, 0, psBiasTables->aui32InterBias_B[n]);
678            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_DIRECT_BIAS_TABLE, 0, psBiasTables->aui32DirectBias_B[n]);
679        } else {
680            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTER_BIAS_TABLE, 0, psBiasTables->aui32InterBias_P[n]);
681            tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_DIRECT_BIAS_TABLE, 0, psBiasTables->aui32DirectBias_P[n]);
682        }
683        tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTRA_BIAS_TABLE, 0, psBiasTables->aui32IntraBias[n]);
684        tng_cmdbuf_insert_reg_write(TOPAZ_MULTICORE_REG, TOPAZHP_TOP_CR_INTRA_SCALE_TABLE, 0, psBiasTables->aui32IntraScale[n]);
685    }
686
687    //aui32HpCoreRegId[ui32Pipe]
688    for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++) {
689        tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, INTEL_SZ, 0, psBiasTables->ui32sz1);
690        tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, INTEL_H264_RT, 0, psBiasTables->ui32RejectThresholdH264);
691    }
692
693//    tng_cmdbuf_insert_reg_write(TOPAZHP_TOP_CR_FIRMWARE_REG_1, (MTX_SCRATCHREG_TOMTX<<2), ui32BuffersReg);
694//    tng_cmdbuf_insert_reg_write(TOPAZHP_TOP_CR_FIRMWARE_REG_1, (MTX_SCRATCHREG_TOHOST<<2),ui32ToHostReg);
695
696    // now setup the LRITC chache priority
697    {
698        //aui32HpCoreRegId[ui32Pipe]
699        for (ui8Pipe = 0; ui8Pipe < ctx->ui8PipesToUse; ui8Pipe++) {
700            tng_cmdbuf_insert_reg_write(TOPAZ_CORE_REG, INTEL_CHCF, 0, psBiasTables->ui32LritcCacheChunkConfig);
701        }
702    }
703    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: count = %d\n", __FUNCTION__, (int)count);
704
705    *pCount = count;
706}
707
708VAStatus tng_load_bias(context_ENC_p ctx, IMG_FRAME_TYPE eFrameType)
709{
710    IMG_STANDARD eStandard = ctx->eStandard;
711
712    switch (eStandard) {
713        case IMG_STANDARD_H264:
714            tng__H264ES_load_bias_tables(ctx, eFrameType); //IMG_INTER_P);
715            break;
716        case IMG_STANDARD_H263:
717            tng__H263ES_load_bias_tables(ctx, eFrameType); //IMG_INTER_P);
718            break;
719        case IMG_STANDARD_MPEG4:
720            tng__MPEG4_load_bias_tables(ctx);
721            break;
722/*
723        case IMG_STANDARD_MPEG2:
724            tng__MPEG2_LoadBiasTables(psBiasTables);
725            break;
726*/
727        default:
728            break;
729    }
730
731    return VA_STATUS_SUCCESS;
732}
733
734void tng_init_bias_params(context_ENC_p ctx)
735{
736    IMG_BIAS_PARAMS * psBiasParams = &(ctx->sBiasParams);
737    memset(psBiasParams, 0, sizeof(IMG_BIAS_PARAMS));
738    //default
739    psBiasParams->uLambdaSAD = 0;
740    psBiasParams->uLambdaSATD = 0;
741    psBiasParams->uLambdaSATDTable = 0;
742
743    psBiasParams->bRCEnable = ctx->sRCParams.bRCEnable;
744    psBiasParams->bRCBiases = IMG_TRUE;
745
746    psBiasParams->iIntra16Bias = UNINIT_PARAM;
747    psBiasParams->iInterMBBias = UNINIT_PARAM;
748    psBiasParams->iInterMBBiasB = UNINIT_PARAM;
749
750    psBiasParams->uDirectVecBias = UNINIT_PARAM;
751    psBiasParams->uIPESkipVecBias = UNINIT_PARAM;
752    psBiasParams->uSPESkipVecBias = 0;      //not in spec
753
754    psBiasParams->uisz1 = 6;
755    psBiasParams->uisz2 = 6;
756    psBiasParams->bZeroDetectionDisable = TOPAZHP_DEFAULT_bZeroDetectionDisable;
757
758    psBiasParams->uzb4 = 6;
759    psBiasParams->uzb8 = 4;
760
761    psBiasParams->uTHInter = TOPAZHP_DEFAULT_uTHInter;
762    psBiasParams->uTHInterQP = TOPAZHP_DEFAULT_uTHInterQP;
763    psBiasParams->uTHInterMaxLevel = TOPAZHP_DEFAULT_uTHInterMaxLevel;
764    psBiasParams->uTHSkipIPE = TOPAZHP_DEFAULT_uTHSkipIPE;
765    psBiasParams->uTHSkipSPE = TOPAZHP_DEFAULT_uTHSkipSPE;
766}
767