purpl3F0x / MQA_identifier

Master Quality Authenticated codec reverse engineering, Tool to identify MQA encoding and Master's Sample Rate
Apache License 2.0
114 stars 10 forks source link

"MQB" - Preserve MQA bits for DSP #7

Closed purpl3F0x closed 3 years ago

purpl3F0x commented 3 years ago

Possible, that this code is enough for preserving MQA signaling on a first unfolded stream and doing DSP on it. Something Like:

MQA Decoder -> MQBSlice -> DSP -> Copy -> ... -> DAC as MQA renderer
                  >------------------^

namespace MQBUtils {

unsigned* Alloc(unsigned n) {
  return  (unsigned*)new unsigned[(n + 31) / 32];
}

unsigned* MQBSlice(unsigned* mqb, int off, int count) {
  unsigned* numArray = MQBAlloc(count);
  for (auto i = 0u; i < count; i++) {
    int num = (off + i) / 32;
    int num1 = (off + i) % 32;
    int num2 = i / 32;
    int num3 = i % 32;

    if (((unsigned long)mqb[num] & (unsigned long)(1 << (num1 & 31))) == 0) {
      numArray[num2] &= ~(1 << (num3 & 31));
    } else {
      numArray[num2] = numArray[num2] | 1 << (num3 & 31);
    }
  }

  return numArray;
}

void Copy(unsigned* src, int src_off, unsigned* dst, int dst_off, int count,
          int dst_size) {
  unsigned num;
  for (int i = 0; i < count; i++) {
    int srcOff = (src_off + i) / 32;
    int srcOff1 = (src_off + i) % 32;
    int dstOff = (dst_off + i) / 32;
    int dstOff1 = (dst_off + i) % 32;

    if (srcOff < (int)dst_size) {
      num = src[srcOff];
    } else {
      num = 0;
    }

    if (((unsigned long)num & (long)(1 << (srcOff1 & 31))) == 0) {
      dst[dstOff] &= ~(1 << (dstOff1 & 31));
    } else {
      dst[dstOff] = dst[dstOff] | 1 << (dstOff1 & 31);
    }
  }
}

}  // namespace MQBUtils