Below with reference to accompanying drawings most preferred embodiment of the present invention is illustrated.
Variable-length coding method of the present invention and device thereof are set based on a standard huffman coding.The one huffman coding tree with n symbol comprises (2n-1) individual node.Promptly be that this (2n-1) individual node comprises that n has the termination node of symbol and (n-1) the individual connected node that does not have symbol.Standard huffman coding tree has a structure, and promptly all termination nodes are positioned at the left side of all connected node of each layer.As everyone knows, all huffman coding trees can convert standard huffman coding tree to and needn't increase average code length.
Fig. 8 has represented a structure corresponding to the standard huffman coding tree of [a, b, c, d, e, f, g, h].8 symbols are positioned at the left side of all connected nodes.Each symbol has a coding bit string from root to the termination node, and this bit string promptly is its code word.For example, symbol [c] has one three bit word [101] and symbol [f] has a five-bit code word [11110].Be that each symbol has a variable length code word according to the definition in the huffman coding tree of its probability of happening.
Fig. 1 is a Variable Length Code manipulated or operated apparatus of expression one embodiment of the invention.
Contrast Fig. 1, Variable Length Code device comprise a buffer unit 100, code book memory 200, a variable length detection part 300 and package parts 400.
Buffer unit 100 is stored provisionally from the symbol of external series or parallel input, and the responsive control signal (not shown in figure 1) is to code book memory 200 output symbols.
Code book memory 200 has the variable length code word of the variable length code word of maximum length according to symbol or the storage of symbol corresponding address.So, when length is input to code book memory 200 less than the specified symbol [X] of the variable length code word VLW (X) of its maximum length by buffer unit 100, code book memory 200 produces one and comprises the storage code word M (X) of variable length code word VLW (X) and filler thereof, and should store code word M (X) and offer variable length detection part 300 and package parts 400 respectively.
For example, when the maximum length of variable length code word is that K position (K be 1 or bigger integer) and symbol [X] are when being input in the code book memory 200, code book memory 200 produces the character string storage code word M (X) of K positions, and should store code word M (X) and output to variable length detection part 300 and package parts 400 respectively.
Variable length detection part 300 detects the length of variable length code word VLW (X) in the storage code word M (X) that code book memory 200 is provided according to standard huffman coding tree shown in Figure 8.
Variable length detection part 300 at first detects the number that huffman coding is set termination node in each layer, to detect the length of variable length code word VLW (X).Variable length detection part 300 calculates according to the number and the storage code word M (X) of the termination node of each layer then, and shown in hereinafter, thereby generation is corresponding to a length value L EG (X) of variable length code word VLW (X) length.Variable length detection part 300 offers package parts 400 with this length value L EG (X) again.
Variable length detection part 300 can also comprise that as shown in Figure 1 Boundary Detection parts 500 and a code length detection part 600 produce length value L EG (X).
Boundary Detection parts 500 are selected 1 among everybody to the K position successively at storage code word M (X), in order to produce respectively K selected code word BW (X) m=1, BW (X) m=2 ..., BW (X) m=K.And, Boundary Detection parts 500 detect corresponding each selected code word BW (X) m=1,2 ..., the number of the termination node in the level of K.Then Boundary Detection parts 500 more selected code word BW (X) m=1, BW (X) m=2 ..., BW (X) m=K and the number of termination node in the layer separately, with the border of variable length code word VLW (X) among the detection of stored code word M (X), and produce the result of boundary value MSB (X) m as Boundary Detection with filler.
Boundary value MSB (X) m that code length detection part 600 provides according to Boundary Detection parts 500 produces the length value L EG (X) of variable length code word VLW (X), and this length value L EG (X) is offered package parts 400.
Fig. 2 has represented the circuit diagram of an example of the described Boundary Detection parts 500 of Fig. 1 among the present invention.
With reference to Fig. 2, in order to produce boundary value MSB (X) m, Boundary Detection parts 500 comprise a coding alternative pack 510, level detection part 520,530 and boundary value production parts 540.
Everybody of the storage code word M (X) that coding alternative pack 510 provides from the code book memory 1 of selection produce successively to the K position selected code word BW (X) 1, BW (X) 2 ..., BW (X) K, and with selected code word BW (X) 1, BW (X) 2 ..., BW (X) K offers level detection part 520 and calculating unit 530 respectively.
When a selected code word BW (X) m from coding alternative pack 510 when being input to level detection part 520, level detection part 520 detects the m layer of selected code word BW (X) m in the huffman coding tree, this is defined by the position of selected code word in the huffman coding tree, and detects the sum of the termination node from root to the m layer.Then, level detection part 520 produces the level code word NW (X) corresponding to the sum of m layer and the termination node from root to the m layer.In order to produce level code word NW (X), level detection part 520 is given weights according to level for the numeral of the termination node of each layer, and the weight number of the termination node of each layer is all added up mutually.
So, be following form corresponding to summation WS (X) m of the weight number of the defined m layer of selected code word BW (X):
Wherein j refers to the one deck in the huffman coding tree, and Lj is a numeral of the termination node of j layer.
Level detection part 520 converts summation WS (X) m to binary digit and produces level code word NW (X) m and level code word NW (X) m is offered calculating unit 530.
Calculating unit 530 from selected code word BW (X) 1, BW (X) 2 ..., deduct among BW (X) K level code word NW (X) 1, NW (X) 2 ..., NW (X) K produce subtraction result value LD (X) 1, LD (X) 2 ..., LD (X) K, each value be used to refer to selected code word BW (X) m=1,2 ..., whether K only be made up of variable length code word VLW (X).
The circuit diagram of an example of the calculating unit 530 that Fig. 3 describes among Fig. 2 of the present invention for expression.
With reference to Fig. 3, the maximum length of variable length code word is the K position, and calculating unit 530 also comprises K subtracter 531 to 537.
Selected code word BW (X) m that m subtracter of calculating unit 530 deducts the m position with level code word NW (X) m of m layer produce a subtraction result value LD (X) m as value LD (X) m=1,2 of subtraction result ..., one of K.Then calculating unit 530 with this subtraction result value LD (X) m=1,2 ..., K offers boundary value production part 540.
Boundary value generation value parts 540 from subtraction result value LD (X) m=1,2 ..., detect among the K highest significant position MSB (X) m=1,2 ..., K, and with detected highest significant position MSB (X)=1,2 ..., K outputs to code length detection part 600 as boundary value MSB (X) m on the border of variable length code word VLW (X) and filler among the indication storage code word M (X).
Referring again to Fig. 1, code length detection part 600 produces length value L EG (X) according to boundary value MSB (X) m on the border of variable length code word VLW (X) and filler among the indication storage code word M (X).
Fig. 4 is the circuit diagram of expression according to an example of the code length detection part 600 described in Fig. 1 of the present invention.
With reference to Fig. 4, code length detection part 600 comprises that K " exclusive OR " door produces length value L EG (X).
First " exclusive OR " goalkeeper MSB (X) 1 and 0 gets " exclusive OR ", and when n is an integer 2 during to K, n " exclusive OR " goalkeeper MSB (X) n-1 and MSB (X) n get " exclusive OR ".
And the logical value that each produced of K " exclusive OR " door is used as the length value L EG (X) that is included in the variable length code word VLW (X) among the storage code word M (X) and outputs to package parts 400.
In Fig. 1, package parts 400 are exported storage code word M (X) selectively according to length value L EG (X), so 400 of package parts are exported this variable length code word VLW (X) and do not exported its filler.
Fig. 5 is the circuit diagram of an example of package parts 400 among expression the present invention.
With reference to Fig. 5, package parts 400 comprise an input block 410, add up parts 420 and an output block 430.
Input block 410 comprises an one-level barrel shifter (first barrel shifter) 411 and one-level latchs (first latch) 412.
One-level barrel shifter 411 moves in the character string from the 2K position of one-level latch 412 and code book memory 200 inputs, produce a K position one-level window bit string B1 according to length value L EG (X), and with the parallel one-level latch 412 that outputs to of this K position one-level window bit string B1.
One-level latch 412 is stored this K position one-level window bit string B1 and will be had the one-level of identical place value to latch bit string W1 with one-level window bit string B1 and outputs to one-level barrel shifter 411 and output block 430 respectively.Therefore, as shown previously, the storage code word M (X) that the one-level of K position latchs bit string W1 and K position is walked abreast is input to one-level barrel shifter 411.
The parts 420 that add up comprise an adder 421, secondary latch 422, subtracter 423 and three grades of latchs 424.
Adder 421 is carried out mould K addition with the length value L EG (X) of variable length code word VLW (X) and the latched value L1 of secondary latch 422, and the result of addition is offered secondary latch 422.In addition, if the value of addition during greater than K, adder 421 will produce a carry signal.This carry signal will output to the outside and only be stored in the level Four latch 432 of output block 430 with everybody of the variable length code word VLW (X) of announcement, as following to discuss.
The value of the addition that secondary latch 422 storage adders transmit, and it is outputed to subtracter 423 and adder 421 respectively.
Subtracter 423 subtracts each other the value of K and addition gained, thereby produces a subtraction value S0.And the poor S0 that subtracter 423 will subtract each other gained offers three grades of latchs 424.
Then, three grades of latchs 424 will subtract each other gained difference S0 and output to output block 430 as control signal.
Output block 430 comprises a secondary barrel shifter 431 and a level Four latch 432.
Move on the bit string of the character string of secondary barrel shifter 431 2K position of one-level latch 412 in from input block 410 and storage code word M (X), responsive control signal produces the secondary window bit string of a K position.
A best tool 431 of level Four latch provides the input sequence of this secondary window bit string B2 for secondary window bit string B2.Level Four latch 431 has the memory space with the identical size of maximum length K of variable length code word.
The operation of variable-length coding method of the present invention and device thereof will be described in detail below.
In data source the symbol that might exist be followed successively by 1510,1310,1210,1110,510,210,110,110 for [a, b, c, d, e, f, g, h] and the recurrent number of times of each symbol, as shown in Figure 8, according to they recurrent number of times, each variable length code word VLW (X) is distributed to 8 symbols.The code value of the variable length code word of 8 symbols is distributed in table 1 expression according to standard huffman coding tree.1 variable length code word is distributed to the symbol [a] of normal generation, and 6 variable length code word is distributed to the symbol [g and h] of minimum generation.
Table 1
Symbol [X] | Variable length code word VLW (X) |
a | 0 |
b | 100 |
c | 101 |
d | 110 |
e | 1110 |
f | 11110 |
g | 111110 |
h | 111111 |
Represented as following table 2, be stored in code book memory 200 according to symbol variable length code word VLW (X) as its address.When variable length code word VLW (X) stored code book memory 200 into, the MSB of variable length code word VLW (X) and LSB were storing a memory cell of memory into respectively at first and at last.
Therefore, because the variable length code word has been provided promptly 6 of length the longest in the variable length code word, when a symbol x input, the storage code word M (X) that 200 outputs of code book memory are 6.
Table 2
Symbol [X] | Variable length code word VLW (X) |
a | 0xxxxx |
b | 100xxx |
c | 101xxx |
d | 110xxx |
e | 1110xx |
f | 11110x |
X: filler (or need not)
Fig. 6 is the flow chart of the Variable Length Code device of description in the presentation graphs 1
With reference to Fig. 6, in the first step, when symbol [x] was input to code book memory 200 from cushioning parts 100, as shown in table 2, code book memory 200 produced the storage code word M (c) that consistently has bit string [101xxx] with symbol [c].Then, code book memory 200 will be stored code word M (c) and offer variable length detection part 300 and package parts 400 (ST1 and ST2) respectively.
As shown in Figure 2, the storage code word M (c) that is input to variable length detection part 300 is imported into the coding alternative pack 510 of Boundary Detection parts 500.When storage code word M (c) is input to coding during alternative pack 510, as shown in table 3, coding alternative pack 510 from storage code word M (c), select successively 1 to 6 produce selected code word BW (c) m=1,2 ..., 6.Then, coding alternative pack 510 with each selected code word BW (c) m=1,2 ..., 6 offer level detection part 510 and calculating unit 520 (ST3) respectively.
Table 3
Selected code word BW (c) m |
BW(c)m=1=1 |
BW(c)m=2=10 |
BW(c)m=3=101 |
BW(c)m=4=1011 |
BW(c)m=5=10111 |
BW(c)m=6=101111 |
When being input to level detection part 520 by selected code word BW (c) m that selects the m position to be produced from storage code word M (c), as shown in Figure 8, level detection part 520 serves as the number that the termination node from ground floor to the m layer is detected on the basis with the huffman coding tree, and as shown above, produce level code word NM (c) m (ST4) of each layer.
For example, as shown in table 3 when m=3, coding alternative pack 510 produces selected code word BW (c) 3=[101].Then, level detection part 520 is given weights for each layer, and weights of these termination nodes are added up mutually, in order to produce addition and WS (c) 3 as follows:
Wherein j specifies the one deck in the standard huffman coding tree, and Lj points out the number of termination node in the j layer.
Then, the level detection part will and WS (c) 3 convert binary number to produce level code word NW (c) 3=[0001000].About all selected code word BW (c) m (m=1,2 ..., 6) level code word NW (c) m (m=1,2 ..., 6) shown in following table 4.
Table 4
m | NW(c) |
1 | 0=0000000 |
2 | 2=000010 |
3 | 8=001000 |
4 | 14=001110 |
5 | 30=011110 |
6 | 62=111110 |
When level code word NW (c) m (m=1,2 ..., 6) and selected code word BW (c) m (m=1,2 ..., 6) when being input to calculating unit 530 respectively, as shown in Figure 3, be input to respectively calculating unit 530 level code word NW (c) m (m=1,2 ..., 6) and selected code word BW (c) m (m=1,2 ..., 6) be input to first to the 6th subtracter 531 to 536 of calculating unit 530 respectively successively.
It is as follows that in first to the 6th subtracter 531 to 536 each is carried out identical function LD (c) m:
LD(c)m=BW(c)m-NW(c)m
Therefore, shown in following table 5, calculating unit 530 produce first to the 6th subtracter 531 to 536 LD as a result (c) m=1,2 ..., 6 as about operating result value LD (X=c) m=1,2 of symbol c ..., 6.
Table 5
m | LD(c)m MSB→LSB |
1 | 000001 |
2 | 000000 |
3 | 000001 |
4 | 111101 |
5 | 111001 |
6 | 110001 |
LD as a result (c) m=1,2 of first to the 6th subtracter 531 to 536 ..., 6 be provided for boundary value production part 540 (ST5).
To going on foot in 5 the detailed description, according to function LD (c) m that the following describes, first to the 6th subtracter 531 to 536 produce first to the 6th subtraction result LD (c) m=1,2 ..., 6, LD (c) m is described as follows:
Wherein bi represents the value of the i position of selected code word, and Lj represents the number of termination node in the j layer.
In the 6th step, boundary value production part 540 from subtraction result LD (c) m=1,2 ..., detect in each bit string of 6 MSB (c) m=1,2 ..., 6 value, and with MSB (c) m=1,2 ..., 6 value as boundary value MSB (c) m=1,2 ..., 6 output to code length detection part 600 (ST6).
In the 7th step, as shown in Figure 4, be input to code length detection part 600 MSB (c) m=1,2 ..., 6=[000111] each outputed to first to the 6th " exclusive OR " door by parallel, so code length detection part 600 produces the length value L EG (c) of bit strings [000100] as the length of the variable length code word of designated symbols [c].And this bit string [000100] is provided for package parts 400 (ST7).
In the 8th step, as length value L EG (c) when being provided for package parts 400, variable length code word VLW (c) among the package parts 400 detection of stored code word M (c) is with response length value LEG (c), and only exports the coding (ST8) of variable length code word VLW (c) as symbol [c].
With reference to Fig. 5 and Fig. 7 the operation of package parts among the present invention 400 is described in detail below.
Fig. 7 for expression when symbol [a, b, c, d, e, f, g, a, b] when being input to buffer unit 100 successively, the view of the output of each part of package parts.
With reference to Fig. 7, when time t=1, because storage code word M (X) is not input to package parts 400 from code book memory 200, as the response to length value L EG (c), the one-level barrel shifter 411 of package parts 400 is provided to one-level latch 412 with a bit string [X, X, X, X, X, X] as one-level window bit string B (t=1) 1.
Therefore, secondary latch 422 is respectively to subtracter 423 and adder 421 output L1=0, so three grades of latchs 424 output to secondary barrel shifter 431 with L2=6 as control signal.Then, as shown in Figure 7, secondary barrel shifter 431 output B (t=1) 2=[x, x, x, x, x, x] as the 6th the secondary window bit string B (t=1) that comprises from LSB to the 2K position.
When t=2, [a] is input to code book memory 200 when symbol, and when the storage code word M (a) of symbol [a] had produced from code book memory 200, storage code word M (a)=[a1, x, x, x, x, x] was input to the one-level barrel shifter 411 of input block 410 and the secondary barrel shifter 431 of output block 430 respectively.Simultaneously, as described above, the length value L EG (a)=1 that symbol [a] is corresponding produces from variable length detection part 300, and this length value L EG (a)=1 is offered one-level barrel shifter 411 and adder 421 respectively.
When the storage code word M (a) that produces length value L EG (a)=1 that produces from variable length detection part 300 and the code book memory 200 is input to one-level barrel shifter 411 respectively, one-level barrel shifter 411 latchs bit string [x in the one-level from one-level latch 412, x, x, x, x, x] and the storage code word M (a) bit string M (a)=[a1, x, x, x, x, x] go up and move with response length value LEG (a)=1, so, as shown in Figure 7, one-level barrel shifter 411 produces one-level window bit string W1=[x, x, x, x, x, a1], and with this one-level window bit string W1=[x, x, x, x, x, a1] output to one-level latch 412.One-level latch 412 is with this one-level window bit string W1=[x, x, x, x, x, a1] latch bit string L1 as one-level and output to one-level barrel shifter 411 and secondary barrel shifter 431 respectively.
Yet this moment, secondary barrel shifter 431 was to level Four latch 432 output one bit strings [a1, x, x, x, x, x] because the control signal L2=0 that three grades of latchs 424 in the parts 420 that add up are provided has been input to secondary barrel shifter 431.
When t=3, the storage code word M (b) of symbol [b]=[b1, b2, b3, x, x, x] and length value L EG (b)=3 are imported into one-level barrel shifter 411.One-level barrel shifter 411 is latching bit string W1=[x, x, x, x, x, a1 by storage code word M (b)=[b1, b2, b3, x, x, x] and one-level] bit string [x, x, x, x, x, a1, b1, b2, b3, x, x, x] formed goes up mobile, and begin to read 6 from three of the MSB back of this bit string, output to one-level latch 412 in order to these 6 [x, x, a1, b1, b2, b3] that will be read as one-level window bit string B (t=3) 1.Then, one-level latch 412 is with one-level window bit string B (t=3) 1=[x, x, a1, b1, b2, b3] latch bit string W1 as one-level and output to one-level barrel shifter 411 and secondary barrel shifter 431 respectively.On the other hand, adder 421 with LEG (b)=3 with from the L1=1 addition of secondary latch 422, and will output to secondary latch 422 with [4].Simultaneously, secondary latch 422 outputed to subtracter 423 with last time with L1=1, stored that current and L1=4 replace last time and L1=1.
That subtracter 423 will be worth that [6] deduct last time and L1=1, and will differ from S0=5 and offer three grades of latchs 424.Also will differ from S0=5 outputs to secondary barrel shifter 431 as control signal L2=5 to three grades of latch 424 storage difference S0=5 then.
Secondary barrel shifter 431 is latching bit string W1=[x, x, x, x, x, a1 by storage code word M (b)=[b1, b2, b3, x, x, x] and one-level] bit string [x, x, x, x, x, a1, b1, b2, b3, x, x, x] formed goes up mobilely responding the control signal L2=5 that three grades of latchs 424 provide, and begins to read 6 [a1, b1, b2, b3, x, x] from 5 of the MSB back of this bit string.Then, secondary barrel shifter 431 with read these 6 as secondary window bit string B (t=3) 2=[a1, b1, b2, b3, x, x] output to level Four latch 432.
Level Four latch 432 store secondary window bit string B (t=3) 2=[a1, b1, b2, b3, x, x].
When t=4, the storage code word M (c) of symbol [c]=[c1, c2, c3, x, x, x] and length value L EG (c)=3 are input to one-level barrel shifter 411.Response length value LEG (c)=3 one-level barrel shifter 411 is latching bit string W1=[x, x, a1, b1, b2, b3 by storage code word M (c)=[c1, c2, c3, x, x, x] and one-level] bit string [x, x, a1, b1, b2, b3, c1, c2, c3, x, x, x] formed goes up mobile, and begin to read 6 from three of the MSB back of this bit string, in order to 6 of gained [b1, b2, b3, c1, c2, c3] are outputed to one-level latch 412 as one-level window bit string B (t=4) 1.Then, one-level latch 412 is with one-level window bit string B (t=4) 1=[b1, b2, b3, c1, c2, c3] latch bit string W1 as one-level and output to one-level barrel shifter 411 and secondary barrel shifter 431 respectively.
On the other hand, adder 421 with LEG (c)=3 with produce one and [7] in the Calais mutually from the L1=4 of secondary latch 422.But because adder 421 is carried out moulds 6 additions, thus adder 421 outputs and for [1], carry signal be C=1.Then, adder 421 will offer secondary latch 422 with [1], and outside output carry signal.
Secondary latch 422 simultaneously with last time output to subtracter 423 with L1=4, as shown in Figure 7, and with and L1=1 replace last time and L1=4.
Subtracter 423 deducts last time from [6] and L1=4, and will differ from S0=2 and offer three grades of latchs 424.Then, three grades of latch 424 saved differences S0=2 and difference S0=2 outputed to secondary barrel shifter 431 as control signal L2=2.
Then, respond the control signal L2=2 secondary barrel shifter 431 that three grades of latchs 424 provide and latching bit string W1=[x, x, a1, b1, b2, b3 by storage code word M (b)=[c1, c2, c3, x, x, x] and one-level] mobile on the bit string [x, x, a1, b1, b2, b3, c1, c2, c3, x, x, x] formed, and begin to read 6 [a1, b1, b2, b3, c1, c2] from 2 of the MSB back of this bit string.Then, secondary barrel shifter 431 with 6 bit strings being read as secondary window bit string B (t=4) 2=[a1, b1, b2, b3, c1, c2] output to level Four latch 432.
At this moment, 6 memory spaces of all of level Four latch 432 are shared by the variable length code word.Therefore, by reading every appearance with response carry signal C=1 of level Four latch 432 outputs, we can obtain the variable length code word except that filler.
The process that goes out as shown above, package parts are constantly exported the code word of variable length according to the symbol of input.
Therefore, the present invention can convert symbol to the code word of variable length and needn't store the length of data in memory.
Although described the present invention in conjunction with most preferred embodiment above, those skilled in the art should be appreciated that the present invention can also make amendment, but all modifications must belong in the scope and spirit essence of the present invention of appended claim book defined.