Skip to content
Browse files

ogg-page: Add reading more than 8 bits at once.

  • Loading branch information...
1 parent c251dc3 commit 0d83ffdd136c3a8f3f7fb0993c6bde7df9e06642 @stassats committed Mar 11, 2012
Showing with 25 additions and 21 deletions.
  1. +25 −21 ogg-page.lisp
View
46 ogg-page.lisp
@@ -127,24 +127,28 @@
(plusp (read-n-bits 1 stream)))
(defun read-n-bits (n stream)
- (let ((position (ogg-page-position stream)))
- (when (= position (ogg-page-length stream))
- (setf position 0)
- (refill-stream stream))
- (let ((data (data (ogg-page stream)))
- (bits-left (bits-left stream)))
- (prog1
- (if (> n bits-left)
- (logior (ldb (byte (min n bits-left) (- 8 bits-left))
- (aref data position))
- (ash (ldb (byte (- n bits-left) 0)
- (aref data (1+ position)))
- bits-left))
- (ldb (byte (min n bits-left) (- 8 bits-left))
- (aref data position)))
- (cond ((> n bits-left)
- (incf (ogg-page-position stream))
- (setf (bits-left stream)
- (- 8 (- n bits-left))))
- (t
- (decf (bits-left stream) n)))))))
+ (cond ((> n 8)
+ (multiple-value-bind (quot rem) (floor n 8)
+ (logior (read-n-bits rem stream))))
+ (t
+ (let ((position (ogg-page-position stream)))
+ (when (= position (ogg-page-length stream))
+ (setf position 0)
+ (refill-stream stream))
+ (let ((data (data (ogg-page stream)))
+ (bits-left (bits-left stream)))
+ (prog1
+ (if (> n bits-left)
+ (logior (ldb (byte (min n bits-left) (- 8 bits-left))
+ (aref data position))
+ (ash (ldb (byte (- n bits-left) 0)
+ (aref data (1+ position)))
+ bits-left))
+ (ldb (byte (min n bits-left) (- 8 bits-left))
+ (aref data position)))
+ (cond ((> n bits-left)
+ (incf (ogg-page-position stream))
+ (setf (bits-left stream)
+ (- 8 (- n bits-left))))
+ (t
+ (decf (bits-left stream) n)))))))))

0 comments on commit 0d83ffd

Please sign in to comment.
Something went wrong with that request. Please try again.