xref: /utopia/UTPA2-700.0.x/modules/gpd/drv/gpd/inftrees.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 /* inftrees.c -- generate Huffman trees for efficient decoding
79  * Copyright (C) 1995-2005 Mark Adler
80  * For conditions of distribution and use, see copyright notice in zlib.h
81  */
82 
83 
84 #include "MsCommon.h"
85 #include "zutil.h"
86 #include "inftrees.h"
87 #include "drvgpd.h"
88 //#include <semihost_io.h>
89 //#include "board.h"
90 
91 #define MAXBITS 15
92 
93 static MS_U16 mincode_tmp[MAXBITS+1];
94 static MS_U16 offset_tmp[MAXBITS+2];
95 static MS_U16 count[MAXBITS+1];    /* number of codes of each length */
96 static MS_U16 offs[MAXBITS+2];     /* offsets in table for each length */
97 
98 static const MS_U16 lbase[31] = { /* Length codes 257..285 base */
99 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
100 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
101 static const MS_U16 lext[31] = { /* Length codes 257..285 extra */
102 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
103 19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
104 static const MS_U16 dbase[32] = { /* Distance codes 0..29 base */
105 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
106 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
107 8193, 12289, 16385, 24577, 0, 0};
108 static const MS_U16 dext[32] = { /* Distance codes 0..29 extra */
109 16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
110 23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
111 28, 28, 29, 29, 64, 64};
112 
113 //const char inflate_copyright[] =
114 //   " inflate 1.2.3 Copyright 1995-2005 Mark Adler ";
115 
gpdinflate_table(type,lens,codes,table,bits,work)116 MS_U32 gpdinflate_table(type, lens, codes, table, bits, work)
117 codetype type;
118 MS_U16 FAR *lens;
119 MS_U32 codes;
120 code FAR * FAR *table;
121 MS_U32 FAR *bits;
122 MS_U16 FAR *work;
123 {
124     MS_U32 i=0;
125     //extern MS_U32 huff_reg_time;
126     MS_U16 mincode=0;
127     //MS_U16 mincode_tmp[288];
128     //MS_U16 offset_tmp[MAXBITS+1];
129     MS_U16  mincode_valid=0;
130     MS_U32 len;               /* a code's length in bits */
131     MS_U32 sym;               /* index of code symbols */
132     MS_U32 min, max;          /* minimum and maximum code lengths */
133     MS_U32 root;              /* number of index bits for root table */
134     MS_U32 curr;              /* number of index bits for current table */
135     MS_U32 drop;              /* code bits to drop for sub-table */
136     MS_U32 left;                   /* number of prefix codes available */
137     MS_U32 used;              /* code entries in table used */
138     MS_U32 huff;              /* Huffman code */
139     MS_U32 incr;              /* for incrementing code, index */
140     MS_U32 fill;              /* index for replicating entries */
141     MS_U32 low;               /* low bits for current root entry */
142     MS_U32 mask;              /* mask for low root bits */
143     code this;                  /* table entry for duplication */
144     code FAR *next;             /* next available space in table */
145     const MS_U16 FAR *base;     /* base value table to use */
146     const MS_U16 FAR *extra;    /* extra bits table to use */
147     MS_U32 end;                    /* use base and extra for symbol > end */
148    //MS_U16 count[MAXBITS+1];    /* number of codes of each length */
149    //MS_U16 offs[MAXBITS+1];     /* offsets in table for each length */
150 
151  #if 0
152     static const MS_U16 lbase[31] = { /* Length codes 257..285 base */
153         3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
154         35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
155     static const MS_U16 lext[31] = { /* Length codes 257..285 extra */
156         16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
157         19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 201, 196};
158     static const MS_U16 dbase[32] = { /* Distance codes 0..29 base */
159         1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
160         257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
161         8193, 12289, 16385, 24577, 0, 0};
162     static const MS_U16 dext[32] = { /* Distance codes 0..29 extra */
163         16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
164         23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
165         28, 28, 29, 29, 64, 64};
166 #endif
167 
168     //console_printf("creating inflate table\n");
169 
170 
171     for(len=0;len<=MAXBITS;len++)
172         count[len]=0;
173 
174     //console_printf("clear length\n");
175 
176 
177     for (sym = 0; sym < codes; sym++)
178         count[lens[sym]]++;
179 
180     //console_printf("accumulate length for codes\n");
181 
182     /* bound code lengths, force root to be within code lengths */
183     root = *bits;
184     for (max = MAXBITS; max >= 1; max--)
185         if (count[max] != 0) break;
186     if (root > max) root = max;
187     if (max == 0) {                     /* no symbols to code at all */
188         this.op = (MS_U8)64;    /* invalid code marker */
189         this.bits = (MS_U8)1;
190         this.val = (MS_U16)0;
191         *(*table)++ = this;             /* make a table to force an error */
192         *(*table)++ = this;
193         *bits = 1;
194         return 0;     /* no symbols, but wait for decoding to report error */
195     }
196     for (min = 1; min <= MAXBITS; min++)
197         if (count[min] != 0) break;
198 
199     if (root < min) root = min;
200 
201     /* check for an over-subscribed or incomplete set of lengths */
202     left = 1;
203     for (len = 1; len <= MAXBITS; len++) {
204         left <<= 1;
205         left -= count[len];
206         if (left < 0) return -1;        /* over-subscribed */
207     }
208     if (left > 0 && (type == CODES || max != 1))
209         return -1;                      /* incomplete set */
210 
211     /* generate offsets into symbol table for each length for sorting */
212     offs[1] = 0;
213     offset_tmp[1]=0;
214     for (len = 1; len < MAXBITS+1; len++)
215         offset_tmp[len+1]=offs[len + 1] = offs[len] + count[len];
216 
217     //console_printf("generate offset\n");
218 
219 
220     /* sort symbols by length, by symbol order within each length */
221     for (sym = 0; sym < codes; sym++)
222         if (lens[sym] != 0) work[offs[lens[sym]]++] = (MS_U16)sym;
223 
224     //console_printf("sort symbols\n");
225 
226 
227    if(type==CODES)
228    {
229         base = extra = work;    /* dummy value--not used */
230         end = 19;
231 
232         /* initialize state for loop */
233         huff = 0;                   /* starting code */
234         sym = 0;                    /* starting code symbol */
235         len = min;                  /* starting code length */
236         next = *table;              /* current table to fill in */
237         curr = root;                /* current table index bits */
238         drop = 0;                   /* current bits to drop from code for index */
239         low = (MS_U32)(-1);       /* trigger new sub-table when len > root */
240         used = 1U << root;          /* use root table entries */
241         mask = used - 1;            /* mask for comparing low */
242 
243         //console_printf("initialize state for loop\n");
244 
245         /* check available table space */
246          if (used >= ENOUGH - MAXD)
247         return 1;
248 
249         /* process all codes and make table entries */
250         for (;;) {
251             /* create table entry */
252             this.bits = (MS_U8)(len - drop);
253             if ((int)(work[sym]) < end) {
254                 this.op = (MS_U8)0;
255                 this.val = work[sym];
256             }
257             else if ((int)(work[sym]) > end) {
258                 this.op = (MS_U8)(extra[work[sym]]);
259                 this.val = base[work[sym]];
260             }
261             else {
262                 this.op = (MS_U8)(32 + 64);         /* end of block */
263                 this.val = 0;
264             }
265 
266             /* replicate for those indices with low len bits equal to huff */
267             incr = 1U << (len - drop);
268             fill = 1U << curr;
269             min = fill;                 /* save offset to next table */
270             do {
271                 fill -= incr;
272                 next[(huff >> drop) + fill] = this;
273             } while (fill != 0);
274 
275 
276 
277             /* backwards increment the len-bit code huff */
278             incr = 1U << (len - 1);
279             while (huff & incr)
280                 incr >>= 1;
281             if (incr != 0) {
282                 huff &= incr - 1;
283                 huff += incr;
284             }
285             else
286                 huff = 0;
287 
288             /* go to next symbol, update count, len */
289             sym++;
290             if (len <= MAXBITS)
291             {
292                 if (--(count[len]) == 0) {
293                     if (len == max) break;
294                         len = lens[work[sym]];
295                 }
296             }
297 
298             /* create new sub-table if needed */
299             if (len > root && (huff & mask) != low) {
300 
301                 /* if first time, transition to sub-tables */
302                 if (drop == 0)
303                     drop = root;
304 
305                 /* increment past last table */
306                 next += min;            /* here min is 1 << curr */
307 
308                 /* determine length of next table */
309                 curr = len - drop;
310                 left = (int)(1 << curr);
311 
312                 while ((curr + drop) < max)
313                 {
314                     MS_U32 index = (curr + drop)%(MAXBITS+1);
315                     if(index <= MAXBITS)   // (curr + drop) < MAXBITS is for coverity check
316                     {
317                         left -= count[index];
318                     }
319                     if (left <= 0) break;
320                     curr++;
321                     left <<= 1;
322                 }
323 
324                 /* check for enough space */
325                 used += 1U << curr;
326                 if (used >= ENOUGH - MAXD)
327                     return 1;
328 
329                 /* point entry in root table to sub-table */
330                 low = huff & mask;
331                 (*table)[low].op = (MS_U8)curr;
332                 (*table)[low].bits = (MS_U8)root;
333                 (*table)[low].val = (MS_U16)(next - *table);
334             }
335         }
336 
337         this.op = (MS_U8)64;                /* invalid code marker */
338         this.bits = (MS_U8)(len - drop);
339         this.val = (MS_U16)0;
340         while (huff != 0) {
341             /* when done with sub-table, drop back to root table */
342             if (drop != 0 && (huff & mask) != low) {
343                 drop = 0;
344                 len = root;
345                 next = *table;
346                 this.bits = (MS_U8)len;
347             }
348 
349             /* put invalid code marker in table */
350             next[huff >> drop] = this;
351 
352             /* backwards increment the len-bit code huff */
353             incr = 1U << (len - 1);
354             while (huff & incr)
355                 incr >>= 1;
356             if (incr != 0) {
357                 huff &= incr - 1;
358                 huff += incr;
359             }
360             else
361                 huff = 0;
362         }
363 
364         /* set return parameters */
365         *table += used;
366         *bits = root;
367    }
368    else
369    {
370         for (i = (int)min; i <= (int)max; i++)
371         {
372             mincode_tmp[i]=mincode;
373 
374             //console_printf("mincode%d:%x\n",i,mincode_tmp[i]);
375             mincode=(mincode+count[i])<<1;
376         }
377 
378         if (type == LENS)
379         {
380             /*
381             //set lbase2 lbase3 lbase4
382             data=offset_tmp[2]+(offset_tmp[3]<<PNG_LBASE3_SHF)+(offset_tmp[4]<<PNG_LBASE4_SHF);
383 
384             drv_gpd_set_lbase_g1(data);
385 
386 
387             //set lbase5 lbase6 lbase7
388             data=offset_tmp[5]+(offset_tmp[6]<<PNG_LBASE6_SHF)+(offset_tmp[7]<<PNG_LBASE7_SHF);
389             drv_gpd_set_lbase_g2(data);
390 
391 
392             //set lbase8 lbase9 lbase10
393             data=offset_tmp[8]+(offset_tmp[9]<<PNG_LBASE9_SHF)+(offset_tmp[10]<<PNG_LBASE10_SHF);
394             drv_gpd_set_lbase_g3(data);
395 
396 
397             //set lbase11 lbase12 lbase13
398             data=offset_tmp[11]+(offset_tmp[12]<<PNG_LBASE12_SHF)+(offset_tmp[13]<<PNG_LBASE13_SHF);
399             drv_gpd_set_lbase_g4(data);
400 
401 
402             //set lbase14 lbase15
403 
404             data=offset_tmp[14]+(offset_tmp[15]<<PNG_LBASE15_SHF);
405             drv_gpd_set_lbase_g5(data);
406 
407             //console_printf("set lit base\n");
408             */
409             drv_gpd_set_lbase(offset_tmp);
410 
411 
412             for (i=1;i<16;i++)
413             {
414                 if (count[i]!=0)
415             	    mincode_valid |= (1<<(i-1));
416             }
417 
418 
419             drv_gpd_set_lmincode_valid(mincode_valid);
420 
421 
422             //console_printf("set lit mincode_valid\n");
423 
424             drv_gpd_set_dynamic_ldata(offset_tmp[max+1], work);
425 
426 
427             //console_printf("set lit data\n");
428 
429             /*
430             //set mincode1_lit~mincode7
431             data=((mincode_tmp[1]&0x1)<<PNG_MINCODE1_SHF)+((mincode_tmp[2]&0x3)<<PNG_MINCODE2_SHF)+
432               ((mincode_tmp[3]&0x7)<<PNG_MINCODE3_SHF)+((mincode_tmp[4]&0xf)<<PNG_MINCODE4_SHF)+
433               ((mincode_tmp[5]&0x1f)<<PNG_MINCODE5_SHF)+((mincode_tmp[6]&0x3f)<<PNG_MINCODE6_SHF)+
434               ((mincode_tmp[7]&0x7f)<<PNG_MINCODE7_SHF);
435 
436             drv_gpd_set_lmincode_g1(data);
437 
438 
439             //set mincode8~mincode10
440             data=(mincode_tmp[8]&0xff)+((mincode_tmp[9]&0x1ff)<<PNG_MINCODE9_SHF)+
441             ((mincode_tmp[10]&0x3ff)<<PNG_MINCODE10_SHF);
442             drv_gpd_set_lmincode_g2(data);
443 
444 
445 
446             //set mincode11 mincode12
447             data=(mincode_tmp[11]&0x7ff)+((mincode_tmp[12]&0xfff)<<PNG_MINCODE12_SHF);
448             drv_gpd_set_lmincode_g3(data);
449 
450 
451 
452             //set mincode13 mincode14
453             data=(mincode_tmp[13]&0x1fff)+((mincode_tmp[14]&0x3fff)<<PNG_MINCODE14_SHF);
454             drv_gpd_set_lmincode_g4(data);
455 
456             //set mincode15
457 
458             data=mincode_tmp[15]&0x7fff;
459             drv_gpd_set_lmincode_g5(data);
460 
461             //console_printf("set lit mincode\n");
462             */
463             drv_gpd_set_lmincode(mincode_tmp);
464 
465         }
466         else if(type==DISTS)
467         {
468             /*
469             //set dbase2 dbase3
470             //console_printf("dbase2:%x\n",offset_tmp[2]);
471             //console_printf("dbase3:%x\n",offset_tmp[3]);
472 
473             data=(offset_tmp[2]<<PNG_DBASE2_SHF)+(offset_tmp[3]<<PNG_DBASE3_SHF);
474             drv_gpd_set_dbase_g1(data);
475 
476 
477 
478             //set dbase4 ~ dbase9
479             data=offset_tmp[4]+(offset_tmp[5]<<PNG_DBASE5_SHF)+(offset_tmp[6]<<PNG_DBASE6_SHF)+
480             (offset_tmp[7]<<PNG_DBASE7_SHF)+(offset_tmp[8]<<PNG_DBASE8_SHF)+(offset_tmp[9]<<PNG_DBASE9_SHF);
481 
482             drv_gpd_set_dbase_g2(data);
483 
484 
485 
486             //set dbase10~dbase15
487             data=offset_tmp[10]+(offset_tmp[11]<<PNG_DBASE11_SHF)+(offset_tmp[12]<<PNG_DBASE12_SHF)+
488             (offset_tmp[13]<<PNG_DBASE13_SHF)+(offset_tmp[14]<<PNG_DBASE14_SHF)+(offset_tmp[15]<<PNG_DBASE15_SHF);
489 
490             drv_gpd_set_dbase_g3(data);
491 
492             //console_printf("set dist base\n");
493             */
494 
495             drv_gpd_set_dbase(offset_tmp);
496 
497             for (i=1;i<16;i++)
498             {
499                 if (count[i]!=0)
500                     mincode_valid |= (1<<(i-1));
501             }
502 
503 
504             drv_gpd_set_dmincode_valid(mincode_valid);
505 
506             //console_printf("set dist valid\n");
507 
508 
509             drv_gpd_set_dynamic_ddata(offset_tmp[max+1],work);
510 
511 
512 
513             //console_printf("set dist data\n");
514 
515             /*
516             //set mincode1_dist ~ mincode5
517             data=((mincode_tmp[1]&0x1)<<PNG2_MINCODE1_SHF)+
518             ((mincode_tmp[2]&0x3)<<PNG2_MINCODE2_SHF)+((mincode_tmp[3]&0x7)<<PNG2_MINCODE3_SHF)+
519             ((mincode_tmp[4]&0xf)<<PNG2_MINCODE4_SHF)+((mincode_tmp[5]&0x1f)<<PNG2_MINCODE5_SHF);
520 
521             drv_gpd_set_dmincode_g1(data);
522 
523 
524 
525             //set mincode6~mincode9
526 
527             data=(mincode_tmp[6]&0x3f)+((mincode_tmp[7]&0x7f)<<PNG2_MINCODE7_SHF)+
528             ((mincode_tmp[8]&0xff)<<PNG2_MINCODE8_SHF)+((mincode_tmp[9]&0x1ff)<<PNG2_MINCODE9_SHF);
529 
530             drv_gpd_set_dmincode_g2(data);
531 
532             //set mincode10~mincode11
533             data=(mincode_tmp[10]&0x3ff)+((mincode_tmp[11]&0x7ff)<<PNG2_MINCODE11_SHF);
534             drv_gpd_set_dmincode_g3(data);
535 
536 
537 
538             //set mincode12~13
539             data=(mincode_tmp[12]&0xfff)+((mincode_tmp[13]&0x1fff)<<PNG2_MINCODE13_SHF);
540             drv_gpd_set_dmincode_g4(data);
541 
542 
543 
544             //set mincode14~15
545             data=(mincode_tmp[14]&0x3fff)+((mincode_tmp[15]&0x7fff)<<PNG2_MINCODE15_SHF);
546             //set_nop_cmd(30);
547             drv_gpd_set_dmincode_g5(data);
548 
549             //console_printf("set dist mincode\n");
550             */
551             drv_gpd_set_dmincode(mincode_tmp);
552         }
553     }
554 
555     return 0;
556 }
557