Export more symbols
[cl-v4l2.git] / videodev2.lisp
blob9bc45a7ce2346b0efaac4cde98b11b7c17c0b343
1 (in-package :cl-v4l2)
3 (defclass v4l2 (standard-class) ())
5 (defmethod validate-superclass ((obj v4l2) (obj1 standard-class)) t)
7 (defmacro define-wrapper (class-and-type supers &optional slots)
8 (destructuring-bind (class-name &optional (struct-type class-name))
9 (cffi::ensure-list class-and-type)
10 (let ((slots (or slots (cffi::foreign-slot-names struct-type))))
11 `(progn
12 (defclass ,class-name ,supers
13 (,@(loop for slot in slots collect
14 `(,slot))
15 (raw :accessor ,(cffi::format-symbol t "~A-RAW" class-name)))
16 (:metaclass v4l2))
18 ,@(loop for slot in slots
19 for slot-name = (cffi::format-symbol t "~A-~A" class-name slot)
20 for slot-type = (cffi::slot-type (cffi::get-slot-info class-name slot))
21 collect
22 `(defun ,slot-name (inst)
23 ,(if (or (eq slot-type :char) (eq slot-type :uchar))
24 `(cffi:convert-from-foreign
25 (cffi:foreign-slot-value (slot-value inst 'raw) ',class-name ',slot) :string)
26 (if (cffi::aggregatep (cffi::parse-type slot-type))
27 `(make-instance ',slot-type
28 :pointer (cffi:foreign-slot-value (slot-value inst 'raw) ',class-name ',slot))
29 `(cffi:foreign-slot-value (slot-value inst 'raw) ',class-name ',slot))))
30 collect
31 `(defun (setf ,slot-name) (new inst)
32 (let ((raw (slot-value inst 'raw)))
33 (setf (cffi:foreign-slot-value raw ',class-name ',slot)
34 (cffi:convert-to-foreign new ',slot-type)))))
36 (defmethod slot-value-using-class ((class v4l2) inst slot)
37 (if (string= (string-upcase (slot-definition-name slot)) "RAW")
38 (call-next-method class inst)
39 (funcall (symbol-function
40 (cffi::format-symbol t "~A-~A"
41 (class-name class)
42 (slot-definition-name slot)))
43 inst)))
45 (defmethod (setf slot-value-using-class) (new (class v4l2) inst slot)
46 (let ((slot-name (string-upcase (slot-definition-name slot))))
47 (if (string= slot-name "RAW")
48 (call-next-method new class inst)
49 (funcall (fdefinition
50 (read-from-string (format nil "(SETF ~A-~A)"
51 (class-name class)
52 slot-name)))
53 new inst))))
55 (defmethod initialize-instance :after ((inst ,class-name) &key pointer)
56 (let ((obj (or pointer (cffi:foreign-alloc ',class-name))))
57 (setf (slot-value inst 'raw) obj)
58 (unless pointer
59 (tg:finalize inst (lambda ()
60 (format t "finalize ~A~%" obj)
61 (foreign-free obj))))))
62 ',class-name))))
64 (defmacro def-c-struct (name &rest args)
65 `(progn
66 (defcstruct ,name
67 ,@args)
68 (define-wrapper ,name ())))
70 (defmacro def-c-union (name &rest args)
71 `(progn
72 (defcunion ,name
73 ,@args)
74 (define-wrapper ,name ())))
76 (def-c-struct v4l2-capability
77 (driver :uchar :count 16)
78 (card :uchar :count 32)
79 (bus-info :uchar :count 32)
80 (version :uint32)
81 (capabilities :uint32)
82 (reserved :uint32 :count 4))
84 (def-c-struct v4l2-fract
85 (numerator :uint32)
86 (denominator :uint32))
88 (def-c-struct v4l2-captureparm
89 (capability :uint32) ; Supported modes
90 (capturemode :uint32) ; Current mode
91 (timeperframe v4l2-fract) ; Time per frame in .1us units
92 (extendedmode :uint32) ; Driver-specific extensions
93 (readbuffers :uint32) ; # of buffers for read
94 (reserved :uint32 :count 4))
96 (def-c-struct v4l2-outputparm
97 (capability :uint32) ; Supported modes
98 (outputmode :uint32) ; Current mode
99 (timeperframe v4l2-fract) ; Time per frame in .1us units
100 (extendedmode :uint32) ; Driver-specific extensions
101 (writebuffers :uint32) ; # of buffers for write
102 (reserved :uint32 :count 4))
104 (def-c-union v4l2-streamparm-union
105 (capture v4l2-captureparm)
106 (output v4l2-outputparm)
107 (raw-data :uchar :count 200))
109 (defcenum v4l2-buf-type
110 (:buf-type-video-capture 1)
111 :buf-type-video-output
112 :buf-type-video-overlay
113 :buf-type-vbi-capture
114 :buf-type-vbi-output
115 :buf-type-sliced-vbi-capture
116 :buf-type-sliced-vbi-output
117 :buf-type-video-output-overlay)
119 (def-c-struct v4l2-streamparm
120 (type v4l2-buf-type)
121 (parm v4l2-streamparm-union))
123 (defcenum v4l2-tuner-type
124 (:tuner-radio 1)
125 :tuner-analog-tv
126 :tuner-digital-tv)
128 (def-c-struct v4l2-tuner
129 (index :uint32)
130 (name :uchar :count 32)
131 (type v4l2-tuner-type)
132 (capability :uint32)
133 (rangelow :uint32)
134 (rangehigh :uint32)
135 (rxsubchans :uint32)
136 (audmode :uint32)
137 (signal :int32)
138 (afc :int32)
139 (reserved :uint32 :count 4))
141 (def-c-struct v4l2-standard
142 (index :uint32)
143 (id :uint64)
144 (name :uchar :count 24)
145 (frameperiod v4l2-fract)
146 (framelines :uint32)
147 (reserved :uint32 :count 4))
149 (def-c-struct v4l2-input
150 (index :uint32) ; Which input
151 (name :uchar :count 32) ; Label
152 (type :uint32) ; Type of input
153 (audioset :uint32) ; Associated audios (bitfield)
154 (tuner :uint32) ; Associated tuner
155 (std :uint64)
156 (status :uint32)
157 (reserved :uint32 :count 4))
160 ;; F O R M A T E N U M E R A T I O N
162 (def-c-struct v4l2-fmtdesc
163 (index :uint32) ; Format number
164 (type v4l2-buf-type) ; buffer type
165 (flags :uint32)
166 (description :uchar :count 32) ; Description string
167 (pixelformat :uint32) ; Format fourcc
168 (reserved :uint32 :count 4))
170 ;; Values for the 'type' field
171 (defconstant v4l2-input-type-tuner 1)
173 (defconstant v4l2-input-type-camera 2)
175 (defcenum v4l2-field
176 :field-any ; driver can choose from none
177 ; top, bottom, interlaced
178 ; depending on whatever it thinks
179 ; is approximate ...
180 :field-none ; this device has no fields ...
181 :field-top ; top field only
182 :field-bottom ; bottom field only
183 :field-interlaced ; both fields interlaced
184 :field-seq-tb ; both fields sequential into one
185 ; buffer, top-bottom order
186 :field-seq-bt ; same as above + bottom-top order
187 :field-alternate ; both fields alternating into
188 ; separate buffers
189 :field-interlaced-tb ; both fields interlaced, top field
190 ; first and the top field is
191 ; transmitted first
192 :field-interlaced-bt ; both fields interlaced, top field
193 ; first and the bottom field is
194 ; transmitted first
197 (defcenum v4l2-colorspace
198 (:colorspace-smpte170m 1) ; ITU-R 601 -- broadcast NTSC/PAL
199 :colorspace_smpte240m ; 1125-Line (US) HDTV
200 :colorspace-rec709 ; HD and modern captures.
201 ; broken BT878 extents (601, luma range 16-253 instead of 16-235)
202 :colorspace-bt878
203 ; These should be useful. Assume 601 extents.
204 :colorspace-470-system-m
205 :colorspace-470-system-bg
207 ; I know there will be cameras that send this. So, this is
208 ; unspecified chromaticities and full 0-255 on each of the
209 ; Y'CbCr components
210 :colorspace-jpeg
212 ; For RGB colourspaces, this is probably a good start.
213 :colorspace-srgb)
216 ;; V I D E O I M A G E F O R M A T
218 (def-c-struct v4l2-pix-format
219 (width :uint32)
220 (height :uint32)
221 (pixelformat :uint32)
222 (field v4l2-field)
223 (bytesperline :uint32) ; for padding, zero if unused
224 (sizeimage :uint32)
225 (colorspace v4l2-colorspace)
226 (priv :uint32)) ; private data, depends on pixelformat
228 ;; Stream data format
231 (def-c-struct v4l2-timecode
232 (type :uint32)
233 (flags :uint32)
234 (frames :uchar)
235 (seconds :uchar)
236 (minutes :uchar)
237 (hours :uchar)
238 (userbits :uchar :count 4))
240 (defcenum v4l2-memory
241 (:memory-mmap 1)
242 :memory-userptr
243 :memory-overlay)
245 ;; M E M O R Y - M A P P I N G B U F F E R S
247 (def-c-struct v4l2-requestbuffers
248 (count :uint32)
249 (type v4l2-buf-type)
250 (memory v4l2-memory)
251 (reserved :uint32 :count 2))
253 (def-c-union v4l2-buffer-union
254 (offset :uint32)
255 (userptr :ulong))
257 (def-c-struct v4l2-buffer
258 (index :uint32)
259 (type v4l2-buf-type)
260 (bytesused :uint32)
261 (flags :uint32)
262 (field v4l2-field)
263 (timestamp timeval)
264 (timecode v4l2-timecode)
265 (sequence :uint32)
266 ; memory location
267 (memory v4l2-memory)
268 (m v4l2-buffer-union)
269 (length :uint32)
270 (input :uint32)
271 (reserved :uint32))
273 (defcenum v4l2-ctrl-type
274 (:ctrl-type-integer 1)
275 :ctrl-type-boolean
276 :ctrl-type-menu
277 :ctrl-type-button
278 :ctrl-type-integer64
279 :ctrl-type-ctrl-class)
281 ;; Used in the VIDIOC_QUERYCTRL ioctl for querying controls
282 (def-c-struct v4l2-queryctrl
283 (id :uint32)
284 (type v4l2-ctrl-type)
285 (name :uchar :count 32)
286 (minimum :int32)
287 (maximum :int32)
288 (step :int32)
289 (default-value :int32)
290 (flags :uint32)
291 (reserved :uint32 :count 2))
293 (def-c-struct v4l2-control
294 (id :uint32)
295 (value :int32))
297 (defcenum v4l2-power-line-frequency
298 :cid-power-line-frequency-disabled
299 :cid-power-line-frequency-50hz
300 :cid-power-line-frequency-60hz)
302 (defcenum v4l2-colorfx
303 :colorfx-none
304 :colorfx-bw
305 :colorfx-sepia)
307 (defcenum v4l2-exposure-auto-type
308 :exposure-auto
309 :exposure-manual
310 :exposure-shutter-priority
311 :exposure-aperture-priority)