3 ;; Generate wrapper classes for all structures.
4 ;; Slot access is overtaken via MOP. This allows to utilize usual `with-slots'
5 ;; macro. This is slow, 'cause it uses hash lookup tables.
7 ;; Better access speed can be achieved using generated accessors
8 ;; named like <class-name>-<slot-name>, e.g. `capability-driver'.
10 (defclass v4l2
(standard-class) ())
12 (defmethod validate-superclass ((obj v4l2
) (obj1 standard-class
)) t
)
14 (defvar *v4l2-slot-readers
* (make-hash-table :test
'equal
))
15 (defvar *v4l2-slot-writers
* (make-hash-table :test
'equal
))
17 (defmethod slot-value-using-class ((class v4l2
) inst slot
)
18 (if (string= (string-upcase (slot-definition-name slot
)) "RAW")
19 (call-next-method class inst
)
20 (funcall (gethash (cons (class-name class
) (slot-definition-name slot
))
24 (defmethod (setf slot-value-using-class
) (new (class v4l2
) inst slot
)
25 (if (string= (string-upcase (slot-definition-name slot
)) "RAW")
26 (call-next-method new class inst
)
27 (funcall (gethash (cons (class-name class
) (slot-definition-name slot
))
31 (defmacro define-wrapper
(class-and-type supers
&optional slots
)
32 (destructuring-bind (class-name &optional
(struct-type class-name
))
33 (cffi::ensure-list class-and-type
)
34 (let ((slots (or slots
(cffi::foreign-slot-names struct-type
)))
35 (raw-accessor (cffi::format-symbol t
"~A-RAW" class-name
)))
37 (defclass ,class-name
,supers
38 (,@(loop for slot in slots collect
39 `(,slot
:initarg
,(intern (string-upcase slot
) "KEYWORD")))
40 (raw :accessor
,raw-accessor
))
43 ,@(loop for slot in slots
44 for slot-name
= (cffi::format-symbol t
"~A-~A" class-name slot
)
45 for slot-type
= (cffi::slot-type
(cffi::get-slot-info class-name slot
))
47 `(defun ,slot-name
(inst)
48 ,(if (or (eq slot-type
:char
) (eq slot-type
:uchar
))
49 `(convert-from-foreign
50 (foreign-slot-value (,raw-accessor inst
) ',class-name
',slot
) :string
)
51 (if (cffi::aggregatep
(cffi::parse-type slot-type
))
52 `(make-instance ',slot-type
53 :pointer
(foreign-slot-value (,raw-accessor inst
) ',class-name
',slot
))
54 `(foreign-slot-value (,raw-accessor inst
) ',class-name
',slot
))))
56 `(setf (gethash (cons ',class-name
',slot
) *v4l2-slot-readers
*)
57 (fdefinition ',slot-name
))
60 `(defun (setf ,slot-name
) (new inst
)
61 (setf (foreign-slot-value (,raw-accessor inst
) ',class-name
',slot
)
62 (convert-to-foreign new
',slot-type
)))
64 `(setf (gethash (cons ',class-name
',slot
) *v4l2-slot-writers
*)
65 (fdefinition '(setf ,slot-name
))))
67 (defmethod initialize-instance :before
((inst ,class-name
) &key pointer
)
68 (let ((obj (or pointer
(foreign-alloc ',class-name
))))
69 (setf (,raw-accessor inst
) obj
)
71 (finalize inst
(lambda ()
72 (cl:format t
"finalize ~A~%" obj
)
73 (foreign-free obj
))))))
76 (defmacro def-c-struct
(name &rest args
)
77 "Define cffi struct and generate wrapper"
81 (define-wrapper ,name
())))
83 (defmacro def-c-union
(name &rest args
)
84 "Define cffi union and generate wrapper"
88 (define-wrapper ,name
())))
90 (def-c-struct capability
91 (driver :uchar
:count
16)
92 (card :uchar
:count
32)
93 (bus-info :uchar
:count
32)
95 (capabilities :uint32
)
96 (reserved :uint32
:count
4))
100 (denominator :uint32
))
102 (def-c-struct captureparm
103 (capability :uint32
) ; Supported modes
104 (capturemode :uint32
) ; Current mode
105 (timeperframe fract
) ; Time per frame in .1us units
106 (extendedmode :uint32
) ; Driver-specific extensions
107 (readbuffers :uint32
) ; # of buffers for read
108 (reserved :uint32
:count
4))
110 (def-c-struct outputparm
111 (capability :uint32
) ; Supported modes
112 (outputmode :uint32
) ; Current mode
113 (timeperframe fract
) ; Time per frame in .1us units
114 (extendedmode :uint32
) ; Driver-specific extensions
115 (writebuffers :uint32
) ; # of buffers for write
116 (reserved :uint32
:count
4))
118 (def-c-union streamparm-union
119 (capture captureparm
)
121 (raw-data :uchar
:count
200))
124 (:buf-type-video-capture
1)
125 :buf-type-video-output
126 :buf-type-video-overlay
127 :buf-type-vbi-capture
129 :buf-type-sliced-vbi-capture
130 :buf-type-sliced-vbi-output
131 :buf-type-video-output-overlay
)
133 (def-c-struct streamparm
135 (parm streamparm-union
))
144 (name :uchar
:count
32)
153 (reserved :uint32
:count
4))
155 (def-c-struct standard
158 (name :uchar
:count
24)
161 (reserved :uint32
:count
4))
164 (index :uint32
) ; Which input
165 (name :uchar
:count
32) ; Label
166 (type :uint32
) ; Type of input
167 (audioset :uint32
) ; Associated audios (bitfield)
168 (tuner :uint32
) ; Associated tuner
171 (reserved :uint32
:count
4))
174 ;; F O R M A T E N U M E R A T I O N
176 (def-c-struct fmtdesc
177 (index :uint32
) ; Format number
178 (type buf-type
) ; buffer type
180 (description :uchar
:count
32) ; Description string
181 (pixelformat :uint32
) ; Format fourcc
182 (reserved :uint32
:count
4))
184 ;; Values for the 'type' field
185 (defconstant input-type-tuner
1)
187 (defconstant input-type-camera
2)
190 :field-any
; driver can choose from none
191 ; top, bottom, interlaced
192 ; depending on whatever it thinks
194 :field-none
; this device has no fields ...
195 :field-top
; top field only
196 :field-bottom
; bottom field only
197 :field-interlaced
; both fields interlaced
198 :field-seq-tb
; both fields sequential into one
199 ; buffer, top-bottom order
200 :field-seq-bt
; same as above + bottom-top order
201 :field-alternate
; both fields alternating into
203 :field-interlaced-tb
; both fields interlaced, top field
204 ; first and the top field is
206 :field-interlaced-bt
; both fields interlaced, top field
207 ; first and the bottom field is
212 (:colorspace-smpte170m
1) ; ITU-R 601 -- broadcast NTSC/PAL
213 :colorspace_smpte240m
; 1125-Line (US) HDTV
214 :colorspace-rec709
; HD and modern captures.
215 ; broken BT878 extents (601, luma range 16-253 instead of 16-235)
217 ; These should be useful. Assume 601 extents.
218 :colorspace-470-system-m
219 :colorspace-470-system-bg
221 ; I know there will be cameras that send this. So, this is
222 ; unspecified chromaticities and full 0-255 on each of the
226 ; For RGB colourspaces, this is probably a good start.
230 ;; V I D E O I M A G E F O R M A T
232 (def-c-struct pix-format
235 (pixelformat :uint32
)
237 (bytesperline :uint32
) ; for padding, zero if unused
239 (colorspace colorspace
)
240 (priv :uint32
)) ; private data, depends on pixelformat
242 ;; Stream data format
245 (def-c-struct timecode
252 (userbits :uchar
:count
4))
259 ;; M E M O R Y - M A P P I N G B U F F E R S
261 (def-c-struct requestbuffers
265 (reserved :uint32
:count
2))
267 (def-c-union buffer-union
277 (timestamp isys
:timeval
)
288 (:ctrl-type-integer
1)
293 :ctrl-type-ctrl-class
)
295 ;; Used in the VIDIOC_QUERYCTRL ioctl for querying controls
296 (def-c-struct queryctrl
299 (name :uchar
:count
32)
303 (default-value :int32
)
305 (reserved :uint32
:count
2))
307 (def-c-struct control
311 (defcenum power-line-frequency
312 :cid-power-line-frequency-disabled
313 :cid-power-line-frequency-50hz
314 :cid-power-line-frequency-60hz
)
321 (defcenum exposure-auto-type
324 :exposure-shutter-priority
325 :exposure-aperture-priority
)