pnw_hostjpeg.c revision c60d5b7bdb5616ca37e0b912c10725bec4320f33
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/*#include <string.h>*/
31#include <stdio.h>
32
33#include <pnw_hostcode.h>
34#include <pnw_hostjpeg.h>
35#include "psb_drv_debug.h"
36
37#define TRACK_FREE(ptr)                 free(ptr)
38#define TRACK_MALLOC(ptr)               malloc(ptr)
39#define TRACK_DEVICE_MEMORY_INIT
40#define TRACK_DEVICE_MEMORY_SHOW
41#define TIMER_INIT
42#define TIMER_START(...)
43#define TIMER_END(...)
44#define TIMER_CLOSE
45#define TIMER_CAPTURE(...)
46
47
48//#include <stdio.h>
49//#include <memory.h>
50//#include <stdlib.h>
51//#include <stdlib.h>
52//#include "malloc.h"
53//#include "mtxmain.h"
54
55//#include "mtxccb.h"
56//#include "topaz_tests.h"
57//#include "mvea_regs.h"
58//#include "mtx_regs.h"
59//#include "defs.h"
60//#include "mtxencode.h"
61//#include "topaz_vlc_regs.h"
62//#include "mtxheader.h"
63//#include "apiinternal.h"
64
65//#ifdef FIXME
66//extern void JPEGInitialiseHardware();
67//#endif
68
69#define JPEG_INCLUDE_NONAPI 1
70
71
72
73/////////////////////////////////////////////////////////////////////////////////////
74// BMP Reading Header Stuff
75/////////////////////////////////////////////////////////////////////////////////////
76
77
78const IMG_UINT8 gQuantLuma[QUANT_TABLE_SIZE_BYTES] = {   16, 11, 10, 16, 24, 40, 51, 61,
79        12, 12, 14, 19, 26, 58, 60, 55,
80        14, 13, 16, 24, 40, 57, 69, 56,
81        14, 17, 22, 29, 51, 87, 80, 62,
82        18, 22, 37, 56, 68, 109, 103, 77,
83        24, 35, 55, 64, 81, 104, 113, 92,
84        49, 64, 78, 87, 103, 121, 120, 101,
85        72, 92, 95, 98, 112, 100, 103, 99
86                                                     };
87
88
89/*const IMG_UINT8 DEBUG_gQuantLumaForCoverage[QUANT_TABLE_SIZE_BYTES] =
90{
91    16, 17, 21, 22, 30, 31, 22, 45,
92    18, 20, 23, 29, 32, 29, 44, 46,
93    19, 24, 28, 33, 36, 43, 47, 58,
94    25, 27, 34, 29, 42, 48, 57, 59,
95    26, 34, 37, 41, 49, 56 , 60 , 67,
96    36, 35, 40, 50, 55, 61 , 66 , 68,
97    37, 39, 51, 54, 62 , 65 , 69 , 72 ,
98    38, 52, 53, 63, 64 , 70 , 71 , 73
99};*/
100
101int DEBUG_giUsegQuantLumaForCoverageTable = 0;
102
103/*****************************************************************************/
104/*  \brief   gQuantChroma                                                    */
105/*                                                                           */
106/*  Contains the data that needs to be sent in the marker segment of an      */
107/*  interchange format JPEG stream or an abbreviated format table            */
108/*  specification data stream.                                               */
109/*  Quantizer table for the chrominance component                            */
110/*****************************************************************************/
111const IMG_UINT8 gQuantChroma[QUANT_TABLE_SIZE_BYTES] = {
112    17, 18, 24, 47, 99, 99, 99, 99,
113    18, 21, 26, 66, 99, 99, 99, 99,
114    24, 26, 56, 99, 99, 99, 99, 99,
115    47, 66, 99, 99, 99, 99, 99, 99,
116    99, 99, 99, 99, 99, 99, 99, 99,
117    99, 99, 99, 99, 99, 99, 99, 99,
118    99, 99, 99, 99, 99, 99, 99, 99,
119    99, 99, 99, 99, 99, 99, 99, 99
120};
121
122/*****************************************************************************/
123/*  \brief   gZigZag                                                         */
124/*                                                                           */
125/*  Zigzag scan pattern                                                      */
126/*****************************************************************************/
127const IMG_UINT8 gZigZag[] = {
128    0,  1,  8, 16,  9,  2,  3, 10,
129    17, 24, 32, 25, 18, 11,  4,  5,
130    12, 19, 26, 33, 40, 48, 41, 34,
131    27, 20, 13,  6,  7, 14, 21, 28,
132    35, 42, 49, 56, 57, 50, 43, 36,
133    29, 22, 15, 23, 30, 37, 44, 51,
134    58, 59, 52, 45, 38, 31, 39, 46,
135    53, 60, 61, 54, 47, 55, 62, 63
136};
137
138/*****************************************************************************/
139/*  \brief   gMarkerDataLumaDc                                               */
140/*                                                                           */
141/*  Contains the data that needs to be sent in the marker segment of an      */
142/*  interchange format JPEG stream or an abbreviated format table            */
143/*  specification data stream.                                               */
144/*  Specifies the huffman table used for encoding the luminance DC           */
145/*  coefficient differences. The table represents Table K.3 of               */
146/*  IS0/IEC 10918-1:1994(E)                                                  */
147/*****************************************************************************/
148const IMG_UINT8 gMarkerDataLumaDc[] = {
149    //TcTh  Li
150    0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00,
151    0x00, 0x00, 0x00, 0x00, 0x00,
152    // Vi
153    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
154};
155
156/*****************************************************************************/
157/*  \brief   gMarkerDataLumaAc                                               */
158/*                                                                           */
159/*  Contains the data that needs to be sent in the marker segment of an      */
160/*  interchange format JPEG stream or an abbreviated format table            */
161/*  specification data stream.                                               */
162/*  Specifies the huffman table used for encoding the luminance AC           */
163/*  coefficients. The table represents Table K.5 of IS0/IEC 10918-1:1994(E)  */
164/*****************************************************************************/
165const IMG_UINT8 gMarkerDataLumaAc[] = {
166    // TcTh  Li
167    0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03, 0x05, 0x05, 0x04,
168    0x04, 0x00, 0x00, 0x01, 0x7D,
169    // Vi
170    0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
171    0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1, 0x08,
172    0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33, 0x62, 0x72,
173    0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26, 0x27, 0x28,
174    0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44, 0x45,
175    0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
176    0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75,
177    0x76, 0x77, 0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
178    0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3,
179    0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6,
180    0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9,
181    0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
182    0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4,
183    0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA
184};
185
186/*****************************************************************************/
187/*  \brief   gMarkerDataChromaDc                                             */
188/*                                                                           */
189/*  Contains the data that needs to be sent in the marker segment of an      */
190/*  interchange format JPEG stream or an abbreviated format table            */
191/*  specification data stream.                                               */
192/*  Specifies the huffman table used for encoding the chrominance DC         */
193/*  coefficient differences. The table represents Table K.4 of               */
194/*  IS0/IEC 10918-1:1994(E)                                                  */
195/*****************************************************************************/
196const IMG_UINT8 gMarkerDataChromaDc[] = {
197    // TcTh Li
198    0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
199    0x00, 0x00, 0x00, 0x00, 0x00,
200
201    // Vi
202    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B
203};
204
205/*****************************************************************************/
206/*  \brief   gMarkerDataChromaAc                                             */
207/*                                                                           */
208/*  Contains the data that needs to be sent in the marker segment of an      */
209/*  interchange format JPEG stream or an abbreviated format table            */
210/*  specification data stream.                                               */
211/*  Specifies the huffman table used for encoding the chrominance AC         */
212/*  coefficients. The table represents Table K.6 of IS0/IEC 10918-1:1994(E)  */
213/*****************************************************************************/
214const IMG_UINT8 gMarkerDataChromaAc[] = {
215    // TcTh
216    0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05, 0x04,
217    0x04, 0x00, 0x01, 0x02, 0x77,
218
219    // Vi
220    0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41,
221    0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
222    0xA1, 0xB1, 0xC1, 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1,
223    0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26,
224    0x27, 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
225    0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
226    0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74,
227    0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
228    0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
229    0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
230    0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
231    0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
232    0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4,
233    0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA
234};
235
236/*****************************************************************************/
237/*  \brief   gLumaDCCode                                                     */
238/*                                                                           */
239/*  Contains the code words to encode the luminance DC coefficient           */
240/*  differences. The code words are mentioned in table K.3 of                */
241/*  ISO/IEC 10918-1:1994(E)                                                  */
242/*****************************************************************************/
243const IMG_UINT16 gLumaDCCode[] = {
244    0x0000, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006,
245    0x000E, 0x001E, 0x003E, 0x007E, 0x00FE, 0x01FE
246};
247
248/*****************************************************************************/
249/*  \brief   gLumaDCSize                                                     */
250/*                                                                           */
251/*  Contains the code length of the code words that are used to encode the   */
252/*  luminance DC coefficient differences. The code lengths are mentioned in  */
253/*  table K.3 of ISO/IEC 10918-1:1994(E)                                     */
254/*****************************************************************************/
255const IMG_UINT8 gLumaDCSize[] = {
256    0x0002, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
257    0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009
258};
259
260/*****************************************************************************/
261/*  \brief   gChromaDCCode                                                   */
262/*                                                                           */
263/*  Contains the code words to encode the chrominance DC coefficient         */
264/*  differences. The code words are mentioned in table K.4 of                */
265/*  ISO/IEC 10918-1:1994(E)                                                  */
266/*****************************************************************************/
267const IMG_UINT16 gChromaDCCode[] = {
268    0x0000, 0x0001, 0x0002, 0x0006, 0x000E, 0x001E,
269    0x003E, 0x007E, 0x00FE, 0x01FE, 0x03FE, 0x07FE
270};
271
272/*****************************************************************************/
273/*  \brief   gChromaDCSize                                                   */
274/*                                                                           */
275/*  Contains the code length of the code words that are used to encode the   */
276/*  chrominance DC coefficient differences. The code lengths are mentioned in*/
277/*  table K.4 of ISO/IEC 10918-1:1994(E)                                     */
278/*****************************************************************************/
279const IMG_UINT8 gChromaDCSize[] = {
280    0x0002, 0x0002, 0x0002, 0x0003, 0x0004, 0x0005,
281    0x0006, 0x0007, 0x0008, 0x0009, 0x000A, 0x000B
282};
283
284
285/*****************************************************************************/
286/*  \brief   gLumaACCode                                                     */
287/*                                                                           */
288/*  Contains the code words to encode the luminance AC coefficients. The     */
289/*  code words are arrange in the increasing order of run followed by size as*/
290/*  in table K.5 of ISO/IEC 10918-1:1994(E)                                  */
291/*****************************************************************************/
292const IMG_UINT16 gLumaACCode[] = {
293    0x000A, 0x0000, 0x0001, 0x0004, 0x000B, 0x001A, 0x0078, 0x00F8, 0x03F6,
294    0xFF82, 0xFF83, /* codes for run 0 */
295    0x000C, 0x001B, 0x0079, 0x01F6, 0x07F6, 0xFF84, 0xFF85, 0xFF86,
296    0xFF87, 0xFF88, /* codes for run 1 */
297    0x001C, 0x00F9, 0x03F7, 0x0FF4, 0xFF89, 0xFF8A, 0xFF8b, 0xFF8C,
298    0xFF8D, 0xFF8E, /* codes for run 2 */
299    0x003A, 0x01F7, 0x0FF5, 0xFF8F, 0xFF90, 0xFF91, 0xFF92, 0xFF93,
300    0xFF94, 0xFF95, /* codes for run 3 */
301    0x003B, 0x03F8, 0xFF96, 0xFF97, 0xFF98, 0xFF99, 0xFF9A, 0xFF9B,
302    0xFF9C, 0xFF9D, /* codes for run 4 */
303    0x007A, 0x07F7, 0xFF9E, 0xFF9F, 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3,
304    0xFFA4, 0xFFA5, /* codes for run 5 */
305    0x007B, 0x0FF6, 0xFFA6, 0xFFA7, 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB,
306    0xFFAC, 0xFFAD, /* codes for run 6 */
307    0x00FA, 0x0FF7, 0xFFAE, 0xFFAF, 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3,
308    0xFFB4, 0xFFB5, /* codes for run 7 */
309    0x01F8, 0x7FC0, 0xFFB6, 0xFFB7, 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB,
310    0xFFBC, 0xFFBD, /* codes for run 8 */
311    0x01F9, 0xFFBE, 0xFFBF, 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4,
312    0xFFC5, 0xFFC6, /* codes for run 9 */
313    0x01FA, 0xFFC7, 0xFFC8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD,
314    0xFFCE, 0xFFCF, /* codes for run A */
315    0x03F9, 0xFFD0, 0xFFD1, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6,
316    0xFFD7, 0xFFD8, /* codes for run B */
317    0x03FA, 0xFFD9, 0xFFDA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF,
318    0xFFE0, 0xFFE1, /* codes for run C */
319    0x07F8, 0xFFE2, 0xFFE3, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8,
320    0xFFE9, 0xFFEA, /* codes for run D */
321    0xFFEB, 0xFFEC, 0xFFED, 0xFFEE, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFF2,
322    0xFFF3, 0xFFF4, /* codes for run E */
323    0xFFF5, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD,
324    0xFFFE, 0x07F9  /* codes for run F */
325};
326
327/*****************************************************************************/
328/*  \brief   gLumaACSize                                                     */
329/*                                                                           */
330/*  Contains the code length of the code words that are used to encode the   */
331/*  luminance AC coefficients. The code lengths as in table K.5 of           */
332/*  ISO/IEC 10918-1:1994(E), are arranged in the increasing order of run     */
333/*  followed by size                                                         */
334/*****************************************************************************/
335const IMG_UINT8 gLumaACSize[] = {
336    0x04, 0x02, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x0A, 0x10, 0x10,/* run 0 */
337    0x04, 0x05, 0x07, 0x09, 0x0B, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 1 */
338    0x05, 0x08, 0x0A, 0x0C, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 2 */
339    0x06, 0x09, 0x0C, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 3 */
340    0x06, 0x0A, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 4 */
341    0x07, 0x0B, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 5 */
342    0x07, 0x0C, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 6 */
343    0x08, 0x0C, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 7 */
344    0x09, 0x0F, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 8 */
345    0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 9 */
346    0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run A */
347    0x0A, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run B */
348    0x0A, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run C */
349    0x0B, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run D */
350    0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run E */
351    0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x0B /* run F */
352};
353
354/*****************************************************************************/
355/*  \brief   gChromaACCode                                                   */
356/*                                                                           */
357/*  Contains the code words to encode the chromiannce AC coefficients. The   */
358/*  code words are arrange in the increasing order of run followed by size   */
359/*  as in table K.6 of ISO/IEC 10918-1:1994(E)                               */
360/*****************************************************************************/
361const IMG_UINT16 gChromaACCode[] = {
362    0x0000, 0x0001, 0x0004, 0x000A, 0x0018, 0x0019, 0x0038, 0x0078, 0x01F4,
363    0x03F6, 0x0FF4, /* codes for run 0 */
364    0x000B, 0x0039, 0x00F6, 0x01F5, 0x07F6, 0x0FF5, 0xFF88, 0xFF89,
365    0xFF8A, 0xFF8B, /* codes for run 1 */
366    0x001A, 0x00F7, 0x03F7, 0x0FF6, 0x7FC2, 0xFF8C, 0xFF8D, 0xFF8E,
367    0xFF8F, 0xFF90, /* codes for run 2 */
368    0x001B, 0x00F8, 0x03F8, 0x0FF7, 0xFF91, 0xFF92, 0xFF93, 0xFF94,
369    0xFF95, 0xFF96, /* codes for run 3 */
370    0x003A, 0x01F6, 0xFF97, 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C,
371    0xFF9D, 0xFF9E, /* codes for run 4 */
372    0x003B, 0x03F9, 0xFF9F, 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3, 0xFFA4,
373    0xFFA5, 0xFFA6, /* codes for run 5 */
374    0x0079, 0x07F7, 0xFFA7, 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB, 0xFFAC,
375    0xFFAD, 0xFFAE, /* codes for run 6 */
376    0x007A, 0x07F8, 0xFFAF, 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4,
377    0xFFB5, 0xFFB6, /* codes for run 7 */
378    0x00F9, 0xFFB7, 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB, 0xFFBC, 0xFFBD,
379    0xFFBE, 0xFFBF, /* codes for run 8 */
380    0x01F7, 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6,
381    0xFFC7, 0xFFC8, /* codes for run 9 */
382    0x01F8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF,
383    0xFFD0, 0xFFD1, /* codes for run A */
384    0x01F9, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7, 0xFFD8,
385    0xFFD9, 0xFFDA, /* codes for run B */
386    0x01FA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF, 0xFFE0, 0xFFE1,
387    0xFFE2, 0xFFE3, /* codes for run C */
388    0x07F9, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8, 0xFFE9, 0xFFEA,
389    0xFFEb, 0xFFEC, /* codes for run D */
390    0x3FE0, 0xFFED, 0xFFEE, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFF2, 0xFFF3,
391    0xFFF4, 0xFFF5, /* codes for run E */
392    0x7FC3, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD,
393    0xFFFE, 0x03FA  /* codes for run F */
394};
395
396/*****************************************************************************/
397/*  \brief   gChromaACSize                                                   */
398/*                                                                           */
399/*  Contains the code length of the code words that are used to encode the   */
400/*  chrominance AC coefficients. The code lengths as in table K.5 of         */
401/*  ISO/IEC 10918-1:1994(E), are arranged in the increasing order of run     */
402/*  followed by size                                                         */
403/*****************************************************************************/
404const IMG_UINT8 gChromaACSize[] = {
405    0x02, 0x02, 0x03, 0x04, 0x05, 0x05, 0x06, 0x07, 0x09, 0x0A, 0x0C,/* run 0 */
406    0x04, 0x06, 0x08, 0x09, 0x0B, 0x0C, 0x10, 0x10, 0x10, 0x10,/* run 1 */
407    0x05, 0x08, 0x0A, 0x0C, 0x0F, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 2 */
408    0x05, 0x08, 0x0A, 0x0C, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 3 */
409    0x06, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 4 */
410    0x06, 0x0A, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 5 */
411    0x07, 0x0B, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 6 */
412    0x07, 0x0B, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 7 */
413    0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 8 */
414    0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run 9 */
415    0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run A */
416    0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run B */
417    0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run C */
418    0x0B, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run D */
419    0x0E, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,/* run E */
420    0x0F, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x0A /* run F */
421};
422
423/*****************************************************************************/
424/*  \brief   gSize                                                           */
425/*                                                                           */
426/*  Table used in the computation of 4 lease significant bits 'SSSS' as      */
427/*  specified in table F.1 and F.2 of ISO/IEC 10918-1:1994(E). These values  */
428/*  are used to computes the 4 least significant bits and are not exactly    */
429/*  the values mentioned in the table                                        */
430/*****************************************************************************/
431const IMG_UINT8 gSize[] = {
432    0,
433    1,
434    2, 2,
435    3, 3, 3, 3,
436    4, 4, 4, 4, 4, 4, 4, 4,
437    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5
438};
439
440
441
442/*****************************************************************************/
443/*                                                                           */
444/* Function Name : fPutBitsToBuffer                                          */
445/*                                                                           */
446/* Description   : The function write a bit string into the stream           */
447/*                                                                           */
448/* Inputs        :                                                           */
449/* BitStream     : Pointer to the stream context                             */
450/* NoOfBits      : Size of the bit string                                    */
451/* ActualBits    : Bit string to be written                                  */
452/*                                                                           */
453/* Outputs       : On return the function has written the bit string into    */
454/*                 the stream                                                */
455/*                                                                           */
456/* Returns       : void                                                      */
457/*                                                                           */
458/* Revision History:                                                         */
459/*                                                                           */
460/* 14 12 2001 BG Creation                                                    */
461/*                                                                           */
462/*****************************************************************************/
463
464void fPutBitsToBuffer(STREAMTYPEW *BitStream, IMG_UINT8 NoOfBytes, IMG_UINT32 ActualBits)
465{
466    IMG_UINT8 ui8Lp;
467    IMG_UINT8 *pui8S;
468
469    pui8S = (IMG_UINT8 *)BitStream->Buffer;
470    pui8S += BitStream->Offset;
471
472    for (ui8Lp = NoOfBytes; ui8Lp > 0; ui8Lp--)
473        *(pui8S++) = ((IMG_UINT8 *) & ActualBits)[ui8Lp-1];
474
475    BitStream->Offset += NoOfBytes;
476}
477
478
479
480/***********************************************************************************
481 * Function Name      : AllocateCodedDataBuffers
482 * Inputs             :
483 * Outputs            :
484 * Returns            : PVRRC
485 * Description        : Allocates Device memory for coded buffers and build BUFFERINFO array
486 ************************************************************************************/
487
488IMG_ERRORCODE AllocateCodedDataBuffers(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext)
489{
490    IMG_UINT8 ui8Loop;
491
492    for (ui8Loop = 0 ; ui8Loop < pContext->sScan_Encode_Info.ui8NumberOfCodedBuffers; ui8Loop ++)
493        if (pContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo == NULL) {
494            pContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferSizeBytes = ((DATA_BUFFER_SIZE(pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan) + sizeof(BUFFER_HEADER)) + 3) & ~3;
495            pContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui32DataBufferUsedBytes = 0;
496            pContext->sScan_Encode_Info.aBufferTable[ui8Loop].i8MTXNumber = 0; // Indicates buffer is idle
497            pContext->sScan_Encode_Info.aBufferTable[ui8Loop].ui16ScanNumber = 0; // Indicates buffer is idle
498            pContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo =
499                (unsigned char *)pContext->jpeg_coded_buf.pMemInfo + PNW_JPEG_HEADER_MAX_SIZE + ui8Loop * pContext->ui32SizePerCodedBuffer;
500
501        }
502
503    return IMG_ERR_OK;
504}
505
506
507
508/***********************************************************************************
509 * Function Name      : FreeCodedDataBuffers
510 * Inputs             :
511 * Outputs            :
512 * Returns            : PVRRC
513 * Description        : Loops through our coded data buffers and frees them
514 ************************************************************************************/
515
516IMG_UINT32 FreeCodedDataBuffers(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext)
517{
518    IMG_UINT8 ui8Loop;
519    ui8Loop = pContext->sScan_Encode_Info.ui8NumberOfCodedBuffers;
520    /* Spin through and remove */
521    while (ui8Loop--)
522        if (pContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo != NULL) {
523            /*      MMFreeDeviceMemory( &(pContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo));*/
524            pContext->sScan_Encode_Info.aBufferTable[ui8Loop].pMemInfo = NULL;
525        }
526
527    return 0;
528}
529
530
531
532
533void SetupMCUDetails(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext,
534                     const IMG_UINT32 uiComponentNumber ,
535                     const IMG_UINT32 uiWidthBlocks ,
536                     const IMG_UINT32 uiHeightBlocks,
537                     const IMG_UINT32 uiLastRow,
538                     const IMG_UINT32 uiLastCol)
539{
540    MCUCOMPONENT* pMCUComp;
541
542    if (uiComponentNumber >=  MTX_MAX_COMPONENTS)
543        return;
544
545    pMCUComp = &pContext->pMTXSetup->MCUComponent[uiComponentNumber];
546
547    pMCUComp->ui32WidthBlocks = uiWidthBlocks * 8;
548    pMCUComp->ui32HeightBlocks = uiHeightBlocks * 8;
549
550    pMCUComp->ui32XLimit = uiLastCol;
551    pMCUComp->ui32YLimit = uiLastRow;
552
553    drv_debug_msg(VIDEO_DEBUG_GENERAL, "MCU Details %i : %ix%i  %i  %i\n", uiComponentNumber, uiWidthBlocks , uiHeightBlocks ,  uiLastCol , uiLastRow);
554
555}
556
557
558/*****************************************************************************/
559/*                                                                           */
560/* Function Name : InitializeJpegEncode                                      */
561/*                                                                           */
562/* Description   : The function initializes an instance of the JPEG encoder  */
563/* Inputs/Outputs: Pointer to the JPEG Host context                                                      */
564/*                 Pointer to a IMG_FRAME type which will be created to be filled        */
565/*                      with the source image data                                                                               */
566/*****************************************************************************/
567
568IMG_ERRORCODE InitializeJpegEncode(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext, object_surface_p pTFrame)
569{
570    IMG_ERRORCODE rc = IMG_ERR_OK;
571    IMG_UINT8  uc_i = 0;
572    IMG_UINT16 ui16_height_min;
573    IMG_UINT16 ui16_height_max;
574    IMG_UINT16 ui16_width_min;
575    IMG_UINT16 ui16_width_max;
576    //IMG_UINT16 ui16_width;
577    //IMG_UINT16 ui16_height;
578    IMG_UINT32 uiBlockCount = 0;
579    IMG_UINT16 ui16_comp_width, ui16_comp_height;
580    IMG_UINT8  uc_h_scale_max = 0, uc_v_scale_max = 0, uc_h_scale = 0;
581    IMG_UINT8  uc_v_scale;
582    IMG_UINT16 ui16_height;
583    context_ENC_p ctx = (context_ENC_p)pContext->ctx;
584
585    /*************************************************************************/
586    /* Determine the horizontal and the vertical scaling factor of each of   */
587    /* the components of the image                                           */
588    /*************************************************************************/
589
590    /*FIXME: ONLY support NV12, YV12 here*/
591    /*pTFrame->height isn't the real height of image, since vaCreateSurface
592     * makes it aligned with 32*/
593    ui16_height = ctx->Height;
594    switch (pContext->eFormat) {
595    case IMG_CODEC_YV16: /*422 format*/
596        ui16_height_min = ui16_height;
597        ui16_height_max = ui16_height;
598        ui16_width_min = pTFrame->width >> 1;
599        ui16_width_max = pTFrame->width;
600        break;
601    case IMG_CODEC_NV12:
602    default:
603        ui16_height_min = ui16_height >> 1;
604        ui16_height_max = ui16_height;
605        ui16_width_min = pTFrame->width >> 1;
606        ui16_width_max = pTFrame->width;
607        break;
608    }
609    /*ui16_height_min = ui16_width_min  = 65535;
610    ui16_height_max = ui16_width_max  = 0;
611
612    for(uc_i = 0; uc_i < pContext->pMTXSetup->ui32ComponentsInScan; uc_i++)
613    {
614    ui16_width  = pTFrame->Width;
615    if(ui16_width_min > ui16_width)
616    ui16_width_min  = ui16_width;
617    if(ui16_width_max < ui16_width)
618    ui16_width_max  = ui16_width;
619
620    ui16_height = pTFrame->Height;
621    if(ui16_height_min > ui16_height)
622    ui16_height_min = ui16_height;
623    if(ui16_height_max < ui16_height)
624    ui16_height_max = ui16_height;
625    }
626    */
627    /*********************************************************************/
628    /* Determine the horizonal and the vertical sampling frequency of    */
629    /* each of components in the image                                   */
630    /*********************************************************************/
631
632    uc_h_scale_max = (ui16_width_max + ui16_width_min - 1) /
633                     ui16_width_min;
634    uc_v_scale_max = (ui16_height_max + ui16_height_min - 1) /
635                     ui16_height_min;
636
637    for (uc_i = 0; uc_i < pContext->pMTXSetup->ui32ComponentsInScan; uc_i++) {
638
639        /*      ui16_comp_width  = pTFrame->aui32ComponentInfo[uc_i].ui32Width;
640                ui16_comp_height = pTFrame->aui32ComponentInfo[uc_i].ui32Height;*/
641        /*Support NV12, YV12, YV16 here. uc_h/v_scale should be
642         * 2x2(Y) or 1x1(U/V)*/
643        if (0 == uc_i) {
644            ui16_comp_width  = pTFrame->width;
645            ui16_comp_height = ui16_height;
646        } else {
647            switch (pContext->eFormat) {
648            case IMG_CODEC_YV16: /*422 format*/
649                ui16_comp_width  = pTFrame->width >> 1;
650                ui16_comp_height = ui16_height;
651                break;
652            case IMG_CODEC_NV12:
653            default:
654                ui16_comp_width  = pTFrame->width >> 1;
655                ui16_comp_height = ui16_height >> 1;
656            }
657        }
658
659        uc_h_scale       = (ui16_comp_width *  uc_h_scale_max) /
660                           ui16_width_max;
661        uc_v_scale       = (ui16_comp_height * uc_v_scale_max) /
662                           ui16_height_max;
663
664        uiBlockCount += (uc_h_scale * uc_v_scale);
665
666        switch (ISCHROMAINTERLEAVED(pContext->eFormat)) {
667        case C_INTERLEAVE:
668            // Chroma format is byte interleaved, as the engine runs using planar colour surfaces we need
669            // to fool the engine into offsetting by 16 instead of 8
670            if (uc_i > 0) { // if chroma, then double values
671                uc_h_scale <<= 1;
672                ui16_comp_width <<= 1;
673            }
674            break;
675        case LC_UVINTERLEAVE:
676            //Y0UY1V_8888 or Y0VY1U_8888 format
677            ui16_comp_width <<= 1;
678            break;
679        case LC_VUINTERLEAVE:
680            //Y0UY1V_8888 or Y0VY1U_8888 format
681            ui16_comp_width <<= 1;
682            break;
683        default:
684            break;
685        }
686
687        SetupMCUDetails(pContext, uc_i , uc_h_scale, uc_v_scale, ui16_comp_height, ui16_comp_width);
688
689        if (uiBlockCount > BLOCKCOUNTMAXPERCOLOURPLANE) {
690            return IMG_ERR_INVALID_SIZE;
691        }
692
693    }
694
695    return rc;
696}
697
698
699/***********************************************************************************
700  Function Name      : JPGEncodeBegin
701Inputs             : hInstance,psJpegInfo,ui32Quality
702Outputs            :
703Returns            : PVRRC
704Description        : Marks the begining of a JPEG encode and sets up encoder
705 ************************************************************************************/
706
707/*IMG_ERRORCODE JPGEncodeBegin(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext, IMG_FRAME *pTFrame)
708{
709    IMG_UINT32 ReturnCode = 0;
710    ReturnCode = InitializeJpegEncode(pContext, pTFrame);
711
712    return ReturnCode;
713} */
714/*****************************************************************************/
715/*                                                                           */
716/* Function Name : EncodeMarkerSegment                                       */
717/*                                                                           */
718/* Description   : Writes the marker segment of a JPEG stream according to   */
719/*                 the syntax mentioned in section B.2.4 of                  */
720/*                 ISO/IEC 10918-1:1994E                                     */
721/*                                                                           */
722/* Inputs        : Pointer to the JPEG Context and coded output buffer       */
723/*                                                                           */
724/* Processing    : Writes each of the following into the marker segment of   */
725/*                 the JPEG stream                                           */
726/*                 - luminance and chrominance quantization tables           */
727/*                 - huffman tables used for encoding the luminance and      */
728/*                   chrominance AC coefficients                             */
729/*                 - huffman tables used for encoding the luminance and      */
730/*                   chorimance  DC coefficient differences                  */
731/*                                                                           */
732/*                                                                           */
733/*****************************************************************************/
734
735IMG_UINT32 Legacy_EncodeMarkerSegment(LEGACY_JPEG_ENCODER_CONTEXT *pContext,
736                                      IMG_UINT8 *puc_stream_buff)
737{
738    STREAMTYPEW s_streamW;
739    IMG_UINT8 uc_i;
740
741    s_streamW.Offset = 0;
742    s_streamW.Buffer = puc_stream_buff;
743
744    /* Writing the start of image marker */
745    fPutBitsToBuffer(&s_streamW, 2, START_OF_IMAGE);
746
747    /* Writing the quantization table for luminance into the stream */
748    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
749
750    fPutBitsToBuffer(&s_streamW, 3, LQPQ << 4); // 20 bits = LQPQ, 4 bits = 0 (Destination identifier for the luminance quantizer tables)
751
752    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
753        fPutBitsToBuffer(&s_streamW, 1, pContext->pvLowLevelEncContext->Qmatrix[0][gZigZag[uc_i]]);
754    }
755
756    /* Writing the quantization table for chrominance into the stream */
757    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
758
759    fPutBitsToBuffer(&s_streamW, 3, (LQPQ << 4) | 1); // 20 bits = LQPQ, 4 bits = 1 (Destination identifier for the chrominance quantizer tables)
760
761    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
762        fPutBitsToBuffer(&s_streamW, 1, pContext->pvLowLevelEncContext->Qmatrix[1][gZigZag[uc_i]]);
763    }
764
765    /* Writing the huffman tables for luminance dc coeffs */
766    /* Write the DHT Marker */
767    fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
768    fPutBitsToBuffer(&s_streamW, 2, LH_DC);
769    for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
770        fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaDc[uc_i]);
771    }
772    /* Writing the huffman tables for luminance ac coeffs */
773    /* Write the DHT Marker */
774    fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
775    fPutBitsToBuffer(&s_streamW, 2, LH_AC);
776    for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
777        fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaAc[uc_i]);
778    }
779    /* Writing the huffman tables for chrominance dc coeffs */
780    fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
781    fPutBitsToBuffer(&s_streamW, 2, LH_DC);
782    for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
783        fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaDc[uc_i]);
784    }
785    /* Writing the huffman tables for luminance ac coeffs */
786    /* Write the DHT Marker */
787    fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
788    fPutBitsToBuffer(&s_streamW, 2, LH_AC);
789    for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
790        fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaAc[uc_i]);
791    }
792
793
794    return s_streamW.Offset;
795}
796
797/*****************************************************************************/
798/*                                                                           */
799/* Function Name : EncodeMarkerSegment                                       */
800/*                                                                           */
801/* Description   : Writes the marker segment of a JPEG stream according to   */
802/*                 the syntax mentioned in section B.2.4 of                  */
803/*                 ISO/IEC 10918-1:1994E                                     */
804/*                                                                           */
805/* Inputs        : Pointer to the JPEG Context and coded output buffer       */
806/*                                                                           */
807/* Processing    : Writes each of the following into the marker segment of   */
808/*                 the JPEG stream                                           */
809/*                 - luminance and chrominance quantization tables           */
810/*                 - huffman tables used for encoding the luminance and      */
811/*                   chrominance AC coefficients                             */
812/*                 - huffman tables used for encoding the luminance and      */
813/*                   chorimance  DC coefficient differences                  */
814/*                                                                           */
815/*****************************************************************************/
816
817IMG_UINT32 EncodeMarkerSegment(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext,
818                               IMG_UINT8 *puc_stream_buff, IMG_BOOL bIncludeHuffmanTables)
819{
820    STREAMTYPEW s_streamW;
821    IMG_UINT8 uc_i;
822
823    s_streamW.Offset = 0;
824    s_streamW.Buffer = puc_stream_buff;
825
826    /* Writing the start of image marker */
827    fPutBitsToBuffer(&s_streamW, 2, START_OF_IMAGE);
828
829    /* Writing the quantization table for luminance into the stream */
830    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
831
832    fPutBitsToBuffer(&s_streamW, 3, LQPQ << 4); // 20 bits = LQPQ, 4 bits = 0 (Destination identifier for the luminance quantizer tables)
833
834    IMG_ASSERT(PELS_IN_BLOCK <= QUANT_TABLE_SIZE_BYTES);
835    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
836        // Write zigzag ordered luma quantization values to our JPEG header
837        fPutBitsToBuffer(&s_streamW, 1, pContext->psTablesBlock->aui8LumaQuantParams[gZigZag[uc_i]]);
838    }
839
840    /* Writing the quantization table for chrominance into the stream */
841    fPutBitsToBuffer(&s_streamW, 2, DQT_MARKER);
842
843    fPutBitsToBuffer(&s_streamW, 3, (LQPQ << 4) | 1); // 20 bits = LQPQ, 4 bits = 1 (Destination identifier for the chrominance quantizer tables)
844
845    for (uc_i = 0; uc_i < PELS_IN_BLOCK; uc_i++) {
846        // Write zigzag ordered chroma quantization values to our JPEG header
847        fPutBitsToBuffer(&s_streamW, 1, pContext->psTablesBlock->aui8ChromaQuantParams[gZigZag[uc_i]]);
848    }
849
850    if (bIncludeHuffmanTables) {
851        /* Writing the huffman tables for luminance dc coeffs */
852        /* Write the DHT Marker */
853        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
854        fPutBitsToBuffer(&s_streamW, 2, LH_DC);
855        for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
856            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaDc[uc_i]);
857        }
858        /* Writing the huffman tables for luminance ac coeffs */
859        /* Write the DHT Marker */
860        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
861        fPutBitsToBuffer(&s_streamW, 2, LH_AC);
862        for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
863            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataLumaAc[uc_i]);
864        }
865        /* Writing the huffman tables for chrominance dc coeffs */
866        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
867        fPutBitsToBuffer(&s_streamW, 2, LH_DC);
868        for (uc_i = 0; uc_i < LH_DC - 2; uc_i++) {
869            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaDc[uc_i]);
870        }
871        /* Writing the huffman tables for luminance ac coeffs */
872        /* Write the DHT Marker */
873        fPutBitsToBuffer(&s_streamW, 2, DHT_MARKER);
874        fPutBitsToBuffer(&s_streamW, 2, LH_AC);
875        for (uc_i = 0; uc_i < LH_AC - 2; uc_i++) {
876            fPutBitsToBuffer(&s_streamW, 1, gMarkerDataChromaAc[uc_i]);
877        }
878    }
879
880    // Activate Restart markers
881    if (pContext->sScan_Encode_Info.ui16CScan > 1) {
882        // Only use restart intervals if we need them (ie. multiple Scan encode and/or parallel CB encode)
883        fPutBitsToBuffer(&s_streamW, 2, 0xFFDD); //Marker header
884        fPutBitsToBuffer(&s_streamW, 2, 4); // Byte size of marker (header not included)
885        fPutBitsToBuffer(&s_streamW, 2, pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan); // Restart Interval (same as MCUs per buffer)
886    }
887
888    return s_streamW.Offset;
889}
890
891/***********************************************************************************
892  Function Name      : JPGEncodeMarker
893Inputs             :    Pointer to JPEG Encoder context
894Pointer to coded buffer stream
895Pointer to pui32BytesWritten value
896Outputs            : ui8BitStreamBuffer,pui32BytesWritten
897Description        : Writes a JPEG marker segment to the bit stream
898 ************************************************************************************/
899
900IMG_UINT32 Legacy_JPGEncodeMarker(/*in */               LEGACY_JPEG_ENCODER_CONTEXT *pContext ,
901        /*in */         IMG_UINT8* pui8BitStreamBuffer ,
902        /*out*/         IMG_UINT32 *pui32BytesWritten)
903{
904#ifdef JPEG_VERBOSE
905    printf("PVRJPGEncodeMarker");
906#endif
907
908    if (pContext->eCurrentActive != LEGACY_JPEG_API_CURRENT_ACTIVE_ENCODE) {
909        /* Cannot be called outside begin and end*/
910        return 2;
911    }
912
913    *pui32BytesWritten += Legacy_EncodeMarkerSegment(pContext, pui8BitStreamBuffer);
914
915    return 0;
916}
917
918/***********************************************************************************
919  Function Name      : JPGEncodeMarker
920Inputs             :    Pointer to JPEG Encoder context
921Pointer to coded buffer stream
922Pointer to Byteswritten value
923bIncludeHuffmanTables - Set to true to include the huffman tables in the stream
924Outputs            : ui8BitStreamBuffer,pui32BytesWritten
925Description        : Writes a JPEG marker segment to the bit stream
926 ************************************************************************************/
927
928IMG_UINT32 JPGEncodeMarker(/*in */              TOPAZSC_JPEG_ENCODER_CONTEXT *pContext ,
929        /*in */         IMG_UINT8* pui8BitStreamBuffer ,
930        /*out*/         IMG_UINT32 *pui32BytesWritten, IMG_BOOL bIncludeHuffmanTables)
931{
932#ifdef JPEG_VERBOSE
933    printf("PVRJPGEncodeMarker");
934#endif
935
936
937    *pui32BytesWritten += EncodeMarkerSegment(pContext, pui8BitStreamBuffer + *pui32BytesWritten, bIncludeHuffmanTables);
938
939    return 0;
940}
941
942
943/*****************************************************************************/
944/*                                                                           */
945/* Function Name : EncodeFrameHeader                                         */
946/*                                                                           */
947/* Description   : Writes the frame header of a JPEG stream according to     */
948/*                 the syntax mentioned in section B.2.2 of                  */
949/*                 ISO/IEC 10918-1:1994E .                                   */
950/*                                                                           */
951/* Inputs        :                                                           */
952/* ps_jpeg_params: Ptr to the JPEG encoder parameter context                 */
953/* ps_jpeg_comp  : Ptr to the JPEG image component context                   */
954/* puc_stream_buff:Ptr to the bistream buffer from where to start writing    */
955/*                                                                           */
956/* Processing    : Writes each of the following into the frame header of     */
957/*                 the JPEG stream                                           */
958/*                 - JPEG image component identifier                         */
959/*                 - Horizontal and vertical sampling factor                 */
960/*                 - Quantization table identifier for each of the components*/
961/*                                                                           */
962/* Returns       : Number of bytes written into the stream                   */
963/*                                                                           */
964/*****************************************************************************/
965
966IMG_UINT32 Legacy_EncodeFrameHeader(LEGACY_JPEGENC_ITTIAM_PARAMS *ps_jpeg_params,
967                                    LEGACY_JPEGENC_ITTIAM_COMPONENT *ps_jpeg_comp,
968                                    IMG_UINT8 *puc_stream_buff)
969{
970    STREAMTYPEW ps_streamW;
971    IMG_UINT16 ui16_i;
972    IMG_UINT8  uc_num_comp_in_img;
973
974    uc_num_comp_in_img = ps_jpeg_comp->uc_num_comp_in_img;
975
976    ps_streamW.Offset = 0;
977    ps_streamW.Buffer = puc_stream_buff;
978
979
980    if (ps_jpeg_params->uc_isAbbreviated != 0)
981        fPutBitsToBuffer(&ps_streamW, 2, START_OF_IMAGE);
982
983    /* Writing the frame header */
984    fPutBitsToBuffer(&ps_streamW, 2, SOF_BASELINE_DCT);
985    /* Frame header length */
986    fPutBitsToBuffer(&ps_streamW, 2, 8 + 3 * uc_num_comp_in_img);
987    /* Precision */
988    fPutBitsToBuffer(&ps_streamW, 1, 8);
989    /* Height : sample lines */
990    fPutBitsToBuffer(&ps_streamW, 2, ps_jpeg_params->ui16_height);
991    /* Width : samples per line */
992    fPutBitsToBuffer(&ps_streamW, 2, ps_jpeg_params->ui16_width);
993    /* Number of image components */
994    fPutBitsToBuffer(&ps_streamW, 1, uc_num_comp_in_img);
995
996    if (uc_num_comp_in_img > MAX_COMP_IN_SCAN)
997        uc_num_comp_in_img = MAX_COMP_IN_SCAN;
998    for (ui16_i = 0; ui16_i < uc_num_comp_in_img; ui16_i++) {
999        /* Component identifier */
1000        fPutBitsToBuffer(&ps_streamW, 1, ps_jpeg_comp->puc_comp_id[ui16_i]);
1001
1002        /* 4 bit Horizontal and 4 bit vertical sampling factors */
1003        fPutBitsToBuffer(&ps_streamW, 1, (ps_jpeg_comp->puc_horiz_scale[ui16_i] << 4) | ps_jpeg_comp->puc_vert_scale[ui16_i]);
1004
1005        /* Quantization table destination selector */
1006        fPutBitsToBuffer(&ps_streamW, 1, ps_jpeg_comp->puc_q_table_id[ui16_i]);
1007
1008        ps_jpeg_comp->CompIdtoIndex[ ps_jpeg_comp->puc_comp_id[ui16_i] ] = (IMG_UINT8) ui16_i;
1009    }
1010
1011    //Use if you want start of scan (image data) to align to 32
1012    //fPutBitsToBuffer(&ps_streamW, 1, 0xFF);
1013
1014    return ps_streamW.Offset;
1015}
1016
1017
1018/*****************************************************************************/
1019/*                                                                           */
1020/* Function Name : EncodeFrameHeader                                         */
1021/*                                                                           */
1022/* Description   : Writes the frame header of a JPEG stream according to     */
1023/*                 the syntax mentioned in section B.2.2 of                  */
1024/*                 ISO/IEC 10918-1:1994E .                                   */
1025/*                                                                           */
1026/* Inputs        :                                                           */
1027/*                                      pContext                        - Ptr to the JPEG encoder context        */
1028/*                                      puc_stream_buff         - Ptr to the bistream buffer from        */
1029/*                                                                                      where to start writing                   */
1030/*                                                                           */
1031/* Processing    : Writes each of the following into the frame header of     */
1032/*                 the JPEG stream                                           */
1033/*                 - JPEG image component identifier                         */
1034/*                 - Horizontal and vertical sampling factor                 */
1035/*                 - Quantization table identifier for each of the components*/
1036/*                                                                           */
1037/* Returns       : Number of bytes written into the stream                   */
1038/*                                                                           */
1039/*****************************************************************************/
1040
1041IMG_UINT32 EncodeFrameHeader(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext,
1042                             IMG_UINT8 *puc_stream_buff)
1043{
1044    STREAMTYPEW ps_streamW;
1045    IMG_UINT8  uc_num_comp_in_img;
1046
1047    uc_num_comp_in_img = pContext->pMTXSetup->ui32ComponentsInScan;
1048
1049    ps_streamW.Offset = 0;
1050    ps_streamW.Buffer = puc_stream_buff;
1051
1052
1053    //if(ps_jpeg_params->uc_isAbbreviated != 0)
1054    //   fPutBitsToBuffer(&ps_streamW, 2, START_OF_IMAGE);
1055
1056    /* Writing the frame header */
1057    fPutBitsToBuffer(&ps_streamW, 2, SOF_BASELINE_DCT);
1058    /* Frame header length */
1059    fPutBitsToBuffer(&ps_streamW, 2, 8 + 3 * uc_num_comp_in_img);
1060    /* Precision */
1061    fPutBitsToBuffer(&ps_streamW, 1, 8);
1062    /* Height : sample lines */
1063    fPutBitsToBuffer(&ps_streamW, 2, pContext->ui32OutputHeight);
1064    /* Width : samples per line */
1065    fPutBitsToBuffer(&ps_streamW, 2, pContext->ui32OutputWidth);
1066    /* Number of image components */
1067    fPutBitsToBuffer(&ps_streamW, 1, uc_num_comp_in_img);
1068
1069    //Luma Details
1070    /* Component identifier */
1071    fPutBitsToBuffer(&ps_streamW, 1, 1); //CompId 0 = 1, 1 = 2, 2 = 3
1072    fPutBitsToBuffer(&ps_streamW, 1, ((pContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks >> 3) << 4) | (pContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks >> 3));
1073    fPutBitsToBuffer(&ps_streamW, 1, 0); // 0 = Luma(0), 1,2 = Chroma(1)
1074
1075    //Chroma Details
1076    if (pContext->pMTXSetup->ui32DataInterleaveStatus < C_INTERLEAVE) { //Chroma planar
1077        fPutBitsToBuffer(&ps_streamW, 1, 2); //CompId 0 = 1, 1 = 2, 2 = 3
1078        /* 4 bit Horizontal and 4 bit vertical sampling factors */
1079        fPutBitsToBuffer(&ps_streamW, 1, ((pContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 3) << 4) | (pContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
1080        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
1081        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
1082        /* 4 bit Horizontal and 4 bit vertical sampling factors */
1083        fPutBitsToBuffer(&ps_streamW, 1, ((pContext->pMTXSetup->MCUComponent[2].ui32WidthBlocks >> 3) << 4) | (pContext->pMTXSetup->MCUComponent[2].ui32HeightBlocks >> 3));
1084        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
1085    } else if (pContext->pMTXSetup->ui32DataInterleaveStatus == C_INTERLEAVE) { // Chroma Interleaved
1086        fPutBitsToBuffer(&ps_streamW, 1, 2); //CompId 0 = 1, 1 = 2, 2 = 3
1087        /* 4 bit Horizontal and 4 bit vertical sampling factors */
1088        fPutBitsToBuffer(&ps_streamW, 1, ((pContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 3) << 3) | (pContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
1089        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
1090        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
1091        /* 4 bit Horizontal and 4 bit vertical sampling factors */
1092        fPutBitsToBuffer(&ps_streamW, 1, ((pContext->pMTXSetup->MCUComponent[2].ui32WidthBlocks >> 3) << 3) | (pContext->pMTXSetup->MCUComponent[2].ui32HeightBlocks >> 3));
1093        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
1094    } else { //Chroma YUYV - Special case
1095        fPutBitsToBuffer(&ps_streamW, 1, 2); //CompId 0 = 1, 1 = 2, 2 = 3
1096        /* 4 bit Horizontal and 4 bit vertical sampling factors */
1097        fPutBitsToBuffer(&ps_streamW, 1, ((pContext->pMTXSetup->MCUComponent[1].ui32WidthBlocks >> 3) << 4) | (pContext->pMTXSetup->MCUComponent[1].ui32HeightBlocks >> 3));
1098        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
1099        fPutBitsToBuffer(&ps_streamW, 1, 3); //CompId 0 = 1, 1 = 2, 2 = 3
1100        /* 4 bit Horizontal and 4 bit vertical sampling factors */
1101        fPutBitsToBuffer(&ps_streamW, 1, ((pContext->pMTXSetup->MCUComponent[2].ui32WidthBlocks >> 3) << 4) | (pContext->pMTXSetup->MCUComponent[2].ui32HeightBlocks >> 3));
1102        fPutBitsToBuffer(&ps_streamW, 1, 1); // 0 = Luma(0), 1,2 = Chroma(1)
1103    }
1104
1105
1106    //Use if you want start of scan (image data) to align to 32
1107    //fPutBitsToBuffer(&ps_streamW, 1, 0xFF);
1108
1109    return ps_streamW.Offset;
1110}
1111
1112
1113/***********************************************************************************
1114  Function Name      : JPGEncodeHeader
1115Inputs             : Pointer to JPEG Context
1116Outputs            : ui8BitStreamBuffer,pui32BytesWritten
1117Returns            : PVRRC
1118Description        : Writes a frame header to the bit stream
1119Max written 21 bytes
1120 ************************************************************************************/
1121
1122IMG_UINT32 Legacy_JPGEncodeHeader(/*in */               LEGACY_JPEG_ENCODER_CONTEXT *pContext,
1123        /*out */        IMG_UINT8*              pui8BitStreamBuffer ,
1124        /*out*/         IMG_UINT32*             pui32BytesWritten)
1125{
1126#ifdef JPEG_VERBOSE
1127    printf("PVRJPGEncodeHeader");
1128#endif
1129
1130    if (pContext->eCurrentActive != LEGACY_JPEG_API_CURRENT_ACTIVE_ENCODE) {
1131        /* Cannot be called outside begin and end*/
1132        return 2;
1133    }
1134
1135    *pui32BytesWritten += Legacy_EncodeFrameHeader(&pContext->JPEGEncoderParams,
1136                          &pContext->sJPEGEncoderComp,
1137                          pui8BitStreamBuffer + *pui32BytesWritten);
1138    return 0;
1139}
1140
1141/***********************************************************************************
1142  Function Name      : JPGEncodeHeader
1143Inputs             : Pointer to JPEG context
1144Outputs            : ui8BitStreamBuffer,pui32BytesWritten
1145Description        : Writes a frame header to the bit stream
1146Max written 21 bytes
1147 ************************************************************************************/
1148
1149IMG_UINT32 JPGEncodeHeader(/*in */              TOPAZSC_JPEG_ENCODER_CONTEXT *pContext,
1150        /*out */        IMG_UINT8*              pui8BitStreamBuffer ,
1151        /*out*/         IMG_UINT32*             pui32BytesWritten)
1152{
1153#ifdef JPEG_VERBOSE
1154    printf("JPGEncodeHeader");
1155#endif
1156
1157    *pui32BytesWritten += EncodeFrameHeader(pContext, pui8BitStreamBuffer + *pui32BytesWritten);
1158
1159    return 0;
1160}
1161
1162
1163/***********************************************************************************
1164 * Function Name      : SetupIssueSetup
1165 * Inputs             :
1166 pContext - Pointer to JPEG context
1167 ui32ComponentsInScan - Number of components to be encoded in a scan
1168 aui8Planes - Array of 3 bytes indexing YUV colour planes
1169 pTFrame - Pointer to source data
1170 ui32TableA, ui32TableB - Quantization table index values
1171 * Outputs            :
1172 * Returns            : PVRRC
1173 * Description        : Issues the Setup structure to MTX
1174 ************************************************************************************/
1175
1176IMG_UINT32 SetupIssueSetup(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext, const IMG_UINT32 ui32ComponentsInScan, IMG_UINT8* aui8Planes,  object_surface_p pTFrame, const IMG_UINT32 ui32TableA , const IMG_UINT32 ui32TableB)
1177{
1178    IMG_UINT32 ReturnCode = 0;
1179    IMG_INT32 i32Lp;
1180    //COMPONENTPLANE* pSrcPlane;
1181    IMG_UINT32 srf_buf_offset;
1182    context_ENC_p ctx = (context_ENC_p)pContext->ctx;
1183    pnw_cmdbuf_p cmdbuf = ctx->obj_context->pnw_cmdbuf;
1184    /*TOPAZSC_JPEG_ENCODER_CONTEXT *pContext = pEncContext->psJpegHC;*/
1185
1186#ifdef JPEG_VERBOSE
1187    printf("\nSetupIssueSetup");
1188#endif
1189
1190    pContext->pMTXSetup->ui32ComponentsInScan = ui32ComponentsInScan;
1191    pContext->pMTXSetup->ui32DataInterleaveStatus = ISCHROMAINTERLEAVED(pContext->eFormat);
1192    pContext->pMTXSetup->ui32TableA = ui32TableA;
1193
1194    /*MMUpdateDeviceMemory( pContext->pMemInfoMTXSetup )*/;
1195    // we want to process the handles that have been placed into the buffer
1196    /*for(n=0;n<pContext->pMTXSetup->ui32ComponentsInScan;n++)
1197    {
1198    pSrcPlane = &pContext->pMTXSetup->ComponentPlane[n];
1199    TIMER_START(hardwareduration,"");
1200    MMDeviceMemWriteDeviceMemRef(pContext->pMemInfoMTXSetup->iu32MemoryRegionID, pContext->pMemInfoMTXSetup->hShadowMem, (IMG_UINT32)   ((IMG_BYTE*)&pSrcPlane->ui32PhysAddr - (IMG_BYTE*)pContext->pMTXSetup),TAL_NULL_MANGLER_ID,(IMG_HANDLE) pTFrame->psBuffer->pMemInfo->hShadowMem, pTFrame->aui32ComponentOffset[aui8Planes[n]]);
1201    TIMER_END("HW - MMDeviceMemWriteDeviceMemRef in SetupIssueSetup (hostjpeg.c)");
1202    }*/
1203    /*Support PL12/NV12, YV12/IYUV, YV16*/
1204    srf_buf_offset = pTFrame->psb_surface->buf.buffer_ofs;
1205    RELOC_PIC_PARAMS_PNW(&pContext->pMTXSetup->ComponentPlane[0].ui32PhysAddr, srf_buf_offset , &pTFrame->psb_surface->buf);
1206    switch (pContext->eFormat) {
1207    case IMG_CODEC_IYUV:
1208    case IMG_CODEC_PL8:
1209        RELOC_PIC_PARAMS_PNW(&pContext->pMTXSetup->ComponentPlane[1].ui32PhysAddr,
1210                             srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height,
1211                             &pTFrame->psb_surface->buf);
1212        RELOC_PIC_PARAMS_PNW(&pContext->pMTXSetup->ComponentPlane[2].ui32PhysAddr,
1213                             srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height
1214                             + (pTFrame->psb_surface->stride / 2) *(pTFrame->height / 2),
1215                             &pTFrame->psb_surface->buf);
1216        break;
1217    case IMG_CODEC_IMC2:
1218    case IMG_CODEC_PL12:
1219    case IMG_CODEC_NV12:
1220        RELOC_PIC_PARAMS_PNW(&pContext->pMTXSetup->ComponentPlane[1].ui32PhysAddr,
1221                             srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height,
1222                             &pTFrame->psb_surface->buf);
1223        //Byte interleaved surface, so need to force chroma to use single surface by fooling it into
1224        //thinking it's dealing with standard 8x8 planaerblocks
1225        RELOC_PIC_PARAMS_PNW(&pContext->pMTXSetup->ComponentPlane[2].ui32PhysAddr,
1226                             srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height
1227                             + 8,
1228                             &pTFrame->psb_surface->buf);
1229        break;
1230    case IMG_CODEC_YV16:
1231        /*V*/
1232        RELOC_PIC_PARAMS_PNW(&pContext->pMTXSetup->ComponentPlane[2].ui32PhysAddr,
1233                             srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height,
1234                             &pTFrame->psb_surface->buf);
1235        /*U*/
1236        RELOC_PIC_PARAMS_PNW(&pContext->pMTXSetup->ComponentPlane[1].ui32PhysAddr,
1237                             srf_buf_offset + pTFrame->psb_surface->stride * pTFrame->height
1238                             + (pTFrame->psb_surface->stride) *(pTFrame->height) / 2,
1239                             &pTFrame->psb_surface->buf);
1240        break;
1241    default:
1242        drv_debug_msg(VIDEO_DEBUG_ERROR, " Not supported FOURCC %x!\n", pContext->eFormat);
1243        return -1;
1244
1245    }
1246
1247
1248    drv_debug_msg(VIDEO_DEBUG_GENERAL, "TOPAZ_PDUMP: ui32DataInterleaveStatus %x\n", pContext->pMTXSetup->ui32DataInterleaveStatus);
1249    drv_debug_msg(VIDEO_DEBUG_GENERAL, "TOPAZ_PDUMP: ui32TableA %x \n", pContext->pMTXSetup->ui32TableA);
1250    drv_debug_msg(VIDEO_DEBUG_GENERAL, "TOPAZ_PDUMP:ui32ComponentsInScan %x\n", pContext->pMTXSetup->ui32ComponentsInScan);
1251    for (i32Lp = 0; i32Lp < 3; i32Lp++) {
1252        drv_debug_msg(VIDEO_DEBUG_GENERAL, "TOPAZ_PDUMP: ComponentPlane[%d]: 0x%x, %d, %d\n",
1253                                 i32Lp, pContext->pMTXSetup->ComponentPlane[i32Lp].ui32PhysAddr, pContext->pMTXSetup->ComponentPlane[i32Lp].ui32Stride, pContext->pMTXSetup->ComponentPlane[i32Lp].ui32Height);
1254        drv_debug_msg(VIDEO_DEBUG_GENERAL, "TOPAZ_PDUMP: MCUComponent[%d]: %d, %d, %d, %d\n", i32Lp, pContext->pMTXSetup->MCUComponent[i32Lp].ui32WidthBlocks, pContext->pMTXSetup->MCUComponent[i32Lp].ui32HeightBlocks, pContext->pMTXSetup->MCUComponent[i32Lp].ui32XLimit, pContext->pMTXSetup->MCUComponent[i32Lp].ui32YLimit);
1255    }
1256
1257    i32Lp = ctx->NumCores;
1258    while (--i32Lp > -1) {
1259        /*TOPAZ_InsertCommand(
1260                pEncContext,
1261                i32Lp,
1262                MTX_CMDID_SETUP,
1263                IMG_FALSE,
1264                IMG_NULL,
1265                pContext->pMemInfoMTXSetup );*/
1266
1267        pnw_cmdbuf_insert_command_package(ctx->obj_context,
1268                                          i32Lp,
1269                                          MTX_CMDID_SETUP,
1270                                          &(ctx->obj_context->pnw_cmdbuf->header_mem),
1271                                          0);
1272    }
1273
1274    return ReturnCode;
1275}
1276
1277
1278IMG_UINT32 Legacy_JPGEncodeSOSHeader(LEGACY_JPEG_ENCODER_CONTEXT *pContext, IMG_CODED_BUFFER *pCBuffer)
1279{
1280    IMG_UINT32  ui32TableIndex ;
1281    IMG_UINT8 uc_comp_id, ui8Comp;
1282    STREAMTYPEW s_streamW;
1283    IMG_UINT8 *puc_stream_buff;
1284
1285    puc_stream_buff = (IMG_UINT8 *)(pCBuffer->pMemInfo) + pCBuffer->ui32BytesWritten;
1286
1287    s_streamW.Offset = 0;
1288    s_streamW.Buffer = puc_stream_buff;
1289    s_streamW.Limit = (pCBuffer->ui32Size - pCBuffer->ui32BytesWritten);
1290
1291    /* Start of scan */
1292    fPutBitsToBuffer(&s_streamW, 2, START_OF_SCAN);
1293    /* Scan header length */
1294    fPutBitsToBuffer(&s_streamW, 2, 6 + (pContext->JPEGEncoderParams.uc_num_comp_in_scan << 1));
1295    /* Number of image components in scan */
1296    fPutBitsToBuffer(&s_streamW, 1, pContext->JPEGEncoderParams.uc_num_comp_in_scan);
1297
1298    if (pContext->JPEGEncoderParams.uc_num_comp_in_scan > MAX_COMP_IN_SCAN)
1299        pContext->JPEGEncoderParams.uc_num_comp_in_scan = MAX_COMP_IN_SCAN;
1300    for (ui8Comp = 0; ui8Comp < pContext->JPEGEncoderParams.uc_num_comp_in_scan; ui8Comp++) {
1301        uc_comp_id = pContext->JPEGEncoderParams.puc_comp_id[ui8Comp];
1302        if (uc_comp_id >= MAX_COMP_IN_SCAN) {
1303            drv_debug_msg(VIDEO_DEBUG_ERROR, "Invalide component index %d\n", uc_comp_id);
1304            uc_comp_id = MAX_COMP_IN_SCAN - 1;
1305        }
1306
1307        ui32TableIndex  = pContext->sJPEGEncoderComp.CompIdtoIndex[uc_comp_id];
1308
1309        /* Scan component selector */
1310        fPutBitsToBuffer(&s_streamW, 1, uc_comp_id);
1311
1312        /*4 Bits Dc entropy coding table destination selector */
1313        /*4 Bits Ac entropy coding table destination selector */
1314        ui32TableIndex %= 255;
1315        fPutBitsToBuffer(&s_streamW, 1, (pContext->sJPEGEncoderComp.puc_huff_table_id[ui32TableIndex] << 4) | pContext->sJPEGEncoderComp.puc_huff_table_id[ui32TableIndex]);
1316    }
1317
1318    /* Start of spectral or predictor selection  */
1319    fPutBitsToBuffer(&s_streamW, 1, 0);
1320    /* End of spectral selection */
1321    fPutBitsToBuffer(&s_streamW, 1, 63);
1322    /*4 Bits Successive approximation bit position high (0)*/
1323    /*4 Bits Successive approximation bit position low or point transform (0)*/
1324    fPutBitsToBuffer(&s_streamW, 1, 0);
1325
1326    pCBuffer->ui32BytesWritten += s_streamW.Offset;
1327
1328    return 0;
1329}
1330
1331
1332/***********************************************************************************
1333 * Function Name      : JPGEncodeSOSHeader
1334 * Inputs             : PContext - Pointer to JPEG context
1335 * Inputs             : pui8BitStreamBuffer start of the coded output buffer
1336 * Inputs             : pui32BytesWritten pointer to offset into the coded output buffer at which to write
1337 * Outputs            : pui8BitStreamBuffer - Start of Scan header written to the coded buffer
1338 * Returns            : Bytes writtren
1339 * Description        : This function writes the Start of Scan Header
1340 ************************************************************************************/
1341
1342IMG_UINT32 JPGEncodeSOSHeader(/*in */           TOPAZSC_JPEG_ENCODER_CONTEXT *pContext,
1343        /*out */        IMG_UINT8*              pui8BitStreamBuffer ,
1344        /*out*/         IMG_UINT32*             pui32BytesWritten)
1345{
1346    IMG_UINT8 uc_comp_id, ui8Comp;
1347    STREAMTYPEW s_streamW;
1348
1349    s_streamW.Offset = 0;
1350    s_streamW.Buffer = pui8BitStreamBuffer + *pui32BytesWritten;
1351
1352    /* Start of scan */
1353    fPutBitsToBuffer(&s_streamW, 2, START_OF_SCAN);
1354    /* Scan header length */
1355    fPutBitsToBuffer(&s_streamW, 2, 6 + (pContext->pMTXSetup->ui32ComponentsInScan << 1));
1356    /* Number of image components in scan */
1357    fPutBitsToBuffer(&s_streamW, 1, pContext->pMTXSetup->ui32ComponentsInScan);
1358    for (ui8Comp = 0; ui8Comp < pContext->pMTXSetup->ui32ComponentsInScan; ui8Comp++) {
1359        uc_comp_id = ui8Comp + 1;
1360
1361        /* Scan component selector */
1362        fPutBitsToBuffer(&s_streamW, 1, uc_comp_id);
1363
1364        /*4 Bits Dc entropy coding table destination selector */
1365        /*4 Bits Ac entropy coding table destination selector */
1366        fPutBitsToBuffer(&s_streamW, 1, ((ui8Comp != 0 ? 1 : 0) << 4) | (ui8Comp != 0 ? 1 : 0)); // Huffman table refs = 0 Luma 1 Chroma
1367    }
1368
1369    /* Start of spectral or predictor selection  */
1370    fPutBitsToBuffer(&s_streamW, 1, 0);
1371    /* End of spectral selection */
1372    fPutBitsToBuffer(&s_streamW, 1, 63);
1373    /*4 Bits Successive approximation bit position high (0)*/
1374    /*4 Bits Successive approximation bit position low or point transform (0)*/
1375    fPutBitsToBuffer(&s_streamW, 1, 0);
1376
1377    *pui32BytesWritten += s_streamW.Offset;
1378
1379    return 0;
1380}
1381
1382
1383/*****************************************************************************/
1384/*                                                                           */
1385/* Function Name : EncodeMJPEGAPP1Marker                                     */
1386/*                                                                           */
1387/* Description   : Writes a Quicktime MJPEG (A) marker                                       */
1388/*                                                                           */
1389/* Inputs        :                                                           */
1390/*                                      pContext                        - Ptr to the JPEG encoder context        */
1391/*                                      puc_stream_buff         - Ptr to the bistream buffer from        */
1392/*                                                                                      where to start writing                   */
1393/*                                                                           */
1394/* Processing    :                                                                                                                       */
1395/*                                                                           */
1396/* Returns       : Number of bytes written into the stream                   */
1397/*                                                                           */
1398/*****************************************************************************/
1399IMG_UINT32 Insert_QT_MJPEGA_APP1Marker(IMG_CODED_BUFFER *pCBuffer, IMG_UINT8 *pui8BitStreamBuffer, IMG_UINT32 ui32OffsetBytes, IMG_BOOL bIsFirstField)
1400{
1401    STREAMTYPEW ps_streamW;
1402#ifdef JPEG_VERBOSE
1403    printf("Insert_QT_MJPEGA_APP1Marker");
1404#endif
1405
1406    ps_streamW.Offset = 0;
1407    ps_streamW.Buffer = &pui8BitStreamBuffer[ui32OffsetBytes];
1408
1409    /* Writing the start of image marker */
1410    fPutBitsToBuffer(&ps_streamW, 2, START_OF_IMAGE);
1411    /* Writing the Motion-JPEG APP1 marker */
1412    fPutBitsToBuffer(&ps_streamW, 2, MJPEG_APP1);
1413    /* Marker content length */
1414    fPutBitsToBuffer(&ps_streamW, 2, 0x002A);
1415    /* Reserved, set to zero */
1416    fPutBitsToBuffer(&ps_streamW, 4, 0x00000000);
1417    /* Motion-JPEG tag = "mjpg"*/
1418    fPutBitsToBuffer(&ps_streamW, 4, 0x6D6A7067);
1419    /* Field size*/
1420    fPutBitsToBuffer(&ps_streamW, 4, pCBuffer->ui32BytesWritten + 2 - ui32OffsetBytes); // +2 because the EOI marker not written yet
1421    /* Padded Field size*/
1422    fPutBitsToBuffer(&ps_streamW, 4, pCBuffer->ui32BytesWritten + 2 - ui32OffsetBytes);
1423    /* Offset to next field*/
1424    if (bIsFirstField)
1425        fPutBitsToBuffer(&ps_streamW, 4, pCBuffer->ui32BytesWritten + 2 - ui32OffsetBytes); // Points to start of next field
1426    else
1427        fPutBitsToBuffer(&ps_streamW, 4, 0); // Set to zero for second field
1428
1429    /* Quantization Table Offset*/
1430    // May be able to leave this at zero
1431    fPutBitsToBuffer(&ps_streamW, 4, H_QT_OFFSET);
1432    /* Huffman Table Offset*/
1433    // May be able to leave this at zero
1434    fPutBitsToBuffer(&ps_streamW, 4, H_HT_OFFSET);
1435    /* Start Of Frame Offset*/
1436    fPutBitsToBuffer(&ps_streamW, 4, H_SOF_OFFSET);
1437    /* Start Of Scan Offset*/
1438    fPutBitsToBuffer(&ps_streamW, 4, H_SOS_OFFSET);
1439    /* Start of data offset*/
1440    fPutBitsToBuffer(&ps_streamW, 4, H_SOI_OFFSET);
1441
1442    return 0;
1443}
1444
1445/* JPEG Start picture function. Sets up all context information, Quantization details, Header output and MTX ready for the main encode loop*/
1446IMG_ERRORCODE SetupJPEGTables(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER *pCBuffer,  object_surface_p pTFrame)
1447{
1448    /*IMG_UINT32 rc = 0;
1449    const IMG_UINT8* StandardQuantLuma;
1450    const IMG_UINT8* StandardQuantChroma;
1451    IMG_UINT8* QuantLumaTableOnHost;
1452    IMG_UINT8* QuantChromaTableOnHost;
1453    JPEG_MTX_QUANT_TABLE * psQuantTables;*/
1454    IMG_UINT16 ui16Lp;
1455    IMG_UINT8 ui8Planes[MAX_COMP_IN_SCAN];
1456    /*IMG_ENC_CONTEXT * pEncContext = (IMG_ENC_CONTEXT*)hEncContext;*/
1457    /*TOPAZSC_JPEG_ENCODER_CONTEXT * pContext = pEncContext->psJpegHC;*/
1458    IMG_INT8 i;
1459    context_ENC_p ctx = (context_ENC_p)pContext->ctx;
1460
1461    // Lets add a pointer from our context to the source surface structure (remember we still need to use GETBUFFER commands if we want to get shared image memory, but info data structures are on host)
1462    pContext->pSourceSurface = pTFrame;
1463
1464    //Try setting MCU details from here
1465    /*JPGEncodeBegin(pContext, pTFrame);*/
1466    InitializeJpegEncode(pContext, pTFrame);
1467
1468    // Special case for YUYV format
1469    if (ISCHROMAINTERLEAVED(pContext->eFormat) > C_INTERLEAVE)
1470        pContext->sScan_Encode_Info.ui32NumberMCUsX = (pContext->pMTXSetup->MCUComponent[0].ui32XLimit + ((pContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks * 2) - 1)) / (pContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks * 2);
1471    else
1472        pContext->sScan_Encode_Info.ui32NumberMCUsX = (pContext->pMTXSetup->MCUComponent[0].ui32XLimit + (pContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks - 1)) / pContext->pMTXSetup->MCUComponent[0].ui32WidthBlocks;
1473
1474    pContext->sScan_Encode_Info.ui32NumberMCUsY = (pContext->pMTXSetup->MCUComponent[0].ui32YLimit + (pContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks - 1)) / pContext->pMTXSetup->MCUComponent[0].ui32HeightBlocks;
1475    pContext->sScan_Encode_Info.ui32NumberMCUsToEncode = pContext->sScan_Encode_Info.ui32NumberMCUsX * pContext->sScan_Encode_Info.ui32NumberMCUsY;
1476
1477    pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan =
1478        JPEG_MCU_PER_SCAN(ctx->Width, ctx->Height, ctx->NumCores, pContext->eFormat);
1479
1480    drv_debug_msg(VIDEO_DEBUG_GENERAL, "MCUs To Encode %dx%d\n",
1481                             pContext->sScan_Encode_Info.ui32NumberMCUsX,
1482                             pContext->sScan_Encode_Info.ui32NumberMCUsY);
1483    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Total MCU %d, per scan %d\n", pContext->sScan_Encode_Info.ui32NumberMCUsToEncode, pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan);
1484
1485    //Allocate our coded data buffers here now we know the number of MCUs we're encoding
1486    /*Use slice parameter buffer*/
1487    if (AllocateCodedDataBuffers(pContext) != IMG_ERR_OK) return IMG_ERR_MEMORY;
1488    // Send Setup message to MTX to initialise it
1489    /*EncodeInitMTX(pEncContext);        Set predictors to zero */
1490
1491    for (i = ctx->NumCores - 1; i >= 0; i--) {
1492        pnw_cmdbuf_insert_command_package(
1493            ((context_ENC_p)pContext->ctx)->obj_context,
1494            i,
1495            MTX_CMDID_RESET_ENCODE,
1496            NULL,
1497            0);
1498    }
1499    //CODE HERE SIMPLIFIED AS WE KNOW THERE WILL ONLY EVER BE 3 IMAGE COMPONENTS
1500    /* Set up Source panes for HW */
1501    // Reverse colour plane pointers fed to MTX when required by format
1502
1503    switch (pContext->eFormat) {
1504    case IMG_CODEC_YV16:
1505        pContext->pMTXSetup->ComponentPlane[0].ui32Stride = pTFrame->psb_surface->stride;
1506        pContext->pMTXSetup->ComponentPlane[1].ui32Stride = pTFrame->psb_surface->stride / 2;
1507        pContext->pMTXSetup->ComponentPlane[2].ui32Stride = pTFrame->psb_surface->stride / 2;
1508
1509        pContext->pMTXSetup->ComponentPlane[0].ui32Height = pTFrame->height;
1510        pContext->pMTXSetup->ComponentPlane[1].ui32Height = pTFrame->height;
1511        pContext->pMTXSetup->ComponentPlane[2].ui32Height = pTFrame->height;
1512
1513        /*YV16's plane order is Y, V, U*/
1514        ui8Planes[0] = 0;
1515        ui8Planes[2] = 1;
1516        ui8Planes[1] = 2;
1517        break;
1518    case IMG_CODEC_IYUV:
1519    case IMG_CODEC_IMC2:
1520        /*case IMG_CODEC_422_IMC2:
1521            SetupYUVPlaneDetails(&(pContext->pMTXSetup->ComponentPlane[0]),
1522                    &(pTFrame->aui32ComponentInfo[0]));
1523
1524            SetupYUVPlaneDetails(&(pContext->pMTXSetup->ComponentPlane[2]),
1525                    &(pTFrame->aui32ComponentInfo[1]));
1526
1527            SetupYUVPlaneDetails(&(pContext->pMTXSetup->ComponentPlane[1]),
1528                    &(pTFrame->aui32ComponentInfo[2]));*/
1529        pContext->pMTXSetup->ComponentPlane[0].ui32Stride = pTFrame->psb_surface->stride;
1530        pContext->pMTXSetup->ComponentPlane[1].ui32Stride = pTFrame->psb_surface->stride / 2;
1531        pContext->pMTXSetup->ComponentPlane[2].ui32Stride = pTFrame->psb_surface->stride / 2;
1532
1533        pContext->pMTXSetup->ComponentPlane[0].ui32Height = pTFrame->height;
1534        pContext->pMTXSetup->ComponentPlane[1].ui32Height = pTFrame->height / 2;
1535        pContext->pMTXSetup->ComponentPlane[2].ui32Height = pTFrame->height / 2;
1536
1537        //ui8Planes[0]=0;ui8Planes[1]=2;ui8Planes[2]=1;
1538        /*IYUV don't need to swap UV color space. Not sure about IMC12*/
1539        ui8Planes[0] = 0;
1540        ui8Planes[1] = 1;
1541        ui8Planes[2] = 2;
1542        break;
1543    default:
1544        /*SetupYUVPlaneDetails(&(pContext->pMTXSetup->ComponentPlane[0]),
1545            &(pTFrame->aui32ComponentInfo[0]));
1546
1547        SetupYUVPlaneDetails(&(pContext->pMTXSetup->ComponentPlane[1]),
1548            &(pTFrame->aui32ComponentInfo[1]));
1549
1550        SetupYUVPlaneDetails(&(pContext->pMTXSetup->ComponentPlane[2]),
1551            &(pTFrame->aui32ComponentInfo[2]));*/
1552        /* It's for NV12*/
1553        pContext->pMTXSetup->ComponentPlane[0].ui32Stride = pTFrame->psb_surface->stride;
1554        pContext->pMTXSetup->ComponentPlane[1].ui32Stride = pTFrame->psb_surface->stride;
1555        pContext->pMTXSetup->ComponentPlane[2].ui32Stride = pTFrame->psb_surface->stride;
1556
1557        pContext->pMTXSetup->ComponentPlane[0].ui32Height = pTFrame->height;
1558        pContext->pMTXSetup->ComponentPlane[1].ui32Height = pTFrame->height / 2;
1559        pContext->pMTXSetup->ComponentPlane[2].ui32Height = pTFrame->height / 2;
1560
1561        ui8Planes[0] = 0;
1562        ui8Planes[1] = 1;
1563        ui8Planes[2] = 2;
1564        break;
1565    };
1566
1567
1568    SetupIssueSetup(pContext, pContext->pMTXSetup->ui32ComponentsInScan, ui8Planes, pTFrame, 0 , 1);
1569
1570    if (pContext->pMTXSetup->ui32ComponentsInScan > MAX_COMP_IN_SCAN) {
1571        return IMG_ERR_UNDEFINED;
1572    }
1573
1574    if ((pCBuffer->ui32Size - pCBuffer->ui32BytesWritten) < 9 + 6 + (4 *(IMG_UINT32)pContext->pMTXSetup->ui32ComponentsInScan)) {
1575        return  IMG_ERR_MEMORY;
1576    }
1577
1578    // Reset Scan Encode structures - just in case
1579    for (ui16Lp = 0; ui16Lp < pContext->sScan_Encode_Info.ui8NumberOfCodedBuffers; ui16Lp++) {
1580        BUFFER_HEADER *pbh;
1581
1582        pContext->sScan_Encode_Info.aBufferTable[ui16Lp].i8MTXNumber = 0; // Indicates buffer is idle
1583        pContext->sScan_Encode_Info.aBufferTable[ui16Lp].ui16ScanNumber = 0; // Indicates buffer is idle
1584
1585        pContext->sScan_Encode_Info.aBufferTable[ui16Lp].ui32DataBufferUsedBytes = 0;
1586
1587        //pbh = MMGetHostLinAddress(pContext->sScan_Encode_Info.aBufferTable[ui16Lp].pMemInfo );
1588        pbh = (BUFFER_HEADER *)(pContext->sScan_Encode_Info.aBufferTable[ui16Lp].pMemInfo);
1589        pbh->ui32BytesEncoded = 0;
1590        pbh->ui32BytesUsed = sizeof(BUFFER_HEADER);
1591    }
1592
1593    //Prefill out MTXIdleTable with MTX references (0 is the Master, and will be the last sent)
1594    for (pContext->sScan_Encode_Info.ui8MTXIdleCnt = 0; pContext->sScan_Encode_Info.ui8MTXIdleCnt < ctx->NumCores; pContext->sScan_Encode_Info.ui8MTXIdleCnt++) {
1595        pContext->sScan_Encode_Info.aui8MTXIdleTable[pContext->sScan_Encode_Info.ui8MTXIdleCnt] = pContext->sScan_Encode_Info.ui8MTXIdleCnt + 1;
1596    }
1597
1598
1599    //Need to set up CB Output slicenumber to equal number of slices required to encode image
1600    // Set current CB scan to maximum scan number (will count down as scans are output)
1601    pContext->sScan_Encode_Info.ui16CScan = (pContext->sScan_Encode_Info.ui32NumberMCUsToEncode + (pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan - 1)) / pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
1602    pContext->sScan_Encode_Info.ui16ScansInImage = pContext->sScan_Encode_Info.ui16CScan;
1603    // Set next scan to send to MTX to maximum scan number
1604    pContext->sScan_Encode_Info.ui16SScan = pContext->sScan_Encode_Info.ui16CScan;
1605    pContext->ui32InitialCBOffset = 0;
1606
1607    return IMG_ERR_OK;
1608}
1609
1610IMG_ERRORCODE Legacy_PrepareHeader(LEGACY_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER *pCBuffer)
1611{
1612    IMG_ERRORCODE rc;
1613    IMG_UINT8 *ui8OutputBuffer;
1614
1615    ui8OutputBuffer = pCBuffer->pMemInfo;
1616    //Lock our JPEG Coded buffer
1617    /* if (IMG_JPEG_GetBuffer(pCBuffer, (IMG_VOID **) &ui8OutputBuffer)!=IMG_ERR_OK)
1618     return IMG_ERR_SURFACE_LOCKED;*/
1619
1620    pCBuffer->ui32BytesWritten = 0;
1621    *((IMG_UINT32*) ui8OutputBuffer) = 0;
1622
1623    // JPGEncodeMarker - Currently misses out the APP0 header
1624    rc = Legacy_JPGEncodeMarker(pContext, (IMG_UINT8 *) ui8OutputBuffer,  &pCBuffer->ui32BytesWritten);
1625    if (rc) return rc;
1626
1627    rc = Legacy_JPGEncodeHeader(pContext , (IMG_UINT8 *) ui8OutputBuffer ,  &pCBuffer->ui32BytesWritten);
1628    if (rc) return rc;
1629
1630    //  JPGEncodeDRIMarker(pContext,pCBuffer);
1631    Legacy_JPGEncodeSOSHeader(pContext, pCBuffer);
1632
1633    //IMG_JPEG_ReleaseBuffer( pCBuffer);
1634
1635
1636    return IMG_ERR_OK;
1637}
1638/* Send header will work for each type of header*/
1639
1640/***********************************************************************************
1641 * Function Name      : PrepareHeader
1642 * Inputs             : Pointer to JPEG Context, Point to coded buffer
1643 * Outputs            :
1644 * Returns            : IMG_ERRORCODE
1645 * Description        : Writes required JPEG Header elements to the coded buffer
1646 ************************************************************************************/
1647IMG_ERRORCODE PrepareHeader(TOPAZSC_JPEG_ENCODER_CONTEXT * pContext, IMG_CODED_BUFFER *pCBuffer, IMG_UINT32 ui32StartOffset, IMG_BOOL bIncludeHuffmanTables)
1648{
1649    IMG_ERRORCODE rc;
1650    IMG_UINT8 *ui8OutputBuffer;
1651
1652    //Lock our JPEG Coded buffer
1653    /*if (IMG_C_GetBuffer((IMG_HENC_CONTEXT) pContext, pCBuffer, (IMG_VOID **) &ui8OutputBuffer)!=IMG_ERR_OK)
1654    return IMG_ERR_SURFACE_LOCKED;*/
1655
1656    ui8OutputBuffer = (IMG_UINT8 *)pCBuffer->pMemInfo;
1657    pCBuffer->ui32BytesWritten = ui32StartOffset;
1658    *((IMG_UINT32*) ui8OutputBuffer + pCBuffer->ui32BytesWritten) = 0;
1659
1660
1661    // JPGEncodeMarker - Currently misses out the APP0 header
1662    rc = JPGEncodeMarker(pContext, (IMG_UINT8 *) ui8OutputBuffer,  &pCBuffer->ui32BytesWritten, bIncludeHuffmanTables);
1663    if (rc) return rc;
1664
1665    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Current bytes of coded buf used: %d\n", pCBuffer->ui32BytesWritten);
1666    rc = JPGEncodeHeader(pContext , (IMG_UINT8 *) ui8OutputBuffer ,  &pCBuffer->ui32BytesWritten);
1667    if (rc) return rc;
1668
1669    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Current bytes of coded buf used: %d\n", pCBuffer->ui32BytesWritten);
1670    rc = JPGEncodeSOSHeader(pContext, (IMG_UINT8 *) ui8OutputBuffer, &pCBuffer->ui32BytesWritten);
1671    if (rc) return rc;
1672
1673    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Current bytes of coded buf used: %d\n", pCBuffer->ui32BytesWritten);
1674    /*IMG_C_ReleaseBuffer((IMG_HENC_CONTEXT) pContext, pCBuffer);*/
1675    return IMG_ERR_OK;
1676}
1677
1678
1679
1680
1681/***********************************************************************************
1682 * Function Name      : IMG_JPEG_ISSUEBUFFERTOHW
1683 * Inputs             :
1684 * Outputs            : pui32MCUsToProccess
1685 * Returns            :
1686 * Description        : Issues a buffer to MTX to recieve coded data.
1687 *
1688 ************************************************************************************/
1689
1690IMG_ERRORCODE IssueBufferToHW(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext, TOPAZSC_JPEG_BUFFER_INFO* pWriteBuf, IMG_UINT16 ui16BCnt, IMG_UINT32 ui32NoMCUsToEncode, IMG_INT8 i8MTXNumber)
1691{
1692    MTX_ISSUE_BUFFERS *psBufferCmd;
1693    context_ENC_p ctx = (context_ENC_p)pContext->ctx;
1694    /*IMG_ENC_CONTEXT * pEncContext = (IMG_ENC_CONTEXT*)hContext;
1695    TOPAZSC_JPEG_ENCODER_CONTEXT *pContext = pEncContext->psJpegHC;*/
1696
1697#ifdef JPEG_VERBOSE
1698    printf("\n**************************************************************************\n");
1699    printf("** HOST SENDING Scan:%i (%i MCUs) to MTX %i, using Buffer %i\n", pWriteBuf->ui16ScanNumber, ui32NoMCUsToEncode, i8MTXNumber - 1, ui16BCnt);
1700#endif
1701    drv_debug_msg(VIDEO_DEBUG_GENERAL, "HOST SENDING Scan:%d (%d MCUs, offset %d MCUs)"
1702                             " to MTX %d, using Buffer %d\n",
1703                             pWriteBuf->ui16ScanNumber, ui32NoMCUsToEncode,
1704                             pContext->sScan_Encode_Info.ui32CurMCUsOffset,
1705                             i8MTXNumber, ui16BCnt);
1706
1707    // Issue to MTX ////////////////////////////
1708    ASSERT(pWriteBuf->pMemInfo);
1709
1710    // Lets send our input parameters using the device memory allocated for the coded output
1711    psBufferCmd = (MTX_ISSUE_BUFFERS *)(pWriteBuf->pMemInfo);
1712
1713    // We've disabled size bound checking in firmware, so can use the full 31 bits for NoMCUsToEncode instead
1714    //psBufferCmd->ui32MCUCntAndResetFlag       = ( DATA_BUFFER_SIZE(pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan) << 16) | (ui32NoMCUsToEncode << 1) | 0x1;
1715
1716    psBufferCmd->ui32MCUCntAndResetFlag = (ui32NoMCUsToEncode << 1) | 0x1;
1717    psBufferCmd->ui32CurrentMTXScanMCUPosition = pContext->sScan_Encode_Info.ui32CurMCUsOffset;
1718    drv_debug_msg(VIDEO_DEBUG_GENERAL, "TOPAZ_PDUMP: ui32MCUCntAndResetFlag 0x%x\n", psBufferCmd->ui32MCUCntAndResetFlag);
1719    drv_debug_msg(VIDEO_DEBUG_GENERAL, "TOPAZ_PDUMP: ui32CurrentMTXScanMCUPosition 0x%x\n", psBufferCmd->ui32CurrentMTXScanMCUPosition);
1720    /* We do not need to do this but in order for params to match on HW we need to know whats in the buffer*/
1721    /*MMUpdateDeviceMemory(pWriteBuf->pMemInfo );*/
1722
1723    /*TOPAZ_InsertCommand(
1724        pEncContext,
1725        (IMG_INT32) (i8MTXNumber-1),
1726        MTX_CMDID_ISSUEBUFF,
1727        IMG_FALSE,
1728        &pWriteBuf->ui32WriteBackVal,
1729        pWriteBuf->pMemInfo );*/
1730    pnw_cmdbuf_insert_command_package(ctx->obj_context,
1731                                      (IMG_INT32)(i8MTXNumber) ,
1732                                      MTX_CMDID_ISSUEBUFF,
1733                                      ctx->coded_buf->psb_buffer,
1734                                      ui16BCnt * pContext->ui32SizePerCodedBuffer + PNW_JPEG_HEADER_MAX_SIZE);
1735
1736    return IMG_ERR_OK;
1737}
1738
1739/***********************************************************************************
1740 * Function Name      : SubmitScanToMTX
1741 * Inputs             :
1742 *                                              pContext - Pointer to JPEG context
1743 *                                              ui16BCnt - Index of the buffer to associate with the MTX
1744 *                                              i8MTXNumber - The ID number of the MTX that will have a scan sent to it
1745 * Outputs            : Status update to current buffer, MTX is activated
1746 * Returns            : Errorcode
1747 * Description        : Associates the empty buffer with the inactive MTX and then
1748 *                                              starts a scan, with output to be sent to the buffer.
1749 ************************************************************************************/
1750
1751IMG_ERRORCODE SubmitScanToMTX(TOPAZSC_JPEG_ENCODER_CONTEXT *pContext,
1752                              IMG_UINT16 ui16BCnt,
1753                              IMG_INT8 i8MTXNumber,
1754                              IMG_UINT32 ui32NoMCUsToEncode)
1755{
1756    /*IMG_ENC_CONTEXT * pEncContext = (IMG_ENC_CONTEXT*)hContext;
1757    TOPAZSC_JPEG_ENCODER_CONTEXT *pContext = pEncContext->psJpegHC;*/
1758
1759
1760#if 0
1761    // Submit a scan and buffer to the appropriate MTX unit
1762    IMG_UINT32 ui32NoMCUsToEncode;
1763
1764    if (pContext->sScan_Encode_Info.ui16SScan == 0) {
1765        // Final scan, may need fewer MCUs than buffer size, calculate the remainder
1766        ui32NoMCUsToEncode = pContext->sScan_Encode_Info.ui32NumberMCUsToEncode % pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
1767        if (ui32NoMCUsToEncode == 0)    ui32NoMCUsToEncode = pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
1768    } else
1769        ui32NoMCUsToEncode = pContext->sScan_Encode_Info.ui32NumberMCUsToEncodePerScan;
1770#endif
1771    //Set the buffer returned size to -1
1772    pContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui32DataBufferUsedBytes = ((BUFFER_HEADER*)(pContext->sScan_Encode_Info.aBufferTable[ui16BCnt].pMemInfo))->ui32BytesUsed = -1; // Won't be necessary with SC Peek commands enabled
1773    IssueBufferToHW(pContext, &(pContext->sScan_Encode_Info.aBufferTable[ui16BCnt]), ui16BCnt, ui32NoMCUsToEncode, i8MTXNumber);
1774
1775    drv_debug_msg(VIDEO_DEBUG_GENERAL, "Submitting scan %i to MTX %i and Buffer %i\n", pContext->sScan_Encode_Info.aBufferTable[ui16BCnt].ui16ScanNumber, i8MTXNumber, ui16BCnt);
1776    return IMG_ERR_OK;
1777}
1778
1779
1780void pnw_jpeg_set_default_qmatix(unsigned char *pMemInfoTableBlock)
1781{
1782    JPEG_MTX_QUANT_TABLE *pQTable = (JPEG_MTX_QUANT_TABLE *)pMemInfoTableBlock;
1783    memcpy(pQTable->aui8LumaQuantParams, gQuantLuma, QUANT_TABLE_SIZE_BYTES);
1784    memcpy(pQTable->aui8ChromaQuantParams, gQuantChroma, QUANT_TABLE_SIZE_BYTES);
1785    return;
1786}
1787