~ubuntu-branches/ubuntu/hoary/kdemultimedia/hoary

« back to all changes in this revision

Viewing changes to mpeglib/lib/splay/huffmanlookup.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Martin Schulze
  • Date: 2003-01-22 15:00:51 UTC
  • Revision ID: james.westby@ubuntu.com-20030122150051-uihwkdoxf15mi1tn
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    /*
 
2
 
 
3
    Copyright (C) 2000 Stefan Westerfeld
 
4
                       stefan@space.twc.de
 
5
 
 
6
    This library is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU Library General Public
 
8
    License as published by the Free Software Foundation; either
 
9
    version 2 of the License, or (at your option) any later version.
 
10
  
 
11
    This library is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
    Library General Public License for more details.
 
15
   
 
16
    You should have received a copy of the GNU Library General Public License
 
17
    along with this library; see the file COPYING.LIB.  If not, write to
 
18
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
19
    Boston, MA 02111-1307, USA.
 
20
 
 
21
    */
 
22
 
 
23
#include "huffmanlookup.h"
 
24
#include <assert.h>
 
25
 
 
26
 
 
27
 
 
28
 
 
29
struct HuffmanLookup::decodeData HuffmanLookup::qdecode[32][256];
 
30
/* for initialization */
 
31
static HuffmanLookup l;
 
32
 
 
33
HuffmanLookup::HuffmanLookup()
 
34
{
 
35
        int table,p,x,y;
 
36
 
 
37
        for(table = 0; table < 32; table++)
 
38
        {
 
39
                // 8 bits pattern
 
40
                for(p = 0; p < 256; p++)
 
41
                {
 
42
                        bits = 24;
 
43
                        pattern = (p << 16);
 
44
 
 
45
                        huffmandecoder_1(&Mpegtoraw::ht[table], &x,&y);
 
46
 
 
47
                        int used = 24 - bits;
 
48
                        qdecode[table][p].skip = (used <= 8)?used:0;
 
49
                        qdecode[table][p].x = x;
 
50
                        qdecode[table][p].y = y;
 
51
                }
 
52
        }
 
53
}
 
54
 
 
55
int HuffmanLookup::wgetbit()   
 
56
{
 
57
        return (pattern >> --bits) & 1;
 
58
}
 
59
 
 
60
int HuffmanLookup::wgetbits (int b)
 
61
{
 
62
        bits -= b;
 
63
        return (pattern >> bits) & ((1 << b) - 1);
 
64
}
 
65
 
 
66
void HuffmanLookup::huffmandecoder_1(const HUFFMANCODETABLE *h, int *x, int *y)
 
67
{  
 
68
  typedef unsigned int HUFFBITS;
 
69
 
 
70
  HUFFBITS level=(1<<(sizeof(HUFFBITS)*8-1));
 
71
  int point=0;
 
72
  /* Lookup in Huffman table. */
 
73
  for(;;)
 
74
  {
 
75
    if(h->val[point][0]==0)
 
76
    {   /*end of tree*/
 
77
      int xx,yy;
 
78
 
 
79
      xx=h->val[point][1]>>4;
 
80
      yy=h->val[point][1]&0xf;
 
81
 
 
82
      if(h->linbits)
 
83
      {
 
84
        if((h->xlen)==(unsigned)xx)xx+=wgetbits(h->linbits);
 
85
        if(xx)if(wgetbit())xx=-xx;
 
86
        if((h->ylen)==(unsigned)yy)yy+=wgetbits(h->linbits);
 
87
        if(yy)if(wgetbit())yy=-yy;
 
88
      }
 
89
      else
 
90
      {
 
91
        if(xx)if(wgetbit())xx=-xx;
 
92
        if(yy)if(wgetbit())yy=-yy;
 
93
      }
 
94
      *x=xx;*y=yy;
 
95
      break;
 
96
    } 
 
97
 
 
98
    point+=h->val[point][wgetbit()];
 
99
    
 
100
    level>>=1;
 
101
    if(!(level || ((unsigned)point<Mpegtoraw::ht->treelen)))
 
102
    {
 
103
      register int xx,yy;
 
104
 
 
105
      xx=(h->xlen<<1);// set x and y to a medium value as a simple concealment
 
106
      yy=(h->ylen<<1);
 
107
 
 
108
      // h->xlen and h->ylen can't be 1 under tablename 32
 
109
      //      if(xx)
 
110
        if(wgetbit())xx=-xx;
 
111
      //      if(yy)
 
112
        if(wgetbit())yy=-yy;
 
113
 
 
114
      *x=xx;*y=yy;
 
115
      break;
 
116
    }
 
117
  }
 
118
}
 
119
 
 
120