-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.go
More file actions
91 lines (80 loc) · 2.12 KB
/
Copy pathcolor.go
File metadata and controls
91 lines (80 loc) · 2.12 KB
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
//go:build amd64 || arm64
package ffmpeg
import (
"sync"
"unsafe"
"github.com/bstkhq/go-ffmpeg-ffi/internal/shim"
)
type ColorRange int32
type ColorSpace int32
type ColorPrimaries int32
type ColorTransfer int32
// Common FFmpeg values (AVColorRange). Kept in sync with libavutil/pixfmt.h.
const (
ColorRangeUnspecified ColorRange = 0
ColorRangeMPEG ColorRange = 1 // limited (16-235)
ColorRangeJPEG ColorRange = 2 // full (0-255)
)
// ColorSpec describes color metadata attached to a frame.
type ColorSpec struct {
Range ColorRange
Space ColorSpace
Primaries ColorPrimaries
Transfer ColorTransfer
}
var (
colorOffOnce sync.Once
colorOffOK bool
offRange int32
offSpace int32
offPrim int32
offTrc int32
)
func ensureColorOffsets() {
colorOffOnce.Do(func() {
_ = shim.Load()
r, s, p, t, err := shim.AVFrameColorOffsets()
if err != nil {
colorOffOK = false
return
}
offRange, offSpace, offPrim, offTrc = r, s, p, t
colorOffOK = true
})
}
// ColorSpec returns the frame's color metadata. If the shim does not provide
// AVFrame color offsets, it returns a zero-value ColorSpec.
func (f Frame) ColorSpec() ColorSpec {
if f.IsNil() {
return ColorSpec{}
}
ensureColorOffsets()
if !colorOffOK {
return ColorSpec{}
}
return ColorSpec{
Range: ColorRange(*(*int32)(unsafe.Add(f.ptr, offRange))),
Space: ColorSpace(*(*int32)(unsafe.Add(f.ptr, offSpace))),
Primaries: ColorPrimaries(*(*int32)(unsafe.Add(f.ptr, offPrim))),
Transfer: ColorTransfer(*(*int32)(unsafe.Add(f.ptr, offTrc))),
}
}
// SetColorSpec sets the frame's color metadata. If the shim does not provide
// AVFrame color offsets, this is a no-op.
func (f Frame) SetColorSpec(spec ColorSpec) {
if f.IsNil() {
return
}
ensureColorOffsets()
if !colorOffOK {
return
}
*(*int32)(unsafe.Add(f.ptr, offRange)) = int32(spec.Range)
*(*int32)(unsafe.Add(f.ptr, offSpace)) = int32(spec.Space)
*(*int32)(unsafe.Add(f.ptr, offPrim)) = int32(spec.Primaries)
*(*int32)(unsafe.Add(f.ptr, offTrc)) = int32(spec.Transfer)
}
func colorOffsetsAvailable() bool {
ensureColorOffsets()
return colorOffOK
}