forked from asticode/go-astiav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.go
73 lines (63 loc) · 1.75 KB
/
dictionary.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
package astiav
//#cgo pkg-config: libavcodec libavutil
//#include <libavcodec/avcodec.h>
//#include <libavutil/dict.h>
//#include <stdlib.h>
import "C"
import (
"unsafe"
)
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavutil/dict.h#L84
type Dictionary struct {
c *C.struct_AVDictionary
}
func NewDictionary() *Dictionary {
return &Dictionary{}
}
func newDictionaryFromC(c *C.struct_AVDictionary) *Dictionary {
if c == nil {
return nil
}
return &Dictionary{c: c}
}
func (d *Dictionary) Set(key, value string, flags DictionaryFlags) error {
ck := C.CString(key)
defer C.free(unsafe.Pointer(ck))
cv := C.CString(value)
defer C.free(unsafe.Pointer(cv))
return newError(C.av_dict_set(&d.c, ck, cv, C.int(flags)))
}
func (d *Dictionary) ParseString(i, keyValSep, pairsSep string, flags DictionaryFlags) error {
ci := C.CString(i)
defer C.free(unsafe.Pointer(ci))
ck := C.CString(keyValSep)
defer C.free(unsafe.Pointer(ck))
cp := C.CString(pairsSep)
defer C.free(unsafe.Pointer(cp))
return newError(C.av_dict_parse_string(&d.c, ci, ck, cp, C.int(flags)))
}
func (d *Dictionary) Get(key string, prev *DictionaryEntry, flags DictionaryFlags) *DictionaryEntry {
ck := C.CString(key)
defer C.free(unsafe.Pointer(ck))
var cp *C.struct_AVDictionaryEntry
if prev != nil {
cp = prev.c
}
if e := C.av_dict_get(d.c, ck, cp, C.int(flags)); e != nil {
return newDictionaryEntryFromC(e)
}
return nil
}
func (d *Dictionary) Free() {
C.av_dict_free(&d.c)
}
func (d *Dictionary) Pack() []byte {
return bytesFromC(func(size *C.int) *C.uint8_t {
return C.av_packet_pack_dictionary(d.c, size)
})
}
func (d *Dictionary) Unpack(b []byte) error {
return bytesToC(b, func(b *C.uint8_t, size C.int) error {
return newError(C.av_packet_unpack_dictionary(b, size, &d.c))
})
}