tng_jpegES.c revision 8153ccc65f30ec7cecee4c6a4f0e72f7c402edbd
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 *
288e9a21e730449c10cac6e6f69d255611c93f63c2hding */
298e9a21e730449c10cac6e6f69d255611c93f63c2hding
308e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <stdlib.h>
318e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <stdint.h>
328e9a21e730449c10cac6e6f69d255611c93f63c2hding#include <string.h>
338e9a21e730449c10cac6e6f69d255611c93f63c2hding
348e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_def.h"
358e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_drv_debug.h"
368e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_surface.h"
378e9a21e730449c10cac6e6f69d255611c93f63c2hding#include "psb_cmdbuf.h"
38430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_hostcode.h"
39430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_hostheader.h"
40430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_jpegES.h"
418e9a21e730449c10cac6e6f69d255611c93f63c2hding#ifdef _TOPAZHP_PDUMP_
42430ce5c32361119ed81a62c918be6afbdd053eddhding#include "tng_trace.h"
438e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
448e9a21e730449c10cac6e6f69d255611c93f63c2hding
45430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic void tng__trace_cmdbuf(tng_cmdbuf_p cmdbuf)
468e9a21e730449c10cac6e6f69d255611c93f63c2hding{
478e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
488e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32CmdTmp[4];
498e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 *ptmp = (IMG_UINT32 *)(cmdbuf->cmd_start);
508e9a21e730449c10cac6e6f69d255611c93f63c2hding
518e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: start\n", __FUNCTION__);
528e9a21e730449c10cac6e6f69d255611c93f63c2hding
538e9a21e730449c10cac6e6f69d255611c93f63c2hding    //skip the newcodec
548e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (*ptmp != MTX_CMDID_SW_NEW_CODEC) {
558e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: error new coded\n", __FUNCTION__);
568e9a21e730449c10cac6e6f69d255611c93f63c2hding        return ;
578e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
58e26d82700f9514cc175d2b54733ebdeb8824922ehding    ptmp += 6;
598e9a21e730449c10cac6e6f69d255611c93f63c2hding
608e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((*ptmp & 0xf) != MTX_CMDID_SETUP_INTERFACE) {
618e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: error setup interface\n", __FUNCTION__);
628e9a21e730449c10cac6e6f69d255611c93f63c2hding        return ;
638e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
648e9a21e730449c10cac6e6f69d255611c93f63c2hding
658e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32CmdTmp[0] = *ptmp++;
668e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32CmdTmp[1] = *ptmp++;
678e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32CmdTmp[2] = *ptmp++;
688e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32CmdTmp[3] = 0;
698e9a21e730449c10cac6e6f69d255611c93f63c2hding#ifdef _TOPAZHP_PDUMP_
708e9a21e730449c10cac6e6f69d255611c93f63c2hding    topazhp_dump_command((unsigned int*)ui32CmdTmp);
718e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
728e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < 3; i++) {
738e9a21e730449c10cac6e6f69d255611c93f63c2hding        ui32CmdTmp[0] = *ptmp++;
748e9a21e730449c10cac6e6f69d255611c93f63c2hding        ui32CmdTmp[1] = *ptmp++;
758e9a21e730449c10cac6e6f69d255611c93f63c2hding        ui32CmdTmp[2] = 0;
768e9a21e730449c10cac6e6f69d255611c93f63c2hding        ui32CmdTmp[3] = 0;
778e9a21e730449c10cac6e6f69d255611c93f63c2hding#ifdef _TOPAZHP_PDUMP_
788e9a21e730449c10cac6e6f69d255611c93f63c2hding        topazhp_dump_command((unsigned int*)ui32CmdTmp);
798e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
808e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
818e9a21e730449c10cac6e6f69d255611c93f63c2hding
828e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "%s: end\n", __FUNCTION__);
838e9a21e730449c10cac6e6f69d255611c93f63c2hding
848e9a21e730449c10cac6e6f69d255611c93f63c2hding    return ;
858e9a21e730449c10cac6e6f69d255611c93f63c2hding}
868e9a21e730449c10cac6e6f69d255611c93f63c2hding
878e9a21e730449c10cac6e6f69d255611c93f63c2hding#define SURFACE(id)    ((object_surface_p) object_heap_lookup( &ctx->obj_context->driver_data->surface_heap, id ))
888e9a21e730449c10cac6e6f69d255611c93f63c2hding#define BUFFER(id)  ((object_buffer_p) object_heap_lookup( &ctx->obj_context->driver_data->buffer_heap, id ))
898e9a21e730449c10cac6e6f69d255611c93f63c2hding
908153ccc65f30ec7cecee4c6a4f0e72f7c402edbdSUN,Jing#define PTG_JPEG_MAX_MCU_PER_SCAN (0x4000)
918e9a21e730449c10cac6e6f69d255611c93f63c2hding#define PTG_JPEG_HEADER_MAX_SIZE (1024)
928e9a21e730449c10cac6e6f69d255611c93f63c2hding
938e9a21e730449c10cac6e6f69d255611c93f63c2hding
948e9a21e730449c10cac6e6f69d255611c93f63c2hding#define C_INTERLEAVE 1
958e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LC_YUYVINTERLEAVE 2
968e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LC_YVYUINTERLEAVE 3
978e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LC_UYVYINTERLEAVE 4
988e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LC_VYUYINTERLEAVE 5
998e9a21e730449c10cac6e6f69d255611c93f63c2hding
1008e9a21e730449c10cac6e6f69d255611c93f63c2hding#define ISCHROMAINTERLEAVED(eSurfaceFormat) ((IMG_UINT)(eSurfaceFormat==IMG_CODEC_PL12) * C_INTERLEAVE)
1018e9a21e730449c10cac6e6f69d255611c93f63c2hding
1028e9a21e730449c10cac6e6f69d255611c93f63c2hding
1038e9a21e730449c10cac6e6f69d255611c93f63c2hding/******************************************************************************
1048e9a21e730449c10cac6e6f69d255611c93f63c2hdingGeneral definitions
1058e9a21e730449c10cac6e6f69d255611c93f63c2hding******************************************************************************/
1068e9a21e730449c10cac6e6f69d255611c93f63c2hding#define BYTE                            8
1078e9a21e730449c10cac6e6f69d255611c93f63c2hding#define BYTES_IN_INT                    4
1088e9a21e730449c10cac6e6f69d255611c93f63c2hding#define BITS_IN_INT                     32
1098e9a21e730449c10cac6e6f69d255611c93f63c2hding#define BLOCK_SIZE                      8
1108e9a21e730449c10cac6e6f69d255611c93f63c2hding#define PELS_IN_BLOCK                   64
1118e9a21e730449c10cac6e6f69d255611c93f63c2hding
1128e9a21e730449c10cac6e6f69d255611c93f63c2hding/******************************************************************************
1138e9a21e730449c10cac6e6f69d255611c93f63c2hdingJPEG marker definitions
1148e9a21e730449c10cac6e6f69d255611c93f63c2hding******************************************************************************/
1158e9a21e730449c10cac6e6f69d255611c93f63c2hding#define START_OF_IMAGE              0xFFD8
1168e9a21e730449c10cac6e6f69d255611c93f63c2hding#define SOF_BASELINE_DCT            0xFFC0
1178e9a21e730449c10cac6e6f69d255611c93f63c2hding#define END_OF_IMAGE                0xFFD9
1188e9a21e730449c10cac6e6f69d255611c93f63c2hding#define START_OF_SCAN               0xFFDA
1198e9a21e730449c10cac6e6f69d255611c93f63c2hding
1208e9a21e730449c10cac6e6f69d255611c93f63c2hding/* Definitions for the huffman table specification in the Marker segment */
1218e9a21e730449c10cac6e6f69d255611c93f63c2hding#define DHT_MARKER                  0xFFC4
1228e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LH_DC                       0x001F
1238e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LH_AC                       0x00B5
1248e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LEVEL_SHIFT                 128
1258e9a21e730449c10cac6e6f69d255611c93f63c2hding
1268e9a21e730449c10cac6e6f69d255611c93f63c2hding/* Definitions for the quantization table specification in the Marker segment */
1278e9a21e730449c10cac6e6f69d255611c93f63c2hding#define DQT_MARKER                  0xFFDB
1288e9a21e730449c10cac6e6f69d255611c93f63c2hding#define ACMAX                       0x03FF
1298e9a21e730449c10cac6e6f69d255611c93f63c2hding#define DCMAX                       0x07FF
1308e9a21e730449c10cac6e6f69d255611c93f63c2hding/* Length and precision of the quantization table parameters */
1318e9a21e730449c10cac6e6f69d255611c93f63c2hding#define LQPQ                        0x00430
1328e9a21e730449c10cac6e6f69d255611c93f63c2hding#define QMAX                        255
1338e9a21e730449c10cac6e6f69d255611c93f63c2hding#define CLIP(Number,Max,Min)    if((Number) > (Max)) (Number) = (Max); \
1348e9a21e730449c10cac6e6f69d255611c93f63c2hding                                else if((Number) < (Min)) (Number) = (Min)
1358e9a21e730449c10cac6e6f69d255611c93f63c2hding
1368e9a21e730449c10cac6e6f69d255611c93f63c2hding/////////////////////////////////////////////////////////////////////////////////////
1378e9a21e730449c10cac6e6f69d255611c93f63c2hding// BMP Reading Header Stuff
1388e9a21e730449c10cac6e6f69d255611c93f63c2hding/////////////////////////////////////////////////////////////////////////////////////
1398e9a21e730449c10cac6e6f69d255611c93f63c2hding
1408e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic const IMG_UINT8 gQuantLuma[QUANT_TABLE_SIZE_BYTES] = {
1418e9a21e730449c10cac6e6f69d255611c93f63c2hding    16, 11, 10, 16, 24, 40, 51, 61,
1428e9a21e730449c10cac6e6f69d255611c93f63c2hding    12, 12, 14, 19, 26, 58, 60, 55,
1438e9a21e730449c10cac6e6f69d255611c93f63c2hding    14, 13, 16, 24, 40, 57, 69, 56,
1448e9a21e730449c10cac6e6f69d255611c93f63c2hding    14, 17, 22, 29, 51, 87, 80, 62,
1458e9a21e730449c10cac6e6f69d255611c93f63c2hding    18, 22, 37, 56, 68, 109, 103, 77,
1468e9a21e730449c10cac6e6f69d255611c93f63c2hding    24, 35, 55, 64, 81, 104, 113, 92,
1478e9a21e730449c10cac6e6f69d255611c93f63c2hding    49, 64, 78, 87, 103, 121, 120, 101,
1488e9a21e730449c10cac6e6f69d255611c93f63c2hding    72, 92, 95, 98, 112, 100, 103, 99
1498e9a21e730449c10cac6e6f69d255611c93f63c2hding};
1508e9a21e730449c10cac6e6f69d255611c93f63c2hding
1518e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
1528e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  \brief   gQuantChroma                                                    */
1538e9a21e730449c10cac6e6f69d255611c93f63c2hding/*                                                                           */
1548e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Contains the data that needs to be sent in the marker segment of an      */
1558e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  interchange format JPEG stream or an abbreviated format table            */
1568e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  specification data stream.                                               */
1578e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Quantizer table for the chrominance component                            */
1588e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
1598e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic const IMG_UINT8 gQuantChroma[QUANT_TABLE_SIZE_BYTES] = {
1608e9a21e730449c10cac6e6f69d255611c93f63c2hding    17, 18, 24, 47, 99, 99, 99, 99,
1618e9a21e730449c10cac6e6f69d255611c93f63c2hding    18, 21, 26, 66, 99, 99, 99, 99,
1628e9a21e730449c10cac6e6f69d255611c93f63c2hding    24, 26, 56, 99, 99, 99, 99, 99,
1638e9a21e730449c10cac6e6f69d255611c93f63c2hding    47, 66, 99, 99, 99, 99, 99, 99,
1648e9a21e730449c10cac6e6f69d255611c93f63c2hding    99, 99, 99, 99, 99, 99, 99, 99,
1658e9a21e730449c10cac6e6f69d255611c93f63c2hding    99, 99, 99, 99, 99, 99, 99, 99,
1668e9a21e730449c10cac6e6f69d255611c93f63c2hding    99, 99, 99, 99, 99, 99, 99, 99,
1678e9a21e730449c10cac6e6f69d255611c93f63c2hding    99, 99, 99, 99, 99, 99, 99, 99
1688e9a21e730449c10cac6e6f69d255611c93f63c2hding};
1698e9a21e730449c10cac6e6f69d255611c93f63c2hding
1708e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
1718e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  \brief   gZigZag                                                         */
1728e9a21e730449c10cac6e6f69d255611c93f63c2hding/*                                                                           */
1738e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Zigzag scan pattern                                                      */
1748e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
1758e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic const IMG_UINT8 gZigZag[] = {
1768e9a21e730449c10cac6e6f69d255611c93f63c2hding    0,  1,  8, 16,  9,  2,  3, 10,
1778e9a21e730449c10cac6e6f69d255611c93f63c2hding    17, 24, 32, 25, 18, 11,  4,  5,
1788e9a21e730449c10cac6e6f69d255611c93f63c2hding    12, 19, 26, 33, 40, 48, 41, 34,
1798e9a21e730449c10cac6e6f69d255611c93f63c2hding    27, 20, 13,  6,  7, 14, 21, 28,
1808e9a21e730449c10cac6e6f69d255611c93f63c2hding    35, 42, 49, 56, 57, 50, 43, 36,
1818e9a21e730449c10cac6e6f69d255611c93f63c2hding    29, 22, 15, 23, 30, 37, 44, 51,
1828e9a21e730449c10cac6e6f69d255611c93f63c2hding    58, 59, 52, 45, 38, 31, 39, 46,
1838e9a21e730449c10cac6e6f69d255611c93f63c2hding    53, 60, 61, 54, 47, 55, 62, 63
1848e9a21e730449c10cac6e6f69d255611c93f63c2hding};
1858e9a21e730449c10cac6e6f69d255611c93f63c2hding
1868e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
1878e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  \brief   gMarkerDataLumaDc                                               */
1888e9a21e730449c10cac6e6f69d255611c93f63c2hding/*                                                                           */
1898e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Contains the data that needs to be sent in the marker segment of an      */
1908e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  interchange format JPEG stream or an abbreviated format table            */
1918e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  specification data stream.                                               */
1928e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Specifies the huffman table used for encoding the luminance DC           */
1938e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  coefficient differences. The table represents Table K.3 of               */
1948e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  IS0/IEC 10918-1:1994(E)                                                  */
1958e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
1968e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic const IMG_UINT8 gMarkerDataLumaDc[] = {
1978e9a21e730449c10cac6e6f69d255611c93f63c2hding    //TcTh  Li
1988e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
1998e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x00, 0x00, 0x00, 0x00, 0x00,
2008e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Vi
2018e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
2028e9a21e730449c10cac6e6f69d255611c93f63c2hding};
2038e9a21e730449c10cac6e6f69d255611c93f63c2hding
2048e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
2058e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  \brief   gMarkerDataLumaAc                                               */
2068e9a21e730449c10cac6e6f69d255611c93f63c2hding/*                                                                           */
2078e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Contains the data that needs to be sent in the marker segment of an      */
2088e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  interchange format JPEG stream or an abbreviated format table            */
2098e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  specification data stream.                                               */
2108e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Specifies the huffman table used for encoding the luminance AC           */
2118e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  coefficients. The table represents Table K.5 of IS0/IEC 10918-1:1994(E)  */
2128e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
2138e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic const IMG_UINT8 gMarkerDataLumaAc[] = {
2148e9a21e730449c10cac6e6f69d255611c93f63c2hding    // TcTh  Li
2158e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
2168e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x04, 0x00, 0x00, 0x01, 0x7D,
2178e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Vi
2188e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
2198e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08,
2208e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72,
2218e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
2228e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45,
2238e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
2248e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75,
2258e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
2268e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3,
2278e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
2288e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
2298e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
2308e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4,
2318e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA
2328e9a21e730449c10cac6e6f69d255611c93f63c2hding};
2338e9a21e730449c10cac6e6f69d255611c93f63c2hding
2348e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
2358e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  \brief   gMarkerDataChromaDc                                             */
2368e9a21e730449c10cac6e6f69d255611c93f63c2hding/*                                                                           */
2378e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Contains the data that needs to be sent in the marker segment of an      */
2388e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  interchange format JPEG stream or an abbreviated format table            */
2398e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  specification data stream.                                               */
2408e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Specifies the huffman table used for encoding the chrominance DC         */
2418e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  coefficient differences. The table represents Table K.4 of               */
2428e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  IS0/IEC 10918-1:1994(E)                                                  */
2438e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
2448e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic const IMG_UINT8 gMarkerDataChromaDc[] = {
2458e9a21e730449c10cac6e6f69d255611c93f63c2hding    // TcTh Li
2468e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
2478e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x00, 0x00, 0x00, 0x00, 0x00,
2488e9a21e730449c10cac6e6f69d255611c93f63c2hding
2498e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Vi
2508e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
2518e9a21e730449c10cac6e6f69d255611c93f63c2hding};
2528e9a21e730449c10cac6e6f69d255611c93f63c2hding
2538e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
2548e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  \brief   gMarkerDataChromaAc                                             */
2558e9a21e730449c10cac6e6f69d255611c93f63c2hding/*                                                                           */
2568e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Contains the data that needs to be sent in the marker segment of an      */
2578e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  interchange format JPEG stream or an abbreviated format table            */
2588e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  specification data stream.                                               */
2598e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  Specifies the huffman table used for encoding the chrominance AC         */
2608e9a21e730449c10cac6e6f69d255611c93f63c2hding/*  coefficients. The table represents Table K.6 of IS0/IEC 10918-1:1994(E)  */
2618e9a21e730449c10cac6e6f69d255611c93f63c2hding/*****************************************************************************/
2628e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic const IMG_UINT8 gMarkerDataChromaAc[] = {
2638e9a21e730449c10cac6e6f69d255611c93f63c2hding    // TcTh
2648e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04,
2658e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x04, 0x00, 0x01, 0x02, 0x77,
2668e9a21e730449c10cac6e6f69d255611c93f63c2hding
2678e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Vi
2688e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41,
2698e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
2708e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1,
2718e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
2728e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
2738e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
2748e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74,
2758e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
2768e9a21e730449c10cac6e6f69d255611c93f63c2hding    0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
2778e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
2788e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
2798e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
2808e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4,
2818e9a21e730449c10cac6e6f69d255611c93f63c2hding    0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA
2828e9a21e730449c10cac6e6f69d255611c93f63c2hding};
2838e9a21e730449c10cac6e6f69d255611c93f63c2hding
2848e9a21e730449c10cac6e6f69d255611c93f63c2hding
2858e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void SetDefaultQmatix(void *pMemInfoTableBlock)
2868e9a21e730449c10cac6e6f69d255611c93f63c2hding{
2878e9a21e730449c10cac6e6f69d255611c93f63c2hding    JPEG_MTX_QUANT_TABLE *pQTable =  pMemInfoTableBlock;
2888e9a21e730449c10cac6e6f69d255611c93f63c2hding    memcpy(pQTable->aui8LumaQuantParams, gQuantLuma, QUANT_TABLE_SIZE_BYTES);
2898e9a21e730449c10cac6e6f69d255611c93f63c2hding    memcpy(pQTable->aui8ChromaQuantParams, gQuantChroma, QUANT_TABLE_SIZE_BYTES);
2908e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
2918e9a21e730449c10cac6e6f69d255611c93f63c2hding}
2928e9a21e730449c10cac6e6f69d255611c93f63c2hding
2938e9a21e730449c10cac6e6f69d255611c93f63c2hding
2948e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void IssueQmatix(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
2958e9a21e730449c10cac6e6f69d255611c93f63c2hding{
2968e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
2978e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
2988e9a21e730449c10cac6e6f69d255611c93f63c2hding
2998e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Dump MTX setup data for debug */
3008e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(NULL != pJPEGContext->pMemInfoTableBlock);
3018e9a21e730449c10cac6e6f69d255611c93f63c2hding
3028e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Issue Quantization Table data\n");
303c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing    for (i=0; i<128; i+=8) {
304c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing        if (0 == i) {
305c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing            drv_debug_msg(VIDEO_DEBUG_GENERAL, "Table 0:\n");
306c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing        }
307c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing        else if (64 == i) {
308c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing            drv_debug_msg(VIDEO_DEBUG_GENERAL, "Table 1:\n");
309c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing        }
310c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing        drv_debug_msg(VIDEO_DEBUG_GENERAL, "%d %d %d %d %d %d %d %d\n",
311c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i),
312c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i+1),
313c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i+2),
314c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i+3),
315c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i+4),
316c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i+5),
317c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i+6),
318c79caccb602f74ef23c5b82d194bf005a7009551SUN,Jing                      *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p+i+7));
3198e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3208e9a21e730449c10cac6e6f69d255611c93f63c2hding
321381e313cd06b652a08bef36b3e9d44ae096e72b2pingshix    tng_cmdbuf_insert_command(ctx->obj_context,
3228e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
3238e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_SETQUANT,
32488d0460822f8807666406c14662591167eed73a0hding                                      0,
325430ce5c32361119ed81a62c918be6afbdd053eddhding                                      &(ctx->obj_context->tng_cmdbuf->jpeg_pic_params),
3268e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0);
3278e9a21e730449c10cac6e6f69d255611c93f63c2hding}
3288e9a21e730449c10cac6e6f69d255611c93f63c2hding
3298e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void InitializeJpegEncode(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
3308e9a21e730449c10cac6e6f69d255611c93f63c2hding{
3318e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
3328e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16_width;
3338e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16_height;
3348e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32UpperLimit;
3358e9a21e730449c10cac6e6f69d255611c93f63c2hding
3368e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*********************************************************************/
3378e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Determine the horizonal and the vertical sampling frequency of    */
3388e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* each of components in the image                                   */
3398e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*********************************************************************/
3408e9a21e730449c10cac6e6f69d255611c93f63c2hding
3418e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui16_width = ctx->ui16Width;
3428e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui16_height = ctx->ui16FrameHeight; //pTFrame->height isn't the real height of image, because vaCreateSurface has made it aligned with 32
3438e9a21e730449c10cac6e6f69d255611c93f63c2hding
3448e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pJPEGContext->eFormat) {
3458e9a21e730449c10cac6e6f69d255611c93f63c2hding    case IMG_CODEC_PL12:
3468e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
3478e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32WidthBlocks = 16;
3488e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32HeightBlocks = 16;
3498e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32XLimit = ui16_width;
3508e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32YLimit = ui16_height;
3518e9a21e730449c10cac6e6f69d255611c93f63c2hding
3528e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32WidthBlocks = 8;
3538e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32HeightBlocks = 8;
3548e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32XLimit = ui16_width >> 1;
3558e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32YLimit = ui16_height >> 1;
3568e9a21e730449c10cac6e6f69d255611c93f63c2hding
3578e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32WidthBlocks = 8;
3588e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32HeightBlocks = 8;
3598e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32XLimit = ui16_width >> 1;
3608e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32YLimit = ui16_height >> 1;
3618e9a21e730449c10cac6e6f69d255611c93f63c2hding
3628e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
3638e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3648e9a21e730449c10cac6e6f69d255611c93f63c2hding
3658e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (ISCHROMAINTERLEAVED(pJPEGContext->eFormat)) {
3668e9a21e730449c10cac6e6f69d255611c93f63c2hding    case C_INTERLEAVE:
3678e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
3688e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Chroma format is byte interleaved, as the engine runs using planar colour surfaces we need
3698e9a21e730449c10cac6e6f69d255611c93f63c2hding        // to fool the engine into offsetting by 16 instead of 8
3708e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32WidthBlocks +=
3718e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->MCUComponent[2].ui32WidthBlocks;
3728e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32XLimit +=
3738e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->MCUComponent[2].ui32XLimit;
3748e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32XLimit =
3758e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->MCUComponent[2].ui32YLimit =
3768e9a21e730449c10cac6e6f69d255611c93f63c2hding                pJPEGContext->MCUComponent[2].ui32WidthBlocks =
3778e9a21e730449c10cac6e6f69d255611c93f63c2hding                    pJPEGContext->MCUComponent[2].ui32HeightBlocks = 0;
3788e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
3798e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3808e9a21e730449c10cac6e6f69d255611c93f63c2hding
3818e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX =
3828e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->MCUComponent[0].ui32XLimit +
3838e9a21e730449c10cac6e6f69d255611c93f63c2hding         (pJPEGContext->MCUComponent[0].ui32WidthBlocks - 1)) /
3848e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32WidthBlocks;
3858e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY =
3868e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->MCUComponent[0].ui32YLimit +
3878e9a21e730449c10cac6e6f69d255611c93f63c2hding         (pJPEGContext->MCUComponent[0].ui32HeightBlocks - 1)) /
3888e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32HeightBlocks;
3898e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncode =
3908e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX *
3918e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY;
3928e9a21e730449c10cac6e6f69d255611c93f63c2hding
3938e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of X MCUs: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX);
3948e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of Y MCUs: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY);
3958e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of total MCUs: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncode);
3968e9a21e730449c10cac6e6f69d255611c93f63c2hding
3978e9a21e730449c10cac6e6f69d255611c93f63c2hding
3988e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Number of MCUs sent for a scan _must_ lie at the beginning of a line so that the chroma component can't violate the 16 byte DMA start alignment constraint
3998e9a21e730449c10cac6e6f69d255611c93f63c2hding    // (Actual memory alignment for final DMA will have width aligned to 64, so start of line will automatically meet the 16 byte alignment required by the DMA engine)
4008e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan =
4018e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY + (pJPEGContext->NumCores - 1)) / pJPEGContext->NumCores;
4028e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan *=
4038e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX;
4048e9a21e730449c10cac6e6f69d255611c93f63c2hding
4058e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of MCUs per core: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
4068e9a21e730449c10cac6e6f69d255611c93f63c2hding
4078e9a21e730449c10cac6e6f69d255611c93f63c2hding
4088e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Limit the scan size to maximum useable (due to it being used as the 16 bit field for Restart Intervals) = 0xFFFF MCUs
4098e9a21e730449c10cac6e6f69d255611c93f63c2hding    // In reality, worst case allocatable bytes is less than this, something around 0x159739C == 0x4b96 MCUs = 139 x 139 MCUS = 2224 * 2224 pixels, approx.
4108e9a21e730449c10cac6e6f69d255611c93f63c2hding    // We'll give this upper limit some margin for error, and limit our MCUsPerScan to 2000 * 2000 pixels = 125 * 125 MCUS = 0x3D09 MCUS = 0x116F322 bytes (1170 worst case per MCU)
4118e9a21e730449c10cac6e6f69d255611c93f63c2hding    // If more MCUs are required, then the image will be automatically encoded with multiple scans on the same pipes
4128e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32UpperLimit = PTG_JPEG_MAX_MCU_PER_SCAN / pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX;
4138e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32UpperLimit *= pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX;
4148e9a21e730449c10cac6e6f69d255611c93f63c2hding
4158e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan > ui32UpperLimit) {
4168e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Set MCUs to encode per scan to equal maximum limit and then truncate to ensure it lies at the first MCU of a line (to satisfy the 64 byte requirement)
4178e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan = ui32UpperLimit;
4188e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
4198e9a21e730449c10cac6e6f69d255611c93f63c2hding
4208e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of MCUs per scan: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
4218e9a21e730449c10cac6e6f69d255611c93f63c2hding
4228e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Need to set up CB Output slicenumber to equal number of slices required to encode image
4238e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Set current CB scan to maximum scan number (will count down as scans are output)
4248e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui16ScansInImage =
4258e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncode +
4268e9a21e730449c10cac6e6f69d255611c93f63c2hding         (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan - 1)) /
4278e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
4288e9a21e730449c10cac6e6f69d255611c93f63c2hding
4298e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers =
4308e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui16ScansInImage;
4318e9a21e730449c10cac6e6f69d255611c93f63c2hding
4328e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Scans in image: %d\n", pJPEGContext->sScan_Encode_Info.ui16ScansInImage);
4338e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of coded buffers: %d\n", pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers);
4348e9a21e730449c10cac6e6f69d255611c93f63c2hding
4358e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
4368e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4378e9a21e730449c10cac6e6f69d255611c93f63c2hding
4388e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void AssignCodedDataBuffers(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
4398e9a21e730449c10cac6e6f69d255611c93f63c2hding{
4408e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 ui8Loop;
4418e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->ui32SizePerCodedBuffer =
4428e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->jpeg_coded_buf.ui32Size - PTG_JPEG_HEADER_MAX_SIZE) /
4438e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers;
444e26d82700f9514cc175d2b54733ebdeb8824922ehding    pJPEGContext->ui32SizePerCodedBuffer &= ~0xf;
4458e9a21e730449c10cac6e6f69d255611c93f63c2hding
4468e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset((void *)pJPEGContext->sScan_Encode_Info.aBufferTable, 0x0,
4478e9a21e730449c10cac6e6f69d255611c93f63c2hding           sizeof(TOPAZHP_JPEG_BUFFER_INFO)*pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers);
4488e9a21e730449c10cac6e6f69d255611c93f63c2hding
4498e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "jpeg_coded_buf.pMemInfo: 0x%x\n", (unsigned int)(pJPEGContext->jpeg_coded_buf.pMemInfo));
4508e9a21e730449c10cac6e6f69d255611c93f63c2hding
4518e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui8Loop = 0 ; ui8Loop < pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers; ui8Loop++) {
4528e9a21e730449c10cac6e6f69d255611c93f63c2hding        //pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes = DATA_BUFFER_SIZE(pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
4538e9a21e730449c10cac6e6f69d255611c93f63c2hding        //pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes = (pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes+sizeof(BUFFER_HEADER)) + 3 & ~3;
4548e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferUsedBytes = 0;
4558e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].i8PipeNumber = 0; // Indicates buffer is idle
4568e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui16ScanNumber = 0; // Indicates buffer is idle
4578e9a21e730449c10cac6e6f69d255611c93f63c2hding
4588e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo = (void *)
4598e9a21e730449c10cac6e6f69d255611c93f63c2hding                (pJPEGContext->jpeg_coded_buf.pMemInfo + PTG_JPEG_HEADER_MAX_SIZE +
4608e9a21e730449c10cac6e6f69d255611c93f63c2hding                 ui8Loop * pJPEGContext->ui32SizePerCodedBuffer);
4618e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "aBufferTable[%d].pMemInfo: 0x%x\n", ui8Loop,
4628e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 (unsigned int)(pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo));
4638e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
4648e9a21e730449c10cac6e6f69d255611c93f63c2hding
4658e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
4668e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4678e9a21e730449c10cac6e6f69d255611c93f63c2hding
4688e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void SetSetupInterface(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
4698e9a21e730449c10cac6e6f69d255611c93f63c2hding{
4708e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
4718e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
4728e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem_size *ps_mem_size = &(ctx->ctx_mem_size);
4738e9a21e730449c10cac6e6f69d255611c93f63c2hding
47488d0460822f8807666406c14662591167eed73a0hding    tng_cmdbuf_set_phys(pJPEGContext->pMTXWritebackMemory->apWritebackRegions, WB_FIFO_SIZE,
4758e9a21e730449c10cac6e6f69d255611c93f63c2hding                        &(ctx->bufs_writeback), 0, ps_mem_size->writeback);
4768e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4778e9a21e730449c10cac6e6f69d255611c93f63c2hding
4788e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void IssueSetupInterface(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
4798e9a21e730449c10cac6e6f69d255611c93f63c2hding{
48088d0460822f8807666406c14662591167eed73a0hding    int i;
4818e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
4828e9a21e730449c10cac6e6f69d255611c93f63c2hding
4838e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(NULL != pJPEGContext->pMTXSetup);
4848e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Issue SetupInterface\n");
4858e9a21e730449c10cac6e6f69d255611c93f63c2hding
48688d0460822f8807666406c14662591167eed73a0hding    for (i = 0; i < WB_FIFO_SIZE; i++) {
48788d0460822f8807666406c14662591167eed73a0hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "apWritebackRegions[%d]: 0x%x\n", i,
48888d0460822f8807666406c14662591167eed73a0hding                                 pJPEGContext->pMTXWritebackMemory->apWritebackRegions[i]);
48988d0460822f8807666406c14662591167eed73a0hding    }
49088d0460822f8807666406c14662591167eed73a0hding
491381e313cd06b652a08bef36b3e9d44ae096e72b2pingshix    tng_cmdbuf_insert_command(ctx->obj_context,
4928e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
4938e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_SETUP_INTERFACE,
49488d0460822f8807666406c14662591167eed73a0hding                                      0,
495430ce5c32361119ed81a62c918be6afbdd053eddhding                                      &(ctx->obj_context->tng_cmdbuf->jpeg_header_interface_mem),
4968e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0);
4978e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4988e9a21e730449c10cac6e6f69d255611c93f63c2hding
4998e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_ERRORCODE SetMTXSetup(
5008e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
5018e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_surface_p pTFrame)
5028e9a21e730449c10cac6e6f69d255611c93f63c2hding{
5038e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 srf_buf_offset;
5048e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
505430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
5068e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
5078e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem_size *ps_mem_size = &(ctx->ctx_mem_size);
5088e9a21e730449c10cac6e6f69d255611c93f63c2hding
5098e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->pMTXSetup->ui32ComponentsInScan = MTX_MAX_COMPONENTS;
5108e9a21e730449c10cac6e6f69d255611c93f63c2hding
5118e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pJPEGContext->eFormat) {
5128e9a21e730449c10cac6e6f69d255611c93f63c2hding    case IMG_CODEC_PL12:
5138e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
5148e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->pMTXSetup->ComponentPlane[0].ui32Stride = pTFrame->psb_surface->stride;
5158e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->pMTXSetup->ComponentPlane[1].ui32Stride = pTFrame->psb_surface->stride;
5168e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->pMTXSetup->ComponentPlane[2].ui32Stride = pTFrame->psb_surface->stride;
5178e9a21e730449c10cac6e6f69d255611c93f63c2hding
518e26d82700f9514cc175d2b54733ebdeb8824922ehding        pJPEGContext->pMTXSetup->ComponentPlane[0].ui32Height = pJPEGContext->MCUComponent[0].ui32YLimit;
519e26d82700f9514cc175d2b54733ebdeb8824922ehding        pJPEGContext->pMTXSetup->ComponentPlane[1].ui32Height = pJPEGContext->MCUComponent[0].ui32YLimit / 2;
520e26d82700f9514cc175d2b54733ebdeb8824922ehding        pJPEGContext->pMTXSetup->ComponentPlane[2].ui32Height = pJPEGContext->MCUComponent[0].ui32YLimit / 2;
5218e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5228e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
5238e9a21e730449c10cac6e6f69d255611c93f63c2hding
5248e9a21e730449c10cac6e6f69d255611c93f63c2hding    srf_buf_offset = pTFrame->psb_surface->buf.buffer_ofs;
5258e9a21e730449c10cac6e6f69d255611c93f63c2hding    RELOC_JPEG_PIC_PARAMS_PTG(&pJPEGContext->pMTXSetup->ComponentPlane[0].ui32PhysAddr, srf_buf_offset, &pTFrame->psb_surface->buf);
5268e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pJPEGContext->eFormat) {
5278e9a21e730449c10cac6e6f69d255611c93f63c2hding    case IMG_CODEC_PL12:
5288e9a21e730449c10cac6e6f69d255611c93f63c2hding        RELOC_JPEG_PIC_PARAMS_PTG(&pJPEGContext->pMTXSetup->ComponentPlane[1].ui32PhysAddr,
5298e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height,
5308e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  &pTFrame->psb_surface->buf);
5318e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Byte interleaved surface, so need to force chroma to use single surface by fooling it into
5328e9a21e730449c10cac6e6f69d255611c93f63c2hding        //thinking it's dealing with standard 8x8 planaerblocks
5338e9a21e730449c10cac6e6f69d255611c93f63c2hding        RELOC_JPEG_PIC_PARAMS_PTG(&pJPEGContext->pMTXSetup->ComponentPlane[2].ui32PhysAddr,
5348e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height + 8,
5358e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  &pTFrame->psb_surface->buf);
5368e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5378e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
5388e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, " Not supported FOURCC %x!\n", pJPEGContext->eFormat);
5398e9a21e730449c10cac6e6f69d255611c93f63c2hding        return IMG_ERR_INVALID_CONTEXT;
5408e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
5418e9a21e730449c10cac6e6f69d255611c93f63c2hding
5428e9a21e730449c10cac6e6f69d255611c93f63c2hding    memcpy((void *)pJPEGContext->pMTXSetup->MCUComponent,
5438e9a21e730449c10cac6e6f69d255611c93f63c2hding           (void *)pJPEGContext->MCUComponent,
5448e9a21e730449c10cac6e6f69d255611c93f63c2hding           sizeof(pJPEGContext->MCUComponent));
5458e9a21e730449c10cac6e6f69d255611c93f63c2hding
5468e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->pMTXSetup->ui32TableA = 0;
5478e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->pMTXSetup->ui16DataInterleaveStatus = ISCHROMAINTERLEAVED(pJPEGContext->eFormat);
54888d0460822f8807666406c14662591167eed73a0hding    pJPEGContext->pMTXSetup->ui16MaxPipes = (IMG_UINT16)pJPEGContext->NumCores;
5498e9a21e730449c10cac6e6f69d255611c93f63c2hding
5508e9a21e730449c10cac6e6f69d255611c93f63c2hding    return IMG_ERR_OK;
5518e9a21e730449c10cac6e6f69d255611c93f63c2hding}
5528e9a21e730449c10cac6e6f69d255611c93f63c2hding
5538e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void IssueMTXSetup(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
5548e9a21e730449c10cac6e6f69d255611c93f63c2hding{
5558e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
5568e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
5578e9a21e730449c10cac6e6f69d255611c93f63c2hding
5588e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Dump MTX setup data for debug */
5598e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(NULL != pJPEGContext->pMTXSetup);
5608e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Issue MTX setup data\n");
5618e9a21e730449c10cac6e6f69d255611c93f63c2hding
5628e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < pJPEGContext->pMTXSetup->ui32ComponentsInScan; i++) {
5638e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "ComponentPlane[%d]: 0x%x, %d, %d\n", i,
5648e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->ComponentPlane[i].ui32PhysAddr,
5658e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->ComponentPlane[i].ui32Stride,
5668e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->ComponentPlane[i].ui32Height);
5678e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "MCUComponent[%d]: %d, %d, %d, %d\n", i,
5688e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32WidthBlocks,
5698e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32HeightBlocks,
5708e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32XLimit,
5718e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32YLimit);
5728e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
5738e9a21e730449c10cac6e6f69d255611c93f63c2hding
5748e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32ComponentsInScan: %d\n", pJPEGContext->pMTXSetup->ui32ComponentsInScan);
5758e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32TableA: %d\n", pJPEGContext->pMTXSetup->ui32TableA);
5768e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16DataInterleaveStatus: %d\n", pJPEGContext->pMTXSetup->ui16DataInterleaveStatus);
5778e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16MaxPipes: %d\n", pJPEGContext->pMTXSetup->ui16MaxPipes);
5788e9a21e730449c10cac6e6f69d255611c93f63c2hding
579381e313cd06b652a08bef36b3e9d44ae096e72b2pingshix    tng_cmdbuf_insert_command(ctx->obj_context,
5808e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
5818e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_SETUP,
58288d0460822f8807666406c14662591167eed73a0hding                                      0,
583430ce5c32361119ed81a62c918be6afbdd053eddhding                                      &(ctx->obj_context->tng_cmdbuf->jpeg_header_mem),
5848e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0);
5858e9a21e730449c10cac6e6f69d255611c93f63c2hding
5868e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
5878e9a21e730449c10cac6e6f69d255611c93f63c2hding}
5888e9a21e730449c10cac6e6f69d255611c93f63c2hding
5898e9a21e730449c10cac6e6f69d255611c93f63c2hding
5908e9a21e730449c10cac6e6f69d255611c93f63c2hding
5918e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void fPutBitsToBuffer(STREAMTYPEW *BitStream, IMG_UINT8 NoOfBytes, IMG_UINT32 ActualBits)
5928e9a21e730449c10cac6e6f69d255611c93f63c2hding{
5938e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 ui8Lp;
5948e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 *pui8S;
5958e9a21e730449c10cac6e6f69d255611c93f63c2hding
5968e9a21e730449c10cac6e6f69d255611c93f63c2hding    pui8S = (IMG_UINT8 *)BitStream->Buffer;
5978e9a21e730449c10cac6e6f69d255611c93f63c2hding    pui8S += BitStream->Offset;
5988e9a21e730449c10cac6e6f69d255611c93f63c2hding
5998e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui8Lp = NoOfBytes; ui8Lp > 0; ui8Lp--)
6008e9a21e730449c10cac6e6f69d255611c93f63c2hding        *(pui8S++) = ((IMG_UINT8 *) &ActualBits)[ui8Lp-1];
6018e9a21e730449c10cac6e6f69d255611c93f63c2hding
6028e9a21e730449c10cac6e6f69d255611c93f63c2hding    BitStream->Offset += NoOfBytes;
6038e9a21e730449c10cac6e6f69d255611c93f63c2hding}
6048e9a21e730449c10cac6e6f69d255611c93f63c2hding
6058e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 EncodeMarkerSegment(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
6068e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      IMG_UINT8 *puc_stream_buff, IMG_BOOL bIncludeHuffmanTables)
6078e9a21e730449c10cac6e6f69d255611c93f63c2hding{
6088e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW s_streamW;
6098e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 uc_i;
6108e9a21e730449c10cac6e6f69d255611c93f63c2hding
6118e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Offset = 0;
6128e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Buffer = puc_stream_buff;
6138e9a21e730449c10cac6e6f69d255611c93f63c2hding
6148e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the start of image marker */
6158e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, START_OF_IMAGE);
6168e9a21e730449c10cac6e6f69d255611c93f63c2hding
6178e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the quantization table for luminance into the stream */
6188e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
6198e9a21e730449c10cac6e6f69d255611c93f63c2hding
6208e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 3, LQPQ << 4); // 20 bits = LQPQ, 4 bits = 0 (Destination identifier for the luminance quantizer tables)
6218e9a21e730449c10cac6e6f69d255611c93f63c2hding
6228e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_ASSERT(PELS_IN_BLOCK <= QUANT_TABLE_SIZE_BYTES);
6238e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
6248e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Write zigzag ordered luma quantization values to our JPEG header
6258e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, pJPEGContext->psTablesBlock->aui8LumaQuantParams[gZigZag[uc_i]]);
6268e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6278e9a21e730449c10cac6e6f69d255611c93f63c2hding
6288e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the quantization table for chrominance into the stream */
6298e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
6308e9a21e730449c10cac6e6f69d255611c93f63c2hding
6318e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 3, (LQPQ << 4) | 1); // 20 bits = LQPQ, 4 bits = 1 (Destination identifier for the chrominance quantizer tables)
6328e9a21e730449c10cac6e6f69d255611c93f63c2hding
6338e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
6348e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Write zigzag ordered chroma quantization values to our JPEG header
6358e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, pJPEGContext->psTablesBlock->aui8ChromaQuantParams[gZigZag[uc_i]]);
6368e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6378e9a21e730449c10cac6e6f69d255611c93f63c2hding
6388e9a21e730449c10cac6e6f69d255611c93f63c2hding
6398e9a21e730449c10cac6e6f69d255611c93f63c2hding
6408e9a21e730449c10cac6e6f69d255611c93f63c2hding
6418e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (bIncludeHuffmanTables) {
6428e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for luminance dc coeffs */
6438e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Write the DHT Marker */
6448e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6458e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_DC);
6468e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
6478e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaDc[uc_i]);
6488e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6498e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for luminance ac coeffs */
6508e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Write the DHT Marker */
6518e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6528e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_AC);
6538e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
6548e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaAc[uc_i]);
6558e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6568e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for chrominance dc coeffs */
6578e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6588e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_DC);
6598e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
6608e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaDc[uc_i]);
6618e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6628e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for luminance ac coeffs */
6638e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Write the DHT Marker */
6648e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6658e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_AC);
6668e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
6678e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaAc[uc_i]);
6688e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6698e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6708e9a21e730449c10cac6e6f69d255611c93f63c2hding
6718e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Activate Restart markers
6728e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pJPEGContext->sScan_Encode_Info.ui16CScan > 1) {
6738e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Only use restart intervals if we need them (ie. multiple Scan encode and/or parallel CB encode)
6748e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, 0xFFDD); //Marker header
6758e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, 4); // Byte size of marker (header not included)
6768e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan); // Restart Interval (same as MCUs per buffer)
6778e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6788e9a21e730449c10cac6e6f69d255611c93f63c2hding
6798e9a21e730449c10cac6e6f69d255611c93f63c2hding    return s_streamW.Offset;
6808e9a21e730449c10cac6e6f69d255611c93f63c2hding}
6818e9a21e730449c10cac6e6f69d255611c93f63c2hding
6828e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 EncodeFrameHeader(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
6838e9a21e730449c10cac6e6f69d255611c93f63c2hding                                    IMG_UINT8 *puc_stream_buff)
6848e9a21e730449c10cac6e6f69d255611c93f63c2hding{
6858e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW ps_streamW;
6868e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8  uc_num_comp_in_img;
6878e9a21e730449c10cac6e6f69d255611c93f63c2hding
6888e9a21e730449c10cac6e6f69d255611c93f63c2hding    uc_num_comp_in_img = pJPEGContext->pMTXSetup->ui32ComponentsInScan;
6898e9a21e730449c10cac6e6f69d255611c93f63c2hding
6908e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_streamW.Offset = 0;
6918e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_streamW.Buffer = puc_stream_buff;
6928e9a21e730449c10cac6e6f69d255611c93f63c2hding
6938e9a21e730449c10cac6e6f69d255611c93f63c2hding
6948e9a21e730449c10cac6e6f69d255611c93f63c2hding    //if(ps_jpeg_params->uc_isAbbreviated != 0)
6958e9a21e730449c10cac6e6f69d255611c93f63c2hding    //   fPutBitsToBuffer(&ps_streamW, 2, START_OF_IMAGE);
6968e9a21e730449c10cac6e6f69d255611c93f63c2hding
6978e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the frame header */
6988e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, SOF_BASELINE_DCT);
6998e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Frame header length */
7008e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, 8 + 3 * uc_num_comp_in_img);
7018e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Precision */
7028e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 1, 8);
7038e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Height : sample lines */
7048e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, pJPEGContext->ui32OutputHeight);
7058e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Width : samples per line */
7068e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, pJPEGContext->ui32OutputWidth);
7078e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Number of image components */
7088e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 1, uc_num_comp_in_img);
7098e9a21e730449c10cac6e6f69d255611c93f63c2hding
7108e9a21e730449c10cac6e6f69d255611c93f63c2hding
7118e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Chroma Details
7128e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pJPEGContext->pMTXSetup->ui16DataInterleaveStatus < C_INTERLEAVE) {
7138e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Luma Details
7148e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Component identifier */
7158e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); //CompId 0 = 1, 1 = 2, 2 = 3
7168e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7178e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 0); // 0 = Luma(0), 1,2 = Chroma(1)
7188e9a21e730449c10cac6e6f69d255611c93f63c2hding
7198e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Chroma planar
7208e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 2); //CompId 0 = 1, 1 = 2, 2 = 3
7218e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7228e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
7238e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7248e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
7258e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7268e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[2].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[2].ui32HeightBlocks >> 3));
7278e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7288e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else if (pJPEGContext->pMTXSetup->ui16DataInterleaveStatus == C_INTERLEAVE) {
7298e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Luma Details
7308e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Component identifier */
7318e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); //CompId 0 = 1, 1 = 2, 2 = 3
7328e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7338e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 0); // 0 = Luma(0), 1,2 = Chroma(1)
7348e9a21e730449c10cac6e6f69d255611c93f63c2hding
7358e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Chroma Interleaved
7368e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 2); //CompId 0 = 1, 1 = 2, 2 = 3
7378e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7388e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 4) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
7398e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7408e9a21e730449c10cac6e6f69d255611c93f63c2hding
7418e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
7428e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7438e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 4) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
7448e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7458e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else {
7468e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Luma Details
7478e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Component identifier */
7488e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); //CompId 0 = 1, 1 = 2, 2 = 3
7498e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 4) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7508e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 0); // 0 = Luma(0), 1,2 = Chroma(1)
7518e9a21e730449c10cac6e6f69d255611c93f63c2hding
7528e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Chroma YUYV - Special case
7538e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 2); //CompId 0 = 1, 1 = 2, 2 = 3
7548e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7558e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 5) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7568e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7578e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
7588e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7598e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 5) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7608e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7618e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
7628e9a21e730449c10cac6e6f69d255611c93f63c2hding
7638e9a21e730449c10cac6e6f69d255611c93f63c2hding
7648e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Use if you want start of scan (image data) to align to 32
765234896afa7fd6fda7e36cb3e60dc3012d207e915hding    //fPutBitsToBuffer(&ps_streamW, 1, 0xFF);
7668e9a21e730449c10cac6e6f69d255611c93f63c2hding
7678e9a21e730449c10cac6e6f69d255611c93f63c2hding    return ps_streamW.Offset;
7688e9a21e730449c10cac6e6f69d255611c93f63c2hding}
7698e9a21e730449c10cac6e6f69d255611c93f63c2hding
7708e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 JPGEncodeMarker(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext ,
7718e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT8* pui8BitStreamBuffer ,
7728e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT32 *pui32BytesWritten, IMG_BOOL bIncludeHuffmanTables)
7738e9a21e730449c10cac6e6f69d255611c93f63c2hding{
7748e9a21e730449c10cac6e6f69d255611c93f63c2hding#ifdef JPEG_VERBOSE
7758e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "PVRJPGEncodeMarker");
7768e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
7778e9a21e730449c10cac6e6f69d255611c93f63c2hding
7788e9a21e730449c10cac6e6f69d255611c93f63c2hding
7798e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui32BytesWritten += EncodeMarkerSegment(pJPEGContext, pui8BitStreamBuffer + *pui32BytesWritten, bIncludeHuffmanTables);
7808e9a21e730449c10cac6e6f69d255611c93f63c2hding
7818e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
7828e9a21e730449c10cac6e6f69d255611c93f63c2hding}
7838e9a21e730449c10cac6e6f69d255611c93f63c2hding
7848e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 JPGEncodeHeader(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
7858e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT8*      pui8BitStreamBuffer ,
7868e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT32*     pui32BytesWritten)
7878e9a21e730449c10cac6e6f69d255611c93f63c2hding{
7888e9a21e730449c10cac6e6f69d255611c93f63c2hding#ifdef JPEG_VERBOSE
7898e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "JPGEncodeHeader");
7908e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
7918e9a21e730449c10cac6e6f69d255611c93f63c2hding
7928e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui32BytesWritten += EncodeFrameHeader(pJPEGContext, pui8BitStreamBuffer + *pui32BytesWritten);
7938e9a21e730449c10cac6e6f69d255611c93f63c2hding
7948e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
7958e9a21e730449c10cac6e6f69d255611c93f63c2hding}
7968e9a21e730449c10cac6e6f69d255611c93f63c2hding
7978e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 JPGEncodeSOSHeader(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
7988e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     IMG_UINT8*      pui8BitStreamBuffer ,
7998e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     IMG_UINT32*     pui32BytesWritten)
8008e9a21e730449c10cac6e6f69d255611c93f63c2hding{
8018e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 uc_comp_id, ui8Comp;
8028e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW s_streamW;
8038e9a21e730449c10cac6e6f69d255611c93f63c2hding
8048e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Offset = 0;
8058e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Buffer = pui8BitStreamBuffer + *pui32BytesWritten;
8068e9a21e730449c10cac6e6f69d255611c93f63c2hding
8078e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Start of scan */
8088e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, START_OF_SCAN);
8098e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Scan header length */
8108e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, 6 + (pJPEGContext->pMTXSetup->ui32ComponentsInScan << 1));
8118e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Number of image components in scan */
8128e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, pJPEGContext->pMTXSetup->ui32ComponentsInScan);
8138e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui8Comp = 0; ui8Comp < pJPEGContext->pMTXSetup->ui32ComponentsInScan; ui8Comp++) {
8148e9a21e730449c10cac6e6f69d255611c93f63c2hding        uc_comp_id = ui8Comp + 1;
8158e9a21e730449c10cac6e6f69d255611c93f63c2hding
8168e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Scan component selector */
8178e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, uc_comp_id);
8188e9a21e730449c10cac6e6f69d255611c93f63c2hding
8198e9a21e730449c10cac6e6f69d255611c93f63c2hding        /*4 Bits Dc entropy coding table destination selector */
8208e9a21e730449c10cac6e6f69d255611c93f63c2hding        /*4 Bits Ac entropy coding table destination selector */
8218e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, ((ui8Comp != 0 ? 1 : 0) << 4) | (ui8Comp != 0 ? 1 : 0)); // Huffman table refs = 0 Luma 1 Chroma
8228e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
8238e9a21e730449c10cac6e6f69d255611c93f63c2hding
8248e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Start of spectral or predictor selection  */
8258e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, 0);
8268e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* End of spectral selection */
8278e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, 63);
8288e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*4 Bits Successive approximation bit position high (0)*/
8298e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*4 Bits Successive approximation bit position low or point transform (0)*/
8308e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, 0);
8318e9a21e730449c10cac6e6f69d255611c93f63c2hding
8328e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui32BytesWritten += s_streamW.Offset;
8338e9a21e730449c10cac6e6f69d255611c93f63c2hding
8348e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
8358e9a21e730449c10cac6e6f69d255611c93f63c2hding}
8368e9a21e730449c10cac6e6f69d255611c93f63c2hding
8378e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void InitializeScanCounter(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
8388e9a21e730449c10cac6e6f69d255611c93f63c2hding{
8398e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui16SScan =
8408e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui16CScan =
8418e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->sScan_Encode_Info.ui16ScansInImage;
8428e9a21e730449c10cac6e6f69d255611c93f63c2hding}
8438e9a21e730449c10cac6e6f69d255611c93f63c2hding
8448e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_ERRORCODE PrepareHeader(TOPAZHP_JPEG_ENCODER_CONTEXT * pJPEGContext, IMG_CODED_BUFFER *pCBuffer, IMG_UINT32 ui32StartOffset, IMG_BOOL bIncludeHuffmanTables)
8458e9a21e730449c10cac6e6f69d255611c93f63c2hding{
8468e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_ERRORCODE rc;
8478e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 *ui8OutputBuffer;
8488e9a21e730449c10cac6e6f69d255611c93f63c2hding
8498e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Locate our JPEG Coded buffer
8508e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui8OutputBuffer = (IMG_UINT8 *)pCBuffer->pMemInfo;
8518e9a21e730449c10cac6e6f69d255611c93f63c2hding
8528e9a21e730449c10cac6e6f69d255611c93f63c2hding    pCBuffer->ui32BytesWritten = ui32StartOffset;
8538e9a21e730449c10cac6e6f69d255611c93f63c2hding    *((IMG_UINT32*)ui8OutputBuffer + pCBuffer->ui32BytesWritten) = 0;
8548e9a21e730449c10cac6e6f69d255611c93f63c2hding
8558e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Before writing headers, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8568e9a21e730449c10cac6e6f69d255611c93f63c2hding
8578e9a21e730449c10cac6e6f69d255611c93f63c2hding    // JPGEncodeMarker - Currently misses out the APP0 header
8588e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = JPGEncodeMarker(pJPEGContext, (IMG_UINT8 *) ui8OutputBuffer,  &pCBuffer->ui32BytesWritten, bIncludeHuffmanTables);
8598e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc) return rc;
8608e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "After JPGEncodeMarker, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8618e9a21e730449c10cac6e6f69d255611c93f63c2hding
8628e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = JPGEncodeHeader(pJPEGContext , (IMG_UINT8 *) ui8OutputBuffer ,  &pCBuffer->ui32BytesWritten);
8638e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc) return rc;
8648e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "After JPGEncodeHeader, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8658e9a21e730449c10cac6e6f69d255611c93f63c2hding
8668e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = JPGEncodeSOSHeader(pJPEGContext, (IMG_UINT8 *) ui8OutputBuffer, &pCBuffer->ui32BytesWritten);
8678e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc) return rc;
8688e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "After JPGEncodeSOSHeader, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8698e9a21e730449c10cac6e6f69d255611c93f63c2hding
8708e9a21e730449c10cac6e6f69d255611c93f63c2hding    return IMG_ERR_OK;
8718e9a21e730449c10cac6e6f69d255611c93f63c2hding}
8728e9a21e730449c10cac6e6f69d255611c93f63c2hding
8738e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_ERRORCODE IssueBufferToHW(
8748e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
8758e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16BCnt,
8768e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_INT8 i8PipeNumber,
8778e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32NoMCUsToEncode)
8788e9a21e730449c10cac6e6f69d255611c93f63c2hding{
8798e9a21e730449c10cac6e6f69d255611c93f63c2hding    MTX_ISSUE_BUFFERS *psBufferCmd;
8808e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)(pJPEGContext->ctx);
8818e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
8828e9a21e730449c10cac6e6f69d255611c93f63c2hding
8838e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui32DataBufferUsedBytes = ((BUFFER_HEADER*)(pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].pMemInfo))->ui32BytesUsed = -1; // Won't be necessary with SC Peek commands enabled
8848e9a21e730449c10cac6e6f69d255611c93f63c2hding
8858e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Submit Scan %d which contains %d MCU in Buffer %d to MTX %d\n",
8868e9a21e730449c10cac6e6f69d255611c93f63c2hding                             pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber,
8878e9a21e730449c10cac6e6f69d255611c93f63c2hding                             ui32NoMCUsToEncode, ui16BCnt, i8PipeNumber);
8888e9a21e730449c10cac6e6f69d255611c93f63c2hding
8898e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Issue to MTX ////////////////////////////
8908e9a21e730449c10cac6e6f69d255611c93f63c2hding
8918e9a21e730449c10cac6e6f69d255611c93f63c2hding    psBufferCmd = (MTX_ISSUE_BUFFERS *)(pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].pMemInfo);
8928e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(psBufferCmd);
8938e9a21e730449c10cac6e6f69d255611c93f63c2hding
8948e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16ScansInImage: %d\n", pJPEGContext->sScan_Encode_Info.ui16ScansInImage);
8958e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16ScanNumber: %d\n", pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber);
8968e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32NumberMCUsToEncodePerScan: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
89788d0460822f8807666406c14662591167eed73a0hding
8988e9a21e730449c10cac6e6f69d255611c93f63c2hding    psBufferCmd->ui32MCUCntAndResetFlag = (ui32NoMCUsToEncode << 1) | 0x1;
89988d0460822f8807666406c14662591167eed73a0hding
9008e9a21e730449c10cac6e6f69d255611c93f63c2hding    psBufferCmd->ui32MCUPositionOfScanAndPipeNo =
90188d0460822f8807666406c14662591167eed73a0hding        (((pJPEGContext->sScan_Encode_Info.ui16ScansInImage -
9028e9a21e730449c10cac6e6f69d255611c93f63c2hding          pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber) *
90388d0460822f8807666406c14662591167eed73a0hding         pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan)<<2)&(~2);
9048e9a21e730449c10cac6e6f69d255611c93f63c2hding
9058e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(0 == i8PipeNumber);
90688d0460822f8807666406c14662591167eed73a0hding    if (i8PipeNumber <= 3)
90788d0460822f8807666406c14662591167eed73a0hding        psBufferCmd->ui32MCUPositionOfScanAndPipeNo |= i8PipeNumber;
9088e9a21e730449c10cac6e6f69d255611c93f63c2hding
9098e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32MCUPositionOfScanAndPipeNo: 0x%x\n", psBufferCmd->ui32MCUPositionOfScanAndPipeNo);
9108e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32MCUCntAndResetFlag: 0x%x\n", psBufferCmd->ui32MCUCntAndResetFlag);
9118e9a21e730449c10cac6e6f69d255611c93f63c2hding
9128e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "psBufferCmd: 0x%x\n", (unsigned int)(psBufferCmd));
9138e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Command Data: 0x%x\n", (unsigned int)(PTG_JPEG_HEADER_MAX_SIZE + ui16BCnt * pJPEGContext->ui32SizePerCodedBuffer));
9148e9a21e730449c10cac6e6f69d255611c93f63c2hding
9158e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Issue buffers
916381e313cd06b652a08bef36b3e9d44ae096e72b2pingshix    tng_cmdbuf_insert_command(ctx->obj_context,
9178e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
9188e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_ISSUEBUFF,
91988d0460822f8807666406c14662591167eed73a0hding                                      0,
9208e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      ps_buf->coded_buf->psb_buffer,
9218e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      PTG_JPEG_HEADER_MAX_SIZE + ui16BCnt * pJPEGContext->ui32SizePerCodedBuffer);
9228e9a21e730449c10cac6e6f69d255611c93f63c2hding
9238e9a21e730449c10cac6e6f69d255611c93f63c2hding    return IMG_ERR_OK;
9248e9a21e730449c10cac6e6f69d255611c93f63c2hding}
9258e9a21e730449c10cac6e6f69d255611c93f63c2hding
926430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic void tng_jpeg_QueryConfigAttributes(
9278e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAProfile profile,
9288e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEntrypoint entrypoint,
9298e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAConfigAttrib *attrib_list,
9308e9a21e730449c10cac6e6f69d255611c93f63c2hding    int num_attribs)
9318e9a21e730449c10cac6e6f69d255611c93f63c2hding{
9328e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
9338e9a21e730449c10cac6e6f69d255611c93f63c2hding
934430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_QueryConfigAttributes\n");
9358e9a21e730449c10cac6e6f69d255611c93f63c2hding
9368e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* RateControl attributes */
9378e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < num_attribs; i++) {
9388e9a21e730449c10cac6e6f69d255611c93f63c2hding        switch (attrib_list[i].type) {
9398e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAConfigAttribRTFormat:
9408e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
9418e9a21e730449c10cac6e6f69d255611c93f63c2hding        default:
9428e9a21e730449c10cac6e6f69d255611c93f63c2hding            attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
9438e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
9448e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
9458e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
9468e9a21e730449c10cac6e6f69d255611c93f63c2hding
9478e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
9488e9a21e730449c10cac6e6f69d255611c93f63c2hding}
9498e9a21e730449c10cac6e6f69d255611c93f63c2hding
9508e9a21e730449c10cac6e6f69d255611c93f63c2hding
951430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_ValidateConfig(
9528e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_config_p obj_config)
9538e9a21e730449c10cac6e6f69d255611c93f63c2hding{
9548e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
955430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_ValidateConfig\n");
9568e9a21e730449c10cac6e6f69d255611c93f63c2hding
9578e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
9588e9a21e730449c10cac6e6f69d255611c93f63c2hding
9598e9a21e730449c10cac6e6f69d255611c93f63c2hding}
9608e9a21e730449c10cac6e6f69d255611c93f63c2hding
961430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_CreateContext(
9628e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context,
9638e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_config_p obj_config)
9648e9a21e730449c10cac6e6f69d255611c93f63c2hding{
9658e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
9668e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
9678e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx;
9688e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p;
9698e9a21e730449c10cac6e6f69d255611c93f63c2hding
970430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_CreateContext\n");
9718e9a21e730449c10cac6e6f69d255611c93f63c2hding
972430ce5c32361119ed81a62c918be6afbdd053eddhding    vaStatus = tng_CreateContext(obj_context, obj_config, 1);
9738e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (VA_STATUS_SUCCESS != vaStatus)
9748e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_ALLOCATION_FAILED;
9758e9a21e730449c10cac6e6f69d255611c93f63c2hding
9768e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx = (context_ENC_p) obj_context->format_data;
9778e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->eCodec = IMG_CODEC_JPEG;
9788e9a21e730449c10cac6e6f69d255611c93f63c2hding
9798e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(0 == (ctx->ui16Width % 2));
9808e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(0 == (ctx->ui16FrameHeight % 2));
9818e9a21e730449c10cac6e6f69d255611c93f63c2hding
9828e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < obj_config->attrib_count; i++) {
9838e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (VAConfigAttribRTFormat ==  obj_config->attrib_list[i].type) {
9848e9a21e730449c10cac6e6f69d255611c93f63c2hding            switch (obj_config->attrib_list[i].value) {
9858e9a21e730449c10cac6e6f69d255611c93f63c2hding            case VA_RT_FORMAT_YUV420:
9868e9a21e730449c10cac6e6f69d255611c93f63c2hding                ctx->eFormat = IMG_CODEC_PL12;
9878e9a21e730449c10cac6e6f69d255611c93f63c2hding                drv_debug_msg(VIDEO_DEBUG_GENERAL, "JPEG encoding: NV12 format chose.\n");
9888e9a21e730449c10cac6e6f69d255611c93f63c2hding                break;
9898e9a21e730449c10cac6e6f69d255611c93f63c2hding            default:
9908e9a21e730449c10cac6e6f69d255611c93f63c2hding                drv_debug_msg(VIDEO_DEBUG_ERROR, "JPEG encoding: unsupported YUV format and force it to be NV12!\n");
9918e9a21e730449c10cac6e6f69d255611c93f63c2hding                ctx->eFormat = IMG_CODEC_PL12;
9928e9a21e730449c10cac6e6f69d255611c93f63c2hding                break;
9938e9a21e730449c10cac6e6f69d255611c93f63c2hding            }
9948e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
9958e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
9968e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
9978e9a21e730449c10cac6e6f69d255611c93f63c2hding
9988e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->jpeg_ctx = (TOPAZHP_JPEG_ENCODER_CONTEXT *)calloc(1, sizeof(TOPAZHP_JPEG_ENCODER_CONTEXT));
9998e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == ctx->jpeg_ctx)
10008e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_ALLOCATION_FAILED;
10018e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p = ctx->jpeg_ctx;
10028e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->ctx = ctx;
10038e9a21e730449c10cac6e6f69d255611c93f63c2hding
10048e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset((void *)jpeg_ctx_p, 0x0, sizeof(jpeg_ctx_p));
10058e9a21e730449c10cac6e6f69d255611c93f63c2hding
1006c4a7c43f761ec6b9b4777aa0ccf3ab6045562242SUN,Jing    jpeg_ctx_p->NumCores = TOPAZHP_PIPE_NUM;
10078e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->eFormat = ctx->eFormat;
10088e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->ui32OutputWidth = ctx->ui16Width;
10098e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->ui32OutputHeight = ctx->ui16FrameHeight;
10108e9a21e730449c10cac6e6f69d255611c93f63c2hding
10118e9a21e730449c10cac6e6f69d255611c93f63c2hding    InitializeJpegEncode(jpeg_ctx_p);
10128e9a21e730449c10cac6e6f69d255611c93f63c2hding
10138e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((jpeg_ctx_p->sScan_Encode_Info.ui16ScansInImage < 1) ||
10148e9a21e730449c10cac6e6f69d255611c93f63c2hding        (jpeg_ctx_p->sScan_Encode_Info.ui16ScansInImage > PTG_JPEG_MAX_SCAN_NUM)) {
10158e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "JPEG MCU scanning number(%d) is wrong!\n", jpeg_ctx_p->sScan_Encode_Info.ui16ScansInImage);
10168e9a21e730449c10cac6e6f69d255611c93f63c2hding        free(ctx->jpeg_ctx);
10178e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->jpeg_ctx = NULL;
10188e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
10198e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
10208e9a21e730449c10cac6e6f69d255611c93f63c2hding
10218e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*Allocate coded buffers' descripters */
10228e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->sScan_Encode_Info.aBufferTable = (TOPAZHP_JPEG_BUFFER_INFO *)calloc(1, (jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers) * (sizeof(TOPAZHP_JPEG_BUFFER_INFO)));
10238e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == jpeg_ctx_p->sScan_Encode_Info.aBufferTable)
10248e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_ALLOCATION_FAILED;
10258e9a21e730449c10cac6e6f69d255611c93f63c2hding
10268e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
10278e9a21e730449c10cac6e6f69d255611c93f63c2hding}
10288e9a21e730449c10cac6e6f69d255611c93f63c2hding
10298e9a21e730449c10cac6e6f69d255611c93f63c2hding
1030430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic void tng_jpeg_DestroyContext(
10318e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
10328e9a21e730449c10cac6e6f69d255611c93f63c2hding{
10338e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx;
10348e9a21e730449c10cac6e6f69d255611c93f63c2hding
1035430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_DestroyPicture\n");
10368e9a21e730449c10cac6e6f69d255611c93f63c2hding
10378e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx = (context_ENC_p)(obj_context->format_data);
10388e9a21e730449c10cac6e6f69d255611c93f63c2hding
10398e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ctx->jpeg_ctx) {
10408e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (ctx->jpeg_ctx->sScan_Encode_Info.aBufferTable) {
10418e9a21e730449c10cac6e6f69d255611c93f63c2hding            free(ctx->jpeg_ctx->sScan_Encode_Info.aBufferTable);
10428e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->jpeg_ctx->sScan_Encode_Info.aBufferTable = NULL;
10438e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
10448e9a21e730449c10cac6e6f69d255611c93f63c2hding
10458e9a21e730449c10cac6e6f69d255611c93f63c2hding        free(ctx->jpeg_ctx);
10468e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
10478e9a21e730449c10cac6e6f69d255611c93f63c2hding
1048430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_DestroyContext(obj_context, 1);
10498e9a21e730449c10cac6e6f69d255611c93f63c2hding}
10508e9a21e730449c10cac6e6f69d255611c93f63c2hding
1051430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng__cmdbuf_lowpower(context_ENC_p ctx)
1052430ce5c32361119ed81a62c918be6afbdd053eddhding{
1053430ce5c32361119ed81a62c918be6afbdd053eddhding    VAStatus vaStatus = VA_STATUS_SUCCESS;
1054430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
1055430ce5c32361119ed81a62c918be6afbdd053eddhding    psb_driver_data_p driver_data = ctx->obj_context->driver_data;
1056430ce5c32361119ed81a62c918be6afbdd053eddhding
1057430ce5c32361119ed81a62c918be6afbdd053eddhding    *cmdbuf->cmd_idx++ =
1058430ce5c32361119ed81a62c918be6afbdd053eddhding        ((MTX_CMDID_SW_LEAVE_LOWPOWER & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT) |
1059430ce5c32361119ed81a62c918be6afbdd053eddhding        ((ctx->ui32RawFrameCount  & MTX_CMDWORD_CORE_MASK) << MTX_CMDWORD_CORE_SHIFT) |
1060430ce5c32361119ed81a62c918be6afbdd053eddhding        (((driver_data->context_id & MTX_CMDWORD_COUNT_MASK) << MTX_CMDWORD_COUNT_SHIFT));
1061430ce5c32361119ed81a62c918be6afbdd053eddhding
1062430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_insert_command_param(ctx->eCodec);
1063430ce5c32361119ed81a62c918be6afbdd053eddhding
1064430ce5c32361119ed81a62c918be6afbdd053eddhding    return vaStatus;
1065430ce5c32361119ed81a62c918be6afbdd053eddhding}
1066430ce5c32361119ed81a62c918be6afbdd053eddhding
1067430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_BeginPicture(
10688e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
10698e9a21e730449c10cac6e6f69d255611c93f63c2hding{
10708e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
10718e9a21e730449c10cac6e6f69d255611c93f63c2hding    int ret;
1072430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf;
10738e9a21e730449c10cac6e6f69d255611c93f63c2hding
10748e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
10758e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p = ctx->jpeg_ctx;
10768e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
1077430ce5c32361119ed81a62c918be6afbdd053eddhding    psb_driver_data_p driver_data = ctx->obj_context->driver_data;
10788e9a21e730449c10cac6e6f69d255611c93f63c2hding
1079430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_BeginPicture: Frame %d\n", ctx->obj_context->frame_count);
10808e9a21e730449c10cac6e6f69d255611c93f63c2hding
10818e9a21e730449c10cac6e6f69d255611c93f63c2hding
10828e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Get the current surface */
10838e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_buf->src_surface = ctx->obj_context->current_render_target;
10848e9a21e730449c10cac6e6f69d255611c93f63c2hding
10858e9a21e730449c10cac6e6f69d255611c93f63c2hding
10868e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Initialize the command buffer */
1087430ce5c32361119ed81a62c918be6afbdd053eddhding    ret = tng_context_get_next_cmdbuf(ctx->obj_context);
10888e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ret) {
10898e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "get next cmdbuf fail\n");
10908e9a21e730449c10cac6e6f69d255611c93f63c2hding        vaStatus = VA_STATUS_ERROR_UNKNOWN;
10918e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
10928e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1093430ce5c32361119ed81a62c918be6afbdd053eddhding    cmdbuf = ctx->obj_context->tng_cmdbuf;
10948e9a21e730449c10cac6e6f69d255611c93f63c2hding
10958e9a21e730449c10cac6e6f69d255611c93f63c2hding
10968e9a21e730449c10cac6e6f69d255611c93f63c2hding    //For the first picture of a set to be encoded, need to ask kernel to perpare JPEG encoding
10978e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ctx->obj_context->frame_count == 0) { /* first picture */
10988e9a21e730449c10cac6e6f69d255611c93f63c2hding
10998e9a21e730449c10cac6e6f69d255611c93f63c2hding        *cmdbuf->cmd_idx++ = ((MTX_CMDID_SW_NEW_CODEC & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT) |
1100430ce5c32361119ed81a62c918be6afbdd053eddhding			     ((ctx->eCodec) << MTX_CMDWORD_CORE_SHIFT) |
11018e9a21e730449c10cac6e6f69d255611c93f63c2hding                             (((driver_data->drm_context & MTX_CMDWORD_COUNT_MASK) << MTX_CMDWORD_COUNT_SHIFT));
1102430ce5c32361119ed81a62c918be6afbdd053eddhding        tng_cmdbuf_insert_command_param((ctx->ui16Width << 16) | ctx->ui16FrameHeight);
110312109f339d6cb5ffe571cf113b8807b7596566c2Ren, Zhaohan	tng_cmdbuf_insert_command_param(jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers);
11048e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11058e9a21e730449c10cac6e6f69d255611c93f63c2hding
11068e9a21e730449c10cac6e6f69d255611c93f63c2hding
11078e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map MTX setup buffer */
11088e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(&cmdbuf->jpeg_header_mem, &cmdbuf->jpeg_header_mem_p);
11098e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
11108e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "Fail to map MTX setup buffer\n");
11118e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
11128e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11138e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->pMemInfoMTXSetup = cmdbuf->jpeg_header_mem_p;
11148e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->pMTXSetup = (JPEG_MTX_DMA_SETUP*)jpeg_ctx_p->pMemInfoMTXSetup;
11158e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset(jpeg_ctx_p->pMemInfoMTXSetup, 0x0, ctx->jpeg_header_mem_size);
11168e9a21e730449c10cac6e6f69d255611c93f63c2hding
11178e9a21e730449c10cac6e6f69d255611c93f63c2hding
11188e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map MTX setup interface buffer */
11198e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(&cmdbuf->jpeg_header_interface_mem, &cmdbuf->jpeg_header_interface_mem_p);
11208e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
11218e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "Fail to map MTX setup interface buffer\n");
11228e9a21e730449c10cac6e6f69d255611c93f63c2hding        psb_buffer_unmap(&cmdbuf->jpeg_header_mem);
11238e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
11248e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
112588d0460822f8807666406c14662591167eed73a0hding    jpeg_ctx_p->pMemInfoWritebackMemory = cmdbuf->jpeg_header_interface_mem_p;
112688d0460822f8807666406c14662591167eed73a0hding    jpeg_ctx_p->pMTXWritebackMemory = (JPEG_MTX_WRITEBACK_MEMORY*)jpeg_ctx_p->pMemInfoWritebackMemory;
112788d0460822f8807666406c14662591167eed73a0hding    memset(jpeg_ctx_p->pMemInfoWritebackMemory, 0x0, ctx->jpeg_header_interface_mem_size);
11288e9a21e730449c10cac6e6f69d255611c93f63c2hding
11298e9a21e730449c10cac6e6f69d255611c93f63c2hding
11308e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map quantization table buffer */
11318e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(&cmdbuf->jpeg_pic_params, &cmdbuf->jpeg_pic_params_p);
11328e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
11338e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "Fail to map quantization table buffer\n");
11348e9a21e730449c10cac6e6f69d255611c93f63c2hding        psb_buffer_unmap(&cmdbuf->jpeg_header_mem);
11358e9a21e730449c10cac6e6f69d255611c93f63c2hding        psb_buffer_unmap(&cmdbuf->jpeg_header_interface_mem);
11368e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
11378e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11388e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->pMemInfoTableBlock = cmdbuf->jpeg_pic_params_p;
11398e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->psTablesBlock = (JPEG_MTX_QUANT_TABLE *)jpeg_ctx_p->pMemInfoTableBlock;
11408e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset(jpeg_ctx_p->pMemInfoTableBlock, 0x0, ctx->jpeg_pic_params_size);
11418e9a21e730449c10cac6e6f69d255611c93f63c2hding
1142430ce5c32361119ed81a62c918be6afbdd053eddhding    vaStatus = tng__cmdbuf_lowpower(ctx);
1143430ce5c32361119ed81a62c918be6afbdd053eddhding    if (vaStatus != VA_STATUS_SUCCESS) {
1144430ce5c32361119ed81a62c918be6afbdd053eddhding        drv_debug_msg(VIDEO_DEBUG_ERROR, "cmdbuf lowpower\n");
1145430ce5c32361119ed81a62c918be6afbdd053eddhding    }
11468e9a21e730449c10cac6e6f69d255611c93f63c2hding
11478e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Set SetupInterface*/
11488e9a21e730449c10cac6e6f69d255611c93f63c2hding    SetSetupInterface(jpeg_ctx_p);
11498e9a21e730449c10cac6e6f69d255611c93f63c2hding    IssueSetupInterface(jpeg_ctx_p);
11508e9a21e730449c10cac6e6f69d255611c93f63c2hding
11518e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Set MTX setup struture */
11528e9a21e730449c10cac6e6f69d255611c93f63c2hding    ret = SetMTXSetup(jpeg_ctx_p, ps_buf->src_surface);
11538e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ret != IMG_ERR_OK)
11548e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
11558e9a21e730449c10cac6e6f69d255611c93f63c2hding    IssueMTXSetup(jpeg_ctx_p);
11568e9a21e730449c10cac6e6f69d255611c93f63c2hding
11578e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Initialize the default quantization tables */
11588e9a21e730449c10cac6e6f69d255611c93f63c2hding    SetDefaultQmatix(jpeg_ctx_p->pMemInfoTableBlock);
11598e9a21e730449c10cac6e6f69d255611c93f63c2hding
11608e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Initialize scan counters */
11618e9a21e730449c10cac6e6f69d255611c93f63c2hding    InitializeScanCounter(jpeg_ctx_p);
11628e9a21e730449c10cac6e6f69d255611c93f63c2hding
1163430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_buffer_ref(cmdbuf, &(ctx->obj_context->current_render_target->psb_surface->buf));
11648e9a21e730449c10cac6e6f69d255611c93f63c2hding
11658e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
11668e9a21e730449c10cac6e6f69d255611c93f63c2hding}
11678e9a21e730449c10cac6e6f69d255611c93f63c2hding
11688e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic VAStatus ProcessQmatrixParam(context_ENC_p ctx, object_buffer_p obj_buffer)
11698e9a21e730449c10cac6e6f69d255611c93f63c2hding{
11708e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
11718e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAQMatrixBufferJPEG *pBuffer;
11728e9a21e730449c10cac6e6f69d255611c93f63c2hding    JPEG_MTX_QUANT_TABLE* pQMatrix = (JPEG_MTX_QUANT_TABLE *)(ctx->jpeg_ctx->psTablesBlock);
11738e9a21e730449c10cac6e6f69d255611c93f63c2hding
11748e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAQMatrixBufferType);
11758e9a21e730449c10cac6e6f69d255611c93f63c2hding
11768e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBuffer = (VAQMatrixBufferJPEG *)obj_buffer->buffer_data;
11778e9a21e730449c10cac6e6f69d255611c93f63c2hding
11788e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (0 != pBuffer->load_lum_quantiser_matrix) {
11798e9a21e730449c10cac6e6f69d255611c93f63c2hding        memcpy(pQMatrix->aui8LumaQuantParams,
11808e9a21e730449c10cac6e6f69d255611c93f63c2hding               pBuffer->lum_quantiser_matrix,
11818e9a21e730449c10cac6e6f69d255611c93f63c2hding               QUANT_TABLE_SIZE_BYTES);
11828e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11838e9a21e730449c10cac6e6f69d255611c93f63c2hding
11848e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (0 != pBuffer->load_chroma_quantiser_matrix) {
11858e9a21e730449c10cac6e6f69d255611c93f63c2hding        memcpy(pQMatrix->aui8ChromaQuantParams,
11868e9a21e730449c10cac6e6f69d255611c93f63c2hding               pBuffer->chroma_quantiser_matrix,
11878e9a21e730449c10cac6e6f69d255611c93f63c2hding               QUANT_TABLE_SIZE_BYTES);
11888e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11898e9a21e730449c10cac6e6f69d255611c93f63c2hding
11908e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(obj_buffer->buffer_data);
11918e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->buffer_data = NULL;
11928e9a21e730449c10cac6e6f69d255611c93f63c2hding
11938e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
11948e9a21e730449c10cac6e6f69d255611c93f63c2hding}
11958e9a21e730449c10cac6e6f69d255611c93f63c2hding
11968e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic VAStatus ProcessPictureParam(context_ENC_p ctx, object_buffer_p obj_buffer)
11978e9a21e730449c10cac6e6f69d255611c93f63c2hding{
11988e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
11998e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncPictureParameterBufferJPEG *pBuffer = NULL;
12008e9a21e730449c10cac6e6f69d255611c93f63c2hding    BUFFER_HEADER *pBufHeader = NULL;
12018e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx = ctx->jpeg_ctx;
12028e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
12038e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_ERRORCODE rc;
120488d0460822f8807666406c14662591167eed73a0hding
12058e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Check the input buffer */
12068e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAEncPictureParameterBufferType);
12078e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((obj_buffer->num_elements != 1) ||
12088e9a21e730449c10cac6e6f69d255611c93f63c2hding        (obj_buffer->size != sizeof(VAEncPictureParameterBufferJPEG))) {
12098e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
12108e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12118e9a21e730449c10cac6e6f69d255611c93f63c2hding
12128e9a21e730449c10cac6e6f69d255611c93f63c2hding
12138e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Lookup and get coded buffer */
12148e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBuffer = (VAEncPictureParameterBufferJPEG *)obj_buffer->buffer_data;
12158e9a21e730449c10cac6e6f69d255611c93f63c2hding
12168e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(ctx->ui16Width == pBuffer->picture_width);
12178e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(ctx->ui16FrameHeight == pBuffer->picture_height);
12188e9a21e730449c10cac6e6f69d255611c93f63c2hding
12198e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_buf->coded_buf = BUFFER(pBuffer->coded_buf);
12208e9a21e730449c10cac6e6f69d255611c93f63c2hding
12218e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(pBuffer);
12228e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->buffer_data = NULL;
12238e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->size = 0;
12248e9a21e730449c10cac6e6f69d255611c93f63c2hding
12258e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == ps_buf->coded_buf) {
12268e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "%s L%d Invalid coded buffer handle\n", __FUNCTION__, __LINE__);
12278e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_INVALID_BUFFER;
12288e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12298e9a21e730449c10cac6e6f69d255611c93f63c2hding
12308e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map coded buffer */
12318e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(ps_buf->coded_buf->psb_buffer, &jpeg_ctx->jpeg_coded_buf.pMemInfo);
12328e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
12338e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "ERROR: Map coded_buf failed!");
12348e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
12358e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12368e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx->jpeg_coded_buf.ui32Size = ps_buf->coded_buf->size;
12378e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx->jpeg_coded_buf.sLock = BUFFER_FREE;
12388e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx->jpeg_coded_buf.ui32BytesWritten = 0;
12398e9a21e730449c10cac6e6f69d255611c93f63c2hding
12408e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((jpeg_ctx->jpeg_coded_buf.ui32Size) < (9 + 6 + (4 * 3))) {
12418e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_INVALID_BUFFER;
12428e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12438e9a21e730449c10cac6e6f69d255611c93f63c2hding
12448e9a21e730449c10cac6e6f69d255611c93f63c2hding
12458e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Assign coded buffer to each scan */
12468e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Assign coded buffer to each scan\n");
12478e9a21e730449c10cac6e6f69d255611c93f63c2hding    AssignCodedDataBuffers(jpeg_ctx);
12488e9a21e730449c10cac6e6f69d255611c93f63c2hding
12498e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Coded buffer total size is %d,"
12508e9a21e730449c10cac6e6f69d255611c93f63c2hding                             "coded segment size per scan is %d\n",
125188d0460822f8807666406c14662591167eed73a0hding                             jpeg_ctx->jpeg_coded_buf.ui32Size, jpeg_ctx->ui32SizePerCodedBuffer);
12528e9a21e730449c10cac6e6f69d255611c93f63c2hding
12538e9a21e730449c10cac6e6f69d255611c93f63c2hding
12548e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Write JPEG headers to coded buffer */
12558e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Write JPEG Headers to coded buf\n");
12568e9a21e730449c10cac6e6f69d255611c93f63c2hding
12578e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader = (BUFFER_HEADER *)jpeg_ctx->jpeg_coded_buf.pMemInfo;
12588e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32BytesUsed = 0; /* Not include BUFFER_HEADER*/
12598e9a21e730449c10cac6e6f69d255611c93f63c2hding
12608e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = PrepareHeader(jpeg_ctx, &jpeg_ctx->jpeg_coded_buf, sizeof(BUFFER_HEADER), IMG_TRUE);
12618e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc != IMG_ERR_OK)
12628e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
12638e9a21e730449c10cac6e6f69d255611c93f63c2hding
12648e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32Reserved3 = PTG_JPEG_HEADER_MAX_SIZE;//Next coded buffer offset
12658e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32BytesUsed = jpeg_ctx->jpeg_coded_buf.ui32BytesWritten - sizeof(BUFFER_HEADER);
12668e9a21e730449c10cac6e6f69d255611c93f63c2hding
12678e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "sizeof(BUFFER_HEADER): %d, ui32BytesUsed :%d, ui32Reserved3: %d, ui32BytesWritten: %d\n",
12688e9a21e730449c10cac6e6f69d255611c93f63c2hding                             sizeof(BUFFER_HEADER), pBufHeader->ui32BytesUsed, pBufHeader->ui32Reserved3, jpeg_ctx->jpeg_coded_buf.ui32BytesWritten);
12698e9a21e730449c10cac6e6f69d255611c93f63c2hding
12708e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
12718e9a21e730449c10cac6e6f69d255611c93f63c2hding}
1272430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_RenderPicture(
12738e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context,
12748e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_buffer_p *buffers,
12758e9a21e730449c10cac6e6f69d255611c93f63c2hding    int num_buffers)
12768e9a21e730449c10cac6e6f69d255611c93f63c2hding{
12778e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
12788e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
12798e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
12808e9a21e730449c10cac6e6f69d255611c93f63c2hding
1281430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_RenderPicture\n");
12828e9a21e730449c10cac6e6f69d255611c93f63c2hding
12838e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < num_buffers; i++) {
12848e9a21e730449c10cac6e6f69d255611c93f63c2hding        object_buffer_p obj_buffer = buffers[i];
12858e9a21e730449c10cac6e6f69d255611c93f63c2hding
12868e9a21e730449c10cac6e6f69d255611c93f63c2hding        switch (obj_buffer->type) {
12878e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAQMatrixBufferType:
1288430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_RenderPicture got VAEncSliceParameterBufferType\n");
12898e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = ProcessQmatrixParam(ctx, obj_buffer);
12908e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
12918e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
12928e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAEncPictureParameterBufferType:
1293430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_RenderPicture got VAEncPictureParameterBufferType\n");
12948e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = ProcessPictureParam(ctx, obj_buffer);
12958e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
12968e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
12978e9a21e730449c10cac6e6f69d255611c93f63c2hding        default:
12988e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_UNKNOWN;
12998e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
13008e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
13018e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
13028e9a21e730449c10cac6e6f69d255611c93f63c2hding
13038e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
13048e9a21e730449c10cac6e6f69d255611c93f63c2hding}
13058e9a21e730449c10cac6e6f69d255611c93f63c2hding
1306430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_EndPicture(
13078e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
13088e9a21e730449c10cac6e6f69d255611c93f63c2hding{
13098e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16BCnt;
13108e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 rc = 0;
13118e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
13128e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32NoMCUsToEncode = 0;
13138e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32RemainMCUs = 0;
13148e9a21e730449c10cac6e6f69d255611c93f63c2hding
13158e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
13168e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p = ctx->jpeg_ctx;
1317430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = (tng_cmdbuf_p)ctx->obj_context->tng_cmdbuf;
13188e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
13198e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
13208e9a21e730449c10cac6e6f69d255611c93f63c2hding
1321430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_EndPicture\n");
13228e9a21e730449c10cac6e6f69d255611c93f63c2hding
13238e9a21e730449c10cac6e6f69d255611c93f63c2hding    IssueQmatix(jpeg_ctx_p);
13248e9a21e730449c10cac6e6f69d255611c93f63c2hding
13258e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Compute the next scan to be sent */
13268e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32RemainMCUs = jpeg_ctx_p->sScan_Encode_Info.ui32NumberMCUsToEncode;
13278e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32RemainMCUs: %d\n", ui32RemainMCUs);
13288e9a21e730449c10cac6e6f69d255611c93f63c2hding
13298e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui16BCnt = 0; (ui16BCnt < jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers)
13308e9a21e730449c10cac6e6f69d255611c93f63c2hding         && (jpeg_ctx_p->sScan_Encode_Info.ui16SScan > 0); ui16BCnt++) {
13318e9a21e730449c10cac6e6f69d255611c93f63c2hding
13328e9a21e730449c10cac6e6f69d255611c93f63c2hding        jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber = jpeg_ctx_p->sScan_Encode_Info.ui16SScan--;
13338e9a21e730449c10cac6e6f69d255611c93f63c2hding        jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].i8PipeNumber =
13348153ccc65f30ec7cecee4c6a4f0e72f7c402edbdSUN,Jing            (1 == jpeg_ctx_p->NumCores) ? 0 : ((ui16BCnt+1) % jpeg_ctx_p->NumCores);
13358e9a21e730449c10cac6e6f69d255611c93f63c2hding
13368e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (jpeg_ctx_p->sScan_Encode_Info.ui16SScan > 0) {
13378e9a21e730449c10cac6e6f69d255611c93f63c2hding            ui32NoMCUsToEncode = jpeg_ctx_p->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
13388e9a21e730449c10cac6e6f69d255611c93f63c2hding        } else {
13398e9a21e730449c10cac6e6f69d255611c93f63c2hding            // Final scan, may need fewer MCUs than buffer size, calculate the remainder
13408e9a21e730449c10cac6e6f69d255611c93f63c2hding            ui32NoMCUsToEncode = ui32RemainMCUs;
13418e9a21e730449c10cac6e6f69d255611c93f63c2hding            jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].i8PipeNumber = 0;
13428e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
13438e9a21e730449c10cac6e6f69d255611c93f63c2hding
13448e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32NoMCUsToEncode: %d\n", ui32NoMCUsToEncode);
13458e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Send scan to MTX
13468e9a21e730449c10cac6e6f69d255611c93f63c2hding        rc = IssueBufferToHW(jpeg_ctx_p, ui16BCnt,
13478e9a21e730449c10cac6e6f69d255611c93f63c2hding                             jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].i8PipeNumber,
13488e9a21e730449c10cac6e6f69d255611c93f63c2hding                             ui32NoMCUsToEncode);
13498e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rc != IMG_ERR_OK) {
13508e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_UNKNOWN;
13518e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
13528e9a21e730449c10cac6e6f69d255611c93f63c2hding            return vaStatus;
13538e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
13548e9a21e730449c10cac6e6f69d255611c93f63c2hding
13558e9a21e730449c10cac6e6f69d255611c93f63c2hding        ui32RemainMCUs -= ui32NoMCUsToEncode;
13568e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
13578e9a21e730449c10cac6e6f69d255611c93f63c2hding
13588e9a21e730449c10cac6e6f69d255611c93f63c2hding
13598e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(&cmdbuf->jpeg_pic_params);
13608e9a21e730449c10cac6e6f69d255611c93f63c2hding    cmdbuf->jpeg_pic_params_p = NULL;
13618e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(&cmdbuf->jpeg_header_mem);
13628e9a21e730449c10cac6e6f69d255611c93f63c2hding    cmdbuf->jpeg_header_mem_p = NULL;
13638e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(ps_buf->coded_buf->psb_buffer);
13648e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.pMemInfo = NULL;
13658e9a21e730449c10cac6e6f69d255611c93f63c2hding
13668e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(&(ctx->bufs_writeback));
13678e9a21e730449c10cac6e6f69d255611c93f63c2hding
13688e9a21e730449c10cac6e6f69d255611c93f63c2hding
1369430ce5c32361119ed81a62c918be6afbdd053eddhding    //tng__trace_cmdbuf(cmdbuf);
13708e9a21e730449c10cac6e6f69d255611c93f63c2hding
1371430ce5c32361119ed81a62c918be6afbdd053eddhding    if (tng_context_flush_cmdbuf(ctx->obj_context)) {
13728e9a21e730449c10cac6e6f69d255611c93f63c2hding        vaStatus = VA_STATUS_ERROR_UNKNOWN;
13738e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
13748e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
13758e9a21e730449c10cac6e6f69d255611c93f63c2hding
13768e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->obj_context->frame_count++;
13778e9a21e730449c10cac6e6f69d255611c93f63c2hding    return VA_STATUS_SUCCESS;
13788e9a21e730449c10cac6e6f69d255611c93f63c2hding}
13798e9a21e730449c10cac6e6f69d255611c93f63c2hding
13808e9a21e730449c10cac6e6f69d255611c93f63c2hding/* Add Restart interval termination (RSTm)to coded buf 1 ~ NumCores-1*/
1381430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic inline VAStatus tng_OutputResetIntervalToCB(IMG_UINT8 *pui8Buf, IMG_UINT8 ui8_marker)
13828e9a21e730449c10cac6e6f69d255611c93f63c2hding{
13838e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == pui8Buf)
13848e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
13858e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*Refer to CCITT Rec. T.81 (1992 E), B.2.1*/
13868e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*RSTm: Restart marker conditional marker which is placed between
13878e9a21e730449c10cac6e6f69d255611c93f63c2hding     * entropy-coded segments only if restartis enabled. There are 8 unique
13888e9a21e730449c10cac6e6f69d255611c93f63c2hding     * restart markers (m = 0 - 7) which repeat in sequence from 0 to 7, starting with
13898e9a21e730449c10cac6e6f69d255611c93f63c2hding     * zero for each scan, to provide a modulo 8 restart interval count*/
13908e9a21e730449c10cac6e6f69d255611c93f63c2hding
13918e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui8Buf++ = 0xff;
13928e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui8Buf = (ui8_marker & 0x7) | 0xD0;
13938e9a21e730449c10cac6e6f69d255611c93f63c2hding
13948e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
13958e9a21e730449c10cac6e6f69d255611c93f63c2hding}
13968e9a21e730449c10cac6e6f69d255611c93f63c2hding
1397430ce5c32361119ed81a62c918be6afbdd053eddhdingVAStatus tng_jpeg_AppendMarkers(object_context_p obj_context, void *raw_coded_buf)
13988e9a21e730449c10cac6e6f69d255611c93f63c2hding{
13998e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
14008e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p = ctx->jpeg_ctx;
14018e9a21e730449c10cac6e6f69d255611c93f63c2hding
14028e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16BCnt;
14038e9a21e730449c10cac6e6f69d255611c93f63c2hding    BUFFER_HEADER* pBufHeader;
14048e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW s_streamW;
14058e9a21e730449c10cac6e6f69d255611c93f63c2hding    void *pSegStart = raw_coded_buf;
14068e9a21e730449c10cac6e6f69d255611c93f63c2hding
14078e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pSegStart == NULL) {
14088e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
14098e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
14108e9a21e730449c10cac6e6f69d255611c93f63c2hding
14118e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader = (BUFFER_HEADER *)pSegStart;
14128e9a21e730449c10cac6e6f69d255611c93f63c2hding
14138e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of Coded buffers %d, Per Coded Buffer size : %d\n",
14148e9a21e730449c10cac6e6f69d255611c93f63c2hding                             jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers,
14158e9a21e730449c10cac6e6f69d255611c93f63c2hding                             jpeg_ctx_p->ui32SizePerCodedBuffer);
14168e9a21e730449c10cac6e6f69d255611c93f63c2hding
14178e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*The first part of coded buffer contains JPEG headers*/
14188e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32Reserved3 = PTG_JPEG_HEADER_MAX_SIZE;
14198e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten = 0;
14208e9a21e730449c10cac6e6f69d255611c93f63c2hding
14218e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui16BCnt = 0;
14228e9a21e730449c10cac6e6f69d255611c93f63c2hding         ui16BCnt < jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers;
14238e9a21e730449c10cac6e6f69d255611c93f63c2hding         ui16BCnt++) {
14248e9a21e730449c10cac6e6f69d255611c93f63c2hding        pBufHeader = (BUFFER_HEADER *)pSegStart;
14258e9a21e730449c10cac6e6f69d255611c93f63c2hding        pBufHeader->ui32Reserved3 =
14268e9a21e730449c10cac6e6f69d255611c93f63c2hding            PTG_JPEG_HEADER_MAX_SIZE + jpeg_ctx_p->ui32SizePerCodedBuffer * ui16BCnt ;
14278e9a21e730449c10cac6e6f69d255611c93f63c2hding
14288e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Coded Buffer Part %d, size %d, next part offset: %d\n",
14298e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 ui16BCnt, pBufHeader->ui32BytesUsed, pBufHeader->ui32Reserved3);
14308e9a21e730449c10cac6e6f69d255611c93f63c2hding
14318e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (ui16BCnt > 0) {
14328e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "Append 2 bytes Reset Interval %d "
14338e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     "to Coded Buffer Part %d\n", ui16BCnt - 1, ui16BCnt);
14348e9a21e730449c10cac6e6f69d255611c93f63c2hding
14358e9a21e730449c10cac6e6f69d255611c93f63c2hding            // OUTPUT RESTART INTERVAL TO CODED BUFFER
1436430ce5c32361119ed81a62c918be6afbdd053eddhding            tng_OutputResetIntervalToCB(
14378e9a21e730449c10cac6e6f69d255611c93f63c2hding                (IMG_UINT8 *)(pSegStart + sizeof(BUFFER_HEADER) + pBufHeader->ui32BytesUsed),
14388e9a21e730449c10cac6e6f69d255611c93f63c2hding                ui16BCnt - 1);
14398e9a21e730449c10cac6e6f69d255611c93f63c2hding
14408e9a21e730449c10cac6e6f69d255611c93f63c2hding            pBufHeader->ui32BytesUsed += 2;
14418e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
14428e9a21e730449c10cac6e6f69d255611c93f63c2hding
14438e9a21e730449c10cac6e6f69d255611c93f63c2hding        jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten += pBufHeader->ui32BytesUsed;
14448e9a21e730449c10cac6e6f69d255611c93f63c2hding        pSegStart = raw_coded_buf + pBufHeader->ui32Reserved3;
14458e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
14468e9a21e730449c10cac6e6f69d255611c93f63c2hding
14478e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader = (BUFFER_HEADER *)pSegStart;
14488e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32Reserved3 = 0; /*Last Part of Coded Buffer*/
14498e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten += pBufHeader->ui32BytesUsed;
14508e9a21e730449c10cac6e6f69d255611c93f63c2hding
14518e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Coded Buffer Part %d, size %d, next part offset: %d\n",
14528e9a21e730449c10cac6e6f69d255611c93f63c2hding                             ui16BCnt, pBufHeader->ui32BytesUsed, pBufHeader->ui32Reserved3);
14538e9a21e730449c10cac6e6f69d255611c93f63c2hding
14548e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Buffer = pSegStart;
14558e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Offset = (sizeof(BUFFER_HEADER) + pBufHeader->ui32BytesUsed);
14568e9a21e730449c10cac6e6f69d255611c93f63c2hding
14578e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, END_OF_IMAGE);
14588e9a21e730449c10cac6e6f69d255611c93f63c2hding
14598e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32BytesUsed += 2;
14608e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten += 2;
14618e9a21e730449c10cac6e6f69d255611c93f63c2hding
14628e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Add two bytes to last part of coded buffer,"
14638e9a21e730449c10cac6e6f69d255611c93f63c2hding                             " total: %d\n", jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten);
14648e9a21e730449c10cac6e6f69d255611c93f63c2hding    return VA_STATUS_SUCCESS;
14658e9a21e730449c10cac6e6f69d255611c93f63c2hding}
14668e9a21e730449c10cac6e6f69d255611c93f63c2hding
1467430ce5c32361119ed81a62c918be6afbdd053eddhdingstruct format_vtable_s tng_JPEGES_vtable = {
14688e9a21e730449c10cac6e6f69d255611c93f63c2hdingqueryConfigAttributes:
1469430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_QueryConfigAttributes,
14708e9a21e730449c10cac6e6f69d255611c93f63c2hdingvalidateConfig:
1471430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_ValidateConfig,
14728e9a21e730449c10cac6e6f69d255611c93f63c2hdingcreateContext:
1473430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_CreateContext,
14748e9a21e730449c10cac6e6f69d255611c93f63c2hdingdestroyContext:
1475430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_DestroyContext,
14768e9a21e730449c10cac6e6f69d255611c93f63c2hdingbeginPicture:
1477430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_BeginPicture,
14788e9a21e730449c10cac6e6f69d255611c93f63c2hdingrenderPicture:
1479430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_RenderPicture,
14808e9a21e730449c10cac6e6f69d255611c93f63c2hdingendPicture:
1481430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_EndPicture
14828e9a21e730449c10cac6e6f69d255611c93f63c2hding};
1483