OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.application/] [h.263_encoder_main/] [1.0/] [src/] [vlc.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*---------------------------------------------------------------------------
2
 *
3
 *  Course:   Systems Design II
4
  *
5
 *  Module:   vlc.c
6
 *
7
 *  Purpose:  To encapsulate VLC coding tables for H.263 encoder
8
 *
9
 *  Notes:    Module required for slave CPUs. Codewords are taken from
10
 *            Telenor's H.263 encoder implementation.
11
 *
12
 *  Author:   Olli Lehtoranta and Tero Kangas
13
 *
14
 *  History:  23/09/2002: + Original version commented and ready
15
 *            31/10/2002: + TK: modified for Digital Systems Design II course
16
 **---------------------------------------------------------------------------
17
 */
18
 
19
#include "headers.h"
20
 
21
 
22
/*
23
* Intra picture coded block pattern for chrominance and macroblock mode
24
*
25
* Table element format = { Bit pattern value, codeword length }
26
*
27
* indexing with [mode][chroma block pattern]
28
*  mode 0 = INTRA
29
*  mode 1 = INTRA_Q
30
*
31
*/
32
const structVLC mcbpcIntraTable[2][4] =
33
{
34
   { {1,1},{1,3},{2,3},{3,3} },
35
   { {1,4},{1,6},{2,6},{6,6} }
36
};
37
 
38
 
39
/*
40
*
41
* Luminance block pattern table for Intra/Inter pictures
42
*
43
*/
44
const structVLC cbpyTable[16] =
45
{
46
   {3,4}, {5,5}, {4,5}, {9,4}, {3,5}, {7,4}, {2,6}, {11,4},
47
   {2,5}, {3,6}, {5,4}, {10,4}, {4,4}, {8,4}, {6,4}, {3,2}
48
};
49
 
50
 
51
/*
52
*
53
* H.263 DCT encoding tables (Taken from Telenor's implementation)
54
*/
55
static const structVLC coeffTab0[24] =
56
{
57
   /* run = 0 */
58
   {0x02, 2}, {0x0f, 4}, {0x15, 6}, {0x17, 7},
59
   {0x1f, 8}, {0x25, 9}, {0x24, 9}, {0x21,10},
60
   {0x20,10}, {0x07,11}, {0x06,11}, {0x20,11},
61
   /* run = 1 */
62
   {0x06, 3}, {0x14, 6}, {0x1e, 8}, {0x0f,10},
63
   {0x21,11}, {0x50,12}, {0x00, 0}, {0x00, 0},
64
   {0x00, 0}, {0x00, 0}, {0x00, 0}, {0x00, 0}
65
};
66
 
67
static const structVLC coeffTab1[100] =
68
{
69
   /* run = 2 */
70
   {0x0e, 4}, {0x1d, 8}, {0x0e,10}, {0x51,12},
71
   /* run = 3 */
72
   {0x0d, 5}, {0x23, 9}, {0x0d,10}, {0x00, 0},
73
   /* run = 4-26 */
74
   {0x0c, 5}, {0x22, 9}, {0x52,12}, {0x00, 0},
75
   {0x0b, 5}, {0x0c,10}, {0x53,12}, {0x00, 0},
76
   {0x13, 6}, {0x0b,10}, {0x54,12}, {0x00, 0},
77
   {0x12, 6}, {0x0a,10}, {0x00, 0}, {0x00, 0},
78
   {0x11, 6}, {0x09,10}, {0x00, 0}, {0x00, 0},
79
   {0x10, 6}, {0x08,10}, {0x00, 0}, {0x00, 0},
80
   {0x16, 7}, {0x55,12}, {0x00, 0}, {0x00, 0},
81
   {0x15, 7}, {0x00, 0}, {0x00, 0}, {0x00, 0},
82
   {0x14, 7}, {0x00, 0}, {0x00, 0}, {0x00, 0},
83
   {0x1c, 8}, {0x00, 0}, {0x00, 0}, {0x00, 0},
84
   {0x1b, 8}, {0x00, 0}, {0x00, 0}, {0x00, 0},
85
   {0x21, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
86
   {0x20, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
87
   {0x1f, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
88
   {0x1e, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
89
   {0x1d, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
90
   {0x1c, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
91
   {0x1b, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
92
   {0x1a, 9}, {0x00, 0}, {0x00, 0}, {0x00, 0},
93
   {0x22,11}, {0x00, 0}, {0x00, 0}, {0x00, 0},
94
   {0x23,11}, {0x00, 0}, {0x00, 0}, {0x00, 0},
95
   {0x56,12}, {0x00, 0}, {0x00, 0}, {0x00, 0},
96
   {0x57,12}, {0x00, 0}, {0x00, 0}, {0x00, 0}
97
};
98
/*
99
* Auxilary table to store limit information for coeffTab1
100
*/
101
static const uint16 coeffTab1Limits[25] =
102
{
103
   4,3,3,3,3,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
104
};
105
 
106
static const structVLC coeffTab2[6] =
107
{
108
   /* run = 0 */
109
   {0x07, 4}, {0x19, 9}, {0x05,11},
110
   /* run = 1 */
111
   {0x0f, 6}, {0x04,11}, {0x00, 0}
112
};
113
 
114
static const structVLC coeffTab3[40] =
115
{
116
   {0x0e, 6}, {0x0d, 6}, {0x0c, 6},
117
   {0x13, 7}, {0x12, 7}, {0x11, 7}, {0x10, 7},
118
   {0x1a, 8}, {0x19, 8}, {0x18, 8}, {0x17, 8},
119
   {0x16, 8}, {0x15, 8}, {0x14, 8}, {0x13, 8},
120
   {0x18, 9}, {0x17, 9}, {0x16, 9}, {0x15, 9},
121
   {0x14, 9}, {0x13, 9}, {0x12, 9}, {0x11, 9},
122
   {0x07,10}, {0x06,10}, {0x05,10}, {0x04,10},
123
   {0x24,11}, {0x25,11}, {0x26,11}, {0x27,11},
124
   {0x58,12}, {0x59,12}, {0x5a,12}, {0x5b,12},
125
   {0x5c,12}, {0x5d,12}, {0x5e,12}, {0x5f,12},
126
   {0x00, 0}
127
};
128
 
129
/*---------------------------------------------------------------------------
130
*
131
*  Function: vlcCodeCoefficient
132
*
133
*  Input:    last = indication, if this is the last coefficient to be coded
134
*            run = number of zeros after previously coded coefficient
135
*            level = absolute value of coefficient to be coded
136
*            sign = positive / negative indication
137
*            stream = target bit buffer
138
*
139
*  Return:   V_TRUE if LAST,RUN,LEVEL combination can be encoded with
140
*            help of standard specified codes (optimal)
141
*
142
*  Purpose:  To provide look-up table based encoding for LAST,RUN,LEVEL
143
*            information
144
*
145
**---------------------------------------------------------------------------
146
*/
147
vbool vlcCodeCoefficient( const sint32 last,
148
                          const sint32 run,
149
                          const sint32 level,
150
                          const sint32 sign,
151
                          BitStreamType * const stream )
152
{
153
   sint32 index = 0;
154
   sint32 value;
155
   sint32 bitCount;
156
 
157
   if( !last && ( run < 2 ) && ( level < 13 ) )
158
   {
159
      if( run == 0 )
160
      {
161
         index = level - 1;
162
      }
163
      else if( run == 1 )
164
      {
165
         if( level > 6 )
166
         {
167
            return V_FALSE;
168
         }
169
         index = 12 + level - 1;
170
      }
171
      bitCount = coeffTab0[index].bitCount + 1;
172
      value = ( ( coeffTab0[index].bitPattern << 1 ) | sign);
173
      bitstreamPut(bitCount,value,stream);
174
      return V_TRUE;
175
   }
176
   else if( !last && ( run >= 2 ) && ( run < 27 ) )
177
   {
178
      if( level > coeffTab1Limits[ run - 2 ] )
179
      {
180
         return V_FALSE;
181
      }
182
      index = ( ( run - 2 ) * 4 ) + level - 1;
183
      bitCount = coeffTab1[index].bitCount + 1;
184
      value = ( (coeffTab1[index].bitPattern << 1 ) | sign);
185
      bitstreamPut(bitCount,value,stream);
186
      return V_TRUE;
187
   }
188
   else if( last && ( run < 2 ) && ( level < 4 ) )
189
   {
190
      if( run == 0 )
191
      {
192
         index = level - 1;
193
      }
194
      else if( run == 1 )
195
      {
196
         if( level > 2 )
197
         {
198
            return V_FALSE;
199
         }
200
         index = 3 + level - 1;
201
      }
202
      bitCount = coeffTab2[index].bitCount + 1;
203
      value = ( ( coeffTab2[index].bitPattern << 1 ) | sign );
204
      bitstreamPut(bitCount,value,stream);
205
      return V_TRUE;
206
   }
207
   else if( last && ( run >= 2 ) && ( run < 41 ) && ( level == 1 ) )
208
   {
209
      bitCount = coeffTab3[run-2].bitCount + 1;
210
      value = ( ( coeffTab3[run-2].bitPattern << 1 ) | sign);
211
      bitstreamPut(bitCount,value,stream);
212
      return V_TRUE;
213
   }
214
 
215
   return V_FALSE;
216
}
217
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.