OpenCores
URL https://opencores.org/ocsvn/mb-jpeg/mb-jpeg/trunk

Subversion Repositories mb-jpeg

[/] [mb-jpeg/] [trunk/] [mb-bmp2jpg/] [huffman.c] - Blame information for rev 66

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 56 quickwayne
/*
2
Only encoder
3
This version works correctly, it is tested with testcase.jpg
4
The translation into real huffman codes works.
5
Changed: If huffman wants to send 0xFFxx (FF in one byte) than there must be 0x00 inserted between FF and xx
6
possible fault in finish send:
7
-must it be filled up with zeros?          YES
8
-must it be filled up to one bye? or 2 byte? --> in this code there is filled up to 2 bytes, but I (joris) thinks this must be filled up to 1 byte.
9
 still dont know
10
- 24-11-05 code clean up
11
- 24-11-05 tables added for color
12
 
13
 
14
 
15
Block numbers:
16
Y = 0
17
cb =1
18
cr= 2
19
*/
20
//---------------------------------------------------------------------------
21
 
22
#include "ejpgl.h"
23
 
24
static unsigned int vlc_remaining;
25
static unsigned char vlc_amount_remaining;
26
static unsigned char dcvalue[4];   // 3 is enough
27
 
28
int vlc_init_start() {
29
 
30
       vlc_remaining=0x00;
31
       vlc_amount_remaining=0x00;
32
        memset(dcvalue, 0, 4);
33
        return 0;
34
 
35
}
36
 
37
int vlc_stop_done() {
38
 
39
       HuffmanEncodeFinishSend();
40
 
41
}
42
 
43
#define vlc_output_byte(c)             put_char(c)
44
 
45
 
46
static unsigned char convertDCMagnitudeCLengthTable[16] = {
47
        0x02, 0x02, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
48
        0x08, 0x09, 0x0a, 0x0b, 0x00, 0x00, 0x00, 0x00
49
};
50
 
51
static unsigned short convertDCMagnitudeCOutTable[16] = {
52
        0x0000, 0x0001, 0x0002, 0x0006, 0x000e, 0x001e, 0x003e, 0x007e,
53
        0x00fe, 0x01fe, 0x03fe, 0x07fe, 0x0000, 0x0000, 0x0000, 0x0000
54
};
55
 
56
void ConvertDCMagnitudeC(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght)
57
{
58
        unsigned char len;
59
 
60 57 quickwayne
/*      if ((magnitude>16) || ((len=convertDCMagnitudeCLengthTable[magnitude])==0)) {
61 56 quickwayne
                printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n");
62 57 quickwayne
                } */
63
 
64 56 quickwayne
        *lenght = len;
65
        *out = convertDCMagnitudeCOutTable[magnitude];
66
 
67
}
68
 
69
static unsigned char convertACMagnitudeCLengthTable[256] = {
70
        0x02, 0x02, 0x03, 0x04, 0x05, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00,    // 00 - 0f
71
        0x00, 0x04, 0x06, 0x08, 0x09, 0x0b, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 10 - 1f
72
        0x00, 0x05, 0x08, 0x0a, 0x0c, 0x0f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 20 - 2f
73
        0x00, 0x05, 0x08, 0x0a, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 30 - 3f
74
        0x00, 0x06, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 40 - 4f
75
        0x00, 0x06, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 50 - 5f
76
        0x00, 0x07, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 60 - 6f
77
        0x00, 0x07, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 70 - 7f
78
        0x00, 0x08, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 80 - 8f
79
        0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 90 - 9f
80
        0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // a0 - af
81
        0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // b0 - bf
82
        0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // c0 - cf
83
        0x00, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // d0 - df
84
        0x00, 0x0e, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // e0 - ef
85
        0x0a, 0x0f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00
86
};
87
 
