| 1 |
/* brrUtils - BRR audio codec utilities |
| 2 |
* Copyright (C) 2022 tildearrow |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
* of this software and associated documentation files (the "Software"), to deal |
| 6 |
* in the Software without restriction, including without limitation the rights |
| 7 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 |
* copies of the Software, and to permit persons to whom the Software is |
| 9 |
* furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included in all |
| 12 |
* copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
* SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "brrUtils.h" |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#define NEXT_SAMPLE buf[j]-(buf[j]>>3) |
| 28 |
|
| 29 |
#define DO_ONE_DEC(r) \ |
| 30 |
if (nextDec&8) nextDec|=0xfffffff0; \ |
| 31 |
\ |
| 32 |
if (r>=13) { /* invalid shift */ \ |
| 33 |
nextDec=(nextDec<0)?0xfffff800:0; \ |
| 34 |
} else { \ |
| 35 |
nextDec<<=r; /* range */ \ |
| 36 |
nextDec>>=1; \ |
| 37 |
} \ |
| 38 |
\ |
| 39 |
switch (filter) { /* filter */ \ |
| 40 |
case 0: \ |
| 41 |
break; \ |
| 42 |
case 1: \ |
| 43 |
nextDec+=last1+((-last1)>>4); \ |
| 44 |
break; \ |
| 45 |
case 2: \ |
| 46 |
nextDec+=last1*2+((-last1*3)>>5)-last2+(last2>>4); \ |
| 47 |
break; \ |
| 48 |
case 3: \ |
| 49 |
nextDec+=last1*2+((-last1*13)>>6)-last2+((last2*3)>>4); \ |
| 50 |
break; \ |
| 51 |
} \ |
| 52 |
\ |
| 53 |
if (nextDec>32767) nextDec=32767; \ |
| 54 |
if (nextDec<-32768) nextDec=-32768; \ |
| 55 |
nextDec&=0x7fff; \ |
| 56 |
if (nextDec&0x4000) nextDec|=0xffff8000; \ |
| 57 |
\ |
| 58 |
last2=last1; \ |
| 59 |
last1=nextDec; \ |
| 60 |
|
| 61 |
void brrEncodeBlock(const short* buf, unsigned char* out, unsigned char range, unsigned char filter, short* last1, short* last2, int* errorSum) { |
| 62 |
// encode one block using BRR |
| 63 |
unsigned char nibble=0; |
| 64 |
int preOut=0; |
| 65 |
int pred=0; |
| 66 |
int nextDec=0; |
| 67 |
int nextError=0; |
| 68 |
*errorSum=0; |
| 69 |
for (int j=0; j<16; j++) { |
| 70 |
short s=NEXT_SAMPLE; |
| 71 |
switch (filter) { |
| 72 |
case 0: // no filter |
| 73 |
pred=s; |
| 74 |
break; |
| 75 |
case 1: // simple |
| 76 |
pred=s-(((int)(*last1*2)*15)>>4); |
| 77 |
break; |
| 78 |
case 2: // complex |
| 79 |
pred=s+(((int)(*last2*2)*15)>>4)-(((int)(*last1*2)*61)>>5); |
| 80 |
break; |
| 81 |
case 3: |
| 82 |
pred=s+(((int)(*last2*2)*13)>>4)-(((int)(*last1*2)*115)>>6); |
| 83 |
break; |
| 84 |
} |
| 85 |
|
| 86 |
if (pred<-32768) pred=-32768; |
| 87 |
if (pred>32767) pred=32767; |
| 88 |
|
| 89 |
preOut=pred>>range; |
| 90 |
if (range) { |
| 91 |
if (pred&(1<<(range>>1))) preOut++; |
| 92 |
if (filter==0 && range>=12) if (preOut<-7) preOut=-7; |
| 93 |
} |
| 94 |
if (preOut>7) preOut=7; |
| 95 |
if (preOut<-8) preOut=-8; |
| 96 |
|
| 97 |
nibble=preOut&15; |
| 98 |
if (j&1) { |
| 99 |
out[j>>1]|=nibble; |
| 100 |
} else { |
| 101 |
out[j>>1]=nibble<<4; |
| 102 |
} |
| 103 |
|
| 104 |
// roll last1/last2 |
| 105 |
nextDec=nibble; |
| 106 |
if (nextDec&8) nextDec|=0xfffffff0; |
| 107 |
|
| 108 |
if (range>=13) { /* invalid shift */ |
| 109 |
nextDec=(nextDec<0)?0xfffff800:0; |
| 110 |
} else { |
| 111 |
nextDec<<=range; /* range */ |
| 112 |
nextDec>>=1; |
| 113 |
} |
| 114 |
|
| 115 |
switch (filter) { /* filter */ |
| 116 |
case 0: |
| 117 |
break; |
| 118 |
case 1: |
| 119 |
nextDec+=(*last1)+((-(*last1))>>4); |
| 120 |
break; |
| 121 |
case 2: |
| 122 |
nextDec+=(*last1)*2+((-(*last1)*3)>>5)-(*last2)+((*last2)>>4); |
| 123 |
break; |
| 124 |
case 3: |
| 125 |
nextDec+=(*last1)*2+((-(*last1)*13)>>6)-(*last2)+(((*last2)*3)>>4); |
| 126 |
break; |
| 127 |
} |
| 128 |
|
| 129 |
nextDec&=0x7fff; |
| 130 |
if (nextDec&0x4000) nextDec|=0xffff8000; |
| 131 |
|
| 132 |
nextError=s-(nextDec<<1); |
| 133 |
if (nextError<0) nextError=-nextError; |
| 134 |
*errorSum+=nextError; |
| 135 |
|
| 136 |
*last2=*last1; |
| 137 |
*last1=nextDec; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
long brrEncode(short* buf, unsigned char* out, long len, long loopStart, unsigned char emphasis, unsigned char noFilter) { |
| 142 |
if (len==0) return 0; |
| 143 |
|
| 144 |
// encoding process: |
| 145 |
// 1. read next group of 16 samples |
| 146 |
// 2. is this the first block? |
| 147 |
// - if yes, don't filter. output and then go to 1 |
| 148 |
// 4. try encoding using 3 filters and 12 ranges (besides no filter) |
| 149 |
// 5. which one of these yields the least amount of error? |
| 150 |
// 6. output the one which does |
| 151 |
// 7. do we still have more to encode? |
| 152 |
// - if so, go to 1 |
| 153 |
// 8. is loop point set? |
| 154 |
// - if not, end process here |
| 155 |
// 9. is transition between last block and loop block smooth? |
| 156 |
// - if not, encode the loop block again and output it |
| 157 |
long total=0; |
| 158 |
unsigned char filter=0; |
| 159 |
unsigned char range=0; |
| 160 |
unsigned char numFilters=noFilter?2:4; |
| 161 |
|
| 162 |
short x0=0; |
| 163 |
short x1=0; |
| 164 |
short x2=0; |
| 165 |
int emphOut=0; |
| 166 |
|
| 167 |
short in[17]; |
| 168 |
|
| 169 |
short last1[4][13]; |
| 170 |
short last2[4][13]; |
| 171 |
int avgError[4][13]; |
| 172 |
unsigned char possibleOut[4][13][8]; |
| 173 |
|
| 174 |
memset(in,0,16*sizeof(short)); |
| 175 |
memset(last1,0,4*13*sizeof(short)); |
| 176 |
memset(last2,0,4*13*sizeof(short)); |
| 177 |
memset(avgError,0,4*13*sizeof(int)); |
| 178 |
memset(possibleOut,0,4*13*8); |
| 179 |
|
| 180 |
for (long i=0; i<len; i+=16) { |
| 181 |
if (i+17>len) { |
| 182 |
long p=i; |
| 183 |
for (int j=0; j<17; j++) { |
| 184 |
if (p>=len) { |
| 185 |
if (loopStart<0 || loopStart>=len) { |
| 186 |
in[j]=0; |
| 187 |
} else { |
| 188 |
p=loopStart; |
| 189 |
in[j]=buf[p++]; |
| 190 |
} |
| 191 |
} else { |
| 192 |
in[j]=buf[p++]; |
| 193 |
} |
| 194 |
} |
| 195 |
} else { |
| 196 |
memcpy(in,&buf[i],17*sizeof(short)); |
| 197 |
} |
| 198 |
|
| 199 |
// emphasis |
| 200 |
if (emphasis) { |
| 201 |
for (int j=0; j<17; j++) { |
| 202 |
x0=x1; |
| 203 |
x1=x2; |
| 204 |
x2=in[j]; |
| 205 |
|
| 206 |
if (j==0) continue; |
| 207 |
emphOut=((x1<<11)-x0*370-in[j]*374)/1305; |
| 208 |
if (emphOut<-32768) emphOut=-32768; |
| 209 |
if (emphOut>32767) emphOut=32767; |
| 210 |
in[j-1]=emphOut; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
// encode |
| 215 |
for (int j=0; j<numFilters; j++) { |
| 216 |
for (int k=0; k<13; k++) { |
| 217 |
brrEncodeBlock(in,possibleOut[j][k],k,j,&last1[j][k],&last2[j][k],&avgError[j][k]); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
// find best filter/range |
| 222 |
int candError=0x7fffffff; |
| 223 |
if (i==0) { |
| 224 |
filter=0; |
| 225 |
for (int k=0; k<13; k++) { |
| 226 |
if (avgError[0][k]<candError) { |
| 227 |
candError=avgError[0][k]; |
| 228 |
range=k; |
| 229 |
} |
| 230 |
} |
| 231 |
} else { |
| 232 |
for (int j=0; j<numFilters; j++) { |
| 233 |
for (int k=0; k<13; k++) { |
| 234 |
if (avgError[j][k]<candError) { |
| 235 |
candError=avgError[j][k]; |
| 236 |
filter=j; |
| 237 |
range=k; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
// write |
| 244 |
out[0]=(range<<4)|(filter<<2)|((i+16>=len && loopStart<0)?1:0); |
| 245 |
for (int j=0; j<8; j++) { |
| 246 |
out[j+1]=possibleOut[filter][range][j]; |
| 247 |
} |
| 248 |
|
| 249 |
for (int j=0; j<numFilters; j++) { |
| 250 |
for (int k=0; k<13; k++) { |
| 251 |
last1[j][k]=last1[filter][range]; |
| 252 |
last2[j][k]=last2[filter][range]; |
| 253 |
} |
| 254 |
} |
| 255 |
out+=9; |
| 256 |
total+=9; |
| 257 |
} |
| 258 |
// encode loop block |
| 259 |
if (loopStart>=0 && loopStart<len) { |
| 260 |
long p=loopStart; |
| 261 |
for (int i=0; i<17; i++) { |
| 262 |
if (p>=len) { |
| 263 |
p=loopStart; |
| 264 |
} |
| 265 |
in[i]=buf[p++]; |
| 266 |
} |
| 267 |
|
| 268 |
if (emphasis) { |
| 269 |
for (int j=0; j<17; j++) { |
| 270 |
x0=x1; |
| 271 |
x1=x2; |
| 272 |
x2=in[j]; |
| 273 |
|
| 274 |
if (j==0) continue; |
| 275 |
emphOut=((x1<<11)-x0*370-in[j]*374)/1305; |
| 276 |
if (emphOut<-32768) emphOut=-32768; |
| 277 |
if (emphOut>32767) emphOut=32767; |
| 278 |
in[j-1]=emphOut; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
// encode (filter 0/1 only) |
| 283 |
for (int j=0; j<2; j++) { |
| 284 |
for (int k=0; k<13; k++) { |
| 285 |
brrEncodeBlock(in,possibleOut[j][k],k,j,&last1[j][k],&last2[j][k],&avgError[j][k]); |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
// find best filter/range |
| 290 |
int candError=0x7fffffff; |
| 291 |
for (int j=0; j<2; j++) { |
| 292 |
for (int k=0; k<13; k++) { |
| 293 |
if (avgError[j][k]<candError) { |
| 294 |
candError=avgError[j][k]; |
| 295 |
filter=j; |
| 296 |
range=k; |
| 297 |
} |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
// write |
| 302 |
out[0]=(range<<4)|(filter<<2)|3; |
| 303 |
for (int j=0; j<8; j++) { |
| 304 |
out[j+1]=possibleOut[filter][range][j]; |
| 305 |
} |
| 306 |
|
| 307 |
for (int j=0; j<4; j++) { |
| 308 |
for (int k=0; k<13; k++) { |
| 309 |
last1[j][k]=last1[filter][range]; |
| 310 |
last2[j][k]=last2[filter][range]; |
| 311 |
} |
| 312 |
} |
| 313 |
out+=9; |
| 314 |
total+=9; |
| 315 |
} |
| 316 |
return total; |
| 317 |
} |
| 318 |
|
| 319 |
#define DO_ONE_SAMPLE \ |
| 320 |
if (next&8) next|=0xfffffff0; \ |
| 321 |
\ |
| 322 |
if (buf[0]>=0xd0) { /* invalid shift */ \ |
| 323 |
next=(next<0)?0xfffff800:0; \ |
| 324 |
} else { \ |
| 325 |
next<<=(buf[0]>>4); /* range */ \ |
| 326 |
next>>=1; \ |
| 327 |
} \ |
| 328 |
\ |
| 329 |
switch (control&0xc) { /* filter */ \ |
| 330 |
case 0: \ |
| 331 |
break; \ |
| 332 |
case 4: \ |
| 333 |
next+=last1+((-last1)>>4); \ |
| 334 |
break; \ |
| 335 |
case 8: \ |
| 336 |
next+=last1*2+((-last1*3)>>5)-last2+(last2>>4); \ |
| 337 |
break; \ |
| 338 |
case 12: \ |
| 339 |
next+=last1*2+((-last1*13)>>6)-last2+((last2*3)>>4); \ |
| 340 |
break; \ |
| 341 |
} \ |
| 342 |
\ |
| 343 |
if (next>32767) next=32767; \ |
| 344 |
if (next<-32768) next=-32768; \ |
| 345 |
next&=0x7fff; \ |
| 346 |
if (next&0x4000) next|=0xffff8000; \ |
| 347 |
\ |
| 348 |
last2=last1; \ |
| 349 |
last1=next; \ |
| 350 |
*out=next<<1; \ |
| 351 |
out++; |
| 352 |
|
| 353 |
long brrDecode(unsigned char* buf, short* out, long len, unsigned char emphasis) { |
| 354 |
if (len==0) return 0; |
| 355 |
|
| 356 |
short* outOrig=out; |
| 357 |
|
| 358 |
long total=0; |
| 359 |
|
| 360 |
int last1=0; |
| 361 |
int last2=0; |
| 362 |
int next=0; |
| 363 |
|
| 364 |
// don't read out of bounds |
| 365 |
len-=8; |
| 366 |
|
| 367 |
for (long i=0; i<len; i+=9) { |
| 368 |
unsigned char control=buf[0]; |
| 369 |
|
| 370 |
for (unsigned char j=1; j<9; j++) { |
| 371 |
next=buf[j]>>4; |
| 372 |
DO_ONE_SAMPLE; |
| 373 |
|
| 374 |
next=buf[j]&15; |
| 375 |
DO_ONE_SAMPLE; |
| 376 |
} |
| 377 |
|
| 378 |
// end bit |
| 379 |
total+=16; |
| 380 |
if (control&1) break; |
| 381 |
buf+=9; |
| 382 |
} |
| 383 |
|
| 384 |
if (emphasis) { |
| 385 |
short x0=0; |
| 386 |
short x1=0; |
| 387 |
short x2=0; |
| 388 |
for (long i=0; i<=total; i++) { |
| 389 |
x0=x1; |
| 390 |
x1=x2; |
| 391 |
x2=(i>=total)?0:outOrig[i]; |
| 392 |
|
| 393 |
if (i==0) continue; |
| 394 |
|
| 395 |
outOrig[i-1]=(x0*370+x1*1305+x2*374)>>11; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
return total; |
| 400 |
} |