Summary of the invention
In view of this, the purpose of the embodiment of the invention is to provide a kind of decoder and coding/decoding method, can decode to the code stream of Ogg Vorbis encapsulation format fast and effectively.
For achieving the above object, the embodiment of the invention provides a kind of decoder, be used for reading code stream and decoding from external memory storage, the encapsulation format of described code stream is: from the idle minimum data position of target byte, each bit of semantic item in the described code stream is write to a high position successively from low level, after described target byte is write and is expired, the remaining bits of the described semantic item minimum data position from next byte is begun to write, the storage mode of described memory is: according to the order that receives, with described code stream from the low address byte to the high address bytes store, described decoder comprises:
First buffer memory is used to store the code stream that reads from described memory, and the storage mode of described first buffer memory is: the low address byte code stream that will read from described memory is stored in idle minimum data position;
Processing module, be used for reading designated length minimum data bit code stream and output from described first buffer memory, and with the described designated length of the data shift right position in described first buffer memory, described designated length is a less value in first length and second length, described first length is the current bit length that needs the semantic item of decoding, and described second length is the significant bit length of the data of current storage in described first buffer memory;
Read module is used for when described second length is zero, reads code stream and store in described first buffer memory from the available code stream of described memory;
First update module is used in described first length during greater than described second length, and the difference of obtaining described first length and described second length is as the first new length, and sends to described processing module;
Second update module is used for when the data of described first buffer memory change, and upgrades described second length, and sends to described processing module;
Second buffer memory is used to store the code stream of described processing module output, and the storage mode of described second buffer memory is: the code stream of described processing module output is stored in idle minimum data position;
When output module, the length that is used for the code stream stored at described second buffer memory equal the length of semantic item of described current needs decoding, with the output of the code stream in described second buffer memory, and with the described second buffer memory zero clearing.
Described processing module further comprises:
First obtains submodule, is used for the coding criterion according to described code stream, obtains the current bit length that needs the semantic item of decoding, as described first length;
Second obtains submodule, is used for obtaining the significant bit length of the data of the current storage of described first buffer memory, as described second length;
Comparison sub-module is used for more described first length and described second length;
Reading submodule is used for the numerical value that described first length and described second length is less as designated length, reads described designated length minimum data bit code stream and output from described first buffer memory;
First updating submodule is used for behind described reading submodule output code flow, with the described designated length of the data shift right position in described first buffer memory.
Described first obtains submodule further comprises:
First judging unit is used to judge whether described first length that gets access to is zero;
The zero setting unit is used for controlling the described second buffer memory zero setting when described first length is zero;
First transmitting element is used for when described first length is non-vanishing described first length being sent to described comparison sub-module.
Described second obtains submodule further comprises:
Second judging unit is used to judge whether described second length that gets access to is zero;
First notification unit is used for notifying described read module when described second length is zero;
Second transmitting element is used for when described second length is non-vanishing described second length being sent to described comparison sub-module.
Described read module further comprises:
Judge submodule, be used for when described second length is zero that whether the available code stream length of judging described memory is greater than first byte length, described first byte length is the maximum length of the data that can store of described first buffer memory;
Fill submodule, be used in described available code stream length during more than or equal to described first byte length, available code stream first address according to described memory, from the available code stream of described memory, read the code stream of described first byte length and be stored in described first buffer memory, in described available code stream length during, from described memory, read the code stream of described available code stream length and be stored in described first buffer memory less than described first byte length;
Second updating submodule is used for after described filling submodule reads code stream, upgrades described available code stream first address and described available code stream length.
Described code stream is the code stream of Ogg Vorbis form.
The embodiment of the invention also provides a kind of coding/decoding method, comprise the step that from external memory storage, reads code stream and decode, the encapsulation format of described code stream is: from the idle minimum data position of target byte, each bit of semantic item in the described code stream is write to a high position successively from low level, after described target byte is write and is expired, the remaining bits of the described semantic item minimum data position from next byte is begun to write, the storage mode of described memory is: according to the order that receives, with described code stream from the low address byte to the high address bytes store, described method is further comprising the steps of:
Obtain the current bit length that needs the semantic item of decoding, as first length;
Obtain the significant bit length of the data of current cache, as second length;
With the smaller value in described first length and described second length as designated length;
From data in buffer, read described designated length minimum data bit code stream, and described code stream is placed the idle minimum data position of returning numerical value, with the data in buffer described designated length position that moves to right;
Whether the length of judging code stream in the described return value equals the described current length that needs the semantic item of decoding;
When the length of code stream equals the length of the described current semantic item that needs decoding in described return value, with the output of the code stream in the described return value, and after output with described return value zero clearing;
Code stream is uneven in length during length in the described current semantic item that needs decoding in described return value, from described external memory storage, read the code stream row cache of going forward side by side, upgrade described first length and described second length, and return described the step of the smaller value in described first length and described second length as designated length.
The described significant bit length of obtaining the data of current cache also comprises as before second length:
Judge that whether described first length is zero, when described first length is zero, directly controls the step of described return value zero setting.
Described with the smaller value in described first length and described second length as also comprising before the designated length:
Judge that whether described second length be zero, when described second length is zero, from described external memory storage, read the code stream row cache of going forward side by side, upgrade the step of described first length and described second length.
Described the go forward side by side method of row cache of code stream that reads from described external memory storage is specially:
Whether the available code stream length of judging described memory greater than first byte length, and described first byte length is the maximum length that described decoder can data in buffer;
In described available code stream length during more than or equal to described first byte length, available code stream first address according to described memory, the code stream that from the available code stream of described memory, reads described first byte length row cache of going forward side by side, in described available code stream length during less than described first byte length, the code stream that from described memory, the reads described available code stream length row cache of going forward side by side;
Upgrade described available code stream first address and described available code stream length.
Described code stream is the code stream of Ogg Vorbis form.
Embodiments of the invention have the following advantages:
Decoder and coding/decoding method by the embodiment of the invention, when the code stream of Ogg Vorbis encapsulation format is decoded, can read code stream in the following order: reading in proper order between the byte to read to the high address byte from the low address byte, byte inner reads order for reading to higher bit position (MSB) from low bit (LSB), in addition, when from external memory storage, reading code stream, it always is unit with the byte, the code stream of the low address byte of memory is read the idle minimum data position of decoder buffer, therefore, the sequence independence of depositing with the platform byte.
Embodiment
The embodiment of the invention provides a kind of decoder and coding/decoding method, make when the code stream that receives is decoded, can read code stream in the following order: read order for to read to the high address byte from the low address byte between the byte, byte inner reads order for reading to higher bit position (MSB) from hanging down bit (LSB).
Below in conjunction with drawings and Examples, the specific embodiment of the present invention is described in further detail.
Be illustrated in figure 5 as the decoder architecture schematic diagram of the embodiment of the invention, this decoder 10, be used for reading code stream and decoding from external memory storage 20, the encapsulation format of described code stream is: from the idle minimum data position of target byte, each bit of semantic item in the described code stream is write to a high position successively from low level, after described target byte is write and is expired, the remaining bits of the described semantic item minimum data position from next byte is begun to write, the storage mode of described memory 20 is: according to the order that receives, with described code stream from the low address byte to the high address bytes store.
Decoder 10 comprises:
First buffer memory 11, be used for storing the code stream that reads from memory 20, the storage mode of first buffer memory 11 is: the low address bytes store that will read from memory 20 is in idle minimum data position, the length of the byte that reads from memory 20 is no more than first byte length, described first byte length is the maximum length of 11 data that can store of described first buffer memory, and described first byte length can be 4 bytes, 8 bytes or other bigger byte lengths.
Processing module 12, be used for reading designated length minimum data bit code stream and output from first buffer memory 11, and with the described designated length of the data shift right position in described first buffer memory 11, described designated length is a less value in first length and second length, described first length is the current bit length that needs the semantic item of decoding, described second length is the significant bit length of the data of current storage in first buffer memory 11, and described significant bit length is the length of the available code stream of storage in described first buffer memory 11.
Read module 13 is used for when described second length is zero, reads code stream and store in first buffer memory 11 from the available code stream of memory 20.
First update module 14 is used in described first length during greater than described second length, and the difference of obtaining described first length and described second length is as the first new length, and sends to processing module 12.
Second update module 15 is used for when the data of first buffer memory 11 change, and upgrades described second length, and sends to processing module 12.Data in described first buffer memory 11 change promptly: first buffer memory 11 carries out the dextroposition operation from the data that memory 20 reads the new code stream or first buffer memory 11.
Second buffer memory 16 is used for the code stream that stores processor module 12 is exported, and the storage mode of second buffer memory 16 is: the code stream of processing module 12 outputs is stored in idle minimum data position.
Output module 17 when being used for length at the code stream of second buffer memory 16 storage and equaling the length of semantic item of described current needs decoding, is exported the code stream in second buffer memory 16, and with 16 zero clearings of second buffer memory.
During the length (first length) of the code stream that the effective length (second length) of the data of storage reads less than current needs in first buffer memory 11, the code stream of above-mentioned processing module 12 outputs then is not a complete semantic item, therefore, the code stream output that output module 17 does not receive second buffer memory 16 immediately, but continue to wait for, the code stream that processing module 12 will be exported next time is stored in the idle minimum data position of second buffer memory 16, when the length of the code stream of storage equaled the length of the described current semantic item that need decode in second buffer memory 16, output module 17 was just exported the code stream in second buffer memory 16.
Above-mentioned processing module 12 further comprises:
First obtains submodule 121, is used for the coding criterion according to the code stream that receives, and obtains the current bit length that needs the semantic item of decoding, as first length.
Second obtains submodule 122, is used for obtaining the significant bit length of first buffer memory, 11 current storage data, as second length.
Comparison sub-module 123 is used for comparison first length and second length.
Reading submodule 124 is used for the numerical value that first length and second length is less as designated length, reads described designated length minimum data bit code stream and output from first buffer memory 11.
First updating submodule 125 is used for behind reading submodule 124 output code flows, with the described designated length of the data shift right position in first buffer memory 11.
Certainly, in second length less than first length, and reading submodule 124 reads described second length minimum data position from first buffer memory 11 after, also can not carry out the dextroposition operation of first updating submodule 125, but directly from memory, read new code stream, the data in first buffer memory 11 are covered.
The above-mentioned described code stream that receives can be the code stream of Ogg Vorbis form, can certainly be the code stream of the extended formatting identical with Ogg Vorbis bit-envelope order.
When described code stream is the code stream of Ogg Vorbis form, requirement according to Ogg Vorbis coding criterion, the semantic item that also may comprise 0 byte length in the described code stream, be above-mentioned first obtain that submodule 121 gets access to current to need the length (i.e. first length) of the semantic item of decoding may be zero, when first length is zero, then need not carry out above-mentioned comparison, read, upgrade operation, but obtain directly control second buffer memory 16 zero setting of submodule 121 by first, therefore, above-mentioned first acquisition module 121 further comprises:
First judging unit is used to judge whether described first length that gets access to is zero.
The zero setting unit is used for controlling 16 zero setting of described second buffer memory when described first length is zero.
First transmitting element is used for when described first length is non-vanishing described first length being sent to described comparison sub-module 123.
When above-mentioned first buffer memory, 11 stored valid data are sky, above-mentioned second to obtain second length that submodule 122 gets access to be zero, second obtains submodule 122 needs to notify read module 13 to read new code stream from memory 20 and store in first buffer memory 11 at this moment, therefore, described second obtain submodule 122 and further comprise:
Second judging unit is used to judge whether described second length that gets access to is zero.
First notification unit is used for notifying described read module 13 when described second length is zero.
Second transmitting element is used for when described second length is non-vanishing described second length being sent to described comparison sub-module 123.
Above-mentioned reading submodule 124 reads a described designated length minimum data position from first buffer memory 11 method is specially: the minimum data bit code stream of the described designated length in first buffer memory 11 is carried out and operation with particular data, the designated length of a described particular data minimum data position bit is 1, all the other bits are 0, described result with operation promptly is the code stream that need read, certainly, reading submodule 124 also can adopt additive method to read code stream from first buffer memory 11.
Above-mentioned read module 13 further comprises:
Judge submodule 131, be used for when described second length is zero that whether judge available code stream length in the memory 20 greater than first byte length, described first byte length is the maximum length of first buffer memory, 11 storable data.Described available code stream length is the byte length of the code stream that is not read in the memory 20, generally, when decoder 10 reads code stream from memory 20, also need to read the available code stream first address and the available code stream length of memory 20, described available code stream first address is the first address of the code stream that is not read in the described memory 20.
Fill submodule 132, be used in described available code stream length during more than or equal to described first byte length, available code stream first address according to described memory 20, from the available code stream of memory 20, read the code stream of described first byte length and store in first buffer memory 11, in described available code stream length during, from memory 20, read the code stream of described available code stream length and store in first buffer memory 11 less than described first byte length.
Second updating submodule 133 is used for upgrading described available code stream first address and described available code stream length after filling submodule 132 reads code stream.
The method that reads code stream and store in first buffer memory 11 from memory 20 is specially: the code stream that reads available code stream first address in the memory 20, and described code stream is stored into the idle minimum data position of first buffer memory 11, upgrade described available code stream first address, be about to available code stream first address and add one, repeat aforesaid operations, finish until reading.In the aforesaid operations, always the code stream of the low address byte of memory 20 is read the idle minimum data position of first buffer memory 11, the sequence independence of depositing with the platform byte, be illustrated in figure 6 as the embodiment of the invention from memory, read code stream after, the corresponding schematic diagram of the code stream of storing in the code stream of storing in first buffer memory and the memory.
Be illustrated in figure 7 as the coding/decoding method schematic flow sheet of the embodiment of the invention, specifically may further comprise the steps:
Step 701 according to the coding criterion of the code stream that reads from external memory storage, is obtained the current bit length that needs the semantic item of decoding, as first length.
The encapsulation format of described code stream is: from the idle minimum data position of target byte, each bit of semantic item in the described code stream is write to a high position successively from low level, after described target byte is write and is expired, the remaining bits of the described semantic item minimum data position from next byte is begun to write, and the low address byte code stream that described decoder will read from described memory is stored in idle minimum data position; The storage mode of described external memory storage is: according to the order that receives, with described code stream from the low address byte to the high address bytes store.
Step 702 is obtained the significant bit length of the data of current cache, as second length.
Step 703, with the smaller value in described first length and described second length as designated length.
Step 704 reads a described designated length minimum data bit code stream from data in buffer, and the described code stream that reads is placed the idle minimum data position of returning numerical value.
Step 705 is with the data in buffer described designated length position that moves to right.
Step 706 judges whether the length of code stream in the described return value equals the described current length that needs the semantic item of decoding, if enter step 709, otherwise enter step 707.
Step 707 reads the code stream row cache of going forward side by side from external memory storage.
Certainly, when external memory storage reads code stream, also can not carry out above-mentioned steps 705, but the code stream that will read covers original data in buffer at needs from external memory storage.
Step 708 is upgraded first length and second length, returns step 703.
At this moment, first length after the renewal equals the poor of former first length and former second length, and second length after the renewal is the length of the code stream that reads from external memory storage.
Step 709, with the code stream in the return value that obtains output, and after output with the return value zero clearing, the code stream of storing in the described return value is the current semantic item that needs decoding.
In addition, also comprise before the above-mentioned steps 703:
Whether the bit length (i.e. first length) of the current semantic item that need decode that judgement gets access to is zero step, when the bit length of the described current semantic item that need decode equals zero, directly return value zero setting is also exported; And
Judge that whether described second length is zero step, when described second length is zero, directly enters described step 707.
Read in the above-mentioned steps 704 data in buffer method can for: the designated length of a data in buffer minimum data bit code stream is carried out and operation with particular data, the bit of the designated length of a described particular data minimum data position is 1, other bits are 0, described result with operation promptly is the code stream that need read, and described designated length is first length or second length.Certainly, also can adopt additive method to read data in buffer.
The implementation method of above-mentioned steps 707 is specially:
Whether the available code stream length of judging described memory greater than first byte length, and described first byte length is the maximum length that described decoder can data in buffer;
In described available code stream length during more than or equal to described first byte length, available code stream first address according to described memory, the code stream that from the available code stream of described memory, reads described first byte length row cache of going forward side by side, in described available code stream length during less than described first byte length, the code stream that from described memory, the reads described available code stream length row cache of going forward side by side;
Upgrade described available code stream first address and described available code stream length.
Below be example with the code stream of Ogg Vorbis form, the coding/decoding method of the embodiment of the invention is elaborated.
Decoder at first needs to read code stream from external memory storage, generally, external memory storage is with the form of array code stream to be stored, the code stream of the byte of storage of each in the array, its storage mode is: according to the reception of code stream order, from the low address byte to the high address bytes store.
When decoder flows from the memory read code fetch, always be that unit reads with the byte, and the code stream of the low address byte of memory is stored in the idle minimum data position of first buffer memory of decoder, and therefore, the data independence that above-mentioned read operation and platform byte are deposited.
In addition, when decoder reads code stream from external memory storage, also need to read the first address bufPointer of current available code stream of memory and the length nBytes of available code stream, along with the carrying out of the read operation of code stream, this is constantly carried out bufPointer and adds one operation.
Below read the identifying code of code stream from external memory storage for decoder:
void?RefillBitstreamCache(BitStreamInfo*bsi)
{
int?nBytes=bsi->nBytes;
/*check?for?underflow?case*/
if(nBytes<=0)
{
bsi->cachedBits=0;
bsi->iCache=0;
bsi->nBytes=-1;
return;
}
/ * read operation, with the code stream of the low address byte of memory be stored in the idle minimum data position * of first buffer memory of decoder/
if(nBytes>=4){
bsi->iCache=(unsigned?int)(*bsi->bytePtr++);
bsi->iCache|=(((unsigned?int)(*bsi->bytePtr++))<<8);
bsi->iCache|=(((unsigned?int)(*bsi->bytePtr++))<<16);
bsi->iCache|=(((unsigned?int)(*bsi->bytePtr++))<<24);
bsi->cachedBits=32;
bsi->nBytes-=4;
}else{
int?n=0;
bsi->iCache=0;
while(nBytes--){
bsi->iCache|=((*bsi->bytePtr++)<<(8*n));
++n;
}
bsi->cachedBits=8*bsi->nBytes;
bsi->nBytes=0;
}
}
First buffer memory in the above-mentioned decoder is equivalent to a register, it can carry out bit manipulation to the code stream of buffer memory, is generally the length of 4 bytes, also may be the length of 8 bytes, perhaps the length of other bytes supposes in the embodiment of the invention that first buffer memory is the length of 4 bytes.
The length of the semantic item in the Ogg Vorbis code stream generally is less than or equal to 32 bits (i.e. 4 bytes).
The coding/decoding method of the embodiment of the invention is specially:
Obtain the current length nBits (regulation nBits is less than or equal to 32 bits) that needs the semantic item of decoding herein;
Owing to require can comprise in the code stream semantic item of 0 byte length in the coding criterion of Ogg Vorbis form, therefore, need also to judge whether nBits is zero, when nBits equals zero,, when nBits is non-vanishing, carry out following operation directly with return value zero setting;
Obtain the effective length cachedBits of the data of storing in first buffer memory, and judge that whether cachedBits is zero, when cachedBits is zero, from external memory storage, reads new code stream and store in first buffer memory, when cachedBits is non-vanishing, carry out following operation;
The above-mentioned method that reads code stream from memory is specially: whether the current available code stream length nBytes that judges memory is greater than the length 4 of first buffer memory; At nBytes more than or equal to 4 o'clock, available code stream first address bufPointer according to described memory, from the available code stream of described memory, read the code stream of 4 bytes and store in described first buffer memory,, from described memory, read the code stream of described nBytes length and store in described first buffer memory less than 4 o'clock at nBytes;
The size that compares nBits and cachedBits;
When nBits is less than or equal to cachedBits, utilize logical bit operation (with operation) to read nBits minimum data position in first buffer memory, being about to nBits minimum data position in first buffer memory carries out with particular data MASK_TAB and operates, the bit of the nBits of described particular data MASK_TAB minimum data position is 1, other bits are 0, place nBits the minimum data position of return value data, and first buffer memory is carried out dextroposition operate, delete nBits the bit that has been read in first buffer memory, with the low bit that is not read, place the minimum data position, upgrade cachedBits simultaneously, this stylish cachedBits equals the poor of nBits and former cachedBits;
At nBits during greater than cachedBits, promptly the bit number of the available code stream in first buffer memory is not enough, from first buffer memory, take out all remaining cachedBits available bits, place cachedBits the minimum data position of return value data, renewal nBits is nBits2, nBits2 is the poor of nBits and cachedBits, simultaneously, first buffer memory is carried out the dextroposition operation, with all the bit deletions in first buffer memory, be that cachedBits is zero, then from memory, read new code stream, and upgrade cachedBits, certainly, also can not carry out the dextroposition operation, the new code stream that directly will read covers former data; Suppose that available code stream length is greater than 4 bytes in memory this moment, therefore, the length that reads new code stream from memory is 4 bytes (32 bits); Because nBits is less than or equal to 32, nBits2 is inevitable less than 32, therefore, only need utilize the logical bit operation from first buffer memory, to read nBits2 minimum data position and can finish reading of semantic item, nBits2 the code stream that reads placed (cachedBits)~nBits-1 bit of return value data, the instant current semantic item that needs decoding of the code stream of storing among the described return value data this moment.
The identifying code of said process is as follows:
unsigned?int?GetBits(BitStreamInfo*bsi,int?nBits)
{
/*const?table?to?extract?fixed?length?bits?from?iCache*/
Static?const?unsigned?int?MASK_TAB[33]=
{
0x00000000,0x00000001,0x00000003,0x00000007,0x0000000f,
0x0000001f,0x0000003f,0x0000007f,0x000000ff,0x000001ff,
0x000003ff,0x000007ff,0x00000fff,0x00001fff,0x00003fff,
0x00007fff,0x0000ffff,0x0001ffff,0x0003ffff,0x0007ffff,
0x000fffff,0x001fffff,0x003fffff,0x007fffff,0x00ffffff,
0x01ffffff,0x03ffffff,0x07ffffff,0x0fffffff,0x1fffffff,
0x3fffffff,0x7fffffff,0xffffffff
};
/*return?data?initialization*/
unsigned?int?data=0;
unsigned?int?mask=0;
/*limit?nBits*/
nBits=MIN(nBits,32);
nBits=MAX(nBits,0);
mask=MASK_TAB[nBits];
/*check?whether?nBits?is?zero*/
if(nBits==0)
return?0;
if(bsi->cachedBits>=nBits)
{
/*enough?readable?bits?in?iCache*/
/*extract?and?update*/
data=bsi->iCache&mask;
bsi->iCache>>=nBits; /*unsigned?type,zero-extending*/
bsi->cachedBits-=nBits;
}
else{
/*not?enough?readable?bits?in?iCache*/
/*extract?all?readable?bits*/
int?bit_num_read=bsi->cachedBits;
mask=MASK_TAB[bit_num_read];
data=bsi->iCache&mask;
/*update?nBits*/
nBits-=bit_num_read;
mask=MASK_TAB[nBits];
/*fill?iCache?with?new?data*/
RefillBitstreamCache(bsi);
/*appending?bits*/
data=data|((bsi->iCache&mask)<<bit_num_read);
/*update*/
bsi->iCache>>=nBits;
bsi->cachedBits-=nBits;
}
return?data;
}
The above only is a preferred implementation of the present invention; should be pointed out that for those skilled in the art, under the prerequisite that does not break away from the principle of the invention; can also make some improvements and modifications, these improvements and modifications also should be considered as protection scope of the present invention.