88
static unsigned short convertACMagnitudeCOutTable[256] = {
89
        0x0000, 0x0001, 0x0004, 0x000a, 0x0018, 0x0019, 0x0038, 0x0078, 0x01f4, 0x03f6, 0x0ff4, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 00 - 0f
90
        0x0000, 0x000b, 0x0039, 0x00f6, 0x01f5, 0x07f6, 0x0ff5, 0xff88, 0xff89, 0xff8a, 0xff8b, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 10 - 1f
91
        0x0000, 0x001a, 0x00f7, 0x03f7, 0x0ff6, 0x7fc2, 0xff8c, 0xff8d, 0xff8e, 0xff8f, 0xff90, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 20 - 2f
92
        0x0000, 0x001b, 0x00f8, 0x03f8, 0x0ff7, 0xff91, 0xff92, 0xff93, 0xff94, 0xff95, 0xff96, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 30 - 3f
93
        0x0000, 0x003a, 0x01f6, 0xff97, 0xff98, 0xff99, 0xff9a, 0xff9b, 0xff9c, 0xff9d, 0xff9e, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 40 - 4f
94
        0x0000, 0x003b, 0x03f9, 0xff9f, 0xffa0, 0xffa1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5, 0xFFA6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 50 - 5f
95
        0x0000, 0x0079, 0x07f7, 0xffa7, 0xffa8, 0xffa9, 0xffaa, 0xffab, 0xFFAc, 0xFFAf, 0xFFAe, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 60 - 6f
96
        0x0000, 0x007a, 0x07f8, 0xffaf, 0xffb0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5, 0xFFB6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 70 - 7f
97
        0x0000, 0x00f9, 0xffb7, 0xFFB8, 0xFFB9, 0xFFBa, 0xFFBb, 0xFFBc, 0xFFBd, 0xFFBe, 0xFFBf, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 80 - 8f
98
        0x0000, 0x01f7, 0xffc0, 0xffc1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0xFFC7, 0xFFC8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 90 - 9f
99
        0x0000, 0x01f8, 0xffc9, 0xFFCa, 0xFFCb, 0xFFCc, 0xFFCd, 0xFFCe, 0xFFCf, 0xFFd0, 0xFFd1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // a0 - af
100
        0x0000, 0x01f9, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7, 0xFFD8, 0xFFD9, 0xFFDa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // b0 - bf
101
        0x0000, 0x01fa, 0xFFDb, 0xFFDc, 0xFFDd, 0xFFDe, 0xFFDf, 0xFFe0, 0xFFe1, 0xFFe2, 0xFFe3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // c0 - cf
102
        0x0000, 0x07f9, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8, 0xFFE9, 0xFFEa, 0xFFEb, 0xFFEc, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // d0 - df
103
        0x0000, 0x3fe0, 0xffed, 0xFFEe, 0xFFEf, 0xFFf0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4, 0xFFF5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // e0 - ef
104
        0x03fa, 0x7fc3, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
105
};
106
 
107
//===========================================================================
108
void ConvertACMagnitudeC(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght)
109
{
110
        unsigned char len;
111
 
112
        len = convertACMagnitudeCLengthTable[magnitude];
113 57 quickwayne
/*      if (!len) {
114 56 quickwayne
                printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n");
115 57 quickwayne
                } */
116 56 quickwayne
        *lenght = len;
117
        *out = convertACMagnitudeCOutTable[magnitude];
118
 
119
}
120
 
121
static unsigned char convertDCMagnitudeYLengthTable[16] = {
122
        0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x04, 0x05,
123
        0x06, 0x07, 0x08, 0x09, 0x00, 0x00, 0x00, 0x00
124
};
125
 
126
static unsigned short convertDCMagnitudeYOutTable[16] = {
127
        0x0000, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x000e, 0x001e,
128
        0x003e, 0x007e, 0x00fe, 0x01fe, 0x0000, 0x0000, 0x0000, 0x0000
129
};
130
 
131
//===========================================================================
132
void ConvertDCMagnitudeY(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght)
133
{
134
        unsigned char len;
135
 
136 57 quickwayne
/*      if ((magnitude>16) || ((len=convertDCMagnitudeYLengthTable[magnitude])==0)) {
137 56 quickwayne
                printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n");
138 57 quickwayne
                } */
139 56 quickwayne
        *lenght = len;
140
        *out = convertDCMagnitudeYOutTable[magnitude];
141
}
142
 
