18e9a21e730449c10cac6e6f69d255611c93f63c2hding/*
2430ce5c32361119ed81a62c918be6afbdd053eddhding * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3430ce5c32361119ed81a62c918be6afbdd053eddhding * Copyright (c) Imagination Technologies Limited, UK
48e9a21e730449c10cac6e6f69d255611c93f63c2hding *
5430ce5c32361119ed81a62c918be6afbdd053eddhding * Permission is hereby granted, free of charge, to any person obtaining a
6430ce5c32361119ed81a62c918be6afbdd053eddhding * copy of this software and associated documentation files (the
7430ce5c32361119ed81a62c918be6afbdd053eddhding * "Software"), to deal in the Software without restriction, including
8430ce5c32361119ed81a62c918be6afbdd053eddhding * without limitation the rights to use, copy, modify, merge, publish,
9430ce5c32361119ed81a62c918be6afbdd053eddhding * distribute, sub license, and/or sell copies of the Software, and to
10430ce5c32361119ed81a62c918be6afbdd053eddhding * permit persons to whom the Software is furnished to do so, subject to
11430ce5c32361119ed81a62c918be6afbdd053eddhding * the following conditions:
12430ce5c32361119ed81a62c918be6afbdd053eddhding *
13430ce5c32361119ed81a62c918be6afbdd053eddhding * The above copyright notice and this permission notice (including the
14430ce5c32361119ed81a62c918be6afbdd053eddhding * next paragraph) shall be included in all copies or substantial portions
15430ce5c32361119ed81a62c918be6afbdd053eddhding * of the Software.
16430ce5c32361119ed81a62c918be6afbdd053eddhding *
17430ce5c32361119ed81a62c918be6afbdd053eddhding * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18430ce5c32361119ed81a62c918be6afbdd053eddhding * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19430ce5c32361119ed81a62c918be6afbdd053eddhding * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20430ce5c32361119ed81a62c918be6afbdd053eddhding * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21430ce5c32361119ed81a62c918be6afbdd053eddhding * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22430ce5c32361119ed81a62c918be6afbdd053eddhding * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23430ce5c32361119ed81a62c918be6afbdd053eddhding * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
248e9a21e730449c10cac6e6f69d255611c93f63c2hding *
258e9a21e730449c10cac6e6f69d255611c93f63c2hding * Authors:
268e9a21e730449c10cac6e6f69d255611c93f63c2hding *    Elaine Wang <elaine.wang@intel.com>
278e9a21e730449c10cac6e6f69d255611c93f63c2hding *    Zeng Li <zeng.li@intel.com>
288e9a21e730449c10cac6e6f69d255611c93f63c2hding *    Edward Lin <edward.lin@intel.com>
298e9a21e730449c10cac6e6f69d255611c93f63c2hding *    Zhaohan Ren<zhaohan.ren@intel.com>
308e9a21e730449c10cac6e6f69d255611c93f63c2hding *
318e9a21e730449c10cac6e6f69d255611c93f63c2hding */
328e9a21e730449c10cac6e6f69d255611c93f63c2hding
338e9a21e730449c10cac6e6f69d255611c93f63c2hding
348e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <errno.h>
358e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <stdlib.h>
368e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <unistd.h>
378e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <stdint.h>
388e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <string.h>
398e9a21e730449c10cac6e6f69d255611c93f63c2hding
408e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_def.h"
418e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_surface.h"
42430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_cmdbuf.h"
43430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_hostcode.h"
44430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_hostheader.h"
45430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_H263ES.h"
468e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_drv_debug.h"
478e9a21e730449c10cac6e6f69d255611c93f63c2hding
488e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "hwdefs/coreflags.h"
498e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "hwdefs/topaz_vlc_regs.h"
508e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "hwdefs/topaz_db_regs.h"
518e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "hwdefs/topazhp_default_params.h"
528e9a21e730449c10cac6e6f69d255611c93f63c2hding
538e9a21e730449c10cac6e6f69d255611c93f63c2hding#define TOPAZ_H263_MAX_BITRATE 16000000
548e9a21e730449c10cac6e6f69d255611c93f63c2hding
558e9a21e730449c10cac6e6f69d255611c93f63c2hding#define INIT_CONTEXT_H263ES     context_ENC_p ctx = (context_ENC_p) obj_context->format_data
568e9a21e730449c10cac6e6f69d255611c93f63c2hding#define SURFACE(id)    ((object_surface_p) object_heap_lookup( &ctx->obj_context->driver_data->surface_heap, id ))
578e9a21e730449c10cac6e6f69d255611c93f63c2hding#define BUFFER(id)  ((object_buffer_p) object_heap_lookup( &ctx->obj_context->driver_data->buffer_heap, id ))
588e9a21e730449c10cac6e6f69d255611c93f63c2hding
59430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic void tng_H263ES_QueryConfigAttributes(
60cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan    VAProfile __maybe_unused profile,
61cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan    VAEntrypoint __maybe_unused entrypoint,
628e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAConfigAttrib *attrib_list,
638e9a21e730449c10cac6e6f69d255611c93f63c2hding    int num_attribs)
648e9a21e730449c10cac6e6f69d255611c93f63c2hding{
658e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
668e9a21e730449c10cac6e6f69d255611c93f63c2hding
678e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s\n", __FUNCTION__);
688e9a21e730449c10cac6e6f69d255611c93f63c2hding
698e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* RateControl attributes */
708e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < num_attribs; i++) {
718e9a21e730449c10cac6e6f69d255611c93f63c2hding        switch (attrib_list[i].type) {
72cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan            case VAConfigAttribRTFormat:
73cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan        break;
74c55c648390836eba6e0da72e263a26caa2129f39jiguoliang
75cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan        case VAConfigAttribEncAutoReference:
76cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan            attrib_list[i].value = 1;
77cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan            break;
7835b15a0a6456d1ca2b2c1ccae60e548743175526Ren Zhaohan
79cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan        case VAConfigAttribEncMaxRefFrames:
80cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan            attrib_list[i].value = 2;
8135b15a0a6456d1ca2b2c1ccae60e548743175526Ren Zhaohan            break;
828e9a21e730449c10cac6e6f69d255611c93f63c2hding
83cecb10be5449aa74cd1d9a2eb41c2a6a76d9ee79ywan        case VAConfigAttribRateControl:
84f5f1ed485ec5506202f592b898425f7c11354ed5Ren Zhaohan            attrib_list[i].value = VA_RC_NONE | VA_RC_CBR | VA_RC_VBR;
858e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
868e9a21e730449c10cac6e6f69d255611c93f63c2hding
878e9a21e730449c10cac6e6f69d255611c93f63c2hding        default:
888e9a21e730449c10cac6e6f69d255611c93f63c2hding            attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
898e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
908e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
918e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
928e9a21e730449c10cac6e6f69d255611c93f63c2hding}
938e9a21e730449c10cac6e6f69d255611c93f63c2hding
948e9a21e730449c10cac6e6f69d255611c93f63c2hding
95430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_H263ES_ValidateConfig(
968e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_config_p obj_config)
978e9a21e730449c10cac6e6f69d255611c93f63c2hding{
98d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun    int i;
998e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s\n", __FUNCTION__);
100d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun    /* Check all attributes */
101d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun    for (i = 0; i < obj_config->attrib_count; i++) {
102d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun        switch (obj_config->attrib_list[i].type) {
103d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun        case VAConfigAttribRTFormat:
104d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun            /* Ignore */
105d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun            break;
106d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun        case VAConfigAttribRateControl:
107d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun            break;
108d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun        case VAConfigAttribEncAutoReference:
109d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun            break;
110d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun        case VAConfigAttribEncMaxRefFrames:
111d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun            break;
112d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun        default:
113d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun            return VA_STATUS_ERROR_ATTR_NOT_SUPPORTED;
114d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun        }
115d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun    }
1168e9a21e730449c10cac6e6f69d255611c93f63c2hding
117d51aa094f181d181c94e845950ffeba168c8fe5aLiu, Bolun    return VA_STATUS_SUCCESS;
1188e9a21e730449c10cac6e6f69d255611c93f63c2hding}
1198e9a21e730449c10cac6e6f69d255611c93f63c2hding
120430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_H263ES_CreateContext(
1218e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context,
1228e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_config_p obj_config)
1238e9a21e730449c10cac6e6f69d255611c93f63c2hding{
1248e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
1258e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx;
126f1b4be712bc1e4f29cc1eb85e0b2cd42f5b40841pingshix
1278e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s\n", __FUNCTION__);
1288e9a21e730449c10cac6e6f69d255611c93f63c2hding
129430ce5c32361119ed81a62c918be6afbdd053eddhding    vaStatus = tng_CreateContext(obj_context, obj_config, 0);
1308e9a21e730449c10cac6e6f69d255611c93f63c2hding
1318e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (VA_STATUS_SUCCESS != vaStatus)
1328e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_ALLOCATION_FAILED;
1338e9a21e730449c10cac6e6f69d255611c93f63c2hding
1348e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx = (context_ENC_p) obj_context->format_data;
1358e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->eStandard = IMG_STANDARD_H263;
1368e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->eFormat = IMG_CODEC_PL12;                          // use default
1378e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bNoOffscreenMv = IMG_TRUE; //Default Value ?? Extended Parameter and bUseOffScreenMVUserSetting
1388e9a21e730449c10cac6e6f69d255611c93f63c2hding
1398e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch(ctx->sRCParams.eRCMode) {
1408e9a21e730449c10cac6e6f69d255611c93f63c2hding       case IMG_RCMODE_NONE:
1418e9a21e730449c10cac6e6f69d255611c93f63c2hding           ctx->eCodec = IMG_CODEC_H263_NO_RC;
1428e9a21e730449c10cac6e6f69d255611c93f63c2hding           break;
1438e9a21e730449c10cac6e6f69d255611c93f63c2hding       case IMG_RCMODE_VBR:
1448e9a21e730449c10cac6e6f69d255611c93f63c2hding           ctx->eCodec = IMG_CODEC_H263_VBR;
1458e9a21e730449c10cac6e6f69d255611c93f63c2hding           break;
1468e9a21e730449c10cac6e6f69d255611c93f63c2hding       case IMG_RCMODE_CBR:
1478e9a21e730449c10cac6e6f69d255611c93f63c2hding           ctx->eCodec = IMG_CODEC_H263_CBR;
1488e9a21e730449c10cac6e6f69d255611c93f63c2hding           break;
1498e9a21e730449c10cac6e6f69d255611c93f63c2hding       default:
1508e9a21e730449c10cac6e6f69d255611c93f63c2hding           drv_debug_msg(VIDEO_DEBUG_ERROR, "Unknown RCMode %08x\n", ctx->sRCParams.eRCMode);
1518e9a21e730449c10cac6e6f69d255611c93f63c2hding           break;
1528e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1538e9a21e730449c10cac6e6f69d255611c93f63c2hding
1548e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bIsInterlaced = IMG_FALSE;
1558e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bIsInterleaved = IMG_FALSE;
1568e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui16PictureHeight = ctx->ui16FrameHeight;
1578e9a21e730449c10cac6e6f69d255611c93f63c2hding
1588e9a21e730449c10cac6e6f69d255611c93f63c2hding    //This parameter need not be exposed
1598e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui8InterIntraIndex = 3;
1608e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui8CodedSkippedIndex = 3;
1618e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bEnableHostQP = IMG_FALSE;
1628e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->uMaxChunks = 0xA0;
1638e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->uChunksPerMb = 0x40;
1648e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->uPriorityChunks = (0xA0 - 0x60);
1658e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui32FCode = 4;
1668e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->iFineYSearchSize = 2;
1678e9a21e730449c10cac6e6f69d255611c93f63c2hding
1688e9a21e730449c10cac6e6f69d255611c93f63c2hding    //This parameter need not be exposed
1698e9a21e730449c10cac6e6f69d255611c93f63c2hding    //host to control the encoding process
1708e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bEnableInpCtrl = IMG_FALSE;
1718e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bEnableHostBias = IMG_FALSE;
1728e9a21e730449c10cac6e6f69d255611c93f63c2hding    //By default false Newly Added
1738e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bEnableCumulativeBiases = IMG_FALSE;
1748e9a21e730449c10cac6e6f69d255611c93f63c2hding
1758e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Weighted Prediction is not supported in TopazHP Version 3.0
1768e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bWeightedPrediction = IMG_FALSE;
1778e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui8VPWeightedImplicitBiPred = 0;
1788e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bInsertHRDParams = 0;
1798e9a21e730449c10cac6e6f69d255611c93f63c2hding
1808e9a21e730449c10cac6e6f69d255611c93f63c2hding
1818e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bArbitrarySO = IMG_FALSE;
1828e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui32BasicUnit = 0;
1838e9a21e730449c10cac6e6f69d255611c93f63c2hding
1848e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
1858e9a21e730449c10cac6e6f69d255611c93f63c2hding}
1868e9a21e730449c10cac6e6f69d255611c93f63c2hding
187430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic void tng_H263ES_DestroyContext(
1888e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
1898e9a21e730449c10cac6e6f69d255611c93f63c2hding{
1908e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s\n", __FUNCTION__);
191430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_DestroyContext(obj_context, 0);
1928e9a21e730449c10cac6e6f69d255611c93f63c2hding}
1938e9a21e730449c10cac6e6f69d255611c93f63c2hding
194430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_H263ES_BeginPicture(
1958e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
1968e9a21e730449c10cac6e6f69d255611c93f63c2hding{
1978e9a21e730449c10cac6e6f69d255611c93f63c2hding    INIT_CONTEXT_H263ES;
198430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
1998e9a21e730449c10cac6e6f69d255611c93f63c2hding	context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
2008e9a21e730449c10cac6e6f69d255611c93f63c2hding	VAStatus vaStatus = VA_STATUS_SUCCESS;
2018e9a21e730449c10cac6e6f69d255611c93f63c2hding
2028e9a21e730449c10cac6e6f69d255611c93f63c2hding
2038e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s\n", __FUNCTION__);
204430ce5c32361119ed81a62c918be6afbdd053eddhding    vaStatus = tng_BeginPicture(ctx);
2058e9a21e730449c10cac6e6f69d255611c93f63c2hding
2068e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
2078e9a21e730449c10cac6e6f69d255611c93f63c2hding}
2088e9a21e730449c10cac6e6f69d255611c93f63c2hding
209430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng__H263ES_process_sequence_param(context_ENC_p ctx, object_buffer_p obj_buffer)
2108e9a21e730449c10cac6e6f69d255611c93f63c2hding{
2118e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
2128e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncSequenceParameterBufferH263 *psSeqParams;
213430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
2148e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_RC_PARAMS *psRCParams = &(ctx->sRCParams);
2158e9a21e730449c10cac6e6f69d255611c93f63c2hding//    IMG_UINT32 ClippedPictureHeight;
2168e9a21e730449c10cac6e6f69d255611c93f63c2hding//    IMG_UINT32 ClippedPictureWidth;
2178e9a21e730449c10cac6e6f69d255611c93f63c2hding
2188e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAEncSequenceParameterBufferType);
2198e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->size == sizeof(VAEncSequenceParameterBufferH263));
2208e9a21e730449c10cac6e6f69d255611c93f63c2hding
2218e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (obj_buffer->size != sizeof(VAEncSequenceParameterBufferH263)) {
2228e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
2238e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
2248e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->obj_context->frame_count = 0;
2258e9a21e730449c10cac6e6f69d255611c93f63c2hding    psSeqParams = (VAEncSequenceParameterBufferH263 *) obj_buffer->buffer_data;
2268e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->buffer_data = NULL;
2278e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->size = 0;
2288e9a21e730449c10cac6e6f69d255611c93f63c2hding
2298e9a21e730449c10cac6e6f69d255611c93f63c2hding    /********************************/
2308e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui32IdrPeriod = psSeqParams->intra_period;
2318e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui32IntraCnt = psSeqParams->intra_period;
2324dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan
2334dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan    if (ctx->ui32IntraCnt == 0) {
2344dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan            ctx->ui32IntraCnt = INT_MAX;
2354dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan        ctx->ui32IdrPeriod = 1;
2364dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan        drv_debug_msg(VIDEO_DEBUG_GENERAL,
2374dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan            "%s: only ONE I frame in the sequence, %d\n",
2384dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan            __FUNCTION__, ctx->ui32IdrPeriod);
2394dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan    }
2404dd34bf685c54fbeab2dcfd01dd76cc826465219Ren Zhaohan
2418e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bCustomScaling = IMG_FALSE;
2428e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bUseDefaultScalingList = IMG_FALSE;
2438e9a21e730449c10cac6e6f69d255611c93f63c2hding
2448e9a21e730449c10cac6e6f69d255611c93f63c2hding
2458e9a21e730449c10cac6e6f69d255611c93f63c2hding    //set MV limit infor
2468e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui32VertMVLimit = 255 ;//(63.75 in qpel increments)
2478e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bLimitNumVectors = IMG_TRUE;
2488e9a21e730449c10cac6e6f69d255611c93f63c2hding
2498e9a21e730449c10cac6e6f69d255611c93f63c2hding    /**************set rc params ****************/
2508e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (psSeqParams->bits_per_second > TOPAZ_H263_MAX_BITRATE) {
2518e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->sRCParams.ui32BitsPerSecond = TOPAZ_H263_MAX_BITRATE;
2528e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, " bits_per_second(%d) exceeds \
2538e9a21e730449c10cac6e6f69d255611c93f63c2hding		the maximum bitrate, set it with %d\n",
2548e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 psSeqParams->bits_per_second,
2558e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 TOPAZ_H263_MAX_BITRATE);
2568e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else
2578e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->sRCParams.ui32BitsPerSecond = psSeqParams->bits_per_second;
2588e9a21e730449c10cac6e6f69d255611c93f63c2hding
2598e9a21e730449c10cac6e6f69d255611c93f63c2hding    //FIXME: Zhaohan, this should be figured out in testsuite?
2608e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (!ctx->uiCbrBufferTenths)
2618e9a21e730449c10cac6e6f69d255611c93f63c2hding	ctx->uiCbrBufferTenths = TOPAZHP_DEFAULT_uiCbrBufferTenths;
2628e9a21e730449c10cac6e6f69d255611c93f63c2hding
2638e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ctx->uiCbrBufferTenths) {
2648e9a21e730449c10cac6e6f69d255611c93f63c2hding        psRCParams->ui32BufferSize      = (IMG_UINT32)(psRCParams->ui32BitsPerSecond * ctx->uiCbrBufferTenths / 10.0);
2658e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else {
2668e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (psRCParams->ui32BitsPerSecond < 256000)
2678e9a21e730449c10cac6e6f69d255611c93f63c2hding            psRCParams->ui32BufferSize = ((9 * psRCParams->ui32BitsPerSecond) >> 1);
2688e9a21e730449c10cac6e6f69d255611c93f63c2hding        else
2698e9a21e730449c10cac6e6f69d255611c93f63c2hding            psRCParams->ui32BufferSize = ((5 * psRCParams->ui32BitsPerSecond) >> 1);
2708e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
2718e9a21e730449c10cac6e6f69d255611c93f63c2hding
2728e9a21e730449c10cac6e6f69d255611c93f63c2hding    psRCParams->i32InitialDelay = (13 * psRCParams->ui32BufferSize) >> 4;
2738e9a21e730449c10cac6e6f69d255611c93f63c2hding    psRCParams->i32InitialLevel = (3 * psRCParams->ui32BufferSize) >> 4;
2748e9a21e730449c10cac6e6f69d255611c93f63c2hding    psRCParams->ui32IntraFreq = psSeqParams->intra_period;
2758e9a21e730449c10cac6e6f69d255611c93f63c2hding    psRCParams->ui32InitialQp = psSeqParams->initial_qp;
2768e9a21e730449c10cac6e6f69d255611c93f63c2hding    psRCParams->iMinQP = psSeqParams->min_qp;
2778e9a21e730449c10cac6e6f69d255611c93f63c2hding    //psRCParams->ui32BUSize = psSeqParams->basic_unit_size;
2788e9a21e730449c10cac6e6f69d255611c93f63c2hding    //ctx->ui32KickSize = psRCParams->ui32BUSize;
2798e9a21e730449c10cac6e6f69d255611c93f63c2hding    psRCParams->ui32FrameRate = psSeqParams->frame_rate;
2808e9a21e730449c10cac6e6f69d255611c93f63c2hding
2818e9a21e730449c10cac6e6f69d255611c93f63c2hding    //B-frames are not supported for non-H.264 streams
2828e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->sRCParams.ui16BFrames = 0;
283e26d82700f9514cc175d2b54733ebdeb8824922ehding    ctx->ui8SlotsInUse = psRCParams->ui16BFrames + 2;
2848e9a21e730449c10cac6e6f69d255611c93f63c2hding
285430ce5c32361119ed81a62c918be6afbdd053eddhding    cmdbuf->cmd_idx_saved[TNG_CMDBUF_SEQ_HEADER_IDX] = cmdbuf->cmd_idx;
2868e9a21e730449c10cac6e6f69d255611c93f63c2hding
2878e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(psSeqParams);
2888e9a21e730449c10cac6e6f69d255611c93f63c2hding
2898e9a21e730449c10cac6e6f69d255611c93f63c2hding    return VA_STATUS_SUCCESS;
2908e9a21e730449c10cac6e6f69d255611c93f63c2hding}
2918e9a21e730449c10cac6e6f69d255611c93f63c2hding
292430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng__H263ES_process_picture_param(context_ENC_p ctx, object_buffer_p obj_buffer)
2938e9a21e730449c10cac6e6f69d255611c93f63c2hding{
2948e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
2958e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
2968e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
2978e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncPictureParameterBufferH263 *psPicParams;
2988e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_BOOL bDepViewPPS = IMG_FALSE;
2998e9a21e730449c10cac6e6f69d255611c93f63c2hding	void* pTmpBuf = NULL;
3008e9a21e730449c10cac6e6f69d255611c93f63c2hding
3018e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: start\n",__FUNCTION__);
3028e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAEncPictureParameterBufferType);
3038e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (obj_buffer->size != sizeof(VAEncPictureParameterBufferH263)) {
3048e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "%s L%d Invalid coded buffer handle\n", __FUNCTION__, __LINE__);
3058e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
3068e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3078e9a21e730449c10cac6e6f69d255611c93f63c2hding
3088e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Transfer ownership of VAEncPictureParameterBufferH263 data */
3098e9a21e730449c10cac6e6f69d255611c93f63c2hding    psPicParams = (VAEncPictureParameterBufferH263 *) obj_buffer->buffer_data;
3108e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->buffer_data = NULL;
3118e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->size = 0;
3128e9a21e730449c10cac6e6f69d255611c93f63c2hding
313be24e0b5b71cb2e30d7798cda8a42e599f84e2f7Ren Zhaohan    /* Save the actual width/height for picture header template */
314be24e0b5b71cb2e30d7798cda8a42e599f84e2f7Ren Zhaohan    ctx->h263_actual_width = psPicParams->picture_width;
315be24e0b5b71cb2e30d7798cda8a42e599f84e2f7Ren Zhaohan    ctx->h263_actual_height = psPicParams->picture_height;
316be24e0b5b71cb2e30d7798cda8a42e599f84e2f7Ren Zhaohan
3178e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(ctx->ui16Width == psPicParams->picture_width);
3188e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(ctx->ui16PictureHeight == psPicParams->picture_height);
3195dded681fc06f6f765840c45a5e6d57cdf79e561edward lin#ifndef _TNG_FRAMES_
3205dded681fc06f6f765840c45a5e6d57cdf79e561edward lin    ps_buf->ref_surface[0] = ps_buf->ref_surface[2] = SURFACE(psPicParams->reference_picture);
3215dded681fc06f6f765840c45a5e6d57cdf79e561edward lin    ps_buf->ref_surface[1] = ps_buf->ref_surface[3] = SURFACE(psPicParams->reconstructed_picture);
3220cb7e3aca829a30af7cbb32df4271b06c906a323Ren Zhaohan
3230cb7e3aca829a30af7cbb32df4271b06c906a323Ren Zhaohan    ps_buf->ref_surface[0]->is_ref_surface = ps_buf->ref_surface[2]->is_ref_surface = 1;
3240cb7e3aca829a30af7cbb32df4271b06c906a323Ren Zhaohan    ps_buf->ref_surface[1]->is_ref_surface = ps_buf->ref_surface[3]->is_ref_surface = 1;
3255dded681fc06f6f765840c45a5e6d57cdf79e561edward lin#else
3268e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_buf->ref_surface = SURFACE(psPicParams->reference_picture);
3278e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_buf->rec_surface = SURFACE(psPicParams->reconstructed_picture);
3285dded681fc06f6f765840c45a5e6d57cdf79e561edward lin#endif
3298e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_buf->coded_buf = BUFFER(psPicParams->coded_buf);
3308e9a21e730449c10cac6e6f69d255611c93f63c2hding
3318e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == ps_buf->coded_buf) {
3328e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "%s L%d Invalid coded buffer handle\n", __FUNCTION__, __LINE__);
3338e9a21e730449c10cac6e6f69d255611c93f63c2hding        free(psPicParams);
3348e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_INVALID_BUFFER;
3358e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3368e9a21e730449c10cac6e6f69d255611c93f63c2hding
3378e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((ctx->ui16Width == 128) && (ctx->ui16FrameHeight == 96))
3388e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->ui8H263SourceFormat = _128x96_SubQCIF;
3398e9a21e730449c10cac6e6f69d255611c93f63c2hding    else if ((ctx->ui16Width == 176) && (ctx->ui16FrameHeight == 144))
3408e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->ui8H263SourceFormat = _176x144_QCIF;
3418e9a21e730449c10cac6e6f69d255611c93f63c2hding    else if ((ctx->ui16Width == 352) && (ctx->ui16FrameHeight == 288))
3428e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->ui8H263SourceFormat = _352x288_CIF;
3438e9a21e730449c10cac6e6f69d255611c93f63c2hding    else if ((ctx->ui16Width == 704) && (ctx->ui16FrameHeight == 576))
3448e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->ui8H263SourceFormat = _704x576_4CIF;
345be24e0b5b71cb2e30d7798cda8a42e599f84e2f7Ren Zhaohan    else if ((ctx->ui16Width <= 2048) && (ctx->ui16FrameHeight <= 1152))
3468e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->ui8H263SourceFormat = 7;
3478e9a21e730449c10cac6e6f69d255611c93f63c2hding    else {
3488e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Unsupported resolution!\n");
3498e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_RESOLUTION_NOT_SUPPORTED;
3508e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3518e9a21e730449c10cac6e6f69d255611c93f63c2hding
3528e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(psPicParams);
3538e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: end\n",__FUNCTION__);
3548e9a21e730449c10cac6e6f69d255611c93f63c2hding
3558e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
3568e9a21e730449c10cac6e6f69d255611c93f63c2hding}
3578e9a21e730449c10cac6e6f69d255611c93f63c2hding
358430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng__H263ES_process_slice_param(context_ENC_p ctx, object_buffer_p obj_buffer)
3598e9a21e730449c10cac6e6f69d255611c93f63c2hding{
3608e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
3618e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncSliceParameterBuffer *psSliceParams;
3628e9a21e730449c10cac6e6f69d255611c93f63c2hding
3638e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAEncSliceParameterBufferType);
3648e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Prepare InParams for macros of current slice, insert slice header, insert do slice command */
3658e9a21e730449c10cac6e6f69d255611c93f63c2hding
3668e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Transfer ownership of VAEncPictureParameterBufferH263 data */
3678e9a21e730449c10cac6e6f69d255611c93f63c2hding    psSliceParams = (VAEncSliceParameterBuffer*) obj_buffer->buffer_data;
3688e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->size = 0;
3698e9a21e730449c10cac6e6f69d255611c93f63c2hding
3708e9a21e730449c10cac6e6f69d255611c93f63c2hding    //deblocking behaviour
3718e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->bArbitrarySO = IMG_FALSE;
3728e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->ui8DeblockIDC = psSliceParams->slice_flags.bits.disable_deblocking_filter_idc;
3738e9a21e730449c10cac6e6f69d255611c93f63c2hding    ++ctx->ui8SlicesPerPicture;
3748e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
3758e9a21e730449c10cac6e6f69d255611c93f63c2hding}
3768e9a21e730449c10cac6e6f69d255611c93f63c2hding
377430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng__H263ES_process_misc_param(context_ENC_p ctx, object_buffer_p obj_buffer)
3788e9a21e730449c10cac6e6f69d255611c93f63c2hding{
3798e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
3808e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterBuffer *pBuffer;
3818e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterFrameRate *frame_rate_param;
3828e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterRateControl *rate_control_param;
3838e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_RC_PARAMS   *psRCParams = &(ctx->sRCParams);
3848e9a21e730449c10cac6e6f69d255611c93f63c2hding
3858e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAEncMiscParameterBufferType);
3868e9a21e730449c10cac6e6f69d255611c93f63c2hding
3878e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Transfer ownership of VAEncMiscParameterBuffer data */
3888e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBuffer = (VAEncMiscParameterBuffer *) obj_buffer->buffer_data;
3898e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->size = 0;
3908e9a21e730449c10cac6e6f69d255611c93f63c2hding
3918e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pBuffer->type) {
3928e9a21e730449c10cac6e6f69d255611c93f63c2hding    case VAEncMiscParameterTypeRateControl:
3938e9a21e730449c10cac6e6f69d255611c93f63c2hding        rate_control_param = (VAEncMiscParameterRateControl *)pBuffer->data;
3948e9a21e730449c10cac6e6f69d255611c93f63c2hding
3958e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->initial_qp > 51 || rate_control_param->min_qp > 51) {
3968e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_ERROR, "Initial_qp(%d) and min_qpinitial_qp(%d) "
3978e9a21e730449c10cac6e6f69d255611c93f63c2hding                               "are invalid.\nQP shouldn't be larger than 51 for H263\n",
3988e9a21e730449c10cac6e6f69d255611c93f63c2hding                               rate_control_param->initial_qp, rate_control_param->min_qp);
3998e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
4008e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
4018e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
4028e9a21e730449c10cac6e6f69d255611c93f63c2hding
4038e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "rate control changed from %d to %d\n",
4048e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 psRCParams->ui32BitsPerSecond,
4058e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 rate_control_param->bits_per_second);
4068e9a21e730449c10cac6e6f69d255611c93f63c2hding
4078e9a21e730449c10cac6e6f69d255611c93f63c2hding        if ((rate_control_param->bits_per_second == psRCParams->ui32BitsPerSecond) &&
4088e9a21e730449c10cac6e6f69d255611c93f63c2hding            (psRCParams->ui32BufferSize == psRCParams->ui32BitsPerSecond / 1000 * rate_control_param->window_size) &&
4098e9a21e730449c10cac6e6f69d255611c93f63c2hding            (psRCParams->iMinQP == rate_control_param->min_qp) &&
4108e9a21e730449c10cac6e6f69d255611c93f63c2hding            (psRCParams->ui32InitialQp == rate_control_param->initial_qp))
4118e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
4128e9a21e730449c10cac6e6f69d255611c93f63c2hding
4138e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->bits_per_second > TOPAZ_H263_MAX_BITRATE) {
4148e9a21e730449c10cac6e6f69d255611c93f63c2hding            psRCParams->ui32BitsPerSecond = TOPAZ_H263_MAX_BITRATE;
4158e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_GENERAL, " bits_per_second(%d) exceeds \
4168e9a21e730449c10cac6e6f69d255611c93f63c2hding				the maximum bitrate, set it with %d\n",
4178e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     rate_control_param->bits_per_second,
4188e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     TOPAZ_H263_MAX_BITRATE);
4198e9a21e730449c10cac6e6f69d255611c93f63c2hding        } else
4208e9a21e730449c10cac6e6f69d255611c93f63c2hding            psRCParams->ui32BitsPerSecond = rate_control_param->bits_per_second;
4218e9a21e730449c10cac6e6f69d255611c93f63c2hding
4228e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->window_size != 0)
4238e9a21e730449c10cac6e6f69d255611c93f63c2hding            psRCParams->ui32BufferSize = psRCParams->ui32BitsPerSecond * rate_control_param->window_size / 1000;
4248e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->initial_qp != 0)
4258e9a21e730449c10cac6e6f69d255611c93f63c2hding            psRCParams->ui32InitialQp = rate_control_param->initial_qp;
4268e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->min_qp != 0)
4278e9a21e730449c10cac6e6f69d255611c93f63c2hding            psRCParams->iMinQP = rate_control_param->min_qp;
4288e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
4298e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
4308e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
4318e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
4328e9a21e730449c10cac6e6f69d255611c93f63c2hding#if 0
4338e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Prepare InParams for macros of current slice, insert slice header, insert do slice command */
4348e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterBuffer *pBuffer;
4358e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterRateControl *rate_control_param;
4368e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterAIR *air_param;
4378e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterMaxSliceSize *max_slice_size_param;
4388e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncMiscParameterFrameRate *frame_rate_param;
4398e9a21e730449c10cac6e6f69d255611c93f63c2hding
4408e9a21e730449c10cac6e6f69d255611c93f63c2hding
4418e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAEncMiscParameterBufferType);
4428e9a21e730449c10cac6e6f69d255611c93f63c2hding
4438e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Transfer ownership of VAEncMiscParameterBuffer data */
4448e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBuffer = (VAEncMiscParameterBuffer *) obj_buffer->buffer_data;
4458e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->size = 0;
4468e9a21e730449c10cac6e6f69d255611c93f63c2hding
4478e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pBuffer->type) {
4488e9a21e730449c10cac6e6f69d255611c93f63c2hding    case VAEncMiscParameterTypeFrameRate:
4498e9a21e730449c10cac6e6f69d255611c93f63c2hding        frame_rate_param = (VAEncMiscParameterFrameRate *)pBuffer->data;
4508e9a21e730449c10cac6e6f69d255611c93f63c2hding
4518e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (frame_rate_param->framerate > 65535) {
4528e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
4538e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
4548e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
4558e9a21e730449c10cac6e6f69d255611c93f63c2hding
4568e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (ctx->sRCParams.FrameRate == frame_rate_param->framerate)
4578e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
4588e9a21e730449c10cac6e6f69d255611c93f63c2hding
4598e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "frame rate changed from %d to %d\n",
4608e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 ctx->sRCParams.FrameRate,
4618e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 frame_rate_param->framerate);
4628e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->sRCParams.FrameRate = frame_rate_param->framerate;
4638e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->sRCParams.bBitrateChanged = IMG_TRUE;
4648e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
4658e9a21e730449c10cac6e6f69d255611c93f63c2hding
4668e9a21e730449c10cac6e6f69d255611c93f63c2hding    case VAEncMiscParameterTypeRateControl:
4678e9a21e730449c10cac6e6f69d255611c93f63c2hding        rate_control_param = (VAEncMiscParameterRateControl *)pBuffer->data;
4688e9a21e730449c10cac6e6f69d255611c93f63c2hding
4698e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->initial_qp > 51 ||
4708e9a21e730449c10cac6e6f69d255611c93f63c2hding            rate_control_param->min_qp > 51) {
4718e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_ERROR, "Initial_qp(%d) and min_qpinitial_qp(%d) "
4728e9a21e730449c10cac6e6f69d255611c93f63c2hding                               "are invalid.\nQP shouldn't be larger than 51 for H264\n",
4738e9a21e730449c10cac6e6f69d255611c93f63c2hding                               rate_control_param->initial_qp, rate_control_param->min_qp);
4748e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
4758e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
4768e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
4778e9a21e730449c10cac6e6f69d255611c93f63c2hding
4788e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "rate control changed from %d to %d\n",
4798e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 ctx->sRCParams.ui32BitsPerSecond,
4808e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 rate_control_param->bits_per_second);
4818e9a21e730449c10cac6e6f69d255611c93f63c2hding
4828e9a21e730449c10cac6e6f69d255611c93f63c2hding        if ((rate_control_param->bits_per_second == ctx->sRCParams.ui32BitsPerSecond) &&
4838e9a21e730449c10cac6e6f69d255611c93f63c2hding            (ctx->sRCParams.ui32BufferSize == ctx->sRCParams.ui32BitsPerSecond / 1000 * rate_control_param->window_size) &&
4848e9a21e730449c10cac6e6f69d255611c93f63c2hding            (ctx->sRCParams.iMinQP == rate_control_param->min_qp) &&
4858e9a21e730449c10cac6e6f69d255611c93f63c2hding            (ctx->sRCParams.ui32InitialQp == rate_control_param->initial_qp))
4868e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
4878e9a21e730449c10cac6e6f69d255611c93f63c2hding        else
4888e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->sRCParams.bBitrateChanged = IMG_TRUE;
4898e9a21e730449c10cac6e6f69d255611c93f63c2hding
4908e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->bits_per_second > TOPAZ_H264_MAX_BITRATE) {
4918e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->sRCParams.ui32BitsPerSecond = TOPAZ_H264_MAX_BITRATE;
4928e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_GENERAL, " bits_per_second(%d) exceeds \
4938e9a21e730449c10cac6e6f69d255611c93f63c2hding			the maximum bitrate, set it with %d\n",
4948e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     rate_control_param->bits_per_second,
4958e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     TOPAZ_H264_MAX_BITRATE);
4968e9a21e730449c10cac6e6f69d255611c93f63c2hding        } else
4978e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->sRCParams.ui32BitsPerSecond = rate_control_param->bits_per_second;
4988e9a21e730449c10cac6e6f69d255611c93f63c2hding
4998e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->window_size != 0)
5008e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->sRCParams.ui32BufferSize = ctx->sRCParams.ui32BitsPerSecond * rate_control_param->window_size / 1000;
5018e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->initial_qp != 0)
5028e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->sRCParams.ui32InitialQp = rate_control_param->initial_qp;
5038e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rate_control_param->min_qp != 0)
5048e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->sRCParams.iMinQP = rate_control_param->min_qp;
5058e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5068e9a21e730449c10cac6e6f69d255611c93f63c2hding
5078e9a21e730449c10cac6e6f69d255611c93f63c2hding    case VAEncMiscParameterTypeMaxSliceSize:
5088e9a21e730449c10cac6e6f69d255611c93f63c2hding        max_slice_size_param = (VAEncMiscParameterMaxSliceSize *)pBuffer->data;
5098e9a21e730449c10cac6e6f69d255611c93f63c2hding
5108e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (ctx->max_slice_size == max_slice_size_param->max_slice_size)
5118e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
5128e9a21e730449c10cac6e6f69d255611c93f63c2hding
5138e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "max slice size changed to %d\n",
5148e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 max_slice_size_param->max_slice_size);
5158e9a21e730449c10cac6e6f69d255611c93f63c2hding
5168e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->max_slice_size = max_slice_size_param->max_slice_size;
5178e9a21e730449c10cac6e6f69d255611c93f63c2hding
5188e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5198e9a21e730449c10cac6e6f69d255611c93f63c2hding
5208e9a21e730449c10cac6e6f69d255611c93f63c2hding    case VAEncMiscParameterTypeAIR:
5218e9a21e730449c10cac6e6f69d255611c93f63c2hding        air_param = (VAEncMiscParameterAIR *)pBuffer->data;
5228e9a21e730449c10cac6e6f69d255611c93f63c2hding
5238e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (air_param->air_num_mbs > 65535 ||
5248e9a21e730449c10cac6e6f69d255611c93f63c2hding            air_param->air_threshold > 65535) {
5258e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_INVALID_PARAMETER;
5268e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
5278e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
5288e9a21e730449c10cac6e6f69d255611c93f63c2hding
5298e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "air slice size changed to num_air_mbs %d "
5308e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 "air_threshold %d, air_auto %d\n",
5318e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 air_param->air_num_mbs, air_param->air_threshold,
5328e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 air_param->air_auto);
5338e9a21e730449c10cac6e6f69d255611c93f63c2hding
5348e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (((ctx->ui16PictureHeight * ctx->ui16Width) >> 8) < air_param->air_num_mbs)
5358e9a21e730449c10cac6e6f69d255611c93f63c2hding            air_param->air_num_mbs = ((ctx->ui16PictureHeight * ctx->ui16Width) >> 8);
5368e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (air_param->air_threshold == 0)
5378e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: air threshold is set to zero\n",
5388e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     __func__);
5398e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->num_air_mbs = air_param->air_num_mbs;
5408e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->air_threshold = air_param->air_threshold;
5418e9a21e730449c10cac6e6f69d255611c93f63c2hding        //ctx->autotune_air_flag = air_param->air_auto;
5428e9a21e730449c10cac6e6f69d255611c93f63c2hding
5438e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5448e9a21e730449c10cac6e6f69d255611c93f63c2hding
5458e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
5468e9a21e730449c10cac6e6f69d255611c93f63c2hding        vaStatus = VA_STATUS_ERROR_UNKNOWN;
5478e9a21e730449c10cac6e6f69d255611c93f63c2hding        DEBUG_FAILURE;
5488e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5498e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
5508e9a21e730449c10cac6e6f69d255611c93f63c2hding
5518e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(obj_buffer->buffer_data);
5528e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->buffer_data = NULL;
5538e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
5548e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
5558e9a21e730449c10cac6e6f69d255611c93f63c2hding}
5568e9a21e730449c10cac6e6f69d255611c93f63c2hding
5578e9a21e730449c10cac6e6f69d255611c93f63c2hding
5588e9a21e730449c10cac6e6f69d255611c93f63c2hding
559430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_H263ES_RenderPicture(
5608e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context,
5618e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_buffer_p *buffers,
5628e9a21e730449c10cac6e6f69d255611c93f63c2hding    int num_buffers)
5638e9a21e730449c10cac6e6f69d255611c93f63c2hding{
5648e9a21e730449c10cac6e6f69d255611c93f63c2hding    INIT_CONTEXT_H263ES;
5658e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
5668e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
5678e9a21e730449c10cac6e6f69d255611c93f63c2hding
568430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_H263ES_RenderPicture\n");
5698e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < num_buffers; i++) {
5708e9a21e730449c10cac6e6f69d255611c93f63c2hding        object_buffer_p obj_buffer = buffers[i];
5718e9a21e730449c10cac6e6f69d255611c93f63c2hding
5728e9a21e730449c10cac6e6f69d255611c93f63c2hding        switch (obj_buffer->type) {
5738e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAEncSequenceParameterBufferType:
574430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_H263_RenderPicture got VAEncSequenceParameterBufferType\n");
575430ce5c32361119ed81a62c918be6afbdd053eddhding            vaStatus = tng__H263ES_process_sequence_param(ctx, obj_buffer);
5768e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
5778e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
5788e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAEncPictureParameterBufferType:
579430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_H263_RenderPicture got VAEncPictureParameterBuffer\n");
580430ce5c32361119ed81a62c918be6afbdd053eddhding            vaStatus = tng__H263ES_process_picture_param(ctx, obj_buffer);
5818e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
5828e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
5838e9a21e730449c10cac6e6f69d255611c93f63c2hding
5848e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAEncSliceParameterBufferType:
585430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_H263_RenderPicture got VAEncSliceParameterBufferType\n");
586430ce5c32361119ed81a62c918be6afbdd053eddhding            vaStatus = tng__H263ES_process_slice_param(ctx, obj_buffer);
5878e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
5888e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
5898e9a21e730449c10cac6e6f69d255611c93f63c2hding
5908e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAEncMiscParameterBufferType:
591430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_H263_RenderPicture got VAEncMiscParameterBufferType\n");
592430ce5c32361119ed81a62c918be6afbdd053eddhding            vaStatus = tng__H263ES_process_misc_param(ctx, obj_buffer);
5938e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
5948e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
5958e9a21e730449c10cac6e6f69d255611c93f63c2hding        default:
5968e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_UNKNOWN;
5978e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
5988e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
5998e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (vaStatus != VA_STATUS_SUCCESS) {
6008e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
6018e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6028e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6038e9a21e730449c10cac6e6f69d255611c93f63c2hding
6048e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
6058e9a21e730449c10cac6e6f69d255611c93f63c2hding}
6068e9a21e730449c10cac6e6f69d255611c93f63c2hding
607430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_H263ES_EndPicture(
6088e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
6098e9a21e730449c10cac6e6f69d255611c93f63c2hding{
6108e9a21e730449c10cac6e6f69d255611c93f63c2hding    INIT_CONTEXT_H263ES;
6118e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
6128e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s start\n", __FUNCTION__);
613430ce5c32361119ed81a62c918be6afbdd053eddhding    vaStatus = tng_EndPicture(ctx);
6148e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s end\n", __FUNCTION__);
6158e9a21e730449c10cac6e6f69d255611c93f63c2hding
6168e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
6178e9a21e730449c10cac6e6f69d255611c93f63c2hding}
6188e9a21e730449c10cac6e6f69d255611c93f63c2hding
619430ce5c32361119ed81a62c918be6afbdd053eddhdingstruct format_vtable_s tng_H263ES_vtable = {
6208e9a21e730449c10cac6e6f69d255611c93f63c2hdingqueryConfigAttributes:
621430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_H263ES_QueryConfigAttributes,
6228e9a21e730449c10cac6e6f69d255611c93f63c2hdingvalidateConfig:
623430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_H263ES_ValidateConfig,
6248e9a21e730449c10cac6e6f69d255611c93f63c2hdingcreateContext:
625430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_H263ES_CreateContext,
6268e9a21e730449c10cac6e6f69d255611c93f63c2hdingdestroyContext:
627430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_H263ES_DestroyContext,
6288e9a21e730449c10cac6e6f69d255611c93f63c2hdingbeginPicture:
629430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_H263ES_BeginPicture,
6308e9a21e730449c10cac6e6f69d255611c93f63c2hdingrenderPicture:
631430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_H263ES_RenderPicture,
6328e9a21e730449c10cac6e6f69d255611c93f63c2hdingendPicture:
633430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_H263ES_EndPicture
6348e9a21e730449c10cac6e6f69d255611c93f63c2hding};
6358e9a21e730449c10cac6e6f69d255611c93f63c2hding
6368e9a21e730449c10cac6e6f69d255611c93f63c2hding/*EOF*/
637