tng_jpegES.c revision 88d0460822f8807666406c14662591167eed73a0
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
908e9a21e730449c10cac6e6f69d255611c93f63c2hding#define PTG_JPEG_MAX_MCU_PER_SCAN (0x3D09)
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");
3038e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < 128 ; i++) {
304430ce5c32361119ed81a62c918be6afbdd053eddhding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "%d \t", *((unsigned char *)ctx->obj_context->tng_cmdbuf->jpeg_pic_params_p + i));
3058e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (((i + 1) % 8) == 0)
3068e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "\n");
3078e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3088e9a21e730449c10cac6e6f69d255611c93f63c2hding
30988d0460822f8807666406c14662591167eed73a0hding    tng_cmdbuf_insert_command_package(ctx->obj_context,
3108e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
3118e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_SETQUANT,
31288d0460822f8807666406c14662591167eed73a0hding                                      0,
313430ce5c32361119ed81a62c918be6afbdd053eddhding                                      &(ctx->obj_context->tng_cmdbuf->jpeg_pic_params),
3148e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0);
3158e9a21e730449c10cac6e6f69d255611c93f63c2hding}
3168e9a21e730449c10cac6e6f69d255611c93f63c2hding
3178e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void InitializeJpegEncode(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
3188e9a21e730449c10cac6e6f69d255611c93f63c2hding{
3198e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
3208e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16_width;
3218e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16_height;
3228e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32UpperLimit;
3238e9a21e730449c10cac6e6f69d255611c93f63c2hding
3248e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*********************************************************************/
3258e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Determine the horizonal and the vertical sampling frequency of    */
3268e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* each of components in the image                                   */
3278e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*********************************************************************/
3288e9a21e730449c10cac6e6f69d255611c93f63c2hding
3298e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui16_width = ctx->ui16Width;
3308e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui16_height = ctx->ui16FrameHeight; //pTFrame->height isn't the real height of image, because vaCreateSurface has made it aligned with 32
3318e9a21e730449c10cac6e6f69d255611c93f63c2hding
3328e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pJPEGContext->eFormat) {
3338e9a21e730449c10cac6e6f69d255611c93f63c2hding    case IMG_CODEC_PL12:
3348e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
3358e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32WidthBlocks = 16;
3368e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32HeightBlocks = 16;
3378e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32XLimit = ui16_width;
3388e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32YLimit = ui16_height;
3398e9a21e730449c10cac6e6f69d255611c93f63c2hding
3408e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32WidthBlocks = 8;
3418e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32HeightBlocks = 8;
3428e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32XLimit = ui16_width >> 1;
3438e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32YLimit = ui16_height >> 1;
3448e9a21e730449c10cac6e6f69d255611c93f63c2hding
3458e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32WidthBlocks = 8;
3468e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32HeightBlocks = 8;
3478e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32XLimit = ui16_width >> 1;
3488e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32YLimit = ui16_height >> 1;
3498e9a21e730449c10cac6e6f69d255611c93f63c2hding
3508e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
3518e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3528e9a21e730449c10cac6e6f69d255611c93f63c2hding
3538e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (ISCHROMAINTERLEAVED(pJPEGContext->eFormat)) {
3548e9a21e730449c10cac6e6f69d255611c93f63c2hding    case C_INTERLEAVE:
3558e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
3568e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Chroma format is byte interleaved, as the engine runs using planar colour surfaces we need
3578e9a21e730449c10cac6e6f69d255611c93f63c2hding        // to fool the engine into offsetting by 16 instead of 8
3588e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32WidthBlocks +=
3598e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->MCUComponent[2].ui32WidthBlocks;
3608e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[1].ui32XLimit +=
3618e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->MCUComponent[2].ui32XLimit;
3628e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[2].ui32XLimit =
3638e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->MCUComponent[2].ui32YLimit =
3648e9a21e730449c10cac6e6f69d255611c93f63c2hding                pJPEGContext->MCUComponent[2].ui32WidthBlocks =
3658e9a21e730449c10cac6e6f69d255611c93f63c2hding                    pJPEGContext->MCUComponent[2].ui32HeightBlocks = 0;
3668e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
3678e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
3688e9a21e730449c10cac6e6f69d255611c93f63c2hding
3698e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX =
3708e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->MCUComponent[0].ui32XLimit +
3718e9a21e730449c10cac6e6f69d255611c93f63c2hding         (pJPEGContext->MCUComponent[0].ui32WidthBlocks - 1)) /
3728e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32WidthBlocks;
3738e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY =
3748e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->MCUComponent[0].ui32YLimit +
3758e9a21e730449c10cac6e6f69d255611c93f63c2hding         (pJPEGContext->MCUComponent[0].ui32HeightBlocks - 1)) /
3768e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->MCUComponent[0].ui32HeightBlocks;
3778e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncode =
3788e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX *
3798e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY;
3808e9a21e730449c10cac6e6f69d255611c93f63c2hding
3818e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of X MCUs: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX);
3828e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of Y MCUs: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY);
3838e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of total MCUs: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncode);
3848e9a21e730449c10cac6e6f69d255611c93f63c2hding
3858e9a21e730449c10cac6e6f69d255611c93f63c2hding
3868e9a21e730449c10cac6e6f69d255611c93f63c2hding    // 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
3878e9a21e730449c10cac6e6f69d255611c93f63c2hding    // (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)
3888e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan =
3898e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsY + (pJPEGContext->NumCores - 1)) / pJPEGContext->NumCores;
3908e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan *=
3918e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX;
3928e9a21e730449c10cac6e6f69d255611c93f63c2hding
3938e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of MCUs per core: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
3948e9a21e730449c10cac6e6f69d255611c93f63c2hding
3958e9a21e730449c10cac6e6f69d255611c93f63c2hding
3968e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Limit the scan size to maximum useable (due to it being used as the 16 bit field for Restart Intervals) = 0xFFFF MCUs
3978e9a21e730449c10cac6e6f69d255611c93f63c2hding    // In reality, worst case allocatable bytes is less than this, something around 0x159739C == 0x4b96 MCUs = 139 x 139 MCUS = 2224 * 2224 pixels, approx.
3988e9a21e730449c10cac6e6f69d255611c93f63c2hding    // 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)
3998e9a21e730449c10cac6e6f69d255611c93f63c2hding    // If more MCUs are required, then the image will be automatically encoded with multiple scans on the same pipes
4008e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32UpperLimit = PTG_JPEG_MAX_MCU_PER_SCAN / pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX;
4018e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32UpperLimit *= pJPEGContext->sScan_Encode_Info.ui32NumberMCUsX;
4028e9a21e730449c10cac6e6f69d255611c93f63c2hding
4038e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan > ui32UpperLimit) {
4048e9a21e730449c10cac6e6f69d255611c93f63c2hding        // 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)
4058e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan = ui32UpperLimit;
4068e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
4078e9a21e730449c10cac6e6f69d255611c93f63c2hding
4088e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of MCUs per scan: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
4098e9a21e730449c10cac6e6f69d255611c93f63c2hding
4108e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Need to set up CB Output slicenumber to equal number of slices required to encode image
4118e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Set current CB scan to maximum scan number (will count down as scans are output)
4128e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui16ScansInImage =
4138e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncode +
4148e9a21e730449c10cac6e6f69d255611c93f63c2hding         (pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan - 1)) /
4158e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
4168e9a21e730449c10cac6e6f69d255611c93f63c2hding
4178e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers =
4188e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui16ScansInImage;
4198e9a21e730449c10cac6e6f69d255611c93f63c2hding
4208e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Scans in image: %d\n", pJPEGContext->sScan_Encode_Info.ui16ScansInImage);
4218e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of coded buffers: %d\n", pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers);
4228e9a21e730449c10cac6e6f69d255611c93f63c2hding
4238e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
4248e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4258e9a21e730449c10cac6e6f69d255611c93f63c2hding
4268e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void AssignCodedDataBuffers(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
4278e9a21e730449c10cac6e6f69d255611c93f63c2hding{
4288e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 ui8Loop;
4298e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->ui32SizePerCodedBuffer =
4308e9a21e730449c10cac6e6f69d255611c93f63c2hding        (pJPEGContext->jpeg_coded_buf.ui32Size - PTG_JPEG_HEADER_MAX_SIZE) /
4318e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers;
432e26d82700f9514cc175d2b54733ebdeb8824922ehding    pJPEGContext->ui32SizePerCodedBuffer &= ~0xf;
4338e9a21e730449c10cac6e6f69d255611c93f63c2hding
4348e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset((void *)pJPEGContext->sScan_Encode_Info.aBufferTable, 0x0,
4358e9a21e730449c10cac6e6f69d255611c93f63c2hding           sizeof(TOPAZHP_JPEG_BUFFER_INFO)*pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers);
4368e9a21e730449c10cac6e6f69d255611c93f63c2hding
4378e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "jpeg_coded_buf.pMemInfo: 0x%x\n", (unsigned int)(pJPEGContext->jpeg_coded_buf.pMemInfo));
4388e9a21e730449c10cac6e6f69d255611c93f63c2hding
4398e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui8Loop = 0 ; ui8Loop < pJPEGContext->sScan_Encode_Info.ui8NumberOfCodedBuffers; ui8Loop++) {
4408e9a21e730449c10cac6e6f69d255611c93f63c2hding        //pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes = DATA_BUFFER_SIZE(pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
4418e9a21e730449c10cac6e6f69d255611c93f63c2hding        //pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes = (pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes+sizeof(BUFFER_HEADER)) + 3 & ~3;
4428e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferUsedBytes = 0;
4438e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].i8PipeNumber = 0; // Indicates buffer is idle
4448e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui16ScanNumber = 0; // Indicates buffer is idle
4458e9a21e730449c10cac6e6f69d255611c93f63c2hding
4468e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo = (void *)
4478e9a21e730449c10cac6e6f69d255611c93f63c2hding                (pJPEGContext->jpeg_coded_buf.pMemInfo + PTG_JPEG_HEADER_MAX_SIZE +
4488e9a21e730449c10cac6e6f69d255611c93f63c2hding                 ui8Loop * pJPEGContext->ui32SizePerCodedBuffer);
4498e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "aBufferTable[%d].pMemInfo: 0x%x\n", ui8Loop,
4508e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 (unsigned int)(pJPEGContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo));
4518e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
4528e9a21e730449c10cac6e6f69d255611c93f63c2hding
4538e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
4548e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4558e9a21e730449c10cac6e6f69d255611c93f63c2hding
4568e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void SetSetupInterface(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
4578e9a21e730449c10cac6e6f69d255611c93f63c2hding{
4588e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
4598e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
4608e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem_size *ps_mem_size = &(ctx->ctx_mem_size);
4618e9a21e730449c10cac6e6f69d255611c93f63c2hding
46288d0460822f8807666406c14662591167eed73a0hding    tng_cmdbuf_set_phys(pJPEGContext->pMTXWritebackMemory->apWritebackRegions, WB_FIFO_SIZE,
4638e9a21e730449c10cac6e6f69d255611c93f63c2hding                        &(ctx->bufs_writeback), 0, ps_mem_size->writeback);
4648e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4658e9a21e730449c10cac6e6f69d255611c93f63c2hding
4668e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void IssueSetupInterface(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
4678e9a21e730449c10cac6e6f69d255611c93f63c2hding{
46888d0460822f8807666406c14662591167eed73a0hding    int i;
4698e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
4708e9a21e730449c10cac6e6f69d255611c93f63c2hding
4718e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(NULL != pJPEGContext->pMTXSetup);
4728e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Issue SetupInterface\n");
4738e9a21e730449c10cac6e6f69d255611c93f63c2hding
47488d0460822f8807666406c14662591167eed73a0hding    for (i = 0; i < WB_FIFO_SIZE; i++) {
47588d0460822f8807666406c14662591167eed73a0hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "apWritebackRegions[%d]: 0x%x\n", i,
47688d0460822f8807666406c14662591167eed73a0hding                                 pJPEGContext->pMTXWritebackMemory->apWritebackRegions[i]);
47788d0460822f8807666406c14662591167eed73a0hding    }
47888d0460822f8807666406c14662591167eed73a0hding
47988d0460822f8807666406c14662591167eed73a0hding    tng_cmdbuf_insert_command_package(ctx->obj_context,
4808e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
4818e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_SETUP_INTERFACE,
48288d0460822f8807666406c14662591167eed73a0hding                                      0,
483430ce5c32361119ed81a62c918be6afbdd053eddhding                                      &(ctx->obj_context->tng_cmdbuf->jpeg_header_interface_mem),
4848e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0);
4858e9a21e730449c10cac6e6f69d255611c93f63c2hding}
4868e9a21e730449c10cac6e6f69d255611c93f63c2hding
4878e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_ERRORCODE SetMTXSetup(
4888e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
4898e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_surface_p pTFrame)
4908e9a21e730449c10cac6e6f69d255611c93f63c2hding{
4918e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 srf_buf_offset;
4928e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
493430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
4948e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
4958e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem_size *ps_mem_size = &(ctx->ctx_mem_size);
4968e9a21e730449c10cac6e6f69d255611c93f63c2hding
4978e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->pMTXSetup->ui32ComponentsInScan = MTX_MAX_COMPONENTS;
4988e9a21e730449c10cac6e6f69d255611c93f63c2hding
4998e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pJPEGContext->eFormat) {
5008e9a21e730449c10cac6e6f69d255611c93f63c2hding    case IMG_CODEC_PL12:
5018e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
5028e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->pMTXSetup->ComponentPlane[0].ui32Stride = pTFrame->psb_surface->stride;
5038e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->pMTXSetup->ComponentPlane[1].ui32Stride = pTFrame->psb_surface->stride;
5048e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->pMTXSetup->ComponentPlane[2].ui32Stride = pTFrame->psb_surface->stride;
5058e9a21e730449c10cac6e6f69d255611c93f63c2hding
506e26d82700f9514cc175d2b54733ebdeb8824922ehding        pJPEGContext->pMTXSetup->ComponentPlane[0].ui32Height = pJPEGContext->MCUComponent[0].ui32YLimit;
507e26d82700f9514cc175d2b54733ebdeb8824922ehding        pJPEGContext->pMTXSetup->ComponentPlane[1].ui32Height = pJPEGContext->MCUComponent[0].ui32YLimit / 2;
508e26d82700f9514cc175d2b54733ebdeb8824922ehding        pJPEGContext->pMTXSetup->ComponentPlane[2].ui32Height = pJPEGContext->MCUComponent[0].ui32YLimit / 2;
5098e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5108e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
5118e9a21e730449c10cac6e6f69d255611c93f63c2hding
5128e9a21e730449c10cac6e6f69d255611c93f63c2hding    srf_buf_offset = pTFrame->psb_surface->buf.buffer_ofs;
5138e9a21e730449c10cac6e6f69d255611c93f63c2hding    RELOC_JPEG_PIC_PARAMS_PTG(&pJPEGContext->pMTXSetup->ComponentPlane[0].ui32PhysAddr, srf_buf_offset, &pTFrame->psb_surface->buf);
5148e9a21e730449c10cac6e6f69d255611c93f63c2hding    switch (pJPEGContext->eFormat) {
5158e9a21e730449c10cac6e6f69d255611c93f63c2hding    case IMG_CODEC_PL12:
5168e9a21e730449c10cac6e6f69d255611c93f63c2hding        RELOC_JPEG_PIC_PARAMS_PTG(&pJPEGContext->pMTXSetup->ComponentPlane[1].ui32PhysAddr,
5178e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height,
5188e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  &pTFrame->psb_surface->buf);
5198e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Byte interleaved surface, so need to force chroma to use single surface by fooling it into
5208e9a21e730449c10cac6e6f69d255611c93f63c2hding        //thinking it's dealing with standard 8x8 planaerblocks
5218e9a21e730449c10cac6e6f69d255611c93f63c2hding        RELOC_JPEG_PIC_PARAMS_PTG(&pJPEGContext->pMTXSetup->ComponentPlane[2].ui32PhysAddr,
5228e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height + 8,
5238e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  &pTFrame->psb_surface->buf);
5248e9a21e730449c10cac6e6f69d255611c93f63c2hding        break;
5258e9a21e730449c10cac6e6f69d255611c93f63c2hding    default:
5268e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, " Not supported FOURCC %x!\n", pJPEGContext->eFormat);
5278e9a21e730449c10cac6e6f69d255611c93f63c2hding        return IMG_ERR_INVALID_CONTEXT;
5288e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
5298e9a21e730449c10cac6e6f69d255611c93f63c2hding
5308e9a21e730449c10cac6e6f69d255611c93f63c2hding    memcpy((void *)pJPEGContext->pMTXSetup->MCUComponent,
5318e9a21e730449c10cac6e6f69d255611c93f63c2hding           (void *)pJPEGContext->MCUComponent,
5328e9a21e730449c10cac6e6f69d255611c93f63c2hding           sizeof(pJPEGContext->MCUComponent));
5338e9a21e730449c10cac6e6f69d255611c93f63c2hding
5348e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->pMTXSetup->ui32TableA = 0;
5358e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->pMTXSetup->ui16DataInterleaveStatus = ISCHROMAINTERLEAVED(pJPEGContext->eFormat);
53688d0460822f8807666406c14662591167eed73a0hding    pJPEGContext->pMTXSetup->ui16MaxPipes = (IMG_UINT16)pJPEGContext->NumCores;
5378e9a21e730449c10cac6e6f69d255611c93f63c2hding
5388e9a21e730449c10cac6e6f69d255611c93f63c2hding    return IMG_ERR_OK;
5398e9a21e730449c10cac6e6f69d255611c93f63c2hding}
5408e9a21e730449c10cac6e6f69d255611c93f63c2hding
5418e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void IssueMTXSetup(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
5428e9a21e730449c10cac6e6f69d255611c93f63c2hding{
5438e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
5448e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)pJPEGContext->ctx;
5458e9a21e730449c10cac6e6f69d255611c93f63c2hding
5468e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Dump MTX setup data for debug */
5478e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(NULL != pJPEGContext->pMTXSetup);
5488e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Issue MTX setup data\n");
5498e9a21e730449c10cac6e6f69d255611c93f63c2hding
5508e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < pJPEGContext->pMTXSetup->ui32ComponentsInScan; i++) {
5518e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "ComponentPlane[%d]: 0x%x, %d, %d\n", i,
5528e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->ComponentPlane[i].ui32PhysAddr,
5538e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->ComponentPlane[i].ui32Stride,
5548e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->ComponentPlane[i].ui32Height);
5558e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "MCUComponent[%d]: %d, %d, %d, %d\n", i,
5568e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32WidthBlocks,
5578e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32HeightBlocks,
5588e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32XLimit,
5598e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 pJPEGContext->pMTXSetup->MCUComponent[i].ui32YLimit);
5608e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
5618e9a21e730449c10cac6e6f69d255611c93f63c2hding
5628e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32ComponentsInScan: %d\n", pJPEGContext->pMTXSetup->ui32ComponentsInScan);
5638e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32TableA: %d\n", pJPEGContext->pMTXSetup->ui32TableA);
5648e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16DataInterleaveStatus: %d\n", pJPEGContext->pMTXSetup->ui16DataInterleaveStatus);
5658e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16MaxPipes: %d\n", pJPEGContext->pMTXSetup->ui16MaxPipes);
5668e9a21e730449c10cac6e6f69d255611c93f63c2hding
56788d0460822f8807666406c14662591167eed73a0hding    tng_cmdbuf_insert_command_package(ctx->obj_context,
5688e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
5698e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_SETUP,
57088d0460822f8807666406c14662591167eed73a0hding                                      0,
571430ce5c32361119ed81a62c918be6afbdd053eddhding                                      &(ctx->obj_context->tng_cmdbuf->jpeg_header_mem),
5728e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0);
5738e9a21e730449c10cac6e6f69d255611c93f63c2hding
5748e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
5758e9a21e730449c10cac6e6f69d255611c93f63c2hding}
5768e9a21e730449c10cac6e6f69d255611c93f63c2hding
5778e9a21e730449c10cac6e6f69d255611c93f63c2hding
5788e9a21e730449c10cac6e6f69d255611c93f63c2hding
5798e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void fPutBitsToBuffer(STREAMTYPEW *BitStream, IMG_UINT8 NoOfBytes, IMG_UINT32 ActualBits)
5808e9a21e730449c10cac6e6f69d255611c93f63c2hding{
5818e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 ui8Lp;
5828e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 *pui8S;
5838e9a21e730449c10cac6e6f69d255611c93f63c2hding
5848e9a21e730449c10cac6e6f69d255611c93f63c2hding    pui8S = (IMG_UINT8 *)BitStream->Buffer;
5858e9a21e730449c10cac6e6f69d255611c93f63c2hding    pui8S += BitStream->Offset;
5868e9a21e730449c10cac6e6f69d255611c93f63c2hding
5878e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui8Lp = NoOfBytes; ui8Lp > 0; ui8Lp--)
5888e9a21e730449c10cac6e6f69d255611c93f63c2hding        *(pui8S++) = ((IMG_UINT8 *) &ActualBits)[ui8Lp-1];
5898e9a21e730449c10cac6e6f69d255611c93f63c2hding
5908e9a21e730449c10cac6e6f69d255611c93f63c2hding    BitStream->Offset += NoOfBytes;
5918e9a21e730449c10cac6e6f69d255611c93f63c2hding}
5928e9a21e730449c10cac6e6f69d255611c93f63c2hding
5938e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 EncodeMarkerSegment(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
5948e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      IMG_UINT8 *puc_stream_buff, IMG_BOOL bIncludeHuffmanTables)
5958e9a21e730449c10cac6e6f69d255611c93f63c2hding{
5968e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW s_streamW;
5978e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 uc_i;
5988e9a21e730449c10cac6e6f69d255611c93f63c2hding
5998e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Offset = 0;
6008e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Buffer = puc_stream_buff;
6018e9a21e730449c10cac6e6f69d255611c93f63c2hding
6028e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the start of image marker */
6038e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, START_OF_IMAGE);
6048e9a21e730449c10cac6e6f69d255611c93f63c2hding
6058e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the quantization table for luminance into the stream */
6068e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
6078e9a21e730449c10cac6e6f69d255611c93f63c2hding
6088e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 3, LQPQ << 4); // 20 bits = LQPQ, 4 bits = 0 (Destination identifier for the luminance quantizer tables)
6098e9a21e730449c10cac6e6f69d255611c93f63c2hding
6108e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_ASSERT(PELS_IN_BLOCK <= QUANT_TABLE_SIZE_BYTES);
6118e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
6128e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Write zigzag ordered luma quantization values to our JPEG header
6138e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, pJPEGContext->psTablesBlock->aui8LumaQuantParams[gZigZag[uc_i]]);
6148e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6158e9a21e730449c10cac6e6f69d255611c93f63c2hding
6168e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the quantization table for chrominance into the stream */
6178e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
6188e9a21e730449c10cac6e6f69d255611c93f63c2hding
6198e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 3, (LQPQ << 4) | 1); // 20 bits = LQPQ, 4 bits = 1 (Destination identifier for the chrominance quantizer tables)
6208e9a21e730449c10cac6e6f69d255611c93f63c2hding
6218e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
6228e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Write zigzag ordered chroma quantization values to our JPEG header
6238e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, pJPEGContext->psTablesBlock->aui8ChromaQuantParams[gZigZag[uc_i]]);
6248e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6258e9a21e730449c10cac6e6f69d255611c93f63c2hding
6268e9a21e730449c10cac6e6f69d255611c93f63c2hding
6278e9a21e730449c10cac6e6f69d255611c93f63c2hding
6288e9a21e730449c10cac6e6f69d255611c93f63c2hding
6298e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (bIncludeHuffmanTables) {
6308e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for luminance dc coeffs */
6318e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Write the DHT Marker */
6328e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6338e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_DC);
6348e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
6358e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaDc[uc_i]);
6368e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6378e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for luminance ac coeffs */
6388e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Write the DHT Marker */
6398e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6408e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_AC);
6418e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
6428e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaAc[uc_i]);
6438e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6448e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for chrominance dc coeffs */
6458e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6468e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_DC);
6478e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
6488e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaDc[uc_i]);
6498e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6508e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Writing the huffman tables for luminance ac coeffs */
6518e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Write the DHT Marker */
6528e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
6538e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, LH_AC);
6548e9a21e730449c10cac6e6f69d255611c93f63c2hding        for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
6558e9a21e730449c10cac6e6f69d255611c93f63c2hding            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaAc[uc_i]);
6568e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
6578e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6588e9a21e730449c10cac6e6f69d255611c93f63c2hding
6598e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Activate Restart markers
6608e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pJPEGContext->sScan_Encode_Info.ui16CScan > 1) {
6618e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Only use restart intervals if we need them (ie. multiple Scan encode and/or parallel CB encode)
6628e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, 0xFFDD); //Marker header
6638e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, 4); // Byte size of marker (header not included)
6648e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 2, pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan); // Restart Interval (same as MCUs per buffer)
6658e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
6668e9a21e730449c10cac6e6f69d255611c93f63c2hding
6678e9a21e730449c10cac6e6f69d255611c93f63c2hding    return s_streamW.Offset;
6688e9a21e730449c10cac6e6f69d255611c93f63c2hding}
6698e9a21e730449c10cac6e6f69d255611c93f63c2hding
6708e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 EncodeFrameHeader(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
6718e9a21e730449c10cac6e6f69d255611c93f63c2hding                                    IMG_UINT8 *puc_stream_buff)
6728e9a21e730449c10cac6e6f69d255611c93f63c2hding{
6738e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW ps_streamW;
6748e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8  uc_num_comp_in_img;
6758e9a21e730449c10cac6e6f69d255611c93f63c2hding
6768e9a21e730449c10cac6e6f69d255611c93f63c2hding    uc_num_comp_in_img = pJPEGContext->pMTXSetup->ui32ComponentsInScan;
6778e9a21e730449c10cac6e6f69d255611c93f63c2hding
6788e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_streamW.Offset = 0;
6798e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_streamW.Buffer = puc_stream_buff;
6808e9a21e730449c10cac6e6f69d255611c93f63c2hding
6818e9a21e730449c10cac6e6f69d255611c93f63c2hding
6828e9a21e730449c10cac6e6f69d255611c93f63c2hding    //if(ps_jpeg_params->uc_isAbbreviated != 0)
6838e9a21e730449c10cac6e6f69d255611c93f63c2hding    //   fPutBitsToBuffer(&ps_streamW, 2, START_OF_IMAGE);
6848e9a21e730449c10cac6e6f69d255611c93f63c2hding
6858e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Writing the frame header */
6868e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, SOF_BASELINE_DCT);
6878e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Frame header length */
6888e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, 8 + 3 * uc_num_comp_in_img);
6898e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Precision */
6908e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 1, 8);
6918e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Height : sample lines */
6928e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, pJPEGContext->ui32OutputHeight);
6938e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Width : samples per line */
6948e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 2, pJPEGContext->ui32OutputWidth);
6958e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Number of image components */
6968e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 1, uc_num_comp_in_img);
6978e9a21e730449c10cac6e6f69d255611c93f63c2hding
6988e9a21e730449c10cac6e6f69d255611c93f63c2hding
6998e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Chroma Details
7008e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pJPEGContext->pMTXSetup->ui16DataInterleaveStatus < C_INTERLEAVE) {
7018e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Luma Details
7028e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Component identifier */
7038e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); //CompId 0 = 1, 1 = 2, 2 = 3
7048e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7058e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 0); // 0 = Luma(0), 1,2 = Chroma(1)
7068e9a21e730449c10cac6e6f69d255611c93f63c2hding
7078e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Chroma planar
7088e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 2); //CompId 0 = 1, 1 = 2, 2 = 3
7098e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7108e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
7118e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7128e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
7138e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7148e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[2].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[2].ui32HeightBlocks >> 3));
7158e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7168e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else if (pJPEGContext->pMTXSetup->ui16DataInterleaveStatus == C_INTERLEAVE) {
7178e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Luma Details
7188e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Component identifier */
7198e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); //CompId 0 = 1, 1 = 2, 2 = 3
7208e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 3) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7218e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 0); // 0 = Luma(0), 1,2 = Chroma(1)
7228e9a21e730449c10cac6e6f69d255611c93f63c2hding
7238e9a21e730449c10cac6e6f69d255611c93f63c2hding        // Chroma Interleaved
7248e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 2); //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[1].ui32WidthBlocks >> 4) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
7278e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7288e9a21e730449c10cac6e6f69d255611c93f63c2hding
7298e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
7308e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7318e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 4) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
7328e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7338e9a21e730449c10cac6e6f69d255611c93f63c2hding    } else {
7348e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Luma Details
7358e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Component identifier */
7368e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); //CompId 0 = 1, 1 = 2, 2 = 3
7378e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 4) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7388e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 0); // 0 = Luma(0), 1,2 = Chroma(1)
7398e9a21e730449c10cac6e6f69d255611c93f63c2hding
7408e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Chroma YUYV - Special case
7418e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 2); //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[0].ui32WidthBlocks >> 5) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7448e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7458e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
7468e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* 4 bit Horizontal and 4 bit vertical sampling factors */
7478e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, ((pJPEGContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 5) << 4) | (pJPEGContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
7488e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
7498e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
7508e9a21e730449c10cac6e6f69d255611c93f63c2hding
7518e9a21e730449c10cac6e6f69d255611c93f63c2hding
7528e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Use if you want start of scan (image data) to align to 32
7538e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&ps_streamW, 1, 0xFF);
7548e9a21e730449c10cac6e6f69d255611c93f63c2hding
7558e9a21e730449c10cac6e6f69d255611c93f63c2hding    return ps_streamW.Offset;
7568e9a21e730449c10cac6e6f69d255611c93f63c2hding}
7578e9a21e730449c10cac6e6f69d255611c93f63c2hding
7588e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 JPGEncodeMarker(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext ,
7598e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT8* pui8BitStreamBuffer ,
7608e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT32 *pui32BytesWritten, IMG_BOOL bIncludeHuffmanTables)
7618e9a21e730449c10cac6e6f69d255611c93f63c2hding{
7628e9a21e730449c10cac6e6f69d255611c93f63c2hding#ifdef JPEG_VERBOSE
7638e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "PVRJPGEncodeMarker");
7648e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
7658e9a21e730449c10cac6e6f69d255611c93f63c2hding
7668e9a21e730449c10cac6e6f69d255611c93f63c2hding
7678e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui32BytesWritten += EncodeMarkerSegment(pJPEGContext, pui8BitStreamBuffer + *pui32BytesWritten, bIncludeHuffmanTables);
7688e9a21e730449c10cac6e6f69d255611c93f63c2hding
7698e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
7708e9a21e730449c10cac6e6f69d255611c93f63c2hding}
7718e9a21e730449c10cac6e6f69d255611c93f63c2hding
7728e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 JPGEncodeHeader(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
7738e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT8*      pui8BitStreamBuffer ,
7748e9a21e730449c10cac6e6f69d255611c93f63c2hding                                  IMG_UINT32*     pui32BytesWritten)
7758e9a21e730449c10cac6e6f69d255611c93f63c2hding{
7768e9a21e730449c10cac6e6f69d255611c93f63c2hding#ifdef JPEG_VERBOSE
7778e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "JPGEncodeHeader");
7788e9a21e730449c10cac6e6f69d255611c93f63c2hding#endif
7798e9a21e730449c10cac6e6f69d255611c93f63c2hding
7808e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui32BytesWritten += EncodeFrameHeader(pJPEGContext, pui8BitStreamBuffer + *pui32BytesWritten);
7818e9a21e730449c10cac6e6f69d255611c93f63c2hding
7828e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
7838e9a21e730449c10cac6e6f69d255611c93f63c2hding}
7848e9a21e730449c10cac6e6f69d255611c93f63c2hding
7858e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_UINT32 JPGEncodeSOSHeader(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
7868e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     IMG_UINT8*      pui8BitStreamBuffer ,
7878e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     IMG_UINT32*     pui32BytesWritten)
7888e9a21e730449c10cac6e6f69d255611c93f63c2hding{
7898e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 uc_comp_id, ui8Comp;
7908e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW s_streamW;
7918e9a21e730449c10cac6e6f69d255611c93f63c2hding
7928e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Offset = 0;
7938e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Buffer = pui8BitStreamBuffer + *pui32BytesWritten;
7948e9a21e730449c10cac6e6f69d255611c93f63c2hding
7958e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Start of scan */
7968e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, START_OF_SCAN);
7978e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Scan header length */
7988e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, 6 + (pJPEGContext->pMTXSetup->ui32ComponentsInScan << 1));
7998e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Number of image components in scan */
8008e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, pJPEGContext->pMTXSetup->ui32ComponentsInScan);
8018e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui8Comp = 0; ui8Comp < pJPEGContext->pMTXSetup->ui32ComponentsInScan; ui8Comp++) {
8028e9a21e730449c10cac6e6f69d255611c93f63c2hding        uc_comp_id = ui8Comp + 1;
8038e9a21e730449c10cac6e6f69d255611c93f63c2hding
8048e9a21e730449c10cac6e6f69d255611c93f63c2hding        /* Scan component selector */
8058e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, uc_comp_id);
8068e9a21e730449c10cac6e6f69d255611c93f63c2hding
8078e9a21e730449c10cac6e6f69d255611c93f63c2hding        /*4 Bits Dc entropy coding table destination selector */
8088e9a21e730449c10cac6e6f69d255611c93f63c2hding        /*4 Bits Ac entropy coding table destination selector */
8098e9a21e730449c10cac6e6f69d255611c93f63c2hding        fPutBitsToBuffer(&s_streamW, 1, ((ui8Comp != 0 ? 1 : 0) << 4) | (ui8Comp != 0 ? 1 : 0)); // Huffman table refs = 0 Luma 1 Chroma
8108e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
8118e9a21e730449c10cac6e6f69d255611c93f63c2hding
8128e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Start of spectral or predictor selection  */
8138e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, 0);
8148e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* End of spectral selection */
8158e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, 63);
8168e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*4 Bits Successive approximation bit position high (0)*/
8178e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*4 Bits Successive approximation bit position low or point transform (0)*/
8188e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 1, 0);
8198e9a21e730449c10cac6e6f69d255611c93f63c2hding
8208e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui32BytesWritten += s_streamW.Offset;
8218e9a21e730449c10cac6e6f69d255611c93f63c2hding
8228e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
8238e9a21e730449c10cac6e6f69d255611c93f63c2hding}
8248e9a21e730449c10cac6e6f69d255611c93f63c2hding
8258e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic void InitializeScanCounter(TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext)
8268e9a21e730449c10cac6e6f69d255611c93f63c2hding{
8278e9a21e730449c10cac6e6f69d255611c93f63c2hding    pJPEGContext->sScan_Encode_Info.ui16SScan =
8288e9a21e730449c10cac6e6f69d255611c93f63c2hding        pJPEGContext->sScan_Encode_Info.ui16CScan =
8298e9a21e730449c10cac6e6f69d255611c93f63c2hding            pJPEGContext->sScan_Encode_Info.ui16ScansInImage;
8308e9a21e730449c10cac6e6f69d255611c93f63c2hding}
8318e9a21e730449c10cac6e6f69d255611c93f63c2hding
8328e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_ERRORCODE PrepareHeader(TOPAZHP_JPEG_ENCODER_CONTEXT * pJPEGContext, IMG_CODED_BUFFER *pCBuffer, IMG_UINT32 ui32StartOffset, IMG_BOOL bIncludeHuffmanTables)
8338e9a21e730449c10cac6e6f69d255611c93f63c2hding{
8348e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_ERRORCODE rc;
8358e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT8 *ui8OutputBuffer;
8368e9a21e730449c10cac6e6f69d255611c93f63c2hding
8378e9a21e730449c10cac6e6f69d255611c93f63c2hding    //Locate our JPEG Coded buffer
8388e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui8OutputBuffer = (IMG_UINT8 *)pCBuffer->pMemInfo;
8398e9a21e730449c10cac6e6f69d255611c93f63c2hding
8408e9a21e730449c10cac6e6f69d255611c93f63c2hding    pCBuffer->ui32BytesWritten = ui32StartOffset;
8418e9a21e730449c10cac6e6f69d255611c93f63c2hding    *((IMG_UINT32*)ui8OutputBuffer + pCBuffer->ui32BytesWritten) = 0;
8428e9a21e730449c10cac6e6f69d255611c93f63c2hding
8438e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Before writing headers, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8448e9a21e730449c10cac6e6f69d255611c93f63c2hding
8458e9a21e730449c10cac6e6f69d255611c93f63c2hding    // JPGEncodeMarker - Currently misses out the APP0 header
8468e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = JPGEncodeMarker(pJPEGContext, (IMG_UINT8 *) ui8OutputBuffer,  &pCBuffer->ui32BytesWritten, bIncludeHuffmanTables);
8478e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc) return rc;
8488e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "After JPGEncodeMarker, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8498e9a21e730449c10cac6e6f69d255611c93f63c2hding
8508e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = JPGEncodeHeader(pJPEGContext , (IMG_UINT8 *) ui8OutputBuffer ,  &pCBuffer->ui32BytesWritten);
8518e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc) return rc;
8528e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "After JPGEncodeHeader, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8538e9a21e730449c10cac6e6f69d255611c93f63c2hding
8548e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = JPGEncodeSOSHeader(pJPEGContext, (IMG_UINT8 *) ui8OutputBuffer, &pCBuffer->ui32BytesWritten);
8558e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc) return rc;
8568e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "After JPGEncodeSOSHeader, ui32BytesWritten: %d\n", pCBuffer->ui32BytesWritten);
8578e9a21e730449c10cac6e6f69d255611c93f63c2hding
8588e9a21e730449c10cac6e6f69d255611c93f63c2hding    return IMG_ERR_OK;
8598e9a21e730449c10cac6e6f69d255611c93f63c2hding}
8608e9a21e730449c10cac6e6f69d255611c93f63c2hding
8618e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic IMG_ERRORCODE IssueBufferToHW(
8628e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *pJPEGContext,
8638e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16BCnt,
8648e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_INT8 i8PipeNumber,
8658e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32NoMCUsToEncode)
8668e9a21e730449c10cac6e6f69d255611c93f63c2hding{
8678e9a21e730449c10cac6e6f69d255611c93f63c2hding    MTX_ISSUE_BUFFERS *psBufferCmd;
8688e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p)(pJPEGContext->ctx);
8698e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
8708e9a21e730449c10cac6e6f69d255611c93f63c2hding
8718e9a21e730449c10cac6e6f69d255611c93f63c2hding    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
8728e9a21e730449c10cac6e6f69d255611c93f63c2hding
8738e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Submit Scan %d which contains %d MCU in Buffer %d to MTX %d\n",
8748e9a21e730449c10cac6e6f69d255611c93f63c2hding                             pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber,
8758e9a21e730449c10cac6e6f69d255611c93f63c2hding                             ui32NoMCUsToEncode, ui16BCnt, i8PipeNumber);
8768e9a21e730449c10cac6e6f69d255611c93f63c2hding
8778e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Issue to MTX ////////////////////////////
8788e9a21e730449c10cac6e6f69d255611c93f63c2hding
8798e9a21e730449c10cac6e6f69d255611c93f63c2hding    psBufferCmd = (MTX_ISSUE_BUFFERS *)(pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].pMemInfo);
8808e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(psBufferCmd);
8818e9a21e730449c10cac6e6f69d255611c93f63c2hding
8828e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16ScansInImage: %d\n", pJPEGContext->sScan_Encode_Info.ui16ScansInImage);
8838e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui16ScanNumber: %d\n", pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber);
8848e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32NumberMCUsToEncodePerScan: %d\n", pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
88588d0460822f8807666406c14662591167eed73a0hding
8868e9a21e730449c10cac6e6f69d255611c93f63c2hding    psBufferCmd->ui32MCUCntAndResetFlag = (ui32NoMCUsToEncode << 1) | 0x1;
88788d0460822f8807666406c14662591167eed73a0hding
8888e9a21e730449c10cac6e6f69d255611c93f63c2hding    psBufferCmd->ui32MCUPositionOfScanAndPipeNo =
88988d0460822f8807666406c14662591167eed73a0hding        (((pJPEGContext->sScan_Encode_Info.ui16ScansInImage -
8908e9a21e730449c10cac6e6f69d255611c93f63c2hding          pJPEGContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber) *
89188d0460822f8807666406c14662591167eed73a0hding         pJPEGContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan)<<2)&(~2);
8928e9a21e730449c10cac6e6f69d255611c93f63c2hding
8938e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(0 == i8PipeNumber);
89488d0460822f8807666406c14662591167eed73a0hding    if (i8PipeNumber <= 3)
89588d0460822f8807666406c14662591167eed73a0hding        psBufferCmd->ui32MCUPositionOfScanAndPipeNo |= i8PipeNumber;
8968e9a21e730449c10cac6e6f69d255611c93f63c2hding
8978e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32MCUPositionOfScanAndPipeNo: 0x%x\n", psBufferCmd->ui32MCUPositionOfScanAndPipeNo);
8988e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32MCUCntAndResetFlag: 0x%x\n", psBufferCmd->ui32MCUCntAndResetFlag);
8998e9a21e730449c10cac6e6f69d255611c93f63c2hding
9008e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "psBufferCmd: 0x%x\n", (unsigned int)(psBufferCmd));
9018e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Command Data: 0x%x\n", (unsigned int)(PTG_JPEG_HEADER_MAX_SIZE + ui16BCnt * pJPEGContext->ui32SizePerCodedBuffer));
9028e9a21e730449c10cac6e6f69d255611c93f63c2hding
9038e9a21e730449c10cac6e6f69d255611c93f63c2hding    // Issue buffers
90488d0460822f8807666406c14662591167eed73a0hding    tng_cmdbuf_insert_command_package(ctx->obj_context,
9058e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      0,
9068e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      MTX_CMDID_ISSUEBUFF,
90788d0460822f8807666406c14662591167eed73a0hding                                      0,
9088e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      ps_buf->coded_buf->psb_buffer,
9098e9a21e730449c10cac6e6f69d255611c93f63c2hding                                      PTG_JPEG_HEADER_MAX_SIZE + ui16BCnt * pJPEGContext->ui32SizePerCodedBuffer);
9108e9a21e730449c10cac6e6f69d255611c93f63c2hding
9118e9a21e730449c10cac6e6f69d255611c93f63c2hding    return IMG_ERR_OK;
9128e9a21e730449c10cac6e6f69d255611c93f63c2hding}
9138e9a21e730449c10cac6e6f69d255611c93f63c2hding
914430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic void tng_jpeg_QueryConfigAttributes(
9158e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAProfile profile,
9168e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEntrypoint entrypoint,
9178e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAConfigAttrib *attrib_list,
9188e9a21e730449c10cac6e6f69d255611c93f63c2hding    int num_attribs)
9198e9a21e730449c10cac6e6f69d255611c93f63c2hding{
9208e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
9218e9a21e730449c10cac6e6f69d255611c93f63c2hding
922430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_QueryConfigAttributes\n");
9238e9a21e730449c10cac6e6f69d255611c93f63c2hding
9248e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* RateControl attributes */
9258e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < num_attribs; i++) {
9268e9a21e730449c10cac6e6f69d255611c93f63c2hding        switch (attrib_list[i].type) {
9278e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAConfigAttribRTFormat:
9288e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
9298e9a21e730449c10cac6e6f69d255611c93f63c2hding        default:
9308e9a21e730449c10cac6e6f69d255611c93f63c2hding            attrib_list[i].value = VA_ATTRIB_NOT_SUPPORTED;
9318e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
9328e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
9338e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
9348e9a21e730449c10cac6e6f69d255611c93f63c2hding
9358e9a21e730449c10cac6e6f69d255611c93f63c2hding    return;
9368e9a21e730449c10cac6e6f69d255611c93f63c2hding}
9378e9a21e730449c10cac6e6f69d255611c93f63c2hding
9388e9a21e730449c10cac6e6f69d255611c93f63c2hding
939430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_ValidateConfig(
9408e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_config_p obj_config)
9418e9a21e730449c10cac6e6f69d255611c93f63c2hding{
9428e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
943430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_ValidateConfig\n");
9448e9a21e730449c10cac6e6f69d255611c93f63c2hding
9458e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
9468e9a21e730449c10cac6e6f69d255611c93f63c2hding
9478e9a21e730449c10cac6e6f69d255611c93f63c2hding}
9488e9a21e730449c10cac6e6f69d255611c93f63c2hding
949430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_CreateContext(
9508e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context,
9518e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_config_p obj_config)
9528e9a21e730449c10cac6e6f69d255611c93f63c2hding{
9538e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
9548e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
9558e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx;
9568e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p;
9578e9a21e730449c10cac6e6f69d255611c93f63c2hding
958430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_CreateContext\n");
9598e9a21e730449c10cac6e6f69d255611c93f63c2hding
960430ce5c32361119ed81a62c918be6afbdd053eddhding    vaStatus = tng_CreateContext(obj_context, obj_config, 1);
9618e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (VA_STATUS_SUCCESS != vaStatus)
9628e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_ALLOCATION_FAILED;
9638e9a21e730449c10cac6e6f69d255611c93f63c2hding
9648e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx = (context_ENC_p) obj_context->format_data;
9658e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->eCodec = IMG_CODEC_JPEG;
9668e9a21e730449c10cac6e6f69d255611c93f63c2hding
9678e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(0 == (ctx->ui16Width % 2));
9688e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(0 == (ctx->ui16FrameHeight % 2));
9698e9a21e730449c10cac6e6f69d255611c93f63c2hding
9708e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < obj_config->attrib_count; i++) {
9718e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (VAConfigAttribRTFormat ==  obj_config->attrib_list[i].type) {
9728e9a21e730449c10cac6e6f69d255611c93f63c2hding            switch (obj_config->attrib_list[i].value) {
9738e9a21e730449c10cac6e6f69d255611c93f63c2hding            case VA_RT_FORMAT_YUV420:
9748e9a21e730449c10cac6e6f69d255611c93f63c2hding                ctx->eFormat = IMG_CODEC_PL12;
9758e9a21e730449c10cac6e6f69d255611c93f63c2hding                drv_debug_msg(VIDEO_DEBUG_GENERAL, "JPEG encoding: NV12 format chose.\n");
9768e9a21e730449c10cac6e6f69d255611c93f63c2hding                break;
9778e9a21e730449c10cac6e6f69d255611c93f63c2hding            default:
9788e9a21e730449c10cac6e6f69d255611c93f63c2hding                drv_debug_msg(VIDEO_DEBUG_ERROR, "JPEG encoding: unsupported YUV format and force it to be NV12!\n");
9798e9a21e730449c10cac6e6f69d255611c93f63c2hding                ctx->eFormat = IMG_CODEC_PL12;
9808e9a21e730449c10cac6e6f69d255611c93f63c2hding                break;
9818e9a21e730449c10cac6e6f69d255611c93f63c2hding            }
9828e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
9838e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
9848e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
9858e9a21e730449c10cac6e6f69d255611c93f63c2hding
9868e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->jpeg_ctx = (TOPAZHP_JPEG_ENCODER_CONTEXT *)calloc(1, sizeof(TOPAZHP_JPEG_ENCODER_CONTEXT));
9878e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == ctx->jpeg_ctx)
9888e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_ALLOCATION_FAILED;
9898e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p = ctx->jpeg_ctx;
9908e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->ctx = ctx;
9918e9a21e730449c10cac6e6f69d255611c93f63c2hding
9928e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset((void *)jpeg_ctx_p, 0x0, sizeof(jpeg_ctx_p));
9938e9a21e730449c10cac6e6f69d255611c93f63c2hding
9948e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->NumCores = 1;
9958e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->eFormat = ctx->eFormat;
9968e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->ui32OutputWidth = ctx->ui16Width;
9978e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->ui32OutputHeight = ctx->ui16FrameHeight;
9988e9a21e730449c10cac6e6f69d255611c93f63c2hding
9998e9a21e730449c10cac6e6f69d255611c93f63c2hding    InitializeJpegEncode(jpeg_ctx_p);
10008e9a21e730449c10cac6e6f69d255611c93f63c2hding
10018e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((jpeg_ctx_p->sScan_Encode_Info.ui16ScansInImage < 1) ||
10028e9a21e730449c10cac6e6f69d255611c93f63c2hding        (jpeg_ctx_p->sScan_Encode_Info.ui16ScansInImage > PTG_JPEG_MAX_SCAN_NUM)) {
10038e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "JPEG MCU scanning number(%d) is wrong!\n", jpeg_ctx_p->sScan_Encode_Info.ui16ScansInImage);
10048e9a21e730449c10cac6e6f69d255611c93f63c2hding        free(ctx->jpeg_ctx);
10058e9a21e730449c10cac6e6f69d255611c93f63c2hding        ctx->jpeg_ctx = NULL;
10068e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
10078e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
10088e9a21e730449c10cac6e6f69d255611c93f63c2hding
10098e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*Allocate coded buffers' descripters */
10108e9a21e730449c10cac6e6f69d255611c93f63c2hding    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)));
10118e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == jpeg_ctx_p->sScan_Encode_Info.aBufferTable)
10128e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_ALLOCATION_FAILED;
10138e9a21e730449c10cac6e6f69d255611c93f63c2hding
10148e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
10158e9a21e730449c10cac6e6f69d255611c93f63c2hding}
10168e9a21e730449c10cac6e6f69d255611c93f63c2hding
10178e9a21e730449c10cac6e6f69d255611c93f63c2hding
1018430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic void tng_jpeg_DestroyContext(
10198e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
10208e9a21e730449c10cac6e6f69d255611c93f63c2hding{
10218e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx;
10228e9a21e730449c10cac6e6f69d255611c93f63c2hding
1023430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_DestroyPicture\n");
10248e9a21e730449c10cac6e6f69d255611c93f63c2hding
10258e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx = (context_ENC_p)(obj_context->format_data);
10268e9a21e730449c10cac6e6f69d255611c93f63c2hding
10278e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ctx->jpeg_ctx) {
10288e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (ctx->jpeg_ctx->sScan_Encode_Info.aBufferTable) {
10298e9a21e730449c10cac6e6f69d255611c93f63c2hding            free(ctx->jpeg_ctx->sScan_Encode_Info.aBufferTable);
10308e9a21e730449c10cac6e6f69d255611c93f63c2hding            ctx->jpeg_ctx->sScan_Encode_Info.aBufferTable = NULL;
10318e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
10328e9a21e730449c10cac6e6f69d255611c93f63c2hding
10338e9a21e730449c10cac6e6f69d255611c93f63c2hding        free(ctx->jpeg_ctx);
10348e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
10358e9a21e730449c10cac6e6f69d255611c93f63c2hding
1036430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_DestroyContext(obj_context, 1);
10378e9a21e730449c10cac6e6f69d255611c93f63c2hding}
10388e9a21e730449c10cac6e6f69d255611c93f63c2hding
1039430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng__cmdbuf_lowpower(context_ENC_p ctx)
1040430ce5c32361119ed81a62c918be6afbdd053eddhding{
1041430ce5c32361119ed81a62c918be6afbdd053eddhding    VAStatus vaStatus = VA_STATUS_SUCCESS;
1042430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = ctx->obj_context->tng_cmdbuf;
1043430ce5c32361119ed81a62c918be6afbdd053eddhding    psb_driver_data_p driver_data = ctx->obj_context->driver_data;
1044430ce5c32361119ed81a62c918be6afbdd053eddhding
1045430ce5c32361119ed81a62c918be6afbdd053eddhding    *cmdbuf->cmd_idx++ =
1046430ce5c32361119ed81a62c918be6afbdd053eddhding        ((MTX_CMDID_SW_LEAVE_LOWPOWER & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT) |
1047430ce5c32361119ed81a62c918be6afbdd053eddhding        ((ctx->ui32RawFrameCount  & MTX_CMDWORD_CORE_MASK) << MTX_CMDWORD_CORE_SHIFT) |
1048430ce5c32361119ed81a62c918be6afbdd053eddhding        (((driver_data->context_id & MTX_CMDWORD_COUNT_MASK) << MTX_CMDWORD_COUNT_SHIFT));
1049430ce5c32361119ed81a62c918be6afbdd053eddhding
1050430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_insert_command_param(ctx->eCodec);
1051430ce5c32361119ed81a62c918be6afbdd053eddhding
1052430ce5c32361119ed81a62c918be6afbdd053eddhding    return vaStatus;
1053430ce5c32361119ed81a62c918be6afbdd053eddhding}
1054430ce5c32361119ed81a62c918be6afbdd053eddhding
1055430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_BeginPicture(
10568e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
10578e9a21e730449c10cac6e6f69d255611c93f63c2hding{
10588e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
10598e9a21e730449c10cac6e6f69d255611c93f63c2hding    int ret;
1060430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf;
10618e9a21e730449c10cac6e6f69d255611c93f63c2hding
10628e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
10638e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p = ctx->jpeg_ctx;
10648e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
1065430ce5c32361119ed81a62c918be6afbdd053eddhding    psb_driver_data_p driver_data = ctx->obj_context->driver_data;
10668e9a21e730449c10cac6e6f69d255611c93f63c2hding
1067430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_BeginPicture: Frame %d\n", ctx->obj_context->frame_count);
10688e9a21e730449c10cac6e6f69d255611c93f63c2hding
10698e9a21e730449c10cac6e6f69d255611c93f63c2hding
10708e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Get the current surface */
10718e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_buf->src_surface = ctx->obj_context->current_render_target;
10728e9a21e730449c10cac6e6f69d255611c93f63c2hding
10738e9a21e730449c10cac6e6f69d255611c93f63c2hding
10748e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Initialize the command buffer */
1075430ce5c32361119ed81a62c918be6afbdd053eddhding    ret = tng_context_get_next_cmdbuf(ctx->obj_context);
10768e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ret) {
10778e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "get next cmdbuf fail\n");
10788e9a21e730449c10cac6e6f69d255611c93f63c2hding        vaStatus = VA_STATUS_ERROR_UNKNOWN;
10798e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
10808e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
1081430ce5c32361119ed81a62c918be6afbdd053eddhding    cmdbuf = ctx->obj_context->tng_cmdbuf;
10828e9a21e730449c10cac6e6f69d255611c93f63c2hding
10838e9a21e730449c10cac6e6f69d255611c93f63c2hding
10848e9a21e730449c10cac6e6f69d255611c93f63c2hding    //For the first picture of a set to be encoded, need to ask kernel to perpare JPEG encoding
10858e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ctx->obj_context->frame_count == 0) { /* first picture */
10868e9a21e730449c10cac6e6f69d255611c93f63c2hding
10878e9a21e730449c10cac6e6f69d255611c93f63c2hding        *cmdbuf->cmd_idx++ = ((MTX_CMDID_SW_NEW_CODEC & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT) |
1088430ce5c32361119ed81a62c918be6afbdd053eddhding			     ((ctx->eCodec) << MTX_CMDWORD_CORE_SHIFT) |
10898e9a21e730449c10cac6e6f69d255611c93f63c2hding                             (((driver_data->drm_context & MTX_CMDWORD_COUNT_MASK) << MTX_CMDWORD_COUNT_SHIFT));
1090430ce5c32361119ed81a62c918be6afbdd053eddhding        tng_cmdbuf_insert_command_param((ctx->ui16Width << 16) | ctx->ui16FrameHeight);
10918e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
10928e9a21e730449c10cac6e6f69d255611c93f63c2hding
10938e9a21e730449c10cac6e6f69d255611c93f63c2hding
10948e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map MTX setup buffer */
10958e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(&cmdbuf->jpeg_header_mem, &cmdbuf->jpeg_header_mem_p);
10968e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
10978e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "Fail to map MTX setup buffer\n");
10988e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
10998e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11008e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->pMemInfoMTXSetup = cmdbuf->jpeg_header_mem_p;
11018e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->pMTXSetup = (JPEG_MTX_DMA_SETUP*)jpeg_ctx_p->pMemInfoMTXSetup;
11028e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset(jpeg_ctx_p->pMemInfoMTXSetup, 0x0, ctx->jpeg_header_mem_size);
11038e9a21e730449c10cac6e6f69d255611c93f63c2hding
11048e9a21e730449c10cac6e6f69d255611c93f63c2hding
11058e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map MTX setup interface buffer */
11068e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(&cmdbuf->jpeg_header_interface_mem, &cmdbuf->jpeg_header_interface_mem_p);
11078e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
11088e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "Fail to map MTX setup interface buffer\n");
11098e9a21e730449c10cac6e6f69d255611c93f63c2hding        psb_buffer_unmap(&cmdbuf->jpeg_header_mem);
11108e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
11118e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
111288d0460822f8807666406c14662591167eed73a0hding    jpeg_ctx_p->pMemInfoWritebackMemory = cmdbuf->jpeg_header_interface_mem_p;
111388d0460822f8807666406c14662591167eed73a0hding    jpeg_ctx_p->pMTXWritebackMemory = (JPEG_MTX_WRITEBACK_MEMORY*)jpeg_ctx_p->pMemInfoWritebackMemory;
111488d0460822f8807666406c14662591167eed73a0hding    memset(jpeg_ctx_p->pMemInfoWritebackMemory, 0x0, ctx->jpeg_header_interface_mem_size);
11158e9a21e730449c10cac6e6f69d255611c93f63c2hding
11168e9a21e730449c10cac6e6f69d255611c93f63c2hding
11178e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map quantization table buffer */
11188e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(&cmdbuf->jpeg_pic_params, &cmdbuf->jpeg_pic_params_p);
11198e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
11208e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "Fail to map quantization table buffer\n");
11218e9a21e730449c10cac6e6f69d255611c93f63c2hding        psb_buffer_unmap(&cmdbuf->jpeg_header_mem);
11228e9a21e730449c10cac6e6f69d255611c93f63c2hding        psb_buffer_unmap(&cmdbuf->jpeg_header_interface_mem);
11238e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
11248e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11258e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->pMemInfoTableBlock = cmdbuf->jpeg_pic_params_p;
11268e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->psTablesBlock = (JPEG_MTX_QUANT_TABLE *)jpeg_ctx_p->pMemInfoTableBlock;
11278e9a21e730449c10cac6e6f69d255611c93f63c2hding    memset(jpeg_ctx_p->pMemInfoTableBlock, 0x0, ctx->jpeg_pic_params_size);
11288e9a21e730449c10cac6e6f69d255611c93f63c2hding
1129430ce5c32361119ed81a62c918be6afbdd053eddhding    *cmdbuf->cmd_idx++ =
1130430ce5c32361119ed81a62c918be6afbdd053eddhding        ((MTX_CMDID_SW_LEAVE_LOWPOWER & MTX_CMDWORD_ID_MASK) << MTX_CMDWORD_ID_SHIFT) |
1131430ce5c32361119ed81a62c918be6afbdd053eddhding        ((0 & MTX_CMDWORD_CORE_MASK) << MTX_CMDWORD_CORE_SHIFT) |
1132430ce5c32361119ed81a62c918be6afbdd053eddhding        (((driver_data->context_id & MTX_CMDWORD_COUNT_MASK) << MTX_CMDWORD_COUNT_SHIFT));
1133430ce5c32361119ed81a62c918be6afbdd053eddhding
1134430ce5c32361119ed81a62c918be6afbdd053eddhding    *cmdbuf->cmd_idx++ = IMG_CODEC_JPEG;
1135430ce5c32361119ed81a62c918be6afbdd053eddhding
1136430ce5c32361119ed81a62c918be6afbdd053eddhding    vaStatus = tng__cmdbuf_lowpower(ctx);
1137430ce5c32361119ed81a62c918be6afbdd053eddhding    if (vaStatus != VA_STATUS_SUCCESS) {
1138430ce5c32361119ed81a62c918be6afbdd053eddhding        drv_debug_msg(VIDEO_DEBUG_ERROR, "cmdbuf lowpower\n");
1139430ce5c32361119ed81a62c918be6afbdd053eddhding    }
11408e9a21e730449c10cac6e6f69d255611c93f63c2hding
11418e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Set SetupInterface*/
11428e9a21e730449c10cac6e6f69d255611c93f63c2hding    SetSetupInterface(jpeg_ctx_p);
11438e9a21e730449c10cac6e6f69d255611c93f63c2hding    IssueSetupInterface(jpeg_ctx_p);
11448e9a21e730449c10cac6e6f69d255611c93f63c2hding
11458e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Set MTX setup struture */
11468e9a21e730449c10cac6e6f69d255611c93f63c2hding    ret = SetMTXSetup(jpeg_ctx_p, ps_buf->src_surface);
11478e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (ret != IMG_ERR_OK)
11488e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
11498e9a21e730449c10cac6e6f69d255611c93f63c2hding    IssueMTXSetup(jpeg_ctx_p);
11508e9a21e730449c10cac6e6f69d255611c93f63c2hding
11518e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Initialize the default quantization tables */
11528e9a21e730449c10cac6e6f69d255611c93f63c2hding    SetDefaultQmatix(jpeg_ctx_p->pMemInfoTableBlock);
11538e9a21e730449c10cac6e6f69d255611c93f63c2hding
11548e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Initialize scan counters */
11558e9a21e730449c10cac6e6f69d255611c93f63c2hding    InitializeScanCounter(jpeg_ctx_p);
11568e9a21e730449c10cac6e6f69d255611c93f63c2hding
1157430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_buffer_ref(cmdbuf, &(ctx->obj_context->current_render_target->psb_surface->buf));
11588e9a21e730449c10cac6e6f69d255611c93f63c2hding
11598e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
11608e9a21e730449c10cac6e6f69d255611c93f63c2hding}
11618e9a21e730449c10cac6e6f69d255611c93f63c2hding
11628e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic VAStatus ProcessQmatrixParam(context_ENC_p ctx, object_buffer_p obj_buffer)
11638e9a21e730449c10cac6e6f69d255611c93f63c2hding{
11648e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
11658e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAQMatrixBufferJPEG *pBuffer;
11668e9a21e730449c10cac6e6f69d255611c93f63c2hding    JPEG_MTX_QUANT_TABLE* pQMatrix = (JPEG_MTX_QUANT_TABLE *)(ctx->jpeg_ctx->psTablesBlock);
11678e9a21e730449c10cac6e6f69d255611c93f63c2hding
11688e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAQMatrixBufferType);
11698e9a21e730449c10cac6e6f69d255611c93f63c2hding
11708e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBuffer = (VAQMatrixBufferJPEG *)obj_buffer->buffer_data;
11718e9a21e730449c10cac6e6f69d255611c93f63c2hding
11728e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (0 != pBuffer->load_lum_quantiser_matrix) {
11738e9a21e730449c10cac6e6f69d255611c93f63c2hding        memcpy(pQMatrix->aui8LumaQuantParams,
11748e9a21e730449c10cac6e6f69d255611c93f63c2hding               pBuffer->lum_quantiser_matrix,
11758e9a21e730449c10cac6e6f69d255611c93f63c2hding               QUANT_TABLE_SIZE_BYTES);
11768e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11778e9a21e730449c10cac6e6f69d255611c93f63c2hding
11788e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (0 != pBuffer->load_chroma_quantiser_matrix) {
11798e9a21e730449c10cac6e6f69d255611c93f63c2hding        memcpy(pQMatrix->aui8ChromaQuantParams,
11808e9a21e730449c10cac6e6f69d255611c93f63c2hding               pBuffer->chroma_quantiser_matrix,
11818e9a21e730449c10cac6e6f69d255611c93f63c2hding               QUANT_TABLE_SIZE_BYTES);
11828e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
11838e9a21e730449c10cac6e6f69d255611c93f63c2hding
11848e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(obj_buffer->buffer_data);
11858e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->buffer_data = NULL;
11868e9a21e730449c10cac6e6f69d255611c93f63c2hding
11878e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
11888e9a21e730449c10cac6e6f69d255611c93f63c2hding}
11898e9a21e730449c10cac6e6f69d255611c93f63c2hding
11908e9a21e730449c10cac6e6f69d255611c93f63c2hdingstatic VAStatus ProcessPictureParam(context_ENC_p ctx, object_buffer_p obj_buffer)
11918e9a21e730449c10cac6e6f69d255611c93f63c2hding{
11928e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
11938e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAEncPictureParameterBufferJPEG *pBuffer = NULL;
11948e9a21e730449c10cac6e6f69d255611c93f63c2hding    BUFFER_HEADER *pBufHeader = NULL;
11958e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx = ctx->jpeg_ctx;
11968e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
11978e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_ERRORCODE rc;
119888d0460822f8807666406c14662591167eed73a0hding
11998e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Check the input buffer */
12008e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(obj_buffer->type == VAEncPictureParameterBufferType);
12018e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((obj_buffer->num_elements != 1) ||
12028e9a21e730449c10cac6e6f69d255611c93f63c2hding        (obj_buffer->size != sizeof(VAEncPictureParameterBufferJPEG))) {
12038e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
12048e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12058e9a21e730449c10cac6e6f69d255611c93f63c2hding
12068e9a21e730449c10cac6e6f69d255611c93f63c2hding
12078e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Lookup and get coded buffer */
12088e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBuffer = (VAEncPictureParameterBufferJPEG *)obj_buffer->buffer_data;
12098e9a21e730449c10cac6e6f69d255611c93f63c2hding
12108e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(ctx->ui16Width == pBuffer->picture_width);
12118e9a21e730449c10cac6e6f69d255611c93f63c2hding    ASSERT(ctx->ui16FrameHeight == pBuffer->picture_height);
12128e9a21e730449c10cac6e6f69d255611c93f63c2hding
12138e9a21e730449c10cac6e6f69d255611c93f63c2hding    ps_buf->coded_buf = BUFFER(pBuffer->coded_buf);
12148e9a21e730449c10cac6e6f69d255611c93f63c2hding
12158e9a21e730449c10cac6e6f69d255611c93f63c2hding    free(pBuffer);
12168e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->buffer_data = NULL;
12178e9a21e730449c10cac6e6f69d255611c93f63c2hding    obj_buffer->size = 0;
12188e9a21e730449c10cac6e6f69d255611c93f63c2hding
12198e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == ps_buf->coded_buf) {
12208e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "%s L%d Invalid coded buffer handle\n", __FUNCTION__, __LINE__);
12218e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_INVALID_BUFFER;
12228e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12238e9a21e730449c10cac6e6f69d255611c93f63c2hding
12248e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Map coded buffer */
12258e9a21e730449c10cac6e6f69d255611c93f63c2hding    vaStatus = psb_buffer_map(ps_buf->coded_buf->psb_buffer, &jpeg_ctx->jpeg_coded_buf.pMemInfo);
12268e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (vaStatus) {
12278e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_ERROR, "ERROR: Map coded_buf failed!");
12288e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
12298e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12308e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx->jpeg_coded_buf.ui32Size = ps_buf->coded_buf->size;
12318e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx->jpeg_coded_buf.sLock = BUFFER_FREE;
12328e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx->jpeg_coded_buf.ui32BytesWritten = 0;
12338e9a21e730449c10cac6e6f69d255611c93f63c2hding
12348e9a21e730449c10cac6e6f69d255611c93f63c2hding    if ((jpeg_ctx->jpeg_coded_buf.ui32Size) < (9 + 6 + (4 * 3))) {
12358e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_INVALID_BUFFER;
12368e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12378e9a21e730449c10cac6e6f69d255611c93f63c2hding
12388e9a21e730449c10cac6e6f69d255611c93f63c2hding
12398e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Assign coded buffer to each scan */
12408e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Assign coded buffer to each scan\n");
12418e9a21e730449c10cac6e6f69d255611c93f63c2hding    AssignCodedDataBuffers(jpeg_ctx);
12428e9a21e730449c10cac6e6f69d255611c93f63c2hding
12438e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Coded buffer total size is %d,"
12448e9a21e730449c10cac6e6f69d255611c93f63c2hding                             "coded segment size per scan is %d\n",
124588d0460822f8807666406c14662591167eed73a0hding                             jpeg_ctx->jpeg_coded_buf.ui32Size, jpeg_ctx->ui32SizePerCodedBuffer);
12468e9a21e730449c10cac6e6f69d255611c93f63c2hding
12478e9a21e730449c10cac6e6f69d255611c93f63c2hding
12488e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Write JPEG headers to coded buffer */
12498e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Write JPEG Headers to coded buf\n");
12508e9a21e730449c10cac6e6f69d255611c93f63c2hding
12518e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader = (BUFFER_HEADER *)jpeg_ctx->jpeg_coded_buf.pMemInfo;
12528e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32BytesUsed = 0; /* Not include BUFFER_HEADER*/
12538e9a21e730449c10cac6e6f69d255611c93f63c2hding
12548e9a21e730449c10cac6e6f69d255611c93f63c2hding    rc = PrepareHeader(jpeg_ctx, &jpeg_ctx->jpeg_coded_buf, sizeof(BUFFER_HEADER), IMG_TRUE);
12558e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (rc != IMG_ERR_OK)
12568e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
12578e9a21e730449c10cac6e6f69d255611c93f63c2hding
12588e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32Reserved3 = PTG_JPEG_HEADER_MAX_SIZE;//Next coded buffer offset
12598e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32BytesUsed = jpeg_ctx->jpeg_coded_buf.ui32BytesWritten - sizeof(BUFFER_HEADER);
12608e9a21e730449c10cac6e6f69d255611c93f63c2hding
12618e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "sizeof(BUFFER_HEADER): %d, ui32BytesUsed :%d, ui32Reserved3: %d, ui32BytesWritten: %d\n",
12628e9a21e730449c10cac6e6f69d255611c93f63c2hding                             sizeof(BUFFER_HEADER), pBufHeader->ui32BytesUsed, pBufHeader->ui32Reserved3, jpeg_ctx->jpeg_coded_buf.ui32BytesWritten);
12638e9a21e730449c10cac6e6f69d255611c93f63c2hding
12648e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
12658e9a21e730449c10cac6e6f69d255611c93f63c2hding}
1266430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_RenderPicture(
12678e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context,
12688e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_buffer_p *buffers,
12698e9a21e730449c10cac6e6f69d255611c93f63c2hding    int num_buffers)
12708e9a21e730449c10cac6e6f69d255611c93f63c2hding{
12718e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
12728e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
12738e9a21e730449c10cac6e6f69d255611c93f63c2hding    int i;
12748e9a21e730449c10cac6e6f69d255611c93f63c2hding
1275430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_RenderPicture\n");
12768e9a21e730449c10cac6e6f69d255611c93f63c2hding
12778e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (i = 0; i < num_buffers; i++) {
12788e9a21e730449c10cac6e6f69d255611c93f63c2hding        object_buffer_p obj_buffer = buffers[i];
12798e9a21e730449c10cac6e6f69d255611c93f63c2hding
12808e9a21e730449c10cac6e6f69d255611c93f63c2hding        switch (obj_buffer->type) {
12818e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAQMatrixBufferType:
1282430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_RenderPicture got VAEncSliceParameterBufferType\n");
12838e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = ProcessQmatrixParam(ctx, obj_buffer);
12848e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
12858e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
12868e9a21e730449c10cac6e6f69d255611c93f63c2hding        case VAEncPictureParameterBufferType:
1287430ce5c32361119ed81a62c918be6afbdd053eddhding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_RenderPicture got VAEncPictureParameterBufferType\n");
12888e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = ProcessPictureParam(ctx, obj_buffer);
12898e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
12908e9a21e730449c10cac6e6f69d255611c93f63c2hding            break;
12918e9a21e730449c10cac6e6f69d255611c93f63c2hding        default:
12928e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_UNKNOWN;
12938e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
12948e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
12958e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
12968e9a21e730449c10cac6e6f69d255611c93f63c2hding
12978e9a21e730449c10cac6e6f69d255611c93f63c2hding    return vaStatus;
12988e9a21e730449c10cac6e6f69d255611c93f63c2hding}
12998e9a21e730449c10cac6e6f69d255611c93f63c2hding
1300430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic VAStatus tng_jpeg_EndPicture(
13018e9a21e730449c10cac6e6f69d255611c93f63c2hding    object_context_p obj_context)
13028e9a21e730449c10cac6e6f69d255611c93f63c2hding{
13038e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16BCnt;
13048e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 rc = 0;
13058e9a21e730449c10cac6e6f69d255611c93f63c2hding    VAStatus vaStatus = VA_STATUS_SUCCESS;
13068e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32NoMCUsToEncode = 0;
13078e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT32 ui32RemainMCUs = 0;
13088e9a21e730449c10cac6e6f69d255611c93f63c2hding
13098e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
13108e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p = ctx->jpeg_ctx;
1311430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_cmdbuf_p cmdbuf = (tng_cmdbuf_p)ctx->obj_context->tng_cmdbuf;
13128e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_mem *ps_mem = &(ctx->ctx_mem[ctx->ui32StreamID]);
13138e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_frame_buf *ps_buf = &(ctx->ctx_frame_buf);
13148e9a21e730449c10cac6e6f69d255611c93f63c2hding
1315430ce5c32361119ed81a62c918be6afbdd053eddhding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "tng_jpeg_EndPicture\n");
13168e9a21e730449c10cac6e6f69d255611c93f63c2hding
13178e9a21e730449c10cac6e6f69d255611c93f63c2hding    IssueQmatix(jpeg_ctx_p);
13188e9a21e730449c10cac6e6f69d255611c93f63c2hding
13198e9a21e730449c10cac6e6f69d255611c93f63c2hding    /* Compute the next scan to be sent */
13208e9a21e730449c10cac6e6f69d255611c93f63c2hding    ui32RemainMCUs = jpeg_ctx_p->sScan_Encode_Info.ui32NumberMCUsToEncode;
13218e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32RemainMCUs: %d\n", ui32RemainMCUs);
13228e9a21e730449c10cac6e6f69d255611c93f63c2hding
13238e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui16BCnt = 0; (ui16BCnt < jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers)
13248e9a21e730449c10cac6e6f69d255611c93f63c2hding         && (jpeg_ctx_p->sScan_Encode_Info.ui16SScan > 0); ui16BCnt++) {
13258e9a21e730449c10cac6e6f69d255611c93f63c2hding
13268e9a21e730449c10cac6e6f69d255611c93f63c2hding        jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber = jpeg_ctx_p->sScan_Encode_Info.ui16SScan--;
13278e9a21e730449c10cac6e6f69d255611c93f63c2hding        jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].i8PipeNumber =
13288e9a21e730449c10cac6e6f69d255611c93f63c2hding            (1 == jpeg_ctx_p->NumCores) ? 0 : (ui16BCnt % jpeg_ctx_p->NumCores);
13298e9a21e730449c10cac6e6f69d255611c93f63c2hding
13308e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (jpeg_ctx_p->sScan_Encode_Info.ui16SScan > 0) {
13318e9a21e730449c10cac6e6f69d255611c93f63c2hding            ui32NoMCUsToEncode = jpeg_ctx_p->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
13328e9a21e730449c10cac6e6f69d255611c93f63c2hding        } else {
13338e9a21e730449c10cac6e6f69d255611c93f63c2hding            // Final scan, may need fewer MCUs than buffer size, calculate the remainder
13348e9a21e730449c10cac6e6f69d255611c93f63c2hding            ui32NoMCUsToEncode = ui32RemainMCUs;
13358e9a21e730449c10cac6e6f69d255611c93f63c2hding            jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].i8PipeNumber = 0;
13368e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
13378e9a21e730449c10cac6e6f69d255611c93f63c2hding
13388e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "ui32NoMCUsToEncode: %d\n", ui32NoMCUsToEncode);
13398e9a21e730449c10cac6e6f69d255611c93f63c2hding        //Send scan to MTX
13408e9a21e730449c10cac6e6f69d255611c93f63c2hding        rc = IssueBufferToHW(jpeg_ctx_p, ui16BCnt,
13418e9a21e730449c10cac6e6f69d255611c93f63c2hding                             jpeg_ctx_p->sScan_Encode_Info.aBufferTable[ui16BCnt].i8PipeNumber,
13428e9a21e730449c10cac6e6f69d255611c93f63c2hding                             ui32NoMCUsToEncode);
13438e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (rc != IMG_ERR_OK) {
13448e9a21e730449c10cac6e6f69d255611c93f63c2hding            vaStatus = VA_STATUS_ERROR_UNKNOWN;
13458e9a21e730449c10cac6e6f69d255611c93f63c2hding            DEBUG_FAILURE;
13468e9a21e730449c10cac6e6f69d255611c93f63c2hding            return vaStatus;
13478e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
13488e9a21e730449c10cac6e6f69d255611c93f63c2hding
13498e9a21e730449c10cac6e6f69d255611c93f63c2hding        ui32RemainMCUs -= ui32NoMCUsToEncode;
13508e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
13518e9a21e730449c10cac6e6f69d255611c93f63c2hding
13528e9a21e730449c10cac6e6f69d255611c93f63c2hding
13538e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(&cmdbuf->jpeg_pic_params);
13548e9a21e730449c10cac6e6f69d255611c93f63c2hding    cmdbuf->jpeg_pic_params_p = NULL;
13558e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(&cmdbuf->jpeg_header_mem);
13568e9a21e730449c10cac6e6f69d255611c93f63c2hding    cmdbuf->jpeg_header_mem_p = NULL;
13578e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(ps_buf->coded_buf->psb_buffer);
13588e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.pMemInfo = NULL;
13598e9a21e730449c10cac6e6f69d255611c93f63c2hding
13608e9a21e730449c10cac6e6f69d255611c93f63c2hding    psb_buffer_unmap(&(ctx->bufs_writeback));
13618e9a21e730449c10cac6e6f69d255611c93f63c2hding
13628e9a21e730449c10cac6e6f69d255611c93f63c2hding
1363430ce5c32361119ed81a62c918be6afbdd053eddhding    //tng__trace_cmdbuf(cmdbuf);
13648e9a21e730449c10cac6e6f69d255611c93f63c2hding
1365430ce5c32361119ed81a62c918be6afbdd053eddhding    if (tng_context_flush_cmdbuf(ctx->obj_context)) {
13668e9a21e730449c10cac6e6f69d255611c93f63c2hding        vaStatus = VA_STATUS_ERROR_UNKNOWN;
13678e9a21e730449c10cac6e6f69d255611c93f63c2hding        return vaStatus;
13688e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
13698e9a21e730449c10cac6e6f69d255611c93f63c2hding
13708e9a21e730449c10cac6e6f69d255611c93f63c2hding    ctx->obj_context->frame_count++;
13718e9a21e730449c10cac6e6f69d255611c93f63c2hding    return VA_STATUS_SUCCESS;
13728e9a21e730449c10cac6e6f69d255611c93f63c2hding}
13738e9a21e730449c10cac6e6f69d255611c93f63c2hding
13748e9a21e730449c10cac6e6f69d255611c93f63c2hding/* Add Restart interval termination (RSTm)to coded buf 1 ~ NumCores-1*/
1375430ce5c32361119ed81a62c918be6afbdd053eddhdingstatic inline VAStatus tng_OutputResetIntervalToCB(IMG_UINT8 *pui8Buf, IMG_UINT8 ui8_marker)
13768e9a21e730449c10cac6e6f69d255611c93f63c2hding{
13778e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (NULL == pui8Buf)
13788e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
13798e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*Refer to CCITT Rec. T.81 (1992 E), B.2.1*/
13808e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*RSTm: Restart marker conditional marker which is placed between
13818e9a21e730449c10cac6e6f69d255611c93f63c2hding     * entropy-coded segments only if restartis enabled. There are 8 unique
13828e9a21e730449c10cac6e6f69d255611c93f63c2hding     * restart markers (m = 0 - 7) which repeat in sequence from 0 to 7, starting with
13838e9a21e730449c10cac6e6f69d255611c93f63c2hding     * zero for each scan, to provide a modulo 8 restart interval count*/
13848e9a21e730449c10cac6e6f69d255611c93f63c2hding
13858e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui8Buf++ = 0xff;
13868e9a21e730449c10cac6e6f69d255611c93f63c2hding    *pui8Buf = (ui8_marker & 0x7) | 0xD0;
13878e9a21e730449c10cac6e6f69d255611c93f63c2hding
13888e9a21e730449c10cac6e6f69d255611c93f63c2hding    return 0;
13898e9a21e730449c10cac6e6f69d255611c93f63c2hding}
13908e9a21e730449c10cac6e6f69d255611c93f63c2hding
1391430ce5c32361119ed81a62c918be6afbdd053eddhdingVAStatus tng_jpeg_AppendMarkers(object_context_p obj_context, void *raw_coded_buf)
13928e9a21e730449c10cac6e6f69d255611c93f63c2hding{
13938e9a21e730449c10cac6e6f69d255611c93f63c2hding    context_ENC_p ctx = (context_ENC_p) obj_context->format_data;
13948e9a21e730449c10cac6e6f69d255611c93f63c2hding    TOPAZHP_JPEG_ENCODER_CONTEXT *jpeg_ctx_p = ctx->jpeg_ctx;
13958e9a21e730449c10cac6e6f69d255611c93f63c2hding
13968e9a21e730449c10cac6e6f69d255611c93f63c2hding    IMG_UINT16 ui16BCnt;
13978e9a21e730449c10cac6e6f69d255611c93f63c2hding    BUFFER_HEADER* pBufHeader;
13988e9a21e730449c10cac6e6f69d255611c93f63c2hding    STREAMTYPEW s_streamW;
13998e9a21e730449c10cac6e6f69d255611c93f63c2hding    void *pSegStart = raw_coded_buf;
14008e9a21e730449c10cac6e6f69d255611c93f63c2hding
14018e9a21e730449c10cac6e6f69d255611c93f63c2hding    if (pSegStart == NULL) {
14028e9a21e730449c10cac6e6f69d255611c93f63c2hding        return VA_STATUS_ERROR_UNKNOWN;
14038e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
14048e9a21e730449c10cac6e6f69d255611c93f63c2hding
14058e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader = (BUFFER_HEADER *)pSegStart;
14068e9a21e730449c10cac6e6f69d255611c93f63c2hding
14078e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Number of Coded buffers %d, Per Coded Buffer size : %d\n",
14088e9a21e730449c10cac6e6f69d255611c93f63c2hding                             jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers,
14098e9a21e730449c10cac6e6f69d255611c93f63c2hding                             jpeg_ctx_p->ui32SizePerCodedBuffer);
14108e9a21e730449c10cac6e6f69d255611c93f63c2hding
14118e9a21e730449c10cac6e6f69d255611c93f63c2hding    /*The first part of coded buffer contains JPEG headers*/
14128e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32Reserved3 = PTG_JPEG_HEADER_MAX_SIZE;
14138e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten = 0;
14148e9a21e730449c10cac6e6f69d255611c93f63c2hding
14158e9a21e730449c10cac6e6f69d255611c93f63c2hding    for (ui16BCnt = 0;
14168e9a21e730449c10cac6e6f69d255611c93f63c2hding         ui16BCnt < jpeg_ctx_p->sScan_Encode_Info.ui8NumberOfCodedBuffers;
14178e9a21e730449c10cac6e6f69d255611c93f63c2hding         ui16BCnt++) {
14188e9a21e730449c10cac6e6f69d255611c93f63c2hding        pBufHeader = (BUFFER_HEADER *)pSegStart;
14198e9a21e730449c10cac6e6f69d255611c93f63c2hding        pBufHeader->ui32Reserved3 =
14208e9a21e730449c10cac6e6f69d255611c93f63c2hding            PTG_JPEG_HEADER_MAX_SIZE + jpeg_ctx_p->ui32SizePerCodedBuffer * ui16BCnt ;
14218e9a21e730449c10cac6e6f69d255611c93f63c2hding
14228e9a21e730449c10cac6e6f69d255611c93f63c2hding        drv_debug_msg(VIDEO_DEBUG_GENERAL, "Coded Buffer Part %d, size %d, next part offset: %d\n",
14238e9a21e730449c10cac6e6f69d255611c93f63c2hding                                 ui16BCnt, pBufHeader->ui32BytesUsed, pBufHeader->ui32Reserved3);
14248e9a21e730449c10cac6e6f69d255611c93f63c2hding
14258e9a21e730449c10cac6e6f69d255611c93f63c2hding        if (ui16BCnt > 0) {
14268e9a21e730449c10cac6e6f69d255611c93f63c2hding            drv_debug_msg(VIDEO_DEBUG_GENERAL, "Append 2 bytes Reset Interval %d "
14278e9a21e730449c10cac6e6f69d255611c93f63c2hding                                     "to Coded Buffer Part %d\n", ui16BCnt - 1, ui16BCnt);
14288e9a21e730449c10cac6e6f69d255611c93f63c2hding
14298e9a21e730449c10cac6e6f69d255611c93f63c2hding            // OUTPUT RESTART INTERVAL TO CODED BUFFER
1430430ce5c32361119ed81a62c918be6afbdd053eddhding            tng_OutputResetIntervalToCB(
14318e9a21e730449c10cac6e6f69d255611c93f63c2hding                (IMG_UINT8 *)(pSegStart + sizeof(BUFFER_HEADER) + pBufHeader->ui32BytesUsed),
14328e9a21e730449c10cac6e6f69d255611c93f63c2hding                ui16BCnt - 1);
14338e9a21e730449c10cac6e6f69d255611c93f63c2hding
14348e9a21e730449c10cac6e6f69d255611c93f63c2hding            pBufHeader->ui32BytesUsed += 2;
14358e9a21e730449c10cac6e6f69d255611c93f63c2hding        }
14368e9a21e730449c10cac6e6f69d255611c93f63c2hding
14378e9a21e730449c10cac6e6f69d255611c93f63c2hding        jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten += pBufHeader->ui32BytesUsed;
14388e9a21e730449c10cac6e6f69d255611c93f63c2hding        pSegStart = raw_coded_buf + pBufHeader->ui32Reserved3;
14398e9a21e730449c10cac6e6f69d255611c93f63c2hding    }
14408e9a21e730449c10cac6e6f69d255611c93f63c2hding
14418e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader = (BUFFER_HEADER *)pSegStart;
14428e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32Reserved3 = 0; /*Last Part of Coded Buffer*/
14438e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten += pBufHeader->ui32BytesUsed;
14448e9a21e730449c10cac6e6f69d255611c93f63c2hding
14458e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Coded Buffer Part %d, size %d, next part offset: %d\n",
14468e9a21e730449c10cac6e6f69d255611c93f63c2hding                             ui16BCnt, pBufHeader->ui32BytesUsed, pBufHeader->ui32Reserved3);
14478e9a21e730449c10cac6e6f69d255611c93f63c2hding
14488e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Buffer = pSegStart;
14498e9a21e730449c10cac6e6f69d255611c93f63c2hding    s_streamW.Offset = (sizeof(BUFFER_HEADER) + pBufHeader->ui32BytesUsed);
14508e9a21e730449c10cac6e6f69d255611c93f63c2hding
14518e9a21e730449c10cac6e6f69d255611c93f63c2hding    fPutBitsToBuffer(&s_streamW, 2, END_OF_IMAGE);
14528e9a21e730449c10cac6e6f69d255611c93f63c2hding
14538e9a21e730449c10cac6e6f69d255611c93f63c2hding    pBufHeader->ui32BytesUsed += 2;
14548e9a21e730449c10cac6e6f69d255611c93f63c2hding    jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten += 2;
14558e9a21e730449c10cac6e6f69d255611c93f63c2hding
14568e9a21e730449c10cac6e6f69d255611c93f63c2hding    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Add two bytes to last part of coded buffer,"
14578e9a21e730449c10cac6e6f69d255611c93f63c2hding                             " total: %d\n", jpeg_ctx_p->jpeg_coded_buf.ui32BytesWritten);
14588e9a21e730449c10cac6e6f69d255611c93f63c2hding    return VA_STATUS_SUCCESS;
14598e9a21e730449c10cac6e6f69d255611c93f63c2hding}
14608e9a21e730449c10cac6e6f69d255611c93f63c2hding
1461430ce5c32361119ed81a62c918be6afbdd053eddhdingstruct format_vtable_s tng_JPEGES_vtable = {
14628e9a21e730449c10cac6e6f69d255611c93f63c2hdingqueryConfigAttributes:
1463430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_QueryConfigAttributes,
14648e9a21e730449c10cac6e6f69d255611c93f63c2hdingvalidateConfig:
1465430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_ValidateConfig,
14668e9a21e730449c10cac6e6f69d255611c93f63c2hdingcreateContext:
1467430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_CreateContext,
14688e9a21e730449c10cac6e6f69d255611c93f63c2hdingdestroyContext:
1469430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_DestroyContext,
14708e9a21e730449c10cac6e6f69d255611c93f63c2hdingbeginPicture:
1471430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_BeginPicture,
14728e9a21e730449c10cac6e6f69d255611c93f63c2hdingrenderPicture:
1473430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_RenderPicture,
14748e9a21e730449c10cac6e6f69d255611c93f63c2hdingendPicture:
1475430ce5c32361119ed81a62c918be6afbdd053eddhding    tng_jpeg_EndPicture
14768e9a21e730449c10cac6e6f69d255611c93f63c2hding};
1477