forked from asticode/go-astiav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame.go
168 lines (132 loc) · 3.21 KB
/
frame.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
package astiav
//#cgo pkg-config: libavutil
//#include <libavutil/frame.h>
//#include <libavutil/samplefmt.h>
import "C"
const NumDataPointers = uint(C.AV_NUM_DATA_POINTERS)
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libavutil/frame.h#L317
type Frame struct {
c *C.struct_AVFrame
}
func newFrameFromC(c *C.struct_AVFrame) *Frame {
if c == nil {
return nil
}
return &Frame{c: c}
}
func AllocFrame() *Frame {
return newFrameFromC(C.av_frame_alloc())
}
func (f *Frame) AllocBuffer(align int) error {
return newError(C.av_frame_get_buffer(f.c, C.int(align)))
}
func (f *Frame) AllocSamples(sf SampleFormat, nbChannels, nbSamples, align int) error {
return newError(C.av_samples_alloc(&f.c.data[0], &f.c.linesize[0], C.int(nbChannels), C.int(nbSamples), (C.enum_AVSampleFormat)(sf), C.int(align)))
}
func (f *Frame) ChannelLayout() ChannelLayout {
return ChannelLayout(f.c.channel_layout)
}
func (f *Frame) SetChannelLayout(l ChannelLayout) {
f.c.channel_layout = C.uint64_t(l)
}
func (f *Frame) Data() [NumDataPointers][]byte {
b := [NumDataPointers][]byte{}
for i := 0; i < int(NumDataPointers); i++ {
b[i] = bytesFromC(func(size *C.int) *C.uint8_t {
*size = f.c.linesize[i]
if f.c.height > 0 {
*size = *size * f.c.height
} else if f.c.channels > 0 {
*size = *size * f.c.channels
}
return f.c.data[i]
})
}
return b
}
func (f *Frame) Height() int {
return int(f.c.height)
}
func (f *Frame) SetHeight(h int) {
f.c.height = C.int(h)
}
func (f *Frame) KeyFrame() bool {
return int(f.c.key_frame) > 0
}
func (f *Frame) SetKeyFrame(k bool) {
i := 0
if k {
i = 1
}
f.c.key_frame = C.int(i)
}
func (f *Frame) Linesize() [NumDataPointers]int {
o := [NumDataPointers]int{}
for i := 0; i < int(NumDataPointers); i++ {
o[i] = int(f.c.linesize[i])
}
return o
}
func (f *Frame) NbSamples() int {
return int(f.c.nb_samples)
}
func (f *Frame) SetNbSamples(n int) {
f.c.nb_samples = C.int(n)
}
func (f *Frame) PictureType() PictureType {
return PictureType(f.c.pict_type)
}
func (f *Frame) SetPictureType(t PictureType) {
f.c.pict_type = C.enum_AVPictureType(t)
}
func (f *Frame) PixelFormat() PixelFormat {
return PixelFormat(f.c.format)
}
func (f *Frame) SetPixelFormat(pf PixelFormat) {
f.c.format = C.int(pf)
}
func (f *Frame) PktPts() int64 {
return int64(f.c.pkt_pts)
}
func (f *Frame) PktDts() int64 {
return int64(f.c.pkt_dts)
}
func (f *Frame) Pts() int64 {
return int64(f.c.pts)
}
func (f *Frame) SetPts(i int64) {
f.c.pts = C.int64_t(i)
}
func (f *Frame) SampleFormat() SampleFormat {
return SampleFormat(f.c.format)
}
func (f *Frame) SetSampleFormat(sf SampleFormat) {
f.c.format = C.int(sf)
}
func (f *Frame) SampleRate() int {
return int(f.c.sample_rate)
}
func (f *Frame) SetSampleRate(r int) {
f.c.sample_rate = C.int(r)
}
func (f *Frame) Width() int {
return int(f.c.width)
}
func (f *Frame) SetWidth(w int) {
f.c.width = C.int(w)
}
func (f *Frame) Free() {
C.av_frame_free(&f.c)
}
func (f *Frame) Ref(src *Frame) error {
return newError(C.av_frame_ref(f.c, src.c))
}
func (f *Frame) Clone() *Frame {
return newFrameFromC(C.av_frame_clone(f.c))
}
func (f *Frame) Unref() {
C.av_frame_unref(f.c)
}
func (f *Frame) MoveRef(src *Frame) {
C.av_frame_move_ref(f.c, src.c)
}