update brrUtils (again)

This commit is contained in:
tildearrow 2022-09-24 16:44:47 -05:00
parent 3bfdb35578
commit ddfdeffc99
2 changed files with 79 additions and 4 deletions

View File

@ -22,10 +22,81 @@
#include "brrUtils.h"
long brrEncode(short* buf, unsigned char* out, long len) {
long brrEncode(short* buf, unsigned char* out, long len, long loopStart) {
if (len==0) return 0;
// TODO
return 0;
// encoding process:
// 1. read next group of 16 samples
// 2. is this the first block?
// - if yes, don't filter. output and then go to 1
// 3. is this the loop block?
// - if yes, don't filter. output and then go to 1
// 4. try encoding using 4 filters
// 5. which one of these yields the least amount of error?
// 6. output the one which does
// 7. is this the last block?
// - if yes, mark end and finish
// 8. go to 1
long total=0;
unsigned char next[9];
unsigned char filter=0;
unsigned char range=0;
unsigned char o=0;
short o0=0;
len&=~15;
loopStart&=~15;
for (long i=0; i<len; i+=16) {
// don't filter on the first or loop block
if (i && i!=loopStart) {
} else {
filter=0;
}
range=0;
for (int j=0; j<16; j++) {
short s=buf[j];
if (s<0) s=-s;
while (range<11 && s>((8<<range)-1)) range++;
}
next[0]=(range<<4)|(filter<<2)|((i+16>len)?1:0);
switch (filter) {
case 0:
for (int j=0; j<16; j++) {
o0=buf[j]>>range;
if (o0>7) o0=7;
if (o0<-8) o0=-8;
o=o0&15;
if (j&1) {
next[1+(j>>1)]|=o;
} else {
next[1+(j>>1)]=o<<4;
}
}
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
out[0]=next[0];
out[1]=next[1];
out[2]=next[2];
out[3]=next[3];
out[4]=next[4];
out[5]=next[5];
out[6]=next[6];
out[7]=next[7];
out[8]=next[8];
buf+=16;
out+=9;
total+=9;
}
return total;
}
#define DO_ONE_SAMPLE \
@ -55,6 +126,9 @@ long brrEncode(short* buf, unsigned char* out, long len) {
*out=next; \
out++;
// TODO:
// - what happens during overflow?
// - what happens when range values 12 to 15 are used?
long brrDecode(unsigned char* buf, short* out, long len) {
if (len==0) return 0;

View File

@ -32,9 +32,10 @@ extern "C" {
* @param buf input data.
* @param out output buffer. shall be at least 9*(len/16) shorts in size.
* @param len input length (should be a multiple of 16. if it isn't, the output will be padded).
* @param loopStart beginning of loop area (may be 0 for no loop). this is used to ensure the respective block has no filter in order to loop properly.
* @return number of written samples.
*/
long brrEncode(short* buf, unsigned char* out, long len);
long brrEncode(short* buf, unsigned char* out, long len, long loopStart);
/**
* read len bytes from buf, decode BRR and output to out.