1 /*
2  * Copyright (c) 2011 Intel Corporation. All Rights Reserved.
3  * Copyright (c) Imagination Technologies Limited, UK
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sub license, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the
14  * next paragraph) shall be included in all copies or substantial portions
15  * of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
21  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  *
25  * Authors:
26  *    Elaine Wang <elaine.wang@intel.com>
27  *
28  */
29 
30 
31 #ifndef _PNW_HOST_JPEG_H_
32 #define _PNW_HOST_JPEG_H_
33 
34 #include <img_types.h>
35 
36 #define QUANT_TABLE_SIZE_BYTES  (64)             //!<JPEG input quantization table size
37 
38 #define MTX_MAX_COMPONENTS              (3)                             //<!JPEG input parameter sizes
39 
40 #define PNW_JPEG_COMPONENTS_NUM (3)
41 
42 #define PNW_JPEG_HEADER_MAX_SIZE (1024)
43 /*Limit the scan size to maximum useable (due to it being used as the
44  * 16 bit field for Restart Intervals) = 0xFFFF MCUs
45  * In reality, worst case allocatable bytes is less than this, something
46  * around 0x159739C == 0x4b96 MCUs = 139 x 139 MCUS = 2224 * 2224 pixels, approx.
47  * We'll give this upper limit some margin for error, and limit our
48  * MCUsPerScan to 2000 * 2000 pixels = 125 * 125 MCUS = 0x3D09 MCUS
49  * = 0x116F322 bytes (1170 worst case per MCU)*/
50 #define JPEG_MAX_MCU_PER_SCAN 0x3D09
51 
52 #define JPEG_MCU_NUMBER(width, height, eFormat) \
53     ((((width) + 15) / 16) * (((height) + 15) / 16) * \
54      (((eFormat) == IMG_CODEC_YV16) ? 2 : 1))
55 
56 #define JPEG_MCU_PER_CORE(width, height, core, eFormat) \
57         ((core) > 1 ? (((uint32_t)JPEG_MCU_NUMBER(width, height, eFormat) + (core) - 1) / (core))\
58          :(uint32_t)JPEG_MCU_NUMBER(width, height, eFormat))
59 
60 #define JPEG_SCANNING_COUNT(width, height, core, eFormat) \
61     ((uint32_t)(JPEG_MCU_PER_CORE(width, height, core, eFormat) > JPEG_MAX_MCU_PER_SCAN) ? \
62       ((uint32_t)(JPEG_MCU_NUMBER(width, height, eFormat) + JPEG_MAX_MCU_PER_SCAN - 1) \
63         / JPEG_MAX_MCU_PER_SCAN) \
64        : (core))
65 
66 #define JPEG_MCU_PER_SCAN(width, height, core, eFormat) \
67     ((JPEG_MCU_PER_CORE(width, height, core, eFormat) > JPEG_MAX_MCU_PER_SCAN) ? \
68     JPEG_MAX_MCU_PER_SCAN : JPEG_MCU_PER_CORE(width, height, core, eFormat))
69 
70 /*The start address of every segment must align 128bits -- DMA burst width*/
71 #define JPEG_CODED_BUF_SEGMENT_SIZE(total, width, height, core, eFormat) \
72     (((total) - PNW_JPEG_HEADER_MAX_SIZE) / \
73      JPEG_SCANNING_COUNT(ctx->Width, ctx->Height, ctx->NumCores, eFormat) \
74      & (~0xf))
75 
76 /*pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan=(pContext->sScan_Encode_Info.ui32NumberMCUsToEncode+pEncContext->i32NumCores-1)/pEncContext->i32NumCores;
77  *pContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes = (DATA_BUFFER_SIZE(pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan) +sizeof(BUFFER_HEADER)) + 3 & ~3;
78  ui32NumberMCUsToEncode is equal (width/16) * (height/16)
79  MAX_MCU_SIZE is 1170
80  For 352x288, size of data buffer is 231676.
81  The number of data buffer is equal to the number of cores minus one*/
82 #define PNW_JPEG_CODED_BUF_SIZE(width, height, NumCores)  ((((((width) + 15) / 16) * (((height) + 15) / 16) * MAX_MCU_SIZE ) + 0xf) & ~0xf)
83 
84 
85 typedef struct {
86     unsigned int        ui32Width;              //!< Width of the image component
87     unsigned int        ui32Stride;             //!< Stride of the image component
88     unsigned int        ui32Step;               //!< Step of the image component
89     unsigned int        ui32Height;             //!< Height of the image component
90 
91 } COMP_INFO;
92 
93 typedef struct {
94     unsigned int        ui32OutputWidth;                //!< Width of the JPEG image
95     unsigned int        ui32OutputHeight;               //!< Height of the JPEG image
96     unsigned int        ui32Components;                 //!< Number of components in the image ( 1 or 3 )
97     COMP_INFO   sCompInfo[3];                   //!< Array containing component info
98 
99 } IMG_JPEG_INFO;
100 
101 typedef enum _img_format_ {
102     IMG_CODEC_IYUV, /* IYUV */
103     IMG_CODEC_IMC2, /* IMC2 */
104     IMG_CODEC_PL8,
105     IMG_CODEC_PL12,
106     IMG_CODEC_NV12,
107     IMG_CODEC_YV16,
108 } IMG_FORMAT;
109 
110 typedef struct {
111     IMG_UINT32  ui32BytesUsed;          //!<
112     IMG_UINT32  ui32BytesEncoded;       //!<
113     IMG_UINT32  ui32BytesToEncode;      //!<
114     IMG_UINT32  ui32Reserved3;          //!<
115 
116 } BUFFER_HEADER;
117 
118 typedef enum {
119     IMG_ERR_OK                                  = 0,    //!< OK
120     IMG_ERR_SURFACE_LOCKED              = 1,    //!< The requested surface was locked
121     IMG_ERR_MEMORY                              = 2,    //!< A memory error occured
122     IMG_ERR_FILE                                = 3,    //!< A file error occured
123     IMG_ERR_NOBUFFERAVAILABLE   = 4,    //!< No buffer was available
124     IMG_ERR_COMPLETE                    = 5,    //!< Command is complete
125     IMG_ERR_INVALID_CONTEXT             = 6,    //!< An invalid context was given
126     IMG_ERR_INVALID_SIZE                = 7,    //!< An invalid size was given
127     IMG_ERR_TIMEOUT                             = 8,    //!< Timeout
128     IMG_ERR_UNDEFINED                   = -1
129 
130 } IMG_ERRORCODE;
131 
132 /*!
133  *  *****************************************************************************
134  *
135  * @details    Struct sent with the MTX_CMDID_ISSUEBUFF command detailing
136  *                                 where a scan encode should begin (calculated from the total count of MCUs)
137  *                                 and how many MCU's should be processed in this scan.
138  *
139  * @brief          JPEG structure defining scan start position and how many MCUs to process
140  *
141  ****************************************************************************/
142 typedef struct {
143     IMG_UINT32  ui32CurrentMTXScanMCUPosition;  //!< Scan start position in MCUs
144     IMG_UINT32  ui32MCUCntAndResetFlag;         //!< [32:2] Number of MCU's to encode or decode, [1] Reset predictors (1=Reset, 0=No Reset)
145 
146 } MTX_ISSUE_BUFFERS;
147 /*!
148  *  *****************************************************************************
149  *
150  * @details    Struct describing surface component info
151  *
152  * @brief          Surface component info
153  *
154  *****************************************************************************/
155 typedef struct {
156     IMG_UINT32 ui32Step;
157     IMG_UINT32 ui32Width;
158     IMG_UINT32 ui32Height;
159     IMG_UINT32 ui32PhysWidth;
160     IMG_UINT32 ui32PhysHeight;
161 
162 } IMG_SURF_COMPONENT_INFO;
163 
164 /*!
165  *  *****************************************************************************
166  *
167  * @details    Enum describing buffer lock status
168  *
169  * @brief          Buffer lock status
170  *
171  ****************************************************************************/
172 typedef enum {
173     BUFFER_FREE = 1,  //!< Buffer is not locked
174     HW_LOCK,          //!< Buffer is locked by hardware
175     SW_LOCK,          //!< Buffer is locked by software
176     NOTDEVICEMEMORY,    //!< Buffer is not a device memory buffer
177 } LOCK_STATUS;
178 
179 
180 /*!v
181  *  *****************************************************************************
182  *
183  * @details    Struct describing a coded data buffer
184  *
185  * @brief          Coded data buffer
186  *
187  * ****************************************************************************/
188 typedef struct {
189     void* pMemInfo;   //!< Pointer to the memory handle for the buffer
190     LOCK_STATUS sLock;                  //!< Lock status for the buffer
191     IMG_UINT32  ui32Size;               //!< Size in bytes of the buffer
192     IMG_UINT32  ui32BytesWritten;       //!< Number of bytes written into buffer
193 } IMG_BUFFER, IMG_CODED_BUFFER;
194 
195 
196 
197 /*!
198  *  *****************************************************************************
199  *
200  * @details    Struct describing a frame
201  *
202  * @brief          Frame information
203  *
204  ****************************************************************************/
205 typedef struct {
206     IMG_BUFFER *psBuffer;                           //!< pointer to the image buffer
207     IMG_UINT32 ui32Width;                                                       //!< stride of pBuffer
208     IMG_UINT32 ui32Height;                                                      //!< height of picture in pBuffer
209 
210     IMG_UINT32 ui32ComponentCount;                                      //!< number of colour components used
211     IMG_FORMAT eFormat;                                                         //!< Format of the surface
212 
213     IMG_UINT32  aui32ComponentOffset[3];                        //!< Offset of the planes within the surface
214     IMG_SURF_COMPONENT_INFO     aui32ComponentInfo[3];  //!< Component plane information
215 
216 } IMG_FRAME, JPEG_SOURCE_SURFACE;
217 #define MAX_NUMBER_OF_MTX_UNITS 4 // Number of MTX units
218 /* Number of buffers for encode coded data*/
219 #define NUMBER_OF_BUFFS 3 // Should be at least equal to number of MTX's for optimal performance
220 #define SIM_TEST_NUMBER_OF_BUFFS 3 // Should be at least equal to number of MTX's for optimal performance
221 #define BLOCKCOUNTMAXPERCOLOURPLANE 6 // Would be 10 for max theoretically possible.. 6 is our actual max
222 #define MAX_MCU_SIZE                    (((8*8)+1)*3 * BLOCKCOUNTMAXPERCOLOURPLANE)
223 #define SIM_TEST_MCUS_IN_BUFFER 22
224 #define RL_END_OF_BLOCK         0xff
225 #define DATA_BUFFER_SIZE(mcus_in_buffer) (MAX_MCU_SIZE*mcus_in_buffer)
226 #define DMEMORYMCUSATSIXSEVENTY_ESTIMATE 55488
227 
228 #define C_INTERLEAVE 1
229 #define LC_UVINTERLEAVE 2
230 #define LC_VUINTERLEAVE 3
231 
232 //#define ISCHROMAINTERLEAVED(eSurfaceFormat) ((IMG_UINT) (eSurfaceFormat==IMG_CODEC_Y0UY1V_8888)*LC_UVINTERLEAVE)+((IMG_UINT) (eSurfaceFormat==IMG_CODEC_Y0VY1U_8888)*LC_VUINTERLEAVE)+((IMG_UINT) (eSurfaceFormat==IMG_CODEC_PL12 || eSurfaceFormat==IMG_CODEC_422_PL12)*C_INTERLEAVE)
233 
234 #define ISCHROMAINTERLEAVED(eSurfaceFormat) ((IMG_UINT) (eSurfaceFormat==IMG_CODEC_PL12 || eSurfaceFormat==IMG_CODEC_NV12)*C_INTERLEAVE)
235 /*****************************************************************************/
236 /*  \brief   MAX_COMP_IN_SCAN                                                */
237 /*                                                                           */
238 /*  Maximum components that can be encoded in a scan as specified by the     */
239 /*  JPEG specification.                                                      */
240 /*****************************************************************************/
241 #define MAX_COMP_IN_SCAN                4
242 //#define MTX_CMD_BUF_SIZE      (32)
243 //#define MTX_MAX_COMPONENTS    (3)
244 #define PERF            1
245 
246 /******************************************************************************
247   General definitions for static header offsets (used for MJPEG A headers)
248  ******************************************************************************/
249 #define H_QT_OFFSET 46
250 #define H_HT_OFFSET 184
251 #define H_SOF_OFFSET 622
252 #define H_SOS_OFFSET 642
253 #define H_SOI_OFFSET 656
254 
255 
256 
257 /******************************************************************************
258   General definitions
259  ******************************************************************************/
260 #define BYTE                            8
261 #define BYTES_IN_INT                    4
262 #define BITS_IN_INT                     32
263 #define BLOCK_SIZE                      8
264 #define PELS_IN_BLOCK                   64
265 
266 /******************************************************************************
267   MJPEG A marker definitions
268  ******************************************************************************/
269 #define MJPEG_APP1                                      0xFFE1
270 
271 /******************************************************************************
272   JPEG marker definitions
273  ******************************************************************************/
274 #define START_OF_IMAGE              0xFFD8
275 #define SOF_BASELINE_DCT            0xFFC0
276 #define END_OF_IMAGE                0xFFD9
277 #define START_OF_SCAN               0xFFDA
278 
279 
280 
281 
282 /* Definitions for the huffman table specification in the Marker segment */
283 #define DHT_MARKER                  0xFFC4
284 #define LH_DC                       0x001F
285 #define LH_AC                       0x00B5
286 #define LEVEL_SHIFT                 128
287 
288 /* Definitions for the quantization table specification in the Marker segment */
289 #define DQT_MARKER                  0xFFDB
290 #define ACMAX                       0x03FF
291 #define DCMAX                       0x07FF
292 /* Length and precision of the quantization table parameters */
293 #define LQPQ                        0x00430
294 #define QMAX                        255
295 #define CLIP(Number,Max,Min)    if((Number) > (Max)) (Number) = (Max); \
296     else if((Number) < (Min)) (Number) = (Min)
297 #define AVAILABLE                       ( 0 )
298 #define JPEG_ENCODE_LOCK        ( 0x20 )
299 
300 
301 
302 //////////////////////////////////////////////////////////////////////////////////////////////
303 // Structures unchanged by TopazSc
304 //////////////////////////////////////////////////////////////////////////////////////////////
305 
306 //typedef struct
307 //{
308 //      IMG_UINT32 ui32BytesUsed;
309 //      IMG_UINT32 ui32BytesEncoded;
310 //      IMG_UINT32 ui32Reserved2;
311 //      IMG_UINT32 ui32Reserved3;
312 //} BUFFER_HEADER;
313 
314 /*****************************************************************************/
315 /*  STREAMTYPEW                                                                          */
316 /*  Container to store the stream context                                    */
317 /*****************************************************************************/
318 typedef struct {
319     IMG_UINT8 *Buffer; /*!< Ptr to the bitstream buffer */
320     IMG_UINT32 Offset;  /*!< Offset in the bitstream buffer */
321     IMG_UINT32 Limit;
322 } STREAMTYPEW;
323 
324 //typedef struct
325 //{
326 //      IMG_UINT32 ui32WidthBlocks ;    /* Width in pixels, shall be a multiple of 8*/
327 //      IMG_UINT32 ui32HeightBlocks ;   /* Height in pixels, shall be a multiple of 8*/
328 //      IMG_UINT32 ui32XLimit;                  /* Blocks will not be encoded beyond this */
329 //      IMG_UINT32 ui32YLimit;                  /* Blocks will not be encoded beyond this */
330 //} MCUCOMPONENT;
331 //
332 //typedef struct
333 //{
334 //      IMG_UINT32 ui32PhysAddr;                /* Physical address Component plane in shared memory*/
335 //      IMG_UINT32 ui32Stride;                  /* Stride of source plane */
336 //      IMG_UINT32 ui32Height;                  /* Height of avaliable data in plane.  shall be a minumum of one MCU high */
337 //} COMPONENTPLANE;
338 //
339 //
340 //typedef struct
341 //{
342 ///// The following values are DMA'd straight across from host
343 //      COMPONENTPLANE  ComponentPlane[MTX_MAX_COMPONENTS];
344 //      MCUCOMPONENT    MCUComponent[MTX_MAX_COMPONENTS];
345 //      IMG_UINT32 ui32ComponentsInScan;        /* Number of compnents */
346 //      IMG_UINT32 ui32TableA;          /* Quantisation table for Luma component*/
347 //      IMG_UINT32 ui32DataInterleaveStatus; /*What kind of interleaving is required*/
348 //} JPEG_MTX_DMA_SETUP;
349 
350 
351 //typedef struct
352 //{
353 ////    IMG_UINT32  ui32BufferPhysAddr;         /*  Physical address of buffer table in shared memory*/
354 //      IMG_UINT32  ui32CurrentMTXScanMCUPosition; // Scan start position in MCUs
355 //    IMG_UINT32  ui32SizeAndProcMCUCount;/*    [31:16] Valid data in bytes
356 //                                                                                      [15:0] Number of MCU's to encode or decode */
357 //} MTX_ISSUE_BUFFERS;
358 
359 
360 
361 
362 //////////////////////////////////////////////////////////////////////////////////////////////
363 // Old Topaz structures (retained for backward compatibility with sim tests)
364 //////////////////////////////////////////////////////////////////////////////////////////////
365 
366 typedef struct {
367     IMG_UINT32 ui32BytesPendingEDMA;
368     IMG_UINT32 ui32BytesEncoded;
369     IMG_UINT32 ui32BlocksEncoded;
370 } LEGACY_ENCODE_HEADER;
371 
372 typedef enum {
373     BUFF_BUSY_IN_HW_0,  /* Indicates that this buffer is in use by the hardware */
374     BUFF_BUSY_IN_SW,    /* Indicates that this buffer is in use by sw */
375     BUFF_AVALIABLE,             /* Indicates this buffer can be submitted to hw */
376 } LEGACY_BUFFER_STATUS;
377 
378 /*****************************************************************************/
379 /*  \brief   _EncType                                                        */
380 /*                                                                           */
381 /*  JPEG low level encoder context structure                                           */
382 /*****************************************************************************/
383 typedef struct {
384     //    STREAMTYPEW streamW;    /*!< Ptr to the stream context */
385     const IMG_UINT16 *ACCode[2];     /*!< Ptr to the huffman tables to code AC coeffs */
386     const IMG_UINT8  *ACSize[2];     /*!< Ptr to the huffman tables to code AC coeffs */
387     const IMG_UINT16 *DCCode[2];     /*!< Ptr to the huffman tables to code DC coeffs */
388     const IMG_UINT8  *DCSize[2];     /*!< Ptr to the huffman tables to code DC coeffs */
389 
390     IMG_UINT8  *Qmatrix[MAX_COMP_IN_SCAN];/*!< Ptr to the Quant tables */
391     IMG_UINT8  uc_num_mcu_x;   /*!< Number of MCUs in the horizontal direction */
392     IMG_UINT8  uc_num_mcu_y;   /*!< Number of MCUs in the vertical direction */
393 
394     IMG_UINT8  aui8QuantTable[MAX_COMP_IN_SCAN][64]; /* Area for creating tables */
395 } LEGACY_ENCTYPE;
396 
397 typedef struct {
398     IMG_UINT8*  pData;
399     IMG_UINT32  ui32DataBufferSize;
400     IMG_UINT32  OpNum;
401     IMG_UINT32  ui32numberOfMCUs;
402     LEGACY_BUFFER_STATUS        eBufferStatus;
403     struct MEMORY_INFO_TAG* pMemInfo;
404 
405     IMG_UINT32  ui32CurrentIndexAccessPos;
406     IMG_UINT32  ui32CurrentIndexHeaderPos;
407 #ifdef PERF
408     IMG_UINT32  ui32IssueTime;
409 #endif
410 
411 } LEGACY_BUFFERINFO;
412 
413 
414 /*****************************************************************************/
415 /*  LEGACY_JPEGENC_ITTIAM_PARAMS                                                 */
416 /*                                                                           */
417 /*  Pointer to the JPEG encoder parameter context. This structure is used by */
418 /*  the sample application to pass configuration parameters to the encoder.  */
419 /*****************************************************************************/
420 typedef struct {
421     IMG_UINT8  uc_num_q_tables;        /*!< Number of Q tables */
422     IMG_UINT16 ui16_q_factor;          /*!< Quality factor */
423     IMG_UINT8  puc_q_table_id[4];      /*!< Q table ID */
424     IMG_UINT8  puc_q_table[4][64];     /*!< Q tables */
425     IMG_UINT8  uc_isAbbreviated;       /*!< 0: Interchange ; 1: Abbreviated */
426     IMG_UINT8  uc_num_comp_in_scan;    /*!< Number of components in scan (<= 3) */
427     IMG_UINT8  puc_comp_id[MAX_COMP_IN_SCAN]; /*!< Component identifier */
428     IMG_UINT16 ui16_width;             /*!< Width of the JPEG image */
429     IMG_UINT16 ui16_height;            /*!< Height of the JPEG image */
430 } LEGACY_JPEGENC_ITTIAM_PARAMS;
431 
432 typedef struct {
433     IMG_UINT8  uc_num_comp_in_img;    /*!< Number of components in image */
434     IMG_UINT8  puc_comp_id[255];      /*!< Component identifier */
435     IMG_UINT8  puc_q_table_id[255];   /*!< Q table id to use */
436     IMG_UINT8  puc_huff_table_id[255];/*!< Huff table id to use */
437     IMG_UINT8 *puc_comp_buff[255];    /*!< Ptr to the component buff */
438     IMG_UINT16 pui16_comp_width[255]; /*!< Width of the component buff */
439     IMG_UINT16 pui16_comp_height[255];/*!< Height of the component buff */
440     IMG_UINT8  puc_horiz_scale[MAX_COMP_IN_SCAN];/*!< Component horizontal scale factor */
441     IMG_UINT8  puc_vert_scale[MAX_COMP_IN_SCAN]; /*!< Component vertical scale factor */
442 
443     IMG_UINT8 CompIdtoIndex[MAX_COMP_IN_SCAN];
444 
445 } LEGACY_JPEGENC_ITTIAM_COMPONENT;
446 
447 typedef enum {
448     LEGACY_JPEG_API_CURRENT_ACTIVE_NONE,
449     LEGACY_JPEG_API_CURRENT_ACTIVE_ENCODE,
450     LEGACY_JPEG_API_CURRENT_ACTIVE_DECODE,
451 
452 } LEGACY_JPEG_API_CURRENT_ACTIVE;
453 
454 
455 /*!
456  *  *****************************************************************************
457  *
458  * @details    Struct describing Minimum Coded Unit information for a single JPEG component plane.
459  *  Details the size of blocks to be taken from the plane and the maximum block positions.
460  *  Send to firmware in the MTX_CMDID_SETUP command as part of the JPEG_MTX_DMA_SETUP structure
461  *
462  * @brief          JPEG Minimum Coded Unit Information
463  *
464  *****************************************************************************/
465 typedef struct {
466     IMG_UINT32  ui32WidthBlocks;        //!< Width in pixels, shall be a multiple of 8
467     IMG_UINT32  ui32HeightBlocks;       //!< Height in pixels, shall be a multiple of 8
468     IMG_UINT32  ui32XLimit;                     //!< Blocks will not be encoded beyond this
469     IMG_UINT32  ui32YLimit;                     //!< Blocks will not be encoded beyond this
470 
471 } MCUCOMPONENT;
472 
473 
474 /*!
475  *  *****************************************************************************
476  *
477  *   @details    Struct describing essential information about a single JPEG component plane, defines the
478  *         Physical address of the colour plane, its stride and its height.
479  *      Send to firmware in the MTX_CMDID_SETUP command as part of the JPEG_MTX_DMA_SETUP structure.
480  *
481  *   @brief          Basic information for a single JPEG component plane, passed to firmware.
482  *
483  *****************************************************************************/
484 typedef struct {
485     IMG_UINT32  ui32PhysAddr;   //!< Physical address Component plane in shared memory
486     IMG_UINT32  ui32Stride;             //!< Stride of source plane */
487     IMG_UINT32  ui32Height;             //!< Height of avaliable data in plane.  shall be a minumum of one MCU high
488 
489 } COMPONENTPLANE;
490 
491 
492 /*!
493  *  *****************************************************************************
494  *
495  *  @details    Struct describing essential information required by firmware to encode a scan.
496  *         Send to firmware in the MTX_CMDID_SETUP command.
497  *
498  *  @brief          Setup information for a single JPEG scan.
499  *
500  *****************************************************************************/
501 typedef struct {
502     COMPONENTPLANE      ComponentPlane[MTX_MAX_COMPONENTS];     //!< Array of component plane source information (detailing physical address, stride and height)
503     MCUCOMPONENT        MCUComponent[MTX_MAX_COMPONENTS];       //!< Array of Minimum Coded Unit information for each component plane
504     IMG_UINT32          ui32ComponentsInScan;                           //!< Number of components
505     IMG_UINT32          ui32TableA;                                                     //!< Quantisation table for Luma component
506     IMG_UINT32          ui32DataInterleaveStatus;                       //!< Source component interleave status (0, C_INTERLEAVE, LC_UVINTERLEAVE or LC_VUINTERLEAVE)
507 
508 } JPEG_MTX_DMA_SETUP;
509 
510 /* JPEG HW Interface state structure */
511 typedef struct {
512     struct MEMORY_INFO_TAG* pMemInfoMTXSetup;
513     JPEG_MTX_DMA_SETUP* pMTXSetup;
514 
515     struct MEMORY_INFO_TAG* pMemInfoTableBlock;
516     IMG_UINT8           *pui8TablesBlock;
517 
518     LEGACY_ENCODE_HEADER* pLastEncodeHeader;
519 
520     LEGACY_BUFFERINFO           asRLCBuffers[SIM_TEST_NUMBER_OF_BUFFS];
521 
522     IMG_UINT8*          pLocalMemBuffer;
523 
524     IMG_UINT32          ui32CurrentIndex;
525 
526     /* These are used by by encode */
527     IMG_UINT32          rlcWriteIndex;
528     IMG_UINT32          rlcReadIndex;
529 
530     /* It would be nice to get rid of this */
531     LEGACY_BUFFERINFO*  pCurrBuff;
532     IMG_UINT8*          pReadFromRLE;
533 
534     IMG_UINT32          uiLastDCValue;
535 
536     IMG_BOOL bNeedRestart;
537 
538 #ifdef PERF
539     IMG_UINT32 ui32PendingBufferTime;
540     IMG_UINT32 ui32ProccessDelay;
541     IMG_UINT32 ui32TotalBytesTX;
542     IMG_UINT32 ui32TotalTXTime;
543 #endif
544 
545 } LEGACY_MTXJPEG_HOST_STATE;
546 
547 typedef struct {
548     LEGACY_JPEGENC_ITTIAM_PARAMS JPEGEncoderParams;
549     LEGACY_JPEGENC_ITTIAM_COMPONENT sJPEGEncoderComp;
550     IMG_UINT32 BytesUsed;
551     IMG_UINT8* pui8ByteBuffer;
552     IMG_JPEG_INFO       sJpegInfo ;
553     LEGACY_ENCTYPE*     pvLowLevelEncContext;
554     LEGACY_JPEG_API_CURRENT_ACTIVE      eCurrentActive;
555     struct MEMORY_INFO_TAG *pHWSync;
556     LEGACY_MTXJPEG_HOST_STATE* gpMTXHostState;
557     IMG_UINT32 ui32MCUsWaitingToBeSentToHW;
558     IMG_UINT32 ui32MCUsPendingHWEnc;
559     IMG_UINT32 ui32BlocksHWEncoded;
560     IMG_UINT8 ui32JPEGBytesWritten;
561     IMG_UINT32 MCUsToDo;
562     IMG_UINT16 ui16Quality;
563     IMG_HANDLE ui32Handles[MTX_MAX_COMPONENTS];
564     IMG_UINT32 ui32Offsets[MTX_MAX_COMPONENTS];
565     JPEG_SOURCE_SURFACE sSurf;
566 
567 } LEGACY_JPEG_ENCODER_CONTEXT;
568 
569 
570 
571 //////////////////////////////////////////////////////////////////////////////////////////////
572 // New TopazSc structures
573 //////////////////////////////////////////////////////////////////////////////////////////////
574 
575 typedef struct {
576     unsigned char * pMemInfo;
577     IMG_UINT16 ui16ScanNumber;
578     IMG_UINT32 ui32WriteBackVal;
579     IMG_INT8 i8MTXNumber; // Doubles as status indicator ( <0 = Awaiting output to CB, 0 = Idle, >0 = Being filled by MTX)
580     IMG_UINT32  MTXOpNum; // Handle returned from MTX issuebuff command, can be used to check for completion
581     IMG_UINT32 ui32DataBufferSizeBytes;
582     IMG_UINT32 ui32DataBufferUsedBytes;
583 } TOPAZSC_JPEG_BUFFER_INFO;
584 
585 typedef struct {
586     IMG_UINT16 ui16CScan; /*The number of scans to be done, ui32NumberMCUsToEncode / ui32NumberMCUsToEncodePerScan*/
587     IMG_UINT16 ui16SScan; /*The current index of scan*/
588     IMG_UINT16 ui16ScansInImage;
589     IMG_UINT8 ui8MTXIdleCnt;
590     IMG_UINT8 aui8MTXIdleTable[MAX_NUMBER_OF_MTX_UNITS];
591     TOPAZSC_JPEG_BUFFER_INFO *aBufferTable;
592     IMG_UINT32 ui32NumberMCUsX; /* Width / 16*/
593     IMG_UINT32 ui32NumberMCUsY; /* Height / 16*/
594     IMG_UINT32 ui32NumberMCUsToEncode; /*Total number of MCUs to encode*/
595     IMG_UINT32 ui32NumberMCUsToEncodePerScan; /*Number of MCUs per scan, should be  ui32NumberMCUsX * ui32NumberMCUsY*/
596     IMG_UINT8 ui8NumberOfCodedBuffers;
597     IMG_UINT32 ui32CurMCUsOffset;
598 } TOPAZSC_SCAN_ENCODE_INFO;
599 
600 typedef struct {
601     IMG_UINT8   aui8LumaQuantParams[QUANT_TABLE_SIZE_BYTES];    //!< Luma quant params
602     IMG_UINT8   aui8ChromaQuantParams[QUANT_TABLE_SIZE_BYTES];  //!< Chroma quant params
603 
604 } JPEG_MTX_QUANT_TABLE;
605 
606 typedef struct context_jpeg_ENC_s {
607 
608     IMG_FORMAT eFormat;
609     /*IMG_UINT16 ui16Quality;*/
610     IMG_UINT32 ui32OutputWidth;
611     IMG_UINT32 ui32OutputHeight;
612     IMG_UINT32 ui32InitialCBOffset;
613 
614     object_surface_p pSourceSurface;
615     unsigned char * pMemInfoMTXSetup;
616     JPEG_MTX_DMA_SETUP* pMTXSetup;
617 
618     unsigned char * pMemInfoTableBlock;
619     JPEG_MTX_QUANT_TABLE        *psTablesBlock;
620 
621     IMG_UINT32 ui32Offsets[MTX_MAX_COMPONENTS];
622 
623     TOPAZSC_SCAN_ENCODE_INFO sScan_Encode_Info;
624 
625     IMG_CODED_BUFFER jpeg_coded_buf;
626 
627     unsigned char *ctx;
628     IMG_UINT32 ui32SizePerCodedBuffer;
629     IMG_UINT8  ui8ScanNum;
630 } TOPAZSC_JPEG_ENCODER_CONTEXT;
631 
632 //////////////////////////////////////////////////////
633 //Function Declarations unchanged by TopazSC
634 //////////////////////////////////////////////////////
635 int customize_quantization_tables(unsigned char *luma_matrix,
636                                   unsigned char *chroma_matrix,
637                                   unsigned int ui32Quality);
638 
639 void SetCompInfoFromFormat(IMG_FORMAT eFormat, IMG_UINT32 ui32Width, IMG_UINT32 ui32Height, IMG_JPEG_INFO       *psJpegInfo);
640 /* This function will copy data from one buffer to another where the destination has a different stride */
641 void APP_CopyToWithStride(IMG_UINT8* pSrc, IMG_UINT8* pDest , IMG_UINT32 ui32SrcWidth, IMG_UINT32 ui32SrcPhysWidth, IMG_UINT32 ui32SrcStep, IMG_UINT32 ui32SrcHeight, IMG_UINT32 ui32DestPhysWidth, IMG_UINT32 ui32DestPhysHeight);
642 IMG_ERRORCODE SetupJPEGSourceSurface(IMG_FORMAT eSurfaceFormat, IMG_UINT32 ui32FrameWidth, IMG_UINT32 ui32FrameHeight, JPEG_SOURCE_SURFACE *pSurf);
643 //void FlushByteAlignedBuffer(IMG_UINT32 *pui_buff, IMG_UINT32 *ui_bytes_to_flush, FILE *pf_out);
644 
645 //////////////////////////////////////////////////////
646 //Legacy function declarations - most of these will eventually be removed
647 //////////////////////////////////////////////////////
648 IMG_ERRORCODE Legacy_PrepareHeader(LEGACY_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER *pCBuffer);
649 //IMG_ERRORCODE Legacy_AllocateCodedDataBuffers(LEGACY_JPEG_ENCODER_CONTEXT *pContext);
650 //IMG_ERRORCODE Legacy_host_InitialiseHardware(LEGACY_JPEG_ENCODER_CONTEXT *pContext);
651 //IMG_ERRORCODE Legacy_IMG_JPEG_EndPicture(IMG_HENC_CONTEXT hEncContext, IMG_CODED_BUFFER *pCBuffer);
652 //IMG_ERRORCODE Legacy_IMG_JPEG_FreeBuffer(LEGACY_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER **ppCBuffer);
653 //IMG_ERRORCODE Legacy_IMG_JPEG_DeAllocateFrames(LEGACY_JPEG_ENCODER_CONTEXT *pContext,IMG_FRAME_ARRAY **pFrameArray);
654 //IMG_ERRORCODE Legacy_IMG_JPEG_EncoderInitialise(IMG_UINT16 ui16Width, IMG_UINT16 ui16Height, IMG_FORMAT eFormat, LEGACY_JPEG_ENCODER_CONTEXT ** ppContext);
655 //IMG_ERRORCODE Legacy_IMG_JPEG_EncoderDeInitialise(LEGACY_JPEG_ENCODER_CONTEXT * pContext);
656 //IMG_ERRORCODE Legacy_IMG_JPEG_AllocateFrames(LEGACY_JPEG_ENCODER_CONTEXT *pContext, IMG_UINT16 ui16ArraySize,IMG_FRAME_ARRAY **ppFrameArray);
657 //IMG_ERRORCODE Legacy_IMG_JPEG_AllocateCodedBuffer(LEGACY_JPEG_ENCODER_CONTEXT * pContext, IMG_UINT32 ui32CBufferSize, IMG_CODED_BUFFER **ppCBuffer);
658 //IMG_ERRORCODE Legacy_IMG_JPEG_StartPicture(IMG_HENC_CONTEXT hEncContext, IMG_UINT16 ui16Quality, IMG_CODED_BUFFER *pCBuffer,IMG_FRAME *pTFrame);
659 
660 //////////////////////////////////////////////////////
661 //TopazSc Function Declarations
662 //////////////////////////////////////////////////////
663 IMG_ERRORCODE PrepareHeader(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER *pCBuffer, IMG_UINT32 ui32StartOffset, IMG_BOOL bIncludeHuffmanTables);
664 IMG_ERRORCODE AllocateCodedDataBuffers(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext);
665 //IMG_ERRORCODE IMG_JPEG_EndPicture(IMG_HENC_CONTEXT hEncContext, IMG_CODED_BUFFER *pCBuffer);
666 //IMG_ERRORCODE IMG_JPEG_FreeBuffer(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER **ppCBuffer);
667 //IMG_ERRORCODE IMG_JPEG_DeAllocateFrames(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext,IMG_FRAME_ARRAY **pFrameArray);
668 //IMG_ERRORCODE IMG_JPEG_EncoderInitialise(IMG_HENC_CONTEXT hEncContext,IMG_UINT16 ui16Width, IMG_UINT16 ui16Height, IMG_FORMAT eFormat, TOPAZSC_JPEG_ENCODER_CONTEXT ** ppContext);
669 //IMG_ERRORCODE IMG_JPEG_EncoderDeInitialise(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext);
670 //IMG_ERRORCODE IMG_JPEG_AllocateFrames(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext, IMG_UINT32 ui32ArraySize,IMG_FRAME_ARRAY **ppFrameArray);
671 IMG_ERRORCODE IMG_JPEG_AllocateCodedBuffer(IMG_UINT32 ui32CBufferSize, IMG_CODED_BUFFER **ppCBuffer);
672 //IMG_ERRORCODE IMG_JPEG_StartPicture(IMG_HENC_CONTEXT hEncContext, IMG_UINT16 ui16Quality, IMG_CODED_BUFFER *pCBuffer,IMG_FRAME *pTFrame, IMG_UINT32 ui32StartOffset, IMG_BOOL bIncludeHuffmanTables);
673 
674 IMG_ERRORCODE InitializeJpegEncode(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext, object_surface_p pTFrame);
675 IMG_ERRORCODE SetupJPEGTables(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER *pCBuffer,  object_surface_p pTFrame);
676 IMG_ERRORCODE SubmitScanToMTX(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext, IMG_UINT16 ui16BCnt, IMG_INT8 i8MTXNumber, IMG_UINT32 ui32NoMCUsToEncode);
677 void pnw_jpeg_set_default_qmatix(unsigned char *pMemInfoTableBlock);
678 void fPutBitsToBuffer(STREAMTYPEW *BitStream, IMG_UINT8 NoOfBytes, IMG_UINT32 ActualBits);
679 #endif /*_HOST_JPEG_H_*/
680