-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
119 lines (99 loc) · 2.39 KB
/
format.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
package camera
import (
"encoding/binary"
)
type Compression string
type CompressionQuality int64
const (
CompressionUndefined = Compression("")
CompressionAuto = Compression("*")
CompressionMJPEG = Compression("MJPG")
CompressionHEIC = Compression("HEIC")
)
type PixelFormat string
const (
PixelFormatUndefined = PixelFormat("")
PixelFormatAuto = PixelFormat("*")
// Raw formats:
PixelFormatNV12 = PixelFormat("NV12") // https://www.kernel.org/doc/html/v4.10/media/uapi/v4l/pixfmt-nv12.html
PixelFormatYU12 = PixelFormat("YU12") // https://www.kernel.org/doc/html/v4.10/media/uapi/v4l/pixfmt-yuv420.html
PixelFormatYUYV = PixelFormat("YUYV") // https://www.kernel.org/doc/html/v4.10/media/uapi/v4l/pixfmt-yuyv.html
)
func PixelFormatByName(pixFmtName string) PixelFormat {
return PixelFormat(pixFmtName)
}
func (pixFmt PixelFormat) Uint32() uint32 {
return binary.NativeEndian.Uint32([]byte(pixFmt))
}
func (pixFmt PixelFormat) rawBitSize() uint32 {
switch pixFmt {
case PixelFormatYUYV:
return 16
case PixelFormatNV12:
return 12
}
return 0
}
func PixelFormatFromUint32(v uint32) PixelFormat {
var value [4]byte
binary.NativeEndian.PutUint32(value[:], v)
return PixelFormat(value[:])
}
type Format struct {
Width uint64
Height uint64
PixelFormat PixelFormat
FPS Fraction
}
type Formats []Format
func (s Formats) FilterByPixelFormat(pixFmt PixelFormat) Formats {
var result Formats
for _, f := range s {
if f.PixelFormat == pixFmt {
result = append(result, f)
}
}
return result
}
func (s Formats) FilterByWidth(width uint64) Formats {
var result Formats
for _, f := range s {
if f.Width == width {
result = append(result, f)
}
}
return result
}
func (s Formats) FilterByFPS(fps float64) Formats {
var result Formats
for _, f := range s {
if f.FPS.Float64() == fps {
result = append(result, f)
}
}
return result
}
func (s Formats) BestResolution() Format {
var best Format
for _, f := range s {
if f.Width*f.Height != best.Width*best.Height {
if f.Width*f.Height > best.Width*best.Height {
best = f
}
continue
}
if f.FPS.Float64() != best.FPS.Float64() {
if f.FPS.Float64() > best.FPS.Float64() {
best = f
}
continue
}
if f.PixelFormat.rawBitSize() != best.PixelFormat.rawBitSize() {
if f.PixelFormat.rawBitSize() > best.PixelFormat.rawBitSize() {
best = f
}
continue
}
}
return best
}