Beispiel #1
0
func riceDecode(br *bit.Reader, n int, M uint) ([]int32, error) {
	ns := make([]int32, n)
	for i := 0; i < n; i++ {
		var q uint64
		for {
			switch b, err := br.Read(1); {
			case err != nil:
				return nil, err
			case b == 0:
				q++
				continue
			}
			break
		}

		u, err := br.Read(M)
		if err != nil {
			return nil, err
		}

		u |= (q << M)
		ns[i] = int32(u>>1) ^ -int32(u&1)
	}
	return ns, nil
}
Beispiel #2
0
func decodeLPCSubFrame(br *bit.Reader, sampleSize uint, blkSize int, predO int) ([]int32, error) {
	warm, err := readInts(br, predO, sampleSize)
	if err != nil {
		return nil, err
	}

	prec, err := br.Read(4)
	if err != nil {
		return nil, err
	} else if prec == 0xF {
		return nil, errors.New("Bad LPC predictor precision")
	}
	prec++

	s, err := br.Read(5)
	if err != nil {
		return nil, err
	}
	shift := int(signExtend(s, 5))
	if shift < 0 {
		return nil, errors.New("Invalid negative shift")
	}

	coeffs, err := readInts(br, predO, uint(prec))
	if err != nil {
		return nil, err
	}

	residual, err := decodeResiduals(br, blkSize, predO)
	if err != nil {
		return nil, err
	}

	return lpcDecode(coeffs, warm, residual, uint(shift)), nil
}
Beispiel #3
0
func utf8Decode(br *bit.Reader) (uint64, error) {
	left := 0
	v := uint64(0)

	switch b0, err := br.Read(8); {
	case err != nil:
		return 0, err

	// 0xxx xxxx
	case b0&0x80 == 0:
		return uint64(b0 & 0x7F), nil

	// 110x xxxx	10xx xxxx
	case b0&0xE0 == 0xC0:
		left = 1
		v = uint64(b0 & 0x1F)

	// 1110 xxxx	10xx xxxx	10xx xxxx
	case b0&0xF0 == 0xE0:
		left = 2
		v = uint64(b0 & 0xF)

	// 1111 0xxx	10xx xxxx	10xx xxxx	10xx xxxx
	case b0&0xF8 == 0xF0:
		left = 3
		v = uint64(b0 & 0x7)

	// 1111 10xx	10xx xxxx	10xx xxxx	10xx xxxx	10xx xxxx
	case b0&0xFC == 0xF8:
		left = 4
		v = uint64(b0 & 0x3)

	// 1111 110x	10xx xxxx	10xx xxxx	10xx xxxx	10xx xxxx	10xx xxxx
	case b0&0xFE == 0xFC:
		left = 5
		v = uint64(b0 & 0x1)
	}

	for n := 0; n < left; n++ {
		switch b, err := br.Read(8); {
		case err == io.EOF:
			return 0, io.ErrUnexpectedEOF

		case err != nil:
			return 0, err

		case b&0xC0 != 0x80:
			return 0, errors.New("Bad UTF-8 encoding in frame header")

		default:
			v = (v << 6) | (b & 0x3F)
		}
	}

	return v, nil
}
Beispiel #4
0
func readInts(br *bit.Reader, n int, bits uint) ([]int32, error) {
	is := make([]int32, n)
	for i := range is {
		w, err := br.Read(bits)
		if err != nil {
			return nil, err
		}
		is[i] = signExtend(w, bits)
	}
	return is, nil
}
Beispiel #5
0
func readSubFrame(br *bit.Reader, h *frameHeader, ch int) ([]int32, error) {
	var data []int32
	bps := h.bitsPerSample(ch)

	kind, order, err := readSubFrameHeader(br)
	if err != nil {
		return nil, err
	}
	switch kind {
	case subFrameConstant:
		v, err := br.Read(bps)
		if err != nil {
			return nil, err
		}
		u := signExtend(v, bps)
		data = make([]int32, h.blockSize)
		for j := range data {
			data[j] = u
		}

	case subFrameVerbatim:
		data = make([]int32, h.blockSize)
		for j := range data {
			v, err := br.Read(bps)
			if err != nil {
				return nil, err
			}
			data[j] = signExtend(v, bps)
		}

	case subFrameFixed:
		data, err = decodeFixedSubFrame(br, bps, h.blockSize, order)
		if err != nil {
			return nil, err
		}

	case subFrameLPC:
		data, err = decodeLPCSubFrame(br, bps, h.blockSize, order)
		if err != nil {
			return nil, err
		}

	default:
		return nil, errors.New("Unsupported frame kind")
	}

	return data, nil
}
Beispiel #6
0
func readSubFrameHeader(br *bit.Reader) (kind subFrameKind, order int, err error) {
	switch pad, err := br.Read(1); {
	case err != nil:
		return 0, 0, err
	case pad != 0:
		// Do nothing, but this is a bad padding value.
	}

	switch k, err := br.Read(6); {
	case err != nil:
		return 0, 0, err

	case k == 0:
		kind = subFrameConstant

	case k == 1:
		kind = subFrameVerbatim

	case (k&0x3E == 0x02) || (k&0x3C == 0x04) || (k&0x30 == 0x10):
		return 0, 0, errors.New("Bad subframe type")

	case k&0x38 == 0x08:
		if order = int(k & 0x07); order > 4 {
			return 0, 0, errors.New("Bad subframe type")
		}
		kind = subFrameFixed

	case k&0x20 == 0x20:
		order = int(k&0x1F) + 1
		kind = subFrameLPC

	default:
		return 0, 0, errors.New("Invalid subframe type")
	}

	n := 0
	switch k, err := br.Read(1); {
	case err != nil:
		return 0, 0, err

	case k == 1:
		n++
		k = uint64(0)
		for k == 0 {
			if k, err = br.Read(1); err != nil {
				return 0, 0, err
			}
			n++
		}
	}

	return kind, order, nil
}
Beispiel #7
0
func decodeResiduals(br *bit.Reader, blkSize int, predO int) ([]int32, error) {
	var bits uint

	switch method, err := br.Read(2); {
	case err != nil:
		return nil, err
	case method == 0:
		bits = 4
	case method == 1:
		bits = 5
	default:
		return nil, errors.New("Bad residual method")
	}

	partO, err := br.Read(4)
	if err != nil {
		return nil, err
	}

	var residue []int32
	for i := 0; i < 1<<partO; i++ {
		M, err := br.Read(bits)
		if err != nil {
			return nil, err
		} else if (bits == 4 && M == 0xF) || (bits == 5 && M == 0x1F) {
			return nil, errors.New("Unsupported, unencoded residuals")
		}

		n := 0
		switch {
		case partO == 0:
			n = blkSize - predO
		case i > 0:
			n = blkSize / (1 << partO)
		default:
			n = (blkSize / (1 << partO)) - predO
		}

		r, err := riceDecode(br, n, uint(M))
		if err != nil {
			return nil, err
		}
		residue = append(residue, r...)
	}
	return residue, nil
}