-
Notifications
You must be signed in to change notification settings - Fork 47
/
class.go
134 lines (110 loc) · 2.5 KB
/
class.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
package astiav
//#include "class.h"
import "C"
import (
"fmt"
"sync"
"unsafe"
)
// https://ffmpeg.org/doxygen/7.0/structAVClass.html
type Class struct {
c *C.AVClass
ptr unsafe.Pointer
}
func newClassFromC(ptr unsafe.Pointer) *Class {
if ptr == nil {
return nil
}
c := (**C.AVClass)(ptr)
if c == nil {
return nil
}
return &Class{
c: *c,
ptr: ptr,
}
}
// https://ffmpeg.org/doxygen/7.0/structAVClass.html#a5fc161d93a0d65a608819da20b7203ba
func (c *Class) Category() ClassCategory {
return ClassCategory(C.astiavClassCategory(c.c, c.ptr))
}
// https://ffmpeg.org/doxygen/7.0/structAVClass.html#ad763b2e6a0846234a165e74574a550bd
func (c *Class) ItemName() string {
return C.GoString(C.astiavClassItemName(c.c, c.ptr))
}
// https://ffmpeg.org/doxygen/7.0/structAVClass.html#aa8883e113a3f2965abd008f7667db7eb
func (c *Class) Name() string {
return C.GoString(c.c.class_name)
}
// https://ffmpeg.org/doxygen/7.0/structAVClass.html#a88948c8a7c6515181771615a54a808bf
func (c *Class) Parent() *Class {
return newClassFromC(unsafe.Pointer(C.astiavClassParent(c.c, c.ptr)))
}
func (c *Class) String() string {
return fmt.Sprintf("%s [%s] @ %p", c.ItemName(), c.Name(), c.ptr)
}
type Classer interface {
Class() *Class
}
var _ Classer = (*UnknownClasser)(nil)
type UnknownClasser struct {
c *Class
}
func newUnknownClasser(ptr unsafe.Pointer) *UnknownClasser {
return &UnknownClasser{c: newClassFromC(ptr)}
}
func (c *UnknownClasser) Class() *Class {
return c.c
}
var _ Classer = (*ClonedClasser)(nil)
type ClonedClasser struct {
c *Class
}
func newClonedClasser(c Classer) *ClonedClasser {
cl := c.Class()
if cl == nil {
return nil
}
return &ClonedClasser{c: newClassFromC(cl.ptr)}
}
func (c *ClonedClasser) Class() *Class {
return c.c
}
var classers = newClasserPool()
type classerPool struct {
m sync.Mutex
p map[unsafe.Pointer]Classer
}
func newClasserPool() *classerPool {
return &classerPool{p: make(map[unsafe.Pointer]Classer)}
}
func (p *classerPool) unsafePointer(c Classer) unsafe.Pointer {
if c == nil {
return nil
}
cl := c.Class()
if cl == nil {
return nil
}
return cl.ptr
}
func (p *classerPool) set(c Classer) {
p.m.Lock()
defer p.m.Unlock()
if ptr := p.unsafePointer(c); ptr != nil {
p.p[ptr] = c
}
}
func (p *classerPool) del(c Classer) {
p.m.Lock()
defer p.m.Unlock()
if ptr := p.unsafePointer(c); ptr != nil {
delete(p.p, ptr)
}
}
func (p *classerPool) get(ptr unsafe.Pointer) (Classer, bool) {
p.m.Lock()
defer p.m.Unlock()
c, ok := p.p[ptr]
return c, ok
}