143
static unsigned char convertACMagnitudeYLength[256] = {
144
        0x04, 0x02, 0x02, 0x03, 0x04, 0x05, 0x07, 0x08, 0x0a, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 00 - 0f
145
        0x00, 0x04, 0x05, 0x07, 0x09, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 10 - 1f
146
        0x00, 0x05, 0x08, 0x0a, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 20 - 2f
147
        0x00, 0x06, 0x09, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 30 - 3f
148
        0x00, 0x06, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 40 - 4f
149
        0x00, 0x07, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 50 - 5f
150
        0x00, 0x07, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 60 - 6f
151
        0x00, 0x08, 0x0c, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 70 - 7f
152
        0x00, 0x09, 0x0f, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 80 - 8f
153
        0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // 90 - 9f
154
        0x00, 0x09, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // a0 - af
155
        0x00, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // b0 - bf
156
        0x00, 0x0a, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // c0 - cf
157
        0x00, 0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // d0 - df
158
        0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,    // e0 - ef
159
        0x0b, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00
160
};
161
 
162
static unsigned short convertACMagnitudeYOut[256] = {
163
        0xFFFA, 0xFFF0, 0xFFF1, 0xFFF4, 0xFFFB, 0xFFFA, 0xFFF8, 0xFFF8, 0xFFF6, 0xFF82, 0xFF83, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 00 - 0f
164
        0x0000, 0xFFFC, 0xFFFB, 0xFFF9, 0xFFF6, 0xFFF6, 0xFF84, 0xFF85, 0xFF86, 0xFF87, 0xFF88, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 10 - 1f
165
        0x0000, 0xFFFC, 0xFFF9, 0xFFF7, 0xFFF4, 0xFF89, 0xFF8A, 0xFF8B, 0xFF8C, 0xFF8D, 0xFF8E, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 20 - 2f
166
        0x0000, 0xFFFA, 0xFFF7, 0xFFF5, 0xFF8F, 0xFF90, 0xFF91, 0xFF92, 0xFF93, 0xFF94, 0xFF95, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 30 - 3f
167
        0x0000, 0xFFFB, 0xFFF8, 0xFF96, 0xFF97, 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C, 0xFF9D, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 40 - 4f
168
        0x0000, 0xFFFA, 0xFFF7, 0xFF9E, 0xFF9F, 0xFFA0, 0xFFA1, 0xFFA2, 0xFFA3, 0xFFA4, 0xFFA5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 50 - 5f
169
        0x0000, 0xFFFB, 0xFFF6, 0xFFA6, 0xFFA7, 0xFFA8, 0xFFA9, 0xFFAA, 0xFFAB, 0xFFAC, 0xFFAD, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 60 - 6f
170
        0x0000, 0xFFFA, 0xFFF7, 0xFFAE, 0xFFAF, 0xFFB0, 0xFFB1, 0xFFB2, 0xFFB3, 0xFFB4, 0xFFB5, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 70 - 7f
171
        0x0000, 0xFFF8, 0xFFC0, 0xFFB6, 0xFFB7, 0xFFB8, 0xFFB9, 0xFFBA, 0xFFBB, 0xFFBC, 0xFFBD, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 80 - 8f
172
        0x0000, 0xFFF9, 0xFFBE, 0xFFBF, 0xFFC0, 0xFFC1, 0xFFC2, 0xFFC3, 0xFFC4, 0xFFC5, 0xFFC6, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // 90 - 9f
173
        0x0000, 0xFFFA, 0xFFC7, 0xFFC8, 0xFFC9, 0xFFCA, 0xFFCB, 0xFFCC, 0xFFCD, 0xFFCE, 0xFFCF, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // a0 - af
174
        0x0000, 0xFFF9, 0xFFD0, 0xFFD1, 0xFFD2, 0xFFD3, 0xFFD4, 0xFFD5, 0xFFD6, 0xFFD7, 0xFFD8, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // b0 - bf
175
        0x0000, 0xFFFA, 0xFFD9, 0xFFDA, 0xFFDB, 0xFFDC, 0xFFDD, 0xFFDE, 0xFFDF, 0xFFE0, 0xFFE1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // c0 - cf
176
        0x0000, 0xFFF8, 0xFFE2, 0xFFE3, 0xFFE4, 0xFFE5, 0xFFE6, 0xFFE7, 0xFFE8, 0xFFE9, 0xFFEA, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // d0 - df
177
        0x0000, 0xFFEB, 0xFFEC, 0xFFED, 0xFFEE, 0xFFEF, 0xFFF0, 0xFFF1, 0xFFF2, 0xFFF3, 0xFFF4, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,    // e0 - ef
178
        0xFFF9, 0xFFF5, 0xFFF6, 0xFFF7, 0xFFF8, 0xFFF9, 0xFFFA, 0xFFFB, 0xFFFC, 0xFFFD, 0xFFFE, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000
179
};
180
 
