anchor.diff:    (loop for chunk on (chunk-queue s) do
          (setf (car chunk) (make-instance 'chunk :frame (make-array (* (width s) (height s)) :element-type '(unsigned-byte 8)))))
     (setf (chunk-decoder s) #'(lambda (stream id size)
  -			      (declare (ignorable id))
  -			      (when (> size (length (frame (wcursor s))))
  -				(setf (frame (wcursor s)) (make-array size :element-type '(unsigned-byte 8))))
  -			      (cond ((string= (subseq id 2) "dc")
  -				     (setf (stream-number (frame (wcursor s))) (parse-integer (subseq id 0 1)))
  -				     (jpeg:decode-stream stream :buffer (frame (wcursor s))
  -							 :descriptor (jpeg-descriptor s))
  +			      (print id)
  +			      (cond ((string-equal (subseq id 2) "dc")
  +				     (bt:with-lock-held ((vacancy-lock (car (wcursor s))))
  +				       (setf (stream-number (car (wcursor s))) (parse-integer (subseq id 0 1)))
  +				       (jpeg:decode-stream stream :buffer (frame (car (wcursor s)))
  +							   :descriptor (jpeg-descriptor s))
  +				       (pop (wcursor s)))
   				     ;; take care of the padding
  -				     (loop repeat (rem size (padding s)) do (read-byte s)))
  +				       (loop repeat (padding s) do (read-byte s)))
   				    (t (read-sequence (make-array size :element-type '(unsigned-byte 8)) stream))))
   	(cdr (last (chunk-queue s))) (chunk-queue s)
   	(rcursor s) (chunk-queue s)
  
positive.code:    (height :accessor height :initarg :height :initform 480)
     (jpeg-descriptor :accessor jpeg-descriptor :initform (cl-jpeg::make-descriptor))
     (padding :accessor padding :initform 1)
     (fps :accessor fps :initarg :fps :initform +default-fps+)))
  
  (defmethod initialize-instance :after ((s avi-mjpeg-stream) &key)
  
negative.code: (defgeneric decode (stream)
    (:documentation "Decodes the video stream"))
  
  (defun read-avi-header (avi stream)
    (loop for chunk = (riff:read-riff-chunk stream)
       when (string-equal (riff:riff-chunk-id chunk) "avih") do
         (flexi-streams:with-input-from-sequence (is (riff:riff-chunk-data chunk))
  	 (riff:read-u4 is)   ;;dwMicroSecPerFrame
  	 (riff:read-u4 is)   ;;dwMaxBytesPerSec
  	 (setf (padding avi) (riff:read-u4 is)
  	       (flags avi) (riff:read-u4 is))
  	 (riff:read-u4 is)   ;;dwTotalFrames
  	 (riff:read-u4 is)   ;;dwInitialFrames
  	 (setf (nstreams avi) (riff:read-u4 is))
  	 (riff:read-u4 is) ;;dwSuggestedBufferSize
  	 (setf (width avi) (riff:read-u4 is)
  	       (height avi) (riff:read-u4 is))
  	 (riff:read-u4 is) (riff:read-u4 is) (riff:read-u4 is) (riff:read-u4 is))
         (return)))
  
  (defmethod decode ((avi avi-mjpeg-stream))
    (with-open-file (stream (filename avi) :direction :input :element-type '(unsigned-byte 8))
      ;; read AVI header first
      (let* ((chunk (riff:read-riff-chunk stream))
  	   (id (getf chunk :chunk-id))
  	   (fourcc (getf chunk :file-type)))
        (unless (string-equal id "riff")
  	(error 'unrecognized-file-format))
        (cond ((string-equal fourcc "avi ") (read-avi-header avi stream))
  	    (t (error 'unsupported-avi-file-format))))
      (loop for chunk = (riff:read-riff-chunk stream :chunk-data-reader (chunk-decoder avi))
  	 while chunk)))
  
  (defun decode-file (pathname)
    (let ((avi-stream (make-instance 'avi-mjpeg-stream :filename pathname)))
      (decode avi-stream)))
  
  (defun show-file-chunks (pathname)
    (with-open-file (stream pathname :direction :input :element-type '(unsigned-byte 8))
      (loop for chunk = (riff:read-riff-chunk stream)
  	 while chunk do (format t "~A size ~D~%" (riff:riff-chunk-id chunk) (riff:riff-chunk-data-size chunk)))))
  