-
Notifications
You must be signed in to change notification settings - Fork 1
/
v4l.go
213 lines (184 loc) · 5.38 KB
/
v4l.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Package v4l gives access to V4L (Video For Linux).
// It accessess v4l directly via open(2) and ioctl(2).
// It does not use cgo wrappings of the C v4l library.
package v4l
import (
"fmt"
"image"
"log"
"os"
"reflect"
"sync"
"syscall"
"unsafe"
)
// A Format is one of the pixel formats specified by V4L.
// Only pixel formats supported by this package are here.
type Format uint32
const (
V4L2_PIX_FMT_UYVY Format = 0x59565955 // 'UYVY', little endian
)
type imgInfo struct {
bitsPerPixel int
subsample image.YCbCrSubsampleRatio
}
var infoMap = map[Format]imgInfo{
V4L2_PIX_FMT_UYVY: {16, image.YCbCrSubsampleRatio422},
}
// A Device holds the state of a connection to a video device.
// Each Device can have at most one stream running.
type Device struct {
path string
f *os.File
wg sync.WaitGroup
ch chan image.Image
}
type FrameFormat struct {
Format Format
Width, Height int
rect image.Rectangle
}
func Open(path string) (dev *Device, err error) {
dev = &Device{path: path}
dev.f, err = os.Open(dev.path)
return
}
// Close closes the underlying file handle to the V4L
// device. It stops any streams in progress, and waits
// for any goroutines to exit.
func (dev *Device) Close() {
dev.f.Close()
dev.f = nil
// This is just to be sure that if the goroutine
// does not exit, the user becomes aware (because
// Close() hangs).
dev.wg.Wait()
}
// Stream configures the device according to the provided FrameFormat.
// Stream returns a channel of Images. The channel is buffered
// so that if the consumer does not consume new images, new ones are
// lost. FrameFormat is validated, and may result in Stream
// returning an error if the frame format is not supported.
//
// It is an error to call Stream on a Device more than once.
//
// Stream starts a goroutine to collect frames from the Device.
// The goroutine exits when Close is called on the Device.
func (dev *Device) Stream(ff FrameFormat) (chan image.Image, error) {
if dev.ch != nil {
return nil, fmt.Errorf("A stream is already running on this device.")
}
if dev.f == nil {
return nil, fmt.Errorf("Device is not open.")
}
ff.rect = image.Rect(0, 0, ff.Width, ff.Height)
imgInfo, ok := infoMap[ff.Format]
if !ok {
return nil, fmt.Errorf("Frame format not supported.")
}
imageSize := imgInfo.bitsPerPixel / 8 * ff.Width * ff.Height
// Setup V4L driver: format and kern<->user transfer method
err := dev.setFormat(ff)
if err != nil {
return nil, err
}
err = dev.setUserptr()
if err != nil {
return nil, err
}
dev.ch = make(chan image.Image, 1)
dev.wg.Add(1)
go func() {
frame := make([]byte, imageSize)
for {
req := v4l2_buffer{
Type: _V4L2_BUF_TYPE_VIDEO_CAPTURE,
Memory: _V4L2_MEMORY_USERPTR,
Userptr: uint32(where(frame)),
Length: uint32(len(frame)),
}.asBytes()
// Enqueue the buffer.
err := ioctl(dev.f.Fd(), _VIDIOC_QBUF, req)
if err != nil {
log.Print("qbuf error:", err)
break
}
// Dequeue the same buffer, now filled: the ioctl blocks until
// the next frame is available.
err = ioctl(dev.f.Fd(), _VIDIOC_DQBUF, req)
if err != nil {
log.Print("dqbuf error:", err)
break
}
im := image.NewYCbCr(ff.rect, imgInfo.subsample)
frameToImage(frame, im)
dev.ch <- im
}
close(dev.ch)
dev.wg.Done()
}()
return dev.ch, nil
}
// frameToImage copies frame into image
func frameToImage(frame []byte, im *image.YCbCr) {
// Format UVUY into Y, Cb and Cr planes
// http://linuxtv.org/downloads/v4l-dvb-apis/V4L2-PIX-FMT-UYVY.html
// U = Cb, V = Cr
y, br := 0, 0
for i := 0; i < len(frame); i += 4 {
im.Cb[br] = frame[i+0]
im.Y[y] = frame[i+1]
im.Cr[br] = frame[i+2]
im.Y[y+1] = frame[i+3]
br += 1
y += 2
}
}
// setUserptr tells the kernel driver to expect us to allocate buffers
func (dev *Device) setUserptr() error {
rb := v4l2_requestbuffers{
Type: _V4L2_BUF_TYPE_VIDEO_CAPTURE,
Memory: _V4L2_MEMORY_USERPTR,
}.asBytes()
return ioctl(dev.f.Fd(), _VIDIOC_REQBUFS, rb)
}
// setFormat applies the FrameFormat to the Device.
func (dev *Device) setFormat(ff FrameFormat) error {
return ioctl(dev.f.Fd(), _VIDIOC_S_FMT, ff.req())
}
// req formats a FrameFormat into a v4l2_pix_format, and then into a []byte,
// ready to be used by ioctl.
func (ff FrameFormat) req() []byte {
return v4l2_pix_format{
Type: uint32(_V4L2_BUF_TYPE_VIDEO_CAPTURE),
Width: uint32(ff.Width),
Height: uint32(ff.Height),
Pixelformat: uint32(ff.Format),
}.asBytes()
}
// where returns the pointer to the data of the slice
func where(in []byte) uintptr {
return (*reflect.SliceHeader)(unsafe.Pointer(&in)).Data
}
func ioctl(fd uintptr, req uintptr, arg []byte) error {
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, req, where(arg))
if e != 0 {
return os.NewSyscallError("ioctl", e)
}
return nil
}
// Utilities for allocating page-aligned buffers
var pageSize = os.Getpagesize()
// allocPageAligned returns a []byte where underlying buffer is
// page aligned.
func allocPageAligned(size int) []byte {
// Make a slice with underlying storage 1 page bigger than is requested.
outer := make([]byte, size+pageSize)
// find out how far we need to move forward in the underlying buffer
// in order to be page aligned
toNextPage := pageSize - int(where(outer)%uintptr(pageSize))
// reslice the outer slice, resulting in an inner one
// which is page aligned
inner := outer[toNextPage : toNextPage+size]
return inner
}