181
//===========================================================================
182
void ConvertACMagnitudeY(unsigned char magnitude,unsigned short int *out, unsigned short int *lenght)
183
{
184
        unsigned char len;
185
 
186
        len = convertACMagnitudeYLength[magnitude];
187 57 quickwayne
/*      if (!len) {
188 56 quickwayne
#ifndef __MICROBLAZE
189
        printf("WAARDE STAAT NIET IN TABEL!!!!!!!!!!!!!!!!!!!!\n");
190
#endif
191 57 quickwayne
                } */
192 56 quickwayne
        *lenght = len;
193
        *out = convertACMagnitudeYOut[magnitude];
194
 
195
}
196
 
197
char Extend (char additional, unsigned char magnitude)
198
{
199
        int vt= 1 << (magnitude-1);
200
        if ( additional < vt ) return (additional + (-1 << magnitude) + 1);
201
        else return additional;
202
}
203
 
204
void ReverseExtend (char value, unsigned char *magnitude, unsigned char *bits)
205
{
206
 //     printf("reverseextend value= %d\n",*magnitude);
207
        if (value >=0)
208
        {
209
                *bits=value;
210
        }
211
        else
212
        {
213
                value=-value;
214
                *bits=~value;
215
        }
216
        *magnitude=0;
217
        while (value !=0)
218
        {
219
                value>>=1;
220
                ++*magnitude;
221
        }
222
 //     printf("reverseextend magnitude= %d bits= %d",magnitude,bits);
223
        return;
224
}
225
 
226
void WriteRawBits16(unsigned char amount_bits, unsigned int bits)     //*remaining needs bo be more than 8 bits because 8 bits could be added and ther ecould already be up ot 7 bits in *remaining
227
// this function collects bits to send
228
// if there less than 16 bits collected, nothing is send and these bits are stored in *remaining. In *amount_remaining there is stated how much bits are stored in *remaining
229
// if more than 16 bits are collected, 16 bits are send and the remaining bits are stored again
230
{
231
        unsigned short int send;
232
        unsigned int mask;
233
        unsigned char send2;
234
        int count;
235
        mask=0x00;                                                              //init mask
236
        vlc_remaining=(vlc_remaining<<amount_bits);                                   //shift to make place for the new bits
237
        for (count=amount_bits; count>0; count--) mask=(mask<<1)|0x01;          //create mask for adding bit
238
        vlc_remaining=vlc_remaining | (bits&mask);                                    //add bits
239
        vlc_amount_remaining=vlc_amount_remaining + amount_bits;                      //change *amount_remaining to the correct new value
240
        if (vlc_amount_remaining >= 16)                                            //are there more than 16 bits in buffer, send 16 bits
241
        {
242 57 quickwayne
/* #ifndef __MICROBLAZE
243 56 quickwayne
if (vlc_amount_remaining >= 32 ) printf("ERROR, more bits to send %d",vlc_amount_remaining);
244 57 quickwayne
#endif */
245 56 quickwayne
                send=vlc_remaining>>(vlc_amount_remaining-16);                        //this value can be send/stored (in art this can be dony by selecting bits)
246
                send2=(send & 0xFF00) >>8;
247
                  vlc_output_byte(send2);
248
                if (send2==0xFF)
249
                {
250
                        send2=0x00;
251
                          vlc_output_byte(send2);
252
                }
253
                send2=send & 0xFF;
254
                  vlc_output_byte(send2);
255
                if (send2==0xFF)
256
                {
257
                        send2=0x00;
258
                           vlc_output_byte(send2);
259
                }
260
                vlc_amount_remaining=vlc_amount_remaining-16;                         //descrease by 16 because these are send
261
        }
262
        return;
263
}
264
 
