anchor.diff:  	     (declare (ignorable condition))
   	     (format stream "Unrecognized AVI file format"))))
   
  -(define-condition unsupported-file-format (media-decoder-error)
  +(define-condition unsupported-avi-file-format (media-decoder-error)
     ()
     (:report (lambda (condition stream)
   	     (declare (ignorable condition))
   	     (format stream "Unsupported AVI file format"))))
   
   (defclass chunk ()
  -  ((stream-number :accessor stream-number)
  +  ((lock :reader vacancy-lock :initform (bt:make-lock))
  +   (stream-number :accessor stream-number)
      (frame :accessor frame :initarg :frame)))
   
   (defclass video-stream ()
  
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)))))
  