265
void HuffmanEncodeFinishSend()
266
// There are still some bits left to send at the end of the 8x8 matrix (or maybe the file),
267
// the remaining bits are filled up with ones and send
268
// possible fault: -must it be filled up with ones?
269
{
270
        unsigned short int send;
271
        unsigned int mask;
272
        int  count;
273
        mask=0x00;                                                              //init mask
274
        if (vlc_amount_remaining >= 8)                                             //2 bytes to send, send first byte
275
        {
276
                send=vlc_remaining>>(vlc_amount_remaining-8);                         //shift so that first byte is ready to send
277
                  vlc_output_byte(send&0xff);
278
                if (send==0xFF)                                                 //is this still needed????
279
                {
280
                        send=0x00;
281
                           vlc_output_byte(send&0xff);
282
                }
283
                vlc_amount_remaining=vlc_amount_remaining -8;                         // lower the value to the amount of bits that still needs to be send
284
        }
285
        if (vlc_amount_remaining >= 0)                                             //there is a last byte to send
286
        {
287
                send=vlc_remaining<<(8-vlc_amount_remaining);                         //shift the last bits to send to the front of the byte
288
                mask=0x00;                                                      //init mask
289
                for (count=(8-vlc_amount_remaining); count>0; count--) mask=(mask<<1)|0x01; //create mask to fill byte up with ones
290
                send=send | mask;                                               //add the ones to the byte
291
                vlc_output_byte(send&0xff);
292
                vlc_amount_remaining=0x00;                                         //is this needed?
293
        }
294
        return;
295
}
296
 
297
void HuffmanEncodeUsingDCTable(unsigned char magnitude)
298
// Translate magnitude into needed data (from table) and send it
299
{
300
        unsigned char send;
301
        unsigned short int huffmancode, huffmanlengt;
302
        ConvertDCMagnitudeY(magnitude, &huffmancode, &huffmanlengt);
303
        WriteRawBits16(huffmanlengt,huffmancode);
304
        //printf("Write DC magnitude= %2x \n",magnitude);
305
        //WriteRawBits16(0x08,magnitude,remaining,amount_remaining, file);
306
        return;
307
}
308
 
309
void HuffmanEncodeUsingACTable(unsigned char mag)
310
// Translate magnitude into needed data (from table) and send it
311
{
312
        unsigned char send;
313
        unsigned short int huffmancode, huffmanlengt;
314
        ConvertACMagnitudeY(mag, &huffmancode, &huffmanlengt);
315
        WriteRawBits16(huffmanlengt,huffmancode);
316
        return;
317
}
318
 
319
char EncodeDataUnit(char dataunit[64], unsigned int color)
320
{
321
        char difference;
322
        unsigned char magnitude,zerorun,ii,ert;
323
        unsigned int bits;
324
        unsigned char bit_char;
325
         char last_dc_value;
326
                                         //init
327
  //    PrintMatrix(dataunit) ;
328
        last_dc_value = dcvalue[color];
329
        difference = dataunit[0] - last_dc_value;
330
        last_dc_value=dataunit[0];
331
        ReverseExtend(difference, &magnitude,&bit_char);
332
        bits = bit_char;
333
        HuffmanEncodeUsingDCTable(magnitude);
334
        WriteRawBits16(magnitude,bits);
335
        zerorun=0;
336
        ii=1;
337
        while ( ii < 64 )
338
        {
339
                if (dataunit[ii] != 0 )
340
                {
341
                        while ( zerorun >= 16 )
342
                        {
343
                                HuffmanEncodeUsingACTable(0xF0);
344
                                zerorun=zerorun-16;
345
                            //    printf("16 zeros:  %d\n",zerorun);
346
                        }
347
                        ReverseExtend(dataunit[ii],&magnitude,&bit_char);
348
                        bits=bit_char;
349
                        ert= ((int)zerorun *16);                                     //ERROR !!!!!!!!!!!
350
                        ert=ert + magnitude;
351
                        HuffmanEncodeUsingACTable(ert);
352
                        WriteRawBits16(magnitude,bits);
353
                        zerorun=0;
354
                }
355
                else zerorun=zerorun+1;
356
                ii++;
357
        }
358
        if ( zerorun != 0 )
359
        {
360
                HuffmanEncodeUsingACTable(0x00);
361
        }
362
        dcvalue[color] = last_dc_value;
363
        return 0;
364
}
365
 

powered by: WebSVN 2.1.0

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